dsh-better-edit 0.3.0 → 0.3.2
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 +27 -0
- package/README.md +9 -5
- package/README.zh.md +1 -2
- package/lib/constants.js +3 -1
- package/lib/contract.js +133 -76
- package/lib/drift.js +5 -98
- package/lib/dsh-context.js +2 -24
- package/lib/edit-engine.js +6 -5
- package/lib/edit-pipeline.js +3 -121
- package/lib/edit-response.js +16 -8
- package/lib/file-kind.js +6 -121
- package/lib/file-reader.js +4 -77
- package/lib/file-view.js +446 -23
- package/lib/fs-bridge.js +39 -1
- package/lib/guidance/materialize.js +25 -4
- package/lib/guidance/resolve.js +3 -9
- package/lib/hash-store.js +6 -3
- package/lib/hashline/alphabet.js +2 -6
- package/lib/hashline/anchor-pipeline.js +770 -7
- package/lib/hashline/apply.js +2 -223
- package/lib/hashline/hash-assign.js +248 -13
- package/lib/hashline/hash.js +67 -29
- package/lib/hashline/hasher.js +2 -23
- package/lib/hashline/index.js +7 -1
- package/lib/hashline/parse.js +2 -35
- package/lib/hashline/pure.js +2 -213
- package/lib/hashline/resolve.js +3 -366
- package/lib/hashline/served.js +2 -120
- package/lib/index.js +0 -2
- package/lib/mutation.js +218 -10
- package/lib/noop-guard.js +20 -0
- package/lib/preview-controller.js +65 -0
- package/lib/prompts.js +14 -20
- package/lib/read-render.js +3 -139
- package/lib/served-store.js +2 -216
- package/lib/session-view.js +309 -11
- package/lib/tool-edit.js +71 -125
- package/lib/tool-undo.js +11 -5
- package/lib/truncate.js +3 -95
- package/lib/types/constants.d.ts +2 -0
- package/lib/types/contract.d.ts +102 -28
- package/lib/types/drift.d.ts +5 -29
- package/lib/types/dsh-context.d.ts +2 -20
- package/lib/types/edit-pipeline.d.ts +3 -44
- package/lib/types/file-kind.d.ts +6 -18
- package/lib/types/file-reader.d.ts +4 -45
- package/lib/types/file-view.d.ts +99 -34
- package/lib/types/guidance/materialize.d.ts +3 -3
- package/lib/types/guidance/resolve.d.ts +2 -2
- package/lib/types/hashline/alphabet.d.ts +1 -5
- package/lib/types/hashline/anchor-pipeline.d.ts +111 -10
- package/lib/types/hashline/apply.d.ts +1 -22
- package/lib/types/hashline/hash-assign.d.ts +25 -19
- package/lib/types/hashline/hash.d.ts +8 -9
- package/lib/types/hashline/hasher.d.ts +1 -8
- package/lib/types/hashline/index.d.ts +7 -1
- package/lib/types/hashline/parse.d.ts +1 -7
- package/lib/types/hashline/pure.d.ts +1 -26
- package/lib/types/hashline/resolve.d.ts +1 -63
- package/lib/types/hashline/served.d.ts +1 -43
- package/lib/types/mutation.d.ts +63 -10
- package/lib/types/noop-guard.d.ts +29 -0
- package/lib/types/preview-controller.d.ts +33 -0
- package/lib/types/prompts.d.ts +9 -9
- package/lib/types/read-render.d.ts +3 -18
- package/lib/types/served-store.d.ts +2 -81
- package/lib/types/session-view.d.ts +68 -9
- package/lib/types/tool-edit.d.ts +3 -14
- package/lib/types/truncate.d.ts +3 -29
- package/lib/types/validation.d.ts +6 -7
- package/lib/types/workspace.d.ts +2 -21
- package/lib/validation.js +6 -33
- package/lib/workspace.js +2 -27
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,33 @@ Entries link to the originating spec issue in [pi-hashline-edit-lsz](https://git
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.3.2] - 2026-08-23
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
- Preserve a stripped UTF-8 BOM across `ctx.fs`-backed `edit` and `undo_last_edit` writes while retaining the file's original line endings (#23).
|
|
14
|
+
|
|
15
|
+
## [0.3.1] - 2026-08-21
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- Deepen `Mutation` owns edit lifecycle (C01, #21): collapse `tool-edit` orchestrator (260→121 lines) into `mutation.ts:execute()` deep seam — one interface owning `applySequence → commit → buildBatchResult → recordServedTruncated` with correct `undoUnavailableMessage`/`restoreUnwrittenUndos` branching; `tool-edit` thin adapter `validate → execute`.
|
|
20
|
+
|
|
21
|
+
- Heal guidance ghost (C02, #21): remove `batch_edit` seam — `GUIDANCE_SECTIONS` 4→3, `materialize.ts` ghost `batch_edit.md` cleanup (shipped + custom presets, `ENOENT`-tolerant), keep `BATCH_EDIT_*` as `@deprecated` aliases; order gap `132` preserved.
|
|
22
|
+
|
|
23
|
+
### Fixed
|
|
24
|
+
|
|
25
|
+
- Dense `servedRows` port `pi-better-edit@7b91958` (post-v1.1.4): `edit-response:buildChanged/buildBatchResult` and `tool-undo` now emit dense `0..n-1` `servedRows` instead of sparse diff rows; `firstChangedLine` fallback to `undoDiffResult` — fixes `E_RANGE_UNVERIFIED` on chained edits.
|
|
26
|
+
|
|
27
|
+
- Fix `tool-undo` SAFETY cast `canonical as unknown as FsEscalationArgs` + `withWorkspace` indentation (2→4 tabs) and `edit-response` biome fixes (no behavior change).
|
|
28
|
+
|
|
29
|
+
- Fix `guidance/resolve.ts` double-tab indent left by `batch_edit` removal on `tool:undo_last_edit` entry.
|
|
30
|
+
|
|
31
|
+
### Added
|
|
32
|
+
|
|
33
|
+
- Docs: `CLAUDE.md` upstream sync section — last absorbed checkpoint `7b91958` (dense `servedRows`), procedure, checkpoint history, `upstream` remote.
|
|
34
|
+
|
|
35
|
+
|
|
9
36
|
## [0.3.0] - 2026-08-20
|
|
10
37
|
|
|
11
38
|
### Changed
|
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="assets/
|
|
2
|
+
<img src="assets/logo.svg" alt="dsh-better-edit" width="200">
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
<h1 align="center">dsh-better-edit</h1>
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
</p>
|
|
30
30
|
|
|
31
31
|
<p align="center">
|
|
32
|
-
<img src="https://img.shields.io/badge/version-0.3.
|
|
32
|
+
<img src="https://img.shields.io/badge/version-0.3.1-blue.svg" alt="Version">
|
|
33
33
|
<img src="https://img.shields.io/badge/license-MIT-green.svg" alt="MIT License">
|
|
34
34
|
<img src="https://img.shields.io/badge/DeepSeek_Harness-Plugin-blueviolet.svg" alt="DeepSeek Harness Plugin">
|
|
35
35
|
<img src="https://img.shields.io/npm/v/dsh-better-edit" alt="npm version">
|
|
@@ -37,6 +37,10 @@
|
|
|
37
37
|
<img src="https://img.shields.io/github/stars/Rianico/dsh-better-edit?style=social" alt="GitHub Stars">
|
|
38
38
|
</p>
|
|
39
39
|
|
|
40
|
+
<p align="center">
|
|
41
|
+
<img src="assets/banner.svg" alt="file.ts → read → hashed lines → edit by hash → diff" width="900">
|
|
42
|
+
</p>
|
|
43
|
+
|
|
40
44
|
---
|
|
41
45
|
|
|
42
46
|
> *"The harness — not the model — is the bottleneck."* — Can Bölük, [*The Harness Problem*](https://stencil.so/blog/the-harness-problem)
|
|
@@ -436,7 +440,7 @@ npm run build # tsc → lib/
|
|
|
436
440
|
### Releasing (tag-first)
|
|
437
441
|
|
|
438
442
|
```sh
|
|
439
|
-
npm run release -- 0.3.
|
|
443
|
+
npm run release -- 0.3.1 # bump + CHANGELOG move + commit + tag + push → GitHub release
|
|
440
444
|
npm publish --registry https://registry.npmjs.org # blocked until the version is tagged
|
|
441
445
|
```
|
|
442
446
|
|
|
@@ -480,11 +484,11 @@ Upstream is the source of truth for reproducibility: [`pi-better-edit/benchmark/
|
|
|
480
484
|
|
|
481
485
|
## Roadmap
|
|
482
486
|
|
|
483
|
-
**Current state (0.3.
|
|
487
|
+
**Current state (0.3.1):** hashline `edit` with merged payload `{path, edits:[[hash,hash,text]]}`, whitespace-insensitive anchors, orphan healing, per-workspace store, 674 tests.
|
|
484
488
|
|
|
485
489
|
<details><summary>Next</summary>
|
|
486
490
|
|
|
487
|
-
- Verify 0.3.
|
|
491
|
+
- Verify 0.3.1 live in a dsh session after the orphan-healing + canon changes.
|
|
488
492
|
- Keep benchmark reference in sync with upstream releases (algorithm is shared).
|
|
489
493
|
- Re-check plugin wiring against the next dsh release (pinned to `0.1.0-rc.6`; dsh is in developer preview and promises breaking changes).
|
|
490
494
|
|
package/README.zh.md
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
</p>
|
|
24
24
|
|
|
25
25
|
<p align="center">
|
|
26
|
-
<img src="https://img.shields.io/badge/version-0.3.
|
|
26
|
+
<img src="https://img.shields.io/badge/version-0.3.1-blue.svg" alt="Version">
|
|
27
27
|
<img src="https://img.shields.io/badge/license-MIT-green.svg" alt="MIT License">
|
|
28
28
|
<img src="https://img.shields.io/badge/DeepSeek_Harness-Plugin-blueviolet.svg" alt="DeepSeek Harness Plugin">
|
|
29
29
|
<img src="https://img.shields.io/npm/v/dsh-better-edit" alt="npm version">
|
|
@@ -54,7 +54,6 @@ Hashline 用两个哈希代替旧文本——**编辑 token 减少 31%**(多
|
|
|
54
54
|
|
|
55
55
|
> [!TIP]
|
|
56
56
|
> **亮点——诚实且可衡量:**
|
|
57
|
-
>
|
|
58
57
|
> - **自愈而非静默。** 外部编辑永不被覆盖——过期范围被拒绝并以全新 `HASH│content` 重发重试;孤儿 served 条目无需全量重读即可自愈(ADR-0008)。Fail-closed,而非自动合并。
|
|
59
58
|
> - **格式化容忍。** ASCII 空白不敏感锚点在 `prettier`/`black`/`eslint --fix` 之间存活(`formatOnSave`、监听、CI)。仅限 linter 场景——字符串内的空白不区分(ADR-0005)。
|
|
60
59
|
> - **链式与批量,无重读仪式。** 未受影响行的锚点保持有效;diff/回显/拒绝行即视为已提供。`edit` 最多 32 个同文件编辑原子执行(`[E_BATCH_ABORT]`),相比 `str_replace` 信封约 -40%。
|
package/lib/constants.js
CHANGED
|
@@ -4,7 +4,9 @@ export const MAX_BYTES = 100 * 1024 * 1024;
|
|
|
4
4
|
export const MAX_READ_LINE_BYTES = 200 * 1024;
|
|
5
5
|
export const HASH_STORE_BUSY_TIMEOUT = 1000;
|
|
6
6
|
export const HASH_STORE_VERSION = 6;
|
|
7
|
-
export const
|
|
7
|
+
export const EDITS_MAX_ITEMS = 32;
|
|
8
|
+
/** @deprecated batch_edit seam removed (ADR-0003) — use EDITS_MAX_ITEMS */
|
|
9
|
+
export const BATCH_EDIT_MAX_ITEMS = EDITS_MAX_ITEMS;
|
|
8
10
|
export const SERVED_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
9
11
|
export const SERVED_ECHO_CAP = 150;
|
|
10
12
|
export const NOOP_LOOP_THRESHOLD = 3;
|
package/lib/contract.js
CHANGED
|
@@ -1,42 +1,74 @@
|
|
|
1
1
|
/**
|
|
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_SHAPE] vocabulary is shared instead of re-implemented per tool.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* decoupled from the tool-layer request contract so the hashline module does
|
|
10
|
-
* not depend on this one.
|
|
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.
|
|
11
9
|
* @module dsh-better-edit/contract
|
|
12
10
|
*/
|
|
13
|
-
import {
|
|
11
|
+
import { EDITS_MAX_ITEMS } from "./constants.js";
|
|
14
12
|
import { isRec, normalizeFilePath, rejectUnknownFields } from "./utils.js";
|
|
13
|
+
// ---- normalized marker -----------------------------------------------------
|
|
14
|
+
export const normalizedEdit = Symbol("normalizedEdit");
|
|
15
|
+
export function isNormalizedEdit(input) {
|
|
16
|
+
return isRec(input) && input[normalizedEdit] === true;
|
|
17
|
+
}
|
|
18
|
+
export function itemFromTuple(value) {
|
|
19
|
+
if (!Array.isArray(value) || value.length !== 3)
|
|
20
|
+
return undefined;
|
|
21
|
+
const [remove_from, remove_to, replacement_text] = value;
|
|
22
|
+
if (typeof remove_from !== "string" || typeof remove_to !== "string" || typeof replacement_text !== "string")
|
|
23
|
+
return undefined;
|
|
24
|
+
return { remove_from, remove_to, replacement_text };
|
|
25
|
+
}
|
|
26
|
+
export function editRequestFrom(input) {
|
|
27
|
+
if (!isRec(input) || !("path" in input) || !("edits" in input))
|
|
28
|
+
return undefined;
|
|
29
|
+
const rec = input;
|
|
30
|
+
// handle file_path alias before checking
|
|
31
|
+
if (typeof rec.path !== "string" && typeof rec.file_path === "string") {
|
|
32
|
+
// alias will be normalized by normalizeFilePath before editRequestFrom in normReq path,
|
|
33
|
+
// but handle here for direct calls
|
|
34
|
+
}
|
|
35
|
+
const { path, edits } = rec;
|
|
36
|
+
if (path !== null && (typeof path !== "string" || path.length === 0))
|
|
37
|
+
return undefined;
|
|
38
|
+
if (!Array.isArray(edits) || edits.length === 0)
|
|
39
|
+
return undefined;
|
|
40
|
+
const items = [];
|
|
41
|
+
for (const item of edits) {
|
|
42
|
+
const normalized = itemFromTuple(item);
|
|
43
|
+
if (!normalized)
|
|
44
|
+
return undefined;
|
|
45
|
+
items.push(normalized);
|
|
46
|
+
}
|
|
47
|
+
return { path: path, edits: items };
|
|
48
|
+
}
|
|
49
|
+
export const EDIT_TUPLE_HINT = "Edit must be called with exactly one payload. Use the canonical payload " +
|
|
50
|
+
'{"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 a " +
|
|
52
|
+
"fixed 3-position array of two inclusive bare-3-char anchors and the full " +
|
|
53
|
+
"replacement (an empty string deletes the range).";
|
|
54
|
+
function describeReceived(input) {
|
|
55
|
+
if (input === undefined)
|
|
56
|
+
return "Received no arguments.";
|
|
57
|
+
if (input === null)
|
|
58
|
+
return "Received null.";
|
|
59
|
+
if (typeof input === "string")
|
|
60
|
+
return `Received a bare string (${JSON.stringify(input)}).`;
|
|
61
|
+
const json = JSON.stringify(input);
|
|
62
|
+
const preview = typeof json === "string" && json.length > 160 ? `${json.slice(0, 160)}…` : json;
|
|
63
|
+
return `Received: ${preview}`;
|
|
64
|
+
}
|
|
15
65
|
// ---- filed sets (declared once) ---------------------------------------------
|
|
16
|
-
const EDIT_KS = new Set([
|
|
17
|
-
"path",
|
|
18
|
-
"remove_from",
|
|
19
|
-
"remove_to",
|
|
20
|
-
"replacement_text",
|
|
21
|
-
"sandbox_permissions",
|
|
22
|
-
"justification",
|
|
23
|
-
]);
|
|
24
|
-
const BATCH_ROOT_KS = new Set([
|
|
25
|
-
"edits",
|
|
26
|
-
"sandbox_permissions",
|
|
27
|
-
"justification",
|
|
28
|
-
]);
|
|
29
|
-
const BATCH_ITEM_KS = new Set([
|
|
30
|
-
"path",
|
|
31
|
-
"remove_from",
|
|
32
|
-
"remove_to",
|
|
33
|
-
"replacement_text",
|
|
34
|
-
]);
|
|
66
|
+
const EDIT_KS = new Set(["path", "edits", "sandbox_permissions", "justification"]);
|
|
35
67
|
const READ_KS = new Set(["path", "offset", "limit"]);
|
|
36
68
|
// ---- normalization -----------------------------------------------------------
|
|
37
69
|
/**
|
|
38
|
-
* Normalize `file_path` → `path` alias on the request record
|
|
39
|
-
* input unchanged when not a record; otherwise returns a shallow copy with
|
|
70
|
+
* Normalize `file_path` → `path` alias on the request record and tuple edits → objects.
|
|
71
|
+
* Returns the input unchanged when not a record; otherwise returns a shallow copy with
|
|
40
72
|
* the alias applied so callers never mutate the original `args` object.
|
|
41
73
|
*/
|
|
42
74
|
export function normalizeRequest(input) {
|
|
@@ -44,91 +76,116 @@ export function normalizeRequest(input) {
|
|
|
44
76
|
return input;
|
|
45
77
|
const record = { ...input };
|
|
46
78
|
normalizeFilePath(record);
|
|
47
|
-
|
|
79
|
+
// also normalize file_path inside edits if they were objects (legacy) — not needed for tuple but harmless
|
|
80
|
+
if (Array.isArray(record.edits)) {
|
|
81
|
+
// keep tuple as-is; editRequestFrom will handle
|
|
82
|
+
}
|
|
83
|
+
const valid = editRequestFrom(record);
|
|
84
|
+
if (!valid)
|
|
85
|
+
return record;
|
|
86
|
+
const normalized = { path: valid.path, edits: valid.edits };
|
|
87
|
+
// preserve non-standard fields like sandbox_permissions/justification for later reject check? but we strip to valid fields and re-add them?
|
|
88
|
+
for (const k of ["sandbox_permissions", "justification"]) {
|
|
89
|
+
if (k in record)
|
|
90
|
+
normalized[k] = record[k];
|
|
91
|
+
}
|
|
92
|
+
Object.defineProperty(normalized, normalizedEdit, { value: true, enumerable: false });
|
|
93
|
+
return normalized;
|
|
48
94
|
}
|
|
49
95
|
/** @deprecated use normalizeRequest — kept as alias for migration */
|
|
50
96
|
export const normReq = normalizeRequest;
|
|
97
|
+
export function prepareEditArguments(args) {
|
|
98
|
+
const valid = editRequestFrom(args);
|
|
99
|
+
if (valid) {
|
|
100
|
+
return { path: valid.path, edits: args.edits };
|
|
101
|
+
}
|
|
102
|
+
throw new Error(`[E_BAD_SHAPE] ${EDIT_TUPLE_HINT} ${describeReceived(args)}`);
|
|
103
|
+
}
|
|
51
104
|
// ---- assertions ---------------------------------------------------------------
|
|
52
105
|
export function assertEditRequest(request) {
|
|
53
|
-
if (!
|
|
54
|
-
throw new Error("[E_BAD_SHAPE] Edit request must be
|
|
106
|
+
if (!isNormalizedEdit(request)) {
|
|
107
|
+
throw new Error("[E_BAD_SHAPE] Edit request must be exactly { path, edits: [[remove_from, remove_to, replacement_text], ...] }.");
|
|
55
108
|
}
|
|
56
109
|
rejectUnknownFields(request, EDIT_KS, "Edit request");
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (typeof request.remove_from !== "string" ||
|
|
61
|
-
typeof request.remove_to !== "string" ||
|
|
62
|
-
typeof request.replacement_text !== "string") {
|
|
63
|
-
throw new Error('[E_BAD_SHAPE] Edit request requires "remove_from", "remove_to", and "replacement_text" at the top level.');
|
|
110
|
+
const req = request;
|
|
111
|
+
if (req.path !== null && (typeof req.path !== "string" || req.path.length === 0)) {
|
|
112
|
+
throw new Error('[E_BAD_SHAPE] Edit request path must be a non-empty string or null.');
|
|
64
113
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (!isRec(request)) {
|
|
68
|
-
throw new Error('[E_BAD_SHAPE] batch_edit request must be an object with an "edits" array.');
|
|
114
|
+
if (!Array.isArray(req.edits) || req.edits.length === 0) {
|
|
115
|
+
throw new Error('[E_BAD_SHAPE] Edit request requires a non-empty "edits" array.');
|
|
69
116
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
throw new Error('[E_BAD_SHAPE] batch_edit request requires a non-empty "edits" array.');
|
|
117
|
+
if (req.edits.length > EDITS_MAX_ITEMS) {
|
|
118
|
+
throw new Error(`[E_BAD_SHAPE] edit accepts at most ${EDITS_MAX_ITEMS} edits; got ${req.edits.length}. Split the batch.`);
|
|
73
119
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (!isRec(item)) {
|
|
79
|
-
throw new Error(`[E_BAD_SHAPE] edits[${index}] must be an object with remove_from, remove_to, and replacement_text.`);
|
|
80
|
-
}
|
|
81
|
-
rejectUnknownFields(item, BATCH_ITEM_KS, `edits[${index}]`);
|
|
82
|
-
if (typeof item.remove_from !== "string" ||
|
|
83
|
-
typeof item.remove_to !== "string" ||
|
|
84
|
-
typeof item.replacement_text !== "string") {
|
|
85
|
-
throw new Error(`[E_BAD_SHAPE] edits[${index}] requires "remove_from", "remove_to", and "replacement_text" strings.`);
|
|
86
|
-
}
|
|
87
|
-
if (item.path !== undefined &&
|
|
88
|
-
(typeof item.path !== "string" || item.path.length === 0)) {
|
|
89
|
-
throw new Error(`[E_BAD_SHAPE] edits[${index}].path must be a non-empty string.`);
|
|
120
|
+
for (let index = 0; index < req.edits.length; index++) {
|
|
121
|
+
const item = req.edits[index];
|
|
122
|
+
if (typeof item.remove_from !== "string" || typeof item.remove_to !== "string" || typeof item.replacement_text !== "string") {
|
|
123
|
+
throw new Error(`[E_BAD_SHAPE] Edit request edits[${index}] must be a three-position array [remove_from, remove_to, replacement_text].`);
|
|
90
124
|
}
|
|
91
|
-
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// legacy — now always fails with new shape message (batch_edit removed)
|
|
128
|
+
export function assertBatchEditRequest(_request) {
|
|
129
|
+
throw new Error("[E_BAD_SHAPE] batch_edit has been removed. Use edit with { path, edits: [[remove_from, remove_to, replacement_text], ...] }.");
|
|
92
130
|
}
|
|
93
131
|
export function assertReadRequest(request) {
|
|
94
|
-
if (!isRec(request))
|
|
132
|
+
if (!isRec(request))
|
|
95
133
|
throw new Error("[E_BAD_SHAPE] Read request must be an object.");
|
|
96
|
-
}
|
|
97
134
|
rejectUnknownFields(request, READ_KS, "Read request");
|
|
98
135
|
if (typeof request.path !== "string" || request.path.length === 0) {
|
|
99
136
|
throw new Error('[E_BAD_SHAPE] Read request requires a non-empty "path" string.');
|
|
100
137
|
}
|
|
101
138
|
}
|
|
102
139
|
export function assertUndoRequest(request) {
|
|
103
|
-
if (!isRec(request))
|
|
140
|
+
if (!isRec(request))
|
|
104
141
|
throw new Error("[E_BAD_SHAPE] undo_last_edit request must be an object.");
|
|
105
|
-
}
|
|
106
142
|
normalizeFilePath(request);
|
|
107
143
|
if (typeof request.path !== "string" || request.path.length === 0) {
|
|
108
144
|
throw new Error('[E_BAD_SHAPE] undo_last_edit request requires a non-empty "path" string.');
|
|
109
145
|
}
|
|
110
146
|
}
|
|
111
147
|
// ---- shared JSON Schema literals (co-located with field sets) ---------------
|
|
112
|
-
/**
|
|
113
|
-
* Shared model-facing parameter schemas for the hashline tools, expressed in
|
|
114
|
-
* the dsh schema DSL (not TypeBox). `path` is deliberately NOT `required` at
|
|
115
|
-
* the schema level: the tools accept the built-in `file_path` spelling too
|
|
116
|
-
* (the implicit parameter root stays open), and enforce path presence in
|
|
117
|
-
* `assertEditRequest` after `normalizeFilePath` aliasing.
|
|
118
|
-
*/
|
|
119
148
|
export const replacementTextSchema = {
|
|
120
149
|
type: 'string',
|
|
121
|
-
description: '
|
|
150
|
+
description: 'Complete replacement for the range; use "" to delete',
|
|
122
151
|
};
|
|
123
152
|
export const removeFromSchema = {
|
|
124
153
|
type: 'string',
|
|
125
|
-
description:
|
|
154
|
+
description: "First line to remove (inclusive)",
|
|
126
155
|
};
|
|
127
156
|
export const removeToSchema = {
|
|
128
157
|
type: 'string',
|
|
129
|
-
description:
|
|
158
|
+
description: "Last line to remove (inclusive)",
|
|
130
159
|
};
|
|
131
160
|
export const pathSchema = {
|
|
132
161
|
type: 'string',
|
|
133
|
-
description:
|
|
162
|
+
description: "File path; null infers it from anchors",
|
|
163
|
+
};
|
|
164
|
+
export const editPathSchema = {
|
|
165
|
+
anyOf: [
|
|
166
|
+
{ type: 'string', minLength: 1, description: "File path; null infers it from anchors" },
|
|
167
|
+
{ type: 'null', description: "null infers path from anchors" },
|
|
168
|
+
],
|
|
169
|
+
};
|
|
170
|
+
export const editTupleSchema = {
|
|
171
|
+
type: 'array',
|
|
172
|
+
prefixItems: [removeFromSchema, removeToSchema, replacementTextSchema],
|
|
173
|
+
minItems: 3,
|
|
174
|
+
maxItems: 3,
|
|
175
|
+
description: "[remove_from, remove_to, replacement_text]",
|
|
176
|
+
};
|
|
177
|
+
export const editToolSchema = {
|
|
178
|
+
type: 'object',
|
|
179
|
+
additionalProperties: false,
|
|
180
|
+
required: ["path", "edits"],
|
|
181
|
+
properties: {
|
|
182
|
+
path: editPathSchema,
|
|
183
|
+
edits: {
|
|
184
|
+
type: 'array',
|
|
185
|
+
description: "Ordered list of edit tuples",
|
|
186
|
+
minItems: 1,
|
|
187
|
+
maxItems: EDITS_MAX_ITEMS,
|
|
188
|
+
items: editTupleSchema,
|
|
189
|
+
},
|
|
190
|
+
},
|
|
134
191
|
};
|
package/lib/drift.js
CHANGED
|
@@ -1,98 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
const { served, resultHashes, resultLines, range, reported, cap = SERVED_ECHO_CAP, } = input;
|
|
7
|
-
const resultHashSet = new Set(resultHashes);
|
|
8
|
-
const currentPosOfHash = new Map();
|
|
9
|
-
for (let i = 0; i < resultHashes.length; i++) {
|
|
10
|
-
currentPosOfHash.set(resultHashes[i], i);
|
|
11
|
-
}
|
|
12
|
-
const startPositions = servedPositionsOf(served, range.startHash);
|
|
13
|
-
const endPositions = servedPositionsOf(served, range.endHash);
|
|
14
|
-
let servedStartIdx;
|
|
15
|
-
let servedEndIdx;
|
|
16
|
-
if (startPositions.length === 1 && endPositions.length === 1) {
|
|
17
|
-
servedStartIdx = startPositions[0];
|
|
18
|
-
servedEndIdx = endPositions[0];
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
servedStartIdx = range.startLine - 1;
|
|
22
|
-
servedEndIdx = range.endLine - 1;
|
|
23
|
-
}
|
|
24
|
-
const rangeFrom = Math.min(servedStartIdx, servedEndIdx);
|
|
25
|
-
const rangeTo = Math.max(servedStartIdx, servedEndIdx);
|
|
26
|
-
let total = 0;
|
|
27
|
-
let unshown = 0;
|
|
28
|
-
let anyNotReported = false;
|
|
29
|
-
const driftedPositions = [];
|
|
30
|
-
for (let p = 0; p < served.length; p++) {
|
|
31
|
-
const servedHash = served[p];
|
|
32
|
-
if (servedHash === null)
|
|
33
|
-
continue;
|
|
34
|
-
if (p >= rangeFrom && p <= rangeTo)
|
|
35
|
-
continue;
|
|
36
|
-
if (resultHashSet.has(servedHash))
|
|
37
|
-
continue;
|
|
38
|
-
total++;
|
|
39
|
-
if (!reported.has(servedHash))
|
|
40
|
-
anyNotReported = true;
|
|
41
|
-
const currentPos = currentPositionOfDrifted(served, currentPosOfHash, resultHashSet, p, range.delta);
|
|
42
|
-
if (currentPos >= 0 &&
|
|
43
|
-
currentPos < resultHashes.length &&
|
|
44
|
-
currentPos < resultLines.length) {
|
|
45
|
-
driftedPositions.push(currentPos);
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
unshown++;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (total === 0)
|
|
52
|
-
return undefined;
|
|
53
|
-
const countLabel = `${total} line(s)`;
|
|
54
|
-
if (!anyNotReported) {
|
|
55
|
-
return {
|
|
56
|
-
text: `${DRIFT_NOTICE_HEADING} ${countLabel} outside the edited range drifted and were already reported — call read to refresh.`,
|
|
57
|
-
rows: [],
|
|
58
|
-
total,
|
|
59
|
-
allAlreadyReported: true,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
const driftedSet = new Set(driftedPositions);
|
|
63
|
-
const windowSet = new Set();
|
|
64
|
-
for (const pos of driftedPositions) {
|
|
65
|
-
for (const w of [pos - 1, pos, pos + 1]) {
|
|
66
|
-
if (w >= 0 && w < resultLines.length)
|
|
67
|
-
windowSet.add(w);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
const windowPositions = [...windowSet].sort((a, b) => a - b);
|
|
71
|
-
const shownPositions = windowPositions.slice(0, cap);
|
|
72
|
-
unshown += windowPositions.length - shownPositions.length;
|
|
73
|
-
const rows = shownPositions.map((position) => ({
|
|
74
|
-
position,
|
|
75
|
-
hash: resultHashes[position],
|
|
76
|
-
content: resultLines[position],
|
|
77
|
-
drifted: driftedSet.has(position),
|
|
78
|
-
}));
|
|
79
|
-
const rowsText = fmtServedRows(rows, resultLines);
|
|
80
|
-
const moreText = unshown > 0
|
|
81
|
-
? `\n[... ${unshown} more line(s) — call read to see them]`
|
|
82
|
-
: "";
|
|
83
|
-
return {
|
|
84
|
-
text: `${DRIFT_NOTICE_HEADING} ${countLabel} outside the edited range drifted. Current content around the drift:\n${rowsText}${moreText}`,
|
|
85
|
-
rows,
|
|
86
|
-
total,
|
|
87
|
-
allAlreadyReported: false,
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
export async function scanDrift(input) {
|
|
91
|
-
const reported = await driftReported(input.sessionKey, input.path);
|
|
92
|
-
const result = computeDrift({ ...input, reported });
|
|
93
|
-
if (!result || result.allAlreadyReported)
|
|
94
|
-
return result?.text;
|
|
95
|
-
await recordServed(input.sessionKey, input.path, result.rows.map((row) => ({ position: row.position, hash: row.hash })), input.resultLines.length);
|
|
96
|
-
await markDriftReported(input.sessionKey, input.path, result.rows.filter((row) => row.drifted).map((row) => row.hash));
|
|
97
|
-
return result.text;
|
|
98
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated — private to SessionView seam. Use `from "./session-view.js"` instead.
|
|
3
|
+
* @module dsh-better-edit/drift
|
|
4
|
+
*/
|
|
5
|
+
export * from "./session-view.js";
|
package/lib/dsh-context.js
CHANGED
|
@@ -1,27 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* served-state session key derived from the dsh {@link ToolExecution}.
|
|
2
|
+
* @deprecated — private to SessionView seam. Use `from "./session-view.js"` instead.
|
|
4
3
|
* @module dsh-better-edit/dsh-context
|
|
5
4
|
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The workspace directory a model-facing filesystem tool resolves relative
|
|
9
|
-
* paths against: the calling agent's per-session cwd (each session's tools act
|
|
10
|
-
* on ITS workspace), falling back to the process cwd for non-agent callers.
|
|
11
|
-
* Mirrors how `dsh-tool-bash` defaults a bash `workdir` to the session cwd.
|
|
12
|
-
* @param exec - the tool-execution context; only its optional `agent` is read.
|
|
13
|
-
* @returns the session cwd, or the process cwd when no agent applies.
|
|
14
|
-
*/
|
|
15
|
-
export function execCwd(exec) {
|
|
16
|
-
return exec.agent?.session.header.cwd ?? process.cwd();
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* The served-state session key for one execution: the live session id. A
|
|
20
|
-
* stable key per session keeps served rows (what the model was shown) and
|
|
21
|
-
* undo history correctly scoped — never shared across sessions.
|
|
22
|
-
* @param exec - the tool-execution context.
|
|
23
|
-
* @returns the session key.
|
|
24
|
-
*/
|
|
25
|
-
export function execSessionKey(exec) {
|
|
26
|
-
return sessionKeyFor(exec.agent?.session.id);
|
|
27
|
-
}
|
|
5
|
+
export * from "./session-view.js";
|
package/lib/edit-engine.js
CHANGED
|
@@ -14,7 +14,8 @@ import { restoreEndings } from "./edit-diff.js";
|
|
|
14
14
|
import { normFromText } from "./file-reader.js";
|
|
15
15
|
import { scanDrift, loadServed } from "./session-view.js";
|
|
16
16
|
import { applyEdit, resEdit, parseHashRef, } from "./hashline/anchor-pipeline.js";
|
|
17
|
-
import { lineHashes
|
|
17
|
+
import { lineHashes } from "./hashline/hash.js";
|
|
18
|
+
import { MAX_HASH_LINES } from "./hashline/hash-assign.js";
|
|
18
19
|
import { AnchorMismatchError, ServedRejectionError, buildRangeEcho, fmtServedRows, recordEchoServes, } from "./hashline/anchor-pipeline.js";
|
|
19
20
|
import { findSnapshotPathsByHashes } from "./hash-store.js";
|
|
20
21
|
import { saveUndo } from "./undo-edit.js";
|
|
@@ -171,10 +172,10 @@ export async function enforceNoopLoop(opts) {
|
|
|
171
172
|
const echoRows = buildRangeEcho(opts.range.startLine, opts.range.endLine, originalHashes);
|
|
172
173
|
const echo = fmtServedRows(echoRows, splitLines(opts.originalNormalized));
|
|
173
174
|
await recordEchoServes(sessionKey, absolutePath, echoRows, "live", originalHashes.length);
|
|
174
|
-
throw new Error(`[E_NOOP_LOOP]
|
|
175
|
+
throw new Error(`[E_NOOP_LOOP] identical edit (${removeFrom} → ${removeTo} in ${displayPath}) submitted ${count}×, no changes each time. Range already contains this text; resend will reject. Current range:\n${echo}`);
|
|
175
176
|
}
|
|
176
177
|
if (count === 2) {
|
|
177
|
-
return `[E_NOOP_LOOP] Notice:
|
|
178
|
+
return `[E_NOOP_LOOP] Notice: identical edit (${removeFrom} → ${removeTo} in ${displayPath}) no-op'd twice; range already has this text. Resend will reject.`;
|
|
178
179
|
}
|
|
179
180
|
return undefined;
|
|
180
181
|
}
|
|
@@ -184,13 +185,13 @@ export async function enforceNoopLoop(opts) {
|
|
|
184
185
|
if (echoRows) {
|
|
185
186
|
await recordEchoServes(sessionKey, absolutePath, echoRows, "live", originalHashes.length);
|
|
186
187
|
}
|
|
187
|
-
throw new Error(`[E_NOOP_LOOP] edits[${index}] (${displayPath}):
|
|
188
|
+
throw new Error(`[E_NOOP_LOOP] edits[${index}] (${displayPath}): identical edit (${removeFrom} → ${removeTo}) submitted ${count}×, no changes each time. Range already has this text; resend will reject the batch.` +
|
|
188
189
|
(echoRows
|
|
189
190
|
? ` Current on-disk range:\n${fmtServedRows(echoRows, originalLines)}`
|
|
190
191
|
: ""));
|
|
191
192
|
}
|
|
192
193
|
if (count === 2) {
|
|
193
|
-
return `[E_NOOP_LOOP] Notice: edits[${index}] (${displayPath}) —
|
|
194
|
+
return `[E_NOOP_LOOP] Notice: edits[${index}] (${displayPath}) — identical edit no-op'd twice; range already has this text. Resend will reject the batch.`;
|
|
194
195
|
}
|
|
195
196
|
return undefined;
|
|
196
197
|
}
|