@criogaid/pi-hashline-edit 0.8.0 → 0.10.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 +13 -10
  2. package/README.zh-CN.md +12 -9
  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 +118 -293
  7. package/src/core/apply.ts +63 -165
  8. package/src/core/case-fold.ts +8 -67
  9. package/src/core/character-set.ts +4 -4
  10. package/src/core/errors.test.ts +23 -7
  11. package/src/core/errors.ts +32 -5
  12. package/src/core/hash.test.ts +8 -16
  13. package/src/core/hash.ts +3 -23
  14. package/src/core/lines.ts +2 -4
  15. package/src/core/ranges.test.ts +16 -8
  16. package/src/core/ranges.ts +10 -6
  17. package/src/core/regex.ts +3 -4
  18. package/src/core/replace.test.ts +47 -1
  19. package/src/core/replace.ts +19 -13
  20. package/src/core/search-analysis.test.ts +24 -27
  21. package/src/core/search-analysis.ts +34 -65
  22. package/src/core/search-matrix.testing.ts +0 -2
  23. package/src/core/search.ts +1 -1
  24. package/src/core/text.test.ts +5 -5
  25. package/src/core/text.ts +5 -12
  26. package/src/core/types.ts +7 -43
  27. package/src/integration/config-registration.test.ts +2 -2
  28. package/src/integration/fusion-dispatch.test.ts +2 -5
  29. package/src/integration/grep-flags.test.ts +0 -14
  30. package/src/integration/grep-js-completeness.test.ts +34 -8
  31. package/src/integration/grep-line-candidates.test.ts +4 -8
  32. package/src/integration/grep-matrix.test.ts +3 -7
  33. package/src/integration/grep-path-matrix.test.ts +5 -5
  34. package/src/integration/grep-prefilter-coverage.test.ts +3 -7
  35. package/src/integration/grep-process-limits.test.ts +78 -0
  36. package/src/integration/grep-rg.test.ts +123 -25
  37. package/src/integration/grep-scan-budget.test.ts +2 -2
  38. package/src/integration/grep-translation.test.ts +12 -27
  39. package/src/integration/large-text.test.ts +1 -47
  40. package/src/integration/model-budget-lifecycle.test.ts +13 -3
  41. package/src/integration/mutation-file-types.testing.ts +1 -1
  42. package/src/integration/npm-package.testing.ts +1 -1
  43. package/src/integration/publication-read-changes.test.ts +125 -47
  44. package/src/integration/replace-worker-boundary.test.ts +21 -11
  45. package/src/integration/report-replay.test.ts +21 -23
  46. package/src/integration/utf8-errors.test.ts +1 -1
  47. package/src/pi/AGENTS.md +16 -16
  48. package/src/pi/action-fusion.test.ts +168 -300
  49. package/src/pi/action-fusion.ts +148 -111
  50. package/src/pi/anchor-format.test.ts +9 -9
  51. package/src/pi/anchor-format.ts +6 -8
  52. package/src/pi/argument-diagnostics.ts +52 -207
  53. package/src/pi/argument-error.ts +2 -1
  54. package/src/pi/argument-format.test.ts +20 -22
  55. package/src/pi/argument-validation.test.ts +382 -418
  56. package/src/pi/argument-validation.ts +5 -8
  57. package/src/pi/budgets.ts +5 -4
  58. package/src/pi/config.test.ts +5 -2
  59. package/src/pi/config.ts +4 -6
  60. package/src/pi/diagnostic-buffer.test.ts +12 -1
  61. package/src/pi/diagnostic-buffer.ts +1 -2
  62. package/src/pi/directory-sync.test.ts +0 -1
  63. package/src/pi/edit-tool.ts +173 -226
  64. package/src/pi/error-text.ts +4 -8
  65. package/src/pi/execute.test.ts +165 -266
  66. package/src/pi/failure-context.test.ts +144 -126
  67. package/src/pi/failure-context.ts +45 -94
  68. package/src/pi/failure-path.test.ts +168 -141
  69. package/src/pi/file-commit.test.ts +110 -53
  70. package/src/pi/file-commit.ts +90 -137
  71. package/src/pi/file-snapshot.test.ts +3 -5
  72. package/src/pi/fusion-card.test.ts +4 -1
  73. package/src/pi/fusion-card.ts +16 -24
  74. package/src/pi/grep-candidates.test.ts +2 -101
  75. package/src/pi/grep-candidates.ts +23 -69
  76. package/src/pi/grep-matrix.test.ts +2 -2
  77. package/src/pi/grep-order.test.ts +4 -4
  78. package/src/pi/grep-output.ts +62 -96
  79. package/src/pi/grep-render.ts +1 -1
  80. package/src/pi/grep-reread.ts +92 -0
  81. package/src/pi/grep-scope.ts +40 -42
  82. package/src/pi/grep-search.ts +22 -31
  83. package/src/pi/grep-tool.ts +55 -82
  84. package/src/pi/grep.test.ts +81 -46
  85. package/src/pi/model-budgets.test.ts +25 -23
  86. package/src/pi/model-budgets.ts +6 -38
  87. package/src/pi/mutation-result.test.ts +44 -35
  88. package/src/pi/mutation-result.ts +51 -20
  89. package/src/pi/mutation-runner.ts +126 -70
  90. package/src/pi/output-budgets.test.ts +38 -16
  91. package/src/pi/path.ts +1 -1
  92. package/src/pi/pi.test.ts +3 -1
  93. package/src/pi/read-tool.ts +5 -5
  94. package/src/pi/regex-worker-contract.ts +3 -13
  95. package/src/pi/regex-worker.ts +1 -10
  96. package/src/pi/registration.test.ts +82 -20
  97. package/src/pi/render.test.ts +79 -42
  98. package/src/pi/render.ts +69 -63
  99. package/src/pi/replace-regex.ts +14 -2
  100. package/src/pi/replace-tool.ts +22 -83
  101. package/src/pi/replace-worker.ts +1 -1
  102. package/src/pi/replace.test.ts +9 -5
  103. package/src/pi/report-cause.ts +3 -8
  104. package/src/pi/report-failure.testing.ts +1 -2
  105. package/src/pi/report-schema.ts +65 -81
  106. package/src/pi/report.test.ts +171 -25
  107. package/src/pi/report.ts +33 -87
  108. package/src/pi/result-safety.test.ts +177 -189
  109. package/src/pi/rg-process.test.ts +77 -63
  110. package/src/pi/rg-process.ts +48 -64
  111. package/src/pi/rg-regex.ts +40 -400
  112. package/src/pi/schema.ts +12 -0
  113. package/src/pi/text-stream.test.ts +0 -21
  114. package/src/pi/text-stream.ts +14 -21
  115. package/src/pi/write-tool.ts +33 -62
  116. package/src/pi/write.test.ts +1 -3
  117. package/src/testing/regex-translation-corpus.ts +12 -12
  118. package/src/pi/replace-worker-contract.ts +0 -10
