dsh-better-edit 0.7.1 → 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 +40 -25
- package/README.md +72 -66
- package/README.zh.md +83 -83
- package/lib/canonical-path.js +4 -1
- package/lib/contract.js +67 -22
- package/lib/edit-diff.js +1 -5
- package/lib/encoding.js +22 -5
- package/lib/file-encoding-state.js +50 -11
- package/lib/file-view.js +28 -41
- package/lib/fs-bridge.js +19 -9
- 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 +12 -20
- package/lib/hashline/anchor-pipeline.js +29 -29
- package/lib/hashline/hash-assign.js +7 -4
- package/lib/hashline/hash.js +3 -3
- package/lib/index.js +6 -0
- package/lib/mutation/engine.js +11 -14
- package/lib/mutation.js +7 -6
- package/lib/prompts.js +9 -8
- package/lib/read-and-serve.js +22 -11
- 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 +5 -1
- package/lib/store-config.js +7 -2
- package/lib/store-lifecycle.js +7 -2
- package/lib/tool-edit.js +5 -2
- package/lib/tool-read.js +6 -9
- package/lib/tool-str-replace-editor.js +274 -0
- package/lib/tool-undo.js +2 -3
- package/lib/types/contract.d.ts +104 -17
- package/lib/types/file-encoding-state.d.ts +3 -0
- package/lib/types/file-view.d.ts +1 -1
- package/lib/types/hashline/index.d.ts +1 -1
- package/lib/types/render-text-warning.d.ts +22 -0
- package/lib/types/self-heal.d.ts +2 -0
- package/lib/types/tool-str-replace-editor.d.ts +27 -0
- package/lib/types/undo-edit.d.ts +1 -1
- package/lib/types/write-hook.d.ts +2 -2
- package/lib/undo-edit.js +6 -8
- package/lib/utils.js +1 -2
- package/lib/workspace-context.js +1 -1
- package/lib/write-hook.js +19 -22
- package/package.json +63 -57
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dsh `str_replace_editor` shadow: a governed agent-layer replacement for
|
|
3
|
+
* the built-in `@deepseek-ai/dsh-tool-str-replace-editor` with identical
|
|
4
|
+
* contract (`view` / `str_replace` / `insert` / `create`, no new `encoding`
|
|
5
|
+
* param) but routed through the file-encoding-state seam.
|
|
6
|
+
*
|
|
7
|
+
* - `view` is observation: decode via the `ctxFsIO` path (`decodeForOpen`
|
|
8
|
+
* inside `io.readText`), records file encoding state, emits `fs/observed`,
|
|
9
|
+
* and never records served state (so it can feed `write` round-trips but
|
|
10
|
+
* cannot authorize hashline `edit` anchors).
|
|
11
|
+
* - `str_replace` / `insert` without a prior `view`/`read` fail loud with
|
|
12
|
+
* `E_BLIND_REPLACE` — no disk write.
|
|
13
|
+
* - `create` defaults to UTF-8 without BOM.
|
|
14
|
+
* - `undo_edit` is out of scope → `E_UNSUPPORTED` (use `undo_last_edit`).
|
|
15
|
+
*
|
|
16
|
+
* See ADR-0015.
|
|
17
|
+
* @module dsh-better-edit/tool-str-replace-editor
|
|
18
|
+
*/
|
|
19
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
20
|
+
import { getAutoGuessFooter } from "./fs-bridge.js";
|
|
21
|
+
import { getEncodingState, invalidateIfStale, prepareForSave, recordOpenState, } from "./file-encoding-state.js";
|
|
22
|
+
import { stripBOM } from "./edit-diff.js";
|
|
23
|
+
import { renderTextWarning } from "./render-text-warning.js";
|
|
24
|
+
import { execCwd, withWorkspace } from "./workspace-context.js";
|
|
25
|
+
import { abortIf, splitLines } from "./utils.js";
|
|
26
|
+
export const STR_REPLACE_EDITOR_DESCRIPTION = "View, create, and edit text files by exact string match. Commands: " +
|
|
27
|
+
"`view` {path, view_range?} shows 1-indexed lines; `str_replace` {path, old_str, new_str} " +
|
|
28
|
+
"replaces the unique occurrence of old_str; `insert` {path, insert_line, new_str} inserts " +
|
|
29
|
+
"new line(s) before insert_line (1-indexed, lines+1 appends); `create` {path, file_text} " +
|
|
30
|
+
"creates a new file (fails if it exists). str_replace/insert require a prior view of the " +
|
|
31
|
+
"file in this session (E_BLIND_REPLACE otherwise). Encoding is governed: non-UTF-8 files " +
|
|
32
|
+
"decode via auto-guess when enabled, otherwise fail loud with Top-3 candidates.";
|
|
33
|
+
const KNOWN_COMMANDS = new Set([
|
|
34
|
+
"view",
|
|
35
|
+
"str_replace",
|
|
36
|
+
"insert",
|
|
37
|
+
"create",
|
|
38
|
+
"undo_edit",
|
|
39
|
+
]);
|
|
40
|
+
function argError(code, message) {
|
|
41
|
+
return new Error(`[MODEL] [${code}] ${message}`);
|
|
42
|
+
}
|
|
43
|
+
function requireString(args, name) {
|
|
44
|
+
const value = args[name];
|
|
45
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
46
|
+
throw argError("E_BAD_PAYLOAD", `str_replace_editor: "${name}" must be a non-empty string.`);
|
|
47
|
+
}
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
function requireViewRange(args) {
|
|
51
|
+
const value = args["view_range"];
|
|
52
|
+
if (value === undefined)
|
|
53
|
+
return undefined;
|
|
54
|
+
if (!Array.isArray(value) ||
|
|
55
|
+
value.length !== 2 ||
|
|
56
|
+
!Number.isInteger(value[0]) ||
|
|
57
|
+
!Number.isInteger(value[1]) ||
|
|
58
|
+
value[0] < 1 ||
|
|
59
|
+
value[1] < 1 ||
|
|
60
|
+
value[0] > value[1]) {
|
|
61
|
+
throw argError("E_BAD_PAYLOAD", 'str_replace_editor: "view_range" must be [start, end] with 1-indexed positive integers and start <= end.');
|
|
62
|
+
}
|
|
63
|
+
return [value[0], value[1]];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Strict read-first gate: the file must carry file encoding state recorded by
|
|
67
|
+
* a prior `view`/`read` at the current version. Stale memos are invalidated
|
|
68
|
+
* first, so drifted files fail loud instead of editing blind. Never writes.
|
|
69
|
+
*/
|
|
70
|
+
async function requireObserved(io, absolutePath, displayPath, signal) {
|
|
71
|
+
const version = await io.statVersion(absolutePath, signal);
|
|
72
|
+
invalidateIfStale(absolutePath, version);
|
|
73
|
+
if (!getEncodingState(absolutePath)) {
|
|
74
|
+
throw argError("E_BLIND_REPLACE", `str_replace_editor: ${displayPath} has not been viewed in this session (no file encoding state at the current version). Call view first, then retry. Nothing was written.`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function countOccurrences(haystack, needle) {
|
|
78
|
+
if (needle.length === 0)
|
|
79
|
+
return 0;
|
|
80
|
+
let count = 0;
|
|
81
|
+
let from = 0;
|
|
82
|
+
while (true) {
|
|
83
|
+
const at = haystack.indexOf(needle, from);
|
|
84
|
+
if (at === -1)
|
|
85
|
+
return count;
|
|
86
|
+
count += 1;
|
|
87
|
+
from = at + needle.length;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** Shared-seam save restoration: BOM + lineEnding via `prepareForSave`.
|
|
91
|
+
* Replaces the former local `restoreBom` so the shadow can't diverge from
|
|
92
|
+
* the file-encoding-state round-trip (report #2). */
|
|
93
|
+
function restoreForSave(content, absolutePath) {
|
|
94
|
+
return prepareForSave(content, getEncodingState(absolutePath)).textToWrite;
|
|
95
|
+
}
|
|
96
|
+
function autoGuessWarning(absolutePath, rawPath) {
|
|
97
|
+
return (getAutoGuessFooter(absolutePath) ??
|
|
98
|
+
getAutoGuessFooter(rawPath) ??
|
|
99
|
+
getAutoGuessFooter(absolutePath.replace(/\\/g, "/")) ??
|
|
100
|
+
undefined);
|
|
101
|
+
}
|
|
102
|
+
export function buildStrReplaceEditorTool(io) {
|
|
103
|
+
return defineTool({
|
|
104
|
+
name: "str_replace_editor",
|
|
105
|
+
description: STR_REPLACE_EDITOR_DESCRIPTION,
|
|
106
|
+
parameters: {
|
|
107
|
+
command: {
|
|
108
|
+
type: "string",
|
|
109
|
+
required: true,
|
|
110
|
+
description: "One of: view, str_replace, insert, create, undo_edit",
|
|
111
|
+
},
|
|
112
|
+
path: {
|
|
113
|
+
type: "string",
|
|
114
|
+
required: true,
|
|
115
|
+
description: "File path",
|
|
116
|
+
},
|
|
117
|
+
view_range: {
|
|
118
|
+
type: "json",
|
|
119
|
+
description: "[start, end] 1-indexed inclusive line range for view",
|
|
120
|
+
},
|
|
121
|
+
old_str: {
|
|
122
|
+
type: "string",
|
|
123
|
+
description: "Exact string to replace (must occur exactly once) for str_replace",
|
|
124
|
+
},
|
|
125
|
+
new_str: {
|
|
126
|
+
type: "string",
|
|
127
|
+
description: "Replacement text for str_replace, or line(s) to insert for insert",
|
|
128
|
+
},
|
|
129
|
+
insert_line: {
|
|
130
|
+
type: "number",
|
|
131
|
+
description: "1-indexed line to insert before (lines+1 appends) for insert",
|
|
132
|
+
},
|
|
133
|
+
file_text: {
|
|
134
|
+
type: "string",
|
|
135
|
+
description: "Full content for create",
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
output: {
|
|
139
|
+
schema: {
|
|
140
|
+
type: "object",
|
|
141
|
+
properties: { text: { type: "string", required: true }, warning: { type: "string" } },
|
|
142
|
+
additionalProperties: false,
|
|
143
|
+
},
|
|
144
|
+
render: (_args, value) => renderTextWarning(value),
|
|
145
|
+
},
|
|
146
|
+
async execute(args, exec) {
|
|
147
|
+
return withWorkspace(execCwd(exec), async () => {
|
|
148
|
+
const cwd = execCwd(exec);
|
|
149
|
+
const signal = exec.signal;
|
|
150
|
+
const rec = (args ?? {});
|
|
151
|
+
const command = rec["command"];
|
|
152
|
+
if (typeof command !== "string" || !KNOWN_COMMANDS.has(command)) {
|
|
153
|
+
throw argError("E_BAD_COMMAND", `str_replace_editor: unknown command ${JSON.stringify(command)} — expected one of view, str_replace, insert, create, undo_edit.`);
|
|
154
|
+
}
|
|
155
|
+
const cmd = command;
|
|
156
|
+
if (cmd === "undo_edit") {
|
|
157
|
+
throw new Error("[MODEL] [E_UNSUPPORTED] str_replace_editor undo_edit is not implemented — use undo_last_edit.");
|
|
158
|
+
}
|
|
159
|
+
const rawPath = requireString(rec, "path");
|
|
160
|
+
abortIf(signal);
|
|
161
|
+
const absolutePath = await io.resolve(rawPath, cwd, signal);
|
|
162
|
+
if (cmd === "view") {
|
|
163
|
+
const text = await io.readText(absolutePath, signal);
|
|
164
|
+
// Observation only: encoding state is recorded by readText;
|
|
165
|
+
// served state is never touched here.
|
|
166
|
+
// NOTE (report #10): the readText + emitObserved pair costs two
|
|
167
|
+
// RPCs by design — readText decodes + records encoding state with
|
|
168
|
+
// no session, while emitObserved emits fs/observed with exec + a
|
|
169
|
+
// fresh stat version. Piggy-backing would need readText to take
|
|
170
|
+
// exec / return a version (FileIO contract break); same pattern
|
|
171
|
+
// as tool-read.ts, so documented, not collapsed.
|
|
172
|
+
await io.emitObserved(absolutePath, exec, signal);
|
|
173
|
+
const display = stripBOM(text).text;
|
|
174
|
+
const range = requireViewRange(rec);
|
|
175
|
+
let out = display;
|
|
176
|
+
if (range) {
|
|
177
|
+
const lines = splitLines(display);
|
|
178
|
+
const total = lines.length === 1 && lines[0] === "" ? 0 : lines.length;
|
|
179
|
+
const [start, end] = range;
|
|
180
|
+
if (total === 0) {
|
|
181
|
+
out = "[File is empty.]";
|
|
182
|
+
}
|
|
183
|
+
else if (start > total) {
|
|
184
|
+
out = `View range [${start}, ${end}] is beyond end of file (${total} lines total).`;
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
out = lines.slice(start - 1, Math.min(end, total)).join("\n");
|
|
188
|
+
if (end < total) {
|
|
189
|
+
out += `\n\n[Showing lines ${start}-${Math.min(end, total)} of ${total}. Use view_range to continue.]`;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const warning = autoGuessWarning(absolutePath, rawPath);
|
|
194
|
+
return warning ? { text: out, warning } : { text: out };
|
|
195
|
+
}
|
|
196
|
+
if (cmd === "create") {
|
|
197
|
+
const fileText = requireString(rec, "file_text");
|
|
198
|
+
const ver = await io.statVersion(absolutePath, signal);
|
|
199
|
+
const exists = ver !== undefined;
|
|
200
|
+
if (exists) {
|
|
201
|
+
throw argError("E_FILE_EXISTS", `str_replace_editor: cannot create ${rawPath} — file already exists.`);
|
|
202
|
+
}
|
|
203
|
+
// Governed default: UTF-8 without BOM (no memo → no BOM).
|
|
204
|
+
await io.writeText(absolutePath, fileText, signal, exec);
|
|
205
|
+
try {
|
|
206
|
+
const version = await io.statVersion(absolutePath, signal);
|
|
207
|
+
recordOpenState(absolutePath, fileText, "utf8", false, version);
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
// best-effort: the write succeeded; state is a hint.
|
|
211
|
+
}
|
|
212
|
+
return { text: `Created ${rawPath}.` };
|
|
213
|
+
}
|
|
214
|
+
// str_replace / insert — strict read-first gate, no disk write on failure.
|
|
215
|
+
// NOTE (report #9): requireObserved's statVersion + the readText below
|
|
216
|
+
// cost two RPCs by design — the stat invalidates stale encoding memos
|
|
217
|
+
// BEFORE the read so a drifted file fails loud with E_BLIND_REPLACE
|
|
218
|
+
// without paying for the read, and collapsing them (Promise.all or
|
|
219
|
+
// skipping the stat) reintroduces TOCTOU: a stale memo could authorize
|
|
220
|
+
// an edit on drifted bytes. Returning the version from readText would
|
|
221
|
+
// need a FileIO contract break for an unmeasured hot-path nit; same
|
|
222
|
+
// pattern as the view double-RPC above, so documented, not collapsed.
|
|
223
|
+
await requireObserved(io, absolutePath, rawPath, signal);
|
|
224
|
+
const current = await io.readText(absolutePath, signal);
|
|
225
|
+
if (cmd === "str_replace") {
|
|
226
|
+
const oldStr = requireString(rec, "old_str");
|
|
227
|
+
if (!("new_str" in rec) || typeof rec["new_str"] !== "string") {
|
|
228
|
+
throw argError("E_BAD_PAYLOAD", 'str_replace_editor: "new_str" must be a string for str_replace.');
|
|
229
|
+
}
|
|
230
|
+
const newStr = rec["new_str"];
|
|
231
|
+
const matches = countOccurrences(current, oldStr);
|
|
232
|
+
if (matches === 0) {
|
|
233
|
+
throw argError("E_NO_MATCH", `str_replace_editor: old_str not found in ${rawPath}. Nothing was written.`);
|
|
234
|
+
}
|
|
235
|
+
if (matches > 1) {
|
|
236
|
+
throw argError("E_AMBIGUOUS_MATCH", `str_replace_editor: old_str has multiple matches (${matches}) in ${rawPath} — must match exactly once. Narrow old_str with more context. Nothing was written.`);
|
|
237
|
+
}
|
|
238
|
+
const next = restoreForSave(current.replace(oldStr, newStr), absolutePath);
|
|
239
|
+
await io.writeText(absolutePath, next, signal, exec);
|
|
240
|
+
return { text: `Replaced 1 occurrence in ${rawPath}.` };
|
|
241
|
+
}
|
|
242
|
+
// insert
|
|
243
|
+
const line = rec["insert_line"];
|
|
244
|
+
if (!Number.isInteger(line) || line < 1) {
|
|
245
|
+
throw argError("E_BAD_PAYLOAD", 'str_replace_editor: "insert_line" must be a positive integer for insert.');
|
|
246
|
+
}
|
|
247
|
+
if (!("new_str" in rec) || typeof rec["new_str"] !== "string") {
|
|
248
|
+
throw argError("E_BAD_PAYLOAD", 'str_replace_editor: "new_str" must be a string for insert.');
|
|
249
|
+
}
|
|
250
|
+
const insertText = rec["new_str"];
|
|
251
|
+
const insertAt = line;
|
|
252
|
+
const stripped = stripBOM(current).text;
|
|
253
|
+
const endsWithNL = stripped.endsWith("\n");
|
|
254
|
+
const body = endsWithNL ? stripped.slice(0, -1) : stripped;
|
|
255
|
+
const lines = stripped.length === 0 ? [] : body.split("\n");
|
|
256
|
+
if (insertAt > lines.length + 1) {
|
|
257
|
+
throw argError("E_BAD_PAYLOAD", `str_replace_editor: insert_line ${insertAt} is beyond end of file (${lines.length} lines). Nothing was written.`);
|
|
258
|
+
}
|
|
259
|
+
const added = insertText.split("\n");
|
|
260
|
+
lines.splice(insertAt - 1, 0, ...added);
|
|
261
|
+
let out2 = lines.join("\n");
|
|
262
|
+
if (endsWithNL && out2.length > 0)
|
|
263
|
+
out2 += "\n";
|
|
264
|
+
const next = restoreForSave(out2, absolutePath);
|
|
265
|
+
await io.writeText(absolutePath, next, signal, exec);
|
|
266
|
+
return { text: `Inserted ${added.length} line(s) at line ${insertAt} in ${rawPath}.` };
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
/** Register the shadow `str_replace_editor` on the calling agent's scope (own layer). */
|
|
272
|
+
export function registerStrReplaceEditorTool(agentCtx, io) {
|
|
273
|
+
return agentCtx.tools.register(buildStrReplaceEditorTool(io));
|
|
274
|
+
}
|
package/lib/tool-undo.js
CHANGED
|
@@ -14,7 +14,7 @@ import { canon, contentChecksum } from "./hashline/hash-assign.js";
|
|
|
14
14
|
import { lineHashes } from "./hashline/hash.js";
|
|
15
15
|
import { changedRange } from "./hashline/anchor-pipeline.js";
|
|
16
16
|
import { getUndo, clearUndo } from "./undo-edit.js";
|
|
17
|
-
import { loadAnchorReservations, recordServedTruncated, retireAnchors
|
|
17
|
+
import { loadAnchorReservations, recordServedTruncated, retireAnchors } from "./session-view.js";
|
|
18
18
|
import { UNDO_DESCRIPTION } from "./prompts.js";
|
|
19
19
|
import { execCwd, execSessionKey } from "./workspace-context.js";
|
|
20
20
|
import { withWorkspace } from "./workspace-context.js";
|
|
@@ -68,8 +68,7 @@ export function buildUndoTool(io, sandbox) {
|
|
|
68
68
|
}
|
|
69
69
|
throw error;
|
|
70
70
|
}
|
|
71
|
-
if (currentRaw !==
|
|
72
|
-
undo.bom + restoreEndings(undo.resultContent, undo.originalEnding)) {
|
|
71
|
+
if (currentRaw !== undo.bom + restoreEndings(undo.resultContent, undo.originalEnding)) {
|
|
73
72
|
await clearUndo(absolutePath);
|
|
74
73
|
return `[E_UNDO_STALE] cannot undo on ${path}: file modified after edit — undo would overwrite changes.`;
|
|
75
74
|
}
|
package/lib/types/contract.d.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
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
|
export type EditItem = {
|
|
@@ -47,6 +47,13 @@ export type NormalizedEditRequest = EditRequest & {
|
|
|
47
47
|
};
|
|
48
48
|
export declare function isNormalizedEdit(input: unknown): input is NormalizedEditRequest;
|
|
49
49
|
export declare function itemFromTuple(value: unknown): EditItem | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Accept both the tuple form (["a", "b", "c"]) and the object form
|
|
52
|
+
* ({ remove_from, remove_to, replacement_text }) per edits entry (#64).
|
|
53
|
+
* Missing/mistyped fields and unknown fields still return undefined so the
|
|
54
|
+
* caller rejects with E_BAD_PAYLOAD.
|
|
55
|
+
*/
|
|
56
|
+
export declare function itemFromEntry(value: unknown): EditItem | undefined;
|
|
50
57
|
export declare function editRequestFrom(input: unknown): NormalizedEditRequest | undefined;
|
|
51
58
|
export declare function sanitizePath(value: unknown): string | null;
|
|
52
59
|
export declare const EDIT_TUPLE_HINT: string;
|
|
@@ -65,7 +72,7 @@ export declare function assertReadRequest(request: unknown): asserts request is
|
|
|
65
72
|
export declare function assertUndoRequest(request: unknown): asserts request is UndoParams;
|
|
66
73
|
export declare const replacementTextSchema: {
|
|
67
74
|
readonly type: "string";
|
|
68
|
-
readonly description:
|
|
75
|
+
readonly description: 'Complete replacement for the range; use "" to delete';
|
|
69
76
|
};
|
|
70
77
|
export declare const removeFromSchema: {
|
|
71
78
|
readonly type: "string";
|
|
@@ -99,12 +106,70 @@ export declare const editTupleSchema: {
|
|
|
99
106
|
readonly description: "Last line to remove (inclusive)";
|
|
100
107
|
}, {
|
|
101
108
|
readonly type: "string";
|
|
102
|
-
readonly description:
|
|
109
|
+
readonly description: 'Complete replacement for the range; use "" to delete';
|
|
103
110
|
}];
|
|
104
111
|
readonly minItems: 3;
|
|
105
112
|
readonly maxItems: 3;
|
|
106
113
|
readonly description: "[remove_from, remove_to, replacement_text]";
|
|
107
114
|
};
|
|
115
|
+
export declare const editObjectSchema: {
|
|
116
|
+
readonly type: "object";
|
|
117
|
+
readonly additionalProperties: false;
|
|
118
|
+
readonly required: readonly ["remove_from", "remove_to", "replacement_text"];
|
|
119
|
+
readonly properties: {
|
|
120
|
+
readonly remove_from: {
|
|
121
|
+
readonly type: "string";
|
|
122
|
+
readonly description: "First line to remove (inclusive)";
|
|
123
|
+
};
|
|
124
|
+
readonly remove_to: {
|
|
125
|
+
readonly type: "string";
|
|
126
|
+
readonly description: "Last line to remove (inclusive)";
|
|
127
|
+
};
|
|
128
|
+
readonly replacement_text: {
|
|
129
|
+
readonly type: "string";
|
|
130
|
+
readonly description: 'Complete replacement for the range; use "" to delete';
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
readonly description: "{remove_from, remove_to, replacement_text}";
|
|
134
|
+
};
|
|
135
|
+
export declare const editItemSchema: {
|
|
136
|
+
readonly anyOf: readonly [{
|
|
137
|
+
readonly type: "array";
|
|
138
|
+
readonly prefixItems: readonly [{
|
|
139
|
+
readonly type: "string";
|
|
140
|
+
readonly description: "First line to remove (inclusive)";
|
|
141
|
+
}, {
|
|
142
|
+
readonly type: "string";
|
|
143
|
+
readonly description: "Last line to remove (inclusive)";
|
|
144
|
+
}, {
|
|
145
|
+
readonly type: "string";
|
|
146
|
+
readonly description: 'Complete replacement for the range; use "" to delete';
|
|
147
|
+
}];
|
|
148
|
+
readonly minItems: 3;
|
|
149
|
+
readonly maxItems: 3;
|
|
150
|
+
readonly description: "[remove_from, remove_to, replacement_text]";
|
|
151
|
+
}, {
|
|
152
|
+
readonly type: "object";
|
|
153
|
+
readonly additionalProperties: false;
|
|
154
|
+
readonly required: readonly ["remove_from", "remove_to", "replacement_text"];
|
|
155
|
+
readonly properties: {
|
|
156
|
+
readonly remove_from: {
|
|
157
|
+
readonly type: "string";
|
|
158
|
+
readonly description: "First line to remove (inclusive)";
|
|
159
|
+
};
|
|
160
|
+
readonly remove_to: {
|
|
161
|
+
readonly type: "string";
|
|
162
|
+
readonly description: "Last line to remove (inclusive)";
|
|
163
|
+
};
|
|
164
|
+
readonly replacement_text: {
|
|
165
|
+
readonly type: "string";
|
|
166
|
+
readonly description: 'Complete replacement for the range; use "" to delete';
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
readonly description: "{remove_from, remove_to, replacement_text}";
|
|
170
|
+
}];
|
|
171
|
+
readonly description: "Edit entry — either tuple or object form (mixed batches allowed)";
|
|
172
|
+
};
|
|
108
173
|
export declare const editToolSchema: {
|
|
109
174
|
readonly type: "object";
|
|
110
175
|
readonly additionalProperties: false;
|
|
@@ -122,24 +187,46 @@ export declare const editToolSchema: {
|
|
|
122
187
|
};
|
|
123
188
|
readonly edits: {
|
|
124
189
|
readonly type: "array";
|
|
125
|
-
readonly description: "Ordered list of edit
|
|
190
|
+
readonly 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)";
|
|
126
191
|
readonly minItems: 1;
|
|
127
192
|
readonly maxItems: 32;
|
|
128
193
|
readonly items: {
|
|
129
|
-
readonly
|
|
130
|
-
|
|
131
|
-
readonly
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
194
|
+
readonly anyOf: readonly [{
|
|
195
|
+
readonly type: "array";
|
|
196
|
+
readonly prefixItems: readonly [{
|
|
197
|
+
readonly type: "string";
|
|
198
|
+
readonly description: "First line to remove (inclusive)";
|
|
199
|
+
}, {
|
|
200
|
+
readonly type: "string";
|
|
201
|
+
readonly description: "Last line to remove (inclusive)";
|
|
202
|
+
}, {
|
|
203
|
+
readonly type: "string";
|
|
204
|
+
readonly description: 'Complete replacement for the range; use "" to delete';
|
|
205
|
+
}];
|
|
206
|
+
readonly minItems: 3;
|
|
207
|
+
readonly maxItems: 3;
|
|
208
|
+
readonly description: "[remove_from, remove_to, replacement_text]";
|
|
136
209
|
}, {
|
|
137
|
-
readonly type: "
|
|
138
|
-
readonly
|
|
210
|
+
readonly type: "object";
|
|
211
|
+
readonly additionalProperties: false;
|
|
212
|
+
readonly required: readonly ["remove_from", "remove_to", "replacement_text"];
|
|
213
|
+
readonly properties: {
|
|
214
|
+
readonly remove_from: {
|
|
215
|
+
readonly type: "string";
|
|
216
|
+
readonly description: "First line to remove (inclusive)";
|
|
217
|
+
};
|
|
218
|
+
readonly remove_to: {
|
|
219
|
+
readonly type: "string";
|
|
220
|
+
readonly description: "Last line to remove (inclusive)";
|
|
221
|
+
};
|
|
222
|
+
readonly replacement_text: {
|
|
223
|
+
readonly type: "string";
|
|
224
|
+
readonly description: 'Complete replacement for the range; use "" to delete';
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
readonly description: "{remove_from, remove_to, replacement_text}";
|
|
139
228
|
}];
|
|
140
|
-
readonly
|
|
141
|
-
readonly maxItems: 3;
|
|
142
|
-
readonly description: "[remove_from, remove_to, replacement_text]";
|
|
229
|
+
readonly description: "Edit entry — either tuple or object form (mixed batches allowed)";
|
|
143
230
|
};
|
|
144
231
|
};
|
|
145
232
|
};
|
|
@@ -64,6 +64,9 @@ export interface EncodeForSaveOptions {
|
|
|
64
64
|
normalizeToUtf8?: boolean;
|
|
65
65
|
currentVersion?: string | undefined;
|
|
66
66
|
}
|
|
67
|
+
/** Shared UTF-8 BOM marker — single source for the BOM round-trip seam.
|
|
68
|
+
* Prefer `prepareForSave` / `stripBOM` over re-implementing this check. */
|
|
69
|
+
export declare const UTF8_BOM = "\uFEFF";
|
|
67
70
|
/**
|
|
68
71
|
* Decide what bytes (as text, since provider expects string) to write and
|
|
69
72
|
* what new state to record. BOM and lineEnding are restored here when a
|
package/lib/types/file-view.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export declare const DEFAULT_MAX_BYTES: number;
|
|
|
27
27
|
export interface TruncationResult {
|
|
28
28
|
content: string;
|
|
29
29
|
truncated: boolean;
|
|
30
|
-
truncatedBy:
|
|
30
|
+
truncatedBy: "lines" | "bytes" | null;
|
|
31
31
|
totalLines: number;
|
|
32
32
|
totalBytes: number;
|
|
33
33
|
outputLines: number;
|
|
@@ -10,4 +10,4 @@ export type { Anchor } from "./parse.js";
|
|
|
10
10
|
export { resEdit } from "./anchor-pipeline.js";
|
|
11
11
|
export type { HEdit, HTEdit, NEdit } from "./anchor-pipeline.js";
|
|
12
12
|
export { applyEdit, fmtRegion, changedRange, buildIdx, ServedRejectionError, AnchorMismatchError, EditHashEchoError, findEditHashEcho, isServedRejection, isAnchorMismatch, verifyServedRange, buildRangeEcho, fmtServedRows, } from "./anchor-pipeline.js";
|
|
13
|
-
export type { ServedRow, ResolvedRange, ServedCode
|
|
13
|
+
export type { ServedRow, ResolvedRange, ServedCode } from "./anchor-pipeline.js";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared output-render seam for the `{ text, warning? }` tool payload.
|
|
3
|
+
*
|
|
4
|
+
* `read` (tool-read.ts) and `str_replace_editor` view (tool-str-replace-editor.ts)
|
|
5
|
+
* previously carried verbatim copies of this render closure, so a warning-format
|
|
6
|
+
* change had to be made in two places (report #7). Both now delegate here.
|
|
7
|
+
* @module dsh-better-edit/render-text-warning
|
|
8
|
+
*/
|
|
9
|
+
export type TextWarningValue = {
|
|
10
|
+
text: string;
|
|
11
|
+
warning?: string;
|
|
12
|
+
} | string;
|
|
13
|
+
export type TextBlock = {
|
|
14
|
+
type: "text";
|
|
15
|
+
text: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Render a `{ text, warning? }` payload (or bare string) as content blocks:
|
|
19
|
+
* string input yields a single text block, object input yields the text block
|
|
20
|
+
* plus a second block when `warning` is present.
|
|
21
|
+
*/
|
|
22
|
+
export declare function renderTextWarning(value: TextWarningValue): TextBlock[];
|
package/lib/types/self-heal.d.ts
CHANGED
|
@@ -24,6 +24,8 @@ export interface SelfHealOptions {
|
|
|
24
24
|
toolsSvc: unknown;
|
|
25
25
|
hashReadDef: unknown;
|
|
26
26
|
hashEditDef: unknown;
|
|
27
|
+
/** ADR-0015 shadow def; when present the watcher also heals `str_replace_editor`. */
|
|
28
|
+
hashStrReplaceDef?: unknown;
|
|
27
29
|
healMinIntervalMs?: number;
|
|
28
30
|
}
|
|
29
31
|
export declare function createSelfHealWatcher(options: SelfHealOptions): () => void;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dsh `str_replace_editor` shadow: a governed agent-layer replacement for
|
|
3
|
+
* the built-in `@deepseek-ai/dsh-tool-str-replace-editor` with identical
|
|
4
|
+
* contract (`view` / `str_replace` / `insert` / `create`, no new `encoding`
|
|
5
|
+
* param) but routed through the file-encoding-state seam.
|
|
6
|
+
*
|
|
7
|
+
* - `view` is observation: decode via the `ctxFsIO` path (`decodeForOpen`
|
|
8
|
+
* inside `io.readText`), records file encoding state, emits `fs/observed`,
|
|
9
|
+
* and never records served state (so it can feed `write` round-trips but
|
|
10
|
+
* cannot authorize hashline `edit` anchors).
|
|
11
|
+
* - `str_replace` / `insert` without a prior `view`/`read` fail loud with
|
|
12
|
+
* `E_BLIND_REPLACE` — no disk write.
|
|
13
|
+
* - `create` defaults to UTF-8 without BOM.
|
|
14
|
+
* - `undo_edit` is out of scope → `E_UNSUPPORTED` (use `undo_last_edit`).
|
|
15
|
+
*
|
|
16
|
+
* See ADR-0015.
|
|
17
|
+
* @module dsh-better-edit/tool-str-replace-editor
|
|
18
|
+
*/
|
|
19
|
+
import type { FileIO } from "./fs-bridge.js";
|
|
20
|
+
export declare const STR_REPLACE_EDITOR_DESCRIPTION: string;
|
|
21
|
+
export declare function buildStrReplaceEditorTool(io: FileIO): import("@deepseek-ai/dsh-tools").ToolDefinition;
|
|
22
|
+
/** Register the shadow `str_replace_editor` on the calling agent's scope (own layer). */
|
|
23
|
+
export declare function registerStrReplaceEditorTool(agentCtx: {
|
|
24
|
+
tools: {
|
|
25
|
+
register(def: unknown): () => void;
|
|
26
|
+
};
|
|
27
|
+
}, io: FileIO): () => void;
|
package/lib/types/undo-edit.d.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* overwritten. Undo survives restarts (the store is on disk).
|
|
8
8
|
* @module dsh-better-edit/undo-edit
|
|
9
9
|
*/
|
|
10
|
-
import type { LineEnding } from
|
|
10
|
+
import type { LineEnding } from "./edit-diff.js";
|
|
11
11
|
export interface UndoEntry {
|
|
12
12
|
content: string;
|
|
13
13
|
bom: string;
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* anchors to the model-facing content (mirroring pi-hashline-edit-lsz).
|
|
6
6
|
* @module dsh-better-edit/write-hook
|
|
7
7
|
*/
|
|
8
|
-
import type { Context } from
|
|
9
|
-
import type { FileIO } from
|
|
8
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
9
|
+
import type { FileIO } from "./fs-bridge.js";
|
|
10
10
|
export interface ServedHashEcho {
|
|
11
11
|
/** One-based candidate line carrying the copied anchor. */
|
|
12
12
|
line: number;
|
package/lib/undo-edit.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* overwritten. Undo survives restarts (the store is on disk).
|
|
8
8
|
* @module dsh-better-edit/undo-edit
|
|
9
9
|
*/
|
|
10
|
-
import { loadHashStore } from
|
|
10
|
+
import { loadHashStore } from "./hash-store.js";
|
|
11
11
|
/** Load the last undo row for a path from the active store, if any. */
|
|
12
12
|
async function readUndo(path) {
|
|
13
13
|
const store = await loadHashStore();
|
|
@@ -43,7 +43,7 @@ export async function saveUndo(path, entry) {
|
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
catch (error) {
|
|
46
|
-
console.error(
|
|
46
|
+
console.error("Failed to persist undo entry:", error);
|
|
47
47
|
return { persisted: false, restore: async () => undefined };
|
|
48
48
|
}
|
|
49
49
|
return {
|
|
@@ -56,7 +56,7 @@ export async function saveUndo(path, entry) {
|
|
|
56
56
|
await removeUndo(path);
|
|
57
57
|
}
|
|
58
58
|
catch (error) {
|
|
59
|
-
console.error(
|
|
59
|
+
console.error("Failed to restore previous undo entry:", error);
|
|
60
60
|
}
|
|
61
61
|
},
|
|
62
62
|
};
|
|
@@ -68,9 +68,7 @@ export async function getUndo(path) {
|
|
|
68
68
|
if (!record)
|
|
69
69
|
return undefined;
|
|
70
70
|
const originalEnding = record.ending;
|
|
71
|
-
if (originalEnding !==
|
|
72
|
-
originalEnding !== '\n' &&
|
|
73
|
-
originalEnding !== '\r') {
|
|
71
|
+
if (originalEnding !== "\r\n" && originalEnding !== "\n" && originalEnding !== "\r") {
|
|
74
72
|
await removeUndo(path);
|
|
75
73
|
return undefined;
|
|
76
74
|
}
|
|
@@ -83,7 +81,7 @@ export async function getUndo(path) {
|
|
|
83
81
|
};
|
|
84
82
|
}
|
|
85
83
|
catch (error) {
|
|
86
|
-
console.error(
|
|
84
|
+
console.error("Failed to load undo entry:", error);
|
|
87
85
|
return undefined;
|
|
88
86
|
}
|
|
89
87
|
}
|
|
@@ -93,6 +91,6 @@ export async function clearUndo(path) {
|
|
|
93
91
|
await removeUndo(path);
|
|
94
92
|
}
|
|
95
93
|
catch (error) {
|
|
96
|
-
console.error(
|
|
94
|
+
console.error("Failed to clear undo entry:", error);
|
|
97
95
|
}
|
|
98
96
|
}
|
package/lib/utils.js
CHANGED
|
@@ -31,8 +31,7 @@ export function cntDiff(diff, marker) {
|
|
|
31
31
|
return 0;
|
|
32
32
|
let count = 0;
|
|
33
33
|
for (const line of diff.split("\n")) {
|
|
34
|
-
if (line.startsWith(marker) &&
|
|
35
|
-
!line.startsWith(`${marker}${marker}${marker}`)) {
|
|
34
|
+
if (line.startsWith(marker) && !line.startsWith(`${marker}${marker}${marker}`)) {
|
|
36
35
|
count += 1;
|
|
37
36
|
}
|
|
38
37
|
}
|
package/lib/workspace-context.js
CHANGED
|
@@ -30,7 +30,7 @@ export function sessionKeyFor(sessionId) {
|
|
|
30
30
|
if (sessionId && sessionId.length > 0)
|
|
31
31
|
return sessionId;
|
|
32
32
|
// fallback for previews/tests
|
|
33
|
-
return fallbackSessionKey ??= randomUUID();
|
|
33
|
+
return (fallbackSessionKey ??= randomUUID());
|
|
34
34
|
}
|
|
35
35
|
export function execCwd(exec) {
|
|
36
36
|
return exec.agent?.session.header.cwd ?? process.cwd();
|