Skip to content

The agent that grows with you

fix(webapp): stream voice in the Webapp on a ticket minted for each voice stream

emozilla7a3dda8242c1
diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.ts
index 6dbbe346151..f85725e1a35 100644
--- a/apps/desktop/src/global.d.ts
+++ b/apps/desktop/src/global.d.ts
@@ -58,6 +58,14 @@ declare global {
pluginId: string
profile?: null | string
}) => Promise<null | string | { refused: 'permission' }>
+ // One-use voice stream socket URL whose ticket opens that stream alone,
+ // for a host whose gateway ticket opens only `/api/ws` (the browser-hosted
+ // Desktop). Absent where the gateway URL's own credential opens the stream.
+ getAudioWsUrl?: (payload: {
+ connectionId?: null | string
+ path: '/api/audio/speak-stream' | '/api/audio/transcribe-stream'
+ profile?: null | string
+ }) => Promise<string>
// Union agent roster across every registered connection.
getAgentRoster?: () => Promise<DesktopAgentRoster>
// Credential-free routes across the union connection registry. The
diff --git a/apps/desktop/src/lib/browser-connection.ts b/apps/desktop/src/lib/browser-connection.ts
index 8707236750e..1c02189d1ed 100644
--- a/apps/desktop/src/lib/browser-connection.ts
+++ b/apps/desktop/src/lib/browser-connection.ts
@@ -3,6 +3,8 @@ import { LOCAL_CONNECTION_ID } from '@hermes/shared'
import type { HermesApiRequest, HermesConnection } from '@/global'
import { webappSignInRequired } from '@/lib/browser-launch-session'
import {
+ type AudioSocketPath,
+ audioWebsocketUrl,
type BrowserBootstrap,
BrowserReauthRequiredError,
endpointUrl,
@@ -77,6 +79,7 @@ interface BrowserConnectionOptions {
export function createBrowserConnectionBridge({ api, bootstrap }: BrowserConnectionOptions): Pick<
Window['hermesDesktop'],
| 'getAgentRoster'
+ | 'getAudioWsUrl'
| 'getConnection'
| 'getConnectionConfig'
| 'getConnectionFor'
@@ -140,6 +143,16 @@ export function createBrowserConnectionBridge({ api, bootstrap }: BrowserConnect
}]
}
},
+ // Each voice stream dial mints its own ticket: the gateway's opens only `/api/ws`.
+ getAudioWsUrl: async (payload: {
+ connectionId?: null | string
+ path: AudioSocketPath
+ profile?: null | string
+ }) => {
+ requireBrowserConnection(payload.connectionId)
+
+ return audioWebsocketUrl(bootstrap, payload.path, payload.profile)
+ },
// Reconnect belongs to the window, not whichever secondary profile is active.
// Explicit null still selects the default backend.
getConnection: async (profile: null | string = windowProfile) => ({
diff --git a/apps/desktop/src/lib/browser-transport.ts b/apps/desktop/src/lib/browser-transport.ts
index 9aef9bf7105..f23ec671d86 100644
--- a/apps/desktop/src/lib/browser-transport.ts
+++ b/apps/desktop/src/lib/browser-transport.ts
@@ -319,3 +319,32 @@ export async function authenticatedWebsocketUrl(
return websocketUrl(bootstrap.basePath, path, { ticket }, profile)
}
+
+/** The voice streams beside `/api/ws`, each opened only by a ticket minted under its socket name. */
+const AUDIO_SOCKETS = {
+ '/api/audio/speak-stream': 'speak-stream',
+ '/api/audio/transcribe-stream': 'transcribe-stream'
+} as const
+
+export type AudioSocketPath = keyof typeof AUDIO_SOCKETS
+
+/**
+ * A voice stream's socket URL for one dial. Its one-use ticket is minted for that stream alone: the
+ * gateway's ticket opens only `/api/ws`, so the stream cannot reuse it.
+ */
+export async function audioWebsocketUrl(
+ bootstrap: BrowserBootstrap,
+ path: AudioSocketPath,
+ profile?: null | string
+): Promise<string> {
+ const result = await browserApi<null | { ticket?: string }>(bootstrap, {
+ method: 'POST',
+ path: `/api/webapp/audio-ticket?socket=${AUDIO_SOCKETS[path]}`
+ })
+
+ const ticket = String(result?.ticket || '').trim()
+
+ if (!ticket) {throw new Error('Hermes did not return a voice stream ticket')}
+
+ return websocketUrl(bootstrap.basePath, path, { ticket }, profile)
+}
diff --git a/apps/desktop/src/lib/browser-voice-sockets.test.ts b/apps/desktop/src/lib/browser-voice-sockets.test.ts
new file mode 100644
index 00000000000..6a3c288e054
--- /dev/null
+++ b/apps/desktop/src/lib/browser-voice-sockets.test.ts
@@ -0,0 +1,116 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { setApiRequestConnection, setApiRequestProfile } from '@/hermes'
+import { clearWebappSession, seedWebappSession } from '@/test/webapp-session'
+
+import { installBrowserDesktopBridge } from './browser-desktop-bridge'
+import { resolveSiblingWsUrl } from './sibling-ws-url'
+import { resolveSpeakStreamUrl } from './voice-playback'
+
+// The browser-hosted Desktop opens each voice stream with a one-use ticket minted for that stream.
+// A socket ticket opens only the socket it names, so the gateway's ticket (named for `/api/ws`)
+// never opens `/api/audio/speak-stream` or `/api/audio/transcribe-stream`; dialing them with it
+// sent streaming voice to its whole-reply fallback every time.
+
+type MutableWindow = Window & {
+ __HERMES_AUTH_REQUIRED__?: boolean
+ __HERMES_BASE_PATH__?: string
+ hermesDesktop?: Window['hermesDesktop']
+}
+
+const mutableWindow = () => window as unknown as MutableWindow
+
+function serveTickets() {
+ const fetchMock = vi.fn(async (input: RequestInfo | URL, _init?: RequestInit) => {
+ const url = new URL(String(input))
+
+ const ticket = url.pathname.endsWith('/api/webapp/audio-ticket')
+ ? `${url.searchParams.get('socket')}-ticket`
+ : url.pathname.endsWith('/api/webapp/gateway-ticket')
+ ? 'gateway-ticket'
+ : ''
+
+ return new Response(JSON.stringify(ticket ? { ticket, ttl_seconds: 60 } : {}), {
+ headers: { 'content-type': 'application/json' },
+ status: ticket ? 200 : 404
+ })
+ })
+
+ vi.stubGlobal('fetch', fetchMock)
+
+ return fetchMock
+}
+
+const minted = (fetchMock: ReturnType<typeof serveTickets>) =>
+ fetchMock.mock.calls.map(([input, init]) => ({ init, url: new URL(String(input)) }))
+
+afterEach(() => {
+ const win = mutableWindow()
+ delete win.__HERMES_AUTH_REQUIRED__
+ delete win.__HERMES_BASE_PATH__
+ clearWebappSession()
+ Reflect.deleteProperty(win, 'hermesDesktop')
+ document.documentElement.removeAttribute('data-hermes-desktop-host')
+ setApiRequestConnection(null)
+ setApiRequestProfile(null)
+ vi.unstubAllGlobals()
+ vi.restoreAllMocks()
+})
+
+describe('browser-hosted Desktop voice streams', () => {
+ it('speaks on a ticket minted for the speech stream, never the gateway ticket', async () => {
+ const win = mutableWindow()
+ win.__HERMES_AUTH_REQUIRED__ = true
+ win.__HERMES_BASE_PATH__ = '/hermes'
+ const fetchMock = serveTickets()
+ expect(installBrowserDesktopBridge()).toBe(true)
+ setApiRequestProfile('worker-a')
+
+ const url = new URL((await resolveSpeakStreamUrl())!)
+
+ expect(url.pathname).toBe('/hermes/api/audio/speak-stream')
+ expect(url.searchParams.get('ticket')).toBe('speak-stream-ticket')
+ expect(url.searchParams.get('profile')).toBe('worker-a')
+ expect(url.searchParams.has('token')).toBe(false)
+
+ const [mint, ...others] = minted(fetchMock)
+ expect(others).toEqual([])
+ expect(mint.url.pathname).toBe('/hermes/api/webapp/audio-ticket')
+ expect(mint.url.searchParams.get('socket')).toBe('speak-stream')
+ expect(mint.init?.method).toBe('POST')
+ expect(mint.init?.credentials).toBe('same-origin')
+ })
+
+ it('dictates on a ticket minted for the dictation stream, the session token kept out of the URL', async () => {
+ seedWebappSession('served-token')
+ const fetchMock = serveTickets()
+ expect(installBrowserDesktopBridge()).toBe(true)
+
+ const url = new URL(await resolveSiblingWsUrl({ profile: 'worker-a' }, '/api/audio/transcribe-stream'))
+
+ expect(url.pathname).toBe('/api/audio/transcribe-stream')
+ expect(url.searchParams.get('ticket')).toBe('transcribe-stream-ticket')
+ expect(url.searchParams.get('profile')).toBe('worker-a')
+ expect(url.toString()).not.toContain('served-token')
+
+ const [mint, ...others] = minted(fetchMock)
+ expect(others).toEqual([])
+ expect(mint.url.pathname).toBe('/api/webapp/audio-ticket')
+ expect(mint.url.searchParams.get('socket')).toBe('transcribe-stream')
+ expect(new Headers(mint.init?.headers).get('X-Hermes-Session-Token')).toBe('served-token')
+ })
+
+ it('leaves the gateway socket on its own ticket', async () => {
+ const win = mutableWindow()
+ win.__HERMES_AUTH_REQUIRED__ = true
+ const fetchMock = serveTickets()
+ expect(installBrowserDesktopBridge()).toBe(true)
+
+ const result = await win.hermesDesktop!.getGatewayWsUrl()
+ const url = new URL(typeof result === 'string' ? result : result.ok ? result.wsUrl : '')
+
+ expect(url.pathname).toBe('/api/ws')
+ expect(url.searchParams.get('ticket')).toBe('gateway-ticket')
+ expect(minted(fetchMock).map(({ url }) => url.pathname)).toEqual(['/api/webapp/gateway-ticket'])
+ })
+})
diff --git a/apps/desktop/src/lib/sibling-ws-url.ts b/apps/desktop/src/lib/sibling-ws-url.ts
index 0157ea0de89..58173060992 100644
--- a/apps/desktop/src/lib/sibling-ws-url.ts
+++ b/apps/desktop/src/lib/sibling-ws-url.ts
@@ -36,6 +36,11 @@ export interface SiblingWsRoute {
profile?: null | string
}
+type AudioSocketPath = '/api/audio/speak-stream' | '/api/audio/transcribe-stream'
+
+const isAudioSocketPath = (path: string): path is AudioSocketPath =>
+ path === '/api/audio/speak-stream' || path === '/api/audio/transcribe-stream'
+
/**
* Resolve `ws(s)://<gateway>/<path>` for `route`. `stripGatewayCredential`
* drops the `?ticket=`/`?token=` the gateway URL carried when the sibling route
@@ -56,6 +61,15 @@ export async function resolveSiblingWsUrl(
const connectionId = route.connectionId?.trim() || null
const profile = route.profile?.trim() || null
+ // A host whose gateway ticket opens only `/api/ws` mints a voice stream its own ticket.
+ if (desktop.getAudioWsUrl && isAudioSocketPath(path)) {
+ return withTimeout(
+ desktop.getAudioWsUrl({ connectionId, path, profile }),
+ RESOLVE_TIMEOUT_MS,
+ 'Timed out minting the voice stream WebSocket URL'
+ )
+ }
+
const conn =
connectionId && desktop.getConnectionFor
? await withTimeout(
diff --git a/apps/desktop/src/lib/voice-playback.ts b/apps/desktop/src/lib/voice-playback.ts
index 94a59f15990..af005a1e136 100644
--- a/apps/desktop/src/lib/voice-playback.ts
+++ b/apps/desktop/src/lib/voice-playback.ts
@@ -146,6 +146,15 @@ export async function resolveSpeakStreamUrl(owner?: OwnerScope): Promise<null |
const profile = owner?.profile || getApiRequestProfile()
const connectionId = owner?.connectionId || getApiRequestConnection()
+ // A host whose gateway ticket opens only `/api/ws` mints the stream its own ticket.
+ if (desktop.getAudioWsUrl) {
+ return await withTimeout(
+ desktop.getAudioWsUrl({ connectionId, path: '/api/audio/speak-stream', profile }),
+ RECONNECT_ATTEMPT_TIMEOUT_MS,
+ `Timed out minting the speech stream URL for profile "${profile}"`
+ )
+ }
+
// Both awaits below are IPC round-trips into the main process with no
// timeout of their own (#93454) — a wedged main-process round-trip
// otherwise hangs voice mode's "speaking" state forever instead of
diff --git a/hermes_cli/web_routers/audio.py b/hermes_cli/web_routers/audio.py
index c4300d7f935..2d38b738762 100644
--- a/hermes_cli/web_routers/audio.py
+++ b/hermes_cli/web_routers/audio.py
@@ -21,6 +21,7 @@ from hermes_cli.web_routers._common import http_failure
from hermes_cli.web_deps import late
from hermes_cli.web_server_chat import _ws_auth_ok, _ws_request_is_allowed
from hermes_cli.web_server_gateway import _read_dashboard_json_response, _split_text_for_speak_stream
+from hermes_cli.web_session_sockets import SPEAK_STREAM_SOCKET, TRANSCRIBE_STREAM_SOCKET
from fastapi import HTTPException, WebSocket, WebSocketDisconnect
from hermes_cli.web_models import (
AudioTranscriptionRequest,
@@ -525,7 +526,8 @@ async def transcribe_stream_ws(ws: WebSocket) -> None:
``{"type": "error", "message": ...}`` — also when no live STT is available, so the
client falls back to ``POST /api/audio/transcribe`` with its recorded blob.
"""
- if not _ws_auth_ok(ws):
+ # A socket ticket opens this stream only when minted for it (``/api/webapp/audio-ticket``).
+ if not _ws_auth_ok(ws, socket=TRANSCRIBE_STREAM_SOCKET):
await ws.close(code=4401)
return
if not _ws_request_is_allowed(ws):
@@ -611,7 +613,8 @@ async def speak_stream_ws(ws: WebSocket) -> None:
still speak per sentence via ``text_to_speech_tool`` and stream
that PCM — the client POST is the last resort, not the Edge path.
"""
- if not _ws_auth_ok(ws):
+ # A socket ticket opens this stream only when minted for it (``/api/webapp/audio-ticket``).
+ if not _ws_auth_ok(ws, socket=SPEAK_STREAM_SOCKET):
await ws.close(code=4401)
return
if not _ws_request_is_allowed(ws):
diff --git a/hermes_cli/web_routers/webapp.py b/hermes_cli/web_routers/webapp.py
index 8a77dca3502..6b95bb5a3b1 100644
--- a/hermes_cli/web_routers/webapp.py
+++ b/hermes_cli/web_routers/webapp.py
@@ -1,8 +1,8 @@
"""Webapp-only routes: the host terminal (loopback or authenticated binds), the
one-use ticket each browser socket dial mints (``/api/webapp/gateway-ticket`` for
-``/api/ws`` on this same origin, ``/api/webapp/terminal-ticket`` for the host terminal),
-and the one-use handoffs (child window, auto-opened launch tab) for the private, loopback
-Webapp session."""
+``/api/ws`` on this same origin, ``/api/webapp/terminal-ticket`` for the host terminal,
+``/api/webapp/audio-ticket`` for each voice stream), and the one-use handoffs (child window,
+auto-opened launch tab) for the private, loopback Webapp session."""
from __future__ import annotations
import ipaddress
@@ -19,7 +19,7 @@ from fastapi.responses import HTMLResponse, JSONResponse
from hermes_cli.dashboard_auth.request_utils import _http_origin
from hermes_cli.web_server_surface import policy, private_launch
-from hermes_cli.web_session_sockets import GATEWAY_SOCKET, HOST_TERMINAL_SOCKET
+from hermes_cli.web_session_sockets import AUDIO_SOCKETS, GATEWAY_SOCKET, HOST_TERMINAL_SOCKET
_log = logging.getLogger("hermes_cli.web_server")
router = APIRouter()
@@ -177,11 +177,11 @@ def _ticket_principal(request: Request) -> tuple[dict, str | None]:
def mint_socket_ticket(identity: dict, socket: str, *, session_token: str | None = None,
sign_in: str | None = None) -> str:
- """A one-use ticket that opens only *socket* (``web_session_sockets.GATEWAY_SOCKET`` or
- ``HOST_TERMINAL_SOCKET``) as *identity*. Minted with *session_token*, or by a login whose
- sign-in binding was checked against the credential with digest *sign_in*
- (``hermes_cli.webapp_sign_in``), it opens nothing once that credential is replaced, and the
- socket it opens closes when the browser signs out."""
+ """A one-use ticket that opens only *socket* (``web_session_sockets.GATEWAY_SOCKET``,
+ ``HOST_TERMINAL_SOCKET`` or one of ``AUDIO_SOCKETS``) as *identity*. Minted with
+ *session_token*, or by a login whose sign-in binding was checked against the credential with
+ digest *sign_in* (``hermes_cli.webapp_sign_in``), it opens nothing once that credential is
+ replaced, and the socket it opens closes when the browser signs out."""
from hermes_cli.dashboard_auth.ws_tickets import mint_ticket
from hermes_cli.web_session_sockets import token_digest
@@ -229,6 +229,20 @@ async def issue_terminal_ticket(request: Request):
return _socket_ticket_response(request, identity, HOST_TERMINAL_SOCKET, session_token)
[email protected]("/api/webapp/audio-ticket")
+async def issue_audio_ticket(request: Request, socket: str = ""):
+ """The browser's one-use ticket for one voice stream dial, named by *socket*:
+ ``speak-stream`` for ``/api/audio/speak-stream``, ``transcribe-stream`` for
+ ``/api/audio/transcribe-stream``. It opens that stream and nothing else, and the gateway ticket
+ opens neither stream, so the browser mints one per dial beside the gateway's."""
+ if not policy(request.app.state).gateway_ticket:
+ raise HTTPException(404, "Webapp voice tickets are unavailable on this surface")
+ identity, session_token = _ticket_principal(request)
+ if socket not in AUDIO_SOCKETS:
+ raise HTTPException(400, "Unknown voice stream")
+ return _socket_ticket_response(request, identity, socket, session_token)
+
+
@router.post("/webapp/window-session")
async def redeem_window_ticket(request: Request):
from hermes_cli.web_server import _SESSION_TOKEN
diff --git a/hermes_cli/web_server_chat.py b/hermes_cli/web_server_chat.py
index 923676e8f2a..60b81d640e7 100644
--- a/hermes_cli/web_server_chat.py
+++ b/hermes_cli/web_server_chat.py
@@ -207,8 +207,8 @@ def _ws_auth_reason(ws: WebSocket, *, allow_internal: bool = False,
``reason`` is None when accepted, else a short token (``no_credential``,
``token_mismatch``, ``ticket_invalid``, ``internal_invalid``);
``credential`` names what was presented so the accept path can log *how*.
- *socket* names the route's socket for its own tickets
- (``web_session_sockets.GATEWAY_SOCKET`` / ``HOST_TERMINAL_SOCKET``): a ticket minted for
+ *socket* names the route's socket for its own tickets (``web_session_sockets.GATEWAY_SOCKET``,
+ ``HOST_TERMINAL_SOCKET`` or one of ``AUDIO_SOCKETS``): a ticket minted for
one socket opens no other, and the host terminal opens only with its own ticket.
Loopback / ``--insecure``: legacy ``?token=`` (constant-time compared).
diff --git a/hermes_cli/web_session_sockets.py b/hermes_cli/web_session_sockets.py
index 2c9c650b71b..5370904e55c 100644
--- a/hermes_cli/web_session_sockets.py
+++ b/hermes_cli/web_session_sockets.py
@@ -1,10 +1,11 @@
"""Browser sockets the dashboard session token opens, and how they end when it is replaced.
A browser on the loopback Webapp never puts the session token in a socket URL. An authenticated
-request mints a one-use ticket for one socket instead: ``GATEWAY_SOCKET`` (``/api/ws``) or
-``HOST_TERMINAL_SOCKET`` (``/api/host-terminal``). A ticket named for one socket opens no other, and
-the host terminal opens only with its own ticket. A ticket minted with the session token records that
-token's digest and opens nothing once the token is replaced.
+request mints a one-use ticket for one socket instead: ``GATEWAY_SOCKET`` (``/api/ws``),
+``HOST_TERMINAL_SOCKET`` (``/api/host-terminal``) or one of ``AUDIO_SOCKETS`` (the voice streams). A
+ticket named for one socket opens no other, and the host terminal opens only with its own ticket. A
+ticket minted with the session token records that token's digest and opens nothing once the token is
+replaced.
Behind the sign-in gate the Webapp's browsers sign in instead, and each login is bound to the session
token (``hermes_cli.webapp_sign_in``): a ticket such a login mints records the token's digest as
@@ -32,6 +33,10 @@ _log = logging.getLogger(__name__)
GATEWAY_SOCKET = "gateway"
HOST_TERMINAL_SOCKET = "host-terminal"
+#: The voice streams beside ``/api/ws``, each named for its route ``/api/audio/<name>``.
+SPEAK_STREAM_SOCKET = "speak-stream"
+TRANSCRIBE_STREAM_SOCKET = "transcribe-stream"
+AUDIO_SOCKETS = frozenset({SPEAK_STREAM_SOCKET, TRANSCRIBE_STREAM_SOCKET})
#: Sockets that open only with a ticket minted for them, never with a standing credential.
OWN_TICKET_SOCKETS = frozenset({HOST_TERMINAL_SOCKET})
#: What a signed-out tab's sockets are closed with: the same code a refused credential gets.
diff --git a/tests/hermes_cli/test_webapp_socket_tickets.py b/tests/hermes_cli/test_webapp_socket_tickets.py
index 168a56fedd4..58aedcc2aa7 100644
--- a/tests/hermes_cli/test_webapp_socket_tickets.py
+++ b/tests/hermes_cli/test_webapp_socket_tickets.py
@@ -1,11 +1,15 @@
"""Each Webapp socket ticket opens only the socket it was minted for.
The browser never puts its session token in a socket URL. An authenticated request mints a one-use,
-short-lived ticket instead: ``POST /api/webapp/gateway-ticket`` for the chat socket (``/api/ws``) and
-``POST /api/webapp/terminal-ticket`` for the host terminal (``/api/host-terminal``). A host shell opens
-only with a terminal ticket, a chat ticket opens no host shell, a terminal ticket opens no chat socket,
-and a ticket minted with a session token that has since been replaced opens nothing.
+short-lived ticket instead: ``POST /api/webapp/gateway-ticket`` for the chat socket (``/api/ws``),
+``POST /api/webapp/terminal-ticket`` for the host terminal (``/api/host-terminal``) and
+``POST /api/webapp/audio-ticket?socket=`` for each voice stream (``/api/audio/speak-stream``,
+``/api/audio/transcribe-stream``). A host shell opens only with a terminal ticket, a chat ticket opens
+no host shell, a terminal ticket opens no chat socket, a voice stream opens with a ticket minted for
+that stream and no other, and a ticket minted with a session token that has since been replaced opens
+nothing.
"""
+import json
from pathlib import Path
from types import SimpleNamespace
@@ -154,3 +158,131 @@ def test_a_signed_in_browser_opens_its_host_shell_with_a_terminal_ticket(webapp,
finally:
clear_providers()
_reset_for_tests()
+
+
+_SPEAK = "/api/audio/speak-stream"
+_TRANSCRIBE = "/api/audio/transcribe-stream"
+
+
+def _mint_audio(client, socket, headers=_SESSION):
+ response = client.post("/api/webapp/audio-ticket", params={"socket": socket}, headers=headers)
+ assert response.status_code == 200, response.text
+ assert response.headers["cache-control"] == "no-store"
+ body = response.json()
+ assert set(body) == {"ticket", "ttl_seconds"} and 0 < body["ttl_seconds"] <= 60
+ return body["ticket"]
+
+
+def voice(monkeypatch):
+ """Voice streams that answer once they admit a socket: speech yields one PCM frame, and live
+ dictation reports that no live STT is configured."""
+ class Streamer:
+ sample_rate = 24000
+ channels = 1
+
+ def stream(self, _text):
+ yield b"\x01\x02"
+
+ monkeypatch.setattr("tools.tts_streaming.resolve_streaming_provider", lambda cfg: Streamer())
+ monkeypatch.setattr("tools.tts_tool._load_tts_config", dict)
+ monkeypatch.setattr("tools.tts_tool._get_provider", lambda cfg: "fake")
+ monkeypatch.setattr("tools.tts_tool._resolve_max_text_length", lambda provider, cfg: 4000)
+ monkeypatch.setattr("tools.transcription_streaming.open_streaming_session", lambda **_kwargs: None)
+
+
+def _speaks(client, url):
+ with client.websocket_connect(url) as ws:
+ ws.send_text(json.dumps({"text": "Hello there.", "done": True}))
+ assert ws.receive_json() == {"type": "start", "sample_rate": 24000, "channels": 1}
+
+
+def _transcribes(client, url):
+ with client.websocket_connect(url) as ws:
+ # Admitted: the stream answers, here that no live STT is configured.
+ assert ws.receive_json()["type"] == "error"
+
+
+def test_each_voice_stream_opens_once_with_a_ticket_minted_for_it(webapp, voice):
+ client = webapp.client
+ assert client.post("/api/webapp/audio-ticket", params={"socket": "speak-stream"}).status_code == 401
+ speak = _mint_audio(client, "speak-stream")
+ _speaks(client, f"ws://localhost{_SPEAK}?ticket={speak}")
+ _refused(client, f"ws://localhost{_SPEAK}?ticket={speak}")
+ transcribe = _mint_audio(client, "transcribe-stream")
+ _transcribes(client, f"ws://localhost{_TRANSCRIBE}?ticket={transcribe}")
+ _refused(client, f"ws://localhost{_TRANSCRIBE}?ticket={transcribe}")
+
+
+def test_a_voice_stream_refuses_a_ticket_minted_for_another_socket(webapp, voice):
+ """The chat socket's ticket, the host terminal's and the other voice stream's open neither voice
+ stream; a voice stream's ticket opens no chat socket and no host shell."""
+ client, host = webapp.client, webapp.host
+ for url, other in ((_SPEAK, "transcribe-stream"), (_TRANSCRIBE, "speak-stream")):
+ for ticket in (_mint(client, "gateway"), _mint(client, "terminal"), _mint_audio(client, other)):
+ _refused(client, f"ws://localhost{url}?ticket={ticket}")
+ _refused(client, f"ws://localhost/api/ws?ticket={_mint_audio(client, 'speak-stream')}")
+ protocols = ["hermes-gateway-v1", f"hermes-gateway-ticket.{_mint_audio(client, 'transcribe-stream')}"]
+ _refused(client, "ws://localhost/api/ws?native_dial=browser", subprotocols=protocols)
+ with TestClient(host) as shells:
+ _refused(shells, f"ws://localhost/api/host-terminal?ticket={_mint_audio(client, 'speak-stream')}")
+ assert webapp.shells == []
+
+
+def test_voice_tickets_minted_before_the_session_token_changed_open_nothing(webapp, voice, monkeypatch):
+ server, client = webapp.server, webapp.client
+ speak, transcribe = _mint_audio(client, "speak-stream"), _mint_audio(client, "transcribe-stream")
+ monkeypatch.setattr(server, "_SESSION_TOKEN", "b" * 43)
+ _refused(client, f"ws://localhost{_SPEAK}?ticket={speak}")
+ _refused(client, f"ws://localhost{_TRANSCRIBE}?ticket={transcribe}")
+
+
+def test_audio_tickets_are_minted_only_for_voice_streams_on_the_webapp(webapp, monkeypatch):
+ server, client = webapp.server, webapp.client
+ for socket in ("gateway", "host-terminal", "display", ""):
+ response = client.post("/api/webapp/audio-ticket", params={"socket": socket}, headers=_SESSION)
+ assert response.status_code == 400, socket
+ for surface in ("dashboard", "serve"):
+ monkeypatch.setattr(server.app.state, "ui_surface", surface)
+ response = client.post("/api/webapp/audio-ticket", params={"socket": "speak-stream"}, headers=_SESSION)
+ assert response.status_code == 404, surface
+
+
+def test_a_native_clients_unnamed_ticket_still_opens_a_voice_stream(webapp, voice):
+ """A ticket named for no socket, as ``/api/auth/ws-ticket`` mints for native Desktop, opens a
+ voice stream as it did before the streams had names."""
+ from hermes_cli.dashboard_auth.ws_tickets import mint_ticket
+
+ client = webapp.client
+ _speaks(client, f"ws://localhost{_SPEAK}?ticket={mint_ticket(user_id='native', provider='stub')}")
+ _transcribes(client, f"ws://localhost{_TRANSCRIBE}?ticket={mint_ticket(user_id='native', provider='stub')}")
+
+
+def test_a_signed_in_browser_opens_its_voice_streams_with_their_own_tickets(webapp, voice, monkeypatch):
+ """Wildcard host: the sign-in cookie mints each voice stream's ticket; the chat socket's does not
+ open one."""
+ from hermes_cli.dashboard_auth import clear_providers, register_provider
+ from hermes_cli.dashboard_auth.ws_tickets import _reset_for_tests
+ from tests.hermes_cli.conftest_dashboard_auth import StubAuthProvider
+ from tests.hermes_cli.test_dashboard_auth_ws_auth import _logged_in
+
+ server = webapp.server
+ _reset_for_tests()
+ clear_providers()
+ register_provider(StubAuthProvider())
+ monkeypatch.setattr(server.app.state, "bound_host", "fly-app.fly.dev")
+ monkeypatch.setattr(server.app.state, "bound_port", 443, raising=False)
+ monkeypatch.setattr(server.app.state, "auth_required", True)
+ try:
+ anonymous = TestClient(server.app, base_url="https://fly-app.fly.dev")
+ assert anonymous.post("/api/webapp/audio-ticket", params={"socket": "speak-stream"},
+ follow_redirects=False).status_code in (302, 401)
+ gated = TestClient(server.app, base_url="https://fly-app.fly.dev")
+ _logged_in(gated)
+ _speaks(gated, f"wss://fly-app.fly.dev{_SPEAK}?ticket={_mint_audio(gated, 'speak-stream', headers={})}")
+ _transcribes(
+ gated, f"wss://fly-app.fly.dev{_TRANSCRIBE}?ticket={_mint_audio(gated, 'transcribe-stream', headers={})}")
+ _refused(gated, f"wss://fly-app.fly.dev{_SPEAK}?ticket={_mint(gated, 'gateway', headers={})}")
+ finally:
+ clear_providers()
+ _reset_for_tests()