@criogaid/pi-hashline-edit 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -12
- package/README.zh-CN.md +12 -12
- package/THIRD_PARTY_NOTICES.md +0 -6
- package/package.json +1 -1
- package/src/core/AGENTS.md +4 -5
- package/src/core/apply.test.ts +96 -271
- package/src/core/apply.ts +62 -169
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.test.ts +2 -3
- package/src/core/character-set.ts +5 -5
- package/src/core/errors.test.ts +23 -7
- package/src/core/errors.ts +32 -5
- package/src/core/hash.test.ts +8 -16
- package/src/core/hash.ts +3 -23
- package/src/core/lines.ts +2 -4
- package/src/core/ranges.test.ts +2 -1
- package/src/core/ranges.ts +1 -1
- package/src/core/regex.ts +3 -4
- package/src/core/replace.test.ts +47 -1
- package/src/core/replace.ts +19 -13
- package/src/core/search-analysis.test.ts +24 -27
- package/src/core/search-analysis.ts +34 -65
- package/src/core/search-matrix.testing.ts +0 -2
- package/src/core/search.ts +1 -1
- package/src/core/text.test.ts +5 -5
- package/src/core/text.ts +5 -12
- package/src/core/types.ts +8 -44
- package/src/index.ts +8 -8
- package/src/integration/config-registration.test.ts +8 -10
- package/src/integration/fusion-dispatch.test.ts +4 -7
- package/src/integration/grep-errors.test.ts +3 -3
- package/src/integration/grep-flags.test.ts +5 -19
- package/src/integration/grep-js-completeness.test.ts +45 -18
- package/src/integration/grep-line-candidates.test.ts +7 -10
- package/src/integration/grep-matrix.test.ts +6 -9
- package/src/integration/grep-order.test.ts +4 -4
- package/src/integration/grep-path-matrix.test.ts +10 -9
- package/src/integration/grep-prefilter-coverage.test.ts +5 -8
- package/src/integration/grep-process-limits.test.ts +76 -0
- package/src/integration/grep-regex.test.ts +10 -10
- package/src/integration/grep-rg.test.ts +166 -66
- package/src/integration/grep-scan-budget.test.ts +9 -9
- package/src/integration/grep-translation.test.ts +18 -32
- package/src/integration/large-text.test.ts +6 -52
- package/src/integration/model-budget-lifecycle.test.ts +10 -9
- package/src/integration/mutation-file-types.testing.ts +4 -4
- package/src/integration/npm-package.testing.ts +3 -3
- package/src/integration/publication-read-changes.test.ts +161 -83
- package/src/integration/read-changes.test.ts +4 -4
- package/src/integration/replace-worker-boundary.test.ts +22 -12
- package/src/integration/report-replay.test.ts +23 -26
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +18 -17
- package/src/pi/action-fusion.test.ts +241 -353
- package/src/pi/action-fusion.testing.ts +38 -0
- package/src/pi/action-fusion.ts +150 -125
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +25 -28
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +19 -18
- package/src/pi/argument-validation.test.ts +334 -283
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +6 -8
- package/src/pi/codemode.test.ts +7 -2
- package/src/pi/config.test.ts +7 -3
- package/src/pi/config.ts +12 -9
- package/src/pi/diagnostic-buffer.test.ts +34 -13
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +86 -211
- package/src/pi/error-text.ts +10 -9
- package/src/pi/execute.test.ts +249 -345
- package/src/pi/failure-context.test.ts +146 -127
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +182 -155
- package/src/pi/file-commit.test.ts +156 -71
- package/src/pi/file-commit.ts +126 -146
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +26 -22
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +19 -120
- package/src/pi/grep-candidates.ts +25 -76
- package/src/pi/grep-matrix.test.ts +5 -5
- package/src/pi/grep-order.test.ts +16 -15
- package/src/pi/grep-output.ts +94 -107
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +97 -0
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +41 -43
- package/src/pi/grep-search.ts +27 -39
- package/src/pi/grep-tool.ts +63 -106
- package/src/pi/grep.test.ts +177 -156
- package/src/pi/model-budgets.test.ts +27 -24
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-outcome.testing.ts +0 -1
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +52 -21
- package/src/pi/mutation-runner.ts +134 -74
- package/src/pi/output-budgets.test.ts +86 -42
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +18 -13
- package/src/pi/read-tool.ts +18 -23
- package/src/pi/read.test.ts +22 -41
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +85 -51
- package/src/pi/render.test.ts +88 -59
- package/src/pi/render.ts +64 -65
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -94
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +53 -38
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +10 -3
- package/src/pi/report-schema.ts +251 -148
- package/src/pi/report.test.ts +215 -67
- package/src/pi/report.ts +50 -98
- package/src/pi/result-safety.test.ts +241 -247
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +53 -75
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +35 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/tool-factories.testing.ts +59 -0
- package/src/pi/write-tool.ts +36 -71
- package/src/pi/write.test.ts +31 -34
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/testing/run-tests.ts +1 -1
- package/src/pi/replace-worker-contract.ts +0 -10
package/src/pi/execute.test.ts
CHANGED
|
@@ -1,26 +1,31 @@
|
|
|
1
|
+
import { commandCompletion, type SessionCommand } from "./action-fusion.testing.ts";
|
|
1
2
|
import { argumentError, rejectsArgument, rejectsArguments } from "./argument-error.testing.ts";
|
|
2
3
|
/** Tool-level edit and shared mutation workflow tests. */
|
|
3
4
|
import { test } from "node:test";
|
|
4
5
|
import assert from "node:assert/strict";
|
|
5
|
-
import { mkdtemp, rm, readFile, writeFile } from "node:fs/promises";
|
|
6
|
+
import { mkdtemp, readdir, rm, readFile, writeFile } from "node:fs/promises";
|
|
6
7
|
import { tmpdir } from "node:os";
|
|
7
8
|
import { join } from "node:path";
|
|
8
9
|
import { initTheme } from "@earendil-works/pi-coding-agent";
|
|
9
10
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
10
11
|
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
11
|
-
import { makeEditOverride } from "./
|
|
12
|
-
import { makeReadOverride } from "./
|
|
13
|
-
import { makeWriteOverride } from "./
|
|
14
|
-
import { makeReplaceTool } from "./
|
|
12
|
+
import { makeEditOverride } from "./tool-factories.testing.ts";
|
|
13
|
+
import { makeReadOverride } from "./tool-factories.testing.ts";
|
|
14
|
+
import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
15
|
+
import { makeReplaceTool } from "./tool-factories.testing.ts";
|
|
16
|
+
import { makeGrepOverride } from "./tool-factories.testing.ts";
|
|
15
17
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
16
18
|
import { computeLineHash } from "../core/hash.ts";
|
|
17
19
|
import { splitLines } from "../core/lines.ts";
|
|
18
20
|
import { callTool } from "./tool-call.testing.ts";
|
|
19
|
-
import { DEFAULT_CONFIG } from "./
|
|
20
|
-
import { formatByteSize
|
|
21
|
+
import { DEFAULT_CONFIG } from "./tool-factories.testing.ts";
|
|
22
|
+
import { formatByteSize } from "./budgets.ts";
|
|
23
|
+
import { DEFAULT_OUTPUT_BUDGETS } from "./tool-factories.testing.ts";
|
|
21
24
|
import { detailsReport, reportOf } from "./report.ts";
|
|
22
25
|
import type { Report } from "./report-schema.ts";
|
|
23
26
|
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
27
|
+
import { PATH_ERROR_MESSAGES } from "../core/errors.ts";
|
|
28
|
+
import { ERROR_DEFINITIONS } from "./report-schema.ts";
|
|
24
29
|
|
|
25
30
|
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
26
31
|
const dir = await mkdtemp(join(tmpdir(), "hl-"));
|
|
@@ -46,23 +51,26 @@ function anchorLine(block: string, line: number) {
|
|
|
46
51
|
return `${line}#${m[1]}`;
|
|
47
52
|
}
|
|
48
53
|
|
|
49
|
-
function failureReport(
|
|
54
|
+
function failureReport(
|
|
55
|
+
result: AgentToolResult<unknown> & { isError?: boolean },
|
|
56
|
+
tool = "edit",
|
|
57
|
+
): Report {
|
|
50
58
|
assert.equal(result.isError, true);
|
|
51
|
-
|
|
59
|
+
assert.ok(
|
|
60
|
+
tool === "edit" || tool === "replace" || tool === "write" || tool === "read" || tool === "grep",
|
|
61
|
+
);
|
|
62
|
+
const report = detailsReport(result.details, tool);
|
|
52
63
|
assert.ok(report?.error);
|
|
53
64
|
return report;
|
|
54
65
|
}
|
|
55
66
|
|
|
56
|
-
test("edit guidance → refreshes anchors
|
|
67
|
+
test("edit guidance → refreshes mutation anchors with an edit-only exception above changes", () => {
|
|
57
68
|
const guidance = makeEditOverride(process.cwd(), DEFAULT_CONFIG).promptGuidelines.join("\n");
|
|
58
|
-
assert.match(guidance, /
|
|
59
|
-
assert.match(
|
|
60
|
-
guidance,
|
|
61
|
-
/added or removed lines.*refresh anchors below the earliest affected line/i,
|
|
62
|
-
);
|
|
69
|
+
assert.match(guidance, /before citing a line after write, edit, or replace/i);
|
|
70
|
+
assert.match(guidance, /anchor from a new read or grep/i);
|
|
63
71
|
assert.match(
|
|
64
72
|
guidance,
|
|
65
|
-
/
|
|
73
|
+
/only after edit, lines above the earliest changed line keep their anchors/i,
|
|
66
74
|
);
|
|
67
75
|
});
|
|
68
76
|
|
|
@@ -77,7 +85,7 @@ test("edit execute: a file without a final newline stays byte-exact", async () =
|
|
|
77
85
|
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
78
86
|
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
79
87
|
path: "f.txt",
|
|
80
|
-
|
|
88
|
+
steps: [{ action: "replace", anchor: h(text, 2), lines: ["new value\u2060"] }],
|
|
81
89
|
});
|
|
82
90
|
assert.equal(r.isError, undefined, "should not be an error");
|
|
83
91
|
assert.equal(await readFile(f, "utf-8"), "guard\nnew value\u2060");
|
|
@@ -92,31 +100,13 @@ test("edit execute: hashline round-trip (read → edit → file changed)", async
|
|
|
92
100
|
const read = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
93
101
|
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
94
102
|
path: "f.txt",
|
|
95
|
-
|
|
103
|
+
steps: [{ action: "replace", anchor: anchorLine(read.content[0].text, 2), lines: ["B"] }],
|
|
96
104
|
});
|
|
97
105
|
assert.equal(r.isError, undefined, "should not be an error");
|
|
98
106
|
assert.equal(await readFile(f, "utf-8"), "a\nB\nc\n");
|
|
99
107
|
});
|
|
100
108
|
});
|
|
101
109
|
|
|
102
|
-
test("edit execute: multiple ops in one call", async () => {
|
|
103
|
-
await withDir(async (dir) => {
|
|
104
|
-
const f = join(dir, "f.txt");
|
|
105
|
-
const text = "a\nb\nc\n";
|
|
106
|
-
await writeFile(f, text);
|
|
107
|
-
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
108
|
-
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
109
|
-
path: "f.txt",
|
|
110
|
-
edits: [
|
|
111
|
-
{ action: "insert_after", anchor: h(text, 3), body: ["z"] },
|
|
112
|
-
{ action: "replace", anchor: h(text, 1), body: ["A"] },
|
|
113
|
-
],
|
|
114
|
-
});
|
|
115
|
-
assert.equal(r.isError, undefined);
|
|
116
|
-
assert.equal(await readFile(f, "utf-8"), "A\nb\nc\nz\n");
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
110
|
test("successful mutation → returns one report without anchor payload", async (t) => {
|
|
121
111
|
for (const tool of ["edit", "replace", "write"] as const) {
|
|
122
112
|
for (const count of [1, 3000]) {
|
|
@@ -138,12 +128,12 @@ test("successful mutation → returns one report without anchor payload", async
|
|
|
138
128
|
instance: makeEditOverride(dir, DEFAULT_CONFIG),
|
|
139
129
|
params: {
|
|
140
130
|
path,
|
|
141
|
-
|
|
131
|
+
steps: [
|
|
142
132
|
{
|
|
143
133
|
action: "replace",
|
|
144
134
|
anchor: anchorLine(rows, 1),
|
|
145
135
|
end: anchorLine(rows, count),
|
|
146
|
-
|
|
136
|
+
lines: Array.from({ length: count }, () => "new"),
|
|
147
137
|
},
|
|
148
138
|
],
|
|
149
139
|
},
|
|
@@ -179,22 +169,6 @@ test("successful mutation → returns one report without anchor payload", async
|
|
|
179
169
|
}
|
|
180
170
|
});
|
|
181
171
|
|
|
182
|
-
test("unrelated external change does NOT block an edit on a stable line", async () => {
|
|
183
|
-
await withDir(async (dir) => {
|
|
184
|
-
const f = join(dir, "f.txt");
|
|
185
|
-
const text = "a\nb\nc\n";
|
|
186
|
-
await writeFile(f, text);
|
|
187
|
-
// simulate an external change at line 3 between read and edit
|
|
188
|
-
await writeFile(f, "a\nb\nCHANGED\n");
|
|
189
|
-
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
190
|
-
path: "f.txt",
|
|
191
|
-
edits: [{ action: "replace", anchor: h(text, 1), body: ["A"] }],
|
|
192
|
-
});
|
|
193
|
-
assert.equal(r.isError, undefined);
|
|
194
|
-
assert.equal(await readFile(f, "utf-8"), "A\nb\nCHANGED\n");
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
|
|
198
172
|
test("edit on a line that changed externally → anchor mismatch", async () => {
|
|
199
173
|
await withDir(async (dir) => {
|
|
200
174
|
const f = join(dir, "f.txt");
|
|
@@ -204,7 +178,7 @@ test("edit on a line that changed externally → anchor mismatch", async () => {
|
|
|
204
178
|
const report = failureReport(
|
|
205
179
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
206
180
|
path: "f.txt",
|
|
207
|
-
|
|
181
|
+
steps: [{ action: "replace", anchor: h(text, 2), lines: ["x"] }],
|
|
208
182
|
}),
|
|
209
183
|
);
|
|
210
184
|
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
@@ -225,18 +199,18 @@ test("unresolved observations require target confirmation before retrying a move
|
|
|
225
199
|
const report = failureReport(
|
|
226
200
|
await call(edit, {
|
|
227
201
|
path: "recover.txt",
|
|
228
|
-
|
|
202
|
+
steps: [{ action: "replace", anchor: h(observed, 5), lines: ["updated"] }],
|
|
229
203
|
}),
|
|
230
204
|
);
|
|
231
205
|
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
232
206
|
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
233
|
-
assert.match(report.next ?? "", /
|
|
207
|
+
assert.match(report.next ?? "", /confirm.*intended target.*before reusing/i);
|
|
234
208
|
assert.match(report.failures?.[0].observed ?? "", /^5#[0-9A-Z]+│changed$/);
|
|
235
209
|
const read = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "recover.txt" });
|
|
236
210
|
const retryAnchor = anchorLine(read.content[0].text, 7);
|
|
237
211
|
await call(edit, {
|
|
238
212
|
path: "recover.txt",
|
|
239
|
-
|
|
213
|
+
steps: [{ action: "replace", anchor: retryAnchor, lines: ["updated"] }],
|
|
240
214
|
});
|
|
241
215
|
assert.equal(
|
|
242
216
|
await readFile(file, "utf8"),
|
|
@@ -245,20 +219,6 @@ test("unresolved observations require target confirmation before retrying a move
|
|
|
245
219
|
});
|
|
246
220
|
});
|
|
247
221
|
|
|
248
|
-
test("edit execute: no read before edit → anchor verification fails", async () => {
|
|
249
|
-
await withDir(async (dir) => {
|
|
250
|
-
await writeFile(join(dir, "f.txt"), "a\nb\n");
|
|
251
|
-
const report = failureReport(
|
|
252
|
-
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
253
|
-
path: "f.txt",
|
|
254
|
-
edits: [{ action: "replace", anchor: "1#XXXX", body: ["A"] }],
|
|
255
|
-
}),
|
|
256
|
-
);
|
|
257
|
-
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
258
|
-
assert.equal(await readFile(join(dir, "f.txt"), "utf8"), "a\nb\n");
|
|
259
|
-
});
|
|
260
|
-
});
|
|
261
|
-
|
|
262
222
|
test("edit execute: delete op", async () => {
|
|
263
223
|
await withDir(async (dir) => {
|
|
264
224
|
const f = join(dir, "f.txt");
|
|
@@ -267,7 +227,7 @@ test("edit execute: delete op", async () => {
|
|
|
267
227
|
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
268
228
|
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
269
229
|
path: "f.txt",
|
|
270
|
-
|
|
230
|
+
steps: [{ action: "delete", anchor: h(text, 2) }],
|
|
271
231
|
});
|
|
272
232
|
assert.equal(r.isError, undefined);
|
|
273
233
|
assert.equal(await readFile(f, "utf-8"), "a\nc\n");
|
|
@@ -323,23 +283,23 @@ test("edit schema rejects misspelled range fields and invalid operation shapes",
|
|
|
323
283
|
const anchor = h(original, 2);
|
|
324
284
|
const callArgs = {
|
|
325
285
|
path: "range.txt",
|
|
326
|
-
|
|
286
|
+
steps: [{ action: "replace", anchor, endd: h(original, 3), lines: ["merged"] }],
|
|
327
287
|
};
|
|
328
|
-
await assert.rejects(call(edit, callArgs), rejectsArgument("
|
|
288
|
+
await assert.rejects(call(edit, callArgs), rejectsArgument("steps[0].endd", /not allowed/));
|
|
329
289
|
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
330
|
-
for (const [
|
|
331
|
-
[[], rejectsArgument("
|
|
332
|
-
[[{ action: "replace", anchor }], rejectsArgument("
|
|
333
|
-
[[{ action: "delete", anchor,
|
|
334
|
-
[[{ action: "insert_after",
|
|
335
|
-
[[{ action: "append", anchor,
|
|
290
|
+
for (const [steps, expected] of [
|
|
291
|
+
[[], rejectsArgument("steps")],
|
|
292
|
+
[[{ action: "replace", anchor }], rejectsArgument("steps[0].lines", /required/)],
|
|
293
|
+
[[{ action: "delete", anchor, lines: ["bad"] }], rejectsArguments()],
|
|
294
|
+
[[{ action: "insert_after", lines: ["bad"] }], rejectsArguments()],
|
|
295
|
+
[[{ action: "append", anchor, lines: ["bad"] }], rejectsArguments()],
|
|
336
296
|
] as const) {
|
|
337
|
-
await assert.rejects(call(edit, { path: "range.txt",
|
|
297
|
+
await assert.rejects(call(edit, { path: "range.txt", steps }), expected);
|
|
338
298
|
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
339
299
|
}
|
|
340
300
|
const alternates: Parameters<typeof validateToolArguments>[1]["arguments"][] = [
|
|
341
|
-
{ path: "range.txt", action: "replace", anchor,
|
|
342
|
-
{ path: "range.txt",
|
|
301
|
+
{ path: "range.txt", action: "replace", anchor, lines: ["ok"] },
|
|
302
|
+
{ path: "range.txt", steps: JSON.stringify([{ action: "replace", anchor, lines: ["ok"] }]) },
|
|
343
303
|
];
|
|
344
304
|
for (const alternate of alternates) {
|
|
345
305
|
const before = structuredClone(alternate);
|
|
@@ -347,7 +307,7 @@ test("edit schema rejects misspelled range fields and invalid operation shapes",
|
|
|
347
307
|
assert.deepEqual(alternate, before);
|
|
348
308
|
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
349
309
|
}
|
|
350
|
-
await call(edit, { path: "range.txt",
|
|
310
|
+
await call(edit, { path: "range.txt", steps: [{ action: "replace", anchor, lines: ["B"] }] });
|
|
351
311
|
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), "a\nB\nc\n");
|
|
352
312
|
}));
|
|
353
313
|
|
|
@@ -363,12 +323,12 @@ test("copied string anchors validate and replace an inclusive range", async () =
|
|
|
363
323
|
name: "edit",
|
|
364
324
|
arguments: {
|
|
365
325
|
path: "range.txt",
|
|
366
|
-
|
|
326
|
+
steps: [
|
|
367
327
|
{
|
|
368
328
|
action: "replace",
|
|
369
329
|
anchor: anchorLine(read.content[0].text, 2),
|
|
370
330
|
end: anchorLine(read.content[0].text, 3),
|
|
371
|
-
|
|
331
|
+
lines: ["merged"],
|
|
372
332
|
},
|
|
373
333
|
],
|
|
374
334
|
},
|
|
@@ -384,17 +344,17 @@ test("invalid anchors and conflicting fields fail before changing the file", asy
|
|
|
384
344
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
385
345
|
const anchor = h(original, 1);
|
|
386
346
|
const invalid = [
|
|
387
|
-
{ action: "replace", anchor: { line: 1, hash: anchor.split("#")[1] },
|
|
347
|
+
{ action: "replace", anchor: { line: 1, hash: anchor.split("#")[1] }, lines: ["changed"] },
|
|
388
348
|
...["0#ABCD", "-1#ABCD", "1.5#ABCD", "1#", "1#abcd", "1#ABCD│a", "9007199254740993#ABCD"].map(
|
|
389
|
-
(anchor) => ({ action: "replace", anchor,
|
|
349
|
+
(anchor) => ({ action: "replace", anchor, lines: ["changed"] }),
|
|
390
350
|
),
|
|
391
|
-
{ action: "insert_after", anchor, end: anchor,
|
|
392
|
-
{ action: "append", anchor,
|
|
393
|
-
{ action: "delete", anchor,
|
|
351
|
+
{ action: "insert_after", anchor, end: anchor, lines: ["changed"] },
|
|
352
|
+
{ action: "append", anchor, lines: ["changed"] },
|
|
353
|
+
{ action: "delete", anchor, lines: ["changed"] },
|
|
394
354
|
];
|
|
395
355
|
for (const operation of invalid) {
|
|
396
356
|
await assert.rejects(
|
|
397
|
-
call(edit, { path: "invalid.txt",
|
|
357
|
+
call(edit, { path: "invalid.txt", steps: [operation] }),
|
|
398
358
|
/INVALID_ARGUMENTS|Invalid anchor/,
|
|
399
359
|
);
|
|
400
360
|
assert.equal(await readFile(join(dir, "invalid.txt"), "utf8"), original);
|
|
@@ -409,17 +369,17 @@ test("edit rejects empty bodies before file access for arrays and Pi-converted o
|
|
|
409
369
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
410
370
|
const anchor = h(original, 1);
|
|
411
371
|
for (const operation of [
|
|
412
|
-
{ action: "insert_after", anchor,
|
|
413
|
-
{ action: "insert_before", anchor,
|
|
414
|
-
{ action: "append",
|
|
415
|
-
{ action: "prepend",
|
|
416
|
-
{ action: "replace", anchor, end: h(original, 2),
|
|
372
|
+
{ action: "insert_after", anchor, lines: [] },
|
|
373
|
+
{ action: "insert_before", anchor, lines: [] },
|
|
374
|
+
{ action: "append", lines: [] },
|
|
375
|
+
{ action: "prepend", lines: [] },
|
|
376
|
+
{ action: "replace", anchor, end: h(original, 2), lines: [] },
|
|
417
377
|
]) {
|
|
418
378
|
for (const path of [file, join(dir, "missing.txt")]) {
|
|
419
|
-
for (const
|
|
379
|
+
for (const steps of [[operation], operation]) {
|
|
420
380
|
await assert.rejects(
|
|
421
|
-
call(edit, { path,
|
|
422
|
-
rejectsArgument("
|
|
381
|
+
call(edit, { path, steps }),
|
|
382
|
+
rejectsArgument("steps[0].lines", /empty/),
|
|
423
383
|
);
|
|
424
384
|
assert.equal(await readFile(file, "utf8"), original);
|
|
425
385
|
}
|
|
@@ -427,36 +387,37 @@ test("edit rejects empty bodies before file access for arrays and Pi-converted o
|
|
|
427
387
|
}
|
|
428
388
|
}));
|
|
429
389
|
|
|
430
|
-
test("mixed empty-
|
|
390
|
+
test("mixed empty-lines batches name every rejected edit without publishing or running commands", async () =>
|
|
431
391
|
withDir(async (dir) => {
|
|
432
392
|
const file = join(dir, "batch.txt");
|
|
433
393
|
const original = "a\nb\nc\nd\ne\n";
|
|
434
394
|
await writeFile(file, original);
|
|
435
395
|
let commandRuns = 0;
|
|
436
|
-
const
|
|
396
|
+
const executeTool: SessionCommand = async () => {
|
|
437
397
|
commandRuns++;
|
|
438
|
-
return {
|
|
439
|
-
}
|
|
398
|
+
return commandCompletion({ content: [{ type: "text", text: "ran" }] });
|
|
399
|
+
};
|
|
400
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
440
401
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
|
|
441
402
|
await assert.rejects(
|
|
442
403
|
callTool(
|
|
443
404
|
edit,
|
|
444
405
|
{
|
|
445
406
|
path: file,
|
|
446
|
-
|
|
447
|
-
{ action: "replace", anchor: h(original, 1),
|
|
448
|
-
{ action: "insert_before", anchor: h(original, 3),
|
|
449
|
-
{ action: "append",
|
|
450
|
-
{ action: "replace", anchor: h(original, 4),
|
|
407
|
+
steps: [
|
|
408
|
+
{ action: "replace", anchor: h(original, 1), lines: ["must not be written"] },
|
|
409
|
+
{ action: "insert_before", anchor: h(original, 3), lines: [] },
|
|
410
|
+
{ action: "append", lines: ["must not be written"] },
|
|
411
|
+
{ action: "replace", anchor: h(original, 4), lines: [] },
|
|
451
412
|
],
|
|
452
413
|
then_run: { command: "must-not-run" },
|
|
453
414
|
},
|
|
454
|
-
{ ctx: { cwd: dir } },
|
|
415
|
+
{ ctx: { ...{ cwd: dir }, executeTool } },
|
|
455
416
|
),
|
|
456
417
|
(error: Error) => {
|
|
457
418
|
const issues = argumentError(error).issues;
|
|
458
|
-
assert.match(issues.find((issue) => issue.field === "
|
|
459
|
-
assert.match(issues.find((issue) => issue.field === "
|
|
419
|
+
assert.match(issues.find((issue) => issue.field === "steps[1].lines")?.fix ?? "", /\[""\]/);
|
|
420
|
+
assert.match(issues.find((issue) => issue.field === "steps[3].lines")?.fix ?? "", /delete/);
|
|
460
421
|
return true;
|
|
461
422
|
},
|
|
462
423
|
);
|
|
@@ -464,21 +425,21 @@ test("mixed empty-body batches name every rejected edit without publishing or ru
|
|
|
464
425
|
assert.equal(commandRuns, 0);
|
|
465
426
|
}));
|
|
466
427
|
|
|
467
|
-
test("blank edit
|
|
428
|
+
test("blank edit lines preserve a real blank line for every line-bearing operation", async () =>
|
|
468
429
|
withDir(async (dir) => {
|
|
469
430
|
const file = join(dir, "blank_line.txt");
|
|
470
431
|
const original = "a\nb\nc\n";
|
|
471
432
|
const anchor = h(original, 1);
|
|
472
433
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
473
434
|
for (const [operation, expected] of [
|
|
474
|
-
[{ action: "replace", anchor,
|
|
475
|
-
[{ action: "insert_before", anchor,
|
|
476
|
-
[{ action: "insert_after", anchor,
|
|
477
|
-
[{ action: "append",
|
|
478
|
-
[{ action: "prepend",
|
|
435
|
+
[{ action: "replace", anchor, lines: [""] }, "\nb\nc\n"],
|
|
436
|
+
[{ action: "insert_before", anchor, lines: [""] }, "\na\nb\nc\n"],
|
|
437
|
+
[{ action: "insert_after", anchor, lines: [""] }, "a\n\nb\nc\n"],
|
|
438
|
+
[{ action: "append", lines: [""] }, "a\nb\nc\n\n"],
|
|
439
|
+
[{ action: "prepend", lines: [""] }, "\na\nb\nc\n"],
|
|
479
440
|
] as const) {
|
|
480
441
|
await writeFile(file, original);
|
|
481
|
-
await call(edit, { path: file,
|
|
442
|
+
await call(edit, { path: file, steps: [operation] });
|
|
482
443
|
assert.equal(await readFile(file, "utf8"), expected);
|
|
483
444
|
}
|
|
484
445
|
}));
|
|
@@ -490,7 +451,7 @@ test("explicit delete → removes an inclusive range", async () =>
|
|
|
490
451
|
await writeFile(file, original);
|
|
491
452
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
492
453
|
path: file,
|
|
493
|
-
|
|
454
|
+
steps: [{ action: "delete", anchor: h(original, 1), end: h(original, 2) }],
|
|
494
455
|
});
|
|
495
456
|
assert.equal(await readFile(file, "utf8"), "c\n");
|
|
496
457
|
}));
|
|
@@ -506,7 +467,7 @@ test("local and full-file recovery return copyable anchors without changing the
|
|
|
506
467
|
const report = failureReport(
|
|
507
468
|
await call(edit, {
|
|
508
469
|
path: "shift.txt",
|
|
509
|
-
|
|
470
|
+
steps: [{ action: "replace", anchor: h(original, 2), lines: ["B"] }],
|
|
510
471
|
}),
|
|
511
472
|
);
|
|
512
473
|
const failure = report.failures?.[0];
|
|
@@ -514,13 +475,12 @@ test("local and full-file recovery return copyable anchors without changing the
|
|
|
514
475
|
replacement = failure.candidate ?? "";
|
|
515
476
|
assert.equal(replacement, h(prefix + original, prefixLines + 2));
|
|
516
477
|
assert.equal(failure.content, "b");
|
|
517
|
-
assert.
|
|
518
|
-
assert.match(report.next ?? "", /confirm.*intended target/);
|
|
478
|
+
assert.match(report.next ?? "", /confirm.*intended target/i);
|
|
519
479
|
assert.equal(report.candidateNeighborhoods, undefined);
|
|
520
480
|
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), prefix + original);
|
|
521
481
|
await call(edit, {
|
|
522
482
|
path: "shift.txt",
|
|
523
|
-
|
|
483
|
+
steps: [{ action: "replace", anchor: replacement, lines: ["B"] }],
|
|
524
484
|
});
|
|
525
485
|
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), prefix + "a\nB\n");
|
|
526
486
|
}
|
|
@@ -535,11 +495,11 @@ test("shifted-anchor recovery keeps the read fallback for oversized candidates",
|
|
|
535
495
|
const report = failureReport(
|
|
536
496
|
await call(edit, {
|
|
537
497
|
path: "shift-large.txt",
|
|
538
|
-
|
|
498
|
+
steps: [{ action: "replace", anchor: h(original, 2), lines: ["B"] }],
|
|
539
499
|
}),
|
|
540
500
|
);
|
|
541
501
|
assert.match(
|
|
542
|
-
report.failures?.[0].
|
|
502
|
+
report.failures?.[0].notShown ?? "",
|
|
543
503
|
new RegExp(`exceeds ${formatByteSize(DEFAULT_OUTPUT_BUDGETS.fieldBytes)}`),
|
|
544
504
|
);
|
|
545
505
|
assert.match(report.next ?? "", /use read or grep/);
|
|
@@ -549,12 +509,15 @@ test("shifted-anchor recovery keeps the read fallback for oversized candidates",
|
|
|
549
509
|
test("failed commands preserve mutation results and stay out of all main card renderers", async () =>
|
|
550
510
|
withDir(async (dir) => {
|
|
551
511
|
const commands: string[] = [];
|
|
552
|
-
const
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
512
|
+
const executeTool: SessionCommand = async () => {
|
|
513
|
+
return commandCompletion(
|
|
514
|
+
{
|
|
515
|
+
content: [{ type: "text", text: "command-only diagnostic\nCommand exited with code 7" }],
|
|
516
|
+
},
|
|
517
|
+
true,
|
|
518
|
+
);
|
|
519
|
+
};
|
|
520
|
+
const fusion = createActionFusionExecutor((event) => commands.push(event.command));
|
|
558
521
|
const cases = [
|
|
559
522
|
{
|
|
560
523
|
path: "edit.txt",
|
|
@@ -562,18 +525,16 @@ test("failed commands preserve mutation results and stay out of all main card re
|
|
|
562
525
|
const tool = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
|
|
563
526
|
const args = {
|
|
564
527
|
path: "edit.txt",
|
|
565
|
-
|
|
528
|
+
steps: [{ action: "append" as const, lines: ["after"] }],
|
|
566
529
|
};
|
|
567
530
|
const result = await callTool(
|
|
568
531
|
tool,
|
|
569
532
|
{ ...args, then_run: { command: "check" } },
|
|
570
|
-
{
|
|
571
|
-
toolCallId: args.path,
|
|
572
|
-
ctx: { cwd: dir },
|
|
573
|
-
},
|
|
533
|
+
{ toolCallId: args.path, ctx: { ...{ cwd: dir }, executeTool } },
|
|
574
534
|
);
|
|
575
535
|
return {
|
|
576
536
|
result,
|
|
537
|
+
tool: tool.name,
|
|
577
538
|
rendered: tool
|
|
578
539
|
.renderResult(
|
|
579
540
|
result,
|
|
@@ -600,13 +561,11 @@ test("failed commands preserve mutation results and stay out of all main card re
|
|
|
600
561
|
const result = await callTool(
|
|
601
562
|
tool,
|
|
602
563
|
{ ...args, then_run: { command: "check" } },
|
|
603
|
-
{
|
|
604
|
-
toolCallId: args.path,
|
|
605
|
-
ctx: { cwd: dir },
|
|
606
|
-
},
|
|
564
|
+
{ toolCallId: args.path, ctx: { ...{ cwd: dir }, executeTool } },
|
|
607
565
|
);
|
|
608
566
|
return {
|
|
609
567
|
result,
|
|
568
|
+
tool: tool.name,
|
|
610
569
|
rendered: tool
|
|
611
570
|
.renderResult(
|
|
612
571
|
result,
|
|
@@ -630,13 +589,11 @@ test("failed commands preserve mutation results and stay out of all main card re
|
|
|
630
589
|
const result = await callTool(
|
|
631
590
|
tool,
|
|
632
591
|
{ ...args, then_run: { command: "check" } },
|
|
633
|
-
{
|
|
634
|
-
toolCallId: args.path,
|
|
635
|
-
ctx: { cwd: dir },
|
|
636
|
-
},
|
|
592
|
+
{ toolCallId: args.path, ctx: { ...{ cwd: dir }, executeTool } },
|
|
637
593
|
);
|
|
638
594
|
return {
|
|
639
595
|
result,
|
|
596
|
+
tool: tool.name,
|
|
640
597
|
rendered: tool
|
|
641
598
|
.renderResult(result, { expanded: true, isPartial: false }, stubTheme as Theme, {
|
|
642
599
|
args,
|
|
@@ -661,9 +618,16 @@ test("failed commands preserve mutation results and stay out of all main card re
|
|
|
661
618
|
];
|
|
662
619
|
for (const { path, run, expected } of cases) {
|
|
663
620
|
await writeFile(join(dir, path), "before\n");
|
|
664
|
-
const { result, rendered } = await run();
|
|
621
|
+
const { result, rendered, tool } = await run();
|
|
665
622
|
assert.equal("isError" in result, false);
|
|
666
|
-
|
|
623
|
+
assert.ok(
|
|
624
|
+
tool === "edit" ||
|
|
625
|
+
tool === "replace" ||
|
|
626
|
+
tool === "write" ||
|
|
627
|
+
tool === "read" ||
|
|
628
|
+
tool === "grep",
|
|
629
|
+
);
|
|
630
|
+
const report = detailsReport(result.details, tool);
|
|
667
631
|
assert.equal(report?.publication, "PUBLISHED");
|
|
668
632
|
const last = result.content.at(-1);
|
|
669
633
|
assert.ok(last?.type === "text");
|
|
@@ -680,6 +644,58 @@ test("failed commands preserve mutation results and stay out of all main card re
|
|
|
680
644
|
);
|
|
681
645
|
}));
|
|
682
646
|
|
|
647
|
+
test("missing or directory targets → tools share path diagnostics without filesystem changes", async (t) => {
|
|
648
|
+
await withDir(async (dir) => {
|
|
649
|
+
const before = await readdir(dir);
|
|
650
|
+
for (const target of [
|
|
651
|
+
{ name: "missing", path: "./missing.txt", code: "PATH_NOT_FOUND" },
|
|
652
|
+
{ name: "directory", path: ".", code: "NOT_REGULAR_FILE" },
|
|
653
|
+
] as const) {
|
|
654
|
+
const path = target.path;
|
|
655
|
+
const cases = [
|
|
656
|
+
{ tool: makeReadOverride(dir, DEFAULT_CONFIG), args: { path } },
|
|
657
|
+
{
|
|
658
|
+
tool: makeEditOverride(dir, DEFAULT_CONFIG),
|
|
659
|
+
args: { path, steps: [{ action: "append", lines: ["changed"] }] },
|
|
660
|
+
},
|
|
661
|
+
{
|
|
662
|
+
tool: makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
663
|
+
args: { path, replacements: [{ literal: true, find: "old", replace: "changed" }] },
|
|
664
|
+
},
|
|
665
|
+
{ tool: makeWriteOverride(dir), args: { path, content: "changed", mode: "overwrite" } },
|
|
666
|
+
...(target.code === "PATH_NOT_FOUND"
|
|
667
|
+
? [
|
|
668
|
+
{
|
|
669
|
+
tool: makeGrepOverride(dir, DEFAULT_CONFIG),
|
|
670
|
+
args: { path: [path], pattern: ["old"], literal: true },
|
|
671
|
+
},
|
|
672
|
+
]
|
|
673
|
+
: []),
|
|
674
|
+
];
|
|
675
|
+
for (const { tool, args } of cases) {
|
|
676
|
+
await t.test(`${tool.name}/${target.name}`, async () => {
|
|
677
|
+
const report = failureReport(await callTool(tool, args), tool.name);
|
|
678
|
+
assert.equal(report.error, target.code);
|
|
679
|
+
assert.equal(report.message, PATH_ERROR_MESSAGES[target.code]);
|
|
680
|
+
if (target.code === "PATH_NOT_FOUND") {
|
|
681
|
+
assert.deepEqual(report.missing, [join(dir, "missing.txt")]);
|
|
682
|
+
assert.equal(
|
|
683
|
+
report.next,
|
|
684
|
+
ERROR_DEFINITIONS.PATH_NOT_FOUND.next[
|
|
685
|
+
tool.name === "write" ? "useCreate" : "default"
|
|
686
|
+
],
|
|
687
|
+
);
|
|
688
|
+
} else {
|
|
689
|
+
assert.equal(report.missing, undefined);
|
|
690
|
+
assert.equal(report.next, ERROR_DEFINITIONS.NOT_REGULAR_FILE.next.default);
|
|
691
|
+
}
|
|
692
|
+
assert.deepEqual(await readdir(dir), before);
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
});
|
|
697
|
+
});
|
|
698
|
+
|
|
683
699
|
test("text tools reject malformed UTF-8 and NUL bytes without rewriting source bytes", async () =>
|
|
684
700
|
withDir(async (dir) => {
|
|
685
701
|
const target = join(dir, "invalid-utf8.txt");
|
|
@@ -689,14 +705,14 @@ test("text tools reject malformed UTF-8 and NUL bytes without rewriting source b
|
|
|
689
705
|
[makeReadOverride(dir, DEFAULT_CONFIG), { path: "invalid-utf8.txt" }],
|
|
690
706
|
[
|
|
691
707
|
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
692
|
-
{ path: "invalid-utf8.txt",
|
|
708
|
+
{ path: "invalid-utf8.txt", steps: [{ action: "append", lines: ["x"] }] },
|
|
693
709
|
],
|
|
694
710
|
[
|
|
695
711
|
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
696
712
|
{ path: "invalid-utf8.txt", replacements: [{ literal: true, find: "a", replace: "b" }] },
|
|
697
713
|
],
|
|
698
714
|
] as const) {
|
|
699
|
-
assert.equal(failureReport(await call(tool, args)).error, "UNSUPPORTED_ENCODING");
|
|
715
|
+
assert.equal(failureReport(await call(tool, args), tool.name).error, "UNSUPPORTED_ENCODING");
|
|
700
716
|
}
|
|
701
717
|
assert.deepEqual(await readFile(target), original);
|
|
702
718
|
|
|
@@ -706,14 +722,14 @@ test("text tools reject malformed UTF-8 and NUL bytes without rewriting source b
|
|
|
706
722
|
for (const [tool, args] of [
|
|
707
723
|
[
|
|
708
724
|
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
709
|
-
{ path: "nul.txt",
|
|
725
|
+
{ path: "nul.txt", steps: [{ action: "append", lines: ["x"] }] },
|
|
710
726
|
],
|
|
711
727
|
[
|
|
712
728
|
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
713
729
|
{ path: "nul.txt", replacements: [{ literal: true, find: "a", replace: "b" }] },
|
|
714
730
|
],
|
|
715
731
|
] as const) {
|
|
716
|
-
assert.equal(failureReport(await call(tool, args)).error, "UNSUPPORTED_TEXT");
|
|
732
|
+
assert.equal(failureReport(await call(tool, args), tool.name).error, "UNSUPPORTED_TEXT");
|
|
717
733
|
}
|
|
718
734
|
assert.deepEqual(await readFile(nulTarget), nulOriginal);
|
|
719
735
|
|
|
@@ -723,7 +739,10 @@ test("text tools reject malformed UTF-8 and NUL bytes without rewriting source b
|
|
|
723
739
|
const utf16Target = join(dir, "utf16.txt");
|
|
724
740
|
await writeFile(utf16Target, Buffer.from([0xff, 0xfe, 0x49, 0x6c]));
|
|
725
741
|
assert.equal(
|
|
726
|
-
failureReport(
|
|
742
|
+
failureReport(
|
|
743
|
+
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "utf16.txt" }),
|
|
744
|
+
"read",
|
|
745
|
+
).error,
|
|
727
746
|
"UNSUPPORTED_ENCODING",
|
|
728
747
|
);
|
|
729
748
|
}));
|
|
@@ -736,7 +755,7 @@ test("edit rejects embedded line terminators at its schema boundary", async () =
|
|
|
736
755
|
await assert.rejects(
|
|
737
756
|
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
738
757
|
path: "body.txt",
|
|
739
|
-
|
|
758
|
+
steps: [{ action: "append", lines: [line] }],
|
|
740
759
|
}),
|
|
741
760
|
rejectsArguments(),
|
|
742
761
|
);
|
|
@@ -748,14 +767,14 @@ test("edit rejects unwritable body lines before reading a missing target", async
|
|
|
748
767
|
withDir(async (dir) => {
|
|
749
768
|
const target = join(dir, "missing.txt");
|
|
750
769
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
751
|
-
const valid = { path: target,
|
|
770
|
+
const valid = { path: target, steps: [{ action: "append" as const, lines: ["ok"] }] };
|
|
752
771
|
assert.equal(edit.prepareArguments(valid), valid);
|
|
753
772
|
for (const [line, expected] of [
|
|
754
|
-
["bad\0", rejectsArgument("
|
|
755
|
-
["\ud800", rejectsArgument("
|
|
773
|
+
["bad\0", rejectsArgument("steps[0].lines[1]", /NUL/)],
|
|
774
|
+
["\ud800", rejectsArgument("steps[0].lines[1]", /losslessly.*UTF-8/)],
|
|
756
775
|
] as const) {
|
|
757
776
|
await assert.rejects(
|
|
758
|
-
call(edit, { path: target,
|
|
777
|
+
call(edit, { path: target, steps: [{ action: "append", lines: ["ok", line] }] }),
|
|
759
778
|
expected,
|
|
760
779
|
);
|
|
761
780
|
await assert.rejects(readFile(target, "utf8"), { code: "ENOENT" });
|
|
@@ -771,7 +790,7 @@ test("editing a BOM-prefixed file → publishes the changed bytes with the BOM i
|
|
|
771
790
|
await writeFile(target, original);
|
|
772
791
|
const result = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
773
792
|
path: "bom.txt",
|
|
774
|
-
|
|
793
|
+
steps: [{ action: "replace", anchor: h(source, line), lines: ["new"] }],
|
|
775
794
|
});
|
|
776
795
|
const expected = Buffer.from(line === 1 ? "\ufeffnew\nold\n" : "\ufeffguard\nnew\n", "utf8");
|
|
777
796
|
assert.deepEqual(await readFile(target), expected);
|
|
@@ -788,9 +807,9 @@ test("edit and replace reject NUL arguments without rewriting source bytes", asy
|
|
|
788
807
|
await assert.rejects(
|
|
789
808
|
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
790
809
|
path: "output.txt",
|
|
791
|
-
|
|
810
|
+
steps: [{ action: "append", lines: ["bad\0text"] }],
|
|
792
811
|
}),
|
|
793
|
-
rejectsArgument("
|
|
812
|
+
rejectsArgument("steps[0].lines[0]", /NUL/),
|
|
794
813
|
);
|
|
795
814
|
assert.deepEqual(await readFile(target), original);
|
|
796
815
|
await assert.rejects(
|
|
@@ -803,7 +822,7 @@ test("edit and replace reject NUL arguments without rewriting source bytes", asy
|
|
|
803
822
|
assert.deepEqual(await readFile(target), original);
|
|
804
823
|
}));
|
|
805
824
|
|
|
806
|
-
test("failed
|
|
825
|
+
test("failed steps return candidate code for a verified fused retry", async () =>
|
|
807
826
|
withDir(async (dir) => {
|
|
808
827
|
const original = Array.from({ length: 14 }, (_, index) => `line-${index + 1}`);
|
|
809
828
|
original[4] = "if (!fusion) throw new Error();";
|
|
@@ -812,10 +831,11 @@ test("failed edits expose input status and candidate code for a verified fused r
|
|
|
812
831
|
const file = join(dir, "candidate.txt");
|
|
813
832
|
await writeFile(file, before);
|
|
814
833
|
let commands = 0;
|
|
815
|
-
const
|
|
834
|
+
const executeTool: SessionCommand = async () => {
|
|
816
835
|
commands++;
|
|
817
|
-
return {
|
|
818
|
-
}
|
|
836
|
+
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
837
|
+
};
|
|
838
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
819
839
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
|
|
820
840
|
const stale = h(original.join("\n"), 5);
|
|
821
841
|
const stable = h(before, 12);
|
|
@@ -825,18 +845,18 @@ test("failed edits expose input status and candidate code for a verified fused r
|
|
|
825
845
|
edit,
|
|
826
846
|
{
|
|
827
847
|
path: file,
|
|
828
|
-
|
|
829
|
-
{ action: "insert_after", anchor: stale,
|
|
830
|
-
{ action: "replace", anchor: stable,
|
|
848
|
+
steps: [
|
|
849
|
+
{ action: "insert_after", anchor: stale, lines: ["const hashLen = 4;"] },
|
|
850
|
+
{ action: "replace", anchor: stable, lines: ["changed"] },
|
|
831
851
|
],
|
|
832
852
|
then_run: { command: "check" },
|
|
833
853
|
},
|
|
834
|
-
{ toolCallId: "failed", ctx: { cwd: dir } },
|
|
854
|
+
{ toolCallId: "failed", ctx: { ...{ cwd: dir }, executeTool } },
|
|
835
855
|
),
|
|
836
856
|
);
|
|
837
857
|
assert.equal(report.then_run?.status, "skipped");
|
|
838
|
-
assert.
|
|
839
|
-
assert.equal(report.failures?.[0].field, "
|
|
858
|
+
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
859
|
+
assert.equal(report.failures?.[0].field, "steps[0].anchor");
|
|
840
860
|
assert.equal(report.failures?.[0].cited, stale);
|
|
841
861
|
assert.equal(report.failures?.[0].candidate, candidate);
|
|
842
862
|
assert.equal(report.failures?.[0].content, original[4]);
|
|
@@ -848,17 +868,17 @@ test("failed edits expose input status and candidate code for a verified fused r
|
|
|
848
868
|
edit,
|
|
849
869
|
{
|
|
850
870
|
path: file,
|
|
851
|
-
|
|
871
|
+
steps: [
|
|
852
872
|
{
|
|
853
873
|
action: "insert_after",
|
|
854
874
|
anchor: report.failures![0].candidate!,
|
|
855
|
-
|
|
875
|
+
lines: ["const hashLen = 4;"],
|
|
856
876
|
},
|
|
857
|
-
{ action: "replace", anchor: stable,
|
|
877
|
+
{ action: "replace", anchor: stable, lines: ["changed"] },
|
|
858
878
|
],
|
|
859
879
|
then_run: { command: "check" },
|
|
860
880
|
},
|
|
861
|
-
{ toolCallId: "retry", ctx: { cwd: dir } },
|
|
881
|
+
{ toolCallId: "retry", ctx: { ...{ cwd: dir }, executeTool } },
|
|
862
882
|
);
|
|
863
883
|
const expected = splitLines(before);
|
|
864
884
|
expected[11] = "changed";
|
|
@@ -881,17 +901,16 @@ test("ambiguous candidates include distinguishing neighborhoods for a verified r
|
|
|
881
901
|
const report = failureReport(
|
|
882
902
|
await call(edit, {
|
|
883
903
|
path: file,
|
|
884
|
-
|
|
904
|
+
steps: [
|
|
885
905
|
{
|
|
886
906
|
action: "replace",
|
|
887
907
|
anchor: h("header\n return value;\n", 2),
|
|
888
|
-
|
|
908
|
+
lines: [" return updated;"],
|
|
889
909
|
},
|
|
890
910
|
],
|
|
891
911
|
}),
|
|
892
912
|
);
|
|
893
913
|
assert.equal(report.failures?.[0].result, "ambiguous");
|
|
894
|
-
assert.equal(report.failures?.[0].search, "local window");
|
|
895
914
|
assert.deepEqual(report.failures?.[0].candidates, [h(before, 5), h(before, 15)]);
|
|
896
915
|
assert.deepEqual(
|
|
897
916
|
report.candidateNeighborhoods?.map((group) => group.lines),
|
|
@@ -904,13 +923,13 @@ test("ambiguous candidates include distinguishing neighborhoods for a verified r
|
|
|
904
923
|
assert.equal(await readFile(file, "utf8"), before);
|
|
905
924
|
await call(edit, {
|
|
906
925
|
path: file,
|
|
907
|
-
|
|
926
|
+
steps: [{ action: "replace", anchor: anchorLine(context, 15), lines: [" return updated;"] }],
|
|
908
927
|
});
|
|
909
928
|
lines[14] = " return updated;";
|
|
910
929
|
assert.equal(await readFile(file, "utf8"), lines.join("\n") + "\n");
|
|
911
930
|
}));
|
|
912
931
|
|
|
913
|
-
test("schema-invalid bodies
|
|
932
|
+
test("schema-invalid bodies reject before anchor verification; retries revalidate", async () =>
|
|
914
933
|
withDir(async (dir) => {
|
|
915
934
|
const before = "a\nb\nc\n";
|
|
916
935
|
const file = join(dir, "checks.txt");
|
|
@@ -918,19 +937,18 @@ test("schema-invalid bodies omit anchor checks; subsequent retries revalidate",
|
|
|
918
937
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
919
938
|
const stable = h(before, 1);
|
|
920
939
|
for (const operation of [
|
|
921
|
-
{ action: "replace", anchor: stable,
|
|
922
|
-
{ action: "append",
|
|
940
|
+
{ action: "replace", anchor: stable, lines: ["bad\nline"] },
|
|
941
|
+
{ action: "append", lines: ["bad\nline"] },
|
|
923
942
|
]) {
|
|
924
943
|
for (const laterAnchor of [h(before, 2), "2#XXXX"]) {
|
|
925
944
|
await assert.rejects(
|
|
926
945
|
call(edit, {
|
|
927
946
|
path: file,
|
|
928
|
-
|
|
947
|
+
steps: [operation, { action: "replace", anchor: laterAnchor, lines: ["B"] }],
|
|
929
948
|
}),
|
|
930
949
|
(error: Error) => {
|
|
931
950
|
rejectsArguments()(error);
|
|
932
951
|
const report = argumentError(error);
|
|
933
|
-
assert.equal(report.matched, undefined);
|
|
934
952
|
assert.equal(report.failures, undefined);
|
|
935
953
|
return true;
|
|
936
954
|
},
|
|
@@ -941,74 +959,60 @@ test("schema-invalid bodies omit anchor checks; subsequent retries revalidate",
|
|
|
941
959
|
const rejected = failureReport(
|
|
942
960
|
await call(edit, {
|
|
943
961
|
path: file,
|
|
944
|
-
|
|
945
|
-
{ action: "replace", anchor: stable,
|
|
962
|
+
steps: [
|
|
963
|
+
{ action: "replace", anchor: stable, lines: ["A"] },
|
|
946
964
|
{ action: "delete", anchor: h(before, 2), end: "3#ZZZZ" },
|
|
947
965
|
],
|
|
948
966
|
}),
|
|
949
967
|
);
|
|
950
|
-
assert.
|
|
951
|
-
assert.equal(rejected.failures?.[0].field, "edits[1].end");
|
|
968
|
+
assert.equal(rejected.failures?.[0].field, "steps[1].end");
|
|
952
969
|
assert.equal(rejected.failures?.[0].cited, "3#ZZZZ");
|
|
953
|
-
assert.match(rejected.next ?? "", /Retries verify every anchor again/);
|
|
954
970
|
assert.equal(await readFile(file, "utf8"), before);
|
|
955
971
|
await writeFile(file, "changed\nb\nc\n");
|
|
956
972
|
const retry = failureReport(
|
|
957
973
|
await call(edit, {
|
|
958
974
|
path: file,
|
|
959
|
-
|
|
960
|
-
{ action: "replace", anchor: stable,
|
|
961
|
-
{ action: "replace", anchor: "3#ZZZZ",
|
|
975
|
+
steps: [
|
|
976
|
+
{ action: "replace", anchor: stable, lines: ["A"] },
|
|
977
|
+
{ action: "replace", anchor: "3#ZZZZ", lines: ["B"] },
|
|
962
978
|
],
|
|
963
979
|
}),
|
|
964
980
|
);
|
|
965
|
-
assert.
|
|
966
|
-
retry.failures?.
|
|
967
|
-
|
|
968
|
-
|
|
981
|
+
assert.deepEqual(
|
|
982
|
+
retry.failures?.map(({ field, cited }) => ({ field, cited })),
|
|
983
|
+
[
|
|
984
|
+
{ field: "steps[0].anchor", cited: stable },
|
|
985
|
+
{ field: "steps[1].anchor", cited: "3#ZZZZ" },
|
|
986
|
+
],
|
|
969
987
|
);
|
|
970
|
-
assert.equal(retry.matched, undefined);
|
|
971
988
|
assert.equal(await readFile(file, "utf8"), "changed\nb\nc\n");
|
|
972
989
|
}));
|
|
973
990
|
|
|
974
|
-
test("
|
|
975
|
-
withDir(async (dir) => {
|
|
976
|
-
const file = join(dir, "single.txt");
|
|
977
|
-
const source = "line1\nline2\nline3\n";
|
|
978
|
-
await writeFile(file, source);
|
|
979
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
980
|
-
for (const operation of [
|
|
981
|
-
{ action: "replace", anchor: "1#XXXX", body: ["new"] },
|
|
982
|
-
{ action: "replace", anchor: h(source, 1), end: "2#YYYY", body: ["new"] },
|
|
983
|
-
]) {
|
|
984
|
-
const report = failureReport(await call(edit, { path: file, edits: [operation] }));
|
|
985
|
-
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
986
|
-
assert.equal(report.matched, undefined);
|
|
987
|
-
assert.equal(report.omittedMatched, undefined);
|
|
988
|
-
assert.equal(await readFile(file, "utf8"), source);
|
|
989
|
-
}
|
|
990
|
-
}));
|
|
991
|
-
|
|
992
|
-
test("failed batches return more than forty checks and mappings when byte budgets allow", async () =>
|
|
991
|
+
test("failed batches over forty steps → report every failed anchor and its candidate content", async () =>
|
|
993
992
|
withDir(async (dir) => {
|
|
994
993
|
const original = "a\ntarget\nz\n";
|
|
995
994
|
const before = `prefix\n${original}`;
|
|
996
995
|
const file = join(dir, "many-checks.txt");
|
|
997
996
|
await writeFile(file, before);
|
|
998
|
-
const
|
|
997
|
+
const steps = Array.from({ length: 45 }, () => ({
|
|
999
998
|
action: "replace",
|
|
1000
999
|
anchor: h(original, 2),
|
|
1001
|
-
|
|
1000
|
+
lines: ["changed"],
|
|
1002
1001
|
}));
|
|
1003
1002
|
const report = failureReport(
|
|
1004
|
-
await call(makeEditOverride(dir, DEFAULT_CONFIG), { path: file,
|
|
1003
|
+
await call(makeEditOverride(dir, DEFAULT_CONFIG), { path: file, steps }),
|
|
1005
1004
|
);
|
|
1006
1005
|
assert.equal(report.failures?.length, 45);
|
|
1007
1006
|
assert.equal(report.omittedFailures, undefined);
|
|
1008
|
-
assert.
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1007
|
+
assert.deepEqual(
|
|
1008
|
+
report.failures?.map((failure) => failure.field),
|
|
1009
|
+
steps.map((_, index) => `steps[${index}].anchor`),
|
|
1010
|
+
);
|
|
1011
|
+
assert.ok(
|
|
1012
|
+
report.failures?.every(
|
|
1013
|
+
(failure) => failure.candidate === h(before, 3) && failure.content === "target",
|
|
1014
|
+
),
|
|
1015
|
+
);
|
|
1012
1016
|
assert.equal(await readFile(file, "utf8"), before);
|
|
1013
1017
|
}));
|
|
1014
1018
|
|
|
@@ -1021,7 +1025,7 @@ test("unique candidate content is independent of oversized neighboring rows", as
|
|
|
1021
1025
|
const report = failureReport(
|
|
1022
1026
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1023
1027
|
path: file,
|
|
1024
|
-
|
|
1028
|
+
steps: [{ action: "replace", anchor: h(original, 2), lines: ["updated"] }],
|
|
1025
1029
|
}),
|
|
1026
1030
|
);
|
|
1027
1031
|
const candidate = report.failures?.[0].candidate;
|
|
@@ -1033,7 +1037,7 @@ test("unique candidate content is independent of oversized neighboring rows", as
|
|
|
1033
1037
|
assert.equal(await readFile(file, "utf8"), before);
|
|
1034
1038
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1035
1039
|
path: file,
|
|
1036
|
-
|
|
1040
|
+
steps: [{ action: "replace", anchor: candidate, lines: ["updated"] }],
|
|
1037
1041
|
});
|
|
1038
1042
|
assert.equal(await readFile(file, "utf8"), before.replace("target", "updated"));
|
|
1039
1043
|
}));
|
|
@@ -1064,13 +1068,13 @@ test("shifted unique candidate → displays standalone CR without altering its a
|
|
|
1064
1068
|
await writeFile(file, before);
|
|
1065
1069
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1066
1070
|
path: file,
|
|
1067
|
-
|
|
1071
|
+
steps: [{ action: "delete", anchor: h(before, 1) }],
|
|
1068
1072
|
});
|
|
1069
1073
|
assert.equal(await readFile(file, "utf8"), "a\rb\n");
|
|
1070
1074
|
const report = failureReport(
|
|
1071
1075
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1072
1076
|
path: file,
|
|
1073
|
-
|
|
1077
|
+
steps: [{ action: "delete", anchor: h(before, 2) }],
|
|
1074
1078
|
}),
|
|
1075
1079
|
);
|
|
1076
1080
|
assert.equal(report.failures?.[0].candidate, h("a\rb\n", 1));
|
|
@@ -1088,19 +1092,19 @@ test("edit receives multiple wrong-length anchors → reports each location with
|
|
|
1088
1092
|
const short = h(text, 2).slice(0, -2);
|
|
1089
1093
|
const args = {
|
|
1090
1094
|
path: "f.txt",
|
|
1091
|
-
|
|
1092
|
-
{ action: "replace", anchor, end: short,
|
|
1095
|
+
steps: [
|
|
1096
|
+
{ action: "replace", anchor, end: short, lines: ["x"] },
|
|
1093
1097
|
{ action: "delete", anchor: short },
|
|
1094
1098
|
],
|
|
1095
1099
|
};
|
|
1096
1100
|
|
|
1097
1101
|
await assert.rejects(call(edit, args), (error: Error) => {
|
|
1098
|
-
rejectsArgument("
|
|
1099
|
-
rejectsArgument("
|
|
1102
|
+
rejectsArgument("steps[0].end")(error);
|
|
1103
|
+
rejectsArgument("steps[1].anchor")(error);
|
|
1100
1104
|
const issues = argumentError(error).issues;
|
|
1101
1105
|
assert.deepEqual(
|
|
1102
1106
|
issues.map((issue) => issue.field),
|
|
1103
|
-
["
|
|
1107
|
+
["steps[0].end", "steps[1].anchor"],
|
|
1104
1108
|
);
|
|
1105
1109
|
assert.equal(issues.filter((issue) => issue.fix?.includes("Read or grep")).length, 1);
|
|
1106
1110
|
assert.match(error.message, new RegExp(`${hashLen - 2} hash characters`));
|
|
@@ -1110,20 +1114,20 @@ test("edit receives multiple wrong-length anchors → reports each location with
|
|
|
1110
1114
|
assert.equal(await readFile(file, "utf8"), text);
|
|
1111
1115
|
}));
|
|
1112
1116
|
|
|
1113
|
-
test("Pi-converted single-object
|
|
1117
|
+
test("Pi-converted single-object steps execute as a canonical array", async () =>
|
|
1114
1118
|
withDir(async (dir) => {
|
|
1115
1119
|
const file = join(dir, "converted.txt");
|
|
1116
1120
|
const original = "first\nsecond\n";
|
|
1117
1121
|
await writeFile(file, original);
|
|
1118
1122
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1119
|
-
const operation = { action: "replace", anchor: h(original, 2),
|
|
1123
|
+
const operation = { action: "replace", anchor: h(original, 2), lines: ["SECOND"] };
|
|
1120
1124
|
const args = validateToolArguments(edit as any, {
|
|
1121
1125
|
type: "toolCall",
|
|
1122
1126
|
id: "converted",
|
|
1123
1127
|
name: "edit",
|
|
1124
|
-
arguments: { path: "converted.txt",
|
|
1128
|
+
arguments: { path: "converted.txt", steps: operation },
|
|
1125
1129
|
});
|
|
1126
|
-
assert.deepEqual(args.
|
|
1130
|
+
assert.deepEqual(args.steps, [operation]);
|
|
1127
1131
|
await call(edit, args);
|
|
1128
1132
|
assert.equal(await readFile(file, "utf8"), "first\nSECOND\n");
|
|
1129
1133
|
}));
|
|
@@ -1138,7 +1142,7 @@ test("unresolved snapshot rows support direct retry and revalidate after further
|
|
|
1138
1142
|
const report = failureReport(
|
|
1139
1143
|
await call(edit, {
|
|
1140
1144
|
path: "recover.txt",
|
|
1141
|
-
|
|
1145
|
+
steps: [{ action: "replace", anchor: h(observed, 2), lines: ["target = new"] }],
|
|
1142
1146
|
}),
|
|
1143
1147
|
);
|
|
1144
1148
|
const diagnostic = report.failures?.[0].observed ?? "";
|
|
@@ -1150,7 +1154,7 @@ test("unresolved snapshot rows support direct retry and revalidate after further
|
|
|
1150
1154
|
const retry = failureReport(
|
|
1151
1155
|
await call(edit, {
|
|
1152
1156
|
path: "recover.txt",
|
|
1153
|
-
|
|
1157
|
+
steps: [{ action: "replace", anchor: retryAnchor, lines: ["target = new"] }],
|
|
1154
1158
|
}),
|
|
1155
1159
|
);
|
|
1156
1160
|
assert.equal(retry.error, "ANCHOR_MISMATCH");
|
|
@@ -1158,7 +1162,7 @@ test("unresolved snapshot rows support direct retry and revalidate after further
|
|
|
1158
1162
|
await writeFile(file, current);
|
|
1159
1163
|
await call(edit, {
|
|
1160
1164
|
path: "recover.txt",
|
|
1161
|
-
|
|
1165
|
+
steps: [{ action: "replace", anchor: retryAnchor, lines: ["target = new"] }],
|
|
1162
1166
|
});
|
|
1163
1167
|
assert.deepEqual(await readFile(file), Buffer.from(observed.replace("old", "new")));
|
|
1164
1168
|
});
|
|
@@ -1174,13 +1178,13 @@ test("unresolved oversized and out-of-range rows require more context without pa
|
|
|
1174
1178
|
const report = failureReport(
|
|
1175
1179
|
await call(edit, {
|
|
1176
1180
|
path: "recover.txt",
|
|
1177
|
-
|
|
1181
|
+
steps: [{ action: "replace", anchor, lines: ["new"] }],
|
|
1178
1182
|
}),
|
|
1179
1183
|
);
|
|
1180
1184
|
assert.match(report.next ?? "", /read or grep/);
|
|
1181
1185
|
assert.equal(report.failures?.[0].observed, undefined);
|
|
1182
1186
|
assert.equal(
|
|
1183
|
-
report.failures?.[0].
|
|
1187
|
+
report.failures?.[0].notShown,
|
|
1184
1188
|
anchor.startsWith("99#")
|
|
1185
1189
|
? "cited line is out of range"
|
|
1186
1190
|
: `row exceeds ${formatByteSize(DEFAULT_OUTPUT_BUDGETS.fieldBytes)}`,
|
|
@@ -1197,7 +1201,7 @@ test("edit rejects impossible checksum characters before accessing the file", as
|
|
|
1197
1201
|
await assert.rejects(
|
|
1198
1202
|
call(tool, {
|
|
1199
1203
|
path: "missing.txt",
|
|
1200
|
-
|
|
1204
|
+
steps: [{ action: "delete", anchor: `1#${char.repeat(DEFAULT_CONFIG.hashLen)}` }],
|
|
1201
1205
|
}),
|
|
1202
1206
|
rejectsArguments(),
|
|
1203
1207
|
);
|
|
@@ -1215,7 +1219,7 @@ test("ambiguous failure lists and neighborhoods select the same first eight cand
|
|
|
1215
1219
|
const report = failureReport(
|
|
1216
1220
|
await call(tool, {
|
|
1217
1221
|
path: "candidates.txt",
|
|
1218
|
-
|
|
1222
|
+
steps: [{ action: "delete", anchor: `200#${computeLineHash(200, "target", 4)}` }],
|
|
1219
1223
|
}),
|
|
1220
1224
|
);
|
|
1221
1225
|
assert.equal(report.failures?.[0].omittedCandidates, 1);
|
|
@@ -1236,114 +1240,14 @@ test("edit verification → uses the supplied hash length", async () =>
|
|
|
1236
1240
|
await writeFile(join(dir, "registered.txt"), "before\n");
|
|
1237
1241
|
const result = await call(edit, {
|
|
1238
1242
|
path: "registered.txt",
|
|
1239
|
-
|
|
1243
|
+
steps: [
|
|
1240
1244
|
{
|
|
1241
1245
|
action: "replace",
|
|
1242
1246
|
anchor: `1#${computeLineHash(1, "before", 6)}`,
|
|
1243
|
-
|
|
1247
|
+
lines: ["after"],
|
|
1244
1248
|
},
|
|
1245
1249
|
],
|
|
1246
1250
|
});
|
|
1247
1251
|
assert.equal(result.isError, undefined);
|
|
1248
1252
|
assert.equal(await readFile(join(dir, "registered.txt"), "utf8"), "after\n");
|
|
1249
1253
|
}));
|
|
1250
|
-
|
|
1251
|
-
test("copy/move through read anchors → publishes captured bytes with BOM and mixed endings intact", async () =>
|
|
1252
|
-
withDir(async (dir) => {
|
|
1253
|
-
const path = "transfer.txt";
|
|
1254
|
-
const file = join(dir, path);
|
|
1255
|
-
const source = "\uFEFFA\r\nB\nC\r\nD\n";
|
|
1256
|
-
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
1257
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1258
|
-
for (const op of ["copy", "move"] as const) {
|
|
1259
|
-
await writeFile(file, source);
|
|
1260
|
-
const observed: Awaited<ReturnType<typeof read.execute>> = await callTool(read, { path });
|
|
1261
|
-
const rows = observed.content
|
|
1262
|
-
.filter((block) => block.type === "text")
|
|
1263
|
-
.map((block) => block.text)
|
|
1264
|
-
.join("\n");
|
|
1265
|
-
const result: Awaited<ReturnType<typeof edit.execute>> = await callTool(edit, {
|
|
1266
|
-
path,
|
|
1267
|
-
edits: [
|
|
1268
|
-
{
|
|
1269
|
-
action: op,
|
|
1270
|
-
anchor: anchorLine(rows, 1),
|
|
1271
|
-
end: anchorLine(rows, 2),
|
|
1272
|
-
after: anchorLine(rows, 4),
|
|
1273
|
-
},
|
|
1274
|
-
],
|
|
1275
|
-
});
|
|
1276
|
-
const expected = op === "copy" ? source + "A\r\nB\n" : "\uFEFFC\r\nD\nA\r\nB\n";
|
|
1277
|
-
const bytes = Buffer.from(expected, "utf8");
|
|
1278
|
-
assert.deepEqual(await readFile(file), bytes);
|
|
1279
|
-
assert.equal(result.isError, undefined);
|
|
1280
|
-
assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
|
|
1281
|
-
}
|
|
1282
|
-
}));
|
|
1283
|
-
|
|
1284
|
-
test("copy/move stale destination → report its field and skip publication and fused commands", async () =>
|
|
1285
|
-
withDir(async (dir) => {
|
|
1286
|
-
const original = "A\nB\nC\n";
|
|
1287
|
-
const current = "A\nB\nNEW\n";
|
|
1288
|
-
const file = join(dir, "stale-transfer.txt");
|
|
1289
|
-
await writeFile(file, current);
|
|
1290
|
-
let commands = 0;
|
|
1291
|
-
const fusion = createActionFusionExecutor(async () => {
|
|
1292
|
-
commands++;
|
|
1293
|
-
return { status: "succeeded", result: { content: [{ type: "text", text: "ran" }] } };
|
|
1294
|
-
});
|
|
1295
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
|
|
1296
|
-
for (const op of ["copy", "move"] as const) {
|
|
1297
|
-
const report = failureReport(
|
|
1298
|
-
await callTool(edit, {
|
|
1299
|
-
path: file,
|
|
1300
|
-
edits: [
|
|
1301
|
-
{ action: op, anchor: h(current, 1), after: h(original, 3) },
|
|
1302
|
-
{ action: "replace", anchor: h(current, 2), body: ["changed"] },
|
|
1303
|
-
],
|
|
1304
|
-
then_run: { command: "check" },
|
|
1305
|
-
}),
|
|
1306
|
-
);
|
|
1307
|
-
assert.equal(report.failures?.[0].field, "edits[0].after");
|
|
1308
|
-
assert.equal(report.failures?.[0].cited, h(original, 3));
|
|
1309
|
-
assert.equal(report.then_run?.status, "skipped");
|
|
1310
|
-
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
1311
|
-
assert.equal(await readFile(file, "utf8"), current);
|
|
1312
|
-
assert.equal(commands, 0);
|
|
1313
|
-
}
|
|
1314
|
-
}));
|
|
1315
|
-
|
|
1316
|
-
test("copy/move with fused command → execute after the complete transfer and preserve it on failure", async () =>
|
|
1317
|
-
withDir(async (dir) => {
|
|
1318
|
-
const source = "A\nB\nC\n";
|
|
1319
|
-
const file = join(dir, "fused-transfer.txt");
|
|
1320
|
-
for (const op of ["copy", "move"] as const) {
|
|
1321
|
-
await writeFile(file, source);
|
|
1322
|
-
const expected = op === "copy" ? "A\nB\nC\nA\n" : "B\nC\nA\n";
|
|
1323
|
-
let commands = 0;
|
|
1324
|
-
let commandBytes: string | undefined;
|
|
1325
|
-
const commandFailure = new Error("Command exited with code 7");
|
|
1326
|
-
const fusion = createActionFusionExecutor(async () => {
|
|
1327
|
-
commands++;
|
|
1328
|
-
commandBytes = await readFile(file, "utf8");
|
|
1329
|
-
throw commandFailure;
|
|
1330
|
-
});
|
|
1331
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
|
|
1332
|
-
const result: Awaited<ReturnType<typeof edit.execute>> = await callTool(
|
|
1333
|
-
edit,
|
|
1334
|
-
{
|
|
1335
|
-
path: file,
|
|
1336
|
-
edits: [{ action: op, anchor: h(source, 1), after: h(source, 3) }],
|
|
1337
|
-
then_run: { command: "check" },
|
|
1338
|
-
},
|
|
1339
|
-
{ ctx: { cwd: dir } },
|
|
1340
|
-
);
|
|
1341
|
-
assert.equal(reportOf(result, "edit")?.then_run?.status, "failed");
|
|
1342
|
-
assert.equal(reportOf(result, "edit")?.then_run?.output, commandFailure.message);
|
|
1343
|
-
assert.equal(commandBytes, expected);
|
|
1344
|
-
assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
|
|
1345
|
-
assert.equal(reportOf(result, "edit")?.freshness, undefined);
|
|
1346
|
-
assert.equal(await readFile(file, "utf8"), expected);
|
|
1347
|
-
assert.equal(commands, 1);
|
|
1348
|
-
}
|
|
1349
|
-
}));
|