dsh-better-edit 0.6.3 → 0.8.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 +59 -15
- package/README.md +78 -71
- package/README.zh.md +83 -83
- package/lib/canonical-path.js +4 -1
- package/lib/constants.js +5 -1
- package/lib/contract.js +127 -33
- package/lib/edit-diff.js +1 -5
- package/lib/edit-response.js +5 -10
- package/lib/encoding.js +23 -6
- package/lib/file-encoding-state.js +54 -15
- package/lib/file-view.js +42 -53
- package/lib/fs-bridge.js +27 -16
- package/lib/fs-write.js +1 -1
- package/lib/guidance/materialize.js +1 -6
- package/lib/guidance/resolve.js +1 -1
- package/lib/hash-store.js +133 -43
- package/lib/hashline/anchor-pipeline.js +124 -93
- package/lib/hashline/hash-assign.js +65 -23
- package/lib/hashline/hash.js +6 -6
- package/lib/index.js +6 -0
- package/lib/mutation/engine.js +89 -31
- package/lib/mutation.js +14 -13
- package/lib/prompts.js +12 -11
- package/lib/read-and-serve.js +105 -19
- package/lib/render-text-warning.js +21 -0
- package/lib/sandbox.js +2 -5
- package/lib/self-heal.js +30 -27
- package/lib/session-view.js +161 -24
- package/lib/store-config.js +7 -2
- package/lib/store-lifecycle.js +7 -2
- package/lib/tool-edit.js +10 -6
- package/lib/tool-read.js +6 -9
- package/lib/tool-str-replace-editor.js +274 -0
- package/lib/tool-undo.js +8 -9
- package/lib/types/constants.d.ts +2 -0
- package/lib/types/contract.d.ts +106 -18
- package/lib/types/edit-response.d.ts +4 -0
- package/lib/types/encoding.d.ts +1 -1
- package/lib/types/file-encoding-state.d.ts +4 -1
- package/lib/types/file-view.d.ts +16 -1
- package/lib/types/hash-store.d.ts +12 -1
- package/lib/types/hashline/anchor-pipeline.d.ts +16 -6
- package/lib/types/hashline/hash-assign.d.ts +10 -2
- package/lib/types/hashline/index.d.ts +1 -1
- package/lib/types/mutation/engine.d.ts +2 -1
- package/lib/types/render-text-warning.d.ts +22 -0
- package/lib/types/self-heal.d.ts +2 -0
- package/lib/types/session-view.d.ts +13 -4
- package/lib/types/tool-str-replace-editor.d.ts +27 -0
- package/lib/types/undo-edit.d.ts +1 -1
- package/lib/types/utils.d.ts +7 -0
- package/lib/types/write-hook.d.ts +2 -2
- package/lib/undo-edit.js +6 -8
- package/lib/utils.js +23 -3
- package/lib/workspace-context.js +1 -1
- package/lib/write-hook.js +20 -23
- package/package.json +63 -57
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
*
|
|
17
17
|
* @module dsh-better-edit/hashline/anchor-pipeline
|
|
18
18
|
*/
|
|
19
|
+
import { CodedError } from "../utils.js";
|
|
19
20
|
export type Anchor = {
|
|
20
21
|
hash: string;
|
|
21
22
|
};
|
|
@@ -44,31 +45,40 @@ export type HTEdit = {
|
|
|
44
45
|
remove_from: string;
|
|
45
46
|
remove_to: string;
|
|
46
47
|
};
|
|
47
|
-
export declare function resEdit(edit: HTEdit,
|
|
48
|
+
export declare function resEdit(edit: HTEdit, _warnings?: string[]): HEdit;
|
|
48
49
|
declare function warnUnicodeEsc(edit: HEdit, warnings: string[]): void;
|
|
49
50
|
export declare function findNewEdge(): undefined;
|
|
50
51
|
export { warnUnicodeEsc };
|
|
51
|
-
export type ServedCode = "
|
|
52
|
+
export type ServedCode = "E_STALE_RANGE" | "E_UNSERVED_RANGE";
|
|
52
53
|
export interface ServedRow {
|
|
53
54
|
position: number;
|
|
54
55
|
hash: string;
|
|
55
56
|
}
|
|
56
|
-
export declare class ServedRejectionError extends
|
|
57
|
+
export declare class ServedRejectionError extends CodedError {
|
|
57
58
|
readonly code: ServedCode;
|
|
59
|
+
readonly unservedKind: "boundary" | "interior" | undefined;
|
|
58
60
|
readonly firstOffendingLine: number | undefined;
|
|
59
61
|
readonly servedRows: ServedRow[];
|
|
60
62
|
constructor(opts: {
|
|
61
63
|
code: ServedCode;
|
|
64
|
+
unservedKind?: "boundary" | "interior";
|
|
62
65
|
message: string;
|
|
63
66
|
firstOffendingLine?: number;
|
|
64
67
|
servedRows: ServedRow[];
|
|
65
68
|
});
|
|
66
69
|
}
|
|
67
70
|
export declare function isServedRejection(error: unknown): error is ServedRejectionError;
|
|
68
|
-
export declare class AnchorMismatchError extends
|
|
71
|
+
export declare class AnchorMismatchError extends CodedError {
|
|
69
72
|
readonly servedRows: ServedRow[];
|
|
70
73
|
constructor(message: string, servedRows: ServedRow[]);
|
|
71
74
|
}
|
|
75
|
+
/** Thrown when replacement_text carries anchor-syntax garbage (HASH│/diff-preview prefixes).
|
|
76
|
+
* Carries the stripped edit so applyEdit can distinguish served-echo (→ E_SERVED_ECHO
|
|
77
|
+
* denial downstream) from garbage (→ E_BAD_ANCHOR stands). */
|
|
78
|
+
export declare class BadAnchorError extends CodedError {
|
|
79
|
+
readonly stripped: HEdit;
|
|
80
|
+
constructor(message: string, stripped: HEdit);
|
|
81
|
+
}
|
|
72
82
|
export declare function isAnchorMismatch(error: unknown): error is AnchorMismatchError;
|
|
73
83
|
export declare function findEditHashEcho(replacementLines: string[], served: readonly (string | null)[], startLine: number): {
|
|
74
84
|
k: number;
|
|
@@ -89,7 +99,7 @@ export declare function verifyServedRange(args: {
|
|
|
89
99
|
fileLines: string[];
|
|
90
100
|
filePath?: string;
|
|
91
101
|
servedCanons?: (string | null)[];
|
|
92
|
-
|
|
102
|
+
retired?: ReadonlySet<string>;
|
|
93
103
|
epochSnapshotId?: string;
|
|
94
104
|
curSnapshotId?: string;
|
|
95
105
|
strictPos?: boolean;
|
|
@@ -108,7 +118,7 @@ type LIdx = {
|
|
|
108
118
|
lineStarts: number[];
|
|
109
119
|
};
|
|
110
120
|
export declare function buildIdx(content: string): LIdx;
|
|
111
|
-
export declare function applyEdit(content: string, edit: HEdit, signal?: AbortSignal, precomputedHashes?: string[], filePath?: string, served?: (string | null)[], servedCanons?: (string | null)[],
|
|
121
|
+
export declare function applyEdit(content: string, edit: HEdit, signal?: AbortSignal, precomputedHashes?: string[], filePath?: string, served?: (string | null)[], servedCanons?: (string | null)[], retired?: ReadonlySet<string>, epochSnapshotId?: string, curSnapshotId?: string, strictPos?: boolean): {
|
|
112
122
|
content: string;
|
|
113
123
|
firstChangedLine: number | undefined;
|
|
114
124
|
lastChangedLine: number | undefined;
|
|
@@ -16,6 +16,14 @@ export declare const HASH_SEP = "\u2502";
|
|
|
16
16
|
export declare const HASH_SPACE: number;
|
|
17
17
|
export declare const MAX_HASH_LINES: number;
|
|
18
18
|
export declare const HASH_PROBE_STRIDE: number;
|
|
19
|
+
export declare class AnchorSpaceExhaustedError extends Error {
|
|
20
|
+
readonly code = "E_ANCHOR_SPACE_EXHAUSTED";
|
|
21
|
+
readonly retiredCount: number;
|
|
22
|
+
readonly servedCount: number;
|
|
23
|
+
constructor(retiredCount: number, servedCount: number, reservedCount: number);
|
|
24
|
+
}
|
|
25
|
+
export declare function idxToHash(idx: number): string;
|
|
26
|
+
export declare function hashAt(idx: number): string;
|
|
19
27
|
export declare function rememberHashCanon(hash: string, canonText: string): void;
|
|
20
28
|
export declare function getCanonForHash(hash: string): string | undefined;
|
|
21
29
|
export declare const HL_PREFIX_PLUS_RE: RegExp;
|
|
@@ -23,5 +31,5 @@ export declare const HL_PREFIX_MINUS_RE: RegExp;
|
|
|
23
31
|
export declare const HL_BARE_PREFIX_RE: RegExp;
|
|
24
32
|
export declare const CANON_VERSION = 2;
|
|
25
33
|
export declare function canon(line: string): string;
|
|
26
|
-
export declare function lineHashesPure(content: string, reservedHashes?: ReadonlySet<string
|
|
27
|
-
export declare function mapStableHashes(oldContent: string, oldHashes: string[], newContent: string, removedHashes?: Set<string>, reservedHashes?: ReadonlySet<string
|
|
34
|
+
export declare function lineHashesPure(content: string, reservedHashes?: ReadonlySet<string>, retiredCount?: number, servedCount?: number): string[];
|
|
35
|
+
export declare function mapStableHashes(oldContent: string, oldHashes: string[], newContent: string, removedHashes?: Set<string>, reservedHashes?: ReadonlySet<string>, retiredCount?: number, servedCount?: number): string[];
|
|
@@ -10,4 +10,4 @@ export type { Anchor } from "./parse.js";
|
|
|
10
10
|
export { resEdit } from "./anchor-pipeline.js";
|
|
11
11
|
export type { HEdit, HTEdit, NEdit } from "./anchor-pipeline.js";
|
|
12
12
|
export { applyEdit, fmtRegion, changedRange, buildIdx, ServedRejectionError, AnchorMismatchError, EditHashEchoError, findEditHashEcho, isServedRejection, isAnchorMismatch, verifyServedRange, buildRangeEcho, fmtServedRows, } from "./anchor-pipeline.js";
|
|
13
|
-
export type { ServedRow, ResolvedRange, ServedCode
|
|
13
|
+
export type { ServedRow, ResolvedRange, ServedCode } from "./anchor-pipeline.js";
|
|
@@ -81,10 +81,11 @@ export interface ApplyOneInput {
|
|
|
81
81
|
persist: boolean;
|
|
82
82
|
reservedHashes?: ReadonlySet<string>;
|
|
83
83
|
servedCanons?: (string | null)[];
|
|
84
|
-
|
|
84
|
+
retired?: ReadonlySet<string>;
|
|
85
85
|
epochSnapshotId?: string;
|
|
86
86
|
curSnapshotId?: string;
|
|
87
87
|
strictPos?: boolean;
|
|
88
|
+
sessionKey?: string;
|
|
88
89
|
/** Pre-resolved edit (single path keeps resEdit before IO for error order). */
|
|
89
90
|
edit?: HEdit;
|
|
90
91
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared output-render seam for the `{ text, warning? }` tool payload.
|
|
3
|
+
*
|
|
4
|
+
* `read` (tool-read.ts) and `str_replace_editor` view (tool-str-replace-editor.ts)
|
|
5
|
+
* previously carried verbatim copies of this render closure, so a warning-format
|
|
6
|
+
* change had to be made in two places (report #7). Both now delegate here.
|
|
7
|
+
* @module dsh-better-edit/render-text-warning
|
|
8
|
+
*/
|
|
9
|
+
export type TextWarningValue = {
|
|
10
|
+
text: string;
|
|
11
|
+
warning?: string;
|
|
12
|
+
} | string;
|
|
13
|
+
export type TextBlock = {
|
|
14
|
+
type: "text";
|
|
15
|
+
text: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Render a `{ text, warning? }` payload (or bare string) as content blocks:
|
|
19
|
+
* string input yields a single text block, object input yields the text block
|
|
20
|
+
* plus a second block when `warning` is present.
|
|
21
|
+
*/
|
|
22
|
+
export declare function renderTextWarning(value: TextWarningValue): TextBlock[];
|
package/lib/types/self-heal.d.ts
CHANGED
|
@@ -24,6 +24,8 @@ export interface SelfHealOptions {
|
|
|
24
24
|
toolsSvc: unknown;
|
|
25
25
|
hashReadDef: unknown;
|
|
26
26
|
hashEditDef: unknown;
|
|
27
|
+
/** ADR-0015 shadow def; when present the watcher also heals `str_replace_editor`. */
|
|
28
|
+
hashStrReplaceDef?: unknown;
|
|
27
29
|
healMinIntervalMs?: number;
|
|
28
30
|
}
|
|
29
31
|
export declare function createSelfHealWatcher(options: SelfHealOptions): () => void;
|
|
@@ -42,21 +42,27 @@ export declare function _mergeServedRows(current: (string | null)[], rows: Serve
|
|
|
42
42
|
}): (string | null)[];
|
|
43
43
|
export declare function loadServed(sessionKey: string, path: string): Promise<(string | null)[]>;
|
|
44
44
|
/** Anchors that must not be allocated while any session can still remember them. */
|
|
45
|
-
export declare function loadAnchorReservations(path: string): Promise<AnchorReservations>;
|
|
45
|
+
export declare function loadAnchorReservations(sessionKey: string, path: string): Promise<AnchorReservations>;
|
|
46
46
|
export declare function loadServedCanons(sessionKey: string, path: string): Promise<(string | null)[]>;
|
|
47
47
|
export declare function loadEpochSnapshotId(sessionKey: string, path: string): Promise<string | undefined>;
|
|
48
48
|
export declare function loadRetiredAnchors(sessionKey: string, path: string): Promise<Set<string>>;
|
|
49
49
|
/** Keep freed anchors dead for this session until it has seen the whole file again. */
|
|
50
50
|
export declare function retireAnchors(sessionKey: string, path: string, hashes: Iterable<string>): Promise<void>;
|
|
51
|
-
|
|
52
|
-
export
|
|
51
|
+
/** Full-file context for a complete read — when present and matching `rows`, the call is a full read. */
|
|
52
|
+
export interface FullReadContext {
|
|
53
|
+
hashes: readonly string[];
|
|
54
|
+
canons?: readonly (string | null)[];
|
|
55
|
+
snapshotId?: string;
|
|
56
|
+
}
|
|
57
|
+
export declare function recordServed(sessionKey: string, path: string, rows: ServedEntry[], lineCount?: number, full?: FullReadContext): Promise<void>;
|
|
58
|
+
export declare function recordServedTruncated(sessionKey: string, path: string, rows: ServedEntry[], lineCount: number, clearFrom?: number, fullCanons?: readonly (string | null)[]): Promise<void>;
|
|
53
59
|
export declare function driftReported(sessionKey: string, path: string): Promise<Set<string>>;
|
|
54
60
|
export declare function markDriftReported(sessionKey: string, path: string, hashes: string[]): Promise<void>;
|
|
55
61
|
export declare function clearDriftReported(sessionKey: string, path: string): Promise<void>;
|
|
56
62
|
export declare function wipeServedState(sessionKey: string): Promise<void>;
|
|
57
63
|
export declare function servedPositionsOf(served: (string | null)[], hash: string): number[];
|
|
58
64
|
export declare function currentPositionOfDrifted(served: (string | null)[], currentPositions: Map<string, number>, surviving: Set<string>, servedIndex: number, delta: number): number;
|
|
59
|
-
export declare const DRIFT_NOTICE_HEADING = "drift:";
|
|
65
|
+
export declare const DRIFT_NOTICE_HEADING = "[USER] drift:";
|
|
60
66
|
export interface DriftRow extends ServedRow {
|
|
61
67
|
content: string;
|
|
62
68
|
drifted: boolean;
|
|
@@ -68,6 +74,9 @@ export interface ComputeDriftInput {
|
|
|
68
74
|
range: ResolvedRange;
|
|
69
75
|
reported: Set<string>;
|
|
70
76
|
cap?: number;
|
|
77
|
+
/** WHY (#68): parallel to `served`, whitespace-stripped form at serve time.
|
|
78
|
+
* Absent/empty preserves legacy hash-equality (old DBs, unit callers). */
|
|
79
|
+
servedCanons?: (string | null)[];
|
|
71
80
|
}
|
|
72
81
|
export interface DriftNoticeResult {
|
|
73
82
|
text: string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dsh `str_replace_editor` shadow: a governed agent-layer replacement for
|
|
3
|
+
* the built-in `@deepseek-ai/dsh-tool-str-replace-editor` with identical
|
|
4
|
+
* contract (`view` / `str_replace` / `insert` / `create`, no new `encoding`
|
|
5
|
+
* param) but routed through the file-encoding-state seam.
|
|
6
|
+
*
|
|
7
|
+
* - `view` is observation: decode via the `ctxFsIO` path (`decodeForOpen`
|
|
8
|
+
* inside `io.readText`), records file encoding state, emits `fs/observed`,
|
|
9
|
+
* and never records served state (so it can feed `write` round-trips but
|
|
10
|
+
* cannot authorize hashline `edit` anchors).
|
|
11
|
+
* - `str_replace` / `insert` without a prior `view`/`read` fail loud with
|
|
12
|
+
* `E_BLIND_REPLACE` — no disk write.
|
|
13
|
+
* - `create` defaults to UTF-8 without BOM.
|
|
14
|
+
* - `undo_edit` is out of scope → `E_UNSUPPORTED` (use `undo_last_edit`).
|
|
15
|
+
*
|
|
16
|
+
* See ADR-0015.
|
|
17
|
+
* @module dsh-better-edit/tool-str-replace-editor
|
|
18
|
+
*/
|
|
19
|
+
import type { FileIO } from "./fs-bridge.js";
|
|
20
|
+
export declare const STR_REPLACE_EDITOR_DESCRIPTION: string;
|
|
21
|
+
export declare function buildStrReplaceEditorTool(io: FileIO): import("@deepseek-ai/dsh-tools").ToolDefinition;
|
|
22
|
+
/** Register the shadow `str_replace_editor` on the calling agent's scope (own layer). */
|
|
23
|
+
export declare function registerStrReplaceEditorTool(agentCtx: {
|
|
24
|
+
tools: {
|
|
25
|
+
register(def: unknown): () => void;
|
|
26
|
+
};
|
|
27
|
+
}, io: FileIO): () => void;
|
package/lib/types/undo-edit.d.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* overwritten. Undo survives restarts (the store is on disk).
|
|
8
8
|
* @module dsh-better-edit/undo-edit
|
|
9
9
|
*/
|
|
10
|
-
import type { LineEnding } from
|
|
10
|
+
import type { LineEnding } from "./edit-diff.js";
|
|
11
11
|
export interface UndoEntry {
|
|
12
12
|
content: string;
|
|
13
13
|
bom: string;
|
package/lib/types/utils.d.ts
CHANGED
|
@@ -11,3 +11,10 @@ export declare function firstNonEmptyIndex(lines: string[]): number;
|
|
|
11
11
|
export declare function lastNonEmpty(lines: string[]): string | undefined;
|
|
12
12
|
export declare function firstNonEmpty(lines: string[]): string | undefined;
|
|
13
13
|
export declare function clipLine(line: string, maxLen?: number): string;
|
|
14
|
+
/** Machine-readable error-code carrier — bare `E_*` code alongside the human message. */
|
|
15
|
+
export declare class CodedError extends Error {
|
|
16
|
+
readonly code: string;
|
|
17
|
+
constructor(code: string, message: string);
|
|
18
|
+
}
|
|
19
|
+
/** Structured bare-code read for any tool error: carrier first, `[E_*]` message convention as fallback. */
|
|
20
|
+
export declare function codeOf(error: unknown): string | undefined;
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* anchors to the model-facing content (mirroring pi-hashline-edit-lsz).
|
|
6
6
|
* @module dsh-better-edit/write-hook
|
|
7
7
|
*/
|
|
8
|
-
import type { Context } from
|
|
9
|
-
import type { FileIO } from
|
|
8
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
9
|
+
import type { FileIO } from "./fs-bridge.js";
|
|
10
10
|
export interface ServedHashEcho {
|
|
11
11
|
/** One-based candidate line carrying the copied anchor. */
|
|
12
12
|
line: number;
|
package/lib/undo-edit.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* overwritten. Undo survives restarts (the store is on disk).
|
|
8
8
|
* @module dsh-better-edit/undo-edit
|
|
9
9
|
*/
|
|
10
|
-
import { loadHashStore } from
|
|
10
|
+
import { loadHashStore } from "./hash-store.js";
|
|
11
11
|
/** Load the last undo row for a path from the active store, if any. */
|
|
12
12
|
async function readUndo(path) {
|
|
13
13
|
const store = await loadHashStore();
|
|
@@ -43,7 +43,7 @@ export async function saveUndo(path, entry) {
|
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
catch (error) {
|
|
46
|
-
console.error(
|
|
46
|
+
console.error("Failed to persist undo entry:", error);
|
|
47
47
|
return { persisted: false, restore: async () => undefined };
|
|
48
48
|
}
|
|
49
49
|
return {
|
|
@@ -56,7 +56,7 @@ export async function saveUndo(path, entry) {
|
|
|
56
56
|
await removeUndo(path);
|
|
57
57
|
}
|
|
58
58
|
catch (error) {
|
|
59
|
-
console.error(
|
|
59
|
+
console.error("Failed to restore previous undo entry:", error);
|
|
60
60
|
}
|
|
61
61
|
},
|
|
62
62
|
};
|
|
@@ -68,9 +68,7 @@ export async function getUndo(path) {
|
|
|
68
68
|
if (!record)
|
|
69
69
|
return undefined;
|
|
70
70
|
const originalEnding = record.ending;
|
|
71
|
-
if (originalEnding !==
|
|
72
|
-
originalEnding !== '\n' &&
|
|
73
|
-
originalEnding !== '\r') {
|
|
71
|
+
if (originalEnding !== "\r\n" && originalEnding !== "\n" && originalEnding !== "\r") {
|
|
74
72
|
await removeUndo(path);
|
|
75
73
|
return undefined;
|
|
76
74
|
}
|
|
@@ -83,7 +81,7 @@ export async function getUndo(path) {
|
|
|
83
81
|
};
|
|
84
82
|
}
|
|
85
83
|
catch (error) {
|
|
86
|
-
console.error(
|
|
84
|
+
console.error("Failed to load undo entry:", error);
|
|
87
85
|
return undefined;
|
|
88
86
|
}
|
|
89
87
|
}
|
|
@@ -93,6 +91,6 @@ export async function clearUndo(path) {
|
|
|
93
91
|
await removeUndo(path);
|
|
94
92
|
}
|
|
95
93
|
catch (error) {
|
|
96
|
-
console.error(
|
|
94
|
+
console.error("Failed to clear undo entry:", error);
|
|
97
95
|
}
|
|
98
96
|
}
|
package/lib/utils.js
CHANGED
|
@@ -23,7 +23,7 @@ export function rejectUnknownFields(obj, allowed, label, hint) {
|
|
|
23
23
|
const unknown = Object.keys(obj).filter((key) => !allowed.has(key));
|
|
24
24
|
if (unknown.length > 0) {
|
|
25
25
|
const suffix = hint ? ` ${hint}` : "";
|
|
26
|
-
throw new Error(`[
|
|
26
|
+
throw new Error(`[MODEL] [E_BAD_PAYLOAD] ${label} contains unknown or unsupported fields: ${unknown.join(", ")}.${suffix}`);
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
export function cntDiff(diff, marker) {
|
|
@@ -31,8 +31,7 @@ export function cntDiff(diff, marker) {
|
|
|
31
31
|
return 0;
|
|
32
32
|
let count = 0;
|
|
33
33
|
for (const line of diff.split("\n")) {
|
|
34
|
-
if (line.startsWith(marker) &&
|
|
35
|
-
!line.startsWith(`${marker}${marker}${marker}`)) {
|
|
34
|
+
if (line.startsWith(marker) && !line.startsWith(`${marker}${marker}${marker}`)) {
|
|
36
35
|
count += 1;
|
|
37
36
|
}
|
|
38
37
|
}
|
|
@@ -74,3 +73,24 @@ export function clipLine(line, maxLen = 200) {
|
|
|
74
73
|
const flat = line.replace(/\n/g, "\\n");
|
|
75
74
|
return flat.length > maxLen ? `${flat.slice(0, maxLen)}...` : flat;
|
|
76
75
|
}
|
|
76
|
+
/** Machine-readable error-code carrier — bare `E_*` code alongside the human message. */
|
|
77
|
+
export class CodedError extends Error {
|
|
78
|
+
code;
|
|
79
|
+
constructor(code, message) {
|
|
80
|
+
super(message);
|
|
81
|
+
this.name = "CodedError";
|
|
82
|
+
this.code = code;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const CODED_RE = /\[(E_[A-Z_]+)\]/;
|
|
86
|
+
/** Structured bare-code read for any tool error: carrier first, `[E_*]` message convention as fallback. */
|
|
87
|
+
export function codeOf(error) {
|
|
88
|
+
if (error instanceof CodedError)
|
|
89
|
+
return error.code;
|
|
90
|
+
if (error instanceof Error) {
|
|
91
|
+
const m = error.message.match(CODED_RE);
|
|
92
|
+
if (m)
|
|
93
|
+
return m[1];
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
package/lib/workspace-context.js
CHANGED
|
@@ -30,7 +30,7 @@ export function sessionKeyFor(sessionId) {
|
|
|
30
30
|
if (sessionId && sessionId.length > 0)
|
|
31
31
|
return sessionId;
|
|
32
32
|
// fallback for previews/tests
|
|
33
|
-
return fallbackSessionKey ??= randomUUID();
|
|
33
|
+
return (fallbackSessionKey ??= randomUUID());
|
|
34
34
|
}
|
|
35
35
|
export function execCwd(exec) {
|
|
36
36
|
return exec.agent?.session.header.cwd ?? process.cwd();
|
package/lib/write-hook.js
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
* anchors to the model-facing content (mirroring pi-hashline-edit-lsz).
|
|
6
6
|
* @module dsh-better-edit/write-hook
|
|
7
7
|
*/
|
|
8
|
-
import { readAndServe } from
|
|
9
|
-
import { execCwd, execSessionKey, withWorkspace } from
|
|
10
|
-
import { loadServed } from
|
|
11
|
-
import { HASH_SEP } from
|
|
12
|
-
import { abortIf, splitLines } from
|
|
13
|
-
const AUTO_READ_HEADING =
|
|
8
|
+
import { readAndServe } from "./read-and-serve.js";
|
|
9
|
+
import { execCwd, execSessionKey, withWorkspace } from "./workspace-context.js";
|
|
10
|
+
import { loadServed } from "./session-view.js";
|
|
11
|
+
import { HASH_SEP } from "./hashline/hash-assign.js";
|
|
12
|
+
import { abortIf, splitLines } from "./utils.js";
|
|
13
|
+
const AUTO_READ_HEADING = "--- Auto-read (hashline anchors) ---";
|
|
14
14
|
/**
|
|
15
15
|
* Find a copied hashline prefix without treating arbitrary `3-char│` text as
|
|
16
16
|
* metadata. A match requires the exact hash currently recorded at the same
|
|
@@ -40,10 +40,10 @@ export async function servedHashEchoDenial(io, rawPath, content, cwd, sessionKey
|
|
|
40
40
|
const match = findServedHashEcho(content, served);
|
|
41
41
|
if (!match)
|
|
42
42
|
return undefined;
|
|
43
|
-
return (`[
|
|
43
|
+
return (`[MODEL] [E_SERVED_ECHO] Refused write to ${rawPath}: line ${match.line} begins with ` +
|
|
44
44
|
`the exact ${match.hash}${HASH_SEP} anchor served for this session, path, and line. ` +
|
|
45
45
|
`HASH${HASH_SEP} anchors are tool output, not file content. ` +
|
|
46
|
-
|
|
46
|
+
"Retry with file content only (remove the entire copied anchor chain). Nothing was written.");
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
49
|
* Register the pre-write echo guard and post-write auto-read on the calling
|
|
@@ -57,19 +57,19 @@ export async function servedHashEchoDenial(io, rawPath, content, cwd, sessionKey
|
|
|
57
57
|
* @returns the exact disposer that unregisters the listener.
|
|
58
58
|
*/
|
|
59
59
|
export function registerWriteHook(rootCtx, agentCtx, io) {
|
|
60
|
-
const disposeGuard = agentCtx.on(
|
|
61
|
-
if (exec.name !==
|
|
60
|
+
const disposeGuard = agentCtx.on("tools/pre-execute", async (exec, next) => {
|
|
61
|
+
if (exec.name !== "write")
|
|
62
62
|
return next();
|
|
63
63
|
const args = exec.arguments;
|
|
64
64
|
const rawPath = args?.file_path ?? args?.path;
|
|
65
65
|
const content = args?.content;
|
|
66
|
-
if (typeof rawPath !==
|
|
66
|
+
if (typeof rawPath !== "string" || typeof content !== "string")
|
|
67
67
|
return next();
|
|
68
68
|
const cwd = execCwd(exec);
|
|
69
69
|
return withWorkspace(cwd, async () => {
|
|
70
70
|
try {
|
|
71
71
|
const reason = await servedHashEchoDenial(io, rawPath, content, cwd, execSessionKey(exec), exec.signal);
|
|
72
|
-
return reason === undefined ? next() : { kind:
|
|
72
|
+
return reason === undefined ? next() : { kind: "deny", reason };
|
|
73
73
|
}
|
|
74
74
|
catch (error) {
|
|
75
75
|
if (exec.signal.aborted)
|
|
@@ -79,19 +79,16 @@ export function registerWriteHook(rootCtx, agentCtx, io) {
|
|
|
79
79
|
}
|
|
80
80
|
});
|
|
81
81
|
});
|
|
82
|
-
const disposeAutoRead = agentCtx.on(
|
|
82
|
+
const disposeAutoRead = agentCtx.on("tools/post-execute", async (exec, result, next) => {
|
|
83
83
|
return withWorkspace(execCwd(exec), async () => {
|
|
84
84
|
const decision = await next();
|
|
85
|
-
if (exec.name !==
|
|
86
|
-
result.isError ||
|
|
87
|
-
decision.kind !== 'accept') {
|
|
85
|
+
if (exec.name !== "write" || result.isError || decision.kind !== "accept") {
|
|
88
86
|
return decision;
|
|
89
87
|
}
|
|
90
88
|
const decisionContent = decision.content ?? result.content;
|
|
91
|
-
const rawPath = exec.arguments
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (typeof rawPath !== 'string')
|
|
89
|
+
const rawPath = exec.arguments?.file_path ??
|
|
90
|
+
exec.arguments?.path;
|
|
91
|
+
if (typeof rawPath !== "string")
|
|
95
92
|
return decision;
|
|
96
93
|
try {
|
|
97
94
|
const cwd = execCwd(exec);
|
|
@@ -99,10 +96,10 @@ export function registerWriteHook(rootCtx, agentCtx, io) {
|
|
|
99
96
|
const signal = exec.signal;
|
|
100
97
|
const { text } = await readAndServe(io, rawPath, cwd, { sessionKey, signal });
|
|
101
98
|
return {
|
|
102
|
-
kind:
|
|
99
|
+
kind: "accept",
|
|
103
100
|
content: [
|
|
104
|
-
...
|
|
105
|
-
{ type:
|
|
101
|
+
...decisionContent,
|
|
102
|
+
{ type: "text", text: `\n\n${AUTO_READ_HEADING}\n${text}` },
|
|
106
103
|
],
|
|
107
104
|
};
|
|
108
105
|
}
|
package/package.json
CHANGED
|
@@ -1,16 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-better-edit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"type": "module",
|
|
3
|
+
"version": "0.8.0",
|
|
5
4
|
"description": "Hash-anchored read/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
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"anchors",
|
|
7
|
+
"batch-edit",
|
|
8
|
+
"coding-agent",
|
|
9
|
+
"deepseek-harness",
|
|
10
|
+
"dsh",
|
|
11
|
+
"dsh-plugin",
|
|
12
|
+
"edit",
|
|
13
|
+
"file-editing",
|
|
14
|
+
"hash",
|
|
15
|
+
"hash-anchored",
|
|
16
|
+
"hashline",
|
|
17
|
+
"oh-my-pi",
|
|
18
|
+
"read",
|
|
19
|
+
"stable",
|
|
20
|
+
"undo"
|
|
21
|
+
],
|
|
22
|
+
"homepage": "https://github.com/Rianico/dsh-better-edit",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/Rianico/dsh-better-edit/issues"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"author": "dsh-better-edit contributors",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/Rianico/dsh-better-edit.git"
|
|
14
31
|
},
|
|
15
32
|
"files": [
|
|
16
33
|
"lib",
|
|
@@ -21,47 +38,15 @@
|
|
|
21
38
|
"assets/",
|
|
22
39
|
"CHANGELOG.md"
|
|
23
40
|
],
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"keywords": [
|
|
34
|
-
"dsh",
|
|
35
|
-
"dsh-plugin",
|
|
36
|
-
"deepseek-harness",
|
|
37
|
-
"coding-agent",
|
|
38
|
-
"oh-my-pi",
|
|
39
|
-
"hashline",
|
|
40
|
-
"hash-anchored",
|
|
41
|
-
"hash",
|
|
42
|
-
"anchors",
|
|
43
|
-
"read",
|
|
44
|
-
"edit",
|
|
45
|
-
"batch-edit",
|
|
46
|
-
"undo",
|
|
47
|
-
"file-editing",
|
|
48
|
-
"stable"
|
|
49
|
-
],
|
|
50
|
-
"license": "MIT",
|
|
51
|
-
"dsh": {
|
|
52
|
-
"bundle": {
|
|
53
|
-
"patch": "./cordis.patch.yml"
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
"dependencies": {
|
|
57
|
-
"diff": "^8.0.2",
|
|
58
|
-
"file-type": "^21.3.0",
|
|
59
|
-
"iconv-lite": "^0.6.3",
|
|
60
|
-
"xxhash-wasm": "^1.1.0",
|
|
61
|
-
"zod": "^3.25.76"
|
|
62
|
-
},
|
|
63
|
-
"engines": {
|
|
64
|
-
"node": "^22.19.0 || >=24.0.0"
|
|
41
|
+
"type": "module",
|
|
42
|
+
"main": "lib/index.js",
|
|
43
|
+
"types": "lib/types/index.d.ts",
|
|
44
|
+
"exports": {
|
|
45
|
+
".": {
|
|
46
|
+
"types": "./lib/types/index.d.ts",
|
|
47
|
+
"default": "./lib/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./package.json": "./package.json"
|
|
65
50
|
},
|
|
66
51
|
"scripts": {
|
|
67
52
|
"prepare": "tsc -p tsconfig.json && husky",
|
|
@@ -73,11 +58,19 @@
|
|
|
73
58
|
"benchmark": "node benchmark/run.mjs",
|
|
74
59
|
"postpublish": "node scripts/tag-current.mjs",
|
|
75
60
|
"release": "node scripts/release.mjs",
|
|
76
|
-
"lint": "
|
|
77
|
-
"lint:fix": "
|
|
78
|
-
"format": "
|
|
79
|
-
"format:check": "
|
|
80
|
-
"test:coverage": "vitest run --coverage"
|
|
61
|
+
"lint": "oxlint .",
|
|
62
|
+
"lint:fix": "oxlint --fix .",
|
|
63
|
+
"format": "oxfmt --check .",
|
|
64
|
+
"format:check": "oxfmt --check .",
|
|
65
|
+
"test:coverage": "vitest run --coverage",
|
|
66
|
+
"format:fix": "oxfmt ."
|
|
67
|
+
},
|
|
68
|
+
"dependencies": {
|
|
69
|
+
"diff": "^8.0.2",
|
|
70
|
+
"file-type": "^21.3.0",
|
|
71
|
+
"iconv-lite": "^0.6.3",
|
|
72
|
+
"xxhash-wasm": "^1.1.0",
|
|
73
|
+
"zod": "^3.25.76"
|
|
81
74
|
},
|
|
82
75
|
"devDependencies": {
|
|
83
76
|
"@biomejs/biome": "^2.1.0",
|
|
@@ -98,11 +91,24 @@
|
|
|
98
91
|
"conventional-changelog-conventionalcommits": "^8.0.0",
|
|
99
92
|
"husky": "^9.1.7",
|
|
100
93
|
"js-tiktoken": "1.0.21",
|
|
94
|
+
"oxfmt": ">=0.15",
|
|
95
|
+
"oxlint": ">=1",
|
|
101
96
|
"semantic-release": "^25.0.9",
|
|
102
|
-
"
|
|
97
|
+
"tsx": ">=4",
|
|
98
|
+
"typescript": ">=7",
|
|
99
|
+
"vite": ">=8",
|
|
103
100
|
"vitest": "^4.1.8"
|
|
104
101
|
},
|
|
105
102
|
"optionalDependencies": {
|
|
106
103
|
"chardet": "2.2.0"
|
|
104
|
+
},
|
|
105
|
+
"engines": {
|
|
106
|
+
"node": ">=24"
|
|
107
|
+
},
|
|
108
|
+
"packageManager": "pnpm@12.0.0",
|
|
109
|
+
"dsh": {
|
|
110
|
+
"bundle": {
|
|
111
|
+
"patch": "./cordis.patch.yml"
|
|
112
|
+
}
|
|
107
113
|
}
|
|
108
114
|
}
|