/** * LIVE CPU 2 (coordinator, 23 Nov 2026). A plain request on his object reads its definitions at most once (parsing * them costs megabytes): while People are sealed and no phone has handed the object its arrival key, keepOwner no * longer tries the owner's sealed link on every request (four reads of every definition and a write rolled back, each * time). Once the key is held, the request after links him. On the invented object of his shape; `BIG_OBJECT=1` at * full size. */ import { env, runInDurableObject } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import { BIG, personalShape, writeShape } from "./big_object.ts"; import { http, personalObject } from "./personal.ts"; type Watched = { store: { definitions: (k: string) => unknown; seen?: number; who?: string[] } }; async function watch(stub: DurableObjectStub): Promise { await runInDurableObject(stub, (instance: unknown) => { const store = (instance as Watched).store; if (store.seen !== undefined) return; const real = store.definitions.bind(store); store.seen = 0; store.definitions = (k: string) => { store.seen! += 1; (store.who ??= []).push((new Error().stack ?? "").split("\n").slice(2, 4).map((l) => l.trim().replace(/\(.*\/src\//, "(")).join(" < ")); return real(k); }; }); } const reads = (stub: DurableObjectStub) => runInDurableObject(stub, (instance: unknown) => { const store = (instance as Watched).store; const n = { n: store.seen ?? 0, who: (store.who ?? []).join("; ") }; store.seen = 0; store.who = []; return n; }); const changes = (stub: DurableObjectStub) => runInDurableObject(stub, (_i, s) => Number(s.storage.sql.exec<{ n: number }>("SELECT count(*) AS n FROM change").one().n)); describe("a plain request's definitions", { timeout: 900_000 }, () => { for (const arrival of [false, true]) { it(`are read at most once a request ${arrival ? "with" : "without"} the arrival key held`, async () => { const slug = `request-defs-big-${arrival}`; const { credential } = await personalObject(slug, { arrival }); const phone = { authorization: `Bearer ${credential}` }; const stub = env.BUSINESS.get(env.BUSINESS.idFromName(slug)); await runInDurableObject(stub, (_i, s) => { const last = writeShape(s.storage.sql, personalShape(BIG ? 1.25 : 1 / 200)); s.storage.sql.exec("UPDATE meta SET value = CAST(? AS TEXT) WHERE key = 'version'", last); }); // row_counts counted, as on his object long since: the alarm's recount is not running beside the requests. await runInDurableObject(stub, (instance, s) => { const store = (instance as unknown as { store: { carryCounts(steps: number, tx: (step: () => void) => void): boolean } }).store; while (store.carryCounts(50, (step) => s.storage.transactionSync(step))); }); await http(slug, "GET", "/v1/send/inbox", undefined, phone); await watch(stub); const before = await changes(stub); for (const path of ["/v1/send/inbox", "/v1/chat/inbox", "/v1/send/inbox"]) { // The alarm's own passes read the definitions too; they are the alarm's, not the request's: held off here. await runInDurableObject(stub, (_i, st) => st.storage.setAlarm(Date.now() + 3_600_000)); expect((await http(slug, "GET", path, undefined, phone)).status).toBe(200); const r = await reads(stub); expect(r.n, `${path}: ${r.who}`).toBeLessThanOrEqual(1); } // Nothing written by a request that asked only to read. expect(await changes(stub)).toBe(before); }); } });