{"version":103,"agent":"codex-capacity-policy","message":"Test deterministic replay and account view cache weight","time":1791758093484,"timeIso":"2026-10-11T22:34:53.484Z","changes":9,"change":1226,"files":["src/engine/test/record-query-contract.test.ts","src/engine/test/view.test.ts","src/engine/view.ts"],"draft":null,"diff":[{"path":"src/engine/test/record-query-contract.test.ts","added":127,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/record-query-contract.test.ts\n@@ -0,0 +1,127 @@\n+// A small live graph and a graph rebuilt from its serialized history must\n+// answer the same one-file record query.\n+\n+import assert from \"node:assert/strict\";\n+import { describe, it } from \"node:test\";\n+import { buildAtom, Graph, View } from \"../index.ts\";\n+import { lines, Repo } from \"./helpers.ts\";\n+\n+describe(\"record-query replay contract\", () => {\n+\tit(\"matches a one-file record query after repeated writers, readers, and delete/recreate history\", () => {\n+\t\tconst repo = Repo.init({ \"src.txt\": lines(\"top\", \"bottom\"), \"recreated.txt\": lines(\"old\") });\n+\t\tconst base = [\"a0\"];\n+\t\tconst insertAtTopGap = (text: string) =>\n+\t\t\trepo.edit({ edits: [{ file: \"src.txt\", start: 1, end: 1, lines: [text], mode: \"after\" }] }, base);\n+\t\tconst writerOne = insertAtTopGap(\"writer one\");\n+\t\tconst writerTwo = insertAtTopGap(\"writer two\");\n+\t\tconst readerOne = insertAtTopGap(\"reader one\");\n+\t\tconst readerTwo = insertAtTopGap(\"reader two\");\n+\t\tconst attrOne = repo.edit({ attrs: [{ file: \"src.txt\", name: \"exec\", value: \"1\" }] }, base);\n+\t\tconst attrTwo = repo.edit({ attrs: [{ file: \"src.txt\", name: \"exec\", value: null }] }, base);\n+\t\tconst deletion = repo.edit({ edits: [{ file: \"recreated.txt\", old: lines(\"old\"), new: \"\" }] });\n+\t\tconst recreation = repo.edit({ edits: [{ file: \"recreated.txt\", old: \"\", new: lines(\"fresh\") }] });\n+\n+\t\t// Nine existing atoms make a finite, repeatable history. The last query\n+\t\t// both replaces a line and inserts at the repeatedly written gap.\n+\t\tassert.equal(repo.graph.size, 9);\n+\t\tassert.deepEqual(recreation.deps, [\"a0\", deletion.id]);\n+\t\tassert.deepEqual(repo.view().attrs(\"src.txt\"), {\n+\t\t\texists: false,\n+\t\t\texec: false,\n+\t\t\tlink: null,\n+\t\t\tblob: null,\n+\t\t});\n+\n+\t\tconst input = {\n+\t\t\tauthor: \"record-query-test\",\n+\t\t\tmessage: \"record one file\",\n+\t\t\ttime: 100,\n+\t\t\tedits: [\n+\t\t\t\t{ file: \"src.txt\", start: 0, end: 1, lines: [\"TOP\"] },\n+\t\t\t\t{ file: \"src.txt\", start: 1, end: 1, lines: [\"recorded\"] },\n+\t\t\t],\n+\t\t\tattrs: [{ file: \"src.txt\", name: \"exec\" as const, value: \"1\" }],\n+\t\t};\n+\t\tconst liveAtom = buildAtom(repo.view(), input);\n+\t\tconst expectedDeps = [\"a0\", writerOne.id, writerTwo.id, readerOne.id, readerTwo.id, attrOne.id, attrTwo.id];\n+\t\tassert.deepEqual(liveAtom.deps, expectedDeps);\n+\t\tassert.equal(liveAtom.deps.length, 7);\n+\t\tassert.equal(liveAtom.writes.length, 3);\n+\t\tassert.equal(liveAtom.reads.length, 2);\n+\t\tassert.equal(liveAtom.holds.length, 0);\n+\t\tassert.equal(liveAtom.seq, 9);\n+\t\tassert.deepEqual(liveAtom.deletes, [\"a0.1\"]);\n+\t\tassert.deepEqual(liveAtom.inserts, [\n+\t\t\t{ file: \"src.txt\", parent: \"a0.1\", lines: [\"TOP\"] },\n+\t\t\t{ file: \"src.txt\", parent: \"a0.1\", lines: [\"recorded\"] },\n+\t\t]);\n+\t\tassert.deepEqual(liveAtom.attrs, [{ file: \"src.txt\", name: \"exec\", value: \"1\" }]);\n+\n+\t\tconst coldGraph = Graph.fromJSON(JSON.parse(JSON.stringify(repo.graph.toJSON())));\n+\t\tconst coldSet = new Set(repo.shared);\n+\t\tconst coldAtom = buildAtom(new View(coldGraph, coldSet), input);\n+\t\tassert.deepEqual(coldAtom, liveAtom);\n+\t\tassert.equal(JSON.stringify(coldAtom), JSON.stringify(liveAtom));\n+\t\tconst perturbedInput = {\n+\t\t\t...input,\n+\t\t\tedits: input.edits.map((edit, index) => (index === 1 ? { ...edit, lines: [\"different\"] } : edit)),\n+\t\t};\n+\t\tconst perturbedAtom = buildAtom(new View(coldGraph, coldSet), perturbedInput);\n+\t\tassert.notDeepEqual(perturbedAtom, liveAtom);\n+\t\tassert.notEqual(JSON.stringify(perturbedAtom), JSON.stringify(liveAtom));\n+\n+\t\trepo.graph.add(liveAtom);\n+\t\trepo.shared.add(liveAtom.id);\n+\t\tcoldGraph.add(coldAtom);\n+\t\tcoldSet.add(coldAtom.id);\n+\t\tconst liveView = new View(repo.graph, repo.shared);\n+\t\tconst coldView = new View(coldGraph, coldSet);\n+\t\tconst visible = (view: View, path: string) => view.lines(path).map((line) => [line.id, line.text, line.atom]);\n+\t\tassert.deepEqual(visible(liveView, \"src.txt\"), visible(coldView, \"src.txt\"));\n+\t\tassert.deepEqual(\n+\t\t\tvisible(liveView, \"src.txt\").map((line) => line[1]),\n+\t\t\t[\"TOP\", \"recorded\", \"reader two\", \"reader one\", \"writer two\", \"writer one\", \"bottom\"],\n+\t\t);\n+\t\tassert.deepEqual(liveView.attrs(\"src.txt\"), coldView.attrs(\"src.txt\"));\n+\t\tassert.equal(liveView.attrs(\"src.txt\").exec, true);\n+\t\tassert.deepEqual(visible(liveView, \"recreated.txt\"), visible(coldView, \"recreated.txt\"));\n+\t\tassert.deepEqual(\n+\t\t\tvisible(liveView, \"recreated.txt\").map((line) => line[1]),\n+\t\t\t[\"fresh\"],\n+\t\t);\n+\t\tassert.deepEqual(\n+\t\t\tliveView.sequence(\"recreated.txt\").map((line) => [line.id, line.text, line.visible, line.deletedBy]),\n+\t\t\t[\n+\t\t\t\t[`${recreation.id}.0`, \"fresh\", true, []],\n+\t\t\t\t[\"a0.0\", \"old\", false, [deletion.id]],\n+\t\t\t],\n+\t\t);\n+\n+\t\tconst recreatedFileInput = {\n+\t\t\tauthor: \"record-query-test\",\n+\t\t\tmessage: \"record the recreated file\",\n+\t\t\ttime: 101,\n+\t\t\tedits: [{ file: \"recreated.txt\", old: lines(\"fresh\"), new: lines(\"FRESH\") }],\n+\t\t};\n+\t\tconst liveRecreatedFileAtom = buildAtom(liveView, recreatedFileInput);\n+\t\tconst coldRecreatedFileAtom = buildAtom(coldView, recreatedFileInput);\n+\t\tassert.deepEqual(liveRecreatedFileAtom.deps, [recreation.id]);\n+\t\tassert.deepEqual(coldRecreatedFileAtom, liveRecreatedFileAtom);\n+\t\tassert.equal(JSON.stringify(coldRecreatedFileAtom), JSON.stringify(liveRecreatedFileAtom));\n+\t\trepo.graph.add(liveRecreatedFileAtom);\n+\t\trepo.shared.add(liveRecreatedFileAtom.id);\n+\t\tcoldGraph.add(coldRecreatedFileAtom);\n+\t\tcoldSet.add(coldRecreatedFileAtom.id);\n+\t\tassert.deepEqual(\n+\t\t\tvisible(new View(repo.graph, repo.shared), \"recreated.txt\"),\n+\t\t\tvisible(new View(coldGraph, coldSet), \"recreated.txt\"),\n+\t\t);\n+\t\tassert.deepEqual(\n+\t\t\tvisible(new View(repo.graph, repo.shared), \"recreated.txt\").map((line) => line[1]),\n+\t\t\t[\"FRESH\"],\n+\t\t);\n+\t\tassert.equal(repo.graph.size, 11);\n+\t\tassert.equal(coldGraph.size, 11);\n+\t\tassert.equal(JSON.stringify(coldGraph.toJSON()), JSON.stringify(repo.graph.toJSON()));\n+\t});\n+});"},{"path":"src/engine/test/view.test.ts","added":78,"removed":0,"diff":"--- a/src/engine/test/view.test.ts\n+++ b/src/engine/test/view.test.ts\n@@ -91,4 +91,82 @@\n \t\tassert.deepEqual(view.knownPaths(), [\"a.txt\", \"b.txt\"]);\n \t\tassert.deepEqual(view.files(), { \"a.txt\": \"a\\n\" });\n \t});\n+\n+\tit(\"accounts for the membership bitmap before layout and stays observational\", () => {\n+\t\tconst small = Repo.init({ \"f.txt\": lines(\"same\") });\n+\t\tconst larger = Repo.init({ \"f.txt\": lines(\"same\") });\n+\t\tfor (let i = 0; i < 8; i++)\n+\t\t\tlarger.edit({ attrs: [{ file: \"f.txt\", name: \"exec\", value: i % 2 === 0 ? \"1\" : null }] });\n+\n+\t\tconst smallView = new View(small.graph, small.shared);\n+\t\tconst largerView = new View(larger.graph, larger.shared);\n+\t\tconst smallBeforeLayout = smallView.weight();\n+\t\tconst largerBeforeLayout = largerView.weight();\n+\t\tassert.ok(smallBeforeLayout >= small.graph.size);\n+\t\tassert.ok(largerBeforeLayout >= larger.graph.size);\n+\t\tassert.equal(largerBeforeLayout - smallBeforeLayout, larger.graph.size - small.graph.size);\n+\t\tassert.equal(largerView.weight(), largerBeforeLayout);\n+\n+\t\tassert.deepEqual(smallView.files(), largerView.files());\n+\t\tconst afterLayout = largerView.weight();\n+\t\tassert.equal(largerView.weight(), afterLayout);\n+\t\tassert.deepEqual(largerView.files(), { \"f.txt\": \"same\\n\" });\n+\t});\n+\n+\tit(\"accounts for lazy view-owned containers and relocation/index growth\", () => {\n+\t\tconst repo = Repo.init({ \"a.txt\": lines(\"one\", \"two\"), \"b.txt\": lines(\"base\") });\n+\t\trepo.edit({ move: { file: \"a.txt\", start: 0, end: 1, to: \"b.txt\", at: 1 } });\n+\t\tconst deletion = repo.edit({ edits: [{ file: \"b.txt\", old: \"base\\n\", new: \"\" }] });\n+\t\tconst view = repo.view();\n+\t\tlet weight = view.weight();\n+\t\tassert.equal(view.weight(), weight);\n+\n+\t\tassert.equal(view.set.size, 3);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tweight = view.weight();\n+\t\tassert.deepEqual(view.knownPaths(), [\"a.txt\", \"b.txt\"]);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tweight = view.weight();\n+\t\tassert.equal(view.fileOf(\"a0.0\"), \"b.txt\");\n+\t\tassert.ok(view.weight() > weight);\n+\n+\t\tconst expectedFiles = { \"a.txt\": \"two\\n\", \"b.txt\": \"one\\n\" };\n+\t\tassert.deepEqual(view.files(), expectedFiles);\n+\t\tweight = view.weight();\n+\t\tassert.deepEqual(\n+\t\t\tview.sequence(\"b.txt\").map((line) => [line.text, line.visible, line.deletedBy]),\n+\t\t\t[\n+\t\t\t\t[\"base\", false, [deletion.id]],\n+\t\t\t\t[\"one\", true, []],\n+\t\t\t],\n+\t\t);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tweight = view.weight();\n+\t\tassert.equal(view.sequenceIndex(\"b.txt\", \"a0.0\"), 1);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tweight = view.weight();\n+\t\tassert.equal(view.lineNumber(\"b.txt\", \"a0.0\"), 0);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tweight = view.weight();\n+\t\tassert.deepEqual(\n+\t\t\tview.lines(\"b.txt\").map((line) => line.text),\n+\t\t\t[\"one\"],\n+\t\t);\n+\t\tassert.ok(view.weight() > weight);\n+\t\tconst finalWeight = view.weight();\n+\t\tassert.equal(view.weight(), finalWeight);\n+\t\tassert.deepEqual(view.files(), expectedFiles);\n+\t});\n+\n+\tit(\"counts line-cache references without charging Graph-owned text per view\", () => {\n+\t\tconst short = Repo.init({ \"f.txt\": lines(\"x\") });\n+\t\tconst long = Repo.init({ \"f.txt\": lines(\"x\".repeat(10_000)) });\n+\t\tconst shortView = short.view();\n+\t\tconst longView = long.view();\n+\t\tshortView.lines(\"f.txt\");\n+\t\tlongView.lines(\"f.txt\");\n+\t\tassert.equal(shortView.weight(), longView.weight());\n+\t\tconst longWeight = longView.weight();\n+\t\tassert.equal(longView.weight(), longWeight);\n+\t});\n });"},{"path":"src/engine/view.ts","added":46,"removed":13,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -71,6 +71,16 @@\n const EMPTY_INTS = new Int32Array(0);\n const NO_FILE: FileState = { attrs: NO_ATTRS, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };\n \n+// Approximate retained-container costs for View.weight(): reference slots,\n+// object headers, and map/set entries. These are cache-budget units, not V8\n+// heap measurements. Referenced Graph strings and records are not charged.\n+const ARRAY_HEADER_BYTES = 24;\n+const OBJECT_BYTES = 32;\n+const MAP_HEADER_BYTES = 32;\n+const MAP_ENTRY_BYTES = 40;\n+const SET_ENTRY_BYTES = 32;\n+const REFERENCE_BYTES = 8;\n+\n /**\n  * The projection of a closed set. Construct once, then ask for files, lines\n  * and sequences. Throws `not-closed` if some atom's dependency is missing.\n@@ -448,21 +458,44 @@\n \t}\n \n \t/**\n-\t * About how many bytes keeping this view costs (#46): its laid-out files\n-\t * (4 bytes a line, and more for a deleted line and for positions once asked\n-\t * for), and the records it keeps for the file it was last asked about.\n+\t * About how many bytes of retained state this view costs (#46). This uses\n+\t * approximate ownership costs for containers and wrappers, not V8 heap\n+\t * measurements. Graph-owned strings and records count only as references.\n+\t * This method only inspects already-created state: it does not lay out files,\n+\t * populate lazy caches, enumerate the graph, or count temporary traversal\n+\t * containers and shared empty singleton payloads as retained view state.\n \t */\n \tweight(): number {\n-\t\tlet n = 0;\n-\t\tfor (const f of this.#files.values())\n-\t\t\tn +=\n-\t\t\t\t4 * (f.seq.length + f.vis.length) +\n-\t\t\t\t40 * f.deletedBy.size +\n-\t\t\t\t24 * ((f.seqIndex?.size ?? 0) + (f.visIndex?.size ?? 0));\n-\t\tn +=\n-\t\t\t100 * (this.#linesCache?.lines.length ?? 0) +\n-\t\t\t120 * (this.#seqCache?.seq.length ?? 0) +\n-\t\t\t2 * (this.#textCache?.text.length ?? 0);\n+\t\tlet n = this.#in.byteLength;\n+\t\tn += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length;\n+\t\tn += MAP_HEADER_BYTES; // #files exists from construction.\n+\t\tn += MAP_HEADER_BYTES; // #fileOf exists from construction.\n+\t\tif (this.#set) n += OBJECT_BYTES + SET_ENTRY_BYTES * this.#set.size;\n+\t\tif (this.#knownList) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#knownList.length;\n+\t\tif (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size;\n+\t\tif (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size;\n+\t\tn += MAP_ENTRY_BYTES * this.#fileOf.size;\n+\t\tfor (const f of this.#files.values()) {\n+\t\t\tn += MAP_ENTRY_BYTES;\n+\t\t\tif (f !== NO_FILE) {\n+\t\t\t\t// FileState and FileAttrs are per laid-out file; typed-array\n+\t\t\t\t// headers are separate from their payload byte lengths below.\n+\t\t\t\tn += OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES;\n+\t\t\t\tn += f.seq.byteLength + f.vis.byteLength;\n+\t\t\t\tn += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + ARRAY_HEADER_BYTES) * f.deletedBy.size;\n+\t\t\t\tfor (const deleters of f.deletedBy.values()) n += REFERENCE_BYTES * deleters.length;\n+\t\t\t\tif (f.seqIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size;\n+\t\t\t\tif (f.visIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size;\n+\t\t\t}\n+\t\t}\n+\t\tif (this.#linesCache)\n+\t\t\tn += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 100 * this.#linesCache.lines.length;\n+\t\tif (this.#seqCache) {\n+\t\t\tn += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 120 * this.#seqCache.seq.length;\n+\t\t\tfor (const line of this.#seqCache.seq)\n+\t\t\t\tif (line.deletedBy !== NONE) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * line.deletedBy.length;\n+\t\t}\n+\t\tif (this.#textCache) n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 2 * this.#textCache.text.length;\n \t\treturn n;\n \t}\n "}],"ci":null}