{"draft":null,"from":136,"to":137,"files":[{"path":"src/engine/graph.ts","added":11,"removed":5,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -2,6 +2,7 @@\n // the codebase) are separate values; the graph only answers questions about\n // atoms and keeps indexes so those questions are cheap.\n \n+import { ChildCounts } from \"./child-counts.ts\";\n import {\n \tatomId,\n \tattrKey,\n@@ -254,6 +255,7 @@\n \n export class Graph {\n \treadonly #storage?: GraphStateStorage;\n+\treadonly #childCounts?: ChildCounts;\n \t#atoms: Atom[] = [];\n \t#lamport: number[] = [];\n \t// Lines, packed (#46): line n is the k-th line atom seq wrote, n = #lineBase[seq] + k. Each holds its\n@@ -293,6 +295,7 @@\n \t\t}\n \t\tif (options.lists) this.#lists = options.lists;\n \t\tthis.#storage = options.storage;\n+\t\tif (options.storage) this.#childCounts = new ChildCounts(options.storage);\n \t\tif (options.storage) {\n \t\t\tconst storage = options.storage;\n \t\t\tconst numbers = (name: string): NumberIndex => storedNumbers(storage, name);\n@@ -663,10 +666,11 @@\n \t*orderedChildrenN(code: number, after?: number): IterableIterator<number> {\n \t\tif (!this.#storage) throw new Error(\"ordered children require indexed storage\");\n \t\tconst index = new OrderedIndex(this.#storage, \"ordered-children\", String(code));\n-\t\tif (this.#storage.read(\"ordered-children\", \"format\") !== 1)\n+\t\tif (this.#storage.read(\"ordered-children\", \"format\") !== 2)\n \t\t\tthrow new Error(\"ordered children require fresh adoption\");\n-\t\tconst count = index.size;\n-\t\tif (count !== this.#runStartPages!.length(String(code))) throw new Error(\"ordered child index count mismatch\");\n+\t\tconst count = code >= 0 ? this.#childCounts!.at(code, this.#lineTotal) : index.size;\n+\t\tif ((code < 0 || count > 0) && (count !== index.size || count !== this.#runStartPages!.length(String(code))))\n+\t\t\tthrow new Error(\"ordered child index count mismatch\");\n \t\tlet implicit: number | undefined;\n \t\tif (\n \t\t\tcode >= 0 &&\n@@ -967,9 +971,9 @@\n \t}\n \n \t#add(atom: Atom): void {\n-\t\tif (this.#storage && this.#storage.read(\"ordered-children\", \"format\") !== 1) {\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\", 1);\n+\t\t\tthis.#storage.write(\"ordered-children\", \"format\", 2);\n \t\t}\n \n \t\tvalidateShape(atom);\n@@ -1069,6 +1073,7 @@\n \t\t\tconst file = this.#pathOf(run.file);\n \t\t\tlet parent = isStartNode(run.parent) ? -1 - file : this.#lineN(run.parent);\n \t\t\tconst first = this.#lineTotal;\n+\t\t\tif (parent >= 0) this.#childCounts?.increment(parent, first);\n \t\t\tif (this.#storage)\n \t\t\t\tnew OrderedIndex(this.#storage, \"ordered-children\", String(parent)).insert(\n \t\t\t\t\torderedChildKey({\n@@ -1085,6 +1090,7 @@\n \t\t\telse pushOne(this.#runStarts, parent, first);\n \t\t\tfor (let i = 0; i < run.lines.length; i++) {\n \t\t\t\tconst n = this.#lineTotal;\n+\t\t\t\tthis.#childCounts?.append(n);\n \t\t\t\tthis.#lineAtom.push(seq);\n \t\t\t\tthis.#lineFile.push(file);\n \t\t\t\tthis.#lineParent.push(parent);"}],"unmatched":[]}