{"version":139,"agent":"codex-capacity-chair","message":"Bound persistent dependency reachability with rollback scratch","time":1791845054361,"timeIso":"2026-10-12T22:44:14.361Z","changes":2,"change":1284,"files":["src/engine/graph.ts","src/engine/test/reachability-scratch.test.ts"],"draft":null,"diff":[{"path":"src/engine/graph.ts","added":26,"removed":0,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -947,6 +947,32 @@\n \t */\n \t#reaches(from: Iterable<AtomId>, target: AtomId): boolean {\n \t\tconst floor = this.#lamport[this.#seq(target)]!;\n+\t\tif (this.#storage) {\n+\t\t\t// Roots belong to the current atom's dependency list, not its history.\n+\t\t\tconst roots = [...from];\n+\t\t\tif (roots.includes(target)) return true;\n+\t\t\tif (roots.every((id) => this.#lamport[this.#seq(id)]! <= floor)) return false;\n+\t\t\treturn this.withProjectionScratch((scratch) => {\n+\t\t\t\tlet length = 0;\n+\t\t\t\tconst push = (id: AtomId): void => {\n+\t\t\t\t\tif (scratch.read(\"seen\", id)) return;\n+\t\t\t\t\tscratch.write(\"seen\", id, true);\n+\t\t\t\t\tscratch.write(\"stack\", String(length++), id);\n+\t\t\t\t};\n+\t\t\t\tfor (const id of roots) push(id);\n+\t\t\t\twhile (length > 0) {\n+\t\t\t\t\tconst key = String(--length);\n+\t\t\t\t\tconst current = scratch.read(\"stack\", key) as AtomId;\n+\t\t\t\t\tscratch.remove(\"stack\", key);\n+\t\t\t\t\tif (this.#lamport[this.#seq(current)]! <= floor) continue;\n+\t\t\t\t\tfor (const dependency of this.atom(current).deps) {\n+\t\t\t\t\t\tif (dependency === target) return true;\n+\t\t\t\t\t\tpush(dependency);\n+\t\t\t\t\t}\n+\t\t\t\t}\n+\t\t\t\treturn false;\n+\t\t\t});\n+\t\t}\n \t\tconst seen = new Set<AtomId>();\n \t\tconst stack = [...from];\n \t\twhile (stack.length > 0) {"},{"path":"src/engine/test/reachability-scratch.test.ts","added":79,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/reachability-scratch.test.ts\n@@ -0,0 +1,79 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { buildBase, Graph } from \"../index.ts\";\n+import { CachedStateStorage } from \"../state-source.ts\";\n+import type { Atom } from \"../types.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"persistent reachability uses rollback scratch across deep shared ancestry and graph append\", () => {\n+\tconst source = new SqlState();\n+\ttry {\n+\t\tconst storage = new CachedStateStorage(source, 8192, 32);\n+\t\tconst graph = new Graph({ storage });\n+\t\tconst oracle = new Graph();\n+\t\tconst add = (atom: Atom) => {\n+\t\t\tgraph.add(atom);\n+\t\t\toracle.add(atom);\n+\t\t};\n+\t\tadd(buildBase({ \"a.txt\": \"base\\n\" }, { author: \"fixture\", message: \"base\", time: 0 }));\n+\t\tfor (let seq = 1; seq <= 513; seq++)\n+\t\t\tadd({\n+\t\t\t\tid: `a${seq}`,\n+\t\t\t\tseq,\n+\t\t\t\tauthor: \"fixture\",\n+\t\t\t\tmessage: \"branch\",\n+\t\t\t\ttime: seq,\n+\t\t\t\tinserts: seq === 1 ? [{ file: \"b.txt\", parent: \"^b.txt\", lines: [\"other\"] }] : [],\n+\t\t\t\tdeletes: [],\n+\t\t\t\twrites: [],\n+\t\t\t\treads: [],\n+\t\t\t\tholds: [],\n+\t\t\t\tneeds: [],\n+\t\t\t\tresolves: [],\n+\t\t\t\treasons: [],\n+\t\t\t\tdeps: seq === 1 ? [] : seq === 2 ? [\"a1\"] : [`a${seq - 1}`, `a${seq - 2}`],\n+\t\t\t});\n+\t\tconst before = source.db.prepare(\"SELECT * FROM state ORDER BY collection, key\").all();\n+\t\tfor (const target of [\"a0\", \"a1\", \"a127\", \"a512\", \"a513\"]) {\n+\t\t\tassert.equal(graph.dependsOn(\"a513\", target), oracle.dependsOn(\"a513\", target));\n+\t\t\tassert.deepEqual(source.db.prepare(\"SELECT * FROM state ORDER BY collection, key\").all(), before);\n+\t\t}\n+\t\tconst atom: Atom = {\n+\t\t\tid: \"a514\",\n+\t\t\tseq: 514,\n+\t\t\tauthor: \"fixture\",\n+\t\t\tmessage: \"append through ancestor\",\n+\t\t\ttime: 514,\n+\t\t\tinserts: [{ file: \"b.txt\", parent: \"a1.0\", lines: [\"next\"] }],\n+\t\t\tdeletes: [],\n+\t\t\twrites: [],\n+\t\t\treads: [],\n+\t\t\tholds: [],\n+\t\t\tneeds: [],\n+\t\t\tresolves: [],\n+\t\t\treasons: [],\n+\t\t\tdeps: [\"a513\"],\n+\t\t};\n+\t\tsource.failWrite = source.writes + 3;\n+\t\tassert.throws(() => graph.add(atom), /injected storage failure/);\n+\t\tassert.deepEqual(source.db.prepare(\"SELECT * FROM state ORDER BY collection, key\").all(), before);\n+\t\tsource.failWrite = Infinity;\n+\t\tconst reopened = new Graph({ storage: new CachedStateStorage(source) });\n+\t\tassert.throws(\n+\t\t\t() => reopened.add({ ...atom, inserts: [{ file: \"a.txt\", parent: \"a0.0\", lines: [\"invalid\"] }] }),\n+\t\t\t/not in its closure/,\n+\t\t);\n+\t\tassert.deepEqual(source.db.prepare(\"SELECT * FROM state ORDER BY collection, key\").all(), before);\n+\t\treopened.add(atom);\n+\t\toracle.add(atom);\n+\t\tassert.equal(reopened.lineCount, oracle.lineCount);\n+\t\tassert.equal(reopened.size, oracle.size);\n+\t\tassert.equal(reopened.dependsOn(\"a514\", \"a1\"), true);\n+\t\tassert.equal(\n+\t\t\tsource.db.prepare(\"SELECT count(*) AS n FROM state WHERE collection LIKE 'projection-scratch:%'\").get()!.n,\n+\t\t\t0,\n+\t\t);\n+\t} finally {\n+\t\tsource.db.close();\n+\t}\n+});"}],"ci":null}