dsh-better-edit 0.7.1 → 0.8.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +50 -25
  2. package/README.md +72 -66
  3. package/README.zh.md +83 -83
  4. package/lib/canonical-path.js +4 -1
  5. package/lib/contract.js +67 -22
  6. package/lib/edit-diff.js +1 -5
  7. package/lib/encoding.js +22 -5
  8. package/lib/file-encoding-state.js +50 -11
  9. package/lib/file-view.js +28 -41
  10. package/lib/fs-bridge.js +59 -23
  11. package/lib/fs-write.js +1 -1
  12. package/lib/guidance/materialize.js +1 -6
  13. package/lib/guidance/resolve.js +1 -1
  14. package/lib/hash-store.js +12 -20
  15. package/lib/hashline/anchor-pipeline.js +29 -29
  16. package/lib/hashline/hash-assign.js +7 -4
  17. package/lib/hashline/hash.js +3 -3
  18. package/lib/index.js +6 -0
  19. package/lib/mutation/engine.js +11 -14
  20. package/lib/mutation.js +7 -6
  21. package/lib/prompts.js +9 -8
  22. package/lib/read-and-serve.js +22 -11
  23. package/lib/render-text-warning.js +21 -0
  24. package/lib/sandbox.js +2 -5
  25. package/lib/self-heal.js +30 -27
  26. package/lib/session-view.js +5 -1
  27. package/lib/store-config.js +7 -2
  28. package/lib/store-lifecycle.js +7 -2
  29. package/lib/tool-edit.js +5 -2
  30. package/lib/tool-read.js +6 -9
  31. package/lib/tool-str-replace-editor.js +274 -0
  32. package/lib/tool-undo.js +2 -3
  33. package/lib/types/contract.d.ts +104 -17
  34. package/lib/types/file-encoding-state.d.ts +3 -0
  35. package/lib/types/file-view.d.ts +1 -1
  36. package/lib/types/hashline/index.d.ts +1 -1
  37. package/lib/types/render-text-warning.d.ts +22 -0
  38. package/lib/types/self-heal.d.ts +2 -0
  39. package/lib/types/tool-str-replace-editor.d.ts +27 -0
  40. package/lib/types/undo-edit.d.ts +1 -1
  41. package/lib/types/write-hook.d.ts +2 -2
  42. package/lib/undo-edit.js +6 -8
  43. package/lib/utils.js +1 -2
  44. package/lib/workspace-context.js +1 -1
  45. package/lib/write-hook.js +19 -22
  46. package/package.json +63 -57
@@ -14,8 +14,8 @@
14
14
  *
15
15
  * @module dsh-better-edit/file-encoding-state
16
16
  */
17
- import { detectBom, isValidUtf8, decodeBytes, normalizeEncoding, top3Candidates, chardetTop3Candidates } from "./encoding.js";
18
- import { detectEnding } from "./edit-diff.js";
17
+ import { detectBom, isValidUtf8, decodeBytes, normalizeEncoding, top3Candidates, chardetTop3Candidates, } from "./encoding.js";
18
+ import { detectEnding, restoreEndings, toLF } from "./edit-diff.js";
19
19
  const encodingMemo = new Map();
20
20
  const autoGuessFooterMemo = new Map();
