@criogaid/pi-hashline-edit 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +76 -220
  2. package/README.zh-CN.md +166 -0
  3. package/package.json +2 -1
  4. package/src/core/AGENTS.md +1 -1
  5. package/src/core/apply.test.ts +171 -133
  6. package/src/core/apply.ts +34 -31
  7. package/src/core/case-fold.test.ts +11 -7
  8. package/src/core/case-fold.ts +2 -6
  9. package/src/core/character-set.ts +14 -0
  10. package/src/core/hash.test.ts +1 -25
  11. package/src/core/hash.ts +0 -5
  12. package/src/core/lines.ts +25 -7
  13. package/src/core/ranges.test.ts +49 -9
  14. package/src/core/ranges.ts +15 -4
  15. package/src/core/regex.ts +36 -6
  16. package/src/core/replace-schema.ts +18 -11
  17. package/src/core/replace.test.ts +90 -0
  18. package/src/core/replace.ts +6 -5
  19. package/src/core/search-analysis.test.ts +9 -0
  20. package/src/core/search-analysis.ts +35 -31
  21. package/src/core/search.ts +1 -1
  22. package/src/core/types.ts +6 -8
  23. package/src/index.ts +2 -4
  24. package/src/integration/config-registration.test.ts +18 -10
  25. package/src/integration/fusion-dispatch.test.ts +41 -22
  26. package/src/integration/grep-flags.test.ts +6 -13
  27. package/src/integration/grep-js-completeness.test.ts +41 -31
  28. package/src/integration/grep-order.test.ts +78 -0
  29. package/src/integration/grep-path-matrix.test.ts +4 -3
  30. package/src/integration/grep-regex.test.ts +13 -16
  31. package/src/integration/grep-rg.test.ts +132 -92
  32. package/src/integration/grep-scan-budget.test.ts +27 -5
  33. package/src/integration/large-text.test.ts +4 -5
  34. package/src/integration/model-budget-lifecycle.test.ts +26 -8
  35. package/src/integration/mutation-file-types.test.ts +0 -1
  36. package/src/integration/mutation-file-types.testing.ts +2 -2
  37. package/src/integration/npm-package.testing.ts +7 -33
  38. package/src/integration/publication-read-changes.test.ts +60 -28
  39. package/src/integration/replace-package.testing.ts +4 -1
  40. package/src/integration/replace-worker-boundary.test.ts +34 -24
  41. package/src/integration/report-replay.test.ts +3 -3
  42. package/src/integration/utf8-errors.test.ts +3 -3
  43. package/src/pi/AGENTS.md +13 -14
  44. package/src/pi/action-fusion.test.ts +75 -220
  45. package/src/pi/action-fusion.ts +37 -74
  46. package/src/pi/anchor-format.test.ts +16 -0
  47. package/src/pi/anchor-format.ts +7 -3
  48. package/src/pi/argument-diagnostics.ts +35 -186
  49. package/src/pi/argument-error.testing.ts +7 -4
  50. package/src/pi/argument-error.ts +5 -7
  51. package/src/pi/argument-format.test.ts +25 -38
  52. package/src/pi/argument-validation.test.ts +446 -420
  53. package/src/pi/argument-validation.ts +4 -5
  54. package/src/pi/budgets.ts +5 -7
  55. package/src/pi/config.test.ts +15 -5
  56. package/src/pi/config.ts +6 -6
  57. package/src/pi/diagnostic-buffer.test.ts +2 -3
  58. package/src/pi/diagnostic-buffer.ts +2 -2
  59. package/src/pi/edit-tool.ts +175 -121
  60. package/src/pi/execute.test.ts +170 -227
  61. package/src/pi/failure-context.test.ts +61 -16
  62. package/src/pi/failure-context.ts +13 -10
  63. package/src/pi/failure-path.test.ts +115 -50
  64. package/src/pi/file-commit.test.ts +30 -46
  65. package/src/pi/file-commit.ts +11 -40
  66. package/src/pi/file-revision.ts +25 -0
  67. package/src/pi/fusion-card-interaction.test.ts +143 -0
  68. package/src/pi/fusion-card.test.ts +193 -37
  69. package/src/pi/fusion-card.ts +186 -116
  70. package/src/pi/grep-candidates.ts +25 -27
  71. package/src/pi/grep-matrix.test.ts +1 -1
  72. package/src/pi/grep-order.test.ts +323 -0
  73. package/src/pi/grep-output.testing.ts +24 -0
  74. package/src/pi/grep-output.ts +62 -69
  75. package/src/pi/grep-render.ts +4 -1
  76. package/src/pi/grep-scope.ts +1 -1
  77. package/src/pi/grep-search.ts +85 -56
  78. package/src/pi/grep-tool.ts +17 -48
  79. package/src/pi/grep.test.ts +271 -170
  80. package/src/pi/mutation-outcome.testing.ts +2 -3
  81. package/src/pi/mutation-result.test.ts +26 -19
  82. package/src/pi/mutation-result.ts +25 -40
  83. package/src/pi/mutation-runner.ts +9 -17
  84. package/src/pi/output-budgets.test.ts +27 -18
  85. package/src/pi/path.ts +3 -1
  86. package/src/pi/pi.test.ts +7 -6
  87. package/src/pi/read-tool.ts +32 -80
  88. package/src/pi/read.test.ts +145 -92
  89. package/src/pi/regex-worker-contract.ts +5 -23
  90. package/src/pi/regex-worker.ts +4 -10
  91. package/src/pi/registration.test.ts +106 -67
  92. package/src/pi/render.test.ts +239 -109
  93. package/src/pi/render.ts +42 -22
  94. package/src/pi/replace-regex.ts +3 -5
  95. package/src/pi/replace-tool.ts +29 -27
  96. package/src/pi/replace-worker-contract.ts +3 -12
  97. package/src/pi/replace.test.ts +144 -226
  98. package/src/pi/report-cause.ts +23 -19
  99. package/src/pi/report-failure.testing.ts +2 -2
  100. package/src/pi/report-schema.ts +60 -45
  101. package/src/pi/report.test.ts +138 -71
  102. package/src/pi/report.ts +40 -43
  103. package/src/pi/result-safety.test.ts +186 -131
  104. package/src/pi/rg-process.test.ts +36 -10
  105. package/src/pi/rg-process.ts +9 -3
  106. package/src/pi/rg-regex.test.ts +17 -0
  107. package/src/pi/rg-regex.ts +28 -22
  108. package/src/pi/text-stream.test.ts +35 -36
  109. package/src/pi/text-stream.ts +14 -14
  110. package/src/pi/tool-call.testing.ts +17 -7
  111. package/src/pi/write-tool.ts +9 -11
  112. package/src/pi/write.test.ts +43 -75
  113. package/src/testing/regex-translation-corpus.ts +16 -0
  114. package/src/testing/rg-line-listing.fixture.mjs +1 -1
  115. package/src/integration/forget-grep.test.ts +0 -148
  116. package/src/pi/forget-tool.ts +0 -367
  117. package/src/pi/forget.test.ts +0 -630
  118. package/src/pi/forget.testing.ts +0 -201
