dsh-better-edit 0.4.1 → 0.5.1
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/CHANGELOG.md +26 -1
- package/README.md +2 -1
- package/lib/contract.js +1 -1
- package/lib/edit-response.js +14 -11
- package/lib/encoding.js +308 -0
- package/lib/file-view.js +1 -1
- package/lib/fs-bridge.js +266 -4
- package/lib/fs-write.js +15 -6
- package/lib/hash-store.js +1 -1
- package/lib/hashline/anchor-pipeline.js +278 -43
- package/lib/hashline/hash-assign.js +18 -3
- package/lib/hashline/index.js +1 -1
- package/lib/prompts.js +14 -11
- package/lib/read-and-serve.js +5 -0
- package/lib/store-config.js +132 -2
- package/lib/tool-read.js +24 -4
- package/lib/types/contract.d.ts +1 -0
- package/lib/types/encoding.d.ts +46 -0
- package/lib/types/file-view.d.ts +1 -0
- package/lib/types/fs-bridge.d.ts +12 -1
- package/lib/types/hashline/anchor-pipeline.d.ts +7 -0
- package/lib/types/hashline/hash-assign.d.ts +2 -0
- package/lib/types/hashline/index.d.ts +1 -1
- package/lib/types/read-and-serve.d.ts +3 -0
- package/lib/types/store-config.d.ts +10 -1
- package/package.json +21 -3
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* @module dsh-better-edit/hashline/anchor-pipeline
|
|
20
20
|
*/
|
|
21
21
|
import { abortIf, splitLines, rejectUnknownFields, firstNonEmptyIndex, lastNonEmptyIndex, clipLine } from "../utils.js";
|
|
22
|
-
import { HASH_CLASS, HL_BARE_PREFIX_RE, HL_PREFIX_PLUS_RE, HL_PREFIX_MINUS_RE, HASH_SEP, ANCHOR_LEN, ALPH_RE, canon, lineHashesPure } from "./hash-assign.js";
|
|
22
|
+
import { HASH_CLASS, HL_BARE_PREFIX_RE, HL_PREFIX_PLUS_RE, HL_PREFIX_MINUS_RE, HASH_SEP, ANCHOR_LEN, ALPH_RE, canon, lineHashesPure, getCanonForHash, rememberHashCanon } from "./hash-assign.js";
|
|
23
23
|
import { recordServed, servedPositionsOf } from "../served-store.js";
|
|
24
24
|
import { SERVED_ECHO_CAP } from "../constants.js";
|
|
25
25
|
import { NEW_CONTENT_NOT_STRING_MSG } from "../constants.js";
|
|
@@ -31,6 +31,18 @@ function diagRef(ref) {
|
|
|
31
31
|
if (/^\d+/.test(trimmed)) {
|
|
32
32
|
return `[E_BAD_REF] Invalid anchor. Use the hash alone (e.g. "aB3") — no line numbers or trailing content.`;
|
|
33
33
|
}
|
|
34
|
+
if (trimmed.includes("│") && trimmed.includes("\n")) {
|
|
35
|
+
const lines = trimmed.split("\n");
|
|
36
|
+
const first = lines[0] ?? "";
|
|
37
|
+
const last = lines[lines.length - 1] ?? "";
|
|
38
|
+
const hashRe = new RegExp(HASH_CLASS);
|
|
39
|
+
const firstMatch = first.match(hashRe);
|
|
40
|
+
const lastMatch = last.match(hashRe);
|
|
41
|
+
const firstHash = firstMatch?.[0] ?? "wUp";
|
|
42
|
+
const lastHash = lastMatch?.[0] ?? "AU6";
|
|
43
|
+
const preview = first.slice(0, 60);
|
|
44
|
+
return `[E_BAD_REF] Invalid anchor — remove_from must be a single bare 3-char hash (e.g. "wUp"), not a block with HASH│. Received ${lines.length} lines starting "${preview}…" — use only the first hash "${firstHash}" as remove_from and "${lastHash}" as remove_to, and put the new content (without HASH│) in replacement_text.`;
|
|
45
|
+
}
|
|
34
46
|
if (trimmed.includes("│")) {
|
|
35
47
|
return `[E_BAD_REF] Invalid anchor "${trimmed}". remove_from and remove_to must contain the 3-char hash only — remove everything from "│" onward.`;
|
|
36
48
|
}
|
|
@@ -151,11 +163,30 @@ function assertItem(edit) {
|
|
|
151
163
|
}
|
|
152
164
|
}
|
|
153
165
|
const ANCHOR_ROW_RE = new RegExp(`^([+-]?)(${HASH_CLASS})│`);
|
|
166
|
+
function firstHashFromBlock(block) {
|
|
167
|
+
for (const line of block.split("\n")) {
|
|
168
|
+
const m = line.match(ANCHOR_ROW_RE);
|
|
169
|
+
if (m)
|
|
170
|
+
return m[2];
|
|
171
|
+
const bare = line.match(new RegExp(HASH_CLASS));
|
|
172
|
+
if (bare)
|
|
173
|
+
return bare[0];
|
|
174
|
+
}
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
154
177
|
export function resEdit(edit, warnings) {
|
|
155
178
|
assertItem(edit);
|
|
156
179
|
const editLines = parseText(edit.replacement_text);
|
|
157
180
|
const bounds = [edit.remove_from, edit.remove_to].map((ref) => {
|
|
158
181
|
const trimmed = ref.trim();
|
|
182
|
+
if (trimmed.includes("\n")) {
|
|
183
|
+
const hash = firstHashFromBlock(trimmed);
|
|
184
|
+
if (hash) {
|
|
185
|
+
const lines = trimmed.split("\n").length;
|
|
186
|
+
warnings?.push(`[E_BAD_REF] extracted first hash "${hash}" from ${lines}-line block — use bare "${hash}" next time`);
|
|
187
|
+
return hash;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
159
190
|
const match = trimmed.match(ANCHOR_ROW_RE);
|
|
160
191
|
if (match) {
|
|
161
192
|
let message;
|
|
@@ -201,7 +232,12 @@ function stripBarePrefixes(edit, fileHashes, warnings) {
|
|
|
201
232
|
.join(", ");
|
|
202
233
|
const matchedCount = stripped.filter((s) => s.matched).length;
|
|
203
234
|
const evidence = matchedCount === 0 ? "0 matched — verify literal 'HASH│' content" : `${matchedCount}/${stripped.length} matched`;
|
|
204
|
-
|
|
235
|
+
if (matchedCount === stripped.length) {
|
|
236
|
+
warnings.push(`[info E_BARE_HASH_PREFIX] stripped "HASH│" prefix from ${locations} (${evidence}) — use bare content without HASH│ next time.`);
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
warnings.push(`[E_BARE_HASH_PREFIX] stripped "HASH│" prefix from ${locations} (${evidence}).`);
|
|
240
|
+
}
|
|
205
241
|
return { ...edit, content_lines: contentLines };
|
|
206
242
|
}
|
|
207
243
|
/** @internal — private to anchor-pipeline seam */
|
|
@@ -439,6 +475,21 @@ export class AnchorMismatchError extends Error {
|
|
|
439
475
|
export function isAnchorMismatch(error) {
|
|
440
476
|
return error instanceof AnchorMismatchError;
|
|
441
477
|
}
|
|
478
|
+
export function findEditHashEcho(replacementLines, served, startLine) {
|
|
479
|
+
for (let k = 0; k < replacementLines.length; k++) {
|
|
480
|
+
const pos = startLine + k - 1;
|
|
481
|
+
if (pos < served.length && served[pos] !== null && replacementLines[k].startsWith(served[pos] + HASH_SEP)) {
|
|
482
|
+
return { k: k + 1, hash: served[pos] };
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return undefined;
|
|
486
|
+
}
|
|
487
|
+
export class EditHashEchoError extends AnchorMismatchError {
|
|
488
|
+
constructor(message, servedRows = []) {
|
|
489
|
+
super(message, servedRows);
|
|
490
|
+
this.name = "EditHashEchoError";
|
|
491
|
+
}
|
|
492
|
+
}
|
|
442
493
|
export function buildRangeEcho(startLine, endLine, fileHashes) {
|
|
443
494
|
const total = endLine - startLine + 1;
|
|
444
495
|
const shown = Math.min(total, SERVED_ECHO_CAP);
|
|
@@ -462,6 +513,20 @@ function paginationHint(nextOffset, more) {
|
|
|
462
513
|
export function verifyServedRange(args) {
|
|
463
514
|
const { served, startHash, endHash, startLine, endLine, fileHashes, fileLines, filePath, } = args;
|
|
464
515
|
const where = filePath ? ` in ${filePath}` : "";
|
|
516
|
+
let isHealed = false;
|
|
517
|
+
for (let i = 0; i < fileHashes.length; i++) {
|
|
518
|
+
const h = fileHashes[i];
|
|
519
|
+
if (getCanonForHash(h) === undefined)
|
|
520
|
+
rememberHashCanon(h, canon(fileLines[i] ?? ""));
|
|
521
|
+
}
|
|
522
|
+
for (let i = 0; i < served.length; i++) {
|
|
523
|
+
const h = served[i];
|
|
524
|
+
if (h !== null && getCanonForHash(h) === undefined) {
|
|
525
|
+
const pos = fileHashes.indexOf(h);
|
|
526
|
+
if (pos >= 0)
|
|
527
|
+
rememberHashCanon(h, canon(fileLines[pos] ?? ""));
|
|
528
|
+
}
|
|
529
|
+
}
|
|
465
530
|
const echoRows = buildRangeEcho(startLine, endLine, fileHashes);
|
|
466
531
|
const totalLen = endLine - startLine + 1;
|
|
467
532
|
const tail = echoRows.length < totalLen
|
|
@@ -507,56 +572,215 @@ export function verifyServedRange(args) {
|
|
|
507
572
|
}
|
|
508
573
|
}
|
|
509
574
|
if (from === undefined || to === undefined) {
|
|
510
|
-
|
|
511
|
-
if (startPositions.length ===
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
575
|
+
let healed;
|
|
576
|
+
if (startPositions.length === 1 && endPositions.length === 1) {
|
|
577
|
+
const sPos = startPositions[0];
|
|
578
|
+
const ePos = endPositions[0];
|
|
579
|
+
const servedFrom = Math.min(sPos, ePos);
|
|
580
|
+
const servedTo = Math.max(sPos, ePos);
|
|
581
|
+
const servedLen = servedTo - servedFrom + 1;
|
|
582
|
+
if (servedLen === currentLen) {
|
|
583
|
+
const expectedCanons = [];
|
|
584
|
+
let canBuild = true;
|
|
585
|
+
for (let k = 0; k < servedLen; k++) {
|
|
586
|
+
const h = served[servedFrom + k];
|
|
587
|
+
if (h === null) {
|
|
588
|
+
canBuild = false;
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
const c = getCanonForHash(h);
|
|
592
|
+
if (c === undefined) {
|
|
593
|
+
canBuild = false;
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
expectedCanons.push(c);
|
|
597
|
+
}
|
|
598
|
+
if (canBuild) {
|
|
599
|
+
const matches = [];
|
|
600
|
+
for (let i = 0; i <= fileLines.length - servedLen; i++) {
|
|
601
|
+
let ok = true;
|
|
602
|
+
for (let k = 0; k < servedLen; k++) {
|
|
603
|
+
if (canon(fileLines[i + k] ?? "") !== expectedCanons[k]) {
|
|
604
|
+
ok = false;
|
|
605
|
+
break;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
if (ok)
|
|
609
|
+
matches.push(i);
|
|
610
|
+
if (matches.length > 1)
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
if (matches.length === 1) {
|
|
614
|
+
healed = { from: matches[0], to: matches[0] + servedLen - 1 };
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
516
618
|
}
|
|
517
|
-
if (
|
|
518
|
-
|
|
619
|
+
if (!healed) {
|
|
620
|
+
const hasServed = served.some((h) => h !== null);
|
|
621
|
+
const startInFile = fileHashes.includes(startHash);
|
|
622
|
+
const endInFile = fileHashes.includes(endHash);
|
|
623
|
+
if (hasServed && (!startInFile || !endInFile)) {
|
|
624
|
+
const startCanon = getCanonForHash(startHash);
|
|
625
|
+
const endCanon = getCanonForHash(endHash);
|
|
626
|
+
if (startCanon !== undefined && endCanon !== undefined) {
|
|
627
|
+
const startMatches = [];
|
|
628
|
+
const endMatches = [];
|
|
629
|
+
for (let i = 0; i < fileLines.length; i++) {
|
|
630
|
+
if (canon(fileLines[i] ?? "") === startCanon)
|
|
631
|
+
startMatches.push(i);
|
|
632
|
+
if (canon(fileLines[i] ?? "") === endCanon)
|
|
633
|
+
endMatches.push(i);
|
|
634
|
+
if (startMatches.length > 1 && endMatches.length > 1)
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
if (startMatches.length === 1 && endMatches.length === 1) {
|
|
638
|
+
const s = startMatches[0];
|
|
639
|
+
const e = endMatches[0];
|
|
640
|
+
const healedFrom = Math.min(s, e);
|
|
641
|
+
const healedTo = Math.max(s, e);
|
|
642
|
+
if (healedTo - healedFrom + 1 === currentLen) {
|
|
643
|
+
let interiorOk = true;
|
|
644
|
+
if (currentLen > 2) {
|
|
645
|
+
const healedCanons = [];
|
|
646
|
+
for (let k = 0; k < currentLen; k++)
|
|
647
|
+
healedCanons.push(canon(fileLines[healedFrom + k] ?? ""));
|
|
648
|
+
let count = 0;
|
|
649
|
+
for (let i = 0; i <= fileLines.length - currentLen; i++) {
|
|
650
|
+
let ok = true;
|
|
651
|
+
for (let k = 0; k < currentLen; k++)
|
|
652
|
+
if (canon(fileLines[i + k] ?? "") !== healedCanons[k]) {
|
|
653
|
+
ok = false;
|
|
654
|
+
break;
|
|
655
|
+
}
|
|
656
|
+
if (ok)
|
|
657
|
+
count++;
|
|
658
|
+
if (count > 1)
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
if (count !== 1)
|
|
662
|
+
interiorOk = false;
|
|
663
|
+
}
|
|
664
|
+
if (interiorOk)
|
|
665
|
+
healed = { from: healedFrom, to: healedTo };
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
519
670
|
}
|
|
520
|
-
|
|
521
|
-
|
|
671
|
+
if (healed) {
|
|
672
|
+
from = healed.from;
|
|
673
|
+
to = healed.to;
|
|
674
|
+
isHealed = true;
|
|
522
675
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
`
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
676
|
+
else {
|
|
677
|
+
const problems = [];
|
|
678
|
+
if (startPositions.length === 0) {
|
|
679
|
+
problems.push(`remove_from "${startHash}" has no served position`);
|
|
680
|
+
}
|
|
681
|
+
else if (startPositions.length > 1) {
|
|
682
|
+
problems.push(`remove_from "${startHash}" was served at ${startPositions.length} positions`);
|
|
683
|
+
}
|
|
684
|
+
if (endPositions.length === 0) {
|
|
685
|
+
problems.push(`remove_to "${endHash}" has no served position`);
|
|
686
|
+
}
|
|
687
|
+
else if (endPositions.length > 1) {
|
|
688
|
+
problems.push(`remove_to "${endHash}" was served at ${endPositions.length} positions`);
|
|
689
|
+
}
|
|
534
690
|
throw new ServedRejectionError({
|
|
535
|
-
code: "
|
|
536
|
-
message: `[
|
|
537
|
-
|
|
691
|
+
code: "E_RANGE_UNVERIFIED",
|
|
692
|
+
message: `[E_RANGE_UNVERIFIED] cannot verify range against served state${where}: ${problems.join("; ")}. ` +
|
|
693
|
+
`No served span matched the current range (${currentLen} lines). ` +
|
|
694
|
+
`A full read will re-sync the served mirror — the echoed range below is current content, ` +
|
|
695
|
+
`but retrying without re-reading cannot clear a stale duplicate outside the echoed window.\n` +
|
|
696
|
+
`Current range:\n${echo}`,
|
|
538
697
|
servedRows: echoRows,
|
|
539
698
|
});
|
|
540
699
|
}
|
|
541
700
|
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
701
|
+
if (isHealed) {
|
|
702
|
+
for (let k = 0; k < currentLen; k++) {
|
|
703
|
+
const servedHash = served[from + k];
|
|
704
|
+
if (servedHash === null)
|
|
705
|
+
continue;
|
|
706
|
+
const expectedCanon = getCanonForHash(servedHash);
|
|
707
|
+
const actualCanon = canon(fileLines[from + k] ?? "");
|
|
708
|
+
if (expectedCanon !== undefined && expectedCanon !== actualCanon) {
|
|
709
|
+
const offendingLine = from + k + 1;
|
|
710
|
+
throw new ServedRejectionError({
|
|
711
|
+
code: "E_RANGE_STALE",
|
|
712
|
+
message: `[E_RANGE_STALE] line ${offendingLine}${where} differs from what was served.\nCurrent range:\n${echo}\n${retryHint()}`,
|
|
713
|
+
firstOffendingLine: offendingLine,
|
|
714
|
+
servedRows: echoRows,
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
}
|
|
550
718
|
}
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
719
|
+
else {
|
|
720
|
+
for (let i = from; i <= to; i++) {
|
|
721
|
+
if (served[i] === null) {
|
|
722
|
+
throw new ServedRejectionError({
|
|
723
|
+
code: "E_RANGE_UNSERVED",
|
|
724
|
+
message: `[E_RANGE_UNSERVED] line ${i + 1}${where} was never served.\nCurrent range:\n${echo}\n${retryHint()}`,
|
|
725
|
+
firstOffendingLine: i + 1,
|
|
726
|
+
servedRows: echoRows,
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
const servedLen = to - from + 1;
|
|
731
|
+
if (servedLen !== currentLen) {
|
|
732
|
+
let lenHealed = false;
|
|
733
|
+
const expectedCanons = [];
|
|
734
|
+
let canBuild = true;
|
|
735
|
+
for (let k = 0; k < servedLen; k++) {
|
|
736
|
+
const h = served[from + k];
|
|
737
|
+
if (h === null) {
|
|
738
|
+
canBuild = false;
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
const c = getCanonForHash(h);
|
|
742
|
+
if (c === undefined) {
|
|
743
|
+
canBuild = false;
|
|
744
|
+
break;
|
|
745
|
+
}
|
|
746
|
+
expectedCanons.push(c);
|
|
747
|
+
}
|
|
748
|
+
if (canBuild) {
|
|
749
|
+
let matches = 0;
|
|
750
|
+
for (let i = 0; i <= fileLines.length - servedLen; i++) {
|
|
751
|
+
let ok = true;
|
|
752
|
+
for (let k = 0; k < servedLen; k++)
|
|
753
|
+
if (canon(fileLines[i + k] ?? "") !== expectedCanons[k]) {
|
|
754
|
+
ok = false;
|
|
755
|
+
break;
|
|
756
|
+
}
|
|
757
|
+
if (ok)
|
|
758
|
+
matches++;
|
|
759
|
+
if (matches > 1)
|
|
760
|
+
break;
|
|
761
|
+
}
|
|
762
|
+
if (matches === 1)
|
|
763
|
+
lenHealed = true;
|
|
764
|
+
}
|
|
765
|
+
if (!lenHealed) {
|
|
766
|
+
throw new ServedRejectionError({
|
|
767
|
+
code: "E_RANGE_STALE",
|
|
768
|
+
message: `[E_RANGE_STALE] served span (${servedLen} lines) no longer matches current range (${currentLen} lines)${where}.\nCurrent range:\n${echo}\n${retryHint()}`,
|
|
769
|
+
firstOffendingLine: startLine,
|
|
770
|
+
servedRows: echoRows,
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
for (let k = 0; k < servedLen; k++) {
|
|
775
|
+
if (served[from + k] !== fileHashes[startLine - 1 + k]) {
|
|
776
|
+
const offendingLine = startLine + k;
|
|
777
|
+
throw new ServedRejectionError({
|
|
778
|
+
code: "E_RANGE_STALE",
|
|
779
|
+
message: `[E_RANGE_STALE] line ${offendingLine}${where} differs from what was served.\nCurrent range:\n${echo}\n${retryHint()}`,
|
|
780
|
+
firstOffendingLine: offendingLine,
|
|
781
|
+
servedRows: echoRows,
|
|
782
|
+
});
|
|
783
|
+
}
|
|
560
784
|
}
|
|
561
785
|
}
|
|
562
786
|
}
|
|
@@ -694,6 +918,17 @@ export function applyEdit(content, edit, signal, precomputedHashes, filePath, se
|
|
|
694
918
|
resolved = correctedResult.resolved;
|
|
695
919
|
}
|
|
696
920
|
if (served) {
|
|
921
|
+
const startLineEcho = resolved.hash_bounds[0].line;
|
|
922
|
+
const rawEcho = findEditHashEcho(edit.content_lines, served, startLineEcho);
|
|
923
|
+
let echo = rawEcho;
|
|
924
|
+
if (!echo)
|
|
925
|
+
echo = findEditHashEcho(resolved.content_lines, served, startLineEcho);
|
|
926
|
+
if (!echo)
|
|
927
|
+
echo = findEditHashEcho(prefixFixed.content_lines, served, startLineEcho);
|
|
928
|
+
if (echo) {
|
|
929
|
+
const msg = `[E_EDIT_HASH_ECHO] Refused edit to ${filePath ?? "(unknown file)"}: replacement line ${echo.k} begins with the exact ${echo.hash}${HASH_SEP} anchor served for this session, path, and range-relative line. Remove the copied anchors and retry. Nothing was written.`;
|
|
930
|
+
throw new EditHashEchoError(msg, []);
|
|
931
|
+
}
|
|
697
932
|
const startAnchor = resolved.hash_bounds[0];
|
|
698
933
|
const endAnchor = resolved.hash_bounds[1];
|
|
699
934
|
verifyServedRange({
|
|
@@ -67,6 +67,14 @@ function hashAt(idx) {
|
|
|
67
67
|
}
|
|
68
68
|
return hash;
|
|
69
69
|
}
|
|
70
|
+
const hashToCanon = new Map();
|
|
71
|
+
export function rememberHashCanon(hash, canonText) {
|
|
72
|
+
if (!hashToCanon.has(hash))
|
|
73
|
+
hashToCanon.set(hash, canonText);
|
|
74
|
+
}
|
|
75
|
+
export function getCanonForHash(hash) {
|
|
76
|
+
return hashToCanon.get(hash);
|
|
77
|
+
}
|
|
70
78
|
export const HL_PREFIX_PLUS_RE = new RegExp(`^\\+${HASH_CLASS}│`);
|
|
71
79
|
export const HL_PREFIX_MINUS_RE = new RegExp(`^-(?:${HASH_CLASS}│| {${ANCHOR_LEN}}│)`);
|
|
72
80
|
export const HL_BARE_PREFIX_RE = new RegExp(`^\\s*(${HASH_CLASS})│`);
|
|
@@ -122,7 +130,9 @@ export function lineHashesPure(content) {
|
|
|
122
130
|
for (let i = 0; i < lines.length; i++) {
|
|
123
131
|
const c = getCanon(canonCache, lines[i]);
|
|
124
132
|
const baseIdx = (xxh32(c) >>> 14) % HASH_SPACE;
|
|
125
|
-
|
|
133
|
+
const h = assignHash(used, baseIdx, hint);
|
|
134
|
+
hashes[i] = h;
|
|
135
|
+
rememberHashCanon(h, c);
|
|
126
136
|
}
|
|
127
137
|
return hashes;
|
|
128
138
|
}
|
|
@@ -223,6 +233,7 @@ export function mapStableHashes(oldContent, oldHashes, newContent, removedHashes
|
|
|
223
233
|
const newIdx = candidates.splice(pos, 1)[0];
|
|
224
234
|
newHashes[newIdx] = entry.hash;
|
|
225
235
|
markUsed(entry.hash);
|
|
236
|
+
rememberHashCanon(entry.hash, getCanon(canonCache, oldLines[entry.index]));
|
|
226
237
|
}
|
|
227
238
|
const removedByContent = new Map();
|
|
228
239
|
for (const entry of removedEntries) {
|
|
@@ -240,15 +251,19 @@ export function mapStableHashes(oldContent, oldHashes, newContent, removedHashes
|
|
|
240
251
|
const queue = removedByContent.get(getCanon(canonCache, newLines[i]));
|
|
241
252
|
if (!queue || queue.pos >= queue.hashes.length)
|
|
242
253
|
continue;
|
|
243
|
-
|
|
254
|
+
const h = queue.hashes[queue.pos];
|
|
255
|
+
newHashes[i] = h;
|
|
244
256
|
queue.pos += 1;
|
|
257
|
+
rememberHashCanon(h, getCanon(canonCache, newLines[i]));
|
|
245
258
|
}
|
|
246
259
|
for (let i = 0; i < newLines.length; i++) {
|
|
247
260
|
if (newHashes[i])
|
|
248
261
|
continue;
|
|
249
262
|
const c = getCanon(canonCache, newLines[i]);
|
|
250
263
|
const baseIdx = (xxh32(c) >>> 14) % HASH_SPACE;
|
|
251
|
-
|
|
264
|
+
const h = assignHash(used, baseIdx, hint);
|
|
265
|
+
newHashes[i] = h;
|
|
266
|
+
rememberHashCanon(h, c);
|
|
252
267
|
}
|
|
253
268
|
return newHashes;
|
|
254
269
|
}
|
package/lib/hashline/index.js
CHANGED
|
@@ -7,4 +7,4 @@ export { CANON_VERSION, ANCHOR_LEN, HASH_SEP, HASH_SPACE, HASH_PROBE_STRIDE, MAX
|
|
|
7
7
|
export { lineHashes } from "./hash.js";
|
|
8
8
|
export { parseHashRef, parseText } from "./parse.js";
|
|
9
9
|
export { resEdit } from "./anchor-pipeline.js";
|
|
10
|
-
export { applyEdit, fmtRegion, changedRange, buildIdx, ServedRejectionError, AnchorMismatchError, isServedRejection, isAnchorMismatch, verifyServedRange, buildRangeEcho, fmtServedRows, } from "./anchor-pipeline.js";
|
|
10
|
+
export { applyEdit, fmtRegion, changedRange, buildIdx, ServedRejectionError, AnchorMismatchError, EditHashEchoError, findEditHashEcho, isServedRejection, isAnchorMismatch, verifyServedRange, buildRangeEcho, fmtServedRows, } from "./anchor-pipeline.js";
|
package/lib/prompts.js
CHANGED
|
@@ -6,19 +6,22 @@
|
|
|
6
6
|
* followed by tight bullets.
|
|
7
7
|
* @module dsh-better-edit/prompts
|
|
8
8
|
*/
|
|
9
|
-
export const EDIT_DESCRIPTION = "Edit a range of lines in a text file
|
|
10
|
-
|
|
11
|
-
"remove_from
|
|
12
|
-
|
|
13
|
-
"
|
|
9
|
+
export const EDIT_DESCRIPTION = "Edit a range of lines in a text file with `{ \"path\": path, \"edits\": [[remove_from, remove_to, replacement_text], ...] }`. " +
|
|
10
|
+
"`path` is the file path or `null`. `read` returns `HASH\u2502content` (e.g. `wUp\u2502 \"site\": {`) \u2014 for `edit`, " +
|
|
11
|
+
"`remove_from`/`remove_to` are HASH anchors (bare 3-char value before `\u2502`, e.g. \"wUp\", \"AU6\"), never `HASH\u2502content` or line content. " +
|
|
12
|
+
"`replacement_text` is bare content without `HASH\u2502`, lines joined by \\n (e.g. \" \\\"site\\\": {\\n \\\"class\\\": SiteScraper,\"); use \"\" to delete. " +
|
|
13
|
+
"Example: read shows `wUp\u2502 \"site\": {` + `AU6\u2502 \"name\": \"old\",` \u2192 edit " +
|
|
14
|
+
"`{\"path\":\"scrape.py\",\"edits\":[[\"wUp\",\"AU6\",\" \\\"site\\\": {\\n \\\"class\\\": SiteScraper,\"]]}`. " +
|
|
15
|
+
"Edits in one call apply atomically; after success reuse `HASH\u2502content` from the returned diff for the next edit (no re-read needed). On failure follow the error hint.";
|
|
14
16
|
export const EDIT_GUIDANCE = {
|
|
15
|
-
intro: "Edit a range of lines via a bare 3-char HASH anchor
|
|
17
|
+
intro: "Edit a range of lines via a bare 3-char HASH anchor \u2014 payload is { path, edits: [[hash,hash,text]] } (single-file atomic, null path infers).",
|
|
16
18
|
lines: [
|
|
17
|
-
"`edit`:
|
|
18
|
-
"`edit`:
|
|
19
|
-
"`edit`:
|
|
20
|
-
"`edit`:
|
|
21
|
-
"`edit`:
|
|
19
|
+
"`edit`: `HASH` vs `HASH\u2502content` \u2014 `HASH` is the bare 3-char (e.g. \"wUp\"), `HASH\u2502content` is the full line from `read`/`diff` (e.g. `wUp\u2502 \"site\": {`); never mix them.",
|
|
20
|
+
"`edit`: get `remove_from`/`remove_to` by copying only the 3 chars before `\u2502` from `read` output \u2014 never include `\u2502` or content after it.",
|
|
21
|
+
"`edit`: `replacement_text` is plain file content without `HASH\u2502` \u2014 e.g. \" \\\"site\\\": {\\n \\\"class\\\": SiteScraper,\" \u2014 never prefix lines with `HASH\u2502`.",
|
|
22
|
+
"`edit`: every `\\n` in `replacement_text` separates lines; mirror trailing blank lines explicitly (use \"\" to delete a range).",
|
|
23
|
+
"`edit`: after a successful edit the returned diff shows fresh anchors (`HASH\u2502content`) \u2014 copy new `HASH` values from there for the next edit; no need to re-read.",
|
|
24
|
+
"`edit`: `remove_from`/`remove_to` are inclusive; batch multiple edits to the same file only when independent \u2014 they apply atomically (fail \u2192 nothing written).",
|
|
22
25
|
],
|
|
23
26
|
};
|
|
24
27
|
export const READ_DESCRIPTION = "Read a text file; each line returned as HASH│content with a 3-char alphanumeric hash. " +
|
package/lib/read-and-serve.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import { abortIf } from "./utils.js";
|
|
11
11
|
import { readView } from "./file-view.js";
|
|
12
|
+
import { getAutoGuessFooter } from "./fs-bridge.js";
|
|
12
13
|
import { recordServed, clearDriftReported } from "./session-view.js";
|
|
13
14
|
/** Appended when the file had non-UTF-8 bytes; editing rewrites it as UTF-8. */
|
|
14
15
|
export const UTF8_REWRITE_NOTE = "[Non-UTF-8 bytes shown as U+FFFD; editing rewrites the file as UTF-8.]";
|
|
@@ -26,6 +27,7 @@ export async function readAndServe(io, rawPath, cwd, options) {
|
|
|
26
27
|
const { sessionKey, signal } = options;
|
|
27
28
|
abortIf(signal);
|
|
28
29
|
const view = await readView(io, rawPath, cwd, {
|
|
30
|
+
encoding: options.encoding,
|
|
29
31
|
offset: options.offset,
|
|
30
32
|
limit: options.limit,
|
|
31
33
|
signal,
|
|
@@ -34,6 +36,8 @@ export async function readAndServe(io, rawPath, cwd, options) {
|
|
|
34
36
|
await recordServed(sessionKey, view.absolutePath, view.served, view.hashes.length);
|
|
35
37
|
}
|
|
36
38
|
await clearDriftReported(sessionKey, view.absolutePath);
|
|
39
|
+
const autoFooter = getAutoGuessFooter(view.absolutePath) ?? getAutoGuessFooter(rawPath) ?? getAutoGuessFooter(view.absolutePath.replace(/\\/g, "/")) ?? "";
|
|
40
|
+
const warning = autoFooter || undefined;
|
|
37
41
|
const text = view.hadUtf8DecodeErrors
|
|
38
42
|
? `${view.text}\n\n${UTF8_REWRITE_NOTE}`
|
|
39
43
|
: view.text;
|
|
@@ -42,5 +46,6 @@ export async function readAndServe(io, rawPath, cwd, options) {
|
|
|
42
46
|
served: view.served,
|
|
43
47
|
hadUtf8DecodeErrors: view.hadUtf8DecodeErrors,
|
|
44
48
|
absolutePath: view.absolutePath,
|
|
49
|
+
warning,
|
|
45
50
|
};
|
|
46
51
|
}
|