Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions pm-stack/voice-line-worker/src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueueSnapshot, "at">;
return { ...parsed, at: at.value };
}

export class TursoClient {
constructor(
private url: string,
Expand Down Expand Up @@ -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<QueueSnapshot, "at">;
return { ...parsed, at: at.value };
return parseQueueSnapshotResult(out);
}
}
56 changes: 56 additions & 0 deletions pm-stack/voice-line-worker/test/ledger.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});