From 66742293d9518b98f88fb54e0dc139a112ec2c61 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Mon, 16 Nov 2026 04:29:55 -0600 Subject: [PATCH] fix(voice-line): WAV-wrap PCM for Whisper + surface processing errors (#9800) --- pm-stack/voice-line-worker/src/session.ts | 38 +++++++++++++++++++-- pm-stack/voice-line-worker/test/wav.test.ts | 37 ++++++++++++++++++++ pm-stack/voice-line-worker/wrangler.toml | 2 +- 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 pm-stack/voice-line-worker/test/wav.test.ts diff --git a/pm-stack/voice-line-worker/src/session.ts b/pm-stack/voice-line-worker/src/session.ts index 0b6de8f56d..3b943399bc 100644 --- a/pm-stack/voice-line-worker/src/session.ts +++ b/pm-stack/voice-line-worker/src/session.ts @@ -45,6 +45,33 @@ function base64ToBytes(b64: string): Uint8Array { return out; } +export function wavEncodePcm16(pcm: Uint8Array, sampleRate: number): Uint8Array { + // Wrap raw PCM16LE mono in a 44-byte RIFF/WAVE header. Whisper + // rejects headerless PCM with AiError "Invalid input" (code 8001) — + // found live during the task 16482 deploy proof, 2026-11-16: the + // binding call threw, the utterance died silently, no ledger row. + const out = new Uint8Array(44 + pcm.byteLength); + const view = new DataView(out.buffer); + const writeAscii = (off: number, text: string) => { + for (let i = 0; i < text.length; i++) view.setUint8(off + i, text.charCodeAt(i)); + }; + writeAscii(0, "RIFF"); + view.setUint32(4, 36 + pcm.byteLength, true); + writeAscii(8, "WAVE"); + writeAscii(12, "fmt "); + view.setUint32(16, 16, true); + view.setUint16(20, 1, true); + view.setUint16(22, 1, true); + view.setUint32(24, sampleRate, true); + view.setUint32(28, sampleRate * 2, true); + view.setUint16(32, 2, true); + view.setUint16(34, 16, true); + writeAscii(36, "data"); + view.setUint32(40, pcm.byteLength, true); + out.set(pcm, 44); + return out; +} + export class VoiceSession { private chunks: Uint8Array[] = []; private startedAt = new Date().toISOString(); @@ -86,7 +113,14 @@ export class VoiceSession { this.startedAt = new Date().toISOString(); this.send(ws, { type: "ready" }); } else if (control.type === "stop") { - await this.processUtterance(ws); + try { + await this.processUtterance(ws); + } catch (err) { + this.send(ws, { + type: "error", + error: `processing failed: ${err instanceof Error ? err.message : String(err)}`, + }); + } } else { this.send(ws, { type: "error", error: `unknown control type: ${control.type ?? "(none)"}` }); } @@ -113,7 +147,7 @@ export class VoiceSession { // Speech-to-text: ONLY the trimmed audio is submitted (never billed // for silence — #9800). const stt = (await this.env.AI.run(STT_MODEL, { - audio: bytesToBase64(trimmed.audio), + audio: bytesToBase64(wavEncodePcm16(trimmed.audio, SAMPLE_RATE)), })) as { text?: string }; const transcript = (stt.text ?? "").trim(); this.send(ws, { diff --git a/pm-stack/voice-line-worker/test/wav.test.ts b/pm-stack/voice-line-worker/test/wav.test.ts new file mode 100644 index 0000000000..313757587d --- /dev/null +++ b/pm-stack/voice-line-worker/test/wav.test.ts @@ -0,0 +1,37 @@ +// lane-scratch: code-c /tmp/lane-code-c-20261116-0412 task 16482 +import { describe, expect, test } from "bun:test"; +import { wavEncodePcm16 } from "../src/session"; + +// Oracle: Whisper (Workers AI) accepts a RIFF/WAVE container and rejects +// headerless PCM with AiError "Invalid input" (code 8001) — verified live +// against @cf/openai/whisper-large-v3-turbo during the #9800 deploy proof +// (task 16482, 2026-11-16). The encoder must produce a standards-shaped +// 44-byte PCM16 mono header and preserve the payload byte-for-byte. +describe("wavEncodePcm16", () => { + test("wraps PCM in a 44-byte RIFF/WAVE header", () => { + const pcm = new Uint8Array([0x01, 0x00, 0xff, 0x7f, 0x00, 0x80]); + const wav = wavEncodePcm16(pcm, 16000); + expect(wav.byteLength).toBe(44 + pcm.byteLength); + const ascii = (off: number, n: number) => + String.fromCharCode(...wav.subarray(off, off + n)); + expect(ascii(0, 4)).toBe("RIFF"); + expect(ascii(8, 4)).toBe("WAVE"); + expect(ascii(12, 4)).toBe("fmt "); + expect(ascii(36, 4)).toBe("data"); + const view = new DataView(wav.buffer); + expect(view.getUint32(4, true)).toBe(36 + pcm.byteLength); + expect(view.getUint16(20, true)).toBe(1); // PCM format + expect(view.getUint16(22, true)).toBe(1); // mono + expect(view.getUint32(24, true)).toBe(16000); // sample rate + expect(view.getUint32(28, true)).toBe(32000); // byte rate + expect(view.getUint16(34, true)).toBe(16); // bits per sample + expect(view.getUint32(40, true)).toBe(pcm.byteLength); + expect([...wav.subarray(44)]).toEqual([...pcm]); + }); + + test("empty PCM still yields a valid header", () => { + const wav = wavEncodePcm16(new Uint8Array(0), 16000); + expect(wav.byteLength).toBe(44); + expect(new DataView(wav.buffer).getUint32(40, true)).toBe(0); + }); +}); diff --git a/pm-stack/voice-line-worker/wrangler.toml b/pm-stack/voice-line-worker/wrangler.toml index 75e81bb73b..52d840378f 100644 --- a/pm-stack/voice-line-worker/wrangler.toml +++ b/pm-stack/voice-line-worker/wrangler.toml @@ -18,4 +18,4 @@ new_sqlite_classes = ["VoiceSession"] # Turso session ledger (#9800). TURSO_TOKEN and STATUS_INGEST_TOKEN are # secrets (wrangler secret put), never vars in this file. [vars] -TURSO_URL = "REPLACE_WITH_TURSO_DATABASE_URL" +TURSO_URL = "https://voice-line-ledger-dezren39.aws-us-east-1.turso.io"