- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/queue-driver.test.ts?draft=login-timeout&format=json
194 linesFirst import 100%0 versions changed it
1// The merge queue's checker loop (#39) on a real project, with the check as a
2// function of the candidate's files: batches land whole, a failing batch is
3// halved to the publish that turned it red, a failure must happen twice,
4// box failures pause instead of blaming anyone, and a red shared lands only
5// what passes.
7import assert from "node:assert/strict";
8import { test } from "node:test";
9import { MemoryStore, Project } from "../project.ts";
10import { type Candidate, type Checker, type CheckResult, queueRound } from "../queue-driver.ts";
12const AGENTS = ["b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b10"];
14function setup() {
15 const project = new Project(new MemoryStore());
16 project.checkerReady = true;
17 const files: Record<string, string> = { "avcs.json": '{"queue": true}\n' };
18 for (const a of AGENTS) files[`${a}.ts`] = `export const ${a} = 0;\n`;
19 project.init(files, 0);
20 for (const a of AGENTS) project.sync(a);
21 return project;
22}
24/** Agent `a` publishes `value` into its own file; it must be queued. */
25function queue(project: Project, a: string, value: string, time = 1): number {
26 const e = project.edit(a, { file: `${a}.ts`, old: "= 0", new: `= ${value}` }, time);
27 assert.ok(e.ok);
28 const p = project.publish(a, `${a} is ${value}`, time);
29 assert.ok(p.ok && "queued" in p, JSON.stringify(p));
30 return p.queued;
31}
33/** A check that fails when any file says BAD, counting its runs. */
34function checker(result?: (c: Candidate, run: number) => CheckResult): Checker & { runs: Candidate[] } {
35 const runs: Candidate[] = [];
36 return {
37 runs,
38 async check(c) {
39 runs.push(c);
40 if (result) return result(c, runs.length);
41 return Object.values(c.files).some((t) => t.includes("BAD"))
42 ? { kind: "fail", output: "not ok 1 - BAD" }
43 : { kind: "pass" };
44 },
45 };
46}
48test("an empty queue is idle and runs no check", async () => {
49 const c = checker();
50 assert.deepEqual(await queueRound(setup(), c), { kind: "idle" });
51 assert.equal(c.runs.length, 0);
52});
54test("a passing batch lands whole, each publish its own version, in one check", async () => {
55 const project = setup();
56 for (const a of AGENTS.slice(0, 3)) queue(project, a, "1");
57 const c = checker();
58 const r = await queueRound(project, c);
59 assert.deepEqual(r, {
60 kind: "landed",
61 versions: [
62 { id: 1, version: 1 },
63 { id: 2, version: 2 },
64 { id: 3, version: 3 },
65 ],
66 checks: 1,
67 });
68 assert.equal(project.treeAt(3), c.runs[0]!.tree);
69 assert.deepEqual(project.queueChecking(0), []);
70});
72test("a batch is capped; the rest waits for the next round", async () => {
73 const project = setup();
74 for (const a of AGENTS) queue(project, a, "1");
75 const r = await queueRound(project, checker(), { cap: 8 });
76 assert.equal(r.kind === "landed" && r.versions.length, 8);
77 assert.deepEqual(
78 project.queue().items.map((q) => q.id),
79 [9, 10],
80 );
81});
83test("a failing batch is halved to the first publish that fails; the prefix before it lands, the rest waits", async () => {
84 const project = setup();
85 for (const [i, a] of AGENTS.slice(0, 8).entries()) queue(project, a, i === 5 ? '"BAD"' : "1");
86 const c = checker();
87 const r = await queueRound(project, c);
88 // 8 fails; 4 passes; 6 fails; 5 passes; 6 fails again: 5 checks, 1 + log2(8) + 1.
89 assert.deepEqual(
90 c.runs.map((x) => x.ids.length),
91 [8, 4, 6, 5, 6],
92 );
93 assert.equal(r.kind, "pulled");
94 assert.ok(r.kind === "pulled");
95 assert.deepEqual(
96 r.versions.map((v) => v.id),
97 [1, 2, 3, 4, 5],
98 );
99 assert.deepEqual(r.pulled, [6]);
100 assert.equal(r.output, "not ok 1 - BAD");
101 assert.deepEqual(
102 project.queue().items.map((q) => q.id),
103 [7, 8],
104 );
105 assert.ok(project.news("b6").news.some((n) => n.includes("not ok 1 - BAD")));
106 assert.deepEqual(await queueRound(project, c), {
107 kind: "landed",
108 versions: [
109 { id: 7, version: 6 },
110 { id: 8, version: 7 },
111 ],
112 checks: 1,
113 });
114});
116test("a lone failing publish is pulled after failing twice", async () => {
117 const project = setup();
118 queue(project, "b1", '"BAD"');
119 const c = checker();
120 const r = await queueRound(project, c);
121 assert.equal(c.runs.length, 2);
122 assert.ok(r.kind === "pulled" && r.pulled[0] === 1 && r.versions.length === 0);
123});
125test("a failure that does not happen again pulls nobody, and the prefix lands", async () => {
126 const project = setup();
127 for (const a of AGENTS.slice(0, 2)) queue(project, a, "1");
128 // Fails once, then passes: flaky.
129 const c = checker((_, run) => (run === 1 ? { kind: "fail", output: "flake" } : { kind: "pass" }));
130 const r = await queueRound(project, c);
131 // 2 fails; 1 passes; 2 passes on the confirming run, so both land.
132 assert.deepEqual(r, {
133 kind: "landed",
134 versions: [
135 { id: 1, version: 1 },
136 { id: 2, version: 2 },
137 ],
138 checks: 3,
139 });
140});
142test("a box failure is retried once; twice in a row pauses the queue and blames nobody", async () => {
143 const project = setup();
144 queue(project, "b1", "1");
145 const once = checker((_, run) => (run === 1 ? { kind: "box", reason: "box lost" } : { kind: "pass" }));
146 assert.equal((await queueRound(project, once)).kind, "landed");
147 queue(project, "b2", "1", 2);
148 const always = checker(() => ({ kind: "box", reason: "no exit code" }));
149 assert.deepEqual(await queueRound(project, always), { kind: "paused", reason: "no exit code", checks: 2 });
150 assert.deepEqual(
151 project.queue().items.map((q) => [q.id, q.checking]),
152 [[2, false]],
153 );
154});
156test("while shared is red, prefixes grow one publish at a time; the first that passes lands, nobody is pulled", async () => {
157 const project = setup();
158 queue(project, "b1", "1");
159 queue(project, "b2", "2");
160 queue(project, "b3", "3");
161 // Shared is red until b2's change is in: b1 alone doesn't fix it.
162 const c = checker((cand) =>
163 cand.files["b2.ts"]!.includes("= 2") ? { kind: "pass" } : { kind: "fail", output: "still red" },
164 );
165 assert.deepEqual(await queueRound(project, c, { sharedRed: true }), {
166 kind: "landed",
167 versions: [
168 { id: 1, version: 1 },
169 { id: 2, version: 2 },
170 ],
171 checks: 2,
172 });
173 assert.deepEqual(
174 c.runs.map((x) => x.ids),
175 [[1], [1, 2]],
176 );
177 assert.deepEqual(
178 project.queue().items.map((q) => q.id),
179 [3],
180 );
181});
183test("while shared is red and nothing fixes it, the queue holds everyone", async () => {
184 const project = setup();
185 queue(project, "b1", "1");
186 queue(project, "b2", "2");
187 const c = checker(() => ({ kind: "fail", output: "still red" }));
188 assert.deepEqual(await queueRound(project, c, { sharedRed: true }), { kind: "held", checks: 2, output: "still red" });
189 assert.deepEqual(
190 project.queue().items.map((q) => q.id),
191 [1, 2],
192 "nobody pulled",
193 );
194});