@criogaid/pi-hashline-edit 0.5.0 → 0.5.1

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.
@@ -11,6 +11,8 @@ import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
11
11
  import { tmpdir } from "node:os";
12
12
  import { join } from "node:path";
13
13
  import { rgPath } from "@vscode/ripgrep";
14
+ import { initTheme } from "@earendil-works/pi-coding-agent";
15
+ import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
14
16
  import { computeLineHash } from "../core/hash.ts";
15
17
  import { makeGrepOverrideWithBackend, type GrepBackend } from "./grep-tool.ts";
16
18
  import { scopeArgs, type SearchRequest } from "./grep-search.ts";
@@ -96,8 +98,14 @@ const call = (tool: any, params: any, signal?: AbortSignal) =>
96
98
  test("grep guidance covers query syntax, search scope, and edit anchors", () => {
97
99
  const tool = makeGrepOverrideWithBackend(process.cwd(), DEFAULT_CONFIG, {});
98
100
  for (const [topic, terms] of [
99
- ["literal code", [/literal:true/, /regex punctuation/]],
100
- ["Rust regex", [/ripgrep \(Rust\)/, /lookaround/, /backreferences/]],
101
+ ["exact text", [/literal:true for exact text/, /names/, /paths/, /code snippets/]],
102
+ [
103
+ "intentional Rust regex",
104
+ [
105
+ /literal:false only for intentional ripgrep \(Rust\) regex/,
106
+ /no lookaround or backreferences/,
107
+ ],
108
+ ],
101
109
  ["pattern alternatives", [/pattern array/, /alternatives/, /\|/]],
102
110
  ["edit context", [/context:3-5/, /code to edit/]],
103
111
  [
@@ -123,8 +131,8 @@ test("grep guidance covers query syntax, search scope, and edit anchors", () =>
123
131
  assert.match(description("pattern"), /ripgrep.*Rust/);
124
132
  assert.match(description("pattern"), /not JavaScript/);
125
133
  assert.match(description("pattern"), /\^ and \$.*line boundaries/);
126
- assert.match(description("literal"), /single invalid pattern.*falls back.*literal/);
127
- assert.match(description("literal"), /invalid array fails/);
134
+ assert.match(description("literal"), /true:.*match the text exactly.*regex punctuation/);
135
+ assert.match(description("literal"), /false:.*ripgrep Rust regex.*foo\(0\).*foo0/);
128
136
  assert.match(description("path"), /omit.*working directory/);
129
137
  assert.match(description("path"), /Wildcards are not expanded.*glob/);
130
138
  assert.match(description("multiline"), /\. wildcard does not match newlines.*\\n or \(\?s\)/);
@@ -210,13 +218,14 @@ test("grep exposes nine parameters and rejects only the six removed fields", asy
210
218
  };
211
219
  for (const [key, value] of Object.entries(removed)) {
212
220
  for (const input of [value, false, null]) {
213
- await assert.rejects(call(tool, { pattern: "needle", [key]: input }), (error: Error) =>
214
- error.message.includes(`- ${key}: schema is false`),
221
+ await assert.rejects(
222
+ call(tool, { literal: true, pattern: "needle", [key]: input }),
223
+ (error: Error) => error.message.includes(`- ${key}: schema is false`),
215
224
  );
216
225
  }
217
226
  }
218
227
  await assert.rejects(
219
- call(tool, { pattern: "needle", follow: false, noIgnore: null }),
228
+ call(tool, { literal: true, pattern: "needle", follow: false, noIgnore: null }),
220
229
  (error: Error) =>
221
230
  error.message.includes("- follow: schema is false") &&
222
231
  error.message.includes("- noIgnore: schema is false"),
@@ -235,12 +244,12 @@ test("grep limit accepts only positive integers", async () => {
235
244
  assert.equal("multipleOf" in limitSchema ? limitSchema.multipleOf : undefined, 1);
236
245
  for (const limit of [0, -3, 0.5, 2.5]) {
237
246
  await assert.rejects(
238
- call(tool, { pattern: "needle", limit }),
247
+ call(tool, { literal: true, pattern: "needle", limit }),
239
248
  /Validation failed for tool "grep":\n {2}- limit: /,
240
249
  );
241
250
  }
242
251
  assert.equal(fake.calls.length, 0);
243
- await assert.doesNotReject(call(tool, { pattern: "needle", limit: 1 }));
252
+ await assert.doesNotReject(call(tool, { literal: true, pattern: "needle", limit: 1 }));
244
253
  });
245
254
  });
246
255
 
@@ -249,15 +258,15 @@ test("grep schema rejects empty search inputs and fractional context", async ()
249
258
  const fake = fakeBackend();
250
259
  const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
251
260
  const invalidArgs = [
252
- { pattern: "" },
253
- { pattern: [] },
254
- { pattern: ["ok", ""] },
255
- { pattern: "needle", path: "" },
256
- { pattern: "needle", path: [] },
257
- { pattern: "needle", glob: "" },
258
- { pattern: "needle", glob: [] },
259
- { pattern: "needle", context: 1.5 },
260
- { pattern: "needle", limit: Number.MAX_SAFE_INTEGER + 1 },
261
+ { literal: true, pattern: "" },
262
+ { literal: true, pattern: [] },
263
+ { literal: true, pattern: ["ok", ""] },
264
+ { literal: true, pattern: "needle", path: "" },
265
+ { literal: true, pattern: "needle", path: [] },
266
+ { literal: true, pattern: "needle", glob: "" },
267
+ { literal: true, pattern: "needle", glob: [] },
268
+ { literal: true, pattern: "needle", context: 1.5 },
269
+ { literal: true, pattern: "needle", limit: Number.MAX_SAFE_INTEGER + 1 },
261
270
  ];
262
271
  for (const args of invalidArgs) {
263
272
  await assert.rejects(
@@ -267,7 +276,7 @@ test("grep schema rejects empty search inputs and fractional context", async ()
267
276
  );
268
277
  }
269
278
  await assert.rejects(
270
- call(tool, { pattern: "needle", context: 1.5 }),
279
+ call(tool, { literal: true, pattern: "needle", context: 1.5 }),
271
280
  /Validation failed for tool "grep":\n {2}- context: /,
272
281
  );
273
282
  assert.equal(fake.calls.length, 0);
@@ -288,7 +297,13 @@ test("case and multiline options select only their matching ripgrep flags", asyn
288
297
  });
289
298
  assert.ok(fake.calls[0].request.matcher.includes("--ignore-case"));
290
299
  assert.ok(fake.calls[0].request.matcher.includes("--multiline"));
291
- await call(tool, { path: file, pattern: "foo", ignoreCase: false, multiline: false });
300
+ await call(tool, {
301
+ literal: true,
302
+ path: file,
303
+ pattern: "foo",
304
+ ignoreCase: false,
305
+ multiline: false,
306
+ });
292
307
  assert.ok(fake.calls.at(-1)?.request.matcher.includes("--case-sensitive"));
293
308
  assert.ok(fake.calls.at(-1)?.request.matcher.includes("--no-multiline"));
294
309
  });
@@ -298,11 +313,11 @@ test("grep points wildcard paths to glob without changing ordinary missing-path
298
313
  await withDir(async (dir) => {
299
314
  const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fakeBackend().backend);
300
315
  await assert.rejects(
301
- call(tool, { pattern: "value", path: "src/fusion-card*" }),
316
+ call(tool, { literal: true, pattern: "value", path: "src/fusion-card*" }),
302
317
  /Path not found: .*fusion-card\*.*Use an existing directory as path and a filename wildcard as glob/,
303
318
  );
304
319
  await assert.rejects(
305
- call(tool, { pattern: "value", path: "src/missing" }),
320
+ call(tool, { literal: true, pattern: "value", path: "src/missing" }),
306
321
  (error: Error) =>
307
322
  error.message.endsWith("src\\missing") || error.message.endsWith("src/missing"),
308
323
  );
@@ -326,10 +341,8 @@ test("formats parsed rg matches with full-line hash anchors", async () => {
326
341
  });
327
342
 
328
343
  const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
329
- assert.deepEqual(tool.parameters.required, ["pattern"]);
330
- const result = await call(tool, {
331
- pattern: "alpha",
332
- });
344
+ assert.deepEqual(tool.parameters.required, ["pattern", "literal"]);
345
+ const result = await call(tool, { literal: true, pattern: "alpha" });
333
346
  const output = text(result);
334
347
  assert.match(output, /a\.ts · 2 matches/);
335
348
  assert.match(output, /b\.ts · 1 match/);
@@ -371,7 +384,10 @@ test("grep rejects a file changed between a match and anchor formatting", async
371
384
  },
372
385
  };
373
386
  await assert.rejects(
374
- call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, backend), { pattern: "needle" }),
387
+ call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, backend), {
388
+ literal: true,
389
+ pattern: "needle",
390
+ }),
375
391
  /File changed during search/,
376
392
  );
377
393
  });
