@criogaid/pi-hashline-edit 0.7.0 → 0.8.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 +74 -222
  2. package/README.zh-CN.md +162 -0
  3. package/package.json +2 -1
  4. package/src/core/AGENTS.md +1 -1
  5. package/src/core/apply.test.ts +172 -134
  6. package/src/core/apply.ts +28 -30
  7. package/src/core/case-fold.test.ts +11 -7
  8. package/src/core/case-fold.ts +2 -6
  9. package/src/core/character-set.ts +14 -0
  10. package/src/core/hash.test.ts +1 -25
  11. package/src/core/hash.ts +0 -5
  12. package/src/core/lines.ts +25 -7
  13. package/src/core/ranges.test.ts +41 -9
  14. package/src/core/ranges.ts +11 -4
  15. package/src/core/regex.ts +36 -6
  16. package/src/core/replace-schema.ts +18 -11
  17. package/src/core/replace.test.ts +90 -0
  18. package/src/core/replace.ts +6 -5
  19. package/src/core/search-analysis.test.ts +9 -0
  20. package/src/core/search-analysis.ts +35 -31
  21. package/src/core/search.ts +1 -1
  22. package/src/core/types.ts +5 -7
  23. package/src/index.ts +2 -4
  24. package/src/integration/config-registration.test.ts +16 -8
  25. package/src/integration/fusion-dispatch.test.ts +41 -22
  26. package/src/integration/grep-flags.test.ts +6 -13
  27. package/src/integration/grep-js-completeness.test.ts +41 -31
  28. package/src/integration/grep-order.test.ts +78 -0
  29. package/src/integration/grep-path-matrix.test.ts +4 -3
  30. package/src/integration/grep-regex.test.ts +13 -16
  31. package/src/integration/grep-rg.test.ts +127 -87
  32. package/src/integration/grep-scan-budget.test.ts +27 -5
  33. package/src/integration/large-text.test.ts +3 -4
  34. package/src/integration/model-budget-lifecycle.test.ts +15 -6
  35. package/src/integration/mutation-file-types.test.ts +0 -1
  36. package/src/integration/mutation-file-types.testing.ts +1 -1
  37. package/src/integration/npm-package.testing.ts +6 -32
  38. package/src/integration/publication-read-changes.test.ts +58 -26
  39. package/src/integration/replace-package.testing.ts +4 -1
  40. package/src/integration/replace-worker-boundary.test.ts +34 -24
  41. package/src/integration/report-replay.test.ts +2 -2
  42. package/src/integration/utf8-errors.test.ts +2 -2
  43. package/src/pi/AGENTS.md +12 -13
  44. package/src/pi/action-fusion.test.ts +71 -216
  45. package/src/pi/action-fusion.ts +37 -74
  46. package/src/pi/anchor-format.test.ts +16 -0
  47. package/src/pi/anchor-format.ts +7 -3
  48. package/src/pi/argument-diagnostics.ts +1 -1
  49. package/src/pi/argument-error.testing.ts +7 -4
  50. package/src/pi/argument-error.ts +5 -7
  51. package/src/pi/argument-format.test.ts +5 -16
  52. package/src/pi/argument-validation.test.ts +59 -25
  53. package/src/pi/argument-validation.ts +4 -5
  54. package/src/pi/budgets.ts +5 -7
  55. package/src/pi/config.test.ts +15 -5
  56. package/src/pi/config.ts +6 -6
  57. package/src/pi/diagnostic-buffer.test.ts +2 -3
  58. package/src/pi/diagnostic-buffer.ts +2 -2
  59. package/src/pi/edit-tool.ts +9 -12
  60. package/src/pi/execute.test.ts +80 -149
  61. package/src/pi/failure-context.test.ts +61 -16
  62. package/src/pi/failure-context.ts +12 -9
  63. package/src/pi/failure-path.test.ts +115 -50
  64. package/src/pi/file-commit.test.ts +30 -46
  65. package/src/pi/file-commit.ts +11 -40
  66. package/src/pi/file-revision.ts +25 -0
  67. package/src/pi/fusion-card-interaction.test.ts +143 -0
  68. package/src/pi/fusion-card.test.ts +193 -37
  69. package/src/pi/fusion-card.ts +186 -116
  70. package/src/pi/grep-candidates.ts +25 -27
  71. package/src/pi/grep-matrix.test.ts +1 -1
  72. package/src/pi/grep-order.test.ts +323 -0
  73. package/src/pi/grep-output.testing.ts +24 -0
  74. package/src/pi/grep-output.ts +62 -69
  75. package/src/pi/grep-render.ts +4 -1
  76. package/src/pi/grep-scope.ts +1 -1
  77. package/src/pi/grep-search.ts +85 -56
  78. package/src/pi/grep-tool.ts +17 -48
  79. package/src/pi/grep.test.ts +270 -169
  80. package/src/pi/mutation-outcome.testing.ts +2 -3
  81. package/src/pi/mutation-result.test.ts +26 -19
  82. package/src/pi/mutation-result.ts +25 -40
  83. package/src/pi/mutation-runner.ts +9 -17
  84. package/src/pi/output-budgets.test.ts +25 -16
  85. package/src/pi/path.ts +3 -1
  86. package/src/pi/pi.test.ts +4 -5
  87. package/src/pi/read-tool.ts +32 -80
  88. package/src/pi/read.test.ts +145 -92
  89. package/src/pi/regex-worker-contract.ts +5 -23
  90. package/src/pi/regex-worker.ts +4 -10
  91. package/src/pi/registration.test.ts +54 -49
  92. package/src/pi/render.test.ts +196 -93
  93. package/src/pi/render.ts +32 -22
  94. package/src/pi/replace-regex.ts +3 -5
  95. package/src/pi/replace-tool.ts +27 -26
  96. package/src/pi/replace-worker-contract.ts +3 -12
  97. package/src/pi/replace.test.ts +144 -226
  98. package/src/pi/report-cause.ts +23 -19
  99. package/src/pi/report-failure.testing.ts +2 -2
  100. package/src/pi/report-schema.ts +55 -43
  101. package/src/pi/report.test.ts +138 -71
  102. package/src/pi/report.ts +40 -43
  103. package/src/pi/result-safety.test.ts +157 -117
  104. package/src/pi/rg-process.test.ts +36 -10
  105. package/src/pi/rg-process.ts +9 -3
  106. package/src/pi/rg-regex.test.ts +17 -0
  107. package/src/pi/rg-regex.ts +28 -22
  108. package/src/pi/text-stream.test.ts +35 -36
  109. package/src/pi/text-stream.ts +14 -14
  110. package/src/pi/tool-call.testing.ts +17 -7
  111. package/src/pi/write-tool.ts +9 -11
  112. package/src/pi/write.test.ts +43 -75
  113. package/src/testing/regex-translation-corpus.ts +16 -0
  114. package/src/testing/rg-line-listing.fixture.mjs +1 -1
  115. package/src/integration/forget-grep.test.ts +0 -148
  116. package/src/pi/forget-tool.ts +0 -367
  117. package/src/pi/forget.test.ts +0 -630
  118. package/src/pi/forget.testing.ts +0 -201
