@criogaid/pi-hashline-edit 0.6.0 → 0.7.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 (148) hide show
  1. package/README.md +161 -304
  2. package/THIRD_PARTY_NOTICES.md +80 -0
  3. package/package.json +10 -6
  4. package/src/core/AGENTS.md +20 -0
  5. package/src/core/apply.test.ts +189 -60
  6. package/src/core/apply.ts +110 -74
  7. package/src/core/case-fold.test.ts +40 -0
  8. package/src/core/case-fold.ts +81 -0
  9. package/src/core/character-set.test.ts +103 -0
  10. package/src/core/character-set.ts +170 -0
  11. package/src/core/errors.test.ts +23 -0
  12. package/src/core/errors.ts +70 -4
  13. package/src/core/lines.ts +7 -0
  14. package/src/core/regex.ts +21 -0
  15. package/src/core/replace-schema.ts +34 -0
  16. package/src/core/replace.ts +37 -54
  17. package/src/core/search-analysis.test.ts +199 -0
  18. package/src/core/search-analysis.ts +375 -0
  19. package/src/core/search-matrix.test.ts +64 -0
  20. package/src/core/search-matrix.testing.ts +183 -0
  21. package/src/core/search-schema.ts +18 -0
  22. package/src/core/search.test.ts +145 -0
  23. package/src/core/search.ts +119 -0
  24. package/src/core/text.test.ts +16 -8
  25. package/src/core/text.ts +26 -6
  26. package/src/core/types.ts +16 -11
  27. package/src/index.ts +25 -15
  28. package/src/integration/config-registration.test.ts +30 -24
  29. package/src/integration/forget-grep.test.ts +52 -2
  30. package/src/integration/fusion-dispatch.test.ts +21 -105
  31. package/src/integration/grep-errors.test.ts +50 -0
  32. package/src/integration/grep-flags.test.ts +280 -0
  33. package/src/integration/grep-js-completeness.test.ts +208 -0
  34. package/src/integration/grep-line-candidates.test.ts +233 -0
  35. package/src/integration/grep-matrix.test.ts +121 -0
  36. package/src/integration/grep-path-matrix.test.ts +132 -0
  37. package/src/integration/grep-prefilter-coverage.test.ts +189 -0
  38. package/src/integration/grep-regex.test.ts +410 -0
  39. package/src/integration/grep-rg.test.ts +220 -402
  40. package/src/integration/grep-scan-budget.test.ts +89 -0
  41. package/src/integration/grep-translation.test.ts +206 -0
  42. package/src/integration/large-text.test.ts +57 -1
  43. package/src/integration/model-budget-lifecycle.test.ts +128 -0
  44. package/src/integration/mutation-file-types.test.ts +12 -25
  45. package/src/integration/mutation-file-types.testing.ts +33 -0
  46. package/src/integration/npm-package.test.ts +17 -0
  47. package/src/integration/npm-package.testing.ts +123 -0
  48. package/src/integration/publication-read-changes.test.ts +344 -0
  49. package/src/integration/read-changes.test.ts +98 -0
  50. package/src/integration/replace-package.test.ts +17 -36
  51. package/src/integration/replace-package.testing.ts +33 -0
  52. package/src/integration/replace-worker-boundary.test.ts +107 -0
  53. package/src/integration/report-replay.test.ts +90 -0
  54. package/src/integration/utf8-errors.test.ts +46 -0
  55. package/src/pi/AGENTS.md +55 -0
  56. package/src/pi/action-fusion.test.ts +253 -103
  57. package/src/pi/action-fusion.ts +116 -290
  58. package/src/pi/anchor-format.ts +9 -0
  59. package/src/pi/argument-diagnostics.ts +478 -0
  60. package/src/pi/argument-error.testing.ts +43 -0
  61. package/src/pi/argument-error.ts +78 -0
  62. package/src/pi/argument-format.test.ts +208 -0
  63. package/src/pi/argument-validation.test.ts +742 -45
  64. package/src/pi/argument-validation.ts +56 -59
  65. package/src/pi/budgets.ts +45 -16
  66. package/src/pi/codemode.test.ts +96 -0
  67. package/src/pi/config.test.ts +14 -10
  68. package/src/pi/config.ts +21 -7
  69. package/src/pi/diagnostic-buffer.test.ts +52 -16
  70. package/src/pi/diagnostic-buffer.ts +11 -14
  71. package/src/pi/directory-sync.test.ts +4 -1
  72. package/src/pi/edit-tool.ts +100 -93
  73. package/src/pi/error-text.ts +39 -16
  74. package/src/pi/execute.test.ts +362 -315
  75. package/src/pi/extension-loading.test.ts +14 -0
  76. package/src/pi/extension-loading.testing.ts +3 -0
  77. package/src/pi/failure-context.test.ts +220 -88
  78. package/src/pi/failure-context.ts +161 -180
  79. package/src/pi/failure-path.test.ts +85 -88
  80. package/src/pi/file-commit.test.ts +78 -45
  81. package/src/pi/file-commit.ts +218 -151
  82. package/src/pi/file-read.test.ts +29 -0
  83. package/src/pi/file-read.ts +60 -0
  84. package/src/pi/file-snapshot.test.ts +1 -1
  85. package/src/pi/forget-tool.ts +189 -90
  86. package/src/pi/forget.test.ts +309 -22
  87. package/src/pi/forget.testing.ts +41 -113
  88. package/src/pi/fusion-card.test.ts +40 -14
  89. package/src/pi/fusion-card.ts +84 -50
  90. package/src/pi/grep-candidates.test.ts +127 -0
  91. package/src/pi/grep-candidates.ts +193 -0
  92. package/src/pi/grep-matrix.test.ts +83 -0
  93. package/src/pi/grep-output.ts +181 -138
  94. package/src/pi/grep-render.ts +28 -25
  95. package/src/pi/grep-scope.ts +61 -53
  96. package/src/pi/grep-search.ts +299 -242
  97. package/src/pi/grep-tool.ts +150 -140
  98. package/src/pi/grep-worker.mjs +4 -0
  99. package/src/pi/grep-worker.ts +14 -0
  100. package/src/pi/grep.test.ts +631 -632
  101. package/src/pi/model-budgets.test.ts +43 -0
  102. package/src/pi/model-budgets.ts +44 -0
  103. package/src/pi/mutation-outcome.testing.ts +5 -5
  104. package/src/pi/mutation-result.test.ts +21 -74
  105. package/src/pi/mutation-result.ts +74 -87
  106. package/src/pi/mutation-runner.ts +83 -75
  107. package/src/pi/output-budgets.test.ts +166 -0
  108. package/src/pi/path.ts +7 -0
  109. package/src/pi/read-tool.ts +151 -137
  110. package/src/pi/read.test.ts +39 -33
  111. package/src/pi/regex-worker-contract.ts +73 -0
  112. package/src/pi/regex-worker.ts +101 -0
  113. package/src/pi/registration.test.ts +111 -92
  114. package/src/pi/render.test.ts +193 -19
  115. package/src/pi/render.ts +137 -29
  116. package/src/pi/replace-regex.ts +11 -44
  117. package/src/pi/replace-tool.ts +38 -145
  118. package/src/pi/replace-worker-contract.ts +19 -0
  119. package/src/pi/replace-worker.ts +12 -8
  120. package/src/pi/replace.test.ts +93 -160
  121. package/src/pi/report-cause.ts +99 -0
  122. package/src/pi/report-failure.testing.ts +20 -0
  123. package/src/pi/report-schema.ts +512 -0
  124. package/src/pi/report.test.ts +276 -0
  125. package/src/pi/report.ts +288 -0
  126. package/src/pi/result-safety.test.ts +122 -427
  127. package/src/pi/rg-process.test.ts +78 -79
  128. package/src/pi/rg-process.ts +124 -177
  129. package/src/pi/rg-regex.test.ts +50 -0
  130. package/src/pi/rg-regex.ts +709 -0
  131. package/src/pi/text-stream.test.ts +67 -38
  132. package/src/pi/text-stream.ts +96 -75
  133. package/src/pi/tool-call.testing.ts +3 -39
  134. package/src/pi/write-tool.ts +45 -34
  135. package/src/pi/write.test.ts +45 -37
  136. package/src/testing/extension-loader.testing.ts +24 -0
  137. package/src/testing/installation.testing.ts +77 -0
  138. package/src/testing/process.testing.ts +33 -0
  139. package/src/testing/regex-host.testing.ts +20 -0
  140. package/src/testing/regex-translation-corpus.ts +308 -0
  141. package/src/testing/rg-line-listing.fixture.mjs +7 -0
  142. package/src/testing/run-tests.ts +47 -0
  143. package/src/testing/session.testing.ts +127 -0
  144. package/src/pi/rg-line-ranges.test.ts +0 -66
  145. package/src/pi/rg-line-ranges.ts +0 -75
  146. package/src/pi/rg-match-bytes.ts +0 -95
  147. package/src/pi/rg-text-view.ts +0 -192
  148. package/src/pi/tool-prompts.ts +0 -15
