Lines 1–14 at 162 (were 1–12 at 161)
1+import { createHash } from "node:crypto"; 12import { canonicalJson } from "../../worker/project/operation-protocol.ts"; 23import { Project, type WriterSource } from "../../worker/project/project.ts"; 34import type { ProjectOptions } from "../../worker/project/project-types.ts"; 45import { EngineProject, SqliteLog, type SqliteStorage } from "./engine-project.ts"; 6+import { SqliteHistoryHost } from "./history-host.ts"; 57import { SqliteProjectionStore } from "./projection-store.ts"; 68import { SqliteWriterHost, SqliteWriterState } from "./writer-host.ts"; 810type Services = Pick<ProjectOptions, "hasBlob" | "whole">; 9−type Progress = { ready: boolean; phase: "empty" | "writer" | "projection" | "ready"; next: number };
11+type Progress = { ready: boolean; phase: "empty" | "writer" | "projection" | "history" | "ready"; next: number }; 1113/** One synchronous page or call per invocation. No object retains a loaded Project. 1214 * The owner schedules subsequent pages and supplies fresh authorization for every call. */ Lines 17–28 at 162 (were 15–24 at 161)
1517 readonly #namespace: string; 1618 readonly #projection: SqliteProjectionStore; 1719 readonly #services: Services; 20+ readonly #history: SqliteHistoryHost; 1821 constructor(storage: SqliteStorage, database: string, namespace: string, services: Services = {}) { 1922 this.#storage = storage; 2023 this.#namespace = namespace; 2124 this.#services = services; 25+ this.#history = new SqliteHistoryHost(storage, `${namespace}:history`); 2226 this.#projection = new SqliteProjectionStore( Lines 35–91 at 162 (were 31–41 at 161)
34− return new SqliteWriterHost(this.#storage, this.#namespace, this.#projection, { indexProjectionAtoms: true });
38+ return new SqliteWriterHost(this.#storage, this.#namespace, this.#projection, { 39+ indexProjectionAtoms: true, 40+ history: this.#history, 3744 return new SqliteWriterState(this.#storage, this.#namespace); 46+ #saveReceipt(state: SqliteWriterState, id: string, identity: string, result: unknown) { 47+ // Receipt payloads can exceed the graph-state value bound. Keep each stored 48+ // piece small without increasing that bound for engine state. 49+ const json = JSON.stringify({ result }); 50+ const characters = 64 * 1024; 51+ const parts = Math.ceil(json.length / characters); 52+ for (let part = 0; part < parts; part++) 53+ state.write(`runtime:receipt:${id}`, String(part), json.slice(part * characters, (part + 1) * characters)); 54+ state.write("runtime:receipts", id, { 58+ characters: json.length, 59+ digest: createHash("sha256").update(json).digest("hex"), 63+ state: SqliteWriterState, 65+ receipt: { format: number; result?: T; parts?: number; characters?: number; digest?: string }, 67+ if (receipt.format === 2) return structuredClone(receipt.result) as T; 69+ receipt.format !== 3 || 70+ !Number.isSafeInteger(receipt.parts) || 71+ receipt.parts! < 1 || 72+ !Number.isSafeInteger(receipt.characters) || 73+ receipt.characters! < 1 || 74+ receipt.parts !== Math.ceil(receipt.characters! / (64 * 1024)) 76+ throw Error("invalid receipt header"); 78+ for (let part = 0; part < receipt.parts!; part++) { 79+ const chunk = state.read(`runtime:receipt:${id}`, String(part)); 80+ if (typeof chunk !== "string" || chunk.length !== Math.min(64 * 1024, receipt.characters! - part * 64 * 1024)) 81+ throw Error("incomplete receipt payload"); 84+ if (createHash("sha256").update(json).digest("hex") !== receipt.digest) 85+ throw Error("receipt payload digest mismatch"); 86+ return (JSON.parse(json) as { result: T }).result; 3989 #validate(validate: () => void) { 4090 const result: unknown = validate(); 4191 if (result && (typeof result === "object" || typeof result === "function") && "then" in result) Lines 95–128 at 162 (were 45–63 at 161)
4595 SqliteWriterState.initialize(this.#storage); 4696 const state = this.#state(); 4797 const format = state.read("runtime", "format"); 48− if (format !== undefined && format !== 1) throw Error("unsupported disk-first runtime format");
49− if (format === undefined) state.write("runtime", "format", 1);
98+ if (format !== undefined && format !== 1 && format !== 2 && format !== 3 && format !== 4 && format !== 5) 99+ throw Error("unsupported disk-first runtime format"); 100+ // Format 5 additionally fences indexed insertion runs and lines from older readers. 101+ // Never lower this marker in place. 102+ if (format !== 5) state.write("runtime", "format", 5); 52105 /** Initializes an empty canonical log without replay. Existing history must be adopted. */ 53− initialize<T>(operation: (calls: EngineProject) => T, validate: () => void): T {
107+ operation: (calls: EngineProject) => T, 108+ validate: () => void, 109+ request?: { id: string; identity: unknown }, 54111 return this.#storage.transactionSync(() => { 55112 this.#validate(validate); 56113 const log = new SqliteLog(this.#storage); 57114 if (log.seq() !== 0) throw Error("disk-first initialization requires an empty log"); 115+ if (request && (!request.id || request.id.length > 128)) throw Error("invalid disk-first request identity"); 58116 const project = new Project(log, { ...this.#services, now: () => Date.now(), shapes: log.shapes() }); 59117 const result = structuredClone(operation(new EngineProject(project))); 60118 this.#validate(validate); 119+ if (request && log.seq() > 0) 123+ createHash("sha256").update(canonicalJson(request.identity)).digest("hex"), Lines 140–151 at 162 (were 75–80 at 161)
75140 if (ready !== undefined && ready !== true) throw Error("invalid runtime readiness marker"); 76141 if (ready === true) { 77142 const root = host.root(); 143+ const history = this.#history.prepare(log, root, () => host.root(), validate); 144+ if (!history.ready) { 145+ state.remove("runtime", "history-ready"); 146+ return { ready: false, phase: "history", next: history.next }; 148+ state.write("runtime", "history-ready", true); 78149 const scope = this.#projection.open({ state, log, root }); 80151 this.#validate(validate); Lines 162–168 at 162 (were 91–97 at 161)
91162 from: cursor?.next ?? 0, 92163 digest: cursor?.digest ?? "", 94− maxBytes: 1_048_576,
165+ maxBytes: log.adoptionPageBytes(cursor?.next ?? 0), Lines 179–191 at 162 (were 108–115 at 161)
110181 this.#validate(validate); 111− if (page.ready) state.write("runtime", "ready", true);
112− return { ready: page.ready, phase: page.ready ? "ready" : "projection", next: root.logLength };
182+ if (!page.ready) return { ready: false, phase: "projection", next: root.logLength }; 183+ const history = this.#history.prepare(log, root, () => host.root(), validate); 184+ if (history.ready) { 185+ state.write("runtime", "ready", true); 186+ state.write("runtime", "history-ready", true); 188+ return { ready: history.ready, phase: history.ready ? "ready" : "history", next: history.next }; Lines 194–204 at 162 (were 118–124 at 161)
120196 throw Error("disk-first adoption is not complete"); 121− if (this.#state().read("runtime", "format") !== 1 || this.#state().read("runtime", "ready") !== true)
198+ this.#state().read("runtime", "format") !== 5 || 199+ this.#state().read("runtime", "ready") !== true || 200+ this.#state().read("runtime", "history-ready") !== true 122202 throw Error("disk-first adoption is not complete"); 124204 read<T>(operation: (calls: EngineProject) => T, validate: () => void): T { Lines 223–239 at 162 (were 143–155 at 161)
143223 const host = this.#host(); 145225 if (request && (!request.id || request.id.length > 128)) throw Error("invalid disk-first request identity"); 146− const identity = request ? canonicalJson(request.identity) : undefined;
226+ const identity = request ? createHash("sha256").update(canonicalJson(request.identity)).digest("hex") : undefined; 148− const receipt = state.read("runtime:receipts", request.id) as { identity: string; result: T } | undefined;
228+ const receipt = state.read("runtime:receipts", request.id) as 229+ | { format: number; identity: string; result: T } 150− if (receipt.identity !== identity) throw Error("disk-first request identity was reused");
232+ if (receipt.format !== 2 && receipt.format !== 3) throw Error("unsupported disk-first receipt format"); 233+ if (receipt.identity !== identity) 234+ throw Object.assign(Error("disk-first request identity was reused"), { code: "request-reused" }); 151235 this.#validate(validate); 152− return structuredClone(receipt.result);
236+ return this.#receiptResult<T>(state, request.id, receipt); 155239 return host.runCalls({ Lines 243–249 at 162 (were 159–165 at 161)
159243 checkerReady: false, 160244 operation: (calls) => structuredClone(operation(calls)), 161245 commitReceipt: (result) => { 162− if (request) state.write("runtime:receipts", request.id, { identity, result });
246+ if (request) this.#saveReceipt(state, request.id, identity!, result);