dsh-better-edit 0.1.9 → 0.2.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 +32 -0
- package/README.md +168 -52
- package/README.zh.md +122 -46
- package/assets/logo.svg +183 -18
- package/lib/contract.js +95 -0
- package/lib/drift.js +1 -1
- package/lib/edit-diff.js +2 -2
- package/lib/edit-engine.js +456 -0
- package/lib/edit-pipeline.js +41 -116
- package/lib/file-reader.js +1 -2
- package/lib/fs-bridge.js +2 -2
- package/lib/fs-write.js +3 -46
- package/lib/guidance.js +282 -0
- package/lib/hash-store.js +246 -13
- package/lib/hashline/apply.js +2 -2
- package/lib/hashline/hash.js +16 -206
- package/lib/hashline/index.js +4 -3
- package/lib/hashline/parse.js +1 -1
- package/lib/hashline/pure.js +213 -0
- package/lib/hashline/resolve.js +1 -5
- package/lib/hashline/served.js +2 -2
- package/lib/index.js +48 -32
- package/lib/paths.js +55 -1
- package/lib/prompts.js +53 -49
- package/lib/read-and-serve.js +46 -0
- package/lib/schema.js +1 -1
- package/lib/served-store.js +144 -183
- package/lib/tool-batch-edit.js +28 -280
- package/lib/tool-edit.js +37 -32
- package/lib/tool-read.js +8 -31
- package/lib/tool-undo.js +4 -13
- package/lib/types/contract.d.ts +39 -0
- package/lib/types/edit-engine.d.ts +179 -0
- package/lib/types/edit-pipeline.d.ts +10 -17
- package/lib/types/fs-write.d.ts +0 -1
- package/lib/types/guidance.d.ts +98 -0
- package/lib/types/hash-store.d.ts +73 -25
- package/lib/types/hashline/hash.d.ts +8 -13
- package/lib/types/hashline/index.d.ts +4 -3
- package/lib/types/hashline/pure.d.ts +26 -0
- package/lib/types/hashline/resolve.d.ts +0 -1
- package/lib/types/index.d.ts +4 -0
- package/lib/types/paths.d.ts +9 -0
- package/lib/types/prompts.d.ts +19 -9
- package/lib/types/read-and-serve.d.ts +39 -0
- package/lib/types/schema.d.ts +2 -16
- package/lib/types/served-store.d.ts +63 -32
- package/lib/types/tool-batch-edit.d.ts +4 -1
- package/lib/types/tool-undo.d.ts +1 -1
- package/lib/undo-edit.js +16 -1
- package/lib/write-hook.js +30 -50
- package/package.json +5 -3
- package/lib/served-state.js +0 -34
- package/lib/snapshot-store.js +0 -108
- package/lib/types/served-state.d.ts +0 -3
- package/lib/types/snapshot-store.d.ts +0 -14
- package/lib/types/undo-store.d.ts +0 -14
- package/lib/undo-store.js +0 -43
package/lib/hashline/hash.js
CHANGED
|
@@ -1,89 +1,26 @@
|
|
|
1
1
|
import { splitLines } from "../utils.js";
|
|
2
|
-
import { loadHashStore
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
for (let j = 0; j < HASH_LEN; j++) {
|
|
14
|
-
out = ALPH[idx % ALPH.length] + out;
|
|
15
|
-
idx = Math.floor(idx / ALPH.length);
|
|
16
|
-
}
|
|
17
|
-
return out;
|
|
18
|
-
}
|
|
19
|
-
const hashCache = new Map();
|
|
20
|
-
function hashAt(idx) {
|
|
21
|
-
let hash = hashCache.get(idx);
|
|
22
|
-
if (hash === undefined) {
|
|
23
|
-
hash = idxToHash(idx);
|
|
24
|
-
hashCache.set(idx, hash);
|
|
25
|
-
}
|
|
26
|
-
return hash;
|
|
27
|
-
}
|
|
28
|
-
export const HL_PREFIX_PLUS_RE = new RegExp(`^\\+${HASH_CLASS}│`);
|
|
29
|
-
export const HL_PREFIX_MINUS_RE = new RegExp(`^-(?:${HASH_CLASS}│| {${ANCHOR_LEN}}│)`);
|
|
30
|
-
export const HL_BARE_PREFIX_RE = new RegExp(`^\\s*(${HASH_CLASS})│`);
|
|
31
|
-
export function canon(line) {
|
|
32
|
-
return line.replace(/\r/g, "").trimEnd();
|
|
33
|
-
}
|
|
34
|
-
const BITSET_WORDS = Math.ceil(HASH_SPACE / 32);
|
|
35
|
-
function getBit(bits, idx) {
|
|
36
|
-
return (bits[idx >>> 5] >>> (idx & 31) & 1) !== 0;
|
|
37
|
-
}
|
|
38
|
-
function setBit(bits, idx) {
|
|
39
|
-
bits[idx >>> 5] |= 1 << (idx & 31);
|
|
40
|
-
}
|
|
41
|
-
function nextZeroBit(bits, start) {
|
|
42
|
-
const totalBits = HASH_SPACE;
|
|
43
|
-
let idx = start % totalBits;
|
|
44
|
-
for (let i = 0; i < totalBits; i++) {
|
|
45
|
-
if (!getBit(bits, idx))
|
|
46
|
-
return idx;
|
|
47
|
-
idx += HASH_PROBE_STRIDE;
|
|
48
|
-
if (idx >= totalBits)
|
|
49
|
-
idx -= totalBits;
|
|
50
|
-
}
|
|
51
|
-
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.`);
|
|
52
|
-
}
|
|
53
|
-
function assignHash(used, baseIdx, hint) {
|
|
54
|
-
if (!getBit(used, baseIdx)) {
|
|
55
|
-
setBit(used, baseIdx);
|
|
56
|
-
hint.value = baseIdx + HASH_PROBE_STRIDE;
|
|
57
|
-
return hashAt(baseIdx);
|
|
58
|
-
}
|
|
59
|
-
const nextIdx = nextZeroBit(used, hint.value);
|
|
60
|
-
setBit(used, nextIdx);
|
|
61
|
-
hint.value = nextIdx + HASH_PROBE_STRIDE;
|
|
62
|
-
return hashAt(nextIdx);
|
|
63
|
-
}
|
|
64
|
-
export function _lineHashesPure(content) {
|
|
65
|
-
const lines = splitLines(content);
|
|
66
|
-
const hashes = new Array(lines.length);
|
|
67
|
-
const used = new Uint32Array(BITSET_WORDS);
|
|
68
|
-
const hint = { value: 0 };
|
|
69
|
-
for (let i = 0; i < lines.length; i++) {
|
|
70
|
-
const c = canon(lines[i]);
|
|
71
|
-
const baseIdx = (xxh32(c) >>> 14) % HASH_SPACE;
|
|
72
|
-
hashes[i] = assignHash(used, baseIdx, hint);
|
|
73
|
-
}
|
|
74
|
-
return hashes;
|
|
75
|
-
}
|
|
2
|
+
import { loadHashStore } from "../hash-store.js";
|
|
3
|
+
import { contentChecksum, initHasher } from "./hasher.js";
|
|
4
|
+
import { lineHashesPure, mapStableHashes } from "./pure.js";
|
|
5
|
+
/**
|
|
6
|
+
* Persistence-aware hash-anchor assignment: the pure hasher
|
|
7
|
+
* ({@link lineHashesPure} / {@link mapStableHashes} in ./pure.js) composed
|
|
8
|
+
* with snapshot load/upsert in the hash store, keyed by
|
|
9
|
+
* (path, content checksum, line count). Without a `path` this never touches
|
|
10
|
+
* the store and is exactly the pure path. With `previous` the stable re-hash
|
|
11
|
+
* mapping keeps surviving anchors stable across edits.
|
|
12
|
+
*/
|
|
76
13
|
export async function lineHashes(content, path, previous, store, persist) {
|
|
77
14
|
await initHasher();
|
|
78
15
|
if (!path) {
|
|
79
|
-
return
|
|
16
|
+
return lineHashesPure(content);
|
|
80
17
|
}
|
|
81
18
|
const hashStore = store ?? await loadHashStore();
|
|
82
19
|
if (previous) {
|
|
83
20
|
const newHashes = mapStableHashes(previous.content, previous.hashes, content, previous.removedHashes);
|
|
84
21
|
if (persist !== false) {
|
|
85
22
|
try {
|
|
86
|
-
upsertSnapshot(
|
|
23
|
+
hashStore.upsertSnapshot(path, contentChecksum(content), splitLines(content).length, newHashes);
|
|
87
24
|
}
|
|
88
25
|
catch (error) {
|
|
89
26
|
console.error("Failed to persist hash snapshot:", error);
|
|
@@ -93,7 +30,7 @@ export async function lineHashes(content, path, previous, store, persist) {
|
|
|
93
30
|
}
|
|
94
31
|
let cached;
|
|
95
32
|
try {
|
|
96
|
-
cached = getSnapshot(
|
|
33
|
+
cached = hashStore.getSnapshot(path, content, persist !== false);
|
|
97
34
|
}
|
|
98
35
|
catch (error) {
|
|
99
36
|
console.error("Failed to read hash store snapshot:", error);
|
|
@@ -101,10 +38,10 @@ export async function lineHashes(content, path, previous, store, persist) {
|
|
|
101
38
|
if (cached) {
|
|
102
39
|
return cached;
|
|
103
40
|
}
|
|
104
|
-
const newHashes =
|
|
41
|
+
const newHashes = lineHashesPure(content);
|
|
105
42
|
if (persist !== false) {
|
|
106
43
|
try {
|
|
107
|
-
upsertSnapshot(
|
|
44
|
+
hashStore.upsertSnapshot(path, contentChecksum(content), splitLines(content).length, newHashes);
|
|
108
45
|
}
|
|
109
46
|
catch (error) {
|
|
110
47
|
console.error("Failed to persist hash snapshot:", error);
|
|
@@ -112,130 +49,3 @@ export async function lineHashes(content, path, previous, store, persist) {
|
|
|
112
49
|
}
|
|
113
50
|
return newHashes;
|
|
114
51
|
}
|
|
115
|
-
function hashToIndex(hash) {
|
|
116
|
-
let idx = 0;
|
|
117
|
-
for (let j = 0; j < HASH_LEN; j++) {
|
|
118
|
-
const charIdx = ALPH.indexOf(hash[j]);
|
|
119
|
-
if (charIdx < 0)
|
|
120
|
-
return -1;
|
|
121
|
-
idx = idx * ALPH.length + charIdx;
|
|
122
|
-
}
|
|
123
|
-
return idx;
|
|
124
|
-
}
|
|
125
|
-
function nearestNew(candidates, target) {
|
|
126
|
-
let lo = 0;
|
|
127
|
-
let hi = candidates.length;
|
|
128
|
-
while (lo < hi) {
|
|
129
|
-
const mid = (lo + hi) >>> 1;
|
|
130
|
-
if (candidates[mid] < target)
|
|
131
|
-
lo = mid + 1;
|
|
132
|
-
else
|
|
133
|
-
hi = mid;
|
|
134
|
-
}
|
|
135
|
-
const left = lo - 1;
|
|
136
|
-
const right = lo;
|
|
137
|
-
if (left >= 0 &&
|
|
138
|
-
(right >= candidates.length ||
|
|
139
|
-
target - candidates[left] <= candidates[right] - target)) {
|
|
140
|
-
return left;
|
|
141
|
-
}
|
|
142
|
-
return right < candidates.length ? right : -1;
|
|
143
|
-
}
|
|
144
|
-
function mapStableHashes(oldContent, oldHashes, newContent, removedHashes) {
|
|
145
|
-
const oldLines = splitLines(oldContent);
|
|
146
|
-
const newLines = splitLines(newContent);
|
|
147
|
-
const newHashes = new Array(newLines.length);
|
|
148
|
-
const used = new Uint32Array(BITSET_WORDS);
|
|
149
|
-
const hint = { value: 0 };
|
|
150
|
-
const removed = removedHashes ?? new Set();
|
|
151
|
-
const oldHashIndex = new Map();
|
|
152
|
-
for (let i = 0; i < oldHashes.length; i++) {
|
|
153
|
-
const hash = oldHashes[i];
|
|
154
|
-
oldHashIndex.set(hash, i);
|
|
155
|
-
const idx = hashToIndex(hash);
|
|
156
|
-
if (idx >= 0)
|
|
157
|
-
setBit(used, idx);
|
|
158
|
-
}
|
|
159
|
-
const removedIndexes = new Set();
|
|
160
|
-
for (const hash of removed) {
|
|
161
|
-
const idx = oldHashIndex.get(hash);
|
|
162
|
-
if (idx !== undefined)
|
|
163
|
-
removedIndexes.add(idx);
|
|
164
|
-
}
|
|
165
|
-
let spanStart = oldLines.length;
|
|
166
|
-
let spanEnd = -1;
|
|
167
|
-
for (const idx of removedIndexes) {
|
|
168
|
-
if (idx < spanStart)
|
|
169
|
-
spanStart = idx;
|
|
170
|
-
if (idx > spanEnd)
|
|
171
|
-
spanEnd = idx;
|
|
172
|
-
}
|
|
173
|
-
const spanLen = spanEnd >= spanStart ? spanEnd - spanStart + 1 : 0;
|
|
174
|
-
const replacementLen = newLines.length - oldLines.length + spanLen;
|
|
175
|
-
const shiftAfterSpan = spanEnd >= spanStart ? replacementLen - spanLen : 0;
|
|
176
|
-
const survivors = [];
|
|
177
|
-
const removedEntries = [];
|
|
178
|
-
for (let i = 0; i < oldLines.length; i++) {
|
|
179
|
-
const entry = { index: i, hash: oldHashes[i] };
|
|
180
|
-
if (removedIndexes.has(i))
|
|
181
|
-
removedEntries.push(entry);
|
|
182
|
-
else
|
|
183
|
-
survivors.push(entry);
|
|
184
|
-
}
|
|
185
|
-
const newByContent = new Map();
|
|
186
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
187
|
-
const key = canon(newLines[i]);
|
|
188
|
-
const list = newByContent.get(key);
|
|
189
|
-
if (list)
|
|
190
|
-
list.push(i);
|
|
191
|
-
else
|
|
192
|
-
newByContent.set(key, [i]);
|
|
193
|
-
}
|
|
194
|
-
const markUsed = (hash) => {
|
|
195
|
-
const idx = hashToIndex(hash);
|
|
196
|
-
if (idx >= 0) {
|
|
197
|
-
setBit(used, idx);
|
|
198
|
-
if (idx + HASH_PROBE_STRIDE > hint.value)
|
|
199
|
-
hint.value = idx + HASH_PROBE_STRIDE;
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
for (const entry of survivors) {
|
|
203
|
-
const candidates = newByContent.get(canon(oldLines[entry.index]));
|
|
204
|
-
if (!candidates || candidates.length === 0)
|
|
205
|
-
continue;
|
|
206
|
-
const target = entry.index > spanEnd ? entry.index + shiftAfterSpan : entry.index;
|
|
207
|
-
const pos = nearestNew(candidates, target);
|
|
208
|
-
if (pos < 0)
|
|
209
|
-
continue;
|
|
210
|
-
const newIdx = candidates.splice(pos, 1)[0];
|
|
211
|
-
newHashes[newIdx] = entry.hash;
|
|
212
|
-
markUsed(entry.hash);
|
|
213
|
-
}
|
|
214
|
-
const removedByContent = new Map();
|
|
215
|
-
for (const entry of removedEntries) {
|
|
216
|
-
const key = canon(oldLines[entry.index]);
|
|
217
|
-
let queue = removedByContent.get(key);
|
|
218
|
-
if (!queue) {
|
|
219
|
-
queue = { hashes: [], pos: 0 };
|
|
220
|
-
removedByContent.set(key, queue);
|
|
221
|
-
}
|
|
222
|
-
queue.hashes.push(entry.hash);
|
|
223
|
-
}
|
|
224
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
225
|
-
if (newHashes[i])
|
|
226
|
-
continue;
|
|
227
|
-
const queue = removedByContent.get(canon(newLines[i]));
|
|
228
|
-
if (!queue || queue.pos >= queue.hashes.length)
|
|
229
|
-
continue;
|
|
230
|
-
newHashes[i] = queue.hashes[queue.pos];
|
|
231
|
-
queue.pos += 1;
|
|
232
|
-
}
|
|
233
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
234
|
-
if (newHashes[i])
|
|
235
|
-
continue;
|
|
236
|
-
const c = canon(newLines[i]);
|
|
237
|
-
const baseIdx = (xxh32(c) >>> 14) % HASH_SPACE;
|
|
238
|
-
newHashes[i] = assignHash(used, baseIdx, hint);
|
|
239
|
-
}
|
|
240
|
-
return newHashes;
|
|
241
|
-
}
|
package/lib/hashline/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { ANCHOR_LEN, HASH_SEP, HASH_SPACE, HASH_PROBE_STRIDE, MAX_HASH_LINES, lineHashesPure, initHasher, canon, } from "./pure.js";
|
|
2
|
+
export { lineHashes } from "./hash.js";
|
|
2
3
|
export { parseHashRef, parseText, } from "./parse.js";
|
|
3
|
-
export { resEdit,
|
|
4
|
-
export {
|
|
4
|
+
export { resEdit, } from "./resolve.js";
|
|
5
|
+
export { applyEdit, fmtRegion, changedRange, } from "./apply.js";
|
package/lib/hashline/parse.js
CHANGED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { splitLines } from "../utils.js";
|
|
2
|
+
import { xxh32, initHasher } from "./hasher.js";
|
|
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
|
+
}
|
package/lib/hashline/resolve.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { abortIf, rejectUnknownFields, firstNonEmptyIndex, lastNonEmptyIndex, clipLine, } from "../utils.js";
|
|
2
|
-
import { HASH_CLASS, HL_BARE_PREFIX_RE, HL_PREFIX_PLUS_RE, HL_PREFIX_MINUS_RE, canon, } from "./
|
|
2
|
+
import { HASH_CLASS, HL_BARE_PREFIX_RE, HL_PREFIX_PLUS_RE, HL_PREFIX_MINUS_RE, canon, } from "./pure.js";
|
|
3
3
|
import { parseHashRef, parseText } from "./parse.js";
|
|
4
4
|
import { NEW_CONTENT_NOT_STRING_MSG } from "../constants.js";
|
|
5
5
|
function resAnchorFromMap(ref, hashIndex) {
|
|
@@ -21,10 +21,6 @@ function assertAligned(fileLines, fileHashes, ctx) {
|
|
|
21
21
|
throw new Error(`${ctx}: fileHashes.length (${fileHashes.length}) must match fileLines.length (${fileLines.length}).`);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
-
export function fmtMismatch(mismatches, fileLines, fileHashes, filePath) {
|
|
25
|
-
return fmtMismatchWithServes(mismatches, fileLines, fileHashes, filePath)
|
|
26
|
-
.message;
|
|
27
|
-
}
|
|
28
24
|
export function fmtMismatchWithServes(mismatches, fileLines, fileHashes, filePath) {
|
|
29
25
|
assertAligned(fileLines, fileHashes, "fmtMismatch");
|
|
30
26
|
const out = [];
|
package/lib/hashline/served.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { HASH_SEP } from "./
|
|
2
|
-
import { recordServed, servedPositionsOf } from "../served-
|
|
1
|
+
import { HASH_SEP } from "./pure.js";
|
|
2
|
+
import { recordServed, servedPositionsOf } from "../served-store.js";
|
|
3
3
|
import { SERVED_ECHO_CAP } from "../constants.js";
|
|
4
4
|
export class ServedRejectionError extends Error {
|
|
5
5
|
code;
|
package/lib/index.js
CHANGED
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
* automatically when the agent is disposed. The built-in `write` stays in
|
|
10
10
|
* place; a scoped `tools/post-execute` listener appends the fresh hashline
|
|
11
11
|
* preview to write results.
|
|
12
|
+
*
|
|
13
|
+
* The four `tool:*` guidance sections resolve per agent preset from override
|
|
14
|
+
* files in the shared home (see `src/guidance.ts`); deployments without the
|
|
15
|
+
* `agentPresets` service keep the compiled defaults unchanged.
|
|
12
16
|
* @module dsh-better-edit
|
|
13
17
|
*/
|
|
14
18
|
import { ctxFsIO } from "./fs-bridge.js";
|
|
@@ -19,7 +23,8 @@ import { registerBatchEditTool } from "./tool-batch-edit.js";
|
|
|
19
23
|
import { registerUndoTool } from "./tool-undo.js";
|
|
20
24
|
import { registerWriteHook } from "./write-hook.js";
|
|
21
25
|
import { initHasher } from "./hashline/hasher.js";
|
|
22
|
-
import {
|
|
26
|
+
import { composeSections, ensurePresetGuidance, GUIDANCE_SECTIONS, } from "./guidance.js";
|
|
27
|
+
import { configDir } from "./paths.js";
|
|
23
28
|
/** Cordis plugin name used by loader diagnostics. */
|
|
24
29
|
export const name = "dsh-better-edit";
|
|
25
30
|
/**
|
|
@@ -30,8 +35,36 @@ export const name = "dsh-better-edit";
|
|
|
30
35
|
* session-start.
|
|
31
36
|
*/
|
|
32
37
|
export const inject = ["tools", "systemPrompt", "fs"];
|
|
38
|
+
/** The four sections as compiled, byte-identical to the pre-guidance install. */
|
|
39
|
+
function compiledDefaultSections() {
|
|
40
|
+
return GUIDANCE_SECTIONS.map((section) => ({
|
|
41
|
+
name: section.name,
|
|
42
|
+
order: section.defaultOrder,
|
|
43
|
+
text: section.renderDefault(),
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resolve the four guidance sections for one agent. Without the `agentPresets`
|
|
48
|
+
* service the fast path returns the compiled defaults untouched. With it, the
|
|
49
|
+
* agent's preset id drives `composeSections` against the shared home; any
|
|
50
|
+
* resolution failure degrades to compiled defaults so a bad override file can
|
|
51
|
+
* never fail the install.
|
|
52
|
+
*/
|
|
53
|
+
async function resolveAgentSections(rootCtx, agent) {
|
|
54
|
+
const agentPresets = rootCtx.get("agentPresets");
|
|
55
|
+
if (!agentPresets)
|
|
56
|
+
return compiledDefaultSections();
|
|
57
|
+
try {
|
|
58
|
+
const presetId = agentPresets.composedPreset(agent.ctx);
|
|
59
|
+
return await composeSections(presetId, configDir());
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
rootCtx.logger.warn(`dsh-better-edit: guidance resolution failed for agent ${agent.id}, using compiled defaults: ${error instanceof Error ? error.message : String(error)}`);
|
|
63
|
+
return compiledDefaultSections();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
33
66
|
function installAgentTools(rootCtx, agent) {
|
|
34
|
-
|
|
67
|
+
agent.ctx.effect(async () => {
|
|
35
68
|
// `fs` is host-plane: use the plugin's own context (covered by
|
|
36
69
|
// inject) rather than the agent's scoped one, whose fiber chain does
|
|
37
70
|
// not declare it. Session cwd still reaches the bridge per call via
|
|
@@ -44,36 +77,13 @@ function installAgentTools(rootCtx, agent) {
|
|
|
44
77
|
disposers.push(registerBatchEditTool(rootCtx, agent.ctx, io, sandbox));
|
|
45
78
|
disposers.push(registerUndoTool(rootCtx, agent.ctx, io, sandbox));
|
|
46
79
|
disposers.push(registerWriteHook(rootCtx, agent.ctx, io));
|
|
47
|
-
// Shadow the preset's built-in tool guidance with the hashline
|
|
48
|
-
// Same section names on the agent's own layer win over the
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
"",
|
|
55
|
-
EDIT_GUIDELINES.map((line) => `- ${line}`).join("\n"),
|
|
56
|
-
].join("\n"),
|
|
57
|
-
}));
|
|
58
|
-
disposers.push(agent.ctx.systemPrompt.section({
|
|
59
|
-
name: "tool:read",
|
|
60
|
-
order: 100,
|
|
61
|
-
text: [
|
|
62
|
-
"Use the read tool — not shell commands like cat — to inspect text files.",
|
|
63
|
-
"",
|
|
64
|
-
READ_GUIDELINES.map((line) => `- ${line}`).join("\n"),
|
|
65
|
-
].join("\n"),
|
|
66
|
-
}));
|
|
67
|
-
disposers.push(agent.ctx.systemPrompt.section({
|
|
68
|
-
name: "tool:batch_edit",
|
|
69
|
-
order: 103,
|
|
70
|
-
text: BATCH_EDIT_GUIDELINES.map((line) => `- ${line}`).join("\n"),
|
|
71
|
-
}));
|
|
72
|
-
disposers.push(agent.ctx.systemPrompt.section({
|
|
73
|
-
name: "tool:undo_last_edit",
|
|
74
|
-
order: 104,
|
|
75
|
-
text: UNDO_GUIDELINES.map((line) => `- ${line}`).join("\n"),
|
|
76
|
-
}));
|
|
80
|
+
// Shadow the preset's built-in tool guidance with the hashline
|
|
81
|
+
// contract. Same section names on the agent's own layer win over the
|
|
82
|
+
// preset's; text and order come from the per-preset resolution.
|
|
83
|
+
const sections = await resolveAgentSections(rootCtx, agent);
|
|
84
|
+
for (const section of sections) {
|
|
85
|
+
disposers.push(agent.ctx.systemPrompt.section(section));
|
|
86
|
+
}
|
|
77
87
|
return () => {
|
|
78
88
|
for (const dispose of disposers)
|
|
79
89
|
dispose();
|
|
@@ -88,6 +98,12 @@ export function apply(rootCtx) {
|
|
|
88
98
|
initHasher().catch((error) => {
|
|
89
99
|
rootCtx.logger.warn(`dsh-better-edit: hasher warm-up failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
90
100
|
});
|
|
101
|
+
// Seed each shipped preset's guidance directory once, so users have
|
|
102
|
+
// editable per-preset overrides (idempotent: never rewrites existing
|
|
103
|
+
// files). A failure must never fail the boot.
|
|
104
|
+
ensurePresetGuidance(configDir()).catch((error) => {
|
|
105
|
+
rootCtx.logger.warn(`dsh-better-edit: guidance materialization failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
106
|
+
});
|
|
91
107
|
const registered = new WeakSet();
|
|
92
108
|
rootCtx.on("agent/session-start", ({ agent }) => {
|
|
93
109
|
if (registered.has(agent))
|