Run Whisper in a worker thread; shorten polling

The speech model now lives in a worker thread (whisperWorker.mjs), so
loading and transcribing no longer block the bridge's HTTP server or its
Hermes polling. Measured longest event-loop pause during transcription:
387 ms before, 3 ms after. A dead worker is replaced on the next request;
closing the bridge terminates it.

Polling: bridge to Hermes every 150 ms (was 500). The app polls the bridge
every 200 ms for the first 8 s of a turn, 400 ms until 30 s, then 1 s
(was a fixed 700 ms).

Tests: 131 unit/integration, 7 real-speech, 9 real-Hermes, 6 simulator,
all passing. Effect on a phone or a droplet is not measured.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-18 18:39:28 -08:00
co-authored by Claude Fable 5.2
parent 6d74f9e121
commit 0c9dcd864f
14 changed files with 212 additions and 31 deletions
+37
View File
@@ -90,6 +90,43 @@ describe('built-in Whisper speech-to-text', () => {
expect(done.error?.code).toBe('stt_empty');
});
it('two recordings submitted together are both transcribed, one after the other', async () => {
const other = await h.pair('second-device');
const otherConversation = await h.conversation(other.token);
const [a, b] = await Promise.all([
h.audioTurn(token, conversation, Buffer.from(speech)),
h.audioTurn(other.token, otherConversation, Buffer.from(speech)),
]);
const [doneA, doneB] = await Promise.all([
h.waitTerminal(token, a.body.request_id, 120_000),
h.waitTerminal(other.token, b.body.request_id, 120_000),
]);
expect(doneA.input_text).toContain('fellow Americans');
expect(doneB.input_text).toContain('fellow Americans');
});
it('a model that cannot be loaded makes speech "unavailable" without taking the bridge down', async () => {
const broken = new BuiltinWhisperStt({ model: 'hermeven-test/does-not-exist', dtype: 'q8', cacheDir: resolve(here, '.cache') });
try {
const err = await broken
.transcribe({ pcm: Buffer.from(speech.subarray(0, 32000)), locale: 'en-US', signal: new AbortController().signal })
.catch((e) => e);
expect(err).toMatchObject({ name: 'SttError', kind: 'unavailable' });
} finally {
await broken.close();
}
expect((await h.api('GET', '/healthz')).status).toBe(200);
});
it('closing the adapter stops its worker thread, and a closed adapter refuses further work', async () => {
const stt = new BuiltinWhisperStt({ model: h.config.stt.model, dtype: h.config.stt.dtype, cacheDir: resolve(here, '.cache') });
const signal = new AbortController().signal;
expect(await stt.transcribe({ pcm: Buffer.from(speech), locale: 'en-US', signal })).toContain('fellow Americans');
await stt.close();
await stt.close();
await expect(stt.transcribe({ pcm: Buffer.from(speech), locale: 'en-US', signal })).rejects.toMatchObject({ kind: 'unavailable' });
});
it('a second recording is transcribed with the model already warm', async () => {
const res = await h.audioTurn(token, conversation, Buffer.from(speech.subarray(0, 6 * 32000)));
const done = await h.waitTerminal(token, res.body.request_id, 120_000);