@criogaid/pi-hashline-edit 0.10.0 → 0.12.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 (103) hide show
  1. package/README.md +21 -7
  2. package/README.zh-CN.md +22 -8
  3. package/package.json +7 -6
  4. package/src/core/AGENTS.md +2 -2
  5. package/src/core/apply.test.ts +4 -4
  6. package/src/core/apply.ts +3 -3
  7. package/src/core/character-set.test.ts +2 -3
  8. package/src/core/character-set.ts +1 -1
  9. package/src/core/ranges.test.ts +2 -1
  10. package/src/core/ranges.ts +1 -1
  11. package/src/core/regex.ts +6 -2
  12. package/src/core/replace.test.ts +51 -0
  13. package/src/core/replace.ts +10 -4
  14. package/src/core/types.ts +1 -1
  15. package/src/index.ts +9 -9
  16. package/src/integration/config-registration.test.ts +8 -10
  17. package/src/integration/fusion-dispatch.test.ts +2 -2
  18. package/src/integration/grep-errors.test.ts +3 -3
  19. package/src/integration/grep-flags.test.ts +5 -5
  20. package/src/integration/grep-js-completeness.test.ts +12 -11
  21. package/src/integration/grep-line-candidates.test.ts +5 -3
  22. package/src/integration/grep-matrix.test.ts +5 -4
  23. package/src/integration/grep-order.test.ts +4 -4
  24. package/src/integration/grep-path-matrix.test.ts +5 -4
  25. package/src/integration/grep-prefilter-coverage.test.ts +17 -5
  26. package/src/integration/grep-process-limits.test.ts +2 -4
  27. package/src/integration/grep-regex.test.ts +10 -10
  28. package/src/integration/grep-rg.test.ts +93 -48
  29. package/src/integration/grep-scan-budget.test.ts +8 -8
  30. package/src/integration/grep-translation.test.ts +8 -7
  31. package/src/integration/large-text.test.ts +5 -5
  32. package/src/integration/model-budget-lifecycle.test.ts +9 -9
  33. package/src/integration/mutation-file-types.testing.ts +4 -4
  34. package/src/integration/npm-package.test.ts +12 -10
  35. package/src/integration/npm-package.testing.ts +151 -4
  36. package/src/integration/publication-read-changes.test.ts +54 -54
  37. package/src/integration/read-changes.test.ts +4 -4
  38. package/src/integration/replace-worker-boundary.test.ts +1 -1
  39. package/src/integration/report-replay.test.ts +9 -6
  40. package/src/integration/utf8-errors.test.ts +4 -4
  41. package/src/pi/AGENTS.md +13 -12
  42. package/src/pi/action-fusion.test.ts +125 -105
  43. package/src/pi/action-fusion.testing.ts +38 -0
  44. package/src/pi/action-fusion.ts +9 -21
  45. package/src/pi/argument-diagnostics.ts +4 -3
  46. package/src/pi/argument-format.test.ts +19 -18
  47. package/src/pi/argument-validation.test.ts +281 -139
  48. package/src/pi/argument-validation.ts +21 -31
  49. package/src/pi/budgets.ts +1 -4
  50. package/src/pi/codemode.test.ts +7 -2
  51. package/src/pi/config.test.ts +2 -1
  52. package/src/pi/config.ts +8 -3
  53. package/src/pi/diagnostic-buffer.test.ts +23 -13
  54. package/src/pi/edit-tool.ts +48 -63
  55. package/src/pi/error-text.ts +7 -2
  56. package/src/pi/execute.test.ts +178 -157
  57. package/src/pi/failure-context.test.ts +5 -4
  58. package/src/pi/failure-context.ts +1 -1
  59. package/src/pi/failure-path.test.ts +37 -37
  60. package/src/pi/file-commit.test.ts +47 -19
  61. package/src/pi/file-commit.ts +36 -9
  62. package/src/pi/fusion-card.test.ts +22 -21
  63. package/src/pi/fusion-card.ts +9 -5
  64. package/src/pi/grep-candidates.test.ts +18 -20
  65. package/src/pi/grep-candidates.ts +13 -17
  66. package/src/pi/grep-matrix.test.ts +3 -3
  67. package/src/pi/grep-order.test.ts +12 -11
  68. package/src/pi/grep-output.ts +45 -17
  69. package/src/pi/grep-reread.ts +7 -2
  70. package/src/pi/grep-rg.testing.ts +70 -0
  71. package/src/pi/grep-scope.ts +10 -10
  72. package/src/pi/grep-search.ts +8 -11
  73. package/src/pi/grep-tool.ts +40 -53
  74. package/src/pi/grep.test.ts +141 -116
  75. package/src/pi/model-budgets.test.ts +2 -1
  76. package/src/pi/mutation-outcome.testing.ts +0 -1
  77. package/src/pi/mutation-result.test.ts +8 -8
  78. package/src/pi/mutation-result.ts +1 -1
  79. package/src/pi/mutation-runner.ts +10 -6
  80. package/src/pi/output-budgets.test.ts +53 -31
  81. package/src/pi/pi.test.ts +18 -13
  82. package/src/pi/read-tool.ts +13 -18
  83. package/src/pi/read.test.ts +67 -41
  84. package/src/pi/registration.test.ts +25 -19
  85. package/src/pi/render.test.ts +94 -32
  86. package/src/pi/render.ts +7 -4
  87. package/src/pi/replace-regex.ts +1 -1
  88. package/src/pi/replace-tool.ts +6 -16
  89. package/src/pi/replace.test.ts +45 -34
  90. package/src/pi/report-failure.testing.ts +9 -1
  91. package/src/pi/report-schema.ts +225 -86
  92. package/src/pi/report.test.ts +81 -87
  93. package/src/pi/report.ts +35 -22
  94. package/src/pi/result-safety.test.ts +121 -100
  95. package/src/pi/rg-process.test.ts +2 -2
  96. package/src/pi/rg-process.ts +9 -15
  97. package/src/pi/rg-regex.test.ts +13 -0
  98. package/src/pi/rg-regex.ts +2 -2
  99. package/src/pi/schema.ts +23 -0
  100. package/src/pi/tool-factories.testing.ts +59 -0
  101. package/src/pi/write-tool.ts +9 -15
  102. package/src/pi/write.test.ts +30 -31
  103. package/src/testing/run-tests.ts +1 -1
