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/worker/project/test/queue.test.ts?format=json
205 linesFirst import 94% · rename 5% · pinch 1%Last change rename in version 100, 3 versions changed it
1// The merge queue's engine half (#39): with `"queue": true` in shared's
2// avcs.json, a publish to shared waits in the queue instead of landing. It is
3// refused at once when it collides with shared or anything queued ahead of
4// it, so the queue only holds publishes that merge cleanly. A checker takes
5// the first few as a candidate, and lands them or pulls one; what it builds
6// on comes out with it.
7
8import assert from "node:assert/strict";
9import { test } from "node:test";
10import { type EditBody, MemoryStore, Project } from "../project.ts";
12function edit(project: Project, agent: string, body: EditBody, time: number): void {
13 const a = project.edit(agent, body, time);
14 if (!a.ok) throw new Error(`edit by ${agent} was out of step`);
17function setup(queue = true) {
18 const store = new MemoryStore();
19 const project = new Project(store);
20 project.checkerReady = true;
21 project.init(
22 {
23 "avcs.json": `${JSON.stringify({ queue })}\n`,
24 "a.ts": "export const a = 1;\n",
25 "b.ts": "export const b = 1;\n",
26 "c.ts": "export const c = 1;\n",
27 },
28 0,
29 );
30 for (const a of ["one", "two", "three"]) project.sync(a);
31 return { project, store };
34/** A publish that must be queued: its id. */
35function queued(project: Project, agent: string, message: string, time: number): number {
36 const a = project.publish(agent, message, time);
37 assert.ok(a.ok && "queued" in a, JSON.stringify(a));
38 return a.queued;
41test("without the queue a push lands as before", () => {
42 const { project } = setup(false);
43 edit(project, "one", { file: "a.ts", old: "1", new: "2" }, 1);
44 const a = project.publish("one", "a is 2", 2);
45 assert.ok(a.ok && "version" in a && a.version === 1);
46 assert.deepEqual(project.queue().items, []);
47});
49test("a queued push waits, shows in status and the candidate, and lands as its own version with the same tree", () => {
50 const { project } = setup();
51 edit(project, "one", { file: "a.ts", old: "1", new: "2" }, 1);
52 const a = project.publish("one", "a is 2", 2);
53 assert.deepEqual(a, { ok: true, queued: 1, position: 1, ahead: 0, changes: 1 });
54 assert.equal(project.version, 0, "shared has not moved");
55 assert.equal(project.files()["a.ts"], "export const a = 1;\n");
56 assert.deepEqual(project.status().agents.find((x) => x.agent === "one")?.queued, [1]);
57 assert.deepEqual(
58 project.queue().items.map((q) => [q.id, q.agent, q.message, q.changes, q.checking]),
59 [[1, "one", "a is 2", 1, false]],
60 );
62 const candidate = project.queueCandidate(1);
63 assert.deepEqual(candidate.ids, [1]);
64 assert.equal(candidate.files["a.ts"], "export const a = 2;\n");
65 assert.notEqual(candidate.tree, project.treeAt(0));
67 assert.deepEqual(project.queueChecking(1), [1]);
68 assert.equal(project.queue().items[0]!.checking, true);
69 assert.deepEqual(project.queueLand(1, 3).versions, [{ id: 1, version: 1 }]);
70 assert.equal(project.version, 1);
71 assert.equal(project.treeAt(1), candidate.tree);
72 assert.deepEqual(project.queue().items, []);
73 assert.equal(project.status().agents.find((x) => x.agent === "one")?.queued, undefined);
74 assert.ok(project.news("one").news.some((n) => n.includes('Your queued push "a is 2" landed as version 1')));
75});
77test("a push that changes lines queued ahead of it is refused at once, naming the queued one", () => {
78 const { project } = setup();
79 edit(project, "one", { file: "a.ts", old: "1", new: "2" }, 1);
80 queued(project, "one", "a is 2", 2);
81 edit(project, "two", { file: "a.ts", old: "1", new: "3" }, 3);
82 const a = project.publish("two", "a is 3", 4);
83 assert.equal(a.ok, false);
84 assert.ok(!a.ok);
85 assert.equal(a.collisions[0]!.kind, "lines");
86 assert.deepEqual([a.collisions[0]!.theirs.agent, a.collisions[0]!.theirs.queued], ["one", 1]);
87 assert.equal(project.queue().items.length, 1);
88});
90test("a name defined both in a queued push and in a new one is refused", () => {
91 const { project } = setup();
92 edit(
93 project,
94 "one",
95 { file: "a.ts", old: "export const a = 1;\n", new: "export const a = 1;\nexport function parse() {}\n" },
96 1,
97 );
98 queued(project, "one", "parse", 2);
99 edit(
100 project,
101 "two",
102 { file: "a.ts", old: "export const a = 1;\n", new: "export function parse() {}\nexport const a = 1;\n" },
103 3,
104 );
105 const a = project.publish("two", "parse too", 4);
106 assert.ok(!a.ok);
107 assert.ok(
108 a.collisions.some((c) => c.name === "parse" && c.theirs.queued === 1),
109 JSON.stringify(a.collisions),
110 );
111});
113test("pushes to different files queue behind each other and land in order", () => {
114 const { project } = setup();
115 edit(project, "one", { file: "a.ts", old: "1", new: "2" }, 1);
116 queued(project, "one", "a is 2", 2);
117 edit(project, "two", { file: "b.ts", old: "1", new: "2" }, 3);
118 const b = project.publish("two", "b is 2", 4);
119 assert.deepEqual(b, { ok: true, queued: 2, position: 2, ahead: 1, changes: 1 });
120 const both = project.queueCandidate(2);
121 assert.deepEqual([both.files["a.ts"], both.files["b.ts"]], ["export const a = 2;\n", "export const b = 2;\n"]);
122 assert.deepEqual(project.queueLand(2, 5).versions, [
123 { id: 1, version: 1 },
124 { id: 2, version: 2 },
125 ]);
126 assert.equal(project.treeAt(2), both.tree);
127 assert.deepEqual(
128 project
129 .log()
130 .versions.map((v) => v.message)
131 .slice(0, 2),
132 ["b is 2", "a is 2"],
133 );
134});
136test("a failed push is turned back with the check's output; its changes stay in the draft and can be queued again", () => {
137 const { project } = setup();
138 edit(project, "one", { file: "a.ts", old: "1", new: "2" }, 1);
139 queued(project, "one", "a is 2", 2);
140 assert.deepEqual(project.queuePull(1, "failed", 3, "line 1\nnot ok 1 - a is 1\n").pulled, [1]);
141 assert.deepEqual(project.queue().items, []);
142 const news = project.news("one").news.join("\n");
143 assert.ok(news.includes("was turned back: the project's check failed on it"), news);
144 assert.ok(news.includes("not ok 1 - a is 1"));
145 assert.equal(project.diff("one").files.length, 1, "the change is still in the draft");
146 assert.equal(queued(project, "one", "a is 2, again", 4), 2);
147});
149test("a queued push built on a pulled one comes out with it", () => {
150 const { project } = setup();
151 edit(project, "one", { file: "a.ts", old: "1", new: "2" }, 1);
152 queued(project, "one", "a is 2", 2);
153 edit(project, "one", { file: "a.ts", old: "2", new: "22" }, 3);
154 assert.deepEqual(project.publish("one", "a is 22", 4), { ok: true, queued: 2, position: 2, ahead: 1, changes: 1 });
155 edit(project, "two", { file: "b.ts", old: "1", new: "2" }, 5);
156 queued(project, "two", "b is 2", 6);
157 assert.deepEqual(project.queuePull(1, "failed", 7).pulled, [1, 2]);
158 assert.deepEqual(
159 project.queue().items.map((q) => q.id),
160 [3],
161 );
162 assert.ok(project.news("one").news.some((n) => n.includes("it builds on queued push 1, which was turned back")));
163});
165test("an agent can withdraw its queued pushes while they wait, not while they are being checked", () => {
166 const { project } = setup();
167 edit(project, "one", { file: "a.ts", old: "1", new: "2" }, 1);
168 queued(project, "one", "a is 2", 2);
169 project.queueChecking(1);
170 assert.throws(() => project.unqueue("one", 3), /being checked/);
171 project.queueChecking(0);
172 assert.deepEqual(project.unqueue("one", 4).pulled, [1]);
173 assert.throws(() => project.unqueue("one", 5), /nothing queued/);
174});
176test("landing leaves edits made after queueing in the agent's draft", () => {
177 const { project } = setup();
178 edit(project, "one", { file: "a.ts", old: "1", new: "2" }, 1);
179 queued(project, "one", "a is 2", 2);
180 edit(project, "one", { file: "b.ts", old: "1", new: "5" }, 3);
181 project.queueLand(1, 4);
182 assert.equal(project.files()["a.ts"], "export const a = 2;\n");
183 assert.deepEqual(
184 project.diff("one").files.map((f) => f.path),
185 ["b.ts"],
186 );
187});
189test("the queue is rebuilt from the log", () => {
190 const { project, store } = setup();
191 edit(project, "one", { file: "a.ts", old: "1", new: "2" }, 1);
192 queued(project, "one", "a is 2", 2);
193 edit(project, "two", { file: "b.ts", old: "1", new: "2" }, 3);
194 queued(project, "two", "b is 2", 4);
195 project.queuePull(1, "withdrawn", 5);
196 const again = Project.load(store.entries, new MemoryStore());
197 again.checkerReady = true;
198 assert.deepEqual(
199 again.queue().items.map((q) => [q.id, q.agent]),
200 [[2, "two"]],
201 );
202 assert.equal(again.queueCandidate(1).tree, project.queueCandidate(1).tree);
203 edit(again, "three", { file: "c.ts", old: "1", new: "2" }, 6);
204 assert.equal(queued(again, "three", "c is 2", 7), 3, "ids keep counting after a reload");
205});