diff --git a/apps/desktop/src/lib/voice-playback.routing.test.ts b/apps/desktop/src/lib/voice-playback.routing.test.ts new file mode 100644 index 0000000000..682870acf6 --- /dev/null +++ b/apps/desktop/src/lib/voice-playback.routing.test.ts @@ -0,0 +1,105 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import { setApiRequestConnection, setApiRequestProfile } from '@/hermes' + +import { resolveSpeakStreamUrl } from './voice-playback' + +// The speak-stream WebSocket must dial the ACTIVE (connection, profile) +// backend — the same one chat and every REST audio call use. Before this +// contract was pinned it resolved through the bare v1 getConnection path, +// so a registry remote riding over a local install synthesized replies with +// the LOCAL machine's (often unconfigured) TTS while chat correctly went +// remote (desktop-remote voice report, Aug 2026). +describe('resolveSpeakStreamUrl', () => { + const remoteWsUrl = 'wss://gateway.example/api/ws?ticket=fresh' + const localWsUrl = 'ws://127.0.0.1:5151/api/ws?token=local' + + let getConnection: ReturnType + let getConnectionFor: ReturnType + let getGatewayWsUrl: ReturnType + let getGatewayWsUrlFor: ReturnType + + beforeEach(() => { + getConnection = vi.fn(async () => ({ authMode: 'token', baseUrl: 'http://127.0.0.1:5151', wsUrl: localWsUrl })) + + getConnectionFor = vi.fn(async () => ({ + authMode: 'token', + baseUrl: 'https://gateway.example', + wsUrl: remoteWsUrl + })) + + getGatewayWsUrl = vi.fn(async () => ({ ok: true, wsUrl: localWsUrl })) + getGatewayWsUrlFor = vi.fn(async () => ({ ok: true, wsUrl: remoteWsUrl })) + + Object.defineProperty(window, 'hermesDesktop', { + configurable: true, + value: { getConnection, getConnectionFor, getGatewayWsUrl, getGatewayWsUrlFor } + }) + }) + + afterEach(() => { + setApiRequestConnection(null) + setApiRequestProfile(null) + Reflect.deleteProperty(window, 'hermesDesktop') + }) + + it('resolves through the registry (connection, profile) bridges when a registry connection is active', async () => { + setApiRequestConnection('gw-tailscale') + setApiRequestProfile('research') + + const url = await resolveSpeakStreamUrl() + + expect(url).toContain('wss://gateway.example') + expect(url).toContain('/api/audio/speak-stream') + expect(getConnectionFor).toHaveBeenCalledWith({ connectionId: 'gw-tailscale', profile: 'research' }) + expect(getGatewayWsUrlFor).toHaveBeenCalledWith({ connectionId: 'gw-tailscale', profile: 'research' }) + // The v1 primary path must NOT be consulted — that's the local machine. + expect(getConnection).not.toHaveBeenCalled() + expect(getGatewayWsUrl).not.toHaveBeenCalled() + }) + + it('keeps the legacy profile path byte-identical when no registry connection is active', async () => { + setApiRequestProfile('coder') + + const url = await resolveSpeakStreamUrl() + + expect(url).toContain('ws://127.0.0.1:5151') + expect(url).toContain('/api/audio/speak-stream') + expect(url).toContain('profile=coder') + expect(getConnection).toHaveBeenCalledWith('coder') + expect(getConnectionFor).not.toHaveBeenCalled() + }) + + it('preserves a backend-namespace profile already minted into the ws URL', async () => { + // SSH remoteProfile aliasing / sharedRemote scoping: the registry mint + // writes the BACKEND's profile name into the URL. The desktop-side + // routing alias must not overwrite it. + setApiRequestConnection('gw-ssh') + setApiRequestProfile('mara') + getGatewayWsUrlFor.mockResolvedValue({ + ok: true, + wsUrl: 'wss://gateway.example/api/ws?ticket=fresh&profile=default' + }) + + const url = await resolveSpeakStreamUrl() + + expect(url).toContain('profile=default') + expect(url).not.toContain('profile=mara') + }) + + it('falls back to the plain connection descriptor when the *For bridges are absent (older main)', async () => { + setApiRequestConnection('gw-tailscale') + setApiRequestProfile('research') + Object.defineProperty(window, 'hermesDesktop', { + configurable: true, + value: { getConnection, getGatewayWsUrl } + }) + + const url = await resolveSpeakStreamUrl() + + // Best available answer without the bridges: the profile-scoped pool + // descriptor's own wsUrl (no cross-scope re-mint). + expect(url).toContain('/api/audio/speak-stream') + expect(getConnection).toHaveBeenCalledWith('research') + }) +}) diff --git a/apps/desktop/src/lib/voice-playback.ts b/apps/desktop/src/lib/voice-playback.ts index 315e1108b0..eb64c4feb3 100644 --- a/apps/desktop/src/lib/voice-playback.ts +++ b/apps/desktop/src/lib/voice-playback.ts @@ -1,6 +1,6 @@ import { resolveGatewayWsUrl } from '@hermes/shared' -import { getApiRequestProfile, speakText } from '@/hermes' +import { getApiRequestConnection, getApiRequestProfile, speakText } from '@/hermes' import { cutSentences, directTtsConfig, @@ -102,7 +102,9 @@ export function stopVoicePlayback() { // instead of after full synthesis + base64 transfer. // --------------------------------------------------------------------------- -async function resolveSpeakStreamUrl(): Promise { +/** Exported for tests: the (connection, profile) routing contract below is + * exactly what broke in the desktop-remote voice report — keep it pinned. */ +export async function resolveSpeakStreamUrl(): Promise { const desktop = window.hermesDesktop if (!desktop?.getConnection) { @@ -111,10 +113,31 @@ async function resolveSpeakStreamUrl(): Promise { try { // Mint a fresh credential (single-use ticket in OAuth mode) for the - // ACTIVE profile's backend, then swap the gateway endpoint for the PCM - // one — auth is shared across WS routes. + // ACTIVE (connection, profile) backend, then swap the gateway endpoint + // for the PCM one — auth is shared across WS routes. A registry-scoped + // remote MUST resolve through the *For bridges (same seam as + // store/gateway's openSecondary): the bare getConnection/getGatewayWsUrl + // pair answers for the v1 primary backend, which — when a registry + // remote rides over a local install — is the LOCAL machine, so spoken + // replies would synthesize with the local (often unconfigured) TTS + // instead of the profile the user is actually talking to (#90051-adjacent + // desktop-remote voice report, Aug 2026). const profile = getApiRequestProfile() - const wsUrl = await resolveGatewayWsUrl(desktop, await desktop.getConnection(profile)) + const connectionId = getApiRequestConnection() + + const conn = + connectionId && desktop.getConnectionFor + ? await desktop.getConnectionFor({ connectionId, profile }) + : await desktop.getConnection(profile) + + const wsDeps = + connectionId && desktop.getGatewayWsUrlFor + ? { getGatewayWsUrl: () => desktop.getGatewayWsUrlFor!({ connectionId, profile }) } + : connectionId + ? {} + : desktop + + const wsUrl = await resolveGatewayWsUrl(wsDeps, conn) const url = new URL(wsUrl) if (!url.pathname.endsWith('/api/ws')) { @@ -124,8 +147,11 @@ async function resolveSpeakStreamUrl(): Promise { url.pathname = url.pathname.replace(/\/api\/ws$/, '/api/audio/speak-stream') // The backend resolves the TTS provider chain from this profile's - // config/.env (same seam as /api/pty?profile=). - if (profile) { + // config/.env (same seam as /api/pty?profile=). A registry-minted URL may + // already carry the BACKEND-namespace profile (sharedRemote scoping, SSH + // remoteProfile aliasing) — never overwrite it with the desktop-side + // routing alias. + if (profile && !url.searchParams.has('profile')) { url.searchParams.set('profile', profile) }