@criogaid/pi-hashline-edit 0.10.0 → 0.12.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 (103) hide show
  1. package/README.md +21 -7
  2. package/README.zh-CN.md +22 -8
  3. package/package.json +7 -6
  4. package/src/core/AGENTS.md +2 -2
  5. package/src/core/apply.test.ts +4 -4
  6. package/src/core/apply.ts +3 -3
  7. package/src/core/character-set.test.ts +2 -3
  8. package/src/core/character-set.ts +1 -1
  9. package/src/core/ranges.test.ts +2 -1
  10. package/src/core/ranges.ts +1 -1
  11. package/src/core/regex.ts +6 -2
  12. package/src/core/replace.test.ts +51 -0
  13. package/src/core/replace.ts +10 -4
  14. package/src/core/types.ts +1 -1
  15. package/src/index.ts +9 -9
  16. package/src/integration/config-registration.test.ts +8 -10
  17. package/src/integration/fusion-dispatch.test.ts +2 -2
  18. package/src/integration/grep-errors.test.ts +3 -3
  19. package/src/integration/grep-flags.test.ts +5 -5
  20. package/src/integration/grep-js-completeness.test.ts +12 -11
  21. package/src/integration/grep-line-candidates.test.ts +5 -3
  22. package/src/integration/grep-matrix.test.ts +5 -4
  23. package/src/integration/grep-order.test.ts +4 -4
  24. package/src/integration/grep-path-matrix.test.ts +5 -4
  25. package/src/integration/grep-prefilter-coverage.test.ts +17 -5
  26. package/src/integration/grep-process-limits.test.ts +2 -4
  27. package/src/integration/grep-regex.test.ts +10 -10
  28. package/src/integration/grep-rg.test.ts +93 -48
  29. package/src/integration/grep-scan-budget.test.ts +8 -8
  30. package/src/integration/grep-translation.test.ts +8 -7
  31. package/src/integration/large-text.test.ts +5 -5
  32. package/src/integration/model-budget-lifecycle.test.ts +9 -9
  33. package/src/integration/mutation-file-types.testing.ts +4 -4
  34. package/src/integration/npm-package.test.ts +12 -10
  35. package/src/integration/npm-package.testing.ts +151 -4
  36. package/src/integration/publication-read-changes.test.ts +54 -54
  37. package/src/integration/read-changes.test.ts +4 -4
  38. package/src/integration/replace-worker-boundary.test.ts +1 -1
  39. package/src/integration/report-replay.test.ts +9 -6
  40. package/src/integration/utf8-errors.test.ts +4 -4
  41. package/src/pi/AGENTS.md +13 -12
  42. package/src/pi/action-fusion.test.ts +125 -105
  43. package/src/pi/action-fusion.testing.ts +38 -0
  44. package/src/pi/action-fusion.ts +9 -21
  45. package/src/pi/argument-diagnostics.ts +4 -3
  46. package/src/pi/argument-format.test.ts +19 -18
  47. package/src/pi/argument-validation.test.ts +281 -139
  48. package/src/pi/argument-validation.ts +21 -31
  49. package/src/pi/budgets.ts +1 -4
  50. package/src/pi/codemode.test.ts +7 -2
  51. package/src/pi/config.test.ts +2 -1
  52. package/src/pi/config.ts +8 -3
  53. package/src/pi/diagnostic-buffer.test.ts +23 -13
  54. package/src/pi/edit-tool.ts +48 -63
  55. package/src/pi/error-text.ts +7 -2
  56. package/src/pi/execute.test.ts +178 -157
  57. package/src/pi/failure-context.test.ts +5 -4
  58. package/src/pi/failure-context.ts +1 -1
  59. package/src/pi/failure-path.test.ts +37 -37
  60. package/src/pi/file-commit.test.ts +47 -19
  61. package/src/pi/file-commit.ts +36 -9
  62. package/src/pi/fusion-card.test.ts +22 -21
  63. package/src/pi/fusion-card.ts +9 -5
  64. package/src/pi/grep-candidates.test.ts +18 -20
  65. package/src/pi/grep-candidates.ts +13 -17
  66. package/src/pi/grep-matrix.test.ts +3 -3
  67. package/src/pi/grep-order.test.ts +12 -11
  68. package/src/pi/grep-output.ts +45 -17
  69. package/src/pi/grep-reread.ts +7 -2
  70. package/src/pi/grep-rg.testing.ts +70 -0
  71. package/src/pi/grep-scope.ts +10 -10
  72. package/src/pi/grep-search.ts +8 -11
  73. package/src/pi/grep-tool.ts +40 -53
  74. package/src/pi/grep.test.ts +141 -116
  75. package/src/pi/model-budgets.test.ts +2 -1
  76. package/src/pi/mutation-outcome.testing.ts +0 -1
  77. package/src/pi/mutation-result.test.ts +8 -8
  78. package/src/pi/mutation-result.ts +1 -1
  79. package/src/pi/mutation-runner.ts +10 -6
  80. package/src/pi/output-budgets.test.ts +53 -31
  81. package/src/pi/pi.test.ts +18 -13
  82. package/src/pi/read-tool.ts +13 -18
  83. package/src/pi/read.test.ts +67 -41
  84. package/src/pi/registration.test.ts +25 -19
  85. package/src/pi/render.test.ts +94 -32
  86. package/src/pi/render.ts +7 -4
  87. package/src/pi/replace-regex.ts +1 -1
  88. package/src/pi/replace-tool.ts +6 -16
  89. package/src/pi/replace.test.ts +45 -34
  90. package/src/pi/report-failure.testing.ts +9 -1
  91. package/src/pi/report-schema.ts +225 -86
  92. package/src/pi/report.test.ts +81 -87
  93. package/src/pi/report.ts +35 -22
  94. package/src/pi/result-safety.test.ts +121 -100
  95. package/src/pi/rg-process.test.ts +2 -2
  96. package/src/pi/rg-process.ts +9 -15
  97. package/src/pi/rg-regex.test.ts +13 -0
  98. package/src/pi/rg-regex.ts +2 -2
  99. package/src/pi/schema.ts +23 -0
  100. package/src/pi/tool-factories.testing.ts +59 -0
  101. package/src/pi/write-tool.ts +9 -15
  102. package/src/pi/write.test.ts +30 -31
  103. package/src/testing/run-tests.ts +1 -1