@@ -30,100 +30,102 @@ const stubTheme = { fg: (_k: string, s: string) => s, bold: (s: string) => s } a
30
30
  // singleton — initialize it once for this test process (watcher off by default).
31
31
  initTheme();
32
32
 
33
- test("replace schema requires a non-empty replacements array", async () => {
34
- for (const fusion of [undefined, createActionFusionExecutor()]) {
35
- const tool = makeReplaceTool(process.cwd(), DEFAULT_CONFIG, fusion);
36
- assert.equal(Object.hasOwn(tool.parameters.properties, "find"), false);
37
- const check = (args: Parameters<typeof validateToolArguments>[1]["arguments"]) =>
38
- validateToolArguments(tool, { type: "toolCall", id: "0", name: "replace", arguments: args });
39
- assert.doesNotThrow(() =>
40
- check({ path: "f.txt", replacements: [{ find: "old", replace: "new" }] }),
41
- );
42
- const invalidArgs: Parameters<typeof validateToolArguments>[1]["arguments"][] = [
43
- { path: "f.txt", find: "old", replace: "new" },
44
- { path: "f.txt", replacements: [] },
45
- { path: "f.txt", replacements: [{ find: "old", replace: "new" }], find: "old" },
46
- { path: "f.txt", replacements: '[{"find":"old","replace":"new"}]' },
47
- ...[1, null, false].map((maxMatches) => ({
48
- path: "f.txt",
49
- replacements: [{ find: "old", replace: "new", maxMatches }],
50
- })),
51
- ];
52
- for (const args of invalidArgs) {
53
- await assert.rejects(call(tool, args), rejectsArguments("replace"));
33
+ test("replace rejects invalid rule shapes → precise fields and no publication or commands", async () =>
34
+ withDir(async (dir) => {
35
+ const path = join(dir, "f.txt");
36
+ await writeFile(path, "old\n");
37
+ let commands = 0;
38
+ const fusion = createActionFusionExecutor(async () => {
39
+ commands++;
40
+ return { status: "succeeded", result: { content: [] } };
41
+ });
42
+ const rule = { literal: true, find: "old", replace: "new" };
43
+ for (const executor of [undefined, fusion]) {
44
+ const tool = makeReplaceTool(dir, DEFAULT_CONFIG, executor);
45
+ for (const [args, field, reason] of [
46
+ [{ find: "old", replace: "new" }, "replacements", /required/],
47
+ [{ replacements: [] }, "replacements", /./],
48
+ [{ replacements: [rule], find: "old" }, "find", /not allowed/],
49
+ [{ replacements: JSON.stringify([rule]) }, "replacements[0]", /object/],
50
+ ...[1, null, false].map(
51
+ (maxMatches) =>
52
+ [
53
+ { replacements: [{ ...rule, maxMatches }] },
54
+ "replacements[0].maxMatches",
55
+ /not allowed/,
56
+ ] as const,
57
+ ),
58
+ [{ path: "", replacements: [rule] }, "path", /./],
59
+ [{ replacements: [{ ...rule, find: "" }] }, "replacements[0].find", /./],
60
+ ] as const) {
61
+ await assert.rejects(
62
+ callTool(tool, {
63
+ path,
64
+ ...args,
65
+ ...(executor ? { then_run: { command: "check" } } : {}),
66
+ }),
67
+ rejectsArgument(field, reason),
68
+ );
69
+ assert.equal(await readFile(path, "utf8"), "old\n");
70
+ }
54
71
  }
55
- }
56
- });
57
-
58
- test("replace schema rejects empty paths, empty find, and unsupported flags", async () => {
59
- const tool = makeReplaceTool(process.cwd(), DEFAULT_CONFIG);
60
- for (const args of [
61
- { path: "", replacements: [{ find: "old", replace: "new" }] },
62
- { path: "f.txt", replacements: [{ find: "", replace: "new" }] },
63
- { path: "f.txt", replacements: [{ find: "old", replace: "new", flags: "x" }] },
64
- ]) {
65
- await assert.rejects(call(tool, args), rejectsArguments("replace"), JSON.stringify(args));
66
- }
67
- });
72
+ assert.equal(commands, 0);
73
+ }));
68
74
 
69
- test("replace rejects top-level rules without publishing", async () =>
75
+ test("replace rules omit literal or retain regex → reject before publication and fused commands", async () =>
70
76
  withDir(async (dir) => {
71
- const file = join(dir, "f.txt");
72
- await writeFile(file, "old\n");
73
- await assert.rejects(
74
- callTool(makeReplaceTool(dir, DEFAULT_CONFIG), {
75
- path: file,
76
- find: "old",
77
- replace: "new",
77
+ const path = join(dir, "f.txt");
78
+ await writeFile(path, "old\n");
79
+ let commands = 0;
80
+ const tool = makeReplaceTool(
81
+ dir,
82
+ DEFAULT_CONFIG,
83
+ createActionFusionExecutor(async () => {
84
+ commands++;
85
+ return { status: "succeeded", result: { content: [] } };
78
86
  }),
79
- rejectsArgument("replacements"),
80
87
  );
81
- assert.equal(await readFile(file, "utf8"), "old\n");
82
- }));
83
-
84
- test("replace rejects unsupported per-rule fields before publishing", async () =>
85
- withDir(async (dir) => {
86
- const file = join(dir, "f.txt");
87
- const before = "foo foo foo\n";
88
- await writeFile(file, before);
89
- const tool = makeReplaceTool(dir, DEFAULT_CONFIG);
90
- for (const maxMatches of [1, null, false]) {
88
+ for (const [rule, field, reason] of [
89
+ [{ find: "old", replace: "new" }, "literal", /required/],
90
+ [{ find: "old", replace: "new", literal: true, regex: true }, "regex", /not allowed/],
91
+ [{ find: "old", replace: "new", literal: true, regex: false }, "regex", /not allowed/],
92
+ ] as const) {
91
93
  await assert.rejects(
92
- callTool(tool, { path: file, replacements: [{ find: "foo", replace: "bar", maxMatches }] }),
93
- rejectsArgument("replacements[0].maxMatches", /not allowed/),
94
+ callTool(tool, {
95
+ path,
96
+ replacements: [{ find: "old", literal: true, replace: "first" }, rule],
97
+ then_run: { command: "check" },
98
+ }),
99
+ rejectsArgument(`replacements[1].${field}`, reason),
94
100
  );
95
- assert.equal(await readFile(file, "utf8"), before);
101
+ assert.equal(await readFile(path, "utf8"), "old\n");
96
102
  }
103
+ assert.equal(commands, 0);
97
104
  }));
98
105
 
99
106
  test("replace rejects unwritable text and invalid regex before reading a missing target", async () =>
100
107
  withDir(async (dir) => {
101
108
  const file = join(dir, "missing.txt");
102
109
  const tool = makeReplaceTool(dir, DEFAULT_CONFIG);
103
- const valid = { path: file, replacements: [{ find: "x", replace: "y", regex: true }] };
110
+ const valid = { path: file, replacements: [{ find: "x", replace: "y", literal: false }] };
104
111
  assert.equal(tool.prepareArguments(valid), valid);
105
112
  for (const [rule, expected] of [
106
- [{ find: "x", replace: "\0" }, rejectsArgument("replacements[0].replace", /NUL/)],
107
113
  [
108
- { find: "x", replace: "\ud800" },
114
+ { literal: true, find: "x", replace: "\0" },
115
+ rejectsArgument("replacements[0].replace", /NUL/),
116
+ ],
117
+ [
118
+ { literal: true, find: "x", replace: "\ud800" },
109
119
  rejectsArgument("replacements[0].replace", /losslessly.*UTF-8/),
110
120
  ],
111
121
  [
112
- { find: "(", replace: "x", regex: true },
122
+ { find: "(", replace: "x", literal: false },
113
123
  rejectsArgument("replacements[0].find", /Invalid regular expression/i),
114
124
  ],
115
125
  ] as const) {
116
126
  await assert.rejects(callTool(tool, { path: file, replacements: [rule] }), expected);
117
127
  await assert.rejects(readFile(file, "utf8"), { code: "ENOENT" });
118
128
  }
119
- await assert.rejects(
120
- callTool(tool, {
121
- path: file,
122
- replacements: [{ find: "(", replace: "x", regex: true, flags: "z" }],
123
- }),
124
- rejectsArgument("replacements[0].flags", /must match pattern/),
125
- );
126
- await assert.rejects(readFile(file, "utf8"), { code: "ENOENT" });
127
129
  }));
128
130
 
129
131
  test("replace literal: replaces all occurrences", async () => {
@@ -132,11 +134,11 @@ test("replace literal: replaces all occurrences", async () => {
132
134
  await writeFile(f, "foo bar foo baz foo\n");
133
135
  const r: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
134
136
  path: "f.txt",
135
- replacements: [{ find: "foo", replace: "qux" }],
137
+ replacements: [{ literal: true, find: "foo", replace: "qux" }],
136
138
  });
137
139
  assert.equal(r.isError, undefined);
138
140
  assert.equal(await readFile(f, "utf-8"), "qux bar qux baz qux\n");
139
- assert.equal(reportOf(r)?.matches, 3);
141
+ assert.equal(reportOf(r, "replace")?.matches, 3);
140
142
  });
141
143
  });
142
144
 
@@ -146,13 +148,13 @@ test("literal multiline replace matches LF and CRLF while retaining local separa
146
148
  await writeFile(file, "\uFEFFhead\r\nold\r\none\r\nmid\nold\none\nend\r\n");
147
149
  const result: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
148
150
  path: "mixed.txt",
149
- replacements: [{ find: "old\none", replace: "NEW\nX" }],
151
+ replacements: [{ literal: true, find: "old\none", replace: "NEW\nX" }],
150
152
  });
151
- assert.equal(reportOf(result)?.matches, 2);
153
+ assert.equal(reportOf(result, "replace")?.matches, 2);
152
154
  assert.equal(await readFile(file, "utf8"), "\uFEFFhead\r\nNEW\r\nX\r\nmid\nNEW\nX\nend\r\n");
153
155
  await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
154
156
  path: "mixed.txt",
155
- replacements: [{ find: "NEW\r\nX", replace: "done" }],
157
+ replacements: [{ literal: true, find: "NEW\r\nX", replace: "done" }],
156
158
  });
157
159
  assert.equal(await readFile(file, "utf8"), "\uFEFFhead\r\ndone\r\nmid\ndone\nend\r\n");
158
160
  }));