@@ -391,6 +407,7 @@ test("grep rejects matches missing from the current file before anchoring contex
391
407
  };
392
408
  await assert.rejects(
393
409
  call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, backend), {
410
+ literal: true,
394
411
  pattern: "needle",
395
412
  context: 2,
396
413
  }),
@@ -478,6 +495,7 @@ test("grep rejects changed context even when the matched line stays the same", a
478
495
  };
479
496
  await assert.rejects(
480
497
  call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, backend), {
498
+ literal: true,
481
499
  pattern: "needle",
482
500
  context: 1,
483
501
  }),
@@ -496,6 +514,7 @@ test("grep in a subdirectory returns a path that edits the matching file", async
496
514
  await writeFile(matchedFile, original);
497
515
  const fake = fakeBackend({ lines: [rgMatch(matchedFile, 1, original)] });
498
516
  const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
517
+ literal: true,
499
518
  pattern: "status",
500
519
  path: "src",
501
520
  });
@@ -532,7 +551,7 @@ test("context preserves logical CRLF anchors around a matched line", async () =>
532
551
  assert.equal(contextSchema.minimum, 0);
533
552
  assert.equal(contextSchema.maximum, 20);
534
553
  assert.equal(contextSchema.multipleOf, 1);
535
- const result = await call(tool, { pattern: "beta$", context: 1 });
554
+ const result = await call(tool, { pattern: "beta$", literal: false, context: 1 });
536
555
  assert.equal(
537
556
  text(result),
538
557
  [
@@ -589,6 +608,7 @@ test("passes output flags and formats files and counts", async () => {
589
608
  assert.deepEqual(fake.calls[0].request.scope.searchPaths, [a, b]);
590
609
 
591
610
  const count = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
611
+ literal: true,
592
612
  pattern: "foo",
593
613
  outputMode: "count",
594
614
  });
@@ -596,6 +616,66 @@ test("passes output flags and formats files and counts", async () => {
596
616
  });
597
617
  });
598
618
 
619
+ for (const diagnosticLineCount of [0, 80]) {
620
+ test(`grep renders ${diagnosticLineCount ? "long errors fully when expanded" : "short errors fully when collapsed"}`, async () => {
621
+ initTheme("dark");
622
+ const diagnostic = [
623
+ "rg: regex parse error:",
624
+ " (",
625
+ " ^",
626
+ ...Array.from({ length: diagnosticLineCount }, (_, index) => `diagnostic ${index}`),
627
+ "error: unclosed group",
628
+ ].join("\n");
629
+ const fake = fakeBackend({ validation: { code: 2, stderr: diagnostic } });
630
+ const tool = makeGrepOverrideWithBackend(process.cwd(), DEFAULT_CONFIG, fake.backend);
631
+ const args = { pattern: "(", literal: false };
632
+ let errorText = "";
633
+ await assert.rejects(callTool(tool, args), (error: unknown) => {
634
+ assert.ok(error instanceof Error);
635
+ errorText = error.message;
636
+ return true;
637
+ });
638
+
639
+ const render = (expanded: boolean) =>
640
+ tool
641
+ .renderResult(
642
+ { content: [{ type: "text", text: errorText }], details: undefined },
643
+ { isPartial: false, expanded },
644
+ theme,
645
+ {
646
+ args,
647
+ toolCallId: "test",
648
+ invalidate() {},
649
+ lastComponent: undefined,
650
+ state: {},
651
+ cwd: process.cwd(),
652
+ executionStarted: true,
653
+ argsComplete: true,
654
+ isPartial: false,
655
+ expanded,
656
+ showImages: false,
657
+ isError: true,
658
+ },
659
+ )
660
+ .render(120)
661
+ .join("\n");
662
+
663
+ const collapsed = render(false);
664
+ const expanded = render(true);
665
+ for (const line of errorText.split("\n")) {
666
+ assert.ok(expanded.includes(line), `Expanded error omitted ${JSON.stringify(line)}`);
667
+ if (!diagnosticLineCount) {
668
+ assert.ok(collapsed.includes(line), `Collapsed error omitted ${JSON.stringify(line)}`);
669
+ }
670
+ }
671
+ if (diagnosticLineCount) {
672
+ assert.match(collapsed, /regex parse error/);
673
+ assert.doesNotMatch(collapsed, /unclosed group/);
674
+ assert.ok(collapsed.split("\n").length < expanded.split("\n").length);
675
+ }
676
+ });
677
+ }
678
+
599
679
  test("aligns TUI line numbers across files to the widest result", () => {
600
680
  const tool = makeGrepOverrideWithBackend(process.cwd(), DEFAULT_CONFIG, fakeBackend().backend);
601
681
  const raw = [
@@ -634,6 +714,7 @@ test("limit counts matched lines and stops the fake runner", async () => {
634
714
  });
635
715
 
636
716
  const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
717
+ literal: true,
637
718
  pattern: "alpha",
638
719
  limit: 1,
639
720
  });
@@ -645,51 +726,98 @@ test("limit counts matched lines and stops the fake runner", async () => {
645
726
  });
646
727
  });
