dsh-better-edit 1.2.3 → 1.2.5

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 (105) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +39 -20
  3. package/README.zh.md +39 -31
  4. package/lib/contract.js +53 -22
  5. package/lib/domain-errors.js +18 -10
  6. package/lib/edit-mode.js +11 -0
  7. package/lib/encoding.js +2 -0
  8. package/lib/file-encoding-state.js +138 -44
  9. package/lib/file-view.js +26 -205
  10. package/lib/fs-bridge.js +53 -71
  11. package/lib/hashline/anchor-pipeline.js +8 -499
  12. package/lib/hashline/hash-assign.js +55 -4
  13. package/lib/hashline/index.js +5 -2
  14. package/lib/hashline/served-evidence.js +531 -0
  15. package/lib/index.js +177 -33
  16. package/lib/mutation/engine.js +19 -61
  17. package/lib/mutation.js +63 -105
  18. package/lib/noop-guard.js +24 -38
  19. package/lib/paths.js +9 -5
  20. package/lib/preset-toolset.js +144 -0
  21. package/lib/prompts.js +3 -3
  22. package/lib/read-and-serve.js +51 -53
  23. package/lib/reclaim.js +65 -0
  24. package/lib/self-heal.js +14 -13
  25. package/lib/session-view.js +79 -49
  26. package/lib/store/index.js +14 -14
  27. package/lib/store-config.js +4 -5
  28. package/lib/store-lifecycle.js +1 -1
  29. package/lib/tool-edit.js +14 -56
  30. package/lib/tool-read.js +2 -8
  31. package/lib/tool-str-replace-editor.js +34 -18
  32. package/lib/tool-undo.js +42 -24
  33. package/lib/types/contract.d.ts +11 -20
  34. package/lib/types/domain-errors.d.ts +14 -10
  35. package/lib/types/edit-mode.d.ts +11 -0
  36. package/lib/types/encoding.d.ts +1 -0
  37. package/lib/types/file-encoding-state.d.ts +51 -14
  38. package/lib/types/file-view.d.ts +18 -56
  39. package/lib/types/fs-bridge.d.ts +0 -1
  40. package/lib/types/hashline/anchor-pipeline.d.ts +6 -128
  41. package/lib/types/hashline/hash-assign.d.ts +40 -0
  42. package/lib/types/hashline/index.d.ts +6 -4
  43. package/lib/types/hashline/served-evidence.d.ts +152 -0
  44. package/lib/types/mutation/engine.d.ts +4 -12
  45. package/lib/types/mutation.d.ts +10 -58
  46. package/lib/types/noop-guard.d.ts +2 -33
  47. package/lib/types/paths.d.ts +0 -1
  48. package/lib/types/preset-toolset.d.ts +61 -0
  49. package/lib/types/reclaim.d.ts +36 -0
  50. package/lib/types/self-heal.d.ts +9 -3
  51. package/lib/types/session-view.d.ts +38 -8
  52. package/lib/types/store/index.d.ts +12 -11
  53. package/lib/types/store-config.d.ts +1 -1
  54. package/lib/types/store-lifecycle.d.ts +12 -2
  55. package/lib/types/tool-edit.d.ts +2 -4
  56. package/lib/types/tool-read.d.ts +0 -5
  57. package/lib/types/tool-str-replace-editor.d.ts +0 -6
  58. package/lib/types/tool-undo.d.ts +0 -5
  59. package/package.json +3 -2
  60. package/lib/drift.js +0 -5
  61. package/lib/dsh-context.js +0 -5
  62. package/lib/edit-engine.js +0 -7
  63. package/lib/edit-normalize.js +0 -9
  64. package/lib/edit-pipeline.js +0 -6
  65. package/lib/file-kind.js +0 -6
  66. package/lib/file-reader.js +0 -6
  67. package/lib/guidance/index.js +0 -8
  68. package/lib/hashline/alphabet.js +0 -2
  69. package/lib/hashline/apply.js +0 -2
  70. package/lib/hashline/hasher.js +0 -2
  71. package/lib/hashline/parse.js +0 -2
  72. package/lib/hashline/pure.js +0 -2
  73. package/lib/hashline/resolve.js +0 -3
  74. package/lib/hashline/served.js +0 -2
  75. package/lib/preview-controller.js +0 -65
  76. package/lib/read-render.js +0 -6
  77. package/lib/schema.js +0 -24
  78. package/lib/served-store.js +0 -5
  79. package/lib/tool-batch-edit.js +0 -190
  80. package/lib/truncate.js +0 -6
  81. package/lib/types/drift.d.ts +0 -5
  82. package/lib/types/dsh-context.d.ts +0 -5
  83. package/lib/types/edit-engine.d.ts +0 -7
  84. package/lib/types/edit-normalize.d.ts +0 -1
  85. package/lib/types/edit-pipeline.d.ts +0 -6
  86. package/lib/types/file-kind.d.ts +0 -6
  87. package/lib/types/file-reader.d.ts +0 -6
  88. package/lib/types/guidance/index.d.ts +0 -8
  89. package/lib/types/hashline/alphabet.d.ts +0 -1
  90. package/lib/types/hashline/apply.d.ts +0 -1
  91. package/lib/types/hashline/hasher.d.ts +0 -1
  92. package/lib/types/hashline/parse.d.ts +0 -1
  93. package/lib/types/hashline/pure.d.ts +0 -1
  94. package/lib/types/hashline/resolve.d.ts +0 -1
  95. package/lib/types/hashline/served.d.ts +0 -1
  96. package/lib/types/preview-controller.d.ts +0 -33
  97. package/lib/types/read-render.d.ts +0 -6
  98. package/lib/types/schema.d.ts +0 -25
  99. package/lib/types/served-store.d.ts +0 -5
  100. package/lib/types/tool-batch-edit.d.ts +0 -25
  101. package/lib/types/truncate.d.ts +0 -6
  102. package/lib/types/validation.d.ts +0 -6
  103. package/lib/types/workspace.d.ts +0 -5
  104. package/lib/validation.js +0 -6
  105. package/lib/workspace.js +0 -5
