@criogaid/pi-hashline-edit 0.9.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.
Files changed (131) hide show
  1. package/README.md +12 -12
  2. package/README.zh-CN.md +12 -12
  3. package/THIRD_PARTY_NOTICES.md +0 -6
  4. package/package.json +1 -1
  5. package/src/core/AGENTS.md +4 -5
  6. package/src/core/apply.test.ts +96 -271
  7. package/src/core/apply.ts +62 -169
  8. package/src/core/case-fold.ts +8 -67
  9. package/src/core/character-set.test.ts +2 -3
  10. package/src/core/character-set.ts +5 -5
  11. package/src/core/errors.test.ts +23 -7
  12. package/src/core/errors.ts +32 -5
  13. package/src/core/hash.test.ts +8 -16
  14. package/src/core/hash.ts +3 -23
  15. package/src/core/lines.ts +2 -4
  16. package/src/core/ranges.test.ts +2 -1
  17. package/src/core/ranges.ts +1 -1
  18. package/src/core/regex.ts +3 -4
  19. package/src/core/replace.test.ts +47 -1
  20. package/src/core/replace.ts +19 -13
  21. package/src/core/search-analysis.test.ts +24 -27
  22. package/src/core/search-analysis.ts +34 -65
  23. package/src/core/search-matrix.testing.ts +0 -2
  24. package/src/core/search.ts +1 -1
  25. package/src/core/text.test.ts +5 -5
  26. package/src/core/text.ts +5 -12
  27. package/src/core/types.ts +8 -44
  28. package/src/index.ts +8 -8
  29. package/src/integration/config-registration.test.ts +8 -10
  30. package/src/integration/fusion-dispatch.test.ts +4 -7
  31. package/src/integration/grep-errors.test.ts +3 -3
  32. package/src/integration/grep-flags.test.ts +5 -19
  33. package/src/integration/grep-js-completeness.test.ts +45 -18
  34. package/src/integration/grep-line-candidates.test.ts +7 -10
  35. package/src/integration/grep-matrix.test.ts +6 -9
  36. package/src/integration/grep-order.test.ts +4 -4
  37. package/src/integration/grep-path-matrix.test.ts +10 -9
  38. package/src/integration/grep-prefilter-coverage.test.ts +5 -8
  39. package/src/integration/grep-process-limits.test.ts +76 -0
  40. package/src/integration/grep-regex.test.ts +10 -10
  41. package/src/integration/grep-rg.test.ts +166 -66
  42. package/src/integration/grep-scan-budget.test.ts +9 -9
  43. package/src/integration/grep-translation.test.ts +18 -32
  44. package/src/integration/large-text.test.ts +6 -52
  45. package/src/integration/model-budget-lifecycle.test.ts +10 -9
  46. package/src/integration/mutation-file-types.testing.ts +4 -4
  47. package/src/integration/npm-package.testing.ts +3 -3
  48. package/src/integration/publication-read-changes.test.ts +161 -83
  49. package/src/integration/read-changes.test.ts +4 -4
  50. package/src/integration/replace-worker-boundary.test.ts +22 -12
  51. package/src/integration/report-replay.test.ts +23 -26
  52. package/src/integration/utf8-errors.test.ts +4 -4
  53. package/src/pi/AGENTS.md +18 -17
  54. package/src/pi/action-fusion.test.ts +241 -353
  55. package/src/pi/action-fusion.testing.ts +38 -0
  56. package/src/pi/action-fusion.ts +150 -125
  57. package/src/pi/anchor-format.test.ts +9 -9
  58. package/src/pi/anchor-format.ts +6 -8
  59. package/src/pi/argument-diagnostics.ts +25 -28
  60. package/src/pi/argument-error.ts +2 -1
  61. package/src/pi/argument-format.test.ts +19 -18
  62. package/src/pi/argument-validation.test.ts +334 -283
  63. package/src/pi/argument-validation.ts +5 -8
  64. package/src/pi/budgets.ts +6 -8
  65. package/src/pi/codemode.test.ts +7 -2
  66. package/src/pi/config.test.ts +7 -3
  67. package/src/pi/config.ts +12 -9
  68. package/src/pi/diagnostic-buffer.test.ts +34 -13
  69. package/src/pi/diagnostic-buffer.ts +1 -2
  70. package/src/pi/directory-sync.test.ts +0 -1
  71. package/src/pi/edit-tool.ts +86 -211
  72. package/src/pi/error-text.ts +10 -9
  73. package/src/pi/execute.test.ts +249 -345
  74. package/src/pi/failure-context.test.ts +146 -127
  75. package/src/pi/failure-context.ts +45 -94
  76. package/src/pi/failure-path.test.ts +182 -155
  77. package/src/pi/file-commit.test.ts +156 -71
  78. package/src/pi/file-commit.ts +126 -146
  79. package/src/pi/file-snapshot.test.ts +3 -5
  80. package/src/pi/fusion-card.test.ts +26 -22
  81. package/src/pi/fusion-card.ts +16 -24
  82. package/src/pi/grep-candidates.test.ts +19 -120
  83. package/src/pi/grep-candidates.ts +25 -76
  84. package/src/pi/grep-matrix.test.ts +5 -5
  85. package/src/pi/grep-order.test.ts +16 -15
  86. package/src/pi/grep-output.ts +94 -107
  87. package/src/pi/grep-render.ts +1 -1
  88. package/src/pi/grep-reread.ts +97 -0
  89. package/src/pi/grep-rg.testing.ts +70 -0
  90. package/src/pi/grep-scope.ts +41 -43
  91. package/src/pi/grep-search.ts +27 -39
  92. package/src/pi/grep-tool.ts +63 -106
  93. package/src/pi/grep.test.ts +177 -156
  94. package/src/pi/model-budgets.test.ts +27 -24
  95. package/src/pi/model-budgets.ts +6 -38
  96. package/src/pi/mutation-outcome.testing.ts +0 -1
  97. package/src/pi/mutation-result.test.ts +44 -35
  98. package/src/pi/mutation-result.ts +52 -21
  99. package/src/pi/mutation-runner.ts +134 -74
  100. package/src/pi/output-budgets.test.ts +86 -42
  101. package/src/pi/path.ts +1 -1
  102. package/src/pi/pi.test.ts +18 -13
  103. package/src/pi/read-tool.ts +18 -23
  104. package/src/pi/read.test.ts +22 -41
  105. package/src/pi/regex-worker-contract.ts +3 -13
  106. package/src/pi/regex-worker.ts +1 -10
  107. package/src/pi/registration.test.ts +85 -51
  108. package/src/pi/render.test.ts +88 -59
  109. package/src/pi/render.ts +64 -65
  110. package/src/pi/replace-regex.ts +14 -2
  111. package/src/pi/replace-tool.ts +22 -94
  112. package/src/pi/replace-worker.ts +1 -1
  113. package/src/pi/replace.test.ts +53 -38
  114. package/src/pi/report-cause.ts +3 -8
  115. package/src/pi/report-failure.testing.ts +10 -3
  116. package/src/pi/report-schema.ts +251 -148
  117. package/src/pi/report.test.ts +215 -67
  118. package/src/pi/report.ts +50 -98
  119. package/src/pi/result-safety.test.ts +241 -247
  120. package/src/pi/rg-process.test.ts +77 -63
  121. package/src/pi/rg-process.ts +53 -75
  122. package/src/pi/rg-regex.ts +40 -400
  123. package/src/pi/schema.ts +35 -0
  124. package/src/pi/text-stream.test.ts +0 -21
  125. package/src/pi/text-stream.ts +14 -21
  126. package/src/pi/tool-factories.testing.ts +59 -0
  127. package/src/pi/write-tool.ts +36 -71
  128. package/src/pi/write.test.ts +31 -34
  129. package/src/testing/regex-translation-corpus.ts +12 -12
  130. package/src/testing/run-tests.ts +1 -1
  131. package/src/pi/replace-worker-contract.ts +0 -10
