@criogaid/pi-hashline-edit 0.1.3 → 0.2.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 +63 -17
- package/package.json +1 -1
- package/src/core/apply.test.ts +85 -8
- package/src/core/apply.ts +99 -79
- package/src/core/ranges.test.ts +28 -0
- package/src/core/ranges.ts +25 -0
- package/src/core/text.ts +5 -0
- package/src/core/types.ts +37 -20
- package/src/integration/grep-rg.test.ts +76 -1
- package/src/pi/action-fusion.ts +9 -21
- package/src/pi/anchor-format.test.ts +30 -0
- package/src/pi/anchor-format.ts +28 -0
- package/src/pi/config.ts +1 -1
- package/src/pi/edit-tool.ts +90 -99
- package/src/pi/execute.test.ts +240 -14
- package/src/pi/failure-context.test.ts +99 -62
- package/src/pi/failure-context.ts +67 -64
- package/src/pi/failure-path.test.ts +3 -2
- package/src/pi/file-commit.test.ts +21 -0
- package/src/pi/file-commit.ts +35 -3
- package/src/pi/grep-tool.ts +75 -48
- package/src/pi/grep.test.ts +67 -3
- package/src/pi/mutation-result.ts +46 -9
- package/src/pi/read-tool.ts +7 -10
- package/src/pi/render.test.ts +24 -0
- package/src/pi/render.ts +18 -4
- package/src/pi/replace-tool.ts +134 -130
- package/src/pi/replace.test.ts +118 -1
- package/src/pi/result-safety.test.ts +241 -8
- package/src/pi/rg-line-filter.test.ts +7 -1
- package/src/pi/rg-line-filter.ts +9 -11
- package/src/pi/rg-line-ranges.test.ts +10 -0
- package/src/pi/rg-line-ranges.ts +21 -15
- package/src/pi/write-tool.ts +10 -44
- package/src/pi/write.test.ts +25 -36
package/src/pi/execute.test.ts
CHANGED
|
@@ -39,7 +39,7 @@ function h(text: string, line: number) {
|
|
|
39
39
|
|
|
40
40
|
/** Extract a `LINE#HASH` anchor from a read/edit result text block. */
|
|
41
41
|
function anchorLine(block: string, line: number) {
|
|
42
|
-
const m = new RegExp(`^${line}#([0-9A-Z]+)
|
|
42
|
+
const m = new RegExp(`^${line}#([0-9A-Z]+)(?:│|$)`, "m").exec(block);
|
|
43
43
|
if (!m) throw new Error(`line ${line} anchor not found in block`);
|
|
44
44
|
return `${line}#${m[1]}`;
|
|
45
45
|
}
|
|
@@ -133,6 +133,7 @@ test("edit result returns Updated anchors that chain the next edit without a re-
|
|
|
133
133
|
assert.equal(r1.isError, undefined);
|
|
134
134
|
const out: string = r1.content[0].text;
|
|
135
135
|
assert.match(out, /Updated anchors/);
|
|
136
|
+
assert.doesNotMatch(out, /│/);
|
|
136
137
|
// second edit chains on the anchor returned by the first edit — no read in between
|
|
137
138
|
const r2: any = await call(edit, {
|
|
138
139
|
path: "f.txt",
|
|
@@ -198,23 +199,24 @@ test("edit on a line that changed externally → anchor mismatch", async () => {
|
|
|
198
199
|
});
|
|
199
200
|
});
|
|
200
201
|
|
|
201
|
-
test("unresolved
|
|
202
|
+
test("unresolved anchors require a fresh read and return no context rows", async () => {
|
|
202
203
|
await withDir(async (dir) => {
|
|
203
204
|
const file = join(dir, "recover.txt");
|
|
204
205
|
const observed = ["one", "two", "three", "four", "old marker", "six", "seven", "eight"].join("\n") + "\n";
|
|
205
206
|
await writeFile(file, ["one", "two", "three", "four", "changed", "six", "new marker", "eight"].join("\n") + "\n");
|
|
206
207
|
const edit = makeEditOverride(dir);
|
|
207
|
-
let retryAnchor = "";
|
|
208
208
|
await assert.rejects(call(edit, {
|
|
209
209
|
path: "recover.txt",
|
|
210
210
|
edits: [{ op: "replace", anchor: h(observed, 5), body: ["updated"] }],
|
|
211
211
|
}), (error: Error) => {
|
|
212
212
|
assert.match(error.message, /Anchor mismatch: 1 unresolved/);
|
|
213
213
|
assert.match(error.message, /No changes written by this edit batch/);
|
|
214
|
-
assert.match(error.message,
|
|
215
|
-
|
|
216
|
-
return
|
|
214
|
+
assert.match(error.message, /Use read to inspect the current file before retrying/);
|
|
215
|
+
assert.doesNotMatch(error.message, /^\d+#[0-9A-Z]+│|neighborhoods|Current-file context/m);
|
|
216
|
+
return true;
|
|
217
217
|
});
|
|
218
|
+
const read = await call(makeReadOverride(dir), { path: "recover.txt" });
|
|
219
|
+
const retryAnchor = anchorLine(read.content[0].text, 7);
|
|
218
220
|
await call(edit, { path: "recover.txt", edits: [{ op: "replace", anchor: retryAnchor, body: ["updated"] }] });
|
|
219
221
|
assert.equal(await readFile(file, "utf8"), "one\ntwo\nthree\nfour\nchanged\nsix\nupdated\neight\n");
|
|
220
222
|
});
|
|
@@ -432,6 +434,8 @@ test("read reports an oversized first row without suggesting an ineffective retr
|
|
|
432
434
|
assert.equal(result.details.truncation.outputLines, 0);
|
|
433
435
|
assert.match(result.content[0].text, /cannot return a complete anchor row/);
|
|
434
436
|
assert.doesNotMatch(result.content[0].text, /use offset\/limit/);
|
|
437
|
+
assert.match(result.content[0].text, /Reducing limit cannot split a physical line/);
|
|
438
|
+
assert.match(result.content[0].text, /use bash to inspect it in chunks, or replace/);
|
|
435
439
|
}));
|
|
436
440
|
|
|
437
441
|
test("read preserves empty files and explicit limits above the native default", async () => withDir(async (dir) => {
|
|
@@ -486,17 +490,40 @@ test("invalid anchors and conflicting fields fail before changing the file", asy
|
|
|
486
490
|
assert.throws(() => validateToolArguments(edit as any, { type: "toolCall", id: "invalid", name: "edit", arguments: { path: "invalid.txt", edits: [invalid[0]] } as any }));
|
|
487
491
|
}));
|
|
488
492
|
|
|
489
|
-
test("
|
|
493
|
+
test("local and full-file recovery return copyable anchors without changing the rejected batch", async () => withDir(async (dir) => {
|
|
490
494
|
const original = "a\nb\n";
|
|
491
|
-
await writeFile(join(dir, "shift.txt"), "prefix\n" + original);
|
|
492
495
|
const edit = makeEditOverride(dir);
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
496
|
+
for (const prefixLines of [1, 40]) {
|
|
497
|
+
const prefix = "prefix\n".repeat(prefixLines);
|
|
498
|
+
await writeFile(join(dir, "shift.txt"), prefix + original);
|
|
499
|
+
let replacement = "";
|
|
500
|
+
await assert.rejects(call(edit, { path: "shift.txt", edits: [{ op: "replace", anchor: h(original, 2), body: ["B"] }] }), (error: Error) => {
|
|
501
|
+
replacement = new RegExp(`^(${prefixLines + 2}#[0-9A-Z]+)│b$`, "m").exec(error.message)?.[1] ?? "";
|
|
502
|
+
assert.match(error.message, /Check the intended target/);
|
|
503
|
+
assert.match(error.message, prefixLines === 1
|
|
504
|
+
? /Search: local; matches outside the window were not checked\./ : /Search: full file\./);
|
|
505
|
+
assert.doesNotMatch(error.message, prefixLines === 1 ? /Search: full file/ : /Search: local/);
|
|
506
|
+
assert.doesNotMatch(error.message, /neighborhoods/);
|
|
507
|
+
assert.equal((error.message.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 1);
|
|
508
|
+
return replacement !== "";
|
|
509
|
+
});
|
|
510
|
+
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), prefix + original);
|
|
511
|
+
await call(edit, { path: "shift.txt", edits: [{ op: "replace", anchor: replacement, body: ["B"] }] });
|
|
512
|
+
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), prefix + "a\nB\n");
|
|
513
|
+
}
|
|
514
|
+
}));
|
|
515
|
+
|
|
516
|
+
test("shifted-anchor recovery keeps the read fallback for oversized candidates", async () => withDir(async (dir) => {
|
|
517
|
+
const oversized = "x".repeat(4 * 1024);
|
|
518
|
+
const original = `a\n${oversized}\n`;
|
|
519
|
+
await writeFile(join(dir, "shift-large.txt"), `prefix\n${original}`);
|
|
520
|
+
const edit = makeEditOverride(dir);
|
|
521
|
+
await assert.rejects(call(edit, { path: "shift-large.txt", edits: [{ op: "replace", anchor: h(original, 2), body: ["B"] }] }), (error: Error) => {
|
|
522
|
+
assert.match(error.message, /Candidate content exceeds 4096 bytes/);
|
|
523
|
+
assert.match(error.message, /use read or grep/);
|
|
524
|
+
assert.doesNotMatch(error.message, new RegExp(`│x{${oversized.length}}`));
|
|
525
|
+
return true;
|
|
497
526
|
});
|
|
498
|
-
await call(edit, { path: "shift.txt", edits: [{ op: "replace", anchor: replacement, body: ["B"] }] });
|
|
499
|
-
assert.equal(await readFile(join(dir, "shift.txt"), "utf8"), "prefix\na\nB\n");
|
|
500
527
|
}));
|
|
501
528
|
|
|
502
529
|
test("failed commands preserve mutation results and stay out of all main card renderers", async () => withDir(async (dir) => {
|
|
@@ -568,3 +595,202 @@ test("edit and replace reject NUL output without rewriting source bytes", async
|
|
|
568
595
|
await assert.rejects(call(makeReplaceTool(dir), { path: "output.txt", find: "before", replace: "bad\0text" }), /UNSUPPORTED_TEXT/);
|
|
569
596
|
assert.deepEqual(await readFile(target), original);
|
|
570
597
|
}));
|
|
598
|
+
|
|
599
|
+
test("failed edits expose input status and candidate code for a verified fused retry", async () => withDir(async (dir) => {
|
|
600
|
+
const original = Array.from({ length: 14 }, (_, index) => `line-${index + 1}`);
|
|
601
|
+
original[4] = "if (!fusion) throw new Error();";
|
|
602
|
+
original[5] = "const absolutePath = canonicalPath(cwd, path);";
|
|
603
|
+
const before = `prefix\n${original.join("\n")}\n`;
|
|
604
|
+
const file = join(dir, "candidate.txt");
|
|
605
|
+
await writeFile(file, before);
|
|
606
|
+
let commands = 0;
|
|
607
|
+
const fusion = createActionFusionExecutor(async () => { commands++; return "checked"; });
|
|
608
|
+
const edit = makeEditOverride(dir, fusion);
|
|
609
|
+
const stale = h(original.join("\n"), 5);
|
|
610
|
+
const stable = h(before, 12);
|
|
611
|
+
const candidate = h(before, 6);
|
|
612
|
+
let message = "";
|
|
613
|
+
await assert.rejects(edit.execute("failed", {
|
|
614
|
+
path: file,
|
|
615
|
+
edits: [
|
|
616
|
+
{ op: "insert_after", anchor: stale, body: ["const hashLen = 4;"] },
|
|
617
|
+
{ op: "replace", anchor: stable, body: ["changed"] },
|
|
618
|
+
],
|
|
619
|
+
then_run: { command: "check" },
|
|
620
|
+
}, undefined, undefined, { cwd: dir }), (error: Error) => { message = error.message; return true; });
|
|
621
|
+
assert.match(message, /then_run:skipped/);
|
|
622
|
+
assert.ok(message.includes(`op 0 / anchor / ${stale} / mismatched`));
|
|
623
|
+
assert.ok(message.includes(`op 1 / anchor / ${stable} / matched`));
|
|
624
|
+
assert.ok(message.includes(`checksum-matching candidate ${candidate}.`));
|
|
625
|
+
assert.equal(message.split(`${candidate}│${original[4]}`).length - 1, 1);
|
|
626
|
+
assert.doesNotMatch(message, /0 omitted|Limits:/);
|
|
627
|
+
assert.doesNotMatch(message, /neighborhoods|Current-file context/);
|
|
628
|
+
assert.equal((message.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 1);
|
|
629
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
630
|
+
assert.equal(commands, 0);
|
|
631
|
+
await edit.execute("retry", {
|
|
632
|
+
path: file,
|
|
633
|
+
edits: [
|
|
634
|
+
{ op: "insert_after", anchor: anchorLine(message, 6), body: ["const hashLen = 4;"] },
|
|
635
|
+
{ op: "replace", anchor: stable, body: ["changed"] },
|
|
636
|
+
],
|
|
637
|
+
then_run: { command: "check" },
|
|
638
|
+
}, undefined, undefined, { cwd: dir });
|
|
639
|
+
const expected = splitLines(before);
|
|
640
|
+
expected[11] = "changed";
|
|
641
|
+
expected.splice(6, 0, "const hashLen = 4;");
|
|
642
|
+
assert.equal(await readFile(file, "utf8"), `${expected.join("\n")}\n`);
|
|
643
|
+
assert.equal(commands, 1);
|
|
644
|
+
}));
|
|
645
|
+
|
|
646
|
+
test("ambiguous candidates include distinguishing neighborhoods for a verified retry", async () => withDir(async (dir) => {
|
|
647
|
+
const lines = Array.from({ length: 20 }, (_, index) => `line-${index + 1}`);
|
|
648
|
+
lines[3] = "function primary() {";
|
|
649
|
+
lines[4] = " return value;";
|
|
650
|
+
lines[13] = "function secondary() {";
|
|
651
|
+
lines[14] = " return value;";
|
|
652
|
+
const before = lines.join("\n") + "\n";
|
|
653
|
+
const file = join(dir, "ambiguous.txt");
|
|
654
|
+
await writeFile(file, before);
|
|
655
|
+
const edit = makeEditOverride(dir);
|
|
656
|
+
let context = "";
|
|
657
|
+
await assert.rejects(call(edit, { path: file, edits: [
|
|
658
|
+
{ op: "replace", anchor: h("header\n return value;\n", 2), body: [" return updated;"] },
|
|
659
|
+
] }), (error: Error) => {
|
|
660
|
+
assert.match(error.message, /ambiguous checksum matches/);
|
|
661
|
+
assert.match(error.message, /Search: local; matches outside the window were not checked\./);
|
|
662
|
+
assert.ok(error.message.includes(`"${h(before, 5)}" / "${h(before, 15)}"`));
|
|
663
|
+
context = error.message.split("Ambiguous-candidate neighborhoods")[1];
|
|
664
|
+
assert.ok(context);
|
|
665
|
+
assert.match(context, /@@ candidate-neighborhood lines 2-8 @@/);
|
|
666
|
+
assert.match(context, /@@ candidate-neighborhood lines 12-18 @@/);
|
|
667
|
+
assert.ok(context.includes(`${h(before, 4)}│function primary() {`));
|
|
668
|
+
assert.ok(context.includes(`${h(before, 14)}│function secondary() {`));
|
|
669
|
+
assert.equal(anchorLine(context, 5), h(before, 5));
|
|
670
|
+
return true;
|
|
671
|
+
});
|
|
672
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
673
|
+
await call(edit, { path: file, edits: [{ op: "replace", anchor: anchorLine(context, 15), body: [" return updated;"] }] });
|
|
674
|
+
lines[14] = " return updated;";
|
|
675
|
+
assert.equal(await readFile(file, "utf8"), lines.join("\n") + "\n");
|
|
676
|
+
}));
|
|
677
|
+
|
|
678
|
+
test("input status distinguishes skipped checks and expires before the next retry", async () => withDir(async (dir) => {
|
|
679
|
+
const before = "a\nb\nc\n";
|
|
680
|
+
const file = join(dir, "checks.txt");
|
|
681
|
+
await writeFile(file, before);
|
|
682
|
+
const edit = makeEditOverride(dir);
|
|
683
|
+
const stable = h(before, 1);
|
|
684
|
+
await assert.rejects(call(edit, { path: file, edits: [{ op: "replace", anchor: stable, body: ["bad\nline"] }] }), (error: Error) => {
|
|
685
|
+
assert.ok(error.message.includes(`op 0 / anchor / ${stable} / not_checked`));
|
|
686
|
+
assert.doesNotMatch(error.message, /\/ matched/);
|
|
687
|
+
return true;
|
|
688
|
+
});
|
|
689
|
+
await assert.rejects(call(edit, { path: file, edits: [
|
|
690
|
+
{ op: "replace", anchor: stable, body: ["A"] },
|
|
691
|
+
{ op: "delete", anchor: h(before, 2), end: "3#ZZ" },
|
|
692
|
+
] }), (error: Error) => {
|
|
693
|
+
assert.ok(error.message.includes(`op 0 / anchor / ${stable} / matched`));
|
|
694
|
+
assert.match(error.message, /op 1 \/ end \/ 3#ZZ \/ mismatched/);
|
|
695
|
+
assert.match(error.message, /Anchor checks only; retries revalidate/);
|
|
696
|
+
return true;
|
|
697
|
+
});
|
|
698
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
699
|
+
await writeFile(file, "changed\nb\nc\n");
|
|
700
|
+
await assert.rejects(call(edit, { path: file, edits: [{ op: "replace", anchor: stable, body: ["A"] }] }), (error: Error) => {
|
|
701
|
+
assert.ok(error.message.includes(`op 0 / anchor / ${stable} / mismatched`));
|
|
702
|
+
return true;
|
|
703
|
+
});
|
|
704
|
+
assert.equal(await readFile(file, "utf8"), "changed\nb\nc\n");
|
|
705
|
+
}));
|
|
706
|
+
|
|
707
|
+
test("failed batches return more than forty checks and mappings when byte budgets allow", async () => withDir(async (dir) => {
|
|
708
|
+
const original = "a\ntarget\nz\n";
|
|
709
|
+
const before = `prefix\n${original}`;
|
|
710
|
+
const file = join(dir, "many-checks.txt");
|
|
711
|
+
await writeFile(file, before);
|
|
712
|
+
const edits = Array.from({ length: 45 }, () => ({ op: "replace", anchor: h(original, 2), body: ["changed"] }));
|
|
713
|
+
await assert.rejects(call(makeEditOverride(dir), { path: file, edits }), (error: Error) => {
|
|
714
|
+
assert.doesNotMatch(error.message, /truncated|failure details omitted|Anchor checks: \d+\/\d+/);
|
|
715
|
+
assert.equal((error.message.match(/^op \d+ \/ anchor \/ .* \/ mismatched$/gm) ?? []).length, 45);
|
|
716
|
+
assert.equal((error.message.match(/checksum-matching candidate/g) ?? []).length, 45);
|
|
717
|
+
assert.equal((error.message.match(/^3#[0-9A-Z]+│target$/gm) ?? []).length, 1);
|
|
718
|
+
assert.doesNotMatch(error.message, /\/ matched/);
|
|
719
|
+
return true;
|
|
720
|
+
});
|
|
721
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
722
|
+
}));
|
|
723
|
+
|
|
724
|
+
test("compact edit anchors retain only untouched deletion successors in mixed batches", async () => withDir(async (dir) => {
|
|
725
|
+
const before = "a\nb\nc\nd\ne\nf\ng\n";
|
|
726
|
+
const file = join(dir, "mixed.txt");
|
|
727
|
+
await writeFile(file, before);
|
|
728
|
+
const edit = makeEditOverride(dir);
|
|
729
|
+
const result = await call(edit, { path: file, edits: [
|
|
730
|
+
{ op: "delete", anchor: h(before, 6) },
|
|
731
|
+
{ op: "replace", anchor: h(before, 4), body: ["D"] },
|
|
732
|
+
{ op: "delete", anchor: h(before, 3) },
|
|
733
|
+
{ op: "replace", anchor: h(before, 1), body: ["A", "X"] },
|
|
734
|
+
] });
|
|
735
|
+
const after = "A\nX\nb\nD\ne\ng\n";
|
|
736
|
+
assert.equal(await readFile(file, "utf8"), after);
|
|
737
|
+
const output = result.content[0].text;
|
|
738
|
+
assert.deepEqual(output.split("\n").filter((line: string) => /^\d+#/.test(line)), [
|
|
739
|
+
h(after, 1), h(after, 2), h(after, 4), `${h(after, 6)}│g`,
|
|
740
|
+
]);
|
|
741
|
+
await call(edit, { path: file, edits: [{ op: "replace", anchor: anchorLine(output, 6), body: ["G"] }] });
|
|
742
|
+
assert.equal(await readFile(file, "utf8"), "A\nX\nb\nD\ne\nG\n");
|
|
743
|
+
}));
|
|
744
|
+
|
|
745
|
+
test("unique candidate content is independent of oversized neighboring rows", async () => withDir(async (dir) => {
|
|
746
|
+
const original = "header\ntarget\ntail\n";
|
|
747
|
+
const before = `header\n${"x".repeat(17000)}\ntarget\ntail\n`;
|
|
748
|
+
const file = join(dir, "fallback.txt");
|
|
749
|
+
await writeFile(file, before);
|
|
750
|
+
let candidate = "";
|
|
751
|
+
await assert.rejects(call(makeEditOverride(dir), { path: file, edits: [
|
|
752
|
+
{ op: "replace", anchor: h(original, 2), body: ["updated"] },
|
|
753
|
+
] }), (error: Error) => {
|
|
754
|
+
candidate = anchorLine(error.message, 3);
|
|
755
|
+
assert.equal((error.message.match(/^3#[0-9A-Z]+│target$/gm) ?? []).length, 1);
|
|
756
|
+
assert.doesNotMatch(error.message, /neighborhoods|truncated/);
|
|
757
|
+
assert.equal((error.message.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 1);
|
|
758
|
+
return true;
|
|
759
|
+
});
|
|
760
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
761
|
+
await call(makeEditOverride(dir), { path: file, edits: [{ op: "replace", anchor: candidate, body: ["updated"] }] });
|
|
762
|
+
assert.equal(await readFile(file, "utf8"), before.replace("target", "updated"));
|
|
763
|
+
}));
|
|
764
|
+
|
|
765
|
+
test("CR replacements remain visible in diffs and exact in patches", async () => withDir(async (dir) => {
|
|
766
|
+
const file = join(dir, "cr.txt");
|
|
767
|
+
for (const [before, find, replacement] of [
|
|
768
|
+
["a\rb\n", "\r", "\n"],
|
|
769
|
+
["a\rb\n", "\r", "␍"],
|
|
770
|
+
["a\r\nb\r\n", "\r\n", "\n"],
|
|
771
|
+
]) {
|
|
772
|
+
await writeFile(file, before);
|
|
773
|
+
const result = await call(makeReplaceTool(dir), { path: file, find, replace: replacement });
|
|
774
|
+
assert.equal(await readFile(file, "utf8"), before.replaceAll(find, replacement));
|
|
775
|
+
assert.equal(result.details.firstChangedLine, 1);
|
|
776
|
+
assert.match(result.details.diff, /^-1 a␍/m);
|
|
777
|
+
assert.match(result.details.diff, /^\+1 a/m);
|
|
778
|
+
assert.ok(!result.details.diff.includes("\r"));
|
|
779
|
+
assert.ok(result.details.patch.includes(`-a${before.includes("\r\n") ? "\r\n" : "\rb\n"}`));
|
|
780
|
+
assert.match(result.details.patch, /@@/);
|
|
781
|
+
}
|
|
782
|
+
}));
|
|
783
|
+
|
|
784
|
+
test("deletion successors and unique candidate rows display CR without altering anchors", async () => withDir(async (dir) => {
|
|
785
|
+
const file = join(dir, "cr.txt");
|
|
786
|
+
const before = "remove\na\rb\n";
|
|
787
|
+
await writeFile(file, before);
|
|
788
|
+
const result = await call(makeEditOverride(dir), { path: file, edits: [{ op: "delete", anchor: h(before, 1) }] });
|
|
789
|
+
assert.ok(result.content[0].text.includes(`${h("a\rb\n", 1)}│a␍b`));
|
|
790
|
+
assert.equal(await readFile(file, "utf8"), "a\rb\n");
|
|
791
|
+
await assert.rejects(call(makeEditOverride(dir), { path: file, edits: [{ op: "delete", anchor: h(before, 2) }] }), (error: Error) => {
|
|
792
|
+
assert.ok(error.message.includes(`${h("a\rb\n", 1)}│a␍b`));
|
|
793
|
+
assert.ok(!error.message.includes("\r"));
|
|
794
|
+
return true;
|
|
795
|
+
});
|
|
796
|
+
}));
|
|
@@ -2,87 +2,124 @@ import assert from "node:assert/strict";
|
|
|
2
2
|
import { test } from "node:test";
|
|
3
3
|
import { computeLineHash } from "../core/hash.ts";
|
|
4
4
|
import type { AnchorFailure } from "../core/types.ts";
|
|
5
|
-
import {
|
|
5
|
+
import { createAnchorFormatter } from "./anchor-format.ts";
|
|
6
|
+
import { formatAmbiguousCandidateNeighborhoods } from "./failure-context.ts";
|
|
6
7
|
|
|
7
|
-
function
|
|
8
|
-
return {
|
|
9
|
-
opIndex: 0,
|
|
10
|
-
which: "anchor",
|
|
11
|
-
op: "replace",
|
|
12
|
-
cited: { line, hash: "OLD" },
|
|
13
|
-
recovery: { kind: "none" },
|
|
14
|
-
current: null,
|
|
15
|
-
};
|
|
8
|
+
function failure(recovery: AnchorFailure["recovery"]): AnchorFailure {
|
|
9
|
+
return { opIndex: 0, which: "anchor", op: "replace", cited: { line: 1, hash: "OLD" }, recovery, current: null };
|
|
16
10
|
}
|
|
17
11
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
12
|
+
function ambiguous(lines: readonly string[], positions: number[], hashLen = 4): AnchorFailure {
|
|
13
|
+
return failure({ kind: "ambiguous", scope: "local", candidates: positions.map(line => ({ line, hash: computeLineHash(line, lines[line - 1], hashLen) })) });
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function format(text: string, failures: readonly AnchorFailure[], hashLen = 4) {
|
|
17
|
+
return formatAmbiguousCandidateNeighborhoods(text, failures, createAnchorFormatter(hashLen));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
test("unique and unresolved failures produce no neighborhoods", () => {
|
|
21
|
+
const failures = [failure({ kind: "found", scope: "local", newLine: 2, newHash: "NEW" }), failure({ kind: "none" })];
|
|
22
|
+
assert.deepEqual(format("a\nb\nc\n", failures), { text: "", shownLines: new Set() });
|
|
23
|
+
assert.deepEqual(format("", [failure({ kind: "none" })]), { text: "", shownLines: new Set() });
|
|
21
24
|
});
|
|
22
25
|
|
|
23
|
-
test("
|
|
24
|
-
const lines =
|
|
25
|
-
const result =
|
|
26
|
-
assert.
|
|
27
|
-
assert.match(result,
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
test("ambiguous context merges and sorts windows with CRLF and BOM-aware anchors", () => {
|
|
27
|
+
const lines = ["\uFEFFfirst", "a", "", "c", "d", "e", "f", "last"];
|
|
28
|
+
const { text: result, shownLines } = format(lines.join("\r\n"), [ambiguous(lines, [6, 2], 6)], 6);
|
|
29
|
+
assert.deepEqual([...shownLines], [1, 2, 3, 4, 5, 6, 7, 8]);
|
|
30
|
+
assert.match(result, /@@ candidate-neighborhood lines 1-8 @@/);
|
|
31
|
+
assert.match(result, /Ambiguous-candidate neighborhoods.*observation only/);
|
|
32
|
+
for (let line = 1; line <= lines.length; line++) {
|
|
33
|
+
const row = `${line}#${computeLineHash(line, lines[line - 1], 6)}│${lines[line - 1]}`;
|
|
30
34
|
assert.equal(result.split(row).length - 1, 1);
|
|
31
35
|
}
|
|
36
|
+
assert.doesNotMatch(result, /\r|omitted|truncated/);
|
|
32
37
|
});
|
|
33
38
|
|
|
34
|
-
test("
|
|
35
|
-
const
|
|
36
|
-
const result =
|
|
37
|
-
assert.match(result, /@@ lines 1-4 @@/);
|
|
38
|
-
assert.match(result, /@@ lines 7-10 @@/);
|
|
39
|
-
assert.match(
|
|
39
|
+
test("ambiguous context clips windows at BOF and EOF without padding", () => {
|
|
40
|
+
const lines = Array.from({ length: 10 }, (_, index) => `${index + 1}`);
|
|
41
|
+
const result = format(lines.join("\n"), [ambiguous(lines, [1, 10])]).text;
|
|
42
|
+
assert.match(result, /@@ candidate-neighborhood lines 1-4 @@/);
|
|
43
|
+
assert.match(result, /@@ candidate-neighborhood lines 7-10 @@/);
|
|
44
|
+
assert.equal((result.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 8);
|
|
45
|
+
assert.doesNotMatch(result, /omitted/);
|
|
40
46
|
});
|
|
41
47
|
|
|
42
|
-
test("
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
assert.doesNotMatch(result,
|
|
48
|
+
test("ambiguous context exceeds forty rows and merges repeated candidate windows", () => {
|
|
49
|
+
const lines = Array.from({ length: 60 }, (_, index) => `line-${index + 1}`);
|
|
50
|
+
const result = format(lines.join("\n"), [ambiguous(lines, [4, 14, 24, 34, 44, 54])]).text;
|
|
51
|
+
assert.doesNotMatch(result, /omitted|truncated/);
|
|
52
|
+
assert.equal((result.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 42);
|
|
53
|
+
const repeated = format(lines.join("\n"), Array.from({ length: 50 }, (_, opIndex) => ({ ...ambiguous(lines, [4, 5]), opIndex }))).text;
|
|
54
|
+
assert.equal((repeated.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 8);
|
|
55
|
+
assert.doesNotMatch(repeated, /omitted|truncated/);
|
|
46
56
|
});
|
|
47
57
|
|
|
48
|
-
test("
|
|
49
|
-
const
|
|
50
|
-
const result =
|
|
51
|
-
assert.match(
|
|
52
|
-
assert.match(result,
|
|
53
|
-
assert.
|
|
54
|
-
assert.doesNotMatch(result, /86#[0-9A-Z]+│line 86/);
|
|
58
|
+
test("ambiguous context uses the same first eight candidates as the detail list", () => {
|
|
59
|
+
const lines = Array.from({ length: 90 }, (_, index) => `line-${index + 1}`);
|
|
60
|
+
const result = format(lines.join("\n"), [ambiguous(lines, [4, 14, 24, 34, 44, 54, 64, 74, 84])]).text;
|
|
61
|
+
assert.equal((result.match(/^\d+#[0-9A-Z]+│/gm) ?? []).length, 56);
|
|
62
|
+
assert.match(result, /^77#[0-9A-Z]+│line-77$/m);
|
|
63
|
+
assert.doesNotMatch(result, /^8[1-7]#[0-9A-Z]+│/m);
|
|
55
64
|
});
|
|
56
65
|
|
|
57
|
-
test("
|
|
58
|
-
const
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
assert.
|
|
62
|
-
assert.match(
|
|
63
|
-
assert.
|
|
66
|
+
test("ambiguous context preserves complete rows at the byte budget", () => {
|
|
67
|
+
const exact = "x".repeat(16 * 1024 - Buffer.byteLength("1#XXXX│\n"));
|
|
68
|
+
const lines = [exact, "target", "last"];
|
|
69
|
+
const byteLimited = format(lines.join("\n"), [ambiguous(lines, [2, 3])]).text;
|
|
70
|
+
assert.ok(byteLimited.includes(`1#${computeLineHash(1, exact)}│${exact}\n`));
|
|
71
|
+
assert.match(byteLimited, /Candidate-neighborhood rows: 1\/3; 2 omitted/);
|
|
72
|
+
assert.match(byteLimited, /truncated: byte limit/);
|
|
73
|
+
assert.doesNotMatch(byteLimited, /^2#[0-9A-Z]+│/m);
|
|
74
|
+
const oversizedFirst = ["界".repeat(6000), "target", "last"];
|
|
75
|
+
const skippedFirst = format(oversizedFirst.join("\n"), [ambiguous(oversizedFirst, [2, 3])]).text;
|
|
76
|
+
assert.match(skippedFirst, /Candidate-neighborhood rows: 2\/3; 1 omitted/);
|
|
77
|
+
assert.doesNotMatch(skippedFirst, /^1#[0-9A-Z]+│/m);
|
|
78
|
+
assert.match(skippedFirst, /^2#[0-9A-Z]+│target$/m);
|
|
79
|
+
assert.match(skippedFirst, /^3#[0-9A-Z]+│last$/m);
|
|
80
|
+
const oversizedMiddle = ["short", "界".repeat(6000), "target", "last"];
|
|
81
|
+
const partial = format(oversizedMiddle.join("\n"), [ambiguous(oversizedMiddle, [3, 4])]).text;
|
|
82
|
+
assert.match(partial, /Candidate-neighborhood rows: 3\/4; 1 omitted/);
|
|
83
|
+
assert.doesNotMatch(partial, /^2#[0-9A-Z]+│/m);
|
|
84
|
+
assert.match(partial, /@@ candidate-neighborhood lines 1-1 @@/);
|
|
85
|
+
assert.match(partial, /@@ candidate-neighborhood lines 3-4 @@/);
|
|
86
|
+
const allOversized = ["x".repeat(4096), "y".repeat(4096)];
|
|
87
|
+
const empty = format(allOversized.join("\n"), [ambiguous(allOversized, [1, 2])]).text;
|
|
88
|
+
assert.match(empty, /No complete neighborhood row fits the limits/);
|
|
89
|
+
assert.match(empty, /Candidate-neighborhood rows: 0\/2; 2 omitted/);
|
|
64
90
|
});
|
|
65
91
|
|
|
66
|
-
test("
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
92
|
+
test("ambiguous context enforces the complete candidate row byte limit", () => {
|
|
93
|
+
for (const extra of [0, 1]) {
|
|
94
|
+
const candidate = "x".repeat(4096 - Buffer.byteLength("2#XXXXXX│") + extra);
|
|
95
|
+
const lines = ["prefix", candidate, "tail", "other"];
|
|
96
|
+
const output = format(lines.join("\n"), [ambiguous(lines, [2, 4], 6)], 6).text;
|
|
97
|
+
if (extra === 0) {
|
|
98
|
+
assert.ok(output.includes(`2#${computeLineHash(2, candidate, 6)}│${candidate}\n`));
|
|
99
|
+
assert.doesNotMatch(output, /omitted/);
|
|
100
|
+
} else {
|
|
101
|
+
assert.match(output, /truncated: candidate row limit/);
|
|
102
|
+
assert.doesNotMatch(output, /^2#[0-9A-Z]+│/m);
|
|
103
|
+
assert.match(output, /^4#[0-9A-Z]+│other$/m);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
73
106
|
});
|
|
74
107
|
|
|
75
|
-
test("
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
108
|
+
test("unique candidates inside ambiguous neighborhoods retain the candidate row limit", () => {
|
|
109
|
+
const lines = ["prefix", "x".repeat(4096), "match", "match"];
|
|
110
|
+
const output = format(lines.join("\n"), [
|
|
111
|
+
ambiguous(lines, [3, 4]),
|
|
112
|
+
failure({ kind: "found", scope: "local", newLine: 2, newHash: computeLineHash(2, lines[1]) }),
|
|
113
|
+
]).text;
|
|
114
|
+
assert.match(output, /truncated: candidate row limit/);
|
|
115
|
+
assert.doesNotMatch(output, /^2#[0-9A-Z]+│/m);
|
|
81
116
|
});
|
|
82
117
|
|
|
83
|
-
test("
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
assert.
|
|
87
|
-
assert.match(
|
|
118
|
+
test("neighborhoods keep shorter later rows when the remaining budget cannot fit a row", () => {
|
|
119
|
+
const lines = ["x".repeat(16 * 1024 - 100), "y".repeat(200), "target", "other"];
|
|
120
|
+
const output = format(lines.join("\n"), [ambiguous(lines, [3, 4])]);
|
|
121
|
+
assert.deepEqual([...output.shownLines], [1, 3, 4]);
|
|
122
|
+
assert.match(output.text, /Candidate-neighborhood rows: 3\/4; 1 omitted/);
|
|
123
|
+
const rows = output.text.match(/^\d+#[0-9A-Z]+│.*$/gm) ?? [];
|
|
124
|
+
assert.ok(Buffer.byteLength(rows.join("\n") + "\n") <= 16 * 1024);
|
|
88
125
|
});
|