@@ -160,11 +162,11 @@ test("literal multiline replace matches LF and CRLF while retaining local separa
160
162
  test("both replacement modes preserve each matched separator", async () =>
161
163
  withDir(async (dir) => {
162
164
  const file = join(dir, "mixed.txt");
163
- for (const regex of [false, true]) {
165
+ for (const literal of [true, false]) {
164
166
  await writeFile(file, "a\r\nb\nc\r\ntail\n");
165
167
  await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
166
168
  path: "mixed.txt",
167
- replacements: [{ find: "a\nb\nc", replace: "x\ny\nz\nextra", regex }],
169
+ replacements: [{ find: "a\nb\nc", replace: "x\ny\nz\nextra", literal }],
168
170
  });
169
171
  assert.equal(await readFile(file, "utf8"), "x\r\ny\nz\nextra\r\ntail\n");
170
172
  }
@@ -178,11 +180,11 @@ test("literal replacement uses one LF view and preserves raw bytes across query
178
180
  await writeFile(file, before);
179
181
  const result = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
180
182
  path: file,
181
- replacements: [{ find, replace: find }],
183
+ replacements: [{ literal: true, find, replace: find }],
182
184
  });
183
185
  assert.equal(await readFile(file, "utf8"), before);
184
- assert.equal(reportOf(result)?.matches, 1);
185
- assert.equal(reportOf(result)?.publication, "NOT_PUBLISHED");
186
+ assert.equal(reportOf(result, "replace")?.matches, 1);
187
+ assert.equal(reportOf(result, "replace")?.publication, "NOT_PUBLISHED");
186
188
  }
187
189
  for (const [before, find, replacement, expected] of [
188
190
  ["a\r\nb", "a", "A\nextra", "A\r\nextra\r\nb"],
@@ -194,7 +196,7 @@ test("literal replacement uses one LF view and preserves raw bytes across query
194
196
  await writeFile(file, before);
195
197
  await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
196
198
  path: file,
197
- replacements: [{ find, replace: replacement }],
199
+ replacements: [{ literal: true, find, replace: replacement }],
198
200
  });
199
201
  assert.equal(await readFile(file, "utf8"), expected);
200
202
  }
@@ -207,8 +209,8 @@ test("mixed literal and regex batches share raw offsets after CRLF normalization
207
209
  await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
208
210
  path: file,
209
211
  replacements: [
210
- { find: "a\nb", replace: "A\nB" },
211
- { find: "(c)$", replace: "$1!", regex: true },
212
+ { literal: true, find: "a\nb", replace: "A\nB" },
213
+ { find: "(c)$", replace: "$1!", literal: false },
212
214
  ],
213
215
  });
