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
package/lib/prompts.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-facing prompt text for the hashline tools, embedded so the bundle
|
|
3
|
+
* ships no external prompt files. Each tool's description is short; the
|
|
4
|
+
* `tool:*` system-prompt sections carry the full usage guidance the model
|
|
5
|
+
* reads when the tools are presented.
|
|
6
|
+
* @module dsh-better-edit/prompts
|
|
7
|
+
*/
|
|
8
|
+
export const EDIT_DESCRIPTION = 'Edit a range of lines in a text file, targeted by the 3-char HASH anchors from read output. ' +
|
|
9
|
+
'remove_from and remove_to must each be a BARE 3-character hash: copy only the hash from the ' +
|
|
10
|
+
'leftmost column of a read row (row `ve7│function hello() {` means `"remove_from": "ve7"`). ' +
|
|
11
|
+
'Never pass the line content, a code line, or a paragraph into these fields.';
|
|
12
|
+
export const EDIT_SNIPPET = 'Edit lines in a text file via bare 3-char HASH anchors from read — hash only, never line content; anchor exactly the lines that change; one edit per tool call';
|
|
13
|
+
export const EDIT_GUIDELINES = [
|
|
14
|
+
'`edit`: remove_from and remove_to take ONLY the bare 3-char hash — read row `ve7│function hello() {` means `"remove_from": "ve7"`. Never paste the line content, a code line, a paragraph, or the whole `HASH│content` row into these fields.',
|
|
15
|
+
'`edit`: remove_from and remove_to mark the exact lines that are REMOVED, and replacement_text is their complete replacement applied in order; nothing outside the range changes. Every line inside the range that is not reproduced byte-exact in replacement_text is deleted from the file.',
|
|
16
|
+
'`edit`: keep the range as tight as the change — anchor only the first and last line that actually change, never a whole function, class, or import block when only part of it changes.',
|
|
17
|
+
'`edit`: to edit a single line, set both remove_from and remove_to to the same hash: remove_from: "<HASH>", remove_to: "<HASH>".',
|
|
18
|
+
'`edit`: when copying a line from read output, remove its HASH│ prefix and keep the leading whitespace exactly as shown.',
|
|
19
|
+
'`edit`: every `\\n` in replacement_text separates lines, so a trailing `\\n` adds a final empty line. Mirror the removed lines exactly: a range that ends on a blank line must end replacement_text with `\\n` (e.g. `"code\\n"`), and a replacement whose last line is not blank must not end with `\\n`. To add a blank line after a line, end replacement_text with an explicit empty line after it (e.g. `"X\\n"` adds a blank after X). A replacement that is only blank lines is written as one `\\n` per blank line.',
|
|
20
|
+
'`edit`: when the tool shows the post-edit diff, its rows are the fresh anchors for the new file — `+HASH│` and ` HASH│` rows carry current hashes and unchanged lines keep their previous hashes, so you can anchor follow-up edits on the diff.',
|
|
21
|
+
'`edit`: the tool verifies every line of your range against what it served you. If the file changed since it was served, or a line was never served, the edit is hard-rejected — `[E_RANGE_STALE]` or `[E_RANGE_UNSERVED]` names the first offending line and echoes the current range as fresh `HASH│content` rows. Copy remove_from/remove_to from those echoed rows and retry — the rows count as serves.',
|
|
22
|
+
'`edit`: only rows the tool delivered count as serves — read output, post-edit diffs, and rejection echoes. Content you saw through bash or another channel is not served state; a range over it is rejected as never-served or unverifiable, and there is no way to waive that check.',
|
|
23
|
+
'`edit`: do not issue multiple edit calls on the same file in one message. Use `batch_edit` for multiple edits — it validates every edit before writing anything, applies the whole batch all-or-nothing, and returns one combined diff per file.',
|
|
24
|
+
];
|
|
25
|
+
export const READ_DESCRIPTION = 'Read a text file; each line returned as HASH│content with a 3-char alphanumeric hash. ' +
|
|
26
|
+
'No line numbers — use the HASH as the anchor in edit calls. Binary/directory → rejected; ' +
|
|
27
|
+
'empty → HASH│ (edit to insert); pageable with offset/limit; BOM stripped; non-UTF-8 shown as U+FFFD.';
|
|
28
|
+
export const READ_SNIPPET = 'Read a file; each line returned as HASH│content';
|
|
29
|
+
export const READ_GUIDELINES = [
|
|
30
|
+
'`read`: call only when you need information the tool never served you — a page you never saw, content past the post-edit diff. `edit` verifies every line of your range against what the tool served; a rejection echoes the current range as fresh `HASH│content` rows to retry on.',
|
|
31
|
+
'`read`: the post-edit diff rows from edit/undo and the drift-notice rows also carry fresh anchors for the lines they show.',
|
|
32
|
+
];
|
|
33
|
+
export const BATCH_EDIT_DESCRIPTION = 'Apply several edits in one atomic call. Each item is exactly like the edit tool: ' +
|
|
34
|
+
'{ path?, remove_from, remove_to, replacement_text }, where remove_from and remove_to are ' +
|
|
35
|
+
'bare 3-char hashes from read or diff output. Items targeting the same file are applied in order. ' +
|
|
36
|
+
'Every item is verified against what the tool served you before ANYTHING is written: if any item ' +
|
|
37
|
+
'fails — stale or ambiguous anchor, changed range interior, never-served line — the whole batch ' +
|
|
38
|
+
'is rejected and no file changes. The failing item\u2019s current range is served back as fresh ' +
|
|
39
|
+
'HASH│content rows so you can retry without a read. Use batch_edit whenever you have multiple ' +
|
|
40
|
+
'edits; do not issue several edit calls in one message.';
|
|
41
|
+
export const BATCH_EDIT_GUIDELINES = [
|
|
42
|
+
'batch_edit: each item takes the same fields as edit — { path?, remove_from, remove_to, replacement_text }. remove_from and remove_to take ONLY the bare 3-char hash from read/diff output; never the line content.',
|
|
43
|
+
'batch_edit: items are applied in order. Edits to the same file must not overlap — an item whose range was changed by an earlier item in the batch is rejected.',
|
|
44
|
+
'batch_edit: the whole batch is all-or-nothing. If any item fails validation, nothing is written anywhere; the failing item\u2019s current range is echoed as fresh HASH│content rows that count as serves.',
|
|
45
|
+
'batch_edit: every item is verified against what the tool served you — stale anchors, changed interiors, or never-served lines reject the batch.',
|
|
46
|
+
'batch_edit: a noop item (the range already contains the replacement text) is reported without failing the batch; an all-noop batch reports no changes.',
|
|
47
|
+
'batch_edit: the result is one combined diff per file with fresh anchors — anchor follow-up edits on those rows without re-reading.',
|
|
48
|
+
];
|
|
49
|
+
export const UNDO_DESCRIPTION = 'Undo the last edit on a file, reverting it to its previous state. Use when an edit produced ' +
|
|
50
|
+
'incorrect results (e.g., wrong content, duplicated lines, broken syntax).';
|
|
51
|
+
export const UNDO_GUIDELINES = [
|
|
52
|
+
'`undo_last_edit`: reverts only the most recent edit on the file — any write to the file clears the undo history, so call it immediately after a bad edit. An edit is bad when its post-edit diff shows `-HASH│` rows for lines you meant to keep (a closing brace, import, or declaration).',
|
|
53
|
+
'`undo_last_edit`: when the tool shows the post-edit diff, its `+HASH│` and ` HASH│` rows are the fresh anchors for the restored file, so follow-up edits can anchor on the diff.',
|
|
54
|
+
];
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hash-anchored read rendering: turn normalized text into `HASH│content` rows
|
|
3
|
+
* with pagination hints, oversized-line markers, and served-row bookkeeping.
|
|
4
|
+
* Ported from pi-hashline-edit-lsz's read tool (pi APIs replaced by local
|
|
5
|
+
* helpers).
|
|
6
|
+
* @module dsh-better-edit/read-render
|
|
7
|
+
*/
|
|
8
|
+
import { lineHashes, fmtRegion, HASH_SEP, MAX_HASH_LINES } from './hashline/index.js';
|
|
9
|
+
import { visLines } from './utils.js';
|
|
10
|
+
import { MAX_READ_LINE_BYTES } from './constants.js';
|
|
11
|
+
import { formatSize, truncateHead, DEFAULT_MAX_LINES } from './truncate.js';
|
|
12
|
+
function normPosInt(value, name) {
|
|
13
|
+
if (value === undefined)
|
|
14
|
+
return undefined;
|
|
15
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
16
|
+
throw new Error(`[E_BAD_SHAPE] Read request field "${name}" must be a positive integer.`);
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
export function formatPaginationHint(startLine, endLine, totalLines, nextOffset, byteLimit) {
|
|
21
|
+
const sizeSuffix = byteLimit !== undefined ? ` (${formatSize(byteLimit)} limit)` : '';
|
|
22
|
+
return `[Showing lines ${startLine}-${endLine} of ${totalLines}${sizeSuffix}. Use offset=${nextOffset} to continue.]`;
|
|
23
|
+
}
|
|
24
|
+
export async function fmtReadPreview(text, options, precomputedHashes, path, maxLineBytes = MAX_READ_LINE_BYTES, maxTruncLines = DEFAULT_MAX_LINES) {
|
|
25
|
+
const allLines = visLines(text);
|
|
26
|
+
const totalLines = allLines.length;
|
|
27
|
+
const startLine = normPosInt(options.offset, 'offset') ?? 1;
|
|
28
|
+
if (totalLines === 0) {
|
|
29
|
+
if (startLine === 1) {
|
|
30
|
+
const allHashes = precomputedHashes ??
|
|
31
|
+
(await (path ? lineHashes(text, path) : lineHashes(text)));
|
|
32
|
+
const emptyLineHash = allHashes[0];
|
|
33
|
+
return {
|
|
34
|
+
text: `${emptyLineHash}${HASH_SEP}\n[File is empty. Use edit to insert content.]`,
|
|
35
|
+
served: [{ position: 0, hash: emptyLineHash }],
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
text: `Offset ${startLine} is beyond end of file (0 lines total). The file is empty. Use edit to insert content.`,
|
|
40
|
+
served: [],
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (startLine > totalLines) {
|
|
44
|
+
return {
|
|
45
|
+
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.`,
|
|
46
|
+
served: [],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const limit = normPosInt(options.limit, 'limit');
|
|
50
|
+
const endIdx = limit
|
|
51
|
+
? Math.min(startLine - 1 + limit, totalLines)
|
|
52
|
+
: totalLines;
|
|
53
|
+
const selected = allLines.slice(startLine - 1, endIdx);
|
|
54
|
+
const allHashes = precomputedHashes ??
|
|
55
|
+
(await (path ? lineHashes(text, path) : lineHashes(text)));
|
|
56
|
+
const selectedHashes = allHashes.slice(startLine - 1, endIdx);
|
|
57
|
+
const formatted = fmtRegion(selectedHashes, selected);
|
|
58
|
+
const maxBytes = maxLineBytes;
|
|
59
|
+
const rowSizes = selected.map((line, index) => ({
|
|
60
|
+
lineNumber: startLine + index,
|
|
61
|
+
bytes: Buffer.byteLength(`${selectedHashes[index]}${HASH_SEP}${line}`, 'utf-8'),
|
|
62
|
+
}));
|
|
63
|
+
if (rowSizes.some((row) => row.bytes > maxBytes)) {
|
|
64
|
+
const oversized = rowSizes.filter((row) => row.bytes > maxBytes);
|
|
65
|
+
const rows = rowSizes.map((row, index) => row.bytes > maxBytes
|
|
66
|
+
? `[Line ${row.lineNumber} is ${formatSize(row.bytes)}, exceeds ${formatSize(maxBytes)}; content not shown. Use bash: sed -n '${row.lineNumber}p' <path> | head -c ${maxBytes}]`
|
|
67
|
+
: fmtRegion([selectedHashes[index]], [selected[index]]));
|
|
68
|
+
const skippedTruncation = truncateHead(rows.join('\n'), {
|
|
69
|
+
maxBytes,
|
|
70
|
+
maxLines: maxTruncLines,
|
|
71
|
+
});
|
|
72
|
+
const shownRowCount = skippedTruncation.content === ''
|
|
73
|
+
? 0
|
|
74
|
+
: skippedTruncation.content.split('\n').length;
|
|
75
|
+
const lastShownLine = shownRowCount > 0 ? startLine + shownRowCount - 1 : startLine - 1;
|
|
76
|
+
const lineLabel = oversized.length === 1
|
|
77
|
+
? `Line ${oversized[0].lineNumber}`
|
|
78
|
+
: `Lines ${oversized.map((row) => row.lineNumber).join(', ')}`;
|
|
79
|
+
const verb = oversized.length === 1 ? 'exceeds' : 'exceed';
|
|
80
|
+
const addresses = oversized.map((row) => `${row.lineNumber}p`).join(';');
|
|
81
|
+
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}]`;
|
|
82
|
+
let preview = skippedTruncation.content;
|
|
83
|
+
let nextOffset;
|
|
84
|
+
if (shownRowCount > 0 &&
|
|
85
|
+
(skippedTruncation.truncated || lastShownLine < totalLines)) {
|
|
86
|
+
nextOffset = lastShownLine + 1;
|
|
87
|
+
preview += `\n\n${warning}\n${formatPaginationHint(startLine, lastShownLine, totalLines, nextOffset, skippedTruncation.truncated ? skippedTruncation.maxBytes : undefined)}`;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
preview += `\n\n${warning}`;
|
|
91
|
+
}
|
|
92
|
+
const served = [];
|
|
93
|
+
for (let index = 0; index < shownRowCount; index++) {
|
|
94
|
+
if (rowSizes[index].bytes <= maxBytes) {
|
|
95
|
+
served.push({
|
|
96
|
+
position: startLine - 1 + index,
|
|
97
|
+
hash: selectedHashes[index],
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
text: preview,
|
|
103
|
+
truncation: skippedTruncation.truncated ? skippedTruncation : undefined,
|
|
104
|
+
...(nextOffset !== undefined ? { nextOffset } : {}),
|
|
105
|
+
served,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const truncation = truncateHead(formatted, {
|
|
109
|
+
maxBytes,
|
|
110
|
+
maxLines: maxTruncLines,
|
|
111
|
+
});
|
|
112
|
+
let preview = truncation.content;
|
|
113
|
+
let nextOffset;
|
|
114
|
+
if (truncation.truncated) {
|
|
115
|
+
const endLineDisplay = startLine + truncation.outputLines - 1;
|
|
116
|
+
nextOffset = endLineDisplay + 1;
|
|
117
|
+
if (truncation.truncatedBy === 'lines') {
|
|
118
|
+
preview += `\n\n${formatPaginationHint(startLine, endLineDisplay, totalLines, nextOffset)}`;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
preview += `\n\n${formatPaginationHint(startLine, endLineDisplay, totalLines, nextOffset, truncation.maxBytes)}`;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else if (endIdx < totalLines) {
|
|
125
|
+
nextOffset = endIdx + 1;
|
|
126
|
+
preview += `\n\n${formatPaginationHint(startLine, endIdx, totalLines, nextOffset)}`;
|
|
127
|
+
}
|
|
128
|
+
const served = [];
|
|
129
|
+
for (let index = 0; index < truncation.outputLines; index++) {
|
|
130
|
+
served.push({
|
|
131
|
+
position: startLine - 1 + index,
|
|
132
|
+
hash: selectedHashes[index],
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
text: preview,
|
|
137
|
+
truncation: truncation.truncated ? truncation : undefined,
|
|
138
|
+
...(nextOffset !== undefined ? { nextOffset } : {}),
|
|
139
|
+
served,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
export { MAX_HASH_LINES };
|
package/lib/schema.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared model-facing parameter schemas for the hashline tools, expressed in
|
|
3
|
+
* the dsh schema DSL (not TypeBox). `path` is deliberately NOT `required` at
|
|
4
|
+
* the schema level: the tools accept the built-in `file_path` spelling too
|
|
5
|
+
* (the implicit parameter root stays open), and enforce path presence in
|
|
6
|
+
* `assertReq` after `normalizeFilePath` aliasing.
|
|
7
|
+
* @module dsh-better-edit/schema
|
|
8
|
+
*/
|
|
9
|
+
export const replacementTextSchema = {
|
|
10
|
+
type: 'string',
|
|
11
|
+
description: 'Replacement text as a single string with \\n line separators; every \\n separates lines, so a trailing \\n adds a final empty line. Mirror the removed lines exactly, blank lines included. A replacement that is only blank lines is written as one \\n per blank line. Use "" to delete the range.',
|
|
12
|
+
};
|
|
13
|
+
export const removeFromSchema = {
|
|
14
|
+
type: 'string',
|
|
15
|
+
description: 'Bare 3-char HASH only (e.g. "aB3") — copy just the hash from the leftmost column of a read row like `aB3│content`; never the line content. Marks the FIRST line to remove (inclusive)',
|
|
16
|
+
};
|
|
17
|
+
export const removeToSchema = {
|
|
18
|
+
type: 'string',
|
|
19
|
+
description: 'Bare 3-char HASH only (e.g. "aB3") — copy just the hash from the leftmost column of a read row like `aB3│content`; never the line content. Marks the LAST line to remove (inclusive)',
|
|
20
|
+
};
|
|
21
|
+
export const pathSchema = {
|
|
22
|
+
type: 'string',
|
|
23
|
+
description: 'Path to edit. Required — always provide it explicitly; it is only auto-resolved from the anchors as a fallback when omitted by mistake.',
|
|
24
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export { loadServed, recordServed, driftReported, markDriftReported, clearDriftReported, wipeServedState, sessionKeyFor, } from "./served-store.js";
|
|
2
|
+
export function servedPositionsOf(served, hash) {
|
|
3
|
+
const out = [];
|
|
4
|
+
for (let i = 0; i < served.length; i++) {
|
|
5
|
+
if (served[i] === hash)
|
|
6
|
+
out.push(i);
|
|
7
|
+
}
|
|
8
|
+
return out;
|
|
9
|
+
}
|
|
10
|
+
function nearestSurvivingPosition(served, surviving, from, direction) {
|
|
11
|
+
if (direction === "below") {
|
|
12
|
+
for (let q = from - 1; q >= 0; q--) {
|
|
13
|
+
const hash = served[q];
|
|
14
|
+
if (hash !== null && surviving.has(hash))
|
|
15
|
+
return q;
|
|
16
|
+
}
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
for (let q = from + 1; q < served.length; q++) {
|
|
20
|
+
const hash = served[q];
|
|
21
|
+
if (hash !== null && surviving.has(hash))
|
|
22
|
+
return q;
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
export function currentPositionOfDrifted(served, currentPositions, surviving, servedIndex, delta) {
|
|
27
|
+
const below = nearestSurvivingPosition(served, surviving, servedIndex, "below");
|
|
28
|
+
if (below !== undefined)
|
|
29
|
+
return currentPositions.get(served[below]) + 1;
|
|
30
|
+
const above = nearestSurvivingPosition(served, surviving, servedIndex, "above");
|
|
31
|
+
if (above !== undefined)
|
|
32
|
+
return currentPositions.get(served[above]) - 1;
|
|
33
|
+
return servedIndex + delta;
|
|
34
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { HASH_RE } from "./hashline/alphabet.js";
|
|
3
|
+
import { loadHashStore, withStore } from "./hash-store.js";
|
|
4
|
+
let fallbackSessionKey;
|
|
5
|
+
/**
|
|
6
|
+
* Map a caller to the served-state session key. dsh passes the live session id
|
|
7
|
+
* (`exec.agent.session.id`); callers without one (tests, previews) share a
|
|
8
|
+
* process-local fallback so hashline verification never fabricates a session.
|
|
9
|
+
*/
|
|
10
|
+
export function sessionKeyFor(sessionId) {
|
|
11
|
+
if (sessionId && sessionId.length > 0)
|
|
12
|
+
return sessionId;
|
|
13
|
+
fallbackSessionKey ??= randomUUID();
|
|
14
|
+
return fallbackSessionKey;
|
|
15
|
+
}
|
|
16
|
+
function isValidServedList(value) {
|
|
17
|
+
if (!Array.isArray(value))
|
|
18
|
+
return false;
|
|
19
|
+
for (const entry of value) {
|
|
20
|
+
if (entry === null)
|
|
21
|
+
continue;
|
|
22
|
+
if (typeof entry !== "string" || !HASH_RE.test(entry))
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
export function getServed(store, sessionKey, path) {
|
|
28
|
+
const row = store.stmts.servedGet(sessionKey, path);
|
|
29
|
+
if (!row)
|
|
30
|
+
return [];
|
|
31
|
+
try {
|
|
32
|
+
const parsed = JSON.parse(row.hashes);
|
|
33
|
+
if (isValidServedList(parsed))
|
|
34
|
+
return parsed;
|
|
35
|
+
store.stmts.servedDelete(sessionKey, path);
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
store.stmts.servedDelete(sessionKey, path);
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export function upsertServed(store, sessionKey, path, entries) {
|
|
44
|
+
if (entries.length === 0)
|
|
45
|
+
return;
|
|
46
|
+
withStore(() => {
|
|
47
|
+
const updated = getServed(store, sessionKey, path).slice();
|
|
48
|
+
for (const entry of entries) {
|
|
49
|
+
if (!Number.isInteger(entry.position) || entry.position < 0) {
|
|
50
|
+
throw new TypeError(`Invalid served position: ${entry.position}`);
|
|
51
|
+
}
|
|
52
|
+
if (entry.hash !== null &&
|
|
53
|
+
(typeof entry.hash !== "string" || !HASH_RE.test(entry.hash))) {
|
|
54
|
+
throw new TypeError(`Invalid served hash: ${String(entry.hash)}`);
|
|
55
|
+
}
|
|
56
|
+
while (updated.length <= entry.position)
|
|
57
|
+
updated.push(null);
|
|
58
|
+
updated[entry.position] = entry.hash;
|
|
59
|
+
}
|
|
60
|
+
while (updated.length > 0 && updated[updated.length - 1] === null)
|
|
61
|
+
updated.pop();
|
|
62
|
+
store.stmts.servedUpsert(sessionKey, path, JSON.stringify(updated), Date.now());
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
export function recordServes(store, sessionKey, path, rows) {
|
|
66
|
+
if (rows.length === 0)
|
|
67
|
+
return;
|
|
68
|
+
try {
|
|
69
|
+
upsertServed(store, sessionKey, path, rows);
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
console.error("Failed to record served rows:", error);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Record post-mutation diff serves against the file's CURRENT length. Before
|
|
77
|
+
* upserting the diff rows, positions beyond `lineCount` are dropped from the
|
|
78
|
+
* stored array: after an edit/undo the file has a new shape, and a hash that
|
|
79
|
+
* survived into the diff must not keep its pre-edit position claim — that
|
|
80
|
+
* stale claim is what makes a chained edit's boundary anchor look ambiguous
|
|
81
|
+
* (`E_RANGE_UNVERIFIED`) instead of verifying cleanly.
|
|
82
|
+
* @param store - open hash store.
|
|
83
|
+
* @param sessionKey - the session whose served rows are being updated.
|
|
84
|
+
* @param path - canonical absolute path.
|
|
85
|
+
* @param rows - diff rows (position → post-mutation hash) to record.
|
|
86
|
+
* @param lineCount - the file's line count AFTER the mutation.
|
|
87
|
+
*/
|
|
88
|
+
export function recordServesTruncated(store, sessionKey, path, rows, lineCount, clearFrom = 0) {
|
|
89
|
+
if (rows.length === 0)
|
|
90
|
+
return;
|
|
91
|
+
try {
|
|
92
|
+
withStore(() => {
|
|
93
|
+
const updated = getServed(store, sessionKey, path).slice();
|
|
94
|
+
if (updated.length > lineCount)
|
|
95
|
+
updated.length = lineCount;
|
|
96
|
+
// Positions at/after the mutation's first changed line are
|
|
97
|
+
// re-shaped by the edit: the model's previous view of them no
|
|
98
|
+
// longer holds (line positions shift, and only the diff window
|
|
99
|
+
// below is what it now saw). Clear them so a stale hash never
|
|
100
|
+
// makes a boundary anchor look ambiguous; the unchanged prefix
|
|
101
|
+
// before the edit keeps its claims (same content, same position).
|
|
102
|
+
for (let i = clearFrom; i < updated.length; i++)
|
|
103
|
+
updated[i] = null;
|
|
104
|
+
for (const entry of rows) {
|
|
105
|
+
if (!Number.isInteger(entry.position) || entry.position < 0) {
|
|
106
|
+
throw new TypeError(`Invalid served position: ${entry.position}`);
|
|
107
|
+
}
|
|
108
|
+
if (entry.hash !== null &&
|
|
109
|
+
(typeof entry.hash !== 'string' || !HASH_RE.test(entry.hash))) {
|
|
110
|
+
throw new TypeError(`Invalid served hash: ${String(entry.hash)}`);
|
|
111
|
+
}
|
|
112
|
+
while (updated.length <= entry.position)
|
|
113
|
+
updated.push(null);
|
|
114
|
+
updated[entry.position] = entry.hash;
|
|
115
|
+
}
|
|
116
|
+
while (updated.length > 0 && updated[updated.length - 1] === null)
|
|
117
|
+
updated.pop();
|
|
118
|
+
store.stmts.servedUpsert(sessionKey, path, JSON.stringify(updated), Date.now());
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
console.error('Failed to record truncated served rows:', error);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
export function getReported(store, sessionKey, path) {
|
|
126
|
+
const row = store.stmts.servedGet(sessionKey, path);
|
|
127
|
+
if (!row)
|
|
128
|
+
return new Set();
|
|
129
|
+
const raw = row.reported;
|
|
130
|
+
if (typeof raw !== "string" || raw.length === 0)
|
|
131
|
+
return new Set();
|
|
132
|
+
try {
|
|
133
|
+
const parsed = JSON.parse(raw);
|
|
134
|
+
if (!Array.isArray(parsed))
|
|
135
|
+
return new Set();
|
|
136
|
+
return new Set(parsed.filter((h) => typeof h === "string" && HASH_RE.test(h)));
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return new Set();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export function addReported(store, sessionKey, path, hashes) {
|
|
143
|
+
const valid = hashes.filter((hash) => HASH_RE.test(hash));
|
|
144
|
+
if (valid.length === 0)
|
|
145
|
+
return;
|
|
146
|
+
withStore(() => {
|
|
147
|
+
const current = getReported(store, sessionKey, path);
|
|
148
|
+
for (const hash of valid)
|
|
149
|
+
current.add(hash);
|
|
150
|
+
store.stmts.servedReportedUpsert(sessionKey, path, JSON.stringify([...current]), Date.now());
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
export function clearReported(store, sessionKey, path) {
|
|
154
|
+
withStore(() => {
|
|
155
|
+
store.stmts.servedReportedClear(sessionKey, Date.now(), path);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
export function deleteServed(store, sessionKey, path) {
|
|
159
|
+
store.stmts.servedDelete(sessionKey, path);
|
|
160
|
+
}
|
|
161
|
+
export function wipeServed(store, sessionKey) {
|
|
162
|
+
store.stmts.servedWipe(sessionKey);
|
|
163
|
+
}
|
|
164
|
+
export async function loadServed(sessionKey, path) {
|
|
165
|
+
const store = await loadHashStore();
|
|
166
|
+
return getServed(store, sessionKey, path);
|
|
167
|
+
}
|
|
168
|
+
export async function recordServed(sessionKey, path, rows) {
|
|
169
|
+
if (rows.length === 0)
|
|
170
|
+
return;
|
|
171
|
+
try {
|
|
172
|
+
const store = await loadHashStore();
|
|
173
|
+
recordServes(store, sessionKey, path, rows);
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
console.error('Failed to record served rows:', error);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Async sibling of {@link recordServesTruncated}: record post-mutation diff
|
|
181
|
+
* rows against the file's current line count.
|
|
182
|
+
*/
|
|
183
|
+
export async function recordServedTruncated(sessionKey, path, rows, lineCount, clearFrom = 0) {
|
|
184
|
+
if (rows.length === 0)
|
|
185
|
+
return;
|
|
186
|
+
try {
|
|
187
|
+
const store = await loadHashStore();
|
|
188
|
+
recordServesTruncated(store, sessionKey, path, rows, lineCount, clearFrom);
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
console.error('Failed to record truncated served rows:', error);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
export async function driftReported(sessionKey, path) {
|
|
195
|
+
try {
|
|
196
|
+
const store = await loadHashStore();
|
|
197
|
+
return getReported(store, sessionKey, path);
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
console.error("Failed to load reported drift set:", error);
|
|
201
|
+
return new Set();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
export async function markDriftReported(sessionKey, path, hashes) {
|
|
205
|
+
try {
|
|
206
|
+
const store = await loadHashStore();
|
|
207
|
+
addReported(store, sessionKey, path, hashes);
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
console.error("Failed to record reported drift set:", error);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
export async function clearDriftReported(sessionKey, path) {
|
|
214
|
+
try {
|
|
215
|
+
const store = await loadHashStore();
|
|
216
|
+
clearReported(store, sessionKey, path);
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
console.error("Failed to clear reported drift set:", error);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
export async function wipeServedState(sessionKey) {
|
|
223
|
+
try {
|
|
224
|
+
const store = await loadHashStore();
|
|
225
|
+
wipeServed(store, sessionKey);
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
console.error("Failed to wipe served state:", error);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { stat } from "fs/promises";
|
|
2
|
+
import { HASH_RE } from "./hashline/alphabet.js";
|
|
3
|
+
import { contentChecksum } from "./hashline/hasher.js";
|
|
4
|
+
import { splitLines } from "./utils.js";
|
|
5
|
+
import { loadHashStore, withStore } from "./hash-store.js";
|
|
6
|
+
export function isValidHashList(value) {
|
|
7
|
+
if (!Array.isArray(value))
|
|
8
|
+
return false;
|
|
9
|
+
for (const hash of value) {
|
|
10
|
+
if (typeof hash !== "string" || !HASH_RE.test(hash))
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
export function isValidSnapshot(value) {
|
|
16
|
+
if (typeof value !== "object" || value === null)
|
|
17
|
+
return false;
|
|
18
|
+
const v = value;
|
|
19
|
+
if (typeof v.content !== "string")
|
|
20
|
+
return false;
|
|
21
|
+
return isValidHashList(v.hashes);
|
|
22
|
+
}
|
|
23
|
+
export function getSnapshot(store, path, content, deleteCorrupt = true) {
|
|
24
|
+
const checksum = contentChecksum(content);
|
|
25
|
+
const lineCount = splitLines(content).length;
|
|
26
|
+
const row = store.stmts.get(path, checksum, lineCount);
|
|
27
|
+
if (!row)
|
|
28
|
+
return undefined;
|
|
29
|
+
try {
|
|
30
|
+
const parsed = JSON.parse(row.hashes);
|
|
31
|
+
if (isValidHashList(parsed))
|
|
32
|
+
return parsed;
|
|
33
|
+
if (deleteCorrupt)
|
|
34
|
+
store.stmts.deleteOne(path);
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
if (deleteCorrupt)
|
|
39
|
+
store.stmts.deleteOne(path);
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export function upsertSnapshot(store, path, checksum, lineCount, hashes) {
|
|
44
|
+
store.stmts.upsert(path, checksum, lineCount, JSON.stringify(hashes), Date.now());
|
|
45
|
+
}
|
|
46
|
+
export async function findSnapshotPathsByHashes(hashes) {
|
|
47
|
+
const store = await loadHashStore();
|
|
48
|
+
return findSnapshotPaths(store, hashes);
|
|
49
|
+
}
|
|
50
|
+
export async function pruneMissingAll() {
|
|
51
|
+
const store = await loadHashStore();
|
|
52
|
+
await pruneMissing(store);
|
|
53
|
+
}
|
|
54
|
+
export async function upsertSnapshotFor(path, checksum, lineCount, hashes) {
|
|
55
|
+
const store = await loadHashStore();
|
|
56
|
+
upsertSnapshot(store, path, checksum, lineCount, hashes);
|
|
57
|
+
}
|
|
58
|
+
export function findSnapshotPaths(store, hashes) {
|
|
59
|
+
const rows = store.stmts.allHashes();
|
|
60
|
+
const matches = [];
|
|
61
|
+
for (const row of rows) {
|
|
62
|
+
try {
|
|
63
|
+
const parsed = JSON.parse(row.hashes);
|
|
64
|
+
if (!isValidHashList(parsed))
|
|
65
|
+
continue;
|
|
66
|
+
if (hashes.every((h) => parsed.includes(h)))
|
|
67
|
+
matches.push(row.path);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return matches;
|
|
74
|
+
}
|
|
75
|
+
const STAT_BATCH = 64;
|
|
76
|
+
async function statMissing(rows) {
|
|
77
|
+
const missing = [];
|
|
78
|
+
for (let i = 0; i < rows.length; i += STAT_BATCH) {
|
|
79
|
+
const batch = rows.slice(i, i + STAT_BATCH);
|
|
80
|
+
const results = await Promise.all(batch.map(async (row) => {
|
|
81
|
+
try {
|
|
82
|
+
await stat(row.path);
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return row.path;
|
|
87
|
+
}
|
|
88
|
+
}));
|
|
89
|
+
for (const path of results) {
|
|
90
|
+
if (path !== undefined)
|
|
91
|
+
missing.push(path);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return missing;
|
|
95
|
+
}
|
|
96
|
+
export async function pruneMissing(store) {
|
|
97
|
+
const rows = store.stmts.allPaths();
|
|
98
|
+
const missing = await statMissing(rows);
|
|
99
|
+
if (missing.length === 0)
|
|
100
|
+
return;
|
|
101
|
+
withStore(() => {
|
|
102
|
+
for (const path of missing) {
|
|
103
|
+
store.stmts.deleteOne(path);
|
|
104
|
+
store.stmts.undoDelete(path);
|
|
105
|
+
store.stmts.servedDeletePath(path);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|