@criogaid/pi-hashline-edit 0.7.0 → 0.8.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.
- package/README.md +74 -222
- package/README.zh-CN.md +162 -0
- package/package.json +2 -1
- package/src/core/AGENTS.md +1 -1
- package/src/core/apply.test.ts +172 -134
- package/src/core/apply.ts +28 -30
- package/src/core/case-fold.test.ts +11 -7
- package/src/core/case-fold.ts +2 -6
- package/src/core/character-set.ts +14 -0
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +25 -7
- package/src/core/ranges.test.ts +41 -9
- package/src/core/ranges.ts +11 -4
- package/src/core/regex.ts +36 -6
- package/src/core/replace-schema.ts +18 -11
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +6 -5
- package/src/core/search-analysis.test.ts +9 -0
- package/src/core/search-analysis.ts +35 -31
- package/src/core/search.ts +1 -1
- package/src/core/types.ts +5 -7
- package/src/index.ts +2 -4
- package/src/integration/config-registration.test.ts +16 -8
- package/src/integration/fusion-dispatch.test.ts +41 -22
- package/src/integration/grep-flags.test.ts +6 -13
- package/src/integration/grep-js-completeness.test.ts +41 -31
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +4 -3
- package/src/integration/grep-regex.test.ts +13 -16
- package/src/integration/grep-rg.test.ts +127 -87
- package/src/integration/grep-scan-budget.test.ts +27 -5
- package/src/integration/large-text.test.ts +3 -4
- package/src/integration/model-budget-lifecycle.test.ts +15 -6
- package/src/integration/mutation-file-types.test.ts +0 -1
- package/src/integration/mutation-file-types.testing.ts +1 -1
- package/src/integration/npm-package.testing.ts +6 -32
- package/src/integration/publication-read-changes.test.ts +58 -26
- package/src/integration/replace-package.testing.ts +4 -1
- package/src/integration/replace-worker-boundary.test.ts +34 -24
- package/src/integration/report-replay.test.ts +2 -2
- package/src/integration/utf8-errors.test.ts +2 -2
- package/src/pi/AGENTS.md +12 -13
- package/src/pi/action-fusion.test.ts +71 -216
- package/src/pi/action-fusion.ts +37 -74
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +7 -3
- package/src/pi/argument-diagnostics.ts +1 -1
- package/src/pi/argument-error.testing.ts +7 -4
- package/src/pi/argument-error.ts +5 -7
- package/src/pi/argument-format.test.ts +5 -16
- package/src/pi/argument-validation.test.ts +59 -25
- package/src/pi/argument-validation.ts +4 -5
- package/src/pi/budgets.ts +5 -7
- package/src/pi/config.test.ts +15 -5
- package/src/pi/config.ts +6 -6
- package/src/pi/diagnostic-buffer.test.ts +2 -3
- package/src/pi/diagnostic-buffer.ts +2 -2
- package/src/pi/edit-tool.ts +9 -12
- package/src/pi/execute.test.ts +80 -149
- package/src/pi/failure-context.test.ts +61 -16
- package/src/pi/failure-context.ts +12 -9
- package/src/pi/failure-path.test.ts +115 -50
- package/src/pi/file-commit.test.ts +30 -46
- package/src/pi/file-commit.ts +11 -40
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +193 -37
- package/src/pi/fusion-card.ts +186 -116
- package/src/pi/grep-candidates.ts +25 -27
- package/src/pi/grep-matrix.test.ts +1 -1
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +62 -69
- package/src/pi/grep-render.ts +4 -1
- package/src/pi/grep-scope.ts +1 -1
- package/src/pi/grep-search.ts +85 -56
- package/src/pi/grep-tool.ts +17 -48
- package/src/pi/grep.test.ts +270 -169
- package/src/pi/mutation-outcome.testing.ts +2 -3
- package/src/pi/mutation-result.test.ts +26 -19
- package/src/pi/mutation-result.ts +25 -40
- package/src/pi/mutation-runner.ts +9 -17
- package/src/pi/output-budgets.test.ts +25 -16
- package/src/pi/path.ts +3 -1
- package/src/pi/pi.test.ts +4 -5
- package/src/pi/read-tool.ts +32 -80
- package/src/pi/read.test.ts +145 -92
- package/src/pi/regex-worker-contract.ts +5 -23
- package/src/pi/regex-worker.ts +4 -10
- package/src/pi/registration.test.ts +54 -49
- package/src/pi/render.test.ts +196 -93
- package/src/pi/render.ts +32 -22
- package/src/pi/replace-regex.ts +3 -5
- package/src/pi/replace-tool.ts +27 -26
- package/src/pi/replace-worker-contract.ts +3 -12
- package/src/pi/replace.test.ts +144 -226
- package/src/pi/report-cause.ts +23 -19
- package/src/pi/report-failure.testing.ts +2 -2
- package/src/pi/report-schema.ts +55 -43
- package/src/pi/report.test.ts +138 -71
- package/src/pi/report.ts +40 -43
- package/src/pi/result-safety.test.ts +157 -117
- package/src/pi/rg-process.test.ts +36 -10
- package/src/pi/rg-process.ts +9 -3
- package/src/pi/rg-regex.test.ts +17 -0
- package/src/pi/rg-regex.ts +28 -22
- package/src/pi/text-stream.test.ts +35 -36
- package/src/pi/text-stream.ts +14 -14
- package/src/pi/tool-call.testing.ts +17 -7
- package/src/pi/write-tool.ts +9 -11
- package/src/pi/write.test.ts +43 -75
- package/src/testing/regex-translation-corpus.ts +16 -0
- package/src/testing/rg-line-listing.fixture.mjs +1 -1
- package/src/integration/forget-grep.test.ts +0 -148
- package/src/pi/forget-tool.ts +0 -367
- package/src/pi/forget.test.ts +0 -630
- package/src/pi/forget.testing.ts +0 -201
|
@@ -5,6 +5,8 @@ import { rejectsArgument } from "../pi/argument-error.testing.ts";
|
|
|
5
5
|
*/
|
|
6
6
|
import { test } from "node:test";
|
|
7
7
|
import assert from "node:assert/strict";
|
|
8
|
+
import childProcess from "node:child_process";
|
|
9
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
8
10
|
import {
|
|
9
11
|
lstat,
|
|
10
12
|
mkdir,
|
|
@@ -18,7 +20,7 @@ import {
|
|
|
18
20
|
} from "node:fs/promises";
|
|
19
21
|
import { tmpdir } from "node:os";
|
|
20
22
|
import { dirname, join } from "node:path";
|
|
21
|
-
import { pathToFileURL } from "node:url";
|
|
23
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
22
24
|
import { rgPath } from "@vscode/ripgrep";
|
|
23
25
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
24
26
|
import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
|
|
@@ -33,7 +35,7 @@ import { callTool } from "../pi/tool-call.testing.ts";
|
|
|
33
35
|
import { DEFAULT_CONFIG } from "../pi/config.ts";
|
|
34
36
|
import { canonicalPath } from "../pi/path.ts";
|
|
35
37
|
import { reportOf } from "../pi/report.ts";
|
|
36
|
-
import {
|
|
38
|
+
import { grepCounts } from "../pi/grep-output.testing.ts";
|
|
37
39
|
import { hostCompiles, unsupportedByHost } from "../testing/regex-host.testing.ts";
|
|
38
40
|
|
|
39
41
|
const invoke = (
|
|
@@ -80,8 +82,8 @@ test("real rg finds whitespace-only literal queries and OR alternatives", async
|
|
|
80
82
|
undefined,
|
|
81
83
|
undefined,
|
|
82
84
|
);
|
|
83
|
-
assert.equal(
|
|
84
|
-
assert.equal(
|
|
85
|
+
assert.equal(grepCounts(result).matches, expected);
|
|
86
|
+
assert.equal(grepCounts(result).files, 1);
|
|
85
87
|
}
|
|
86
88
|
const lineBreaks = await invoke(
|
|
87
89
|
tool,
|
|
@@ -90,8 +92,8 @@ test("real rg finds whitespace-only literal queries and OR alternatives", async
|
|
|
90
92
|
undefined,
|
|
91
93
|
undefined,
|
|
92
94
|
);
|
|
93
|
-
assert.equal(
|
|
94
|
-
assert.equal(
|
|
95
|
+
assert.equal(grepCounts(lineBreaks).matches, 3);
|
|
96
|
+
assert.equal(grepCounts(lineBreaks).files, 1);
|
|
95
97
|
} finally {
|
|
96
98
|
await rm(directory, { recursive: true, force: true });
|
|
97
99
|
}
|
|
@@ -207,7 +209,7 @@ test("owned rg processes ignore RIPGREP_CONFIG_PATH", async () => {
|
|
|
207
209
|
assert.doesNotMatch(result.content[0].text, /│bar/);
|
|
208
210
|
const lineOnly = await invoke(tool, "0", { pattern: "foo\\nbar", literal: false });
|
|
209
211
|
assert.equal(lineOnly.isError, undefined);
|
|
210
|
-
assert.equal(
|
|
212
|
+
assert.equal(grepCounts(lineOnly).matches, 0);
|
|
211
213
|
const multiline = await invoke(
|
|
212
214
|
tool,
|
|
213
215
|
"0",
|
|
@@ -223,7 +225,7 @@ test("owned rg processes ignore RIPGREP_CONFIG_PATH", async () => {
|
|
|
223
225
|
}
|
|
224
226
|
});
|
|
225
227
|
|
|
226
|
-
test("real rg
|
|
228
|
+
test("real rg separates exact text from regex", async () => {
|
|
227
229
|
const directory = await mkdtemp(join(tmpdir(), "hl-grep-regex-"));
|
|
228
230
|
try {
|
|
229
231
|
await writeFile(
|
|
@@ -231,10 +233,6 @@ test("real rg requires an explicit mode and separates exact text from regex", as
|
|
|
231
233
|
"FOO\nfoo\nfoo0\nfoo(0)\nqueueTool(\nfoo(?=bar)\n",
|
|
232
234
|
);
|
|
233
235
|
const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
|
|
234
|
-
await assert.rejects(
|
|
235
|
-
invoke(tool, "missing-mode", { pattern: "foo" }),
|
|
236
|
-
rejectsArgument("literal", /required/),
|
|
237
|
-
);
|
|
238
236
|
for (const pattern of ["(?i:^foo$)", "(?<name>foo)$"].filter((source) =>
|
|
239
237
|
hostCompiles(source),
|
|
240
238
|
)) {
|
|
@@ -249,7 +247,7 @@ test("real rg requires an explicit mode and separates exact text from regex", as
|
|
|
249
247
|
assert.ok(result.content[0].text.includes(`│${pattern}`));
|
|
250
248
|
}
|
|
251
249
|
const missing = await invoke(tool, "no-match", { pattern: "missing(", literal: true });
|
|
252
|
-
assert.equal(
|
|
250
|
+
assert.equal(grepCounts(missing).matches, 0);
|
|
253
251
|
assert.equal(missing.isError, undefined);
|
|
254
252
|
await assert.rejects(
|
|
255
253
|
invoke(tool, "invalid-regex", { pattern: "queueTool(", literal: false }),
|
|
@@ -295,7 +293,10 @@ test("real rg keeps CRLF line-end anchors with regex OR matching", async () => {
|
|
|
295
293
|
test("real rg finds a late OR match and respects the line limit", async () => {
|
|
296
294
|
const directory = await mkdtemp(join(tmpdir(), "hl-grep-limit-"));
|
|
297
295
|
try {
|
|
298
|
-
await writeFile(
|
|
296
|
+
await writeFile(
|
|
297
|
+
join(directory, "fixture.ts"),
|
|
298
|
+
"foo skip\n".repeat(4096) + "foo keep\nfoo keep later\n",
|
|
299
|
+
);
|
|
299
300
|
const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
|
|
300
301
|
const result = await invoke(
|
|
301
302
|
tool,
|
|
@@ -304,40 +305,71 @@ test("real rg finds a late OR match and respects the line limit", async () => {
|
|
|
304
305
|
undefined,
|
|
305
306
|
undefined,
|
|
306
307
|
);
|
|
307
|
-
assert.match(result.content[0].text, /4097#[0-9A-Z]+│foo keep
|
|
308
|
-
assert.
|
|
308
|
+
assert.match(result.content[0].text, /4097#[0-9A-Z]+│foo keep$/m);
|
|
309
|
+
assert.doesNotMatch(result.content[0].text, /foo keep later|4098#/);
|
|
310
|
+
assert.equal(grepCounts(result).matches, 1);
|
|
311
|
+
assert.equal(reportOf(result, "grep")?.matchLimit, 1);
|
|
309
312
|
} finally {
|
|
310
313
|
await rm(directory, { recursive: true, force: true });
|
|
311
314
|
}
|
|
312
315
|
});
|
|
313
316
|
|
|
314
|
-
test("shared rg runner stops and cleans up after limits, callback failures, and cancellation", async () => {
|
|
317
|
+
test("shared rg runner stops and cleans up after limits, callback failures, and cancellation", async (t) => {
|
|
315
318
|
const directory = await mkdtemp(join(tmpdir(), "hl-grep-process-"));
|
|
319
|
+
const spawn = childProcess.spawn;
|
|
320
|
+
const children: ReturnType<typeof spawn>[] = [];
|
|
321
|
+
const closed = new Set<ReturnType<typeof spawn>>();
|
|
316
322
|
try {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
323
|
+
t.mock.method(childProcess, "spawn", (...args: Parameters<typeof spawn>) => {
|
|
324
|
+
const child = spawn(...args);
|
|
325
|
+
children.push(child);
|
|
326
|
+
child.once("close", () => closed.add(child));
|
|
327
|
+
return child;
|
|
328
|
+
});
|
|
329
|
+
syncBuiltinESMExports();
|
|
330
|
+
const fixture = fileURLToPath(
|
|
331
|
+
new URL("../testing/rg-line-listing.fixture.mjs", import.meta.url),
|
|
332
|
+
);
|
|
333
|
+
const processTimeoutMs = 5_000;
|
|
334
|
+
const args = [
|
|
335
|
+
fixture,
|
|
336
|
+
JSON.stringify([Buffer.from("first\0second\0").toString("base64")]),
|
|
337
|
+
"wait",
|
|
338
|
+
];
|
|
339
|
+
for (const outcome of ["limit", "callback", "cancel"] as const) {
|
|
340
|
+
const controller = new AbortController();
|
|
341
|
+
const consumed: string[] = [];
|
|
342
|
+
let aliveAtCallback = false;
|
|
343
|
+
const run = runRgPaths(
|
|
344
|
+
process.execPath,
|
|
345
|
+
args,
|
|
346
|
+
AbortSignal.any([controller.signal, AbortSignal.timeout(processTimeoutMs)]),
|
|
347
|
+
(path) => {
|
|
348
|
+
consumed.push(path);
|
|
349
|
+
const child = children.at(-1);
|
|
350
|
+
aliveAtCallback = child?.exitCode === null && child.signalCode === null;
|
|
351
|
+
if (outcome === "callback") throw new Error("callback failed");
|
|
352
|
+
if (outcome === "cancel") controller.abort();
|
|
353
|
+
return outcome !== "limit";
|
|
354
|
+
},
|
|
355
|
+
);
|
|
356
|
+
if (outcome === "limit") assert.equal((await run).stopped, true);
|
|
357
|
+
else
|
|
358
|
+
await assert.rejects(run, outcome === "callback" ? /callback failed/ : /Operation aborted/);
|
|
359
|
+
assert.deepEqual(consumed, ["first"]);
|
|
360
|
+
assert.equal(aliveAtCallback, true);
|
|
361
|
+
const child = children.at(-1);
|
|
362
|
+
assert.ok(child && closed.has(child), "runner must await child close");
|
|
363
|
+
assert.ok(child.exitCode !== null || child.signalCode !== null);
|
|
364
|
+
}
|
|
336
365
|
await assert.rejects(
|
|
337
366
|
runRgPaths(join(directory, "missing-rg"), [], undefined, () => true),
|
|
338
367
|
/Failed to run ripgrep/,
|
|
339
368
|
);
|
|
340
369
|
} finally {
|
|
370
|
+
for (const child of children) if (!closed.has(child)) child.kill("SIGKILL");
|
|
371
|
+
t.mock.restoreAll();
|
|
372
|
+
syncBuiltinESMExports();
|
|
341
373
|
await rm(directory, { recursive: true, force: true });
|
|
342
374
|
}
|
|
343
375
|
});
|
|
@@ -356,7 +388,7 @@ test("directory searches respect ignores but explicit files and hidden files rem
|
|
|
356
388
|
undefined,
|
|
357
389
|
undefined,
|
|
358
390
|
);
|
|
359
|
-
assert.equal(
|
|
391
|
+
assert.equal(grepCounts(normal).matches, 0);
|
|
360
392
|
const explicit = await invoke(
|
|
361
393
|
tool,
|
|
362
394
|
"0",
|
|
@@ -372,7 +404,7 @@ test("directory searches respect ignores but explicit files and hidden files rem
|
|
|
372
404
|
undefined,
|
|
373
405
|
undefined,
|
|
374
406
|
);
|
|
375
|
-
assert.equal(
|
|
407
|
+
assert.equal(grepCounts(excluded).matches, 0);
|
|
376
408
|
await writeFile(join(directory, ".hidden.txt"), "needle\n");
|
|
377
409
|
const hidden = await invoke(
|
|
378
410
|
tool,
|
|
@@ -410,8 +442,9 @@ test("directory traversal with caller globs → excludes .git while an explicit
|
|
|
410
442
|
pattern: "needle",
|
|
411
443
|
outputMode: "files",
|
|
412
444
|
});
|
|
413
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result)));
|
|
414
|
-
assert.equal(
|
|
445
|
+
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
446
|
+
assert.equal(grepCounts(result).matches, 1);
|
|
447
|
+
assert.equal(result.content[0].type, "text");
|
|
415
448
|
assert.equal(result.content[0].text.replace(/\\/g, "/"), expected);
|
|
416
449
|
}
|
|
417
450
|
}
|
|
@@ -442,12 +475,12 @@ test("explicit file with different case and a glob → admits the same file on a
|
|
|
442
475
|
literal: true,
|
|
443
476
|
pattern: "needle",
|
|
444
477
|
});
|
|
445
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result)));
|
|
446
|
-
assert.equal(
|
|
447
|
-
assert.equal(
|
|
478
|
+
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
479
|
+
assert.equal(grepCounts(result).matches, 1);
|
|
480
|
+
assert.equal(grepCounts(result).files, 1);
|
|
481
|
+
assert.equal(result.content[0].type, "text");
|
|
448
482
|
assert.match(result.content[0].text, /1#[0-9A-Z]+│needle/);
|
|
449
|
-
assert.equal(reportOf(result)?.diagnostics, undefined);
|
|
450
|
-
assert.equal(result.details.incomplete, undefined);
|
|
483
|
+
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
451
484
|
});
|
|
452
485
|
|
|
453
486
|
test("explicit file with a dangling sibling link and a glob → returns complete matches without listing diagnostics", async (t) => {
|
|
@@ -488,10 +521,9 @@ test("explicit file with a dangling sibling link and a glob → returns complete
|
|
|
488
521
|
pattern: "needle",
|
|
489
522
|
});
|
|
490
523
|
assert.match(listingDiagnostics, /broken\.txt/);
|
|
491
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result)));
|
|
492
|
-
assert.equal(
|
|
493
|
-
assert.equal(reportOf(result)?.diagnostics, undefined);
|
|
494
|
-
assert.equal(result.details.incomplete, undefined);
|
|
524
|
+
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
525
|
+
assert.equal(grepCounts(result).matches, 1);
|
|
526
|
+
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
495
527
|
});
|
|
496
528
|
|
|
497
529
|
test("explicit file paths obey ordered glob filters without inheriting ignore rules", async () => {
|
|
@@ -526,7 +558,7 @@ test("explicit file paths obey ordered glob filters without inheriting ignore ru
|
|
|
526
558
|
undefined,
|
|
527
559
|
undefined,
|
|
528
560
|
);
|
|
529
|
-
assert.equal(
|
|
561
|
+
assert.equal(grepCounts(none).matches, 0);
|
|
530
562
|
} finally {
|
|
531
563
|
await rm(directory, { recursive: true, force: true });
|
|
532
564
|
}
|
|
@@ -559,7 +591,7 @@ test("directory traversal skips symlinks but explicitly named linked files remai
|
|
|
559
591
|
undefined,
|
|
560
592
|
undefined,
|
|
561
593
|
);
|
|
562
|
-
assert.equal(
|
|
594
|
+
assert.equal(grepCounts(hidden).matches, 0);
|
|
563
595
|
const found: any = await invoke(
|
|
564
596
|
grep,
|
|
565
597
|
"0",
|
|
@@ -595,8 +627,8 @@ test("unbounded lookbehind → native JS matches after rg discovery", async (t)
|
|
|
595
627
|
await writeFile(join(directory, "fixture.txt"), "aaab\n");
|
|
596
628
|
const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
|
|
597
629
|
const result = await invoke(tool, "0", { pattern: "(?<=a+)b", literal: false });
|
|
598
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result)));
|
|
599
|
-
assert.equal(
|
|
630
|
+
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
631
|
+
assert.equal(grepCounts(result).matches, 1);
|
|
600
632
|
});
|
|
601
633
|
|
|
602
634
|
test("line-based grep uses context for display but never matches across CRLF", async () => {
|
|
@@ -621,7 +653,7 @@ test("line-based grep uses context for display but never matches across CRLF", a
|
|
|
621
653
|
] as const) {
|
|
622
654
|
const result = await invoke(tool, "0", { pattern, literal });
|
|
623
655
|
assert.equal(result.isError, undefined);
|
|
624
|
-
assert.equal(
|
|
656
|
+
assert.equal(grepCounts(result).matches, 0);
|
|
625
657
|
}
|
|
626
658
|
const limited = await invoke(
|
|
627
659
|
tool,
|
|
@@ -631,7 +663,7 @@ test("line-based grep uses context for display but never matches across CRLF", a
|
|
|
631
663
|
undefined,
|
|
632
664
|
);
|
|
633
665
|
assert.match(limited.content[0].text, /fixture\.txt: 1/);
|
|
634
|
-
assert.equal(reportOf(limited)?.matchLimit, 1);
|
|
666
|
+
assert.equal(reportOf(limited, "grep")?.matchLimit, 1);
|
|
635
667
|
} finally {
|
|
636
668
|
await rm(directory, { recursive: true, force: true });
|
|
637
669
|
}
|
|
@@ -684,9 +716,9 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
|
|
|
684
716
|
undefined,
|
|
685
717
|
);
|
|
686
718
|
assert.match(bounded.content[0].text, /fixture\.txt: 2/);
|
|
687
|
-
assert.equal(
|
|
688
|
-
assert.equal(
|
|
689
|
-
assert.equal(reportOf(bounded)?.matchLimit, 2);
|
|
719
|
+
assert.equal(grepCounts(bounded).matches, 2);
|
|
720
|
+
assert.equal(grepCounts(bounded).files, 1);
|
|
721
|
+
assert.equal(reportOf(bounded, "grep")?.matchLimit, 2);
|
|
690
722
|
const files = await invoke(
|
|
691
723
|
tool,
|
|
692
724
|
"0",
|
|
@@ -702,7 +734,7 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
|
|
|
702
734
|
undefined,
|
|
703
735
|
);
|
|
704
736
|
assert.equal(files.content[0].text, "fixture.txt");
|
|
705
|
-
assert.equal(reportOf(files)?.matchLimit, 1);
|
|
737
|
+
assert.equal(reportOf(files, "grep")?.matchLimit, 1);
|
|
706
738
|
const boundary = await invoke(
|
|
707
739
|
tool,
|
|
708
740
|
"0",
|
|
@@ -803,7 +835,7 @@ test("multiline zero-width EOF stays on the last existing physical line", {
|
|
|
803
835
|
undefined,
|
|
804
836
|
);
|
|
805
837
|
if (content.endsWith("\n")) {
|
|
806
|
-
assert.equal(
|
|
838
|
+
assert.equal(grepCounts(result).matches, 0);
|
|
807
839
|
} else {
|
|
808
840
|
assert.match(result.content[0].text, /fixture\.txt · 1 match/);
|
|
809
841
|
assert.match(result.content[0].text, /1#[0-9A-Z]+│last line/);
|
|
@@ -831,7 +863,7 @@ test("files and count modes aggregate only matching lines inside the limit", asy
|
|
|
831
863
|
undefined,
|
|
832
864
|
);
|
|
833
865
|
assert.match(files.content[0].text, /^(?:a|b)\.txt$/);
|
|
834
|
-
assert.equal(reportOf(files)?.matchLimit, 1);
|
|
866
|
+
assert.equal(reportOf(files, "grep")?.matchLimit, 1);
|
|
835
867
|
const limited = await invoke(
|
|
836
868
|
tool,
|
|
837
869
|
"count",
|
|
@@ -839,8 +871,8 @@ test("files and count modes aggregate only matching lines inside the limit", asy
|
|
|
839
871
|
undefined,
|
|
840
872
|
undefined,
|
|
841
873
|
);
|
|
842
|
-
assert.equal(
|
|
843
|
-
assert.equal(
|
|
874
|
+
assert.equal(grepCounts(limited).matches, 1);
|
|
875
|
+
assert.equal(grepCounts(limited).files, 1);
|
|
844
876
|
const complete = await invoke(
|
|
845
877
|
tool,
|
|
846
878
|
"count-all",
|
|
@@ -848,8 +880,8 @@ test("files and count modes aggregate only matching lines inside the limit", asy
|
|
|
848
880
|
undefined,
|
|
849
881
|
undefined,
|
|
850
882
|
);
|
|
851
|
-
assert.equal(
|
|
852
|
-
assert.equal(
|
|
883
|
+
assert.equal(grepCounts(complete).matches, 3);
|
|
884
|
+
assert.equal(grepCounts(complete).files, 2);
|
|
853
885
|
const contextual = await invoke(
|
|
854
886
|
tool,
|
|
855
887
|
"content",
|
|
@@ -881,8 +913,8 @@ test("real rg accepts wildcard-only regexes and preserves limits and literal mod
|
|
|
881
913
|
undefined,
|
|
882
914
|
undefined,
|
|
883
915
|
);
|
|
884
|
-
assert.equal(
|
|
885
|
-
assert.equal(
|
|
916
|
+
assert.equal(grepCounts(result).matches, expected);
|
|
917
|
+
assert.equal(grepCounts(result).files, 1);
|
|
886
918
|
}
|
|
887
919
|
const limited: any = await invoke(
|
|
888
920
|
tool,
|
|
@@ -945,17 +977,14 @@ test("tools share physical lines and anchors across text representations", async
|
|
|
945
977
|
edits: [{ op: "replace", anchor, body: ["new"] }],
|
|
946
978
|
});
|
|
947
979
|
assert.equal(await readFile(file, "utf8"), before.replace("old", "new"));
|
|
948
|
-
assert.equal(edit.details.firstChangedLine, 2);
|
|
949
980
|
assert.match(edit.details.diff, /^-2 old/m);
|
|
950
981
|
assert.match(edit.details.diff, /^\+2 new/m);
|
|
951
982
|
assert.ok(!edit.details.diff.includes("\r"));
|
|
952
|
-
|
|
953
|
-
const replaced = await call(makeReplaceTool(directory, DEFAULT_CONFIG), {
|
|
983
|
+
await call(makeReplaceTool(directory, DEFAULT_CONFIG), {
|
|
954
984
|
path: file,
|
|
955
|
-
replacements: [{ find: "new", replace: "next" }],
|
|
985
|
+
replacements: [{ literal: true, find: "new", replace: "next" }],
|
|
956
986
|
});
|
|
957
987
|
assert.equal(await readFile(file, "utf8"), before.replace("old", "next"));
|
|
958
|
-
assert.equal(replaced.details.firstChangedLine, 2);
|
|
959
988
|
}
|
|
960
989
|
} finally {
|
|
961
990
|
await rm(directory, { recursive: true, force: true });
|
|
@@ -991,7 +1020,10 @@ test("long-line previews preserve full-line anchors across literal and JavaScrip
|
|
|
991
1020
|
`3#${computeLineHash(3, context, 4)}│[partial, columns 1-${DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits - 1}]`,
|
|
992
1021
|
),
|
|
993
1022
|
);
|
|
994
|
-
assert.equal(
|
|
1023
|
+
assert.equal(
|
|
1024
|
+
reportOf(result, "grep")?.linePreviewLimit,
|
|
1025
|
+
DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits,
|
|
1026
|
+
);
|
|
995
1027
|
}
|
|
996
1028
|
} finally {
|
|
997
1029
|
await rm(directory, { recursive: true, force: true });
|
|
@@ -1094,7 +1126,7 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
|
|
|
1094
1126
|
{
|
|
1095
1127
|
find: mode === "regex" ? "alpha\\nbeta" : "alpha\r\nbeta",
|
|
1096
1128
|
replace: "A\nB\nC",
|
|
1097
|
-
|
|
1129
|
+
literal: mode !== "regex",
|
|
1098
1130
|
},
|
|
1099
1131
|
],
|
|
1100
1132
|
},
|
|
@@ -1159,7 +1191,7 @@ test("visible source escapes remain readable and searchable while real CRLF stay
|
|
|
1159
1191
|
await invoke(
|
|
1160
1192
|
replace,
|
|
1161
1193
|
"literal",
|
|
1162
|
-
{ path, replacements: [{ find: String.raw`\r\n`, replace: String.raw`\n` }] },
|
|
1194
|
+
{ path, replacements: [{ literal: true, find: String.raw`\r\n`, replace: String.raw`\n` }] },
|
|
1163
1195
|
undefined,
|
|
1164
1196
|
undefined,
|
|
1165
1197
|
{ cwd: directory } as ExtensionContext,
|
|
@@ -1168,7 +1200,10 @@ test("visible source escapes remain readable and searchable while real CRLF stay
|
|
|
1168
1200
|
await invoke(
|
|
1169
1201
|
replace,
|
|
1170
1202
|
"regex",
|
|
1171
|
-
{
|
|
1203
|
+
{
|
|
1204
|
+
path,
|
|
1205
|
+
replacements: [{ find: String.raw`\\n`, replace: String.raw`\r\n`, literal: false }],
|
|
1206
|
+
},
|
|
1172
1207
|
undefined,
|
|
1173
1208
|
undefined,
|
|
1174
1209
|
{ cwd: directory } as ExtensionContext,
|
|
@@ -1197,7 +1232,8 @@ test("grep retains original paths and ignores unmatched invalid UTF-8 across man
|
|
|
1197
1232
|
assert.equal((result.content[0].text.match(/file \d+\.txt: 1/g) ?? []).length, 70);
|
|
1198
1233
|
assert.doesNotMatch(result.content[0].text, /invalid\.txt|UNSUPPORTED_ENCODING/);
|
|
1199
1234
|
assert.doesNotMatch(result.content[0].text, /hashline-grep-/);
|
|
1200
|
-
assert.equal(result
|
|
1235
|
+
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1236
|
+
assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
|
|
1201
1237
|
} finally {
|
|
1202
1238
|
await rm(directory, { recursive: true, force: true });
|
|
1203
1239
|
}
|
|
@@ -1224,8 +1260,8 @@ test("NUL files are silently skipped across search scopes and output modes", asy
|
|
|
1224
1260
|
undefined,
|
|
1225
1261
|
undefined,
|
|
1226
1262
|
);
|
|
1227
|
-
assert.equal(
|
|
1228
|
-
assert.equal(reportOf(skipped)?.diagnostics, undefined);
|
|
1263
|
+
assert.equal(grepCounts(skipped).matches, 0);
|
|
1264
|
+
assert.equal(reportOf(skipped, "grep")?.diagnostics, undefined);
|
|
1229
1265
|
const result = await invoke(
|
|
1230
1266
|
grep,
|
|
1231
1267
|
"directory",
|
|
@@ -1235,7 +1271,8 @@ test("NUL files are silently skipped across search scopes and output modes", asy
|
|
|
1235
1271
|
);
|
|
1236
1272
|
assert.match(result.content[0].text, /valid\.txt/);
|
|
1237
1273
|
assert.doesNotMatch(result.content[0].text, /binary\.txt|UNSUPPORTED_TEXT/);
|
|
1238
|
-
assert.equal(result
|
|
1274
|
+
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1275
|
+
assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
|
|
1239
1276
|
}
|
|
1240
1277
|
}
|
|
1241
1278
|
assert.deepEqual(await readFile(binary), source);
|
|
@@ -1264,9 +1301,10 @@ test("invalid UTF-8 matches use plain previews while valid files retain edit anc
|
|
|
1264
1301
|
undefined,
|
|
1265
1302
|
);
|
|
1266
1303
|
assert.match(result.content[0].text, /1│before\n2│needle �\n3│after/);
|
|
1267
|
-
assert.deepEqual(reportOf(result)?.invalidUtf8, ["invalid.txt"]);
|
|
1304
|
+
assert.deepEqual(reportOf(result, "grep")?.invalidUtf8, ["invalid.txt"]);
|
|
1268
1305
|
assert.doesNotMatch(result.content[0].text, /\d+#/);
|
|
1269
|
-
assert.equal(result
|
|
1306
|
+
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1307
|
+
assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
|
|
1270
1308
|
assert.deepEqual(await readFile(invalid), source);
|
|
1271
1309
|
}
|
|
1272
1310
|
await writeFile(join(directory, "valid.txt"), "needle\n");
|
|
@@ -1289,7 +1327,8 @@ test("invalid UTF-8 matches use plain previews while valid files retain edit anc
|
|
|
1289
1327
|
);
|
|
1290
1328
|
assert.match(result.content[0].text, /invalid\.txt/);
|
|
1291
1329
|
assert.match(result.content[0].text, /valid\.txt/);
|
|
1292
|
-
assert.equal(result
|
|
1330
|
+
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1331
|
+
assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
|
|
1293
1332
|
}
|
|
1294
1333
|
const multiline = await invoke(
|
|
1295
1334
|
grep,
|
|
@@ -1299,7 +1338,7 @@ test("invalid UTF-8 matches use plain previews while valid files retain edit anc
|
|
|
1299
1338
|
undefined,
|
|
1300
1339
|
);
|
|
1301
1340
|
// The explicit class excludes the replacement character in the lossy view.
|
|
1302
|
-
assert.equal(
|
|
1341
|
+
assert.equal(grepCounts(multiline).matches, 0);
|
|
1303
1342
|
} finally {
|
|
1304
1343
|
await rm(directory, { recursive: true, force: true });
|
|
1305
1344
|
}
|
|
@@ -1382,10 +1421,11 @@ test("literal LF and CRLF queries match original valid and invalid UTF-8 files a
|
|
|
1382
1421
|
assert.match(result.content[0].text, /1│alpha\n2│beta/);
|
|
1383
1422
|
assert.match(result.content[0].text, /1#[0-9A-Z]+│alpha\n2#[0-9A-Z]+│beta/);
|
|
1384
1423
|
} else if (outputMode === "count") {
|
|
1385
|
-
assert.equal(
|
|
1386
|
-
assert.equal(
|
|
1424
|
+
assert.equal(grepCounts(result).matches, 4);
|
|
1425
|
+
assert.equal(grepCounts(result).files, 2);
|
|
1387
1426
|
}
|
|
1388
|
-
assert.equal(result
|
|
1427
|
+
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1428
|
+
assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
|
|
1389
1429
|
const lf = await invoke(grep, "lf", {
|
|
1390
1430
|
path: directory,
|
|
1391
1431
|
pattern: "alpha\nbeta",
|
|
@@ -3,15 +3,37 @@ import { mkdtemp, rm, truncate, writeFile } from "node:fs/promises";
|
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { test } from "node:test";
|
|
6
|
-
import {
|
|
6
|
+
import { formatByteSize } from "../pi/budgets.ts";
|
|
7
7
|
import { DEFAULT_CONFIG } from "../pi/config.ts";
|
|
8
8
|
import { MAX_MULTILINE_SEARCH_BYTES } from "../pi/grep-search.ts";
|
|
9
9
|
import { MAX_UNNARROWED_SCAN_BYTES } from "../pi/grep-candidates.ts";
|
|
10
10
|
import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
|
|
11
11
|
import { OUTCOME_NEXT } from "../pi/report-schema.ts";
|
|
12
12
|
import { reportOf } from "../pi/report.ts";
|
|
13
|
+
import { grepCounts } from "../pi/grep-output.testing.ts";
|
|
13
14
|
import { callTool } from "../pi/tool-call.testing.ts";
|
|
14
15
|
|
|
16
|
+
test("repeated all-file candidates → count each file once toward the scan budget", async (t) => {
|
|
17
|
+
const directory = await mkdtemp(join(tmpdir(), "hl-grep-repeated-scan-"));
|
|
18
|
+
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
19
|
+
const padding = join(directory, "a.bin");
|
|
20
|
+
const fixture = join(directory, "b.txt");
|
|
21
|
+
await writeFile(padding, "");
|
|
22
|
+
await truncate(padding, MAX_MULTILINE_SEARCH_BYTES);
|
|
23
|
+
await writeFile(fixture, "ab\n");
|
|
24
|
+
const repeats = Math.ceil(MAX_UNNARROWED_SCAN_BYTES / MAX_MULTILINE_SEARCH_BYTES);
|
|
25
|
+
const result = await callTool(makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {}), {
|
|
26
|
+
path: [...Array<string>(repeats).fill(padding), fixture],
|
|
27
|
+
pattern: ".",
|
|
28
|
+
literal: false,
|
|
29
|
+
multiline: true,
|
|
30
|
+
outputMode: "files",
|
|
31
|
+
});
|
|
32
|
+
assert.equal(result.isError, undefined);
|
|
33
|
+
assert.deepEqual(result.content[0], { type: "text", text: "b.txt" });
|
|
34
|
+
assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
|
|
35
|
+
});
|
|
36
|
+
|
|
15
37
|
test("all-file candidates exceed the scan budget → stop listing and retain scan facts with or without matches", async (t) => {
|
|
16
38
|
const directory = await mkdtemp(join(tmpdir(), "hl-grep-scan-budget-"));
|
|
17
39
|
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
@@ -68,20 +90,20 @@ test("all-file candidates exceed the scan budget → stop listing and retain sca
|
|
|
68
90
|
});
|
|
69
91
|
assert.equal(stopped, true);
|
|
70
92
|
assert.deepEqual(offered, [fixture, ...padding, blocked]);
|
|
71
|
-
const report = reportOf(result);
|
|
93
|
+
const report = reportOf(result, "grep");
|
|
72
94
|
assert.ok(report);
|
|
73
95
|
assert.equal(report.candidates, "all");
|
|
74
|
-
assert.equal(report.scanLimit,
|
|
96
|
+
assert.equal(report.scanLimit, formatByteSize(MAX_UNNARROWED_SCAN_BYTES));
|
|
75
97
|
if (scenario.stderr) {
|
|
76
98
|
assert.equal(result.isError, true);
|
|
77
99
|
assert.equal(report.error, "SEARCH_INCOMPLETE");
|
|
78
100
|
assert.match(report.diagnostics ?? "", /listing interrupted/);
|
|
79
101
|
} else {
|
|
80
102
|
assert.equal(result.isError, undefined);
|
|
81
|
-
assert.equal(
|
|
103
|
+
assert.equal(grepCounts(result).matches, scenario.matches);
|
|
82
104
|
assert.equal(report.diagnostics, undefined);
|
|
83
|
-
assert.equal(result.details.incomplete, true);
|
|
84
105
|
assert.equal(report.next, scanLimitNext);
|
|
106
|
+
assert.equal(result.content[0].type, "text");
|
|
85
107
|
if (scenario.matches) assert.match(result.content[0].text, /fixture\.txt/);
|
|
86
108
|
}
|
|
87
109
|
});
|
|
@@ -5,7 +5,7 @@ import { tmpdir } from "node:os";
|
|
|
5
5
|
import { join } from "node:path";
|
|
6
6
|
import { makeReadOverride } from "../pi/read-tool.ts";
|
|
7
7
|
import { makeEditOverride } from "../pi/edit-tool.ts";
|
|
8
|
-
import {
|
|
8
|
+
import { reportOf } from "../pi/report.ts";
|
|
9
9
|
import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
|
|
10
10
|
import { computeLineHash } from "../core/hash.ts";
|
|
11
11
|
import { callTool } from "../pi/tool-call.testing.ts";
|
|
@@ -110,8 +110,7 @@ test("copy/move a 150,000-line block → publish every captured line", async (t)
|
|
|
110
110
|
"utf8",
|
|
111
111
|
);
|
|
112
112
|
assert.deepEqual(await readFile(path), expected);
|
|
113
|
-
assert.
|
|
114
|
-
assert.equal(result
|
|
115
|
-
assert.equal(result.details.publication, "PUBLISHED");
|
|
113
|
+
assert.equal(result.isError, undefined);
|
|
114
|
+
assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
|
|
116
115
|
}
|
|
117
116
|
});
|
|
@@ -69,24 +69,33 @@ test("model capacity changes → read and grep grow and shrink without re-regist
|
|
|
69
69
|
const ctx = session.extensionRunner.createToolContext("read-budget", undefined);
|
|
70
70
|
await session.setModel({ ...model, id: "small", contextWindow: 65_536 });
|
|
71
71
|
const small = await callTool(tool, { path }, { ctx });
|
|
72
|
-
|
|
72
|
+
const smallReport = reportOf(small, "read");
|
|
73
|
+
assert.ok(smallReport?.lineTooLong);
|
|
73
74
|
await session.setModel({ ...model, id: "large", contextWindow: 1_048_576 });
|
|
74
75
|
const large = await callTool(tool, { path }, { ctx });
|
|
75
|
-
|
|
76
|
+
const largeReport = reportOf(large, "read");
|
|
77
|
+
assert.ok(largeReport);
|
|
78
|
+
assert.equal(largeReport.lineTooLong, undefined);
|
|
79
|
+
assert.equal(large.content[0].type, "text");
|
|
76
80
|
assert.ok(large.content[0].text.includes(text));
|
|
77
81
|
await session.setModel({ ...model, id: "small", contextWindow: 65_536 });
|
|
78
82
|
const smallAgain = await callTool(tool, { path }, { ctx });
|
|
79
|
-
assert.deepEqual(smallAgain
|
|
83
|
+
assert.deepEqual(reportOf(smallAgain, "read")?.lineTooLong, smallReport.lineTooLong);
|
|
80
84
|
const grep = session.getToolDefinition("grep");
|
|
81
85
|
assert.ok(grep);
|
|
82
86
|
const smallSearch = await callTool(grep, { path, pattern: "中文", literal: true }, { ctx });
|
|
83
87
|
await session.setModel({ ...model, id: "large", contextWindow: 1_048_576 });
|
|
84
88
|
const largeSearch = await callTool(grep, { path, pattern: "中文", literal: true }, { ctx });
|
|
85
|
-
assert.equal(reportOf(smallSearch)?.linePreviewLimit, outputBudgetsFor(65_536).grepPreviewUnits);
|
|
86
89
|
assert.equal(
|
|
87
|
-
reportOf(
|
|
90
|
+
reportOf(smallSearch, "grep")?.linePreviewLimit,
|
|
91
|
+
outputBudgetsFor(65_536).grepPreviewUnits,
|
|
92
|
+
);
|
|
93
|
+
assert.equal(
|
|
94
|
+
reportOf(largeSearch, "grep")?.linePreviewLimit,
|
|
88
95
|
outputBudgetsFor(1_048_576).grepPreviewUnits,
|
|
89
96
|
);
|
|
97
|
+
assert.equal(largeSearch.content[0].type, "text");
|
|
98
|
+
assert.equal(smallSearch.content[0].type, "text");
|
|
90
99
|
assert.ok(largeSearch.content[0].text.length > smallSearch.content[0].text.length);
|
|
91
100
|
assert.ok(!largeSearch.content[0].text.includes("\uFFFD"));
|
|
92
101
|
});
|
|
@@ -115,7 +124,7 @@ test("model switches before argument preparation → rejection uses the new budg
|
|
|
115
124
|
assert.ok(error instanceof Error);
|
|
116
125
|
const budget = outputBudgetsFor(contextWindow);
|
|
117
126
|
assert.ok(Buffer.byteLength(error.message) <= budget.blockBytes);
|
|
118
|
-
const report = decodeReport(error.message);
|
|
127
|
+
const report = decodeReport(error.message, "edit");
|
|
119
128
|
assert.ok(report?.issues);
|
|
120
129
|
assert.equal(report.issues.length + (report.omittedIssues ?? 0), args.edits.length);
|
|
121
130
|
reports.push(report);
|
|
@@ -16,7 +16,7 @@ const cases = [
|
|
|
16
16
|
},
|
|
17
17
|
{
|
|
18
18
|
tool: makeReplaceTool(directory, DEFAULT_CONFIG),
|
|
19
|
-
args: { replacements: [{ find: "before", replace: "after" }] },
|
|
19
|
+
args: { replacements: [{ literal: true, find: "before", replace: "after" }] },
|
|
20
20
|
},
|
|
21
21
|
];
|
|
22
22
|
for (const path of ["pipe", "alias"]) {
|