dsh-boost 0.1.2 → 0.1.4

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.
@@ -395,8 +395,25 @@ function ownProjectionCarries(body, anchor) {
395
395
  return false;
396
396
  const head = headLen > 0 ? points.slice(0, headLen).join('') : '';
397
397
  const tail = tailLen > 0 ? points.slice(-tailLen).join('') : '';
398
- return body.startsWith(`${match[0]}\n${head}`) && (tailLen === 0 || body.endsWith(tail));
398
+ if (!body.startsWith(`${match[0]}\n${head}`))
399
+ return false;
400
+ if (tailLen === 0)
401
+ return true;
402
+ // Our own replacement may append rescued marker lines after the tail (design
403
+ // §5.4-4), so the body does not have to *end* with the tail — it has to
404
+ // contain it at the position the header implies, followed by nothing but
405
+ // those markers. A rewritten middle still fails, because the tail must sit
406
+ // where the two windows put it.
407
+ const tailAt = body.indexOf(`\n${tail}`);
408
+ if (tailAt === -1)
409
+ return false;
410
+ const rest = body.slice(tailAt + 1 + tail.length);
411
+ if (rest === '')
412
+ return true;
413
+ return rest.startsWith('\n') && PRESERVED_SIGNAL_LINE.test(rest);
399
414
  }
