@criogaid/pi-hashline-edit 0.13.0 → 0.15.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 +18 -12
- package/README.zh-CN.md +18 -12
- package/package.json +8 -1
- package/src/core/AGENTS.md +3 -1
- package/src/core/apply.ts +110 -117
- package/src/core/character-set.ts +2 -3
- package/src/core/copy-span.ts +15 -0
- package/src/core/errors.ts +24 -9
- package/src/core/lines.ts +71 -17
- package/src/core/ranges.ts +5 -4
- package/src/core/replace-schema.ts +3 -1
- package/src/core/replace.ts +50 -29
- package/src/core/search-analysis.ts +21 -22
- package/src/core/search.ts +15 -32
- package/src/core/types.ts +3 -0
- package/src/index.ts +47 -39
- package/src/pi/AGENTS.md +30 -19
- package/src/pi/action-fusion.ts +284 -211
- package/src/pi/argument-diagnostics.ts +50 -37
- package/src/pi/argument-error.ts +2 -2
- package/src/pi/argument-validation.ts +163 -66
- package/src/pi/budgets.ts +12 -0
- package/src/pi/diagnostic-buffer.ts +12 -11
- package/src/pi/edit-tool.ts +138 -115
- package/src/pi/error-text.ts +10 -2
- package/src/pi/failure-context.ts +24 -37
- package/src/pi/file-commit.ts +131 -164
- package/src/pi/file-read.ts +122 -31
- package/src/pi/fusion-arguments.ts +214 -0
- package/src/pi/fusion-card.ts +102 -60
- package/src/pi/grep-candidates.ts +35 -22
- package/src/pi/grep-output.ts +197 -190
- package/src/pi/grep-render.ts +22 -13
- package/src/pi/grep-reread.ts +7 -6
- package/src/pi/grep-scope.ts +31 -16
- package/src/pi/grep-search.ts +56 -47
- package/src/pi/grep-tool.ts +35 -33
- package/src/pi/grep-worker.ts +3 -4
- package/src/pi/mutation-diff.ts +238 -0
- package/src/pi/mutation-result.ts +102 -79
- package/src/pi/mutation-runner.ts +107 -84
- package/src/pi/native-bash.ts +41 -0
- package/src/pi/path.ts +59 -6
- package/src/pi/read-tool.ts +173 -138
- package/src/pi/regex-worker.ts +14 -8
- package/src/pi/render.ts +101 -54
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +14 -7
- package/src/pi/report-cause.ts +34 -17
- package/src/pi/report-schema.ts +184 -85
- package/src/pi/report.ts +133 -64
- package/src/pi/rg-process.ts +68 -48
- package/src/pi/schema.ts +4 -0
- package/src/pi/text-stream.ts +81 -87
- package/src/pi/workspace-cwd.js +2 -0
- package/src/pi/workspace.ts +46 -0
- package/src/pi/write-tool.ts +19 -13
- package/src/core/apply.test.ts +0 -644
- package/src/core/case-fold.test.ts +0 -35
- package/src/core/character-set.test.ts +0 -97
- package/src/core/errors.test.ts +0 -39
- package/src/core/hash.test.ts +0 -64
- package/src/core/lines.test.ts +0 -63
- package/src/core/ranges.test.ts +0 -123
- package/src/core/replace.test.ts +0 -187
- package/src/core/search-analysis.test.ts +0 -210
- package/src/core/search-matrix.test.ts +0 -64
- package/src/core/search-matrix.testing.ts +0 -171
- package/src/core/search.test.ts +0 -132
- package/src/core/text.test.ts +0 -52
- package/src/integration/config-registration.test.ts +0 -291
- package/src/integration/fusion-dispatch.test.ts +0 -320
- package/src/integration/grep-errors.test.ts +0 -81
- package/src/integration/grep-flags.test.ts +0 -76
- package/src/integration/grep-js-completeness.test.ts +0 -278
- package/src/integration/grep-line-candidates.test.ts +0 -201
- package/src/integration/grep-matrix.test.ts +0 -118
- package/src/integration/grep-order.test.ts +0 -78
- package/src/integration/grep-path-matrix.test.ts +0 -128
- package/src/integration/grep-prefilter-coverage.test.ts +0 -204
- package/src/integration/grep-process-limits.test.ts +0 -122
- package/src/integration/grep-regex.test.ts +0 -412
- package/src/integration/grep-rg.test.ts +0 -1659
- package/src/integration/grep-scan-budget.test.ts +0 -111
- package/src/integration/grep-translation.test.ts +0 -199
- package/src/integration/large-text.test.ts +0 -70
- package/src/integration/model-budget-lifecycle.test.ts +0 -186
- package/src/integration/mutation-file-types.test.ts +0 -30
- package/src/integration/mutation-file-types.testing.ts +0 -33
- package/src/integration/npm-package.test.ts +0 -19
- package/src/integration/npm-package.testing.ts +0 -243
- package/src/integration/publication-read-changes.test.ts +0 -454
- package/src/integration/read-changes.test.ts +0 -98
- package/src/integration/replace-package.test.ts +0 -22
- package/src/integration/replace-package.testing.ts +0 -36
- package/src/integration/replace-worker-boundary.test.ts +0 -127
- package/src/integration/report-replay.test.ts +0 -91
- package/src/integration/utf8-errors.test.ts +0 -46
- package/src/pi/action-fusion.test.ts +0 -659
- package/src/pi/action-fusion.testing.ts +0 -38
- package/src/pi/anchor-format.test.ts +0 -124
- package/src/pi/argument-error.testing.ts +0 -46
- package/src/pi/argument-format.test.ts +0 -196
- package/src/pi/argument-validation.test.ts +0 -1007
- package/src/pi/codemode.test.ts +0 -101
- package/src/pi/config.test.ts +0 -204
- package/src/pi/diagnostic-buffer.test.ts +0 -115
- package/src/pi/directory-sync.test.ts +0 -57
- package/src/pi/execute.test.ts +0 -1257
- package/src/pi/extension-loading.test.ts +0 -14
- package/src/pi/extension-loading.testing.ts +0 -3
- package/src/pi/failure-context.test.ts +0 -370
- package/src/pi/failure-path.test.ts +0 -289
- package/src/pi/file-commit.test.ts +0 -628
- package/src/pi/file-read.test.ts +0 -29
- package/src/pi/file-snapshot.test.ts +0 -22
- package/src/pi/fusion-card-interaction.test.ts +0 -143
- package/src/pi/fusion-card.test.ts +0 -353
- package/src/pi/grep-candidates.test.ts +0 -26
- package/src/pi/grep-matrix.test.ts +0 -68
- package/src/pi/grep-order.test.ts +0 -447
- package/src/pi/grep-output.testing.ts +0 -24
- package/src/pi/grep-rg.testing.ts +0 -73
- package/src/pi/grep.test.ts +0 -1670
- package/src/pi/model-budgets.test.ts +0 -96
- package/src/pi/mutation-outcome.testing.ts +0 -20
- package/src/pi/mutation-result.test.ts +0 -96
- package/src/pi/output-budgets.test.ts +0 -221
- package/src/pi/pi.test.ts +0 -158
- package/src/pi/read-delegation.test.ts +0 -119
- package/src/pi/read.test.ts +0 -735
- package/src/pi/regex-worker.test.ts +0 -42
- package/src/pi/registration.test.ts +0 -549
- package/src/pi/render.test.ts +0 -841
- package/src/pi/replace.test.ts +0 -634
- package/src/pi/report-failure.testing.ts +0 -27
- package/src/pi/report.test.ts +0 -533
- package/src/pi/result-safety.test.ts +0 -597
- package/src/pi/rg-process.test.ts +0 -133
- package/src/pi/rg-regex.test.ts +0 -40
- package/src/pi/text-stream.test.ts +0 -281
- package/src/pi/tool-call.testing.ts +0 -61
- package/src/pi/tool-factories.testing.ts +0 -59
- package/src/pi/write.test.ts +0 -256
- package/src/testing/extension-loader.testing.ts +0 -24
- package/src/testing/installation.testing.ts +0 -77
- package/src/testing/process.testing.ts +0 -33
- package/src/testing/regex-host.testing.ts +0 -20
- package/src/testing/regex-translation-corpus.ts +0 -267
- package/src/testing/rg-line-listing.fixture.mjs +0 -7
- package/src/testing/run-tests.ts +0 -47
- package/src/testing/session.testing.ts +0 -127
package/src/pi/execute.test.ts
DELETED
|
@@ -1,1257 +0,0 @@
|
|
|
1
|
-
import { commandCompletion, type SessionCommand } from "./action-fusion.testing.ts";
|
|
2
|
-
import { argumentError, rejectsArgument, rejectsArguments } from "./argument-error.testing.ts";
|
|
3
|
-
/** Tool-level edit and shared mutation workflow tests. */
|
|
4
|
-
import { test } from "node:test";
|
|
5
|
-
import assert from "node:assert/strict";
|
|
6
|
-
import { mkdtemp, readdir, rm, readFile, writeFile } from "node:fs/promises";
|
|
7
|
-
import { tmpdir } from "node:os";
|
|
8
|
-
import { join } from "node:path";
|
|
9
|
-
import { initTheme } from "@earendil-works/pi-coding-agent";
|
|
10
|
-
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
11
|
-
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
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";
|
|
17
|
-
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
18
|
-
import { computeLineHash } from "../core/hash.ts";
|
|
19
|
-
import { splitLines } from "../core/lines.ts";
|
|
20
|
-
import { callTool } from "./tool-call.testing.ts";
|
|
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";
|
|
24
|
-
import { detailsReport, reportOf } from "./report.ts";
|
|
25
|
-
import type { Report } from "./report-schema.ts";
|
|
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";
|
|
29
|
-
|
|
30
|
-
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
31
|
-
const dir = await mkdtemp(join(tmpdir(), "hl-"));
|
|
32
|
-
try {
|
|
33
|
-
return await fn(dir);
|
|
34
|
-
} finally {
|
|
35
|
-
await rm(dir, { recursive: true, force: true });
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Call a tool through Pi's argument preparation and schema validation, as production does. */
|
|
40
|
-
const call = (tool: any, params: any) => callTool(tool, params, { toolCallId: "0" });
|
|
41
|
-
|
|
42
|
-
/** Anchor a model would copy from read output for `line` of `text` (1-based). */
|
|
43
|
-
function h(text: string, line: number) {
|
|
44
|
-
return `${line}#${computeLineHash(line, splitLines(text)[line - 1], 4)}`;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Extract a `LINE#HASH` anchor from a read/edit result text block. */
|
|
48
|
-
function anchorLine(block: string, line: number) {
|
|
49
|
-
const m = new RegExp(`^${line}#([0-9A-Z]+)(?:│|$)`, "m").exec(block);
|
|
50
|
-
if (!m) throw new Error(`line ${line} anchor not found in block`);
|
|
51
|
-
return `${line}#${m[1]}`;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function failureReport(
|
|
55
|
-
result: AgentToolResult<unknown> & { isError?: boolean },
|
|
56
|
-
tool = "edit",
|
|
57
|
-
): Report {
|
|
58
|
-
assert.equal(result.isError, true);
|
|
59
|
-
assert.ok(
|
|
60
|
-
tool === "edit" || tool === "replace" || tool === "write" || tool === "read" || tool === "grep",
|
|
61
|
-
);
|
|
62
|
-
const report = detailsReport(result.details, tool);
|
|
63
|
-
assert.ok(report?.error);
|
|
64
|
-
return report;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
test("edit guidance → refreshes mutation anchors with an edit-only exception above changes", () => {
|
|
68
|
-
const guidance = makeEditOverride(process.cwd(), DEFAULT_CONFIG).promptGuidelines.join("\n");
|
|
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);
|
|
71
|
-
assert.match(
|
|
72
|
-
guidance,
|
|
73
|
-
/only after edit, lines above the earliest changed line keep their anchors/i,
|
|
74
|
-
);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test("edit execute: a file without a final newline stays byte-exact", async () => {
|
|
78
|
-
await withDir(async (dir) => {
|
|
79
|
-
const f = join(dir, "f.txt");
|
|
80
|
-
// The benchmark's literal-1-no-final-newline fixture: no terminator, plus a
|
|
81
|
-
// word-joiner the model cannot see. Only line 2 may change; the missing
|
|
82
|
-
// terminator must not turn into a new byte.
|
|
83
|
-
const text = "guard\nold value\u2060";
|
|
84
|
-
await writeFile(f, text);
|
|
85
|
-
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
86
|
-
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
87
|
-
path: "f.txt",
|
|
88
|
-
steps: [{ action: "replace", anchor: h(text, 2), lines: ["new value\u2060"] }],
|
|
89
|
-
});
|
|
90
|
-
assert.equal(r.isError, undefined, "should not be an error");
|
|
91
|
-
assert.equal(await readFile(f, "utf-8"), "guard\nnew value\u2060");
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test("edit execute: hashline round-trip (read → edit → file changed)", async () => {
|
|
96
|
-
await withDir(async (dir) => {
|
|
97
|
-
const f = join(dir, "f.txt");
|
|
98
|
-
const text = "a\nb\nc\n";
|
|
99
|
-
await writeFile(f, text);
|
|
100
|
-
const read = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
101
|
-
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
102
|
-
path: "f.txt",
|
|
103
|
-
steps: [{ action: "replace", anchor: anchorLine(read.content[0].text, 2), lines: ["B"] }],
|
|
104
|
-
});
|
|
105
|
-
assert.equal(r.isError, undefined, "should not be an error");
|
|
106
|
-
assert.equal(await readFile(f, "utf-8"), "a\nB\nc\n");
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
test("successful mutation → returns one report without anchor payload", async (t) => {
|
|
111
|
-
for (const tool of ["edit", "replace", "write"] as const) {
|
|
112
|
-
for (const count of [1, 3000]) {
|
|
113
|
-
await t.test(`${tool}: ${count} changed lines`, async () =>
|
|
114
|
-
withDir(async (dir) => {
|
|
115
|
-
const path = "f.txt";
|
|
116
|
-
const before = "old\n".repeat(count);
|
|
117
|
-
const after = "new\n".repeat(count);
|
|
118
|
-
await writeFile(join(dir, path), before);
|
|
119
|
-
const inspection = await callTool(makeReadOverride(dir, DEFAULT_CONFIG), {
|
|
120
|
-
path,
|
|
121
|
-
limit: count,
|
|
122
|
-
});
|
|
123
|
-
assert.equal(inspection.content[0].type, "text");
|
|
124
|
-
const rows = inspection.content[0].text;
|
|
125
|
-
const fixture =
|
|
126
|
-
tool === "edit"
|
|
127
|
-
? {
|
|
128
|
-
instance: makeEditOverride(dir, DEFAULT_CONFIG),
|
|
129
|
-
params: {
|
|
130
|
-
path,
|
|
131
|
-
steps: [
|
|
132
|
-
{
|
|
133
|
-
action: "replace",
|
|
134
|
-
anchor: anchorLine(rows, 1),
|
|
135
|
-
end: anchorLine(rows, count),
|
|
136
|
-
lines: Array.from({ length: count }, () => "new"),
|
|
137
|
-
},
|
|
138
|
-
],
|
|
139
|
-
},
|
|
140
|
-
}
|
|
141
|
-
: tool === "replace"
|
|
142
|
-
? {
|
|
143
|
-
instance: makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
144
|
-
params: {
|
|
145
|
-
path,
|
|
146
|
-
replacements: [{ literal: true, find: "old", replace: "new" }],
|
|
147
|
-
},
|
|
148
|
-
}
|
|
149
|
-
: {
|
|
150
|
-
instance: makeWriteOverride(dir),
|
|
151
|
-
params: { path, mode: "overwrite", content: after },
|
|
152
|
-
};
|
|
153
|
-
const result = await callTool(fixture.instance, fixture.params);
|
|
154
|
-
assert.equal(result.isError, undefined);
|
|
155
|
-
assert.equal(await readFile(join(dir, path), "utf8"), after);
|
|
156
|
-
assert.equal(result.content.length, 1);
|
|
157
|
-
assert.equal(result.content[0].type, "text");
|
|
158
|
-
assert.deepEqual(JSON.parse(result.content[0].text), {
|
|
159
|
-
publication: "PUBLISHED",
|
|
160
|
-
...(tool === "replace" ? { matches: count } : {}),
|
|
161
|
-
});
|
|
162
|
-
if (tool !== "write") {
|
|
163
|
-
assert.ok("diff" in result.details && typeof result.details.diff === "string");
|
|
164
|
-
assert.match(result.details.diff, /new/);
|
|
165
|
-
} else assert.deepEqual(result.details, { report: reportOf(result, "write") });
|
|
166
|
-
}),
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
test("edit on a line that changed externally → anchor mismatch", async () => {
|
|
173
|
-
await withDir(async (dir) => {
|
|
174
|
-
const f = join(dir, "f.txt");
|
|
175
|
-
const text = "a\nb\nc\n";
|
|
176
|
-
await writeFile(f, text);
|
|
177
|
-
await writeFile(f, "a\nBCHANGED\nc\n"); // line 2 changed
|
|
178
|
-
const report = failureReport(
|
|
179
|
-
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
180
|
-
path: "f.txt",
|
|
181
|
-
steps: [{ action: "replace", anchor: h(text, 2), lines: ["x"] }],
|
|
182
|
-
}),
|
|
183
|
-
);
|
|
184
|
-
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
185
|
-
assert.equal(await readFile(f, "utf8"), "a\nBCHANGED\nc\n");
|
|
186
|
-
});
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
test("unresolved observations require target confirmation before retrying a moved target", async () => {
|
|
190
|
-
await withDir(async (dir) => {
|
|
191
|
-
const file = join(dir, "recover.txt");
|
|
192
|
-
const observed =
|
|
193
|
-
["one", "two", "three", "four", "old marker", "six", "seven", "eight"].join("\n") + "\n";
|
|
194
|
-
await writeFile(
|
|
195
|
-
file,
|
|
196
|
-
["one", "two", "three", "four", "changed", "six", "new marker", "eight"].join("\n") + "\n",
|
|
197
|
-
);
|
|
198
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
199
|
-
const report = failureReport(
|
|
200
|
-
await call(edit, {
|
|
201
|
-
path: "recover.txt",
|
|
202
|
-
steps: [{ action: "replace", anchor: h(observed, 5), lines: ["updated"] }],
|
|
203
|
-
}),
|
|
204
|
-
);
|
|
205
|
-
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
206
|
-
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
207
|
-
assert.match(report.next ?? "", /confirm.*intended target.*before reusing/i);
|
|
208
|
-
assert.match(report.failures?.[0].observed ?? "", /^5#[0-9A-Z]+│changed$/);
|
|
209
|
-
const read = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "recover.txt" });
|
|
210
|
-
const retryAnchor = anchorLine(read.content[0].text, 7);
|
|
211
|
-
await call(edit, {
|
|
212
|
-
path: "recover.txt",
|
|
213
|
-
steps: [{ action: "replace", anchor: retryAnchor, lines: ["updated"] }],
|
|
214
|
-
});
|
|
215
|
-
assert.equal(
|
|
216
|
-
await readFile(file, "utf8"),
|
|
217
|
-
"one\ntwo\nthree\nfour\nchanged\nsix\nupdated\neight\n",
|
|
218
|
-
);
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
test("edit execute: delete op", async () => {
|
|
223
|
-
await withDir(async (dir) => {
|
|
224
|
-
const f = join(dir, "f.txt");
|
|
225
|
-
const text = "a\nb\nc\n";
|
|
226
|
-
await writeFile(f, text);
|
|
227
|
-
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
228
|
-
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
229
|
-
path: "f.txt",
|
|
230
|
-
steps: [{ action: "delete", anchor: h(text, 2) }],
|
|
231
|
-
});
|
|
232
|
-
assert.equal(r.isError, undefined);
|
|
233
|
-
assert.equal(await readFile(f, "utf-8"), "a\nc\n");
|
|
234
|
-
});
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
const stubTheme = { fg: (_k: string, s: string) => s, bold: (s: string) => s } as Theme;
|
|
238
|
-
|
|
239
|
-
// renderResult delegates to pi's renderDiff, which reads the global TUI theme
|
|
240
|
-
// singleton — initialize it once for this test process (watcher off by default).
|
|
241
|
-
initTheme();
|
|
242
|
-
|
|
243
|
-
test("native read and write renderers preserve resource titles, previews, and full errors", async () =>
|
|
244
|
-
withDir(async (dir) => {
|
|
245
|
-
const context = {
|
|
246
|
-
cwd: dir,
|
|
247
|
-
state: {},
|
|
248
|
-
argsComplete: true,
|
|
249
|
-
expanded: false,
|
|
250
|
-
isPartial: false,
|
|
251
|
-
lastComponent: undefined,
|
|
252
|
-
};
|
|
253
|
-
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
254
|
-
const readCall = read.renderCall!({ path: "SKILL.md", offset: 2, limit: 3 }, stubTheme, {
|
|
255
|
-
...context,
|
|
256
|
-
args: { path: "SKILL.md" },
|
|
257
|
-
} as Parameters<NonNullable<typeof read.renderCall>>[2]);
|
|
258
|
-
assert.match(readCall.render(120).join("\n"), /\[skill\]/);
|
|
259
|
-
const write = makeWriteOverride(dir);
|
|
260
|
-
const writeCall = write.renderCall(
|
|
261
|
-
{ path: "preview.txt", content: "native content preview\n", mode: "create" },
|
|
262
|
-
stubTheme,
|
|
263
|
-
context as Parameters<typeof write.renderCall>[2],
|
|
264
|
-
);
|
|
265
|
-
assert.match(writeCall.render(120).join("\n"), /native content preview/);
|
|
266
|
-
const error = write.renderResult(
|
|
267
|
-
{
|
|
268
|
-
content: [{ type: "text", text: "first error\nsecond error" }],
|
|
269
|
-
details: {},
|
|
270
|
-
},
|
|
271
|
-
{ isPartial: false, expanded: false },
|
|
272
|
-
stubTheme,
|
|
273
|
-
{ ...context, isError: true } as Parameters<typeof write.renderResult>[3],
|
|
274
|
-
);
|
|
275
|
-
assert.match(error.render(120).join("\n"), /first error[\s\S]*second error/);
|
|
276
|
-
}));
|
|
277
|
-
|
|
278
|
-
test("edit schema rejects misspelled range fields and invalid operation shapes", async () =>
|
|
279
|
-
withDir(async (dir) => {
|
|
280
|
-
const original = "a\nb\nc\n";
|
|
281
|
-
await writeFile(join(dir, "range.txt"), original);
|
|
282
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
283
|
-
const anchor = h(original, 2);
|
|
284
|
-
const callArgs = {
|
|
285
|
-
path: "range.txt",
|
|
286
|
-
steps: [{ action: "replace", anchor, endd: h(original, 3), lines: ["merged"] }],
|
|
287
|
-
};
|
|
288
|
-
await assert.rejects(call(edit, callArgs), rejectsArgument("steps[0].endd", /not allowed/));
|
|
289
|
-
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
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()],
|
|
296
|
-
] as const) {
|
|
297
|
-
await assert.rejects(call(edit, { path: "range.txt", steps }), expected);
|
|
298
|
-
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
299
|
-
}
|
|
300
|
-
const alternates: Parameters<typeof validateToolArguments>[1]["arguments"][] = [
|
|
301
|
-
{ path: "range.txt", action: "replace", anchor, lines: ["ok"] },
|
|
302
|
-
{ path: "range.txt", steps: JSON.stringify([{ action: "replace", anchor, lines: ["ok"] }]) },
|
|
303
|
-
];
|
|
304
|
-
for (const alternate of alternates) {
|
|
305
|
-
const before = structuredClone(alternate);
|
|
306
|
-
await assert.rejects(call(edit, alternate), rejectsArguments());
|
|
307
|
-
assert.deepEqual(alternate, before);
|
|
308
|
-
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
309
|
-
}
|
|
310
|
-
await call(edit, { path: "range.txt", steps: [{ action: "replace", anchor, lines: ["B"] }] });
|
|
311
|
-
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), "a\nB\nc\n");
|
|
312
|
-
}));
|
|
313
|
-
|
|
314
|
-
test("copied string anchors validate and replace an inclusive range", async () =>
|
|
315
|
-
withDir(async (dir) => {
|
|
316
|
-
const original = "a\nb\nc\nd\n";
|
|
317
|
-
await writeFile(join(dir, "range.txt"), original);
|
|
318
|
-
const read = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "range.txt" });
|
|
319
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
320
|
-
const args = validateToolArguments(edit as any, {
|
|
321
|
-
type: "toolCall",
|
|
322
|
-
id: "range",
|
|
323
|
-
name: "edit",
|
|
324
|
-
arguments: {
|
|
325
|
-
path: "range.txt",
|
|
326
|
-
steps: [
|
|
327
|
-
{
|
|
328
|
-
action: "replace",
|
|
329
|
-
anchor: anchorLine(read.content[0].text, 2),
|
|
330
|
-
end: anchorLine(read.content[0].text, 3),
|
|
331
|
-
lines: ["merged"],
|
|
332
|
-
},
|
|
333
|
-
],
|
|
334
|
-
},
|
|
335
|
-
});
|
|
336
|
-
await call(edit, args);
|
|
337
|
-
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), "a\nmerged\nd\n");
|
|
338
|
-
}));
|
|
339
|
-
|
|
340
|
-
test("invalid anchors and conflicting fields fail before changing the file", async () =>
|
|
341
|
-
withDir(async (dir) => {
|
|
342
|
-
const original = "a\nb\n";
|
|
343
|
-
await writeFile(join(dir, "invalid.txt"), original);
|
|
344
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
345
|
-
const anchor = h(original, 1);
|
|
346
|
-
const invalid = [
|
|
347
|
-
{ action: "replace", anchor: { line: 1, hash: anchor.split("#")[1] }, lines: ["changed"] },
|
|
348
|
-
...["0#ABCD", "-1#ABCD", "1.5#ABCD", "1#", "1#abcd", "1#ABCD│a", "9007199254740993#ABCD"].map(
|
|
349
|
-
(anchor) => ({ action: "replace", anchor, lines: ["changed"] }),
|
|
350
|
-
),
|
|
351
|
-
{ action: "insert_after", anchor, end: anchor, lines: ["changed"] },
|
|
352
|
-
{ action: "append", anchor, lines: ["changed"] },
|
|
353
|
-
{ action: "delete", anchor, lines: ["changed"] },
|
|
354
|
-
];
|
|
355
|
-
for (const operation of invalid) {
|
|
356
|
-
await assert.rejects(
|
|
357
|
-
call(edit, { path: "invalid.txt", steps: [operation] }),
|
|
358
|
-
/INVALID_ARGUMENTS|Invalid anchor/,
|
|
359
|
-
);
|
|
360
|
-
assert.equal(await readFile(join(dir, "invalid.txt"), "utf8"), original);
|
|
361
|
-
}
|
|
362
|
-
}));
|
|
363
|
-
|
|
364
|
-
test("edit rejects empty bodies before file access for arrays and Pi-converted objects", async () =>
|
|
365
|
-
withDir(async (dir) => {
|
|
366
|
-
const original = "a\nb\nc\n";
|
|
367
|
-
const file = join(dir, "empty_body.txt");
|
|
368
|
-
await writeFile(file, original);
|
|
369
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
370
|
-
const anchor = h(original, 1);
|
|
371
|
-
for (const operation of [
|
|
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: [] },
|
|
377
|
-
]) {
|
|
378
|
-
for (const path of [file, join(dir, "missing.txt")]) {
|
|
379
|
-
for (const steps of [[operation], operation]) {
|
|
380
|
-
await assert.rejects(
|
|
381
|
-
call(edit, { path, steps }),
|
|
382
|
-
rejectsArgument("steps[0].lines", /empty/),
|
|
383
|
-
);
|
|
384
|
-
assert.equal(await readFile(file, "utf8"), original);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
}));
|
|
389
|
-
|
|
390
|
-
test("mixed empty-lines batches name every rejected edit without publishing or running commands", async () =>
|
|
391
|
-
withDir(async (dir) => {
|
|
392
|
-
const file = join(dir, "batch.txt");
|
|
393
|
-
const original = "a\nb\nc\nd\ne\n";
|
|
394
|
-
await writeFile(file, original);
|
|
395
|
-
let commandRuns = 0;
|
|
396
|
-
const executeTool: SessionCommand = async () => {
|
|
397
|
-
commandRuns++;
|
|
398
|
-
return commandCompletion({ content: [{ type: "text", text: "ran" }] });
|
|
399
|
-
};
|
|
400
|
-
const fusion = createActionFusionExecutor(() => {});
|
|
401
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
|
|
402
|
-
await assert.rejects(
|
|
403
|
-
callTool(
|
|
404
|
-
edit,
|
|
405
|
-
{
|
|
406
|
-
path: file,
|
|
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: [] },
|
|
412
|
-
],
|
|
413
|
-
then_run: { command: "must-not-run" },
|
|
414
|
-
},
|
|
415
|
-
{ ctx: { ...{ cwd: dir }, executeTool } },
|
|
416
|
-
),
|
|
417
|
-
(error: Error) => {
|
|
418
|
-
const issues = argumentError(error).issues;
|
|
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/);
|
|
421
|
-
return true;
|
|
422
|
-
},
|
|
423
|
-
);
|
|
424
|
-
assert.equal(await readFile(file, "utf8"), original);
|
|
425
|
-
assert.equal(commandRuns, 0);
|
|
426
|
-
}));
|
|
427
|
-
|
|
428
|
-
test("blank edit lines preserve a real blank line for every line-bearing operation", async () =>
|
|
429
|
-
withDir(async (dir) => {
|
|
430
|
-
const file = join(dir, "blank_line.txt");
|
|
431
|
-
const original = "a\nb\nc\n";
|
|
432
|
-
const anchor = h(original, 1);
|
|
433
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
434
|
-
for (const [operation, expected] of [
|
|
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"],
|
|
440
|
-
] as const) {
|
|
441
|
-
await writeFile(file, original);
|
|
442
|
-
await call(edit, { path: file, steps: [operation] });
|
|
443
|
-
assert.equal(await readFile(file, "utf8"), expected);
|
|
444
|
-
}
|
|
445
|
-
}));
|
|
446
|
-
|
|
447
|
-
test("explicit delete → removes an inclusive range", async () =>
|
|
448
|
-
withDir(async (dir) => {
|
|
449
|
-
const original = "a\nb\nc\n";
|
|
450
|
-
const file = join(dir, "delete_range.txt");
|
|
451
|
-
await writeFile(file, original);
|
|
452
|
-
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
453
|
-
path: file,
|
|
454
|
-
steps: [{ action: "delete", anchor: h(original, 1), end: h(original, 2) }],
|
|
455
|
-
});
|
|
456
|
-
assert.equal(await readFile(file, "utf8"), "c\n");
|
|
457
|
-
}));
|
|
458
|
-
|
|
459
|
-
test("local and full-file recovery return copyable anchors without changing the rejected batch", async () =>
|
|
460
|
-
withDir(async (dir) => {
|
|
461
|
-
const original = "a\nb\n";
|
|
462
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
463
|
-
for (const prefixLines of [1, 40]) {
|
|
464
|
-
const prefix = "prefix\n".repeat(prefixLines);
|
|
465
|
-
await writeFile(join(dir, "shift.txt"), prefix + original);
|
|
466
|
-
let replacement = "";
|
|
467
|
-
const report = failureReport(
|
|
468
|
-
await call(edit, {
|
|
469
|
-
path: "shift.txt",
|
|
470
|
-
steps: [{ action: "replace", anchor: h(original, 2), lines: ["B"] }],
|
|
471
|
-
}),
|
|
472
|
-
);
|
|
473
|
-
const failure = report.failures?.[0];
|
|
474
|
-
assert.ok(failure);
|
|
475
|
-
replacement = failure.candidate ?? "";
|
|
476
|
-
assert.equal(replacement, h(prefix + original, prefixLines + 2));
|
|
477
|
-
assert.equal(failure.content, "b");
|
|
478
|
-
assert.match(report.next ?? "", /confirm.*intended target/i);
|
|
479
|
-
assert.equal(report.candidateNeighborhoods, undefined);
|
|
480
|
-
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), prefix + original);
|
|
481
|
-
await call(edit, {
|
|
482
|
-
path: "shift.txt",
|
|
483
|
-
steps: [{ action: "replace", anchor: replacement, lines: ["B"] }],
|
|
484
|
-
});
|
|
485
|
-
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), prefix + "a\nB\n");
|
|
486
|
-
}
|
|
487
|
-
}));
|
|
488
|
-
|
|
489
|
-
test("shifted-anchor recovery keeps the read fallback for oversized candidates", async () =>
|
|
490
|
-
withDir(async (dir) => {
|
|
491
|
-
const oversized = "x".repeat(DEFAULT_OUTPUT_BUDGETS.fieldBytes);
|
|
492
|
-
const original = `a\n${oversized}\n`;
|
|
493
|
-
await writeFile(join(dir, "shift-large.txt"), `prefix\n${original}`);
|
|
494
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
495
|
-
const report = failureReport(
|
|
496
|
-
await call(edit, {
|
|
497
|
-
path: "shift-large.txt",
|
|
498
|
-
steps: [{ action: "replace", anchor: h(original, 2), lines: ["B"] }],
|
|
499
|
-
}),
|
|
500
|
-
);
|
|
501
|
-
assert.match(
|
|
502
|
-
report.failures?.[0].notShown ?? "",
|
|
503
|
-
new RegExp(`exceeds ${formatByteSize(DEFAULT_OUTPUT_BUDGETS.fieldBytes)}`),
|
|
504
|
-
);
|
|
505
|
-
assert.match(report.next ?? "", /use read or grep/);
|
|
506
|
-
assert.equal(report.failures?.[0].content, undefined);
|
|
507
|
-
}));
|
|
508
|
-
|
|
509
|
-
test("failed commands preserve mutation results and stay out of all main card renderers", async () =>
|
|
510
|
-
withDir(async (dir) => {
|
|
511
|
-
const commands: string[] = [];
|
|
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));
|
|
521
|
-
const cases = [
|
|
522
|
-
{
|
|
523
|
-
path: "edit.txt",
|
|
524
|
-
run: async () => {
|
|
525
|
-
const tool = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
|
|
526
|
-
const args = {
|
|
527
|
-
path: "edit.txt",
|
|
528
|
-
steps: [{ action: "append" as const, lines: ["after"] }],
|
|
529
|
-
};
|
|
530
|
-
const result = await callTool(
|
|
531
|
-
tool,
|
|
532
|
-
{ ...args, then_run: { command: "check" } },
|
|
533
|
-
{ toolCallId: args.path, ctx: { ...{ cwd: dir }, executeTool } },
|
|
534
|
-
);
|
|
535
|
-
return {
|
|
536
|
-
result,
|
|
537
|
-
tool: tool.name,
|
|
538
|
-
rendered: tool
|
|
539
|
-
.renderResult(
|
|
540
|
-
result,
|
|
541
|
-
{ expanded: true, isPartial: false },
|
|
542
|
-
stubTheme as Theme,
|
|
543
|
-
{ args, state: {}, cwd: dir, isError: false } as Parameters<
|
|
544
|
-
typeof tool.renderResult
|
|
545
|
-
>[3],
|
|
546
|
-
)
|
|
547
|
-
.render(120)
|
|
548
|
-
.join("\n"),
|
|
549
|
-
};
|
|
550
|
-
},
|
|
551
|
-
expected: "before\nafter\n",
|
|
552
|
-
},
|
|
553
|
-
{
|
|
554
|
-
path: "replace.txt",
|
|
555
|
-
run: async () => {
|
|
556
|
-
const tool = makeReplaceTool(dir, DEFAULT_CONFIG, fusion);
|
|
557
|
-
const args = {
|
|
558
|
-
path: "replace.txt",
|
|
559
|
-
replacements: [{ literal: true, find: "before", replace: "after" }],
|
|
560
|
-
};
|
|
561
|
-
const result = await callTool(
|
|
562
|
-
tool,
|
|
563
|
-
{ ...args, then_run: { command: "check" } },
|
|
564
|
-
{ toolCallId: args.path, ctx: { ...{ cwd: dir }, executeTool } },
|
|
565
|
-
);
|
|
566
|
-
return {
|
|
567
|
-
result,
|
|
568
|
-
tool: tool.name,
|
|
569
|
-
rendered: tool
|
|
570
|
-
.renderResult(
|
|
571
|
-
result,
|
|
572
|
-
{ expanded: true, isPartial: false },
|
|
573
|
-
stubTheme as Theme,
|
|
574
|
-
{ args, state: {}, cwd: dir, isError: false } as Parameters<
|
|
575
|
-
typeof tool.renderResult
|
|
576
|
-
>[3],
|
|
577
|
-
)
|
|
578
|
-
.render(120)
|
|
579
|
-
.join("\n"),
|
|
580
|
-
};
|
|
581
|
-
},
|
|
582
|
-
expected: "after\n",
|
|
583
|
-
},
|
|
584
|
-
{
|
|
585
|
-
path: "write.txt",
|
|
586
|
-
run: async () => {
|
|
587
|
-
const tool = makeWriteOverride(dir, fusion);
|
|
588
|
-
const args = { path: "write.txt", content: "after\n", mode: "overwrite" as const };
|
|
589
|
-
const result = await callTool(
|
|
590
|
-
tool,
|
|
591
|
-
{ ...args, then_run: { command: "check" } },
|
|
592
|
-
{ toolCallId: args.path, ctx: { ...{ cwd: dir }, executeTool } },
|
|
593
|
-
);
|
|
594
|
-
return {
|
|
595
|
-
result,
|
|
596
|
-
tool: tool.name,
|
|
597
|
-
rendered: tool
|
|
598
|
-
.renderResult(result, { expanded: true, isPartial: false }, stubTheme as Theme, {
|
|
599
|
-
args,
|
|
600
|
-
state: {},
|
|
601
|
-
cwd: dir,
|
|
602
|
-
toolCallId: args.path,
|
|
603
|
-
invalidate() {},
|
|
604
|
-
lastComponent: undefined,
|
|
605
|
-
executionStarted: true,
|
|
606
|
-
durationMs: undefined,
|
|
607
|
-
outputPad: 1,
|
|
608
|
-
keyText: () => "",
|
|
609
|
-
keyHint: (_key, description) => description,
|
|
610
|
-
argsComplete: true,
|
|
611
|
-
isPartial: false,
|
|
612
|
-
expanded: true,
|
|
613
|
-
showImages: false,
|
|
614
|
-
isError: false,
|
|
615
|
-
})
|
|
616
|
-
.render(120)
|
|
617
|
-
.join("\n"),
|
|
618
|
-
};
|
|
619
|
-
},
|
|
620
|
-
expected: "after\n",
|
|
621
|
-
},
|
|
622
|
-
];
|
|
623
|
-
for (const { path, run, expected } of cases) {
|
|
624
|
-
await writeFile(join(dir, path), "before\n");
|
|
625
|
-
const { result, rendered, tool } = await run();
|
|
626
|
-
assert.equal("isError" in result, false);
|
|
627
|
-
assert.ok(
|
|
628
|
-
tool === "edit" ||
|
|
629
|
-
tool === "replace" ||
|
|
630
|
-
tool === "write" ||
|
|
631
|
-
tool === "read" ||
|
|
632
|
-
tool === "grep",
|
|
633
|
-
);
|
|
634
|
-
const report = detailsReport(result.details, tool);
|
|
635
|
-
assert.equal(report?.publication, "PUBLISHED");
|
|
636
|
-
const last = result.content.at(-1);
|
|
637
|
-
assert.ok(last?.type === "text");
|
|
638
|
-
assert.equal(report?.then_run?.status, "failed");
|
|
639
|
-
assert.match(report?.then_run?.output ?? "", /command-only diagnostic/);
|
|
640
|
-
if (path !== "write.txt")
|
|
641
|
-
assert.equal(typeof ("diff" in result.details ? result.details.diff : undefined), "string");
|
|
642
|
-
assert.doesNotMatch(rendered, /command-only diagnostic|then_run:failed/);
|
|
643
|
-
assert.equal(await readFile(join(dir, path), "utf8"), expected);
|
|
644
|
-
}
|
|
645
|
-
assert.deepEqual(
|
|
646
|
-
commands,
|
|
647
|
-
cases.flatMap(() => ["waiting", "waiting", "running", "failed"]),
|
|
648
|
-
);
|
|
649
|
-
}));
|
|
650
|
-
|
|
651
|
-
test("missing or directory targets → tools share path diagnostics without filesystem changes", async (t) => {
|
|
652
|
-
await withDir(async (dir) => {
|
|
653
|
-
const before = await readdir(dir);
|
|
654
|
-
for (const target of [
|
|
655
|
-
{ name: "missing", path: "./missing.txt", code: "PATH_NOT_FOUND" },
|
|
656
|
-
{ name: "directory", path: ".", code: "NOT_REGULAR_FILE" },
|
|
657
|
-
] as const) {
|
|
658
|
-
const path = target.path;
|
|
659
|
-
const cases = [
|
|
660
|
-
{ tool: makeReadOverride(dir, DEFAULT_CONFIG), args: { path } },
|
|
661
|
-
{
|
|
662
|
-
tool: makeEditOverride(dir, DEFAULT_CONFIG),
|
|
663
|
-
args: { path, steps: [{ action: "append", lines: ["changed"] }] },
|
|
664
|
-
},
|
|
665
|
-
{
|
|
666
|
-
tool: makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
667
|
-
args: { path, replacements: [{ literal: true, find: "old", replace: "changed" }] },
|
|
668
|
-
},
|
|
669
|
-
{ tool: makeWriteOverride(dir), args: { path, content: "changed", mode: "overwrite" } },
|
|
670
|
-
...(target.code === "PATH_NOT_FOUND"
|
|
671
|
-
? [
|
|
672
|
-
{
|
|
673
|
-
tool: makeGrepOverride(dir, DEFAULT_CONFIG),
|
|
674
|
-
args: { path: [path], pattern: ["old"], literal: true },
|
|
675
|
-
},
|
|
676
|
-
]
|
|
677
|
-
: []),
|
|
678
|
-
];
|
|
679
|
-
for (const { tool, args } of cases) {
|
|
680
|
-
await t.test(`${tool.name}/${target.name}`, async () => {
|
|
681
|
-
const report = failureReport(await callTool(tool, args), tool.name);
|
|
682
|
-
assert.equal(report.error, target.code);
|
|
683
|
-
assert.equal(report.message, PATH_ERROR_MESSAGES[target.code]);
|
|
684
|
-
if (target.code === "PATH_NOT_FOUND") {
|
|
685
|
-
assert.deepEqual(report.missing, [join(dir, "missing.txt")]);
|
|
686
|
-
assert.equal(
|
|
687
|
-
report.next,
|
|
688
|
-
ERROR_DEFINITIONS.PATH_NOT_FOUND.next[
|
|
689
|
-
tool.name === "write" ? "useCreate" : "default"
|
|
690
|
-
],
|
|
691
|
-
);
|
|
692
|
-
} else {
|
|
693
|
-
assert.equal(report.missing, undefined);
|
|
694
|
-
assert.equal(report.next, ERROR_DEFINITIONS.NOT_REGULAR_FILE.next.default);
|
|
695
|
-
}
|
|
696
|
-
assert.deepEqual(await readdir(dir), before);
|
|
697
|
-
});
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
});
|
|
701
|
-
});
|
|
702
|
-
|
|
703
|
-
test("text tools reject malformed UTF-8 and NUL bytes without rewriting source bytes", async () =>
|
|
704
|
-
withDir(async (dir) => {
|
|
705
|
-
const target = join(dir, "invalid-utf8.txt");
|
|
706
|
-
const original = Buffer.from([0x61, 0x0a, 0xc3, 0x28, 0x0a]);
|
|
707
|
-
await writeFile(target, original);
|
|
708
|
-
for (const [tool, args] of [
|
|
709
|
-
[makeReadOverride(dir, DEFAULT_CONFIG), { path: "invalid-utf8.txt" }],
|
|
710
|
-
[
|
|
711
|
-
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
712
|
-
{ path: "invalid-utf8.txt", steps: [{ action: "append", lines: ["x"] }] },
|
|
713
|
-
],
|
|
714
|
-
[
|
|
715
|
-
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
716
|
-
{ path: "invalid-utf8.txt", replacements: [{ literal: true, find: "a", replace: "b" }] },
|
|
717
|
-
],
|
|
718
|
-
] as const) {
|
|
719
|
-
assert.equal(failureReport(await call(tool, args), tool.name).error, "UNSUPPORTED_ENCODING");
|
|
720
|
-
}
|
|
721
|
-
assert.deepEqual(await readFile(target), original);
|
|
722
|
-
|
|
723
|
-
const nulTarget = join(dir, "nul.txt");
|
|
724
|
-
const nulOriginal = Buffer.from([0x61, 0x00, 0x62]);
|
|
725
|
-
await writeFile(nulTarget, nulOriginal);
|
|
726
|
-
for (const [tool, args] of [
|
|
727
|
-
[
|
|
728
|
-
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
729
|
-
{ path: "nul.txt", steps: [{ action: "append", lines: ["x"] }] },
|
|
730
|
-
],
|
|
731
|
-
[
|
|
732
|
-
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
733
|
-
{ path: "nul.txt", replacements: [{ literal: true, find: "a", replace: "b" }] },
|
|
734
|
-
],
|
|
735
|
-
] as const) {
|
|
736
|
-
assert.equal(failureReport(await call(tool, args), tool.name).error, "UNSUPPORTED_TEXT");
|
|
737
|
-
}
|
|
738
|
-
assert.deepEqual(await readFile(nulTarget), nulOriginal);
|
|
739
|
-
|
|
740
|
-
const nativeRead: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "nul.txt" });
|
|
741
|
-
assert.equal(nativeRead.content[0].text, "a\0b");
|
|
742
|
-
|
|
743
|
-
const utf16Target = join(dir, "utf16.txt");
|
|
744
|
-
await writeFile(utf16Target, Buffer.from([0xff, 0xfe, 0x49, 0x6c]));
|
|
745
|
-
assert.equal(
|
|
746
|
-
failureReport(
|
|
747
|
-
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "utf16.txt" }),
|
|
748
|
-
"read",
|
|
749
|
-
).error,
|
|
750
|
-
"UNSUPPORTED_ENCODING",
|
|
751
|
-
);
|
|
752
|
-
}));
|
|
753
|
-
|
|
754
|
-
test("edit rejects embedded line terminators at its schema boundary", async () =>
|
|
755
|
-
withDir(async (dir) => {
|
|
756
|
-
const target = join(dir, "body.txt");
|
|
757
|
-
await writeFile(target, "a\n");
|
|
758
|
-
for (const line of ["x\ny", "x\ry"]) {
|
|
759
|
-
await assert.rejects(
|
|
760
|
-
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
761
|
-
path: "body.txt",
|
|
762
|
-
steps: [{ action: "append", lines: [line] }],
|
|
763
|
-
}),
|
|
764
|
-
rejectsArguments(),
|
|
765
|
-
);
|
|
766
|
-
assert.equal(await readFile(target, "utf8"), "a\n");
|
|
767
|
-
}
|
|
768
|
-
}));
|
|
769
|
-
|
|
770
|
-
test("edit rejects unwritable body lines before reading a missing target", async () =>
|
|
771
|
-
withDir(async (dir) => {
|
|
772
|
-
const target = join(dir, "missing.txt");
|
|
773
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
774
|
-
const valid = { path: target, steps: [{ action: "append" as const, lines: ["ok"] }] };
|
|
775
|
-
assert.equal(edit.prepareArguments(valid), valid);
|
|
776
|
-
for (const [line, expected] of [
|
|
777
|
-
["bad\0", rejectsArgument("steps[0].lines[1]", /NUL/)],
|
|
778
|
-
["\ud800", rejectsArgument("steps[0].lines[1]", /losslessly.*UTF-8/)],
|
|
779
|
-
] as const) {
|
|
780
|
-
await assert.rejects(
|
|
781
|
-
call(edit, { path: target, steps: [{ action: "append", lines: ["ok", line] }] }),
|
|
782
|
-
expected,
|
|
783
|
-
);
|
|
784
|
-
await assert.rejects(readFile(target, "utf8"), { code: "ENOENT" });
|
|
785
|
-
}
|
|
786
|
-
}));
|
|
787
|
-
|
|
788
|
-
test("editing a BOM-prefixed file → publishes the changed bytes with the BOM intact", async () =>
|
|
789
|
-
withDir(async (dir) => {
|
|
790
|
-
const target = join(dir, "bom.txt");
|
|
791
|
-
const source = "\ufeffguard\nold\n";
|
|
792
|
-
const original = Buffer.from(source, "utf8");
|
|
793
|
-
for (const line of [1, 2]) {
|
|
794
|
-
await writeFile(target, original);
|
|
795
|
-
const result = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
796
|
-
path: "bom.txt",
|
|
797
|
-
steps: [{ action: "replace", anchor: h(source, line), lines: ["new"] }],
|
|
798
|
-
});
|
|
799
|
-
const expected = Buffer.from(line === 1 ? "\ufeffnew\nold\n" : "\ufeffguard\nnew\n", "utf8");
|
|
800
|
-
assert.deepEqual(await readFile(target), expected);
|
|
801
|
-
assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
|
|
802
|
-
assert.equal(reportOf(result, "edit")?.freshness, undefined);
|
|
803
|
-
}
|
|
804
|
-
}));
|
|
805
|
-
|
|
806
|
-
test("edit and replace reject NUL arguments without rewriting source bytes", async () =>
|
|
807
|
-
withDir(async (dir) => {
|
|
808
|
-
const target = join(dir, "output.txt");
|
|
809
|
-
const original = Buffer.from("\ufeffbefore\r\n", "utf8");
|
|
810
|
-
await writeFile(target, original);
|
|
811
|
-
await assert.rejects(
|
|
812
|
-
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
813
|
-
path: "output.txt",
|
|
814
|
-
steps: [{ action: "append", lines: ["bad\0text"] }],
|
|
815
|
-
}),
|
|
816
|
-
rejectsArgument("steps[0].lines[0]", /NUL/),
|
|
817
|
-
);
|
|
818
|
-
assert.deepEqual(await readFile(target), original);
|
|
819
|
-
await assert.rejects(
|
|
820
|
-
call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
821
|
-
path: "output.txt",
|
|
822
|
-
replacements: [{ literal: true, find: "before", replace: "bad\0text" }],
|
|
823
|
-
}),
|
|
824
|
-
rejectsArgument("replacements[0].replace", /NUL/),
|
|
825
|
-
);
|
|
826
|
-
assert.deepEqual(await readFile(target), original);
|
|
827
|
-
}));
|
|
828
|
-
|
|
829
|
-
test("failed steps return candidate code for a verified fused retry", async () =>
|
|
830
|
-
withDir(async (dir) => {
|
|
831
|
-
const original = Array.from({ length: 14 }, (_, index) => `line-${index + 1}`);
|
|
832
|
-
original[4] = "if (!fusion) throw new Error();";
|
|
833
|
-
original[5] = "const absolutePath = canonicalPath(cwd, path);";
|
|
834
|
-
const before = `prefix\n${original.join("\n")}\n`;
|
|
835
|
-
const file = join(dir, "candidate.txt");
|
|
836
|
-
await writeFile(file, before);
|
|
837
|
-
let commands = 0;
|
|
838
|
-
const executeTool: SessionCommand = async () => {
|
|
839
|
-
commands++;
|
|
840
|
-
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
841
|
-
};
|
|
842
|
-
const fusion = createActionFusionExecutor(() => {});
|
|
843
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
|
|
844
|
-
const stale = h(original.join("\n"), 5);
|
|
845
|
-
const stable = h(before, 12);
|
|
846
|
-
const candidate = h(before, 6);
|
|
847
|
-
const report = failureReport(
|
|
848
|
-
await callTool(
|
|
849
|
-
edit,
|
|
850
|
-
{
|
|
851
|
-
path: file,
|
|
852
|
-
steps: [
|
|
853
|
-
{ action: "insert_after", anchor: stale, lines: ["const hashLen = 4;"] },
|
|
854
|
-
{ action: "replace", anchor: stable, lines: ["changed"] },
|
|
855
|
-
],
|
|
856
|
-
then_run: { command: "check" },
|
|
857
|
-
},
|
|
858
|
-
{ toolCallId: "failed", ctx: { ...{ cwd: dir }, executeTool } },
|
|
859
|
-
),
|
|
860
|
-
);
|
|
861
|
-
assert.equal(report.then_run?.status, "skipped");
|
|
862
|
-
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
863
|
-
assert.equal(report.failures?.[0].field, "steps[0].anchor");
|
|
864
|
-
assert.equal(report.failures?.[0].cited, stale);
|
|
865
|
-
assert.equal(report.failures?.[0].candidate, candidate);
|
|
866
|
-
assert.equal(report.failures?.[0].content, original[4]);
|
|
867
|
-
assert.equal(report.candidateNeighborhoods, undefined);
|
|
868
|
-
assert.equal(report.omittedFailures, undefined);
|
|
869
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
870
|
-
assert.equal(commands, 0);
|
|
871
|
-
await callTool(
|
|
872
|
-
edit,
|
|
873
|
-
{
|
|
874
|
-
path: file,
|
|
875
|
-
steps: [
|
|
876
|
-
{
|
|
877
|
-
action: "insert_after",
|
|
878
|
-
anchor: report.failures![0].candidate!,
|
|
879
|
-
lines: ["const hashLen = 4;"],
|
|
880
|
-
},
|
|
881
|
-
{ action: "replace", anchor: stable, lines: ["changed"] },
|
|
882
|
-
],
|
|
883
|
-
then_run: { command: "check" },
|
|
884
|
-
},
|
|
885
|
-
{ toolCallId: "retry", ctx: { ...{ cwd: dir }, executeTool } },
|
|
886
|
-
);
|
|
887
|
-
const expected = splitLines(before);
|
|
888
|
-
expected[11] = "changed";
|
|
889
|
-
expected.splice(6, 0, "const hashLen = 4;");
|
|
890
|
-
assert.equal(await readFile(file, "utf8"), `${expected.join("\n")}\n`);
|
|
891
|
-
assert.equal(commands, 1);
|
|
892
|
-
}));
|
|
893
|
-
|
|
894
|
-
test("ambiguous candidates include distinguishing neighborhoods for a verified retry", async () =>
|
|
895
|
-
withDir(async (dir) => {
|
|
896
|
-
const lines = Array.from({ length: 20 }, (_, index) => `line-${index + 1}`);
|
|
897
|
-
lines[3] = "function primary() {";
|
|
898
|
-
lines[4] = " return value;";
|
|
899
|
-
lines[13] = "function secondary() {";
|
|
900
|
-
lines[14] = " return value;";
|
|
901
|
-
const before = lines.join("\n") + "\n";
|
|
902
|
-
const file = join(dir, "ambiguous.txt");
|
|
903
|
-
await writeFile(file, before);
|
|
904
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
905
|
-
const report = failureReport(
|
|
906
|
-
await call(edit, {
|
|
907
|
-
path: file,
|
|
908
|
-
steps: [
|
|
909
|
-
{
|
|
910
|
-
action: "replace",
|
|
911
|
-
anchor: h("header\n return value;\n", 2),
|
|
912
|
-
lines: [" return updated;"],
|
|
913
|
-
},
|
|
914
|
-
],
|
|
915
|
-
}),
|
|
916
|
-
);
|
|
917
|
-
assert.equal(report.failures?.[0].result, "ambiguous");
|
|
918
|
-
assert.deepEqual(report.failures?.[0].candidates, [h(before, 5), h(before, 15)]);
|
|
919
|
-
assert.deepEqual(
|
|
920
|
-
report.candidateNeighborhoods?.map((group) => group.lines),
|
|
921
|
-
["2-8", "12-18"],
|
|
922
|
-
);
|
|
923
|
-
const context = report.candidateNeighborhoods?.flatMap((group) => group.rows).join("\n") ?? "";
|
|
924
|
-
assert.ok(context.includes(`${h(before, 4)}│function primary() {`));
|
|
925
|
-
assert.ok(context.includes(`${h(before, 14)}│function secondary() {`));
|
|
926
|
-
assert.equal(anchorLine(context, 5), h(before, 5));
|
|
927
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
928
|
-
await call(edit, {
|
|
929
|
-
path: file,
|
|
930
|
-
steps: [{ action: "replace", anchor: anchorLine(context, 15), lines: [" return updated;"] }],
|
|
931
|
-
});
|
|
932
|
-
lines[14] = " return updated;";
|
|
933
|
-
assert.equal(await readFile(file, "utf8"), lines.join("\n") + "\n");
|
|
934
|
-
}));
|
|
935
|
-
|
|
936
|
-
test("schema-invalid bodies reject before anchor verification; retries revalidate", async () =>
|
|
937
|
-
withDir(async (dir) => {
|
|
938
|
-
const before = "a\nb\nc\n";
|
|
939
|
-
const file = join(dir, "checks.txt");
|
|
940
|
-
await writeFile(file, before);
|
|
941
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
942
|
-
const stable = h(before, 1);
|
|
943
|
-
for (const operation of [
|
|
944
|
-
{ action: "replace", anchor: stable, lines: ["bad\nline"] },
|
|
945
|
-
{ action: "append", lines: ["bad\nline"] },
|
|
946
|
-
]) {
|
|
947
|
-
for (const laterAnchor of [h(before, 2), "2#XXXX"]) {
|
|
948
|
-
await assert.rejects(
|
|
949
|
-
call(edit, {
|
|
950
|
-
path: file,
|
|
951
|
-
steps: [operation, { action: "replace", anchor: laterAnchor, lines: ["B"] }],
|
|
952
|
-
}),
|
|
953
|
-
(error: Error) => {
|
|
954
|
-
rejectsArguments()(error);
|
|
955
|
-
const report = argumentError(error);
|
|
956
|
-
assert.equal(report.failures, undefined);
|
|
957
|
-
return true;
|
|
958
|
-
},
|
|
959
|
-
);
|
|
960
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
const rejected = failureReport(
|
|
964
|
-
await call(edit, {
|
|
965
|
-
path: file,
|
|
966
|
-
steps: [
|
|
967
|
-
{ action: "replace", anchor: stable, lines: ["A"] },
|
|
968
|
-
{ action: "delete", anchor: h(before, 2), end: "3#ZZZZ" },
|
|
969
|
-
],
|
|
970
|
-
}),
|
|
971
|
-
);
|
|
972
|
-
assert.equal(rejected.failures?.[0].field, "steps[1].end");
|
|
973
|
-
assert.equal(rejected.failures?.[0].cited, "3#ZZZZ");
|
|
974
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
975
|
-
await writeFile(file, "changed\nb\nc\n");
|
|
976
|
-
const retry = failureReport(
|
|
977
|
-
await call(edit, {
|
|
978
|
-
path: file,
|
|
979
|
-
steps: [
|
|
980
|
-
{ action: "replace", anchor: stable, lines: ["A"] },
|
|
981
|
-
{ action: "replace", anchor: "3#ZZZZ", lines: ["B"] },
|
|
982
|
-
],
|
|
983
|
-
}),
|
|
984
|
-
);
|
|
985
|
-
assert.deepEqual(
|
|
986
|
-
retry.failures?.map(({ field, cited }) => ({ field, cited })),
|
|
987
|
-
[
|
|
988
|
-
{ field: "steps[0].anchor", cited: stable },
|
|
989
|
-
{ field: "steps[1].anchor", cited: "3#ZZZZ" },
|
|
990
|
-
],
|
|
991
|
-
);
|
|
992
|
-
assert.equal(await readFile(file, "utf8"), "changed\nb\nc\n");
|
|
993
|
-
}));
|
|
994
|
-
|
|
995
|
-
test("failed batches over forty steps → report every failed anchor and its candidate content", async () =>
|
|
996
|
-
withDir(async (dir) => {
|
|
997
|
-
const original = "a\ntarget\nz\n";
|
|
998
|
-
const before = `prefix\n${original}`;
|
|
999
|
-
const file = join(dir, "many-checks.txt");
|
|
1000
|
-
await writeFile(file, before);
|
|
1001
|
-
const steps = Array.from({ length: 45 }, () => ({
|
|
1002
|
-
action: "replace",
|
|
1003
|
-
anchor: h(original, 2),
|
|
1004
|
-
lines: ["changed"],
|
|
1005
|
-
}));
|
|
1006
|
-
const report = failureReport(
|
|
1007
|
-
await call(makeEditOverride(dir, DEFAULT_CONFIG), { path: file, steps }),
|
|
1008
|
-
);
|
|
1009
|
-
assert.equal(report.failures?.length, 45);
|
|
1010
|
-
assert.equal(report.omittedFailures, undefined);
|
|
1011
|
-
assert.deepEqual(
|
|
1012
|
-
report.failures?.map((failure) => failure.field),
|
|
1013
|
-
steps.map((_, index) => `steps[${index}].anchor`),
|
|
1014
|
-
);
|
|
1015
|
-
assert.ok(
|
|
1016
|
-
report.failures?.every(
|
|
1017
|
-
(failure) => failure.candidate === h(before, 3) && failure.content === "target",
|
|
1018
|
-
),
|
|
1019
|
-
);
|
|
1020
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
1021
|
-
}));
|
|
1022
|
-
|
|
1023
|
-
test("unique candidate content is independent of oversized neighboring rows", async () =>
|
|
1024
|
-
withDir(async (dir) => {
|
|
1025
|
-
const original = "header\ntarget\ntail\n";
|
|
1026
|
-
const before = `header\n${"x".repeat(DEFAULT_OUTPUT_BUDGETS.blockBytes + 1)}\ntarget\ntail\n`;
|
|
1027
|
-
const file = join(dir, "fallback.txt");
|
|
1028
|
-
await writeFile(file, before);
|
|
1029
|
-
const report = failureReport(
|
|
1030
|
-
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1031
|
-
path: file,
|
|
1032
|
-
steps: [{ action: "replace", anchor: h(original, 2), lines: ["updated"] }],
|
|
1033
|
-
}),
|
|
1034
|
-
);
|
|
1035
|
-
const candidate = report.failures?.[0].candidate;
|
|
1036
|
-
assert.ok(candidate);
|
|
1037
|
-
assert.equal(candidate, h(before, 3));
|
|
1038
|
-
assert.equal(report.failures?.[0].content, "target");
|
|
1039
|
-
assert.equal(report.candidateNeighborhoods, undefined);
|
|
1040
|
-
assert.equal(report.omittedFailures, undefined);
|
|
1041
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
1042
|
-
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1043
|
-
path: file,
|
|
1044
|
-
steps: [{ action: "replace", anchor: candidate, lines: ["updated"] }],
|
|
1045
|
-
});
|
|
1046
|
-
assert.equal(await readFile(file, "utf8"), before.replace("target", "updated"));
|
|
1047
|
-
}));
|
|
1048
|
-
|
|
1049
|
-
test("standalone CR replacements remain visible in diffs and exact in file bytes", async () =>
|
|
1050
|
-
withDir(async (dir) => {
|
|
1051
|
-
const file = join(dir, "cr.txt");
|
|
1052
|
-
for (const [before, find, replacement] of [
|
|
1053
|
-
["a\rb\n", "\r", "\n"],
|
|
1054
|
-
["a\rb\n", "\r", "␍"],
|
|
1055
|
-
]) {
|
|
1056
|
-
await writeFile(file, before);
|
|
1057
|
-
const result = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
1058
|
-
path: file,
|
|
1059
|
-
replacements: [{ find, replace: replacement, literal: false }],
|
|
1060
|
-
});
|
|
1061
|
-
assert.equal(await readFile(file, "utf8"), before.replaceAll(find, replacement));
|
|
1062
|
-
assert.match(result.details.diff, /^-1 a␍/m);
|
|
1063
|
-
assert.match(result.details.diff, /^\+1 a/m);
|
|
1064
|
-
assert.ok(!result.details.diff.includes("\r"));
|
|
1065
|
-
}
|
|
1066
|
-
}));
|
|
1067
|
-
|
|
1068
|
-
test("shifted unique candidate → displays standalone CR without altering its anchor", async () =>
|
|
1069
|
-
withDir(async (dir) => {
|
|
1070
|
-
const file = join(dir, "cr.txt");
|
|
1071
|
-
const before = "remove\na\rb\n";
|
|
1072
|
-
await writeFile(file, before);
|
|
1073
|
-
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1074
|
-
path: file,
|
|
1075
|
-
steps: [{ action: "delete", anchor: h(before, 1) }],
|
|
1076
|
-
});
|
|
1077
|
-
assert.equal(await readFile(file, "utf8"), "a\rb\n");
|
|
1078
|
-
const report = failureReport(
|
|
1079
|
-
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1080
|
-
path: file,
|
|
1081
|
-
steps: [{ action: "delete", anchor: h(before, 2) }],
|
|
1082
|
-
}),
|
|
1083
|
-
);
|
|
1084
|
-
assert.equal(report.failures?.[0].candidate, h("a\rb\n", 1));
|
|
1085
|
-
assert.equal(report.failures?.[0].content, "a␍b");
|
|
1086
|
-
}));
|
|
1087
|
-
|
|
1088
|
-
test("edit receives multiple wrong-length anchors → reports each location with one recovery hint", async () =>
|
|
1089
|
-
withDir(async (dir) => {
|
|
1090
|
-
const text = "first\nsecond\n";
|
|
1091
|
-
const file = join(dir, "f.txt");
|
|
1092
|
-
await writeFile(file, text);
|
|
1093
|
-
const { hashLen } = DEFAULT_CONFIG;
|
|
1094
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1095
|
-
const anchor = h(text, 1);
|
|
1096
|
-
const short = h(text, 2).slice(0, -2);
|
|
1097
|
-
const args = {
|
|
1098
|
-
path: "f.txt",
|
|
1099
|
-
steps: [
|
|
1100
|
-
{ action: "replace", anchor, end: short, lines: ["x"] },
|
|
1101
|
-
{ action: "delete", anchor: short },
|
|
1102
|
-
],
|
|
1103
|
-
};
|
|
1104
|
-
|
|
1105
|
-
await assert.rejects(call(edit, args), (error: Error) => {
|
|
1106
|
-
rejectsArgument("steps[0].end")(error);
|
|
1107
|
-
rejectsArgument("steps[1].anchor")(error);
|
|
1108
|
-
const issues = argumentError(error).issues;
|
|
1109
|
-
assert.deepEqual(
|
|
1110
|
-
issues.map((issue) => issue.field),
|
|
1111
|
-
["steps[0].end", "steps[1].anchor"],
|
|
1112
|
-
);
|
|
1113
|
-
assert.equal(issues.filter((issue) => issue.fix?.includes("Read or grep")).length, 1);
|
|
1114
|
-
assert.match(error.message, new RegExp(`${hashLen - 2} hash characters`));
|
|
1115
|
-
assert.match(error.message, new RegExp(`hashLen.*${hashLen}`));
|
|
1116
|
-
return true;
|
|
1117
|
-
});
|
|
1118
|
-
assert.equal(await readFile(file, "utf8"), text);
|
|
1119
|
-
}));
|
|
1120
|
-
|
|
1121
|
-
test("Pi-converted single-object steps execute as a canonical array", async () =>
|
|
1122
|
-
withDir(async (dir) => {
|
|
1123
|
-
const file = join(dir, "converted.txt");
|
|
1124
|
-
const original = "first\nsecond\n";
|
|
1125
|
-
await writeFile(file, original);
|
|
1126
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1127
|
-
const operation = { action: "replace", anchor: h(original, 2), lines: ["SECOND"] };
|
|
1128
|
-
const args = validateToolArguments(edit as any, {
|
|
1129
|
-
type: "toolCall",
|
|
1130
|
-
id: "converted",
|
|
1131
|
-
name: "edit",
|
|
1132
|
-
arguments: { path: "converted.txt", steps: operation },
|
|
1133
|
-
});
|
|
1134
|
-
assert.deepEqual(args.steps, [operation]);
|
|
1135
|
-
await call(edit, args);
|
|
1136
|
-
assert.equal(await readFile(file, "utf8"), "first\nSECOND\n");
|
|
1137
|
-
}));
|
|
1138
|
-
|
|
1139
|
-
test("unresolved snapshot rows support direct retry and revalidate after further changes", async () => {
|
|
1140
|
-
await withDir(async (dir) => {
|
|
1141
|
-
const file = join(dir, "recover.txt");
|
|
1142
|
-
const observed = "\uFEFFguard\r\ntarget = old\r\n中文 literal \\n\\0\r\n";
|
|
1143
|
-
const current = observed.replace("old", "pending");
|
|
1144
|
-
await writeFile(file, current);
|
|
1145
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1146
|
-
const report = failureReport(
|
|
1147
|
-
await call(edit, {
|
|
1148
|
-
path: "recover.txt",
|
|
1149
|
-
steps: [{ action: "replace", anchor: h(observed, 2), lines: ["target = new"] }],
|
|
1150
|
-
}),
|
|
1151
|
-
);
|
|
1152
|
-
const diagnostic = report.failures?.[0].observed ?? "";
|
|
1153
|
-
assert.match(diagnostic, /^2#[0-9A-Z]+│target = pending$/);
|
|
1154
|
-
assert.deepEqual(await readFile(file), Buffer.from(current));
|
|
1155
|
-
const retryAnchor = anchorLine(diagnostic, 2);
|
|
1156
|
-
const changedAgain = current.replace("pending", "other");
|
|
1157
|
-
await writeFile(file, changedAgain);
|
|
1158
|
-
const retry = failureReport(
|
|
1159
|
-
await call(edit, {
|
|
1160
|
-
path: "recover.txt",
|
|
1161
|
-
steps: [{ action: "replace", anchor: retryAnchor, lines: ["target = new"] }],
|
|
1162
|
-
}),
|
|
1163
|
-
);
|
|
1164
|
-
assert.equal(retry.error, "ANCHOR_MISMATCH");
|
|
1165
|
-
assert.deepEqual(await readFile(file), Buffer.from(changedAgain));
|
|
1166
|
-
await writeFile(file, current);
|
|
1167
|
-
await call(edit, {
|
|
1168
|
-
path: "recover.txt",
|
|
1169
|
-
steps: [{ action: "replace", anchor: retryAnchor, lines: ["target = new"] }],
|
|
1170
|
-
});
|
|
1171
|
-
assert.deepEqual(await readFile(file), Buffer.from(observed.replace("old", "new")));
|
|
1172
|
-
});
|
|
1173
|
-
});
|
|
1174
|
-
test("unresolved oversized and out-of-range rows require more context without partial anchors", async () => {
|
|
1175
|
-
await withDir(async (dir) => {
|
|
1176
|
-
const file = join(dir, "recover.txt");
|
|
1177
|
-
const current =
|
|
1178
|
-
"界".repeat(Math.ceil(DEFAULT_OUTPUT_BUDGETS.fieldBytes / Buffer.byteLength("界"))) + "\n";
|
|
1179
|
-
await writeFile(file, current);
|
|
1180
|
-
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1181
|
-
for (const anchor of [h("old\n", 1), "99#XXXX"]) {
|
|
1182
|
-
const report = failureReport(
|
|
1183
|
-
await call(edit, {
|
|
1184
|
-
path: "recover.txt",
|
|
1185
|
-
steps: [{ action: "replace", anchor, lines: ["new"] }],
|
|
1186
|
-
}),
|
|
1187
|
-
);
|
|
1188
|
-
assert.match(report.next ?? "", /read or grep/);
|
|
1189
|
-
assert.equal(report.failures?.[0].observed, undefined);
|
|
1190
|
-
assert.equal(
|
|
1191
|
-
report.failures?.[0].notShown,
|
|
1192
|
-
anchor.startsWith("99#")
|
|
1193
|
-
? "cited line is out of range"
|
|
1194
|
-
: `row exceeds ${formatByteSize(DEFAULT_OUTPUT_BUDGETS.fieldBytes)}`,
|
|
1195
|
-
);
|
|
1196
|
-
assert.deepEqual(await readFile(file), Buffer.from(current));
|
|
1197
|
-
}
|
|
1198
|
-
});
|
|
1199
|
-
});
|
|
1200
|
-
|
|
1201
|
-
test("edit rejects impossible checksum characters before accessing the file", async () =>
|
|
1202
|
-
withDir(async (dir) => {
|
|
1203
|
-
const tool = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1204
|
-
for (const char of ["I", "L", "O", "U"]) {
|
|
1205
|
-
await assert.rejects(
|
|
1206
|
-
call(tool, {
|
|
1207
|
-
path: "missing.txt",
|
|
1208
|
-
steps: [{ action: "delete", anchor: `1#${char.repeat(DEFAULT_CONFIG.hashLen)}` }],
|
|
1209
|
-
}),
|
|
1210
|
-
rejectsArguments(),
|
|
1211
|
-
);
|
|
1212
|
-
}
|
|
1213
|
-
}));
|
|
1214
|
-
|
|
1215
|
-
test("ambiguous failure lists and neighborhoods select the same first eight candidates", async () =>
|
|
1216
|
-
withDir(async (dir) => {
|
|
1217
|
-
const positions = [4, 14, 24, 34, 44, 54, 64, 74, 84];
|
|
1218
|
-
const lines = Array.from({ length: 90 }, (_, index) => `line-${index + 1}`);
|
|
1219
|
-
for (const position of positions) lines[position - 1] = "target";
|
|
1220
|
-
const text = lines.join("\n");
|
|
1221
|
-
await writeFile(join(dir, "candidates.txt"), text);
|
|
1222
|
-
const tool = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1223
|
-
const report = failureReport(
|
|
1224
|
-
await call(tool, {
|
|
1225
|
-
path: "candidates.txt",
|
|
1226
|
-
steps: [{ action: "delete", anchor: `200#${computeLineHash(200, "target", 4)}` }],
|
|
1227
|
-
}),
|
|
1228
|
-
);
|
|
1229
|
-
assert.equal(report.failures?.[0].omittedCandidates, 1);
|
|
1230
|
-
assert.deepEqual(
|
|
1231
|
-
report.failures?.[0].candidates,
|
|
1232
|
-
positions.slice(0, 8).map((position) => h(text, position)),
|
|
1233
|
-
);
|
|
1234
|
-
const rows = report.candidateNeighborhoods?.flatMap((group) => group.rows) ?? [];
|
|
1235
|
-
for (const position of positions.slice(0, 8))
|
|
1236
|
-
assert.ok(rows.includes(`${h(text, position)}│target`));
|
|
1237
|
-
assert.ok(!JSON.stringify(report).includes(h(text, 84)));
|
|
1238
|
-
assert.equal(await readFile(join(dir, "candidates.txt"), "utf8"), text);
|
|
1239
|
-
}));
|
|
1240
|
-
|
|
1241
|
-
test("edit verification → uses the supplied hash length", async () =>
|
|
1242
|
-
withDir(async (dir) => {
|
|
1243
|
-
const edit = makeEditOverride(dir, { ...DEFAULT_CONFIG, hashLen: 6 });
|
|
1244
|
-
await writeFile(join(dir, "registered.txt"), "before\n");
|
|
1245
|
-
const result = await call(edit, {
|
|
1246
|
-
path: "registered.txt",
|
|
1247
|
-
steps: [
|
|
1248
|
-
{
|
|
1249
|
-
action: "replace",
|
|
1250
|
-
anchor: `1#${computeLineHash(1, "before", 6)}`,
|
|
1251
|
-
lines: ["after"],
|
|
1252
|
-
},
|
|
1253
|
-
],
|
|
1254
|
-
});
|
|
1255
|
-
assert.equal(result.isError, undefined);
|
|
1256
|
-
assert.equal(await readFile(join(dir, "registered.txt"), "utf8"), "after\n");
|
|
1257
|
-
}));
|