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 157 to 158

Compare

+167 −0 in 1 place
new file, 167 lines
1+import { canonicalJson } from "../../worker/project/operation-protocol.ts";
2+import { Project, type WriterSource } from "../../worker/project/project.ts";
3+import type { ProjectOptions } from "../../worker/project/project-types.ts";
4+import { EngineProject, SqliteLog, type SqliteStorage } from "./engine-project.ts";
5+import { SqliteProjectionStore } from "./projection-store.ts";
6+import { SqliteWriterHost, SqliteWriterState } from "./writer-host.ts";
7+
8+type Services = Pick<ProjectOptions, "hasBlob" | "whole">;
9+type Progress = { ready: boolean; phase: "empty" | "writer" | "projection" | "ready"; next: number };
11+/** One synchronous page or call per invocation. No object retains a loaded Project.
12+ * The owner schedules subsequent pages and supplies fresh authorization for every call. */
13+export class DiskFirstRuntime {
14+ readonly #storage: SqliteStorage;
15+ readonly #namespace: string;
16+ readonly #projection: SqliteProjectionStore;
17+ readonly #services: Services;
18+ constructor(storage: SqliteStorage, database: string, namespace: string, services: Services = {}) {
19+ this.#storage = storage;
20+ this.#namespace = namespace;
21+ this.#services = services;
22+ this.#projection = new SqliteProjectionStore(
23+ {
24+ database,
25+ namespace: `${namespace}:views`,
26+ registry: `${namespace}:roots`,
27+ defaultFile: "__abso_projection_default__",
28+ },
29+ 1,
30+ { indexedAtoms: true },
31+ );
32+ }
33+ #host() {
34+ return new SqliteWriterHost(this.#storage, this.#namespace, this.#projection, { indexProjectionAtoms: true });
35+ }
36+ #state() {
37+ return new SqliteWriterState(this.#storage, this.#namespace);
38+ }
39+ #validate(validate: () => void) {
40+ const result: unknown = validate();
41+ if (result && (typeof result === "object" || typeof result === "function") && "then" in result)
42+ throw Error("runtime authorization must be synchronous");
43+ }
44+ #setup() {
45+ SqliteWriterState.initialize(this.#storage);
46+ const state = this.#state();
47+ 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);
50+ return state;
51+ }
52+ /** Initializes an empty canonical log without replay. Existing history must be adopted. */
53+ initialize<T>(operation: (calls: EngineProject) => T, validate: () => void): T {
54+ return this.#storage.transactionSync(() => {
55+ this.#validate(validate);
56+ const log = new SqliteLog(this.#storage);
57+ if (log.seq() !== 0) throw Error("disk-first initialization requires an empty log");
58+ const project = new Project(log, { ...this.#services, now: () => Date.now(), shapes: log.shapes() });
59+ const result = structuredClone(operation(new EngineProject(project)));
60+ this.#validate(validate);
61+ return result;
62+ });
63+ }
64+ preparePage(validate: () => void): Progress {
65+ return this.#storage.transactionSync(() => {
66+ this.#validate(validate);
67+ const log = new SqliteLog(this.#storage);
68+ if (log.seq() === 0) {
69+ this.#validate(validate);
70+ return { ready: false, phase: "empty", next: 0 };
71+ }
72+ const state = this.#setup();
73+ const host = this.#host();
74+ const ready = state.read("runtime", "ready");
75+ if (ready !== undefined && ready !== true) throw Error("invalid runtime readiness marker");
76+ if (ready === true) {
77+ const root = host.root();
78+ const scope = this.#projection.open({ state, log, root });
79+ scope.close();
80+ this.#validate(validate);
81+ return { ready: true, phase: "ready", next: root.logLength };
82+ }
83+ const bound = state.read("host", "canonical-root");
84+ if (bound === undefined) {
85+ const cursor = state.read("host", "adoption-root") as
86+ | { source: WriterSource; next: number; digest: string }
87+ | undefined;
88+ const page = host.adoptVerifiedPage(
89+ {
90+ expected: cursor?.source ?? host.source(),
91+ from: cursor?.next ?? 0,
92+ digest: cursor?.digest ?? "",
93+ maxEntries: 32,
94+ maxBytes: 1_048_576,
95+ },
96+ validate,
97+ );
98+ this.#validate(validate);
99+ return { ready: false, phase: "writer", next: page.next };
100+ }
101+ const root = host.root();
102+ const page = this.#projection.adopt({
103+ state,
104+ expected: root,
105+ currentRoot: () => host.root(),
106+ maxAtoms: 32,
107+ maxVersions: 1,
108+ maxSteps: 32,
109+ });
110+ 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 };
113+ });
114+ }
115+ #requireReady() {
116+ if (
117+ this.#storage.sql.exec("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'writer_state'").toArray()
118+ .length === 0
119+ )
120+ throw Error("disk-first adoption is not complete");
121+ if (this.#state().read("runtime", "format") !== 1 || this.#state().read("runtime", "ready") !== true)
122+ throw Error("disk-first adoption is not complete");
123+ }
124+ read<T>(operation: (calls: EngineProject) => T, validate: () => void): T {
125+ this.#requireReady();
126+ const host = this.#host();
127+ return host.readCalls({
128+ expected: host.source(),
129+ validateAuthority: validate,
130+ services: this.#services,
131+ operation: (calls) => structuredClone(operation(calls)),
132+ });
133+ }
134+ write<T>(
135+ operation: (calls: EngineProject) => T,
136+ validate: () => void,
137+ request?: { id: string; identity: unknown },
138+ ): T {
139+ return this.#storage.transactionSync(() => {
140+ this.#validate(validate);
141+ this.#requireReady();
142+ const state = this.#state();
143+ const host = this.#host();
144+ host.root();
145+ if (request && (!request.id || request.id.length > 128)) throw Error("invalid disk-first request identity");
146+ const identity = request ? canonicalJson(request.identity) : undefined;
147+ if (request) {
148+ const receipt = state.read("runtime:receipts", request.id) as { identity: string; result: T } | undefined;
149+ if (receipt) {
150+ if (receipt.identity !== identity) throw Error("disk-first request identity was reused");
151+ this.#validate(validate);
152+ return structuredClone(receipt.result);
153+ }
154+ }
155+ return host.runCalls({
156+ expected: host.source(),
157+ validateAuthority: validate,
158+ services: this.#services,
159+ checkerReady: false,
160+ operation: (calls) => structuredClone(operation(calls)),
161+ commitReceipt: (result) => {
162+ if (request) state.write("runtime:receipts", request.id, { identity, result });
163+ },
164+ });
165+ });
166+ }
167+}
MCP
compare on this site's /mcp
CLI
abso diff 157 158 server/src/disk-first-runtime.ts --json
JSON
/christopher/abso-core/compare/157..158?file=server%2Fsrc%2Fdisk-first-runtime.ts&format=json