214
216
  assert.equal(await readFile(file, "utf8"), "😀\r\nA\r\nB\r\nc!");
@@ -216,12 +218,12 @@ test("mixed literal and regex batches share raw offsets after CRLF normalization
216
218
  const failed = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
217
219
  path: file,
218
220
  replacements: [
219
- { find: "A\nB", replace: "x" },
220
- { find: "\\nB", replace: "y", regex: true },
221
+ { literal: true, find: "A\nB", replace: "x" },
222
+ { find: "\\nB", replace: "y", literal: false },
221
223
  ],
222
224
  });
223
225
  assert.equal(failed.isError, true);
224
- assert.equal(reportOf(failed)?.error, "OVERLAPPING_MATCHES");
226
+ assert.equal(reportOf(failed, "replace")?.error, "OVERLAPPING_MATCHES");
225
227
  assert.equal(await readFile(file, "utf8"), before);
226
228
  }));
227
229
 
@@ -231,11 +233,10 @@ test("replace literal: $ in replacement stays literal (no expansion)", async ()
231
233
  await writeFile(f, "cost: $5 here\n");
232
234
  const r: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
233
235
  path: "f.txt",
234
- replacements: [{ find: "$5", replace: "$10" }],
236
+ replacements: [{ literal: true, find: "$5", replace: "$1|$<name>|$&|$$" }],
235
237
  });
236
238
  assert.equal(r.isError, undefined);
