@criogaid/pi-hashline-edit 0.4.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.
Files changed (66) hide show
  1. package/README.md +134 -30
  2. package/package.json +3 -3
  3. package/src/core/apply.test.ts +194 -104
  4. package/src/core/apply.ts +6 -37
  5. package/src/core/errors.ts +9 -0
  6. package/src/core/hash.test.ts +8 -8
  7. package/src/core/hash.ts +5 -5
  8. package/src/{pi/replace-apply.ts → core/replace.ts} +4 -4
  9. package/src/core/text.test.ts +30 -1
  10. package/src/core/text.ts +9 -6
  11. package/src/core/types.ts +4 -7
  12. package/src/index.ts +9 -11
  13. package/src/integration/config-registration.test.ts +206 -0
  14. package/src/integration/grep-rg.test.ts +285 -153
  15. package/src/integration/large-text.test.ts +11 -12
  16. package/src/integration/replace-package.test.ts +11 -7
  17. package/src/pi/action-fusion.test.ts +251 -39
  18. package/src/pi/action-fusion.ts +113 -117
  19. package/src/pi/anchor-format.test.ts +91 -24
  20. package/src/pi/anchor-format.ts +43 -9
  21. package/src/pi/budgets.ts +7 -2
  22. package/src/pi/config.test.ts +153 -40
  23. package/src/pi/config.ts +87 -41
  24. package/src/pi/diagnostic-buffer.test.ts +58 -0
  25. package/src/pi/diagnostic-buffer.ts +59 -0
  26. package/src/pi/e2e-scenarios.test.ts +13 -11
  27. package/src/pi/edit-tool.ts +21 -157
  28. package/src/pi/error-text.ts +10 -5
  29. package/src/pi/execute.test.ts +299 -175
  30. package/src/pi/failure-context.test.ts +7 -2
  31. package/src/pi/failure-context.ts +155 -8
  32. package/src/pi/failure-path.test.ts +33 -26
  33. package/src/pi/file-commit.ts +2 -1
  34. package/src/pi/fusion-card.test.ts +26 -0
  35. package/src/pi/fusion-card.ts +102 -6
  36. package/src/pi/grep-output.ts +50 -35
  37. package/src/pi/grep-render.ts +13 -15
  38. package/src/pi/grep-scope.ts +8 -46
  39. package/src/pi/grep-search.ts +90 -63
  40. package/src/pi/grep-tool.ts +109 -121
  41. package/src/pi/grep.test.ts +597 -377
  42. package/src/pi/mutation-outcome.testing.ts +22 -0
  43. package/src/pi/mutation-result.test.ts +51 -2
  44. package/src/pi/mutation-result.ts +62 -55
  45. package/src/pi/mutation-runner.ts +16 -33
  46. package/src/pi/pi.test.ts +11 -6
  47. package/src/pi/read-tool.ts +62 -34
  48. package/src/pi/registration.test.ts +7 -7
  49. package/src/pi/render.test.ts +143 -9
  50. package/src/pi/render.ts +32 -96
  51. package/src/pi/replace-regex.ts +54 -0
  52. package/src/pi/replace-tool.ts +25 -59
  53. package/src/pi/replace-worker.ts +2 -2
  54. package/src/pi/replace.test.ts +62 -58
  55. package/src/pi/result-safety.test.ts +124 -84
  56. package/src/pi/rg-line-ranges.ts +3 -2
  57. package/src/pi/{rg-line-filter.test.ts → rg-process.test.ts} +1 -1
  58. package/src/pi/{rg-line-filter.ts → rg-process.ts} +43 -9
  59. package/src/pi/rg-text-view.ts +25 -38
  60. package/src/pi/schema.ts +2 -10
  61. package/src/pi/text-stream.test.ts +153 -0
  62. package/src/pi/text-stream.ts +33 -13
  63. package/src/pi/write-tool.ts +3 -2
  64. package/src/pi/write.test.ts +10 -9
  65. package/src/core/index.ts +0 -14
  66. package/src/pi/state.ts +0 -26
@@ -23,9 +23,11 @@ import { makeEditOverride } from "../pi/edit-tool.ts";
23
23
  import { makeReadOverride } from "../pi/read-tool.ts";
24
24
  import { makeWriteOverride } from "../pi/write-tool.ts";
25
25
  import { makeReplaceTool } from "../pi/replace-tool.ts";
26
- import { COMMON_RG_ARGS, resolveIgnoreCase, runRg } from "../pi/rg-line-filter.ts";
26
+ import { COMMON_RG_ARGS, probeRegex, resolveIgnoreCase, runRg } from "../pi/rg-process.ts";
27
+ import { runRgTextView } from "../pi/rg-text-view.ts";
27
28
  import { computeLineHash } from "../core/hash.ts";
28
29
  import { callTool } from "../pi/tool-call.testing.ts";
30
+ import { DEFAULT_CONFIG } from "../pi/config.ts";
29
31
 
