@criogaid/pi-hashline-edit 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/README.md +4 -3
  2. package/README.zh-CN.md +4 -3
  3. package/package.json +1 -1
  4. package/src/core/apply.test.ts +2 -2
  5. package/src/core/apply.ts +2 -2
  6. package/src/core/character-set.test.ts +2 -3
  7. package/src/core/character-set.ts +1 -1
  8. package/src/core/ranges.test.ts +2 -1
  9. package/src/core/ranges.ts +1 -1
  10. package/src/core/types.ts +1 -1
  11. package/src/index.ts +8 -8
  12. package/src/integration/config-registration.test.ts +8 -10
  13. package/src/integration/fusion-dispatch.test.ts +2 -2
  14. package/src/integration/grep-errors.test.ts +3 -3
  15. package/src/integration/grep-flags.test.ts +5 -5
  16. package/src/integration/grep-js-completeness.test.ts +12 -11
  17. package/src/integration/grep-line-candidates.test.ts +3 -2
  18. package/src/integration/grep-matrix.test.ts +5 -4
  19. package/src/integration/grep-order.test.ts +4 -4
  20. package/src/integration/grep-path-matrix.test.ts +5 -4
  21. package/src/integration/grep-prefilter-coverage.test.ts +4 -3
  22. package/src/integration/grep-process-limits.test.ts +2 -4
  23. package/src/integration/grep-regex.test.ts +10 -10
  24. package/src/integration/grep-rg.test.ts +50 -48
  25. package/src/integration/grep-scan-budget.test.ts +8 -8
  26. package/src/integration/grep-translation.test.ts +8 -7
  27. package/src/integration/large-text.test.ts +5 -5
  28. package/src/integration/model-budget-lifecycle.test.ts +9 -9
  29. package/src/integration/mutation-file-types.testing.ts +4 -4
  30. package/src/integration/npm-package.testing.ts +3 -3
  31. package/src/integration/publication-read-changes.test.ts +54 -54
  32. package/src/integration/read-changes.test.ts +4 -4
  33. package/src/integration/replace-worker-boundary.test.ts +1 -1
  34. package/src/integration/report-replay.test.ts +5 -6
  35. package/src/integration/utf8-errors.test.ts +4 -4
  36. package/src/pi/AGENTS.md +6 -5
  37. package/src/pi/action-fusion.test.ts +125 -105
  38. package/src/pi/action-fusion.testing.ts +38 -0
  39. package/src/pi/action-fusion.ts +9 -21
  40. package/src/pi/argument-diagnostics.ts +4 -3
  41. package/src/pi/argument-format.test.ts +19 -18
  42. package/src/pi/argument-validation.test.ts +218 -139
  43. package/src/pi/budgets.ts +1 -4
  44. package/src/pi/codemode.test.ts +7 -2
  45. package/src/pi/config.test.ts +2 -1
  46. package/src/pi/config.ts +8 -3
  47. package/src/pi/diagnostic-buffer.test.ts +23 -13
  48. package/src/pi/edit-tool.ts +47 -62
  49. package/src/pi/error-text.ts +7 -2
  50. package/src/pi/execute.test.ts +174 -157
  51. package/src/pi/failure-context.test.ts +4 -3
  52. package/src/pi/failure-context.ts +1 -1
  53. package/src/pi/failure-path.test.ts +37 -37
  54. package/src/pi/file-commit.test.ts +47 -19
  55. package/src/pi/file-commit.ts +36 -9
  56. package/src/pi/fusion-card.test.ts +22 -21
  57. package/src/pi/grep-candidates.test.ts +18 -20
  58. package/src/pi/grep-candidates.ts +8 -13
  59. package/src/pi/grep-matrix.test.ts +3 -3
  60. package/src/pi/grep-order.test.ts +12 -11
  61. package/src/pi/grep-output.ts +36 -15
  62. package/src/pi/grep-reread.ts +7 -2
  63. package/src/pi/grep-rg.testing.ts +70 -0
  64. package/src/pi/grep-scope.ts +4 -4
  65. package/src/pi/grep-search.ts +8 -11
  66. package/src/pi/grep-tool.ts +23 -39
  67. package/src/pi/grep.test.ts +102 -116
  68. package/src/pi/model-budgets.test.ts +2 -1
  69. package/src/pi/mutation-outcome.testing.ts +0 -1
  70. package/src/pi/mutation-result.test.ts +8 -8
  71. package/src/pi/mutation-result.ts +1 -1
  72. package/src/pi/mutation-runner.ts +9 -5
  73. package/src/pi/output-budgets.test.ts +53 -31
  74. package/src/pi/pi.test.ts +18 -13
  75. package/src/pi/read-tool.ts +13 -18
  76. package/src/pi/read.test.ts +22 -41
  77. package/src/pi/registration.test.ts +25 -19
  78. package/src/pi/render.test.ts +51 -32
  79. package/src/pi/render.ts +7 -4
  80. package/src/pi/replace-regex.ts +1 -1
  81. package/src/pi/replace-tool.ts +6 -16
  82. package/src/pi/replace.test.ts +45 -34
  83. package/src/pi/report-failure.testing.ts +9 -1
  84. package/src/pi/report-schema.ts +205 -83
  85. package/src/pi/report.test.ts +66 -64
  86. package/src/pi/report.ts +23 -17
  87. package/src/pi/result-safety.test.ts +121 -100
  88. package/src/pi/rg-process.test.ts +2 -2
  89. package/src/pi/rg-process.ts +9 -15
  90. package/src/pi/schema.ts +23 -0
  91. package/src/pi/tool-factories.testing.ts +59 -0
  92. package/src/pi/write-tool.ts +9 -15
  93. package/src/pi/write.test.ts +30 -31
  94. package/src/testing/run-tests.ts +1 -1
