dsh-better-edit 0.5.0 → 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.
Files changed (44) hide show
  1. package/CHANGELOG.md +20 -2
  2. package/README.md +1 -1
  3. package/lib/contract.js +1 -1
  4. package/lib/edit-engine.js +4 -454
  5. package/lib/edit-response.js +24 -2
  6. package/lib/encoding.js +308 -0
  7. package/lib/file-encoding-state.js +263 -0
  8. package/lib/file-view.js +1 -1
  9. package/lib/fs-bridge.js +124 -19
  10. package/lib/hash-store.js +1 -2
  11. package/lib/hashline/anchor-pipeline.js +19 -165
  12. package/lib/index.js +2 -2
  13. package/lib/mutation/engine.js +388 -0
  14. package/lib/mutation.js +84 -11
  15. package/lib/paths.js +1 -1
  16. package/lib/read-and-serve.js +5 -0
  17. package/lib/session-view.js +16 -49
  18. package/lib/store/index.js +28 -0
  19. package/lib/store-config.js +132 -2
  20. package/lib/store-lifecycle.js +1 -1
  21. package/lib/tool-edit.js +2 -2
  22. package/lib/tool-read.js +26 -6
  23. package/lib/tool-undo.js +2 -2
  24. package/lib/types/contract.d.ts +1 -0
  25. package/lib/types/edit-diff.d.ts +1 -1
  26. package/lib/types/edit-engine.d.ts +4 -176
  27. package/lib/types/edit-response.d.ts +1 -1
  28. package/lib/types/encoding.d.ts +46 -0
  29. package/lib/types/file-encoding-state.d.ts +77 -0
  30. package/lib/types/file-view.d.ts +2 -1
  31. package/lib/types/fs-bridge.d.ts +5 -1
  32. package/lib/types/hashline/anchor-pipeline.d.ts +6 -20
  33. package/lib/types/hashline/index.d.ts +1 -1
  34. package/lib/types/mutation/engine.d.ts +143 -0
  35. package/lib/types/mutation.d.ts +30 -5
  36. package/lib/types/paths.d.ts +1 -1
  37. package/lib/types/read-and-serve.d.ts +4 -1
  38. package/lib/types/session-view.d.ts +16 -30
  39. package/lib/types/store/index.d.ts +25 -0
  40. package/lib/types/store-config.d.ts +10 -1
  41. package/lib/types/workspace-context.d.ts +23 -0
  42. package/lib/workspace-context.js +44 -0
  43. package/lib/write-hook.js +2 -1
  44. package/package.json +5 -1
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Store — deep module owning tenancy + lifecycle + path resolution.
3
+ *
4
+ * Single seam that answers "which store, where, and is it open?"
5
+ * Tenancy (where DB lives) and lifecycle (central GC + row TTL + WAL)
6
+ * are private details of this seam; hash-store keeps only schema +
7
+ * row-family domain logic and imports path resolution from here.
8
+ * StoreConfig remains the config complement seam but its path is
9
+ * resolved via this Store seam.
10
+ *
11
+ * YAGNI-gated: DB plumbing (openDb/buildStore/quarantine/busy-retry/
12
+ * WAL/legacy JSON migration) stays in hash-store for now and is
13
+ * re-exported here for testability without importing domain helpers
14
+ * — full plumbing move is deferred to avoid contract risk. Old
15
+ * store-tenancy/store-lifecycle files are kept as compat shims
16
+ * re-exporting from this seam (reversible).
17
+ *
18
+ * @module dsh-better-edit/store
19
+ */
20
+ // Tenancy — re-export as seam ownership (private details, public via Store)
21
+ export { tenancyFor, configDir, hashStorePath, hashStoreDir, legacyHashStorePath, loadConfig, _resetConfigCache, expand, } from "../store-tenancy.js";
22
+ // Lifecycle — re-export as seam ownership
23
+ export { onStoreOpen, onAppStart, onSessionStart, setStoresGetter, runCentralJanitorIfDue, _resetLifecycleForTests, } from "../store-lifecycle.js";
24
+ // StoreConfig path helper — canonical Store seam entry for config consumers
25
+ export { loadConfig as loadStoreConfig } from "../store-config.js";
26
+ // Hash-store plumbing re-exported for testability without domain coupling
27
+ // (future: move openDb/buildStore/quarantine/busy-retry here)
28
+ export { isCorruptionError, loadHashStore, shutdownHashStore, withStore, findSnapshotPathsByHashes, upsertSnapshotFor, } from "../hash-store.js";
@@ -6,7 +6,7 @@
6
6
  */
