dsh-better-edit 0.2.2 → 0.3.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 +33 -0
- package/README.md +317 -240
- package/README.zh.md +19 -11
- package/lib/constants.js +3 -1
- package/lib/contract.js +159 -63
- package/lib/drift.js +5 -98
- package/lib/dsh-context.js +2 -24
- package/lib/edit-engine.js +9 -8
- package/lib/edit-pipeline.js +3 -121
- package/lib/edit-response.js +16 -8
- package/lib/file-kind.js +6 -121
- package/lib/file-reader.js +4 -77
- package/lib/file-view.js +486 -0
- package/lib/guidance/index.js +8 -0
- package/lib/guidance/materialize.js +212 -0
- package/lib/guidance/parse.js +80 -0
- package/lib/guidance/resolve.js +116 -0
- package/lib/guidance.js +6 -388
- package/lib/hash-store.js +6 -4
- package/lib/hashline/alphabet.js +2 -6
- package/lib/hashline/anchor-pipeline.js +786 -0
- package/lib/hashline/apply.js +2 -223
- package/lib/hashline/hash-assign.js +255 -0
- package/lib/hashline/hash.js +67 -29
- package/lib/hashline/hasher.js +2 -23
- package/lib/hashline/index.js +9 -4
- package/lib/hashline/parse.js +2 -35
- package/lib/hashline/pure.js +2 -213
- package/lib/hashline/resolve.js +3 -361
- package/lib/hashline/served.js +2 -120
- package/lib/index.js +1 -3
- package/lib/mutation.js +266 -0
- package/lib/noop-guard.js +20 -0
- package/lib/preview-controller.js +65 -0
- package/lib/prompts.js +14 -20
- package/lib/read-and-serve.js +23 -23
- package/lib/read-render.js +3 -139
- package/lib/served-store.js +2 -216
- package/lib/session-view.js +332 -0
- package/lib/tool-batch-edit.js +9 -10
- package/lib/tool-edit.js +73 -129
- package/lib/tool-read.js +3 -5
- package/lib/tool-undo.js +16 -10
- package/lib/truncate.js +3 -95
- package/lib/types/constants.d.ts +2 -0
- package/lib/types/contract.d.ts +120 -15
- package/lib/types/drift.d.ts +5 -29
- package/lib/types/dsh-context.d.ts +2 -20
- package/lib/types/edit-engine.d.ts +2 -2
- package/lib/types/edit-pipeline.d.ts +3 -44
- package/lib/types/file-kind.d.ts +6 -18
- package/lib/types/file-reader.d.ts +4 -45
- package/lib/types/file-view.d.ts +139 -0
- package/lib/types/guidance/index.d.ts +8 -0
- package/lib/types/guidance/materialize.d.ts +23 -0
- package/lib/types/guidance/parse.d.ts +36 -0
- package/lib/types/guidance/resolve.d.ts +55 -0
- package/lib/types/guidance.d.ts +6 -127
- package/lib/types/hashline/alphabet.d.ts +1 -5
- package/lib/types/hashline/anchor-pipeline.d.ts +127 -0
- package/lib/types/hashline/apply.d.ts +1 -22
- package/lib/types/hashline/hash-assign.d.ts +25 -0
- package/lib/types/hashline/hash.d.ts +8 -9
- package/lib/types/hashline/hasher.d.ts +1 -8
- package/lib/types/hashline/index.d.ts +12 -4
- package/lib/types/hashline/parse.d.ts +1 -7
- package/lib/types/hashline/pure.d.ts +1 -26
- package/lib/types/hashline/resolve.d.ts +1 -58
- package/lib/types/hashline/served.d.ts +1 -43
- package/lib/types/mutation.d.ts +131 -0
- package/lib/types/noop-guard.d.ts +29 -0
- package/lib/types/preview-controller.d.ts +33 -0
- package/lib/types/prompts.d.ts +9 -9
- package/lib/types/read-and-serve.d.ts +6 -5
- package/lib/types/read-render.d.ts +3 -18
- package/lib/types/served-store.d.ts +2 -81
- package/lib/types/session-view.d.ts +92 -0
- package/lib/types/tool-edit.d.ts +3 -14
- package/lib/types/truncate.d.ts +3 -29
- package/lib/types/validation.d.ts +6 -7
- package/lib/types/workspace.d.ts +2 -21
- package/lib/validation.js +6 -33
- package/lib/workspace.js +2 -27
- package/lib/write-hook.js +2 -2
- package/package.json +2 -2
package/lib/hashline/pure.js
CHANGED
|
@@ -1,213 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { HASH_LEN, ALPH, ALPH_RE, HASH_CLASS } from "./alphabet.js";
|
|
4
|
-
export { initHasher, HASH_LEN, ALPH_RE, HASH_CLASS };
|
|
5
|
-
export const ANCHOR_LEN = HASH_LEN;
|
|
6
|
-
export const HASH_SEP = "│";
|
|
7
|
-
export const HASH_SPACE = ALPH.length ** HASH_LEN;
|
|
8
|
-
export const MAX_HASH_LINES = HASH_SPACE;
|
|
9
|
-
export const HASH_PROBE_STRIDE = ALPH.length ** 2 + ALPH.length + 1;
|
|
10
|
-
function idxToHash(idx) {
|
|
11
|
-
let out = "";
|
|
12
|
-
for (let j = 0; j < HASH_LEN; j++) {
|
|
13
|
-
out = ALPH[idx % ALPH.length] + out;
|
|
14
|
-
idx = Math.floor(idx / ALPH.length);
|
|
15
|
-
}
|
|
16
|
-
return out;
|
|
17
|
-
}
|
|
18
|
-
const hashCache = new Map();
|
|
19
|
-
function hashAt(idx) {
|
|
20
|
-
let hash = hashCache.get(idx);
|
|
21
|
-
if (hash === undefined) {
|
|
22
|
-
hash = idxToHash(idx);
|
|
23
|
-
hashCache.set(idx, hash);
|
|
24
|
-
}
|
|
25
|
-
return hash;
|
|
26
|
-
}
|
|
27
|
-
export const HL_PREFIX_PLUS_RE = new RegExp(`^\\+${HASH_CLASS}│`);
|
|
28
|
-
export const HL_PREFIX_MINUS_RE = new RegExp(`^-(?:${HASH_CLASS}│| {${ANCHOR_LEN}}│)`);
|
|
29
|
-
export const HL_BARE_PREFIX_RE = new RegExp(`^\\s*(${HASH_CLASS})│`);
|
|
30
|
-
export function canon(line) {
|
|
31
|
-
return line.replace(/\r/g, "").trimEnd();
|
|
32
|
-
}
|
|
33
|
-
const BITSET_WORDS = Math.ceil(HASH_SPACE / 32);
|
|
34
|
-
function getBit(bits, idx) {
|
|
35
|
-
return (bits[idx >>> 5] >>> (idx & 31) & 1) !== 0;
|
|
36
|
-
}
|
|
37
|
-
function setBit(bits, idx) {
|
|
38
|
-
bits[idx >>> 5] |= 1 << (idx & 31);
|
|
39
|
-
}
|
|
40
|
-
function nextZeroBit(bits, start) {
|
|
41
|
-
const totalBits = HASH_SPACE;
|
|
42
|
-
let idx = start % totalBits;
|
|
43
|
-
for (let i = 0; i < totalBits; i++) {
|
|
44
|
-
if (!getBit(bits, idx))
|
|
45
|
-
return idx;
|
|
46
|
-
idx += HASH_PROBE_STRIDE;
|
|
47
|
-
if (idx >= totalBits)
|
|
48
|
-
idx -= totalBits;
|
|
49
|
-
}
|
|
50
|
-
throw new Error(`[E_FILE_TOO_LARGE] Cannot allocate a unique hash anchor: the file exceeds the ${HASH_SPACE}-line limit for ${HASH_LEN}-char hashline anchors. For very large files use write or a non-line-based approach.`);
|
|
51
|
-
}
|
|
52
|
-
function assignHash(used, baseIdx, hint) {
|
|
53
|
-
if (!getBit(used, baseIdx)) {
|
|
54
|
-
setBit(used, baseIdx);
|
|
55
|
-
hint.value = baseIdx + HASH_PROBE_STRIDE;
|
|
56
|
-
return hashAt(baseIdx);
|
|
57
|
-
}
|
|
58
|
-
const nextIdx = nextZeroBit(used, hint.value);
|
|
59
|
-
setBit(used, nextIdx);
|
|
60
|
-
hint.value = nextIdx + HASH_PROBE_STRIDE;
|
|
61
|
-
return hashAt(nextIdx);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Pure hash-anchor assignment: every line of `content` gets a unique 3-char
|
|
65
|
-
* anchor derived from its canonicalized content via xxhash32 + probe-stride
|
|
66
|
-
* allocation. No I/O, no store — the persistence-aware wrapper
|
|
67
|
-
* {@link lineHashes} (in ./hash.js) composes this with snapshot load/upsert.
|
|
68
|
-
*/
|
|
69
|
-
export function lineHashesPure(content) {
|
|
70
|
-
const lines = splitLines(content);
|
|
71
|
-
const hashes = new Array(lines.length);
|
|
72
|
-
const used = new Uint32Array(BITSET_WORDS);
|
|
73
|
-
const hint = { value: 0 };
|
|
74
|
-
for (let i = 0; i < lines.length; i++) {
|
|
75
|
-
const c = canon(lines[i]);
|
|
76
|
-
const baseIdx = (xxh32(c) >>> 14) % HASH_SPACE;
|
|
77
|
-
hashes[i] = assignHash(used, baseIdx, hint);
|
|
78
|
-
}
|
|
79
|
-
return hashes;
|
|
80
|
-
}
|
|
81
|
-
function hashToIndex(hash) {
|
|
82
|
-
let idx = 0;
|
|
83
|
-
for (let j = 0; j < HASH_LEN; j++) {
|
|
84
|
-
const charIdx = ALPH.indexOf(hash[j]);
|
|
85
|
-
if (charIdx < 0)
|
|
86
|
-
return -1;
|
|
87
|
-
idx = idx * ALPH.length + charIdx;
|
|
88
|
-
}
|
|
89
|
-
return idx;
|
|
90
|
-
}
|
|
91
|
-
function nearestNew(candidates, target) {
|
|
92
|
-
let lo = 0;
|
|
93
|
-
let hi = candidates.length;
|
|
94
|
-
while (lo < hi) {
|
|
95
|
-
const mid = (lo + hi) >>> 1;
|
|
96
|
-
if (candidates[mid] < target)
|
|
97
|
-
lo = mid + 1;
|
|
98
|
-
else
|
|
99
|
-
hi = mid;
|
|
100
|
-
}
|
|
101
|
-
const left = lo - 1;
|
|
102
|
-
const right = lo;
|
|
103
|
-
if (left >= 0 &&
|
|
104
|
-
(right >= candidates.length ||
|
|
105
|
-
target - candidates[left] <= candidates[right] - target)) {
|
|
106
|
-
return left;
|
|
107
|
-
}
|
|
108
|
-
return right < candidates.length ? right : -1;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Pure stable re-hash: given the pre-edit content + anchors and the new
|
|
112
|
-
* content (plus the hashes removed by the edit), map surviving lines to
|
|
113
|
-
* their old anchors, re-claim removed anchors for identical lines, and
|
|
114
|
-
* allocate fresh anchors for the rest. No I/O, no store.
|
|
115
|
-
*/
|
|
116
|
-
export function mapStableHashes(oldContent, oldHashes, newContent, removedHashes) {
|
|
117
|
-
const oldLines = splitLines(oldContent);
|
|
118
|
-
const newLines = splitLines(newContent);
|
|
119
|
-
const newHashes = new Array(newLines.length);
|
|
120
|
-
const used = new Uint32Array(BITSET_WORDS);
|
|
121
|
-
const hint = { value: 0 };
|
|
122
|
-
const removed = removedHashes ?? new Set();
|
|
123
|
-
const oldHashIndex = new Map();
|
|
124
|
-
for (let i = 0; i < oldHashes.length; i++) {
|
|
125
|
-
const hash = oldHashes[i];
|
|
126
|
-
oldHashIndex.set(hash, i);
|
|
127
|
-
const idx = hashToIndex(hash);
|
|
128
|
-
if (idx >= 0)
|
|
129
|
-
setBit(used, idx);
|
|
130
|
-
}
|
|
131
|
-
const removedIndexes = new Set();
|
|
132
|
-
for (const hash of removed) {
|
|
133
|
-
const idx = oldHashIndex.get(hash);
|
|
134
|
-
if (idx !== undefined)
|
|
135
|
-
removedIndexes.add(idx);
|
|
136
|
-
}
|
|
137
|
-
let spanStart = oldLines.length;
|
|
138
|
-
let spanEnd = -1;
|
|
139
|
-
for (const idx of removedIndexes) {
|
|
140
|
-
if (idx < spanStart)
|
|
141
|
-
spanStart = idx;
|
|
142
|
-
if (idx > spanEnd)
|
|
143
|
-
spanEnd = idx;
|
|
144
|
-
}
|
|
145
|
-
const spanLen = spanEnd >= spanStart ? spanEnd - spanStart + 1 : 0;
|
|
146
|
-
const replacementLen = newLines.length - oldLines.length + spanLen;
|
|
147
|
-
const shiftAfterSpan = spanEnd >= spanStart ? replacementLen - spanLen : 0;
|
|
148
|
-
const survivors = [];
|
|
149
|
-
const removedEntries = [];
|
|
150
|
-
for (let i = 0; i < oldLines.length; i++) {
|
|
151
|
-
const entry = { index: i, hash: oldHashes[i] };
|
|
152
|
-
if (removedIndexes.has(i))
|
|
153
|
-
removedEntries.push(entry);
|
|
154
|
-
else
|
|
155
|
-
survivors.push(entry);
|
|
156
|
-
}
|
|
157
|
-
const newByContent = new Map();
|
|
158
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
159
|
-
const key = canon(newLines[i]);
|
|
160
|
-
const list = newByContent.get(key);
|
|
161
|
-
if (list)
|
|
162
|
-
list.push(i);
|
|
163
|
-
else
|
|
164
|
-
newByContent.set(key, [i]);
|
|
165
|
-
}
|
|
166
|
-
const markUsed = (hash) => {
|
|
167
|
-
const idx = hashToIndex(hash);
|
|
168
|
-
if (idx >= 0) {
|
|
169
|
-
setBit(used, idx);
|
|
170
|
-
if (idx + HASH_PROBE_STRIDE > hint.value)
|
|
171
|
-
hint.value = idx + HASH_PROBE_STRIDE;
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
for (const entry of survivors) {
|
|
175
|
-
const candidates = newByContent.get(canon(oldLines[entry.index]));
|
|
176
|
-
if (!candidates || candidates.length === 0)
|
|
177
|
-
continue;
|
|
178
|
-
const target = entry.index > spanEnd ? entry.index + shiftAfterSpan : entry.index;
|
|
179
|
-
const pos = nearestNew(candidates, target);
|
|
180
|
-
if (pos < 0)
|
|
181
|
-
continue;
|
|
182
|
-
const newIdx = candidates.splice(pos, 1)[0];
|
|
183
|
-
newHashes[newIdx] = entry.hash;
|
|
184
|
-
markUsed(entry.hash);
|
|
185
|
-
}
|
|
186
|
-
const removedByContent = new Map();
|
|
187
|
-
for (const entry of removedEntries) {
|
|
188
|
-
const key = canon(oldLines[entry.index]);
|
|
189
|
-
let queue = removedByContent.get(key);
|
|
190
|
-
if (!queue) {
|
|
191
|
-
queue = { hashes: [], pos: 0 };
|
|
192
|
-
removedByContent.set(key, queue);
|
|
193
|
-
}
|
|
194
|
-
queue.hashes.push(entry.hash);
|
|
195
|
-
}
|
|
196
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
197
|
-
if (newHashes[i])
|
|
198
|
-
continue;
|
|
199
|
-
const queue = removedByContent.get(canon(newLines[i]));
|
|
200
|
-
if (!queue || queue.pos >= queue.hashes.length)
|
|
201
|
-
continue;
|
|
202
|
-
newHashes[i] = queue.hashes[queue.pos];
|
|
203
|
-
queue.pos += 1;
|
|
204
|
-
}
|
|
205
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
206
|
-
if (newHashes[i])
|
|
207
|
-
continue;
|
|
208
|
-
const c = canon(newLines[i]);
|
|
209
|
-
const baseIdx = (xxh32(c) >>> 14) % HASH_SPACE;
|
|
210
|
-
newHashes[i] = assignHash(used, baseIdx, hint);
|
|
211
|
-
}
|
|
212
|
-
return newHashes;
|
|
213
|
-
}
|
|
1
|
+
// @deprecated — private to HashAssign seam. Use `from "./hash-assign.js"` instead.
|
|
2
|
+
export * from "./hash-assign.js";
|
package/lib/hashline/resolve.js
CHANGED
|
@@ -1,361 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { NEW_CONTENT_NOT_STRING_MSG } from "../constants.js";
|
|
5
|
-
function resAnchorFromMap(ref, hashIndex) {
|
|
6
|
-
const hashMatches = hashIndex.get(ref.hash);
|
|
7
|
-
if (!hashMatches || hashMatches.length === 0) {
|
|
8
|
-
return { ref, kind: "not_found" };
|
|
9
|
-
}
|
|
10
|
-
if (hashMatches.length === 1) {
|
|
11
|
-
return {
|
|
12
|
-
line: hashMatches[0],
|
|
13
|
-
hash: ref.hash,
|
|
14
|
-
hashMatched: true,
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
return { ref, kind: "ambiguous", candidates: hashMatches };
|
|
18
|
-
}
|
|
19
|
-
function assertAligned(fileLines, fileHashes, ctx) {
|
|
20
|
-
if (fileHashes.length !== fileLines.length) {
|
|
21
|
-
throw new Error(`${ctx}: fileHashes.length (${fileHashes.length}) must match fileLines.length (${fileLines.length}).`);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
export function fmtMismatchWithServes(mismatches, fileLines, fileHashes, filePath) {
|
|
25
|
-
assertAligned(fileLines, fileHashes, "fmtMismatch");
|
|
26
|
-
const out = [];
|
|
27
|
-
const servedRows = [];
|
|
28
|
-
const seen = new Set();
|
|
29
|
-
const pushRow = (ln) => {
|
|
30
|
-
if (ln < 1 || ln > fileLines.length)
|
|
31
|
-
return;
|
|
32
|
-
const position = ln - 1;
|
|
33
|
-
if (seen.has(position))
|
|
34
|
-
return;
|
|
35
|
-
seen.add(position);
|
|
36
|
-
servedRows.push({ position, hash: fileHashes[ln - 1] });
|
|
37
|
-
};
|
|
38
|
-
const notFound = mismatches.filter((m) => m.kind === "not_found");
|
|
39
|
-
const ambiguous = mismatches.filter((m) => m.kind === "ambiguous");
|
|
40
|
-
const refList = notFound.map((m) => `"${m.ref.hash}"`).join(", ");
|
|
41
|
-
if (notFound.length > 0) {
|
|
42
|
-
out.push(`[E_STALE_ANCHOR] ${notFound.length} stale anchor${notFound.length > 1 ? "s" : ""}${filePath ? ` in ${filePath}` : ""}: ${refList}. The file content has changed since those anchors were read. Call read() to get fresh anchors, then copy the 3-char HASH of the start and end of the range you are editing into remove_from and remove_to of your next edit call.`);
|
|
43
|
-
for (const m of notFound) {
|
|
44
|
-
const ctx = m.context;
|
|
45
|
-
if (!ctx)
|
|
46
|
-
continue;
|
|
47
|
-
const from = Math.max(1, ctx.line - 1);
|
|
48
|
-
const to = Math.min(fileLines.length, ctx.line + 1);
|
|
49
|
-
const rows = [];
|
|
50
|
-
for (let ln = from; ln <= to; ln++) {
|
|
51
|
-
rows.push(` ${ln}: ${fileHashes[ln - 1]}│${clipLine(fileLines[ln - 1] ?? "")}`);
|
|
52
|
-
pushRow(ln);
|
|
53
|
-
}
|
|
54
|
-
out.push("");
|
|
55
|
-
out.push(` Current context around resolved anchor "${ctx.hash}" (line ${ctx.line}):\n${rows.join("\n")}`);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (ambiguous.length > 0) {
|
|
59
|
-
if (out.length > 0)
|
|
60
|
-
out.push("");
|
|
61
|
-
out.push(`[E_AMBIGUOUS_ANCHOR] ${ambiguous.length} ambiguous anchor${ambiguous.length > 1 ? "s" : ""}${filePath ? ` in ${filePath}` : ""}. Call read() to get fresh anchors, then copy the 3-char HASH of the start and end of the range you are editing into remove_from and remove_to of your next edit call.`);
|
|
62
|
-
for (const m of ambiguous) {
|
|
63
|
-
const sample = (m.candidates ?? []).slice(0, 5);
|
|
64
|
-
const more = (m.candidates?.length ?? 0) > sample.length
|
|
65
|
-
? `, ... (+${(m.candidates?.length ?? 0) - sample.length} more)`
|
|
66
|
-
: "";
|
|
67
|
-
const lines = sample
|
|
68
|
-
.map((line) => {
|
|
69
|
-
const content = clipLine(fileLines[line - 1] ?? "");
|
|
70
|
-
pushRow(line);
|
|
71
|
-
return ` ${line}: ${fileHashes[line - 1]}│${content}`;
|
|
72
|
-
})
|
|
73
|
-
.join("\n");
|
|
74
|
-
out.push(` Hash "${m.ref.hash}" matches lines ${sample.join(", ")}${more}.\n${lines}`);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return { message: out.join("\n"), servedRows };
|
|
78
|
-
}
|
|
79
|
-
const ITEM_KS = new Set(["replacement_text", "remove_from", "remove_to"]);
|
|
80
|
-
function assertItem(edit) {
|
|
81
|
-
rejectUnknownFields(edit, ITEM_KS, "Edit", "The edit takes only { replacement_text, remove_from, remove_to }.");
|
|
82
|
-
if ("remove_from" in edit && typeof edit.remove_from !== "string") {
|
|
83
|
-
throw new Error(`[E_BAD_SHAPE] Field "remove_from" must be an anchor string (3-char hash).`);
|
|
84
|
-
}
|
|
85
|
-
if ("remove_to" in edit && typeof edit.remove_to !== "string") {
|
|
86
|
-
throw new Error(`[E_BAD_SHAPE] Field "remove_to" must be an anchor string (3-char hash).`);
|
|
87
|
-
}
|
|
88
|
-
if (!("replacement_text" in edit)) {
|
|
89
|
-
throw new Error(`[E_BAD_SHAPE] The edit requires a "replacement_text" field. Provide the replacement text (use "" to delete).`);
|
|
90
|
-
}
|
|
91
|
-
if (typeof edit.replacement_text !== "string") {
|
|
92
|
-
throw new Error(NEW_CONTENT_NOT_STRING_MSG);
|
|
93
|
-
}
|
|
94
|
-
if (typeof edit.remove_from !== "string" ||
|
|
95
|
-
typeof edit.remove_to !== "string") {
|
|
96
|
-
throw new Error(`[E_BAD_SHAPE] The edit requires "remove_from" and "remove_to" anchor strings (3-char hashes from read output).`);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const ANCHOR_ROW_RE = new RegExp(`^([+-]?)(${HASH_CLASS})│`);
|
|
100
|
-
export function resEdit(edit, warnings) {
|
|
101
|
-
assertItem(edit);
|
|
102
|
-
const editLines = parseText(edit.replacement_text);
|
|
103
|
-
const bounds = [edit.remove_from, edit.remove_to].map((ref) => {
|
|
104
|
-
const trimmed = ref.trim();
|
|
105
|
-
const match = trimmed.match(ANCHOR_ROW_RE);
|
|
106
|
-
if (match) {
|
|
107
|
-
let message;
|
|
108
|
-
if (match[1] === "+") {
|
|
109
|
-
message = `[E_BAD_REF] Autocorrected: stripped diff-preview marker copied from the diff preview in remove_from/remove_to entry "${trimmed}".`;
|
|
110
|
-
}
|
|
111
|
-
else if (match[1] === "-") {
|
|
112
|
-
message = `[E_BAD_REF] Autocorrected: stripped leading "-" marker in remove_from/remove_to entry "${trimmed}".`;
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
message = `[E_BAD_REF] Autocorrected: stripped "HASH│" prefix copied from read output in remove_from/remove_to entry "${trimmed}".`;
|
|
116
|
-
}
|
|
117
|
-
warnings?.push(message);
|
|
118
|
-
return match[2];
|
|
119
|
-
}
|
|
120
|
-
return ref;
|
|
121
|
-
});
|
|
122
|
-
return {
|
|
123
|
-
content_lines: editLines,
|
|
124
|
-
hash_bounds: [parseHashRef(bounds[0]), parseHashRef(bounds[1])],
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
function warnUnicodeEsc(edit, warnings) {
|
|
128
|
-
if (edit.content_lines.some((line) => /\\uDDDD/i.test(line))) {
|
|
129
|
-
warnings.push("Detected literal \\uDDDD in edit content; no autocorrection applied. Verify whether this should be a real Unicode escape or plain text.");
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
export function stripBarePrefixes(edit, fileHashes, warnings) {
|
|
133
|
-
const fileHashSet = new Set(fileHashes);
|
|
134
|
-
const stripped = [];
|
|
135
|
-
const contentLines = edit.content_lines.map((line, lineIndex) => {
|
|
136
|
-
const match = line.match(HL_BARE_PREFIX_RE);
|
|
137
|
-
if (!match)
|
|
138
|
-
return line;
|
|
139
|
-
stripped.push({ lineIndex, matched: fileHashSet.has(match[1]) });
|
|
140
|
-
return line.slice(match[0].length);
|
|
141
|
-
});
|
|
142
|
-
if (stripped.length === 0)
|
|
143
|
-
return edit;
|
|
144
|
-
const locations = stripped
|
|
145
|
-
.map((s) => `replacement_text line ${s.lineIndex + 1}`)
|
|
146
|
-
.join(", ");
|
|
147
|
-
const matchedCount = stripped.filter((s) => s.matched).length;
|
|
148
|
-
const evidence = matchedCount === 0
|
|
149
|
-
? "none of the stripped hashes match current file lines"
|
|
150
|
-
: `${matchedCount} of ${stripped.length} stripped hash(es) match current file lines`;
|
|
151
|
-
const guidance = matchedCount === 0
|
|
152
|
-
? " Verify that these lines were pasted from read output; literal content starting with 'HASH│' would be altered by this strip."
|
|
153
|
-
: "";
|
|
154
|
-
warnings.push(`[E_BARE_HASH_PREFIX] Autocorrected: stripped "HASH│" prefix copied from read output in ${locations} (${evidence}).${guidance}`);
|
|
155
|
-
return { ...edit, content_lines: contentLines };
|
|
156
|
-
}
|
|
157
|
-
export function stripDiffPrefixes(edit, warnings) {
|
|
158
|
-
const stripped = [];
|
|
159
|
-
const contentLines = edit.content_lines.map((line, lineIndex) => {
|
|
160
|
-
const plus = line.match(HL_PREFIX_PLUS_RE);
|
|
161
|
-
if (plus) {
|
|
162
|
-
stripped.push(lineIndex);
|
|
163
|
-
return line.slice(plus[0].length);
|
|
164
|
-
}
|
|
165
|
-
const minus = line.match(HL_PREFIX_MINUS_RE);
|
|
166
|
-
if (minus) {
|
|
167
|
-
stripped.push(lineIndex);
|
|
168
|
-
return line.slice(minus[0].length);
|
|
169
|
-
}
|
|
170
|
-
return line;
|
|
171
|
-
});
|
|
172
|
-
if (stripped.length === 0)
|
|
173
|
-
return edit;
|
|
174
|
-
const locations = stripped
|
|
175
|
-
.map((i) => `replacement_text line ${i + 1}`)
|
|
176
|
-
.join(", ");
|
|
177
|
-
warnings.push(`[E_INVALID_PATCH] Autocorrected: stripped diff-preview marker copied from the diff preview in ${locations}.`);
|
|
178
|
-
return { ...edit, content_lines: contentLines };
|
|
179
|
-
}
|
|
180
|
-
export function swapReversedRanges(edit, fileHashes, warnings) {
|
|
181
|
-
const lineByHash = new Map();
|
|
182
|
-
for (let i = 0; i < fileHashes.length; i++) {
|
|
183
|
-
lineByHash.set(fileHashes[i], i + 1);
|
|
184
|
-
}
|
|
185
|
-
const [startRef, endRef] = edit.hash_bounds;
|
|
186
|
-
const startLine = lineByHash.get(startRef.hash);
|
|
187
|
-
const endLine = lineByHash.get(endRef.hash);
|
|
188
|
-
if (startLine === undefined ||
|
|
189
|
-
endLine === undefined ||
|
|
190
|
-
startLine <= endLine) {
|
|
191
|
-
return edit;
|
|
192
|
-
}
|
|
193
|
-
warnings.push(`[E_BAD_OP] Autocorrected: remove_from and remove_to were reversed (remove_from ${startRef.hash} is after remove_to ${endRef.hash}); swapped the pair.`);
|
|
194
|
-
return { ...edit, hash_bounds: [endRef, startRef] };
|
|
195
|
-
}
|
|
196
|
-
function trailingDups(contentLines, fileLines, endLine) {
|
|
197
|
-
const start = lastNonEmptyIndex(contentLines);
|
|
198
|
-
if (start < 0)
|
|
199
|
-
return [];
|
|
200
|
-
const dups = [];
|
|
201
|
-
const maxK = Math.min(start + 1, fileLines.length - endLine);
|
|
202
|
-
for (let k = 0; k < maxK; k++) {
|
|
203
|
-
if (contentLines[start - k] !== fileLines[endLine + k])
|
|
204
|
-
break;
|
|
205
|
-
dups.push({ kind: "trailing", replacementLineIndex: start - k });
|
|
206
|
-
}
|
|
207
|
-
return dups;
|
|
208
|
-
}
|
|
209
|
-
function leadingDups(contentLines, fileLines, startLine) {
|
|
210
|
-
const start = firstNonEmptyIndex(contentLines);
|
|
211
|
-
if (start < 0)
|
|
212
|
-
return [];
|
|
213
|
-
const dups = [];
|
|
214
|
-
const maxK = Math.min(contentLines.length - start, startLine - 1);
|
|
215
|
-
for (let k = 0; k < maxK; k++) {
|
|
216
|
-
if (contentLines[start + k] !== fileLines[startLine - 2 - k])
|
|
217
|
-
break;
|
|
218
|
-
dups.push({ kind: "leading", replacementLineIndex: start + k });
|
|
219
|
-
}
|
|
220
|
-
return dups;
|
|
221
|
-
}
|
|
222
|
-
function sectionIsUnique(canonLines, start, length) {
|
|
223
|
-
let count = 0;
|
|
224
|
-
for (let i = 0; i + length <= canonLines.length; i++) {
|
|
225
|
-
let k = 0;
|
|
226
|
-
while (k < length && canonLines[i + k] === canonLines[start + k])
|
|
227
|
-
k++;
|
|
228
|
-
if (k < length)
|
|
229
|
-
continue;
|
|
230
|
-
count++;
|
|
231
|
-
if (count > 1)
|
|
232
|
-
return false;
|
|
233
|
-
}
|
|
234
|
-
return true;
|
|
235
|
-
}
|
|
236
|
-
function firstNewAfterDups(contentLines, rangeLines, canonLines, endLine) {
|
|
237
|
-
const firstNew = findNewEdge(contentLines, rangeLines, false);
|
|
238
|
-
if (!firstNew)
|
|
239
|
-
return [];
|
|
240
|
-
const maxK = Math.min(contentLines.length - firstNew.index, canonLines.length - endLine);
|
|
241
|
-
let runLen = 0;
|
|
242
|
-
while (runLen < maxK &&
|
|
243
|
-
canon(contentLines[firstNew.index + runLen]) ===
|
|
244
|
-
canonLines[endLine + runLen]) {
|
|
245
|
-
runLen++;
|
|
246
|
-
}
|
|
247
|
-
if (runLen === 0 || !sectionIsUnique(canonLines, endLine, runLen))
|
|
248
|
-
return [];
|
|
249
|
-
const dups = [];
|
|
250
|
-
for (let k = 0; k < runLen; k++) {
|
|
251
|
-
dups.push({
|
|
252
|
-
kind: "first-new-after",
|
|
253
|
-
replacementLineIndex: firstNew.index + k,
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
return dups;
|
|
257
|
-
}
|
|
258
|
-
function lastNewBeforeDups(contentLines, rangeLines, canonLines, startLine) {
|
|
259
|
-
const lastNew = findNewEdge(contentLines, rangeLines, true);
|
|
260
|
-
if (!lastNew)
|
|
261
|
-
return [];
|
|
262
|
-
const maxK = Math.min(lastNew.index + 1, startLine - 1);
|
|
263
|
-
let runLen = 0;
|
|
264
|
-
while (runLen < maxK &&
|
|
265
|
-
canon(contentLines[lastNew.index - runLen]) ===
|
|
266
|
-
canonLines[startLine - 2 - runLen]) {
|
|
267
|
-
runLen++;
|
|
268
|
-
}
|
|
269
|
-
if (runLen === 0)
|
|
270
|
-
return [];
|
|
271
|
-
const sectionStart = startLine - 1 - runLen;
|
|
272
|
-
if (!sectionIsUnique(canonLines, sectionStart, runLen))
|
|
273
|
-
return [];
|
|
274
|
-
const dups = [];
|
|
275
|
-
for (let k = 0; k < runLen; k++) {
|
|
276
|
-
dups.push({
|
|
277
|
-
kind: "last-new-before",
|
|
278
|
-
replacementLineIndex: lastNew.index - k,
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
return dups;
|
|
282
|
-
}
|
|
283
|
-
function canonCounts(lines) {
|
|
284
|
-
const counts = new Map();
|
|
285
|
-
for (const line of lines) {
|
|
286
|
-
const key = canon(line);
|
|
287
|
-
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
288
|
-
}
|
|
289
|
-
return counts;
|
|
290
|
-
}
|
|
291
|
-
export function findNewEdge(contentLines, rangeLines, fromEnd) {
|
|
292
|
-
const multiset = canonCounts(rangeLines);
|
|
293
|
-
const step = fromEnd ? -1 : 1;
|
|
294
|
-
const start = fromEnd ? contentLines.length - 1 : 0;
|
|
295
|
-
for (let i = start; i >= 0 && i < contentLines.length; i += step) {
|
|
296
|
-
const line = contentLines[i];
|
|
297
|
-
if (line.length === 0)
|
|
298
|
-
continue;
|
|
299
|
-
const key = canon(line);
|
|
300
|
-
const count = multiset.get(key) ?? 0;
|
|
301
|
-
if (count > 0) {
|
|
302
|
-
multiset.set(key, count - 1);
|
|
303
|
-
}
|
|
304
|
-
else {
|
|
305
|
-
return { index: i, line };
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
return undefined;
|
|
309
|
-
}
|
|
310
|
-
export function valEdit(edit, fileLines, fileHashes, warnings, signal) {
|
|
311
|
-
assertAligned(fileLines, fileHashes, "valEdit");
|
|
312
|
-
const mismatches = [];
|
|
313
|
-
const boundaryDups = [];
|
|
314
|
-
const hashIndex = new Map();
|
|
315
|
-
for (let i = 0; i < fileHashes.length; i++) {
|
|
316
|
-
const h = fileHashes[i];
|
|
317
|
-
const list = hashIndex.get(h) ?? [];
|
|
318
|
-
list.push(i + 1);
|
|
319
|
-
hashIndex.set(h, list);
|
|
320
|
-
}
|
|
321
|
-
const tryResolve = (ref) => {
|
|
322
|
-
const result = resAnchorFromMap(ref, hashIndex);
|
|
323
|
-
if ("kind" in result) {
|
|
324
|
-
mismatches.push(result);
|
|
325
|
-
return undefined;
|
|
326
|
-
}
|
|
327
|
-
return result;
|
|
328
|
-
};
|
|
329
|
-
abortIf(signal);
|
|
330
|
-
const startResolved = tryResolve(edit.hash_bounds[0]);
|
|
331
|
-
const endResolved = tryResolve(edit.hash_bounds[1]);
|
|
332
|
-
if (!startResolved || !endResolved) {
|
|
333
|
-
if (!startResolved && endResolved) {
|
|
334
|
-
const startMismatch = mismatches.findLast((m) => m.ref === edit.hash_bounds[0]);
|
|
335
|
-
if (startMismatch && startMismatch.kind === "not_found")
|
|
336
|
-
startMismatch.context = endResolved;
|
|
337
|
-
}
|
|
338
|
-
else if (startResolved && !endResolved) {
|
|
339
|
-
const endMismatch = mismatches.findLast((m) => m.ref === edit.hash_bounds[1]);
|
|
340
|
-
if (endMismatch && endMismatch.kind === "not_found")
|
|
341
|
-
endMismatch.context = startResolved;
|
|
342
|
-
}
|
|
343
|
-
return { resolved: undefined, mismatches, boundaryDups };
|
|
344
|
-
}
|
|
345
|
-
if (startResolved.line > endResolved.line) {
|
|
346
|
-
throw new Error(`[E_BAD_OP] Range start line ${startResolved.line} must be <= end line ${endResolved.line} (anchors ${edit.hash_bounds[0].hash} and ${edit.hash_bounds[1].hash}).`);
|
|
347
|
-
}
|
|
348
|
-
const endLine = endResolved.line;
|
|
349
|
-
const rangeLines = fileLines.slice(startResolved.line - 1, endLine);
|
|
350
|
-
const canonLines = fileLines.map((line) => canon(line));
|
|
351
|
-
boundaryDups.push(...trailingDups(edit.content_lines, fileLines, endLine), ...leadingDups(edit.content_lines, fileLines, startResolved.line), ...firstNewAfterDups(edit.content_lines, rangeLines, canonLines, endLine), ...lastNewBeforeDups(edit.content_lines, rangeLines, canonLines, startResolved.line));
|
|
352
|
-
return {
|
|
353
|
-
resolved: {
|
|
354
|
-
content_lines: edit.content_lines,
|
|
355
|
-
hash_bounds: [startResolved, endResolved],
|
|
356
|
-
},
|
|
357
|
-
mismatches,
|
|
358
|
-
boundaryDups,
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
export { warnUnicodeEsc };
|
|
1
|
+
// @deprecated — private to AnchorPipeline seam. Use `from "./anchor-pipeline.js"` instead.
|
|
2
|
+
// stripBarePrefixes, stripDiffPrefixes, swapReversedRanges, valEdit, findNewEdge are @internal to AnchorPipeline.
|
|
3
|
+
export * from "./anchor-pipeline.js";
|