dsh-better-edit 0.4.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/fs-bridge.js CHANGED
@@ -16,6 +16,8 @@
16
16
  */
17
17
  import { readFile } from "node:fs/promises";
18
18
  import { writeAtomic } from "./fs-write.js";
19
+ import { loadConfig } from "./store-config.js";
20
+ import { detectBom, isValidUtf8, decodeBytes, encodeText, normalizeEncoding, top3Candidates, isSupportedEncoding, detectWithChardet, chardetTop3Candidates, getTop3Candidates } from "./encoding.js";
19
21
  import { fileSnap } from "./file-reader.js";
20
22
  import { resolveTarget, toCwd } from "./paths.js";
21
23
  /**
@@ -37,7 +39,13 @@ export function mapFsError(error, displayPath) {
37
39
  throw new Error(`[E_ACCESS] Cannot access file: ${displayPath}`);
38
40
  }
39
41
  if (code === "FS_NOT_TEXT" || code === "FS_NOT_REGULAR_FILE") {
40
- throw new Error(`[E_NOT_TEXT] Path is not a readable UTF-8 text file: ${displayPath}. Hashline editing only supports text files.`);
42
+ throw new Error(`[E_NOT_TEXT] Path is not a readable UTF-8 text file: ${displayPath}. Hashline editing only supports text files. Try read({encoding: "gbk"}) or enable autoGuessEncoding.`);
43
+ }
44
+ if (code === "FS_BAD_ENCODING") {
45
+ throw new Error(`[E_BAD_ENCODING] Unknown encoding for ${displayPath}. Supported: utf8, gbk, big5, shift_jis, euc-kr, windows-1251, iso-8859-1`);
46
+ }
47
+ if (code === "FS_DECODE_FAILED") {
48
+ throw new Error(`[E_DECODE_FAILED] Bytes in ${displayPath} cannot be decoded with requested encoding.`);
41
49
  }
42
50
  if (code === "FS_STALE_VERSION") {
43
51
  throw new Error(`[E_RANGE_STALE] The file changed on disk since it was read (version guard rejected the write). Call read() to get fresh anchors, then retry.`);
@@ -54,6 +62,35 @@ export function mapFsError(error, displayPath) {
54
62
  const UTF8_BOM = "\uFEFF";
55
63
  const UTF8_BOM_BYTES = [0xef, 0xbb, 0xbf];
56
64
  const UTF8_BOM_LEN = UTF8_BOM_BYTES.length;
65
+ const encodingMemo = new Map();
66
+ const autoGuessFooterMemo = new Map();
67
+ export function getAutoGuessFooter(targetKey) {
68
+ return autoGuessFooterMemo.get(targetKey);
69
+ }
70
+ export function setAutoGuessFooter(targetKey, footer) {
71
+ autoGuessFooterMemo.set(targetKey, footer);
72
+ }
73
+ export function clearAutoGuessFooter(targetKey) {
74
+ if (targetKey)
75
+ autoGuessFooterMemo.delete(targetKey);
76
+ else
77
+ autoGuessFooterMemo.clear();
78
+ }
79
+ export function getEncodingState(targetKey) {
80
+ return encodingMemo.get(targetKey);
81
+ }
82
+ export function setEncodingState(targetKey, state) {
83
+ encodingMemo.set(targetKey, state);
84
+ }
85
+ export function clearEncodingState(targetKey) {
86
+ if (targetKey)
87
+ encodingMemo.delete(targetKey);
88
+ else
89
+ encodingMemo.clear();
90
+ }
91
+ function isFsNotText(error) {
92
+ return error instanceof Error && typeof error.code === "string" && (error.code === "FS_NOT_TEXT" || error.message.includes("FS_NOT_TEXT"));
93
+ }
57
94
  /**
58
95
  * Restore a UTF-8 BOM consumed by a backend's {@link TextDecoder}. The raw
59
96
  * byte seam reads whole files rather than prefixes, so first use stat size to
@@ -98,8 +135,34 @@ export function ctxFsIO(fs, ctx) {
98
135
  });
99
136
  return fs.processPath(target);
100
137
  },
101
- async readText(absolutePath, signal) {
138
+ async readText(absolutePath, signal, encodingHint) {
102
139
  try {
140
+ // explicit encoding override (Reopen with Encoding)
141
+ if (encodingHint) {
142
+ const norm = normalizeEncoding(encodingHint);
143
+ if (!norm)
144
+ throw new (await import("@deepseek-ai/dsh-fs")).FsError("bad encoding", "FS_BAD_ENCODING");
145
+ const target = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
146
+ const info = await fs.stat(target, signal);
147
+ const maxBytes = info?.size ?? 10 * 1024 * 1024;
148
+ const bytes = await fs.readBytes(target, signal, maxBytes);
149
+ const bom = detectBom(bytes);
150
+ const slice = bom ? bytes.subarray(bom.bomLen) : bytes;
151
+ // for utf16 etc, decode via helper
152
+ let decoded = decodeBytes(bom ? bytes : slice, bom ? bom.encoding : norm);
153
+ if (bom && (bom.encoding === "utf8bom" || bom.encoding === "utf16le" || bom.encoding === "utf16be")) {
154
+ decoded = decodeBytes(bytes, bom.encoding);
155
+ }
156
+ else if (norm) {
157
+ const off = bom ? bom.bomLen : 0;
158
+ decoded = decodeBytes(bytes.subarray(off), norm);
159
+ }
160
+ if (decoded === undefined)
161
+ throw new (await import("@deepseek-ai/dsh-fs")).FsError("decode failed", "FS_DECODE_FAILED");
162
+ const version = info?.version;
163
+ setEncodingState(String(target.targetKey ?? absolutePath), { encoding: norm, hasBOM: !!bom, version });
164
+ return decoded;
165
+ }
103
166
  const target = await fs.resolve(absolutePath, {
104
167
  ...(signal === undefined ? {} : { signal }),
105
168
  });
@@ -107,10 +170,115 @@ export function ctxFsIO(fs, ctx) {
107
170
  return await restoreStrippedUtf8Bom(fs, target, text, signal);
108
171
  }
109
172
  catch (error) {
173
+ // fallback for non-UTF8 when autoGuess enabled; when disabled surface top-3 + hint
174
+ const isNotText = error instanceof Error && error.code === "FS_NOT_TEXT";
175
+ if (isNotText) {
176
+ try {
177
+ const cfg = loadConfig();
178
+ if (cfg.autoGuessEncoding) {
179
+ const target = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
180
+ const info = await fs.stat(target, signal);
181
+ const maxBytes = info?.size ?? 10 * 1024 * 1024;
182
+ if (info && info.size !== undefined && info.size > maxBytes)
183
+ throw error;
184
+ const bytes = await fs.readBytes(target, signal, maxBytes);
185
+ const bom = detectBom(bytes);
186
+ if (bom) {
187
+ const dec = decodeBytes(bytes, bom.encoding);
188
+ if (dec !== undefined) {
189
+ setEncodingState(String(target.targetKey ?? absolutePath), { encoding: bom.encoding, hasBOM: true, version: info?.version });
190
+ return dec;
191
+ }
192
+ }
193
+ if (isValidUtf8(bytes)) {
194
+ const dec = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
195
+ setEncodingState(String(target.targetKey ?? absolutePath), { encoding: "utf8", hasBOM: false, version: info?.version });
196
+ return dec;
197
+ }
198
+ // score allowlist top-3, pick best for now (model will pick via details.candidates in tool layer)
199
+ const allow = cfg.supportedEncodings;
200
+ // try chardet (optional dep) first for more accurate top-1 — with footer for mid-confidence
201
+ try {
202
+ const chardetCands = await chardetTop3Candidates(bytes, allow);
203
+ if (chardetCands.length > 0) {
204
+ const top = chardetCands[0];
205
+ const second = chardetCands[1];
206
+ const isMid = top.confidence < 70 || (second !== undefined && top.confidence - second.confidence < 10);
207
+ const dec = decodeBytes(bytes, top.encoding);
208
+ if (dec !== undefined && !dec.includes("\uFFFD")) {
209
+ let footer = "";
210
+ if (isMid) {
211
+ const candsStr = chardetCands.map((c) => `${c.encoding} ${c.confidence}`).join(", ");
212
+ footer = `\n\n[Auto-guessed: ${top.encoding} ${top.confidence}, candidates: ${candsStr} — re-read with read({encoding}) if garbled]`;
213
+ }
214
+ setEncodingState(String(target.targetKey ?? absolutePath), { encoding: top.encoding, hasBOM: false, version: info?.version });
215
+ if (footer)
216
+ setAutoGuessFooter(String(target.targetKey ?? absolutePath), footer);
217
+ return dec;
218
+ }
219
+ }
220
+ }
221
+ catch { } // biome-ignore: best-effort
222
+ const candidates = top3Candidates(bytes, allow);
223
+ if (candidates.length > 0) {
224
+ const best = candidates[0];
225
+ const second2 = candidates[1];
226
+ const isMidHeu = second2 !== undefined && best.score - second2.score < 10;
227
+ let footerHeu = "";
228
+ {
229
+ const candsStrHeu = candidates.map((c) => `${c.encoding} ${c.score.toFixed(0)}`).join(", ");
230
+ footerHeu = `\n\n[Auto-guessed: ${best.encoding} ${best.score.toFixed(0)}, candidates: ${candsStrHeu} — re-read with read({encoding}) if garbled]`;
231
+ }
232
+ const decHeu = decodeBytes(bytes, best.encoding);
233
+ if (decHeu !== undefined) {
234
+ setEncodingState(String(target.targetKey ?? absolutePath), { encoding: best.encoding, hasBOM: false, version: info?.version });
235
+ if (footerHeu)
236
+ setAutoGuessFooter(String(target.targetKey ?? absolutePath), footerHeu);
237
+ return decHeu;
238
+ }
239
+ }
240
+ }
241
+ // autoGuess disabled or no candidate succeeded — surface top-3 + env hint
242
+ try {
243
+ const target2 = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
244
+ const info2 = await fs.stat(target2, signal);
245
+ const maxBytes2 = info2?.size ?? 10 * 1024 * 1024;
246
+ const bytes2 = await fs.readBytes(target2, signal, maxBytes2);
247
+ const allow2 = cfg.supportedEncodings;
248
+ const candsViaChardet = await chardetTop3Candidates(bytes2, allow2);
249
+ const cands = candsViaChardet.length > 0 ? candsViaChardet.map((c) => ({ encoding: c.encoding, sample: c.sample, score: c.confidence })) : top3Candidates(bytes2, allow2);
250
+ if (cands.length > 0) {
251
+ const candStr = cands.map((c) => `${c.encoding}("${c.sample.slice(0, 20).replace(/"/g, "'")}")`).join(", ");
252
+ throw new Error(`[E_NOT_TEXT] Path is not a readable UTF-8 text file: ${absolutePath}. Hashline editing only supports text files. Top-3 guesses: ${candStr}. Try read({encoding: "<encoding>"}) or set DSH_BETTER_EDIT_AUTO_GUESS_ENCODING=true to auto-decode.`);
253
+ }
254
+ }
255
+ catch (inner) {
256
+ if (inner instanceof Error && inner.message.includes("[E_NOT_TEXT]"))
257
+ throw inner;
258
+ }
259
+ }
260
+ catch (e) {
261
+ if (e instanceof Error && e.message.includes("[E_NOT_TEXT]"))
262
+ throw e;
263
+ }
264
+ }
110
265
  return mapFsError(error, absolutePath);
111
266
  }
112
267
  },
113
- async writeText(absolutePath, content, signal, exec, sandboxPolicy) {
268
+ async writeText(absolutePath, content, signal, exec, sandboxPolicy, encodingHint) {
269
+ // handle drift invalidation before encode
270
+ try {
271
+ const targetTmp = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
272
+ const key = String(targetTmp.targetKey ?? absolutePath);
273
+ const memo = encodingMemo.get(key);
274
+ if (memo) {
275
+ const info = await fs.stat(targetTmp, signal);
276
+ const curVer = info?.version;
277
+ if (curVer !== memo.version)
278
+ encodingMemo.delete(key);
279
+ }
280
+ }
281
+ catch { } // biome-ignore: best-effort
114
282
  try {
115
283
  const target = await fs.resolve(absolutePath, {
116
284
  ...(signal === undefined ? {} : { signal }),
@@ -123,6 +291,30 @@ export function ctxFsIO(fs, ctx) {
123
291
  // confined backend checks: without it the backend falls back to
124
292
  // the deployment default root and denies writes inside the
125
293
  // session workspace under workspace-write.
294
+ // re-encode if needed (round-trip unless normalizeToUtf8)
295
+ const _writeContent = content; // keep for future use
296
+ try {
297
+ const cfgW = loadConfig();
298
+ if (encodingHint) {
299
+ const normW = normalizeEncoding(encodingHint);
300
+ if (!normW)
301
+ throw new (await import("@deepseek-ai/dsh-fs")).FsError("bad encoding", "FS_BAD_ENCODING");
302
+ // Save with Encoding: update memo and write as requested encoding (but fs.writeText expects utf8 string, so we keep utf8 string and update memo for next read?)
303
+ // For now, record memo; actual bytes will be written as utf8 string (provider normalizes). For legacy preserve, we would need writeBytes seam — deferred, record state only.
304
+ const tgt = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
305
+ const _inf = await fs.stat(tgt, signal);
306
+ setEncodingState(String(tgt.targetKey ?? absolutePath), { encoding: normW, hasBOM: normW === "utf8bom", version: undefined });
307
+ }
308
+ else if (!cfgW.normalizeToUtf8) {
309
+ const tgt2 = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
310
+ const mem = encodingMemo.get(String(tgt2.targetKey ?? absolutePath));
311
+ if (mem && mem.encoding !== "utf8" && mem.encoding !== "utf8bom") {
312
+ // For provider that expects utf8 string, we keep string as-is (round-trip would need raw bytes seam). Record indicates file was legacy.
313
+ // No transcode on wire — provider will write utf8; next read will see new version and re-detect.
314
+ }
315
+ }
316
+ }
317
+ catch { } // biome-ignore: best-effort
126
318
  const outcome = await fs.writeText(target, content, intent, signal, sandboxPolicy);
127
319
  // Record the present observation (a no-op when no policy
128
320
  // plugin listens), so later built-in tools see the new version.
@@ -170,8 +362,78 @@ export function localIO() {
170
362
  async resolve(path, cwd) {
171
363
  return resolveTarget(toCwd(path, cwd ?? process.cwd()));
172
364
  },
173
- async readText(absolutePath, signal) {
365
+ async readText(absolutePath, signal, encodingHint) {
174
366
  signal?.throwIfAborted();
367
+ if (encodingHint) {
368
+ const norm = normalizeEncoding(encodingHint);
369
+ if (!norm)
370
+ throw new Error(`[E_BAD_ENCODING] Unknown encoding: ${encodingHint}`);
371
+ const bytes = await readFile(absolutePath);
372
+ const bom = detectBom(bytes);
373
+ const off = bom ? bom.bomLen : 0;
374
+ const dec = decodeBytes(bytes.subarray(off), norm ?? "utf8");
375
+ if (dec === undefined)
376
+ throw new Error(`[E_DECODE_FAILED] Cannot decode ${absolutePath} as ${norm}`);
377
+ const info = await fileSnap(absolutePath).catch(() => undefined);
378
+ setEncodingState(absolutePath, { encoding: norm, hasBOM: !!bom, version: info?.snapshotId });
379
+ return dec;
380
+ }
381
+ // try deterministic BOM→UTF8 first, then autoGuess if enabled
382
+ try {
383
+ const bytes = await readFile(absolutePath);
384
+ const bom = detectBom(bytes);
385
+ if (bom) {
386
+ const dec = decodeBytes(bytes, bom.encoding);
387
+ if (dec !== undefined) {
388
+ const info = await fileSnap(absolutePath).catch(() => undefined);
389
+ setEncodingState(absolutePath, { encoding: bom.encoding, hasBOM: true, version: info?.snapshotId });
390
+ return dec;
391
+ }
392
+ }
393
+ if (isValidUtf8(bytes))
394
+ return bytes.toString("utf-8");
395
+ const cfgL = loadConfig();
396
+ if (cfgL.autoGuessEncoding) {
397
+ // chardet first with footer
398
+ try {
399
+ const chardetCandsL = await chardetTop3Candidates(bytes, cfgL.supportedEncodings);
400
+ if (chardetCandsL.length > 0) {
401
+ const topL = chardetCandsL[0];
402
+ const secondL = chardetCandsL[1];
403
+ const isMidL = topL.confidence < 70 || (secondL !== undefined && topL.confidence - secondL.confidence < 10);
404
+ const decL2 = decodeBytes(bytes, topL.encoding);
405
+ if (decL2 !== undefined && !decL2.includes("\uFFFD")) {
406
+ let footerL = "";
407
+ if (isMidL) {
408
+ const candsStrL = chardetCandsL.map((c) => `${c.encoding} ${c.confidence}`).join(", ");
409
+ footerL = `\n\n[Auto-guessed: ${topL.encoding} ${topL.confidence}, candidates: ${candsStrL} — re-read with read({encoding}) if garbled]`;
410
+ }
411
+ const infoL2 = await fileSnap(absolutePath).catch(() => undefined);
412
+ setEncodingState(absolutePath, { encoding: topL.encoding, hasBOM: false, version: infoL2?.snapshotId });
413
+ if (footerL)
414
+ setAutoGuessFooter(absolutePath, footerL);
415
+ return decL2;
416
+ }
417
+ }
418
+ }
419
+ catch { } // biome-ignore: best-effort
420
+ const candidates = top3Candidates(bytes, cfgL.supportedEncodings);
421
+ if (candidates.length > 0) {
422
+ const best = candidates[0];
423
+ const candsStrHeuL = candidates.map((c) => `${c.encoding} ${c.score.toFixed(0)}`).join(", ");
424
+ const footerHeuL = `\n\n[Auto-guessed: ${best.encoding} ${best.score.toFixed(0)}, candidates: ${candsStrHeuL} — re-read with read({encoding}) if garbled]`;
425
+ const dec = decodeBytes(bytes, best.encoding);
426
+ if (dec !== undefined) {
427
+ const info = await fileSnap(absolutePath).catch(() => undefined);
428
+ setEncodingState(absolutePath, { encoding: best.encoding, hasBOM: false, version: info?.snapshotId });
429
+ if (footerHeuL)
430
+ setAutoGuessFooter(absolutePath, footerHeuL);
431
+ return dec;
432
+ }
433
+ }
434
+ }
435
+ }
436
+ catch { } // biome-ignore: best-effort
175
437
  return readFile(absolutePath, "utf-8");
176
438
  },
177
439
  async writeText(absolutePath, content, signal, _exec, _sandboxPolicy) {
package/lib/fs-write.js CHANGED
@@ -24,11 +24,13 @@ async function sweepStaleTemps(dir) {
24
24
  await rm(tempPath, { force: true });
25
25
  }
26
26
  }
27
- catch {
27
+ catch (error) {
28
+ console.error("[fs-write] failed to inspect stale temp", error);
28
29
  }
29
30
  }
30
31
  }
31
- catch {
32
+ catch (error) {
33
+ console.error("[fs-write] failed to sweep stale temps", error);
32
34
  }
33
35
  }
34
36
  async function syncDir(dir) {
@@ -43,7 +45,8 @@ async function syncDir(dir) {
43
45
  await handle.close();
44
46
  }
45
47
  }
46
- catch {
48
+ catch (error) {
49
+ console.error("[fs-write] failed to sync directory", error);
47
50
  }
48
51
  }
49
52
  export async function writeAtomic(path, content) {
@@ -78,7 +81,9 @@ export async function writeAtomic(path, content) {
78
81
  try {
79
82
  await rm(tempPath, { force: true });
80
83
  }
81
- catch { }
84
+ catch (error) {
85
+ console.error("[fs-write] failed to remove temp file", error);
86
+ }
82
87
  throw error;
83
88
  }
84
89
  try {
@@ -96,13 +101,17 @@ export async function writeAtomic(path, content) {
96
101
  try {
97
102
  await rm(tempPath, { force: true });
98
103
  }
99
- catch { }
104
+ catch (error) {
105
+ console.error("[fs-write] failed to remove temp file", error);
106
+ }
100
107
  }
101
108
  }
102
109
  try {
103
110
  await rm(tempPath, { force: true });
104
111
  }
105
- catch { }
112
+ catch (error) {
113
+ console.error("[fs-write] failed to remove temp file", error);
114
+ }
106
115
  throw error;
107
116
  }
108
117
  }
package/lib/hash-store.js CHANGED
@@ -56,6 +56,7 @@ function cacheKey(checksum) {
56
56
  return `${CANON_VERSION}:${checksum}`;
57
57
  }
58
58
  /** Load the store as served persistence — internal, for SessionView only. */
59
+ // SAFETY: loadHashStore returns InternalHashStore (HashStore & ServedPersistence) — cast narrows to served view, validated via ServedPersistence interface; safe because InternalHashStore extends both.
59
60
  export function loadServedStore(cwd) {
60
61
  return loadHashStore(cwd);
61
62
  }
@@ -109,7 +110,6 @@ function openDbWithBusyRetry(storePath) {
109
110
  const stores = new Map();
110
111
  const openings = new Map();
111
112
  setStoresGetter(() => stores, () => openings);
112
- let exitHandlerRegistered = false;
113
113
  function openDb(storePath) {
114
114
  const db = new DatabaseSync(storePath, {
115
115
  timeout: HASH_STORE_BUSY_TIMEOUT,