@criogaid/pi-hashline-edit 0.8.0 → 0.10.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 +13 -10
  2. package/README.zh-CN.md +12 -9
  3. package/THIRD_PARTY_NOTICES.md +0 -6
  4. package/package.json +1 -1
  5. package/src/core/AGENTS.md +4 -5
  6. package/src/core/apply.test.ts +118 -293
  7. package/src/core/apply.ts +63 -165
  8. package/src/core/case-fold.ts +8 -67
  9. package/src/core/character-set.ts +4 -4
  10. package/src/core/errors.test.ts +23 -7
  11. package/src/core/errors.ts +32 -5
  12. package/src/core/hash.test.ts +8 -16
  13. package/src/core/hash.ts +3 -23
  14. package/src/core/lines.ts +2 -4
  15. package/src/core/ranges.test.ts +16 -8
  16. package/src/core/ranges.ts +10 -6
  17. package/src/core/regex.ts +3 -4
  18. package/src/core/replace.test.ts +47 -1
  19. package/src/core/replace.ts +19 -13
  20. package/src/core/search-analysis.test.ts +24 -27
  21. package/src/core/search-analysis.ts +34 -65
  22. package/src/core/search-matrix.testing.ts +0 -2
  23. package/src/core/search.ts +1 -1
  24. package/src/core/text.test.ts +5 -5
  25. package/src/core/text.ts +5 -12
  26. package/src/core/types.ts +7 -43
  27. package/src/integration/config-registration.test.ts +2 -2
  28. package/src/integration/fusion-dispatch.test.ts +2 -5
  29. package/src/integration/grep-flags.test.ts +0 -14
  30. package/src/integration/grep-js-completeness.test.ts +34 -8
  31. package/src/integration/grep-line-candidates.test.ts +4 -8
  32. package/src/integration/grep-matrix.test.ts +3 -7
  33. package/src/integration/grep-path-matrix.test.ts +5 -5
  34. package/src/integration/grep-prefilter-coverage.test.ts +3 -7
  35. package/src/integration/grep-process-limits.test.ts +78 -0
  36. package/src/integration/grep-rg.test.ts +123 -25
  37. package/src/integration/grep-scan-budget.test.ts +2 -2
  38. package/src/integration/grep-translation.test.ts +12 -27
  39. package/src/integration/large-text.test.ts +1 -47
  40. package/src/integration/model-budget-lifecycle.test.ts +13 -3
  41. package/src/integration/mutation-file-types.testing.ts +1 -1
  42. package/src/integration/npm-package.testing.ts +1 -1
  43. package/src/integration/publication-read-changes.test.ts +125 -47
  44. package/src/integration/replace-worker-boundary.test.ts +21 -11
  45. package/src/integration/report-replay.test.ts +21 -23
  46. package/src/integration/utf8-errors.test.ts +1 -1
  47. package/src/pi/AGENTS.md +16 -16
  48. package/src/pi/action-fusion.test.ts +168 -300
  49. package/src/pi/action-fusion.ts +148 -111
  50. package/src/pi/anchor-format.test.ts +9 -9
  51. package/src/pi/anchor-format.ts +6 -8
  52. package/src/pi/argument-diagnostics.ts +52 -207
  53. package/src/pi/argument-error.ts +2 -1
  54. package/src/pi/argument-format.test.ts +20 -22
  55. package/src/pi/argument-validation.test.ts +382 -418
  56. package/src/pi/argument-validation.ts +5 -8
  57. package/src/pi/budgets.ts +5 -4
  58. package/src/pi/config.test.ts +5 -2
  59. package/src/pi/config.ts +4 -6
  60. package/src/pi/diagnostic-buffer.test.ts +12 -1
  61. package/src/pi/diagnostic-buffer.ts +1 -2
  62. package/src/pi/directory-sync.test.ts +0 -1
  63. package/src/pi/edit-tool.ts +173 -226
  64. package/src/pi/error-text.ts +4 -8
  65. package/src/pi/execute.test.ts +165 -266
  66. package/src/pi/failure-context.test.ts +144 -126
  67. package/src/pi/failure-context.ts +45 -94
  68. package/src/pi/failure-path.test.ts +168 -141
  69. package/src/pi/file-commit.test.ts +110 -53
  70. package/src/pi/file-commit.ts +90 -137
  71. package/src/pi/file-snapshot.test.ts +3 -5
  72. package/src/pi/fusion-card.test.ts +4 -1
  73. package/src/pi/fusion-card.ts +16 -24
  74. package/src/pi/grep-candidates.test.ts +2 -101
  75. package/src/pi/grep-candidates.ts +23 -69
  76. package/src/pi/grep-matrix.test.ts +2 -2
  77. package/src/pi/grep-order.test.ts +4 -4
  78. package/src/pi/grep-output.ts +62 -96
  79. package/src/pi/grep-render.ts +1 -1
  80. package/src/pi/grep-reread.ts +92 -0
  81. package/src/pi/grep-scope.ts +40 -42
  82. package/src/pi/grep-search.ts +22 -31
  83. package/src/pi/grep-tool.ts +55 -82
  84. package/src/pi/grep.test.ts +81 -46
  85. package/src/pi/model-budgets.test.ts +25 -23
  86. package/src/pi/model-budgets.ts +6 -38
  87. package/src/pi/mutation-result.test.ts +44 -35
  88. package/src/pi/mutation-result.ts +51 -20
  89. package/src/pi/mutation-runner.ts +126 -70
  90. package/src/pi/output-budgets.test.ts +38 -16
  91. package/src/pi/path.ts +1 -1
  92. package/src/pi/pi.test.ts +3 -1
  93. package/src/pi/read-tool.ts +5 -5
  94. package/src/pi/regex-worker-contract.ts +3 -13
  95. package/src/pi/regex-worker.ts +1 -10
  96. package/src/pi/registration.test.ts +82 -20
  97. package/src/pi/render.test.ts +79 -42
  98. package/src/pi/render.ts +69 -63
  99. package/src/pi/replace-regex.ts +14 -2
  100. package/src/pi/replace-tool.ts +22 -83
  101. package/src/pi/replace-worker.ts +1 -1
  102. package/src/pi/replace.test.ts +9 -5
  103. package/src/pi/report-cause.ts +3 -8
  104. package/src/pi/report-failure.testing.ts +1 -2
  105. package/src/pi/report-schema.ts +65 -81
  106. package/src/pi/report.test.ts +171 -25
  107. package/src/pi/report.ts +33 -87
  108. package/src/pi/result-safety.test.ts +177 -189
  109. package/src/pi/rg-process.test.ts +77 -63
  110. package/src/pi/rg-process.ts +48 -64
  111. package/src/pi/rg-regex.ts +40 -400
  112. package/src/pi/schema.ts +12 -0
  113. package/src/pi/text-stream.test.ts +0 -21
  114. package/src/pi/text-stream.ts +14 -21
  115. package/src/pi/write-tool.ts +33 -62
  116. package/src/pi/write.test.ts +1 -3
  117. package/src/testing/regex-translation-corpus.ts +12 -12
  118. package/src/pi/replace-worker-contract.ts +0 -10
