@criogaid/pi-hashline-edit 0.5.1 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -15
- package/package.json +6 -6
- package/src/core/apply.test.ts +66 -16
- package/src/core/apply.ts +36 -19
- package/src/core/lines.ts +3 -3
- package/src/index.ts +2 -0
- package/src/integration/forget-grep.test.ts +98 -0
- package/src/integration/fusion-dispatch.test.ts +343 -0
- package/src/integration/grep-rg.test.ts +269 -33
- package/src/integration/mutation-file-types.test.ts +44 -0
- package/src/pi/action-fusion.test.ts +71 -128
- package/src/pi/action-fusion.ts +83 -45
- package/src/pi/argument-validation.test.ts +136 -0
- package/src/pi/argument-validation.ts +102 -0
- package/src/pi/budgets.ts +4 -1
- package/src/pi/config.test.ts +1 -0
- package/src/pi/config.ts +4 -0
- package/src/pi/directory-sync.test.ts +1 -1
- package/src/pi/edit-tool.ts +55 -36
- package/src/pi/execute.test.ts +185 -645
- package/src/pi/extension-loading.test.ts +34 -0
- package/src/pi/extension-loading.testing.ts +15 -0
- package/src/pi/failure-context.test.ts +4 -3
- package/src/pi/failure-path.test.ts +3 -3
- package/src/pi/file-commit.test.ts +24 -17
- package/src/pi/file-commit.ts +36 -9
- package/src/pi/file-snapshot.test.ts +24 -0
- package/src/pi/forget-tool.ts +268 -0
- package/src/pi/forget.test.ts +343 -0
- package/src/pi/forget.testing.ts +273 -0
- package/src/pi/grep-output.ts +57 -28
- package/src/pi/grep-search.ts +35 -12
- package/src/pi/grep-tool.ts +28 -9
- package/src/pi/grep.test.ts +173 -152
- package/src/pi/mutation-result.test.ts +23 -0
- package/src/pi/mutation-runner.ts +8 -4
- package/src/pi/path.ts +16 -2
- package/src/pi/pi.test.ts +3 -2
- package/src/pi/read-tool.ts +58 -26
- package/src/pi/read.test.ts +361 -0
- package/src/pi/registration.test.ts +115 -2
- package/src/pi/render.test.ts +45 -14
- package/src/pi/replace-tool.ts +18 -27
- package/src/pi/replace.test.ts +8 -107
- package/src/pi/result-safety.test.ts +123 -149
- package/src/pi/rg-match-bytes.ts +95 -0
- package/src/pi/rg-process.test.ts +40 -59
- package/src/pi/rg-text-view.ts +52 -5
- package/src/pi/tool-call.testing.ts +50 -0
- package/src/pi/tool-prompts.ts +15 -0
- package/src/pi/write-tool.ts +17 -26
- package/src/pi/write.test.ts +70 -61
- package/src/pi/e2e-scenarios.test.ts +0 -361
package/src/pi/write.test.ts
CHANGED
|
@@ -49,83 +49,73 @@ test("write rejects unwritable content before accessing a missing target", async
|
|
|
49
49
|
withTemp(async (dir) => {
|
|
50
50
|
const file = join(dir, "missing.txt");
|
|
51
51
|
const tool = makeWriteOverride(dir);
|
|
52
|
-
const valid = { path: file, content: "ready\n" };
|
|
52
|
+
const valid = { path: file, content: "ready\n", mode: "create" };
|
|
53
53
|
assert.equal(tool.prepareArguments(valid), valid);
|
|
54
54
|
for (const [content, expected] of [
|
|
55
55
|
["bad\0", /Invalid argument content: UNSUPPORTED_TEXT: NUL bytes are not editable\./],
|
|
56
56
|
["\ud800", /Invalid argument content: INVALID_UNICODE:/],
|
|
57
57
|
] as const) {
|
|
58
58
|
await assert.rejects(
|
|
59
|
-
callTool(tool, { path: file, content }, { ctx: context(dir) }),
|
|
59
|
+
callTool(tool, { path: file, content, mode: "create" }, { ctx: context(dir) }),
|
|
60
60
|
expected,
|
|
61
61
|
);
|
|
62
62
|
await assert.rejects(readFile(file, "utf8"), { code: "ENOENT" });
|
|
63
63
|
}
|
|
64
64
|
}));
|
|
65
65
|
|
|
66
|
-
test("write
|
|
66
|
+
test("write omits mode → rejects before changing files or running commands", async () =>
|
|
67
67
|
withTemp(async (dir) => {
|
|
68
|
-
const
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
68
|
+
const existing = join(dir, "existing.txt");
|
|
69
|
+
const missingParent = join(dir, "missing");
|
|
70
|
+
const missing = join(missingParent, "new.txt");
|
|
71
|
+
await writeFile(existing, "original\n");
|
|
72
|
+
let commandRuns = 0;
|
|
73
|
+
const fusion = createActionFusionExecutor(async () => {
|
|
74
|
+
commandRuns++;
|
|
75
|
+
return { status: "succeeded", output: "checked" };
|
|
76
|
+
});
|
|
77
|
+
for (const executor of [undefined, fusion]) {
|
|
78
|
+
const tool = makeWriteOverride(dir, executor);
|
|
79
|
+
for (const path of [existing, missing]) {
|
|
80
|
+
await assert.rejects(
|
|
81
|
+
callTool(
|
|
82
|
+
tool,
|
|
83
|
+
{ path, content: "changed\n", ...(executor ? { then_run: { command: "check" } } : {}) },
|
|
84
|
+
{ ctx: context(dir) },
|
|
85
|
+
),
|
|
86
|
+
/\n {2}- mode:/,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
assert.equal(await readFile(existing, "utf8"), "original\n");
|
|
91
|
+
await assert.rejects(readFile(missingParent), { code: "ENOENT" });
|
|
92
|
+
assert.equal(commandRuns, 0);
|
|
89
93
|
}));
|
|
90
94
|
|
|
91
95
|
test("write supports create-only and overwrite-only modes", async () =>
|
|
92
96
|
withTemp(async (dir) => {
|
|
93
|
-
const write = makeWriteOverride(dir)
|
|
94
|
-
|
|
95
|
-
|
|
97
|
+
const write = makeWriteOverride(dir);
|
|
98
|
+
const target = join(dir, "new.txt");
|
|
99
|
+
const options = { ctx: context(dir) };
|
|
100
|
+
const created = await callTool(
|
|
101
|
+
write,
|
|
96
102
|
{ path: "new.txt", content: "new\n", mode: "create" },
|
|
97
|
-
|
|
98
|
-
undefined,
|
|
99
|
-
context(dir),
|
|
103
|
+
options,
|
|
100
104
|
);
|
|
105
|
+
assert.doesNotMatch(created.content[0].text, /Revision:|[0-9a-f]{64}/);
|
|
106
|
+
assert.equal(created.details.publishedRevision, await fileRevision(target));
|
|
107
|
+
assert.equal("revision" in created.details, false);
|
|
101
108
|
await assert.rejects(
|
|
102
|
-
write.
|
|
103
|
-
"create-again",
|
|
104
|
-
{ path: "new.txt", content: "bad\n", mode: "create" },
|
|
105
|
-
undefined,
|
|
106
|
-
undefined,
|
|
107
|
-
context(dir),
|
|
108
|
-
),
|
|
109
|
+
callTool(write, { path: "new.txt", content: "bad\n", mode: "create" }, options),
|
|
109
110
|
/already exists/,
|
|
110
111
|
);
|
|
112
|
+
assert.equal(await readFile(target, "utf8"), "new\n");
|
|
111
113
|
await assert.rejects(
|
|
112
|
-
write.
|
|
113
|
-
"missing-overwrite",
|
|
114
|
-
{ path: "missing.txt", content: "bad\n", mode: "overwrite" },
|
|
115
|
-
undefined,
|
|
116
|
-
undefined,
|
|
117
|
-
context(dir),
|
|
118
|
-
),
|
|
114
|
+
callTool(write, { path: "missing.txt", content: "bad\n", mode: "overwrite" }, options),
|
|
119
115
|
/does not exist/,
|
|
120
116
|
);
|
|
121
|
-
await write.
|
|
122
|
-
|
|
123
|
-
{ path: "new.txt", content: "updated\n", mode: "overwrite" },
|
|
124
|
-
undefined,
|
|
125
|
-
undefined,
|
|
126
|
-
context(dir),
|
|
127
|
-
);
|
|
128
|
-
assert.equal(await readFile(join(dir, "new.txt"), "utf8"), "updated\n");
|
|
117
|
+
await callTool(write, { path: "new.txt", content: "updated\n", mode: "overwrite" }, options);
|
|
118
|
+
assert.equal(await readFile(target, "utf8"), "updated\n");
|
|
129
119
|
}));
|
|
130
120
|
|
|
131
121
|
test("write rejects obsolete expectedRevision without overwriting", async () =>
|
|
@@ -135,6 +125,7 @@ test("write rejects obsolete expectedRevision without overwriting", async () =>
|
|
|
135
125
|
const params = {
|
|
136
126
|
path: target,
|
|
137
127
|
content: "changed\n",
|
|
128
|
+
mode: "overwrite",
|
|
138
129
|
expectedRevision: await fileRevision(target),
|
|
139
130
|
};
|
|
140
131
|
for (const fusion of [undefined, createActionFusionExecutor()]) {
|
|
@@ -158,7 +149,7 @@ test("write returns only a summary for empty, short, and long content", async ()
|
|
|
158
149
|
const path = `file-${index}.txt`;
|
|
159
150
|
const result = await makeWriteOverride(dir).execute(
|
|
160
151
|
"write",
|
|
161
|
-
{ path, content },
|
|
152
|
+
{ path, content, mode: "create" },
|
|
162
153
|
undefined,
|
|
163
154
|
undefined,
|
|
164
155
|
context(dir),
|
|
@@ -170,11 +161,19 @@ test("write returns only a summary for empty, short, and long content", async ()
|
|
|
170
161
|
|
|
171
162
|
test("write returns its summary and command output after an unchanged then_run", async () =>
|
|
172
163
|
withTemp(async (dir) => {
|
|
173
|
-
const fusion = createActionFusionExecutor(async () =>
|
|
164
|
+
const fusion = createActionFusionExecutor(async () => ({
|
|
165
|
+
status: "succeeded",
|
|
166
|
+
output: "checked",
|
|
167
|
+
}));
|
|
174
168
|
const write = makeWriteOverride(dir, fusion) as any;
|
|
175
169
|
const result = await write.execute(
|
|
176
170
|
"unchanged",
|
|
177
|
-
{
|
|
171
|
+
{
|
|
172
|
+
path: "unchanged.txt",
|
|
173
|
+
content: "mutation\n",
|
|
174
|
+
mode: "create",
|
|
175
|
+
then_run: { command: "check" },
|
|
176
|
+
},
|
|
178
177
|
undefined,
|
|
179
178
|
undefined,
|
|
180
179
|
context(dir),
|
|
@@ -191,12 +190,17 @@ test("write reports changed freshness when then_run changes the target", async (
|
|
|
191
190
|
const target = join(dir, "changed.txt");
|
|
192
191
|
const fusion = createActionFusionExecutor(async () => {
|
|
193
192
|
await writeFile(target, "command changed\n");
|
|
194
|
-
return "checked";
|
|
193
|
+
return { status: "succeeded", output: "checked" };
|
|
195
194
|
});
|
|
196
195
|
const write = makeWriteOverride(dir, fusion) as any;
|
|
197
196
|
const result = await callTool(
|
|
198
197
|
write,
|
|
199
|
-
{
|
|
198
|
+
{
|
|
199
|
+
path: "changed.txt",
|
|
200
|
+
content: "mutation\n",
|
|
201
|
+
mode: "create",
|
|
202
|
+
then_run: { command: "check" },
|
|
203
|
+
},
|
|
200
204
|
{ toolCallId: "changed", ctx: context(dir) },
|
|
201
205
|
);
|
|
202
206
|
const text = result.content.map((block: any) => block.text).join("\n");
|
|
@@ -219,11 +223,16 @@ test("write reports missing freshness when then_run removes the target", async (
|
|
|
219
223
|
const target = join(dir, "missing.txt");
|
|
220
224
|
const fusion = createActionFusionExecutor(async () => {
|
|
221
225
|
await rm(target);
|
|
222
|
-
return "removed";
|
|
226
|
+
return { status: "succeeded", output: "removed" };
|
|
223
227
|
});
|
|
224
228
|
const result = await makeWriteOverride(dir, fusion).execute(
|
|
225
229
|
"missing",
|
|
226
|
-
{
|
|
230
|
+
{
|
|
231
|
+
path: "missing.txt",
|
|
232
|
+
content: "mutation\n",
|
|
233
|
+
mode: "create",
|
|
234
|
+
then_run: { command: "remove" },
|
|
235
|
+
},
|
|
227
236
|
undefined,
|
|
228
237
|
undefined,
|
|
229
238
|
context(dir),
|
|
@@ -244,7 +253,7 @@ test("write preserves command failure and changed freshness", async () =>
|
|
|
244
253
|
});
|
|
245
254
|
const result = await makeWriteOverride(dir, fusion).execute(
|
|
246
255
|
"failed",
|
|
247
|
-
{ path: "failed.txt", content: "mutation\n", then_run: { command: "fail" } },
|
|
256
|
+
{ path: "failed.txt", content: "mutation\n", mode: "create", then_run: { command: "fail" } },
|
|
248
257
|
undefined,
|
|
249
258
|
undefined,
|
|
250
259
|
context(dir),
|
|
@@ -263,7 +272,7 @@ test("write rejects NUL content", async () =>
|
|
|
263
272
|
await assert.rejects(
|
|
264
273
|
write.execute(
|
|
265
274
|
"nul",
|
|
266
|
-
{ path: "nul.txt", content: "a\0b" },
|
|
275
|
+
{ path: "nul.txt", content: "a\0b", mode: "create" },
|
|
267
276
|
undefined,
|
|
268
277
|
undefined,
|
|
269
278
|
context(dir),
|
|
@@ -1,361 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* End-to-End file editing workflow tests for pi-hashline-edit.
|
|
3
|
-
* Evaluates the full pipeline:
|
|
4
|
-
* Discovery -> Search/Read -> Anchor Localization -> Edit Construction -> Apply -> Recovery -> Verification.
|
|
5
|
-
*
|
|
6
|
-
* Covers Dev Set (D1-D6) and Holdout Set (H1-H3).
|
|
7
|
-
*/
|
|
8
|
-
import { test } from "node:test";
|
|
9
|
-
import assert from "node:assert/strict";
|
|
10
|
-
import { mkdtemp, rm, readFile, writeFile } from "node:fs/promises";
|
|
11
|
-
import { tmpdir } from "node:os";
|
|
12
|
-
import { join } from "node:path";
|
|
13
|
-
import { makeEditOverride } from "./edit-tool.ts";
|
|
14
|
-
import { makeReadOverride } from "./read-tool.ts";
|
|
15
|
-
import { makeGrepOverride } from "./grep-tool.ts";
|
|
16
|
-
import { makeReplaceTool } from "./replace-tool.ts";
|
|
17
|
-
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
18
|
-
import { computeLineHash } from "../core/hash.ts";
|
|
19
|
-
import { splitLines } from "../core/lines.ts";
|
|
20
|
-
import { callTool } from "./tool-call.testing.ts";
|
|
21
|
-
import { DEFAULT_CONFIG } from "./config.ts";
|
|
22
|
-
|
|
23
|
-
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
24
|
-
const dir = await mkdtemp(join(tmpdir(), "hl-e2e-"));
|
|
25
|
-
try {
|
|
26
|
-
return await fn(dir);
|
|
27
|
-
} finally {
|
|
28
|
-
await rm(dir, { recursive: true, force: true });
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const call = (tool: any, params: any) => callTool(tool, params, { toolCallId: "0" });
|
|
33
|
-
|
|
34
|
-
function h(text: string, line: number) {
|
|
35
|
-
return `${line}#${computeLineHash(line, splitLines(text)[line - 1], 4)}`;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function anchorLine(block: string, line: number) {
|
|
39
|
-
const m = new RegExp(`^${line}#([0-9A-Z]+)(?:│|$)`, "m").exec(block);
|
|
40
|
-
if (!m) throw new Error(`line ${line} anchor not found in block:\n${block}`);
|
|
41
|
-
return `${line}#${m[1]}`;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// ---------------------------------------------------------------------------
|
|
45
|
-
// Dev Set (D1 - D6)
|
|
46
|
-
// ---------------------------------------------------------------------------
|
|
47
|
-
|
|
48
|
-
test("E2E Dev D1: Search-and-edit single function without separate read call", async () =>
|
|
49
|
-
withDir(async (dir) => {
|
|
50
|
-
const file = join(dir, "calc.ts");
|
|
51
|
-
const original =
|
|
52
|
-
[
|
|
53
|
-
"import { math } from './math.ts';",
|
|
54
|
-
"",
|
|
55
|
-
"export function calculateDiscount(price: number): number {",
|
|
56
|
-
" if (price <= 0) return 0;",
|
|
57
|
-
" return price * 0.1;",
|
|
58
|
-
"}",
|
|
59
|
-
"",
|
|
60
|
-
"export function formatCurrency(amount: number): string {",
|
|
61
|
-
" return `$${amount.toFixed(2)}`;",
|
|
62
|
-
"}",
|
|
63
|
-
].join("\n") + "\n";
|
|
64
|
-
await writeFile(file, original);
|
|
65
|
-
|
|
66
|
-
const grep = makeGrepOverride(dir, DEFAULT_CONFIG);
|
|
67
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
68
|
-
|
|
69
|
-
// Turn 1: Search with context: 3
|
|
70
|
-
const grepResult = await call(grep, {
|
|
71
|
-
path: "calc.ts",
|
|
72
|
-
pattern: "calculateDiscount",
|
|
73
|
-
literal: true,
|
|
74
|
-
context: 3,
|
|
75
|
-
});
|
|
76
|
-
const grepText = grepResult.content[0].text;
|
|
77
|
-
assert.ok(grepText.includes("calculateDiscount"));
|
|
78
|
-
|
|
79
|
-
// Extract anchors directly from grep result
|
|
80
|
-
const targetAnchor = anchorLine(grepText, 5); // " return price * 0.1;"
|
|
81
|
-
|
|
82
|
-
// Turn 2: Direct edit using grep-provided anchor, without calling read!
|
|
83
|
-
const editResult = await call(edit, {
|
|
84
|
-
path: "calc.ts",
|
|
85
|
-
edits: [{ op: "replace", anchor: targetAnchor, body: [" return price * 0.15;"] }],
|
|
86
|
-
});
|
|
87
|
-
assert.equal(editResult.isError, undefined);
|
|
88
|
-
|
|
89
|
-
// Verify target file state
|
|
90
|
-
const updated = await readFile(file, "utf8");
|
|
91
|
-
assert.ok(updated.includes("return price * 0.15;"));
|
|
92
|
-
assert.ok(updated.includes("export function formatCurrency")); // outside range untouched
|
|
93
|
-
}));
|
|
94
|
-
|
|
95
|
-
test("E2E Dev D2: Chained multi-site edits using returned fresh anchors", async () =>
|
|
96
|
-
withDir(async (dir) => {
|
|
97
|
-
const file = join(dir, "service.ts");
|
|
98
|
-
const original =
|
|
99
|
-
[
|
|
100
|
-
"class Service {",
|
|
101
|
-
" version = 1;",
|
|
102
|
-
" enabled = false;",
|
|
103
|
-
" status() {",
|
|
104
|
-
" return 'idle';",
|
|
105
|
-
" }",
|
|
106
|
-
"}",
|
|
107
|
-
].join("\n") + "\n";
|
|
108
|
-
await writeFile(file, original);
|
|
109
|
-
|
|
110
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
111
|
-
|
|
112
|
-
// Step 1: Edit version
|
|
113
|
-
const r1 = await call(edit, {
|
|
114
|
-
path: "service.ts",
|
|
115
|
-
edits: [{ op: "replace", anchor: h(original, 2), body: [" version = 2;"] }],
|
|
116
|
-
});
|
|
117
|
-
const returnedAnchor = anchorLine(r1.content[0].text, 2);
|
|
118
|
-
assert.equal(returnedAnchor, h("class Service {\n version = 2;\n", 2));
|
|
119
|
-
|
|
120
|
-
// Step 2: Reuse the returned anchor and edit another site in the same batch.
|
|
121
|
-
const r2 = await call(edit, {
|
|
122
|
-
path: "service.ts",
|
|
123
|
-
edits: [
|
|
124
|
-
{ op: "replace", anchor: returnedAnchor, body: [" version = 3;"] },
|
|
125
|
-
{ op: "replace", anchor: h(original, 5), body: [" return 'active';"] },
|
|
126
|
-
],
|
|
127
|
-
});
|
|
128
|
-
assert.match(r2.content[0].text, /Updated anchors:/);
|
|
129
|
-
|
|
130
|
-
const final = await readFile(file, "utf8");
|
|
131
|
-
assert.equal(
|
|
132
|
-
final,
|
|
133
|
-
[
|
|
134
|
-
"class Service {",
|
|
135
|
-
" version = 3;",
|
|
136
|
-
" enabled = false;",
|
|
137
|
-
" status() {",
|
|
138
|
-
" return 'active';",
|
|
139
|
-
" }",
|
|
140
|
-
"}",
|
|
141
|
-
].join("\n") + "\n",
|
|
142
|
-
);
|
|
143
|
-
}));
|
|
144
|
-
|
|
145
|
-
test("E2E Dev D3: Recovery from shifted anchor after external line insertion", async () =>
|
|
146
|
-
withDir(async (dir) => {
|
|
147
|
-
const file = join(dir, "config.ts");
|
|
148
|
-
const original =
|
|
149
|
-
[
|
|
150
|
-
"// Config file",
|
|
151
|
-
"export const TIMEOUT = 1000;",
|
|
152
|
-
"export const RETRIES = 3;",
|
|
153
|
-
"export const DEBUG = false;",
|
|
154
|
-
].join("\n") + "\n";
|
|
155
|
-
await writeFile(file, original);
|
|
156
|
-
|
|
157
|
-
// Agent observed anchor at line 3 ("export const RETRIES = 3;")
|
|
158
|
-
const staleAnchor = h(original, 3);
|
|
159
|
-
|
|
160
|
-
// External process inserts 2 lines at top
|
|
161
|
-
const externallyModified =
|
|
162
|
-
[
|
|
163
|
-
"// Header comment line 1",
|
|
164
|
-
"// Header comment line 2",
|
|
165
|
-
"// Config file",
|
|
166
|
-
"export const TIMEOUT = 1000;",
|
|
167
|
-
"export const RETRIES = 3;",
|
|
168
|
-
"export const DEBUG = false;",
|
|
169
|
-
].join("\n") + "\n";
|
|
170
|
-
await writeFile(file, externallyModified);
|
|
171
|
-
|
|
172
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
173
|
-
|
|
174
|
-
// Agent attempts edit with stale anchor
|
|
175
|
-
let candidateAnchor = "";
|
|
176
|
-
await assert.rejects(
|
|
177
|
-
call(edit, {
|
|
178
|
-
path: "config.ts",
|
|
179
|
-
edits: [{ op: "replace", anchor: staleAnchor, body: ["export const RETRIES = 5;"] }],
|
|
180
|
-
}),
|
|
181
|
-
(error: Error) => {
|
|
182
|
-
assert.match(error.message, /Anchor mismatch: 1 shifted/);
|
|
183
|
-
assert.match(error.message, /candidate 5#/); // Shifted to line 5
|
|
184
|
-
const match = /candidate ([0-9]+#[0-9A-Z]+)/.exec(error.message);
|
|
185
|
-
if (match) candidateAnchor = match[1];
|
|
186
|
-
return true;
|
|
187
|
-
},
|
|
188
|
-
);
|
|
189
|
-
|
|
190
|
-
assert.ok(candidateAnchor.length > 0, "Recovery candidate was found and returned");
|
|
191
|
-
|
|
192
|
-
// Agent retries using candidate anchor
|
|
193
|
-
const recovered = await call(edit, {
|
|
194
|
-
path: "config.ts",
|
|
195
|
-
edits: [{ op: "replace", anchor: candidateAnchor, body: ["export const RETRIES = 5;"] }],
|
|
196
|
-
});
|
|
197
|
-
assert.equal(recovered.isError, undefined);
|
|
198
|
-
|
|
199
|
-
const resultText = await readFile(file, "utf8");
|
|
200
|
-
assert.ok(resultText.includes("export const RETRIES = 5;"));
|
|
201
|
-
assert.ok(resultText.includes("// Header comment line 1"));
|
|
202
|
-
}));
|
|
203
|
-
|
|
204
|
-
test("E2E Dev D4: Bulk replace with Action Fusion then_run validation", async () =>
|
|
205
|
-
withDir(async (dir) => {
|
|
206
|
-
const file = join(dir, "api.ts");
|
|
207
|
-
await writeFile(file, "const old_var_1 = 10;\nconst old_var_2 = 20;\n");
|
|
208
|
-
|
|
209
|
-
let commandRan = false;
|
|
210
|
-
const fusionExecutor = createActionFusionExecutor(async (_id, input) => {
|
|
211
|
-
commandRan = true;
|
|
212
|
-
assert.equal(input.command, "npm run check");
|
|
213
|
-
return "All checks passed";
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
const replace = makeReplaceTool(dir, DEFAULT_CONFIG, fusionExecutor);
|
|
217
|
-
const res = await call(replace, {
|
|
218
|
-
path: "api.ts",
|
|
219
|
-
replacements: [{ find: "old_var", replace: "new_var" }],
|
|
220
|
-
then_run: { command: "npm run check" },
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
assert.equal(commandRan, true);
|
|
224
|
-
assert.equal(res.details.actionFusion.command, "succeeded");
|
|
225
|
-
assert.equal(res.details.actionFusion.freshness, "unchanged");
|
|
226
|
-
assert.match(res.content[1].text, /All checks passed/);
|
|
227
|
-
|
|
228
|
-
const content = await readFile(file, "utf8");
|
|
229
|
-
assert.equal(content, "const new_var_1 = 10;\nconst new_var_2 = 20;\n");
|
|
230
|
-
}));
|
|
231
|
-
|
|
232
|
-
test("E2E Dev D5: Line ending preservation and deletion successor anchor", async () =>
|
|
233
|
-
withDir(async (dir) => {
|
|
234
|
-
const file = join(dir, "crlf.txt");
|
|
235
|
-
// CRLF file
|
|
236
|
-
const crlfText = "line1\r\nline2\r\nline3\r\nline4\r\n";
|
|
237
|
-
await writeFile(file, crlfText);
|
|
238
|
-
|
|
239
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
240
|
-
|
|
241
|
-
// Delete line 2: line 3 shifts to line 2 (deletion successor)
|
|
242
|
-
const res = await call(edit, {
|
|
243
|
-
path: "crlf.txt",
|
|
244
|
-
edits: [{ op: "delete", anchor: h(crlfText, 2) }],
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
// Deletion successor anchor should be returned with full row
|
|
248
|
-
assert.match(res.content[0].text, /2#[0-9A-Z]+│line3/);
|
|
249
|
-
|
|
250
|
-
// File bytes must retain CRLF line endings
|
|
251
|
-
const afterBytes = await readFile(file);
|
|
252
|
-
assert.equal(afterBytes.toString("utf8"), "line1\r\nline3\r\nline4\r\n");
|
|
253
|
-
}));
|
|
254
|
-
|
|
255
|
-
test("E2E Dev D6: structured edit batches use one original snapshot", async () =>
|
|
256
|
-
withDir(async (dir) => {
|
|
257
|
-
const file = join(dir, "batch.txt");
|
|
258
|
-
const original = "alpha\nbeta\ngamma\n";
|
|
259
|
-
await writeFile(file, original);
|
|
260
|
-
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
261
|
-
path: "batch.txt",
|
|
262
|
-
edits: [
|
|
263
|
-
{ op: "replace", anchor: h(original, 1), body: ["ALPHA"] },
|
|
264
|
-
{ op: "replace", anchor: h(original, 3), body: ["GAMMA"] },
|
|
265
|
-
],
|
|
266
|
-
});
|
|
267
|
-
assert.equal(await readFile(file, "utf8"), "ALPHA\nbeta\nGAMMA\n");
|
|
268
|
-
}));
|
|
269
|
-
|
|
270
|
-
// ---------------------------------------------------------------------------
|
|
271
|
-
// Holdout Set (H1 - H3)
|
|
272
|
-
// ---------------------------------------------------------------------------
|
|
273
|
-
|
|
274
|
-
test("E2E Holdout H1: Ambiguous candidates provide distinguishing neighborhoods", async () =>
|
|
275
|
-
withDir(async (dir) => {
|
|
276
|
-
const file = join(dir, "ambiguous.txt");
|
|
277
|
-
const text =
|
|
278
|
-
["function one() {", " return true;", "}", "function two() {", " return true;", "}"].join(
|
|
279
|
-
"\n",
|
|
280
|
-
) + "\n";
|
|
281
|
-
await writeFile(file, text);
|
|
282
|
-
|
|
283
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
284
|
-
// The cited content moved away from line 3 and now occurs at lines 2 and 5.
|
|
285
|
-
const staleAnchor = h("header\nheader\n return true;\n", 3);
|
|
286
|
-
let candidateAnchor = "";
|
|
287
|
-
await assert.rejects(
|
|
288
|
-
call(edit, {
|
|
289
|
-
path: "ambiguous.txt",
|
|
290
|
-
edits: [{ op: "replace", anchor: staleAnchor, body: [" return false;"] }],
|
|
291
|
-
}),
|
|
292
|
-
(error: Error) => {
|
|
293
|
-
assert.match(error.message, /ambiguous checksum matches/);
|
|
294
|
-
assert.match(error.message, /Ambiguous-candidate neighborhoods/);
|
|
295
|
-
assert.ok(error.message.includes(`${h(text, 2)}│ return true;`));
|
|
296
|
-
assert.ok(error.message.includes(`${h(text, 5)}│ return true;`));
|
|
297
|
-
assert.ok(error.message.includes(`${h(text, 1)}│function one() {`));
|
|
298
|
-
assert.ok(error.message.includes(`${h(text, 4)}│function two() {`));
|
|
299
|
-
candidateAnchor = anchorLine(
|
|
300
|
-
error.message.split("Ambiguous-candidate neighborhoods")[1],
|
|
301
|
-
5,
|
|
302
|
-
);
|
|
303
|
-
return true;
|
|
304
|
-
},
|
|
305
|
-
);
|
|
306
|
-
assert.equal(await readFile(file, "utf8"), text);
|
|
307
|
-
await call(edit, {
|
|
308
|
-
path: "ambiguous.txt",
|
|
309
|
-
edits: [{ op: "replace", anchor: candidateAnchor, body: [" return false;"] }],
|
|
310
|
-
});
|
|
311
|
-
assert.equal(
|
|
312
|
-
await readFile(file, "utf8"),
|
|
313
|
-
"function one() {\n return true;\n}\nfunction two() {\n return false;\n}\n",
|
|
314
|
-
);
|
|
315
|
-
}));
|
|
316
|
-
|
|
317
|
-
test("E2E Holdout H2: Action Fusion command failure preserves file changes and anchors", async () =>
|
|
318
|
-
withDir(async (dir) => {
|
|
319
|
-
const file = join(dir, "fusion_fail.ts");
|
|
320
|
-
await writeFile(file, "let x = 1;\n");
|
|
321
|
-
|
|
322
|
-
const fusionExecutor = createActionFusionExecutor(async () => {
|
|
323
|
-
throw new Error("Syntax error on line 42");
|
|
324
|
-
});
|
|
325
|
-
|
|
326
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG, fusionExecutor);
|
|
327
|
-
const res = await call(edit, {
|
|
328
|
-
path: "fusion_fail.ts",
|
|
329
|
-
edits: [{ op: "replace", anchor: h("let x = 1;\n", 1), body: ["let x = 2;"] }],
|
|
330
|
-
then_run: { command: "test" },
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
// File change must be published and saved
|
|
334
|
-
assert.equal(await readFile(file, "utf8"), "let x = 2;\n");
|
|
335
|
-
assert.equal(res.details.actionFusion.publication, "PUBLISHED");
|
|
336
|
-
assert.equal(res.details.actionFusion.command, "failed");
|
|
337
|
-
assert.match(res.content[0].text, /Edited fusion_fail\.ts/);
|
|
338
|
-
assert.match(res.content[0].text, /Updated anchors/);
|
|
339
|
-
assert.match(res.content[1].text, /Syntax error on line 42/);
|
|
340
|
-
}));
|
|
341
|
-
|
|
342
|
-
test("E2E Holdout H3: UTF-8 BOM file retains BOM at byte 0 after first-line edit", async () =>
|
|
343
|
-
withDir(async (dir) => {
|
|
344
|
-
const file = join(dir, "bom.txt");
|
|
345
|
-
const bomContent = "\uFEFFline1\nline2\n";
|
|
346
|
-
await writeFile(file, bomContent);
|
|
347
|
-
|
|
348
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
349
|
-
const a1 = h(bomContent, 1);
|
|
350
|
-
|
|
351
|
-
await call(edit, {
|
|
352
|
-
path: "bom.txt",
|
|
353
|
-
edits: [{ op: "replace", anchor: a1, body: ["replaced1"] }],
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
const afterBytes = await readFile(file);
|
|
357
|
-
assert.equal(afterBytes[0], 0xef);
|
|
358
|
-
assert.equal(afterBytes[1], 0xbb);
|
|
359
|
-
assert.equal(afterBytes[2], 0xbf);
|
|
360
|
-
assert.equal(afterBytes.toString("utf8"), "\uFEFFreplaced1\nline2\n");
|
|
361
|
-
}));
|