How I built a voice stream that stops instead of repeating itself: 2,000-char chunks, 1 read-ahead sentence, 0 replays

Ankita AI is a desktop AI assistant by Krish Verma, and it speaks its progress reports out loud. That sounds simple until the network drops mid-sentence. The obvious fix — just play the sentence again — is worse than the failure: the listener hears the same sentence twice and has no way to tell which copy is real. So the stream is built to die loudly instead of repeating itself. This is how that boundary works, and the one place it is still silent.

The rule: only a capability failure may fall back

The streaming path starts in prepareVoiceStreamClip (src/channels/voice-stream-clip.mjs). It asks the provider for exactly one chunk first, then pipes the iterator straight into the player's stdin. The fallback to a buffered clip is deliberately narrow: it fires only when the error carries the code VOICE_STREAM_UNSUPPORTED, which the Sarvam path sets for HTTP 404, 405, 415, 422 and 501 — capability or model failures that happen before any audio exists. Auth and rate-limit failures are not in that set, and neither is a stream that dies after audio started.

A mid-stream death throws. The iterator is closed, the controller aborts, and the sentence is never re-spoken. Only a clip that finishes cleanly gets cached (cached = Buffer.concat(chunks) runs after the loop completes), so a half-played sentence can never come back as a replay later. The delivery object the player reports — {mode, fallback, firstChunkMs} — carries the mode so the panel can show whether what you heard was streamed, buffered, or cached, instead of guessing.

The queue: one sentence ahead, no second chances

Playback is owned by VoiceSpeechQueue (src/automation/voice-speech-queue.mjs). Sentences are cut with chunkSentences — 190 characters for the Groq path, up to 2,000 characters per Sarvam chunk — and the queue pre-fetches at most one following sentence while the current one plays, hiding its TTS latency under live audio. The pending backlog is capped at 24 clips.

When a part fails, the queue's pump() catches it and calls onError — but only for non-abort errors. The entry is marked unavailable, resolves as not played, and the queue moves on. There is no retry for a failed part: the retry flag exists for reports interrupted by the user, not for synthesis that died. That is the whole point. A retry would need to know how much of the sentence already played, and the queue does not track that — so it refuses to guess.

The measured cost of the design is small: first audio arrived in 491 ms on the 10 November re-verification, with the first chunk consumed 32 ms later. The numbers were single samples, not a benchmark, but they are the reason streaming was worth building at all.

What the listener actually gets today

Here is the honest limit, and it is the reason this article exists. When a stream dies mid-sentence, the error travels up through onError into fail(session, error) in desktop/electron/voice.mjs — the failure is visible in the panel, but nothing is spoken. The listener hears half a sentence, then silence. Silence after half a sentence is easy to mistake for a pause, so they keep waiting for the rest.

That gap came out of a thread on Ahmad Dogar's TTS-voices article, where he put it plainly: he'd rather the player say something short when the stream dies. He is right, and I am going with the spoken note. There is already a precedent in the same code — a stop word gets a spoken Stopped. (STOPPED_LINE in desktop/electron/voice.mjs) — so a cut-off note would be a short fixed phrase through the same path, not a re-speak of the sentence. Re-speaking would reopen the double-play the whole design exists to prevent.

The open question

The part still undecided is what the note should say. A fixed phrase — "That update got cut off" — is honest about what happened and costs nothing to generate. Naming the sentence that died would be more useful, but it means speaking text the listener already half-heard, through the same queue that just failed. Tell me I'm wrong: is a fixed short phrase enough when the listener already heard the first half, or should the note name what was lost?

Ankita is open source at https://github.com/akyourowngames/A.N.K.I.T.A, and the site is https://ankita-sable.vercel.app. If you run spoken output anywhere — a voice assistant, a screen reader, a notification reader — how do you signal a cut-off stream without replaying it?

— Krish Verma, creator of Ankita AI