Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix(voice): clear the finalize deadline once the last text is in

Samrat Talukdar30491a5a909c
diff --git a/apps/app/src/app/lib/voice-core/session.ts b/apps/app/src/app/lib/voice-core/session.ts
index 0b11366b..70c8a013 100644
--- a/apps/app/src/app/lib/voice-core/session.ts
+++ b/apps/app/src/app/lib/voice-core/session.ts
@@ -218,10 +218,14 @@ export class VoiceSession {
const last = this.segmenter.flush();
if (last) this.enqueue(last);
const deadline = this.options.finalizeTimeoutMs ?? 45_000;
+ // The deadline's timer is cancelled as soon as the race is decided, so a
+ // finished recording leaves nothing pending.
+ const deadlineDone = new AbortController();
const timedOut = await Promise.race([
this.whenIdle().then(() => false),
- wait(deadline, this.abort.signal).then(() => true),
+ wait(deadline, AbortSignal.any([this.abort.signal, deadlineDone.signal])).then(() => !deadlineDone.signal.aborted),
]);
+ deadlineDone.abort();
if (this.phase === "cancelled") return this.result ?? { text: "", words: 0, segments: 0, error: null };
if (timedOut && this.active + this.queue.length > 0) {
this.abort.abort();
diff --git a/apps/app/tests/voice-core.test.ts b/apps/app/tests/voice-core.test.ts
index eef7db2c..3e76b691 100644
--- a/apps/app/tests/voice-core.test.ts
+++ b/apps/app/tests/voice-core.test.ts
@@ -345,6 +345,37 @@ describe("voice session", () => {
expect(session.snapshot().pending).toBe(0);
});
+ test("a finished recording leaves no timer pending (the finalize deadline is cleared)", async () => {
+ const realSetTimeout = globalThis.setTimeout;
+ const realClearTimeout = globalThis.clearTimeout;
+ const pending = new Map<unknown, number>();
+ globalThis.setTimeout = ((handler: () => void, ms?: number, ...args: unknown[]) => {
+ const id = realSetTimeout(() => {
+ pending.delete(id);
+ handler();
+ }, ms, ...args);
+ pending.set(id, ms ?? 0);
+ return id;
+ }) as typeof setTimeout;
+ globalThis.clearTimeout = ((id: Parameters<typeof clearTimeout>[0]) => {
+ pending.delete(id);
+ realClearTimeout(id);
+ }) as typeof clearTimeout;
+ try {
+ const transcriber = fakeTranscriber(async (request) => {
+ await sleep(20);
+ return `part ${request.segmentIndex}`;
+ });
+ const { result } = await dictate(THREE_PHRASES, transcriber, { finalizeTimeoutMs: 45_000 });
+ expect(result.text).toBe("part 0 part 1 part 2");
+ await sleep(10);
+ expect([...pending.values()].filter((ms) => ms >= 1_000)).toEqual([]);
+ } finally {
+ globalThis.setTimeout = realSetTimeout;
+ globalThis.clearTimeout = realClearTimeout;
+ }
+ });
+
test("the breaker opens after three failures in ten seconds", async () => {
let now = 1_000;
const breaker = new CircuitBreaker(3, 10_000, 30_000, () => now);