Bound persistent dependency reachability with rollback scratch
of shared·codex-capacity-chair··Not checked
Version 138 · Files at version 139
2 files, 105 lines added, 0 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -947,6 +947,32 @@
948948 #reaches(from: Iterable<AtomId>, target: AtomId): boolean { 949949 const floor = this.#lamport[this.#seq(target)]!; 950+ if (this.#storage) { 951+ // Roots belong to the current atom's dependency list, not its history. 952+ const roots = [...from]; 953+ if (roots.includes(target)) return true; 954+ if (roots.every((id) => this.#lamport[this.#seq(id)]! <= floor)) return false; 955+ return this.withProjectionScratch((scratch) => { 957+ const push = (id: AtomId): void => { 958+ if (scratch.read("seen", id)) return; 959+ scratch.write("seen", id, true); 960+ scratch.write("stack", String(length++), id); 962+ for (const id of roots) push(id); 963+ while (length > 0) { 964+ const key = String(--length); 965+ const current = scratch.read("stack", key) as AtomId; 966+ scratch.remove("stack", key); 967+ if (this.#lamport[this.#seq(current)]! <= floor) continue; 968+ for (const dependency of this.atom(current).deps) { 969+ if (dependency === target) return true; 950976 const seen = new Set<AtomId>(); 951977 const stack = [...from]; 952978 while (stack.length > 0) { --- /dev/null
+++ b/src/engine/test/reachability-scratch.test.ts
@@ -0,0 +1,79 @@
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { buildBase, Graph } from "../index.ts"; 4+import { CachedStateStorage } from "../state-source.ts"; 5+import type { Atom } from "../types.ts"; 6+import { SqlState } from "./storage-helper.ts"; 8+test("persistent reachability uses rollback scratch across deep shared ancestry and graph append", () => { 9+ const source = new SqlState(); 11+ const storage = new CachedStateStorage(source, 8192, 32); 12+ const graph = new Graph({ storage }); 13+ const oracle = new Graph(); 14+ const add = (atom: Atom) => { 18+ add(buildBase({ "a.txt": "base\n" }, { author: "fixture", message: "base", time: 0 })); 19+ for (let seq = 1; seq <= 513; seq++) 26+ inserts: seq === 1 ? [{ file: "b.txt", parent: "^b.txt", lines: ["other"] }] : [], 34+ deps: seq === 1 ? [] : seq === 2 ? ["a1"] : [`a${seq - 1}`, `a${seq - 2}`], 36+ const before = source.db.prepare("SELECT * FROM state ORDER BY collection, key").all(); 37+ for (const target of ["a0", "a1", "a127", "a512", "a513"]) { 38+ assert.equal(graph.dependsOn("a513", target), oracle.dependsOn("a513", target)); 39+ assert.deepEqual(source.db.prepare("SELECT * FROM state ORDER BY collection, key").all(), before); 45+ message: "append through ancestor", 47+ inserts: [{ file: "b.txt", parent: "a1.0", lines: ["next"] }], 57+ source.failWrite = source.writes + 3; 58+ assert.throws(() => graph.add(atom), /injected storage failure/); 59+ assert.deepEqual(source.db.prepare("SELECT * FROM state ORDER BY collection, key").all(), before); 60+ source.failWrite = Infinity; 61+ const reopened = new Graph({ storage: new CachedStateStorage(source) }); 63+ () => reopened.add({ ...atom, inserts: [{ file: "a.txt", parent: "a0.0", lines: ["invalid"] }] }), 64+ /not in its closure/, 66+ assert.deepEqual(source.db.prepare("SELECT * FROM state ORDER BY collection, key").all(), before); 69+ assert.equal(reopened.lineCount, oracle.lineCount); 70+ assert.equal(reopened.size, oracle.size); 71+ assert.equal(reopened.dependsOn("a514", "a1"), true); 73+ source.db.prepare("SELECT count(*) AS n FROM state WHERE collection LIKE 'projection-scratch:%'").get()!.n,