@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
@@ -1,17 +1,16 @@
1
+ import { commandCompletion, type SessionCommand } from "./action-fusion.testing.ts";
1
2
  import fs, { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
2
3
  import { syncBuiltinESMExports } from "node:module";
3
4
  import { tmpdir } from "node:os";
4
5
  import { join } from "node:path";
5
6
  import assert from "node:assert/strict";
6
7
  import { test } from "node:test";
7
- import { createActionFusionExecutor, type ActionFusionProgress } from "./action-fusion.ts";
8
- import { FileMutationError } from "./file-commit.ts";
9
- import { DEFAULT_OUTPUT_BUDGETS } from "./budgets.ts";
10
- import { REREAD } from "./report-schema.ts";
11
- import { CommandNotRun, describeFailure, renderReport, reportOf } from "./report.ts";
8
+ import { createActionFusionExecutor } from "./action-fusion.ts";
9
+ import type { ActionFusionProgress } from "./report-schema.ts";
10
+ import { outputBudgetsFor } from "./budgets.ts";
11
+ import { reportOf } from "./report.ts";
12
12
  import { callTool, createToolContext } from "./tool-call.testing.ts";
13
- import { makeWriteOverride } from "./write-tool.ts";
14
- import { publishedMutation } from "./mutation-outcome.testing.ts";
13
+ import { makeWriteOverride } from "./tool-factories.testing.ts";
15
14
 
16
15
  async function withTemp<T>(run: (dir: string) => Promise<T>): Promise<T> {
17
16
  const dir = await mkdtemp(join(tmpdir(), "hashline-failure-"));
@@ -21,7 +20,6 @@ async function withTemp<T>(run: (dir: string) => Promise<T>): Promise<T> {
21
20
  await rm(dir, { recursive: true, force: true });
22
21
  }
23
22
  }
24
- const mutation = (content: string) => publishedMutation(content, { content: [], details: {} });
25
23
 
26
24
  test("temporary preparation and cleanup failures → preserve the commit failure and target bytes", async (t) =>
27
25
  withTemp(async (dir) => {
@@ -51,7 +49,6 @@ test("temporary preparation and cleanup failures → preserve the commit failure
51
49
  const report = reportOf(result, "write");
52
50
  assert.equal(result.isError, true);
53
51
  assert.equal(report?.error, "PUBLISH_FAILED");
54
- assert.equal(report.stage, "commit");
55
52
  assert.equal(report.publication, "NOT_PUBLISHED");
56
53
  assert.deepEqual(report.cause, { code: original.code, message: original.message });
57
54
  assert.equal(await readFile(target, "utf8"), "before\n");
@@ -62,126 +59,135 @@ test("temporary preparation and cleanup failures → preserve the commit failure
62
59
  }
63
60
  }));
64
61
 
65
- test("published post-processing failure → skips command and preserves publication state", async (t) =>
62
+ test("publication boundary and observer failures → preserve recovery and emit each diagnostic once", async (t) =>
66
63
  withTemp(async (dir) => {
67
- const target = join(dir, "published.txt");
68
- await writeFile(target, "content\n");
69
- let commanded = false;
70
- const progress: ActionFusionProgress[] = [];
71
- const mutationError = new FileMutationError(
72
- "POST_PROCESS_FAILED",
73
- "post_process",
74
- "PUBLISHED",
75
- "result generation failed",
76
- );
77
- const fusion = createActionFusionExecutor(
78
- async () => {
79
- commanded = true;
80
- return { status: "succeeded", result: { content: [{ type: "text", text: "never" }] } };
81
- },
82
- (update) => progress.push(update),
83
- );
84
- await assert.rejects(
85
- fusion({
86
- budget: DEFAULT_OUTPUT_BUDGETS,
87
- toolCallId: "published",
88
- tool: "edit",
89
- displayPath: "published.txt",
90
- absolutePath: target,
91
- thenRun: { command: "check" },
92
- mutate: async () => {
93
- throw mutationError;
94
- },
95
- signal: undefined,
96
- ctx: await createToolContext(dir, t),
97
- }),
98
- (error: unknown) => {
99
- assert.ok(error instanceof CommandNotRun);
100
- assert.equal(error.status, "skipped");
101
- assert.equal(error.cause, mutationError);
102
- assert.equal(Object.hasOwn(error, "error"), false);
103
- const report = describeFailure("edit", error, { budget: DEFAULT_OUTPUT_BUDGETS });
104
- assert.equal(report.publication, "PUBLISHED");
105
- assert.equal(report.stage, "post_process");
106
- assert.equal(report.then_run?.status, "skipped");
107
- assert.match(report.next ?? "", /change is saved; do not repeat/);
108
- const diagnostic = renderReport(report);
109
- assert.equal(diagnostic.split(mutationError.message).length - 1, 1);
110
- assert.equal(diagnostic.split('"status": "skipped"').length - 1, 1);
111
- return true;
112
- },
113
- );
114
- assert.equal(commanded, false);
115
- assert.ok(progress.every((update) => update.mutationCompleted === false));
116
- assert.equal(await readFile(target, "utf8"), "content\n");
117
- }));
118
-
119
- test("command fails and modifies the target → reports changed freshness and command failure", async (t) =>
120
- withTemp(async (dir) => {
121
- const target = join(dir, "failed.txt");
122
- const fusion = createActionFusionExecutor(async () => {
123
- await writeFile(target, "command changed\n");
124
- throw new Error("command failed");
125
- });
126
- const outcome = await fusion({
127
- budget: DEFAULT_OUTPUT_BUDGETS,
128
- toolCallId: "failed",
129
- tool: "edit",
130
- displayPath: "failed.txt",
131
- absolutePath: target,
132
- thenRun: { command: "check" },
133
- mutate: async () => {
134
- await writeFile(target, "mutation\n");
135
- return mutation("mutation\n");
136
- },
137
- signal: undefined,
138
- ctx: await createToolContext(dir, t),
139
- });
140
- const report = reportOf(outcome, "edit");
141
- assert.equal(report?.publication, "PUBLISHED");
142
- assert.equal(report?.then_run?.status, "failed");
143
- assert.equal(report?.freshness, "changed");
144
- assert.equal(report?.next, REREAD);
145
- assert.equal(report?.then_run?.output, "command failed");
146
- assert.doesNotMatch(
147
- outcome.content.map((block) => (block.type === "text" ? block.text : "")).join("\n"),
148
- /Updated anchors|1#AB/,
149
- );
150
- assert.equal(await readFile(target, "utf8"), "command changed\n");
64
+ const budget = outputBudgetsFor(4096);
65
+ const context = await createToolContext(dir, t);
66
+ for (const publication of ["NOT_PUBLISHED", "PUBLISHED", "UNKNOWN"] as const) {
67
+ for (const progressError of [
68
+ undefined,
69
+ "display unavailable",
70
+ Object.assign(new Error("display unavailable " + "界".repeat(budget.fieldBytes)), {
71
+ code: "EIO",
72
+ cause: new Error("nested " + "界".repeat(budget.fieldBytes)),
73
+ }),
74
+ ]) {
75
+ const target = join(dir, "boundary.txt");
76
+ await writeFile(target, "before\n");
77
+ const original = Object.assign(new Error("injected filesystem failure"), { code: "EIO" });
78
+ const nativeOpen = fs.open;
79
+ const nativeRename = fs.rename;
80
+ let published = false;
81
+ let commands = 0;
82
+ const progress: ActionFusionProgress[] = [];
83
+ try {
84
+ t.mock.method(fs, "open", async (...args: Parameters<typeof nativeOpen>) => {
85
+ if (
86
+ publication === "NOT_PUBLISHED" &&
87
+ String(args[0]).includes(".hashline-commit-") &&
88
+ args[1] === "w"
89
+ )
90
+ throw original;
91
+ if (publication === "PUBLISHED" && published && String(args[0]) === target)
92
+ throw original;
93
+ return nativeOpen(...args);
94
+ });
95
+ t.mock.method(fs, "rename", async (...args: Parameters<typeof nativeRename>) => {
96
+ if (String(args[1]) === target && publication === "UNKNOWN") throw original;
97
+ await nativeRename(...args);
98
+ published = true;
99
+ });
100
+ syncBuiltinESMExports();
101
+ const executeTool: SessionCommand = async () => {
102
+ commands++;
103
+ return commandCompletion({ content: [] });
104
+ };
105
+ const fusion = createActionFusionExecutor((update) => {
106
+ progress.push(update);
107
+ if (progressError !== undefined) throw progressError;
108
+ });
109
+ const result = await callTool(
110
+ makeWriteOverride(dir, fusion, () => budget),
111
+ { path: target, mode: "overwrite", content: "after\n", then_run: { command: "check" } },
112
+ { ctx: { ...context, executeTool } },
113
+ );
114
+ const report = reportOf(result, "write");
115
+ assert.equal(result.isError, true);
116
+ assert.ok(report);
117
+ assert.equal(
118
+ report.error,
119
+ publication === "PUBLISHED" ? "POST_PROCESS_FAILED" : "PUBLISH_FAILED",
120
+ );
121
+ assert.equal(report.publication, publication);
122
+ assert.deepEqual(report.cause, { code: original.code, message: original.message });
123
+ assert.equal(report.then_run?.status, "skipped");
124
+ if (publication === "PUBLISHED")
125
+ assert.match(report.next ?? "", /change is saved; do not repeat/);
126
+ else if (publication === "UNKNOWN")
127
+ assert.match(report.next ?? "", /may or may not be saved/);
128
+ else assert.equal(report.next, undefined);
129
+ assert.equal(result.content.length, 1);
130
+ const block = result.content[0];
131
+ assert.equal(block.type, "text");
132
+ const diagnostic = block.text;
133
+ for (const once of [
134
+ original.message,
135
+ report.message,
136
+ '"then_run"',
137
+ '"status": "skipped"',
138
+ ])
139
+ assert.equal(diagnostic.split(once).length - 1, 1);
140
+ assert.equal(commands, 0);
141
+ assert.deepEqual(
142
+ progress.map((update) => update.command),
143
+ ["waiting", "skipped"],
144
+ );
145
+ assert.ok(progress.every((update) => !update.mutationCompleted));
146
+ if (typeof progressError === "string") {
147
+ assert.deepEqual(report.progressError, { message: progressError });
148
+ } else if (progressError) {
149
+ assert.equal(report.progressError?.code, "EIO");
150
+ assert.ok(report.progressError.message.startsWith("display unavailable"));
151
+ assert.ok(Buffer.byteLength(report.progressError.message) <= budget.fieldBytes);
152
+ assert.notEqual(report.progressError.message, progressError.message);
153
+ assert.ok(report.progressError.cause);
154
+ assert.ok(report.progressError.cause.message.startsWith("nested"));
155
+ assert.ok(Buffer.byteLength(report.progressError.cause.message) <= budget.fieldBytes);
156
+ assert.notEqual(report.progressError.cause.message, progressError.cause.message);
157
+ } else assert.equal(report.progressError, undefined);
158
+ } finally {
159
+ t.mock.restoreAll();
160
+ syncBuiltinESMExports();
161
+ }
162
+ assert.equal(
163
+ await readFile(target, "utf8"),
164
+ publication === "PUBLISHED" ? "after\n" : "before\n",
165
+ );
166
+ }
167
+ }
151
168
  }));
152
169
 
153
170
  test("cancellation after publication → does not rerun mutation and still inspects freshness", async (t) =>
154
171
  withTemp(async (dir) => {
155
172
  for (const state of ["changed", "missing"] as const) {
156
173
  const target = join(dir, `${state}.txt`);
157
- let mutations = 0;
158
174
  const controller = new AbortController();
159
- const fusion = createActionFusionExecutor(async () => {
175
+ const executeTool: SessionCommand = async () => {
160
176
  if (state === "changed") await writeFile(target, "command changed\n");
161
177
  else await rm(target);
162
178
  controller.abort();
163
- throw new Error("timed out");
164
- });
165
- const outcome = await fusion({
166
- budget: DEFAULT_OUTPUT_BUDGETS,
167
- toolCallId: "cancelled",
168
- tool: "edit",
169
- displayPath: `${state}.txt`,
170
- absolutePath: target,
171
- thenRun: { command: "check" },
172
- mutate: async () => {
173
- mutations++;
174
- await writeFile(target, "mutation\n");
175
- return mutation("mutation\n");
176
- },
177
- signal: controller.signal,
178
- ctx: await createToolContext(dir, t),
179
- });
180
- const report = reportOf(outcome, "edit");
179
+ return commandCompletion({ content: [{ type: "text", text: "timed out" }] }, true);
180
+ };
181
+ const fusion = createActionFusionExecutor(() => {});
182
+ const result = await callTool(
183
+ makeWriteOverride(dir, fusion),
184
+ { path: target, mode: "create", content: "mutation\n", then_run: { command: "check" } },
185
+ { signal: controller.signal, ctx: { ...(await createToolContext(dir, t)), executeTool } },
186
+ );
187
+ const report = reportOf(result, "write");
181
188
  assert.equal(report?.publication, "PUBLISHED");
182
189
  assert.equal(report?.then_run?.status, "cancelled");
183
190
  assert.equal(report?.freshness, state);
184
- assert.equal(mutations, 1);
185
191
  if (state === "changed") assert.equal(await readFile(target, "utf8"), "command changed\n");
186
192
  else await assert.rejects(readFile(target), { code: "ENOENT" });
187
193
  }
@@ -192,28 +198,29 @@ test("first command fails → releases the queue for the next mutation", async (
192
198
  const target = join(dir, "queue.txt");
193
199
  const ctx = await createToolContext(dir, t);
194
200
  let calls = 0;
195
- const fusion = createActionFusionExecutor(async () => {
201
+ const executeTool: SessionCommand = async () => {
196
202
  calls++;
197
- if (calls === 1) throw new Error("first command failed");
198
- return { status: "succeeded", result: { content: [{ type: "text", text: "ok" }] } };
199
- });
203
+ if (calls === 1)
204
+ return commandCompletion(
205
+ { content: [{ type: "text", text: "first command failed" }] },
206
+ true,
207
+ );
208
+ return commandCompletion({ content: [{ type: "text", text: "ok" }] });
209
+ };
210
+ const fusion = createActionFusionExecutor(() => {});
200
211
  for (const content of ["first\n", "second\n"]) {
201
- const result = await fusion({
202
- budget: DEFAULT_OUTPUT_BUDGETS,
203
- toolCallId: content,
204
- tool: "edit",
205
- displayPath: "queue.txt",
206
- absolutePath: target,
207
- thenRun: { command: "check" },
208
- mutate: async () => {
209
- await writeFile(target, content);
210
- return mutation(content);
212
+ const result = await callTool(
213
+ makeWriteOverride(dir, fusion),
214
+ {
215
+ path: target,
216
+ content,
217
+ mode: calls === 0 ? "create" : "overwrite",
218
+ then_run: { command: "check" },
211
219
  },
212
- signal: undefined,
213
- ctx,
214
- });
220
+ { toolCallId: content, ctx: { ...ctx, executeTool } },
221
+ );
215
222
  assert.equal(
216
- reportOf(result, "edit")?.then_run?.status,
223
+ reportOf(result, "write")?.then_run?.status,
217
224
  calls === 1 ? "failed" : "succeeded",
218
225
  );
219
226
  }
@@ -225,33 +232,53 @@ test("command succeeds on an unchanged target → retains the result and complet
225
232
  withTemp(async (dir) => {
226
233
  const target = join(dir, "finalize.txt");
227
234
  const progress: ActionFusionProgress[] = [];
228
- const fusion = createActionFusionExecutor(
229
- async () => ({
230
- status: "succeeded",
231
- result: { content: [{ type: "text", text: "command finished" }] },
232
- }),
233
- (event) => progress.push(event),
235
+ const executeTool: SessionCommand = async () =>
236
+ commandCompletion({ content: [{ type: "text", text: "command finished" }] });
237
+ const fusion = createActionFusionExecutor((event) => progress.push(event));
238
+ const completed = await callTool(
239
+ makeWriteOverride(dir, fusion),
240
+ { path: target, content: "saved\n", mode: "create", then_run: { command: "check" } },
241
+ { ctx: { ...(await createToolContext(dir, t)), executeTool } },
234
242
  );
235
- const completed = await fusion({
236
- budget: DEFAULT_OUTPUT_BUDGETS,
237
- toolCallId: "finalize",
238
- tool: "edit",
239
- displayPath: "finalize.txt",
240
- absolutePath: target,
241
- thenRun: { command: "check" },
242
- signal: undefined,
243
- ctx: await createToolContext(dir, t),
244
- mutate: async () => {
245
- await writeFile(target, "saved\n");
246
- return mutation("saved\n");
247
- },
248
- });
249
243
  assert.equal(completed.content.length, 1);
250
- assert.equal(reportOf(completed, "edit")?.then_run?.status, "succeeded");
251
- assert.equal(reportOf(completed, "edit")?.then_run?.output, "command finished");
244
+ assert.equal(reportOf(completed, "write")?.then_run?.status, "succeeded");
245
+ assert.equal(reportOf(completed, "write")?.then_run?.output, "command finished");
252
246
  assert.equal(progress.filter((event) => event.command === "succeeded").length, 1);
253
247
  assert.equal(progress.at(-1)?.freshness, "unchanged");
254
248
  assert.deepEqual(progress.at(-1)?.result, {
255
249
  content: [{ type: "text", text: "command finished" }],
250
+ details: {},
256
251
  });
257
252
  }));
253
+
254
+ test("cancellation before mutation → reports a cancelled command without publishing", async (t) =>
255
+ withTemp(async (dir) => {
256
+ const target = join(dir, "cancelled.txt");
257
+ await writeFile(target, "before\n");
258
+ const controller = new AbortController();
259
+ controller.abort();
260
+ let commands = 0;
261
+ const progress: ActionFusionProgress[] = [];
262
+ const executeTool: SessionCommand = async () => {
263
+ commands++;
264
+ return commandCompletion({ content: [] });
265
+ };
266
+ const fusion = createActionFusionExecutor((event) => progress.push(event));
267
+ const result = await callTool(
268
+ makeWriteOverride(dir, fusion),
269
+ { path: target, mode: "overwrite", content: "after\n", then_run: { command: "check" } },
270
+ { signal: controller.signal, ctx: { ...(await createToolContext(dir, t)), executeTool } },
271
+ );
272
+ const report = reportOf(result, "write");
273
+ assert.equal(result.isError, true);
274
+ assert.equal(report?.error, "OPERATION_ABORTED");
275
+ assert.equal(report?.publication, "NOT_PUBLISHED");
276
+ assert.equal(report?.then_run?.status, "cancelled");
277
+ assert.deepEqual(
278
+ progress.map((event) => event.command),
279
+ ["waiting", "cancelled"],
280
+ );
281
+ assert.ok(progress.every((event) => !event.mutationCompleted));
282
+ assert.equal(commands, 0);
283
+ assert.equal(await readFile(target, "utf8"), "before\n");
284
+ }));