/** * LIVE READS (coordinator, 28 Nov 2026): live broke at 093315d0fb. Every MCP tool that reads records hung ~34 s and * the object was reset for CPU, on an account of hundreds of thousands of rows, while the same reads took 1 s before. * ADMIN KEYS 2 had the call's view ask `definitions()` once per table, and `definitions()` asked where both sealing * locks stand, which on a fully sealed table seeks through every row for an open one. Test databases are tiny, so * nothing failed. * * Here: an object with both locks sealed and 200k sealed rows in one locked table. One `get_record_by_table` of a note * must read a handful of rows, whatever the size of the database: the rows SQLite reads for the call are counted. */ import { env, runInDurableObject } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import { personalObject, tool } from "./personal.ts"; const SLUG = "read-cost"; const ROWS = 200_000; const stub = () => env.BUSINESS.get(env.BUSINESS.idFromName(SLUG)); type Counted = { rowsRead: number; calls: number; cursors: SqlStorageCursor>[]; said: string[] }; describe("a read's cost does not grow with the database (LIVE READS)", () => { it("get_record_by_table reads a handful of rows beside 200k sealed rows of a locked table", async () => { const { owner } = await personalObject(SLUG, { open: ["note"] }); const now = new Date().toISOString(); await runInDurableObject(stub(), (instance, state) => { const store = (instance as unknown as { store: { setMeta(k: string, v: string): void } }).store; // Both locks on and past handing, as on the owner's account: every locked table is served sealed. for (const key of ["memory_lock", "open_tables_lock"]) store.setMeta(key, JSON.stringify({ on_at: now, by: "birth", sealing_at: now })); state.storage.sql.exec( `WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ?) INSERT INTO rows (table_id, id, data, owner, version, deleted, created_at, updated_at) SELECT 'session', 'session-' || i, '{"sealed_enc":"s1:AAAA","dek_enc":"d1:AAAA","seal_v":1}', NULL, 1, 0, ?, ? FROM n`, ROWS, now, now, ); state.storage.sql.exec( "INSERT INTO rows (table_id, id, data, owner, version, deleted, created_at, updated_at) VALUES ('note', 'note-1', ?, NULL, 1, 0, ?, ?)", JSON.stringify({ title: "a note", body: "words" }), now, now, ); }); // A warm call first: the object's own once-per-instance work (definitions parsed, the kit kept) is not the read's. expect((await tool(SLUG, owner, "get_record_by_table", { table: "note", id: "note-1" })).isError).toBe(false); const costOf = async (name: string, args: Record): Promise<{ rowsRead: number; calls: number; text: string }> => { await runInDurableObject(stub(), (_i, state) => { const sql = state.storage.sql as unknown as { exec: (...a: unknown[]) => SqlStorageCursor>; __counted?: Counted; __exec?: (...a: unknown[]) => SqlStorageCursor> }; const exec = (sql.__exec ??= sql.exec.bind(sql)); const counted: Counted = { rowsRead: 0, calls: 0, cursors: [], said: [] }; sql.__counted = counted; sql.exec = (...a: unknown[]) => { const cursor = exec(...a); counted.calls++; counted.cursors.push(cursor); counted.said.push(String(a[0]).replace(/\s+/g, " ").slice(0, 300)); return cursor; }; }); // A sealed table's refusal (a window to ask for) is an answer too: what is counted is what the call read. const text = JSON.stringify(await tool(SLUG, owner, name, args)); const { rowsRead, calls, heaviest } = await runInDurableObject(stub(), (_i, state) => { const counted = (state.storage.sql as unknown as { __counted?: Counted }).__counted!; return { rowsRead: counted.cursors.reduce((n, c) => n + c.rowsRead, 0), calls: counted.calls, heaviest: counted.cursors.map((c, i) => ({ n: c.rowsRead, sql: counted.said[i] })).filter((x) => x.n > ROWS / 20) }; }); for (const h of heaviest) console.log(`LIVE READS heavy: ${h.n} rows: ${h.sql}`); return { rowsRead, calls, text }; }; const one = await costOf("get_record_by_table", { table: "note", id: "note-1" }); expect(one.text).toContain("note-1"); expect(one.calls, "the patched exec saw the call's statements").toBeGreaterThan(0); // The other reads that hung on live, each on its own: none may walk the locked table's rows. (list_bin is left out: it // reads each binned table along its rows by its own query, unchanged since before the break; a walk, not the break.) for (const [name, args] of [ ["get_record_by_table", { table: "note", id: "note-1" }], ["list_records", { table: "note", limit: 1 }], ["list_todos", {}], ["list_projects", {}], ["list_changes", { limit: 1 }], ["list_sessions", {}], ["search_records", { query: "note" }], ] as const) { const { rowsRead, calls } = await costOf(name, args); console.log(`LIVE READS cost: ${name} read ${rowsRead} rows in ${calls} statements`); expect(rowsRead, `one ${name} read ${rowsRead} rows in ${calls} statements`).toBeLessThan(ROWS / 20); } }, 300_000); });