Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 158 to 318

Compare

+105 −15 in 10 places
Lines 1–15 at 318 (were 1–12 at 158)
1−import { canonicalJson } from "../../worker/project/operation-protocol.ts";
1+import { createHash } from "node:crypto";
2+import { jsonValueChunks } from "../../src/engine/json-value-chunks.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";
8+import { requestIdentityDigest } from "./request-identity-digest.ts";
69import { SqliteWriterHost, SqliteWriterState } from "./writer-host.ts";
811type Services = Pick<ProjectOptions, "hasBlob" | "whole">;
9−type Progress = { ready: boolean; phase: "empty" | "writer" | "projection" | "ready"; next: number };
12+type Progress = { ready: boolean; phase: "empty" | "writer" | "projection" | "history" | "ready"; next: number };
1013
1114/** One synchronous page or call per invocation. No object retains a loaded Project.
1215 * The owner schedules subsequent pages and supplies fresh authorization for every call. */
Lines 18–29 at 318 (were 15–24 at 158)
1518 readonly #namespace: string;
1619 readonly #projection: SqliteProjectionStore;
1720 readonly #services: Services;
21+ readonly #history: SqliteHistoryHost;
1822 constructor(storage: SqliteStorage, database: string, namespace: string, services: Services = {}) {
1923 this.#storage = storage;
2024 this.#namespace = namespace;
2125 this.#services = services;
26+ this.#history = new SqliteHistoryHost(storage, `${namespace}:history`);
2227 this.#projection = new SqliteProjectionStore(
2328 {
2429 database,
Lines 36–102 at 318 (were 31–41 at 158)
3136 );
3237 }
3338 #host() {
34− return new SqliteWriterHost(this.#storage, this.#namespace, this.#projection, { indexProjectionAtoms: true });
39+ return new SqliteWriterHost(this.#storage, this.#namespace, this.#projection, {
40+ indexProjectionAtoms: true,
41+ history: this.#history,
42+ });
3543 }
3644 #state() {
3745 return new SqliteWriterState(this.#storage, this.#namespace);
3846 }
47+ #saveReceipt(state: SqliteWriterState, id: string, identity: string, result: unknown) {
48+ // Receipt payloads can exceed the graph-state value bound. Keep each stored
49+ // piece small without increasing that bound for engine state.
50+ const characters = 64 * 1024;
51+ const digest = createHash("sha256");
52+ let pending = "",
53+ parts = 0,
54+ length = 0;
55+ for (const chunk of jsonValueChunks({ result }, { maxDepth: Infinity, unboxPrimitives: true })) {
56+ digest.update(chunk);
57+ length += chunk.length;
58+ pending += chunk;
59+ while (pending.length >= characters) {
60+ state.write(`runtime:receipt:${id}`, String(parts++), pending.slice(0, characters));
61+ pending = pending.slice(characters);
62+ }
63+ }
64+ if (pending) state.write(`runtime:receipt:${id}`, String(parts++), pending);
65+ state.write("runtime:receipts", id, {
66+ format: 3,
67+ identity,
68+ parts,
69+ characters: length,
70+ digest: digest.digest("hex"),
71+ });
72+ }
73+ #receiptResult<T>(
74+ state: SqliteWriterState,
75+ id: string,
76+ receipt: { format: number; result?: T; parts?: number; characters?: number; digest?: string },
77+ ): T {
78+ if (receipt.format === 2) return structuredClone(receipt.result) as T;
79+ if (
80+ receipt.format !== 3 ||
81+ !Number.isSafeInteger(receipt.parts) ||
82+ receipt.parts! < 1 ||
83+ !Number.isSafeInteger(receipt.characters) ||
84+ receipt.characters! < 1 ||
85+ receipt.parts !== Math.ceil(receipt.characters! / (64 * 1024))
86+ )
87+ throw Error("invalid receipt header");
88+ let json = "";
89+ for (let part = 0; part < receipt.parts!; part++) {
90+ const chunk = state.read(`runtime:receipt:${id}`, String(part));
91+ if (typeof chunk !== "string" || chunk.length !== Math.min(64 * 1024, receipt.characters! - part * 64 * 1024))
92+ throw Error("incomplete receipt payload");
93+ json += chunk;
94+ }
95+ if (createHash("sha256").update(json).digest("hex") !== receipt.digest)
96+ throw Error("receipt payload digest mismatch");
97+ return (JSON.parse(json) as { result: T }).result;
98+ }
39100 #validate(validate: () => void) {
40101 const result: unknown = validate();
41102 if (result && (typeof result === "object" || typeof result === "function") && "then" in result)
Lines 106–134 at 318 (were 45–63 at 158)
45106 SqliteWriterState.initialize(this.#storage);
46107 const state = this.#state();
47108 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);
109+ if (format !== undefined && format !== 1 && format !== 2 && format !== 3 && format !== 4 && format !== 5)
110+ throw Error("unsupported disk-first runtime format");
111+ // Format 5 additionally fences indexed insertion runs and lines from older readers.
112+ // Never lower this marker in place.
113+ if (format !== 5) state.write("runtime", "format", 5);
50114 return state;
51115 }
52116 /** Initializes an empty canonical log without replay. Existing history must be adopted. */
53− initialize<T>(operation: (calls: EngineProject) => T, validate: () => void): T {
117+ initialize<T>(
118+ operation: (calls: EngineProject) => T,
119+ validate: () => void,
120+ request?: { id: string; identity: unknown },
121+ ): T {
54122 return this.#storage.transactionSync(() => {
55123 this.#validate(validate);
56124 const log = new SqliteLog(this.#storage);
57125 if (log.seq() !== 0) throw Error("disk-first initialization requires an empty log");
126+ if (request && (!request.id || request.id.length > 128)) throw Error("invalid disk-first request identity");
58127 const project = new Project(log, { ...this.#services, now: () => Date.now(), shapes: log.shapes() });
59128 const result = structuredClone(operation(new EngineProject(project)));
60129 this.#validate(validate);
130+ if (request && log.seq() > 0)
131+ this.#saveReceipt(this.#setup(), request.id, requestIdentityDigest(request.identity), result);
61132 return result;
62133 });
63134 }
Lines 146–157 at 318 (were 75–80 at 158)
75146 if (ready !== undefined && ready !== true) throw Error("invalid runtime readiness marker");
76147 if (ready === true) {
77148 const root = host.root();
149+ const history = this.#history.prepare(log, root, () => host.root(), validate);
150+ if (!history.ready) {
151+ state.remove("runtime", "history-ready");
152+ return { ready: false, phase: "history", next: history.next };
153+ }
154+ state.write("runtime", "history-ready", true);
78155 const scope = this.#projection.open({ state, log, root });
79156 scope.close();
80157 this.#validate(validate);
Lines 168–174 at 318 (were 91–97 at 158)
91168 from: cursor?.next ?? 0,
92169 digest: cursor?.digest ?? "",
93170 maxEntries: 32,
94− maxBytes: 1_048_576,
171+ maxBytes: log.adoptionPageBytes(cursor?.next ?? 0),
95172 },
96173 validate,
97174 );
Lines 185–197 at 318 (were 108–115 at 158)
108185 maxSteps: 32,
109186 });
110187 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 };
188+ if (!page.ready) return { ready: false, phase: "projection", next: root.logLength };
189+ const history = this.#history.prepare(log, root, () => host.root(), validate);
190+ if (history.ready) {
191+ state.write("runtime", "ready", true);
192+ state.write("runtime", "history-ready", true);
193+ }
194+ return { ready: history.ready, phase: history.ready ? "ready" : "history", next: history.next };
113195 });
114196 }
115197 #requireReady() {
Lines 200–210 at 318 (were 118–124 at 158)
118200 .length === 0
119201 )
120202 throw Error("disk-first adoption is not complete");
121− if (this.#state().read("runtime", "format") !== 1 || this.#state().read("runtime", "ready") !== true)
203+ if (
204+ this.#state().read("runtime", "format") !== 5 ||
205+ this.#state().read("runtime", "ready") !== true ||
206+ this.#state().read("runtime", "history-ready") !== true
207+ )
122208 throw Error("disk-first adoption is not complete");
123209 }
124210 read<T>(operation: (calls: EngineProject) => T, validate: () => void): T {
Lines 229–245 at 318 (were 143–155 at 158)
143229 const host = this.#host();
144230 host.root();
145231 if (request && (!request.id || request.id.length > 128)) throw Error("invalid disk-first request identity");
146− const identity = request ? canonicalJson(request.identity) : undefined;
232+ const identity = request ? requestIdentityDigest(request.identity) : undefined;
147233 if (request) {
148− const receipt = state.read("runtime:receipts", request.id) as { identity: string; result: T } | undefined;
234+ const receipt = state.read("runtime:receipts", request.id) as
235+ | { format: number; identity: string; result: T }
236+ | undefined;
149237 if (receipt) {
150− if (receipt.identity !== identity) throw Error("disk-first request identity was reused");
238+ if (receipt.format !== 2 && receipt.format !== 3) throw Error("unsupported disk-first receipt format");
239+ if (receipt.identity !== identity)
240+ throw Object.assign(Error("disk-first request identity was reused"), { code: "request-reused" });
151241 this.#validate(validate);
152− return structuredClone(receipt.result);
242+ return this.#receiptResult<T>(state, request.id, receipt);
153243 }
154244 }
155245 return host.runCalls({
Lines 249–255 at 318 (were 159–165 at 158)
159249 checkerReady: false,
160250 operation: (calls) => structuredClone(operation(calls)),
161251 commitReceipt: (result) => {
162− if (request) state.write("runtime:receipts", request.id, { identity, result });
252+ if (request) this.#saveReceipt(state, request.id, identity!, result);
163253 },
164254 });
165255 });
MCP
compare on this site's /mcp
CLI
abso diff 158 318 server/src/disk-first-runtime.ts --json
JSON
/christopher/abso-core/compare/158..318?file=server%2Fsrc%2Fdisk-first-runtime.ts&format=json