@criogaid/pi-hashline-edit 0.8.0 → 0.9.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 +6 -2
- package/README.zh-CN.md +6 -2
- package/package.json +1 -1
- package/src/core/apply.test.ts +35 -35
- package/src/core/apply.ts +9 -4
- package/src/core/ranges.test.ts +16 -8
- package/src/core/ranges.ts +10 -6
- package/src/core/types.ts +1 -1
- package/src/integration/config-registration.test.ts +2 -2
- package/src/integration/grep-rg.test.ts +5 -5
- package/src/integration/large-text.test.ts +1 -1
- package/src/integration/model-budget-lifecycle.test.ts +11 -2
- package/src/integration/mutation-file-types.testing.ts +1 -1
- package/src/integration/npm-package.testing.ts +1 -1
- package/src/integration/publication-read-changes.test.ts +2 -2
- package/src/integration/report-replay.test.ts +1 -1
- package/src/integration/utf8-errors.test.ts +1 -1
- package/src/pi/AGENTS.md +2 -2
- package/src/pi/action-fusion.test.ts +4 -4
- package/src/pi/argument-diagnostics.ts +34 -185
- package/src/pi/argument-format.test.ts +20 -22
- package/src/pi/argument-validation.test.ts +388 -396
- package/src/pi/edit-tool.ts +172 -115
- package/src/pi/execute.test.ts +97 -85
- package/src/pi/failure-context.ts +1 -1
- package/src/pi/grep.test.ts +1 -1
- package/src/pi/output-budgets.test.ts +2 -2
- package/src/pi/pi.test.ts +3 -1
- package/src/pi/registration.test.ts +52 -18
- package/src/pi/render.test.ts +43 -16
- package/src/pi/render.ts +10 -0
- package/src/pi/replace-tool.ts +2 -1
- package/src/pi/report-schema.ts +5 -2
- package/src/pi/result-safety.test.ts +30 -15
package/src/pi/execute.test.ts
CHANGED
|
@@ -77,7 +77,7 @@ test("edit execute: a file without a final newline stays byte-exact", async () =
|
|
|
77
77
|
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
78
78
|
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
79
79
|
path: "f.txt",
|
|
80
|
-
edits: [{
|
|
80
|
+
edits: [{ action: "replace", anchor: h(text, 2), body: ["new value\u2060"] }],
|
|
81
81
|
});
|
|
82
82
|
assert.equal(r.isError, undefined, "should not be an error");
|
|
83
83
|
assert.equal(await readFile(f, "utf-8"), "guard\nnew value\u2060");
|
|
@@ -92,7 +92,7 @@ test("edit execute: hashline round-trip (read → edit → file changed)", async
|
|
|
92
92
|
const read = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
93
93
|
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
94
94
|
path: "f.txt",
|
|
95
|
-
edits: [{
|
|
95
|
+
edits: [{ action: "replace", anchor: anchorLine(read.content[0].text, 2), body: ["B"] }],
|
|
96
96
|
});
|
|
97
97
|
assert.equal(r.isError, undefined, "should not be an error");
|
|
98
98
|
assert.equal(await readFile(f, "utf-8"), "a\nB\nc\n");
|
|
@@ -108,8 +108,8 @@ test("edit execute: multiple ops in one call", async () => {
|
|
|
108
108
|
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
109
109
|
path: "f.txt",
|
|
110
110
|
edits: [
|
|
111
|
-
{
|
|
112
|
-
{
|
|
111
|
+
{ action: "insert_after", anchor: h(text, 3), body: ["z"] },
|
|
112
|
+
{ action: "replace", anchor: h(text, 1), body: ["A"] },
|
|
113
113
|
],
|
|
114
114
|
});
|
|
115
115
|
assert.equal(r.isError, undefined);
|
|
@@ -140,7 +140,7 @@ test("successful mutation → returns one report without anchor payload", async
|
|
|
140
140
|
path,
|
|
141
141
|
edits: [
|
|
142
142
|
{
|
|
143
|
-
|
|
143
|
+
action: "replace",
|
|
144
144
|
anchor: anchorLine(rows, 1),
|
|
145
145
|
end: anchorLine(rows, count),
|
|
146
146
|
body: Array.from({ length: count }, () => "new"),
|
|
@@ -188,7 +188,7 @@ test("unrelated external change does NOT block an edit on a stable line", async
|
|
|
188
188
|
await writeFile(f, "a\nb\nCHANGED\n");
|
|
189
189
|
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
190
190
|
path: "f.txt",
|
|
191
|
-
edits: [{
|
|
191
|
+
edits: [{ action: "replace", anchor: h(text, 1), body: ["A"] }],
|
|
192
192
|
});
|
|
193
193
|
assert.equal(r.isError, undefined);
|
|
194
194
|
assert.equal(await readFile(f, "utf-8"), "A\nb\nCHANGED\n");
|
|
@@ -204,7 +204,7 @@ test("edit on a line that changed externally → anchor mismatch", async () => {
|
|
|
204
204
|
const report = failureReport(
|
|
205
205
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
206
206
|
path: "f.txt",
|
|
207
|
-
edits: [{
|
|
207
|
+
edits: [{ action: "replace", anchor: h(text, 2), body: ["x"] }],
|
|
208
208
|
}),
|
|
209
209
|
);
|
|
210
210
|
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
@@ -225,7 +225,7 @@ test("unresolved observations require target confirmation before retrying a move
|
|
|
225
225
|
const report = failureReport(
|
|
226
226
|
await call(edit, {
|
|
227
227
|
path: "recover.txt",
|
|
228
|
-
edits: [{
|
|
228
|
+
edits: [{ action: "replace", anchor: h(observed, 5), body: ["updated"] }],
|
|
229
229
|
}),
|
|
230
230
|
);
|
|
231
231
|
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
@@ -236,7 +236,7 @@ test("unresolved observations require target confirmation before retrying a move
|
|
|
236
236
|
const retryAnchor = anchorLine(read.content[0].text, 7);
|
|
237
237
|
await call(edit, {
|
|
238
238
|
path: "recover.txt",
|
|
239
|
-
edits: [{
|
|
239
|
+
edits: [{ action: "replace", anchor: retryAnchor, body: ["updated"] }],
|
|
240
240
|
});
|
|
241
241
|
assert.equal(
|
|
242
242
|
await readFile(file, "utf8"),
|
|
@@ -251,7 +251,7 @@ test("edit execute: no read before edit → anchor verification fails", async ()
|
|
|
251
251
|
const report = failureReport(
|
|
252
252
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
253
253
|
path: "f.txt",
|
|
254
|
-
edits: [{
|
|
254
|
+
edits: [{ action: "replace", anchor: "1#XXXX", body: ["A"] }],
|
|
255
255
|
}),
|
|
256
256
|
);
|
|
257
257
|
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
@@ -267,7 +267,7 @@ test("edit execute: delete op", async () => {
|
|
|
267
267
|
await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
268
268
|
const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
269
269
|
path: "f.txt",
|
|
270
|
-
edits: [{
|
|
270
|
+
edits: [{ action: "delete", anchor: h(text, 2) }],
|
|
271
271
|
});
|
|
272
272
|
assert.equal(r.isError, undefined);
|
|
273
273
|
assert.equal(await readFile(f, "utf-8"), "a\nc\n");
|
|
@@ -323,23 +323,23 @@ test("edit schema rejects misspelled range fields and invalid operation shapes",
|
|
|
323
323
|
const anchor = h(original, 2);
|
|
324
324
|
const callArgs = {
|
|
325
325
|
path: "range.txt",
|
|
326
|
-
edits: [{
|
|
326
|
+
edits: [{ action: "replace", anchor, endd: h(original, 3), body: ["merged"] }],
|
|
327
327
|
};
|
|
328
328
|
await assert.rejects(call(edit, callArgs), rejectsArgument("edits[0].endd", /not allowed/));
|
|
329
329
|
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
330
330
|
for (const [edits, expected] of [
|
|
331
331
|
[[], rejectsArgument("edits")],
|
|
332
|
-
[[{
|
|
333
|
-
[[{
|
|
334
|
-
[[{
|
|
335
|
-
[[{
|
|
332
|
+
[[{ action: "replace", anchor }], rejectsArgument("edits[0].body", /required/)],
|
|
333
|
+
[[{ action: "delete", anchor, body: ["bad"] }], rejectsArguments()],
|
|
334
|
+
[[{ action: "insert_after", body: ["bad"] }], rejectsArguments()],
|
|
335
|
+
[[{ action: "append", anchor, body: ["bad"] }], rejectsArguments()],
|
|
336
336
|
] as const) {
|
|
337
337
|
await assert.rejects(call(edit, { path: "range.txt", edits }), expected);
|
|
338
338
|
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
339
339
|
}
|
|
340
340
|
const alternates: Parameters<typeof validateToolArguments>[1]["arguments"][] = [
|
|
341
|
-
{ path: "range.txt",
|
|
342
|
-
{ path: "range.txt", edits: JSON.stringify([{
|
|
341
|
+
{ path: "range.txt", action: "replace", anchor, body: ["ok"] },
|
|
342
|
+
{ path: "range.txt", edits: JSON.stringify([{ action: "replace", anchor, body: ["ok"] }]) },
|
|
343
343
|
];
|
|
344
344
|
for (const alternate of alternates) {
|
|
345
345
|
const before = structuredClone(alternate);
|
|
@@ -347,7 +347,7 @@ test("edit schema rejects misspelled range fields and invalid operation shapes",
|
|
|
347
347
|
assert.deepEqual(alternate, before);
|
|
348
348
|
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
|
|
349
349
|
}
|
|
350
|
-
await call(edit, { path: "range.txt", edits: [{
|
|
350
|
+
await call(edit, { path: "range.txt", edits: [{ action: "replace", anchor, body: ["B"] }] });
|
|
351
351
|
assert.equal(await readFile(join(dir, "range.txt"), "utf8"), "a\nB\nc\n");
|
|
352
352
|
}));
|
|
353
353
|
|
|
@@ -365,7 +365,7 @@ test("copied string anchors validate and replace an inclusive range", async () =
|
|
|
365
365
|
path: "range.txt",
|
|
366
366
|
edits: [
|
|
367
367
|
{
|
|
368
|
-
|
|
368
|
+
action: "replace",
|
|
369
369
|
anchor: anchorLine(read.content[0].text, 2),
|
|
370
370
|
end: anchorLine(read.content[0].text, 3),
|
|
371
371
|
body: ["merged"],
|
|
@@ -384,13 +384,13 @@ test("invalid anchors and conflicting fields fail before changing the file", asy
|
|
|
384
384
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
385
385
|
const anchor = h(original, 1);
|
|
386
386
|
const invalid = [
|
|
387
|
-
{
|
|
387
|
+
{ action: "replace", anchor: { line: 1, hash: anchor.split("#")[1] }, body: ["changed"] },
|
|
388
388
|
...["0#ABCD", "-1#ABCD", "1.5#ABCD", "1#", "1#abcd", "1#ABCD│a", "9007199254740993#ABCD"].map(
|
|
389
|
-
(anchor) => ({
|
|
389
|
+
(anchor) => ({ action: "replace", anchor, body: ["changed"] }),
|
|
390
390
|
),
|
|
391
|
-
{
|
|
392
|
-
{
|
|
393
|
-
{
|
|
391
|
+
{ action: "insert_after", anchor, end: anchor, body: ["changed"] },
|
|
392
|
+
{ action: "append", anchor, body: ["changed"] },
|
|
393
|
+
{ action: "delete", anchor, body: ["changed"] },
|
|
394
394
|
];
|
|
395
395
|
for (const operation of invalid) {
|
|
396
396
|
await assert.rejects(
|
|
@@ -409,11 +409,11 @@ test("edit rejects empty bodies before file access for arrays and Pi-converted o
|
|
|
409
409
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
410
410
|
const anchor = h(original, 1);
|
|
411
411
|
for (const operation of [
|
|
412
|
-
{
|
|
413
|
-
{
|
|
414
|
-
{
|
|
415
|
-
{
|
|
416
|
-
{
|
|
412
|
+
{ action: "insert_after", anchor, body: [] },
|
|
413
|
+
{ action: "insert_before", anchor, body: [] },
|
|
414
|
+
{ action: "append", body: [] },
|
|
415
|
+
{ action: "prepend", body: [] },
|
|
416
|
+
{ action: "replace", anchor, end: h(original, 2), body: [] },
|
|
417
417
|
]) {
|
|
418
418
|
for (const path of [file, join(dir, "missing.txt")]) {
|
|
419
419
|
for (const edits of [[operation], operation]) {
|
|
@@ -444,10 +444,10 @@ test("mixed empty-body batches name every rejected edit without publishing or ru
|
|
|
444
444
|
{
|
|
445
445
|
path: file,
|
|
446
446
|
edits: [
|
|
447
|
-
{
|
|
448
|
-
{
|
|
449
|
-
{
|
|
450
|
-
{
|
|
447
|
+
{ action: "replace", anchor: h(original, 1), body: ["must not be written"] },
|
|
448
|
+
{ action: "insert_before", anchor: h(original, 3), body: [] },
|
|
449
|
+
{ action: "append", body: ["must not be written"] },
|
|
450
|
+
{ action: "replace", anchor: h(original, 4), body: [] },
|
|
451
451
|
],
|
|
452
452
|
then_run: { command: "must-not-run" },
|
|
453
453
|
},
|
|
@@ -471,11 +471,11 @@ test("blank edit bodies preserve a real blank line for every body-bearing operat
|
|
|
471
471
|
const anchor = h(original, 1);
|
|
472
472
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
473
473
|
for (const [operation, expected] of [
|
|
474
|
-
[{
|
|
475
|
-
[{
|
|
476
|
-
[{
|
|
477
|
-
[{
|
|
478
|
-
[{
|
|
474
|
+
[{ action: "replace", anchor, body: [""] }, "\nb\nc\n"],
|
|
475
|
+
[{ action: "insert_before", anchor, body: [""] }, "\na\nb\nc\n"],
|
|
476
|
+
[{ action: "insert_after", anchor, body: [""] }, "a\n\nb\nc\n"],
|
|
477
|
+
[{ action: "append", body: [""] }, "a\nb\nc\n\n"],
|
|
478
|
+
[{ action: "prepend", body: [""] }, "\na\nb\nc\n"],
|
|
479
479
|
] as const) {
|
|
480
480
|
await writeFile(file, original);
|
|
481
481
|
await call(edit, { path: file, edits: [operation] });
|
|
@@ -490,7 +490,7 @@ test("explicit delete → removes an inclusive range", async () =>
|
|
|
490
490
|
await writeFile(file, original);
|
|
491
491
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
492
492
|
path: file,
|
|
493
|
-
edits: [{
|
|
493
|
+
edits: [{ action: "delete", anchor: h(original, 1), end: h(original, 2) }],
|
|
494
494
|
});
|
|
495
495
|
assert.equal(await readFile(file, "utf8"), "c\n");
|
|
496
496
|
}));
|
|
@@ -506,7 +506,7 @@ test("local and full-file recovery return copyable anchors without changing the
|
|
|
506
506
|
const report = failureReport(
|
|
507
507
|
await call(edit, {
|
|
508
508
|
path: "shift.txt",
|
|
509
|
-
edits: [{
|
|
509
|
+
edits: [{ action: "replace", anchor: h(original, 2), body: ["B"] }],
|
|
510
510
|
}),
|
|
511
511
|
);
|
|
512
512
|
const failure = report.failures?.[0];
|
|
@@ -520,7 +520,7 @@ test("local and full-file recovery return copyable anchors without changing the
|
|
|
520
520
|
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), prefix + original);
|
|
521
521
|
await call(edit, {
|
|
522
522
|
path: "shift.txt",
|
|
523
|
-
edits: [{
|
|
523
|
+
edits: [{ action: "replace", anchor: replacement, body: ["B"] }],
|
|
524
524
|
});
|
|
525
525
|
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), prefix + "a\nB\n");
|
|
526
526
|
}
|
|
@@ -535,7 +535,7 @@ test("shifted-anchor recovery keeps the read fallback for oversized candidates",
|
|
|
535
535
|
const report = failureReport(
|
|
536
536
|
await call(edit, {
|
|
537
537
|
path: "shift-large.txt",
|
|
538
|
-
edits: [{
|
|
538
|
+
edits: [{ action: "replace", anchor: h(original, 2), body: ["B"] }],
|
|
539
539
|
}),
|
|
540
540
|
);
|
|
541
541
|
assert.match(
|
|
@@ -560,7 +560,10 @@ test("failed commands preserve mutation results and stay out of all main card re
|
|
|
560
560
|
path: "edit.txt",
|
|
561
561
|
run: async () => {
|
|
562
562
|
const tool = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
|
|
563
|
-
const args = {
|
|
563
|
+
const args = {
|
|
564
|
+
path: "edit.txt",
|
|
565
|
+
edits: [{ action: "append" as const, body: ["after"] }],
|
|
566
|
+
};
|
|
564
567
|
const result = await callTool(
|
|
565
568
|
tool,
|
|
566
569
|
{ ...args, then_run: { command: "check" } },
|
|
@@ -686,7 +689,7 @@ test("text tools reject malformed UTF-8 and NUL bytes without rewriting source b
|
|
|
686
689
|
[makeReadOverride(dir, DEFAULT_CONFIG), { path: "invalid-utf8.txt" }],
|
|
687
690
|
[
|
|
688
691
|
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
689
|
-
{ path: "invalid-utf8.txt", edits: [{
|
|
692
|
+
{ path: "invalid-utf8.txt", edits: [{ action: "append", body: ["x"] }] },
|
|
690
693
|
],
|
|
691
694
|
[
|
|
692
695
|
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
@@ -703,7 +706,7 @@ test("text tools reject malformed UTF-8 and NUL bytes without rewriting source b
|
|
|
703
706
|
for (const [tool, args] of [
|
|
704
707
|
[
|
|
705
708
|
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
706
|
-
{ path: "nul.txt", edits: [{
|
|
709
|
+
{ path: "nul.txt", edits: [{ action: "append", body: ["x"] }] },
|
|
707
710
|
],
|
|
708
711
|
[
|
|
709
712
|
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
@@ -733,7 +736,7 @@ test("edit rejects embedded line terminators at its schema boundary", async () =
|
|
|
733
736
|
await assert.rejects(
|
|
734
737
|
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
735
738
|
path: "body.txt",
|
|
736
|
-
edits: [{
|
|
739
|
+
edits: [{ action: "append", body: [line] }],
|
|
737
740
|
}),
|
|
738
741
|
rejectsArguments(),
|
|
739
742
|
);
|
|
@@ -745,14 +748,14 @@ test("edit rejects unwritable body lines before reading a missing target", async
|
|
|
745
748
|
withDir(async (dir) => {
|
|
746
749
|
const target = join(dir, "missing.txt");
|
|
747
750
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
748
|
-
const valid = { path: target, edits: [{
|
|
751
|
+
const valid = { path: target, edits: [{ action: "append" as const, body: ["ok"] }] };
|
|
749
752
|
assert.equal(edit.prepareArguments(valid), valid);
|
|
750
753
|
for (const [line, expected] of [
|
|
751
754
|
["bad\0", rejectsArgument("edits[0].body[1]", /NUL/)],
|
|
752
755
|
["\ud800", rejectsArgument("edits[0].body[1]", /losslessly.*UTF-8/)],
|
|
753
756
|
] as const) {
|
|
754
757
|
await assert.rejects(
|
|
755
|
-
call(edit, { path: target, edits: [{
|
|
758
|
+
call(edit, { path: target, edits: [{ action: "append", body: ["ok", line] }] }),
|
|
756
759
|
expected,
|
|
757
760
|
);
|
|
758
761
|
await assert.rejects(readFile(target, "utf8"), { code: "ENOENT" });
|
|
@@ -768,7 +771,7 @@ test("editing a BOM-prefixed file → publishes the changed bytes with the BOM i
|
|
|
768
771
|
await writeFile(target, original);
|
|
769
772
|
const result = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
770
773
|
path: "bom.txt",
|
|
771
|
-
edits: [{
|
|
774
|
+
edits: [{ action: "replace", anchor: h(source, line), body: ["new"] }],
|
|
772
775
|
});
|
|
773
776
|
const expected = Buffer.from(line === 1 ? "\ufeffnew\nold\n" : "\ufeffguard\nnew\n", "utf8");
|
|
774
777
|
assert.deepEqual(await readFile(target), expected);
|
|
@@ -785,7 +788,7 @@ test("edit and replace reject NUL arguments without rewriting source bytes", asy
|
|
|
785
788
|
await assert.rejects(
|
|
786
789
|
call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
787
790
|
path: "output.txt",
|
|
788
|
-
edits: [{
|
|
791
|
+
edits: [{ action: "append", body: ["bad\0text"] }],
|
|
789
792
|
}),
|
|
790
793
|
rejectsArgument("edits[0].body[0]", /NUL/),
|
|
791
794
|
);
|
|
@@ -823,8 +826,8 @@ test("failed edits expose input status and candidate code for a verified fused r
|
|
|
823
826
|
{
|
|
824
827
|
path: file,
|
|
825
828
|
edits: [
|
|
826
|
-
{
|
|
827
|
-
{
|
|
829
|
+
{ action: "insert_after", anchor: stale, body: ["const hashLen = 4;"] },
|
|
830
|
+
{ action: "replace", anchor: stable, body: ["changed"] },
|
|
828
831
|
],
|
|
829
832
|
then_run: { command: "check" },
|
|
830
833
|
},
|
|
@@ -847,11 +850,11 @@ test("failed edits expose input status and candidate code for a verified fused r
|
|
|
847
850
|
path: file,
|
|
848
851
|
edits: [
|
|
849
852
|
{
|
|
850
|
-
|
|
853
|
+
action: "insert_after",
|
|
851
854
|
anchor: report.failures![0].candidate!,
|
|
852
855
|
body: ["const hashLen = 4;"],
|
|
853
856
|
},
|
|
854
|
-
{
|
|
857
|
+
{ action: "replace", anchor: stable, body: ["changed"] },
|
|
855
858
|
],
|
|
856
859
|
then_run: { command: "check" },
|
|
857
860
|
},
|
|
@@ -879,7 +882,11 @@ test("ambiguous candidates include distinguishing neighborhoods for a verified r
|
|
|
879
882
|
await call(edit, {
|
|
880
883
|
path: file,
|
|
881
884
|
edits: [
|
|
882
|
-
{
|
|
885
|
+
{
|
|
886
|
+
action: "replace",
|
|
887
|
+
anchor: h("header\n return value;\n", 2),
|
|
888
|
+
body: [" return updated;"],
|
|
889
|
+
},
|
|
883
890
|
],
|
|
884
891
|
}),
|
|
885
892
|
);
|
|
@@ -897,7 +904,7 @@ test("ambiguous candidates include distinguishing neighborhoods for a verified r
|
|
|
897
904
|
assert.equal(await readFile(file, "utf8"), before);
|
|
898
905
|
await call(edit, {
|
|
899
906
|
path: file,
|
|
900
|
-
edits: [{
|
|
907
|
+
edits: [{ action: "replace", anchor: anchorLine(context, 15), body: [" return updated;"] }],
|
|
901
908
|
});
|
|
902
909
|
lines[14] = " return updated;";
|
|
903
910
|
assert.equal(await readFile(file, "utf8"), lines.join("\n") + "\n");
|
|
@@ -911,14 +918,14 @@ test("schema-invalid bodies omit anchor checks; subsequent retries revalidate",
|
|
|
911
918
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
912
919
|
const stable = h(before, 1);
|
|
913
920
|
for (const operation of [
|
|
914
|
-
{
|
|
915
|
-
{
|
|
921
|
+
{ action: "replace", anchor: stable, body: ["bad\nline"] },
|
|
922
|
+
{ action: "append", body: ["bad\nline"] },
|
|
916
923
|
]) {
|
|
917
924
|
for (const laterAnchor of [h(before, 2), "2#XXXX"]) {
|
|
918
925
|
await assert.rejects(
|
|
919
926
|
call(edit, {
|
|
920
927
|
path: file,
|
|
921
|
-
edits: [operation, {
|
|
928
|
+
edits: [operation, { action: "replace", anchor: laterAnchor, body: ["B"] }],
|
|
922
929
|
}),
|
|
923
930
|
(error: Error) => {
|
|
924
931
|
rejectsArguments()(error);
|
|
@@ -935,8 +942,8 @@ test("schema-invalid bodies omit anchor checks; subsequent retries revalidate",
|
|
|
935
942
|
await call(edit, {
|
|
936
943
|
path: file,
|
|
937
944
|
edits: [
|
|
938
|
-
{
|
|
939
|
-
{
|
|
945
|
+
{ action: "replace", anchor: stable, body: ["A"] },
|
|
946
|
+
{ action: "delete", anchor: h(before, 2), end: "3#ZZZZ" },
|
|
940
947
|
],
|
|
941
948
|
}),
|
|
942
949
|
);
|
|
@@ -950,8 +957,8 @@ test("schema-invalid bodies omit anchor checks; subsequent retries revalidate",
|
|
|
950
957
|
await call(edit, {
|
|
951
958
|
path: file,
|
|
952
959
|
edits: [
|
|
953
|
-
{
|
|
954
|
-
{
|
|
960
|
+
{ action: "replace", anchor: stable, body: ["A"] },
|
|
961
|
+
{ action: "replace", anchor: "3#ZZZZ", body: ["B"] },
|
|
955
962
|
],
|
|
956
963
|
}),
|
|
957
964
|
);
|
|
@@ -971,8 +978,8 @@ test("single-operation anchor failures omit redundant matched-anchor facts", asy
|
|
|
971
978
|
await writeFile(file, source);
|
|
972
979
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
973
980
|
for (const operation of [
|
|
974
|
-
{
|
|
975
|
-
{
|
|
981
|
+
{ action: "replace", anchor: "1#XXXX", body: ["new"] },
|
|
982
|
+
{ action: "replace", anchor: h(source, 1), end: "2#YYYY", body: ["new"] },
|
|
976
983
|
]) {
|
|
977
984
|
const report = failureReport(await call(edit, { path: file, edits: [operation] }));
|
|
978
985
|
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
@@ -989,7 +996,7 @@ test("failed batches return more than forty checks and mappings when byte budget
|
|
|
989
996
|
const file = join(dir, "many-checks.txt");
|
|
990
997
|
await writeFile(file, before);
|
|
991
998
|
const edits = Array.from({ length: 45 }, () => ({
|
|
992
|
-
|
|
999
|
+
action: "replace",
|
|
993
1000
|
anchor: h(original, 2),
|
|
994
1001
|
body: ["changed"],
|
|
995
1002
|
}));
|
|
@@ -1014,7 +1021,7 @@ test("unique candidate content is independent of oversized neighboring rows", as
|
|
|
1014
1021
|
const report = failureReport(
|
|
1015
1022
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1016
1023
|
path: file,
|
|
1017
|
-
edits: [{
|
|
1024
|
+
edits: [{ action: "replace", anchor: h(original, 2), body: ["updated"] }],
|
|
1018
1025
|
}),
|
|
1019
1026
|
);
|
|
1020
1027
|
const candidate = report.failures?.[0].candidate;
|
|
@@ -1026,7 +1033,7 @@ test("unique candidate content is independent of oversized neighboring rows", as
|
|
|
1026
1033
|
assert.equal(await readFile(file, "utf8"), before);
|
|
1027
1034
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1028
1035
|
path: file,
|
|
1029
|
-
edits: [{
|
|
1036
|
+
edits: [{ action: "replace", anchor: candidate, body: ["updated"] }],
|
|
1030
1037
|
});
|
|
1031
1038
|
assert.equal(await readFile(file, "utf8"), before.replace("target", "updated"));
|
|
1032
1039
|
}));
|
|
@@ -1057,13 +1064,13 @@ test("shifted unique candidate → displays standalone CR without altering its a
|
|
|
1057
1064
|
await writeFile(file, before);
|
|
1058
1065
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1059
1066
|
path: file,
|
|
1060
|
-
edits: [{
|
|
1067
|
+
edits: [{ action: "delete", anchor: h(before, 1) }],
|
|
1061
1068
|
});
|
|
1062
1069
|
assert.equal(await readFile(file, "utf8"), "a\rb\n");
|
|
1063
1070
|
const report = failureReport(
|
|
1064
1071
|
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
1065
1072
|
path: file,
|
|
1066
|
-
edits: [{
|
|
1073
|
+
edits: [{ action: "delete", anchor: h(before, 2) }],
|
|
1067
1074
|
}),
|
|
1068
1075
|
);
|
|
1069
1076
|
assert.equal(report.failures?.[0].candidate, h("a\rb\n", 1));
|
|
@@ -1082,8 +1089,8 @@ test("edit receives multiple wrong-length anchors → reports each location with
|
|
|
1082
1089
|
const args = {
|
|
1083
1090
|
path: "f.txt",
|
|
1084
1091
|
edits: [
|
|
1085
|
-
{
|
|
1086
|
-
{
|
|
1092
|
+
{ action: "replace", anchor, end: short, body: ["x"] },
|
|
1093
|
+
{ action: "delete", anchor: short },
|
|
1087
1094
|
],
|
|
1088
1095
|
};
|
|
1089
1096
|
|
|
@@ -1109,7 +1116,7 @@ test("Pi-converted single-object edits execute as a canonical array", async () =
|
|
|
1109
1116
|
const original = "first\nsecond\n";
|
|
1110
1117
|
await writeFile(file, original);
|
|
1111
1118
|
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
1112
|
-
const operation = {
|
|
1119
|
+
const operation = { action: "replace", anchor: h(original, 2), body: ["SECOND"] };
|
|
1113
1120
|
const args = validateToolArguments(edit as any, {
|
|
1114
1121
|
type: "toolCall",
|
|
1115
1122
|
id: "converted",
|
|
@@ -1131,7 +1138,7 @@ test("unresolved snapshot rows support direct retry and revalidate after further
|
|
|
1131
1138
|
const report = failureReport(
|
|
1132
1139
|
await call(edit, {
|
|
1133
1140
|
path: "recover.txt",
|
|
1134
|
-
edits: [{
|
|
1141
|
+
edits: [{ action: "replace", anchor: h(observed, 2), body: ["target = new"] }],
|
|
1135
1142
|
}),
|
|
1136
1143
|
);
|
|
1137
1144
|
const diagnostic = report.failures?.[0].observed ?? "";
|
|
@@ -1143,7 +1150,7 @@ test("unresolved snapshot rows support direct retry and revalidate after further
|
|
|
1143
1150
|
const retry = failureReport(
|
|
1144
1151
|
await call(edit, {
|
|
1145
1152
|
path: "recover.txt",
|
|
1146
|
-
edits: [{
|
|
1153
|
+
edits: [{ action: "replace", anchor: retryAnchor, body: ["target = new"] }],
|
|
1147
1154
|
}),
|
|
1148
1155
|
);
|
|
1149
1156
|
assert.equal(retry.error, "ANCHOR_MISMATCH");
|
|
@@ -1151,7 +1158,7 @@ test("unresolved snapshot rows support direct retry and revalidate after further
|
|
|
1151
1158
|
await writeFile(file, current);
|
|
1152
1159
|
await call(edit, {
|
|
1153
1160
|
path: "recover.txt",
|
|
1154
|
-
edits: [{
|
|
1161
|
+
edits: [{ action: "replace", anchor: retryAnchor, body: ["target = new"] }],
|
|
1155
1162
|
});
|
|
1156
1163
|
assert.deepEqual(await readFile(file), Buffer.from(observed.replace("old", "new")));
|
|
1157
1164
|
});
|
|
@@ -1167,7 +1174,7 @@ test("unresolved oversized and out-of-range rows require more context without pa
|
|
|
1167
1174
|
const report = failureReport(
|
|
1168
1175
|
await call(edit, {
|
|
1169
1176
|
path: "recover.txt",
|
|
1170
|
-
edits: [{
|
|
1177
|
+
edits: [{ action: "replace", anchor, body: ["new"] }],
|
|
1171
1178
|
}),
|
|
1172
1179
|
);
|
|
1173
1180
|
assert.match(report.next ?? "", /read or grep/);
|
|
@@ -1190,7 +1197,7 @@ test("edit rejects impossible checksum characters before accessing the file", as
|
|
|
1190
1197
|
await assert.rejects(
|
|
1191
1198
|
call(tool, {
|
|
1192
1199
|
path: "missing.txt",
|
|
1193
|
-
edits: [{
|
|
1200
|
+
edits: [{ action: "delete", anchor: `1#${char.repeat(DEFAULT_CONFIG.hashLen)}` }],
|
|
1194
1201
|
}),
|
|
1195
1202
|
rejectsArguments(),
|
|
1196
1203
|
);
|
|
@@ -1208,7 +1215,7 @@ test("ambiguous failure lists and neighborhoods select the same first eight cand
|
|
|
1208
1215
|
const report = failureReport(
|
|
1209
1216
|
await call(tool, {
|
|
1210
1217
|
path: "candidates.txt",
|
|
1211
|
-
edits: [{
|
|
1218
|
+
edits: [{ action: "delete", anchor: `200#${computeLineHash(200, "target", 4)}` }],
|
|
1212
1219
|
}),
|
|
1213
1220
|
);
|
|
1214
1221
|
assert.equal(report.failures?.[0].omittedCandidates, 1);
|
|
@@ -1231,7 +1238,7 @@ test("edit verification → uses the supplied hash length", async () =>
|
|
|
1231
1238
|
path: "registered.txt",
|
|
1232
1239
|
edits: [
|
|
1233
1240
|
{
|
|
1234
|
-
|
|
1241
|
+
action: "replace",
|
|
1235
1242
|
anchor: `1#${computeLineHash(1, "before", 6)}`,
|
|
1236
1243
|
body: ["after"],
|
|
1237
1244
|
},
|
|
@@ -1258,7 +1265,12 @@ test("copy/move through read anchors → publishes captured bytes with BOM and m
|
|
|
1258
1265
|
const result: Awaited<ReturnType<typeof edit.execute>> = await callTool(edit, {
|
|
1259
1266
|
path,
|
|
1260
1267
|
edits: [
|
|
1261
|
-
{
|
|
1268
|
+
{
|
|
1269
|
+
action: op,
|
|
1270
|
+
anchor: anchorLine(rows, 1),
|
|
1271
|
+
end: anchorLine(rows, 2),
|
|
1272
|
+
after: anchorLine(rows, 4),
|
|
1273
|
+
},
|
|
1262
1274
|
],
|
|
1263
1275
|
});
|
|
1264
1276
|
const expected = op === "copy" ? source + "A\r\nB\n" : "\uFEFFC\r\nD\nA\r\nB\n";
|
|
@@ -1286,8 +1298,8 @@ test("copy/move stale destination → report its field and skip publication and
|
|
|
1286
1298
|
await callTool(edit, {
|
|
1287
1299
|
path: file,
|
|
1288
1300
|
edits: [
|
|
1289
|
-
{ op, anchor: h(current, 1), after: h(original, 3) },
|
|
1290
|
-
{
|
|
1301
|
+
{ action: op, anchor: h(current, 1), after: h(original, 3) },
|
|
1302
|
+
{ action: "replace", anchor: h(current, 2), body: ["changed"] },
|
|
1291
1303
|
],
|
|
1292
1304
|
then_run: { command: "check" },
|
|
1293
1305
|
}),
|
|
@@ -1321,7 +1333,7 @@ test("copy/move with fused command → execute after the complete transfer and p
|
|
|
1321
1333
|
edit,
|
|
1322
1334
|
{
|
|
1323
1335
|
path: file,
|
|
1324
|
-
edits: [{ op, anchor: h(source, 1), after: h(source, 3) }],
|
|
1336
|
+
edits: [{ action: op, anchor: h(source, 1), after: h(source, 3) }],
|
|
1325
1337
|
then_run: { command: "check" },
|
|
1326
1338
|
},
|
|
1327
1339
|
{ ctx: { cwd: dir } },
|
|
@@ -144,7 +144,7 @@ function failureEntry(
|
|
|
144
144
|
const rowTooLarge = `row exceeds ${formatByteSize(budget.fieldBytes)}`;
|
|
145
145
|
const entry: { -readonly [K in keyof AnchorFailureFact]: AnchorFailureFact[K] } = {
|
|
146
146
|
field: `edits[${f.opIndex}].${f.which}`,
|
|
147
|
-
|
|
147
|
+
action: f.op,
|
|
148
148
|
cited: snapshot.anchors.reference(f.cited.line, f.cited.hash),
|
|
149
149
|
result: RESULT[f.recovery.kind],
|
|
150
150
|
};
|
package/src/pi/grep.test.ts
CHANGED
|
@@ -149,7 +149,7 @@ test("model switches while a mutation waits → failure uses its starting budget
|
|
|
149
149
|
try {
|
|
150
150
|
const running = callTool(makeEditOverride(dir, DEFAULT_CONFIG, undefined, budgets), {
|
|
151
151
|
path,
|
|
152
|
-
edits: [{
|
|
152
|
+
edits: [{ action: "delete", anchor: `1#${wrongHash(text)}` }],
|
|
153
153
|
});
|
|
154
154
|
model = { ...model, id: "large", contextWindow: 1_048_576 };
|
|
155
155
|
assert.equal(budgets().readBytes, large.readBytes);
|
|
@@ -160,7 +160,7 @@ test("model switches while a mutation waits → failure uses its starting budget
|
|
|
160
160
|
const next = reportOf(
|
|
161
161
|
await callTool(makeEditOverride(dir, DEFAULT_CONFIG, undefined, budgets), {
|
|
162
162
|
path,
|
|
163
|
-
edits: [{
|
|
163
|
+
edits: [{ action: "delete", anchor: `1#${wrongHash(text)}` }],
|
|
164
164
|
}),
|
|
165
165
|
"edit",
|
|
166
166
|
);
|
package/src/pi/pi.test.ts
CHANGED
|
@@ -95,7 +95,9 @@ test("file tools share Pi-style URL and @ path resolution", async () => {
|
|
|
95
95
|
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
96
96
|
{
|
|
97
97
|
path: `@${url}`,
|
|
98
|
-
edits: [
|
|
98
|
+
edits: [
|
|
99
|
+
{ action: "replace", anchor: `1#${computeLineHash(1, "after", 4)}`, body: ["edited"] },
|
|
100
|
+
],
|
|
99
101
|
},
|
|
100
102
|
{ toolCallId: "edit", ctx },
|
|
101
103
|
);
|