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
+1 -1
View File
@@ -43,7 +43,7 @@ calls no tools. They need a run against a real model with a tool that requires a
## Consequences
- Polling adds up to one poll interval (500 ms by default) of latency to every answer.
- Polling adds up to one poll interval (150 ms by default) of latency to every answer.
- No token streaming to the HUD. Acceptable: the HUD shows whole pages, and streaming through the
Even WebView is undocumented.
- If the target Hermes runs without the durable run ledger, the idempotency guarantee weakens.
+1 -1
View File
@@ -30,7 +30,7 @@ weights:
| 11 s of speech, process pinned to one core | about 1.7 s |
| Same, `whisper-tiny` | about 1.2 s, about 0.6 GB |
| Model download on first start | about 80 MB (`base`), cached afterwards |
| Longest pause of the bridge's event loop while transcribing | about 0.4 s |
| Longest pause of the bridge's event loop while transcribing | 3 ms (the model runs in a worker thread) |
Pinning to one core made no difference to speed, so extra cores will not help; a single droplet
vCPU is the relevant unit. **A shared droplet vCPU is slower than that desktop core by an amount
+6 -4
View File
@@ -31,6 +31,7 @@ DigitalOcean droplet.
| Tests | `services/bridge/test/builtinWhisper.test.ts`, `tests/real-stt/*` (with a public-domain speech fixture) |
| Deployment examples, **not run on a droplet** | `deploy/*`, `docs/deploy-droplet.md` |
| New dependency | `@huggingface/transformers` 4.3.2 (brings ONNX Runtime and `sharp`) |
| Whisper moved to a worker thread; polling shortened (bridge 150 ms; app 200/400/1000 ms by elapsed time) | `services/bridge/src/stt/whisperWorker.mjs`, `builtinWhisper.ts`, `config.ts`, `apps/even-client/src/core/controller.ts` |
## Changed files
@@ -54,8 +55,8 @@ Full detail, including failures along the way, is in `docs/test-evidence.md`.
|---|---|
| `npm ci` | clean install, 0 vulnerabilities reported |
| `npm run typecheck` | exit 0 |
| `npm test` | 130 passed, 0 failed (8 files) |
| `npm run test:real-stt` | 4 passed, 0 failed |
| `npm test` | 131 passed, 0 failed (8 files) |
| `npm run test:real-stt` | 7 passed, 0 failed |
| `npm run test:simulator` | 6 passed, 0 failed; 10 consecutive runs of the final revision |
| `npm run test:real-hermes` | 9 passed, 0 failed |
| `npm run client:build` | built; bundle searched for server secrets, none |
@@ -68,7 +69,7 @@ Full detail, including failures along the way, is in `docs/test-evidence.md`.
| Fakes only | 122 tests |
| Even simulator 0.9.5 | 6 scenarios, screenshots in `docs/evidence/simulator/` |
| Real Hermes v0.23.1, stub model | 9 scenarios |
| Real STT | 4 scenarios: built-in Whisper on one clean archive recording |
| Real STT | 7 scenarios: built-in Whisper on one clean archive recording |
| Real model or tools | none |
| Physical device | **none** |
@@ -107,7 +108,8 @@ Offered as places to look, not as findings.
- The client holds captured audio in memory for a possible resend until the user discards it.
- The real-Hermes test covers neither approvals nor the `unknown` status.
- The built-in speech model is fetched from the Hugging Face hub at first start with no pinned
revision or hash, and runs in the bridge's own process (a measured 0.4 s event-loop pause).
revision or hash. It runs in a worker thread of the bridge process, in plain JavaScript
(`whisperWorker.mjs`) that the type checker does not cover.
- The silence threshold in `builtinWhisper.ts` was chosen without any audio from the glasses.
- The systemd hardening options in `deploy/` have never been run with ONNX Runtime.
+4 -3
View File
@@ -56,9 +56,10 @@ Stated so they are not mistaken for protections.
3a. **The built-in model's files are downloaded from the Hugging Face hub on first start** and
are not pinned to a revision or checked against a known hash. A compromised hub account or
path could serve different weights. Pinning a revision is a follow-up.
3b. **While a recording is being transcribed the bridge can pause for a few hundred
milliseconds** (measured about 0.4 s), because the model runs in the same process. Harmless
for one user; a worker thread would remove it.
3b. **The speech model runs in a worker thread of the bridge process**, not a separate
sandboxed process. It no longer stalls the bridge (measured 3 ms), but a crash inside the
native runtime could still take the bridge down; systemd restarts it and in-flight turns are
recovered or failed honestly.
4. **In the simulator, raw audio frames are echoed to the WebView console.** Whether the phone
does the same is unknown and is on the device checklist. If it does, recordings would be
visible to whoever can read that console.
+22 -5
View File
@@ -29,7 +29,7 @@ Exit 0, no diagnostics, for contracts, bridge, client and tests.
```
$ npm test
Test Files 8 passed (8)
Tests 130 passed (130)
Tests 131 passed (131)
```
Run after a clean `npm ci`. Six consecutive full runs were green after the fix described under
@@ -40,7 +40,7 @@ Run after a clean `npm ci`. Six consecutive full runs were green after the fix d
| `packages/contracts/test/contracts.test.ts` | 4 | Schema acceptance and rejection, error-code retryability |
| `services/bridge/test/units.test.ts` | 13 | Config guards, credential expiry and renewal, pairing codes, adapter helpers |
| `services/bridge/test/builtinWhisper.test.ts` | 8 | Built-in STT adapter with a stand-in recogniser: silence gate, language, ordering, cancel, load failure |
| `apps/even-client/test/reducer.test.ts` | 24 | State transitions, cancellation, stale and late results, connection loss |
| `apps/even-client/test/reducer.test.ts` | 25 | State transitions, cancellation, stale and late results, connection loss |
| `apps/even-client/test/layout.test.ts` | 16 | Pagination, Unicode, long answers, plain-text conversion, HUD frames fit |
| `apps/even-client/test/events.test.ts` | 8 | Gesture mapping through the SDK's parser, the tap-has-no-eventType quirk |
| `tests/integration/bridge.test.ts` | 40 | Auth, device binding, limits, rate limiting, STT and Hermes outcomes, duplicates, cancel, restart, redaction |
@@ -150,10 +150,10 @@ later turns well under 1 s. These are not performance figures for anything real.
```
$ npm run test:real-stt
Tests 4 passed (4)
Tests 7 passed (7)
```
`onnx-community/whisper-base`, 8-bit weights, run in the bridge process on the CPU. The input is
`onnx-community/whisper-base`, 8-bit weights, run on the CPU in a worker thread of the bridge. The input is
`tests/real-stt/fixtures/jfk.wav`, 11.0 s of clean archive speech, uploaded through
`POST /v1/turns` as raw PCM exactly as the client uploads a capture.
@@ -163,6 +163,9 @@ $ npm run test:real-stt
| Three seconds of digital silence: `stt_empty`, the agent is not called | pass |
| Three seconds of very quiet noise: `stt_empty` | pass |
| A second recording with the model already loaded | pass |
| Two devices submit recordings at the same moment: both transcribed, in turn | pass |
| A model id that does not exist: speech is `unavailable`, the bridge keeps answering | pass |
| Closing the adapter stops the worker; a closed adapter refuses work | pass |
Transcript produced: "And so my fellow Americans, ask not what your country can do for you, ask
what you can do for your country." That matches the recording.
@@ -174,7 +177,7 @@ Measurements from this run, one sample each, on an Intel i7-13850HX desktop:
| Model load (files already cached) | 1.2 to 3.8 s |
| 11.0 s of speech | 1.6 s |
| Memory (resident) after loading / after transcribing | about 0.5 GB / 0.8 GB |
| Longest event-loop pause during transcription | 0.39 s |
| Longest event-loop pause during transcription | 0.39 s with the model in the bridge's main thread; **3 ms** after moving it to a worker thread |
| Same clip with the process pinned to one core | 1.7 s (no slower) |
Things this run also showed that are worth knowing:
@@ -190,6 +193,20 @@ Things this run also showed that are worth knowing:
The bridge process was also started with default settings: it logged `stt.loading`, then
`stt.ready`, and answered `/healthz` throughout.
### Polling intervals
Shortened at Dan's request. These are settings, and their effect on a real answer has not been
measured on a phone.
| Loop | Before | Now |
|---|---|---|
| Bridge asks Hermes for run status | every 500 ms | every 150 ms (`HERMES_POLL_INTERVAL_MS`) |
| App asks the bridge for the turn | every 700 ms | every 200 ms for the first 8 s, 400 ms until 30 s, then 1 s |
Worst-case delay added by polling to an answer that arrives within 8 s: about 0.35 s, down from
about 1.2 s. The app's schedule eases off for long agent runs to limit radio use; battery impact
is unmeasured.
### Build and packaging
```