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
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
*
|
|
15
15
|
* @module dsh-better-edit/file-encoding-state
|
|
16
16
|
*/
|
|
17
|
-
import { detectBom, isValidUtf8, decodeBytes, normalizeEncoding, top3Candidates, chardetTop3Candidates } from "./encoding.js";
|
|
18
|
-
import { detectEnding } from "./edit-diff.js";
|
|
17
|
+
import { detectBom, isValidUtf8, decodeBytes, normalizeEncoding, top3Candidates, chardetTop3Candidates, } from "./encoding.js";
|
|
18
|
+
import { detectEnding, restoreEndings, toLF } from "./edit-diff.js";
|
|
19
19
|
const encodingMemo = new Map();
|
|
20
20
|
const autoGuessFooterMemo = new Map();
|
|
21
21
|
export function getEncodingState(targetKey) {
|
|
@@ -52,19 +52,29 @@ export function invalidateIfStale(targetKey, currentVersion) {
|
|
|
52
52
|
// Pure helpers — no IO
|
|
53
53
|
// ---------------------------------------------------------------------------
|
|
54
54
|
export function buildTop3ErrorMessage(displayPath, candidates) {
|
|
55
|
-
const candStr = candidates
|
|
55
|
+
const candStr = candidates
|
|
56
|
+
.map((c) => `${c.encoding}("${c.sample.slice(0, 20).replace(/"/g, "'")}")`)
|
|
57
|
+
.join(", ");
|
|
56
58
|
return `[MODEL] [E_UNSUPPORTED_FILE] Path is not a readable UTF-8 text file: ${displayPath}. Hashline editing only supports text files. Top-3 guesses: ${candStr}. Try read({encoding: "<encoding>"}) or set DSH_BETTER_EDIT_AUTO_GUESS_ENCODING=true to auto-decode.`;
|
|
57
59
|
}
|
|
58
60
|
function buildAutoGuessFooterFromCandidates(candidates) {
|
|
59
61
|
// chardet path vs heuristic path share same footer shape, just score vs confidence
|
|
60
62
|
const candsStr = candidates
|
|
61
63
|
.map((c) => {
|
|
62
|
-
const score = c.confidence !== undefined
|
|
64
|
+
const score = c.confidence !== undefined
|
|
65
|
+
? String(c.confidence)
|
|
66
|
+
: c.score !== undefined
|
|
67
|
+
? c.score.toFixed(0)
|
|
68
|
+
: "0";
|
|
63
69
|
return `${c.encoding} ${score}`;
|
|
64
70
|
})
|
|
65
71
|
.join(", ");
|
|
66
72
|
const top = candidates[0];
|
|
67
|
-
const topScore = top.confidence !== undefined
|
|
73
|
+
const topScore = top.confidence !== undefined
|
|
74
|
+
? String(top.confidence)
|
|
75
|
+
: top.score !== undefined
|
|
76
|
+
? top.score.toFixed(0)
|
|
77
|
+
: "0";
|
|
68
78
|
return `\n\n[Auto-guessed: ${top.encoding} ${topScore}, candidates: ${candsStr} — re-read with read({encoding}) if garbled]`;
|
|
69
79
|
}
|
|
70
80
|
function isMidConfidenceChardet(top, second) {
|
|
@@ -95,7 +105,12 @@ export async function decodeForOpen(bytes, config, opts = {}) {
|
|
|
95
105
|
const slice = bom ? bytes.subarray(off) : bytes;
|
|
96
106
|
// For BOM encodings, decode the full bytes (including BOM) via helper; otherwise hint
|
|
97
107
|
let decoded;
|
|
98
|
-
if (bom &&
|
|
108
|
+
if (bom &&
|
|
109
|
+
(bom.encoding === "utf8bom" ||
|
|
110
|
+
bom.encoding === "utf16le" ||
|
|
111
|
+
bom.encoding === "utf16be" ||
|
|
112
|
+
bom.encoding === "utf32le" ||
|
|
113
|
+
bom.encoding === "utf32be")) {
|
|
99
114
|
// When hint differs from BOM, hint takes precedence for decoding the slice
|
|
100
115
|
// but we preserve hasBOM from BOM sniff. For utf variants, BOM encoding
|
|
101
116
|
// already handled; for explicit hint we decode slice with hint.
|
|
@@ -166,7 +181,11 @@ export async function decodeForOpen(bytes, config, opts = {}) {
|
|
|
166
181
|
hasBOM: false,
|
|
167
182
|
lineEnding: detectEnding(dec),
|
|
168
183
|
footer,
|
|
169
|
-
candidates: chardetCands.map((c) => ({
|
|
184
|
+
candidates: chardetCands.map((c) => ({
|
|
185
|
+
encoding: c.encoding,
|
|
186
|
+
sample: c.sample,
|
|
187
|
+
score: c.confidence,
|
|
188
|
+
})),
|
|
170
189
|
};
|
|
171
190
|
}
|
|
172
191
|
}
|
|
@@ -204,7 +223,11 @@ export async function decodeForOpen(bytes, config, opts = {}) {
|
|
|
204
223
|
try {
|
|
205
224
|
const chardetCands = await chardetTop3Candidates(bytes, config.supportedEncodings);
|
|
206
225
|
if (chardetCands.length > 0) {
|
|
207
|
-
candidates = chardetCands.map((c) => ({
|
|
226
|
+
candidates = chardetCands.map((c) => ({
|
|
227
|
+
encoding: c.encoding,
|
|
228
|
+
sample: c.sample,
|
|
229
|
+
score: c.confidence,
|
|
230
|
+
}));
|
|
208
231
|
}
|
|
209
232
|
else {
|
|
210
233
|
candidates = top3Candidates(bytes, config.supportedEncodings);
|
|
@@ -233,6 +256,9 @@ export function recordFooter(targetKey, footer) {
|
|
|
233
256
|
if (footer)
|
|
234
257
|
setAutoGuessFooter(targetKey, footer);
|
|
235
258
|
}
|
|
259
|
+
/** Shared UTF-8 BOM marker — single source for the BOM round-trip seam.
|
|
260
|
+
* Prefer `prepareForSave` / `stripBOM` over re-implementing this check. */
|
|
261
|
+
export const UTF8_BOM = "\uFEFF";
|
|
236
262
|
/**
|
|
237
263
|
* Decide what bytes (as text, since provider expects string) to write and
|
|
238
264
|
* what new state to record. BOM and lineEnding are restored here when a
|
|
@@ -253,11 +279,24 @@ export function prepareForSave(content, existingState, opts = {}) {
|
|
|
253
279
|
newState: { encoding: norm, hasBOM, lineEnding, version: undefined },
|
|
254
280
|
};
|
|
255
281
|
}
|
|
256
|
-
if (existingState &&
|
|
282
|
+
if (existingState &&
|
|
283
|
+
!opts.normalizeToUtf8 &&
|
|
284
|
+
existingState.encoding !== "utf8" &&
|
|
285
|
+
existingState.encoding !== "utf8bom") {
|
|
257
286
|
// Legacy file, normalize false — preserve memo on wire (no transcode),
|
|
258
287
|
// provider writes UTF-8 string, version bump will invalidate on next read.
|
|
259
288
|
return { textToWrite: content };
|
|
260
289
|
}
|
|
261
|
-
// Default:
|
|
262
|
-
|
|
290
|
+
// Default: restore recorded BOM + lineEnding via the shared seam so
|
|
291
|
+
// governed writers (e.g. the str_replace_editor shadow) don't re-implement
|
|
292
|
+
// the round-trip locally and diverge on future utf16le/lineEnding evolution.
|
|
293
|
+
// Only add, never strip: a missing memo means UTF-8 without BOM + LF.
|
|
294
|
+
let textToWrite = content;
|
|
295
|
+
if (existingState?.lineEnding) {
|
|
296
|
+
textToWrite = restoreEndings(toLF(textToWrite), existingState.lineEnding);
|
|
297
|
+
}
|
|
298
|
+
if (existingState?.hasBOM && !textToWrite.startsWith(UTF8_BOM)) {
|
|
299
|
+
textToWrite = `${UTF8_BOM}${textToWrite}`;
|
|
300
|
+
}
|
|
301
|
+
return { textToWrite };
|
|
263
302
|
}
|
package/lib/file-view.js
CHANGED
|
@@ -33,8 +33,8 @@ export const DEFAULT_MAX_BYTES = 50 * 1024;
|
|
|
33
33
|
function splitLinesForCounting(content) {
|
|
34
34
|
if (content.length === 0)
|
|
35
35
|
return [];
|
|
36
|
-
const lines = content.split(
|
|
37
|
-
if (content.endsWith(
|
|
36
|
+
const lines = content.split("\n");
|
|
37
|
+
if (content.endsWith("\n"))
|
|
38
38
|
lines.pop();
|
|
39
39
|
return lines;
|
|
40
40
|
}
|
|
@@ -48,7 +48,7 @@ export function formatSize(bytes) {
|
|
|
48
48
|
export function truncateHead(content, options = {}) {
|
|
49
49
|
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
|
50
50
|
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
51
|
-
const totalBytes = Buffer.byteLength(content,
|
|
51
|
+
const totalBytes = Buffer.byteLength(content, "utf-8");
|
|
52
52
|
const lines = splitLinesForCounting(content);
|
|
53
53
|
const totalLines = lines.length;
|
|
54
54
|
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
|
@@ -66,12 +66,12 @@ export function truncateHead(content, options = {}) {
|
|
|
66
66
|
maxBytes,
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
|
-
const firstLineBytes = Buffer.byteLength(lines[0] ??
|
|
69
|
+
const firstLineBytes = Buffer.byteLength(lines[0] ?? "", "utf-8");
|
|
70
70
|
if (firstLineBytes > maxBytes) {
|
|
71
71
|
return {
|
|
72
|
-
content:
|
|
72
|
+
content: "",
|
|
73
73
|
truncated: true,
|
|
74
|
-
truncatedBy:
|
|
74
|
+
truncatedBy: "bytes",
|
|
75
75
|
totalLines,
|
|
76
76
|
totalBytes,
|
|
77
77
|
outputLines: 0,
|
|
@@ -84,22 +84,22 @@ export function truncateHead(content, options = {}) {
|
|
|
84
84
|
}
|
|
85
85
|
const outputLinesArr = [];
|
|
86
86
|
let outputBytesCount = 0;
|
|
87
|
-
let truncatedBy =
|
|
87
|
+
let truncatedBy = "lines";
|
|
88
88
|
for (let i = 0; i < lines.length && i < maxLines; i++) {
|
|
89
89
|
const line = lines[i];
|
|
90
|
-
const lineBytes = Buffer.byteLength(line,
|
|
90
|
+
const lineBytes = Buffer.byteLength(line, "utf-8") + (i > 0 ? 1 : 0);
|
|
91
91
|
if (outputBytesCount + lineBytes > maxBytes) {
|
|
92
|
-
truncatedBy =
|
|
92
|
+
truncatedBy = "bytes";
|
|
93
93
|
break;
|
|
94
94
|
}
|
|
95
95
|
outputLinesArr.push(line);
|
|
96
96
|
outputBytesCount += lineBytes;
|
|
97
97
|
}
|
|
98
98
|
if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
|
|
99
|
-
truncatedBy =
|
|
99
|
+
truncatedBy = "lines";
|
|
100
100
|
}
|
|
101
|
-
const outputContent = outputLinesArr.join(
|
|
102
|
-
const finalOutputBytes = Buffer.byteLength(outputContent,
|
|
101
|
+
const outputContent = outputLinesArr.join("\n");
|
|
102
|
+
const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
|
|
103
103
|
return {
|
|
104
104
|
content: outputContent,
|
|
105
105
|
truncated: true,
|
|
@@ -115,12 +115,7 @@ export function truncateHead(content, options = {}) {
|
|
|
115
115
|
};
|
|
116
116
|
}
|
|
117
117
|
// --- File kind (from file-kind.ts, private) ---
|
|
118
|
-
const IMG_TYPES = new Set([
|
|
119
|
-
"image/jpeg",
|
|
120
|
-
"image/png",
|
|
121
|
-
"image/gif",
|
|
122
|
-
"image/webp",
|
|
123
|
-
]);
|
|
118
|
+
const IMG_TYPES = new Set(["image/jpeg", "image/png", "image/gif", "image/webp"]);
|
|
124
119
|
const TEXT_TYPES = new Set([
|
|
125
120
|
"application/rtf",
|
|
126
121
|
"application/xml",
|
|
@@ -181,8 +176,7 @@ export async function loadFileKindAndText(filePath, options) {
|
|
|
181
176
|
};
|
|
182
177
|
}
|
|
183
178
|
const detectedMimeType = (await fileTypeFromBuffer(sample))?.mime;
|
|
184
|
-
if (detectedMimeType !== undefined &&
|
|
185
|
-
!isTextType(detectedMimeType)) {
|
|
179
|
+
if (detectedMimeType !== undefined && !isTextType(detectedMimeType)) {
|
|
186
180
|
if (IMG_TYPES.has(detectedMimeType)) {
|
|
187
181
|
return { kind: "image", mimeType: detectedMimeType };
|
|
188
182
|
}
|
|
@@ -338,17 +332,16 @@ function normPosInt(value, name) {
|
|
|
338
332
|
return value;
|
|
339
333
|
}
|
|
340
334
|
export function formatPaginationHint(startLine, endLine, totalLines, nextOffset, byteLimit) {
|
|
341
|
-
const sizeSuffix = byteLimit !== undefined ? ` (${formatSize(byteLimit)} limit)` :
|
|
335
|
+
const sizeSuffix = byteLimit !== undefined ? ` (${formatSize(byteLimit)} limit)` : "";
|
|
342
336
|
return `[Showing lines ${startLine}-${endLine} of ${totalLines}${sizeSuffix}. Use offset=${nextOffset} to continue.]`;
|
|
343
337
|
}
|
|
344
338
|
export async function fmtReadPreview(text, options, precomputedHashes, path, maxLineBytes = MAX_READ_LINE_BYTES, maxTruncLines = DEFAULT_MAX_LINES) {
|
|
345
339
|
const allLines = visLines(text);
|
|
346
340
|
const totalLines = allLines.length;
|
|
347
|
-
const startLine = normPosInt(options.offset,
|
|
341
|
+
const startLine = normPosInt(options.offset, "offset") ?? 1;
|
|
348
342
|
if (totalLines === 0) {
|
|
349
343
|
if (startLine === 1) {
|
|
350
|
-
const allHashes = precomputedHashes ??
|
|
351
|
-
(await (path ? lineHashes(text, path) : lineHashes(text)));
|
|
344
|
+
const allHashes = precomputedHashes ?? (await (path ? lineHashes(text, path) : lineHashes(text)));
|
|
352
345
|
const emptyLineHash = allHashes[0];
|
|
353
346
|
return {
|
|
354
347
|
text: `${emptyLineHash}${HASH_SEP}\n[File is empty. Use edit to insert content.]`,
|
|
@@ -366,43 +359,37 @@ export async function fmtReadPreview(text, options, precomputedHashes, path, max
|
|
|
366
359
|
served: [],
|
|
367
360
|
};
|
|
368
361
|
}
|
|
369
|
-
const limit = normPosInt(options.limit,
|
|
370
|
-
const endIdx = limit
|
|
371
|
-
? Math.min(startLine - 1 + limit, totalLines)
|
|
372
|
-
: totalLines;
|
|
362
|
+
const limit = normPosInt(options.limit, "limit");
|
|
363
|
+
const endIdx = limit ? Math.min(startLine - 1 + limit, totalLines) : totalLines;
|
|
373
364
|
const selected = allLines.slice(startLine - 1, endIdx);
|
|
374
|
-
const allHashes = precomputedHashes ??
|
|
375
|
-
(await (path ? lineHashes(text, path) : lineHashes(text)));
|
|
365
|
+
const allHashes = precomputedHashes ?? (await (path ? lineHashes(text, path) : lineHashes(text)));
|
|
376
366
|
const selectedHashes = allHashes.slice(startLine - 1, endIdx);
|
|
377
367
|
const formatted = fmtRegion(selectedHashes, selected);
|
|
378
368
|
const maxBytes = maxLineBytes;
|
|
379
369
|
const rowSizes = selected.map((line, index) => ({
|
|
380
370
|
lineNumber: startLine + index,
|
|
381
|
-
bytes: Buffer.byteLength(`${selectedHashes[index]}${HASH_SEP}${line}`,
|
|
371
|
+
bytes: Buffer.byteLength(`${selectedHashes[index]}${HASH_SEP}${line}`, "utf-8"),
|
|
382
372
|
}));
|
|
383
373
|
if (rowSizes.some((row) => row.bytes > maxBytes)) {
|
|
384
374
|
const oversized = rowSizes.filter((row) => row.bytes > maxBytes);
|
|
385
375
|
const rows = rowSizes.map((row, index) => row.bytes > maxBytes
|
|
386
376
|
? `[Line ${row.lineNumber} is ${formatSize(row.bytes)}, exceeds ${formatSize(maxBytes)}; content not shown. Use bash: sed -n '${row.lineNumber}p' <path> | head -c ${maxBytes}]`
|
|
387
377
|
: fmtRegion([selectedHashes[index]], [selected[index]]));
|
|
388
|
-
const skippedTruncation = truncateHead(rows.join(
|
|
378
|
+
const skippedTruncation = truncateHead(rows.join("\n"), {
|
|
389
379
|
maxBytes,
|
|
390
380
|
maxLines: maxTruncLines,
|
|
391
381
|
});
|
|
392
|
-
const shownRowCount = skippedTruncation.content ===
|
|
393
|
-
? 0
|
|
394
|
-
: skippedTruncation.content.split('\n').length;
|
|
382
|
+
const shownRowCount = skippedTruncation.content === "" ? 0 : skippedTruncation.content.split("\n").length;
|
|
395
383
|
const lastShownLine = shownRowCount > 0 ? startLine + shownRowCount - 1 : startLine - 1;
|
|
396
384
|
const lineLabel = oversized.length === 1
|
|
397
385
|
? `Line ${oversized[0].lineNumber}`
|
|
398
|
-
: `Lines ${oversized.map((row) => row.lineNumber).join(
|
|
399
|
-
const verb = oversized.length === 1 ?
|
|
400
|
-
const addresses = oversized.map((row) => `${row.lineNumber}p`).join(
|
|
386
|
+
: `Lines ${oversized.map((row) => row.lineNumber).join(", ")}`;
|
|
387
|
+
const verb = oversized.length === 1 ? "exceeds" : "exceed";
|
|
388
|
+
const addresses = oversized.map((row) => `${row.lineNumber}p`).join(";");
|
|
401
389
|
const warning = `[${lineLabel} ${verb} ${formatSize(maxBytes)}; content not shown because hashline anchors require full lines. Inspect with bash: sed -n '${addresses}' <path> | head -c ${maxBytes}]`;
|
|
402
390
|
let preview = skippedTruncation.content;
|
|
403
391
|
let nextOffset;
|
|
404
|
-
if (shownRowCount > 0 &&
|
|
405
|
-
(skippedTruncation.truncated || lastShownLine < totalLines)) {
|
|
392
|
+
if (shownRowCount > 0 && (skippedTruncation.truncated || lastShownLine < totalLines)) {
|
|
406
393
|
nextOffset = lastShownLine + 1;
|
|
407
394
|
preview += `\n\n${warning}\n${formatPaginationHint(startLine, lastShownLine, totalLines, nextOffset, skippedTruncation.truncated ? skippedTruncation.maxBytes : undefined)}`;
|
|
408
395
|
}
|
|
@@ -434,7 +421,7 @@ export async function fmtReadPreview(text, options, precomputedHashes, path, max
|
|
|
434
421
|
if (truncation.truncated) {
|
|
435
422
|
const endLineDisplay = startLine + truncation.outputLines - 1;
|
|
436
423
|
nextOffset = endLineDisplay + 1;
|
|
437
|
-
if (truncation.truncatedBy ===
|
|
424
|
+
if (truncation.truncatedBy === "lines") {
|
|
438
425
|
preview += `\n\n${formatPaginationHint(startLine, endLineDisplay, totalLines, nextOffset)}`;
|
|
439
426
|
}
|
|
440
427
|
else {
|
package/lib/fs-bridge.js
CHANGED
|
@@ -34,8 +34,7 @@ export function getAutoGuessFooterCompat(k) {
|
|
|
34
34
|
* @returns the mapped error, rethrown.
|
|
35
35
|
*/
|
|
36
36
|
export function mapFsError(error, displayPath) {
|
|
37
|
-
if (error instanceof Error &&
|
|
38
|
-
typeof error.code === "string") {
|
|
37
|
+
if (error instanceof Error && typeof error.code === "string") {
|
|
39
38
|
const code = error.code;
|
|
40
39
|
if (code === "FS_NOT_FOUND") {
|
|
41
40
|
throw new Error(`[MODEL] [E_NOT_FOUND] File not found: ${displayPath}`);
|
|
@@ -83,8 +82,7 @@ async function restoreStrippedUtf8Bom(fs, target, text, signal) {
|
|
|
83
82
|
if (text.startsWith(UTF8_BOM))
|
|
84
83
|
return text;
|
|
85
84
|
const info = await fs.stat(target, signal);
|
|
86
|
-
if (info?.size === undefined ||
|
|
87
|
-
info.size !== Buffer.byteLength(text, "utf-8") + UTF8_BOM_LEN) {
|
|
85
|
+
if (info?.size === undefined || info.size !== Buffer.byteLength(text, "utf-8") + UTF8_BOM_LEN) {
|
|
88
86
|
return text;
|
|
89
87
|
}
|
|
90
88
|
const bytes = await fs.readBytes(target, signal, info.size);
|
|
@@ -114,12 +112,17 @@ export function ctxFsIO(fs, ctx) {
|
|
|
114
112
|
async readText(absolutePath, signal, encodingHint) {
|
|
115
113
|
// explicit encoding override (Reopen with Encoding) — bytes + seam
|
|
116
114
|
if (encodingHint) {
|
|
117
|
-
const target = await fs.resolve(absolutePath, {
|
|
115
|
+
const target = await fs.resolve(absolutePath, {
|
|
116
|
+
...(signal === undefined ? {} : { signal }),
|
|
117
|
+
});
|
|
118
118
|
const info = await fs.stat(target, signal);
|
|
119
119
|
const maxBytes = info?.size ?? 10 * 1024 * 1024;
|
|
120
120
|
const bytes = await fs.readBytes(target, signal, maxBytes);
|
|
121
121
|
const cfg = loadConfig();
|
|
122
|
-
const decoded = await decodeForOpen(bytes, cfg, {
|
|
122
|
+
const decoded = await decodeForOpen(bytes, cfg, {
|
|
123
|
+
encodingHint,
|
|
124
|
+
displayPath: absolutePath,
|
|
125
|
+
});
|
|
123
126
|
const targetKey = String(target.targetKey ?? absolutePath);
|
|
124
127
|
recordOpenState(targetKey, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
|
|
125
128
|
if (decoded.footer)
|
|
@@ -154,7 +157,9 @@ export function ctxFsIO(fs, ctx) {
|
|
|
154
157
|
const isNotText = error instanceof Error && error.code === "FS_NOT_TEXT";
|
|
155
158
|
if (isNotText) {
|
|
156
159
|
const cfg = loadConfig();
|
|
157
|
-
const target = await fs.resolve(absolutePath, {
|
|
160
|
+
const target = await fs.resolve(absolutePath, {
|
|
161
|
+
...(signal === undefined ? {} : { signal }),
|
|
162
|
+
});
|
|
158
163
|
const info = await fs.stat(target, signal);
|
|
159
164
|
const maxBytes = info?.size ?? 10 * 1024 * 1024;
|
|
160
165
|
if (info && info.size !== undefined && info.size > maxBytes)
|
|
@@ -174,7 +179,9 @@ export function ctxFsIO(fs, ctx) {
|
|
|
174
179
|
async writeText(absolutePath, content, signal, exec, sandboxPolicy) {
|
|
175
180
|
// handle drift invalidation before encode — seam owns version check
|
|
176
181
|
try {
|
|
177
|
-
const targetTmp = await fs.resolve(absolutePath, {
|
|
182
|
+
const targetTmp = await fs.resolve(absolutePath, {
|
|
183
|
+
...(signal === undefined ? {} : { signal }),
|
|
184
|
+
});
|
|
178
185
|
const key = String(targetTmp.targetKey ?? absolutePath);
|
|
179
186
|
const info = await fs.stat(targetTmp, signal).catch(() => undefined);
|
|
180
187
|
invalidateIfStale(key, info?.version);
|
|
@@ -248,7 +255,10 @@ export function localIO() {
|
|
|
248
255
|
if (encodingHint) {
|
|
249
256
|
const bytes = await readFile(absolutePath);
|
|
250
257
|
const cfg = loadConfig();
|
|
251
|
-
const decoded = await decodeForOpen(bytes, cfg, {
|
|
258
|
+
const decoded = await decodeForOpen(bytes, cfg, {
|
|
259
|
+
encodingHint,
|
|
260
|
+
displayPath: absolutePath,
|
|
261
|
+
});
|
|
252
262
|
const info = await fileSnap(absolutePath).catch(() => undefined);
|
|
253
263
|
recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.snapshotId);
|
|
254
264
|
if (decoded.footer)
|
package/lib/fs-write.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { mkdir, open, readdir, rename, rm, stat, writeFile
|
|
2
|
+
import { mkdir, open, readdir, rename, rm, stat, writeFile } from "fs/promises";
|
|
3
3
|
import { dirname, join } from "node:path";
|
|
4
4
|
import { errCode } from "./utils.js";
|
|
5
5
|
import { resolveTarget } from "./paths.js";
|
|
@@ -9,12 +9,7 @@ import { errCode } from "../utils.js";
|
|
|
9
9
|
import { isBlankOverride } from "./parse.js";
|
|
10
10
|
import { GUIDANCE_SECTIONS } from "./resolve.js";
|
|
11
11
|
/** The presets shipped by the harness, each seeded with editable guidance. */
|
|
12
|
-
export const DEFAULT_PRESETS = [
|
|
13
|
-
"standard",
|
|
14
|
-
"code",
|
|
15
|
-
"minimal",
|
|
16
|
-
"cordis",
|
|
17
|
-
];
|
|
12
|
+
export const DEFAULT_PRESETS = ["standard", "code", "minimal", "cordis"];
|
|
18
13
|
/**
|
|
19
14
|
* The root README: the per-preset customization convention users see in the
|
|
20
15
|
* plugin home.
|
package/lib/guidance/resolve.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { readFile } from "node:fs/promises";
|
|
9
9
|
import { join } from "node:path";
|
|
10
|
-
import { EDIT_GUIDANCE, READ_GUIDANCE, UNDO_GUIDANCE
|
|
10
|
+
import { EDIT_GUIDANCE, READ_GUIDANCE, UNDO_GUIDANCE } from "../prompts.js";
|
|
11
11
|
import { errCode } from "../utils.js";
|
|
12
12
|
import { isBlankOverride, parseSectionFile } from "./parse.js";
|
|
13
13
|
/**
|
package/lib/hash-store.js
CHANGED
|
@@ -21,8 +21,8 @@ import { hashStorePath } from "./store-tenancy.js";
|
|
|
21
21
|
import { onStoreOpen, setStoresGetter } from "./store-lifecycle.js";
|
|
22
22
|
import { workspaceCwd } from "./workspace-context.js";
|
|
23
23
|
import { errCode, splitLines } from "./utils.js";
|
|
24
|
-
import { initHasher, contentChecksum, HASH_RE, CANON_VERSION
|
|
25
|
-
import { HASH_STORE_VERSION, HASH_STORE_BUSY_TIMEOUT, SERVED_TTL_MS
|
|
24
|
+
import { initHasher, contentChecksum, HASH_RE, CANON_VERSION } from "./hashline/hash-assign.js";
|
|
25
|
+
import { HASH_STORE_VERSION, HASH_STORE_BUSY_TIMEOUT, SERVED_TTL_MS } from "./constants.js";
|
|
26
26
|
export function isValidHashList(value) {
|
|
27
27
|
if (!Array.isArray(value))
|
|
28
28
|
return false;
|
|
@@ -70,7 +70,9 @@ export function isValidRetiredEntry(value) {
|
|
|
70
70
|
if (typeof value !== "object" || value === null)
|
|
71
71
|
return false;
|
|
72
72
|
const v = value;
|
|
73
|
-
return typeof v.hash === "string" &&
|
|
73
|
+
return (typeof v.hash === "string" &&
|
|
74
|
+
(v.deathPos === null || typeof v.deathPos === "number") &&
|
|
75
|
+
HASH_RE.test(v.hash));
|
|
74
76
|
}
|
|
75
77
|
export function isValidRetiredEntries(value) {
|
|
76
78
|
if (!Array.isArray(value))
|
|
@@ -100,7 +102,7 @@ function parseRetiredJson(raw) {
|
|
|
100
102
|
if (!Array.isArray(parsed) || parsed.length === 0)
|
|
101
103
|
return [];
|
|
102
104
|
if (isValidRetiredEntries(parsed)) {
|
|
103
|
-
return parsed.map((e) =>
|
|
105
|
+
return parsed.map((e) => e.deathPos === -1 ? { hash: e.hash, deathPos: null } : e);
|
|
104
106
|
}
|
|
105
107
|
const out = [];
|
|
106
108
|
for (const e of parsed) {
|
|
@@ -200,10 +202,7 @@ function buildStore(db) {
|
|
|
200
202
|
"hashes TEXT NOT NULL, " +
|
|
201
203
|
"updated_at INTEGER NOT NULL" +
|
|
202
204
|
")");
|
|
203
|
-
db.exec("CREATE TABLE IF NOT EXISTS meta (" +
|
|
204
|
-
"key TEXT PRIMARY KEY, " +
|
|
205
|
-
"value TEXT NOT NULL" +
|
|
206
|
-
")");
|
|
205
|
+
db.exec("CREATE TABLE IF NOT EXISTS meta (" + "key TEXT PRIMARY KEY, " + "value TEXT NOT NULL" + ")");
|
|
207
206
|
db.exec("CREATE TABLE IF NOT EXISTS undo (" +
|
|
208
207
|
"path TEXT PRIMARY KEY, " +
|
|
209
208
|
"content TEXT NOT NULL, " +
|
|
@@ -213,17 +212,14 @@ function buildStore(db) {
|
|
|
213
212
|
"result_content TEXT NOT NULL, " +
|
|
214
213
|
"updated_at INTEGER NOT NULL" +
|
|
215
214
|
")");
|
|
216
|
-
const versionRow = db
|
|
217
|
-
.prepare("SELECT value FROM meta WHERE key = 'version'")
|
|
218
|
-
.get();
|
|
215
|
+
const versionRow = db.prepare("SELECT value FROM meta WHERE key = 'version'").get();
|
|
219
216
|
const versionChanged = versionRow !== undefined && versionRow.value !== String(HASH_STORE_VERSION);
|
|
220
217
|
if (versionChanged) {
|
|
221
218
|
db.exec("DELETE FROM snapshots");
|
|
222
219
|
db.exec("DELETE FROM undo");
|
|
223
220
|
}
|
|
224
221
|
const servedColumns = db.prepare("PRAGMA table_info(served)").all();
|
|
225
|
-
if (versionChanged ||
|
|
226
|
-
!servedColumns.some((column) => column.name === "session_id")) {
|
|
222
|
+
if (versionChanged || !servedColumns.some((column) => column.name === "session_id")) {
|
|
227
223
|
db.exec("DROP TABLE IF EXISTS served");
|
|
228
224
|
}
|
|
229
225
|
db.exec("CREATE TABLE IF NOT EXISTS served (" +
|
|
@@ -244,9 +240,7 @@ function buildStore(db) {
|
|
|
244
240
|
try {
|
|
245
241
|
db.exec("BEGIN IMMEDIATE");
|
|
246
242
|
migrationOpen = true;
|
|
247
|
-
const migrationColumns = db
|
|
248
|
-
.prepare("PRAGMA table_info(served)")
|
|
249
|
-
.all();
|
|
243
|
+
const migrationColumns = db.prepare("PRAGMA table_info(served)").all();
|
|
250
244
|
if (!migrationColumns.some((column) => column.name === "retired")) {
|
|
251
245
|
db.exec("ALTER TABLE served ADD COLUMN retired TEXT");
|
|
252
246
|
// Pre-fix snapshots and undo entries may already bind a remembered
|
|
@@ -282,10 +276,8 @@ function buildStore(db) {
|
|
|
282
276
|
if (!cardsColumns.some((column) => column.name === "cards")) {
|
|
283
277
|
db.exec("ALTER TABLE served ADD COLUMN cards TEXT");
|
|
284
278
|
}
|
|
285
|
-
db
|
|
286
|
-
|
|
287
|
-
"ON CONFLICT(key) DO UPDATE SET value = excluded.value")
|
|
288
|
-
.run(String(HASH_STORE_VERSION));
|
|
279
|
+
db.prepare("INSERT INTO meta (key, value) VALUES ('version', ?) " +
|
|
280
|
+
"ON CONFLICT(key) DO UPDATE SET value = excluded.value").run(String(HASH_STORE_VERSION));
|
|
289
281
|
const getStmt = db.prepare("SELECT hashes FROM snapshots WHERE path = ? AND checksum = ? AND line_count = ?");
|
|
290
282
|
const allStmt = db.prepare("SELECT path FROM snapshots UNION SELECT path FROM undo UNION SELECT path FROM served");
|
|
291
283
|
const allHashesStmt = db.prepare("SELECT path, hashes FROM snapshots");
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* @module dsh-better-edit/hashline/anchor-pipeline
|
|
18
18
|
*/
|
|
19
19
|
import { abortIf, splitLines, rejectUnknownFields, clipLine, CodedError } from "../utils.js";
|
|
20
|
-
import { HASH_CLASS, HL_BARE_PREFIX_RE, HL_PREFIX_PLUS_RE, HL_PREFIX_MINUS_RE, HASH_SEP, ANCHOR_LEN, ALPH_RE, canon, lineHashesPure, getCanonForHash, rememberHashCanon } from "./hash-assign.js";
|
|
20
|
+
import { HASH_CLASS, HL_BARE_PREFIX_RE, HL_PREFIX_PLUS_RE, HL_PREFIX_MINUS_RE, HASH_SEP, ANCHOR_LEN, ALPH_RE, canon, lineHashesPure, getCanonForHash, rememberHashCanon, } from "./hash-assign.js";
|
|
21
21
|
import { recordServed, servedPositionsOf } from "../session-view.js";
|
|
22
22
|
import { SERVED_ECHO_CAP } from "../constants.js";
|
|
23
23
|
import { NEW_CONTENT_NOT_STRING_MSG } from "../constants.js";
|
|
@@ -48,8 +48,7 @@ function diagRef(ref) {
|
|
|
48
48
|
}
|
|
49
49
|
function parseRef(ref) {
|
|
50
50
|
const trimmed = ref.trim();
|
|
51
|
-
if (trimmed.length === ANCHOR_LEN &&
|
|
52
|
-
ALPH_RE.test(trimmed)) {
|
|
51
|
+
if (trimmed.length === ANCHOR_LEN && ALPH_RE.test(trimmed)) {
|
|
53
52
|
return { hash: trimmed };
|
|
54
53
|
}
|
|
55
54
|
throw new CodedError("E_BAD_ANCHOR", diagRef(ref));
|
|
@@ -155,8 +154,7 @@ function assertItem(edit) {
|
|
|
155
154
|
if (typeof edit.replacement_text !== "string") {
|
|
156
155
|
throw new CodedError("E_BAD_PAYLOAD", NEW_CONTENT_NOT_STRING_MSG);
|
|
157
156
|
}
|
|
158
|
-
if (typeof edit.remove_from !== "string" ||
|
|
159
|
-
typeof edit.remove_to !== "string") {
|
|
157
|
+
if (typeof edit.remove_from !== "string" || typeof edit.remove_to !== "string") {
|
|
160
158
|
throw new CodedError("E_BAD_PAYLOAD", `[MODEL] [E_BAD_PAYLOAD] The edit requires "remove_from" and "remove_to" anchor strings (3-char hashes from read output).`);
|
|
161
159
|
}
|
|
162
160
|
}
|
|
@@ -223,11 +221,11 @@ function stripBarePrefixes(edit, fileHashes, _warnings) {
|
|
|
223
221
|
});
|
|
224
222
|
if (stripped.length === 0)
|
|
225
223
|
return edit;
|
|
226
|
-
const locations = stripped
|
|
227
|
-
.map((s) => `replacement_text line ${s.lineIndex + 1}`)
|
|
228
|
-
.join(", ");
|
|
224
|
+
const locations = stripped.map((s) => `replacement_text line ${s.lineIndex + 1}`).join(", ");
|
|
229
225
|
const matchedCount = stripped.filter((s) => s.matched).length;
|
|
230
|
-
const evidence = matchedCount === 0
|
|
226
|
+
const evidence = matchedCount === 0
|
|
227
|
+
? "0 matched — verify literal 'HASH│' content"
|
|
228
|
+
: `${matchedCount}/${stripped.length} matched`;
|
|
231
229
|
if (matchedCount === stripped.length) {
|
|
232
230
|
throw new BadAnchorError(`[MODEL] [E_BAD_ANCHOR] stripped "HASH│" prefix from ${locations} (${evidence}) — use bare content without HASH│ next time.`, { ...edit, content_lines: contentLines });
|
|
233
231
|
}
|
|
@@ -251,9 +249,7 @@ function stripDiffPrefixes(edit, _warnings) {
|
|
|
251
249
|
});
|
|
252
250
|
if (stripped.length === 0)
|
|
253
251
|
return edit;
|
|
254
|
-
const locations = stripped
|
|
255
|
-
.map((i) => `replacement_text line ${i + 1}`)
|
|
256
|
-
.join(", ");
|
|
252
|
+
const locations = stripped.map((i) => `replacement_text line ${i + 1}`).join(", ");
|
|
257
253
|
throw new BadAnchorError(`[MODEL] [E_BAD_ANCHOR] stripped diff-preview marker from ${locations}.`, { ...edit, content_lines: contentLines });
|
|
258
254
|
}
|
|
259
255
|
/** @internal — private to anchor-pipeline seam */
|
|
@@ -265,9 +261,7 @@ function swapReversedRanges(edit, fileHashes, warnings) {
|
|
|
265
261
|
const [startRef, endRef] = edit.hash_bounds;
|
|
266
262
|
const startLine = lineByHash.get(startRef.hash);
|
|
267
263
|
const endLine = lineByHash.get(endRef.hash);
|
|
268
|
-
if (startLine === undefined ||
|
|
269
|
-
endLine === undefined ||
|
|
270
|
-
startLine <= endLine) {
|
|
264
|
+
if (startLine === undefined || endLine === undefined || startLine <= endLine) {
|
|
271
265
|
return edit;
|
|
272
266
|
}
|
|
273
267
|
warnings.push(`[USER] [E_REVERSED_ANCHORS] reversed remove_from/remove_to (${startRef.hash} after ${endRef.hash}); swapped (healed).`);
|
|
@@ -320,7 +314,9 @@ function valEdit(edit, fileLines, fileHashes, warnings, signal) {
|
|
|
320
314
|
mismatches,
|
|
321
315
|
};
|
|
322
316
|
}
|
|
323
|
-
export function findNewEdge() {
|
|
317
|
+
export function findNewEdge() {
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
324
320
|
export { warnUnicodeEsc };
|
|
325
321
|
export class ServedRejectionError extends CodedError {
|
|
326
322
|
code;
|
|
@@ -364,7 +360,9 @@ export function isAnchorMismatch(error) {
|
|
|
364
360
|
export function findEditHashEcho(replacementLines, served, startLine) {
|
|
365
361
|
for (let k = 0; k < replacementLines.length; k++) {
|
|
366
362
|
const pos = startLine + k - 1;
|
|
367
|
-
if (pos < served.length &&
|
|
363
|
+
if (pos < served.length &&
|
|
364
|
+
served[pos] !== null &&
|
|
365
|
+
replacementLines[k].startsWith(served[pos] + HASH_SEP)) {
|
|
368
366
|
return { k: k + 1, hash: served[pos] };
|
|
369
367
|
}
|
|
370
368
|
}
|
|
@@ -386,9 +384,7 @@ export function buildRangeEcho(startLine, endLine, fileHashes) {
|
|
|
386
384
|
return rows;
|
|
387
385
|
}
|
|
388
386
|
export function fmtServedRows(rows, fileLines) {
|
|
389
|
-
return rows
|
|
390
|
-
.map((row) => `${row.hash}${HASH_SEP}${fileLines[row.position] ?? ""}`)
|
|
391
|
-
.join("\n");
|
|
387
|
+
return rows.map((row) => `${row.hash}${HASH_SEP}${fileLines[row.position] ?? ""}`).join("\n");
|
|
392
388
|
}
|
|
393
389
|
function retryHint() {
|
|
394
390
|
return "Retry with these anchors (no read needed).";
|
|
@@ -397,7 +393,7 @@ function paginationHint(nextOffset, more) {
|
|
|
397
393
|
return `[... ${more} more — read offset=${nextOffset}]`;
|
|
398
394
|
}
|
|
399
395
|
export function verifyServedRange(args) {
|
|
400
|
-
const { served, startHash, endHash, startLine, endLine, fileHashes, fileLines, filePath
|
|
396
|
+
const { served, startHash, endHash, startLine, endLine, fileHashes, fileLines, filePath } = args;
|
|
401
397
|
const where = filePath ? ` in ${filePath}` : "";
|
|
402
398
|
const retiredSet = args.retired ?? new Set();
|
|
403
399
|
const servedCanons = args.servedCanons;
|
|
@@ -718,7 +714,9 @@ export function verifyServedRange(args) {
|
|
|
718
714
|
if (h && retiredSet.has(h)) {
|
|
719
715
|
const expectedCanon = servedCanons?.[from + k] ?? undefined;
|
|
720
716
|
const actualCanon = canon(fileLines[startLine - 1 + k] ?? "");
|
|
721
|
-
if (expectedCanon !== undefined &&
|
|
717
|
+
if (expectedCanon !== undefined &&
|
|
718
|
+
expectedCanon !== null &&
|
|
719
|
+
expectedCanon !== actualCanon) {
|
|
722
720
|
throw new ServedRejectionError({
|
|
723
721
|
code: "E_STALE_RANGE",
|
|
724
722
|
message: `[MODEL] [E_STALE_RANGE] line ${startLine + k}${where} uses retired anchor "${h}" (freed since last full read, canon changed). Re-read.\nCurrent range:\n${echo}`,
|
|
@@ -824,7 +822,7 @@ function resToSpan(edit, content, lineIndex) {
|
|
|
824
822
|
}
|
|
825
823
|
function assemble(content, span, signal) {
|
|
826
824
|
abortIf(signal);
|
|
827
|
-
return
|
|
825
|
+
return content.slice(0, span.start) + span.replacement + content.slice(span.end);
|
|
828
826
|
}
|
|
829
827
|
export function applyEdit(content, edit, signal, precomputedHashes, filePath, served, servedCanons, retired, epochSnapshotId, curSnapshotId, strictPos) {
|
|
830
828
|
abortIf(signal);
|
|
@@ -847,15 +845,19 @@ export function applyEdit(content, edit, signal, precomputedHashes, filePath, se
|
|
|
847
845
|
for (let i = 0; i < fileHashes.length; i++)
|
|
848
846
|
lineByHash.set(fileHashes[i], i + 1);
|
|
849
847
|
const startLine = lineByHash.get(stripped.hash_bounds[0].hash);
|
|
850
|
-
const echo = (startLine !== undefined
|
|
851
|
-
|
|
848
|
+
const echo = (startLine !== undefined
|
|
849
|
+
? findEditHashEcho(edit.content_lines, served, startLine)
|
|
850
|
+
: undefined) ??
|
|
851
|
+
(startLine !== undefined
|
|
852
|
+
? findEditHashEcho(stripped.content_lines, served, startLine)
|
|
853
|
+
: undefined);
|
|
852
854
|
if (echo) {
|
|
853
855
|
const msg = `[MODEL] [E_SERVED_ECHO] Refused edit to ${filePath ?? "(unknown file)"}: replacement line ${echo.k} begins with the exact ${echo.hash}${HASH_SEP} anchor served for this session, path, and range-relative line. Remove the copied anchors and retry. Nothing was written.`;
|
|
854
856
|
throw new EditHashEchoError(msg, []);
|
|
855
857
|
}
|
|
856
858
|
throw error;
|
|
857
859
|
}
|
|
858
|
-
const { resolved: initialResolved, mismatches
|
|
860
|
+
const { resolved: initialResolved, mismatches } = valEdit(prefixFixed, lineIndex.fileLines, fileHashes, warnings, signal);
|
|
859
861
|
if (mismatches.length || !initialResolved) {
|
|
860
862
|
const { message, servedRows } = fmtMismatchWithServes(mismatches, lineIndex.fileLines, fileHashes, filePath);
|
|
861
863
|
throw new AnchorMismatchError(message, servedRows);
|
|
@@ -931,9 +933,7 @@ export function fmtRegion(hashes, lines) {
|
|
|
931
933
|
if (hashes.length !== lines.length) {
|
|
932
934
|
throw new Error(`fmtRegion: hashes.length (${hashes.length}) must match lines.length (${lines.length}).`);
|
|
933
935
|
}
|
|
934
|
-
return lines
|
|
935
|
-
.map((line, index) => `${hashes[index]}${HASH_SEP}${line}`)
|
|
936
|
-
.join("\n");
|
|
936
|
+
return lines.map((line, index) => `${hashes[index]}${HASH_SEP}${line}`).join("\n");
|
|
937
937
|
}
|
|
938
938
|
export function changedRange(original, result) {
|
|
939
939
|
if (original === result)
|