647
728
 
648
- test("auto-detects literal or regex mode and keeps explicit overrides", async () => {
649
- await withDir(async (dir) => {
650
- const valid = fakeBackend();
651
- const invalid = fakeBackend({
652
- validation: { code: 2, stderr: "regex parse error:\nerror: unclosed group" },
729
+ test("grep requires literal before invoking the backend", async () => {
730
+ const fake = fakeBackend();
731
+ const tool = makeGrepOverrideWithBackend(process.cwd(), DEFAULT_CONFIG, fake.backend);
732
+ await assert.rejects(
733
+ callTool(tool, { pattern: "needle" }),
734
+ /Validation failed for tool "grep":\n {2}- literal: /,
735
+ );
736
+ assert.equal(fake.probes.length, 0);
737
+ assert.equal(fake.calls.length, 0);
738
+ });
739
+
740
+ test("literal grep searches regex punctuation exactly without a regex probe", async () =>
741
+ withDir(async (dir) => {
742
+ const file = join(dir, "literal.txt");
743
+ await writeFile(file, "foo(0)\n");
744
+ const fake = fakeBackend({
745
+ lines: [rgMatch(file, 1, "foo(0)\n")],
746
+ validation: { code: 2, stderr: "regex probe must not run" },
653
747
  });
654
- const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, valid.backend);
655
- const fallback = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, invalid.backend);
748
+ const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
749
+ const result = await call(tool, { pattern: "foo(0)", literal: true });
750
+ assert.match(text(result), /1#[0-9A-Z]+│foo\(0\)/);
751
+ assert.deepEqual(fake.probes, []);
752
+ assert.equal(fake.calls.length, 1);
753
+ assert.ok(fake.calls[0].request.matcher.includes("--fixed-strings"));
754
+ assert.ok(fake.calls[0].request.matcher.includes("foo(0)"));
755
+ }));
656
756
 
657
- const result = await call(fallback, { pattern: "queueTool(" });
658
- assert.match(text(result), /Invalid regex; searched the pattern as literal text/);
659
- await assert.rejects(
660
- call(fallback, { pattern: ["plain", "broken("] }),
661
- /Invalid regex in compound query; automatic literal fallback is disabled/,
662
- );
663
- await call(tool, { pattern: "value.*" });
664
- await call(tool, { pattern: "plain", literal: false });
665
- await call(tool, { pattern: "value.*", literal: true });
757
+ test("regex grep accepts successful and no-match probe exit codes before searching", async () =>
758
+ withDir(async (dir) => {
759
+ const file = join(dir, "regex.txt");
760
+ await writeFile(file, "foo0\n");
761
+ for (const code of [0, 1]) {
762
+ const fake = fakeBackend({
763
+ lines: [rgMatch(file, 1, "foo0\n")],
764
+ validation: { code, stderr: "" },
765
+ });
766
+ const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
767
+ const result = await call(tool, { pattern: "foo(0)", literal: false });
768
+ assert.match(text(result), /1#[0-9A-Z]+│foo0/);
769
+ assert.deepEqual(fake.probes, [[rgPath, ["foo(0)"], false, undefined]]);
770
+ assert.equal(fake.calls.length, 1);
771
+ assert.ok(!fake.calls[0].request.matcher.includes("--fixed-strings"));
772
+ }
773
+ }));
666
774
 
667
- assert.deepEqual(
668
- [...invalid.calls, ...valid.calls].map(({ request }) =>
669
- request.matcher.includes("--fixed-strings"),
670
- ),
671
- [true, false, false, true],
672
- );
673
- assert.deepEqual(valid.probes, [
674
- [rgPath, ["value.*"], false, undefined],
675
- [rgPath, ["plain"], false, undefined],
676
- ]);
677
- assert.deepEqual(invalid.probes, [
678
- [rgPath, ["queueTool("], false, undefined],
679
- [rgPath, ["plain", "broken("], false, undefined],
680
- ]);
775
+ test("invalid regex strings and arrays fail before search with explicit literal guidance", async () =>
776
+ withDir(async (dir) => {
777
+ for (const pattern of ["streamSimple(", ["plain", "streamSimple("]]) {
778
+ const fake = fakeBackend({
779
+ validation: { code: 2, stderr: "regex parse error:\nerror: unclosed group" },
780
+ });
781
+ const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
782
+ await assert.rejects(
783
+ call(tool, { pattern, literal: false }),
784
+ /regex parse error:[\s\S]*set literal:true to search the text exactly\.$/,
785
+ );
786
+ assert.equal(fake.probes.length, 1);
787
+ assert.deepEqual(fake.probes[0][1], typeof pattern === "string" ? [pattern] : pattern);
788
+ assert.equal(fake.calls.length, 0);
789
+ }
790
+ }));
681
791
 
682
- const failed = fakeBackend({ validation: { code: 2, stderr: "Permission denied" } });
683
- await assert.rejects(
684
- call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, failed.backend), {
685
- pattern: "value.*",
686
- }),
687
- /Permission denied/,
688
- );
689
- assert.equal(failed.probes.length, 1);
690
- assert.equal(failed.calls.length, 0);
691
- });
692
- });
792
+ test("unsupported regex syntax reports the Rust dialect hint before search", async () =>
793
+ withDir(async (dir) => {
794
+ for (const pattern of ["foo(?=bar)", "(foo)\\1", ["plain", "foo(?=bar)"]]) {
795
+ const fake = fakeBackend({
796
+ validation: { code: 2, stderr: "regex parse error:\nerror: unsupported syntax" },
797
+ });
798
+ const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
799
+ await assert.rejects(
800
+ call(tool, { pattern, literal: false }),
801
+ /regex parse error:[\s\S]*no lookaround or backreferences; rewrite the pattern, or use replace/,
802
+ );
803
+ assert.equal(fake.probes.length, 1);
804
+ assert.equal(fake.calls.length, 0);
805
+ }
806
+ }));
807
+
808
+ test("regex probe failures retain diagnostics and never search", async () =>
809
+ withDir(async (dir) => {
810
+ for (const [validation, message] of [
811
+ [{ code: 2, stderr: "Permission denied" }, "Permission denied"],
812
+ [{ code: 3, stderr: "" }, "ripgrep exited with code 3"],
813
+ ] as const) {
814
+ const fake = fakeBackend({ validation });
815
+ const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
816
+ await assert.rejects(call(tool, { pattern: "value.*", literal: false }), { message });
817
+ assert.equal(fake.probes.length, 1);
818
+ assert.equal(fake.calls.length, 0);
819
+ }
820
+ }));
693
821
 