@@ -3,28 +3,37 @@ import { test } from "node:test";
3
3
  import { stripVTControlCharacters } from "node:util";
4
4
  import { initTheme } from "@earendil-works/pi-coding-agent";
5
5
  import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
6
+ import { HashlineError, PATH_ERROR_MESSAGES } from "../core/errors.ts";
7
+ import { FileMutationError } from "./file-commit.ts";
8
+ import { describeMutationFailure } from "./mutation-result.ts";
6
9
  import { formatArgumentError } from "./argument-error.ts";
7
10
  import { renderToolError, splitReport } from "./render.ts";
8
- import {
9
- causeOf,
10
- decodeReport,
11
- failureResult,
12
- reportOf,
13
- reportResult,
14
- describeFailure,
15
- renderReport,
16
- } from "./report.ts";
17
- import { DEFAULT_OUTPUT_BUDGETS, formatByteSize } from "./budgets.ts";
11
+ import { causeOf } from "./report-cause.ts";
12
+ import { reportOf, reportResult, describeFailure, renderReport, runTool } from "./report.ts";
13
+ import { formatByteSize } from "./budgets.ts";
14
+ import { DEFAULT_OUTPUT_BUDGETS } from "./tool-factories.testing.ts";
18
15
  import { MAX_UNNARROWED_SCAN_BYTES } from "./grep-candidates.ts";
