Cotton code
61 lines · 4.5 KB · TypeScriptRaw
/** * READ COST 3 (coordinator, 28 Nov 2026): live, with the switch on, no shadow was asked for. This walks the live shape * with nothing pretended: a file really over ORDER_INDEX_MAX_BYTES, both locks sealed, the row counts counted, the * switch set through its admin door, and list_bin and log_session called at /mcp. Synthetic rows only. */import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { ORDER_INDEX_MAX_BYTES, Store } from "../src/store.ts";import { ADMIN, http, personalObject, tool } from "./personal.ts";const SLUG = "read-cost-live-shape";const stub = () => env.BUSINESS.get(env.BUSINESS.idFromName(SLUG));describe("on a real file over the index limit, the lookups ask for their shadows (READ COST 3)", { timeout: 900_000 }, () => {  it("asks for, fills and walks the shadows of log_session and list_bin", 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: Store }).store;      for (const key of ["memory_lock", "open_tables_lock"]) store.setMeta(key, JSON.stringify({ on_at: now, by: "birth", sealing_at: now }));      const fill = (table: string, n: number, data: string, prefix = table) =>        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 ?, ? || '-' || i, ${data}, NULL, 1, 0, ?, ? FROM n`,          n, table, prefix, now, now,        );      fill("session", 20_000, `json_object('tool', 'claude-code', 'external_id', 'invented-ext-' || i, 'sealed_enc', 's1:AAAA', 'dek_enc', 'd1:AAAA', 'seal_v', 1)`);      fill("note", 20_000, `json_object('title', 'binned ' || i, 'trashed_at', '${now}')`, "binned-note");      state.storage.sql.exec("CREATE TABLE IF NOT EXISTS filler (b BLOB)");      while (state.storage.sql.databaseSize < ORDER_INDEX_MAX_BYTES + 4 * 1024 * 1024) state.storage.sql.exec("INSERT INTO filler (b) SELECT randomblob(1048576) FROM (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4)");      while (store.carryCounts(10_000));    });    const on = await http<{ on: boolean; shadows: unknown[] }>(SLUG, "POST", "/v1/admin/order-shadow", { on: true }, ADMIN);    expect(on.body.on).toBe(true);    const lb = await tool(SLUG, owner, "list_bin", { limit: 5 });    console.log(`READ COST 3 list_bin: ${JSON.stringify(lb).slice(0, 300)}`);    const ls = await tool(SLUG, owner, "log_session", { tool: "claude-code", externalId: "invented-ext-7", machine: "invented-mac", lastActiveAt: now });    console.log(`READ COST 3 log_session: ${JSON.stringify(ls).slice(0, 300)}`);    type Got = { on: boolean; slug: string; over_index_limit: boolean; counts_ready: boolean; file_bytes: number; shadows: { name: string; ready: boolean }[]; lookups: { name: string; said: string; rows: number | null }[] };    const got = await http<Got>(SLUG, "GET", "/v1/admin/order-shadow", undefined, ADMIN);    console.log(`READ COST 3 order-shadow: ${JSON.stringify(got.body)}`);    // What the coordinator reads on live: this object, past the limit, counted, each lookup asking for its shadow.    expect(got.body).toMatchObject({ on: true, slug: SLUG, over_index_limit: true, counts_ready: true });    expect(got.body.file_bytes).toBeGreaterThan(ORDER_INDEX_MAX_BYTES);    expect(got.body.shadows.map((s) => s.name)).toEqual(expect.arrayContaining(["bin_note__trashed_at", "eq_session__external_id"]));    expect(got.body.lookups).toEqual(expect.arrayContaining([expect.objectContaining({ name: "bin_note__trashed_at", said: "walked: shadow filling", rows: 5_000 })]));    // The alarm fills them; then the lookups say "shadow" and list_bin reads along it.    await runInDurableObject(stub(), (instance) => {      const store = (instance as unknown as { store: Store }).store;      while (store.shadowStep(2_500));    });    await tool(SLUG, owner, "list_bin", { limit: 5 });    const filled = await http<Got>(SLUG, "GET", "/v1/admin/order-shadow", undefined, ADMIN);    console.log(`READ COST 3 filled: ${JSON.stringify(filled.body.lookups)}`);    for (const sh of filled.body.shadows) expect(sh.ready, sh.name).toBe(true);    expect(filled.body.lookups).toEqual(expect.arrayContaining([expect.objectContaining({ name: "bin_note__trashed_at", said: "shadow" })]));  });});