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/merge.test.ts?format=json
182 linesFirst import 98% · infra-split 1% · pinch 1% · rename 1%Last change rename in version 100, 4 versions changed it
1// Merging at sync (#25, from the regex trial): when an agent's draft and work
2// published since its base change the same lines, a sync merges the file the
3// way git does. Changes both made identically are kept once; real
4// differences stay a collision. A name both sides define in one file is a
5// collision of its own.
6
7import assert from "node:assert/strict";
8import { test } from "node:test";
9import { MemoryStore, Project } from "../project.ts";
11const BASE = [
12 "// classset",
13 "export function parse(h: string, h2: string) {",
14 " if (!/^[0-9a-f]{2}$/i.test(h)) err('Invalid escape');",
15 " if (!/^[0-9a-f]+$/i.test(h)) err('Invalid Unicode escape');",
16 " return h2;",
17 "}",
18 "function err(m: string): never {",
19 " throw new SyntaxError(m);",
20 "}",
21 "",
22].join("\n");
23const CALLS = BASE.replace("!/^[0-9a-f]{2}$/i.test(h)", "!isHex(h, 2)").replace(
24 "!/^[0-9a-f]+$/i.test(h)",
25 "!isHex(h, -1)",
26);
27const HELPER =
28 "function isHex(s: string, len: number): boolean {\n return /* no RegExp */ s.length > 0 && (len < 0 || s.length === len);\n}\n";
30function setup() {
31 const store = new MemoryStore();
32 const project = new Project(store);
33 project.init({ "engine/classset.ts": BASE }, 0);
34 project.sync("prefix");
35 project.sync("fuzzer");
36 return { project, store };
39test("the same edit on both sides merges into one copy at pull: no collision, no doubled lines", () => {
40 const { project } = setup();
41 project.record("prefix", { "engine/classset.ts": CALLS + HELPER }, 1);
42 assert.equal(project.publish("prefix", "isHex instead of regex literals", 2).ok, true);
43 project.record("fuzzer", { "engine/classset.ts": CALLS + HELPER }, 3);
44 const s = project.sync("fuzzer");
45 assert.deepEqual(s.collisions, []);
46 assert.equal(project.draftOf("fuzzer")!.view.text("engine/classset.ts"), CALLS + HELPER);
47 assert.equal(project.publish("fuzzer", "Nothing left of mine", 4).ok, true);
48 assert.equal(project.files()["engine/classset.ts"], CALLS + HELPER);
49});
51test("Fuzzer's case: identical call lines merge once; the two helpers are a duplicate collision until one goes", () => {
52 const { project } = setup();
53 // prefix: the calls, and its helper at the bottom.
54 project.record("prefix", { "engine/classset.ts": CALLS + HELPER }, 1);
55 assert.equal(project.publish("prefix", "isHex instead of regex literals", 2).ok, true);
56 // fuzzer, unpublished: the same calls, and its own helper at the top.
57 const mine = CALLS.replace("// classset\n", `// classset\n${HELPER.replace("no RegExp", "hex only")}`);
58 project.record("fuzzer", { "engine/classset.ts": mine }, 3);
59 const s = project.sync("fuzzer");
60 const text = project.draftOf("fuzzer")!.view.text("engine/classset.ts");
61 assert.equal(text.split("\n").filter((l) => l.includes("err('Invalid escape')")).length, 1);
62 assert.equal(text.split("\n").filter((l) => l.startsWith("function isHex")).length, 2);
63 assert.deepEqual(
64 s.collisions.map((c) => [c.kind, c.name, c.file, c.theirs.agent]),
65 [["duplicate", "isHex", "engine/classset.ts", "prefix"]],
66 );
67 const refused = project.publish("fuzzer", "Helper", 4);
68 assert.deepEqual(!refused.ok && refused.collisions.map((c) => c.kind), ["duplicate"]);
69 // fuzzer keeps prefix's helper; now it publishes.
70 project.record("fuzzer", { "engine/classset.ts": CALLS + HELPER }, 5);
71 assert.equal(project.publish("fuzzer", "Keep one isHex", 6).ok, true);
72});
74test("different edits to the same lines still collide, with both sides in the file", () => {
75 const { project } = setup();
76 project.edit("prefix", { file: "engine/classset.ts", old: "return h2;", new: "return h2.trim();" }, 1);
77 project.publish("prefix", "Trim", 2);
78 project.edit("fuzzer", { file: "engine/classset.ts", old: "return h2;", new: "return h2.toLowerCase();" }, 3);
79 const s = project.sync("fuzzer");
80 assert.deepEqual(
81 s.collisions.map((c) => c.kind),
82 ["lines"],
83 );
84 const text = project.draftOf("fuzzer")!.view.text("engine/classset.ts");
85 assert.ok(text.includes("h2.trim()") && text.includes("h2.toLowerCase()"));
86});
88test("a merge keeps the draft's other changes, and a log with a merge loads back the same", () => {
89 const { project, store } = setup();
90 project.record("prefix", { "engine/classset.ts": CALLS }, 1);
91 project.publish("prefix", "Calls", 2);
92 // fuzzer: the same calls, plus its own change elsewhere in the file.
93 project.record(
94 "fuzzer",
95 { "engine/classset.ts": CALLS.replace("throw new SyntaxError(m);", "throw new SyntaxError(`regex: ${m}`);") },
96 3,
97 );
98 assert.deepEqual(project.sync("fuzzer").collisions, []);
99 assert.equal(project.publish("fuzzer", "Prefix errors", 4).ok, true);
100 assert.equal(
101 project.files()["engine/classset.ts"],
102 CALLS.replace("throw new SyntaxError(m);", "throw new SyntaxError(`regex: ${m}`);"),
103 );
104 assert.deepEqual(Project.load(store.entries, new MemoryStore()).files(), project.files());
105});
107test("a duplicate shows each whole definition, body and all, and counts common names too", () => {
108 const store = new MemoryStore();
109 const project = new Project(store);
110 project.init({ "p.ts": "// parser\n" }, 0);
111 project.sync("alice");
112 project.sync("bob");
113 project.record("alice", { "p.ts": "// parser\nexport function parse() {\n return 1;\n}\n" }, 1);
114 project.publish("alice", "parse", 2);
115 project.record("bob", { "p.ts": "// parser\nexport function parse() {\n return 2;\n}\n" }, 3);
116 const s = project.sync("bob");
117 // Both inserted at the same place, so the lines collide too, with markers (#11); the duplicate is listed beside them.
118 assert.deepEqual(
119 s.collisions.map((c) => [c.kind, c.marked]),
120 [
121 ["duplicate", undefined],
122 ["lines", true],
123 ],
124 );
125 const d = s.collisions[0]!;
126 assert.equal(d.name, "parse");
127 assert.equal(d.yours.text, "export function parse() {\n return 2;\n}\n");
128 assert.equal(d.theirs.text, "export function parse() {\n return 1;\n}\n");
129 assert.equal(d.yours.lines[1] - d.yours.lines[0], 2);
130 assert.equal(d.theirs.lines[1] - d.theirs.lines[0], 2);
131});
133test("loop variables and functions passed as arguments are not top-level definitions (#37)", () => {
134 const project = new Project(new MemoryStore());
135 project.init({ "tools/play.mjs": "// play\n" }, 0);
136 project.sync("b4");
137 project.record(
138 "b4",
139 { "tools/play.mjs": "// play\nfor (let i = 0; i < 3; i++) step();\nrun(function helper() {});\n" },
140 1,
141 );
142 assert.equal(project.publish("b4", "One loop", 2).ok, true);
143 project.record(
144 "b4",
145 {
146 "tools/play.mjs":
147 "// play\nfor (let i = 0; i < 3; i++) step();\nrun(function helper() {});\nfor (let i = 0; i < 9; i++) beam();\nrun(function helper() {});\n",
148 },
149 3,
150 );
151 assert.equal(project.publish("b4", "Another loop", 4).ok, true);
152 // A real top-level redeclaration against earlier work still is one.
153 project.record("b4", { "tools/play.mjs": "export let speed = 1;\n" }, 5);
154 project.publish("b4", "Speed", 6);
155 project.record("b4", { "tools/play.mjs": "export let speed = 1;\nexport let speed = 2;\n" }, 7);
156 const refused = project.publish("b4", "Speed again", 8);
157 assert.deepEqual(!refused.ok && refused.collisions.map((c) => [c.kind, c.name]), [["duplicate", "speed"]]);
158});
160test("for, while and if at the start of a line name nothing (#37)", () => {
161 const project = new Project(new MemoryStore());
162 project.init({ "tools/fjord.mjs": "// fjord\n" }, 0);
163 project.sync("b4");
164 project.record(
165 "b4",
166 {
167 "tools/fjord.mjs":
168 "// fjord\nfor (let i = 0; i < 3; i++) {\n step(i);\n}\nwhile (busy()) {\n wait();\n}\nif (ready()) {\n go();\n}\n",
169 },
170 1,
171 );
172 assert.equal(project.publish("b4", "Loops", 2).ok, true);
173 project.record(
174 "b4",
175 {
176 "tools/fjord.mjs":
177 "// fjord\nfor (let i = 0; i < 3; i++) {\n step(i);\n}\nwhile (busy()) {\n wait();\n}\nif (ready()) {\n go();\n}\nfor (const [x, y] of [[720, 540]]) {\n beam(x, y);\n}\nwhile (more()) {\n drain();\n}\nif (done()) {\n stop();\n}\n",
178 },
179 3,
180 );
181 assert.equal(project.publish("b4", "More loops", 4).ok, true);
182});