@@ -1,3 +1,4 @@
1
+ import { makeMockGrep } from "../pi/grep-rg.testing.ts";
1
2
  /**
2
3
  * Explicit integration coverage for the platform rg bundled by @vscode/ripgrep.
3
4
  * This suite is excluded from the default test script.
@@ -21,19 +22,20 @@ import { tmpdir } from "node:os";
21
22
  import { dirname, join } from "node:path";
22
23
  import { fileURLToPath, pathToFileURL } from "node:url";
23
24
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
24
- import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
25
- import { makeEditOverride } from "../pi/edit-tool.ts";
26
- import { makeReadOverride } from "../pi/read-tool.ts";
27
- import { makeWriteOverride } from "../pi/write-tool.ts";
28
- import { makeReplaceTool } from "../pi/replace-tool.ts";
29
- import { COMMON_RG_ARGS, MAX_RG_RECORD_BYTES, runRgPaths } from "../pi/rg-process.ts";
30
- import { DEFAULT_OUTPUT_BUDGETS } from "../pi/budgets.ts";
25
+
26
+ const { makeEditOverride } = await import("../pi/tool-factories.testing.ts");
27
+ const { makeReadOverride } = await import("../pi/tool-factories.testing.ts");
28
+ const { makeWriteOverride } = await import("../pi/tool-factories.testing.ts");
29
+ const { makeReplaceTool } = await import("../pi/tool-factories.testing.ts");
30
+ import { COMMON_RG_ARGS, runRgPaths } from "../pi/rg-process.ts";
31
+ const { DEFAULT_OUTPUT_BUDGETS } = await import("../pi/tool-factories.testing.ts");
32
+ const MAX_RG_RECORD_BYTES = 16 * 1024 * 1024;
31
33
  import { computeLineHash } from "../core/hash.ts";
32
34
  import { callTool } from "../pi/tool-call.testing.ts";
33
- import { DEFAULT_CONFIG } from "../pi/config.ts";
35
+ const { DEFAULT_CONFIG, makeGrepOverride } = await import("../pi/tool-factories.testing.ts");
34
36
  import { canonicalPath } from "../pi/path.ts";
35
37
  import { reportOf } from "../pi/report.ts";
36
- import { grepCounts } from "../pi/grep-output.testing.ts";
38
+ const { grepCounts } = await import("../pi/grep-output.testing.ts");
37
39
  import { hostCompiles, unsupportedByHost } from "../testing/regex-host.testing.ts";
38
40
 
39
41
  const invoke = (
@@ -50,7 +52,7 @@ test("real rg emits anchored matches from a temporary directory", async () => {
50
52
  try {
51
53
  const file = join(directory, "fixture.ts");
52
54
  await writeFile(file, "needle\nother\n");
53
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
55
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
54
56
 
55
57
  for (const pattern of ["needle", ["needle"]]) {
56
58
  const result: any = await invoke(tool, "0", { pattern, literal: true }, undefined, undefined);
@@ -67,7 +69,7 @@ test("real rg finds whitespace-only literal queries and OR alternatives", async
67
69
  try {
68
70
  const file = join(directory, "fixture.txt");
69
71
  await writeFile(file, "plain word\nfirst\tsecond\nboth \tforms\n");
70
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
72
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
71
73
  for (const { pattern, expected } of [
72
74
  { pattern: " ", expected: 2 },
73
75
  { pattern: "\t", expected: 2 },
@@ -101,7 +103,7 @@ test("real rg uses smart-case across OR patterns and Unicode matches", async ()
101
103
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-case-"));
102
104
  try {
103
105
  await writeFile(join(directory, "fixture.ts"), "FOO abc\nfoo BAR\nfoo bar\nK zip\nk zip\n");
104
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
106
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
105
107
  const lower = await invoke(
106
108
  tool,
107
109
  "0",
@@ -134,7 +136,7 @@ test("explicit ignoreCase overrides smart-case for regex and literal queries", a
134
136
  try {
135
137
  const file = join(directory, "fixture.txt");
136
138
  await writeFile(file, "FOO\nfoo\n");
137
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
139
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
138
140
  const sensitive = await invoke(
139
141
  tool,
140
142
  "0",
@@ -193,7 +195,7 @@ test("owned rg processes ignore RIPGREP_CONFIG_PATH", async () => {
193
195
  );
194
196
  await writeFile(join(directory, "fixture.ts"), "FOO\nfoo\nbar\n");
195
197
  process.env.RIPGREP_CONFIG_PATH = config;
196
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
198
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
197
199
  const result: any = await invoke(
198
200
  tool,
199
201
  "0",
@@ -230,7 +232,7 @@ test("real rg separates exact text from regex", async () => {
230
232
  join(directory, "fixture.ts"),
231
233
  "FOO\nfoo\nfoo0\nfoo(0)\nqueueTool(\nfoo(?=bar)\n",
232
234
  );
233
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
235
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
234
236
  for (const pattern of ["(?i:^foo$)", "(?<name>foo)$"].filter((source) =>
235
237
  hostCompiles(source),
236
238
  )) {
@@ -256,7 +258,7 @@ test("real rg keeps CRLF line-end anchors with regex OR matching", async () => {
256
258
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-crlf-"));
257
259
  try {
258
260
  await writeFile(join(directory, "fixture.ts"), "alpha beta drop\r\nalpha beta\r\nalpha only");
259
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
261
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
260
262
  const ending = await invoke(
261
263
  tool,
262
264
  "0",
@@ -287,7 +289,7 @@ test("real rg finds a late OR match and respects the line limit", async () => {
287
289
  join(directory, "fixture.ts"),
288
290
  "foo skip\n".repeat(4096) + "foo keep\nfoo keep later\n",
289
291
  );
290
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
292
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
291
293
  const result = await invoke(
292
294
  tool,
293
295
  "0",
@@ -375,7 +377,7 @@ test("directory searches respect ignores but explicit files and hidden files rem
375
377
  await writeFile(join(directory, ".ignore"), "ignored.txt\n");
376
378
  const ignored = join(directory, "ignored.txt");
377
379
  await writeFile(ignored, "needle\n");
378
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
380
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
379
381
  const normal = await invoke(
380
382
  tool,
381
383
  "0",
@@ -424,7 +426,7 @@ test("gitignored directory → root traversal skips it and an explicit directory
424
426
  await writeFile(join(directory, ".gitignore"), "ignored/\n");
425
427
  await writeFile(join(ignored, "x.txt"), "needle\n");
426
428
  await writeFile(join(directory, "a.txt"), "needle\n");
427
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
429
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
428
430
  for (const [path, expected] of [
429
431
  [directory, "a.txt"],
430
432
  [ignored, "ignored/x.txt"],
@@ -458,7 +460,7 @@ test("named directory → nested ignores, .git and links still limit traversal",
458
460
  await mkdir(dirname(join(directory, file)), { recursive: true });
459
461
  await writeFile(join(directory, file), "needle\n");
460
462
  }
461
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
463
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
462
464
  for (const { path, expected, linked } of [
463
465
  { path: "node_modules/foo", expected: "node_modules/foo/index.js" },
464
466
  { path: "node_modules/foo/dist", expected: "node_modules/foo/dist/bundle.js" },
@@ -507,7 +509,7 @@ test("directory traversal with caller globs → excludes .git while an explicit
507
509
  await mkdir(git);
508
510
  await writeFile(join(git, "x.txt"), "needle\n");
509
511
  await writeFile(join(directory, "a.txt"), "needle\n");
510
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
512
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
511
513
  for (const multiline of [false, true]) {
512
514
  for (const glob of [undefined, "*"]) {
513
515
  for (const [path, expected] of [
@@ -531,6 +533,49 @@ test("directory traversal with caller globs → excludes .git while an explicit
531
533
  }
532
534
  });
533
535
 
536
+ test("case-variant candidate paths → deduplicate aliases while retaining distinct files and later matches", async (t) => {
537
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-case-dedup-"));
538
+ t.after(() => rm(directory, { recursive: true, force: true }));
539
+ const first = join(directory, "MiXeD.txt");
540
+ const variant = join(directory, "mixed.txt");
541
+ const later = join(directory, "later.txt");
542
+ await writeFile(first, "needle\n");
543
+ await writeFile(later, "needle\n");
544
+ let distinct = true;
545
+ try {
546
+ await writeFile(variant, "needle\n", { flag: "wx" });
547
+ } catch (error) {
548
+ if (error instanceof Error && "code" in error && error.code === "EEXIST") distinct = false;
549
+ else throw error;
550
+ }
551
+ const expected = distinct
552
+ ? "MiXeD.txt: 1\nmixed.txt: 1\nlater.txt: 1"
553
+ : "MiXeD.txt: 1\nlater.txt: 1";
554
+ const limit = distinct ? 3 : 2;
555
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
556
+ for (const [pattern, multiline] of [
557
+ ["needle", false],
558
+ ["needle", true],
559
+ [".", true],
560
+ ] as const) {
561
+ await t.test(`${pattern}, multiline=${multiline}`, async () => {
562
+ const result = await callTool(tool, {
563
+ path: [first, variant, later],
564
+ pattern: [pattern],
565
+ literal: false,
566
+ multiline,
567
+ outputMode: "count",
568
+ limit,
569
+ });
570
+ assert.equal(result.isError, undefined);
571
+ assert.equal(result.content[0].type, "text");
572
+ assert.equal(result.content[0].text.split("\n{\n")[0].replaceAll("\\", "/"), expected);
573
+ assert.equal(grepCounts(result).matches, limit);
574
+ assert.equal(grepCounts(result).files, limit);
575
+ });
576
+ }
577
+ });
578
+
534
579
  test("explicit file with different case and a glob → admits the same file on a case-insensitive filesystem", async (t) => {
535
580
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-case-admission-"));
536
581
  t.after(() => rm(directory, { recursive: true, force: true }));
@@ -548,7 +593,7 @@ test("explicit file with different case and a glob → admits the same file on a
548
593
  const original = await stat(file, { bigint: true });
549
594
  assert.equal(alias.dev, original.dev);
550
595
  assert.equal(alias.ino, original.ino);
551
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
596
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
552
597
  const result = await callTool(tool, {
553
598
  path: requested,
554
599
  glob: "*.txt",
@@ -587,7 +632,7 @@ test("explicit file with a dangling sibling link and a glob → returns complete
587
632
  }
588
633
  await assert.rejects(stat(link), { code: "ENOENT" });
589
634
  let listingDiagnostics = "";
590
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
635
+ const tool = makeMockGrep(directory, DEFAULT_CONFIG, {
591
636
  runRgPaths: async (...args) => {
592
637
  const listed = await runRgPaths(...args);
593
638
  listingDiagnostics = listed.stderr;
@@ -614,7 +659,7 @@ test("explicit file paths obey ordered glob filters without inheriting ignore ru
614
659
  const ignored = join(directory, "ignored.ts");
615
660
  await writeFile(join(directory, ".ignore"), "ignored.ts\n");
616
661
  await Promise.all([kept, excluded, ignored].map((path) => writeFile(path, "needle\n")));
617
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
662
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
618
663
  const result: any = await invoke(
619
664
  tool,
620
665
  "0",
@@ -663,7 +708,7 @@ test("directory traversal skips symlinks but explicit linked directories and fil
663
708
  return t.skip("symbolic link creation unavailable");
664
709
  throw error;
665
710
  }
666
- const grep = makeGrepOverrideWithBackend(root, DEFAULT_CONFIG, {});
711
+ const grep = makeGrepOverride(root, DEFAULT_CONFIG);
667
712
  const hidden: any = await invoke(
668
713
  grep,
669
714
  "0",
@@ -702,7 +747,7 @@ test("directory traversal skips symlinks but explicit linked directories and fil
702
747
  await invoke(
703
748
  edit,
704
749
  "0",
705
- { path: resultPath, edits: [{ action: "replace", anchor, body: ["updated"] }] },
750
+ { path: resultPath, steps: [{ action: "replace", anchor, lines: ["updated"] }] },
706
751
  undefined,
707
752
  undefined,
708
753
  );
@@ -719,7 +764,7 @@ test("unbounded lookbehind → native JS matches after rg discovery", async (t)
719
764
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-lookbehind-"));
720
765
  t.after(() => rm(directory, { recursive: true, force: true }));
721
766
  await writeFile(join(directory, "fixture.txt"), "aaab\n");
722
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
767
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
723
768
  const result = await invoke(tool, "0", { pattern: "(?<=a+)b", literal: false });
724
769
  assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
725
770
  assert.equal(grepCounts(result).matches, 1);
@@ -729,7 +774,7 @@ test("line-based grep uses context for display but never matches across CRLF", a
729
774
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-lines-"));
730
775
  try {
731
776
  await writeFile(join(directory, "fixture.txt"), "alpha\r\nbeta\r\ngamma\r\n");
732
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
777
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
733
778
  const context = await invoke(
734
779
  tool,
735
780
  "0",
@@ -768,7 +813,7 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
768
813
  try {
769
814
  const file = join(directory, "fixture.txt");
770
815
  await writeFile(file, "alpha\r\nbeta\r\ngamma\r\n");
771
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
816
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
772
817
  const regex = await invoke(
773
818
  tool,
774
819
  "0",
@@ -843,7 +888,7 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
843
888
  await invoke(
844
889
  edit,
845
890
  "0",
846
- { path: file, edits: [{ action: "replace", anchor, body: ["updated"] }] },
891
+ { path: file, steps: [{ action: "replace", anchor, lines: ["updated"] }] },
847
892
  undefined,
848
893
  undefined,
849
894
  );
@@ -858,7 +903,7 @@ test("multiline grep preserves BOM and standalone CR while normalizing CRLF", as
858
903
  try {
859
904
  const file = join(directory, "fixture.txt");
860
905
  await writeFile(file, "\uFEFFalpha\rsolo\r\nbeta\r\n");
861
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
906
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
862
907
  const result = await invoke(
863
908
  tool,
864
909
  "0",
@@ -887,7 +932,7 @@ test("multiline grep anchors empty lines and line-start zero-width matches", asy
887
932
  try {
888
933
  const file = join(directory, "fixture.txt");
889
934
  await writeFile(file, "alpha\r\n\r\nbeta\r\n");
890
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
935
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
891
936
  const span = await invoke(
892
937
  tool,
893
938
  "0",
@@ -918,7 +963,7 @@ test("multiline zero-width EOF stays on the last existing physical line", {
918
963
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-eof-"));
919
964
  try {
920
965
  const file = join(directory, "fixture.txt");
921
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
966
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
922
967
  for (const content of ["last line", "last line\n"]) {
923
968
  await writeFile(file, content);
924
969
  const result = await invoke(
@@ -947,7 +992,7 @@ test("files and count modes aggregate only matching lines inside the limit", asy
947
992
  const second = join(directory, "b.txt");
948
993
  await writeFile(first, "needle needle\nneedle\n");
949
994
  await writeFile(second, "needle\n");
950
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
995
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
951
996
  const query = { literal: true, pattern: "needle", path: [first, second], limit: 1 };
952
997
  const files = await invoke(
953
998
  tool,
@@ -994,7 +1039,7 @@ test("real rg accepts wildcard-only regexes and preserves limits and literal mod
994
1039
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-wildcard-"));
995
1040
  try {
996
1041
  await writeFile(join(directory, "fixture.txt"), "first\n\n.*\n");
997
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1042
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
998
1043
  for (const [pattern, expected] of [
999
1044
  ["[\\w.*]*", 3],
1000
1045
  ["^[\\w.*]+$", 2],
@@ -1053,7 +1098,7 @@ test("tools share physical lines and anchors across text representations", async
1053
1098
  });
1054
1099
  assert.equal(await readFile(file, "utf8"), before);
1055
1100
  const read = await call(makeReadOverride(directory, DEFAULT_CONFIG), { path: file });
1056
- const grep = await call(makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {}), {
1101
+ const grep = await call(makeGrepOverride(directory, DEFAULT_CONFIG), {
1057
1102
  literal: true,
1058
1103
  path: file,
1059
1104
  pattern: "old",
@@ -1064,7 +1109,7 @@ test("tools share physical lines and anchors across text representations", async
1064
1109
  const anchor = rows(grep)[1].split("│")[0];
1065
1110
  const edit = await call(makeEditOverride(directory, DEFAULT_CONFIG), {
1066
1111
  path: file,
1067
- edits: [{ action: "replace", anchor, body: ["new"] }],
1112
+ steps: [{ action: "replace", anchor, lines: ["new"] }],
1068
1113
  });
1069
1114
  assert.equal(await readFile(file, "utf8"), before.replace("old", "new"));
1070
1115
  assert.match(edit.details.diff, /^-2 old/m);
@@ -1087,7 +1132,7 @@ test("long-line previews preserve full-line anchors across literal and JavaScrip
1087
1132
  const long = "😀界".repeat(220) + "NEEDLE" + "tail".repeat(200);
1088
1133
  const context = "x" + "😀".repeat(400);
1089
1134
  await writeFile(join(directory, "long.txt"), `${long}\r\nfollow\r\n${context}\r\n`);
1090
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1135
+ const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
1091
1136
  for (const params of [
1092
1137
  { literal: true, pattern: "NEEDLE" },
1093
1138
  { pattern: ["NEEDLE", "tail"], literal: true },
@@ -1127,7 +1172,7 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1127
1172
  const before = "\uFEFFhead\r\nalpha\r\nbeta\nstand\rCR\r\r\nlast";
1128
1173
  const write = makeWriteOverride(directory);
1129
1174
  const read = makeReadOverride(directory, DEFAULT_CONFIG);
1130
- const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1175
+ const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
1131
1176
  await invoke(write, "write", { path, content: before, mode: "create" }, undefined, undefined, {
1132
1177
  cwd: directory,
1133
1178
  } as ExtensionContext);
@@ -1193,12 +1238,12 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1193
1238
  "edit",
1194
1239
  {
1195
1240
  path,
1196
- edits: [
1241
+ steps: [
1197
1242
  {
1198
1243
  action: "replace",
1199
1244
  anchor: rows[1].split("│")[0],
1200
1245
  end: rows[2].split("│")[0],
1201
- body: ["A", "B", "C"],
1246
+ lines: ["A", "B", "C"],
1202
1247
  },
1203
1248
  ],
1204
1249
  },
@@ -1269,7 +1314,7 @@ test("visible source escapes remain readable and searchable while real CRLF stay
1269
1314
  );
1270
1315
  assert.ok(read.content[0].type === "text");
1271
1316
  assert.ok(read.content[0].text.includes(String.raw`const eol = "\r\n";`));
1272
- const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1317
+ const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
1273
1318
  for (const query of [
1274
1319
  { pattern: String.raw`\r\n`, literal: true },
1275
1320
  { pattern: String.raw`\\r\\n`, literal: false },
@@ -1311,7 +1356,7 @@ test("grep retains original paths and ignores unmatched invalid UTF-8 across man
1311
1356
  Array.from({ length: 70 }, (_, i) => writeFile(join(directory, `file ${i}.txt`), "--\r\n")),
1312
1357
  );
1313
1358
  await writeFile(join(directory, "invalid.txt"), Buffer.from([0xc3, 0x28]));
1314
- const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1359
+ const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
1315
1360
  const result = await invoke(
1316
1361
  grep,
1317
1362
  "grep",
@@ -1340,7 +1385,7 @@ test("NUL files are silently skipped across search scopes and output modes", asy
1340
1385
  ]);
1341
1386
  await writeFile(binary, source);
1342
1387
  await writeFile(join(directory, "valid.txt"), "needle\n");
1343
- const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1388
+ const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
1344
1389
  for (const outputMode of ["content", "files", "count"] as const) {
1345
1390
  for (const multiline of [false, true]) {
1346
1391
  const skipped = await invoke(
@@ -1374,7 +1419,7 @@ test("NUL files are silently skipped across search scopes and output modes", asy
1374
1419
  test("invalid UTF-8 matches use plain previews while valid files retain edit anchors", async () => {
1375
1420
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-preview-"));
1376
1421
  try {
1377
- const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1422
+ const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
1378
1423
  const invalid = join(directory, "invalid.txt");
1379
1424
  for (const separator of ["\n", "\r\n"]) {
1380
1425
  const source = Buffer.concat([
@@ -1451,7 +1496,7 @@ test("grep paths with input shorthand or literal separators still edit the searc
1451
1496
  await mkdir(dirname(otherFile), { recursive: true });
1452
1497
  await writeFile(file, "needle\n");
1453
1498
  await writeFile(otherFile, "needle\n");
1454
- const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1499
+ const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
1455
1500
  let content = "";
1456
1501
  let returnedPath = "";
1457
1502
  for (const outputMode of ["content", "files", "count"] as const) {
@@ -1476,7 +1521,7 @@ test("grep paths with input shorthand or literal separators still edit the searc
1476
1521
  assert.ok(anchor);
1477
1522
  await invoke(makeEditOverride(directory, DEFAULT_CONFIG), "edit", {
1478
1523
  path: returnedPath,
1479
- edits: [{ action: "replace", anchor, body: ["updated"] }],
1524
+ steps: [{ action: "replace", anchor, lines: ["updated"] }],
1480
1525
  });
1481
1526
  assert.equal(await readFile(file, "utf8"), "updated\n");
1482
1527
  assert.equal(await readFile(otherFile, "utf8"), "needle\n");
@@ -1495,7 +1540,7 @@ test("literal LF and CRLF queries match original valid and invalid UTF-8 files a
1495
1540
  join(directory, "invalid.txt"),
1496
1541
  Buffer.concat([Buffer.from("alpha\r\nbeta\r\n"), Buffer.from([0xff])]),
1497
1542
  );
1498
- const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1543
+ const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
1499
1544
  for (const outputMode of ["content", "files", "count"] as const) {
1500
1545
  await t.test(outputMode, async () => {
1501
1546
  const result = await invoke(grep, "crlf", {
@@ -1,16 +1,17 @@
1
+ import { makeMockGrep } from "../pi/grep-rg.testing.ts";
1
2
  import assert from "node:assert/strict";
2
3
  import { mkdtemp, rm, truncate, writeFile } from "node:fs/promises";
3
4
  import { tmpdir } from "node:os";
4
5
  import { join } from "node:path";
5
6
  import { test } from "node:test";
6
7
  import { formatByteSize } from "../pi/budgets.ts";
7
- import { DEFAULT_CONFIG } from "../pi/config.ts";
8
- import { MAX_MULTILINE_SEARCH_BYTES } from "../pi/grep-search.ts";
9
- import { MAX_UNNARROWED_SCAN_BYTES } from "../pi/grep-candidates.ts";
10
- import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
8
+ const { DEFAULT_CONFIG, makeGrepOverride } = await import("../pi/tool-factories.testing.ts");
9
+ const MAX_MULTILINE_SEARCH_BYTES = 64 * 1024 * 1024;
10
+ const { MAX_UNNARROWED_SCAN_BYTES } = await import("../pi/grep-candidates.ts");
11
+
11
12
  import { OUTCOME_NEXT } from "../pi/report-schema.ts";
12
13
  import { reportOf } from "../pi/report.ts";
13
- import { grepCounts } from "../pi/grep-output.testing.ts";
14
+ const { grepCounts } = await import("../pi/grep-output.testing.ts");
14
15
  import { callTool } from "../pi/tool-call.testing.ts";
15
16
 
16
17
  test("repeated all-file candidates → count each file once toward the scan budget", async (t) => {
@@ -22,7 +23,7 @@ test("repeated all-file candidates → count each file once toward the scan budg
22
23
  await truncate(padding, MAX_MULTILINE_SEARCH_BYTES);
23
24
  await writeFile(fixture, "ab\n");
24
25
  const repeats = Math.ceil(MAX_UNNARROWED_SCAN_BYTES / MAX_MULTILINE_SEARCH_BYTES);
25
- const result = await callTool(makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {}), {
26
+ const result = await callTool(makeGrepOverride(directory, DEFAULT_CONFIG), {
26
27
  path: [...Array<string>(repeats).fill(padding), fixture],
27
28
  pattern: ".",
28
29
  literal: false,
@@ -69,7 +70,7 @@ test("all-file candidates exceed the scan budget → stop listing and retain sca
69
70
  await writeFile(fixture, scenario.source);
70
71
  const offered: string[] = [];
71
72
  let stopped = false;
72
- const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
73
+ const tool = makeMockGrep(directory, DEFAULT_CONFIG, {
73
74
  runRgLines: async () => assert.fail("multiline candidates must use file listing"),
74
75
  runRgPaths: async (_args, _signal, onFile) => {
75
76
  for (const path of paths) {
@@ -92,7 +93,6 @@ test("all-file candidates exceed the scan budget → stop listing and retain sca
92
93
  assert.deepEqual(offered, [fixture, ...padding, blocked]);
93
94
  const report = reportOf(result, "grep");
94
95
  assert.ok(report);
95
- assert.equal(report.scanned, "all");
96
96
  assert.equal(report.scanLimit, formatByteSize(MAX_UNNARROWED_SCAN_BYTES));
97
97
  if (scenario.stderr) {
98
98
  assert.equal(result.isError, true);
@@ -1,14 +1,15 @@
1
+ import { searchWithMockRg } from "../pi/grep-rg.testing.ts";
1
2
  import assert from "node:assert/strict";
2
3
  import { mkdtemp, rm, writeFile } from "node:fs/promises";
3
4
  import { tmpdir } from "node:os";
4
5
  import { join, resolve } from "node:path";
5
6
  import { test } from "node:test";
6
7
  import { searchText, type GrepQuery } from "../core/search.ts";
7
- import { searchFiles, type GrepFileMatch } from "../pi/grep-search.ts";
8
+ import type { GrepFileMatch } from "../pi/grep-search.ts";
8
9
  import { runRgLines, runRgPaths } from "../pi/rg-process.ts";
9
- import { candidateArgs } from "../pi/grep-candidates.ts";
10
+ import { fileCandidateArgs } from "../pi/grep-rg.testing.ts";
10
11
  import { lineCandidatePatterns } from "../pi/rg-regex.ts";
11
- import { extractPrefilter } from "../core/search-analysis.ts";
12
+
12
13
  import { REGEX_TRANSLATION_CORPUS } from "../testing/regex-translation-corpus.ts";
13
14
  import { unsupportedByHost } from "../testing/regex-host.testing.ts";
14
15
 
@@ -70,7 +71,7 @@ test("upstream regex witnesses × LF/CRLF/malformed bytes → sound candidates,
70
71
  inputs.push({ bytes, filePath });
71
72
  }
72
73
  const candidates = new Map<string, readonly number[] | undefined>();
73
- const result = await searchFiles({
74
+ const result = await searchWithMockRg({
74
75
  backend: {
75
76
  runRgPaths: async () => assert.fail("unexpected file fallback"),
76
77
  runRgLines: (args, signal, maxLines, onFile) =>
@@ -131,7 +132,7 @@ test("real rg AND/OR file prefilter → broad candidates retain exact JS results
131
132
  };
132
133
  const candidates: string[] = [];
133
134
  const listing = await runRgPaths(
134
- [...candidateArgs(extractPrefilter(query)), "--null", "--", directory],
135
+ [...(await fileCandidateArgs(query)), "--null", "--", directory],
135
136
  AbortSignal.timeout(timeoutMs),
136
137
  (path) => {
137
138
  candidates.push(resolve(path));
@@ -141,7 +142,7 @@ test("real rg AND/OR file prefilter → broad candidates retain exact JS results
141
142
  assert.equal(listing.code, 0);
142
143
  assert.equal(listing.stderr, "");
143
144
  assert.deepEqual(candidates.sort(), [paths[0], paths[1], paths[2], paths[3], paths[4]].sort());
144
- const result = await searchFiles({
145
+ const result = await searchWithMockRg({
145
146
  backend: { runRgPaths, runRgLines },
146
147
  scope: { globs: [], searchPaths: [directory] },
147
148
  query,
@@ -169,7 +170,7 @@ test("oversized line translation → file prefilter fallback preserves JS result
169
170
  const path = join(directory, "long.txt");
170
171
  await writeFile(path, pattern);
171
172
  let filePasses = 0;
172
- const result = await searchFiles({
173
+ const result = await searchWithMockRg({
173
174
  backend: {
174
175
  runRgPaths: (...args) => {
175
176
  filePasses++;
@@ -3,11 +3,11 @@ import assert from "node:assert/strict";
3
3
  import { mkdtemp, open, rm, writeFile } from "node:fs/promises";
4
4
  import { tmpdir } from "node:os";
5
5
  import { join } from "node:path";
6
- import { makeReadOverride } from "../pi/read-tool.ts";
7
- import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
6
+ import { makeReadOverride } from "../pi/tool-factories.testing.ts";
7
+ import { makeGrepOverride } from "../pi/tool-factories.testing.ts";
8
8
  import { computeLineHash } from "../core/hash.ts";
9
9
  import { callTool } from "../pi/tool-call.testing.ts";
10
- import { DEFAULT_CONFIG } from "../pi/config.ts";
10
+ import { DEFAULT_CONFIG } from "../pi/tool-factories.testing.ts";
11
11
 
12
12
  const call = (tool: any, params: any) => callTool(tool, params, { toolCallId: "large-text" });
13
13
 
@@ -15,7 +15,7 @@ test("read and line-based grep retain anchors on LF and CRLF files over 100 MiB"
15
15
  const directory = await mkdtemp(join(tmpdir(), "hashline-large-text-"));
16
16
  try {
17
17
  const read = makeReadOverride(directory, DEFAULT_CONFIG);
18
- const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
18
+ const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
19
19
  for (const ending of ["\n", "\r\n"]) {
20
20
  const path = join(directory, "large.txt");
21
21
  const handle = await open(path, "w");
@@ -56,7 +56,7 @@ test("CRLF snapshots normalize terminators split across read chunks and preserve
56
56
  try {
57
57
  const path = join(directory, "boundary.txt");
58
58
  await writeFile(path, "x".repeat(65535) + "\r\nneedle\r\nstandalone\r");
59
- const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
59
+ const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
60
60
  const result: any = await call(grep, {
61
61
  path,
62
62
  pattern: ["needle", "standalone\r"],
@@ -5,7 +5,7 @@ import { join } from "node:path";
5
5
  import hashlineEdit from "../index.ts";
6
6
  import { callTool } from "../pi/tool-call.testing.ts";
7
7
  import { outputBudgetsFor } from "../pi/budgets.ts";
8
- import { decodeReport, reportOf } from "../pi/report.ts";
8
+ import { reportOf } from "../pi/report.ts";
9
9
  import type { Report } from "../pi/report-schema.ts";
10
10
  import { openTestSession } from "../testing/session.testing.ts";
11
11
 
@@ -81,7 +81,7 @@ test("model capacity changes → read and grep grow and shrink without re-regist
81
81
  assert.ok(large.content[0].text.includes(text));
82
82
  await session.setModel({ ...model, id: "small", contextWindow: 65_536 });
83
83
  const smallAgain = await callTool(tool, { path }, { ctx });
84
- assert.deepEqual(reportOf(smallAgain, "read")?.lineTooLong, smallReport.lineTooLong);
84
+ assert.equal(reportOf(smallAgain, "read")?.lineTooLong, smallReport?.lineTooLong);
85
85
  const grep = session.getToolDefinition("grep");
86
86
  assert.ok(grep);
87
87
  const smallSearch = await callTool(grep, { path, pattern: "中文", literal: true }, { ctx });
@@ -111,15 +111,15 @@ test("model switches before argument preparation → rejection uses the new budg
111
111
  assert.ok(prepare);
112
112
  const properties: unknown = Reflect.get(tool.parameters, "properties");
113
113
  assert.ok(properties && typeof properties === "object");
114
- const edits: unknown = Reflect.get(properties, "edits");
115
- assert.ok(edits && typeof edits === "object");
116
- const maxEdits: unknown = Reflect.get(edits, "maxItems");
114
+ const steps: unknown = Reflect.get(properties, "steps");
115
+ assert.ok(steps && typeof steps === "object");
116
+ const maxEdits: unknown = Reflect.get(steps, "maxItems");
117
117
  assert.ok(typeof maxEdits === "number");
118
118
  const args = {
119
119
  path: "never-opened.txt",
120
- edits: Array.from({ length: maxEdits }, () => ({
120
+ steps: Array.from({ length: maxEdits }, () => ({
121
121
  action: "append",
122
- body: Array(4).fill("\ud800"),
122
+ lines: Array(4).fill("\ud800"),
123
123
  })),
124
124
  };
125
125
  const reports: Report[] = [];
@@ -134,9 +134,9 @@ test("model switches before argument preparation → rejection uses the new budg
134
134
  assert.ok(error instanceof Error);
135
135
  const budget = outputBudgetsFor(contextWindow);
136
136
  assert.ok(Buffer.byteLength(error.message) <= budget.blockBytes);
137
- const report = decodeReport(error.message, "edit");
137
+ const report = reportOf({ content: [{ type: "text", text: error.message }] }, "edit");
138
138
  assert.ok(report?.issues);
139
- assert.equal(report.issues.length + (report.omittedIssues ?? 0), args.edits.length * 4);
139
+ assert.equal(report.issues.length + (report.omittedIssues ?? 0), args.steps.length * 4);
140
140
  reports.push(report);
141
141
  return true;
142
142
  },
@@ -1,9 +1,9 @@
1
1
  /** Isolate a FIFO open so a regression cannot stall the parent runner's I/O threads. */
2
2
  import assert from "node:assert/strict";
3
3
  import { join } from "node:path";
4
- import { makeEditOverride } from "../pi/edit-tool.ts";
5
- import { makeReplaceTool } from "../pi/replace-tool.ts";
6
- import { DEFAULT_CONFIG } from "../pi/config.ts";
4
+ import { makeEditOverride } from "../pi/tool-factories.testing.ts";
5
+ import { makeReplaceTool } from "../pi/tool-factories.testing.ts";
6
+ import { DEFAULT_CONFIG } from "../pi/tool-factories.testing.ts";
7
7
  import { callTool } from "../pi/tool-call.testing.ts";
8
8
  import { expectToolFailure } from "../pi/report-failure.testing.ts";
9
9
 
@@ -12,7 +12,7 @@ assert.ok(directory);
12
12
  const cases = [
13
13
  {
14
14
  tool: makeEditOverride(directory, DEFAULT_CONFIG),
15
- args: { edits: [{ action: "append", body: ["after"] }] },
15
+ args: { steps: [{ action: "append", lines: ["after"] }] },
16
16
  },
17
17
  {
18
18
  tool: makeReplaceTool(directory, DEFAULT_CONFIG),