dsh-better-edit 0.6.3 → 0.8.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 +59 -15
- package/README.md +78 -71
- package/README.zh.md +83 -83
- package/lib/canonical-path.js +4 -1
- package/lib/constants.js +5 -1
- package/lib/contract.js +127 -33
- package/lib/edit-diff.js +1 -5
- package/lib/edit-response.js +5 -10
- package/lib/encoding.js +23 -6
- package/lib/file-encoding-state.js +54 -15
- package/lib/file-view.js +42 -53
- package/lib/fs-bridge.js +27 -16
- package/lib/fs-write.js +1 -1
- package/lib/guidance/materialize.js +1 -6
- package/lib/guidance/resolve.js +1 -1
- package/lib/hash-store.js +133 -43
- package/lib/hashline/anchor-pipeline.js +124 -93
- package/lib/hashline/hash-assign.js +65 -23
- package/lib/hashline/hash.js +6 -6
- package/lib/index.js +6 -0
- package/lib/mutation/engine.js +89 -31
- package/lib/mutation.js +14 -13
- package/lib/prompts.js +12 -11
- package/lib/read-and-serve.js +105 -19
- package/lib/render-text-warning.js +21 -0
- package/lib/sandbox.js +2 -5
- package/lib/self-heal.js +30 -27
- package/lib/session-view.js +161 -24
- package/lib/store-config.js +7 -2
- package/lib/store-lifecycle.js +7 -2
- package/lib/tool-edit.js +10 -6
- package/lib/tool-read.js +6 -9
- package/lib/tool-str-replace-editor.js +274 -0
- package/lib/tool-undo.js +8 -9
- package/lib/types/constants.d.ts +2 -0
- package/lib/types/contract.d.ts +106 -18
- package/lib/types/edit-response.d.ts +4 -0
- package/lib/types/encoding.d.ts +1 -1
- package/lib/types/file-encoding-state.d.ts +4 -1
- package/lib/types/file-view.d.ts +16 -1
- package/lib/types/hash-store.d.ts +12 -1
- package/lib/types/hashline/anchor-pipeline.d.ts +16 -6
- package/lib/types/hashline/hash-assign.d.ts +10 -2
- package/lib/types/hashline/index.d.ts +1 -1
- package/lib/types/mutation/engine.d.ts +2 -1
- package/lib/types/render-text-warning.d.ts +22 -0
- package/lib/types/self-heal.d.ts +2 -0
- package/lib/types/session-view.d.ts +13 -4
- package/lib/types/tool-str-replace-editor.d.ts +27 -0
- package/lib/types/undo-edit.d.ts +1 -1
- package/lib/types/utils.d.ts +7 -0
- package/lib/types/write-hook.d.ts +2 -2
- package/lib/undo-edit.js +6 -8
- package/lib/utils.js +23 -3
- package/lib/workspace-context.js +1 -1
- package/lib/write-hook.js +20 -23
- package/package.json +63 -57
package/README.zh.md
CHANGED
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
|
|
38
38
|
---
|
|
39
39
|
|
|
40
|
-
>
|
|
41
|
-
> —— Can Bölük,[
|
|
40
|
+
> _"瓶颈在于 harness——而不是模型。"_
|
|
41
|
+
> —— Can Bölük,[_The Harness Problem_]
|
|
42
42
|
>
|
|
43
43
|
> **这是 harness 的修复。** 内容哈希取代行号——上方编辑永不使下方锚点移位。每个范围都对照 Agent 实际看到的内容校验。过期或未见的行被硬拒绝并回传新锚点重试——无需 `read`。
|
|
44
44
|
>
|
|
@@ -79,11 +79,11 @@ npx @deepseek-ai/dsh plugin --profile web add /path/to/dsh-better-edit # 从
|
|
|
79
79
|
dsh --profile <name> --dump-config # 会显示 "# == dsh-better-edit" 层
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
-
| 要求
|
|
83
|
-
|
|
|
84
|
-
| Node
|
|
85
|
-
| Profile | 一个 dsh profile(首次使用 `dsh plugin` 时初始化)
|
|
86
|
-
| 后端
|
|
82
|
+
| 要求 | |
|
|
83
|
+
| ------- | -------------------------------------------------------------- |
|
|
84
|
+
| Node | `^22.19.0 \|\| >=24.0.0`(dsh 的要求;存储使用 `node:sqlite`) |
|
|
85
|
+
| Profile | 一个 dsh profile(首次使用 `dsh plugin` 时初始化) |
|
|
86
|
+
| 后端 | 支持沙箱/远程文件系统(写入经 `ctx.fs`) |
|
|
87
87
|
|
|
88
88
|
`read` 返回的每一行都带有哈希前缀——哈希*就是*这一行的地址:
|
|
89
89
|
|
|
@@ -127,11 +127,11 @@ kQm│}
|
|
|
127
127
|
|
|
128
128
|
```yaml
|
|
129
129
|
# $DSH_HOME/plugins/dsh-better-edit/config.yaml
|
|
130
|
-
storeDir: central
|
|
131
|
-
autoGitignore: false
|
|
132
|
-
undo_ttl_s: 604800
|
|
133
|
-
storeMaxAgeS: 2592000
|
|
134
|
-
storeMaxTotalBytes: 524288000
|
|
130
|
+
storeDir: central # 存储位置:"central"(默认,位于 $DSH_HOME/.../runtime/<name>-<hash8>/)| "workspace"(旧版 <ws>/.dsh_better_edit/)| "/abs/path"(自定义根目录)
|
|
131
|
+
autoGitignore: false # 仅 workspace:当 .git 存在但 .gitignore 缺少 .dsh_better_edit/ 时,true = 自动追加 ".dsh_better_edit/",false = 仅告警一次
|
|
132
|
+
undo_ttl_s: 604800 # 撤销历史 TTL(秒);-1 = 永久保留(默认 604800 = 7 天)
|
|
133
|
+
storeMaxAgeS: 2592000 # central 清理:runtime/<name>-<hash8>/ 目录最大闲置时长(秒),超时后被清理(默认 2592000 = 30 天)
|
|
134
|
+
storeMaxTotalBytes: 524288000 # central 清理:runtime/ 下总字节数上限,超限后按 LRU 清理(默认 524288000 = 500 MB)
|
|
135
135
|
```
|
|
136
136
|
|
|
137
137
|
> [!TIP]
|
|
@@ -156,11 +156,11 @@ $DSH_HOME/plugins/dsh-better-edit/<preset>/<section>.md
|
|
|
156
156
|
|
|
157
157
|
(默认主目录 `~/.dsh`,即 `~/.dsh/plugins/dsh-better-edit/`)。片段对照表:
|
|
158
158
|
|
|
159
|
-
| 文件
|
|
160
|
-
|
|
|
161
|
-
| `read.md`
|
|
162
|
-
| `edit.md`
|
|
163
|
-
| `undo_last_edit.md` | `tool:undo_last_edit` | 133
|
|
159
|
+
| 文件 | 提示词片段 | 默认 order |
|
|
160
|
+
| ------------------- | --------------------- | ---------- |
|
|
161
|
+
| `read.md` | `tool:read` | 130 |
|
|
162
|
+
| `edit.md` | `tool:edit` | 131 |
|
|
163
|
+
| `undo_last_edit.md` | `tool:undo_last_edit` | 133 |
|
|
164
164
|
|
|
165
165
|
首次启动时插件会为三个随附 preset——`standard/`、`code/`、`minimal/`、`cordis/`——各自写入编译内置的可编辑指引文件(含 `order` front-matter)。详见[配置详情](#按-preset-配置指引)的重置/恢复。完整规范:`docs/specs/issue-24-store-tenancy.md` 与指引文档。
|
|
166
166
|
|
|
@@ -194,20 +194,20 @@ $DSH_HOME/plugins/dsh-better-edit/<preset>/<section>.md
|
|
|
194
194
|
|
|
195
195
|
### 对比
|
|
196
196
|
|
|
197
|
-
|
|
|
198
|
-
|
|
|
199
|
-
| 调用中永不回显被替换文本
|
|
200
|
-
| 按什么定位行
|
|
201
|
-
| 对照模型所见内容校验
|
|
202
|
-
| 检测文件已过期
|
|
203
|
-
| 上方编辑后锚点依然有效
|
|
204
|
-
| 连续编辑无需重读
|
|
205
|
-
| 文本重复时无歧义
|
|
206
|
-
| 错行编辑永远不会悄悄落盘
|
|
207
|
-
| 块操作 / 寄存器 / `MV` / `REM` |
|
|
208
|
-
| 一次变更一个文档
|
|
209
|
-
| 运行时
|
|
210
|
-
| 撤销
|
|
197
|
+
| | hashline `edit` | `str_replace`(Claude Code / Codex) | @oh-my-pi/hashline 补丁 |
|
|
198
|
+
| ------------------------------ | :-----------------: | :----------------------------------: | :--------------------------------------: |
|
|
199
|
+
| 调用中永不回显被替换文本 | ✅ 只有两个哈希 | ❌ 逐字回显 | ✅ 只有 `+` 行 |
|
|
200
|
+
| 按什么定位行 | 内容哈希 | 文本匹配 | 行号 + 文件内容标签 |
|
|
201
|
+
| 对照模型所见内容校验 | ✅ 每一行 | ❌ 取第一个匹配 | ~ 仅文件版本 |
|
|
202
|
+
| 检测文件已过期 | ✅ 拒绝并回传新锚点 | ❌ 可能匹配到错误位置 | ✅ 标签不匹配 → 拒绝或三方合并 |
|
|
203
|
+
| 上方编辑后锚点依然有效 | ✅ 内容寻址 | ✅ 基于内容 | ❌ 重新编号 + 新标签 |
|
|
204
|
+
| 连续编辑无需重读 | ✅ diff 提供新锚点 | ~ | ~ 从编辑响应取行号 |
|
|
205
|
+
| 文本重复时无歧义 | ✅ 边界锚点需校验 | ❌ 取第一个出现 | ~ 按位置,行未逐行校验 |
|
|
206
|
+
| 错行编辑永远不会悄悄落盘 | ✅ 每一行都校验 | ❌ 取第一个匹配 | ~ 原则上可能(标签只校验版本,不校验行) |
|
|
207
|
+
| 块操作 / 寄存器 / `MV` / `REM` | ❌ | ❌ | ✅ |
|
|
208
|
+
| 一次变更一个文档 | ❌ 每次一个调用 | ❌ 每次一个调用 | ✅ 多 hunk 补丁 |
|
|
209
|
+
| 运行时 | ✅ Node(dsh) | — | ⚠️ 仅 Bun |
|
|
210
|
+
| 撤销 | ✅ 持久化 | ❌ | ❌ 不在范围内 |
|
|
211
211
|
|
|
212
212
|
> `~` = 偶尔/不稳定。`@oh-my-pi/hashline` 是一种紧凑的行锚定补丁语言([npm](https://www.npmjs.com/package/@oh-my-pi/hashline)、[仓库](https://github.com/can1357/oh-my-pi/tree/main/packages/hashline)):`[path#tag]` 头把每个 hunk 绑定到全文件内容哈希,`PUT N.=M:` 按行号定位;每次编辑都会重新编号——下一次的行号与标签取自编辑响应或重新 `read`。
|
|
213
213
|
|
|
@@ -217,15 +217,15 @@ $DSH_HOME/plugins/dsh-better-edit/<preset>/<section>.md
|
|
|
217
217
|
|
|
218
218
|
token 基准测试衡量的是模型发出的负载——它假设模型每次都能拿到**正确**的地址,而且免费。正确性才是两种 hashline 实现真正分道扬镳的地方。下面是 harness-problem 文献里的真实故障模式(错行编辑、漂移、重复文本),以及各自在遭遇它们时的表现:
|
|
219
219
|
|
|
220
|
-
| 边界情况
|
|
221
|
-
|
|
|
222
|
-
| 错误地址(锚点/行号差一行)
|
|
223
|
-
| 模型查看后文件在磁盘上被改动 | 硬拒绝 + 回传新锚点(reject-and-serve);重试无需 `read`
|
|
224
|
-
| 上方编辑导致文件移位
|
|
225
|
-
| 重复/相同文本
|
|
226
|
-
| 从未展示给模型的行
|
|
227
|
-
| 表达式中间 / 错误的块节点
|
|
228
|
-
| 多编辑批量中途失败
|
|
220
|
+
| 边界情况 | hashline `edit`(本插件) | @oh-my-pi/hashline 补丁 |
|
|
221
|
+
| ---------------------------- | ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
|
|
222
|
+
| 错误地址(锚点/行号差一行) | **不可能**——锚点解析到具体行;解析出的每一行都对照已提供状态校验,在**写入任何内容之前**被拒绝 | **可能**——当前标签下的错误行号会**悄悄**落到错误位置;标签只证明文件版本,从不证明行 |
|
|
223
|
+
| 模型查看后文件在磁盘上被改动 | 硬拒绝 + 回传新锚点(reject-and-serve);重试无需 `read` | 标签不匹配 → 拒绝**或**对未知的当前内容做尽力而为的三方合并 |
|
|
224
|
+
| 上方编辑导致文件移位 | 什么都不移位——锚点是内容地址;diff 提供新锚点 | **每次编辑都重新编号**——“RE-GROUND AFTER EVERY EDIT” 是它自己的头号规则;账由模型记 |
|
|
225
|
+
| 重复/相同文本 | 每行哈希唯一(冲突已消解);歧义 → `[E_AMBIGUOUS_ANCHOR]` | 基于位置,重复不会混淆——但位置本身未被校验 |
|
|
226
|
+
| 从未展示给模型的行 | `[E_RANGE_UNSERVED]`——硬拒绝并回传新锚点 | 未展示的 hunk 被拒绝——同样依赖模型知道自己看过什么 |
|
|
227
|
+
| 表达式中间 / 错误的块节点 | 无关——任何已校验的行范围都合法 | 语法规则 + `PUT N*:` 节点选择;点错(锚在 `def` 会让装饰器变成孤儿)会悄悄落错;无语法检查 |
|
|
228
|
+
| 多编辑批量中途失败 | `edit` 多条目——原子、全有或全无;失败项以新锚点回显 | 多段补丁先预检——同样原子 |
|
|
229
229
|
|
|
230
230
|
> oh-my-pi 42–53% 的负载节省来自更轻的线格式;上表才是该格式反过来要求模型记在脑中的东西——重新编号、追标签、选节点——而这恰恰是最容易出错的组件(替换式编辑的补丁失败率 46–51%)。本插件 31% 的代价买来的是一个“错编辑落不了地、任何拒绝都不需要重读”的契约。
|
|
231
231
|
|
|
@@ -233,25 +233,25 @@ token 基准测试衡量的是模型发出的负载——它假设模型每次
|
|
|
233
233
|
|
|
234
234
|
在同一份 103 行文件上、用相同的 12 组替换(8 个单行、4 个 3/6/10/15 行多行),以固定的 `js-tiktoken` `cl100k_base` 词表测量。三个被测方发出相同的替换文本:本插件的 `edit`(两个 3 字符锚点)、`str_replace` 工具(逐字回显旧文本)、以及 [`@oh-my-pi/hashline`](https://www.npmjs.com/package/@oh-my-pi/hashline) 的两种模式——每次编辑一个 `[path#tag]` 段(`seq`)和一个多 hunk 批量文档(`batch`):
|
|
235
235
|
|
|
236
|
-
| 指标
|
|
237
|
-
|
|
|
238
|
-
| 被替换文本是否上线
|
|
239
|
-
| 输出 token 节省(12 次编辑) |
|
|
240
|
-
| 多行范围节省(3–15 行)
|
|
241
|
-
| 按 5 倍输出计价的实际成本
|
|
242
|
-
| 范围对照已提供状态校验
|
|
243
|
-
| 模型需要跟踪的行号
|
|
244
|
-
| 确定性、可在本地复现
|
|
236
|
+
| 指标 | hashline | str_replace | oh-my-pi seq / batch |
|
|
237
|
+
| ---------------------------- | :--------------------: | :-------------: | :-------------------------: |
|
|
238
|
+
| 被替换文本是否上线 | ✅ 从不 | ❌ 每次编辑都发 | ✅ 从不 |
|
|
239
|
+
| 输出 token 节省(12 次编辑) | ✅ **31%** | ❌ 0% | ✅ **42% / 53%** |
|
|
240
|
+
| 多行范围节省(3–15 行) | ✅ **29–47%** | ❌ 0% | ✅ **40–53%** |
|
|
241
|
+
| 按 5 倍输出计价的实际成本 | ✅ **低约 1.4 倍** | ❌ 1× | ✅ **低约 1.7 倍 / 2.1 倍** |
|
|
242
|
+
| 范围对照已提供状态校验 | ✅ 100% | ❌ 无 | ~ 仅文件版本 |
|
|
243
|
+
| 模型需要跟踪的行号 | ✅ 无——内容锚点 | ✅ 无——文本匹配 | ❌ 每次编辑重新编号 |
|
|
244
|
+
| 确定性、可在本地复现 | ✅ `npm run benchmark` | — | — |
|
|
245
245
|
|
|
246
246
|
### 可复现
|
|
247
247
|
|
|
248
248
|
上面的数字**是确定性的,你可以本地复现**——`npm run benchmark`:
|
|
249
249
|
|
|
250
|
-
| 场景
|
|
251
|
-
|
|
|
252
|
-
| 单行 ×8
|
|
253
|
-
| 多行 ×4
|
|
254
|
-
| **合计 ×12** |
|
|
250
|
+
| 场景 | 行数 | hashline | str_replace | oh-my-pi seq | oh-my-pi batch |
|
|
251
|
+
| ------------ | :--: | :------: | :---------: | :----------: | :------------: |
|
|
252
|
+
| 单行 ×8 | 1 | 309 | 324 | 241 | — |
|
|
253
|
+
| 多行 ×4 | 3–15 | 393 | 691 | 349 | — |
|
|
254
|
+
| **合计 ×12** | | **702** | **1015** | **590** | **480** |
|
|
255
255
|
|
|
256
256
|
相对 `str_replace` 的节省:hashline **313(31%)** · oh-my-pi 逐次 **425(42%)** · oh-my-pi 批量 **535(53%)**。
|
|
257
257
|
|
|
@@ -261,39 +261,39 @@ token 基准测试衡量的是模型发出的负载——它假设模型每次
|
|
|
261
261
|
|
|
262
262
|
## 工具
|
|
263
263
|
|
|
264
|
-
| 工具
|
|
265
|
-
|
|
|
266
|
-
| `read`
|
|
267
|
-
| `edit`
|
|
268
|
-
| `undo_last_edit` | `{ path }` 撤销该文件上一次 hashline 编辑,仅当文件仍与存储的编辑后内容一致时生效;重启后依然有效。
|
|
264
|
+
| 工具 | 作用 |
|
|
265
|
+
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
266
|
+
| `read` | 以 `HASH│内容` 形式返回文件。参数:`offset`(1 起始)、`limit`。分页输出以 `[Showing lines N-M of T. Use offset=… to continue.]` 结尾。超过 200KB 的行显示为标记并附 `sed` 提示——哈希锚点需要完整行。 |
|
|
267
|
+
| `edit` | 对象根负载 `{ "path": path, "edits": [[remove_from, remove_to, replacement_text], …] }`;`path` 可为 `null` 以通过锚点推断。单个条目编辑一个范围;多条目对同一文件原子批量(最多 32)。对每个包含范围内的每一行校验,reject-and-serve 返回新锚点。 |
|
|
268
|
+
| `undo_last_edit` | `{ path }` 撤销该文件上一次 hashline 编辑,仅当文件仍与存储的编辑后内容一致时生效;重启后依然有效。 |
|
|
269
269
|
|
|
270
270
|
内置 `write` 仍用于整文件替换。执行前,插件只会在候选行以同一会话、规范路径及行位置所提供的精确 `HASH│` 锚点开头时拒绝;文件保持不变,并提示仅用文件正文重试。插件不会泛化地剥离类似哈希的文本。
|
|
271
271
|
|
|
272
272
|
### 错误码
|
|
273
273
|
|
|
274
|
-
| 代码
|
|
275
|
-
|
|
|
276
|
-
| `[E_ACCESS]`
|
|
277
|
-
| `[E_AMBIGUOUS_ANCHOR]` | 一个哈希匹配当前多行;调用 `read` 获取新锚点。
|
|
278
|
-
| `[E_BAD_OP]`
|
|
279
|
-
| `[E_BAD_REF]`
|
|
280
|
-
| `[E_BAD_SHAPE]`
|
|
281
|
-
| `[E_BARE_HASH_PREFIX]` | `HASH│` 前缀被粘贴进 `replacement_text`(自动纠正)。
|
|
282
|
-
| `[E_BATCH_ABORT]`
|
|
283
|
-
| `[E_FILE_TOO_LARGE]`
|
|
284
|
-
| `[E_INVALID_PATCH]`
|
|
285
|
-
| `[E_NOOP_LOOP]`
|
|
286
|
-
| `[E_NOT_FOUND]`
|
|
287
|
-
| `[E_NOT_OBSERVED]`
|
|
288
|
-
| `[E_NOT_TEXT]`
|
|
289
|
-
| `[E_WRITE_HASH_ECHO]`
|
|
290
|
-
| `[E_RANGE_STALE]`
|
|
291
|
-
| `[E_RANGE_UNSERVED]`
|
|
292
|
-
| `[E_RANGE_UNVERIFIED]` | 边界锚点无法对照已提供状态验证。
|
|
293
|
-
| `[E_STALE_ANCHOR]`
|
|
294
|
-
| `[E_UNDO_STALE]`
|
|
295
|
-
| `[E_UNDO_UNAVAILABLE]` | 撤销历史无法持久化;编辑未被应用。
|
|
296
|
-
| `[E_WOULD_EMPTY]`
|
|
274
|
+
| 代码 | 含义 |
|
|
275
|
+
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
|
|
276
|
+
| `[E_ACCESS]` | 文件存在但工具不可读/不可写。 |
|
|
277
|
+
| `[E_AMBIGUOUS_ANCHOR]` | 一个哈希匹配当前多行;调用 `read` 获取新锚点。 |
|
|
278
|
+
| `[E_BAD_OP]` | 范围结束先于范围开始(首尾颠倒时会自动纠正)。 |
|
|
279
|
+
| `[E_BAD_REF]` | `remove_from`/`remove_to` 不是裸 3 字符哈希。 |
|
|
280
|
+
| `[E_BAD_SHAPE]` | 请求/字段形态错误(未知字段、缺少 path、非字符串文本等)。 |
|
|
281
|
+
| `[E_BARE_HASH_PREFIX]` | `HASH│` 前缀被粘贴进 `replacement_text`(自动纠正)。 |
|
|
282
|
+
| `[E_BATCH_ABORT]` | 批次内某项失败;整个批次被拒绝,未写入任何内容。 |
|
|
283
|
+
| `[E_FILE_TOO_LARGE]` | 文件超过 hashline 行数上限;请改用 `write` 或其他方式。 |
|
|
284
|
+
| `[E_INVALID_PATCH]` | diff 预览标记被粘贴进 `replacement_text`(自动纠正)。 |
|
|
285
|
+
| `[E_NOOP_LOOP]` | 完全相同的编辑反复不产生任何变化;再次提交会被拒绝。 |
|
|
286
|
+
| `[E_NOT_FOUND]` | 文件不存在。 |
|
|
287
|
+
| `[E_NOT_OBSERVED]` | 该文件在本会话中尚未被观察(先读后写策略);请先调用 `read`。 |
|
|
288
|
+
| `[E_NOT_TEXT]` | 路径是目录、二进制或非 UTF-8 文件;hashline 只能编辑文本。 |
|
|
289
|
+
| `[E_WRITE_HASH_ECHO]` | 内置 `write` 候选内容复制了同一会话、路径及行位置所提供的 `HASH│` 预览锚点。写入在执行前被拒绝;移除整条复制的锚点链后重试。 |
|
|
290
|
+
| `[E_RANGE_STALE]` | 某行自被读取以来在磁盘上发生变化;范围以全新锚点回显。 |
|
|
291
|
+
| `[E_RANGE_UNSERVED]` | 范围内包含从未提供给模型的行。 |
|
|
292
|
+
| `[E_RANGE_UNVERIFIED]` | 边界锚点无法对照已提供状态验证。 |
|
|
293
|
+
| `[E_STALE_ANCHOR]` | 锚点不再能解析;调用 `read` 获取新锚点。 |
|
|
294
|
+
| `[E_UNDO_STALE]` | 无法撤销:编辑之后文件被修改(或删除)。 |
|
|
295
|
+
| `[E_UNDO_UNAVAILABLE]` | 撤销历史无法持久化;编辑未被应用。 |
|
|
296
|
+
| `[E_WOULD_EMPTY]` | 编辑会把非空文件清空;请用 `write` 清空。 |
|
|
297
297
|
|
|
298
298
|
## 如何替换内置工具
|
|
299
299
|
|
|
@@ -375,7 +375,7 @@ MIT License——详见 [LICENSE](LICENSE)。移植自 pi-hashline-edit-lsz(MI
|
|
|
375
375
|
|
|
376
376
|
## 致谢
|
|
377
377
|
|
|
378
|
-
哈希锚定编辑源于 Can Bölük 的 [
|
|
378
|
+
哈希锚定编辑源于 Can Bölük 的 [_The Harness Problem_](https://stencil.so/blog/the-harness-problem)——那篇文章证明了瓶颈在于 harness 而非模型,并证明锚定编辑优于搜索替换。本项目站在以下巨人的肩膀上:
|
|
379
379
|
|
|
380
380
|
- [**pi-hashline-edit**](https://github.com/RimuruW/pi-hashline-edit)(RimuruW)——引入 3 字符哈希与冲突消解的原创 pi-coding-agent 扩展。
|
|
381
381
|
- [**pi-hashline-edit-pro**](https://github.com/YuGiMob/pi-hashline-edit-pro)(YuGiMob)——本仓库 hashline 核心所移植自的加固版 fork。
|
package/lib/canonical-path.js
CHANGED
|
@@ -14,7 +14,10 @@ function splitAfterRoot(absolutePath) {
|
|
|
14
14
|
const { root } = parse(absolutePath);
|
|
15
15
|
return {
|
|
16
16
|
root,
|
|
17
|
-
parts: absolutePath
|
|
17
|
+
parts: absolutePath
|
|
18
|
+
.slice(root.length)
|
|
19
|
+
.split(sep)
|
|
20
|
+
.filter((p) => p.length > 0),
|
|
18
21
|
};
|
|
19
22
|
}
|
|
20
23
|
function eLoopError(ws) {
|
package/lib/constants.js
CHANGED
|
@@ -10,5 +10,9 @@ export const BATCH_EDIT_MAX_ITEMS = EDITS_MAX_ITEMS;
|
|
|
10
10
|
export const SERVED_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
11
11
|
export const SERVED_ECHO_CAP = 150;
|
|
12
12
|
export const NOOP_LOOP_THRESHOLD = 3;
|
|
13
|
-
export const NEW_CONTENT_NOT_STRING_MSG = `[
|
|
13
|
+
export const NEW_CONTENT_NOT_STRING_MSG = `[MODEL] [E_BAD_PAYLOAD] "replacement_text" must be a string with \\n line separators, not an array.` +
|
|
14
14
|
` Do not pass an array of lines — pass the replacement text as one string: "line1\\nline2". Use "" to delete a range.`;
|
|
15
|
+
export function eLargeFileMsg(displayPath, lineCount, maxLines) {
|
|
16
|
+
return `[MODEL] [E_LARGE_FILE] ${displayPath} has ${lineCount} lines, exceeding the ${maxLines}-line edit limit. Hashline editing targets source-sized files; for very large files use write or a non-line-based approach.`;
|
|
17
|
+
}
|
|
18
|
+
export const E_ANCHOR_SPACE_EXHAUSTED_WARNING = "[E_ANCHOR_SPACE_EXHAUSTED] Anchor space exhausted";
|
package/lib/contract.js
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
* One module owns the request shapes for the hashline tools — edit,
|
|
3
3
|
* read, undo_last_edit — plus their validation. Field sets are
|
|
4
4
|
* declared once here; every tool validates through these asserts, and the
|
|
5
|
-
* [
|
|
5
|
+
* [E_BAD_PAYLOAD] vocabulary is shared instead of re-implemented per tool.
|
|
6
6
|
*
|
|
7
|
-
* Contract now mirrors upstream ADR-0007: {path: string|null, edits: [[remove_from,remove_to,replacement_text],...]} tuple payload
|
|
8
|
-
* single-file, atomic. batch_edit is removed.
|
|
7
|
+
* Contract now mirrors upstream ADR-0007: {path: string|null, edits: [[remove_from,remove_to,replacement_text],...]} tuple payload
|
|
8
|
+
* (plus object-form {remove_from, remove_to, replacement_text} per #64), single-file, atomic. batch_edit is removed.
|
|
9
9
|
* @module dsh-better-edit/contract
|
|
10
10
|
*/
|
|
11
11
|
import { EDITS_MAX_ITEMS } from "./constants.js";
|
|
12
|
-
import { isRec, normalizeFilePath, rejectUnknownFields } from "./utils.js";
|
|
12
|
+
import { isRec, normalizeFilePath, rejectUnknownFields, CodedError } from "./utils.js";
|
|
13
13
|
// ---- normalized marker -----------------------------------------------------
|
|
14
14
|
export const normalizedEdit = Symbol("normalizedEdit");
|
|
15
15
|
export function isNormalizedEdit(input) {
|
|
@@ -19,10 +19,34 @@ export function itemFromTuple(value) {
|
|
|
19
19
|
if (!Array.isArray(value) || value.length !== 3)
|
|
20
20
|
return undefined;
|
|
21
21
|
const [remove_from, remove_to, replacement_text] = value;
|
|
22
|
-
if (typeof remove_from !== "string" ||
|
|
22
|
+
if (typeof remove_from !== "string" ||
|
|
23
|
+
typeof remove_to !== "string" ||
|
|
24
|
+
typeof replacement_text !== "string")
|
|
23
25
|
return undefined;
|
|
24
26
|
return { remove_from, remove_to, replacement_text };
|
|
25
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Accept both the tuple form (["a", "b", "c"]) and the object form
|
|
30
|
+
* ({ remove_from, remove_to, replacement_text }) per edits entry (#64).
|
|
31
|
+
* Missing/mistyped fields and unknown fields still return undefined so the
|
|
32
|
+
* caller rejects with E_BAD_PAYLOAD.
|
|
33
|
+
*/
|
|
34
|
+
export function itemFromEntry(value) {
|
|
35
|
+
if (Array.isArray(value))
|
|
36
|
+
return itemFromTuple(value);
|
|
37
|
+
if (isRec(value)) {
|
|
38
|
+
const rec = value;
|
|
39
|
+
const { remove_from, remove_to, replacement_text } = rec;
|
|
40
|
+
if (typeof remove_from !== "string" ||
|
|
41
|
+
typeof remove_to !== "string" ||
|
|
42
|
+
typeof replacement_text !== "string")
|
|
43
|
+
return undefined;
|
|
44
|
+
if (Object.keys(rec).some((k) => !EDIT_ITEM_KS.has(k)))
|
|
45
|
+
return undefined;
|
|
46
|
+
return { remove_from, remove_to, replacement_text };
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
26
50
|
export function editRequestFrom(input) {
|
|
27
51
|
if (!isRec(input) || !("path" in input) || !("edits" in input))
|
|
28
52
|
return undefined;
|
|
@@ -33,23 +57,75 @@ export function editRequestFrom(input) {
|
|
|
33
57
|
// but handle here for direct calls
|
|
34
58
|
}
|
|
35
59
|
const { path, edits } = rec;
|
|
36
|
-
|
|
60
|
+
// Gemma-4 tool-call bleed (#55): the model may wrap the path in <|>, │, |,
|
|
61
|
+
// quotes, or backticks after seeing │ separators. Strip wrappers iteratively.
|
|
62
|
+
let effectivePath = path;
|
|
63
|
+
if (typeof effectivePath === "string") {
|
|
64
|
+
const sanitized = sanitizePath(effectivePath);
|
|
65
|
+
if (sanitized === null)
|
|
66
|
+
return undefined;
|
|
67
|
+
effectivePath = sanitized;
|
|
68
|
+
}
|
|
69
|
+
if (effectivePath !== null &&
|
|
70
|
+
(typeof effectivePath !== "string" || effectivePath.length === 0))
|
|
37
71
|
return undefined;
|
|
38
72
|
if (!Array.isArray(edits) || edits.length === 0)
|
|
39
73
|
return undefined;
|
|
40
74
|
const items = [];
|
|
41
75
|
for (const item of edits) {
|
|
42
|
-
const normalized =
|
|
76
|
+
const normalized = itemFromEntry(item);
|
|
43
77
|
if (!normalized)
|
|
44
78
|
return undefined;
|
|
45
79
|
items.push(normalized);
|
|
46
80
|
}
|
|
47
|
-
return { path:
|
|
81
|
+
return { path: effectivePath, edits: items };
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Strip Gemma-4 tool-call bleed wrappers from a path string: `<|>`, `\u2502`, `|`
|
|
85
|
+
* pairs plus surrounding quotes/backticks, applied iteratively. Returns the
|
|
86
|
+
* cleaned path, or null when nothing usable remains (caller rejects).
|
|
87
|
+
*/
|
|
88
|
+
/** Paired/single bleed wrappers, longest-token first. Each entry strips one layer. */
|
|
89
|
+
const PATH_WRAPPERS = [
|
|
90
|
+
{ open: "<|>", close: "<|>", minLen: 6, pairOnly: false },
|
|
91
|
+
{ open: "\u2502", close: "\u2502", minLen: 2, pairOnly: true },
|
|
92
|
+
{ open: "|", close: "|", minLen: 2, pairOnly: true },
|
|
93
|
+
{ open: '"', close: '"', minLen: 0, pairOnly: true },
|
|
94
|
+
{ open: "'", close: "'", minLen: 0, pairOnly: true },
|
|
95
|
+
{ open: "`", close: "`", minLen: 0, pairOnly: true },
|
|
96
|
+
];
|
|
97
|
+
export function sanitizePath(value) {
|
|
98
|
+
if (typeof value !== "string")
|
|
99
|
+
return null;
|
|
100
|
+
let cleaned = value.trim();
|
|
101
|
+
let changed = true;
|
|
102
|
+
while (changed) {
|
|
103
|
+
changed = false;
|
|
104
|
+
for (const w of PATH_WRAPPERS) {
|
|
105
|
+
if (cleaned.length > w.minLen && cleaned.startsWith(w.open) && cleaned.endsWith(w.close)) {
|
|
106
|
+
cleaned = cleaned.slice(w.open.length, -w.close.length).trim();
|
|
107
|
+
changed = true;
|
|
108
|
+
}
|
|
109
|
+
else if (!w.pairOnly) {
|
|
110
|
+
if (cleaned.startsWith(w.open)) {
|
|
111
|
+
cleaned = cleaned.slice(w.open.length).trim();
|
|
112
|
+
changed = true;
|
|
113
|
+
}
|
|
114
|
+
else if (cleaned.endsWith(w.close)) {
|
|
115
|
+
cleaned = cleaned.slice(0, -w.close.length).trim();
|
|
116
|
+
changed = true;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return cleaned.length > 0 ? cleaned : null;
|
|
48
122
|
}
|
|
49
123
|
export const EDIT_TUPLE_HINT = "Edit must be called with exactly one payload. Use the canonical payload " +
|
|
50
124
|
'{"path": path, "edits": [[remove_from, remove_to, replacement_text], ...]}: ' +
|
|
51
|
-
"path is a non-empty string (or null to infer from anchors), each item is
|
|
52
|
-
"fixed 3-position array
|
|
125
|
+
"path is a non-empty string (or null to infer from anchors), each item is " +
|
|
126
|
+
"either a fixed 3-position array [remove_from, remove_to, replacement_text] " +
|
|
127
|
+
"or an object {remove_from, remove_to, replacement_text} (mixed batches allowed) " +
|
|
128
|
+
"of two inclusive bare-3-char anchors and the full " +
|
|
53
129
|
"replacement (an empty string deletes the range).";
|
|
54
130
|
function describeReceived(input) {
|
|
55
131
|
if (input === undefined)
|
|
@@ -65,6 +141,7 @@ function describeReceived(input) {
|
|
|
65
141
|
// ---- filed sets (declared once) ---------------------------------------------
|
|
66
142
|
const EDIT_KS = new Set(["path", "edits", "sandbox_permissions", "justification"]);
|
|
67
143
|
const READ_KS = new Set(["path", "offset", "limit", "encoding"]);
|
|
144
|
+
const EDIT_ITEM_KS = new Set(["remove_from", "remove_to", "replacement_text"]);
|
|
68
145
|
// ---- normalization -----------------------------------------------------------
|
|
69
146
|
/**
|
|
70
147
|
* Normalize `file_path` → `path` alias on the request record and tuple edits → objects.
|
|
@@ -99,93 +176,110 @@ export function prepareEditArguments(args) {
|
|
|
99
176
|
if (valid) {
|
|
100
177
|
return { path: valid.path, edits: args.edits };
|
|
101
178
|
}
|
|
102
|
-
throw new
|
|
179
|
+
throw new CodedError("E_BAD_PAYLOAD", `[MODEL] [E_BAD_PAYLOAD] ${EDIT_TUPLE_HINT} ${describeReceived(args)}`);
|
|
103
180
|
}
|
|
104
181
|
// ---- assertions ---------------------------------------------------------------
|
|
105
182
|
export function assertEditRequest(request) {
|
|
106
183
|
if (!isNormalizedEdit(request)) {
|
|
107
|
-
throw new
|
|
184
|
+
throw new CodedError("E_BAD_PAYLOAD", "[MODEL] [E_BAD_PAYLOAD] Edit request must be exactly { path, edits: [[remove_from, remove_to, replacement_text], ...] } or { path, edits: [{remove_from, remove_to, replacement_text}, ...] } (mixed batches allowed).");
|
|
108
185
|
}
|
|
109
186
|
rejectUnknownFields(request, EDIT_KS, "Edit request");
|
|
110
187
|
const req = request;
|
|
111
188
|
if (req.path !== null && (typeof req.path !== "string" || req.path.length === 0)) {
|
|
112
|
-
throw new
|
|
189
|
+
throw new CodedError("E_BAD_PAYLOAD", "[MODEL] [E_BAD_PAYLOAD] Edit request path must be a non-empty string or null.");
|
|
113
190
|
}
|
|
114
191
|
if (!Array.isArray(req.edits) || req.edits.length === 0) {
|
|
115
|
-
throw new
|
|
192
|
+
throw new CodedError("E_BAD_PAYLOAD", '[MODEL] [E_BAD_PAYLOAD] Edit request requires a non-empty "edits" array.');
|
|
116
193
|
}
|
|
117
194
|
if (req.edits.length > EDITS_MAX_ITEMS) {
|
|
118
|
-
throw new
|
|
195
|
+
throw new CodedError("E_BAD_PAYLOAD", `[MODEL] [E_BAD_PAYLOAD] edit accepts at most ${EDITS_MAX_ITEMS} edits; got ${req.edits.length}. Split the batch.`);
|
|
119
196
|
}
|
|
120
197
|
for (let index = 0; index < req.edits.length; index++) {
|
|
121
198
|
const item = req.edits[index];
|
|
122
|
-
if (typeof item.remove_from !== "string" ||
|
|
123
|
-
|
|
199
|
+
if (typeof item.remove_from !== "string" ||
|
|
200
|
+
typeof item.remove_to !== "string" ||
|
|
201
|
+
typeof item.replacement_text !== "string") {
|
|
202
|
+
throw new CodedError("E_BAD_PAYLOAD", `[MODEL] [E_BAD_PAYLOAD] Edit request edits[${index}] must be a three-position array [remove_from, remove_to, replacement_text] or an object {remove_from, remove_to, replacement_text}.`);
|
|
124
203
|
}
|
|
125
204
|
}
|
|
126
205
|
}
|
|
127
206
|
// legacy — now always fails with new shape message (batch_edit removed)
|
|
128
207
|
export function assertBatchEditRequest(_request) {
|
|
129
|
-
throw new
|
|
208
|
+
throw new CodedError("E_BAD_PAYLOAD", "[MODEL] [E_BAD_PAYLOAD] batch_edit has been removed. Use edit with { path, edits: [[remove_from, remove_to, replacement_text], ...] }.");
|
|
130
209
|
}
|
|
131
210
|
export function assertReadRequest(request) {
|
|
132
211
|
if (!isRec(request))
|
|
133
|
-
throw new
|
|
212
|
+
throw new CodedError("E_BAD_PAYLOAD", "[MODEL] [E_BAD_PAYLOAD] Read request must be an object.");
|
|
134
213
|
rejectUnknownFields(request, READ_KS, "Read request");
|
|
135
214
|
if (typeof request.path !== "string" || request.path.length === 0) {
|
|
136
|
-
throw new
|
|
215
|
+
throw new CodedError("E_BAD_PAYLOAD", '[MODEL] [E_BAD_PAYLOAD] Read request requires a non-empty "path" string.');
|
|
137
216
|
}
|
|
138
217
|
}
|
|
139
218
|
export function assertUndoRequest(request) {
|
|
140
219
|
if (!isRec(request))
|
|
141
|
-
throw new
|
|
220
|
+
throw new CodedError("E_BAD_PAYLOAD", "[MODEL] [E_BAD_PAYLOAD] undo_last_edit request must be an object.");
|
|
142
221
|
normalizeFilePath(request);
|
|
143
222
|
if (typeof request.path !== "string" || request.path.length === 0) {
|
|
144
|
-
throw new
|
|
223
|
+
throw new CodedError("E_BAD_PAYLOAD", '[MODEL] [E_BAD_PAYLOAD] undo_last_edit request requires a non-empty "path" string.');
|
|
145
224
|
}
|
|
146
225
|
}
|
|
147
226
|
// ---- shared JSON Schema literals (co-located with field sets) ---------------
|
|
148
227
|
export const replacementTextSchema = {
|
|
149
|
-
type:
|
|
228
|
+
type: "string",
|
|
150
229
|
description: 'Complete replacement for the range; use "" to delete',
|
|
151
230
|
};
|
|
152
231
|
export const removeFromSchema = {
|
|
153
|
-
type:
|
|
232
|
+
type: "string",
|
|
154
233
|
description: "First line to remove (inclusive)",
|
|
155
234
|
};
|
|
156
235
|
export const removeToSchema = {
|
|
157
|
-
type:
|
|
236
|
+
type: "string",
|
|
158
237
|
description: "Last line to remove (inclusive)",
|
|
159
238
|
};
|
|
160
239
|
export const pathSchema = {
|
|
161
|
-
type:
|
|
240
|
+
type: "string",
|
|
162
241
|
description: "File path; null infers it from anchors",
|
|
163
242
|
};
|
|
164
243
|
export const editPathSchema = {
|
|
165
244
|
anyOf: [
|
|
166
|
-
{ type:
|
|
167
|
-
{ type:
|
|
245
|
+
{ type: "string", minLength: 1, description: "File path; null infers it from anchors" },
|
|
246
|
+
{ type: "null", description: "null infers path from anchors" },
|
|
168
247
|
],
|
|
169
248
|
};
|
|
170
249
|
export const editTupleSchema = {
|
|
171
|
-
type:
|
|
250
|
+
type: "array",
|
|
172
251
|
prefixItems: [removeFromSchema, removeToSchema, replacementTextSchema],
|
|
173
252
|
minItems: 3,
|
|
174
253
|
maxItems: 3,
|
|
175
254
|
description: "[remove_from, remove_to, replacement_text]",
|
|
176
255
|
};
|
|
256
|
+
export const editObjectSchema = {
|
|
257
|
+
type: "object",
|
|
258
|
+
additionalProperties: false,
|
|
259
|
+
required: ["remove_from", "remove_to", "replacement_text"],
|
|
260
|
+
properties: {
|
|
261
|
+
remove_from: removeFromSchema,
|
|
262
|
+
remove_to: removeToSchema,
|
|
263
|
+
replacement_text: replacementTextSchema,
|
|
264
|
+
},
|
|
265
|
+
description: "{remove_from, remove_to, replacement_text}",
|
|
266
|
+
};
|
|
267
|
+
export const editItemSchema = {
|
|
268
|
+
anyOf: [editTupleSchema, editObjectSchema],
|
|
269
|
+
description: "Edit entry — either tuple or object form (mixed batches allowed)",
|
|
270
|
+
};
|
|
177
271
|
export const editToolSchema = {
|
|
178
|
-
type:
|
|
272
|
+
type: "object",
|
|
179
273
|
additionalProperties: false,
|
|
180
274
|
required: ["path", "edits"],
|
|
181
275
|
properties: {
|
|
182
276
|
path: editPathSchema,
|
|
183
277
|
edits: {
|
|
184
|
-
type:
|
|
185
|
-
description: "Ordered list of edit
|
|
278
|
+
type: "array",
|
|
279
|
+
description: "Ordered list of edit entries — each entry is either a tuple [remove_from, remove_to, replacement_text] or an object {remove_from, remove_to, replacement_text} (mixed batches allowed)",
|
|
186
280
|
minItems: 1,
|
|
187
281
|
maxItems: EDITS_MAX_ITEMS,
|
|
188
|
-
items:
|
|
282
|
+
items: editItemSchema,
|
|
189
283
|
},
|
|
190
284
|
},
|
|
191
285
|
};
|
package/lib/edit-diff.js
CHANGED
|
@@ -80,11 +80,7 @@ export function genDiff(oldContent, newContent, contextLines = 2, newContentHash
|
|
|
80
80
|
}
|
|
81
81
|
else if (nextPartIsChange && displayLines.length > contextLines * 2) {
|
|
82
82
|
const tail = displayLines.slice(-contextLines);
|
|
83
|
-
linesToShow = [
|
|
84
|
-
...displayLines.slice(0, contextLines),
|
|
85
|
-
ELLIPSIS_MARKER,
|
|
86
|
-
...tail,
|
|
87
|
-
];
|
|
83
|
+
linesToShow = [...displayLines.slice(0, contextLines), ELLIPSIS_MARKER, ...tail];
|
|
88
84
|
skipMiddle = displayLines.length - contextLines * 2;
|
|
89
85
|
}
|
|
90
86
|
else if (linesToShow.length > contextLines) {
|