@@ -5,19 +5,26 @@ 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");
21
28
 
22
29
  const invalidCommand = { command: "", timeout: 0 };
23
30
  const anchorHash = "A".repeat(DEFAULT_CONFIG.hashLen);
@@ -41,31 +48,31 @@ const cases = [
41
48
  make: (cwd: string, fusion: ReturnType<typeof createActionFusionExecutor>) =>
42
49
  makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
43
50
  args: {
44
- edits: [
45
- { action: "replace", anchor: `1#${anchorHash}`, body: [] },
46
- { action: "append", body: ["bad\0", "bad\ud800"] },
51
+ steps: [
52
+ { action: "replace", anchor: `1#${anchorHash}`, lines: [] },
53
+ { action: "append", lines: ["bad\0", "bad\ud800"] },
47
54
  { 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"] },
55
+ { action: "append", lines: [] },
56
+ { action: "replace", anchor: `3#${anchorHash}`, end: `1#${anchorHash}`, lines: ["text"] },
50
57
  { action: "delete", anchor: `4#${anchorHash}`, end: `2#${anchorHash}` },
51
58
  ],
52
59
  then_run: invalidCommand,
53
60
  },
54
61
  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",
62
+ "steps[0].lines",
63
+ "steps[1].lines[0]",
64
+ "steps[1].lines[1]",
65
+ "steps[2].anchor",
66
+ "steps[2].end",
67
+ "steps[3].lines",
68
+ "steps[4].end",
69
+ "steps[5].end",
63
70
  "then_run.command",
64
71
  "then_run.timeout",
65
72
  ],
66
73
  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/ },
74
+ { field: "steps[4].end", reason: /line 1 is before anchor line 3/ },
75
+ { field: "steps[5].end", reason: /line 2 is before anchor line 4/ },
69
76
  ],
70
77
  },
