@criogaid/pi-hashline-edit 0.3.0 → 0.4.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 (69) hide show
  1. package/README.md +59 -50
  2. package/package.json +8 -3
  3. package/src/core/apply.test.ts +366 -310
  4. package/src/core/apply.ts +269 -202
  5. package/src/core/hash.test.ts +61 -23
  6. package/src/core/hash.ts +45 -16
  7. package/src/core/lines.test.ts +46 -27
  8. package/src/core/lines.ts +36 -28
  9. package/src/core/ranges.test.ts +73 -19
  10. package/src/core/ranges.ts +15 -15
  11. package/src/core/text.test.ts +3 -3
  12. package/src/core/text.ts +28 -9
  13. package/src/core/types.ts +54 -36
  14. package/src/index.ts +22 -16
  15. package/src/integration/grep-rg.test.ts +834 -219
  16. package/src/integration/large-text.test.ts +62 -0
  17. package/src/integration/replace-package.test.ts +37 -0
  18. package/src/pi/action-fusion.test.ts +535 -206
  19. package/src/pi/action-fusion.ts +469 -244
  20. package/src/pi/anchor-format.test.ts +30 -19
  21. package/src/pi/anchor-format.ts +27 -14
  22. package/src/pi/budgets.ts +20 -0
  23. package/src/pi/config.test.ts +60 -30
  24. package/src/pi/config.ts +45 -32
  25. package/src/pi/directory-sync.test.ts +55 -0
  26. package/src/pi/e2e-scenarios.test.ts +359 -0
  27. package/src/pi/edit-tool.ts +426 -262
  28. package/src/pi/error-text.ts +21 -0
  29. package/src/pi/execute.test.ts +1594 -694
  30. package/src/pi/failure-context.test.ts +105 -83
  31. package/src/pi/failure-context.ts +83 -71
  32. package/src/pi/failure-path.test.ts +171 -64
  33. package/src/pi/file-commit.test.ts +446 -233
  34. package/src/pi/file-commit.ts +347 -203
  35. package/src/pi/fusion-card.test.ts +118 -83
  36. package/src/pi/fusion-card.ts +120 -85
  37. package/src/pi/grep-output.ts +208 -0
  38. package/src/pi/grep-render.ts +58 -0
  39. package/src/pi/grep-scope.ts +152 -0
  40. package/src/pi/grep-search.ts +217 -0
  41. package/src/pi/grep-tool.ts +191 -766
  42. package/src/pi/grep.test.ts +562 -150
  43. package/src/pi/mutation-result.test.ts +37 -0
  44. package/src/pi/mutation-result.ts +107 -59
  45. package/src/pi/mutation-runner.ts +168 -0
  46. package/src/pi/path.ts +20 -12
  47. package/src/pi/pi.test.ts +95 -52
  48. package/src/pi/read-tool.ts +219 -130
  49. package/src/pi/registration.test.ts +274 -113
  50. package/src/pi/render.test.ts +218 -83
  51. package/src/pi/render.ts +152 -95
  52. package/src/pi/replace-apply.ts +109 -0
  53. package/src/pi/replace-tool.ts +280 -240
  54. package/src/pi/replace-worker.mjs +4 -0
  55. package/src/pi/replace-worker.ts +10 -0
  56. package/src/pi/replace.test.ts +773 -389
  57. package/src/pi/result-safety.test.ts +779 -342
  58. package/src/pi/rg-line-filter.test.ts +64 -36
  59. package/src/pi/rg-line-filter.ts +40 -124
  60. package/src/pi/rg-line-ranges.test.ts +57 -31
  61. package/src/pi/rg-line-ranges.ts +44 -82
  62. package/src/pi/rg-text-view.ts +106 -36
  63. package/src/pi/schema.ts +27 -0
  64. package/src/pi/state.ts +7 -7
  65. package/src/pi/text-stream.test.ts +118 -0
  66. package/src/pi/text-stream.ts +136 -0
  67. package/src/pi/tool-call.testing.ts +37 -0
  68. package/src/pi/write-tool.ts +115 -52
  69. package/src/pi/write.test.ts +248 -96
@@ -4,19 +4,37 @@
4
4
  */
5
5
  import { test } from "node:test";
6
6
  import assert from "node:assert/strict";
7
- import { lstat, mkdir, mkdtemp, readFile, readlink, realpath, rm, symlink, writeFile } from "node:fs/promises";
7
+ import {
8
+ lstat,
9
+ mkdir,
10
+ mkdtemp,
11
+ readFile,
12
+ readlink,
13
+ rm,
14
+ symlink,
15
+ writeFile,
16
+ } from "node:fs/promises";
8
17
  import { tmpdir } from "node:os";
9
18
  import { join } from "node:path";
10
19
  import { rgPath } from "@vscode/ripgrep";
20
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
11
21
  import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
12
22
  import { makeEditOverride } from "../pi/edit-tool.ts";
13
23
  import { makeReadOverride } from "../pi/read-tool.ts";
14
24
  import { makeWriteOverride } from "../pi/write-tool.ts";
15
25
  import { makeReplaceTool } from "../pi/replace-tool.ts";
16
- import { COMMON_RG_ARGS, createLinePredicate, resolveIgnoreCase, runRg } from "../pi/rg-line-filter.ts";
26
+ import { COMMON_RG_ARGS, resolveIgnoreCase, runRg } from "../pi/rg-line-filter.ts";
17
27
  import { computeLineHash } from "../core/hash.ts";
28
+ import { callTool } from "../pi/tool-call.testing.ts";
18
29
 
19
- const REGEX_MODE = { engine: "default", multiline: false, literal: false } as const;
30
+ const invoke = (
31
+ tool: any,
32
+ toolCallId: string,
33
+ args: unknown,
34
+ signal?: AbortSignal,
35
+ onUpdate?: unknown,
36
+ ctx?: unknown,
37
+ ) => callTool(tool, args, { toolCallId, signal, onUpdate, ctx });
20
38
 
