dsh-better-edit 0.3.0 → 0.3.1
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 +21 -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/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/lib/file-view.js
CHANGED
|
@@ -2,38 +2,463 @@
|
|
|
2
2
|
* FileView — deep module owning "what the model sees".
|
|
3
3
|
*
|
|
4
4
|
* Single seam for normalize → hash → render → truncate → served-row
|
|
5
|
-
* selection.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* selection. Previously split across file-reader, read-render, truncate,
|
|
6
|
+
* file-kind, validation — each shallow (interface≈implementation). Now one
|
|
7
|
+
* file owns the invariant; deleting it would scatter complexity (deep).
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
9
|
+
* Private helpers inlined from file-reader (normFromText/fileSnap),
|
|
10
|
+
* read-render (fmtReadPreview), truncate (truncateHead), file-kind
|
|
11
|
+
* (loadFileKindAndText), validation (valKind/valAccess).
|
|
12
|
+
* Old files are shims re-exporting from this seam for compat.
|
|
13
13
|
*
|
|
14
14
|
* Two surfaces:
|
|
15
15
|
* - `preview` (pure, no IO) — tested without filesystem
|
|
16
16
|
* - `readView` (IO) — read + normalize + render + truncate + hashes
|
|
17
17
|
*
|
|
18
|
-
* The emergent interaction (MAX_HASH_LINES vs MAX_READ_LINE_BYTES vs
|
|
19
|
-
* truncation, partial serve on oversized lines) now lives in one module
|
|
20
|
-
* and is one test surface.
|
|
21
|
-
*
|
|
22
18
|
* @module dsh-better-edit/file-view
|
|
23
19
|
*/
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
import { constants } from "node:fs";
|
|
21
|
+
import { open as fsOpen, stat as fsStat } from "fs/promises";
|
|
22
|
+
import { access as fsAccess } from "fs/promises";
|
|
23
|
+
import { fileTypeFromBuffer } from "file-type";
|
|
24
|
+
import { SNIFF_BYTES, MAX_BYTES, MAX_READ_LINE_BYTES } from "./constants.js";
|
|
25
|
+
import { lineHashes, fmtRegion, HASH_SEP } from "./hashline/index.js";
|
|
26
|
+
import { HASH_SPACE } from "./hashline/hash-assign.js";
|
|
27
|
+
import { visLines, abortIf, errCode } from "./utils.js";
|
|
28
|
+
import { detectEnding, toLF, stripBOM } from "./edit-diff.js";
|
|
29
|
+
import { resolveTarget, toCwd } from "./paths.js";
|
|
30
|
+
export const MAX_HASH_LINES = HASH_SPACE;
|
|
31
|
+
export const DEFAULT_MAX_LINES = 2000;
|
|
32
|
+
export const DEFAULT_MAX_BYTES = 50 * 1024;
|
|
33
|
+
function splitLinesForCounting(content) {
|
|
34
|
+
if (content.length === 0)
|
|
35
|
+
return [];
|
|
36
|
+
const lines = content.split('\n');
|
|
37
|
+
if (content.endsWith('\n'))
|
|
38
|
+
lines.pop();
|
|
39
|
+
return lines;
|
|
40
|
+
}
|
|
41
|
+
export function formatSize(bytes) {
|
|
42
|
+
if (bytes < 1024)
|
|
43
|
+
return `${bytes}B`;
|
|
44
|
+
if (bytes < 1024 * 1024)
|
|
45
|
+
return `${(bytes / 1024).toFixed(1)}KB`;
|
|
46
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
|
|
47
|
+
}
|
|
48
|
+
export function truncateHead(content, options = {}) {
|
|
49
|
+
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
|
50
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
51
|
+
const totalBytes = Buffer.byteLength(content, 'utf-8');
|
|
52
|
+
const lines = splitLinesForCounting(content);
|
|
53
|
+
const totalLines = lines.length;
|
|
54
|
+
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
|
55
|
+
return {
|
|
56
|
+
content,
|
|
57
|
+
truncated: false,
|
|
58
|
+
truncatedBy: null,
|
|
59
|
+
totalLines,
|
|
60
|
+
totalBytes,
|
|
61
|
+
outputLines: totalLines,
|
|
62
|
+
outputBytes: totalBytes,
|
|
63
|
+
lastLinePartial: false,
|
|
64
|
+
firstLineExceedsLimit: false,
|
|
65
|
+
maxLines,
|
|
66
|
+
maxBytes,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const firstLineBytes = Buffer.byteLength(lines[0] ?? '', 'utf-8');
|
|
70
|
+
if (firstLineBytes > maxBytes) {
|
|
71
|
+
return {
|
|
72
|
+
content: '',
|
|
73
|
+
truncated: true,
|
|
74
|
+
truncatedBy: 'bytes',
|
|
75
|
+
totalLines,
|
|
76
|
+
totalBytes,
|
|
77
|
+
outputLines: 0,
|
|
78
|
+
outputBytes: 0,
|
|
79
|
+
lastLinePartial: false,
|
|
80
|
+
firstLineExceedsLimit: true,
|
|
81
|
+
maxLines,
|
|
82
|
+
maxBytes,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const outputLinesArr = [];
|
|
86
|
+
let outputBytesCount = 0;
|
|
87
|
+
let truncatedBy = 'lines';
|
|
88
|
+
for (let i = 0; i < lines.length && i < maxLines; i++) {
|
|
89
|
+
const line = lines[i];
|
|
90
|
+
const lineBytes = Buffer.byteLength(line, 'utf-8') + (i > 0 ? 1 : 0);
|
|
91
|
+
if (outputBytesCount + lineBytes > maxBytes) {
|
|
92
|
+
truncatedBy = 'bytes';
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
outputLinesArr.push(line);
|
|
96
|
+
outputBytesCount += lineBytes;
|
|
97
|
+
}
|
|
98
|
+
if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
|
|
99
|
+
truncatedBy = 'lines';
|
|
100
|
+
}
|
|
101
|
+
const outputContent = outputLinesArr.join('\n');
|
|
102
|
+
const finalOutputBytes = Buffer.byteLength(outputContent, 'utf-8');
|
|
103
|
+
return {
|
|
104
|
+
content: outputContent,
|
|
105
|
+
truncated: true,
|
|
106
|
+
truncatedBy,
|
|
107
|
+
totalLines,
|
|
108
|
+
totalBytes,
|
|
109
|
+
outputLines: outputLinesArr.length,
|
|
110
|
+
outputBytes: finalOutputBytes,
|
|
111
|
+
lastLinePartial: false,
|
|
112
|
+
firstLineExceedsLimit: false,
|
|
113
|
+
maxLines,
|
|
114
|
+
maxBytes,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
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
|
+
]);
|
|
124
|
+
const TEXT_TYPES = new Set([
|
|
125
|
+
"application/rtf",
|
|
126
|
+
"application/xml",
|
|
127
|
+
"application/x-ms-regedit",
|
|
128
|
+
]);
|
|
129
|
+
function detectTextBom(sample) {
|
|
130
|
+
if (sample.length >= 4 &&
|
|
131
|
+
sample[0] === 0xff &&
|
|
132
|
+
sample[1] === 0xfe &&
|
|
133
|
+
sample[2] === 0x00 &&
|
|
134
|
+
sample[3] === 0x00)
|
|
135
|
+
return "UTF-32LE";
|
|
136
|
+
if (sample.length >= 4 &&
|
|
137
|
+
sample[0] === 0x00 &&
|
|
138
|
+
sample[1] === 0x00 &&
|
|
139
|
+
sample[2] === 0xfe &&
|
|
140
|
+
sample[3] === 0xff)
|
|
141
|
+
return "UTF-32BE";
|
|
142
|
+
if (sample.length >= 2 && sample[0] === 0xff && sample[1] === 0xfe)
|
|
143
|
+
return "UTF-16LE";
|
|
144
|
+
if (sample.length >= 2 && sample[0] === 0xfe && sample[1] === 0xff)
|
|
145
|
+
return "UTF-16BE";
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
function isTextType(mimeType) {
|
|
149
|
+
return mimeType.startsWith("text/") || TEXT_TYPES.has(mimeType);
|
|
150
|
+
}
|
|
151
|
+
export async function loadFileKindAndText(filePath, options) {
|
|
152
|
+
const pathStat = await fsStat(filePath);
|
|
153
|
+
if (pathStat.isDirectory()) {
|
|
154
|
+
return { kind: "directory" };
|
|
155
|
+
}
|
|
156
|
+
if (!pathStat.isFile()) {
|
|
157
|
+
return {
|
|
158
|
+
kind: "binary",
|
|
159
|
+
description: "unsupported file type",
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
if (pathStat.size > MAX_BYTES) {
|
|
163
|
+
return {
|
|
164
|
+
kind: "binary",
|
|
165
|
+
description: `file exceeds ${MAX_BYTES} byte limit`,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
const fileHandle = await fsOpen(filePath, "r");
|
|
169
|
+
try {
|
|
170
|
+
const buffer = Buffer.alloc(SNIFF_BYTES);
|
|
171
|
+
const { bytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, 0);
|
|
172
|
+
if (bytesRead === 0) {
|
|
173
|
+
return { kind: "text", text: "" };
|
|
174
|
+
}
|
|
175
|
+
const sample = buffer.subarray(0, bytesRead);
|
|
176
|
+
const textBom = detectTextBom(sample);
|
|
177
|
+
if (textBom) {
|
|
178
|
+
return {
|
|
179
|
+
kind: "binary",
|
|
180
|
+
description: `${textBom} encoded text`,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
const detectedMimeType = (await fileTypeFromBuffer(sample))?.mime;
|
|
184
|
+
if (detectedMimeType !== undefined &&
|
|
185
|
+
!isTextType(detectedMimeType)) {
|
|
186
|
+
if (IMG_TYPES.has(detectedMimeType)) {
|
|
187
|
+
return { kind: "image", mimeType: detectedMimeType };
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
kind: "binary",
|
|
191
|
+
description: detectedMimeType,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
const decoder = new TextDecoder("utf-8", { fatal: false, ignoreBOM: true });
|
|
195
|
+
let hadUtf8DecodeErrors = false;
|
|
196
|
+
let newlineCount = 0;
|
|
197
|
+
const parts = [];
|
|
198
|
+
function decodeChunk(chunk, stream) {
|
|
199
|
+
const decoded = decoder.decode(chunk, { stream });
|
|
200
|
+
if (!hadUtf8DecodeErrors && decoded.includes("\uFFFD")) {
|
|
201
|
+
hadUtf8DecodeErrors = true;
|
|
202
|
+
}
|
|
203
|
+
if (options?.maxLines !== undefined) {
|
|
204
|
+
for (let i = 0; i < decoded.length; i++) {
|
|
205
|
+
if (decoded.charCodeAt(i) === 10)
|
|
206
|
+
newlineCount++;
|
|
207
|
+
}
|
|
208
|
+
if (newlineCount > options.maxLines) {
|
|
209
|
+
throw new Error(`[E_FILE_TOO_LARGE] ${options.displayPath ?? filePath} has more than ${options.maxLines} lines, exceeding the ${options.maxLines}-line edit limit. Hashline editing targets source-sized files; for very large files use write or a non-line-based approach.`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return decoded;
|
|
213
|
+
}
|
|
214
|
+
parts.push(decodeChunk(sample, true));
|
|
215
|
+
let position = bytesRead;
|
|
216
|
+
while (true) {
|
|
217
|
+
const { bytesRead: chunkBytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, position);
|
|
218
|
+
if (chunkBytesRead === 0) {
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
const chunk = buffer.subarray(0, chunkBytesRead);
|
|
222
|
+
parts.push(decodeChunk(chunk, true));
|
|
223
|
+
position += chunkBytesRead;
|
|
224
|
+
}
|
|
225
|
+
parts.push(decodeChunk(new Uint8Array(0), false));
|
|
226
|
+
return {
|
|
227
|
+
kind: "text",
|
|
228
|
+
text: parts.join(""),
|
|
229
|
+
...(hadUtf8DecodeErrors ? { hadUtf8DecodeErrors: true } : {}),
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
finally {
|
|
233
|
+
await fileHandle.close();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
// --- Validation (from validation.ts, private) ---
|
|
237
|
+
export async function valAccess(absolutePath, path, accessMode = constants.R_OK) {
|
|
238
|
+
try {
|
|
239
|
+
await fsAccess(absolutePath, accessMode);
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
const code = errCode(error);
|
|
243
|
+
if (code === "ENOENT") {
|
|
244
|
+
throw new Error(`[E_NOT_FOUND] File not found: ${path}`);
|
|
245
|
+
}
|
|
246
|
+
if (code === "EACCES" || code === "EPERM") {
|
|
247
|
+
const accessLabel = accessMode & constants.W_OK ? "not writable" : "not readable";
|
|
248
|
+
throw new Error(`[E_ACCESS] File is ${accessLabel}: ${path}`);
|
|
249
|
+
}
|
|
250
|
+
if (code === "ELOOP") {
|
|
251
|
+
throw new Error(`[E_ACCESS] Too many symbolic links while resolving: ${path}`);
|
|
252
|
+
}
|
|
253
|
+
throw new Error(`[E_ACCESS] Cannot access file: ${path}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
export function valKind(file, path) {
|
|
257
|
+
if (file.kind === "directory") {
|
|
258
|
+
throw new Error(`[E_NOT_TEXT] Path is a directory: ${path}. Use ls to inspect directories.`);
|
|
259
|
+
}
|
|
260
|
+
if (file.kind === "binary") {
|
|
261
|
+
throw new Error(`[E_NOT_TEXT] Path is a binary file: ${path} (${file.description}). Hashline edit only supports text files.`);
|
|
262
|
+
}
|
|
263
|
+
if (file.kind === "image") {
|
|
264
|
+
throw new Error(`[E_NOT_TEXT] Path is an image file: ${path}. Hashline edit only supports text files.`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
function fmtSnapId(canonicalPath, info) {
|
|
268
|
+
return `v2|${canonicalPath}|${info.ino}|${info.mtimeMs}|${info.ctimeMs}|${info.size}`;
|
|
269
|
+
}
|
|
270
|
+
export async function fileSnap(absolutePath) {
|
|
271
|
+
const canonicalPath = await resolveTarget(absolutePath);
|
|
272
|
+
const stats = await fsStat(canonicalPath);
|
|
273
|
+
return {
|
|
274
|
+
snapshotId: fmtSnapId(canonicalPath, stats),
|
|
275
|
+
ino: stats.ino,
|
|
276
|
+
mtimeMs: stats.mtimeMs,
|
|
277
|
+
ctimeMs: stats.ctimeMs,
|
|
278
|
+
size: stats.size,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
export async function normFromText(input) {
|
|
282
|
+
const { absolutePath, displayPath, signal } = input;
|
|
283
|
+
abortIf(signal);
|
|
284
|
+
const { bom, text: rawContent } = stripBOM(input.rawText);
|
|
285
|
+
const originalEnding = detectEnding(rawContent);
|
|
286
|
+
const normalized = toLF(rawContent);
|
|
287
|
+
if (input.maxLines !== undefined) {
|
|
288
|
+
const lineCount = visLines(normalized).length;
|
|
289
|
+
if (lineCount > input.maxLines) {
|
|
290
|
+
throw new Error(`[E_FILE_TOO_LARGE] ${displayPath} has ${lineCount} lines, exceeding the ${input.maxLines}-line edit limit. Hashline editing targets source-sized files; for very large files use write or a non-line-based approach.`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const fileHashes = await lineHashes(normalized, absolutePath, undefined, input.store, input.noPersist !== true);
|
|
294
|
+
return {
|
|
295
|
+
absolutePath,
|
|
296
|
+
normalized,
|
|
297
|
+
bom,
|
|
298
|
+
originalEnding,
|
|
299
|
+
fileHashes,
|
|
300
|
+
hadUtf8DecodeErrors: input.hadUtf8DecodeErrors === true,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
export async function readNormFile(path, cwd, options) {
|
|
304
|
+
const absolutePath = toCwd(path, cwd);
|
|
305
|
+
const resolvedPath = await resolveTarget(absolutePath);
|
|
306
|
+
const signal = options?.signal;
|
|
307
|
+
const accessMode = options?.accessMode ?? constants.R_OK;
|
|
308
|
+
abortIf(signal);
|
|
309
|
+
await valAccess(resolvedPath, path, accessMode);
|
|
310
|
+
abortIf(signal);
|
|
311
|
+
const file = options?.preloadedFile ??
|
|
312
|
+
(await loadFileKindAndText(resolvedPath, {
|
|
313
|
+
maxLines: options?.maxLines,
|
|
314
|
+
displayPath: path,
|
|
315
|
+
}));
|
|
316
|
+
valKind(file, path);
|
|
317
|
+
return normFromText({
|
|
318
|
+
absolutePath: resolvedPath,
|
|
319
|
+
rawText: file.text,
|
|
320
|
+
displayPath: path,
|
|
321
|
+
signal,
|
|
322
|
+
maxLines: options?.maxLines,
|
|
323
|
+
store: options?.store,
|
|
324
|
+
noPersist: options?.noPersist,
|
|
325
|
+
hadUtf8DecodeErrors: file.hadUtf8DecodeErrors,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
// --- Read render (from read-render.ts, private) ---
|
|
329
|
+
function normPosInt(value, name) {
|
|
330
|
+
if (value === undefined)
|
|
331
|
+
return undefined;
|
|
332
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
333
|
+
throw new Error(`[E_BAD_SHAPE] Read request field "${name}" must be a positive integer.`);
|
|
334
|
+
}
|
|
335
|
+
return value;
|
|
336
|
+
}
|
|
337
|
+
export function formatPaginationHint(startLine, endLine, totalLines, nextOffset, byteLimit) {
|
|
338
|
+
const sizeSuffix = byteLimit !== undefined ? ` (${formatSize(byteLimit)} limit)` : '';
|
|
339
|
+
return `[Showing lines ${startLine}-${endLine} of ${totalLines}${sizeSuffix}. Use offset=${nextOffset} to continue.]`;
|
|
340
|
+
}
|
|
341
|
+
export async function fmtReadPreview(text, options, precomputedHashes, path, maxLineBytes = MAX_READ_LINE_BYTES, maxTruncLines = DEFAULT_MAX_LINES) {
|
|
342
|
+
const allLines = visLines(text);
|
|
343
|
+
const totalLines = allLines.length;
|
|
344
|
+
const startLine = normPosInt(options.offset, 'offset') ?? 1;
|
|
345
|
+
if (totalLines === 0) {
|
|
346
|
+
if (startLine === 1) {
|
|
347
|
+
const allHashes = precomputedHashes ??
|
|
348
|
+
(await (path ? lineHashes(text, path) : lineHashes(text)));
|
|
349
|
+
const emptyLineHash = allHashes[0];
|
|
350
|
+
return {
|
|
351
|
+
text: `${emptyLineHash}${HASH_SEP}\n[File is empty. Use edit to insert content.]`,
|
|
352
|
+
served: [{ position: 0, hash: emptyLineHash }],
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
return {
|
|
356
|
+
text: `Offset ${startLine} is beyond end of file (0 lines total). The file is empty. Use edit to insert content.`,
|
|
357
|
+
served: [],
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
if (startLine > totalLines) {
|
|
361
|
+
return {
|
|
362
|
+
text: `Offset ${startLine} is beyond end of file (${totalLines} lines total). Use offset=1 to read from the start, or offset=${totalLines} to read the last line.`,
|
|
363
|
+
served: [],
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
const limit = normPosInt(options.limit, 'limit');
|
|
367
|
+
const endIdx = limit
|
|
368
|
+
? Math.min(startLine - 1 + limit, totalLines)
|
|
369
|
+
: totalLines;
|
|
370
|
+
const selected = allLines.slice(startLine - 1, endIdx);
|
|
371
|
+
const allHashes = precomputedHashes ??
|
|
372
|
+
(await (path ? lineHashes(text, path) : lineHashes(text)));
|
|
373
|
+
const selectedHashes = allHashes.slice(startLine - 1, endIdx);
|
|
374
|
+
const formatted = fmtRegion(selectedHashes, selected);
|
|
375
|
+
const maxBytes = maxLineBytes;
|
|
376
|
+
const rowSizes = selected.map((line, index) => ({
|
|
377
|
+
lineNumber: startLine + index,
|
|
378
|
+
bytes: Buffer.byteLength(`${selectedHashes[index]}${HASH_SEP}${line}`, 'utf-8'),
|
|
379
|
+
}));
|
|
380
|
+
if (rowSizes.some((row) => row.bytes > maxBytes)) {
|
|
381
|
+
const oversized = rowSizes.filter((row) => row.bytes > maxBytes);
|
|
382
|
+
const rows = rowSizes.map((row, index) => row.bytes > maxBytes
|
|
383
|
+
? `[Line ${row.lineNumber} is ${formatSize(row.bytes)}, exceeds ${formatSize(maxBytes)}; content not shown. Use bash: sed -n '${row.lineNumber}p' <path> | head -c ${maxBytes}]`
|
|
384
|
+
: fmtRegion([selectedHashes[index]], [selected[index]]));
|
|
385
|
+
const skippedTruncation = truncateHead(rows.join('\n'), {
|
|
386
|
+
maxBytes,
|
|
387
|
+
maxLines: maxTruncLines,
|
|
388
|
+
});
|
|
389
|
+
const shownRowCount = skippedTruncation.content === ''
|
|
390
|
+
? 0
|
|
391
|
+
: skippedTruncation.content.split('\n').length;
|
|
392
|
+
const lastShownLine = shownRowCount > 0 ? startLine + shownRowCount - 1 : startLine - 1;
|
|
393
|
+
const lineLabel = oversized.length === 1
|
|
394
|
+
? `Line ${oversized[0].lineNumber}`
|
|
395
|
+
: `Lines ${oversized.map((row) => row.lineNumber).join(', ')}`;
|
|
396
|
+
const verb = oversized.length === 1 ? 'exceeds' : 'exceed';
|
|
397
|
+
const addresses = oversized.map((row) => `${row.lineNumber}p`).join(';');
|
|
398
|
+
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}]`;
|
|
399
|
+
let preview = skippedTruncation.content;
|
|
400
|
+
let nextOffset;
|
|
401
|
+
if (shownRowCount > 0 &&
|
|
402
|
+
(skippedTruncation.truncated || lastShownLine < totalLines)) {
|
|
403
|
+
nextOffset = lastShownLine + 1;
|
|
404
|
+
preview += `\n\n${warning}\n${formatPaginationHint(startLine, lastShownLine, totalLines, nextOffset, skippedTruncation.truncated ? skippedTruncation.maxBytes : undefined)}`;
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
preview += `\n\n${warning}`;
|
|
408
|
+
}
|
|
409
|
+
const served = [];
|
|
410
|
+
for (let index = 0; index < shownRowCount; index++) {
|
|
411
|
+
if (rowSizes[index].bytes <= maxBytes) {
|
|
412
|
+
served.push({
|
|
413
|
+
position: startLine - 1 + index,
|
|
414
|
+
hash: selectedHashes[index],
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return {
|
|
419
|
+
text: preview,
|
|
420
|
+
truncation: skippedTruncation.truncated ? skippedTruncation : undefined,
|
|
421
|
+
...(nextOffset !== undefined ? { nextOffset } : {}),
|
|
422
|
+
served,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
const truncation = truncateHead(formatted, {
|
|
426
|
+
maxBytes,
|
|
427
|
+
maxLines: maxTruncLines,
|
|
428
|
+
});
|
|
429
|
+
let preview = truncation.content;
|
|
430
|
+
let nextOffset;
|
|
431
|
+
if (truncation.truncated) {
|
|
432
|
+
const endLineDisplay = startLine + truncation.outputLines - 1;
|
|
433
|
+
nextOffset = endLineDisplay + 1;
|
|
434
|
+
if (truncation.truncatedBy === 'lines') {
|
|
435
|
+
preview += `\n\n${formatPaginationHint(startLine, endLineDisplay, totalLines, nextOffset)}`;
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
preview += `\n\n${formatPaginationHint(startLine, endLineDisplay, totalLines, nextOffset, truncation.maxBytes)}`;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
else if (endIdx < totalLines) {
|
|
442
|
+
nextOffset = endIdx + 1;
|
|
443
|
+
preview += `\n\n${formatPaginationHint(startLine, endIdx, totalLines, nextOffset)}`;
|
|
444
|
+
}
|
|
445
|
+
const served = [];
|
|
446
|
+
for (let index = 0; index < truncation.outputLines; index++) {
|
|
447
|
+
served.push({
|
|
448
|
+
position: startLine - 1 + index,
|
|
449
|
+
hash: selectedHashes[index],
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
return {
|
|
453
|
+
text: preview,
|
|
454
|
+
truncation: truncation.truncated ? truncation : undefined,
|
|
455
|
+
...(nextOffset !== undefined ? { nextOffset } : {}),
|
|
456
|
+
served,
|
|
457
|
+
};
|
|
458
|
+
}
|
|
30
459
|
export async function preview(content, hashes, opts = {}, absolutePath) {
|
|
31
460
|
return fmtReadPreview(content, opts, hashes, absolutePath);
|
|
32
461
|
}
|
|
33
|
-
/**
|
|
34
|
-
* IO version: read + normalize + render + truncate + hashes.
|
|
35
|
-
* Single place that decides `served[]` — no emergent partial-serve.
|
|
36
|
-
*/
|
|
37
462
|
export async function readView(io, path, cwd, opts = {}) {
|
|
38
463
|
const { signal } = opts;
|
|
39
464
|
const absolutePath = await io.resolve(path, cwd, signal);
|
|
@@ -59,5 +484,3 @@ export async function readView(io, path, cwd, opts = {}) {
|
|
|
59
484
|
normalized,
|
|
60
485
|
};
|
|
61
486
|
}
|
|
62
|
-
// Re-export constants that belong to this seam for callers that need limits
|
|
63
|
-
export { MAX_HASH_LINES };
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* and README generation. Idempotent and concurrent-safe (wx).
|
|
4
4
|
* @module dsh-better-edit/guidance/materialize
|
|
5
5
|
*/
|
|
6
|
-
import { mkdir, readdir, readFile, writeFile } from "node:fs/promises";
|
|
6
|
+
import { mkdir, readdir, readFile, unlink, writeFile } from "node:fs/promises";
|
|
7
7
|
import { join } from "node:path";
|
|
8
8
|
import { errCode } from "../utils.js";
|
|
9
9
|
import { isBlankOverride } from "./parse.js";
|
|
@@ -30,7 +30,6 @@ Each file is one tool section:
|
|
|
30
30
|
|
|
31
31
|
- \`read.md\` -> \`tool:read\`
|
|
32
32
|
- \`edit.md\` -> \`tool:edit\`
|
|
33
|
-
- \`batch_edit.md\` -> \`tool:batch_edit\`
|
|
34
33
|
- \`undo_last_edit.md\` -> \`tool:undo_last_edit\`
|
|
35
34
|
|
|
36
35
|
## Customize
|
|
@@ -68,7 +67,6 @@ export const GUIDANCE_HOME_README_ZH = `# dsh-better-edit 指引
|
|
|
68
67
|
|
|
69
68
|
- \`read.md\` -> \`tool:read\`
|
|
70
69
|
- \`edit.md\` -> \`tool:edit\`
|
|
71
|
-
- \`batch_edit.md\` -> \`tool:batch_edit\`
|
|
72
70
|
- \`undo_last_edit.md\` -> \`tool:undo_last_edit\`
|
|
73
71
|
|
|
74
72
|
## 自定义
|
|
@@ -122,7 +120,7 @@ async function healBlankOverride(path, section) {
|
|
|
122
120
|
/**
|
|
123
121
|
* Materialize per-preset guidance directories in the plugin home.
|
|
124
122
|
*
|
|
125
|
-
* For each of `DEFAULT_PRESETS` creates `<preset>/{read,edit,
|
|
123
|
+
* For each of `DEFAULT_PRESETS` creates `<preset>/{read,edit,
|
|
126
124
|
* undo_last_edit}.md` rendered from the compiled defaults (with order
|
|
127
125
|
* front-matter), plus a root `README.md` documenting the convention.
|
|
128
126
|
* Idempotent: a user-edited file survives repeated calls. A blank override file
|
|
@@ -156,6 +154,18 @@ export async function ensurePresetGuidance(homeDir) {
|
|
|
156
154
|
});
|
|
157
155
|
}));
|
|
158
156
|
}));
|
|
157
|
+
// Ghost seam cleanup (ADR-0003): remove orphan batch_edit.md override files left
|
|
158
|
+
// from pre-0.3.0 homes. The payload contract merged batch_edit into edit's
|
|
159
|
+
// {path, edits:[[hash,hash,text]]} arity — the file is dead. Best-effort,
|
|
160
|
+
// idempotent, concurrent-safe (ignore ENOENT).
|
|
161
|
+
for (const preset of DEFAULT_PRESETS) {
|
|
162
|
+
const ghost = join(homeDir, preset, "batch_edit.md");
|
|
163
|
+
await unlink(ghost).catch((error) => {
|
|
164
|
+
if (errCode(error) === "ENOENT")
|
|
165
|
+
return;
|
|
166
|
+
throw error;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
159
169
|
// Custom presets present on disk: heal existing blank section files only.
|
|
160
170
|
// Absence is respected — a custom preset's files are never fabricated, and
|
|
161
171
|
// malformed / non-blank / deliberate-blank files are left untouched.
|
|
@@ -171,6 +181,17 @@ export async function ensurePresetGuidance(homeDir) {
|
|
|
171
181
|
await healBlankOverride(join(dir, section.file), section);
|
|
172
182
|
}));
|
|
173
183
|
}));
|
|
184
|
+
// Ghost seam cleanup for custom presets (same ADR-0003 dead file).
|
|
185
|
+
for (const entry of entries) {
|
|
186
|
+
if (!entry.isDirectory() || DEFAULT_PRESETS.includes(entry.name))
|
|
187
|
+
continue;
|
|
188
|
+
const ghost = join(homeDir, entry.name, "batch_edit.md");
|
|
189
|
+
await unlink(ghost).catch((error) => {
|
|
190
|
+
if (errCode(error) === "ENOENT")
|
|
191
|
+
return;
|
|
192
|
+
throw error;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
174
195
|
const homeFiles = new Set(await readdir(homeDir));
|
|
175
196
|
const readmes = [
|
|
176
197
|
["README.md", GUIDANCE_HOME_README],
|
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 {
|
|
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
|
/**
|
|
@@ -21,7 +21,7 @@ function guidanceText(g) {
|
|
|
21
21
|
function bulletLines(lines) {
|
|
22
22
|
return lines.map((line) => `- ${line}`).join("\n");
|
|
23
23
|
}
|
|
24
|
-
/** The
|
|
24
|
+
/** The three sections, in default-order sequence. */
|
|
25
25
|
export const GUIDANCE_SECTIONS = [
|
|
26
26
|
{
|
|
27
27
|
name: "tool:read",
|
|
@@ -35,12 +35,6 @@ export const GUIDANCE_SECTIONS = [
|
|
|
35
35
|
defaultOrder: 131,
|
|
36
36
|
renderDefault: () => guidanceText(EDIT_GUIDANCE),
|
|
37
37
|
},
|
|
38
|
-
{
|
|
39
|
-
name: "tool:batch_edit",
|
|
40
|
-
file: "batch_edit.md",
|
|
41
|
-
defaultOrder: 132,
|
|
42
|
-
renderDefault: () => guidanceText(BATCH_EDIT_GUIDANCE),
|
|
43
|
-
},
|
|
44
38
|
{
|
|
45
39
|
name: "tool:undo_last_edit",
|
|
46
40
|
file: "undo_last_edit.md",
|
|
@@ -103,7 +97,7 @@ export async function resolveSection(name, options) {
|
|
|
103
97
|
return { order: section.defaultOrder, text: section.renderDefault() };
|
|
104
98
|
}
|
|
105
99
|
/**
|
|
106
|
-
* Resolve all
|
|
100
|
+
* Resolve all three sections for a preset. `presetId === undefined` skips the
|
|
107
101
|
* `<preset>/` layer and resolves straight to the compiled defaults.
|
|
108
102
|
*/
|
|
109
103
|
export async function composeSections(presetId, homeDir) {
|
package/lib/hash-store.js
CHANGED
|
@@ -20,7 +20,7 @@ import { DatabaseSync } from "node:sqlite";
|
|
|
20
20
|
import { hashStorePath } from "./paths.js";
|
|
21
21
|
import { workspaceCwd } from "./workspace.js";
|
|
22
22
|
import { errCode, splitLines } from "./utils.js";
|
|
23
|
-
import { initHasher, contentChecksum, HASH_RE } from "./hashline/hash-assign.js";
|
|
23
|
+
import { initHasher, contentChecksum, HASH_RE, CANON_VERSION } from "./hashline/hash-assign.js";
|
|
24
24
|
import { HASH_STORE_VERSION, HASH_STORE_BUSY_TIMEOUT, SERVED_TTL_MS } from "./constants.js";
|
|
25
25
|
export function isValidHashList(value) {
|
|
26
26
|
if (!Array.isArray(value))
|
|
@@ -51,6 +51,9 @@ export function isValidServedList(value) {
|
|
|
51
51
|
}
|
|
52
52
|
return true;
|
|
53
53
|
}
|
|
54
|
+
function cacheKey(checksum) {
|
|
55
|
+
return `${CANON_VERSION}:${checksum}`;
|
|
56
|
+
}
|
|
54
57
|
// ---- db plumbing (private) --------------------------------------------------
|
|
55
58
|
export function isCorruptionError(error) {
|
|
56
59
|
if (error && typeof error === "object") {
|
|
@@ -254,7 +257,7 @@ function makeDomainStore(stmts) {
|
|
|
254
257
|
return {
|
|
255
258
|
engine: "node:sqlite",
|
|
256
259
|
getSnapshot(path, content, deleteCorrupt = true) {
|
|
257
|
-
const checksum = contentChecksum(content);
|
|
260
|
+
const checksum = cacheKey(contentChecksum(content));
|
|
258
261
|
const lineCount = splitLines(content).length;
|
|
259
262
|
const row = stmts.get(path, checksum, lineCount);
|
|
260
263
|
if (!row)
|
|
@@ -274,7 +277,7 @@ function makeDomainStore(stmts) {
|
|
|
274
277
|
}
|
|
275
278
|
},
|
|
276
279
|
upsertSnapshot(path, checksum, lineCount, hashes) {
|
|
277
|
-
stmts.upsert(path, checksum, lineCount, JSON.stringify(hashes), Date.now());
|
|
280
|
+
stmts.upsert(path, cacheKey(checksum), lineCount, JSON.stringify(hashes), Date.now());
|
|
278
281
|
},
|
|
279
282
|
allKnownPaths() {
|
|
280
283
|
return stmts.allPaths();
|
package/lib/hashline/alphabet.js
CHANGED
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
const ALPH_SAFE = ALPH.replace(/-/g, "\\-");
|
|
4
|
-
export const ALPH_RE = new RegExp(`^[${ALPH_SAFE}]+$`);
|
|
5
|
-
export const HASH_CLASS = `[${ALPH_SAFE}]{${HASH_LEN}}`;
|
|
6
|
-
export const HASH_RE = new RegExp(`^${HASH_CLASS}$`);
|
|
1
|
+
// @deprecated — private to HashAssign seam. Use `from "./hash-assign.js"` instead.
|
|
2
|
+
export * from "./hash-assign.js";
|