21
21
  export function getEncodingState(targetKey) {
@@ -52,19 +52,29 @@ export function invalidateIfStale(targetKey, currentVersion) {
52
52
  // Pure helpers — no IO
53
53
  // ---------------------------------------------------------------------------
54
54
  export function buildTop3ErrorMessage(displayPath, candidates) {
55
- const candStr = candidates.map((c) => `${c.encoding}("${c.sample.slice(0, 20).replace(/"/g, "'")}")`).join(", ");
55
+ const candStr = candidates
56
+ .map((c) => `${c.encoding}("${c.sample.slice(0, 20).replace(/"/g, "'")}")`)
57
+ .join(", ");
56
58
  return `[MODEL] [E_UNSUPPORTED_FILE] Path is not a readable UTF-8 text file: ${displayPath}. 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.`;
57
59
  }
58
60
  function buildAutoGuessFooterFromCandidates(candidates) {
59
61
  // chardet path vs heuristic path share same footer shape, just score vs confidence
60
62
  const candsStr = candidates
61
63
  .map((c) => {
62
- const score = c.confidence !== undefined ? String(c.confidence) : c.score !== undefined ? c.score.toFixed(0) : "0";
64
+ const score = c.confidence !== undefined
65
+ ? String(c.confidence)
66
+ : c.score !== undefined
67
+ ? c.score.toFixed(0)
68
+ : "0";
63
69
  return `${c.encoding} ${score}`;
64
70
  })
65
71
  .join(", ");
66
72
  const top = candidates[0];
67
- const topScore = top.confidence !== undefined ? String(top.confidence) : top.score !== undefined ? top.score.toFixed(0) : "0";
73
+ const topScore = top.confidence !== undefined
74
+ ? String(top.confidence)
75
+ : top.score !== undefined
76
+ ? top.score.toFixed(0)
77
+ : "0";
68
78
  return `\n\n[Auto-guessed: ${top.encoding} ${topScore}, candidates: ${candsStr} — re-read with read({encoding}) if garbled]`;
69
79
  }
70
80
  function isMidConfidenceChardet(top, second) {
@@ -95,7 +105,12 @@ export async function decodeForOpen(bytes, config, opts = {}) {
95
105
  const slice = bom ? bytes.subarray(off) : bytes;
96
106
  // For BOM encodings, decode the full bytes (including BOM) via helper; otherwise hint
97
107
  let decoded;
98
- if (bom && (bom.encoding === "utf8bom" || bom.encoding === "utf16le" || bom.encoding === "utf16be" || bom.encoding === "utf32le" || bom.encoding === "utf32be")) {
108
+ if (bom &&
109
+ (bom.encoding === "utf8bom" ||
110
+ bom.encoding === "utf16le" ||
111
+ bom.encoding === "utf16be" ||
112
+ bom.encoding === "utf32le" ||
113
+ bom.encoding === "utf32be")) {
99
114
  // When hint differs from BOM, hint takes precedence for decoding the slice
100
115
  // but we preserve hasBOM from BOM sniff. For utf variants, BOM encoding
101
116
  // already handled; for explicit hint we decode slice with hint.
@@ -166,7 +181,11 @@ export async function decodeForOpen(bytes, config, opts = {}) {
166
181
  hasBOM: false,
167
182
  lineEnding: detectEnding(dec),
168
183
  footer,
169
- candidates: chardetCands.map((c) => ({ encoding: c.encoding, sample: c.sample, score: c.confidence })),
184
+ candidates: chardetCands.map((c) => ({
185
+ encoding: c.encoding,
186
+ sample: c.sample,
187
+ score: c.confidence,
188
+ })),
170
189
  };
171
190
  }
172
191
  }
@@ -204,7 +223,11 @@ export async function decodeForOpen(bytes, config, opts = {}) {
204
223
  try {
205
224
  const chardetCands = await chardetTop3Candidates(bytes, config.supportedEncodings);
206
225
  if (chardetCands.length > 0) {
207
- candidates = chardetCands.map((c) => ({ encoding: c.encoding, sample: c.sample, score: c.confidence }));
226
+ candidates = chardetCands.map((c) => ({
227
+ encoding: c.encoding,
228
+ sample: c.sample,
229
+ score: c.confidence,
230
+ }));
208
231
  }
209
232
  else {
210
233
  candidates = top3Candidates(bytes, config.supportedEncodings);
@@ -233,6 +256,9 @@ export function recordFooter(targetKey, footer) {
233
256
  if (footer)
234
257
  setAutoGuessFooter(targetKey, footer);
235
258
  }
259
+ /** Shared UTF-8 BOM marker — single source for the BOM round-trip seam.
260
+ * Prefer `prepareForSave` / `stripBOM` over re-implementing this check. */
261
+ export const UTF8_BOM = "\uFEFF";
236
262
  /**
237
263
  * Decide what bytes (as text, since provider expects string) to write and
238
264
  * what new state to record. BOM and lineEnding are restored here when a
@@ -253,11 +279,24 @@ export function prepareForSave(content, existingState, opts = {}) {
253
279
  newState: { encoding: norm, hasBOM, lineEnding, version: undefined },
254
280
  };
255
281
  }
256
- if (existingState && !opts.normalizeToUtf8 && existingState.encoding !== "utf8" && existingState.encoding !== "utf8bom") {
282
+ if (existingState &&
283
+ !opts.normalizeToUtf8 &&
284
+ existingState.encoding !== "utf8" &&
285
+ existingState.encoding !== "utf8bom") {
257
286
  // Legacy file, normalize false — preserve memo on wire (no transcode),
258
287
  // provider writes UTF-8 string, version bump will invalidate on next read.
259
288
  return { textToWrite: content };
260
289
  }
261
- // Default: UTF-8 without BOM, preserve lineEnding if known
262
- return { textToWrite: content };
290
+ // Default: restore recorded BOM + lineEnding via the shared seam so
291
+ // governed writers (e.g. the str_replace_editor shadow) don't re-implement
292
+ // the round-trip locally and diverge on future utf16le/lineEnding evolution.
293
+ // Only add, never strip: a missing memo means UTF-8 without BOM + LF.
294
+ let textToWrite = content;
295
+ if (existingState?.lineEnding) {
296
+ textToWrite = restoreEndings(toLF(textToWrite), existingState.lineEnding);
297
+ }
298
+ if (existingState?.hasBOM && !textToWrite.startsWith(UTF8_BOM)) {
299
+ textToWrite = `${UTF8_BOM}${textToWrite}`;
300
+ }
301
+ return { textToWrite };
263
302
  }
package/lib/file-view.js CHANGED
@@ -33,8 +33,8 @@ export const DEFAULT_MAX_BYTES = 50 * 1024;
33
33
  function splitLinesForCounting(content) {
34
34
  if (content.length === 0)
35
35
  return [];
36
- const lines = content.split('\n');
37
- if (content.endsWith('\n'))
36
+ const lines = content.split("\n");
37
+ if (content.endsWith("\n"))
38
38
  lines.pop();
39
39
  return lines;
40
40
  }
@@ -48,7 +48,7 @@ export function formatSize(bytes) {
48
48
  export function truncateHead(content, options = {}) {
49
49
  const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
50
50
  const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
51
- const totalBytes = Buffer.byteLength(content, 'utf-8');
51
+ const totalBytes = Buffer.byteLength(content, "utf-8");
52
52
  const lines = splitLinesForCounting(content);
53
53
  const totalLines = lines.length;
54
54
  if (totalLines <= maxLines && totalBytes <= maxBytes) {
@@ -66,12 +66,12 @@ export function truncateHead(content, options = {}) {
66
66
  maxBytes,
67
67
  };
68
68
  }
69
- const firstLineBytes = Buffer.byteLength(lines[0] ?? '', 'utf-8');
69
+ const firstLineBytes = Buffer.byteLength(lines[0] ?? "", "utf-8");
70
70
  if (firstLineBytes > maxBytes) {
71
71
  return {
72
- content: '',
72
+ content: "",
73
73
  truncated: true,
74
- truncatedBy: 'bytes',
74
+ truncatedBy: "bytes",
75
75
  totalLines,
76
76
  totalBytes,
77
77
  outputLines: 0,
@@ -84,22 +84,22 @@ export function truncateHead(content, options = {}) {
84
84
  }
85
85
  const outputLinesArr = [];
86
86
  let outputBytesCount = 0;
87
- let truncatedBy = 'lines';
87
+ let truncatedBy = "lines";
88
88
  for (let i = 0; i < lines.length && i < maxLines; i++) {
89
89
  const line = lines[i];
90
- const lineBytes = Buffer.byteLength(line, 'utf-8') + (i > 0 ? 1 : 0);
90
+ const lineBytes = Buffer.byteLength(line, "utf-8") + (i > 0 ? 1 : 0);
91
91
  if (outputBytesCount + lineBytes > maxBytes) {
92
- truncatedBy = 'bytes';
92
+ truncatedBy = "bytes";
93
93
  break;
94
94
  }
95
95
  outputLinesArr.push(line);
96
96
  outputBytesCount += lineBytes;
97
97
  }
98
98
  if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
99
- truncatedBy = 'lines';
99
+ truncatedBy = "lines";
100
100
  }
101
- const outputContent = outputLinesArr.join('\n');
102
- const finalOutputBytes = Buffer.byteLength(outputContent, 'utf-8');
101
+ const outputContent = outputLinesArr.join("\n");
102
+ const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
103
103
  return {
104
104
  content: outputContent,
105
105
  truncated: true,
@@ -115,12 +115,7 @@ export function truncateHead(content, options = {}) {
115
115
  };
116
116
  }
117
117
  // --- File kind (from file-kind.ts, private) ---
118
- const IMG_TYPES = new Set([
119
- "image/jpeg",
120
- "image/png",
121
- "image/gif",
122
- "image/webp",
123
- ]);
118
+ const IMG_TYPES = new Set(["image/jpeg", "image/png", "image/gif", "image/webp"]);
124
119
  const TEXT_TYPES = new Set([
125
120
  "application/rtf",
126
121
  "application/xml",
@@ -181,8 +176,7 @@ export async function loadFileKindAndText(filePath, options) {
181
176
  };
182
177
  }
183
178
  const detectedMimeType = (await fileTypeFromBuffer(sample))?.mime;
184
- if (detectedMimeType !== undefined &&
185
- !isTextType(detectedMimeType)) {
179
+ if (detectedMimeType !== undefined && !isTextType(detectedMimeType)) {
186
180
  if (IMG_TYPES.has(detectedMimeType)) {
187
181
  return { kind: "image", mimeType: detectedMimeType };
188
182
  }
@@ -338,17 +332,16 @@ function normPosInt(value, name) {
338
332
  return value;
339
333
  }
340
334
  export function formatPaginationHint(startLine, endLine, totalLines, nextOffset, byteLimit) {
341
- const sizeSuffix = byteLimit !== undefined ? ` (${formatSize(byteLimit)} limit)` : '';
335
+ const sizeSuffix = byteLimit !== undefined ? ` (${formatSize(byteLimit)} limit)` : "";
342
336
  return `[Showing lines ${startLine}-${endLine} of ${totalLines}${sizeSuffix}. Use offset=${nextOffset} to continue.]`;
343
337
  }
344
338
  export async function fmtReadPreview(text, options, precomputedHashes, path, maxLineBytes = MAX_READ_LINE_BYTES, maxTruncLines = DEFAULT_MAX_LINES) {
345
339
  const allLines = visLines(text);
346
340
  const totalLines = allLines.length;
347
- const startLine = normPosInt(options.offset, 'offset') ?? 1;
341
+ const startLine = normPosInt(options.offset, "offset") ?? 1;
348
342
  if (totalLines === 0) {
349
343
  if (startLine === 1) {
350
- const allHashes = precomputedHashes ??
351
- (await (path ? lineHashes(text, path) : lineHashes(text)));
344
+ const allHashes = precomputedHashes ?? (await (path ? lineHashes(text, path) : lineHashes(text)));
352
345
  const emptyLineHash = allHashes[0];
353
346
  return {
354
347
  text: `${emptyLineHash}${HASH_SEP}\n[File is empty. Use edit to insert content.]`,
@@ -366,43 +359,37 @@ export async function fmtReadPreview(text, options, precomputedHashes, path, max
366
359
  served: [],
367
360
  };
368
361
  }
369
- const limit = normPosInt(options.limit, 'limit');
370
- const endIdx = limit
371
- ? Math.min(startLine - 1 + limit, totalLines)
372
- : totalLines;
362
+ const limit = normPosInt(options.limit, "limit");
363
+ const endIdx = limit ? Math.min(startLine - 1 + limit, totalLines) : totalLines;
373
364
  const selected = allLines.slice(startLine - 1, endIdx);
374
- const allHashes = precomputedHashes ??
375
- (await (path ? lineHashes(text, path) : lineHashes(text)));
365
+ const allHashes = precomputedHashes ?? (await (path ? lineHashes(text, path) : lineHashes(text)));
376
366
  const selectedHashes = allHashes.slice(startLine - 1, endIdx);
377
367
  const formatted = fmtRegion(selectedHashes, selected);
378
368
  const maxBytes = maxLineBytes;
379
369
  const rowSizes = selected.map((line, index) => ({
380
370
  lineNumber: startLine + index,
381
- bytes: Buffer.byteLength(`${selectedHashes[index]}${HASH_SEP}${line}`, 'utf-8'),
371
+ bytes: Buffer.byteLength(`${selectedHashes[index]}${HASH_SEP}${line}`, "utf-8"),
382
372
  }));
383
373
  if (rowSizes.some((row) => row.bytes > maxBytes)) {
384
374
  const oversized = rowSizes.filter((row) => row.bytes > maxBytes);
385
375
  const rows = rowSizes.map((row, index) => row.bytes > maxBytes
386
376
  ? `[Line ${row.lineNumber} is ${formatSize(row.bytes)}, exceeds ${formatSize(maxBytes)}; content not shown. Use bash: sed -n '${row.lineNumber}p' <path> | head -c ${maxBytes}]`
387
377
  : fmtRegion([selectedHashes[index]], [selected[index]]));
388
- const skippedTruncation = truncateHead(rows.join('\n'), {
378
+ const skippedTruncation = truncateHead(rows.join("\n"), {
389
379
  maxBytes,
390
380
  maxLines: maxTruncLines,
391
381
  });
392
- const shownRowCount = skippedTruncation.content === ''
393
- ? 0
394
- : skippedTruncation.content.split('\n').length;
382
+ const shownRowCount = skippedTruncation.content === "" ? 0 : skippedTruncation.content.split("\n").length;
395
383
  const lastShownLine = shownRowCount > 0 ? startLine + shownRowCount - 1 : startLine - 1;
396
384
  const lineLabel = oversized.length === 1
397
385
  ? `Line ${oversized[0].lineNumber}`
398
- : `Lines ${oversized.map((row) => row.lineNumber).join(', ')}`;
399
- const verb = oversized.length === 1 ? 'exceeds' : 'exceed';
400
- const addresses = oversized.map((row) => `${row.lineNumber}p`).join(';');
386
+ : `Lines ${oversized.map((row) => row.lineNumber).join(", ")}`;
387
+ const verb = oversized.length === 1 ? "exceeds" : "exceed";
388
+ const addresses = oversized.map((row) => `${row.lineNumber}p`).join(";");
401
389
  const warning = `[${lineLabel} ${verb} ${formatSize(maxBytes)}; content not shown because hashline anchors require full lines. Inspect with bash: sed -n '${addresses}' <path> | head -c ${maxBytes}]`;
402
390
  let preview = skippedTruncation.content;
403
391
  let nextOffset;
404
- if (shownRowCount > 0 &&
405
- (skippedTruncation.truncated || lastShownLine < totalLines)) {
392
+ if (shownRowCount > 0 && (skippedTruncation.truncated || lastShownLine < totalLines)) {
406
393
  nextOffset = lastShownLine + 1;
407
394
  preview += `\n\n${warning}\n${formatPaginationHint(startLine, lastShownLine, totalLines, nextOffset, skippedTruncation.truncated ? skippedTruncation.maxBytes : undefined)}`;
408
395
  }
@@ -434,7 +421,7 @@ export async function fmtReadPreview(text, options, precomputedHashes, path, max
434
421
  if (truncation.truncated) {
435
422
  const endLineDisplay = startLine + truncation.outputLines - 1;
436
423
  nextOffset = endLineDisplay + 1;
437
- if (truncation.truncatedBy === 'lines') {
424
+ if (truncation.truncatedBy === "lines") {
438
425
  preview += `\n\n${formatPaginationHint(startLine, endLineDisplay, totalLines, nextOffset)}`;
439
426
  }
440
427
  else {
package/lib/fs-bridge.js CHANGED
@@ -34,8 +34,7 @@ export function getAutoGuessFooterCompat(k) {
34
34
  * @returns the mapped error, rethrown.
35
35
  */
36
36
  export function mapFsError(error, displayPath) {
37
- if (error instanceof Error &&
38
- typeof error.code === "string") {
37
+ if (error instanceof Error && typeof error.code === "string") {
39
38
  const code = error.code;
40
39
  if (code === "FS_NOT_FOUND") {
41
40
  throw new Error(`[MODEL] [E_NOT_FOUND] File not found: ${displayPath}`);
@@ -80,12 +79,16 @@ const UTF8_BOM_LEN = UTF8_BOM_BYTES.length;
80
79
  * https://github.com/Rianico/dsh-better-edit/issues/23
81
80
  */
82
81
  async function restoreStrippedUtf8Bom(fs, target, text, signal) {
83
- if (text.startsWith(UTF8_BOM))
84
- return text;
82
+ // BOM already preserved: one metadata stat for the read-first gate version
83
+ // (issue #69). No byte probe here — readBytes stays forbidden in this branch.
84
+ if (text.startsWith(UTF8_BOM)) {
85
+ const info = await fs.stat(target, signal).catch(() => undefined);
86
+ return { text, version: info?.version };
87
+ }
85
88
  const info = await fs.stat(target, signal);
86
- if (info?.size === undefined ||
87
- info.size !== Buffer.byteLength(text, "utf-8") + UTF8_BOM_LEN) {
88
- return text;
89
+ const version = info?.version;
90
+ if (info?.size === undefined || info.size !== Buffer.byteLength(text, "utf-8") + UTF8_BOM_LEN) {
91
+ return { text, version };
89
92
  }
90
93
  const bytes = await fs.readBytes(target, signal, info.size);
91
94
  const encodedText = Buffer.from(text, "utf-8");
@@ -93,13 +96,13 @@ async function restoreStrippedUtf8Bom(fs, target, text, signal) {
93
96
  bytes[0] !== UTF8_BOM_BYTES[0] ||
94
97
  bytes[1] !== UTF8_BOM_BYTES[1] ||
95
98
  bytes[2] !== UTF8_BOM_BYTES[2]) {
96
- return text;
99
+ return { text, version };
97
100
  }
98
101
  for (let i = 0; i < encodedText.length; i += 1) {
99
102
  if (bytes[i + UTF8_BOM_LEN] !== encodedText[i])
100
- return text;
103
+ return { text, version };
101
104
  }
102
- return `${UTF8_BOM}${text}`;
105
+ return { text: `${UTF8_BOM}${text}`, version };
103
106
  }
104
107
  /** FileIO over the deployment's `ctx.fs` service. */
105
108
  export function ctxFsIO(fs, ctx) {
@@ -114,16 +117,29 @@ export function ctxFsIO(fs, ctx) {
114
117
  async readText(absolutePath, signal, encodingHint) {
115
118
  // explicit encoding override (Reopen with Encoding) — bytes + seam
116
119
  if (encodingHint) {
117
- const target = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
120
+ const target = await fs.resolve(absolutePath, {
121
+ ...(signal === undefined ? {} : { signal }),
122
+ });
118
123
  const info = await fs.stat(target, signal);
119
124
  const maxBytes = info?.size ?? 10 * 1024 * 1024;
120
125
  const bytes = await fs.readBytes(target, signal, maxBytes);
121
126
  const cfg = loadConfig();
122
- const decoded = await decodeForOpen(bytes, cfg, { encodingHint, displayPath: absolutePath });
127
+ const decoded = await decodeForOpen(bytes, cfg, {
128
+ encodingHint,
129
+ displayPath: absolutePath,
130
+ });
123
131
  const targetKey = String(target.targetKey ?? absolutePath);
124
132
  recordOpenState(targetKey, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
125
- if (decoded.footer)
133
+ // Gate looks up by resolved absolutePath; targetKey may differ
134
+ // (symlink/case) — mirror under both keys (issue #69 note).
135
+ if (targetKey !== absolutePath) {
136
+ recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
137
+ }
138
+ if (decoded.footer) {
126
139
  recordFooter(targetKey, decoded.footer);
140
+ if (targetKey !== absolutePath)
141
+ recordFooter(absolutePath, decoded.footer);
142
+ }
127
143
  return decoded.text;
128
144
  }
129
145
  try {
@@ -131,19 +147,24 @@ export function ctxFsIO(fs, ctx) {
131
147
  ...(signal === undefined ? {} : { signal }),
132
148
  });
133
149
  const text = await fs.readText(target, signal);
134
- const restored = await restoreStrippedUtf8Bom(fs, target, text, signal);
150
+ // Thread the restore path's stat version into the encoding memo so the
151
+ // read-first gate validates against the real version (issue #69 fix 2).
152
+ // The BOM-preserved branch stats for version only — no byte probe.
153
+ const { text: restored, version } = await restoreStrippedUtf8Bom(fs, target, text, signal);
135
154
  // record file encoding state for round-trip (BOM + lineEnding)
136
- // Do not probe fs.stat when BOM is already preserved — keep the no-probe guarantee
137
- // tested in fs-bridge.policy.test.ts (keeps a BOM already preserved without probing)
138
155
  try {
139
156
  const targetKey = String(target.targetKey ?? absolutePath);
140
157
  const hasBOM = restored.startsWith(UTF8_BOM);
141
158
  const clean = hasBOM ? restored.slice(1) : restored;
142
- // use seam to record with correct lineEnding detection; version left undefined
143
- // to avoid extra stat (the success path must stay probe-free when BOM preserved)
144
- recordOpenState(targetKey, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, undefined);
159
+ // use seam to record with correct lineEnding detection
160
+ recordOpenState(targetKey, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, version);
161
+ if (targetKey !== absolutePath) {
162
+ recordOpenState(absolutePath, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, version);
163
+ }
145
164
  // clear any stale autoGuess footer on clean UTF-8 read
146
165
  _clearAutoGuessFooter(targetKey);
166
+ if (targetKey !== absolutePath)
167
+ _clearAutoGuessFooter(absolutePath);
147
168
  }
148
169
  catch {
149
170
  // best-effort
@@ -154,7 +175,9 @@ export function ctxFsIO(fs, ctx) {
154
175
  const isNotText = error instanceof Error && error.code === "FS_NOT_TEXT";
155
176
  if (isNotText) {
156
177
  const cfg = loadConfig();
157
- const target = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
178
+ const target = await fs.resolve(absolutePath, {
179
+ ...(signal === undefined ? {} : { signal }),
180
+ });
158
181
  const info = await fs.stat(target, signal);
159
182
  const maxBytes = info?.size ?? 10 * 1024 * 1024;
160
183
  if (info && info.size !== undefined && info.size > maxBytes)
@@ -164,8 +187,14 @@ export function ctxFsIO(fs, ctx) {
164
187
  // Delegate deterministic admission to the deep seam
165
188
  const decoded = await decodeForOpen(bytes, cfg, { displayPath: absolutePath });
166
189
  recordOpenState(targetKey, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
167
- if (decoded.footer)
190
+ if (targetKey !== absolutePath) {
191
+ recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
192
+ }
193
+ if (decoded.footer) {
168
194
  recordFooter(targetKey, decoded.footer);
195
+ if (targetKey !== absolutePath)
196
+ recordFooter(absolutePath, decoded.footer);
197
+ }
169
198
  return decoded.text;
170
199
  }
171
200
  return mapFsError(error, absolutePath);
@@ -174,10 +203,14 @@ export function ctxFsIO(fs, ctx) {
174
203
  async writeText(absolutePath, content, signal, exec, sandboxPolicy) {
175
204
  // handle drift invalidation before encode — seam owns version check
176
205
  try {
177
- const targetTmp = await fs.resolve(absolutePath, { ...(signal === undefined ? {} : { signal }) });
206
+ const targetTmp = await fs.resolve(absolutePath, {
207
+ ...(signal === undefined ? {} : { signal }),
208
+ });
178
209
  const key = String(targetTmp.targetKey ?? absolutePath);
179
210
  const info = await fs.stat(targetTmp, signal).catch(() => undefined);
180
211
  invalidateIfStale(key, info?.version);
212
+ if (key !== absolutePath)
213
+ invalidateIfStale(absolutePath, info?.version);
181
214
  }
182
215
  catch { } // biome-ignore: best-effort
183
216
  try {
@@ -248,7 +281,10 @@ export function localIO() {
248
281
  if (encodingHint) {
249
282
  const bytes = await readFile(absolutePath);
250
283
  const cfg = loadConfig();
251
- const decoded = await decodeForOpen(bytes, cfg, { encodingHint, displayPath: absolutePath });
284
+ const decoded = await decodeForOpen(bytes, cfg, {
285
+ encodingHint,
286
+ displayPath: absolutePath,
287
+ });
252
288
  const info = await fileSnap(absolutePath).catch(() => undefined);
253
289
  recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.snapshotId);
254
290
  if (decoded.footer)
package/lib/fs-write.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { randomUUID } from "node:crypto";
2
- import { mkdir, open, readdir, rename, rm, stat, writeFile, } from "fs/promises";
2
+ import { mkdir, open, readdir, rename, rm, stat, writeFile } from "fs/promises";
3
3
  import { dirname, join } from "node:path";
4
4
  import { errCode } from "./utils.js";
5
5
  import { resolveTarget } from "./paths.js";
@@ -9,12 +9,7 @@ import { errCode } from "../utils.js";
9
9
  import { isBlankOverride } from "./parse.js";
10
10
  import { GUIDANCE_SECTIONS } from "./resolve.js";
11
11
  /** The presets shipped by the harness, each seeded with editable guidance. */
12
- export const DEFAULT_PRESETS = [
13
- "standard",
14
- "code",
15
- "minimal",
16
- "cordis",
17
- ];
12
+ export const DEFAULT_PRESETS = ["standard", "code", "minimal", "cordis"];
18
13
  /**
19
14
  * The root README: the per-preset customization convention users see in the
20
15
  * plugin home.
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { readFile } from "node:fs/promises";
9
9
  import { join } from "node:path";
10
- import { EDIT_GUIDANCE, READ_GUIDANCE, UNDO_GUIDANCE, } from "../prompts.js";
10
+ import { EDIT_GUIDANCE, READ_GUIDANCE, UNDO_GUIDANCE } from "../prompts.js";
11
11
  import { errCode } from "../utils.js";
12
12
  import { isBlankOverride, parseSectionFile } from "./parse.js";
13
13
  /**
package/lib/hash-store.js CHANGED
@@ -21,8 +21,8 @@ import { hashStorePath } from "./store-tenancy.js";
21
21
  import { onStoreOpen, setStoresGetter } from "./store-lifecycle.js";
22
22
  import { workspaceCwd } from "./workspace-context.js";
23
23
  import { errCode, splitLines } from "./utils.js";
24
- import { initHasher, contentChecksum, HASH_RE, CANON_VERSION, } from "./hashline/hash-assign.js";
25
- import { HASH_STORE_VERSION, HASH_STORE_BUSY_TIMEOUT, SERVED_TTL_MS, } from "./constants.js";
24
+ import { initHasher, contentChecksum, HASH_RE, CANON_VERSION } from "./hashline/hash-assign.js";
25
+ import { HASH_STORE_VERSION, HASH_STORE_BUSY_TIMEOUT, SERVED_TTL_MS } from "./constants.js";
26
26
  export function isValidHashList(value) {
27
27
  if (!Array.isArray(value))
28
28
  return false;
@@ -70,7 +70,9 @@ export function isValidRetiredEntry(value) {
70
70
  if (typeof value !== "object" || value === null)
71
71
  return false;
72
72
  const v = value;
73
- return typeof v.hash === "string" && (v.deathPos === null || typeof v.deathPos === "number") && HASH_RE.test(v.hash);
73
+ return (typeof v.hash === "string" &&
74
+ (v.deathPos === null || typeof v.deathPos === "number") &&
75
+ HASH_RE.test(v.hash));
74
76
  }
75
77
  export function isValidRetiredEntries(value) {
76
78
  if (!Array.isArray(value))
@@ -100,7 +102,7 @@ function parseRetiredJson(raw) {
100
102
  if (!Array.isArray(parsed) || parsed.length === 0)
101
103
  return [];
102
104
  if (isValidRetiredEntries(parsed)) {
103
- return parsed.map((e) => (e.deathPos === -1 ? { hash: e.hash, deathPos: null } : e));
105
+ return parsed.map((e) => e.deathPos === -1 ? { hash: e.hash, deathPos: null } : e);
104
106
  }
105
107
  const out = [];
106
108
  for (const e of parsed) {
@@ -200,10 +202,7 @@ function buildStore(db) {
200
202
  "hashes TEXT NOT NULL, " +
201
203
  "updated_at INTEGER NOT NULL" +
202
204
  ")");
203
- db.exec("CREATE TABLE IF NOT EXISTS meta (" +
204
- "key TEXT PRIMARY KEY, " +
205
- "value TEXT NOT NULL" +
206
- ")");
205
+ db.exec("CREATE TABLE IF NOT EXISTS meta (" + "key TEXT PRIMARY KEY, " + "value TEXT NOT NULL" + ")");
207
206
  db.exec("CREATE TABLE IF NOT EXISTS undo (" +
208
207
  "path TEXT PRIMARY KEY, " +
209
208
  "content TEXT NOT NULL, " +
@@ -213,17 +212,14 @@ function buildStore(db) {
213
212
  "result_content TEXT NOT NULL, " +
214
213
  "updated_at INTEGER NOT NULL" +
215
214
  ")");
216
- const versionRow = db
217
- .prepare("SELECT value FROM meta WHERE key = 'version'")
218
- .get();
215
+ const versionRow = db.prepare("SELECT value FROM meta WHERE key = 'version'").get();
219
216
  const versionChanged = versionRow !== undefined && versionRow.value !== String(HASH_STORE_VERSION);
220
217
  if (versionChanged) {
221
218
  db.exec("DELETE FROM snapshots");
222
219
  db.exec("DELETE FROM undo");
223
220
  }
224
221
  const servedColumns = db.prepare("PRAGMA table_info(served)").all();
225
- if (versionChanged ||
226
- !servedColumns.some((column) => column.name === "session_id")) {
222
+ if (versionChanged || !servedColumns.some((column) => column.name === "session_id")) {
227
223
  db.exec("DROP TABLE IF EXISTS served");
228
224
  }
229
225
  db.exec("CREATE TABLE IF NOT EXISTS served (" +
@@ -244,9 +240,7 @@ function buildStore(db) {
244
240
  try {
245
241
  db.exec("BEGIN IMMEDIATE");
246
242
  migrationOpen = true;
247
- const migrationColumns = db
248
- .prepare("PRAGMA table_info(served)")
249
- .all();
243
+ const migrationColumns = db.prepare("PRAGMA table_info(served)").all();
250
244
  if (!migrationColumns.some((column) => column.name === "retired")) {
251
245
  db.exec("ALTER TABLE served ADD COLUMN retired TEXT");
252
246
  // Pre-fix snapshots and undo entries may already bind a remembered
@@ -282,10 +276,8 @@ function buildStore(db) {
282
276
  if (!cardsColumns.some((column) => column.name === "cards")) {
283
277
  db.exec("ALTER TABLE served ADD COLUMN cards TEXT");
284
278
  }
285
- db
286
- .prepare("INSERT INTO meta (key, value) VALUES ('version', ?) " +
287
- "ON CONFLICT(key) DO UPDATE SET value = excluded.value")
288
- .run(String(HASH_STORE_VERSION));
279
+ db.prepare("INSERT INTO meta (key, value) VALUES ('version', ?) " +
280
+ "ON CONFLICT(key) DO UPDATE SET value = excluded.value").run(String(HASH_STORE_VERSION));
289
281
  const getStmt = db.prepare("SELECT hashes FROM snapshots WHERE path = ? AND checksum = ? AND line_count = ?");
290
282
  const allStmt = db.prepare("SELECT path FROM snapshots UNION SELECT path FROM undo UNION SELECT path FROM served");
291
283
  const allHashesStmt = db.prepare("SELECT path, hashes FROM snapshots");