dsh-better-edit 0.8.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +173 -99
- package/README.md +61 -33
- package/README.zh.md +20 -17
- package/cordis.patch.yml +1 -1
- package/lib/constants.js +19 -4
- package/lib/contract.js +136 -79
- package/lib/domain-errors.js +570 -0
- package/lib/edit-diff.js +33 -7
- package/lib/file-encoding-state.js +27 -7
- package/lib/file-view.js +305 -82
- package/lib/fs-bridge.js +23 -7
- package/lib/guidance/materialize.js +7 -7
- package/lib/guidance/resolve.js +13 -8
- package/lib/hash-store.js +404 -278
- package/lib/hashline/anchor-pipeline.js +586 -378
- package/lib/hashline/hash-assign.js +21 -22
- package/lib/hashline/hash.js +6 -6
- package/lib/hashline/index.js +1 -1
- package/lib/hashline/resolve.js +1 -1
- package/lib/hashline/served-guard.js +181 -0
- package/lib/index.js +37 -11
- package/lib/mutation/engine.js +392 -96
- package/lib/mutation.js +22 -130
- package/lib/noop-guard.js +20 -9
- package/lib/prompts.js +20 -18
- package/lib/read-and-serve.js +44 -20
- package/lib/sandbox.js +1 -1
- package/lib/session-view.js +57 -43
- package/lib/snapshot-store/index.js +97 -0
- package/lib/snapshot-store/lineage-store.js +400 -0
- package/lib/snapshot-store/migrate.js +80 -0
- package/lib/snapshot-store/pairing.js +412 -0
- package/lib/snapshot-store/txn.js +63 -0
- package/lib/snapshot-store/vacuum.js +243 -0
- package/lib/store-lifecycle.js +13 -1
- package/lib/store-retry.js +34 -0
- package/lib/tool-edit.js +90 -44
- package/lib/tool-read.js +29 -1
- package/lib/tool-str-replace-editor.js +26 -18
- package/lib/tool-undo.js +25 -8
- package/lib/types/constants.d.ts +5 -2
- package/lib/types/contract.d.ts +76 -71
- package/lib/types/domain-errors.d.ts +410 -0
- package/lib/types/edit-response.d.ts +1 -1
- package/lib/types/file-encoding-state.d.ts +1 -0
- package/lib/types/file-view.d.ts +35 -6
- package/lib/types/guidance/materialize.d.ts +2 -2
- package/lib/types/guidance/resolve.d.ts +14 -2
- package/lib/types/hash-store.d.ts +38 -19
- package/lib/types/hashline/anchor-pipeline.d.ts +117 -36
- package/lib/types/hashline/hash-assign.d.ts +5 -5
- package/lib/types/hashline/hash.d.ts +1 -1
- package/lib/types/hashline/index.d.ts +2 -2
- package/lib/types/hashline/served-guard.d.ts +100 -0
- package/lib/types/index.d.ts +3 -3
- package/lib/types/mutation/engine.d.ts +49 -30
- package/lib/types/mutation.d.ts +1 -39
- package/lib/types/noop-guard.d.ts +4 -4
- package/lib/types/prompts.d.ts +1 -1
- package/lib/types/read-and-serve.d.ts +6 -0
- package/lib/types/sandbox.d.ts +1 -1
- package/lib/types/session-view.d.ts +38 -9
- package/lib/types/snapshot-store/index.d.ts +28 -0
- package/lib/types/snapshot-store/lineage-store.d.ts +96 -0
- package/lib/types/snapshot-store/migrate.d.ts +16 -0
- package/lib/types/snapshot-store/pairing.d.ts +99 -0
- package/lib/types/snapshot-store/txn.d.ts +12 -0
- package/lib/types/snapshot-store/vacuum.d.ts +79 -0
- package/lib/types/store-lifecycle.d.ts +0 -1
- package/lib/types/store-retry.d.ts +6 -0
- package/lib/types/tool-edit.d.ts +2 -2
- package/lib/types/tool-str-replace-editor.d.ts +2 -2
- package/lib/types/utils.d.ts +5 -6
- package/lib/types/workspace-context.d.ts +10 -1
- package/lib/undo-edit.js +27 -11
- package/lib/utils.js +12 -12
- package/lib/workspace-context.js +27 -9
- package/lib/write-hook.js +13 -4
- package/package.json +28 -30
package/lib/contract.js
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
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
|
|
8
|
-
* (plus
|
|
7
|
+
* Contract mirrors upstream ADR-0015: {file: string|null, edits: [{anchor_from, anchor_to, replace_with},...]} named-object payload
|
|
8
|
+
* (plus legacy tuple items and legacy key spellings folded pre-validation), 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
|
|
12
|
+
import { isRec, normalizeFilePath, rejectUnknownFields } from "./utils.js";
|
|
13
|
+
import { DomainError } from "./domain-errors.js";
|
|
13
14
|
// ---- normalized marker -----------------------------------------------------
|
|
14
15
|
export const normalizedEdit = Symbol("normalizedEdit");
|
|
15
16
|
export function isNormalizedEdit(input) {
|
|
@@ -18,57 +19,81 @@ export function isNormalizedEdit(input) {
|
|
|
18
19
|
export function itemFromTuple(value) {
|
|
19
20
|
if (!Array.isArray(value) || value.length !== 3)
|
|
20
21
|
return undefined;
|
|
21
|
-
const [
|
|
22
|
-
if (typeof
|
|
23
|
-
typeof
|
|
24
|
-
typeof
|
|
22
|
+
const [anchor_from, anchor_to, replace_with] = value;
|
|
23
|
+
if (typeof anchor_from !== "string" ||
|
|
24
|
+
typeof anchor_to !== "string" ||
|
|
25
|
+
typeof replace_with !== "string")
|
|
25
26
|
return undefined;
|
|
26
|
-
return {
|
|
27
|
+
return { anchor_from, anchor_to, replace_with };
|
|
27
28
|
}
|
|
28
29
|
/**
|
|
29
|
-
* Accept
|
|
30
|
-
* (
|
|
31
|
-
*
|
|
32
|
-
*
|
|
30
|
+
* Accept the named-object form ({ anchor_from, anchor_to, replace_with }),
|
|
31
|
+
* the legacy tuple form ([a, b, t]), and the legacy-key form
|
|
32
|
+
* (legacy spelling) per edits entry — all fold
|
|
33
|
+
* to the named triple. Mixed old/new keys, extra keys, and 2-/4-position
|
|
34
|
+
* tuples still return undefined so the caller rejects with E_BAD_PAYLOAD.
|
|
33
35
|
*/
|
|
34
36
|
export function itemFromEntry(value) {
|
|
35
37
|
if (Array.isArray(value))
|
|
36
38
|
return itemFromTuple(value);
|
|
37
39
|
if (isRec(value)) {
|
|
38
40
|
const rec = value;
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
const keys = new Set(Object.keys(rec));
|
|
42
|
+
const useLegacy = keys.size === LEGACY_ITEM_KS.size && [...LEGACY_ITEM_KS].every((k) => keys.has(k));
|
|
43
|
+
const useNamed = keys.size === ITEM_KS.size && [...ITEM_KS].every((k) => keys.has(k));
|
|
44
|
+
if (!useNamed && !useLegacy)
|
|
43
45
|
return undefined;
|
|
44
|
-
|
|
46
|
+
const from = useNamed ? rec.anchor_from : rec.remove_from;
|
|
47
|
+
const to = useNamed ? rec.anchor_to : rec.remove_to;
|
|
48
|
+
const text = useNamed ? rec.replace_with : rec.replacement_text;
|
|
49
|
+
if (typeof from !== "string" || typeof to !== "string" || typeof text !== "string")
|
|
45
50
|
return undefined;
|
|
46
|
-
return {
|
|
51
|
+
return { anchor_from: from, anchor_to: to, replace_with: text };
|
|
47
52
|
}
|
|
48
53
|
return undefined;
|
|
49
54
|
}
|
|
55
|
+
const ROOT_INPUT_KS = new Set([
|
|
56
|
+
"file",
|
|
57
|
+
"file_path",
|
|
58
|
+
"path",
|
|
59
|
+
"edits",
|
|
60
|
+
"mode",
|
|
61
|
+
"sandbox_permissions",
|
|
62
|
+
"justification",
|
|
63
|
+
]);
|
|
50
64
|
export function editRequestFrom(input) {
|
|
51
|
-
if (!isRec(input)
|
|
65
|
+
if (!isRec(input))
|
|
52
66
|
return undefined;
|
|
53
67
|
const rec = input;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
// but handle here for direct calls
|
|
68
|
+
for (const key of Object.keys(rec)) {
|
|
69
|
+
if (!ROOT_INPUT_KS.has(key))
|
|
70
|
+
return undefined;
|
|
58
71
|
}
|
|
59
|
-
const { path, edits } = rec;
|
|
60
|
-
//
|
|
61
|
-
//
|
|
62
|
-
let
|
|
63
|
-
if (
|
|
64
|
-
|
|
72
|
+
const { file, file_path, path, edits, mode } = rec;
|
|
73
|
+
// file preferred; legacy path/file_path fold (sanitized). null stays null:
|
|
74
|
+
// the undocumented anchor-inference seam (resolveNullPath) still resolves it.
|
|
75
|
+
let effectiveFile;
|
|
76
|
+
if (file !== undefined)
|
|
77
|
+
effectiveFile = file;
|
|
78
|
+
else if (path !== undefined)
|
|
79
|
+
effectiveFile = path;
|
|
80
|
+
else if (file_path !== undefined)
|
|
81
|
+
effectiveFile = file_path;
|
|
82
|
+
else
|
|
83
|
+
return undefined;
|
|
84
|
+
let effectivePath;
|
|
85
|
+
if (typeof effectiveFile === "string") {
|
|
86
|
+
const sanitized = sanitizePath(effectiveFile);
|
|
65
87
|
if (sanitized === null)
|
|
66
88
|
return undefined;
|
|
67
89
|
effectivePath = sanitized;
|
|
68
90
|
}
|
|
69
|
-
if (
|
|
70
|
-
|
|
91
|
+
else if (effectiveFile === null) {
|
|
92
|
+
effectivePath = null;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
71
95
|
return undefined;
|
|
96
|
+
}
|
|
72
97
|
if (!Array.isArray(edits) || edits.length === 0)
|
|
73
98
|
return undefined;
|
|
74
99
|
const items = [];
|
|
@@ -78,7 +103,13 @@ export function editRequestFrom(input) {
|
|
|
78
103
|
return undefined;
|
|
79
104
|
items.push(normalized);
|
|
80
105
|
}
|
|
81
|
-
|
|
106
|
+
let effectiveMode = "general";
|
|
107
|
+
if (mode !== undefined) {
|
|
108
|
+
if (mode !== "general" && mode !== "literal")
|
|
109
|
+
return undefined;
|
|
110
|
+
effectiveMode = mode;
|
|
111
|
+
}
|
|
112
|
+
return { file: effectivePath, edits: items, mode: effectiveMode };
|
|
82
113
|
}
|
|
83
114
|
/**
|
|
84
115
|
* Strip Gemma-4 tool-call bleed wrappers from a path string: `<|>`, `\u2502`, `|`
|
|
@@ -120,13 +151,12 @@ export function sanitizePath(value) {
|
|
|
120
151
|
}
|
|
121
152
|
return cleaned.length > 0 ? cleaned : null;
|
|
122
153
|
}
|
|
123
|
-
export const
|
|
124
|
-
'{"
|
|
125
|
-
"
|
|
126
|
-
"
|
|
127
|
-
"
|
|
128
|
-
"
|
|
129
|
-
"replacement (an empty string deletes the range).";
|
|
154
|
+
export const EDIT_PAYLOAD_HINT = "Edit must be called with exactly one payload. Use the canonical payload " +
|
|
155
|
+
'{"file": file, "edits": [{ "anchor_from": anchor_from, "anchor_to": anchor_to, "replace_with": replace_with }, ...], "mode"?: "general" | "literal"}: ' +
|
|
156
|
+
'"file" is the text file to edit (a non-empty string, never a directory); each item names ' +
|
|
157
|
+
"two inclusive bare-3-char anchors and the full replacement " +
|
|
158
|
+
'(an empty string deletes the range); optional "mode" is "general" (default, reproduced served rows are refused) or "literal" (declared literal content). ' +
|
|
159
|
+
"Legacy tuple items and legacy key spellings still fold; do not mix old and new key spellings.";
|
|
130
160
|
function describeReceived(input) {
|
|
131
161
|
if (input === undefined)
|
|
132
162
|
return "Received no arguments.";
|
|
@@ -139,9 +169,10 @@ function describeReceived(input) {
|
|
|
139
169
|
return `Received: ${preview}`;
|
|
140
170
|
}
|
|
141
171
|
// ---- filed sets (declared once) ---------------------------------------------
|
|
142
|
-
const EDIT_KS = new Set(["
|
|
143
|
-
const READ_KS = new Set(["path", "offset", "limit", "encoding"]);
|
|
144
|
-
const
|
|
172
|
+
const EDIT_KS = new Set(["file", "edits", "mode", "sandbox_permissions", "justification"]);
|
|
173
|
+
const READ_KS = new Set(["path", "offset", "limit", "encoding", "windows"]);
|
|
174
|
+
const ITEM_KS = new Set(["anchor_from", "anchor_to", "replace_with"]);
|
|
175
|
+
const LEGACY_ITEM_KS = new Set(["remove_from", "remove_to", "replacement_text"]);
|
|
145
176
|
// ---- normalization -----------------------------------------------------------
|
|
146
177
|
/**
|
|
147
178
|
* Normalize `file_path` → `path` alias on the request record and tuple edits → objects.
|
|
@@ -160,7 +191,11 @@ export function normalizeRequest(input) {
|
|
|
160
191
|
const valid = editRequestFrom(record);
|
|
161
192
|
if (!valid)
|
|
162
193
|
return record;
|
|
163
|
-
const normalized = {
|
|
194
|
+
const normalized = {
|
|
195
|
+
file: valid.file,
|
|
196
|
+
edits: valid.edits,
|
|
197
|
+
mode: valid.mode,
|
|
198
|
+
};
|
|
164
199
|
// preserve non-standard fields like sandbox_permissions/justification for later reject check? but we strip to valid fields and re-add them?
|
|
165
200
|
for (const k of ["sandbox_permissions", "justification"]) {
|
|
166
201
|
if (k in record)
|
|
@@ -174,67 +209,89 @@ export const normReq = normalizeRequest;
|
|
|
174
209
|
export function prepareEditArguments(args) {
|
|
175
210
|
const valid = editRequestFrom(args);
|
|
176
211
|
if (valid) {
|
|
177
|
-
|
|
212
|
+
// F10.1: preserve mode like normalizeRequest — the two normalizers agree.
|
|
213
|
+
// prepareEditArguments returns schema-ready folded objects (named triple).
|
|
214
|
+
return { file: valid.file, edits: valid.edits, mode: valid.mode };
|
|
178
215
|
}
|
|
179
|
-
throw new
|
|
216
|
+
throw new DomainError("E_BAD_PAYLOAD", {
|
|
217
|
+
message: `${EDIT_PAYLOAD_HINT} ${describeReceived(args)}`,
|
|
218
|
+
});
|
|
180
219
|
}
|
|
181
220
|
// ---- assertions ---------------------------------------------------------------
|
|
182
221
|
export function assertEditRequest(request) {
|
|
183
222
|
if (!isNormalizedEdit(request)) {
|
|
184
|
-
throw new
|
|
223
|
+
throw new DomainError("E_BAD_PAYLOAD", {
|
|
224
|
+
message: 'Edit request must be exactly { file, edits: [{ anchor_from, anchor_to, replace_with }, ...], mode?: "general" | "literal" }. ' +
|
|
225
|
+
EDIT_PAYLOAD_HINT,
|
|
226
|
+
});
|
|
185
227
|
}
|
|
186
228
|
rejectUnknownFields(request, EDIT_KS, "Edit request");
|
|
187
229
|
const req = request;
|
|
188
|
-
if (req.
|
|
189
|
-
throw new
|
|
230
|
+
if (req.file !== null && (typeof req.file !== "string" || req.file.length === 0)) {
|
|
231
|
+
throw new DomainError("E_BAD_PAYLOAD", {
|
|
232
|
+
message: "Edit request file must be a non-empty string or null.",
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
if (req.mode !== undefined && req.mode !== "general" && req.mode !== "literal") {
|
|
236
|
+
throw new DomainError("E_BAD_PAYLOAD", {
|
|
237
|
+
message: 'Edit request "mode" must be "general" or "literal" (absent means "general"). Reproduced served rows are refused unless mode is "literal".',
|
|
238
|
+
});
|
|
190
239
|
}
|
|
191
240
|
if (!Array.isArray(req.edits) || req.edits.length === 0) {
|
|
192
|
-
throw new
|
|
241
|
+
throw new DomainError("E_BAD_PAYLOAD", {
|
|
242
|
+
message: 'Edit request requires a non-empty "edits" array.',
|
|
243
|
+
});
|
|
193
244
|
}
|
|
194
245
|
if (req.edits.length > EDITS_MAX_ITEMS) {
|
|
195
|
-
throw new
|
|
246
|
+
throw new DomainError("E_BAD_PAYLOAD", {
|
|
247
|
+
message: `edit accepts at most ${EDITS_MAX_ITEMS} edits; got ${req.edits.length}. Split the batch.`,
|
|
248
|
+
});
|
|
196
249
|
}
|
|
197
250
|
for (let index = 0; index < req.edits.length; index++) {
|
|
198
251
|
const item = req.edits[index];
|
|
199
|
-
if (typeof item.
|
|
200
|
-
typeof item.
|
|
201
|
-
typeof item.
|
|
202
|
-
throw new
|
|
252
|
+
if (typeof item.anchor_from !== "string" ||
|
|
253
|
+
typeof item.anchor_to !== "string" ||
|
|
254
|
+
typeof item.replace_with !== "string") {
|
|
255
|
+
throw new DomainError("E_BAD_PAYLOAD", {
|
|
256
|
+
message: `Edit request edits[${index}] must be { anchor_from, anchor_to, replace_with }: two bare 3-char anchors and the replacement text.`,
|
|
257
|
+
});
|
|
203
258
|
}
|
|
204
259
|
}
|
|
205
260
|
}
|
|
206
|
-
// legacy — now always fails with new shape message (batch_edit removed)
|
|
207
|
-
export function assertBatchEditRequest(_request) {
|
|
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], ...] }.");
|
|
209
|
-
}
|
|
210
261
|
export function assertReadRequest(request) {
|
|
211
262
|
if (!isRec(request))
|
|
212
|
-
throw new
|
|
263
|
+
throw new DomainError("E_BAD_PAYLOAD", { message: "Read request must be an object." });
|
|
213
264
|
rejectUnknownFields(request, READ_KS, "Read request");
|
|
214
265
|
if (typeof request.path !== "string" || request.path.length === 0) {
|
|
215
|
-
throw new
|
|
266
|
+
throw new DomainError("E_BAD_PAYLOAD", {
|
|
267
|
+
message: 'Read request requires a non-empty "path" string.',
|
|
268
|
+
});
|
|
216
269
|
}
|
|
217
270
|
}
|
|
218
271
|
export function assertUndoRequest(request) {
|
|
219
272
|
if (!isRec(request))
|
|
220
|
-
throw new
|
|
273
|
+
throw new DomainError("E_BAD_PAYLOAD", {
|
|
274
|
+
message: "undo_last_edit request must be an object.",
|
|
275
|
+
});
|
|
221
276
|
normalizeFilePath(request);
|
|
222
277
|
if (typeof request.path !== "string" || request.path.length === 0) {
|
|
223
|
-
throw new
|
|
278
|
+
throw new DomainError("E_BAD_PAYLOAD", {
|
|
279
|
+
message: 'undo_last_edit request requires a non-empty "path" string.',
|
|
280
|
+
});
|
|
224
281
|
}
|
|
225
282
|
}
|
|
226
283
|
// ---- shared JSON Schema literals (co-located with field sets) ---------------
|
|
227
|
-
export const
|
|
284
|
+
export const replaceWithSchema = {
|
|
228
285
|
type: "string",
|
|
229
|
-
description: '
|
|
286
|
+
description: 'Bare file content for the range; use "" to delete',
|
|
230
287
|
};
|
|
231
|
-
export const
|
|
288
|
+
export const anchorFromSchema = {
|
|
232
289
|
type: "string",
|
|
233
|
-
description: "
|
|
290
|
+
description: "Bare 3-char hash anchor of the first range line (inclusive)",
|
|
234
291
|
};
|
|
235
|
-
export const
|
|
292
|
+
export const anchorToSchema = {
|
|
236
293
|
type: "string",
|
|
237
|
-
description: "
|
|
294
|
+
description: "Bare 3-char hash anchor of the last range line (inclusive)",
|
|
238
295
|
};
|
|
239
296
|
export const pathSchema = {
|
|
240
297
|
type: "string",
|
|
@@ -248,21 +305,21 @@ export const editPathSchema = {
|
|
|
248
305
|
};
|
|
249
306
|
export const editTupleSchema = {
|
|
250
307
|
type: "array",
|
|
251
|
-
prefixItems: [
|
|
308
|
+
prefixItems: [anchorFromSchema, anchorToSchema, replaceWithSchema],
|
|
252
309
|
minItems: 3,
|
|
253
310
|
maxItems: 3,
|
|
254
|
-
description: "[
|
|
311
|
+
description: "[anchor_from, anchor_to, replace_with] (legacy tuple form)",
|
|
255
312
|
};
|
|
256
313
|
export const editObjectSchema = {
|
|
257
314
|
type: "object",
|
|
258
315
|
additionalProperties: false,
|
|
259
|
-
required: ["
|
|
316
|
+
required: ["anchor_from", "anchor_to", "replace_with"],
|
|
260
317
|
properties: {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
318
|
+
anchor_from: anchorFromSchema,
|
|
319
|
+
anchor_to: anchorToSchema,
|
|
320
|
+
replace_with: replaceWithSchema,
|
|
264
321
|
},
|
|
265
|
-
description: "{
|
|
322
|
+
description: "{anchor_from, anchor_to, replace_with}",
|
|
266
323
|
};
|
|
267
324
|
export const editItemSchema = {
|
|
268
325
|
anyOf: [editTupleSchema, editObjectSchema],
|
|
@@ -271,12 +328,12 @@ export const editItemSchema = {
|
|
|
271
328
|
export const editToolSchema = {
|
|
272
329
|
type: "object",
|
|
273
330
|
additionalProperties: false,
|
|
274
|
-
required: ["
|
|
331
|
+
required: ["file", "edits"],
|
|
275
332
|
properties: {
|
|
276
|
-
|
|
333
|
+
file: editPathSchema,
|
|
277
334
|
edits: {
|
|
278
335
|
type: "array",
|
|
279
|
-
description: "Ordered list of edit entries — each entry is
|
|
336
|
+
description: "Ordered list of edit entries — each entry is a named object {anchor_from, anchor_to, replace_with} (legacy tuples still fold pre-validation)",
|
|
280
337
|
minItems: 1,
|
|
281
338
|
maxItems: EDITS_MAX_ITEMS,
|
|
282
339
|
items: editItemSchema,
|