package/lib/fs-bridge.js CHANGED
@@ -17,10 +17,11 @@
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 { fileSnap } from "./file-reader.js";
20
+ import { fileSnap } from "./file-view.js";
21
21
  import { resolveTarget, toCwd } from "./paths.js";
22
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
- import { codeOf } from "./utils.js";
23
+ import { codeOf, errCode } from "./utils.js";
24
+ import { SUPPORTED_ENCODINGS_LABEL } from "./encoding.js";
24
25
  import { DomainError } from "./domain-errors.js";
25
26
  export { _getEncodingState as getEncodingState, _setEncodingState as setEncodingState, _clearEncodingState as clearEncodingState, _getAutoGuessFooter as getAutoGuessFooter, _setAutoGuessFooter as setAutoGuessFooter, _clearAutoGuessFooter as clearAutoGuessFooter, };
26
27
  // Keep alias for tests that import getAutoGuessFooter via fs-bridge
@@ -36,6 +37,8 @@ export function getAutoGuessFooterCompat(k) {
36
37
  */
37
38
  export function mapFsError(error, displayPath) {
38
39
  if (error instanceof Error && typeof error.code === "string") {
40
+ // SAFETY: provider failures (FsError or the local backend's plain Error) carry a
41
+ // string `code` field that the published error type does not declare.
39
42
  const code = error.code;
40
43
  if (code === "FS_NOT_FOUND") {
41
44
  throw new DomainError("E_NOT_FOUND", { path: displayPath });
@@ -52,7 +55,7 @@ export function mapFsError(error, displayPath) {
52
55
  }
53
56
  if (code === "FS_BAD_ENCODING") {
54
57
  throw new DomainError("E_BAD_PAYLOAD", {
55
- message: `Unknown encoding for ${displayPath}. Supported: utf8, gbk, big5, shift_jis, euc-kr, windows-1251, iso-8859-1`,
58
+ message: `Unknown encoding for ${displayPath}. Supported: ${SUPPORTED_ENCODINGS_LABEL}`,
56
59
  });
57
60
  }
58
61
  if (code === "FS_DECODE_FAILED") {
@@ -120,7 +123,6 @@ async function restoreStrippedUtf8Bom(fs, target, text, signal) {
120
123
  }
121
124
  return { text: `${UTF8_BOM}${text}`, version };
122
125
  }
123
- /** FileIO over the deployment's `ctx.fs` service. */
124
126
  export function ctxFsIO(fs, ctx) {
125
127
  return {
126
128
  async resolve(path, cwd, signal) {
@@ -144,18 +146,9 @@ export function ctxFsIO(fs, ctx) {
144
146
  encodingHint,
145
147
  displayPath: absolutePath,
146
148
  });
147
- const targetKey = String(target.targetKey ?? absolutePath);
148
- recordOpenState(targetKey, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
149
- // Gate looks up by resolved absolutePath; targetKey may differ
150
- // (symlink/case) — mirror under both keys (issue #69 note).
151
- if (targetKey !== absolutePath) {
152
- recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
153
- }
154
- if (decoded.footer) {
155
- recordFooter(targetKey, decoded.footer);
156
- if (targetKey !== absolutePath)
157
- recordFooter(absolutePath, decoded.footer);
158
- }
149
+ recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
150
+ if (decoded.footer)
151
+ recordFooter(absolutePath, decoded.footer);
159
152
  return decoded.text;
160
153
  }
161
154
  try {
@@ -169,18 +162,12 @@ export function ctxFsIO(fs, ctx) {
169
162
  const { text: restored, version } = await restoreStrippedUtf8Bom(fs, target, text, signal);
170
163
  // record file encoding state for round-trip (BOM + lineEnding)
171
164
  try {
172
- const targetKey = String(target.targetKey ?? absolutePath);
173
165
  const hasBOM = restored.startsWith(UTF8_BOM);
174
166
  const clean = hasBOM ? restored.slice(1) : restored;
175
167
  // use seam to record with correct lineEnding detection
176
- recordOpenState(targetKey, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, version);
177
- if (targetKey !== absolutePath) {
178
- recordOpenState(absolutePath, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, version);
179
- }
168
+ recordOpenState(absolutePath, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, version);
180
169
  // clear any stale autoGuess footer on clean UTF-8 read
181
- _clearAutoGuessFooter(targetKey);
182
- if (targetKey !== absolutePath)
183
- _clearAutoGuessFooter(absolutePath);
170
+ _clearAutoGuessFooter(absolutePath);
184
171
  }
185
172
  catch {
186
173
  // best-effort
@@ -188,7 +175,7 @@ export function ctxFsIO(fs, ctx) {
188
175
  return restored;
189
176
  }
190
177
  catch (error) {
191
- const isNotText = error instanceof Error && error.code === "FS_NOT_TEXT";
178
+ const isNotText = errCode(error) === "FS_NOT_TEXT";
192
179
  if (isNotText) {
193
180
  const cfg = loadConfig();
194
181
  const target = await fs.resolve(absolutePath, {
@@ -199,60 +186,48 @@ export function ctxFsIO(fs, ctx) {
199
186
  if (info && info.size !== undefined && info.size > maxBytes)
200
187
  throw error;
201
188
  const bytes = await fs.readBytes(target, signal, maxBytes);
202
- const targetKey = String(target.targetKey ?? absolutePath);
203
189
  // Delegate deterministic admission to the deep seam
204
190
  const decoded = await decodeForOpen(bytes, cfg, { displayPath: absolutePath });
205
- recordOpenState(targetKey, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
206
- if (targetKey !== absolutePath) {
207
- recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
208
- }
209
- if (decoded.footer) {
210
- recordFooter(targetKey, decoded.footer);
211
- if (targetKey !== absolutePath)
212
- recordFooter(absolutePath, decoded.footer);
213
- }
191
+ recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
192
+ if (decoded.footer)
193
+ recordFooter(absolutePath, decoded.footer);
214
194
  return decoded.text;
215
195
  }
216
196
  return mapFsError(error, absolutePath);
217
197
  }
218
198
  },
219
199
  async writeText(absolutePath, content, signal, exec, sandboxPolicy) {
220
- // handle drift invalidation before encode — seam owns version check
221
- try {
222
- const targetTmp = await fs.resolve(absolutePath, {
223
- ...(signal === undefined ? {} : { signal }),
224
- });
225
- const key = String(targetTmp.targetKey ?? absolutePath);
226
- const info = await fs.stat(targetTmp, signal).catch(() => undefined);
227
- invalidateIfStale(key, info?.version);
228
- if (key !== absolutePath)
229
- invalidateIfStale(absolutePath, info?.version);
230
- }
231
- catch { } // biome-ignore: best-effort
232
200
  try {
233
201
  const target = await fs.resolve(absolutePath, {
234
202
  ...(signal === undefined ? {} : { signal }),
235
203
  });
204
+ // Drop a memo that no longer matches the on-disk version before deciding:
205
+ // the save verdict must describe the bytes actually being replaced.
206
+ const info = await fs.stat(target, signal).catch(() => undefined);
207
+ const version = info?.version;
208
+ invalidateIfStale(absolutePath, version);
209
+ // The seam owns the round-trip verdict. It throws E_ENCODING_UNWRITABLE
210
+ // BEFORE the provider sees a write when the recorded encoding cannot be
211
+ // reproduced through the text-only write (issue #112).
212
+ const cfgW = loadConfig();
213
+ const existing = _getEncodingState(absolutePath);
214
+ const prep = prepareForSave(content, existing, {
215
+ normalizeToUtf8: cfgW.normalizeToUtf8,
216
+ displayPath: absolutePath,
217
+ });
236
218
  const intent = await ctx.waterfall("fs/write-intent", target, exec, () => undefined);
237
- // Seam owns Save-with-Encoding memo update and normalizeToUtf8 check.
238
- // Provider expects UTF-8 string; for legacy without normalize we keep
239
- // string as-is (next read re-detects via version bump).
240
- try {
241
- const cfgW = loadConfig();
242
- const key = String(target.targetKey ?? absolutePath);
243
- // Honor manual override if content was produced via write({encoding}) — not wired here,
244
- // encodingHint is not part of writeText signature for ctxFsIO in current call sites.
245
- // Check existing memo for legacy preservation.
246
- const existing = _getEncodingState(key);
247
- const prep = prepareForSave(content, existing, { normalizeToUtf8: cfgW.normalizeToUtf8 });
248
- // For Save with Encoding hint, prepareForSave would create newState — not used in this path (tool-undo/write-hook carry hint separately)
249
- if (prep.newState)
250
- _setEncodingState(key, prep.newState);
251
- // content stays as UTF-8 string for provider; prep.textToWrite is same as content
252
- }
253
- catch { } // biome-ignore: best-effort
254
- const outcome = await fs.writeText(target, content, intent, signal, sandboxPolicy);
219
+ // The provider encodes the string as UTF-8; prep.textToWrite already carries
220
+ // the restored BOM + line endings (or the explicit normalizeToUtf8 migration).
221
+ const outcome = await fs.writeText(target, prep.textToWrite, intent, signal, sandboxPolicy);
255
222
  ctx.emit("fs/observed", target, { kind: "present", version: outcome.version }, exec);
223
+ // Memo/bytes agreement: every write records the state of the bytes just written.
224
+ if (prep.newState) {
225
+ const written = {
226
+ ...prep.newState,
227
+ version: outcome.version,
228
+ };
229
+ _setEncodingState(absolutePath, written);
230
+ }
256
231
  }
257
232
  catch (error) {
258
233
  return mapFsError(error, absolutePath);
@@ -328,13 +303,20 @@ export function localIO() {
328
303
  },
329
304
  async writeText(absolutePath, content, signal, _exec, _sandboxPolicy) {
330
305
  signal?.throwIfAborted();
331
- // Invalidate stale memo before write, and handle lineEnding via seam if needed
332
- try {
333
- const info = await fileSnap(absolutePath).catch(() => undefined);
334
- invalidateIfStale(absolutePath, info?.snapshotId);
306
+ // Drop a stale memo, then let the seam decide: it throws E_ENCODING_UNWRITABLE
307
+ // before writeAtomic when the recorded encoding cannot round-trip (#112).
308
+ const info = await fileSnap(absolutePath).catch(() => undefined);
309
+ invalidateIfStale(absolutePath, info?.snapshotId);
310
+ const cfgL = loadConfig();
311
+ const prep = prepareForSave(content, _getEncodingState(absolutePath), {
312
+ normalizeToUtf8: cfgL.normalizeToUtf8,
313
+ displayPath: absolutePath,
314
+ });
315
+ await writeAtomic(absolutePath, prep.textToWrite);
316
+ if (prep.newState) {
317
+ const after = await fileSnap(absolutePath).catch(() => undefined);
318
+ _setEncodingState(absolutePath, { ...prep.newState, version: after?.snapshotId });
335
319
  }
336
- catch { }
337
- await writeAtomic(absolutePath, content);
338
320
  },
339
321
  async emitObserved() {
340
322
  // No policy event gate on the host filesystem; nothing to record.