{"agent":"codex-capacity-policy","draft":null,"base":106,"files":[{"path":"src/engine/state-source.ts","added":25,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/state-source.ts\n@@ -0,0 +1,25 @@\n+import type { Atom, AtomId, Key, LineId } from \"./types.ts\";\n+import type { LineInfo, RelocationInfo } from \"./graph.ts\";\n+\n+/**\n+ * Read-only historical metadata needed by a Graph that does not retain every\n+ * accepted atom and index in its working set. This is a storage-neutral port:\n+ * it does not prescribe SQLite, D1, R2, a schema, or a persistence lifecycle.\n+ *\n+ * Reads are synchronous because today's Graph queries are synchronous. An\n+ * asynchronous D1/R2 implementation cannot be wired directly into this port;\n+ * it needs an explicitly reviewed bounded prefetch or async Graph boundary.\n+ */\n+export interface GraphStateSource {\n+\treadonly atomCount: number;\n+\tatomAt(seq: number): Atom | undefined;\n+\tlineAt(number: number): LineInfo | undefined;\n+\tlineNumber(id: LineId): number | undefined;\n+\tclaims(key: Key): {\n+\t\treadonly writers: readonly AtomId[];\n+\t\treadonly readers: readonly AtomId[];\n+\t\treadonly holders: readonly AtomId[];\n+\t};\n+\trelocations(line: LineId): readonly RelocationInfo[];\n+\tdependencies(id: AtomId): readonly AtomId[] | undefined;\n+}"},{"path":"src/engine/test/state-source.test.ts","added":89,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/state-source.test.ts\n@@ -0,0 +1,89 @@\n+import assert from \"node:assert/strict\";\n+import { describe, it } from \"node:test\";\n+import { type AtomId, Graph, type Key, type LineId, type LineInfo, type RelocationInfo, View } from \"../index.ts\";\n+import type { GraphStateSource } from \"../state-source.ts\";\n+import type { Atom } from \"../types.ts\";\n+import { Repo, lines } from \"./helpers.ts\";\n+\n+/** A small independent read adapter over the accepted Graph oracle. */\n+function oracleSource(graph: Graph): GraphStateSource {\n+\tconst atoms = graph.atoms();\n+\tconst lineCount = atoms.reduce((n, atom) => n + atom.inserts.reduce((m, run) => m + run.lines.length, 0), 0);\n+\treturn {\n+\t\tatomCount: graph.size,\n+\t\tatomAt(seq: number): Atom | undefined {\n+\t\t\treturn atoms[seq];\n+\t\t},\n+\t\tlineAt(number: number): LineInfo | undefined {\n+\t\t\tif (!Number.isSafeInteger(number) || number < 0 || number >= lineCount) return undefined;\n+\t\t\treturn graph.line(graph.lineIdOf(number));\n+\t\t},\n+\t\tlineNumber(id: LineId): number | undefined {\n+\t\t\tconst n = graph.lineN(id);\n+\t\t\treturn n < 0 ? undefined : n;\n+\t\t},\n+\t\tclaims(key: Key) {\n+\t\t\treturn { writers: graph.writersOf(key), readers: graph.readersOf(key), holders: graph.holdersOf(key) };\n+\t\t},\n+\t\trelocations(line: LineId): readonly RelocationInfo[] {\n+\t\t\treturn graph.relocationsOf(line);\n+\t\t},\n+\t\tdependencies(id: AtomId): readonly AtomId[] | undefined {\n+\t\t\ttry {\n+\t\t\t\treturn graph.atom(id).deps;\n+\t\t\t} catch {\n+\t\t\t\treturn undefined;\n+\t\t\t}\n+\t\t},\n+\t};\n+}\n+\n+describe(\"GraphStateSource oracle contract\", () => {\n+\tit(\"exposes historical atom, line, claim, relocation, and dependency facts from Graph\", () => {\n+\t\tconst repo = Repo.init({ \"a.txt\": lines(\"one\", \"two\", \"three\"), \"b.txt\": \"\" });\n+\t\tconst first = repo.edit({ edits: [{ file: \"a.txt\", old: \"two\", new: \"TWO\" }] });\n+\t\tconst moved = repo.edit({ move: { file: \"a.txt\", start: 1, end: 2, to: \"b.txt\", at: 0 } });\n+\t\tconst source = oracleSource(repo.graph);\n+\n+\t\tassert.equal(source.atomCount, repo.graph.size);\n+\t\tfor (let seq = 0; seq < source.atomCount; seq++) {\n+\t\t\tconst atom = source.atomAt(seq);\n+\t\t\tassert.deepEqual(atom, repo.graph.atom(`a${seq}`));\n+\t\t\tassert.deepEqual(source.dependencies(atom!.id), atom!.deps);\n+\t\t}\n+\t\tassert.equal(source.atomAt(-1), undefined);\n+\t\tassert.equal(source.atomAt(source.atomCount), undefined);\n+\n+\t\tfor (const id of [\"a0.0\", \"a0.1\", \"a0.2\", `${first.id}.0` as LineId]) {\n+\t\t\tconst number = source.lineNumber(id as LineId);\n+\t\t\tassert.notEqual(number, undefined);\n+\t\t\tassert.deepEqual(source.lineAt(number!), repo.graph.line(id as LineId));\n+\t\t}\n+\t\tassert.equal(source.lineAt(-1), undefined);\n+\t\tassert.equal(source.lineAt(Number.MAX_SAFE_INTEGER), undefined);\n+\t\tassert.equal(source.lineNumber(\"a999.0\" as LineId), undefined);\n+\n+\t\tconst key = `L:${first.id}.0` as Key;\n+\t\tassert.deepEqual(source.claims(key), {\n+\t\t\twriters: repo.graph.writersOf(key),\n+\t\t\treaders: repo.graph.readersOf(key),\n+\t\t\tholders: repo.graph.holdersOf(key),\n+\t\t});\n+\t\tassert.ok(moved.moves?.length);\n+\t\tconst movedLine = moved.moves![0]!.line;\n+\t\tassert.deepEqual(source.relocations(movedLine), repo.graph.relocationsOf(movedLine));\n+\t});\n+\n+\tit(\"keeps actual View layout and conflict decisions as the independent semantic oracle\", () => {\n+\t\tconst repo = Repo.init({ \"a.txt\": lines(\"one\", \"two\", \"three\") });\n+\t\tconst left = repo.edit({ edits: [{ file: \"a.txt\", old: \"one\", new: \"ONE\" }], author: \"left\" });\n+\t\tconst right = repo.draft({ edits: [{ file: \"a.txt\", old: \"one\", new: \"uno\" }], author: \"right\" }, [\"a0\"]);\n+\t\tconst source = oracleSource(repo.graph);\n+\n+\t\tconst view = new View(repo.graph, [\"a0\", left.id]);\n+\t\tassert.deepEqual(view.files(), { \"a.txt\": \"ONE\\ntwo\\nthree\\n\" });\n+\t\tassert.deepEqual(repo.graph.writersOf(`L:a0.0` as Key), source.claims(`L:a0.0` as Key).writers);\n+\t\tassert.equal(repo.graph.dependsOn(left.id, \"a0\"), source.dependencies(left.id)!.includes(\"a0\" as AtomId));\n+\t\tassert.equal(repo.check(right).conflicts.length, 1);\n+\t});\n+});"}]}