dsh-better-edit 0.8.2 → 1.1.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 (79) hide show
  1. package/CHANGELOG.md +173 -99
  2. package/README.md +61 -33
  3. package/README.zh.md +20 -17
  4. package/cordis.patch.yml +1 -1
  5. package/lib/constants.js +19 -4
  6. package/lib/contract.js +136 -79
  7. package/lib/domain-errors.js +570 -0
  8. package/lib/edit-diff.js +33 -7
  9. package/lib/file-encoding-state.js +27 -7
  10. package/lib/file-view.js +305 -82
  11. package/lib/fs-bridge.js +23 -7
  12. package/lib/guidance/materialize.js +7 -7
  13. package/lib/guidance/resolve.js +13 -8
  14. package/lib/hash-store.js +404 -278
  15. package/lib/hashline/anchor-pipeline.js +586 -378
  16. package/lib/hashline/hash-assign.js +21 -22
  17. package/lib/hashline/hash.js +6 -6
  18. package/lib/hashline/index.js +1 -1
  19. package/lib/hashline/resolve.js +1 -1
  20. package/lib/hashline/served-guard.js +181 -0
  21. package/lib/index.js +37 -11
  22. package/lib/mutation/engine.js +392 -96
  23. package/lib/mutation.js +22 -130
  24. package/lib/noop-guard.js +20 -9
  25. package/lib/prompts.js +20 -18
  26. package/lib/read-and-serve.js +44 -20
  27. package/lib/sandbox.js +1 -1
  28. package/lib/session-view.js +57 -43
  29. package/lib/snapshot-store/index.js +97 -0
  30. package/lib/snapshot-store/lineage-store.js +400 -0
  31. package/lib/snapshot-store/migrate.js +80 -0
  32. package/lib/snapshot-store/pairing.js +412 -0
  33. package/lib/snapshot-store/txn.js +63 -0
  34. package/lib/snapshot-store/vacuum.js +243 -0
  35. package/lib/store-lifecycle.js +13 -1
  36. package/lib/store-retry.js +34 -0
  37. package/lib/tool-edit.js +90 -44
  38. package/lib/tool-read.js +29 -1
  39. package/lib/tool-str-replace-editor.js +26 -18
  40. package/lib/tool-undo.js +25 -8
  41. package/lib/types/constants.d.ts +5 -2
  42. package/lib/types/contract.d.ts +76 -71
  43. package/lib/types/domain-errors.d.ts +410 -0
  44. package/lib/types/edit-response.d.ts +1 -1
  45. package/lib/types/file-encoding-state.d.ts +1 -0
  46. package/lib/types/file-view.d.ts +35 -6
  47. package/lib/types/guidance/materialize.d.ts +2 -2
  48. package/lib/types/guidance/resolve.d.ts +14 -2
  49. package/lib/types/hash-store.d.ts +38 -19
  50. package/lib/types/hashline/anchor-pipeline.d.ts +117 -36
  51. package/lib/types/hashline/hash-assign.d.ts +5 -5
  52. package/lib/types/hashline/hash.d.ts +1 -1
  53. package/lib/types/hashline/index.d.ts +2 -2
  54. package/lib/types/hashline/served-guard.d.ts +100 -0
  55. package/lib/types/index.d.ts +3 -3
  56. package/lib/types/mutation/engine.d.ts +49 -30
  57. package/lib/types/mutation.d.ts +1 -39
  58. package/lib/types/noop-guard.d.ts +4 -4
  59. package/lib/types/prompts.d.ts +1 -1
  60. package/lib/types/read-and-serve.d.ts +6 -0
  61. package/lib/types/sandbox.d.ts +1 -1
  62. package/lib/types/session-view.d.ts +38 -9
  63. package/lib/types/snapshot-store/index.d.ts +28 -0
  64. package/lib/types/snapshot-store/lineage-store.d.ts +96 -0
  65. package/lib/types/snapshot-store/migrate.d.ts +16 -0
  66. package/lib/types/snapshot-store/pairing.d.ts +99 -0
  67. package/lib/types/snapshot-store/txn.d.ts +12 -0
  68. package/lib/types/snapshot-store/vacuum.d.ts +79 -0
  69. package/lib/types/store-lifecycle.d.ts +0 -1
  70. package/lib/types/store-retry.d.ts +6 -0
  71. package/lib/types/tool-edit.d.ts +2 -2
  72. package/lib/types/tool-str-replace-editor.d.ts +2 -2
  73. package/lib/types/utils.d.ts +5 -6
  74. package/lib/types/workspace-context.d.ts +10 -1
  75. package/lib/undo-edit.js +27 -11
  76. package/lib/utils.js +12 -12
  77. package/lib/workspace-context.js +27 -9
  78. package/lib/write-hook.js +13 -4
  79. package/package.json +28 -30
