dsh-better-edit 0.1.9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +12 -0
  3. package/README.zh.md +9 -0
  4. package/assets/logo.svg +183 -18
  5. package/lib/contract.js +95 -0
  6. package/lib/drift.js +1 -1
  7. package/lib/edit-diff.js +2 -2
  8. package/lib/edit-engine.js +456 -0
  9. package/lib/edit-pipeline.js +41 -116
  10. package/lib/file-reader.js +1 -2
  11. package/lib/fs-bridge.js +2 -2
  12. package/lib/fs-write.js +3 -46
  13. package/lib/hash-store.js +246 -13
  14. package/lib/hashline/apply.js +2 -2
  15. package/lib/hashline/hash.js +16 -206
  16. package/lib/hashline/index.js +4 -3
  17. package/lib/hashline/parse.js +1 -1
  18. package/lib/hashline/pure.js +213 -0
  19. package/lib/hashline/resolve.js +1 -5
  20. package/lib/hashline/served.js +2 -2
  21. package/lib/paths.js +55 -1
  22. package/lib/read-and-serve.js +46 -0
  23. package/lib/schema.js +1 -1
  24. package/lib/served-store.js +144 -183
  25. package/lib/tool-batch-edit.js +28 -280
  26. package/lib/tool-edit.js +37 -32
  27. package/lib/tool-read.js +8 -31
  28. package/lib/tool-undo.js +4 -13
  29. package/lib/types/contract.d.ts +39 -0
  30. package/lib/types/edit-engine.d.ts +179 -0
  31. package/lib/types/edit-pipeline.d.ts +10 -17
  32. package/lib/types/fs-write.d.ts +0 -1
  33. package/lib/types/hash-store.d.ts +73 -25
  34. package/lib/types/hashline/hash.d.ts +8 -13
  35. package/lib/types/hashline/index.d.ts +4 -3
  36. package/lib/types/hashline/pure.d.ts +26 -0
  37. package/lib/types/hashline/resolve.d.ts +0 -1
  38. package/lib/types/paths.d.ts +9 -0
  39. package/lib/types/read-and-serve.d.ts +39 -0
  40. package/lib/types/schema.d.ts +2 -16
  41. package/lib/types/served-store.d.ts +63 -32
  42. package/lib/types/tool-batch-edit.d.ts +4 -1
  43. package/lib/types/tool-undo.d.ts +1 -1
  44. package/lib/undo-edit.js +16 -1
  45. package/lib/write-hook.js +30 -50
  46. package/package.json +4 -3
  47. package/lib/served-state.js +0 -34
  48. package/lib/snapshot-store.js +0 -108
  49. package/lib/types/served-state.d.ts +0 -3
  50. package/lib/types/snapshot-store.d.ts +0 -14
  51. package/lib/types/undo-store.d.ts +0 -14
  52. package/lib/undo-store.js +0 -43
@@ -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
+ }
@@ -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 "./hash.js";
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 = [];
@@ -1,5 +1,5 @@
1
- import { HASH_SEP } from "./hash.js";
2
- import { recordServed, servedPositionsOf } from "../served-state.js";
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/paths.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { homedir } from "node:os";
2
- import { isAbsolute, resolve as resolvePath, join, dirname } from "node:path";
2
+ import { isAbsolute, resolve as resolvePath, join, dirname, parse, sep, } from "node:path";
3
+ import { lstat, readlink } from "node:fs/promises";
3
4
  import { resolveDshHome } from "@deepseek-ai/dsh-home-paths";
