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
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The edit-sequence engine shared by `edit`, `batch_edit`, and previews:
|
|
3
|
+
* apply-one-edit against in-memory content with served verification, the
|
|
4
|
+
* multi-edit sequencer that drives a whole file's item list against evolving
|
|
5
|
+
* content, the noop-loop guard, and the persist-undo → write → restore
|
|
6
|
+
* transaction both mutating tools run.
|
|
7
|
+
*
|
|
8
|
+
* The model-facing contract lives here unchanged: [E_BATCH_ABORT],
|
|
9
|
+
* [E_NOOP_LOOP], [E_UNDO_UNAVAILABLE] carry byte-identical messages, and
|
|
10
|
+
* reject-and-serve records the same echo serves.
|
|
11
|
+
* @module dsh-better-edit/edit-engine
|
|
12
|
+
*/
|
|
13
|
+
import type { ToolExecution } from "@deepseek-ai/dsh-tools";
|
|
14
|
+
import type { SandboxExecutionPolicy } from "@deepseek-ai/dsh-sandbox";
|
|
15
|
+
import type { FileIO } from "./fs-bridge.js";
|
|
16
|
+
import type { HashStore } from "./hash-store.js";
|
|
17
|
+
import type { LineEnding } from "./edit-diff.js";
|
|
18
|
+
import { type HEdit, type NEdit } from "./hashline/index.js";
|
|
19
|
+
import { type ResolvedRange, type ServeRecordPolicy, type ServedRow } from "./hashline/served.js";
|
|
20
|
+
import type { FsSandboxController } from "./sandbox.js";
|
|
21
|
+
export interface PreparedItem {
|
|
22
|
+
index: number;
|
|
23
|
+
path: string;
|
|
24
|
+
absolutePath: string;
|
|
25
|
+
remove_from: string;
|
|
26
|
+
remove_to: string;
|
|
27
|
+
replacement_text: string;
|
|
28
|
+
pathWarning?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface FileEditResult {
|
|
31
|
+
displayPath: string;
|
|
32
|
+
absolutePath: string;
|
|
33
|
+
originalNormalized: string;
|
|
34
|
+
result: string;
|
|
35
|
+
bom: string;
|
|
36
|
+
originalEnding: LineEnding;
|
|
37
|
+
hadUtf8DecodeErrors: boolean;
|
|
38
|
+
warnings: string[];
|
|
39
|
+
originalHashes: string[];
|
|
40
|
+
resultHashes: string[];
|
|
41
|
+
appliedCount: number;
|
|
42
|
+
noopCount: number;
|
|
43
|
+
totalAddedLines: number;
|
|
44
|
+
totalRemovedLines: number;
|
|
45
|
+
driftNotice: string | undefined;
|
|
46
|
+
range: ResolvedRange;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Resolve a request's missing `path` from its anchors: the only file whose
|
|
50
|
+
* stored hashes contain both anchors. Returns the path plus an autocorrect
|
|
51
|
+
* warning, or undefined when no resolution is possible.
|
|
52
|
+
*/
|
|
53
|
+
export declare function resolveMissingPath(request: Record<string, unknown>): Promise<{
|
|
54
|
+
path: string;
|
|
55
|
+
warning: string;
|
|
56
|
+
} | undefined>;
|
|
57
|
+
/** The hashes a range edit removes, for stable re-hash bookkeeping. */
|
|
58
|
+
export declare function collectRemovedHashes(edit: HEdit, originalHashes: string[]): Set<string>;
|
|
59
|
+
/** Added/removed line counts for one resolved edit against a file's original hashes. */
|
|
60
|
+
export declare function countLineChanges(edit: HEdit, originalHashes: string[], isNoop: boolean, removedAutoFixes: number): {
|
|
61
|
+
totalAddedLines: number;
|
|
62
|
+
totalRemovedLines: number;
|
|
63
|
+
};
|
|
64
|
+
export interface ApplyOneInput {
|
|
65
|
+
content: string;
|
|
66
|
+
hashes: string[];
|
|
67
|
+
served: (string | null)[];
|
|
68
|
+
removeFrom: string;
|
|
69
|
+
removeTo: string;
|
|
70
|
+
replacementText: string;
|
|
71
|
+
absolutePath: string;
|
|
72
|
+
displayPath: string;
|
|
73
|
+
signal?: AbortSignal;
|
|
74
|
+
/** Shared warnings array; resEdit warnings are pushed here. */
|
|
75
|
+
warnings: string[];
|
|
76
|
+
/**
|
|
77
|
+
* The hashes to count added/removed lines against. Defaults to `hashes`;
|
|
78
|
+
* the batch sequencer passes the file's ORIGINAL hashes so later edits in
|
|
79
|
+
* a sequence still count against the file as first served.
|
|
80
|
+
*/
|
|
81
|
+
countHashes?: string[];
|
|
82
|
+
store?: HashStore;
|
|
83
|
+
persist: boolean;
|
|
84
|
+
/** Pre-resolved edit (single path keeps resEdit before IO for error order). */
|
|
85
|
+
edit?: HEdit;
|
|
86
|
+
}
|
|
87
|
+
export interface ApplyOneResult {
|
|
88
|
+
result: string;
|
|
89
|
+
/** Stable re-hash after the edit (equals `hashes` for a noop). */
|
|
90
|
+
hashes: string[];
|
|
91
|
+
range: ResolvedRange;
|
|
92
|
+
noop: boolean;
|
|
93
|
+
edit: HEdit;
|
|
94
|
+
noopEdit?: NEdit;
|
|
95
|
+
firstChangedLine?: number;
|
|
96
|
+
lastChangedLine?: number;
|
|
97
|
+
removedHashes: Set<string> | undefined;
|
|
98
|
+
totalAddedLines: number;
|
|
99
|
+
totalRemovedLines: number;
|
|
100
|
+
anchorWarnings: string[] | undefined;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* One edit against in-memory content: resolve (unless a pre-resolved edit was
|
|
104
|
+
* given) → apply with served verification → stable re-hash → line counts.
|
|
105
|
+
*
|
|
106
|
+
* `onReject` owns the reject-and-serve policy: it receives resolve/verify
|
|
107
|
+
* failures (and the edit that failed, when resolved) and MUST throw. The
|
|
108
|
+
* single path rethrows the original anchor error after recording echo serves;
|
|
109
|
+
* the batch path wraps with [E_BATCH_ABORT] plus the current-range echo.
|
|
110
|
+
*/
|
|
111
|
+
export declare function applyOne(input: ApplyOneInput, onReject: (error: unknown, edit: HEdit | undefined) => Promise<never>): Promise<ApplyOneResult>;
|
|
112
|
+
export interface NoopLoopOptions {
|
|
113
|
+
absolutePath: string;
|
|
114
|
+
removeFrom: string;
|
|
115
|
+
removeTo: string;
|
|
116
|
+
replacementText: string;
|
|
117
|
+
displayPath: string;
|
|
118
|
+
/** Batch item index; undefined = single-edit flavor. */
|
|
119
|
+
index?: number;
|
|
120
|
+
count: number;
|
|
121
|
+
sessionKey: string;
|
|
122
|
+
originalHashes: string[];
|
|
123
|
+
originalNormalized: string;
|
|
124
|
+
/** Single-edit flavor only: the edit's range, for the echo rows. */
|
|
125
|
+
range?: ResolvedRange;
|
|
126
|
+
/** Batch flavor: precomputed echo rows for the failed item (may be absent). */
|
|
127
|
+
echoRows?: ServedRow[];
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The shared noop-loop guard. Returns the "twice in a row" notice for the
|
|
131
|
+
* caller to append to warnings, or throws [E_NOOP_LOOP] (after recording the
|
|
132
|
+
* echo serves) once the payload has been submitted NOOP_LOOP_THRESHOLD times
|
|
133
|
+
* with no change. Messages are byte-identical to the pre-engine tools.
|
|
134
|
+
*/
|
|
135
|
+
export declare function enforceNoopLoop(opts: NoopLoopOptions): Promise<string | undefined>;
|
|
136
|
+
/**
|
|
137
|
+
* Run a file's item list against freshly-read content with served
|
|
138
|
+
* verification, evolving content/hashes, union range, noop tracking, and a
|
|
139
|
+
* per-file drift notice. All-or-nothing is enforced by the caller's
|
|
140
|
+
* transaction ({@link persistUndoAndWrite}): nothing here writes to disk.
|
|
141
|
+
*/
|
|
142
|
+
export declare function runFileEdits(io: FileIO, items: PreparedItem[], opts: {
|
|
143
|
+
signal?: AbortSignal;
|
|
144
|
+
sessionKey: string;
|
|
145
|
+
}): Promise<FileEditResult>;
|
|
146
|
+
export interface UndoWriteFile {
|
|
147
|
+
absolutePath: string;
|
|
148
|
+
displayPath: string;
|
|
149
|
+
originalNormalized: string;
|
|
150
|
+
bom: string;
|
|
151
|
+
originalEnding: LineEnding;
|
|
152
|
+
originalHashes: string[];
|
|
153
|
+
result: string;
|
|
154
|
+
}
|
|
155
|
+
export interface PersistWriteOptions {
|
|
156
|
+
io: FileIO;
|
|
157
|
+
files: UndoWriteFile[];
|
|
158
|
+
exec: ToolExecution;
|
|
159
|
+
sandbox: FsSandboxController;
|
|
160
|
+
sandboxPolicy: SandboxExecutionPolicy | undefined;
|
|
161
|
+
signal?: AbortSignal;
|
|
162
|
+
/** [E_UNDO_UNAVAILABLE] message builder, per tool flavor. */
|
|
163
|
+
undoUnavailableMessage: (displayPath: string) => string;
|
|
164
|
+
/**
|
|
165
|
+
* On write failure, also restore undo entries of files that were saved but
|
|
166
|
+
* never written. The single-edit tool restores its one entry; the batch
|
|
167
|
+
* tool keeps current behavior and restores only written files.
|
|
168
|
+
*/
|
|
169
|
+
restoreUnwrittenUndos?: boolean;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The persist-undo → write-all → restore-on-failure transaction shared by
|
|
173
|
+
* `edit` (one file) and `batch_edit` (many files). Every file's undo entry is
|
|
174
|
+
* persisted before anything is written; if a write fails, already-written
|
|
175
|
+
* files are restored (original content written back, undo entry restored) and
|
|
176
|
+
* the sandbox-mapped error rethrown.
|
|
177
|
+
*/
|
|
178
|
+
export declare function persistUndoAndWrite(opts: PersistWriteOptions): Promise<void>;
|
|
179
|
+
export type { ServeRecordPolicy };
|
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The single-edit pipeline shared by `edit`,
|
|
3
|
-
* resolve → normalize → hash → verify anchors against
|
|
4
|
-
* re-hash with stable anchors → drift notice.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
2
|
+
* The single-edit pipeline shared by `edit`, previews, and (through the
|
|
3
|
+
* engine) `batch_edit`: resolve → normalize → hash → verify anchors against
|
|
4
|
+
* served state → apply → re-hash with stable anchors → drift notice. The
|
|
5
|
+
* apply/reject/re-hash computation lives in the edit engine
|
|
6
|
+
* ({@link edit-engine}); this module keeps the single-edit orchestration
|
|
7
|
+
* shape, preview-mode options, and the pipeline result type. All IO goes
|
|
8
|
+
* through the {@link FileIO} bridge so the same pipeline runs on `ctx.fs` and
|
|
9
|
+
* on the host filesystem.
|
|
7
10
|
* @module dsh-better-edit/edit-pipeline
|
|
8
11
|
*/
|
|
9
12
|
import type { HashStore } from './hash-store.js';
|
|
10
13
|
import type { FileIO } from './fs-bridge.js';
|
|
11
14
|
import type { LineEnding } from './edit-diff.js';
|
|
12
|
-
import { type
|
|
15
|
+
import { type NEdit } from './hashline/index.js';
|
|
13
16
|
import type { ResolvedRange } from './hashline/served.js';
|
|
14
|
-
import type { EditParams } from './
|
|
15
|
-
export declare function assertReq(request: unknown): asserts request is EditParams;
|
|
16
|
-
export declare function resolveMissingPath(request: Record<string, unknown>): Promise<{
|
|
17
|
-
path: string;
|
|
18
|
-
warning: string;
|
|
19
|
-
} | undefined>;
|
|
17
|
+
import type { EditParams } from './contract.js';
|
|
20
18
|
export interface PipelineResult {
|
|
21
19
|
path: string;
|
|
22
20
|
absolutePath: string;
|
|
@@ -42,11 +40,6 @@ export interface ExecPipelineOptions {
|
|
|
42
40
|
noPersist?: boolean;
|
|
43
41
|
sessionKey?: string;
|
|
44
42
|
}
|
|
45
|
-
export declare function collectRemovedHashes(edit: HEdit, originalHashes: string[]): Set<string>;
|
|
46
|
-
export declare function countLineChanges(edit: HEdit, originalHashes: string[], isNoop: boolean, removedAutoFixes: number): {
|
|
47
|
-
totalAddedLines: number;
|
|
48
|
-
totalRemovedLines: number;
|
|
49
|
-
};
|
|
50
43
|
export declare function execPipeline(io: FileIO, params: EditParams, cwd: string, options?: ExecPipelineOptions): Promise<PipelineResult>;
|
|
51
44
|
/** Resolve the display path a caller names against the session cwd. */
|
|
52
45
|
export declare function resolveDisplayPath(path: string, cwd: string): string;
|
package/lib/types/fs-write.d.ts
CHANGED
|
@@ -1,25 +1,75 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The hash store — ONE deep persistence module for the hashline domain.
|
|
3
|
+
*
|
|
4
|
+
* Owns the sqlite db, the schema and migrations, corruption quarantine,
|
|
5
|
+
* busy-retry, WAL, the legacy-JSON migration, AND the three narrow row APIs
|
|
6
|
+
* the rest of the plugin needs: hash snapshots, undo entries, and served
|
|
7
|
+
* rows. The prepared statements are a private implementation detail — callers
|
|
8
|
+
* use domain methods, never SQL.
|
|
9
|
+
*
|
|
10
|
+
* Corrupt-row handling (parse the JSON column → validate against the hash
|
|
11
|
+
* alphabet → delete the corrupt row) lives here, once, for every row family.
|
|
12
|
+
* Cross-table cleanup (pruneMissing) lives here too — a sibling module never
|
|
13
|
+
* reaches into another family's rows.
|
|
14
|
+
* @module dsh-better-edit/hash-store
|
|
15
|
+
*/
|
|
16
|
+
/** The legacy JSON snapshot shape (pre-sqlite stores). */
|
|
17
|
+
export interface LegacySnapshot {
|
|
18
|
+
content: string;
|
|
19
|
+
hashes: string[];
|
|
20
|
+
}
|
|
21
|
+
export declare function isValidHashList(value: unknown): value is string[];
|
|
22
|
+
export declare function isValidSnapshot(value: unknown): value is LegacySnapshot;
|
|
23
|
+
/** A served-row array: per-position hash, or null for never-served slots. */
|
|
24
|
+
export declare function isValidServedList(value: unknown): value is (string | null)[];
|
|
25
|
+
/** The undo row contract shared by undo-edit and the store. */
|
|
26
|
+
export interface UndoRecord {
|
|
27
|
+
content: string;
|
|
28
|
+
bom: string;
|
|
29
|
+
ending: string;
|
|
30
|
+
hashes: string[];
|
|
31
|
+
resultContent: string;
|
|
19
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* The domain face of the hash store. Each row family gets a narrow API;
|
|
35
|
+
* corruption healing (parse → validate → delete) happens inside the getters.
|
|
36
|
+
*/
|
|
20
37
|
export interface HashStore {
|
|
21
|
-
readonly stmts: Prepared;
|
|
22
38
|
readonly engine: "node:sqlite";
|
|
39
|
+
/** The stored hashes for a path+content, or undefined on a miss; a corrupt row is deleted (when deleteCorrupt) and treated as a miss. */
|
|
40
|
+
getSnapshot(path: string, content: string, deleteCorrupt?: boolean): string[] | undefined;
|
|
41
|
+
upsertSnapshot(path: string, checksum: string, lineCount: number, hashes: string[]): void;
|
|
42
|
+
/** Every path referenced by any row family (snapshots ∪ undo ∪ served). */
|
|
43
|
+
allKnownPaths(): {
|
|
44
|
+
path: string;
|
|
45
|
+
}[];
|
|
46
|
+
/** Every snapshot's path and raw hashes JSON (for path-by-hash scans). */
|
|
47
|
+
allSnapshotHashes(): {
|
|
48
|
+
path: string;
|
|
49
|
+
hashes: string;
|
|
50
|
+
}[];
|
|
51
|
+
deleteSnapshot(path: string): void;
|
|
52
|
+
/** Paths whose stored snapshot hashes contain every given anchor. */
|
|
53
|
+
findSnapshotPaths(hashes: string[]): string[];
|
|
54
|
+
/** The undo row for a path, healing a corrupt row (parse → validate → delete). */
|
|
55
|
+
getUndo(path: string): UndoRecord | undefined;
|
|
56
|
+
upsertUndo(path: string, entry: UndoRecord): void;
|
|
57
|
+
deleteUndo(path: string): void;
|
|
58
|
+
/** The served hashes array for a session+path, healing a corrupt row; [] when nothing was served. */
|
|
59
|
+
getServed(sessionKey: string, path: string): (string | null)[];
|
|
60
|
+
/** The reported-drift hash set for a session+path (lenient parse, never deletes). */
|
|
61
|
+
getServedReported(sessionKey: string, path: string): Set<string>;
|
|
62
|
+
/** Persist the hashes JSON column for a session+path. */
|
|
63
|
+
upsertServed(sessionKey: string, path: string, hashesJson: string): void;
|
|
64
|
+
/** Persist the reported-drift JSON column for a session+path (inserting a fresh empty hashes row). */
|
|
65
|
+
upsertServedReported(sessionKey: string, path: string, reportedJson: string): void;
|
|
66
|
+
clearServedReported(sessionKey: string, path: string): void;
|
|
67
|
+
deleteServed(sessionKey: string, path: string): void;
|
|
68
|
+
deleteServedByPath(path: string): void;
|
|
69
|
+
wipeServed(sessionKey: string): void;
|
|
70
|
+
pruneServedOlderThan(ts: number): void;
|
|
71
|
+
/** Delete every row family's entries for paths that no longer exist on disk. */
|
|
72
|
+
pruneMissing(): Promise<void>;
|
|
23
73
|
}
|
|
24
74
|
export declare function isCorruptionError(error: unknown): boolean;
|
|
25
75
|
/**
|
|
@@ -37,9 +87,7 @@ export declare function shutdownHashStore(): void;
|
|
|
37
87
|
* caller has already loaded the store in every in-process path).
|
|
38
88
|
*/
|
|
39
89
|
export declare function withStore(fn: () => void): void;
|
|
40
|
-
|
|
41
|
-
export
|
|
42
|
-
|
|
43
|
-
export
|
|
44
|
-
export { upsertUndo, getUndoEntry, deleteUndo, } from "./undo-store.js";
|
|
45
|
-
export type { UndoRecord } from "./undo-store.js";
|
|
90
|
+
/** Find files whose stored snapshot hashes contain every given anchor. */
|
|
91
|
+
export declare function findSnapshotPathsByHashes(hashes: string[]): Promise<string[]>;
|
|
92
|
+
/** Persist a hash snapshot for one path (async over the active store). */
|
|
93
|
+
export declare function upsertSnapshotFor(path: string, checksum: string, lineCount: number, hashes: string[]): Promise<void>;
|
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
import { type HashStore } from "../hash-store.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export declare const HL_PREFIX_PLUS_RE: RegExp;
|
|
11
|
-
export declare const HL_PREFIX_MINUS_RE: RegExp;
|
|
12
|
-
export declare const HL_BARE_PREFIX_RE: RegExp;
|
|
13
|
-
export declare function canon(line: string): string;
|
|
14
|
-
export declare function _lineHashesPure(content: string): string[];
|
|
2
|
+
/**
|
|
3
|
+
* Persistence-aware hash-anchor assignment: the pure hasher
|
|
4
|
+
* ({@link lineHashesPure} / {@link mapStableHashes} in ./pure.js) composed
|
|
5
|
+
* with snapshot load/upsert in the hash store, keyed by
|
|
6
|
+
* (path, content checksum, line count). Without a `path` this never touches
|
|
7
|
+
* the store and is exactly the pure path. With `previous` the stable re-hash
|
|
8
|
+
* mapping keeps surviving anchors stable across edits.
|
|
9
|
+
*/
|
|
15
10
|
export declare function lineHashes(content: string, path?: string, previous?: {
|
|
16
11
|
content: string;
|
|
17
12
|
hashes: string[];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { ANCHOR_LEN, HASH_SEP, HASH_SPACE, HASH_PROBE_STRIDE, MAX_HASH_LINES, lineHashesPure, initHasher, canon, } from "./pure.js";
|
|
2
|
+
export { lineHashes } from "./hash.js";
|
|
2
3
|
export { parseHashRef, parseText, type Anchor, } from "./parse.js";
|
|
3
|
-
export { type
|
|
4
|
-
export {
|
|
4
|
+
export { type HEdit, type HTEdit, type NEdit, resEdit, } from "./resolve.js";
|
|
5
|
+
export { applyEdit, fmtRegion, changedRange, } from "./apply.js";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { initHasher } from "./hasher.js";
|
|
2
|
+
import { HASH_LEN, ALPH_RE, HASH_CLASS } from "./alphabet.js";
|
|
3
|
+
export { initHasher, HASH_LEN, ALPH_RE, HASH_CLASS };
|
|
4
|
+
export declare const ANCHOR_LEN = 3;
|
|
5
|
+
export declare const HASH_SEP = "\u2502";
|
|
6
|
+
export declare const HASH_SPACE: number;
|
|
7
|
+
export declare const MAX_HASH_LINES: number;
|
|
8
|
+
export declare const HASH_PROBE_STRIDE: number;
|
|
9
|
+
export declare const HL_PREFIX_PLUS_RE: RegExp;
|
|
10
|
+
export declare const HL_PREFIX_MINUS_RE: RegExp;
|
|
11
|
+
export declare const HL_BARE_PREFIX_RE: RegExp;
|
|
12
|
+
export declare function canon(line: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Pure hash-anchor assignment: every line of `content` gets a unique 3-char
|
|
15
|
+
* anchor derived from its canonicalized content via xxhash32 + probe-stride
|
|
16
|
+
* allocation. No I/O, no store — the persistence-aware wrapper
|
|
17
|
+
* {@link lineHashes} (in ./hash.js) composes this with snapshot load/upsert.
|
|
18
|
+
*/
|
|
19
|
+
export declare function lineHashesPure(content: string): string[];
|
|
20
|
+
/**
|
|
21
|
+
* Pure stable re-hash: given the pre-edit content + anchors and the new
|
|
22
|
+
* content (plus the hashes removed by the edit), map surviving lines to
|
|
23
|
+
* their old anchors, re-claim removed anchors for identical lines, and
|
|
24
|
+
* allocate fresh anchors for the rest. No I/O, no store.
|
|
25
|
+
*/
|
|
26
|
+
export declare function mapStableHashes(oldContent: string, oldHashes: string[], newContent: string, removedHashes?: Set<string>): string[];
|
|
@@ -37,7 +37,6 @@ export type HTEdit = {
|
|
|
37
37
|
remove_from: string;
|
|
38
38
|
remove_to: string;
|
|
39
39
|
};
|
|
40
|
-
export declare function fmtMismatch(mismatches: HMismatch[], fileLines: string[], fileHashes: string[], filePath?: string): string;
|
|
41
40
|
export declare function fmtMismatchWithServes(mismatches: HMismatch[], fileLines: string[], fileHashes: string[], filePath?: string): {
|
|
42
41
|
message: string;
|
|
43
42
|
servedRows: ServedRow[];
|
package/lib/types/paths.d.ts
CHANGED
|
@@ -13,3 +13,12 @@ export declare function hashStorePath(cwd?: string): string;
|
|
|
13
13
|
export declare function legacyHashStorePath(cwd?: string): string;
|
|
14
14
|
export declare function hashStoreDir(cwd?: string): string;
|
|
15
15
|
export declare function toCwd(filePath: string, cwd: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Canonicalize a path, resolving every symlink component to its target
|
|
18
|
+
* (loop-guarded, ELOOP on cycles). Non-existent final components resolve
|
|
19
|
+
* lexically — the canonical form of a not-yet-created file. The hashline
|
|
20
|
+
* tools key their state by canonical absolute paths, so the same file reached
|
|
21
|
+
* through different symlink spellings lands on the same store rows.
|
|
22
|
+
* @param path - the path to canonicalize (absolute or relative).
|
|
23
|
+
*/
|
|
24
|
+
export declare function resolveTarget(path: string): Promise<string>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared read-and-serve operation: resolve → read → normalize → render
|
|
3
|
+
* the hashline preview → record the served rows → clear the drift marks →
|
|
4
|
+
* append the UTF-8 rewrite note. Used by the `read` tool and by the write
|
|
5
|
+
* auto-read hook, so the model is always shown fresh anchors the same way no
|
|
6
|
+
* matter which tool produced the read.
|
|
7
|
+
* @module dsh-better-edit/read-and-serve
|
|
8
|
+
*/
|
|
9
|
+
import type { FileIO } from "./fs-bridge.js";
|
|
10
|
+
import type { ServedRow } from "./hashline/served.js";
|
|
11
|
+
/** Appended when the file had non-UTF-8 bytes; editing rewrites it as UTF-8. */
|
|
12
|
+
export declare const UTF8_REWRITE_NOTE = "[Non-UTF-8 bytes shown as U+FFFD; editing rewrites the file as UTF-8.]";
|
|
13
|
+
export interface ReadAndServeOptions {
|
|
14
|
+
/** The session whose served rows these lines belong to. */
|
|
15
|
+
sessionKey: string;
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
/** Pagination for the rendered preview (undefined = from the start). */
|
|
18
|
+
offset?: number;
|
|
19
|
+
limit?: number;
|
|
20
|
+
}
|
|
21
|
+
export interface ReadAndServeResult {
|
|
22
|
+
/** The model-facing read text, including the UTF-8 note when applicable. */
|
|
23
|
+
text: string;
|
|
24
|
+
/** The rows recorded as served (empty when nothing was shown). */
|
|
25
|
+
served: ServedRow[];
|
|
26
|
+
hadUtf8DecodeErrors: boolean;
|
|
27
|
+
absolutePath: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Perform one read-and-serve: normalize the file at `rawPath`, render its
|
|
31
|
+
* hashline preview, record the shown rows as served for the session, and clear
|
|
32
|
+
* the reported-drift marks (a fresh read resets them). The returned text
|
|
33
|
+
* carries the UTF-8 rewrite note when the file had decode errors.
|
|
34
|
+
*
|
|
35
|
+
* Emits nothing on the fs-observation gate — callers that need the
|
|
36
|
+
* observation recorded (the `read` tool) do that themselves with their exec
|
|
37
|
+
* context.
|
|
38
|
+
*/
|
|
39
|
+
export declare function readAndServe(io: FileIO, rawPath: string, cwd: string, options: ReadAndServeOptions): Promise<ReadAndServeResult>;
|
package/lib/types/schema.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* the dsh schema DSL (not TypeBox). `path` is deliberately NOT `required` at
|
|
4
4
|
* the schema level: the tools accept the built-in `file_path` spelling too
|
|
5
5
|
* (the implicit parameter root stays open), and enforce path presence in
|
|
6
|
-
* `
|
|
6
|
+
* `assertEditRequest` after `normalizeFilePath` aliasing.
|
|
7
7
|
* @module dsh-better-edit/schema
|
|
8
8
|
*/
|
|
9
9
|
export declare const replacementTextSchema: {
|
|
@@ -22,18 +22,4 @@ export declare const pathSchema: {
|
|
|
22
22
|
readonly type: "string";
|
|
23
23
|
readonly 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
24
|
};
|
|
25
|
-
export
|
|
26
|
-
path: string;
|
|
27
|
-
remove_from: string;
|
|
28
|
-
remove_to: string;
|
|
29
|
-
replacement_text: string;
|
|
30
|
-
}
|
|
31
|
-
export interface BatchItemParams {
|
|
32
|
-
path?: string;
|
|
33
|
-
remove_from: string;
|
|
34
|
-
remove_to: string;
|
|
35
|
-
replacement_text: string;
|
|
36
|
-
}
|
|
37
|
-
export interface BatchEditParams {
|
|
38
|
-
edits: BatchItemParams[];
|
|
39
|
-
}
|
|
25
|
+
export type { EditParams, BatchItemParams, BatchEditParams, } from "./contract.js";
|
|
@@ -1,4 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Served state — what the model has been shown, per session and path.
|
|
3
|
+
*
|
|
4
|
+
* This is the ONE module that owns the served-state concept: the served-row
|
|
5
|
+
* merge invariant, the reported-drift set, and the position-reconstruction
|
|
6
|
+
* math used by drift notices. Persistence goes through the hash store's
|
|
7
|
+
* served domain API (`hash-store.ts`); the reject-and-serve errors and range
|
|
8
|
+
* verification live with the hashline module (`hashline/served.ts`); drift
|
|
9
|
+
* computation lives in `drift.ts`. Both import from here.
|
|
10
|
+
*
|
|
11
|
+
* The API is async-only: every operation loads the per-workspace hash store
|
|
12
|
+
* and resolves it through the active workspace context. There is deliberately
|
|
13
|
+
* no sync half — the two-vocabulary, every-op-twice surface it produced is
|
|
14
|
+
* gone.
|
|
15
|
+
* @module dsh-better-edit/served-store
|
|
16
|
+
*/
|
|
2
17
|
export type ServedEntry = {
|
|
3
18
|
position: number;
|
|
4
19
|
hash: string | null;
|
|
@@ -9,45 +24,61 @@ export type ServedEntry = {
|
|
|
9
24
|
* process-local fallback so hashline verification never fabricates a session.
|
|
10
25
|
*/
|
|
11
26
|
export declare function sessionKeyFor(sessionId?: string): string;
|
|
12
|
-
export declare function getServed(store: HashStore, sessionKey: string, path: string): (string | null)[];
|
|
13
|
-
export declare function upsertServed(store: HashStore, sessionKey: string, path: string, entries: Array<{
|
|
14
|
-
position: number;
|
|
15
|
-
hash: string | null;
|
|
16
|
-
}>): void;
|
|
17
|
-
export declare function recordServes(store: HashStore, sessionKey: string, path: string, rows: Array<{
|
|
18
|
-
position: number;
|
|
19
|
-
hash: string | null;
|
|
20
|
-
}>, lineCount?: number): void;
|
|
21
27
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
28
|
+
* Merge served rows into a copy of the stored array. This single helper owns
|
|
29
|
+
* the served-merge invariant shared by {@link recordServed} and
|
|
30
|
+
* {@link recordServedTruncated}:
|
|
31
|
+
*
|
|
32
|
+
* - positions are validated before anything is written (invalid rows throw
|
|
33
|
+
* and never reach the store);
|
|
34
|
+
* - when `truncateTo` is given, positions beyond the file's line count are
|
|
35
|
+
* dropped FIRST — a serve of the current file state must never leave a hash
|
|
36
|
+
* at an old position beyond the line count, because that stale claim makes
|
|
37
|
+
* a chained edit's boundary anchor look ambiguous (`E_RANGE_UNVERIFIED`,
|
|
38
|
+
* "served at N positions") instead of verifying cleanly;
|
|
39
|
+
* - when `clearFrom` is given, positions at/after the mutation's first
|
|
40
|
+
* changed line are re-shaped by the edit, so the model's previous view of
|
|
41
|
+
* them is cleared before the diff rows land;
|
|
42
|
+
* - trailing never-served markers are trimmed so an empty tail never grows
|
|
43
|
+
* the stored array.
|
|
44
|
+
*
|
|
45
|
+
* Underscore-prefixed: internal, exported only so the invariant has a direct
|
|
46
|
+
* test seam.
|
|
33
47
|
*/
|
|
34
|
-
export declare function
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export declare function addReported(store: HashStore, sessionKey: string, path: string, hashes: string[]): void;
|
|
40
|
-
export declare function clearReported(store: HashStore, sessionKey: string, path: string): void;
|
|
41
|
-
export declare function deleteServed(store: HashStore, sessionKey: string, path: string): void;
|
|
42
|
-
export declare function wipeServed(store: HashStore, sessionKey: string): void;
|
|
48
|
+
export declare function _mergeServedRows(current: (string | null)[], rows: ServedEntry[], options?: {
|
|
49
|
+
truncateTo?: number;
|
|
50
|
+
clearFrom?: number;
|
|
51
|
+
}): (string | null)[];
|
|
52
|
+
/** The served array for one session+path, or `[]` when nothing was served. */
|
|
43
53
|
export declare function loadServed(sessionKey: string, path: string): Promise<(string | null)[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Record served rows for one session+path. Without `lineCount` the rows merge
|
|
56
|
+
* into the stored array as-is; with it, the stored array is truncated to the
|
|
57
|
+
* file's current line count before the rows land (see
|
|
58
|
+
* {@link _mergeServedRows} for why). Failures are logged, never thrown — a
|
|
59
|
+
* store problem must not fail the read that produced these rows.
|
|
60
|
+
*/
|
|
44
61
|
export declare function recordServed(sessionKey: string, path: string, rows: ServedEntry[], lineCount?: number): Promise<void>;
|
|
45
62
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
63
|
+
* Record post-mutation diff serves against the file's CURRENT length. The
|
|
64
|
+
* stored array is truncated to `lineCount` and cleared from `clearFrom`
|
|
65
|
+
* (the edit's first changed line) before the diff rows land, so a hash that
|
|
66
|
+
* survived into the diff never keeps its pre-edit position claim.
|
|
48
67
|
*/
|
|
49
68
|
export declare function recordServedTruncated(sessionKey: string, path: string, rows: ServedEntry[], lineCount: number, clearFrom?: number): Promise<void>;
|
|
69
|
+
/** The hashes already reported as drifted for one session+path. */
|
|
50
70
|
export declare function driftReported(sessionKey: string, path: string): Promise<Set<string>>;
|
|
71
|
+
/** Mark hashes as already reported as drifted, so later notices skip them. */
|
|
51
72
|
export declare function markDriftReported(sessionKey: string, path: string, hashes: string[]): Promise<void>;
|
|
73
|
+
/** Clear the reported-drift set for one session+path (a fresh read resets it). */
|
|
52
74
|
export declare function clearDriftReported(sessionKey: string, path: string): Promise<void>;
|
|
75
|
+
/** Drop every served row and reported-drift mark for one session. */
|
|
53
76
|
export declare function wipeServedState(sessionKey: string): Promise<void>;
|
|
77
|
+
/** Every served position of a hash in a served array (the E_RANGE_UNVERIFIED probe). */
|
|
78
|
+
export declare function servedPositionsOf(served: (string | null)[], hash: string): number[];
|
|
79
|
+
/**
|
|
80
|
+
* Reconstruct where a drifted hash sits in the current file: map through the
|
|
81
|
+
* nearest surviving served neighbor (below, else above), else fall back to
|
|
82
|
+
* the served index plus the range's line delta.
|
|
83
|
+
*/
|
|
84
|
+
export declare function currentPositionOfDrifted(served: (string | null)[], currentPositions: Map<string, number>, surviving: Set<string>, servedIndex: number, delta: number): number;
|
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
* The dsh `batch_edit` tool: several hash-anchored edits in one all-or-nothing
|
|
3
3
|
* call. Items targeting the same file are applied in order against the served
|
|
4
4
|
* state; any failing item rejects the whole batch with nothing written, and
|
|
5
|
-
* the failing item's current range is echoed as fresh serves.
|
|
5
|
+
* the failing item's current range is echoed as fresh serves. The per-file
|
|
6
|
+
* sequencing and the persist-undo → write → restore transaction live in the
|
|
7
|
+
* edit engine; this module owns request preparation and result rendering.
|
|
6
8
|
* @module dsh-better-edit/tool-batch-edit
|
|
7
9
|
*/
|
|
8
10
|
import type { Context } from "@deepseek-ai/cordis";
|
|
@@ -13,6 +15,7 @@ import type { FsSandboxController } from "./sandbox.js";
|
|
|
13
15
|
* @param _rootCtx - host context.
|
|
14
16
|
* @param agentCtx - the agent's scoped context (own scope layer).
|
|
15
17
|
* @param io - the filesystem bridge.
|
|
18
|
+
* @param sandbox - the sandbox-escalation controller.
|
|
16
19
|
* @returns the exact disposer that unregisters the tool.
|
|
17
20
|
*/
|
|
18
21
|
export declare function buildBatchEditTool(io: FileIO, sandbox: FsSandboxController): import("@deepseek-ai/dsh-tools").ToolDefinition;
|
package/lib/types/tool-undo.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import type { Context } from "@deepseek-ai/cordis";
|
|
8
8
|
import type { FileIO } from "./fs-bridge.js";
|
|
9
|
-
import { FsSandboxController } from "./sandbox.js";
|
|
9
|
+
import type { FsSandboxController } from "./sandbox.js";
|
|
10
10
|
/**
|
|
11
11
|
* Register the `undo_last_edit` tool on the calling agent's scope.
|
|
12
12
|
* @param _rootCtx - host context.
|