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