19
- import { OUTCOME_NEXT, type Report, type ReportTool, type SuccessReport } from "./report-schema.ts";
16
+ import {
17
+ ERROR_DEFINITIONS,
18
+ OUTCOME_NEXT,
19
+ type Report,
20
+ type ReportTool,
21
+ type SuccessReport,
22
+ } from "./report-schema.ts";
20
23
 
21
24
  // npm run typecheck checks these invalid callers; they must never run.
22
- function invalidReportConstruction(report: NonNullable<ReturnType<typeof decodeReport>>) {
23
- // @ts-expect-error Grep successes cannot contain write-only facts.
24
- reportResult({ tool: "grep", created: true });
25
- // @ts-expect-error NO_MATCH requires its rule fact.
26
- failureResult({ tool: "replace", error: "NO_MATCH", message: "No replacement matched." });
25
+ function invalidReportConstruction(report: Report) {
26
+ // @ts-expect-error Removed facts cannot be supplied to a report.
27
+ const removedFact: Report = { tool: "grep", created: true };
28
+ // @ts-expect-error NO_MATCH requires its unmatched facts.
29
+ const failure: import("./report-schema.ts").FailureReport = {
30
+ tool: "replace",
31
+ error: "NO_MATCH",
32
+ message: "No replacement matched.",
33
+ };
27
34
  if (report.error === "NO_MATCH") {
35
+ const unmatched: string[] = report.unmatched;
36
+ // @ts-expect-error NO_MATCH no longer carries a single rule.
28
37
  const rule: number = report.rule;
29
38
  // @ts-expect-error NO_MATCH cannot carry a search-total fact.
30
39
  const matches: number = report.matches;
@@ -32,6 +41,8 @@ function invalidReportConstruction(report: NonNullable<ReturnType<typeof decodeR
32
41
  }
33
42
 
34
43
  const text = (value: string) => ({ type: "text" as const, text: value });
44
+ const reportFromText = (value: string, tool: ReportTool) =>
45
+ reportOf({ content: [text(value)] }, tool);
35
46
  function displayed(result: Parameters<typeof renderToolError>[0], tool: ReportTool) {
36
47
  initTheme("dark");
37
48
  return stripVTControlCharacters(
@@ -39,13 +50,14 @@ function displayed(result: Parameters<typeof renderToolError>[0], tool: ReportTo
39
50
  );
40
51
  }
41
52
 
42
- test("structured failure → omits call fields from text and renders the complete details once", () => {
43
- const result = failureResult({
44
- tool: "write",
45
- path: "target.txt",
46
- error: "TARGET_EXISTS",
47
- message: "Target already exists.",
48
- });
53
+ test("structured failure → omits call fields from text and renders the complete details once", async () => {
54
+ const result = await runTool(
55
+ "write",
56
+ { path: "target.txt", budget: DEFAULT_OUTPUT_BUDGETS },
57
+ async () => {
58
+ throw new HashlineError("TARGET_EXISTS", "Target already exists.");
59
+ },
60
+ );
49
61
  const before = structuredClone(result);
50
62
  const block = result.content[0];
51
63
  assert.ok(block.type === "text");
@@ -56,10 +68,11 @@ test("structured failure → omits call fields from text and renders the complet
56
68
  assert.equal(result.details.report.tool, "write");
57
69
  assert.equal(result.details.report.path, "target.txt");
58
70
  assert.equal(reportOf(result, "write"), result.details.report);
59
- assert.deepEqual(decodeReport(block.text, "write"), {
71
+ assert.deepEqual(reportFromText(block.text, "write"), {
60
72
  tool: "write",
61
73
  error: "TARGET_EXISTS",
62
74
  message: "Target already exists.",
75
+ next: ERROR_DEFINITIONS.TARGET_EXISTS.next.default,
63
76
  });
64
77
  const shown = displayed(result, "write");
65
78
  assert.match(shown, /TARGET_EXISTS/);
@@ -109,7 +122,7 @@ test("invalid serialized report → retains the raw diagnostic", () => {
109
122
  cause: { message: "native", foreign: true },
110
123
  }),
111
124
  ]) {
112
- assert.equal(decodeReport(invalid, "edit"), undefined, invalid);
125
+ assert.equal(reportFromText(invalid, "edit"), undefined, invalid);
113
126
  const report: unknown = JSON.parse(invalid);
114
127
  for (const details of [undefined, { report }]) {
115
128
  const result = { content: [text(invalid)], details };
@@ -132,7 +145,7 @@ test("native failure with deeply nested cause → retains the terminal diagnosti
132
145
  error = Object.assign(new Error(`layer ${index}`, { cause: error }), { code: "EIO" });
133
146
  const report = describeFailure("grep", error, { path: "source", budget: DEFAULT_OUTPUT_BUDGETS });
134
147
  const serialized = renderReport(report);
135
- const decoded = decodeReport(serialized, "grep");
148
+ const decoded = reportFromText(serialized, "grep");
136
149
  assert.ok(decoded);
137
150
  assert.deepEqual(decoded, { tool: "grep", ...JSON.parse(serialized) });
138
151
  let cause = decoded.cause;
@@ -142,19 +155,23 @@ test("native failure with deeply nested cause → retains the terminal diagnosti
142
155
  assert.doesNotMatch(serialized, /"omitted"/);
143
156
  });
144
157
 
145
- test("wide and repeated cause graph with a cycle → retains every distinct failure", () => {
146
- const repeated = new Error("repeated failure");
147
- const errors = Array.from({ length: 24 }, (_, index) => new Error(`failure ${index}`));
148
- const aggregate = new AggregateError([repeated, repeated, ...errors], "root");
149
- aggregate.cause = aggregate;
150
- const cause = causeOf(aggregate, DEFAULT_OUTPUT_BUDGETS.fieldBytes);
151
- assert.ok(cause);
152
- assert.equal(cause.cause, undefined);
153
- assert.deepEqual(
154
- cause.errors?.map((value) => value.message),
155
- [repeated.message, ...errors.map((error) => error.message)],
156
- );
157
- assert.doesNotMatch(JSON.stringify(cause), /"omitted"/);
158
+ test("aggregate and cyclic causes → preserve only the linear cause chain", () => {
159
+ const terminal = new Error("terminal");
160
+ const aggregate = new AggregateError([terminal, terminal, new Error("ignored")], "aggregate", {
161
+ cause: terminal,
162
+ });
163
+ const self = new Error("self");
164
+ self.cause = self;
165
+ const first = new Error("first");
166
+ const second = new Error("second", { cause: first });
167
+ first.cause = second;
168
+ for (const [input, expected] of [
169
+ [aggregate, { message: "aggregate", cause: { message: "terminal" } }],
170
+ [self, { message: "self" }],
171
+ [first, { message: "first", cause: { message: "second" } }],
172
+ ] as const) {
173
+ assert.deepEqual(causeOf(input, DEFAULT_OUTPUT_BUDGETS.fieldBytes), expected);
174
+ }
158
175
  });
159
176
 
160
177
  test("oversized cause message → retains opening and terminal text within the message budget", () => {
@@ -186,29 +203,77 @@ test("native filesystem failure → reports its diagnostic once without a synthe
186
203
  }
187
204
  });
188
205
 
206
+ test("native path errors → classify fixed messages and retain the missing path", () => {
207
+ for (const [code, expected, missing] of [
208
+ ["ENOENT", "PATH_NOT_FOUND", ["resolved/source"]],
209
+ ["EISDIR", "NOT_REGULAR_FILE", undefined],
210
+ ] as const) {
211
+ const native = Object.assign(new Error(`${code}: native diagnostic`), {
212
+ code,
213
+ path: "resolved/source",
214
+ });
215
+ const report = describeFailure("read", native, {
216
+ path: "source",
217
+ budget: DEFAULT_OUTPUT_BUDGETS,
218
+ });
219
+ assert.equal(report.error, expected);
220
+ assert.equal(report.message, PATH_ERROR_MESSAGES[expected]);
221
+ assert.deepEqual(report.missing, missing);
222
+ assert.equal(report.cause, undefined);
223
+ }
224
+ });
225
+
226
+ test("mutation publication → recovery reflects whether the change was saved", () => {
227
+ for (const [publication, expected] of [
228
+ ["NOT_PUBLISHED", undefined],
229
+ ["PUBLISHED", "The change is saved; do not repeat it. Read the file before further edits."],
230
+ [
231
+ "UNKNOWN",
232
+ "The change may or may not be saved. Read the file, and repeat the change only if it is missing.",
233
+ ],
234
+ ] as const) {
235
+ const error =
236
+ publication === "NOT_PUBLISHED"
237
+ ? new HashlineError("PUBLISH_FAILED", "failed")
238
+ : new FileMutationError(
239
+ publication === "PUBLISHED" ? "POST_PROCESS_FAILED" : "PUBLISH_FAILED",
240
+ publication,
241
+ "failed",
242
+ );
243
+ const report = describeMutationFailure("edit", error, { budget: DEFAULT_OUTPUT_BUDGETS });
244
+ assert.equal(report.publication, publication);
245
+ assert.equal(report.next, expected);
246
+ assert.equal(reportFromText(renderReport(report), "edit")?.next, expected);
247
+ }
248
+ });
249
+
189
250
  test("success reports → omit repeated call fields while retaining details and payload", () => {
190
251
  const report: SuccessReport = {
191
252
  tool: "write",
192
253
  path: "target.txt",
193
254
  publication: "PUBLISHED",
194
- created: true,
195
255
  };
196
256
  const payload = [text("payload")];
197
257
  const result = reportResult(report, payload);
198
258
  const last = result.content.at(-1);
199
259
  assert.ok(last?.type === "text");
200
- assert.deepEqual(JSON.parse(last.text), { publication: "PUBLISHED", created: true });
260
+ assert.deepEqual(JSON.parse(last.text), { publication: "PUBLISHED" });
201
261
  assert.deepEqual(result.details.report, report);
202
- assert.deepEqual(splitReport(result), { content: payload, report });
262
+ assert.deepEqual(splitReport(result, "write"), { content: payload, report });
203
263
  assert.equal(result.content.length, 2);
204
264
  });
205
265
 
206
266
  test("success facts without a tool → use call context and enforce tool-specific fields", () => {
207
- const report: Report = { tool: "write", publication: "PUBLISHED", created: true };
267
+ const report: Report = { tool: "write", publication: "PUBLISHED" };
208
268
  const serialized = renderReport(report);
209
- assert.deepEqual(decodeReport(serialized, "write"), report);
210
- for (const tool of ["read", "grep", "edit", "replace"] as const)
211
- assert.equal(decodeReport(serialized, tool), undefined);
269
+ assert.deepEqual(reportFromText(serialized, "write"), {
270
+ tool: "write",
271
+ publication: "PUBLISHED",
272
+ });
273
+ for (const tool of ["read", "grep"] as const)
274
+ assert.equal(reportFromText(serialized, tool), undefined);
275
+ for (const tool of ["edit", "replace"] as const)
276
+ assert.deepEqual(reportFromText(serialized, tool), { tool, publication: "PUBLISHED" });
212
277
  for (const invalid of [
213
278
  '{"created":true,"foreign":true}',
214
279
  '{"error":"INVALID_ARGUMENTS","message":"missing issues"}',
@@ -216,7 +281,13 @@ test("success facts without a tool → use call context and enforce tool-specifi
216
281
  "[]",
217
282
  "null",
218
283
  ])
219
- assert.equal(decodeReport(invalid, "write"), undefined);
284
+ assert.equal(reportFromText(invalid, "write"), undefined);
285
+ const foreign: Report = { tool: "grep", matches: 1 };
286
+ const foreignText = JSON.stringify(foreign);
287
+ const hooked = { content: [text(foreignText)], details: { report: foreign } };
288
+ assert.equal(reportFromText(foreignText, "read"), undefined);
289
+ assert.equal(reportOf(hooked, "read"), undefined);
290
+ assert.deepEqual(splitReport(hooked, "read"), { content: hooked.content, report: undefined });
220
291
  });
221
292
 
222
293
  test("checked report with a different trailing block → preserves the entire payload", () => {
@@ -229,8 +300,8 @@ test("checked report with a different trailing block → preserves the entire pa
229
300
  ]) {
230
301
  const content = [text("saved payload"), last];
231
302
  const result: Parameters<typeof splitReport>[0] = { content, details: { report } };
232
- assert.deepEqual(splitReport(result), { content, report });
233
- assert.equal(splitReport(result).content, content);
303
+ assert.deepEqual(splitReport(result, "write"), { content, report });
304
+ assert.equal(splitReport(result, "write").content, content);
234
305
  }
235
306
  });
236
307
 
@@ -246,28 +317,44 @@ test("report-contract: native failure with an independent cause → retains the
246
317
  assert.equal(report.cause?.code, "EIO");
247
318
  });
248
319
 
249
- test("grep success and incomplete candidate facts → survive report serialization", () => {
320
+ test("replacement failure facts → survive report serialization", () => {
250
321
  for (const report of [
251
- { tool: "grep", matches: 1 },
252
- { tool: "grep", matches: 1, candidates: "files" },
253
322
  {
254
- tool: "grep",
255
- matches: 0,
256
- candidates: "all",
257
- scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
323
+ tool: "replace",
324
+ error: "NO_MATCH",
325
+ message: "No match",
326
+ unmatched: ["replacements[0]", "replacements[2]"],
327
+ },
328
+ {
329
+ tool: "replace",
330
+ error: "OVERLAPPING_MATCHES",
331
+ message: "Overlap",
332
+ conflict: ["replacements[0]", "replacements[1]"],
333
+ line: 2,
258
334
  },
335
+ ] satisfies Report[]) {
336
+ assert.deepEqual(reportFromText(renderReport(report), "replace"), report);
337
+ }
338
+ });
339
+
340
+ test("success facts survive details and model serialization", () => {
341
+ for (const report of [
342
+ { tool: "grep", matches: 1 },
343
+ { tool: "grep", matches: 0, scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES) },
259
344
  {
260
345
  tool: "grep",
261
346
  error: "SEARCH_INCOMPLETE",
262
347
  message: "Search incomplete.",
263
348
  diagnostics: "unreadable file",
264
- candidates: "all",
265
349
  scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
266
350
  },
267
351
  ] satisfies Report[]) {
268
352
  const serialized = renderReport(report);
269
- assert.deepEqual(decodeReport(serialized, report.tool), report);
270
- assert.deepEqual(reportOf({ content: [text(serialized)] }, report.tool), report);
353
+ assert.deepEqual(reportFromText(serialized, report.tool), report);
354
+ assert.deepEqual(
355
+ reportOf({ content: [text(serialized)], details: { report } }, report.tool),
356
+ report,
357
+ );
271
358
  }
272
359
  });
273
360
 
@@ -278,12 +365,40 @@ test("match and scan limits → recovery prioritizes the match limit", () => {
278
365
  tool: "grep",
279
366
  matches: 1,
280
367
  matchLimit: 1,
281
- candidates: "all",
282
368
  scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
283
369
  });
284
370
  assert.equal(reportOf(result, "grep")?.next, next);
285
371
  });
286
372
 
373
+ for (const { name, report, fact } of [
374
+ {
375
+ name: "output limit with grep match limit → recovery prioritizes the output limit",
376
+ report: {
377
+ tool: "grep",
378
+ matches: 1,
379
+ matchLimit: 1,
380
+ outputLimit: formatByteSize(DEFAULT_OUTPUT_BUDGETS.grepBytes),
381
+ },
382
+ fact: "outputLimit",
383
+ },
384
+ {
385
+ name: "output limit with read next offset → recovery prioritizes the next offset",
386
+ report: {
387
+ tool: "read",
388
+ nextOffset: 2,
389
+ outputLimit: formatByteSize(DEFAULT_OUTPUT_BUDGETS.readBytes),
390
+ },
391
+ fact: "nextOffset",
392
+ },
393
+ ] satisfies { name: string; report: SuccessReport; fact: "outputLimit" | "nextOffset" }[]) {
394
+ test(name, () => {
395
+ const next = OUTCOME_NEXT.find(([key]) => key === fact)?.[1];
396
+ assert.ok(next);
397
+ const result = reportResult(report);
398
+ assert.equal(reportOf(result, report.tool)?.next, next);
399
+ });
400
+ }
401
+
287
402
  test("text-only error contains facts invalid for the originating tool → preserves the raw diagnostic", () => {
288
403
  const diagnostic = '{"publication":"PUBLISHED"}';
289
404
  const result = { content: [text(diagnostic)] };
@@ -298,20 +413,18 @@ test("invalid details report → falls back to valid text and leaves content int
298
413
  const last = expected ? renderReport(expected) : JSON.stringify({ matches: "x" }, null, 2);
299
414
  const result = { details, content: [text("payload"), text(last)] };
300
415
  assert.deepEqual(reportOf(result, "grep"), expected);
301
- assert.deepEqual(splitReport(result), { content: result.content, report: undefined });
416
+ assert.deepEqual(splitReport(result, "grep"), { content: result.content, report: undefined });
302
417
  assert.equal(result.content.length, 2);
303
418
  }
304
419
  });
305
420
 
306
421
  test("output byte limit → follows the scan limit and precedes the line preview limit", () => {
307
422
  const serialized = renderReport({
308
- candidates: "all",
309
423
  scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
310
424
  outputLimit: formatByteSize(DEFAULT_OUTPUT_BUDGETS.grepBytes),
311
425
  linePreviewLimit: DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits,
312
426
  });
313
427
  assert.deepEqual(Object.keys(JSON.parse(serialized)), [
314
- "candidates",
315
428
  "scanLimit",
316
429
  "outputLimit",
317
430
  "linePreviewLimit",
@@ -329,15 +442,50 @@ test("hooked details contain a cyclic report cause → preserves payload and dec
329
442
  const content = [text(renderReport(fallback))];
330
443
  const result = { details: { report: { ...fallback, cause } }, content };
331
444
  assert.deepEqual(reportOf(result, "read"), fallback);
332
- assert.deepEqual(splitReport(result), { content, report: undefined });
445
+ assert.deepEqual(splitReport(result, "read"), { content, report: undefined });
333
446
  });
334
447
 
335
- test("native mutation failure before commit → retains publication and stage", () => {
448
+ test("native mutation failure before commit → retains publication", () => {
336
449
  for (const tool of ["edit", "replace", "write"] as const) {
337
- const report = describeFailure(tool, new Error("native failure"), {
450
+ const report = describeMutationFailure(tool, new Error("native failure"), {
338
451
  budget: DEFAULT_OUTPUT_BUDGETS,
339
452
  });
340
453
  assert.equal(report.publication, "NOT_PUBLISHED", tool);
341
- assert.equal(report.stage, "prepare", tool);
454
+ }
455
+ });
456
+
457
+ test("failure classification without mutation facts → omits mutation state for every tool", () => {
458
+ for (const tool of ["read", "grep", "edit", "replace", "write"] as const) {
459
+ const error =
460
+ tool === "read" || tool === "grep"
461
+ ? new Error("native failure")
462
+ : new FileMutationError("POST_PROCESS_FAILED", "PUBLISHED", "result generation failed");
463
+ const report = describeFailure(tool, error, { budget: DEFAULT_OUTPUT_BUDGETS });
464
+ assert.equal(
465
+ report.error,
466
+ error instanceof FileMutationError ? "POST_PROCESS_FAILED" : "UNCLASSIFIED",
467
+ );
468
+ assert.equal(report.publication, undefined);
469
+ assert.equal(report.then_run, undefined);
470
+ assert.equal(report.progressError, undefined);
471
+ assert.equal(report.next, undefined);
472
+ const serialized: unknown = JSON.parse(renderReport(report));
473
+ assert.ok(typeof serialized === "object" && serialized !== null);
474
+ for (const field of ["publication", "then_run", "progressError", "next"])
475
+ assert.equal(Object.hasOwn(serialized, field), false);
476
+ }
477
+ });
478
+
479
+ test("mutation freshness → retains the fact and requests reread only for an existing or unknown target", () => {
480
+ for (const freshness of ["missing", "changed", "unknown"] as const) {
481
+ const result = reportResult({ tool: "edit", publication: "PUBLISHED", freshness });
482
+ const report = reportOf(result, "edit");
483
+ assert.equal(report?.freshness, freshness);
484
+ if (freshness === "missing") assert.equal(report?.next, undefined);
485
+ else assert.match(report?.next ?? "", /read.*before.*edit|reread/i);
486
+ const block = result.content.at(-1);
487
+ assert.ok(block?.type === "text");
488
+ assert.deepEqual(reportFromText(block.text, "edit"), report);
489
+ assert.equal(Object.hasOwn(JSON.parse(block.text), "next"), freshness !== "missing");
342
490
  }
343
491
  });