{"draft":null,"from":7,"to":318,"files":[{"path":"src/engine/experimental/projection-root.ts","added":121,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/experimental/projection-root.ts\n@@ -0,0 +1,121 @@\n+import type { GraphStateStorage } from \"../state-source.ts\";\n+\n+/** Internal experiment only. No production writer selects this format. */\n+export interface ProjectionIdentity {\n+\treadonly database: string;\n+\treadonly namespace: string;\n+\treadonly sourceGeneration: number;\n+\treadonly sourceLength: number;\n+\treadonly sourceDigest: string;\n+\treadonly membership: string;\n+\treadonly registry: string;\n+\treadonly defaultFile: string;\n+}\n+export interface ProjectionRoots {\n+\treadonly sequence: number | null;\n+\treadonly sequenceRoot: number | null;\n+\treadonly sequenceNext: number;\n+\treadonly locator: number | null;\n+\treadonly children: number | null;\n+\treadonly membership: number | null;\n+\treadonly attributes: number | null;\n+}\n+export interface ProjectionHandle {\n+\treadonly format: 1;\n+\treadonly id: number;\n+\treadonly identity: ProjectionIdentity;\n+}\n+interface RootRecord extends ProjectionHandle {\n+\treadonly roots: ProjectionRoots;\n+}\n+const fields = [\n+\t\"database\",\n+\t\"namespace\",\n+\t\"sourceGeneration\",\n+\t\"sourceLength\",\n+\t\"sourceDigest\",\n+\t\"membership\",\n+\t\"registry\",\n+\t\"defaultFile\",\n+] as const;\n+const rootFields = [\n+\t\"sequence\",\n+\t\"sequenceRoot\",\n+\t\"sequenceNext\",\n+\t\"locator\",\n+\t\"children\",\n+\t\"membership\",\n+\t\"attributes\",\n+] as const;\n+const integer = (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+function identity(value: unknown): asserts value is ProjectionIdentity {\n+\tif (!object(value) || Object.keys(value).length !== fields.length) throw Error(\"invalid projection identity\");\n+\tfor (const field of fields) {\n+\t\tconst item = value[field];\n+\t\tif (field === \"sourceGeneration\" || field === \"sourceLength\") {\n+\t\t\tif (!integer(item)) throw Error(\"invalid projection source position\");\n+\t\t} else if (typeof item !== \"string\" || item.length === 0 || item.length > 4096)\n+\t\t\tthrow Error(\"invalid projection identity field\");\n+\t}\n+\tif (!/^[a-f0-9]{64}$/.test(value.sourceDigest as string)) throw Error(\"invalid projection source digest\");\n+}\n+function roots(value: unknown): asserts value is ProjectionRoots {\n+\tif (!object(value) || Object.keys(value).length !== rootFields.length) throw Error(\"invalid projection roots\");\n+\tfor (const field of rootFields)\n+\t\tif (value[field] !== null && !integer(value[field])) throw Error(\"invalid projection page reference\");\n+\tif (\n+\t\t!integer(value.sequenceNext) ||\n+\t\tvalue.sequenceNext > 2 ** 32 ||\n+\t\t(value.sequenceRoot !== null && (!integer(value.sequenceRoot) || value.sequenceRoot >= value.sequenceNext)) ||\n+\t\t(value.sequenceRoot === null) !== (value.sequenceNext === 0) ||\n+\t\t(value.sequence === null) !== (value.sequenceRoot === null)\n+\t)\n+\t\tthrow Error(\"invalid projection sequence state\");\n+}\n+export function assertProjectionIdentity(actual: ProjectionIdentity, expected: ProjectionIdentity): void {\n+\tidentity(actual);\n+\tidentity(expected);\n+\tfor (const field of fields)\n+\t\tif (actual[field] !== expected[field]) throw Error(`projection identity mismatch: ${field}`);\n+}\n+function collection(expected: ProjectionIdentity): string {\n+\treturn `experimental-projection:${JSON.stringify([expected.database, expected.namespace])}:roots`;\n+}\n+/** Identity checks precede storage access. The host supplies trusted expected identity. */\n+export function readProjectionRoot(\n+\tstorage: GraphStateStorage,\n+\thandle: ProjectionHandle,\n+\texpected: ProjectionIdentity,\n+): ProjectionRoots {\n+\tif (!object(handle) || handle.format !== 1 || !integer(handle.id)) throw Error(\"invalid projection handle\");\n+\tassertProjectionIdentity(handle.identity, expected);\n+\tconst record = storage.read(collection(expected), String(handle.id));\n+\tif (!object(record) || record.format !== 1 || record.id !== handle.id || Object.keys(record).length !== 4)\n+\t\tthrow Error(\"invalid projection root record\");\n+\tidentity(record.identity);\n+\tassertProjectionIdentity(record.identity, expected);\n+\troots(record.roots);\n+\treturn structuredClone(record.roots);\n+}\n+/** The caller's enclosing transaction must include canonical state and its receipt. */\n+export function createProjectionRoot(\n+\tstorage: GraphStateStorage,\n+\texpected: ProjectionIdentity,\n+\tid: number,\n+\tpages: ProjectionRoots,\n+): ProjectionHandle {\n+\tidentity(expected);\n+\troots(pages);\n+\tif (!integer(id)) throw Error(\"invalid projection root id\");\n+\tconst frozenIdentity = structuredClone(expected),\n+\t\tfrozenPages = structuredClone(pages);\n+\treturn storage.transaction(() => {\n+\t\tconst name = collection(frozenIdentity);\n+\t\tif (storage.read(name, String(id)) !== undefined) throw Error(\"projection root already exists\");\n+\t\tconst record: RootRecord = { format: 1, id, identity: frozenIdentity, roots: frozenPages };\n+\t\tstorage.write(name, String(id), record);\n+\t\treturn { format: 1, id, identity: structuredClone(frozenIdentity) };\n+\t});\n+}"}],"unmatched":[]}