@criogaid/pi-hashline-edit 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -40,7 +40,7 @@ The model copies an anchor into `edit`:
40
40
  ```json
41
41
  {
42
42
  "path": "greet.ts",
43
- "edits": [{ "op": "replace", "anchor": "2#GFYR", "body": [" return \"hello, hashline\";"] }]
43
+ "edits": [{ "action": "replace", "anchor": "2#GFYR", "body": [" return \"hello, hashline\";"] }]
44
44
  }
45
45
  ```
46
46
 
@@ -64,13 +64,17 @@ Grep matches the LF view with native JavaScript regexes. Without `multiline`, ea
64
64
 
65
65
  Edit operations:
66
66
 
67
- | `op` | Effect |
67
+ | `action` | Effect |
68
68
  | --- | --- |
69
69
  | `replace` / `delete` | Replace or remove a line, or an inclusive range with `end`. |
70
70
  | `insert_before` / `insert_after` | Insert lines next to an anchor. |
71
71
  | `prepend` / `append` | Insert lines at the start or end of the file. |
72
72
  | `copy` / `move` | Copy or move a line range to before or after another anchor. |
73
73
 
74
+ One call takes at most [64 operations](src/pi/edit-tool.ts). Ranges in one call must not overlap, and each gap between lines takes at most one insertion, copy and move destinations included. An insertion at the start of a replaced or deleted range goes before it.
75
+
76
+ `edit` asks for constrained sampling (Pi's `constrainedSampling`, a strict JSON schema) where the provider supports it, so the model cannot exceed the operation limit or emit fields the schema does not declare. Each operation is then written with every field, `null` for those its `action` does not take.
77
+
74
78
  The model sees each tool's full parameter schema; the definitions live in the tool files under [`src/pi/`](src/pi/).
75
79
 
76
80
  Range operations verify the cited `anchor` and `end`, plus the destination for copy/move. Interior lines have no individual anchor check. Changes elsewhere before the call do not invalidate unchanged anchors; publication rechecks the bytes read while applying the call and rejects a detected change.
package/README.zh-CN.md CHANGED
@@ -40,7 +40,7 @@ greet.ts · 3 lines
40
40
  ```json
41
41
  {
42
42
  "path": "greet.ts",
43
- "edits": [{ "op": "replace", "anchor": "2#GFYR", "body": [" return \"hello, hashline\";"] }]
43
+ "edits": [{ "action": "replace", "anchor": "2#GFYR", "body": [" return \"hello, hashline\";"] }]
44
44
  }
45
45
  ```
46
46
 
@@ -64,13 +64,17 @@ Grep 用原生 JavaScript 正则匹配 LF 视图。不设 `multiline` 时逐物
64
64
 
65
65
  `edit` 的操作:
66
66
 
67
- | `op` | 效果 |
67
+ | `action` | 效果 |
68
68
  | --- | --- |
69
69
  | `replace` / `delete` | 替换或删除一行;配合 `end` 处理闭区间范围。 |
70
70
  | `insert_before` / `insert_after` | 在锚点前或后插入行。 |
71
71
  | `prepend` / `append` | 在文件开头或末尾插入行。 |
72
72
  | `copy` / `move` | 把一段行复制或移动到另一个锚点之前或之后。 |
73
73
 
74
+ 一次调用最多 [64 个操作](src/pi/edit-tool.ts)。一次调用中的范围不能重叠,相邻两行之间的每个位置最多一次插入,复制或移动的目标位置也算在内。位于替换或删除区间起点的插入放在该区间之前。
75
+
76
+ 在提供方支持时,`edit` 请求约束采样(Pi 的 `constrainedSampling`,即 strict JSON schema),模型因此无法超出操作数上限,也无法写出 schema 未声明的字段。这时每个操作都会写出全部字段,其 `action` 用不到的字段写 `null`。
77
+
74
78
  模型能看到每个工具完整的参数 schema,定义位于 [`src/pi/`](src/pi/) 下的各工具文件。
75
79
 
76
80
  范围操作核对给出的 `anchor`、`end`,以及复制或移动的目标锚点,不逐行验证区间内部的内容。调用前其他位置的变化不会使未改变的锚点失效;发布前会复核修改操作实际读取的字节,检测到变化时拒绝发布。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@criogaid/pi-hashline-edit",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
5
  "description": "Hashline-style file editing for pi — line-anchored edits verified by content hash, replacing oldText/newText matching",
6
6
  "homepage": "https://github.com/Criogaid/pi-hashline-edit#readme",
@@ -145,6 +145,7 @@ test("range starts inside a range or shares its start → reject overlapping edi
145
145
  );
146
146
  assert(!result.ok && result.failure.kind === "range");
147
147
  assert.equal(result.failure.code, "OVERLAPPING_EDITS");
148
+ assert.match(result.failure.message, /overlap/);
148
149
  }
