The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): drain every live voice playback on stop
Brooklyn Nicholson298a01c79b38
diff --git a/apps/desktop/src/lib/voice-playback.test.ts b/apps/desktop/src/lib/voice-playback.test.tsnew file mode 100644index 00000000000..5abcde247e3--- /dev/null+++ b/apps/desktop/src/lib/voice-playback.test.ts@@ -0,0 +1,302 @@+// Teardown contract for voice-playback (#91991): a barge-in (stopVoicePlayback)+// must drain EVERY live playback — streaming sessions (WebSocket + AudioContext)+// and data-URL audio elements — and a settled session must never resume or+// write stale state into a newer turn.++import type * as SharedTypes from '@hermes/shared'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++import { $voicePlayback } from '@/store/voice-playback'++import { playSpeechText, startSpeechStream, stopVoicePlayback } from './voice-playback'++const gateway = vi.hoisted(() => {+ let nextUrl: string | null = 'ws://gateway.test/api/ws'++ return {+ currentUrl: () => nextUrl,+ setNextUrl: (url: string | null) => {+ nextUrl = url+ }+ }+})++vi.mock('@hermes/shared', async importOriginal => {+ const actual = await importOriginal<typeof SharedTypes>()++ return {+ ...actual,+ resolveGatewayWsUrl: async () => gateway.currentUrl()+ }+})++vi.mock('@/hermes', () => ({+ getApiRequestProfile: () => null,+ getApiRequestConnection: () => null,+ speakText: vi.fn(async () => ({+ data_url: 'data:audio/mpeg;base64,AAAA',+ mime_type: 'audio/mpeg',+ ok: true+ }))+}))++class FakeSocket {+ static readonly OPEN = 1+ static readonly CONNECTING = 0+ static instances: FakeSocket[] = []++ binaryType = 'blob'+ readyState = FakeSocket.CONNECTING+ sent: string[] = []+ url: string++ close = vi.fn(() => {+ this.readyState = 3+ })+ send = vi.fn((data: string) => {+ this.sent.push(data)+ })++ onopen: (() => void) | null = null+ onmessage: ((event: { data: string | ArrayBuffer }) => void) | null = null+ onclose: (() => void) | null = null+ onerror: (() => void) | null = null++ constructor(url: string) {+ this.url = url+ FakeSocket.instances.push(this)+ }++ serverOpen() {+ this.readyState = FakeSocket.OPEN+ this.onopen?.()+ }++ serverFrame(frame: object) {+ this.onmessage?.({ data: JSON.stringify(frame) })+ }++ serverPcm(bytes: number) {+ this.onmessage?.({ data: new ArrayBuffer(bytes) })+ }++ serverClose() {+ this.onclose?.()+ }++ serverError() {+ this.onerror?.()+ }+}++class FakeAudioContext {+ static instances: FakeAudioContext[] = []++ closed = false+ currentTime = 0+ destination = {}+ state = 'running'++ close = vi.fn(() => {+ this.closed = true++ return Promise.resolve()+ })+ resume = vi.fn(() => Promise.resolve())+ createBuffer = vi.fn((_channels: number, length: number, rate: number) => ({+ duration: length / rate,+ getChannelData: () => new Float32Array(length)+ }))+ createBufferSource = vi.fn(() => ({+ buffer: null as unknown,+ connect: vi.fn(),+ start: vi.fn()+ }))++ constructor() {+ FakeAudioContext.instances.push(this)+ }+}++class FakeAudio {+ static instances: FakeAudio[] = []++ paused = false+ src: string++ addEventListener = vi.fn()+ load = vi.fn()+ pause = vi.fn(() => {+ this.paused = true+ })+ play = vi.fn(() => {+ this.paused = false++ return Promise.resolve()+ })+ removeEventListener = vi.fn()++ constructor(src: string) {+ this.src = src+ FakeAudio.instances.push(this)+ }+}++async function liveSession() {+ const session = await startSpeechStream({ source: 'voice-conversation' })++ if (!session) {+ throw new Error('expected a live speech stream')+ }++ return session+}++describe('voice-playback teardown (#91991)', () => {+ beforeEach(() => {+ vi.useFakeTimers()+ FakeSocket.instances = []+ FakeAudioContext.instances = []+ FakeAudio.instances = []+ gateway.setNextUrl('ws://gateway.test/api/ws')+ vi.stubGlobal('WebSocket', FakeSocket)+ vi.stubGlobal('AudioContext', FakeAudioContext)+ vi.stubGlobal('Audio', FakeAudio)+ vi.stubGlobal('hermesDesktop', { getConnection: vi.fn(async () => ({ profile: 'test' })) })+ $voicePlayback.set({ audioElement: null, messageId: null, sequence: 0, source: null, status: 'idle' })+ })++ afterEach(() => {+ vi.unstubAllGlobals()+ vi.useRealTimers()+ })++ it('abandons a stream start that crosses a stop instead of resuming playback', async () => {+ // Stream discovery (gateway connection) is in flight when the user barges.+ const pending = startSpeechStream({ source: 'voice-conversation' })++ stopVoicePlayback()++ const session = await pending++ expect(session).toBeNull()+ expect(FakeSocket.instances).toHaveLength(0)+ expect($voicePlayback.get().status).toBe('idle')+ })++ it('does not let a settled session reset the playback state of a newer turn', async () => {+ const first = await liveSession()+ const second = await liveSession()++ // Starting the second session settles the first (its socket closes), but+ // the settled session's completion callback must not stomp the state the+ // newer session just wrote.+ expect(FakeSocket.instances[0]?.close).toHaveBeenCalled()+ expect($voicePlayback.get().status).toBe('preparing')++ stopVoicePlayback()+ })++ it('a barge-in closes the socket and audio context of a live streaming session and settles it', async () => {+ const session = await liveSession()+ const ws = FakeSocket.instances[0]++ ws.serverOpen()+ ws.serverFrame({ type: 'start', sample_rate: 24_000 })+ ws.serverPcm(64)++ expect($voicePlayback.get().status).toBe('speaking')++ stopVoicePlayback()++ expect(ws.close).toHaveBeenCalled()+ expect(FakeAudioContext.instances[0]?.closed).toBe(true)+ expect($voicePlayback.get().status).toBe('idle')+ await expect(session.done).resolves.toBe('done')++ // A settled session can no longer feed text — nothing reaches the socket.+ const sentBefore = ws.sent.length++ session.append('stale text')+ session.finish()++ expect(ws.sent.length).toBe(sentBefore)+ })++ it('a barge-in during the drain window settles the session and the late drain is a no-op', async () => {+ const session = await liveSession()+ const ws = FakeSocket.instances[0]++ ws.serverOpen()+ ws.serverFrame({ type: 'start', sample_rate: 24_000 })+ ws.serverPcm(64)+ ws.serverFrame({ type: 'end' }) // drains, then settles — timer pending++ stopVoicePlayback()++ await expect(session.done).resolves.toBe('done')+ expect(ws.close).toHaveBeenCalled()++ // The pending drain timer fires later — it must not double-settle or+ // disturb anything that started since.+ await vi.runOnlyPendingTimersAsync()++ expect($voicePlayback.get().status).toBe('idle')+ })++ it('a barge-in cuts a data-url playback and clears the audio element', async () => {+ gateway.setNextUrl(null) // no streaming endpoint → data-url fallback++ const playback = playSpeechText('hello world', { messageId: 'm1', source: 'read-aloud' })++ await vi.advanceTimersByTimeAsync(0)++ expect($voicePlayback.get().status).toBe('speaking')+ expect(FakeAudio.instances).toHaveLength(1)++ stopVoicePlayback()++ expect(FakeAudio.instances[0]?.pause).toHaveBeenCalled()+ expect(FakeAudio.instances[0]?.src).toBe('')+ expect(FakeAudio.instances[0]?.load).toHaveBeenCalled()+ expect($voicePlayback.get().status).toBe('idle')+ await expect(playback).resolves.toBe(false)+ })++ it('one stop drains BOTH of two overlapping streaming sessions (#91991)', async () => {+ // The reported overlap: session A is live with an open AudioContext;+ // session B starts (its stopVoicePlayback drains A under the Set+ // registry — the old single currentStop slot left A's socket open and+ // its done unsettled). A final explicit stop must still tear down+ // everything that is live.+ const first = await liveSession()++ const firstWs = FakeSocket.instances[0]++ firstWs.serverOpen()+ firstWs.serverFrame({ type: 'start', sample_rate: 24_000 })+ firstWs.serverPcm(64)+ first.append('turn one')++ const second = await liveSession()++ const secondWs = FakeSocket.instances[1]++ secondWs.serverOpen()+ secondWs.serverFrame({ type: 'start', sample_rate: 24_000 })+ secondWs.serverPcm(64)+ second.append('turn two')++ // Starting B drained A: both its socket and its AudioContext are gone.+ await expect(first.done).resolves.toBe('done')+ expect(firstWs.close).toHaveBeenCalled()+ expect(FakeAudioContext.instances[0]?.closed).toBe(true)++ stopVoicePlayback()++ await expect(second.done).resolves.toBe('done')+ expect(secondWs.close).toHaveBeenCalled()+ expect(FakeAudioContext.instances[1]?.closed).toBe(true)+ expect($voicePlayback.get().status).toBe('idle')+ })+})diff --git a/apps/desktop/src/lib/voice-playback.ts b/apps/desktop/src/lib/voice-playback.tsindex a31f9154da2..94a59f15990 100644--- a/apps/desktop/src/lib/voice-playback.ts+++ b/apps/desktop/src/lib/voice-playback.ts@@ -20,7 +20,13 @@ import { cutSentences, sanitizeTextForSpeech } from './speech-text'const PLAYBACK_STALL_MS = 15_000let currentAudio: HTMLAudioElement | null = null-let currentStop: (() => void) | null = null+// Every live playback registers its barge-in stop here: streaming sessions+// (kill the WebSocket + AudioContext) and data-URL audio elements (cut+// playback). A single slot cannot represent two overlapping playbacks — the+// overwritten session keeps its socket open, keeps scheduling buffers, and+// its audio resurfaces on a later turn (#91991). Draining the whole set keeps+// stopVoicePlayback() deterministic no matter how playbacks overlap.+const liveStops = new Set<() => void>()let sequence = 0let claimTurnKey: string | null = nulllet inFlight: { done: Promise<boolean>; turnKey: string } | null = null@@ -86,8 +92,14 @@ export interface VoicePlaybackOptions extends OwnerScope {export function stopVoicePlayback() {inFlight = nullsequence += 1- currentStop?.()- currentStop = null++ // Drain EVERY live playback, not just the newest. Each stop is idempotent+ // and unregisters itself; the snapshot keeps the iteration safe.+ for (const stop of [...liveStops]) {+ stop()+ }++ liveStops.clear()if (currentAudio) {currentAudio.pause()@@ -223,6 +235,12 @@ function openClientDirectSpeechSession(tts: DirectTtsConfig, options: VoicePlayblet settle: (value: 'done' | 'fallback') => void = () => undefined+ // stopVoicePlayback() → immediate barge-in: settle this session so its+ // queued audio never resumes. Registered until settle, so a barge always+ // reaches this session even while another playback is live alongside it+ // (#91991).+ const stop = () => settle(started ? 'done' : 'fallback')+const done = new Promise<'done' | 'fallback'>(resolve => {settle = value => {if (settled) {@@ -230,7 +248,7 @@ function openClientDirectSpeechSession(tts: DirectTtsConfig, options: VoicePlayb}settled = true- currentStop = null+ liveStops.delete(stop)if (playing) {playing.pause()@@ -242,7 +260,7 @@ function openClientDirectSpeechSession(tts: DirectTtsConfig, options: VoicePlayb}})- currentStop = () => settle(started ? 'done' : 'fallback')+ liveStops.add(stop)const pump = async () => {if (synthesizing || settled) {@@ -376,6 +394,12 @@ function openSpeechStream(wsUrl: string, options: VoicePlaybackOptions): SpeechSlet settle: (value: 'done' | 'fallback') => void = () => undefined+ // stopVoicePlayback() → immediate barge-in: kill the socket (the server+ // aborts synthesis on disconnect) and the audio context (cuts sound now).+ // Registered until settle, so a barge always reaches this session even+ // while another playback is live alongside it (#91991).+ const stop = () => settle('done')+const done = new Promise<'done' | 'fallback'>(resolve => {settle = value => {if (settled) {@@ -383,7 +407,7 @@ function openSpeechStream(wsUrl: string, options: VoicePlaybackOptions): SpeechS}settled = true- currentStop = null+ liveStops.delete(stop)try {ws.close()@@ -407,9 +431,7 @@ function openSpeechStream(wsUrl: string, options: VoicePlaybackOptions): SpeechS}}- // stopVoicePlayback() → immediate barge-in: kill the socket (the server- // aborts synthesis on disconnect) and the audio context (cuts sound now).- currentStop = () => settle('done')+ liveStops.add(stop)const finishWhenDrained = () => {const remainingMs = context ? Math.max(0, nextStartAt - context.currentTime) * 1_000 : 0@@ -537,16 +559,27 @@ function openSpeechStream(wsUrl: string, options: VoicePlaybackOptions): SpeechS* `playSpeechText`).*/export async function startSpeechStream(options: VoicePlaybackOptions): Promise<null | SpeechStreamSession> {+ const startSequence = sequence+const direct = await directTtsConfig(options).catch(() => null)if (direct) {+ // A stop (barge-in, Stop button, another playback) landed while the+ // direct TTS config was resolving — do not resurrect stopped playback.+ if (sequence !== startSequence) {+ return null+ }+stopVoicePlayback()setVoicePlaybackState(currentState('preparing', options))const session = openClientDirectSpeechSession(direct, options)+ const sessionSequence = sequencevoid session.done.then(outcome => {- if (outcome === 'done') {+ // A settled session must not reset the state of a NEWER playback —+ // only write idle when no other playback started since this one.+ if (outcome === 'done' && sessionSequence === sequence) {setVoicePlaybackState(currentState('idle'))}})@@ -560,13 +593,24 @@ export async function startSpeechStream(options: VoicePlaybackOptions): Promise<return null}+ // A stop (barge-in, Stop button, another playback) landed while the+ // stream URL was resolving — do not resurrect playback that was already+ // stopped. This is the reported overlap (#91991): barge + next-turn open+ // crossing the async discovery gap previously orphaned the older session.+ if (sequence !== startSequence) {+ return null+ }+stopVoicePlayback()setVoicePlaybackState(currentState('preparing', options))const session = openSpeechStream(wsUrl, options)+ const sessionSequence = sequencevoid session.done.then(outcome => {- if (outcome === 'done') {+ // A settled session must not reset the state of a NEWER playback — only+ // write idle when no other playback started since this one was created.+ if (outcome === 'done' && sessionSequence === sequence) {setVoicePlaybackState(currentState('idle'))}})@@ -601,6 +645,13 @@ async function playSpeechDataUrl(await new Promise<void>((resolve, reject) => {let stall: number | null = null+ // Registered until cleanup so stopVoicePlayback() reaches this element+ // even while a streaming session is live alongside it (#91991).+ const stop = () => {+ cleanup()+ resolve()+ }+const cleanup = () => {if (stall !== null) {window.clearTimeout(stall)@@ -610,7 +661,7 @@ async function playSpeechDataUrl(audio.removeEventListener('ended', onEnded)audio.removeEventListener('error', onError)audio.removeEventListener('timeupdate', armStall)- currentStop = null+ liveStops.delete(stop)}const armStall = () => {@@ -634,10 +685,7 @@ async function playSpeechDataUrl(reject(new Error('Playback failed'))}- currentStop = () => {- cleanup()- resolve()- }+ liveStops.add(stop)audio.addEventListener('ended', onEnded, { once: true })audio.addEventListener('error', onError, { once: true })@@ -765,7 +813,6 @@ async function startSpeechText(text: string, options: VoicePlaybackOptions): Proreturn played} catch (error) {if (isCurrent()) {- currentStop = nullcurrentAudio = nullsetVoicePlaybackState(currentState('idle'))}