Lines 5–11 at 162 (were 5–10 at 161)
55import type { GraphStateStorage } from "../../src/engine/state-source.ts"; 66import { Project, type WriterSource } from "../../worker/project/project.ts"; 77import type { ProjectOptions } from "../../worker/project/project-types.ts"; 8+import { withDecodedFrames } from "./decoded-frames.ts"; Lines 13–19 at 162 (were 12–17 at 161)
1415} from "./engine-project.ts"; 16+import type { SqliteHistoryHost } from "./history-host.ts"; 1618export class SqliteWriterState implements GraphStateStorage { 1719 readonly #storage: SqliteStorage; Lines 147–185 at 162 (were 145–169 at 161)
146148/** Domain-separated SHA-256 chain over exact canonical UTF-8 frames and their positions. */ 147149export function writerFrameDigest(previous: string, seq: number, json: string): string { 148− return createHash("sha256")
150+ return writerFrameChunksDigest(previous, seq, [json]); 153+/** Same canonical digest without retaining the complete frame. */ 154+export function writerFrameChunksDigest(previous: string, seq: number, chunks: Iterable<string>): string { 155+ const hash = createHash("sha256") 149156 .update("abso-writer-log-v1\n") 150157 .update(JSON.stringify([previous, seq])) 151− .update("\n")
152− .update(json, "utf8")
153− .digest("hex");
160+ for (const chunk of chunks) { 161+ const text = carry + chunk; 162+ const last = text.charCodeAt(text.length - 1); 163+ const split = last >= 0xd800 && last <= 0xdbff; 164+ carry = split ? text.slice(-1) : ""; 165+ hash.update(split ? text.slice(0, -1) : text, "utf8"); 167+ if (carry) hash.update(carry, "utf8"); 168+ return hash.digest("hex"); 156171export function advanceWriterRoot(log: SqliteLog, previous: WriterRoot, source: WriterSource): WriterRoot { 157172 let digest = previous.digest; 158173 if (source.logLength < previous.logLength) throw new Error("writer root cannot move backwards"); 159174 for (let seq = previous.logLength; seq < source.logLength; seq++) 160− digest = writerFrameDigest(digest, seq, log.boundedFrame(seq, 8_000_000));
175+ digest = writerFrameChunksDigest(digest, seq, log.frameChunks(seq)); 161176 return { format: 1, ...source, digest }; 164179/** Inactive host for explicit adoption and synchronous fenced writer operations. */ 165180export interface WriterProjectionSession { 166181 readonly readViewProvider: NonNullable<ProjectOptions["readViewProvider"]>; 182+ historicalReadViewProvider?(state: GraphStateStorage): NonNullable<ProjectOptions["readViewProvider"]>; 167183 commit(project: Project, root: WriterRoot): void; 168184 /** Cleanup is inside the canonical transaction. Any failure aborts roots and receipt too. */ Lines 192–216 at 162 (were 176–196 at 161)
177193export class SqliteWriterHost { 178194 readonly #storage: SqliteStorage; 195+ readonly #namespace: string; 179196 readonly #state: SqliteWriterState; 180197 readonly #log: SqliteLog; 181198 readonly #projection?: WriterProjectionHost; 182199 readonly #indexProjectionAtoms: boolean; 200+ readonly #history?: SqliteHistoryHost; 184202 storage: SqliteStorage, 185203 namespace: string, 186204 projection?: WriterProjectionHost, 187− options: { indexProjectionAtoms?: boolean } = {},
205+ options: { indexProjectionAtoms?: boolean; history?: SqliteHistoryHost } = {}, 189207 this.#storage = storage; 208+ this.#namespace = namespace; 190209 this.#state = new SqliteWriterState(storage, namespace); 191210 this.#log = new SqliteLog(storage); 192211 this.#projection = projection; 193212 this.#indexProjectionAtoms = options.indexProjectionAtoms ?? false; 213+ this.#history = options.history; 195215 #indexOptions(adopting = false) { 196216 const mode = this.#state.read("host", "projection-atom-index-mode"); Lines 266–271 at 162 (were 246–252 at 161)
246266 input.maxEntries > 256 || 247267 !Number.isSafeInteger(input.maxBytes) || 248268 input.maxBytes < 1 || 249− input.maxBytes > 8_000_000 ||
250269 !(input.digest === "" || /^[a-f0-9]{64}$/.test(input.digest)) 252271 throw new Error("invalid bounded adoption request"); Lines 307–335 at 162 (were 288–317 at 161)
289308 throw new Error("writer adoption root cursor is stale"); 290309 let digest = previous?.digest ?? ""; 291− const frames = function* () {
292− let bytes = 0;
293− for (let seq = from; seq < source.logLength && seq < from + limits.maxEntries; seq++) {
294− // Each frame is bounded before reading; the core also checks the
295− // cumulative byte count before applying a frame.
296− const json = log.boundedFrame(seq, limits.maxBytes);
297− const next = new TextEncoder().encode(json).byteLength;
298− if (bytes + next > limits.maxBytes && bytes > 0) return;
299− bytes += next;
300− digest = writerFrameDigest(digest, seq, json);
301− yield { seq, json };
302− }
303− };
304− const result = Project.adoptWriterPage(
305− this.#state,
306− {
307− source,
308− from,
309− frames: frames(),
310− ...limits,
311− currentSource: () => this.source(),
310+ const decoded = withDecodedFrames( 312+ `${this.#namespace}:decoded`, 314+ { from, to: source.logLength, ...limits, digest }, 316+ const result = Project.adoptWriterPage( 323+ decodedFrames: true, 324+ currentSource: () => this.source(), 313− indexOptions,
331+ const result = decoded.result; 332+ digest = decoded.digest; 315333 SqliteWriterHost.#sync(validateAuthority()); 316334 this.#state.write("host", "adoption-root", { source, next: result.next, digest }); 317335 if (result.ready) { Lines 369–393 at 162 (were 351–357 at 161)
351369 readViewProvider: projection?.readViewProvider, 352370 entryAt: (index) => this.#log.since(index, 1)[0]!, 354− const result = input.operation(new EngineProject(project, (from, limit) => this.#log.since(from, limit)));
372+ const root = this.root(); 373+ const result = input.operation( 376+ (from, limit) => this.#log.since(from, limit), 379+ ? (at, operation) => 380+ this.#history!.read( 383+ input.validateAuthority, 386+ projection?.historicalReadViewProvider, 355391 SqliteWriterHost.#sync(result); 356392 SqliteWriterHost.#sync(input.validateAuthority()); 357393 if (!SqliteWriterHost.#same(source, this.source())) throw new Error("reader source changed"); Lines 410–424 at 162 (were 374–380 at 161)
376412 operation: (project) => { 377− const calls = new EngineProject(project, (from, limit) => this.#log.since(from, limit), this.#log);
413+ const root = this.root(); 414+ const calls = new EngineProject( 416+ (from, limit) => this.#log.since(from, limit), 419+ ? (at, operation) => this.#history!.read(root, () => this.root(), input.validateAuthority, at, operation) 378422 calls.setCheckerReady(input.checkerReady); 379423 return input.operation(calls); Lines 457–463 at 162 (were 413–418 at 161)
413457 const committedRoot = advanceWriterRoot(this.#log, root, committed); 414458 this.#state.write("host", "canonical-root", committedRoot); 415459 SqliteWriterHost.#sync(projection?.commit(project, committedRoot)); 460+ this.#history?.advance(this.#log, root, committedRoot, () => this.root(), input.validateAuthority); 416461 SqliteWriterHost.#sync(input.commitReceipt(result, committed));