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}`;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { SCHEMA_VERSION, type TurnResult } from '@hermeven/contracts';
|
||||
import { pollDelay } from '../src/core/controller.ts';
|
||||
import { initialState, reduce, type AppEvent, type AppState, type Effect } from '../src/core/reducer.ts';
|
||||
|
||||
const ID1 = '11111111-1111-4111-8111-111111111111';
|
||||
@@ -39,6 +40,18 @@ function result(requestId: string, over: Partial<TurnResult> = {}): TurnResult {
|
||||
};
|
||||
}
|
||||
|
||||
describe('poll schedule', () => {
|
||||
it('checks often while an answer is likely, and eases off for long runs', () => {
|
||||
expect(pollDelay(0)).toBe(200);
|
||||
expect(pollDelay(7_999)).toBe(200);
|
||||
expect(pollDelay(8_000)).toBe(400);
|
||||
expect(pollDelay(30_000)).toBe(1000);
|
||||
expect(pollDelay(600_000)).toBe(1000);
|
||||
// Stays far inside the bridge's 600 requests per minute per device.
|
||||
expect(60_000 / pollDelay(0)).toBeLessThan(600);
|
||||
});
|
||||
});
|
||||
|
||||
describe('happy path', () => {
|
||||
it('READY -> LISTENING -> TRANSCRIBING -> THINKING -> ANSWER', () => {
|
||||
let s = ready();
|
||||
|
||||
Reference in New Issue
Block a user