dsh-better-edit 1.2.1 → 1.2.3
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 +14 -0
- package/lib/file-view.js +56 -16
- package/lib/hashline/anchor-pipeline.js +16 -38
- package/lib/read-and-serve.js +2 -2
- package/lib/tool-read.js +1 -1
- package/lib/types/read-and-serve.d.ts +2 -2
- package/lib/write-hook.js +10 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -11,6 +11,20 @@ All notable changes to this project will be documented in this file.
|
|
|
11
11
|
|
|
12
12
|
## [Unreleased]
|
|
13
13
|
|
|
14
|
+
## [1.2.3] - 2026-11-16
|
|
15
|
+
|
|
16
|
+
### Bug Fixes
|
|
17
|
+
|
|
18
|
+
* **hashline:** stop naming unverified anchors in `E_MALFORMED_ANCHOR` hints — a multi-line paste is refused by shape with the block redacted to `<n>-line block`, and `diagRef`'s `wUp`/`AU6` defaults are gone (#108) (#118)
|
|
19
|
+
* **write-hook:** record the `fs/observed` observation on the post-write auto-read, so an `edit` using an auto-read anchor no longer fails the host read-before-write gate with `E_BLIND_REPLACE` / `E_STALE_RANGE` — the emit is unconditional and fail-open, and the serve seam stays emission-free (ADR-0030) (#109) (#119)
|
|
20
|
+
|
|
21
|
+
## [1.2.2] - 2026-11-15
|
|
22
|
+
|
|
23
|
+
### Bug Fixes
|
|
24
|
+
|
|
25
|
+
* **edit-diagnostics:** count and name distinct anchors in a stale/ambiguous rejection instead of repeated bounds, so one anchor submitted as both bounds reports one stale anchor rather than two — port of `pi-better-edit@3b22008` (#107) (#113)
|
|
26
|
+
* **read:** merge overlapping windows into their union, so every line requested by overlapping windows renders and is served once per call (#114)
|
|
27
|
+
|
|
14
28
|
## [1.2.1] - 2026-11-04
|
|
15
29
|
|
|
16
30
|
### Bug Fixes
|
package/lib/file-view.js
CHANGED
|
@@ -486,6 +486,44 @@ function buildNormalPreview(params) {
|
|
|
486
486
|
function windowHeader(startLine, endLine, totalLines) {
|
|
487
487
|
return `=== Lines ${startLine}-${endLine} of ${totalLines} ===`;
|
|
488
488
|
}
|
|
489
|
+
/**
|
|
490
|
+
* Folds a `windows` request into the sections the preview renders: the requested line ranges
|
|
491
|
+
* merge into their union, so every requested line renders — and is served — exactly once. Each
|
|
492
|
+
* maximal contiguous span of that union is one run, carrying the smallest caller index that
|
|
493
|
+
* contributed to it; ranges that touch coalesce, since no gap exists between them to frame.
|
|
494
|
+
* A window past EOF contributes no interval and keeps its message at its caller slot, which
|
|
495
|
+
* orders sections but never splits a run. Runs stay contiguous, so a section's rows are a single
|
|
496
|
+
* slice and `startLine - 1 + index` still locates every row.
|
|
497
|
+
*/
|
|
498
|
+
function mergeWindowRuns(windows, totalLines) {
|
|
499
|
+
const intervals = [];
|
|
500
|
+
const pastEof = [];
|
|
501
|
+
windows.forEach((window, order) => {
|
|
502
|
+
if (window.offset > totalLines) {
|
|
503
|
+
pastEof.push({ kind: "pastEof", offset: window.offset, order });
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
intervals.push({
|
|
507
|
+
startLine: window.offset,
|
|
508
|
+
// WHY: the end is exclusive, and `offset + limit` can overshoot the file by thousands.
|
|
509
|
+
endLine: Math.min(window.offset - 1 + window.limit, totalLines),
|
|
510
|
+
order,
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
intervals.sort((left, right) => left.startLine - right.startLine);
|
|
514
|
+
const runs = [];
|
|
515
|
+
for (const interval of intervals) {
|
|
516
|
+
const last = runs.at(-1);
|
|
517
|
+
if (last?.kind === "run" && interval.startLine <= last.endLine + 1) {
|
|
518
|
+
last.endLine = Math.max(last.endLine, interval.endLine);
|
|
519
|
+
last.order = Math.min(last.order, interval.order);
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
runs.push({ kind: "run", ...interval });
|
|
523
|
+
}
|
|
524
|
+
// WHY: the caller's order is part of the request; `order` is unique, so this sort is total.
|
|
525
|
+
return [...runs, ...pastEof].sort((left, right) => left.order - right.order);
|
|
526
|
+
}
|
|
489
527
|
/**
|
|
490
528
|
* Renders one window through the same oversized/truncation pipeline a single-window read uses, but
|
|
491
529
|
* against the shared budget actually LEFT to this window: `maxBytes`/`maxTruncLines` are the
|
|
@@ -525,10 +563,13 @@ function buildWindowSection(params) {
|
|
|
525
563
|
};
|
|
526
564
|
}
|
|
527
565
|
/**
|
|
528
|
-
* Multi-window read (one tool result, several
|
|
529
|
-
*
|
|
530
|
-
*
|
|
531
|
-
*
|
|
566
|
+
* Multi-window read (one tool result, several requested line ranges). The ranges merge into their
|
|
567
|
+
* union first (see `mergeWindowRuns`), so a line requested by several windows renders and is
|
|
568
|
+
* served once and every header names the run it actually shows. Sections render in the order their
|
|
569
|
+
* runs first appear in the request — that order is part of the request, so it is never sorted by
|
|
570
|
+
* position — while every section draws on ONE shared byte/line budget so N windows cannot multiply
|
|
571
|
+
* the auto-read budget by N. Rows are served only for the lines actually shown; because each
|
|
572
|
+
* position belongs to exactly one run, the position record is injective by construction.
|
|
532
573
|
*/
|
|
533
574
|
function buildWindowedPreview(params) {
|
|
534
575
|
const { windows, allLines, allHashes, totalLines, maxBytes, maxTruncLines } = params;
|
|
@@ -537,18 +578,17 @@ function buildWindowedPreview(params) {
|
|
|
537
578
|
let truncation;
|
|
538
579
|
let remainingBytes = maxBytes;
|
|
539
580
|
let remainingLines = maxTruncLines;
|
|
540
|
-
for (const
|
|
541
|
-
if (
|
|
542
|
-
sections.push(`Offset ${
|
|
581
|
+
for (const plan of mergeWindowRuns(windows, totalLines)) {
|
|
582
|
+
if (plan.kind === "pastEof") {
|
|
583
|
+
sections.push(`Offset ${plan.offset} is beyond end of file (${totalLines} lines total). Use offset=1 to read from the start, or offset=${totalLines} to read the last line.`);
|
|
543
584
|
continue;
|
|
544
585
|
}
|
|
545
|
-
const
|
|
546
|
-
const
|
|
547
|
-
const
|
|
548
|
-
const selectedHashes = allHashes.slice(window.offset - 1, endIdx);
|
|
586
|
+
const header = windowHeader(plan.startLine, plan.endLine, totalLines);
|
|
587
|
+
const selected = allLines.slice(plan.startLine - 1, plan.endLine);
|
|
588
|
+
const selectedHashes = allHashes.slice(plan.startLine - 1, plan.endLine);
|
|
549
589
|
if (remainingBytes <= 0 || remainingLines <= 0) {
|
|
550
|
-
sections.push(`${header}\n[Read budget exhausted; this
|
|
551
|
-
// WHY: `metrics.truncated` must be honest when the shared budget cut a
|
|
590
|
+
sections.push(`${header}\n[Read budget exhausted; this section is not shown. Re-read it on its own.]`);
|
|
591
|
+
// WHY: `metrics.truncated` must be honest when the shared budget cut a section away, so the
|
|
552
592
|
// WHY: same function that reports truncation elsewhere derives it from the budget actually spent.
|
|
553
593
|
const skipped = truncateHead(fmtRegion(selectedHashes, selected), {
|
|
554
594
|
maxBytes: Math.max(0, remainingBytes),
|
|
@@ -559,15 +599,15 @@ function buildWindowedPreview(params) {
|
|
|
559
599
|
continue;
|
|
560
600
|
}
|
|
561
601
|
const rowSizes = selected.map((line, index) => ({
|
|
562
|
-
lineNumber:
|
|
602
|
+
lineNumber: plan.startLine + index,
|
|
563
603
|
bytes: Buffer.byteLength(`${selectedHashes[index]}${HASH_SEP}${line}`, "utf-8"),
|
|
564
604
|
}));
|
|
565
605
|
const built = buildWindowSection({
|
|
566
606
|
rowSizes,
|
|
567
607
|
selected,
|
|
568
608
|
selectedHashes,
|
|
569
|
-
startLine:
|
|
570
|
-
endIdx,
|
|
609
|
+
startLine: plan.startLine,
|
|
610
|
+
endIdx: plan.endLine,
|
|
571
611
|
totalLines,
|
|
572
612
|
maxBytes: remainingBytes,
|
|
573
613
|
maxTruncLines: remainingLines,
|
|
@@ -34,21 +34,6 @@ function diagRef(ref) {
|
|
|
34
34
|
reason: 'Use the hash alone (e.g. "aB3") — no line numbers or trailing content.',
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
|
-
if (trimmed.includes("│") && trimmed.includes("\n")) {
|
|
38
|
-
const lines = trimmed.split("\n");
|
|
39
|
-
const first = lines[0] ?? "";
|
|
40
|
-
const last = lines[lines.length - 1] ?? "";
|
|
41
|
-
const hashRe = new RegExp(HASH_CLASS);
|
|
42
|
-
const firstMatch = first.match(hashRe);
|
|
43
|
-
const lastMatch = last.match(hashRe);
|
|
44
|
-
const firstHash = firstMatch?.[0] ?? "wUp";
|
|
45
|
-
const lastHash = lastMatch?.[0] ?? "AU6";
|
|
46
|
-
const preview = first.slice(0, 60);
|
|
47
|
-
return {
|
|
48
|
-
rawAnchor: trimmed,
|
|
49
|
-
reason: `anchor_from must be a single bare 3-char hash (e.g. "wUp"), not a block with HASH│. Received ${lines.length} lines starting "${preview}…" — use only the first hash "${firstHash}" as anchor_from and "${lastHash}" as anchor_to, and put the new content (without HASH│) in replace_with.`,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
37
|
if (trimmed.includes("│")) {
|
|
53
38
|
return {
|
|
54
39
|
rawAnchor: trimmed,
|
|
@@ -113,8 +98,12 @@ function fmtMismatchWithServes(mismatches, fileLines, fileHashes, filePath) {
|
|
|
113
98
|
seen.add(position);
|
|
114
99
|
servedRows.push({ position, hash: fileHashes[ln - 1] });
|
|
115
100
|
};
|
|
116
|
-
const
|
|
117
|
-
const
|
|
101
|
+
const notFoundRaw = mismatches.filter((m) => m.kind === "not_found");
|
|
102
|
+
const ambiguousRaw = mismatches.filter((m) => m.kind === "ambiguous");
|
|
103
|
+
// WHY: a rejection counts and names distinct anchors, never bounds — one single-line edit
|
|
104
|
+
// submits one anchor as both bounds, so both slots report the same mismatch.
|
|
105
|
+
const notFound = [...new Map(notFoundRaw.map((m) => [m.ref.hash, m])).values()];
|
|
106
|
+
const ambiguous = [...new Map(ambiguousRaw.map((m) => [m.ref.hash, m])).values()];
|
|
118
107
|
const refList = notFound.map((m) => `"${m.ref.hash}"`).join(", ");
|
|
119
108
|
if (notFound.length > 0) {
|
|
120
109
|
headlines.push(
|
|
@@ -183,31 +172,21 @@ function assertItem(edit) {
|
|
|
183
172
|
}
|
|
184
173
|
}
|
|
185
174
|
const ANCHOR_ROW_RE = new RegExp(`^([+-]?)(${HASH_CLASS})│`);
|
|
186
|
-
function firstHashFromBlock(block) {
|
|
187
|
-
for (const line of block.split("\n")) {
|
|
188
|
-
const m = line.match(ANCHOR_ROW_RE);
|
|
189
|
-
if (m)
|
|
190
|
-
return m[2];
|
|
191
|
-
const bare = line.match(new RegExp(HASH_CLASS));
|
|
192
|
-
if (bare)
|
|
193
|
-
return bare[0];
|
|
194
|
-
}
|
|
195
|
-
return undefined;
|
|
196
|
-
}
|
|
197
175
|
export function resEdit(edit, _warnings) {
|
|
198
176
|
assertItem(edit);
|
|
199
177
|
const editLines = parseText(edit.replace_with);
|
|
200
178
|
const bounds = [edit.anchor_from, edit.anchor_to].map((ref) => {
|
|
201
179
|
const trimmed = ref.trim();
|
|
180
|
+
// #108: a pasted block carries no verified coordinate — refuse by SHAPE, naming no
|
|
181
|
+
// extracted hash. The old first-3-char-alnum-run guess minted a pseudo-anchor whose
|
|
182
|
+
// "use bare <hash> next time" hint steered the model into an unrelated line.
|
|
202
183
|
if (trimmed.includes("\n")) {
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
});
|
|
210
|
-
}
|
|
184
|
+
const lines = trimmed.split("\n").length;
|
|
185
|
+
throw new DomainError("E_MALFORMED_ANCHOR", {
|
|
186
|
+
// Redacted, upstream shape: never echo the pasted block back into the transcript.
|
|
187
|
+
rawAnchor: `${lines}-line block`,
|
|
188
|
+
reason: `anchor_from/anchor_to must be a single bare 3-char hash — the anchor read() served beside the one line you mean. You passed a ${lines}-line block; pass that bare hash, or re-read the file.`,
|
|
189
|
+
});
|
|
211
190
|
}
|
|
212
191
|
const match = trimmed.match(ANCHOR_ROW_RE);
|
|
213
192
|
if (match) {
|
|
@@ -307,7 +286,7 @@ function swapReversedRanges(edit, fileHashes, warnings) {
|
|
|
307
286
|
return { ...edit, hash_bounds: [endRef, startRef] };
|
|
308
287
|
}
|
|
309
288
|
/** @internal — private to anchor-pipeline seam */
|
|
310
|
-
function valEdit(edit, fileLines, fileHashes,
|
|
289
|
+
function valEdit(edit, fileLines, fileHashes, _warnings, signal) {
|
|
311
290
|
assertAligned(fileLines, fileHashes, "valEdit");
|
|
312
291
|
const mismatches = [];
|
|
313
292
|
const hashIndex = new Map();
|
|
@@ -345,7 +324,6 @@ function valEdit(edit, fileLines, fileHashes, warnings, signal) {
|
|
|
345
324
|
// valEdit in applyEdit): when both anchors resolve, startLine <= endLine is
|
|
346
325
|
// guaranteed, so no refusal arm exists here. Anchors carry no order — only
|
|
347
326
|
// the resolved lines of the anchor_from/anchor_to slot pair matter.
|
|
348
|
-
const endLine = endResolved.line;
|
|
349
327
|
return {
|
|
350
328
|
resolved: {
|
|
351
329
|
content_lines: edit.content_lines,
|
package/lib/read-and-serve.js
CHANGED
|
@@ -31,8 +31,8 @@ const SERVE_NOT_RECORDED_NOTE = "WARNING: the rows above were NOT recorded as se
|
|
|
31
31
|
* carries the UTF-8 rewrite note when the file had decode errors.
|
|
32
32
|
*
|
|
33
33
|
* Emits nothing on the fs-observation gate — callers that need the
|
|
34
|
-
* observation recorded (the `read` tool
|
|
35
|
-
* context.
|
|
34
|
+
* observation recorded (the `read` tool and the write auto-read hook) do that
|
|
35
|
+
* themselves with their exec context.
|
|
36
36
|
*/
|
|
37
37
|
export async function readAndServe(io, rawPath, cwd, options) {
|
|
38
38
|
const { sessionKey, signal } = options;
|
package/lib/tool-read.js
CHANGED
|
@@ -41,7 +41,7 @@ export function buildReadTool(io) {
|
|
|
41
41
|
// file-view's normWindows and stated in the description to stay discoverable.
|
|
42
42
|
windows: {
|
|
43
43
|
type: "array",
|
|
44
|
-
description: `Optional array of up to ${MAX_READ_WINDOWS} line windows to read in a single turn; every window's rows are served, so anchors from all of them are usable in one edit`,
|
|
44
|
+
description: `Optional array of up to ${MAX_READ_WINDOWS} line windows to read in a single turn; every window's rows are served, so anchors from all of them are usable in one edit; overlapping windows merge into the union of their line ranges (one section per merged run), while disjoint windows keep a section each`,
|
|
45
45
|
items: {
|
|
46
46
|
type: "object",
|
|
47
47
|
additionalProperties: false,
|
|
@@ -43,7 +43,7 @@ export interface ReadAndServeResult {
|
|
|
43
43
|
* carries the UTF-8 rewrite note when the file had decode errors.
|
|
44
44
|
*
|
|
45
45
|
* Emits nothing on the fs-observation gate — callers that need the
|
|
46
|
-
* observation recorded (the `read` tool
|
|
47
|
-
* context.
|
|
46
|
+
* observation recorded (the `read` tool and the write auto-read hook) do that
|
|
47
|
+
* themselves with their exec context.
|
|
48
48
|
*/
|
|
49
49
|
export declare function readAndServe(io: FileIO, rawPath: string, cwd: string, options: ReadAndServeOptions): Promise<ReadAndServeResult>;
|
package/lib/write-hook.js
CHANGED
|
@@ -103,7 +103,16 @@ export function registerWriteHook(rootCtx, agentCtx, io) {
|
|
|
103
103
|
const cwd = execCwd(exec);
|
|
104
104
|
const sessionKey = execSessionKey(exec);
|
|
105
105
|
const signal = exec.signal;
|
|
106
|
-
const { text } = await readAndServe(io, rawPath, cwd, {
|
|
106
|
+
const { text, absolutePath } = await readAndServe(io, rawPath, cwd, {
|
|
107
|
+
sessionKey,
|
|
108
|
+
signal,
|
|
109
|
+
});
|
|
110
|
+
// Record the present observation with the fs policy gate so later
|
|
111
|
+
// built-in write/edit calls see this file as observed at the version
|
|
112
|
+
// these rows describe — this block advertises every row above as an
|
|
113
|
+
// anchor, and the gate only knows a version it was told about
|
|
114
|
+
// (a no-op when no policy listens).
|
|
115
|
+
await io.emitObserved(absolutePath, exec, signal);
|
|
107
116
|
return {
|
|
108
117
|
kind: "accept",
|
|
109
118
|
content: [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-better-edit",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
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",
|