{"draft":null,"from":161,"to":162,"files":[{"path":"src/engine/graph.ts","added":310,"removed":70,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -20,12 +20,14 @@\n import { withProjectionScratch } from \"./projection-scratch.ts\";\n import {\n \ttype GraphStateStorage,\n+\treadonlyStoredArray,\n \ttype StateMap,\n \tstoredArray,\n \tstoredLists,\n \tstoredMap,\n \tstoredNumbers,\n } from \"./state-source.ts\";\n+import { StoredText } from \"./stored-text.ts\";\n import {\n \tATTR_NAMES,\n \ttype Atom,\n@@ -306,12 +308,169 @@\n \t\tif (options.storage) {\n \t\t\tconst storage = options.storage;\n \t\t\tconst numbers = (name: string): NumberIndex => storedNumbers(storage, name);\n-\t\t\tthis.#atoms = storedArray<Atom>(storage, \"atoms\", (atom) =>\n-\t\t\t\tfreezeAtom(\n-\t\t\t\t\tatom,\n-\t\t\t\t\t(id) => id,\n-\t\t\t\t\t(author) => author,\n-\t\t\t\t),\n+\t\t\tconst atomText = new StoredText(storage, \"atom-text:long\");\n+\t\t\ttype TextReference = { text: string };\n+\t\t\tconst listFields = [\n+\t\t\t\t\"deletes\",\n+\t\t\t\t\"writes\",\n+\t\t\t\t\"reads\",\n+\t\t\t\t\"holds\",\n+\t\t\t\t\"needs\",\n+\t\t\t\t\"resolves\",\n+\t\t\t\t\"deps\",\n+\t\t\t\t\"reasons\",\n+\t\t\t\t\"attrs\",\n+\t\t\t\t\"moves\",\n+\t\t\t] as const;\n+\t\t\ttype EncodedAtom = {\n+\t\t\t\tinsertionFormat?: 1;\n+\t\t\t\tinsertionRuns?: string;\n+\t\t\t\ttextFormat?: 1;\n+\t\t\t\tlistFormat?: 1;\n+\t\t\t\tlistCount?: number;\n+\t\t\t\tlists?: Partial<Record<(typeof listFields)[number], string>>;\n+\t\t\t\tatom: Atom;\n+\t\t\t};\n+\t\t\tthis.#atoms = storedArray<Atom>(\n+\t\t\t\tstorage,\n+\t\t\t\t\"atoms\",\n+\t\t\t\t(value) => {\n+\t\t\t\t\tconst encoded = value as unknown as Partial<EncodedAtom>;\n+\t\t\t\t\tif (encoded.textFormat !== undefined && encoded.textFormat !== 1)\n+\t\t\t\t\t\tthrow Error(\"invalid stored atom text format\");\n+\t\t\t\t\tif (encoded.listFormat !== undefined && encoded.listFormat !== 1)\n+\t\t\t\t\t\tthrow Error(\"invalid stored atom list format\");\n+\t\t\t\t\tif (encoded.listFormat === 1) {\n+\t\t\t\t\t\tif (\n+\t\t\t\t\t\t\t!encoded.lists ||\n+\t\t\t\t\t\t\ttypeof encoded.lists !== \"object\" ||\n+\t\t\t\t\t\t\tArray.isArray(encoded.lists) ||\n+\t\t\t\t\t\t\t!Number.isSafeInteger(encoded.listCount) ||\n+\t\t\t\t\t\t\tencoded.listCount! < 1 ||\n+\t\t\t\t\t\t\tObject.keys(encoded.lists).length !== encoded.listCount ||\n+\t\t\t\t\t\t\tObject.keys(encoded.lists).some((field) => !listFields.includes(field as (typeof listFields)[number]))\n+\t\t\t\t\t\t)\n+\t\t\t\t\t\t\tthrow Error(\"invalid stored atom list references\");\n+\t\t\t\t\t}\n+\t\t\t\t\tif (encoded.insertionFormat !== undefined && encoded.insertionFormat !== 1)\n+\t\t\t\t\t\tthrow Error(\"invalid stored insertion format\");\n+\t\t\t\t\tconst atom =\n+\t\t\t\t\t\tencoded.textFormat === 1 || encoded.listFormat === 1 || encoded.insertionFormat === 1\n+\t\t\t\t\t\t\t? encoded.atom!\n+\t\t\t\t\t\t\t: value;\n+\t\t\t\t\tconst text =\n+\t\t\t\t\t\tencoded.textFormat === 1\n+\t\t\t\t\t\t\t? (start: number, length: number): readonly string[] => {\n+\t\t\t\t\t\t\t\t\tconst result: string[] = [];\n+\t\t\t\t\t\t\t\t\tlet index = 0;\n+\t\t\t\t\t\t\t\t\tfor (const run of atom.inserts)\n+\t\t\t\t\t\t\t\t\t\tfor (const raw of run.lines) {\n+\t\t\t\t\t\t\t\t\t\t\tif (index >= start && index < start + length) {\n+\t\t\t\t\t\t\t\t\t\t\t\tconst reference = raw as unknown as TextReference;\n+\t\t\t\t\t\t\t\t\t\t\t\tif (typeof raw === \"string\") result.push(raw);\n+\t\t\t\t\t\t\t\t\t\t\t\telse if (reference && typeof reference.text === \"string\")\n+\t\t\t\t\t\t\t\t\t\t\t\t\tresult.push(atomText.read(reference.text));\n+\t\t\t\t\t\t\t\t\t\t\t\telse throw Error(\"invalid stored atom text reference\");\n+\t\t\t\t\t\t\t\t\t\t\t}\n+\t\t\t\t\t\t\t\t\t\t\tif (++index >= start + length) return result;\n+\t\t\t\t\t\t\t\t\t\t}\n+\t\t\t\t\t\t\t\t\tthrow Error(\"stored atom text range missing\");\n+\t\t\t\t\t\t\t\t}\n+\t\t\t\t\t\t\t: undefined;\n+\t\t\t\t\tconst frozen = freezeAtom(\n+\t\t\t\t\t\tatom,\n+\t\t\t\t\t\t(id) => id,\n+\t\t\t\t\t\t(author) => author,\n+\t\t\t\t\t\ttext,\n+\t\t\t\t\t);\n+\t\t\t\t\tif (encoded.listFormat !== 1 && encoded.insertionFormat !== 1) return frozen;\n+\t\t\t\t\tconst result = { ...frozen };\n+\t\t\t\t\tif (encoded.insertionFormat === 1) {\n+\t\t\t\t\t\tconst collection = `atom-inserts:${atom.seq}`;\n+\t\t\t\t\t\tif (encoded.insertionRuns !== collection) throw Error(\"invalid stored insertion reference\");\n+\t\t\t\t\t\tconst inserts = readonlyStoredArray<InsertRun>(storage, collection, (raw, index) => {\n+\t\t\t\t\t\t\tconst run = raw as { file: string; parent: InsertRun[\"parent\"]; lines: string };\n+\t\t\t\t\t\t\tconst lines = `${collection}:lines:${index}`;\n+\t\t\t\t\t\t\tif (run.lines !== lines || typeof run.file !== \"string\" || typeof run.parent !== \"string\")\n+\t\t\t\t\t\t\t\tthrow Error(\"invalid stored insertion run\");\n+\t\t\t\t\t\t\treturn Object.freeze({\n+\t\t\t\t\t\t\t\tfile: run.file,\n+\t\t\t\t\t\t\t\tparent: run.parent,\n+\t\t\t\t\t\t\t\tlines: readonlyStoredArray<string>(storage, lines, (rawLine) => {\n+\t\t\t\t\t\t\t\t\tif (typeof rawLine === \"string\") return rawLine;\n+\t\t\t\t\t\t\t\t\tconst ref = rawLine as TextReference;\n+\t\t\t\t\t\t\t\t\tif (!ref || typeof ref.text !== \"string\") throw Error(\"invalid stored insertion text\");\n+\t\t\t\t\t\t\t\t\treturn atomText.read(ref.text);\n+\t\t\t\t\t\t\t\t}),\n+\t\t\t\t\t\t\t});\n+\t\t\t\t\t\t});\n+\t\t\t\t\t\tObject.defineProperty(result, \"inserts\", { value: inserts, enumerable: true });\n+\t\t\t\t\t}\n+\t\t\t\t\tfor (const field of listFields) {\n+\t\t\t\t\t\tconst collection = encoded.lists?.[field];\n+\t\t\t\t\t\tif (collection === undefined) continue;\n+\t\t\t\t\t\tif (typeof collection !== \"string\" || collection !== `atom-list:${atom.seq}:${field}`)\n+\t\t\t\t\t\t\tthrow Error(\"invalid stored atom list reference\");\n+\t\t\t\t\t\tObject.defineProperty(result, field, { value: readonlyStoredArray(storage, collection), enumerable: true });\n+\t\t\t\t\t}\n+\t\t\t\t\treturn Object.freeze(result);\n+\t\t\t\t},\n+\t\t\t\t(input, key) => {\n+\t\t\t\t\tconst atom = { ...input };\n+\t\t\t\t\tconst lists: Partial<Record<(typeof listFields)[number], string>> = {};\n+\t\t\t\t\tfor (const field of listFields) {\n+\t\t\t\t\t\tconst values = input[field];\n+\t\t\t\t\t\tif (!values || values.length <= 256) continue;\n+\t\t\t\t\t\tconst collection = `atom-list:${input.seq}:${field}`;\n+\t\t\t\t\t\tconst saved = storedArray<unknown>(storage, collection);\n+\t\t\t\t\t\tfor (const value of values) saved.push(value);\n+\t\t\t\t\t\tlists[field] = collection;\n+\t\t\t\t\t\tObject.defineProperty(atom, field, { value: [], enumerable: true });\n+\t\t\t\t\t}\n+\t\t\t\t\tconst listCount = Object.keys(lists).length;\n+\t\t\t\t\tconst hasLists = listCount > 0;\n+\t\t\t\t\tif (atom.inserts.length > 256 || atom.inserts.some((run) => run.lines.length > 256)) {\n+\t\t\t\t\t\tconst collection = `atom-inserts:${input.seq}`;\n+\t\t\t\t\t\tconst runs = storedArray<unknown>(storage, collection);\n+\t\t\t\t\t\tlet runIndex = 0;\n+\t\t\t\t\t\tlet lineIndex = 0;\n+\t\t\t\t\t\tfor (const run of atom.inserts) {\n+\t\t\t\t\t\t\tconst lineCollection = `${collection}:lines:${runIndex++}`;\n+\t\t\t\t\t\t\tconst lines = storedArray<unknown>(storage, lineCollection);\n+\t\t\t\t\t\t\tfor (const line of run.lines) {\n+\t\t\t\t\t\t\t\tconst text = JSON.stringify([key, lineIndex++]);\n+\t\t\t\t\t\t\t\tif (line.length > StoredText.chunkCharacters) {\n+\t\t\t\t\t\t\t\t\tatomText.write(text, line);\n+\t\t\t\t\t\t\t\t\tlines.push({ text });\n+\t\t\t\t\t\t\t\t} else lines.push(line);\n+\t\t\t\t\t\t\t}\n+\t\t\t\t\t\t\truns.push({ file: run.file, parent: run.parent, lines: lineCollection });\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\treturn {\n+\t\t\t\t\t\t\tinsertionFormat: 1,\n+\t\t\t\t\t\t\tinsertionRuns: collection,\n+\t\t\t\t\t\t\t...(hasLists ? { listFormat: 1, listCount, lists } : {}),\n+\t\t\t\t\t\t\tatom: { ...atom, inserts: [] },\n+\t\t\t\t\t\t};\n+\t\t\t\t\t}\n+\t\t\t\t\tif (!atom.inserts.some((run) => run.lines.some((line) => line.length > StoredText.chunkCharacters)))\n+\t\t\t\t\t\treturn hasLists ? { listFormat: 1, listCount, lists, atom } : atom;\n+\t\t\t\t\tlet index = 0;\n+\t\t\t\t\tconst inserts = atom.inserts.map((run) => ({\n+\t\t\t\t\t\t...run,\n+\t\t\t\t\t\tlines: run.lines.map((line) => {\n+\t\t\t\t\t\t\tconst text = JSON.stringify([key, index++]);\n+\t\t\t\t\t\t\tif (line.length <= StoredText.chunkCharacters) return line;\n+\t\t\t\t\t\t\tatomText.write(text, line);\n+\t\t\t\t\t\t\treturn { text };\n+\t\t\t\t\t\t}),\n+\t\t\t\t\t}));\n+\t\t\t\t\treturn {\n+\t\t\t\t\t\ttextFormat: 1,\n+\t\t\t\t\t\t...(hasLists ? { listFormat: 1, listCount, lists } : {}),\n+\t\t\t\t\t\tatom: { ...atom, inserts },\n+\t\t\t\t\t};\n+\t\t\t\t},\n \t\t\t);\n \t\t\tthis.#lamport = storedArray(storage, \"lamport\");\n \t\t\tthis.#lineBase = storedArray(storage, \"line-base\");\n@@ -324,7 +483,26 @@\n \t\t\tthis.#lineParent = numbers(\"line-parent\");\n \t\t\tthis.#deleter = numbers(\"deleter\");\n \t\t\tthis.#replacing = numbers(\"replacing\");\n-\t\t\tthis.#lineText = storedArray(storage, \"line-text\");\n+\t\t\tconst longText = atomText;\n+\t\t\tthis.#lineText = storedArray<string>(\n+\t\t\t\tstorage,\n+\t\t\t\t\"line-text\",\n+\t\t\t\t(value) => {\n+\t\t\t\t\tif (typeof value === \"string\") return value;\n+\t\t\t\t\tconst reference = value as unknown as { format: number; text: string };\n+\t\t\t\t\tif (reference?.format !== 1 || typeof reference.text !== \"string\") throw Error(\"invalid long line reference\");\n+\t\t\t\t\treturn longText.read(reference.text);\n+\t\t\t\t},\n+\t\t\t\t(value, key) => {\n+\t\t\t\t\tif (value.length <= StoredText.chunkCharacters) return value;\n+\t\t\t\t\tconst number = Number(key);\n+\t\t\t\t\tconst seq = this.#lineAtom.at(number);\n+\t\t\t\t\tconst text = JSON.stringify([String(seq), number - this.#lineBase[seq]!]);\n+\t\t\t\t\tconst header = storage.read(\"atom-text:long\", text) as { characters?: number } | undefined;\n+\t\t\t\t\tif (header?.characters !== value.length) throw Error(\"long line atom text is missing\");\n+\t\t\t\t\treturn { format: 1, text };\n+\t\t\t\t},\n+\t\t\t);\n \t\t\tthis.#pathIndex = storedMap(storage, \"path-index\");\n \t\t\tthis.#moreDeleterPages = storedLists<number>(storage, \"more-deleters\", (key) =>\n \t\t\t\tJSON.stringify([\"number\", Number(key)]),\n@@ -565,6 +743,15 @@\n \n \t/** The lines an atom deletes, from the graph's own packed copy (#46): never a read of the log. */\n \tdeletedLines(id: AtomId): readonly LineId[] {\n+\t\tconst seq = this.#seq(id);\n+\t\tconst count = this.#storage?.read(\"atom-delete-counts\", String(seq));\n+\t\tif (count !== undefined) {\n+\t\t\tconst out: LineId[] = [];\n+\t\t\tfor (const n of storedLists<number>(this.#storage!, \"atom-deletes\").values(String(seq)))\n+\t\t\t\tout.push(this.#lineName(n));\n+\t\t\tif (out.length !== count) throw Error(\"incomplete atom deletion index\");\n+\t\t\treturn out;\n+\t\t}\n \t\tconst ns = this.#atomDeletes[this.#seq(id)];\n \t\tif (!ns) return EMPTY;\n \t\tconst out: LineId[] = new Array(ns.length);\n@@ -574,7 +761,13 @@\n \n \t/** How many lines an atom deletes, without reading its lists. */\n \tdeleteCount(id: AtomId): number {\n-\t\treturn this.#atomDeletes[this.#seq(id)]?.length ?? 0;\n+\t\tconst seq = this.#seq(id);\n+\t\tconst count = this.#storage?.read(\"atom-delete-counts\", String(seq));\n+\t\tif (count !== undefined) {\n+\t\t\tif (!Number.isSafeInteger(count) || (count as number) < 0) throw Error(\"invalid atom deletion count\");\n+\t\t\treturn count as number;\n+\t\t}\n+\t\treturn this.#atomDeletes[seq]?.length ?? 0;\n \t}\n \n \ttextOf(n: number): string {\n@@ -957,10 +1150,6 @@\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@@ -968,7 +1157,10 @@\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\tfor (const id of from) {\n+\t\t\t\t\tif (id === target) return true;\n+\t\t\t\t\tif (this.#lamport[this.#seq(id)]! > floor) push(id);\n+\t\t\t\t}\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@@ -1011,12 +1203,11 @@\n \t\telse this.#add(atom);\n \t}\n \n-\t#add(atom: Atom): void {\n-\t\tif (this.#storage && this.#storage.read(\"ordered-children\", \"format\") !== 2) {\n-\t\t\tif (this.#atoms.length !== 0) throw new Error(\"ordered children require fresh adoption\");\n-\t\t\tthis.#storage.write(\"ordered-children\", \"format\", 2);\n-\t\t}\n-\n+\t#validateAcceptedAtom(atom: Atom, scratch?: GraphStateStorage): void {\n+\t\tconst membership = <T extends string>(name: string): { has(key: T): boolean; add(key: T): unknown } => {\n+\t\t\tif (!scratch) return new Set<T>();\n+\t\t\treturn { has: (key) => scratch.read(name, key) === true, add: (key) => scratch.write(name, key, true) };\n+\t\t};\n \t\tvalidateShape(atom);\n \t\tconst seq = this.#atoms.length;\n \t\tconst bad = (why: string): never => {\n@@ -1024,24 +1215,32 @@\n \t\t};\n \t\tif (atom.seq !== seq || atom.id !== atomId(seq)) bad(`expected id ${atomId(seq)} with seq ${seq}`);\n \n-\t\tconst deps = new Set<AtomId>();\n+\t\tconst deps = membership<AtomId>(\"deps\");\n \t\tfor (const d of atom.deps) {\n \t\t\tif (!this.has(d)) bad(`dependency ${d} is not in the graph`);\n \t\t\tif (deps.has(d)) bad(`dependency ${d} listed twice`);\n \t\t\tdeps.add(d);\n \t\t}\n-\t\tfor (const d of [...atom.needs, ...atom.resolves]) {\n-\t\t\tif (!deps.has(d)) bad(`${d} is in needs/resolves but not in deps`);\n-\t\t}\n+\t\tfor (const list of [atom.needs, atom.resolves])\n+\t\t\tfor (const d of list) {\n+\t\t\t\tif (!deps.has(d)) bad(`${d} is in needs/resolves but not in deps`);\n+\t\t\t}\n \t\tfor (const r of atom.reasons) if (!this.has(r.to)) bad(`reason points at unknown atom ${r.to}`);\n \n \t\t// Everything it touches must come from its closure. Derived deps name the\n \t\t// writers directly, so this is usually one lookup per writer.\n-\t\tconst below = new Map<AtomId, boolean>();\n+\t\tconst memoryBelow = scratch ? undefined : new Map<AtomId, boolean>();\n+\t\tconst below = {\n+\t\t\tget: (key: AtomId) => (scratch ? (scratch.read(\"below\", key) as boolean | undefined) : memoryBelow!.get(key)),\n+\t\t\tset: (key: AtomId, value: boolean) => {\n+\t\t\t\tif (scratch) scratch.write(\"below\", key, value);\n+\t\t\t\telse memoryBelow!.set(key, value);\n+\t\t\t},\n+\t\t};\n \t\tconst inClosure = (writer: AtomId): boolean => {\n \t\t\tlet hit = below.get(writer);\n \t\t\tif (hit === undefined) {\n-\t\t\t\thit = deps.has(writer) || this.#reaches(deps, writer);\n+\t\t\t\thit = deps.has(writer) || this.#reaches(atom.deps, writer);\n \t\t\t\tbelow.set(writer, hit);\n \t\t\t}\n \t\t\treturn hit;\n@@ -1060,45 +1259,65 @@\n \t\t\t\tcheckLine(run.parent, \"an insert\");\n \t\t\t}\n \t\t}\n-\t\tconst moved = new Set<LineId>();\n+\t\tconst writes = membership<Key>(\"writes\");\n+\t\tfor (const key of atom.writes) writes.add(key);\n+\t\tconst moved = membership<LineId>(\"moved\");\n \t\tfor (const m of atom.moves ?? []) {\n \t\t\tcheckLine(m.line, \"a move\");\n \t\t\tif (!isStartNode(m.parent)) checkLine(m.parent, \"a move\");\n \t\t\tif (m.line === m.parent) bad(`moves ${m.line} after itself`);\n \t\t\tif (moved.has(m.line)) bad(`moves ${m.line} twice`);\n \t\t\tmoved.add(m.line);\n-\t\t\tif (!atom.writes.includes(posKey(m.line)) || !atom.writes.includes(gapKey(m.parent))) {\n+\t\t\tif (!writes.has(posKey(m.line)) || !writes.has(gapKey(m.parent))) {\n \t\t\t\tbad(`moves ${m.line} without writing ${posKey(m.line)} and ${gapKey(m.parent)}`);\n \t\t\t}\n \t\t}\n-\t\tconst deleted = new Set<LineId>();\n+\t\tconst deleted = membership<LineId>(\"deleted\");\n \t\tfor (const line of atom.deletes) {\n \t\t\tcheckLine(line, \"a delete\");\n \t\t\tif (deleted.has(line)) bad(`deletes ${line} twice`);\n \t\t\tdeleted.add(line);\n \t\t}\n-\t\tconst writes = new Set(atom.writes);\n-\t\tfor (const line of deleted) if (!writes.has(lineKey(line))) bad(`deletes ${line} without writing ${lineKey(line)}`);\n-\t\tfor (const key of [...atom.writes, ...atom.reads, ...atom.holds]) {\n-\t\t\tconst parsed = parseKey(key);\n-\t\t\tif (parsed.kind === \"line\") checkLine(parsed.node, `key ${key}`);\n-\t\t\telse if ((parsed.kind === \"gap\" || parsed.kind === \"pos\") && !isStartNode(parsed.node))\n-\t\t\t\tcheckLine(parsed.node, `key ${key}`);\n+\t\tfor (const line of atom.deletes)\n+\t\t\tif (!writes.has(lineKey(line))) bad(`deletes ${line} without writing ${lineKey(line)}`);\n+\t\tfor (const list of [atom.writes, atom.reads, atom.holds])\n+\t\t\tfor (const key of list) {\n+\t\t\t\tconst parsed = parseKey(key);\n+\t\t\t\tif (parsed.kind === \"line\") checkLine(parsed.node, `key ${key}`);\n+\t\t\t\telse if ((parsed.kind === \"gap\" || parsed.kind === \"pos\") && !isStartNode(parsed.node))\n+\t\t\t\t\tcheckLine(parsed.node, `key ${key}`);\n+\t\t\t}\n+\t}\n+\n+\t#add(atom: Atom): void {\n+\t\tif (this.#storage && this.#storage.read(\"ordered-children\", \"format\") !== 2) {\n+\t\t\tif (this.#atoms.length !== 0) throw new Error(\"ordered children require fresh adoption\");\n+\t\t\tthis.#storage.write(\"ordered-children\", \"format\", 2);\n \t\t}\n \n+\t\tif (this.#storage) this.withProjectionScratch((scratch) => this.#validateAcceptedAtom(atom, scratch));\n+\t\telse this.#validateAcceptedAtom(atom);\n+\t\tconst seq = this.#atoms.length;\n+\t\tconst deps = atom.deps;\n+\t\tconst deleted = this.#storage ? undefined : new Set(atom.deletes);\n+\t\tconst writes = this.#storage ? atom.writes : new Set(atom.writes);\n+\n \t\t// Accept.\n-\t\tconst frozen = freezeAtom(\n-\t\t\tatom,\n-\t\t\t(id) => this.idOfSeq(seqOf(id)),\n-\t\t\t(author) => {\n-\t\t\t\tif (this.#storage) return author;\n-\t\t\t\tlet a = this.#authors.get(author);\n-\t\t\t\tif (a === undefined) this.#authors.set(author, (a = author));\n-\t\t\t\treturn a;\n-\t\t\t},\n-\t\t\tthis.#texts ? this.#reader(seq) : undefined,\n-\t\t\tthis.#lists ? this.#listReader(seq) : undefined,\n-\t\t);\n+\t\t// Persistent storage copies the accepted atom; a second frozen copy only duplicates its lists.\n+\t\tconst frozen = this.#storage\n+\t\t\t? atom\n+\t\t\t: freezeAtom(\n+\t\t\t\t\tatom,\n+\t\t\t\t\t(id) => this.idOfSeq(seqOf(id)),\n+\t\t\t\t\t(author) => {\n+\t\t\t\t\t\tif (this.#storage) return author;\n+\t\t\t\t\t\tlet a = this.#authors.get(author);\n+\t\t\t\t\t\tif (a === undefined) this.#authors.set(author, (a = author));\n+\t\t\t\t\t\treturn a;\n+\t\t\t\t\t},\n+\t\t\t\t\tthis.#texts ? this.#reader(seq) : undefined,\n+\t\t\t\t\tthis.#lists ? this.#listReader(seq) : undefined,\n+\t\t\t\t);\n \t\tthis.#atoms.push(this.#storage ? atom : frozen);\n \t\tlet lam = 0;\n \t\tfor (const d of deps) lam = Math.max(lam, this.lamport(d) + 1);\n@@ -1106,9 +1325,28 @@\n \n \t\t// The run starts' parents and the deleted lines, as codes, before this atom's own lines exist.\n \t\t// The atom as given: with lists kept elsewhere, the frozen one reads them back from the log.\n-\t\tconst deletes = atom.deletes.length > 0 ? new Set(atom.deletes) : undefined;\n-\t\tconst deletedN = atom.deletes.map((line) => this.#lineN(line));\n-\t\tthis.#atomDeletes.push(deletedN.length > 0 ? (this.#storage ? deletedN : Int32Array.from(deletedN)) : undefined);\n+\t\tconst deletes = (line: LineId | NodeId): boolean => {\n+\t\t\tif (isStartNode(line)) return false;\n+\t\t\tif (deleted) return deleted.has(line);\n+\t\t\tconst n = this.#lineN(line);\n+\t\t\treturn this.#deleter.at(n) === seq || this.#moreDeleterPages!.last(String(n)) === seq;\n+\t\t};\n+\t\tconst deletedN = this.#storage ? undefined : atom.deletes.map((line) => this.#lineN(line));\n+\t\tthis.#atomDeletes.push(deletedN?.length ? Int32Array.from(deletedN) : undefined);\n+\t\tif (this.#storage) {\n+\t\t\tconst pages = storedLists<number>(this.#storage, \"atom-deletes\");\n+\t\t\tfor (const line of atom.deletes) pages.append(String(seq), this.#lineN(line));\n+\t\t\tthis.#storage.write(\"atom-delete-counts\", String(seq), atom.deletes.length);\n+\t\t}\n+\t\tconst deletedNumbers = function* (graph: Graph) {\n+\t\t\tif (deletedN) yield* deletedN;\n+\t\t\telse for (const line of atom.deletes) yield graph.#lineN(line);\n+\t\t};\n+\t\tfor (const n of deletedNumbers(this)) {\n+\t\t\tif (this.#deleter.at(n) < 0) this.#deleter.set(n, seq);\n+\t\t\telse if (this.#moreDeleterPages) this.#moreDeleterPages.append(String(n), seq);\n+\t\t\telse pushIndex(this.#moreDeleters, n, seq);\n+\t\t}\n \t\tthis.#lineBase.push(this.#lineTotal);\n \t\tfor (const run of atom.inserts) {\n \t\t\tconst file = this.#pathOf(run.file);\n@@ -1119,7 +1357,7 @@\n \t\t\t\tnew OrderedIndex(this.#storage, \"ordered-children\", String(parent)).insert(\n \t\t\t\t\torderedChildKey({\n \t\t\t\t\t\tn: first,\n-\t\t\t\t\t\treplaces: deletes?.has(run.parent) ? 1 : 0,\n+\t\t\t\t\t\treplaces: deletes(run.parent) ? 1 : 0,\n \t\t\t\t\t\tlamport: lam,\n \t\t\t\t\t\tseq,\n \t\t\t\t\t\tk: first - this.#lineBase[seq]!,\n@@ -1137,7 +1375,7 @@\n \t\t\t\tthis.#lineParent.push(parent);\n \t\t\t\tthis.#lineText?.push(run.lines[i]!);\n \t\t\t\tthis.#deleter.push(-1);\n-\t\t\t\tthis.#replacing.push(i === 0 && deletes?.has(run.parent) ? 1 : 0);\n+\t\t\t\tthis.#replacing.push(i === 0 && deletes(run.parent) ? 1 : 0);\n \t\t\t\tparent = n;\n \t\t\t}\n \t\t}\n@@ -1152,37 +1390,39 @@\n \t\t\telse pushIndex(this.#relocatedToCode, parentCode, info);\n \t\t});\n \t\tif (frozen.moves) this.#movers.push(frozen.id);\n-\t\tfor (const n of deletedN) {\n-\t\t\tif (this.#deleter.at(n) < 0) this.#deleter.set(n, seq);\n-\t\t\telse if (this.#moreDeleterPages) this.#moreDeleterPages.append(String(n), seq);\n-\t\t\telse pushIndex(this.#moreDeleters, n, seq);\n-\t\t}\n-\t\tfor (const key of new Set(atom.writes)) {\n+\t\tfor (const key of writes) {\n \t\t\tconst code = this.#code(key);\n-\t\t\tif (this.#writersPages) this.#writersPages.append(JSON.stringify([typeof code, code]), frozen.id);\n-\t\t\telse pushOne(this.#writers, code, frozen.id);\n+\t\t\tif (this.#writersPages) {\n+\t\t\t\tconst key = JSON.stringify([typeof code, code]);\n+\t\t\t\tif (this.#writersPages.last(key) !== frozen.id) this.#writersPages.append(key, frozen.id);\n+\t\t\t} else pushOne(this.#writers, code, frozen.id);\n \t\t}\n-\t\tfor (const key of new Set(atom.reads)) {\n+\t\tfor (const key of this.#storage ? atom.reads : new Set(atom.reads)) {\n \t\t\tconst code = this.#code(key);\n-\t\t\tif (this.#readersPages) this.#readersPages.append(JSON.stringify([typeof code, code]), frozen.id);\n-\t\t\telse pushOne(this.#readers, code, frozen.id);\n+\t\t\tif (this.#readersPages) {\n+\t\t\t\tconst key = JSON.stringify([typeof code, code]);\n+\t\t\t\tif (this.#readersPages.last(key) !== frozen.id) this.#readersPages.append(key, frozen.id);\n+\t\t\t} else pushOne(this.#readers, code, frozen.id);\n \t\t}\n-\t\tfor (const key of new Set(atom.holds)) {\n+\t\tfor (const key of this.#storage ? atom.holds : new Set(atom.holds)) {\n \t\t\tconst code = this.#code(key);\n-\t\t\tif (this.#holdersPages) this.#holdersPages.append(JSON.stringify([typeof code, code]), frozen.id);\n-\t\t\telse pushOne(this.#holders, code, frozen.id);\n+\t\t\tif (this.#holdersPages) {\n+\t\t\t\tconst key = JSON.stringify([typeof code, code]);\n+\t\t\t\tif (this.#holdersPages.last(key) !== frozen.id) this.#holdersPages.append(key, frozen.id);\n+\t\t\t} else pushOne(this.#holders, code, frozen.id);\n \t\t}\n \t\tfor (const d of deps) {\n \t\t\tif (this.#dependentsPages) this.#dependentsPages.append(d, frozen.id);\n \t\t\telse pushOne(this.#dependents, d, frozen.id);\n \t\t}\n-\t\tfor (const r of new Set(frozen.resolves)) {\n-\t\t\tif (this.#resolversPages) this.#resolversPages.append(r, frozen.id);\n-\t\t\telse pushOne(this.#resolvers, r, frozen.id);\n+\t\tfor (const r of this.#storage ? frozen.resolves : new Set(frozen.resolves)) {\n+\t\t\tif (this.#resolversPages) {\n+\t\t\t\tif (this.#resolversPages.last(r) !== frozen.id) this.#resolversPages.append(r, frozen.id);\n+\t\t\t} else pushOne(this.#resolvers, r, frozen.id);\n \t\t}\n \t\tconst files = new Set<string>();\n \t\tfor (const run of atom.inserts) files.add(run.file);\n-\t\tfor (const n of deletedN) files.add(this.#paths[this.#lineFile.at(n)]!);\n+\t\tfor (const n of deletedNumbers(this)) files.add(this.#paths[this.#lineFile.at(n)]!);\n \t\tfor (const a of frozen.attrs ?? []) files.add(a.file);\n \t\tfor (const m of frozen.moves ?? []) {\n \t\t\tfiles.add(this.lineFile(m.line)!);"}],"unmatched":[]}