@criogaid/pi-hashline-edit 0.6.1 → 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.
- package/README.md +78 -369
- package/README.zh-CN.md +162 -0
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +11 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +328 -161
- package/src/core/apply.ts +129 -95
- package/src/core/case-fold.test.ts +44 -0
- package/src/core/case-fold.ts +77 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +184 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +32 -7
- package/src/core/ranges.test.ts +41 -9
- package/src/core/ranges.ts +11 -4
- package/src/core/regex.ts +51 -0
- package/src/core/replace-schema.ts +41 -0
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +40 -56
- package/src/core/search-analysis.test.ts +208 -0
- package/src/core/search-analysis.ts +379 -0
- package/src/core/search-matrix.test.ts +64 -0
- package/src/core/search-matrix.testing.ts +183 -0
- package/src/core/search-schema.ts +18 -0
- package/src/core/search.test.ts +145 -0
- package/src/core/search.ts +119 -0
- package/src/core/text.test.ts +16 -8
- package/src/core/text.ts +26 -6
- package/src/core/types.ts +20 -17
- package/src/index.ts +24 -16
- package/src/integration/config-registration.test.ts +42 -28
- package/src/integration/fusion-dispatch.test.ts +62 -127
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +273 -0
- package/src/integration/grep-js-completeness.test.ts +218 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +133 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +407 -0
- package/src/integration/grep-rg.test.ts +294 -436
- package/src/integration/grep-scan-budget.test.ts +111 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +56 -1
- package/src/integration/model-budget-lifecycle.test.ts +137 -0
- package/src/integration/mutation-file-types.test.ts +12 -26
- package/src/integration/mutation-file-types.testing.ts +33 -0
- package/src/integration/npm-package.test.ts +17 -0
- package/src/integration/npm-package.testing.ts +97 -0
- package/src/integration/publication-read-changes.test.ts +376 -0
- package/src/integration/read-changes.test.ts +98 -0
- package/src/integration/replace-package.test.ts +17 -36
- package/src/integration/replace-package.testing.ts +36 -0
- package/src/integration/replace-worker-boundary.test.ts +117 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +54 -0
- package/src/pi/action-fusion.test.ts +256 -251
- package/src/pi/action-fusion.ts +121 -332
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +16 -3
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +46 -0
- package/src/pi/argument-error.ts +76 -0
- package/src/pi/argument-format.test.ts +197 -0
- package/src/pi/argument-validation.test.ts +780 -49
- package/src/pi/argument-validation.ts +55 -58
- package/src/pi/budgets.ts +48 -21
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +29 -15
- package/src/pi/config.ts +27 -13
- package/src/pi/diagnostic-buffer.test.ts +51 -16
- package/src/pi/diagnostic-buffer.ts +13 -16
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +103 -99
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +407 -429
- package/src/pi/extension-loading.test.ts +7 -27
- package/src/pi/extension-loading.testing.ts +2 -14
- package/src/pi/failure-context.test.ts +265 -88
- package/src/pi/failure-context.ts +165 -181
- package/src/pi/failure-path.test.ts +179 -117
- package/src/pi/file-commit.test.ts +90 -73
- package/src/pi/file-commit.ts +216 -178
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +233 -51
- package/src/pi/fusion-card.ts +246 -142
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +191 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +187 -151
- package/src/pi/grep-render.ts +31 -25
- package/src/pi/grep-scope.ts +62 -54
- package/src/pi/grep-search.ts +327 -241
- package/src/pi/grep-tool.ts +126 -147
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +813 -713
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +6 -7
- package/src/pi/mutation-result.test.ts +45 -91
- package/src/pi/mutation-result.ts +86 -114
- package/src/pi/mutation-runner.ts +81 -81
- package/src/pi/output-budgets.test.ts +175 -0
- package/src/pi/path.ts +10 -1
- package/src/pi/pi.test.ts +4 -5
- package/src/pi/read-tool.ts +108 -142
- package/src/pi/read.test.ts +155 -96
- package/src/pi/regex-worker-contract.ts +55 -0
- package/src/pi/regex-worker.ts +95 -0
- package/src/pi/registration.test.ts +148 -124
- package/src/pi/render.test.ts +361 -84
- package/src/pi/render.ts +150 -32
- package/src/pi/replace-regex.ts +13 -48
- package/src/pi/replace-tool.ts +52 -158
- package/src/pi/replace-worker-contract.ts +10 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +191 -340
- package/src/pi/report-cause.ts +103 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +524 -0
- package/src/pi/report.test.ts +343 -0
- package/src/pi/report.ts +285 -0
- package/src/pi/result-safety.test.ts +216 -481
- package/src/pi/rg-process.test.ts +101 -76
- package/src/pi/rg-process.ts +130 -177
- package/src/pi/rg-regex.test.ts +67 -0
- package/src/pi/rg-regex.ts +715 -0
- package/src/pi/text-stream.test.ts +97 -69
- package/src/pi/text-stream.ts +97 -76
- package/src/pi/tool-call.testing.ts +20 -46
- package/src/pi/write-tool.ts +50 -41
- package/src/pi/write.test.ts +71 -95
- package/src/testing/extension-loader.testing.ts +24 -0
- package/src/testing/installation.testing.ts +77 -0
- package/src/testing/process.testing.ts +33 -0
- package/src/testing/regex-host.testing.ts +20 -0
- package/src/testing/regex-translation-corpus.ts +324 -0
- package/src/testing/rg-line-listing.fixture.mjs +7 -0
- package/src/testing/run-tests.ts +47 -0
- package/src/testing/session.testing.ts +127 -0
- package/src/integration/forget-grep.test.ts +0 -98
- package/src/pi/forget-tool.ts +0 -268
- package/src/pi/forget.test.ts +0 -343
- package/src/pi/forget.testing.ts +0 -273
- package/src/pi/rg-line-ranges.test.ts +0 -66
- package/src/pi/rg-line-ranges.ts +0 -75
- package/src/pi/rg-match-bytes.ts +0 -95
- package/src/pi/rg-text-view.ts +0 -192
- package/src/pi/tool-prompts.ts +0 -15
package/src/pi/replace.test.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* capture groups, flags, 0-match / invalid-pattern
|
|
4
|
-
* failures (signaled by throwing — pi's failure contract), diff + fresh-anchor
|
|
5
|
-
* return, and chaining a hashline `edit` on an anchor returned by `replace`.
|
|
6
|
-
*
|
|
7
|
-
* Failures are signaled by throwing (see edit-tool.ts); tests use assert.rejects.
|
|
8
|
-
*/
|
|
1
|
+
import { rejectsArgument, rejectsArguments } from "./argument-error.testing.ts";
|
|
2
|
+
/** Tool-level replace contracts: byte preservation, matching, and failures. */
|
|
9
3
|
import { test } from "node:test";
|
|
10
4
|
import assert from "node:assert/strict";
|
|
11
5
|
import { mkdtemp, rm, readFile, writeFile } from "node:fs/promises";
|
|
@@ -13,11 +7,11 @@ import { tmpdir } from "node:os";
|
|
|
13
7
|
import { join } from "node:path";
|
|
14
8
|
import { initTheme, type Theme } from "@earendil-works/pi-coding-agent";
|
|
15
9
|
import { makeReplaceTool } from "./replace-tool.ts";
|
|
16
|
-
import { makeEditOverride } from "./edit-tool.ts";
|
|
17
10
|
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
18
11
|
import { callTool } from "./tool-call.testing.ts";
|
|
19
12
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
20
13
|
import { DEFAULT_CONFIG } from "./config.ts";
|
|
14
|
+
import { reportOf } from "./report.ts";
|
|
21
15
|
|
|
22
16
|
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
23
17
|
const dir = await mkdtemp(join(tmpdir(), "hl-replace-"));
|
|
@@ -30,120 +24,108 @@ async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
|
30
24
|
|
|
31
25
|
const call = (tool: any, params: any) => callTool(tool, params, { toolCallId: "0" });
|
|
32
26
|
|
|
33
|
-
/** Extract a `LINE#HASH` anchor for `line` from a result text block. */
|
|
34
|
-
function anchorLine(block: string, line: number) {
|
|
35
|
-
const m = new RegExp(`^${line}#([0-9A-Z]+)(?:│|$)`, "m").exec(block);
|
|
36
|
-
if (!m) throw new Error(`line ${line} anchor not found in block`);
|
|
37
|
-
return `${line}#${m[1]}`;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
27
|
const stubTheme = { fg: (_k: string, s: string) => s, bold: (s: string) => s } as Theme;
|
|
41
28
|
|
|
42
29
|
// renderResult delegates to pi's renderDiff, which reads the global TUI theme
|
|
43
30
|
// singleton — initialize it once for this test process (watcher off by default).
|
|
44
31
|
initTheme();
|
|
45
32
|
|
|
46
|
-
test("replace
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
);
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
replacements: [
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
+
}
|
|
67
71
|
}
|
|
68
|
-
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test("replace schema rejects empty paths, empty find, and unsupported flags", async () => {
|
|
72
|
-
const tool = makeReplaceTool(process.cwd(), DEFAULT_CONFIG);
|
|
73
|
-
for (const args of [
|
|
74
|
-
{ path: "", replacements: [{ find: "old", replace: "new" }] },
|
|
75
|
-
{ path: "f.txt", replacements: [{ find: "", replace: "new" }] },
|
|
76
|
-
{ path: "f.txt", replacements: [{ find: "old", replace: "new", flags: "x" }] },
|
|
77
|
-
]) {
|
|
78
|
-
await assert.rejects(
|
|
79
|
-
call(tool, args),
|
|
80
|
-
/Validation failed for tool "replace"/,
|
|
81
|
-
JSON.stringify(args),
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
72
|
+
assert.equal(commands, 0);
|
|
73
|
+
}));
|
|
85
74
|
|
|
86
|
-
test("replace
|
|
75
|
+
test("replace rules omit literal or retain regex → reject before publication and fused commands", async () =>
|
|
87
76
|
withDir(async (dir) => {
|
|
88
|
-
const
|
|
89
|
-
await writeFile(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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: [] } };
|
|
95
86
|
}),
|
|
96
|
-
/Validation failed for tool "replace"[\s\S]*replacements/,
|
|
97
87
|
);
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const file = join(dir, "f.txt");
|
|
104
|
-
const before = "foo foo foo\n";
|
|
105
|
-
await writeFile(file, before);
|
|
106
|
-
const tool = makeReplaceTool(dir, DEFAULT_CONFIG);
|
|
107
|
-
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) {
|
|
108
93
|
await assert.rejects(
|
|
109
|
-
callTool(tool, {
|
|
110
|
-
|
|
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),
|
|
111
100
|
);
|
|
112
|
-
assert.equal(await readFile(
|
|
101
|
+
assert.equal(await readFile(path, "utf8"), "old\n");
|
|
113
102
|
}
|
|
103
|
+
assert.equal(commands, 0);
|
|
114
104
|
}));
|
|
115
105
|
|
|
116
106
|
test("replace rejects unwritable text and invalid regex before reading a missing target", async () =>
|
|
117
107
|
withDir(async (dir) => {
|
|
118
108
|
const file = join(dir, "missing.txt");
|
|
119
109
|
const tool = makeReplaceTool(dir, DEFAULT_CONFIG);
|
|
120
|
-
const valid = { path: file, replacements: [{ find: "x", replace: "y",
|
|
110
|
+
const valid = { path: file, replacements: [{ find: "x", replace: "y", literal: false }] };
|
|
121
111
|
assert.equal(tool.prepareArguments(valid), valid);
|
|
122
112
|
for (const [rule, expected] of [
|
|
123
113
|
[
|
|
124
|
-
{ find: "x", replace: "\0" },
|
|
125
|
-
|
|
114
|
+
{ literal: true, find: "x", replace: "\0" },
|
|
115
|
+
rejectsArgument("replacements[0].replace", /NUL/),
|
|
126
116
|
],
|
|
127
117
|
[
|
|
128
|
-
{ find: "x", replace: "\ud800" },
|
|
129
|
-
|
|
118
|
+
{ literal: true, find: "x", replace: "\ud800" },
|
|
119
|
+
rejectsArgument("replacements[0].replace", /losslessly.*UTF-8/),
|
|
130
120
|
],
|
|
131
121
|
[
|
|
132
|
-
{ find: "(", replace: "x",
|
|
133
|
-
|
|
122
|
+
{ find: "(", replace: "x", literal: false },
|
|
123
|
+
rejectsArgument("replacements[0].find", /Invalid regular expression/i),
|
|
134
124
|
],
|
|
135
125
|
] as const) {
|
|
136
126
|
await assert.rejects(callTool(tool, { path: file, replacements: [rule] }), expected);
|
|
137
127
|
await assert.rejects(readFile(file, "utf8"), { code: "ENOENT" });
|
|
138
128
|
}
|
|
139
|
-
await assert.rejects(
|
|
140
|
-
callTool(tool, {
|
|
141
|
-
path: file,
|
|
142
|
-
replacements: [{ find: "(", replace: "x", regex: true, flags: "z" }],
|
|
143
|
-
}),
|
|
144
|
-
/Validation failed for tool "replace":[\s\S]*replacements\.0\.flags: must match pattern/,
|
|
145
|
-
);
|
|
146
|
-
await assert.rejects(readFile(file, "utf8"), { code: "ENOENT" });
|
|
147
129
|
}));
|
|
148
130
|
|
|
149
131
|
test("replace literal: replaces all occurrences", async () => {
|
|
@@ -152,11 +134,11 @@ test("replace literal: replaces all occurrences", async () => {
|
|
|
152
134
|
await writeFile(f, "foo bar foo baz foo\n");
|
|
153
135
|
const r: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
154
136
|
path: "f.txt",
|
|
155
|
-
replacements: [{ find: "foo", replace: "qux" }],
|
|
137
|
+
replacements: [{ literal: true, find: "foo", replace: "qux" }],
|
|
156
138
|
});
|
|
157
139
|
assert.equal(r.isError, undefined);
|
|
158
140
|
assert.equal(await readFile(f, "utf-8"), "qux bar qux baz qux\n");
|
|
159
|
-
assert.
|
|
141
|
+
assert.equal(reportOf(r, "replace")?.matches, 3);
|
|
160
142
|
});
|
|
161
143
|
});
|
|
162
144
|
|
|
@@ -166,13 +148,13 @@ test("literal multiline replace matches LF and CRLF while retaining local separa
|
|
|
166
148
|
await writeFile(file, "\uFEFFhead\r\nold\r\none\r\nmid\nold\none\nend\r\n");
|
|
167
149
|
const result: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
168
150
|
path: "mixed.txt",
|
|
169
|
-
replacements: [{ find: "old\none", replace: "NEW\nX" }],
|
|
151
|
+
replacements: [{ literal: true, find: "old\none", replace: "NEW\nX" }],
|
|
170
152
|
});
|
|
171
|
-
assert.
|
|
153
|
+
assert.equal(reportOf(result, "replace")?.matches, 2);
|
|
172
154
|
assert.equal(await readFile(file, "utf8"), "\uFEFFhead\r\nNEW\r\nX\r\nmid\nNEW\nX\nend\r\n");
|
|
173
155
|
await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
174
156
|
path: "mixed.txt",
|
|
175
|
-
replacements: [{ find: "NEW\r\nX", replace: "done" }],
|
|
157
|
+
replacements: [{ literal: true, find: "NEW\r\nX", replace: "done" }],
|
|
176
158
|
});
|
|
177
159
|
assert.equal(await readFile(file, "utf8"), "\uFEFFhead\r\ndone\r\nmid\ndone\nend\r\n");
|
|
178
160
|
}));
|
|
@@ -180,11 +162,11 @@ test("literal multiline replace matches LF and CRLF while retaining local separa
|
|
|
180
162
|
test("both replacement modes preserve each matched separator", async () =>
|
|
181
163
|
withDir(async (dir) => {
|
|
182
164
|
const file = join(dir, "mixed.txt");
|
|
183
|
-
for (const
|
|
165
|
+
for (const literal of [true, false]) {
|
|
184
166
|
await writeFile(file, "a\r\nb\nc\r\ntail\n");
|
|
185
167
|
await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
186
168
|
path: "mixed.txt",
|
|
187
|
-
replacements: [{ find: "a\nb\nc", replace: "x\ny\nz\nextra",
|
|
169
|
+
replacements: [{ find: "a\nb\nc", replace: "x\ny\nz\nextra", literal }],
|
|
188
170
|
});
|
|
189
171
|
assert.equal(await readFile(file, "utf8"), "x\r\ny\nz\nextra\r\ntail\n");
|
|
190
172
|
}
|
|
@@ -198,10 +180,11 @@ test("literal replacement uses one LF view and preserves raw bytes across query
|
|
|
198
180
|
await writeFile(file, before);
|
|
199
181
|
const result = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
200
182
|
path: file,
|
|
201
|
-
replacements: [{ find, replace: find }],
|
|
183
|
+
replacements: [{ literal: true, find, replace: find }],
|
|
202
184
|
});
|
|
203
185
|
assert.equal(await readFile(file, "utf8"), before);
|
|
204
|
-
assert.
|
|
186
|
+
assert.equal(reportOf(result, "replace")?.matches, 1);
|
|
187
|
+
assert.equal(reportOf(result, "replace")?.publication, "NOT_PUBLISHED");
|
|
205
188
|
}
|
|
206
189
|
for (const [before, find, replacement, expected] of [
|
|
207
190
|
["a\r\nb", "a", "A\nextra", "A\r\nextra\r\nb"],
|
|
@@ -213,7 +196,7 @@ test("literal replacement uses one LF view and preserves raw bytes across query
|
|
|
213
196
|
await writeFile(file, before);
|
|
214
197
|
await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
215
198
|
path: file,
|
|
216
|
-
replacements: [{ find, replace: replacement }],
|
|
199
|
+
replacements: [{ literal: true, find, replace: replacement }],
|
|
217
200
|
});
|
|
218
201
|
assert.equal(await readFile(file, "utf8"), expected);
|
|
219
202
|
}
|
|
@@ -226,22 +209,21 @@ test("mixed literal and regex batches share raw offsets after CRLF normalization
|
|
|
226
209
|
await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
227
210
|
path: file,
|
|
228
211
|
replacements: [
|
|
229
|
-
{ find: "a\nb", replace: "A\nB" },
|
|
230
|
-
{ find: "(c)$", replace: "$1!",
|
|
212
|
+
{ literal: true, find: "a\nb", replace: "A\nB" },
|
|
213
|
+
{ find: "(c)$", replace: "$1!", literal: false },
|
|
231
214
|
],
|
|
232
215
|
});
|
|
233
216
|
assert.equal(await readFile(file, "utf8"), "😀\r\nA\r\nB\r\nc!");
|
|
234
217
|
const before = await readFile(file, "utf8");
|
|
235
|
-
await
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
);
|
|
218
|
+
const failed = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
219
|
+
path: file,
|
|
220
|
+
replacements: [
|
|
221
|
+
{ literal: true, find: "A\nB", replace: "x" },
|
|
222
|
+
{ find: "\\nB", replace: "y", literal: false },
|
|
223
|
+
],
|
|
224
|
+
});
|
|
225
|
+
assert.equal(failed.isError, true);
|
|
226
|
+
assert.equal(reportOf(failed, "replace")?.error, "OVERLAPPING_MATCHES");
|
|
245
227
|
assert.equal(await readFile(file, "utf8"), before);
|
|
246
228
|
}));
|
|
247
229
|
|
|
@@ -251,11 +233,10 @@ test("replace literal: $ in replacement stays literal (no expansion)", async ()
|
|
|
251
233
|
await writeFile(f, "cost: $5 here\n");
|
|
252
234
|
const r: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
253
235
|
path: "f.txt",
|
|
254
|
-
replacements: [{ find: "$5", replace: "$
|
|
236
|
+
replacements: [{ literal: true, find: "$5", replace: "$1|$<name>|$&|$$" }],
|
|
255
237
|
});
|
|
256
238
|
assert.equal(r.isError, undefined);
|
|
257
|
-
|
|
258
|
-
assert.equal(await readFile(f, "utf-8"), "cost: $10 here\n");
|
|
239
|
+
assert.equal(await readFile(f, "utf-8"), "cost: $1|$<name>|$&|$$ here\n");
|
|
259
240
|
});
|
|
260
241
|
});
|
|
261
242
|
|
|
@@ -265,7 +246,7 @@ test("replace literal: case-insensitive via flags", async () => {
|
|
|
265
246
|
await writeFile(f, "Foo fOo FOO\n");
|
|
266
247
|
await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
267
248
|
path: "f.txt",
|
|
268
|
-
replacements: [{ find: "foo", replace: "x", flags: "i" }],
|
|
249
|
+
replacements: [{ literal: true, find: "foo", replace: "x", flags: "i" }],
|
|
269
250
|
});
|
|
270
251
|
assert.equal(await readFile(f, "utf-8"), "x x x\n");
|
|
271
252
|
});
|
|
@@ -280,18 +261,14 @@ test("replace aborts catastrophic regex without blocking the event loop or publi
|
|
|
280
261
|
const timer = setTimeout(() => controller.abort(), 100);
|
|
281
262
|
const start = performance.now();
|
|
282
263
|
try {
|
|
283
|
-
await
|
|
284
|
-
makeReplaceTool(dir, DEFAULT_CONFIG)
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
controller.signal,
|
|
288
|
-
undefined,
|
|
289
|
-
{ cwd: dir } as Parameters<ReturnType<typeof makeReplaceTool>["execute"]>[4],
|
|
290
|
-
),
|
|
291
|
-
(error: unknown) =>
|
|
292
|
-
error instanceof Error &&
|
|
293
|
-
error.message === `Operation aborted before apply; ${file} was not changed.`,
|
|
264
|
+
const failed = await callTool(
|
|
265
|
+
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
266
|
+
{ path: file, replacements: [{ find: "^(a+)+$", replace: "x", literal: false }] },
|
|
267
|
+
{ toolCallId: "abort", signal: controller.signal, ctx: { cwd: dir } },
|
|
294
268
|
);
|
|
269
|
+
assert.equal(failed.isError, true);
|
|
270
|
+
assert.equal(reportOf(failed, "replace")?.error, "OPERATION_ABORTED");
|
|
271
|
+
assert.equal(reportOf(failed, "replace")?.publication, "NOT_PUBLISHED");
|
|
295
272
|
assert.ok(performance.now() - start < 2000);
|
|
296
273
|
assert.equal(await readFile(file, "utf8"), before);
|
|
297
274
|
} finally {
|
|
@@ -305,15 +282,14 @@ test("replace times out catastrophic regex at the configured limit without publi
|
|
|
305
282
|
const before = `${"a".repeat(35)}!`;
|
|
306
283
|
await writeFile(file, before);
|
|
307
284
|
const config = { ...DEFAULT_CONFIG, replace: { regexTimeoutMs: 1_000 } };
|
|
308
|
-
await
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
);
|
|
285
|
+
const failed = await call(makeReplaceTool(dir, config), {
|
|
286
|
+
path: file,
|
|
287
|
+
replacements: [{ find: "^(a+)+$", replace: "x", literal: false }],
|
|
288
|
+
});
|
|
289
|
+
assert.equal(failed.isError, true);
|
|
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");
|
|
317
293
|
assert.equal(await readFile(file, "utf8"), before);
|
|
318
294
|
});
|
|
319
295
|
});
|
|
@@ -324,7 +300,7 @@ test("replace regex: capture groups in replacement", async () => {
|
|
|
324
300
|
await writeFile(f, "name: alice\nname: bob\n");
|
|
325
301
|
await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
326
302
|
path: "f.txt",
|
|
327
|
-
replacements: [{ find: "name: (\\w+)", replace: "user: $1",
|
|
303
|
+
replacements: [{ find: "name: (\\w+)", replace: "user: $1", literal: false }],
|
|
328
304
|
});
|
|
329
305
|
assert.equal(await readFile(f, "utf-8"), "user: alice\nuser: bob\n");
|
|
330
306
|
});
|
|
@@ -337,7 +313,7 @@ test("replace regex: multiline flag matches line-anchored pattern", async () =>
|
|
|
337
313
|
// without `m`, ^a$ wouldn't match (a isn't at end of string); with `m` it does
|
|
338
314
|
await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
339
315
|
path: "f.txt",
|
|
340
|
-
replacements: [{ find: "^a$", replace: "A",
|
|
316
|
+
replacements: [{ find: "^a$", replace: "A", literal: false, flags: "m" }],
|
|
341
317
|
});
|
|
342
318
|
assert.equal(await readFile(f, "utf-8"), "A\nb\nc\n");
|
|
343
319
|
});
|
|
@@ -350,76 +326,24 @@ test("replace regex: dotall flag makes . match newlines", async () => {
|
|
|
350
326
|
// a.b only matches across the newline with the `s` (dotall) flag
|
|
351
327
|
await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
352
328
|
path: "f.txt",
|
|
353
|
-
replacements: [{ find: "a.b", replace: "X",
|
|
329
|
+
replacements: [{ find: "a.b", replace: "X", literal: false, flags: "s" }],
|
|
354
330
|
});
|
|
355
331
|
assert.equal(await readFile(f, "utf-8"), "X\n");
|
|
356
332
|
});
|
|
357
333
|
});
|
|
358
334
|
|
|
359
|
-
test("replace
|
|
335
|
+
test("replace finds no matches → returns failure and leaves file untouched", async () => {
|
|
360
336
|
await withDir(async (dir) => {
|
|
361
337
|
const f = join(dir, "f.txt");
|
|
362
338
|
await writeFile(f, "a\nb\n");
|
|
363
|
-
await
|
|
364
|
-
call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
365
|
-
path: "f.txt",
|
|
366
|
-
replacements: [{ find: "zzz", replace: "y" }],
|
|
367
|
-
}),
|
|
368
|
-
/no matches/,
|
|
369
|
-
);
|
|
370
|
-
assert.equal(await readFile(f, "utf-8"), "a\nb\n", "file untouched on 0-match failure");
|
|
371
|
-
});
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
test("replace: empty find throws", async () => {
|
|
375
|
-
await withDir(async (dir) => {
|
|
376
|
-
await writeFile(join(dir, "f.txt"), "a\n");
|
|
377
|
-
await assert.rejects(
|
|
378
|
-
callTool(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
379
|
-
path: "f.txt",
|
|
380
|
-
replacements: [{ find: "", replace: "x" }],
|
|
381
|
-
}),
|
|
382
|
-
/Validation failed for tool "replace"[\s\S]*find/,
|
|
383
|
-
);
|
|
384
|
-
});
|
|
385
|
-
});
|
|
386
|
-
|
|
387
|
-
test("replace: handles large match counts without an arbitrary cap", async () => {
|
|
388
|
-
await withDir(async (dir) => {
|
|
389
|
-
const f = join(dir, "f.txt");
|
|
390
|
-
await writeFile(f, "a\n".repeat(2500));
|
|
391
|
-
const result: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
339
|
+
const failed = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
392
340
|
path: "f.txt",
|
|
393
|
-
replacements: [{ find: "
|
|
341
|
+
replacements: [{ literal: true, find: "zzz", replace: "y" }],
|
|
394
342
|
});
|
|
395
|
-
assert.
|
|
396
|
-
assert.equal(
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
test("replace: invalid regex throws a friendly message", async () => {
|
|
401
|
-
await withDir(async (dir) => {
|
|
402
|
-
await writeFile(join(dir, "f.txt"), "a\n");
|
|
403
|
-
await assert.rejects(
|
|
404
|
-
call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
405
|
-
path: "f.txt",
|
|
406
|
-
replacements: [{ find: "(unclosed", replace: "x", regex: true }],
|
|
407
|
-
}),
|
|
408
|
-
/invalid regex/,
|
|
409
|
-
);
|
|
410
|
-
});
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
test("replace: invalid flag char throws", async () => {
|
|
414
|
-
await withDir(async (dir) => {
|
|
415
|
-
await writeFile(join(dir, "f.txt"), "a\n");
|
|
416
|
-
await assert.rejects(
|
|
417
|
-
callTool(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
418
|
-
path: "f.txt",
|
|
419
|
-
replacements: [{ find: "a", replace: "x", flags: "z" }],
|
|
420
|
-
}),
|
|
421
|
-
/Validation failed for tool "replace"[\s\S]*flags/,
|
|
422
|
-
);
|
|
343
|
+
assert.equal(failed.isError, true);
|
|
344
|
+
assert.equal(reportOf(failed, "replace")?.error, "NO_MATCH");
|
|
345
|
+
assert.equal(reportOf(failed, "replace")?.rule, 0);
|
|
346
|
+
assert.equal(await readFile(f, "utf-8"), "a\nb\n", "file untouched on 0-match failure");
|
|
423
347
|
});
|
|
424
348
|
});
|
|
425
349
|
|
|
@@ -430,82 +354,25 @@ test("replace: count-but-no-net-change does not rewrite and reports it", async (
|
|
|
430
354
|
// $& = whole match = "x", so the text is unchanged despite 2 matches
|
|
431
355
|
const r: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
432
356
|
path: "f.txt",
|
|
433
|
-
replacements: [{ find: "x", replace: "$&",
|
|
357
|
+
replacements: [{ find: "x", replace: "$&", literal: false }],
|
|
434
358
|
});
|
|
435
359
|
assert.equal(r.isError, undefined);
|
|
436
360
|
assert.equal(await readFile(f, "utf-8"), "xx\n");
|
|
437
|
-
assert.
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
test("replace: returns details.diff (string) + patch + firstChangedLine", async () => {
|
|
442
|
-
await withDir(async (dir) => {
|
|
443
|
-
const f = join(dir, "f.txt");
|
|
444
|
-
await writeFile(f, "a\nb\nc\n");
|
|
445
|
-
const r: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
446
|
-
path: "f.txt",
|
|
447
|
-
replacements: [{ find: "b", replace: "B" }],
|
|
448
|
-
});
|
|
449
|
-
assert.equal(typeof r.details.diff, "string");
|
|
450
|
-
assert.equal(typeof r.details.patch, "string");
|
|
451
|
-
assert.equal(typeof r.details.firstChangedLine, "number");
|
|
452
|
-
});
|
|
453
|
-
});
|
|
454
|
-
|
|
455
|
-
test("replace: returns fresh anchors for the changed region", async () => {
|
|
456
|
-
await withDir(async (dir) => {
|
|
457
|
-
const f = join(dir, "f.txt");
|
|
458
|
-
await writeFile(f, "a\nb\nc\n");
|
|
459
|
-
const r: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
460
|
-
path: "f.txt",
|
|
461
|
-
replacements: [{ find: "b", replace: "B" }],
|
|
462
|
-
});
|
|
463
|
-
const out: string = r.content[0].text;
|
|
464
|
-
assert.match(out, /Updated anchors/);
|
|
465
|
-
// line 2 now holds "B"; its anchor must be present and correct
|
|
466
|
-
const a2 = anchorLine(out, 2);
|
|
467
|
-
assert.match(a2, /^2#[0-9A-Z]{2,8}$/);
|
|
468
|
-
assert.match(out, new RegExp(`^${a2}$`, "m"));
|
|
469
|
-
assert.doesNotMatch(out, /│B/);
|
|
470
|
-
});
|
|
471
|
-
});
|
|
472
|
-
|
|
473
|
-
test("replace: changed-region anchor chains a subsequent hashline edit without a re-read", async () => {
|
|
474
|
-
await withDir(async (dir) => {
|
|
475
|
-
const f = join(dir, "f.txt");
|
|
476
|
-
await writeFile(f, "old old old\n");
|
|
477
|
-
const replace = makeReplaceTool(dir, DEFAULT_CONFIG);
|
|
478
|
-
const r1: any = await call(replace, {
|
|
479
|
-
path: "f.txt",
|
|
480
|
-
replacements: [{ find: "old", replace: "new" }],
|
|
481
|
-
});
|
|
482
|
-
const out: string = r1.content[0].text;
|
|
483
|
-
// line 1 is the changed line; chain an edit on its returned anchor
|
|
484
|
-
const a1 = anchorLine(out, 1);
|
|
485
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
486
|
-
const r2: any = await call(edit, {
|
|
487
|
-
path: "f.txt",
|
|
488
|
-
edits: [{ op: "replace", anchor: a1, body: ["NEW NEW NEW"] }],
|
|
489
|
-
});
|
|
490
|
-
assert.equal(r2.isError, undefined);
|
|
491
|
-
assert.equal(await readFile(f, "utf-8"), "NEW NEW NEW\n");
|
|
361
|
+
assert.equal(reportOf(r, "replace")?.publication, "NOT_PUBLISHED");
|
|
362
|
+
assert.equal(reportOf(r, "replace")?.matches, 2);
|
|
363
|
+
assert.equal(r.details.diff, "");
|
|
492
364
|
});
|
|
493
365
|
});
|
|
494
366
|
|
|
495
|
-
test("
|
|
367
|
+
test("multiline replacement → preserves the surrounding file text", async () => {
|
|
496
368
|
await withDir(async (dir) => {
|
|
497
369
|
const f = join(dir, "f.txt");
|
|
498
370
|
await writeFile(f, "a\nb\nc\n");
|
|
499
|
-
|
|
500
|
-
const r: any = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
371
|
+
await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
501
372
|
path: "f.txt",
|
|
502
|
-
replacements: [{ find: "b", replace: "B1\nB2" }],
|
|
373
|
+
replacements: [{ literal: true, find: "b", replace: "B1\nB2" }],
|
|
503
374
|
});
|
|
504
375
|
assert.equal(await readFile(f, "utf-8"), "a\nB1\nB2\nc\n");
|
|
505
|
-
const out: string = r.content[0].text;
|
|
506
|
-
// the two inserted/changed lines (2 and 3) should both be anchored
|
|
507
|
-
assert.doesNotThrow(() => anchorLine(out, 2));
|
|
508
|
-
assert.doesNotThrow(() => anchorLine(out, 3));
|
|
509
376
|
});
|
|
510
377
|
});
|
|
511
378
|
|
|
@@ -514,8 +381,8 @@ test("replace header retains the rule count when a long path wraps", () => {
|
|
|
514
381
|
const args = {
|
|
515
382
|
path: "x".repeat(68),
|
|
516
383
|
replacements: [
|
|
517
|
-
{ find: "first", replace: "second" },
|
|
518
|
-
{ find: "third", replace: "fourth" },
|
|
384
|
+
{ literal: true, find: "first", replace: "second" },
|
|
385
|
+
{ literal: true, find: "third", replace: "fourth" },
|
|
519
386
|
],
|
|
520
387
|
};
|
|
521
388
|
const header = tool.renderCall(args, stubTheme, {} as Parameters<typeof tool.renderCall>[2]);
|
|
@@ -523,28 +390,23 @@ test("replace header retains the rule count when a long path wraps", () => {
|
|
|
523
390
|
assert.match(header.render(80).join("\n"), /2\s+rules/);
|
|
524
391
|
});
|
|
525
392
|
|
|
526
|
-
test("replacement batches
|
|
393
|
+
test("replacement batches → evaluate every rule against one snapshot", async () =>
|
|
527
394
|
withDir(async (dir) => {
|
|
528
395
|
const file = join(dir, "batch.txt");
|
|
529
396
|
const tool = makeReplaceTool(dir, DEFAULT_CONFIG);
|
|
530
397
|
const args = {
|
|
531
398
|
path: file,
|
|
532
399
|
replacements: [
|
|
533
|
-
{ find: "foo", replace: "bar" },
|
|
534
|
-
{ find: "bar", replace: "baz" },
|
|
400
|
+
{ literal: true, find: "foo", replace: "bar" },
|
|
401
|
+
{ literal: true, find: "bar", replace: "baz" },
|
|
535
402
|
],
|
|
536
403
|
};
|
|
537
404
|
validateToolArguments(tool, { type: "toolCall", id: "0", name: "replace", arguments: args });
|
|
538
405
|
await writeFile(file, "\uFEFFfoo\r\nbar\nfoo");
|
|
539
406
|
const result = await call(tool, args);
|
|
540
407
|
assert.equal(await readFile(file, "utf8"), "\uFEFFbar\r\nbaz\nbar");
|
|
541
|
-
assert.
|
|
408
|
+
assert.equal(reportOf(result, "replace")?.matches, 3);
|
|
542
409
|
assert.match(result.details.diff, /^\+2 baz/m);
|
|
543
|
-
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
544
|
-
path: file,
|
|
545
|
-
edits: [{ op: "replace", anchor: anchorLine(result.content[0].text, 2), body: ["chained"] }],
|
|
546
|
-
});
|
|
547
|
-
assert.equal(await readFile(file, "utf8"), "\uFEFFbar\r\nchained\nbar");
|
|
548
410
|
}));
|
|
549
411
|
|
|
550
412
|
test("invalid batches reject every change and never run a fused command", async () =>
|
|
@@ -558,36 +420,28 @@ test("invalid batches reject every change and never run a fused command", async
|
|
|
558
420
|
DEFAULT_CONFIG,
|
|
559
421
|
createActionFusionExecutor(async () => {
|
|
560
422
|
commands++;
|
|
561
|
-
return { status: "succeeded",
|
|
423
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
|
|
562
424
|
}),
|
|
563
425
|
);
|
|
564
|
-
const first = { find: "bar", replace: "changed" };
|
|
426
|
+
const first = { literal: true, find: "bar", replace: "changed" };
|
|
565
427
|
const cases = [
|
|
566
428
|
[
|
|
567
|
-
/
|
|
568
|
-
[
|
|
569
|
-
{ replacements: [first, { find: "missing", replace: "x" }] },
|
|
570
|
-
{ replacements: [first, { find: "bar foo", replace: "x" }] },
|
|
571
|
-
{ replacements: [first, { find: "bar", replace: "x" }] },
|
|
572
|
-
],
|
|
429
|
+
rejectsArgument("replacements[1].find", /Invalid regular expression/i),
|
|
430
|
+
[{ replacements: [first, { find: "(", replace: "x", literal: false }] }],
|
|
573
431
|
],
|
|
574
432
|
[
|
|
575
|
-
|
|
576
|
-
[{ replacements: [first, { find: "
|
|
433
|
+
rejectsArgument("replacements[1].replace", /NUL/),
|
|
434
|
+
[{ replacements: [first, { literal: true, find: "foo", replace: "\0" }] }],
|
|
577
435
|
],
|
|
578
436
|
[
|
|
579
|
-
|
|
580
|
-
[{ replacements: [first, { find: "foo", replace: "\
|
|
437
|
+
rejectsArgument("replacements[1].replace", /losslessly.*UTF-8/),
|
|
438
|
+
[{ replacements: [first, { literal: true, find: "foo", replace: "\ud800" }] }],
|
|
581
439
|
],
|
|
582
440
|
[
|
|
583
|
-
|
|
584
|
-
[{ replacements: [first, { find: "foo", replace: "\ud800" }] }],
|
|
585
|
-
],
|
|
586
|
-
[
|
|
587
|
-
/Validation failed for tool "replace"/,
|
|
441
|
+
rejectsArguments(),
|
|
588
442
|
[
|
|
589
|
-
{ replacements: [first, { find: "", replace: "x" }] },
|
|
590
|
-
{ replacements: [first, { find: "foo" }] },
|
|
443
|
+
{ replacements: [first, { literal: true, find: "", replace: "x" }] },
|
|
444
|
+
{ replacements: [first, { literal: true, find: "foo" }] },
|
|
591
445
|
{ replacements: [] },
|
|
592
446
|
{ replacements: [first], find: "foo", replace: "x" },
|
|
593
447
|
{ replacements: [first], flags: "i" },
|
|
@@ -595,6 +449,22 @@ test("invalid batches reject every change and never run a fused command", async
|
|
|
595
449
|
],
|
|
596
450
|
],
|
|
597
451
|
] as const;
|
|
452
|
+
for (const [rule, code] of [
|
|
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"],
|
|
456
|
+
] as const) {
|
|
457
|
+
const failed = await callTool(
|
|
458
|
+
tool,
|
|
459
|
+
{ path: file, replacements: [first, rule], then_run: { command: "check" } },
|
|
460
|
+
{ ctx: { cwd: dir } },
|
|
461
|
+
);
|
|
462
|
+
assert.equal(failed.isError, true);
|
|
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");
|
|
466
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
467
|
+
}
|
|
598
468
|
for (const [expected, inputs] of cases) {
|
|
599
469
|
for (const args of inputs) {
|
|
600
470
|
await assert.rejects(
|
|
@@ -619,12 +489,12 @@ test("adjacent matches and zero-width boundaries have deterministic batch semant
|
|
|
619
489
|
const tool = makeReplaceTool(dir, DEFAULT_CONFIG);
|
|
620
490
|
for (const rules of [
|
|
621
491
|
[
|
|
622
|
-
{ find: "a", replace: "b" },
|
|
623
|
-
{ find: "b", replace: "a" },
|
|
492
|
+
{ literal: true, find: "a", replace: "b" },
|
|
493
|
+
{ literal: true, find: "b", replace: "a" },
|
|
624
494
|
],
|
|
625
495
|
[
|
|
626
|
-
{ find: "b", replace: "a" },
|
|
627
|
-
{ find: "a", replace: "b" },
|
|
496
|
+
{ literal: true, find: "b", replace: "a" },
|
|
497
|
+
{ literal: true, find: "a", replace: "b" },
|
|
628
498
|
],
|
|
629
499
|
]) {
|
|
630
500
|
await writeFile(file, "ab");
|
|
@@ -636,29 +506,31 @@ test("adjacent matches and zero-width boundaries have deterministic batch semant
|
|
|
636
506
|
const args = {
|
|
637
507
|
path: file,
|
|
638
508
|
replacements: [
|
|
639
|
-
{ find: "ab", replace: "X" },
|
|
640
|
-
{ find, replace: "!",
|
|
509
|
+
{ literal: true, find: "ab", replace: "X" },
|
|
510
|
+
{ find, replace: "!", literal: false },
|
|
641
511
|
],
|
|
642
512
|
};
|
|
643
513
|
if (find === "$") {
|
|
644
514
|
await call(tool, args);
|
|
645
515
|
assert.equal(await readFile(file, "utf8"), "X!");
|
|
646
516
|
} else {
|
|
647
|
-
await
|
|
517
|
+
const failed = await call(tool, args);
|
|
518
|
+
assert.equal(failed.isError, true);
|
|
519
|
+
assert.equal(reportOf(failed, "replace")?.error, "OVERLAPPING_MATCHES");
|
|
648
520
|
assert.equal(await readFile(file, "utf8"), "ab");
|
|
649
521
|
}
|
|
650
522
|
}
|
|
651
523
|
await writeFile(file, "ab");
|
|
652
|
-
await
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
);
|
|
524
|
+
const failed = await call(tool, {
|
|
525
|
+
path: file,
|
|
526
|
+
replacements: [
|
|
527
|
+
{ find: "^", replace: "x", literal: false },
|
|
528
|
+
{ find: "(?=a)", replace: "y", literal: false },
|
|
529
|
+
],
|
|
530
|
+
});
|
|
531
|
+
assert.equal(failed.isError, true);
|
|
532
|
+
assert.equal(reportOf(failed, "replace")?.error, "OVERLAPPING_MATCHES");
|
|
533
|
+
assert.equal(await readFile(file, "utf8"), "ab");
|
|
662
534
|
}));
|
|
663
535
|
|
|
664
536
|
test("regex templates agree with native replacement in single and snapshot batch modes", async () =>
|
|
@@ -677,13 +549,13 @@ test("regex templates agree with native replacement in single and snapshot batch
|
|
|
677
549
|
["😀#", "(?=[^#])", "gu"],
|
|
678
550
|
["aab#", "(a)", "gy"],
|
|
679
551
|
]) {
|
|
680
|
-
const rule = { find, replace: template,
|
|
552
|
+
const rule = { find, replace: template, literal: false, flags };
|
|
681
553
|
const expected = source.replace(new RegExp(find, flags), template);
|
|
682
554
|
for (const batch of [false, true]) {
|
|
683
555
|
await writeFile(file, source);
|
|
684
556
|
// The second rule changes only the original final sentinel, not copies inserted by $' or $`.
|
|
685
557
|
const args = batch
|
|
686
|
-
? { replacements: [rule, { find: "#$", replace: "!",
|
|
558
|
+
? { replacements: [rule, { find: "#$", replace: "!", literal: false }] }
|
|
687
559
|
: { replacements: [rule] };
|
|
688
560
|
await call(tool, { path: file, ...args });
|
|
689
561
|
assert.equal(
|
|
@@ -707,7 +579,7 @@ test("successful batches run one command against the complete result", async ()
|
|
|
707
579
|
assert.equal(input.command, "check");
|
|
708
580
|
commands++;
|
|
709
581
|
assert.equal(await readFile(file, "utf8"), "bar baz");
|
|
710
|
-
return { status: "succeeded",
|
|
582
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "checked" }] } };
|
|
711
583
|
}),
|
|
712
584
|
);
|
|
713
585
|
const result = await tool.execute(
|
|
@@ -715,8 +587,8 @@ test("successful batches run one command against the complete result", async ()
|
|
|
715
587
|
{
|
|
716
588
|
path: file,
|
|
717
589
|
replacements: [
|
|
718
|
-
{ find: "foo", replace: "bar" },
|
|
719
|
-
{ find: "bar", replace: "baz" },
|
|
590
|
+
{ literal: true, find: "foo", replace: "bar" },
|
|
591
|
+
{ literal: true, find: "bar", replace: "baz" },
|
|
720
592
|
],
|
|
721
593
|
then_run: { command: "check" },
|
|
722
594
|
},
|
|
@@ -725,14 +597,8 @@ test("successful batches run one command against the complete result", async ()
|
|
|
725
597
|
{ cwd: dir } as Parameters<typeof tool.execute>[4],
|
|
726
598
|
);
|
|
727
599
|
assert.equal(commands, 1);
|
|
728
|
-
assert.
|
|
729
|
-
assert.equal(result
|
|
730
|
-
assert.equal(result.details.actionFusion.freshness, "unchanged");
|
|
731
|
-
const command = result.content[1];
|
|
732
|
-
assert.ok(command.type === "text");
|
|
733
|
-
assert.match(command.text, /checked/);
|
|
734
|
-
assert.ok(result.content[0].type === "text");
|
|
735
|
-
assert.match(result.content[0].text, /Updated anchors/);
|
|
600
|
+
assert.equal(reportOf(result, "replace")?.then_run?.status, "succeeded");
|
|
601
|
+
assert.equal(reportOf(result, "replace")?.then_run?.output, "checked");
|
|
736
602
|
}));
|
|
737
603
|
|
|
738
604
|
test("regex captures and zero-width insertions use logical offsets without consuming CRLF bytes", async () =>
|
|
@@ -742,27 +608,12 @@ test("regex captures and zero-width insertions use logical offsets without consu
|
|
|
742
608
|
await writeFile(path, before);
|
|
743
609
|
await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
744
610
|
path,
|
|
745
|
-
replacements: [{ find: "(b\rc\r)\\n", replace: "$1\n",
|
|
611
|
+
replacements: [{ find: "(b\rc\r)\\n", replace: "$1\n", literal: false }],
|
|
746
612
|
});
|
|
747
613
|
assert.equal(await readFile(path, "utf8"), before);
|
|
748
614
|
await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
749
615
|
path,
|
|
750
|
-
replacements: [{ find: "(?=\\n)", replace: "!",
|
|
616
|
+
replacements: [{ find: "(?=\\n)", replace: "!", literal: false }],
|
|
751
617
|
});
|
|
752
618
|
assert.equal(await readFile(path, "utf8"), "a!\r\nb\rc\r!\r\n");
|
|
753
619
|
}));
|
|
754
|
-
|
|
755
|
-
test("replace rejects JSON-string rules without publishing", async () =>
|
|
756
|
-
withDir(async (dir) => {
|
|
757
|
-
const file = join(dir, "f.txt");
|
|
758
|
-
await writeFile(file, "hello foo world");
|
|
759
|
-
const tool = makeReplaceTool(dir, DEFAULT_CONFIG);
|
|
760
|
-
await assert.rejects(
|
|
761
|
-
callTool(tool, {
|
|
762
|
-
path: "f.txt",
|
|
763
|
-
replacements: JSON.stringify([{ find: "foo", replace: "bar" }]),
|
|
764
|
-
}),
|
|
765
|
-
/Validation failed for tool "replace"[\s\S]*replacements/,
|
|
766
|
-
);
|
|
767
|
-
assert.equal(await readFile(file, "utf8"), "hello foo world");
|
|
768
|
-
}));
|