@criogaid/pi-hashline-edit 0.9.0 → 0.10.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 +9 -10
- package/README.zh-CN.md +8 -9
- package/THIRD_PARTY_NOTICES.md +0 -6
- package/package.json +1 -1
- package/src/core/AGENTS.md +4 -5
- package/src/core/apply.test.ts +96 -271
- package/src/core/apply.ts +62 -169
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.ts +4 -4
- package/src/core/errors.test.ts +23 -7
- package/src/core/errors.ts +32 -5
- package/src/core/hash.test.ts +8 -16
- package/src/core/hash.ts +3 -23
- package/src/core/lines.ts +2 -4
- package/src/core/regex.ts +3 -4
- package/src/core/replace.test.ts +47 -1
- package/src/core/replace.ts +19 -13
- package/src/core/search-analysis.test.ts +24 -27
- package/src/core/search-analysis.ts +34 -65
- package/src/core/search-matrix.testing.ts +0 -2
- package/src/core/search.ts +1 -1
- package/src/core/text.test.ts +5 -5
- package/src/core/text.ts +5 -12
- package/src/core/types.ts +7 -43
- package/src/integration/fusion-dispatch.test.ts +2 -5
- package/src/integration/grep-flags.test.ts +0 -14
- package/src/integration/grep-js-completeness.test.ts +34 -8
- package/src/integration/grep-line-candidates.test.ts +4 -8
- package/src/integration/grep-matrix.test.ts +3 -7
- package/src/integration/grep-path-matrix.test.ts +5 -5
- package/src/integration/grep-prefilter-coverage.test.ts +3 -7
- package/src/integration/grep-process-limits.test.ts +78 -0
- package/src/integration/grep-rg.test.ts +118 -20
- package/src/integration/grep-scan-budget.test.ts +2 -2
- package/src/integration/grep-translation.test.ts +12 -27
- package/src/integration/large-text.test.ts +1 -47
- package/src/integration/model-budget-lifecycle.test.ts +2 -1
- package/src/integration/publication-read-changes.test.ts +123 -45
- package/src/integration/replace-worker-boundary.test.ts +21 -11
- package/src/integration/report-replay.test.ts +20 -22
- package/src/pi/AGENTS.md +15 -15
- package/src/pi/action-fusion.test.ts +164 -296
- package/src/pi/action-fusion.ts +148 -111
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +21 -25
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-validation.test.ts +129 -157
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +5 -4
- package/src/pi/config.test.ts +5 -2
- package/src/pi/config.ts +4 -6
- package/src/pi/diagnostic-buffer.test.ts +12 -1
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +46 -156
- package/src/pi/error-text.ts +4 -8
- package/src/pi/execute.test.ts +83 -196
- package/src/pi/failure-context.test.ts +144 -126
- package/src/pi/failure-context.ts +44 -93
- package/src/pi/failure-path.test.ts +168 -141
- package/src/pi/file-commit.test.ts +110 -53
- package/src/pi/file-commit.ts +90 -137
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +4 -1
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +2 -101
- package/src/pi/grep-candidates.ts +23 -69
- package/src/pi/grep-matrix.test.ts +2 -2
- package/src/pi/grep-order.test.ts +4 -4
- package/src/pi/grep-output.ts +62 -96
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +92 -0
- package/src/pi/grep-scope.ts +40 -42
- package/src/pi/grep-search.ts +22 -31
- package/src/pi/grep-tool.ts +55 -82
- package/src/pi/grep.test.ts +80 -45
- package/src/pi/model-budgets.test.ts +25 -23
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +51 -20
- package/src/pi/mutation-runner.ts +126 -70
- package/src/pi/output-budgets.test.ts +36 -14
- package/src/pi/path.ts +1 -1
- package/src/pi/read-tool.ts +5 -5
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +64 -36
- package/src/pi/render.test.ts +38 -28
- package/src/pi/render.ts +59 -63
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +20 -82
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +9 -5
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +1 -2
- package/src/pi/report-schema.ts +62 -81
- package/src/pi/report.test.ts +171 -25
- package/src/pi/report.ts +33 -87
- package/src/pi/result-safety.test.ts +149 -176
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +48 -64
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +12 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/write-tool.ts +33 -62
- package/src/pi/write.test.ts +1 -3
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/pi/replace-worker-contract.ts +0 -10
|
@@ -3,7 +3,8 @@ import { mkdtemp, readFile, rm, 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 { HashlineError } from "../core/errors.ts";
|
|
7
|
+
import { readEditableSnapshot } from "./file-commit.ts";
|
|
7
8
|
|
|
8
9
|
test("snapshot cancelled while opening → rejects as an unpublished cancellation", async (t) => {
|
|
9
10
|
const directory = await mkdtemp(join(tmpdir(), "hashline-snapshot-"));
|
|
@@ -15,10 +16,7 @@ test("snapshot cancelled while opening → rejects as an unpublished cancellatio
|
|
|
15
16
|
controller.abort();
|
|
16
17
|
await assert.rejects(
|
|
17
18
|
reading,
|
|
18
|
-
(error: unknown) =>
|
|
19
|
-
error instanceof FileMutationError &&
|
|
20
|
-
error.publication === "NOT_PUBLISHED" &&
|
|
21
|
-
error.message.startsWith("Operation aborted"),
|
|
19
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "OPERATION_ABORTED",
|
|
22
20
|
);
|
|
23
21
|
assert.equal(await readFile(path, "utf8"), "before\n");
|
|
24
22
|
});
|
|
@@ -5,7 +5,8 @@ import { tmpdir } from "node:os";
|
|
|
5
5
|
import { join } from "node:path";
|
|
6
6
|
import { initTheme } from "@earendil-works/pi-coding-agent";
|
|
7
7
|
import { registerFusionCards } from "./fusion-card.ts";
|
|
8
|
-
import { createActionFusionExecutor
|
|
8
|
+
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
9
|
+
import type { ActionFusionProgress } from "./report-schema.ts";
|
|
9
10
|
import type { CommandResult } from "./report-schema.ts";
|
|
10
11
|
import { commandText } from "./mutation-result.ts";
|
|
11
12
|
import { decodeReport } from "./report.ts";
|
|
@@ -46,6 +47,7 @@ function harness(entries: any[] = []) {
|
|
|
46
47
|
handlers.get(event)(
|
|
47
48
|
{},
|
|
48
49
|
{
|
|
50
|
+
cwd: process.cwd(),
|
|
49
51
|
mode: "tui",
|
|
50
52
|
ui: {
|
|
51
53
|
setWidget(_key: string, content: Parameters<ExtensionContext["ui"]["setWidget"]>[1]) {
|
|
@@ -137,6 +139,7 @@ test("Fusion command outcomes → progress, cards, and reports preserve command
|
|
|
137
139
|
const dir = await mkdtemp(join(tmpdir(), "hashline-fusion-card-"));
|
|
138
140
|
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
139
141
|
const h = harness();
|
|
142
|
+
h.restore("session_start", h.entries, { cwd: dir });
|
|
140
143
|
const events: ActionFusionProgress[] = [];
|
|
141
144
|
const controller = new AbortController();
|
|
142
145
|
if (scenario === "cancelled") controller.abort();
|
package/src/pi/fusion-card.ts
CHANGED
|
@@ -25,8 +25,7 @@ import {
|
|
|
25
25
|
} from "@earendil-works/pi-tui";
|
|
26
26
|
import type { TSchema } from "typebox";
|
|
27
27
|
import { Value } from "typebox/value";
|
|
28
|
-
import { ACTION_FUSION_PROGRESS_SCHEMA } from "./report-schema.ts";
|
|
29
|
-
import type { ActionFusionProgress } from "./action-fusion.ts";
|
|
28
|
+
import { ACTION_FUSION_PROGRESS_SCHEMA, type ActionFusionProgress } from "./report-schema.ts";
|
|
30
29
|
import { commandText } from "./mutation-result.ts";
|
|
31
30
|
import type { MutationRenderState } from "./render.ts";
|
|
32
31
|
|
|
@@ -37,6 +36,11 @@ const RESULT_TYPE = "hashline-then-run-result";
|
|
|
37
36
|
const COMMAND_CARD_SCHEMA = ACTION_FUSION_PROGRESS_SCHEMA;
|
|
38
37
|
type CommandCardData = ActionFusionProgress;
|
|
39
38
|
|
|
39
|
+
const commandPending = ({ command }: CommandCardData) =>
|
|
40
|
+
command === "waiting" || command === "running";
|
|
41
|
+
const commandFailed = ({ command }: CommandCardData) =>
|
|
42
|
+
command === "failed" || command === "timeout";
|
|
43
|
+
|
|
40
44
|
const TARGET_STATE = {
|
|
41
45
|
changed: "the target changed after the mutation",
|
|
42
46
|
missing: "the target is missing after the mutation",
|
|
@@ -95,7 +99,7 @@ export function registerFusionCards(pi: ExtensionAPI) {
|
|
|
95
99
|
let tui: TUI | undefined;
|
|
96
100
|
|
|
97
101
|
const restore = (ctx: ExtensionContext) => {
|
|
98
|
-
|
|
102
|
+
currentCwd = ctx.cwd;
|
|
99
103
|
states.clear();
|
|
100
104
|
active.clear();
|
|
101
105
|
cards.clear();
|
|
@@ -160,14 +164,14 @@ export function registerFusionCards(pi: ExtensionAPI) {
|
|
|
160
164
|
box = new Box(outputPadding, 1);
|
|
161
165
|
}
|
|
162
166
|
const value = current();
|
|
163
|
-
const pending = value
|
|
167
|
+
const pending = commandPending(value);
|
|
164
168
|
const interrupted = pending && !active.has(value.toolCallId);
|
|
165
169
|
const status = interrupted ? "interrupted (final status unknown)" : value.command;
|
|
166
170
|
const countdown =
|
|
167
171
|
value.command === "running" && !interrupted && value.timing
|
|
168
172
|
? ` · ${value.timing.remainingSeconds}s remaining`
|
|
169
173
|
: "";
|
|
170
|
-
const failed = value
|
|
174
|
+
const failed = commandFailed(value);
|
|
171
175
|
const color =
|
|
172
176
|
pending || value.command === "cancelled"
|
|
173
177
|
? "warning"
|
|
@@ -221,10 +225,9 @@ export function registerFusionCards(pi: ExtensionAPI) {
|
|
|
221
225
|
native.updateResult(
|
|
222
226
|
{
|
|
223
227
|
...(value.result ?? { content: [] }),
|
|
224
|
-
isError: value
|
|
228
|
+
isError: commandFailed(value),
|
|
225
229
|
},
|
|
226
|
-
(value
|
|
227
|
-
active.has(value.toolCallId),
|
|
230
|
+
commandPending(value) && active.has(value.toolCallId),
|
|
228
231
|
);
|
|
229
232
|
},
|
|
230
233
|
"bash",
|
|
@@ -247,11 +250,10 @@ export function registerFusionCards(pi: ExtensionAPI) {
|
|
|
247
250
|
return card;
|
|
248
251
|
});
|
|
249
252
|
|
|
250
|
-
return (progress: ActionFusionProgress
|
|
251
|
-
if (ctx?.cwd) currentCwd = ctx.cwd;
|
|
253
|
+
return (progress: ActionFusionProgress) => {
|
|
252
254
|
const first = !states.has(progress.toolCallId);
|
|
253
255
|
states.set(progress.toolCallId, progress);
|
|
254
|
-
const pending = progress
|
|
256
|
+
const pending = commandPending(progress);
|
|
255
257
|
if (pending) active.add(progress.toolCallId);
|
|
256
258
|
else active.delete(progress.toolCallId);
|
|
257
259
|
// Only endpoints are persisted; streaming snapshots reuse Bash's bounded output.
|
|
@@ -276,20 +278,10 @@ interface FusedMutationRenderState extends MutationRenderState {
|
|
|
276
278
|
};
|
|
277
279
|
}
|
|
278
280
|
|
|
279
|
-
type MutationCardTool<TParams extends TSchema, TDetails, TRenderDetails> = Omit<
|
|
280
|
-
ToolDefinition<TParams, TDetails, FusedMutationRenderState>,
|
|
281
|
-
"renderResult"
|
|
282
|
-
> &
|
|
283
|
-
Pick<ToolDefinition<TParams, TRenderDetails, FusedMutationRenderState>, "renderResult">;
|
|
284
|
-
|
|
285
281
|
/** Finish the mutation card independently while the fused command remains active. */
|
|
286
|
-
export function withMutationStatus<
|
|
287
|
-
TParams
|
|
288
|
-
|
|
289
|
-
TRenderDetails,
|
|
290
|
-
>(
|
|
291
|
-
tool: MutationCardTool<TParams, TDetails, TRenderDetails>,
|
|
292
|
-
): MutationCardTool<TParams, TDetails, TRenderDetails> {
|
|
282
|
+
export function withMutationStatus<TParams extends TSchema, TDetails>(
|
|
283
|
+
tool: ToolDefinition<TParams, TDetails, FusedMutationRenderState>,
|
|
284
|
+
): ToolDefinition<TParams, TDetails, FusedMutationRenderState> {
|
|
293
285
|
return {
|
|
294
286
|
...tool,
|
|
295
287
|
renderShell: "self",
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { test } from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
|
-
import { candidateArgs
|
|
4
|
-
import { COMMON_RG_ARGS
|
|
5
|
-
import type { Prefilter } from "../core/search-analysis.ts";
|
|
3
|
+
import { candidateArgs } from "./grep-candidates.ts";
|
|
4
|
+
import { COMMON_RG_ARGS } from "./rg-process.ts";
|
|
6
5
|
|
|
7
6
|
test("candidate literals → rg lists files with escaped byte-preserving alternatives", () => {
|
|
8
7
|
assert.deepEqual(candidateArgs({ kind: "all" }), [...COMMON_RG_ARGS, "--files"]);
|
|
@@ -27,101 +26,3 @@ test("candidate literals → rg lists files with escaped byte-preserving alterna
|
|
|
27
26
|
]);
|
|
28
27
|
}
|
|
29
28
|
});
|
|
30
|
-
|
|
31
|
-
const conjunction: Prefilter = {
|
|
32
|
-
kind: "and",
|
|
33
|
-
filters: [
|
|
34
|
-
{ kind: "literals", literals: [[["a"]]] },
|
|
35
|
-
{ kind: "literals", literals: [[["b"]]] },
|
|
36
|
-
],
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
test("AND file passes → intersect paths before emitting and preserve final callback stop", async () => {
|
|
40
|
-
let pass = 0;
|
|
41
|
-
const calls: string[][] = [];
|
|
42
|
-
const run: GrepBackend["runRgPaths"] = async (_rg, args, _signal, onPath) => {
|
|
43
|
-
calls.push([...args]);
|
|
44
|
-
for (const path of [
|
|
45
|
-
["a-only", "both"],
|
|
46
|
-
["b-only", "both", "later"],
|
|
47
|
-
][pass++])
|
|
48
|
-
if (!(await onPath(path))) return { code: null, stderr: "", stopped: true };
|
|
49
|
-
return { code: 0, stderr: "", stopped: false };
|
|
50
|
-
};
|
|
51
|
-
const paths: string[] = [];
|
|
52
|
-
const result = await runCandidatePaths(
|
|
53
|
-
run,
|
|
54
|
-
"rg",
|
|
55
|
-
conjunction,
|
|
56
|
-
["--", "root"],
|
|
57
|
-
undefined,
|
|
58
|
-
(path) => {
|
|
59
|
-
paths.push(path);
|
|
60
|
-
return false;
|
|
61
|
-
},
|
|
62
|
-
);
|
|
63
|
-
assert.deepEqual(paths, ["both"]);
|
|
64
|
-
assert.equal(result.stopped, true);
|
|
65
|
-
assert.equal(calls.length, 2);
|
|
66
|
-
for (const args of calls) assert.deepEqual(args.slice(-2), ["--", "root"]);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("AND path retention fills → rerun one necessary clause without excluding its candidates", async () => {
|
|
70
|
-
let pass = 0;
|
|
71
|
-
const run: GrepBackend["runRgPaths"] = async (_rg, _args, _signal, onPath) => {
|
|
72
|
-
const keepGoing = await onPath(
|
|
73
|
-
pass++ === 0 ? "x".repeat(MAX_PREFILTER_PATH_BYTES + 1) : "candidate",
|
|
74
|
-
);
|
|
75
|
-
return { code: keepGoing ? 0 : null, stderr: "", stopped: !keepGoing };
|
|
76
|
-
};
|
|
77
|
-
const paths: string[] = [];
|
|
78
|
-
const result = await runCandidatePaths(
|
|
79
|
-
run,
|
|
80
|
-
"rg",
|
|
81
|
-
conjunction,
|
|
82
|
-
["--", "root"],
|
|
83
|
-
undefined,
|
|
84
|
-
(path) => {
|
|
85
|
-
paths.push(path);
|
|
86
|
-
return true;
|
|
87
|
-
},
|
|
88
|
-
);
|
|
89
|
-
assert.deepEqual(paths, ["candidate"]);
|
|
90
|
-
assert.equal(pass, 2);
|
|
91
|
-
assert.equal(result.stopped, false);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
test("incomplete intermediate pass → retain reported candidates and the failure diagnostics", async () => {
|
|
95
|
-
const run: GrepBackend["runRgPaths"] = async (_rg, _args, _signal, onPath) => {
|
|
96
|
-
await onPath("reported");
|
|
97
|
-
return { code: 2, stderr: "unreadable path", stopped: false };
|
|
98
|
-
};
|
|
99
|
-
const paths: string[] = [];
|
|
100
|
-
const result = await runCandidatePaths(run, "rg", conjunction, [], undefined, (path) => {
|
|
101
|
-
paths.push(path);
|
|
102
|
-
return true;
|
|
103
|
-
});
|
|
104
|
-
assert.deepEqual(paths, ["reported"]);
|
|
105
|
-
assert.equal(result.code, 2);
|
|
106
|
-
assert.match(result.stderr, /unreadable path/);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
test("cancel between AND passes → propagate cancellation before emitting candidates", async () => {
|
|
110
|
-
const controller = new AbortController();
|
|
111
|
-
let pass = 0;
|
|
112
|
-
const run: GrepBackend["runRgPaths"] = async (_rg, _args, signal, onPath) => {
|
|
113
|
-
assert.equal(signal, controller.signal);
|
|
114
|
-
if (pass++) {
|
|
115
|
-
controller.abort(new Error("cancelled"));
|
|
116
|
-
signal?.throwIfAborted();
|
|
117
|
-
}
|
|
118
|
-
await onPath("candidate");
|
|
119
|
-
return { code: 0, stderr: "", stopped: false };
|
|
120
|
-
};
|
|
121
|
-
await assert.rejects(
|
|
122
|
-
runCandidatePaths(run, "rg", conjunction, [], controller.signal, () =>
|
|
123
|
-
assert.fail("cancelled candidate"),
|
|
124
|
-
),
|
|
125
|
-
/cancelled/,
|
|
126
|
-
);
|
|
127
|
-
});
|
|
@@ -10,16 +10,15 @@ import { stat } from "node:fs/promises";
|
|
|
10
10
|
import { resolve } from "node:path";
|
|
11
11
|
import type { GrepQuery } from "../core/search.ts";
|
|
12
12
|
import { extractPrefilter, type Literal, type Prefilter } from "../core/search-analysis.ts";
|
|
13
|
-
import { DiagnosticBuffer } from "./diagnostic-buffer.ts";
|
|
14
13
|
import { scopeArgs, type SearchScope } from "./grep-scope.ts";
|
|
15
14
|
import {
|
|
16
15
|
COMMON_RG_ARGS,
|
|
17
16
|
LINE_LISTING_ARGS,
|
|
18
|
-
|
|
17
|
+
MAX_RG_PATTERN_UNITS,
|
|
19
18
|
type GrepBackend,
|
|
20
19
|
type RgRunResult,
|
|
21
20
|
} from "./rg-process.ts";
|
|
22
|
-
import { lineCandidatePatterns } from "./rg-regex.ts";
|
|
21
|
+
import { lineCandidatePatterns, rgCodePoint } from "./rg-regex.ts";
|
|
23
22
|
|
|
24
23
|
/** Beyond this many candidate lines, a file's every line is checked instead. */
|
|
25
24
|
const MAX_CANDIDATE_LINES = 64 * 1024;
|
|
@@ -28,8 +27,6 @@ const MAX_CANDIDATE_LINES = 64 * 1024;
|
|
|
28
27
|
* them were queued; README's search limits link here.
|
|
29
28
|
*/
|
|
30
29
|
export const MAX_UNNARROWED_SCAN_BYTES = 256 * 1024 * 1024;
|
|
31
|
-
/** Maximum retained paths while intersecting necessary file conditions. */
|
|
32
|
-
export const MAX_PREFILTER_PATH_BYTES = 4 * 1024 * 1024;
|
|
33
30
|
/** Candidate patterns match original bytes exactly as written; JavaScript decides case and encoding. */
|
|
34
31
|
const BYTE_PATTERN_ARGS = ["--engine=default", "--case-sensitive", "--encoding=none"];
|
|
35
32
|
|
|
@@ -39,85 +36,40 @@ export interface Candidate {
|
|
|
39
36
|
readonly lines?: readonly number[];
|
|
40
37
|
}
|
|
41
38
|
|
|
42
|
-
/** A literal's characters as one rg pattern
|
|
39
|
+
/** A literal's characters as one rg pattern. */
|
|
43
40
|
function literalPattern(literal: Literal): string {
|
|
44
|
-
const escape = (char: string) =>
|
|
45
|
-
/^[0-9A-Za-z_]$/.test(char) ? char : `\\x{${char.codePointAt(0)!.toString(16)}}`;
|
|
41
|
+
const escape = (char: string) => rgCodePoint(char.codePointAt(0)!);
|
|
46
42
|
return literal
|
|
47
43
|
.map((chars) => (chars.length === 1 ? escape(chars[0]) : `[${chars.map(escape).join("")}]`))
|
|
48
44
|
.join("");
|
|
49
45
|
}
|
|
50
46
|
|
|
47
|
+
const literalAlternation = (literals: readonly Literal[]): string =>
|
|
48
|
+
literals.map(literalPattern).join("|");
|
|
49
|
+
|
|
50
|
+
/** Necessary literals too long for rg's command line widen to every file in scope. */
|
|
51
|
+
function boundedPrefilter(prefilter: Prefilter): Prefilter {
|
|
52
|
+
return prefilter.kind === "literals" &&
|
|
53
|
+
literalAlternation(prefilter.literals).length > MAX_RG_PATTERN_UNITS
|
|
54
|
+
? { kind: "all" }
|
|
55
|
+
: prefilter;
|
|
56
|
+
}
|
|
57
|
+
|
|
51
58
|
/**
|
|
52
59
|
* List candidate files: those whose original bytes contain a prefilter literal, or every
|
|
53
60
|
* file in scope. Literals never contain a line break, so line-oriented matching finds them.
|
|
54
61
|
*/
|
|
55
62
|
export function candidateArgs(prefilter: Prefilter): string[] {
|
|
56
|
-
if (prefilter.kind === "and") return candidateArgs(prefilter.filters[0]);
|
|
57
63
|
if (prefilter.kind === "all") return [...COMMON_RG_ARGS, "--files"];
|
|
58
64
|
return [
|
|
59
65
|
...COMMON_RG_ARGS,
|
|
60
66
|
"--files-with-matches",
|
|
61
67
|
...BYTE_PATTERN_ARGS,
|
|
62
68
|
"-e",
|
|
63
|
-
prefilter.literals
|
|
69
|
+
literalAlternation(prefilter.literals),
|
|
64
70
|
];
|
|
65
71
|
}
|
|
66
72
|
|
|
67
|
-
/** Execute AND-of-OR file conditions, falling back to one necessary clause if retention fills. */
|
|
68
|
-
export async function runCandidatePaths(
|
|
69
|
-
run: GrepBackend["runRgPaths"],
|
|
70
|
-
rgPath: string,
|
|
71
|
-
prefilter: Prefilter,
|
|
72
|
-
scopedArgs: readonly string[],
|
|
73
|
-
signal: AbortSignal | undefined,
|
|
74
|
-
onFile: (path: string) => boolean | Promise<boolean>,
|
|
75
|
-
): Promise<RgRunResult> {
|
|
76
|
-
const filters = prefilter.kind === "and" ? prefilter.filters : [prefilter];
|
|
77
|
-
const diagnostics = new DiagnosticBuffer(MAX_RG_STDERR_BYTES);
|
|
78
|
-
let allowed: ReadonlySet<string> | undefined;
|
|
79
|
-
for (const [index, filter] of filters.entries()) {
|
|
80
|
-
const last = index === filters.length - 1;
|
|
81
|
-
const paths = new Set<string>();
|
|
82
|
-
let bytes = 0;
|
|
83
|
-
let overflow = false;
|
|
84
|
-
const result = await run(
|
|
85
|
-
rgPath,
|
|
86
|
-
[...candidateArgs(filter), "--null", ...scopedArgs],
|
|
87
|
-
signal,
|
|
88
|
-
async (path) => {
|
|
89
|
-
if (allowed && !allowed.has(path)) return true;
|
|
90
|
-
if (last) return onFile(path);
|
|
91
|
-
if (!paths.has(path)) {
|
|
92
|
-
paths.add(path);
|
|
93
|
-
bytes += Buffer.byteLength(path);
|
|
94
|
-
}
|
|
95
|
-
overflow = bytes > MAX_PREFILTER_PATH_BYTES;
|
|
96
|
-
return !overflow;
|
|
97
|
-
},
|
|
98
|
-
);
|
|
99
|
-
if (result.stderr) diagnostics.append(`${result.stderr}\n`);
|
|
100
|
-
if (overflow) {
|
|
101
|
-
const fallback = await run(
|
|
102
|
-
rgPath,
|
|
103
|
-
[...candidateArgs(filters[0]), "--null", ...scopedArgs],
|
|
104
|
-
signal,
|
|
105
|
-
onFile,
|
|
106
|
-
);
|
|
107
|
-
if (fallback.stderr) diagnostics.append(fallback.stderr);
|
|
108
|
-
return { ...fallback, stderr: diagnostics.toString() };
|
|
109
|
-
}
|
|
110
|
-
if (last || result.stopped || (result.code !== 0 && result.code !== 1)) {
|
|
111
|
-
// An incomplete intermediate listing cannot exclude its already reported candidates.
|
|
112
|
-
if (!last) for (const path of paths) if (!(await onFile(path))) break;
|
|
113
|
-
return { ...result, stderr: diagnostics.toString() };
|
|
114
|
-
}
|
|
115
|
-
if (!paths.size) return { code: 1, stderr: diagnostics.toString(), stopped: false };
|
|
116
|
-
allowed = paths;
|
|
117
|
-
}
|
|
118
|
-
return { code: 1, stderr: diagnostics.toString(), stopped: false };
|
|
119
|
-
}
|
|
120
|
-
|
|
121
73
|
/** How candidates were selected: rg's candidate lines, or whole files (see Prefilter). */
|
|
122
74
|
type CandidateSource = "lines" | "files" | "all";
|
|
123
75
|
|
|
@@ -127,7 +79,6 @@ type CandidateSource = "lines" | "files" | "all";
|
|
|
127
79
|
*/
|
|
128
80
|
export async function listCandidates(options: {
|
|
129
81
|
readonly backend: GrepBackend;
|
|
130
|
-
readonly rgPath: string;
|
|
131
82
|
readonly scope: SearchScope;
|
|
132
83
|
readonly query: GrepQuery;
|
|
133
84
|
readonly signal: AbortSignal;
|
|
@@ -138,7 +89,7 @@ export async function listCandidates(options: {
|
|
|
138
89
|
readonly candidates: CandidateSource;
|
|
139
90
|
readonly scanLimitReached: boolean;
|
|
140
91
|
}> {
|
|
141
|
-
const { backend,
|
|
92
|
+
const { backend, scope, query, signal, stopped, onCandidate } = options;
|
|
142
93
|
if (scope.searchPaths.length === 0) return { candidates: "lines", scanLimitReached: false };
|
|
143
94
|
let candidates: CandidateSource = "lines";
|
|
144
95
|
let listedAny = false;
|
|
@@ -168,7 +119,6 @@ export async function listCandidates(options: {
|
|
|
168
119
|
const patterns = query.multiline ? undefined : lineCandidatePatterns(query);
|
|
169
120
|
let listing = patterns
|
|
170
121
|
? await backend.runRgLines(
|
|
171
|
-
rgPath,
|
|
172
122
|
[
|
|
173
123
|
...COMMON_RG_ARGS,
|
|
174
124
|
...BYTE_PATTERN_ARGS,
|
|
@@ -183,9 +133,13 @@ export async function listCandidates(options: {
|
|
|
183
133
|
: undefined;
|
|
184
134
|
// rg reports a pattern it cannot compile, such as one beyond its size limit, before any file.
|
|
185
135
|
if (!listing || (listing.code === 2 && !listedAny)) {
|
|
186
|
-
const prefilter = extractPrefilter(query);
|
|
136
|
+
const prefilter = boundedPrefilter(extractPrefilter(query));
|
|
187
137
|
candidates = prefilter.kind === "all" ? "all" : "files";
|
|
188
|
-
listing = await
|
|
138
|
+
listing = await backend.runRgPaths(
|
|
139
|
+
[...candidateArgs(prefilter), "--null", ...scoped],
|
|
140
|
+
signal,
|
|
141
|
+
offer,
|
|
142
|
+
);
|
|
189
143
|
}
|
|
190
144
|
return { listing, candidates, scanLimitReached };
|
|
191
145
|
}
|
|
@@ -40,7 +40,7 @@ test("Cartesian JS syntax × all flag combinations → tool entry agrees with V8
|
|
|
40
40
|
for (const pattern of patterns) {
|
|
41
41
|
for (const flags of flagsCases) {
|
|
42
42
|
const normalizedFlags = [...new Set(flags)].join("");
|
|
43
|
-
let field: "flags" | "pattern" | undefined;
|
|
43
|
+
let field: "flags" | "pattern[0]" | undefined;
|
|
44
44
|
try {
|
|
45
45
|
new RegExp("", normalizedFlags);
|
|
46
46
|
} catch (error) {
|
|
@@ -52,7 +52,7 @@ test("Cartesian JS syntax × all flag combinations → tool entry agrees with V8
|
|
|
52
52
|
new RegExp(pattern, normalizedFlags);
|
|
53
53
|
} catch (error) {
|
|
54
54
|
if (!(error instanceof SyntaxError)) throw error;
|
|
55
|
-
field = "pattern";
|
|
55
|
+
field = "pattern[0]";
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
const before = discoveries;
|
|
@@ -79,7 +79,7 @@ test("out-of-order file completions → listing order fixes rows, warnings, and
|
|
|
79
79
|
);
|
|
80
80
|
const backend: GrepBackend = {
|
|
81
81
|
runRgPaths: async () => assert.fail("This query uses line candidates"),
|
|
82
|
-
runRgLines: async (
|
|
82
|
+
runRgLines: async (_args, _signal, _maxLines, onFile) => {
|
|
83
83
|
for (const name of names) {
|
|
84
84
|
if (!(await onFile(join(dir, name), undefined))) {
|
|
85
85
|
return { code: 2, stderr: listingDiagnostic, stopped: true };
|
|
@@ -179,9 +179,9 @@ test("match limit reached before a repeated candidate → stop listing before la
|
|
|
179
179
|
};
|
|
180
180
|
const result = await callTool(
|
|
181
181
|
makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, {
|
|
182
|
-
runRgLines: (
|
|
182
|
+
runRgLines: (_args, _signal, _maxLines, onFile) =>
|
|
183
183
|
list((path) => onFile(path, undefined)),
|
|
184
|
-
runRgPaths: (
|
|
184
|
+
runRgPaths: (_args, _signal, onFile) => list(onFile),
|
|
185
185
|
}),
|
|
186
186
|
{ path: file, ...query, limit: 1, outputMode: "files" },
|
|
187
187
|
);
|
|
@@ -231,7 +231,7 @@ test("file-associated failure → listing position decides failure or cutoff, wh
|
|
|
231
231
|
] as const;
|
|
232
232
|
const backend: GrepBackend = {
|
|
233
233
|
runRgPaths: async () => assert.fail("This query uses line candidates"),
|
|
234
|
-
runRgLines: async (
|
|
234
|
+
runRgLines: async (_args, _signal, _maxLines, onFile) => {
|
|
235
235
|
for (const name of names) if (!(await onFile(join(dir, name), undefined))) break;
|
|
236
236
|
return { code: 0, stderr: "", stopped: false };
|
|
237
237
|
},
|