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