@criogaid/pi-hashline-edit 0.5.0 → 0.6.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.
Files changed (66) hide show
  1. package/README.md +66 -22
  2. package/package.json +6 -6
  3. package/src/core/apply.test.ts +66 -16
  4. package/src/core/apply.ts +36 -19
  5. package/src/core/lines.ts +3 -3
  6. package/src/core/text.test.ts +30 -1
  7. package/src/core/text.ts +8 -3
  8. package/src/index.ts +2 -0
  9. package/src/integration/forget-grep.test.ts +98 -0
  10. package/src/integration/fusion-dispatch.test.ts +343 -0
  11. package/src/integration/grep-rg.test.ts +466 -111
  12. package/src/integration/large-text.test.ts +3 -5
  13. package/src/integration/mutation-file-types.test.ts +44 -0
  14. package/src/pi/action-fusion.test.ts +158 -79
  15. package/src/pi/action-fusion.ts +105 -34
  16. package/src/pi/anchor-format.test.ts +46 -0
  17. package/src/pi/anchor-format.ts +21 -2
  18. package/src/pi/argument-validation.test.ts +136 -0
  19. package/src/pi/argument-validation.ts +103 -0
  20. package/src/pi/budgets.ts +4 -1
  21. package/src/pi/config.test.ts +1 -0
  22. package/src/pi/config.ts +4 -0
  23. package/src/pi/diagnostic-buffer.test.ts +58 -0
  24. package/src/pi/diagnostic-buffer.ts +59 -0
  25. package/src/pi/directory-sync.test.ts +1 -1
  26. package/src/pi/edit-tool.ts +56 -37
  27. package/src/pi/execute.test.ts +185 -645
  28. package/src/pi/failure-context.test.ts +4 -3
  29. package/src/pi/failure-path.test.ts +3 -3
  30. package/src/pi/file-commit.test.ts +24 -17
  31. package/src/pi/file-commit.ts +36 -9
  32. package/src/pi/file-snapshot.test.ts +24 -0
  33. package/src/pi/forget-tool.ts +268 -0
  34. package/src/pi/forget.test.ts +343 -0
  35. package/src/pi/forget.testing.ts +273 -0
  36. package/src/pi/fusion-card.test.ts +26 -0
  37. package/src/pi/fusion-card.ts +21 -6
  38. package/src/pi/grep-output.ts +82 -55
  39. package/src/pi/grep-render.ts +8 -11
  40. package/src/pi/grep-scope.ts +6 -31
  41. package/src/pi/grep-search.ts +94 -58
  42. package/src/pi/grep-tool.ts +50 -59
  43. package/src/pi/grep.test.ts +412 -208
  44. package/src/pi/mutation-result.test.ts +23 -0
  45. package/src/pi/mutation-runner.ts +8 -4
  46. package/src/pi/path.ts +16 -2
  47. package/src/pi/pi.test.ts +4 -3
  48. package/src/pi/read-tool.ts +59 -27
  49. package/src/pi/read.test.ts +361 -0
  50. package/src/pi/registration.test.ts +121 -4
  51. package/src/pi/render.test.ts +172 -15
  52. package/src/pi/render.ts +28 -5
  53. package/src/pi/replace-tool.ts +18 -27
  54. package/src/pi/replace.test.ts +8 -107
  55. package/src/pi/result-safety.test.ts +123 -149
  56. package/src/pi/rg-match-bytes.ts +95 -0
  57. package/src/pi/rg-process.test.ts +40 -59
  58. package/src/pi/rg-process.ts +8 -5
  59. package/src/pi/rg-text-view.ts +59 -9
  60. package/src/pi/text-stream.test.ts +153 -0
  61. package/src/pi/text-stream.ts +33 -13
  62. package/src/pi/tool-call.testing.ts +50 -0
  63. package/src/pi/tool-prompts.ts +15 -0
  64. package/src/pi/write-tool.ts +19 -26
  65. package/src/pi/write.test.ts +70 -61
  66. package/src/pi/e2e-scenarios.test.ts +0 -360
