@criogaid/pi-hashline-edit 0.1.0
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/LICENSE +21 -0
- package/README.md +279 -0
- package/package.json +50 -0
- package/src/core/apply.test.ts +327 -0
- package/src/core/apply.ts +264 -0
- package/src/core/hash.test.ts +54 -0
- package/src/core/hash.ts +68 -0
- package/src/core/index.ts +13 -0
- package/src/core/lines.test.ts +33 -0
- package/src/core/lines.ts +52 -0
- package/src/core/types.ts +87 -0
- package/src/index.ts +44 -0
- package/src/integration/grep-rg.test.ts +390 -0
- package/src/pi/action-fusion.test.ts +244 -0
- package/src/pi/action-fusion.ts +284 -0
- package/src/pi/config.test.ts +22 -0
- package/src/pi/config.ts +56 -0
- package/src/pi/edit-tool.ts +367 -0
- package/src/pi/execute.test.ts +521 -0
- package/src/pi/failure-context.test.ts +88 -0
- package/src/pi/failure-context.ts +98 -0
- package/src/pi/failure-path.test.ts +60 -0
- package/src/pi/file-commit.test.ts +200 -0
- package/src/pi/file-commit.ts +185 -0
- package/src/pi/fusion-card.test.ts +69 -0
- package/src/pi/fusion-card.ts +78 -0
- package/src/pi/grep-tool.ts +795 -0
- package/src/pi/grep.test.ts +515 -0
- package/src/pi/pi.test.ts +19 -0
- package/src/pi/read-tool.ts +178 -0
- package/src/pi/registration.test.ts +41 -0
- package/src/pi/render.ts +103 -0
- package/src/pi/replace-tool.ts +303 -0
- package/src/pi/replace.test.ts +287 -0
- package/src/pi/rg-line-filter.test.ts +79 -0
- package/src/pi/rg-line-filter.ts +358 -0
- package/src/pi/rg-line-ranges.test.ts +30 -0
- package/src/pi/rg-line-ranges.ts +106 -0
- package/src/pi/state.ts +27 -0
- package/src/pi/write-tool.ts +83 -0
- package/src/pi/write.test.ts +126 -0
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi integration execute tests: drive the real makeReadOverride/makeEditOverride
|
|
3
|
+
* execute, covering text read with anchors, the hashline edit round-trip,
|
|
4
|
+
* chained edits via returned anchors, and error returns with isError.
|
|
5
|
+
*/
|
|
6
|
+
import { test } from "node:test";
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import { mkdir, mkdtemp, rm, readFile, writeFile } from "node:fs/promises";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { createEditTool, initTheme } from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
13
|
+
import registerHashline from "../index.ts";
|
|
14
|
+
import { makeEditOverride } from "./edit-tool.ts";
|
|
15
|
+
import { makeReadOverride } from "./read-tool.ts";
|
|
16
|
+
import { makeWriteOverride } from "./write-tool.ts";
|
|
17
|
+
import { makeReplaceTool } from "./replace-tool.ts";
|
|
18
|
+
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
19
|
+
import { getState } from "./state.ts";
|
|
20
|
+
import { computeLineHash } from "../core/hash.ts";
|
|
21
|
+
import { splitLines } from "../core/lines.ts";
|
|
22
|
+
|
|
23
|
+
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
24
|
+
const dir = await mkdtemp(join(tmpdir(), "hl-"));
|
|
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) => tool.execute("0", params, undefined, undefined);
|
|
33
|
+
|
|
34
|
+
/** Anchor a model would copy from read output for `line` of `text` (1-based). */
|
|
35
|
+
function h(text: string, line: number) {
|
|
36
|
+
return `${line}#${computeLineHash(line, splitLines(text)[line - 1])}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Extract a `LINE#HASH` anchor from a read/edit result text block. */
|
|
40
|
+
function anchorLine(block: string, line: number) {
|
|
41
|
+
const m = new RegExp(`^${line}#([0-9A-Z]+)│`, "m").exec(block);
|
|
42
|
+
if (!m) throw new Error(`line ${line} anchor not found in block`);
|
|
43
|
+
return `${line}#${m[1]}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
test("read execute: text outputs LINE#HASH│content", async () => {
|
|
47
|
+
await withDir(async (dir) => {
|
|
48
|
+
await writeFile(join(dir, "f.txt"), "line1\nline2\n");
|
|
49
|
+
const r: any = await call(makeReadOverride(dir), { path: "f.txt" });
|
|
50
|
+
const text = r.content[0];
|
|
51
|
+
assert.equal(text.type, "text");
|
|
52
|
+
assert.match(text.text, /1#[0-9A-Z]+│line1/);
|
|
53
|
+
assert.match(text.text, /2#[0-9A-Z]+│line2/);
|
|
54
|
+
assert.match(text.text, /f\.txt · 2 lines/);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("read execute: a missing final newline is stated in the header", async () => {
|
|
59
|
+
await withDir(async (dir) => {
|
|
60
|
+
await writeFile(join(dir, "f.txt"), "line1\nline2");
|
|
61
|
+
const bare: any = await call(makeReadOverride(dir), { path: "f.txt" });
|
|
62
|
+
assert.match(bare.content[0].text, /f\.txt · 2 lines · no trailing newline/);
|
|
63
|
+
|
|
64
|
+
await writeFile(join(dir, "g.txt"), "line1\nline2\n");
|
|
65
|
+
const terminated: any = await call(makeReadOverride(dir), { path: "g.txt" });
|
|
66
|
+
assert.doesNotMatch(terminated.content[0].text, /no trailing newline/);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("edit execute: a file without a final newline stays byte-exact", async () => {
|
|
71
|
+
await withDir(async (dir) => {
|
|
72
|
+
const f = join(dir, "f.txt");
|
|
73
|
+
// The benchmark's literal-1-no-final-newline fixture: no terminator, plus a
|
|
74
|
+
// word-joiner the model cannot see. Only line 2 may change; the missing
|
|
75
|
+
// terminator must not turn into a new byte.
|
|
76
|
+
const text = "guard\nold value\u2060";
|
|
77
|
+
await writeFile(f, text);
|
|
78
|
+
await call(makeReadOverride(dir), { path: "f.txt" });
|
|
79
|
+
const r: any = await call(makeEditOverride(dir), {
|
|
80
|
+
path: "f.txt",
|
|
81
|
+
edits: [{ op: "replace", anchor: h(text, 2), body: ["new value\u2060"] }],
|
|
82
|
+
});
|
|
83
|
+
assert.equal(r.isError, undefined, "should not be an error");
|
|
84
|
+
assert.equal(await readFile(f, "utf-8"), "guard\nnew value\u2060");
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("edit execute: hashline round-trip (read → edit → file changed)", async () => {
|
|
89
|
+
await withDir(async (dir) => {
|
|
90
|
+
const f = join(dir, "f.txt");
|
|
91
|
+
const text = "a\nb\nc\n";
|
|
92
|
+
await writeFile(f, text);
|
|
93
|
+
await call(makeReadOverride(dir), { path: "f.txt" });
|
|
94
|
+
const r: any = await call(makeEditOverride(dir), {
|
|
95
|
+
path: "f.txt",
|
|
96
|
+
edits: [{ op: "replace", anchor: h(text, 2), body: ["B"] }],
|
|
97
|
+
});
|
|
98
|
+
assert.equal(r.isError, undefined, "should not be an error");
|
|
99
|
+
assert.equal(await readFile(f, "utf-8"), "a\nB\nc\n");
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("edit execute: multiple ops in one call", async () => {
|
|
104
|
+
await withDir(async (dir) => {
|
|
105
|
+
const f = join(dir, "f.txt");
|
|
106
|
+
const text = "a\nb\nc\n";
|
|
107
|
+
await writeFile(f, text);
|
|
108
|
+
await call(makeReadOverride(dir), { path: "f.txt" });
|
|
109
|
+
const r: any = await call(makeEditOverride(dir), {
|
|
110
|
+
path: "f.txt",
|
|
111
|
+
edits: [
|
|
112
|
+
{ op: "insert_after", anchor: h(text, 3), body: ["z"] },
|
|
113
|
+
{ op: "replace", anchor: h(text, 1), body: ["A"] },
|
|
114
|
+
],
|
|
115
|
+
});
|
|
116
|
+
assert.equal(r.isError, undefined);
|
|
117
|
+
assert.equal(await readFile(f, "utf-8"), "A\nb\nc\nz\n");
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("edit result returns Updated anchors that chain the next edit without a re-read", async () => {
|
|
122
|
+
await withDir(async (dir) => {
|
|
123
|
+
const f = join(dir, "f.txt");
|
|
124
|
+
const text = "a\nb\nc\n";
|
|
125
|
+
await writeFile(f, text);
|
|
126
|
+
const edit = makeEditOverride(dir);
|
|
127
|
+
// first edit (model cites the read anchor for line 1)
|
|
128
|
+
const r1: any = await call(edit, {
|
|
129
|
+
path: "f.txt",
|
|
130
|
+
edits: [{ op: "replace", anchor: h(text, 1), body: ["A"] }],
|
|
131
|
+
});
|
|
132
|
+
assert.equal(r1.isError, undefined);
|
|
133
|
+
const out: string = r1.content[0].text;
|
|
134
|
+
assert.match(out, /Updated anchors/);
|
|
135
|
+
// second edit chains on the anchor returned by the first edit — no read in between
|
|
136
|
+
const r2: any = await call(edit, {
|
|
137
|
+
path: "f.txt",
|
|
138
|
+
edits: [{ op: "replace", anchor: anchorLine(out, 1), body: ["AA"] }],
|
|
139
|
+
});
|
|
140
|
+
assert.equal(r2.isError, undefined);
|
|
141
|
+
assert.equal(await readFile(f, "utf-8"), "AA\nb\nc\n");
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("edit result anchors cover an inserted block (chain an edit inside it)", async () => {
|
|
146
|
+
await withDir(async (dir) => {
|
|
147
|
+
const f = join(dir, "f.txt");
|
|
148
|
+
const text = "a\nb\n";
|
|
149
|
+
await writeFile(f, text);
|
|
150
|
+
const edit = makeEditOverride(dir);
|
|
151
|
+
const r1: any = await call(edit, {
|
|
152
|
+
path: "f.txt",
|
|
153
|
+
edits: [{ op: "insert_after", anchor: h(text, 2), body: ["c", "d", "e"] }],
|
|
154
|
+
});
|
|
155
|
+
assert.equal(r1.isError, undefined);
|
|
156
|
+
const out: string = r1.content[0].text;
|
|
157
|
+
// line 4 (d, one of the inserted lines) must be anchored in the result
|
|
158
|
+
const a4 = anchorLine(out, 4);
|
|
159
|
+
const r2: any = await call(edit, {
|
|
160
|
+
path: "f.txt",
|
|
161
|
+
edits: [{ op: "replace", anchor: a4, body: ["DD"] }],
|
|
162
|
+
});
|
|
163
|
+
assert.equal(r2.isError, undefined);
|
|
164
|
+
assert.equal(await readFile(f, "utf-8"), "a\nb\nc\nDD\ne\n");
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test("unrelated external change does NOT block an edit on a stable line", async () => {
|
|
169
|
+
await withDir(async (dir) => {
|
|
170
|
+
const f = join(dir, "f.txt");
|
|
171
|
+
const text = "a\nb\nc\n";
|
|
172
|
+
await writeFile(f, text);
|
|
173
|
+
// simulate an external change at line 3 between read and edit
|
|
174
|
+
await writeFile(f, "a\nb\nCHANGED\n");
|
|
175
|
+
const r: any = await call(makeEditOverride(dir), {
|
|
176
|
+
path: "f.txt",
|
|
177
|
+
edits: [{ op: "replace", anchor: h(text, 1), body: ["A"] }],
|
|
178
|
+
});
|
|
179
|
+
assert.equal(r.isError, undefined);
|
|
180
|
+
assert.equal(await readFile(f, "utf-8"), "A\nb\nCHANGED\n");
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("edit on a line that changed externally → anchor mismatch", async () => {
|
|
185
|
+
await withDir(async (dir) => {
|
|
186
|
+
const f = join(dir, "f.txt");
|
|
187
|
+
const text = "a\nb\nc\n";
|
|
188
|
+
await writeFile(f, text);
|
|
189
|
+
await writeFile(f, "a\nBCHANGED\nc\n"); // line 2 changed
|
|
190
|
+
await assert.rejects(
|
|
191
|
+
call(makeEditOverride(dir), {
|
|
192
|
+
path: "f.txt",
|
|
193
|
+
edits: [{ op: "replace", anchor: h(text, 2), body: ["x"] }],
|
|
194
|
+
}),
|
|
195
|
+
/anchor|re-read/i,
|
|
196
|
+
);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test("unresolved anchor context supplies a fresh nearby token for a verified retry", async () => {
|
|
201
|
+
await withDir(async (dir) => {
|
|
202
|
+
const file = join(dir, "recover.txt");
|
|
203
|
+
const observed = ["one", "two", "three", "four", "old marker", "six", "seven", "eight"].join("\n") + "\n";
|
|
204
|
+
await writeFile(file, ["one", "two", "three", "four", "changed", "six", "new marker", "eight"].join("\n") + "\n");
|
|
205
|
+
const edit = makeEditOverride(dir);
|
|
206
|
+
let retryAnchor = "";
|
|
207
|
+
await assert.rejects(call(edit, {
|
|
208
|
+
path: "recover.txt",
|
|
209
|
+
edits: [{ op: "replace", anchor: h(observed, 5), body: ["updated"] }],
|
|
210
|
+
}), (error: Error) => {
|
|
211
|
+
assert.match(error.message, /Anchor mismatch: 1 unresolved/);
|
|
212
|
+
assert.match(error.message, /No changes written by this edit batch/);
|
|
213
|
+
assert.match(error.message, /@@ lines 2-8 @@/);
|
|
214
|
+
retryAnchor = /^(7#[0-9A-Z]+)│new marker$/m.exec(error.message)?.[1] ?? "";
|
|
215
|
+
return retryAnchor !== "";
|
|
216
|
+
});
|
|
217
|
+
await call(edit, { path: "recover.txt", edits: [{ op: "replace", anchor: retryAnchor, body: ["updated"] }] });
|
|
218
|
+
assert.equal(await readFile(file, "utf8"), "one\ntwo\nthree\nfour\nchanged\nsix\nupdated\neight\n");
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("edit execute: no read before edit → anchor verification fails", async () => {
|
|
223
|
+
await withDir(async (dir) => {
|
|
224
|
+
await writeFile(join(dir, "f.txt"), "a\nb\n");
|
|
225
|
+
await assert.rejects(
|
|
226
|
+
call(makeEditOverride(dir), {
|
|
227
|
+
path: "f.txt",
|
|
228
|
+
edits: [{ op: "replace", anchor: "1#XXXX", body: ["A"] }],
|
|
229
|
+
}),
|
|
230
|
+
/anchor|re-read/i,
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test("edit execute: empty edits → throws", async () => {
|
|
236
|
+
await withDir(async (dir) => {
|
|
237
|
+
await writeFile(join(dir, "f.txt"), "a\n");
|
|
238
|
+
await assert.rejects(
|
|
239
|
+
call(makeEditOverride(dir), { path: "f.txt", edits: [] }),
|
|
240
|
+
/empty|missing/i,
|
|
241
|
+
);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test("edit execute: malformed op (replace without body) → throws", async () => {
|
|
246
|
+
await withDir(async (dir) => {
|
|
247
|
+
await writeFile(join(dir, "f.txt"), "a\n");
|
|
248
|
+
await assert.rejects(
|
|
249
|
+
call(makeEditOverride(dir), {
|
|
250
|
+
path: "f.txt",
|
|
251
|
+
edits: [{ op: "replace", anchor: "1#XX" }],
|
|
252
|
+
}),
|
|
253
|
+
/body/i,
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test("edit execute: delete op", async () => {
|
|
259
|
+
await withDir(async (dir) => {
|
|
260
|
+
const f = join(dir, "f.txt");
|
|
261
|
+
const text = "a\nb\nc\n";
|
|
262
|
+
await writeFile(f, text);
|
|
263
|
+
await call(makeReadOverride(dir), { path: "f.txt" });
|
|
264
|
+
const r: any = await call(makeEditOverride(dir), {
|
|
265
|
+
path: "f.txt",
|
|
266
|
+
edits: [{ op: "delete", anchor: h(text, 2) }],
|
|
267
|
+
});
|
|
268
|
+
assert.equal(r.isError, undefined);
|
|
269
|
+
assert.equal(await readFile(f, "utf-8"), "a\nc\n");
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test("disabled config registers no tools — built-ins remain", async () => {
|
|
274
|
+
await withDir(async (dir) => {
|
|
275
|
+
const oldCwd = process.cwd();
|
|
276
|
+
const state = getState();
|
|
277
|
+
const previous = state.config;
|
|
278
|
+
try {
|
|
279
|
+
await mkdir(join(dir, ".pi"));
|
|
280
|
+
await writeFile(join(dir, ".pi", "settings.json"), JSON.stringify({ hashlineEdit: { enabled: false } }));
|
|
281
|
+
await writeFile(join(dir, "f.txt"), "old value\n");
|
|
282
|
+
process.chdir(dir);
|
|
283
|
+
const registered: string[] = [];
|
|
284
|
+
registerHashline({ on() {}, registerTool(tool: { name: string }) { registered.push(tool.name); } } as any);
|
|
285
|
+
assert.deepEqual(registered, []);
|
|
286
|
+
const builtin = createEditTool(dir);
|
|
287
|
+
const params = validateToolArguments(builtin, {
|
|
288
|
+
name: "edit", arguments: { path: "f.txt", edits: [{ oldText: "old value", newText: "new value" }] },
|
|
289
|
+
} as any);
|
|
290
|
+
await call(builtin, params);
|
|
291
|
+
assert.equal(await readFile(join(dir, "f.txt"), "utf-8"), "new value\n");
|
|
292
|
+
} finally {
|
|
293
|
+
process.chdir(oldCwd);
|
|
294
|
+
state.config = previous;
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// --- renderer regression guards (details.diff must be a string, renderResult must not throw) ---
|
|
300
|
+
|
|
301
|
+
const stubTheme = { fg: (_k: string, s: string) => s, bold: (s: string) => s };
|
|
302
|
+
|
|
303
|
+
// renderResult delegates to pi's renderDiff, which reads the global TUI theme
|
|
304
|
+
// singleton — initialize it once for this test process (watcher off by default).
|
|
305
|
+
initTheme();
|
|
306
|
+
|
|
307
|
+
test("edit success: details.diff is a string (not the generateDiffString object)", async () => {
|
|
308
|
+
await withDir(async (dir) => {
|
|
309
|
+
const f = join(dir, "f.txt");
|
|
310
|
+
const text = "a\nb\nc\n";
|
|
311
|
+
await writeFile(f, text);
|
|
312
|
+
await call(makeReadOverride(dir), { path: "f.txt" });
|
|
313
|
+
const r: any = await call(makeEditOverride(dir), {
|
|
314
|
+
path: "f.txt",
|
|
315
|
+
edits: [{ op: "replace", anchor: h(text, 2), body: ["B"] }],
|
|
316
|
+
});
|
|
317
|
+
assert.equal(typeof r.details.diff, "string", "details.diff must be a string");
|
|
318
|
+
assert.equal(typeof r.details.patch, "string");
|
|
319
|
+
assert.equal(typeof r.details.firstChangedLine, "number");
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test("edit success: renderResult renders the diff without throwing", async () => {
|
|
324
|
+
await withDir(async (dir) => {
|
|
325
|
+
const f = join(dir, "f.txt");
|
|
326
|
+
const text = "a\nb\nc\n";
|
|
327
|
+
await writeFile(f, text);
|
|
328
|
+
const edit = makeEditOverride(dir);
|
|
329
|
+
const r: any = await call(edit, {
|
|
330
|
+
path: "f.txt",
|
|
331
|
+
edits: [{ op: "replace", anchor: h(text, 2), body: ["B"] }],
|
|
332
|
+
});
|
|
333
|
+
// @ts-ignore — drive the renderer with a stub theme
|
|
334
|
+
const comp: any = edit.renderResult({ content: r.content, details: r.details }, { isPartial: false, expanded: true }, stubTheme, { isError: r.isError ?? false, state: {}, invalidate: () => {} });
|
|
335
|
+
assert.ok(typeof comp?.text === "string");
|
|
336
|
+
assert.ok(comp.text.includes("B"), "rendered diff should contain the new content");
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test("edit header: renderResult refreshes the call header in place — no invalidate", async () => {
|
|
341
|
+
await withDir(async (dir) => {
|
|
342
|
+
const f = join(dir, "f.txt");
|
|
343
|
+
const text = "a\nb\nc\nd\ne\n";
|
|
344
|
+
await writeFile(f, text);
|
|
345
|
+
await call(makeReadOverride(dir), { path: "f.txt" });
|
|
346
|
+
const edit = makeEditOverride(dir);
|
|
347
|
+
const r: any = await call(edit, {
|
|
348
|
+
path: "f.txt",
|
|
349
|
+
edits: [
|
|
350
|
+
{ op: "replace", anchor: h(text, 2), end: h(text, 3), body: ["B"] },
|
|
351
|
+
{ op: "insert_after", anchor: h(text, 5), body: ["f", "g"] },
|
|
352
|
+
],
|
|
353
|
+
});
|
|
354
|
+
const args = { path: "f.txt", edits: [{ op: "replace" as const }] };
|
|
355
|
+
let invalidated = false;
|
|
356
|
+
const context: any = { args, isError: false, state: {}, invalidate: () => { invalidated = true; } };
|
|
357
|
+
// first pass: renderCall builds the header; no counts exist yet
|
|
358
|
+
const header: any = edit.renderCall(args, stubTheme, context);
|
|
359
|
+
assert.ok(!header.text.includes("+3"), "pre-execution header must not show counts");
|
|
360
|
+
// result lands: renderResult refreshes the SAME component in place
|
|
361
|
+
edit.renderResult({ content: r.content, details: r.details }, { isPartial: false, expanded: true }, stubTheme, context);
|
|
362
|
+
assert.deepEqual(context.state.diffCounts, { added: 3, removed: 2 });
|
|
363
|
+
assert.ok(header.text.includes("+3"), "header should show added count");
|
|
364
|
+
assert.ok(header.text.includes("-2"), "header should show removed count");
|
|
365
|
+
// refreshing via context.invalidate() re-enters updateDisplay and renders
|
|
366
|
+
// the diff twice — the renderer must never call it
|
|
367
|
+
assert.ok(!invalidated, "renderResult must not call invalidate");
|
|
368
|
+
// later full passes (expand/collapse) re-run renderCall; counts survive in state
|
|
369
|
+
const header2: any = edit.renderCall(args, stubTheme, { args, state: context.state, lastComponent: header });
|
|
370
|
+
assert.equal(header2, header, "renderCall reuses the stashed component");
|
|
371
|
+
assert.ok(header2.text.includes("+3"), "re-render keeps the counts");
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test("edit error: renderResult renders the error line without throwing", async () => {
|
|
376
|
+
await withDir(async (dir) => {
|
|
377
|
+
await writeFile(join(dir, "f.txt"), "a\n");
|
|
378
|
+
const edit = makeEditOverride(dir);
|
|
379
|
+
let thrown: any;
|
|
380
|
+
await call(edit, {
|
|
381
|
+
path: "f.txt",
|
|
382
|
+
edits: [{ op: "replace", anchor: "1#XXXX", body: ["A"] }],
|
|
383
|
+
}).catch((e: any) => {
|
|
384
|
+
thrown = e;
|
|
385
|
+
});
|
|
386
|
+
assert.ok(thrown, "expected the edit to throw");
|
|
387
|
+
// @ts-ignore — simulate the framework handing the thrown message to renderResult
|
|
388
|
+
const comp: any = edit.renderResult(
|
|
389
|
+
{ content: [{ type: "text", text: thrown.message }] },
|
|
390
|
+
{ isPartial: false, expanded: false },
|
|
391
|
+
stubTheme,
|
|
392
|
+
{ isError: true },
|
|
393
|
+
);
|
|
394
|
+
assert.ok(typeof comp?.text === "string");
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
test("hash length stays 4 even for runs of identical lines (no explosion)", async () => {
|
|
399
|
+
await withDir(async (dir) => {
|
|
400
|
+
const f = join(dir, "f.txt");
|
|
401
|
+
await writeFile(f, "\n\n\n\ncode\n");
|
|
402
|
+
const r: any = await call(makeReadOverride(dir), { path: "f.txt" });
|
|
403
|
+
const text: string = r.content[0].text;
|
|
404
|
+
for (const m of text.matchAll(/\d+#([0-9A-Z]+)│/g)) {
|
|
405
|
+
assert.equal(m[1].length, 4, `anchor ${m[0]} hash is not 4 chars`);
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
test("read byte truncation counts UTF-8 and separators without cutting anchors", async () => withDir(async (dir) => {
|
|
411
|
+
const maxBytes = 256 * 1024;
|
|
412
|
+
const hashLen = getState().config.hashLen;
|
|
413
|
+
const prefixBytes = Buffer.byteLength(`1#${"X".repeat(hashLen)}│`);
|
|
414
|
+
const first = "界".repeat(40000);
|
|
415
|
+
const second = "x".repeat(maxBytes - Buffer.byteLength(first) - 2 * prefixBytes);
|
|
416
|
+
await writeFile(join(dir, "large.txt"), `${first}\n${second}\n`);
|
|
417
|
+
const result = await call(makeReadOverride(dir), { path: "large.txt" });
|
|
418
|
+
assert.equal(result.details.truncation.outputLines, 1);
|
|
419
|
+
assert.equal(result.details.truncation.truncatedBy, "bytes");
|
|
420
|
+
assert.match(result.content[0].text, /truncated at 256KB/);
|
|
421
|
+
assert.ok(result.content[0].text.includes(`1#${computeLineHash(1, first, hashLen)}│${first}\n`));
|
|
422
|
+
assert.doesNotMatch(result.content[0].text, /\n2#/);
|
|
423
|
+
const next = await call(makeReadOverride(dir), { path: "large.txt", offset: 2, limit: 1 });
|
|
424
|
+
assert.ok(next.content[0].text.includes(`2#${computeLineHash(2, second, hashLen)}│${second}`));
|
|
425
|
+
}));
|
|
426
|
+
|
|
427
|
+
test("read reports an oversized first row without suggesting an ineffective retry", async () => withDir(async (dir) => {
|
|
428
|
+
await writeFile(join(dir, "long.txt"), "x".repeat(256 * 1024));
|
|
429
|
+
const result = await call(makeReadOverride(dir), { path: "long.txt" });
|
|
430
|
+
assert.equal(result.details.truncation.firstLineExceedsLimit, true);
|
|
431
|
+
assert.equal(result.details.truncation.outputLines, 0);
|
|
432
|
+
assert.match(result.content[0].text, /cannot return a complete anchor row/);
|
|
433
|
+
assert.doesNotMatch(result.content[0].text, /use offset\/limit/);
|
|
434
|
+
}));
|
|
435
|
+
|
|
436
|
+
test("read preserves empty files and explicit limits above the native default", async () => withDir(async (dir) => {
|
|
437
|
+
await writeFile(join(dir, "empty.txt"), "");
|
|
438
|
+
const empty = await call(makeReadOverride(dir), { path: "empty.txt" });
|
|
439
|
+
assert.match(empty.content[0].text, /0 lines/);
|
|
440
|
+
assert.equal(empty.details, undefined);
|
|
441
|
+
await writeFile(join(dir, "many.txt"), "x\n".repeat(2001));
|
|
442
|
+
const many = await call(makeReadOverride(dir), { path: "many.txt", limit: 2001 });
|
|
443
|
+
assert.match(many.content[0].text, /\n2001#[0-9A-Z]+│x/);
|
|
444
|
+
assert.equal(many.details, undefined);
|
|
445
|
+
}));
|
|
446
|
+
|
|
447
|
+
test("native read and write renderers preserve resource titles, previews, and full errors", async () => withDir(async (dir) => {
|
|
448
|
+
const context = { cwd: dir, state: {}, argsComplete: true, expanded: false, isPartial: false, lastComponent: undefined };
|
|
449
|
+
const read = makeReadOverride(dir);
|
|
450
|
+
const readCall = read.renderCall!({ path: "SKILL.md", offset: 2, limit: 3 }, stubTheme as any, { ...context, args: { path: "SKILL.md" } } as any);
|
|
451
|
+
assert.match(readCall.render(120).join("\n"), /\[skill\]/);
|
|
452
|
+
const write = makeWriteOverride(dir);
|
|
453
|
+
const writeCall = write.renderCall({ path: "preview.txt", content: "native content preview\n" }, stubTheme, context);
|
|
454
|
+
assert.match(writeCall.render(120).join("\n"), /native content preview/);
|
|
455
|
+
const error = write.renderResult({ content: [{ type: "text", text: "first error\nsecond error" }] }, { isPartial: false, expanded: false }, stubTheme, { ...context, isError: true });
|
|
456
|
+
assert.match(error.render(120).join("\n"), /first error[\s\S]*second error/);
|
|
457
|
+
}));
|
|
458
|
+
|
|
459
|
+
test("copied string anchors validate and replace an inclusive range", async () => withDir(async (dir) => {
|
|
460
|
+
const original = "a\nb\nc\nd\n";
|
|
461
|
+
await writeFile(join(dir, "range.txt"), original);
|
|
462
|
+
const read = await call(makeReadOverride(dir), { path: "range.txt" });
|
|
463
|
+
const edit = makeEditOverride(dir);
|
|
464
|
+
const args = validateToolArguments(edit as any, { type: "toolCall", id: "range", name: "edit", arguments: { path: "range.txt", edits: [{ op: "replace", anchor: anchorLine(read.content[0].text, 2), end: anchorLine(read.content[0].text, 3), body: ["merged"] }] } });
|
|
465
|
+
await call(edit, args);
|
|
466
|
+
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), "a\nmerged\nd\n");
|
|
467
|
+
}));
|
|
468
|
+
|
|
469
|
+
test("invalid anchors and conflicting fields fail before changing the file", async () => withDir(async (dir) => {
|
|
470
|
+
const original = "a\nb\n";
|
|
471
|
+
await writeFile(join(dir, "invalid.txt"), original);
|
|
472
|
+
const edit = makeEditOverride(dir);
|
|
473
|
+
const anchor = h(original, 1);
|
|
474
|
+
const invalid = [
|
|
475
|
+
{ op: "replace", anchor: { line: 1, hash: anchor.split("#")[1] }, body: ["changed"] },
|
|
476
|
+
...['0#AB', '-1#AB', '1.5#AB', '1#', '1#ab', '1#AB│a', '9007199254740993#AB'].map(anchor => ({ op: "replace", anchor, body: ["changed"] })),
|
|
477
|
+
{ op: "insert_after", anchor, end: anchor, body: ["changed"] },
|
|
478
|
+
{ op: "append", anchor, body: ["changed"] },
|
|
479
|
+
{ op: "delete", anchor, body: ["changed"] },
|
|
480
|
+
];
|
|
481
|
+
for (const operation of invalid) {
|
|
482
|
+
await assert.rejects(call(edit, { path: "invalid.txt", edits: [operation] }), /Invalid anchor|does not accept/);
|
|
483
|
+
assert.equal(await readFile(join(dir, "invalid.txt"), "utf8"), original);
|
|
484
|
+
}
|
|
485
|
+
assert.throws(() => validateToolArguments(edit as any, { type: "toolCall", id: "invalid", name: "edit", arguments: { path: "invalid.txt", edits: [invalid[0]] } as any }));
|
|
486
|
+
}));
|
|
487
|
+
|
|
488
|
+
test("shifted-anchor recovery returns a token that can be copied into the retry", async () => withDir(async (dir) => {
|
|
489
|
+
const original = "a\nb\n";
|
|
490
|
+
await writeFile(join(dir, "shift.txt"), "prefix\n" + original);
|
|
491
|
+
const edit = makeEditOverride(dir);
|
|
492
|
+
let replacement = "";
|
|
493
|
+
await assert.rejects(call(edit, { path: "shift.txt", edits: [{ op: "replace", anchor: h(original, 2), body: ["B"] }] }), (error: Error) => {
|
|
494
|
+
replacement = /"(3#[0-9A-Z]+)"/.exec(error.message)?.[1] ?? "";
|
|
495
|
+
return replacement !== "";
|
|
496
|
+
});
|
|
497
|
+
await call(edit, { path: "shift.txt", edits: [{ op: "replace", anchor: replacement, body: ["B"] }] });
|
|
498
|
+
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), "prefix\na\nB\n");
|
|
499
|
+
}));
|
|
500
|
+
|
|
501
|
+
test("failed commands preserve mutation results and stay out of all main card renderers", async () => withDir(async (dir) => {
|
|
502
|
+
const commands: string[] = [];
|
|
503
|
+
const fusion = createActionFusionExecutor(async () => { throw new Error("command-only diagnostic\nCommand exited with code 7"); }, event => commands.push(event.command));
|
|
504
|
+
const cases = [
|
|
505
|
+
{ tool: makeEditOverride(dir, fusion), args: { path: "edit.txt", edits: [{ op: "append", body: ["after"] }] }, expected: "before\nafter\n" },
|
|
506
|
+
{ tool: makeReplaceTool(dir, fusion), args: { path: "replace.txt", find: "before", replace: "after" }, expected: "after\n" },
|
|
507
|
+
{ tool: makeWriteOverride(dir, fusion), args: { path: "write.txt", content: "after\n" }, expected: "after\n" },
|
|
508
|
+
];
|
|
509
|
+
for (const { tool, args, expected } of cases) {
|
|
510
|
+
await writeFile(join(dir, args.path), "before\n");
|
|
511
|
+
const result = await tool.execute(args.path, { ...args, then_run: { command: "check" } }, undefined, undefined, { cwd: dir });
|
|
512
|
+
assert.equal(result.isError, undefined);
|
|
513
|
+
assert.equal(result.details.actionFusion.command, "failed");
|
|
514
|
+
assert.match(result.content.at(-1).text, /then_run:failed[\s\S]*command-only diagnostic/);
|
|
515
|
+
if (tool.name !== "write") assert.equal(typeof result.details.diff, "string");
|
|
516
|
+
const rendered = tool.renderResult(result, { expanded: true, isPartial: false }, stubTheme, { args, state: {}, cwd: dir, isError: false });
|
|
517
|
+
assert.doesNotMatch(rendered.render(120).join("\n"), /command-only diagnostic|then_run:failed/);
|
|
518
|
+
assert.equal(await readFile(join(dir, args.path), "utf8"), expected);
|
|
519
|
+
}
|
|
520
|
+
assert.deepEqual(commands, ["waiting", "running", "failed", "waiting", "running", "failed", "waiting", "running", "failed"]);
|
|
521
|
+
}));
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { computeLineHash } from "../core/hash.ts";
|
|
4
|
+
import type { AnchorFailure } from "../core/types.ts";
|
|
5
|
+
import { formatFailureContext } from "./failure-context.ts";
|
|
6
|
+
|
|
7
|
+
function unresolved(line: number): AnchorFailure {
|
|
8
|
+
return {
|
|
9
|
+
opIndex: 0,
|
|
10
|
+
which: "anchor",
|
|
11
|
+
op: "replace",
|
|
12
|
+
cited: { line, hash: "OLD" },
|
|
13
|
+
recovery: { kind: "none" },
|
|
14
|
+
current: null,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
test("failure context is omitted when every anchor was recovered", () => {
|
|
19
|
+
const found: AnchorFailure = { ...unresolved(2), recovery: { kind: "found", newLine: 3, newHash: "NEW" } };
|
|
20
|
+
assert.equal(formatFailureContext("a\nb\nc\n", [found], 4), "");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("failure context merges adjacent windows and emits each anchored line once", () => {
|
|
24
|
+
const lines = Array.from({ length: 14 }, (_, index) => `line ${index + 1}`);
|
|
25
|
+
const result = formatFailureContext(`${lines.join("\n")}\n`, [unresolved(5), unresolved(9)], 4);
|
|
26
|
+
assert.match(result, /@@ lines 2-12 @@/);
|
|
27
|
+
assert.match(result, /Context rows: 11\/11; 0 omitted/);
|
|
28
|
+
for (let line = 2; line <= 12; line++) {
|
|
29
|
+
const row = `${line}#${computeLineHash(line, lines[line - 1])}│${lines[line - 1]}`;
|
|
30
|
+
assert.equal(result.split(row).length - 1, 1);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("failure context clamps BOF, EOF, and out-of-range anchors without padding", () => {
|
|
35
|
+
const text = Array.from({ length: 10 }, (_, index) => `${index + 1}`).join("\n");
|
|
36
|
+
const result = formatFailureContext(text, [unresolved(1000), unresolved(-10)], 4);
|
|
37
|
+
assert.match(result, /@@ lines 1-4 @@/);
|
|
38
|
+
assert.match(result, /@@ lines 7-10 @@/);
|
|
39
|
+
assert.match(result, /Context rows: 8\/8; 0 omitted/);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("failure context returns no invented anchor for an empty file", () => {
|
|
43
|
+
const result = formatFailureContext("", [unresolved(1)], 4);
|
|
44
|
+
assert.match(result, /file is empty/i);
|
|
45
|
+
assert.doesNotMatch(result, /\d+#[0-9A-Z]+│/);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("failure context keeps the lowest 40 rows and reports row truncation", () => {
|
|
49
|
+
const text = Array.from({ length: 100 }, (_, index) => `line ${index + 1}`).join("\n");
|
|
50
|
+
const result = formatFailureContext(text, [4, 20, 36, 52, 68, 84].map(unresolved), 4);
|
|
51
|
+
assert.match(result, /Context rows: 40\/42; 2 omitted/);
|
|
52
|
+
assert.match(result, /Context truncated: row limit/);
|
|
53
|
+
assert.match(result, /85#[0-9A-Z]+│line 85/);
|
|
54
|
+
assert.doesNotMatch(result, /86#[0-9A-Z]+│line 86/);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("failure context never truncates or skips an oversized first row", () => {
|
|
58
|
+
const oversized = "界".repeat(16 * 1024);
|
|
59
|
+
const result = formatFailureContext(`${oversized}\nshort\n`, [unresolved(1)], 4);
|
|
60
|
+
assert.match(result, /No context row fits the byte budget/);
|
|
61
|
+
assert.match(result, /Context rows: 0\/2; 2 omitted/);
|
|
62
|
+
assert.match(result, /Context truncated: byte limit/);
|
|
63
|
+
assert.doesNotMatch(result, /│short/);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("failure context accepts an exact 16 KiB row and then stops at the next row", () => {
|
|
67
|
+
const exact = "x".repeat(16 * 1024 - 10);
|
|
68
|
+
const result = formatFailureContext(`${exact}\nlater\n`, [unresolved(1)], 4);
|
|
69
|
+
assert.match(result, /Context rows: 1\/2; 1 omitted/);
|
|
70
|
+
assert.match(result, /Context truncated: byte limit/);
|
|
71
|
+
assert.match(result, new RegExp(`1#${computeLineHash(1, exact)}│x`));
|
|
72
|
+
assert.doesNotMatch(result, /│later/);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("failure context stops at an oversized middle row without skipping it", () => {
|
|
76
|
+
const oversized = "界".repeat(16 * 1024);
|
|
77
|
+
const result = formatFailureContext(`short\n${oversized}\nlater\n`, [unresolved(2)], 4);
|
|
78
|
+
assert.match(result, /1#[0-9A-Z]+│short/);
|
|
79
|
+
assert.match(result, /Context rows: 1\/3; 2 omitted/);
|
|
80
|
+
assert.doesNotMatch(result, /3#[0-9A-Z]+│later/);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("failure context hashes canonical CRLF lines with the captured hash length", () => {
|
|
84
|
+
const result = formatFailureContext("alpha\r\n\r\nomega\r\n", [unresolved(2)], 6);
|
|
85
|
+
assert.match(result, new RegExp(`1#${computeLineHash(1, "alpha", 6)}│alpha`));
|
|
86
|
+
assert.match(result, new RegExp(`2#${computeLineHash(2, "", 6)}│(?:\\n|$)`));
|
|
87
|
+
assert.match(result, new RegExp(`3#${computeLineHash(3, "omega", 6)}│omega`));
|
|
88
|
+
});
|