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.
- package/CHANGELOG.md +173 -99
- package/README.md +61 -33
- package/README.zh.md +20 -17
- package/cordis.patch.yml +1 -1
- package/lib/constants.js +19 -4
- package/lib/contract.js +136 -79
- package/lib/domain-errors.js +570 -0
- package/lib/edit-diff.js +33 -7
- package/lib/file-encoding-state.js +27 -7
- package/lib/file-view.js +305 -82
- package/lib/fs-bridge.js +23 -7
- package/lib/guidance/materialize.js +7 -7
- package/lib/guidance/resolve.js +13 -8
- package/lib/hash-store.js +404 -278
- package/lib/hashline/anchor-pipeline.js +586 -378
- package/lib/hashline/hash-assign.js +21 -22
- package/lib/hashline/hash.js +6 -6
- package/lib/hashline/index.js +1 -1
- package/lib/hashline/resolve.js +1 -1
- package/lib/hashline/served-guard.js +181 -0
- package/lib/index.js +37 -11
- package/lib/mutation/engine.js +392 -96
- package/lib/mutation.js +22 -130
- package/lib/noop-guard.js +20 -9
- package/lib/prompts.js +20 -18
- package/lib/read-and-serve.js +44 -20
- package/lib/sandbox.js +1 -1
- package/lib/session-view.js +57 -43
- package/lib/snapshot-store/index.js +97 -0
- package/lib/snapshot-store/lineage-store.js +400 -0
- package/lib/snapshot-store/migrate.js +80 -0
- package/lib/snapshot-store/pairing.js +412 -0
- package/lib/snapshot-store/txn.js +63 -0
- package/lib/snapshot-store/vacuum.js +243 -0
- package/lib/store-lifecycle.js +13 -1
- package/lib/store-retry.js +34 -0
- package/lib/tool-edit.js +90 -44
- package/lib/tool-read.js +29 -1
- package/lib/tool-str-replace-editor.js +26 -18
- package/lib/tool-undo.js +25 -8
- package/lib/types/constants.d.ts +5 -2
- package/lib/types/contract.d.ts +76 -71
- package/lib/types/domain-errors.d.ts +410 -0
- package/lib/types/edit-response.d.ts +1 -1
- package/lib/types/file-encoding-state.d.ts +1 -0
- package/lib/types/file-view.d.ts +35 -6
- package/lib/types/guidance/materialize.d.ts +2 -2
- package/lib/types/guidance/resolve.d.ts +14 -2
- package/lib/types/hash-store.d.ts +38 -19
- package/lib/types/hashline/anchor-pipeline.d.ts +117 -36
- package/lib/types/hashline/hash-assign.d.ts +5 -5
- package/lib/types/hashline/hash.d.ts +1 -1
- package/lib/types/hashline/index.d.ts +2 -2
- package/lib/types/hashline/served-guard.d.ts +100 -0
- package/lib/types/index.d.ts +3 -3
- package/lib/types/mutation/engine.d.ts +49 -30
- package/lib/types/mutation.d.ts +1 -39
- package/lib/types/noop-guard.d.ts +4 -4
- package/lib/types/prompts.d.ts +1 -1
- package/lib/types/read-and-serve.d.ts +6 -0
- package/lib/types/sandbox.d.ts +1 -1
- package/lib/types/session-view.d.ts +38 -9
- package/lib/types/snapshot-store/index.d.ts +28 -0
- package/lib/types/snapshot-store/lineage-store.d.ts +96 -0
- package/lib/types/snapshot-store/migrate.d.ts +16 -0
- package/lib/types/snapshot-store/pairing.d.ts +99 -0
- package/lib/types/snapshot-store/txn.d.ts +12 -0
- package/lib/types/snapshot-store/vacuum.d.ts +79 -0
- package/lib/types/store-lifecycle.d.ts +0 -1
- package/lib/types/store-retry.d.ts +6 -0
- package/lib/types/tool-edit.d.ts +2 -2
- package/lib/types/tool-str-replace-editor.d.ts +2 -2
- package/lib/types/utils.d.ts +5 -6
- package/lib/types/workspace-context.d.ts +10 -1
- package/lib/undo-edit.js +27 -11
- package/lib/utils.js +12 -12
- package/lib/workspace-context.js +27 -9
- package/lib/write-hook.js +13 -4
- 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
|
|
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.
|
|
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
|
-
/**
|
|
22
|
-
async function
|
|
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.
|
|
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
|
|
55
|
-
|
|
56
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
-
/**
|
|
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
|
|
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);
|
package/lib/workspace-context.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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": "
|
|
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.
|
|
80
|
-
"@deepseek-ai/dsh-agent": "0.
|
|
81
|
-
"@deepseek-ai/dsh-fs": "0.
|
|
82
|
-
"@deepseek-ai/dsh-home-paths": "0.
|
|
83
|
-
"@deepseek-ai/dsh-sandbox": "0.
|
|
84
|
-
"@deepseek-ai/dsh-session": "0.
|
|
85
|
-
"@deepseek-ai/dsh-tools": "0.
|
|
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": "
|
|
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": "
|
|
100
|
-
"vitest": "^4.1.
|
|
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
|
+
}
|