{"draft":null,"from":2,"to":318,"files":[{"path":"server/src/writer-host.ts","added":474,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/writer-host.ts\n@@ -0,0 +1,474 @@\n+// Shared SQLite writer host.\n+// Callers provide current authority and own activation; this module adds no route.\n+import { createHash } from \"node:crypto\";\n+import { indexAcceptedProjectionAtom } from \"../../src/engine/experimental/projection-atom-index.ts\";\n+import type { GraphStateStorage } from \"../../src/engine/state-source.ts\";\n+import { Project, type WriterSource } from \"../../worker/project/project.ts\";\n+import type { ProjectOptions } from \"../../worker/project/project-types.ts\";\n+import { withDecodedFrames } from \"./decoded-frames.ts\";\n+import {\n+\tEngineProject,\n+\tlogParts,\n+\tLOG_PART_CHARACTERS as PART,\n+\tSqliteLog,\n+\ttype SqliteStorage,\n+} from \"./engine-project.ts\";\n+import type { SqliteHistoryHost } from \"./history-host.ts\";\n+\n+export class SqliteWriterState implements GraphStateStorage {\n+\treadonly #storage: SqliteStorage;\n+\treadonly #namespace: string;\n+\tstatic readonly maxValueBytes = 8_000_000;\n+\tconstructor(storage: SqliteStorage, namespace: string) {\n+\t\tif (!namespace || namespace.length > 128) throw new Error(\"invalid writer-state namespace\");\n+\t\tthis.#storage = storage;\n+\t\tthis.#namespace = namespace;\n+\t}\n+\t/** Explicit schema setup, reserved for an authorized adoption host. */\n+\tstatic initialize(storage: SqliteStorage): void {\n+\t\tstorage.sql.exec(\n+\t\t\t\"CREATE TABLE IF NOT EXISTS writer_state (namespace TEXT NOT NULL, collection TEXT NOT NULL, key TEXT NOT NULL, part INTEGER NOT NULL, value TEXT NOT NULL, PRIMARY KEY(namespace, collection, key, part))\",\n+\t\t);\n+\t}\n+\tread(collection: string, key: string): unknown {\n+\t\tconst header = this.#storage.sql\n+\t\t\t.exec(\n+\t\t\t\t\"SELECT value FROM writer_state WHERE namespace = ? AND collection = ? AND key = ? AND part = -1\",\n+\t\t\t\tthis.#namespace,\n+\t\t\t\tcollection,\n+\t\t\t\tkey,\n+\t\t\t)\n+\t\t\t.toArray() as { value: string }[];\n+\t\tif (!header.length) return undefined;\n+\t\tconst meta = JSON.parse(header[0]!.value) as { parts: number; characters: number; bytes: number };\n+\t\tif (\n+\t\t\t!Number.isSafeInteger(meta.parts) ||\n+\t\t\tmeta.parts < 1 ||\n+\t\t\tmeta.parts > Math.ceil(SqliteWriterState.maxValueBytes / (PART - 1)) ||\n+\t\t\t!Number.isSafeInteger(meta.characters) ||\n+\t\t\tmeta.characters < 1 ||\n+\t\t\tmeta.characters > SqliteWriterState.maxValueBytes ||\n+\t\t\t!Number.isSafeInteger(meta.bytes) ||\n+\t\t\tmeta.bytes < 1 ||\n+\t\t\tmeta.bytes > SqliteWriterState.maxValueBytes\n+\t\t)\n+\t\t\tthrow new Error(\"invalid writer-state value header\");\n+\t\tconst rows = this.#storage.sql\n+\t\t\t.exec(\n+\t\t\t\t\"SELECT part, value FROM writer_state WHERE namespace = ? AND collection = ? AND key = ? AND part >= 0 ORDER BY part LIMIT ?\",\n+\t\t\t\tthis.#namespace,\n+\t\t\t\tcollection,\n+\t\t\t\tkey,\n+\t\t\t\tmeta.parts + 1,\n+\t\t\t)\n+\t\t\t.toArray() as { part: number; value: string }[];\n+\t\tif (rows.length !== meta.parts || rows.some((row, index) => row.part !== index || row.value.length > PART))\n+\t\t\tthrow new Error(\"incomplete writer-state value\");\n+\t\tconst json = rows.map((row) => row.value).join(\"\");\n+\t\tif (json.length !== meta.characters || new TextEncoder().encode(json).byteLength !== meta.bytes)\n+\t\t\tthrow new Error(\"writer-state value length mismatch\");\n+\t\treturn (JSON.parse(json) as { value?: unknown }).value;\n+\t}\n+\twrite(collection: string, key: string, value: unknown): void {\n+\t\tconst json = JSON.stringify({ value });\n+\t\tif (json.length > SqliteWriterState.maxValueBytes) throw new Error(\"writer-state value exceeds byte bound\");\n+\t\tconst bytes = new TextEncoder().encode(json).byteLength;\n+\t\tif (bytes > SqliteWriterState.maxValueBytes) throw new Error(\"writer-state value exceeds byte bound\");\n+\t\tconst parts = logParts(json);\n+\t\tthis.transaction(() => {\n+\t\t\tthis.remove(collection, key);\n+\t\t\tthis.#storage.sql.exec(\n+\t\t\t\t\"INSERT INTO writer_state VALUES (?, ?, ?, -1, ?)\",\n+\t\t\t\tthis.#namespace,\n+\t\t\t\tcollection,\n+\t\t\t\tkey,\n+\t\t\t\tJSON.stringify({ parts: parts.length, characters: json.length, bytes }),\n+\t\t\t);\n+\t\t\tfor (const [part, text] of parts.entries())\n+\t\t\t\tthis.#storage.sql.exec(\n+\t\t\t\t\t\"INSERT INTO writer_state VALUES (?, ?, ?, ?, ?)\",\n+\t\t\t\t\tthis.#namespace,\n+\t\t\t\t\tcollection,\n+\t\t\t\t\tkey,\n+\t\t\t\t\tpart,\n+\t\t\t\t\ttext,\n+\t\t\t\t);\n+\t\t});\n+\t}\n+\tremove(collection: string, key: string): void {\n+\t\tthis.#storage.sql.exec(\n+\t\t\t\"DELETE FROM writer_state WHERE namespace = ? AND collection = ? AND key = ?\",\n+\t\t\tthis.#namespace,\n+\t\t\tcollection,\n+\t\t\tkey,\n+\t\t);\n+\t}\n+\t/** Delete one owned temporary projection collection without reading its values. */\n+\tclearProjectionOverlay(collection: string): void {\n+\t\tif (!collection.startsWith(\"projection:temporary:\") || collection === \"projection:temporary:\")\n+\t\t\tthrow Error(\"invalid temporary projection collection\");\n+\t\tthis.#storage.sql.exec(\n+\t\t\t\"DELETE FROM writer_state WHERE namespace = ? AND collection = ?\",\n+\t\t\tthis.#namespace,\n+\t\t\tcollection,\n+\t\t);\n+\t}\n+\t*entries(collection: string): Iterable<readonly [string, unknown]> {\n+\t\tlet after: string | undefined;\n+\t\tfor (;;) {\n+\t\t\tconst rows = (\n+\t\t\t\tafter === undefined\n+\t\t\t\t\t? this.#storage.sql.exec(\n+\t\t\t\t\t\t\t\"SELECT key FROM writer_state WHERE namespace = ? AND collection = ? AND part = -1 ORDER BY key LIMIT 1\",\n+\t\t\t\t\t\t\tthis.#namespace,\n+\t\t\t\t\t\t\tcollection,\n+\t\t\t\t\t\t)\n+\t\t\t\t\t: this.#storage.sql.exec(\n+\t\t\t\t\t\t\t\"SELECT key FROM writer_state WHERE namespace = ? AND collection = ? AND part = -1 AND key > ? ORDER BY key LIMIT 1\",\n+\t\t\t\t\t\t\tthis.#namespace,\n+\t\t\t\t\t\t\tcollection,\n+\t\t\t\t\t\t\tafter,\n+\t\t\t\t\t\t)\n+\t\t\t).toArray() as { key: string }[];\n+\t\t\tif (!rows.length) return;\n+\t\t\tafter = rows[0]!.key;\n+\t\t\tyield [after, this.read(collection, after)];\n+\t\t}\n+\t}\n+\ttransaction<T>(body: () => T): T {\n+\t\treturn this.#storage.transactionSync(body);\n+\t}\n+}\n+\n+export interface WriterRoot extends WriterSource {\n+\treadonly format: 1;\n+\treadonly digest: string;\n+}\n+\n+/** Domain-separated SHA-256 chain over exact canonical UTF-8 frames and their positions. */\n+export function writerFrameDigest(previous: string, seq: number, json: string): string {\n+\treturn writerFrameChunksDigest(previous, seq, [json]);\n+}\n+\n+/** Same canonical digest without retaining the complete frame. */\n+export function writerFrameChunksDigest(previous: string, seq: number, chunks: Iterable<string>): string {\n+\tconst hash = createHash(\"sha256\")\n+\t\t.update(\"abso-writer-log-v1\\n\")\n+\t\t.update(JSON.stringify([previous, seq]))\n+\t\t.update(\"\\n\");\n+\tlet carry = \"\";\n+\tfor (const chunk of chunks) {\n+\t\tconst text = carry + chunk;\n+\t\tconst last = text.charCodeAt(text.length - 1);\n+\t\tconst split = last >= 0xd800 && last <= 0xdbff;\n+\t\tcarry = split ? text.slice(-1) : \"\";\n+\t\thash.update(split ? text.slice(0, -1) : text, \"utf8\");\n+\t}\n+\tif (carry) hash.update(carry, \"utf8\");\n+\treturn hash.digest(\"hex\");\n+}\n+\n+export function advanceWriterRoot(log: SqliteLog, previous: WriterRoot, source: WriterSource): WriterRoot {\n+\tlet digest = previous.digest;\n+\tif (source.logLength < previous.logLength) throw new Error(\"writer root cannot move backwards\");\n+\tfor (let seq = previous.logLength; seq < source.logLength; seq++)\n+\t\tdigest = writerFrameChunksDigest(digest, seq, log.frameChunks(seq));\n+\treturn { format: 1, ...source, digest };\n+}\n+\n+/** Inactive host for explicit adoption and synchronous fenced writer operations. */\n+export interface WriterProjectionSession {\n+\treadonly readViewProvider: NonNullable<ProjectOptions[\"readViewProvider\"]>;\n+\thistoricalReadViewProvider?(state: GraphStateStorage): NonNullable<ProjectOptions[\"readViewProvider\"]>;\n+\tcommit(project: Project, root: WriterRoot): void;\n+\t/** Cleanup is inside the canonical transaction. Any failure aborts roots and receipt too. */\n+\tclose(): void;\n+}\n+\n+/** Private integration hook. No endpoint or persisted setting enables it. */\n+export interface WriterProjectionHost {\n+\topen(input: { state: SqliteWriterState; log: SqliteLog; root: WriterRoot }): WriterProjectionSession;\n+}\n+\n+export class SqliteWriterHost {\n+\treadonly #storage: SqliteStorage;\n+\treadonly #namespace: string;\n+\treadonly #state: SqliteWriterState;\n+\treadonly #log: SqliteLog;\n+\treadonly #projection?: WriterProjectionHost;\n+\treadonly #indexProjectionAtoms: boolean;\n+\treadonly #history?: SqliteHistoryHost;\n+\tconstructor(\n+\t\tstorage: SqliteStorage,\n+\t\tnamespace: string,\n+\t\tprojection?: WriterProjectionHost,\n+\t\toptions: { indexProjectionAtoms?: boolean; history?: SqliteHistoryHost } = {},\n+\t) {\n+\t\tthis.#storage = storage;\n+\t\tthis.#namespace = namespace;\n+\t\tthis.#state = new SqliteWriterState(storage, namespace);\n+\t\tthis.#log = new SqliteLog(storage);\n+\t\tthis.#projection = projection;\n+\t\tthis.#indexProjectionAtoms = options.indexProjectionAtoms ?? false;\n+\t\tthis.#history = options.history;\n+\t}\n+\t#indexOptions(adopting = false) {\n+\t\tconst mode = this.#state.read(\"host\", \"projection-atom-index-mode\");\n+\t\tconst expected = this.#indexProjectionAtoms ? 1 : 0;\n+\t\tif (mode !== undefined && mode !== expected) throw Error(\"writer atom indexing mode mismatch\");\n+\t\tif (mode === undefined && this.#indexProjectionAtoms && (!adopting || this.#state.read(\"atoms:meta\", \"length\")))\n+\t\t\tthrow Error(\"writer atom indexing requires fresh adoption\");\n+\t\tif (mode === undefined && adopting) this.#state.write(\"host\", \"projection-atom-index-mode\", expected);\n+\t\treturn { indexAcceptedAtom: this.#indexProjectionAtoms ? indexAcceptedProjectionAtom : undefined };\n+\t}\n+\n+\tsource(): WriterSource {\n+\t\tthis.#log.refreshSequence();\n+\t\tconst shared = this.#storage.sql.exec(\"SELECT value FROM counts WHERE name = 'shared'\").toArray() as {\n+\t\t\tvalue: number;\n+\t\t}[];\n+\t\tif (!Number.isSafeInteger(shared[0]?.value) || shared[0]!.value < 0)\n+\t\t\tthrow new Error(\"canonical shared head must be established before writer adoption\");\n+\t\treturn { generation: this.#log.generation(), logLength: this.#log.seq(), sharedVersion: shared[0]!.value };\n+\t}\n+\tstatic #same(a: WriterSource | undefined, b: WriterSource): boolean {\n+\t\treturn a?.generation === b.generation && a.logLength === b.logLength && a.sharedVersion === b.sharedVersion;\n+\t}\n+\troot(): WriterRoot {\n+\t\tconst root = this.#state.read(\"host\", \"canonical-root\") as WriterRoot | undefined;\n+\t\tif (root?.format !== 1 || !/^[a-f0-9]{64}$/.test(root.digest) || !SqliteWriterHost.#same(root, this.source()))\n+\t\t\tthrow new Error(\"writer canonical root is missing or stale\");\n+\t\treturn root;\n+\t}\n+\tstatic #sync(value: unknown): void {\n+\t\tif (value && (typeof value === \"object\" || typeof value === \"function\") && \"then\" in value)\n+\t\t\tthrow new Error(\"writer transaction callbacks must be synchronous\");\n+\t}\n+\t/** Resume one bounded adoption page against an exact source and prior digest. */\n+\tadoptVerifiedPage(\n+\t\tinput: {\n+\t\t\texpected: WriterSource;\n+\t\t\tfrom: number;\n+\t\t\tdigest: string;\n+\t\t\tmaxEntries: number;\n+\t\t\tmaxBytes: number;\n+\t\t},\n+\t\tvalidateAuthority: () => void,\n+\t) {\n+\t\treturn this.#storage.transactionSync(() => {\n+\t\t\tSqliteWriterHost.#sync(validateAuthority());\n+\t\t\tif (!SqliteWriterHost.#same(input.expected, this.source())) throw new Error(\"adoption source changed\");\n+\t\t\tif (\n+\t\t\t\t!Number.isSafeInteger(input.from) ||\n+\t\t\t\tinput.from < 0 ||\n+\t\t\t\t!Number.isSafeInteger(input.maxEntries) ||\n+\t\t\t\tinput.maxEntries < 1 ||\n+\t\t\t\tinput.maxEntries > 256 ||\n+\t\t\t\t!Number.isSafeInteger(input.maxBytes) ||\n+\t\t\t\tinput.maxBytes < 1 ||\n+\t\t\t\t!(input.digest === \"\" || /^[a-f0-9]{64}$/.test(input.digest))\n+\t\t\t)\n+\t\t\t\tthrow new Error(\"invalid bounded adoption request\");\n+\t\t\tconst request = JSON.stringify([\n+\t\t\t\tinput.expected.generation,\n+\t\t\t\tinput.expected.logLength,\n+\t\t\t\tinput.expected.sharedVersion,\n+\t\t\t\tinput.from,\n+\t\t\t\tinput.digest,\n+\t\t\t\tinput.maxEntries,\n+\t\t\t\tinput.maxBytes,\n+\t\t\t]);\n+\t\t\tconst previous = this.#state.read(\"host\", \"adoption-page-receipt\") as\n+\t\t\t\t| { request: string; result: { next: number; ready: boolean; digest: string } }\n+\t\t\t\t| undefined;\n+\t\t\tif (previous?.request === request) return previous.result;\n+\t\t\tconst cursor = this.#state.read(\"host\", \"adoption-root\") as { next: number; digest: string } | undefined;\n+\t\t\tif ((cursor?.next ?? 0) !== input.from || (cursor?.digest ?? \"\") !== input.digest)\n+\t\t\t\tthrow new Error(\"adoption cursor or digest changed\");\n+\t\t\tconst page = this.adoptPage(input.from, input, validateAuthority);\n+\t\t\tconst root = this.#state.read(\"host\", \"adoption-root\") as { digest: string };\n+\t\t\tconst result = { next: page.next, ready: page.ready, digest: root.digest };\n+\t\t\tthis.#state.write(\"host\", \"adoption-page-receipt\", { request, result });\n+\t\t\treturn result;\n+\t\t});\n+\t}\n+\tadoptPage(from: number, limits: { maxEntries: number; maxBytes: number }, validateAuthority: () => void) {\n+\t\treturn this.#storage.transactionSync(() => {\n+\t\t\tSqliteWriterHost.#sync(validateAuthority());\n+\t\t\tconst source = this.source();\n+\t\t\tconst indexOptions = this.#indexOptions(true);\n+\t\t\tconst log = this.#log;\n+\t\t\tconst previous = this.#state.read(\"host\", \"adoption-root\") as\n+\t\t\t\t| { source: WriterSource; next: number; digest: string }\n+\t\t\t\t| undefined;\n+\t\t\tif (\n+\t\t\t\t(from === 0 && previous) ||\n+\t\t\t\t(from > 0 && (!previous || previous.next !== from || !SqliteWriterHost.#same(previous.source, source)))\n+\t\t\t)\n+\t\t\t\tthrow new Error(\"writer adoption root cursor is stale\");\n+\t\t\tlet digest = previous?.digest ?? \"\";\n+\t\t\tconst decoded = withDecodedFrames(\n+\t\t\t\tthis.#storage,\n+\t\t\t\t`${this.#namespace}:decoded`,\n+\t\t\t\tlog,\n+\t\t\t\t{ from, to: source.logLength, ...limits, digest },\n+\t\t\t\t(frames) => {\n+\t\t\t\t\tconst result = Project.adoptWriterPage(\n+\t\t\t\t\t\tthis.#state,\n+\t\t\t\t\t\t{\n+\t\t\t\t\t\t\tsource,\n+\t\t\t\t\t\t\tfrom,\n+\t\t\t\t\t\t\tframes,\n+\t\t\t\t\t\t\t...limits,\n+\t\t\t\t\t\t\tdecodedFrames: true,\n+\t\t\t\t\t\t\tcurrentSource: () => this.source(),\n+\t\t\t\t\t\t},\n+\t\t\t\t\t\tindexOptions,\n+\t\t\t\t\t);\n+\t\t\t\t\treturn result;\n+\t\t\t\t},\n+\t\t\t);\n+\t\t\tconst result = decoded.result;\n+\t\t\tdigest = decoded.digest;\n+\t\t\tSqliteWriterHost.#sync(validateAuthority());\n+\t\t\tthis.#state.write(\"host\", \"adoption-root\", { source, next: result.next, digest });\n+\t\t\tif (result.ready) {\n+\t\t\t\tthis.#state.write(\"host\", \"canonical\", source);\n+\t\t\t\tthis.#state.write(\"host\", \"canonical-root\", { format: 1, ...source, digest } satisfies WriterRoot);\n+\t\t\t}\n+\t\t\treturn result;\n+\t\t});\n+\t}\n+\t/** Read through the existing call adapter with persistent mutations forbidden. */\n+\treadCalls<T>(input: {\n+\t\texpected: WriterSource;\n+\t\tvalidateAuthority(): void;\n+\t\tservices?: Pick<ProjectOptions, \"hasBlob\" | \"whole\">;\n+\t\toperation(calls: EngineProject): T;\n+\t}): T {\n+\t\treturn this.#storage.transactionSync(() => {\n+\t\t\tSqliteWriterHost.#sync(input.validateAuthority());\n+\t\t\tconst source = this.source();\n+\t\t\tthis.root();\n+\t\t\tif (!SqliteWriterHost.#same(input.expected, source)) throw new Error(\"reader source is stale\");\n+\t\t\tconst refuse = () => {\n+\t\t\t\tthrow new Error(\"indexed read attempted to mutate persistent state\");\n+\t\t\t};\n+\t\t\tconst readOnly: GraphStateStorage = {\n+\t\t\t\tread: (collection, key) => this.#state.read(collection, key),\n+\t\t\t\tentries: (collection) => this.#state.entries(collection),\n+\t\t\t\twrite: refuse,\n+\t\t\t\tremove: refuse,\n+\t\t\t\ttransaction: (body) => this.#storage.transactionSync(body),\n+\t\t\t};\n+\t\t\tconst projection = this.#projection?.open({ state: this.#state, log: this.#log, root: this.root() });\n+\t\t\ttry {\n+\t\t\t\tconst project = Project.openWriter({ append: refuse }, readOnly, source.logLength, {\n+\t\t\t\t\t...input.services,\n+\t\t\t\t\t...(input.services?.whole ? { whole: { read: input.services.whole.read, keep: refuse } } : {}),\n+\t\t\t\t\treadViewProvider: projection?.readViewProvider,\n+\t\t\t\t\tentryAt: (index) => this.#log.since(index, 1)[0]!,\n+\t\t\t\t});\n+\t\t\t\tconst root = this.root();\n+\t\t\t\tconst result = input.operation(\n+\t\t\t\t\tnew EngineProject(\n+\t\t\t\t\t\tproject,\n+\t\t\t\t\t\t(from, limit) => this.#log.since(from, limit),\n+\t\t\t\t\t\tundefined,\n+\t\t\t\t\t\tthis.#history\n+\t\t\t\t\t\t\t? (at, operation) =>\n+\t\t\t\t\t\t\t\t\tthis.#history!.read(\n+\t\t\t\t\t\t\t\t\t\troot,\n+\t\t\t\t\t\t\t\t\t\t() => this.root(),\n+\t\t\t\t\t\t\t\t\t\tinput.validateAuthority,\n+\t\t\t\t\t\t\t\t\t\tat,\n+\t\t\t\t\t\t\t\t\t\toperation,\n+\t\t\t\t\t\t\t\t\t\tprojection?.historicalReadViewProvider,\n+\t\t\t\t\t\t\t\t\t)\n+\t\t\t\t\t\t\t: undefined,\n+\t\t\t\t\t),\n+\t\t\t\t);\n+\t\t\t\tSqliteWriterHost.#sync(result);\n+\t\t\t\tSqliteWriterHost.#sync(input.validateAuthority());\n+\t\t\t\tif (!SqliteWriterHost.#same(source, this.source())) throw new Error(\"reader source changed\");\n+\t\t\t\treturn result;\n+\t\t\t} finally {\n+\t\t\t\tSqliteWriterHost.#sync(projection?.close());\n+\t\t\t}\n+\t\t});\n+\t}\n+\n+\t/** Reuse the existing call adapter within one fenced writer transaction. */\n+\trunCalls<T>(input: {\n+\t\texpected: WriterSource;\n+\t\tvalidateAuthority(): void;\n+\t\tcheckerReady: boolean;\n+\t\tservices?: Pick<ProjectOptions, \"hasBlob\" | \"whole\">;\n+\t\toperation(calls: EngineProject): T;\n+\t\tcommitReceipt(result: NoInfer<T>, source: WriterSource): void;\n+\t}): T {\n+\t\treturn this.run({\n+\t\t\t...input,\n+\t\t\toperation: (project) => {\n+\t\t\t\tconst root = this.root();\n+\t\t\t\tconst calls = new EngineProject(\n+\t\t\t\t\tproject,\n+\t\t\t\t\t(from, limit) => this.#log.since(from, limit),\n+\t\t\t\t\tthis.#log,\n+\t\t\t\t\tthis.#history\n+\t\t\t\t\t\t? (at, operation) => this.#history!.read(root, () => this.root(), input.validateAuthority, at, operation)\n+\t\t\t\t\t\t: undefined,\n+\t\t\t\t);\n+\t\t\t\tcalls.setCheckerReady(input.checkerReady);\n+\t\t\t\treturn input.operation(calls);\n+\t\t\t},\n+\t\t});\n+\t}\n+\t/** No asynchronous gap is permitted between authority validation and commit. */\n+\trun<T>(input: {\n+\t\texpected: WriterSource;\n+\t\tvalidateAuthority(): void;\n+\t\toperation(project: Project): T;\n+\t\tservices?: Pick<ProjectOptions, \"hasBlob\" | \"whole\">;\n+\t\tcommitReceipt(result: NoInfer<T>, source: WriterSource): void;\n+\t}): T {\n+\t\ttry {\n+\t\t\treturn this.#storage.transactionSync(() => {\n+\t\t\t\tSqliteWriterHost.#sync(input.validateAuthority());\n+\t\t\t\tconst source = this.source();\n+\t\t\t\tconst root = this.root();\n+\t\t\t\tconst bound = this.#state.read(\"host\", \"canonical\") as WriterSource | undefined;\n+\t\t\t\tif (!SqliteWriterHost.#same(input.expected, source) || !SqliteWriterHost.#same(bound, source))\n+\t\t\t\t\tthrow new Error(\"writer source or adopted generation is stale\");\n+\t\t\t\tconst projection = this.#projection?.open({ state: this.#state, log: this.#log, root });\n+\t\t\t\ttry {\n+\t\t\t\t\tconst project = Project.openWriter(this.#log, this.#state, source.logLength, {\n+\t\t\t\t\t\t...input.services,\n+\t\t\t\t\t\t...this.#indexOptions(),\n+\t\t\t\t\t\treadViewProvider: projection?.readViewProvider,\n+\t\t\t\t\t\tshapes: this.#log.shapes(),\n+\t\t\t\t\t\tnow: () => Date.now(),\n+\t\t\t\t\t});\n+\t\t\t\t\tconst result = input.operation(project);\n+\t\t\t\t\tSqliteWriterHost.#sync(result);\n+\t\t\t\t\tSqliteWriterHost.#sync(input.validateAuthority());\n+\t\t\t\t\tconst committed = this.source();\n+\t\t\t\t\tthis.#state.write(\"host\", \"canonical\", committed);\n+\t\t\t\t\tconst committedRoot = advanceWriterRoot(this.#log, root, committed);\n+\t\t\t\t\tthis.#state.write(\"host\", \"canonical-root\", committedRoot);\n+\t\t\t\t\tSqliteWriterHost.#sync(projection?.commit(project, committedRoot));\n+\t\t\t\t\tthis.#history?.advance(this.#log, root, committedRoot, () => this.root(), input.validateAuthority);\n+\t\t\t\t\tSqliteWriterHost.#sync(input.commitReceipt(result, committed));\n+\t\t\t\t\treturn result;\n+\t\t\t\t} finally {\n+\t\t\t\t\tSqliteWriterHost.#sync(projection?.close());\n+\t\t\t\t}\n+\t\t\t});\n+\t\t} catch (error) {\n+\t\t\t// The Project was scoped to the transaction and is discarded. Restore\n+\t\t\t// SqliteLog's cached sequence after an enclosing rollback as well.\n+\t\t\tthis.#log.refreshSequence();\n+\t\t\tthrow error;\n+\t\t}\n+\t}\n+}"}],"unmatched":[]}