dsh-better-edit 0.5.1 → 0.6.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.
- package/CHANGELOG.md +11 -0
- package/lib/edit-engine.js +4 -454
- package/lib/edit-response.js +24 -2
- package/lib/file-encoding-state.js +263 -0
- package/lib/fs-bridge.js +94 -251
- package/lib/hash-store.js +1 -1
- package/lib/hashline/anchor-pipeline.js +19 -165
- package/lib/index.js +2 -2
- package/lib/mutation/engine.js +388 -0
- package/lib/mutation.js +84 -11
- package/lib/paths.js +1 -1
- package/lib/session-view.js +16 -49
- package/lib/store/index.js +28 -0
- package/lib/store-lifecycle.js +1 -1
- package/lib/tool-edit.js +2 -2
- package/lib/tool-read.js +2 -2
- package/lib/tool-undo.js +2 -2
- package/lib/types/edit-diff.d.ts +1 -1
- package/lib/types/edit-engine.d.ts +4 -176
- package/lib/types/edit-response.d.ts +1 -1
- package/lib/types/file-encoding-state.d.ts +77 -0
- package/lib/types/file-view.d.ts +1 -1
- package/lib/types/fs-bridge.d.ts +4 -11
- package/lib/types/hashline/anchor-pipeline.d.ts +6 -20
- package/lib/types/hashline/index.d.ts +1 -1
- package/lib/types/mutation/engine.d.ts +143 -0
- package/lib/types/mutation.d.ts +30 -5
- package/lib/types/paths.d.ts +1 -1
- package/lib/types/read-and-serve.d.ts +1 -1
- package/lib/types/session-view.d.ts +16 -30
- package/lib/types/store/index.d.ts +25 -0
- package/lib/types/workspace-context.d.ts +23 -0
- package/lib/workspace-context.js +44 -0
- package/lib/write-hook.js +2 -1
- package/package.json +1 -1
package/lib/fs-bridge.js
CHANGED
|
@@ -17,9 +17,14 @@
|
|
|
17
17
|
import { readFile } from "node:fs/promises";
|
|
18
18
|
import { writeAtomic } from "./fs-write.js";
|
|
19
19
|
import { loadConfig } from "./store-config.js";
|
|
20
|
-
import { detectBom, isValidUtf8, decodeBytes, encodeText, normalizeEncoding, top3Candidates, isSupportedEncoding, detectWithChardet, chardetTop3Candidates, getTop3Candidates } from "./encoding.js";
|
|
21
20
|
import { fileSnap } from "./file-reader.js";
|
|
22
21
|
import { resolveTarget, toCwd } from "./paths.js";
|
|
22
|
+
import { decodeForOpen, recordOpenState, recordFooter, getEncodingState as _getEncodingState, setEncodingState as _setEncodingState, clearEncodingState as _clearEncodingState, getAutoGuessFooter as _getAutoGuessFooter, setAutoGuessFooter as _setAutoGuessFooter, clearAutoGuessFooter as _clearAutoGuessFooter, invalidateIfStale, prepareForSave, } from "./file-encoding-state.js";
|
|
23
|
+
export { _getEncodingState as getEncodingState, _setEncodingState as setEncodingState, _clearEncodingState as clearEncodingState, _getAutoGuessFooter as getAutoGuessFooter, _setAutoGuessFooter as setAutoGuessFooter, _clearAutoGuessFooter as clearAutoGuessFooter, };
|
|
24
|
+
// Keep alias for tests that import getAutoGuessFooter via fs-bridge
|
|
25
|
+
export function getAutoGuessFooterCompat(k) {
|
|
26
|
+
return _getAutoGuessFooter(k);
|
|
27
|
+
}
|
|
23
28
|
/**
|
|
24
29
|
* Map an `ctx.fs` failure onto the hashline model-facing vocabulary so the
|
|
25
30
|
* model sees the same structured error codes as the pure pipeline.
|
|
@@ -30,7 +35,6 @@ import { resolveTarget, toCwd } from "./paths.js";
|
|
|
30
35
|
export function mapFsError(error, displayPath) {
|
|
31
36
|
if (error instanceof Error &&
|
|
32
37
|
typeof error.code === "string") {
|
|
33
|
-
// SAFETY: `error` narrowed to Error with string `code` above — typed extraction for FS code switch
|
|
34
38
|
const code = error.code;
|
|
35
39
|
if (code === "FS_NOT_FOUND") {
|
|
36
40
|
throw new Error(`[E_NOT_FOUND] File not found: ${displayPath}`);
|
|
@@ -62,35 +66,6 @@ export function mapFsError(error, displayPath) {
|
|
|
62
66
|
const UTF8_BOM = "\uFEFF";
|
|
63
67
|
const UTF8_BOM_BYTES = [0xef, 0xbb, 0xbf];
|
|
64
68
|
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
|
-
}
|
|
94
69
|
/**
|
|
95
70
|
* Restore a UTF-8 BOM consumed by a backend's {@link TextDecoder}. The raw
|
|
96
71
|
* byte seam reads whole files rather than prefixes, so first use stat size to
|
|
@@ -136,194 +111,100 @@ export function ctxFsIO(fs, ctx) {
|
|
|
136
111
|
return fs.processPath(target);
|
|
137
112
|
},
|
|
138
113
|
async readText(absolutePath, signal, encodingHint) {
|
|
114
|
+
// explicit encoding override (Reopen with Encoding) — bytes + seam
|
|
115
|
+
if (encodingHint) {
|
|
116
|
+
const target = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
|
|
117
|
+
const info = await fs.stat(target, signal);
|
|
118
|
+
const maxBytes = info?.size ?? 10 * 1024 * 1024;
|
|
119
|
+
const bytes = await fs.readBytes(target, signal, maxBytes);
|
|
120
|
+
const cfg = loadConfig();
|
|
121
|
+
const decoded = await decodeForOpen(bytes, cfg, { encodingHint, displayPath: absolutePath });
|
|
122
|
+
const targetKey = String(target.targetKey ?? absolutePath);
|
|
123
|
+
recordOpenState(targetKey, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
|
|
124
|
+
if (decoded.footer)
|
|
125
|
+
recordFooter(targetKey, decoded.footer);
|
|
126
|
+
return decoded.text;
|
|
127
|
+
}
|
|
139
128
|
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
|
-
}
|
|
166
129
|
const target = await fs.resolve(absolutePath, {
|
|
167
130
|
...(signal === undefined ? {} : { signal }),
|
|
168
131
|
});
|
|
169
132
|
const text = await fs.readText(target, signal);
|
|
170
|
-
|
|
133
|
+
const restored = await restoreStrippedUtf8Bom(fs, target, text, signal);
|
|
134
|
+
// record file encoding state for round-trip (BOM + lineEnding)
|
|
135
|
+
// Do not probe fs.stat when BOM is already preserved — keep the no-probe guarantee
|
|
136
|
+
// tested in fs-bridge.policy.test.ts (keeps a BOM already preserved without probing)
|
|
137
|
+
try {
|
|
138
|
+
const targetKey = String(target.targetKey ?? absolutePath);
|
|
139
|
+
const hasBOM = restored.startsWith(UTF8_BOM);
|
|
140
|
+
const clean = hasBOM ? restored.slice(1) : restored;
|
|
141
|
+
// use seam to record with correct lineEnding detection; version left undefined
|
|
142
|
+
// to avoid extra stat (the success path must stay probe-free when BOM preserved)
|
|
143
|
+
recordOpenState(targetKey, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, undefined);
|
|
144
|
+
// clear any stale autoGuess footer on clean UTF-8 read
|
|
145
|
+
_clearAutoGuessFooter(targetKey);
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
// best-effort
|
|
149
|
+
}
|
|
150
|
+
return restored;
|
|
171
151
|
}
|
|
172
152
|
catch (error) {
|
|
173
|
-
// fallback for non-UTF8 when autoGuess enabled; when disabled surface top-3 + hint
|
|
174
153
|
const isNotText = error instanceof Error && error.code === "FS_NOT_TEXT";
|
|
175
154
|
if (isNotText) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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
|
-
}
|
|
155
|
+
const cfg = loadConfig();
|
|
156
|
+
const target = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
|
|
157
|
+
const info = await fs.stat(target, signal);
|
|
158
|
+
const maxBytes = info?.size ?? 10 * 1024 * 1024;
|
|
159
|
+
if (info && info.size !== undefined && info.size > maxBytes)
|
|
160
|
+
throw error;
|
|
161
|
+
const bytes = await fs.readBytes(target, signal, maxBytes);
|
|
162
|
+
const targetKey = String(target.targetKey ?? absolutePath);
|
|
163
|
+
// Delegate deterministic admission to the deep seam
|
|
164
|
+
const decoded = await decodeForOpen(bytes, cfg, { displayPath: absolutePath });
|
|
165
|
+
recordOpenState(targetKey, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
|
|
166
|
+
if (decoded.footer)
|
|
167
|
+
recordFooter(targetKey, decoded.footer);
|
|
168
|
+
return decoded.text;
|
|
264
169
|
}
|
|
265
170
|
return mapFsError(error, absolutePath);
|
|
266
171
|
}
|
|
267
172
|
},
|
|
268
|
-
async writeText(absolutePath, content, signal, exec, sandboxPolicy
|
|
269
|
-
// handle drift invalidation before encode
|
|
173
|
+
async writeText(absolutePath, content, signal, exec, sandboxPolicy) {
|
|
174
|
+
// handle drift invalidation before encode — seam owns version check
|
|
270
175
|
try {
|
|
271
176
|
const targetTmp = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
|
|
272
177
|
const key = String(targetTmp.targetKey ?? absolutePath);
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
const info = await fs.stat(targetTmp, signal);
|
|
276
|
-
const curVer = info?.version;
|
|
277
|
-
if (curVer !== memo.version)
|
|
278
|
-
encodingMemo.delete(key);
|
|
279
|
-
}
|
|
178
|
+
const info = await fs.stat(targetTmp, signal).catch(() => undefined);
|
|
179
|
+
invalidateIfStale(key, info?.version);
|
|
280
180
|
}
|
|
281
181
|
catch { } // biome-ignore: best-effort
|
|
282
182
|
try {
|
|
283
183
|
const target = await fs.resolve(absolutePath, {
|
|
284
184
|
...(signal === undefined ? {} : { signal }),
|
|
285
185
|
});
|
|
286
|
-
// Single-slot decision: the observation policy produces
|
|
287
|
-
// createIfAbsent / replaceIfVersion; the bare default is
|
|
288
|
-
// undefined (unconditional) when no policy is mounted.
|
|
289
186
|
const intent = await ctx.waterfall("fs/write-intent", target, exec, () => undefined);
|
|
290
|
-
//
|
|
291
|
-
//
|
|
292
|
-
//
|
|
293
|
-
// session workspace under workspace-write.
|
|
294
|
-
// re-encode if needed (round-trip unless normalizeToUtf8)
|
|
295
|
-
const _writeContent = content; // keep for future use
|
|
187
|
+
// Seam owns Save-with-Encoding memo update and normalizeToUtf8 check.
|
|
188
|
+
// Provider expects UTF-8 string; for legacy without normalize we keep
|
|
189
|
+
// string as-is (next read re-detects via version bump).
|
|
296
190
|
try {
|
|
297
191
|
const cfgW = loadConfig();
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
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
|
-
}
|
|
192
|
+
const key = String(target.targetKey ?? absolutePath);
|
|
193
|
+
// Honor manual override if content was produced via write({encoding}) — not wired here,
|
|
194
|
+
// encodingHint is not part of writeText signature for ctxFsIO in current call sites.
|
|
195
|
+
// Check existing memo for legacy preservation.
|
|
196
|
+
const existing = _getEncodingState(key);
|
|
197
|
+
const prep = prepareForSave(content, existing, { normalizeToUtf8: cfgW.normalizeToUtf8 });
|
|
198
|
+
// For Save with Encoding hint, prepareForSave would create newState — not used in this path (tool-undo/write-hook carry hint separately)
|
|
199
|
+
if (prep.newState)
|
|
200
|
+
_setEncodingState(key, prep.newState);
|
|
201
|
+
// content stays as UTF-8 string for provider; prep.textToWrite is same as content
|
|
316
202
|
}
|
|
317
203
|
catch { } // biome-ignore: best-effort
|
|
318
204
|
const outcome = await fs.writeText(target, content, intent, signal, sandboxPolicy);
|
|
319
|
-
// Record the present observation (a no-op when no policy
|
|
320
|
-
// plugin listens), so later built-in tools see the new version.
|
|
321
205
|
ctx.emit("fs/observed", target, { kind: "present", version: outcome.version }, exec);
|
|
322
206
|
}
|
|
323
207
|
catch (error) {
|
|
324
|
-
// FS_SANDBOX_DENIED passes through raw; the tool layer maps it
|
|
325
|
-
// to the shared [sandbox: …] marker + escalation hint via its
|
|
326
|
-
// sandbox controller.
|
|
327
208
|
return mapFsError(error, absolutePath);
|
|
328
209
|
}
|
|
329
210
|
},
|
|
@@ -338,7 +219,6 @@ export function ctxFsIO(fs, ctx) {
|
|
|
338
219
|
}
|
|
339
220
|
}
|
|
340
221
|
catch (error) {
|
|
341
|
-
// A failed observation must not fail the read that preceded it.
|
|
342
222
|
console.error(`dsh-better-edit: fs/observed emission failed for ${absolutePath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
343
223
|
}
|
|
344
224
|
},
|
|
@@ -365,79 +245,42 @@ export function localIO() {
|
|
|
365
245
|
async readText(absolutePath, signal, encodingHint) {
|
|
366
246
|
signal?.throwIfAborted();
|
|
367
247
|
if (encodingHint) {
|
|
368
|
-
const norm = normalizeEncoding(encodingHint);
|
|
369
|
-
if (!norm)
|
|
370
|
-
throw new Error(`[E_BAD_ENCODING] Unknown encoding: ${encodingHint}`);
|
|
371
248
|
const bytes = await readFile(absolutePath);
|
|
372
|
-
const
|
|
373
|
-
const
|
|
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}`);
|
|
249
|
+
const cfg = loadConfig();
|
|
250
|
+
const decoded = await decodeForOpen(bytes, cfg, { encodingHint, displayPath: absolutePath });
|
|
377
251
|
const info = await fileSnap(absolutePath).catch(() => undefined);
|
|
378
|
-
|
|
379
|
-
|
|
252
|
+
recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.snapshotId);
|
|
253
|
+
if (decoded.footer)
|
|
254
|
+
recordFooter(absolutePath, decoded.footer);
|
|
255
|
+
return decoded.text;
|
|
380
256
|
}
|
|
381
|
-
//
|
|
257
|
+
// Deterministic path via seam, with local fallback for E_NOT_TEXT when autoGuess off
|
|
382
258
|
try {
|
|
383
259
|
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
260
|
const cfgL = loadConfig();
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
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
|
-
}
|
|
261
|
+
const decoded = await decodeForOpen(bytes, cfgL, { displayPath: absolutePath });
|
|
262
|
+
const info = await fileSnap(absolutePath).catch(() => undefined);
|
|
263
|
+
recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.snapshotId);
|
|
264
|
+
if (decoded.footer)
|
|
265
|
+
recordFooter(absolutePath, decoded.footer);
|
|
266
|
+
return decoded.text;
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
// Local fallback: when autoGuess off, E_NOT_TEXT should fall back to raw UTF-8 with � (no throw)
|
|
270
|
+
if (error instanceof Error && error.message.includes("[E_NOT_TEXT]")) {
|
|
271
|
+
return readFile(absolutePath, "utf-8");
|
|
434
272
|
}
|
|
273
|
+
throw error;
|
|
435
274
|
}
|
|
436
|
-
catch { } // biome-ignore: best-effort
|
|
437
|
-
return readFile(absolutePath, "utf-8");
|
|
438
275
|
},
|
|
439
276
|
async writeText(absolutePath, content, signal, _exec, _sandboxPolicy) {
|
|
440
277
|
signal?.throwIfAborted();
|
|
278
|
+
// Invalidate stale memo before write, and handle lineEnding via seam if needed
|
|
279
|
+
try {
|
|
280
|
+
const info = await fileSnap(absolutePath).catch(() => undefined);
|
|
281
|
+
invalidateIfStale(absolutePath, info?.snapshotId);
|
|
282
|
+
}
|
|
283
|
+
catch { }
|
|
441
284
|
await writeAtomic(absolutePath, content);
|
|
442
285
|
},
|
|
443
286
|
async emitObserved() {
|
package/lib/hash-store.js
CHANGED
|
@@ -19,7 +19,7 @@ import { dirname, join } from "node:path";
|
|
|
19
19
|
import { DatabaseSync } from "node:sqlite";
|
|
20
20
|
import { hashStorePath } from "./store-tenancy.js";
|
|
21
21
|
import { onStoreOpen, setStoresGetter } from "./store-lifecycle.js";
|
|
22
|
-
import { workspaceCwd } from "./workspace.js";
|
|
22
|
+
import { workspaceCwd } from "./workspace-context.js";
|
|
23
23
|
import { errCode, splitLines } from "./utils.js";
|
|
24
24
|
import { initHasher, contentChecksum, HASH_RE, CANON_VERSION, } from "./hashline/hash-assign.js";
|
|
25
25
|
import { HASH_STORE_VERSION, HASH_STORE_BUSY_TIMEOUT, SERVED_TTL_MS, } from "./constants.js";
|