dsh-better-edit 0.8.0 → 0.8.2

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 CHANGED
@@ -1,17 +1,33 @@
1
+ ## [0.8.2](https://github.com/Rianico/dsh-better-edit/compare/v0.8.1...v0.8.2) (2026-09-14)
2
+
3
+ ### Bug Fixes
4
+
5
+ * **str-replace:** stamp the sandbox policy on shadow writes ([#72](https://github.com/Rianico/dsh-better-edit/issues/72)) ([1afaa53](https://github.com/Rianico/dsh-better-edit/commit/1afaa53bfd65dbcbebdcffb673a6c3923abc3045))
6
+
7
+ ## [0.8.1](https://github.com/Rianico/dsh-better-edit/compare/v0.8.0...v0.8.1) (2026-09-11)
8
+
9
+ ### Bug Fixes
10
+
11
+ - **str-replace:** record real fs version so view/read authorizes str_replace/insert ([#70](https://github.com/Rianico/dsh-better-edit/issues/70)) ([06b76ff](https://github.com/Rianico/dsh-better-edit/commit/06b76ffec4c0393f790d2195c06910328b611059))
12
+
13
+ ### Documentation
14
+
15
+ - add PR template and document PR requirements via git scaffolding ([#68](https://github.com/Rianico/dsh-better-edit/issues/68)) ([b341fbc](https://github.com/Rianico/dsh-better-edit/commit/b341fbc778868496bdee2e49801c5cf3745d1c99))
16
+
1
17
  ## [0.8.0](https://github.com/Rianico/dsh-better-edit/compare/v0.7.1...v0.8.0) (2026-09-09)
2
18
 
3
19
  ### Features
4
20
 
5
- * **edit:** accept object-form edits entries ({remove_from, remove_to, replacement_text}), closes [#64](https://github.com/Rianico/dsh-better-edit/issues/64) ([#66](https://github.com/Rianico/dsh-better-edit/issues/66)) ([a14ef4c](https://github.com/Rianico/dsh-better-edit/commit/a14ef4cee1c4d8126f40516624b6811d57041bf8))
6
- * **str-replace:** add governed editor shadow ([#67](https://github.com/Rianico/dsh-better-edit/issues/67)) ([a015629](https://github.com/Rianico/dsh-better-edit/commit/a0156296ce615069f10acf60e3d96c4dc3fa9e14))
21
+ - **edit:** accept object-form edits entries ({remove_from, remove_to, replacement_text}), closes [#64](https://github.com/Rianico/dsh-better-edit/issues/64) ([#66](https://github.com/Rianico/dsh-better-edit/issues/66)) ([a14ef4c](https://github.com/Rianico/dsh-better-edit/commit/a14ef4cee1c4d8126f40516624b6811d57041bf8))
22
+ - **str-replace:** add governed editor shadow ([#67](https://github.com/Rianico/dsh-better-edit/issues/67)) ([a015629](https://github.com/Rianico/dsh-better-edit/commit/a0156296ce615069f10acf60e3d96c4dc3fa9e14))
7
23
 
8
24
  ### Bug Fixes
9
25
 
10
- * **script:** correct repo inference and headless dispatch for gh-release ([b517646](https://github.com/Rianico/dsh-better-edit/commit/b5176465f8363ff80bb48e96cdfa86e6254f2ebf)), closes [#51](https://github.com/Rianico/dsh-better-edit/issues/51)
26
+ - **script:** correct repo inference and headless dispatch for gh-release ([b517646](https://github.com/Rianico/dsh-better-edit/commit/b5176465f8363ff80bb48e96cdfa86e6254f2ebf)), closes [#51](https://github.com/Rianico/dsh-better-edit/issues/51)
11
27
 
12
28
  ### Documentation
13
29
 
14
- * **agents:** sync AGENTS.md — remove tag-first releasing, fix git guide and upstream cursor ([d1088d1](https://github.com/Rianico/dsh-better-edit/commit/d1088d1f43f151f80f204fc5189268a5945066f8))
30
+ - **agents:** sync AGENTS.md — remove tag-first releasing, fix git guide and upstream cursor ([d1088d1](https://github.com/Rianico/dsh-better-edit/commit/d1088d1f43f151f80f204fc5189268a5945066f8))
15
31
 
16
32
  ## [0.7.1](https://github.com/Rianico/dsh-better-edit/compare/v0.7.0...v0.7.1) (2026-09-06)
17
33
 
package/lib/fs-bridge.js CHANGED
@@ -79,11 +79,16 @@ const UTF8_BOM_LEN = UTF8_BOM_BYTES.length;
79
79
  * https://github.com/Rianico/dsh-better-edit/issues/23
80
80
  */
81
81
  async function restoreStrippedUtf8Bom(fs, target, text, signal) {
82
- if (text.startsWith(UTF8_BOM))
83
- return text;
82
+ // BOM already preserved: one metadata stat for the read-first gate version
83
+ // (issue #69). No byte probe here — readBytes stays forbidden in this branch.
84
+ if (text.startsWith(UTF8_BOM)) {
85
+ const info = await fs.stat(target, signal).catch(() => undefined);
86
+ return { text, version: info?.version };
87
+ }
84
88
  const info = await fs.stat(target, signal);
89
+ const version = info?.version;
85
90
  if (info?.size === undefined || info.size !== Buffer.byteLength(text, "utf-8") + UTF8_BOM_LEN) {
86
- return text;
91
+ return { text, version };
87
92
  }
88
93
  const bytes = await fs.readBytes(target, signal, info.size);
89
94
  const encodedText = Buffer.from(text, "utf-8");
@@ -91,13 +96,13 @@ async function restoreStrippedUtf8Bom(fs, target, text, signal) {
91
96
  bytes[0] !== UTF8_BOM_BYTES[0] ||
92
97
  bytes[1] !== UTF8_BOM_BYTES[1] ||
93
98
  bytes[2] !== UTF8_BOM_BYTES[2]) {
94
- return text;
99
+ return { text, version };
95
100
  }
96
101
  for (let i = 0; i < encodedText.length; i += 1) {
97
102
  if (bytes[i + UTF8_BOM_LEN] !== encodedText[i])
98
- return text;
103
+ return { text, version };
99
104
  }
100
- return `${UTF8_BOM}${text}`;
105
+ return { text: `${UTF8_BOM}${text}`, version };
101
106
  }
102
107
  /** FileIO over the deployment's `ctx.fs` service. */
103
108
  export function ctxFsIO(fs, ctx) {
@@ -125,8 +130,16 @@ export function ctxFsIO(fs, ctx) {
125
130
  });
126
131
  const targetKey = String(target.targetKey ?? absolutePath);
127
132
  recordOpenState(targetKey, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
128
- if (decoded.footer)
133
+ // Gate looks up by resolved absolutePath; targetKey may differ
134
+ // (symlink/case) — mirror under both keys (issue #69 note).
135
+ if (targetKey !== absolutePath) {
136
+ recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
137
+ }
138
+ if (decoded.footer) {
129
139
  recordFooter(targetKey, decoded.footer);
140
+ if (targetKey !== absolutePath)
141
+ recordFooter(absolutePath, decoded.footer);
142
+ }
130
143
  return decoded.text;
131
144
  }
132
145
  try {
@@ -134,19 +147,24 @@ export function ctxFsIO(fs, ctx) {
134
147
  ...(signal === undefined ? {} : { signal }),
135
148
  });
136
149
  const text = await fs.readText(target, signal);
137
- const restored = await restoreStrippedUtf8Bom(fs, target, text, signal);
150
+ // Thread the restore path's stat version into the encoding memo so the
151
+ // read-first gate validates against the real version (issue #69 fix 2).
152
+ // The BOM-preserved branch stats for version only — no byte probe.
153
+ const { text: restored, version } = await restoreStrippedUtf8Bom(fs, target, text, signal);
138
154
  // record file encoding state for round-trip (BOM + lineEnding)
139
- // Do not probe fs.stat when BOM is already preserved — keep the no-probe guarantee
140
- // tested in fs-bridge.policy.test.ts (keeps a BOM already preserved without probing)
141
155
  try {
142
156
  const targetKey = String(target.targetKey ?? absolutePath);
143
157
  const hasBOM = restored.startsWith(UTF8_BOM);
144
158
  const clean = hasBOM ? restored.slice(1) : restored;
145
- // use seam to record with correct lineEnding detection; version left undefined
146
- // to avoid extra stat (the success path must stay probe-free when BOM preserved)
147
- recordOpenState(targetKey, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, undefined);
159
+ // use seam to record with correct lineEnding detection
160
+ recordOpenState(targetKey, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, version);
161
+ if (targetKey !== absolutePath) {
162
+ recordOpenState(absolutePath, clean, hasBOM ? "utf8bom" : "utf8", hasBOM, version);
163
+ }
148
164
  // clear any stale autoGuess footer on clean UTF-8 read
149
165
  _clearAutoGuessFooter(targetKey);
166
+ if (targetKey !== absolutePath)
167
+ _clearAutoGuessFooter(absolutePath);
150
168
  }
151
169
  catch {
152
170
  // best-effort
@@ -169,8 +187,14 @@ export function ctxFsIO(fs, ctx) {
169
187
  // Delegate deterministic admission to the deep seam
170
188
  const decoded = await decodeForOpen(bytes, cfg, { displayPath: absolutePath });
171
189
  recordOpenState(targetKey, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
172
- if (decoded.footer)
190
+ if (targetKey !== absolutePath) {
191
+ recordOpenState(absolutePath, decoded.text, decoded.encoding, decoded.hasBOM, info?.version);
192
+ }
193
+ if (decoded.footer) {
173
194
  recordFooter(targetKey, decoded.footer);
195
+ if (targetKey !== absolutePath)
196
+ recordFooter(absolutePath, decoded.footer);
197
+ }
174
198
  return decoded.text;
175
199
  }
176
200
  return mapFsError(error, absolutePath);
@@ -185,6 +209,8 @@ export function ctxFsIO(fs, ctx) {
185
209
  const key = String(targetTmp.targetKey ?? absolutePath);
186
210
  const info = await fs.stat(targetTmp, signal).catch(() => undefined);
187
211
  invalidateIfStale(key, info?.version);
212
+ if (key !== absolutePath)
213
+ invalidateIfStale(absolutePath, info?.version);
188
214
  }
189
215
  catch { } // biome-ignore: best-effort
190
216
  try {
package/lib/index.js CHANGED
@@ -73,7 +73,7 @@ function installAgentTools(rootCtx, agent) {
73
73
  disposers.push(agent.ctx.tools.register(hashEditDef));
74
74
  // Governed shadow of the preset's built-in str_replace_editor
75
75
  // (ADR-0015): identical contract, encoding-governed mutations.
76
- const hashStrReplaceDef = buildStrReplaceEditorTool(io);
76
+ const hashStrReplaceDef = buildStrReplaceEditorTool(io, sandbox);
77
77
  disposers.push(agent.ctx.tools.register(hashStrReplaceDef));
78
78
  disposers.push(registerUndoTool(rootCtx, agent.ctx, io, sandbox));
79
79
  disposers.push(registerWriteHook(rootCtx, agent.ctx, io));
@@ -13,6 +13,11 @@
13
13
  * - `create` defaults to UTF-8 without BOM.
14
14
  * - `undo_edit` is out of scope → `E_UNSUPPORTED` (use `undo_last_edit`).
15
15
  *
16
+ * - `create` / `str_replace` / `insert` stamp the per-call sandbox policy from
17
+ * `FsSandboxController` (the calling session's workspace root plus any
18
+ * approved one-shot escalation), matching the built-in and the hashline
19
+ * `edit` / `undo_last_edit` paths; unconfined deployments stamp `undefined`.
20
+ *
16
21
  * See ADR-0015.
17
22
  * @module dsh-better-edit/tool-str-replace-editor
18
23
  */
@@ -74,6 +79,27 @@ async function requireObserved(io, absolutePath, displayPath, signal) {
74
79
  throw argError("E_BLIND_REPLACE", `str_replace_editor: ${displayPath} has not been viewed in this session (no file encoding state at the current version). Call view first, then retry. Nothing was written.`);
75
80
  }
76
81
  }
82
+ /**
83
+ * Governed save: resolve the per-call sandbox policy — the session's standing
84
+ * mode stamped with the calling session's workspace root, or an approved
85
+ * one-shot escalation grant — and hand it to the provider write; a sandbox
86
+ * denial is remapped to the shared `[sandbox: …]` marker plus the escalation
87
+ * hint so the caller can retry once.
88
+ *
89
+ * Without it the confined backend falls back to the deployment default root,
90
+ * so a write inside the session workspace is denied under `workspace-write`
91
+ * even though the built-in `str_replace_editor` (which carries the policy)
92
+ * succeeds there. Mirrors the hashline `edit` / `undo_last_edit` path.
93
+ */
94
+ async function writeGoverned(io, sandbox, absolutePath, content, args, exec) {
95
+ const policy = await sandbox.resolvePolicy("str_replace_editor", args, exec);
96
+ try {
97
+ await io.writeText(absolutePath, content, exec.signal, exec, policy);
98
+ }
99
+ catch (error) {
100
+ throw sandbox.mapError(error, policy);
101
+ }
102
+ }
77
103
  function countOccurrences(haystack, needle) {
78
104
  if (needle.length === 0)
79
105
  return 0;
@@ -99,7 +125,7 @@ function autoGuessWarning(absolutePath, rawPath) {
99
125
  getAutoGuessFooter(absolutePath.replace(/\\/g, "/")) ??
100
126
  undefined);
101
127
  }
102
- export function buildStrReplaceEditorTool(io) {
128
+ export function buildStrReplaceEditorTool(io, sandbox) {
103
129
  return defineTool({
104
130
  name: "str_replace_editor",
105
131
  description: STR_REPLACE_EDITOR_DESCRIPTION,
@@ -134,6 +160,7 @@ export function buildStrReplaceEditorTool(io) {
134
160
  type: "string",
135
161
  description: "Full content for create",
136
162
  },
163
+ ...(sandbox.escalationModes.length > 0 ? sandbox.schemaFields() : {}),
137
164
  },
138
165
  output: {
139
166
  schema: {
@@ -201,7 +228,7 @@ export function buildStrReplaceEditorTool(io) {
201
228
  throw argError("E_FILE_EXISTS", `str_replace_editor: cannot create ${rawPath} — file already exists.`);
202
229
  }
203
230
  // Governed default: UTF-8 without BOM (no memo → no BOM).
204
- await io.writeText(absolutePath, fileText, signal, exec);
231
+ await writeGoverned(io, sandbox, absolutePath, fileText, rec, exec);
205
232
  try {
206
233
  const version = await io.statVersion(absolutePath, signal);
207
234
  recordOpenState(absolutePath, fileText, "utf8", false, version);
@@ -236,7 +263,7 @@ export function buildStrReplaceEditorTool(io) {
236
263
  throw argError("E_AMBIGUOUS_MATCH", `str_replace_editor: old_str has multiple matches (${matches}) in ${rawPath} — must match exactly once. Narrow old_str with more context. Nothing was written.`);
237
264
  }
238
265
  const next = restoreForSave(current.replace(oldStr, newStr), absolutePath);
239
- await io.writeText(absolutePath, next, signal, exec);
266
+ await writeGoverned(io, sandbox, absolutePath, next, rec, exec);
240
267
  return { text: `Replaced 1 occurrence in ${rawPath}.` };
241
268
  }
242
269
  // insert
@@ -262,13 +289,13 @@ export function buildStrReplaceEditorTool(io) {
262
289
  if (endsWithNL && out2.length > 0)
263
290
  out2 += "\n";
264
291
  const next = restoreForSave(out2, absolutePath);
265
- await io.writeText(absolutePath, next, signal, exec);
292
+ await writeGoverned(io, sandbox, absolutePath, next, rec, exec);
266
293
  return { text: `Inserted ${added.length} line(s) at line ${insertAt} in ${rawPath}.` };
267
294
  });
268
295
  },
269
296
  });
270
297
  }
271
298
  /** Register the shadow `str_replace_editor` on the calling agent's scope (own layer). */
272
- export function registerStrReplaceEditorTool(agentCtx, io) {
273
- return agentCtx.tools.register(buildStrReplaceEditorTool(io));
299
+ export function registerStrReplaceEditorTool(agentCtx, io, sandbox) {
300
+ return agentCtx.tools.register(buildStrReplaceEditorTool(io, sandbox));
274
301
  }
@@ -13,15 +13,21 @@
13
13
  * - `create` defaults to UTF-8 without BOM.
14
14
  * - `undo_edit` is out of scope → `E_UNSUPPORTED` (use `undo_last_edit`).
15
15
  *
16
+ * - `create` / `str_replace` / `insert` stamp the per-call sandbox policy from
17
+ * `FsSandboxController` (the calling session's workspace root plus any
18
+ * approved one-shot escalation), matching the built-in and the hashline
19
+ * `edit` / `undo_last_edit` paths; unconfined deployments stamp `undefined`.
20
+ *
16
21
  * See ADR-0015.
17
22
  * @module dsh-better-edit/tool-str-replace-editor
18
23
  */
24
+ import type { FsSandboxController } from "./sandbox.js";
19
25
  import type { FileIO } from "./fs-bridge.js";
20
26
  export declare const STR_REPLACE_EDITOR_DESCRIPTION: string;
21
- export declare function buildStrReplaceEditorTool(io: FileIO): import("@deepseek-ai/dsh-tools").ToolDefinition;
27
+ export declare function buildStrReplaceEditorTool(io: FileIO, sandbox: FsSandboxController): import("@deepseek-ai/dsh-tools").ToolDefinition;
22
28
  /** Register the shadow `str_replace_editor` on the calling agent's scope (own layer). */
23
29
  export declare function registerStrReplaceEditorTool(agentCtx: {
24
30
  tools: {
25
31
  register(def: unknown): () => void;
26
32
  };
27
- }, io: FileIO): () => void;
33
+ }, io: FileIO, sandbox: FsSandboxController): () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-better-edit",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
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.",
5
5
  "keywords": [
6
6
  "anchors",