694
822
  test("uses smart-case across the entire OR pattern array", async () => {
695
823
  await withDir(async (dir) => {
@@ -699,26 +827,22 @@ test("uses smart-case across the entire OR pattern array", async () => {
699
827
  const lower = fakeBackend({ lines: [rgMatch(target, 1, "FOO alpha\n")] });
700
828
  const lowerResult = await call(
701
829
  makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, lower.backend),
702
- {
703
- pattern: ["foo", "alpha"],
704
- },
830
+ { literal: true, pattern: ["foo", "alpha"] },
705
831
  );
706
832
  assert.match(text(lowerResult), /FOO alpha/);
707
833
 
708
834
  const mixed = fakeBackend();
709
835
  const mixedResult = await call(
710
836
  makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, mixed.backend),
711
- {
712
- pattern: ["Foo", "alpha"],
713
- },
837
+ { literal: true, pattern: ["Foo", "alpha"] },
714
838
  );
715
839
  assert.equal(text(mixedResult), "No matches found");
716
840
 
717
841
  const flags = fakeBackend();
718
842
  const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, flags.backend);
719
- await call(tool, { pattern: "lower" });
720
- await call(tool, { pattern: "Upper" });
721
- await call(tool, { pattern: "foo\\S*" });
843
+ await call(tool, { literal: true, pattern: "lower" });
844
+ await call(tool, { literal: true, pattern: "Upper" });
845
+ await call(tool, { pattern: "foo\\S*", literal: false });
722
846
  assert.deepEqual(
723
847
  flags.calls.map(({ request }) => [
724
848
  request.matcher.includes("--ignore-case"),
@@ -738,7 +862,7 @@ test("OR pattern arrays are not limited by the former AND filter", async () => {
738
862
  const fake = fakeBackend();
739
863
  const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
740
864
  const patterns = Array.from({ length: 17 }, (_, index) => `pattern${index}`);
741
- assert.equal(text(await call(tool, { pattern: patterns })), "No matches found");
865
+ assert.equal(text(await call(tool, { literal: true, pattern: patterns })), "No matches found");
742
866
  assert.deepEqual(
743
867
  fake.calls[0].request.matcher.flatMap((arg, index, args) =>
744
868
  args[index - 1] === "-e" ? [arg] : [],
@@ -754,15 +878,18 @@ test("rejects empty patterns while allowing wildcard, literal, and empty-line se
754
878
  const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
755
879
 
756
880
  for (const pattern of ["", [], ["valid", ""]]) {
757
- await assert.rejects(call(tool, { pattern }), /Validation failed for tool "grep"/);
881
+ await assert.rejects(
882
+ call(tool, { pattern, literal: true }),
883
+ /Validation failed for tool "grep"/,
884
+ );
758
885
  }
759
886
  assert.equal(fake.calls.length, 0);
760
887
  for (const pattern of [".*", "^.+$", ".?"]) {
761
- assert.equal(text(await call(tool, { pattern })), "No matches found");
888
+ assert.equal(text(await call(tool, { pattern, literal: false })), "No matches found");
762
889
  }
763
890
 
764
891
  assert.equal(text(await call(tool, { pattern: ".*", literal: true })), "No matches found");
765
- assert.equal(text(await call(tool, { pattern: "^$" })), "No matches found");
892
+ assert.equal(text(await call(tool, { pattern: "^$", literal: false })), "No matches found");
766
893
  assert.equal(fake.calls.length, 5);
767
894
  assert.equal(fake.probes.length, 4);
768
895
  });
@@ -785,6 +912,7 @@ test("reports empty output and ripgrep execution failures", async () => {
785
912
  assert.equal(
786
913
  text(
787
914
  await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, empty.backend), {
915
+ literal: true,
788
916
  pattern: "missing",
789
917
  }),
790
918
  ),
@@ -802,7 +930,10 @@ test("reports empty output and ripgrep execution failures", async () => {
802
930
 
803
931
  const rejected = fakeBackend({ error: new Error("spawn failed") });
804
932
  await assert.rejects(
805
- call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, rejected.backend), { pattern: "x" }),
933
+ call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, rejected.backend), {
934
+ literal: true,
935
+ pattern: "x",
936
+ }),
806
937
  /spawn failed/,
807
938
  );
808
939
  });
@@ -816,7 +947,7 @@ test("rejects calls aborted before or during rg execution", async () => {
816
947
  await assert.rejects(
817
948
  call(
818
949
  makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, alreadyAborted.backend),
819
- { pattern: ["x", "y"] },
950
+ { literal: true, pattern: ["x", "y"] },
820
951
  first.signal,
821
952
  ),
822
953
  /Operation aborted/,
@@ -827,7 +958,7 @@ test("rejects calls aborted before or during rg execution", async () => {
827
958
  await assert.rejects(
828
959
  call(
829
960
  makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, interrupted.backend),
830
- { pattern: ["x", "y"] },
961
+ { literal: true, pattern: ["x", "y"] },
831
962
  controller.signal,
832
963
  ),
833
964
  /Operation aborted/,
@@ -876,28 +1007,71 @@ test("long candidate lines remain countable without content previews", async ()
876
1007
  });
877
1008
  });
878
1009
 
879
- test("grep rejects malformed UTF-8 and NUL bytes instead of hashing binary text", async () => {
1010
+ test("grep previews invalid UTF-8 without anchors and skips NUL hits that require snapshots", async () => {
880
1011
  await withDir(async (dir) => {
881
- const cases = [
882
- {
883
- name: "invalid.txt",
884
- bytes: Buffer.from([0x61, 0x0a, 0xc3, 0x28, 0x0a]),
885
- error: /UNSUPPORTED_ENCODING/,
886
- },
887
- { name: "nul.txt", bytes: Buffer.from([0x61, 0x00, 0x62]), error: /UNSUPPORTED_TEXT/ },
888
- ];
889
- for (const fixture of cases) {
890
- const file = join(dir, fixture.name);
891
- await writeFile(file, fixture.bytes);
892
- const fake = fakeBackend({ lines: [rgMatch(file, 1, "a\n")], paths: [file] });
893
- await assert.rejects(
894
- call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
895
- pattern: "a",
896
- path: file,
897
- }),
898
- fixture.error,
1012
+ const invalid = join(dir, "invalid.txt");
1013
+ const source = Buffer.from([0x61, 0x0a, 0xc3, 0x28, 0x0a]);
1014
+ await writeFile(invalid, source);
1015
+ const fake = fakeBackend({ lines: [rgMatch(invalid, 1, "a\n")], paths: [invalid] });
1016
+ const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
1017
+ literal: true,
1018
+ pattern: "a",
1019
+ path: invalid,
1020
+ context: 1,
1021
+ });
1022
+ assert.match(text(result), /1│a\n2│�\(/);
1023
+ assert.match(text(result), /Invalid UTF-8.*cannot be used as edit anchors/);
1024
+ assert.doesNotMatch(text(result), /\d+#|Search incomplete/);
1025
+ assert.deepEqual(await readFile(invalid), source);
1026
+
1027
+ const binary = join(dir, "nul.txt");
1028
+ const binarySource = Buffer.from("a\nb\0c");
1029
+ await writeFile(binary, binarySource);
1030
+ const binaryBackend = fakeBackend({ lines: [rgMatch(binary, 1, "a\n")], paths: [binary] });
1031
+ // Single-line files/count filtering belongs to runRgTextView and is covered by real-rg tests.
1032
+ for (const { outputMode, multiline } of [
1033
+ { outputMode: "content", multiline: false },
1034
+ { outputMode: "content", multiline: true },
1035
+ { outputMode: "files", multiline: true },
1036
+ { outputMode: "count", multiline: true },
1037
+ ] as const) {
1038
+ const skipped = await call(
1039
+ makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, binaryBackend.backend),
1040
+ { literal: true, pattern: "a", path: binary, outputMode, multiline },
899
1041
  );
900
- assert.deepEqual(await readFile(file), fixture.bytes);
1042
+ assert.equal(text(skipped), "No matches found");
1043
+ }
1044
+ assert.deepEqual(await readFile(binary), binarySource);
1045
+ });
1046
+ });
1047
+
1048
+ test("single-line files and count modes do not reread a matched file after it disappears", async () => {
1049
+ await withDir(async (dir) => {
1050
+ const file = join(dir, "listed.txt");
1051
+ for (const outputMode of ["files", "count"] as const) {
1052
+ await writeFile(file, "needle\n");
1053
+ const fake = fakeBackend({ lines: [rgMatch(file, 1, "needle\n")] });
1054
+ const backend: GrepBackend = {
1055
+ ...fake.backend,
1056
+ async search(...args) {
1057
+ // Scope resolution has succeeded; the backend can still deliver a hit already read by rg.
1058
+ await rm(file);
1059
+ return fake.backend.search(...args);
1060
+ },
1061
+ };
1062
+ const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, backend), {
1063
+ literal: true,
1064
+ pattern: "needle",
1065
+ path: file,
1066
+ outputMode,
1067
+ });
1068
+ assert.equal(
1069
+ text(result),
1070
+ outputMode === "files" ? "listed.txt" : "listed.txt: 1\nTotal: 1 match in 1 file",
1071
+ );
1072
+ assert.equal(result.details, undefined);
1073
+ assert.equal(fake.calls.length, 1);
1074
+ await assert.rejects(readFile(file), { code: "ENOENT" });
901
1075
  }
902
1076
  });
903
1077
  });
@@ -914,6 +1088,7 @@ test("partial searches retain matches and surface stderr across output modes", a
914
1088
  stderr: "unreadable.txt: Permission denied",
915
1089
  });
916
1090
  const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
1091
+ literal: true,
917
1092
  pattern: "needle",
918
1093
  outputMode,
919
1094
  limit,
@@ -929,7 +1104,10 @@ test("partial searches retain matches and surface stderr across output modes", a
929
1104
  }
930
1105
  const fake = fakeBackend({ code: 2, stderr: "Permission denied" });
931
1106
  await assert.rejects(
932
- call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), { pattern: "needle" }),
1107
+ call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
1108
+ literal: true,
1109
+ pattern: "needle",
1110
+ }),
933
1111
  /No matches confirmed.*Search incomplete/s,
934
1112
  );
935
1113
  });
