@criogaid/pi-hashline-edit 0.10.0 → 0.12.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 +21 -7
- package/README.zh-CN.md +22 -8
- package/package.json +7 -6
- package/src/core/AGENTS.md +2 -2
- package/src/core/apply.test.ts +4 -4
- package/src/core/apply.ts +3 -3
- package/src/core/character-set.test.ts +2 -3
- package/src/core/character-set.ts +1 -1
- package/src/core/ranges.test.ts +2 -1
- package/src/core/ranges.ts +1 -1
- package/src/core/regex.ts +6 -2
- package/src/core/replace.test.ts +51 -0
- package/src/core/replace.ts +10 -4
- package/src/core/types.ts +1 -1
- package/src/index.ts +9 -9
- package/src/integration/config-registration.test.ts +8 -10
- package/src/integration/fusion-dispatch.test.ts +2 -2
- package/src/integration/grep-errors.test.ts +3 -3
- package/src/integration/grep-flags.test.ts +5 -5
- package/src/integration/grep-js-completeness.test.ts +12 -11
- package/src/integration/grep-line-candidates.test.ts +5 -3
- package/src/integration/grep-matrix.test.ts +5 -4
- package/src/integration/grep-order.test.ts +4 -4
- package/src/integration/grep-path-matrix.test.ts +5 -4
- package/src/integration/grep-prefilter-coverage.test.ts +17 -5
- package/src/integration/grep-process-limits.test.ts +2 -4
- package/src/integration/grep-regex.test.ts +10 -10
- package/src/integration/grep-rg.test.ts +93 -48
- package/src/integration/grep-scan-budget.test.ts +8 -8
- package/src/integration/grep-translation.test.ts +8 -7
- package/src/integration/large-text.test.ts +5 -5
- package/src/integration/model-budget-lifecycle.test.ts +9 -9
- package/src/integration/mutation-file-types.testing.ts +4 -4
- package/src/integration/npm-package.test.ts +12 -10
- package/src/integration/npm-package.testing.ts +151 -4
- package/src/integration/publication-read-changes.test.ts +54 -54
- package/src/integration/read-changes.test.ts +4 -4
- package/src/integration/replace-worker-boundary.test.ts +1 -1
- package/src/integration/report-replay.test.ts +9 -6
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +13 -12
- package/src/pi/action-fusion.test.ts +125 -105
- package/src/pi/action-fusion.testing.ts +38 -0
- package/src/pi/action-fusion.ts +9 -21
- package/src/pi/argument-diagnostics.ts +4 -3
- package/src/pi/argument-format.test.ts +19 -18
- package/src/pi/argument-validation.test.ts +281 -139
- package/src/pi/argument-validation.ts +21 -31
- package/src/pi/budgets.ts +1 -4
- package/src/pi/codemode.test.ts +7 -2
- package/src/pi/config.test.ts +2 -1
- package/src/pi/config.ts +8 -3
- package/src/pi/diagnostic-buffer.test.ts +23 -13
- package/src/pi/edit-tool.ts +48 -63
- package/src/pi/error-text.ts +7 -2
- package/src/pi/execute.test.ts +178 -157
- package/src/pi/failure-context.test.ts +5 -4
- package/src/pi/failure-context.ts +1 -1
- package/src/pi/failure-path.test.ts +37 -37
- package/src/pi/file-commit.test.ts +47 -19
- package/src/pi/file-commit.ts +36 -9
- package/src/pi/fusion-card.test.ts +22 -21
- package/src/pi/fusion-card.ts +9 -5
- package/src/pi/grep-candidates.test.ts +18 -20
- package/src/pi/grep-candidates.ts +13 -17
- package/src/pi/grep-matrix.test.ts +3 -3
- package/src/pi/grep-order.test.ts +12 -11
- package/src/pi/grep-output.ts +45 -17
- package/src/pi/grep-reread.ts +7 -2
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +10 -10
- package/src/pi/grep-search.ts +8 -11
- package/src/pi/grep-tool.ts +40 -53
- package/src/pi/grep.test.ts +141 -116
- package/src/pi/model-budgets.test.ts +2 -1
- package/src/pi/mutation-outcome.testing.ts +0 -1
- package/src/pi/mutation-result.test.ts +8 -8
- package/src/pi/mutation-result.ts +1 -1
- package/src/pi/mutation-runner.ts +10 -6
- package/src/pi/output-budgets.test.ts +53 -31
- package/src/pi/pi.test.ts +18 -13
- package/src/pi/read-tool.ts +13 -18
- package/src/pi/read.test.ts +67 -41
- package/src/pi/registration.test.ts +25 -19
- package/src/pi/render.test.ts +94 -32
- package/src/pi/render.ts +7 -4
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +6 -16
- package/src/pi/replace.test.ts +45 -34
- package/src/pi/report-failure.testing.ts +9 -1
- package/src/pi/report-schema.ts +225 -86
- package/src/pi/report.test.ts +81 -87
- package/src/pi/report.ts +35 -22
- package/src/pi/result-safety.test.ts +121 -100
- package/src/pi/rg-process.test.ts +2 -2
- package/src/pi/rg-process.ts +9 -15
- package/src/pi/rg-regex.test.ts +13 -0
- package/src/pi/rg-regex.ts +2 -2
- package/src/pi/schema.ts +23 -0
- package/src/pi/tool-factories.testing.ts +59 -0
- package/src/pi/write-tool.ts +9 -15
- package/src/pi/write.test.ts +30 -31
- package/src/testing/run-tests.ts +1 -1
|
@@ -2,10 +2,10 @@ import { rejectsArgument } from "./argument-error.testing.ts";
|
|
|
2
2
|
import { createEditTool } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import { validateToolArguments, type Tool } from "@earendil-works/pi-ai";
|
|
4
4
|
import { makeStrictJsonSchema } from "@earendil-works/pi-ai/api/constrained-sampling";
|
|
5
|
-
import { makeEditOverride } from "./
|
|
6
|
-
import { makeReadOverride } from "./
|
|
5
|
+
import { makeEditOverride } from "./tool-factories.testing.ts";
|
|
6
|
+
import { makeReadOverride } from "./tool-factories.testing.ts";
|
|
7
7
|
import { loadConfig } from "./config.ts";
|
|
8
|
-
import { DEFAULT_CONFIG } from "./
|
|
8
|
+
import { DEFAULT_CONFIG } from "./tool-factories.testing.ts";
|
|
9
9
|
import { mkdir, mkdtemp, rm, writeFile, readFile } from "node:fs/promises";
|
|
10
10
|
import { tmpdir } from "node:os";
|
|
11
11
|
import { join } from "node:path";
|
|
@@ -58,18 +58,18 @@ test("registered tools with or without Fusion → request strict sampling with c
|
|
|
58
58
|
assert.partialDeepStrictEqual(strict, {
|
|
59
59
|
type: "object",
|
|
60
60
|
additionalProperties: false,
|
|
61
|
-
required: actionFusion ? ["path", "
|
|
61
|
+
required: actionFusion ? ["path", "steps", "then_run"] : ["path", "steps"],
|
|
62
62
|
properties: {
|
|
63
|
-
|
|
63
|
+
steps: {
|
|
64
64
|
type: "array",
|
|
65
65
|
items: {
|
|
66
66
|
type: "object",
|
|
67
67
|
additionalProperties: false,
|
|
68
|
-
required: ["action", "anchor", "end", "
|
|
68
|
+
required: ["action", "anchor", "end", "lines"],
|
|
69
69
|
properties: {
|
|
70
70
|
anchor: { anyOf: [{ type: "string" }, { type: "null" }] },
|
|
71
71
|
end: { anyOf: [{ type: "string" }, { type: "null" }] },
|
|
72
|
-
|
|
72
|
+
lines: { anyOf: [{ type: "array" }, { type: "null" }] },
|
|
73
73
|
},
|
|
74
74
|
},
|
|
75
75
|
},
|
|
@@ -78,6 +78,17 @@ test("registered tools with or without Fusion → request strict sampling with c
|
|
|
78
78
|
: {}),
|
|
79
79
|
},
|
|
80
80
|
});
|
|
81
|
+
const properties = strict.properties as Record<
|
|
82
|
+
string,
|
|
83
|
+
{ items?: { properties?: Record<string, unknown> } }
|
|
84
|
+
>;
|
|
85
|
+
assert.deepEqual(
|
|
86
|
+
Object.keys(properties),
|
|
87
|
+
actionFusion ? ["path", "steps", "then_run"] : ["path", "steps"],
|
|
88
|
+
);
|
|
89
|
+
const stepProperties = properties.steps.items?.properties;
|
|
90
|
+
assert.ok(stepProperties);
|
|
91
|
+
assert.deepEqual(Object.keys(stepProperties), ["action", "anchor", "end", "lines"]);
|
|
81
92
|
}
|
|
82
93
|
});
|
|
83
94
|
}
|
|
@@ -171,7 +182,7 @@ test("mutation cards use Fusion by default and explicit false removes command su
|
|
|
171
182
|
);
|
|
172
183
|
const cases = [
|
|
173
184
|
{ name: "write", args: { content: "published\n", mode: "create" } },
|
|
174
|
-
{ name: "edit", args: {
|
|
185
|
+
{ name: "edit", args: { steps: [{ action: "append", lines: ["appended"] }] } },
|
|
175
186
|
{
|
|
176
187
|
name: "replace",
|
|
177
188
|
args: { replacements: [{ literal: true, find: "published", replace: "published" }] },
|
|
@@ -312,8 +323,8 @@ test("mutation cards use Fusion by default and explicit false removes command su
|
|
|
312
323
|
const edit = tools.find((tool) => tool.name === "edit");
|
|
313
324
|
const stale = {
|
|
314
325
|
path: "stale.txt",
|
|
315
|
-
|
|
316
|
-
{ action: "replace", anchor: `1#${computeLineHash(1, "target", 4)}`,
|
|
326
|
+
steps: [
|
|
327
|
+
{ action: "replace", anchor: `1#${computeLineHash(1, "target", 4)}`, lines: ["changed"] },
|
|
317
328
|
],
|
|
318
329
|
then_run: { command: "exit 99" },
|
|
319
330
|
};
|
|
@@ -447,7 +458,7 @@ test("loaded configuration controls hash length and recovery radius", async () =
|
|
|
447
458
|
await expectToolFailure(
|
|
448
459
|
call(makeEditOverride(dir, config), {
|
|
449
460
|
path: "configured.txt",
|
|
450
|
-
|
|
461
|
+
steps: [{ action: "delete", anchor: `1#${computeLineHash(1, "target", hashLen)}` }],
|
|
451
462
|
}),
|
|
452
463
|
"ANCHOR_MISMATCH",
|
|
453
464
|
(report) => {
|
|
@@ -470,19 +481,14 @@ test("configured read defaults bound omitted limits and returned bytes", async (
|
|
|
470
481
|
assert.match(description, /default 2\)/);
|
|
471
482
|
await writeFile(join(dir, "short.txt"), "a\nb\nc\n");
|
|
472
483
|
const paged = await call(read, { path: "short.txt" });
|
|
473
|
-
assert.
|
|
474
|
-
|
|
475
|
-
end: 2,
|
|
476
|
-
totalLines: 3,
|
|
477
|
-
nextOffset: 3,
|
|
478
|
-
});
|
|
479
|
-
assert.match(reportOf(paged, "read")?.next ?? "", /offset.*pagination\.nextOffset/);
|
|
484
|
+
assert.equal(reportOf(paged, "read")?.nextOffset, 3);
|
|
485
|
+
assert.match(reportOf(paged, "read")?.next ?? "", /offset.*nextOffset/);
|
|
480
486
|
assert.match(paged.content[0].text, /^2#[0-9A-Z]+│b$/m);
|
|
481
487
|
assert.doesNotMatch(paged.content[0].text, /^3#/m);
|
|
482
488
|
const explicit = await call(read, { path: "short.txt", limit: 3 });
|
|
483
489
|
assert.match(explicit.content[0].text, /^3#[0-9A-Z]+│c$/m);
|
|
484
490
|
await writeFile(join(dir, "wide.txt"), `${"x".repeat(2048)}\n`);
|
|
485
491
|
const wide = await call(read, { path: "wide.txt" });
|
|
486
|
-
assert.
|
|
492
|
+
assert.equal(reportOf(wide, "read")?.lineTooLong, formatByteSize(1024));
|
|
487
493
|
assert.doesNotMatch(wide.content[0].text, /^\d+#/m);
|
|
488
494
|
}));
|
package/src/pi/render.test.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { commandCompletion, type SessionCommand } from "./action-fusion.testing.ts";
|
|
1
2
|
import { argumentError } from "./argument-error.testing.ts";
|
|
2
3
|
import { test } from "node:test";
|
|
3
4
|
import assert from "node:assert/strict";
|
|
@@ -9,25 +10,26 @@ import { visibleWidth } from "@earendil-works/pi-tui";
|
|
|
9
10
|
import { initTheme, type ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
10
11
|
import type { TSchema } from "typebox";
|
|
11
12
|
import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
|
|
12
|
-
import { makeEditOverride } from "./
|
|
13
|
-
import { makeReplaceTool } from "./
|
|
14
|
-
import { diffCardRenderers, reportLines } from "./render.ts";
|
|
13
|
+
import { makeEditOverride } from "./tool-factories.testing.ts";
|
|
14
|
+
import { makeReplaceTool } from "./tool-factories.testing.ts";
|
|
15
|
+
import { MUTATION_CARD_SHOWN, diffCardRenderers, reportLines } from "./render.ts";
|
|
15
16
|
import { withMutationStatus } from "./fusion-card.ts";
|
|
16
|
-
import { makeWriteOverride } from "./
|
|
17
|
+
import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
17
18
|
import { generateMutationDetails } from "./mutation-result.ts";
|
|
18
19
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
19
|
-
import { DEFAULT_CONFIG } from "./
|
|
20
|
+
import { DEFAULT_CONFIG } from "./tool-factories.testing.ts";
|
|
20
21
|
import { toDisplayLines } from "./grep-render.ts";
|
|
21
22
|
import { parseFileHeader } from "./grep-output.ts";
|
|
22
|
-
import { makeReadOverride } from "./
|
|
23
|
+
import { makeReadOverride } from "./tool-factories.testing.ts";
|
|
23
24
|
import { callTool } from "./tool-call.testing.ts";
|
|
24
25
|
import { renderToolError } from "./render.ts";
|
|
25
26
|
import { formatArgumentError } from "./argument-error.ts";
|
|
26
|
-
import { makeGrepOverride } from "./
|
|
27
|
+
import { makeGrepOverride } from "./tool-factories.testing.ts";
|
|
27
28
|
import { openTestSession } from "../testing/session.testing.ts";
|
|
28
29
|
import type { ActionFusionProgress, Report, SuccessReport } from "./report-schema.ts";
|
|
29
30
|
import { renderReport } from "./report.ts";
|
|
30
|
-
import {
|
|
31
|
+
import { MAX_CONTENT_KIB, outputBudgetsFor } from "./budgets.ts";
|
|
32
|
+
import { DEFAULT_OUTPUT_BUDGETS } from "./tool-factories.testing.ts";
|
|
31
33
|
|
|
32
34
|
const mutationDetails = (
|
|
33
35
|
report: Pick<SuccessReport, "publication" | "freshness" | "then_run">,
|
|
@@ -75,7 +77,7 @@ test("mutation headers refresh in place and retain isolated per-call counts", ()
|
|
|
75
77
|
]) {
|
|
76
78
|
const args = {
|
|
77
79
|
path: "a.txt",
|
|
78
|
-
|
|
80
|
+
steps: [{ action: "append" as const, lines: ["new"] }],
|
|
79
81
|
replacements: [{ literal: true, find: "old", replace: "new" }],
|
|
80
82
|
};
|
|
81
83
|
const context: any = {
|
|
@@ -109,28 +111,28 @@ test("mutation headers refresh in place and retain isolated per-call counts", ()
|
|
|
109
111
|
test("partial edit actions → refresh per-action counts", () => {
|
|
110
112
|
initTheme("dark");
|
|
111
113
|
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
112
|
-
const
|
|
113
|
-
{ action: "replace", anchor: "1#AB",
|
|
114
|
-
{ action: "append",
|
|
115
|
-
{ action: "replace", anchor: "3#CD",
|
|
114
|
+
const steps = [
|
|
115
|
+
{ action: "replace", anchor: "1#AB", lines: ["new"] },
|
|
116
|
+
{ action: "append", lines: ["more"] },
|
|
117
|
+
{ action: "replace", anchor: "3#CD", lines: ["changed"] },
|
|
116
118
|
{ action: "delete", anchor: "5#EF" },
|
|
117
|
-
{ action: "insert_before", anchor: "7#GH",
|
|
118
|
-
{ action: "insert_after", anchor: "9#JK",
|
|
119
|
-
{ action: "prepend",
|
|
119
|
+
{ action: "insert_before", anchor: "7#GH", lines: ["before"] },
|
|
120
|
+
{ action: "insert_after", anchor: "9#JK", lines: ["after"] },
|
|
121
|
+
{ action: "prepend", lines: ["first"] },
|
|
120
122
|
];
|
|
121
123
|
let header: ReturnType<typeof tool.renderCall> | undefined;
|
|
122
124
|
const context = { state: {} } as Parameters<typeof tool.renderCall>[2];
|
|
123
125
|
const render = (value: unknown) => {
|
|
124
126
|
header = tool.renderCall(
|
|
125
127
|
// Streaming arguments reach the renderer before schema validation.
|
|
126
|
-
{ path: "f.txt",
|
|
128
|
+
{ path: "f.txt", steps: value } as Parameters<typeof tool.renderCall>[0],
|
|
127
129
|
theme,
|
|
128
130
|
{ ...context, lastComponent: header },
|
|
129
131
|
);
|
|
130
132
|
return header.render(240).join("\n");
|
|
131
133
|
};
|
|
132
|
-
assert.match(render([
|
|
133
|
-
const mixed = render(
|
|
134
|
+
assert.match(render([steps[0], {}]), /unknown ×1/);
|
|
135
|
+
const mixed = render(steps);
|
|
134
136
|
assert.match(mixed, /7 ops:/);
|
|
135
137
|
for (const [kind, count] of [
|
|
136
138
|
["replace", 2],
|
|
@@ -143,14 +145,14 @@ test("partial edit actions → refresh per-action counts", () => {
|
|
|
143
145
|
assert.ok(mixed.includes(`${kind} ×${count}`), mixed);
|
|
144
146
|
}
|
|
145
147
|
assert.doesNotMatch(mixed, /unknown/);
|
|
146
|
-
const single = render([
|
|
148
|
+
const single = render([steps[0]]);
|
|
147
149
|
assert.match(single, /1 op: replace ×1/);
|
|
148
150
|
assert.doesNotMatch(single, /append|delete|insert_before|insert_after|prepend/);
|
|
149
151
|
assert.match(
|
|
150
152
|
render([null, { action: "rep" }, { action: "__proto__" }, { action: { toString: null } }]),
|
|
151
153
|
/4 ops: unknown ×4/,
|
|
152
154
|
);
|
|
153
|
-
for (const invalid of [[], undefined, JSON.stringify(
|
|
155
|
+
for (const invalid of [[], undefined, JSON.stringify(steps), "[{bad"]) {
|
|
154
156
|
assert.doesNotMatch(render(invalid), /ops?:/);
|
|
155
157
|
}
|
|
156
158
|
});
|
|
@@ -168,7 +170,7 @@ test("missing or empty mutation path → show a themed placeholder until the pat
|
|
|
168
170
|
let header: ReturnType<typeof tool.renderCall> | undefined;
|
|
169
171
|
for (const path of [undefined, "", "f.txt"]) {
|
|
170
172
|
// Streaming arguments may contain operations before the path arrives.
|
|
171
|
-
const args = { path,
|
|
173
|
+
const args = { path, steps: [], replacements: [] } as Parameters<
|
|
172
174
|
ReturnType<typeof makeEditOverride>["renderCall"]
|
|
173
175
|
>[0] &
|
|
174
176
|
Parameters<ReturnType<typeof makeReplaceTool>["renderCall"]>[0];
|
|
@@ -185,7 +187,7 @@ test("missing or empty mutation path → show a themed placeholder until the pat
|
|
|
185
187
|
test("mutation card owns stale-anchor notices without internal status", () => {
|
|
186
188
|
initTheme("dark");
|
|
187
189
|
const tool = withMutationStatus(makeEditOverride(process.cwd(), DEFAULT_CONFIG));
|
|
188
|
-
const args = { path: "a.txt",
|
|
190
|
+
const args = { path: "a.txt", steps: [{ action: "append" as const, lines: ["new"] }] };
|
|
189
191
|
const context: any = { args, state: {}, isPartial: false, isError: false, invalidate() {} };
|
|
190
192
|
const card = tool.renderCall!(args, theme, context);
|
|
191
193
|
tool.renderResult!(
|
|
@@ -215,7 +217,7 @@ test("mutation card owns stale-anchor notices without internal status", () => {
|
|
|
215
217
|
test("mutation card omits status when then_run was not requested", () => {
|
|
216
218
|
initTheme("dark");
|
|
217
219
|
const tool = withMutationStatus(makeEditOverride(process.cwd(), DEFAULT_CONFIG));
|
|
218
|
-
const args = { path: "a.txt",
|
|
220
|
+
const args = { path: "a.txt", steps: [{ action: "append" as const, lines: ["new"] }] };
|
|
219
221
|
const context: any = { args, state: {}, isPartial: false, isError: false, invalidate() {} };
|
|
220
222
|
const card = tool.renderCall!(args, theme, context);
|
|
221
223
|
tool.renderResult!(
|
|
@@ -311,7 +313,7 @@ test("diff previews retain standalone CR and literal control-picture characters"
|
|
|
311
313
|
{ expanded: true, isPartial: false },
|
|
312
314
|
theme,
|
|
313
315
|
{
|
|
314
|
-
args: { path: "a.txt",
|
|
316
|
+
args: { path: "a.txt", steps: [{ action: "append", lines: ["new"] }] },
|
|
315
317
|
state: {},
|
|
316
318
|
isError: false,
|
|
317
319
|
} as Parameters<typeof tool.renderResult>[3],
|
|
@@ -356,6 +358,37 @@ test("Unicode line separators in grep paths → parses the full file header", as
|
|
|
356
358
|
}
|
|
357
359
|
});
|
|
358
360
|
|
|
361
|
+
test("grep files mode with header-like and anchor-like names → preserves complete paths", () => {
|
|
362
|
+
initTheme("dark");
|
|
363
|
+
const tool = makeGrepOverride(process.cwd(), DEFAULT_CONFIG);
|
|
364
|
+
const paths = ["a · 1 match", "1#ABCD│name"];
|
|
365
|
+
const rendered = tool.renderResult(
|
|
366
|
+
{ content: [{ type: "text", text: paths.join("\n") }], details: undefined },
|
|
367
|
+
{ isPartial: false, expanded: true },
|
|
368
|
+
theme,
|
|
369
|
+
{
|
|
370
|
+
args: { pattern: ["needle"], literal: true, outputMode: "files" },
|
|
371
|
+
toolCallId: "test",
|
|
372
|
+
invalidate() {},
|
|
373
|
+
lastComponent: undefined,
|
|
374
|
+
state: {},
|
|
375
|
+
cwd: process.cwd(),
|
|
376
|
+
executionStarted: true,
|
|
377
|
+
durationMs: undefined,
|
|
378
|
+
outputPad: 1,
|
|
379
|
+
keyText: () => "",
|
|
380
|
+
keyHint: (_key: string, description: string) => description,
|
|
381
|
+
argsComplete: true,
|
|
382
|
+
isPartial: false,
|
|
383
|
+
expanded: true,
|
|
384
|
+
showImages: false,
|
|
385
|
+
isError: false,
|
|
386
|
+
},
|
|
387
|
+
);
|
|
388
|
+
const lines = rendered.render(120).map((line) => stripVTControlCharacters(line).trimEnd());
|
|
389
|
+
for (const path of paths) assert.ok(lines.includes(path), JSON.stringify(lines));
|
|
390
|
+
});
|
|
391
|
+
|
|
359
392
|
test("grep renders anchored and plain groups with shared width and folded indentation", () => {
|
|
360
393
|
initTheme("dark");
|
|
361
394
|
const raw = [
|
|
@@ -404,7 +437,7 @@ test("all tools render argument issues as field diagnostics while preserving mod
|
|
|
404
437
|
path: tool.name === "grep" ? ["unused.txt"] : "unused.txt",
|
|
405
438
|
content: "unused",
|
|
406
439
|
mode: "create" as const,
|
|
407
|
-
|
|
440
|
+
steps: [{ action: "append" as const, lines: ["unused"] }],
|
|
408
441
|
replacements: [{ literal: true, find: "unused", replace: "unused" }],
|
|
409
442
|
pattern: ["unused"],
|
|
410
443
|
literal: true,
|
|
@@ -428,6 +461,10 @@ test("all tools render argument issues as field diagnostics while preserving mod
|
|
|
428
461
|
lastComponent: undefined,
|
|
429
462
|
cwd,
|
|
430
463
|
executionStarted: true,
|
|
464
|
+
durationMs: undefined,
|
|
465
|
+
outputPad: 1,
|
|
466
|
+
keyText: () => "",
|
|
467
|
+
keyHint: (_key: string, description: string) => description,
|
|
431
468
|
argsComplete: true,
|
|
432
469
|
isPartial: false,
|
|
433
470
|
expanded,
|
|
@@ -486,7 +523,7 @@ test("expanded errors retain every text block while collapsed errors stay bounde
|
|
|
486
523
|
test("argument cards preserve omission notices and all expanded issues", () => {
|
|
487
524
|
initTheme("dark");
|
|
488
525
|
const issues = Array.from({ length: 200 }, (_, index) => ({
|
|
489
|
-
field: `
|
|
526
|
+
field: `steps[${index}].lines`,
|
|
490
527
|
reason: `中文🙂 ${index}: supply a line.\n${"context ".repeat(50)}\nFinal cause: empty body.`,
|
|
491
528
|
}));
|
|
492
529
|
const diagnostic = formatArgumentError(issues, true, DEFAULT_OUTPUT_BUDGETS);
|
|
@@ -532,7 +569,7 @@ test("unrecognized or malformed JSON errors retain their original diagnostic", (
|
|
|
532
569
|
});
|
|
533
570
|
|
|
534
571
|
test("argument diagnostics wrap Unicode and multiline reasons at narrow terminal widths", () => {
|
|
535
|
-
const field = "
|
|
572
|
+
const field = "steps[0].lines";
|
|
536
573
|
const reason = '中文🙂: supply at least one line.\nUse ["空行"] for this example.';
|
|
537
574
|
const diagnostic = formatArgumentError([{ field, reason }], false, DEFAULT_OUTPUT_BUDGETS);
|
|
538
575
|
const result = { content: [{ type: "text" as const, text: diagnostic }] };
|
|
@@ -558,12 +595,13 @@ test("command changes or removes the target → completed mutation stays success
|
|
|
558
595
|
for (const disturbance of ["changed", "missing"] as const) {
|
|
559
596
|
const path = `${disturbance}.txt`;
|
|
560
597
|
const target = join(cwd, path);
|
|
561
|
-
const
|
|
598
|
+
const executeTool: SessionCommand = async () => {
|
|
562
599
|
assert.equal(await readFile(target, "utf8"), "saved\n");
|
|
563
600
|
if (disturbance === "changed") await writeFile(target, "command changed the file\n");
|
|
564
601
|
else await unlink(target);
|
|
565
|
-
return {
|
|
566
|
-
}
|
|
602
|
+
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
603
|
+
};
|
|
604
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
567
605
|
const tool = withMutationStatus(makeWriteOverride(cwd, fusion));
|
|
568
606
|
const args = {
|
|
569
607
|
path,
|
|
@@ -579,6 +617,10 @@ test("command changes or removes the target → completed mutation stays success
|
|
|
579
617
|
lastComponent: undefined,
|
|
580
618
|
invalidate() {},
|
|
581
619
|
executionStarted: true,
|
|
620
|
+
durationMs: undefined,
|
|
621
|
+
outputPad: 1,
|
|
622
|
+
keyText: () => "",
|
|
623
|
+
keyHint: (_key: string, description: string) => description,
|
|
582
624
|
argsComplete: true,
|
|
583
625
|
isPartial: true,
|
|
584
626
|
expanded: false,
|
|
@@ -593,7 +635,7 @@ test("command changes or removes the target → completed mutation stays success
|
|
|
593
635
|
const completedFrames: string[] = [];
|
|
594
636
|
const result = await callTool(tool, args, {
|
|
595
637
|
toolCallId: disturbance,
|
|
596
|
-
ctx: session.extensionRunner.createToolContext(disturbance, undefined),
|
|
638
|
+
ctx: { ...session.extensionRunner.createToolContext(disturbance, undefined), executeTool },
|
|
597
639
|
onUpdate: (update: AgentToolResult<{ actionFusion: ActionFusionProgress }>) => {
|
|
598
640
|
tool.renderCall!(args, theme, context);
|
|
599
641
|
renderResult(update, { isPartial: true, expanded: false }, theme, context);
|
|
@@ -639,6 +681,10 @@ test("success renderers receive replayed or hooked reports → display only sche
|
|
|
639
681
|
lastComponent: undefined,
|
|
640
682
|
invalidate() {},
|
|
641
683
|
executionStarted: true,
|
|
684
|
+
durationMs: undefined,
|
|
685
|
+
outputPad: 1,
|
|
686
|
+
keyText: () => "",
|
|
687
|
+
keyHint: (_key: string, description: string) => description,
|
|
642
688
|
argsComplete: true,
|
|
643
689
|
isPartial: false,
|
|
644
690
|
expanded: true,
|
|
@@ -723,3 +769,19 @@ test("report text has many short lines within the byte budget → renders every
|
|
|
723
769
|
});
|
|
724
770
|
}
|
|
725
771
|
});
|
|
772
|
+
|
|
773
|
+
test("TUI-only mutation facts render on the card while model serialization omits them", () => {
|
|
774
|
+
initTheme("dark");
|
|
775
|
+
const report: Report = {
|
|
776
|
+
tool: "edit",
|
|
777
|
+
path: "a.txt",
|
|
778
|
+
publication: "PUBLISHED",
|
|
779
|
+
progressError: { message: "display unavailable" },
|
|
780
|
+
};
|
|
781
|
+
const output = stripVTControlCharacters(
|
|
782
|
+
reportLines(report, theme, MUTATION_CARD_SHOWN).join("\n"),
|
|
783
|
+
);
|
|
784
|
+
assert.match(output, /progressError/);
|
|
785
|
+
assert.match(output, /display unavailable/);
|
|
786
|
+
assert.doesNotMatch(renderReport(report), /progressError|display unavailable/);
|
|
787
|
+
});
|
package/src/pi/render.ts
CHANGED
|
@@ -214,11 +214,14 @@ export function reportLines(
|
|
|
214
214
|
* read reports only here or through renderToolError, so the facts a card shows and the
|
|
215
215
|
* block it strips come from one check; content that is not our report stays.
|
|
216
216
|
*/
|
|
217
|
-
export function splitReport(
|
|
217
|
+
export function splitReport(
|
|
218
|
+
result: Pick<AgentToolResult<unknown>, "content" | "details">,
|
|
219
|
+
tool: ReportTool,
|
|
220
|
+
): {
|
|
218
221
|
content: AgentToolResult<unknown>["content"];
|
|
219
222
|
report: Report | undefined;
|
|
220
223
|
} {
|
|
221
|
-
const report = detailsReport(result.details);
|
|
224
|
+
const report = detailsReport(result.details, tool);
|
|
222
225
|
const last = result.content.at(-1);
|
|
223
226
|
const ours = report !== undefined && last?.type === "text" && last.text === renderReport(report);
|
|
224
227
|
return { content: ours ? result.content.slice(0, -1) : result.content, report };
|
|
@@ -244,7 +247,7 @@ export function renderToolError(
|
|
|
244
247
|
}
|
|
245
248
|
|
|
246
249
|
/** Facts a mutation card presents another way: the command card owns then_run. */
|
|
247
|
-
export const MUTATION_CARD_SHOWN = new Set(["then_run"
|
|
250
|
+
export const MUTATION_CARD_SHOWN = new Set(["then_run"]);
|
|
248
251
|
|
|
249
252
|
/** The cards of a tool that shows a diff (edit, replace); each supplies only its header and pending text. */
|
|
250
253
|
export function diffCardRenderers<TArgs>(
|
|
@@ -279,7 +282,7 @@ export function diffCardRenderers<TArgs>(
|
|
|
279
282
|
publishDiffCounts(diff, context, (counts) => {
|
|
280
283
|
context.state.callText?.setText(header(context.args, theme, counts));
|
|
281
284
|
});
|
|
282
|
-
const { report } = splitReport(result);
|
|
285
|
+
const { report } = splitReport(result, tool);
|
|
283
286
|
const facts = report ? reportLines(report, theme, MUTATION_CARD_SHOWN) : [];
|
|
284
287
|
const emptyDiff =
|
|
285
288
|
report?.publication === "NOT_PUBLISHED"
|
package/src/pi/replace-regex.ts
CHANGED
|
@@ -13,7 +13,7 @@ export interface ReplacementRequest {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/** Shared worker errors are checked by the host. */
|
|
16
|
-
|
|
16
|
+
const decodeWorkerReply = workerReplyDecoder(REPLACEMENT_RESULT_SCHEMA);
|
|
17
17
|
|
|
18
18
|
export function runRegexReplacements(
|
|
19
19
|
source: string,
|
package/src/pi/replace-tool.ts
CHANGED
|
@@ -41,18 +41,15 @@ import {
|
|
|
41
41
|
import {
|
|
42
42
|
mutationToolBase,
|
|
43
43
|
runTextMutation,
|
|
44
|
-
type ActionFusionExecutor,
|
|
45
44
|
type MutationTarget,
|
|
45
|
+
type MutationToolEnvironment,
|
|
46
46
|
} from "./mutation-runner.ts";
|
|
47
|
-
import { defaultOutputBudgets, type OutputBudgetProvider } from "./budgets.ts";
|
|
48
47
|
import { argumentItems, type ReportArgumentIssue } from "./argument-validation.ts";
|
|
48
|
+
import { filePathField } from "./schema.ts";
|
|
49
49
|
|
|
50
50
|
const replaceSchema = Type.Object(
|
|
51
51
|
{
|
|
52
|
-
path:
|
|
53
|
-
minLength: 1,
|
|
54
|
-
description: "Path to the file (relative or absolute)",
|
|
55
|
-
}),
|
|
52
|
+
path: filePathField(),
|
|
56
53
|
replacements: REPLACEMENTS_SCHEMA,
|
|
57
54
|
},
|
|
58
55
|
{ additionalProperties: false },
|
|
@@ -89,24 +86,17 @@ function checkReplaceArguments(args: unknown, report: ReportArgumentIssue): void
|
|
|
89
86
|
});
|
|
90
87
|
}
|
|
91
88
|
|
|
92
|
-
export function makeReplaceTool(
|
|
93
|
-
cwd: string,
|
|
94
|
-
config: HashlineEditConfig,
|
|
95
|
-
fusion?: ActionFusionExecutor,
|
|
96
|
-
budgets: OutputBudgetProvider = defaultOutputBudgets,
|
|
97
|
-
) {
|
|
89
|
+
export function makeReplaceTool(env: MutationToolEnvironment) {
|
|
98
90
|
return {
|
|
99
91
|
...mutationToolBase({
|
|
92
|
+
...env,
|
|
100
93
|
tool: "replace",
|
|
101
|
-
cwd,
|
|
102
|
-
fusion,
|
|
103
|
-
budgets,
|
|
104
94
|
schema: replaceSchema,
|
|
105
95
|
description:
|
|
106
96
|
"Replace every match of one or more literal or JavaScript-regex rules in a file. Rules swap only their matched fragments in one call, without line anchors. Use edit instead for inserting, deleting, or rewriting whole lines. Overlapping matches or a rule with no match reject the whole call.",
|
|
107
97
|
promptSnippet: "Replace matching text across a file",
|
|
108
98
|
check: checkReplaceArguments,
|
|
109
|
-
run: (params, target) => runReplace(target, params.replacements, config),
|
|
99
|
+
run: (params, target) => runReplace(target, params.replacements, env.config),
|
|
110
100
|
}),
|
|
111
101
|
...diffCardRenderers("replace", "Replacing…", replaceHeader),
|
|
112
102
|
};
|
package/src/pi/replace.test.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { commandCompletion, type SessionCommand } from "./action-fusion.testing.ts";
|
|
1
2
|
import { rejectsArgument, rejectsArguments } from "./argument-error.testing.ts";
|
|
2
3
|
/** Tool-level replace contracts: byte preservation, matching, and failures. */
|
|
3
4
|
import { test } from "node:test";
|
|
@@ -6,11 +7,11 @@ import { mkdtemp, rm, readFile, writeFile } from "node:fs/promises";
|
|
|
6
7
|
import { tmpdir } from "node:os";
|
|
7
8
|
import { join } from "node:path";
|
|
8
9
|
import { initTheme, type Theme } from "@earendil-works/pi-coding-agent";
|
|
9
|
-
import { makeReplaceTool } from "./
|
|
10
|
+
import { makeReplaceTool } from "./tool-factories.testing.ts";
|
|
10
11
|
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
11
12
|
import { callTool } from "./tool-call.testing.ts";
|
|
12
13
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
13
|
-
import { DEFAULT_CONFIG } from "./
|
|
14
|
+
import { DEFAULT_CONFIG } from "./tool-factories.testing.ts";
|
|
14
15
|
import { reportOf } from "./report.ts";
|
|
15
16
|
|
|
16
17
|
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
@@ -35,10 +36,11 @@ test("replace rejects invalid rule shapes → precise fields and no publication
|
|
|
35
36
|
const path = join(dir, "f.txt");
|
|
36
37
|
await writeFile(path, "old\n");
|
|
37
38
|
let commands = 0;
|
|
38
|
-
const
|
|
39
|
+
const executeTool: SessionCommand = async () => {
|
|
39
40
|
commands++;
|
|
40
|
-
return {
|
|
41
|
-
}
|
|
41
|
+
return commandCompletion({ content: [] });
|
|
42
|
+
};
|
|
43
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
42
44
|
const rule = { literal: true, find: "old", replace: "new" };
|
|
43
45
|
for (const executor of [undefined, fusion]) {
|
|
44
46
|
const tool = makeReplaceTool(dir, DEFAULT_CONFIG, executor);
|
|
@@ -59,11 +61,15 @@ test("replace rejects invalid rule shapes → precise fields and no publication
|
|
|
59
61
|
[{ replacements: [{ ...rule, find: "" }] }, "replacements[0].find", /./],
|
|
60
62
|
] as const) {
|
|
61
63
|
await assert.rejects(
|
|
62
|
-
callTool(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
callTool(
|
|
65
|
+
tool,
|
|
66
|
+
{
|
|
67
|
+
path,
|
|
68
|
+
...args,
|
|
69
|
+
...(executor ? { then_run: { command: "check" } } : {}),
|
|
70
|
+
},
|
|
71
|
+
{ ctx: { cwd: dir, executeTool } },
|
|
72
|
+
),
|
|
67
73
|
rejectsArgument(field, reason),
|
|
68
74
|
);
|
|
69
75
|
assert.equal(await readFile(path, "utf8"), "old\n");
|
|
@@ -77,13 +83,14 @@ test("replace rules omit literal or retain regex → reject before publication a
|
|
|
77
83
|
const path = join(dir, "f.txt");
|
|
78
84
|
await writeFile(path, "old\n");
|
|
79
85
|
let commands = 0;
|
|
86
|
+
const executeTool: SessionCommand = async () => {
|
|
87
|
+
commands++;
|
|
88
|
+
return commandCompletion({ content: [] });
|
|
89
|
+
};
|
|
80
90
|
const tool = makeReplaceTool(
|
|
81
91
|
dir,
|
|
82
92
|
DEFAULT_CONFIG,
|
|
83
|
-
createActionFusionExecutor(
|
|
84
|
-
commands++;
|
|
85
|
-
return { status: "succeeded", result: { content: [] } };
|
|
86
|
-
}),
|
|
93
|
+
createActionFusionExecutor(() => {}),
|
|
87
94
|
);
|
|
88
95
|
for (const [rule, field, reason] of [
|
|
89
96
|
[{ find: "old", replace: "new" }, "literal", /required/],
|
|
@@ -91,11 +98,15 @@ test("replace rules omit literal or retain regex → reject before publication a
|
|
|
91
98
|
[{ find: "old", replace: "new", literal: true, regex: false }, "regex", /not allowed/],
|
|
92
99
|
] as const) {
|
|
93
100
|
await assert.rejects(
|
|
94
|
-
callTool(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
101
|
+
callTool(
|
|
102
|
+
tool,
|
|
103
|
+
{
|
|
104
|
+
path,
|
|
105
|
+
replacements: [{ find: "old", literal: true, replace: "first" }, rule],
|
|
106
|
+
then_run: { command: "check" },
|
|
107
|
+
},
|
|
108
|
+
{ ctx: { cwd: dir, executeTool } },
|
|
109
|
+
),
|
|
99
110
|
rejectsArgument(`replacements[1].${field}`, reason),
|
|
100
111
|
);
|
|
101
112
|
assert.equal(await readFile(path, "utf8"), "old\n");
|
|
@@ -419,13 +430,14 @@ test("invalid batches reject every change and never run a fused command", async
|
|
|
419
430
|
const before = "foo bar foo";
|
|
420
431
|
await writeFile(file, before);
|
|
421
432
|
let commands = 0;
|
|
433
|
+
const executeTool: SessionCommand = async () => {
|
|
434
|
+
commands++;
|
|
435
|
+
return commandCompletion({ content: [{ type: "text", text: "done" }] });
|
|
436
|
+
};
|
|
422
437
|
const tool = makeReplaceTool(
|
|
423
438
|
dir,
|
|
424
439
|
DEFAULT_CONFIG,
|
|
425
|
-
createActionFusionExecutor(
|
|
426
|
-
commands++;
|
|
427
|
-
return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
|
|
428
|
-
}),
|
|
440
|
+
createActionFusionExecutor(() => {}),
|
|
429
441
|
);
|
|
430
442
|
const first = { literal: true, find: "bar", replace: "changed" };
|
|
431
443
|
const cases = [
|
|
@@ -461,7 +473,7 @@ test("invalid batches reject every change and never run a fused command", async
|
|
|
461
473
|
const failed = await callTool(
|
|
462
474
|
tool,
|
|
463
475
|
{ path: file, replacements: [first, rule], then_run: { command: "check" } },
|
|
464
|
-
{ ctx: { cwd: dir } },
|
|
476
|
+
{ ctx: { ...{ cwd: dir }, executeTool } },
|
|
465
477
|
);
|
|
466
478
|
assert.equal(failed.isError, true);
|
|
467
479
|
assert.equal(reportOf(failed, "replace")?.error, code);
|
|
@@ -475,9 +487,7 @@ test("invalid batches reject every change and never run a fused command", async
|
|
|
475
487
|
callTool(
|
|
476
488
|
tool,
|
|
477
489
|
{ path: file, ...args, then_run: { command: "check" } },
|
|
478
|
-
{
|
|
479
|
-
ctx: { cwd: dir },
|
|
480
|
-
},
|
|
490
|
+
{ ctx: { ...{ cwd: dir }, executeTool } },
|
|
481
491
|
),
|
|
482
492
|
expected,
|
|
483
493
|
);
|
|
@@ -576,15 +586,16 @@ test("successful batches run one command against the complete result", async ()
|
|
|
576
586
|
const file = join(dir, "batch.txt");
|
|
577
587
|
await writeFile(file, "foo bar");
|
|
578
588
|
let commands = 0;
|
|
589
|
+
const executeTool: SessionCommand = async (_name, input) => {
|
|
590
|
+
assert.deepEqual(input, { command: "check" });
|
|
591
|
+
commands++;
|
|
592
|
+
assert.equal(await readFile(file, "utf8"), "bar baz");
|
|
593
|
+
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
594
|
+
};
|
|
579
595
|
const tool = makeReplaceTool(
|
|
580
596
|
dir,
|
|
581
597
|
DEFAULT_CONFIG,
|
|
582
|
-
createActionFusionExecutor(
|
|
583
|
-
assert.equal(input.command, "check");
|
|
584
|
-
commands++;
|
|
585
|
-
assert.equal(await readFile(file, "utf8"), "bar baz");
|
|
586
|
-
return { status: "succeeded", result: { content: [{ type: "text", text: "checked" }] } };
|
|
587
|
-
}),
|
|
598
|
+
createActionFusionExecutor(() => {}),
|
|
588
599
|
);
|
|
589
600
|
const result = await tool.execute(
|
|
590
601
|
"0",
|
|
@@ -598,7 +609,7 @@ test("successful batches run one command against the complete result", async ()
|
|
|
598
609
|
},
|
|
599
610
|
undefined,
|
|
600
611
|
undefined,
|
|
601
|
-
{ cwd: dir } as Parameters<typeof tool.execute>[4],
|
|
612
|
+
{ ...({ cwd: dir } as Parameters<typeof tool.execute>[4]), executeTool },
|
|
602
613
|
);
|
|
603
614
|
assert.equal(commands, 1);
|
|
604
615
|
assert.equal(reportOf(result, "replace")?.then_run?.status, "succeeded");
|
|
@@ -11,7 +11,15 @@ export async function expectToolFailure(
|
|
|
11
11
|
): Promise<Report> {
|
|
12
12
|
const result = await pending;
|
|
13
13
|
assert.equal(result.isError, true);
|
|
14
|
-
const
|
|
14
|
+
const details = result.details;
|
|
15
|
+
assert.ok(typeof details === "object" && details !== null && "report" in details);
|
|
16
|
+
const value = details.report;
|
|
17
|
+
assert.ok(typeof value === "object" && value !== null && "tool" in value);
|
|
18
|
+
const tool = (["read", "grep", "edit", "replace", "write"] as const).find(
|
|
19
|
+
(name) => name === value.tool,
|
|
20
|
+
);
|
|
21
|
+
assert.ok(tool);
|
|
22
|
+
const report = detailsReport(details, tool);
|
|
15
23
|
assert.ok(report);
|
|
16
24
|
assert.equal(report.error, code);
|
|
17
25
|
inspect?.(report);
|