{"draft":null,"from":153,"to":318,"files":[{"path":"src/engine/experimental/projection-priority.ts","added":106,"removed":71,"diff":"--- a/src/engine/experimental/projection-priority.ts\n+++ b/src/engine/experimental/projection-priority.ts\n@@ -1,26 +1,15 @@\n import type { GraphStateStorage } from \"../state-source.ts\";\n \n type Page = number | null;\n-interface Leaf {\n-\tformat: 1;\n-\tkind: \"leaf\";\n-\tdepth: 57;\n-\tvalue: unknown;\n-}\n-interface Branch {\n-\tformat: 1;\n-\tkind: \"branch\";\n-\tdepth: number;\n-\tchildren: Record<string, number>;\n-}\n-type Node = Leaf | Branch;\n-const safe = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;\n-const object = (value: unknown): value is Record<string, unknown> =>\n-\tvalue !== null && typeof value === \"object\" && !Array.isArray(value);\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-/** Internal immutable priority experiment. At most 57 levels and 16 children. */\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@@ -31,85 +20,131 @@\n \t\tthis.#collection = `experimental-priority:${JSON.stringify(namespace)}`;\n \t\tthis.#decode = decode;\n \t}\n-\t#read(id: number, depth: number): Node {\n-\t\tif (!safe(id) || !safe(depth) || depth > 57) throw Error(\"invalid priority reference\");\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) || row.format !== 1 || row.depth !== depth || Object.keys(row).length !== 4)\n-\t\t\tthrow Error(\"invalid priority node\");\n-\t\tif (depth === 57) {\n-\t\t\tif (row.kind !== \"leaf\" || !(\"value\" in row)) throw Error(\"invalid priority leaf\");\n-\t\t\treturn row as unknown as Leaf;\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\" || !object(row.children)) throw Error(\"invalid priority branch\");\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 || entries.length > 16) throw Error(\"invalid priority fanout\");\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 row as unknown as Branch;\n+\t\treturn { format: 2, kind: \"branch\", prefix, children: row.children as Record<string, number> };\n \t}\n-\t#write(node: Node): number {\n-\t\tconst next = this.#storage.read(this.#collection, \"next\") ?? 0;\n-\t\tif (!safe(next) || !Number.isSafeInteger(next + 1)) throw Error(\"invalid priority allocation\");\n-\t\tif (this.#storage.read(this.#collection, String(next)) !== undefined) throw Error(\"priority allocation collision\");\n-\t\tthis.#storage.write(this.#collection, String(next), node);\n-\t\tthis.#storage.write(this.#collection, \"next\", next + 1);\n-\t\treturn next;\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.#storage.transaction(() => {\n-\t\t\tconst visit = (id: Page, depth: number): number => {\n-\t\t\t\tconst node = id === null ? undefined : this.#read(id, depth);\n-\t\t\t\tif (depth === 57) return this.#write({ format: 1, kind: \"leaf\", depth: 57, value: checked });\n-\t\t\t\tif (node?.kind === \"leaf\") throw Error(\"invalid priority depth\");\n-\t\t\t\tconst children = node?.children ?? {},\n-\t\t\t\t\tdigit = key[depth]!;\n-\t\t\t\tconst child = visit(children[digit] ?? null, depth + 1);\n-\t\t\t\treturn this.#write({ format: 1, kind: \"branch\", depth, children: { ...children, [digit]: child } });\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, 0);\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.#storage.transaction(() => {\n-\t\t\tconst visit = (id: Page, depth: number): Page => {\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, depth);\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[depth]!,\n-\t\t\t\t\tchild = visit(node.children[digit] ?? null, depth + 1),\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\treturn Object.keys(children).length ? this.#write({ ...node, children }) : null;\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, 0);\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\tlet current = root;\n-\t\tlet candidate: { id: number; depth: number } | undefined;\n-\t\tfor (let depth = 0; current !== null && depth < 57; depth++) {\n-\t\t\tconst node = this.#read(current, depth);\n-\t\t\tif (node.kind !== \"branch\") throw Error(\"invalid priority depth\");\n-\t\t\tconst digit = key[depth]!,\n-\t\t\t\tlower = Object.keys(node.children)\n-\t\t\t\t\t.filter((item) => item < digit)\n-\t\t\t\t\t.sort()\n-\t\t\t\t\t.at(-1);\n-\t\t\tif (lower !== undefined) candidate = { id: node.children[lower]!, depth: depth + 1 };\n-\t\t\tcurrent = node.children[digit] ?? null;\n-\t\t}\n-\t\tif (!candidate) return undefined;\n-\t\tlet { id, depth } = candidate;\n-\t\tfor (; depth <= 57; depth++) {\n-\t\t\tconst node = this.#read(id, depth);\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\tid = node.children[Object.keys(node.children).sort().at(-1)!]!;\n-\t\t}\n-\t\tthrow Error(\"invalid priority depth\");\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":[]}