dsh-better-edit 0.3.0 → 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 +21 -0
- package/README.md +9 -5
- package/README.zh.md +1 -2
- package/lib/constants.js +3 -1
- package/lib/contract.js +133 -76
- package/lib/drift.js +5 -98
- package/lib/dsh-context.js +2 -24
- package/lib/edit-engine.js +6 -5
- 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 +446 -23
- package/lib/guidance/materialize.js +25 -4
- package/lib/guidance/resolve.js +3 -9
- package/lib/hash-store.js +6 -3
- package/lib/hashline/alphabet.js +2 -6
- package/lib/hashline/anchor-pipeline.js +770 -7
- package/lib/hashline/apply.js +2 -223
- package/lib/hashline/hash-assign.js +248 -13
- package/lib/hashline/hash.js +67 -29
- package/lib/hashline/hasher.js +2 -23
- package/lib/hashline/index.js +7 -1
- package/lib/hashline/parse.js +2 -35
- package/lib/hashline/pure.js +2 -213
- package/lib/hashline/resolve.js +3 -366
- package/lib/hashline/served.js +2 -120
- package/lib/index.js +0 -2
- package/lib/mutation.js +218 -10
- package/lib/noop-guard.js +20 -0
- package/lib/preview-controller.js +65 -0
- package/lib/prompts.js +14 -20
- package/lib/read-render.js +3 -139
- package/lib/served-store.js +2 -216
- package/lib/session-view.js +309 -11
- package/lib/tool-edit.js +71 -125
- package/lib/tool-undo.js +11 -5
- package/lib/truncate.js +3 -95
- package/lib/types/constants.d.ts +2 -0
- package/lib/types/contract.d.ts +102 -28
- package/lib/types/drift.d.ts +5 -29
- package/lib/types/dsh-context.d.ts +2 -20
- 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 +99 -34
- package/lib/types/guidance/materialize.d.ts +3 -3
- package/lib/types/guidance/resolve.d.ts +2 -2
- package/lib/types/hashline/alphabet.d.ts +1 -5
- package/lib/types/hashline/anchor-pipeline.d.ts +111 -10
- package/lib/types/hashline/apply.d.ts +1 -22
- package/lib/types/hashline/hash-assign.d.ts +25 -19
- 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 +7 -1
- 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 -63
- package/lib/types/hashline/served.d.ts +1 -43
- package/lib/types/mutation.d.ts +63 -10
- 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-render.d.ts +3 -18
- package/lib/types/served-store.d.ts +2 -81
- package/lib/types/session-view.d.ts +68 -9
- 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/package.json +2 -2
package/lib/hashline/apply.js
CHANGED
|
@@ -1,223 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { AnchorMismatchError, verifyServedRange, } from "./served.js";
|
|
4
|
-
import { valEdit, stripBarePrefixes, stripDiffPrefixes, swapReversedRanges, warnUnicodeEsc, fmtMismatchWithServes, } from "./resolve.js";
|
|
5
|
-
export function buildIdx(content) {
|
|
6
|
-
const fileLines = splitLines(content);
|
|
7
|
-
const lineStarts = [];
|
|
8
|
-
let offset = 0;
|
|
9
|
-
for (let index = 0; index < fileLines.length; index++) {
|
|
10
|
-
lineStarts.push(offset);
|
|
11
|
-
offset += fileLines[index].length;
|
|
12
|
-
if (index < fileLines.length - 1) {
|
|
13
|
-
offset += 1;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return {
|
|
17
|
-
fileLines,
|
|
18
|
-
lineStarts,
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
function assertNotEmpty(originalContent, result) {
|
|
22
|
-
if (originalContent.length > 0 && result.length === 0) {
|
|
23
|
-
throw new Error("[E_WOULD_EMPTY] Cannot empty a non-empty file via edit. Use `write` if you need to clear the file.");
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function resToSpan(edit, content, lineIndex) {
|
|
27
|
-
const { fileLines, lineStarts } = lineIndex;
|
|
28
|
-
const startLine = edit.hash_bounds[0].line;
|
|
29
|
-
const endLine = edit.hash_bounds[1].line;
|
|
30
|
-
const originalLines = fileLines.slice(startLine - 1, endLine);
|
|
31
|
-
if (originalLines.length === edit.content_lines.length &&
|
|
32
|
-
originalLines.every((line, lineIndex) => line === edit.content_lines[lineIndex])) {
|
|
33
|
-
return {
|
|
34
|
-
kind: "noop",
|
|
35
|
-
loc: edit.hash_bounds[0].hash,
|
|
36
|
-
currentContent: originalLines.join("\n"),
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
if (edit.content_lines.length > 0) {
|
|
40
|
-
return {
|
|
41
|
-
kind: "replace",
|
|
42
|
-
start: lineStarts[startLine - 1],
|
|
43
|
-
end: lineStarts[endLine - 1] + fileLines[endLine - 1].length,
|
|
44
|
-
replacement: edit.content_lines.join("\n"),
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
if (startLine === 1 && endLine === fileLines.length) {
|
|
48
|
-
return {
|
|
49
|
-
kind: "replace",
|
|
50
|
-
start: 0,
|
|
51
|
-
end: content.length,
|
|
52
|
-
replacement: "",
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
if (endLine < fileLines.length) {
|
|
56
|
-
return {
|
|
57
|
-
kind: "replace",
|
|
58
|
-
start: lineStarts[startLine - 1],
|
|
59
|
-
end: lineStarts[endLine],
|
|
60
|
-
replacement: "",
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
if (content.endsWith("\n")) {
|
|
64
|
-
return {
|
|
65
|
-
kind: "replace",
|
|
66
|
-
start: lineStarts[startLine - 1],
|
|
67
|
-
end: content.length,
|
|
68
|
-
replacement: "",
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
const prevLine = startLine >= 2 ? fileLines[startLine - 2] : undefined;
|
|
72
|
-
return {
|
|
73
|
-
kind: "replace",
|
|
74
|
-
start: prevLine !== undefined && prevLine.length === 0
|
|
75
|
-
? lineStarts[startLine - 1]
|
|
76
|
-
: Math.max(0, lineStarts[startLine - 1] - 1),
|
|
77
|
-
end: content.length,
|
|
78
|
-
replacement: "",
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
function assemble(content, span, signal) {
|
|
82
|
-
abortIf(signal);
|
|
83
|
-
return (content.slice(0, span.start) + span.replacement + content.slice(span.end));
|
|
84
|
-
}
|
|
85
|
-
export function applyEdit(content, edit, signal, precomputedHashes, filePath, served) {
|
|
86
|
-
abortIf(signal);
|
|
87
|
-
const lineIndex = buildIdx(content);
|
|
88
|
-
const fileHashes = precomputedHashes ?? lineHashesPure(content);
|
|
89
|
-
const warnings = [];
|
|
90
|
-
const rangeFixed = swapReversedRanges(edit, fileHashes, warnings);
|
|
91
|
-
const prefixFixed = stripDiffPrefixes(stripBarePrefixes(rangeFixed, fileHashes, warnings), warnings);
|
|
92
|
-
const { resolved: initialResolved, mismatches, boundaryDups, } = valEdit(prefixFixed, lineIndex.fileLines, fileHashes, warnings, signal);
|
|
93
|
-
if (mismatches.length || !initialResolved) {
|
|
94
|
-
const { message, servedRows } = fmtMismatchWithServes(mismatches, lineIndex.fileLines, fileHashes, filePath);
|
|
95
|
-
throw new AnchorMismatchError(message, servedRows);
|
|
96
|
-
}
|
|
97
|
-
warnUnicodeEsc(prefixFixed, warnings);
|
|
98
|
-
let resolved = initialResolved;
|
|
99
|
-
let autoFixes;
|
|
100
|
-
if (boundaryDups.length > 0) {
|
|
101
|
-
autoFixes = [];
|
|
102
|
-
const correctedEdit = {
|
|
103
|
-
...prefixFixed,
|
|
104
|
-
content_lines: [...prefixFixed.content_lines],
|
|
105
|
-
};
|
|
106
|
-
const seen = new Set();
|
|
107
|
-
const uniqueDups = [];
|
|
108
|
-
for (const dup of boundaryDups) {
|
|
109
|
-
if (seen.has(dup.replacementLineIndex))
|
|
110
|
-
continue;
|
|
111
|
-
seen.add(dup.replacementLineIndex);
|
|
112
|
-
uniqueDups.push(dup);
|
|
113
|
-
}
|
|
114
|
-
const dupsByIndex = uniqueDups.sort((a, b) => b.replacementLineIndex - a.replacementLineIndex);
|
|
115
|
-
for (const dup of dupsByIndex) {
|
|
116
|
-
const idx = dup.replacementLineIndex;
|
|
117
|
-
if (idx < 0 || idx >= correctedEdit.content_lines.length)
|
|
118
|
-
continue;
|
|
119
|
-
const removed = correctedEdit.content_lines.splice(idx, 1)[0];
|
|
120
|
-
autoFixes.push({
|
|
121
|
-
kind: dup.kind,
|
|
122
|
-
removedLine: removed,
|
|
123
|
-
removedLineIndex: idx,
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
const correctedResult = valEdit(correctedEdit, lineIndex.fileLines, fileHashes, warnings, signal);
|
|
127
|
-
if (correctedResult.mismatches.length || !correctedResult.resolved) {
|
|
128
|
-
const { message, servedRows } = fmtMismatchWithServes(correctedResult.mismatches, lineIndex.fileLines, fileHashes, filePath);
|
|
129
|
-
throw new AnchorMismatchError(message, servedRows);
|
|
130
|
-
}
|
|
131
|
-
resolved = correctedResult.resolved;
|
|
132
|
-
}
|
|
133
|
-
if (served) {
|
|
134
|
-
const startAnchor = resolved.hash_bounds[0];
|
|
135
|
-
const endAnchor = resolved.hash_bounds[1];
|
|
136
|
-
verifyServedRange({
|
|
137
|
-
served,
|
|
138
|
-
startHash: startAnchor.hash,
|
|
139
|
-
endHash: endAnchor.hash,
|
|
140
|
-
startLine: startAnchor.line,
|
|
141
|
-
endLine: endAnchor.line,
|
|
142
|
-
fileHashes,
|
|
143
|
-
fileLines: lineIndex.fileLines,
|
|
144
|
-
filePath,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
const spanResult = resToSpan(resolved, content, lineIndex);
|
|
148
|
-
if (spanResult.kind === "noop") {
|
|
149
|
-
return {
|
|
150
|
-
content,
|
|
151
|
-
firstChangedLine: undefined,
|
|
152
|
-
lastChangedLine: undefined,
|
|
153
|
-
range: resolvedRange(resolved),
|
|
154
|
-
...(warnings.length ? { warnings } : {}),
|
|
155
|
-
noopEdit: {
|
|
156
|
-
loc: spanResult.loc,
|
|
157
|
-
currentContent: spanResult.currentContent,
|
|
158
|
-
},
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
const result = assemble(content, spanResult, signal);
|
|
162
|
-
assertNotEmpty(content, result);
|
|
163
|
-
const changed = changedRange(content, result);
|
|
164
|
-
return {
|
|
165
|
-
content: result,
|
|
166
|
-
firstChangedLine: changed?.firstChangedLine,
|
|
167
|
-
lastChangedLine: changed?.lastChangedLine,
|
|
168
|
-
range: resolvedRange(resolved),
|
|
169
|
-
...(warnings.length ? { warnings } : {}),
|
|
170
|
-
...(autoFixes ? { autoFixes } : {}),
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
function resolvedRange(resolved) {
|
|
174
|
-
const [start, end] = resolved.hash_bounds;
|
|
175
|
-
return {
|
|
176
|
-
startLine: start.line,
|
|
177
|
-
endLine: end.line,
|
|
178
|
-
startHash: start.hash,
|
|
179
|
-
endHash: end.hash,
|
|
180
|
-
delta: resolved.content_lines.length - (Math.abs(end.line - start.line) + 1),
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
export function fmtRegion(hashes, lines) {
|
|
184
|
-
if (hashes.length !== lines.length) {
|
|
185
|
-
throw new Error(`fmtRegion: hashes.length (${hashes.length}) must match lines.length (${lines.length}).`);
|
|
186
|
-
}
|
|
187
|
-
return lines
|
|
188
|
-
.map((line, index) => `${hashes[index]}${HASH_SEP}${line}`)
|
|
189
|
-
.join("\n");
|
|
190
|
-
}
|
|
191
|
-
export function changedRange(original, result) {
|
|
192
|
-
if (original === result)
|
|
193
|
-
return null;
|
|
194
|
-
if (original.length === 0) {
|
|
195
|
-
return {
|
|
196
|
-
firstChangedLine: 1,
|
|
197
|
-
lastChangedLine: splitLines(result).length,
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
const originalLines = splitLines(original);
|
|
201
|
-
const resultLines = splitLines(result);
|
|
202
|
-
if (originalLines.length === resultLines.length &&
|
|
203
|
-
originalLines.every((line, index) => line === resultLines[index])) {
|
|
204
|
-
return null;
|
|
205
|
-
}
|
|
206
|
-
const minLen = Math.min(originalLines.length, resultLines.length);
|
|
207
|
-
let first = 0;
|
|
208
|
-
while (first < minLen && originalLines[first] === resultLines[first]) {
|
|
209
|
-
first++;
|
|
210
|
-
}
|
|
211
|
-
let lastOrig = originalLines.length - 1;
|
|
212
|
-
let lastRes = resultLines.length - 1;
|
|
213
|
-
while (lastOrig >= first &&
|
|
214
|
-
lastRes >= first &&
|
|
215
|
-
originalLines[lastOrig] === resultLines[lastRes]) {
|
|
216
|
-
lastOrig--;
|
|
217
|
-
lastRes--;
|
|
218
|
-
}
|
|
219
|
-
return {
|
|
220
|
-
firstChangedLine: first + 1,
|
|
221
|
-
lastChangedLine: Math.max(first, lastRes) + 1,
|
|
222
|
-
};
|
|
223
|
-
}
|
|
1
|
+
// @deprecated — private to AnchorPipeline seam. Use `from "./anchor-pipeline.js"` instead.
|
|
2
|
+
export * from "./anchor-pipeline.js";
|
|
@@ -1,20 +1,255 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* HashAssign — deep module owning hash assignment.
|
|
3
3
|
*
|
|
4
|
-
* Owns the `HASH_SPACE = 62^3` allocation invariant: every line
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* Owns the `HASH_SPACE = 62^3` allocation invariant: every line gets a unique
|
|
5
|
+
* 3-char anchor derived from canonicalized content via xxHash + probe-stride.
|
|
6
|
+
* Previously split across alphabet.ts (ALPH), hasher.ts (xxh32), pure.ts
|
|
7
|
+
* (lineHashesPure/mapStableHashes), hash.ts (lineHashes persistence).
|
|
8
|
+
* Now one file owns the triad — ALPH/xxh32/hashAt are private to this seam.
|
|
8
9
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* stable mapping + persistence.
|
|
10
|
+
* Callers use this seam's public surface: lineHashesPure, mapStableHashes,
|
|
11
|
+
* lineHashes, canon, HASH_SEP, etc. Deleting this module would scatter the
|
|
12
|
+
* allocation invariant across 4 files — it concentrates.
|
|
13
13
|
*
|
|
14
14
|
* @module dsh-better-edit/hashline/hash-assign
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
export
|
|
20
|
-
|
|
16
|
+
import xxhash from "xxhash-wasm";
|
|
17
|
+
import { splitLines } from "../utils.js";
|
|
18
|
+
let hasher = null;
|
|
19
|
+
export function getH() {
|
|
20
|
+
if (hasher)
|
|
21
|
+
return hasher;
|
|
22
|
+
throw new Error("xxhash-wasm hasher not initialized; await initHasher() before calling hashline APIs.");
|
|
23
|
+
}
|
|
24
|
+
const hasherP = xxhash().then((h) => {
|
|
25
|
+
hasher = h;
|
|
26
|
+
return hasher;
|
|
27
|
+
}).catch((err) => {
|
|
28
|
+
console.error("xxhash-wasm initialization failed:", err);
|
|
29
|
+
throw err;
|
|
30
|
+
});
|
|
31
|
+
export function initHasher() {
|
|
32
|
+
return hasherP;
|
|
33
|
+
}
|
|
34
|
+
export function xxh32(input, seed = 0) {
|
|
35
|
+
return getH().h32(input, seed) >>> 0;
|
|
36
|
+
}
|
|
37
|
+
export function contentChecksum(content) {
|
|
38
|
+
return getH().h64ToString(content);
|
|
39
|
+
}
|
|
40
|
+
// --- alphabet (private to this seam) ---
|
|
41
|
+
export const HASH_LEN = 3;
|
|
42
|
+
export const ALPH = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
43
|
+
const ALPH_SAFE = ALPH.replace(/-/g, "\\-");
|
|
44
|
+
export const ALPH_RE = new RegExp(`^[${ALPH_SAFE}]+$`);
|
|
45
|
+
export const HASH_CLASS = `[${ALPH_SAFE}]{${HASH_LEN}}`;
|
|
46
|
+
export const HASH_RE = new RegExp(`^${HASH_CLASS}$`);
|
|
47
|
+
// --- pure (allocation invariant) ---
|
|
48
|
+
export const ANCHOR_LEN = HASH_LEN;
|
|
49
|
+
export const HASH_SEP = "│";
|
|
50
|
+
export const HASH_SPACE = ALPH.length ** HASH_LEN;
|
|
51
|
+
export const MAX_HASH_LINES = HASH_SPACE;
|
|
52
|
+
export const HASH_PROBE_STRIDE = ALPH.length ** 2 + ALPH.length + 1;
|
|
53
|
+
function idxToHash(idx) {
|
|
54
|
+
let out = "";
|
|
55
|
+
for (let j = 0; j < HASH_LEN; j++) {
|
|
56
|
+
out = ALPH[idx % ALPH.length] + out;
|
|
57
|
+
idx = Math.floor(idx / ALPH.length);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
const hashCache = new Map();
|
|
62
|
+
function hashAt(idx) {
|
|
63
|
+
let hash = hashCache.get(idx);
|
|
64
|
+
if (hash === undefined) {
|
|
65
|
+
hash = idxToHash(idx);
|
|
66
|
+
hashCache.set(idx, hash);
|
|
67
|
+
}
|
|
68
|
+
return hash;
|
|
69
|
+
}
|
|
70
|
+
export const HL_PREFIX_PLUS_RE = new RegExp(`^\\+${HASH_CLASS}│`);
|
|
71
|
+
export const HL_PREFIX_MINUS_RE = new RegExp(`^-(?:${HASH_CLASS}│| {${ANCHOR_LEN}}│)`);
|
|
72
|
+
export const HL_BARE_PREFIX_RE = new RegExp(`^\\s*(${HASH_CLASS})│`);
|
|
73
|
+
export const CANON_VERSION = 2;
|
|
74
|
+
const CANON_RE = /[ \t\r\n]+/g;
|
|
75
|
+
export function canon(line) {
|
|
76
|
+
return line.replace(CANON_RE, "");
|
|
77
|
+
}
|
|
78
|
+
function getCanon(cache, line) {
|
|
79
|
+
let v = cache.get(line);
|
|
80
|
+
if (v !== undefined)
|
|
81
|
+
return v;
|
|
82
|
+
v = canon(line);
|
|
83
|
+
cache.set(line, v);
|
|
84
|
+
return v;
|
|
85
|
+
}
|
|
86
|
+
const BITSET_WORDS = Math.ceil(HASH_SPACE / 32);
|
|
87
|
+
function getBit(bits, idx) {
|
|
88
|
+
return (bits[idx >>> 5] >>> (idx & 31) & 1) !== 0;
|
|
89
|
+
}
|
|
90
|
+
function setBit(bits, idx) {
|
|
91
|
+
bits[idx >>> 5] |= 1 << (idx & 31);
|
|
92
|
+
}
|
|
93
|
+
function nextZeroBit(bits, start) {
|
|
94
|
+
const totalBits = HASH_SPACE;
|
|
95
|
+
let idx = start % totalBits;
|
|
96
|
+
for (let i = 0; i < totalBits; i++) {
|
|
97
|
+
if (!getBit(bits, idx))
|
|
98
|
+
return idx;
|
|
99
|
+
idx += HASH_PROBE_STRIDE;
|
|
100
|
+
if (idx >= totalBits)
|
|
101
|
+
idx -= totalBits;
|
|
102
|
+
}
|
|
103
|
+
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.`);
|
|
104
|
+
}
|
|
105
|
+
function assignHash(used, baseIdx, hint) {
|
|
106
|
+
if (!getBit(used, baseIdx)) {
|
|
107
|
+
setBit(used, baseIdx);
|
|
108
|
+
hint.value = baseIdx + HASH_PROBE_STRIDE;
|
|
109
|
+
return hashAt(baseIdx);
|
|
110
|
+
}
|
|
111
|
+
const nextIdx = nextZeroBit(used, hint.value);
|
|
112
|
+
setBit(used, nextIdx);
|
|
113
|
+
hint.value = nextIdx + HASH_PROBE_STRIDE;
|
|
114
|
+
return hashAt(nextIdx);
|
|
115
|
+
}
|
|
116
|
+
export function lineHashesPure(content) {
|
|
117
|
+
const lines = splitLines(content);
|
|
118
|
+
const hashes = new Array(lines.length);
|
|
119
|
+
const used = new Uint32Array(BITSET_WORDS);
|
|
120
|
+
const hint = { value: 0 };
|
|
121
|
+
const canonCache = new Map();
|
|
122
|
+
for (let i = 0; i < lines.length; i++) {
|
|
123
|
+
const c = getCanon(canonCache, lines[i]);
|
|
124
|
+
const baseIdx = (xxh32(c) >>> 14) % HASH_SPACE;
|
|
125
|
+
hashes[i] = assignHash(used, baseIdx, hint);
|
|
126
|
+
}
|
|
127
|
+
return hashes;
|
|
128
|
+
}
|
|
129
|
+
function hashToIndex(hash) {
|
|
130
|
+
let idx = 0;
|
|
131
|
+
for (let j = 0; j < HASH_LEN; j++) {
|
|
132
|
+
const charIdx = ALPH.indexOf(hash[j]);
|
|
133
|
+
if (charIdx < 0)
|
|
134
|
+
return -1;
|
|
135
|
+
idx = idx * ALPH.length + charIdx;
|
|
136
|
+
}
|
|
137
|
+
return idx;
|
|
138
|
+
}
|
|
139
|
+
function nearestNew(candidates, target) {
|
|
140
|
+
let lo = 0;
|
|
141
|
+
let hi = candidates.length;
|
|
142
|
+
while (lo < hi) {
|
|
143
|
+
const mid = (lo + hi) >>> 1;
|
|
144
|
+
if (candidates[mid] < target)
|
|
145
|
+
lo = mid + 1;
|
|
146
|
+
else
|
|
147
|
+
hi = mid;
|
|
148
|
+
}
|
|
149
|
+
const left = lo - 1;
|
|
150
|
+
const right = lo;
|
|
151
|
+
if (left >= 0 && (right >= candidates.length || target - candidates[left] <= candidates[right] - target)) {
|
|
152
|
+
return left;
|
|
153
|
+
}
|
|
154
|
+
return right < candidates.length ? right : -1;
|
|
155
|
+
}
|
|
156
|
+
export function mapStableHashes(oldContent, oldHashes, newContent, removedHashes) {
|
|
157
|
+
const oldLines = splitLines(oldContent);
|
|
158
|
+
const newLines = splitLines(newContent);
|
|
159
|
+
const newHashes = new Array(newLines.length);
|
|
160
|
+
const used = new Uint32Array(BITSET_WORDS);
|
|
161
|
+
const hint = { value: 0 };
|
|
162
|
+
const canonCache = new Map();
|
|
163
|
+
const removed = removedHashes ?? new Set();
|
|
164
|
+
const oldHashIndex = new Map();
|
|
165
|
+
for (let i = 0; i < oldHashes.length; i++) {
|
|
166
|
+
const hash = oldHashes[i];
|
|
167
|
+
oldHashIndex.set(hash, i);
|
|
168
|
+
const idx = hashToIndex(hash);
|
|
169
|
+
if (idx >= 0)
|
|
170
|
+
setBit(used, idx);
|
|
171
|
+
}
|
|
172
|
+
const removedIndexes = new Set();
|
|
173
|
+
for (const hash of removed) {
|
|
174
|
+
const idx = oldHashIndex.get(hash);
|
|
175
|
+
if (idx !== undefined)
|
|
176
|
+
removedIndexes.add(idx);
|
|
177
|
+
}
|
|
178
|
+
let spanStart = oldLines.length;
|
|
179
|
+
let spanEnd = -1;
|
|
180
|
+
for (const idx of removedIndexes) {
|
|
181
|
+
if (idx < spanStart)
|
|
182
|
+
spanStart = idx;
|
|
183
|
+
if (idx > spanEnd)
|
|
184
|
+
spanEnd = idx;
|
|
185
|
+
}
|
|
186
|
+
const spanLen = spanEnd >= spanStart ? spanEnd - spanStart + 1 : 0;
|
|
187
|
+
const replacementLen = newLines.length - oldLines.length + spanLen;
|
|
188
|
+
const shiftAfterSpan = spanEnd >= spanStart ? replacementLen - spanLen : 0;
|
|
189
|
+
const survivors = [];
|
|
190
|
+
const removedEntries = [];
|
|
191
|
+
for (let i = 0; i < oldLines.length; i++) {
|
|
192
|
+
const entry = { index: i, hash: oldHashes[i] };
|
|
193
|
+
if (removedIndexes.has(i))
|
|
194
|
+
removedEntries.push(entry);
|
|
195
|
+
else
|
|
196
|
+
survivors.push(entry);
|
|
197
|
+
}
|
|
198
|
+
const newByContent = new Map();
|
|
199
|
+
for (let i = 0; i < newLines.length; i++) {
|
|
200
|
+
const key = getCanon(canonCache, newLines[i]);
|
|
201
|
+
const list = newByContent.get(key);
|
|
202
|
+
if (list)
|
|
203
|
+
list.push(i);
|
|
204
|
+
else
|
|
205
|
+
newByContent.set(key, [i]);
|
|
206
|
+
}
|
|
207
|
+
const markUsed = (hash) => {
|
|
208
|
+
const idx = hashToIndex(hash);
|
|
209
|
+
if (idx >= 0) {
|
|
210
|
+
setBit(used, idx);
|
|
211
|
+
if (idx + HASH_PROBE_STRIDE > hint.value)
|
|
212
|
+
hint.value = idx + HASH_PROBE_STRIDE;
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
for (const entry of survivors) {
|
|
216
|
+
const candidates = newByContent.get(getCanon(canonCache, oldLines[entry.index]));
|
|
217
|
+
if (!candidates || candidates.length === 0)
|
|
218
|
+
continue;
|
|
219
|
+
const target = entry.index > spanEnd ? entry.index + shiftAfterSpan : entry.index;
|
|
220
|
+
const pos = nearestNew(candidates, target);
|
|
221
|
+
if (pos < 0)
|
|
222
|
+
continue;
|
|
223
|
+
const newIdx = candidates.splice(pos, 1)[0];
|
|
224
|
+
newHashes[newIdx] = entry.hash;
|
|
225
|
+
markUsed(entry.hash);
|
|
226
|
+
}
|
|
227
|
+
const removedByContent = new Map();
|
|
228
|
+
for (const entry of removedEntries) {
|
|
229
|
+
const key = getCanon(canonCache, oldLines[entry.index]);
|
|
230
|
+
let queue = removedByContent.get(key);
|
|
231
|
+
if (!queue) {
|
|
232
|
+
queue = { hashes: [], pos: 0 };
|
|
233
|
+
removedByContent.set(key, queue);
|
|
234
|
+
}
|
|
235
|
+
queue.hashes.push(entry.hash);
|
|
236
|
+
}
|
|
237
|
+
for (let i = 0; i < newLines.length; i++) {
|
|
238
|
+
if (newHashes[i])
|
|
239
|
+
continue;
|
|
240
|
+
const queue = removedByContent.get(getCanon(canonCache, newLines[i]));
|
|
241
|
+
if (!queue || queue.pos >= queue.hashes.length)
|
|
242
|
+
continue;
|
|
243
|
+
newHashes[i] = queue.hashes[queue.pos];
|
|
244
|
+
queue.pos += 1;
|
|
245
|
+
}
|
|
246
|
+
for (let i = 0; i < newLines.length; i++) {
|
|
247
|
+
if (newHashes[i])
|
|
248
|
+
continue;
|
|
249
|
+
const c = getCanon(canonCache, newLines[i]);
|
|
250
|
+
const baseIdx = (xxh32(c) >>> 14) % HASH_SPACE;
|
|
251
|
+
newHashes[i] = assignHash(used, baseIdx, hint);
|
|
252
|
+
}
|
|
253
|
+
return newHashes;
|
|
254
|
+
}
|
|
255
|
+
// --- persistence wrapper (from hash.ts) ---
|
package/lib/hashline/hash.js
CHANGED
|
@@ -1,50 +1,88 @@
|
|
|
1
|
-
import { splitLines } from "../utils.js";
|
|
2
|
-
import { loadHashStore } from "../hash-store.js";
|
|
3
|
-
import { contentChecksum, initHasher } from "./hasher.js";
|
|
4
|
-
import { lineHashesPure, mapStableHashes } from "./pure.js";
|
|
5
1
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
2
|
+
* Hash persistence — deep persistence wrapper for HashAssign.
|
|
3
|
+
* Private to HashAssign seam; use `from "./hash-assign.js"` for pure APIs
|
|
4
|
+
* and `from "./hash.js"` only for persistence-aware lineHashes.
|
|
5
|
+
* Now imports pure APIs from hash-assign (no circular).
|
|
6
|
+
* Hash line purity seam: lineHashes takes HashSnapshotIO injection so hash.ts
|
|
7
|
+
* is the only place that touches the DB — hash-assign stays pure.
|
|
8
|
+
* @module dsh-better-edit/hashline/hash
|
|
12
9
|
*/
|
|
13
|
-
|
|
10
|
+
import { splitLines } from "../utils.js";
|
|
11
|
+
import { loadHashStore } from "../hash-store.js";
|
|
12
|
+
import { contentChecksum, initHasher, HASH_RE } from "./hash-assign.js";
|
|
13
|
+
import { lineHashesPure, mapStableHashes } from "./hash-assign.js";
|
|
14
|
+
let defaultHashSnapshotIO;
|
|
15
|
+
export function setDefaultHashSnapshotIO(io) {
|
|
16
|
+
defaultHashSnapshotIO = io;
|
|
17
|
+
}
|
|
18
|
+
export function snapshotIOFor(store) {
|
|
19
|
+
if (store) {
|
|
20
|
+
return {
|
|
21
|
+
get: (path, content, deleteCorrupt) => Promise.resolve(store.getSnapshot(path, content, deleteCorrupt)),
|
|
22
|
+
upsert: (path, checksum, lineCount, hashes) => {
|
|
23
|
+
store.upsertSnapshot(path, checksum, lineCount, hashes);
|
|
24
|
+
return Promise.resolve();
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return defaultHashSnapshotIO ?? {
|
|
29
|
+
get: async (path, content, deleteCorrupt) => {
|
|
30
|
+
const s = await loadHashStore();
|
|
31
|
+
return s.getSnapshot(path, content, deleteCorrupt);
|
|
32
|
+
},
|
|
33
|
+
upsert: async (path, checksum, lineCount, hashes) => {
|
|
34
|
+
const s = await loadHashStore();
|
|
35
|
+
s.upsertSnapshot(path, checksum, lineCount, hashes);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function isValidHashList(value) {
|
|
40
|
+
if (!Array.isArray(value))
|
|
41
|
+
return false;
|
|
42
|
+
for (const hash of value) {
|
|
43
|
+
if (typeof hash !== "string" || !HASH_RE.test(hash))
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
export async function lineHashes(content, path, previous, ioOrStore, persist) {
|
|
14
49
|
await initHasher();
|
|
15
|
-
if (!path)
|
|
50
|
+
if (!path)
|
|
16
51
|
return lineHashesPure(content);
|
|
17
|
-
|
|
18
|
-
const
|
|
52
|
+
// Back-compat: caller may pass HashStore as 4th arg; adapt to IO.
|
|
53
|
+
const io = ioOrStore && "getSnapshot" in ioOrStore
|
|
54
|
+
? snapshotIOFor(ioOrStore)
|
|
55
|
+
: ioOrStore ?? snapshotIOFor(undefined);
|
|
19
56
|
if (previous) {
|
|
20
57
|
const newHashes = mapStableHashes(previous.content, previous.hashes, content, previous.removedHashes);
|
|
21
|
-
if (persist !== false) {
|
|
58
|
+
if (persist !== false && io) {
|
|
22
59
|
try {
|
|
23
|
-
|
|
60
|
+
await io.upsert(path, contentChecksum(content), splitLines(content).length, newHashes);
|
|
24
61
|
}
|
|
25
|
-
catch (
|
|
26
|
-
console.error("Failed to persist hash snapshot:",
|
|
62
|
+
catch (e) {
|
|
63
|
+
console.error("Failed to persist hash snapshot:", e);
|
|
27
64
|
}
|
|
28
65
|
}
|
|
29
66
|
return newHashes;
|
|
30
67
|
}
|
|
31
68
|
let cached;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
69
|
+
if (io) {
|
|
70
|
+
try {
|
|
71
|
+
cached = await io.get(path, content, persist !== false);
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
console.error("Failed to read hash store snapshot:", e);
|
|
75
|
+
}
|
|
37
76
|
}
|
|
38
|
-
if (cached)
|
|
77
|
+
if (cached)
|
|
39
78
|
return cached;
|
|
40
|
-
}
|
|
41
79
|
const newHashes = lineHashesPure(content);
|
|
42
|
-
if (persist !== false) {
|
|
80
|
+
if (persist !== false && io) {
|
|
43
81
|
try {
|
|
44
|
-
|
|
82
|
+
await io.upsert(path, contentChecksum(content), splitLines(content).length, newHashes);
|
|
45
83
|
}
|
|
46
|
-
catch (
|
|
47
|
-
console.error("Failed to persist hash snapshot:",
|
|
84
|
+
catch (e) {
|
|
85
|
+
console.error("Failed to persist hash snapshot:", e);
|
|
48
86
|
}
|
|
49
87
|
}
|
|
50
88
|
return newHashes;
|
package/lib/hashline/hasher.js
CHANGED
|
@@ -1,23 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export function getH() {
|
|
4
|
-
if (hasher)
|
|
5
|
-
return hasher;
|
|
6
|
-
throw new Error("xxhash-wasm hasher not initialized; await initHasher() before calling hashline APIs.");
|
|
7
|
-
}
|
|
8
|
-
const hasherP = xxhash().then((h) => {
|
|
9
|
-
hasher = h;
|
|
10
|
-
return hasher;
|
|
11
|
-
}).catch((err) => {
|
|
12
|
-
console.error("xxhash-wasm initialization failed:", err);
|
|
13
|
-
throw err;
|
|
14
|
-
});
|
|
15
|
-
export function initHasher() {
|
|
16
|
-
return hasherP;
|
|
17
|
-
}
|
|
18
|
-
export function xxh32(input, seed = 0) {
|
|
19
|
-
return getH().h32(input, seed) >>> 0;
|
|
20
|
-
}
|
|
21
|
-
export function contentChecksum(content) {
|
|
22
|
-
return getH().h64ToString(content);
|
|
23
|
-
}
|
|
1
|
+
// @deprecated — private to HashAssign seam. Use `from "./hash-assign.js"` instead.
|
|
2
|
+
export * from "./hash-assign.js";
|
package/lib/hashline/index.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Hashline barrel — thin re-export via deep seams.
|
|
3
|
+
* Deep seams: hash-assign (allocation), anchor-pipeline (ordering), hash (persistence).
|
|
4
|
+
* @module dsh-better-edit/hashline
|
|
5
|
+
*/
|
|
6
|
+
export { CANON_VERSION, ANCHOR_LEN, HASH_SEP, HASH_SPACE, HASH_PROBE_STRIDE, MAX_HASH_LINES, HASH_LEN, HASH_CLASS, HASH_RE, ALPH_RE, HL_PREFIX_PLUS_RE, HL_PREFIX_MINUS_RE, HL_BARE_PREFIX_RE, canon, lineHashesPure, mapStableHashes, initHasher, contentChecksum, } from "./hash-assign.js";
|
|
7
|
+
export { lineHashes } from "./hash.js";
|
|
2
8
|
export { parseHashRef, parseText } from "./parse.js";
|
|
3
9
|
export { resEdit } from "./anchor-pipeline.js";
|
|
4
10
|
export { applyEdit, fmtRegion, changedRange, buildIdx, ServedRejectionError, AnchorMismatchError, isServedRejection, isAnchorMismatch, verifyServedRange, buildRangeEcho, fmtServedRows, } from "./anchor-pipeline.js";
|