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

+285 −4 in 6 places
Lines 1–10 at 162 (were 1–8 at 161)
11import assert from "node:assert/strict";
22import { registerHooks } from "node:module";
33import { test } from "node:test";
4+import git from "isomorphic-git";
45import { Project } from "../../worker/project/project.ts";
56import { EngineProject } from "../src/engine-project.ts";
7+import { MemoryFS } from "../src/memory-fs.ts";
68import { memoryStorage } from "./helpers.ts";
79
810const hooks = registerHooks({
Lines 47–54 at 162 (were 45–50 at 161)
4547 const values = new Map<string, unknown>();
4648 let alarm: number | null = null;
4749 let onReaders: (() => Promise<void>) | undefined;
50+ let failGitAcknowledgement = false;
51+ let failNamePersistence = false;
4852 const context = {
4953 id: { toString: () => "project-1" },
5054 storage: {
Lines 62–75 at 162 (were 58–63 at 161)
5862 return values.get(key);
5963 },
6064 put: async (key: string | Record<string, unknown>, value?: unknown) => {
65+ if (key === "name" && failNamePersistence) {
66+ failNamePersistence = false;
67+ throw Error("injected name persistence failure");
68+ }
69+ if (failGitAcknowledgement && typeof key === "object" && "git:head" in key) {
70+ failGitAcknowledgement = false;
71+ throw Error("injected Git cursor persistence failure");
72+ }
6173 if (typeof key === "string") values.set(key, value);
6274 else for (const [name, v] of Object.entries(key)) values.set(name, v);
6375 },
Lines 88–96 at 162 (were 76–84 at 161)
7688 t.mock.method(Project, "load", () => {
7789 throw Error("full history replay forbidden");
7890 });
79− const call = async (op: string, agent: string | undefined, body: unknown) => {
91+ const call = async (op: string, agent: string | undefined, body: unknown, requestId?: string) => {
8092 for (let page = 0; page < 100; page++) {
81− const result = await object().call("project", op, agent, body);
93+ const result = await object().call("project", op, agent, body, false, undefined, undefined, requestId);
8294 if (result.status !== 503) {
8395 assert.notEqual(result.status, 500, JSON.stringify({ op, result }));
8496 return result;
Lines 100–213 at 162 (were 88–99 at 161)
88100 }
89101 throw Error("adoption did not finish");
90102 };
91− assert.equal((await call("init", undefined, { files: { "a.txt": "one\ntwo\n" } })).status, 200);
103+ const initId = "00000000-0000-4000-8000-000000000008";
104+ const initBody = { files: { "a.txt": "one\ntwo\n" } };
105+ failNamePersistence = true;
106+ await assert.rejects(
107+ () => object().call("project", "init", undefined, initBody, false, undefined, undefined, initId),
108+ /name persistence failure/,
109+ );
110+ const initializedLog = storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all();
111+ assert.equal((await call("init", undefined, initBody, initId)).status, 200);
112+ assert.deepEqual(storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(), initializedLog);
92113 assert.equal((await call("sync", "alice", {})).status, 200);
93− assert.equal((await call("record", "alice", { files: { "a.txt": "changed\n" } })).status, 200);
114+ const requestId = "00000000-0000-4000-8000-000000000007";
115+ const recordBody = { files: { "a.txt": "changed\n" } };
116+ failNamePersistence = true;
117+ await assert.rejects(
118+ () => object().call("project", "record", "alice", recordBody, false, undefined, undefined, requestId),
119+ /name persistence failure/,
120+ );
121+ const recordedLog = storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all();
122+ assert.equal(
123+ (await object().call("project", "record", "alice", recordBody, false, undefined, undefined, requestId)).status,
124+ 200,
125+ );
126+ assert.deepEqual(storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(), recordedLog);
127+ assert.equal(
128+ (
129+ await object().call(
130+ "project",
131+ "record",
132+ "alice",
133+ { files: { "a.txt": "different\n" } },
134+ false,
135+ undefined,
136+ undefined,
137+ requestId,
138+ )
139+ ).status,
140+ 409,
141+ );
94142 const published = await call("publish", "alice", { message: "Disk first" });
95143 assert.equal(published.status, 200);
96144 assert.equal((published.body as { ok: boolean }).ok, true);
145+ const issued = await object().issueAgentKey("alice", "test-address", null, "writer");
146+ assert.ok("key" in issued);
147+ const keyCall = { presented: issued.key, address: "test-address", write: true, route: "record" };
148+ const crossPrincipal = await object().call(
149+ "project",
150+ "record",
151+ "alice",
152+ recordBody,
153+ false,
154+ undefined,
155+ keyCall,
156+ requestId,
157+ );
158+ assert.equal(crossPrincipal.status, 200, "each checked principal owns its own request ID namespace");
159+ const principalReceipts = storage.db
160+ .prepare("SELECT DISTINCT key FROM writer_state WHERE collection = 'runtime:receipts' AND key LIKE ?")
161+ .all(`http:%:${requestId}`);
162+ assert.equal(principalReceipts.length, 2);
163+ const beforeSharedReplay = storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all();
164+ assert.equal((await call("record", "alice", recordBody, requestId)).status, 200);
165+ assert.deepEqual(storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(), beforeSharedReplay);
166+ const keyRequestId = "00000000-0000-4000-8000-000000000010";
167+ assert.equal(
168+ (await object().call("project", "record", "alice", recordBody, false, undefined, keyCall, keyRequestId)).status,
169+ 200,
170+ );
171+ await object().revokeAgentKey(issued.id, "test-address");
172+ const beforeRevokedRetry = storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all();
173+ const revokedRetry = await object().call(
174+ "project",
175+ "record",
176+ "alice",
177+ recordBody,
178+ false,
179+ undefined,
180+ keyCall,
181+ keyRequestId,
182+ );
183+ assert.equal(revokedRetry.status, 403);
184+ assert.deepEqual(storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(), beforeRevokedRetry);
185+ const readerId = "00000000-0000-4000-8000-000000000009";
186+ failNamePersistence = true;
187+ await assert.rejects(
188+ () => object().call("project", "reader", "alice", { reader: true }, false, undefined, undefined, readerId),
189+ /name persistence failure/,
190+ );
191+ const nowRefused = await object().call(
192+ "project",
193+ "record",
194+ "alice",
195+ recordBody,
196+ false,
197+ undefined,
198+ undefined,
199+ requestId,
200+ );
201+ assert.equal(
202+ (nowRefused.body as { ok: boolean }).ok,
203+ false,
204+ "current reader authority overrides an earlier successful receipt",
205+ );
206+ assert.equal(
207+ (await object().call("project", "reader", "alice", { reader: true }, false, undefined, undefined, readerId)).status,
208+ 200,
209+ );
210+ assert.equal((await call("reader", "alice", { reader: false })).status, 200);
97211 const file = await call("file", undefined, { path: "a.txt", version: "1" });
98212 assert.equal(file.status, 200);
99213 assert.match(JSON.stringify(file.body), /changed/);
Lines 279–449 at 162 (were 165–168 at 161)
165279 /injected after record/,
166280 );
167281 assert.deepEqual(rows(), stable);
282+ // Exercise the real alarm and commit builder; only remote transport is mocked.
283+ t.mock.method(git, "listServerRefs", async () => []);
284+ t.mock.method(git, "clone", async () => {
285+ throw Error("empty test repository");
286+ });
287+ let pushed = 0;
288+ let remoteFiles: MemoryFS | undefined;
289+ t.mock.method(git, "push", async (options: Parameters<typeof git.push>[0]) => {
290+ const oid = await git.resolveRef({ fs: options.fs, dir: options.dir, ref: "main" });
291+ const commit = await git.readCommit({ fs: options.fs, dir: options.dir, oid });
292+ assert.match(commit.commit.message, /abso-version: 2/);
293+ const blob = await git.readBlob({ fs: options.fs, dir: options.dir, oid, filepath: "a.txt" });
294+ assert.equal(new TextDecoder().decode(blob.blob), "Alice's change\n");
295+ pushed++;
296+ remoteFiles = options.fs as MemoryFS;
297+ return { ok: true };
298+ });
299+ const artifacts = {
300+ get: async () => ({
301+ info: async () => ({ remote: "https://artifacts.invalid/test" }),
302+ createToken: async () => ({ plaintext: "invented-test-token" }),
303+ [Symbol.dispose]() {},
304+ }),
305+ };
306+ const bound = () => new ProjectObject(context as never, { ABSO_DISK_FIRST: "on", ARTIFACTS: artifacts } as never);
307+ await bound().alarm();
308+ assert.equal(pushed, 1);
309+ assert.equal((await bound().out()).pushed?.version, 2);
310+ assert.deepEqual(rows(), stable, "pushing shared does not mutate canonical project state");
311+ await bound().alarm();
312+ assert.equal(pushed, 1, "a cold alarm does not push the same version again");
313+ assert.ok(remoteFiles);
314+ const remote = remoteFiles;
315+ const parent = await git.resolveRef({ fs: remote, dir: "/repo", ref: "main" });
316+ const blob = await git.writeBlob({ fs: remote, dir: "/repo", blob: new TextEncoder().encode("From Git\n") });
317+ const tree = await git.writeTree({
318+ fs: remote,
319+ dir: "/repo",
320+ tree: [{ mode: "100644", path: "a.txt", oid: blob, type: "blob" }],
321+ });
322+ const incoming = await git.commit({
323+ fs: remote,
324+ dir: "/repo",
325+ tree,
326+ parent: [parent],
327+ message: "Plain Git edit",
328+ author: { name: "Git writer", email: "test@example.invalid" },
329+ });
330+ await git.writeRef({ fs: remote, dir: "/repo", ref: "main", value: incoming, force: true });
331+ t.mock.method(git, "listServerRefs", async () => [{ ref: "refs/heads/main", oid: incoming }]);
332+ t.mock.method(git, "clone", async (options: Parameters<typeof git.clone>[0]) => {
333+ (options.fs as MemoryFS).entries = structuredClone(remote.entries);
334+ });
335+ const beforeIntake = rows();
336+ const originalIngest = EngineProject.prototype.ingest;
337+ const failedIngest = t.mock.method(
338+ EngineProject.prototype,
339+ "ingest",
340+ function (this: EngineProject, ...args: Parameters<EngineProject["ingest"]>) {
341+ originalIngest.apply(this, args);
342+ throw Error("injected after Git intake");
343+ },
344+ );
345+ await assert.rejects(() => bound().gitSync(), /injected after Git intake/);
346+ assert.deepEqual(rows(), beforeIntake);
347+ assert.equal((await bound().out()).git.head, parent, "failed intake must not acknowledge the remote head");
348+ failedIngest.mock.restore();
349+ failGitAcknowledgement = true;
350+ await assert.rejects(() => bound().gitSync(), /Git cursor persistence failure/);
351+ const committedIntake = rows();
352+ assert.equal((await bound().out()).version, 3);
353+ assert.equal((await bound().out()).git.head, parent);
354+ const intake = await bound().gitSync();
355+ assert.deepEqual(rows(), committedIntake, "retry after cursor failure returns the atomic intake receipt");
356+ assert.ok(intake);
357+ assert.equal(intake?.head, incoming);
358+ assert.equal(intake?.commits, 1);
359+ assert.equal((intake.answer as { landed: boolean }).landed, true, JSON.stringify(intake));
360+ const imported = await call("file", undefined, { path: "a.txt", version: "3" });
361+ assert.equal(imported.status, 200);
362+ assert.match(JSON.stringify(imported.body), /From Git/);
363+ const afterIntake = rows();
364+ assert.equal(await bound().gitSync(), null);
365+ assert.deepEqual(rows(), afterIntake, "cold intake does not apply the same commit twice");
168366});
368+for (const disk of [false, true]) {
369+ test(`Git refusal permits a later fixing commit with disk mode ${disk}`, async (t) => {
370+ const storage = memoryStorage();
371+ t.after(() => storage.db.close());
372+ const values = new Map<string, unknown>();
373+ const context = {
374+ id: { toString: () => "refusal" },
375+ storage: {
376+ ...storage,
377+ get: async (key: string) => values.get(key),
378+ put: async (key: string | Record<string, unknown>, value?: unknown) => {
379+ if (typeof key === "string") values.set(key, value);
380+ else for (const [k, v] of Object.entries(key)) values.set(k, v);
381+ },
382+ getAlarm: async () => null,
383+ setAlarm: async () => {},
384+ },
385+ };
386+ const artifacts = {
387+ get: async () => ({
388+ info: async () => ({ remote: "https://artifacts.invalid/refusal" }),
389+ createToken: async () => ({ plaintext: "invented-test-token" }),
390+ [Symbol.dispose]() {},
391+ }),
392+ };
393+ const object = () =>
394+ new ProjectObject(context as never, { ABSO_DISK_FIRST: disk ? "on" : "off", ARTIFACTS: artifacts } as never);
395+ const call = async (op: string, body: unknown) => {
396+ for (let page = 0; page < 100; page++) {
397+ const result = await object().call("refusal", op, undefined, body);
398+ if (result.status !== 503) return result;
399+ }
400+ throw Error("preparation did not finish");
401+ };
402+ assert.equal((await call("init", { files: { "a.txt": "original\n" } })).status, 200);
403+ assert.equal((await call("status", {})).status, 200);
404+ const remote = new MemoryFS();
405+ await git.init({ fs: remote, dir: "/repo", defaultBranch: "main" });
406+ const commit = async (files: Record<string, string>, message: string, parent: string[]) => {
407+ const entries = [];
408+ for (const [path, text] of Object.entries(files))
409+ entries.push({
410+ mode: "100644",
411+ path,
412+ type: "blob" as const,
413+ oid: await git.writeBlob({ fs: remote, dir: "/repo", blob: new TextEncoder().encode(text) }),
414+ });
415+ const tree = await git.writeTree({ fs: remote, dir: "/repo", tree: entries });
416+ const oid = await git.commit({
417+ fs: remote,
418+ dir: "/repo",
419+ tree,
420+ parent,
421+ message,
422+ author: { name: "Git writer", email: "test@example.invalid" },
423+ });
424+ await git.writeRef({ fs: remote, dir: "/repo", ref: "main", value: oid, force: true });
425+ return oid;
426+ };
427+ const base = await commit({ "a.txt": "original\n" }, "Initial\n\nabso-version: 0", []);
428+ values.set("git:head", base);
429+ let head = await commit({ "A.TXT": "clash\n", "a.txt": "original\n" }, "Refused", [base]);
430+ t.mock.method(git, "listServerRefs", async () => [{ ref: "refs/heads/main", oid: head }]);
431+ t.mock.method(git, "clone", async (options: Parameters<typeof git.clone>[0]) => {
432+ (options.fs as MemoryFS).entries = structuredClone(remote.entries);
433+ });
434+ const refused = await object().gitSync();
435+ assert.ok(refused);
436+ assert.match(JSON.stringify(refused.answer), /one file|clash/i);
437+ assert.equal(values.get("git:head"), head);
438+ assert.equal(await object().gitSync(), null);
439+ head = await commit({ "a.txt": "fixed\n" }, "Fixed", [head]);
440+ const fixed = await object().gitSync();
441+ assert.ok(fixed);
442+ assert.equal((fixed.answer as { landed: boolean }).landed, true, JSON.stringify(fixed));
443+ assert.equal(values.get("git:head"), head);
444+ assert.match(
445+ JSON.stringify((await call("file", { path: "a.txt", version: String((await object().out()).version) })).body),
446+ /fixed/,
447+ );
448+ });
449+}
MCP
compare on this site's /mcp
CLI
abso diff 161 162 server/test/project-object-disk-first.test.ts --json
JSON
/christopher/abso-core/compare/161..162?file=server%2Ftest%2Fproject-object-disk-first.test.ts&format=json