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

+168 −0 in 1 place
new file, 168 lines
1+import assert from "node:assert/strict";
2+import { registerHooks } from "node:module";
3+import { test } from "node:test";
4+import { Project } from "../../worker/project/project.ts";
5+import { EngineProject } from "../src/engine-project.ts";
6+import { memoryStorage } from "./helpers.ts";
7+
8+const hooks = registerHooks({
9+ resolve(specifier, context, next) {
10+ if (specifier === "cloudflare:workers")
11+ return {
12+ url: `data:text/javascript,${encodeURIComponent("export class DurableObject { constructor(ctx, env) { this.ctx = ctx; this.env = env; } }")}`,
13+ shortCircuit: true,
14+ };
15+ return next(specifier, context);
16+ },
17+});
18+const { ProjectObject } = await import("../src/project-object.ts");
19+hooks.deregister();
21+test("empty disk projects and unloadable legacy projects preserve lookup fallbacks", async (t) => {
22+ const storage = memoryStorage();
23+ t.after(() => storage.db.close());
24+ const context = { id: { toString: () => "empty" }, storage: { ...storage, get: async () => undefined } };
25+ const disk = new ProjectObject(context as never, { ABSO_DISK_FIRST: "on" } as never);
26+ assert.deepEqual(await disk.call("empty", "status", undefined, {}), {
27+ status: 404,
28+ body: { error: "no such project; run init first" },
29+ });
30+ assert.deepEqual(await disk.agents("empty"), []);
31+ assert.deepEqual(await disk.news("empty", [{ from: "alice", issue: null, text: "Hello" }]), [{ to: [] }]);
32+ await disk.alarm();
33+ t.mock.method(EngineProject, "open", () => {
34+ throw Error("unloadable legacy log");
35+ });
36+ const legacy = new ProjectObject(context as never, {} as never);
37+ assert.deepEqual(await legacy.agents("empty"), []);
38+ assert.deepEqual(await legacy.news("empty", [{ from: "alice", issue: null, text: "Hello" }]), [{ to: [] }]);
39+ await legacy.alarm();
40+});
42+test("ProjectObject disk-first calls survive cold objects without replay", async (t) => {
43+ const storage = memoryStorage();
44+ t.after(() => storage.db.close());
45+ const values = new Map<string, unknown>();
46+ let alarm: number | null = null;
47+ let onReaders: (() => Promise<void>) | undefined;
48+ const context = {
49+ id: { toString: () => "project-1" },
50+ storage: {
51+ ...storage,
52+ get: async (key: string) => {
53+ if (key === "readers" && onReaders) {
54+ const action = onReaders;
55+ onReaders = undefined;
56+ await action();
57+ }
58+ return values.get(key);
59+ },
60+ put: async (key: string | Record<string, unknown>, value?: unknown) => {
61+ if (typeof key === "string") values.set(key, value);
62+ else for (const [name, v] of Object.entries(key)) values.set(name, v);
63+ },
64+ delete: async (key: string) => values.delete(key),
65+ getAlarm: async () => alarm,
66+ setAlarm: async (value: number) => {
67+ alarm = value;
68+ },
69+ },
70+ };
71+ const object = () =>
72+ new ProjectObject(
73+ context as never,
74+ { ABSO_DISK_FIRST: "on", ABSO_ARTIFACTS: "off", KEY_HMAC_SECRET: "invented-test-secret" } as never,
75+ );
76+ t.mock.method(Project, "load", () => {
77+ throw Error("full history replay forbidden");
78+ });
79+ const call = async (op: string, agent: string | undefined, body: unknown) => {
80+ for (let page = 0; page < 100; page++) {
81+ const result = await object().call("project", op, agent, body);
82+ if (result.status !== 503) {
83+ assert.notEqual(result.status, 500, JSON.stringify({ op, result }));
84+ return result;
85+ }
86+ assert.notEqual(alarm, null);
87+ await object().alarm();
88+ }
89+ throw Error("adoption did not finish");
90+ };
91+ assert.equal((await call("init", undefined, { files: { "a.txt": "one\ntwo\n" } })).status, 200);
92+ assert.equal((await call("sync", "alice", {})).status, 200);
93+ assert.equal((await call("record", "alice", { files: { "a.txt": "changed\n" } })).status, 200);
94+ const published = await call("publish", "alice", { message: "Disk first" });
95+ assert.equal(published.status, 200);
96+ assert.equal((published.body as { ok: boolean }).ok, true);
97+ const file = await call("file", undefined, { path: "a.txt", version: "1" });
98+ assert.equal(file.status, 200);
99+ assert.match(JSON.stringify(file.body), /changed/);
100+ const feed = await call("feed", undefined, { from: "0", limit: "2" });
101+ assert.equal(feed.status, 200);
102+ assert.ok((feed.body as { next: number }).next > 0);
103+ assert.deepEqual(await object().agents("project"), ["alice"]);
104+ assert.equal((await object().out()).version, 1);
105+ assert.deepEqual(await object().news("project", [{ from: "bob", issue: null, text: "Hello", to: ["alice"] }]), [
106+ { to: ["alice"] },
107+ ]);
108+ assert.equal((await call("status", undefined, {})).status, 200);
109+ for (const agent of ["alice", "bob"]) assert.equal((await call("sync", agent, {})).status, 200);
110+ assert.equal((await call("record", "alice", { files: { "a.txt": "Alice's change\n" } })).status, 200);
111+ assert.equal((await call("record", "bob", { files: { "a.txt": "Bob's change\n" } })).status, 200);
112+ assert.equal((await call("publish", "alice", { message: "Alice wins first" })).status, 200);
113+ const collision = await call("publish", "bob", { message: "Bob conflicts" });
114+ assert.equal((collision.body as { ok: boolean }).ok, false);
115+ assert.equal((collision.body as { collisions: unknown[] }).collisions.length, 1);
116+ const historical = await call("file", undefined, { path: "a.txt", version: "1" });
117+ assert.equal(historical.status, 200);
118+ assert.match(JSON.stringify(historical.body), /changed/);
119+ const rows = () => ({
120+ log: storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(),
121+ state: storage.db.prepare("SELECT * FROM writer_state ORDER BY namespace,collection,key,part").all(),
122+ });
123+ const stable = rows();
124+ const disabled = new ProjectObject(context as never, { ABSO_DISK_FIRST: "off" } as never);
125+ const held = await disabled.call("project", "record", "alice", { files: { "a.txt": "legacy write\n" } });
126+ assert.equal(held.status, 409);
127+ assert.match(JSON.stringify(held.body), /enable ABSO_DISK_FIRST/);
128+ assert.equal(await disabled.gitSync(), null);
129+ assert.deepEqual(rows(), stable);
130+ assert.equal((await call("status", undefined, {})).status, 200, "re-enabling disk mode preserves the valid root");
131+ const originalAnnounce = EngineProject.prototype.announce;
132+ const failingAnnounce = t.mock.method(
133+ EngineProject.prototype,
134+ "announce",
135+ function (this: EngineProject, ...args: Parameters<EngineProject["announce"]>) {
136+ originalAnnounce.apply(this, args);
137+ throw Error("injected after announce");
138+ },
139+ );
140+ await assert.rejects(
141+ () => object().news("project", [{ from: "bob", issue: null, text: "Must roll back", to: ["alice"] }]),
142+ /injected after announce/,
143+ );
144+ assert.deepEqual(rows(), stable);
145+ failingAnnounce.mock.restore();
146+ const current = object();
147+ onReaders = async () => {
148+ await current.revokeAgentKey("missing-test-key", "127.0.0.1");
149+ };
150+ const revoked = await current.call("project", "record", "alice", { files: { "a.txt": "unauthorized\n" } });
151+ assert.equal(revoked.status, 409);
152+ assert.equal((revoked.body as { retryable: boolean }).retryable, true);
153+ assert.deepEqual(rows(), stable);
154+ const original = EngineProject.prototype.record;
155+ t.mock.method(
156+ EngineProject.prototype,
157+ "record",
158+ function (this: EngineProject, ...args: Parameters<EngineProject["record"]>) {
159+ original.apply(this, args);
160+ throw Error("injected after record");
161+ },
162+ );
163+ await assert.rejects(
164+ () => object().call("project", "record", "alice", { files: { "a.txt": "failed\n" } }),
165+ /injected after record/,
166+ );
167+ assert.deepEqual(rows(), stable);
168+});
MCP
compare on this site's /mcp
CLI
abso diff 157 158 server/test/project-object-disk-first.test.ts --json
JSON
/christopher/abso-core/compare/157..158?file=server%2Ftest%2Fproject-object-disk-first.test.ts&format=json