@criogaid/pi-hashline-edit 0.7.0 → 0.9.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 (118) hide show
  1. package/README.md +76 -220
  2. package/README.zh-CN.md +166 -0
  3. package/package.json +2 -1
  4. package/src/core/AGENTS.md +1 -1
  5. package/src/core/apply.test.ts +171 -133
  6. package/src/core/apply.ts +34 -31
  7. package/src/core/case-fold.test.ts +11 -7
  8. package/src/core/case-fold.ts +2 -6
  9. package/src/core/character-set.ts +14 -0
  10. package/src/core/hash.test.ts +1 -25
  11. package/src/core/hash.ts +0 -5
  12. package/src/core/lines.ts +25 -7
  13. package/src/core/ranges.test.ts +49 -9
  14. package/src/core/ranges.ts +15 -4
  15. package/src/core/regex.ts +36 -6
  16. package/src/core/replace-schema.ts +18 -11
  17. package/src/core/replace.test.ts +90 -0
  18. package/src/core/replace.ts +6 -5
  19. package/src/core/search-analysis.test.ts +9 -0
  20. package/src/core/search-analysis.ts +35 -31
  21. package/src/core/search.ts +1 -1
  22. package/src/core/types.ts +6 -8
  23. package/src/index.ts +2 -4
  24. package/src/integration/config-registration.test.ts +18 -10
  25. package/src/integration/fusion-dispatch.test.ts +41 -22
  26. package/src/integration/grep-flags.test.ts +6 -13
  27. package/src/integration/grep-js-completeness.test.ts +41 -31
  28. package/src/integration/grep-order.test.ts +78 -0
  29. package/src/integration/grep-path-matrix.test.ts +4 -3
  30. package/src/integration/grep-regex.test.ts +13 -16
  31. package/src/integration/grep-rg.test.ts +132 -92
  32. package/src/integration/grep-scan-budget.test.ts +27 -5
  33. package/src/integration/large-text.test.ts +4 -5
  34. package/src/integration/model-budget-lifecycle.test.ts +26 -8
  35. package/src/integration/mutation-file-types.test.ts +0 -1
  36. package/src/integration/mutation-file-types.testing.ts +2 -2
  37. package/src/integration/npm-package.testing.ts +7 -33
  38. package/src/integration/publication-read-changes.test.ts +60 -28
  39. package/src/integration/replace-package.testing.ts +4 -1
  40. package/src/integration/replace-worker-boundary.test.ts +34 -24
  41. package/src/integration/report-replay.test.ts +3 -3
  42. package/src/integration/utf8-errors.test.ts +3 -3
  43. package/src/pi/AGENTS.md +13 -14
  44. package/src/pi/action-fusion.test.ts +75 -220
  45. package/src/pi/action-fusion.ts +37 -74
  46. package/src/pi/anchor-format.test.ts +16 -0
  47. package/src/pi/anchor-format.ts +7 -3
  48. package/src/pi/argument-diagnostics.ts +35 -186
  49. package/src/pi/argument-error.testing.ts +7 -4
  50. package/src/pi/argument-error.ts +5 -7
  51. package/src/pi/argument-format.test.ts +25 -38
  52. package/src/pi/argument-validation.test.ts +446 -420
  53. package/src/pi/argument-validation.ts +4 -5
  54. package/src/pi/budgets.ts +5 -7
  55. package/src/pi/config.test.ts +15 -5
  56. package/src/pi/config.ts +6 -6
  57. package/src/pi/diagnostic-buffer.test.ts +2 -3
  58. package/src/pi/diagnostic-buffer.ts +2 -2
  59. package/src/pi/edit-tool.ts +175 -121
  60. package/src/pi/execute.test.ts +170 -227
  61. package/src/pi/failure-context.test.ts +61 -16
  62. package/src/pi/failure-context.ts +13 -10
  63. package/src/pi/failure-path.test.ts +115 -50
  64. package/src/pi/file-commit.test.ts +30 -46
  65. package/src/pi/file-commit.ts +11 -40
  66. package/src/pi/file-revision.ts +25 -0
  67. package/src/pi/fusion-card-interaction.test.ts +143 -0
  68. package/src/pi/fusion-card.test.ts +193 -37
  69. package/src/pi/fusion-card.ts +186 -116
  70. package/src/pi/grep-candidates.ts +25 -27
  71. package/src/pi/grep-matrix.test.ts +1 -1
  72. package/src/pi/grep-order.test.ts +323 -0
  73. package/src/pi/grep-output.testing.ts +24 -0
  74. package/src/pi/grep-output.ts +62 -69
  75. package/src/pi/grep-render.ts +4 -1
  76. package/src/pi/grep-scope.ts +1 -1
  77. package/src/pi/grep-search.ts +85 -56
  78. package/src/pi/grep-tool.ts +17 -48
  79. package/src/pi/grep.test.ts +271 -170
  80. package/src/pi/mutation-outcome.testing.ts +2 -3
  81. package/src/pi/mutation-result.test.ts +26 -19
  82. package/src/pi/mutation-result.ts +25 -40
  83. package/src/pi/mutation-runner.ts +9 -17
  84. package/src/pi/output-budgets.test.ts +27 -18
  85. package/src/pi/path.ts +3 -1
  86. package/src/pi/pi.test.ts +7 -6
  87. package/src/pi/read-tool.ts +32 -80
  88. package/src/pi/read.test.ts +145 -92
  89. package/src/pi/regex-worker-contract.ts +5 -23
  90. package/src/pi/regex-worker.ts +4 -10
  91. package/src/pi/registration.test.ts +106 -67
  92. package/src/pi/render.test.ts +239 -109
  93. package/src/pi/render.ts +42 -22
  94. package/src/pi/replace-regex.ts +3 -5
  95. package/src/pi/replace-tool.ts +29 -27
  96. package/src/pi/replace-worker-contract.ts +3 -12
  97. package/src/pi/replace.test.ts +144 -226
  98. package/src/pi/report-cause.ts +23 -19
  99. package/src/pi/report-failure.testing.ts +2 -2
  100. package/src/pi/report-schema.ts +60 -45
  101. package/src/pi/report.test.ts +138 -71
  102. package/src/pi/report.ts +40 -43
  103. package/src/pi/result-safety.test.ts +186 -131
  104. package/src/pi/rg-process.test.ts +36 -10
  105. package/src/pi/rg-process.ts +9 -3
  106. package/src/pi/rg-regex.test.ts +17 -0
  107. package/src/pi/rg-regex.ts +28 -22
  108. package/src/pi/text-stream.test.ts +35 -36
  109. package/src/pi/text-stream.ts +14 -14
  110. package/src/pi/tool-call.testing.ts +17 -7
  111. package/src/pi/write-tool.ts +9 -11
  112. package/src/pi/write.test.ts +43 -75
  113. package/src/testing/regex-translation-corpus.ts +16 -0
  114. package/src/testing/rg-line-listing.fixture.mjs +1 -1
  115. package/src/integration/forget-grep.test.ts +0 -148
  116. package/src/pi/forget-tool.ts +0 -367
  117. package/src/pi/forget.test.ts +0 -630
  118. package/src/pi/forget.testing.ts +0 -201
