@criogaid/pi-hashline-edit 0.13.0 → 0.15.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 +18 -12
- package/README.zh-CN.md +18 -12
- package/package.json +8 -1
- package/src/core/AGENTS.md +3 -1
- package/src/core/apply.ts +110 -117
- package/src/core/character-set.ts +2 -3
- package/src/core/copy-span.ts +15 -0
- package/src/core/errors.ts +24 -9
- package/src/core/lines.ts +71 -17
- package/src/core/ranges.ts +5 -4
- package/src/core/replace-schema.ts +3 -1
- package/src/core/replace.ts +50 -29
- package/src/core/search-analysis.ts +21 -22
- package/src/core/search.ts +15 -32
- package/src/core/types.ts +3 -0
- package/src/index.ts +47 -39
- package/src/pi/AGENTS.md +30 -19
- package/src/pi/action-fusion.ts +284 -211
- package/src/pi/argument-diagnostics.ts +50 -37
- package/src/pi/argument-error.ts +2 -2
- package/src/pi/argument-validation.ts +163 -66
- package/src/pi/budgets.ts +12 -0
- package/src/pi/diagnostic-buffer.ts +12 -11
- package/src/pi/edit-tool.ts +138 -115
- package/src/pi/error-text.ts +10 -2
- package/src/pi/failure-context.ts +24 -37
- package/src/pi/file-commit.ts +131 -164
- package/src/pi/file-read.ts +122 -31
- package/src/pi/fusion-arguments.ts +214 -0
- package/src/pi/fusion-card.ts +102 -60
- package/src/pi/grep-candidates.ts +35 -22
- package/src/pi/grep-output.ts +197 -190
- package/src/pi/grep-render.ts +22 -13
- package/src/pi/grep-reread.ts +7 -6
- package/src/pi/grep-scope.ts +31 -16
- package/src/pi/grep-search.ts +56 -47
- package/src/pi/grep-tool.ts +35 -33
- package/src/pi/grep-worker.ts +3 -4
- package/src/pi/mutation-diff.ts +238 -0
- package/src/pi/mutation-result.ts +102 -79
- package/src/pi/mutation-runner.ts +107 -84
- package/src/pi/native-bash.ts +41 -0
- package/src/pi/path.ts +59 -6
- package/src/pi/read-tool.ts +173 -138
- package/src/pi/regex-worker.ts +14 -8
- package/src/pi/render.ts +101 -54
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +14 -7
- package/src/pi/report-cause.ts +34 -17
- package/src/pi/report-schema.ts +184 -85
- package/src/pi/report.ts +133 -64
- package/src/pi/rg-process.ts +68 -48
- package/src/pi/schema.ts +4 -0
- package/src/pi/text-stream.ts +81 -87
- package/src/pi/workspace-cwd.js +2 -0
- package/src/pi/workspace.ts +46 -0
- package/src/pi/write-tool.ts +19 -13
- package/src/core/apply.test.ts +0 -644
- package/src/core/case-fold.test.ts +0 -35
- package/src/core/character-set.test.ts +0 -97
- package/src/core/errors.test.ts +0 -39
- package/src/core/hash.test.ts +0 -64
- package/src/core/lines.test.ts +0 -63
- package/src/core/ranges.test.ts +0 -123
- package/src/core/replace.test.ts +0 -187
- package/src/core/search-analysis.test.ts +0 -210
- package/src/core/search-matrix.test.ts +0 -64
- package/src/core/search-matrix.testing.ts +0 -171
- package/src/core/search.test.ts +0 -132
- package/src/core/text.test.ts +0 -52
- package/src/integration/config-registration.test.ts +0 -291
- package/src/integration/fusion-dispatch.test.ts +0 -320
- package/src/integration/grep-errors.test.ts +0 -81
- package/src/integration/grep-flags.test.ts +0 -76
- package/src/integration/grep-js-completeness.test.ts +0 -278
- package/src/integration/grep-line-candidates.test.ts +0 -201
- package/src/integration/grep-matrix.test.ts +0 -118
- package/src/integration/grep-order.test.ts +0 -78
- package/src/integration/grep-path-matrix.test.ts +0 -128
- package/src/integration/grep-prefilter-coverage.test.ts +0 -204
- package/src/integration/grep-process-limits.test.ts +0 -122
- package/src/integration/grep-regex.test.ts +0 -412
- package/src/integration/grep-rg.test.ts +0 -1659
- package/src/integration/grep-scan-budget.test.ts +0 -111
- package/src/integration/grep-translation.test.ts +0 -199
- package/src/integration/large-text.test.ts +0 -70
- package/src/integration/model-budget-lifecycle.test.ts +0 -186
- package/src/integration/mutation-file-types.test.ts +0 -30
- package/src/integration/mutation-file-types.testing.ts +0 -33
- package/src/integration/npm-package.test.ts +0 -19
- package/src/integration/npm-package.testing.ts +0 -243
- package/src/integration/publication-read-changes.test.ts +0 -454
- package/src/integration/read-changes.test.ts +0 -98
- package/src/integration/replace-package.test.ts +0 -22
- package/src/integration/replace-package.testing.ts +0 -36
- package/src/integration/replace-worker-boundary.test.ts +0 -127
- package/src/integration/report-replay.test.ts +0 -91
- package/src/integration/utf8-errors.test.ts +0 -46
- package/src/pi/action-fusion.test.ts +0 -659
- package/src/pi/action-fusion.testing.ts +0 -38
- package/src/pi/anchor-format.test.ts +0 -124
- package/src/pi/argument-error.testing.ts +0 -46
- package/src/pi/argument-format.test.ts +0 -196
- package/src/pi/argument-validation.test.ts +0 -1007
- package/src/pi/codemode.test.ts +0 -101
- package/src/pi/config.test.ts +0 -204
- package/src/pi/diagnostic-buffer.test.ts +0 -115
- package/src/pi/directory-sync.test.ts +0 -57
- package/src/pi/execute.test.ts +0 -1257
- package/src/pi/extension-loading.test.ts +0 -14
- package/src/pi/extension-loading.testing.ts +0 -3
- package/src/pi/failure-context.test.ts +0 -370
- package/src/pi/failure-path.test.ts +0 -289
- package/src/pi/file-commit.test.ts +0 -628
- package/src/pi/file-read.test.ts +0 -29
- package/src/pi/file-snapshot.test.ts +0 -22
- package/src/pi/fusion-card-interaction.test.ts +0 -143
- package/src/pi/fusion-card.test.ts +0 -353
- package/src/pi/grep-candidates.test.ts +0 -26
- package/src/pi/grep-matrix.test.ts +0 -68
- package/src/pi/grep-order.test.ts +0 -447
- package/src/pi/grep-output.testing.ts +0 -24
- package/src/pi/grep-rg.testing.ts +0 -73
- package/src/pi/grep.test.ts +0 -1670
- package/src/pi/model-budgets.test.ts +0 -96
- package/src/pi/mutation-outcome.testing.ts +0 -20
- package/src/pi/mutation-result.test.ts +0 -96
- package/src/pi/output-budgets.test.ts +0 -221
- package/src/pi/pi.test.ts +0 -158
- package/src/pi/read-delegation.test.ts +0 -119
- package/src/pi/read.test.ts +0 -735
- package/src/pi/regex-worker.test.ts +0 -42
- package/src/pi/registration.test.ts +0 -549
- package/src/pi/render.test.ts +0 -841
- package/src/pi/replace.test.ts +0 -634
- package/src/pi/report-failure.testing.ts +0 -27
- package/src/pi/report.test.ts +0 -533
- package/src/pi/result-safety.test.ts +0 -597
- package/src/pi/rg-process.test.ts +0 -133
- package/src/pi/rg-regex.test.ts +0 -40
- package/src/pi/text-stream.test.ts +0 -281
- package/src/pi/tool-call.testing.ts +0 -61
- package/src/pi/tool-factories.testing.ts +0 -59
- package/src/pi/write.test.ts +0 -256
- package/src/testing/extension-loader.testing.ts +0 -24
- package/src/testing/installation.testing.ts +0 -77
- package/src/testing/process.testing.ts +0 -33
- package/src/testing/regex-host.testing.ts +0 -20
- package/src/testing/regex-translation-corpus.ts +0 -267
- package/src/testing/rg-line-listing.fixture.mjs +0 -7
- package/src/testing/run-tests.ts +0 -47
- package/src/testing/session.testing.ts +0 -127
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { test } from "node:test";
|
|
6
|
-
import { computeLineHash } from "../core/hash.ts";
|
|
7
|
-
const { DEFAULT_CONFIG, makeGrepOverride } = await import("../pi/tool-factories.testing.ts");
|
|
8
|
-
const { parseFileHeader } = await import("../pi/grep-output.ts");
|
|
9
|
-
|
|
10
|
-
import { reportOf } from "../pi/report.ts";
|
|
11
|
-
import { callTool } from "../pi/tool-call.testing.ts";
|
|
12
|
-
|
|
13
|
-
test("repeated real-rg query with increasing limits → stable traversal order and a growing row prefix", async (t) => {
|
|
14
|
-
const dir = await mkdtemp(join(tmpdir(), "hl-grep-order-integration-"));
|
|
15
|
-
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
16
|
-
await mkdir(join(dir, "sub"));
|
|
17
|
-
await writeFile(join(dir, ".ignore"), "a-ignored.txt\n");
|
|
18
|
-
const names = ["z.txt", "sub/b.txt", "sub.txt", "a.txt", "sub/a-ignored.txt"];
|
|
19
|
-
const sourceLine = (name: string, line: number) => `needle ${name} ${line}`;
|
|
20
|
-
for (const name of names) {
|
|
21
|
-
await writeFile(join(dir, name), `${sourceLine(name, 1)}\n${sourceLine(name, 2)}\n`);
|
|
22
|
-
}
|
|
23
|
-
const tool = makeGrepOverride(dir, DEFAULT_CONFIG);
|
|
24
|
-
for (const [roots, expectedNames] of [
|
|
25
|
-
[["."], ["a.txt", "sub/b.txt", "sub.txt", "z.txt"]],
|
|
26
|
-
[
|
|
27
|
-
["z.txt", "sub", "a.txt"],
|
|
28
|
-
["z.txt", "sub/b.txt", "a.txt"],
|
|
29
|
-
],
|
|
30
|
-
[
|
|
31
|
-
["sub", ".", "sub/a-ignored.txt"],
|
|
32
|
-
["sub/b.txt", "a.txt", "sub.txt", "z.txt", "sub/a-ignored.txt"],
|
|
33
|
-
],
|
|
34
|
-
[
|
|
35
|
-
["sub/a-ignored.txt", ".", "a.txt"],
|
|
36
|
-
["sub/a-ignored.txt", "a.txt", "sub/b.txt", "sub.txt", "z.txt"],
|
|
37
|
-
],
|
|
38
|
-
]) {
|
|
39
|
-
const expected = expectedNames.flatMap((name) =>
|
|
40
|
-
[1, 2].map((line) => {
|
|
41
|
-
const source = sourceLine(name, line);
|
|
42
|
-
return `${name}\n${line}#${computeLineHash(line, source, DEFAULT_CONFIG.hashLen)}│${source}`;
|
|
43
|
-
}),
|
|
44
|
-
);
|
|
45
|
-
for (const multiline of [false, true]) {
|
|
46
|
-
await t.test(JSON.stringify({ roots, multiline }), async () => {
|
|
47
|
-
let previous: string[] = [];
|
|
48
|
-
for (const limit of [1, 3, expected.length]) {
|
|
49
|
-
const query = {
|
|
50
|
-
path: roots.map((root) => join(dir, root)),
|
|
51
|
-
pattern: "needle",
|
|
52
|
-
literal: true,
|
|
53
|
-
multiline,
|
|
54
|
-
limit,
|
|
55
|
-
};
|
|
56
|
-
const first = await callTool(tool, query);
|
|
57
|
-
const second = await callTool(tool, query);
|
|
58
|
-
for (const result of [first, second]) {
|
|
59
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
60
|
-
}
|
|
61
|
-
assert.deepEqual(first.content, second.content);
|
|
62
|
-
const body = first.content[0];
|
|
63
|
-
assert.ok(body.type === "text");
|
|
64
|
-
const rows: string[] = [];
|
|
65
|
-
let path = "";
|
|
66
|
-
for (const line of body.text.split("\n")) {
|
|
67
|
-
const header = parseFileHeader(line);
|
|
68
|
-
if (header) path = header.path;
|
|
69
|
-
else if (/^\d+#[0-9A-Z]+│/.test(line)) rows.push(`${path}\n${line}`);
|
|
70
|
-
}
|
|
71
|
-
assert.deepEqual(rows, expected.slice(0, limit));
|
|
72
|
-
assert.deepEqual(rows.slice(0, previous.length), previous);
|
|
73
|
-
previous = rows;
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
});
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { makeMockGrep } from "../pi/grep-rg.testing.ts";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { test } from "node:test";
|
|
4
|
-
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
5
|
-
import { tmpdir } from "node:os";
|
|
6
|
-
import { basename, join } from "node:path";
|
|
7
|
-
import { searchText } from "../core/search.ts";
|
|
8
|
-
const { DEFAULT_CONFIG } = await import("../pi/tool-factories.testing.ts");
|
|
9
|
-
|
|
10
|
-
import { reportOf } from "../pi/report.ts";
|
|
11
|
-
const { grepCounts } = await import("../pi/grep-output.testing.ts");
|
|
12
|
-
import { runRgLines, runRgPaths } from "../pi/rg-process.ts";
|
|
13
|
-
import { callTool } from "../pi/tool-call.testing.ts";
|
|
14
|
-
import { acceptsNative } from "../core/search-matrix.testing.ts";
|
|
15
|
-
import { rejectsArgument } from "../pi/argument-error.testing.ts";
|
|
16
|
-
|
|
17
|
-
test("Cartesian literal mode × case override × line mode × bytes → the real grep path agrees with JS", async (t) => {
|
|
18
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-path-matrix-"));
|
|
19
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
20
|
-
const source = "a.b\naxb\n[ab]\na|b\n-e\n\\K\nK\na\nb\na�b\n😀\nk\nK\nK\ns\nS\nſ\n";
|
|
21
|
-
const inputs = [
|
|
22
|
-
{ name: "lf.txt", bytes: Buffer.from(source) },
|
|
23
|
-
{ name: "crlf.txt", bytes: Buffer.from(source.replaceAll("\n", "\r\n")) },
|
|
24
|
-
{
|
|
25
|
-
name: "malformed.txt",
|
|
26
|
-
bytes: Buffer.concat([Buffer.from([0x61, 0xff, 0x62, 0x0d, 0x0a]), Buffer.from(source)]),
|
|
27
|
-
},
|
|
28
|
-
];
|
|
29
|
-
for (const input of inputs) await writeFile(join(directory, input.name), input.bytes);
|
|
30
|
-
const limit = inputs.reduce((sum, input) => sum + input.bytes.length, 0);
|
|
31
|
-
const candidates = new Map<string, readonly number[] | undefined>();
|
|
32
|
-
const tool = makeMockGrep(directory, DEFAULT_CONFIG, {
|
|
33
|
-
runRgPaths(args, signal, onPath) {
|
|
34
|
-
return runRgPaths(args, signal, (path) => {
|
|
35
|
-
candidates.set(basename(path), undefined);
|
|
36
|
-
return onPath(path);
|
|
37
|
-
});
|
|
38
|
-
},
|
|
39
|
-
runRgLines(args, signal, maxLines, onFile) {
|
|
40
|
-
return runRgLines(args, signal, maxLines, (path, lines) => {
|
|
41
|
-
candidates.set(basename(path), lines);
|
|
42
|
-
return onFile(path, lines);
|
|
43
|
-
});
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
let calls = 0;
|
|
47
|
-
let rejected = 0;
|
|
48
|
-
let nativeMatchingFiles = 0;
|
|
49
|
-
let extraCandidates = 0;
|
|
50
|
-
const patterns = ["a.b", "\\K", "a\nb", "a�b", "😀", "\uD83D", "k", "s", "[ab]", "a|b", "-e"];
|
|
51
|
-
for (const pattern of patterns) {
|
|
52
|
-
for (const literal of [false, true]) {
|
|
53
|
-
for (const ignoreCase of [false, true]) {
|
|
54
|
-
for (const multiline of [false, true]) {
|
|
55
|
-
const params = {
|
|
56
|
-
pattern,
|
|
57
|
-
literal,
|
|
58
|
-
ignoreCase,
|
|
59
|
-
multiline,
|
|
60
|
-
outputMode: "count",
|
|
61
|
-
limit,
|
|
62
|
-
};
|
|
63
|
-
const query = { patterns: [pattern], literal, ignoreCase, multiline };
|
|
64
|
-
const context = JSON.stringify(params);
|
|
65
|
-
candidates.clear();
|
|
66
|
-
if (!literal && !acceptsNative(query)) {
|
|
67
|
-
await assert.rejects(callTool(tool, params), rejectsArgument("pattern[0]"), context);
|
|
68
|
-
assert.equal(candidates.size, 0, context);
|
|
69
|
-
rejected++;
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
const expected = inputs
|
|
73
|
-
.map((input) => ({
|
|
74
|
-
name: input.name,
|
|
75
|
-
rows: searchText({ source: input.bytes.toString("utf8"), query, limit }).lines,
|
|
76
|
-
}))
|
|
77
|
-
.filter((input) => input.rows.length > 0);
|
|
78
|
-
const actual = await callTool(tool, params);
|
|
79
|
-
calls++;
|
|
80
|
-
assert.equal(
|
|
81
|
-
actual.isError,
|
|
82
|
-
undefined,
|
|
83
|
-
`${context}: ${JSON.stringify(reportOf(actual, "grep"))}`,
|
|
84
|
-
);
|
|
85
|
-
assert.equal(
|
|
86
|
-
grepCounts(actual).matches,
|
|
87
|
-
expected.reduce((sum, input) => sum + input.rows.length, 0),
|
|
88
|
-
context,
|
|
89
|
-
);
|
|
90
|
-
assert.equal(grepCounts(actual).files, expected.length, context);
|
|
91
|
-
for (const input of expected) {
|
|
92
|
-
nativeMatchingFiles++;
|
|
93
|
-
assert.ok(candidates.has(input.name), `${context}: rg dropped ${input.name}`);
|
|
94
|
-
const listed = candidates.get(input.name);
|
|
95
|
-
for (const row of input.rows) {
|
|
96
|
-
assert.ok(
|
|
97
|
-
listed === undefined || listed.includes(row.lineNumber),
|
|
98
|
-
`${context}: rg dropped ${input.name}:${row.lineNumber}`,
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
extraCandidates += [...candidates.keys()].filter(
|
|
103
|
-
(name) => !expected.some((input) => input.name === name),
|
|
104
|
-
).length;
|
|
105
|
-
if (expected.length) {
|
|
106
|
-
const body = actual.content[0];
|
|
107
|
-
assert.ok(body.type === "text");
|
|
108
|
-
assert.deepEqual(
|
|
109
|
-
body.text.split("\n").sort(),
|
|
110
|
-
expected.map((input) => `${input.name}: ${input.rows.length}`).sort(),
|
|
111
|
-
context,
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
t.diagnostic(
|
|
119
|
-
JSON.stringify({
|
|
120
|
-
calls,
|
|
121
|
-
rejected,
|
|
122
|
-
byteInputs: inputs.length,
|
|
123
|
-
nativeMatchingFiles,
|
|
124
|
-
extraCandidates,
|
|
125
|
-
}),
|
|
126
|
-
);
|
|
127
|
-
assert.ok(nativeMatchingFiles > 0);
|
|
128
|
-
});
|
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { basename, join } from "node:path";
|
|
5
|
-
import { test } from "node:test";
|
|
6
|
-
import { extractPrefilter, type Prefilter } from "../core/search-analysis.ts";
|
|
7
|
-
import {
|
|
8
|
-
acceptsNative,
|
|
9
|
-
MATRIX_IGNORE_CASE,
|
|
10
|
-
MATRIX_INPUTS,
|
|
11
|
-
MATRIX_SYNTAX,
|
|
12
|
-
} from "../core/search-matrix.testing.ts";
|
|
13
|
-
import { searchText, type GrepQuery } from "../core/search.ts";
|
|
14
|
-
import { runRgPaths } from "../pi/rg-process.ts";
|
|
15
|
-
import { fileCandidateArgs } from "../pi/grep-rg.testing.ts";
|
|
16
|
-
|
|
17
|
-
const syntaxCases = [
|
|
18
|
-
...MATRIX_SYNTAX,
|
|
19
|
-
["optional literal", "colou?r"],
|
|
20
|
-
["empty alternative", "(?:needle|)"],
|
|
21
|
-
["literal around wildcard", "needle.*tail"],
|
|
22
|
-
["literal lookahead only", "(?=needle)"],
|
|
23
|
-
["literal lookbehind only", "(?<=needle)"],
|
|
24
|
-
["consuming after lookbehind", "(?<=needle)tail"],
|
|
25
|
-
["literal capture reference", "(needle)\\1"],
|
|
26
|
-
["lookahead capture reference", "(?=(needle))\\1"],
|
|
27
|
-
["mixed case modifiers", "(?i:n)(?-i:eedle)"],
|
|
28
|
-
["zero LF capture", "(a\\n{0})\\1"],
|
|
29
|
-
["optional LF capture", "(a\\n{0,1})\\1"],
|
|
30
|
-
["zero Unicode capture", "(aé{0})\\1"],
|
|
31
|
-
["zero reference capture", "(a(?:\\1){0})\\1"],
|
|
32
|
-
["separated surrogate halves", "\\uD83D(?:)\\uDE00"],
|
|
33
|
-
["surrogate classes", "[\\uD83D][\\uDE00]"],
|
|
34
|
-
["wide class", "[\\u0001-\\u0100]"],
|
|
35
|
-
["many alternatives", Array.from({ length: 65 }, (_, i) => `token${i}`).join("|")],
|
|
36
|
-
["long repetition", "a{65}"],
|
|
37
|
-
["zero repetition", "x{0}"],
|
|
38
|
-
] as const;
|
|
39
|
-
|
|
40
|
-
interface Coverage {
|
|
41
|
-
queries: number;
|
|
42
|
-
rejected: number;
|
|
43
|
-
fullScan: number;
|
|
44
|
-
withHits: number;
|
|
45
|
-
nativeFiles: number;
|
|
46
|
-
candidateFiles: number;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function emptyCoverage(): Coverage {
|
|
50
|
-
return { queries: 0, rejected: 0, fullScan: 0, withHits: 0, nativeFiles: 0, candidateFiles: 0 };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
interface CandidateCase {
|
|
54
|
-
readonly prefilter: Prefilter;
|
|
55
|
-
readonly query: GrepQuery;
|
|
56
|
-
readonly required: Map<string, GrepQuery>;
|
|
57
|
-
readonly contributions: Map<Coverage, number>;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
test("Cartesian prefilter coverage × composition × case mode × bytes → real rg retains witnessed JS hits and reports selectivity", async (t) => {
|
|
61
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-prefilter-coverage-"));
|
|
62
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
63
|
-
const witnesses = [
|
|
64
|
-
"needle\nneedletail\nneedleXYZtail\nneedleneedle\nNeedle\nNEEDLE\ncolor\ncolour\n",
|
|
65
|
-
`${"a".repeat(65)}\ntoken0\ntoken64\n`,
|
|
66
|
-
"ab\ncd\nα\nβ\n漢\n𐐀\n𐐨\n👩💻\n🇺🇳\n",
|
|
67
|
-
"kſ\nKs\nσςΣ\néÉ\n\\K\nK\n",
|
|
68
|
-
"a\nb\r\na\r\nb\na\na\n",
|
|
69
|
-
"u{1F600}\np{Letter}\nP{ASCII}\n",
|
|
70
|
-
];
|
|
71
|
-
const sources = [
|
|
72
|
-
...MATRIX_INPUTS.map(([, bytes]) => bytes),
|
|
73
|
-
...witnesses.map((source) => Buffer.from(source)),
|
|
74
|
-
...[...MATRIX_INPUTS.map(([, bytes]) => bytes.toString("utf8")), ...witnesses].map((source) =>
|
|
75
|
-
Buffer.from(source.replace(/$/gm, "needle")),
|
|
76
|
-
),
|
|
77
|
-
// Separate nonmatching files make selectivity observable without timing assertions.
|
|
78
|
-
...Array.from({ length: 64 }, (_, i) =>
|
|
79
|
-
Buffer.from(`export const record_${i} = { id: ${i}, enabled: true };\n`),
|
|
80
|
-
),
|
|
81
|
-
];
|
|
82
|
-
const inputs = sources.map((bytes, index) => ({
|
|
83
|
-
name: `${index}.txt`,
|
|
84
|
-
source: bytes.toString("utf8"),
|
|
85
|
-
bytes,
|
|
86
|
-
}));
|
|
87
|
-
for (const input of inputs) await writeFile(join(directory, input.name), input.bytes);
|
|
88
|
-
const compositions = new Map<string, Coverage>();
|
|
89
|
-
const syntax = new Map<string, Coverage>();
|
|
90
|
-
const groups = new Map<string, CandidateCase>();
|
|
91
|
-
let rejected = 0;
|
|
92
|
-
for (const [label, pattern] of syntaxCases) {
|
|
93
|
-
const row = emptyCoverage();
|
|
94
|
-
syntax.set(label, row);
|
|
95
|
-
for (const [composition, patterns] of [
|
|
96
|
-
["bare", [pattern]],
|
|
97
|
-
["required literal", [`(?:${pattern})needle`]],
|
|
98
|
-
["OR-array", [pattern, "needle"]],
|
|
99
|
-
] as const) {
|
|
100
|
-
const total = compositions.get(composition) ?? emptyCoverage();
|
|
101
|
-
compositions.set(composition, total);
|
|
102
|
-
for (const ignoreCase of MATRIX_IGNORE_CASE) {
|
|
103
|
-
for (const multiline of [false, true]) {
|
|
104
|
-
const query: GrepQuery = {
|
|
105
|
-
patterns: [...patterns],
|
|
106
|
-
ignoreCase,
|
|
107
|
-
multiline,
|
|
108
|
-
literal: false,
|
|
109
|
-
};
|
|
110
|
-
if (!acceptsNative(query)) {
|
|
111
|
-
rejected++;
|
|
112
|
-
for (const stats of composition === "bare" ? [total, row] : [total]) stats.rejected++;
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
const prefilter = extractPrefilter(query);
|
|
116
|
-
const key = JSON.stringify(prefilter);
|
|
117
|
-
let group = groups.get(key);
|
|
118
|
-
if (!group) {
|
|
119
|
-
group = { prefilter, query, required: new Map(), contributions: new Map() };
|
|
120
|
-
groups.set(key, group);
|
|
121
|
-
}
|
|
122
|
-
let hits = 0;
|
|
123
|
-
for (const input of inputs) {
|
|
124
|
-
if (!searchText({ source: input.source, query, limit: 1 }).lines.length) continue;
|
|
125
|
-
hits++;
|
|
126
|
-
if (!group.required.has(input.name)) group.required.set(input.name, query);
|
|
127
|
-
}
|
|
128
|
-
for (const stats of composition === "bare" ? [total, row] : [total]) {
|
|
129
|
-
stats.queries++;
|
|
130
|
-
stats.fullScan += Number(prefilter.kind === "all");
|
|
131
|
-
stats.withHits += Number(hits > 0);
|
|
132
|
-
stats.nativeFiles += hits;
|
|
133
|
-
group.contributions.set(stats, (group.contributions.get(stats) ?? 0) + 1);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
const cases = [...groups.values()];
|
|
140
|
-
let next = 0;
|
|
141
|
-
let checkedFiles = 0;
|
|
142
|
-
const processTimeoutMs = 10_000;
|
|
143
|
-
const concurrency = 4;
|
|
144
|
-
const workers = await Promise.allSettled(
|
|
145
|
-
Array.from({ length: concurrency }, async () => {
|
|
146
|
-
while (next < cases.length) {
|
|
147
|
-
const group = cases[next++];
|
|
148
|
-
const actual = new Set<string>();
|
|
149
|
-
const result = await runRgPaths(
|
|
150
|
-
[...(await fileCandidateArgs(group.query)), "--null", "--", directory],
|
|
151
|
-
AbortSignal.timeout(processTimeoutMs),
|
|
152
|
-
(path) => {
|
|
153
|
-
actual.add(basename(path));
|
|
154
|
-
return true;
|
|
155
|
-
},
|
|
156
|
-
);
|
|
157
|
-
assert.ok(result.code === 0 || result.code === 1, JSON.stringify(result));
|
|
158
|
-
for (const [name, query] of group.required) {
|
|
159
|
-
checkedFiles++;
|
|
160
|
-
assert.ok(actual.has(name), JSON.stringify({ name, query, prefilter: group.prefilter }));
|
|
161
|
-
}
|
|
162
|
-
for (const [stats, count] of group.contributions)
|
|
163
|
-
stats.candidateFiles += actual.size * count;
|
|
164
|
-
}
|
|
165
|
-
}),
|
|
166
|
-
);
|
|
167
|
-
for (const worker of workers) if (worker.status === "rejected") throw worker.reason;
|
|
168
|
-
const unsupported = [...syntax]
|
|
169
|
-
.filter(([, stats]) => stats.queries === 0)
|
|
170
|
-
.map(([label]) => label);
|
|
171
|
-
const unwitnessed = [...syntax]
|
|
172
|
-
.filter(([, stats]) => stats.queries > 0 && stats.withHits === 0)
|
|
173
|
-
.map(([label]) => label);
|
|
174
|
-
t.diagnostic(
|
|
175
|
-
JSON.stringify({
|
|
176
|
-
files: inputs.length,
|
|
177
|
-
syntaxCases: syntaxCases.length,
|
|
178
|
-
rejected,
|
|
179
|
-
unsupported,
|
|
180
|
-
supportedSyntaxCases: syntax.size - unsupported.length,
|
|
181
|
-
candidateExpressions: cases.length,
|
|
182
|
-
checkedFiles,
|
|
183
|
-
unwitnessed,
|
|
184
|
-
}),
|
|
185
|
-
);
|
|
186
|
-
for (const [composition, stats] of compositions)
|
|
187
|
-
t.diagnostic(
|
|
188
|
-
JSON.stringify({ composition, ...stats, possibleFiles: stats.queries * inputs.length }),
|
|
189
|
-
);
|
|
190
|
-
for (const [label, stats] of syntax) t.diagnostic(JSON.stringify({ syntax: label, ...stats }));
|
|
191
|
-
for (const stats of syntax.values())
|
|
192
|
-
assert.equal(stats.queries + stats.rejected, MATRIX_IGNORE_CASE.length * 2);
|
|
193
|
-
for (const stats of compositions.values())
|
|
194
|
-
assert.equal(
|
|
195
|
-
stats.queries + stats.rejected,
|
|
196
|
-
syntaxCases.length * MATRIX_IGNORE_CASE.length * 2,
|
|
197
|
-
);
|
|
198
|
-
// A NUL-only pattern cannot match grep's searchable (non-NUL) file domain.
|
|
199
|
-
assert.deepEqual(unwitnessed, ["NUL"]);
|
|
200
|
-
const requiredLiteral = compositions.get("required literal");
|
|
201
|
-
assert.ok(requiredLiteral);
|
|
202
|
-
assert.ok(requiredLiteral.candidateFiles < requiredLiteral.queries * inputs.length);
|
|
203
|
-
assert.ok(checkedFiles > 0);
|
|
204
|
-
});
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { test } from "node:test";
|
|
6
|
-
import { DEFAULT_CONFIG } from "../pi/tool-factories.testing.ts";
|
|
7
|
-
import { grepCounts } from "../pi/grep-output.testing.ts";
|
|
8
|
-
import { makeGrepOverride } from "../pi/tool-factories.testing.ts";
|
|
9
|
-
import { expectToolFailure } from "../pi/report-failure.testing.ts";
|
|
10
|
-
import { reportOf } from "../pi/report.ts";
|
|
11
|
-
import { callTool } from "../pi/tool-call.testing.ts";
|
|
12
|
-
|
|
13
|
-
for (const input of ["explicit paths", "globs"] as const) {
|
|
14
|
-
test(`2000 ${input} exceed the Windows command line → report RIPGREP_FAILED with ENAMETOOLONG`, {
|
|
15
|
-
skip: process.platform !== "win32" ? "Requires the Windows command-line length limit" : false,
|
|
16
|
-
}, async (t) => {
|
|
17
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-rg-launch-"));
|
|
18
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
19
|
-
const file = join(directory, "needle.txt");
|
|
20
|
-
await writeFile(file, "needle\n");
|
|
21
|
-
const args =
|
|
22
|
-
input === "explicit paths"
|
|
23
|
-
? { path: Array.from({ length: 2000 }, () => file) }
|
|
24
|
-
: {
|
|
25
|
-
path: [file],
|
|
26
|
-
glob: Array.from({ length: 2000 }, (_, index) => `**/needle-${index}-*.txt`),
|
|
27
|
-
};
|
|
28
|
-
await expectToolFailure(
|
|
29
|
-
callTool(makeGrepOverride(directory, DEFAULT_CONFIG), {
|
|
30
|
-
...args,
|
|
31
|
-
pattern: ["needle"],
|
|
32
|
-
literal: true,
|
|
33
|
-
}),
|
|
34
|
-
"RIPGREP_FAILED",
|
|
35
|
-
(report) => assert.equal(report.cause?.code, "ENAMETOOLONG"),
|
|
36
|
-
);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
test("many short grep patterns exceed the Windows command line → retain native matches", {
|
|
41
|
-
skip: process.platform !== "win32" ? "Requires the Windows command-line length limit" : false,
|
|
42
|
-
}, async (t) => {
|
|
43
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-rg-short-patterns-"));
|
|
44
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
45
|
-
// 18,000 units of pattern text, but 36,000 with a flag and separators per pattern.
|
|
46
|
-
const pattern = Array.from({ length: 4_500 }, (_, index) => index.toString(36).padStart(4, "0"));
|
|
47
|
-
await writeFile(join(directory, "matches.txt"), `${pattern[0]}\n${pattern.at(-1)}\nother\n`);
|
|
48
|
-
const result = await callTool(makeGrepOverride(directory, DEFAULT_CONFIG), {
|
|
49
|
-
pattern,
|
|
50
|
-
literal: true,
|
|
51
|
-
ignoreCase: false,
|
|
52
|
-
});
|
|
53
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
54
|
-
assert.deepEqual(grepCounts(result), { matches: 2, files: 1 });
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
for (const multiline of [false, true]) {
|
|
58
|
-
test(`Windows command line fits scope but not ${multiline ? "file" : "line"} pattern args → retain scoped matches`, {
|
|
59
|
-
skip: process.platform !== "win32" ? "Requires the Windows command-line length limit" : false,
|
|
60
|
-
}, async (t) => {
|
|
61
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-rg-pattern-launch-"));
|
|
62
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
63
|
-
// The globs cost about 30,000 units. Either candidate mode adds over 4,000 units.
|
|
64
|
-
const glob = [
|
|
65
|
-
"*.txt",
|
|
66
|
-
...Array.from({ length: 250 }, (_, index) => `**/${"q".repeat(100)}-${index}-*.txt`),
|
|
67
|
-
];
|
|
68
|
-
const pattern = Array.from(
|
|
69
|
-
{ length: 64 },
|
|
70
|
-
(_, index) => index.toString(36).padStart(2, "0") + "a".repeat(62),
|
|
71
|
-
);
|
|
72
|
-
await writeFile(join(directory, "included.txt"), `${pattern.at(-1)}\nother\n`);
|
|
73
|
-
await writeFile(join(directory, "excluded.log"), `${pattern[0]}\n`);
|
|
74
|
-
const result = await callTool(makeGrepOverride(directory, DEFAULT_CONFIG), {
|
|
75
|
-
glob,
|
|
76
|
-
pattern,
|
|
77
|
-
literal: true,
|
|
78
|
-
ignoreCase: false,
|
|
79
|
-
multiline,
|
|
80
|
-
});
|
|
81
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
82
|
-
assert.deepEqual(grepCounts(result), { matches: 1, files: 1 });
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
test("multiline prefilter exceeds the rg pattern budget → scan all files and retain native matches", async (t) => {
|
|
87
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-rg-prefilter-budget-"));
|
|
88
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
89
|
-
const starts = [0x100, 0x140, 0x180, 0x1c0];
|
|
90
|
-
for (const start of starts)
|
|
91
|
-
await writeFile(join(directory, `${start}.txt`), String.fromCodePoint(start).repeat(64));
|
|
92
|
-
await writeFile(join(directory, "literal.txt"), "needle\n");
|
|
93
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
94
|
-
for (const count of [2, 3, 4]) {
|
|
95
|
-
await t.test(
|
|
96
|
-
`${count} large character classes → full scan finds every matching file`,
|
|
97
|
-
async () => {
|
|
98
|
-
const result = await callTool(tool, {
|
|
99
|
-
path: [directory],
|
|
100
|
-
pattern: starts
|
|
101
|
-
.slice(0, count)
|
|
102
|
-
.map(
|
|
103
|
-
(start) => `[${String.fromCodePoint(start)}-${String.fromCodePoint(start + 63)}]{64}`,
|
|
104
|
-
),
|
|
105
|
-
literal: false,
|
|
106
|
-
multiline: true,
|
|
107
|
-
ignoreCase: false,
|
|
108
|
-
});
|
|
109
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
110
|
-
assert.deepEqual(grepCounts(result), { matches: count, files: count });
|
|
111
|
-
},
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
const literal = await callTool(tool, {
|
|
115
|
-
path: [directory],
|
|
116
|
-
pattern: ["needle"],
|
|
117
|
-
literal: true,
|
|
118
|
-
multiline: true,
|
|
119
|
-
});
|
|
120
|
-
assert.equal(literal.isError, undefined);
|
|
121
|
-
assert.deepEqual(grepCounts(literal), { matches: 1, files: 1 });
|
|
122
|
-
});
|