{"draft":null,"from":59,"to":318,"files":[{"path":"src/engine/test/state-source.test.ts","added":565,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/state-source.test.ts\n@@ -0,0 +1,565 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { checkDraft, Graph, View } from \"../index.ts\";\n+import { CachedStateStorage, StableOverlaySet, storedLists, storedNumbers } from \"../state-source.ts\";\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+\t\toverride read(collection: string, key: string): unknown {\n+\t\t\tif (collection === \"more-deleters:pages\") this.pageReads++;\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\": \"gone\\nlive\\n\" });\n+\t\tconst deletion = oracle.draft({ edits: [{ file: \"a.txt\", old: \"gone\\n\", new: \"\" }] });\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+\t\t\tconst atom = { ...deletion, id: `a${seq}`, seq };\n+\t\t\tgraph.add(atom);\n+\t\t\toracle.graph.add(atom);\n+\t\t\tids.push(atom.id);\n+\t\t}\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+\t\tassert.equal(view.text(\"a.txt\"), \"live\\n\");\n+\t\tassert.equal(storage.pageReads, 0, \"current visibility needs no later deleters after the first match\");\n+\t\tassert.deepEqual(view.sequence(\"a.txt\"), new View(oracle.graph, membership).sequence(\"a.txt\"));\n+\t\tassert.equal(view.sequence(\"a.txt\")[0]!.deletedBy.length, 514);\n+\t\tassert.equal(storage.pageReads, 3);\n+\t\tassert.deepEqual(reopened.deletersOf(\"a0.0\"), oracle.graph.deletersOf(\"a0.0\"));\n+\t\tconst lastOnly = new View(reopened, new StableOverlaySet([\"a0\", \"a514\"]));\n+\t\tassert.equal(lastOnly.text(\"a.txt\"), \"live\\n\", \"excluded early deletions must not hide a later included one\");\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"relocation indexes stream one fixed page before yielding and preserve order\", () => {\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.startsWith(\"relocat\") && collection.endsWith(\":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.startsWith(\"relocat\") && collection.endsWith(\":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\\n\", \"b.txt\": \"\" });\n+\t\tconst move = oracle.draft({ move: { file: \"a.txt\", start: 0, end: 1, to: \"b.txt\", at: 0 } });\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 = { ...move, 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\tstorage.pageReads = 0;\n+\t\tconst iterator = reopened.eachRelocationOf(\"a0.0\");\n+\t\tassert.deepEqual(iterator.next().value, oracle.graph.relocationsOf(\"a0.0\")[0]);\n+\t\tassert.equal(storage.pageReads, 1, \"first result must decode only one page\");\n+\t\tassert.deepEqual(reopened.relocationsOf(\"a0.0\"), oracle.graph.relocationsOf(\"a0.0\"));\n+\t\tconst parent = move.moves![0]!.parent;\n+\t\tconst code = reopened.codeOf(parent)!;\n+\t\tassert.deepEqual(reopened.relocatedTo(parent), oracle.graph.relocatedTo(parent));\n+\t\tassert.deepEqual([...reopened.eachRelocatedToCode(code)], oracle.graph.relocatedToCode(code));\n+\t\tassert.equal(storage.largestPage, 256);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"hot claim indexes stream bounded pages after reopening\", (t) => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\", \"two\") });\n+\tconst draft = oracle.draft({ edits: [{ file: \"a.txt\", old: \"one\", new: \"ONE\" }] });\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst graph = new Graph({ storage: source });\n+\t\tgraph.add(oracle.graph.atom(\"a0\"));\n+\t\tfor (let i = 0; i < 513; i++) {\n+\t\t\tconst atom = { ...draft, id: `a${i + 1}`, seq: i + 1 };\n+\t\t\tgraph.add(atom);\n+\t\t\toracle.graph.add(atom);\n+\t\t}\n+\t\tconst reopened = new Graph({ storage: source });\n+\t\tconst read = source.read.bind(source);\n+\t\tlet pagesRead = 0;\n+\t\tt.mock.method(source, \"read\", (collection: string, key: string) => {\n+\t\t\tif (collection === \"writers:pages\") pagesRead++;\n+\t\t\treturn read(collection, key);\n+\t\t});\n+\t\tconst iterator = reopened.eachWriterOf(\"L:a0.0\");\n+\t\tassert.equal(iterator.next().value, \"a1\");\n+\t\tassert.equal(pagesRead, 1, \"first claimant reads one claim page, not the entire hot-key history\");\n+\t\tassert.deepEqual(reopened.writersOf(\"L:a0.0\"), oracle.graph.writersOf(\"L:a0.0\"));\n+\t\tassert.deepEqual([...reopened.eachDependentOf(\"a0\")], oracle.graph.dependentsOf(\"a0\"));\n+\t\tfor (const name of [\"writers\", \"readers\", \"holders\", \"dependents\", \"resolvers\"]) {\n+\t\t\tassert.equal([...source.entries(name)].length, 0, \"no whole-list claim rows remain\");\n+\t\t\tfor (const [, page] of source.entries(`${name}:pages`)) assert.ok((page as string[]).length <= 256);\n+\t\t}\n+\t\tconst key = JSON.stringify([\"number\", 0]);\n+\t\tsource.write(\"legacy-claims\", key, [\"old\"]);\n+\t\tassert.throws(\n+\t\t\t() => [...storedLists(source, \"legacy-claims\", (value) => value).values(key)],\n+\t\t\t/canonical-log adoption/,\n+\t\t);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});\n+\n+test(\"stable views stream movers instead of allocating a filtered history array\", (t) => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\", \"two\"), \"b.txt\": \"\" });\n+\toracle.edit({ move: { file: \"a.txt\", start: 0, end: 1, to: \"b.txt\", at: 0 } });\n+\tconst expected = oracle.files();\n+\tconst movers = oracle.graph.movers();\n+\tt.mock.method(\n+\t\toracle.graph,\n+\t\t\"movers\",\n+\t\t() =>\n+\t\t\tnew Proxy(movers, {\n+\t\t\t\tget(target, key, receiver) {\n+\t\t\t\t\tif (key === \"filter\") throw new Error(\"whole mover history copied\");\n+\t\t\t\t\treturn Reflect.get(target, key, receiver);\n+\t\t\t\t},\n+\t\t\t}),\n+\t);\n+\tconst view = new View(oracle.graph, new StableOverlaySet(oracle.shared));\n+\tassert.equal(view.hasMoves(), true);\n+\tassert.deepEqual(view.files(), expected);\n+});\n+\n+test(\"per-file list pages append and reopen without reading the full historical list\", () => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst storage = new CachedStateStorage(source);\n+\t\tconst lists = storedLists<string>(storage, \"files\");\n+\t\tfor (let i = 0; i < 1025; i++) storage.transaction(() => lists.append(\"a.txt\", `a${i}`));\n+\t\tassert.equal(lists.length(\"a.txt\"), 1025);\n+\t\tassert.deepEqual([...lists.keys()], [\"a.txt\"]);\n+\t\tstorage.clear();\n+\t\tconst before = source.reads;\n+\t\tstorage.transaction(() => lists.append(\"a.txt\", \"a1025\"));\n+\t\tassert.ok(source.reads - before <= 2, \"append reads only one page and its length\");\n+\t\tassert.throws(\n+\t\t\t() =>\n+\t\t\t\tstorage.transaction(() => {\n+\t\t\t\t\tlists.append(\"a.txt\", \"rolled-back\");\n+\t\t\t\t\tthrow new Error(\"rollback\");\n+\t\t\t\t}),\n+\t\t\t/rollback/,\n+\t\t);\n+\t\tconst reopened = storedLists<string>(new CachedStateStorage(source), \"files\");\n+\t\tassert.deepEqual(\n+\t\t\t[...reopened.values(\"a.txt\")],\n+\t\t\tArray.from({ length: 1026 }, (_, i) => `a${i}`),\n+\t\t);\n+\t\tfor (const [, page] of source.entries(\"files:pages\")) assert.ok((page as string[]).length <= 256);\n+\t\tsource.write(\"legacy\", \"a.txt\", [\"a0\"]);\n+\t\tassert.throws(() => [...storedLists(source, \"legacy\").keys()], /canonical-log adoption/);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});\n+\n+test(\"numeric pages cross boundaries, reopen, mutate and roll back without retaining history\", () => {\n+\tconst source = new SqlState();\n+\tconst storage = new CachedStateStorage(source);\n+\tconst numbers = storedNumbers(storage, \"numbers\");\n+\tstorage.transaction(() => {\n+\t\tfor (let i = 0; i < 1025; i++) numbers.push(i - 1);\n+\t});\n+\tstorage.clear();\n+\tconst reads = source.reads;\n+\tfor (let i = 0; i < 1025; i++) assert.equal(numbers.at(i), i - 1);\n+\tassert.ok(source.reads - reads <= 7, \"one backing read per page plus metadata\");\n+\tassert.equal([...source.entries(\"numbers:pages\")].length, 5);\n+\tassert.throws(\n+\t\t() =>\n+\t\t\tstorage.transaction(() => {\n+\t\t\t\tnumbers.set(255, -99);\n+\t\t\t\tnumbers.set(256, -98);\n+\t\t\t\tnumbers.push(1025);\n+\t\t\t\tthrow new Error(\"rollback\");\n+\t\t\t}),\n+\t\t/rollback/,\n+\t);\n+\tconst reopened = storedNumbers(new CachedStateStorage(source), \"numbers\");\n+\tassert.equal(reopened.length, 1025);\n+\tassert.equal(reopened.at(255), 254);\n+\tassert.equal(reopened.at(256), 255);\n+\tassert.equal(numbers.at(255), 254);\n+\treopened.set(256, -42);\n+\tassert.equal(reopened.at(256), -42);\n+\tassert.throws(() => reopened.at(1025), /out of bounds/);\n+\tassert.throws(() => reopened.set(-1, 2), /out of bounds/);\n+\tassert.throws(() => reopened.push(NaN), /safe integers/);\n+});\n+\n+test(\"numeric pages reject legacy or missing pages instead of fabricating values\", () => {\n+\tconst storage = new SqlState();\n+\tstorage.write(\"legacy:meta\", \"length\", 256);\n+\tconst legacy = storedNumbers(storage, \"legacy\");\n+\tassert.throws(() => legacy.at(0), /canonical-log adoption/);\n+\tassert.throws(() => legacy.push(1), /canonical-log adoption/);\n+\tconst numbers = storedNumbers(storage, \"new\");\n+\tnumbers.push(5);\n+\tstorage.remove(\"new:pages\", \"0\");\n+\tassert.throws(() => numbers.at(0), /missing writer numeric page/);\n+});\n+\n+test(\"stored graph reopens without replay and preserves moves, conflicts and new edits\", () => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\", \"two\", \"three\"), \"b.txt\": \"\" });\n+\toracle.edit({ edits: [{ file: \"a.txt\", old: \"two\", new: \"TWO\" }] });\n+\toracle.edit({ move: { file: \"a.txt\", start: 1, end: 2, to: \"b.txt\", at: 0 } });\n+\tconst storage = new SqlState();\n+\tconst original = new Graph({ storage });\n+\tfor (const atom of oracle.graph.atoms()) original.add(atom);\n+\toriginal.setLabel({ name: \"saved\", head: \"a2\", title: \"Saved\", message: \"\" });\n+\tconst reads = storage.reads;\n+\tconst reopened = new Graph({ storage });\n+\tassert.equal(storage.reads, reads, \"opening does not hydrate historical collections\");\n+\tassert.equal(storage.scans, 0);\n+\tassert.equal(reopened.size, oracle.graph.size);\n+\tassert.equal(reopened.lineCount, oracle.graph.lineCount);\n+\tassert.deepEqual([...reopened.movers()], [...oracle.graph.movers()]);\n+\tfor (const atom of oracle.graph.atoms()) {\n+\t\tassert.deepEqual(reopened.filesOf(atom.id), oracle.graph.filesOf(atom.id));\n+\t\tfor (const move of atom.moves ?? []) {\n+\t\t\tassert.deepEqual(reopened.relocationsOf(move.line), oracle.graph.relocationsOf(move.line));\n+\t\t\tassert.deepEqual(reopened.relocatedTo(move.parent), oracle.graph.relocatedTo(move.parent));\n+\t\t\tassert.equal(reopened.codeOf(move.parent), oracle.graph.codeOf(move.parent));\n+\t\t}\n+\t}\n+\tassert.deepEqual(new View(reopened, oracle.shared).files(), oracle.files());\n+\tassert.equal(reopened.label(\"saved\")?.head, \"a2\");\n+\tconst draft = oracle.draft({ edits: [{ file: \"a.txt\", old: \"one\", new: \"ONE\" }] });\n+\tassert.deepEqual(checkDraft(reopened, oracle.shared, draft), oracle.check(draft));\n+\toracle.graph.add(draft);\n+\treopened.add(draft);\n+\toracle.shared.add(draft.id);\n+\tassert.deepEqual(new View(new Graph({ storage }), oracle.shared).files(), oracle.files());\n+\tfor (let n = 0; n < oracle.graph.lineCount; n++) {\n+\t\tconst id = oracle.graph.lineIdOf(n);\n+\t\tassert.deepEqual(reopened.line(id), oracle.graph.line(id));\n+\t\tassert.deepEqual(reopened.deletersOf(id), oracle.graph.deletersOf(id));\n+\t}\n+\tassert.ok(Object.isFrozen(reopened.atom(\"a0\")));\n+\tstorage.db.close();\n+});\n+\n+test(\"stored indexes retain multiple claimants and preserve a real divergent conflict\", () => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\", \"two\", \"three\") });\n+\toracle.edit({ edits: [{ file: \"a.txt\", old: \"one\", new: \"ONE\" }], author: \"left\" });\n+\tconst right = oracle.draft({ edits: [{ file: \"a.txt\", old: \"one\", new: \"uno\" }], author: \"right\" }, [\"a0\"]);\n+\tconst storage = new SqlState();\n+\tconst graph = new Graph({ storage });\n+\tfor (const atom of oracle.graph.atoms()) graph.add(atom);\n+\tassert.equal(oracle.check(right).conflicts.length, 1);\n+\tassert.deepEqual(checkDraft(new Graph({ storage }), oracle.shared, right), oracle.check(right));\n+\tgraph.add(right);\n+\toracle.graph.add(right);\n+\tassert.deepEqual(new Graph({ storage }).writersOf(\"L:a0.0\"), oracle.graph.writersOf(\"L:a0.0\"));\n+\tassert.deepEqual(new Graph({ storage }).deletedLines(right.id), oracle.graph.deletedLines(right.id));\n+\tstorage.db.close();\n+});\n+\n+test(\"failed stored append rolls back all graph indexes and can be retried\", () => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\", \"two\") });\n+\tconst storage = new SqlState();\n+\tconst graph = new Graph({ storage });\n+\tgraph.add(oracle.graph.atom(\"a0\"));\n+\tconst before = JSON.stringify(graph.toJSON());\n+\tconst draft = oracle.draft({ edits: [{ file: \"a.txt\", old: \"one\", new: \"ONE\" }] });\n+\tstorage.failWrite = storage.writes + 10;\n+\tassert.throws(() => graph.add(draft), /injected storage failure/);\n+\tassert.equal(JSON.stringify(graph.toJSON()), before);\n+\tassert.equal(JSON.stringify(new Graph({ storage }).toJSON()), before);\n+\tstorage.failWrite = Infinity;\n+\tgraph.add(draft);\n+\tassert.equal(new Graph({ storage }).size, 2);\n+\tstorage.db.close();\n+});\n+\n+test(\"removing a stored label rolls back dependent rewrites on failure\", () => {\n+\tconst oracle = Repo.init({ \"a.txt\": lines(\"one\") });\n+\tconst storage = new SqlState();\n+\tconst graph = new Graph({ storage });\n+\tgraph.add(oracle.graph.atom(\"a0\"));\n+\tgraph.setLabel({ name: \"base\", head: \"a0\", title: \"Base\", message: \"\" });\n+\tgraph.setLabel({ name: \"child\", head: \"a0\", base: \"base\", title: \"Child\", message: \"\" });\n+\tconst before = graph.labels();\n+\tstorage.failWrite = storage.writes + 1;\n+\tassert.throws(() => graph.removeLabel(\"base\"), /injected storage failure/);\n+\tassert.deepEqual(new Graph({ storage }).labels(), before);\n+\tstorage.failWrite = Infinity;\n+\tassert.equal(graph.removeLabel(\"base\"), true);\n+\tassert.equal(new Graph({ storage }).label(\"base\"), undefined);\n+\tassert.equal(new Graph({ storage }).label(\"child\")?.base, \"a0\");\n+\tstorage.db.close();\n+});\n+\n+test(\"together links stream bounded pages in both directions after storage reopen\", () => {\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 === \"together: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 === \"together: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\\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 = {\n+\t\t\t\t...draft,\n+\t\t\t\tid: `a${seq}`,\n+\t\t\t\tseq,\n+\t\t\t\treasons: [{ to: \"a0\", kind: \"together\" as const, text: `reason ${seq}` }],\n+\t\t\t};\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\tstorage.pageReads = 0;\n+\t\tconst iterator = reopened.eachTogetherLink(\"a0\");\n+\t\tassert.deepEqual(iterator.next().value, oracle.graph.togetherLinks(\"a0\")[0]);\n+\t\tassert.equal(storage.pageReads, 1);\n+\t\tstorage.pageReads = 0;\n+\t\tassert.deepEqual(reopened.togetherLinks(\"a0\"), oracle.graph.togetherLinks(\"a0\"));\n+\t\tassert.equal(storage.pageReads, 3);\n+\t\tassert.deepEqual(reopened.togetherLinks(\"a513\"), oracle.graph.togetherLinks(\"a513\"));\n+\t\tassert.equal(storage.largestPage, 256);\n+\t} finally {\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":[]}