{"version":149,"agent":"codex-capacity-chair","message":"Index atom metadata for resumable projection adoption","time":1791861423553,"timeIso":"2026-10-13T03:17:03.553Z","changes":25,"change":1284,"files":["src/engine/experimental/projection-atom-adoption.ts","src/engine/experimental/projection-atom-index.ts","src/engine/experimental/projection-marker.ts","src/engine/experimental/projection-move-adoption.ts","src/engine/experimental/projection-move-resolution.ts","src/engine/graph.ts","src/engine/test/projection-atom-index.test.ts","src/engine/test/projection-marker-cycle.test.ts","worker/project/project-types.ts","worker/project/project.ts","worker/project/test/project-writer-storage.test.ts"],"draft":null,"diff":[{"path":"src/engine/experimental/projection-atom-adoption.ts","added":76,"removed":35,"diff":"--- a/src/engine/experimental/projection-atom-adoption.ts\n+++ b/src/engine/experimental/projection-atom-adoption.ts\n@@ -1,5 +1,6 @@\n import { createHash } from \"node:crypto\";\n import type { Atom } from \"../types.ts\";\n+import { ProjectionAtomDescriptor } from \"./projection-atom-index.ts\";\n import type { ExperimentalMarkerProjection, MarkerState } from \"./projection-marker.ts\";\n import { adoptMovesStep } from \"./projection-move-adoption.ts\";\n \n@@ -81,7 +82,7 @@\n \t}\n \tpage(input: {\n \t\tprevious: MarkerState;\n-\t\tatom: Atom;\n+\t\tatom: Atom | ProjectionAtomDescriptor;\n \t\tremove: boolean;\n \t\tsource: string;\n \t\tmaxSteps: number;\n@@ -101,8 +102,36 @@\n \t\t)\n \t\t\tthrow Error(\"invalid atom adoption page bounds\");\n \t\tconst atom = input.atom;\n-\t\tconst identity = digest([input.source, input.previous, atom, input.remove]);\n-\t\tif (digest(p.graph.atom(atom.id)) !== digest(atom)) throw Error(\"canonical adoption atom mismatch\");\n+\t\tconst indexed = atom instanceof ProjectionAtomDescriptor ? atom : undefined;\n+\t\tconst full = indexed ? undefined : (atom as Atom);\n+\t\tconst identity = digest([\n+\t\t\tinput.source,\n+\t\t\tinput.previous,\n+\t\t\tindexed ? [indexed.id, indexed.seq, indexed.digest] : atom,\n+\t\t\tinput.remove,\n+\t\t]);\n+\t\tif (indexed) {\n+\t\t\tif (p.graph.idOfSeq(indexed.seq) !== indexed.id) throw Error(\"canonical adoption atom mismatch\");\n+\t\t} else if (digest(p.graph.atom(atom.id)) !== digest(full)) throw Error(\"canonical adoption atom mismatch\");\n+\t\tconst inserts = indexed?.count(\"inserts\") ?? full!.inserts.length;\n+\t\tconst run = (i: number) =>\n+\t\t\tindexed\n+\t\t\t\t? indexed.item(\"inserts\", i)\n+\t\t\t\t: { file: full!.inserts[i]!.file, parent: full!.inserts[i]!.parent, lines: full!.inserts[i]!.lines.length };\n+\t\tconst deps = indexed?.count(\"deps\") ?? full!.deps.length;\n+\t\tconst dependency = (i: number) => (indexed ? indexed.item(\"deps\", i) : full!.deps[i]!);\n+\t\tconst deletes = indexed?.count(\"deletes\") ?? full!.deletes.length;\n+\t\tconst deletion = (i: number) => (indexed ? indexed.item(\"deletes\", i) : full!.deletes[i]!);\n+\t\tconst attrs = indexed?.count(\"attrs\") ?? full!.attrs?.length ?? 0;\n+\t\tconst attribute = (i: number) => (indexed ? indexed.item(\"attrs\", i) : full!.attrs![i]!);\n+\t\tconst moves = indexed?.count(\"moves\") ?? full!.moves?.length ?? 0;\n+\t\tconst fullFiles = indexed ? undefined : p.graph.filesOf(atom.id);\n+\t\tconst files = indexed?.count(\"files\") ?? fullFiles!.length;\n+\t\tconst file = (i: number) => (indexed ? indexed.item(\"files\", i) : fullFiles![i]!);\n+\t\tconst seqOf = (id: string) => {\n+\t\t\tif (!p.graph.has(id)) throw Error(\"missing dependency\");\n+\t\t\treturn Number(id.slice(1));\n+\t\t};\n \t\tconst collection = `atom-adoption:${this.task}`;\n \t\tconst validate = () => {\n \t\t\tconst result: unknown = input.validateSource();\n@@ -121,19 +150,19 @@\n \t\t\t\t)\n \t\t\t\t\tthrow Error(\"invalid or changed atom adoption cursor\");\n \t\t\t\tp.assertContext(cursor.state);\n-\t\t\t\tif (cursor.run > atom.inserts.length || cursor.line > (atom.inserts[cursor.run]?.lines.length ?? 0))\n+\t\t\t\tif (cursor.run > inserts || cursor.line > (cursor.run < inserts ? run(cursor.run).lines : 0))\n \t\t\t\t\tthrow Error(\"invalid atom adoption line position\");\n \t\t\t\tconst limits: Record<Phase, number> = {\n-\t\t\t\t\tdependencies: atom.deps.length,\n-\t\t\t\t\tfiles: atom.inserts.length + 1,\n+\t\t\t\t\tdependencies: deps,\n+\t\t\t\t\tfiles: inserts + 1,\n \t\t\t\t\tlines: 0,\n-\t\t\t\t\tdeletes: atom.deletes.length,\n-\t\t\t\t\tcounts: atom.deps.length,\n-\t\t\t\t\tattributes: atom.attrs?.length ?? 0,\n-\t\t\t\t\tcontributions: p.graph.filesOf(atom.id).length,\n+\t\t\t\t\tdeletes: deletes,\n+\t\t\t\t\tcounts: deps,\n+\t\t\t\t\tattributes: attrs,\n+\t\t\t\t\tcontributions: files,\n \t\t\t\t\tmembership: 0,\n \t\t\t\t\tmoves: 0,\n-\t\t\t\t\tcleanup: atom.attrs?.length ?? 0,\n+\t\t\t\t\tcleanup: attrs,\n \t\t\t\t\tdone: 0,\n \t\t\t\t};\n \t\t\t\tif (cursor.index > limits[cursor.phase]) throw Error(\"invalid atom adoption phase position\");\n@@ -158,35 +187,35 @@\n \t\t\t\tcursor!.index = 0;\n \t\t\t};\n \t\t\tconst state = cursor.state;\n-\t\t\tconst contributionFiles = p.graph.filesOf(atom.id);\n+\n \t\t\twhile (cursor.phase !== \"done\" && steps < input.maxSteps) {\n \t\t\t\tsteps++;\n \t\t\t\tswitch (cursor.phase) {\n \t\t\t\t\tcase \"dependencies\": {\n-\t\t\t\t\t\tif (input.remove || cursor.index === atom.deps.length) {\n+\t\t\t\t\t\tif (input.remove || cursor.index === deps) {\n \t\t\t\t\t\t\tnext();\n \t\t\t\t\t\t\tbreak;\n \t\t\t\t\t\t}\n-\t\t\t\t\t\tconst id = atom.deps[cursor.index++]!;\n-\t\t\t\t\t\tif (!p.members.get(state.membership, p.graph.atom(id).seq)?.present) throw Error(\"missing dependency\");\n+\t\t\t\t\t\tconst id = dependency(cursor.index++);\n+\t\t\t\t\t\tif (!p.members.get(state.membership, seqOf(id))?.present) throw Error(\"missing dependency\");\n \t\t\t\t\t\tbreak;\n \t\t\t\t\t}\n \t\t\t\t\tcase \"files\": {\n-\t\t\t\t\t\tif (input.remove || cursor.index > atom.inserts.length) {\n+\t\t\t\t\t\tif (input.remove || cursor.index > inserts) {\n \t\t\t\t\t\t\tnext();\n \t\t\t\t\t\t\tbreak;\n \t\t\t\t\t\t}\n-\t\t\t\t\t\tp.ensureFile(state, cursor.index === 0 ? p.path : atom.inserts[cursor.index - 1]!.file);\n+\t\t\t\t\t\tp.ensureFile(state, cursor.index === 0 ? p.path : run(cursor.index - 1).file);\n \t\t\t\t\t\tcursor.index++;\n \t\t\t\t\t\tbreak;\n \t\t\t\t\t}\n \t\t\t\t\tcase \"lines\": {\n-\t\t\t\t\t\tif (cursor.run === atom.inserts.length) {\n+\t\t\t\t\t\tif (cursor.run === inserts) {\n \t\t\t\t\t\t\tnext();\n \t\t\t\t\t\t\tbreak;\n \t\t\t\t\t\t}\n-\t\t\t\t\t\tconst run = atom.inserts[cursor.run]!;\n-\t\t\t\t\t\tif (cursor.line === run.lines.length) {\n+\t\t\t\t\t\tconst currentRun = run(cursor.run);\n+\t\t\t\t\t\tif (cursor.line === currentRun.lines) {\n \t\t\t\t\t\t\tcursor.run++;\n \t\t\t\t\t\t\tcursor.line = 0;\n \t\t\t\t\t\t\tbreak;\n@@ -202,11 +231,11 @@\n \t\t\t\t\t\tbreak;\n \t\t\t\t\t}\n \t\t\t\t\tcase \"deletes\": {\n-\t\t\t\t\t\tif (cursor.index === atom.deletes.length) {\n+\t\t\t\t\t\tif (cursor.index === deletes) {\n \t\t\t\t\t\t\tnext();\n \t\t\t\t\t\t\tbreak;\n \t\t\t\t\t\t}\n-\t\t\t\t\t\tconst marker = p.markers.get(state.locator, p.graph.lineN(atom.deletes[cursor.index++]!) + 1);\n+\t\t\t\t\t\tconst marker = p.markers.get(state.locator, p.graph.lineN(deletion(cursor.index++)) + 1);\n \t\t\t\t\t\tif (!marker) throw Error(\"missing deleted marker\");\n \t\t\t\t\t\tstate.sequence = p.sequence.update(state.sequence, {\n \t\t\t\t\t\t\tkind: \"visibility\",\n@@ -216,11 +245,11 @@\n \t\t\t\t\t\tbreak;\n \t\t\t\t\t}\n \t\t\t\t\tcase \"counts\": {\n-\t\t\t\t\t\tif (cursor.index === atom.deps.length) {\n+\t\t\t\t\t\tif (cursor.index === deps) {\n \t\t\t\t\t\t\tnext();\n \t\t\t\t\t\t\tbreak;\n \t\t\t\t\t\t}\n-\t\t\t\t\t\tconst seq = p.graph.atom(atom.deps[cursor.index++]!).seq;\n+\t\t\t\t\t\tconst seq = seqOf(dependency(cursor.index++));\n \t\t\t\t\t\tconst member = p.members.get(state.membership, seq);\n \t\t\t\t\t\tif (!member?.present || (input.remove && member.dependents < 1)) throw Error(\"invalid dependency count\");\n \t\t\t\t\t\tstate.membership = p.members.set(state.membership, seq, {\n@@ -230,25 +259,24 @@\n \t\t\t\t\t\tbreak;\n \t\t\t\t\t}\n \t\t\t\t\tcase \"attributes\": {\n-\t\t\t\t\t\tconst attrs = atom.attrs ?? [];\n-\t\t\t\t\t\tif (cursor.index === attrs.length) {\n+\t\t\t\t\t\tif (cursor.index === attrs) {\n \t\t\t\t\t\t\tnext();\n \t\t\t\t\t\t\tbreak;\n \t\t\t\t\t\t}\n-\t\t\t\t\t\tconst attr = attrs[cursor.index++]!;\n+\t\t\t\t\t\tconst attr = attribute(cursor.index++);\n \t\t\t\t\t\tconst key = JSON.stringify([attr.file, attr.name]);\n \t\t\t\t\t\tif (!p.storage.read(`${collection}:attributes`, key)) {\n-\t\t\t\t\t\t\tp.updateAttributes(state, { ...atom, attrs: [attr] }, !input.remove);\n+\t\t\t\t\t\t\tp.updateAttributes(state, { seq: atom.seq, attrs: [attr] }, !input.remove);\n \t\t\t\t\t\t\tp.storage.write(`${collection}:attributes`, key, true);\n \t\t\t\t\t\t}\n \t\t\t\t\t\tbreak;\n \t\t\t\t\t}\n \t\t\t\t\tcase \"contributions\": {\n-\t\t\t\t\t\tif (cursor.index === contributionFiles.length) {\n+\t\t\t\t\t\tif (cursor.index === files) {\n \t\t\t\t\t\t\tnext();\n \t\t\t\t\t\t\tbreak;\n \t\t\t\t\t\t}\n-\t\t\t\t\t\tconst path = contributionFiles[cursor.index++]!;\n+\t\t\t\t\t\tconst path = file(cursor.index++);\n \t\t\t\t\t\tp.ensureFile(state, path);\n \t\t\t\t\t\tconst key = p.fileKey(path)!;\n \t\t\t\t\t\tconst marker = p.markers.get(state.locator, key);\n@@ -266,20 +294,33 @@\n \t\t\t\t\t\tnext();\n \t\t\t\t\t\tbreak;\n \t\t\t\t\tcase \"moves\":\n-\t\t\t\t\t\tif (atom.moves?.length) {\n-\t\t\t\t\t\t\tconst result = adoptMovesStep(p, state, this.task, identity);\n+\t\t\t\t\t\tif (moves) {\n+\t\t\t\t\t\t\tconst result = adoptMovesStep(\n+\t\t\t\t\t\t\t\tp,\n+\t\t\t\t\t\t\t\tstate,\n+\t\t\t\t\t\t\t\tthis.task,\n+\t\t\t\t\t\t\t\tidentity,\n+\t\t\t\t\t\t\t\tindexed\n+\t\t\t\t\t\t\t\t\t? {\n+\t\t\t\t\t\t\t\t\t\t\tcount: (id) => indexed.related(id).count(\"moves\"),\n+\t\t\t\t\t\t\t\t\t\t\tat: (id, k) => {\n+\t\t\t\t\t\t\t\t\t\t\t\tconst atom = indexed.related(id);\n+\t\t\t\t\t\t\t\t\t\t\t\treturn k < atom.count(\"moves\") ? atom.item(\"moves\", k) : undefined;\n+\t\t\t\t\t\t\t\t\t\t\t},\n+\t\t\t\t\t\t\t\t\t\t}\n+\t\t\t\t\t\t\t\t\t: undefined,\n+\t\t\t\t\t\t\t);\n \t\t\t\t\t\t\tObject.assign(state, result.state);\n \t\t\t\t\t\t\tif (!result.done) break;\n \t\t\t\t\t\t}\n \t\t\t\t\t\tnext();\n \t\t\t\t\t\tbreak;\n \t\t\t\t\tcase \"cleanup\": {\n-\t\t\t\t\t\tconst attrs = atom.attrs ?? [];\n-\t\t\t\t\t\tif (cursor.index === attrs.length) {\n+\t\t\t\t\t\tif (cursor.index === attrs) {\n \t\t\t\t\t\t\tnext();\n \t\t\t\t\t\t\tbreak;\n \t\t\t\t\t\t}\n-\t\t\t\t\t\tconst attr = attrs[cursor.index++]!;\n+\t\t\t\t\t\tconst attr = attribute(cursor.index++);\n \t\t\t\t\t\tp.storage.remove(`${collection}:attributes`, JSON.stringify([attr.file, attr.name]));\n \t\t\t\t\t\tbreak;\n \t\t\t\t\t}"},{"path":"src/engine/experimental/projection-atom-index.ts","added":177,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/experimental/projection-atom-index.ts\n@@ -0,0 +1,177 @@\n+import { createHash } from \"node:crypto\";\n+import type { GraphStateStorage } from \"../state-source.ts\";\n+import type { Atom, AttrSet, Relocation } from \"../types.ts\";\n+\n+type Field = \"deps\" | \"inserts\" | \"deletes\" | \"attrs\" | \"moves\" | \"files\";\n+interface Run {\n+\tfile: string;\n+\tparent: string;\n+\tlines: number;\n+}\n+interface Header {\n+\tformat: 1;\n+\tid: string;\n+\tseq: number;\n+\tdigest: string;\n+\tcounts: Record<Field, number>;\n+\tchecksum: string;\n+}\n+const fields: Field[] = [\"deps\", \"inserts\", \"deletes\", \"attrs\", \"moves\", \"files\"];\n+\n+// Checksums detect record damage; authority comes from the canonical writer transaction.\n+const checksum = (value: unknown): string => createHash(\"sha256\").update(JSON.stringify(value)).digest(\"hex\");\n+const headerChecksum = (header: Omit<Header, \"checksum\">): string =>\n+\tchecksum([header.format, header.id, header.seq, header.digest, fields.map((field) => header.counts[field])]);\n+\n+function shape(value: unknown, keys: readonly string[]): asserts value is Record<string, unknown> {\n+\tif (\n+\t\t!value ||\n+\t\ttypeof value !== \"object\" ||\n+\t\tArray.isArray(value) ||\n+\t\tObject.keys(value).length !== keys.length ||\n+\t\tkeys.some((key) => !Object.hasOwn(value, key))\n+\t)\n+\t\tthrow Error(\"invalid projection atom record\");\n+}\n+const integer = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;\n+const atomId = (value: unknown): value is string =>\n+\ttypeof value === \"string\" && /^a(0|[1-9][0-9]*)$/.test(value) && integer(Number(value.slice(1)));\n+const lineId = (value: unknown): value is string =>\n+\ttypeof value === \"string\" &&\n+\t/^a(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$/.test(value) &&\n+\tvalue.split(\".\").every((part, i) => integer(Number(i === 0 ? part.slice(1) : part)));\n+const parent = (value: unknown) => typeof value === \"string\" && (value.startsWith(\"^\") || lineId(value));\n+function itemValue(field: Field, value: unknown): unknown {\n+\tif (field === \"deps\" || field === \"deletes\" || field === \"files\") {\n+\t\tif (!(field === \"deps\" ? atomId(value) : field === \"deletes\" ? lineId(value) : typeof value === \"string\"))\n+\t\t\tthrow Error(\"invalid projection atom item\");\n+\t} else if (field === \"inserts\") {\n+\t\tshape(value, [\"file\", \"parent\", \"lines\"]);\n+\t\tif (typeof value.file !== \"string\" || !parent(value.parent) || !integer(value.lines))\n+\t\t\tthrow Error(\"invalid projection atom run\");\n+\t} else if (field === \"moves\") {\n+\t\tshape(value, [\"line\", \"parent\"]);\n+\t\tif (!lineId(value.line) || !parent(value.parent)) throw Error(\"invalid projection atom move\");\n+\t} else {\n+\t\tshape(value, [\"file\", \"name\", \"value\"]);\n+\t\tif (\n+\t\t\ttypeof value.file !== \"string\" ||\n+\t\t\t![\"exists\", \"exec\", \"link\", \"blob\"].includes(String(value.name)) ||\n+\t\t\t(value.value !== null && typeof value.value !== \"string\")\n+\t\t)\n+\t\t\tthrow Error(\"invalid projection atom attribute\");\n+\t}\n+\treturn structuredClone(value);\n+}\n+\n+/** Derived index only. The caller supplies an already validated canonical atom and\n+ * its digest inside the canonical indexing transaction. This builder does not\n+ * claim bounded decoding of the original atom. */\n+export class ProjectionAtomIndex {\n+\treadonly storage: GraphStateStorage;\n+\tconstructor(storage: GraphStateStorage) {\n+\t\tthis.storage = storage;\n+\t}\n+\tput(atom: Atom, digest: string, files: readonly string[]): void {\n+\t\tif (!/^[a-f0-9]{64}$/.test(digest) || !Number.isSafeInteger(atom.seq) || atom.seq < 0 || atom.id !== `a${atom.seq}`)\n+\t\t\tthrow Error(\"invalid projection atom identity\");\n+\t\tconst name = `projection-atom-index:${atom.id}`;\n+\t\tthis.storage.transaction(() => {\n+\t\t\tif (this.storage.read(name, \"header\") !== undefined) throw Error(\"projection atom index already exists\");\n+\t\t\tconst counts = {} as Record<Field, number>;\n+\t\t\tfor (const field of fields) {\n+\t\t\t\tconst values = field === \"files\" ? files : (atom[field] ?? []);\n+\t\t\t\tcounts[field] = values.length;\n+\t\t\t\tfor (let i = 0; i < values.length; i++) {\n+\t\t\t\t\tconst value =\n+\t\t\t\t\t\tfield === \"inserts\"\n+\t\t\t\t\t\t\t? { file: atom.inserts[i]!.file, parent: atom.inserts[i]!.parent, lines: atom.inserts[i]!.lines.length }\n+\t\t\t\t\t\t\t: values[i];\n+\t\t\t\t\tconst item = itemValue(field, value);\n+\t\t\t\t\tthis.storage.write(name, `${field}:${i}`, {\n+\t\t\t\t\t\tvalue: item,\n+\t\t\t\t\t\tchecksum: checksum([atom.id, digest, field, i, item]),\n+\t\t\t\t\t});\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tconst header = { format: 1 as const, id: atom.id, seq: atom.seq, digest, counts };\n+\t\t\tthis.storage.write(name, \"header\", { ...header, checksum: headerChecksum(header) } satisfies Header);\n+\t\t});\n+\t}\n+\t/** Read a descriptor from a source-fenced, atomically maintained writer index. */\n+\topenAccepted(id: string): ProjectionAtomDescriptor {\n+\t\tif (!atomId(id)) throw Error(\"invalid projection atom identity\");\n+\t\tconst header = this.storage.read(`projection-atom-index:${id}`, \"header\");\n+\t\tshape(header, [\"format\", \"id\", \"seq\", \"digest\", \"counts\", \"checksum\"]);\n+\t\tif (typeof header.digest !== \"string\") throw Error(\"invalid projection atom digest\");\n+\t\treturn this.open(id, header.digest);\n+\t}\n+\n+\topen(id: string, digest: string): ProjectionAtomDescriptor {\n+\t\tif (!/^a(0|[1-9][0-9]*)$/.test(id) || !/^[a-f0-9]{64}$/.test(digest))\n+\t\t\tthrow Error(\"invalid projection atom identity\");\n+\t\tconst name = `projection-atom-index:${id}`;\n+\t\tconst raw = this.storage.read(name, \"header\");\n+\t\tshape(raw, [\"format\", \"id\", \"seq\", \"digest\", \"counts\", \"checksum\"]);\n+\t\tshape(raw.counts, fields);\n+\t\tconst header = raw as unknown as Header;\n+\t\tif (\n+\t\t\theader.checksum !== headerChecksum(header) ||\n+\t\t\theader.format !== 1 ||\n+\t\t\theader.id !== id ||\n+\t\t\theader.digest !== digest ||\n+\t\t\t!Number.isSafeInteger(header.seq) ||\n+\t\t\theader.seq < 0 ||\n+\t\t\tid !== `a${header.seq}` ||\n+\t\t\t!header.counts ||\n+\t\t\tObject.keys(header.counts).length !== fields.length ||\n+\t\t\tfields.some((field) => !Number.isSafeInteger(header.counts[field]) || header.counts[field] < 0)\n+\t\t)\n+\t\t\tthrow Error(\"missing or invalid projection atom index\");\n+\t\treturn new ProjectionAtomDescriptor(this.storage, name, structuredClone(header));\n+\t}\n+}\n+export class ProjectionAtomDescriptor {\n+\treadonly id: string;\n+\treadonly seq: number;\n+\treadonly digest: string;\n+\treadonly #storage: GraphStateStorage;\n+\treadonly #name: string;\n+\treadonly #counts: Record<Field, number>;\n+\tconstructor(storage: GraphStateStorage, name: string, header: Header) {\n+\t\tthis.#storage = storage;\n+\t\tthis.#name = name;\n+\t\tthis.id = header.id;\n+\t\tthis.seq = header.seq;\n+\t\tthis.digest = header.digest;\n+\t\tthis.#counts = { ...header.counts };\n+\t}\n+\trelated(id: string): ProjectionAtomDescriptor {\n+\t\treturn new ProjectionAtomIndex(this.#storage).openAccepted(id);\n+\t}\n+\n+\tcount(field: Field): number {\n+\t\tif (!fields.includes(field)) throw Error(\"invalid projection atom field\");\n+\t\treturn this.#counts[field];\n+\t}\n+\titem(field: \"inserts\", index: number): Run;\n+\titem(field: \"attrs\", index: number): AttrSet;\n+\titem(field: \"moves\", index: number): Relocation;\n+\titem(field: \"deps\" | \"deletes\" | \"files\", index: number): string;\n+\titem(field: Field, index: number): unknown {\n+\t\tif (!Number.isSafeInteger(index) || index < 0 || index >= this.count(field))\n+\t\t\tthrow Error(\"projection atom item out of range\");\n+\t\tconst value = this.#storage.read(this.#name, `${field}:${index}`);\n+\t\tif (value === undefined) throw Error(\"missing projection atom item\");\n+\t\tshape(value, [\"value\", \"checksum\"]);\n+\t\tif (value.checksum !== checksum([this.id, this.digest, field, index, value.value]))\n+\t\t\tthrow Error(\"invalid projection atom item checksum\");\n+\t\treturn itemValue(field, value.value);\n+\t}\n+}\n+\n+/** Opt-in persistent Graph callback: indexes the just-accepted canonical atom. */\n+export function indexAcceptedProjectionAtom(atom: Atom, files: readonly string[], storage: GraphStateStorage): void {\n+\tconst digest = createHash(\"sha256\").update(JSON.stringify(atom)).digest(\"hex\");\n+\tnew ProjectionAtomIndex(storage).put(atom, digest, files);\n+}"},{"path":"src/engine/experimental/projection-marker.ts","added":1,"removed":1,"diff":"--- a/src/engine/experimental/projection-marker.ts\n+++ b/src/engine/experimental/projection-marker.ts\n@@ -380,7 +380,7 @@\n \t\t}\n \t\treturn paths.sort();\n \t}\n-\tupdateAttributes(state: MarkerState, atom: Atom, adding: boolean): void {\n+\tupdateAttributes(state: MarkerState, atom: Pick<Atom, \"seq\" | \"attrs\">, adding: boolean): void {\n \t\tthis.assertContext(state);\n \t\tconst index = this.attributePriorities,\n \t\t\thex = (n: number) => n.toString(16).padStart(14, \"0\");"},{"path":"src/engine/experimental/projection-move-adoption.ts","added":12,"removed":10,"diff":"--- a/src/engine/experimental/projection-move-adoption.ts\n+++ b/src/engine/experimental/projection-move-adoption.ts\n@@ -3,7 +3,7 @@\n import { orderedChildKey } from \"../ordered-index.ts\";\n import type { GraphStateStorage } from \"../state-source.ts\";\n import type { ExperimentalMarkerProjection, MarkerState } from \"./projection-marker.ts\";\n-import { resolveMovesPage } from \"./projection-move-resolution.ts\";\n+import { graphMoveSource, type ProjectionMoveSource, resolveMovesPage } from \"./projection-move-resolution.ts\";\n \n type Phase = \"files\" | \"resolve\" | \"plans\" | \"park\" | \"place\" | \"cleanup\" | \"done\";\n interface Cursor {\n@@ -41,6 +41,7 @@\n \tprevious: MarkerState,\n \ttask: string,\n \tidentity: string,\n+\tsource: ProjectionMoveSource = graphMoveSource(p.graph),\n ): { done: boolean; state: MarkerState } {\n \tp.assertContext(previous);\n \tif (!task || task.length > 128 || !/^[a-f0-9]{64}$/.test(identity)) throw Error(\"invalid move adoption identity\");\n@@ -83,10 +84,9 @@\n \t\t)\n \t\t\tthrow Error(\"invalid move adoption cursor\");\n \t\tconst mover = p.graph.movers()[cursor.mover];\n-\t\tif (cursor.move > (mover === undefined ? 0 : (p.graph.atom(mover).moves?.length ?? 0)))\n-\t\t\tthrow Error(\"invalid move adoption offset\");\n+\t\tif (cursor.move > (mover === undefined ? 0 : source.count(mover))) throw Error(\"invalid move adoption offset\");\n \t\tlet state = { ...previous };\n-\t\tconst has = (id: string) => p.members.get(state.membership, p.graph.atom(id).seq)?.present ?? false;\n+\t\tconst has = (id: string) => p.members.get(state.membership, Number(id.slice(1)))?.present ?? false;\n \t\tswitch (cursor.phase) {\n \t\t\tcase \"files\":\n \t\t\tcase \"plans\": {\n@@ -96,14 +96,16 @@\n \t\t\t\t\tcursor.move = 0;\n \t\t\t\t\tbreak;\n \t\t\t\t}\n-\t\t\t\tconst moves = p.graph.atom(p.graph.movers()[cursor.mover]!).moves ?? [];\n-\t\t\t\tif (cursor.move > moves.length) throw Error(\"invalid move adoption offset\");\n-\t\t\t\tif (cursor.move === moves.length) {\n+\t\t\t\tconst moverId = p.graph.movers()[cursor.mover]!;\n+\t\t\t\tconst count = source.count(moverId);\n+\t\t\t\tif (cursor.move > count) throw Error(\"invalid move adoption offset\");\n+\t\t\t\tif (cursor.move === count) {\n \t\t\t\t\tcursor.mover++;\n \t\t\t\t\tcursor.move = 0;\n \t\t\t\t\tbreak;\n \t\t\t\t}\n-\t\t\t\tconst move = moves[cursor.move++]!;\n+\t\t\t\tconst move = source.at(moverId, cursor.move++);\n+\t\t\t\tif (!move) throw Error(\"missing indexed move\");\n \t\t\t\tif (cursor.phase === \"files\") {\n \t\t\t\t\tif (move.parent.startsWith(\"^\")) p.ensureFile(state, move.parent.slice(1));\n \t\t\t\t\tbreak;\n@@ -119,7 +121,7 @@\n \t\t\t\tif (parent === undefined) throw Error(\"missing relocation ancestry\");\n \t\t\t\tconst parentKey = parent.startsWith(\"^\") ? p.fileKey(parent.slice(1)) : p.graph.lineN(parent) + 1;\n \t\t\t\tif (parentKey === undefined) throw Error(\"missing relocation file\");\n-\t\t\t\tconst seq = selected ? p.graph.atom(selected.atom).seq : p.graph.writerSeqOf(n);\n+\t\t\t\tconst seq = selected ? Number(selected.atom.slice(1)) : p.graph.writerSeqOf(n);\n \t\t\t\tconst key = orderedChildKey({\n \t\t\t\t\tn,\n \t\t\t\t\treplaces: selected ? 0 : Number(p.graph.replacesN(n)),\n@@ -132,7 +134,7 @@\n \t\t\t\tbreak;\n \t\t\t}\n \t\t\tcase \"resolve\":\n-\t\t\t\tif (resolveMovesPage(p.graph, has, scratch, identity, 1).done) cursor.phase = \"plans\";\n+\t\t\t\tif (resolveMovesPage(p.graph, has, scratch, identity, 1, source).done) cursor.phase = \"plans\";\n \t\t\t\tbreak;\n \t\t\tcase \"park\":\n \t\t\tcase \"place\": {"},{"path":"src/engine/experimental/projection-move-resolution.ts","added":19,"removed":9,"diff":"--- a/src/engine/experimental/projection-move-resolution.ts\n+++ b/src/engine/experimental/projection-move-resolution.ts\n@@ -2,8 +2,16 @@\n import type { Graph, RelocationInfo } from \"../graph.ts\";\n import { isStartNode } from \"../keys.ts\";\n import type { GraphStateStorage } from \"../state-source.ts\";\n-import type { NodeId } from \"../types.ts\";\n+import type { NodeId, Relocation } from \"../types.ts\";\n \n+export interface ProjectionMoveSource {\n+\tcount(id: string): number;\n+\tat(id: string, k: number): Relocation | undefined;\n+}\n+export function graphMoveSource(graph: Graph): ProjectionMoveSource {\n+\treturn { count: (id) => graph.atom(id).moves?.length ?? 0, at: (id, k) => graph.atom(id).moves?.[k] };\n+}\n+\n interface Progress {\n \tidentity: string;\n \tphase: \"scan\" | \"walk\" | \"entry\" | \"weakest\" | \"repick\" | \"mark\" | \"done\";\n@@ -33,7 +41,7 @@\n \t)\n \t\tthrow Error(\"invalid move resolution record\");\n }\n-function readProgress(value: unknown, graph: Graph): Progress | undefined {\n+function readProgress(value: unknown, graph: Graph, source: ProjectionMoveSource): Progress | undefined {\n \tif (value === undefined) return undefined;\n \tshape(value, \"format checksum state\");\n \tif (value.format !== 1 || value.checksum !== digest(value.state)) throw Error(\"damaged move resolution cursor\");\n@@ -51,8 +59,7 @@\n \t)\n \t\tthrow Error(\"invalid move resolution position\");\n \tconst mover = graph.movers()[Number(state.mover)];\n-\tif (Number(state.move) > (mover === undefined ? 0 : (graph.atom(mover).moves?.length ?? 0)))\n-\t\tthrow Error(\"invalid move offset\");\n+\tif (Number(state.move) > (mover === undefined ? 0 : source.count(mover))) throw Error(\"invalid move offset\");\n \tfor (const key of [\"line\", \"slow\", \"fast\", \"meeting\", \"entry\", \"current\"])\n \t\tif (state[key] !== null && (typeof state[key] !== \"string\" || !state[key] || String(state[key]).length > 4096))\n \t\t\tthrow Error(\"invalid move ancestry cursor\");\n@@ -61,7 +68,7 @@\n \t\tif (move === null) continue;\n \t\tshape(move, \"atom line parent k\");\n \t\tif (typeof move.atom !== \"string\" || !integer(move.k)) throw Error(\"invalid saved move\");\n-\t\tconst canonical = graph.atom(move.atom).moves?.[move.k];\n+\t\tconst canonical = source.at(move.atom, move.k);\n \t\tif (!canonical || canonical.line !== move.line || canonical.parent !== move.parent)\n \t\t\tthrow Error(\"saved move identity mismatch\");\n \t}\n@@ -83,11 +90,12 @@\n \tstorage: GraphStateStorage,\n \tidentity: string,\n \tmaxSteps: number,\n+\tsource: ProjectionMoveSource = graphMoveSource(graph),\n ): { done: boolean; steps: number } {\n \tif (!identity || identity.length > 4096 || !Number.isSafeInteger(maxSteps) || maxSteps < 1 || maxSteps > 256)\n \t\tthrow Error(\"invalid move resolution page\");\n \treturn storage.transaction(() => {\n-\t\tlet state = readProgress(storage.read(\"progress\", \"cursor\"), graph);\n+\t\tlet state = readProgress(storage.read(\"progress\", \"cursor\"), graph, source);\n \t\tif (state && state.identity !== identity) throw Error(\"move resolution identity changed\");\n \t\tstate ??= {\n \t\t\tidentity,\n@@ -144,14 +152,16 @@\n \t\t\t\t\t\tstate.move = 0;\n \t\t\t\t\t\tbreak;\n \t\t\t\t\t}\n-\t\t\t\t\tconst moves = graph.atom(id).moves ?? [];\n-\t\t\t\t\tif (state.move === moves.length) {\n+\t\t\t\t\tconst count = source.count(id);\n+\t\t\t\t\tif (state.move === count) {\n \t\t\t\t\t\tstate.mover++;\n \t\t\t\t\t\tstate.move = 0;\n \t\t\t\t\t\tbreak;\n \t\t\t\t\t}\n \t\t\t\t\tconst k = state.move++;\n-\t\t\t\t\tconst candidate: RelocationInfo = { atom: id, ...moves[k]!, k };\n+\t\t\t\t\tconst move = source.at(id, k);\n+\t\t\t\t\tif (!move) throw Error(\"missing indexed move\");\n+\t\t\t\t\tconst candidate: RelocationInfo = { atom: id, ...move, k };\n \t\t\t\t\tif (state.phase === \"scan\") {\n \t\t\t\t\t\tif (!storage.read(\"seen\", candidate.line)) {\n \t\t\t\t\t\t\tstorage.write(\"seen\", candidate.line, true);"},{"path":"src/engine/graph.ts","added":14,"removed":1,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -277,6 +277,8 @@\n \t#textCache = new Map<number, readonly string[]>();\n \t#textCached = 0;\n \n+\treadonly #indexAcceptedAtom?: (atom: Atom, files: readonly string[], storage: GraphStateStorage) => void;\n+\n \t/**\n \t * `texts`, when given, says where each atom's lines' text can be read back\n \t * (#46): the graph then keeps no text, only each line's place, and reads an\n@@ -287,8 +289,13 @@\n \t\t\ttexts?: (seq: number) => readonly (readonly string[])[];\n \t\t\tlists?: (seq: number) => AtomLists;\n \t\t\tstorage?: GraphStateStorage;\n+\t\t\t/** Optional derived indexing inside persistent atom acceptance; must be synchronous. */\n+\t\t\tindexAcceptedAtom?: (atom: Atom, files: readonly string[], storage: GraphStateStorage) => void;\n \t\t} = {},\n \t) {\n+\t\tif (options.indexAcceptedAtom && !options.storage)\n+\t\t\tthrow Error(\"accepted atom indexing requires persistent storage\");\n+\t\tthis.#indexAcceptedAtom = options.indexAcceptedAtom;\n \t\tif (options.texts) {\n \t\t\tthis.#texts = options.texts;\n \t\t\tthis.#lineText = undefined;\n@@ -994,7 +1001,13 @@\n \t * The atom is frozen. Adding to the graph does not add it to any set.\n \t */\n \tadd(atom: Atom): void {\n-\t\tif (this.#storage) this.#storage.transaction(() => this.#add(atom));\n+\t\tif (this.#storage)\n+\t\t\tthis.#storage.transaction(() => {\n+\t\t\t\tthis.#add(atom);\n+\t\t\t\tconst result: unknown = this.#indexAcceptedAtom?.(atom, this.filesOf(atom.id), this.#storage!);\n+\t\t\t\tif (result && typeof result === \"object\" && \"then\" in result)\n+\t\t\t\t\tthrow Error(\"accepted atom indexing must be synchronous\");\n+\t\t\t});\n \t\telse this.#add(atom);\n \t}\n "},{"path":"src/engine/test/projection-atom-index.test.ts","added":227,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/projection-atom-index.test.ts\n@@ -0,0 +1,227 @@\n+import assert from \"node:assert/strict\";\n+import { createHash } from \"node:crypto\";\n+import { mkdtempSync, rmdirSync, unlinkSync } from \"node:fs\";\n+import { tmpdir } from \"node:os\";\n+import { join } from \"node:path\";\n+import { test } from \"node:test\";\n+import { ProjectionAtomAdoption } from \"../experimental/projection-atom-adoption.ts\";\n+import { indexAcceptedProjectionAtom, ProjectionAtomIndex } from \"../experimental/projection-atom-index.ts\";\n+import { ExperimentalMarkerProjection } from \"../experimental/projection-marker.ts\";\n+import { Graph } from \"../graph.ts\";\n+import { Repo } from \"./helpers.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"projection atom descriptors exclude inserted text and read one indexed item\", () => {\n+\tconst db = new SqlState();\n+\ttry {\n+\t\tconst repo = Repo.init({ \"a.txt\": \"large text\\n\".repeat(1000) });\n+\t\tconst atom = repo.graph.atom(\"a0\"),\n+\t\t\tdigest = \"a\".repeat(64);\n+\t\tnew ProjectionAtomIndex(db).put(atom, digest, [\"a.txt\"]);\n+\t\tconst descriptor = new ProjectionAtomIndex(db).open(\"a0\", digest);\n+\t\tconst before = db.reads;\n+\t\tassert.equal(descriptor.count(\"inserts\"), 1);\n+\t\tassert.deepEqual(descriptor.item(\"inserts\", 0), { file: \"a.txt\", parent: \"^a.txt\", lines: 1000 });\n+\t\tassert.equal(db.reads - before, 1);\n+\t\tassert.throws(() => descriptor.item(\"inserts\", 1), /out of range/);\n+\t\tassert.throws(() => new ProjectionAtomIndex(db).open(\"a0\", \"b\".repeat(64)), /invalid/);\n+\t\tconst rows = db.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\t\tassert.ok(!JSON.stringify(rows).includes(\"large text\"));\n+\t\tassert.throws(() => new ProjectionAtomIndex(db).put(atom, digest, [\"a.txt\"]), /already exists/);\n+\t\tassert.deepEqual(db.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all(), rows);\n+\t} finally {\n+\t\tdb.db.close();\n+\t}\n+});\n+\n+test(\"descriptor builds roll back every write and corrupted records refuse\", () => {\n+\tconst db = new SqlState();\n+\ttry {\n+\t\tconst repo = Repo.init({ \"a.txt\": \"one\\n\" });\n+\t\tconst atom = repo.graph.atom(\"a0\"),\n+\t\t\tdigest = \"c\".repeat(64);\n+\t\tconst index = new ProjectionAtomIndex(db);\n+\t\tconst writeCount =\n+\t\t\tatom.deps.length +\n+\t\t\tatom.inserts.length +\n+\t\t\tatom.deletes.length +\n+\t\t\t(atom.attrs?.length ?? 0) +\n+\t\t\t(atom.moves?.length ?? 0) +\n+\t\t\t2;\n+\t\tfor (let offset = 1; offset <= writeCount; offset++) {\n+\t\t\tdb.failWrite = db.writes + offset;\n+\t\t\tassert.throws(() => index.put(atom, digest, [\"a.txt\"]), /injected/);\n+\t\t\tassert.deepEqual(db.db.prepare(\"SELECT * FROM state\").all(), []);\n+\t\t}\n+\t\tdb.failWrite = Infinity;\n+\t\tindex.put(atom, digest, [\"a.txt\"]);\n+\t\tconst name = \"projection-atom-index:a0\";\n+\t\tconst header = db.read(name, \"header\") as { counts: Record<string, number> };\n+\t\tfor (const invalid of [\n+\t\t\tnull,\n+\t\t\t{ ...header, extra: true },\n+\t\t\t{ ...header, counts: { ...header.counts, moves: -1 } },\n+\t\t\t{ ...header, counts: [] },\n+\t\t\t{ ...header, counts: { ...header.counts, inserts: 0 } },\n+\t\t]) {\n+\t\t\tdb.write(name, \"header\", invalid);\n+\t\t\tassert.throws(() => new ProjectionAtomIndex(db).open(\"a0\", digest), /invalid/);\n+\t\t}\n+\t\tdb.write(name, \"header\", header);\n+\t\tconst descriptor = new ProjectionAtomIndex(db).open(\"a0\", digest);\n+\t\tconst original = db.read(name, \"inserts:0\") as { value: unknown; checksum: string };\n+\t\tfor (const invalid of [\n+\t\t\tnull,\n+\t\t\t{ file: \"a.txt\", parent: \"bad\", lines: 1 },\n+\t\t\t{ file: \"a.txt\", parent: \"^a.txt\", lines: -1 },\n+\t\t\t{ file: \"a.txt\", parent: \"^a.txt\", lines: 1, extra: true },\n+\t\t\t{ ...original, value: { file: \"a.txt\", parent: \"^a.txt\", lines: 2 } },\n+\t\t]) {\n+\t\t\tdb.write(name, \"inserts:0\", invalid);\n+\t\t\tassert.throws(() => descriptor.item(\"inserts\", 0), /invalid/);\n+\t\t}\n+\t\tdb.write(name, \"inserts:0\", original);\n+\t\tconst run = descriptor.item(\"inserts\", 0);\n+\t\trun.lines = 999;\n+\t\tassert.equal(descriptor.item(\"inserts\", 0).lines, 1);\n+\t} finally {\n+\t\tdb.db.close();\n+\t}\n+});\n+\n+test(\"descriptor-driven insertion adoption never reads full atom storage\", () => {\n+\tconst db = new SqlState();\n+\ttry {\n+\t\tconst repo = Repo.init({ \"a.txt\": \"one\\ntwo\\nthree\\n\" });\n+\t\tconst atom = repo.graph.atom(\"a0\"),\n+\t\t\tdigest = \"d\".repeat(64);\n+\t\tconst graph = new Graph({ storage: db });\n+\t\tgraph.add(atom);\n+\t\tnew ProjectionAtomIndex(db).put(atom, digest, graph.filesOf(atom.id));\n+\t\tconst projection = new ExperimentalMarkerProjection(db, graph, \"a.txt\", \"indexed-adoption\");\n+\t\tconst previous = projection.empty();\n+\t\tconst read = db.read.bind(db);\n+\t\tdb.read = (collection, key) => {\n+\t\t\tif (collection === \"atoms\") throw Error(\"full atom read forbidden\");\n+\t\t\treturn read(collection, key);\n+\t\t};\n+\t\tlet pages = 0;\n+\t\tfor (;;) {\n+\t\t\tconst descriptor = new ProjectionAtomIndex(db).open(atom.id, digest);\n+\t\t\tconst result = new ProjectionAtomAdoption(projection, \"indexed\").page({\n+\t\t\t\tprevious,\n+\t\t\t\tatom: descriptor,\n+\t\t\t\tremove: false,\n+\t\t\t\tsource: \"e\".repeat(64),\n+\t\t\t\tmaxSteps: 2,\n+\t\t\t\tvalidateSource: () => {},\n+\t\t\t});\n+\t\t\tassert.ok(result.steps <= 2);\n+\t\t\tassert.ok(++pages < 100);\n+\t\t\tif (result.done) {\n+\t\t\t\tassert.deepEqual(projection.lines(result.state), [\"a0.0\", \"a0.1\", \"a0.2\"]);\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t\tassert.ok(pages > 1);\n+\t} finally {\n+\t\tdb.db.close();\n+\t}\n+});\n+\n+test(\"indexed adoption reopens through dependencies, attributes, deletion and reverse removal without atom reads\", () => {\n+\tconst directory = mkdtempSync(join(tmpdir(), \"abso-descriptor-\")),\n+\t\tfilename = join(directory, \"state.sqlite\");\n+\tlet db = new SqlState(filename);\n+\tconst oracleDb = new SqlState();\n+\ttry {\n+\t\tconst repo = Repo.init({ \"a.txt\": \"one\\ntwo\\nthree\\n\", \"b.txt\": \"\" });\n+\t\tconst atoms = [\n+\t\t\trepo.graph.atom(\"a0\"),\n+\t\t\trepo.edit({\n+\t\t\t\tedits: [{ file: \"a.txt\", old: \"two\\n\", new: \"replacement\\n\" }],\n+\t\t\t\tattrs: [{ file: \"b.txt\", name: \"exec\", value: \"1\" }],\n+\t\t\t}),\n+\t\t];\n+\t\tatoms.push(repo.edit({ move: { file: \"a.txt\", start: 0, end: 1, to: \"b.txt\", at: 0 } }));\n+\t\tconst graph = new Graph({ storage: db });\n+\t\tfor (const atom of atoms) {\n+\t\t\tgraph.add(atom);\n+\t\t\tnew ProjectionAtomIndex(db).put(atom, \"a\".repeat(64), graph.filesOf(atom.id));\n+\t\t}\n+\t\tconst oracle = new ExperimentalMarkerProjection(oracleDb, repo.graph, \"a.txt\", \"oracle\");\n+\t\tlet expected = oracle.empty();\n+\t\tlet state = new ExperimentalMarkerProjection(db, graph, \"a.txt\", \"indexed\").empty();\n+\t\tlet task = 0;\n+\t\tfor (const [atom, remove] of [\n+\t\t\t...atoms.map((atom) => [atom, false] as const),\n+\t\t\t...atoms.toReversed().map((atom) => [atom, true] as const),\n+\t\t]) {\n+\t\t\tconst previous = state,\n+\t\t\t\tid = String(task++);\n+\t\t\tlet pages = 0,\n+\t\t\t\tdone = false;\n+\t\t\twhile (!done) {\n+\t\t\t\tconst read = db.read.bind(db);\n+\t\t\t\tdb.read = (collection, key) => {\n+\t\t\t\t\tif (collection === \"atoms\") throw Error(\"full atom read forbidden\");\n+\t\t\t\t\treturn read(collection, key);\n+\t\t\t\t};\n+\t\t\t\tconst p = new ExperimentalMarkerProjection(db, new Graph({ storage: db }), \"a.txt\", \"indexed\");\n+\t\t\t\tconst descriptor = new ProjectionAtomIndex(db).open(atom.id, \"a\".repeat(64));\n+\t\t\t\tconst result = new ProjectionAtomAdoption(p, id).page({\n+\t\t\t\t\tprevious,\n+\t\t\t\t\tatom: descriptor,\n+\t\t\t\t\tremove,\n+\t\t\t\t\tsource: \"b\".repeat(64),\n+\t\t\t\t\tmaxSteps: 2,\n+\t\t\t\t\tvalidateSource: () => {},\n+\t\t\t\t});\n+\t\t\t\tstate = result.state;\n+\t\t\t\tdone = result.done;\n+\t\t\t\tassert.ok(++pages < 100);\n+\t\t\t\tdb.db.close();\n+\t\t\t\tdb = new SqlState(filename);\n+\t\t\t}\n+\t\t\texpected = remove ? oracle.removeAtom(expected, atom) : oracle.apply(expected, atom);\n+\t\t\tconst actual = new ExperimentalMarkerProjection(db, new Graph({ storage: db }), \"a.txt\", \"indexed\");\n+\t\t\tfor (const path of [\"a.txt\", \"b.txt\"]) {\n+\t\t\t\tassert.deepEqual(actual.lines(state, path), oracle.lines(expected, path));\n+\t\t\t\tassert.deepEqual(actual.attrs(state, path), oracle.attrs(expected, path));\n+\t\t\t}\n+\t\t\tassert.deepEqual(actual.knownPaths(state), oracle.knownPaths(expected));\n+\t\t}\n+\t} finally {\n+\t\tdb.db.close();\n+\t\toracleDb.db.close();\n+\t\tunlinkSync(filename);\n+\t\trmdirSync(directory);\n+\t}\n+});\n+\n+test(\"accepted graph and descriptor commit together and retry after callback failure\", () => {\n+\tconst db = new SqlState();\n+\ttry {\n+\t\tconst atom = Repo.init({ \"a.txt\": \"one\\n\" }).graph.atom(\"a0\");\n+\t\tlet fail = true;\n+\t\tconst graph = new Graph({\n+\t\t\tstorage: db,\n+\t\t\tindexAcceptedAtom: (accepted, files, storage) => {\n+\t\t\t\tindexAcceptedProjectionAtom(accepted, files, storage);\n+\t\t\t\tif (fail) throw Error(\"after descriptor failure\");\n+\t\t\t},\n+\t\t});\n+\t\tconst before = db.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\t\tassert.throws(() => graph.add(atom), /after descriptor failure/);\n+\t\tassert.deepEqual(db.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all(), before);\n+\t\tassert.equal(graph.size, 0);\n+\t\tfail = false;\n+\t\tgraph.add(atom);\n+\t\tconst digest = createHash(\"sha256\").update(JSON.stringify(atom)).digest(\"hex\");\n+\t\tassert.equal(new ProjectionAtomIndex(db).open(\"a0\", digest).item(\"inserts\", 0).lines, 1);\n+\t\tassert.equal(new Graph({ storage: db }).size, 1);\n+\t\tassert.throws(() => new Graph({ indexAcceptedAtom: indexAcceptedProjectionAtom }), /requires persistent/);\n+\t} finally {\n+\t\tdb.db.close();\n+\t}\n+});"},{"path":"src/engine/test/projection-marker-cycle.test.ts","added":25,"removed":10,"diff":"--- a/src/engine/test/projection-marker-cycle.test.ts\n+++ b/src/engine/test/projection-marker-cycle.test.ts\n@@ -1,6 +1,7 @@\n import assert from \"node:assert/strict\";\n import { test } from \"node:test\";\n import { ProjectionAtomAdoption } from \"../experimental/projection-atom-adoption.ts\";\n+import { indexAcceptedProjectionAtom, ProjectionAtomIndex } from \"../experimental/projection-atom-index.ts\";\n import { ExperimentalMarkerProjection as MarkerProjection } from \"../experimental/projection-marker.ts\";\n import { Graph } from \"../graph.ts\";\n import { CachedStateStorage } from \"../state-source.ts\";\n@@ -8,12 +9,15 @@\n import { Repo, Rng } from \"./helpers.ts\";\n import { SqlState } from \"./storage-helper.ts\";\n \n-for (const paged of [false, true])\n+for (const paged of [false, true, \"indexed\"] as const)\n \ttest(`typed marker projection matches competing cyclic moves and removals (paged=${paged})`, () => {\n \t\tconst db = new SqlState();\n \t\ttry {\n \t\t\tconst repo = Repo.init({ \"f.txt\": Array.from({ length: 12 }, (_, n) => `${n}\\n`).join(\"\") }),\n-\t\t\t\tgraph = new Graph({ storage: new CachedStateStorage(db) }),\n+\t\t\t\tgraph = new Graph({\n+\t\t\t\t\tstorage: new CachedStateStorage(db),\n+\t\t\t\t\tindexAcceptedAtom: paged === \"indexed\" ? indexAcceptedProjectionAtom : undefined,\n+\t\t\t\t}),\n \t\t\t\trng = new Rng(45131);\n \t\t\tgraph.add(repo.graph.atom(\"a0\"));\n \t\t\tconst p = new MarkerProjection(db, graph, \"f.txt\", \"typed-\" + \"cycle\");\n@@ -26,14 +30,25 @@\n \t\t\t\tconst job = new ProjectionAtomAdoption(p, String(task++));\n \t\t\t\tconst previous = state;\n \t\t\t\tfor (let pages = 0; pages < 10000; pages++) {\n-\t\t\t\t\tconst result = job.page({\n-\t\t\t\t\t\tprevious,\n-\t\t\t\t\t\tatom,\n-\t\t\t\t\t\tremove,\n-\t\t\t\t\t\tsource: \"c\".repeat(64),\n-\t\t\t\t\t\tmaxSteps: 3,\n-\t\t\t\t\t\tvalidateSource: () => {},\n-\t\t\t\t\t});\n+\t\t\t\t\tconst read = db.read.bind(db);\n+\t\t\t\t\tif (paged === \"indexed\")\n+\t\t\t\t\t\tdb.read = (collection, key) => {\n+\t\t\t\t\t\t\tif (collection === \"atoms\") throw Error(\"full atom read forbidden\");\n+\t\t\t\t\t\t\treturn read(collection, key);\n+\t\t\t\t\t\t};\n+\t\t\t\t\tlet result;\n+\t\t\t\t\ttry {\n+\t\t\t\t\t\tresult = job.page({\n+\t\t\t\t\t\t\tprevious,\n+\t\t\t\t\t\t\tatom: paged === \"indexed\" ? new ProjectionAtomIndex(db).openAccepted(atom.id) : atom,\n+\t\t\t\t\t\t\tremove,\n+\t\t\t\t\t\t\tsource: \"c\".repeat(64),\n+\t\t\t\t\t\t\tmaxSteps: 3,\n+\t\t\t\t\t\t\tvalidateSource: () => {},\n+\t\t\t\t\t\t});\n+\t\t\t\t\t} finally {\n+\t\t\t\t\t\tdb.read = read;\n+\t\t\t\t\t}\n \t\t\t\t\tassert.ok(result.steps <= 3);\n \t\t\t\t\tassert.equal(result.atomicMoveWork, false);\n \t\t\t\t\tif (result.done) return result.state;"},{"path":"worker/project/project-types.ts","added":2,"removed":0,"diff":"--- a/worker/project/project-types.ts\n+++ b/worker/project/project-types.ts\n@@ -567,6 +567,8 @@\n \n /** What the project needs from the store of binary files' bytes. */\n export interface ProjectOptions {\n+\t/** Optional derived atom indexing within persistent graph acceptance. */\n+\tindexAcceptedAtom?: (atom: Atom, files: readonly string[], storage: GraphStateStorage) => void;\n \t/** Private experimental reader route; never falls back for unsupported membership. */\n \treadViewProvider?: (graph: Graph) => ReadViewProvider;\n \t/**"},{"path":"worker/project/project.ts","added":3,"removed":1,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -348,6 +348,8 @@\n \treadonly #hasBlob: (sha256: string) => boolean;\n \n \tconstructor(store: ProjectStore, options: ProjectOptions = {}, checkpoint?: typeof WRITER_REOPEN) {\n+\t\tif (options.indexAcceptedAtom && !options.writerStorage)\n+\t\t\tthrow Error(\"accepted atom indexing requires writer storage\");\n \t\tthis.#store = store;\n \t\tthis.#writerStorage = options.writerStorage ? new CachedStateStorage(options.writerStorage) : undefined;\n \t\tif (this.#writerStorage) {\n@@ -402,7 +404,7 @@\n \t\t};\n \t\t// Line text and each atom's deletes and claim keys stay in the log (#46), read back when asked for.\n \t\tthis.#graph = this.#writerStorage\n-\t\t\t? new Graph({ storage: this.#writerStorage })\n+\t\t\t? new Graph({ storage: this.#writerStorage, indexAcceptedAtom: options.indexAcceptedAtom })\n \t\t\t: entryAt\n \t\t\t\t? new Graph({ texts: (seq) => stored(seq).inserts.map((r) => r.lines), lists: (seq) => stored(seq) })\n \t\t\t\t: new Graph();"},{"path":"worker/project/test/project-writer-storage.test.ts","added":35,"removed":0,"diff":"--- a/worker/project/test/project-writer-storage.test.ts\n+++ b/worker/project/test/project-writer-storage.test.ts\n@@ -1,5 +1,10 @@\n import assert from \"node:assert/strict\";\n+import { createHash } from \"node:crypto\";\n import { test } from \"node:test\";\n+import {\n+\tindexAcceptedProjectionAtom,\n+\tProjectionAtomIndex,\n+} from \"../../../src/engine/experimental/projection-atom-index.ts\";\n import { Graph } from \"../../../src/engine/graph.ts\";\n import { StableSet, storedArray } from \"../../../src/engine/state-source.ts\";\n import { SqlState } from \"../../../src/engine/test/storage-helper.ts\";\n@@ -206,3 +211,33 @@\n \tassert.deepEqual(Project.openWriter(store, storage, entries.length).files(), { \"a.txt\": \"ONE\\n\" });\n \tstorage.db.close();\n });\n+\n+test(\"opted-in descriptor indexing survives writer reopen and rolls back canonical append failure\", () => {\n+\tconst fixture = setup();\n+\tconst { storage, entries, store } = fixture;\n+\tconst options = { indexAcceptedAtom: indexAcceptedProjectionAtom };\n+\ttry {\n+\t\tlet project = new Project(store, { writerStorage: storage, ...options });\n+\t\tstorage.transaction(() => project.init({ \"a.txt\": \"one\\n\" }, 0));\n+\t\tproject = Project.openWriter(store, storage, entries.length, options);\n+\t\tconst rows = storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\t\tfixture.fail();\n+\t\tassert.throws(\n+\t\t\t() => storage.transaction(() => project.record(\"writer\", { \"a.txt\": \"two\\n\" }, 1)),\n+\t\t\t/canonical append failure/,\n+\t\t);\n+\t\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all(), rows);\n+\t\tfixture.recover();\n+\t\tproject = Project.openWriter(store, storage, entries.length, options);\n+\t\tstorage.transaction(() => project.record(\"writer\", { \"a.txt\": \"two\\n\" }, 1));\n+\t\tstorage.transaction(() => project.publish(\"writer\", \"Indexed\", 2));\n+\t\tconst graph = new Graph({ storage });\n+\t\tfor (const atom of graph.atoms()) {\n+\t\t\tconst digest = createHash(\"sha256\").update(JSON.stringify(atom)).digest(\"hex\");\n+\t\t\tassert.equal(new ProjectionAtomIndex(storage).open(atom.id, digest).seq, atom.seq);\n+\t\t}\n+\t\tassert.deepEqual(Project.openWriter(store, storage, entries.length, options).files(), { \"a.txt\": \"two\\n\" });\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});"}],"ci":null}