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 161 to 162

Compare

+98 −14 in 10 places
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";
79
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 };
1012
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(
2327 {
2428 database,
Lines 35–91 at 162 (were 31–41 at 161)
3135 );
3236 }
3337 #host() {
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,
41+ });
3542 }
3643 #state() {
3744 return new SqliteWriterState(this.#storage, this.#namespace);
3845 }
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, {
55+ format: 3,
56+ identity,
57+ parts,
58+ characters: json.length,
59+ digest: createHash("sha256").update(json).digest("hex"),
60+ });
61+ }
62+ #receiptResult<T>(
63+ state: SqliteWriterState,
64+ id: string,
65+ receipt: { format: number; result?: T; parts?: number; characters?: number; digest?: string },
66+ ): T {
67+ if (receipt.format === 2) return structuredClone(receipt.result) as T;
68+ if (
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))
75+ )
76+ throw Error("invalid receipt header");
77+ let json = "";
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");
82+ json += chunk;
83+ }
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;
87+ }
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);
50103 return state;
51104 }
52105 /** Initializes an empty canonical log without replay. Existing history must be adopted. */
53− initialize<T>(operation: (calls: EngineProject) => T, validate: () => void): T {
106+ initialize<T>(
107+ operation: (calls: EngineProject) => T,
108+ validate: () => void,
109+ request?: { id: string; identity: unknown },
110+ ): T {
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)
120+ this.#saveReceipt(
121+ this.#setup(),
122+ request.id,
123+ createHash("sha256").update(canonicalJson(request.identity)).digest("hex"),
124+ result,
125+ );
61126 return result;
62127 });
63128 }
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 };
147+ }
148+ state.write("runtime", "history-ready", true);
78149 const scope = this.#projection.open({ state, log, root });
79150 scope.close();
80151 this.#validate(validate);
Lines 162–168 at 162 (were 91–97 at 161)
91162 from: cursor?.next ?? 0,
92163 digest: cursor?.digest ?? "",
93164 maxEntries: 32,
94− maxBytes: 1_048_576,
165+ maxBytes: log.adoptionPageBytes(cursor?.next ?? 0),
95166 },
96167 validate,
97168 );
Lines 179–191 at 162 (were 108–115 at 161)
108179 maxSteps: 32,
109180 });
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);
187+ }
188+ return { ready: history.ready, phase: history.ready ? "ready" : "history", next: history.next };
113189 });
114190 }
115191 #requireReady() {
Lines 194–204 at 162 (were 118–124 at 161)
118194 .length === 0
119195 )
120196 throw Error("disk-first adoption is not complete");
121− if (this.#state().read("runtime", "format") !== 1 || this.#state().read("runtime", "ready") !== true)
197+ if (
198+ this.#state().read("runtime", "format") !== 5 ||
199+ this.#state().read("runtime", "ready") !== true ||
200+ this.#state().read("runtime", "history-ready") !== true
201+ )
122202 throw Error("disk-first adoption is not complete");
123203 }
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();
144224 host.root();
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;
147227 if (request) {
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 }
230+ | undefined;
149231 if (receipt) {
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);
153237 }
154238 }
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);
163247 },
164248 });
165249 });
MCP
compare on this site's /mcp
CLI
abso diff 161 162 server/src/disk-first-runtime.ts --json
JSON
/christopher/abso-core/compare/161..162?file=server%2Fsrc%2Fdisk-first-runtime.ts&format=json