30
32
  const invoke = (
31
33
  tool: any,
@@ -41,10 +43,10 @@ test("real rg emits anchored matches from a temporary directory", async () => {
41
43
  try {
42
44
  const file = join(directory, "fixture.ts");
43
45
  await writeFile(file, "needle\nother\n");
44
- const tool = makeGrepOverrideWithBackend(directory, {});
46
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
45
47
 
46
48
  for (const pattern of ["needle", ["needle"]]) {
47
- const result: any = await invoke(tool, "0", { pattern }, undefined, undefined);
49
+ const result: any = await invoke(tool, "0", { pattern, literal: true }, undefined, undefined);
48
50
  assert.match(result.content[0].text, /fixture\.ts · 1 match/);
49
51
  assert.match(result.content[0].text, /1#[0-9A-Z]+│needle/);
50
52
  }
@@ -58,7 +60,7 @@ test("real rg finds whitespace-only literal queries and OR alternatives", async
58
60
  try {
59
61
  const file = join(directory, "fixture.txt");
60
62
  await writeFile(file, "plain word\nfirst\tsecond\nboth \tforms\n");
61
- const tool = makeGrepOverrideWithBackend(directory, {});
63
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
62
64
  for (const { pattern, expected } of [
63
65
  { pattern: " ", expected: 2 },
64
66
  { pattern: "\t", expected: 2 },
@@ -90,14 +92,26 @@ test("real rg uses smart-case across OR patterns and Unicode matches", async ()
90
92
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-case-"));
91
93
  try {
92
94
  await writeFile(join(directory, "fixture.ts"), "FOO abc\nfoo BAR\nfoo bar\nK zip\nk zip\n");
93
- const tool = makeGrepOverrideWithBackend(directory, {});
94
- const lower = await invoke(tool, "0", { pattern: ["foo", "bar"] }, undefined, undefined);
95
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
96
+ const lower = await invoke(
97
+ tool,
98
+ "0",
99
+ { literal: true, pattern: ["foo", "bar"] },
100
+ undefined,
101
+ undefined,
102
+ );
95
103
  assert.match(lower.content[0].text, /fixture\.ts · 3 matches/);
96
104
  assert.match(lower.content[0].text, /│FOO abc/);
97
- const mixed = await invoke(tool, "0", { pattern: ["foo", "BAR"] }, undefined, undefined);
105
+ const mixed = await invoke(
106
+ tool,
107
+ "0",
108
+ { literal: true, pattern: ["foo", "BAR"] },
109
+ undefined,
110
+ undefined,
111
+ );
98
112
  assert.match(mixed.content[0].text, /fixture\.ts · 2 matches/);
99
113
  assert.doesNotMatch(mixed.content[0].text, /│FOO abc/);
100
- const unicode = await invoke(tool, "0", { pattern: "k" }, undefined, undefined);
114
+ const unicode = await invoke(tool, "0", { literal: true, pattern: "k" }, undefined, undefined);
101
115
  assert.match(unicode.content[0].text, /fixture\.ts · 2 matches/);
102
116
  assert.match(unicode.content[0].text, /│K zip/);
103
117
  } finally {
@@ -110,11 +124,11 @@ test("explicit ignoreCase overrides smart-case for regex and literal queries", a
110
124
  try {
111
125
  const file = join(directory, "fixture.txt");
112
126
  await writeFile(file, "FOO\nfoo\n");
113
- const tool = makeGrepOverrideWithBackend(directory, {});
127
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
114
128
  const sensitive = await invoke(
115
129
  tool,
116
130
  "0",
117
- { pattern: "foo", path: file, ignoreCase: false },
131
+ { literal: true, pattern: "foo", path: file, ignoreCase: false },
118
132
  undefined,
119
133
  undefined,
120
134
  );
@@ -202,7 +216,7 @@ test("real rg accepts Rust regex syntax and rejects unsupported lookarounds", as
202
216
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-regex-engine-"));
203
217
  try {
204
218
  await writeFile(join(directory, "fixture.ts"), "FOO abc\nfoo BAR\nfoo bar\n");
205
- const tool = makeGrepOverrideWithBackend(directory, {});
219
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
206
220
  const result = await invoke(
207
221
  tool,
208
222
  "0",
@@ -231,13 +245,11 @@ test("owned rg processes ignore RIPGREP_CONFIG_PATH", async () => {
231
245
  );
232
246
  await writeFile(join(directory, "fixture.ts"), "FOO\nfoo\nbar\n");
233
247
  process.env.RIPGREP_CONFIG_PATH = config;
234
- const tool = makeGrepOverrideWithBackend(directory, {});
248
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
235
249
  const result: any = await invoke(
236
250
  tool,
237
251
  "0",
238
- {
239
- pattern: ["^foo$", "foo"],
240
- },
252
+ { literal: false, pattern: ["^foo$", "foo"] },
241
253
  undefined,
242
254
  undefined,
243
255
  );
@@ -264,57 +276,62 @@ test("owned rg processes ignore RIPGREP_CONFIG_PATH", async () => {
264
276
  }
265
277
  });
266
278
 
267
- test("real rg validates its own regex syntax and limits automatic literal fallback", async () => {
279
+ test("real rg requires an explicit mode and separates exact text from regex", async () => {
268
280
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-regex-"));
269
281
  try {
270
- await writeFile(join(directory, "fixture.ts"), "FOO\nfoo\nqueueTool(\nfoo(?=bar)\n");
271
- const tool = makeGrepOverrideWithBackend(directory, {});
282
+ await writeFile(
283
+ join(directory, "fixture.ts"),
284
+ "FOO\nfoo\nfoo0\nfoo(0)\nqueueTool(\nfoo(?=bar)\n",
285
+ );
286
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
287
+ await assert.rejects(
288
+ invoke(tool, "missing-mode", { pattern: "foo" }),
289
+ /Validation failed for tool "grep":\n {2}- literal: /,
290
+ );
272
291
  for (const pattern of ["(?i)^foo$", "(?P<name>foo)$"]) {
273
- const result: any = await invoke(tool, "0", { pattern }, undefined, undefined);
292
+ const result = await invoke(tool, "regex", { pattern, literal: false });
274
293
  assert.match(result.content[0].text, /│foo/);
275
- assert.doesNotMatch(result.content[0].text, /Invalid regex/);
276
294
  }
277
- for (const pattern of ["queueTool(", "foo(?=bar)"]) {
278
- const result: any = await invoke(tool, "0", { pattern }, undefined, undefined);
295
+ const regex = await invoke(tool, "regex-punctuation", { pattern: "foo(0)", literal: false });
296
+ assert.match(regex.content[0].text, /│foo0/);
297
+ assert.doesNotMatch(regex.content[0].text, /│foo\(0\)/);
298
+ for (const pattern of ["foo(0)", "queueTool(", "foo(?=bar)"]) {
299
+ const result = await invoke(tool, "literal", { pattern, literal: true });
279
300
  assert.ok(result.content[0].text.includes(`│${pattern}`));
280
- assert.match(result.content[0].text, /Invalid regex; searched the pattern as literal text/);
281
- await assert.rejects(
282
- invoke(tool, "0", { pattern, literal: false }, undefined, undefined),
283
- /regex parse error/,
284
- );
285
301
  }
286
- const missing: any = await invoke(tool, "0", { pattern: "missing(" }, undefined, undefined);
287
- assert.match(missing.content[0].text, /No matches found\n\n\[Invalid regex/);
288
- const explicit: any = await invoke(
289
- tool,
290
- "0",
291
- { pattern: "queueTool(", literal: true },
292
- undefined,
293
- undefined,
302
+ const missing = await invoke(tool, "no-match", { pattern: "missing(", literal: true });
303
+ assert.equal(missing.content[0].text, "No matches found");
304
+ await assert.rejects(
305
+ invoke(tool, "invalid-regex", { pattern: "queueTool(", literal: false }),
306
+ /regex parse error[\s\S]*set literal:true to search the text exactly/,
294
307
  );
295
- assert.match(explicit.content[0].text, /│queueTool\(/);
296
- assert.doesNotMatch(explicit.content[0].text, /Invalid regex/);
297
308
  await assert.rejects(
298
- invoke(tool, "0", { pattern: ["queueTool(", "\\bfoo\\b"] }, undefined, undefined),
299
- /Invalid regex in compound query/,
309
+ invoke(tool, "invalid-array", { pattern: ["queueTool(", "\\bfoo\\b"], literal: false }),
310
+ /regex parse error[\s\S]*set literal:true to search the text exactly/,
300
311
  );
301
312
  } finally {
302
313
  await rm(directory, { recursive: true, force: true });
303
314
  }
304
315
  });
305
316
 
306
- test("real rg keeps CRLF line-end anchors with fixed OR matching", async () => {
317
+ test("real rg keeps CRLF line-end anchors with regex OR matching", async () => {
307
318
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-crlf-"));
308
319
  try {
309
320
  await writeFile(join(directory, "fixture.ts"), "alpha beta drop\r\nalpha beta\r\nalpha only");
310
- const tool = makeGrepOverrideWithBackend(directory, {});
311
- const ending = await invoke(tool, "0", { pattern: "beta$" }, undefined, undefined);
321
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
322
+ const ending = await invoke(
323
+ tool,
324
+ "0",
325
+ { pattern: "beta$", literal: false },
326
+ undefined,
327
+ undefined,
328
+ );
312
329
  assert.match(ending.content[0].text, /fixture\.ts · 1 match/);
313
330
  assert.match(ending.content[0].text, /2#[0-9A-Z]+│alpha beta$/);
314
331
  const alternatives = await invoke(
315
332
  tool,
316
333
  "0",
317
- { pattern: ["beta$", "alpha only"] },
334
+ { pattern: ["beta$", "alpha only"], literal: false },
318
335
  undefined,
319
336
  undefined,
320
337
  );
@@ -329,7 +346,7 @@ test("real rg finds a late OR match and respects the line limit", async () => {
329
346
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-limit-"));
330
347
  try {
331
348
  await writeFile(join(directory, "fixture.ts"), "foo skip\n".repeat(4096) + "foo keep\n");
332
- const tool = makeGrepOverrideWithBackend(directory, {});
349
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
333
350
  const result = await invoke(
334
351
  tool,
335
352
  "0",
@@ -379,6 +396,10 @@ test("shared rg runner stops and cleans up after limits, callback failures, and
379
396
  runRg(join(directory, "missing-rg"), [], undefined, () => true),
380
397
  /Failed to run ripgrep/,
381
398
  );
399
+ await assert.rejects(
400
+ probeRegex(join(directory, "missing-rg"), ["needle"], false),
401
+ /^Error: Failed to run ripgrep: /,
402
+ );
382
403
  } finally {
383
404
  await rm(directory, { recursive: true, force: true });
384
405
  }
@@ -390,13 +411,19 @@ test("directory searches respect ignores but explicit files and hidden files rem
390
411
  await writeFile(join(directory, ".ignore"), "ignored.txt\n");
391
412
  const ignored = join(directory, "ignored.txt");
392
413
  await writeFile(ignored, "needle\n");
393
- const tool = makeGrepOverrideWithBackend(directory, {});
394
- const normal = await invoke(tool, "0", { pattern: "needle" }, undefined, undefined);
414
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
415
+ const normal = await invoke(
416
+ tool,
417
+ "0",
418
+ { literal: true, pattern: "needle" },
419
+ undefined,
420
+ undefined,
421
+ );
395
422
  assert.equal(normal.content[0].text, "No matches found");
396
423
  const explicit = await invoke(
397
424
  tool,
398
425
  "0",
399
- { pattern: "needle", path: ignored },
426
+ { literal: true, pattern: "needle", path: ignored },
400
427
  undefined,
401
428
  undefined,
402
429
  );
@@ -404,13 +431,19 @@ test("directory searches respect ignores but explicit files and hidden files rem
404
431
  const excluded = await invoke(
405
432
  tool,
406
433
  "0",
407
- { pattern: "needle", path: ignored, glob: "!ignored.txt" },
434
+ { literal: true, pattern: "needle", path: ignored, glob: "!ignored.txt" },
408
435
  undefined,
409
436
  undefined,
410
437
  );
411
438
  assert.equal(excluded.content[0].text, "No matches found");
412
439
  await writeFile(join(directory, ".hidden.txt"), "needle\n");
413
- const hidden = await invoke(tool, "0", { pattern: "needle" }, undefined, undefined);
440
+ const hidden = await invoke(
441
+ tool,
442
+ "0",
443
+ { literal: true, pattern: "needle" },
444
+ undefined,
445
+ undefined,
446
+ );
414
447
  assert.match(hidden.content[0].text, /\.hidden\.txt · 1 match/);
415
448
  assert.doesNotMatch(hidden.content[0].text, /ignored\.txt · 1 match/);
416
449
  } finally {
@@ -426,11 +459,12 @@ test("explicit file paths obey ordered glob filters without inheriting ignore ru
426
459
  const ignored = join(directory, "ignored.ts");
427
460
  await writeFile(join(directory, ".ignore"), "ignored.ts\n");
428
461
  await Promise.all([kept, excluded, ignored].map((path) => writeFile(path, "needle\n")));
429
- const tool = makeGrepOverrideWithBackend(directory, {});
462
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
430
463
  const result: any = await invoke(
431
464
  tool,
432
465
  "0",
433
466
  {
467
+ literal: true,
434
468
  pattern: "needle",
435
469
  path: [kept, excluded, ignored],
436
470
  glob: ["*.ts", "!**/*.test.ts"],
@@ -445,11 +479,7 @@ test("explicit file paths obey ordered glob filters without inheriting ignore ru
445
479
  const none: any = await invoke(
446
480
  tool,
447
481
  "0",
448
- {
449
- pattern: "needle",
450
- path: excluded,
451
- glob: "!**/*.test.ts",
452
- },
482
+ { literal: true, pattern: "needle", path: excluded, glob: "!**/*.test.ts" },
453
483
  undefined,
454
484
  undefined,
455
485
  );
@@ -478,13 +508,19 @@ test("directory traversal skips symlinks but explicitly named linked files remai
478
508
  return t.skip("symbolic link creation unavailable");
479
509
  throw error;
480
510
  }
481
- const grep = makeGrepOverrideWithBackend(root, {});
482
- const hidden: any = await invoke(grep, "0", { pattern: "needle" }, undefined, undefined);
511
+ const grep = makeGrepOverrideWithBackend(root, DEFAULT_CONFIG, {});
512
+ const hidden: any = await invoke(
513
+ grep,
514
+ "0",
515
+ { literal: true, pattern: "needle" },
516
+ undefined,
517
+ undefined,
518
+ );
483
519
  assert.equal(hidden.content[0].text, "No matches found");
484
520
  const found: any = await invoke(
485
521
  grep,
486
522
  "0",
487
- { pattern: "needle", path: join(link, "linked.txt") },
523
+ { literal: true, pattern: "needle", path: join(link, "linked.txt") },
488
524
  undefined,
489
525
  undefined,
490
526
  );
@@ -493,7 +529,7 @@ test("directory traversal skips symlinks but explicitly named linked files remai
493
529
  assert.equal(resultPath.replace(/\\/g, "/"), "link/linked.txt");
494
530
  assert.equal(found.content[0].text.match(/ · 1 match/g)?.length, 1);
495
531
  const anchor = anchored.slice(0, anchored.indexOf("│"));
496
- const edit: any = makeEditOverride(root);
532
+ const edit: any = makeEditOverride(root, DEFAULT_CONFIG);
497
533
  await invoke(
498
534
  edit,
499
535
  "0",
@@ -514,7 +550,7 @@ test("real rg reports unsupported lookarounds and backreferences under Rust rege
514
550
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-rust-boundary-"));
515
551
  try {
516
552
  await writeFile(join(directory, "fixture.txt"), "foobar\nfoofoo\n");
517
- const tool = makeGrepOverrideWithBackend(directory, {});
553
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
518
554
  const valid = await invoke(
519
555
  tool,
520
556
  "0",
@@ -523,22 +559,21 @@ test("real rg reports unsupported lookarounds and backreferences under Rust rege
523
559
  undefined,
524
560
  );
525
561
  assert.match(valid.content[0].text, /fixture\.txt · 2 matches/);
526
- for (const pattern of ["foo(?=bar)", "(foo)\\1"]) {
562
+ for (const pattern of ["foo(?=bar)", "(foo)\\1", ["plain", "foo(?=bar)"]]) {
527
563
  await assert.rejects(
528
564
  invoke(tool, "0", { pattern, literal: false }, undefined, undefined),
529
565
  /regex parse error[\s\S]*no lookaround or backreferences; rewrite the pattern, or use replace/,
530
566
  );
531
567
  }
532
- const fallback = await invoke(tool, "0", { pattern: "foo(?=bar)" }, undefined, undefined);
533
- assert.match(
534
- fallback.content[0].text,
535
- /No matches found.*Invalid regex; searched the pattern as literal text; ripgrep's Rust regex has no lookaround or backreferences/s,
536
- );
537
- // A parse error without other-dialect syntax gets no dialect hint.
568
+ const literal = await invoke(tool, "0", { pattern: "foo(?=bar)", literal: true });
569
+ assert.equal(literal.content[0].text, "No matches found");
570
+ // A parse error without other-dialect syntax requests an exact-text search.
538
571
  await assert.rejects(
539
572
  invoke(tool, "0", { pattern: "foo(", literal: false }, undefined, undefined),
540
573
  (error: Error) =>
541
- /regex parse error/.test(error.message) && !/lookaround/.test(error.message),
574
+ /regex parse error/.test(error.message) &&
575
+ /set literal:true to search the text exactly/.test(error.message) &&
576
+ !/lookaround/.test(error.message),
542
577
  );
543
578
  } finally {
544
579
  await rm(directory, { recursive: true, force: true });
@@ -549,8 +584,14 @@ test("line-based grep uses context for display but never matches across CRLF", a
549
584
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-lines-"));
550
585
  try {
551
586
  await writeFile(join(directory, "fixture.txt"), "alpha\r\nbeta\r\ngamma\r\n");
552
- const tool = makeGrepOverrideWithBackend(directory, {});
553
- const context = await invoke(tool, "0", { pattern: "alpha", context: 1 }, undefined, undefined);
587
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
588
+ const context = await invoke(
589
+ tool,
590
+ "0",
591
+ { literal: true, pattern: "alpha", context: 1 },
592
+ undefined,
593
+ undefined,
594
+ );
554
595
  assert.match(context.content[0].text, /fixture\.txt · 1 match/);
555
596
  assert.match(context.content[0].text, /1#[0-9A-Z]+│alpha/);
556
597
  assert.match(context.content[0].text, /2#[0-9A-Z]+│beta/);
@@ -566,7 +607,7 @@ test("line-based grep uses context for display but never matches across CRLF", a
566
607
  const limited = await invoke(
567
608
  tool,
568
609
  "0",
569
- { pattern: ["alpha", "beta"], outputMode: "count", limit: 1 },
610
+ { literal: true, pattern: ["alpha", "beta"], outputMode: "count", limit: 1 },
570
611
  undefined,
571
612
  undefined,
572
613
  );
@@ -582,7 +623,7 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
582
623
  try {
583
624
  const file = join(directory, "fixture.txt");
584
625
  await writeFile(file, "alpha\r\nbeta\r\ngamma\r\n");
585
- const tool = makeGrepOverrideWithBackend(directory, {});
626
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
586
627
  const regex = await invoke(
587
628
  tool,
588
629
  "0",
@@ -612,7 +653,14 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
612
653
  const bounded = await invoke(
613
654
  tool,
614
655
  "0",
615
- { pattern: "(?s)alpha.*gamma", path: file, multiline: true, outputMode: "count", limit: 2 },
656
+ {
657
+ pattern: "(?s)alpha.*gamma",
658
+ literal: false,
659
+ path: file,
660
+ multiline: true,
661
+ outputMode: "count",
662
+ limit: 2,
663
+ },
616
664
  undefined,
617
665
  undefined,
618
666
  );
@@ -622,7 +670,14 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
622
670
  const files = await invoke(
623
671
  tool,
624
672
  "0",
625
- { pattern: "alpha\\nbeta", path: file, multiline: true, outputMode: "files", limit: 1 },
673
+ {
674
+ pattern: "alpha\\nbeta",
675
+ literal: false,
676
+ path: file,
677
+ multiline: true,
678
+ outputMode: "files",
679
+ limit: 1,
680
+ },
626
681
  undefined,
627
682
  undefined,
628
683
  );
@@ -635,7 +690,7 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
635
690
  undefined,
636
691
  );
637
692
  assert.match(boundary.content[0].text, /fixture\.txt · 1 match/);
638
- const edit: any = makeEditOverride(directory);
693
+ const edit: any = makeEditOverride(directory, DEFAULT_CONFIG);
639
694
  const anchor = regex.content[0].text.match(/(2#[0-9A-Z]+)│beta/)?.[1];
640
695
  assert.ok(anchor);
641
696
  await invoke(
@@ -656,7 +711,7 @@ test("multiline grep preserves BOM and standalone CR while normalizing CRLF", as
656
711
  try {
657
712
  const file = join(directory, "fixture.txt");
658
713
  await writeFile(file, "\uFEFFalpha\rsolo\r\nbeta\r\n");
659
- const tool = makeGrepOverrideWithBackend(directory, {});
714
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
660
715
  const result = await invoke(
661
716
  tool,
662
717
  "0",
@@ -685,7 +740,7 @@ test("multiline grep anchors empty lines and line-start zero-width matches", asy
685
740
  try {
686
741
  const file = join(directory, "fixture.txt");
687
742
  await writeFile(file, "alpha\r\n\r\nbeta\r\n");
688
- const tool = makeGrepOverrideWithBackend(directory, {});
743
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
689
744
  const span = await invoke(
690
745
  tool,
691
746
  "0",
@@ -714,7 +769,7 @@ test("multiline zero-width EOF stays on the last existing physical line", async
714
769
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-eof-"));
715
770
  try {
716
771
  const file = join(directory, "fixture.txt");
717
- const tool = makeGrepOverrideWithBackend(directory, {});
772
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
718
773
  for (const content of ["last line", "last line\n"]) {
719
774
  await writeFile(file, content);
720
775
  const result = await invoke(
@@ -741,10 +796,10 @@ test("multiline grep detects changed files and propagates cancellation", async (
741
796
  try {
742
797
  const file = join(directory, "fixture.txt");
743
798
  await writeFile(file, "alpha\nbeta\n");
744
- const changing = makeGrepOverrideWithBackend(directory, {
745
- async runRg(path, args, signal, onLine) {
746
- const result = await runRg(path, args, signal, onLine);
747
- if (args.includes("--json")) await writeFile(file, "alpha\nchanged\n");
799
+ const changing = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
800
+ async search(path, request, signal, onLine) {
801
+ const result = await runRgTextView(path, request, signal, onLine);
802
+ await writeFile(file, "alpha\nchanged\n");
748
803
  return result;
749
804
  },
750
805
  });
@@ -760,10 +815,10 @@ test("multiline grep detects changed files and propagates cancellation", async (
760
815
  );
761
816
  await writeFile(file, "alpha\nbeta\n");
762
817
  const controller = new AbortController();
763
- const cancelling = makeGrepOverrideWithBackend(directory, {
764
- runRg(path, args, signal, onLine) {
765
- if (args.includes("--json")) controller.abort();
766
- return runRg(path, args, signal, onLine);
818
+ const cancelling = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
819
+ search(path, request, signal, onLine) {
820
+ controller.abort();
821
+ return runRgTextView(path, request, signal, onLine);
767
822
  },
768
823
  });
769
824
  await assert.rejects(
@@ -788,8 +843,8 @@ test("files and count modes aggregate only matching lines inside the limit", asy
788
843
  const second = join(directory, "b.txt");
789
844
  await writeFile(first, "needle needle\nneedle\n");
790
845
  await writeFile(second, "needle\n");
791
- const tool = makeGrepOverrideWithBackend(directory, {});
792
- const query = { pattern: "needle", path: [first, second], limit: 1 };
846
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
847
+ const query = { literal: true, pattern: "needle", path: [first, second], limit: 1 };
793
848
  const files = await invoke(
794
849
  tool,
795
850
  "files",
@@ -833,28 +888,34 @@ test("default grep rejects file changes and propagates cancellation", async () =
833
888
  try {
834
889
  const file = join(directory, "fixture.txt");
835
890
  await writeFile(file, "foo bar\n");
836
- const changing = makeGrepOverrideWithBackend(directory, {
837
- async runRg(path, args, signal, onLine) {
838
- const result = await runRg(path, args, signal, onLine);
839
- if (args.includes("--json")) await writeFile(file, "changed content\n");
891
+ const changing = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
892
+ async search(path, request, signal, onLine) {
893
+ const result = await runRgTextView(path, request, signal, onLine);
894
+ await writeFile(file, "changed content\n");
840
895
  return result;
841
896
  },
842
897
  });
843
898
  await assert.rejects(
844
- invoke(changing, "0", { pattern: "foo", path: file }, undefined, undefined),
899
+ invoke(changing, "0", { literal: true, pattern: "foo", path: file }, undefined, undefined),
845
900
  /File changed during search/,
846
901
  );
847
902
 
848
903
  await writeFile(file, "foo bar\n");
849
904
  const controller = new AbortController();
850
- const cancelling = makeGrepOverrideWithBackend(directory, {
851
- runRg(path, args, signal, onLine) {
852
- if (args.includes("--json")) controller.abort();
853
- return runRg(path, args, signal, onLine);
905
+ const cancelling = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
906
+ search(path, request, signal, onLine) {
907
+ controller.abort();
908
+ return runRgTextView(path, request, signal, onLine);
854
909
  },
855
910
  });
856
911
  await assert.rejects(
857
- invoke(cancelling, "0", { pattern: "foo", path: file }, controller.signal, undefined),
912
+ invoke(
913
+ cancelling,
914
+ "0",
915
+ { literal: true, pattern: "foo", path: file },
916
+ controller.signal,
917
+ undefined,
918
+ ),
858
919
  /Operation aborted/,
859
920
  );
860
921
  } finally {
@@ -866,7 +927,7 @@ test("owned rg rejects an oversized JSONL record", async () => {
866
927
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-record-limit-"));
867
928
  try {
868
929
  await writeFile(join(directory, "large.txt"), `needle${"x".repeat(17 * 1024 * 1024)}\n`);
869
- const tool = makeGrepOverrideWithBackend(directory, {});
930
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
870
931
  await assert.rejects(
871
932
  invoke(tool, "0", { pattern: "needle", literal: true }, undefined, undefined),
872
933
  /record exceeds 16 MiB/,
@@ -880,7 +941,7 @@ test("real rg accepts wildcard-only regexes and preserves limits and literal mod
880
941
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-wildcard-"));
881
942
  try {
882
943
  await writeFile(join(directory, "fixture.txt"), "first\n\n.*\n");
883
- const tool = makeGrepOverrideWithBackend(directory, {});
944
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
884
945
  for (const [pattern, expected] of [
885
946
  [".*", 3],
886
947
  ["^.+$", 2],
@@ -889,7 +950,7 @@ test("real rg accepts wildcard-only regexes and preserves limits and literal mod
889
950
  const result: any = await invoke(
890
951
  tool,
891
952
  "count",
892
- { pattern, outputMode: "count" },
953
+ { pattern, literal: false, outputMode: "count" },
893
954
  undefined,
894
955
  undefined,
895
956
  );
@@ -898,7 +959,7 @@ test("real rg accepts wildcard-only regexes and preserves limits and literal mod
898
959
  const limited: any = await invoke(
899
960
  tool,
900
961
  "limited",
901
- { pattern: ".*", limit: 1 },
962
+ { pattern: ".*", literal: false, limit: 1 },
902
963
  undefined,
903
964
  undefined,
904
965
  );
@@ -937,8 +998,9 @@ test("tools share physical lines and anchors across text representations", async
937
998
  ]) {
938
999
  await call(makeWriteOverride(directory), { path: file, content: before });
939
1000
  assert.equal(await readFile(file, "utf8"), before);
940
- const read = await call(makeReadOverride(directory), { path: file });
941
- const grep = await call(makeGrepOverrideWithBackend(directory, {}), {
1001
+ const read = await call(makeReadOverride(directory, DEFAULT_CONFIG), { path: file });
1002
+ const grep = await call(makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {}), {
1003
+ literal: true,
942
1004
  path: file,
943
1005
  pattern: "old",
944
1006
  context: 2,
@@ -946,7 +1008,7 @@ test("tools share physical lines and anchors across text representations", async
946
1008
  assert.deepEqual(rows(grep), rows(read));
947
1009
  if (before.includes("a\rb")) assert.match(rows(read)[0], /│a␍b$/);
948
1010
  const anchor = rows(grep)[1].split("│")[0];
949
- const edit = await call(makeEditOverride(directory), {
1011
+ const edit = await call(makeEditOverride(directory, DEFAULT_CONFIG), {
950
1012
  path: file,
951
1013
  edits: [{ op: "replace", anchor, body: ["new"] }],
952
1014
  });
@@ -956,15 +1018,15 @@ test("tools share physical lines and anchors across text representations", async
956
1018
  assert.match(edit.details.diff, /^\+2 new/m);
957
1019
  assert.ok(!edit.details.diff.includes("\r"));
958
1020
  if (before.includes("a\rb")) assert.ok(edit.details.patch.includes(" a\rb\n"));
959
- const editedRead = await call(makeReadOverride(directory), { path: file });
1021
+ const editedRead = await call(makeReadOverride(directory, DEFAULT_CONFIG), { path: file });
960
1022
  assert.equal(rows(edit)[0], rows(editedRead)[1].split("│")[0]);
961
- const replaced = await call(makeReplaceTool(directory), {
1023
+ const replaced = await call(makeReplaceTool(directory, DEFAULT_CONFIG), {
962
1024
  path: file,
963
1025
  replacements: [{ find: "new", replace: "next" }],
964
1026
  });
965
1027
  assert.equal(await readFile(file, "utf8"), before.replace("old", "next"));
966
1028
  assert.equal(replaced.details.firstChangedLine, 2);
967
- const replacedRead = await call(makeReadOverride(directory), { path: file });
1029
+ const replacedRead = await call(makeReadOverride(directory, DEFAULT_CONFIG), { path: file });
968
1030
  assert.equal(rows(replaced)[0], rows(replacedRead)[1].split("│")[0]);
969
1031
  }
970
1032
  } finally {
@@ -978,9 +1040,9 @@ test("long-line previews preserve full-line anchors across literal and Rust rege
978
1040
  const long = "😀界".repeat(220) + "NEEDLE" + "tail".repeat(200);
979
1041
  const context = "x" + "😀".repeat(400);
980
1042
  await writeFile(join(directory, "long.txt"), `${long}\r\nfollow\r\n${context}\r\n`);
981
- const tool = makeGrepOverrideWithBackend(directory, {});
1043
+ const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
982
1044
  for (const params of [
983
- { pattern: "NEEDLE" },
1045
+ { literal: true, pattern: "NEEDLE" },
984
1046
  { pattern: ["NEEDLE", "tail"], literal: true },
985
1047
  { pattern: "NEEDLE(?:tail)+", literal: false },
986
1048
  ]) {
@@ -993,10 +1055,10 @@ test("long-line previews preserve full-line anchors across literal and Rust rege
993
1055
  );
994
1056
  const output = result.content[0].text;
995
1057
  const row = output.split("\n").find((line: string) => line.startsWith("1#"));
996
- assert.ok(row.startsWith(`1#${computeLineHash(1, long)}│[partial, columns `));
1058
+ assert.ok(row.startsWith(`1#${computeLineHash(1, long, 4)}│[partial, columns `));
997
1059
  assert.ok(row.includes("NEEDLE"));
998
1060
  assert.equal(Buffer.from(row).toString("utf8"), row);
999
- assert.ok(output.includes(`3#${computeLineHash(3, context)}│[partial, columns 1-499]`));
1061
+ assert.ok(output.includes(`3#${computeLineHash(3, context, 4)}│[partial, columns 1-499]`));
1000
1062
  assert.match(output, /anchors hash full lines/);
1001
1063
  }
1002
1064
  } finally {
@@ -1010,8 +1072,8 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1010
1072
  const path = join(directory, "mixed.txt");
1011
1073
  const before = "\uFEFFhead\r\nalpha\r\nbeta\nstand\rCR\r\r\nlast";
1012
1074
  const write = makeWriteOverride(directory);
1013
- const read = makeReadOverride(directory);
1014
- const grep = makeGrepOverrideWithBackend(directory, {});
1075
+ const read = makeReadOverride(directory, DEFAULT_CONFIG);
1076
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1015
1077
  await invoke(write, "write", { path, content: before }, undefined, undefined, {
1016
1078
  cwd: directory,
1017
1079
  } as ExtensionContext);
@@ -1034,7 +1096,7 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1034
1096
  const contextual = await invoke(
1035
1097
  grep,
1036
1098
  "grep",
1037
- { path, pattern: "alpha", context: 1 },
1099
+ { literal: true, path, pattern: "alpha", context: 1 },
1038
1100
  undefined,
1039
1101
  undefined,
1040
1102
  );
@@ -1066,7 +1128,7 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1066
1128
  } as ExtensionContext);
1067
1129
  if (mode === "edit") {
1068
1130
  await invoke(
1069
- makeEditOverride(directory),
1131
+ makeEditOverride(directory, DEFAULT_CONFIG),
1070
1132
  "edit",
1071
1133
  {
1072
1134
  path,
@@ -1085,7 +1147,7 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1085
1147
  );
1086
1148
  } else {
1087
1149
  await invoke(
1088
- makeReplaceTool(directory),
1150
+ makeReplaceTool(directory, DEFAULT_CONFIG),
1089
1151
  "replace",
1090
1152
  {
1091
1153
  path,
@@ -1127,12 +1189,19 @@ test("visible source escapes remain readable and searchable while real CRLF stay
1127
1189
  undefined,
1128
1190
  { cwd: directory } as ExtensionContext,
1129
1191
  );
1130
- const read = await invoke(makeReadOverride(directory), "read", { path }, undefined, undefined, {
1131
- cwd: directory,
1132
- } as ExtensionContext);
1192
+ const read = await invoke(
1193
+ makeReadOverride(directory, DEFAULT_CONFIG),
1194
+ "read",
1195
+ { path },
1196
+ undefined,
1197
+ undefined,
1198
+ {
1199
+ cwd: directory,
1200
+ } as ExtensionContext,
1201
+ );
1133
1202
  assert.ok(read.content[0].type === "text");
1134
1203
  assert.ok(read.content[0].text.includes(String.raw`const eol = "\r\n";`));
1135
- const grep = makeGrepOverrideWithBackend(directory, {});
1204
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1136
1205
  for (const query of [
1137
1206
  { pattern: String.raw`\r\n`, literal: true },
1138
1207
  { pattern: String.raw`\\r\\n`, literal: false },
@@ -1140,7 +1209,7 @@ test("visible source escapes remain readable and searchable while real CRLF stay
1140
1209
  const result = await invoke(grep, "grep", { path, ...query }, undefined, undefined);
1141
1210
  assert.ok(result.content[0].text.includes(String.raw`const eol = "\r\n";`));
1142
1211
  }
1143
- const replace = makeReplaceTool(directory);
1212
+ const replace = makeReplaceTool(directory, DEFAULT_CONFIG);
1144
1213
  await invoke(
1145
1214
  replace,
1146
1215
  "literal",
@@ -1164,14 +1233,14 @@ test("visible source escapes remain readable and searchable while real CRLF stay
1164
1233
  }
1165
1234
  });
1166
1235
 
1167
- test("normalized grep batches retain original paths, literal option markers, and decode diagnostics", async () => {
1236
+ test("normalized grep batches retain original paths and ignore unmatched invalid UTF-8", async () => {
1168
1237
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-snapshots-"));
1169
1238
  try {
1170
1239
  await Promise.all(
1171
1240
  Array.from({ length: 70 }, (_, i) => writeFile(join(directory, `file ${i}.txt`), "--\r\n")),
1172
1241
  );
1173
1242
  await writeFile(join(directory, "invalid.txt"), Buffer.from([0xc3, 0x28]));
1174
- const grep = makeGrepOverrideWithBackend(directory, {});
1243
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1175
1244
  const result = await invoke(
1176
1245
  grep,
1177
1246
  "grep",
@@ -1180,59 +1249,122 @@ test("normalized grep batches retain original paths, literal option markers, and
1180
1249
  undefined,
1181
1250
  );
1182
1251
  assert.equal((result.content[0].text.match(/file \d+\.txt: 1/g) ?? []).length, 70);
1183
- assert.match(result.content[0].text, /Search incomplete/);
1184
- assert.match(result.content[0].text, /invalid\.txt/);
1185
- assert.match(result.content[0].text, /UNSUPPORTED_ENCODING/);
1252
+ assert.doesNotMatch(
1253
+ result.content[0].text,
1254
+ /Search incomplete|invalid\.txt|UNSUPPORTED_ENCODING/,
1255
+ );
1186
1256
  assert.doesNotMatch(result.content[0].text, /hashline-grep-/);
1187
1257
  } finally {
1188
1258
  await rm(directory, { recursive: true, force: true });
1189
1259
  }
1190
1260
  });
1191
1261
 
1192
- test("NUL files are rejected on a confirmed hit, not reported as no match", async () => {
1262
+ test("NUL files are silently skipped across search scopes and output modes", async () => {
1193
1263
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-nul-"));
1194
1264
  try {
1195
1265
  const binary = join(directory, "binary.txt");
1196
- await writeFile(binary, Buffer.from("needle\r\nother\0needle\n"));
1197
- const grep = makeGrepOverrideWithBackend(directory, {});
1198
- await assert.rejects(
1199
- invoke(
1266
+ const source = Buffer.concat([
1267
+ Buffer.from("needle\r\n"),
1268
+ Buffer.alloc(65536, 97),
1269
+ Buffer.from("\0needle\n"),
1270
+ ]);
1271
+ await writeFile(binary, source);
1272
+ await writeFile(join(directory, "valid.txt"), "needle\n");
1273
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1274
+ for (const outputMode of ["content", "files", "count"] as const) {
1275
+ for (const multiline of [false, true]) {
1276
+ const skipped = await invoke(
1277
+ grep,
1278
+ "binary",
1279
+ { path: binary, pattern: "needle", literal: true, outputMode, multiline },
1280
+ undefined,
1281
+ undefined,
1282
+ );
1283
+ assert.equal(skipped.content[0].text, "No matches found");
1284
+ const result = await invoke(
1285
+ grep,
1286
+ "directory",
1287
+ { path: directory, pattern: "needle", literal: true, outputMode, multiline, limit: 1 },
1288
+ undefined,
1289
+ undefined,
1290
+ );
1291
+ assert.match(result.content[0].text, /valid\.txt/);
1292
+ assert.doesNotMatch(
1293
+ result.content[0].text,
1294
+ /binary\.txt|UNSUPPORTED_TEXT|Search incomplete/,
1295
+ );
1296
+ }
1297
+ }
1298
+ assert.deepEqual(await readFile(binary), source);
1299
+ } finally {
1300
+ await rm(directory, { recursive: true, force: true });
1301
+ }
1302
+ });
1303
+
1304
+ test("invalid UTF-8 matches use plain previews while valid files retain edit anchors", async () => {
1305
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-preview-"));
1306
+ try {
1307
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1308
+ const invalid = join(directory, "invalid.txt");
1309
+ for (const separator of ["\n", "\r\n"]) {
1310
+ const source = Buffer.concat([
1311
+ Buffer.from(`before${separator}needle `),
1312
+ Buffer.from([0xff]),
1313
+ Buffer.from(`${separator}after${separator}`),
1314
+ ]);
1315
+ await writeFile(invalid, source);
1316
+ const result = await invoke(
1200
1317
  grep,
1201
- "grep",
1202
- { path: binary, pattern: "needle", literal: true },
1318
+ "preview",
1319
+ { path: invalid, pattern: "needle", literal: true, context: 1 },
1203
1320
  undefined,
1204
1321
  undefined,
1205
- ),
1206
- /UNSUPPORTED_TEXT/,
1207
- );
1208
- const absent = await invoke(
1322
+ );
1323
+ assert.match(result.content[0].text, /1│before\n2│needle �\n3│after/);
1324
+ assert.match(result.content[0].text, /Invalid UTF-8.*cannot be used as edit anchors/);
1325
+ assert.doesNotMatch(result.content[0].text, /\d+#|Search incomplete/);
1326
+ assert.deepEqual(await readFile(invalid), source);
1327
+ }
1328
+ await writeFile(join(directory, "valid.txt"), "needle\n");
1329
+ const mixed = await invoke(
1209
1330
  grep,
1210
- "grep",
1211
- { path: binary, pattern: "absent", literal: true },
1331
+ "mixed",
1332
+ { path: directory, pattern: "needle", literal: true },
1212
1333
  undefined,
1213
1334
  undefined,
1214
1335
  );
1215
- assert.equal(absent.content[0].type, "text");
1216
- if (absent.content[0].type === "text") assert.equal(absent.content[0].text, "No matches found");
1217
- await writeFile(join(directory, "valid.txt"), "needle\n");
1218
- await assert.rejects(
1219
- invoke(
1336
+ assert.match(mixed.content[0].text, /valid\.txt · 1 match\n1#[A-Z0-9]+│needle/);
1337
+ assert.match(mixed.content[0].text, /invalid\.txt · 1 match\n2│needle �/);
1338
+ for (const outputMode of ["files", "count"] as const) {
1339
+ const result = await invoke(
1220
1340
  grep,
1221
- "grep",
1222
- { path: directory, pattern: "needle", literal: true },
1341
+ "summary",
1342
+ { path: directory, pattern: "needle", literal: true, outputMode },
1223
1343
  undefined,
1224
1344
  undefined,
1225
- ),
1226
- /UNSUPPORTED_TEXT/,
1345
+ );
1346
+ assert.match(result.content[0].text, /invalid\.txt/);
1347
+ assert.match(result.content[0].text, /valid\.txt/);
1348
+ assert.doesNotMatch(result.content[0].text, /Search incomplete/);
1349
+ }
1350
+ const multiline = await invoke(
1351
+ grep,
1352
+ "multiline",
1353
+ { path: invalid, pattern: "needle.*\\r\\nafter", literal: false, multiline: true },
1354
+ undefined,
1355
+ undefined,
1227
1356
  );
1228
- const filesMode: any = await invoke(
1357
+ // Raw-byte regex matching follows rg semantics: dot does not match malformed UTF-8.
1358
+ assert.equal(multiline.content[0].text, "No matches found");
1359
+ const rawBoundary = await invoke(
1229
1360
  grep,
1230
- "grep",
1231
- { path: binary, pattern: "needle", literal: true, outputMode: "files" },
1361
+ "raw-boundary",
1362
+ { path: invalid, pattern: "(?-u:needle[^\\r]*\\r\\nafter)", literal: false, multiline: true },
1232
1363
  undefined,
1233
1364
  undefined,
1234
1365
  );
1235
- assert.match(filesMode.content[0].text, /binary\.txt/);
1366
+ assert.match(rawBoundary.content[0].text, /2│needle �\n3│after/);
1367
+ assert.doesNotMatch(rawBoundary.content[0].text, /\d+#/);
1236
1368
  } finally {
1237
1369
  await rm(directory, { recursive: true, force: true });
1238
1370
  }