149
150
  });
150
151
 
@@ -166,19 +167,41 @@ test("insertion at a replaced or deleted range start → insert before the range
166
167
  }
167
168
  });
168
169
 
169
- test("insertions sharing a gap → preserve array order", () => {
170
- const text = "a\nb\n";
171
- const edits: Edit[] = [
172
- { op: "insert_after", anchor: at(text, 1), body: ["x"] },
173
- { op: "insert_before", anchor: at(text, 2), body: ["y"] },
170
+ test("insertions sharing a gap → reject the batch", () => {
171
+ const text = "a\nb\nc\nd\n";
172
+ const cases: Edit[][] = [
173
+ [
174
+ { op: "insert_after", anchor: at(text, 2), body: ["x"] },
175
+ { op: "insert_before", anchor: at(text, 3), body: ["y"] },
176
+ ],
177
+ [
178
+ { op: "insert_before", anchor: at(text, 3), body: ["x"] },
179
+ { op: "insert_before", anchor: at(text, 3), body: ["y"] },
180
+ ],
181
+ [
182
+ { op: "copy", anchor: at(text, 1), after: at(text, 2) },
183
+ { op: "insert_before", anchor: at(text, 3), body: ["y"] },
184
+ ],
185
+ [
186
+ { op: "move", anchor: at(text, 1), after: at(text, 2) },
187
+ { op: "insert_before", anchor: at(text, 3), body: ["y"] },
188
+ ],
189
+ [
190
+ { op: "append", body: ["x"] },
191
+ { op: "append", body: ["y"] },
192
+ ],
193
+ [
194
+ { op: "prepend", body: ["x"] },
195
+ { op: "insert_before", anchor: at(text, 1), body: ["y"] },
196
+ ],
174
197
  ];
175
- for (const [batch, expected] of [
176
- [edits, "a\nx\ny\nb\n"],
177
- [[...edits].reverse(), "a\ny\nx\nb\n"],
178
- ] satisfies [Edit[], string][]) {
179
- const result = applyEdits(text, batch, 4, 0);
180
- assert(result.ok);
181
- assert.equal(result.text, expected);
198
+ for (const edits of cases) {
199
+ for (const batch of [edits, [...edits].reverse()]) {
200
+ const result = applyEdits(text, batch, 4, 0);
201
+ assert(!result.ok && result.failure.kind === "range");
202
+ assert.equal(result.failure.code, "OVERLAPPING_EDITS");
203
+ assert.match(result.failure.message, /insert more than once/);
204
+ }
182
205
  }
183
206
  });
184
207
 
@@ -752,29 +775,6 @@ test("copy/move bad source and destination anchors → aggregate every supplied
752
775
  }
753
776
  });
754
777
 
755
- test("copy/move captures sharing a gap → preserve original captures in array order", () => {
756
- const text = "A\nB\nC\nD\nE\n";
757
- for (const [first, second, remaining] of [
758
- ["copy", "copy", text],
759
- ["copy", "move", "A\nC\nD\nE\n"],
760
- ["move", "copy", "B\nC\nD\nE\n"],
761
- ["move", "move", "C\nD\nE\n"],
762
- ] as const) {
763
- const edits: Edit[] = [
764
- { op: first, anchor: at(text, 1), after: at(text, 5) },
765
- { op: second, anchor: at(text, 2), after: at(text, 5) },
766
- ];
767
- for (const [batch, suffix] of [
768
- [edits, "A\nB\n"],
769
- [[...edits].reverse(), "B\nA\n"],
770
- ] satisfies [Edit[], string][]) {
771
- const result = applyEdits(text, batch, 4, 0);
772
- assert(result.ok);
773
- assert.equal(result.text, remaining + suffix);
774
- }
775
- }
776
- });
777
-
778
778
  test("move interior destination or reversed capture range → reject invalid ranges", () => {
779
779
  const text = "A\nB\nC\nD\nE\n";
780
780
  for (const edit of [
package/src/core/apply.ts CHANGED
@@ -21,8 +21,9 @@
21
21
  * deferred until anchors are corrected.
22
22
  *
23
23
  * Other strict semantics:
24
- * - Operation ranges must not overlap. Insertions at one position, or at the start of a
25
- * replaced or deleted range, apply in batch order before that range.
24
+ * - Operation ranges must not overlap, and each position takes at most one insertion,
25
+ * copy/move destinations included. An insertion at the start of a replaced or deleted
26
+ * range applies before that range.
26
27
  * - Byte-identical output is a success; the commit layer reports it as a no-op.
27
28
  *
28
29
  * @module pi-hashline-edit/core
@@ -302,19 +303,23 @@ export function applyEdits(
302
303
  return { ok: false, failure: { kind: "range", ...rangeError, checks: anchorChecks } };
303
304
  }
304
305
 
305
- // A stable sort keeps batch order among insertions at one position.
306
306
  const sorted = [...ops].sort((a, b) => a.lo - b.lo || a.hi - b.hi);
307
307
  const conflict = findSortedRangeConflict(
308
308
  sorted.map((op) => [op.lo, op.hi]),
309
309
  true,
310
310
  );
311
311
  if (conflict !== undefined) {
312
+ const { lo, hi } = sorted[conflict];
313
+ const previous = sorted[conflict - 1];
314
+ const repeatedInsertion = lo === hi && previous.lo === lo && previous.hi === hi;
312
315
  return {
313
316
  ok: false,
314
317
  failure: {
315
318
  kind: "range",
316
319
  code: "OVERLAPPING_EDITS",
317
- message: `Edits overlap near line ${sorted[conflict].lo + 1}.`,
320
+ message: repeatedInsertion
321
+ ? `Edits insert more than once at one position near line ${lo + 1}.`
322
+ : `Edits overlap near line ${lo + 1}.`,
318
323
  checks: anchorChecks,
319
324
  },
320
325
  };
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
2
2
  import { test } from "node:test";
3
3
  import { findSortedRangeConflict, mergeRanges, type HalfOpenRange } from "./ranges.ts";
4
4
 
5
- test("sorted ranges → ordered starts allow only empty ranges before a shared start", () => {
5
+ test("sorted ranges sharing or crossing boundaries → report conflicts according to insertion policy", () => {
6
6
  const cases: [HalfOpenRange[], number | undefined, number | undefined][] = [
7
7
  [[], undefined, undefined],
8
8
  [
@@ -27,7 +27,7 @@ test("sorted ranges → ordered starts allow only empty ranges before a shared s
27
27
  [0, 0],
28
28
  ],
29
29
  1,
30
- undefined,
30
+ 1,
31
31
  ],
32
32
  [
33
33
  [
@@ -44,7 +44,7 @@ test("sorted ranges → ordered starts allow only empty ranges before a shared s
44
44
  [0, 2],
45
45
  ],
46
46
  1,
47
- undefined,
47
+ 1,
48
48
  ],
49
49
  [
50
50
  [
@@ -70,6 +70,14 @@ test("sorted ranges → ordered starts allow only empty ranges before a shared s
70
70
  1,
71
71
  1,
72
72
  ],
73
+ [
74
+ [
75
+ [0, 2],
76
+ [0, 2],
77
+ ],
78
+ 1,
79
+ 1,
80
+ ],
73
81
  [
74
82
  [
75
83
  [0, 1],
@@ -80,17 +88,17 @@ test("sorted ranges → ordered starts allow only empty ranges before a shared s
80
88
  2,
81
89
  ],
82
90
  ];
83
- for (const [ranges, strictConflict, orderedConflict] of cases) {
84
- for (const orderedStarts of [false, true]) {
85
- const expected = orderedStarts ? orderedConflict : strictConflict;
91
+ for (const [ranges, strictConflict, insertionBeforeRangeConflict] of cases) {
92
+ for (const insertBeforeRange of [false, true]) {
93
+ const expected = insertBeforeRange ? insertionBeforeRangeConflict : strictConflict;
86
94
  for (const shift of [0, 10]) {
87
95
  assert.equal(
88
96
  findSortedRangeConflict(
89
97
  ranges.map(([start, end]) => [start + shift, end + shift]),
90
- orderedStarts,
98
+ insertBeforeRange,
91
99
  ),
92
100
  expected,
93
- JSON.stringify({ ranges, orderedStarts, shift }),
101
+ JSON.stringify({ ranges, insertBeforeRange, shift }),
94
102
  );
95
103
  }
96
104
  }
@@ -2,19 +2,23 @@
2
2
  export type HalfOpenRange = readonly [start: number, end: number];
3
3
 
4
4
  /**
5
- * Find the first conflicting pair's second index. Input must be sorted by start, then end,
6
- * with ties in caller order. A range starting inside an earlier non-empty range always
7
- * conflicts. An insertion (empty range) at a shared start conflicts unless `orderedStarts`
8
- * is set; then it applies in input order before the range that starts there.
5
+ * Find the first conflicting pair's second index. Input must be sorted by start, then end.
6
+ * A range starting inside an earlier non-empty range conflicts, and so do two insertions
7
+ * (empty ranges) at one position. An insertion at the start of a non-empty range also
8
+ * conflicts unless `insertBeforeRange` is set; then it applies before that range.
9
9
  */
10
10
  export function findSortedRangeConflict(
11
11
  ranges: readonly HalfOpenRange[],
12
- orderedStarts = false,
12
+ insertBeforeRange = false,
13
13
  ): number | undefined {
14
14
  for (let i = 1; i < ranges.length; i++) {
15
15
  const previous = ranges[i - 1];
16
16
  const current = ranges[i];
17
- if (current[0] < previous[1] || (!orderedStarts && current[0] === previous[0])) return i;
17
+ if (
18
+ current[0] < previous[1] ||
19
+ (current[0] === previous[0] && (!insertBeforeRange || current[1] === previous[1]))
20
+ )
21
+ return i;
18
22
  }
19
23
  return undefined;
20
24
  }
package/src/core/types.ts CHANGED
@@ -101,7 +101,7 @@ export type ApplyFailure =
101
101
  }
102
102
  | {
103
103
  readonly kind: "range";
104
- /** INVALID_RANGE for a reversed range or a move into itself; OVERLAPPING_EDITS for overlap. */
104
+ /** INVALID_RANGE for a reversed range or a move into itself; OVERLAPPING_EDITS for overlap or a repeated insertion position. */
105
105
  readonly code: "INVALID_RANGE" | "OVERLAPPING_EDITS";
106
106
  readonly message: string;
107
107
  readonly checks: readonly AnchorCheck[];
@@ -75,7 +75,7 @@ test("registered tools retain their configuration when another tool set is regis
75
75
  path,
76
76
  edits: [
77
77
  {
78
- op: "replace",
78
+ action: "replace",
79
79
  anchor: `1#${computeLineHash(1, "before", hashLen)}`,
80
80
  body: ["after"],
81
81
  },
@@ -104,7 +104,7 @@ test("registered tools retain their configuration when another tool set is regis
104
104
  await expectToolFailure(
105
105
  callTool(tool, {
106
106
  path,
107
- edits: [{ op: "delete", anchor: `1#${computeLineHash(1, "target", hashLen)}` }],
107
+ edits: [{ action: "delete", anchor: `1#${computeLineHash(1, "target", hashLen)}` }],
108
108
  }),
109
109
  "ANCHOR_MISMATCH",
110
110
  (report) => {
@@ -608,7 +608,7 @@ test("directory traversal skips symlinks but explicitly named linked files remai
608
608
  await invoke(
609
609
  edit,
610
610
  "0",
611
- { path: resultPath, edits: [{ op: "replace", anchor, body: ["updated"] }] },
611
+ { path: resultPath, edits: [{ action: "replace", anchor, body: ["updated"] }] },
612
612
  undefined,
613
613
  undefined,
614
614
  );
@@ -749,7 +749,7 @@ test("multiline grep anchors CRLF spans and counts overlapping physical lines on
749
749
  await invoke(
750
750
  edit,
751
751
  "0",
752
- { path: file, edits: [{ op: "replace", anchor, body: ["updated"] }] },
752
+ { path: file, edits: [{ action: "replace", anchor, body: ["updated"] }] },
753
753
  undefined,
754
754
  undefined,
755
755
  );
@@ -974,7 +974,7 @@ test("tools share physical lines and anchors across text representations", async
974
974
  const anchor = rows(grep)[1].split("│")[0];
975
975
  const edit = await call(makeEditOverride(directory, DEFAULT_CONFIG), {
976
976
  path: file,
977
- edits: [{ op: "replace", anchor, body: ["new"] }],
977
+ edits: [{ action: "replace", anchor, body: ["new"] }],
978
978
  });
979
979
  assert.equal(await readFile(file, "utf8"), before.replace("old", "new"));
980
980
  assert.match(edit.details.diff, /^-2 old/m);
@@ -1105,7 +1105,7 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1105
1105
  path,
1106
1106
  edits: [
1107
1107
  {
1108
- op: "replace",
1108
+ action: "replace",
1109
1109
  anchor: rows[1].split("│")[0],
1110
1110
  end: rows[2].split("│")[0],
1111
1111
  body: ["A", "B", "C"],
@@ -1386,7 +1386,7 @@ test("grep paths with input shorthand or literal separators still edit the searc
1386
1386
  assert.ok(anchor);
1387
1387
  await invoke(makeEditOverride(directory, DEFAULT_CONFIG), "edit", {
1388
1388
  path: returnedPath,
1389
- edits: [{ op: "replace", anchor, body: ["updated"] }],
1389
+ edits: [{ action: "replace", anchor, body: ["updated"] }],
1390
1390
  });
1391
1391
  assert.equal(await readFile(file, "utf8"), "updated\n");
1392
1392
  assert.equal(await readFile(otherFile, "utf8"), "needle\n");
@@ -103,7 +103,7 @@ test("copy/move a 150,000-line block → publish every captured line", async (t)
103
103
  ]);
104
104
  const result: Awaited<ReturnType<typeof edit.execute>> = await callTool(edit, {
105
105
  path,
106
- edits: [{ op, anchor, end, after }],
106
+ edits: [{ action: op, anchor, end, after }],
107
107
  });
108
108
  const expected = Buffer.from(
109
109
  (op === "copy" ? source : "\uFEFFSTART\nEND") + "\r\n" + block.slice(0, -2),
@@ -108,9 +108,18 @@ test("model switches before argument preparation → rejection uses the new budg
108
108
  const tool = session.getToolDefinition("edit");
109
109
  const prepare = tool?.prepareArguments;
110
110
  assert.ok(prepare);
111
+ const properties: unknown = Reflect.get(tool.parameters, "properties");
112
+ assert.ok(properties && typeof properties === "object");
113
+ const edits: unknown = Reflect.get(properties, "edits");
114
+ assert.ok(edits && typeof edits === "object");
115
+ const maxEdits: unknown = Reflect.get(edits, "maxItems");
116
+ assert.ok(typeof maxEdits === "number");
111
117
  const args = {
112
118
  path: "never-opened.txt",
113
- edits: Array.from({ length: 400 }, () => ({ op: "append", body: [] })),
119
+ edits: Array.from({ length: maxEdits }, () => ({
120
+ action: "append",
121
+ body: Array(4).fill("\ud800"),
122
+ })),
114
123
  };
115
124
  const reports: Report[] = [];
116
125
  for (const [id, contextWindow] of [
@@ -126,7 +135,7 @@ test("model switches before argument preparation → rejection uses the new budg
126
135
  assert.ok(Buffer.byteLength(error.message) <= budget.blockBytes);
127
136
  const report = decodeReport(error.message, "edit");
128
137
  assert.ok(report?.issues);
129
- assert.equal(report.issues.length + (report.omittedIssues ?? 0), args.edits.length);
138
+ assert.equal(report.issues.length + (report.omittedIssues ?? 0), args.edits.length * 4);
130
139
  reports.push(report);
131
140
  return true;
132
141
  },
@@ -12,7 +12,7 @@ assert.ok(directory);
12
12
  const cases = [
13
13
  {
14
14
  tool: makeEditOverride(directory, DEFAULT_CONFIG),
15
- args: { edits: [{ op: "append", body: ["after"] }] },
15
+ args: { edits: [{ action: "append", body: ["after"] }] },
16
16
  },
17
17
  {
18
18
  tool: makeReplaceTool(directory, DEFAULT_CONFIG),
@@ -64,7 +64,7 @@ test("packed extension completes file, worker, and command operations", {
64
64
  assert.equal(anchor("grep"), anchor("read"));
65
65
  return call("edit", "edit", {
66
66
  path: "fixture.txt",
67
- edits: [{ op: "replace", anchor: anchor("grep"), body: ["edited"] }],
67
+ edits: [{ action: "replace", anchor: anchor("grep"), body: ["edited"] }],
68
68
  });
69
69
  },
70
70
  () =>
@@ -91,7 +91,7 @@ const mutationCases = [
91
91
  name: "edit",
92
92
  make: (cwd: string, fusion?: ReturnType<typeof createActionFusionExecutor>) =>
93
93
  makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
94
- args: { edits: [{ op: "append", body: ["after"] }] },
94
+ args: { edits: [{ action: "append", body: ["after"] }] },
95
95
  },
96
96
  {
97
97
  name: "replace",
@@ -357,7 +357,7 @@ test("concurrent change during editable snapshot read → skips Fusion with NOT_
357
357
  makeEditOverride(directory, DEFAULT_CONFIG, fusion),
358
358
  {
359
359
  path,
360
- edits: [{ op: "append", body: ["after"] }],
360
+ edits: [{ action: "append", body: ["after"] }],
361
361
  then_run: { command: "check" },
362
362
  },
363
363
  { ctx },
@@ -11,7 +11,7 @@ import { makeEditOverride } from "../pi/edit-tool.ts";
11
11
  import { reportOf } from "../pi/report.ts";
12
12
  import { openTestSession, responseStream } from "../testing/session.testing.ts";
13
13
 
14
- const args = { path: "target.txt", edits: [{ op: "append", body: [] }] };
14
+ const args = { path: "target.txt", edits: [{ action: "append", body: [] }] };
15
15
 
16
16
  test("live preparation failure → replays diagnostic after fresh registration", async (t) => {
17
17
  initTheme("dark");
@@ -32,7 +32,7 @@ test("valid UTF-8 exceeding the runtime string limit → mutations retain the si
32
32
  await t.test(tool.name, async () => {
33
33
  const args =
34
34
  tool.name === "edit"
35
- ? { path, edits: [{ op: "append", body: ["tail"] }] }
35
+ ? { path, edits: [{ action: "append", body: ["tail"] }] }
36
36
  : { path, replacements: [{ literal: true, find: "中文", replace: "汉字" }] };
37
37
  await expectToolFailure(callTool(tool, args), "FILESYSTEM_ERROR", (report) => {
38
38
  assert.doesNotMatch(JSON.stringify(report), /UNSUPPORTED_ENCODING/);
package/src/pi/AGENTS.md CHANGED
@@ -8,9 +8,9 @@ Pi integration. Cross-cutting contracts live in the root `AGENTS.md`; this file
8
8
 
9
9
  ## Argument funnel
10
10
 
11
- - Tool files (`read-tool.ts`, `grep-tool.ts`, `edit-tool.ts`, `replace-tool.ts`, `write-tool.ts`) define TypeBox schemas and each mutation tool's description names its alternative (edit ↔ replace ↔ write). Each tool's `prepareArguments` comes from `argument-validation.ts`, then Pi validates against the schema again, and only then calls `execute`. Caller arguments are never rewritten. Each tool supplies only the semantic checks the schema cannot express or name precisely: edit rejects empty bodies, body lines that cannot be written as UTF-8, line numbers beyond the safe-integer range, and anchors of another hash length; replace rejects unwritable replacement text; replace and grep share `checkRegexArguments`, which reports flags V8 rejects and compiles regex-mode patterns; write rejects unwritable content. File-dependent checks stay in execution. Shared integer rules live in `schema.ts`.
11
+ - Tool files (`read-tool.ts`, `grep-tool.ts`, `edit-tool.ts`, `replace-tool.ts`, `write-tool.ts`) define TypeBox schemas and each mutation tool's description names its alternative (edit ↔ replace ↔ write). Each tool's `prepareArguments` comes from `argument-validation.ts`, then Pi validates against the schema again, and only then calls `execute`. Caller arguments are never rewritten. Each tool supplies only the semantic checks the schema cannot express or name precisely: edit rejects fields an operation's `action` does not take or requires but lacks, empty bodies, body lines that cannot be written as UTF-8, line numbers beyond the safe-integer range, and anchors of another hash length; replace rejects unwritable replacement text; replace and grep share `checkRegexArguments`, which reports flags V8 rejects and compiles regex-mode patterns; write rejects unwritable content. File-dependent checks stay in execution. Shared integer rules live in `schema.ts`. A tool that declares `constrainedSampling` (edit) keeps its parameters convertible by Pi's strict converter: no object or array unions, so each edit item is one flat object whose `EDIT_OPS` table, not the schema, fixes the fields per action. Constrained sampling and some gateways emit keys alphabetically, so the item's discriminator keeps the alphabetically first key.
12
12
  - `argument-validation.ts` owns every tool's `prepareArguments` hook, issue collection, bounded diagnostic output, and the Pi schema validation adapter. Tools report semantic field issues through its callback and do not throw during collection. Successful schema conversion informs those checks without changing caller arguments. Observe prepared failures without reimplementing coercion or optional-null rules.
13
- - `argument-diagnostics.ts` owns failure-only schema projection and structured aggregation. Top-level tool fields have independent schemas; failure diagnosis derives field schemas with TypeBox and keeps Pi's coercion and per-field error limits. Derive branches, choices, and the explanations of missing, forbidden, and pattern-mismatched fields from the declared schema: when a schema reason does not say how to correct the value, a missing or pattern-mismatched field's `fix` is its description (each choice's value and description), and a forbidden field's `fix` lists the selected shape's accepted fields after the meaning of the operation that selected it. Suppress only inapplicable or already explained issues, and retain independent fields with bounded native diagnostics.
13
+ - `argument-diagnostics.ts` owns failure-only schema projection and structured aggregation. Top-level tool fields have independent schemas; failure diagnosis derives field schemas with TypeBox and keeps Pi's coercion and per-field error limits. Derive choices and the explanations of missing, undeclared, and pattern-mismatched fields from the declared schema: when a schema reason does not say how to correct the value, a missing or pattern-mismatched field's `fix` is its description (each choice's value and description), and an undeclared field's `fix` lists its object's accepted fields. A declared field is applicable even when absent, so a tool check may require it. An issue on a whole object leaves its fields' diagnostics in place. Suppress only inapplicable or already explained issues, and retain independent fields with bounded native diagnostics.
14
14
  - `argument-error.ts` owns the JSON argument-error envelope and its byte budget: retain exact field paths, omit whole issues with explicit counts, and never truncate serialized JSON or repeat the call's arguments. Preparation failures use the same envelope.
15
15
 
16
16
  ## Mutations
@@ -61,7 +61,7 @@ test("then_run schemas reject invalid commands, unknown keys, and excessive time
61
61
  await writeFile(join(dir, path), "old\n");
62
62
  const mutation =
63
63
  tool.name === "edit"
64
- ? { edits: [{ op: "append", body: ["new"] }] }
64
+ ? { edits: [{ action: "append", body: ["new"] }] }
65
65
  : tool.name === "replace"
66
66
  ? { replacements: [{ literal: true, find: "old", replace: "new" }] }
67
67
  : { content: "new\n", mode: "overwrite" };
@@ -101,7 +101,7 @@ test("edit and replace share one embedded executor and preserve mutation results
101
101
  "edit-1",
102
102
  {
103
103
  path: "edit.txt",
104
- edits: [{ op: "append", body: ["after"] }],
104
+ edits: [{ action: "append", body: ["after"] }],
105
105
  then_run: { command: "check edit" },
106
106
  },
107
107
  undefined,
@@ -300,7 +300,7 @@ test("all mutation tools forward command progress before completion in RPC mode"
300
300
  "edit",
301
301
  {
302
302
  path: "progress.txt",
303
- edits: [{ op: "append", body: ["after"] }],
303
+ edits: [{ action: "append", body: ["after"] }],
304
304
  then_run: { command: "check" },
305
305
  },
306
306
  undefined,
@@ -376,7 +376,7 @@ test("progress reports skipped mutations and failed commands without rolling bac
376
376
  "skip",
377
377
  {
378
378
  path: "missing.txt",
379
- edits: [{ op: "append", body: ["after"] }],
379
+ edits: [{ action: "append", body: ["after"] }],
380
380
  then_run: { command: "check" },
381
381
  },
382
382
  undefined,