From b55955541e4e77d5881ec3de5141d4f1571c0c73 Mon Sep 17 00:00:00 2001 From: tieo <65707274+tieo@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:05:47 +0100 Subject: [PATCH] fix(dictation): surface the daemon's stream error instead of a delivery timeout (#9784) The daemon drops a dictation stream when its STT session errors and answers every later chunk with dictation_stream_error. The app only listened for acks, so finishing waited 30s and reported a generic timeout. The sender now fails finish with the daemon's message, and the daemon logs the failure that dropped the stream once instead of nothing. Co-authored-by: Claude Opus 5.5 --- .../dictation/dictation-stream-sender.test.ts | 33 +++++++++++++++++ .../src/dictation/dictation-stream-sender.ts | 35 +++++++++++++++++-- .../dictation/dictation-stream-manager.ts | 16 ++++++++- 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/packages/app/src/dictation/dictation-stream-sender.test.ts b/packages/app/src/dictation/dictation-stream-sender.test.ts index 1af06997a..ea56e2db5 100644 --- a/packages/app/src/dictation/dictation-stream-sender.test.ts +++ b/packages/app/src/dictation/dictation-stream-sender.test.ts @@ -90,6 +90,16 @@ class FakeDaemonClient { cancelDictationStream(dictationId: string): void { this.cancels.push(dictationId); } + + emitStreamError(dictationId: string, error: string): void { + const message = { + type: "dictation_stream_error", + payload: { dictationId, error, retryable: true }, + } as SessionOutboundMessage; + for (const listener of this.rawMessageListeners) { + listener(message); + } + } } const tick = async () => { @@ -314,6 +324,29 @@ describe("DictationStreamSender", () => { await expect(finish).resolves.toEqual({ dictationId: "d1", text: "ok" }); }); + it("fails finish with the daemon's stream error instead of waiting for acks", async () => { + const client = new FakeDaemonClient(); + client.autoAck = false; + let nextId = 0; + const sender = new DictationStreamSender({ + client, + format: "audio/pcm;rate=16000;bits=16", + createDictationId: () => `d${++nextId}`, + }); + + sender.enqueueSegment("seg0"); + await tick(); + client.emitStreamError("d1", "Local speech worker request timed out: commit"); + sender.enqueueSegment("seg1"); + await tick(); + + const finish = sender.finish(1); + await expect(promiseOutcome(finish, 1000)).resolves.toBe("rejected"); + await expect(finish).rejects.toThrow("Local speech worker request timed out"); + expect(client.finishes).toEqual([]); + expect(sender.getSegmentCount()).toBe(2); + }); + it("does not replay long buffered native dictation in one synchronous burst", async () => { const client = new FakeDaemonClient(); client.isConnected = false; diff --git a/packages/app/src/dictation/dictation-stream-sender.ts b/packages/app/src/dictation/dictation-stream-sender.ts index 238845884..387155bc3 100644 --- a/packages/app/src/dictation/dictation-stream-sender.ts +++ b/packages/app/src/dictation/dictation-stream-sender.ts @@ -8,6 +8,10 @@ const DICTATION_DRAIN_IDLE_TIMEOUT_MS = 30_000; const waitForNextFlushTurn = (): Promise => new Promise((resolve) => setTimeout(resolve, 0)); type DictationStreamAckMessage = Extract; +type DictationStreamErrorMessage = Extract< + SessionOutboundMessage, + { type: "dictation_stream_error" } +>; interface DrainWaitResult { kind: "signaled" | "timed-out"; @@ -64,6 +68,7 @@ export class DictationStreamSender { private startGeneration = 0; private startPromise: Promise | null = null; + private streamError: Error | null = null; constructor(params: DictationStreamSenderParams) { this.format = params.format; @@ -88,10 +93,11 @@ export class DictationStreamSender { } this.clientCleanup = client.subscribeRawMessages((message) => { - if (message.type !== "dictation_stream_ack") { - return; + if (message.type === "dictation_stream_ack") { + this.handleAck(message); + } else if (message.type === "dictation_stream_error") { + this.handleStreamError(message); } - this.handleAck(message); }); } @@ -127,6 +133,7 @@ export class DictationStreamSender { this.acknowledgedSeq = -1; this.streamReady = false; this.startPromise = null; + this.streamError = null; this.startGeneration += 1; this.resolveDrainWaiters(); } @@ -197,6 +204,7 @@ export class DictationStreamSender { this.sendSeq = 0; this.acknowledgedSeq = -1; this.streamReady = false; + this.streamError = null; const start = (async () => { await client.startDictationStream(dictationId, this.format); @@ -285,6 +293,25 @@ export class DictationStreamSender { this.resolveDrainWaiters(); } + // The daemon drops a stream after an error and answers later chunks with + // errors instead of acks, so waiting for delivery can only time out. + private handleStreamError(message: DictationStreamErrorMessage): void { + if (message.payload.dictationId !== this.dictationId || this.streamError) { + return; + } + this.streamError = new Error(message.payload.error); + this.resolveDrainWaiters(); + } + + private throwStreamError(): void { + const error = this.streamError; + if (!error) { + return; + } + this.invalidateStream(); + throw error; + } + private invalidateStream(): void { this.resetStreamForReplay(); } @@ -309,11 +336,13 @@ export class DictationStreamSender { private async waitForFlushDrain(finalSeq: number): Promise { while (this.hasPendingDelivery(finalSeq)) { + this.throwStreamError(); const client = this.client; if (!client?.isConnected || !this.dictationId || !this.streamReady) { throw new Error("Failed to flush dictation stream"); } const result = await this.waitForDrainSignal(); + this.throwStreamError(); if (result.kind === "timed-out") { this.invalidateStream(); throw new Error("Timed out waiting for dictation stream delivery"); diff --git a/packages/server/src/server/dictation/dictation-stream-manager.ts b/packages/server/src/server/dictation/dictation-stream-manager.ts index b1b061501..ac91fba00 100644 --- a/packages/server/src/server/dictation/dictation-stream-manager.ts +++ b/packages/server/src/server/dictation/dictation-stream-manager.ts @@ -345,7 +345,7 @@ export class DictationStreamManager { }): Promise { const state = this.streams.get(params.dictationId); if (!state) { - this.failDictationStream(params.dictationId, "Dictation stream not started", true); + this.emitUnknownStreamError(params.dictationId); return; } @@ -528,7 +528,17 @@ export class DictationStreamManager { return path; } + // Chunks keep arriving for a stream the daemon already dropped, one error + // per chunk; the failure that dropped it is logged where it happened. + private emitUnknownStreamError(dictationId: string): void { + this.emit({ + type: "dictation_stream_error", + payload: { dictationId, error: "Dictation stream not started", retryable: true }, + }); + } + private failDictationStream(dictationId: string, error: string, retryable: boolean): void { + this.logger.warn({ dictationId, error, retryable }, "Dictation stream failed"); this.emit({ type: "dictation_stream_error", payload: { dictationId, error, retryable }, @@ -543,6 +553,10 @@ export class DictationStreamManager { const state = this.streams.get(dictationId); const debugRecordingPath = await this.maybePersistDictationStreamAudio(dictationId); if (!state || this.streams.get(dictationId) !== state) return; + this.logger.warn( + { dictationId, error, retryable, ackSeq: state.ackSeq }, + "Dictation stream failed", + ); this.emit({ type: "dictation_stream_error", payload: {