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:
@@ -50,6 +50,7 @@ export interface ControllerOptions {
|
||||
onChange?: (snapshot: Snapshot) => void;
|
||||
/** Receives each diagnostic entry (ids, codes, timings; never content). */
|
||||
onNote?: (entry: DiagEntry) => void;
|
||||
/** Override the poll schedule with one fixed interval (tests). */
|
||||
pollIntervalMs?: number;
|
||||
submitAttempts?: number;
|
||||
submitRetryMs?: number;
|
||||
@@ -88,7 +89,7 @@ export class Controller {
|
||||
private readonly device: DevicePort;
|
||||
private readonly now: () => number;
|
||||
private readonly newId: () => string;
|
||||
private readonly pollIntervalMs: number;
|
||||
private readonly pollIntervalMs: number | null;
|
||||
private readonly submitAttempts: number;
|
||||
private readonly submitRetryMs: number;
|
||||
private readonly micStallMs: number;
|
||||
@@ -97,7 +98,7 @@ export class Controller {
|
||||
this.device = options.device;
|
||||
this.now = options.now ?? Date.now;
|
||||
this.newId = options.newId ?? (() => crypto.randomUUID());
|
||||
this.pollIntervalMs = options.pollIntervalMs ?? 700;
|
||||
this.pollIntervalMs = options.pollIntervalMs ?? null;
|
||||
this.submitAttempts = options.submitAttempts ?? 4;
|
||||
this.submitRetryMs = options.submitRetryMs ?? 1500;
|
||||
this.micStallMs = options.micStallMs ?? 3000;
|
||||
@@ -422,8 +423,10 @@ export class Controller {
|
||||
private async poll(generation: number, requestId: string): Promise<void> {
|
||||
if (!this.client) return;
|
||||
let failures = 0;
|
||||
const started = this.now();
|
||||
for (;;) {
|
||||
await sleep(failures ? Math.min(5000, this.pollIntervalMs * 2 ** Math.min(failures, 3)) : this.pollIntervalMs);
|
||||
const interval = this.pollIntervalMs ?? pollDelay(this.now() - started);
|
||||
await sleep(failures ? Math.min(5000, Math.max(interval, 250) * 2 ** Math.min(failures, 3)) : interval);
|
||||
if (this.stale(generation)) return;
|
||||
try {
|
||||
const result = await this.client.getTurn(requestId);
|
||||
@@ -656,6 +659,17 @@ export class Controller {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* How long to wait before asking the bridge again, given how long this turn has been running.
|
||||
* Most answers arrive within a few seconds, so checks are frequent then; a long agent run is
|
||||
* checked less often to spare the phone's radio and battery.
|
||||
*/
|
||||
export function pollDelay(elapsedMs: number): number {
|
||||
if (elapsedMs < 8_000) return 200;
|
||||
if (elapsedMs < 30_000) return 400;
|
||||
return 1000;
|
||||
}
|
||||
|
||||
function describe(err: unknown): string {
|
||||
if (err instanceof BridgeApiError) return `${err.body.code} (HTTP ${err.status})`;
|
||||
if (err instanceof Error) return `${err.name}: ${err.message}`;
|
||||
|
||||
Reference in New Issue
Block a user