@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,447 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import fs from "node:fs/promises";
|
|
3
|
-
import { syncBuiltinESMExports } from "node:module";
|
|
4
|
-
import os from "node:os";
|
|
5
|
-
import { basename, join } from "node:path";
|
|
6
|
-
import { test } from "node:test";
|
|
7
|
-
import { setImmediate } from "node:timers/promises";
|
|
8
|
-
import { Worker } from "node:worker_threads";
|
|
9
|
-
import { computeLineHash } from "../core/hash.ts";
|
|
10
|
-
import { searchText, type LineSearchInput, type SearchInput } from "../core/search.ts";
|
|
11
|
-
import { loadConfig } from "./config.ts";
|
|
12
|
-
const DEFAULT_CONFIG = loadConfig(os.tmpdir());
|
|
13
|
-
import { reportOf } from "./report.ts";
|
|
14
|
-
import type { MockRg } from "./grep-rg.testing.ts";
|
|
15
|
-
import type { WorkerError } from "./report-cause.ts";
|
|
16
|
-
import { callTool } from "./tool-call.testing.ts";
|
|
17
|
-
|
|
18
|
-
const REGEX_TIMEOUT: WorkerError = {
|
|
19
|
-
code: "REGEX_TIMEOUT",
|
|
20
|
-
message: "Regex evaluation timed out.",
|
|
21
|
-
facts: { timeoutMs: DEFAULT_CONFIG.grep.regexTimeoutMs },
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
test("out-of-order file completions → listing order fixes rows, warnings, and the limit cutoff", {
|
|
25
|
-
timeout: 15_000,
|
|
26
|
-
}, async (t) => {
|
|
27
|
-
const concurrentFiles = 4;
|
|
28
|
-
// Fix the pool size before its module loads, including on single-core CI hosts.
|
|
29
|
-
t.mock.method(os, "availableParallelism", () => concurrentFiles + 1);
|
|
30
|
-
syncBuiltinESMExports();
|
|
31
|
-
const { makeMockGrep } = await import("./grep-rg.testing.ts");
|
|
32
|
-
t.mock.restoreAll();
|
|
33
|
-
syncBuiltinESMExports();
|
|
34
|
-
|
|
35
|
-
const dir = await fs.mkdtemp(join(os.tmpdir(), "hl-grep-order-"));
|
|
36
|
-
t.after(() => fs.rm(dir, { recursive: true, force: true }));
|
|
37
|
-
const names = ["00-warning.txt", "01-slow.txt", "02-fast.txt", "03-warning.txt"];
|
|
38
|
-
const sourceLines = (name: string) => [`needle ${name} first`, `needle ${name} second`];
|
|
39
|
-
for (const name of names.filter((name) => !name.includes("warning"))) {
|
|
40
|
-
await fs.writeFile(join(dir, name), `${sourceLines(name).join("\n")}\n`);
|
|
41
|
-
}
|
|
42
|
-
const listingDiagnostic = "listing: Permission denied";
|
|
43
|
-
const warning = (name: string) => `${name}: EACCES: ${name}`;
|
|
44
|
-
|
|
45
|
-
for (const limit of [3, 5]) {
|
|
46
|
-
const outputs: unknown[] = [];
|
|
47
|
-
for (const order of [
|
|
48
|
-
["02-fast.txt", "03-warning.txt", "00-warning.txt", "01-slow.txt"],
|
|
49
|
-
["00-warning.txt", "01-slow.txt", "02-fast.txt", "03-warning.txt"],
|
|
50
|
-
]) {
|
|
51
|
-
await t.test(`limit=${limit}, completion=${order.join(",")}`, async (t) => {
|
|
52
|
-
const ready = Promise.withResolvers<void>();
|
|
53
|
-
const pending = new Map<string, () => void>();
|
|
54
|
-
const hold = (name: string, finish: () => void) => {
|
|
55
|
-
pending.set(name, () => {
|
|
56
|
-
pending.delete(name);
|
|
57
|
-
finish();
|
|
58
|
-
});
|
|
59
|
-
if (pending.size === concurrentFiles) ready.resolve();
|
|
60
|
-
};
|
|
61
|
-
const open = fs.open;
|
|
62
|
-
t.mock.method(fs, "open", (...args: Parameters<typeof fs.open>) => {
|
|
63
|
-
const path = args[0];
|
|
64
|
-
if (
|
|
65
|
-
typeof path === "string" &&
|
|
66
|
-
names.some((name) => name.includes("warning") && path === join(dir, name))
|
|
67
|
-
) {
|
|
68
|
-
const name = basename(path);
|
|
69
|
-
return new Promise<Awaited<ReturnType<typeof fs.open>>>((_resolve, reject) => {
|
|
70
|
-
hold(name, () =>
|
|
71
|
-
reject(Object.assign(new Error(`EACCES: ${name}`), { code: "EACCES" })),
|
|
72
|
-
);
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
return open(...args);
|
|
76
|
-
});
|
|
77
|
-
syncBuiltinESMExports();
|
|
78
|
-
t.mock.method(
|
|
79
|
-
Worker.prototype,
|
|
80
|
-
"postMessage",
|
|
81
|
-
function (this: Worker, input: SearchInput | LineSearchInput) {
|
|
82
|
-
assert.ok("source" in input);
|
|
83
|
-
const name = input.source.includes("01-slow.txt") ? "01-slow.txt" : "02-fast.txt";
|
|
84
|
-
hold(name, () => this.emit("message", { result: searchText(input) }));
|
|
85
|
-
},
|
|
86
|
-
);
|
|
87
|
-
const backend: MockRg = {
|
|
88
|
-
runRgPaths: async () => assert.fail("This query uses line candidates"),
|
|
89
|
-
runRgLines: async (_args, _signal, _maxLines, onFile) => {
|
|
90
|
-
for (const name of names) {
|
|
91
|
-
if (!(await onFile(join(dir, name), undefined))) {
|
|
92
|
-
return { code: 2, stderr: listingDiagnostic, stopped: true };
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return { code: 2, stderr: listingDiagnostic, stopped: false };
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
const running = callTool(makeMockGrep(dir, DEFAULT_CONFIG, backend), {
|
|
99
|
-
path: dir,
|
|
100
|
-
pattern: "needle",
|
|
101
|
-
literal: true,
|
|
102
|
-
limit,
|
|
103
|
-
});
|
|
104
|
-
try {
|
|
105
|
-
await Promise.race([
|
|
106
|
-
ready.promise,
|
|
107
|
-
running.then(() => assert.fail("Search finished before all files were held")),
|
|
108
|
-
]);
|
|
109
|
-
for (const name of order) {
|
|
110
|
-
const finish = pending.get(name);
|
|
111
|
-
assert.ok(finish, `Missing completion for ${name}`);
|
|
112
|
-
finish();
|
|
113
|
-
await setImmediate();
|
|
114
|
-
}
|
|
115
|
-
const result = await running;
|
|
116
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
117
|
-
const payload = result.content[0];
|
|
118
|
-
assert.ok(payload.type === "text");
|
|
119
|
-
const expected = ["01-slow.txt", "02-fast.txt"]
|
|
120
|
-
.map((name, index) => {
|
|
121
|
-
const lines = sourceLines(name).slice(0, index === 0 || limit === 5 ? 2 : 1);
|
|
122
|
-
return [
|
|
123
|
-
`${name} · ${lines.length} match${lines.length === 1 ? "" : "es"}`,
|
|
124
|
-
...lines.map(
|
|
125
|
-
(line, index) =>
|
|
126
|
-
`${index + 1}#${computeLineHash(index + 1, line, DEFAULT_CONFIG.hashLen)}│${line}`,
|
|
127
|
-
),
|
|
128
|
-
].join("\n");
|
|
129
|
-
})
|
|
130
|
-
.join("\n\n");
|
|
131
|
-
assert.equal(payload.text, expected);
|
|
132
|
-
const report = reportOf(result, "grep");
|
|
133
|
-
assert.ok(report);
|
|
134
|
-
assert.equal(report.matches, limit === 3 ? 3 : 4);
|
|
135
|
-
assert.equal(report.files, 2);
|
|
136
|
-
assert.equal(report.matchLimit, limit === 3 ? limit : undefined);
|
|
137
|
-
assert.equal(
|
|
138
|
-
report.diagnostics,
|
|
139
|
-
[
|
|
140
|
-
warning("00-warning.txt"),
|
|
141
|
-
...(limit === 5 ? [warning("03-warning.txt")] : []),
|
|
142
|
-
listingDiagnostic,
|
|
143
|
-
].join("\n"),
|
|
144
|
-
);
|
|
145
|
-
outputs.push(result.content);
|
|
146
|
-
} finally {
|
|
147
|
-
for (const finish of [...pending.values()]) finish();
|
|
148
|
-
await running;
|
|
149
|
-
t.mock.restoreAll();
|
|
150
|
-
syncBuiltinESMExports();
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
assert.deepEqual(outputs[0], outputs[1]);
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
test("match limit reached before a repeated candidate → stop listing before later diagnostics", async (t) => {
|
|
159
|
-
const { makeMockGrep } = await import("./grep-rg.testing.ts");
|
|
160
|
-
const dir = await fs.mkdtemp(join(os.tmpdir(), "hl-grep-duplicate-stop-"));
|
|
161
|
-
t.after(() => fs.rm(dir, { recursive: true, force: true }));
|
|
162
|
-
const file = join(dir, "fixture.txt");
|
|
163
|
-
await fs.writeFile(file, "needle\n");
|
|
164
|
-
for (const query of [
|
|
165
|
-
{ pattern: "needle", literal: true, multiline: false },
|
|
166
|
-
{ pattern: "needle", literal: true, multiline: true },
|
|
167
|
-
{ pattern: ".", literal: false, multiline: true },
|
|
168
|
-
]) {
|
|
169
|
-
await t.test(JSON.stringify(query), async (t) => {
|
|
170
|
-
const evaluated = Promise.withResolvers<void>();
|
|
171
|
-
t.mock.method(
|
|
172
|
-
Worker.prototype,
|
|
173
|
-
"postMessage",
|
|
174
|
-
function (this: Worker, input: SearchInput | LineSearchInput) {
|
|
175
|
-
assert.ok("source" in input);
|
|
176
|
-
this.emit("message", { result: searchText(input) });
|
|
177
|
-
evaluated.resolve();
|
|
178
|
-
},
|
|
179
|
-
);
|
|
180
|
-
const list = async (onFile: (path: string) => boolean | Promise<boolean>) => {
|
|
181
|
-
await onFile(file);
|
|
182
|
-
await evaluated.promise;
|
|
183
|
-
await setImmediate();
|
|
184
|
-
const stopped = !(await onFile(file));
|
|
185
|
-
return { code: stopped ? 0 : 2, stderr: stopped ? "" : "later diagnostic", stopped };
|
|
186
|
-
};
|
|
187
|
-
const result = await callTool(
|
|
188
|
-
makeMockGrep(dir, DEFAULT_CONFIG, {
|
|
189
|
-
runRgLines: (_args, _signal, _maxLines, onFile) =>
|
|
190
|
-
list((path) => onFile(path, undefined)),
|
|
191
|
-
runRgPaths: (_args, _signal, onFile) => list(onFile),
|
|
192
|
-
}),
|
|
193
|
-
{ path: file, ...query, limit: 1, outputMode: "files" },
|
|
194
|
-
);
|
|
195
|
-
assert.equal(result.isError, undefined);
|
|
196
|
-
assert.deepEqual(result.content[0], { type: "text", text: "fixture.txt" });
|
|
197
|
-
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
test("file-associated failure → listing position decides failure or cutoff, while cancellation stays immediate", {
|
|
203
|
-
timeout: 15_000,
|
|
204
|
-
}, async (t) => {
|
|
205
|
-
t.mock.method(os, "availableParallelism", () => 3);
|
|
206
|
-
syncBuiltinESMExports();
|
|
207
|
-
const { makeMockGrep } = await import("./grep-rg.testing.ts");
|
|
208
|
-
t.mock.restoreAll();
|
|
209
|
-
syncBuiltinESMExports();
|
|
210
|
-
const dir = await fs.mkdtemp(join(os.tmpdir(), "hl-grep-failure-order-"));
|
|
211
|
-
t.after(() => fs.rm(dir, { recursive: true, force: true }));
|
|
212
|
-
const names = ["a.txt", "z.txt"];
|
|
213
|
-
for (const name of names) await fs.writeFile(join(dir, name), `needle ${name}\n`);
|
|
214
|
-
const failures = [
|
|
215
|
-
{
|
|
216
|
-
name: "timeout",
|
|
217
|
-
code: REGEX_TIMEOUT.code,
|
|
218
|
-
finish: (worker: Worker) => worker.emit("message", { error: REGEX_TIMEOUT }),
|
|
219
|
-
},
|
|
220
|
-
{
|
|
221
|
-
name: "worker error",
|
|
222
|
-
code: "REGEX_WORKER_FAILED",
|
|
223
|
-
finish: (worker: Worker) => worker.emit("error", new Error("Worker crashed")),
|
|
224
|
-
},
|
|
225
|
-
{
|
|
226
|
-
name: "invalid reply schema",
|
|
227
|
-
code: "REGEX_WORKER_FAILED",
|
|
228
|
-
finish: (worker: Worker) =>
|
|
229
|
-
worker.emit("message", {
|
|
230
|
-
result: { lines: [{ lineNumber: 0, column: 0 }] },
|
|
231
|
-
}),
|
|
232
|
-
},
|
|
233
|
-
] as const;
|
|
234
|
-
const backend: MockRg = {
|
|
235
|
-
runRgPaths: async () => assert.fail("This query uses line candidates"),
|
|
236
|
-
runRgLines: async (_args, _signal, _maxLines, onFile) => {
|
|
237
|
-
for (const name of names) if (!(await onFile(join(dir, name), undefined))) break;
|
|
238
|
-
return { code: 0, stderr: "", stopped: false };
|
|
239
|
-
},
|
|
240
|
-
};
|
|
241
|
-
for (const failure of failures) {
|
|
242
|
-
for (const outcome of ["limited", "failed", "cancelled"] as const) {
|
|
243
|
-
const outputs: unknown[] = [];
|
|
244
|
-
for (const failureFirst of outcome === "cancelled" ? [true] : [false, true]) {
|
|
245
|
-
await t.test(
|
|
246
|
-
JSON.stringify({ failure: failure.name, outcome, failureFirst }),
|
|
247
|
-
async (t) => {
|
|
248
|
-
const ready = Promise.withResolvers<void>();
|
|
249
|
-
const pending = new Map<
|
|
250
|
-
string,
|
|
251
|
-
{
|
|
252
|
-
worker: Worker;
|
|
253
|
-
input: SearchInput;
|
|
254
|
-
stopped: ReturnType<typeof Promise.withResolvers<void>>;
|
|
255
|
-
}
|
|
256
|
-
>();
|
|
257
|
-
const terminate = Worker.prototype.terminate;
|
|
258
|
-
t.mock.method(Worker.prototype, "terminate", function (this: Worker) {
|
|
259
|
-
for (const file of pending.values()) if (file.worker === this) file.stopped.resolve();
|
|
260
|
-
return terminate.call(this);
|
|
261
|
-
});
|
|
262
|
-
t.mock.method(
|
|
263
|
-
Worker.prototype,
|
|
264
|
-
"postMessage",
|
|
265
|
-
function (this: Worker, input: SearchInput | LineSearchInput) {
|
|
266
|
-
assert.ok("source" in input);
|
|
267
|
-
const name = input.source.includes("a.txt") ? "a.txt" : "z.txt";
|
|
268
|
-
pending.set(name, { worker: this, input, stopped: Promise.withResolvers<void>() });
|
|
269
|
-
if (pending.size === names.length) ready.resolve();
|
|
270
|
-
},
|
|
271
|
-
);
|
|
272
|
-
const controller = new AbortController();
|
|
273
|
-
const running = callTool(
|
|
274
|
-
makeMockGrep(dir, DEFAULT_CONFIG, backend),
|
|
275
|
-
{
|
|
276
|
-
path: dir,
|
|
277
|
-
pattern: "needle",
|
|
278
|
-
literal: true,
|
|
279
|
-
limit: outcome === "failed" ? 2 : 1,
|
|
280
|
-
},
|
|
281
|
-
{ signal: controller.signal },
|
|
282
|
-
);
|
|
283
|
-
try {
|
|
284
|
-
await Promise.race([
|
|
285
|
-
ready.promise,
|
|
286
|
-
running.then(() => assert.fail("Search finished before both workers were held")),
|
|
287
|
-
]);
|
|
288
|
-
const early = pending.get("a.txt");
|
|
289
|
-
const late = pending.get("z.txt");
|
|
290
|
-
assert.ok(early && late);
|
|
291
|
-
const succeed = () =>
|
|
292
|
-
early.worker.emit("message", { result: searchText(early.input) });
|
|
293
|
-
if (failureFirst) failure.finish(late.worker);
|
|
294
|
-
else succeed();
|
|
295
|
-
await (failureFirst ? late : early).stopped.promise;
|
|
296
|
-
if (outcome === "cancelled") controller.abort();
|
|
297
|
-
else if (failureFirst) succeed();
|
|
298
|
-
else failure.finish(late.worker);
|
|
299
|
-
const result = await running;
|
|
300
|
-
const report = reportOf(result, "grep");
|
|
301
|
-
if (outcome === "limited") {
|
|
302
|
-
assert.equal(result.isError, undefined, JSON.stringify(report));
|
|
303
|
-
assert.equal(report?.matchLimit, 1);
|
|
304
|
-
assert.equal(report?.diagnostics, undefined);
|
|
305
|
-
assert.equal(result.content[0].type, "text");
|
|
306
|
-
assert.match(result.content[0].text, /1#[0-9A-Z]+│needle a\.txt/);
|
|
307
|
-
} else {
|
|
308
|
-
assert.equal(result.isError, true);
|
|
309
|
-
assert.equal(
|
|
310
|
-
report?.error,
|
|
311
|
-
outcome === "cancelled" ? "OPERATION_ABORTED" : failure.code,
|
|
312
|
-
);
|
|
313
|
-
}
|
|
314
|
-
outputs.push(result.content);
|
|
315
|
-
} finally {
|
|
316
|
-
controller.abort();
|
|
317
|
-
await running;
|
|
318
|
-
}
|
|
319
|
-
},
|
|
320
|
-
);
|
|
321
|
-
}
|
|
322
|
-
if (outputs.length === 2) assert.deepEqual(outputs[0], outputs[1]);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
test("partial budget consumed by an earlier file → later file has the same regex timeout in either completion order", {
|
|
328
|
-
timeout: 15_000,
|
|
329
|
-
}, async (t) => {
|
|
330
|
-
t.mock.method(os, "availableParallelism", () => 3);
|
|
331
|
-
syncBuiltinESMExports();
|
|
332
|
-
const { makeMockGrep } = await import("./grep-rg.testing.ts");
|
|
333
|
-
t.mock.restoreAll();
|
|
334
|
-
syncBuiltinESMExports();
|
|
335
|
-
|
|
336
|
-
const dir = await fs.mkdtemp(join(os.tmpdir(), "hl-grep-chunk-order-"));
|
|
337
|
-
t.after(() => fs.rm(dir, { recursive: true, force: true }));
|
|
338
|
-
const early = join(dir, "a.txt");
|
|
339
|
-
const late = join(dir, "z.txt");
|
|
340
|
-
// End the later file's first regex request after its first match.
|
|
341
|
-
const chunkUnits = 1024 * 1024;
|
|
342
|
-
await fs.writeFile(early, "needle early\n");
|
|
343
|
-
await fs.writeFile(late, `${"needle late".padEnd(chunkUnits - 1)}\nslow tail\n`);
|
|
344
|
-
const backend: MockRg = {
|
|
345
|
-
runRgPaths: async () => assert.fail("This query uses line candidates"),
|
|
346
|
-
runRgLines: async (_args, _signal, _maxLines, onFile) => {
|
|
347
|
-
for (const file of [early, late]) if (!(await onFile(file, undefined))) break;
|
|
348
|
-
return { code: 0, stderr: "", stopped: false };
|
|
349
|
-
},
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
for (const earlyFirst of [false, true]) {
|
|
353
|
-
await t.test(
|
|
354
|
-
earlyFirst ? "earlier file commits first" : "later file times out first",
|
|
355
|
-
async (t) => {
|
|
356
|
-
type Held = { worker: Worker; finish: () => void };
|
|
357
|
-
const earlyRequest = Promise.withResolvers<Held>();
|
|
358
|
-
const lateRequest = Promise.withResolvers<Held>();
|
|
359
|
-
const stopped = new Map<Worker, () => void>();
|
|
360
|
-
const terminate = Worker.prototype.terminate;
|
|
361
|
-
t.mock.method(Worker.prototype, "terminate", function (this: Worker) {
|
|
362
|
-
stopped.get(this)?.();
|
|
363
|
-
return terminate.call(this);
|
|
364
|
-
});
|
|
365
|
-
t.mock.method(
|
|
366
|
-
Worker.prototype,
|
|
367
|
-
"postMessage",
|
|
368
|
-
function (this: Worker, input: SearchInput | LineSearchInput) {
|
|
369
|
-
assert.ok("source" in input);
|
|
370
|
-
if (input.source === "slow tail\n") {
|
|
371
|
-
this.emit("message", { error: REGEX_TIMEOUT });
|
|
372
|
-
} else {
|
|
373
|
-
const request = input.source.startsWith("needle early") ? earlyRequest : lateRequest;
|
|
374
|
-
request.resolve({
|
|
375
|
-
worker: this,
|
|
376
|
-
finish: () => this.emit("message", { result: searchText(input) }),
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
},
|
|
380
|
-
);
|
|
381
|
-
const controller = new AbortController();
|
|
382
|
-
const running = callTool(
|
|
383
|
-
makeMockGrep(dir, DEFAULT_CONFIG, backend),
|
|
384
|
-
{ path: dir, pattern: "needle", literal: true, limit: 2, outputMode: "files" },
|
|
385
|
-
{ signal: controller.signal },
|
|
386
|
-
);
|
|
387
|
-
try {
|
|
388
|
-
const [a, z] = await Promise.race([
|
|
389
|
-
Promise.all([earlyRequest.promise, lateRequest.promise]),
|
|
390
|
-
running.then(() => assert.fail("Search finished before both requests were held")),
|
|
391
|
-
]);
|
|
392
|
-
const [first, second] = earlyFirst ? [a, z] : [z, a];
|
|
393
|
-
const firstSettled = new Promise<void>((resolve) => stopped.set(first.worker, resolve));
|
|
394
|
-
first.finish();
|
|
395
|
-
await firstSettled;
|
|
396
|
-
second.finish();
|
|
397
|
-
const result = await running;
|
|
398
|
-
const report = reportOf(result, "grep");
|
|
399
|
-
assert.equal(report?.error, REGEX_TIMEOUT.code, JSON.stringify(report));
|
|
400
|
-
assert.equal(result.isError, true);
|
|
401
|
-
} finally {
|
|
402
|
-
controller.abort();
|
|
403
|
-
await running;
|
|
404
|
-
}
|
|
405
|
-
},
|
|
406
|
-
);
|
|
407
|
-
}
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
test("regex request fails before a later NUL → skip that file and search the next candidate", async (t) => {
|
|
411
|
-
const { makeMockGrep } = await import("./grep-rg.testing.ts");
|
|
412
|
-
const dir = await fs.mkdtemp(join(os.tmpdir(), "hl-grep-late-nul-"));
|
|
413
|
-
t.after(() => fs.rm(dir, { recursive: true, force: true }));
|
|
414
|
-
const binaries = Array.from({ length: 4 }, (_, index) => join(dir, `binary-${index}.txt`));
|
|
415
|
-
const text = join(dir, "text.txt");
|
|
416
|
-
const binarySource = `${"binary request\n".repeat(100_000)}\0`;
|
|
417
|
-
await Promise.all(binaries.map((file) => fs.writeFile(file, binarySource)));
|
|
418
|
-
await fs.writeFile(text, "needle\n");
|
|
419
|
-
t.mock.method(
|
|
420
|
-
Worker.prototype,
|
|
421
|
-
"postMessage",
|
|
422
|
-
function (this: Worker, input: SearchInput | LineSearchInput) {
|
|
423
|
-
assert.ok("source" in input);
|
|
424
|
-
this.emit(
|
|
425
|
-
"message",
|
|
426
|
-
input.source.startsWith("binary request")
|
|
427
|
-
? { error: REGEX_TIMEOUT }
|
|
428
|
-
: { result: searchText(input) },
|
|
429
|
-
);
|
|
430
|
-
},
|
|
431
|
-
);
|
|
432
|
-
const tool = makeMockGrep(dir, DEFAULT_CONFIG, {
|
|
433
|
-
runRgLines: async (_args, _signal, _maxLines, onFile) => {
|
|
434
|
-
for (const file of [...binaries, text]) if (!(await onFile(file, undefined))) break;
|
|
435
|
-
return { code: 0, stderr: "", stopped: false };
|
|
436
|
-
},
|
|
437
|
-
});
|
|
438
|
-
const result = await callTool(tool, {
|
|
439
|
-
path: dir,
|
|
440
|
-
pattern: "needle",
|
|
441
|
-
literal: true,
|
|
442
|
-
outputMode: "files",
|
|
443
|
-
});
|
|
444
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
445
|
-
assert.deepEqual(result.content[0], { type: "text", text: "text.txt" });
|
|
446
|
-
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
447
|
-
});
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { parseFileHeader } from "./grep-output.ts";
|
|
3
|
-
import { reportOf } from "./report.ts";
|
|
4
|
-
|
|
5
|
-
/** Read successful grep totals from their report or the single complete payload block. */
|
|
6
|
-
export function grepCounts(result: Parameters<typeof reportOf>[0]): {
|
|
7
|
-
matches: number;
|
|
8
|
-
files: number;
|
|
9
|
-
} {
|
|
10
|
-
const report = reportOf(result, "grep");
|
|
11
|
-
assert.ok(report?.tool === "grep" && report.error === undefined, JSON.stringify(report));
|
|
12
|
-
if (report.matches !== undefined) {
|
|
13
|
-
assert.ok(typeof report.files === "number");
|
|
14
|
-
return { matches: report.matches, files: report.files };
|
|
15
|
-
}
|
|
16
|
-
assert.equal(report.files, undefined);
|
|
17
|
-
const payload = result.content?.[0];
|
|
18
|
-
assert.ok(payload?.type === "text");
|
|
19
|
-
const header = parseFileHeader(payload.text.split("\n")[0]);
|
|
20
|
-
if (header) return { matches: Number.parseInt(header.summary, 10), files: 1 };
|
|
21
|
-
const count = /^.+: (\d+)$/.exec(payload.text);
|
|
22
|
-
assert.ok(count, `Missing grep counts: ${payload.text}`);
|
|
23
|
-
return { matches: Number(count[1]), files: 1 };
|
|
24
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
-
import { after, mock } from "node:test";
|
|
3
|
-
import type { GrepQuery } from "../core/search.ts";
|
|
4
|
-
import type { HashlineEditConfig } from "./config.ts";
|
|
5
|
-
import type { OutputBudgetProvider } from "./budgets.ts";
|
|
6
|
-
|
|
7
|
-
import * as real from "./rg-process.ts";
|
|
8
|
-
export type MockRg = Partial<Pick<typeof real, "runRgPaths" | "runRgLines" | "rgRejectsPatterns">>;
|
|
9
|
-
const overrides = new AsyncLocalStorage<MockRg>();
|
|
10
|
-
const moduleMock = mock.module("./rg-process.ts", {
|
|
11
|
-
namedExports: {
|
|
12
|
-
...real,
|
|
13
|
-
runRgPaths: (...args: Parameters<typeof real.runRgPaths>) =>
|
|
14
|
-
(overrides.getStore()?.runRgPaths ?? real.runRgPaths)(...args),
|
|
15
|
-
runRgLines: (...args: Parameters<typeof real.runRgLines>) =>
|
|
16
|
-
(overrides.getStore()?.runRgLines ?? real.runRgLines)(...args),
|
|
17
|
-
rgRejectsPatterns: (...args: Parameters<typeof real.rgRejectsPatterns>) =>
|
|
18
|
-
(overrides.getStore()?.rgRejectsPatterns ?? real.rgRejectsPatterns)(...args),
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
after(() => moduleMock.restore());
|
|
23
|
-
|
|
24
|
-
// 在替身注册后才加载完整工具图,避免候选层捕获真实 rg 导出。
|
|
25
|
-
const factories = await import("./tool-factories.testing.ts");
|
|
26
|
-
const { searchFiles } = await import("./grep-search.ts");
|
|
27
|
-
const { listCandidates } = await import("./grep-candidates.ts");
|
|
28
|
-
|
|
29
|
-
export function makeMockGrep(
|
|
30
|
-
cwd: string,
|
|
31
|
-
config: HashlineEditConfig,
|
|
32
|
-
rg: MockRg,
|
|
33
|
-
budgets?: OutputBudgetProvider,
|
|
34
|
-
) {
|
|
35
|
-
const tool = factories.makeGrepOverride(cwd, config, budgets);
|
|
36
|
-
return {
|
|
37
|
-
...tool,
|
|
38
|
-
execute: (...args: Parameters<typeof tool.execute>) =>
|
|
39
|
-
overrides.run(rg, () => tool.execute(...args)),
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function searchWithMockRg({
|
|
44
|
-
backend,
|
|
45
|
-
...options
|
|
46
|
-
}: Parameters<typeof searchFiles>[0] & { readonly backend: MockRg }) {
|
|
47
|
-
return overrides.run(backend, () => searchFiles(options));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// 观察生产候选流程生成的文件预筛参数,不复制私有序列化规则。
|
|
51
|
-
export async function fileCandidateArgs(query: GrepQuery): Promise<string[]> {
|
|
52
|
-
let captured: string[] | undefined;
|
|
53
|
-
await overrides.run(
|
|
54
|
-
{
|
|
55
|
-
runRgLines: async () => ({ code: 2, stderr: "", stopped: false }),
|
|
56
|
-
rgRejectsPatterns: async () => true,
|
|
57
|
-
runRgPaths: async (args) => {
|
|
58
|
-
captured = args.slice(0, args.indexOf("--null"));
|
|
59
|
-
return { code: 1, stderr: "", stopped: false };
|
|
60
|
-
},
|
|
61
|
-
},
|
|
62
|
-
() =>
|
|
63
|
-
listCandidates({
|
|
64
|
-
query,
|
|
65
|
-
scope: { globs: [], searchPaths: [process.cwd()] },
|
|
66
|
-
signal: new AbortController().signal,
|
|
67
|
-
stopped: () => false,
|
|
68
|
-
onCandidate: async () => true,
|
|
69
|
-
}),
|
|
70
|
-
);
|
|
71
|
-
if (!captured) throw new Error("File prefilter was not observed");
|
|
72
|
-
return captured;
|
|
73
|
-
}
|