@criogaid/pi-hashline-edit 0.7.0 → 0.9.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.
Files changed (118) hide show
  1. package/README.md +76 -220
  2. package/README.zh-CN.md +166 -0
  3. package/package.json +2 -1
  4. package/src/core/AGENTS.md +1 -1
  5. package/src/core/apply.test.ts +171 -133
  6. package/src/core/apply.ts +34 -31
  7. package/src/core/case-fold.test.ts +11 -7
  8. package/src/core/case-fold.ts +2 -6
  9. package/src/core/character-set.ts +14 -0
  10. package/src/core/hash.test.ts +1 -25
  11. package/src/core/hash.ts +0 -5
  12. package/src/core/lines.ts +25 -7
  13. package/src/core/ranges.test.ts +49 -9
  14. package/src/core/ranges.ts +15 -4
  15. package/src/core/regex.ts +36 -6
  16. package/src/core/replace-schema.ts +18 -11
  17. package/src/core/replace.test.ts +90 -0
  18. package/src/core/replace.ts +6 -5
  19. package/src/core/search-analysis.test.ts +9 -0
  20. package/src/core/search-analysis.ts +35 -31
  21. package/src/core/search.ts +1 -1
  22. package/src/core/types.ts +6 -8
  23. package/src/index.ts +2 -4
  24. package/src/integration/config-registration.test.ts +18 -10
  25. package/src/integration/fusion-dispatch.test.ts +41 -22
  26. package/src/integration/grep-flags.test.ts +6 -13
  27. package/src/integration/grep-js-completeness.test.ts +41 -31
  28. package/src/integration/grep-order.test.ts +78 -0
  29. package/src/integration/grep-path-matrix.test.ts +4 -3
  30. package/src/integration/grep-regex.test.ts +13 -16
  31. package/src/integration/grep-rg.test.ts +132 -92
  32. package/src/integration/grep-scan-budget.test.ts +27 -5
  33. package/src/integration/large-text.test.ts +4 -5
  34. package/src/integration/model-budget-lifecycle.test.ts +26 -8
  35. package/src/integration/mutation-file-types.test.ts +0 -1
  36. package/src/integration/mutation-file-types.testing.ts +2 -2
  37. package/src/integration/npm-package.testing.ts +7 -33
  38. package/src/integration/publication-read-changes.test.ts +60 -28
  39. package/src/integration/replace-package.testing.ts +4 -1
  40. package/src/integration/replace-worker-boundary.test.ts +34 -24
  41. package/src/integration/report-replay.test.ts +3 -3
  42. package/src/integration/utf8-errors.test.ts +3 -3
  43. package/src/pi/AGENTS.md +13 -14
  44. package/src/pi/action-fusion.test.ts +75 -220
  45. package/src/pi/action-fusion.ts +37 -74
  46. package/src/pi/anchor-format.test.ts +16 -0
  47. package/src/pi/anchor-format.ts +7 -3
  48. package/src/pi/argument-diagnostics.ts +35 -186
  49. package/src/pi/argument-error.testing.ts +7 -4
  50. package/src/pi/argument-error.ts +5 -7
  51. package/src/pi/argument-format.test.ts +25 -38
  52. package/src/pi/argument-validation.test.ts +446 -420
  53. package/src/pi/argument-validation.ts +4 -5
  54. package/src/pi/budgets.ts +5 -7
  55. package/src/pi/config.test.ts +15 -5
  56. package/src/pi/config.ts +6 -6
  57. package/src/pi/diagnostic-buffer.test.ts +2 -3
  58. package/src/pi/diagnostic-buffer.ts +2 -2
  59. package/src/pi/edit-tool.ts +175 -121
  60. package/src/pi/execute.test.ts +170 -227
  61. package/src/pi/failure-context.test.ts +61 -16
  62. package/src/pi/failure-context.ts +13 -10
  63. package/src/pi/failure-path.test.ts +115 -50
  64. package/src/pi/file-commit.test.ts +30 -46
  65. package/src/pi/file-commit.ts +11 -40
  66. package/src/pi/file-revision.ts +25 -0
  67. package/src/pi/fusion-card-interaction.test.ts +143 -0
  68. package/src/pi/fusion-card.test.ts +193 -37
  69. package/src/pi/fusion-card.ts +186 -116
  70. package/src/pi/grep-candidates.ts +25 -27
  71. package/src/pi/grep-matrix.test.ts +1 -1
  72. package/src/pi/grep-order.test.ts +323 -0
  73. package/src/pi/grep-output.testing.ts +24 -0
  74. package/src/pi/grep-output.ts +62 -69
  75. package/src/pi/grep-render.ts +4 -1
  76. package/src/pi/grep-scope.ts +1 -1
  77. package/src/pi/grep-search.ts +85 -56
  78. package/src/pi/grep-tool.ts +17 -48
  79. package/src/pi/grep.test.ts +271 -170
  80. package/src/pi/mutation-outcome.testing.ts +2 -3
  81. package/src/pi/mutation-result.test.ts +26 -19
  82. package/src/pi/mutation-result.ts +25 -40
  83. package/src/pi/mutation-runner.ts +9 -17
  84. package/src/pi/output-budgets.test.ts +27 -18
  85. package/src/pi/path.ts +3 -1
  86. package/src/pi/pi.test.ts +7 -6
  87. package/src/pi/read-tool.ts +32 -80
  88. package/src/pi/read.test.ts +145 -92
  89. package/src/pi/regex-worker-contract.ts +5 -23
  90. package/src/pi/regex-worker.ts +4 -10
  91. package/src/pi/registration.test.ts +106 -67
  92. package/src/pi/render.test.ts +239 -109
  93. package/src/pi/render.ts +42 -22
  94. package/src/pi/replace-regex.ts +3 -5
  95. package/src/pi/replace-tool.ts +29 -27
  96. package/src/pi/replace-worker-contract.ts +3 -12
  97. package/src/pi/replace.test.ts +144 -226
  98. package/src/pi/report-cause.ts +23 -19
  99. package/src/pi/report-failure.testing.ts +2 -2
  100. package/src/pi/report-schema.ts +60 -45
  101. package/src/pi/report.test.ts +138 -71
  102. package/src/pi/report.ts +40 -43
  103. package/src/pi/result-safety.test.ts +186 -131
  104. package/src/pi/rg-process.test.ts +36 -10
  105. package/src/pi/rg-process.ts +9 -3
  106. package/src/pi/rg-regex.test.ts +17 -0
  107. package/src/pi/rg-regex.ts +28 -22
  108. package/src/pi/text-stream.test.ts +35 -36
  109. package/src/pi/text-stream.ts +14 -14
  110. package/src/pi/tool-call.testing.ts +17 -7
  111. package/src/pi/write-tool.ts +9 -11
  112. package/src/pi/write.test.ts +43 -75
  113. package/src/testing/regex-translation-corpus.ts +16 -0
  114. package/src/testing/rg-line-listing.fixture.mjs +1 -1
  115. package/src/integration/forget-grep.test.ts +0 -148
  116. package/src/pi/forget-tool.ts +0 -367
  117. package/src/pi/forget.test.ts +0 -630
  118. package/src/pi/forget.testing.ts +0 -201