@@ -1,360 +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
- context: 3,
74
- });
75
- const grepText = grepResult.content[0].text;
76
- assert.ok(grepText.includes("calculateDiscount"));
77
-
78
- // Extract anchors directly from grep result
79
- const targetAnchor = anchorLine(grepText, 5); // " return price * 0.1;"
80
-
81
- // Turn 2: Direct edit using grep-provided anchor, without calling read!
82
- const editResult = await call(edit, {
83
- path: "calc.ts",
84
- edits: [{ op: "replace", anchor: targetAnchor, body: [" return price * 0.15;"] }],
85
- });
86
- assert.equal(editResult.isError, undefined);
87
-
88
- // Verify target file state
89
- const updated = await readFile(file, "utf8");
90
- assert.ok(updated.includes("return price * 0.15;"));
91
- assert.ok(updated.includes("export function formatCurrency")); // outside range untouched
92
- }));
93
-
94
- test("E2E Dev D2: Chained multi-site edits using returned fresh anchors", async () =>
95
- withDir(async (dir) => {
96
- const file = join(dir, "service.ts");
97
- const original =
98
- [
99
- "class Service {",
100
- " version = 1;",
101
- " enabled = false;",
102
- " status() {",
103
- " return 'idle';",
104
- " }",
105
- "}",
106
- ].join("\n") + "\n";
107
- await writeFile(file, original);
108
-
109
- const edit = makeEditOverride(dir, DEFAULT_CONFIG);
110
-
111
- // Step 1: Edit version
112
- const r1 = await call(edit, {
113
- path: "service.ts",
114
- edits: [{ op: "replace", anchor: h(original, 2), body: [" version = 2;"] }],
115
- });
116
- const returnedAnchor = anchorLine(r1.content[0].text, 2);
117
- assert.equal(returnedAnchor, h("class Service {\n version = 2;\n", 2));
118
-
119
- // Step 2: Reuse the returned anchor and edit another site in the same batch.
120
- const r2 = await call(edit, {
121
- path: "service.ts",
122
- edits: [
123
- { op: "replace", anchor: returnedAnchor, body: [" version = 3;"] },
124
- { op: "replace", anchor: h(original, 5), body: [" return 'active';"] },
125
- ],
126
- });
127
- assert.match(r2.content[0].text, /Updated anchors:/);
128
-
129
- const final = await readFile(file, "utf8");
130
- assert.equal(
131
- final,
132
- [
133
- "class Service {",
134
- " version = 3;",
135
- " enabled = false;",
136
- " status() {",
137
- " return 'active';",
138
- " }",
139
- "}",
140
- ].join("\n") + "\n",
141
- );
142
- }));
143
-
144
- test("E2E Dev D3: Recovery from shifted anchor after external line insertion", async () =>
145
- withDir(async (dir) => {
146
- const file = join(dir, "config.ts");
147
- const original =
148
- [
149
- "// Config file",
150
- "export const TIMEOUT = 1000;",
151
- "export const RETRIES = 3;",
152
- "export const DEBUG = false;",
153
- ].join("\n") + "\n";
154
- await writeFile(file, original);
155
-
156
- // Agent observed anchor at line 3 ("export const RETRIES = 3;")
157
- const staleAnchor = h(original, 3);
158
-
159
- // External process inserts 2 lines at top
160
- const externallyModified =
161
- [
162
- "// Header comment line 1",
163
- "// Header comment line 2",
164
- "// Config file",
165
- "export const TIMEOUT = 1000;",
166
- "export const RETRIES = 3;",
167
- "export const DEBUG = false;",
168
- ].join("\n") + "\n";
169
- await writeFile(file, externallyModified);
170
-
171
- const edit = makeEditOverride(dir, DEFAULT_CONFIG);
172
-
173
- // Agent attempts edit with stale anchor
174
- let candidateAnchor = "";
175
- await assert.rejects(
176
- call(edit, {
177
- path: "config.ts",
178
- edits: [{ op: "replace", anchor: staleAnchor, body: ["export const RETRIES = 5;"] }],
179
- }),
180
- (error: Error) => {
181
- assert.match(error.message, /Anchor mismatch: 1 shifted/);
182
- assert.match(error.message, /candidate 5#/); // Shifted to line 5
183
- const match = /candidate ([0-9]+#[0-9A-Z]+)/.exec(error.message);
184
- if (match) candidateAnchor = match[1];
185
- return true;
186
- },
187
- );
188
-
189
- assert.ok(candidateAnchor.length > 0, "Recovery candidate was found and returned");
190
-
191
- // Agent retries using candidate anchor
192
- const recovered = await call(edit, {
193
- path: "config.ts",
194
- edits: [{ op: "replace", anchor: candidateAnchor, body: ["export const RETRIES = 5;"] }],
195
- });
196
- assert.equal(recovered.isError, undefined);
197
-
198
- const resultText = await readFile(file, "utf8");
199
- assert.ok(resultText.includes("export const RETRIES = 5;"));
200
- assert.ok(resultText.includes("// Header comment line 1"));
201
- }));
202
-
203
- test("E2E Dev D4: Bulk replace with Action Fusion then_run validation", async () =>
204
- withDir(async (dir) => {
205
- const file = join(dir, "api.ts");
206
- await writeFile(file, "const old_var_1 = 10;\nconst old_var_2 = 20;\n");
207
-
208
- let commandRan = false;
209
- const fusionExecutor = createActionFusionExecutor(async (_id, input) => {
210
- commandRan = true;
211
- assert.equal(input.command, "npm run check");
212
- return "All checks passed";
213
- });
214
-
215
- const replace = makeReplaceTool(dir, DEFAULT_CONFIG, fusionExecutor);
216
- const res = await call(replace, {
217
- path: "api.ts",
218
- replacements: [{ find: "old_var", replace: "new_var" }],
219
- then_run: { command: "npm run check" },
220
- });
221
-
222
- assert.equal(commandRan, true);
223
- assert.equal(res.details.actionFusion.command, "succeeded");
224
- assert.equal(res.details.actionFusion.freshness, "unchanged");
225
- assert.match(res.content[1].text, /All checks passed/);
226
-
227
- const content = await readFile(file, "utf8");
228
- assert.equal(content, "const new_var_1 = 10;\nconst new_var_2 = 20;\n");
229
- }));
230
-
231
- test("E2E Dev D5: Line ending preservation and deletion successor anchor", async () =>
232
- withDir(async (dir) => {
233
- const file = join(dir, "crlf.txt");
234
- // CRLF file
235
- const crlfText = "line1\r\nline2\r\nline3\r\nline4\r\n";
236
- await writeFile(file, crlfText);
237
-
238
- const edit = makeEditOverride(dir, DEFAULT_CONFIG);
239
-
240
- // Delete line 2: line 3 shifts to line 2 (deletion successor)
241
- const res = await call(edit, {
242
- path: "crlf.txt",
243
- edits: [{ op: "delete", anchor: h(crlfText, 2) }],
244
- });
245
-
246
- // Deletion successor anchor should be returned with full row
247
- assert.match(res.content[0].text, /2#[0-9A-Z]+│line3/);
248
-
249
- // File bytes must retain CRLF line endings
250
- const afterBytes = await readFile(file);
251
- assert.equal(afterBytes.toString("utf8"), "line1\r\nline3\r\nline4\r\n");
252
- }));
253
-
254
- test("E2E Dev D6: structured edit batches use one original snapshot", async () =>
255
- withDir(async (dir) => {
256
- const file = join(dir, "batch.txt");
257
- const original = "alpha\nbeta\ngamma\n";
258
- await writeFile(file, original);
259
- await call(makeEditOverride(dir, DEFAULT_CONFIG), {
260
- path: "batch.txt",
261
- edits: [
262
- { op: "replace", anchor: h(original, 1), body: ["ALPHA"] },
263
- { op: "replace", anchor: h(original, 3), body: ["GAMMA"] },
264
- ],
265
- });
266
- assert.equal(await readFile(file, "utf8"), "ALPHA\nbeta\nGAMMA\n");
267
- }));
268
-
269
- // ---------------------------------------------------------------------------
270
- // Holdout Set (H1 - H3)
271
- // ---------------------------------------------------------------------------
272
-
273
- test("E2E Holdout H1: Ambiguous candidates provide distinguishing neighborhoods", async () =>
274
- withDir(async (dir) => {
275
- const file = join(dir, "ambiguous.txt");
276
- const text =
277
- ["function one() {", " return true;", "}", "function two() {", " return true;", "}"].join(
278
- "\n",
279
- ) + "\n";
280
- await writeFile(file, text);
281
-
282
- const edit = makeEditOverride(dir, DEFAULT_CONFIG);
283
- // The cited content moved away from line 3 and now occurs at lines 2 and 5.
284
- const staleAnchor = h("header\nheader\n return true;\n", 3);
285
- let candidateAnchor = "";
286
- await assert.rejects(
287
- call(edit, {
288
- path: "ambiguous.txt",
289
- edits: [{ op: "replace", anchor: staleAnchor, body: [" return false;"] }],
290
- }),
291
- (error: Error) => {
292
- assert.match(error.message, /ambiguous checksum matches/);
293
- assert.match(error.message, /Ambiguous-candidate neighborhoods/);
294
- assert.ok(error.message.includes(`${h(text, 2)}│ return true;`));
295
- assert.ok(error.message.includes(`${h(text, 5)}│ return true;`));
296
- assert.ok(error.message.includes(`${h(text, 1)}│function one() {`));
297
- assert.ok(error.message.includes(`${h(text, 4)}│function two() {`));
298
- candidateAnchor = anchorLine(
299
- error.message.split("Ambiguous-candidate neighborhoods")[1],
300
- 5,
301
- );
302
- return true;
303
- },
304
- );
305
- assert.equal(await readFile(file, "utf8"), text);
306
- await call(edit, {
307
- path: "ambiguous.txt",
308
- edits: [{ op: "replace", anchor: candidateAnchor, body: [" return false;"] }],
309
- });
310
- assert.equal(
311
- await readFile(file, "utf8"),
312
- "function one() {\n return true;\n}\nfunction two() {\n return false;\n}\n",
313
- );
314
- }));
315
-
316
- test("E2E Holdout H2: Action Fusion command failure preserves file changes and anchors", async () =>
317
- withDir(async (dir) => {
318
- const file = join(dir, "fusion_fail.ts");
319
- await writeFile(file, "let x = 1;\n");
320
-
321
- const fusionExecutor = createActionFusionExecutor(async () => {
322
- throw new Error("Syntax error on line 42");
323
- });
324
-
325
- const edit = makeEditOverride(dir, DEFAULT_CONFIG, fusionExecutor);
326
- const res = await call(edit, {
327
- path: "fusion_fail.ts",
328
- edits: [{ op: "replace", anchor: h("let x = 1;\n", 1), body: ["let x = 2;"] }],
329
- then_run: { command: "test" },
330
- });
331
-
332
- // File change must be published and saved
333
- assert.equal(await readFile(file, "utf8"), "let x = 2;\n");
334
- assert.equal(res.details.actionFusion.publication, "PUBLISHED");
335
- assert.equal(res.details.actionFusion.command, "failed");
336
- assert.match(res.content[0].text, /Edited fusion_fail\.ts/);
337
- assert.match(res.content[0].text, /Updated anchors/);
338
- assert.match(res.content[1].text, /Syntax error on line 42/);
339
- }));
340
-
341
- test("E2E Holdout H3: UTF-8 BOM file retains BOM at byte 0 after first-line edit", async () =>
342
- withDir(async (dir) => {
343
- const file = join(dir, "bom.txt");
344
- const bomContent = "\uFEFFline1\nline2\n";
345
- await writeFile(file, bomContent);
346
-
347
- const edit = makeEditOverride(dir, DEFAULT_CONFIG);
348
- const a1 = h(bomContent, 1);
349
-
350
- await call(edit, {
351
- path: "bom.txt",
352
- edits: [{ op: "replace", anchor: a1, body: ["replaced1"] }],
353
- });
354
-
355
- const afterBytes = await readFile(file);
356
- assert.equal(afterBytes[0], 0xef);
357
- assert.equal(afterBytes[1], 0xbb);
358
- assert.equal(afterBytes[2], 0xbf);
359
- assert.equal(afterBytes.toString("utf8"), "\uFEFFreplaced1\nline2\n");
360
- }));