@@ -2,20 +2,18 @@ import { rejectsArgument } from "./argument-error.testing.ts";
2
2
  import { computeLineHash } from "../core/hash.ts";
3
3
  import type { AgentToolResult } from "@earendil-works/pi-agent-core";
4
4
  import assert from "node:assert/strict";
5
- import { mkdtemp, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
5
+ import fs, { mkdtemp, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
6
+ import { syncBuiltinESMExports } from "node:module";
6
7
  import { writeFileSync } from "node:fs";
7
8
  import { tmpdir } from "node:os";
8
9
  import { join } from "node:path";
9
10
  import { test } from "node:test";
10
- import { DEFAULT_OUTPUT_BUDGETS } from "./budgets.ts";
11
+ import { outputBudgetsFor } from "./budgets.ts";
11
12
  import { createActionFusionExecutor } from "./action-fusion.ts";
12
13
  import { makeReplaceTool } from "./replace-tool.ts";
13
14
  import { makeWriteOverride } from "./write-tool.ts";
14
15
  import { makeEditOverride } from "./edit-tool.ts";
15
- import { commitFreshness, FileMutationError } from "./file-commit.ts";
16
- import { byteRevision } from "./file-revision.ts";
17
- import { finalizeMutation, postProcessMutation, type MutationOutcome } from "./mutation-result.ts";
18
- import { CommandNotRun, decodeReport, reportOf } from "./report.ts";
16
+ import { decodeReport, reportOf } from "./report.ts";
19
17
  import { REREAD } from "./report-schema.ts";
20
18
  import { callTool, createToolContext as ctx } from "./tool-call.testing.ts";
21
19
  import { DEFAULT_CONFIG } from "./config.ts";
@@ -23,32 +21,26 @@ import { DEFAULT_CONFIG } from "./config.ts";
23
21
  const text = (result: Pick<AgentToolResult<unknown>, "content">): string =>
24
22
  result.content.map((block) => (block.type === "text" ? block.text : "")).join("\n");
25
23
 
26
- const invoke = (
27
- tool: any,
28
- toolCallId: string,
29
- args: unknown,
30
- signal?: AbortSignal,
31
- onUpdate?: unknown,
32
- context?: unknown,
33
- ) => callTool(tool, args, { toolCallId, signal, onUpdate, ctx: context });
24
+ const anchorBudget = outputBudgetsFor(4096);
25
+ const anchorBatchSize: unknown = Reflect.get(
26
+ makeEditOverride(process.cwd(), DEFAULT_CONFIG).parameters.properties.edits,
27
+ "maxItems",
28
+ );
29
+ assert.ok(typeof anchorBatchSize === "number");
34
30
 
35
31
  function assertFailureByteBudgets(message: string): void {
36
32
  const report = decodeReport(message, "edit");
37
33
  assert.ok(report);
38
34
  assert.equal(report.error, "ANCHOR_MISMATCH");
39
35
  assert.ok(report.failures && report.failures.length > 0);
40
- assert.ok(
41
- Buffer.byteLength(JSON.stringify(report.failures)) <= DEFAULT_OUTPUT_BUDGETS.blockBytes,
42
- );
43
- assert.equal(report.failures.length + (report.omittedFailures ?? 0), 1000);
36
+ assert.ok(Buffer.byteLength(JSON.stringify(report.failures)) <= anchorBudget.blockBytes);
37
+ assert.equal(report.failures.length + (report.omittedFailures ?? 0), anchorBatchSize);
44
38
  assert.ok(report.omittedFailures);
45
- assert.equal(report.matched, undefined);
46
- assert.equal(report.omittedMatched, undefined);
47
39
  assert.ok(report.next?.includes("intended target"));
48
40
  if (report.candidateNeighborhoods) {
49
41
  const rows = report.candidateNeighborhoods.flatMap((group) => group.rows);
50
42
  assert.ok(rows.length > 0);
51
- assert.ok(Buffer.byteLength(rows.join("\n")) + 1 <= DEFAULT_OUTPUT_BUDGETS.blockBytes);
43
+ assert.ok(Buffer.byteLength(rows.join("\n")) + 1 <= anchorBudget.blockBytes);
52
44
  }
53
45
  }
54
46
 
@@ -62,18 +54,16 @@ test("edit and replace commands change or remove the file → withhold anchors a
62
54
  const path = join(dir, `${toolName}-${state}-${fails}.txt`);
63
55
  await writeFile(path, "before\n");
64
56
  const updates: AgentToolResult<unknown>[] = [];
65
- const fusion = createActionFusionExecutor(
66
- async (_id, _input, _signal, _ctx, onUpdate) => {
67
- if (state === "changed") await writeFile(path, "command output\n");
68
- else {
69
- await rm(path);
70
- if (state === "unknown") await mkdir(path);
71
- }
72
- onUpdate?.({ content: [{ type: "text", text: "progress" }], details: undefined });
73
- if (fails) throw new Error("command failed");
74
- return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
75
- },
76
- );
57
+ const fusion = createActionFusionExecutor(async (_input, _signal, _ctx, onUpdate) => {
58
+ if (state === "changed") await writeFile(path, "command output\n");
59
+ else {
60
+ await rm(path);
61
+ if (state === "unknown") await mkdir(path);
62
+ }
63
+ onUpdate?.({ content: [{ type: "text", text: "progress" }], details: undefined });
64
+ if (fails) throw new Error("command failed");
65
+ return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
66
+ });
77
67
  const tool =
78
68
  toolName === "edit"
79
69
  ? makeEditOverride(dir, DEFAULT_CONFIG, fusion)
@@ -83,7 +73,7 @@ test("edit and replace commands change or remove the file → withhold anchors a
83
73
  ? {
84
74
  edits: [
85
75
  {
86
- op: "replace",
76
+ action: "replace",
87
77
  anchor: `1#${computeLineHash(1, "before", DEFAULT_CONFIG.hashLen)}`,
88
78
  body: ["after"],
89
79
  },
@@ -102,8 +92,9 @@ test("edit and replace commands change or remove the file → withhold anchors a
102
92
  const report = reportOf(result, toolName);
103
93
  assert.equal(report?.freshness, state);
104
94
  assert.equal(report?.publication, "PUBLISHED");
105
- assert.equal(report?.next, REREAD);
95
+ assert.equal(report?.next, state === "missing" ? undefined : REREAD);
106
96
  assert.equal(report?.then_run?.status, fails ? "failed" : "succeeded");
97
+ if (fails) assert.equal(report?.then_run?.output, "command failed");
107
98
  assert.equal(result.content.length, 1);
108
99
  assert.ok(updates.length > 0);
109
100
  for (const update of updates)
@@ -119,7 +110,7 @@ test("edit and replace commands change or remove the file → withhold anchors a
119
110
  }
120
111
  });
121
112
 
122
- test("progress callback failures preserve publication and do not prevent the command", async (t) => {
113
+ test("progress callback failures → preserve publication and allow the command", async (t) => {
123
114
  const dir = await mkdtemp(join(tmpdir(), "hashline-progress-"));
124
115
  try {
125
116
  for (const callback of ["reporter", "update"]) {
@@ -135,18 +126,15 @@ test("progress callback failures preserve publication and do not prevent the com
135
126
  callback === "reporter" ? fail : undefined,
136
127
  );
137
128
  const path = join(dir, callback);
138
- const result = await invoke(
129
+ const result = await callTool(
139
130
  makeWriteOverride(dir, fusion),
140
- "write",
141
131
  {
142
132
  path,
143
133
  content: "saved\n",
144
134
  mode: "create",
145
135
  then_run: { command: "check" },
146
136
  },
147
- undefined,
148
- callback === "update" ? fail : undefined,
149
- await ctx(dir, t),
137
+ { onUpdate: callback === "update" ? fail : undefined, ctx: await ctx(dir, t) },
150
138
  );
151
139
  assert.equal(commands, 1);
152
140
  assert.equal(await readFile(path, "utf8"), "saved\n");
@@ -154,110 +142,6 @@ test("progress callback failures preserve publication and do not prevent the com
154
142
  assert.equal(reportOf(result, "write")?.then_run?.status, "succeeded");
155
143
  assert.match(text(result), /display unavailable/);
156
144
  }
157
- const fusion = createActionFusionExecutor(undefined, () => {
158
- throw new Error("display unavailable");
159
- });
160
- await assert.rejects(
161
- fusion({
162
- budget: DEFAULT_OUTPUT_BUDGETS,
163
- toolCallId: "failed",
164
- absolutePath: join(dir, "failed"),
165
- tool: "write",
166
- displayPath: "failed",
167
- thenRun: { command: "check" },
168
- mutate: async () => {
169
- throw new FileMutationError(
170
- "POST_PROCESS_FAILED",
171
- "post_process",
172
- "PUBLISHED",
173
- "original failure",
174
- );
175
- },
176
- signal: undefined,
177
- ctx: await ctx(dir, t),
178
- }),
179
- (error: unknown) =>
180
- error instanceof CommandNotRun &&
181
- error.cause instanceof FileMutationError &&
182
- error.cause.publication === "PUBLISHED" &&
183
- /original failure/.test(error.cause.message),
184
- );
185
- } finally {
186
- await rm(dir, { recursive: true, force: true });
187
- }
188
- });
189
-
190
- test("standalone and Fusion finalizers → take publication and freshness from commit facts, independent of diff", async (t) => {
191
- const dir = await mkdtemp(join(tmpdir(), "hashline-finalizer-"));
192
- try {
193
- const path = join(dir, "file");
194
- const context = await ctx(dir, t);
195
- const publishedRevision = byteRevision(Buffer.from("published\n"));
196
- for (const publication of ["PUBLISHED", "NOT_PUBLISHED"] as const) {
197
- for (const content of ["published\n", "external\n"]) {
198
- await writeFile(path, content);
199
- for (const details of [{}, { diff: "" }]) {
200
- const expectedDetails = structuredClone(details);
201
- const mutation = {
202
- details,
203
- facts: {},
204
- commit: {
205
- publication,
206
- publishedRevision,
207
- observedRevision: byteRevision(Buffer.from(content)),
208
- created: false,
209
- },
210
- } satisfies MutationOutcome<object>;
211
- const fresh = content === "published\n";
212
- const observation = commitFreshness(mutation.commit);
213
- assert.equal(observation.freshness, fresh ? "unchanged" : "changed");
214
- const standalone = finalizeMutation(
215
- "edit",
216
- path,
217
- mutation,
218
- observation,
219
- undefined,
220
- DEFAULT_OUTPUT_BUDGETS,
221
- );
222
- assert.equal(reportOf(standalone, "edit")?.publication, publication);
223
- assert.equal(reportOf(standalone, "edit")?.freshness, fresh ? undefined : "changed");
224
- for (const thenRun of [undefined, { command: "check" }]) {
225
- let commands = 0;
226
- const fused = await createActionFusionExecutor(async () => {
227
- commands++;
228
- return {
229
- status: "succeeded",
230
- result: { content: [{ type: "text", text: "checked" }] },
231
- };
232
- })({
233
- budget: DEFAULT_OUTPUT_BUDGETS,
234
- tool: "edit",
235
- displayPath: path,
236
- toolCallId: "commit-facts",
237
- absolutePath: path,
238
- thenRun,
239
- mutate: async () => mutation,
240
- signal: undefined,
241
- ctx: context,
242
- });
243
- if (!thenRun) assert.deepEqual(fused.content, standalone.content);
244
- assert.equal(fused.content.length, 1);
245
- const report = reportOf(fused, "edit");
246
- assert.equal(report?.publication, publication);
247
- assert.equal(report?.freshness, fresh ? undefined : "changed");
248
- assert.equal(
249
- report?.then_run?.status,
250
- thenRun ? (fresh ? "succeeded" : "skipped") : undefined,
251
- );
252
- assert.equal(commands, thenRun && fresh ? 1 : 0);
253
- for (const [key, value] of Object.entries(expectedDetails))
254
- assert.deepEqual(Reflect.get(fused.details, key), value);
255
- assert.equal(await readFile(path, "utf8"), content);
256
- }
257
- assert.deepEqual(mutation.details, expectedDetails);
258
- }
259
- }
260
- }
261
145
  } finally {
262
146
  await rm(dir, { recursive: true, force: true });
263
147
  }
@@ -268,13 +152,16 @@ test("aggregate anchor diagnostics → respect their byte budgets", async (t) =>
268
152
  try {
269
153
  const path = join(dir, "long.txt");
270
154
  await writeFile(path, "current\n");
271
- const failure = await invoke(
272
- makeEditOverride(dir, DEFAULT_CONFIG),
273
- "errors",
274
- { path, edits: Array.from({ length: 1000 }, () => ({ op: "delete", anchor: "1#XXXX" })) },
275
- undefined,
276
- undefined,
277
- await ctx(dir, t),
155
+ const failure = await callTool(
156
+ makeEditOverride(dir, DEFAULT_CONFIG, undefined, () => anchorBudget),
157
+ {
158
+ path,
159
+ edits: Array.from({ length: anchorBatchSize }, () => ({
160
+ action: "delete",
161
+ anchor: "1#XXXX",
162
+ })),
163
+ },
164
+ { ctx: await ctx(dir, t) },
278
165
  );
279
166
  assert.equal(failure.isError, true);
280
167
  assertFailureByteBudgets(text(failure));
@@ -300,7 +187,11 @@ test("unpaired surrogate arguments are rejected before Fusion", async (t) => {
300
187
  const cases = [
301
188
  {
302
189
  tool: makeEditOverride(dir, DEFAULT_CONFIG, fusion),
303
- args: { path, edits: [{ op: "append", body: ["\ud800"] }], then_run: { command: "check" } },
190
+ args: {
191
+ path,
192
+ edits: [{ action: "append", body: ["\ud800"] }],
193
+ then_run: { command: "check" },
194
+ },
304
195
  field: "edits[0].body[0]",
305
196
  },
306
197
  {
@@ -342,19 +233,16 @@ test("ambiguous recovery bounds candidate lists and never claims content identit
342
233
  const lines = Array.from({ length: 100 }, () => "same");
343
234
  lines[49] = "different";
344
235
  await writeFile(path, lines.join("\n"));
345
- const failure = await invoke(
346
- makeEditOverride(dir, DEFAULT_CONFIG),
347
- "ambiguous",
236
+ const failure = await callTool(
237
+ makeEditOverride(dir, DEFAULT_CONFIG, undefined, () => anchorBudget),
348
238
  {
349
239
  path,
350
- edits: Array.from({ length: 1000 }, () => ({
351
- op: "delete",
240
+ edits: Array.from({ length: anchorBatchSize }, () => ({
241
+ action: "delete",
352
242
  anchor: `50#${computeLineHash(50, "same", 4)}`,
353
243
  })),
354
244
  },
355
- undefined,
356
- undefined,
357
- await ctx(dir, t),
245
+ { ctx: await ctx(dir, t) },
358
246
  );
359
247
  assert.equal(failure.isError, true);
360
248
  assertFailureByteBudgets(text(failure));
@@ -385,7 +273,7 @@ test("NUL arguments are rejected before Fusion for all mutation tools", async (t
385
273
  const cases = [
386
274
  {
387
275
  tool: makeEditOverride(dir, DEFAULT_CONFIG, fusion),
388
- args: { path, edits: [{ op: "append", body: ["\0"] }], then_run: { command: "check" } },
276
+ args: { path, edits: [{ action: "append", body: ["\0"] }], then_run: { command: "check" } },
389
277
  field: "edits[0].body[0]",
390
278
  },
391
279
  {
@@ -456,20 +344,69 @@ test("Fusion skips the command when a replacement produces unencodable text", as
456
344
  }
457
345
  });
458
346
 
459
- test("result generation failure → preserves publication and cause", () => {
460
- for (const publication of ["NOT_PUBLISHED", "PUBLISHED", "UNKNOWN"] as const) {
461
- const cause = new Error("render failed");
462
- assert.throws(
463
- () =>
464
- postProcessMutation(publication, () => {
465
- throw cause;
466
- }),
467
- (error: any) =>
468
- error instanceof FileMutationError &&
469
- error.stage === "post_process" &&
470
- error.publication === publication &&
471
- error.cause === cause,
472
- );
347
+ test("result generation fails after a real mutation → preserves publication and cause", async (t) => {
348
+ const dir = await mkdtemp(join(tmpdir(), "hashline-result-generation-"));
349
+ try {
350
+ for (const content of ["before\n", "after\n"]) {
351
+ const path = join(dir, "file.txt");
352
+ await writeFile(path, "before\n");
353
+ const cause = new Error("render failed");
354
+ const context = await ctx(dir, t);
355
+ const nativeOpen = fs.open;
356
+ const nativeReplace = String.prototype.replace;
357
+ const readsBeforeDiff = content === "before\n" ? 2 : 3;
358
+ let targetReads = 0;
359
+ let injected = false;
360
+ let commands = 0;
361
+ const fusion = createActionFusionExecutor(async () => {
362
+ commands++;
363
+ return { status: "succeeded", result: { content: [] } };
364
+ });
365
+ try {
366
+ t.mock.method(fs, "open", async (...args: Parameters<typeof nativeOpen>) => {
367
+ if (String(args[0]) === path) targetReads++;
368
+ return nativeOpen(...args);
369
+ });
370
+ syncBuiltinESMExports();
371
+ // Only fail diff normalization after the commit's final target read.
372
+ t.mock.method(
373
+ String.prototype,
374
+ "replace",
375
+ function (this: string, ...args: Parameters<typeof nativeReplace>) {
376
+ if (!injected && targetReads === readsBeforeDiff && String(this) === content) {
377
+ injected = true;
378
+ throw cause;
379
+ }
380
+ return nativeReplace.apply(this, args);
381
+ },
382
+ );
383
+ const result = await callTool(
384
+ makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
385
+ {
386
+ path,
387
+ replacements: [{ literal: true, find: "before\n", replace: content }],
388
+ then_run: { command: "check" },
389
+ },
390
+ { ctx: context },
391
+ );
392
+ const report = reportOf(result, "replace");
393
+ assert.equal(injected, true, `diff injection after ${targetReads} target reads`);
394
+ assert.equal(result.isError, true);
395
+ assert.equal(report?.error, "POST_PROCESS_FAILED");
396
+ assert.ok(result.details);
397
+ assert.equal(Reflect.get(result.details, "diff"), undefined);
398
+ assert.equal(report?.publication, content === "before\n" ? "NOT_PUBLISHED" : "PUBLISHED");
399
+ assert.deepEqual(report?.cause, { message: cause.message });
400
+ assert.equal(report?.then_run?.status, "skipped");
401
+ assert.equal(commands, 0);
402
+ } finally {
403
+ t.mock.restoreAll();
404
+ syncBuiltinESMExports();
405
+ }
406
+ assert.equal(await readFile(path, "utf8"), content);
407
+ }
408
+ } finally {
409
+ await rm(dir, { recursive: true, force: true });
473
410
  }
474
411
  });
475
412
 
@@ -480,7 +417,7 @@ const noOpCases = [
480
417
  params: {
481
418
  edits: [
482
419
  {
483
- op: "replace",
420
+ action: "replace",
484
421
  anchor: `1#${computeLineHash(1, "same", DEFAULT_CONFIG.hashLen)}`,
485
422
  body: ["same"],
486
423
  },
@@ -507,16 +444,15 @@ test("all mutation tools succeed without rewriting on no-op, with and without Fu
507
444
  commands++;
508
445
  return { status: "succeeded", result: { content: [{ type: "text", text: "checked" }] } };
509
446
  });
510
- const tool: any = makeTool(dir, mode === "standalone" ? undefined : fusion);
511
- const result = await invoke(
447
+ const tool = makeTool(dir, mode === "standalone" ? undefined : fusion);
448
+ const result = await callTool(
512
449
  tool,
513
- tool.name,
514
450
  { path, ...params, ...(mode === "command" ? { then_run: { command: "check" } } : {}) },
515
- undefined,
516
- undefined,
517
- await ctx(dir, t),
451
+ { ctx: await ctx(dir, t) },
518
452
  );
519
453
  assert.equal(reportOf(result, tool.name)?.publication, "NOT_PUBLISHED");
454
+ assert.ok(result.details);
455
+ assert.equal(Reflect.get(result.details, "diff"), tool.name === "write" ? undefined : "");
520
456
  assert.equal(reportOf(result, tool.name)?.freshness, undefined);
521
457
  assert.doesNotMatch(text(result), /Updated anchors|\d+#[0-9A-Z]+│/);
522
458
  assert.equal(commands, mode === "command" ? 1 : 0);
@@ -537,6 +473,61 @@ test("all mutation tools succeed without rewriting on no-op, with and without Fu
537
473
  }
538
474
  });
539
475
 
476
+ test("delete and append change only line endings → publishes with an empty diff and still checks Fusion freshness", async (t) => {
477
+ const dir = await mkdtemp(join(tmpdir(), "hashline-empty-diff-"));
478
+ try {
479
+ const context = await ctx(dir, t);
480
+ for (const mode of ["standalone", "fusion", "command", "stale"] as const) {
481
+ const path = join(dir, `${mode}.txt`);
482
+ await writeFile(path, "same\r\nsame\n");
483
+ let commands = 0;
484
+ const fusion = createActionFusionExecutor(
485
+ async () => {
486
+ commands++;
487
+ assert.equal(await readFile(path, "utf8"), "same\nsame\r\n");
488
+ return { status: "succeeded", result: { content: [] } };
489
+ },
490
+ (progress) => {
491
+ if (mode === "stale" && progress.mutationCompleted && progress.command === "waiting")
492
+ writeFileSync(path, "external\n");
493
+ },
494
+ );
495
+ const result = await callTool(
496
+ makeEditOverride(dir, DEFAULT_CONFIG, mode === "standalone" ? undefined : fusion),
497
+ {
498
+ path,
499
+ edits: [
500
+ {
501
+ action: "delete",
502
+ anchor: `1#${computeLineHash(1, "same", DEFAULT_CONFIG.hashLen)}`,
503
+ },
504
+ { action: "append", body: ["same"] },
505
+ ],
506
+ ...(["command", "stale"].includes(mode) ? { then_run: { command: "check" } } : {}),
507
+ },
508
+ { ctx: context },
509
+ );
510
+ const report = reportOf(result, "edit");
511
+ assert.equal(result.isError, undefined);
512
+ assert.equal(Reflect.get(result.details, "diff"), "");
513
+ assert.equal(report?.publication, "PUBLISHED");
514
+ assert.equal(report?.freshness, mode === "stale" ? "changed" : undefined);
515
+ assert.equal(
516
+ report?.then_run?.status,
517
+ mode === "command" ? "succeeded" : mode === "stale" ? "skipped" : undefined,
518
+ );
519
+ assert.equal(report?.next, mode === "stale" ? REREAD : undefined);
520
+ assert.equal(commands, mode === "command" ? 1 : 0);
521
+ assert.equal(
522
+ await readFile(path, "utf8"),
523
+ mode === "stale" ? "external\n" : "same\nsame\r\n",
524
+ );
525
+ }
526
+ } finally {
527
+ await rm(dir, { recursive: true, force: true });
528
+ }
529
+ });
530
+
540
531
  test("no-op Fusion still detects external changes and reports command failures", async (t) => {
541
532
  const dir = await mkdtemp(join(tmpdir(), "hashline-noop-fusion-"));
542
533
  try {
@@ -561,14 +552,11 @@ test("no-op Fusion still detects external changes and reports command failures",
561
552
  writeFileSync(path, "external\n");
562
553
  },
563
554
  );
564
- const tool: any = makeTool(dir, fusion);
565
- const result = await invoke(
555
+ const tool = makeTool(dir, fusion);
556
+ const result = await callTool(
566
557
  tool,
567
- tool.name,
568
558
  { path, ...params, then_run: { command: "check" } },
569
- undefined,
570
- undefined,
571
- await ctx(dir, t),
559
+ { ctx: await ctx(dir, t) },
572
560
  );
573
561
  assert.equal(reportOf(result, tool.name)?.publication, "NOT_PUBLISHED");
574
562
  assert.equal(commands, scenario === "before" ? 0 : 1);