237
- // literal mode: "$10" inserted verbatim, NOT interpreted as group-10 + "0"
238
- assert.equal(await readFile(f, "utf-8"), "cost: $10 here\n");
239
+ assert.equal(await readFile(f, "utf-8"), "cost: $1|$<name>|$&|$$ here\n");
239
240
  });
240
241
  });
241
242
 
@@ -245,7 +246,7 @@ test("replace literal: case-insensitive via flags", async () => {
245
246
  await writeFile(f, "Foo fOo FOO\n");
246
247
  await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
247
248
  path: "f.txt",
248
- replacements: [{ find: "foo", replace: "x", flags: "i" }],
249
+ replacements: [{ literal: true, find: "foo", replace: "x", flags: "i" }],
249
250
  });
250
251
  assert.equal(await readFile(f, "utf-8"), "x x x\n");
251
252
  });
@@ -262,12 +263,12 @@ test("replace aborts catastrophic regex without blocking the event loop or publi
262
263
  try {
263
264
  const failed = await callTool(
264
265
  makeReplaceTool(dir, DEFAULT_CONFIG),
265
- { path: file, replacements: [{ find: "^(a+)+$", replace: "x", regex: true }] },
266
+ { path: file, replacements: [{ find: "^(a+)+$", replace: "x", literal: false }] },
266
267
  { toolCallId: "abort", signal: controller.signal, ctx: { cwd: dir } },
267
268
  );
268
269
  assert.equal(failed.isError, true);
269
- assert.equal(reportOf(failed)?.error, "OPERATION_ABORTED");
270
- assert.equal(reportOf(failed)?.publication, "NOT_PUBLISHED");
270
+ assert.equal(reportOf(failed, "replace")?.error, "OPERATION_ABORTED");
271
+ assert.equal(reportOf(failed, "replace")?.publication, "NOT_PUBLISHED");
271
272
  assert.ok(performance.now() - start < 2000);
272
273
  assert.equal(await readFile(file, "utf8"), before);
273
274
  } finally {
@@ -283,12 +284,12 @@ test("replace times out catastrophic regex at the configured limit without publi
283
284
  const config = { ...DEFAULT_CONFIG, replace: { regexTimeoutMs: 1_000 } };
284
285
  const failed = await call(makeReplaceTool(dir, config), {
285
286
  path: file,
286
- replacements: [{ find: "^(a+)+$", replace: "x", regex: true }],
287
+ replacements: [{ find: "^(a+)+$", replace: "x", literal: false }],
287
288
  });
288
289
  assert.equal(failed.isError, true);
289
- assert.equal(reportOf(failed)?.error, "REGEX_TIMEOUT");
290
- assert.equal(reportOf(failed)?.timeoutMs, config.replace.regexTimeoutMs);
291
- assert.equal(reportOf(failed)?.publication, "NOT_PUBLISHED");
290
+ assert.equal(reportOf(failed, "replace")?.error, "REGEX_TIMEOUT");
291
+ assert.equal(reportOf(failed, "replace")?.timeoutMs, config.replace.regexTimeoutMs);
292
+ assert.equal(reportOf(failed, "replace")?.publication, "NOT_PUBLISHED");
292
293
  assert.equal(await readFile(file, "utf8"), before);
293
294
  });
294
295
  });
@@ -299,7 +300,7 @@ test("replace regex: capture groups in replacement", async () => {
299
300
  await writeFile(f, "name: alice\nname: bob\n");
300
301
  await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
301
302
  path: "f.txt",
302
- replacements: [{ find: "name: (\\w+)", replace: "user: $1", regex: true }],
303
+ replacements: [{ find: "name: (\\w+)", replace: "user: $1", literal: false }],
303
304
  });
304
305
  assert.equal(await readFile(f, "utf-8"), "user: alice\nuser: bob\n");
305
306
  });
@@ -312,7 +313,7 @@ test("replace regex: multiline flag matches line-anchored pattern", async () =>
312
313
  // without `m`, ^a$ wouldn't match (a isn't at end of string); with `m` it does
313
314
  await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
314
315
  path: "f.txt",
315
- replacements: [{ find: "^a$", replace: "A", regex: true, flags: "m" }],
316
+ replacements: [{ find: "^a$", replace: "A", literal: false, flags: "m" }],
316
317
  });
317
318
  assert.equal(await readFile(f, "utf-8"), "A\nb\nc\n");
318
319
  });
@@ -325,7 +326,7 @@ test("replace regex: dotall flag makes . match newlines", async () => {
325
326
  // a.b only matches across the newline with the `s` (dotall) flag
326
327
  await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
327
328
  path: "f.txt",
328
- replacements: [{ find: "a.b", replace: "X", regex: true, flags: "s" }],
329
+ replacements: [{ find: "a.b", replace: "X", literal: false, flags: "s" }],
329
330
  });
330
331
  assert.equal(await readFile(f, "utf-8"), "X\n");
331
332
  });
@@ -337,67 +338,15 @@ test("replace finds no matches → returns failure and leaves file untouched", a
337
338
  await writeFile(f, "a\nb\n");
338
339
  const failed = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
339
340
  path: "f.txt",
340
- replacements: [{ find: "zzz", replace: "y" }],
341
+ replacements: [{ literal: true, find: "zzz", replace: "y" }],
341
342
  });
342
343
  assert.equal(failed.isError, true);
343
- assert.equal(reportOf(failed)?.error, "NO_MATCH");
344
- assert.equal(reportOf(failed)?.rule, 0);
344
+ assert.equal(reportOf(failed, "replace")?.error, "NO_MATCH");
345
+ assert.equal(reportOf(failed, "replace")?.rule, 0);
345
346
  assert.equal(await readFile(f, "utf-8"), "a\nb\n", "file untouched on 0-match failure");