@@ -6,45 +6,67 @@ import { readFile, unlink, writeFile } from "node:fs/promises";
6
6
  import { join } from "node:path";
7
7
  import type { AgentToolResult } from "@earendil-works/pi-agent-core";
8
8
  import { visibleWidth } from "@earendil-works/pi-tui";
9
- import {
10
- generateDiffString,
11
- initTheme,
12
- type ToolDefinition,
13
- } from "@earendil-works/pi-coding-agent";
9
+ import { initTheme, type ToolDefinition } from "@earendil-works/pi-coding-agent";
14
10
  import type { TSchema } from "typebox";
15
11
  import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
16
- import { displayCarriageReturns } from "./anchor-format.ts";
17
12
  import { makeEditOverride } from "./edit-tool.ts";
18
13
  import { makeReplaceTool } from "./replace-tool.ts";
19
- import { renderDiffPreview } from "./render.ts";
14
+ import { renderDiffPreview, renderMutationResult, reportLines } from "./render.ts";
20
15
  import { withMutationStatus } from "./fusion-card.ts";
21
16
  import { makeWriteOverride } from "./write-tool.ts";
22
17
  import { generateMutationDetails } from "./mutation-result.ts";
23
- import {
24
- createActionFusionExecutor,
25
- type ActionFusionDetails,
26
- type ActionFusionProgress,
27
- } from "./action-fusion.ts";
18
+ import { createActionFusionExecutor, type ActionFusionProgress } from "./action-fusion.ts";
28
19
  import { DEFAULT_CONFIG } from "./config.ts";
29
20
  import { toDisplayLines } from "./grep-render.ts";
21
+ import { parseFileHeader } from "./grep-output.ts";
30
22
  import { makeReadOverride } from "./read-tool.ts";
31
23
  import { callTool } from "./tool-call.testing.ts";
32
24
  import { renderToolError } from "./render.ts";
33
25
  import { formatArgumentError } from "./argument-error.ts";
34
26
  import { makeGrepOverride } from "./grep-tool.ts";
35
- import { registerForgetTool } from "./forget-tool.ts";
36
27
  import { openTestSession } from "../testing/session.testing.ts";
28
+ import type { SuccessReport } from "./report-schema.ts";
29
+ import { renderReport, type Report } from "./report.ts";
30
+ import { DEFAULT_OUTPUT_BUDGETS, MAX_CONTENT_KIB, outputBudgetsFor } from "./budgets.ts";
37
31
 