@@ -5,19 +5,49 @@ import { tmpdir } from "node:os";
5
5
  import { join } from "node:path";
6
6
  import { test } from "node:test";
7
7
  import { createActionFusionExecutor } from "./action-fusion.ts";
8
- import { DEFAULT_CONFIG } from "./config.ts";
9
- import { makeEditOverride } from "./edit-tool.ts";
10
- import { makeGrepOverride, makeGrepOverrideWithBackend } from "./grep-tool.ts";
11
- import { makeReadOverride } from "./read-tool.ts";
12
- import { makeReplaceTool } from "./replace-tool.ts";
13
- import { makeWriteOverride } from "./write-tool.ts";
14
- import { callTool } from "./tool-call.testing.ts";
8
+ import { makeMockGrep } from "./grep-rg.testing.ts";
9
+ import { commandCompletion } from "./action-fusion.testing.ts";
10
+ import { callTool, createToolContext } from "./tool-call.testing.ts";
15
11
  import { argumentError, rejectsArgument } from "./argument-error.testing.ts";
16
12
  import { POSITIVE_SAFE_INTEGER } from "./schema.ts";
17
13
  import { reportOf } from "./report.ts";
18
14
  import { createAnchorFormatter } from "./anchor-format.ts";
19
15
  import { computeLineHash } from "../core/hash.ts";
20
- import { grepCounts } from "./grep-output.testing.ts";
16
+
17
+ // Load candidate-dependent helpers only after grep-rg.testing registers the module mock.
18
+ const { grepCounts } = await import("./grep-output.testing.ts");
19
+
20
+ const {
21
+ DEFAULT_CONFIG,
22
+ makeReadOverride,
23
+ makeGrepOverride,
24
+ makeEditOverride,
25
+ makeReplaceTool,
26
+ makeWriteOverride,
27
+ } = await import("./tool-factories.testing.ts");
28
+
29
+ test("grep has invalid flags alongside invalid literal or pattern → reports independent fields", async (t) => {
30
+ const tool = makeGrepOverride(process.cwd(), DEFAULT_CONFIG);
31
+ for (const args of [
32
+ { pattern: ["text"], flags: "x" },
33
+ { pattern: ["text"], literal: {}, flags: "x" },
34
+ { pattern: {}, literal: true, flags: "x" },
35
+ ]) {
36
+ await t.test(JSON.stringify(args), async () => {
37
+ await assert.rejects(callTool(tool, args), (error: unknown) => {
38
+ const fields = argumentError(error).issues.map((issue) => issue.field);
39
+ assert.ok(fields.includes("flags"), JSON.stringify(fields));
40
+ assert.ok(
41
+ fields.some((field) =>
42
+ Array.isArray(args.pattern) ? field === "literal" : field.startsWith("pattern"),
43
+ ),
44
+ JSON.stringify(fields),
45
+ );
46
+ return true;
47
+ });
48
+ });
49
+ }
50
+ });
21
51
 
22
52
  const invalidCommand = { command: "", timeout: 0 };
23
53
  const anchorHash = "A".repeat(DEFAULT_CONFIG.hashLen);
