@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
@@ -15,7 +15,8 @@ import {
15
15
  writeFile,
16
16
  } from "node:fs/promises";
17
17
  import { tmpdir } from "node:os";
18
- import { join } from "node:path";
18
+ import { dirname, join } from "node:path";
19
+ import { pathToFileURL } from "node:url";
19
20
  import { rgPath } from "@vscode/ripgrep";
20
21
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
21
22
  import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
@@ -23,11 +24,19 @@ import { makeEditOverride } from "../pi/edit-tool.ts";
23
24
  import { makeReadOverride } from "../pi/read-tool.ts";
24
25
  import { makeWriteOverride } from "../pi/write-tool.ts";
25
26
  import { makeReplaceTool } from "../pi/replace-tool.ts";
26
- import { COMMON_RG_ARGS, probeRegex, resolveIgnoreCase, runRg } from "../pi/rg-process.ts";
27
+ import {
28
+ COMMON_RG_ARGS,
29
+ MAX_RG_RECORD_BYTES,
30
+ probeRegex,
31
+ resolveIgnoreCase,
32
+ runRg,
33
+ } from "../pi/rg-process.ts";
34
+ import { GREP_MAX_LINE_LENGTH } from "../pi/budgets.ts";
27
35
  import { runRgTextView } from "../pi/rg-text-view.ts";
28
36
  import { computeLineHash } from "../core/hash.ts";
29
37
  import { callTool } from "../pi/tool-call.testing.ts";
30
38
  import { DEFAULT_CONFIG } from "../pi/config.ts";
39
+ import { canonicalPath } from "../pi/path.ts";
31
40
 