@@ -5,6 +5,8 @@ import { rejectsArgument } from "../pi/argument-error.testing.ts";
5
5
  */
6
6
  import { test } from "node:test";
7
7
  import assert from "node:assert/strict";
8
+ import childProcess from "node:child_process";
9
+ import { syncBuiltinESMExports } from "node:module";
8
10
  import {
9
11
  lstat,
10
12
  mkdir,
@@ -18,7 +20,7 @@ import {
18
20
  } from "node:fs/promises";
19
21
  import { tmpdir } from "node:os";
20
22
  import { dirname, join } from "node:path";
21
- import { pathToFileURL } from "node:url";
23
+ import { fileURLToPath, pathToFileURL } from "node:url";
22
24
  import { rgPath } from "@vscode/ripgrep";
23
25
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
24
26
  import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
@@ -33,7 +35,7 @@ import { callTool } from "../pi/tool-call.testing.ts";
33
35
  import { DEFAULT_CONFIG } from "../pi/config.ts";
34
36
  import { canonicalPath } from "../pi/path.ts";
35
37
  import { reportOf } from "../pi/report.ts";
36
- import { expectToolFailure } from "../pi/report-failure.testing.ts";
38
+ import { grepCounts } from "../pi/grep-output.testing.ts";
37
39
  import { hostCompiles, unsupportedByHost } from "../testing/regex-host.testing.ts";
38
40
 
39
41
  const invoke = (
@@ -80,8 +82,8 @@ test("real rg finds whitespace-only literal queries and OR alternatives", async
80
82
  undefined,
81
83
  undefined,
82
84
  );
83
- assert.equal(reportOf(result)?.matches, expected);
84
- assert.equal(reportOf(result)?.files, 1);
85
+ assert.equal(grepCounts(result).matches, expected);
86
+ assert.equal(grepCounts(result).files, 1);
85
87
  }
86
88
  const lineBreaks = await invoke(
87
89
  tool,
@@ -90,8 +92,8 @@ test("real rg finds whitespace-only literal queries and OR alternatives", async
90
92
  undefined,
91
93
  undefined,
92
94
  );
93
- assert.equal(reportOf(lineBreaks)?.matches, 3);
94
- assert.equal(reportOf(lineBreaks)?.files, 1);
95
+ assert.equal(grepCounts(lineBreaks).matches, 3);
96
+ assert.equal(grepCounts(lineBreaks).files, 1);
95
97
  } finally {
96
98
  await rm(directory, { recursive: true, force: true });
97
99
  }
@@ -207,7 +209,7 @@ test("owned rg processes ignore RIPGREP_CONFIG_PATH", async () => {
207
209
  assert.doesNotMatch(result.content[0].text, /│bar/);
208
210
  const lineOnly = await invoke(tool, "0", { pattern: "foo\\nbar", literal: false });
209
211
  assert.equal(lineOnly.isError, undefined);
210
- assert.equal(reportOf(lineOnly)?.matches, 0);
212
+ assert.equal(grepCounts(lineOnly).matches, 0);
211
213
  const multiline = await invoke(
212
214
  tool,
213
215
  "0",
@@ -223,7 +225,7 @@ test("owned rg processes ignore RIPGREP_CONFIG_PATH", async () => {
223
225
  }
224
226
  });
225
227
 
226
- test("real rg requires an explicit mode and separates exact text from regex", async () => {
228
+ test("real rg separates exact text from regex", async () => {
227
229
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-regex-"));
228
230
  try {
229
231
  await writeFile(
@@ -231,10 +233,6 @@ test("real rg requires an explicit mode and separates exact text from regex", as
231
233
  "FOO\nfoo\nfoo0\nfoo(0)\nqueueTool(\nfoo(?=bar)\n",
232
234
  );
233
235
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
234
- await assert.rejects(
235
- invoke(tool, "missing-mode", { pattern: "foo" }),
236
- rejectsArgument("literal", /required/),
237
- );
238
236
  for (const pattern of ["(?i:^foo$)", "(?<name>foo)$"].filter((source) =>
239
237
  hostCompiles(source),
240
238
  )) {
@@ -249,7 +247,7 @@ test("real rg requires an explicit mode and separates exact text from regex", as
249
247
  assert.ok(result.content[0].text.includes(`│${pattern}`));
250
248
  }
251
249
  const missing = await invoke(tool, "no-match", { pattern: "missing(", literal: true });
252
- assert.equal(reportOf(missing)?.matches, 0);
250
+ assert.equal(grepCounts(missing).matches, 0);
253
251
  assert.equal(missing.isError, undefined);
254
252
  await assert.rejects(
255
253
  invoke(tool, "invalid-regex", { pattern: "queueTool(", literal: false }),
@@ -295,7 +293,10 @@ test("real rg keeps CRLF line-end anchors with regex OR matching", async () => {
295
293
  test("real rg finds a late OR match and respects the line limit", async () => {
296
294
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-limit-"));
297
295
  try {
298
- await writeFile(join(directory, "fixture.ts"), "foo skip\n".repeat(4096) + "foo keep\n");
296
+ await writeFile(
297
+ join(directory, "fixture.ts"),
298
+ "foo skip\n".repeat(4096) + "foo keep\nfoo keep later\n",
299
+ );
299
300
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
300
301
  const result = await invoke(
301
302
  tool,
@@ -304,40 +305,71 @@ test("real rg finds a late OR match and respects the line limit", async () => {
304
305
  undefined,
305
306
  undefined,
306
307
  );
307
- assert.match(result.content[0].text, /4097#[0-9A-Z]+│foo keep/);
308
- assert.equal(reportOf(result)?.matchLimit, 1);
308
+ assert.match(result.content[0].text, /4097#[0-9A-Z]+│foo keep$/m);
309
+ assert.doesNotMatch(result.content[0].text, /foo keep later|4098#/);
310
+ assert.equal(grepCounts(result).matches, 1);
311
+ assert.equal(reportOf(result, "grep")?.matchLimit, 1);
309
312
  } finally {
310
313
  await rm(directory, { recursive: true, force: true });
311
314
  }
312
315
  });
313
316
 
314
- test("shared rg runner stops and cleans up after limits, callback failures, and cancellation", async () => {
317
+ test("shared rg runner stops and cleans up after limits, callback failures, and cancellation", async (t) => {
315
318
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-process-"));
319
+ const spawn = childProcess.spawn;
320
+ const children: ReturnType<typeof spawn>[] = [];
321
+ const closed = new Set<ReturnType<typeof spawn>>();
316
322
  try {
317
- const file = join(directory, "fixture.txt");
318
- await writeFile(file, "needle\n".repeat(10_000));
319
- const args = [...COMMON_RG_ARGS, "--files", "--null", "--", file];
320
- const stopped = await runRgPaths(rgPath, args, undefined, () => false);
321
- assert.equal(stopped.stopped, true);
322
- await assert.rejects(
323
- runRgPaths(rgPath, args, undefined, () => {
324
- throw new Error("callback failed");
325
- }),
326
- /callback failed/,
327
- );
328
- const controller = new AbortController();
329
- await assert.rejects(
330
- runRgPaths(rgPath, args, controller.signal, () => {
331
- controller.abort();
332
- return true;
333
- }),
334
- /Operation aborted/,
335
- );
323
+ t.mock.method(childProcess, "spawn", (...args: Parameters<typeof spawn>) => {
324
+ const child = spawn(...args);
325
+ children.push(child);
326
+ child.once("close", () => closed.add(child));
327
+ return child;
328
+ });
329
+ syncBuiltinESMExports();
330
+ const fixture = fileURLToPath(
331
+ new URL("../testing/rg-line-listing.fixture.mjs", import.meta.url),
332
+ );
333
+ const processTimeoutMs = 5_000;
334
+ const args = [
335
+ fixture,
336
+ JSON.stringify([Buffer.from("first\0second\0").toString("base64")]),
337
+ "wait",
338
+ ];
339
+ for (const outcome of ["limit", "callback", "cancel"] as const) {
340
+ const controller = new AbortController();
341
+ const consumed: string[] = [];
342
+ let aliveAtCallback = false;
343
+ const run = runRgPaths(
344
+ process.execPath,
345
+ args,
346
+ AbortSignal.any([controller.signal, AbortSignal.timeout(processTimeoutMs)]),
347
+ (path) => {
348
+ consumed.push(path);
349
+ const child = children.at(-1);
350
+ aliveAtCallback = child?.exitCode === null && child.signalCode === null;
351
+ if (outcome === "callback") throw new Error("callback failed");
352
+ if (outcome === "cancel") controller.abort();
353
+ return outcome !== "limit";
354
+ },
355
+ );
356
+ if (outcome === "limit") assert.equal((await run).stopped, true);
357
+ else
358
+ await assert.rejects(run, outcome === "callback" ? /callback failed/ : /Operation aborted/);
359
+ assert.deepEqual(consumed, ["first"]);
360
+ assert.equal(aliveAtCallback, true);
361
+ const child = children.at(-1);
362
+ assert.ok(child && closed.has(child), "runner must await child close");
363
+ assert.ok(child.exitCode !== null || child.signalCode !== null);
364
+ }
336
365
  await assert.rejects(
337
366
  runRgPaths(join(directory, "missing-rg"), [], undefined, () => true),
338
367
  /Failed to run ripgrep/,
339
368
  );
340
369
  } finally {
370
+ for (const child of children) if (!closed.has(child)) child.kill("SIGKILL");
371
+ t.mock.restoreAll();
372
+ syncBuiltinESMExports();
341
373
  await rm(directory, { recursive: true, force: true });
342
374
  }
343
375
  });
@@ -356,7 +388,7 @@ test("directory searches respect ignores but explicit files and hidden files rem
356
388
  undefined,
357
389
  undefined,
358
390
  );
359
- assert.equal(reportOf(normal)?.matches, 0);
391
+ assert.equal(grepCounts(normal).matches, 0);
360
392
  const explicit = await invoke(
361
393
  tool,
362
394
  "0",
@@ -372,7 +404,7 @@ test("directory searches respect ignores but explicit files and hidden files rem
372
404
  undefined,
373
405
  undefined,
374
406
  );
375
- assert.equal(reportOf(excluded)?.matches, 0);
407
+ assert.equal(grepCounts(excluded).matches, 0);
376
408
  await writeFile(join(directory, ".hidden.txt"), "needle\n");
377
409
  const hidden = await invoke(
378
410
  tool,
@@ -410,8 +442,9 @@ test("directory traversal with caller globs → excludes .git while an explicit
410
442
  pattern: "needle",
411
443
  outputMode: "files",
412
444
  });
413
- assert.equal(result.isError, undefined, JSON.stringify(reportOf(result)));
414
- assert.equal(reportOf(result)?.matches, 1);
445
+ assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
446
+ assert.equal(grepCounts(result).matches, 1);
447
+ assert.equal(result.content[0].type, "text");
415
448
  assert.equal(result.content[0].text.replace(/\\/g, "/"), expected);
416
449
  }
417
450
  }
@@ -442,12 +475,12 @@ test("explicit file with different case and a glob → admits the same file on a
442
475
  literal: true,
443
476
  pattern: "needle",
444
477
  });
445
- assert.equal(result.isError, undefined, JSON.stringify(reportOf(result)));
446
- assert.equal(reportOf(result)?.matches, 1);
447
- assert.equal(reportOf(result)?.files, 1);
478
+ assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
479
+ assert.equal(grepCounts(result).matches, 1);
480
+ assert.equal(grepCounts(result).files, 1);
481
+ assert.equal(result.content[0].type, "text");
448
482
  assert.match(result.content[0].text, /1#[0-9A-Z]+│needle/);
449
- assert.equal(reportOf(result)?.diagnostics, undefined);
450
- assert.equal(result.details.incomplete, undefined);
483
+ assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
451
484
  });
452
485
 
453
486
  test("explicit file with a dangling sibling link and a glob → returns complete matches without listing diagnostics", async (t) => {
@@ -488,10 +521,9 @@ test("explicit file with a dangling sibling link and a glob → returns complete
488
521
  pattern: "needle",
489
522
  });
490
523
  assert.match(listingDiagnostics, /broken\.txt/);
491
- assert.equal(result.isError, undefined, JSON.stringify(reportOf(result)));
492
- assert.equal(reportOf(result)?.matches, 1);
493
- assert.equal(reportOf(result)?.diagnostics, undefined);
494
- assert.equal(result.details.incomplete, undefined);
524
+ assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
525
+ assert.equal(grepCounts(result).matches, 1);
526
+ assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
495
527
  });
496
528
 
497
529
  test("explicit file paths obey ordered glob filters without inheriting ignore rules", async () => {
@@ -526,7 +558,7 @@ test("explicit file paths obey ordered glob filters without inheriting ignore ru
526
558
  undefined,
527
559
  undefined,
528
560
  );
529
- assert.equal(reportOf(none)?.matches, 0);
561
+ assert.equal(grepCounts(none).matches, 0);
530
562
  } finally {
531
563
  await rm(directory, { recursive: true, force: true });
532
564
  }
@@ -559,7 +591,7 @@ test("directory traversal skips symlinks but explicitly named linked files remai
559
591
  undefined,
560
592
  undefined,
561
593
  );
562
- assert.equal(reportOf(hidden)?.matches, 0);
594
+ assert.equal(grepCounts(hidden).matches, 0);
563
595
  const found: any = await invoke(
564
596
  grep,
565
597
  "0",
@@ -576,7 +608,7 @@ test("directory traversal skips symlinks but explicitly named linked files remai
576
608
  await invoke(
577
609
  edit,
578
610
  "0",
579
- { path: resultPath, edits: [{ op: "replace", anchor, body: ["updated"] }] },
611
+ { path: resultPath, edits: [{ action: "replace", anchor, body: ["updated"] }] },
580
612
  undefined,
581
613
  undefined,
582
614
  );
@@ -595,8 +627,8 @@ test("unbounded lookbehind → native JS matches after rg discovery", async (t)
595
627
  await writeFile(join(directory, "fixture.txt"), "aaab\n");
596
628
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
597
629
  const result = await invoke(tool, "0", { pattern: "(?<=a+)b", literal: false });
598
- assert.equal(result.isError, undefined, JSON.stringify(reportOf(result)));
599
- assert.equal(reportOf(result)?.matches, 1);
630
+ assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
631
+ assert.equal(grepCounts(result).matches, 1);
600
632
  });
601
633
 
602
634
  test("line-based grep uses context for display but never matches across CRLF", async () => {
@@ -621,7 +653,7 @@ test("line-based grep uses context for display but never matches across CRLF", a
621
653
  ] as const) {
622
654
  const result = await invoke(tool, "0", { pattern, literal });
623
655
  assert.equal(result.isError, undefined);
624
- assert.equal(reportOf(result)?.matches, 0);
656
+ assert.equal(grepCounts(result).matches, 0);
625
657
  }
626
658
  const limited = await invoke(
627
659
  tool,
@@ -631,7 +663,7 @@ test("line-based grep uses context for display but never matches across CRLF", a
631
663
  undefined,
632
664
  );
633
665
  assert.match(limited.content[0].text, /fixture\.txt: 1/);
634
- assert.equal(reportOf(limited)?.matchLimit, 1);
666
+ assert.equal(reportOf(limited, "grep")?.matchLimit, 1);
635
667
  } finally {
636
668
  await rm(directory, { recursive: true, force: true });
637
669
  }
@@ -684,9 +716,9 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
684
716
  undefined,
685
717
  );
686
718
  assert.match(bounded.content[0].text, /fixture\.txt: 2/);
687
- assert.equal(reportOf(bounded)?.matches, 2);
688
- assert.equal(reportOf(bounded)?.files, 1);
689
- assert.equal(reportOf(bounded)?.matchLimit, 2);
719
+ assert.equal(grepCounts(bounded).matches, 2);
720
+ assert.equal(grepCounts(bounded).files, 1);
721
+ assert.equal(reportOf(bounded, "grep")?.matchLimit, 2);
690
722
  const files = await invoke(
691
723
  tool,
692
724
  "0",
@@ -702,7 +734,7 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
702
734
  undefined,
703
735
  );
704
736
  assert.equal(files.content[0].text, "fixture.txt");
705
- assert.equal(reportOf(files)?.matchLimit, 1);
737
+ assert.equal(reportOf(files, "grep")?.matchLimit, 1);
706
738
  const boundary = await invoke(
707
739
  tool,
708
740
  "0",
@@ -717,7 +749,7 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
717
749
  await invoke(
718
750
  edit,
719
751
  "0",
720
- { path: file, edits: [{ op: "replace", anchor, body: ["updated"] }] },
752
+ { path: file, edits: [{ action: "replace", anchor, body: ["updated"] }] },
721
753
  undefined,
722
754
  undefined,
723
755
  );
@@ -803,7 +835,7 @@ test("multiline zero-width EOF stays on the last existing physical line", {
803
835
  undefined,
804
836
  );
805
837
  if (content.endsWith("\n")) {
806
- assert.equal(reportOf(result)?.matches, 0);
838
+ assert.equal(grepCounts(result).matches, 0);
807
839
  } else {
808
840
  assert.match(result.content[0].text, /fixture\.txt · 1 match/);
809
841
  assert.match(result.content[0].text, /1#[0-9A-Z]+│last line/);
@@ -831,7 +863,7 @@ test("files and count modes aggregate only matching lines inside the limit", asy
831
863
  undefined,
832
864
  );
833
865
  assert.match(files.content[0].text, /^(?:a|b)\.txt$/);
834
- assert.equal(reportOf(files)?.matchLimit, 1);
866
+ assert.equal(reportOf(files, "grep")?.matchLimit, 1);
835
867
  const limited = await invoke(
836
868
  tool,
837
869
  "count",
@@ -839,8 +871,8 @@ test("files and count modes aggregate only matching lines inside the limit", asy
839
871
  undefined,
840
872
  undefined,
841
873
  );
842
- assert.equal(reportOf(limited)?.matches, 1);
843
- assert.equal(reportOf(limited)?.files, 1);
874
+ assert.equal(grepCounts(limited).matches, 1);
875
+ assert.equal(grepCounts(limited).files, 1);
844
876
  const complete = await invoke(
845
877
  tool,
846
878
  "count-all",
@@ -848,8 +880,8 @@ test("files and count modes aggregate only matching lines inside the limit", asy
848
880
  undefined,
849
881
  undefined,
850
882
  );
851
- assert.equal(reportOf(complete)?.matches, 3);
852
- assert.equal(reportOf(complete)?.files, 2);
883
+ assert.equal(grepCounts(complete).matches, 3);
884
+ assert.equal(grepCounts(complete).files, 2);
853
885
  const contextual = await invoke(
854
886
  tool,
855
887
  "content",
@@ -881,8 +913,8 @@ test("real rg accepts wildcard-only regexes and preserves limits and literal mod
881
913
  undefined,
882
914
  undefined,
883
915
  );
884
- assert.equal(reportOf(result)?.matches, expected);
885
- assert.equal(reportOf(result)?.files, 1);
916
+ assert.equal(grepCounts(result).matches, expected);
917
+ assert.equal(grepCounts(result).files, 1);
886
918
  }
887
919
  const limited: any = await invoke(
888
920
  tool,
@@ -942,20 +974,17 @@ test("tools share physical lines and anchors across text representations", async
942
974
  const anchor = rows(grep)[1].split("│")[0];
943
975
  const edit = await call(makeEditOverride(directory, DEFAULT_CONFIG), {
944
976
  path: file,
945
- edits: [{ op: "replace", anchor, body: ["new"] }],
977
+ edits: [{ action: "replace", anchor, body: ["new"] }],
946
978
  });
947
979
  assert.equal(await readFile(file, "utf8"), before.replace("old", "new"));
948
- assert.equal(edit.details.firstChangedLine, 2);
949
980
  assert.match(edit.details.diff, /^-2 old/m);
950
981
  assert.match(edit.details.diff, /^\+2 new/m);
951
982
  assert.ok(!edit.details.diff.includes("\r"));
952
- if (before.includes("a\rb")) assert.ok(edit.details.patch.includes(" a\rb\n"));
953
- const replaced = await call(makeReplaceTool(directory, DEFAULT_CONFIG), {
983
+ await call(makeReplaceTool(directory, DEFAULT_CONFIG), {
954
984
  path: file,
955
- replacements: [{ find: "new", replace: "next" }],
985
+ replacements: [{ literal: true, find: "new", replace: "next" }],
956
986
  });
957
987
  assert.equal(await readFile(file, "utf8"), before.replace("old", "next"));
958
- assert.equal(replaced.details.firstChangedLine, 2);
959
988
  }
960
989
  } finally {
961
990
  await rm(directory, { recursive: true, force: true });
@@ -991,7 +1020,10 @@ test("long-line previews preserve full-line anchors across literal and JavaScrip
991
1020
  `3#${computeLineHash(3, context, 4)}│[partial, columns 1-${DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits - 1}]`,
992
1021
  ),
993
1022
  );
994
- assert.equal(reportOf(result)?.linePreviewLimit, DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits);
1023
+ assert.equal(
1024
+ reportOf(result, "grep")?.linePreviewLimit,
1025
+ DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits,
1026
+ );
995
1027
  }
996
1028
  } finally {
997
1029
  await rm(directory, { recursive: true, force: true });
@@ -1073,7 +1105,7 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1073
1105
  path,
1074
1106
  edits: [
1075
1107
  {
1076
- op: "replace",
1108
+ action: "replace",
1077
1109
  anchor: rows[1].split("│")[0],
1078
1110
  end: rows[2].split("│")[0],
1079
1111
  body: ["A", "B", "C"],
@@ -1094,7 +1126,7 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1094
1126
  {
1095
1127
  find: mode === "regex" ? "alpha\\nbeta" : "alpha\r\nbeta",
1096
1128
  replace: "A\nB\nC",
1097
- regex: mode === "regex",
1129
+ literal: mode !== "regex",
1098
1130
  },
1099
1131
  ],
1100
1132
  },
@@ -1159,7 +1191,7 @@ test("visible source escapes remain readable and searchable while real CRLF stay
1159
1191
  await invoke(
1160
1192
  replace,
1161
1193
  "literal",
1162
- { path, replacements: [{ find: String.raw`\r\n`, replace: String.raw`\n` }] },
1194
+ { path, replacements: [{ literal: true, find: String.raw`\r\n`, replace: String.raw`\n` }] },
1163
1195
  undefined,
1164
1196
  undefined,
1165
1197
  { cwd: directory } as ExtensionContext,
@@ -1168,7 +1200,10 @@ test("visible source escapes remain readable and searchable while real CRLF stay
1168
1200
  await invoke(
1169
1201
  replace,
1170
1202
  "regex",
1171
- { path, replacements: [{ find: String.raw`\\n`, replace: String.raw`\r\n`, regex: true }] },
1203
+ {
1204
+ path,
1205
+ replacements: [{ find: String.raw`\\n`, replace: String.raw`\r\n`, literal: false }],
1206
+ },
1172
1207
  undefined,
1173
1208
  undefined,
1174
1209
  { cwd: directory } as ExtensionContext,
@@ -1197,7 +1232,8 @@ test("grep retains original paths and ignores unmatched invalid UTF-8 across man
1197
1232
  assert.equal((result.content[0].text.match(/file \d+\.txt: 1/g) ?? []).length, 70);
1198
1233
  assert.doesNotMatch(result.content[0].text, /invalid\.txt|UNSUPPORTED_ENCODING/);
1199
1234
  assert.doesNotMatch(result.content[0].text, /hashline-grep-/);
1200
- assert.equal(result.details?.incomplete, undefined);
1235
+ assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
1236
+ assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
1201
1237
  } finally {
1202
1238
  await rm(directory, { recursive: true, force: true });
1203
1239
  }
@@ -1224,8 +1260,8 @@ test("NUL files are silently skipped across search scopes and output modes", asy
1224
1260
  undefined,
1225
1261
  undefined,
1226
1262
  );
1227
- assert.equal(reportOf(skipped)?.matches, 0);
1228
- assert.equal(reportOf(skipped)?.diagnostics, undefined);
1263
+ assert.equal(grepCounts(skipped).matches, 0);
1264
+ assert.equal(reportOf(skipped, "grep")?.diagnostics, undefined);
1229
1265
  const result = await invoke(
1230
1266
  grep,
1231
1267
  "directory",
@@ -1235,7 +1271,8 @@ test("NUL files are silently skipped across search scopes and output modes", asy
1235
1271
  );
1236
1272
  assert.match(result.content[0].text, /valid\.txt/);
1237
1273
  assert.doesNotMatch(result.content[0].text, /binary\.txt|UNSUPPORTED_TEXT/);
1238
- assert.equal(result.details?.incomplete, undefined);
1274
+ assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
1275
+ assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
1239
1276
  }
1240
1277
  }
1241
1278
  assert.deepEqual(await readFile(binary), source);
@@ -1264,9 +1301,10 @@ test("invalid UTF-8 matches use plain previews while valid files retain edit anc
1264
1301
  undefined,
1265
1302
  );
1266
1303
  assert.match(result.content[0].text, /1│before\n2│needle �\n3│after/);
1267
- assert.deepEqual(reportOf(result)?.invalidUtf8, ["invalid.txt"]);
1304
+ assert.deepEqual(reportOf(result, "grep")?.invalidUtf8, ["invalid.txt"]);
1268
1305
  assert.doesNotMatch(result.content[0].text, /\d+#/);
1269
- assert.equal(result.details?.incomplete, undefined);
1306
+ assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
1307
+ assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
1270
1308
  assert.deepEqual(await readFile(invalid), source);
1271
1309
  }
1272
1310
  await writeFile(join(directory, "valid.txt"), "needle\n");
@@ -1289,7 +1327,8 @@ test("invalid UTF-8 matches use plain previews while valid files retain edit anc
1289
1327
  );
1290
1328
  assert.match(result.content[0].text, /invalid\.txt/);
1291
1329
  assert.match(result.content[0].text, /valid\.txt/);
1292
- assert.equal(result.details?.incomplete, undefined);
1330
+ assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
1331
+ assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
1293
1332
  }
1294
1333
  const multiline = await invoke(
1295
1334
  grep,
@@ -1299,7 +1338,7 @@ test("invalid UTF-8 matches use plain previews while valid files retain edit anc
1299
1338
  undefined,
1300
1339
  );
1301
1340
  // The explicit class excludes the replacement character in the lossy view.
1302
- assert.equal(reportOf(multiline)?.matches, 0);
1341
+ assert.equal(grepCounts(multiline).matches, 0);
1303
1342
  } finally {
1304
1343
  await rm(directory, { recursive: true, force: true });
1305
1344
  }
@@ -1347,7 +1386,7 @@ test("grep paths with input shorthand or literal separators still edit the searc
1347
1386
  assert.ok(anchor);
1348
1387
  await invoke(makeEditOverride(directory, DEFAULT_CONFIG), "edit", {
1349
1388
  path: returnedPath,
1350
- edits: [{ op: "replace", anchor, body: ["updated"] }],
1389
+ edits: [{ action: "replace", anchor, body: ["updated"] }],
1351
1390
  });
1352
1391
  assert.equal(await readFile(file, "utf8"), "updated\n");
1353
1392
  assert.equal(await readFile(otherFile, "utf8"), "needle\n");
@@ -1382,10 +1421,11 @@ test("literal LF and CRLF queries match original valid and invalid UTF-8 files a
1382
1421
  assert.match(result.content[0].text, /1│alpha\n2│beta/);
1383
1422
  assert.match(result.content[0].text, /1#[0-9A-Z]+│alpha\n2#[0-9A-Z]+│beta/);
1384
1423
  } else if (outputMode === "count") {
1385
- assert.equal(reportOf(result)?.matches, 4);
1386
- assert.equal(reportOf(result)?.files, 2);
1424
+ assert.equal(grepCounts(result).matches, 4);
1425
+ assert.equal(grepCounts(result).files, 2);
1387
1426
  }
1388
- assert.equal(result.details?.incomplete, undefined);
1427
+ assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
1428
+ assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
1389
1429
  const lf = await invoke(grep, "lf", {
1390
1430
  path: directory,
1391
1431
  pattern: "alpha\nbeta",
@@ -3,15 +3,37 @@ import { mkdtemp, rm, truncate, writeFile } from "node:fs/promises";
3
3
  import { tmpdir } from "node:os";
4
4
  import { join } from "node:path";
5
5
  import { test } from "node:test";
6
- import { formatSize } from "@earendil-works/pi-coding-agent";
6
+ import { formatByteSize } from "../pi/budgets.ts";
7
7
  import { DEFAULT_CONFIG } from "../pi/config.ts";
8
8
  import { MAX_MULTILINE_SEARCH_BYTES } from "../pi/grep-search.ts";
9
9
  import { MAX_UNNARROWED_SCAN_BYTES } from "../pi/grep-candidates.ts";
10
10
  import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
11
11
  import { OUTCOME_NEXT } from "../pi/report-schema.ts";
12
12
  import { reportOf } from "../pi/report.ts";
13
+ import { grepCounts } from "../pi/grep-output.testing.ts";
13
14
  import { callTool } from "../pi/tool-call.testing.ts";
14
15
 
16
+ test("repeated all-file candidates → count each file once toward the scan budget", async (t) => {
17
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-repeated-scan-"));
18
+ t.after(() => rm(directory, { recursive: true, force: true }));
19
+ const padding = join(directory, "a.bin");
20
+ const fixture = join(directory, "b.txt");
21
+ await writeFile(padding, "");
22
+ await truncate(padding, MAX_MULTILINE_SEARCH_BYTES);
23
+ await writeFile(fixture, "ab\n");
24
+ const repeats = Math.ceil(MAX_UNNARROWED_SCAN_BYTES / MAX_MULTILINE_SEARCH_BYTES);
25
+ const result = await callTool(makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {}), {
26
+ path: [...Array<string>(repeats).fill(padding), fixture],
27
+ pattern: ".",
28
+ literal: false,
29
+ multiline: true,
30
+ outputMode: "files",
31
+ });
32
+ assert.equal(result.isError, undefined);
33
+ assert.deepEqual(result.content[0], { type: "text", text: "b.txt" });
34
+ assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
35
+ });
36
+
15
37
  test("all-file candidates exceed the scan budget → stop listing and retain scan facts with or without matches", async (t) => {
16
38
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-scan-budget-"));
17
39
  t.after(() => rm(directory, { recursive: true, force: true }));
@@ -68,20 +90,20 @@ test("all-file candidates exceed the scan budget → stop listing and retain sca
68
90
  });
69
91
  assert.equal(stopped, true);
70
92
  assert.deepEqual(offered, [fixture, ...padding, blocked]);
71
- const report = reportOf(result);
93
+ const report = reportOf(result, "grep");
72
94
  assert.ok(report);
73
95
  assert.equal(report.candidates, "all");
74
- assert.equal(report.scanLimit, formatSize(MAX_UNNARROWED_SCAN_BYTES));
96
+ assert.equal(report.scanLimit, formatByteSize(MAX_UNNARROWED_SCAN_BYTES));
75
97
  if (scenario.stderr) {
76
98
  assert.equal(result.isError, true);
77
99
  assert.equal(report.error, "SEARCH_INCOMPLETE");
78
100
  assert.match(report.diagnostics ?? "", /listing interrupted/);
79
101
  } else {
80
102
  assert.equal(result.isError, undefined);
81
- assert.equal(report.matches, scenario.matches);
103
+ assert.equal(grepCounts(result).matches, scenario.matches);
82
104
  assert.equal(report.diagnostics, undefined);
83
- assert.equal(result.details.incomplete, true);
84
105
  assert.equal(report.next, scanLimitNext);
106
+ assert.equal(result.content[0].type, "text");
85
107
  if (scenario.matches) assert.match(result.content[0].text, /fixture\.txt/);
86
108
  }
87
109
  });
@@ -5,7 +5,7 @@ import { tmpdir } from "node:os";
5
5
  import { join } from "node:path";
6
6
  import { makeReadOverride } from "../pi/read-tool.ts";
7
7
  import { makeEditOverride } from "../pi/edit-tool.ts";
8
- import { byteRevision } from "../pi/file-commit.ts";
8
+ import { reportOf } from "../pi/report.ts";
9
9
  import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
10
10
  import { computeLineHash } from "../core/hash.ts";
11
11
  import { callTool } from "../pi/tool-call.testing.ts";
@@ -103,15 +103,14 @@ test("copy/move a 150,000-line block → publish every captured line", async (t)
103
103
  ]);
104
104
  const result: Awaited<ReturnType<typeof edit.execute>> = await callTool(edit, {
105
105
  path,
106
- edits: [{ op, anchor, end, after }],
106
+ edits: [{ action: op, anchor, end, after }],
107
107
  });
108
108
  const expected = Buffer.from(
109
109
  (op === "copy" ? source : "\uFEFFSTART\nEND") + "\r\n" + block.slice(0, -2),
110
110
  "utf8",
111
111
  );
112
112
  assert.deepEqual(await readFile(path), expected);
113
- assert.ok("publishedRevision" in result.details);
114
- assert.equal(result.details.publishedRevision, byteRevision(expected));
115
- assert.equal(result.details.publication, "PUBLISHED");
113
+ assert.equal(result.isError, undefined);
114
+ assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
116
115
  }
117
116
  });