@criogaid/pi-hashline-edit 0.10.0 → 0.11.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 +4 -3
- package/README.zh-CN.md +4 -3
- package/package.json +1 -1
- package/src/core/apply.test.ts +2 -2
- package/src/core/apply.ts +2 -2
- 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/types.ts +1 -1
- package/src/index.ts +8 -8
- 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 +3 -2
- 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 +4 -3
- 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 +50 -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.testing.ts +3 -3
- 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 +5 -6
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +6 -5
- 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 +218 -139
- 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 +47 -62
- package/src/pi/error-text.ts +7 -2
- package/src/pi/execute.test.ts +174 -157
- package/src/pi/failure-context.test.ts +4 -3
- 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/grep-candidates.test.ts +18 -20
- package/src/pi/grep-candidates.ts +8 -13
- package/src/pi/grep-matrix.test.ts +3 -3
- package/src/pi/grep-order.test.ts +12 -11
- package/src/pi/grep-output.ts +36 -15
- package/src/pi/grep-reread.ts +7 -2
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +4 -4
- package/src/pi/grep-search.ts +8 -11
- package/src/pi/grep-tool.ts +23 -39
- package/src/pi/grep.test.ts +102 -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 +9 -5
- 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 +22 -41
- package/src/pi/registration.test.ts +25 -19
- package/src/pi/render.test.ts +51 -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 +205 -83
- package/src/pi/report.test.ts +66 -64
- package/src/pi/report.ts +23 -17
- 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/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
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],
|
|
@@ -404,7 +406,7 @@ test("all tools render argument issues as field diagnostics while preserving mod
|
|
|
404
406
|
path: tool.name === "grep" ? ["unused.txt"] : "unused.txt",
|
|
405
407
|
content: "unused",
|
|
406
408
|
mode: "create" as const,
|
|
407
|
-
|
|
409
|
+
steps: [{ action: "append" as const, lines: ["unused"] }],
|
|
408
410
|
replacements: [{ literal: true, find: "unused", replace: "unused" }],
|
|
409
411
|
pattern: ["unused"],
|
|
410
412
|
literal: true,
|
|
@@ -486,7 +488,7 @@ test("expanded errors retain every text block while collapsed errors stay bounde
|
|
|
486
488
|
test("argument cards preserve omission notices and all expanded issues", () => {
|
|
487
489
|
initTheme("dark");
|
|
488
490
|
const issues = Array.from({ length: 200 }, (_, index) => ({
|
|
489
|
-
field: `
|
|
491
|
+
field: `steps[${index}].lines`,
|
|
490
492
|
reason: `中文🙂 ${index}: supply a line.\n${"context ".repeat(50)}\nFinal cause: empty body.`,
|
|
491
493
|
}));
|
|
492
494
|
const diagnostic = formatArgumentError(issues, true, DEFAULT_OUTPUT_BUDGETS);
|
|
@@ -532,7 +534,7 @@ test("unrecognized or malformed JSON errors retain their original diagnostic", (
|
|
|
532
534
|
});
|
|
533
535
|
|
|
534
536
|
test("argument diagnostics wrap Unicode and multiline reasons at narrow terminal widths", () => {
|
|
535
|
-
const field = "
|
|
537
|
+
const field = "steps[0].lines";
|
|
536
538
|
const reason = '中文🙂: supply at least one line.\nUse ["空行"] for this example.';
|
|
537
539
|
const diagnostic = formatArgumentError([{ field, reason }], false, DEFAULT_OUTPUT_BUDGETS);
|
|
538
540
|
const result = { content: [{ type: "text" as const, text: diagnostic }] };
|
|
@@ -558,12 +560,13 @@ test("command changes or removes the target → completed mutation stays success
|
|
|
558
560
|
for (const disturbance of ["changed", "missing"] as const) {
|
|
559
561
|
const path = `${disturbance}.txt`;
|
|
560
562
|
const target = join(cwd, path);
|
|
561
|
-
const
|
|
563
|
+
const executeTool: SessionCommand = async () => {
|
|
562
564
|
assert.equal(await readFile(target, "utf8"), "saved\n");
|
|
563
565
|
if (disturbance === "changed") await writeFile(target, "command changed the file\n");
|
|
564
566
|
else await unlink(target);
|
|
565
|
-
return {
|
|
566
|
-
}
|
|
567
|
+
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
568
|
+
};
|
|
569
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
567
570
|
const tool = withMutationStatus(makeWriteOverride(cwd, fusion));
|
|
568
571
|
const args = {
|
|
569
572
|
path,
|
|
@@ -593,7 +596,7 @@ test("command changes or removes the target → completed mutation stays success
|
|
|
593
596
|
const completedFrames: string[] = [];
|
|
594
597
|
const result = await callTool(tool, args, {
|
|
595
598
|
toolCallId: disturbance,
|
|
596
|
-
ctx: session.extensionRunner.createToolContext(disturbance, undefined),
|
|
599
|
+
ctx: { ...session.extensionRunner.createToolContext(disturbance, undefined), executeTool },
|
|
597
600
|
onUpdate: (update: AgentToolResult<{ actionFusion: ActionFusionProgress }>) => {
|
|
598
601
|
tool.renderCall!(args, theme, context);
|
|
599
602
|
renderResult(update, { isPartial: true, expanded: false }, theme, context);
|
|
@@ -723,3 +726,19 @@ test("report text has many short lines within the byte budget → renders every
|
|
|
723
726
|
});
|
|
724
727
|
}
|
|
725
728
|
});
|
|
729
|
+
|
|
730
|
+
test("TUI-only mutation facts render on the card while model serialization omits them", () => {
|
|
731
|
+
initTheme("dark");
|
|
732
|
+
const report: Report = {
|
|
733
|
+
tool: "edit",
|
|
734
|
+
path: "a.txt",
|
|
735
|
+
publication: "PUBLISHED",
|
|
736
|
+
progressError: { message: "display unavailable" },
|
|
737
|
+
};
|
|
738
|
+
const output = stripVTControlCharacters(
|
|
739
|
+
reportLines(report, theme, MUTATION_CARD_SHOWN).join("\n"),
|
|
740
|
+
);
|
|
741
|
+
assert.match(output, /progressError/);
|
|
742
|
+
assert.match(output, /display unavailable/);
|
|
743
|
+
assert.doesNotMatch(renderReport(report), /progressError|display unavailable/);
|
|
744
|
+
});
|
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);
|