Skip to content

The agent that grows with you

fix(desktop): stop the sentence-fallback poll on unmount and session drop (#79314)

kshitijk4poord5785bb525e4
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-voice-conversation-rearm.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-voice-conversation-rearm.test.tsx
index f4b55824c70..049495d58f0 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-voice-conversation-rearm.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-voice-conversation-rearm.test.tsx
@@ -165,6 +165,7 @@ function renderRearmConversation(responseId: string, responseText: string) {
function renderIncrementalFallbackConversation() {
let response: null | { id: string; pending: boolean; text: string } = null
+ const pendingResponse = vi.fn(() => response)
const hook = renderHook(
({ enabled }) =>
@@ -176,12 +177,13 @@ function renderIncrementalFallbackConversation() {
response = { id: 'reply-edge', pending: true, text: 'The first sentence is ready. ' }
},
onTranscribeAudio: async () => 'Hello',
- pendingResponse: () => response
+ pendingResponse
}),
{ initialProps: { enabled: false } }
)
return {
+ pendingResponse,
finishResponse() {
response = {
id: 'reply-edge',
@@ -333,6 +335,23 @@ describe('useVoiceConversation playback rearm', () => {
await waitFor(() => expect(hook.result.current.status).toBe('listening'))
})
+ it('stops polling the pending fallback reply once the hook unmounts', async () => {
+ mocks.useFallbackSpeech()
+ mocks.deferFallbackPlayback()
+ const { hook, pendingResponse } = renderIncrementalFallbackConversation()
+
+ await beginReply(hook)
+ await waitFor(() => expect(mocks.playSpeechText).toHaveBeenCalledTimes(1))
+
+ hook.unmount()
+ const pollsAtUnmount = pendingResponse.mock.calls.length
+ await act(async () => {
+ await new Promise(resolve => setTimeout(resolve, 400))
+ })
+
+ expect(pendingResponse.mock.calls.length).toBe(pollsAtUnmount)
+ })
+
it('does not play the next fallback sentence or re-arm after Stop', async () => {
mocks.useFallbackSpeech()
mocks.deferFallbackPlayback()
@@ -354,6 +373,7 @@ describe('useVoiceConversation playback rearm', () => {
it('speaks a sealed interim bubble while a tool is still running', async () => {
mocks.useFallbackSpeech()
+
let releaseSubmit: () => void = () => {}
let response: null | { id: string; pending: boolean; text: string } = null
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-voice-conversation.ts b/apps/desktop/src/app/chat/composer/hooks/use-voice-conversation.ts
index 1b25e29e941..4169b3a7a11 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-voice-conversation.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-voice-conversation.ts
@@ -128,6 +128,14 @@ export function useVoiceConversation({
statusRef.current = status
}, [status])
+ // The sentence-by-sentence fallback polls the pending reply on a timer; it
+ // must not outlive the session or the hook (a stray tick after unmount hits
+ // a torn-down window).
+ const cancelFallbackPollRef = useRef<(() => void) | null>(null)
+
+
+ useEffect(() => () => cancelFallbackPollRef.current?.(), [])
+
const clearTurnTimeout = () => {
if (turnTimeoutRef.current) {
window.clearTimeout(turnTimeoutRef.current)
@@ -136,6 +144,7 @@ export function useVoiceConversation({
}
const dropSpeechSession = () => {
+ cancelFallbackPollRef.current?.()
stopBargeMonitorRef.current?.()
stopBargeMonitorRef.current = null
bargeCapturePendingRef.current = false
@@ -462,17 +471,27 @@ export function useVoiceConversation({
let pollTimer: number | null = null
let ownedSequence = $voicePlayback.get().sequence
- const finishFallback = (barged: boolean, stopped = false) => {
- if (settled) {
- return
- }
-
+ const cancelPoll = () => {
settled = true
if (pollTimer !== null) {
window.clearTimeout(pollTimer)
+ pollTimer = null
}
+ if (cancelFallbackPollRef.current === cancelPoll) {
+ cancelFallbackPollRef.current = null
+ }
+ }
+
+ cancelFallbackPollRef.current = cancelPoll
+
+ const finishFallback = (barged: boolean, stopped = false) => {
+ if (settled) {
+ return
+ }
+
+ cancelPoll()
awaitingSpokenResponseRef.current = false
settleAfterSpeech(barged, stopped)
}
@@ -509,6 +528,7 @@ export function useVoiceConversation({
source: 'voice-conversation',
syncOnly: true
})
+
ownedSequence = $voicePlayback.get().sequence
speechStartSequenceRef.current = ownedSequence
let playbackFailed = false