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
135 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 132, 1 version changed it

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

1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { Graph } from "../graph.ts";
4import { withProjectionScratch } from "../projection-scratch.ts";
5import { resolveScratchMoves } from "../relocation-scratch.ts";
6import { CachedStateStorage, StableOverlaySet } from "../state-source.ts";
7import { View } from "../view.ts";
8import { Repo, Rng } from "./helpers.ts";
9import { SqlState } from "./storage-helper.ts";
11test("scratch relocation cycle selection matches View across differing memberships and tied moves", () => {
12 const rng = new Rng(45131);
13 const source = new SqlState();
14 const storage = new CachedStateStorage(source, 4096, 16);
15 try {
16 for (let trial = 0; trial < 24; trial++) {
17 const repo = Repo.init({ "f.txt": Array.from({ length: 24 }, (_, n) => `line ${n}\n`).join(""), "g.txt": "" });
18 const earlier = new Set(repo.shared);
19 for (let j = 0; j < 32; j++) {
20 const offset = rng.int(24);
21 const moves = Array.from({ length: 3 }, (_, index) => {
22 const line = `a0.${(offset + index) % 24}`;
23 const candidate = `a0.${rng.int(24)}`;
24 return { line, parent: candidate === line ? (j % 2 ? "^f.txt" : "^g.txt") : candidate };
25 });
26 const id = repo.graph.nextId();
27 repo.graph.add({
28 id,
29 seq: repo.graph.size,
30 author: "fixture",
31 message: "move",
32 time: j,
33 inserts: [],
34 deletes: [],
35 writes: moves.flatMap((move) => [`P:${move.line}`, `G:${move.parent}`]),
36 reads: [],
37 holds: [],
38 needs: [],
39 resolves: [],
40 reasons: [],
41 deps: ["a0"],
42 moves,
43 });
44 repo.shared.add(id);
45 if (j < 16) earlier.add(id);
46 }
47 for (const membership of [repo.shared, earlier, repo.shared]) {
48 const oracle = new View(repo.graph, membership);
49 const parents = withProjectionScratch(
50 storage,
51 () => {},
52 (scratch) => {
53 const resolved = resolveScratchMoves(repo.graph, (id) => membership.has(id), scratch);
54 return Array.from({ length: 24 }, (_, n) => resolved.parent(`a0.${n}`));
55 },
56 );
57 assert.deepEqual(
58 parents,
59 Array.from({ length: 24 }, (_, n) => oracle.parentOf(`a0.${n}`)),
60 );
61 assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, 0);
62 assert.equal(storage.retainedEntries, 0);
63 }
64 if (trial < 4) {
65 const persisted = new SqlState();
66 try {
67 const indexed = new Graph({ storage: new CachedStateStorage(persisted, 8192, 32) });
68 for (const id of repo.shared) indexed.add(repo.graph.atom(id));
69 const before = persisted.db.prepare("SELECT * FROM state ORDER BY collection, key").all();
70 // Restart before projecting, then alternate membership on the same graph.
71 const reopened = new Graph({ storage: new CachedStateStorage(persisted, 8192, 32) });
72 for (const membership of [repo.shared, earlier, repo.shared]) {
73 const expected = new View(repo.graph, membership);
74 const actual = new View(reopened, new StableOverlaySet(membership));
75 for (const path of ["f.txt", "g.txt"]) {
76 assert.equal(actual.text(path), expected.text(path));
77 assert.deepEqual(actual.sequence(path), expected.sequence(path));
78 }
79 for (const n of [0, 11, 23]) {
80 assert.equal(actual.parentOf(`a0.${n}`), expected.parentOf(`a0.${n}`));
81 assert.equal(actual.fileOf(`a0.${n}`), expected.fileOf(`a0.${n}`));
82 }
83 assert.deepEqual(persisted.db.prepare("SELECT * FROM state ORDER BY collection, key").all(), before);
84 }
85 } finally {
86 persisted.db.close();
87 }
88 }
89 }
90 } finally {
91 source.db.close();
92 }
93});
95test("scratch relocation write failure rolls back without changing canonical graph state", () => {
96 const repo = Repo.init({ f: "one\ntwo\n" });
97 const source = new SqlState();
98 try {
99 repo.graph.add({
100 id: "a1",
101 seq: 1,
102 author: "fixture",
103 message: "move",
104 time: 1,
105 inserts: [],
106 deletes: [],
107 writes: ["P:a0.0", "G:a0.1"],
108 reads: [],
109 holds: [],
110 needs: [],
111 resolves: [],
112 reasons: [],
113 deps: ["a0"],
114 moves: [{ line: "a0.0", parent: "a0.1" }],
115 });
116 repo.shared.add("a1");
117 const before = { size: repo.graph.size, lines: repo.graph.lineCount };
118 source.failWrite = 3;
119 assert.throws(
120 () =>
121 withProjectionScratch(
122 source,
123 () => {},
124 (scratch) => {
125 resolveScratchMoves(repo.graph, (id) => repo.shared.has(id), scratch);
126 },
127 ),
128 /injected storage failure/,
129 );
130 assert.deepEqual({ size: repo.graph.size, lines: repo.graph.lineCount }, before);
131 assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, 0);
132 } finally {
133 source.db.close();
134 }
135});