@@ -6,13 +6,15 @@ import { test } from "node:test";
6
6
  import { createActionFusionExecutor } from "./action-fusion.ts";
7
7
  import { DEFAULT_CONFIG } from "./config.ts";
8
8
  import { makeEditOverride } from "./edit-tool.ts";
9
- import { makeGrepOverride } from "./grep-tool.ts";
9
+ import { makeGrepOverride, makeGrepOverrideWithBackend } from "./grep-tool.ts";
10
10
  import { makeReadOverride } from "./read-tool.ts";
11
11
  import { makeReplaceTool } from "./replace-tool.ts";
12
12
  import { makeWriteOverride } from "./write-tool.ts";
13
13
  import { callTool } from "./tool-call.testing.ts";
14
14
  import { argumentError, rejectsArgument } from "./argument-error.testing.ts";
15
15
  import { POSITIVE_SAFE_INTEGER } from "./schema.ts";
16
+ import { reportOf } from "./report.ts";
17
+ import { createAnchorFormatter } from "./anchor-format.ts";
16
18
 
17
19
  const invalidCommand = { command: "", timeout: 0 };
18
20
  const anchorHash = "A".repeat(DEFAULT_CONFIG.hashLen);
@@ -37,10 +39,10 @@ const cases = [
37
39
  makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
38
40
  args: {
39
41
  edits: [
40
- { op: "replace", anchor: `1#${anchorHash}`, body: [] },
41
- { op: "append", body: ["bad\0", "bad\ud800"] },
42
- { op: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#${anchorHash}`, end: "1#A" },
43
- { op: "append", body: [] },
42
+ { action: "replace", anchor: `1#${anchorHash}`, body: [] },
43
+ { action: "append", body: ["bad\0", "bad\ud800"] },
44
+ { action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#${anchorHash}`, end: "1#A" },
45
+ { action: "append", body: [] },
44
46
  ],
45
47
  then_run: invalidCommand,
46
48
  },
@@ -61,8 +63,8 @@ const cases = [
61
63
  makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
62
64
  args: {
63
65
  replacements: [
64
- { find: "(", replace: "bad\0", regex: true },
65
- { find: "[", replace: "bad\ud800", regex: true },
66
+ { find: "(", replace: "bad\0", literal: false },
67
+ { find: "[", replace: "bad\ud800", literal: false },
66
68
  ],
67
69
  then_run: invalidCommand,
68
70
  },
@@ -94,10 +96,11 @@ for (const scenario of cases) {
94
96
  let commandRuns = 0;
95
97
  const fusion = createActionFusionExecutor(async () => {
96
98
  commandRuns++;
97
- return { status: "succeeded", output: "ran" };
99
+ return { status: "succeeded", result: { content: [{ type: "text", text: "ran" }] } };
98
100
  });
99
101
  const tool = scenario.make(cwd, fusion);
100
102
  const args: unknown = JSON.parse(JSON.stringify({ path, ...scenario.args, unexpected: true }));
103
+ const snapshot = structuredClone(args);
101
104
  await assert.rejects(callTool(tool, args), (error: unknown) => {
102
105
  const result = argumentError(error);
103
106
  assert.deepEqual(
@@ -106,117 +109,303 @@ for (const scenario of cases) {
106
109
  );
107
110
  return true;
108
111
  });
112
+ assert.deepEqual(args, snapshot);
109
113
  assert.equal(await readFile(path, "utf8"), original);
110
114
  assert.equal(commandRuns, 0);
111
115
  });
112
116
  }
113
117
 
114
- test("known edit operations with empty bodies → report the actionable issue once", async () => {
115
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
116
- for (const op of ["replace", "insert_after", "insert_before", "append", "prepend"]) {
117
- const operation = {
118
- op,
119
- ...(op === "append" || op === "prepend" ? {} : { anchor: `22#${anchorHash}` }),
120
- body: [],
121
- };
122
- await assert.rejects(
123
- callTool(tool, { path: "unused.txt", edits: [operation] }),
124
- (error: unknown) => {
125
- const result = argumentError(error);
126
- assert.equal(result.issues.length, 1);
127
- assert.equal(result.issues[0].field, "edits[0].body");
128
- assert.match(result.issues[0].reason, /empty/);
129
- assert.match(
130
- result.issues[0].fix ?? "",
131
- op === "replace" ? /delete/ : /supply at least one line/,
132
- );
133
- assert.equal(result.schemaLimited, undefined);
134
- return true;
135
- },
136
- );
118
+ test("grep and replace flags → accept V8 modes and reject invalid flags before patterns or file reads", async (t) => {
119
+ const cwd = await mkdtemp(join(tmpdir(), "hashline-flags-"));
120
+ t.after(() => rm(cwd, { recursive: true, force: true }));
121
+ const path = join(cwd, "missing.txt");
122
+ const tools = [
123
+ {
124
+ tool: makeGrepOverride(cwd, DEFAULT_CONFIG),
125
+ args: (find: string, literal: boolean, flags: string) => ({ pattern: find, literal, flags }),
126
+ field: "flags",
127
+ },
128
+ {
129
+ tool: makeReplaceTool(cwd, DEFAULT_CONFIG),
130
+ args: (find: string, literal: boolean, flags: string) => ({
131
+ replacements: [
132
+ { literal: true, find: "valid", replace: "first" },
133
+ { find, replace: "second", literal, flags },
134
+ ],
135
+ }),
136
+ field: "replacements[1].flags",
137
+ },
138
+ ];
139
+ for (const { tool, args, field } of tools) {
140
+ for (const literal of [false, true]) {
141
+ for (const flags of ["v", "uv", "x"]) {
142
+ await t.test(`${tool.name}/${literal ? "literal" : "regex"}/${flags}`, async () => {
143
+ const input = { path, ...args(flags === "v" ? "a" : "(", literal, flags) };
144
+ if (flags === "v") {
145
+ const result = await callTool(tool, input);
146
+ assert.equal(reportOf(result, tool.name)?.error, "PATH_NOT_FOUND");
147
+ } else {
148
+ await assert.rejects(callTool(tool, input), (error: unknown) => {
149
+ const report = argumentError(error);
150
+ assert.equal(report.issues.length, 1);
151
+ assert.equal(report.issues[0].field, field);
152
+ assert.match(report.issues[0].reason, /Invalid flags supplied to RegExp constructor/);
153
+ return true;
154
+ });
155
+ }
156
+ });
157
+ }
158
+ }
137
159
  }
138
160
  });
139
161
 
140
- test("known edit branch has independent errors → report them without unrelated branches", async () => {
141
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG, createActionFusionExecutor());
142
- await assert.rejects(
143
- callTool(tool, {
144
- path: "unused.txt",
145
- edits: [
146
- { op: "insert_after", body: [], extra: true },
147
- { op: "delete", anchor: `2#${anchorHash}`, body: ["wrong"] },
148
- { op: "replace", anchor: "bad", body: ["valid"] },
149
- ],
150
- then_run: invalidCommand,
151
- unexpected: true,
152
- }),
153
- (error: unknown) => {
154
- const result = argumentError(error);
155
- assert.deepEqual(
156
- new Set(result.issues.map((issue) => issue.field)),
157
- new Set([
158
- "edits[0].body",
159
- "edits[0].anchor",
160
- "edits[0].extra",
161
- "edits[1].body",
162
- "edits[2].anchor",
163
- "then_run.command",
164
- "then_run.timeout",
165
- "unexpected",
166
- ]),
167
- );
168
- return true;
169
- },
170
- );
171
- });
162
+ const editFields = ["anchor", "end", "body", "before", "after"] as const;
163
+ type EditField = (typeof editFields)[number];
164
+ const fieldValues = {
165
+ anchor: `1#${anchorHash}`,
166
+ end: `2#${anchorHash}`,
167
+ body: ["text"],
168
+ before: `3#${anchorHash}`,
169
+ after: `3#${anchorHash}`,
170
+ };
171
+ const actionCases: readonly {
172
+ action: string;
173
+ required: readonly EditField[];
174
+ optional: readonly EditField[];
175
+ transfer?: boolean;
176
+ expected: string;
177
+ }[] = [
178
+ {
179
+ action: "replace",
180
+ required: ["anchor", "body"],
181
+ optional: ["end"],
182
+ expected: "text\nbeta\ngamma\n",
183
+ },
184
+ { action: "delete", required: ["anchor"], optional: ["end"], expected: "beta\ngamma\n" },
185
+ {
186
+ action: "insert_after",
187
+ required: ["anchor", "body"],
188
+ optional: [],
189
+ expected: "alpha\ntext\nbeta\ngamma\n",
190
+ },
191
+ {
192
+ action: "insert_before",
193
+ required: ["anchor", "body"],
194
+ optional: [],
195
+ expected: "text\nalpha\nbeta\ngamma\n",
196
+ },
197
+ {
198
+ action: "copy",
199
+ required: ["anchor"],
200
+ optional: ["end", "before", "after"],
201
+ transfer: true,
202
+ expected: "alpha\nbeta\nalpha\ngamma\n",
203
+ },
204
+ {
205
+ action: "move",
206
+ required: ["anchor"],
207
+ optional: ["end", "before", "after"],
208
+ transfer: true,
209
+ expected: "beta\nalpha\ngamma\n",
210
+ },
211
+ { action: "append", required: ["body"], optional: [], expected: "alpha\nbeta\ngamma\ntext\n" },
212
+ { action: "prepend", required: ["body"], optional: [], expected: "text\nalpha\nbeta\ngamma\n" },
213
+ ];
214
+
215
+ for (const scenario of actionCases) {
216
+ test(`${scenario.action} field matrix → requires its fields and rejects fields it does not take`, async (t) => {
217
+ const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
218
+ const fields = schemaProperties(tool).edits?.items?.properties;
219
+ const choice = fields?.action?.anyOf?.find((option) => option.const === scenario.action);
220
+ assert.ok(choice?.description);
221
+ const description = choice.description;
222
+ const valid = {
223
+ action: scenario.action,
224
+ ...Object.fromEntries(scenario.required.map((field) => [field, fieldValues[field]])),
225
+ ...(scenario.transfer ? { before: fieldValues.before } : {}),
226
+ };
227
+ for (const field of scenario.required) {
228
+ for (const value of [undefined, null]) {
229
+ await t.test(
230
+ `${field} is ${value === null ? "null" : "missing"} → reports the required field`,
231
+ async () => {
232
+ const edit: Record<string, unknown> = Object.fromEntries(
233
+ Object.entries(valid).filter(([key]) => key !== field),
234
+ );
235
+ if (value === null) edit[field] = null;
236
+ const args = { path: "unused.txt", edits: [edit] };
237
+ await assert.rejects(callTool(tool, args), (error: unknown) => {
238
+ assert.deepEqual(argumentError(error).issues, [
239
+ {
240
+ field: `edits[0].${field}`,
241
+ reason: `is required for "${scenario.action}"`,
242
+ fix: fields?.[field]?.description,
243
+ },
244
+ ]);
245
+ return true;
246
+ });
247
+ },
248
+ );
249
+ }
250
+ }
251
+ for (const field of editFields.filter(
252
+ (field) => !scenario.required.includes(field) && !scenario.optional.includes(field),
253
+ )) {
254
+ await t.test(`${field} is supplied → reports only that forbidden field`, async () => {
255
+ await assert.rejects(
256
+ callTool(tool, {
257
+ path: "unused.txt",
258
+ edits: [{ ...valid, [field]: fieldValues[field] }],
259
+ }),
260
+ (error: unknown) => {
261
+ assert.deepEqual(argumentError(error).issues, [
262
+ {
263
+ field: `edits[0].${field}`,
264
+ reason: "is not allowed",
265
+ fix: `"${scenario.action}": ${description}`,
266
+ },
267
+ ]);
268
+ assert.match(description, /Takes /);
269
+ return true;
270
+ },
271
+ );
272
+ });
273
+ }
274
+ });
275
+ }
172
276
 
173
- test("edit operation tag is missing or invalid → report its field once without guessing a branch", async () => {
277
+ test("edit action is missing or invalid → reports choices without guessing field permissions", async () => {
174
278
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
175
- for (const tag of [undefined, "unknown", null, 42, {}]) {
279
+ const choices = schemaProperties(tool).edits?.items?.properties?.action?.anyOf ?? [];
280
+ assert.equal(choices.length, actionCases.length);
281
+ for (const action of [undefined, "unknown", null, 42, {}]) {
176
282
  await assert.rejects(
177
283
  callTool(tool, {
178
284
  path: "unused.txt",
179
285
  edits: [
180
- { ...(tag === undefined ? {} : { op: tag }), anchor: `1#${anchorHash}`, body: ["valid"] },
286
+ {
287
+ ...(action === undefined ? {} : { action }),
288
+ anchor: `1#${anchorHash}`,
289
+ body: ["valid"],
290
+ },
181
291
  ],
182
292
  }),
183
293
  (error: unknown) => {
184
- const result = argumentError(error);
185
- assert.equal(result.issues.length, 1);
186
- assert.equal(result.issues[0].field, "edits[0].op");
187
- if (tag === undefined) assert.match(result.issues[0].reason, /required/);
188
- else
189
- for (const op of [
190
- "replace",
191
- "delete",
192
- "insert_before",
193
- "insert_after",
194
- "append",
195
- "prepend",
196
- ])
197
- assert.ok(result.issues[0].reason.includes(op));
294
+ const { issues } = argumentError(error);
295
+ assert.equal(issues.length, 1);
296
+ const issue = issues[0];
297
+ assert.equal(issue.field, "edits[0].action");
298
+ if (action === undefined) {
299
+ assert.equal(issue.reason, "is required");
300
+ for (const choice of choices) {
301
+ assert.ok(choice.description);
302
+ inOrder(issue.fix ?? "", [JSON.stringify(choice.const), choice.description]);
303
+ }
304
+ } else {
305
+ assert.equal(
306
+ issue.reason,
307
+ `expected one of: ${choices.map((choice) => JSON.stringify(choice.const)).join(", ")}`,
308
+ );
309
+ assert.equal(issue.fix, undefined);
310
+ }
198
311
  return true;
199
312
  },
200
313
  );
201
314
  }
202
315
  });
203
316
 
204
- test("edit range end is null → preserve the original rejection and its field diagnostic", async () => {
205
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
206
- for (const operation of [
207
- { op: "delete", anchor: `1#${anchorHash}`, end: null },
208
- { op: "replace", anchor: `1#${anchorHash}`, end: null, body: ["ok"] },
209
- { op: "replace", anchor: `1#${anchorHash}`, end: null, body: [] },
210
- ]) {
211
- await assert.rejects(
212
- callTool(tool, { path: "unused.txt", edits: [operation] }),
213
- (error: unknown) => {
214
- const result = argumentError(error);
215
- assert.deepEqual(
216
- result.issues.map((issue) => issue.field),
217
- operation.body?.length === 0 ? ["edits[0].body", "edits[0].end"] : ["edits[0].end"],
218
- );
219
- return true;
317
+ test("optional edit fields and then_run are null with Fusion → publishes bytes without rewriting caller arguments", async (t) => {
318
+ const cwd = await mkdtemp(join(tmpdir(), "hashline-null-"));
319
+ t.after(() => rm(cwd, { recursive: true, force: true }));
320
+ const path = join(cwd, "target.txt");
321
+ let commandRuns = 0;
322
+ const fusion = createActionFusionExecutor(async () => {
323
+ commandRuns++;
324
+ return { status: "succeeded", result: { content: [{ type: "text", text: "ran" }] } };
325
+ });
326
+ const tool = makeEditOverride(cwd, DEFAULT_CONFIG, fusion);
327
+ const anchors = createAnchorFormatter(DEFAULT_CONFIG.hashLen);
328
+ for (const scenario of actionCases) {
329
+ for (const side of scenario.transfer ? ["before", "after"] : [undefined]) {
330
+ await t.test(
331
+ `${scenario.action}${side ? `/${side}` : ""} → accepts null optional fields`,
332
+ async () => {
333
+ await writeFile(path, "alpha\nbeta\ngamma\n");
334
+ const edit = {
335
+ action: scenario.action,
336
+ ...Object.fromEntries(editFields.map((field) => [field, null])),
337
+ ...Object.fromEntries(
338
+ scenario.required.map((field) => [
339
+ field,
340
+ field === "body" ? ["text"] : anchors.token(1, "alpha"),
341
+ ]),
342
+ ),
343
+ ...(side
344
+ ? {
345
+ [side]: anchors.token(
346
+ side === "before" ? 3 : 2,
347
+ side === "before" ? "gamma" : "beta",
348
+ ),
349
+ }
350
+ : {}),
351
+ };
352
+ const args = { path, edits: [edit], then_run: null };
353
+ const snapshot = structuredClone(args);
354
+ const result = await callTool(tool, args);
355
+ assert.equal(result.isError, undefined);
356
+ assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
357
+ assert.equal(reportOf(result, "edit")?.then_run, undefined);
358
+ assert.deepEqual(await readFile(path), Buffer.from(scenario.expected));
359
+ assert.deepEqual(args, snapshot);
360
+ },
361
+ );
362
+ }
363
+ }
364
+ assert.equal(commandRuns, 0);
365
+ });
366
+
367
+ test("edit batch has 64 or 65 operations → publishes the maximum or rejects the oversized batch unchanged", async (t) => {
368
+ const cwd = await mkdtemp(join(tmpdir(), "hashline-edit-limit-"));
369
+ t.after(() => rm(cwd, { recursive: true, force: true }));
370
+ const path = join(cwd, "target.txt");
371
+ const tool = makeEditOverride(cwd, DEFAULT_CONFIG);
372
+ const maxEdits: unknown = Reflect.get(tool.parameters.properties.edits, "maxItems");
373
+ assert.ok(typeof maxEdits === "number");
374
+ assert.equal(maxEdits, 64);
375
+ const lines = Array.from({ length: maxEdits + 2 }, (_, i) => `line ${i + 1}`);
376
+ const original = Buffer.from(`${lines.join("\n")}\n`);
377
+ const anchors = createAnchorFormatter(DEFAULT_CONFIG.hashLen);
378
+ for (const count of [maxEdits, maxEdits + 1]) {
379
+ await t.test(
380
+ `${count} edits → ${count === maxEdits ? "publishes distinct deletions" : `rejects edits above max${maxEdits}`}`,
381
+ async () => {
382
+ await writeFile(path, original);
383
+ const args = {
384
+ path,
385
+ edits: lines
386
+ .slice(0, count)
387
+ .map((line, i) => ({ action: "delete", anchor: anchors.token(i + 1, line) })),
388
+ };
389
+ const snapshot = structuredClone(args);
390
+ if (count === maxEdits) {
391
+ const result = await callTool(tool, args);
392
+ assert.equal(result.isError, undefined);
393
+ assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
394
+ assert.deepEqual(await readFile(path), Buffer.from(`${lines.slice(count).join("\n")}\n`));
395
+ } else {
396
+ await assert.rejects(callTool(tool, args), (error: unknown) => {
397
+ const report = argumentError(error);
398
+ assert.equal(report.issues.length, 1);
399
+ assert.equal(report.issues[0].field, "edits");
400
+ assert.match(
401
+ report.issues[0].reason,
402
+ new RegExp(`must not have more than ${maxEdits} items`),
403
+ );
404
+ return true;
405
+ });
406
+ assert.deepEqual(await readFile(path), original);
407
+ }
408
+ assert.deepEqual(args, snapshot);
220
409
  },
221
410
  );
222
411
  }
@@ -264,87 +453,6 @@ test("literal enum is invalid → report the permitted values once", async () =>
264
453
  );
265
454
  });
266
455
 
267
- test("specific child errors exist → omit parent summaries and retain independent fields", async () => {
268
- await assert.rejects(
269
- callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG, createActionFusionExecutor()), {
270
- path: "unused.txt",
271
- edits: [{ op: "insert_after", body: ["ok"], extra: true }, { op: "unknown" }],
272
- then_run: { command: "echo ok", extra: true },
273
- unexpected: true,
274
- }),
275
- (error: unknown) => {
276
- const result = argumentError(error);
277
- assert.deepEqual(
278
- new Set(result.issues.map((issue) => issue.field)),
279
- new Set([
280
- "edits[0].anchor",
281
- "edits[0].extra",
282
- "edits[1].op",
283
- "then_run.extra",
284
- "unexpected",
285
- ]),
286
- );
287
- return true;
288
- },
289
- );
290
- });
291
-
292
- test("operation shape does not permit a field → omit inapplicable semantic advice", async () => {
293
- for (const edits of [
294
- [{ op: "unknown", anchor: "1#A", body: [] }],
295
- [{ op: "delete", anchor: `1#${anchorHash}`, body: [] }],
296
- "bad",
297
- ]) {
298
- await assert.rejects(
299
- callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), { path: "unused.txt", edits }),
300
- (error: unknown) => {
301
- const result = argumentError(error);
302
- assert.equal(result.issues.length, 1);
303
- assert.doesNotMatch(result.issues[0].reason, /empty|hash length mismatch/);
304
- return true;
305
- },
306
- );
307
- }
308
- });
309
-
310
- test("Pi coerces valid fields while another field fails → do not diagnose the original value", async () => {
311
- await assert.rejects(
312
- callTool(makeReadOverride(process.cwd(), DEFAULT_CONFIG), {
313
- path: "unused.txt",
314
- offset: "1",
315
- limit: 0,
316
- }),
317
- (error: unknown) => {
318
- const result = argumentError(error);
319
- assert.deepEqual(
320
- result.issues.map((issue) => issue.field),
321
- ["limit"],
322
- );
323
- assert.equal(result.schemaLimited, undefined);
324
- return true;
325
- },
326
- );
327
- });
328
-
329
- test("multiple required fields are missing → report every missing field", async () => {
330
- await assert.rejects(
331
- callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
332
- path: "unused.txt",
333
- edits: [{ op: "replace" }],
334
- }),
335
- (error: unknown) => {
336
- const result = argumentError(error);
337
- assert.deepEqual(
338
- new Set(result.issues.map((issue) => issue.field)),
339
- new Set(["edits[0].anchor", "edits[0].body"]),
340
- );
341
- assert.ok(result.issues.every((issue) => /required/.test(issue.reason)));
342
- assert.equal(result.schemaLimited, undefined);
343
- return true;
344
- },
345
- );
346
- });
347
-
348
456
  test("unknown field names contain punctuation, numeric keys or newlines → quote property keys without treating them as array indices", async () => {
349
457
  for (const key of ["odd.\nfield/~", "0", "01", "a/b", "a~b", 'quote"field']) {
350
458
  await assert.rejects(
@@ -366,12 +474,12 @@ test("one field exhausts native diagnostics → label the limit and retain anoth
366
474
  await assert.rejects(
367
475
  callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
368
476
  path: "",
369
- edits: Array.from({ length: 30 }, () => ({ op: "unknown" })),
477
+ edits: Array.from({ length: 30 }, () => ({ action: "unknown" })),
370
478
  }),
371
479
  (error: unknown) => {
372
480
  const result = argumentError(error);
373
481
  assert.ok(result.issues.some((issue) => issue.field === "path"));
374
- assert.ok(result.issues.some((issue) => issue.field === "edits[0].op"));
482
+ assert.ok(result.issues.some((issue) => issue.field === "edits[0].action"));
375
483
  assert.equal(result.schemaLimited, true);
376
484
  return true;
377
485
  },
@@ -382,7 +490,7 @@ test("one field violates independent constraints → combine its reasons without
382
490
  await assert.rejects(
383
491
  callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
384
492
  path: "unused.txt",
385
- edits: [{ op: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#A` }],
493
+ edits: [{ action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#A` }],
386
494
  }),
387
495
  (error: unknown) => {
388
496
  const result = argumentError(error);
@@ -405,180 +513,153 @@ test("one field violates independent constraints → combine its reasons without
405
513
  );
406
514
  });
407
515
 
408
- test("copy/move destination tokens → reject unsafe positions and wrong hash lengths before reading", async () => {
516
+ test("edit fields contain semantic errors → checks only fields accepted by their action", async (t) => {
409
517
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
410
- for (const op of ["copy", "move"]) {
411
- for (const side of ["before", "after"]) {
412
- for (const [token, reason] of [
413
- [`${Number.MAX_SAFE_INTEGER + 1}#${anchorHash}`, /safe integer/],
414
- ["1#A", /has 1 hash characters/],
415
- ] as const) {
416
- await assert.rejects(
417
- callTool(tool, {
418
- path: "missing-copy-move.txt",
419
- edits: [{ op, anchor: `1#${anchorHash}`, [side]: token }],
420
- }),
421
- rejectsArgument(`edits[0].${side}`, reason),
518
+ for (const scenario of actionCases) {
519
+ const valid = {
520
+ action: scenario.action,
521
+ ...Object.fromEntries(scenario.required.map((field) => [field, fieldValues[field]])),
522
+ ...(scenario.transfer ? { before: fieldValues.before } : {}),
523
+ };
524
+ for (const field of editFields) {
525
+ const accepted = scenario.required.includes(field) || scenario.optional.includes(field);
526
+ const values: readonly (readonly [unknown, RegExp])[] =
527
+ field === "body"
528
+ ? [
529
+ [[], /empty/],
530
+ [["bad\0"], /NUL/],
531
+ [["bad\ud800"], /surrogate|UTF-8/],
532
+ ]
533
+ : [
534
+ [`${Number.MAX_SAFE_INTEGER + 1}#${anchorHash}`, /safe integer/],
535
+ ["1#A", /hash characters/],
536
+ ];
537
+ for (const [value, reason] of values) {
538
+ await t.test(
539
+ `${scenario.action}/${field}/${reason.source} → ${accepted ? "reports value error" : "reports only forbidden field"}`,
540
+ async () => {
541
+ const edit = {
542
+ ...Object.fromEntries(
543
+ Object.entries(valid).filter(
544
+ ([key]) => !(scenario.transfer && field === "after" && key === "before"),
545
+ ),
546
+ ),
547
+ [field]: value,
548
+ };
549
+ await assert.rejects(
550
+ callTool(tool, { path: "unused.txt", edits: [edit] }),
551
+ (error: unknown) => {
552
+ const report = argumentError(error);
553
+ assert.equal(report.issues.length, 1, JSON.stringify(report));
554
+ const issue = report.issues[0];
555
+ assert.equal(
556
+ issue.field,
557
+ `edits[0].${field}${accepted && field === "body" && Array.isArray(value) && value.length ? "[0]" : ""}`,
558
+ );
559
+ if (accepted) {
560
+ assert.match(issue.reason, reason);
561
+ if (field === "body" && Array.isArray(value) && value.length === 0) {
562
+ assert.match(
563
+ issue.fix ?? "",
564
+ scenario.action === "replace"
565
+ ? /"action":"delete"/
566
+ : /supply at least one line/,
567
+ );
568
+ }
569
+ } else {
570
+ assert.equal(issue.reason, "is not allowed");
571
+ assert.match(issue.fix ?? "", new RegExp(`^"${scenario.action}":.*Takes `));
572
+ }
573
+ return true;
574
+ },
575
+ );
576
+ },
422
577
  );
423
578
  }
424
579
  }
425
580
  }
426
581
  });
427
582
 
428
- test("copy/move illegal argument shapes → reject before any file access", async () => {
429
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
430
- const anchor = `1#${anchorHash}`;
431
- for (const op of ["copy", "move"]) {
432
- for (const fields of [
433
- {},
434
- { before: anchor, after: anchor },
435
- { before: anchor, body: ["unused"] },
436
- { after: anchor, end: null },
437
- { before: 1 },
438
- { before: anchor, end: "1#IXXX" },
439
- ]) {
583
+ test("copy/move destination count and field errors coexist → retains object and field diagnostics", async (t) => {
584
+ const cwd = await mkdtemp(join(tmpdir(), "hashline-object-diagnostics-"));
585
+ t.after(() => rm(cwd, { recursive: true, force: true }));
586
+ const tool = makeEditOverride(cwd, DEFAULT_CONFIG);
587
+ for (const scenario of [
588
+ {
589
+ edit: { action: "copy", anchor: "abc" },
590
+ field: "anchor",
591
+ reason: /must match pattern/,
592
+ fix: /LINE#HASH/,
593
+ },
594
+ {
595
+ edit: { action: "copy", anchor: `1#${anchorHash}`, extra: 1 },
596
+ field: "extra",
597
+ reason: /^is not allowed$/,
598
+ fix: /^Accepted fields:/,
599
+ },
600
+ {
601
+ edit: {
602
+ action: "move",
603
+ anchor: `1#${anchorHash}`,
604
+ end: "x",
605
+ before: `3#${anchorHash}`,
606
+ after: `4#${anchorHash}`,
607
+ },
608
+ field: "end",
609
+ reason: /must match pattern/,
610
+ fix: /LINE#HASH/,
611
+ },
612
+ ]) {
613
+ await t.test(`${scenario.edit.action}/${scenario.field} → retains both issues`, async () => {
440
614
  await assert.rejects(
441
- callTool(tool, { path: "missing-copy-move.txt", edits: [{ op, anchor, ...fields }] }),
615
+ callTool(tool, { path: "missing.txt", edits: [scenario.edit] }),
442
616
  (error: unknown) => {
443
- const result = argumentError(error);
444
- assert.equal(result.tool, "edit");
445
- assert(result.issues.length > 0);
446
- if ("body" in fields)
447
- assert(result.issues.some((issue) => issue.field === "edits[0].body"));
448
- if ("end" in fields)
449
- assert(result.issues.some((issue) => issue.field === "edits[0].end"));
450
- if (fields.before === 1)
451
- assert(result.issues.some((issue) => issue.field === "edits[0].before"));
617
+ const { issues } = argumentError(error);
618
+ assert.deepEqual(
619
+ issues.map((issue) => issue.field).sort(),
620
+ ["edits[0]", `edits[0].${scenario.field}`].sort(),
621
+ );
622
+ const item = issues.find((issue) => issue.field === "edits[0]");
623
+ assert.equal(item?.reason, "expected exactly one of: before, after");
624
+ assert.equal(item?.fix, undefined);
625
+ const field = issues.find((issue) => issue.field === `edits[0].${scenario.field}`);
626
+ assert.ok(field);
627
+ assert.match(field.reason, scenario.reason);
628
+ assert.match(field.fix ?? "", scenario.fix);
452
629
  return true;
453
630
  },
454
631
  );
455
- }
632
+ });
456
633
  }
457
634
  });
458
635
 
459
- const exactlyOneDestination = /expected exactly one of: before, after/;
460
-
461
- for (const op of ["copy", "move"] as const) {
462
- test(`${op} without a destination → one exactly-one issue without a description fix`, async () => {
463
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
464
- await assert.rejects(
465
- callTool(tool, {
466
- path: "unused.txt",
467
- edits: [{ op, anchor: `1#${anchorHash}` }],
468
- }),
469
- (error: unknown) => {
470
- const result = argumentError(error);
471
- assert.equal(result.issues.length, 1);
472
- assert.equal(result.issues[0].field, "edits[0]");
473
- assert.match(result.issues[0].reason, exactlyOneDestination);
474
- assert.equal(result.issues[0].fix, undefined);
475
- assert.equal(result.schemaLimited, undefined);
476
- return true;
477
- },
478
- );
479
- });
480
-
481
- test(`${op} with both destinations → the same single exactly-one issue, no not-allowed pair`, async () => {
482
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
483
- await assert.rejects(
484
- callTool(tool, {
485
- path: "unused.txt",
486
- edits: [
487
- { op, anchor: `1#${anchorHash}`, before: `2#${anchorHash}`, after: `3#${anchorHash}` },
488
- ],
489
- }),
490
- (error: unknown) => {
491
- const result = argumentError(error);
492
- assert.equal(result.issues.length, 1);
493
- assert.equal(result.issues[0].field, "edits[0]");
494
- assert.match(result.issues[0].reason, exactlyOneDestination);
495
- assert.equal(result.schemaLimited, undefined);
496
- return true;
497
- },
498
- );
499
- });
500
- }
501
-
502
- test("mixed copy and move batch → every operation keeps its exactly-one and unknown-field issues", async () => {
636
+ test("copy/move has zero or two destinations → reports one exactly-one issue without a fix", async (t) => {
503
637
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
504
- await assert.rejects(
505
- callTool(tool, {
506
- path: "unused.txt",
507
- edits: [
508
- { op: "copy", anchor: `1#${anchorHash}`, start: { x: 1 }, to: `2#${anchorHash}` },
509
- { op: "move", anchor: `3#${anchorHash}` },
510
- ],
511
- }),
512
- (error: unknown) => {
513
- const result = argumentError(error);
514
- assert.deepEqual(
515
- new Set(result.issues.map((issue) => issue.field)),
516
- new Set(["edits[0]", "edits[0].start", "edits[0].to", "edits[1]"]),
517
- );
518
- for (const field of ["edits[0]", "edits[1]"])
519
- assert.match(
520
- result.issues.find((issue) => issue.field === field)?.reason ?? "",
521
- exactlyOneDestination,
522
- );
523
- for (const field of ["edits[0].start", "edits[0].to"])
524
- assert.match(
525
- result.issues.find((issue) => issue.field === field)?.reason ?? "",
526
- /not allowed/,
527
- );
528
- assert.equal(result.schemaLimited, undefined);
529
- return true;
530
- },
531
- );
532
- });
533
-
534
- for (const op of ["copy", "move"] as const) {
535
- test(`${op} with both destinations and a wrong before hash → keeps the hash-length issue beside exactly-one`, async () => {
536
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
537
- const shortHash = `2#${"A".repeat(DEFAULT_CONFIG.hashLen - 1)}`;
538
- await assert.rejects(
539
- callTool(tool, {
540
- path: "unused.txt",
541
- edits: [{ op, anchor: `1#${anchorHash}`, before: shortHash, after: `3#${anchorHash}` }],
542
- }),
543
- (error: unknown) => {
544
- const result = argumentError(error);
545
- const exactly = result.issues.filter((issue) => issue.field === "edits[0]");
546
- assert.equal(exactly.length, 1);
547
- assert.match(exactly[0].reason, exactlyOneDestination);
548
- const hashIssue = result.issues.find((issue) => issue.field === "edits[0].before");
549
- assert.ok(hashIssue, JSON.stringify(result.issues));
550
- assert.match(hashIssue.reason, /hash characters/);
551
- assert.match(hashIssue.fix ?? "", /Read or grep/);
552
- assert.ok(!result.issues.some((issue) => issue.field === "edits[0].after"));
553
- return true;
554
- },
555
- );
556
- });
557
- }
558
-
559
- for (const op of ["copy", "move"] as const) {
560
- for (const side of ["before", "after"] as const) {
561
- test(`${op} with one ${side} destination and an unknown field → only the unknown field is reported`, async () => {
562
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
563
- await assert.rejects(
564
- callTool(tool, {
565
- path: "unused.txt",
566
- edits: [
567
- { op, anchor: `1#${anchorHash}`, [side]: `2#${anchorHash}`, to: `3#${anchorHash}` },
568
- ],
569
- }),
570
- (error: unknown) => {
571
- const result = argumentError(error);
572
- assert.equal(result.issues.length, 1);
573
- assert.equal(result.issues[0].field, "edits[0].to");
574
- assert.match(result.issues[0].reason, /not allowed/);
575
- assert.equal(result.schemaLimited, undefined);
576
- return true;
638
+ for (const action of ["copy", "move"]) {
639
+ for (const destinations of [{}, { before: fieldValues.before, after: fieldValues.after }]) {
640
+ await t.test(
641
+ `${action}/${Object.keys(destinations).length} destinations → reports the item`,
642
+ async () => {
643
+ await assert.rejects(
644
+ callTool(tool, {
645
+ path: "unused.txt",
646
+ edits: [{ action, anchor: fieldValues.anchor, ...destinations }],
647
+ }),
648
+ (error: unknown) => {
649
+ assert.deepEqual(argumentError(error).issues, [
650
+ {
651
+ field: "edits[0]",
652
+ reason: "expected exactly one of: before, after",
653
+ },
654
+ ]);
655
+ return true;
656
+ },
657
+ );
577
658
  },
578
659
  );
579
- });
660
+ }
580
661
  }
581
- }
662
+ });
582
663
 
583
664
  /** Tool schema properties, so expected fix text comes from the schema instead of the test. */
584
665
  interface SchemaNode {
@@ -604,7 +685,15 @@ const inOrder = (fix: string, pieces: readonly string[]): void => {
604
685
  };
605
686
 
606
687
  test("missing required parameter → fix states the parameter's role from its schema description", async () => {
607
- const grep = makeGrepOverride(process.cwd(), DEFAULT_CONFIG);
688
+ let discoveries = 0;
689
+ const discover = async () => {
690
+ discoveries++;
691
+ return { code: 1, stderr: "", stopped: false };
692
+ };
693
+ const grep = makeGrepOverrideWithBackend(process.cwd(), DEFAULT_CONFIG, {
694
+ runRgPaths: discover,
695
+ runRgLines: discover,
696
+ });
608
697
  const write = makeWriteOverride(process.cwd());
609
698
  const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
610
699
  const cases = [
@@ -626,27 +715,6 @@ test("missing required parameter → fix states the parameter's role from its sc
626
715
  inOrder(fix, ['"create"', create.description, '"overwrite"', overwrite.description]);
627
716
  },
628
717
  },
629
- {
630
- name: "edit op",
631
- tool: edit,
632
- args: { path: "unused.txt", edits: [{ anchor: `1#${anchorHash}`, body: ["text"] }] },
633
- field: "edits[0].op",
634
- verify: (fix: string) => {
635
- for (const op of [
636
- "replace",
637
- "delete",
638
- "insert_before",
639
- "insert_after",
640
- "copy",
641
- "move",
642
- "append",
643
- "prepend",
644
- ])
645
- assert.ok(fix.includes(`\"${op}\"`), `${op} missing from ${fix}`);
646
- assert.equal(fix.split('"copy"').length - 1, 1);
647
- assert.equal(fix.split('"move"').length - 1, 1);
648
- },
649
- },
650
718
  ] as const;
651
719
  for (const scenario of cases) {
652
720
  await assert.rejects(callTool(scenario.tool, scenario.args), (error: unknown) => {
@@ -659,12 +727,13 @@ test("missing required parameter → fix states the parameter's role from its sc
659
727
  return true;
660
728
  });
661
729
  }
730
+ assert.equal(discoveries, 0);
662
731
  });
663
732
 
664
733
  test("two operations miss the same required field → the shared fix appears once", async () => {
665
734
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
666
735
  await assert.rejects(
667
- callTool(tool, { path: "unused.txt", edits: [{ op: "replace" }, { op: "replace" }] }),
736
+ callTool(tool, { path: "unused.txt", edits: [{ action: "replace" }, { action: "replace" }] }),
668
737
  (error: unknown) => {
669
738
  const result = argumentError(error);
670
739
  const anchors = result.issues.filter((issue) => issue.field.endsWith(".anchor"));
@@ -672,13 +741,7 @@ test("two operations miss the same required field → the shared fix appears onc
672
741
  assert.equal(anchors.filter((issue) => issue.fix).length, 1);
673
742
  const fixes = result.issues.flatMap((issue) => (issue.fix ? [issue.fix] : []));
674
743
  assert.equal(new Set(fixes).size, fixes.length, JSON.stringify(fixes));
675
- const anchorDescriptions = new Set(
676
- (schemaProperties(tool).edits?.items?.anyOf ?? []).flatMap((branch) =>
677
- branch.properties?.anchor ? [branch.properties.anchor.description] : [],
678
- ),
679
- );
680
- assert.equal(anchorDescriptions.size, 1);
681
- const description = [...anchorDescriptions][0];
744
+ const description = schemaProperties(tool).edits?.items?.properties?.anchor?.description;
682
745
  assert.ok(description);
683
746
  assert.ok(fixes.includes(description));
684
747
  return true;
@@ -705,66 +768,30 @@ test("invalid enum value → the reason carries no description fix", async () =>
705
768
  );
706
769
  });
707
770
 
708
- const branchWithOp = (tool: { readonly parameters: unknown }, op: string): SchemaNode => {
709
- const branch = (schemaProperties(tool).edits?.items?.anyOf ?? []).find((candidate) => {
710
- const opSchema = candidate.properties?.op;
711
- return [opSchema?.const, ...(opSchema?.anyOf ?? []).map((option) => option.const)].includes(op);
712
- });
713
- assert.ok(branch, `no edit branch for op ${op}`);
714
- return branch;
715
- };
716
-
717
- const acceptedFields = (branch: SchemaNode): string =>
718
- `Accepted fields: ${Object.keys(branch.properties ?? {}).join(", ")}.`;
771
+ const acceptedFields = (shape: SchemaNode): string =>
772
+ `Accepted fields: ${Object.keys(shape.properties ?? {}).join(", ")}.`;
719
773
 
720
- const opExplanation = (branch: SchemaNode): string => {
721
- const options = branch.properties?.op?.anyOf ?? [{ const: branch.properties?.op?.const }];
722
- return `${options.map((option) => JSON.stringify(option.const)).join(" or ")}: ${branch.properties?.op?.description}`;
723
- };
724
-
725
- test("extra field is not allowed → fix describes the selected shape from its schema", async () => {
774
+ test("unknown field is supplied → fix lists only the flat object's accepted fields", async () => {
726
775
  const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
727
776
  const grep = makeGrepOverride(process.cwd(), DEFAULT_CONFIG);
728
- const append = branchWithOp(edit, "append");
729
- const move = branchWithOp(edit, "move");
730
- const cases = [
777
+ for (const scenario of [
731
778
  {
732
- name: "tagged branch rejects another branch's field",
733
779
  tool: edit,
734
- args: {
735
- path: "unused.txt",
736
- edits: [{ op: "append", anchor: `1#${anchorHash}`, body: ["text"] }],
737
- },
738
- field: "edits[0].anchor",
739
- fix: `${opExplanation(append)} ${acceptedFields(append)}`,
780
+ args: { path: "unused.txt", edits: [{ action: "append", body: ["text"], op: "append" }] },
781
+ field: "edits[0].op",
782
+ fix: "Accepted fields: action, anchor, end, body, before, after.",
740
783
  },
741
784
  {
742
- name: "unknown top-level field",
743
785
  tool: grep,
744
- args: { path: "unused.txt", pattern: "text", literal: true, bogus: 1 },
786
+ args: { pattern: "text", literal: true, bogus: 1 },
745
787
  field: "bogus",
746
788
  fix: acceptedFields({ properties: schemaProperties(grep) }),
747
789
  },
748
- {
749
- name: "transfer branch selected by before",
750
- tool: edit,
751
- args: {
752
- path: "unused.txt",
753
- edits: [
754
- { op: "move", anchor: `1#${anchorHash}`, before: `2#${anchorHash}`, body: ["text"] },
755
- ],
756
- },
757
- field: "edits[0].body",
758
- fix: `${opExplanation(move)} ${acceptedFields(move)}`,
759
- },
760
- ] as const;
761
- for (const scenario of cases) {
790
+ ]) {
762
791
  await assert.rejects(callTool(scenario.tool, scenario.args), (error: unknown) => {
763
- const result = argumentError(error);
764
- const issue = result.issues.find((candidate) => candidate.field === scenario.field);
765
- assert.ok(issue, JSON.stringify(result.issues));
766
- assert.match(issue.reason, /is not allowed/);
767
- assert.equal(issue.fix, scenario.fix, JSON.stringify(result.issues));
792
+ assert.deepEqual(argumentError(error).issues, [
793
+ { field: scenario.field, reason: "is not allowed", fix: scenario.fix },
794
+ ]);
768
795
  return true;
769
796
  });
770
797
  }
@@ -772,20 +799,19 @@ test("extra field is not allowed → fix describes the selected shape from its s
772
799
 
773
800
  test("value mismatches the field pattern → fix states the field description from the schema", async () => {
774
801
  const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
775
- const append = branchWithOp(edit, "append");
776
- const replace = branchWithOp(edit, "replace");
802
+ const fields = schemaProperties(edit).edits?.items?.properties;
777
803
  const cases = [
778
804
  {
779
805
  name: "body element with a newline",
780
- args: { path: "unused.txt", edits: [{ op: "append", body: ["a\nb"] }] },
806
+ args: { path: "unused.txt", edits: [{ action: "append", body: ["a\nb"] }] },
781
807
  field: "edits[0].body[0]",
782
- fix: append.properties?.body?.description,
808
+ fix: fields?.body?.description,
783
809
  },
784
810
  {
785
811
  name: "anchor without the line-hash shape",
786
- args: { path: "unused.txt", edits: [{ op: "replace", anchor: "abc", body: ["text"] }] },
812
+ args: { path: "unused.txt", edits: [{ action: "replace", anchor: "abc", body: ["text"] }] },
787
813
  field: "edits[0].anchor",
788
- fix: replace.properties?.anchor?.description,
814
+ fix: fields?.anchor?.description,
789
815
  },
790
816
  ] as const;
791
817
  for (const scenario of cases) {
@@ -805,7 +831,7 @@ test("two unknown fields in one object → the shared fix appears once", async (
805
831
  await assert.rejects(
806
832
  callTool(edit, {
807
833
  path: "unused.txt",
808
- edits: [{ op: "append", body: ["text"], zzz: 1, yyy: 2 }],
834
+ edits: [{ action: "append", body: ["text"], zzz: 1, yyy: 2 }],
809
835
  }),
810
836
  (error: unknown) => {
811
837
  const result = argumentError(error);