@criogaid/pi-hashline-edit 0.12.0 → 0.13.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/LICENSE +1 -0
- package/README.md +59 -16
- package/README.zh-CN.md +59 -16
- package/package.json +2 -2
- package/src/core/AGENTS.md +2 -2
- package/src/core/apply.test.ts +13 -0
- package/src/core/apply.ts +5 -9
- package/src/core/case-fold.test.ts +16 -25
- package/src/core/case-fold.ts +4 -4
- package/src/core/character-set.test.ts +1 -6
- package/src/core/character-set.ts +7 -61
- package/src/core/regex.ts +2 -36
- package/src/core/search-analysis.test.ts +66 -61
- package/src/core/search-analysis.ts +24 -39
- package/src/core/search-matrix.test.ts +2 -2
- package/src/core/search-matrix.testing.ts +6 -16
- package/src/core/search-schema.ts +0 -1
- package/src/core/search.test.ts +6 -19
- package/src/core/search.ts +10 -8
- package/src/index.ts +27 -2
- package/src/integration/config-registration.test.ts +77 -4
- package/src/integration/fusion-dispatch.test.ts +47 -2
- package/src/integration/grep-errors.test.ts +31 -0
- package/src/integration/grep-flags.test.ts +13 -196
- package/src/integration/grep-js-completeness.test.ts +41 -8
- package/src/integration/grep-line-candidates.test.ts +3 -33
- package/src/integration/grep-matrix.test.ts +1 -1
- package/src/integration/grep-path-matrix.test.ts +59 -65
- package/src/integration/grep-prefilter-coverage.test.ts +15 -8
- package/src/integration/grep-process-limits.test.ts +46 -0
- package/src/integration/grep-regex.test.ts +7 -2
- package/src/integration/grep-rg.test.ts +63 -12
- package/src/integration/grep-translation.test.ts +14 -7
- package/src/integration/model-budget-lifecycle.test.ts +41 -2
- package/src/integration/npm-package.testing.ts +2 -3
- package/src/integration/replace-package.testing.ts +2 -2
- package/src/pi/AGENTS.md +21 -13
- package/src/pi/action-fusion.test.ts +38 -4
- package/src/pi/action-fusion.ts +23 -19
- package/src/pi/argument-diagnostics.ts +28 -48
- package/src/pi/argument-validation.test.ts +61 -27
- package/src/pi/argument-validation.ts +34 -0
- package/src/pi/budgets.ts +19 -10
- package/src/pi/config.test.ts +1 -1
- package/src/pi/diagnostic-buffer.test.ts +1 -0
- package/src/pi/edit-tool.ts +1 -1
- package/src/pi/failure-context.test.ts +23 -2
- package/src/pi/failure-context.ts +36 -42
- package/src/pi/failure-path.test.ts +8 -3
- package/src/pi/file-commit.test.ts +109 -52
- package/src/pi/file-commit.ts +62 -56
- package/src/pi/fusion-card.ts +3 -2
- package/src/pi/grep-candidates.test.ts +5 -5
- package/src/pi/grep-candidates.ts +81 -50
- package/src/pi/grep-matrix.test.ts +14 -29
- package/src/pi/grep-order.test.ts +132 -9
- package/src/pi/grep-output.ts +169 -51
- package/src/pi/grep-render.ts +40 -34
- package/src/pi/grep-rg.testing.ts +9 -6
- package/src/pi/grep-scope.ts +51 -29
- package/src/pi/grep-search.ts +96 -71
- package/src/pi/grep-tool.ts +13 -22
- package/src/pi/grep.test.ts +310 -84
- package/src/pi/model-budgets.test.ts +56 -6
- package/src/pi/model-budgets.ts +7 -4
- package/src/pi/mutation-result.test.ts +25 -1
- package/src/pi/mutation-result.ts +7 -2
- package/src/pi/mutation-runner.ts +3 -4
- package/src/pi/output-budgets.test.ts +3 -1
- package/src/pi/path.ts +18 -4
- package/src/pi/pi.test.ts +22 -0
- package/src/pi/read-delegation.test.ts +119 -0
- package/src/pi/read-tool.ts +109 -61
- package/src/pi/read.test.ts +306 -17
- package/src/pi/regex-worker.test.ts +42 -0
- package/src/pi/regex-worker.ts +17 -11
- package/src/pi/registration.test.ts +157 -102
- package/src/pi/render.test.ts +54 -0
- package/src/pi/render.ts +9 -9
- package/src/pi/replace-tool.ts +5 -2
- package/src/pi/report-schema.ts +52 -31
- package/src/pi/report.test.ts +60 -10
- package/src/pi/rg-process.ts +41 -5
- package/src/pi/rg-regex.test.ts +12 -52
- package/src/pi/rg-regex.ts +29 -77
- package/src/pi/text-stream.test.ts +3 -0
- package/src/pi/text-stream.ts +1 -1
- package/src/testing/regex-translation-corpus.ts +39 -96
package/src/pi/grep.test.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { stripVTControlCharacters } from "node:util";
|
|
1
2
|
import type { MockRg } from "./grep-rg.testing.ts";
|
|
2
3
|
import { rejectsArgument, rejectsArguments } from "./argument-error.testing.ts";
|
|
3
4
|
/**
|
|
@@ -11,7 +12,7 @@ import { syncBuiltinESMExports } from "node:module";
|
|
|
11
12
|
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
12
13
|
import fsPromises from "node:fs/promises";
|
|
13
14
|
import { tmpdir } from "node:os";
|
|
14
|
-
import { join } from "node:path";
|
|
15
|
+
import { join, toNamespacedPath } from "node:path";
|
|
15
16
|
import { initTheme } from "@earendil-works/pi-coding-agent";
|
|
16
17
|
import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
|
|
17
18
|
import { computeLineHash } from "../core/hash.ts";
|
|
@@ -40,6 +41,7 @@ type FakeOptions = {
|
|
|
40
41
|
onRun?: () => void | Promise<void>;
|
|
41
42
|
paths?: string[];
|
|
42
43
|
lines?: readonly number[];
|
|
44
|
+
rejectsPatterns?: boolean;
|
|
43
45
|
};
|
|
44
46
|
|
|
45
47
|
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
@@ -52,7 +54,7 @@ async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
function fakeBackend(options: FakeOptions = {}) {
|
|
55
|
-
const calls: { kind: "paths" | "lines"; args: readonly string[] }[] = [];
|
|
57
|
+
const calls: { kind: "paths" | "lines" | "probe"; args: readonly string[] }[] = [];
|
|
56
58
|
const run = async (
|
|
57
59
|
kind: "paths" | "lines",
|
|
58
60
|
args: readonly string[],
|
|
@@ -70,11 +72,15 @@ function fakeBackend(options: FakeOptions = {}) {
|
|
|
70
72
|
stopped: false,
|
|
71
73
|
};
|
|
72
74
|
};
|
|
73
|
-
const backend
|
|
75
|
+
const backend = {
|
|
74
76
|
runRgPaths: (args, _signal, onPath) => run("paths", args, onPath),
|
|
75
77
|
runRgLines: (args, _signal, _maxLines, onFile) =>
|
|
76
78
|
run("lines", args, (file) => onFile(file, options.lines)),
|
|
77
|
-
|
|
79
|
+
rgRejectsPatterns: async (args) => {
|
|
80
|
+
calls.push({ kind: "probe", args });
|
|
81
|
+
return options.rejectsPatterns ?? false;
|
|
82
|
+
},
|
|
83
|
+
} satisfies MockRg;
|
|
78
84
|
return { backend, calls };
|
|
79
85
|
}
|
|
80
86
|
|
|
@@ -82,17 +88,17 @@ const text = (result: any): string => result.content[0].text;
|
|
|
82
88
|
const call = (tool: any, params: any, signal?: AbortSignal) =>
|
|
83
89
|
callTool(tool, params, { toolCallId: "0", signal });
|
|
84
90
|
|
|
85
|
-
test("
|
|
91
|
+
test("regex patterns → call rendering shows the pattern without trailing flags", () => {
|
|
86
92
|
initTheme("dark");
|
|
87
93
|
const { backend } = fakeBackend();
|
|
88
94
|
const tool = makeMockGrep(process.cwd(), DEFAULT_CONFIG, backend);
|
|
89
95
|
const rendered = tool.renderCall!(
|
|
90
|
-
{ pattern: ["
|
|
96
|
+
{ pattern: ["needle", "other"], literal: false, ignoreCase: true },
|
|
91
97
|
theme,
|
|
92
98
|
)
|
|
93
99
|
.render(120)
|
|
94
100
|
.join("\n");
|
|
95
|
-
assert.
|
|
101
|
+
assert.match(stripVTControlCharacters(rendered), /\/needle \| other\/(?:\s|$)/);
|
|
96
102
|
});
|
|
97
103
|
|
|
98
104
|
test("streamed grep strings and null fields → render the pattern and default path without a limit", () => {
|
|
@@ -227,35 +233,143 @@ test("grep points wildcard paths to glob without changing ordinary missing-path
|
|
|
227
233
|
});
|
|
228
234
|
});
|
|
229
235
|
|
|
230
|
-
test("
|
|
236
|
+
test("some grep paths are missing → searches existing paths and reports missing paths in input order", async (t) =>
|
|
237
|
+
withDir(async (dir) => {
|
|
238
|
+
const existing = ["present.txt", "second.txt"];
|
|
239
|
+
await Promise.all(existing.map((name) => writeFile(join(dir, name), "value\n")));
|
|
240
|
+
for (const [path, missing, limit] of [
|
|
241
|
+
[["missing.txt", ...existing], ["missing.txt"], 10],
|
|
242
|
+
[["other.txt", "present.txt", "missing.txt", "second.txt"], ["other.txt", "missing.txt"], 10],
|
|
243
|
+
[["missing.txt", ...existing, "other.txt"], ["missing.txt", "other.txt"], 1],
|
|
244
|
+
] as const) {
|
|
245
|
+
await t.test(`${path.join(", ")}; limit=${limit}`, async () => {
|
|
246
|
+
const fake = fakeBackend({ paths: existing.map((name) => join(dir, name)) });
|
|
247
|
+
const tool = makeMockGrep(dir, DEFAULT_CONFIG, fake.backend);
|
|
248
|
+
const result = await callTool(tool, {
|
|
249
|
+
literal: true,
|
|
250
|
+
pattern: ["value"],
|
|
251
|
+
path,
|
|
252
|
+
limit,
|
|
253
|
+
context: 0,
|
|
254
|
+
});
|
|
255
|
+
assert.equal(result.isError, undefined);
|
|
256
|
+
const report = reportOf(result, "grep");
|
|
257
|
+
assert.deepEqual(
|
|
258
|
+
report?.missing,
|
|
259
|
+
missing.map((name) => join(dir, name)),
|
|
260
|
+
);
|
|
261
|
+
assert.match(report?.next ?? "", /Correct the paths in missing/);
|
|
262
|
+
const payload = text(result);
|
|
263
|
+
for (const name of existing.slice(0, limit))
|
|
264
|
+
assert.ok(payload.includes(`${name} · 1 match`));
|
|
265
|
+
assert.deepEqual(
|
|
266
|
+
payload.split("\n").filter((line) => /^\d+#/.test(line)),
|
|
267
|
+
existing
|
|
268
|
+
.slice(0, limit)
|
|
269
|
+
.map(() => `1#${computeLineHash(1, "value", DEFAULT_CONFIG.hashLen)}│value`),
|
|
270
|
+
);
|
|
271
|
+
assert.deepEqual(
|
|
272
|
+
fake.calls[0].args.slice(-existing.length),
|
|
273
|
+
existing.map((name) => join(dir, name)),
|
|
274
|
+
);
|
|
275
|
+
if (limit === 1) assert.equal(report?.matchLimit, 1);
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
}));
|
|
279
|
+
|
|
280
|
+
test("question mark in the workspace path → a missing relative name does not hide existing matches", async () =>
|
|
281
|
+
withDir(async (dir) => {
|
|
282
|
+
const cwd = process.platform === "win32" ? toNamespacedPath(dir) : join(dir, "work?");
|
|
283
|
+
if (process.platform !== "win32") await mkdir(cwd);
|
|
284
|
+
const file = join(cwd, "present.txt");
|
|
285
|
+
await writeFile(file, "value\n");
|
|
286
|
+
const { backend } = fakeBackend({ paths: [file] });
|
|
287
|
+
const result = await callTool(makeMockGrep(cwd, DEFAULT_CONFIG, backend), {
|
|
288
|
+
literal: true,
|
|
289
|
+
pattern: "value",
|
|
290
|
+
path: ["missing.txt", "present.txt"],
|
|
291
|
+
outputMode: "count",
|
|
292
|
+
});
|
|
293
|
+
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
294
|
+
assert.equal(grepCounts(result).matches, 1);
|
|
295
|
+
assert.deepEqual(reportOf(result, "grep")?.missing, [join(cwd, "missing.txt")]);
|
|
296
|
+
}));
|
|
297
|
+
|
|
298
|
+
test("every grep path is missing → reports all resolved paths with default recovery", async (t) =>
|
|
299
|
+
withDir(async (dir) => {
|
|
300
|
+
for (const path of [["missing.txt"], ["other.txt", "missing.txt"]]) {
|
|
301
|
+
await t.test(path.join(", "), async () => {
|
|
302
|
+
const fake = fakeBackend();
|
|
303
|
+
const tool = makeMockGrep(dir, DEFAULT_CONFIG, fake.backend);
|
|
304
|
+
await expectToolFailure(
|
|
305
|
+
callTool(tool, { literal: true, pattern: ["value"], path }),
|
|
306
|
+
"PATH_NOT_FOUND",
|
|
307
|
+
(report) => {
|
|
308
|
+
assert.deepEqual(
|
|
309
|
+
report.missing,
|
|
310
|
+
path.map((name) => join(dir, name)),
|
|
311
|
+
);
|
|
312
|
+
assert.match(report.next ?? "", /Check the path/);
|
|
313
|
+
},
|
|
314
|
+
);
|
|
315
|
+
assert.equal(fake.calls.length, 0);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
}));
|
|
319
|
+
|
|
320
|
+
test("a missing grep path contains a wildcard alongside an existing path → reports all missing paths with glob recovery", async (t) =>
|
|
231
321
|
withDir(async (dir) => {
|
|
232
322
|
await writeFile(join(dir, "present.txt"), "value\n");
|
|
233
|
-
for (const [path, missing
|
|
234
|
-
[
|
|
235
|
-
|
|
236
|
-
|
|
323
|
+
for (const [path, missing] of [
|
|
324
|
+
[
|
|
325
|
+
["missing.txt", "present.txt", "other*.txt"],
|
|
326
|
+
["missing.txt", "other*.txt"],
|
|
327
|
+
],
|
|
328
|
+
[
|
|
329
|
+
["missing?.txt", "present.txt", "other.txt"],
|
|
330
|
+
["missing?.txt", "other.txt"],
|
|
331
|
+
],
|
|
237
332
|
] as const) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
333
|
+
await t.test(path.join(", "), async () => {
|
|
334
|
+
const fake = fakeBackend();
|
|
335
|
+
const tool = makeMockGrep(dir, DEFAULT_CONFIG, fake.backend);
|
|
336
|
+
await expectToolFailure(
|
|
337
|
+
callTool(tool, { literal: true, pattern: ["value"], path }),
|
|
338
|
+
"PATH_NOT_FOUND",
|
|
339
|
+
(report) => {
|
|
340
|
+
assert.deepEqual(
|
|
341
|
+
report.missing,
|
|
342
|
+
missing.map((name) => join(dir, name)),
|
|
343
|
+
);
|
|
344
|
+
assert.match(report.next ?? "", /directory.*path.*wildcard.*glob/);
|
|
345
|
+
},
|
|
346
|
+
);
|
|
347
|
+
assert.equal(fake.calls.length, 0);
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}));
|
|
351
|
+
|
|
352
|
+
test("existing grep paths have no matches alongside a missing path → succeeds with zero totals and missing paths", async (t) =>
|
|
353
|
+
withDir(async (dir) => {
|
|
354
|
+
const file = join(dir, "present.txt");
|
|
355
|
+
await writeFile(file, "other content\n");
|
|
356
|
+
for (const outputMode of ["content", "files", "count"] as const) {
|
|
357
|
+
await t.test(outputMode, async () => {
|
|
358
|
+
const fake = fakeBackend({ paths: [file] });
|
|
359
|
+
const tool = makeMockGrep(dir, DEFAULT_CONFIG, fake.backend);
|
|
360
|
+
const result = await callTool(tool, {
|
|
361
|
+
literal: true,
|
|
362
|
+
pattern: ["value"],
|
|
363
|
+
path: ["present.txt", "missing.txt"],
|
|
364
|
+
outputMode,
|
|
365
|
+
});
|
|
366
|
+
assert.equal(result.isError, undefined);
|
|
367
|
+
const report = reportOf(result, "grep");
|
|
368
|
+
assert.equal(report?.matches, 0);
|
|
369
|
+
assert.equal(report?.files, 0);
|
|
370
|
+
assert.deepEqual(report?.missing, [join(dir, "missing.txt")]);
|
|
371
|
+
assert.match(report?.next ?? "", /Correct the paths in missing/);
|
|
372
|
+
});
|
|
259
373
|
}
|
|
260
374
|
}));
|
|
261
375
|
|
|
@@ -333,7 +447,8 @@ test("grep output byte limit → recovery follows output mode and effective cont
|
|
|
333
447
|
await Promise.all(
|
|
334
448
|
paths.map((path) => writeFile(path, "before\nneedle\nafter\nneedle\nafter\nneedle\nafter\n")),
|
|
335
449
|
);
|
|
336
|
-
|
|
450
|
+
// Room for the header and one hit, but not its preceding context as well.
|
|
451
|
+
const budget = { ...DEFAULT_OUTPUT_BUDGETS, grepBytes: 48 };
|
|
337
452
|
const config = {
|
|
338
453
|
...DEFAULT_CONFIG,
|
|
339
454
|
grep: { ...DEFAULT_CONFIG.grep, defaultContext: 2 },
|
|
@@ -362,10 +477,88 @@ test("grep output byte limit → recovery follows output mode and effective cont
|
|
|
362
477
|
}
|
|
363
478
|
}));
|
|
364
479
|
|
|
480
|
+
test("grep byte limit with every line matched → removing context is not suggested", async () =>
|
|
481
|
+
withDir(async (dir) => {
|
|
482
|
+
const path = join(dir, "all.txt");
|
|
483
|
+
await writeFile(path, `needle ${"x".repeat(100)}\n`.repeat(200));
|
|
484
|
+
const { backend } = fakeBackend({ paths: [path] });
|
|
485
|
+
const tool = makeMockGrep(dir, DEFAULT_CONFIG, backend, () => outputBudgetsFor(4096));
|
|
486
|
+
const args = { path, pattern: ["needle"], literal: true, limit: 200 };
|
|
487
|
+
const withContext = await callTool(tool, { ...args, context: 1 });
|
|
488
|
+
const withoutContext = await callTool(tool, { ...args, context: 0 });
|
|
489
|
+
assert.equal(text(withContext), text(withoutContext));
|
|
490
|
+
assert.ok(reportOf(withContext, "grep")?.outputLimit);
|
|
491
|
+
assert.equal(reportOf(withContext, "grep")?.next, reportOf(withoutContext, "grep")?.next);
|
|
492
|
+
}));
|
|
493
|
+
|
|
494
|
+
test("small grep payload and a long multibyte path → keeps the only anchored match", async () =>
|
|
495
|
+
withDir(async (dir) => {
|
|
496
|
+
const subdir = join(dir, "项目".repeat(25));
|
|
497
|
+
const path = join(subdir, `${"文档".repeat(25)}.md`);
|
|
498
|
+
await mkdir(subdir);
|
|
499
|
+
const pattern = "命中";
|
|
500
|
+
const line = pattern + "文".repeat(600);
|
|
501
|
+
await writeFile(path, `${line}\n`);
|
|
502
|
+
const { backend } = fakeBackend({ paths: [path] });
|
|
503
|
+
const budget = outputBudgetsFor(4_096);
|
|
504
|
+
const result = await callTool(
|
|
505
|
+
makeMockGrep(dir, DEFAULT_CONFIG, backend, () => budget),
|
|
506
|
+
{
|
|
507
|
+
path,
|
|
508
|
+
pattern,
|
|
509
|
+
literal: true,
|
|
510
|
+
context: 0,
|
|
511
|
+
limit: 1,
|
|
512
|
+
},
|
|
513
|
+
);
|
|
514
|
+
|
|
515
|
+
const payload = text(result);
|
|
516
|
+
const [, row = ""] = payload.split("\n");
|
|
517
|
+
const parsedRow = parseHashline(row);
|
|
518
|
+
assert.ok(parsedRow, `Expected an anchored match row after the header:\n${payload}`);
|
|
519
|
+
assert.equal(parsedRow.lineNo, "1");
|
|
520
|
+
assert.ok(Buffer.byteLength(payload) <= budget.grepBytes);
|
|
521
|
+
|
|
522
|
+
const marker = /^\[partial, columns 1-(\d+)\]/.exec(parsedRow.content);
|
|
523
|
+
assert.ok(marker);
|
|
524
|
+
const width = Number(marker[1]);
|
|
525
|
+
assert.equal(parsedRow.content, `${marker[0]} ${line.slice(0, width)}…`);
|
|
526
|
+
const report = reportOf(result, "grep");
|
|
527
|
+
assert.ok(report);
|
|
528
|
+
assert.equal(report.linePreviewLimit, width);
|
|
529
|
+
assert.equal(report.outputLimit, undefined);
|
|
530
|
+
}));
|
|
531
|
+
|
|
532
|
+
test("a tiny row budget at an astral match → shows the complete character or just an ellipsis", async () =>
|
|
533
|
+
withDir(async (dir) => {
|
|
534
|
+
const path = join(dir, "a.txt");
|
|
535
|
+
const pattern = "🍊";
|
|
536
|
+
await writeFile(path, pattern.repeat(20));
|
|
537
|
+
const { backend } = fakeBackend({ paths: [path] });
|
|
538
|
+
const args = { path, pattern, literal: true };
|
|
539
|
+
const full = await callTool(makeMockGrep(dir, DEFAULT_CONFIG, backend), args);
|
|
540
|
+
const prefix = text(full).split(pattern)[0];
|
|
541
|
+
for (const [preview, kept] of [
|
|
542
|
+
[`${pattern}…`, 2],
|
|
543
|
+
["…", 0],
|
|
544
|
+
] as const) {
|
|
545
|
+
const expected = prefix + preview;
|
|
546
|
+
const result = await callTool(
|
|
547
|
+
makeMockGrep(dir, DEFAULT_CONFIG, backend, () => ({
|
|
548
|
+
...DEFAULT_OUTPUT_BUDGETS,
|
|
549
|
+
grepBytes: Buffer.byteLength(expected),
|
|
550
|
+
})),
|
|
551
|
+
args,
|
|
552
|
+
);
|
|
553
|
+
assert.equal(text(result), expected);
|
|
554
|
+
assert.equal(reportOf(result, "grep")?.linePreviewLimit, kept);
|
|
555
|
+
}
|
|
556
|
+
}));
|
|
557
|
+
|
|
365
558
|
test("grep output truncates preview rows → reports only displayed preview facts", async () =>
|
|
366
559
|
withDir(async (dir) => {
|
|
367
560
|
const paths = [join(dir, "a.txt"), join(dir, "z.txt")];
|
|
368
|
-
const budget =
|
|
561
|
+
const budget = DEFAULT_OUTPUT_BUDGETS;
|
|
369
562
|
await writeFile(paths[0], "needle\n".repeat(3));
|
|
370
563
|
await writeFile(
|
|
371
564
|
paths[1],
|
|
@@ -838,22 +1031,21 @@ test("literal pipe patterns find no match → alternatives recovery preserves li
|
|
|
838
1031
|
|
|
839
1032
|
test("invalid JavaScript → its field rejects before file discovery", async () =>
|
|
840
1033
|
withDir(async (dir) => {
|
|
841
|
-
for (const [pattern,
|
|
842
|
-
["(",
|
|
843
|
-
[["plain", "("],
|
|
844
|
-
["(?m)^foo",
|
|
845
|
-
[["plain", "(?m)^foo"],
|
|
846
|
-
["(?i)abc",
|
|
847
|
-
[["plain", "(?i)abc"],
|
|
848
|
-
["
|
|
849
|
-
["queueTool(",
|
|
850
|
-
[
|
|
851
|
-
["*", undefined, "pattern[0]"],
|
|
1034
|
+
for (const [pattern, field] of [
|
|
1035
|
+
["(", "pattern[0]"],
|
|
1036
|
+
[["plain", "("], "pattern[1]"],
|
|
1037
|
+
["(?m)^foo", "pattern[0]"],
|
|
1038
|
+
[["plain", "(?m)^foo"], "pattern[1]"],
|
|
1039
|
+
["(?i)abc", "pattern[0]"],
|
|
1040
|
+
[["plain", "(?i)abc"], "pattern[1]"],
|
|
1041
|
+
["queueTool(", "pattern[0]"],
|
|
1042
|
+
[["queueTool(", "\\bfoo\\b"], "pattern[0]"],
|
|
1043
|
+
["*", "pattern[0]"],
|
|
852
1044
|
] as const) {
|
|
853
1045
|
const fake = fakeBackend();
|
|
854
1046
|
const tool = makeMockGrep(dir, DEFAULT_CONFIG, fake.backend);
|
|
855
1047
|
await assert.rejects(
|
|
856
|
-
call(tool, { path: "missing.txt", pattern, literal: false
|
|
1048
|
+
call(tool, { path: "missing.txt", pattern, literal: false }),
|
|
857
1049
|
rejectsArgument(field, /Invalid regular expression/),
|
|
858
1050
|
);
|
|
859
1051
|
assert.equal(fake.calls.length, 0);
|
|
@@ -909,7 +1101,7 @@ test("reports empty output and ripgrep execution failures", async () => {
|
|
|
909
1101
|
assert.equal(reportOf(noMatches, "grep")?.matches, 0);
|
|
910
1102
|
assert.equal(reportOf(noMatches, "grep")?.files, 0);
|
|
911
1103
|
|
|
912
|
-
const failed = fakeBackend({ code: 2, stderr: "bad regex" });
|
|
1104
|
+
const failed = fakeBackend({ code: 2, stderr: "bad regex", rejectsPatterns: true });
|
|
913
1105
|
await expectToolFailure(
|
|
914
1106
|
call(makeMockGrep(dir, DEFAULT_CONFIG, failed.backend), {
|
|
915
1107
|
pattern: "x",
|
|
@@ -1091,17 +1283,6 @@ test("candidate listing reports errors → all output modes retain confirmed mat
|
|
|
1091
1283
|
);
|
|
1092
1284
|
}
|
|
1093
1285
|
}
|
|
1094
|
-
const fake = fakeBackend({ code: 2, stderr: "Permission denied" });
|
|
1095
|
-
await expectToolFailure(
|
|
1096
|
-
call(makeMockGrep(dir, DEFAULT_CONFIG, fake.backend), {
|
|
1097
|
-
literal: true,
|
|
1098
|
-
pattern: "needle",
|
|
1099
|
-
}),
|
|
1100
|
-
"SEARCH_INCOMPLETE",
|
|
1101
|
-
(report) => {
|
|
1102
|
-
assert.match(JSON.stringify(report.diagnostics), /Permission denied/);
|
|
1103
|
-
},
|
|
1104
|
-
);
|
|
1105
1286
|
});
|
|
1106
1287
|
});
|
|
1107
1288
|
|
|
@@ -1232,17 +1413,13 @@ test("invalid worker replies → the entire grep call fails", async (t) => {
|
|
|
1232
1413
|
const path = join(dir, "input.txt");
|
|
1233
1414
|
await writeFile(path, "😀\n");
|
|
1234
1415
|
for (const result of [
|
|
1235
|
-
{ lines: [{ lineNumber:
|
|
1236
|
-
{
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
},
|
|
1243
|
-
{ lines: [{ lineNumber: 1, column: 0 }], totalLines: 2 },
|
|
1244
|
-
{ lines: [{ lineNumber: 1, column: -1 }], totalLines: 1 },
|
|
1245
|
-
{ lines: [{ lineNumber: 1, column: 999 }], totalLines: 1 },
|
|
1416
|
+
{ lines: [{ lineNumber: 1, column: 0 }], extra: true },
|
|
1417
|
+
{ lines: [{ lineNumber: 1, column: 0, extra: true }] },
|
|
1418
|
+
{ lines: [{ lineNumber: 0, column: 0 }] },
|
|
1419
|
+
{ lines: [{ lineNumber: 1, column: -1 }] },
|
|
1420
|
+
{ lines: [{ lineNumber: 1.5, column: 0 }] },
|
|
1421
|
+
{ lines: [{ lineNumber: 1, column: 0.5 }] },
|
|
1422
|
+
{},
|
|
1246
1423
|
]) {
|
|
1247
1424
|
await t.test(`${JSON.stringify(result)} → REGEX_WORKER_FAILED`, async (t) => {
|
|
1248
1425
|
const emit = Worker.prototype.emit;
|
|
@@ -1302,7 +1479,6 @@ test("non-multiline or multiline query → use line candidates or the file prefi
|
|
|
1302
1479
|
glob: ["*", "!*.bak"],
|
|
1303
1480
|
pattern: ["a", "b"],
|
|
1304
1481
|
literal: true,
|
|
1305
|
-
flags: "",
|
|
1306
1482
|
multiline,
|
|
1307
1483
|
ignoreCase: false,
|
|
1308
1484
|
};
|
|
@@ -1310,7 +1486,7 @@ test("non-multiline or multiline query → use line candidates or the file prefi
|
|
|
1310
1486
|
assert.equal(grepCounts(result).matches, 2);
|
|
1311
1487
|
assert.equal(fake.calls.length, 1);
|
|
1312
1488
|
assert.equal(fake.calls[0].kind, multiline ? "paths" : "lines");
|
|
1313
|
-
const query = { patterns: params.pattern, literal: true,
|
|
1489
|
+
const query = { patterns: params.pattern, literal: true, ignoreCase: false, multiline };
|
|
1314
1490
|
const candidates = lineCandidatePatterns(query);
|
|
1315
1491
|
assert.ok(candidates);
|
|
1316
1492
|
const prefix = multiline
|
|
@@ -1338,15 +1514,14 @@ test("non-multiline or multiline query → use line candidates or the file prefi
|
|
|
1338
1514
|
}
|
|
1339
1515
|
}));
|
|
1340
1516
|
|
|
1341
|
-
test("line
|
|
1517
|
+
test("line listing succeeds or reports a file → no pattern probe or fallback", async () =>
|
|
1342
1518
|
withDir(async (dir) => {
|
|
1343
1519
|
const file = join(dir, "input.txt");
|
|
1344
1520
|
await writeFile(file, "needle\n");
|
|
1345
|
-
for (const [code, paths
|
|
1346
|
-
[0, [file]
|
|
1347
|
-
[1, []
|
|
1348
|
-
[2, []
|
|
1349
|
-
[2, [file], ["lines"]],
|
|
1521
|
+
for (const [code, paths] of [
|
|
1522
|
+
[0, [file]],
|
|
1523
|
+
[1, []],
|
|
1524
|
+
[2, [file]],
|
|
1350
1525
|
] as const) {
|
|
1351
1526
|
const fake = fakeBackend({ code, paths: [...paths], lines: [1] });
|
|
1352
1527
|
const result = await callTool(makeMockGrep(dir, DEFAULT_CONFIG, fake.backend), {
|
|
@@ -1356,13 +1531,36 @@ test("line discovery status and emitted files → fallback only for code 2 befor
|
|
|
1356
1531
|
});
|
|
1357
1532
|
assert.deepEqual(
|
|
1358
1533
|
fake.calls.map(({ kind }) => kind),
|
|
1359
|
-
|
|
1534
|
+
["lines"],
|
|
1360
1535
|
);
|
|
1361
|
-
|
|
1362
|
-
else assert.equal(grepCounts(result).matches, paths.length);
|
|
1536
|
+
assert.equal(grepCounts(result).matches, paths.length);
|
|
1363
1537
|
}
|
|
1364
1538
|
}));
|
|
1365
1539
|
|
|
1540
|
+
for (const multiline of [false, true]) {
|
|
1541
|
+
const stage = multiline ? "file prefilter" : "line listing";
|
|
1542
|
+
test(`${stage} exits 2 without candidates and its patterns are accepted → keep the path diagnostic without widening`, async () =>
|
|
1543
|
+
withDir(async (dir) => {
|
|
1544
|
+
const fake = fakeBackend({ code: 2, stderr: "unreadable.txt: Permission denied" });
|
|
1545
|
+
await expectToolFailure(
|
|
1546
|
+
callTool(makeMockGrep(dir, DEFAULT_CONFIG, fake.backend), {
|
|
1547
|
+
path: dir,
|
|
1548
|
+
pattern: "needle",
|
|
1549
|
+
literal: true,
|
|
1550
|
+
multiline,
|
|
1551
|
+
}),
|
|
1552
|
+
"SEARCH_INCOMPLETE",
|
|
1553
|
+
(report) => {
|
|
1554
|
+
assert.match(report.diagnostics ?? "", /unreadable\.txt: Permission denied/);
|
|
1555
|
+
},
|
|
1556
|
+
);
|
|
1557
|
+
assert.deepEqual(
|
|
1558
|
+
fake.calls.map(({ kind }) => kind),
|
|
1559
|
+
[multiline ? "paths" : "lines", "probe"],
|
|
1560
|
+
);
|
|
1561
|
+
}));
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1366
1564
|
test("sparse, dense, or overflowed candidate lines → matching preserves original line numbers and CR", async () =>
|
|
1367
1565
|
withDir(async (dir) => {
|
|
1368
1566
|
const file = join(dir, "input.txt");
|
|
@@ -1384,7 +1582,6 @@ test("sparse, dense, or overflowed candidate lines → matching preserves origin
|
|
|
1384
1582
|
path: file,
|
|
1385
1583
|
pattern: "\\r$",
|
|
1386
1584
|
literal: false,
|
|
1387
|
-
flags: "",
|
|
1388
1585
|
});
|
|
1389
1586
|
assert.equal(grepCounts(result).matches, 1);
|
|
1390
1587
|
assert.match(text(result), /5#[0-9A-Z]+│needle␍/);
|
|
@@ -1425,11 +1622,12 @@ test("rg rejects line patterns before listing files → successful file fallback
|
|
|
1425
1622
|
withDir(async (dir) => {
|
|
1426
1623
|
const file = join(dir, "input.txt");
|
|
1427
1624
|
await writeFile(file, "needle\n");
|
|
1428
|
-
const lines = fakeBackend({ code: 2, stderr: "regex too large" });
|
|
1625
|
+
const lines = fakeBackend({ code: 2, stderr: "regex too large", rejectsPatterns: true });
|
|
1429
1626
|
const paths = fakeBackend({ paths: [file] });
|
|
1430
1627
|
const result = await callTool(
|
|
1431
1628
|
makeMockGrep(dir, DEFAULT_CONFIG, {
|
|
1432
1629
|
runRgLines: lines.backend.runRgLines,
|
|
1630
|
+
rgRejectsPatterns: lines.backend.rgRejectsPatterns,
|
|
1433
1631
|
runRgPaths: paths.backend.runRgPaths,
|
|
1434
1632
|
}),
|
|
1435
1633
|
{ path: dir, pattern: "needle", literal: true },
|
|
@@ -1439,6 +1637,34 @@ test("rg rejects line patterns before listing files → successful file fallback
|
|
|
1439
1637
|
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1440
1638
|
assert.deepEqual(
|
|
1441
1639
|
[...lines.calls, ...paths.calls].map(({ kind }) => kind),
|
|
1442
|
-
["lines", "paths"],
|
|
1640
|
+
["lines", "probe", "paths"],
|
|
1641
|
+
);
|
|
1642
|
+
assert.ok(paths.calls[0].args.includes("--files-with-matches"));
|
|
1643
|
+
}));
|
|
1644
|
+
|
|
1645
|
+
test("rg rejects file prefilter patterns before listing paths → search unfiltered candidates", async () =>
|
|
1646
|
+
withDir(async (dir) => {
|
|
1647
|
+
const file = join(dir, "input.txt");
|
|
1648
|
+
await writeFile(file, "needle\n");
|
|
1649
|
+
const rejected = fakeBackend({ code: 2, stderr: "prefilter rejected", rejectsPatterns: true });
|
|
1650
|
+
const unfiltered = fakeBackend({ paths: [file] });
|
|
1651
|
+
const result = await callTool(
|
|
1652
|
+
makeMockGrep(dir, DEFAULT_CONFIG, {
|
|
1653
|
+
rgRejectsPatterns: rejected.backend.rgRejectsPatterns,
|
|
1654
|
+
runRgPaths: (...args) =>
|
|
1655
|
+
(args[0].includes("--files-with-matches") ? rejected : unfiltered).backend.runRgPaths(
|
|
1656
|
+
...args,
|
|
1657
|
+
),
|
|
1658
|
+
}),
|
|
1659
|
+
{ path: dir, pattern: "needle", literal: true, multiline: true },
|
|
1660
|
+
);
|
|
1661
|
+
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
1662
|
+
assert.equal(grepCounts(result).matches, 1);
|
|
1663
|
+
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1664
|
+
assert.deepEqual(
|
|
1665
|
+
rejected.calls.map(({ kind }) => kind),
|
|
1666
|
+
["paths", "probe"],
|
|
1443
1667
|
);
|
|
1668
|
+
assert.equal(unfiltered.calls.length, 1);
|
|
1669
|
+
assert.ok(unfiltered.calls[0].args.includes("--files"));
|
|
1444
1670
|
}));
|
|
@@ -33,14 +33,64 @@ test("session start and model selection → budgets follow the live model", asyn
|
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
test("unknown or invalid model capacity → bounded defaults are used", () => {
|
|
36
|
-
|
|
36
|
+
assert.deepEqual(DEFAULT_OUTPUT_BUDGETS, {
|
|
37
|
+
readBytes: 64 * 1024,
|
|
38
|
+
grepBytes: 50 * 1024,
|
|
39
|
+
blockBytes: 16 * 1024,
|
|
40
|
+
fieldBytes: 4 * 1024,
|
|
41
|
+
grepPreviewUnits: 500,
|
|
42
|
+
});
|
|
43
|
+
for (const capacity of [
|
|
44
|
+
undefined,
|
|
45
|
+
0,
|
|
46
|
+
-1,
|
|
47
|
+
NaN,
|
|
48
|
+
Infinity,
|
|
49
|
+
-Infinity,
|
|
50
|
+
2.5,
|
|
51
|
+
Number.MAX_SAFE_INTEGER + 1,
|
|
52
|
+
]) {
|
|
37
53
|
assert.equal(outputBudgetsFor(capacity), DEFAULT_OUTPUT_BUDGETS);
|
|
38
54
|
}
|
|
39
55
|
});
|
|
40
56
|
|
|
41
|
-
test("declared capacity
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
57
|
+
test("declared model capacity → read and grep shares have independent byte minimums and scaled previews", () => {
|
|
58
|
+
for (const [capacity, readBytes, grepBytes, grepPreviewUnits] of [
|
|
59
|
+
[1, 512, 512, 500],
|
|
60
|
+
[2_047, 512, 512, 500],
|
|
61
|
+
[2_048, 512, 512, 500],
|
|
62
|
+
[2_052, 513, 512, 500],
|
|
63
|
+
[4_095, 1_023, 512, 500],
|
|
64
|
+
[4_096, 1_024, 512, 500],
|
|
65
|
+
[4_104, 1_026, 513, 500],
|
|
66
|
+
[200_000, 50_000, 25_000, 500],
|
|
67
|
+
[384_000, 96_000, 48_000, 500],
|
|
68
|
+
[384_007, 96_001, 48_000, 500],
|
|
69
|
+
[400_000, 100_000, 50_000, 500],
|
|
70
|
+
[400_799, 100_199, 50_099, 500],
|
|
71
|
+
[400_800, 100_200, 50_100, 501],
|
|
72
|
+
[1_000_000, 250_000, 125_000, 1_250],
|
|
73
|
+
]) {
|
|
74
|
+
const budget = outputBudgetsFor(capacity);
|
|
75
|
+
const scenario = `contextWindow=${capacity}`;
|
|
76
|
+
assert.equal(budget.readBytes, readBytes, scenario);
|
|
77
|
+
assert.equal(budget.grepBytes, grepBytes, scenario);
|
|
78
|
+
assert.equal(budget.grepPreviewUnits, grepPreviewUnits, scenario);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("declared capacity reaches the ceiling → budgets use shares of the capped capacity", () => {
|
|
83
|
+
for (const capacity of [
|
|
84
|
+
MAX_CONTENT_KIB * 1024,
|
|
85
|
+
MAX_CONTENT_KIB * 1024 + 1,
|
|
86
|
+
Number.MAX_SAFE_INTEGER,
|
|
87
|
+
]) {
|
|
88
|
+
assert.deepEqual(outputBudgetsFor(capacity), {
|
|
89
|
+
readBytes: 1_048_576,
|
|
90
|
+
grepBytes: 524_288,
|
|
91
|
+
blockBytes: 1_048_576,
|
|
92
|
+
fieldBytes: 262_144,
|
|
93
|
+
grepPreviewUnits: 5_242,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
46
96
|
});
|
package/src/pi/model-budgets.ts
CHANGED
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import { outputBudgetsFor, type OutputBudgetProvider } from "./budgets.ts";
|
|
4
4
|
|
|
5
|
-
/** Retain Pi's
|
|
5
|
+
/** Retain Pi's live model getter; the headless SDK can reload without emitting session_start. */
|
|
6
6
|
export function registerModelBudgets(pi: ExtensionAPI): OutputBudgetProvider {
|
|
7
7
|
let context: ExtensionContext | undefined;
|
|
8
|
-
|
|
9
|
-
context
|
|
10
|
-
}
|
|
8
|
+
const capture = (_event: unknown, ctx: ExtensionContext) => {
|
|
9
|
+
context ??= ctx;
|
|
10
|
+
};
|
|
11
|
+
pi.on("session_start", capture);
|
|
12
|
+
// Pi emits this before argument preparation, including for nested calls.
|
|
13
|
+
pi.on("tool_execution_start", capture);
|
|
11
14
|
return () => outputBudgetsFor(context?.model?.contextWindow);
|
|
12
15
|
}
|