@@ -952,6 +1130,7 @@ test("first-match revision read errors retain results from other files", async (
952
1130
  },
953
1131
  };
954
1132
  const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, backend), {
1133
+ literal: true,
955
1134
  pattern: "needle",
956
1135
  });
957
1136
  assert.match(text(result), /good\.txt · 1 match/);
@@ -979,7 +1158,7 @@ test("failed result reads report incomplete coverage while retaining readable fi
979
1158
  return result;
980
1159
  },
981
1160
  }),
982
- { pattern: "needle" },
1161
+ { literal: true, pattern: "needle" },
983
1162
  );
984
1163
  assert.match(text(result), /good\.txt · 1 match/);
985
1164
  assert.doesNotMatch(text(result), /gone\.txt · 1 match/);
@@ -1010,7 +1189,7 @@ test("explicit-file glob listing warnings retain confirmed matches", async () =>
1010
1189
  return { ...result, code: 2, stderr: "directory: Permission denied" };
1011
1190
  },
1012
1191
  }),
1013
- { pattern: "needle", path: file, glob: "*.txt" },
1192
+ { literal: true, pattern: "needle", path: file, glob: "*.txt" },
1014
1193
  );
1015
1194
  assert.match(text(listing), /1#[0-9A-Z]+│needle/);
1016
1195
  assert.match(text(listing), /Search incomplete/);
@@ -1029,6 +1208,7 @@ test("concurrent file reads preserve discovery order across multiple matched fil
1029
1208
  }
1030
1209
  const fake = fakeBackend({ lines: matches, paths: files });
1031
1210
  const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
1211
+ literal: true,
1032
1212
  pattern: "match",
1033
1213
  });
1034
1214
  const content = text(result);
@@ -1066,7 +1246,10 @@ test("fatal format errors stop workers from starting new file reads after reject
1066
1246
  try {
1067
1247
  const fake = fakeBackend({ lines: events });
1068
1248
  await assert.rejects(
1069
- call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), { pattern: "needle" }),
1249
+ call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
1250
+ literal: true,
1251
+ pattern: "needle",
1252
+ }),
1070
1253
  /File changed during search/,
1071
1254
  );
1072
1255
  const atRejection = started;