- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/test/order.test.ts?format=json
68 linesFirst import 94% · pinch 4% · rename 1%Last change rename in version 100, 3 versions changed it
1// Randomized: agents edit lines far enough apart that the engine's rule finds
2// no collision. Every publish order must go through and give the same files,
3// equal to doing all the edits to plain text.
5import assert from "node:assert/strict";
6import { test } from "node:test";
7import { MemoryStore, Project } from "../project.ts";
9function rng(seed: number): () => number {
10 let s = seed >>> 0;
11 return () => {
12 s = (s + 0x6d2b79f5) >>> 0;
13 let t = s;
14 t = Math.imul(t ^ (t >>> 15), t | 1);
15 t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
16 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
17 };
18}
20interface Plan {
21 agent: string;
22 start: number;
23 end: number;
24 lines: string[];
25}
27test("changes that do not collide give the same files in every push order", () => {
28 for (let seed = 1; seed <= 60; seed++) {
29 const rand = rng(seed);
30 const pick = (n: number): number => Math.floor(rand() * n);
31 const count = 12 + pick(30);
32 const base = Array.from({ length: count }, (_, i) => `line ${i}`);
33 // Cut the file into regions with two untouched lines between them.
34 const plans: Plan[] = [];
35 let at = pick(3);
36 let n = 0;
37 while (at < count) {
38 const len = pick(3); // 0 = pure insertion
39 if (at + len > count) break;
40 const lines = Array.from({ length: pick(3) + (len === 0 ? 1 : 0) }, (_, i) => `agent${n} new ${i}`);
41 if (len > 0 || lines.length > 0) plans.push({ agent: `agent${n++}`, start: at, end: at + len, lines });
42 at += len + 2 + pick(3);
43 }
44 if (plans.length < 2) continue;
46 const expected = [...base];
47 for (const p of [...plans].reverse()) expected.splice(p.start, p.end - p.start, ...p.lines);
48 const expectedText = expected.length > 0 ? `${expected.join("\n")}\n` : undefined;
50 const orders = [plans.map((_, i) => i), plans.map((_, i) => plans.length - 1 - i)];
51 for (let k = 0; k < 3; k++) orders.push([...orders[0]!].sort(() => rand() - 0.5));
52 for (const order of orders) {
53 const project = new Project(new MemoryStore());
54 project.init({ "f.txt": `${base.join("\n")}\n` }, 0);
55 for (const p of plans) {
56 project.sync(p.agent);
57 const lines = [...base];
58 lines.splice(p.start, p.end - p.start, ...p.lines);
59 project.record(p.agent, { "f.txt": `${lines.join("\n")}\n` }, 1);
60 }
61 for (const i of order) {
62 const answer = project.publish(plans[i]!.agent, "m", 2);
63 assert.equal(answer.ok, true, `seed ${seed}, order ${order}: ${JSON.stringify(answer)}`);
64 }
65 assert.equal(project.files()["f.txt"], expectedText, `seed ${seed}, order ${order}`);
66 }
67 }
68});