dsh-better-edit 0.4.0 → 0.4.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.
- package/CHANGELOG.md +6 -0
- package/README.md +6 -2
- package/README.zh.md +4 -1
- package/lib/index.js +2 -2
- package/lib/store-tenancy.js +12 -3
- package/lib/types/index.d.ts +2 -2
- package/lib/types/write-hook.d.ts +26 -8
- package/lib/write-hook.js +72 -11
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,12 @@ Entries link to the originating spec issue in [pi-hashline-edit-lsz](https://git
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.4.1] - 2026-08-27
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
- Refuse a built-in `write` before dispatch when a candidate line starts with the exact hashline anchor served for the same session, canonical path, and line. This prevents copied `HASH│` preview chains from entering Markdown while leaving the file byte-identical; unrelated hash-like text is not generically stripped (#29).
|
|
14
|
+
|
|
9
15
|
## [0.4.0] - 2026-08-24
|
|
10
16
|
|
|
11
17
|
### Added
|
package/README.md
CHANGED
|
@@ -255,6 +255,8 @@ Both engines preserved the external change and produced the expected final file
|
|
|
255
255
|
| `edit` | An object-root payload `{ "path": path, "edits": [[remove_from, remove_to, replacement_text], …] }`; the path may be `null` for anchor-based inference. A single item edits one range; several items batch same-file edits atomically (up to 32). Verifies every line of each inclusive range and reject-and-serve returns fresh anchors. |
|
|
256
256
|
| `undo_last_edit` | `{ path }` restores the most recent successful edit with its original content, BOM, line endings, and anchors; persisted across restarts. |
|
|
257
257
|
|
|
258
|
+
The built-in `write` remains available for full-file replacement. Before it runs, the plugin refuses only a candidate line that begins with the exact `HASH│` anchor served for the same session, canonical path, and line; the file stays unchanged and the response asks for a retry with file content only. It never generically strips hash-like text.
|
|
259
|
+
|
|
258
260
|
`edit` accepts `{ "path": path, "edits": [[remove_from, remove_to, replacement_text], …] }`. The path
|
|
259
261
|
position is a non-empty string or `null` for unique anchor-based inference. Each range is inclusive,
|
|
260
262
|
and an empty replacement deletes the range. All items are checked before file I/O and applied
|
|
@@ -276,6 +278,7 @@ atomically to that one file — one item per call is the norm, several same-file
|
|
|
276
278
|
| `[E_ACCESS]` | The path is not readable or writable. |
|
|
277
279
|
| `[E_NOT_TEXT]` | The path is a directory, binary file, image, or UTF-16/UTF-32 encoded text; hashline editing only supports text files. |
|
|
278
280
|
| `[E_NOT_OBSERVED]` | The file has not been observed in this session (read-before-write); call `read` first. |
|
|
281
|
+
| `[E_WRITE_HASH_ECHO]` | A built-in `write` candidate copied a `HASH│` preview anchor served for the same session, path, and line. The write is refused before dispatch; remove the entire copied anchor chain and retry. |
|
|
279
282
|
| `[E_UNDO_STALE]` | `undo_last_edit` refused: the file was modified or deleted after the last edit. |
|
|
280
283
|
| `[E_UNDO_UNAVAILABLE]` | Undo history could not be persisted to the hash store; the `edit` was refused and the file was left unchanged. |
|
|
281
284
|
| `[E_FILE_TOO_LARGE]` | The file exceeds the 238,328-line hashline limit. |
|
|
@@ -414,8 +417,9 @@ registration cannot replace them. This plugin:
|
|
|
414
417
|
2. On `agent/session-start`, registers the hashline tools **and** the `tool:read` / `tool:edit`
|
|
415
418
|
prompt sections on the agent's own scope layer — they shadow the preset's built-ins for that
|
|
416
419
|
agent and unwind automatically when the agent is disposed.
|
|
417
|
-
3. Leaves the built-in `write` in place
|
|
418
|
-
|
|
420
|
+
3. Leaves the built-in `write` in place: a scoped `tools/pre-execute` listener rejects an exact
|
|
421
|
+
same-session/path/line served-anchor echo before dispatch, and `tools/post-execute` appends the
|
|
422
|
+
hashline auto-read to successful results.
|
|
419
423
|
|
|
420
424
|
## Store
|
|
421
425
|
|
package/README.zh.md
CHANGED
|
@@ -267,6 +267,8 @@ token 基准测试衡量的是模型发出的负载——它假设模型每次
|
|
|
267
267
|
| `edit` | 对象根负载 `{ "path": path, "edits": [[remove_from, remove_to, replacement_text], …] }`;`path` 可为 `null` 以通过锚点推断。单个条目编辑一个范围;多条目对同一文件原子批量(最多 32)。对每个包含范围内的每一行校验,reject-and-serve 返回新锚点。 |
|
|
268
268
|
| `undo_last_edit` | `{ path }` 撤销该文件上一次 hashline 编辑,仅当文件仍与存储的编辑后内容一致时生效;重启后依然有效。 |
|
|
269
269
|
|
|
270
|
+
内置 `write` 仍用于整文件替换。执行前,插件只会在候选行以同一会话、规范路径及行位置所提供的精确 `HASH│` 锚点开头时拒绝;文件保持不变,并提示仅用文件正文重试。插件不会泛化地剥离类似哈希的文本。
|
|
271
|
+
|
|
270
272
|
### 错误码
|
|
271
273
|
|
|
272
274
|
| 代码 | 含义 |
|
|
@@ -284,6 +286,7 @@ token 基准测试衡量的是模型发出的负载——它假设模型每次
|
|
|
284
286
|
| `[E_NOT_FOUND]` | 文件不存在。 |
|
|
285
287
|
| `[E_NOT_OBSERVED]` | 该文件在本会话中尚未被观察(先读后写策略);请先调用 `read`。 |
|
|
286
288
|
| `[E_NOT_TEXT]` | 路径是目录、二进制或非 UTF-8 文件;hashline 只能编辑文本。 |
|
|
289
|
+
| `[E_WRITE_HASH_ECHO]` | 内置 `write` 候选内容复制了同一会话、路径及行位置所提供的 `HASH│` 预览锚点。写入在执行前被拒绝;移除整条复制的锚点链后重试。 |
|
|
287
290
|
| `[E_RANGE_STALE]` | 某行自被读取以来在磁盘上发生变化;范围以全新锚点回显。 |
|
|
288
291
|
| `[E_RANGE_UNSERVED]` | 范围内包含从未提供给模型的行。 |
|
|
289
292
|
| `[E_RANGE_UNVERIFIED]` | 边界锚点无法对照已提供状态验证。 |
|
|
@@ -298,7 +301,7 @@ dsh 的工具注册表按作用域解析:agent 看到的是 `agent → preset
|
|
|
298
301
|
|
|
299
302
|
1. 通过其 `cordis.patch.yml` bundle 补丁作为宿主层 Cordis 插件挂载。
|
|
300
303
|
2. 在 `agent/session-start` 时,将 hashline 工具**以及** `tool:read` / `tool:edit` 提示词片段注册到 agent 自身的作用域层——从而为该 agent 遮蔽 preset 的内置工具,并在 agent 销毁时自动解除。
|
|
301
|
-
3. 保留内置的 `write
|
|
304
|
+
3. 保留内置的 `write`,通过作用域内的 `tools/pre-execute` 监听器在执行前拒绝精确的同会话/同路径/同行锚点回显,并由 `tools/post-execute` 在成功结果后附加 hashline 自动读取。
|
|
302
305
|
|
|
303
306
|
## 存储
|
|
304
307
|
|
package/lib/index.js
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
* the AGENT's own scope layer, so they shadow the preset's built-in `read` /
|
|
8
8
|
* `edit` for that agent (nearest layer wins in dsh's tool registry) and unwind
|
|
9
9
|
* automatically when the agent is disposed. The built-in `write` stays in
|
|
10
|
-
* place;
|
|
11
|
-
* preview
|
|
10
|
+
* place; scoped pre/post listeners reject copied same-line served anchors
|
|
11
|
+
* before dispatch and append a fresh hashline preview after successful writes.
|
|
12
12
|
*
|
|
13
13
|
* The four `tool:*` guidance sections resolve per agent preset from override
|
|
14
14
|
* files in the shared home (see `src/guidance.ts`); deployments without the
|
package/lib/store-tenancy.js
CHANGED
|
@@ -51,16 +51,25 @@ export function tenancyFor(cwd) {
|
|
|
51
51
|
const cfg = loadConfig();
|
|
52
52
|
const sd = cfg.storeDir;
|
|
53
53
|
if (sd === "workspace") {
|
|
54
|
-
return {
|
|
54
|
+
return {
|
|
55
|
+
dir: join(resolvePath(cwd), ".dsh_better_edit"),
|
|
56
|
+
mode: "workspace",
|
|
57
|
+
canonical: canonicalSync(cwd),
|
|
58
|
+
};
|
|
55
59
|
}
|
|
56
60
|
const canonical = canonicalSync(cwd);
|
|
57
61
|
const h = hash8(cwd);
|
|
58
62
|
if (sd === "central") {
|
|
59
63
|
const dir = join(resolveDshHome(), "plugins", "dsh-better-edit", "runtime", `${sanitizedBasename(cwd)}-${h}`);
|
|
60
64
|
ensureWsPathSidecar(dir, canonical);
|
|
61
|
-
return {
|
|
65
|
+
return {
|
|
66
|
+
dir,
|
|
67
|
+
mode: "central",
|
|
68
|
+
runtimeDir: join(resolveDshHome(), "plugins", "dsh-better-edit", "runtime"),
|
|
69
|
+
canonical,
|
|
70
|
+
};
|
|
62
71
|
}
|
|
63
|
-
const dir = join(sd, h);
|
|
72
|
+
const dir = join(sd, `${sanitizedBasename(cwd)}-${h}`);
|
|
64
73
|
ensureWsPathSidecar(dir, canonical);
|
|
65
74
|
return { dir, mode: "custom", runtimeDir: sd, canonical };
|
|
66
75
|
}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
* the AGENT's own scope layer, so they shadow the preset's built-in `read` /
|
|
8
8
|
* `edit` for that agent (nearest layer wins in dsh's tool registry) and unwind
|
|
9
9
|
* automatically when the agent is disposed. The built-in `write` stays in
|
|
10
|
-
* place;
|
|
11
|
-
* preview
|
|
10
|
+
* place; scoped pre/post listeners reject copied same-line served anchors
|
|
11
|
+
* before dispatch and append a fresh hashline preview after successful writes.
|
|
12
12
|
*
|
|
13
13
|
* The four `tool:*` guidance sections resolve per agent preset from override
|
|
14
14
|
* files in the shared home (see `src/guidance.ts`); deployments without the
|
|
@@ -1,17 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* anchors
|
|
2
|
+
* Guard + auto-read around `write`: a scoped `tools/pre-execute` listener
|
|
3
|
+
* rejects copied hashline preview rows before they can reach disk, then a
|
|
4
|
+
* `tools/post-execute` listener re-reads successful writes and appends fresh
|
|
5
|
+
* anchors to the model-facing content (mirroring pi-hashline-edit-lsz).
|
|
6
6
|
* @module dsh-better-edit/write-hook
|
|
7
7
|
*/
|
|
8
8
|
import type { Context } from '@deepseek-ai/cordis';
|
|
9
9
|
import type { FileIO } from './fs-bridge.js';
|
|
10
|
+
export interface ServedHashEcho {
|
|
11
|
+
/** One-based candidate line carrying the copied anchor. */
|
|
12
|
+
line: number;
|
|
13
|
+
/** The exact anchor served for this session, path, and line. */
|
|
14
|
+
hash: string;
|
|
15
|
+
}
|
|
10
16
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
|
|
17
|
+
* Find a copied hashline prefix without treating arbitrary `3-char│` text as
|
|
18
|
+
* metadata. A match requires the exact hash currently recorded at the same
|
|
19
|
+
* line position for this session and canonical path.
|
|
20
|
+
*/
|
|
21
|
+
export declare function findServedHashEcho(content: string, served: readonly (string | null)[]): ServedHashEcho | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Inspect one validated-looking built-in write request against session-scoped
|
|
24
|
+
* served state. Returns a pre-dispatch denial reason only for an exact
|
|
25
|
+
* same-session / same-canonical-path / same-line anchor echo.
|
|
26
|
+
*/
|
|
27
|
+
export declare function servedHashEchoDenial(io: FileIO, rawPath: string, content: string, cwd: string, sessionKey: string, signal?: AbortSignal): Promise<string | undefined>;
|
|
28
|
+
/**
|
|
29
|
+
* Register the pre-write echo guard and post-write auto-read on the calling
|
|
30
|
+
* agent's scope. The guard denies before dispatch; the auto-read replaces only
|
|
31
|
+
* model-facing result content (never the canonical value). Infrastructure
|
|
32
|
+
* failures fail open so the plugin never breaks an otherwise valid write.
|
|
15
33
|
* @param rootCtx - host context for diagnostics.
|
|
16
34
|
* @param agentCtx - the agent's scoped context; the listener receives only
|
|
17
35
|
* this agent's tool results.
|
package/lib/write-hook.js
CHANGED
|
@@ -1,19 +1,54 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* anchors
|
|
2
|
+
* Guard + auto-read around `write`: a scoped `tools/pre-execute` listener
|
|
3
|
+
* rejects copied hashline preview rows before they can reach disk, then a
|
|
4
|
+
* `tools/post-execute` listener re-reads successful writes and appends fresh
|
|
5
|
+
* anchors to the model-facing content (mirroring pi-hashline-edit-lsz).
|
|
6
6
|
* @module dsh-better-edit/write-hook
|
|
7
7
|
*/
|
|
8
8
|
import { readAndServe } from './read-and-serve.js';
|
|
9
|
-
import { execCwd, execSessionKey } from './session-view.js';
|
|
10
|
-
import {
|
|
9
|
+
import { execCwd, execSessionKey, loadServed, withWorkspace } from './session-view.js';
|
|
10
|
+
import { HASH_SEP } from './hashline/hash-assign.js';
|
|
11
|
+
import { abortIf, splitLines } from './utils.js';
|
|
11
12
|
const AUTO_READ_HEADING = '--- Auto-read (hashline anchors) ---';
|
|
12
13
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
|
|
14
|
+
* Find a copied hashline prefix without treating arbitrary `3-char│` text as
|
|
15
|
+
* metadata. A match requires the exact hash currently recorded at the same
|
|
16
|
+
* line position for this session and canonical path.
|
|
17
|
+
*/
|
|
18
|
+
export function findServedHashEcho(content, served) {
|
|
19
|
+
const lines = splitLines(content);
|
|
20
|
+
const compared = Math.min(lines.length, served.length);
|
|
21
|
+
for (let index = 0; index < compared; index += 1) {
|
|
22
|
+
const hash = served[index];
|
|
23
|
+
if (hash !== null && lines[index].startsWith(`${hash}${HASH_SEP}`)) {
|
|
24
|
+
return { line: index + 1, hash };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Inspect one validated-looking built-in write request against session-scoped
|
|
31
|
+
* served state. Returns a pre-dispatch denial reason only for an exact
|
|
32
|
+
* same-session / same-canonical-path / same-line anchor echo.
|
|
33
|
+
*/
|
|
34
|
+
export async function servedHashEchoDenial(io, rawPath, content, cwd, sessionKey, signal) {
|
|
35
|
+
abortIf(signal);
|
|
36
|
+
const absolutePath = await io.resolve(rawPath, cwd, signal);
|
|
37
|
+
abortIf(signal);
|
|
38
|
+
const served = await loadServed(sessionKey, absolutePath);
|
|
39
|
+
const match = findServedHashEcho(content, served);
|
|
40
|
+
if (!match)
|
|
41
|
+
return undefined;
|
|
42
|
+
return (`[E_WRITE_HASH_ECHO] Refused write to ${rawPath}: line ${match.line} begins with ` +
|
|
43
|
+
`the exact ${match.hash}${HASH_SEP} anchor served for this session, path, and line. ` +
|
|
44
|
+
`HASH${HASH_SEP} anchors are tool output, not file content. ` +
|
|
45
|
+
'Retry with file content only (remove the entire copied anchor chain). Nothing was written.');
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Register the pre-write echo guard and post-write auto-read on the calling
|
|
49
|
+
* agent's scope. The guard denies before dispatch; the auto-read replaces only
|
|
50
|
+
* model-facing result content (never the canonical value). Infrastructure
|
|
51
|
+
* failures fail open so the plugin never breaks an otherwise valid write.
|
|
17
52
|
* @param rootCtx - host context for diagnostics.
|
|
18
53
|
* @param agentCtx - the agent's scoped context; the listener receives only
|
|
19
54
|
* this agent's tool results.
|
|
@@ -21,7 +56,29 @@ const AUTO_READ_HEADING = '--- Auto-read (hashline anchors) ---';
|
|
|
21
56
|
* @returns the exact disposer that unregisters the listener.
|
|
22
57
|
*/
|
|
23
58
|
export function registerWriteHook(rootCtx, agentCtx, io) {
|
|
24
|
-
|
|
59
|
+
const disposeGuard = agentCtx.on('tools/pre-execute', async (exec, next) => {
|
|
60
|
+
if (exec.name !== 'write')
|
|
61
|
+
return next();
|
|
62
|
+
const args = exec.arguments;
|
|
63
|
+
const rawPath = args?.file_path ?? args?.path;
|
|
64
|
+
const content = args?.content;
|
|
65
|
+
if (typeof rawPath !== 'string' || typeof content !== 'string')
|
|
66
|
+
return next();
|
|
67
|
+
const cwd = execCwd(exec);
|
|
68
|
+
return withWorkspace(cwd, async () => {
|
|
69
|
+
try {
|
|
70
|
+
const reason = await servedHashEchoDenial(io, rawPath, content, cwd, execSessionKey(exec), exec.signal);
|
|
71
|
+
return reason === undefined ? next() : { kind: 'deny', reason };
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (exec.signal.aborted)
|
|
75
|
+
throw error;
|
|
76
|
+
rootCtx.logger.warn(`dsh-better-edit: pre-write hash-echo guard failed open: ${error instanceof Error ? error.message : String(error)}`);
|
|
77
|
+
return next();
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
const disposeAutoRead = agentCtx.on('tools/post-execute', async (exec, result, next) => {
|
|
25
82
|
return withWorkspace(execCwd(exec), async () => {
|
|
26
83
|
const decision = await next();
|
|
27
84
|
if (exec.name !== 'write' ||
|
|
@@ -54,4 +111,8 @@ export function registerWriteHook(rootCtx, agentCtx, io) {
|
|
|
54
111
|
}
|
|
55
112
|
});
|
|
56
113
|
});
|
|
114
|
+
return () => {
|
|
115
|
+
disposeGuard();
|
|
116
|
+
disposeAutoRead();
|
|
117
|
+
};
|
|
57
118
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-better-edit",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"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.",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -63,6 +63,7 @@
|
|
|
63
63
|
"node": "^22.19.0 || >=24.0.0"
|
|
64
64
|
},
|
|
65
65
|
"scripts": {
|
|
66
|
+
"prepare": "tsc -p tsconfig.json",
|
|
66
67
|
"build": "tsc -p tsconfig.json",
|
|
67
68
|
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
|
|
68
69
|
"test": "vitest run",
|