7
7
  import { homedir } from "node:os";
8
8
  import { isAbsolute, join } from "node:path";
9
- import { readFileSync, statSync } from "node:fs";
9
+ import { readFileSync, statSync, appendFileSync } from "node:fs";
10
10
  import { mkdir, writeFile } from "node:fs/promises";
11
11
  import { resolveDshHome } from "@deepseek-ai/dsh-home-paths";
12
12
  import { z } from "zod";
@@ -46,6 +46,11 @@ function parseSimpleYaml(content) {
46
46
  continue;
47
47
  const key = line.slice(0, colon).trim();
48
48
  let value = line.slice(colon + 1).trim();
49
+ if (value.startsWith("[") && value.endsWith("]")) {
50
+ value = value.slice(1, -1);
51
+ out[key] = value;
52
+ continue;
53
+ }
49
54
  value = stripQuotes(value);
50
55
  value = stripInlineComment(value);
51
56
  out[key] = value;
@@ -69,6 +74,13 @@ function parseAutoGitIgnoreRaw(v) {
69
74
  return false;
70
75
  return undefined;
71
76
  }
77
+ function parseSupportedEncodingsRaw(v) {
78
+ const inner = v.trim().replace(/^\[/, "").replace(/\]$/, "");
79
+ const items = inner.split(/[ ,;]+/).map((s) => s.trim().toLowerCase()).filter((s) => s.length > 0);
80
+ if (items.length === 0)
81
+ return undefined;
82
+ return items;
83
+ }
72
84
  // ---- Zod schema (single source of truth) ----
73
85
  const StoreConfigSchema = z.object({
74
86
  storeDir: z
@@ -76,6 +88,9 @@ const StoreConfigSchema = z.object({
76
88
  .default("central")
77
89
  .transform((v) => normalizeStoreDir(v) ?? "central"), // where the store lives: "central" (default, in $DSH_HOME/.../runtime/<name>-<hash8>/) | "workspace" (legacy <ws>/.dsh_better_edit/) | "/abs/path" (custom root)
78
90
  autoGitignore: z.boolean().default(false), // workspace only: auto-append ".dsh_better_edit/" to .gitignore when .git exists but entry missing (false = warn once)
91
+ autoGuessEncoding: z.boolean().default(false), // VS Code files.autoGuessEncoding — deterministic BOM→UTF-8 first, model-assisted top-3 only when true
92
+ normalizeToUtf8: z.boolean().default(false), // when true, legacy files (GBK etc.) migrate to UTF-8 on first edit/write
93
+ supportedEncodings: z.array(z.string()).default(["gbk", "big5", "shift_jis", "euc-kr", "windows-1251", "iso-8859-1"]),
79
94
  undo_ttl_s: z.number().int().min(-1).default(604800), // undo history TTL in seconds; -1 = keep forever (default 604800 = 7 days)
80
95
  storeMaxAgeS: z.number().int().min(1).default(2592000), // central janitor: max idle age in seconds before evicting runtime/<name>-<hash8>/ (default 2592000 = 30 days)
81
96
  storeMaxTotalBytes: z.number().int().min(0).default(524288000), // central janitor: max total bytes under runtime/ before LRU eviction (default 524288000 = 500 MB)
@@ -86,6 +101,9 @@ let cachedMtimeMs;
86
101
  let cachedYamlPath;
87
102
  let cachedEnvStoreDir;
88
103
  let cachedEnvAutoGitignore;
104
+ let cachedEnvAutoGuessEncoding;
105
+ let cachedEnvNormalizeToUtf8;
106
+ let cachedEnvSupportedEncodings;
89
107
  function configYamlPath() {
90
108
  return join(resolveDshHome(), "plugins", "dsh-better-edit", "config.yaml");
91
109
  }
@@ -102,6 +120,14 @@ storeDir: central
102
120
  # workspace only: when .git exists but .gitignore lacks .dsh_better_edit/, true = auto-append ".dsh_better_edit/", false = warn once
103
121
  autoGitignore: false
104
122
 
123
+ # encoding: deterministic BOM→UTF-8 first, guessing only when autoGuessEncoding:true
124
+ # when true, FS_NOT_TEXT scores supportedEncodings and surfaces top-3 previews for model pick
125
+ autoGuessEncoding: false
126
+ # when true, legacy files (GBK etc.) migrate to UTF-8 on first write
127
+ normalizeToUtf8: false
128
+ # allowlist scored for top-3 previews when autoGuessEncoding:true
129
+ supportedEncodings: [gbk, big5, shift_jis, euc-kr, windows-1251, iso-8859-1]
130
+
105
131
  # undo history TTL in seconds; -1 = keep forever (default 604800 = 7 days)
106
132
  undo_ttl_s: 604800
107
133
 
@@ -217,6 +243,27 @@ function fsAdapter() {
217
243
  }
218
244
  }
219
245
  }
246
+ if (parsed.autoGuessEncoding !== undefined) {
247
+ const b = parseAutoGitIgnoreRaw(parsed.autoGuessEncoding);
248
+ if (b === undefined)
249
+ console.warn(`dsh-better-edit: autoGuessEncoding "${parsed.autoGuessEncoding}" invalid — expected true|false`);
250
+ else
251
+ out.autoGuessEncoding = b;
252
+ }
253
+ if (parsed.normalizeToUtf8 !== undefined) {
254
+ const b = parseAutoGitIgnoreRaw(parsed.normalizeToUtf8);
255
+ if (b === undefined)
256
+ console.warn(`dsh-better-edit: normalizeToUtf8 "${parsed.normalizeToUtf8}" invalid — expected true|false`);
257
+ else
258
+ out.normalizeToUtf8 = b;
259
+ }
260
+ if (parsed.supportedEncodings !== undefined) {
261
+ const arr = parseSupportedEncodingsRaw(parsed.supportedEncodings);
262
+ if (arr === undefined)
263
+ console.warn(`dsh-better-edit: supportedEncodings "${parsed.supportedEncodings}" invalid`);
264
+ else
265
+ out.supportedEncodings = arr;
266
+ }
220
267
  if (parsed.storeMaxTotalBytes !== undefined) {
221
268
  const n = Number(parsed.storeMaxTotalBytes);
222
269
  const res = z.number().int().min(0).safeParse(n);
@@ -254,6 +301,30 @@ function envAdapter(base) {
254
301
  }
255
302
  }
256
303
  }
304
+ const envGuess = process.env.DSH_BETTER_EDIT_AUTO_GUESS_ENCODING;
305
+ if (envGuess !== undefined) {
306
+ const b = parseAutoGitIgnoreRaw(envGuess);
307
+ if (b === undefined)
308
+ console.warn(`dsh-better-edit: DSH_BETTER_EDIT_AUTO_GUESS_ENCODING invalid — expected true|false`);
309
+ else
310
+ out.autoGuessEncoding = b;
311
+ }
312
+ const envNorm = process.env.DSH_BETTER_EDIT_NORMALIZE_TO_UTF8;
313
+ if (envNorm !== undefined) {
314
+ const b = parseAutoGitIgnoreRaw(envNorm);
315
+ if (b === undefined)
316
+ console.warn(`dsh-better-edit: DSH_BETTER_EDIT_NORMALIZE_TO_UTF8 invalid — expected true|false`);
317
+ else
318
+ out.normalizeToUtf8 = b;
319
+ }
320
+ const envSup = process.env.DSH_BETTER_EDIT_SUPPORTED_ENCODINGS;
321
+ if (envSup !== undefined) {
322
+ const arr = parseSupportedEncodingsRaw(envSup);
323
+ if (arr === undefined)
324
+ console.warn(`dsh-better-edit: DSH_BETTER_EDIT_SUPPORTED_ENCODINGS invalid`);
325
+ else
326
+ out.supportedEncodings = arr;
327
+ }
257
328
  const envAuto = process.env.DSH_BETTER_EDIT_AUTO_GITIGNORE;
258
329
  if (envAuto !== undefined) {
259
330
  const b = parseAutoGitIgnoreRaw(envAuto);
@@ -266,6 +337,46 @@ function envAdapter(base) {
266
337
  }
267
338
  return out;
268
339
  }
340
+ function complementMissingFieldsSync(yamlPath, yamlPartial) {
341
+ // Best-effort: append missing canonical keys with defaults, preserving existing content.
342
+ // Only runs when the file exists (mtime !== undefined) and some canonical key is absent.
343
+ const missingLines = [];
344
+ if (yamlPartial.storeDir === undefined)
345
+ missingLines.push("storeDir: central");
346
+ if (yamlPartial.autoGitignore === undefined)
347
+ missingLines.push("autoGitignore: false");
348
+ if (yamlPartial.autoGuessEncoding === undefined)
349
+ missingLines.push("autoGuessEncoding: false");
350
+ if (yamlPartial.normalizeToUtf8 === undefined)
351
+ missingLines.push("normalizeToUtf8: false");
352
+ if (yamlPartial.supportedEncodings === undefined)
353
+ missingLines.push("supportedEncodings: [gbk, big5, shift_jis, euc-kr, windows-1251, iso-8859-1]");
354
+ if (yamlPartial.undo_ttl_s === undefined)
355
+ missingLines.push("undo_ttl_s: 604800");
356
+ if (yamlPartial.storeMaxAgeS === undefined)
357
+ missingLines.push("storeMaxAgeS: 2592000");
358
+ if (yamlPartial.storeMaxTotalBytes === undefined)
359
+ missingLines.push("storeMaxTotalBytes: 524288000");
360
+ if (missingLines.length === 0)
361
+ return;
362
+ try {
363
+ // Ensure we don't duplicate if file was concurrently complemented — re-read raw keys.
364
+ const raw = readFileSync(yamlPath, "utf-8");
365
+ const parsed = parseSimpleYaml(raw);
366
+ // Re-check against raw parsed (aliases for storeMaxAgeS already handled via yamlPartial, but double-check).
367
+ const stillMissing = missingLines.filter((line) => {
368
+ const key = line.split(":")[0].trim();
369
+ return parsed[key] === undefined;
370
+ });
371
+ if (stillMissing.length === 0)
372
+ return;
373
+ const suffix = (raw.endsWith("\n") || raw.length === 0 ? "" : "\n") + stillMissing.join("\n") + "\n";
374
+ appendFileSync(yamlPath, suffix, "utf-8");
375
+ }
376
+ catch {
377
+ // best-effort: ENOENT (file deleted between stat and read) or permission — ignore
378
+ }
379
+ }
269
380
  // ---- public load (merged, cached) ----
270
381
  export function loadConfig() {
271
382
  const yamlPath = configYamlPath();
@@ -277,15 +388,28 @@ export function loadConfig() {
277
388
  // no yaml file — fall through to defaults/cache miss
278
389
  }
279
390
  const envStoreDir = process.env.DSH_BETTER_EDIT_STORE_DIR;
391
+ const envGuess = process.env.DSH_BETTER_EDIT_AUTO_GUESS_ENCODING;
392
+ const envNorm = process.env.DSH_BETTER_EDIT_NORMALIZE_TO_UTF8;
393
+ const envSup = process.env.DSH_BETTER_EDIT_SUPPORTED_ENCODINGS;
280
394
  const envAuto = process.env.DSH_BETTER_EDIT_AUTO_GITIGNORE;
281
395
  if (cachedConfig !== undefined &&
282
396
  cachedMtimeMs === mtime &&
283
397
  cachedYamlPath === yamlPath &&
284
398
  cachedEnvStoreDir === envStoreDir &&
285
- cachedEnvAutoGitignore === envAuto) {
399
+ cachedEnvAutoGitignore === envAuto &&
400
+ cachedEnvAutoGuessEncoding === envGuess &&
401
+ cachedEnvNormalizeToUtf8 === envNorm &&
402
+ cachedEnvSupportedEncodings === envSup) {
286
403
  return cachedConfig;
287
404
  }
288
405
  const yamlPartial = fsAdapter();
406
+ if (mtime !== undefined) {
407
+ complementMissingFieldsSync(yamlPath, yamlPartial);
408
+ try {
409
+ mtime = statSync(yamlPath).mtimeMs;
410
+ }
411
+ catch { }
412
+ }
289
413
  const envPartial = envAdapter({ ...DEFAULT_CONFIG, ...yamlPartial });
290
414
  const merged = { ...DEFAULT_CONFIG, ...yamlPartial, ...envPartial };
291
415
  // final Zod validation + normalization (expand abs, fallback)
@@ -305,6 +429,9 @@ export function loadConfig() {
305
429
  cachedYamlPath = yamlPath;
306
430
  cachedEnvStoreDir = envStoreDir;
307
431
  cachedEnvAutoGitignore = envAuto;
432
+ cachedEnvAutoGuessEncoding = envGuess;
433
+ cachedEnvNormalizeToUtf8 = envNorm;
434
+ cachedEnvSupportedEncodings = envSup;
308
435
  return cfg;
309
436
  }
310
437
  // For tests: reset cache
@@ -314,4 +441,7 @@ export function _resetConfigCache() {
314
441
  cachedYamlPath = undefined;
315
442
  cachedEnvStoreDir = undefined;
316
443
  cachedEnvAutoGitignore = undefined;
444
+ cachedEnvAutoGuessEncoding = undefined;
445
+ cachedEnvNormalizeToUtf8 = undefined;
446
+ cachedEnvSupportedEncodings = undefined;
317
447
  }
@@ -16,7 +16,7 @@ import { dirname, join } from "node:path";
16
16
  import { DatabaseSync } from "node:sqlite";
17
17
  import { resolveDshHome } from "@deepseek-ai/dsh-home-paths";
18
18
  import { hashStorePath, loadConfig } from "./paths.js";
19
- import { workspaceCwd } from "./workspace.js";
19
+ import { workspaceCwd } from "./workspace-context.js";
20
20
  import { errCode, splitLines } from "./utils.js";
21
21
  import { HASH_STORE_BUSY_TIMEOUT, SERVED_TTL_MS } from "./constants.js";
22
22
  // ---- throttling state (owned here) ----
package/lib/tool-edit.js CHANGED
@@ -10,8 +10,8 @@ import { normalizeRequest as normReq, assertEditRequest, } from "./contract.js";
10
10
  import { abortIf } from "./utils.js";
11
11
  import { execute } from "./mutation.js";
12
12
  import { EDIT_DESCRIPTION } from "./prompts.js";
13
- import { execCwd, execSessionKey } from "./session-view.js";
14
- import { withWorkspace } from "./session-view.js";
13
+ import { execCwd, execSessionKey } from "./workspace-context.js";
14
+ import { withWorkspace } from "./workspace-context.js";
15
15
  import { findSnapshotPathsByHashes } from "./hash-store.js";
16
16
  import { parseHashRef } from "./hashline/anchor-pipeline.js";
17
17
  async function resolveNullPath(edits) {
package/lib/tool-read.js CHANGED
@@ -9,8 +9,9 @@ import { defineTool } from "@deepseek-ai/dsh-tools";
9
9
  import { normalizeRequest as normReq, assertReadRequest, pathSchema } from "./contract.js";
10
10
  import { readAndServe } from "./read-and-serve.js";
11
11
  import { READ_DESCRIPTION } from "./prompts.js";
12
- import { execCwd, execSessionKey } from "./session-view.js";
13
- import { withWorkspace } from "./session-view.js";
12
+ import { normalizeEncoding } from "./encoding.js";
13
+ import { execCwd, execSessionKey } from "./workspace-context.js";
14
+ import { withWorkspace } from "./workspace-context.js";
14
15
  /**
15
16
  * Register the hash-anchored `read` tool on the calling agent's scope.
16
17
  * @param _rootCtx - host context.
@@ -32,30 +33,49 @@ export function buildReadTool(io) {
32
33
  type: "number",
33
34
  description: "Maximum number of lines to read",
34
35
  },
36
+ encoding: {
37
+ type: "string",
38
+ description: "Text encoding for Reopen with Encoding (e.g. gbk, shift_jis, windows-1251). Case-insensitive.",
39
+ },
35
40
  },
36
41
  output: {
37
- schema: { type: "string" },
38
- render: (_args, value) => [{ type: "text", text: value }],
42
+ schema: { type: "object", properties: { text: { type: "string", required: true }, warning: { type: "string" } }, additionalProperties: false },
43
+ render: (_args, value) => {
44
+ const v = value;
45
+ if (typeof v === "string")
46
+ return [{ type: "text", text: v }];
47
+ const blocks = [{ type: "text", text: v.text }];
48
+ if (v.warning)
49
+ blocks.push({ type: "text", text: v.warning });
50
+ return blocks;
51
+ },
39
52
  },
40
53
  async execute(args, exec) {
41
54
  return withWorkspace(execCwd(exec), async () => {
42
55
  const cwd = execCwd(exec);
43
56
  const sessionKey = execSessionKey(exec);
44
57
  const signal = exec.signal;
58
+ const encoding = args.encoding;
59
+ if (encoding !== undefined) {
60
+ const norm = normalizeEncoding(String(encoding));
61
+ if (!norm)
62
+ throw new Error(`[E_BAD_ENCODING] Unknown encoding: ${String(encoding)}. Supported: utf8, gbk, big5, shift_jis, euc-kr, windows-1251, iso-8859-1`);
63
+ }
45
64
  const canonical = normReq(args);
46
65
  assertReadRequest(canonical);
47
66
  const rawPath = canonical.path;
48
- const { text, absolutePath } = await readAndServe(io, rawPath, cwd, {
67
+ const { text, absolutePath, warning } = await readAndServe(io, rawPath, cwd, {
49
68
  sessionKey,
50
69
  signal,
51
70
  offset: canonical.offset,
52
71
  limit: canonical.limit,
72
+ encoding: encoding,
53
73
  });
54
74
  // Record the present observation with the fs policy gate so later
55
75
  // built-in write/edit calls see this file as observed at the
56
76
  // version the model just read (a no-op when no policy listens).
57
77
  await io.emitObserved(absolutePath, exec, signal);
58
- return text;
78
+ return warning ? { text, warning } : { text };
59
79
  });
60
80
  },
61
81
  });
package/lib/tool-undo.js CHANGED
@@ -16,8 +16,8 @@ import { changedRange } from "./hashline/anchor-pipeline.js";
16
16
  import { getUndo, clearUndo } from "./undo-edit.js";
17
17
  import { recordServedTruncated } from "./session-view.js";
18
18
  import { UNDO_DESCRIPTION } from "./prompts.js";
19
- import { execCwd, execSessionKey } from "./session-view.js";
20
- import { withWorkspace } from "./session-view.js";
19
+ import { execCwd, execSessionKey } from "./workspace-context.js";
20
+ import { withWorkspace } from "./workspace-context.js";
21
21
  /**
22
22
  * Register the `undo_last_edit` tool on the calling agent's scope.
23
23
  * @param _rootCtx - host context.
@@ -27,6 +27,7 @@ export interface ReadParams {
27
27
  path: string;
28
28
  offset?: number;
29
29
  limit?: number;
30
+ encoding?: string;
30
31
  }
31
32
  export interface UndoParams {
32
33
  path: string;
@@ -1,4 +1,4 @@
1
- import type { ServedRow } from "./hashline/served.js";
1
+ import type { ServedRow } from "./hashline/anchor-pipeline.js";
2
2
  export type LineEnding = "\r\n" | "\n" | "\r";
3
3
  export declare function detectEnding(content: string): LineEnding;
4
4
  export declare function toLF(text: string): string;
@@ -1,179 +1,7 @@
1
1
  /**
2
- * The edit-sequence engine shared by `edit`, `batch_edit`, and previews:
3
- * apply-one-edit against in-memory content with served verification, the
4
- * multi-edit sequencer that drives a whole file's item list against evolving
5
- * content, the noop-loop guard, and the persist-undo → write → restore
6
- * transaction both mutating tools run.
7
- *
8
- * The model-facing contract lives here unchanged: [E_BATCH_ABORT],
9
- * [E_NOOP_LOOP], [E_UNDO_UNAVAILABLE] carry byte-identical messages, and
10
- * reject-and-serve records the same echo serves.
2
+ * @deprecated — private to Mutation seam. Use `from "./mutation/engine.js"` instead.
3
+ * Thin shim re-exporting the engine now owned by Mutation.
11
4
  * @module dsh-better-edit/edit-engine
12
5
  */
13
- import type { ToolExecution } from "@deepseek-ai/dsh-tools";
14
- import type { SandboxExecutionPolicy } from "@deepseek-ai/dsh-sandbox";
15
- import type { FileIO } from "./fs-bridge.js";
16
- import type { HashStore } from "./hash-store.js";
17
- import type { LineEnding } from "./edit-diff.js";
18
- import { type HEdit, type NEdit } from "./hashline/anchor-pipeline.js";
19
- import { type ResolvedRange, type ServeRecordPolicy, type ServedRow } from "./hashline/anchor-pipeline.js";
20
- import type { FsSandboxController } from "./sandbox.js";
21
- export interface PreparedItem {
22
- index: number;
23
- path: string;
24
- absolutePath: string;
25
- remove_from: string;
26
- remove_to: string;
27
- replacement_text: string;
28
- pathWarning?: string;
29
- }
30
- export interface FileEditResult {
31
- displayPath: string;
32
- absolutePath: string;
33
- originalNormalized: string;
34
- result: string;
35
- bom: string;
36
- originalEnding: LineEnding;
37
- hadUtf8DecodeErrors: boolean;
38
- warnings: string[];
39
- originalHashes: string[];
40
- resultHashes: string[];
41
- appliedCount: number;
42
- noopCount: number;
43
- totalAddedLines: number;
44
- totalRemovedLines: number;
45
- driftNotice: string | undefined;
46
- range: ResolvedRange;
47
- }
48
- /**
49
- * Resolve a request's missing `path` from its anchors: the only file whose
50
- * stored hashes contain both anchors. Returns the path plus an autocorrect
51
- * warning, or undefined when no resolution is possible.
52
- */
53
- export declare function resolveMissingPath(request: Record<string, unknown>): Promise<{
54
- path: string;
55
- warning: string;
56
- } | undefined>;
57
- /** The hashes a range edit removes, for stable re-hash bookkeeping. */
58
- export declare function collectRemovedHashes(edit: HEdit, originalHashes: string[]): Set<string>;
59
- /** Added/removed line counts for one resolved edit against a file's original hashes. */
60
- export declare function countLineChanges(edit: HEdit, originalHashes: string[], isNoop: boolean, removedAutoFixes: number): {
61
- totalAddedLines: number;
62
- totalRemovedLines: number;
63
- };
64
- export interface ApplyOneInput {
65
- content: string;
66
- hashes: string[];
67
- served: (string | null)[];
68
- removeFrom: string;
69
- removeTo: string;
70
- replacementText: string;
71
- absolutePath: string;
72
- displayPath: string;
73
- signal?: AbortSignal;
74
- /** Shared warnings array; resEdit warnings are pushed here. */
75
- warnings: string[];
76
- /**
77
- * The hashes to count added/removed lines against. Defaults to `hashes`;
78
- * the batch sequencer passes the file's ORIGINAL hashes so later edits in
79
- * a sequence still count against the file as first served.
80
- */
81
- countHashes?: string[];
82
- store?: HashStore;
83
- persist: boolean;
84
- /** Pre-resolved edit (single path keeps resEdit before IO for error order). */
85
- edit?: HEdit;
86
- }
87
- export interface ApplyOneResult {
88
- result: string;
89
- /** Stable re-hash after the edit (equals `hashes` for a noop). */
90
- hashes: string[];
91
- range: ResolvedRange;
92
- noop: boolean;
93
- edit: HEdit;
94
- noopEdit?: NEdit;
95
- firstChangedLine?: number;
96
- lastChangedLine?: number;
97
- removedHashes: Set<string> | undefined;
98
- totalAddedLines: number;
99
- totalRemovedLines: number;
100
- anchorWarnings: string[] | undefined;
101
- }
102
- /**
103
- * One edit against in-memory content: resolve (unless a pre-resolved edit was
104
- * given) → apply with served verification → stable re-hash → line counts.
105
- *
106
- * `onReject` owns the reject-and-serve policy: it receives resolve/verify
107
- * failures (and the edit that failed, when resolved) and MUST throw. The
108
- * single path rethrows the original anchor error after recording echo serves;
109
- * the batch path wraps with [E_BATCH_ABORT] plus the current-range echo.
110
- */
111
- export declare function applyOne(input: ApplyOneInput, onReject: (error: unknown, edit: HEdit | undefined) => Promise<never>): Promise<ApplyOneResult>;
112
- export interface NoopLoopOptions {
113
- absolutePath: string;
114
- removeFrom: string;
115
- removeTo: string;
116
- replacementText: string;
117
- displayPath: string;
118
- /** Batch item index; undefined = single-edit flavor. */
119
- index?: number;
120
- count: number;
121
- sessionKey: string;
122
- originalHashes: string[];
123
- originalNormalized: string;
124
- /** Single-edit flavor only: the edit's range, for the echo rows. */
125
- range?: ResolvedRange;
126
- /** Batch flavor: precomputed echo rows for the failed item (may be absent). */
127
- echoRows?: ServedRow[];
128
- }
129
- /**
130
- * The shared noop-loop guard. Returns the "twice in a row" notice for the
131
- * caller to append to warnings, or throws [E_NOOP_LOOP] (after recording the
132
- * echo serves) once the payload has been submitted NOOP_LOOP_THRESHOLD times
133
- * with no change. Messages are byte-identical to the pre-engine tools.
134
- */
135
- export declare function enforceNoopLoop(opts: NoopLoopOptions): Promise<string | undefined>;
136
- /**
137
- * Run a file's item list against freshly-read content with served
138
- * verification, evolving content/hashes, union range, noop tracking, and a
139
- * per-file drift notice. All-or-nothing is enforced by the caller's
140
- * transaction ({@link persistUndoAndWrite}): nothing here writes to disk.
141
- */
142
- export declare function runFileEdits(io: FileIO, items: PreparedItem[], opts: {
143
- signal?: AbortSignal;
144
- sessionKey: string;
145
- }): Promise<FileEditResult>;
146
- export interface UndoWriteFile {
147
- absolutePath: string;
148
- displayPath: string;
149
- originalNormalized: string;
150
- bom: string;
151
- originalEnding: LineEnding;
152
- originalHashes: string[];
153
- result: string;
154
- }
155
- export interface PersistWriteOptions {
156
- io: FileIO;
157
- files: UndoWriteFile[];
158
- exec: ToolExecution;
159
- sandbox: FsSandboxController;
160
- sandboxPolicy: SandboxExecutionPolicy | undefined;
161
- signal?: AbortSignal;
162
- /** [E_UNDO_UNAVAILABLE] message builder, per tool flavor. */
163
- undoUnavailableMessage: (displayPath: string) => string;
164
- /**
165
- * On write failure, also restore undo entries of files that were saved but
166
- * never written. The single-edit tool restores its one entry; the batch
167
- * tool keeps current behavior and restores only written files.
168
- */
169
- restoreUnwrittenUndos?: boolean;
170
- }
171
- /**
172
- * The persist-undo → write-all → restore-on-failure transaction shared by
173
- * `edit` (one file) and `batch_edit` (many files). Every file's undo entry is
174
- * persisted before anything is written; if a write fails, already-written
175
- * files are restored (original content written back, undo entry restored) and
176
- * the sandbox-mapped error rethrown.
177
- */
178
- export declare function persistUndoAndWrite(opts: PersistWriteOptions): Promise<void>;
179
- export type { ServeRecordPolicy };
6
+ export * from "./mutation/engine.js";
7
+ export { persistUndoAndWrite, commit, commitSingle, commitBatch } from "./mutation.js";
@@ -1,4 +1,4 @@
1
- import type { ServedRow } from "./hashline/served.js";
1
+ import type { ServedRow } from "./hashline/anchor-pipeline.js";
2
2
  export type EditDetails = {
3
3
  diff: string;
4
4
  firstChangedLine?: number;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * VS Code encoding model — pure helpers for BOM→UTF-8→model-assisted top-3.
3
+ * No jschardet; heuristic scoring + iconv-lite rendering.
4
+ * @module dsh-better-edit/encoding
5
+ */
6
+ export declare const SUPPORTED_ENCODINGS: readonly ["gbk", "big5", "shift_jis", "euc-kr", "windows-1251", "iso-8859-1"];
7
+ export type SupportedEncoding = (typeof SUPPORTED_ENCODINGS)[number];
8
+ /** Canonicalize case-insensitive encoding param, strip -_ . */
9
+ export declare function normalizeEncoding(input: string): string | undefined;
10
+ export declare function isSupportedEncoding(enc: string): boolean;
11
+ export interface BomInfo {
12
+ encoding: string;
13
+ bomLen: number;
14
+ hasBOM: boolean;
15
+ }
16
+ export declare function detectBom(bytes: Uint8Array): BomInfo | undefined;
17
+ export declare function isValidUtf8(bytes: Uint8Array): boolean;
18
+ export interface CandidatePreview {
19
+ encoding: string;
20
+ sample: string;
21
+ score: number;
22
+ }
23
+ export declare function scoreText(text: string, enc: string): number;
24
+ /** Decode bytes under enc via iconv-lite, return text or undefined on failure. */
25
+ export declare function decodeBytes(bytes: Uint8Array, enc: string): string | undefined;
26
+ export declare function encodeText(text: string, enc: string): Uint8Array | undefined;
27
+ /** Top-3 candidates for autoGuess: decode each allowlist enc, score, sort. */
28
+ export declare function top3Candidates(bytes: Uint8Array, allowlist: string[]): CandidatePreview[];
29
+ export declare function hasReplacementChar(text: string): boolean;
30
+ /**
31
+ * Try chardet (runk/node-chardet) for autoGuess when available.
32
+ * Dynamically imports 'chardet' (optionalDep) and returns canonical encoding
33
+ * if confidence >=45 and allowlist-accepted and decodable without �.
34
+ * Falls back to heuristic when chardet unavailable/low confidence.
35
+ */
36
+ export declare function detectWithChardet(bytes: Uint8Array, allowlist: string[]): Promise<string | undefined>;
37
+ /**
38
+ * Chardet-based Top-3 for E_NOT_TEXT (primary) and autoGuess.
39
+ * Returns chardet candidates filtered by allowlist and decodable, else empty.
40
+ */
41
+ export declare function chardetTop3Candidates(bytes: Uint8Array, allowlist: string[]): Promise<Array<{
42
+ encoding: string;
43
+ confidence: number;
44
+ sample: string;
45
+ }>>;
46
+ export declare function getTop3Candidates(bytes: Uint8Array, allowlist: string[]): Promise<CandidatePreview[]>;