dsh-better-edit 0.1.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -6
- package/README.md +27 -7
- package/README.zh.md +24 -7
- package/assets/logo.svg +183 -18
- package/lib/contract.js +95 -0
- package/lib/drift.js +1 -1
- package/lib/edit-diff.js +2 -2
- package/lib/edit-engine.js +456 -0
- package/lib/edit-pipeline.js +41 -116
- package/lib/file-reader.js +1 -2
- package/lib/fs-bridge.js +2 -2
- package/lib/fs-write.js +3 -46
- package/lib/hash-store.js +246 -13
- package/lib/hashline/apply.js +2 -2
- package/lib/hashline/hash.js +16 -206
- package/lib/hashline/index.js +4 -3
- package/lib/hashline/parse.js +1 -1
- package/lib/hashline/pure.js +213 -0
- package/lib/hashline/resolve.js +1 -5
- package/lib/hashline/served.js +2 -2
- package/lib/paths.js +55 -1
- package/lib/read-and-serve.js +46 -0
- package/lib/schema.js +1 -1
- package/lib/served-store.js +144 -183
- package/lib/tool-batch-edit.js +28 -280
- package/lib/tool-edit.js +37 -32
- package/lib/tool-read.js +8 -31
- package/lib/tool-undo.js +4 -13
- package/lib/types/contract.d.ts +39 -0
- package/lib/types/edit-engine.d.ts +179 -0
- package/lib/types/edit-pipeline.d.ts +10 -17
- package/lib/types/fs-write.d.ts +0 -1
- package/lib/types/hash-store.d.ts +73 -25
- package/lib/types/hashline/hash.d.ts +8 -13
- package/lib/types/hashline/index.d.ts +4 -3
- package/lib/types/hashline/pure.d.ts +26 -0
- package/lib/types/hashline/resolve.d.ts +0 -1
- package/lib/types/paths.d.ts +9 -0
- package/lib/types/read-and-serve.d.ts +39 -0
- package/lib/types/schema.d.ts +2 -16
- package/lib/types/served-store.d.ts +63 -32
- package/lib/types/tool-batch-edit.d.ts +4 -1
- package/lib/types/tool-undo.d.ts +1 -1
- package/lib/undo-edit.js +16 -1
- package/lib/write-hook.js +30 -50
- package/package.json +4 -3
- package/lib/served-state.js +0 -34
- package/lib/snapshot-store.js +0 -108
- package/lib/types/served-state.d.ts +0 -3
- package/lib/types/snapshot-store.d.ts +0 -14
- package/lib/types/undo-store.d.ts +0 -14
- package/lib/undo-store.js +0 -43
package/lib/undo-edit.js
CHANGED
|
@@ -7,7 +7,22 @@
|
|
|
7
7
|
* overwritten. Undo survives restarts (the store is on disk).
|
|
8
8
|
* @module dsh-better-edit/undo-edit
|
|
9
9
|
*/
|
|
10
|
-
import {
|
|
10
|
+
import { loadHashStore } from './hash-store.js';
|
|
11
|
+
/** Load the last undo row for a path from the active store, if any. */
|
|
12
|
+
async function readUndo(path) {
|
|
13
|
+
const store = await loadHashStore();
|
|
14
|
+
return store.getUndo(path);
|
|
15
|
+
}
|
|
16
|
+
/** Persist the undo row for a path to the active store. */
|
|
17
|
+
async function writeUndo(path, entry) {
|
|
18
|
+
const store = await loadHashStore();
|
|
19
|
+
store.upsertUndo(path, entry);
|
|
20
|
+
}
|
|
21
|
+
/** Drop the undo row for a path from the active store. */
|
|
22
|
+
async function removeUndo(path) {
|
|
23
|
+
const store = await loadHashStore();
|
|
24
|
+
store.deleteUndo(path);
|
|
25
|
+
}
|
|
11
26
|
/**
|
|
12
27
|
* Persist an undo entry for one path before mutating it.
|
|
13
28
|
* @param path - canonical absolute path.
|
package/lib/write-hook.js
CHANGED
|
@@ -5,9 +5,7 @@
|
|
|
5
5
|
* anchors without an explicit read call (mirroring pi-hashline-edit-lsz).
|
|
6
6
|
* @module dsh-better-edit/write-hook
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
9
|
-
import { fmtReadPreview, MAX_HASH_LINES } from './read-render.js';
|
|
10
|
-
import { recordServed, clearDriftReported } from './served-store.js';
|
|
8
|
+
import { readAndServe } from './read-and-serve.js';
|
|
11
9
|
import { execCwd, execSessionKey } from './dsh-context.js';
|
|
12
10
|
import { withWorkspace } from './workspace.js';
|
|
13
11
|
const AUTO_READ_HEADING = '--- Auto-read (hashline anchors) ---';
|
|
@@ -25,53 +23,35 @@ const AUTO_READ_HEADING = '--- Auto-read (hashline anchors) ---';
|
|
|
25
23
|
export function registerWriteHook(rootCtx, agentCtx, io) {
|
|
26
24
|
return agentCtx.on('tools/post-execute', async (exec, result, next) => {
|
|
27
25
|
return withWorkspace(execCwd(exec), async () => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
await clearDriftReported(sessionKey, absolutePath);
|
|
59
|
-
const text = hadUtf8DecodeErrors
|
|
60
|
-
? `${preview.text}\n\n[Non-UTF-8 bytes shown as U+FFFD; editing rewrites the file as UTF-8.]`
|
|
61
|
-
: preview.text;
|
|
62
|
-
return {
|
|
63
|
-
kind: 'accept',
|
|
64
|
-
content: [
|
|
65
|
-
...(decisionContent),
|
|
66
|
-
{ type: 'text', text: `\n\n${AUTO_READ_HEADING}\n${text}` },
|
|
67
|
-
],
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
catch (error) {
|
|
71
|
-
rootCtx.logger.warn(`dsh-better-edit: auto-read after write failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
72
|
-
return decision;
|
|
73
|
-
}
|
|
74
|
-
});
|
|
26
|
+
const decision = await next();
|
|
27
|
+
if (exec.name !== 'write' ||
|
|
28
|
+
result.isError ||
|
|
29
|
+
decision.kind !== 'accept') {
|
|
30
|
+
return decision;
|
|
31
|
+
}
|
|
32
|
+
const decisionContent = decision.content ?? result.content;
|
|
33
|
+
const rawPath = exec.arguments
|
|
34
|
+
?.file_path ?? exec.arguments
|
|
35
|
+
?.path;
|
|
36
|
+
if (typeof rawPath !== 'string')
|
|
37
|
+
return decision;
|
|
38
|
+
try {
|
|
39
|
+
const cwd = execCwd(exec);
|
|
40
|
+
const sessionKey = execSessionKey(exec);
|
|
41
|
+
const signal = exec.signal;
|
|
42
|
+
const { text } = await readAndServe(io, rawPath, cwd, { sessionKey, signal });
|
|
43
|
+
return {
|
|
44
|
+
kind: 'accept',
|
|
45
|
+
content: [
|
|
46
|
+
...(decisionContent),
|
|
47
|
+
{ type: 'text', text: `\n\n${AUTO_READ_HEADING}\n${text}` },
|
|
48
|
+
],
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
rootCtx.logger.warn(`dsh-better-edit: auto-read after write failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
53
|
+
return decision;
|
|
54
|
+
}
|
|
75
55
|
});
|
|
76
56
|
});
|
|
77
57
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-better-edit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Hash-anchored read/edit/batch_edit/undo_last_edit tools for DeepSeek Harness (dsh). Every line gets a unique 3-char hash (A-Za-z0-9) that stays stable across edits; stale or ambiguous anchors are rejected, never fuzzy-matched. Undo persists across restarts.",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -74,9 +74,10 @@
|
|
|
74
74
|
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
|
|
75
75
|
"test": "vitest run",
|
|
76
76
|
"test:watch": "vitest",
|
|
77
|
-
"prepublishOnly": "npm run typecheck && npm test",
|
|
77
|
+
"prepublishOnly": "npm run typecheck && npm test && node scripts/assert-tagged.mjs",
|
|
78
78
|
"benchmark": "node benchmark/run.mjs",
|
|
79
|
-
"postpublish": "node scripts/tag-current.mjs"
|
|
79
|
+
"postpublish": "node scripts/tag-current.mjs",
|
|
80
|
+
"release": "node scripts/release.mjs"
|
|
80
81
|
},
|
|
81
82
|
"devDependencies": {
|
|
82
83
|
"@deepseek-ai/cordis": "^4.0.1",
|
package/lib/served-state.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
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
|
-
}
|
package/lib/snapshot-store.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
export { type ServedEntry, loadServed, recordServed, driftReported, markDriftReported, clearDriftReported, wipeServedState, sessionKeyFor, } from "./served-store.js";
|
|
2
|
-
export declare function servedPositionsOf(served: (string | null)[], hash: string): number[];
|
|
3
|
-
export declare function currentPositionOfDrifted(served: (string | null)[], currentPositions: Map<string, number>, surviving: Set<string>, servedIndex: number, delta: number): number;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { type HashStore } from "./hash-store.js";
|
|
2
|
-
export interface LegacySnapshot {
|
|
3
|
-
content: string;
|
|
4
|
-
hashes: string[];
|
|
5
|
-
}
|
|
6
|
-
export declare function isValidHashList(value: unknown): value is string[];
|
|
7
|
-
export declare function isValidSnapshot(value: unknown): value is LegacySnapshot;
|
|
8
|
-
export declare function getSnapshot(store: HashStore, path: string, content: string, deleteCorrupt?: boolean): string[] | undefined;
|
|
9
|
-
export declare function upsertSnapshot(store: HashStore, path: string, checksum: string, lineCount: number, hashes: string[]): void;
|
|
10
|
-
export declare function findSnapshotPathsByHashes(hashes: string[]): Promise<string[]>;
|
|
11
|
-
export declare function pruneMissingAll(): Promise<void>;
|
|
12
|
-
export declare function upsertSnapshotFor(path: string, checksum: string, lineCount: number, hashes: string[]): Promise<void>;
|
|
13
|
-
export declare function findSnapshotPaths(store: HashStore, hashes: string[]): string[];
|
|
14
|
-
export declare function pruneMissing(store: HashStore): Promise<void>;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { type HashStore } from "./hash-store.js";
|
|
2
|
-
export interface UndoRecord {
|
|
3
|
-
content: string;
|
|
4
|
-
bom: string;
|
|
5
|
-
ending: string;
|
|
6
|
-
hashes: string[];
|
|
7
|
-
resultContent: string;
|
|
8
|
-
}
|
|
9
|
-
export declare function upsertUndo(store: HashStore, path: string, entry: UndoRecord): void;
|
|
10
|
-
export declare function getUndoEntry(store: HashStore, path: string): UndoRecord | undefined;
|
|
11
|
-
export declare function deleteUndo(store: HashStore, path: string): void;
|
|
12
|
-
export declare function readUndo(path: string): Promise<UndoRecord | undefined>;
|
|
13
|
-
export declare function writeUndo(path: string, entry: UndoRecord): Promise<void>;
|
|
14
|
-
export declare function removeUndo(path: string): Promise<void>;
|
package/lib/undo-store.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { loadHashStore } from "./hash-store.js";
|
|
2
|
-
import { isValidHashList } from "./snapshot-store.js";
|
|
3
|
-
export function upsertUndo(store, path, entry) {
|
|
4
|
-
store.stmts.undoUpsert(path, entry.content, entry.bom, entry.ending, JSON.stringify(entry.hashes), entry.resultContent, Date.now());
|
|
5
|
-
}
|
|
6
|
-
export function getUndoEntry(store, path) {
|
|
7
|
-
const row = store.stmts.undoGet(path);
|
|
8
|
-
if (!row)
|
|
9
|
-
return undefined;
|
|
10
|
-
try {
|
|
11
|
-
const parsed = JSON.parse(row.hashes);
|
|
12
|
-
if (!isValidHashList(parsed)) {
|
|
13
|
-
store.stmts.undoDelete(path);
|
|
14
|
-
return undefined;
|
|
15
|
-
}
|
|
16
|
-
return {
|
|
17
|
-
content: row.content,
|
|
18
|
-
bom: row.bom,
|
|
19
|
-
ending: row.ending,
|
|
20
|
-
hashes: parsed,
|
|
21
|
-
resultContent: row.result_content,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
catch {
|
|
25
|
-
store.stmts.undoDelete(path);
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
export function deleteUndo(store, path) {
|
|
30
|
-
store.stmts.undoDelete(path);
|
|
31
|
-
}
|
|
32
|
-
export async function readUndo(path) {
|
|
33
|
-
const store = await loadHashStore();
|
|
34
|
-
return getUndoEntry(store, path);
|
|
35
|
-
}
|
|
36
|
-
export async function writeUndo(path, entry) {
|
|
37
|
-
const store = await loadHashStore();
|
|
38
|
-
upsertUndo(store, path, entry);
|
|
39
|
-
}
|
|
40
|
-
export async function removeUndo(path) {
|
|
41
|
-
const store = await loadHashStore();
|
|
42
|
-
deleteUndo(store, path);
|
|
43
|
-
}
|