{"draft":null,"from":4,"to":318,"files":[{"path":"src/engine/experimental/projection-priority.ts","added":150,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/experimental/projection-priority.ts\n@@ -0,0 +1,150 @@\n+import type { GraphStateStorage } from \"../state-source.ts\";\n+\n+type Page = number | null;\n+type Node =\n+\t| { format: 2; kind: \"leaf\"; prefix: string; value: unknown }\n+\t| { format: 2; kind: \"branch\"; prefix: string; children: Record<string, number> };\n+const safe = (v: unknown): v is number => Number.isSafeInteger(v) && Number(v) >= 0;\n+const object = (v: unknown): v is Record<string, unknown> => v !== null && typeof v === \"object\" && !Array.isArray(v);\n+function check(root: Page, key: string): void {\n+\tif ((root !== null && !safe(root)) || !/^[0-9a-f]{57}$/.test(key)) throw Error(\"invalid priority lookup\");\n+}\n+/** Immutable compressed priority paths. Reads legacy depth pages; writes format 2. */\n+export class ProjectionPriority<T> {\n+\treadonly #storage: GraphStateStorage;\n+\treadonly #collection: string;\n+\treadonly #decode: (value: unknown) => T;\n+\tconstructor(storage: GraphStateStorage, namespace: string, decode: (value: unknown) => T) {\n+\t\tif (!namespace || namespace.length > 4096) throw Error(\"invalid priority namespace\");\n+\t\tthis.#storage = storage;\n+\t\tthis.#collection = `experimental-priority:${JSON.stringify(namespace)}`;\n+\t\tthis.#decode = decode;\n+\t}\n+\t#read(id: number, expected: string): Node {\n+\t\tif (!safe(id) || expected.length > 57) throw Error(\"invalid priority reference\");\n+\t\tconst row = this.#storage.read(this.#collection, String(id));\n+\t\tif (!object(row) || Object.keys(row).length !== 4) throw Error(\"invalid priority node\");\n+\t\tconst legacy = row.format === 1;\n+\t\tconst prefix = legacy ? expected : row.prefix;\n+\t\tif (\n+\t\t\t(legacy ? row.depth !== expected.length : row.format !== 2) ||\n+\t\t\ttypeof prefix !== \"string\" ||\n+\t\t\t!/^[0-9a-f]{0,57}$/.test(prefix) ||\n+\t\t\t!prefix.startsWith(expected)\n+\t\t)\n+\t\t\tthrow Error(\"invalid priority prefix\");\n+\t\tif (row.kind === \"leaf\") {\n+\t\t\tif (prefix.length !== 57 || !(\"value\" in row)) throw Error(\"invalid priority leaf\");\n+\t\t\treturn { format: 2, kind: \"leaf\", prefix, value: row.value };\n+\t\t}\n+\t\tif (row.kind !== \"branch\" || prefix.length >= 57 || !object(row.children)) throw Error(\"invalid priority branch\");\n+\t\tconst entries = Object.entries(row.children);\n+\t\tif (entries.length < (legacy ? 1 : 2) || entries.length > 16) throw Error(\"invalid priority fanout\");\n+\t\tfor (const [digit, child] of entries)\n+\t\t\tif (!/^[0-9a-f]$/.test(digit) || !safe(child) || child === id) throw Error(\"invalid priority child\");\n+\t\treturn { format: 2, kind: \"branch\", prefix, children: row.children as Record<string, number> };\n+\t}\n+\t#allocate<R>(body: (write: (node: Node) => number) => R): R {\n+\t\treturn this.#storage.transaction(() => {\n+\t\t\tlet next = this.#storage.read(this.#collection, \"next\") ?? 0;\n+\t\t\tif (!safe(next)) throw Error(\"invalid priority allocation\");\n+\t\t\tconst initial = next;\n+\t\t\tconst result = body((node) => {\n+\t\t\t\t// Collapse legacy singleton paths before storing a compressed page.\n+\t\t\t\twhile (node.kind === \"branch\" && Object.keys(node.children).length === 1) {\n+\t\t\t\t\tconst [digit, child] = Object.entries(node.children)[0]!;\n+\t\t\t\t\tnode = this.#read(child, node.prefix + digit);\n+\t\t\t\t}\n+\t\t\t\tif (!safe(next) || !Number.isSafeInteger(next + 1)) throw Error(\"invalid priority allocation\");\n+\t\t\t\tif (this.#storage.read(this.#collection, String(next)) !== undefined)\n+\t\t\t\t\tthrow Error(\"priority allocation collision\");\n+\t\t\t\tconst id = next++;\n+\t\t\t\tthis.#storage.write(this.#collection, String(id), node);\n+\t\t\t\treturn id;\n+\t\t\t});\n+\t\t\tif (next !== initial) this.#storage.write(this.#collection, \"next\", next);\n+\t\t\treturn result;\n+\t\t});\n+\t}\n+\n+\tinsert(root: Page, key: string, value: T): number {\n+\t\tcheck(root, key);\n+\t\tconst checked = structuredClone(this.#decode(structuredClone(value)));\n+\t\treturn this.#allocate((write) => {\n+\t\t\tconst leaf = () => write({ format: 2, kind: \"leaf\", prefix: key, value: checked });\n+\t\t\tconst visit = (id: Page, expected: string): number => {\n+\t\t\t\tif (id === null) return leaf();\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\t// Legacy pages need their inferred prefix persisted when moved below a new branch.\n+\t\t\t\t\tconst old = write(node),\n+\t\t\t\t\t\tadded = leaf();\n+\t\t\t\t\treturn write({\n+\t\t\t\t\t\tformat: 2,\n+\t\t\t\t\t\tkind: \"branch\",\n+\t\t\t\t\t\tprefix: key.slice(0, common),\n+\t\t\t\t\t\tchildren: { [node.prefix[common]!]: old, [key[common]!]: added },\n+\t\t\t\t\t});\n+\t\t\t\t}\n+\t\t\t\tif (node.kind === \"leaf\") return leaf();\n+\t\t\t\tconst digit = key[node.prefix.length]!;\n+\t\t\t\tconst child = visit(node.children[digit] ?? null, node.prefix + digit);\n+\t\t\t\treturn write({ ...node, children: { ...node.children, [digit]: child } });\n+\t\t\t};\n+\t\t\treturn visit(root, \"\");\n+\t\t});\n+\t}\n+\tremove(root: Page, key: string): Page {\n+\t\tcheck(root, key);\n+\t\treturn this.#allocate((write) => {\n+\t\t\tconst visit = (id: Page, expected: string): Page => {\n+\t\t\t\tif (id === null) throw Error(\"missing priority key\");\n+\t\t\t\tconst node = this.#read(id, expected);\n+\t\t\t\tif (!key.startsWith(node.prefix)) throw Error(\"missing priority key\");\n+\t\t\t\tif (node.kind === \"leaf\") return null;\n+\t\t\t\tconst digit = key[node.prefix.length]!,\n+\t\t\t\t\tchild = visit(node.children[digit] ?? null, node.prefix + digit),\n+\t\t\t\t\tchildren = { ...node.children };\n+\t\t\t\tif (child === null) delete children[digit];\n+\t\t\t\telse children[digit] = child;\n+\t\t\t\tconst entries = Object.entries(children);\n+\t\t\t\tif (!entries.length) return null;\n+\t\t\t\tif (entries.length === 1) {\n+\t\t\t\t\tconst [d, c] = entries[0]!;\n+\t\t\t\t\treturn write(this.#read(c, node.prefix + d));\n+\t\t\t\t}\n+\t\t\t\treturn write({ ...node, children });\n+\t\t\t};\n+\t\t\treturn visit(root, \"\");\n+\t\t});\n+\t}\n+\tbefore(root: Page, key: string): T | undefined {\n+\t\tcheck(root, key);\n+\t\tconst maximum = (id: number, expected: string): T => {\n+\t\t\tconst node = this.#read(id, expected);\n+\t\t\tif (node.kind === \"leaf\") return this.#decode(structuredClone(node.value));\n+\t\t\tconst digit = Object.keys(node.children).sort().at(-1)!;\n+\t\t\treturn maximum(node.children[digit]!, node.prefix + digit);\n+\t\t};\n+\t\tconst visit = (id: Page, expected: string): T | undefined => {\n+\t\t\tif (id === null) return undefined;\n+\t\t\tconst node = this.#read(id, expected);\n+\t\t\tif (!key.startsWith(node.prefix)) return node.prefix < key ? maximum(id, expected) : undefined;\n+\t\t\tif (node.kind === \"leaf\") return undefined;\n+\t\t\tconst digit = key[node.prefix.length]!;\n+\t\t\tconst same = node.children[digit];\n+\t\t\tif (same !== undefined) {\n+\t\t\t\tconst found = visit(same, node.prefix + digit);\n+\t\t\t\tif (found !== undefined) return found;\n+\t\t\t}\n+\t\t\tconst lower = Object.keys(node.children)\n+\t\t\t\t.filter((d) => d < digit)\n+\t\t\t\t.sort()\n+\t\t\t\t.at(-1);\n+\t\t\treturn lower === undefined ? undefined : maximum(node.children[lower]!, node.prefix + lower);\n+\t\t};\n+\t\treturn visit(root, \"\");\n+\t}\n+}"}],"unmatched":[]}