dsh-better-edit 1.2.3 → 1.2.5

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.
Files changed (105) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +39 -20
  3. package/README.zh.md +39 -31
  4. package/lib/contract.js +53 -22
  5. package/lib/domain-errors.js +18 -10
  6. package/lib/edit-mode.js +11 -0
  7. package/lib/encoding.js +2 -0
  8. package/lib/file-encoding-state.js +138 -44
  9. package/lib/file-view.js +26 -205
  10. package/lib/fs-bridge.js +53 -71
  11. package/lib/hashline/anchor-pipeline.js +8 -499
  12. package/lib/hashline/hash-assign.js +55 -4
  13. package/lib/hashline/index.js +5 -2
  14. package/lib/hashline/served-evidence.js +531 -0
  15. package/lib/index.js +177 -33
  16. package/lib/mutation/engine.js +19 -61
  17. package/lib/mutation.js +63 -105
  18. package/lib/noop-guard.js +24 -38
  19. package/lib/paths.js +9 -5
  20. package/lib/preset-toolset.js +144 -0
  21. package/lib/prompts.js +3 -3
  22. package/lib/read-and-serve.js +51 -53
  23. package/lib/reclaim.js +65 -0
  24. package/lib/self-heal.js +14 -13
  25. package/lib/session-view.js +79 -49
  26. package/lib/store/index.js +14 -14
  27. package/lib/store-config.js +4 -5
  28. package/lib/store-lifecycle.js +1 -1
  29. package/lib/tool-edit.js +14 -56
  30. package/lib/tool-read.js +2 -8
  31. package/lib/tool-str-replace-editor.js +34 -18
  32. package/lib/tool-undo.js +42 -24
  33. package/lib/types/contract.d.ts +11 -20
  34. package/lib/types/domain-errors.d.ts +14 -10
  35. package/lib/types/edit-mode.d.ts +11 -0
  36. package/lib/types/encoding.d.ts +1 -0
  37. package/lib/types/file-encoding-state.d.ts +51 -14
  38. package/lib/types/file-view.d.ts +18 -56
  39. package/lib/types/fs-bridge.d.ts +0 -1
  40. package/lib/types/hashline/anchor-pipeline.d.ts +6 -128
  41. package/lib/types/hashline/hash-assign.d.ts +40 -0
  42. package/lib/types/hashline/index.d.ts +6 -4
  43. package/lib/types/hashline/served-evidence.d.ts +152 -0
  44. package/lib/types/mutation/engine.d.ts +4 -12
  45. package/lib/types/mutation.d.ts +10 -58
  46. package/lib/types/noop-guard.d.ts +2 -33
  47. package/lib/types/paths.d.ts +0 -1
  48. package/lib/types/preset-toolset.d.ts +61 -0
  49. package/lib/types/reclaim.d.ts +36 -0
  50. package/lib/types/self-heal.d.ts +9 -3
  51. package/lib/types/session-view.d.ts +38 -8
  52. package/lib/types/store/index.d.ts +12 -11
  53. package/lib/types/store-config.d.ts +1 -1
  54. package/lib/types/store-lifecycle.d.ts +12 -2
  55. package/lib/types/tool-edit.d.ts +2 -4
  56. package/lib/types/tool-read.d.ts +0 -5
  57. package/lib/types/tool-str-replace-editor.d.ts +0 -6
  58. package/lib/types/tool-undo.d.ts +0 -5
  59. package/package.json +3 -2
  60. package/lib/drift.js +0 -5
  61. package/lib/dsh-context.js +0 -5
  62. package/lib/edit-engine.js +0 -7
  63. package/lib/edit-normalize.js +0 -9
  64. package/lib/edit-pipeline.js +0 -6
  65. package/lib/file-kind.js +0 -6
  66. package/lib/file-reader.js +0 -6
  67. package/lib/guidance/index.js +0 -8
  68. package/lib/hashline/alphabet.js +0 -2
  69. package/lib/hashline/apply.js +0 -2
  70. package/lib/hashline/hasher.js +0 -2
  71. package/lib/hashline/parse.js +0 -2
  72. package/lib/hashline/pure.js +0 -2
  73. package/lib/hashline/resolve.js +0 -3
  74. package/lib/hashline/served.js +0 -2
  75. package/lib/preview-controller.js +0 -65
  76. package/lib/read-render.js +0 -6
  77. package/lib/schema.js +0 -24
  78. package/lib/served-store.js +0 -5
  79. package/lib/tool-batch-edit.js +0 -190
  80. package/lib/truncate.js +0 -6
  81. package/lib/types/drift.d.ts +0 -5
  82. package/lib/types/dsh-context.d.ts +0 -5
  83. package/lib/types/edit-engine.d.ts +0 -7
  84. package/lib/types/edit-normalize.d.ts +0 -1
  85. package/lib/types/edit-pipeline.d.ts +0 -6
  86. package/lib/types/file-kind.d.ts +0 -6
  87. package/lib/types/file-reader.d.ts +0 -6
  88. package/lib/types/guidance/index.d.ts +0 -8
  89. package/lib/types/hashline/alphabet.d.ts +0 -1
  90. package/lib/types/hashline/apply.d.ts +0 -1
  91. package/lib/types/hashline/hasher.d.ts +0 -1
  92. package/lib/types/hashline/parse.d.ts +0 -1
  93. package/lib/types/hashline/pure.d.ts +0 -1
  94. package/lib/types/hashline/resolve.d.ts +0 -1
  95. package/lib/types/hashline/served.d.ts +0 -1
  96. package/lib/types/preview-controller.d.ts +0 -33
  97. package/lib/types/read-render.d.ts +0 -6
  98. package/lib/types/schema.d.ts +0 -25
  99. package/lib/types/served-store.d.ts +0 -5
  100. package/lib/types/tool-batch-edit.d.ts +0 -25
  101. package/lib/types/truncate.d.ts +0 -6
  102. package/lib/types/validation.d.ts +0 -6
  103. package/lib/types/workspace.d.ts +0 -5
  104. package/lib/validation.js +0 -6
  105. package/lib/workspace.js +0 -5
