{"draft":null,"from":161,"to":162,"files":[{"path":"server/src/writer-host.ts","added":76,"removed":31,"diff":"--- a/server/src/writer-host.ts\n+++ b/server/src/writer-host.ts\n@@ -5,6 +5,7 @@\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@@ -12,6 +13,7 @@\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@@ -145,25 +147,39 @@\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 createHash(\"sha256\")\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-\t\t.update(json, \"utf8\")\n-\t\t.digest(\"hex\");\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 = writerFrameDigest(digest, seq, log.boundedFrame(seq, 8_000_000));\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@@ -176,21 +192,25 @@\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 } = {},\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@@ -246,7 +266,6 @@\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\tinput.maxBytes > 8_000_000 ||\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@@ -288,30 +307,29 @@\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 frames = function* () {\n-\t\t\t\tlet bytes = 0;\n-\t\t\t\tfor (let seq = from; seq < source.logLength && seq < from + limits.maxEntries; seq++) {\n-\t\t\t\t\t// Each frame is bounded before reading; the core also checks the\n-\t\t\t\t\t// cumulative byte count before applying a frame.\n-\t\t\t\t\tconst json = log.boundedFrame(seq, limits.maxBytes);\n-\t\t\t\t\tconst next = new TextEncoder().encode(json).byteLength;\n-\t\t\t\t\tif (bytes + next > limits.maxBytes && bytes > 0) return;\n-\t\t\t\t\tbytes += next;\n-\t\t\t\t\tdigest = writerFrameDigest(digest, seq, json);\n-\t\t\t\t\tyield { seq, json };\n-\t\t\t\t}\n-\t\t\t};\n-\t\t\tconst result = Project.adoptWriterPage(\n-\t\t\t\tthis.#state,\n-\t\t\t\t{\n-\t\t\t\t\tsource,\n-\t\t\t\t\tfrom,\n-\t\t\t\t\tframes: frames(),\n-\t\t\t\t\t...limits,\n-\t\t\t\t\tcurrentSource: () => this.source(),\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\tindexOptions,\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@@ -351,7 +369,25 @@\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 result = input.operation(new EngineProject(project, (from, limit) => this.#log.since(from, limit)));\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@@ -374,7 +410,15 @@\n \t\treturn this.run({\n \t\t\t...input,\n \t\t\toperation: (project) => {\n-\t\t\t\tconst calls = new EngineProject(project, (from, limit) => this.#log.since(from, limit), this.#log);\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@@ -413,6 +457,7 @@\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 {"}],"unmatched":[]}