38
- const versions = { publishedRevision: "r", observedRevision: "r" };
39
- const mutationDetails = (actionFusion?: ActionFusionDetails) => ({
40
- ...generateMutationDetails("a.txt", "old\n", "new\n", versions, "PUBLISHED"),
32
+ const mutationDetails = (
33
+ report: Pick<SuccessReport, "publication" | "freshness" | "then_run">,
34
+ ) => ({
35
+ ...generateMutationDetails("old\n", "new\n"),
41
36
  report: {
42
37
  tool: "edit" as const,
43
38
  path: "a.txt",
44
- publication: actionFusion?.publication,
45
- freshness: actionFusion?.freshness === "unchanged" ? undefined : actionFusion?.freshness,
39
+ ...report,
46
40
  },
47
- actionFusion,
41
+ });
42
+
43
+ test("empty logical diff → render according to confirmed publication state", () => {
44
+ initTheme("dark");
45
+ const diff = generateMutationDetails("same\r\nsame\n", "same\nsame\r\n");
46
+ assert.equal(diff.diff, "");
47
+ for (const publication of ["PUBLISHED", "NOT_PUBLISHED", undefined] as const) {
48
+ const output = stripVTControlCharacters(
49
+ renderMutationResult(
50
+ {
51
+ content: [],
52
+ details: { ...diff, report: { tool: "edit", publication } },
53
+ },
54
+ { isPartial: false, expanded: true },
55
+ theme,
56
+ { isError: false, args: {} },
57
+ "Editing…",
58
+ () => "",
59
+ "edit",
60
+ )
61
+ .render(100)
62
+ .join("\n"),
63
+ );
64
+ if (publication === "NOT_PUBLISHED") assert.match(output, /No net change/);
65
+ else {
66
+ assert.doesNotMatch(output, /No net change/);
67
+ assert.match(output, publication === "PUBLISHED" ? /Updated/ : /No diff available/);
68
+ }
69
+ }
48
70
  });
49
71
 
50
72
  test("mutation headers refresh in place and retain isolated per-call counts", () => {
@@ -55,8 +77,8 @@ test("mutation headers refresh in place and retain isolated per-call counts", ()
55
77
  ]) {
56
78
  const args = {
57
79
  path: "a.txt",
58
- edits: [{ op: "append" as const, body: ["new"] }],
59
- replacements: [{ find: "old", replace: "new" }],
80
+ edits: [{ action: "append" as const, body: ["new"] }],
81
+ replacements: [{ literal: true, find: "old", replace: "new" }],
60
82
  };
61
83
  const context: any = {
62
84
  args,
@@ -71,7 +93,7 @@ test("mutation headers refresh in place and retain isolated per-call counts", ()
71
93
  tool.renderResult(
72
94
  {
73
95
  content: [{ type: "text", text: "Done" }],
74
- details: generateMutationDetails("a.txt", "old\n", "new\n", versions, "PUBLISHED"),
96
+ details: generateMutationDetails("old\n", "new\n"),
75
97
  },
76
98
  { isPartial: false, expanded: false },
77
99
  theme,
@@ -86,19 +108,19 @@ test("mutation headers refresh in place and retain isolated per-call counts", ()
86
108
  }
87
109
  });
88
110
 
89
- test("edit call titles count each operation type and refresh with partial input", () => {
111
+ test("partial edit actions → refresh per-action counts", () => {
90
112
  initTheme("dark");
91
113
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
92
114
  const edits = [
93
- { op: "replace", anchor: "1#AB", body: ["new"] },
94
- { op: "append", body: ["more"] },
95
- { op: "replace", anchor: "3#CD", body: ["changed"] },
96
- { op: "delete", anchor: "5#EF" },
97
- { op: "insert_before", anchor: "7#GH", body: ["before"] },
98
- { op: "insert_after", anchor: "9#JK", body: ["after"] },
99
- { op: "prepend", body: ["first"] },
100
- { op: "copy", anchor: "1#AB", before: "3#CD" },
101
- { op: "move", anchor: "3#CD", after: "5#EF" },
115
+ { action: "replace", anchor: "1#AB", body: ["new"] },
116
+ { action: "append", body: ["more"] },
117
+ { action: "replace", anchor: "3#CD", body: ["changed"] },
118
+ { action: "delete", anchor: "5#EF" },
119
+ { action: "insert_before", anchor: "7#GH", body: ["before"] },
120
+ { action: "insert_after", anchor: "9#JK", body: ["after"] },
121
+ { action: "prepend", body: ["first"] },
122
+ { action: "copy", anchor: "1#AB", before: "3#CD" },
123
+ { action: "move", anchor: "3#CD", after: "5#EF" },
102
124
  ];
103
125
  let header: ReturnType<typeof tool.renderCall> | undefined;
104
126
  const context = { state: {} } as Parameters<typeof tool.renderCall>[2];
@@ -131,7 +153,7 @@ test("edit call titles count each operation type and refresh with partial input"
131
153
  assert.match(single, /1 op: replace ×1/);
132
154
  assert.doesNotMatch(single, /append|copy|delete|insert_before|insert_after|move|prepend/);
133
155
  assert.match(
134
- render([null, { op: "rep" }, { op: "__proto__" }, { op: { toString: null } }]),
156
+ render([null, { action: "rep" }, { action: "__proto__" }, { action: { toString: null } }]),
135
157
  /4 ops: unknown ×4/,
136
158
  );
137
159
  for (const invalid of [[], undefined, JSON.stringify(edits), "[{bad"]) {
@@ -139,10 +161,37 @@ test("edit call titles count each operation type and refresh with partial input"
139
161
  }
140
162
  });
141
163
 
164
+ test("missing or empty mutation path → show a themed placeholder until the path arrives", () => {
165
+ initTheme("dark");
166
+ for (const tool of [
167
+ makeEditOverride(process.cwd(), DEFAULT_CONFIG),
168
+ makeReplaceTool(process.cwd(), DEFAULT_CONFIG),
169
+ ]) {
170
+ const context = { state: {} } as Parameters<
171
+ ReturnType<typeof makeEditOverride>["renderCall"]
172
+ >[2] &
173
+ Parameters<ReturnType<typeof makeReplaceTool>["renderCall"]>[2];
174
+ let header: ReturnType<typeof tool.renderCall> | undefined;
175
+ for (const path of [undefined, "", "f.txt"]) {
176
+ // Streaming arguments may contain operations before the path arrives.
177
+ const args = { path, edits: [], replacements: [] } as Parameters<
178
+ ReturnType<typeof makeEditOverride>["renderCall"]
179
+ >[0] &
180
+ Parameters<ReturnType<typeof makeReplaceTool>["renderCall"]>[0];
181
+ const previous = header;
182
+ header = tool.renderCall(args, theme, { ...context, lastComponent: header });
183
+ if (previous) assert.equal(header, previous);
184
+ const output = header.render(100).join("\n");
185
+ assert.ok(output.includes(theme.fg(path ? "accent" : "toolOutput", path || "...")));
186
+ assert.ok(stripVTControlCharacters(output).startsWith(`${tool.name} ${path || "..."}`));
187
+ }
188
+ }
189
+ });
190
+
142
191
  test("mutation card owns stale-anchor notices without internal status", () => {
143
192
  initTheme("dark");
144
193
  const tool = withMutationStatus(makeEditOverride(process.cwd(), DEFAULT_CONFIG));
145
- const args = { path: "a.txt", edits: [{ op: "append" as const, body: ["new"] }] };
194
+ const args = { path: "a.txt", edits: [{ action: "append" as const, body: ["new"] }] };
146
195
  const context: any = { args, state: {}, isPartial: false, isError: false, invalidate() {} };
147
196
  const card = tool.renderCall!(args, theme, context);
148
197
  tool.renderResult!(
@@ -154,7 +203,7 @@ test("mutation card owns stale-anchor notices without internal status", () => {
154
203
  details: mutationDetails({
155
204
  publication: "PUBLISHED",
156
205
  freshness: "changed",
157
- command: "failed",
206
+ then_run: { status: "failed" },
158
207
  }),
159
208
  },
160
209
  { isPartial: false, expanded: false },
@@ -163,7 +212,7 @@ test("mutation card owns stale-anchor notices without internal status", () => {
163
212
  );
164
213
  const output = card.render(160).join("\n");
165
214
  assert.match(output, /new/);
166
- assert.doesNotMatch(output, /publication=|freshness=/);
215
+ assert.doesNotMatch(stripVTControlCharacters(output), /(?:^|\n)\s*publication:/);
167
216
  assert.match(stripVTControlCharacters(output), /freshness: changed/);
168
217
  assert.doesNotMatch(output, /command diagnostic/);
169
218
  assert.ok(output.includes(theme.getBgAnsi("toolSuccessBg")));
@@ -172,7 +221,7 @@ test("mutation card owns stale-anchor notices without internal status", () => {
172
221
  test("mutation card omits status when then_run was not requested", () => {
173
222
  initTheme("dark");
174
223
  const tool = withMutationStatus(makeEditOverride(process.cwd(), DEFAULT_CONFIG));
175
- const args = { path: "a.txt", edits: [{ op: "append" as const, body: ["new"] }] };
224
+ const args = { path: "a.txt", edits: [{ action: "append" as const, body: ["new"] }] };
176
225
  const context: any = { args, state: {}, isPartial: false, isError: false, invalidate() {} };
177
226
  const card = tool.renderCall!(args, theme, context);
178
227
  tool.renderResult!(
@@ -180,8 +229,6 @@ test("mutation card omits status when then_run was not requested", () => {
180
229
  content: [{ type: "text", text: "Edited a.txt." }],
181
230
  details: mutationDetails({
182
231
  publication: "PUBLISHED",
183
- freshness: "unchanged",
184
- command: "not_requested",
185
232
  }),
186
233
  },
187
234
  { isPartial: false, expanded: false },
@@ -190,7 +237,7 @@ test("mutation card omits status when then_run was not requested", () => {
190
237
  );
191
238
  const output = card.render(160).join("\n");
192
239
  assert.match(output, /new/);
193
- assert.doesNotMatch(output, /publication=|freshness=/);
240
+ assert.doesNotMatch(stripVTControlCharacters(output), /(?:^|\n)\s*(?:publication|freshness):/);
194
241
  tool.renderCall!(args, theme, context);
195
242
  tool.renderResult!(
196
243
  {
@@ -198,7 +245,6 @@ test("mutation card omits status when then_run was not requested", () => {
198
245
  details: mutationDetails({
199
246
  publication: "PUBLISHED",
200
247
  freshness: "changed",
201
- command: "not_requested",
202
248
  }),
203
249
  },
204
250
  { isPartial: false, expanded: false },
@@ -207,7 +253,7 @@ test("mutation card omits status when then_run was not requested", () => {
207
253
  );
208
254
  const staleOutput = card.render(160).join("\n");
209
255
  assert.match(stripVTControlCharacters(staleOutput), /freshness: changed/);
210
- assert.doesNotMatch(staleOutput, /publication=|freshness=/);
256
+ assert.doesNotMatch(stripVTControlCharacters(staleOutput), /(?:^|\n)\s*publication:/);
211
257
  });
212
258
 
213
259
  test("unfused write errors retain the native full diagnostic", () => {
@@ -219,13 +265,7 @@ test("unfused write errors retain the native full diagnostic", () => {
219
265
  tool.renderResult!(
220
266
  {
221
267
  content: [{ type: "text", text: "write failed\nimportant detail" }],
222
- details: {
223
- path: "a.txt",
224
- created: false,
225
- publication: "NOT_PUBLISHED",
226
- publishedRevision: "r",
227
- observedRevision: "r",
228
- },
268
+ details: {},
229
269
  },
230
270
  { isPartial: false, expanded: false },
231
271
  theme,
@@ -238,16 +278,7 @@ test("mutation previews hide CRLF boundaries even with mixed endings or an unter
238
278
  initTheme("dark");
239
279
  for (const before of ["old\r\ncontext\r\n", "old\r\ncontext\nlast", "old\r\ncontext\r\nlast"]) {
240
280
  const after = before.replace("old", "new");
241
- const details = generateMutationDetails(
242
- "a.txt",
243
- before,
244
- after,
245
- { publishedRevision: "r", observedRevision: "r" },
246
- "PUBLISHED",
247
- );
248
- assert.equal(details.diff, displayCarriageReturns(generateDiffString(before, after).diff));
249
- assert.match(details.diff, /␍/);
250
- assert.ok(details.patch.includes("-old\r\n"));
281
+ const details = generateMutationDetails(before, after);
251
282
  for (const tool of [
252
283
  makeEditOverride(process.cwd(), DEFAULT_CONFIG),
253
284
  makeReplaceTool(process.cwd(), DEFAULT_CONFIG),
@@ -271,20 +302,14 @@ test("mutation previews hide CRLF boundaries even with mixed endings or an unter
271
302
  test("diff previews retain standalone CR and literal control-picture characters", () => {
272
303
  initTheme("dark");
273
304
  for (const before of ["old\r", "old␍", "old\rcontent\r\n"]) {
274
- const details = generateMutationDetails(
275
- "a.txt",
276
- before,
277
- before.replace("old", "new"),
278
- { publishedRevision: "r", observedRevision: "r" },
279
- "PUBLISHED",
280
- );
305
+ const details = generateMutationDetails(before, before.replace("old", "new"));
281
306
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
282
307
  const result = tool.renderResult(
283
308
  { content: [], details },
284
309
  { expanded: true, isPartial: false },
285
310
  theme,
286
311
  {
287
- args: { path: "a.txt", edits: [{ op: "append", body: ["new"] }] },
312
+ args: { path: "a.txt", edits: [{ action: "append", body: ["new"] }] },
288
313
  state: {},
289
314
  isError: false,
290
315
  } as Parameters<typeof tool.renderResult>[3],
@@ -294,26 +319,33 @@ test("diff previews retain standalone CR and literal control-picture characters"
294
319
  assert.match(renderDiffPreview("-1 old␍\n+1 new␍", true, theme), /␍/);
295
320
  });
296
321
 
297
- test("withMutationStatus renderResult initializes mutationShell defensively", () => {
322
+ test("150,000 rows in one grep group → renders every row with shared indentation folded", () => {
298
323
  initTheme("dark");
299
- const tool = withMutationStatus(makeEditOverride(process.cwd(), DEFAULT_CONFIG));
300
- const context: any = { args: { path: "a.txt" }, state: {}, isError: false };
301
- const container = tool.renderResult!(
302
- {
303
- content: [{ type: "text", text: "Edited a.txt." }],
304
- details: mutationDetails({
305
- publication: "PUBLISHED",
306
- freshness: "unchanged",
307
- command: "not_requested",
308
- }),
309
- },
310
- { isPartial: false, expanded: false },
311
- theme,
312
- context,
313
- );
314
- assert.ok(container);
315
- assert.ok(context.state.mutationShell);
316
- assert.ok(context.state.mutationShell.box);
324
+ const rowCount = 150_000;
325
+ const header = `many.txt · ${rowCount} matches`;
326
+ const raw = [
327
+ header,
328
+ ...Array.from(
329
+ { length: rowCount },
330
+ (_, index) => `${index + 1}#ABCD│${index === 0 ? " " : " "}hit`,
331
+ ),
332
+ ].join("\n");
333
+ const lines = toDisplayLines(raw, theme).map(stripVTControlCharacters);
334
+ assert.equal(lines.length, rowCount + 1);
335
+ assert.equal(lines[0], header);
336
+ const width = String(rowCount).length;
337
+ assert.equal(lines[1], ` ${"1".padStart(width)}: › hit`);
338
+ assert.equal(lines[2], ` ${"2".padStart(width)}: › hit`);
339
+ assert.equal(lines.at(-1), ` ${rowCount}: › hit`);
340
+ });
341
+
342
+ test("Unicode line separators in grep paths → parses the full file header", async (t) => {
343
+ for (const separator of ["\u2028", "\u2029"]) {
344
+ await t.test(JSON.stringify(separator), () => {
345
+ const path = `before${separator}after.txt`;
346
+ assert.deepEqual(parseFileHeader(`${path} · 2 matches`), { path, summary: "2 matches" });
347
+ });
348
+ }
317
349
  });
318
350
 
319
351
  test("grep renders anchored and plain groups with shared width and folded indentation", () => {
@@ -348,24 +380,15 @@ test("grep renders plain and anchored rows in one group without exposing hashes"
348
380
  ]);
349
381
  });
350
382
 
351
- test("all tools render argument issues as field diagnostics while preserving model JSON", async (t) => {
383
+ test("all tools render argument issues as field diagnostics while preserving model JSON", async () => {
352
384
  initTheme("dark");
353
385
  const cwd = process.cwd();
354
- const { session } = await openTestSession(t, {
355
- tools: ["forget"],
356
- configure(pi) {
357
- registerForgetTool(pi);
358
- },
359
- });
360
- const forget = session.getToolDefinition("forget");
361
- assert.ok(forget);
362
386
  const tools = [
363
387
  makeReadOverride(cwd, DEFAULT_CONFIG),
364
388
  withMutationStatus(makeEditOverride(cwd, DEFAULT_CONFIG)),
365
389
  withMutationStatus(makeReplaceTool(cwd, DEFAULT_CONFIG)),
366
390
  withMutationStatus(makeWriteOverride(cwd)),
367
391
  makeGrepOverride(cwd, DEFAULT_CONFIG),
368
- forget,
369
392
  ];
370
393
  for (const tool of tools) {
371
394
  for (const fused of [false, true]) {
@@ -373,11 +396,10 @@ test("all tools render argument issues as field diagnostics while preserving mod
373
396
  path: "unused.txt",
374
397
  content: "unused",
375
398
  mode: "create" as const,
376
- edits: [{ op: "append" as const, body: ["unused"] }],
377
- replacements: [{ find: "unused", replace: "unused" }],
399
+ edits: [{ action: "append" as const, body: ["unused"] }],
400
+ replacements: [{ literal: true, find: "unused", replace: "unused" }],
378
401
  pattern: "unused",
379
402
  literal: true,
380
- ids: ["r00000"],
381
403
  unexpected: true,
382
404
  ...(fused ? { then_run: { command: "must-not-run" } } : {}),
383
405
  };
@@ -443,8 +465,8 @@ test("expanded errors retain every text block while collapsed errors stay bounde
443
465
  { type: "text" as const, text: "Final cause: permission denied" },
444
466
  ],
445
467
  };
446
- const collapsed = renderToolError(result, theme, false).render(120).join("\n");
447
- const expanded = renderToolError(result, theme, true).render(120).join("\n");
468
+ const collapsed = renderToolError(result, "edit", theme, false).render(120).join("\n");
469
+ const expanded = renderToolError(result, "edit", theme, true).render(120).join("\n");
448
470
  assert.match(collapsed, /Error header/);
449
471
  assert.doesNotMatch(collapsed, /Final cause/);
450
472
  assert.match(expanded, /Final cause: permission denied/);
@@ -457,7 +479,7 @@ test("argument cards preserve omission notices and all expanded issues", () => {
457
479
  field: `edits[${index}].body`,
458
480
  reason: `中文🙂 ${index}: supply a line.\n${"context ".repeat(50)}\nFinal cause: empty body.`,
459
481
  }));
460
- const diagnostic = formatArgumentError("edit", issues, true);
482
+ const diagnostic = formatArgumentError(issues, true, DEFAULT_OUTPUT_BUDGETS);
461
483
  const parsed = argumentError(new Error(diagnostic));
462
484
  assert.ok(parsed.schemaLimited && parsed.omittedIssues);
463
485
  const result = {
@@ -465,10 +487,10 @@ test("argument cards preserve omission notices and all expanded issues", () => {
465
487
  };
466
488
  const original = structuredClone(result);
467
489
  const collapsed = stripVTControlCharacters(
468
- renderToolError(result, theme, false).render(100).join("\n"),
490
+ renderToolError(result, "edit", theme, false).render(100).join("\n"),
469
491
  );
470
492
  const expanded = stripVTControlCharacters(
471
- renderToolError(result, theme, true).render(100).join("\n"),
493
+ renderToolError(result, "edit", theme, true).render(100).join("\n"),
472
494
  );
473
495
  for (const displayed of [collapsed, expanded]) {
474
496
  assert.match(displayed, /Schema diagnostic limit/);
@@ -493,7 +515,7 @@ test("unrecognized or malformed JSON errors retain their original diagnostic", (
493
515
  ]) {
494
516
  const result = { content: [{ type: "text" as const, text: diagnostic }] };
495
517
  const displayed = stripVTControlCharacters(
496
- renderToolError(result, theme, true).render(300).join("\n"),
518
+ renderToolError(result, "edit", theme, true).render(300).join("\n"),
497
519
  );
498
520
  assert.ok(displayed.includes(diagnostic || "Error"));
499
521
  }
@@ -502,13 +524,13 @@ test("unrecognized or malformed JSON errors retain their original diagnostic", (
502
524
  test("argument diagnostics wrap Unicode and multiline reasons at narrow terminal widths", () => {
503
525
  const field = "edits[0].body";
504
526
  const reason = '中文🙂: supply at least one line.\nUse ["空行"] for this example.';
505
- const diagnostic = formatArgumentError("edit", [{ field, reason }], false);
527
+ const diagnostic = formatArgumentError([{ field, reason }], false, DEFAULT_OUTPUT_BUDGETS);
506
528
  const result = { content: [{ type: "text" as const, text: diagnostic }] };
507
529
  for (const appearance of ["dark", "light"] as const) {
508
530
  initTheme(appearance);
509
531
  for (const width of [24, 80]) {
510
532
  for (const expanded of [false, true]) {
511
- const lines = renderToolError(result, theme, expanded).render(width);
533
+ const lines = renderToolError(result, "edit", theme, expanded).render(width);
512
534
  assert.ok(lines.every((line) => visibleWidth(line) <= width));
513
535
  const displayed = stripVTControlCharacters(lines.join("\n")).replace(/\s/g, "");
514
536
  for (const value of [field, reason, "edit not executed"]) {
@@ -530,7 +552,7 @@ test("command changes or removes the target → completed mutation stays success
530
552
  assert.equal(await readFile(target, "utf8"), "saved\n");
531
553
  if (disturbance === "changed") await writeFile(target, "command changed the file\n");
532
554
  else await unlink(target);
533
- return { status: "succeeded", output: "checked" };
555
+ return { status: "succeeded", result: { content: [{ type: "text", text: "checked" }] } };
534
556
  });
535
557
  const tool = withMutationStatus(makeWriteOverride(cwd, fusion));
536
558
  const args = {
@@ -571,7 +593,6 @@ test("command changes or removes the target → completed mutation stays success
571
593
  });
572
594
  assert.ok(completedFrames.length > 0);
573
595
  assert.ok(completedFrames.every((frame) => frame.includes(theme.getBgAnsi("toolSuccessBg"))));
574
- assert.equal(result.details.actionFusion.freshness, disturbance);
575
596
  context.isPartial = false;
576
597
  tool.renderCall!(args, theme, context);
577
598
  renderResult(result, { isPartial: false, expanded: false }, theme, context);
@@ -583,3 +604,112 @@ test("command changes or removes the target → completed mutation stays success
583
604
  else await assert.rejects(readFile(target), { code: "ENOENT" });
584
605
  }
585
606
  });
607
+
608
+ test("success renderers receive replayed or hooked reports → display only schema-checked facts", () => {
609
+ initTheme("dark");
610
+ const cwd = process.cwd();
611
+ const cases = [
612
+ { name: "grep" as const, tool: makeGrepOverride(cwd, DEFAULT_CONFIG) },
613
+ { name: "read" as const, tool: makeReadOverride(cwd, DEFAULT_CONFIG) },
614
+ { name: "read" as const, tool: makeReadOverride(cwd, DEFAULT_CONFIG), nativeRead: true },
615
+ { name: "write" as const, tool: makeWriteOverride(cwd) },
616
+ { name: "edit" as const, tool: makeEditOverride(cwd, DEFAULT_CONFIG) },
617
+ { name: "replace" as const, tool: makeReplaceTool(cwd, DEFAULT_CONFIG) },
618
+ ];
619
+ for (const { name, tool, nativeRead } of cases) {
620
+ // Pi renders persisted or hooked details with their result type erased.
621
+ const render = tool.renderResult as NonNullable<
622
+ ToolDefinition<TSchema, unknown>["renderResult"]
623
+ >;
624
+ const context: Parameters<typeof render>[3] = {
625
+ args: { path: "a.txt" },
626
+ state: {},
627
+ cwd,
628
+ toolCallId: `${tool.name}-${nativeRead ?? false}`,
629
+ lastComponent: undefined,
630
+ invalidate() {},
631
+ executionStarted: true,
632
+ argsComplete: true,
633
+ isPartial: false,
634
+ expanded: true,
635
+ showImages: false,
636
+ isError: false,
637
+ };
638
+ const show = (report: unknown, text: string) => {
639
+ const result = {
640
+ content: [{ type: "text" as const, text }],
641
+ details: { nativeRead, report },
642
+ };
643
+ const before = structuredClone(result);
644
+ const output = stripVTControlCharacters(
645
+ render(result, { isPartial: false, expanded: true }, theme, context).render(240).join("\n"),
646
+ );
647
+ assert.deepEqual(result, before);
648
+ return output;
649
+ };
650
+ const payload = (text: string) =>
651
+ name === "read" && !nativeRead ? `a.txt · 1 lines\n1#ABCD│${text}` : text;
652
+ const report: SuccessReport = { tool: name, next: "CHECKED_REPORT_RECOVERY" };
653
+ const checked = show(report, renderReport(report));
654
+ assert.doesNotMatch(checked, /"next"/);
655
+ if (!nativeRead) assert.match(checked, /next: CHECKED_REPORT_RECOVERY/);
656
+ else assert.doesNotMatch(checked, /CHECKED_REPORT_RECOVERY/);
657
+ for (const invalid of [
658
+ undefined,
659
+ null,
660
+ "foreign report",
661
+ { ...report, tool: "foreign", next: "UNTRUSTED_REPORT_FACT" },
662
+ { ...report, foreign: true, next: "UNTRUSTED_REPORT_FACT" },
663
+ { ...report, then_run: { status: "foreign" }, next: "UNTRUSTED_REPORT_FACT" },
664
+ ]) {
665
+ const output = show(invalid, payload("VISIBLE_PAYLOAD"));
666
+ assert.doesNotMatch(output, /UNTRUSTED_REPORT_FACT|CHECKED_REPORT_RECOVERY/);
667
+ if (name === "write") assert.equal(output, "");
668
+ else
669
+ assert.match(
670
+ output,
671
+ name === "edit" || name === "replace" ? /No diff available/ : /VISIBLE_PAYLOAD/,
672
+ );
673
+ }
674
+ if (tool.name === "grep" || tool.name === "read") {
675
+ const foreign = '{"hook":"VISIBLE_JSON"}';
676
+ assert.ok(show(report, payload(foreign)).includes(foreign));
677
+ }
678
+ }
679
+ });
680
+
681
+ test("report text has many short lines within the byte budget → renders every diagnostic line", async (t) => {
682
+ initTheme("dark");
683
+ const newlineCount = 150_000;
684
+ const message = `first${"\n".repeat(newlineCount)}last`;
685
+ assert.ok(Buffer.byteLength(message) < outputBudgetsFor(MAX_CONTENT_KIB * 1024).fieldBytes);
686
+ const cases: readonly { name: string; report: Report; lines: number }[] = [
687
+ {
688
+ name: "failure message",
689
+ report: { tool: "read", error: "UNCLASSIFIED", message },
690
+ lines: newlineCount + 2,
691
+ },
692
+ {
693
+ name: "search diagnostic",
694
+ report: { tool: "grep", diagnostics: message },
695
+ lines: newlineCount + 1,
696
+ },
697
+ {
698
+ name: "argument reason",
699
+ report: {
700
+ tool: "edit",
701
+ error: "INVALID_ARGUMENTS",
702
+ issues: [{ field: "path", reason: message }],
703
+ },
704
+ lines: newlineCount + 2,
705
+ },
706
+ ];
707
+ for (const entry of cases) {
708
+ await t.test(entry.name, () => {
709
+ const lines = reportLines(entry.report, theme).map(stripVTControlCharacters);
710
+ assert.equal(lines.length, entry.lines);
711
+ assert.ok(lines.some((line) => line.includes("first")));
712
+ assert.equal(lines.at(-1)?.trim(), "last");
713
+ });
714
+ }
715
+ });