@@ -1,190 +0,0 @@
1
- /**
2
- * The dsh `batch_edit` tool: several hash-anchored edits in one all-or-nothing
3
- * call. Items targeting the same file are applied in order against the served
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. 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.
8
- * @module dsh-better-edit/tool-batch-edit
9
- */
10
- import { defineTool } from "@deepseek-ai/dsh-tools";
11
- import { abortIf, isRec, normalizeFilePath, splitLines, } from "./utils.js";
12
- import { assertBatchEditRequest, normalizeRequest as normReq, } from "./contract.js";
13
- import { commit, resolveMissingPath, applySequence, } from "./mutation.js";
14
- import { buildBatchResult } from "./mutation.js";
15
- import { recordServedTruncated } from "./session-view.js";
16
- import { BATCH_EDIT_DESCRIPTION } from "./prompts.js";
17
- import { pathSchema, removeFromSchema, removeToSchema, replacementTextSchema, } from "./contract.js";
18
- import { execCwd, execSessionKey } from "./session-view.js";
19
- import { withWorkspace } from "./session-view.js";
20
- async function prepareItems(io, params, cwd, signal) {
21
- const items = [];
22
- for (let index = 0; index < params.edits.length; index++) {
23
- const raw = params.edits[index];
24
- const record = { ...raw };
25
- normalizeFilePath(record);
26
- let path = typeof record.path === "string" ? record.path : undefined;
27
- let pathWarning;
28
- if (!path) {
29
- let resolution;
30
- try {
31
- resolution = await resolveMissingPath(record);
32
- }
33
- catch (error) {
34
- if (error instanceof Error) {
35
- throw new Error(`edits[${index}]: ${error.message}`);
36
- }
37
- throw error;
38
- }
39
- if (resolution) {
40
- path = resolution.path;
41
- pathWarning = resolution.warning;
42
- }
43
- }
44
- if (!path) {
45
- throw new Error(`[E_BAD_SHAPE] edits[${index}] requires a non-empty "path" string, and its anchors match no known file.`);
46
- }
47
- items.push({
48
- index,
49
- path,
50
- absolutePath: await io.resolve(path, cwd, signal),
51
- remove_from: record.remove_from,
52
- remove_to: record.remove_to,
53
- replacement_text: record.replacement_text,
54
- pathWarning,
55
- });
56
- }
57
- return items;
58
- }
59
- function groupByPath(items) {
60
- const groups = new Map();
61
- for (const item of items) {
62
- const list = groups.get(item.absolutePath);
63
- if (list)
64
- list.push(item);
65
- else
66
- groups.set(item.absolutePath, [item]);
67
- }
68
- return groups;
69
- }
70
- function toSection(file) {
71
- return {
72
- path: file.displayPath,
73
- originalNormalized: file.originalNormalized,
74
- result: file.result,
75
- originalHashes: file.originalHashes,
76
- resultHashes: file.resultHashes,
77
- warnings: file.warnings,
78
- driftNotice: file.driftNotice,
79
- appliedCount: file.appliedCount,
80
- noopCount: file.noopCount,
81
- totalAddedLines: file.totalAddedLines,
82
- totalRemovedLines: file.totalRemovedLines,
83
- };
84
- }
85
- /**
86
- * Register the `batch_edit` tool on the calling agent's scope.
87
- * @param _rootCtx - host context.
88
- * @param agentCtx - the agent's scoped context (own scope layer).
89
- * @param io - the filesystem bridge.
90
- * @param sandbox - the sandbox-escalation controller.
91
- * @returns the exact disposer that unregisters the tool.
92
- */
93
- export function buildBatchEditTool(io, sandbox) {
94
- return defineTool({
95
- name: "batch_edit",
96
- description: BATCH_EDIT_DESCRIPTION,
97
- parameters: {
98
- edits: {
99
- type: "array",
100
- required: true,
101
- description: `Ordered list of edits, each with the same shape as the edit tool: { path?, remove_from, remove_to, replacement_text }. ` +
102
- "Edits to the same file are applied in order and verified against what was served before anything is written. " +
103
- "The batch is all-or-nothing: if any edit fails validation, nothing is written and the failing edit\u2019s current range is served back. " +
104
- "Use batch_edit when you have multiple edits; do not issue several edit calls in one message.",
105
- items: {
106
- type: "object",
107
- additionalProperties: false,
108
- properties: {
109
- path: pathSchema,
110
- remove_from: removeFromSchema,
111
- remove_to: removeToSchema,
112
- replacement_text: replacementTextSchema,
113
- },
114
- },
115
- },
116
- },
117
- output: {
118
- schema: { type: "string" },
119
- render: (_args, value) => [{ type: "text", text: value }],
120
- },
121
- async execute(args, exec) {
122
- return withWorkspace(execCwd(exec), async () => {
123
- const cwd = execCwd(exec);
124
- const sessionKey = execSessionKey(exec);
125
- const signal = exec.signal;
126
- const canonical = normReq(args);
127
- if (isRec(canonical) && Array.isArray(canonical.edits)) {
128
- canonical.edits = canonical.edits.map((item) => {
129
- if (!isRec(item))
130
- return item;
131
- const cloned = { ...item };
132
- normalizeFilePath(cloned);
133
- return cloned;
134
- });
135
- }
136
- assertBatchEditRequest(canonical);
137
- const sandboxPolicy = await sandbox.resolvePolicy("batch_edit", canonical, exec);
138
- const items = await prepareItems(io, canonical, cwd, signal);
139
- const groups = groupByPath(items);
140
- const processed = [];
141
- for (const groupItems of groups.values()) {
142
- abortIf(signal);
143
- processed.push(await applySequence(io, groupItems, {
144
- signal,
145
- sessionKey,
146
- }));
147
- }
148
- await commit({
149
- io,
150
- files: processed
151
- .filter((file) => file.appliedCount > 0)
152
- .map((file) => ({
153
- absolutePath: file.absolutePath,
154
- displayPath: file.displayPath,
155
- originalNormalized: file.originalNormalized,
156
- bom: file.bom,
157
- originalEnding: file.originalEnding,
158
- originalHashes: file.originalHashes,
159
- result: file.result,
160
- })),
161
- exec,
162
- sandbox,
163
- sandboxPolicy,
164
- signal,
165
- undoUnavailableMessage: () => "[E_UNDO_UNAVAILABLE] Cannot persist undo history to the hash store; the batch was NOT applied and no file was written. Retry the batch, or use write if the store cannot be recovered.",
166
- restoreUnwrittenUndos: false,
167
- });
168
- const result = buildBatchResult(processed.map(toSection));
169
- if (result.details.servedRows && result.details.servedRows.length > 0) {
170
- const byPath = result.details.servedByPath ?? [];
171
- for (const entry of byPath) {
172
- if (entry.servedRows.length === 0)
173
- continue;
174
- const file = processed.find((f) => f.displayPath === entry.path);
175
- if (file) {
176
- await recordServedTruncated(sessionKey, file.absolutePath, entry.servedRows, splitLines(file.result).length, file.range.startLine - 1);
177
- }
178
- }
179
- }
180
- return result.content[0].text;
181
- });
182
- },
183
- });
184
- }
185
- /**
186
- * Register the hashline tool on the calling agent’s scope (own layer).
187
- */
188
- export function registerBatchEditTool(_rootCtx, agentCtx, io, sandbox) {
189
- return agentCtx.tools.register(buildBatchEditTool(io, sandbox));
190
- }
package/lib/truncate.js DELETED
@@ -1,6 +0,0 @@
1
- /**
2
- * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
- * Thin shim re-exporting the read path now owned by FileView.
4
- * @module dsh-better-edit/truncate
5
- */
6
- export * from "./file-view.js";
@@ -1,5 +0,0 @@
1
- /**
2
- * @deprecated — private to SessionView seam. Use `from "./session-view.js"` instead.
3
- * @module dsh-better-edit/drift
4
- */
5
- export * from "./session-view.js";
@@ -1,5 +0,0 @@
1
- /**
2
- * @deprecated — private to SessionView seam. Use `from "./session-view.js"` instead.
3
- * @module dsh-better-edit/dsh-context
4
- */
5
- export * from "./session-view.js";
@@ -1,7 +0,0 @@
1
- /**
2
- * @deprecated — private to Mutation seam. Use `from "./mutation/engine.js"` instead.
3
- * Thin shim re-exporting the engine now owned by Mutation.
4
- * @module dsh-better-edit/edit-engine
5
- */
6
- export * from "./mutation/engine.js";
7
- export { persistUndoAndWrite, commit, commitSingle, commitBatch } from "./mutation.js";
@@ -1 +0,0 @@
1
- export declare function normReq(input: unknown): unknown;
@@ -1,6 +0,0 @@
1
- /**
2
- * @deprecated — private to Mutation seam. Use `from "./mutation.js"` instead.
3
- * Thin shim re-exporting the pipeline now owned by Mutation.
4
- * @module dsh-better-edit/edit-pipeline
5
- */
6
- export * from "./mutation.js";
@@ -1,6 +0,0 @@
1
- /**
2
- * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
- * Thin shim re-exporting the read path now owned by FileView.
4
- * @module dsh-better-edit/file-kind
5
- */
6
- export * from "./file-view.js";
@@ -1,6 +0,0 @@
1
- /**
2
- * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
- * Thin shim re-exporting the read path now owned by FileView.
4
- * @module dsh-better-edit/file-reader
5
- */
6
- export * from "./file-view.js";
@@ -1,8 +0,0 @@
1
- /**
2
- * Barrel for guidance sub-seams — parse (pure), resolve (IO-light), materialize (IO-heavy).
3
- * Re-exported via `src/guidance.ts` so existing `from "./guidance.js"` imports keep working.
4
- * @module dsh-better-edit/guidance/index
5
- */
6
- export * from "./parse.js";
7
- export * from "./resolve.js";
8
- export * from "./materialize.js";
@@ -1 +0,0 @@
1
- export * from "./hash-assign.js";
@@ -1 +0,0 @@
1
- export * from "./anchor-pipeline.js";
@@ -1 +0,0 @@
1
- export * from "./hash-assign.js";
@@ -1 +0,0 @@
1
- export * from "./anchor-pipeline.js";
@@ -1 +0,0 @@
1
- export * from "./hash-assign.js";
@@ -1 +0,0 @@
1
- export * from "./anchor-pipeline.js";
@@ -1 +0,0 @@
1
- export * from "./anchor-pipeline.js";
@@ -1,33 +0,0 @@
1
- /**
2
- * Debounced preview controller — extracted from buildToolDef so hashline seams
3
- * stay pure and the debounce invariant is testable.
4
- * @module dsh-better-edit/preview-controller
5
- */
6
- export declare const PREVIEW_DEBOUNCE_MS = 150;
7
- export interface PreviewHost {
8
- cwd: string;
9
- executionStarted: boolean;
10
- argsComplete: boolean;
11
- state: RRState;
12
- invalidate: () => void;
13
- }
14
- export type PreviewCompute = (args: unknown, cwd: string) => Promise<RPreview>;
15
- export interface RPreview {
16
- error?: string;
17
- text?: string;
18
- }
19
- export interface RRState {
20
- argsKey?: string;
21
- preview?: RPreview;
22
- previewGeneration?: number;
23
- previewTimer?: ReturnType<typeof setTimeout>;
24
- }
25
- export declare function getPreviewInput(_args: unknown): unknown;
26
- export declare class DebouncedPreview {
27
- private readonly compute;
28
- private readonly debounceMs;
29
- constructor(compute: PreviewCompute, debounceMs?: number);
30
- renderCall(host: PreviewHost, args: unknown): void;
31
- cancel(state: RRState): void;
32
- clearResult(state: RRState): void;
33
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
- * Thin shim re-exporting the read path now owned by FileView.
4
- * @module dsh-better-edit/read-render
5
- */
6
- export * from "./file-view.js";
@@ -1,25 +0,0 @@
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
- * `assertEditRequest` after `normalizeFilePath` aliasing.
7
- * @module dsh-better-edit/schema
8
- */
9
- export declare const replacementTextSchema: {
10
- readonly type: "string";
11
- readonly 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 declare const removeFromSchema: {
14
- readonly type: "string";
15
- readonly 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 declare const removeToSchema: {
18
- readonly type: "string";
19
- readonly 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 declare const pathSchema: {
22
- readonly type: "string";
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
- };
25
- export type { EditParams, BatchItemParams, BatchEditParams, } from "./contract.js";
@@ -1,5 +0,0 @@
1
- /**
2
- * @deprecated — private to SessionView seam. Use `from "./session-view.js"` instead.
3
- * @module dsh-better-edit/served-store
4
- */
5
- export * from "./session-view.js";
@@ -1,25 +0,0 @@
1
- /**
2
- * The dsh `batch_edit` tool: several hash-anchored edits in one all-or-nothing
3
- * call. Items targeting the same file are applied in order against the served
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. 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.
8
- * @module dsh-better-edit/tool-batch-edit
9
- */
10
- import type { Context } from "@deepseek-ai/cordis";
11
- import type { FileIO } from "./fs-bridge.js";
12
- import type { FsSandboxController } from "./sandbox.js";
13
- /**
14
- * Register the `batch_edit` tool on the calling agent's scope.
15
- * @param _rootCtx - host context.
16
- * @param agentCtx - the agent's scoped context (own scope layer).
17
- * @param io - the filesystem bridge.
18
- * @param sandbox - the sandbox-escalation controller.
19
- * @returns the exact disposer that unregisters the tool.
20
- */
21
- export declare function buildBatchEditTool(io: FileIO, sandbox: FsSandboxController): import("@deepseek-ai/dsh-tools").ToolDefinition;
22
- /**
23
- * Register the hashline tool on the calling agent’s scope (own layer).
24
- */
25
- export declare function registerBatchEditTool(_rootCtx: Context, agentCtx: Context, io: FileIO, sandbox: FsSandboxController): () => void;
@@ -1,6 +0,0 @@
1
- /**
2
- * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
- * Thin shim re-exporting the read path now owned by FileView.
4
- * @module dsh-better-edit/truncate
5
- */
6
- export * from "./file-view.js";
@@ -1,6 +0,0 @@
1
- /**
2
- * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
- * Thin shim re-exporting the read path now owned by FileView.
4
- * @module dsh-better-edit/validation
5
- */
6
- export * from "./file-view.js";
@@ -1,5 +0,0 @@
1
- /**
2
- * @deprecated — private to SessionView seam. Use `from "./session-view.js"` instead.
3
- * @module dsh-better-edit/workspace
4
- */
5
- export * from "./session-view.js";
package/lib/validation.js DELETED
@@ -1,6 +0,0 @@
1
- /**
2
- * @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
3
- * Thin shim re-exporting the read path now owned by FileView.
4
- * @module dsh-better-edit/validation
5
- */
6
- export * from "./file-view.js";
package/lib/workspace.js DELETED
@@ -1,5 +0,0 @@
1
- /**
2
- * @deprecated — private to SessionView seam. Use `from "./session-view.js"` instead.
3
- * @module dsh-better-edit/workspace
4
- */
5
- export * from "./session-view.js";