{"draft":null,"from":4,"to":318,"files":[{"path":"src/engine/child-counts.ts","added":72,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/child-counts.ts\n@@ -0,0 +1,72 @@\n+import type { GraphStateStorage } from \"./state-source.ts\";\n+\n+const PAGE_SIZE = 256;\n+type Page = { format: 1; page: number; counts: number[]; digest: string };\n+\n+/** Two independent 32-bit checksums over page identity and contents. This\n+ * detects inconsistent storage corruption, not an adversarial coherent rewrite. */\n+function digest(page: number, counts: readonly number[]): string {\n+\tconst text = JSON.stringify([1, page, counts]);\n+\tlet a = 2166136261;\n+\tlet b = 5381;\n+\tfor (let i = 0; i < text.length; i++) {\n+\t\ta = Math.imul(a ^ text.charCodeAt(i), 16777619) >>> 0;\n+\t\tb = (Math.imul(b, 33) ^ text.charCodeAt(i)) >>> 0;\n+\t}\n+\treturn a.toString(16).padStart(8, \"0\") + b.toString(16).padStart(8, \"0\");\n+}\n+\n+/** Dense line-node counts. Graph append owns the enclosing transaction. */\n+export class ChildCounts {\n+\treadonly #storage: GraphStateStorage;\n+\tconstructor(storage: GraphStateStorage) {\n+\t\tthis.#storage = storage;\n+\t}\n+\t#read(page: number, total: number): Page {\n+\t\tconst value = this.#storage.read(\"ordered-child-counts\", String(page)) as Page | undefined;\n+\t\tconst expected = Math.min(PAGE_SIZE, total - page * PAGE_SIZE);\n+\t\tif (\n+\t\t\t!value ||\n+\t\t\tvalue.format !== 1 ||\n+\t\t\tvalue.page !== page ||\n+\t\t\t!Array.isArray(value.counts) ||\n+\t\t\texpected < 1 ||\n+\t\t\tvalue.counts.length !== expected ||\n+\t\t\tObject.keys(value.counts).length !== expected ||\n+\t\t\tvalue.counts.some((count) => !Number.isSafeInteger(count) || count < 0) ||\n+\t\t\tvalue.digest !== digest(page, value.counts)\n+\t\t)\n+\t\t\tthrow new Error(\"invalid ordered child count page\");\n+\t\treturn value;\n+\t}\n+\t#write(page: number, counts: number[]): void {\n+\t\tthis.#storage.write(\"ordered-child-counts\", String(page), {\n+\t\t\tformat: 1,\n+\t\t\tpage,\n+\t\t\tcounts,\n+\t\t\tdigest: digest(page, counts),\n+\t\t});\n+\t}\n+\tat(index: number, total: number): number {\n+\t\tif (!Number.isSafeInteger(index) || index < 0 || index >= total)\n+\t\t\tthrow new Error(\"ordered child count out of bounds\");\n+\t\treturn this.#read(Math.floor(index / PAGE_SIZE), total).counts[index % PAGE_SIZE]!;\n+\t}\n+\tappend(total: number): void {\n+\t\tconst page = Math.floor(total / PAGE_SIZE);\n+\t\tconst offset = total % PAGE_SIZE;\n+\t\tconst counts = offset === 0 ? [] : this.#read(page, total).counts;\n+\t\tif (offset === 0 && this.#storage.read(\"ordered-child-counts\", String(page)) !== undefined)\n+\t\t\tthrow new Error(\"unexpected existing ordered child count page\");\n+\t\tcounts.push(0);\n+\t\tthis.#write(page, counts);\n+\t}\n+\tincrement(index: number, total: number): void {\n+\t\tconst count = this.at(index, total);\n+\t\tif (count === Number.MAX_SAFE_INTEGER) throw new Error(\"ordered child count exhausted\");\n+\t\tconst page = Math.floor(index / PAGE_SIZE);\n+\t\tconst counts = this.#read(page, total).counts;\n+\t\tcounts[index % PAGE_SIZE] = count + 1;\n+\t\tthis.#write(page, counts);\n+\t}\n+}"}],"unmatched":[]}