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 148 to 149

Compare

+227 −0 in 1 place
new file, 227 lines
1+import assert from "node:assert/strict";
2+import { createHash } from "node:crypto";
3+import { mkdtempSync, rmdirSync, unlinkSync } from "node:fs";
4+import { tmpdir } from "node:os";
5+import { join } from "node:path";
6+import { test } from "node:test";
7+import { ProjectionAtomAdoption } from "../experimental/projection-atom-adoption.ts";
8+import { indexAcceptedProjectionAtom, ProjectionAtomIndex } from "../experimental/projection-atom-index.ts";
9+import { ExperimentalMarkerProjection } from "../experimental/projection-marker.ts";
10+import { Graph } from "../graph.ts";
11+import { Repo } from "./helpers.ts";
12+import { SqlState } from "./storage-helper.ts";
14+test("projection atom descriptors exclude inserted text and read one indexed item", () => {
15+ const db = new SqlState();
16+ try {
17+ const repo = Repo.init({ "a.txt": "large text\n".repeat(1000) });
18+ const atom = repo.graph.atom("a0"),
19+ digest = "a".repeat(64);
20+ new ProjectionAtomIndex(db).put(atom, digest, ["a.txt"]);
21+ const descriptor = new ProjectionAtomIndex(db).open("a0", digest);
22+ const before = db.reads;
23+ assert.equal(descriptor.count("inserts"), 1);
24+ assert.deepEqual(descriptor.item("inserts", 0), { file: "a.txt", parent: "^a.txt", lines: 1000 });
25+ assert.equal(db.reads - before, 1);
26+ assert.throws(() => descriptor.item("inserts", 1), /out of range/);
27+ assert.throws(() => new ProjectionAtomIndex(db).open("a0", "b".repeat(64)), /invalid/);
28+ const rows = db.db.prepare("SELECT * FROM state ORDER BY collection,key").all();
29+ assert.ok(!JSON.stringify(rows).includes("large text"));
30+ assert.throws(() => new ProjectionAtomIndex(db).put(atom, digest, ["a.txt"]), /already exists/);
31+ assert.deepEqual(db.db.prepare("SELECT * FROM state ORDER BY collection,key").all(), rows);
32+ } finally {
33+ db.db.close();
34+ }
35+});
37+test("descriptor builds roll back every write and corrupted records refuse", () => {
38+ const db = new SqlState();
39+ try {
40+ const repo = Repo.init({ "a.txt": "one\n" });
41+ const atom = repo.graph.atom("a0"),
42+ digest = "c".repeat(64);
43+ const index = new ProjectionAtomIndex(db);
44+ const writeCount =
45+ atom.deps.length +
46+ atom.inserts.length +
47+ atom.deletes.length +
48+ (atom.attrs?.length ?? 0) +
49+ (atom.moves?.length ?? 0) +
50+ 2;
51+ for (let offset = 1; offset <= writeCount; offset++) {
52+ db.failWrite = db.writes + offset;
53+ assert.throws(() => index.put(atom, digest, ["a.txt"]), /injected/);
54+ assert.deepEqual(db.db.prepare("SELECT * FROM state").all(), []);
55+ }
56+ db.failWrite = Infinity;
57+ index.put(atom, digest, ["a.txt"]);
58+ const name = "projection-atom-index:a0";
59+ const header = db.read(name, "header") as { counts: Record<string, number> };
60+ for (const invalid of [
61+ null,
62+ { ...header, extra: true },
63+ { ...header, counts: { ...header.counts, moves: -1 } },
64+ { ...header, counts: [] },
65+ { ...header, counts: { ...header.counts, inserts: 0 } },
66+ ]) {
67+ db.write(name, "header", invalid);
68+ assert.throws(() => new ProjectionAtomIndex(db).open("a0", digest), /invalid/);
69+ }
70+ db.write(name, "header", header);
71+ const descriptor = new ProjectionAtomIndex(db).open("a0", digest);
72+ const original = db.read(name, "inserts:0") as { value: unknown; checksum: string };
73+ for (const invalid of [
74+ null,
75+ { file: "a.txt", parent: "bad", lines: 1 },
76+ { file: "a.txt", parent: "^a.txt", lines: -1 },
77+ { file: "a.txt", parent: "^a.txt", lines: 1, extra: true },
78+ { ...original, value: { file: "a.txt", parent: "^a.txt", lines: 2 } },
79+ ]) {
80+ db.write(name, "inserts:0", invalid);
81+ assert.throws(() => descriptor.item("inserts", 0), /invalid/);
82+ }
83+ db.write(name, "inserts:0", original);
84+ const run = descriptor.item("inserts", 0);
85+ run.lines = 999;
86+ assert.equal(descriptor.item("inserts", 0).lines, 1);
87+ } finally {
88+ db.db.close();
89+ }
90+});
92+test("descriptor-driven insertion adoption never reads full atom storage", () => {
93+ const db = new SqlState();
94+ try {
95+ const repo = Repo.init({ "a.txt": "one\ntwo\nthree\n" });
96+ const atom = repo.graph.atom("a0"),
97+ digest = "d".repeat(64);
98+ const graph = new Graph({ storage: db });
99+ graph.add(atom);
100+ new ProjectionAtomIndex(db).put(atom, digest, graph.filesOf(atom.id));
101+ const projection = new ExperimentalMarkerProjection(db, graph, "a.txt", "indexed-adoption");
102+ const previous = projection.empty();
103+ const read = db.read.bind(db);
104+ db.read = (collection, key) => {
105+ if (collection === "atoms") throw Error("full atom read forbidden");
106+ return read(collection, key);
107+ };
108+ let pages = 0;
109+ for (;;) {
110+ const descriptor = new ProjectionAtomIndex(db).open(atom.id, digest);
111+ const result = new ProjectionAtomAdoption(projection, "indexed").page({
112+ previous,
113+ atom: descriptor,
114+ remove: false,
115+ source: "e".repeat(64),
116+ maxSteps: 2,
117+ validateSource: () => {},
118+ });
119+ assert.ok(result.steps <= 2);
120+ assert.ok(++pages < 100);
121+ if (result.done) {
122+ assert.deepEqual(projection.lines(result.state), ["a0.0", "a0.1", "a0.2"]);
123+ break;
124+ }
125+ }
126+ assert.ok(pages > 1);
127+ } finally {
128+ db.db.close();
129+ }
130+});
132+test("indexed adoption reopens through dependencies, attributes, deletion and reverse removal without atom reads", () => {
133+ const directory = mkdtempSync(join(tmpdir(), "abso-descriptor-")),
134+ filename = join(directory, "state.sqlite");
135+ let db = new SqlState(filename);
136+ const oracleDb = new SqlState();
137+ try {
138+ const repo = Repo.init({ "a.txt": "one\ntwo\nthree\n", "b.txt": "" });
139+ const atoms = [
140+ repo.graph.atom("a0"),
141+ repo.edit({
142+ edits: [{ file: "a.txt", old: "two\n", new: "replacement\n" }],
143+ attrs: [{ file: "b.txt", name: "exec", value: "1" }],
144+ }),
145+ ];
146+ atoms.push(repo.edit({ move: { file: "a.txt", start: 0, end: 1, to: "b.txt", at: 0 } }));
147+ const graph = new Graph({ storage: db });
148+ for (const atom of atoms) {
149+ graph.add(atom);
150+ new ProjectionAtomIndex(db).put(atom, "a".repeat(64), graph.filesOf(atom.id));
151+ }
152+ const oracle = new ExperimentalMarkerProjection(oracleDb, repo.graph, "a.txt", "oracle");
153+ let expected = oracle.empty();
154+ let state = new ExperimentalMarkerProjection(db, graph, "a.txt", "indexed").empty();
155+ let task = 0;
156+ for (const [atom, remove] of [
157+ ...atoms.map((atom) => [atom, false] as const),
158+ ...atoms.toReversed().map((atom) => [atom, true] as const),
159+ ]) {
160+ const previous = state,
161+ id = String(task++);
162+ let pages = 0,
163+ done = false;
164+ while (!done) {
165+ const read = db.read.bind(db);
166+ db.read = (collection, key) => {
167+ if (collection === "atoms") throw Error("full atom read forbidden");
168+ return read(collection, key);
169+ };
170+ const p = new ExperimentalMarkerProjection(db, new Graph({ storage: db }), "a.txt", "indexed");
171+ const descriptor = new ProjectionAtomIndex(db).open(atom.id, "a".repeat(64));
172+ const result = new ProjectionAtomAdoption(p, id).page({
173+ previous,
174+ atom: descriptor,
175+ remove,
176+ source: "b".repeat(64),
177+ maxSteps: 2,
178+ validateSource: () => {},
179+ });
180+ state = result.state;
181+ done = result.done;
182+ assert.ok(++pages < 100);
183+ db.db.close();
184+ db = new SqlState(filename);
185+ }
186+ expected = remove ? oracle.removeAtom(expected, atom) : oracle.apply(expected, atom);
187+ const actual = new ExperimentalMarkerProjection(db, new Graph({ storage: db }), "a.txt", "indexed");
188+ for (const path of ["a.txt", "b.txt"]) {
189+ assert.deepEqual(actual.lines(state, path), oracle.lines(expected, path));
190+ assert.deepEqual(actual.attrs(state, path), oracle.attrs(expected, path));
191+ }
192+ assert.deepEqual(actual.knownPaths(state), oracle.knownPaths(expected));
193+ }
194+ } finally {
195+ db.db.close();
196+ oracleDb.db.close();
197+ unlinkSync(filename);
198+ rmdirSync(directory);
199+ }
200+});
202+test("accepted graph and descriptor commit together and retry after callback failure", () => {
203+ const db = new SqlState();
204+ try {
205+ const atom = Repo.init({ "a.txt": "one\n" }).graph.atom("a0");
206+ let fail = true;
207+ const graph = new Graph({
208+ storage: db,
209+ indexAcceptedAtom: (accepted, files, storage) => {
210+ indexAcceptedProjectionAtom(accepted, files, storage);
211+ if (fail) throw Error("after descriptor failure");
212+ },
213+ });
214+ const before = db.db.prepare("SELECT * FROM state ORDER BY collection,key").all();
215+ assert.throws(() => graph.add(atom), /after descriptor failure/);
216+ assert.deepEqual(db.db.prepare("SELECT * FROM state ORDER BY collection,key").all(), before);
217+ assert.equal(graph.size, 0);
218+ fail = false;
219+ graph.add(atom);
220+ const digest = createHash("sha256").update(JSON.stringify(atom)).digest("hex");
221+ assert.equal(new ProjectionAtomIndex(db).open("a0", digest).item("inserts", 0).lines, 1);
222+ assert.equal(new Graph({ storage: db }).size, 1);
223+ assert.throws(() => new Graph({ indexAcceptedAtom: indexAcceptedProjectionAtom }), /requires persistent/);
224+ } finally {
225+ db.db.close();
226+ }
227+});
MCP
compare on this site's /mcp
CLI
abso diff 148 149 src/engine/test/projection-atom-index.test.ts --json
JSON
/christopher/abso-core/compare/148..149?file=src%2Fengine%2Ftest%2Fprojection-atom-index.test.ts&format=json