package/lib/undo-edit.js CHANGED
@@ -8,20 +8,28 @@
8
8
  * @module dsh-better-edit/undo-edit
9
9
  */
10
10
  import { loadHashStore } from "./hash-store.js";
11
+ import { snapshotHashFor } from "./snapshot-store/lineage-store.js";
11
12
  /** Load the last undo row for a path from the active store, if any. */
12
13
  async function readUndo(path) {
13
14
  const store = await loadHashStore();
14
15
  return store.getUndo(path);
15
16
  }
16
- /** Persist the undo row for a path to the active store. */
17
+ /** Persist the undo rows (legacy + v7 snapshot pin) for a path to the active store. */
17
18
  async function writeUndo(path, entry) {
18
19
  const store = await loadHashStore();
19
- store.upsertUndo(path, entry);
20
+ store.writeUndoPair(path, entry, {
21
+ content: entry.content,
22
+ bom: entry.bom,
23
+ ending: entry.ending,
24
+ hashes: entry.hashes,
25
+ resultContent: entry.resultContent,
26
+ snapshotHash: snapshotHashFor(entry.content),
27
+ });
20
28
  }
21
- /** Drop the undo row for a path from the active store. */
22
- async function removeUndo(path) {
29
+ /** Load the v7 undo row for a path from the active store, if any. */
30
+ async function readFileUndo(path) {
23
31
  const store = await loadHashStore();
24
- store.deleteUndo(path);
32
+ return store.getFileUndo(path);
25
33
  }
26
34
  /**
27
35
  * Persist an undo entry for one path before mutating it.
@@ -32,8 +40,10 @@ async function removeUndo(path) {
32
40
  */
33
41
  export async function saveUndo(path, entry) {
34
42
  let previous;
43
+ let previousFileUndo;
35
44
  try {
36
45
  previous = await readUndo(path);
46
+ previousFileUndo = await readFileUndo(path);
37
47
  await writeUndo(path, {
38
48
  content: entry.content,
39
49
  bom: entry.bom,
@@ -50,10 +60,14 @@ export async function saveUndo(path, entry) {
50
60
  persisted: true,
51
61
  restore: async () => {
52
62
  try {
53
- if (previous)
54
- await writeUndo(path, previous);
55
- else
56
- await removeUndo(path);
63
+ if (previous ?? previousFileUndo) {
64
+ const store = await loadHashStore();
65
+ store.writeUndoPair(path, previous, previousFileUndo);
66
+ }
67
+ else {
68
+ const store = await loadHashStore();
69
+ store.deleteUndoPair(path);
70
+ }
57
71
  }
58
72
  catch (error) {
59
73
  console.error("Failed to restore previous undo entry:", error);
@@ -69,7 +83,8 @@ export async function getUndo(path) {
69
83
  return undefined;
70
84
  const originalEnding = record.ending;
71
85
  if (originalEnding !== "\r\n" && originalEnding !== "\n" && originalEnding !== "\r") {
72
- await removeUndo(path);
86
+ const store = await loadHashStore();
87
+ store.deleteUndoPair(path);
73
88
  return undefined;
74
89
  }
75
90
  return {
@@ -88,7 +103,8 @@ export async function getUndo(path) {
88
103
  /** Drop the undo entry for a path (a write or an undone revert clears history). */
89
104
  export async function clearUndo(path) {
90
105
  try {
91
- await removeUndo(path);
106
+ const store = await loadHashStore();
107
+ store.deleteUndoPair(path);
92
108
  }
93
109
  catch (error) {
94
110
  console.error("Failed to clear undo entry:", error);
package/lib/utils.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { DomainError } from "./domain-errors.js";
1
2
  export function isRec(value) {
2
3
  return typeof value === "object" && value !== null && !Array.isArray(value);
3
4
  }
@@ -23,7 +24,9 @@ export function rejectUnknownFields(obj, allowed, label, hint) {
23
24
  const unknown = Object.keys(obj).filter((key) => !allowed.has(key));
24
25
  if (unknown.length > 0) {
25
26
  const suffix = hint ? ` ${hint}` : "";
26
- throw new Error(`[MODEL] [E_BAD_PAYLOAD] ${label} contains unknown or unsupported fields: ${unknown.join(", ")}.${suffix}`);
27
+ throw new DomainError("E_BAD_PAYLOAD", {
28
+ message: `${label} contains unknown or unsupported fields: ${unknown.join(", ")}.${suffix}`,
29
+ });
27
30
  }
28
31
  }
29
32
  export function cntDiff(diff, marker) {
@@ -42,6 +45,8 @@ export function abortIf(signal) {
42
45
  throw new Error("Operation aborted");
43
46
  }
44
47
  export function errCode(error) {
48
+ if (error instanceof DomainError)
49
+ return error.code;
45
50
  if (error instanceof Error) {
46
51
  return error.code;
47
52
  }
@@ -73,19 +78,14 @@ export function clipLine(line, maxLen = 200) {
73
78
  const flat = line.replace(/\n/g, "\\n");
74
79
  return flat.length > maxLen ? `${flat.slice(0, maxLen)}...` : flat;
75
80
  }
76
- /** Machine-readable error-code carrier — bare `E_*` code alongside the human message. */
77
- export class CodedError extends Error {
78
- code;
79
- constructor(code, message) {
80
- super(message);
81
- this.name = "CodedError";
82
- this.code = code;
83
- }
84
- }
85
81
  const CODED_RE = /\[(E_[A-Z_]+)\]/;
86
- /** Structured bare-code read for any tool error: carrier first, `[E_*]` message convention as fallback. */
82
+ /**
83
+ * Structured bare-code read for any tool error: DomainError carrier first,
84
+ * then the `[E_*]` message convention as a legacy fallback
85
+ * registry members route the typed path; the regex is last resort).
86
+ */
87
87
  export function codeOf(error) {
88
- if (error instanceof CodedError)
88
+ if (error instanceof DomainError)
89
89
  return error.code;
90
90
  if (error instanceof Error) {
91
91
  const m = error.message.match(CODED_RE);
@@ -14,7 +14,6 @@
14
14
  *
15
15
  * @module dsh-better-edit/workspace-context
16
16
  */
17
- import { randomUUID } from "node:crypto";
18
17
  import { AsyncLocalStorage } from "node:async_hooks";
19
18
  // --- AsyncLocalStorage for cwd propagation ---
20
19
  const current = new AsyncLocalStorage();
@@ -25,20 +24,39 @@ export function workspaceCwd() {
25
24
  return current.getStore();
26
25
  }
27
26
  // --- sessionKey helpers ---
28
- let fallbackSessionKey;
27
+ // WHY: (#165) there is deliberately no fallback key: a minted key that was never served anything
28
+ // WHY: makes every lease lookup miss and surfaces a misleading E_UNKNOWN_ANCHOR. Entrypoints
29
+ // WHY: without a session fail here, at the boundary, with the real cause.
29
30
  export function sessionKeyFor(sessionId) {
30
31
  if (sessionId && sessionId.length > 0)
31
32
  return sessionId;
32
- // fallback for previews/tests
33
- return (fallbackSessionKey ??= randomUUID());
33
+ throw new Error("sessionKeyFor: tool context carries no session — entrypoints must supply the ToolExecution's session id");
34
34
  }
35
+ /** The live working directory, when the agent's scope can see the service. */
36
+ function workingDirectoryFor(agent) {
37
+ const ctx = agent?.ctx;
38
+ if (typeof ctx?.get !== "function")
39
+ return undefined;
40
+ return ctx.get("workingDirectory");
41
+ }
42
+ /**
43
+ * The directory this call operates in.
44
+ *
45
+ * Since dsh 0.2.1 the session header keeps only the directory the session was
46
+ * CREATED in — the `working_directory` tool may have moved the session since —
47
+ * so the effective directory is read from the `workingDirectory` service, whose
48
+ * synchronous `get(session)` returns the committed value. A composition without
49
+ * that service falls back to the header, where the directory lived before the
50
+ * service existed.
51
+ */
35
52
  export function execCwd(exec) {
36
- return exec.agent?.session.header.cwd ?? process.cwd();
53
+ const session = exec.agent?.session;
54
+ const workingDirectory = workingDirectoryFor(exec.agent);
55
+ if (session !== undefined && workingDirectory !== undefined) {
56
+ return workingDirectory.get(session);
57
+ }
58
+ return session?.header.cwd ?? process.cwd();
37
59
  }
38
60
  export function execSessionKey(exec) {
39
61
  return sessionKeyFor(exec.agent?.session.id);
40
62
  }
41
- // For tests: reset the fallback UUID so tests can be isolated
42
- export function _resetWorkspaceContextForTests() {
43
- fallbackSessionKey = undefined;
44
- }
package/lib/write-hook.js CHANGED
@@ -10,6 +10,7 @@ import { execCwd, execSessionKey, withWorkspace } from "./workspace-context.js";
10
10
  import { loadServed } from "./session-view.js";
11
11
  import { HASH_SEP } from "./hashline/hash-assign.js";
12
12
  import { abortIf, splitLines } from "./utils.js";
13
+ import { formatError } from "./domain-errors.js";
13
14
  const AUTO_READ_HEADING = "--- Auto-read (hashline anchors) ---";
14
15
  /**
15
16
  * Find a copied hashline prefix without treating arbitrary `3-char│` text as
@@ -40,10 +41,15 @@ export async function servedHashEchoDenial(io, rawPath, content, cwd, sessionKey
40
41
  const match = findServedHashEcho(content, served);
41
42
  if (!match)
42
43
  return undefined;
43
- return (`[MODEL] [E_SERVED_ECHO] Refused write to ${rawPath}: line ${match.line} begins with ` +
44
- `the exact ${match.hash}${HASH_SEP} anchor served for this session, path, and line. ` +
45
- `HASH${HASH_SEP} anchors are tool output, not file content. ` +
46
- "Retry with file content only (remove the entire copied anchor chain). Nothing was written.");
44
+ // A write echo names the same session/path/line the anchor was served for,
45
+ // so the served line equals the content line.
46
+ return formatError("E_SUSPICIOUS_TEXT", {
47
+ target: "write",
48
+ path: rawPath,
49
+ line: match.line,
50
+ hash: match.hash,
51
+ servedLine: match.line,
52
+ });
47
53
  }
48
54
  /**
49
55
  * Register the pre-write echo guard and post-write auto-read on the calling
@@ -65,6 +71,9 @@ export function registerWriteHook(rootCtx, agentCtx, io) {
65
71
  const content = args?.content;
66
72
  if (typeof rawPath !== "string" || typeof content !== "string")
67
73
  return next();
74
+ // F2: no mode escape here — the built-in write tool takes file_path /
75
+ // content only, so a mode branch would be dead and the refusal below
76
+ // must not promise it.
68
77
  const cwd = execCwd(exec);
69
78
  return withWorkspace(cwd, async () => {
70
79
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-better-edit",
3
- "version": "0.8.2",
3
+ "version": "1.1.0",
4
4
  "description": "Hash-anchored read/edit/undo_last_edit tools for DeepSeek Harness (dsh). Every line gets a unique 3-char hash (A-Za-z0-9) that stays stable across edits; stale or ambiguous anchors are rejected, never fuzzy-matched. Undo persists across restarts.",
5
5
  "keywords": [
6
6
  "anchors",
@@ -48,23 +48,6 @@
48
48
  },
49
49
  "./package.json": "./package.json"
50
50
  },
51
- "scripts": {
52
- "prepare": "tsc -p tsconfig.json && husky",
53
- "build": "tsc -p tsconfig.json",
54
- "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
55
- "test": "vitest run",
56
- "test:watch": "vitest",
57
- "prepublishOnly": "npm run typecheck && npm test && node scripts/assert-tagged.mjs",
58
- "benchmark": "node benchmark/run.mjs",
59
- "postpublish": "node scripts/tag-current.mjs",
60
- "release": "node scripts/release.mjs",
61
- "lint": "oxlint .",
62
- "lint:fix": "oxlint --fix .",
63
- "format": "oxfmt --check .",
64
- "format:check": "oxfmt --check .",
65
- "test:coverage": "vitest run --coverage",
66
- "format:fix": "oxfmt ."
67
- },
68
51
  "dependencies": {
69
52
  "diff": "^8.0.2",
70
53
  "file-type": "^21.3.0",
@@ -76,13 +59,13 @@
76
59
  "@biomejs/biome": "^2.1.0",
77
60
  "@commitlint/cli": "^21.2.2",
78
61
  "@commitlint/config-conventional": "^21.2.2",
79
- "@deepseek-ai/cordis": "^4.0.1",
80
- "@deepseek-ai/dsh-agent": "0.1.1-rc.2",
81
- "@deepseek-ai/dsh-fs": "0.1.1-rc.2",
82
- "@deepseek-ai/dsh-home-paths": "0.1.1-rc.2",
83
- "@deepseek-ai/dsh-sandbox": "0.1.1-rc.2",
84
- "@deepseek-ai/dsh-session": "0.1.1-rc.2",
85
- "@deepseek-ai/dsh-tools": "0.1.1-rc.2",
62
+ "@deepseek-ai/cordis": "^4.0.5",
63
+ "@deepseek-ai/dsh-agent": "0.2.1-rc.2",
64
+ "@deepseek-ai/dsh-fs": "0.2.1-rc.2",
65
+ "@deepseek-ai/dsh-home-paths": "0.2.1-rc.2",
66
+ "@deepseek-ai/dsh-sandbox": "0.2.1-rc.2",
67
+ "@deepseek-ai/dsh-session": "0.2.1-rc.2",
68
+ "@deepseek-ai/dsh-tools": "0.2.1-rc.2",
86
69
  "@semantic-release/changelog": "^7.0.0",
87
70
  "@semantic-release/git": "^11.0.1",
88
71
  "@semantic-release/github": "^12.0.9",
@@ -91,13 +74,13 @@
91
74
  "conventional-changelog-conventionalcommits": "^8.0.0",
92
75
  "husky": "^9.1.7",
93
76
  "js-tiktoken": "1.0.21",
94
- "oxfmt": ">=0.15",
77
+ "oxfmt": "0.67.0",
95
78
  "oxlint": ">=1",
96
79
  "semantic-release": "^25.0.9",
97
80
  "tsx": ">=4",
98
81
  "typescript": ">=7",
99
- "vite": ">=8",
100
- "vitest": "^4.1.8"
82
+ "vite": "^8.3.5",
83
+ "vitest": "^4.1.12"
101
84
  },
102
85
  "optionalDependencies": {
103
86
  "chardet": "2.2.0"
@@ -105,10 +88,25 @@
105
88
  "engines": {
106
89
  "node": ">=24"
107
90
  },
108
- "packageManager": "pnpm@12.0.0",
109
91
  "dsh": {
110
92
  "bundle": {
111
93
  "patch": "./cordis.patch.yml"
112
94
  }
95
+ },
96
+ "scripts": {
97
+ "build": "tsc -p tsconfig.json",
98
+ "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
99
+ "typecheck:test": "tsc --noEmit -p tsconfig.test.json",
100
+ "test": "vitest run",
101
+ "test:watch": "vitest",
102
+ "benchmark": "node benchmark/run.mjs",
103
+ "lint": "oxlint .",
104
+ "lint:fix": "oxlint --fix .",
105
+ "format": "oxfmt --check .",
106
+ "format:check": "oxfmt --check .",
107
+ "test:coverage": "vitest run --coverage",
108
+ "format:fix": "oxfmt .",
109
+ "audit": "pnpm audit --audit-level high --registry https://registry.npmjs.org/",
110
+ "verify": "pnpm run lint && pnpm run format && pnpm run typecheck && pnpm run test:coverage"
113
111
  }
114
- }
112
+ }