{"version":127,"agent":"codex-capacity-chair","message":"Page persistent relocation indexes and stream projection reads","time":1791838447750,"timeIso":"2026-10-12T20:54:07.750Z","changes":3,"change":1284,"files":["src/engine/graph.ts","src/engine/test/state-source.test.ts","src/engine/view.ts"],"draft":null,"diff":[{"path":"src/engine/graph.ts","added":26,"removed":10,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -323,9 +323,11 @@\n \t\t\tthis.#together = storedMap(storage, \"together\");\n \t\t\tthis.#fileAtomPages = storedLists<AtomId>(storage, \"file-atoms\");\n \t\t\tthis.#runStarts = storedMap(storage, \"run-starts\");\n-\t\t\tthis.#relocations = storedMap(storage, \"relocations\");\n-\t\t\tthis.#relocatedTo = storedMap(storage, \"relocated-to\");\n-\t\t\tthis.#relocatedToCode = storedMap(storage, \"relocated-to-code\");\n+\t\t\tthis.#relocationPages = storedLists<RelocationInfo>(storage, \"relocations\");\n+\t\t\tthis.#relocatedToPages = storedLists<RelocationInfo>(storage, \"relocated-to\");\n+\t\t\tthis.#relocatedToCodePages = storedLists<RelocationInfo>(storage, \"relocated-to-code\", (key) =>\n+\t\t\t\tJSON.stringify([\"number\", Number(key)]),\n+\t\t\t);\n \t\t\tthis.#labels = storedMap(storage, \"labels\");\n \t\t}\n \t}\n@@ -438,9 +440,12 @@\n \t/** 1 for the first line of a run whose atom deleted the line it hangs off: the run replaces it. */\n \t#replacing: NumberIndex = new Ints(true);\n \t#relocations: StateMap<LineId, RelocationInfo[]> = new Map();\n+\t#relocationPages: ReturnType<typeof storedLists<RelocationInfo>> | undefined;\n \t#relocatedTo: StateMap<NodeId, RelocationInfo[]> = new Map();\n+\t#relocatedToPages: ReturnType<typeof storedLists<RelocationInfo>> | undefined;\n \t/** The same relocations by node code (a line's n, or -1 - a path's index), for views laid out by number. */\n \t#relocatedToCode: StateMap<number, RelocationInfo[]> = new Map();\n+\t#relocatedToCodePages: ReturnType<typeof storedLists<RelocationInfo>> | undefined;\n \t#movers: AtomId[] = [];\n \t#atomFiles: (readonly string[])[] = [];\n \t/** Each atom's deleted lines by n, packed (#46), or undefined when it deletes none. */\n@@ -631,8 +636,13 @@\n \n \t/** Every relocation that puts a line right after the node with this code. */\n \trelocatedToCode(code: number): readonly RelocationInfo[] {\n+\t\tif (this.#relocatedToCodePages) return [...this.eachRelocatedToCode(code)];\n \t\treturn this.#relocatedToCode.get(code) ?? EMPTY;\n \t}\n+\t*eachRelocatedToCode(code: number): IterableIterator<RelocationInfo> {\n+\t\tif (this.#relocatedToCodePages) yield* this.#relocatedToCodePages.values(String(code));\n+\t\telse yield* this.#relocatedToCode.get(code) ?? EMPTY;\n+\t}\n \n \t/** How many lines every atom together has written. */\n \tget lineCount(): number {\n@@ -782,11 +792,17 @@\n \n \t/** Every relocation of a line, in seq order. */\n \trelocationsOf(line: LineId): readonly RelocationInfo[] {\n+\t\tif (this.#relocationPages) return [...this.eachRelocationOf(line)];\n \t\treturn this.#relocations.get(line) ?? EMPTY;\n \t}\n+\t*eachRelocationOf(line: LineId): IterableIterator<RelocationInfo> {\n+\t\tif (this.#relocationPages) yield* this.#relocationPages.values(line);\n+\t\telse yield* this.#relocations.get(line) ?? EMPTY;\n+\t}\n \n \t/** Every relocation that puts a line right after this node, in seq order. */\n \trelocatedTo(node: NodeId): readonly RelocationInfo[] {\n+\t\tif (this.#relocatedToPages) return [...this.#relocatedToPages.values(node)];\n \t\treturn this.#relocatedTo.get(node) ?? EMPTY;\n \t}\n \n@@ -998,13 +1014,13 @@\n \t\t}\n \t\t(frozen.moves ?? []).forEach((m, i) => {\n \t\t\tconst info: RelocationInfo = Object.freeze({ atom: frozen.id, line: m.line, parent: m.parent, k: i });\n-\t\t\tpushIndex(this.#relocations, m.line, info);\n-\t\t\tpushIndex(this.#relocatedTo, m.parent, info);\n-\t\t\tpushIndex(\n-\t\t\t\tthis.#relocatedToCode,\n-\t\t\t\tisStartNode(m.parent) ? -1 - this.#pathOf(m.parent.slice(1)) : this.#lineN(m.parent),\n-\t\t\t\tinfo,\n-\t\t\t);\n+\t\t\tif (this.#relocationPages) this.#relocationPages.append(m.line, info);\n+\t\t\telse pushIndex(this.#relocations, m.line, info);\n+\t\t\tif (this.#relocatedToPages) this.#relocatedToPages.append(m.parent, info);\n+\t\t\telse pushIndex(this.#relocatedTo, m.parent, info);\n+\t\t\tconst parentCode = isStartNode(m.parent) ? -1 - this.#pathOf(m.parent.slice(1)) : this.#lineN(m.parent);\n+\t\t\tif (this.#relocatedToCodePages) this.#relocatedToCodePages.append(String(parentCode), info);\n+\t\t\telse pushIndex(this.#relocatedToCode, parentCode, info);\n \t\t});\n \t\tif (frozen.moves) this.#movers.push(frozen.id);\n \t\tfor (const n of deletedN) {"},{"path":"src/engine/test/state-source.test.ts","added":43,"removed":0,"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(\"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\" }] });"},{"path":"src/engine/view.ts","added":2,"removed":2,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -204,7 +204,7 @@\n \t\tconst ignored = new Set<string>();\n \t\tconst pick = (line: LineId): RelocationInfo | undefined => {\n \t\t\tlet best: RelocationInfo | undefined;\n-\t\t\tfor (const r of graph.relocationsOf(line)) {\n+\t\t\tfor (const r of graph.eachRelocationOf(line)) {\n \t\t\t\tif (this.#has(graph.atom(r.atom).seq) && !ignored.has(identity(r)) && (!best || stronger(r, best))) best = r;\n \t\t\t}\n \t\t\treturn best;\n@@ -328,7 +328,7 @@\n \t\t\tout.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) });\n \t\t}\n \t\tif (relocs) {\n-\t\t\tfor (const r of graph.relocatedToCode(code)) {\n+\t\t\tfor (const r of graph.eachRelocatedToCode(code)) {\n \t\t\t\tconst n = graph.lineN(r.line);\n \t\t\t\tconst selected = relocs.get(n)?.r;\n \t\t\t\tif (!selected || selected.atom !== r.atom || selected.k !== r.k) continue;"}],"ci":null}