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/moves.test.ts?version=100&format=json
210 linesFirst import 97% · rename 2% · pinch 1%Last change rename in version 100, 3 versions changed it

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

1// Moves in drafts (#28): within one agent's unpublished draft, a line that
2// leaves one place and the same text that arrives somewhere else is the same
3// line. Publishing records it as a move, so someone else's edit to those
4// lines follows them instead of colliding.
5
6import assert from "node:assert/strict";
7import { test } from "node:test";
8import { MemoryStore, Project } from "../project.ts";
9
10const AREA = "export function area(r: number) {\n const pi = 3.14;\n return pi * r * r;\n}\n";
11const START = {
12 "src/shapes.ts": `// shapes\n${AREA}\nexport function other() {\n return 1;\n}\n`,
13 "src/geometry.ts": "// geometry\n",
14};
15const SHAPES_AFTER = "// shapes\n\nexport function other() {\n return 1;\n}\n";
17function setup() {
18 const store = new MemoryStore();
19 const project = new Project(store);
20 project.init(START, 0);
21 project.sync("ann");
22 project.sync("bob");
23 return { project, store };
26/** ann moves area() to geometry.ts the way an agent's tools do: delete it, then write it. */
27function annMovesArea(project: Project, order: "delete first" | "write first" = "delete first"): void {
28 const del = () => project.edit("ann", { file: "src/shapes.ts", old: AREA, new: "" }, 1);
29 const write = () => project.edit("ann", { file: "src/geometry.ts", content: `// geometry\n${AREA}` }, 1);
30 if (order === "delete first") {
31 del();
32 write();
33 } else {
34 write();
35 del();
36 }
39for (const order of ["delete first", "write first"] as const) {
40 test(`a function moved to another file (${order}) while someone else edits it: the edit follows it`, () => {
41 const { project } = setup();
42 project.edit("bob", { file: "src/shapes.ts", old: "3.14", new: "Math.PI" }, 1);
43 assert.equal(project.publish("bob", "Use Math.PI", 2).ok, true);
44 annMovesArea(project, order);
45 const published = project.publish("ann", "Move area to geometry", 3);
46 assert.equal(published.ok, true, JSON.stringify(published));
47 const files = project.files();
48 assert.equal(files["src/shapes.ts"], SHAPES_AFTER);
49 assert.equal(files["src/geometry.ts"], `// geometry\n${AREA.replace("3.14", "Math.PI")}`);
50 // ann's folder gets bob's edit at the function's new place.
51 assert.deepEqual(project.sync("ann").files, { "src/geometry.ts": files["src/geometry.ts"] });
52 });
55test("an edit pushed after the move, made against the old place, lands at the new one", () => {
56 const { project } = setup();
57 annMovesArea(project);
58 project.edit("bob", { file: "src/shapes.ts", old: "const pi = 3.14;", new: "const pi = 3.1416;" }, 1);
59 assert.equal(project.publish("ann", "Move area", 2).ok, true);
60 // bob hears that the lines it changed moved, with its change.
61 const news = project.edit("bob", { file: "src/shapes.ts", old: "return 1;", new: "return 1; // other" }, 2);
62 assert.deepEqual(news.ok && news.news, [
63 'ann pushed "Move area" (version 1), which moved lines your draft changes from src/shapes.ts to src/geometry.ts; your changes moved with them. Run abso pull to see them there.',
64 ]);
65 project.restore("bob", null, 2);
66 project.edit("bob", { file: "src/shapes.ts", old: "return pi * r * r;", new: "return pi * r ** 2;" }, 3);
67 assert.equal(project.publish("bob", "Square", 4).ok, true);
68 assert.equal(project.files()["src/geometry.ts"], `// geometry\n${AREA.replace("r * r", "r ** 2")}`);
69 assert.equal(project.files()["src/shapes.ts"], SHAPES_AFTER);
70});
72test("a file renamed with record, while someone edits it", () => {
73 const { project } = setup();
74 project.edit("bob", { file: "src/shapes.ts", old: "return 1;", new: "return 2;" }, 1);
75 assert.equal(project.publish("bob", "Two", 2).ok, true);
76 project.record("ann", { "src/shapes.ts": null, "src/figures.ts": START["src/shapes.ts"] }, 3);
77 assert.equal(project.publish("ann", "Rename shapes to figures", 4).ok, true);
78 const files = project.files();
79 assert.equal("src/shapes.ts" in files, false);
80 assert.equal(files["src/figures.ts"], START["src/shapes.ts"].replace("return 1;", "return 2;"));
81});
83test("pushing a move leaves the mover's own files exactly as they were", () => {
84 const { project } = setup();
85 annMovesArea(project);
86 project.edit("ann", { file: "src/geometry.ts", old: "3.14", new: "3.14159" }, 2);
87 const before = project.diff("ann").files.map((f) => [f.path, f.after]);
88 assert.equal(project.publish("ann", "Move and refine area", 3).ok, true);
89 assert.deepEqual(project.sync("ann").files, {});
90 const files = project.files();
91 for (const [path, after] of before) assert.equal(files[path!], after);
92});
94test("a split: part of a file moves to a new one while someone edits both parts", () => {
95 const { project } = setup();
96 project.edit("bob", { file: "src/shapes.ts", old: "3.14", new: "Math.PI" }, 1);
97 project.edit("bob", { file: "src/shapes.ts", old: "return 1;", new: "return 2;" }, 1);
98 assert.equal(project.publish("bob", "Both parts", 2).ok, true);
99 project.record(
100 "ann",
101 { "src/shapes.ts": `// shapes\n${AREA}`, "src/other.ts": "\nexport function other() {\n return 1;\n}\n" },
102 3,
103 );
104 assert.equal(project.publish("ann", "Split other out", 4).ok, true);
105 const files = project.files();
106 assert.equal(files["src/shapes.ts"], `// shapes\n${AREA.replace("3.14", "Math.PI")}`);
107 assert.equal(files["src/other.ts"], "\nexport function other() {\n return 2;\n}\n");
108});
110test("lines too plain to identify do not count as moved on their own", () => {
111 const { project } = setup();
112 // A closing bracket deleted in one place and another written elsewhere.
113 project.record(
114 "ann",
115 {
116 "src/geometry.ts": "// geometry\n}\n",
117 "src/shapes.ts": START["src/shapes.ts"].replace(" return 1;\n}\n", " return 1;\n"),
118 },
119 1,
120 );
121 assert.equal(project.publish("ann", "Brackets", 2).ok, true);
122 assert.equal(
123 project.graph.atoms().some((a) => a.moves !== undefined),
124 false,
125 );
126});
128test("a plain-git commit that renames a file keeps its lines, so an agent's edit follows", () => {
129 const { project } = setup();
130 project.edit("bob", { file: "src/shapes.ts", old: "return 1;", new: "return 3;" }, 1);
131 const files = { "src/figures.ts": START["src/shapes.ts"], "src/geometry.ts": START["src/geometry.ts"] };
132 const answer = project.ingest(
133 { author: "pat", base: 0, commits: [{ sha: "abcdef1234", message: "Rename", files }] },
134 2,
135 );
136 assert.equal(answer.landed, true);
137 assert.equal(project.publish("bob", "Three", 3).ok, true);
138 assert.equal(project.files()["src/figures.ts"], START["src/shapes.ts"].replace("return 1;", "return 3;"));
139 assert.equal("src/shapes.ts" in project.files(), false);
140});
142test("a log with moves loads back to the same state", () => {
143 const { project, store } = setup();
144 annMovesArea(project);
145 project.publish("ann", "Move area", 2);
146 const again = Project.load(store.entries, new MemoryStore());
147 assert.deepEqual(again.files(), project.files());
148});
150test("a file's log lists an edit to lines moved into it, though the lines were written elsewhere (#43)", () => {
151 const { project } = setup();
152 annMovesArea(project);
153 project.publish("ann", "Move area to geometry", 2);
154 project.sync("bob");
155 // Taking a line out names only the file it was written in, shapes.ts.
156 project.edit("bob", { file: "src/geometry.ts", old: " const pi = 3.14;\n", new: "" }, 3);
157 project.publish("bob", "No pi line", 4);
158 assert.deepEqual(
159 project.log({ path: "src/geometry.ts" }).versions.map((v) => v.message),
160 ["No pi line", "Move area to geometry", "Start the project"],
161 );
162 assert.deepEqual(
163 project.log({ path: "src/shapes.ts" }).versions.map((v) => v.message),
164 ["Move area to geometry", "Start the project"],
165 );
166});
168// Where moves stop today (#62, step 2, proposal C would change the first). Pinned so a change is a choice.
169test("a function re-indented on its way to another file is not a move: someone's edit to it collides", () => {
170 const { project } = setup();
171 project.edit("bob", { file: "src/shapes.ts", old: "3.14", new: "Math.PI" }, 1);
172 project.edit("ann", { file: "src/shapes.ts", old: AREA, new: "" }, 1);
173 const indented = AREA.split("\n")
174 .map((l) => (l === "" ? l : `\t${l}`))
175 .join("\n");
176 project.edit("ann", { file: "src/geometry.ts", content: `// geometry\nexport namespace G {\n${indented}}\n` }, 1);
177 assert.equal(project.publish("ann", "Move area into a namespace", 2).ok, true);
178 const bob = project.publish("bob", "Use Math.PI", 3);
179 assert.equal(bob.ok, false, "the lines bob edited are gone, not moved");
180});
182test("a move split across two pushes is a delete and a fresh write: the moved lines are new", () => {
183 const { project } = setup();
184 project.edit("ann", { file: "src/shapes.ts", old: AREA, new: "" }, 1);
185 assert.equal(project.publish("ann", "Take area out", 2).ok, true);
186 project.edit("ann", { file: "src/geometry.ts", content: `// geometry\n${AREA}` }, 3);
187 assert.equal(project.publish("ann", "Put area back, elsewhere", 4).ok, true);
188 const lines = project.sharedView().lines("src/geometry.ts");
189 assert.equal(project.publishOf(lines[1]!.atom)?.version, 2, "written fresh in version 2, not carried from version 0");
190});
192test("past the move limit only the longest blocks move: a rewrite of many single lines still pushes, and a block moved whole keeps someone's edit (abso issue 137)", () => {
193 const keys = Array.from({ length: 200 }, (_, i) => ` "k${i}": ${i},`);
194 const block = [" // block", ' "b1": "one",', ' "b2": "two",', ' "b3": "three",', " // end block"];
195 const before = `${["{", ...block, ...keys, "}"].join("\n")}\n`;
196 // The draft reverses the keys with a new line between each, so every one is its own one-line match.
197 const rewritten = keys.toReversed().flatMap((k, i) => [k, ` "n${i}": 0,`]);
198 const after = `${["{", ...rewritten, ...block, "}"].join("\n")}\n`;
199 const store = new MemoryStore();
200 const project = new Project(store);
201 project.init({ "f.json": before }, 0);
202 project.sync("ann");
203 project.sync("bob");
204 project.edit("bob", { file: "f.json", old: '"two"', new: '"TWO"' }, 1);
205 assert.equal(project.publish("bob", "two is TWO", 2).ok, true);
206 project.edit("ann", { file: "f.json", content: after }, 3);
207 const published = project.publish("ann", "Rewrite f.json", 4);
208 assert.equal(published.ok, true, JSON.stringify(published));
209 assert.equal(project.files()["f.json"], after.replace('"two"', '"TWO"'));
210});