32
41
  const invoke = (
33
42
  tool: any,
@@ -46,7 +55,7 @@ test("real rg emits anchored matches from a temporary directory", async () => {
46
55
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
47
56
 
48
57
  for (const pattern of ["needle", ["needle"]]) {
49
- const result: any = await invoke(tool, "0", { pattern }, undefined, undefined);
58
+ const result: any = await invoke(tool, "0", { pattern, literal: true }, undefined, undefined);
50
59
  assert.match(result.content[0].text, /fixture\.ts · 1 match/);
51
60
  assert.match(result.content[0].text, /1#[0-9A-Z]+│needle/);
52
61
  }
@@ -93,13 +102,25 @@ test("real rg uses smart-case across OR patterns and Unicode matches", async ()
93
102
  try {
94
103
  await writeFile(join(directory, "fixture.ts"), "FOO abc\nfoo BAR\nfoo bar\nK zip\nk zip\n");
95
104
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
96
- const lower = await invoke(tool, "0", { pattern: ["foo", "bar"] }, undefined, undefined);
105
+ const lower = await invoke(
106
+ tool,
107
+ "0",
108
+ { literal: true, pattern: ["foo", "bar"] },
109
+ undefined,
110
+ undefined,
111
+ );
97
112
  assert.match(lower.content[0].text, /fixture\.ts · 3 matches/);
98
113
  assert.match(lower.content[0].text, /│FOO abc/);
99
- const mixed = await invoke(tool, "0", { pattern: ["foo", "BAR"] }, undefined, undefined);
114
+ const mixed = await invoke(
115
+ tool,
116
+ "0",
117
+ { literal: true, pattern: ["foo", "BAR"] },
118
+ undefined,
119
+ undefined,
120
+ );
100
121
  assert.match(mixed.content[0].text, /fixture\.ts · 2 matches/);
101
122
  assert.doesNotMatch(mixed.content[0].text, /│FOO abc/);
102
- const unicode = await invoke(tool, "0", { pattern: "k" }, undefined, undefined);
123
+ const unicode = await invoke(tool, "0", { literal: true, pattern: "k" }, undefined, undefined);
103
124
  assert.match(unicode.content[0].text, /fixture\.ts · 2 matches/);
104
125
  assert.match(unicode.content[0].text, /│K zip/);
105
126
  } finally {
@@ -116,7 +137,7 @@ test("explicit ignoreCase overrides smart-case for regex and literal queries", a
116
137
  const sensitive = await invoke(
117
138
  tool,
118
139
  "0",
119
- { pattern: "foo", path: file, ignoreCase: false },
140
+ { literal: true, pattern: "foo", path: file, ignoreCase: false },
120
141
  undefined,
121
142
  undefined,
122
143
  );
@@ -237,9 +258,7 @@ test("owned rg processes ignore RIPGREP_CONFIG_PATH", async () => {
237
258
  const result: any = await invoke(
238
259
  tool,
239
260
  "0",
240
- {
241
- pattern: ["^foo$", "foo"],
242
- },
261
+ { literal: false, pattern: ["^foo$", "foo"] },
243
262
  undefined,
244
263
  undefined,
245
264
  );
@@ -266,57 +285,62 @@ test("owned rg processes ignore RIPGREP_CONFIG_PATH", async () => {
266
285
  }
267
286
  });
268
287
 
269
- test("real rg validates its own regex syntax and limits automatic literal fallback", async () => {
288
+ test("real rg requires an explicit mode and separates exact text from regex", async () => {
270
289
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-regex-"));
271
290
  try {
272
- await writeFile(join(directory, "fixture.ts"), "FOO\nfoo\nqueueTool(\nfoo(?=bar)\n");
291
+ await writeFile(
292
+ join(directory, "fixture.ts"),
293
+ "FOO\nfoo\nfoo0\nfoo(0)\nqueueTool(\nfoo(?=bar)\n",
294
+ );
273
295
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
296
+ await assert.rejects(
297
+ invoke(tool, "missing-mode", { pattern: "foo" }),
298
+ /Validation failed[\s\S]*literal:/,
299
+ );
274
300
  for (const pattern of ["(?i)^foo$", "(?P<name>foo)$"]) {
275
- const result: any = await invoke(tool, "0", { pattern }, undefined, undefined);
301
+ const result = await invoke(tool, "regex", { pattern, literal: false });
276
302
  assert.match(result.content[0].text, /│foo/);
277
- assert.doesNotMatch(result.content[0].text, /Invalid regex/);
278
303
  }
279
- for (const pattern of ["queueTool(", "foo(?=bar)"]) {
280
- const result: any = await invoke(tool, "0", { pattern }, undefined, undefined);
304
+ const regex = await invoke(tool, "regex-punctuation", { pattern: "foo(0)", literal: false });
305
+ assert.match(regex.content[0].text, /│foo0/);
306
+ assert.doesNotMatch(regex.content[0].text, /│foo\(0\)/);
307
+ for (const pattern of ["foo(0)", "queueTool(", "foo(?=bar)"]) {
308
+ const result = await invoke(tool, "literal", { pattern, literal: true });
281
309
  assert.ok(result.content[0].text.includes(`│${pattern}`));
282
- assert.match(result.content[0].text, /Invalid regex; searched the pattern as literal text/);
283
- await assert.rejects(
284
- invoke(tool, "0", { pattern, literal: false }, undefined, undefined),
285
- /regex parse error/,
286
- );
287
310
  }
288
- const missing: any = await invoke(tool, "0", { pattern: "missing(" }, undefined, undefined);
289
- assert.match(missing.content[0].text, /No matches found\n\n\[Invalid regex/);
290
- const explicit: any = await invoke(
291
- tool,
292
- "0",
293
- { pattern: "queueTool(", literal: true },
294
- undefined,
295
- undefined,
311
+ const missing = await invoke(tool, "no-match", { pattern: "missing(", literal: true });
312
+ assert.equal(missing.content[0].text, "No matches found");
313
+ await assert.rejects(
314
+ invoke(tool, "invalid-regex", { pattern: "queueTool(", literal: false }),
315
+ /regex parse error[\s\S]*literal:true/,
296
316
  );
297
- assert.match(explicit.content[0].text, /│queueTool\(/);
298
- assert.doesNotMatch(explicit.content[0].text, /Invalid regex/);
299
317
  await assert.rejects(
300
- invoke(tool, "0", { pattern: ["queueTool(", "\\bfoo\\b"] }, undefined, undefined),
301
- /Invalid regex in compound query/,
318
+ invoke(tool, "invalid-array", { pattern: ["queueTool(", "\\bfoo\\b"], literal: false }),
319
+ /regex parse error[\s\S]*literal:true/,
302
320
  );
303
321
  } finally {
304
322
  await rm(directory, { recursive: true, force: true });
305
323
  }
306
324
  });
307
325
 
308
- test("real rg keeps CRLF line-end anchors with fixed OR matching", async () => {
326
+ test("real rg keeps CRLF line-end anchors with regex OR matching", async () => {
309
327
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-crlf-"));
310
328
  try {
311
329
  await writeFile(join(directory, "fixture.ts"), "alpha beta drop\r\nalpha beta\r\nalpha only");
312
330
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
313
- const ending = await invoke(tool, "0", { pattern: "beta$" }, undefined, undefined);
331
+ const ending = await invoke(
332
+ tool,
333
+ "0",
334
+ { pattern: "beta$", literal: false },
335
+ undefined,
336
+ undefined,
337
+ );
314
338
  assert.match(ending.content[0].text, /fixture\.ts · 1 match/);
315
339
  assert.match(ending.content[0].text, /2#[0-9A-Z]+│alpha beta$/);
316
340
  const alternatives = await invoke(
317
341
  tool,
318
342
  "0",
319
- { pattern: ["beta$", "alpha only"] },
343
+ { pattern: ["beta$", "alpha only"], literal: false },
320
344
  undefined,
321
345
  undefined,
322
346
  );
@@ -383,7 +407,7 @@ test("shared rg runner stops and cleans up after limits, callback failures, and
383
407
  );
384
408
  await assert.rejects(
385
409
  probeRegex(join(directory, "missing-rg"), ["needle"], false),
386
- /^Error: Failed to run ripgrep: /,
410
+ /Failed to run ripgrep/,
387
411
  );
388
412
  } finally {
389
413
  await rm(directory, { recursive: true, force: true });
@@ -397,12 +421,18 @@ test("directory searches respect ignores but explicit files and hidden files rem
397
421
  const ignored = join(directory, "ignored.txt");
398
422
  await writeFile(ignored, "needle\n");
399
423
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
400
- const normal = await invoke(tool, "0", { pattern: "needle" }, undefined, undefined);
424
+ const normal = await invoke(
425
+ tool,
426
+ "0",
427
+ { literal: true, pattern: "needle" },
428
+ undefined,
429
+ undefined,
430
+ );
401
431
  assert.equal(normal.content[0].text, "No matches found");
402
432
  const explicit = await invoke(
403
433
  tool,
404
434
  "0",
405
- { pattern: "needle", path: ignored },
435
+ { literal: true, pattern: "needle", path: ignored },
406
436
  undefined,
407
437
  undefined,
408
438
  );
@@ -410,13 +440,19 @@ test("directory searches respect ignores but explicit files and hidden files rem
410
440
  const excluded = await invoke(
411
441
  tool,
412
442
  "0",
413
- { pattern: "needle", path: ignored, glob: "!ignored.txt" },
443
+ { literal: true, pattern: "needle", path: ignored, glob: "!ignored.txt" },
414
444
  undefined,
415
445
  undefined,
416
446
  );
417
447
  assert.equal(excluded.content[0].text, "No matches found");
418
448
  await writeFile(join(directory, ".hidden.txt"), "needle\n");
419
- const hidden = await invoke(tool, "0", { pattern: "needle" }, undefined, undefined);
449
+ const hidden = await invoke(
450
+ tool,
451
+ "0",
452
+ { literal: true, pattern: "needle" },
453
+ undefined,
454
+ undefined,
455
+ );
420
456
  assert.match(hidden.content[0].text, /\.hidden\.txt · 1 match/);
421
457
  assert.doesNotMatch(hidden.content[0].text, /ignored\.txt · 1 match/);
422
458
  } finally {
@@ -437,6 +473,7 @@ test("explicit file paths obey ordered glob filters without inheriting ignore ru
437
473
  tool,
438
474
  "0",
439
475
  {
476
+ literal: true,
440
477
  pattern: "needle",
441
478
  path: [kept, excluded, ignored],
442
479
  glob: ["*.ts", "!**/*.test.ts"],
@@ -451,11 +488,7 @@ test("explicit file paths obey ordered glob filters without inheriting ignore ru
451
488
  const none: any = await invoke(
452
489
  tool,
453
490
  "0",
454
- {
455
- pattern: "needle",
456
- path: excluded,
457
- glob: "!**/*.test.ts",
458
- },
491
+ { literal: true, pattern: "needle", path: excluded, glob: "!**/*.test.ts" },
459
492
  undefined,
460
493
  undefined,
461
494
  );
@@ -485,12 +518,18 @@ test("directory traversal skips symlinks but explicitly named linked files remai
485
518
  throw error;
486
519
  }
487
520
  const grep = makeGrepOverrideWithBackend(root, DEFAULT_CONFIG, {});
488
- const hidden: any = await invoke(grep, "0", { pattern: "needle" }, undefined, undefined);
521
+ const hidden: any = await invoke(
522
+ grep,
523
+ "0",
524
+ { literal: true, pattern: "needle" },
525
+ undefined,
526
+ undefined,
527
+ );
489
528
  assert.equal(hidden.content[0].text, "No matches found");
490
529
  const found: any = await invoke(
491
530
  grep,
492
531
  "0",
493
- { pattern: "needle", path: join(link, "linked.txt") },
532
+ { literal: true, pattern: "needle", path: join(link, "linked.txt") },
494
533
  undefined,
495
534
  undefined,
496
535
  );
@@ -529,22 +568,27 @@ test("real rg reports unsupported lookarounds and backreferences under Rust rege
529
568
  undefined,
530
569
  );
531
570
  assert.match(valid.content[0].text, /fixture\.txt · 2 matches/);
532
- for (const pattern of ["foo(?=bar)", "(foo)\\1"]) {
571
+ for (const pattern of ["foo(?=bar)", "(foo)\\1", ["plain", "foo(?=bar)"]]) {
533
572
  await assert.rejects(
534
573
  invoke(tool, "0", { pattern, literal: false }, undefined, undefined),
535
- /regex parse error[\s\S]*no lookaround or backreferences; rewrite the pattern, or use replace/,
574
+ (error: Error) => {
575
+ assert.match(error.message, /regex parse error/);
576
+ assert.match(error.message, /no lookaround or backreferences/);
577
+ assert.match(error.message, /rewrite.*pattern/);
578
+ assert.match(error.message, /use replace/);
579
+ return true;
580
+ },
536
581
  );
537
582
  }
538
- const fallback = await invoke(tool, "0", { pattern: "foo(?=bar)" }, undefined, undefined);
539
- assert.match(
540
- fallback.content[0].text,
541
- /No matches found.*Invalid regex; searched the pattern as literal text; ripgrep's Rust regex has no lookaround or backreferences/s,
542
- );
543
- // A parse error without other-dialect syntax gets no dialect hint.
583
+ const literal = await invoke(tool, "0", { pattern: "foo(?=bar)", literal: true });
584
+ assert.equal(literal.content[0].text, "No matches found");
585
+ // A parse error without other-dialect syntax requests an exact-text search.
544
586
  await assert.rejects(
545
587
  invoke(tool, "0", { pattern: "foo(", literal: false }, undefined, undefined),
546
588
  (error: Error) =>
547
- /regex parse error/.test(error.message) && !/lookaround/.test(error.message),
589
+ /regex parse error/.test(error.message) &&
590
+ /literal:true/.test(error.message) &&
591
+ !/lookaround/.test(error.message),
548
592
  );
549
593
  } finally {
550
594
  await rm(directory, { recursive: true, force: true });
@@ -556,7 +600,13 @@ test("line-based grep uses context for display but never matches across CRLF", a
556
600
  try {
557
601
  await writeFile(join(directory, "fixture.txt"), "alpha\r\nbeta\r\ngamma\r\n");
558
602
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
559
- const context = await invoke(tool, "0", { pattern: "alpha", context: 1 }, undefined, undefined);
603
+ const context = await invoke(
604
+ tool,
605
+ "0",
606
+ { literal: true, pattern: "alpha", context: 1 },
607
+ undefined,
608
+ undefined,
609
+ );
560
610
  assert.match(context.content[0].text, /fixture\.txt · 1 match/);
561
611
  assert.match(context.content[0].text, /1#[0-9A-Z]+│alpha/);
562
612
  assert.match(context.content[0].text, /2#[0-9A-Z]+│beta/);
@@ -572,7 +622,7 @@ test("line-based grep uses context for display but never matches across CRLF", a
572
622
  const limited = await invoke(
573
623
  tool,
574
624
  "0",
575
- { pattern: ["alpha", "beta"], outputMode: "count", limit: 1 },
625
+ { literal: true, pattern: ["alpha", "beta"], outputMode: "count", limit: 1 },
576
626
  undefined,
577
627
  undefined,
578
628
  );
@@ -618,7 +668,14 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
618
668
  const bounded = await invoke(
619
669
  tool,
620
670
  "0",
621
- { pattern: "(?s)alpha.*gamma", path: file, multiline: true, outputMode: "count", limit: 2 },
671
+ {
672
+ pattern: "(?s)alpha.*gamma",
673
+ literal: false,
674
+ path: file,
675
+ multiline: true,
676
+ outputMode: "count",
677
+ limit: 2,
678
+ },
622
679
  undefined,
623
680
  undefined,
624
681
  );
@@ -628,7 +685,14 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
628
685
  const files = await invoke(
629
686
  tool,
630
687
  "0",
631
- { pattern: "alpha\\nbeta", path: file, multiline: true, outputMode: "files", limit: 1 },
688
+ {
689
+ pattern: "alpha\\nbeta",
690
+ literal: false,
691
+ path: file,
692
+ multiline: true,
693
+ outputMode: "files",
694
+ limit: 1,
695
+ },
632
696
  undefined,
633
697
  undefined,
634
698
  );
@@ -795,7 +859,7 @@ test("files and count modes aggregate only matching lines inside the limit", asy
795
859
  await writeFile(first, "needle needle\nneedle\n");
796
860
  await writeFile(second, "needle\n");
797
861
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
798
- const query = { pattern: "needle", path: [first, second], limit: 1 };
862
+ const query = { literal: true, pattern: "needle", path: [first, second], limit: 1 };
799
863
  const files = await invoke(
800
864
  tool,
801
865
  "files",
@@ -847,7 +911,7 @@ test("default grep rejects file changes and propagates cancellation", async () =
847
911
  },
848
912
  });
849
913
  await assert.rejects(
850
- invoke(changing, "0", { pattern: "foo", path: file }, undefined, undefined),
914
+ invoke(changing, "0", { literal: true, pattern: "foo", path: file }, undefined, undefined),
851
915
  /File changed during search/,
852
916
  );
853
917
 
@@ -860,7 +924,13 @@ test("default grep rejects file changes and propagates cancellation", async () =
860
924
  },
861
925
  });
862
926
  await assert.rejects(
863
- invoke(cancelling, "0", { pattern: "foo", path: file }, controller.signal, undefined),
927
+ invoke(
928
+ cancelling,
929
+ "0",
930
+ { literal: true, pattern: "foo", path: file },
931
+ controller.signal,
932
+ undefined,
933
+ ),
864
934
  /Operation aborted/,
865
935
  );
866
936
  } finally {
@@ -871,11 +941,11 @@ test("default grep rejects file changes and propagates cancellation", async () =
871
941
  test("owned rg rejects an oversized JSONL record", async () => {
872
942
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-record-limit-"));
873
943
  try {
874
- await writeFile(join(directory, "large.txt"), `needle${"x".repeat(17 * 1024 * 1024)}\n`);
944
+ await writeFile(join(directory, "large.txt"), `needle${"x".repeat(MAX_RG_RECORD_BYTES + 1)}\n`);
875
945
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
876
946
  await assert.rejects(
877
947
  invoke(tool, "0", { pattern: "needle", literal: true }, undefined, undefined),
878
- /record exceeds 16 MiB/,
948
+ new RegExp(`record exceeds ${MAX_RG_RECORD_BYTES / 1024 ** 2} MiB`),
879
949
  );
880
950
  } finally {
881
951
  await rm(directory, { recursive: true, force: true });
@@ -895,7 +965,7 @@ test("real rg accepts wildcard-only regexes and preserves limits and literal mod
895
965
  const result: any = await invoke(
896
966
  tool,
897
967
  "count",
898
- { pattern, outputMode: "count" },
968
+ { pattern, literal: false, outputMode: "count" },
899
969
  undefined,
900
970
  undefined,
901
971
  );
@@ -904,7 +974,7 @@ test("real rg accepts wildcard-only regexes and preserves limits and literal mod
904
974
  const limited: any = await invoke(
905
975
  tool,
906
976
  "limited",
907
- { pattern: ".*", limit: 1 },
977
+ { pattern: ".*", literal: false, limit: 1 },
908
978
  undefined,
909
979
  undefined,
910
980
  );
@@ -934,17 +1004,22 @@ test("tools share physical lines and anchors across text representations", async
934
1004
  result.content[0].text.split("\n").filter((line: string) => /^\d+#/.test(line));
935
1005
  try {
936
1006
  const file = join(directory, "fixture.txt");
937
- for (const before of [
1007
+ for (const [index, before] of [
938
1008
  "a\nold\n",
939
1009
  "a\r\nold\r\n",
940
1010
  "\uFEFFa\r\nold\nkeep\r\n",
941
1011
  "a\rb\nold\n",
942
1012
  "a\rb\nold",
943
- ]) {
944
- await call(makeWriteOverride(directory), { path: file, content: before });
1013
+ ].entries()) {
1014
+ await call(makeWriteOverride(directory), {
1015
+ path: file,
1016
+ content: before,
1017
+ mode: index === 0 ? "create" : "overwrite",
1018
+ });
945
1019
  assert.equal(await readFile(file, "utf8"), before);
946
1020
  const read = await call(makeReadOverride(directory, DEFAULT_CONFIG), { path: file });
947
1021
  const grep = await call(makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {}), {
1022
+ literal: true,
948
1023
  path: file,
949
1024
  pattern: "old",
950
1025
  context: 2,
@@ -986,7 +1061,7 @@ test("long-line previews preserve full-line anchors across literal and Rust rege
986
1061
  await writeFile(join(directory, "long.txt"), `${long}\r\nfollow\r\n${context}\r\n`);
987
1062
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
988
1063
  for (const params of [
989
- { pattern: "NEEDLE" },
1064
+ { literal: true, pattern: "NEEDLE" },
990
1065
  { pattern: ["NEEDLE", "tail"], literal: true },
991
1066
  { pattern: "NEEDLE(?:tail)+", literal: false },
992
1067
  ]) {
@@ -1002,7 +1077,11 @@ test("long-line previews preserve full-line anchors across literal and Rust rege
1002
1077
  assert.ok(row.startsWith(`1#${computeLineHash(1, long, 4)}│[partial, columns `));
1003
1078
  assert.ok(row.includes("NEEDLE"));
1004
1079
  assert.equal(Buffer.from(row).toString("utf8"), row);
1005
- assert.ok(output.includes(`3#${computeLineHash(3, context, 4)}│[partial, columns 1-499]`));
1080
+ assert.ok(
1081
+ output.includes(
1082
+ `3#${computeLineHash(3, context, 4)}│[partial, columns 1-${GREP_MAX_LINE_LENGTH - 1}]`,
1083
+ ),
1084
+ );
1006
1085
  assert.match(output, /anchors hash full lines/);
1007
1086
  }
1008
1087
  } finally {
@@ -1018,7 +1097,7 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1018
1097
  const write = makeWriteOverride(directory);
1019
1098
  const read = makeReadOverride(directory, DEFAULT_CONFIG);
1020
1099
  const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1021
- await invoke(write, "write", { path, content: before }, undefined, undefined, {
1100
+ await invoke(write, "write", { path, content: before, mode: "create" }, undefined, undefined, {
1022
1101
  cwd: directory,
1023
1102
  } as ExtensionContext);
1024
1103
  assert.equal(await readFile(path, "utf8"), before);
@@ -1040,7 +1119,7 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1040
1119
  const contextual = await invoke(
1041
1120
  grep,
1042
1121
  "grep",
1043
- { path, pattern: "alpha", context: 1 },
1122
+ { literal: true, path, pattern: "alpha", context: 1 },
1044
1123
  undefined,
1045
1124
  undefined,
1046
1125
  );
@@ -1067,9 +1146,16 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1067
1146
 
1068
1147
  const expected = "\uFEFFhead\r\nA\r\nB\r\nC\nstand\rCR\r\r\nlast";
1069
1148
  for (const mode of ["edit", "literal", "regex"]) {
1070
- await invoke(write, "reset", { path, content: before }, undefined, undefined, {
1071
- cwd: directory,
1072
- } as ExtensionContext);
1149
+ await invoke(
1150
+ write,
1151
+ "reset",
1152
+ { path, content: before, mode: "overwrite" },
1153
+ undefined,
1154
+ undefined,
1155
+ {
1156
+ cwd: directory,
1157
+ } as ExtensionContext,
1158
+ );
1073
1159
  if (mode === "edit") {
1074
1160
  await invoke(
1075
1161
  makeEditOverride(directory, DEFAULT_CONFIG),
@@ -1111,9 +1197,16 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1111
1197
  assert.equal(await readFile(path, "utf8"), expected, mode);
1112
1198
  }
1113
1199
  // Whole-file write remains the explicit representation boundary, including EOL conversion.
1114
- await invoke(write, "convert", { path, content: "alpha\nbeta\n" }, undefined, undefined, {
1115
- cwd: directory,
1116
- } as ExtensionContext);
1200
+ await invoke(
1201
+ write,
1202
+ "convert",
1203
+ { path, content: "alpha\nbeta\n", mode: "overwrite" },
1204
+ undefined,
1205
+ undefined,
1206
+ {
1207
+ cwd: directory,
1208
+ } as ExtensionContext,
1209
+ );
1117
1210
  assert.equal(await readFile(path, "utf8"), "alpha\nbeta\n");
1118
1211
  } finally {
1119
1212
  await rm(directory, { recursive: true, force: true });
@@ -1128,7 +1221,7 @@ test("visible source escapes remain readable and searchable while real CRLF stay
1128
1221
  await invoke(
1129
1222
  makeWriteOverride(directory),
1130
1223
  "write",
1131
- { path, content: code },
1224
+ { path, content: code, mode: "create" },
1132
1225
  undefined,
1133
1226
  undefined,
1134
1227
  { cwd: directory } as ExtensionContext,
@@ -1177,7 +1270,7 @@ test("visible source escapes remain readable and searchable while real CRLF stay
1177
1270
  }
1178
1271
  });
1179
1272
 
1180
- test("normalized grep batches retain original paths, literal option markers, and decode diagnostics", async () => {
1273
+ test("normalized grep batches retain original paths and ignore unmatched invalid UTF-8", async () => {
1181
1274
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-snapshots-"));
1182
1275
  try {
1183
1276
  await Promise.all(
@@ -1193,59 +1286,321 @@ test("normalized grep batches retain original paths, literal option markers, and
1193
1286
  undefined,
1194
1287
  );
1195
1288
  assert.equal((result.content[0].text.match(/file \d+\.txt: 1/g) ?? []).length, 70);
1196
- assert.match(result.content[0].text, /Search incomplete/);
1197
- assert.match(result.content[0].text, /invalid\.txt/);
1198
- assert.match(result.content[0].text, /UNSUPPORTED_ENCODING/);
1289
+ assert.doesNotMatch(result.content[0].text, /invalid\.txt|UNSUPPORTED_ENCODING/);
1199
1290
  assert.doesNotMatch(result.content[0].text, /hashline-grep-/);
1291
+ assert.equal(result.details?.incomplete, undefined);
1200
1292
  } finally {
1201
1293
  await rm(directory, { recursive: true, force: true });
1202
1294
  }
1203
1295
  });
1204
1296
 
1205
- test("NUL files are rejected on a confirmed hit, not reported as no match", async () => {
1297
+ test("NUL files are silently skipped across search scopes and output modes", async () => {
1206
1298
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-nul-"));
1207
1299
  try {
1208
1300
  const binary = join(directory, "binary.txt");
1209
- await writeFile(binary, Buffer.from("needle\r\nother\0needle\n"));
1301
+ const source = Buffer.concat([
1302
+ Buffer.from("needle\r\n"),
1303
+ Buffer.alloc(65536, 97),
1304
+ Buffer.from("\0needle\n"),
1305
+ ]);
1306
+ await writeFile(binary, source);
1307
+ await writeFile(join(directory, "valid.txt"), "needle\n");
1210
1308
  const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1211
- await assert.rejects(
1212
- invoke(
1309
+ for (const outputMode of ["content", "files", "count"] as const) {
1310
+ for (const multiline of [false, true]) {
1311
+ const skipped = await invoke(
1312
+ grep,
1313
+ "binary",
1314
+ { path: binary, pattern: "needle", literal: true, outputMode, multiline },
1315
+ undefined,
1316
+ undefined,
1317
+ );
1318
+ assert.equal(skipped.content[0].text, "No matches found");
1319
+ const result = await invoke(
1320
+ grep,
1321
+ "directory",
1322
+ { path: directory, pattern: "needle", literal: true, outputMode, multiline, limit: 1 },
1323
+ undefined,
1324
+ undefined,
1325
+ );
1326
+ assert.match(result.content[0].text, /valid\.txt/);
1327
+ assert.doesNotMatch(result.content[0].text, /binary\.txt|UNSUPPORTED_TEXT/);
1328
+ assert.equal(result.details?.incomplete, undefined);
1329
+ }
1330
+ }
1331
+ assert.deepEqual(await readFile(binary), source);
1332
+ } finally {
1333
+ await rm(directory, { recursive: true, force: true });
1334
+ }
1335
+ });
1336
+
1337
+ test("invalid UTF-8 matches use plain previews while valid files retain edit anchors", async () => {
1338
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-preview-"));
1339
+ try {
1340
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1341
+ const invalid = join(directory, "invalid.txt");
1342
+ for (const separator of ["\n", "\r\n"]) {
1343
+ const source = Buffer.concat([
1344
+ Buffer.from(`before${separator}needle `),
1345
+ Buffer.from([0xff]),
1346
+ Buffer.from(`${separator}after${separator}`),
1347
+ ]);
1348
+ await writeFile(invalid, source);
1349
+ const result = await invoke(
1213
1350
  grep,
1214
- "grep",
1215
- { path: binary, pattern: "needle", literal: true },
1351
+ "preview",
1352
+ { path: invalid, pattern: "needle", literal: true, context: 1 },
1216
1353
  undefined,
1217
1354
  undefined,
1218
- ),
1219
- /UNSUPPORTED_TEXT/,
1220
- );
1221
- const absent = await invoke(
1355
+ );
1356
+ assert.match(result.content[0].text, /1│before\n2│needle �\n3│after/);
1357
+ assert.match(result.content[0].text, /Invalid UTF-8.*cannot.*edit anchors/);
1358
+ assert.doesNotMatch(result.content[0].text, /\d+#/);
1359
+ assert.equal(result.details?.incomplete, undefined);
1360
+ assert.deepEqual(await readFile(invalid), source);
1361
+ }
1362
+ await writeFile(join(directory, "valid.txt"), "needle\n");
1363
+ const mixed = await invoke(
1222
1364
  grep,
1223
- "grep",
1224
- { path: binary, pattern: "absent", literal: true },
1365
+ "mixed",
1366
+ { path: directory, pattern: "needle", literal: true },
1225
1367
  undefined,
1226
1368
  undefined,
1227
1369
  );
1228
- assert.equal(absent.content[0].type, "text");
1229
- if (absent.content[0].type === "text") assert.equal(absent.content[0].text, "No matches found");
1230
- await writeFile(join(directory, "valid.txt"), "needle\n");
1231
- await assert.rejects(
1232
- invoke(
1370
+ assert.match(mixed.content[0].text, /valid\.txt · 1 match\n1#[A-Z0-9]+│needle/);
1371
+ assert.match(mixed.content[0].text, /invalid\.txt · 1 match\n2│needle �/);
1372
+ for (const outputMode of ["files", "count"] as const) {
1373
+ const result = await invoke(
1233
1374
  grep,
1234
- "grep",
1235
- { path: directory, pattern: "needle", literal: true },
1375
+ "summary",
1376
+ { path: directory, pattern: "needle", literal: true, outputMode },
1236
1377
  undefined,
1237
1378
  undefined,
1238
- ),
1239
- /UNSUPPORTED_TEXT/,
1379
+ );
1380
+ assert.match(result.content[0].text, /invalid\.txt/);
1381
+ assert.match(result.content[0].text, /valid\.txt/);
1382
+ assert.equal(result.details?.incomplete, undefined);
1383
+ }
1384
+ const multiline = await invoke(
1385
+ grep,
1386
+ "multiline",
1387
+ { path: invalid, pattern: "needle.*\\r\\nafter", literal: false, multiline: true },
1388
+ undefined,
1389
+ undefined,
1240
1390
  );
1241
- const filesMode: any = await invoke(
1391
+ // Raw-byte regex matching follows rg semantics: dot does not match malformed UTF-8.
1392
+ assert.equal(multiline.content[0].text, "No matches found");
1393
+ const rawBoundary = await invoke(
1242
1394
  grep,
1243
- "grep",
1244
- { path: binary, pattern: "needle", literal: true, outputMode: "files" },
1395
+ "raw-boundary",
1396
+ { path: invalid, pattern: "(?-u:needle[^\\r]*\\r\\nafter)", literal: false, multiline: true },
1245
1397
  undefined,
1246
1398
  undefined,
1247
1399
  );
1248
- assert.match(filesMode.content[0].text, /binary\.txt/);
1400
+ assert.match(rawBoundary.content[0].text, /2│needle �\n3│after/);
1401
+ assert.doesNotMatch(rawBoundary.content[0].text, /\d+#/);
1402
+ } finally {
1403
+ await rm(directory, { recursive: true, force: true });
1404
+ }
1405
+ });
1406
+
1407
+ test("grep paths with input shorthand or literal separators still edit the searched file", async (t) => {
1408
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-path-roundtrip-"));
1409
+ try {
1410
+ const cases = [
1411
+ { name: "@config.txt", other: "config.txt" },
1412
+ { name: "space\u00a0file.txt", other: "space file.txt" },
1413
+ ...(process.platform === "win32"
1414
+ ? []
1415
+ : [{ name: "dir\\target.txt", other: "dir/target.txt" }]),
1416
+ ];
1417
+ for (const { name, other } of cases) {
1418
+ await t.test(name, async () => {
1419
+ const file = join(directory, name);
1420
+ const otherFile = join(directory, other);
1421
+ await mkdir(dirname(otherFile), { recursive: true });
1422
+ await writeFile(file, "needle\n");
1423
+ await writeFile(otherFile, "needle\n");
1424
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1425
+ let content = "";
1426
+ let returnedPath = "";
1427
+ for (const outputMode of ["content", "files", "count"] as const) {
1428
+ const result = await invoke(grep, "path", {
1429
+ path: pathToFileURL(file).href,
1430
+ pattern: "needle",
1431
+ literal: true,
1432
+ outputMode,
1433
+ });
1434
+ const text = result.content[0].text;
1435
+ const firstLine = text.split("\n")[0];
1436
+ returnedPath =
1437
+ outputMode === "content"
1438
+ ? firstLine.slice(0, firstLine.lastIndexOf(" · "))
1439
+ : outputMode === "count"
1440
+ ? firstLine.slice(0, firstLine.lastIndexOf(": "))
1441
+ : firstLine;
1442
+ assert.equal(canonicalPath(directory, returnedPath), file, outputMode);
1443
+ if (outputMode === "content") content = text;
1444
+ }
1445
+ const anchor = content.match(/(1#[0-9A-Z]+)│needle/)?.[1];
1446
+ assert.ok(anchor);
1447
+ await invoke(makeEditOverride(directory, DEFAULT_CONFIG), "edit", {
1448
+ path: returnedPath,
1449
+ edits: [{ op: "replace", anchor, body: ["updated"] }],
1450
+ });
1451
+ assert.equal(await readFile(file, "utf8"), "updated\n");
1452
+ assert.equal(await readFile(otherFile, "utf8"), "needle\n");
1453
+ });
1454
+ }
1455
+ } finally {
1456
+ await rm(directory, { recursive: true, force: true });
1457
+ }
1458
+ });
1459
+
1460
+ test("literal CRLF queries use the corresponding UTF-8 and raw-byte search views", async (t) => {
1461
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-raw-crlf-"));
1462
+ try {
1463
+ await writeFile(join(directory, "valid.txt"), "alpha\r\nbeta\r\n");
1464
+ await writeFile(
1465
+ join(directory, "invalid.txt"),
1466
+ Buffer.concat([Buffer.from("alpha\r\nbeta\r\n"), Buffer.from([0xff])]),
1467
+ );
1468
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1469
+ for (const outputMode of ["content", "files", "count"] as const) {
1470
+ await t.test(outputMode, async () => {
1471
+ const result = await invoke(grep, "crlf", {
1472
+ path: directory,
1473
+ pattern: "alpha\r\nbeta",
1474
+ literal: true,
1475
+ multiline: true,
1476
+ outputMode,
1477
+ });
1478
+ assert.match(result.content[0].text, /(?:^|\n)valid\.txt/);
1479
+ assert.match(result.content[0].text, /(?:^|\n)invalid\.txt/);
1480
+ if (outputMode === "content") {
1481
+ assert.match(result.content[0].text, /1│alpha\n2│beta/);
1482
+ assert.match(result.content[0].text, /1#[0-9A-Z]+│alpha\n2#[0-9A-Z]+│beta/);
1483
+ } else if (outputMode === "count") {
1484
+ assert.match(result.content[0].text, /Total: 4 matches in 2 files/);
1485
+ }
1486
+ assert.equal(result.details?.incomplete, undefined);
1487
+ const lf = await invoke(grep, "lf", {
1488
+ path: directory,
1489
+ pattern: "alpha\nbeta",
1490
+ literal: true,
1491
+ multiline: true,
1492
+ outputMode,
1493
+ });
1494
+ assert.match(lf.content[0].text, /valid\.txt/);
1495
+ assert.doesNotMatch(lf.content[0].text, /invalid\.txt/);
1496
+ });
1497
+ }
1498
+ } finally {
1499
+ await rm(directory, { recursive: true, force: true });
1500
+ }
1501
+ });
1502
+
1503
+ test("raw-byte matches changed before revision acquisition are never confirmed by lossy previews", async (t) => {
1504
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-raw-revision-"));
1505
+ try {
1506
+ const file = join(directory, "invalid.txt");
1507
+ for (const separator of ["\n", "\r\n"]) {
1508
+ for (const mode of [false, true, "limited", "eof"] as const) {
1509
+ const multiline = mode !== false;
1510
+ for (const outputMode of ["content", "files", "count"] as const) {
1511
+ await t.test(`${JSON.stringify(separator)} ${mode} ${outputMode}`, async () => {
1512
+ const source = (byte: number) =>
1513
+ Buffer.concat([
1514
+ Buffer.from(`before${separator}${"x".repeat(64 * 1024)}needle`),
1515
+ Buffer.from([byte]),
1516
+ Buffer.from(`${separator}after${separator}`),
1517
+ ]);
1518
+ await writeFile(file, source(0xff));
1519
+ const changedSource =
1520
+ mode === "eof"
1521
+ ? Buffer.concat([source(0xff), Buffer.from(`tail${separator}`)])
1522
+ : source(0xfe);
1523
+ let changed = false;
1524
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
1525
+ search(path, request, signal, onLine) {
1526
+ return runRgTextView(path, request, signal, async (...event) => {
1527
+ if (!changed && JSON.parse(event[0]).type === "match") {
1528
+ changed = true;
1529
+ await writeFile(file, changedSource);
1530
+ }
1531
+ return onLine(...event);
1532
+ });
1533
+ },
1534
+ });
1535
+ const boundary = separator === "\r\n" ? "\\r\\n" : "\\n";
1536
+ await assert.rejects(
1537
+ invoke(grep, "changed", {
1538
+ path: file,
1539
+ pattern:
1540
+ mode === "limited"
1541
+ ? `before${boundary}(?-u:[^\\r\\n]*\\xFF)`
1542
+ : mode === "eof"
1543
+ ? `(?-u:\\xFF${boundary}after${boundary})\\z`
1544
+ : `(?-u:\\xFF${multiline ? `${boundary}after` : ""})`,
1545
+ literal: false,
1546
+ ignoreCase: false,
1547
+ multiline,
1548
+ ...(mode === "limited" || mode === "eof" ? { limit: 1 } : {}),
1549
+ outputMode,
1550
+ }),
1551
+ /File changed during search/,
1552
+ );
1553
+ assert.equal(changed, true);
1554
+ assert.deepEqual(await readFile(file), changedSource);
1555
+ });
1556
+ }
1557
+ }
1558
+ }
1559
+ } finally {
1560
+ await rm(directory, { recursive: true, force: true });
1561
+ }
1562
+ });
1563
+
1564
+ test("raw files lost before summary verification leave only confirmed counts and an incomplete notice", async (t) => {
1565
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-raw-summary-"));
1566
+ try {
1567
+ const invalid = join(directory, "invalid.txt");
1568
+ const valid = join(directory, "valid.txt");
1569
+ for (const outputMode of ["files", "count"] as const) {
1570
+ for (const keepValid of [true, false]) {
1571
+ await t.test(`${outputMode} ${keepValid}`, async () => {
1572
+ await writeFile(invalid, Buffer.concat([Buffer.from("needle "), Buffer.from([0xff])]));
1573
+ await writeFile(valid, "needle\n");
1574
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
1575
+ async search(...args) {
1576
+ const result = await runRgTextView(...args);
1577
+ await rm(invalid);
1578
+ return result;
1579
+ },
1580
+ });
1581
+ const result = invoke(grep, "summary", {
1582
+ path: keepValid ? [invalid, valid] : invalid,
1583
+ pattern: "needle",
1584
+ literal: true,
1585
+ limit: keepValid ? 2 : 1,
1586
+ outputMode,
1587
+ });
1588
+ if (!keepValid) {
1589
+ await assert.rejects(result, /No matches could be displayed\.[\s\S]*Search incomplete/);
1590
+ return;
1591
+ }
1592
+ const summary = await result;
1593
+ assert.equal(summary.details?.incomplete, true);
1594
+ const shown = summary.content[0].text.split("\n\n[")[0];
1595
+ assert.equal(
1596
+ shown,
1597
+ outputMode === "files" ? "valid.txt" : "valid.txt: 1\nTotal: 1 match in 1 file",
1598
+ );
1599
+ assert.match(summary.content[0].text, /2 matches limit reached/);
1600
+ assert.match(summary.content[0].text, /Could not read .*invalid\.txt/);
1601
+ });
1602
+ }
1603
+ }
1249
1604
  } finally {
1250
1605
  await rm(directory, { recursive: true, force: true });
1251
1606
  }