Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: cancel browser voice audio on LLM timeout (#5497)

[._.]/ Adam Eivyd4d76b9d0399
diff --git a/client/src/services/voiceClient.js b/client/src/services/voiceClient.js
index b92ecdf5fb..b53ea40c8f 100644
--- a/client/src/services/voiceClient.js
+++ b/client/src/services/voiceClient.js
@@ -28,6 +28,7 @@ let audioCtx = null;
let playQueue = Promise.resolve();
let currentSource = null;
let ttsQueueDepth = 0;
+let playbackGeneration = 0;
// Timestamp after which the post-TTS echo-tail window ends. See VAD.ttsTailMs.
let ttsCooldownUntil = 0;
// Raised when a turn is cancelled (barge-in, explicit interrupt, reset, new
@@ -61,6 +62,7 @@ const ensureCtx = () => {
};
const stopPlayback = () => {
+ playbackGeneration += 1;
if (currentSource) {
try { currentSource.stop(); } catch { /* already stopped */ }
currentSource = null;
@@ -141,12 +143,19 @@ const blobToWav16k = async (blob) => {
};
const enqueuePlay = async (bytes) => {
+ const generation = playbackGeneration;
const ctx = ensureCtx();
// decodeAudioData consumes its buffer — clone so we don't mutate the socket frame
const copy = bytes.slice(0);
const buffer = await ctx.decodeAudioData(copy);
+ if (generation !== playbackGeneration) return;
ttsQueueDepth += 1;
playQueue = playQueue.then(() => new Promise((resolve) => {
+ if (generation !== playbackGeneration) {
+ ttsQueueDepth = Math.max(0, ttsQueueDepth - 1);
+ resolve();
+ return;
+ }
const src = ctx.createBufferSource();
src.buffer = buffer;
src.connect(ctx.destination);
@@ -348,6 +357,11 @@ socket.on('voice:tts:audio', ({ sentence, wav }) => {
enqueuePlay(ab).catch((err) => console.warn('[voice] playback failed:', err));
});
+// A provider timeout can happen after earlier sentences were already emitted.
+// Clear those browser-side frames too, so recovery does not leave the stale
+// reply speaking over the next turn.
+socket.on('voice:tts:cancel', () => stopPlayback());
+
// Proactive CoS speech. Server-pushed lines (alerts/briefings/reminders) come
// in on a separate channel so the client can render a distinct visual cue;
// VoiceWidget decides whether/how to display them (toast + history appending).
diff --git a/server/services/voice/pipeline.js b/server/services/voice/pipeline.js
index 6025189b2b..ce68e6ea3b 100644
--- a/server/services/voice/pipeline.js
+++ b/server/services/voice/pipeline.js
@@ -669,7 +669,10 @@ export const runTurn = async ({ audio, text, mimeType, source, history = [], emi
provider: cfg.llm.provider,
model: cfg.llm.model,
signal,
- onTimeout: () => ttsController.abort(),
+ onTimeout: () => {
+ ttsController.abort();
+ emit('voice:tts:cancel', { reason: 'timeout' });
+ },
tools: toolSpecs,
tag: turnId,
onDelta: (delta) => {
diff --git a/server/services/voice/pipelineTimeout.test.js b/server/services/voice/pipelineTimeout.test.js
index a78e7357a3..9ea307d5d0 100644
--- a/server/services/voice/pipelineTimeout.test.js
+++ b/server/services/voice/pipelineTimeout.test.js
@@ -57,6 +57,10 @@ describe('runTurn LLM timeout recovery', () => {
await expect(turn).rejects.toThrow('Voice LLM request timed out');
await vi.waitFor(() => expect(mocks.synthesize).not.toHaveBeenCalled());
expect(signal.aborted).toBe(false);
+ expect(events).toContainEqual({
+ event: 'voice:tts:cancel',
+ payload: { reason: 'timeout' },
+ });
expect(events.some(({ event }) => event === 'voice:tts:audio')).toBe(false);
});
});
diff --git a/server/sockets/voice.js b/server/sockets/voice.js
index c07404aaff..c90289168e 100644
--- a/server/sockets/voice.js
+++ b/server/sockets/voice.js
@@ -3,7 +3,7 @@
// | voice:dictation:set | voice:ui:index | voice:screenshot:result
// | voice:ui:read-response | voice:output:available | voice:output:claim
// Outbound: voice:transcript | voice:llm:delta | voice:llm:done | voice:tts:audio
-// | voice:tool | voice:dictation | voice:navigate
+// | voice:tts:cancel | voice:tool | voice:dictation | voice:navigate
// | voice:ui:click | voice:ui:fill | voice:ui:select | voice:ui:check
// | voice:ui:read-request
// | voice:dailyLog:appended | voice:error | voice:idle