@criogaid/pi-hashline-edit 0.3.1 → 0.5.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 +166 -61
- package/package.json +8 -3
- package/src/core/apply.test.ts +459 -313
- package/src/core/apply.ts +251 -215
- package/src/core/errors.ts +9 -0
- package/src/core/hash.test.ts +61 -23
- package/src/core/hash.ts +49 -20
- package/src/core/lines.test.ts +46 -27
- package/src/core/lines.ts +36 -28
- package/src/core/ranges.test.ts +73 -19
- package/src/core/ranges.ts +15 -15
- package/src/core/replace.ts +109 -0
- package/src/core/text.test.ts +3 -3
- package/src/core/text.ts +26 -9
- package/src/core/types.ts +56 -41
- package/src/index.ts +23 -19
- package/src/integration/config-registration.test.ts +206 -0
- package/src/integration/grep-rg.test.ts +879 -251
- package/src/integration/large-text.test.ts +63 -0
- package/src/integration/replace-package.test.ts +41 -0
- package/src/pi/action-fusion.test.ts +612 -207
- package/src/pi/action-fusion.ts +442 -254
- package/src/pi/anchor-format.test.ts +53 -21
- package/src/pi/anchor-format.ts +48 -20
- package/src/pi/budgets.ts +25 -0
- package/src/pi/config.test.ts +178 -35
- package/src/pi/config.ts +96 -37
- package/src/pi/directory-sync.test.ts +55 -0
- package/src/pi/e2e-scenarios.test.ts +360 -0
- package/src/pi/edit-tool.ts +307 -279
- package/src/pi/error-text.ts +26 -0
- package/src/pi/execute.test.ts +1718 -713
- package/src/pi/failure-context.test.ts +110 -83
- package/src/pi/failure-context.ts +232 -73
- package/src/pi/failure-path.test.ts +180 -66
- package/src/pi/file-commit.test.ts +446 -233
- package/src/pi/file-commit.ts +348 -203
- package/src/pi/fusion-card.test.ts +118 -83
- package/src/pi/fusion-card.ts +203 -87
- package/src/pi/grep-output.ts +225 -0
- package/src/pi/grep-render.ts +59 -0
- package/src/pi/grep-scope.ts +139 -0
- package/src/pi/grep-search.ts +231 -0
- package/src/pi/grep-tool.ts +215 -774
- package/src/pi/grep.test.ts +825 -376
- package/src/pi/mutation-outcome.testing.ts +22 -0
- package/src/pi/mutation-result.test.ts +86 -0
- package/src/pi/mutation-result.ts +118 -63
- package/src/pi/mutation-runner.ts +151 -0
- package/src/pi/path.ts +20 -12
- package/src/pi/pi.test.ts +100 -52
- package/src/pi/read-tool.ts +245 -134
- package/src/pi/registration.test.ts +271 -114
- package/src/pi/render.test.ts +227 -84
- package/src/pi/render.ts +86 -116
- package/src/pi/replace-regex.ts +54 -0
- package/src/pi/replace-tool.ts +247 -241
- package/src/pi/replace-worker.mjs +4 -0
- package/src/pi/replace-worker.ts +10 -0
- package/src/pi/replace.test.ts +778 -390
- package/src/pi/result-safety.test.ts +821 -344
- package/src/pi/rg-line-ranges.test.ts +57 -31
- package/src/pi/rg-line-ranges.ts +45 -82
- package/src/pi/rg-process.test.ts +113 -0
- package/src/pi/{rg-line-filter.ts → rg-process.ts} +72 -125
- package/src/pi/rg-text-view.ts +102 -48
- package/src/pi/schema.ts +19 -0
- package/src/pi/text-stream.test.ts +118 -0
- package/src/pi/text-stream.ts +136 -0
- package/src/pi/tool-call.testing.ts +37 -0
- package/src/pi/write-tool.ts +114 -52
- package/src/pi/write.test.ts +250 -97
- package/src/core/index.ts +0 -14
- package/src/pi/rg-line-filter.test.ts +0 -85
- package/src/pi/state.ts +0 -26
package/src/pi/execute.test.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { mkdir, mkdtemp, rm, readFile, writeFile } from "node:fs/promises";
|
|
|
9
9
|
import { tmpdir } from "node:os";
|
|
10
10
|
import { join } from "node:path";
|
|
11
11
|
import { createEditTool, initTheme } from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
12
13
|
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
13
14
|
import registerHashline from "../index.ts";
|
|
14
15
|
import { makeEditOverride } from "./edit-tool.ts";
|
|
@@ -16,799 +17,1803 @@ import { makeReadOverride } from "./read-tool.ts";
|
|
|
16
17
|
import { makeWriteOverride } from "./write-tool.ts";
|
|
17
18
|
import { makeReplaceTool } from "./replace-tool.ts";
|
|
18
19
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
19
|
-
import {
|
|
20
|
+
import { loadConfig } from "./config.ts";
|
|
20
21
|
import { computeLineHash } from "../core/hash.ts";
|
|
21
22
|
import { splitLines } from "../core/lines.ts";
|
|
22
23
|
import { byteRevision } from "./file-commit.ts";
|
|
24
|
+
import { generateMutationDetails } from "./mutation-result.ts";
|
|
25
|
+
import { callTool } from "./tool-call.testing.ts";
|
|
26
|
+
import { DEFAULT_CONFIG } from "./config.ts";
|
|
23
27
|
|
|
24
28
|
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
const dir = await mkdtemp(join(tmpdir(), "hl-"));
|
|
30
|
+
try {
|
|
31
|
+
return await fn(dir);
|
|
32
|
+
} finally {
|
|
33
|
+
await rm(dir, { recursive: true, force: true });
|
|
34
|
+
}
|
|
31
35
|
}
|
|
32
36
|
|
|
33
|
-
|
|
37
|
+
/** Call a tool through Pi's argument preparation and schema validation, as production does. */
|
|
38
|
+
const call = (tool: any, params: any) => callTool(tool, params, { toolCallId: "0" });
|
|
34
39
|
|
|
35
40
|
/** Anchor a model would copy from read output for `line` of `text` (1-based). */
|
|
36
41
|
function h(text: string, line: number) {
|
|
37
|
-
|
|
42
|
+
return `${line}#${computeLineHash(line, splitLines(text)[line - 1], 4)}`;
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
/** Extract a `LINE#HASH` anchor from a read/edit result text block. */
|
|
41
46
|
function anchorLine(block: string, line: number) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
const m = new RegExp(`^${line}#([0-9A-Z]+)(?:│|$)`, "m").exec(block);
|
|
48
|
+
if (!m) throw new Error(`line ${line} anchor not found in block`);
|
|
49
|
+
return `${line}#${m[1]}`;
|
|
45
50
|
}
|
|
46
51
|
|
|
52
|
+
test("edit guidance keeps insert anchors and warns about shifted lines", () => {
|
|
53
|
+
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
54
|
+
const insertOp = tool.parameters.properties.edits.items.anyOf.find((variant) => {
|
|
55
|
+
const op = variant.properties.op;
|
|
56
|
+
return "anyOf" in op && op.anyOf.some((choice) => choice.const === "insert_after");
|
|
57
|
+
})?.properties.op;
|
|
58
|
+
assert.ok(insertOp);
|
|
59
|
+
const description: unknown = Reflect.get(insertOp, "description");
|
|
60
|
+
assert.ok(typeof description === "string");
|
|
61
|
+
assert.match(description, /anchor line.*kept/);
|
|
62
|
+
assert.match(description, /do not repeat.*body/);
|
|
63
|
+
assert.ok(
|
|
64
|
+
tool.promptGuidelines.some(
|
|
65
|
+
(rule) => /inserts and deletes/.test(rule) && /shift later lines/.test(rule),
|
|
66
|
+
),
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
47
70
|
test("read execute: text outputs LINE#HASH│content", async () => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
71
|
+
await withDir(async (dir) => {
|
|
72
|
+
await writeFile(join(dir, "f.txt"), "line1\nline2\n");
|
|
73
|
+
const r: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
74
|
+
const text = r.content[0];
|
|
75
|
+
assert.equal(text.type, "text");
|
|
76
|
+
assert.match(text.text, /1#[0-9A-Z]+│line1/);
|
|
77
|
+
assert.match(text.text, /2#[0-9A-Z]+│line2/);
|
|
78
|
+
assert.match(text.text, /f\.txt · 2 lines/);
|
|
79
|
+
});
|
|
57
80
|
});
|
|
58
81
|
|
|
59
|
-
test("read execute: Pi-supported images without NUL bypass text decoding", async () =>
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
82
|
+
test("read execute: Pi-supported images without NUL bypass text decoding", async () =>
|
|
83
|
+
withDir(async (dir) => {
|
|
84
|
+
for (const [name, bytes, mime] of [
|
|
85
|
+
["picture.jpg", Buffer.from([0xff, 0xd8, 0xff, 0xd9]), "image/jpeg"],
|
|
86
|
+
["picture.gif", Buffer.from("GIF89a"), "image/gif"],
|
|
87
|
+
] as const) {
|
|
88
|
+
await writeFile(join(dir, name), bytes);
|
|
89
|
+
const result: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: name });
|
|
90
|
+
assert.ok(result.content[0].text.startsWith(`Read image file [${mime}]`));
|
|
91
|
+
assert.deepEqual(await readFile(join(dir, name)), bytes);
|
|
92
|
+
}
|
|
93
|
+
}));
|
|
70
94
|
|
|
71
95
|
test("read execute: a missing final newline is stated in the header", async () => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
96
|
+
await withDir(async (dir) => {
|
|
97
|
+
await writeFile(join(dir, "f.txt"), "line1\nline2");
|
|
98
|
+
const bare: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
99
|
+
assert.match(bare.content[0].text, /f\.txt · 2 lines · no trailing newline/);
|
|
100
|
+
|
|
101
|
+
await writeFile(join(dir, "g.txt"), "line1\nline2\n");
|
|
102
|
+
const terminated: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "g.txt" });
|
|
103
|
+
assert.doesNotMatch(terminated.content[0].text, /no trailing newline/);
|
|
104
|
+
});
|
|
81
105
|
});
|
|
82
106
|
|
|
83
107
|
test("edit execute: a file without a final newline stays byte-exact", async () => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
108
|
+
await withDir(async (dir) => {
|
|
109
|
+
const f = join(dir, "f.txt");
|
|
110
|
+
// The benchmark's literal-1-no-final-newline fixture: no terminator, plus a
|
|
111
|
+
// word-joiner the model cannot see. Only line 2 may change; the missing
|
|
112
|
+
// terminator must not turn into a new byte.
|
|
113
|
+
const text = "guard\nold value\u2060";
|
|
114
|
+
await writeFile(f, text);
|
|
115
|
+
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
116
|
+
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
117
|
+
path: "f.txt",
|
|
118
|
+
edits: [{ op: "replace", anchor: h(text, 2), body: ["new value\u2060"] }],
|
|
119
|
+
});
|
|
120
|
+
assert.equal(r.isError, undefined, "should not be an error");
|
|
121
|
+
assert.equal(await readFile(f, "utf-8"), "guard\nnew value\u2060");
|
|
122
|
+
});
|
|
99
123
|
});
|
|
100
124
|
|
|
101
125
|
test("edit execute: hashline round-trip (read → edit → file changed)", async () => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
126
|
+
await withDir(async (dir) => {
|
|
127
|
+
const f = join(dir, "f.txt");
|
|
128
|
+
const text = "a\nb\nc\n";
|
|
129
|
+
await writeFile(f, text);
|
|
130
|
+
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
131
|
+
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
132
|
+
path: "f.txt",
|
|
133
|
+
edits: [{ op: "replace", anchor: h(text, 2), body: ["B"] }],
|
|
134
|
+
});
|
|
135
|
+
assert.equal(r.isError, undefined, "should not be an error");
|
|
136
|
+
assert.equal(await readFile(f, "utf-8"), "a\nB\nc\n");
|
|
137
|
+
});
|
|
114
138
|
});
|
|
115
139
|
|
|
116
140
|
test("edit execute: multiple ops in one call", async () => {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
141
|
+
await withDir(async (dir) => {
|
|
142
|
+
const f = join(dir, "f.txt");
|
|
143
|
+
const text = "a\nb\nc\n";
|
|
144
|
+
await writeFile(f, text);
|
|
145
|
+
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
146
|
+
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
147
|
+
path: "f.txt",
|
|
148
|
+
edits: [
|
|
149
|
+
{ op: "insert_after", anchor: h(text, 3), body: ["z"] },
|
|
150
|
+
{ op: "replace", anchor: h(text, 1), body: ["A"] },
|
|
151
|
+
],
|
|
152
|
+
});
|
|
153
|
+
assert.equal(r.isError, undefined);
|
|
154
|
+
assert.equal(await readFile(f, "utf-8"), "A\nb\nc\nz\n");
|
|
155
|
+
});
|
|
132
156
|
});
|
|
133
157
|
|
|
134
158
|
test("edit result returns Updated anchors that chain the next edit without a re-read", async () => {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
159
|
+
await withDir(async (dir) => {
|
|
160
|
+
const f = join(dir, "f.txt");
|
|
161
|
+
const text = "a\nb\nc\n";
|
|
162
|
+
await writeFile(f, text);
|
|
163
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
164
|
+
// first edit (model cites the read anchor for line 1)
|
|
165
|
+
const r1: any = await call(edit, {
|
|
166
|
+
path: "f.txt",
|
|
167
|
+
edits: [{ op: "replace", anchor: h(text, 1), body: ["A"] }],
|
|
168
|
+
});
|
|
169
|
+
assert.equal(r1.isError, undefined);
|
|
170
|
+
const out: string = r1.content[0].text;
|
|
171
|
+
assert.match(out, /Updated anchors/);
|
|
172
|
+
assert.doesNotMatch(out, /│/);
|
|
173
|
+
// second edit chains on the anchor returned by the first edit — no read in between
|
|
174
|
+
const r2: any = await call(edit, {
|
|
175
|
+
path: "f.txt",
|
|
176
|
+
edits: [{ op: "replace", anchor: anchorLine(out, 1), body: ["AA"] }],
|
|
177
|
+
});
|
|
178
|
+
assert.equal(r2.isError, undefined);
|
|
179
|
+
assert.equal(await readFile(f, "utf-8"), "AA\nb\nc\n");
|
|
180
|
+
});
|
|
157
181
|
});
|
|
158
182
|
|
|
159
183
|
test("edit result anchors cover an inserted block (chain an edit inside it)", async () => {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
184
|
+
await withDir(async (dir) => {
|
|
185
|
+
const f = join(dir, "f.txt");
|
|
186
|
+
const text = "a\nb\n";
|
|
187
|
+
await writeFile(f, text);
|
|
188
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
189
|
+
const r1: any = await call(edit, {
|
|
190
|
+
path: "f.txt",
|
|
191
|
+
edits: [{ op: "insert_after", anchor: h(text, 2), body: ["c", "d", "e"] }],
|
|
192
|
+
});
|
|
193
|
+
assert.equal(r1.isError, undefined);
|
|
194
|
+
const out: string = r1.content[0].text;
|
|
195
|
+
// line 4 (d, one of the inserted lines) must be anchored in the result
|
|
196
|
+
const a4 = anchorLine(out, 4);
|
|
197
|
+
const r2: any = await call(edit, {
|
|
198
|
+
path: "f.txt",
|
|
199
|
+
edits: [{ op: "replace", anchor: a4, body: ["DD"] }],
|
|
200
|
+
});
|
|
201
|
+
assert.equal(r2.isError, undefined);
|
|
202
|
+
assert.equal(await readFile(f, "utf-8"), "a\nb\nc\nDD\ne\n");
|
|
203
|
+
});
|
|
180
204
|
});
|
|
181
205
|
|
|
182
206
|
test("unrelated external change does NOT block an edit on a stable line", async () => {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
207
|
+
await withDir(async (dir) => {
|
|
208
|
+
const f = join(dir, "f.txt");
|
|
209
|
+
const text = "a\nb\nc\n";
|
|
210
|
+
await writeFile(f, text);
|
|
211
|
+
// simulate an external change at line 3 between read and edit
|
|
212
|
+
await writeFile(f, "a\nb\nCHANGED\n");
|
|
213
|
+
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
214
|
+
path: "f.txt",
|
|
215
|
+
edits: [{ op: "replace", anchor: h(text, 1), body: ["A"] }],
|
|
216
|
+
});
|
|
217
|
+
assert.equal(r.isError, undefined);
|
|
218
|
+
assert.equal(await readFile(f, "utf-8"), "A\nb\nCHANGED\n");
|
|
219
|
+
});
|
|
196
220
|
});
|
|
197
221
|
|
|
198
222
|
test("edit on a line that changed externally → anchor mismatch", async () => {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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 writeFile(f, "a\nBCHANGED\nc\n"); // line 2 changed
|
|
228
|
+
await assert.rejects(
|
|
229
|
+
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
230
|
+
path: "f.txt",
|
|
231
|
+
edits: [{ op: "replace", anchor: h(text, 2), body: ["x"] }],
|
|
232
|
+
}),
|
|
233
|
+
/anchor|re-read/i,
|
|
234
|
+
);
|
|
235
|
+
});
|
|
212
236
|
});
|
|
213
237
|
|
|
214
|
-
test("unresolved
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
238
|
+
test("unresolved observations require target confirmation before retrying a moved target", async () => {
|
|
239
|
+
await withDir(async (dir) => {
|
|
240
|
+
const file = join(dir, "recover.txt");
|
|
241
|
+
const observed =
|
|
242
|
+
["one", "two", "three", "four", "old marker", "six", "seven", "eight"].join("\n") + "\n";
|
|
243
|
+
await writeFile(
|
|
244
|
+
file,
|
|
245
|
+
["one", "two", "three", "four", "changed", "six", "new marker", "eight"].join("\n") + "\n",
|
|
246
|
+
);
|
|
247
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
248
|
+
await assert.rejects(
|
|
249
|
+
call(edit, {
|
|
250
|
+
path: "recover.txt",
|
|
251
|
+
edits: [{ op: "replace", anchor: h(observed, 5), body: ["updated"] }],
|
|
252
|
+
}),
|
|
253
|
+
(error: Error) => {
|
|
254
|
+
assert.match(error.message, /Anchor mismatch: 1 unresolved/);
|
|
255
|
+
assert.match(error.message, /No changes written by this edit batch/);
|
|
256
|
+
assert.match(
|
|
257
|
+
error.message,
|
|
258
|
+
/Confirm this is the intended target before reusing its anchor/,
|
|
259
|
+
);
|
|
260
|
+
assert.match(error.message, /^5#[0-9A-Z]+│changed$/m);
|
|
261
|
+
return true;
|
|
262
|
+
},
|
|
263
|
+
);
|
|
264
|
+
const read = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "recover.txt" });
|
|
265
|
+
const retryAnchor = anchorLine(read.content[0].text, 7);
|
|
266
|
+
await call(edit, {
|
|
267
|
+
path: "recover.txt",
|
|
268
|
+
edits: [{ op: "replace", anchor: retryAnchor, body: ["updated"] }],
|
|
269
|
+
});
|
|
270
|
+
assert.equal(
|
|
271
|
+
await readFile(file, "utf8"),
|
|
272
|
+
"one\ntwo\nthree\nfour\nchanged\nsix\nupdated\neight\n",
|
|
273
|
+
);
|
|
274
|
+
});
|
|
235
275
|
});
|
|
236
276
|
|
|
237
277
|
test("edit execute: no read before edit → anchor verification fails", async () => {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
278
|
+
await withDir(async (dir) => {
|
|
279
|
+
await writeFile(join(dir, "f.txt"), "a\nb\n");
|
|
280
|
+
await assert.rejects(
|
|
281
|
+
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
282
|
+
path: "f.txt",
|
|
283
|
+
edits: [{ op: "replace", anchor: "1#XXXX", body: ["A"] }],
|
|
284
|
+
}),
|
|
285
|
+
/anchor|re-read/i,
|
|
286
|
+
);
|
|
287
|
+
});
|
|
248
288
|
});
|
|
249
289
|
|
|
250
290
|
test("edit execute: empty edits → throws", async () => {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
291
|
+
await withDir(async (dir) => {
|
|
292
|
+
await writeFile(join(dir, "f.txt"), "a\n");
|
|
293
|
+
await assert.rejects(
|
|
294
|
+
call(makeEditOverride(dir, DEFAULT_CONFIG), { path: "f.txt", edits: [] }),
|
|
295
|
+
/Validation failed for tool "edit"/,
|
|
296
|
+
);
|
|
297
|
+
});
|
|
258
298
|
});
|
|
259
299
|
|
|
260
300
|
test("edit execute: malformed op (replace without body) → throws", async () => {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
301
|
+
await withDir(async (dir) => {
|
|
302
|
+
await writeFile(join(dir, "f.txt"), "a\n");
|
|
303
|
+
await assert.rejects(
|
|
304
|
+
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
305
|
+
path: "f.txt",
|
|
306
|
+
edits: [{ op: "replace", anchor: "1#XXXX" }],
|
|
307
|
+
}),
|
|
308
|
+
/body/i,
|
|
309
|
+
);
|
|
310
|
+
});
|
|
271
311
|
});
|
|
272
312
|
|
|
273
313
|
test("edit execute: delete op", async () => {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
314
|
+
await withDir(async (dir) => {
|
|
315
|
+
const f = join(dir, "f.txt");
|
|
316
|
+
const text = "a\nb\nc\n";
|
|
317
|
+
await writeFile(f, text);
|
|
318
|
+
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
319
|
+
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
320
|
+
path: "f.txt",
|
|
321
|
+
edits: [{ op: "delete", anchor: h(text, 2) }],
|
|
322
|
+
});
|
|
323
|
+
assert.equal(r.isError, undefined);
|
|
324
|
+
assert.equal(await readFile(f, "utf-8"), "a\nc\n");
|
|
325
|
+
});
|
|
286
326
|
});
|
|
287
327
|
|
|
288
328
|
test("disabled config registers no tools — built-ins remain", async () => {
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
329
|
+
await withDir(async (dir) => {
|
|
330
|
+
const oldCwd = process.cwd();
|
|
331
|
+
try {
|
|
332
|
+
await mkdir(join(dir, ".pi"));
|
|
333
|
+
await writeFile(
|
|
334
|
+
join(dir, ".pi", "settings.json"),
|
|
335
|
+
JSON.stringify({ hashlineEdit: { enabled: false } }),
|
|
336
|
+
);
|
|
337
|
+
await writeFile(join(dir, "f.txt"), "old value\n");
|
|
338
|
+
process.chdir(dir);
|
|
339
|
+
const registered: string[] = [];
|
|
340
|
+
registerHashline({
|
|
341
|
+
on() {},
|
|
342
|
+
registerTool(tool: { name: string }) {
|
|
343
|
+
registered.push(tool.name);
|
|
344
|
+
},
|
|
345
|
+
} as any);
|
|
346
|
+
assert.deepEqual(registered, []);
|
|
347
|
+
const builtin = createEditTool(dir);
|
|
348
|
+
const params = validateToolArguments(builtin, {
|
|
349
|
+
name: "edit",
|
|
350
|
+
arguments: { path: "f.txt", edits: [{ oldText: "old value", newText: "new value" }] },
|
|
351
|
+
} as any);
|
|
352
|
+
await call(builtin, params);
|
|
353
|
+
assert.equal(await readFile(join(dir, "f.txt"), "utf-8"), "new value\n");
|
|
354
|
+
} finally {
|
|
355
|
+
process.chdir(oldCwd);
|
|
356
|
+
}
|
|
357
|
+
});
|
|
312
358
|
});
|
|
313
359
|
|
|
314
360
|
// --- renderer regression guards (details.diff must be a string, renderResult must not throw) ---
|
|
315
361
|
|
|
316
|
-
const stubTheme = { fg: (_k: string, s: string) => s, bold: (s: string) => s };
|
|
362
|
+
const stubTheme = { fg: (_k: string, s: string) => s, bold: (s: string) => s } as Theme;
|
|
317
363
|
|
|
318
364
|
// renderResult delegates to pi's renderDiff, which reads the global TUI theme
|
|
319
365
|
// singleton — initialize it once for this test process (watcher off by default).
|
|
320
366
|
initTheme();
|
|
321
367
|
|
|
322
368
|
test("edit success: details.diff is a string (not the generateDiffString object)", async () => {
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
369
|
+
await withDir(async (dir) => {
|
|
370
|
+
const f = join(dir, "f.txt");
|
|
371
|
+
const text = "a\nb\nc\n";
|
|
372
|
+
await writeFile(f, text);
|
|
373
|
+
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
374
|
+
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
375
|
+
path: "f.txt",
|
|
376
|
+
edits: [{ op: "replace", anchor: h(text, 2), body: ["B"] }],
|
|
377
|
+
});
|
|
378
|
+
assert.equal(typeof r.details.diff, "string", "details.diff must be a string");
|
|
379
|
+
assert.equal(typeof r.details.patch, "string");
|
|
380
|
+
assert.equal(typeof r.details.firstChangedLine, "number");
|
|
381
|
+
});
|
|
336
382
|
});
|
|
337
383
|
|
|
338
384
|
test("edit success: renderResult renders the diff without throwing", async () => {
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
385
|
+
await withDir(async (dir) => {
|
|
386
|
+
const f = join(dir, "f.txt");
|
|
387
|
+
const text = "a\nb\nc\n";
|
|
388
|
+
await writeFile(f, text);
|
|
389
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
390
|
+
const r: any = await call(edit, {
|
|
391
|
+
path: "f.txt",
|
|
392
|
+
edits: [{ op: "replace", anchor: h(text, 2), body: ["B"] }],
|
|
393
|
+
});
|
|
394
|
+
const comp = edit.renderResult(
|
|
395
|
+
{ content: r.content, details: r.details },
|
|
396
|
+
{ isPartial: false, expanded: true },
|
|
397
|
+
stubTheme,
|
|
398
|
+
{
|
|
399
|
+
args: { path: "f.txt", edits: [{ op: "replace" }] },
|
|
400
|
+
isError: r.isError ?? false,
|
|
401
|
+
state: {},
|
|
402
|
+
invalidate: () => {},
|
|
403
|
+
} as Parameters<typeof edit.renderResult>[3],
|
|
404
|
+
);
|
|
405
|
+
assert.match(comp.render(80).join("\n"), /B/, "rendered diff should contain the new content");
|
|
406
|
+
});
|
|
353
407
|
});
|
|
354
408
|
|
|
355
409
|
test("edit header: renderResult refreshes the call header in place — no invalidate", async () => {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
410
|
+
await withDir(async (dir) => {
|
|
411
|
+
const f = join(dir, "f.txt");
|
|
412
|
+
const text = "a\nb\nc\nd\ne\n";
|
|
413
|
+
await writeFile(f, text);
|
|
414
|
+
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
415
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
416
|
+
const r: any = await call(edit, {
|
|
417
|
+
path: "f.txt",
|
|
418
|
+
edits: [
|
|
419
|
+
{ op: "replace", anchor: h(text, 2), end: h(text, 3), body: ["B"] },
|
|
420
|
+
{ op: "insert_after", anchor: h(text, 5), body: ["f", "g"] },
|
|
421
|
+
],
|
|
422
|
+
});
|
|
423
|
+
const args = { path: "f.txt", edits: [{ op: "replace" as const }] } as Parameters<
|
|
424
|
+
typeof edit.renderCall
|
|
425
|
+
>[0];
|
|
426
|
+
let invalidated = false;
|
|
427
|
+
const context: any = {
|
|
428
|
+
args,
|
|
429
|
+
isError: false,
|
|
430
|
+
state: {},
|
|
431
|
+
invalidate: () => {
|
|
432
|
+
invalidated = true;
|
|
433
|
+
},
|
|
434
|
+
};
|
|
435
|
+
// first pass: renderCall builds the header; no counts exist yet
|
|
436
|
+
const header: any = edit.renderCall(args, stubTheme, context);
|
|
437
|
+
assert.ok(!header.text.includes("+3"), "pre-execution header must not show counts");
|
|
438
|
+
// result lands: renderResult refreshes the SAME component in place
|
|
439
|
+
edit.renderResult(
|
|
440
|
+
{ content: r.content, details: r.details },
|
|
441
|
+
{ isPartial: false, expanded: true },
|
|
442
|
+
stubTheme,
|
|
443
|
+
context,
|
|
444
|
+
);
|
|
445
|
+
assert.deepEqual(context.state.diffCounts, { added: 3, removed: 2 });
|
|
446
|
+
assert.ok(header.text.includes("+3"), "header should show added count");
|
|
447
|
+
assert.ok(header.text.includes("-2"), "header should show removed count");
|
|
448
|
+
// refreshing via context.invalidate() re-enters updateDisplay and renders
|
|
449
|
+
// the diff twice — the renderer must never call it
|
|
450
|
+
assert.ok(!invalidated, "renderResult must not call invalidate");
|
|
451
|
+
// later full passes (expand/collapse) re-run renderCall; counts survive in state
|
|
452
|
+
const header2: any = edit.renderCall(args, stubTheme, {
|
|
453
|
+
...context,
|
|
454
|
+
lastComponent: header,
|
|
455
|
+
});
|
|
456
|
+
assert.equal(header2, header, "renderCall reuses the stashed component");
|
|
457
|
+
assert.ok(header2.text.includes("+3"), "re-render keeps the counts");
|
|
458
|
+
});
|
|
388
459
|
});
|
|
389
460
|
|
|
390
461
|
test("edit error: renderResult renders the error line without throwing", async () => {
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
462
|
+
await withDir(async (dir) => {
|
|
463
|
+
await writeFile(join(dir, "f.txt"), "a\n");
|
|
464
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
465
|
+
let thrown: any;
|
|
466
|
+
await call(edit, {
|
|
467
|
+
path: "f.txt",
|
|
468
|
+
edits: [{ op: "replace", anchor: "1#XXXX", body: ["A"] }],
|
|
469
|
+
}).catch((e: any) => {
|
|
470
|
+
thrown = e;
|
|
471
|
+
});
|
|
472
|
+
assert.ok(thrown, "expected the edit to throw");
|
|
473
|
+
const comp = edit.renderResult(
|
|
474
|
+
{
|
|
475
|
+
content: [{ type: "text", text: thrown.message }],
|
|
476
|
+
details: generateMutationDetails(
|
|
477
|
+
"f.txt",
|
|
478
|
+
"a\n",
|
|
479
|
+
"a\n",
|
|
480
|
+
{ publishedRevision: "r", observedRevision: "r" },
|
|
481
|
+
"PUBLISHED",
|
|
482
|
+
),
|
|
483
|
+
},
|
|
484
|
+
{ isPartial: false, expanded: false },
|
|
485
|
+
stubTheme,
|
|
486
|
+
{ args: { path: "f.txt", edits: [{ op: "replace" }] }, isError: true } as Parameters<
|
|
487
|
+
typeof edit.renderResult
|
|
488
|
+
>[3],
|
|
489
|
+
);
|
|
490
|
+
assert.match(comp.render(80).join("\n"), /anchor|checksum|mismatch/i);
|
|
491
|
+
});
|
|
411
492
|
});
|
|
412
493
|
|
|
413
494
|
test("hash length stays 4 even for runs of identical lines (no explosion)", async () => {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
495
|
+
await withDir(async (dir) => {
|
|
496
|
+
const f = join(dir, "f.txt");
|
|
497
|
+
await writeFile(f, "\n\n\n\ncode\n");
|
|
498
|
+
const r: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
499
|
+
const text: string = r.content[0].text;
|
|
500
|
+
for (const m of text.matchAll(/\d+#([0-9A-Z]+)│/g)) {
|
|
501
|
+
assert.equal(m[1].length, 4, `anchor ${m[0]} hash is not 4 chars`);
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
test("read byte truncation counts UTF-8 and separators without cutting anchors", async () =>
|
|
507
|
+
withDir(async (dir) => {
|
|
508
|
+
const maxBytes = 256 * 1024;
|
|
509
|
+
const hashLen = DEFAULT_CONFIG.hashLen;
|
|
510
|
+
const prefixBytes = Buffer.byteLength(`1#${"X".repeat(hashLen)}│`);
|
|
511
|
+
const first = "界".repeat(40000);
|
|
512
|
+
const second = "x".repeat(maxBytes - Buffer.byteLength(first) - 2 * prefixBytes);
|
|
513
|
+
await writeFile(join(dir, "large.txt"), `${first}\n${second}\n`);
|
|
514
|
+
const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "large.txt" });
|
|
515
|
+
assert.equal(result.details.truncation.outputLines, 1);
|
|
516
|
+
assert.equal(result.details.truncation.truncatedBy, "bytes");
|
|
517
|
+
assert.match(result.content[0].text, /truncated at 256 KiB/);
|
|
518
|
+
assert.ok(
|
|
519
|
+
result.content[0].text.includes(`1#${computeLineHash(1, first, hashLen)}│${first}\n`),
|
|
520
|
+
);
|
|
521
|
+
assert.doesNotMatch(result.content[0].text, /\n2#/);
|
|
522
|
+
const next = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
|
|
523
|
+
path: "large.txt",
|
|
524
|
+
offset: 2,
|
|
525
|
+
limit: 1,
|
|
526
|
+
});
|
|
527
|
+
assert.ok(next.content[0].text.includes(`2#${computeLineHash(2, second, hashLen)}│${second}`));
|
|
528
|
+
}));
|
|
529
|
+
|
|
530
|
+
test("read reports an oversized first row without suggesting an ineffective retry", async () =>
|
|
531
|
+
withDir(async (dir) => {
|
|
532
|
+
await writeFile(join(dir, "long.txt"), "x".repeat(256 * 1024));
|
|
533
|
+
const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "long.txt" });
|
|
534
|
+
assert.equal(result.details.truncation.firstLineExceedsLimit, true);
|
|
535
|
+
assert.equal(result.details.truncation.outputLines, 0);
|
|
536
|
+
assert.match(result.content[0].text, /cannot return a complete anchor row/);
|
|
537
|
+
assert.doesNotMatch(result.content[0].text, /use offset\/limit/);
|
|
538
|
+
assert.match(result.content[0].text, /Reducing limit cannot split a physical line/);
|
|
539
|
+
assert.match(result.content[0].text, /use bash to inspect it in chunks, or replace/);
|
|
540
|
+
}));
|
|
541
|
+
|
|
542
|
+
test("read preserves empty files and explicit limits above the native default", async () =>
|
|
543
|
+
withDir(async (dir) => {
|
|
544
|
+
await writeFile(join(dir, "empty.txt"), "");
|
|
545
|
+
const empty = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "empty.txt" });
|
|
546
|
+
assert.match(empty.content[0].text, /0 lines/);
|
|
547
|
+
assert.equal(empty.details, undefined);
|
|
548
|
+
await writeFile(join(dir, "many.txt"), "x\n".repeat(2001));
|
|
549
|
+
const many = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
|
|
550
|
+
path: "many.txt",
|
|
551
|
+
limit: 2001,
|
|
552
|
+
});
|
|
553
|
+
assert.match(many.content[0].text, /\n2001#[0-9A-Z]+│x/);
|
|
554
|
+
assert.equal(many.details, undefined);
|
|
555
|
+
}));
|
|
556
|
+
|
|
557
|
+
test("read defaults to 500 lines when limit is omitted and respects explicit limits", async () =>
|
|
558
|
+
withDir(async (dir) => {
|
|
559
|
+
await writeFile(
|
|
560
|
+
join(dir, "large.txt"),
|
|
561
|
+
Array.from({ length: 600 }, (_, i) => `line${i + 1}\n`).join(""),
|
|
562
|
+
);
|
|
563
|
+
const def = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "large.txt" });
|
|
564
|
+
assert.match(def.content[0].text, /large\.txt · 600 lines/);
|
|
565
|
+
assert.match(def.content[0].text, /\n500#[0-9A-Z]+│line500/);
|
|
566
|
+
assert.doesNotMatch(def.content[0].text, /\n501#[0-9A-Z]+│/);
|
|
567
|
+
assert.match(def.content[0].text, /showing lines 1-500 of 600; use offset 501 to continue/);
|
|
568
|
+
assert.deepEqual(def.details, {
|
|
569
|
+
pagination: { start: 1, end: 500, totalLines: 600, nextOffset: 501 },
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
const custom = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
|
|
573
|
+
path: "large.txt",
|
|
574
|
+
limit: 550,
|
|
575
|
+
});
|
|
576
|
+
assert.match(custom.content[0].text, /\n550#[0-9A-Z]+│line550/);
|
|
577
|
+
assert.doesNotMatch(custom.content[0].text, /\n551#[0-9A-Z]+│/);
|
|
578
|
+
assert.match(custom.content[0].text, /showing lines 1-550 of 600; use offset 551 to continue/);
|
|
579
|
+
assert.deepEqual(custom.details, {
|
|
580
|
+
pagination: { start: 1, end: 550, totalLines: 600, nextOffset: 551 },
|
|
581
|
+
});
|
|
582
|
+
}));
|
|
583
|
+
|
|
584
|
+
test("read pagination supports offset windows and stops suggesting continuation at EOF", async () =>
|
|
585
|
+
withDir(async (dir) => {
|
|
586
|
+
await writeFile(
|
|
587
|
+
join(dir, "pages.txt"),
|
|
588
|
+
Array.from({ length: 600 }, (_, i) => `line${i + 1}\n`).join(""),
|
|
589
|
+
);
|
|
590
|
+
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
591
|
+
const page = await call(read, { path: "pages.txt", offset: 20 });
|
|
592
|
+
assert.match(page.content[0].text, /showing lines 20-519 of 600; use offset 520 to continue/);
|
|
593
|
+
assert.deepEqual(page.details, {
|
|
594
|
+
pagination: { start: 20, end: 519, totalLines: 600, nextOffset: 520 },
|
|
595
|
+
});
|
|
596
|
+
const next = await call(read, {
|
|
597
|
+
path: "pages.txt",
|
|
598
|
+
offset: page.details.pagination.nextOffset,
|
|
599
|
+
});
|
|
600
|
+
assert.match(next.content[0].text, /\n520#[0-9A-Z]+│line520/);
|
|
601
|
+
assert.match(next.content[0].text, /\n600#[0-9A-Z]+│line600/);
|
|
602
|
+
assert.equal(next.details, undefined);
|
|
603
|
+
assert.doesNotMatch(next.content[0].text, /to continue/);
|
|
604
|
+
for (const offset of [101, 601]) {
|
|
605
|
+
const result = await call(read, { path: "pages.txt", offset });
|
|
606
|
+
assert.equal(result.details, undefined);
|
|
607
|
+
assert.doesNotMatch(result.content[0].text, /to continue/);
|
|
608
|
+
}
|
|
609
|
+
}));
|
|
610
|
+
|
|
611
|
+
test("read rejects noninteger and nonpositive offsets and limits before reading", async () =>
|
|
612
|
+
withDir(async (dir) => {
|
|
613
|
+
await writeFile(join(dir, "pages.txt"), "first\nsecond\nthird\n");
|
|
614
|
+
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
615
|
+
for (const key of ["offset", "limit"] as const) {
|
|
616
|
+
const schema = read.parameters.properties[key];
|
|
617
|
+
assert.equal(schema.type, "number");
|
|
618
|
+
assert.equal("minimum" in schema ? schema.minimum : undefined, 1);
|
|
619
|
+
assert.equal("multipleOf" in schema ? schema.multipleOf : undefined, 1);
|
|
620
|
+
assert.equal("maximum" in schema ? schema.maximum : undefined, Number.MAX_SAFE_INTEGER);
|
|
621
|
+
for (const invalid of [
|
|
622
|
+
0,
|
|
623
|
+
-4,
|
|
624
|
+
0.5,
|
|
625
|
+
1.5,
|
|
626
|
+
NaN,
|
|
627
|
+
Infinity,
|
|
628
|
+
-Infinity,
|
|
629
|
+
Number.MAX_SAFE_INTEGER + 1,
|
|
630
|
+
]) {
|
|
631
|
+
const params = { path: "pages.txt", [key]: invalid };
|
|
632
|
+
await assert.rejects(
|
|
633
|
+
call(read, params),
|
|
634
|
+
new RegExp(`Validation failed for tool "read":\\n {2}- ${key}: `),
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
assert.deepEqual(
|
|
639
|
+
validateToolArguments(read as any, {
|
|
640
|
+
type: "toolCall",
|
|
641
|
+
id: "valid",
|
|
642
|
+
name: "read",
|
|
643
|
+
arguments: { path: "pages.txt", offset: 2, limit: 550 },
|
|
644
|
+
}),
|
|
645
|
+
{ path: "pages.txt", offset: 2, limit: 550 },
|
|
646
|
+
);
|
|
647
|
+
await assert.rejects(
|
|
648
|
+
call(read, { path: "missing.txt", offset: 1.5 }),
|
|
649
|
+
/Validation failed for tool "read"/,
|
|
650
|
+
);
|
|
651
|
+
const page = await call(read, { path: "pages.txt", offset: 2, limit: 1 });
|
|
652
|
+
assert.match(page.content[0].text, /\n2#[0-9A-Z]+│second/);
|
|
653
|
+
assert.match(page.content[0].text, /showing lines 2-2 of 3; use offset 3 to continue/);
|
|
654
|
+
assert.deepEqual(page.details, {
|
|
655
|
+
pagination: { start: 2, end: 2, totalLines: 3, nextOffset: 3 },
|
|
656
|
+
});
|
|
657
|
+
await writeFile(join(dir, "long.txt"), "x".repeat(300 * 1024));
|
|
658
|
+
const long = await call(read, { path: "long.txt", offset: 1, limit: 1 });
|
|
659
|
+
assert.match(long.content[0].text, /line 1 exceeds 256 KiB/);
|
|
660
|
+
}));
|
|
661
|
+
|
|
662
|
+
test("read schema rejects empty paths and unknown fields before file access", async () =>
|
|
663
|
+
withDir(async (dir) => {
|
|
664
|
+
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
665
|
+
const invalidArgs: Parameters<typeof validateToolArguments>[1]["arguments"][] = [
|
|
666
|
+
{ path: "" },
|
|
667
|
+
{ path: "missing.txt", offest: 3 },
|
|
668
|
+
];
|
|
669
|
+
for (const args of invalidArgs) {
|
|
670
|
+
await assert.rejects(call(read, args), /Validation failed for tool "read"/);
|
|
671
|
+
}
|
|
672
|
+
}));
|
|
673
|
+
|
|
674
|
+
test("read byte truncation takes precedence over line pagination", async () =>
|
|
675
|
+
withDir(async (dir) => {
|
|
676
|
+
await writeFile(join(dir, "large.txt"), `first\n${"x".repeat(256 * 1024)}\ntail\n`);
|
|
677
|
+
const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
|
|
678
|
+
path: "large.txt",
|
|
679
|
+
limit: 2,
|
|
680
|
+
});
|
|
681
|
+
assert.equal(result.details.truncation.truncatedBy, "bytes");
|
|
682
|
+
assert.equal(result.details.pagination, undefined);
|
|
683
|
+
assert.match(result.content[0].text, /truncated at 256 KiB/);
|
|
684
|
+
assert.doesNotMatch(result.content[0].text, /showing lines|to continue/);
|
|
685
|
+
}));
|
|
686
|
+
|
|
687
|
+
test("native read and write renderers preserve resource titles, previews, and full errors", async () =>
|
|
688
|
+
withDir(async (dir) => {
|
|
689
|
+
const context = {
|
|
690
|
+
cwd: dir,
|
|
691
|
+
state: {},
|
|
692
|
+
argsComplete: true,
|
|
693
|
+
expanded: false,
|
|
694
|
+
isPartial: false,
|
|
695
|
+
lastComponent: undefined,
|
|
696
|
+
};
|
|
697
|
+
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
698
|
+
const readCall = read.renderCall!({ path: "SKILL.md", offset: 2, limit: 3 }, stubTheme, {
|
|
699
|
+
...context,
|
|
700
|
+
args: { path: "SKILL.md" },
|
|
701
|
+
} as Parameters<NonNullable<typeof read.renderCall>>[2]);
|
|
702
|
+
assert.match(readCall.render(120).join("\n"), /\[skill\]/);
|
|
703
|
+
const write = makeWriteOverride(dir);
|
|
704
|
+
const writeCall = write.renderCall(
|
|
705
|
+
{ path: "preview.txt", content: "native content preview\n" },
|
|
706
|
+
stubTheme,
|
|
707
|
+
context as Parameters<typeof write.renderCall>[2],
|
|
708
|
+
);
|
|
709
|
+
assert.match(writeCall.render(120).join("\n"), /native content preview/);
|
|
710
|
+
const error = write.renderResult(
|
|
711
|
+
{
|
|
712
|
+
content: [{ type: "text", text: "first error\nsecond error" }],
|
|
713
|
+
details: {
|
|
714
|
+
path: "preview.txt",
|
|
715
|
+
created: false,
|
|
716
|
+
publication: "NOT_PUBLISHED",
|
|
717
|
+
publishedRevision: "r",
|
|
718
|
+
observedRevision: "r",
|
|
719
|
+
},
|
|
720
|
+
},
|
|
721
|
+
{ isPartial: false, expanded: false },
|
|
722
|
+
stubTheme,
|
|
723
|
+
{ ...context, isError: true } as Parameters<typeof write.renderResult>[3],
|
|
724
|
+
);
|
|
725
|
+
assert.match(error.render(120).join("\n"), /first error[\s\S]*second error/);
|
|
726
|
+
}));
|
|
727
|
+
|
|
728
|
+
test("edit schema rejects misspelled range fields and invalid operation shapes", async () =>
|
|
729
|
+
withDir(async (dir) => {
|
|
730
|
+
const original = "a\nb\nc\n";
|
|
731
|
+
await writeFile(join(dir, "range.txt"), original);
|
|
732
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
733
|
+
const anchor = h(original, 2);
|
|
734
|
+
const callArgs = {
|
|
735
|
+
path: "range.txt",
|
|
736
|
+
edits: [{ op: "replace", anchor, endd: h(original, 3), body: ["merged"] }],
|
|
737
|
+
};
|
|
738
|
+
await assert.rejects(call(edit, callArgs), /edits\.0\.endd: schema is false/);
|
|
739
|
+
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
740
|
+
for (const edits of [
|
|
741
|
+
[],
|
|
742
|
+
[{ op: "replace", anchor }],
|
|
743
|
+
[{ op: "delete", anchor, body: ["bad"] }],
|
|
744
|
+
[{ op: "insert_after", body: ["bad"] }],
|
|
745
|
+
[{ op: "append", anchor, body: ["bad"] }],
|
|
746
|
+
]) {
|
|
747
|
+
await assert.rejects(call(edit, { path: "range.txt", edits }), /Validation failed/);
|
|
748
|
+
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
749
|
+
}
|
|
750
|
+
const alternates: Parameters<typeof validateToolArguments>[1]["arguments"][] = [
|
|
751
|
+
{ path: "range.txt", op: "replace", anchor, body: ["ok"] },
|
|
752
|
+
{ path: "range.txt", edits: JSON.stringify([{ op: "replace", anchor, body: ["ok"] }]) },
|
|
753
|
+
];
|
|
754
|
+
for (const alternate of alternates) {
|
|
755
|
+
await assert.rejects(call(edit, alternate), /Validation failed/);
|
|
756
|
+
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
757
|
+
}
|
|
758
|
+
}));
|
|
759
|
+
|
|
760
|
+
test("copied string anchors validate and replace an inclusive range", async () =>
|
|
761
|
+
withDir(async (dir) => {
|
|
762
|
+
const original = "a\nb\nc\nd\n";
|
|
763
|
+
await writeFile(join(dir, "range.txt"), original);
|
|
764
|
+
const read = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "range.txt" });
|
|
765
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
766
|
+
const args = validateToolArguments(edit as any, {
|
|
767
|
+
type: "toolCall",
|
|
768
|
+
id: "range",
|
|
769
|
+
name: "edit",
|
|
770
|
+
arguments: {
|
|
771
|
+
path: "range.txt",
|
|
772
|
+
edits: [
|
|
773
|
+
{
|
|
774
|
+
op: "replace",
|
|
775
|
+
anchor: anchorLine(read.content[0].text, 2),
|
|
776
|
+
end: anchorLine(read.content[0].text, 3),
|
|
777
|
+
body: ["merged"],
|
|
778
|
+
},
|
|
779
|
+
],
|
|
780
|
+
},
|
|
781
|
+
});
|
|
782
|
+
await call(edit, args);
|
|
783
|
+
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), "a\nmerged\nd\n");
|
|
784
|
+
}));
|
|
785
|
+
|
|
786
|
+
test("invalid anchors and conflicting fields fail before changing the file", async () =>
|
|
787
|
+
withDir(async (dir) => {
|
|
788
|
+
const original = "a\nb\n";
|
|
789
|
+
await writeFile(join(dir, "invalid.txt"), original);
|
|
790
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
791
|
+
const anchor = h(original, 1);
|
|
792
|
+
const invalid = [
|
|
793
|
+
{ op: "replace", anchor: { line: 1, hash: anchor.split("#")[1] }, body: ["changed"] },
|
|
794
|
+
...["0#ABCD", "-1#ABCD", "1.5#ABCD", "1#", "1#abcd", "1#ABCD│a", "9007199254740993#ABCD"].map(
|
|
795
|
+
(anchor) => ({ op: "replace", anchor, body: ["changed"] }),
|
|
796
|
+
),
|
|
797
|
+
{ op: "insert_after", anchor, end: anchor, body: ["changed"] },
|
|
798
|
+
{ op: "append", anchor, body: ["changed"] },
|
|
799
|
+
{ op: "delete", anchor, body: ["changed"] },
|
|
800
|
+
];
|
|
801
|
+
for (const operation of invalid) {
|
|
802
|
+
await assert.rejects(
|
|
803
|
+
call(edit, { path: "invalid.txt", edits: [operation] }),
|
|
804
|
+
/Invalid argument|Invalid anchor|Validation failed for tool "edit"/,
|
|
805
|
+
);
|
|
806
|
+
assert.equal(await readFile(join(dir, "invalid.txt"), "utf8"), original);
|
|
807
|
+
}
|
|
808
|
+
}));
|
|
809
|
+
|
|
810
|
+
test("edit execute accepts empty insertion bodies and empty replacements delete the anchored range", async () =>
|
|
811
|
+
withDir(async (dir) => {
|
|
812
|
+
const original = "a\nb\nc\n";
|
|
813
|
+
await writeFile(join(dir, "empty_body.txt"), original);
|
|
814
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
815
|
+
const anchor = h(original, 1);
|
|
816
|
+
for (const op of ["insert_after", "insert_before", "append", "prepend"] as const) {
|
|
817
|
+
const editOp =
|
|
818
|
+
op === "append" || op === "prepend" ? { op, body: [] } : { op, anchor, body: [] };
|
|
819
|
+
await call(edit, { path: "empty_body.txt", edits: [editOp] });
|
|
820
|
+
assert.equal(await readFile(join(dir, "empty_body.txt"), "utf8"), original);
|
|
821
|
+
}
|
|
822
|
+
const result = await call(edit, {
|
|
823
|
+
path: "empty_body.txt",
|
|
824
|
+
edits: [{ op: "replace", anchor, end: h(original, 2), body: [] }],
|
|
825
|
+
});
|
|
826
|
+
assert.equal(await readFile(join(dir, "empty_body.txt"), "utf8"), "c\n");
|
|
827
|
+
assert.match(result.content[0].text, /1#[0-9A-Z]+│c/);
|
|
828
|
+
}));
|
|
829
|
+
|
|
830
|
+
test("local and full-file recovery return copyable anchors without changing the rejected batch", async () =>
|
|
831
|
+
withDir(async (dir) => {
|
|
832
|
+
const original = "a\nb\n";
|
|
833
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
834
|
+
for (const prefixLines of [1, 40]) {
|
|
835
|
+
const prefix = "prefix\n".repeat(prefixLines);
|
|
836
|
+
await writeFile(join(dir, "shift.txt"), prefix + original);
|
|
837
|
+
let replacement = "";
|
|
838
|
+
await assert.rejects(
|
|
839
|
+
call(edit, {
|
|
840
|
+
path: "shift.txt",
|
|
841
|
+
edits: [{ op: "replace", anchor: h(original, 2), body: ["B"] }],
|
|
842
|
+
}),
|
|
843
|
+
(error: Error) => {
|
|
844
|
+
replacement =
|
|
845
|
+
new RegExp(`^(${prefixLines + 2}#[0-9A-Z]+)│b$`, "m").exec(error.message)?.[1] ?? "";
|
|
846
|
+
assert.match(error.message, /Check the intended target/);
|
|
847
|
+
assert.match(
|
|
848
|
+
error.message,
|
|
849
|
+
prefixLines === 1
|
|
850
|
+
? /Search: local; matches outside the window were not checked\./
|
|
851
|
+
: /Search: full file\./,
|
|
852
|
+
);
|
|
853
|
+
assert.doesNotMatch(
|
|
854
|
+
error.message,
|
|
855
|
+
prefixLines === 1 ? /Search: full file/ : /Search: local/,
|
|
856
|
+
);
|
|
857
|
+
assert.doesNotMatch(error.message, /neighborhoods/);
|
|
858
|
+
assert.equal((error.message.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 1);
|
|
859
|
+
return replacement !== "";
|
|
860
|
+
},
|
|
861
|
+
);
|
|
862
|
+
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), prefix + original);
|
|
863
|
+
await call(edit, {
|
|
864
|
+
path: "shift.txt",
|
|
865
|
+
edits: [{ op: "replace", anchor: replacement, body: ["B"] }],
|
|
866
|
+
});
|
|
867
|
+
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), prefix + "a\nB\n");
|
|
868
|
+
}
|
|
869
|
+
}));
|
|
870
|
+
|
|
871
|
+
test("shifted-anchor recovery keeps the read fallback for oversized candidates", async () =>
|
|
872
|
+
withDir(async (dir) => {
|
|
873
|
+
const oversized = "x".repeat(4 * 1024);
|
|
874
|
+
const original = `a\n${oversized}\n`;
|
|
875
|
+
await writeFile(join(dir, "shift-large.txt"), `prefix\n${original}`);
|
|
876
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
877
|
+
await assert.rejects(
|
|
878
|
+
call(edit, {
|
|
879
|
+
path: "shift-large.txt",
|
|
880
|
+
edits: [{ op: "replace", anchor: h(original, 2), body: ["B"] }],
|
|
881
|
+
}),
|
|
882
|
+
(error: Error) => {
|
|
883
|
+
assert.match(error.message, /Candidate content exceeds 4 KiB/);
|
|
884
|
+
assert.match(error.message, /use read or grep/);
|
|
885
|
+
assert.doesNotMatch(error.message, new RegExp(`│x{${oversized.length}}`));
|
|
886
|
+
return true;
|
|
887
|
+
},
|
|
888
|
+
);
|
|
889
|
+
}));
|
|
890
|
+
|
|
891
|
+
test("failed commands preserve mutation results and stay out of all main card renderers", async () =>
|
|
892
|
+
withDir(async (dir) => {
|
|
893
|
+
const commands: string[] = [];
|
|
894
|
+
const fusion = createActionFusionExecutor(
|
|
895
|
+
async () => {
|
|
896
|
+
throw new Error("command-only diagnostic\nCommand exited with code 7");
|
|
897
|
+
},
|
|
898
|
+
(event) => commands.push(event.command),
|
|
899
|
+
);
|
|
900
|
+
const cases = [
|
|
901
|
+
{
|
|
902
|
+
path: "edit.txt",
|
|
903
|
+
run: async () => {
|
|
904
|
+
const tool = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
|
|
905
|
+
const args = { path: "edit.txt", edits: [{ op: "append" as const, body: ["after"] }] };
|
|
906
|
+
const result = await callTool(
|
|
907
|
+
tool,
|
|
908
|
+
{ ...args, then_run: { command: "check" } },
|
|
909
|
+
{
|
|
910
|
+
toolCallId: args.path,
|
|
911
|
+
ctx: { cwd: dir },
|
|
912
|
+
},
|
|
913
|
+
);
|
|
914
|
+
return {
|
|
915
|
+
result,
|
|
916
|
+
rendered: tool
|
|
917
|
+
.renderResult(
|
|
918
|
+
result,
|
|
919
|
+
{ expanded: true, isPartial: false },
|
|
920
|
+
stubTheme as Theme,
|
|
921
|
+
{ args, state: {}, cwd: dir, isError: false } as Parameters<
|
|
922
|
+
typeof tool.renderResult
|
|
923
|
+
>[3],
|
|
924
|
+
)
|
|
925
|
+
.render(120)
|
|
926
|
+
.join("\n"),
|
|
927
|
+
};
|
|
928
|
+
},
|
|
929
|
+
expected: "before\nafter\n",
|
|
930
|
+
},
|
|
931
|
+
{
|
|
932
|
+
path: "replace.txt",
|
|
933
|
+
run: async () => {
|
|
934
|
+
const tool = makeReplaceTool(dir, DEFAULT_CONFIG, fusion);
|
|
935
|
+
const args = {
|
|
936
|
+
path: "replace.txt",
|
|
937
|
+
replacements: [{ find: "before", replace: "after" }],
|
|
938
|
+
};
|
|
939
|
+
const result = await callTool(
|
|
940
|
+
tool,
|
|
941
|
+
{ ...args, then_run: { command: "check" } },
|
|
942
|
+
{
|
|
943
|
+
toolCallId: args.path,
|
|
944
|
+
ctx: { cwd: dir },
|
|
945
|
+
},
|
|
946
|
+
);
|
|
947
|
+
return {
|
|
948
|
+
result,
|
|
949
|
+
rendered: tool
|
|
950
|
+
.renderResult(
|
|
951
|
+
result,
|
|
952
|
+
{ expanded: true, isPartial: false },
|
|
953
|
+
stubTheme as Theme,
|
|
954
|
+
{ args, state: {}, cwd: dir, isError: false } as Parameters<
|
|
955
|
+
typeof tool.renderResult
|
|
956
|
+
>[3],
|
|
957
|
+
)
|
|
958
|
+
.render(120)
|
|
959
|
+
.join("\n"),
|
|
960
|
+
};
|
|
961
|
+
},
|
|
962
|
+
expected: "after\n",
|
|
963
|
+
},
|
|
964
|
+
{
|
|
965
|
+
path: "write.txt",
|
|
966
|
+
run: async () => {
|
|
967
|
+
const tool = makeWriteOverride(dir, fusion);
|
|
968
|
+
const args = { path: "write.txt", content: "after\n" };
|
|
969
|
+
const result = await callTool(
|
|
970
|
+
tool,
|
|
971
|
+
{ ...args, then_run: { command: "check" } },
|
|
972
|
+
{
|
|
973
|
+
toolCallId: args.path,
|
|
974
|
+
ctx: { cwd: dir },
|
|
975
|
+
},
|
|
976
|
+
);
|
|
977
|
+
return {
|
|
978
|
+
result,
|
|
979
|
+
rendered: tool
|
|
980
|
+
.renderResult(
|
|
981
|
+
result,
|
|
982
|
+
{ expanded: true, isPartial: false },
|
|
983
|
+
stubTheme as Theme,
|
|
984
|
+
{ args, state: {}, cwd: dir, isError: false } as Parameters<
|
|
985
|
+
typeof tool.renderResult
|
|
986
|
+
>[3],
|
|
987
|
+
)
|
|
988
|
+
.render(120)
|
|
989
|
+
.join("\n"),
|
|
990
|
+
};
|
|
991
|
+
},
|
|
992
|
+
expected: "after\n",
|
|
993
|
+
},
|
|
994
|
+
];
|
|
995
|
+
for (const { path, run, expected } of cases) {
|
|
996
|
+
await writeFile(join(dir, path), "before\n");
|
|
997
|
+
const { result, rendered } = await run();
|
|
998
|
+
assert.equal("isError" in result, false);
|
|
999
|
+
assert.equal(result.details.actionFusion?.command, "failed");
|
|
1000
|
+
const last = result.content.at(-1);
|
|
1001
|
+
assert.ok(last?.type === "text");
|
|
1002
|
+
assert.match(last.text, /then_run:failed[\s\S]*command-only diagnostic/);
|
|
1003
|
+
if (path !== "write.txt")
|
|
1004
|
+
assert.equal(typeof ("diff" in result.details ? result.details.diff : undefined), "string");
|
|
1005
|
+
assert.doesNotMatch(rendered, /command-only diagnostic|then_run:failed/);
|
|
1006
|
+
assert.equal(await readFile(join(dir, path), "utf8"), expected);
|
|
1007
|
+
}
|
|
1008
|
+
assert.deepEqual(
|
|
1009
|
+
commands,
|
|
1010
|
+
cases.flatMap(() => ["waiting", "waiting", "running", "failed"]),
|
|
1011
|
+
);
|
|
1012
|
+
}));
|
|
1013
|
+
|
|
1014
|
+
test("text tools reject malformed UTF-8 and NUL bytes without rewriting source bytes", async () =>
|
|
1015
|
+
withDir(async (dir) => {
|
|
1016
|
+
const target = join(dir, "invalid-utf8.txt");
|
|
1017
|
+
const original = Buffer.from([0x61, 0x0a, 0xc3, 0x28, 0x0a]);
|
|
1018
|
+
await writeFile(target, original);
|
|
1019
|
+
await assert.rejects(
|
|
1020
|
+
call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "invalid-utf8.txt" }),
|
|
1021
|
+
/UNSUPPORTED_ENCODING/,
|
|
1022
|
+
);
|
|
1023
|
+
await assert.rejects(
|
|
1024
|
+
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1025
|
+
path: "invalid-utf8.txt",
|
|
1026
|
+
edits: [{ op: "append", body: ["x"] }],
|
|
1027
|
+
}),
|
|
1028
|
+
/UNSUPPORTED_ENCODING/,
|
|
1029
|
+
);
|
|
1030
|
+
await assert.rejects(
|
|
1031
|
+
call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
1032
|
+
path: "invalid-utf8.txt",
|
|
1033
|
+
replacements: [{ find: "a", replace: "b" }],
|
|
1034
|
+
}),
|
|
1035
|
+
/UNSUPPORTED_ENCODING/,
|
|
1036
|
+
);
|
|
1037
|
+
assert.deepEqual(await readFile(target), original);
|
|
1038
|
+
|
|
1039
|
+
const nulTarget = join(dir, "nul.txt");
|
|
1040
|
+
const nulOriginal = Buffer.from([0x61, 0x00, 0x62]);
|
|
1041
|
+
await writeFile(nulTarget, nulOriginal);
|
|
1042
|
+
await assert.rejects(
|
|
1043
|
+
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1044
|
+
path: "nul.txt",
|
|
1045
|
+
edits: [{ op: "append", body: ["x"] }],
|
|
1046
|
+
}),
|
|
1047
|
+
/UNSUPPORTED_TEXT/,
|
|
1048
|
+
);
|
|
1049
|
+
await assert.rejects(
|
|
1050
|
+
call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
1051
|
+
path: "nul.txt",
|
|
1052
|
+
replacements: [{ find: "a", replace: "b" }],
|
|
1053
|
+
}),
|
|
1054
|
+
/UNSUPPORTED_TEXT/,
|
|
1055
|
+
);
|
|
1056
|
+
assert.deepEqual(await readFile(nulTarget), nulOriginal);
|
|
1057
|
+
|
|
1058
|
+
const nativeRead: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "nul.txt" });
|
|
1059
|
+
assert.equal(nativeRead.content[0].text, "a\0b");
|
|
1060
|
+
|
|
1061
|
+
const utf16Target = join(dir, "utf16.txt");
|
|
1062
|
+
await writeFile(utf16Target, Buffer.from([0xff, 0xfe, 0x49, 0x6c]));
|
|
1063
|
+
await assert.rejects(
|
|
1064
|
+
call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "utf16.txt" }),
|
|
1065
|
+
/UNSUPPORTED_ENCODING/,
|
|
1066
|
+
);
|
|
1067
|
+
}));
|
|
1068
|
+
|
|
1069
|
+
test("edit rejects embedded line terminators at its schema boundary", async () =>
|
|
1070
|
+
withDir(async (dir) => {
|
|
1071
|
+
const target = join(dir, "body.txt");
|
|
1072
|
+
await writeFile(target, "a\n");
|
|
1073
|
+
for (const line of ["x\ny", "x\ry"]) {
|
|
1074
|
+
await assert.rejects(
|
|
1075
|
+
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1076
|
+
path: "body.txt",
|
|
1077
|
+
edits: [{ op: "append", body: [line] }],
|
|
1078
|
+
}),
|
|
1079
|
+
/Validation failed for tool "edit"/,
|
|
1080
|
+
);
|
|
1081
|
+
assert.equal(await readFile(target, "utf8"), "a\n");
|
|
1082
|
+
}
|
|
1083
|
+
}));
|
|
1084
|
+
|
|
1085
|
+
test("edit rejects unwritable body lines before reading a missing target", async () =>
|
|
1086
|
+
withDir(async (dir) => {
|
|
1087
|
+
const target = join(dir, "missing.txt");
|
|
1088
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1089
|
+
const valid = { path: target, edits: [{ op: "append" as const, body: ["ok"] }] };
|
|
1090
|
+
assert.equal(edit.prepareArguments(valid), valid);
|
|
1091
|
+
for (const [line, expected] of [
|
|
1092
|
+
["bad\0", /Invalid argument edits\[0\]\.body\[1\]: UNSUPPORTED_TEXT:/],
|
|
1093
|
+
["\ud800", /Invalid argument edits\[0\]\.body\[1\]: INVALID_UNICODE:/],
|
|
1094
|
+
] as const) {
|
|
1095
|
+
await assert.rejects(
|
|
1096
|
+
call(edit, { path: target, edits: [{ op: "append", body: ["ok", line] }] }),
|
|
1097
|
+
expected,
|
|
1098
|
+
);
|
|
1099
|
+
await assert.rejects(readFile(target, "utf8"), { code: "ENOENT" });
|
|
1100
|
+
}
|
|
1101
|
+
}));
|
|
1102
|
+
|
|
1103
|
+
test("edit preserves a UTF-8 BOM and reports bound mutation revisions", async () =>
|
|
1104
|
+
withDir(async (dir) => {
|
|
1105
|
+
const target = join(dir, "bom.txt");
|
|
1106
|
+
const original = Buffer.from("\ufeffguard\nold\n", "utf8");
|
|
1107
|
+
await writeFile(target, original);
|
|
1108
|
+
const result: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1109
|
+
path: "bom.txt",
|
|
1110
|
+
edits: [{ op: "replace", anchor: h("\ufeffguard\nold\n", 2), body: ["new"] }],
|
|
1111
|
+
});
|
|
1112
|
+
const expected = Buffer.from("\ufeffguard\nnew\n", "utf8");
|
|
1113
|
+
assert.deepEqual(await readFile(target), expected);
|
|
1114
|
+
assert.equal(result.details.baseRevision, byteRevision(original));
|
|
1115
|
+
assert.equal(result.details.publishedRevision, byteRevision(expected));
|
|
1116
|
+
assert.equal(result.details.observedRevision, result.details.publishedRevision);
|
|
1117
|
+
assert.equal("revision" in result.details, false);
|
|
1118
|
+
}));
|
|
1119
|
+
|
|
1120
|
+
test("edit and replace reject NUL arguments without rewriting source bytes", async () =>
|
|
1121
|
+
withDir(async (dir) => {
|
|
1122
|
+
const target = join(dir, "output.txt");
|
|
1123
|
+
const original = Buffer.from("\ufeffbefore\r\n", "utf8");
|
|
1124
|
+
await writeFile(target, original);
|
|
1125
|
+
await assert.rejects(
|
|
1126
|
+
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1127
|
+
path: "output.txt",
|
|
1128
|
+
edits: [{ op: "append", body: ["bad\0text"] }],
|
|
1129
|
+
}),
|
|
1130
|
+
/UNSUPPORTED_TEXT/,
|
|
1131
|
+
);
|
|
1132
|
+
assert.deepEqual(await readFile(target), original);
|
|
1133
|
+
await assert.rejects(
|
|
1134
|
+
call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
1135
|
+
path: "output.txt",
|
|
1136
|
+
replacements: [{ find: "before", replace: "bad\0text" }],
|
|
1137
|
+
}),
|
|
1138
|
+
/UNSUPPORTED_TEXT/,
|
|
1139
|
+
);
|
|
1140
|
+
assert.deepEqual(await readFile(target), original);
|
|
1141
|
+
}));
|
|
1142
|
+
|
|
1143
|
+
test("failed edits expose input status and candidate code for a verified fused retry", async () =>
|
|
1144
|
+
withDir(async (dir) => {
|
|
1145
|
+
const original = Array.from({ length: 14 }, (_, index) => `line-${index + 1}`);
|
|
1146
|
+
original[4] = "if (!fusion) throw new Error();";
|
|
1147
|
+
original[5] = "const absolutePath = canonicalPath(cwd, path);";
|
|
1148
|
+
const before = `prefix\n${original.join("\n")}\n`;
|
|
1149
|
+
const file = join(dir, "candidate.txt");
|
|
1150
|
+
await writeFile(file, before);
|
|
1151
|
+
let commands = 0;
|
|
1152
|
+
const fusion = createActionFusionExecutor(async () => {
|
|
1153
|
+
commands++;
|
|
1154
|
+
return "checked";
|
|
1155
|
+
});
|
|
1156
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
|
|
1157
|
+
const stale = h(original.join("\n"), 5);
|
|
1158
|
+
const stable = h(before, 12);
|
|
1159
|
+
const candidate = h(before, 6);
|
|
1160
|
+
let message = "";
|
|
1161
|
+
await assert.rejects(
|
|
1162
|
+
callTool(
|
|
1163
|
+
edit,
|
|
1164
|
+
{
|
|
1165
|
+
path: file,
|
|
1166
|
+
edits: [
|
|
1167
|
+
{ op: "insert_after", anchor: stale, body: ["const hashLen = 4;"] },
|
|
1168
|
+
{ op: "replace", anchor: stable, body: ["changed"] },
|
|
1169
|
+
],
|
|
1170
|
+
then_run: { command: "check" },
|
|
1171
|
+
},
|
|
1172
|
+
{ toolCallId: "failed", ctx: { cwd: dir } },
|
|
1173
|
+
),
|
|
1174
|
+
(error: Error) => {
|
|
1175
|
+
message = error.message;
|
|
1176
|
+
return true;
|
|
1177
|
+
},
|
|
1178
|
+
);
|
|
1179
|
+
assert.match(message, /then_run:skipped/);
|
|
1180
|
+
assert.ok(message.includes(`op 0 / anchor / ${stale} / mismatched`));
|
|
1181
|
+
assert.ok(message.includes(`op 1 / anchor / ${stable} / matched`));
|
|
1182
|
+
assert.ok(message.includes(`checksum-matching candidate ${candidate}.`));
|
|
1183
|
+
assert.equal(message.split(`${candidate}│${original[4]}`).length - 1, 1);
|
|
1184
|
+
assert.doesNotMatch(message, /0 omitted|Limits:/);
|
|
1185
|
+
assert.doesNotMatch(message, /neighborhoods|Current-file context/);
|
|
1186
|
+
assert.equal((message.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 1);
|
|
1187
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
1188
|
+
assert.equal(commands, 0);
|
|
1189
|
+
await callTool(
|
|
1190
|
+
edit,
|
|
1191
|
+
{
|
|
1192
|
+
path: file,
|
|
1193
|
+
edits: [
|
|
1194
|
+
{ op: "insert_after", anchor: anchorLine(message, 6), body: ["const hashLen = 4;"] },
|
|
1195
|
+
{ op: "replace", anchor: stable, body: ["changed"] },
|
|
1196
|
+
],
|
|
1197
|
+
then_run: { command: "check" },
|
|
1198
|
+
},
|
|
1199
|
+
{ toolCallId: "retry", ctx: { cwd: dir } },
|
|
1200
|
+
);
|
|
1201
|
+
const expected = splitLines(before);
|
|
1202
|
+
expected[11] = "changed";
|
|
1203
|
+
expected.splice(6, 0, "const hashLen = 4;");
|
|
1204
|
+
assert.equal(await readFile(file, "utf8"), `${expected.join("\n")}\n`);
|
|
1205
|
+
assert.equal(commands, 1);
|
|
1206
|
+
}));
|
|
1207
|
+
|
|
1208
|
+
test("ambiguous candidates include distinguishing neighborhoods for a verified retry", async () =>
|
|
1209
|
+
withDir(async (dir) => {
|
|
1210
|
+
const lines = Array.from({ length: 20 }, (_, index) => `line-${index + 1}`);
|
|
1211
|
+
lines[3] = "function primary() {";
|
|
1212
|
+
lines[4] = " return value;";
|
|
1213
|
+
lines[13] = "function secondary() {";
|
|
1214
|
+
lines[14] = " return value;";
|
|
1215
|
+
const before = lines.join("\n") + "\n";
|
|
1216
|
+
const file = join(dir, "ambiguous.txt");
|
|
1217
|
+
await writeFile(file, before);
|
|
1218
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1219
|
+
let context = "";
|
|
1220
|
+
await assert.rejects(
|
|
1221
|
+
call(edit, {
|
|
1222
|
+
path: file,
|
|
1223
|
+
edits: [
|
|
1224
|
+
{ op: "replace", anchor: h("header\n return value;\n", 2), body: [" return updated;"] },
|
|
1225
|
+
],
|
|
1226
|
+
}),
|
|
1227
|
+
(error: Error) => {
|
|
1228
|
+
assert.match(error.message, /ambiguous checksum matches/);
|
|
1229
|
+
assert.match(error.message, /Search: local; matches outside the window were not checked\./);
|
|
1230
|
+
assert.ok(error.message.includes(`"${h(before, 5)}" / "${h(before, 15)}"`));
|
|
1231
|
+
context = error.message.split("Ambiguous-candidate neighborhoods")[1];
|
|
1232
|
+
assert.ok(context);
|
|
1233
|
+
assert.match(context, /@@ candidate-neighborhood lines 2-8 @@/);
|
|
1234
|
+
assert.match(context, /@@ candidate-neighborhood lines 12-18 @@/);
|
|
1235
|
+
assert.ok(context.includes(`${h(before, 4)}│function primary() {`));
|
|
1236
|
+
assert.ok(context.includes(`${h(before, 14)}│function secondary() {`));
|
|
1237
|
+
assert.equal(anchorLine(context, 5), h(before, 5));
|
|
1238
|
+
return true;
|
|
1239
|
+
},
|
|
1240
|
+
);
|
|
1241
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
1242
|
+
await call(edit, {
|
|
1243
|
+
path: file,
|
|
1244
|
+
edits: [{ op: "replace", anchor: anchorLine(context, 15), body: [" return updated;"] }],
|
|
1245
|
+
});
|
|
1246
|
+
lines[14] = " return updated;";
|
|
1247
|
+
assert.equal(await readFile(file, "utf8"), lines.join("\n") + "\n");
|
|
1248
|
+
}));
|
|
1249
|
+
|
|
1250
|
+
test("schema-invalid bodies omit anchor checks; subsequent retries revalidate", async () =>
|
|
1251
|
+
withDir(async (dir) => {
|
|
1252
|
+
const before = "a\nb\nc\n";
|
|
1253
|
+
const file = join(dir, "checks.txt");
|
|
1254
|
+
await writeFile(file, before);
|
|
1255
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1256
|
+
const stable = h(before, 1);
|
|
1257
|
+
await assert.rejects(
|
|
1258
|
+
call(edit, {
|
|
1259
|
+
path: file,
|
|
1260
|
+
edits: [
|
|
1261
|
+
{ op: "replace", anchor: stable, body: ["bad\nline"] },
|
|
1262
|
+
{ op: "replace", anchor: "2#XXXX", body: ["B"] },
|
|
1263
|
+
],
|
|
1264
|
+
}),
|
|
1265
|
+
(error: Error) => {
|
|
1266
|
+
assert.match(error.message, /Validation failed for tool "edit"/);
|
|
1267
|
+
assert.doesNotMatch(error.message, /Input-anchor checks|\/ matched|\/ mismatched/);
|
|
1268
|
+
return true;
|
|
1269
|
+
},
|
|
1270
|
+
);
|
|
1271
|
+
await assert.rejects(
|
|
1272
|
+
call(edit, {
|
|
1273
|
+
path: file,
|
|
1274
|
+
edits: [
|
|
1275
|
+
{ op: "replace", anchor: stable, body: ["A"] },
|
|
1276
|
+
{ op: "delete", anchor: h(before, 2), end: "3#ZZZZ" },
|
|
1277
|
+
],
|
|
1278
|
+
}),
|
|
1279
|
+
(error: Error) => {
|
|
1280
|
+
assert.ok(error.message.includes(`op 0 / anchor / ${stable} / matched`));
|
|
1281
|
+
assert.match(error.message, /op 1 \/ end \/ 3#ZZZZ \/ mismatched/);
|
|
1282
|
+
assert.match(error.message, /Anchor checks only; retries revalidate/);
|
|
1283
|
+
return true;
|
|
1284
|
+
},
|
|
1285
|
+
);
|
|
1286
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
1287
|
+
await writeFile(file, "changed\nb\nc\n");
|
|
1288
|
+
await assert.rejects(
|
|
1289
|
+
call(edit, {
|
|
1290
|
+
path: file,
|
|
1291
|
+
edits: [
|
|
1292
|
+
{ op: "replace", anchor: stable, body: ["A"] },
|
|
1293
|
+
{ op: "replace", anchor: "3#ZZZZ", body: ["B"] },
|
|
1294
|
+
],
|
|
1295
|
+
}),
|
|
1296
|
+
(error: Error) => {
|
|
1297
|
+
assert.ok(error.message.includes(`op 0 / anchor / ${stable} / mismatched`));
|
|
1298
|
+
return true;
|
|
1299
|
+
},
|
|
1300
|
+
);
|
|
1301
|
+
assert.equal(await readFile(file, "utf8"), "changed\nb\nc\n");
|
|
1302
|
+
}));
|
|
1303
|
+
|
|
1304
|
+
test("single-operation edit failures omit redundant Input-anchor checks table", async () =>
|
|
1305
|
+
withDir(async (dir) => {
|
|
1306
|
+
const file = join(dir, "single.txt");
|
|
1307
|
+
await writeFile(file, "line1\nline2\n");
|
|
1308
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1309
|
+
await assert.rejects(
|
|
1310
|
+
call(edit, { path: file, edits: [{ op: "replace", anchor: "1#XXXX", body: ["new"] }] }),
|
|
1311
|
+
(error: Error) => {
|
|
1312
|
+
assert.match(error.message, /Anchor mismatch: 1 unresolved/);
|
|
1313
|
+
assert.match(error.message, /no checksum-matching candidate found/);
|
|
1314
|
+
assert.doesNotMatch(error.message, /Input-anchor checks/);
|
|
1315
|
+
return true;
|
|
1316
|
+
},
|
|
1317
|
+
);
|
|
1318
|
+
}));
|
|
1319
|
+
|
|
1320
|
+
test("single-op range edits with two anchors omit Input-anchor checks table on failure", async () =>
|
|
1321
|
+
withDir(async (dir) => {
|
|
1322
|
+
const file = join(dir, "range.txt");
|
|
1323
|
+
await writeFile(file, "line1\nline2\nline3\n");
|
|
1324
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1325
|
+
await assert.rejects(
|
|
1326
|
+
call(edit, {
|
|
1327
|
+
path: file,
|
|
1328
|
+
edits: [{ op: "replace", anchor: "1#XXXX", end: "2#YYYY", body: ["new"] }],
|
|
1329
|
+
}),
|
|
1330
|
+
(error: Error) => {
|
|
1331
|
+
assert.match(error.message, /Anchor mismatch/);
|
|
1332
|
+
assert.doesNotMatch(error.message, /Input-anchor checks/);
|
|
1333
|
+
return true;
|
|
1334
|
+
},
|
|
1335
|
+
);
|
|
1336
|
+
}));
|
|
1337
|
+
test("multi-op schema failures omit anchor checks even when later ops have anchors", async () =>
|
|
1338
|
+
withDir(async (dir) => {
|
|
1339
|
+
const file = join(dir, "batch-append.txt");
|
|
1340
|
+
await writeFile(file, "line1\nline2\n");
|
|
1341
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1342
|
+
const stable = h("line1\nline2\n", 1);
|
|
1343
|
+
// Static body validation occurs before snapshot-based anchor verification.
|
|
1344
|
+
await assert.rejects(
|
|
1345
|
+
call(edit, {
|
|
1346
|
+
path: file,
|
|
1347
|
+
edits: [
|
|
1348
|
+
{ op: "append", body: ["bad\nline"] },
|
|
1349
|
+
{ op: "replace", anchor: stable, body: ["new"] },
|
|
1350
|
+
],
|
|
1351
|
+
}),
|
|
1352
|
+
(error: Error) => {
|
|
1353
|
+
assert.match(error.message, /Validation failed for tool "edit"/);
|
|
1354
|
+
assert.doesNotMatch(error.message, /Input-anchor checks/);
|
|
1355
|
+
return true;
|
|
1356
|
+
},
|
|
1357
|
+
);
|
|
1358
|
+
assert.equal(await readFile(file, "utf8"), "line1\nline2\n");
|
|
1359
|
+
}));
|
|
1360
|
+
|
|
1361
|
+
test("failed batches return more than forty checks and mappings when byte budgets allow", async () =>
|
|
1362
|
+
withDir(async (dir) => {
|
|
1363
|
+
const original = "a\ntarget\nz\n";
|
|
1364
|
+
const before = `prefix\n${original}`;
|
|
1365
|
+
const file = join(dir, "many-checks.txt");
|
|
1366
|
+
await writeFile(file, before);
|
|
1367
|
+
const edits = Array.from({ length: 45 }, () => ({
|
|
1368
|
+
op: "replace",
|
|
1369
|
+
anchor: h(original, 2),
|
|
1370
|
+
body: ["changed"],
|
|
1371
|
+
}));
|
|
1372
|
+
await assert.rejects(
|
|
1373
|
+
call(makeEditOverride(dir, DEFAULT_CONFIG), { path: file, edits }),
|
|
1374
|
+
(error: Error) => {
|
|
1375
|
+
assert.doesNotMatch(
|
|
1376
|
+
error.message,
|
|
1377
|
+
/truncated|failure details omitted|Anchor checks: \d+\/\d+/,
|
|
1378
|
+
);
|
|
1379
|
+
assert.equal(
|
|
1380
|
+
(error.message.match(/^op \d+ \/ anchor \/ .* \/ mismatched$/gm) ?? []).length,
|
|
1381
|
+
45,
|
|
1382
|
+
);
|
|
1383
|
+
assert.equal((error.message.match(/checksum-matching candidate/g) ?? []).length, 45);
|
|
1384
|
+
assert.equal((error.message.match(/^3#[0-9A-Z]+│target$/gm) ?? []).length, 1);
|
|
1385
|
+
assert.doesNotMatch(error.message, /\/ matched/);
|
|
1386
|
+
return true;
|
|
1387
|
+
},
|
|
1388
|
+
);
|
|
1389
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
1390
|
+
}));
|
|
1391
|
+
|
|
1392
|
+
test("compact edit anchors retain only untouched deletion successors in mixed batches", async () =>
|
|
1393
|
+
withDir(async (dir) => {
|
|
1394
|
+
const before = "a\nb\nc\nd\ne\nf\ng\n";
|
|
1395
|
+
const file = join(dir, "mixed.txt");
|
|
1396
|
+
await writeFile(file, before);
|
|
1397
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1398
|
+
const result = await call(edit, {
|
|
1399
|
+
path: file,
|
|
1400
|
+
edits: [
|
|
1401
|
+
{ op: "delete", anchor: h(before, 6) },
|
|
1402
|
+
{ op: "replace", anchor: h(before, 4), body: ["D"] },
|
|
1403
|
+
{ op: "delete", anchor: h(before, 3) },
|
|
1404
|
+
{ op: "replace", anchor: h(before, 1), body: ["A", "X"] },
|
|
1405
|
+
],
|
|
1406
|
+
});
|
|
1407
|
+
const after = "A\nX\nb\nD\ne\ng\n";
|
|
1408
|
+
assert.equal(await readFile(file, "utf8"), after);
|
|
1409
|
+
const output = result.content[0].text;
|
|
1410
|
+
assert.deepEqual(
|
|
1411
|
+
output.split("\n").filter((line: string) => /^\d+#/.test(line)),
|
|
1412
|
+
[h(after, 1), h(after, 2), h(after, 4), `${h(after, 6)}│g`],
|
|
1413
|
+
);
|
|
1414
|
+
await call(edit, {
|
|
1415
|
+
path: file,
|
|
1416
|
+
edits: [{ op: "replace", anchor: anchorLine(output, 6), body: ["G"] }],
|
|
1417
|
+
});
|
|
1418
|
+
assert.equal(await readFile(file, "utf8"), "A\nX\nb\nD\ne\nG\n");
|
|
1419
|
+
}));
|
|
1420
|
+
|
|
1421
|
+
test("unique candidate content is independent of oversized neighboring rows", async () =>
|
|
1422
|
+
withDir(async (dir) => {
|
|
1423
|
+
const original = "header\ntarget\ntail\n";
|
|
1424
|
+
const before = `header\n${"x".repeat(17000)}\ntarget\ntail\n`;
|
|
1425
|
+
const file = join(dir, "fallback.txt");
|
|
1426
|
+
await writeFile(file, before);
|
|
1427
|
+
let candidate = "";
|
|
1428
|
+
await assert.rejects(
|
|
1429
|
+
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1430
|
+
path: file,
|
|
1431
|
+
edits: [{ op: "replace", anchor: h(original, 2), body: ["updated"] }],
|
|
1432
|
+
}),
|
|
1433
|
+
(error: Error) => {
|
|
1434
|
+
candidate = anchorLine(error.message, 3);
|
|
1435
|
+
assert.equal((error.message.match(/^3#[0-9A-Z]+│target$/gm) ?? []).length, 1);
|
|
1436
|
+
assert.doesNotMatch(error.message, /neighborhoods|truncated/);
|
|
1437
|
+
assert.equal((error.message.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 1);
|
|
1438
|
+
return true;
|
|
1439
|
+
},
|
|
1440
|
+
);
|
|
1441
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
1442
|
+
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1443
|
+
path: file,
|
|
1444
|
+
edits: [{ op: "replace", anchor: candidate, body: ["updated"] }],
|
|
1445
|
+
});
|
|
1446
|
+
assert.equal(await readFile(file, "utf8"), before.replace("target", "updated"));
|
|
1447
|
+
}));
|
|
1448
|
+
|
|
1449
|
+
test("standalone CR replacements remain visible in diffs and exact in patches", async () =>
|
|
1450
|
+
withDir(async (dir) => {
|
|
1451
|
+
const file = join(dir, "cr.txt");
|
|
1452
|
+
for (const [before, find, replacement] of [
|
|
1453
|
+
["a\rb\n", "\r", "\n"],
|
|
1454
|
+
["a\rb\n", "\r", "␍"],
|
|
1455
|
+
]) {
|
|
1456
|
+
await writeFile(file, before);
|
|
1457
|
+
const result = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
|
|
1458
|
+
path: file,
|
|
1459
|
+
replacements: [{ find, replace: replacement, regex: true }],
|
|
1460
|
+
});
|
|
1461
|
+
assert.equal(await readFile(file, "utf8"), before.replaceAll(find, replacement));
|
|
1462
|
+
assert.equal(result.details.firstChangedLine, 1);
|
|
1463
|
+
assert.match(result.details.diff, /^-1 a␍/m);
|
|
1464
|
+
assert.match(result.details.diff, /^\+1 a/m);
|
|
1465
|
+
assert.ok(!result.details.diff.includes("\r"));
|
|
1466
|
+
assert.ok(result.details.patch.includes(`-a${before.includes("\r\n") ? "\r\n" : "\rb\n"}`));
|
|
1467
|
+
assert.match(result.details.patch, /@@/);
|
|
1468
|
+
}
|
|
1469
|
+
}));
|
|
1470
|
+
|
|
1471
|
+
test("deletion successors and unique candidate rows display CR without altering anchors", async () =>
|
|
1472
|
+
withDir(async (dir) => {
|
|
1473
|
+
const file = join(dir, "cr.txt");
|
|
1474
|
+
const before = "remove\na\rb\n";
|
|
1475
|
+
await writeFile(file, before);
|
|
1476
|
+
const result = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1477
|
+
path: file,
|
|
1478
|
+
edits: [{ op: "delete", anchor: h(before, 1) }],
|
|
1479
|
+
});
|
|
1480
|
+
assert.ok(result.content[0].text.includes(`${h("a\rb\n", 1)}│a␍b`));
|
|
1481
|
+
assert.equal(await readFile(file, "utf8"), "a\rb\n");
|
|
1482
|
+
await assert.rejects(
|
|
1483
|
+
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1484
|
+
path: file,
|
|
1485
|
+
edits: [{ op: "delete", anchor: h(before, 2) }],
|
|
1486
|
+
}),
|
|
1487
|
+
(error: Error) => {
|
|
1488
|
+
assert.ok(error.message.includes(`${h("a\rb\n", 1)}│a␍b`));
|
|
1489
|
+
assert.ok(!error.message.includes("\r"));
|
|
1490
|
+
return true;
|
|
1491
|
+
},
|
|
1492
|
+
);
|
|
1493
|
+
}));
|
|
1494
|
+
|
|
1495
|
+
test("read bounds oversized selected lines while preserving truncation metadata and later anchors", async () =>
|
|
1496
|
+
withDir(async (dir) => {
|
|
1497
|
+
const long = "界".repeat(200_000);
|
|
1498
|
+
await writeFile(join(dir, "long.txt"), `first\r\n${long}\r\nlast`);
|
|
1499
|
+
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
1500
|
+
const result = await call(read, { path: "long.txt" });
|
|
1501
|
+
assert.match(result.content[0].text, /1#[0-9A-Z]+│first/);
|
|
1502
|
+
assert.doesNotMatch(result.content[0].text, /2#[0-9A-Z]+│/);
|
|
1503
|
+
assert.equal(result.details.truncation.firstLineExceedsLimit, false);
|
|
1504
|
+
assert.equal(result.details.truncation.outputLines, 1);
|
|
1505
|
+
const expectedRows = ["first", long, "last"].map(
|
|
1506
|
+
(text, index) => `${index + 1}#${computeLineHash(index + 1, text, 4)}│${text}`,
|
|
1507
|
+
);
|
|
1508
|
+
assert.equal(result.details.truncation.totalBytes, Buffer.byteLength(expectedRows.join("\n")));
|
|
1509
|
+
const oversized = await call(read, { path: "long.txt", offset: 2, limit: 1 });
|
|
1510
|
+
assert.equal(oversized.details.truncation.firstLineExceedsLimit, true);
|
|
1511
|
+
assert.equal(oversized.details.truncation.outputBytes, 0);
|
|
1512
|
+
const last = await call(read, { path: "long.txt", offset: 3, limit: 1 });
|
|
1513
|
+
assert.match(last.content[0].text, /3#[0-9A-Z]+│last/);
|
|
1514
|
+
assert.match(last.content[0].text, /no trailing newline/);
|
|
1515
|
+
}));
|
|
1516
|
+
|
|
1517
|
+
test("read budgets visible standalone CR characters using rendered UTF-8 bytes", async () =>
|
|
1518
|
+
withDir(async (dir) => {
|
|
1519
|
+
const content = "\r".repeat(90_000);
|
|
1520
|
+
await writeFile(join(dir, "cr.txt"), content);
|
|
1521
|
+
const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "cr.txt", limit: 1 });
|
|
1522
|
+
assert.equal(result.details.truncation.firstLineExceedsLimit, true);
|
|
1523
|
+
assert.equal(result.details.truncation.outputBytes, 0);
|
|
1524
|
+
assert.equal(
|
|
1525
|
+
result.details.truncation.totalBytes,
|
|
1526
|
+
Buffer.byteLength(`1#${computeLineHash(1, content, 4)}│${"␍".repeat(content.length)}`),
|
|
1527
|
+
);
|
|
1528
|
+
}));
|
|
1529
|
+
|
|
1530
|
+
test("edit tool rejects non-array formats Pi cannot convert", async () =>
|
|
1531
|
+
withDir(async (dir) => {
|
|
1532
|
+
const file = join(dir, "f.txt");
|
|
1533
|
+
await writeFile(file, "first\nsecond\n");
|
|
1534
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1535
|
+
const anchor = h("first\nsecond\n", 2);
|
|
1536
|
+
for (const alternate of [
|
|
1537
|
+
{ path: "f.txt", edits: JSON.stringify([{ op: "replace", anchor, body: ["SECOND"] }]) },
|
|
1538
|
+
{ path: "f.txt", op: "replace", anchor, body: ["SECOND"] },
|
|
1539
|
+
]) {
|
|
1540
|
+
// prepareArguments only explains wrong hash lengths; it never rewrites arguments.
|
|
1541
|
+
assert.equal(edit.prepareArguments(alternate), alternate);
|
|
1542
|
+
await assert.rejects(call(edit, alternate), /Validation failed/);
|
|
1543
|
+
assert.equal(await readFile(file, "utf8"), "first\nsecond\n");
|
|
1544
|
+
}
|
|
1545
|
+
await call(edit, { path: "f.txt", edits: [{ op: "replace", anchor, body: ["SECOND"] }] });
|
|
1546
|
+
assert.equal(await readFile(file, "utf8"), "first\nSECOND\n");
|
|
1547
|
+
}));
|
|
1548
|
+
|
|
1549
|
+
test("edit names anchors whose hash length differs from the registered hashLen", async () =>
|
|
1550
|
+
withDir(async (dir) => {
|
|
1551
|
+
const text = "first\nsecond\n";
|
|
1552
|
+
const file = join(dir, "f.txt");
|
|
1553
|
+
await writeFile(file, text);
|
|
1554
|
+
const { hashLen } = DEFAULT_CONFIG;
|
|
1555
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1556
|
+
const anchor = h(text, 1);
|
|
1557
|
+
const short = h(text, 2).slice(0, -2);
|
|
1558
|
+
const args = { path: "f.txt", edits: [{ op: "replace", anchor, end: short, body: ["x"] }] };
|
|
1559
|
+
|
|
1560
|
+
await assert.rejects(
|
|
1561
|
+
call(edit, args),
|
|
1562
|
+
new RegExp(
|
|
1563
|
+
`^Error: Anchor hash length mismatch: edits\\[0\\]\\.end ${short} has ${hashLen - 2} hash characters, but hashLen is ${hashLen}\\.`,
|
|
1564
|
+
),
|
|
1565
|
+
);
|
|
1566
|
+
assert.equal(await readFile(file, "utf8"), text);
|
|
1567
|
+
}));
|
|
1568
|
+
|
|
1569
|
+
test("Pi-converted single-object edits execute as a canonical array", async () =>
|
|
1570
|
+
withDir(async (dir) => {
|
|
1571
|
+
const file = join(dir, "converted.txt");
|
|
1572
|
+
const original = "first\nsecond\n";
|
|
1573
|
+
await writeFile(file, original);
|
|
1574
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1575
|
+
const operation = { op: "replace", anchor: h(original, 2), body: ["SECOND"] };
|
|
1576
|
+
const args = validateToolArguments(edit as any, {
|
|
1577
|
+
type: "toolCall",
|
|
1578
|
+
id: "converted",
|
|
1579
|
+
name: "edit",
|
|
1580
|
+
arguments: { path: "converted.txt", edits: operation },
|
|
1581
|
+
});
|
|
1582
|
+
assert.deepEqual(args.edits, [operation]);
|
|
1583
|
+
await call(edit, args);
|
|
1584
|
+
assert.equal(await readFile(file, "utf8"), "first\nSECOND\n");
|
|
1585
|
+
}));
|
|
1586
|
+
|
|
1587
|
+
test("edit execute rejects legacy oldText/newText without op", async () =>
|
|
1588
|
+
withDir(async (dir) => {
|
|
1589
|
+
const file = join(dir, "f.txt");
|
|
1590
|
+
await writeFile(file, "first\nsecond\n");
|
|
1591
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1592
|
+
await assert.rejects(
|
|
1593
|
+
call(edit, {
|
|
1594
|
+
path: "f.txt",
|
|
1595
|
+
oldText: "first",
|
|
1596
|
+
newText: "FIRST",
|
|
1597
|
+
}),
|
|
1598
|
+
/Validation failed for tool "edit"/,
|
|
1599
|
+
);
|
|
1600
|
+
await assert.rejects(
|
|
1601
|
+
call(edit, {
|
|
1602
|
+
path: "f.txt",
|
|
1603
|
+
edits: [{ oldText: "first", newText: "FIRST" } as any],
|
|
1604
|
+
}),
|
|
1605
|
+
/Validation failed for tool "edit"/,
|
|
1606
|
+
);
|
|
1607
|
+
assert.equal(await readFile(file, "utf8"), "first\nsecond\n");
|
|
1608
|
+
}));
|
|
1609
|
+
|
|
1610
|
+
test("unresolved snapshot rows support direct retry and revalidate after further changes", async () => {
|
|
1611
|
+
await withDir(async (dir) => {
|
|
1612
|
+
const file = join(dir, "recover.txt");
|
|
1613
|
+
const observed = "\uFEFFguard\r\ntarget = old\r\n中文 literal \\n\\0\r\n";
|
|
1614
|
+
const current = observed.replace("old", "pending");
|
|
1615
|
+
await writeFile(file, current);
|
|
1616
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1617
|
+
let diagnostic = "";
|
|
1618
|
+
await assert.rejects(
|
|
1619
|
+
call(edit, {
|
|
1620
|
+
path: "recover.txt",
|
|
1621
|
+
edits: [{ op: "replace", anchor: h(observed, 2), body: ["target = new"] }],
|
|
1622
|
+
}),
|
|
1623
|
+
(error: Error) => {
|
|
1624
|
+
diagnostic = error.message;
|
|
1625
|
+
assert.match(diagnostic, /^2#[0-9A-Z]+│target = pending$/m);
|
|
1626
|
+
return true;
|
|
1627
|
+
},
|
|
1628
|
+
);
|
|
1629
|
+
assert.deepEqual(await readFile(file), Buffer.from(current));
|
|
1630
|
+
const retryAnchor = anchorLine(diagnostic, 2);
|
|
1631
|
+
const changedAgain = current.replace("pending", "other");
|
|
1632
|
+
await writeFile(file, changedAgain);
|
|
1633
|
+
await assert.rejects(
|
|
1634
|
+
call(edit, {
|
|
1635
|
+
path: "recover.txt",
|
|
1636
|
+
edits: [{ op: "replace", anchor: retryAnchor, body: ["target = new"] }],
|
|
1637
|
+
}),
|
|
1638
|
+
/Anchor mismatch/,
|
|
1639
|
+
);
|
|
1640
|
+
assert.deepEqual(await readFile(file), Buffer.from(changedAgain));
|
|
1641
|
+
await writeFile(file, current);
|
|
1642
|
+
await call(edit, {
|
|
1643
|
+
path: "recover.txt",
|
|
1644
|
+
edits: [{ op: "replace", anchor: retryAnchor, body: ["target = new"] }],
|
|
1645
|
+
});
|
|
1646
|
+
assert.deepEqual(await readFile(file), Buffer.from(observed.replace("old", "new")));
|
|
1647
|
+
});
|
|
1648
|
+
});
|
|
1649
|
+
test("unresolved oversized and out-of-range rows require more context without partial anchors", async () => {
|
|
1650
|
+
await withDir(async (dir) => {
|
|
1651
|
+
const file = join(dir, "recover.txt");
|
|
1652
|
+
const current = "界".repeat(1400) + "\n";
|
|
1653
|
+
await writeFile(file, current);
|
|
1654
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1655
|
+
for (const anchor of [h("old\n", 1), "99#XXXX"]) {
|
|
1656
|
+
await assert.rejects(
|
|
1657
|
+
call(edit, { path: "recover.txt", edits: [{ op: "replace", anchor, body: ["new"] }] }),
|
|
1658
|
+
(error: Error) => {
|
|
1659
|
+
assert.match(error.message, /Use read or grep to inspect/);
|
|
1660
|
+
assert.doesNotMatch(error.message, /^\d+#[0-9A-Z]+│/m);
|
|
1661
|
+
return true;
|
|
1662
|
+
},
|
|
1663
|
+
);
|
|
1664
|
+
assert.deepEqual(await readFile(file), Buffer.from(current));
|
|
1665
|
+
}
|
|
1666
|
+
});
|
|
423
1667
|
});
|
|
424
1668
|
|
|
425
|
-
test("
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
})
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
await assert.rejects(call(makeReplaceTool(dir), { path: "nul.txt", find: "a", replace: "b" }), /UNSUPPORTED_TEXT/);
|
|
577
|
-
assert.deepEqual(await readFile(nulTarget), nulOriginal);
|
|
578
|
-
|
|
579
|
-
const nativeRead: any = await call(makeReadOverride(dir), { path: "nul.txt" });
|
|
580
|
-
assert.equal(nativeRead.content[0].text, "a\0b");
|
|
581
|
-
|
|
582
|
-
const utf16Target = join(dir, "utf16.txt");
|
|
583
|
-
await writeFile(utf16Target, Buffer.from([0xff, 0xfe, 0x49, 0x6c]));
|
|
584
|
-
await assert.rejects(call(makeReadOverride(dir), { path: "utf16.txt" }), /UNSUPPORTED_ENCODING/);
|
|
585
|
-
}));
|
|
586
|
-
|
|
587
|
-
test("edit rejects embedded line terminators even when schema validation is bypassed", async () => withDir(async (dir) => {
|
|
588
|
-
const target = join(dir, "body.txt");
|
|
589
|
-
await writeFile(target, "a\n");
|
|
590
|
-
await assert.rejects(call(makeEditOverride(dir), { path: "body.txt", edits: [{ op: "append", body: ["x\ny"] }] }), /INVALID_BODY/);
|
|
591
|
-
assert.equal(await readFile(target, "utf8"), "a\n");
|
|
592
|
-
assert.throws(() => validateToolArguments(makeEditOverride(dir) as any, { type: "toolCall", id: "body", name: "edit", arguments: { path: "body.txt", edits: [{ op: "append", body: ["x\ny"] }] } }));
|
|
593
|
-
}));
|
|
594
|
-
|
|
595
|
-
test("edit preserves a UTF-8 BOM and reports bound mutation revisions", async () => withDir(async (dir) => {
|
|
596
|
-
const target = join(dir, "bom.txt");
|
|
597
|
-
const original = Buffer.from("\ufeffguard\nold\n", "utf8");
|
|
598
|
-
await writeFile(target, original);
|
|
599
|
-
const result: any = await call(makeEditOverride(dir), { path: "bom.txt", edits: [{ op: "replace", anchor: h("\ufeffguard\nold\n", 2), body: ["new"] }] });
|
|
600
|
-
const expected = Buffer.from("\ufeffguard\nnew\n", "utf8");
|
|
601
|
-
assert.deepEqual(await readFile(target), expected);
|
|
602
|
-
assert.equal(result.details.baseRevision, byteRevision(original));
|
|
603
|
-
assert.equal(result.details.publishedRevision, byteRevision(expected));
|
|
604
|
-
assert.equal(result.details.observedRevision, result.details.publishedRevision);
|
|
605
|
-
assert.equal(result.details.revision, result.details.publishedRevision);
|
|
606
|
-
}));
|
|
607
|
-
|
|
608
|
-
test("edit and replace reject NUL output without rewriting source bytes", async () => withDir(async (dir) => {
|
|
609
|
-
const target = join(dir, "output.txt");
|
|
610
|
-
const original = Buffer.from("\ufeffbefore\r\n", "utf8");
|
|
611
|
-
await writeFile(target, original);
|
|
612
|
-
await assert.rejects(call(makeEditOverride(dir), { path: "output.txt", edits: [{ op: "append", body: ["bad\0text"] }] }), /UNSUPPORTED_TEXT/);
|
|
613
|
-
assert.deepEqual(await readFile(target), original);
|
|
614
|
-
await assert.rejects(call(makeReplaceTool(dir), { path: "output.txt", find: "before", replace: "bad\0text" }), /UNSUPPORTED_TEXT/);
|
|
615
|
-
assert.deepEqual(await readFile(target), original);
|
|
616
|
-
}));
|
|
617
|
-
|
|
618
|
-
test("failed edits expose input status and candidate code for a verified fused retry", async () => withDir(async (dir) => {
|
|
619
|
-
const original = Array.from({ length: 14 }, (_, index) => `line-${index + 1}`);
|
|
620
|
-
original[4] = "if (!fusion) throw new Error();";
|
|
621
|
-
original[5] = "const absolutePath = canonicalPath(cwd, path);";
|
|
622
|
-
const before = `prefix\n${original.join("\n")}\n`;
|
|
623
|
-
const file = join(dir, "candidate.txt");
|
|
624
|
-
await writeFile(file, before);
|
|
625
|
-
let commands = 0;
|
|
626
|
-
const fusion = createActionFusionExecutor(async () => { commands++; return "checked"; });
|
|
627
|
-
const edit = makeEditOverride(dir, fusion);
|
|
628
|
-
const stale = h(original.join("\n"), 5);
|
|
629
|
-
const stable = h(before, 12);
|
|
630
|
-
const candidate = h(before, 6);
|
|
631
|
-
let message = "";
|
|
632
|
-
await assert.rejects(edit.execute("failed", {
|
|
633
|
-
path: file,
|
|
634
|
-
edits: [
|
|
635
|
-
{ op: "insert_after", anchor: stale, body: ["const hashLen = 4;"] },
|
|
636
|
-
{ op: "replace", anchor: stable, body: ["changed"] },
|
|
637
|
-
],
|
|
638
|
-
then_run: { command: "check" },
|
|
639
|
-
}, undefined, undefined, { cwd: dir }), (error: Error) => { message = error.message; return true; });
|
|
640
|
-
assert.match(message, /then_run:skipped/);
|
|
641
|
-
assert.ok(message.includes(`op 0 / anchor / ${stale} / mismatched`));
|
|
642
|
-
assert.ok(message.includes(`op 1 / anchor / ${stable} / matched`));
|
|
643
|
-
assert.ok(message.includes(`checksum-matching candidate ${candidate}.`));
|
|
644
|
-
assert.equal(message.split(`${candidate}│${original[4]}`).length - 1, 1);
|
|
645
|
-
assert.doesNotMatch(message, /0 omitted|Limits:/);
|
|
646
|
-
assert.doesNotMatch(message, /neighborhoods|Current-file context/);
|
|
647
|
-
assert.equal((message.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 1);
|
|
648
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
649
|
-
assert.equal(commands, 0);
|
|
650
|
-
await edit.execute("retry", {
|
|
651
|
-
path: file,
|
|
652
|
-
edits: [
|
|
653
|
-
{ op: "insert_after", anchor: anchorLine(message, 6), body: ["const hashLen = 4;"] },
|
|
654
|
-
{ op: "replace", anchor: stable, body: ["changed"] },
|
|
655
|
-
],
|
|
656
|
-
then_run: { command: "check" },
|
|
657
|
-
}, undefined, undefined, { cwd: dir });
|
|
658
|
-
const expected = splitLines(before);
|
|
659
|
-
expected[11] = "changed";
|
|
660
|
-
expected.splice(6, 0, "const hashLen = 4;");
|
|
661
|
-
assert.equal(await readFile(file, "utf8"), `${expected.join("\n")}\n`);
|
|
662
|
-
assert.equal(commands, 1);
|
|
663
|
-
}));
|
|
664
|
-
|
|
665
|
-
test("ambiguous candidates include distinguishing neighborhoods for a verified retry", async () => withDir(async (dir) => {
|
|
666
|
-
const lines = Array.from({ length: 20 }, (_, index) => `line-${index + 1}`);
|
|
667
|
-
lines[3] = "function primary() {";
|
|
668
|
-
lines[4] = " return value;";
|
|
669
|
-
lines[13] = "function secondary() {";
|
|
670
|
-
lines[14] = " return value;";
|
|
671
|
-
const before = lines.join("\n") + "\n";
|
|
672
|
-
const file = join(dir, "ambiguous.txt");
|
|
673
|
-
await writeFile(file, before);
|
|
674
|
-
const edit = makeEditOverride(dir);
|
|
675
|
-
let context = "";
|
|
676
|
-
await assert.rejects(call(edit, { path: file, edits: [
|
|
677
|
-
{ op: "replace", anchor: h("header\n return value;\n", 2), body: [" return updated;"] },
|
|
678
|
-
] }), (error: Error) => {
|
|
679
|
-
assert.match(error.message, /ambiguous checksum matches/);
|
|
680
|
-
assert.match(error.message, /Search: local; matches outside the window were not checked\./);
|
|
681
|
-
assert.ok(error.message.includes(`"${h(before, 5)}" / "${h(before, 15)}"`));
|
|
682
|
-
context = error.message.split("Ambiguous-candidate neighborhoods")[1];
|
|
683
|
-
assert.ok(context);
|
|
684
|
-
assert.match(context, /@@ candidate-neighborhood lines 2-8 @@/);
|
|
685
|
-
assert.match(context, /@@ candidate-neighborhood lines 12-18 @@/);
|
|
686
|
-
assert.ok(context.includes(`${h(before, 4)}│function primary() {`));
|
|
687
|
-
assert.ok(context.includes(`${h(before, 14)}│function secondary() {`));
|
|
688
|
-
assert.equal(anchorLine(context, 5), h(before, 5));
|
|
689
|
-
return true;
|
|
690
|
-
});
|
|
691
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
692
|
-
await call(edit, { path: file, edits: [{ op: "replace", anchor: anchorLine(context, 15), body: [" return updated;"] }] });
|
|
693
|
-
lines[14] = " return updated;";
|
|
694
|
-
assert.equal(await readFile(file, "utf8"), lines.join("\n") + "\n");
|
|
695
|
-
}));
|
|
696
|
-
|
|
697
|
-
test("input status distinguishes skipped checks and expires before the next retry", async () => withDir(async (dir) => {
|
|
698
|
-
const before = "a\nb\nc\n";
|
|
699
|
-
const file = join(dir, "checks.txt");
|
|
700
|
-
await writeFile(file, before);
|
|
701
|
-
const edit = makeEditOverride(dir);
|
|
702
|
-
const stable = h(before, 1);
|
|
703
|
-
await assert.rejects(call(edit, { path: file, edits: [{ op: "replace", anchor: stable, body: ["bad\nline"] }] }), (error: Error) => {
|
|
704
|
-
assert.ok(error.message.includes(`op 0 / anchor / ${stable} / not_checked`));
|
|
705
|
-
assert.doesNotMatch(error.message, /\/ matched/);
|
|
706
|
-
return true;
|
|
707
|
-
});
|
|
708
|
-
await assert.rejects(call(edit, { path: file, edits: [
|
|
709
|
-
{ op: "replace", anchor: stable, body: ["A"] },
|
|
710
|
-
{ op: "delete", anchor: h(before, 2), end: "3#ZZ" },
|
|
711
|
-
] }), (error: Error) => {
|
|
712
|
-
assert.ok(error.message.includes(`op 0 / anchor / ${stable} / matched`));
|
|
713
|
-
assert.match(error.message, /op 1 \/ end \/ 3#ZZ \/ mismatched/);
|
|
714
|
-
assert.match(error.message, /Anchor checks only; retries revalidate/);
|
|
715
|
-
return true;
|
|
716
|
-
});
|
|
717
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
718
|
-
await writeFile(file, "changed\nb\nc\n");
|
|
719
|
-
await assert.rejects(call(edit, { path: file, edits: [{ op: "replace", anchor: stable, body: ["A"] }] }), (error: Error) => {
|
|
720
|
-
assert.ok(error.message.includes(`op 0 / anchor / ${stable} / mismatched`));
|
|
721
|
-
return true;
|
|
722
|
-
});
|
|
723
|
-
assert.equal(await readFile(file, "utf8"), "changed\nb\nc\n");
|
|
724
|
-
}));
|
|
725
|
-
|
|
726
|
-
test("failed batches return more than forty checks and mappings when byte budgets allow", async () => withDir(async (dir) => {
|
|
727
|
-
const original = "a\ntarget\nz\n";
|
|
728
|
-
const before = `prefix\n${original}`;
|
|
729
|
-
const file = join(dir, "many-checks.txt");
|
|
730
|
-
await writeFile(file, before);
|
|
731
|
-
const edits = Array.from({ length: 45 }, () => ({ op: "replace", anchor: h(original, 2), body: ["changed"] }));
|
|
732
|
-
await assert.rejects(call(makeEditOverride(dir), { path: file, edits }), (error: Error) => {
|
|
733
|
-
assert.doesNotMatch(error.message, /truncated|failure details omitted|Anchor checks: \d+\/\d+/);
|
|
734
|
-
assert.equal((error.message.match(/^op \d+ \/ anchor \/ .* \/ mismatched$/gm) ?? []).length, 45);
|
|
735
|
-
assert.equal((error.message.match(/checksum-matching candidate/g) ?? []).length, 45);
|
|
736
|
-
assert.equal((error.message.match(/^3#[0-9A-Z]+│target$/gm) ?? []).length, 1);
|
|
737
|
-
assert.doesNotMatch(error.message, /\/ matched/);
|
|
738
|
-
return true;
|
|
739
|
-
});
|
|
740
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
741
|
-
}));
|
|
742
|
-
|
|
743
|
-
test("compact edit anchors retain only untouched deletion successors in mixed batches", async () => withDir(async (dir) => {
|
|
744
|
-
const before = "a\nb\nc\nd\ne\nf\ng\n";
|
|
745
|
-
const file = join(dir, "mixed.txt");
|
|
746
|
-
await writeFile(file, before);
|
|
747
|
-
const edit = makeEditOverride(dir);
|
|
748
|
-
const result = await call(edit, { path: file, edits: [
|
|
749
|
-
{ op: "delete", anchor: h(before, 6) },
|
|
750
|
-
{ op: "replace", anchor: h(before, 4), body: ["D"] },
|
|
751
|
-
{ op: "delete", anchor: h(before, 3) },
|
|
752
|
-
{ op: "replace", anchor: h(before, 1), body: ["A", "X"] },
|
|
753
|
-
] });
|
|
754
|
-
const after = "A\nX\nb\nD\ne\ng\n";
|
|
755
|
-
assert.equal(await readFile(file, "utf8"), after);
|
|
756
|
-
const output = result.content[0].text;
|
|
757
|
-
assert.deepEqual(output.split("\n").filter((line: string) => /^\d+#/.test(line)), [
|
|
758
|
-
h(after, 1), h(after, 2), h(after, 4), `${h(after, 6)}│g`,
|
|
759
|
-
]);
|
|
760
|
-
await call(edit, { path: file, edits: [{ op: "replace", anchor: anchorLine(output, 6), body: ["G"] }] });
|
|
761
|
-
assert.equal(await readFile(file, "utf8"), "A\nX\nb\nD\ne\nG\n");
|
|
762
|
-
}));
|
|
763
|
-
|
|
764
|
-
test("unique candidate content is independent of oversized neighboring rows", async () => withDir(async (dir) => {
|
|
765
|
-
const original = "header\ntarget\ntail\n";
|
|
766
|
-
const before = `header\n${"x".repeat(17000)}\ntarget\ntail\n`;
|
|
767
|
-
const file = join(dir, "fallback.txt");
|
|
768
|
-
await writeFile(file, before);
|
|
769
|
-
let candidate = "";
|
|
770
|
-
await assert.rejects(call(makeEditOverride(dir), { path: file, edits: [
|
|
771
|
-
{ op: "replace", anchor: h(original, 2), body: ["updated"] },
|
|
772
|
-
] }), (error: Error) => {
|
|
773
|
-
candidate = anchorLine(error.message, 3);
|
|
774
|
-
assert.equal((error.message.match(/^3#[0-9A-Z]+│target$/gm) ?? []).length, 1);
|
|
775
|
-
assert.doesNotMatch(error.message, /neighborhoods|truncated/);
|
|
776
|
-
assert.equal((error.message.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 1);
|
|
777
|
-
return true;
|
|
778
|
-
});
|
|
779
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
780
|
-
await call(makeEditOverride(dir), { path: file, edits: [{ op: "replace", anchor: candidate, body: ["updated"] }] });
|
|
781
|
-
assert.equal(await readFile(file, "utf8"), before.replace("target", "updated"));
|
|
782
|
-
}));
|
|
783
|
-
|
|
784
|
-
test("standalone CR replacements remain visible in diffs and exact in patches", async () => withDir(async (dir) => {
|
|
785
|
-
const file = join(dir, "cr.txt");
|
|
786
|
-
for (const [before, find, replacement] of [
|
|
787
|
-
["a\rb\n", "\r", "\n"],
|
|
788
|
-
["a\rb\n", "\r", "␍"],
|
|
789
|
-
]) {
|
|
790
|
-
await writeFile(file, before);
|
|
791
|
-
const result = await call(makeReplaceTool(dir), { path: file, find, replace: replacement, regex: true });
|
|
792
|
-
assert.equal(await readFile(file, "utf8"), before.replaceAll(find, replacement));
|
|
793
|
-
assert.equal(result.details.firstChangedLine, 1);
|
|
794
|
-
assert.match(result.details.diff, /^-1 a␍/m);
|
|
795
|
-
assert.match(result.details.diff, /^\+1 a/m);
|
|
796
|
-
assert.ok(!result.details.diff.includes("\r"));
|
|
797
|
-
assert.ok(result.details.patch.includes(`-a${before.includes("\r\n") ? "\r\n" : "\rb\n"}`));
|
|
798
|
-
assert.match(result.details.patch, /@@/);
|
|
799
|
-
}
|
|
800
|
-
}));
|
|
801
|
-
|
|
802
|
-
test("deletion successors and unique candidate rows display CR without altering anchors", async () => withDir(async (dir) => {
|
|
803
|
-
const file = join(dir, "cr.txt");
|
|
804
|
-
const before = "remove\na\rb\n";
|
|
805
|
-
await writeFile(file, before);
|
|
806
|
-
const result = await call(makeEditOverride(dir), { path: file, edits: [{ op: "delete", anchor: h(before, 1) }] });
|
|
807
|
-
assert.ok(result.content[0].text.includes(`${h("a\rb\n", 1)}│a␍b`));
|
|
808
|
-
assert.equal(await readFile(file, "utf8"), "a\rb\n");
|
|
809
|
-
await assert.rejects(call(makeEditOverride(dir), { path: file, edits: [{ op: "delete", anchor: h(before, 2) }] }), (error: Error) => {
|
|
810
|
-
assert.ok(error.message.includes(`${h("a\rb\n", 1)}│a␍b`));
|
|
811
|
-
assert.ok(!error.message.includes("\r"));
|
|
812
|
-
return true;
|
|
813
|
-
});
|
|
814
|
-
}));
|
|
1669
|
+
test("edit rejects impossible checksum characters before accessing the file", async () =>
|
|
1670
|
+
withDir(async (dir) => {
|
|
1671
|
+
const tool = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1672
|
+
for (const char of ["I", "L", "O", "U"]) {
|
|
1673
|
+
await assert.rejects(
|
|
1674
|
+
call(tool, {
|
|
1675
|
+
path: "missing.txt",
|
|
1676
|
+
edits: [{ op: "delete", anchor: `1#${char.repeat(DEFAULT_CONFIG.hashLen)}` }],
|
|
1677
|
+
}),
|
|
1678
|
+
/Validation failed/,
|
|
1679
|
+
);
|
|
1680
|
+
}
|
|
1681
|
+
}));
|
|
1682
|
+
|
|
1683
|
+
test("read renders native content without interpreting anchor-like prefixes", async () =>
|
|
1684
|
+
withDir(async (dir) => {
|
|
1685
|
+
const tool = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
1686
|
+
for (const content of ["12#abc│ordinary content", "12#ABCD│ordinary content"]) {
|
|
1687
|
+
for (const suffix of ["\n", "\n\0tail"]) {
|
|
1688
|
+
await writeFile(join(dir, "prefix.txt"), content + suffix);
|
|
1689
|
+
const result = await call(tool, { path: "prefix.txt" });
|
|
1690
|
+
// Round-trip details as persisted session results do before rendering.
|
|
1691
|
+
const rendered = tool.renderResult!(
|
|
1692
|
+
JSON.parse(JSON.stringify(result)),
|
|
1693
|
+
{ expanded: true, isPartial: false },
|
|
1694
|
+
stubTheme as Theme,
|
|
1695
|
+
{ args: { path: "prefix.txt" }, state: {}, cwd: dir, isError: false } as any,
|
|
1696
|
+
)
|
|
1697
|
+
.render(120)
|
|
1698
|
+
.join("\n");
|
|
1699
|
+
assert.ok(rendered.includes(content), rendered);
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
}));
|
|
1703
|
+
|
|
1704
|
+
test("read accepts safe offsets and limits whose sum exceeds the safe integer range", async () =>
|
|
1705
|
+
withDir(async (dir) => {
|
|
1706
|
+
await writeFile(join(dir, "range.txt"), "first\nsecond\nthird\n");
|
|
1707
|
+
const tool = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
1708
|
+
for (const offset of [2, 3, Number.MAX_SAFE_INTEGER]) {
|
|
1709
|
+
const result = await call(tool, {
|
|
1710
|
+
path: "range.txt",
|
|
1711
|
+
offset,
|
|
1712
|
+
limit: Number.MAX_SAFE_INTEGER,
|
|
1713
|
+
});
|
|
1714
|
+
assert.equal(result.details, undefined);
|
|
1715
|
+
assert.doesNotMatch(result.content[0].text, /to continue|│first/);
|
|
1716
|
+
if (offset <= 3) assert.match(result.content[0].text, /│third/);
|
|
1717
|
+
else assert.doesNotMatch(result.content[0].text, /│/);
|
|
1718
|
+
}
|
|
1719
|
+
}));
|
|
1720
|
+
|
|
1721
|
+
test("ambiguous failure lists and neighborhoods select the same first eight candidates", async () =>
|
|
1722
|
+
withDir(async (dir) => {
|
|
1723
|
+
const positions = [4, 14, 24, 34, 44, 54, 64, 74, 84];
|
|
1724
|
+
const lines = Array.from({ length: 90 }, (_, index) => `line-${index + 1}`);
|
|
1725
|
+
for (const position of positions) lines[position - 1] = "target";
|
|
1726
|
+
const text = lines.join("\n");
|
|
1727
|
+
await writeFile(join(dir, "candidates.txt"), text);
|
|
1728
|
+
const tool = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1729
|
+
await assert.rejects(
|
|
1730
|
+
call(tool, {
|
|
1731
|
+
path: "candidates.txt",
|
|
1732
|
+
edits: [{ op: "delete", anchor: `200#${computeLineHash(200, "target", 4)}` }],
|
|
1733
|
+
}),
|
|
1734
|
+
(error: Error) => {
|
|
1735
|
+
const list = error.message
|
|
1736
|
+
.split("\n")
|
|
1737
|
+
.find((line) => line.includes("ambiguous checksum matches:"));
|
|
1738
|
+
assert.ok(list);
|
|
1739
|
+
assert.match(list, /1 more candidates omitted/);
|
|
1740
|
+
for (const position of positions.slice(0, 8)) {
|
|
1741
|
+
const anchor = h(text, position);
|
|
1742
|
+
assert.ok(list.includes(`"${anchor}"`));
|
|
1743
|
+
assert.ok(error.message.includes(`${anchor}│target`));
|
|
1744
|
+
}
|
|
1745
|
+
assert.ok(!error.message.includes(h(text, 84)));
|
|
1746
|
+
return true;
|
|
1747
|
+
},
|
|
1748
|
+
);
|
|
1749
|
+
assert.equal(await readFile(join(dir, "candidates.txt"), "utf8"), text);
|
|
1750
|
+
}));
|
|
1751
|
+
|
|
1752
|
+
test("loaded configuration controls hash length and recovery radius", async () =>
|
|
1753
|
+
withDir(async (dir) => {
|
|
1754
|
+
const text = "changed\ntarget\npadding\ntarget\n";
|
|
1755
|
+
await mkdir(join(dir, ".pi"));
|
|
1756
|
+
await writeFile(join(dir, "configured.txt"), text);
|
|
1757
|
+
for (const [hashLen, shiftRadius, expected] of [
|
|
1758
|
+
[6, 0, /no checksum-matching candidate found/],
|
|
1759
|
+
[6, 1, /checksum-matching candidate 2#/],
|
|
1760
|
+
[8, 3, /ambiguous checksum matches/],
|
|
1761
|
+
] as const) {
|
|
1762
|
+
await writeFile(
|
|
1763
|
+
join(dir, ".pi", "settings.json"),
|
|
1764
|
+
JSON.stringify({ hashlineEdit: { hashLen, shiftRadius } }),
|
|
1765
|
+
);
|
|
1766
|
+
const config = loadConfig(dir);
|
|
1767
|
+
const read = await call(makeReadOverride(dir, config), { path: "configured.txt" });
|
|
1768
|
+
assert.equal(anchorLine(read.content[0].text, 1).split("#")[1].length, hashLen);
|
|
1769
|
+
await assert.rejects(
|
|
1770
|
+
call(makeEditOverride(dir, config), {
|
|
1771
|
+
path: "configured.txt",
|
|
1772
|
+
edits: [{ op: "delete", anchor: `1#${computeLineHash(1, "target", hashLen)}` }],
|
|
1773
|
+
}),
|
|
1774
|
+
expected,
|
|
1775
|
+
);
|
|
1776
|
+
assert.equal(await readFile(join(dir, "configured.txt"), "utf8"), text);
|
|
1777
|
+
}
|
|
1778
|
+
}));
|
|
1779
|
+
|
|
1780
|
+
test("configured read defaults bound omitted limits and returned bytes", async () =>
|
|
1781
|
+
withDir(async (dir) => {
|
|
1782
|
+
const read = makeReadOverride(dir, {
|
|
1783
|
+
...DEFAULT_CONFIG,
|
|
1784
|
+
read: { defaultLimit: 2, maxKiB: 1 },
|
|
1785
|
+
});
|
|
1786
|
+
const description: unknown = Reflect.get(read.parameters.properties.limit, "description");
|
|
1787
|
+
assert.ok(typeof description === "string");
|
|
1788
|
+
assert.match(description, /default 2\)/);
|
|
1789
|
+
await writeFile(join(dir, "short.txt"), "a\nb\nc\n");
|
|
1790
|
+
const paged = await call(read, { path: "short.txt" });
|
|
1791
|
+
assert.match(paged.content[0].text, /showing lines 1-2 of 3; use offset 3 to continue/);
|
|
1792
|
+
assert.match(paged.content[0].text, /^2#[0-9A-Z]+│b$/m);
|
|
1793
|
+
assert.doesNotMatch(paged.content[0].text, /^3#/m);
|
|
1794
|
+
const explicit = await call(read, { path: "short.txt", limit: 3 });
|
|
1795
|
+
assert.match(explicit.content[0].text, /^3#[0-9A-Z]+│c$/m);
|
|
1796
|
+
await writeFile(join(dir, "wide.txt"), `${"x".repeat(2048)}\n`);
|
|
1797
|
+
const wide = await call(read, { path: "wide.txt" });
|
|
1798
|
+
assert.match(wide.content[0].text, /line 1 exceeds 1 KiB/);
|
|
1799
|
+
assert.equal(wide.details.truncation.maxBytes, 1024);
|
|
1800
|
+
assert.equal(wide.details.truncation.outputBytes, 0);
|
|
1801
|
+
}));
|
|
1802
|
+
|
|
1803
|
+
test("edit verification and returned anchors use the supplied hash length", async () =>
|
|
1804
|
+
withDir(async (dir) => {
|
|
1805
|
+
const edit = makeEditOverride(dir, { ...DEFAULT_CONFIG, hashLen: 6 });
|
|
1806
|
+
await writeFile(join(dir, "registered.txt"), "before\n");
|
|
1807
|
+
const result = await call(edit, {
|
|
1808
|
+
path: "registered.txt",
|
|
1809
|
+
edits: [
|
|
1810
|
+
{
|
|
1811
|
+
op: "replace",
|
|
1812
|
+
anchor: `1#${computeLineHash(1, "before", 6)}`,
|
|
1813
|
+
body: ["after"],
|
|
1814
|
+
},
|
|
1815
|
+
],
|
|
1816
|
+
});
|
|
1817
|
+
assert.equal(anchorLine(result.content[0].text, 1), `1#${computeLineHash(1, "after", 6)}`);
|
|
1818
|
+
assert.equal(await readFile(join(dir, "registered.txt"), "utf8"), "after\n");
|
|
1819
|
+
}));
|