@@ -11,7 +11,6 @@ import {
11
11
  type TLiteralValue,
12
12
  type TObject,
13
13
  type TSchema,
14
- type TUnion,
15
14
  } from "typebox";
16
15
  import type { TLocalizedValidationError } from "typebox/error";
17
16
  import { Value } from "typebox/value";
@@ -22,22 +21,14 @@ interface ArgumentDiagnostics {
22
21
  readonly limited: boolean;
23
22
  }
24
23
 
25
- /** The declared object a value was projected with, and the tag field that selected it among branches. */
26
- interface SelectedShape {
27
- readonly object: TObject;
28
- readonly tag?: string;
29
- }
30
-
31
24
  /** State shared by one failure projection. */
32
25
  interface Projection {
33
- /** Fields a tool check already explained; their schema constraints are dropped. */
26
+ /** Fields a tool check already explained; their schema constraints are dropped, except an object's fields'. */
34
27
  readonly explained: ReadonlySet<string>;
35
- /** Fields that belong to the selected shapes. */
28
+ /** Fields that belong to the projected shapes; a declared field belongs even when absent. */
36
29
  readonly applicable: Set<string>;
37
- /** Branch constraints that native diagnostics would split across branches. */
38
- readonly derived: ArgumentIssue[];
39
- /** Each object path's selected shape; paths left to several branches are absent. */
40
- readonly selected: Map<string, SelectedShape>;
30
+ /** Each object path's declared shape. */
31
+ readonly objects: Map<string, TObject>;
41
32
  }
