{"draft":null,"from":3,"to":318,"files":[{"path":"server/src/projection-store.ts","added":464,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/projection-store.ts\n@@ -0,0 +1,464 @@\n+import { ProjectionAtomAdoption } from \"../../src/engine/experimental/projection-atom-adoption.ts\";\n+import { ProjectionAtomIndex } from \"../../src/engine/experimental/projection-atom-index.ts\";\n+import { ExperimentalMarkerProjection, type MarkerState } from \"../../src/engine/experimental/projection-marker.ts\";\n+import { ProjectionOverlayStorage } from \"../../src/engine/experimental/projection-overlay-storage.ts\";\n+import { ProjectionReadScope } from \"../../src/engine/experimental/projection-read-scope.ts\";\n+import {\n+\tassertProjectionIdentity,\n+\ttype ProjectionHandle,\n+\ttype ProjectionIdentity,\n+} from \"../../src/engine/experimental/projection-root.ts\";\n+import { ProjectionSnapshots } from \"../../src/engine/experimental/projection-snapshots.ts\";\n+import { Graph } from \"../../src/engine/graph.ts\";\n+import { CachedStateStorage, type GraphStateStorage } from \"../../src/engine/state-source.ts\";\n+import { canonicalJson } from \"../../worker/project/operation-protocol.ts\";\n+import { projectedReader } from \"../../worker/project/projection-provider.ts\";\n+import { Membership, type Published } from \"../../worker/project/spaces.ts\";\n+import {\n+\tadvanceWriterRoot,\n+\ttype SqliteWriterState,\n+\ttype WriterProjectionHost,\n+\ttype WriterProjectionSession,\n+\ttype WriterRoot,\n+} from \"./writer-host.ts\";\n+\n+type Context = Pick<ProjectionIdentity, \"database\" | \"namespace\" | \"registry\" | \"defaultFile\">;\n+interface Cursor {\n+\tsource: WriterRoot;\n+\tversion: number;\n+\toffset: number;\n+\tordered: boolean;\n+\tstate: MarkerState;\n+\tready: boolean;\n+}\n+interface Head {\n+\tversion: number;\n+\thandle: ProjectionHandle;\n+\tsource: WriterRoot;\n+}\n+const integer = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;\n+function shape(value: unknown, fields: string): asserts value is Record<string, unknown> {\n+\tconst keys = fields.split(\" \");\n+\tif (\n+\t\t!value ||\n+\t\ttypeof value !== \"object\" ||\n+\t\tArray.isArray(value) ||\n+\t\tObject.keys(value).length !== keys.length ||\n+\t\tkeys.some((key) => !Object.hasOwn(value, key))\n+\t)\n+\t\tthrow Error(\"invalid projection host record\");\n+}\n+function source(value: unknown): asserts value is WriterRoot {\n+\tshape(value, \"format generation logLength sharedVersion digest\");\n+\tif (\n+\t\tvalue.format !== 1 ||\n+\t\t!integer(value.generation) ||\n+\t\t!integer(value.logLength) ||\n+\t\t!integer(value.sharedVersion) ||\n+\t\ttypeof value.digest !== \"string\" ||\n+\t\t!/^[a-f0-9]{64}$/.test(value.digest)\n+\t)\n+\t\tthrow Error(\"invalid projection source root\");\n+}\n+\n+/** Private format experiment. Constructor injection only; production has no selection path. */\n+export class SqliteProjectionStore implements WriterProjectionHost {\n+\treadonly #context: Context;\n+\treadonly #generation: number;\n+\treadonly #indexedAtoms: boolean;\n+\treadonly #meta: string;\n+\t#active = false;\n+\t#nextOverlay = 0;\n+\tconstructor(context: Context, namespaceGeneration: number, options: { indexedAtoms?: boolean } = {}) {\n+\t\tif (!Number.isSafeInteger(namespaceGeneration) || namespaceGeneration < 0)\n+\t\t\tthrow Error(\"invalid projection namespace generation\");\n+\t\tthis.#context = Object.freeze({ ...context });\n+\t\tthis.#generation = namespaceGeneration;\n+\t\tthis.#indexedAtoms = options.indexedAtoms ?? false;\n+\t\tthis.#meta = `projection-host:${JSON.stringify(context.namespace)}`;\n+\t}\n+\t#bind(state: SqliteWriterState): void {\n+\t\tconst mode = state.read(\"host\", \"projection-atom-index-mode\");\n+\t\tif (this.#indexedAtoms ? mode !== 1 : mode === 1) throw Error(\"projection atom indexing mode mismatch\");\n+\t\tconst expected = {\n+\t\t\t...this.#context,\n+\t\t\tgeneration: this.#generation,\n+\t\t\t...(this.#indexedAtoms ? { atomIndexFormat: 1 } : {}),\n+\t\t};\n+\t\tconst found = state.read(this.#meta, \"binding\");\n+\t\tif (found !== undefined && canonicalJson(found) !== canonicalJson(expected))\n+\t\t\tthrow Error(\"projection namespace identity mismatch\");\n+\t\tif (found === undefined) state.write(this.#meta, \"binding\", expected);\n+\t}\n+\t#firstPending(state: SqliteWriterState, collection: string): readonly [string, unknown] | undefined {\n+\t\tconst iterator = state.entries(collection)[Symbol.iterator]();\n+\t\ttry {\n+\t\t\tconst next = iterator.next();\n+\t\t\treturn next.done ? undefined : next.value;\n+\t\t} finally {\n+\t\t\titerator.return?.();\n+\t\t}\n+\t}\n+\t#identity(source: WriterRoot, version: number): ProjectionIdentity {\n+\t\treturn {\n+\t\t\t...this.#context,\n+\t\t\tsourceGeneration: this.#generation,\n+\t\t\tsourceLength: source.logLength,\n+\t\t\tsourceDigest: source.digest,\n+\t\t\tmembership: String(version),\n+\t\t};\n+\t}\n+\t#cursor(value: unknown): Cursor | undefined {\n+\t\tif (value === undefined) return undefined;\n+\t\tshape(value, \"source version offset ordered state ready\");\n+\t\tsource(value.source);\n+\t\tif (\n+\t\t\t!integer(value.version) ||\n+\t\t\tvalue.version > value.source.sharedVersion + 1 ||\n+\t\t\t!integer(value.offset) ||\n+\t\t\ttypeof value.ordered !== \"boolean\" ||\n+\t\t\ttypeof value.ready !== \"boolean\" ||\n+\t\t\tvalue.ready !== (value.version === value.source.sharedVersion + 1) ||\n+\t\t\t(value.ready && (value.offset !== 0 || value.ordered))\n+\t\t)\n+\t\t\tthrow Error(\"invalid projection adoption cursor\");\n+\t\tshape(value.state, \"context sequence locator membership children attributeFiles\");\n+\t\tif (value.state.context !== JSON.stringify([this.#context.namespace, this.#context.defaultFile]))\n+\t\t\tthrow Error(\"projection adoption context mismatch\");\n+\t\tfor (const key of [\"locator\", \"membership\", \"children\", \"attributeFiles\"])\n+\t\t\tif (value.state[key] !== null && !integer(value.state[key])) throw Error(\"invalid adoption page reference\");\n+\t\tshape(value.state.sequence, \"map root next\");\n+\t\tconst seq = value.state.sequence;\n+\t\tif (\n+\t\t\t!integer(seq.next) ||\n+\t\t\tseq.next > 2 ** 32 ||\n+\t\t\t(seq.map !== null && !integer(seq.map)) ||\n+\t\t\t(seq.root !== null && (!integer(seq.root) || seq.root >= seq.next)) ||\n+\t\t\t(seq.root === null) !== (seq.next === 0) ||\n+\t\t\t(seq.map === null) !== (seq.root === null)\n+\t\t)\n+\t\t\tthrow Error(\"invalid adoption sequence state\");\n+\t\treturn value as unknown as Cursor;\n+\t}\n+\t#head(value: unknown, expected: WriterRoot): Head | undefined {\n+\t\tif (value === undefined) return undefined;\n+\t\tshape(value, \"version handle source\");\n+\t\tsource(value.source);\n+\t\tif (\n+\t\t\t!integer(value.version) ||\n+\t\t\tvalue.version !== expected.sharedVersion ||\n+\t\t\tcanonicalJson(value.source) !== canonicalJson(expected)\n+\t\t)\n+\t\t\tthrow Error(\"projection head is stale\");\n+\t\tshape(value.handle, \"format id identity\");\n+\t\tif (value.handle.format !== 1 || !integer(value.handle.id)) throw Error(\"invalid projection head handle\");\n+\t\tassertProjectionIdentity(value.handle.identity as ProjectionIdentity, this.#identity(expected, value.version));\n+\t\treturn value as unknown as Head;\n+\t}\n+\t#save(\n+\t\tstate: SqliteWriterState,\n+\t\tsnapshots: ProjectionSnapshots,\n+\t\tvalue: MarkerState,\n+\t\tsource: WriterRoot,\n+\t\tversion: number,\n+\t): ProjectionHandle {\n+\t\tconst stored = state.read(this.#meta, \"next-root\");\n+\t\tconst next = stored === undefined ? 0 : stored;\n+\t\tif (!integer(next) || !Number.isSafeInteger(next + 1)) throw Error(\"invalid projection root allocation\");\n+\t\tconst handle = snapshots.save(next, this.#identity(source, version), value);\n+\t\tstate.write(this.#meta, \"next-root\", next + 1);\n+\t\tstate.write(`${this.#meta}:versions`, String(version), handle);\n+\t\tstate.write(`${this.#meta}:prefixes`, String(source.logLength), source.digest);\n+\t\tstate.write(this.#meta, \"head\", { version, handle, source } satisfies Head);\n+\t\treturn handle;\n+\t}\n+\t#atoms(graph: Graph, delta: Published): { id: string; remove: boolean }[] {\n+\t\treturn [\n+\t\t\t...[...(delta.dropped ?? [])]\n+\t\t\t\t.sort((a, b) => graph.atom(b).seq - graph.atom(a).seq)\n+\t\t\t\t.map((id) => ({ id, remove: true })),\n+\t\t\t...[...delta.atoms].sort((a, b) => graph.atom(a).seq - graph.atom(b).seq).map((id) => ({ id, remove: false })),\n+\t\t];\n+\t}\n+\t/** Each page is bounded in atoms and completed versions; the writer root is rechecked before commit. */\n+\tadopt(input: {\n+\t\tstate: SqliteWriterState;\n+\t\texpected: WriterRoot;\n+\t\tcurrentRoot(): WriterRoot;\n+\t\tmaxAtoms: number;\n+\t\tmaxVersions: number;\n+\t\tmaxSteps?: number;\n+\t}): { ready: boolean; version: number; applied: number; prepared: number; steps: number; atomicMoveWork: boolean } {\n+\t\tif (this.#active) throw Error(\"projection writer is active\");\n+\t\tsource(input.expected);\n+\t\tconst maxSteps = input.maxSteps ?? 64;\n+\t\tif (![input.maxAtoms, input.maxVersions, maxSteps].every((n) => Number.isSafeInteger(n) && n > 0 && n <= 256))\n+\t\t\tthrow Error(\"invalid projection adoption bounds\");\n+\t\treturn input.state.transaction(() => {\n+\t\t\tconst check = () => {\n+\t\t\t\tif (canonicalJson(input.currentRoot()) !== canonicalJson(input.expected))\n+\t\t\t\t\tthrow Error(\"projection adoption source changed\");\n+\t\t\t};\n+\t\t\tcheck();\n+\t\t\tthis.#bind(input.state);\n+\t\t\t// One bounded cache per adoption page; no cache survives a return or rollback.\n+\t\t\tconst pageStorage = new CachedStateStorage(input.state, 262_144, 2048);\n+\t\t\tconst graph = new Graph({ storage: pageStorage });\n+\t\t\tconst atomIndex = this.#indexedAtoms ? new ProjectionAtomIndex(pageStorage) : undefined;\n+\t\t\tconst atomSeq = (id: string) => {\n+\t\t\t\tif (!graph.has(id)) throw Error(\"missing projection atom\");\n+\t\t\t\treturn Number(id.slice(1));\n+\t\t\t};\n+\t\t\tconst adapter = new ExperimentalMarkerProjection(\n+\t\t\t\tpageStorage,\n+\t\t\t\tgraph,\n+\t\t\t\tthis.#context.defaultFile,\n+\t\t\t\tthis.#context.namespace,\n+\t\t\t);\n+\t\t\tconst snapshots = new ProjectionSnapshots(pageStorage, adapter, this.#context);\n+\t\t\tconst versions = new Membership(pageStorage).shared.versions;\n+\t\t\tlet cursor = this.#cursor(input.state.read(this.#meta, \"adoption\"));\n+\t\t\tif (cursor && canonicalJson(cursor.source) !== canonicalJson(input.expected))\n+\t\t\t\tthrow Error(\"projection adoption cursor changed\");\n+\t\t\tcursor ??= {\n+\t\t\t\tsource: input.expected,\n+\t\t\t\tversion: 0,\n+\t\t\t\toffset: 0,\n+\t\t\t\tordered: false,\n+\t\t\t\tstate: adapter.empty(),\n+\t\t\t\tready: false,\n+\t\t\t};\n+\t\t\tlet applied = 0,\n+\t\t\t\tprepared = 0,\n+\t\t\t\tcompleted = 0,\n+\t\t\t\tsteps = 0,\n+\t\t\t\tatomicMoveWork = false;\n+\t\t\tconst pending = `${this.#meta}:adoption-order`;\n+\t\t\twhile (\n+\t\t\t\t!cursor.ready &&\n+\t\t\t\tapplied + prepared < input.maxAtoms &&\n+\t\t\t\tcompleted < input.maxVersions &&\n+\t\t\t\tsteps < maxSteps\n+\t\t\t) {\n+\t\t\t\tconst delta = versions[cursor.version];\n+\t\t\t\tif (!delta || delta.version !== cursor.version) throw Error(\"missing projection adoption version\");\n+\t\t\t\tconst drops = delta.dropped ?? [];\n+\t\t\t\tconst count = drops.length + delta.atoms.length;\n+\t\t\t\tif (cursor.offset > count || (cursor.ordered && cursor.offset !== count))\n+\t\t\t\t\tthrow Error(\"invalid projection adoption offset\");\n+\t\t\t\twhile (!cursor.ordered && cursor.offset < count && applied + prepared < input.maxAtoms) {\n+\t\t\t\t\tconst remove = cursor.offset < drops.length;\n+\t\t\t\t\tconst id = remove ? drops[cursor.offset]! : delta.atoms[cursor.offset - drops.length]!;\n+\t\t\t\t\tconst seq = atomSeq(id);\n+\t\t\t\t\tif (!Number.isSafeInteger(seq) || seq < 0) throw Error(\"invalid adoption atom sequence\");\n+\t\t\t\t\tconst rank = remove ? Number.MAX_SAFE_INTEGER - seq : seq;\n+\t\t\t\t\tconst key = `${remove ? \"0\" : \"1\"}:${rank.toString(16).padStart(14, \"0\")}`;\n+\t\t\t\t\tinput.state.write(pending, key, { id, remove, version: cursor.version, index: cursor.offset });\n+\t\t\t\t\tcursor.offset++;\n+\t\t\t\t\tprepared++;\n+\t\t\t\t}\n+\t\t\t\tif (cursor.offset === count) cursor.ordered = true;\n+\t\t\t\tif (!cursor.ordered) break;\n+\t\t\t\twhile (applied + prepared < input.maxAtoms && steps < maxSteps) {\n+\t\t\t\t\tconst next = this.#firstPending(input.state, pending);\n+\t\t\t\t\tif (!next) break;\n+\t\t\t\t\tconst [key, value] = next;\n+\t\t\t\t\tshape(value, \"id remove version index\");\n+\t\t\t\t\tif (\n+\t\t\t\t\t\ttypeof value.id !== \"string\" ||\n+\t\t\t\t\t\t!value.id.length ||\n+\t\t\t\t\t\tvalue.id.length > 4096 ||\n+\t\t\t\t\t\ttypeof value.remove !== \"boolean\" ||\n+\t\t\t\t\t\tvalue.version !== cursor.version ||\n+\t\t\t\t\t\t!integer(value.index) ||\n+\t\t\t\t\t\tvalue.index >= count ||\n+\t\t\t\t\t\tvalue.remove !== value.index < drops.length ||\n+\t\t\t\t\t\tvalue.id !== (value.remove ? drops[value.index] : delta.atoms[value.index - drops.length])\n+\t\t\t\t\t)\n+\t\t\t\t\t\tthrow Error(\"invalid projection pending atom\");\n+\t\t\t\t\tconst item = value as { id: string; remove: boolean };\n+\t\t\t\t\tconst seq = atomSeq(item.id);\n+\t\t\t\t\tconst rank = item.remove ? Number.MAX_SAFE_INTEGER - seq : seq;\n+\t\t\t\t\tif (key !== `${item.remove ? \"0\" : \"1\"}:${rank.toString(16).padStart(14, \"0\")}`)\n+\t\t\t\t\t\tthrow Error(\"projection pending order mismatch\");\n+\t\t\t\t\tconst task = `${cursor.version}:${key}`;\n+\t\t\t\t\tconst page = new ProjectionAtomAdoption(adapter, task).page({\n+\t\t\t\t\t\tprevious: cursor.state,\n+\t\t\t\t\t\tatom: atomIndex ? atomIndex.openAccepted(item.id) : graph.atom(item.id),\n+\t\t\t\t\t\tremove: item.remove,\n+\t\t\t\t\t\tsource: input.expected.digest,\n+\t\t\t\t\t\tmaxSteps: maxSteps - steps,\n+\t\t\t\t\t\tvalidateSource: check,\n+\t\t\t\t\t});\n+\t\t\t\t\tsteps += page.steps;\n+\t\t\t\t\tatomicMoveWork ||= page.atomicMoveWork;\n+\t\t\t\t\tif (!page.done) break;\n+\t\t\t\t\tcursor.state = page.state;\n+\t\t\t\t\tadapter.storage.remove(`atom-adoption:${task}`, \"cursor\");\n+\t\t\t\t\tinput.state.remove(pending, key);\n+\t\t\t\t\tapplied++;\n+\t\t\t\t}\n+\t\t\t\tif (!this.#firstPending(input.state, pending)) {\n+\t\t\t\t\tthis.#save(input.state, snapshots, cursor.state, input.expected, cursor.version);\n+\t\t\t\t\tcursor.version++;\n+\t\t\t\t\tcursor.offset = 0;\n+\t\t\t\t\tcursor.ordered = false;\n+\t\t\t\t\tcompleted++;\n+\t\t\t\t\tcursor.ready = cursor.version > input.expected.sharedVersion;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tcheck();\n+\t\t\tinput.state.write(this.#meta, \"adoption\", cursor);\n+\t\t\treturn { ready: cursor.ready, version: cursor.version - 1, applied, prepared, steps, atomicMoveWork };\n+\t\t});\n+\t}\n+\topen({ state, log, root }: Parameters<WriterProjectionHost[\"open\"]>[0]): WriterProjectionSession {\n+\t\tif (this.#active) throw Error(\"nested projection writers are not supported\");\n+\t\tthis.#bind(state);\n+\t\tsource(root);\n+\t\tconst adoption = this.#cursor(state.read(this.#meta, \"adoption\"));\n+\t\tconst head = this.#head(state.read(this.#meta, \"head\"), root);\n+\t\tif (!adoption?.ready || !head || canonicalJson(head.source) !== canonicalJson(root))\n+\t\t\tthrow Error(\"projection head is missing, incomplete or stale\");\n+\t\tthis.#active = true;\n+\t\tlet live = true,\n+\t\t\tobserved = root;\n+\t\tconst token = {};\n+\t\tconst overlays: ProjectionOverlayStorage[] = [];\n+\t\tconst scopes: ProjectionReadScope[] = [];\n+\t\tconst membership = new Membership(state);\n+\t\tconst versions = membership.shared.versions;\n+\t\tconst current = (): WriterRoot => {\n+\t\t\tif (!live) throw Error(\"closed projection writer\");\n+\t\t\tif (log.seq() === observed.logLength && log.generation() !== observed.generation)\n+\t\t\t\tthrow Error(\"projection source changed without an append\");\n+\t\t\tif (log.seq() !== observed.logLength)\n+\t\t\t\tobserved = advanceWriterRoot(log, observed, {\n+\t\t\t\t\tgeneration: log.generation(),\n+\t\t\t\t\tlogLength: log.seq(),\n+\t\t\t\t\tsharedVersion: versions.length - 1,\n+\t\t\t\t});\n+\t\t\treturn observed;\n+\t\t};\n+\t\tconst provider =\n+\t\t\t(membershipState: GraphStateStorage): WriterProjectionSession[\"readViewProvider\"] =>\n+\t\t\t(graph) => {\n+\t\t\t\tconst membership = new Membership(membershipState);\n+\t\t\t\tconst versions = membership.shared.versions;\n+\t\t\t\tconst scope = new ProjectionReadScope(graph, () => (live ? token : undefined));\n+\t\t\t\tscopes.push(scope);\n+\t\t\t\tconst readers = new Map<string, ReturnType<typeof projectedReader>>();\n+\t\t\t\tlet identity = \"\";\n+\t\t\t\tconst reuse = (key: string, build: () => ReturnType<typeof projectedReader>) => {\n+\t\t\t\t\tconst source = current();\n+\t\t\t\t\tconst next = JSON.stringify([\n+\t\t\t\t\t\tgraph.size,\n+\t\t\t\t\t\tsource.generation,\n+\t\t\t\t\t\tsource.logLength,\n+\t\t\t\t\t\tsource.digest,\n+\t\t\t\t\t\tversions.length,\n+\t\t\t\t\t]);\n+\t\t\t\t\tif (next !== identity) {\n+\t\t\t\t\t\treaders.clear();\n+\t\t\t\t\t\tidentity = next;\n+\t\t\t\t\t}\n+\t\t\t\t\tconst cacheable = key.length <= 8192 && /^(?:v|a) @/.test(key);\n+\t\t\t\t\tconst cached = cacheable ? readers.get(key) : undefined;\n+\t\t\t\t\tif (cached) {\n+\t\t\t\t\t\tscope.guard(graph)();\n+\t\t\t\t\t\treturn cached;\n+\t\t\t\t\t}\n+\t\t\t\t\tconst reader = build();\n+\t\t\t\t\tif (cacheable) {\n+\t\t\t\t\t\tif (readers.size >= 32) readers.delete(readers.keys().next().value!);\n+\t\t\t\t\t\treaders.set(key, reader);\n+\t\t\t\t\t}\n+\t\t\t\t\treturn reader;\n+\t\t\t\t};\n+\t\t\t\treturn {\n+\t\t\t\t\tkeyed: (key) =>\n+\t\t\t\t\t\treuse(key, () =>\n+\t\t\t\t\t\t\tprojectedReader(\n+\t\t\t\t\t\t\t\t{\n+\t\t\t\t\t\t\t\t\tgraph,\n+\t\t\t\t\t\t\t\t\tcontext: this.#context,\n+\t\t\t\t\t\t\t\t\tsourceGeneration: this.#generation,\n+\t\t\t\t\t\t\t\t\tsourceLength: () => current().logLength,\n+\t\t\t\t\t\t\t\t\tprefixDigest: (length) =>\n+\t\t\t\t\t\t\t\t\t\tlength === current().logLength\n+\t\t\t\t\t\t\t\t\t\t\t? current().digest\n+\t\t\t\t\t\t\t\t\t\t\t: (state.read(`${this.#meta}:prefixes`, String(length)) as string | undefined),\n+\t\t\t\t\t\t\t\t\tversion: () => versions.length - 1,\n+\t\t\t\t\t\t\t\t\tcommitted: () => head,\n+\t\t\t\t\t\t\t\t\tat: (version) =>\n+\t\t\t\t\t\t\t\t\t\tstate.read(`${this.#meta}:versions`, String(version)) as ProjectionHandle | undefined,\n+\t\t\t\t\t\t\t\t\tdelta: (version) => versions[version],\n+\t\t\t\t\t\t\t\t\tnamedVersion: (name, version) => {\n+\t\t\t\t\t\t\t\t\t\tconst space = membership.spaces.get(name);\n+\t\t\t\t\t\t\t\t\t\tconst selected = space?.versions[version];\n+\t\t\t\t\t\t\t\t\t\tif (!space || selected?.sharedBase === undefined) return undefined;\n+\t\t\t\t\t\t\t\t\t\treturn {\n+\t\t\t\t\t\t\t\t\t\t\tsharedBase: selected.sharedBase,\n+\t\t\t\t\t\t\t\t\t\t\tatoms: (function* () {\n+\t\t\t\t\t\t\t\t\t\t\t\tfor (let at = 0; at <= version; at++) {\n+\t\t\t\t\t\t\t\t\t\t\t\t\tconst delta = space.versions[at]!;\n+\t\t\t\t\t\t\t\t\t\t\t\t\tif (delta.dropped?.length) throw Error(\"unexpected named draft removal\");\n+\t\t\t\t\t\t\t\t\t\t\t\t\tyield* delta.atoms;\n+\t\t\t\t\t\t\t\t\t\t\t\t}\n+\t\t\t\t\t\t\t\t\t\t\t})(),\n+\t\t\t\t\t\t\t\t\t\t};\n+\t\t\t\t\t\t\t\t\t},\n+\t\t\t\t\t\t\t\t\tguard: () => scope.guard(graph),\n+\t\t\t\t\t\t\t\t\tallocate: () => {\n+\t\t\t\t\t\t\t\t\t\tconst overlay = new ProjectionOverlayStorage(\n+\t\t\t\t\t\t\t\t\t\t\tstate,\n+\t\t\t\t\t\t\t\t\t\t\t`${this.#context.namespace}:${this.#nextOverlay++}`,\n+\t\t\t\t\t\t\t\t\t\t\t(collection) => state.clearProjectionOverlay(collection),\n+\t\t\t\t\t\t\t\t\t\t);\n+\t\t\t\t\t\t\t\t\t\toverlays.push(overlay);\n+\t\t\t\t\t\t\t\t\t\treturn new CachedStateStorage(overlay, overlays.length <= 32 ? 65_536 : 0, 256);\n+\t\t\t\t\t\t\t\t\t},\n+\t\t\t\t\t\t\t\t},\n+\t\t\t\t\t\t\t\tkey,\n+\t\t\t\t\t\t\t),\n+\t\t\t\t\t\t),\n+\t\t\t\t};\n+\t\t\t};\n+\t\treturn {\n+\t\t\treadViewProvider: provider(state),\n+\t\t\thistoricalReadViewProvider: provider,\n+\t\t\tcommit: (project, committed) => {\n+\t\t\t\tif (canonicalJson(current()) !== canonicalJson(committed)) throw Error(\"projection commit source mismatch\");\n+\t\t\t\tconst commitState = new CachedStateStorage(state, 65_536, 256);\n+\t\t\t\tconst adapter = new ExperimentalMarkerProjection(\n+\t\t\t\t\tcommitState,\n+\t\t\t\t\tproject.graph,\n+\t\t\t\t\tthis.#context.defaultFile,\n+\t\t\t\t\tthis.#context.namespace,\n+\t\t\t\t);\n+\t\t\t\tconst snapshots = new ProjectionSnapshots(state, adapter, this.#context);\n+\t\t\t\tlet value = snapshots.load(head.handle, this.#identity(head.source, head.version));\n+\t\t\t\tlet saved = false;\n+\t\t\t\tfor (let version = head.version + 1; version <= project.version; version++) {\n+\t\t\t\t\tfor (const item of this.#atoms(project.graph, versions[version]!))\n+\t\t\t\t\t\tvalue = item.remove\n+\t\t\t\t\t\t\t? adapter.removeAtom(value, project.graph.atom(item.id))\n+\t\t\t\t\t\t\t: adapter.apply(value, project.graph.atom(item.id));\n+\t\t\t\t\tthis.#save(state, snapshots, value, committed, version);\n+\t\t\t\t\tsaved = true;\n+\t\t\t\t}\n+\t\t\t\tif (!saved) this.#save(state, snapshots, value, committed, project.version);\n+\t\t\t},\n+\t\t\tclose: () => {\n+\t\t\t\tif (!live) return;\n+\t\t\t\tlive = false;\n+\t\t\t\ttry {\n+\t\t\t\t\tfor (const scope of scopes) scope.close();\n+\t\t\t\t\tfor (const overlay of overlays) overlay.close();\n+\t\t\t\t} finally {\n+\t\t\t\t\tthis.#active = false;\n+\t\t\t\t}\n+\t\t\t},\n+\t\t};\n+\t}\n+}"}],"unmatched":[]}