415
+ /** One rescued marker line, or several of them, each on its own line. */
416
+ const PRESERVED_SIGNAL_LINE = /^\n(?:\[exit code: -?\d+\]|exit_code\s*[:=]\s*-?\d+|is_error\s*[:=]\s*(?:true|false))(?:\n(?:\[exit code: -?\d+\]|exit_code\s*[:=]\s*-?\d+|is_error\s*[:=]\s*(?:true|false)))*$/i;
400
417
  /**
401
418
  * Whether `body` is the anchor rewritten **by the host pruner**, and nothing
402
419
  * else.
@@ -433,6 +450,74 @@ function prunedCarries(body, anchor) {
433
450
  return (points.slice(0, headPoints.length).join('') === head &&
434
451
  points.slice(-tailPoints.length).join('') === tail);
435
452
  }
453
+ /**
454
+ * The locator phrase every host spill notice carries.
455
+ *
456
+ * Both outer shapers end a rewrite with a parenthesised notice naming where the
457
+ * complete result lives: `dsh-spill-policy` renders `(Omitted N bytes. Full
458
+ * formatted result stored at: …)` and `dsh-tool-fs-search` renders `(Showing N
459
+ * of M paths. Full sorted result stored at: …)`. The counts and the sentence
460
+ * around them differ by shaper and result kind; the locator phrase is the part
461
+ * both always include.
462
+ */
463
+ const SPILL_LOCATION = ' stored at: ';
464
+ /**
465
+ * Where a trailing host-shape spill footer starts, or `-1` when the body does
466
+ * not end in one.
467
+ *
468
+ * The footer is a final parenthesised block introduced by a blank line, and the
469
+ * locator phrase separates it from an ordinary parenthetical. This is a shape
470
+ * check, not authentication: the identity evidence remains the anchor's own
471
+ * head and tail, which the caller requires at the two ends of the rewritten
472
+ * body.
473
+ */
474
+ function trailingSpillFooterStart(body) {
475
+ if (!body.endsWith(')'))
476
+ return -1;
477
+ const start = body.lastIndexOf('\n\n(');
478
+ if (start === -1)
479
+ return -1;
480
+ const notice = body.slice(start + 2);
481
+ if (!notice.includes(SPILL_LOCATION))
482
+ return -1;
483
+ // The notice must also name how much was dropped. A footer carrying only a
484
+ // locator is not evidence that a shaper rewrote this receipt, and without a
485
+ // count the only remaining identity check would be "shares the first and last
486
+ // window" — the edited-then-re-read collision `prunedCarries` rejects.
487
+ return OMITTED_BYTES.test(notice) ? start : -1;
488
+ }
489
+ /** An omitted-byte notice: the only in-body evidence a shaper rewrote the middle. */
490
+ const OMITTED_BYTES = /\([^)]*\d[\d,]*\s*(?:bytes|characters)[^)]*\)/i;
491
+ /**
492
+ * Whether `core` is `anchor` rewritten by a foreign shaper.
493
+ *
494
+ * The anchor's head must sit at the very start and its tail at the very end of
495
+ * the rewritten body, and a spill footer must be the rewrite's evidence: either
496
+ * the trailing notice the caller stripped (`strippedFooter`), or one sitting
497
+ * between the two windows. A full receipt that merely shares the two probes —
498
+ * the edited-then-re-read collision `prunedCarries` exists to reject — has no
499
+ * footer, and a wrapper that embeds the anchor does not start with its head, so
500
+ * neither qualifies. The length guard rejects a body that is only the two
501
+ * windows with nothing rewritten between them.
502
+ */
503
+ function foreignShaperCarries(core, anchor, head, tail, strippedFooter) {
504
+ if (core === anchor)
505
+ return false;
506
+ if (head.length > 0 && !core.startsWith(head))
507
+ return false;
508
+ if (tail.length > 0 && !core.endsWith(tail))
509
+ return false;
510
+ if (core.length <= head.length + tail.length)
511
+ return false;
512
+ if (strippedFooter)
513
+ return true;
514
+ const middle = core.slice(head.length, core.length - tail.length);
515
+ const footerStart = middle.indexOf('\n\n(');
516
+ if (footerStart === -1)
517
+ return false;
518
+ const footerEnd = middle.indexOf(')', footerStart);
519
+ return footerEnd !== -1 && middle.slice(footerStart, footerEnd + 1).includes(SPILL_LOCATION);
520
+ }
436
521
  /**
437
522
  * Whether `body` still carries `anchor`, verbatim or as a verified in-place
438
523
  * rewrite.
@@ -443,11 +528,29 @@ function prunedCarries(body, anchor) {
443
528
  * beginning and end, so a body holding both probes *and* the rewrite's own
444
529
  * evidence is the same receipt rewritten in place.
445
530
  *
531
+ * An outer shaper (`dsh-spill-policy` for non-`read`, `dsh-tool-fs-search` for
532
+ * `glob`/`grep`) also keeps the head and tail but appends its footer after the
533
+ * tail, so the model-visible body ends with the notice. That case is checked
534
+ * twice: the trailing notice is stripped and our own projection is verified on
535
+ * the remaining body, then the remaining body is accepted as a foreign rewrite
536
+ * only when it still starts with the anchor's head and ends with its tail.
537
+ *
446
538
  * Sharing the two probes alone is not identity: a file edited in the middle and
447
539
  * re-read produces a full receipt with the same 32-character head and tail but
448
- * a different body. Accepting it would substitute against content the model no
449
- * longer holds, so a probe match must be accompanied by the pruner marker or
450
- * our projection's head line. Anything else is a lost anchor (fail closed).
540
+ * a different body.
541
+ *
542
+ * Residual, accepted: for a foreign shaper's rewrite the middle cannot be
543
+ * verified from the model-visible body — we keep neither the anchor's byte
544
+ * length nor a digest of it, and the shaper's own window (120 code points) need
545
+ * not equal our probe window. What is checked is the shape (head at index 0,
546
+ * tail at the end, a spill notice naming both a location and an omitted-byte
547
+ * count) and that the body is longer than the two windows. A re-spilled receipt
548
+ * that happens to share both probes would be accepted; rejecting every foreign
549
+ * rewrite instead would discard every spilled receipt, which is the common
550
+ * case. Fail closed only where the shape does not hold. Accepting it would substitute against content the model no
551
+ * longer holds, so a probe match must be accompanied by the pruner marker, our
552
+ * projection's head line, or a spill footer. Anything else is a lost anchor
553
+ * (fail closed).
451
554
  *
452
555
  * Probes are sliced by **code point**, exactly like `truncate`: a UTF-16 slice
453
556
  * can end on half a surrogate pair, and a lone surrogate is not a substring a
@@ -472,7 +575,17 @@ function carriesAnchor(body, anchor, probe) {
472
575
  return false;
473
576
  if (tailLen > 0 && body.indexOf(tail, start + headLen) === -1)
474
577
  return false;
475
- return prunedCarries(body, anchor) || ownProjectionCarries(body, anchor);
578
+ if (prunedCarries(body, anchor) || ownProjectionCarries(body, anchor))
579
+ return true;
580
+ // An outer shaper's footer follows the rewrite, so the model-visible body
581
+ // ends with the notice rather than with the anchor's tail. Verify the same
582
+ // body with that notice removed; without one, the in-middle footer shape is
583
+ // checked instead.
584
+ const footerStart = trailingSpillFooterStart(body);
585
+ const core = footerStart === -1 ? body : body.slice(0, footerStart);
586
+ if (footerStart !== -1 && ownProjectionCarries(core, anchor))
587
+ return true;
588
+ return foreignShaperCarries(core, anchor, head, tail, footerStart !== -1);
476
589
  }
477
590
  /**
478
591
  * Why the decision's anchored receipts are no longer trustworthy, if they are.
@@ -847,6 +960,9 @@ async function settlePendingMark(deps, sessionId) {
847
960
  // evidence the synth-time line carried, parked when that line was written.
848
961
  ...(fact?.evidence ?? {}),
849
962
  saved_input_tokens: settled.saved_input_tokens,
963
+ // Row 7: the settled line carries both estimates — the upper input figure
964
+ // and the lower whole-request figure from the plan's own request.
965
+ avoided_total_tokens: settled.avoided_total_tokens,
850
966
  cache_hit_delta: settled.cache_hit_delta,
851
967
  output_tokens: settled.output_tokens,
852
968
  });