@@ -1,16 +1,13 @@
1
- import { mkdtemp, rm, writeFile } from "node:fs/promises";
1
+ import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
2
2
  import { tmpdir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import assert from "node:assert/strict";
5
5
  import { test } from "node:test";
6
- import {
7
- createActionFusionExecutor,
8
- ActionFusionError,
9
- THEN_RUN_STALE,
10
- THEN_RUN_SUCCEEDED,
11
- } from "./action-fusion.ts";
6
+ import { createActionFusionExecutor, type ActionFusionProgress } from "./action-fusion.ts";
12
7
  import { FileMutationError } from "./file-commit.ts";
13
- import { staleTargetNotice } from "./mutation-result.ts";
8
+ import { REREAD } from "./report-schema.ts";
9
+ import { CommandNotRun, describeFailure, reportOf } from "./report.ts";
10
+ import { createToolContext } from "./tool-call.testing.ts";
14
11
  import { publishedMutation } from "./mutation-outcome.testing.ts";
15
12
 
16
13
  async function withTemp<T>(run: (dir: string) => Promise<T>): Promise<T> {
@@ -21,175 +18,175 @@ async function withTemp<T>(run: (dir: string) => Promise<T>): Promise<T> {
21
18
  await rm(dir, { recursive: true, force: true });
22
19
  }
23
20
  }
21
+ const mutation = (content: string) => publishedMutation(content, { content: [], details: {} });
24
22
 
25
- const context = (cwd: string) => ({ cwd }) as any;
26
- const result = (content: string) =>
27
- publishedMutation(content, {
28
- content: [{ type: "text", text: "mutation" }],
29
- details: undefined,
30
- });
31
-
32
- test("published post-processing failure skips command and preserves publication state", async () =>
23
+ test("published post-processing failure → skips command and preserves publication state", async (t) =>
33
24
  withTemp(async (dir) => {
34
25
  const target = join(dir, "published.txt");
35
26
  await writeFile(target, "content\n");
36
27
  let commanded = false;
37
- const updates: any[] = [];
38
- const fusion = createActionFusionExecutor(async () => {
39
- commanded = true;
40
- return { status: "succeeded", output: "never" };
41
- });
28
+ const progress: ActionFusionProgress[] = [];
29
+ const fusion = createActionFusionExecutor(
30
+ async () => {
31
+ commanded = true;
32
+ return { status: "succeeded", output: "never" };
33
+ },
34
+ (update) => progress.push(update),
35
+ );
42
36
  await assert.rejects(
43
37
  fusion({
44
38
  toolCallId: "published",
39
+ tool: "edit",
40
+ displayPath: "published.txt",
45
41
  absolutePath: target,
46
42
  thenRun: { command: "check" },
47
43
  mutate: async () => {
48
44
  throw new FileMutationError(
45
+ "POST_PROCESS_FAILED",
49
46
  "post_process",
50
47
  "PUBLISHED",
51
- "file was published but result generation failed",
48
+ "result generation failed",
52
49
  );
53
50
  },
54
51
  signal: undefined,
55
- ctx: context(dir),
56
- onUpdate: (update) => updates.push(update),
52
+ ctx: await createToolContext(dir, t),
57
53
  }),
58
- (error: unknown) =>
59
- error instanceof ActionFusionError &&
60
- error.publication === "PUBLISHED" &&
61
- error.command === "skipped" &&
62
- !commanded &&
63
- /Re-read before retrying/.test(error.message),
54
+ (error: unknown) => {
55
+ assert.ok(error instanceof CommandNotRun);
56
+ const report = describeFailure("edit", error, { mutation: true });
57
+ assert.equal(report.publication, "PUBLISHED");
58
+ assert.equal(report.then_run?.status, "skipped");
59
+ assert.match(report.next ?? "", /change is saved; do not repeat/);
60
+ return true;
61
+ },
64
62
  );
65
- assert.ok(updates.every((update) => update.details.actionFusion.mutationCompleted === false));
66
- assert.equal(updates.at(-1).details.actionFusion.publication, "PUBLISHED");
63
+ assert.equal(commanded, false);
64
+ assert.ok(progress.every((update) => update.mutationCompleted === false));
65
+ assert.equal(progress.at(-1)?.publication, "PUBLISHED");
66
+ assert.equal(await readFile(target, "utf8"), "content\n");
67
67
  }));
68
68
 
69
- test("command failure still reports final changed freshness", async () =>
69
+ test("command fails and modifies the target → reports changed freshness and command failure", async (t) =>
70
70
  withTemp(async (dir) => {
71
71
  const target = join(dir, "failed.txt");
72
- await writeFile(target, "before\n");
73
72
  const fusion = createActionFusionExecutor(async () => {
74
73
  await writeFile(target, "command changed\n");
75
74
  throw new Error("command failed");
76
75
  });
77
- const outcome: any = await fusion({
76
+ const outcome = await fusion({
78
77
  toolCallId: "failed",
78
+ tool: "edit",
79
+ displayPath: "failed.txt",
79
80
  absolutePath: target,
80
81
  thenRun: { command: "check" },
81
82
  mutate: async () => {
82
83
  await writeFile(target, "mutation\n");
83
- return result("mutation\n");
84
+ return mutation("mutation\n");
84
85
  },
85
86
  signal: undefined,
86
- ctx: context(dir),
87
+ ctx: await createToolContext(dir, t),
87
88
  });
88
89
  assert.deepEqual(outcome.details.actionFusion, {
89
90
  publication: "PUBLISHED",
90
91
  command: "failed",
91
92
  freshness: "changed",
92
93
  });
93
- assert.deepEqual(outcome.content[1], { type: "text", text: staleTargetNotice(THEN_RUN_STALE) });
94
- assert.equal(outcome.content.length, 3);
95
- assert.match(outcome.content[2].text, /command failed/);
94
+ const report = reportOf(outcome);
95
+ assert.equal(report?.freshness, "changed");
96
+ assert.equal(report?.next, REREAD);
97
+ assert.equal(report?.then_run?.output, "command failed");
98
+ assert.doesNotMatch(
99
+ outcome.content.map((block) => (block.type === "text" ? block.text : "")).join("\n"),
100
+ /Updated anchors|1#AB/,
101
+ );
102
+ assert.equal(await readFile(target, "utf8"), "command changed\n");
96
103
  }));
97
104
 
98
- test("timeout and cancellation do not rerun mutation and still inspect freshness", async () =>
105
+ test("cancellation after publication → does not rerun mutation and still inspects freshness", async (t) =>
99
106
  withTemp(async (dir) => {
100
107
  const target = join(dir, "cancelled.txt");
101
- await writeFile(target, "before\n");
102
108
  let mutations = 0;
103
109
  const controller = new AbortController();
104
110
  const fusion = createActionFusionExecutor(async () => {
105
111
  controller.abort();
106
112
  throw new Error("timed out");
107
113
  });
108
- const outcome: any = await fusion({
114
+ const outcome = await fusion({
109
115
  toolCallId: "cancelled",
116
+ tool: "edit",
117
+ displayPath: "cancelled.txt",
110
118
  absolutePath: target,
111
119
  thenRun: { command: "check" },
112
120
  mutate: async () => {
113
121
  mutations++;
114
122
  await writeFile(target, "mutation\n");
115
- return result("mutation\n");
123
+ return mutation("mutation\n");
116
124
  },
117
125
  signal: controller.signal,
118
- ctx: context(dir),
126
+ ctx: await createToolContext(dir, t),
119
127
  });
120
128
  assert.equal(outcome.details.actionFusion.command, "cancelled");
121
129
  assert.equal(outcome.details.actionFusion.freshness, "unchanged");
122
130
  assert.equal(mutations, 1);
131
+ assert.equal(await readFile(target, "utf8"), "mutation\n");
123
132
  }));
124
133
 
125
- test("queue is released after a failed command", async () =>
134
+ test("first command fails → releases the queue for the next mutation", async (t) =>
126
135
  withTemp(async (dir) => {
127
136
  const target = join(dir, "queue.txt");
128
- await writeFile(target, "before\n");
137
+ const ctx = await createToolContext(dir, t);
129
138
  let calls = 0;
130
139
  const fusion = createActionFusionExecutor(async () => {
131
140
  calls++;
132
141
  if (calls === 1) throw new Error("first command failed");
133
142
  return { status: "succeeded", output: "ok" };
134
143
  });
135
- const first = fusion({
136
- toolCallId: "first",
137
- absolutePath: target,
138
- thenRun: { command: "fail" },
139
- mutate: async () => {
140
- await writeFile(target, "first\n");
141
- return result("first\n");
142
- },
143
- signal: undefined,
144
- ctx: context(dir),
145
- });
146
- const failed: any = await first;
147
- assert.equal(failed.details.actionFusion.command, "failed");
148
- const second = await fusion({
149
- toolCallId: "second",
150
- absolutePath: target,
151
- thenRun: { command: "ok" },
152
- mutate: async () => {
153
- await writeFile(target, "second\n");
154
- return result("second\n");
155
- },
156
- signal: undefined,
157
- ctx: context(dir),
158
- });
144
+ for (const content of ["first\n", "second\n"]) {
145
+ const result = await fusion({
146
+ toolCallId: content,
147
+ tool: "edit",
148
+ displayPath: "queue.txt",
149
+ absolutePath: target,
150
+ thenRun: { command: "check" },
151
+ mutate: async () => {
152
+ await writeFile(target, content);
153
+ return mutation(content);
154
+ },
155
+ signal: undefined,
156
+ ctx,
157
+ });
158
+ assert.equal(reportOf(result)?.then_run?.status, calls === 1 ? "failed" : "succeeded");
159
+ }
159
160
  assert.equal(calls, 2);
160
- assert.match(
161
- second.content[1].type === "text" ? second.content[1].text : "",
162
- /then_run:succeeded/,
163
- );
161
+ assert.equal(await readFile(target, "utf8"), "second\n");
164
162
  }));
165
163
 
166
- test("successful command finalizes anchors and retains completed progress", async () =>
164
+ test("command succeeds on an unchanged target → retains the result and completed progress", async (t) =>
167
165
  withTemp(async (dir) => {
168
166
  const target = join(dir, "finalize.txt");
169
- const progress: any[] = [];
167
+ const progress: ActionFusionProgress[] = [];
170
168
  const fusion = createActionFusionExecutor(
171
169
  async () => ({ status: "succeeded", output: "command finished" }),
172
170
  (event) => progress.push(event),
173
171
  );
174
172
  const completed = await fusion({
175
173
  toolCallId: "finalize",
174
+ tool: "edit",
175
+ displayPath: "finalize.txt",
176
176
  absolutePath: target,
177
177
  thenRun: { command: "check" },
178
178
  signal: undefined,
179
- ctx: context(dir),
179
+ ctx: await createToolContext(dir, t),
180
180
  mutate: async () => {
181
181
  await writeFile(target, "saved\n");
182
- return { ...result("saved\n"), anchors: " ANCHOR" };
182
+ return mutation("saved\n");
183
183
  },
184
184
  });
185
- assert.deepEqual(completed.content, [
186
- { type: "text", text: "mutation ANCHOR" },
187
- { type: "text", text: `${THEN_RUN_SUCCEEDED}\ncommand finished` },
188
- ]);
185
+ assert.equal(completed.content.length, 1);
186
+ assert.equal(reportOf(completed)?.then_run?.status, "succeeded");
187
+ assert.equal(reportOf(completed)?.then_run?.output, "command finished");
189
188
  assert.equal(progress.filter((event) => event.command === "succeeded").length, 1);
190
- assert.equal(progress.at(-1).publication, "PUBLISHED");
191
- assert.equal(progress.at(-1).freshness, "unchanged");
192
- assert.equal(progress.at(-1).command, "succeeded");
193
- assert.equal(progress.at(-1).output, "command finished");
194
- assert.equal(progress.at(-1).reason, undefined);
189
+ assert.equal(progress.at(-1)?.publication, "PUBLISHED");
190
+ assert.equal(progress.at(-1)?.freshness, "unchanged");
191
+ assert.equal(progress.at(-1)?.output, "command finished");
195
192
  }));
@@ -24,10 +24,43 @@ import {
24
24
  FileMutationError,
25
25
  fileRevision,
26
26
  readEditableSnapshot,
27
+ type CommitResult,
28
+ type FreshnessObservation,
27
29
  } from "./file-commit.ts";
28
30
  import { createActionFusionExecutor } from "./action-fusion.ts";
29
31
  import { makeWriteOverride } from "./write-tool.ts";
30
32
  import { callTool } from "./tool-call.testing.ts";
33
+ import { reportOf } from "./report.ts";
34
+
35
+ // Checked by tsc; these states cannot be returned by the commit layer.
36
+ function invalidCommitStates() {
37
+ // @ts-expect-error A newly created target is always published.
38
+ const unpublishedCreate: CommitResult = {
39
+ created: true,
40
+ publication: "NOT_PUBLISHED",
41
+ publishedRevision: "r",
42
+ };
43
+ // @ts-expect-error Replacing an existing target retains its base revision.
44
+ const missingBase: CommitResult = {
45
+ created: false,
46
+ publication: "PUBLISHED",
47
+ publishedRevision: "r",
48
+ };
49
+ // @ts-expect-error A no-op has a confirmed observed revision.
50
+ const unobservedNoop: CommitResult = {
51
+ created: false,
52
+ publication: "NOT_PUBLISHED",
53
+ baseRevision: "r",
54
+ publishedRevision: "r",
55
+ };
56
+ // @ts-expect-error A known freshness state cannot carry a read error.
57
+ const knownError: FreshnessObservation = {
58
+ freshness: "changed",
59
+ error: new Error("read failed"),
60
+ };
61
+ // @ts-expect-error Unknown freshness records the error that prevented observation.
62
+ const unexplainedUnknown: FreshnessObservation = { freshness: "unknown" };
63
+ }
31
64
 
32
65
  async function withTemp<T>(run: (dir: string) => Promise<T>): Promise<T> {
33
66
  const dir = await mkdtemp(join(tmpdir(), "hashline-commit-"));
@@ -98,7 +131,7 @@ test("independent create commits race at the no-replace publication primitive",
98
131
  assert.match(await readFile(target, "utf8"), /^winner-[ab]\n$/);
99
132
  }));
100
133
 
101
- test("create never overwrites an existing target and expectedRevision remains strict", async () =>
134
+ test("create never overwrites an existing target", async () =>
102
135
  withTemp(async (dir) => {
103
136
  const target = join(dir, "existing.txt");
104
137
  await writeFile(target, "original\n");
@@ -108,10 +141,6 @@ test("create never overwrites an existing target and expectedRevision remains st
108
141
  (error: unknown) =>
109
142
  error instanceof FileMutationError && error.publication === "NOT_PUBLISHED",
110
143
  );
111
- await assert.rejects(
112
- commitFile(target, "replacement\n", { mode: "overwrite", expectedRevision: "stale" }),
113
- /expectedRevision/,
114
- );
115
144
  assert.equal(await fileRevision(target), revision);
116
145
  assert.equal(await readFile(target, "utf8"), "original\n");
117
146
  }));
@@ -229,22 +258,21 @@ test("Windows shared access failure preserves the target, skips then_run, and re
229
258
  }),
230
259
  );
231
260
  try {
232
- await assert.rejects(
233
- callTool(
234
- tool,
235
- {
236
- path: target,
237
- content: "replacement\n",
238
- mode: "overwrite",
239
- then_run: { command: "deterministic-command" },
240
- },
241
- { toolCallId: "shared-failure", ctx: { cwd: dir } },
242
- ),
243
- (error: unknown) =>
244
- error instanceof Error &&
245
- /File state is uncertain|No file changes were published/.test(error.message) &&
246
- /Command skipped/.test(error.message),
261
+ const failed = await callTool(
262
+ tool,
263
+ {
264
+ path: target,
265
+ content: "replacement\n",
266
+ mode: "overwrite",
267
+ then_run: { command: "deterministic-command" },
268
+ },
269
+ { toolCallId: "shared-failure", ctx: { cwd: dir } },
247
270
  );
271
+ assert.equal(failed.isError, true);
272
+ const report = reportOf(failed);
273
+ assert.ok(report);
274
+ assert.ok(report.publication === "NOT_PUBLISHED" || report.publication === "UNKNOWN");
275
+ assert.equal(report.then_run?.status, "skipped");
248
276
  assert.equal(commandRuns, 0);
249
277
  } finally {
250
278
  holder.release();
@@ -279,10 +307,8 @@ test("Windows shared access failure preserves the target, skips then_run, and re
279
307
  },
280
308
  { toolCallId: "shared-retry", ctx: { cwd: dir } },
281
309
  );
282
- assert.match(
283
- result.content.map((item: any) => (item.type === "text" ? item.text : "")).join("\n"),
284
- /then_run:succeeded/,
285
- );
310
+ assert.equal(result.isError, undefined);
311
+ assert.equal(reportOf(result)?.then_run?.status, "succeeded");
286
312
  assert.equal(commandRuns, 1);
287
313
  assert.equal(await readFile(target, "utf8"), "replacement\n");
288
314
  }));
@@ -318,7 +344,7 @@ test("commit result binds base, published, and observed revisions", async () =>
318
344
  const baseRevision = await fileRevision(target);
319
345
  const result = await commitFile(target, "after\n", {
320
346
  mode: "overwrite",
321
- expectedRevision: baseRevision,
347
+ baseRevision,
322
348
  });
323
349
  assert.equal(result.baseRevision, baseRevision);
324
350
  assert.equal(result.publishedRevision, byteRevision("after\n"));
@@ -335,8 +361,10 @@ test("commit rejects lossy UTF-8 output before modifying or creating files", asy
335
361
  for (const path of [existing, missing]) {
336
362
  await assert.rejects(
337
363
  commitFile(path, content, { mode: path === existing ? "overwrite" : "create" }),
338
- (error: any) =>
339
- error.publication === "NOT_PUBLISHED" && /INVALID_UNICODE/.test(error.message),
364
+ (error: unknown) =>
365
+ error instanceof FileMutationError &&
366
+ error.publication === "NOT_PUBLISHED" &&
367
+ error.errorCode === "INVALID_UNICODE",
340
368
  );
341
369
  }
342
370
  }
@@ -357,7 +385,7 @@ test("identical commits preserve the file and still enforce mode, revision, and
357
385
  const revision = await fileRevision(target);
358
386
  const result = await commitFile(target, content, {
359
387
  mode: "overwrite",
360
- expectedRevision: revision,
388
+ baseRevision: revision,
361
389
  });
362
390
  assert.deepEqual(result, {
363
391
  created: false,
@@ -373,8 +401,13 @@ test("identical commits preserve the file and still enforce mode, revision, and
373
401
  );
374
402
  await assert.rejects(commitFile(target, content, { mode: "create" }), /already exists/);
375
403
  await assert.rejects(
376
- commitFile(target, content, { mode: "overwrite", expectedRevision: "stale" }),
377
- /expectedRevision/,
404
+ commitFile(target, content, { mode: "overwrite", baseRevision: "stale" }),
405
+ (error: unknown) =>
406
+ error instanceof FileMutationError &&
407
+ error.errorCode === "FILE_CHANGED" &&
408
+ error.stage === "prepare" &&
409
+ error.publication === "NOT_PUBLISHED" &&
410
+ /before publication/.test(error.message),
378
411
  );
379
412
  await assert.rejects(
380
413
  commitFile(target, content, { mode: "overwrite", signal: AbortSignal.abort() }),
@@ -390,12 +423,12 @@ test("identical commits preserve the file and still enforce mode, revision, and
390
423
  assert.equal(created.publication, "PUBLISHED");
391
424
  }));
392
425
 
393
- test("no-op commit with knownBeforeRevision validates disk revision against external modifications", async () =>
426
+ test("no-op commit with baseRevision validates disk revision against external modifications", async () =>
394
427
  withTemp(async (dir) => {
395
428
  const target = join(dir, "target.txt");
396
429
  const initialContent = "line 1\nline 2\n";
397
430
  await writeFile(target, initialContent);
398
- const snapshot = await readEditableSnapshot(target, "target.txt");
431
+ const snapshot = await readEditableSnapshot(target);
399
432
  assert.equal(snapshot.text, initialContent);
400
433
 
401
434
  // External process modifies target after snapshot is read.
@@ -404,44 +437,44 @@ test("no-op commit with knownBeforeRevision validates disk revision against exte
404
437
 
405
438
  // A no-op replacement (same text as snapshot) must reject because disk has changed.
406
439
  await assert.rejects(
407
- commitReplacement(target, "target.txt", snapshot.text, snapshot.baseRevision),
440
+ commitReplacement(target, snapshot.text, snapshot.baseRevision),
408
441
  (error: unknown) =>
409
442
  error instanceof FileMutationError &&
410
443
  error.stage === "prepare" &&
411
444
  error.publication === "NOT_PUBLISHED" &&
412
- /expectedRevision/.test(error.message),
445
+ error.errorCode === "FILE_CHANGED" &&
446
+ /after it was read/.test(error.message),
413
447
  );
414
448
  // Disk content must remain untouched by the rejected commit.
415
449
  assert.equal(await readFile(target, "utf8"), "line 1\nmodified line 2\n");
416
450
  assert.equal(await fileRevision(target), externalRevision);
417
451
 
418
- // Direct commitFile with knownBeforeRevision and matching expectedRevision also rejects.
452
+ // The direct commit path enforces the same snapshot revision.
419
453
  await assert.rejects(
420
454
  commitFile(target, snapshot.text, {
421
455
  mode: "overwrite",
422
- expectedRevision: snapshot.baseRevision,
423
- knownBeforeRevision: snapshot.baseRevision,
456
+ baseRevision: snapshot.baseRevision,
424
457
  }),
425
458
  (error: unknown) =>
426
459
  error instanceof FileMutationError &&
427
460
  error.stage === "prepare" &&
428
461
  error.publication === "NOT_PUBLISHED" &&
429
- /expectedRevision/.test(error.message),
462
+ error.errorCode === "FILE_CHANGED" &&
463
+ /after it was read/.test(error.message),
430
464
  );
431
465
 
432
- // Without expectedRevision, commitFile recognizes disk has changed and publishes the content.
466
+ // Without baseRevision, overwrite hashes the current target instead of using the snapshot.
433
467
  const overwriteResult = await commitFile(target, snapshot.text, {
434
468
  mode: "overwrite",
435
- knownBeforeRevision: snapshot.baseRevision,
436
469
  });
437
470
  assert.equal(overwriteResult.publication, "PUBLISHED");
471
+ assert.equal(overwriteResult.baseRevision, externalRevision);
438
472
  assert.equal(await readFile(target, "utf8"), initialContent);
439
473
 
440
474
  // When target does not change externally, no-op commit succeeds and confirms observed revision.
441
- const freshSnapshot = await readEditableSnapshot(target, "target.txt");
475
+ const freshSnapshot = await readEditableSnapshot(target);
442
476
  const noopResult = await commitReplacement(
443
477
  target,
444
- "target.txt",
445
478
  freshSnapshot.text,
446
479
  freshSnapshot.baseRevision,
447
480
  );
@@ -451,16 +484,16 @@ test("no-op commit with knownBeforeRevision validates disk revision against exte
451
484
  assert.equal(noopResult.observedRevision, freshSnapshot.baseRevision);
452
485
  }));
453
486
 
454
- test("no-op commit with knownBeforeRevision rejects if target was deleted externally", async () =>
487
+ test("no-op commit with baseRevision rejects if target was deleted externally", async () =>
455
488
  withTemp(async (dir) => {
456
489
  const target = join(dir, "deleted.txt");
457
490
  await writeFile(target, "content\n");
458
- const snapshot = await readEditableSnapshot(target, "deleted.txt");
491
+ const snapshot = await readEditableSnapshot(target);
459
492
 
460
493
  await rm(target);
461
494
 
462
495
  await assert.rejects(
463
- commitReplacement(target, "deleted.txt", snapshot.text, snapshot.baseRevision),
496
+ commitReplacement(target, snapshot.text, snapshot.baseRevision),
464
497
  (error: unknown) =>
465
498
  error instanceof FileMutationError &&
466
499
  error.stage === "prepare" &&