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
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/server/test/disk-first-runtime.test.ts?version=160&format=json
132 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 158, 1 version changed it

You're reading version 160. The newest is 318, and 2 versions since then changed this file. Go to the newestCompare with 318

1import assert from "node:assert/strict";
2import { mkdtempSync, rmdirSync, unlinkSync } from "node:fs";
3import { tmpdir } from "node:os";
4import { join } from "node:path";
5import { test } from "node:test";
6import { answer } from "../../worker/project/http.ts";
7import { Project } from "../../worker/project/project.ts";
8import { DiskFirstRuntime } from "../src/disk-first-runtime.ts";
9import { SqliteLog } from "../src/engine-project.ts";
10import { memoryStorage } from "./helpers.ts";
12test("runtime resumes from disk and detaches reads, with exact authorized retries", (t) => {
13 const directory = mkdtempSync(join(tmpdir(), "abso-runtime-"));
14 const filename = join(directory, "state.sqlite");
15 let storage = memoryStorage(filename);
16 t.after(() => {
17 storage.db.close();
18 unlinkSync(filename);
19 rmdirSync(directory);
20 });
21 const runtime = () => new DiskFirstRuntime(storage, "project-1", "disk-v1");
22 const validate = () => {};
23 t.mock.method(Project, "load", () => {
24 throw Error("full-history load forbidden");
25 });
26 assert.equal(runtime().preparePage(validate).phase, "empty");
27 assert.equal(
28 storage.db.prepare("SELECT COUNT(*) AS n FROM sqlite_master WHERE name = 'writer_state'").get()!.n,
29 0,
30 "an empty lookup must not install the legacy-write fence",
31 );
32 assert.equal(
33 runtime().initialize((calls) => answer(calls, "init", undefined, { files: { "a.txt": "one\ntwo\n" } }), validate)
34 .status,
35 200,
36 );
37 assert.throws(() => runtime().write(() => undefined, validate), /not complete/);
38 let pages = 0;
39 while (!runtime().preparePage(validate).ready) {
40 assert.ok(++pages < 100);
41 storage.db.close();
42 storage = memoryStorage(filename);
43 }
44 assert.ok(pages > 0);
45 const req = { id: "record-1", identity: { agent: "alice", operation: "record", files: { "a.txt": "changed\n" } } };
46 const operation = (calls: Parameters<Parameters<DiskFirstRuntime["write"]>[0]>[0]) =>
47 answer(calls, "record", "alice", { files: req.identity.files });
48 const first = runtime().write(operation, validate, req);
49 assert.equal(first.status, 200);
50 const length = new SqliteLog(storage).seq();
51 storage.db.close();
52 storage = memoryStorage(filename);
53 assert.deepEqual(
54 runtime().write(
55 () => {
56 throw Error("retry executed");
57 },
58 validate,
59 req,
60 ),
61 first,
62 );
63 assert.equal(new SqliteLog(storage).seq(), length);
64 assert.throws(() => runtime().write(operation, validate, { ...req, identity: { agent: "bob" } }), /reused/);
65 assert.throws(
66 () =>
67 runtime().write(
68 operation,
69 () => {
70 throw Error("revoked");
71 },
72 req,
73 ),
74 /revoked/,
75 );
76 assert.equal(
77 runtime().write((calls) => answer(calls, "publish", "alice", { message: "Publish" }), validate).status,
78 200,
79 );
80 const shared = runtime().read((calls) => calls.shared(), validate);
81 assert.equal(Object.getOwnPropertyDescriptor(shared, "files")?.get, undefined);
82 assert.equal(shared.files["a.txt"], "changed\n");
83 storage.db.close();
84 storage = memoryStorage(filename);
85 assert.equal(runtime().preparePage(validate).ready, true);
86 assert.equal(
87 runtime().read((calls) => calls.shared().files["a.txt"], validate),
88 "changed\n",
89 );
90});
92test("runtime page and write authority failures leave no partial state", (t) => {
93 const storage = memoryStorage();
94 t.after(() => storage.db.close());
95 const runtime = new DiskFirstRuntime(storage, "project", "disk-v1");
96 runtime.initialize(
97 (calls) => answer(calls, "init", undefined, { files: { "a.txt": "one\n" } }),
98 () => {},
99 );
100 const rows = () => {
101 const tables = storage.db.prepare("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name").all();
102 return tables.map((row) => [
103 row.name,
104 storage.db.prepare(`SELECT * FROM "${String(row.name).replaceAll('"', '""')}" ORDER BY rowid`).all(),
105 ]);
106 };
107 const before = rows();
108 let checks = 0;
109 assert.throws(
110 () =>
111 runtime.preparePage(() => {
112 if (++checks === 3) throw Error("authority changed");
113 }),
114 /authority changed/,
115 );
116 assert.deepEqual(rows(), before);
117 while (!runtime.preparePage(() => {}).ready) {}
118 const adopted = rows();
119 checks = 0;
120 assert.throws(
121 () =>
122 runtime.write(
123 (calls) => answer(calls, "record", "alice", { files: { "a.txt": "two\n" } }),
124 () => {
125 if (++checks === 3) throw Error("authority changed");
126 },
127 { id: "failed", identity: "record" },
128 ),
129 /authority changed/,
130 );
131 assert.deepEqual(rows(), adopted);
132});