{"draft":null,"from":129,"to":318,"files":[{"path":"src/engine/test/state-source.test.ts","added":203,"removed":2,"diff":"--- a/src/engine/test/state-source.test.ts\n+++ b/src/engine/test/state-source.test.ts\n@@ -5,6 +5,49 @@\n import { lines, Repo } from \"./helpers.ts\";\n import { SqlState } from \"./storage-helper.ts\";\n \n+test(\"counter writes avoid rereads while respecting failed writes, rollback and cache bounds\", () => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst cache = new CachedStateStorage(source, 512, 2);\n+\t\tconst before = source.reads;\n+\t\tfor (let n = 0; n < 100; n++) {\n+\t\t\tcache.write(\"counter\", \"next\", n);\n+\t\t\tassert.equal(cache.read(\"counter\", \"next\"), n);\n+\t\t\tassert.ok(cache.retainedBytes <= 512);\n+\t\t\tassert.ok(cache.retainedEntries <= 2);\n+\t\t}\n+\t\tassert.equal(source.reads, before);\n+\t\tsource.failWrite = source.writes + 1;\n+\t\tassert.throws(() => cache.write(\"counter\", \"next\", 100), /injected/);\n+\t\tassert.equal(cache.read(\"counter\", \"next\"), 99);\n+\t\tsource.failWrite = Infinity;\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tcache.transaction(() => {\n+\t\t\t\t\tcache.write(\"counter\", \"next\", 101);\n+\t\t\t\t\tassert.equal(cache.read(\"counter\", \"next\"), 101);\n+\t\t\t\t\tthrow Error(\"counter rollback\");\n+\t\t\t\t}),\n+\t\t\t/counter rollback/,\n+\t\t);\n+\t\tassert.equal(cache.retainedEntries, 0);\n+\t\tassert.equal(cache.read(\"counter\", \"next\"), 99);\n+\t\tfor (let n = 0; n < 20; n++) cache.write(\"counter\", String(n), n);\n+\t\tassert.ok(cache.retainedBytes <= 512);\n+\t\tassert.ok(cache.retainedEntries <= 2);\n+\t\tassert.equal(cache.read(\"counter\", \"0\"), 0);\n+\t\tconst disabled = new CachedStateStorage(source, 0, 0);\n+\t\tdisabled.write(\"counter\", \"next\", 102);\n+\t\tassert.equal(disabled.retainedEntries, 0);\n+\t\tassert.equal(disabled.read(\"counter\", \"next\"), 102);\n+\t\tcache.write(\"counter\", \"negative-zero\", -0);\n+\t\tassert.equal(Object.is(cache.read(\"counter\", \"negative-zero\"), source.read(\"counter\", \"negative-zero\")), true);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});\n+\n+// The line-by-line layout, which a graph adopted before packed file history still uses (packFiles: false).\n test(\"current visibility stops at the first included deletion while history retains every deleter\", () => {\n \tclass ObservedStorage extends SqlState {\n \t\tpageReads = 0;\n@@ -17,7 +60,7 @@\n \ttry {\n \t\tconst oracle = Repo.init({ \"a.txt\": \"gone\\nlive\\n\" });\n \t\tconst deletion = oracle.draft({ edits: [{ file: \"a.txt\", old: \"gone\\n\", new: \"\" }] });\n-\t\tconst graph = new Graph({ storage });\n+\t\tconst graph = new Graph({ storage, packFiles: false });\n \t\tgraph.add(oracle.graph.atom(\"a0\"));\n \t\tconst ids = [\"a0\"];\n \t\tfor (let seq = 1; seq <= 514; seq++) {\n@@ -26,7 +69,7 @@\n \t\t\toracle.graph.add(atom);\n \t\t\tids.push(atom.id);\n \t\t}\n-\t\tconst reopened = new Graph({ storage });\n+\t\tconst reopened = new Graph({ storage, packFiles: false });\n \t\tconst membership = new StableOverlaySet(ids);\n \t\tconst view = new View(reopened, membership);\n \t\tstorage.pageReads = 0;\n@@ -362,3 +405,161 @@\n \t\tstorage.db.close();\n \t}\n });\n+\n+test(\"child indexes stream bounded pages before filtering historical siblings\", () => {\n+\tclass ObservedStorage extends SqlState {\n+\t\tpageReads = 0;\n+\t\tlargestPage = 0;\n+\t\toverride read(collection: string, key: string): unknown {\n+\t\t\tif (collection === \"run-starts:pages\") this.pageReads++;\n+\t\t\treturn super.read(collection, key);\n+\t\t}\n+\t\toverride write(collection: string, key: string, value: unknown): void {\n+\t\t\tif (collection === \"run-starts:pages\") {\n+\t\t\t\tassert.ok(Array.isArray(value));\n+\t\t\t\tthis.largestPage = Math.max(this.largestPage, value.length);\n+\t\t\t}\n+\t\t\tsuper.write(collection, key, value);\n+\t\t}\n+\t}\n+\tconst storage = new ObservedStorage();\n+\ttry {\n+\t\tconst oracle = Repo.init({ \"a.txt\": \"one\\ntwo\\n\" });\n+\t\tconst draft = oracle.draft({ edits: [{ file: \"a.txt\", old: \"one\", new: \"ONE\" }] });\n+\t\tconst graph = new Graph({ storage });\n+\t\tgraph.add(oracle.graph.atom(\"a0\"));\n+\t\tfor (let seq = 1; seq <= 513; seq++) {\n+\t\t\tconst atom = { ...draft, id: `a${seq}`, seq };\n+\t\t\tgraph.add(atom);\n+\t\t\toracle.graph.add(atom);\n+\t\t}\n+\t\tconst reopened = new Graph({ storage });\n+\t\tconst parent = draft.inserts[0]!.parent;\n+\t\tconst code = reopened.codeOf(parent)!;\n+\t\tstorage.pageReads = 0;\n+\t\tconst iterator = reopened.eachChildN(code);\n+\t\tconst first = iterator.next().value!;\n+\t\tassert.equal(first, reopened.lineN(oracle.graph.childrenOf(parent)[0]!));\n+\t\tassert.ok(storage.pageReads <= 1);\n+\t\tif (storage.pageReads === 0) {\n+\t\t\titerator.next();\n+\t\t\tassert.equal(storage.pageReads, 1, \"the implicit child needs no page; the next sibling needs one\");\n+\t\t}\n+\t\tassert.deepEqual(reopened.childrenOf(parent), oracle.graph.childrenOf(parent));\n+\t\tassert.deepEqual(\n+\t\t\treopened.childrenOf(\"a0.0\"),\n+\t\t\toracle.graph.childrenOf(\"a0.0\"),\n+\t\t\t\"implicit same-run child order is retained\",\n+\t\t);\n+\t\tassert.equal(storage.largestPage, 256);\n+\t\tconst membership = new StableOverlaySet([\"a0\"]);\n+\t\tassert.equal(new View(reopened, membership).text(\"a.txt\"), \"one\\ntwo\\n\");\n+\t\tassert.deepEqual(\n+\t\t\tnew View(reopened, membership).sequence(\"a.txt\"),\n+\t\t\tnew View(oracle.graph, membership).sequence(\"a.txt\"),\n+\t\t);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"persistent layout uses ordered successors without retaining deep or wide history frontiers\", () => {\n+\tfor (const shape of [\"deep\", \"wide\"] as const) {\n+\t\tconst storage = new SqlState();\n+\t\ttry {\n+\t\t\tconst oracle = Repo.init({\n+\t\t\t\t\"a.txt\": shape === \"deep\" ? Array.from({ length: 1024 }, (_, n) => `line ${n}\\n`).join(\"\") : \"base\\n\",\n+\t\t\t});\n+\t\t\tif (shape === \"wide\") {\n+\t\t\t\tconst draft = oracle.draft({ edits: [{ file: \"a.txt\", old: \"base\", new: \"branch\" }] });\n+\t\t\t\tfor (let seq = 1; seq <= 256; seq++) {\n+\t\t\t\t\tconst atom = { ...draft, id: `a${seq}`, seq };\n+\t\t\t\t\toracle.graph.add(atom);\n+\t\t\t\t\toracle.shared.add(atom.id);\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tconst before = oracle.view().text(\"a.txt\");\n+\t\t\toracle.edit({ edits: [{ file: \"a.txt\", old: before, new: \"live\\n\" }] });\n+\t\t\tconst graph = new Graph({ storage, packFiles: false });\n+\t\t\tfor (const id of oracle.shared) graph.add(oracle.graph.atom(id));\n+\t\t\tconst reopened = new Graph({ storage, packFiles: false });\n+\t\t\treopened.eachChildN = () => {\n+\t\t\t\tthrow Error(\"unordered child collection forbidden\");\n+\t\t\t};\n+\t\t\treopened.childrenN = () => {\n+\t\t\t\tthrow Error(\"materialized child collection forbidden\");\n+\t\t\t};\n+\t\t\tconst original = reopened.orderedChildrenN.bind(reopened);\n+\t\t\tlet active = 0,\n+\t\t\t\tmaximum = 0;\n+\t\t\treopened.orderedChildrenN = function* (parent: number, after?: number) {\n+\t\t\t\tactive++;\n+\t\t\t\tmaximum = Math.max(maximum, active);\n+\t\t\t\ttry {\n+\t\t\t\t\tyield* original(parent, after);\n+\t\t\t\t} finally {\n+\t\t\t\t\tactive--;\n+\t\t\t\t}\n+\t\t\t};\n+\t\t\tconst view = new View(reopened, new StableOverlaySet(oracle.shared));\n+\t\t\tassert.equal(view.text(\"a.txt\"), \"live\\n\");\n+\t\t\tassert.equal(active, 0);\n+\t\t\tassert.equal(maximum, 1, \"no ordered iterator retained per historical ancestor\");\n+\t\t\tassert.deepEqual(view.sequence(\"a.txt\"), oracle.view().sequence(\"a.txt\"));\n+\t\t} finally {\n+\t\t\tstorage.db.close();\n+\t\t}\n+\t}\n+});\n+\n+test(\"implicit ordered successors avoid priority reads but retain index validation\", () => {\n+\tclass ObservedStorage extends SqlState {\n+\t\tpriorityReads = 0;\n+\t\toverride read(collection: string, key: string): unknown {\n+\t\t\tif (collection === \"lamport\" || collection.startsWith(\"replacing:\")) this.priorityReads++;\n+\t\t\treturn super.read(collection, key);\n+\t\t}\n+\t}\n+\tconst storage = new ObservedStorage();\n+\ttry {\n+\t\tconst oracle = Repo.init({ \"a.txt\": \"one\\ntwo\\nthree\\n\" });\n+\t\tconst graph = new Graph({ storage });\n+\t\tgraph.add(oracle.graph.atom(\"a0\"));\n+\t\tstorage.priorityReads = 0;\n+\t\tassert.deepEqual([...graph.orderedChildrenN(0)], [1]);\n+\t\tassert.deepEqual([...graph.orderedChildrenN(0, 1)], []);\n+\t\tassert.deepEqual([...graph.orderedChildrenN(2)], []);\n+\t\tassert.equal(storage.priorityReads, 0, \"single-child traversal does not decode sort priority\");\n+\t\tstorage.remove(\"ordered-children\", \"format\");\n+\t\tassert.throws(() => [...graph.orderedChildrenN(0)], /fresh adoption/);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"ordered layout refuses missing format or child metadata instead of dropping history\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst oracle = Repo.init({ \"a.txt\": \"one\\n\" });\n+\t\tconst graph = new Graph({ storage, packFiles: false });\n+\t\tgraph.add(oracle.graph.atom(\"a0\"));\n+\t\tstorage.remove(\"ordered-children\", \"format\");\n+\t\tassert.throws(\n+\t\t\t() => new View(new Graph({ storage, packFiles: false }), new StableOverlaySet([\"a0\"])).text(\"a.txt\"),\n+\t\t\t/fresh adoption/,\n+\t\t);\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tnew Graph({ storage, packFiles: false }).add(\n+\t\t\t\t\toracle.draft({ edits: [{ file: \"a.txt\", old: \"one\", new: \"two\" }] }),\n+\t\t\t\t),\n+\t\t\t/fresh adoption/,\n+\t\t);\n+\t\tstorage.write(\"ordered-children\", \"format\", 2);\n+\t\tconst parent = graph.startCodeOf(\"a.txt\")!;\n+\t\tstorage.remove(\"ordered-children\", JSON.stringify([String(parent), \"meta\"]));\n+\t\tassert.throws(() => [...graph.orderedChildrenN(parent)], /missing ordered index metadata/);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});"}],"unmatched":[]}