5
+ import { errCode } from "./utils.js";
4
6
  /**
5
7
  * On-disk home for dsh-better-edit state. Inside a tool call the store lives
6
8
  * co-located with the files being edited: `<workspace>/.dsh_better_edit/` (the
@@ -41,3 +43,55 @@ export function toCwd(filePath, cwd) {
41
43
  const expanded = expand(filePath);
42
44
  return isAbsolute(expanded) ? expanded : resolvePath(cwd, expanded);
43
45
  }
46
+ /**
47
+ * Canonicalize a path, resolving every symlink component to its target
48
+ * (loop-guarded, ELOOP on cycles). Non-existent final components resolve
49
+ * lexically — the canonical form of a not-yet-created file. The hashline
50
+ * tools key their state by canonical absolute paths, so the same file reached
51
+ * through different symlink spellings lands on the same store rows.
52
+ * @param path - the path to canonicalize (absolute or relative).
53
+ */
54
+ export async function resolveTarget(path) {
55
+ const absolutePath = resolvePath(path);
56
+ const { root } = parse(absolutePath);
57
+ const parts = absolutePath
58
+ .slice(root.length)
59
+ .split(sep)
60
+ .filter((part) => part.length > 0);
61
+ const visitedSymlinks = new Set();
62
+ async function resParts(currentPath, remainingParts) {
63
+ if (remainingParts.length === 0) {
64
+ return currentPath;
65
+ }
66
+ const [nextPart, ...tail] = remainingParts;
67
+ const candidatePath = join(currentPath, nextPart);
68
+ try {
69
+ const candidateStats = await lstat(candidatePath);
70
+ if (!candidateStats.isSymbolicLink()) {
71
+ return resParts(candidatePath, tail);
72
+ }
73
+ if (visitedSymlinks.has(candidatePath)) {
74
+ const error = new Error(`Too many symbolic links while resolving ${path}`);
75
+ error.code = "ELOOP";
76
+ throw error;
77
+ }
78
+ visitedSymlinks.add(candidatePath);
79
+ const linkTargetPath = resolvePath(dirname(candidatePath), await readlink(candidatePath));
80
+ const targetParts = linkTargetPath
81
+ .slice(parse(linkTargetPath).root.length)
82
+ .split(sep)
83
+ .filter((part) => part.length > 0);
84
+ return resParts(parse(linkTargetPath).root, [
85
+ ...targetParts,
86
+ ...tail,
87
+ ]);
88
+ }
89
+ catch (error) {
90
+ if (errCode(error) === "ENOENT") {
91
+ return join(candidatePath, ...tail);
92
+ }
93
+ throw error;
94
+ }
95
+ }
96
+ return resParts(root, parts);
97
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * The shared read-and-serve operation: resolve → read → normalize → render
3
+ * the hashline preview → record the served rows → clear the drift marks →
4
+ * append the UTF-8 rewrite note. Used by the `read` tool and by the write
5
+ * auto-read hook, so the model is always shown fresh anchors the same way no
6
+ * matter which tool produced the read.
7
+ * @module dsh-better-edit/read-and-serve
8
+ */
9
+ import { abortIf } from "./utils.js";
10
+ import { normFromText } from "./file-reader.js";
11
+ import { fmtReadPreview, MAX_HASH_LINES } from "./read-render.js";
12
+ import { recordServed, clearDriftReported } from "./served-store.js";
13
+ /** Appended when the file had non-UTF-8 bytes; editing rewrites it as UTF-8. */
14
+ export const UTF8_REWRITE_NOTE = "[Non-UTF-8 bytes shown as U+FFFD; editing rewrites the file as UTF-8.]";
15
+ /**
16
+ * Perform one read-and-serve: normalize the file at `rawPath`, render its
17
+ * hashline preview, record the shown rows as served for the session, and clear
18
+ * the reported-drift marks (a fresh read resets them). The returned text
19
+ * carries the UTF-8 rewrite note when the file had decode errors.
20
+ *
21
+ * Emits nothing on the fs-observation gate — callers that need the
22
+ * observation recorded (the `read` tool) do that themselves with their exec
23
+ * context.
24
+ */
25
+ export async function readAndServe(io, rawPath, cwd, options) {
26
+ const { sessionKey, signal } = options;
27
+ abortIf(signal);
28
+ const absolutePath = await io.resolve(rawPath, cwd, signal);
29
+ const rawText = await io.readText(absolutePath, signal);
30
+ const { normalized, fileHashes, hadUtf8DecodeErrors } = await normFromText({
31
+ absolutePath,
32
+ rawText,
33
+ displayPath: rawPath,
34
+ signal,
35
+ maxLines: MAX_HASH_LINES,
36
+ });
37
+ const preview = await fmtReadPreview(normalized, { offset: options.offset, limit: options.limit }, fileHashes, absolutePath);
38
+ if (preview.served.length > 0) {
39
+ await recordServed(sessionKey, absolutePath, preview.served, fileHashes.length);
40
+ }
41
+ await clearDriftReported(sessionKey, absolutePath);
42
+ const text = hadUtf8DecodeErrors
43
+ ? `${preview.text}\n\n${UTF8_REWRITE_NOTE}`
44
+ : preview.text;
45
+ return { text, served: preview.served, hadUtf8DecodeErrors, absolutePath };
46
+ }
package/lib/schema.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * the dsh schema DSL (not TypeBox). `path` is deliberately NOT `required` at
4
4
  * the schema level: the tools accept the built-in `file_path` spelling too
5
5
  * (the implicit parameter root stays open), and enforce path presence in
6
- * `assertReq` after `normalizeFilePath` aliasing.
6
+ * `assertEditRequest` after `normalizeFilePath` aliasing.
7
7
  * @module dsh-better-edit/schema
8
8
  */
9
9
  export const replacementTextSchema = {