dsh-better-edit 1.2.1 → 1.2.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
@@ -11,6 +11,13 @@ All notable changes to this project will be documented in this file.
11
11
 
12
12
  ## [Unreleased]
13
13
 
14
+ ## [1.2.2] - 2026-11-15
15
+
16
+ ### Bug Fixes
17
+
18
+ * **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)
19
+ * **read:** merge overlapping windows into their union, so every line requested by overlapping windows renders and is served once per call (#114)
20
+
14
21
  ## [1.2.1] - 2026-11-04
15
22
 
16
23
  ### 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 disjoint ranges). Sections render in the caller's
529
- * order — the order is part of the request, so it is never sorted — while every window draws on ONE
530
- * shared byte/line budget so N windows cannot multiply the auto-read budget by N. Rows are served
531
- * only for the lines actually shown, and overlapping windows collapse to one served row per line.
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 window of windows) {
541
- if (window.offset > totalLines) {
542
- sections.push(`Offset ${window.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.`);
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 endIdx = Math.min(window.offset - 1 + window.limit, totalLines);
546
- const header = windowHeader(window.offset, endIdx, totalLines);
547
- const selected = allLines.slice(window.offset - 1, endIdx);
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 window is not shown. Re-read it on its own.]`);
551
- // WHY: `metrics.truncated` must be honest when the shared budget cut a window away, so the
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: window.offset + index,
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: window.offset,
570
- endIdx,
609
+ startLine: plan.startLine,
610
+ endIdx: plan.endLine,
571
611
  totalLines,
572
612
  maxBytes: remainingBytes,
573
613
  maxTruncLines: remainingLines,
@@ -113,8 +113,12 @@ function fmtMismatchWithServes(mismatches, fileLines, fileHashes, filePath) {
113
113
  seen.add(position);
114
114
  servedRows.push({ position, hash: fileHashes[ln - 1] });
115
115
  };
116
- const notFound = mismatches.filter((m) => m.kind === "not_found");
117
- const ambiguous = mismatches.filter((m) => m.kind === "ambiguous");
116
+ const notFoundRaw = mismatches.filter((m) => m.kind === "not_found");
117
+ const ambiguousRaw = mismatches.filter((m) => m.kind === "ambiguous");
118
+ // WHY: a rejection counts and names distinct anchors, never bounds — one single-line edit
119
+ // submits one anchor as both bounds, so both slots report the same mismatch.
120
+ const notFound = [...new Map(notFoundRaw.map((m) => [m.ref.hash, m])).values()];
121
+ const ambiguous = [...new Map(ambiguousRaw.map((m) => [m.ref.hash, m])).values()];
118
122
  const refList = notFound.map((m) => `"${m.ref.hash}"`).join(", ");
119
123
  if (notFound.length > 0) {
120
124
  headlines.push(
@@ -345,7 +349,6 @@ function valEdit(edit, fileLines, fileHashes, warnings, signal) {
345
349
  // valEdit in applyEdit): when both anchors resolve, startLine <= endLine is
346
350
  // guaranteed, so no refusal arm exists here. Anchors carry no order — only
347
351
  // the resolved lines of the anchor_from/anchor_to slot pair matter.
348
- const endLine = endResolved.line;
349
352
  return {
350
353
  resolved: {
351
354
  content_lines: edit.content_lines,
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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-better-edit",
3
- "version": "1.2.1",
3
+ "version": "1.2.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",