42
33
 
43
34
  function literalValues(schema: TSchema): readonly TLiteralValue[] | undefined {
@@ -48,48 +39,6 @@ function literalValues(schema: TSchema): readonly TLiteralValue[] | undefined {
48
39
  return choices.length === values.length ? choices.flat() : undefined;
49
40
  }
50
41
 
51
- function objectTag(schema: TUnion):
52
- | {
53
- readonly field: string;
54
- readonly branches: readonly TObject[];
55
- readonly choices: readonly TLiteralValue[];
56
- }
57
- | undefined {
58
- const branches = schema.anyOf.filter(IsObject);
59
- const first = branches[0];
60
- if (!first || branches.length !== schema.anyOf.length) return undefined;
61
- for (const field of Object.keys(first.properties)) {
62
- if (!branches.every((branch) => branch.required?.includes(field))) continue;
63
- const values = branches.map((branch) => literalValues(branch.properties[field]));
64
- const choices = values.filter((value) => value !== undefined);
65
- if (choices.length === branches.length) return { field, branches, choices: choices.flat() };
66
- }
67
- return undefined;
68
- }
69
-
70
- /**
71
- * Fields that alone tell tag-sharing branches apart: each branch requires one
72
- * field that every other branch forbids, and shares all others unchanged.
73
- * Shared fields are compared by schema identity, so branches must reuse one
74
- * instance per field; otherwise the general union projection applies.
75
- */
76
- function exclusiveFields(branches: readonly TObject[]): readonly string[] | undefined {
77
- const fields = branches.map((branch) => {
78
- const own = Object.keys(branch.properties).filter((field) =>
79
- branches.some((other) => other.properties[field] !== branch.properties[field]),
80
- );
81
- return own.length === 1 &&
82
- branch.required?.includes(own[0]) &&
83
- ObjectOptions(branch).additionalProperties === false
84
- ? own[0]
85
- : undefined;
86
- });
87
- return fields.every((field): field is string => field !== undefined) &&
88
- new Set(fields).size === fields.length
89
- ? fields
90
- : undefined;
91
- }
92
-
93
42
  function projectSchema(
94
43
  schema: TSchema,
95
44
  value: unknown,
@@ -112,106 +61,25 @@ function projectValueSchema(
112
61
  projection.applicable.add(path);
113
62
  return Type.Unknown({ enum: [...new Set(choices)] });
114
63
  }
115
- const tag = objectTag(schema);
116
- if (tag) {
117
- if (typeof value !== "object" || value === null || Array.isArray(value))
118
- return Type.Unknown({ type: "object" });
119
- const select = (branch: TObject): TSchema => {
120
- const projected = projectSchema(branch, value, path, projection);
121
- projection.selected.set(path, { object: branch, tag: tag.field });
122
- return projected;
123
- };
124
- const matches = tag.branches.filter((branch) =>
125
- Value.Check(branch.properties[tag.field], Reflect.get(value, tag.field)),
126
- );
127
- if (matches.length === 1) return select(matches[0]);
128
- if (matches.length === 0)
129
- return Type.Object({ [tag.field]: Type.Unknown({ enum: [...new Set(tag.choices)] }) });
130
- // Distinct property presence can disambiguate branches sharing a tag even
131
- // when common fields are missing or field values are invalid. Only choose
132
- // a branch here; its original constraints still supply the diagnostics.
133
- const knownFields = new Set(matches.flatMap((branch) => Object.keys(branch.properties)));
134
- const shapeValue = Object.fromEntries(
135
- Object.entries(value).filter(([field]) => knownFields.has(field)),
136
- );
137
- const shapes = matches.filter((branch) =>
138
- Value.Check(
139
- Type.Object(
140
- Object.fromEntries(
141
- Object.entries(branch.properties).map(([field, child]) => [
142
- field,
143
- IsOptional(child) ||
144
- matches.every((other) => Object.hasOwn(other.properties, field))
145
- ? Type.Optional(Type.Unknown())
146
- : Type.Unknown(),
147
- ]),
148
- ),
149
- ObjectOptions(branch),
150
- ),
151
- shapeValue,
152
- ),
153
- );
154
- if (shapes.length === 1) return select(shapes[0]);
155
- // Branches set apart only by exclusive fields (for example before/after
156
- // destinations) fail together when none or several are given. Validate their
157
- // merged shape once and state the choice as one issue, instead of each branch
158
- // requiring its own field and forbidding the others.
159
- const alternatives = exclusiveFields(matches);
160
- if (alternatives) {
161
- const given = alternatives.filter((field) => Object.hasOwn(value, field));
162
- if (given.length !== 1)
163
- projection.derived.push({
164
- field: path || "$",
165
- reason: `expected exactly one of: ${alternatives.join(", ")}`,
166
- });
167
- const merged = Type.Object(
168
- Object.fromEntries(
169
- matches
170
- .flatMap((branch) => Object.entries(branch.properties))
171
- .map(([field, child]) => [
172
- field,
173
- alternatives.includes(field) ? Type.Optional(child) : child,
174
- ]),
175
- ),
176
- ObjectOptions(matches[0]),
177
- );
178
- return select(merged);
179
- }
180
- // Otherwise project every applicable branch so semantic checks are retained, then
181
- // let the native engine select a shape after already-explained constraints are removed.
182
- const projected = matches.map((branch) => projectSchema(branch, value, path, projection));
183
- projection.selected.delete(path);
184
- const accepted = projected.filter((branch) => Value.Check(branch, value));
185
- return accepted.length === 1 ? accepted[0] : Type.Union(projected);
186
- }
187
- const typed = schema.anyOf.filter((branch) => "type" in branch);
188
- if (typed.length === schema.anyOf.length) {
189
- const matches = typed.filter(
190
- (branch) => "type" in branch && Value.Check(Type.Unknown({ type: branch.type }), value),
191
- );
192
- if (matches.length === 1) return projectSchema(matches[0], value, path, projection);
193
- if (matches.length === 0) {
194
- const types = typed.flatMap((branch) =>
195
- "type" in branch ? (Array.isArray(branch.type) ? branch.type : [branch.type]) : [],
196
- );
197
- return Type.Unknown({ type: [...new Set(types)] });
198
- }
199
- }
200
64
  return schema;
201
65
  }
202
66
  if ("type" in schema && !Value.Check(Type.Unknown({ type: schema.type }), value)) return schema;
203
67
  projection.applicable.add(path);
204
- if (projection.explained.has(path)) return Type.Unknown();
68
+ // An issue on a whole object, such as a choice between its fields, explains
69
+ // none of the object's own constraints; its fields keep their diagnostics.
205
70
  if (IsObject(schema) && typeof value === "object" && value !== null) {
206
- projection.selected.set(path, { object: schema });
71
+ projection.objects.set(path, schema);
207
72
  const properties = Object.fromEntries(
208
- Object.entries(schema.properties).map(([key, child]) => [
209
- key,
210
- projectSchema(child, Reflect.get(value, key), propertyPath(path, key), projection),
211
- ]),
73
+ Object.entries(schema.properties).map(([key, child]) => {
74
+ const field = propertyPath(path, key);
75
+ // A tool check may require a field the schema leaves optional.
76
+ if (!Object.hasOwn(value, key)) projection.applicable.add(field);
77
+ return [key, projectSchema(child, Reflect.get(value, key), field, projection)];
78
+ }),
212
79
  );
213
80
  return Type.Object(properties, ObjectOptions(schema));
214
81
  }
82
+ if (projection.explained.has(path)) return Type.Unknown();
215
83
  if (IsArray(schema) && Array.isArray(value)) {
216
84
  const items = value.map((item, index) =>
217
85
  projectSchema(schema.items, item, `${path}[${index}]`, projection),
@@ -238,14 +106,15 @@ function pointerParts(pointer: string): readonly string[] {
238
106
  }
239
107
 
240
108
  /**
241
- * The projection path of the value at `pointer`; the root is "". Pi's TypeBox leaves `/` and
242
- * `~` in property names unescaped, so a name spans the fewest segments that form an own key,
243
- * read escaped or as is.
109
+ * The projection paths of the value at `pointer` and of its parent; the root is "". Pi's TypeBox
110
+ * leaves `/` and `~` in property names unescaped, so a name spans the fewest segments that form
111
+ * an own key, read escaped or as is.
244
112
  */
245
- function valuePath(pointer: string, prepared: unknown): string {
113
+ function valuePaths(pointer: string, prepared: unknown): { path: string; parent: string } {
246
114
  const segments = pointer.split("/").slice(1);
247
115
  let value = prepared;
248
116
  let path = "";
117
+ let parent = "";
249
118
  for (let index = 0; index < segments.length; index++) {
250
119
  let part = unescapePointer(segments[index]);
251
120
  if (typeof value === "object" && value !== null && !Array.isArray(value)) {
@@ -260,14 +129,15 @@ function valuePath(pointer: string, prepared: unknown): string {
260
129
  }
261
130
  }
262
131
  }
132
+ parent = path;
263
133
  path = Array.isArray(value) ? `${path}[${part}]` : propertyPath(path, part);
264
134
  value = typeof value === "object" && value !== null ? Reflect.get(value, part) : undefined;
265
135
  }
266
- return path;
136
+ return { path, parent };
267
137
  }
268
138
 
269
139
  function fieldPath(pointer: string, prepared: unknown, key?: string): string {
270
- const path = valuePath(pointer, prepared);
140
+ const { path } = valuePaths(pointer, prepared);
271
141
  return (key === undefined ? path : propertyPath(path, key)) || "$";
272
142
  }
273
143
 
@@ -284,43 +154,24 @@ function explainDeclaration(schema: TSchema): string | undefined {
284
154
  }
285
155
 
286
156
  /**
287
- * What the field at `parts` does, from its declarations in every branch.
288
- * Choices label each value, so every branch's choices are listed together;
289
- * other declarations must agree, so one branch's description is never
290
- * attributed to another. An array item without its own description is
291
- * explained by its array.
157
+ * What the field at `parts` does, from its declaration. An array item without
158
+ * its own description is explained by its array.
292
159
  */
293
160
  function fieldExplanation(schema: TSchema, parts: readonly string[]): string | undefined {
294
- const declarations = new Set<TSchema>();
295
- const visit = (node: TSchema, depth: number): void => {
296
- if (depth === parts.length) declarations.add(node);
297
- else if (IsUnion(node)) for (const branch of node.anyOf) visit(branch, depth);
298
- else if (IsObject(node)) {
299
- if (Object.hasOwn(node.properties, parts[depth]))
300
- visit(node.properties[parts[depth]], depth + 1);
301
- } else if (IsArray(node))
302
- visit(
303
- depth + 1 === parts.length && explainDeclaration(node.items) === undefined
304
- ? node
305
- : node.items,
306
- depth + 1,
307
- );
308
- };
309
- visit(schema, 0);
310
- if (declarations.size === 0) return undefined;
311
- const explanations = new Set([...declarations].map(explainDeclaration));
312
- if (explanations.has(undefined)) return undefined;
313
- const choices = [...declarations].every(
314
- (declaration) => literalValues(declaration) !== undefined,
315
- );
316
- return choices || explanations.size === 1 ? [...explanations].join(" ") : undefined;
161
+ if (parts.length === 0) return explainDeclaration(schema);
162
+ const [part, ...rest] = parts;
163
+ if (IsObject(schema))
164
+ return Object.hasOwn(schema.properties, part)
165
+ ? fieldExplanation(schema.properties[part], rest)
166
+ : undefined;
167
+ if (!IsArray(schema)) return undefined;
168
+ const item = fieldExplanation(schema.items, rest);
169
+ return item ?? (rest.length === 0 ? explainDeclaration(schema) : undefined);
317
170
  }
318
171
 
319
- /** Why a field is not allowed: the fields the selected shape declares, after the meaning of the tag that chose it. */
320
- function shapeExplanation({ object, tag }: SelectedShape): string {
321
- const fields = `Accepted fields: ${Object.keys(object.properties).join(", ")}.`;
322
- const choice = tag === undefined ? undefined : explainDeclaration(object.properties[tag]);
323
- return choice === undefined ? fields : `${choice} ${fields}`;
172
+ /** Why a field is not allowed: the fields its object declares. */
173
+ function shapeExplanation(object: TObject | undefined): string | undefined {
174
+ return object && `Accepted fields: ${Object.keys(object.properties).join(", ")}.`;
324
175
  }
325
176
 
326
177
  /** One issue per field: distinct reasons joined, and distinct fixes joined, each in report order. */
@@ -342,30 +193,19 @@ function combineIssues(issues: readonly ArgumentIssue[]): readonly ArgumentIssue
342
193
  /**
343
194
  * One issue per native error. Reasons that do not say how to correct the value
344
195
  * carry a fix derived from the declared schema: a missing field and a pattern
345
- * mismatch explain the field, and a field that is not allowed explains its
346
- * selected shape.
196
+ * mismatch explain the field, and a field that is not allowed lists its
197
+ * object's declared fields.
347
198
  */
348
199
  function aggregateErrors(
349
200
  errors: readonly TLocalizedValidationError[],
350
201
  parameters: TObject,
351
202
  prepared: unknown,
352
- selected: ReadonlyMap<string, SelectedShape>,
203
+ objects: ReadonlyMap<string, TObject>,
353
204
  ): readonly ArgumentIssue[] {
354
205
  const issues: ArgumentIssue[] = [];
355
206
  const add = (field: string, reason: string, fix?: string) =>
356
207
  issues.push(fix === undefined ? { field, reason } : { field, reason, fix });
357
208
  for (const error of errors) {
358
- if (
359
- error.keyword === "anyOf" &&
360
- errors.some(
361
- (child) =>
362
- child !== error &&
363
- child.keyword !== "anyOf" &&
364
- (child.instancePath === error.instancePath ||
365
- child.instancePath.startsWith(`${error.instancePath}/`)),
366
- )
367
- )
368
- continue;
369
209
  switch (error.keyword) {
370
210
  case "required": {
371
211
  const parts = pointerParts(error.instancePath);
@@ -378,8 +218,8 @@ function aggregateErrors(
378
218
  break;
379
219
  }
380
220
  case "additionalProperties": {
381
- const shape = selected.get(valuePath(error.instancePath, prepared));
382
- const fix = shape && shapeExplanation(shape);
221
+ const { path } = valuePaths(error.instancePath, prepared);
222
+ const fix = shapeExplanation(objects.get(path));
383
223
  for (const key of error.params.additionalProperties)
384
224
  add(fieldPath(error.instancePath, prepared, key), "is not allowed", fix);
385
225
  break;
@@ -391,9 +231,16 @@ function aggregateErrors(
391
231
  fieldExplanation(parameters, pointerParts(error.instancePath)),
392
232
  );
393
233
  break;
394
- case "boolean":
395
- add(fieldPath(error.instancePath, prepared), "is not allowed");
234
+ case "boolean": {
235
+ // Once TypeBox's error capacity ends before `additionalProperties`, this is the only report.
236
+ const { parent } = valuePaths(error.instancePath, prepared);
237
+ add(
238
+ fieldPath(error.instancePath, prepared),
239
+ "is not allowed",
240
+ shapeExplanation(objects.get(parent)),
241
+ );
396
242
  break;
243
+ }
397
244
  case "enum":
398
245
  add(
399
246
  fieldPath(error.instancePath, prepared),
@@ -432,8 +279,7 @@ export function diagnoseArguments(
432
279
  const projection: Projection = {
433
280
  explained: new Set(semanticIssues.map((issue) => issue.field)),
434
281
  applicable: new Set<string>(),
435
- derived: [],
436
- selected: new Map(),
282
+ objects: new Map(),
437
283
  };
438
284
  const projected = projectSchema(parameters, prepared, "", projection);
439
285
  const retained = combineIssues(
@@ -468,9 +314,8 @@ export function diagnoseArguments(
468
314
  return {
469
315
  issues: combineIssues([
470
316
  ...retained,
471
- ...projection.derived,
472
317
  ...batches.flatMap((batch) =>
473
- aggregateErrors(batch.errors, parameters, prepared, projection.selected),
318
+ aggregateErrors(batch.errors, parameters, prepared, projection.objects),
474
319
  ),
475
320
  ]),
476
321
  limited: batches.some((batch) => batch.limited),
@@ -4,7 +4,8 @@
4
4
  * so it travels as the thrown message only. Oversized issues are omitted whole.
5
5
  */
6
6
  import type { OutputBudgets } from "./budgets.ts";
7
- import { boundedText, renderReport, type ReportText } from "./report.ts";
7
+ import { renderReport, type ReportText } from "./report.ts";
8
+ import { boundedText } from "./report-cause.ts";
8
9
  import type { ArgumentIssue, FailureReport } from "./report-schema.ts";
9
10
 
10
11
  type ArgumentRejection = ReportText<Extract<FailureReport, { error: "INVALID_ARGUMENTS" }>>;
@@ -6,7 +6,7 @@ import { makeGrepOverride } from "./grep-tool.ts";
6
6
  import { makeWriteOverride } from "./write-tool.ts";
7
7
  import { makeReplaceTool } from "./replace-tool.ts";
8
8
  import { DEFAULT_CONFIG } from "./config.ts";
9
- import { DEFAULT_OUTPUT_BUDGETS } from "./budgets.ts";
9
+ import { DEFAULT_OUTPUT_BUDGETS, outputBudgetsFor } from "./budgets.ts";
10
10
  import { callTool } from "./tool-call.testing.ts";
11
11
  import { argumentError } from "./argument-error.testing.ts";
12
12
 
@@ -16,7 +16,7 @@ test("all tools receive invalid arguments → return field diagnostics without a
16
16
  const cases = [
17
17
  {
18
18
  tool: makeEditOverride(cwd, DEFAULT_CONFIG),
19
- args: { path: "unused", edits: [{ op: "append", body: [] }] },
19
+ args: { path: "unused", edits: [{ action: "append", body: [] }] },
20
20
  },
21
21
  { tool: makeReadOverride(cwd, DEFAULT_CONFIG), args: { path: "unused", limit: 0 } },
22
22
  { tool: makeGrepOverride(cwd, DEFAULT_CONFIG), args: { pattern: "", literal: true } },
@@ -53,12 +53,12 @@ test("all tools receive invalid arguments → return field diagnostics without a
53
53
  test("Pi prepares a singleton operation → semantic and schema paths resolve to the prepared array", async () => {
54
54
  const tool = makeEditOverride(cwd, DEFAULT_CONFIG);
55
55
  await assert.rejects(
56
- callTool(tool, { path: "unused", edits: { op: "insert_after", body: [] } }),
56
+ callTool(tool, { path: "unused", edits: { action: "insert_after", body: [] } }),
57
57
  (error: unknown) => {
58
58
  const result = argumentError(error);
59
59
  assert.deepEqual(
60
60
  result.issues.map((issue) => issue.field),
61
- ["edits[0].body", "edits[0].anchor"],
61
+ ["edits[0].anchor", "edits[0].body"],
62
62
  );
63
63
  return true;
64
64
  },
@@ -78,25 +78,23 @@ test("Pi prepares a singleton operation → semantic and schema paths resolve to
78
78
  });
79
79
 
80
80
  test("JSON diagnostics exceed the budget → retain whole opening and closing issues and label omissions", async () => {
81
- const edits = Array.from({ length: DEFAULT_OUTPUT_BUDGETS.blockBytes }, () => ({
82
- op: "append",
81
+ const budget = outputBudgetsFor(4096);
82
+ const tool = makeEditOverride(cwd, DEFAULT_CONFIG, undefined, () => budget);
83
+ const maxItems: unknown = Reflect.get(tool.parameters.properties.edits, "maxItems");
84
+ assert.ok(typeof maxItems === "number");
85
+ const edits = Array.from({ length: maxItems }, () => ({
86
+ action: "append",
83
87
  body: [],
84
88
  }));
85
- await assert.rejects(
86
- callTool(makeEditOverride(cwd, DEFAULT_CONFIG), { path: "", edits }),
87
- (error: unknown) => {
88
- const result = argumentError(error);
89
- assert.ok(
90
- error instanceof Error &&
91
- Buffer.byteLength(error.message) <= DEFAULT_OUTPUT_BUDGETS.blockBytes,
92
- );
93
- assert.equal(result.issues[0].field, "edits[0].body");
94
- assert.equal(result.issues.at(-1)?.field, "path");
95
- assert.ok((result.omittedIssues ?? 0) > 0);
96
- assert.equal(result.issues.length + (result.omittedIssues ?? 0), edits.length + 1);
97
- return true;
98
- },
99
- );
89
+ await assert.rejects(callTool(tool, { path: "", edits }), (error: unknown) => {
90
+ const result = argumentError(error);
91
+ assert.ok(error instanceof Error && Buffer.byteLength(error.message) <= budget.blockBytes);
92
+ assert.equal(result.issues[0].field, "edits[0].body");
93
+ assert.equal(result.issues.at(-1)?.field, "path");
94
+ assert.ok((result.omittedIssues ?? 0) > 0);
95
+ assert.equal(result.issues.length + (result.omittedIssues ?? 0), edits.length + 1);
96
+ return true;
97
+ });
100
98
  });
101
99
 
102
100
  test("a reason contains an oversized anchor → keep its cause and recovery instruction inside valid JSON", async () => {
@@ -104,7 +102,7 @@ test("a reason contains an oversized anchor → keep its cause and recovery inst
104
102
  await assert.rejects(
105
103
  callTool(makeEditOverride(cwd, DEFAULT_CONFIG), {
106
104
  path: "unused",
107
- edits: [{ op: "delete", anchor }],
105
+ edits: [{ action: "delete", anchor }],
108
106
  }),
109
107
  (error: unknown) => {
110
108
  const result = argumentError(error);