346
347
  });
347
348
  });
348
349
 
349
- test("replace: empty find throws", async () => {
350
- await withDir(async (dir) => {
351
- await writeFile(join(dir, "f.txt"), "a\n");
352
- await assert.rejects(
353
- callTool(makeReplaceTool(dir, DEFAULT_CONFIG), {
354
- path: "f.txt",
355
- replacements: [{ find: "", replace: "x" }],
356
- }),
357
- rejectsArgument("replacements[0].find"),
358
- );
359
- });
360
- });
361
-
362
- test("replace: handles large match counts without an arbitrary cap", async () => {
363
- await withDir(async (dir) => {
364
- const f = join(dir, "f.txt");
365
- await writeFile(f, "a\n".repeat(2500));
366
- const result: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
367
- path: "f.txt",
368
- replacements: [{ find: "a", replace: "b" }],
369
- });
370
- assert.equal(reportOf(result)?.matches, 2500);
371
- assert.equal(await readFile(f, "utf-8"), "b\n".repeat(2500));
372
- });
373
- });
374
-
375
- test("replace: invalid regex throws a friendly message", async () => {
376
- await withDir(async (dir) => {
377
- await writeFile(join(dir, "f.txt"), "a\n");
378
- await assert.rejects(
379
- call(makeReplaceTool(dir, DEFAULT_CONFIG), {
380
- path: "f.txt",
381
- replacements: [{ find: "(unclosed", replace: "x", regex: true }],
382
- }),
383
- /Invalid regular expression/i,
384
- );
385
- });
386
- });
387
-
388
- test("replace: invalid flag char throws", async () => {
389
- await withDir(async (dir) => {
390
- await writeFile(join(dir, "f.txt"), "a\n");
391
- await assert.rejects(
392
- callTool(makeReplaceTool(dir, DEFAULT_CONFIG), {
393
- path: "f.txt",
394
- replacements: [{ find: "a", replace: "x", flags: "z" }],
395
- }),
396
- rejectsArgument("replacements[0].flags"),
397
- );
398
- });
399
- });
400
-
401
350
  test("replace: count-but-no-net-change does not rewrite and reports it", async () => {
402
351
  await withDir(async (dir) => {
403
352
  const f = join(dir, "f.txt");
@@ -405,37 +354,23 @@ test("replace: count-but-no-net-change does not rewrite and reports it", async (
405
354
  // $& = whole match = "x", so the text is unchanged despite 2 matches
406
355
  const r: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
407
356
  path: "f.txt",
408
- replacements: [{ find: "x", replace: "$&", regex: true }],
357
+ replacements: [{ find: "x", replace: "$&", literal: false }],
409
358
  });
410
359
  assert.equal(r.isError, undefined);
411
360
  assert.equal(await readFile(f, "utf-8"), "xx\n");
412
- assert.equal(reportOf(r)?.publication, "NOT_PUBLISHED");
413
- assert.equal(reportOf(r)?.matches, 2);
361
+ assert.equal(reportOf(r, "replace")?.publication, "NOT_PUBLISHED");
362
+ assert.equal(reportOf(r, "replace")?.matches, 2);
414
363
  assert.equal(r.details.diff, "");
415
364
  });
416
365
  });
417
366
 
418
- test("replace: returns details.diff (string) + patch + firstChangedLine", async () => {
419
- await withDir(async (dir) => {
420
- const f = join(dir, "f.txt");
421
- await writeFile(f, "a\nb\nc\n");
422
- const r: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
423
- path: "f.txt",
424
- replacements: [{ find: "b", replace: "B" }],
425
- });
426
- assert.equal(typeof r.details.diff, "string");
427
- assert.equal(typeof r.details.patch, "string");
428
- assert.equal(typeof r.details.firstChangedLine, "number");
429
- });
430
- });
431
-
432
367
  test("multiline replacement → preserves the surrounding file text", async () => {
433
368
  await withDir(async (dir) => {
434
369
  const f = join(dir, "f.txt");
435
370
  await writeFile(f, "a\nb\nc\n");
436
371
  await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
437
372
  path: "f.txt",
438
- replacements: [{ find: "b", replace: "B1\nB2" }],
373
+ replacements: [{ literal: true, find: "b", replace: "B1\nB2" }],
439
374
  });
440
375
  assert.equal(await readFile(f, "utf-8"), "a\nB1\nB2\nc\n");
441
376
  });