21
39
  test("real rg emits anchored matches from a temporary directory", async () => {
22
40
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-integration-"));
@@ -26,7 +44,7 @@ test("real rg emits anchored matches from a temporary directory", async () => {
26
44
  const tool = makeGrepOverrideWithBackend(directory, {});
27
45
 
28
46
  for (const pattern of ["needle", ["needle"]]) {
29
- const result: any = await tool.execute("0", { pattern }, undefined, undefined);
47
+ const result: any = await invoke(tool, "0", { pattern }, undefined, undefined);
30
48
  assert.match(result.content[0].text, /fixture\.ts · 1 match/);
31
49
  assert.match(result.content[0].text, /1#[0-9A-Z]+│needle/);
32
50
  }
@@ -35,37 +53,105 @@ test("real rg emits anchored matches from a temporary directory", async () => {
35
53
  }
36
54
  });
37
55
 
38
- test("real rg and line filters share case and Unicode semantics", async () => {
56
+ test("real rg finds whitespace-only literal queries and OR alternatives", async () => {
57
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-whitespace-"));
58
+ try {
59
+ const file = join(directory, "fixture.txt");
60
+ await writeFile(file, "plain word\nfirst\tsecond\nboth \tforms\n");
61
+ const tool = makeGrepOverrideWithBackend(directory, {});
62
+ for (const { pattern, expected } of [
63
+ { pattern: " ", expected: 2 },
64
+ { pattern: "\t", expected: 2 },
65
+ { pattern: [" ", "\t"], expected: 3 },
66
+ ]) {
67
+ const result = await invoke(
68
+ tool,
69
+ "0",
70
+ { pattern, path: file, literal: true, outputMode: "count" },
71
+ undefined,
72
+ undefined,
73
+ );
74
+ assert.match(result.content[0].text, new RegExp(`Total: ${expected} matches in 1 file`));
75
+ }
76
+ const lineBreaks = await invoke(
77
+ tool,
78
+ "0",
79
+ { pattern: "\n", path: file, literal: true, multiline: true, outputMode: "count" },
80
+ undefined,
81
+ undefined,
82
+ );
83
+ assert.match(lineBreaks.content[0].text, /Total: 3 matches in 1 file/);
84
+ } finally {
85
+ await rm(directory, { recursive: true, force: true });
86
+ }
87
+ });
88
+
89
+ test("real rg uses smart-case across OR patterns and Unicode matches", async () => {
39
90
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-case-"));
40
91
  try {
41
92
  await writeFile(join(directory, "fixture.ts"), "FOO abc\nfoo BAR\nfoo bar\nK zip\nk zip\n");
42
93
  const tool = makeGrepOverrideWithBackend(directory, {});
43
- for (const ignoreCase of [undefined, true, false]) {
44
- const expectedCount = ignoreCase === false ? 2 : 3;
45
- for (const query of [
46
- { pattern: "foo\\S*" },
47
- { pattern: ["foo\\S*", "\\S+"], matchMode: "all" },
48
- ]) {
49
- const result: any = await tool.execute("0", { ...query, ignoreCase }, undefined, undefined);
50
- assert.match(result.content[0].text, new RegExp(`fixture\\.ts · ${expectedCount} matches`));
51
- assert.equal(result.content[0].text.includes("FOO abc"), ignoreCase !== false);
52
- }
53
- const excluded: any = await tool.execute("0", {
54
- pattern: "foo\\S*", excludePattern: "bar", ignoreCase,
55
- }, undefined, undefined);
56
- assert.match(excluded.content[0].text, /fixture\.ts · 1 match/);
57
- assert.ok(excluded.content[0].text.includes(ignoreCase === false ? "foo BAR" : "FOO abc"));
58
- }
59
- const unicodeAll: any = await tool.execute("0", {
60
- pattern: ["k", "zip"], matchMode: "all",
61
- }, undefined, undefined);
62
- assert.match(unicodeAll.content[0].text, /fixture\.ts · 2 matches/);
63
- assert.ok(unicodeAll.content[0].text.includes("K zip"));
64
-
65
- const unicodeExcluded: any = await tool.execute("0", {
66
- pattern: "zip", excludePattern: "k",
67
- }, undefined, undefined);
68
- assert.equal(unicodeExcluded.content[0].text, "No matches found");
94
+ const lower = await invoke(tool, "0", { pattern: ["foo", "bar"] }, undefined, undefined);
95
+ assert.match(lower.content[0].text, /fixture\.ts · 3 matches/);
96
+ assert.match(lower.content[0].text, /│FOO abc/);
97
+ const mixed = await invoke(tool, "0", { pattern: ["foo", "BAR"] }, undefined, undefined);
98
+ assert.match(mixed.content[0].text, /fixture\.ts · 2 matches/);
99
+ assert.doesNotMatch(mixed.content[0].text, /│FOO abc/);
100
+ const unicode = await invoke(tool, "0", { pattern: "k" }, undefined, undefined);
101
+ assert.match(unicode.content[0].text, /fixture\.ts · 2 matches/);
102
+ assert.match(unicode.content[0].text, /│K zip/);
103
+ } finally {
104
+ await rm(directory, { recursive: true, force: true });
105
+ }
106
+ });
107
+
108
+ test("explicit ignoreCase overrides smart-case for regex and literal queries", async () => {
109
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-case-override-"));
110
+ try {
111
+ const file = join(directory, "fixture.txt");
112
+ await writeFile(file, "FOO\nfoo\n");
113
+ const tool = makeGrepOverrideWithBackend(directory, {});
114
+ const sensitive = await invoke(
115
+ tool,
116
+ "0",
117
+ { pattern: "foo", path: file, ignoreCase: false },
118
+ undefined,
119
+ undefined,
120
+ );
121
+ assert.match(sensitive.content[0].text, /fixture\.txt · 1 match/);
122
+ assert.match(sensitive.content[0].text, /2#[0-9A-Z]+│foo/);
123
+ const insensitive = await invoke(
124
+ tool,
125
+ "0",
126
+ { pattern: "FOO", path: file, ignoreCase: true, literal: true },
127
+ undefined,
128
+ undefined,
129
+ );
130
+ assert.match(insensitive.content[0].text, /fixture\.txt · 2 matches/);
131
+ const regex = await invoke(
132
+ tool,
133
+ "0",
134
+ { pattern: "^FOO$", path: file, ignoreCase: true, literal: false },
135
+ undefined,
136
+ undefined,
137
+ );
138
+ assert.match(regex.content[0].text, /fixture\.txt · 2 matches/);
139
+ const inlineSensitive = await invoke(
140
+ tool,
141
+ "0",
142
+ { pattern: "(?-i:^FOO$)", path: file, ignoreCase: true, literal: false },
143
+ undefined,
144
+ undefined,
145
+ );
146
+ assert.match(inlineSensitive.content[0].text, /fixture\.txt · 1 match/);
147
+ const inlineInsensitive = await invoke(
148
+ tool,
149
+ "0",
150
+ { pattern: "(?i:^FOO$)", path: file, ignoreCase: false, literal: false },
151
+ undefined,
152
+ undefined,
153
+ );
154
+ assert.match(inlineInsensitive.content[0].text, /fixture\.txt · 2 matches/);
69
155
  } finally {
70
156
  await rm(directory, { recursive: true, force: true });
71
157
  }
@@ -87,30 +173,46 @@ test("real rg resolves regex-aware smart-case decisions", async () => {
87
173
  ["\\p{Lu}", false],
88
174
  ]);
89
175
  for (const [pattern, expected] of cases) {
90
- assert.equal(await resolveIgnoreCase(rgPath!, [pattern], REGEX_MODE, undefined), expected, pattern);
176
+ assert.equal(
177
+ await resolveIgnoreCase(rgPath!, [pattern], { literal: false, multiline: false }, undefined),
178
+ expected,
179
+ pattern,
180
+ );
91
181
  }
92
- assert.equal(await resolveIgnoreCase(rgPath!, ["foo\\S*"], { ...REGEX_MODE, literal: true }, undefined), false);
93
- assert.equal(await resolveIgnoreCase(rgPath!, ["(a)", "Foo"], REGEX_MODE, undefined), false);
182
+ assert.equal(
183
+ await resolveIgnoreCase(rgPath!, ["foo\\S*"], { literal: true, multiline: false }, undefined),
184
+ false,
185
+ );
186
+ assert.equal(
187
+ await resolveIgnoreCase(
188
+ rgPath!,
189
+ ["(a)", "Foo"],
190
+ { literal: false, multiline: false },
191
+ undefined,
192
+ ),
193
+ false,
194
+ );
195
+ assert.equal(
196
+ await resolveIgnoreCase(rgPath!, ["foo\\nbar"], { literal: false, multiline: true }, undefined),
197
+ true,
198
+ );
94
199
  });
95
200
 
96
- test("real rg owns AND and exclusion regex semantics", async () => {
97
- const directory = await mkdtemp(join(tmpdir(), "hl-grep-filter-"));
201
+ test("real rg accepts Rust regex syntax and rejects unsupported lookarounds", async () => {
202
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-regex-engine-"));
98
203
  try {
99
204
  await writeFile(join(directory, "fixture.ts"), "FOO abc\nfoo BAR\nfoo bar\n");
100
205
  const tool = makeGrepOverrideWithBackend(directory, {});
101
- const rustSyntax: any = await tool.execute("0", {
102
- pattern: ["(?P<name>foo)\\S*", "BAR"],
103
- matchMode: "all",
104
- }, undefined, undefined);
105
- assert.match(rustSyntax.content[0].text, /│foo BAR/);
106
- assert.doesNotMatch(rustSyntax.content[0].text, /│foo bar/);
107
-
206
+ const result = await invoke(
207
+ tool,
208
+ "0",
209
+ { pattern: "(?P<name>foo)\\S*", literal: false },
210
+ undefined,
211
+ undefined,
212
+ );
213
+ assert.match(result.content[0].text, /│foo BAR/);
108
214
  await assert.rejects(
109
- tool.execute("0", {
110
- pattern: "missing",
111
- excludePattern: "queueTool(",
112
- literal: false,
113
- }, undefined, undefined),
215
+ invoke(tool, "0", { pattern: "foo(?= BAR)", literal: false }, undefined, undefined),
114
216
  /regex parse error/,
115
217
  );
116
218
  } finally {
@@ -123,18 +225,38 @@ test("owned rg processes ignore RIPGREP_CONFIG_PATH", async () => {
123
225
  const previous = process.env.RIPGREP_CONFIG_PATH;
124
226
  try {
125
227
  const config = join(directory, "ripgreprc");
126
- await writeFile(config, "--ignore-case\n--fixed-strings\n--invert-match\n--pcre2\n");
228
+ await writeFile(
229
+ config,
230
+ "--ignore-case\n--fixed-strings\n--invert-match\n--pcre2\n--multiline\n",
231
+ );
127
232
  await writeFile(join(directory, "fixture.ts"), "FOO\nfoo\nbar\n");
128
233
  process.env.RIPGREP_CONFIG_PATH = config;
129
234
  const tool = makeGrepOverrideWithBackend(directory, {});
130
- const result: any = await tool.execute("0", {
131
- pattern: ["^foo$", "foo"],
132
- matchMode: "all",
133
- }, undefined, undefined);
235
+ const result: any = await invoke(
236
+ tool,
237
+ "0",
238
+ {
239
+ pattern: ["^foo$", "foo"],
240
+ },
241
+ undefined,
242
+ undefined,
243
+ );
134
244
  assert.match(result.content[0].text, /fixture\.ts · 2 matches/);
135
245
  assert.match(result.content[0].text, /│foo/);
136
246
  assert.match(result.content[0].text, /│FOO/);
137
247
  assert.doesNotMatch(result.content[0].text, /│bar/);
248
+ await assert.rejects(
249
+ invoke(tool, "0", { pattern: "foo\\nbar", literal: false }, undefined, undefined),
250
+ /not allowed in a regex/,
251
+ );
252
+ const multiline = await invoke(
253
+ tool,
254
+ "0",
255
+ { pattern: "foo\\nbar", literal: false, multiline: true },
256
+ undefined,
257
+ undefined,
258
+ );
259
+ assert.match(multiline.content[0].text, /fixture\.ts · 2 matches/);
138
260
  } finally {
139
261
  if (previous === undefined) delete process.env.RIPGREP_CONFIG_PATH;
140
262
  else process.env.RIPGREP_CONFIG_PATH = previous;
@@ -148,75 +270,75 @@ test("real rg validates its own regex syntax and limits automatic literal fallba
148
270
  await writeFile(join(directory, "fixture.ts"), "FOO\nfoo\nqueueTool(\nfoo(?=bar)\n");
149
271
  const tool = makeGrepOverrideWithBackend(directory, {});
150
272
  for (const pattern of ["(?i)^foo$", "(?P<name>foo)$"]) {
151
- const result: any = await tool.execute("0", { pattern }, undefined, undefined);
273
+ const result: any = await invoke(tool, "0", { pattern }, undefined, undefined);
152
274
  assert.match(result.content[0].text, /│foo/);
153
275
  assert.doesNotMatch(result.content[0].text, /Invalid regex/);
154
276
  }
155
277
  for (const pattern of ["queueTool(", "foo(?=bar)"]) {
156
- const result: any = await tool.execute("0", { pattern }, undefined, undefined);
278
+ const result: any = await invoke(tool, "0", { pattern }, undefined, undefined);
157
279
  assert.ok(result.content[0].text.includes(`│${pattern}`));
158
280
  assert.match(result.content[0].text, /Invalid regex; searched the pattern as literal text/);
159
281
  await assert.rejects(
160
- tool.execute("0", { pattern, literal: false }, undefined, undefined),
282
+ invoke(tool, "0", { pattern, literal: false }, undefined, undefined),
161
283
  /regex parse error/,
162
284
  );
163
285
  }
164
- const missing: any = await tool.execute("0", { pattern: "missing(" }, undefined, undefined);
286
+ const missing: any = await invoke(tool, "0", { pattern: "missing(" }, undefined, undefined);
165
287
  assert.match(missing.content[0].text, /No matches found\n\n\[Invalid regex/);
166
- const explicit: any = await tool.execute("0", { pattern: "queueTool(", literal: true }, undefined, undefined);
288
+ const explicit: any = await invoke(
289
+ tool,
290
+ "0",
291
+ { pattern: "queueTool(", literal: true },
292
+ undefined,
293
+ undefined,
294
+ );
167
295
  assert.match(explicit.content[0].text, /│queueTool\(/);
168
296
  assert.doesNotMatch(explicit.content[0].text, /Invalid regex/);
169
- for (const params of [
170
- { pattern: ["queueTool(", "\\bfoo\\b"] },
171
- { pattern: "queueTool(", excludePattern: "^\\s*//" },
172
- ]) {
173
- await assert.rejects(tool.execute("0", params, undefined, undefined), /Invalid regex in compound query/);
174
- }
297
+ await assert.rejects(
298
+ invoke(tool, "0", { pattern: ["queueTool(", "\\bfoo\\b"] }, undefined, undefined),
299
+ /Invalid regex in compound query/,
300
+ );
175
301
  } finally {
176
302
  await rm(directory, { recursive: true, force: true });
177
303
  }
178
304
  });
179
305
 
180
- test("real rg preserves CRLF end anchors in main searches, AND filters, and exclusions", async () => {
306
+ test("real rg keeps CRLF line-end anchors with fixed OR matching", async () => {
181
307
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-crlf-"));
182
308
  try {
183
309
  await writeFile(join(directory, "fixture.ts"), "alpha beta drop\r\nalpha beta\r\nalpha only");
184
310
  const tool = makeGrepOverrideWithBackend(directory, {});
185
- for (const pattern of [["alpha", "beta$"], ["beta$", "alpha"], ["beta$"]]) {
186
- const result = await tool.execute("0", { pattern, matchMode: "all" }, undefined, undefined);
187
- assert.match(result.content[0].text, /fixture\.ts · 1 match/);
188
- assert.match(result.content[0].text, /2#[0-9A-Z]+│alpha beta$/);
189
- }
190
- const excluded = await tool.execute("0", {
191
- pattern: "alpha", excludePattern: "beta$",
192
- }, undefined, undefined);
193
- assert.match(excluded.content[0].text, /fixture\.ts · 2 matches/);
194
- assert.doesNotMatch(excluded.content[0].text, /2#[0-9A-Z]+│/);
195
- assert.match(excluded.content[0].text, /3#[0-9A-Z]+│alpha only$/);
311
+ const ending = await invoke(tool, "0", { pattern: "beta$" }, undefined, undefined);
312
+ assert.match(ending.content[0].text, /fixture\.ts · 1 match/);
313
+ assert.match(ending.content[0].text, /2#[0-9A-Z]+│alpha beta$/);
314
+ const alternatives = await invoke(
315
+ tool,
316
+ "0",
317
+ { pattern: ["beta$", "alpha only"] },
318
+ undefined,
319
+ undefined,
320
+ );
321
+ assert.match(alternatives.content[0].text, /fixture\.ts · 2 matches/);
322
+ assert.match(alternatives.content[0].text, /3#[0-9A-Z]+│alpha only$/);
196
323
  } finally {
197
324
  await rm(directory, { recursive: true, force: true });
198
325
  }
199
326
  });
200
327
 
201
- test("real rg reaches a late survivor with two bounded filter invocations", async () => {
202
- const directory = await mkdtemp(join(tmpdir(), "hl-grep-batches-"));
328
+ test("real rg finds a late OR match and respects the line limit", async () => {
329
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-limit-"));
203
330
  try {
204
331
  await writeFile(join(directory, "fixture.ts"), "foo skip\n".repeat(4096) + "foo keep\n");
205
- let invocations = 0;
206
- const tool = makeGrepOverrideWithBackend(directory, {
207
- createLinePredicate(...args) {
208
- const predicate = createLinePredicate(...args);
209
- return async (lines) => {
210
- invocations++;
211
- return predicate(lines);
212
- };
213
- },
214
- });
215
- const result = await tool.execute("0", {
216
- pattern: "foo", excludePattern: "skip", literal: true, ignoreCase: false, limit: 1,
217
- }, undefined, undefined);
332
+ const tool = makeGrepOverrideWithBackend(directory, {});
333
+ const result = await invoke(
334
+ tool,
335
+ "0",
336
+ { pattern: ["foo keep", "missing"], literal: true, limit: 1 },
337
+ undefined,
338
+ undefined,
339
+ );
218
340
  assert.match(result.content[0].text, /4097#[0-9A-Z]+│foo keep/);
219
- assert.equal(invocations, 2);
341
+ assert.match(result.content[0].text, /1 matches limit reached/);
220
342
  } finally {
221
343
  await rm(directory, { recursive: true, force: true });
222
344
  }
@@ -227,35 +349,70 @@ test("shared rg runner stops and cleans up after limits, callback failures, and
227
349
  try {
228
350
  const file = join(directory, "fixture.txt");
229
351
  await writeFile(file, "needle\n".repeat(10_000));
230
- const args = [...COMMON_RG_ARGS, "--engine=default", "--no-multiline", "--json", "-e", "needle", "--", file];
352
+ const args = [
353
+ ...COMMON_RG_ARGS,
354
+ "--engine=default",
355
+ "--no-multiline",
356
+ "--json",
357
+ "-e",
358
+ "needle",
359
+ "--",
360
+ file,
361
+ ];
231
362
  const stopped = await runRg(rgPath, args, undefined, () => false);
232
363
  assert.equal(stopped.stopped, true);
233
- await assert.rejects(runRg(rgPath, args, undefined, () => {
234
- throw new Error("callback failed");
235
- }), /callback failed/);
364
+ await assert.rejects(
365
+ runRg(rgPath, args, undefined, () => {
366
+ throw new Error("callback failed");
367
+ }),
368
+ /callback failed/,
369
+ );
236
370
  const controller = new AbortController();
237
- await assert.rejects(runRg(rgPath, args, controller.signal, () => {
238
- controller.abort();
239
- return true;
240
- }), /Operation aborted/);
241
- await assert.rejects(runRg(join(directory, "missing-rg"), [], undefined, () => true), /Failed to run ripgrep/);
371
+ await assert.rejects(
372
+ runRg(rgPath, args, controller.signal, () => {
373
+ controller.abort();
374
+ return true;
375
+ }),
376
+ /Operation aborted/,
377
+ );
378
+ await assert.rejects(
379
+ runRg(join(directory, "missing-rg"), [], undefined, () => true),
380
+ /Failed to run ripgrep/,
381
+ );
242
382
  } finally {
243
383
  await rm(directory, { recursive: true, force: true });
244
384
  }
245
385
  });
246
386
 
247
- test("noIgnore searches ignored files while explicit excluding globs still apply", async () => {
387
+ test("directory searches respect ignores but explicit files and hidden files remain searchable", async () => {
248
388
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-ignore-"));
249
389
  try {
250
390
  await writeFile(join(directory, ".ignore"), "ignored.txt\n");
251
- await writeFile(join(directory, "ignored.txt"), "needle\n");
391
+ const ignored = join(directory, "ignored.txt");
392
+ await writeFile(ignored, "needle\n");
252
393
  const tool = makeGrepOverrideWithBackend(directory, {});
253
- const normal: any = await tool.execute("0", { pattern: "needle" }, undefined, undefined);
394
+ const normal = await invoke(tool, "0", { pattern: "needle" }, undefined, undefined);
254
395
  assert.equal(normal.content[0].text, "No matches found");
255
- const included: any = await tool.execute("0", { pattern: "needle", noIgnore: true }, undefined, undefined);
256
- assert.match(included.content[0].text, /ignored\.txt · 1 match/);
257
- const excluded: any = await tool.execute("0", { pattern: "needle", noIgnore: true, glob: "!ignored.txt" }, undefined, undefined);
396
+ const explicit = await invoke(
397
+ tool,
398
+ "0",
399
+ { pattern: "needle", path: ignored },
400
+ undefined,
401
+ undefined,
402
+ );
403
+ assert.match(explicit.content[0].text, /ignored\.txt · 1 match/);
404
+ const excluded = await invoke(
405
+ tool,
406
+ "0",
407
+ { pattern: "needle", path: ignored, glob: "!ignored.txt" },
408
+ undefined,
409
+ undefined,
410
+ );
258
411
  assert.equal(excluded.content[0].text, "No matches found");
412
+ await writeFile(join(directory, ".hidden.txt"), "needle\n");
413
+ const hidden = await invoke(tool, "0", { pattern: "needle" }, undefined, undefined);
414
+ assert.match(hidden.content[0].text, /\.hidden\.txt · 1 match/);
415
+ assert.doesNotMatch(hidden.content[0].text, /ignored\.txt · 1 match/);
259
416
  } finally {
260
417
  await rm(directory, { recursive: true, force: true });
261
418
  }
@@ -270,25 +427,39 @@ test("explicit file paths obey ordered glob filters without inheriting ignore ru
270
427
  await writeFile(join(directory, ".ignore"), "ignored.ts\n");
271
428
  await Promise.all([kept, excluded, ignored].map((path) => writeFile(path, "needle\n")));
272
429
  const tool = makeGrepOverrideWithBackend(directory, {});
273
- const result: any = await tool.execute("0", {
274
- pattern: "needle",
275
- path: [kept, excluded, ignored],
276
- glob: ["*.ts", "!**/*.test.ts"],
277
- outputMode: "files",
278
- }, undefined, undefined);
430
+ const result: any = await invoke(
431
+ tool,
432
+ "0",
433
+ {
434
+ pattern: "needle",
435
+ path: [kept, excluded, ignored],
436
+ glob: ["*.ts", "!**/*.test.ts"],
437
+ outputMode: "files",
438
+ },
439
+ undefined,
440
+ undefined,
441
+ );
279
442
  const files = new Set(result.content[0].text.split("\n"));
280
443
  assert.deepEqual(files, new Set(["keep.ts", "ignored.ts"]));
281
444
 
282
- const none: any = await tool.execute("0", {
283
- pattern: "needle", path: excluded, glob: "!**/*.test.ts",
284
- }, undefined, undefined);
445
+ const none: any = await invoke(
446
+ tool,
447
+ "0",
448
+ {
449
+ pattern: "needle",
450
+ path: excluded,
451
+ glob: "!**/*.test.ts",
452
+ },
453
+ undefined,
454
+ undefined,
455
+ );
285
456
  assert.equal(none.content[0].text, "No matches found");
286
457
  } finally {
287
458
  await rm(directory, { recursive: true, force: true });
288
459
  }
289
460
  });
290
461
 
291
- test("follow returns a resolved target path that edit can update without replacing the link", async (t) => {
462
+ test("directory traversal skips symlinks but explicitly named linked files remain searchable", async (t) => {
292
463
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-follow-"));
293
464
  try {
294
465
  const root = join(directory, "root");
@@ -303,20 +474,33 @@ test("follow returns a resolved target path that edit can update without replaci
303
474
  await symlink(target, link, process.platform === "win32" ? "junction" : "dir");
304
475
  await symlink(target, secondLink, process.platform === "win32" ? "junction" : "dir");
305
476
  } catch (error: any) {
306
- if (["EPERM", "EACCES", "ENOTSUP"].includes(error?.code)) return t.skip("symbolic link creation unavailable");
477
+ if (["EPERM", "EACCES", "ENOTSUP"].includes(error?.code))
478
+ return t.skip("symbolic link creation unavailable");
307
479
  throw error;
308
480
  }
309
481
  const grep = makeGrepOverrideWithBackend(root, {});
310
- const hidden: any = await grep.execute("0", { pattern: "needle" }, undefined, undefined);
482
+ const hidden: any = await invoke(grep, "0", { pattern: "needle" }, undefined, undefined);
311
483
  assert.equal(hidden.content[0].text, "No matches found");
312
- const found: any = await grep.execute("0", { pattern: "needle", follow: true }, undefined, undefined);
484
+ const found: any = await invoke(
485
+ grep,
486
+ "0",
487
+ { pattern: "needle", path: join(link, "linked.txt") },
488
+ undefined,
489
+ undefined,
490
+ );
313
491
  const [header, anchored] = found.content[0].text.split("\n");
314
492
  const resultPath = header.slice(0, header.lastIndexOf(" · "));
315
- assert.equal(resultPath, await realpath(targetFile));
493
+ assert.equal(resultPath.replace(/\\/g, "/"), "link/linked.txt");
316
494
  assert.equal(found.content[0].text.match(/ · 1 match/g)?.length, 1);
317
495
  const anchor = anchored.slice(0, anchored.indexOf("│"));
318
496
  const edit: any = makeEditOverride(root);
319
- await edit.execute("0", { path: resultPath, edits: [{ op: "replace", anchor, body: ["updated"] }] }, undefined, undefined);
497
+ await invoke(
498
+ edit,
499
+ "0",
500
+ { path: resultPath, edits: [{ op: "replace", anchor, body: ["updated"] }] },
501
+ undefined,
502
+ undefined,
503
+ );
320
504
  assert.equal(await readFile(targetFile, "utf8"), "updated\n");
321
505
  assert.equal((await lstat(link)).isSymbolicLink(), true);
322
506
  assert.equal(await readlink(link), target);
@@ -326,90 +510,353 @@ test("follow returns a resolved target path that edit can update without replaci
326
510
  }
327
511
  });
328
512
 
329
- test("PCRE2 applies to main, AND, exclusion, backreference, and inherited smart-case matches", async () => {
330
- const directory = await mkdtemp(join(tmpdir(), "hl-grep-pcre2-"));
513
+ test("real rg reports unsupported lookarounds and backreferences under Rust regex", async () => {
514
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-rust-boundary-"));
331
515
  try {
332
- await writeFile(join(directory, "fixture.txt"), "FOO abc\nfoo BAR\nfoobar\nfoofoo\n");
516
+ await writeFile(join(directory, "fixture.txt"), "foobar\nfoofoo\n");
333
517
  const tool = makeGrepOverrideWithBackend(directory, {});
334
- const main: any = await tool.execute("0", { pattern: "foo(?=bar)", pcre2: true }, undefined, undefined);
335
- assert.match(main.content[0].text, /│foobar/);
336
- const all: any = await tool.execute("0", { pattern: ["foo", "(?<=foo)bar"], matchMode: "all", pcre2: true }, undefined, undefined);
337
- assert.match(all.content[0].text, /fixture\.txt · 1 match/);
338
- assert.match(all.content[0].text, /│foobar/);
339
- const excluded: any = await tool.execute("0", { pattern: "foo", excludePattern: "(?<=foo)bar", pcre2: true }, undefined, undefined);
340
- assert.doesNotMatch(excluded.content[0].text, /│foobar/);
341
- const backreference: any = await tool.execute("0", { pattern: "(foo)\\1", pcre2: true }, undefined, undefined);
342
- assert.match(backreference.content[0].text, /│foofoo/);
343
- const inherited: any = await tool.execute("0", { pattern: "foo\\S*", excludePattern: "bar", pcre2: true }, undefined, undefined);
344
- assert.match(inherited.content[0].text, /│FOO abc/);
345
- assert.doesNotMatch(inherited.content[0].text, /│foo BAR/);
346
- await assert.rejects(tool.execute("0", { pattern: "(", pcre2: true }, undefined, undefined), /PCRE2: error compiling pattern/);
347
- await assert.rejects(tool.execute("0", { pattern: "foo", pcre2: true, literal: true }, undefined, undefined), /cannot be combined/);
518
+ const valid = await invoke(
519
+ tool,
520
+ "0",
521
+ { pattern: "foo(?:bar|foo)", literal: false },
522
+ undefined,
523
+ undefined,
524
+ );
525
+ assert.match(valid.content[0].text, /fixture\.txt · 2 matches/);
526
+ for (const pattern of ["foo(?=bar)", "(foo)\\1"]) {
527
+ await assert.rejects(
528
+ invoke(tool, "0", { pattern, literal: false }, undefined, undefined),
529
+ /regex parse error[\s\S]*no lookaround or backreferences; rewrite the pattern, or use replace/,
530
+ );
531
+ }
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.
538
+ await assert.rejects(
539
+ invoke(tool, "0", { pattern: "foo(", literal: false }, undefined, undefined),
540
+ (error: Error) =>
541
+ /regex parse error/.test(error.message) && !/lookaround/.test(error.message),
542
+ );
348
543
  } finally {
349
544
  await rm(directory, { recursive: true, force: true });
350
545
  }
351
546
  });
352
547
 
353
- test("multiline combines physical line sets for all, exclusion, limit, and PCRE2", async () => {
354
- const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-"));
548
+ test("line-based grep uses context for display but never matches across CRLF", async () => {
549
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-lines-"));
355
550
  try {
356
551
  await writeFile(join(directory, "fixture.txt"), "alpha\r\nbeta\r\ngamma\r\n");
357
- await writeFile(join(directory, "eof.txt"), "last line");
358
552
  const tool = makeGrepOverrideWithBackend(directory, {});
359
- const match: any = await tool.execute("0", { pattern: "alpha\\r?\\nbeta", multiline: true, literal: false }, undefined, undefined);
360
- assert.match(match.content[0].text, /fixture\.txt · 2 matches/);
361
- assert.match(match.content[0].text, /1#[0-9A-Z]+│alpha/);
362
- assert.match(match.content[0].text, /2#[0-9A-Z]+│beta/);
363
- const all: any = await tool.execute("0", {
364
- pattern: ["alpha\\r?\\nbeta", "beta\\r?\\ngamma"], matchMode: "all", multiline: true, literal: false,
365
- }, undefined, undefined);
366
- assert.match(all.content[0].text, /fixture\.txt · 1 match/);
367
- assert.match(all.content[0].text, /2#[0-9A-Z]+│beta/);
368
- const excluded: any = await tool.execute("0", {
369
- pattern: "alpha\\r?\\nbeta", excludePattern: "beta", multiline: true, literal: false,
370
- }, undefined, undefined);
371
- assert.match(excluded.content[0].text, /fixture\.txt · 1 match/);
372
- assert.match(excluded.content[0].text, /1#[0-9A-Z]+│alpha/);
373
- const limited: any = await tool.execute("0", {
374
- pattern: "(?s)alpha.*gamma", multiline: true, pcre2: true, limit: 2, outputMode: "count",
375
- }, undefined, undefined);
376
- assert.match(limited.content[0].text, /fixture\.txt: 2/);
377
- assert.match(limited.content[0].text, /2 matches limit reached/);
378
- const eof: any = await tool.execute("0", { pattern: "\\z", path: "eof.txt", multiline: true, pcre2: true }, undefined, undefined);
379
- assert.match(eof.content[0].text, /1#[0-9A-Z]+│last line/);
553
+ const context = await invoke(tool, "0", { pattern: "alpha", context: 1 }, undefined, undefined);
554
+ assert.match(context.content[0].text, /fixture\.txt · 1 match/);
555
+ assert.match(context.content[0].text, /1#[0-9A-Z]+│alpha/);
556
+ assert.match(context.content[0].text, /2#[0-9A-Z]+│beta/);
557
+ assert.doesNotMatch(context.content[0].text, /3#[0-9A-Z]+│gamma/);
558
+ await assert.rejects(
559
+ invoke(tool, "0", { pattern: "alpha\\nbeta", literal: false }, undefined, undefined),
560
+ /not allowed in a regex/,
561
+ );
562
+ await assert.rejects(
563
+ invoke(tool, "0", { pattern: "alpha\nbeta", literal: true }, undefined, undefined),
564
+ /not allowed/,
565
+ );
566
+ const limited = await invoke(
567
+ tool,
568
+ "0",
569
+ { pattern: ["alpha", "beta"], outputMode: "count", limit: 1 },
570
+ undefined,
571
+ undefined,
572
+ );
573
+ assert.match(limited.content[0].text, /fixture\.txt: 1/);
574
+ assert.match(limited.content[0].text, /1 matches limit reached/);
380
575
  } finally {
381
576
  await rm(directory, { recursive: true, force: true });
382
577
  }
383
578
  });
384
579
 
385
- test("complex searches reject file changes and propagate cancellation", async () => {
386
- const directory = await mkdtemp(join(tmpdir(), "hl-grep-complex-state-"));
580
+ test("multiline grep anchors CRLF spans and counts overlapping physical lines once", async () => {
581
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-"));
582
+ try {
583
+ const file = join(directory, "fixture.txt");
584
+ await writeFile(file, "alpha\r\nbeta\r\ngamma\r\n");
585
+ const tool = makeGrepOverrideWithBackend(directory, {});
586
+ const regex = await invoke(
587
+ tool,
588
+ "0",
589
+ { pattern: "alpha\\nbeta", path: file, literal: false, multiline: true },
590
+ undefined,
591
+ undefined,
592
+ );
593
+ assert.match(regex.content[0].text, /fixture\.txt · 2 matches/);
594
+ assert.match(regex.content[0].text, /1#[0-9A-Z]+│alpha/);
595
+ assert.match(regex.content[0].text, /2#[0-9A-Z]+│beta/);
596
+ const literal = await invoke(
597
+ tool,
598
+ "0",
599
+ { pattern: "alpha\nbeta", path: file, literal: true, multiline: true },
600
+ undefined,
601
+ undefined,
602
+ );
603
+ assert.match(literal.content[0].text, /fixture\.txt · 2 matches/);
604
+ const overlapping = await invoke(
605
+ tool,
606
+ "0",
607
+ { pattern: ["alpha\\nbeta", "beta\\ngamma"], path: file, multiline: true, literal: false },
608
+ undefined,
609
+ undefined,
610
+ );
611
+ assert.match(overlapping.content[0].text, /fixture\.txt · 3 matches/);
612
+ const bounded = await invoke(
613
+ tool,
614
+ "0",
615
+ { pattern: "(?s)alpha.*gamma", path: file, multiline: true, outputMode: "count", limit: 2 },
616
+ undefined,
617
+ undefined,
618
+ );
619
+ assert.match(bounded.content[0].text, /fixture\.txt: 2/);
620
+ assert.match(bounded.content[0].text, /Total: 2 matches in 1 file/);
621
+ assert.match(bounded.content[0].text, /2 matches limit reached/);
622
+ const files = await invoke(
623
+ tool,
624
+ "0",
625
+ { pattern: "alpha\\nbeta", path: file, multiline: true, outputMode: "files", limit: 1 },
626
+ undefined,
627
+ undefined,
628
+ );
629
+ assert.match(files.content[0].text, /^fixture\.txt\n\n\[1 matches limit reached/);
630
+ const boundary = await invoke(
631
+ tool,
632
+ "0",
633
+ { pattern: "alpha\\n", path: file, multiline: true, literal: false },
634
+ undefined,
635
+ undefined,
636
+ );
637
+ assert.match(boundary.content[0].text, /fixture\.txt · 1 match/);
638
+ const edit: any = makeEditOverride(directory);
639
+ const anchor = regex.content[0].text.match(/(2#[0-9A-Z]+)│beta/)?.[1];
640
+ assert.ok(anchor);
641
+ await invoke(
642
+ edit,
643
+ "0",
644
+ { path: file, edits: [{ op: "replace", anchor, body: ["updated"] }] },
645
+ undefined,
646
+ undefined,
647
+ );
648
+ assert.equal(await readFile(file, "utf8"), "alpha\r\nupdated\r\ngamma\r\n");
649
+ } finally {
650
+ await rm(directory, { recursive: true, force: true });
651
+ }
652
+ });
653
+
654
+ test("multiline grep preserves BOM and standalone CR while normalizing CRLF", async () => {
655
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-cr-"));
656
+ try {
657
+ const file = join(directory, "fixture.txt");
658
+ await writeFile(file, "\uFEFFalpha\rsolo\r\nbeta\r\n");
659
+ const tool = makeGrepOverrideWithBackend(directory, {});
660
+ const result = await invoke(
661
+ tool,
662
+ "0",
663
+ { pattern: "alpha\\rsolo\\nbeta", path: file, multiline: true, literal: false },
664
+ undefined,
665
+ undefined,
666
+ );
667
+ assert.match(result.content[0].text, /fixture\.txt · 2 matches/);
668
+ assert.match(result.content[0].text, /1#[0-9A-Z]+│\uFEFFalpha␍solo/);
669
+ assert.match(result.content[0].text, /2#[0-9A-Z]+│beta/);
670
+ const literal = await invoke(
671
+ tool,
672
+ "0",
673
+ { pattern: "alpha\rsolo\r\nbeta", path: file, multiline: true, literal: true },
674
+ undefined,
675
+ undefined,
676
+ );
677
+ assert.match(literal.content[0].text, /fixture\.txt · 2 matches/);
678
+ } finally {
679
+ await rm(directory, { recursive: true, force: true });
680
+ }
681
+ });
682
+
683
+ test("multiline grep anchors empty lines and line-start zero-width matches", async () => {
684
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-empty-"));
685
+ try {
686
+ const file = join(directory, "fixture.txt");
687
+ await writeFile(file, "alpha\r\n\r\nbeta\r\n");
688
+ const tool = makeGrepOverrideWithBackend(directory, {});
689
+ const span = await invoke(
690
+ tool,
691
+ "0",
692
+ { pattern: "alpha\\n\\nbeta", path: file, multiline: true, literal: false },
693
+ undefined,
694
+ undefined,
695
+ );
696
+ assert.match(span.content[0].text, /fixture\.txt · 3 matches/);
697
+ assert.match(span.content[0].text, /2#[0-9A-Z]+│\n3#[0-9A-Z]+│beta/);
698
+ const starts = await invoke(
699
+ tool,
700
+ "0",
701
+ { pattern: "(?m)^", path: file, multiline: true, literal: false },
702
+ undefined,
703
+ undefined,
704
+ );
705
+ assert.match(starts.content[0].text, /fixture\.txt · 3 matches/);
706
+ assert.match(starts.content[0].text, /1#[0-9A-Z]+│alpha/);
707
+ assert.match(starts.content[0].text, /3#[0-9A-Z]+│beta/);
708
+ } finally {
709
+ await rm(directory, { recursive: true, force: true });
710
+ }
711
+ });
712
+
713
+ test("multiline zero-width EOF stays on the last existing physical line", async () => {
714
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-eof-"));
715
+ try {
716
+ const file = join(directory, "fixture.txt");
717
+ const tool = makeGrepOverrideWithBackend(directory, {});
718
+ for (const content of ["last line", "last line\n"]) {
719
+ await writeFile(file, content);
720
+ const result = await invoke(
721
+ tool,
722
+ "0",
723
+ { pattern: "\\z", path: file, multiline: true, literal: false },
724
+ undefined,
725
+ undefined,
726
+ );
727
+ if (content.endsWith("\n")) {
728
+ assert.equal(result.content[0].text, "No matches found");
729
+ } else {
730
+ assert.match(result.content[0].text, /fixture\.txt · 1 match/);
731
+ assert.match(result.content[0].text, /1#[0-9A-Z]+│last line/);
732
+ }
733
+ }
734
+ } finally {
735
+ await rm(directory, { recursive: true, force: true });
736
+ }
737
+ });
738
+
739
+ test("multiline grep detects changed files and propagates cancellation", async () => {
740
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-state-"));
741
+ try {
742
+ const file = join(directory, "fixture.txt");
743
+ 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");
748
+ return result;
749
+ },
750
+ });
751
+ await assert.rejects(
752
+ invoke(
753
+ changing,
754
+ "0",
755
+ { pattern: "alpha\\nbeta", path: file, multiline: true, literal: false },
756
+ undefined,
757
+ undefined,
758
+ ),
759
+ /File changed during search/,
760
+ );
761
+ await writeFile(file, "alpha\nbeta\n");
762
+ 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);
767
+ },
768
+ });
769
+ await assert.rejects(
770
+ invoke(
771
+ cancelling,
772
+ "0",
773
+ { pattern: "alpha\\nbeta", path: file, multiline: true, literal: false },
774
+ controller.signal,
775
+ undefined,
776
+ ),
777
+ /Operation aborted/,
778
+ );
779
+ } finally {
780
+ await rm(directory, { recursive: true, force: true });
781
+ }
782
+ });
783
+
784
+ test("files and count modes aggregate only matching lines inside the limit", async () => {
785
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-output-modes-"));
786
+ try {
787
+ const first = join(directory, "a.txt");
788
+ const second = join(directory, "b.txt");
789
+ await writeFile(first, "needle needle\nneedle\n");
790
+ await writeFile(second, "needle\n");
791
+ const tool = makeGrepOverrideWithBackend(directory, {});
792
+ const query = { pattern: "needle", path: [first, second], limit: 1 };
793
+ const files = await invoke(
794
+ tool,
795
+ "files",
796
+ { ...query, outputMode: "files" },
797
+ undefined,
798
+ undefined,
799
+ );
800
+ assert.match(files.content[0].text, /^(?:a|b)\.txt\n\n\[1 matches limit reached/);
801
+ const limited = await invoke(
802
+ tool,
803
+ "count",
804
+ { ...query, outputMode: "count" },
805
+ undefined,
806
+ undefined,
807
+ );
808
+ assert.match(limited.content[0].text, /Total: 1 match in 1 file/);
809
+ const complete = await invoke(
810
+ tool,
811
+ "count-all",
812
+ { ...query, outputMode: "count", limit: 10 },
813
+ undefined,
814
+ undefined,
815
+ );
816
+ assert.match(complete.content[0].text, /Total: 3 matches in 2 files/);
817
+ const contextual = await invoke(
818
+ tool,
819
+ "content",
820
+ { ...query, path: first, outputMode: "content", context: 1 },
821
+ undefined,
822
+ undefined,
823
+ );
824
+ assert.match(contextual.content[0].text, /a\.txt · 1 match/);
825
+ assert.match(contextual.content[0].text, /2#[0-9A-Z]+│needle/);
826
+ } finally {
827
+ await rm(directory, { recursive: true, force: true });
828
+ }
829
+ });
830
+
831
+ test("default grep rejects file changes and propagates cancellation", async () => {
832
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-default-state-"));
387
833
  try {
388
834
  const file = join(directory, "fixture.txt");
389
835
  await writeFile(file, "foo bar\n");
390
- let scans = 0;
391
836
  const changing = makeGrepOverrideWithBackend(directory, {
392
837
  async runRg(path, args, signal, onLine) {
393
838
  const result = await runRg(path, args, signal, onLine);
394
- if (args.includes("--threads=1") && scans++ === 0) await writeFile(file, "changed content\n");
839
+ if (args.includes("--json")) await writeFile(file, "changed content\n");
395
840
  return result;
396
841
  },
397
842
  });
398
- await assert.rejects(changing.execute("0", {
399
- pattern: ["foo", "bar"], matchMode: "all", pcre2: true, ignoreCase: false,
400
- }, undefined, undefined), /File changed during search/);
843
+ await assert.rejects(
844
+ invoke(changing, "0", { pattern: "foo", path: file }, undefined, undefined),
845
+ /File changed during search/,
846
+ );
401
847
 
402
848
  await writeFile(file, "foo bar\n");
403
849
  const controller = new AbortController();
404
850
  const cancelling = makeGrepOverrideWithBackend(directory, {
405
851
  runRg(path, args, signal, onLine) {
406
- if (args.includes("--threads=1")) controller.abort();
852
+ if (args.includes("--json")) controller.abort();
407
853
  return runRg(path, args, signal, onLine);
408
854
  },
409
855
  });
410
- await assert.rejects(cancelling.execute("0", {
411
- pattern: "foo(?= bar)", pcre2: true, ignoreCase: false,
412
- }, controller.signal, undefined), /Operation aborted/);
856
+ await assert.rejects(
857
+ invoke(cancelling, "0", { pattern: "foo", path: file }, controller.signal, undefined),
858
+ /Operation aborted/,
859
+ );
413
860
  } finally {
414
861
  await rm(directory, { recursive: true, force: true });
415
862
  }
@@ -420,7 +867,10 @@ test("owned rg rejects an oversized JSONL record", async () => {
420
867
  try {
421
868
  await writeFile(join(directory, "large.txt"), `needle${"x".repeat(17 * 1024 * 1024)}\n`);
422
869
  const tool = makeGrepOverrideWithBackend(directory, {});
423
- await assert.rejects(tool.execute("0", { pattern: "needle", literal: true }, undefined, undefined), /record exceeds 16 MiB/);
870
+ await assert.rejects(
871
+ invoke(tool, "0", { pattern: "needle", literal: true }, undefined, undefined),
872
+ /record exceeds 16 MiB/,
873
+ );
424
874
  } finally {
425
875
  await rm(directory, { recursive: true, force: true });
426
876
  }
@@ -431,16 +881,41 @@ test("real rg accepts wildcard-only regexes and preserves limits and literal mod
431
881
  try {
432
882
  await writeFile(join(directory, "fixture.txt"), "first\n\n.*\n");
433
883
  const tool = makeGrepOverrideWithBackend(directory, {});
434
- for (const [pattern, expected] of [[".*", 3], ["^.+$", 2], [".?", 3]] as const) {
435
- const result: any = await tool.execute("count", { pattern, outputMode: "count" }, undefined, undefined);
884
+ for (const [pattern, expected] of [
885
+ [".*", 3],
886
+ ["^.+$", 2],
887
+ [".?", 3],
888
+ ] as const) {
889
+ const result: any = await invoke(
890
+ tool,
891
+ "count",
892
+ { pattern, outputMode: "count" },
893
+ undefined,
894
+ undefined,
895
+ );
436
896
  assert.match(result.content[0].text, new RegExp(`Total: ${expected} matches in 1 file`));
437
897
  }
438
- const limited: any = await tool.execute("limited", { pattern: ".*", limit: 1 }, undefined, undefined);
898
+ const limited: any = await invoke(
899
+ tool,
900
+ "limited",
901
+ { pattern: ".*", limit: 1 },
902
+ undefined,
903
+ undefined,
904
+ );
439
905
  assert.match(limited.content[0].text, /1#[0-9A-Z]+│first/);
440
906
  assert.doesNotMatch(limited.content[0].text, /[23]#[0-9A-Z]+│/);
441
- const literal: any = await tool.execute("literal", { pattern: ".*", literal: true }, undefined, undefined);
907
+ const literal: any = await invoke(
908
+ tool,
909
+ "literal",
910
+ { pattern: ".*", literal: true },
911
+ undefined,
912
+ undefined,
913
+ );
442
914
  assert.match(literal.content[0].text, /3#[0-9A-Z]+│\.\*/);
443
- await assert.rejects(tool.execute("invalid", { pattern: "*", literal: false }, undefined, undefined), /regex parse error/);
915
+ await assert.rejects(
916
+ invoke(tool, "invalid", { pattern: "*", literal: false }, undefined, undefined),
917
+ /regex parse error/,
918
+ );
444
919
  } finally {
445
920
  await rm(directory, { recursive: true, force: true });
446
921
  }
@@ -448,8 +923,9 @@ test("real rg accepts wildcard-only regexes and preserves limits and literal mod
448
923
 
449
924
  test("tools share physical lines and anchors across text representations", async () => {
450
925
  const directory = await mkdtemp(join(tmpdir(), "hl-text-integration-"));
451
- const call = (tool: any, params: any) => tool.execute("0", params, undefined, undefined);
452
- const rows = (result: any): string[] => result.content[0].text.split("\n").filter((line: string) => /^\d+#/.test(line));
926
+ const call = (tool: any, params: any) => invoke(tool, "0", params, undefined, undefined);
927
+ const rows = (result: any): string[] =>
928
+ result.content[0].text.split("\n").filter((line: string) => /^\d+#/.test(line));
453
929
  try {
454
930
  const file = join(directory, "fixture.txt");
455
931
  for (const before of [
@@ -462,11 +938,18 @@ test("tools share physical lines and anchors across text representations", async
462
938
  await call(makeWriteOverride(directory), { path: file, content: before });
463
939
  assert.equal(await readFile(file, "utf8"), before);
464
940
  const read = await call(makeReadOverride(directory), { path: file });
465
- const grep = await call(makeGrepOverrideWithBackend(directory, {}), { path: file, pattern: "old", context: 2 });
941
+ const grep = await call(makeGrepOverrideWithBackend(directory, {}), {
942
+ path: file,
943
+ pattern: "old",
944
+ context: 2,
945
+ });
466
946
  assert.deepEqual(rows(grep), rows(read));
467
947
  if (before.includes("a\rb")) assert.match(rows(read)[0], /│a␍b$/);
468
948
  const anchor = rows(grep)[1].split("│")[0];
469
- const edit = await call(makeEditOverride(directory), { path: file, edits: [{ op: "replace", anchor, body: ["new"] }] });
949
+ const edit = await call(makeEditOverride(directory), {
950
+ path: file,
951
+ edits: [{ op: "replace", anchor, body: ["new"] }],
952
+ });
470
953
  assert.equal(await readFile(file, "utf8"), before.replace("old", "new"));
471
954
  assert.equal(edit.details.firstChangedLine, 2);
472
955
  assert.match(edit.details.diff, /^-2 old/m);
@@ -475,7 +958,10 @@ test("tools share physical lines and anchors across text representations", async
475
958
  if (before.includes("a\rb")) assert.ok(edit.details.patch.includes(" a\rb\n"));
476
959
  const editedRead = await call(makeReadOverride(directory), { path: file });
477
960
  assert.equal(rows(edit)[0], rows(editedRead)[1].split("│")[0]);
478
- const replaced = await call(makeReplaceTool(directory), { path: file, find: "new", replace: "next" });
961
+ const replaced = await call(makeReplaceTool(directory), {
962
+ path: file,
963
+ replacements: [{ find: "new", replace: "next" }],
964
+ });
479
965
  assert.equal(await readFile(file, "utf8"), before.replace("old", "next"));
480
966
  assert.equal(replaced.details.firstChangedLine, 2);
481
967
  const replacedRead = await call(makeReadOverride(directory), { path: file });
@@ -486,7 +972,7 @@ test("tools share physical lines and anchors across text representations", async
486
972
  }
487
973
  });
488
974
 
489
- test("long-line previews expose real rg hits across engines and preserve full-line anchors", async () => {
975
+ test("long-line previews preserve full-line anchors across literal and Rust regex queries", async () => {
490
976
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-preview-"));
491
977
  try {
492
978
  const long = "😀界".repeat(220) + "NEEDLE" + "tail".repeat(200);
@@ -495,11 +981,16 @@ test("long-line previews expose real rg hits across engines and preserve full-li
495
981
  const tool = makeGrepOverrideWithBackend(directory, {});
496
982
  for (const params of [
497
983
  { pattern: "NEEDLE" },
498
- { pattern: ["NEEDLE", "tail"], matchMode: "all", excludePattern: "absent" },
499
- { pattern: "NEEDLE(?=tail)", pcre2: true },
500
- { pattern: "NEEDLE(?:tail)+\\r?\\nfollow", multiline: true },
984
+ { pattern: ["NEEDLE", "tail"], literal: true },
985
+ { pattern: "NEEDLE(?:tail)+", literal: false },
501
986
  ]) {
502
- const result: any = await tool.execute("preview", { ...params, context: 2 }, undefined, undefined);
987
+ const result: any = await invoke(
988
+ tool,
989
+ "preview",
990
+ { ...params, context: 2 },
991
+ undefined,
992
+ undefined,
993
+ );
503
994
  const output = result.content[0].text;
504
995
  const row = output.split("\n").find((line: string) => line.startsWith("1#"));
505
996
  assert.ok(row.startsWith(`1#${computeLineHash(1, long)}│[partial, columns `));
@@ -508,7 +999,9 @@ test("long-line previews expose real rg hits across engines and preserve full-li
508
999
  assert.ok(output.includes(`3#${computeLineHash(3, context)}│[partial, columns 1-499]`));
509
1000
  assert.match(output, /anchors hash full lines/);
510
1001
  }
511
- } finally { await rm(directory, { recursive: true, force: true }); }
1002
+ } finally {
1003
+ await rm(directory, { recursive: true, force: true });
1004
+ }
512
1005
  });
513
1006
 
514
1007
  test("all text tools share logical CRLF matching, anchors, and mutation separators", async () => {
@@ -519,42 +1012,102 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
519
1012
  const write = makeWriteOverride(directory);
520
1013
  const read = makeReadOverride(directory);
521
1014
  const grep = makeGrepOverrideWithBackend(directory, {});
522
- await write.execute("write", { path, content: before }, undefined, undefined);
1015
+ await invoke(write, "write", { path, content: before }, undefined, undefined, {
1016
+ cwd: directory,
1017
+ } as ExtensionContext);
523
1018
  assert.equal(await readFile(path, "utf8"), before);
524
- const observed = await read.execute("read", { path }, undefined, undefined);
1019
+ const observed = await invoke(read, "read", { path }, undefined, undefined, {
1020
+ cwd: directory,
1021
+ } as ExtensionContext);
525
1022
  assert.ok(observed.content[0].type === "text");
526
- const rows = observed.content[0].text.split("\n").filter((line: string) => /^\d+#[0-9A-Z]+│/.test(line));
1023
+ const rows = observed.content[0].text
1024
+ .split("\n")
1025
+ .filter((line: string) => /^\d+#[0-9A-Z]+│/.test(line));
527
1026
  for (const query of [
528
- { pattern: "alpha\r\nbeta", literal: true },
529
- { pattern: "alpha\\nbeta", literal: false },
530
- { pattern: "alpha\\nbeta", literal: false, pcre2: true },
531
- { pattern: ["alpha\\nbeta", "alpha|beta"], matchMode: "all", literal: false },
1027
+ { pattern: ["alpha", "beta"], literal: true },
1028
+ { pattern: "alpha|beta", literal: false },
532
1029
  ]) {
533
- const found = await grep.execute("grep", { path, multiline: true, ignoreCase: false, ...query }, undefined, undefined);
1030
+ const found = await invoke(grep, "grep", { path, ...query }, undefined, undefined);
534
1031
  assert.ok(found.content[0].text.includes(rows[1]), JSON.stringify(query));
535
1032
  assert.ok(found.content[0].text.includes(rows[2]), JSON.stringify(query));
536
1033
  }
537
- const excluded = await grep.execute("grep", { path, pattern: "alpha\\nbeta", excludePattern: "alpha", multiline: true, literal: false }, undefined, undefined);
538
- assert.ok(!excluded.content[0].text.includes(rows[1]));
539
- assert.ok(excluded.content[0].text.includes(rows[2]));
540
- const carriage = await grep.execute("grep", { path, pattern: "\\r", literal: false }, undefined, undefined);
1034
+ const contextual = await invoke(
1035
+ grep,
1036
+ "grep",
1037
+ { path, pattern: "alpha", context: 1 },
1038
+ undefined,
1039
+ undefined,
1040
+ );
1041
+ assert.match(contextual.content[0].text, /mixed\.txt · 1 match/);
1042
+ assert.ok(contextual.content[0].text.includes(rows[1]));
1043
+ assert.ok(contextual.content[0].text.includes(rows[2]));
1044
+ const carriage = await invoke(
1045
+ grep,
1046
+ "grep",
1047
+ { path, pattern: "\\r", literal: false },
1048
+ undefined,
1049
+ undefined,
1050
+ );
541
1051
  assert.ok(carriage.content[0].text.includes(rows[3]));
542
1052
  assert.ok(!carriage.content[0].text.includes(rows[1]));
543
- const standaloneEnd = await grep.execute("grep", { path, pattern: "CR\\r$", literal: false }, undefined, undefined);
1053
+ const standaloneEnd = await invoke(
1054
+ grep,
1055
+ "grep",
1056
+ { path, pattern: "CR\\r$", literal: false },
1057
+ undefined,
1058
+ undefined,
1059
+ );
544
1060
  assert.ok(standaloneEnd.content[0].text.includes(rows[3]));
545
1061
 
546
1062
  const expected = "\uFEFFhead\r\nA\r\nB\r\nC\nstand\rCR\r\r\nlast";
547
1063
  for (const mode of ["edit", "literal", "regex"]) {
548
- await write.execute("reset", { path, content: before }, undefined, undefined);
1064
+ await invoke(write, "reset", { path, content: before }, undefined, undefined, {
1065
+ cwd: directory,
1066
+ } as ExtensionContext);
549
1067
  if (mode === "edit") {
550
- await makeEditOverride(directory).execute("edit", { path, edits: [{ op: "replace", anchor: rows[1].split("│")[0], end: rows[2].split("│")[0], body: ["A", "B", "C"] }] }, undefined, undefined);
1068
+ await invoke(
1069
+ makeEditOverride(directory),
1070
+ "edit",
1071
+ {
1072
+ path,
1073
+ edits: [
1074
+ {
1075
+ op: "replace",
1076
+ anchor: rows[1].split("│")[0],
1077
+ end: rows[2].split("│")[0],
1078
+ body: ["A", "B", "C"],
1079
+ },
1080
+ ],
1081
+ },
1082
+ undefined,
1083
+ undefined,
1084
+ { cwd: directory } as ExtensionContext,
1085
+ );
551
1086
  } else {
552
- await makeReplaceTool(directory).execute("replace", { path, find: mode === "regex" ? "alpha\\nbeta" : "alpha\r\nbeta", replace: "A\nB\nC", regex: mode === "regex" }, undefined, undefined);
1087
+ await invoke(
1088
+ makeReplaceTool(directory),
1089
+ "replace",
1090
+ {
1091
+ path,
1092
+ replacements: [
1093
+ {
1094
+ find: mode === "regex" ? "alpha\\nbeta" : "alpha\r\nbeta",
1095
+ replace: "A\nB\nC",
1096
+ regex: mode === "regex",
1097
+ },
1098
+ ],
1099
+ },
1100
+ undefined,
1101
+ undefined,
1102
+ { cwd: directory } as ExtensionContext,
1103
+ );
553
1104
  }
554
1105
  assert.equal(await readFile(path, "utf8"), expected, mode);
555
1106
  }
556
1107
  // Whole-file write remains the explicit representation boundary, including EOL conversion.
557
- await write.execute("convert", { path, content: "alpha\nbeta\n" }, undefined, undefined);
1108
+ await invoke(write, "convert", { path, content: "alpha\nbeta\n" }, undefined, undefined, {
1109
+ cwd: directory,
1110
+ } as ExtensionContext);
558
1111
  assert.equal(await readFile(path, "utf8"), "alpha\nbeta\n");
559
1112
  } finally {
560
1113
  await rm(directory, { recursive: true, force: true });
@@ -566,23 +1119,45 @@ test("visible source escapes remain readable and searchable while real CRLF stay
566
1119
  try {
567
1120
  const path = join(directory, "escapes.ts");
568
1121
  const code = 'const eol = "\\r\\n";\r\n';
569
- await makeWriteOverride(directory).execute("write", { path, content: code }, undefined, undefined);
570
- const read = await makeReadOverride(directory).execute("read", { path }, undefined, undefined);
1122
+ await invoke(
1123
+ makeWriteOverride(directory),
1124
+ "write",
1125
+ { path, content: code },
1126
+ undefined,
1127
+ undefined,
1128
+ { cwd: directory } as ExtensionContext,
1129
+ );
1130
+ const read = await invoke(makeReadOverride(directory), "read", { path }, undefined, undefined, {
1131
+ cwd: directory,
1132
+ } as ExtensionContext);
571
1133
  assert.ok(read.content[0].type === "text");
572
1134
  assert.ok(read.content[0].text.includes(String.raw`const eol = "\r\n";`));
573
1135
  const grep = makeGrepOverrideWithBackend(directory, {});
574
1136
  for (const query of [
575
1137
  { pattern: String.raw`\r\n`, literal: true },
576
1138
  { pattern: String.raw`\\r\\n`, literal: false },
577
- { pattern: String.raw`\\r\\n`, literal: false, pcre2: true },
578
1139
  ]) {
579
- const result = await grep.execute("grep", { path, ...query, ignoreCase: false }, undefined, undefined);
1140
+ const result = await invoke(grep, "grep", { path, ...query }, undefined, undefined);
580
1141
  assert.ok(result.content[0].text.includes(String.raw`const eol = "\r\n";`));
581
1142
  }
582
1143
  const replace = makeReplaceTool(directory);
583
- await replace.execute("literal", { path, find: String.raw`\r\n`, replace: String.raw`\n` }, undefined, undefined);
1144
+ await invoke(
1145
+ replace,
1146
+ "literal",
1147
+ { path, replacements: [{ find: String.raw`\r\n`, replace: String.raw`\n` }] },
1148
+ undefined,
1149
+ undefined,
1150
+ { cwd: directory } as ExtensionContext,
1151
+ );
584
1152
  assert.equal(await readFile(path, "utf8"), 'const eol = "\\n";\r\n');
585
- await replace.execute("regex", { path, find: String.raw`\\n`, replace: String.raw`\r\n`, regex: true }, undefined, undefined);
1153
+ await invoke(
1154
+ replace,
1155
+ "regex",
1156
+ { path, replacements: [{ find: String.raw`\\n`, replace: String.raw`\r\n`, regex: true }] },
1157
+ undefined,
1158
+ undefined,
1159
+ { cwd: directory } as ExtensionContext,
1160
+ );
586
1161
  assert.equal(await readFile(path, "utf8"), code);
587
1162
  } finally {
588
1163
  await rm(directory, { recursive: true, force: true });
@@ -592,10 +1167,18 @@ test("visible source escapes remain readable and searchable while real CRLF stay
592
1167
  test("normalized grep batches retain original paths, literal option markers, and decode diagnostics", async () => {
593
1168
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-snapshots-"));
594
1169
  try {
595
- await Promise.all(Array.from({ length: 70 }, (_, i) => writeFile(join(directory, `file ${i}.txt`), "--\r\n")));
1170
+ await Promise.all(
1171
+ Array.from({ length: 70 }, (_, i) => writeFile(join(directory, `file ${i}.txt`), "--\r\n")),
1172
+ );
596
1173
  await writeFile(join(directory, "invalid.txt"), Buffer.from([0xc3, 0x28]));
597
1174
  const grep = makeGrepOverrideWithBackend(directory, {});
598
- const result = await grep.execute("grep", { path: directory, pattern: "--", literal: true, outputMode: "count", limit: 100 }, undefined, undefined);
1175
+ const result = await invoke(
1176
+ grep,
1177
+ "grep",
1178
+ { path: directory, pattern: "--", literal: true, outputMode: "count", limit: 100 },
1179
+ undefined,
1180
+ undefined,
1181
+ );
599
1182
  assert.equal((result.content[0].text.match(/file \d+\.txt: 1/g) ?? []).length, 70);
600
1183
  assert.match(result.content[0].text, /Search incomplete/);
601
1184
  assert.match(result.content[0].text, /invalid\.txt/);
@@ -612,12 +1195,44 @@ test("NUL files are rejected on a confirmed hit, not reported as no match", asyn
612
1195
  const binary = join(directory, "binary.txt");
613
1196
  await writeFile(binary, Buffer.from("needle\r\nother\0needle\n"));
614
1197
  const grep = makeGrepOverrideWithBackend(directory, {});
615
- await assert.rejects(grep.execute("grep", { path: binary, pattern: "needle", literal: true }, undefined, undefined), /UNSUPPORTED_TEXT/);
616
- const absent = await grep.execute("grep", { path: binary, pattern: "absent", literal: true }, undefined, undefined);
1198
+ await assert.rejects(
1199
+ invoke(
1200
+ grep,
1201
+ "grep",
1202
+ { path: binary, pattern: "needle", literal: true },
1203
+ undefined,
1204
+ undefined,
1205
+ ),
1206
+ /UNSUPPORTED_TEXT/,
1207
+ );
1208
+ const absent = await invoke(
1209
+ grep,
1210
+ "grep",
1211
+ { path: binary, pattern: "absent", literal: true },
1212
+ undefined,
1213
+ undefined,
1214
+ );
617
1215
  assert.equal(absent.content[0].type, "text");
618
1216
  if (absent.content[0].type === "text") assert.equal(absent.content[0].text, "No matches found");
619
1217
  await writeFile(join(directory, "valid.txt"), "needle\n");
620
- await assert.rejects(grep.execute("grep", { path: directory, pattern: "needle", literal: true }, undefined, undefined), /UNSUPPORTED_TEXT/);
1218
+ await assert.rejects(
1219
+ invoke(
1220
+ grep,
1221
+ "grep",
1222
+ { path: directory, pattern: "needle", literal: true },
1223
+ undefined,
1224
+ undefined,
1225
+ ),
1226
+ /UNSUPPORTED_TEXT/,
1227
+ );
1228
+ const filesMode: any = await invoke(
1229
+ grep,
1230
+ "grep",
1231
+ { path: binary, pattern: "needle", literal: true, outputMode: "files" },
1232
+ undefined,
1233
+ undefined,
1234
+ );
1235
+ assert.match(filesMode.content[0].text, /binary\.txt/);
621
1236
  } finally {
622
1237
  await rm(directory, { recursive: true, force: true });
623
1238
  }