@criogaid/pi-hashline-edit 0.3.1 → 0.5.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 +166 -61
- package/package.json +8 -3
- package/src/core/apply.test.ts +459 -313
- package/src/core/apply.ts +251 -215
- package/src/core/errors.ts +9 -0
- package/src/core/hash.test.ts +61 -23
- package/src/core/hash.ts +49 -20
- package/src/core/lines.test.ts +46 -27
- package/src/core/lines.ts +36 -28
- package/src/core/ranges.test.ts +73 -19
- package/src/core/ranges.ts +15 -15
- package/src/core/replace.ts +109 -0
- package/src/core/text.test.ts +3 -3
- package/src/core/text.ts +26 -9
- package/src/core/types.ts +56 -41
- package/src/index.ts +23 -19
- package/src/integration/config-registration.test.ts +206 -0
- package/src/integration/grep-rg.test.ts +879 -251
- package/src/integration/large-text.test.ts +63 -0
- package/src/integration/replace-package.test.ts +41 -0
- package/src/pi/action-fusion.test.ts +612 -207
- package/src/pi/action-fusion.ts +442 -254
- package/src/pi/anchor-format.test.ts +53 -21
- package/src/pi/anchor-format.ts +48 -20
- package/src/pi/budgets.ts +25 -0
- package/src/pi/config.test.ts +178 -35
- package/src/pi/config.ts +96 -37
- package/src/pi/directory-sync.test.ts +55 -0
- package/src/pi/e2e-scenarios.test.ts +360 -0
- package/src/pi/edit-tool.ts +307 -279
- package/src/pi/error-text.ts +26 -0
- package/src/pi/execute.test.ts +1718 -713
- package/src/pi/failure-context.test.ts +110 -83
- package/src/pi/failure-context.ts +232 -73
- package/src/pi/failure-path.test.ts +180 -66
- package/src/pi/file-commit.test.ts +446 -233
- package/src/pi/file-commit.ts +348 -203
- package/src/pi/fusion-card.test.ts +118 -83
- package/src/pi/fusion-card.ts +203 -87
- package/src/pi/grep-output.ts +225 -0
- package/src/pi/grep-render.ts +59 -0
- package/src/pi/grep-scope.ts +139 -0
- package/src/pi/grep-search.ts +231 -0
- package/src/pi/grep-tool.ts +215 -774
- package/src/pi/grep.test.ts +825 -376
- package/src/pi/mutation-outcome.testing.ts +22 -0
- package/src/pi/mutation-result.test.ts +86 -0
- package/src/pi/mutation-result.ts +118 -63
- package/src/pi/mutation-runner.ts +151 -0
- package/src/pi/path.ts +20 -12
- package/src/pi/pi.test.ts +100 -52
- package/src/pi/read-tool.ts +245 -134
- package/src/pi/registration.test.ts +271 -114
- package/src/pi/render.test.ts +227 -84
- package/src/pi/render.ts +86 -116
- package/src/pi/replace-regex.ts +54 -0
- package/src/pi/replace-tool.ts +247 -241
- package/src/pi/replace-worker.mjs +4 -0
- package/src/pi/replace-worker.ts +10 -0
- package/src/pi/replace.test.ts +778 -390
- package/src/pi/result-safety.test.ts +821 -344
- package/src/pi/rg-line-ranges.test.ts +57 -31
- package/src/pi/rg-line-ranges.ts +45 -82
- package/src/pi/rg-process.test.ts +113 -0
- package/src/pi/{rg-line-filter.ts → rg-process.ts} +72 -125
- package/src/pi/rg-text-view.ts +102 -48
- package/src/pi/schema.ts +19 -0
- package/src/pi/text-stream.test.ts +118 -0
- package/src/pi/text-stream.ts +136 -0
- package/src/pi/tool-call.testing.ts +37 -0
- package/src/pi/write-tool.ts +114 -52
- package/src/pi/write.test.ts +250 -97
- package/src/core/index.ts +0 -14
- package/src/pi/rg-line-filter.test.ts +0 -85
- package/src/pi/state.ts +0 -26
package/src/pi/render.test.ts
CHANGED
|
@@ -5,107 +5,250 @@ import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/m
|
|
|
5
5
|
import { displayCarriageReturns } from "./anchor-format.ts";
|
|
6
6
|
import { makeEditOverride } from "./edit-tool.ts";
|
|
7
7
|
import { makeReplaceTool } from "./replace-tool.ts";
|
|
8
|
-
import { renderDiffPreview
|
|
8
|
+
import { renderDiffPreview } from "./render.ts";
|
|
9
|
+
import { withMutationStatus } from "./fusion-card.ts";
|
|
9
10
|
import { makeWriteOverride } from "./write-tool.ts";
|
|
10
11
|
import { generateMutationDetails } from "./mutation-result.ts";
|
|
12
|
+
import type { ActionFusionDetails } from "./action-fusion.ts";
|
|
13
|
+
import { DEFAULT_CONFIG } from "./config.ts";
|
|
14
|
+
|
|
15
|
+
const versions = { publishedRevision: "r", observedRevision: "r" };
|
|
16
|
+
const mutationDetails = (actionFusion?: ActionFusionDetails) => ({
|
|
17
|
+
...generateMutationDetails("a.txt", "same\n", "same\n", versions, "PUBLISHED"),
|
|
18
|
+
actionFusion,
|
|
19
|
+
});
|
|
11
20
|
|
|
12
21
|
test("mutation headers refresh in place and retain isolated per-call counts", () => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
initTheme("dark");
|
|
23
|
+
for (const tool of [
|
|
24
|
+
makeEditOverride(process.cwd(), DEFAULT_CONFIG),
|
|
25
|
+
makeReplaceTool(process.cwd(), DEFAULT_CONFIG),
|
|
26
|
+
]) {
|
|
27
|
+
const args = {
|
|
28
|
+
path: "a.txt",
|
|
29
|
+
edits: [{ op: "append" as const, body: ["new"] }],
|
|
30
|
+
replacements: [{ find: "old", replace: "new" }],
|
|
31
|
+
};
|
|
32
|
+
const context: any = {
|
|
33
|
+
args,
|
|
34
|
+
state: {},
|
|
35
|
+
invalidate() {
|
|
36
|
+
assert.fail("rendering must not invalidate the row");
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
const header = tool.renderCall(args, theme, context);
|
|
40
|
+
const other = tool.renderCall({ ...args, path: "b.txt" }, theme, { ...context, state: {} });
|
|
41
|
+
assert.equal(context.state.callText, header);
|
|
42
|
+
tool.renderResult(
|
|
43
|
+
{
|
|
44
|
+
content: [{ type: "text", text: "Done" }],
|
|
45
|
+
details: generateMutationDetails("a.txt", "old\n", "new\n", versions, "PUBLISHED"),
|
|
46
|
+
},
|
|
47
|
+
{ isPartial: false, expanded: false },
|
|
48
|
+
theme,
|
|
49
|
+
context,
|
|
50
|
+
);
|
|
51
|
+
assert.match(header.render(100).join("\n"), /\+1/);
|
|
52
|
+
assert.match(header.render(100).join("\n"), /-1/);
|
|
53
|
+
assert.doesNotMatch(other.render(100).join("\n"), /\+1|-1/);
|
|
54
|
+
context.lastComponent = header;
|
|
55
|
+
assert.equal(tool.renderCall(args, theme, context), header);
|
|
56
|
+
assert.match(header.render(100).join("\n"), /\+1/);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("edit call titles show structured batches and tolerate invalid partial input", () => {
|
|
61
|
+
initTheme("dark");
|
|
62
|
+
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
63
|
+
const edits = [
|
|
64
|
+
{ op: "replace" as const, anchor: "1#AB", body: ["new"] },
|
|
65
|
+
{ op: "append" as const, body: ["more"] },
|
|
66
|
+
];
|
|
67
|
+
const render = (value: unknown) =>
|
|
68
|
+
tool
|
|
69
|
+
.renderCall({ path: "f.txt", edits: value } as Parameters<typeof tool.renderCall>[0], theme, {
|
|
70
|
+
state: {},
|
|
71
|
+
} as Parameters<typeof tool.renderCall>[2])
|
|
72
|
+
.render(120)
|
|
73
|
+
.join("\n");
|
|
74
|
+
assert.match(render(edits), /2 ops: replace/);
|
|
75
|
+
assert.doesNotMatch(render(JSON.stringify(edits)), /ops:/);
|
|
76
|
+
assert.doesNotMatch(render("[{bad"), /ops:/);
|
|
28
77
|
});
|
|
29
78
|
|
|
30
79
|
test("mutation card owns stale-anchor notices without internal status", () => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
80
|
+
initTheme("dark");
|
|
81
|
+
const tool = withMutationStatus(makeEditOverride(process.cwd(), DEFAULT_CONFIG));
|
|
82
|
+
const args = { path: "a.txt", edits: [{ op: "append" as const, body: ["new"] }] };
|
|
83
|
+
const context: any = { args, state: {}, isPartial: false, isError: false, invalidate() {} };
|
|
84
|
+
const card = tool.renderCall!(args, theme, context);
|
|
85
|
+
tool.renderResult!(
|
|
86
|
+
{
|
|
87
|
+
content: [
|
|
88
|
+
{ type: "text", text: "Edit saved." },
|
|
89
|
+
{ type: "text", text: "command diagnostic" },
|
|
90
|
+
],
|
|
91
|
+
details: mutationDetails({
|
|
92
|
+
publication: "PUBLISHED",
|
|
93
|
+
freshness: "changed",
|
|
94
|
+
command: "failed",
|
|
95
|
+
}),
|
|
96
|
+
},
|
|
97
|
+
{ isPartial: false, expanded: false },
|
|
98
|
+
theme,
|
|
99
|
+
context,
|
|
100
|
+
);
|
|
101
|
+
const output = card.render(160).join("\n");
|
|
102
|
+
assert.match(output, /Edit saved/);
|
|
103
|
+
assert.doesNotMatch(output, /publication=|freshness=/);
|
|
104
|
+
assert.match(output, /Anchors are stale: target changed/);
|
|
105
|
+
assert.doesNotMatch(output, /command diagnostic/);
|
|
106
|
+
assert.ok(output.includes(theme.getBgAnsi("toolSuccessBg")));
|
|
46
107
|
});
|
|
47
108
|
|
|
48
109
|
test("mutation card omits status when then_run was not requested", () => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
110
|
+
initTheme("dark");
|
|
111
|
+
const tool = withMutationStatus(makeEditOverride(process.cwd(), DEFAULT_CONFIG));
|
|
112
|
+
const args = { path: "a.txt", edits: [{ op: "append" as const, body: ["new"] }] };
|
|
113
|
+
const context: any = { args, state: {}, isPartial: false, isError: false, invalidate() {} };
|
|
114
|
+
const card = tool.renderCall!(args, theme, context);
|
|
115
|
+
tool.renderResult!(
|
|
116
|
+
{
|
|
117
|
+
content: [{ type: "text", text: "Edited a.txt." }],
|
|
118
|
+
details: mutationDetails({
|
|
119
|
+
publication: "PUBLISHED",
|
|
120
|
+
freshness: "unchanged",
|
|
121
|
+
command: "not_requested",
|
|
122
|
+
}),
|
|
123
|
+
},
|
|
124
|
+
{ isPartial: false, expanded: false },
|
|
125
|
+
theme,
|
|
126
|
+
context,
|
|
127
|
+
);
|
|
128
|
+
const output = card.render(160).join("\n");
|
|
129
|
+
assert.match(output, /Edited a\.txt/);
|
|
130
|
+
assert.doesNotMatch(output, /publication=|freshness=/);
|
|
131
|
+
tool.renderCall!(args, theme, context);
|
|
132
|
+
tool.renderResult!(
|
|
133
|
+
{
|
|
134
|
+
content: [{ type: "text", text: "Edited a.txt." }],
|
|
135
|
+
details: mutationDetails({
|
|
136
|
+
publication: "PUBLISHED",
|
|
137
|
+
freshness: "changed",
|
|
138
|
+
command: "not_requested",
|
|
139
|
+
}),
|
|
140
|
+
},
|
|
141
|
+
{ isPartial: false, expanded: false },
|
|
142
|
+
theme,
|
|
143
|
+
context,
|
|
144
|
+
);
|
|
145
|
+
const staleOutput = card.render(160).join("\n");
|
|
146
|
+
assert.match(staleOutput, /Anchors are stale: target changed/);
|
|
147
|
+
assert.doesNotMatch(staleOutput, /publication=|freshness=/);
|
|
69
148
|
});
|
|
70
149
|
|
|
71
150
|
test("unfused write errors retain the native full diagnostic", () => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
151
|
+
initTheme("dark");
|
|
152
|
+
const tool = withMutationStatus(makeWriteOverride(process.cwd()));
|
|
153
|
+
const args = { path: "a.txt", content: "new" };
|
|
154
|
+
const context: any = { args, state: {}, isPartial: false, isError: true, invalidate() {} };
|
|
155
|
+
const card = tool.renderCall!(args, theme, context);
|
|
156
|
+
tool.renderResult!(
|
|
157
|
+
{
|
|
158
|
+
content: [{ type: "text", text: "write failed\nimportant detail" }],
|
|
159
|
+
details: {
|
|
160
|
+
path: "a.txt",
|
|
161
|
+
created: false,
|
|
162
|
+
publication: "NOT_PUBLISHED",
|
|
163
|
+
publishedRevision: "r",
|
|
164
|
+
observedRevision: "r",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
{ isPartial: false, expanded: false },
|
|
168
|
+
theme,
|
|
169
|
+
context,
|
|
170
|
+
);
|
|
171
|
+
assert.match(card.render(160).join("\n"), /write failed[\s\S]*important detail/);
|
|
80
172
|
});
|
|
81
173
|
|
|
82
174
|
test("mutation previews hide CRLF boundaries even with mixed endings or an unterminated last line", () => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
175
|
+
initTheme("dark");
|
|
176
|
+
for (const before of ["old\r\ncontext\r\n", "old\r\ncontext\nlast", "old\r\ncontext\r\nlast"]) {
|
|
177
|
+
const after = before.replace("old", "new");
|
|
178
|
+
const details = generateMutationDetails(
|
|
179
|
+
"a.txt",
|
|
180
|
+
before,
|
|
181
|
+
after,
|
|
182
|
+
{ publishedRevision: "r", observedRevision: "r" },
|
|
183
|
+
"PUBLISHED",
|
|
184
|
+
);
|
|
185
|
+
assert.equal(details.diff, displayCarriageReturns(generateDiffString(before, after).diff));
|
|
186
|
+
assert.match(details.diff, /␍/);
|
|
187
|
+
assert.ok(details.patch.includes("-old\r\n"));
|
|
188
|
+
for (const tool of [
|
|
189
|
+
makeEditOverride(process.cwd(), DEFAULT_CONFIG),
|
|
190
|
+
makeReplaceTool(process.cwd(), DEFAULT_CONFIG),
|
|
191
|
+
]) {
|
|
192
|
+
for (const expanded of [false, true]) {
|
|
193
|
+
const context: any = { args: { path: "a.txt" }, state: {}, isError: false };
|
|
194
|
+
const rendered = tool.renderResult(
|
|
195
|
+
{ content: [{ type: "text", text: "Done" }], details },
|
|
196
|
+
{ isPartial: false, expanded },
|
|
197
|
+
theme,
|
|
198
|
+
context,
|
|
199
|
+
);
|
|
200
|
+
const output = rendered.render(160).join("\n");
|
|
201
|
+
assert.doesNotMatch(output, /␍/);
|
|
202
|
+
assert.match(output, /old[\s\S]*new[\s\S]*context/);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
100
206
|
});
|
|
101
207
|
|
|
102
208
|
test("diff previews retain standalone CR and literal control-picture characters", () => {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
209
|
+
initTheme("dark");
|
|
210
|
+
for (const before of ["old\r", "old␍", "old\rcontent\r\n"]) {
|
|
211
|
+
const details = generateMutationDetails(
|
|
212
|
+
"a.txt",
|
|
213
|
+
before,
|
|
214
|
+
before.replace("old", "new"),
|
|
215
|
+
{ publishedRevision: "r", observedRevision: "r" },
|
|
216
|
+
"PUBLISHED",
|
|
217
|
+
);
|
|
218
|
+
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
219
|
+
const result = tool.renderResult(
|
|
220
|
+
{ content: [], details },
|
|
221
|
+
{ expanded: true, isPartial: false },
|
|
222
|
+
theme,
|
|
223
|
+
{
|
|
224
|
+
args: { path: "a.txt", edits: [{ op: "append", body: ["new"] }] },
|
|
225
|
+
state: {},
|
|
226
|
+
isError: false,
|
|
227
|
+
} as Parameters<typeof tool.renderResult>[3],
|
|
228
|
+
);
|
|
229
|
+
assert.match(result.render(160).join("\n"), /␍/);
|
|
230
|
+
}
|
|
231
|
+
assert.match(renderDiffPreview("-1 old␍\n+1 new␍", true, theme), /␍/);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test("withMutationStatus renderResult initializes mutationShell defensively", () => {
|
|
235
|
+
initTheme("dark");
|
|
236
|
+
const tool = withMutationStatus(makeEditOverride(process.cwd(), DEFAULT_CONFIG));
|
|
237
|
+
const context: any = { args: { path: "a.txt" }, state: {}, isError: false };
|
|
238
|
+
const container = tool.renderResult!(
|
|
239
|
+
{
|
|
240
|
+
content: [{ type: "text", text: "Edited a.txt." }],
|
|
241
|
+
details: mutationDetails({
|
|
242
|
+
publication: "PUBLISHED",
|
|
243
|
+
freshness: "unchanged",
|
|
244
|
+
command: "not_requested",
|
|
245
|
+
}),
|
|
246
|
+
},
|
|
247
|
+
{ isPartial: false, expanded: false },
|
|
248
|
+
theme,
|
|
249
|
+
context,
|
|
250
|
+
);
|
|
251
|
+
assert.ok(container);
|
|
252
|
+
assert.ok(context.state.mutationShell);
|
|
253
|
+
assert.ok(context.state.mutationShell.box);
|
|
111
254
|
});
|
package/src/pi/render.ts
CHANGED
|
@@ -1,36 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared helpers for the hashline-aware tool renderers.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* an anchor can be copied straight into an edit op. The user-facing TUI
|
|
6
|
-
* renderers (read, grep) strip that prefix back to a clean ` N: content` form.
|
|
7
|
-
* Parsing the anchor format in one place keeps read and grep in sync.
|
|
4
|
+
* Mutation previews and shared error presentation for the user-facing TUI.
|
|
8
5
|
*
|
|
9
6
|
* @module pi-hashline-edit/pi
|
|
10
7
|
*/
|
|
11
8
|
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
lineNo: string;
|
|
20
|
-
/** Line content with the `LINE#HASH│` prefix removed. */
|
|
21
|
-
content: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Parse a `LINE#HASH│content` anchor line.
|
|
26
|
-
*
|
|
27
|
-
* @returns the row, or `null` for non-anchor lines (headers, notices, free
|
|
28
|
-
* text) so callers can fall through to their own formatting.
|
|
29
|
-
*/
|
|
30
|
-
export function parseHashline(line: string): HashlineRow | null {
|
|
31
|
-
const m = line.match(HASHLINE_RE);
|
|
32
|
-
return m ? { lineNo: m[1], content: m[2] } : null;
|
|
33
|
-
}
|
|
9
|
+
import {
|
|
10
|
+
renderDiff,
|
|
11
|
+
type Theme,
|
|
12
|
+
type ToolRenderResultOptions,
|
|
13
|
+
} from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
15
|
+
import { Text, type Component } from "@earendil-works/pi-tui";
|
|
34
16
|
|
|
35
17
|
/** Max diff lines shown when a result is rendered collapsed. */
|
|
36
18
|
const MAX_COLLAPSED_DIFF_LINES = 24;
|
|
@@ -42,37 +24,43 @@ const MAX_COLLAPSED_DIFF_LINES = 24;
|
|
|
42
24
|
* first 24 rendered lines with an overflow marker — truncating after rendering
|
|
43
25
|
* keeps `-`/`+` pairs intact so the intra-line highlight never dangles.
|
|
44
26
|
*/
|
|
45
|
-
export function renderDiffPreview(diff: string, expanded: boolean, theme:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
27
|
+
export function renderDiffPreview(diff: string, expanded: boolean, theme: Theme): string {
|
|
28
|
+
const rendered = renderDiff(diff);
|
|
29
|
+
if (expanded) return rendered;
|
|
30
|
+
const allLines = rendered.split("\n");
|
|
31
|
+
const more =
|
|
32
|
+
allLines.length > MAX_COLLAPSED_DIFF_LINES
|
|
33
|
+
? `\n${theme.fg("dim", `… (${allLines.length - MAX_COLLAPSED_DIFF_LINES} more)`)}`
|
|
34
|
+
: "";
|
|
35
|
+
return allLines.slice(0, MAX_COLLAPSED_DIFF_LINES).join("\n") + more;
|
|
54
36
|
}
|
|
55
37
|
|
|
56
38
|
/** Added/removed line counts of a pi-format diff string (`+N`/`-N` leading char). */
|
|
57
39
|
export interface DiffCounts {
|
|
58
|
-
|
|
59
|
-
|
|
40
|
+
added: number;
|
|
41
|
+
removed: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Row-local render state shared by the mutation call and result renderers. */
|
|
45
|
+
export interface MutationRenderState {
|
|
46
|
+
diffCounts?: DiffCounts;
|
|
47
|
+
callText?: Text;
|
|
60
48
|
}
|
|
61
49
|
|
|
62
50
|
/** Count added/removed lines in a pi-format diff (`+N content` / `-N content` / ` N content`). */
|
|
63
51
|
export function countDiffLines(diff: string): DiffCounts {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
52
|
+
let added = 0;
|
|
53
|
+
let removed = 0;
|
|
54
|
+
for (const line of diff.split("\n")) {
|
|
55
|
+
if (line.startsWith("+")) added++;
|
|
56
|
+
else if (line.startsWith("-")) removed++;
|
|
57
|
+
}
|
|
58
|
+
return { added, removed };
|
|
71
59
|
}
|
|
72
60
|
|
|
73
61
|
/** Format `+N -N` with the theme's diff colors for the tool call header. */
|
|
74
|
-
export function formatDiffCounts(counts: DiffCounts, theme:
|
|
75
|
-
|
|
62
|
+
export function formatDiffCounts(counts: DiffCounts, theme: Theme): string {
|
|
63
|
+
return ` ${theme.fg("toolDiffAdded", `+${counts.added}`)} ${theme.fg("toolDiffRemoved", `-${counts.removed}`)}`;
|
|
76
64
|
}
|
|
77
65
|
|
|
78
66
|
/**
|
|
@@ -92,86 +80,68 @@ export function formatDiffCounts(counts: DiffCounts, theme: any): string {
|
|
|
92
80
|
* component after the nested one — the diff renders twice.
|
|
93
81
|
*/
|
|
94
82
|
export function publishDiffCounts(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
83
|
+
diff: string | undefined,
|
|
84
|
+
context: { state?: MutationRenderState },
|
|
85
|
+
refreshHeader: (counts: DiffCounts) => void,
|
|
98
86
|
): void {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
87
|
+
if (!diff || !context?.state) return;
|
|
88
|
+
const counts = countDiffLines(diff);
|
|
89
|
+
const prev: DiffCounts | undefined = context.state.diffCounts;
|
|
90
|
+
context.state.diffCounts = counts;
|
|
91
|
+
if (!prev || prev.added !== counts.added || prev.removed !== counts.removed)
|
|
92
|
+
refreshHeader(counts);
|
|
104
93
|
}
|
|
105
94
|
|
|
106
95
|
/** Reuse the call component and retain it for the result's in-place count refresh. */
|
|
107
|
-
export function renderMutationCall(
|
|
108
|
-
|
|
96
|
+
export function renderMutationCall<TArgs>(
|
|
97
|
+
args: TArgs,
|
|
98
|
+
theme: Theme,
|
|
99
|
+
context: { lastComponent?: Component; state?: MutationRenderState },
|
|
100
|
+
header: (args: TArgs, theme: Theme, counts?: DiffCounts) => string,
|
|
109
101
|
): Text {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
102
|
+
const text = (context?.lastComponent as Text | undefined) ?? new Text("", 0, 0);
|
|
103
|
+
if (context?.state) context.state.callText = text;
|
|
104
|
+
text.setText(header(args, theme, context?.state?.diffCounts));
|
|
105
|
+
return text;
|
|
114
106
|
}
|
|
115
107
|
|
|
116
108
|
/** Keep model-facing diagnostic details out of the compact error row. */
|
|
117
|
-
export function renderToolError(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
109
|
+
export function renderToolError(
|
|
110
|
+
result: Pick<AgentToolResult<unknown>, "content">,
|
|
111
|
+
theme: Theme,
|
|
112
|
+
): Text {
|
|
113
|
+
const content = result.content?.[0];
|
|
114
|
+
const text = content?.type === "text" ? content.text.split("\n")[0] : "Error";
|
|
115
|
+
return new Text(theme.fg("error", text), 0, 0);
|
|
121
116
|
}
|
|
122
117
|
|
|
123
118
|
/** Render mutation status or a diff, refreshing the call header's counts in place. */
|
|
124
|
-
export function renderMutationResult(
|
|
125
|
-
|
|
126
|
-
|
|
119
|
+
export function renderMutationResult<TArgs>(
|
|
120
|
+
result: AgentToolResult<{
|
|
121
|
+
displayDiff?: string;
|
|
122
|
+
diff?: string;
|
|
123
|
+
actionFusion?: { publication?: string };
|
|
124
|
+
}>,
|
|
125
|
+
{ isPartial, expanded }: ToolRenderResultOptions,
|
|
126
|
+
theme: Theme,
|
|
127
|
+
context: { isError: boolean; args: TArgs; state?: MutationRenderState },
|
|
128
|
+
pending: string,
|
|
129
|
+
fallback: string,
|
|
130
|
+
header: (args: TArgs, theme: Theme, counts?: DiffCounts) => string,
|
|
127
131
|
): Text {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
/** Keep the mutation card's background independent of the fused command's lifetime. */
|
|
145
|
-
export function withMutationStatus(tool: ToolDefinition<any, any, any>): ToolDefinition<any, any, any> {
|
|
146
|
-
return {
|
|
147
|
-
...tool,
|
|
148
|
-
renderShell: "self",
|
|
149
|
-
renderCall(args, theme, context) {
|
|
150
|
-
const shell = context.state.mutationShell ??= { box: new Box(1, 1) };
|
|
151
|
-
shell.call = tool.renderCall!(args, theme, { ...context, lastComponent: shell.call });
|
|
152
|
-
shell.box.clear();
|
|
153
|
-
shell.box.addChild(shell.call);
|
|
154
|
-
shell.box.setBgFn((line: string) => theme.bg(context.isPartial ? "toolPendingBg" : context.isError ? "toolErrorBg" : "toolSuccessBg", line));
|
|
155
|
-
return shell.box;
|
|
156
|
-
},
|
|
157
|
-
renderResult(result, options, theme, context) {
|
|
158
|
-
const shell = context.state.mutationShell;
|
|
159
|
-
const isPartial = options.isPartial && result.details?.actionFusion?.mutationCompleted !== true;
|
|
160
|
-
// Fused errors retain combined details for the model; show the mutation's summary here.
|
|
161
|
-
const fusedError = context.isError && (context.args as { then_run?: unknown })?.then_run;
|
|
162
|
-
shell.result = fusedError ? renderToolError(result, theme)
|
|
163
|
-
: tool.renderResult!(result, { ...options, isPartial }, theme, { ...context, isPartial, lastComponent: shell.result });
|
|
164
|
-
// Pi runs renderCall first; update its box in place without invalidating the tool row.
|
|
165
|
-
shell.box.addChild(shell.result);
|
|
166
|
-
const file = result.details?.actionFusion ?? shell.fileState;
|
|
167
|
-
if (file) {
|
|
168
|
-
shell.fileState = { freshness: file.freshness };
|
|
169
|
-
if (file.freshness === "changed" || file.freshness === "missing") {
|
|
170
|
-
shell.box.addChild(new Text(theme.fg("warning", `Anchors are stale: target ${file.freshness}.`), 0, 0));
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
shell.box.setBgFn((line: string) => theme.bg(isPartial ? "toolPendingBg" : context.isError ? "toolErrorBg" : "toolSuccessBg", line));
|
|
174
|
-
return new Container();
|
|
175
|
-
},
|
|
176
|
-
};
|
|
132
|
+
if (isPartial && result.details?.actionFusion?.publication !== "PUBLISHED")
|
|
133
|
+
return new Text(theme.fg("warning", pending), 0, 0);
|
|
134
|
+
const content = result.content?.[0];
|
|
135
|
+
if (context.isError) return renderToolError(result, theme);
|
|
136
|
+
const diff: string | undefined = result.details?.displayDiff ?? result.details?.diff;
|
|
137
|
+
// Refresh in place: invalidation inside a renderer re-enters updateDisplay.
|
|
138
|
+
publishDiffCounts(diff, context, (counts) => {
|
|
139
|
+
context.state?.callText?.setText(header(context.args, theme, counts));
|
|
140
|
+
});
|
|
141
|
+
if (!diff) {
|
|
142
|
+
// Only the summary is displayed; subsequent anchor rows are for the model.
|
|
143
|
+
const summary = content?.type === "text" ? content.text.split("\n")[0] : fallback;
|
|
144
|
+
return new Text(theme.fg("success", summary), 0, 0);
|
|
145
|
+
}
|
|
146
|
+
return new Text(renderDiffPreview(diff, expanded, theme), 0, 0);
|
|
177
147
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run a replace batch that contains regex rules in a worker thread, so a
|
|
3
|
+
* catastrophic pattern can be stopped by the configured timeout or by
|
|
4
|
+
* cancellation. The worker (`replace-worker.mjs` → `replace-worker.ts`) calls
|
|
5
|
+
* the same `core/replace.ts` engine that literal-only batches run in-process.
|
|
6
|
+
*
|
|
7
|
+
* @module pi-hashline-edit/pi
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Worker } from "node:worker_threads";
|
|
11
|
+
import type { applyReplacements, Replacement } from "../core/replace.ts";
|
|
12
|
+
import { cancellationError, throwIfCancelled } from "./error-text.ts";
|
|
13
|
+
|
|
14
|
+
type ReplaceResult = ReturnType<typeof applyReplacements>;
|
|
15
|
+
|
|
16
|
+
export async function runRegexReplacements(
|
|
17
|
+
source: string,
|
|
18
|
+
rules: readonly Replacement[],
|
|
19
|
+
timeoutMs: number,
|
|
20
|
+
signal: AbortSignal | undefined,
|
|
21
|
+
): Promise<ReplaceResult> {
|
|
22
|
+
throwIfCancelled(signal);
|
|
23
|
+
const worker = new Worker(new URL("./replace-worker.mjs", import.meta.url), {
|
|
24
|
+
workerData: { source, rules },
|
|
25
|
+
});
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
let settled = false;
|
|
28
|
+
const finish = (result: ReplaceResult | Error, terminate = false) => {
|
|
29
|
+
if (settled) return;
|
|
30
|
+
settled = true;
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
signal?.removeEventListener("abort", abort);
|
|
33
|
+
const complete = () => (result instanceof Error ? reject(result) : resolve(result));
|
|
34
|
+
if (terminate) void worker.terminate().then(complete, reject);
|
|
35
|
+
else complete();
|
|
36
|
+
};
|
|
37
|
+
const abort = () => finish(cancellationError(), true);
|
|
38
|
+
const timer = setTimeout(
|
|
39
|
+
() => finish(new Error(`regex evaluation timed out after ${timeoutMs}ms`), true),
|
|
40
|
+
timeoutMs,
|
|
41
|
+
);
|
|
42
|
+
worker.on("message", (message: { result?: ReplaceResult; error?: string }) => {
|
|
43
|
+
if (message.error !== undefined) finish(new Error(message.error));
|
|
44
|
+
else if (message.result) finish(message.result);
|
|
45
|
+
else finish(new Error("Regex worker returned an invalid result"));
|
|
46
|
+
});
|
|
47
|
+
worker.on("error", (error) =>
|
|
48
|
+
finish(error instanceof Error ? error : new Error(String(error))),
|
|
49
|
+
);
|
|
50
|
+
worker.on("exit", (code) => finish(new Error(`Regex worker exited with code ${code}`)));
|
|
51
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
52
|
+
if (signal?.aborted) abort();
|
|
53
|
+
});
|
|
54
|
+
}
|