{"version":119,"agent":"codex-capacity-chair","message":"Page per-file atom indexes and stream current-view attribute scans","time":1791832938431,"timeIso":"2026-10-12T19:22:18.431Z","changes":4,"change":1284,"files":["src/engine/graph.ts","src/engine/state-source.ts","src/engine/test/state-source.test.ts","src/engine/view.ts"],"draft":null,"diff":[{"path":"src/engine/graph.ts","added":23,"removed":4,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -15,8 +15,15 @@\n \tseqOf,\n \tstartNode,\n } from \"./keys.ts\";\n-import { type GraphStateStorage, type StateMap, storedArray, storedMap, storedNumbers } from \"./state-source.ts\";\n import {\n+\ttype GraphStateStorage,\n+\ttype StateMap,\n+\tstoredArray,\n+\tstoredLists,\n+\tstoredMap,\n+\tstoredNumbers,\n+} from \"./state-source.ts\";\n+import {\n \tATTR_NAMES,\n \ttype Atom,\n \ttype AtomId,\n@@ -314,7 +321,7 @@\n \t\t\tthis.#dependents = storedMap(storage, \"dependents\");\n \t\t\tthis.#resolvers = storedMap(storage, \"resolvers\");\n \t\t\tthis.#together = storedMap(storage, \"together\");\n-\t\t\tthis.#fileAtoms = storedMap(storage, \"file-atoms\");\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@@ -420,6 +427,7 @@\n \t#resolvers: StateMap<AtomId, AtomId | AtomId[]> = new Map();\n \t#together: StateMap<AtomId, TogetherLink[]> = new Map();\n \t#fileAtoms: StateMap<string, AtomId[]> = new Map();\n+\t#fileAtomPages: ReturnType<typeof storedLists<AtomId>> | undefined;\n \t/** First lines of runs (their n), by the node they were written after: a line's n, or -1 - a path's index. */\n \t#runStarts: StateMap<number, number | number[]> = new Map();\n \t/** 1 for the first line of a run whose atom deleted the line it hangs off: the run replaces it. */\n@@ -702,12 +710,20 @@\n \n \t/** Every atom that inserts or deletes a line of the file, in seq order. */\n \tatomsIn(file: string): readonly AtomId[] {\n+\t\tif (this.#fileAtomPages) return [...this.#fileAtomPages.values(file)];\n \t\treturn this.#fileAtoms.get(file) ?? EMPTY;\n \t}\n+\t*eachAtomIn(file: string): IterableIterator<AtomId> {\n+\t\tif (this.#fileAtomPages) yield* this.#fileAtomPages.values(file);\n+\t\telse yield* this.#fileAtoms.get(file) ?? EMPTY;\n+\t}\n+\thasAtomsIn(file: string): boolean {\n+\t\treturn this.#fileAtomPages ? this.#fileAtomPages.length(file) > 0 : this.#fileAtoms.has(file);\n+\t}\n \n \t/** Unique historical file paths, streamed without collecting their names. */\n \t*knownFiles(): IterableIterator<string> {\n-\t\tyield* this.#fileAtoms.keys();\n+\t\tyield* this.#fileAtomPages ? this.#fileAtomPages.keys() : this.#fileAtoms.keys();\n \t}\n \n \t/**\n@@ -981,7 +997,10 @@\n \t\t\tfiles.add(isStartNode(m.parent) ? m.parent.slice(1) : this.lineFile(m.parent)!);\n \t\t}\n \t\tthis.#atomFiles.push(Object.freeze([...files].sort()));\n-\t\tfor (const file of files) pushIndex(this.#fileAtoms, file, frozen.id);\n+\t\tfor (const file of files) {\n+\t\t\tif (this.#fileAtomPages) this.#fileAtomPages.append(file, frozen.id);\n+\t\t\telse pushIndex(this.#fileAtoms, file, frozen.id);\n+\t\t}\n \t\tfor (const r of frozen.reasons) {\n \t\t\tif (r.kind !== \"together\" || r.to === frozen.id) continue;\n \t\t\tpushIndex(this.#together, frozen.id, { other: r.to, text: r.text, from: frozen.id });"},{"path":"src/engine/state-source.ts","added":50,"removed":0,"diff":"--- a/src/engine/state-source.ts\n+++ b/src/engine/state-source.ts\n@@ -259,6 +259,56 @@\n \tvalues(): IterableIterator<V>;\n }\n \n+/** Append-only per-key lists, retaining at most one fixed-size page per iterator. */\n+export function storedLists<V>(storage: GraphStateStorage, name: string) {\n+\tconst pageSize = 256;\n+\t// The previous experimental layout stored one whole historical list per key.\n+\tconst length = (key: string) => {\n+\t\tconst stored = storage.read(`${name}:lengths`, key) as number | undefined;\n+\t\tif (stored === undefined && storage.read(name, JSON.stringify([\"string\", key])) !== undefined)\n+\t\t\tthrow new Error(`writer list index ${name} requires canonical-log adoption`);\n+\t\tconst count = stored ?? 0;\n+\t\tif (!Number.isSafeInteger(count) || count < 0) throw new Error(`invalid writer list count in ${name}`);\n+\t\treturn count;\n+\t};\n+\tconst pageKey = (key: string, page: number) => JSON.stringify([key, page]);\n+\tconst page = (key: string, index: number): V[] => {\n+\t\tconst values = storage.read(`${name}:pages`, pageKey(key, index)) as V[] | undefined;\n+\t\tif (!Array.isArray(values) || values.length < 1 || values.length > pageSize)\n+\t\t\tthrow new Error(`missing or invalid writer list page in ${name}`);\n+\t\treturn values;\n+\t};\n+\treturn {\n+\t\tlength,\n+\t\t*keys(): IterableIterator<string> {\n+\t\t\tfor (const entry of storage.entries(name)) {\n+\t\t\t\tvoid entry;\n+\t\t\t\tthrow new Error(`writer list index ${name} requires canonical-log adoption`);\n+\t\t\t}\n+\t\t\tfor (const [key] of storage.entries(`${name}:lengths`)) yield key;\n+\t\t},\n+\t\t*values(key: string): IterableIterator<V> {\n+\t\t\tconst count = length(key);\n+\t\t\tfor (let start = 0; start < count; start += pageSize) {\n+\t\t\t\tconst values = page(key, start / pageSize);\n+\t\t\t\tif (values.length !== Math.min(pageSize, count - start))\n+\t\t\t\t\tthrow new Error(`incomplete writer list page in ${name}`);\n+\t\t\t\tyield* values;\n+\t\t\t}\n+\t\t},\n+\t\tappend(key: string, value: V): void {\n+\t\t\tconst count = length(key);\n+\t\t\tconst offset = count % pageSize;\n+\t\t\tconst index = Math.floor(count / pageSize);\n+\t\t\tconst values = offset === 0 ? [] : page(key, index);\n+\t\t\tif (values.length !== offset) throw new Error(`invalid writer list length in ${name}`);\n+\t\t\tvalues.push(value);\n+\t\t\tstorage.write(`${name}:pages`, pageKey(key, index), values);\n+\t\t\tstorage.write(`${name}:lengths`, key, count + 1);\n+\t\t},\n+\t};\n+}\n+\n /** Keys are tagged so numeric graph codes cannot collide with string keys. */\n export function storedMap<K extends string | number, V>(storage: GraphStateStorage, name: string): StateMap<K, V> {\n \tconst keyOf = (key: K) => JSON.stringify([typeof key, key]);"},{"path":"src/engine/test/state-source.test.ts","added":34,"removed":1,"diff":"--- a/src/engine/test/state-source.test.ts\n+++ b/src/engine/test/state-source.test.ts\n@@ -1,10 +1,43 @@\n import assert from \"node:assert/strict\";\n import { test } from \"node:test\";\n import { checkDraft, Graph, View } from \"../index.ts\";\n-import { CachedStateStorage, storedNumbers } from \"../state-source.ts\";\n+import { CachedStateStorage, storedLists, storedNumbers } from \"../state-source.ts\";\n import { lines, Repo } from \"./helpers.ts\";\n import { SqlState } from \"./storage-helper.ts\";\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);"},{"path":"src/engine/view.ts","added":2,"removed":2,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -330,7 +330,7 @@\n \t\tconst graph = this.graph;\n \t\t// For each attribute, the highest-priority setter in the set wins.\n \t\tconst setters = new Map<string, { id: AtomId; value: string | null }>();\n-\t\tfor (const id of graph.atomsIn(path)) {\n+\t\tfor (const id of graph.eachAtomIn(path)) {\n \t\t\tif (!this.#has(graph.atom(id).seq)) continue;\n \t\t\tfor (const a of graph.atom(id).attrs ?? []) {\n \t\t\t\tif (a.file !== path) continue;\n@@ -553,7 +553,7 @@\n \t#file(path: string, complete = false): FileState {\n \t\tlet state = this.#files.get(path);\n \t\tif (!state || (complete && !state.complete)) {\n-\t\t\tstate = this.graph.atomsIn(path).length === 0 ? NO_FILE : this.#layout(path, complete);\n+\t\t\tstate = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE;\n \t\t\tthis.#files.set(path, state);\n \t\t}\n \t\treturn state;"}],"ci":null}