71
78
  {
@@ -105,17 +112,21 @@ for (const scenario of cases) {
105
112
  const original = "original\n";
106
113
  await writeFile(path, original);
107
114
  let commandRuns = 0;
108
- const fusion = createActionFusionExecutor(async () => {
109
- commandRuns++;
110
- return { status: "succeeded", result: { content: [{ type: "text", text: "ran" }] } };
111
- });
115
+ const fusion = createActionFusionExecutor(() => {});
116
+ const ctx = {
117
+ ...(await createToolContext(cwd, t)),
118
+ executeTool: async () => {
119
+ commandRuns++;
120
+ return commandCompletion({ content: [{ type: "text", text: "ran" }], details: undefined });
121
+ },
122
+ };
112
123
  const tool = scenario.make(cwd, fusion);
113
124
  const args: unknown = JSON.parse(JSON.stringify({ path, ...scenario.args, unexpected: true }));
114
125
  const snapshot = structuredClone(args);
115
126
  const open = t.mock.method(fs, "open");
116
127
  syncBuiltinESMExports();
117
128
  try {
118
- await assert.rejects(callTool(tool, args), (error: unknown) => {
129
+ await assert.rejects(callTool(tool, args, { ctx }), (error: unknown) => {
119
130
  const result = argumentError(error);
120
131
  assert.deepEqual(
121
132
  new Set(result.issues.map((issue) => issue.field)),
@@ -141,6 +152,50 @@ for (const scenario of cases) {
141
152
  });
142
153
  }
143
154
 
155
+ test("blank then_run command with Fusion → reports exactly one command issue", async (t) => {
156
+ const cwd = process.cwd();
157
+ const fusion = createActionFusionExecutor(() => {});
158
+ const mutations = [
159
+ {
160
+ name: "edit",
161
+ tool: makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
162
+ args: { steps: [{ action: "append", lines: ["text"] }] },
163
+ },
164
+ {
165
+ name: "replace",
166
+ tool: makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
167
+ args: { replacements: [{ find: "old", replace: "new", literal: true }] },
168
+ },
169
+ {
170
+ name: "write",
171
+ tool: makeWriteOverride(cwd, fusion),
172
+ args: { content: "text", mode: "create" },
173
+ },
174
+ ] as const;
175
+ for (const scenario of mutations) {
176
+ for (const command of ["", " "]) {
177
+ await t.test(
178
+ `${scenario.name}/${JSON.stringify(command)} → rejects one command issue`,
179
+ async () => {
180
+ await assert.rejects(
181
+ callTool(scenario.tool, {
182
+ path: "unused.txt",
183
+ ...scenario.args,
184
+ then_run: { command },
185
+ }),
186
+ (error: unknown) => {
187
+ const result = argumentError(error);
188
+ assert.equal(result.issues.length, 1, JSON.stringify(result));
189
+ assert.equal(result.issues[0].field, "then_run.command");
190
+ return true;
191
+ },
192
+ );
193
+ },
194
+ );
195
+ }
196
+ }
197
+ });
198
+
144
199
  test("grep and replace flags → accept V8 modes and reject invalid flags before patterns or file reads", async (t) => {
145
200
  const cwd = await mkdtemp(join(tmpdir(), "hashline-flags-"));
146
201
  t.after(() => rm(cwd, { recursive: true, force: true }));
@@ -185,12 +240,12 @@ test("grep and replace flags → accept V8 modes and reject invalid flags before
185
240
  }
186
241
  });
187
242
 
188
- const editFields = ["anchor", "end", "body"] as const;
243
+ const editFields = ["anchor", "end", "lines"] as const;
189
244
  type EditField = (typeof editFields)[number];
190
245
  const fieldValues = {
191
246
  anchor: `1#${anchorHash}`,
192
247
  end: `2#${anchorHash}`,
193
- body: ["text"],
248
+ lines: ["text"],
194
249
  };
195
250
  const actionCases: readonly {
196
251
  action: string;
@@ -200,31 +255,31 @@ const actionCases: readonly {
200
255
  }[] = [
201
256
  {
202
257
  action: "replace",
203
- required: ["anchor", "body"],
258
+ required: ["anchor", "lines"],
204
259
  optional: ["end"],
205
260
  expected: "text\nbeta\ngamma\n",
206
261
  },
207
262
  { action: "delete", required: ["anchor"], optional: ["end"], expected: "beta\ngamma\n" },
208
263
  {
209
264
  action: "insert_after",
210
- required: ["anchor", "body"],
265
+ required: ["anchor", "lines"],
211
266
  optional: [],
212
267
  expected: "alpha\ntext\nbeta\ngamma\n",
213
268
  },
214
269
  {
215
270
  action: "insert_before",
216
- required: ["anchor", "body"],
271
+ required: ["anchor", "lines"],
217
272
  optional: [],
218
273
  expected: "text\nalpha\nbeta\ngamma\n",
219
274
  },
220
- { action: "append", required: ["body"], optional: [], expected: "alpha\nbeta\ngamma\ntext\n" },
221
- { action: "prepend", required: ["body"], optional: [], expected: "text\nalpha\nbeta\ngamma\n" },
275
+ { action: "append", required: ["lines"], optional: [], expected: "alpha\nbeta\ngamma\ntext\n" },
276
+ { action: "prepend", required: ["lines"], optional: [], expected: "text\nalpha\nbeta\ngamma\n" },
222
277
  ];
223
278
 
224
279
  for (const scenario of actionCases) {
225
280
  test(`${scenario.action} field matrix → requires its fields and rejects fields it does not take`, async (t) => {
226
281
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
227
- const fields = schemaProperties(tool).edits?.items?.properties;
282
+ const fields = schemaProperties(tool).steps?.items?.properties;
228
283
  const choice = fields?.action?.anyOf?.find((option) => option.const === scenario.action);
229
284
  assert.ok(choice?.description);
230
285
  const description = choice.description;
@@ -241,11 +296,11 @@ for (const scenario of actionCases) {
241
296
  Object.entries(valid).filter(([key]) => key !== field),
242
297
  );
243
298
  if (value === null) edit[field] = null;
244
- const args = { path: "unused.txt", edits: [edit] };
299
+ const args = { path: "unused.txt", steps: [edit] };
245
300
  await assert.rejects(callTool(tool, args), (error: unknown) => {
246
301
  assert.deepEqual(argumentError(error).issues, [
247
302
  {
248
- field: `edits[0].${field}`,
303
+ field: `steps[0].${field}`,
249
304
  reason: `is required for "${scenario.action}"`,
250
305
  fix: fields?.[field]?.description,
251
306
  },
@@ -263,12 +318,12 @@ for (const scenario of actionCases) {
263
318
  await assert.rejects(
264
319
  callTool(tool, {
265
320
  path: "unused.txt",
266
- edits: [{ ...valid, [field]: fieldValues[field] }],
321
+ steps: [{ ...valid, [field]: fieldValues[field] }],
267
322
  }),
268
323
  (error: unknown) => {
269
324
  assert.deepEqual(argumentError(error).issues, [
270
325
  {
271
- field: `edits[0].${field}`,
326
+ field: `steps[0].${field}`,
272
327
  reason: "is not allowed",
273
328
  fix: `"${scenario.action}": ${description}`,
274
329
  },
@@ -282,47 +337,31 @@ for (const scenario of actionCases) {
282
337
  });
283
338
  }
284
339
 
285
- test("edit action is missing or invalid → reports choices without guessing field permissions", async () => {
340
+ test("edit action is missing → reports choices without guessing field permissions", async () => {
286
341
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
287
- const choices = schemaProperties(tool).edits?.items?.properties?.action?.anyOf ?? [];
342
+ const choices = schemaProperties(tool).steps?.items?.properties?.action?.anyOf ?? [];
288
343
  assert.deepEqual(
289
344
  choices.map((choice) => choice.const),
290
345
  actionCases.map(({ action }) => action),
291
346
  );
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
- }
347
+ await assert.rejects(
348
+ callTool(tool, {
349
+ path: "unused.txt",
350
+ steps: [{ anchor: `1#${anchorHash}`, lines: ["valid"] }],
351
+ }),
352
+ (error: unknown) => {
353
+ const { issues } = argumentError(error);
354
+ assert.equal(issues.length, 1);
355
+ const issue = issues[0];
356
+ assert.equal(issue.field, "steps[0].action");
357
+ assert.equal(issue.reason, "is required");
358
+ for (const choice of choices) {
359
+ assert.ok(choice.description);
360
+ inOrder(issue.fix ?? "", [JSON.stringify(choice.const), choice.description]);
361
+ }
362
+ return true;
363
+ },
364
+ );
326
365
  });
327
366
 
328
367
  test("optional edit fields and then_run are null with Fusion → publishes bytes without rewriting caller arguments", async (t) => {
@@ -330,10 +369,14 @@ test("optional edit fields and then_run are null with Fusion → publishes bytes
330
369
  t.after(() => rm(cwd, { recursive: true, force: true }));
331
370
  const path = join(cwd, "target.txt");
332
371
  let commandRuns = 0;
333
- const fusion = createActionFusionExecutor(async () => {
334
- commandRuns++;
335
- return { status: "succeeded", result: { content: [{ type: "text", text: "ran" }] } };
336
- });
372
+ const fusion = createActionFusionExecutor(() => {});
373
+ const ctx = {
374
+ ...(await createToolContext(cwd, t)),
375
+ executeTool: async () => {
376
+ commandRuns++;
377
+ return commandCompletion({ content: [{ type: "text", text: "ran" }], details: undefined });
378
+ },
379
+ };
337
380
  const tool = makeEditOverride(cwd, DEFAULT_CONFIG, fusion);
338
381
  for (const scenario of actionCases) {
339
382
  await t.test(`${scenario.action} → accepts null optional fields`, async () => {
@@ -344,13 +387,13 @@ test("optional edit fields and then_run are null with Fusion → publishes bytes
344
387
  ...Object.fromEntries(
345
388
  scenario.required.map((field) => [
346
389
  field,
347
- field === "body" ? ["text"] : token(1, "alpha"),
390
+ field === "lines" ? ["text"] : token(1, "alpha"),
348
391
  ]),
349
392
  ),
350
393
  };
351
- const args = { path, edits: [edit], then_run: null };
394
+ const args = { path, steps: [edit], then_run: null };
352
395
  const snapshot = structuredClone(args);
353
- const result = await callTool(tool, args);
396
+ const result = await callTool(tool, args, { ctx });
354
397
  assert.equal(result.isError, undefined);
355
398
  assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
356
399
  assert.equal(reportOf(result, "edit")?.then_run, undefined);
@@ -376,7 +419,7 @@ test("null optional fields → grep applies defaults without filename filters",
376
419
  await onPath(path);
377
420
  return { code: 0, stderr: "", stopped: false };
378
421
  };
379
- const tool = makeGrepOverrideWithBackend(cwd, config, {
422
+ const tool = makeMockGrep(cwd, config, {
380
423
  runRgPaths: (args, _signal, onPath) => discover(args, onPath),
381
424
  runRgLines: (args, _signal, _maxLines, onFile) =>
382
425
  discover(args, (file) => onFile(file, undefined)),
@@ -404,19 +447,19 @@ test("edit batch has 64 or 65 operations → publishes the maximum or rejects th
404
447
  t.after(() => rm(cwd, { recursive: true, force: true }));
405
448
  const path = join(cwd, "target.txt");
406
449
  const tool = makeEditOverride(cwd, DEFAULT_CONFIG);
407
- const maxEdits: unknown = Reflect.get(tool.parameters.properties.edits, "maxItems");
450
+ const maxEdits: unknown = Reflect.get(tool.parameters.properties.steps, "maxItems");
408
451
  assert.ok(typeof maxEdits === "number");
409
452
  assert.equal(maxEdits, 64);
410
453
  const lines = Array.from({ length: maxEdits + 2 }, (_, i) => `line ${i + 1}`);
411
454
  const original = Buffer.from(`${lines.join("\n")}\n`);
412
455
  for (const count of [maxEdits, maxEdits + 1]) {
413
456
  await t.test(
414
- `${count} edits → ${count === maxEdits ? "publishes distinct deletions" : `rejects edits above max${maxEdits}`}`,
457
+ `${count} steps → ${count === maxEdits ? "publishes distinct deletions" : `rejects steps above max${maxEdits}`}`,
415
458
  async () => {
416
459
  await writeFile(path, original);
417
460
  const args = {
418
461
  path,
419
- edits: lines
462
+ steps: lines
420
463
  .slice(0, count)
421
464
  .map((line, i) => ({ action: "delete", anchor: token(i + 1, line) })),
422
465
  };
@@ -430,7 +473,7 @@ test("edit batch has 64 or 65 operations → publishes the maximum or rejects th
430
473
  await assert.rejects(callTool(tool, args), (error: unknown) => {
431
474
  const report = argumentError(error);
432
475
  assert.equal(report.issues.length, 1);
433
- assert.equal(report.issues[0].field, "edits");
476
+ assert.equal(report.issues[0].field, "steps");
434
477
  assert.match(
435
478
  report.issues[0].reason,
436
479
  new RegExp(`must not have more than ${maxEdits} items`),
@@ -450,12 +493,12 @@ test("edit item is not an object → report the item type once", async () => {
450
493
  await assert.rejects(
451
494
  callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
452
495
  path: "unused.txt",
453
- edits: [item],
496
+ steps: [item],
454
497
  }),
455
498
  (error: unknown) => {
456
499
  const result = argumentError(error);
457
500
  assert.equal(result.issues.length, 1);
458
- return rejectsArgument("edits[0]", /object/)(error);
501
+ return rejectsArgument("steps[0]", /object/)(error);
459
502
  },
460
503
  );
461
504
  }
@@ -476,17 +519,6 @@ test("grep union value has one applicable type → report only its constraint",
476
519
  }
477
520
  });
478
521
 
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
522
  test("unknown field names contain punctuation, numeric keys or newlines → quote property keys without treating them as array indices", async () => {
491
523
  for (const key of ["odd.\nfield/~", "0", "01", "a/b", "a~b", 'quote"field']) {
492
524
  await assert.rejects(
@@ -508,12 +540,12 @@ test("one field exhausts native diagnostics → label the limit and retain anoth
508
540
  await assert.rejects(
509
541
  callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
510
542
  path: "",
511
- edits: Array.from({ length: 30 }, () => ({ action: "unknown" })),
543
+ steps: Array.from({ length: 30 }, () => ({ action: "unknown" })),
512
544
  }),
513
545
  (error: unknown) => {
514
546
  const result = argumentError(error);
515
547
  assert.ok(result.issues.some((issue) => issue.field === "path"));
516
- assert.ok(result.issues.some((issue) => issue.field === "edits[0].action"));
548
+ assert.ok(result.issues.some((issue) => issue.field === "steps[0].action"));
517
549
  assert.equal(result.schemaLimited, true);
518
550
  return true;
519
551
  },
@@ -524,12 +556,12 @@ test("one field violates independent constraints → combine its reasons without
524
556
  await assert.rejects(
525
557
  callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
526
558
  path: "unused.txt",
527
- edits: [{ action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#A` }],
559
+ steps: [{ action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#A` }],
528
560
  }),
529
561
  (error: unknown) => {
530
562
  const result = argumentError(error);
531
563
  assert.equal(result.issues.length, 1);
532
- return rejectsArgument("edits[0].anchor", /safe integer[\s\S]*hash characters/)(error);
564
+ return rejectsArgument("steps[0].anchor", /safe integer[\s\S]*hash characters/)(error);
533
565
  },
534
566
  );
535
567
  await assert.rejects(
@@ -557,7 +589,7 @@ test("edit fields contain semantic errors → checks only fields accepted by the
557
589
  for (const field of editFields) {
558
590
  const accepted = scenario.required.includes(field) || scenario.optional.includes(field);
559
591
  const values: readonly (readonly [unknown, RegExp])[] =
560
- field === "body"
592
+ field === "lines"
561
593
  ? [
562
594
  [[], /empty/],
563
595
  [["bad\0"], /NUL/],
@@ -573,18 +605,18 @@ test("edit fields contain semantic errors → checks only fields accepted by the
573
605
  async () => {
574
606
  const edit = { ...valid, [field]: value };
575
607
  await assert.rejects(
576
- callTool(tool, { path: "unused.txt", edits: [edit] }),
608
+ callTool(tool, { path: "unused.txt", steps: [edit] }),
577
609
  (error: unknown) => {
578
610
  const report = argumentError(error);
579
611
  assert.equal(report.issues.length, 1, JSON.stringify(report));
580
612
  const issue = report.issues[0];
581
613
  assert.equal(
582
614
  issue.field,
583
- `edits[0].${field}${accepted && field === "body" && Array.isArray(value) && value.length ? "[0]" : ""}`,
615
+ `steps[0].${field}${accepted && field === "lines" && Array.isArray(value) && value.length ? "[0]" : ""}`,
584
616
  );
585
617
  if (accepted) {
586
618
  assert.match(issue.reason, reason);
587
- if (field === "body" && Array.isArray(value) && value.length === 0) {
619
+ if (field === "lines" && Array.isArray(value) && value.length === 0) {
588
620
  assert.match(
589
621
  issue.fix ?? "",
590
622
  scenario.action === "replace"
@@ -635,7 +667,7 @@ test("missing required parameter → fix states the parameter's role from its sc
635
667
  discoveries++;
636
668
  return { code: 1, stderr: "", stopped: false };
637
669
  };
638
- const grep = makeGrepOverrideWithBackend(process.cwd(), DEFAULT_CONFIG, {
670
+ const grep = makeMockGrep(process.cwd(), DEFAULT_CONFIG, {
639
671
  runRgPaths: discover,
640
672
  runRgLines: discover,
641
673
  });
@@ -678,7 +710,7 @@ test("missing required parameter → fix states the parameter's role from its sc
678
710
  test("two operations miss the same required field → the shared fix appears once", async () => {
679
711
  const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
680
712
  await assert.rejects(
681
- callTool(tool, { path: "unused.txt", edits: [{ action: "replace" }, { action: "replace" }] }),
713
+ callTool(tool, { path: "unused.txt", steps: [{ action: "replace" }, { action: "replace" }] }),
682
714
  (error: unknown) => {
683
715
  const result = argumentError(error);
684
716
  const anchors = result.issues.filter((issue) => issue.field.endsWith(".anchor"));
@@ -686,7 +718,7 @@ test("two operations miss the same required field → the shared fix appears onc
686
718
  assert.equal(anchors.filter((issue) => issue.fix).length, 1);
687
719
  const fixes = result.issues.flatMap((issue) => (issue.fix ? [issue.fix] : []));
688
720
  assert.equal(new Set(fixes).size, fixes.length, JSON.stringify(fixes));
689
- const description = schemaProperties(tool).edits?.items?.properties?.anchor?.description;
721
+ const description = schemaProperties(tool).steps?.items?.properties?.anchor?.description;
690
722
  assert.ok(description);
691
723
  assert.ok(fixes.includes(description));
692
724
  return true;
@@ -694,23 +726,70 @@ test("two operations miss the same required field → the shared fix appears onc
694
726
  );
695
727
  });
696
728
 
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;
729
+ test("out-of-choice value → reason lists values and fix explains each choice", async (t) => {
730
+ const cwd = process.cwd();
731
+ const cases = [
732
+ {
733
+ tool: makeEditOverride(cwd, DEFAULT_CONFIG),
734
+ field: "steps[0].action",
735
+ values: ["unknown", null, 42, {}],
736
+ args: (action: unknown) => ({
737
+ path: "unused.txt",
738
+ steps: [{ action, anchor: `1#${anchorHash}`, lines: ["valid"] }],
739
+ }),
740
+ choices: [
741
+ ["replace", "Replace the cited line(s)"],
742
+ ["delete", "Delete the cited line(s)"],
743
+ ["insert_after", "Insert lines after the anchor line"],
744
+ ["insert_before", "Insert lines before the anchor line"],
745
+ ["append", "Add lines at the end"],
746
+ ["prepend", "Add lines at the start"],
747
+ ],
712
748
  },
713
- );
749
+ {
750
+ tool: makeWriteOverride(cwd),
751
+ field: "mode",
752
+ values: ["unknown"],
753
+ args: (mode: unknown) => ({ path: "unused.txt", content: "ok", mode }),
754
+ choices: [
755
+ ["create", "Fail if the target already exists."],
756
+ ["overwrite", "Fail if the target does not exist."],
757
+ ],
758
+ },
759
+ {
760
+ tool: makeGrepOverride(cwd, DEFAULT_CONFIG),
761
+ field: "outputMode",
762
+ values: ["unknown"],
763
+ args: (outputMode: unknown) => ({ pattern: "text", literal: true, outputMode }),
764
+ choices: [
765
+ ["content", "Matches and context", "anchors", "plain previews"],
766
+ ["files", "Paths of the files", "only paths"],
767
+ ["count", "Matching lines per file", "total", "only counts"],
768
+ ],
769
+ },
770
+ ] as const;
771
+ for (const scenario of cases) {
772
+ await t.test(`${scenario.tool.name}/${scenario.field}`, async () => {
773
+ for (const value of scenario.values) {
774
+ await assert.rejects(callTool(scenario.tool, scenario.args(value)), (error: unknown) => {
775
+ const { issues } = argumentError(error);
776
+ assert.equal(issues.length, 1, JSON.stringify(issues));
777
+ const issue = issues[0];
778
+ assert.equal(issue.field, scenario.field);
779
+ assert.equal(
780
+ issue.reason,
781
+ `expected one of: ${scenario.choices.map(([value]) => JSON.stringify(value)).join(", ")}`,
782
+ );
783
+ assert.ok(issue.fix, JSON.stringify(issues));
784
+ inOrder(
785
+ issue.fix,
786
+ scenario.choices.flatMap(([value, ...meaning]) => [JSON.stringify(value), ...meaning]),
787
+ );
788
+ return true;
789
+ });
790
+ }
791
+ });
792
+ }
714
793
  });
715
794
 
716
795
  const acceptedFields = (shape: SchemaNode): string =>
@@ -722,9 +801,9 @@ test("unknown field is supplied → fix lists only the flat object's accepted fi
722
801
  for (const scenario of [
723
802
  {
724
803
  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.",
804
+ args: { path: "unused.txt", steps: [{ action: "append", lines: ["text"], op: "append" }] },
805
+ field: "steps[0].op",
806
+ fix: "Accepted fields: action, anchor, end, lines.",
728
807
  },
729
808
  {
730
809
  tool: grep,
@@ -744,18 +823,18 @@ test("unknown field is supplied → fix lists only the flat object's accepted fi
744
823
 
745
824
  test("value mismatches the field pattern → fix states the field description from the schema", async () => {
746
825
  const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
747
- const fields = schemaProperties(edit).edits?.items?.properties;
826
+ const fields = schemaProperties(edit).steps?.items?.properties;
748
827
  const cases = [
749
828
  {
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,
829
+ name: "line element with a newline",
830
+ args: { path: "unused.txt", steps: [{ action: "append", lines: ["a\nb"] }] },
831
+ field: "steps[0].lines[0]",
832
+ fix: fields?.lines?.description,
754
833
  },
755
834
  {
756
835
  name: "anchor without the line-hash shape",
757
- args: { path: "unused.txt", edits: [{ action: "replace", anchor: "abc", body: ["text"] }] },
758
- field: "edits[0].anchor",
836
+ args: { path: "unused.txt", steps: [{ action: "replace", anchor: "abc", lines: ["text"] }] },
837
+ field: "steps[0].anchor",
759
838
  fix: fields?.anchor?.description,
760
839
  },
761
840
  ] as const;
@@ -776,7 +855,7 @@ test("two unknown fields in one object → the shared fix appears once", async (
776
855
  await assert.rejects(
777
856
  callTool(edit, {
778
857
  path: "unused.txt",
779
- edits: [{ action: "append", body: ["text"], zzz: 1, yyy: 2 }],
858
+ steps: [{ action: "append", lines: ["text"], zzz: 1, yyy: 2 }],
780
859
  }),
781
860
  (error: unknown) => {
782
861
  const result = argumentError(error);
package/src/pi/budgets.ts CHANGED
@@ -23,7 +23,7 @@ const fallbackBudgets = {
23
23
  fieldBytes: 4 * 1024,
24
24
  grepPreviewUnits: 500,
25
25
  };
26
- export const DEFAULT_OUTPUT_BUDGETS = Object.freeze(fallbackBudgets);
26
+ const DEFAULT_OUTPUT_BUDGETS = Object.freeze(fallbackBudgets);
27
27
  export type OutputBudgets = typeof DEFAULT_OUTPUT_BUDGETS;
28
28
  export type OutputBudgetProvider = () => OutputBudgets;
29
29
 
@@ -45,9 +45,6 @@ export function outputBudgetsFor(contextWindow: number | undefined): OutputBudge
45
45
  });
46
46
  }
47
47
 
48
- /** Standalone tool factories have no session model until a provider is supplied. */
49
- export const defaultOutputBudgets: OutputBudgetProvider = () => DEFAULT_OUTPUT_BUDGETS;
50
-
51
48
  const MIB = 1024 * 1024;
52
49
 
53
50
  /** Size wording for every model-facing limit: whole MiB as `256 MiB`, otherwise `195.3 KiB`. */