@@ -41,31 +71,31 @@ const cases = [
41
71
  make: (cwd: string, fusion: ReturnType<typeof createActionFusionExecutor>) =>
42
72
  makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
43
73
  args: {
44
- edits: [
45
- { action: "replace", anchor: `1#${anchorHash}`, body: [] },
46
- { action: "append", body: ["bad\0", "bad\ud800"] },
74
+ steps: [
75
+ { action: "replace", anchor: `1#${anchorHash}`, lines: [] },
76
+ { action: "append", lines: ["bad\0", "bad\ud800"] },
47
77
  { action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#${anchorHash}`, end: "1#A" },
48
- { action: "append", body: [] },
49
- { action: "replace", anchor: `3#${anchorHash}`, end: `1#${anchorHash}`, body: ["text"] },
78
+ { action: "append", lines: [] },
79
+ { action: "replace", anchor: `3#${anchorHash}`, end: `1#${anchorHash}`, lines: ["text"] },
50
80
  { action: "delete", anchor: `4#${anchorHash}`, end: `2#${anchorHash}` },
51
81
  ],
52
82
  then_run: invalidCommand,
53
83
  },
54
84
  fields: [
55
- "edits[0].body",
56
- "edits[1].body[0]",
57
- "edits[1].body[1]",
58
- "edits[2].anchor",
59
- "edits[2].end",
60
- "edits[3].body",
61
- "edits[4].end",
62
- "edits[5].end",
85
+ "steps[0].lines",
86
+ "steps[1].lines[0]",
87
+ "steps[1].lines[1]",
88
+ "steps[2].anchor",
89
+ "steps[2].end",
90
+ "steps[3].lines",
91
+ "steps[4].end",
92
+ "steps[5].end",
63
93
  "then_run.command",
64
94
  "then_run.timeout",
65
95
  ],
66
96
  reasons: [
67
- { field: "edits[4].end", reason: /line 1 is before anchor line 3/ },
68
- { field: "edits[5].end", reason: /line 2 is before anchor line 4/ },
97
+ { field: "steps[4].end", reason: /line 1 is before anchor line 3/ },
98
+ { field: "steps[5].end", reason: /line 2 is before anchor line 4/ },
69
99
  ],
70
100
  },
71
101
  {
@@ -105,17 +135,21 @@ for (const scenario of cases) {
105
135
  const original = "original\n";
106
136
  await writeFile(path, original);
107
137
  let commandRuns = 0;
108
- const fusion = createActionFusionExecutor(async () => {
109
- commandRuns++;
110
- return { status: "succeeded", result: { content: [{ type: "text", text: "ran" }] } };
111
- });
138
+ const fusion = createActionFusionExecutor(() => {});
139
+ const ctx = {
140
+ ...(await createToolContext(cwd, t)),
141
+ executeTool: async () => {
142
+ commandRuns++;
143
+ return commandCompletion({ content: [{ type: "text", text: "ran" }], details: undefined });
144
+ },
145
+ };
112
146
  const tool = scenario.make(cwd, fusion);
113
147
  const args: unknown = JSON.parse(JSON.stringify({ path, ...scenario.args, unexpected: true }));
114
148
  const snapshot = structuredClone(args);
115
149
  const open = t.mock.method(fs, "open");
116
150
  syncBuiltinESMExports();
117
151
  try {
118
- await assert.rejects(callTool(tool, args), (error: unknown) => {
152
+ await assert.rejects(callTool(tool, args, { ctx }), (error: unknown) => {
119
153
  const result = argumentError(error);
120
154
  assert.deepEqual(
121
155
  new Set(result.issues.map((issue) => issue.field)),
@@ -141,6 +175,50 @@ for (const scenario of cases) {
141
175
  });
142
176
  }
143
177
 
178
+ test("blank then_run command with Fusion → reports exactly one command issue", async (t) => {
179
+ const cwd = process.cwd();
180
+ const fusion = createActionFusionExecutor(() => {});
181
+ const mutations = [
182
+ {
183
+ name: "edit",
184
+ tool: makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
185
+ args: { steps: [{ action: "append", lines: ["text"] }] },
186
+ },
187
+ {
188
+ name: "replace",
189
+ tool: makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
190
+ args: { replacements: [{ find: "old", replace: "new", literal: true }] },
191
+ },
192
+ {
193
+ name: "write",
194
+ tool: makeWriteOverride(cwd, fusion),
195
+ args: { content: "text", mode: "create" },
196
+ },
197
+ ] as const;
198
+ for (const scenario of mutations) {
199
+ for (const command of ["", " "]) {
200
+ await t.test(
201
+ `${scenario.name}/${JSON.stringify(command)} → rejects one command issue`,
202
+ async () => {
203
+ await assert.rejects(
204
+ callTool(scenario.tool, {
205
+ path: "unused.txt",
206
+ ...scenario.args,
207
+ then_run: { command },
208
+ }),
209
+ (error: unknown) => {
210
+ const result = argumentError(error);
211
+ assert.equal(result.issues.length, 1, JSON.stringify(result));
212
+ assert.equal(result.issues[0].field, "then_run.command");
213
+ return true;
214
+ },
215
+ );
216
+ },
217
+ );
218
+ }
219
+ }
220
+ });
221
+
144
222
  test("grep and replace flags → accept V8 modes and reject invalid flags before patterns or file reads", async (t) => {
145
223
  const cwd = await mkdtemp(join(tmpdir(), "hashline-flags-"));
146
224
  t.after(() => rm(cwd, { recursive: true, force: true }));
@@ -185,12 +263,52 @@ test("grep and replace flags → accept V8 modes and reject invalid flags before
185
263
  }
186
264
  });
187
265
 
188
- const editFields = ["anchor", "end", "body"] as const;
266
+ test("invalid regex pattern → explains literal recovery without changing the requested mode", async (t) => {
267
+ const cwd = await mkdtemp(join(tmpdir(), "hashline-regex-recovery-"));
268
+ t.after(() => rm(cwd, { recursive: true, force: true }));
269
+ const path = join(cwd, "missing.txt");
270
+ const cases = [
271
+ {
272
+ tool: makeGrepOverride(cwd, DEFAULT_CONFIG),
273
+ args: (find: string, literal: boolean) => ({ pattern: [find], literal }),
274
+ field: "pattern[0]",
275
+ },
276
+ {
277
+ tool: makeReplaceTool(cwd, DEFAULT_CONFIG),
278
+ args: (find: string, literal: boolean) => ({
279
+ replacements: [{ find, literal, replace: "updated" }],
280
+ }),
281
+ field: "replacements[0].find",
282
+ },
283
+ ];
284
+ for (const { tool, args, field } of cases) {
285
+ await t.test(tool.name, async () => {
286
+ const input = { path, ...args("call(", false) };
287
+ const original = structuredClone(input);
288
+ await assert.rejects(callTool(tool, input), (error: unknown) => {
289
+ const { issues } = argumentError(error);
290
+ assert.equal(issues.length, 1);
291
+ assert.equal(issues[0].field, field);
292
+ assert.match(issues[0].reason, /Invalid regular expression/);
293
+ assert.match(issues[0].fix ?? "", /literal.*true.*otherwise.*regex/);
294
+ return true;
295
+ });
296
+ assert.deepEqual(input, original);
297
+ for (const corrected of [args("call(", true), args("call\\(", false)]) {
298
+ const result = await callTool(tool, { path, ...corrected });
299
+ assert.equal(reportOf(result, tool.name)?.error, "PATH_NOT_FOUND");
300
+ }
301
+ await assert.rejects(readFile(path), { code: "ENOENT" });
302
+ });
303
+ }
304
+ });
305
+
306
+ const editFields = ["anchor", "end", "lines"] as const;
189
307
  type EditField = (typeof editFields)[number];
190
308
  const fieldValues = {
191
309
  anchor: `1#${anchorHash}`,
192
310
  end: `2#${anchorHash}`,
193
- body: ["text"],
311
+ lines: ["text"],
194
312
  };
195
313
  const actionCases: readonly {
196
314
  action: string;
@@ -200,31 +318,31 @@ const actionCases: readonly {
200
318
  }[] = [
201
319
  {
202
320
  action: "replace",
203
- required: ["anchor", "body"],
321
+ required: ["anchor", "lines"],
204
322
  optional: ["end"],
205
323
  expected: "text\nbeta\ngamma\n",
206
324
  },
207
325
  { action: "delete", required: ["anchor"], optional: ["end"], expected: "beta\ngamma\n" },
208
326
  {
209
327
  action: "insert_after",
210
- required: ["anchor", "body"],
328
+ required: ["anchor", "lines"],
211
329
  optional: [],
212
330
  expected: "alpha\ntext\nbeta\ngamma\n",
213
331
  },
214
332
  {
215
333
  action: "insert_before",
216
- required: ["anchor", "body"],
334
+ required: ["anchor", "lines"],
217
335
  optional: [],
218
336
  expected: "text\nalpha\nbeta\ngamma\n",
219
337
  },
220
- { action: "append", required: ["body"], optional: [], expected: "alpha\nbeta\ngamma\ntext\n" },
221
- { action: "prepend", required: ["body"], optional: [], expected: "text\nalpha\nbeta\ngamma\n" },
338
+ { action: "append", required: ["lines"], optional: [], expected: "alpha\nbeta\ngamma\ntext\n" },
339
+ { action: "prepend", required: ["lines"], optional: [], expected: "text\nalpha\nbeta\ngamma\n" },
222
340
  ];
223
341
 
224
342
  for (const scenario of actionCases) {
225
343
  test(`${scenario.action} field matrix → requires its fields and rejects fields it does not take`, async (t) => {
226
344
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
227
- const fields = schemaProperties(tool).edits?.items?.properties;
345
+ const fields = schemaProperties(tool).steps?.items?.properties;
228
346
  const choice = fields?.action?.anyOf?.find((option) => option.const === scenario.action);
229
347
  assert.ok(choice?.description);
230
348
  const description = choice.description;
@@ -241,11 +359,11 @@ for (const scenario of actionCases) {
241
359
  Object.entries(valid).filter(([key]) => key !== field),
242
360
  );
243
361
  if (value === null) edit[field] = null;
244
- const args = { path: "unused.txt", edits: [edit] };
362
+ const args = { path: "unused.txt", steps: [edit] };
245
363
  await assert.rejects(callTool(tool, args), (error: unknown) => {
246
364
  assert.deepEqual(argumentError(error).issues, [
247
365
  {
248
- field: `edits[0].${field}`,
366
+ field: `steps[0].${field}`,
249
367
  reason: `is required for "${scenario.action}"`,
250
368
  fix: fields?.[field]?.description,
251
369
  },
@@ -263,12 +381,12 @@ for (const scenario of actionCases) {
263
381
  await assert.rejects(
264
382
  callTool(tool, {
265
383
  path: "unused.txt",
266
- edits: [{ ...valid, [field]: fieldValues[field] }],
384
+ steps: [{ ...valid, [field]: fieldValues[field] }],
267
385
  }),
268
386
  (error: unknown) => {
269
387
  assert.deepEqual(argumentError(error).issues, [
270
388
  {
271
- field: `edits[0].${field}`,
389
+ field: `steps[0].${field}`,
272
390
  reason: "is not allowed",
273
391
  fix: `"${scenario.action}": ${description}`,
274
392
  },
@@ -282,47 +400,31 @@ for (const scenario of actionCases) {
282
400
  });
283
401
  }
284
402
 
285
- test("edit action is missing or invalid → reports choices without guessing field permissions", async () => {
403
+ test("edit action is missing → reports choices without guessing field permissions", async () => {
286
404
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
287
- const choices = schemaProperties(tool).edits?.items?.properties?.action?.anyOf ?? [];
405
+ const choices = schemaProperties(tool).steps?.items?.properties?.action?.anyOf ?? [];
288
406
  assert.deepEqual(
289
407
  choices.map((choice) => choice.const),
290
408
  actionCases.map(({ action }) => action),
291
409
  );
292
- for (const action of [undefined, "unknown", null, 42, {}]) {
293
- await assert.rejects(
294
- callTool(tool, {
295
- path: "unused.txt",
296
- edits: [
297
- {
298
- ...(action === undefined ? {} : { action }),
299
- anchor: `1#${anchorHash}`,
300
- body: ["valid"],
301
- },
302
- ],
303
- }),
304
- (error: unknown) => {
305
- const { issues } = argumentError(error);
306
- assert.equal(issues.length, 1);
307
- const issue = issues[0];
308
- assert.equal(issue.field, "edits[0].action");
309
- if (action === undefined) {
310
- assert.equal(issue.reason, "is required");
311
- for (const choice of choices) {
312
- assert.ok(choice.description);
313
- inOrder(issue.fix ?? "", [JSON.stringify(choice.const), choice.description]);
314
- }
315
- } else {
316
- assert.equal(
317
- issue.reason,
318
- `expected one of: ${choices.map((choice) => JSON.stringify(choice.const)).join(", ")}`,
319
- );
320
- assert.equal(issue.fix, undefined);
321
- }
322
- return true;
323
- },
324
- );
325
- }
410
+ await assert.rejects(
411
+ callTool(tool, {
412
+ path: "unused.txt",
413
+ steps: [{ anchor: `1#${anchorHash}`, lines: ["valid"] }],
414
+ }),
415
+ (error: unknown) => {
416
+ const { issues } = argumentError(error);
417
+ assert.equal(issues.length, 1);
418
+ const issue = issues[0];
419
+ assert.equal(issue.field, "steps[0].action");
420
+ assert.equal(issue.reason, "is required");
421
+ for (const choice of choices) {
422
+ assert.ok(choice.description);
423
+ inOrder(issue.fix ?? "", [JSON.stringify(choice.const), choice.description]);
424
+ }
425
+ return true;
426
+ },
427
+ );
326
428
  });
327
429
 
328
430
  test("optional edit fields and then_run are null with Fusion → publishes bytes without rewriting caller arguments", async (t) => {
@@ -330,10 +432,14 @@ test("optional edit fields and then_run are null with Fusion → publishes bytes
330
432
  t.after(() => rm(cwd, { recursive: true, force: true }));
331
433
  const path = join(cwd, "target.txt");
332
434
  let commandRuns = 0;
333
- const fusion = createActionFusionExecutor(async () => {
334
- commandRuns++;
335
- return { status: "succeeded", result: { content: [{ type: "text", text: "ran" }] } };
336
- });
435
+ const fusion = createActionFusionExecutor(() => {});
436
+ const ctx = {
437
+ ...(await createToolContext(cwd, t)),
438
+ executeTool: async () => {
439
+ commandRuns++;
440
+ return commandCompletion({ content: [{ type: "text", text: "ran" }], details: undefined });
441
+ },
442
+ };
337
443
  const tool = makeEditOverride(cwd, DEFAULT_CONFIG, fusion);
338
444
  for (const scenario of actionCases) {
339
445
  await t.test(`${scenario.action} → accepts null optional fields`, async () => {
@@ -344,13 +450,13 @@ test("optional edit fields and then_run are null with Fusion → publishes bytes
344
450
  ...Object.fromEntries(
345
451
  scenario.required.map((field) => [
346
452
  field,
347
- field === "body" ? ["text"] : token(1, "alpha"),
453
+ field === "lines" ? ["text"] : token(1, "alpha"),
348
454
  ]),
349
455
  ),
350
456
  };
351
- const args = { path, edits: [edit], then_run: null };
457
+ const args = { path, steps: [edit], then_run: null };
352
458
  const snapshot = structuredClone(args);
353
- const result = await callTool(tool, args);
459
+ const result = await callTool(tool, args, { ctx });
354
460
  assert.equal(result.isError, undefined);
355
461
  assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
356
462
  assert.equal(reportOf(result, "edit")?.then_run, undefined);
@@ -376,7 +482,7 @@ test("null optional fields → grep applies defaults without filename filters",
376
482
  await onPath(path);
377
483
  return { code: 0, stderr: "", stopped: false };
378
484
  };
379
- const tool = makeGrepOverrideWithBackend(cwd, config, {
485
+ const tool = makeMockGrep(cwd, config, {
380
486
  runRgPaths: (args, _signal, onPath) => discover(args, onPath),
381
487
  runRgLines: (args, _signal, _maxLines, onFile) =>
382
488
  discover(args, (file) => onFile(file, undefined)),
@@ -404,19 +510,19 @@ test("edit batch has 64 or 65 operations → publishes the maximum or rejects th
404
510
  t.after(() => rm(cwd, { recursive: true, force: true }));
405
511
  const path = join(cwd, "target.txt");
406
512
  const tool = makeEditOverride(cwd, DEFAULT_CONFIG);
407
- const maxEdits: unknown = Reflect.get(tool.parameters.properties.edits, "maxItems");
513
+ const maxEdits: unknown = Reflect.get(tool.parameters.properties.steps, "maxItems");
408
514
  assert.ok(typeof maxEdits === "number");
409
515
  assert.equal(maxEdits, 64);
410
516
  const lines = Array.from({ length: maxEdits + 2 }, (_, i) => `line ${i + 1}`);
411
517
  const original = Buffer.from(`${lines.join("\n")}\n`);
412
518
  for (const count of [maxEdits, maxEdits + 1]) {
413
519
  await t.test(
414
- `${count} edits → ${count === maxEdits ? "publishes distinct deletions" : `rejects edits above max${maxEdits}`}`,
520
+ `${count} steps → ${count === maxEdits ? "publishes distinct deletions" : `rejects steps above max${maxEdits}`}`,
415
521
  async () => {
416
522
  await writeFile(path, original);
417
523
  const args = {
418
524
  path,
419
- edits: lines
525
+ steps: lines
420
526
  .slice(0, count)
421
527
  .map((line, i) => ({ action: "delete", anchor: token(i + 1, line) })),
422
528
  };
@@ -430,7 +536,7 @@ test("edit batch has 64 or 65 operations → publishes the maximum or rejects th
430
536
  await assert.rejects(callTool(tool, args), (error: unknown) => {
431
537
  const report = argumentError(error);
432
538
  assert.equal(report.issues.length, 1);
433
- assert.equal(report.issues[0].field, "edits");
539
+ assert.equal(report.issues[0].field, "steps");
434
540
  assert.match(
435
541
  report.issues[0].reason,
436
542
  new RegExp(`must not have more than ${maxEdits} items`),
@@ -450,12 +556,12 @@ test("edit item is not an object → report the item type once", async () => {
450
556
  await assert.rejects(
451
557
  callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
452
558
  path: "unused.txt",
453
- edits: [item],
559
+ steps: [item],
454
560
  }),
455
561
  (error: unknown) => {
456
562
  const result = argumentError(error);
457
563
  assert.equal(result.issues.length, 1);
458
- return rejectsArgument("edits[0]", /object/)(error);
564
+ return rejectsArgument("steps[0]", /object/)(error);
459
565
  },
460
566
  );
461
567
  }
@@ -476,17 +582,6 @@ test("grep union value has one applicable type → report only its constraint",
476
582
  }
477
583
  });
478
584
 
479
- test("literal enum is invalid → report the permitted values once", async () => {
480
- await assert.rejects(
481
- callTool(makeWriteOverride(process.cwd()), { path: "unused.txt", content: "ok", mode: "bad" }),
482
- (error: unknown) => {
483
- const result = argumentError(error);
484
- assert.equal(result.issues.length, 1);
485
- return rejectsArgument("mode", /create.*overwrite/)(error);
486
- },
487
- );
488
- });
489
-
490
585
  test("unknown field names contain punctuation, numeric keys or newlines → quote property keys without treating them as array indices", async () => {
491
586
  for (const key of ["odd.\nfield/~", "0", "01", "a/b", "a~b", 'quote"field']) {
492
587
  await assert.rejects(
@@ -508,12 +603,12 @@ test("one field exhausts native diagnostics → label the limit and retain anoth
508
603
  await assert.rejects(
509
604
  callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
510
605
  path: "",
511
- edits: Array.from({ length: 30 }, () => ({ action: "unknown" })),
606
+ steps: Array.from({ length: 30 }, () => ({ action: "unknown" })),
512
607
  }),
513
608
  (error: unknown) => {
514
609
  const result = argumentError(error);
515
610
  assert.ok(result.issues.some((issue) => issue.field === "path"));
516
- assert.ok(result.issues.some((issue) => issue.field === "edits[0].action"));
611
+ assert.ok(result.issues.some((issue) => issue.field === "steps[0].action"));
517
612
  assert.equal(result.schemaLimited, true);
518
613
  return true;
519
614
  },
@@ -524,12 +619,12 @@ test("one field violates independent constraints → combine its reasons without
524
619
  await assert.rejects(
525
620
  callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
526
621
  path: "unused.txt",
527
- edits: [{ action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#A` }],
622
+ steps: [{ action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#A` }],
528
623
  }),
529
624
  (error: unknown) => {
530
625
  const result = argumentError(error);
531
626
  assert.equal(result.issues.length, 1);
532
- return rejectsArgument("edits[0].anchor", /safe integer[\s\S]*hash characters/)(error);
627
+ return rejectsArgument("steps[0].anchor", /safe integer[\s\S]*hash characters/)(error);
533
628
  },
534
629
  );
535
630
  await assert.rejects(
@@ -557,7 +652,7 @@ test("edit fields contain semantic errors → checks only fields accepted by the
557
652
  for (const field of editFields) {
558
653
  const accepted = scenario.required.includes(field) || scenario.optional.includes(field);
559
654
  const values: readonly (readonly [unknown, RegExp])[] =
560
- field === "body"
655
+ field === "lines"
561
656
  ? [
562
657
  [[], /empty/],
563
658
  [["bad\0"], /NUL/],
@@ -573,18 +668,18 @@ test("edit fields contain semantic errors → checks only fields accepted by the
573
668
  async () => {
574
669
  const edit = { ...valid, [field]: value };
575
670
  await assert.rejects(
576
- callTool(tool, { path: "unused.txt", edits: [edit] }),
671
+ callTool(tool, { path: "unused.txt", steps: [edit] }),
577
672
  (error: unknown) => {
578
673
  const report = argumentError(error);
579
674
  assert.equal(report.issues.length, 1, JSON.stringify(report));
580
675
  const issue = report.issues[0];
581
676
  assert.equal(
582
677
  issue.field,
583
- `edits[0].${field}${accepted && field === "body" && Array.isArray(value) && value.length ? "[0]" : ""}`,
678
+ `steps[0].${field}${accepted && field === "lines" && Array.isArray(value) && value.length ? "[0]" : ""}`,
584
679
  );
585
680
  if (accepted) {
586
681
  assert.match(issue.reason, reason);
587
- if (field === "body" && Array.isArray(value) && value.length === 0) {
682
+ if (field === "lines" && Array.isArray(value) && value.length === 0) {
588
683
  assert.match(
589
684
  issue.fix ?? "",
590
685
  scenario.action === "replace"
@@ -635,7 +730,7 @@ test("missing required parameter → fix states the parameter's role from its sc
635
730
  discoveries++;
636
731
  return { code: 1, stderr: "", stopped: false };
637
732
  };
638
- const grep = makeGrepOverrideWithBackend(process.cwd(), DEFAULT_CONFIG, {
733
+ const grep = makeMockGrep(process.cwd(), DEFAULT_CONFIG, {
639
734
  runRgPaths: discover,
640
735
  runRgLines: discover,
641
736
  });
@@ -678,7 +773,7 @@ test("missing required parameter → fix states the parameter's role from its sc
678
773
  test("two operations miss the same required field → the shared fix appears once", async () => {
679
774
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
680
775
  await assert.rejects(
681
- callTool(tool, { path: "unused.txt", edits: [{ action: "replace" }, { action: "replace" }] }),
776
+ callTool(tool, { path: "unused.txt", steps: [{ action: "replace" }, { action: "replace" }] }),
682
777
  (error: unknown) => {
683
778
  const result = argumentError(error);
684
779
  const anchors = result.issues.filter((issue) => issue.field.endsWith(".anchor"));
@@ -686,7 +781,7 @@ test("two operations miss the same required field → the shared fix appears onc
686
781
  assert.equal(anchors.filter((issue) => issue.fix).length, 1);
687
782
  const fixes = result.issues.flatMap((issue) => (issue.fix ? [issue.fix] : []));
688
783
  assert.equal(new Set(fixes).size, fixes.length, JSON.stringify(fixes));
689
- const description = schemaProperties(tool).edits?.items?.properties?.anchor?.description;
784
+ const description = schemaProperties(tool).steps?.items?.properties?.anchor?.description;
690
785
  assert.ok(description);
691
786
  assert.ok(fixes.includes(description));
692
787
  return true;
@@ -694,23 +789,70 @@ test("two operations miss the same required field → the shared fix appears onc
694
789
  );
695
790
  });
696
791
 
697
- test("invalid enum value → the reason carries no description fix", async () => {
698
- await assert.rejects(
699
- callTool(makeGrepOverride(process.cwd(), DEFAULT_CONFIG), {
700
- path: "unused.txt",
701
- pattern: "text",
702
- literal: true,
703
- outputMode: "unknown",
704
- }),
705
- (error: unknown) => {
706
- const result = argumentError(error);
707
- assert.equal(result.issues.length, 1);
708
- assert.equal(result.issues[0].field, "outputMode");
709
- assert.match(result.issues[0].reason, /expected one of/);
710
- assert.equal(result.issues[0].fix, undefined);
711
- return true;
792
+ test("out-of-choice value → reason lists values and fix explains each choice", async (t) => {
793
+ const cwd = process.cwd();
794
+ const cases = [
795
+ {
796
+ tool: makeEditOverride(cwd, DEFAULT_CONFIG),
797
+ field: "steps[0].action",
798
+ values: ["unknown", null, 42, {}],
799
+ args: (action: unknown) => ({
800
+ path: "unused.txt",
801
+ steps: [{ action, anchor: `1#${anchorHash}`, lines: ["valid"] }],
802
+ }),
803
+ choices: [
804
+ ["replace", "Replace the cited line(s)"],
805
+ ["delete", "Delete the cited line(s)"],
806
+ ["insert_after", "Insert lines after the anchor line"],
807
+ ["insert_before", "Insert lines before the anchor line"],
808
+ ["append", "Add lines at the end"],
809
+ ["prepend", "Add lines at the start"],
810
+ ],
712
811
  },
713
- );
812
+ {
813
+ tool: makeWriteOverride(cwd),
814
+ field: "mode",
815
+ values: ["unknown"],
816
+ args: (mode: unknown) => ({ path: "unused.txt", content: "ok", mode }),
817
+ choices: [
818
+ ["create", "Fail if the target already exists."],
819
+ ["overwrite", "Fail if the target does not exist."],
820
+ ],
821
+ },
822
+ {
823
+ tool: makeGrepOverride(cwd, DEFAULT_CONFIG),
824
+ field: "outputMode",
825
+ values: ["unknown"],
826
+ args: (outputMode: unknown) => ({ pattern: "text", literal: true, outputMode }),
827
+ choices: [
828
+ ["content", "Matches and context", "anchors", "plain previews"],
829
+ ["files", "Paths of the files", "only paths"],
830
+ ["count", "Matching lines per file", "total", "only counts"],
831
+ ],
832
+ },
833
+ ] as const;
834
+ for (const scenario of cases) {
835
+ await t.test(`${scenario.tool.name}/${scenario.field}`, async () => {
836
+ for (const value of scenario.values) {
837
+ await assert.rejects(callTool(scenario.tool, scenario.args(value)), (error: unknown) => {
838
+ const { issues } = argumentError(error);
839
+ assert.equal(issues.length, 1, JSON.stringify(issues));
840
+ const issue = issues[0];
841
+ assert.equal(issue.field, scenario.field);
842
+ assert.equal(
843
+ issue.reason,
844
+ `expected one of: ${scenario.choices.map(([value]) => JSON.stringify(value)).join(", ")}`,
845
+ );
846
+ assert.ok(issue.fix, JSON.stringify(issues));
847
+ inOrder(
848
+ issue.fix,
849
+ scenario.choices.flatMap(([value, ...meaning]) => [JSON.stringify(value), ...meaning]),
850
+ );
851
+ return true;
852
+ });
853
+ }
854
+ });
855
+ }
714
856
  });
715
857
 
716
858
  const acceptedFields = (shape: SchemaNode): string =>
@@ -722,9 +864,9 @@ test("unknown field is supplied → fix lists only the flat object's accepted fi
722
864
  for (const scenario of [
723
865
  {
724
866
  tool: edit,
725
- args: { path: "unused.txt", edits: [{ action: "append", body: ["text"], op: "append" }] },
726
- field: "edits[0].op",
727
- fix: "Accepted fields: action, anchor, end, body.",
867
+ args: { path: "unused.txt", steps: [{ action: "append", lines: ["text"], op: "append" }] },
868
+ field: "steps[0].op",
869
+ fix: "Accepted fields: action, anchor, end, lines.",
728
870
  },
729
871
  {
730
872
  tool: grep,
@@ -744,18 +886,18 @@ test("unknown field is supplied → fix lists only the flat object's accepted fi
744
886
 
745
887
  test("value mismatches the field pattern → fix states the field description from the schema", async () => {
746
888
  const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
747
- const fields = schemaProperties(edit).edits?.items?.properties;
889
+ const fields = schemaProperties(edit).steps?.items?.properties;
748
890
  const cases = [
749
891
  {
750
- name: "body element with a newline",
751
- args: { path: "unused.txt", edits: [{ action: "append", body: ["a\nb"] }] },
752
- field: "edits[0].body[0]",
753
- fix: fields?.body?.description,
892
+ name: "line element with a newline",
893
+ args: { path: "unused.txt", steps: [{ action: "append", lines: ["a\nb"] }] },
894
+ field: "steps[0].lines[0]",
895
+ fix: fields?.lines?.description,
754
896
  },
755
897
  {
756
898
  name: "anchor without the line-hash shape",
757
- args: { path: "unused.txt", edits: [{ action: "replace", anchor: "abc", body: ["text"] }] },
758
- field: "edits[0].anchor",
899
+ args: { path: "unused.txt", steps: [{ action: "replace", anchor: "abc", lines: ["text"] }] },
900
+ field: "steps[0].anchor",
759
901
  fix: fields?.anchor?.description,
760
902
  },
761
903
  ] as const;
@@ -776,7 +918,7 @@ test("two unknown fields in one object → the shared fix appears once", async (
776
918
  await assert.rejects(
777
919
  callTool(edit, {
778
920
  path: "unused.txt",
779
- edits: [{ action: "append", body: ["text"], zzz: 1, yyy: 2 }],
921
+ steps: [{ action: "append", lines: ["text"], zzz: 1, yyy: 2 }],
780
922
  }),
781
923
  (error: unknown) => {
782
924
  const result = argumentError(error);