dsh-better-edit 0.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/LICENSE +23 -0
- package/README.md +188 -0
- package/cordis.patch.yml +8 -0
- package/lib/constants.js +12 -0
- package/lib/drift.js +98 -0
- package/lib/dsh-context.js +27 -0
- package/lib/edit-diff.js +121 -0
- package/lib/edit-normalize.js +9 -0
- package/lib/edit-pipeline.js +199 -0
- package/lib/edit-response.js +172 -0
- package/lib/file-kind.js +121 -0
- package/lib/file-reader.js +80 -0
- package/lib/fs-bridge.js +106 -0
- package/lib/fs-write.js +151 -0
- package/lib/hash-store.js +380 -0
- package/lib/hashline/alphabet.js +6 -0
- package/lib/hashline/apply.js +223 -0
- package/lib/hashline/hash.js +241 -0
- package/lib/hashline/hasher.js +23 -0
- package/lib/hashline/index.js +4 -0
- package/lib/hashline/parse.js +35 -0
- package/lib/hashline/resolve.js +365 -0
- package/lib/hashline/served.js +120 -0
- package/lib/index.js +91 -0
- package/lib/noop-guard.js +15 -0
- package/lib/paths.js +37 -0
- package/lib/prompts.js +54 -0
- package/lib/read-render.js +142 -0
- package/lib/schema.js +24 -0
- package/lib/served-state.js +34 -0
- package/lib/served-store.js +230 -0
- package/lib/snapshot-store.js +108 -0
- package/lib/tool-batch-edit.js +434 -0
- package/lib/tool-edit.js +152 -0
- package/lib/tool-read.js +86 -0
- package/lib/tool-undo.js +118 -0
- package/lib/truncate.js +98 -0
- package/lib/types/constants.d.ts +11 -0
- package/lib/types/drift.d.ts +29 -0
- package/lib/types/dsh-context.d.ts +23 -0
- package/lib/types/edit-diff.d.ts +14 -0
- package/lib/types/edit-normalize.d.ts +1 -0
- package/lib/types/edit-pipeline.d.ts +54 -0
- package/lib/types/edit-response.d.ts +107 -0
- package/lib/types/file-kind.d.ts +18 -0
- package/lib/types/file-reader.d.ts +47 -0
- package/lib/types/fs-bridge.d.ts +32 -0
- package/lib/types/fs-write.d.ts +2 -0
- package/lib/types/hash-store.d.ts +33 -0
- package/lib/types/hashline/alphabet.d.ts +5 -0
- package/lib/types/hashline/apply.d.ts +22 -0
- package/lib/types/hashline/hash.d.ts +19 -0
- package/lib/types/hashline/hasher.d.ts +8 -0
- package/lib/types/hashline/index.d.ts +4 -0
- package/lib/types/hashline/parse.d.ts +7 -0
- package/lib/types/hashline/resolve.d.ts +59 -0
- package/lib/types/hashline/served.d.ts +43 -0
- package/lib/types/index.d.ts +23 -0
- package/lib/types/noop-guard.d.ts +5 -0
- package/lib/types/paths.d.ts +11 -0
- package/lib/types/prompts.d.ts +17 -0
- package/lib/types/read-render.d.ts +21 -0
- package/lib/types/schema.d.ts +39 -0
- package/lib/types/served-state.d.ts +3 -0
- package/lib/types/served-store.d.ts +53 -0
- package/lib/types/snapshot-store.d.ts +14 -0
- package/lib/types/tool-batch-edit.d.ts +21 -0
- package/lib/types/tool-edit.d.ts +22 -0
- package/lib/types/tool-read.d.ts +21 -0
- package/lib/types/tool-undo.d.ts +20 -0
- package/lib/types/truncate.d.ts +32 -0
- package/lib/types/undo-edit.d.ts +32 -0
- package/lib/types/undo-store.d.ts +14 -0
- package/lib/types/utils.d.ts +13 -0
- package/lib/types/validation.d.ts +7 -0
- package/lib/types/write-hook.d.ts +21 -0
- package/lib/undo-edit.js +83 -0
- package/lib/undo-store.js +43 -0
- package/lib/utils.js +76 -0
- package/lib/validation.js +33 -0
- package/lib/write-hook.js +72 -0
- package/package.json +87 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single-edit pipeline shared by `edit`, `batch_edit`, and previews:
|
|
3
|
+
* resolve → normalize → hash → verify anchors against served state → apply →
|
|
4
|
+
* re-hash with stable anchors → drift notice. All IO goes through the
|
|
5
|
+
* {@link FileIO} bridge so the same pipeline runs on `ctx.fs` and on the host
|
|
6
|
+
* filesystem.
|
|
7
|
+
* @module dsh-better-edit/edit-pipeline
|
|
8
|
+
*/
|
|
9
|
+
import { normFromText, fileSnap } from './file-reader.js';
|
|
10
|
+
import { toCwd } from './paths.js';
|
|
11
|
+
import { applyEdit, lineHashes, resEdit, parseHashRef, MAX_HASH_LINES, } from './hashline/index.js';
|
|
12
|
+
import { AnchorMismatchError, ServedRejectionError, recordEchoServes, } from './hashline/served.js';
|
|
13
|
+
import { loadServed, sessionKeyFor } from './served-store.js';
|
|
14
|
+
import { findSnapshotPathsByHashes } from './snapshot-store.js';
|
|
15
|
+
import { scanDrift } from './drift.js';
|
|
16
|
+
import { abortIf, isRec, rejectUnknownFields, splitLines } from './utils.js';
|
|
17
|
+
const ROOT_KS = new Set(['path', 'remove_from', 'remove_to', 'replacement_text']);
|
|
18
|
+
export function assertReq(request) {
|
|
19
|
+
if (!isRec(request)) {
|
|
20
|
+
throw new Error('[E_BAD_SHAPE] Edit request must be an object.');
|
|
21
|
+
}
|
|
22
|
+
rejectUnknownFields(request, ROOT_KS, 'Edit request');
|
|
23
|
+
if (typeof request.path !== 'string' || request.path.length === 0) {
|
|
24
|
+
throw new Error('[E_BAD_SHAPE] Edit request requires a non-empty "path" string.');
|
|
25
|
+
}
|
|
26
|
+
if (typeof request.remove_from !== 'string' ||
|
|
27
|
+
typeof request.remove_to !== 'string' ||
|
|
28
|
+
typeof request.replacement_text !== 'string') {
|
|
29
|
+
throw new Error('[E_BAD_SHAPE] Edit request requires "remove_from", "remove_to", and "replacement_text" at the top level.');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export async function resolveMissingPath(request) {
|
|
33
|
+
if (typeof request.path === 'string')
|
|
34
|
+
return undefined;
|
|
35
|
+
const from = request.remove_from;
|
|
36
|
+
const to = request.remove_to;
|
|
37
|
+
if (typeof from !== 'string' || typeof to !== 'string')
|
|
38
|
+
return undefined;
|
|
39
|
+
const hashes = [];
|
|
40
|
+
for (const ref of [from, to]) {
|
|
41
|
+
try {
|
|
42
|
+
hashes.push(parseHashRef(ref).hash);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
let matches;
|
|
49
|
+
try {
|
|
50
|
+
matches = await findSnapshotPathsByHashes(hashes);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
if (matches.length === 1) {
|
|
56
|
+
return {
|
|
57
|
+
path: matches[0],
|
|
58
|
+
warning: `[E_BAD_SHAPE] Autocorrected: missing "path" resolved to ${matches[0]} — the only file whose stored hashes contain both anchors.`,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (matches.length > 1) {
|
|
62
|
+
throw new Error(`[E_BAD_SHAPE] Edit request requires a non-empty "path" string; the anchors match multiple known files: ${matches.join(', ')}. Include the intended path.`);
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
export function collectRemovedHashes(edit, originalHashes) {
|
|
67
|
+
const removedHashes = new Set();
|
|
68
|
+
const startHash = edit.hash_bounds[0].hash;
|
|
69
|
+
const endHash = edit.hash_bounds[1].hash;
|
|
70
|
+
const startLine = originalHashes.indexOf(startHash);
|
|
71
|
+
const endLine = originalHashes.indexOf(endHash);
|
|
72
|
+
if (startLine >= 0 && endLine >= 0) {
|
|
73
|
+
const firstLine = Math.min(startLine, endLine);
|
|
74
|
+
const lastLine = Math.max(startLine, endLine);
|
|
75
|
+
for (let i = firstLine; i <= lastLine; i++) {
|
|
76
|
+
removedHashes.add(originalHashes[i]);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return removedHashes;
|
|
80
|
+
}
|
|
81
|
+
export function countLineChanges(edit, originalHashes, isNoop, removedAutoFixes) {
|
|
82
|
+
if (isNoop)
|
|
83
|
+
return { totalAddedLines: 0, totalRemovedLines: 0 };
|
|
84
|
+
let totalRemovedLines = 0;
|
|
85
|
+
const startLine = originalHashes.indexOf(edit.hash_bounds[0].hash);
|
|
86
|
+
const endLine = originalHashes.indexOf(edit.hash_bounds[1].hash);
|
|
87
|
+
if (startLine >= 0 && endLine >= 0) {
|
|
88
|
+
totalRemovedLines = Math.abs(endLine - startLine) + 1;
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
totalAddedLines: Math.max(0, edit.content_lines.length - removedAutoFixes),
|
|
92
|
+
totalRemovedLines,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
export async function execPipeline(io, params, cwd, options) {
|
|
96
|
+
const path = params.path;
|
|
97
|
+
const editWarnings = [];
|
|
98
|
+
const edit = resEdit({
|
|
99
|
+
remove_from: params.remove_from,
|
|
100
|
+
remove_to: params.remove_to,
|
|
101
|
+
replacement_text: params.replacement_text,
|
|
102
|
+
}, editWarnings);
|
|
103
|
+
const hashStore = options?.store;
|
|
104
|
+
const signal = options?.signal;
|
|
105
|
+
abortIf(signal);
|
|
106
|
+
const absolutePath = await io.resolve(path, cwd, signal);
|
|
107
|
+
const rawText = await io.readText(absolutePath, signal);
|
|
108
|
+
const { normalized: originalNormalized, bom, originalEnding, fileHashes: originalHashes, hadUtf8DecodeErrors, } = await normFromText({
|
|
109
|
+
absolutePath,
|
|
110
|
+
rawText,
|
|
111
|
+
displayPath: path,
|
|
112
|
+
signal,
|
|
113
|
+
maxLines: MAX_HASH_LINES,
|
|
114
|
+
store: hashStore,
|
|
115
|
+
noPersist: options?.noPersist,
|
|
116
|
+
});
|
|
117
|
+
const sessionKey = options?.sessionKey ?? sessionKeyFor(undefined);
|
|
118
|
+
const served = await loadServed(sessionKey, absolutePath);
|
|
119
|
+
const policy = options?.noPersist === true ? 'preview' : 'live';
|
|
120
|
+
let anchorResult;
|
|
121
|
+
try {
|
|
122
|
+
anchorResult = applyEdit(originalNormalized, edit, signal, originalHashes, path, served);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
if (error instanceof AnchorMismatchError ||
|
|
126
|
+
error instanceof ServedRejectionError) {
|
|
127
|
+
await recordEchoServes(sessionKey, absolutePath, error.servedRows, policy);
|
|
128
|
+
}
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
const result = anchorResult.content;
|
|
132
|
+
const isNoop = result === originalNormalized;
|
|
133
|
+
const noPersist = options?.noPersist;
|
|
134
|
+
const removedHashes = isNoop
|
|
135
|
+
? undefined
|
|
136
|
+
: collectRemovedHashes(edit, originalHashes);
|
|
137
|
+
const resultHashes = isNoop
|
|
138
|
+
? originalHashes
|
|
139
|
+
: await lineHashes(result, absolutePath, {
|
|
140
|
+
content: originalNormalized,
|
|
141
|
+
hashes: originalHashes,
|
|
142
|
+
removedHashes,
|
|
143
|
+
}, hashStore, noPersist !== true);
|
|
144
|
+
const warnings = [...editWarnings, ...(anchorResult.warnings ?? [])];
|
|
145
|
+
const { totalAddedLines, totalRemovedLines } = countLineChanges(edit, originalHashes, isNoop, anchorResult.autoFixes?.length ?? 0);
|
|
146
|
+
let driftNotice;
|
|
147
|
+
if (options?.noPersist !== true) {
|
|
148
|
+
try {
|
|
149
|
+
driftNotice = await scanDrift({
|
|
150
|
+
sessionKey,
|
|
151
|
+
served,
|
|
152
|
+
resultHashes,
|
|
153
|
+
resultLines: splitLines(result),
|
|
154
|
+
range: anchorResult.range,
|
|
155
|
+
path: absolutePath,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
console.error('Failed to compute drift notice:', error);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
path,
|
|
164
|
+
absolutePath,
|
|
165
|
+
originalNormalized,
|
|
166
|
+
result,
|
|
167
|
+
bom,
|
|
168
|
+
originalEnding,
|
|
169
|
+
hadUtf8DecodeErrors,
|
|
170
|
+
warnings,
|
|
171
|
+
noopEdit: anchorResult.noopEdit,
|
|
172
|
+
firstChangedLine: anchorResult.firstChangedLine,
|
|
173
|
+
lastChangedLine: anchorResult.lastChangedLine,
|
|
174
|
+
resultHashes,
|
|
175
|
+
originalHashes,
|
|
176
|
+
totalAddedLines,
|
|
177
|
+
totalRemovedLines,
|
|
178
|
+
driftNotice,
|
|
179
|
+
range: anchorResult.range,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
/** Resolve the display path a caller names against the session cwd. */
|
|
183
|
+
export function resolveDisplayPath(path, cwd) {
|
|
184
|
+
return toCwd(path, cwd);
|
|
185
|
+
}
|
|
186
|
+
/** Snapshot bookkeeping for noop/success results (best-effort). */
|
|
187
|
+
export async function snapshotIdFor(io, absolutePath, signal) {
|
|
188
|
+
try {
|
|
189
|
+
return await io.statVersion(absolutePath, signal);
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
try {
|
|
193
|
+
return (await fileSnap(absolutePath)).snapshotId;
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { genDiff } from "./edit-diff.js";
|
|
2
|
+
import { visLines, clipLine } from "./utils.js";
|
|
3
|
+
export function buildMetrics(args) {
|
|
4
|
+
const metrics = {
|
|
5
|
+
edits_attempted: args.editsAttempted,
|
|
6
|
+
edits_noop: args.noopEditsCount,
|
|
7
|
+
warnings: args.warningsCount,
|
|
8
|
+
classification: args.classification,
|
|
9
|
+
};
|
|
10
|
+
if (args.classification === "applied" &&
|
|
11
|
+
args.firstChangedLine !== undefined &&
|
|
12
|
+
args.lastChangedLine !== undefined) {
|
|
13
|
+
metrics.changed_lines = {
|
|
14
|
+
first: args.firstChangedLine,
|
|
15
|
+
last: args.lastChangedLine,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
if (args.addedLines !== undefined)
|
|
19
|
+
metrics.added_lines = args.addedLines;
|
|
20
|
+
if (args.removedLines !== undefined)
|
|
21
|
+
metrics.removed_lines = args.removedLines;
|
|
22
|
+
return metrics;
|
|
23
|
+
}
|
|
24
|
+
export function finalizeResult(input) {
|
|
25
|
+
const base = input.diff + warnBlock(input.warnings);
|
|
26
|
+
return base + driftBlock(input.driftNotice);
|
|
27
|
+
}
|
|
28
|
+
export function finalizeToolResult(details) {
|
|
29
|
+
const text = finalizeResult({
|
|
30
|
+
diff: details.diff,
|
|
31
|
+
warnings: details.warnings,
|
|
32
|
+
driftNotice: details.driftNotice,
|
|
33
|
+
});
|
|
34
|
+
return { content: [{ type: "text", text }], servedRows: details.servedRows };
|
|
35
|
+
}
|
|
36
|
+
function warnBlock(warnings) {
|
|
37
|
+
return warnings?.length ? `\n\nWarnings:\n${warnings.join("\n")}` : "";
|
|
38
|
+
}
|
|
39
|
+
function driftBlock(driftNotice) {
|
|
40
|
+
return driftNotice ? `\n\n${driftNotice}` : "";
|
|
41
|
+
}
|
|
42
|
+
export function buildNoop(input) {
|
|
43
|
+
const { path, noopEdit, snapshotId, editMeta, warnings, driftNotice } = input;
|
|
44
|
+
const noopDetailsText = noopEdit
|
|
45
|
+
? `Edit for ${noopEdit.loc} is identical to current content:\n ${noopEdit.loc}: ${clipLine(noopEdit.currentContent)}`
|
|
46
|
+
: "The edit produced identical content.";
|
|
47
|
+
const noticeBlock = driftBlock(driftNotice);
|
|
48
|
+
const text = `No changes made to ${path}\nClassification: noop\n${noopDetailsText}${warnBlock(warnings)}${noticeBlock}`;
|
|
49
|
+
const metrics = buildMetrics({
|
|
50
|
+
classification: "noop",
|
|
51
|
+
editsAttempted: editMeta.editsAttempted,
|
|
52
|
+
noopEditsCount: editMeta.noopEditsCount,
|
|
53
|
+
warningsCount: warnings?.length ?? 0,
|
|
54
|
+
});
|
|
55
|
+
return {
|
|
56
|
+
content: [{ type: "text", text }],
|
|
57
|
+
details: {
|
|
58
|
+
diff: "",
|
|
59
|
+
firstChangedLine: undefined,
|
|
60
|
+
snapshotId,
|
|
61
|
+
classification: "noop",
|
|
62
|
+
metrics,
|
|
63
|
+
...(warnings !== undefined && warnings.length > 0 ? { warnings } : {}),
|
|
64
|
+
...(driftNotice !== undefined ? { driftNotice } : {}),
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export function buildChanged(input) {
|
|
69
|
+
const { path, result, warnings, snapshotId, originalNormalized, originalHashes, editMeta, resultHashes, driftNotice, } = input;
|
|
70
|
+
const resultLines = visLines(result);
|
|
71
|
+
const diffResult = genDiff(originalNormalized, result, 1, resultHashes, originalHashes);
|
|
72
|
+
const addedLines = editMeta.addedLines;
|
|
73
|
+
const removedLines = editMeta.removedLines;
|
|
74
|
+
const warningsBlock = warnBlock(warnings);
|
|
75
|
+
const successPrefix = `Successfully edited in ${path}.`;
|
|
76
|
+
const lineSummary = addedLines > 0 || removedLines > 0
|
|
77
|
+
? ` Added ${addedLines} line(s), removed ${removedLines} line(s).`
|
|
78
|
+
: "";
|
|
79
|
+
const noticeBlock = driftBlock(driftNotice);
|
|
80
|
+
const text = resultLines.length === 0
|
|
81
|
+
? "File is empty. Use edit to insert content." + noticeBlock
|
|
82
|
+
: warningsBlock
|
|
83
|
+
? `${successPrefix}${lineSummary}${warningsBlock}${noticeBlock}`
|
|
84
|
+
: `${successPrefix}${lineSummary}${noticeBlock}`;
|
|
85
|
+
const metrics = buildMetrics({
|
|
86
|
+
classification: "applied",
|
|
87
|
+
editsAttempted: editMeta.editsAttempted,
|
|
88
|
+
noopEditsCount: editMeta.noopEditsCount,
|
|
89
|
+
warningsCount: warnings?.length ?? 0,
|
|
90
|
+
firstChangedLine: editMeta.firstChangedLine,
|
|
91
|
+
lastChangedLine: editMeta.lastChangedLine,
|
|
92
|
+
addedLines,
|
|
93
|
+
removedLines,
|
|
94
|
+
});
|
|
95
|
+
return {
|
|
96
|
+
content: [{ type: "text", text }],
|
|
97
|
+
details: {
|
|
98
|
+
diff: diffResult.diff,
|
|
99
|
+
firstChangedLine: editMeta.firstChangedLine ?? diffResult.firstChangedLine,
|
|
100
|
+
snapshotId,
|
|
101
|
+
metrics,
|
|
102
|
+
...(warnings !== undefined && warnings.length > 0 ? { warnings } : {}),
|
|
103
|
+
servedRows: diffResult.servedRows,
|
|
104
|
+
...(driftNotice !== undefined ? { driftNotice } : {}),
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
export function buildBatchResult(sections) {
|
|
109
|
+
const totalEdits = sections.reduce((n, s) => n + s.appliedCount + s.noopCount, 0);
|
|
110
|
+
const appliedFiles = sections.filter((s) => s.appliedCount > 0);
|
|
111
|
+
const appliedTotal = appliedFiles.reduce((n, s) => n + s.appliedCount, 0);
|
|
112
|
+
const noopTotal = sections.reduce((n, s) => n + s.noopCount, 0);
|
|
113
|
+
const addedLines = sections.reduce((n, s) => n + s.totalAddedLines, 0);
|
|
114
|
+
const removedLines = sections.reduce((n, s) => n + s.totalRemovedLines, 0);
|
|
115
|
+
const allNoop = appliedTotal === 0;
|
|
116
|
+
const warnings = sections.flatMap((s) => s.warnings ?? []);
|
|
117
|
+
const driftNotice = sections
|
|
118
|
+
.map((s) => s.driftNotice)
|
|
119
|
+
.filter((d) => d !== undefined)
|
|
120
|
+
.join("\n\n");
|
|
121
|
+
if (allNoop) {
|
|
122
|
+
const text = `No changes made. All ${totalEdits} edit(s) in the batch produced identical content.\nClassification: noop${warnBlock(warnings)}${driftBlock(driftNotice)}`;
|
|
123
|
+
return {
|
|
124
|
+
content: [{ type: "text", text }],
|
|
125
|
+
details: {
|
|
126
|
+
diff: "",
|
|
127
|
+
classification: "noop",
|
|
128
|
+
metrics: buildMetrics({
|
|
129
|
+
classification: "noop",
|
|
130
|
+
editsAttempted: totalEdits,
|
|
131
|
+
noopEditsCount: noopTotal,
|
|
132
|
+
warningsCount: warnings.length,
|
|
133
|
+
}),
|
|
134
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
135
|
+
...(driftNotice !== undefined ? { driftNotice } : {}),
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
const servedByPath = [];
|
|
140
|
+
const diffParts = [];
|
|
141
|
+
for (const s of appliedFiles) {
|
|
142
|
+
const diffResult = genDiff(s.originalNormalized, s.result, 1, s.resultHashes, s.originalHashes);
|
|
143
|
+
diffParts.push(`--- ${s.path} ---\n${diffResult.diff}`);
|
|
144
|
+
if (diffResult.servedRows.length > 0) {
|
|
145
|
+
servedByPath.push({ path: s.path, servedRows: diffResult.servedRows });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const diff = diffParts.join("\n\n");
|
|
149
|
+
const lineSummary = addedLines > 0 || removedLines > 0
|
|
150
|
+
? ` Added ${addedLines} line(s), removed ${removedLines} line(s).`
|
|
151
|
+
: "";
|
|
152
|
+
const summary = `Successfully edited ${appliedFiles.length} file(s) — ${appliedTotal} of ${totalEdits} edit(s) applied${noopTotal > 0 ? ` (${noopTotal} noop)` : ""}.${lineSummary}`;
|
|
153
|
+
const text = `${summary}${warnBlock(warnings)}${driftBlock(driftNotice)}`;
|
|
154
|
+
return {
|
|
155
|
+
content: [{ type: "text", text }],
|
|
156
|
+
details: {
|
|
157
|
+
diff,
|
|
158
|
+
metrics: buildMetrics({
|
|
159
|
+
classification: "applied",
|
|
160
|
+
editsAttempted: totalEdits,
|
|
161
|
+
noopEditsCount: noopTotal,
|
|
162
|
+
warningsCount: warnings.length,
|
|
163
|
+
addedLines,
|
|
164
|
+
removedLines,
|
|
165
|
+
}),
|
|
166
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
167
|
+
servedRows: servedByPath.flatMap((e) => e.servedRows),
|
|
168
|
+
servedByPath,
|
|
169
|
+
...(driftNotice !== undefined ? { driftNotice } : {}),
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
package/lib/file-kind.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { open as fsOpen, stat as fsStat } from "fs/promises";
|
|
2
|
+
import { fileTypeFromBuffer } from "file-type";
|
|
3
|
+
import { SNIFF_BYTES, MAX_BYTES } from "./constants.js";
|
|
4
|
+
const IMG_TYPES = new Set([
|
|
5
|
+
"image/jpeg",
|
|
6
|
+
"image/png",
|
|
7
|
+
"image/gif",
|
|
8
|
+
"image/webp",
|
|
9
|
+
]);
|
|
10
|
+
const TEXT_TYPES = new Set([
|
|
11
|
+
"application/rtf",
|
|
12
|
+
"application/xml",
|
|
13
|
+
"application/x-ms-regedit",
|
|
14
|
+
]);
|
|
15
|
+
function detectTextBom(sample) {
|
|
16
|
+
if (sample.length >= 4 &&
|
|
17
|
+
sample[0] === 0xff &&
|
|
18
|
+
sample[1] === 0xfe &&
|
|
19
|
+
sample[2] === 0x00 &&
|
|
20
|
+
sample[3] === 0x00)
|
|
21
|
+
return "UTF-32LE";
|
|
22
|
+
if (sample.length >= 4 &&
|
|
23
|
+
sample[0] === 0x00 &&
|
|
24
|
+
sample[1] === 0x00 &&
|
|
25
|
+
sample[2] === 0xfe &&
|
|
26
|
+
sample[3] === 0xff)
|
|
27
|
+
return "UTF-32BE";
|
|
28
|
+
if (sample.length >= 2 && sample[0] === 0xff && sample[1] === 0xfe)
|
|
29
|
+
return "UTF-16LE";
|
|
30
|
+
if (sample.length >= 2 && sample[0] === 0xfe && sample[1] === 0xff)
|
|
31
|
+
return "UTF-16BE";
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
function isTextType(mimeType) {
|
|
35
|
+
return mimeType.startsWith("text/") || TEXT_TYPES.has(mimeType);
|
|
36
|
+
}
|
|
37
|
+
export async function loadFileKindAndText(filePath, options) {
|
|
38
|
+
const pathStat = await fsStat(filePath);
|
|
39
|
+
if (pathStat.isDirectory()) {
|
|
40
|
+
return { kind: "directory" };
|
|
41
|
+
}
|
|
42
|
+
if (!pathStat.isFile()) {
|
|
43
|
+
return {
|
|
44
|
+
kind: "binary",
|
|
45
|
+
description: "unsupported file type",
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (pathStat.size > MAX_BYTES) {
|
|
49
|
+
return {
|
|
50
|
+
kind: "binary",
|
|
51
|
+
description: `file exceeds ${MAX_BYTES} byte limit`
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const fileHandle = await fsOpen(filePath, "r");
|
|
55
|
+
try {
|
|
56
|
+
const buffer = Buffer.alloc(SNIFF_BYTES);
|
|
57
|
+
const { bytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, 0);
|
|
58
|
+
if (bytesRead === 0) {
|
|
59
|
+
return { kind: "text", text: "" };
|
|
60
|
+
}
|
|
61
|
+
const sample = buffer.subarray(0, bytesRead);
|
|
62
|
+
const textBom = detectTextBom(sample);
|
|
63
|
+
if (textBom) {
|
|
64
|
+
return {
|
|
65
|
+
kind: "binary",
|
|
66
|
+
description: `${textBom} encoded text`
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const detectedMimeType = (await fileTypeFromBuffer(sample))?.mime;
|
|
70
|
+
if (detectedMimeType !== undefined &&
|
|
71
|
+
!isTextType(detectedMimeType)) {
|
|
72
|
+
if (IMG_TYPES.has(detectedMimeType)) {
|
|
73
|
+
return { kind: "image", mimeType: detectedMimeType };
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
kind: "binary",
|
|
77
|
+
description: detectedMimeType,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const decoder = new TextDecoder("utf-8", { fatal: false, ignoreBOM: true });
|
|
81
|
+
let hadUtf8DecodeErrors = false;
|
|
82
|
+
let newlineCount = 0;
|
|
83
|
+
const parts = [];
|
|
84
|
+
function decodeChunk(chunk, stream) {
|
|
85
|
+
const decoded = decoder.decode(chunk, { stream });
|
|
86
|
+
if (!hadUtf8DecodeErrors && decoded.includes("\uFFFD")) {
|
|
87
|
+
hadUtf8DecodeErrors = true;
|
|
88
|
+
}
|
|
89
|
+
if (options?.maxLines !== undefined) {
|
|
90
|
+
for (let i = 0; i < decoded.length; i++) {
|
|
91
|
+
if (decoded.charCodeAt(i) === 10)
|
|
92
|
+
newlineCount++;
|
|
93
|
+
}
|
|
94
|
+
if (newlineCount > options.maxLines) {
|
|
95
|
+
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.`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return decoded;
|
|
99
|
+
}
|
|
100
|
+
parts.push(decodeChunk(sample, true));
|
|
101
|
+
let position = bytesRead;
|
|
102
|
+
while (true) {
|
|
103
|
+
const { bytesRead: chunkBytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, position);
|
|
104
|
+
if (chunkBytesRead === 0) {
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
const chunk = buffer.subarray(0, chunkBytesRead);
|
|
108
|
+
parts.push(decodeChunk(chunk, true));
|
|
109
|
+
position += chunkBytesRead;
|
|
110
|
+
}
|
|
111
|
+
parts.push(decodeChunk(new Uint8Array(0), false));
|
|
112
|
+
return {
|
|
113
|
+
kind: "text",
|
|
114
|
+
text: parts.join(""),
|
|
115
|
+
...(hadUtf8DecodeErrors ? { hadUtf8DecodeErrors: true } : {}),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
await fileHandle.close();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { stat } from "fs/promises";
|
|
3
|
+
import { lineHashes } from "./hashline/index.js";
|
|
4
|
+
import { loadFileKindAndText } from "./file-kind.js";
|
|
5
|
+
import { resolveTarget } from "./fs-write.js";
|
|
6
|
+
import { toCwd } from "./paths.js";
|
|
7
|
+
import { detectEnding, toLF, stripBOM } from "./edit-diff.js";
|
|
8
|
+
import { abortIf } from "./utils.js";
|
|
9
|
+
import { valKind, valAccess } from "./validation.js";
|
|
10
|
+
import { visLines } from "./utils.js";
|
|
11
|
+
function fmtSnapId(canonicalPath, info) {
|
|
12
|
+
return `v2|${canonicalPath}|${info.ino}|${info.mtimeMs}|${info.ctimeMs}|${info.size}`;
|
|
13
|
+
}
|
|
14
|
+
export async function fileSnap(absolutePath) {
|
|
15
|
+
const canonicalPath = await resolveTarget(absolutePath);
|
|
16
|
+
const stats = await stat(canonicalPath);
|
|
17
|
+
return {
|
|
18
|
+
snapshotId: fmtSnapId(canonicalPath, stats),
|
|
19
|
+
ino: stats.ino,
|
|
20
|
+
mtimeMs: stats.mtimeMs,
|
|
21
|
+
ctimeMs: stats.ctimeMs,
|
|
22
|
+
size: stats.size,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Hashline normalization shared by every reader: strip the BOM, detect the
|
|
27
|
+
* original line ending, normalize to LF, enforce the line ceiling, and assign
|
|
28
|
+
* (or recall from the hash store) the per-line hash anchors.
|
|
29
|
+
*
|
|
30
|
+
* This is the string-level core of the read/edit pipeline; the caller owns
|
|
31
|
+
* path resolution and raw IO (local filesystem, `ctx.fs`, or a sandbox
|
|
32
|
+
* backend) so the same anchors are produced no matter where the file lives.
|
|
33
|
+
*/
|
|
34
|
+
export async function normFromText(input) {
|
|
35
|
+
const { absolutePath, displayPath, signal } = input;
|
|
36
|
+
abortIf(signal);
|
|
37
|
+
const { bom, text: rawContent } = stripBOM(input.rawText);
|
|
38
|
+
const originalEnding = detectEnding(rawContent);
|
|
39
|
+
const normalized = toLF(rawContent);
|
|
40
|
+
if (input.maxLines !== undefined) {
|
|
41
|
+
const lineCount = visLines(normalized).length;
|
|
42
|
+
if (lineCount > input.maxLines) {
|
|
43
|
+
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.`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const fileHashes = await lineHashes(normalized, absolutePath, undefined, input.store, input.noPersist !== true);
|
|
47
|
+
return {
|
|
48
|
+
absolutePath,
|
|
49
|
+
normalized,
|
|
50
|
+
bom,
|
|
51
|
+
originalEnding,
|
|
52
|
+
fileHashes,
|
|
53
|
+
hadUtf8DecodeErrors: input.hadUtf8DecodeErrors === true,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export async function readNormFile(path, cwd, options) {
|
|
57
|
+
const absolutePath = toCwd(path, cwd);
|
|
58
|
+
const resolvedPath = await resolveTarget(absolutePath);
|
|
59
|
+
const signal = options?.signal;
|
|
60
|
+
const accessMode = options?.accessMode ?? constants.R_OK;
|
|
61
|
+
abortIf(signal);
|
|
62
|
+
await valAccess(resolvedPath, path, accessMode);
|
|
63
|
+
abortIf(signal);
|
|
64
|
+
const file = options?.preloadedFile ??
|
|
65
|
+
(await loadFileKindAndText(resolvedPath, {
|
|
66
|
+
maxLines: options?.maxLines,
|
|
67
|
+
displayPath: path,
|
|
68
|
+
}));
|
|
69
|
+
valKind(file, path);
|
|
70
|
+
return normFromText({
|
|
71
|
+
absolutePath: resolvedPath,
|
|
72
|
+
rawText: file.text,
|
|
73
|
+
displayPath: path,
|
|
74
|
+
signal,
|
|
75
|
+
maxLines: options?.maxLines,
|
|
76
|
+
store: options?.store,
|
|
77
|
+
noPersist: options?.noPersist,
|
|
78
|
+
hadUtf8DecodeErrors: file.hadUtf8DecodeErrors,
|
|
79
|
+
});
|
|
80
|
+
}
|
package/lib/fs-bridge.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IO seam for the hashline tool layer. The tools resolve, read, and write
|
|
3
|
+
* through this bridge so they honor the deployment's `ctx.fs` backend — a
|
|
4
|
+
* sandboxed or remote filesystem — instead of reaching around it. The local
|
|
5
|
+
* implementation exists for tests and for pure-pipeline verification.
|
|
6
|
+
* @module dsh-better-edit/fs-bridge
|
|
7
|
+
*/
|
|
8
|
+
import { readFile } from 'node:fs/promises';
|
|
9
|
+
import { resolveTarget, writeAtomic } from './fs-write.js';
|
|
10
|
+
import { fileSnap } from './file-reader.js';
|
|
11
|
+
import { toCwd } from './paths.js';
|
|
12
|
+
/**
|
|
13
|
+
* Map an `ctx.fs` failure onto the hashline model-facing vocabulary so the
|
|
14
|
+
* model sees the same structured error codes as the pure pipeline.
|
|
15
|
+
* @param error - the thrown FsError or any error.
|
|
16
|
+
* @param displayPath - the path as the model wrote it.
|
|
17
|
+
* @returns the mapped error, rethrown.
|
|
18
|
+
*/
|
|
19
|
+
export function mapFsError(error, displayPath) {
|
|
20
|
+
if (error instanceof Error && typeof error.code === 'string') {
|
|
21
|
+
const code = error.code;
|
|
22
|
+
if (code === 'FS_NOT_FOUND') {
|
|
23
|
+
throw new Error(`[E_NOT_FOUND] File not found: ${displayPath}`);
|
|
24
|
+
}
|
|
25
|
+
if (code === 'FS_PERMISSION_DENIED') {
|
|
26
|
+
throw new Error(`[E_ACCESS] Cannot access file: ${displayPath}`);
|
|
27
|
+
}
|
|
28
|
+
if (code === 'FS_NOT_TEXT' || code === 'FS_NOT_REGULAR_FILE') {
|
|
29
|
+
throw new Error(`[E_NOT_TEXT] Path is not a readable UTF-8 text file: ${displayPath}. Hashline editing only supports text files.`);
|
|
30
|
+
}
|
|
31
|
+
if (code === 'FS_ABORTED') {
|
|
32
|
+
throw new Error('Operation aborted');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
/** FileIO over the deployment's `ctx.fs` service. */
|
|
38
|
+
export function ctxFsIO(fs, _ctx) {
|
|
39
|
+
return {
|
|
40
|
+
async resolve(path, cwd, signal) {
|
|
41
|
+
const target = await fs.resolve(path, {
|
|
42
|
+
...(cwd !== undefined ? { cwd } : {}),
|
|
43
|
+
...(signal !== undefined ? { signal } : {}),
|
|
44
|
+
});
|
|
45
|
+
return fs.processPath(target);
|
|
46
|
+
},
|
|
47
|
+
async readText(absolutePath, signal) {
|
|
48
|
+
try {
|
|
49
|
+
const target = await fs.resolve(absolutePath, {
|
|
50
|
+
...(signal !== undefined ? { signal } : {}),
|
|
51
|
+
});
|
|
52
|
+
return await fs.readText(target, signal);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
return mapFsError(error, absolutePath);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
async writeText(absolutePath, content, signal) {
|
|
59
|
+
try {
|
|
60
|
+
const target = await fs.resolve(absolutePath, {
|
|
61
|
+
...(signal !== undefined ? { signal } : {}),
|
|
62
|
+
});
|
|
63
|
+
await fs.writeText(target, content, undefined, signal);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
return mapFsError(error, absolutePath);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
async statVersion(absolutePath, signal) {
|
|
70
|
+
try {
|
|
71
|
+
const target = await fs.resolve(absolutePath, {
|
|
72
|
+
...(signal !== undefined ? { signal } : {}),
|
|
73
|
+
});
|
|
74
|
+
const info = await fs.stat(target, signal);
|
|
75
|
+
return info?.version ?? undefined;
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** FileIO over the host filesystem directly (tests, previews, fallback). */
|
|
84
|
+
export function localIO() {
|
|
85
|
+
return {
|
|
86
|
+
async resolve(path, cwd) {
|
|
87
|
+
return resolveTarget(toCwd(path, cwd ?? process.cwd()));
|
|
88
|
+
},
|
|
89
|
+
async readText(absolutePath, signal) {
|
|
90
|
+
signal?.throwIfAborted();
|
|
91
|
+
return readFile(absolutePath, 'utf-8');
|
|
92
|
+
},
|
|
93
|
+
async writeText(absolutePath, content, signal) {
|
|
94
|
+
signal?.throwIfAborted();
|
|
95
|
+
await writeAtomic(absolutePath, content);
|
|
96
|
+
},
|
|
97
|
+
async statVersion(absolutePath) {
|
|
98
|
+
try {
|
|
99
|
+
return (await fileSnap(absolutePath)).snapshotId;
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|