@@ -446,8 +381,8 @@ test("replace header retains the rule count when a long path wraps", () => {
446
381
  const args = {
447
382
  path: "x".repeat(68),
448
383
  replacements: [
449
- { find: "first", replace: "second" },
450
- { find: "third", replace: "fourth" },
384
+ { literal: true, find: "first", replace: "second" },
385
+ { literal: true, find: "third", replace: "fourth" },
451
386
  ],
452
387
  };
453
388
  const header = tool.renderCall(args, stubTheme, {} as Parameters<typeof tool.renderCall>[2]);
@@ -462,15 +397,15 @@ test("replacement batches → evaluate every rule against one snapshot", async (
462
397
  const args = {
463
398
  path: file,
464
399
  replacements: [
465
- { find: "foo", replace: "bar" },
466
- { find: "bar", replace: "baz" },
400
+ { literal: true, find: "foo", replace: "bar" },
401
+ { literal: true, find: "bar", replace: "baz" },
467
402
  ],
468
403
  };
469
404
  validateToolArguments(tool, { type: "toolCall", id: "0", name: "replace", arguments: args });
470
405
  await writeFile(file, "\uFEFFfoo\r\nbar\nfoo");
471
406
  const result = await call(tool, args);
472
407
  assert.equal(await readFile(file, "utf8"), "\uFEFFbar\r\nbaz\nbar");
473
- assert.equal(reportOf(result)?.matches, 3);
408
+ assert.equal(reportOf(result, "replace")?.matches, 3);
474
409
  assert.match(result.details.diff, /^\+2 baz/m);
475
410
  }));
476
411
 
@@ -485,28 +420,28 @@ test("invalid batches reject every change and never run a fused command", async
485
420
  DEFAULT_CONFIG,
486
421
  createActionFusionExecutor(async () => {
487
422
  commands++;
488
- return { status: "succeeded", output: "done" };
423
+ return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
489
424
  }),
490
425
  );
491
- const first = { find: "bar", replace: "changed" };
426
+ const first = { literal: true, find: "bar", replace: "changed" };
492
427
  const cases = [
493
428
  [
494
429
  rejectsArgument("replacements[1].find", /Invalid regular expression/i),
495
- [{ replacements: [first, { find: "(", replace: "x", regex: true }] }],
430
+ [{ replacements: [first, { find: "(", replace: "x", literal: false }] }],
496
431
  ],
497
432
  [
498
433
  rejectsArgument("replacements[1].replace", /NUL/),
499
- [{ replacements: [first, { find: "foo", replace: "\0" }] }],
434
+ [{ replacements: [first, { literal: true, find: "foo", replace: "\0" }] }],
500
435
  ],
501
436
  [
502
437
  rejectsArgument("replacements[1].replace", /losslessly.*UTF-8/),
503
- [{ replacements: [first, { find: "foo", replace: "\ud800" }] }],
438
+ [{ replacements: [first, { literal: true, find: "foo", replace: "\ud800" }] }],
504
439
  ],
505
440
  [
506
- rejectsArguments("replace"),
441
+ rejectsArguments(),
507
442
  [
508
- { replacements: [first, { find: "", replace: "x" }] },
509
- { replacements: [first, { find: "foo" }] },
443
+ { replacements: [first, { literal: true, find: "", replace: "x" }] },
444
+ { replacements: [first, { literal: true, find: "foo" }] },
510
445
  { replacements: [] },
511
446
  { replacements: [first], find: "foo", replace: "x" },
512
447
  { replacements: [first], flags: "i" },
@@ -515,9 +450,9 @@ test("invalid batches reject every change and never run a fused command", async
515
450
  ],
516
451
  ] as const;
517
452
  for (const [rule, code] of [
518
- [{ find: "missing", replace: "x" }, "NO_MATCH"],
519
- [{ find: "bar foo", replace: "x" }, "OVERLAPPING_MATCHES"],
520
- [{ find: "bar", replace: "x" }, "OVERLAPPING_MATCHES"],
453
+ [{ literal: true, find: "missing", replace: "x" }, "NO_MATCH"],
454
+ [{ literal: true, find: "bar foo", replace: "x" }, "OVERLAPPING_MATCHES"],
455
+ [{ literal: true, find: "bar", replace: "x" }, "OVERLAPPING_MATCHES"],
521
456
  ] as const) {
522
457
  const failed = await callTool(
523
458
  tool,
@@ -525,9 +460,9 @@ test("invalid batches reject every change and never run a fused command", async
525
460
  { ctx: { cwd: dir } },
526
461
  );
527
462
  assert.equal(failed.isError, true);
528
- assert.equal(reportOf(failed)?.error, code);
529
- assert.equal(reportOf(failed)?.then_run?.status, "skipped");
530
- assert.equal(reportOf(failed)?.publication, "NOT_PUBLISHED");
463
+ assert.equal(reportOf(failed, "replace")?.error, code);
464
+ assert.equal(reportOf(failed, "replace")?.then_run?.status, "skipped");
465
+ assert.equal(reportOf(failed, "replace")?.publication, "NOT_PUBLISHED");
531
466
  assert.equal(await readFile(file, "utf8"), before);
532
467
  }
533
468
  for (const [expected, inputs] of cases) {
@@ -554,12 +489,12 @@ test("adjacent matches and zero-width boundaries have deterministic batch semant
554
489
  const tool = makeReplaceTool(dir, DEFAULT_CONFIG);
555
490
  for (const rules of [
556
491
  [
557
- { find: "a", replace: "b" },
558
- { find: "b", replace: "a" },
492
+ { literal: true, find: "a", replace: "b" },
493
+ { literal: true, find: "b", replace: "a" },
559
494
  ],
560
495
  [
561
- { find: "b", replace: "a" },
562
- { find: "a", replace: "b" },
496
+ { literal: true, find: "b", replace: "a" },
497
+ { literal: true, find: "a", replace: "b" },
563
498
  ],
564
499
  ]) {
565
500
  await writeFile(file, "ab");
@@ -571,8 +506,8 @@ test("adjacent matches and zero-width boundaries have deterministic batch semant
571
506
  const args = {
572
507
  path: file,
573
508
  replacements: [
574
- { find: "ab", replace: "X" },
575
- { find, replace: "!", regex: true },
509
+ { literal: true, find: "ab", replace: "X" },
510
+ { find, replace: "!", literal: false },
576
511
  ],
577
512
  };
578
513
  if (find === "$") {
@@ -581,7 +516,7 @@ test("adjacent matches and zero-width boundaries have deterministic batch semant
581
516
  } else {
582
517
  const failed = await call(tool, args);
583
518
  assert.equal(failed.isError, true);
584
- assert.equal(reportOf(failed)?.error, "OVERLAPPING_MATCHES");
519
+ assert.equal(reportOf(failed, "replace")?.error, "OVERLAPPING_MATCHES");
585
520
  assert.equal(await readFile(file, "utf8"), "ab");
586
521
  }
587
522
  }
@@ -589,12 +524,12 @@ test("adjacent matches and zero-width boundaries have deterministic batch semant
589
524
  const failed = await call(tool, {
590
525
  path: file,
591
526
  replacements: [
592
- { find: "^", replace: "x", regex: true },
593
- { find: "(?=a)", replace: "y", regex: true },
527
+ { find: "^", replace: "x", literal: false },
528
+ { find: "(?=a)", replace: "y", literal: false },
594
529
  ],
595
530
  });
596
531
  assert.equal(failed.isError, true);
597
- assert.equal(reportOf(failed)?.error, "OVERLAPPING_MATCHES");
532
+ assert.equal(reportOf(failed, "replace")?.error, "OVERLAPPING_MATCHES");
598
533
  assert.equal(await readFile(file, "utf8"), "ab");
599
534
  }));
600
535
 
@@ -614,13 +549,13 @@ test("regex templates agree with native replacement in single and snapshot batch
614
549
  ["😀#", "(?=[^#])", "gu"],
615
550
  ["aab#", "(a)", "gy"],
616
551
  ]) {
617
- const rule = { find, replace: template, regex: true, flags };
552
+ const rule = { find, replace: template, literal: false, flags };
618
553
  const expected = source.replace(new RegExp(find, flags), template);
619
554
  for (const batch of [false, true]) {
620
555
  await writeFile(file, source);
621
556
  // The second rule changes only the original final sentinel, not copies inserted by $' or $`.
622
557
  const args = batch
623
- ? { replacements: [rule, { find: "#$", replace: "!", regex: true }] }
558
+ ? { replacements: [rule, { find: "#$", replace: "!", literal: false }] }
624
559
  : { replacements: [rule] };
625
560
  await call(tool, { path: file, ...args });
626
561
  assert.equal(
@@ -644,7 +579,7 @@ test("successful batches run one command against the complete result", async ()
644
579
  assert.equal(input.command, "check");
645
580
  commands++;
646
581
  assert.equal(await readFile(file, "utf8"), "bar baz");
647
- return { status: "succeeded", output: "checked" };
582
+ return { status: "succeeded", result: { content: [{ type: "text", text: "checked" }] } };
648
583
  }),
649
584
  );
650
585
  const result = await tool.execute(
@@ -652,8 +587,8 @@ test("successful batches run one command against the complete result", async ()
652
587
  {
653
588
  path: file,
654
589
  replacements: [
655
- { find: "foo", replace: "bar" },
656
- { find: "bar", replace: "baz" },
590
+ { literal: true, find: "foo", replace: "bar" },
591
+ { literal: true, find: "bar", replace: "baz" },
657
592
  ],
658
593
  then_run: { command: "check" },
659
594
  },
@@ -662,10 +597,8 @@ test("successful batches run one command against the complete result", async ()
662
597
  { cwd: dir } as Parameters<typeof tool.execute>[4],
663
598
  );
664
599
  assert.equal(commands, 1);
665
- assert.ok(result.details.actionFusion);
666
- assert.equal(result.details.actionFusion.command, "succeeded");
667
- assert.equal(result.details.actionFusion.freshness, "unchanged");
668
- assert.equal(reportOf(result)?.then_run?.output, "checked");
600
+ assert.equal(reportOf(result, "replace")?.then_run?.status, "succeeded");
601
+ assert.equal(reportOf(result, "replace")?.then_run?.output, "checked");
669
602
  }));
670
603
 
671
604
  test("regex captures and zero-width insertions use logical offsets without consuming CRLF bytes", async () =>
@@ -675,27 +608,12 @@ test("regex captures and zero-width insertions use logical offsets without consu
675
608
  await writeFile(path, before);
676
609
  await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
677
610
  path,
678
- replacements: [{ find: "(b\rc\r)\\n", replace: "$1\n", regex: true }],
611
+ replacements: [{ find: "(b\rc\r)\\n", replace: "$1\n", literal: false }],
679
612
  });
680
613
  assert.equal(await readFile(path, "utf8"), before);
681
614
  await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
682
615
  path,
683
- replacements: [{ find: "(?=\\n)", replace: "!", regex: true }],
616
+ replacements: [{ find: "(?=\\n)", replace: "!", literal: false }],
684
617
  });
685
618
  assert.equal(await readFile(path, "utf8"), "a!\r\nb\rc\r!\r\n");
686
619
  }));
687
-
688
- test("replace rejects JSON-string rules without publishing", async () =>
689
- withDir(async (dir) => {
690
- const file = join(dir, "f.txt");
691
- await writeFile(file, "hello foo world");
692
- const tool = makeReplaceTool(dir, DEFAULT_CONFIG);
693
- await assert.rejects(
694
- callTool(tool, {
695
- path: "f.txt",
696
- replacements: JSON.stringify([{ find: "foo", replace: "bar" }]),
697
- }),
698
- rejectsArgument("replacements[0]", /object/),
699
- );
700
- assert.equal(await readFile(file, "utf8"), "hello foo world");
701
- }));