diff --git a/pm-stack/voice-line-worker/src/ledger.ts b/pm-stack/voice-line-worker/src/ledger.ts index d5bd5d2fdf..14b90dd5db 100644 --- a/pm-stack/voice-line-worker/src/ledger.ts +++ b/pm-stack/voice-line-worker/src/ledger.ts @@ -40,11 +40,26 @@ interface PipelineResult { type: string; response?: { type: string; - result?: { cols: unknown[]; rows: Array<{ values: TursoValue[] }> }; + // Turso HTTP pipeline v2: each row is a bare array of values, + // NOT an object with a `values` field (verified live 2026-11-16, + // task 16482 — reading `.values` off the row array returns + // Array.prototype.values and destructuring it throws + // "function is not iterable"). + result?: { cols: unknown[]; rows: TursoValue[][] }; }; }>; } +/** Parse the newest queue_state snapshot out of a pipeline SELECT result. */ +export function parseQueueSnapshotResult(out: PipelineResult): QueueSnapshot | null { + const rows = out.results[0]?.response?.result?.rows ?? []; + if (rows.length === 0) return null; + const [payload, at] = rows[0]; + if (!payload || !at || payload.type !== "text" || at.type !== "text") return null; + const parsed = JSON.parse(payload.value) as Omit; + return { ...parsed, at: at.value }; +} + export class TursoClient { constructor( private url: string, @@ -124,11 +139,6 @@ export class TursoClient { args: [], }, ]); - const rows = out.results[0]?.response?.result?.rows ?? []; - if (rows.length === 0) return null; - const [payload, at] = rows[0].values; - if (payload.type !== "text" || at.type !== "text") return null; - const parsed = JSON.parse(payload.value) as Omit; - return { ...parsed, at: at.value }; + return parseQueueSnapshotResult(out); } } diff --git a/pm-stack/voice-line-worker/test/ledger.test.ts b/pm-stack/voice-line-worker/test/ledger.test.ts new file mode 100644 index 0000000000..291b054cfe --- /dev/null +++ b/pm-stack/voice-line-worker/test/ledger.test.ts @@ -0,0 +1,56 @@ +// lane-scratch: code-c /tmp/lane-code-c-20261116-0412 task 16482 +import { describe, expect, test } from "bun:test"; +import { parseQueueSnapshotResult } from "../src/ledger"; + +// Oracle: the live Turso HTTP pipeline v2 response for +// SELECT payload, at FROM status_snapshots WHERE kind='queue_state' +// captured 2026-11-16 during the #9800 deploy proof (task 16482). +// Rows are bare arrays of typed values — the original code read +// `rows[0].values` (an object shape the API does not return), which +// resolved to Array.prototype.values and threw +// "function is not iterable" in the Durable Object. +const LIVE_SHAPE = { + results: [ + { + type: "ok", + response: { + type: "execute", + result: { + cols: [ + { name: "payload", decltype: "TEXT" }, + { name: "at", decltype: "TEXT" }, + ], + rows: [ + [ + { type: "text", value: '{"queued":116,"claimed":10,"parked":18,"blocked":552}' }, + { type: "text", value: "2026-11-16T10:23:47.659Z" }, + ], + ], + }, + }, + }, + { type: "ok", response: { type: "close" } }, + ], +} as const; + +describe("parseQueueSnapshotResult", () => { + test("parses the live v2 row shape", () => { + const snap = parseQueueSnapshotResult(LIVE_SHAPE as never); + expect(snap).toEqual({ + queued: 116, + claimed: 10, + parked: 18, + blocked: 552, + at: "2026-11-16T10:23:47.659Z", + }); + }); + + test("no rows -> null", () => { + const empty = { + results: [ + { type: "ok", response: { type: "execute", result: { cols: [], rows: [] } } }, + ], + }; + expect(parseQueueSnapshotResult(empty as never)).toBeNull(); + }); +});