{"draft":null,"from":73,"to":318,"files":[{"path":"src/engine/ordered-index.ts","added":152,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/ordered-index.ts\n@@ -0,0 +1,152 @@\n+import type { GraphStateStorage } from \"./state-source.ts\";\n+\n+const WIDTH = 57;\n+type Leaf = { prefix: string; value: number };\n+type Branch = { prefix: string; children: (number | null)[] };\n+type Node = Leaf | Branch;\n+const safe = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;\n+\n+/** Exact existing layout priority, encoded for ascending lexical traversal. */\n+export function orderedChildKey(child: {\n+\treplaces: number;\n+\tlamport: number;\n+\tseq: number;\n+\tk: number;\n+\tn: number;\n+}): string {\n+\tif (![child.lamport, child.seq, child.k, child.n].every(safe) || ![0, 1].includes(child.replaces))\n+\t\tthrow new Error(\"invalid ordered child priority\");\n+\tconst hex = (n: number) => n.toString(16).padStart(14, \"0\");\n+\treturn `${1 - child.replaces}${hex(Number.MAX_SAFE_INTEGER - child.lamport)}${hex(Number.MAX_SAFE_INTEGER - child.seq)}${hex(child.k)}${hex(child.n)}`;\n+}\n+\n+/** Compressed radix tree with at most 16 references per node and 57 levels.\n+ * No collection scan or history-sized sibling array is needed. Values are\n+ * immutable numeric identities; the caller owns namespace isolation. */\n+export class OrderedIndex {\n+\treadonly #storage: GraphStateStorage;\n+\treadonly #collection: string;\n+\treadonly #group: string;\n+\tconstructor(storage: GraphStateStorage, collection: string, group: string) {\n+\t\tthis.#storage = storage;\n+\t\tthis.#collection = collection;\n+\t\tthis.#group = group;\n+\t}\n+\t#key(id: number | \"meta\"): string {\n+\t\treturn JSON.stringify([this.#group, id]);\n+\t}\n+\t#next(): number | undefined {\n+\t\tconst meta = this.#storage.read(this.#collection, this.#key(\"meta\")) as\n+\t\t\t| { format?: unknown; next?: unknown; count?: unknown }\n+\t\t\t| undefined;\n+\t\tif (meta === undefined) {\n+\t\t\tif (this.#storage.read(this.#collection, this.#key(0)) !== undefined)\n+\t\t\t\tthrow new Error(\"missing ordered index metadata\");\n+\t\t\treturn undefined;\n+\t\t}\n+\t\tif (\n+\t\t\tmeta?.format !== 1 ||\n+\t\t\t!safe(meta.next) ||\n+\t\t\tmeta.next < 1 ||\n+\t\t\t!safe(meta.count) ||\n+\t\t\tmeta.count < 1 ||\n+\t\t\tmeta.count > meta.next\n+\t\t)\n+\t\t\tthrow new Error(\"invalid ordered index metadata\");\n+\t\treturn meta.next;\n+\t}\n+\tget size(): number {\n+\t\tif (this.#next() === undefined) return 0;\n+\t\treturn (this.#storage.read(this.#collection, this.#key(\"meta\")) as { count: number }).count;\n+\t}\n+\t#write(id: number, node: Node): void {\n+\t\tthis.#storage.write(this.#collection, this.#key(id), node);\n+\t}\n+\t#read(id: number, expected: string): Node {\n+\t\tconst node = this.#storage.read(this.#collection, this.#key(id)) as Node | undefined;\n+\t\tif (\n+\t\t\t!node ||\n+\t\t\ttypeof node.prefix !== \"string\" ||\n+\t\t\t!/^[0-9a-f]*$/.test(node.prefix) ||\n+\t\t\tnode.prefix.length > WIDTH ||\n+\t\t\t!node.prefix.startsWith(expected)\n+\t\t)\n+\t\t\tthrow new Error(\"invalid ordered index node\");\n+\t\tif (\"value\" in node) {\n+\t\t\tif (node.prefix.length !== WIDTH || !safe(node.value) || \"children\" in node)\n+\t\t\t\tthrow new Error(\"invalid ordered index leaf\");\n+\t\t} else if (\n+\t\t\tnode.prefix.length >= WIDTH ||\n+\t\t\t!Array.isArray(node.children) ||\n+\t\t\tnode.children.length !== 16 ||\n+\t\t\t!node.children.every((id) => id === null || safe(id)) ||\n+\t\t\tnode.children.filter((id) => id !== null).length < 2\n+\t\t) {\n+\t\t\tthrow new Error(\"invalid ordered index branch\");\n+\t\t}\n+\t\treturn node;\n+\t}\n+\t/** Nested host transactions must roll back all allocation, split and link writes. */\n+\tinsert(key: string, value: number): void {\n+\t\tif (key.length !== WIDTH || !/^[0-9a-f]+$/.test(key) || !safe(value))\n+\t\t\tthrow new Error(\"invalid ordered index entry\");\n+\t\tthis.#storage.transaction(() => {\n+\t\t\tlet next = this.#next();\n+\t\t\tlet count = this.size;\n+\t\t\tif (next === undefined) {\n+\t\t\t\tthis.#write(0, { prefix: key, value });\n+\t\t\t\tthis.#storage.write(this.#collection, this.#key(\"meta\"), { format: 1, next: 1, count: 1 });\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t\tconst allocate = (node: Node): number => {\n+\t\t\t\tif (next! >= Number.MAX_SAFE_INTEGER) throw new Error(\"ordered index identity exhausted\");\n+\t\t\t\tconst id = next!++;\n+\t\t\t\tthis.#write(id, node);\n+\t\t\t\treturn id;\n+\t\t\t};\n+\t\t\tconst add = (id: number, expected: string): void => {\n+\t\t\t\tconst node = this.#read(id, expected);\n+\t\t\t\tlet common = 0;\n+\t\t\t\twhile (common < node.prefix.length && node.prefix[common] === key[common]) common++;\n+\t\t\t\tif (common < node.prefix.length) {\n+\t\t\t\t\tcount++;\n+\t\t\t\t\tconst children: (number | null)[] = Array(16).fill(null);\n+\t\t\t\t\tchildren[parseInt(node.prefix[common]!, 16)] = allocate(node);\n+\t\t\t\t\tchildren[parseInt(key[common]!, 16)] = allocate({ prefix: key, value });\n+\t\t\t\t\tthis.#write(id, { prefix: key.slice(0, common), children });\n+\t\t\t\t} else if (\"value\" in node) {\n+\t\t\t\t\tif (node.value !== value) throw new Error(\"ordered index key reused\");\n+\t\t\t\t} else {\n+\t\t\t\t\tconst digit = parseInt(key[common]!, 16);\n+\t\t\t\t\tconst child = node.children[digit]!;\n+\t\t\t\t\tif (child === null) {\n+\t\t\t\t\t\tcount++;\n+\t\t\t\t\t\tnode.children[digit] = allocate({ prefix: key, value });\n+\t\t\t\t\t\tthis.#write(id, node);\n+\t\t\t\t\t} else add(child, node.prefix + key[common]);\n+\t\t\t\t}\n+\t\t\t};\n+\t\t\tadd(0, \"\");\n+\t\t\tthis.#storage.write(this.#collection, this.#key(\"meta\"), { format: 1, next, count });\n+\t\t});\n+\t}\n+\t*values(after?: string): IterableIterator<number> {\n+\t\tif (after !== undefined && (after.length !== WIDTH || !/^[0-9a-f]+$/.test(after)))\n+\t\t\tthrow new Error(\"invalid ordered index cursor\");\n+\t\tif (this.#next() === undefined) return;\n+\t\tconst walk = function* (this: OrderedIndex, id: number, expected: string): IterableIterator<number> {\n+\t\t\tif (after !== undefined && expected < after.slice(0, expected.length)) return;\n+\t\t\tconst node = this.#read(id, expected);\n+\t\t\tif (after !== undefined && node.prefix < after.slice(0, node.prefix.length)) return;\n+\t\t\tif (\"value\" in node) {\n+\t\t\t\tif (after === undefined || node.prefix > after) yield node.value;\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t\tfor (let digit = 0; digit < 16; digit++) {\n+\t\t\t\tconst child = node.children[digit]!;\n+\t\t\t\tif (child !== null) yield* walk.call(this, child, node.prefix + digit.toString(16));\n+\t\t\t}\n+\t\t};\n+\t\tyield* walk.call(this, 0, \"\");\n+\t}\n+}"}],"unmatched":[]}