dsh-boost 0.1.0 → 0.1.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.
Files changed (54) hide show
  1. package/README.en.md +184 -70
  2. package/README.md +148 -59
  3. package/dist/gate/decide.js +33 -2
  4. package/dist/gate/decide.js.map +1 -1
  5. package/dist/hooks/llm-stream.js +242 -89
  6. package/dist/hooks/llm-stream.js.map +1 -1
  7. package/dist/hooks/post-execute.js +282 -25
  8. package/dist/hooks/post-execute.js.map +1 -1
  9. package/dist/hooks/session-state.js +18 -8
  10. package/dist/hooks/session-state.js.map +1 -1
  11. package/dist/host/root.js +22 -1
  12. package/dist/host/root.js.map +1 -1
  13. package/dist/index.js +494 -72
  14. package/dist/index.js.map +1 -1
  15. package/dist/scope/doc.js.map +1 -1
  16. package/dist/scope/store.js +24 -9
  17. package/dist/scope/store.js.map +1 -1
  18. package/dist/tools/boost.js +37 -4
  19. package/dist/tools/boost.js.map +1 -1
  20. package/dist/types.d.ts +18 -1
  21. package/dist/types.js.map +1 -1
  22. package/package.json +10 -6
  23. package/dist/admission/fixed-cost.d.ts +0 -23
  24. package/dist/admission/ledger.d.ts +0 -113
  25. package/dist/admission/retrieval.d.ts +0 -65
  26. package/dist/admission/usage.d.ts +0 -75
  27. package/dist/config.d.ts +0 -43
  28. package/dist/gate/binding.d.ts +0 -113
  29. package/dist/gate/decide.d.ts +0 -82
  30. package/dist/gate/guard.d.ts +0 -69
  31. package/dist/gate/invalidate.d.ts +0 -112
  32. package/dist/gate/plan.d.ts +0 -86
  33. package/dist/hooks/clock.d.ts +0 -68
  34. package/dist/hooks/execute.d.ts +0 -79
  35. package/dist/hooks/llm-stream.d.ts +0 -276
  36. package/dist/hooks/pending.d.ts +0 -89
  37. package/dist/hooks/post-execute.d.ts +0 -247
  38. package/dist/hooks/session-state.d.ts +0 -158
  39. package/dist/hooks/synthesize.d.ts +0 -74
  40. package/dist/host/loader.d.ts +0 -23
  41. package/dist/host/platform.d.ts +0 -18
  42. package/dist/host/root.d.ts +0 -18
  43. package/dist/projection/apply.d.ts +0 -174
  44. package/dist/projection/canonicalise.d.ts +0 -43
  45. package/dist/projection/counters.d.ts +0 -65
  46. package/dist/projection/truncate.d.ts +0 -47
  47. package/dist/prompt/protocol.d.ts +0 -45
  48. package/dist/receipts/store.d.ts +0 -198
  49. package/dist/scope/doc.d.ts +0 -117
  50. package/dist/scope/resolve.d.ts +0 -91
  51. package/dist/scope/store.d.ts +0 -135
  52. package/dist/selector/jev.d.ts +0 -93
  53. package/dist/tools/boost.d.ts +0 -367
  54. package/dist/tools/family.d.ts +0 -26
@@ -1,7 +1,7 @@
1
1
  import { StepBudget } from '../admission/ledger.js';
2
2
  import { UsageLedger } from '../admission/usage.js';
3
3
  import { DEFAULT_CONFIG } from '../config.js';
4
- import { looksPruned } from '../gate/invalidate.js';
4
+ import { looksPruned, PRUNE_MARKER } from '../gate/invalidate.js';
5
5
  import { parsePlan } from '../gate/plan.js';
6
6
  import { TurnClock } from './clock.js';
7
7
  import { synthesizeTurn } from './synthesize.js';
@@ -109,29 +109,36 @@ function sessionIdOf(options) {
109
109
  return typeof id === 'string' && id.length > 0 ? id : undefined;
110
110
  }
111
111
  /**
112
- * Which session has been lent the shared fallback runtime.
112
+ * Which session has been lent each shared fallback instance.
113
113
  *
114
114
  * `apply` always passes `sessions`, so this exists for embedders and tests that
115
115
  * hand in one `clock`/`budget`/`usage` instead. Those three carry per-session
116
116
  * state — a turn's step counter, a pending usage mark — so lending one to a
117
117
  * second session attributes B's tokens to A's candidate. That surfaces as a
118
118
  * saving that never happened, which is the one error this plugin must not
119
- * produce. The first session to arrive keeps the loan; every other session gets
120
- * `undefined`, which each caller already reads as "no substitution" (fail
121
- * closed), and the warning is emitted once per deps object rather than per call.
119
+ * produce. Ownership is keyed by the **instance**, not by the deps object: two
120
+ * assemblies that share the same three objects (the pattern the fallback exists
121
+ * for) must agree on the one session they serve, or each claims its own loan
122
+ * and both borrow. The first session to arrive keeps the loan; every other
123
+ * session gets `undefined`, which each caller already reads as "no
124
+ * substitution" (fail closed), and the warning is emitted once per deps object
125
+ * rather than per call. `clearSession` drops the loans its session holds, so a
126
+ * disposed owner does not lock the instance for the rest of the process.
122
127
  */
123
- const fallbackOwner = new WeakMap();
124
- /** Whether the shared fallback runtime may serve this session. */
125
- function mayBorrowShared(deps, sessionId) {
126
- const held = fallbackOwner.get(deps);
128
+ const fallbackLoans = new WeakMap();
129
+ /** Deps objects already warned about a refused loan (once each, not per call). */
130
+ const fallbackWarned = new WeakSet();
131
+ /** Whether the shared fallback instance may serve this session. */
132
+ function mayBorrowShared(instance, deps, sessionId) {
133
+ const held = fallbackLoans.get(instance);
127
134
  if (held === undefined) {
128
- fallbackOwner.set(deps, { sessionId, warned: false });
135
+ fallbackLoans.set(instance, { sessionId });
129
136
  return true;
130
137
  }
131
138
  if (held.sessionId === sessionId)
132
139
  return true;
133
- if (!held.warned) {
134
- held.warned = true;
140
+ if (!fallbackWarned.has(deps)) {
141
+ fallbackWarned.add(deps);
135
142
  try {
136
143
  deps.logger?.warn(`dsh-boost: no per-session runtime is wired, and session ${sessionId} is not the one already using the shared clock/budget/usage; substitution stays off for it rather than mixing two sessions' measurements`);
137
144
  }
@@ -141,6 +148,15 @@ function mayBorrowShared(deps, sessionId) {
141
148
  }
142
149
  return false;
143
150
  }
151
+ /** Release the shared-instance loans the disposing session holds. */
152
+ function releaseSharedLoans(deps, sessionId) {
153
+ for (const instance of [deps.clock, deps.budget, deps.usage]) {
154
+ if (instance === undefined)
155
+ continue;
156
+ if (fallbackLoans.get(instance)?.sessionId === sessionId)
157
+ fallbackLoans.delete(instance);
158
+ }
159
+ }
144
160
  /**
145
161
  * The session's clock: the per-session runtime when wired, the shared instance
146
162
  * otherwise — but only for the one session that instance belongs to.
@@ -158,7 +174,9 @@ export function sessionClock(deps, sessionId) {
158
174
  catch {
159
175
  // Fall through to the shared instance, subject to the ownership rule.
160
176
  }
161
- return deps.clock !== undefined && mayBorrowShared(deps, sessionId) ? deps.clock : undefined;
177
+ return deps.clock !== undefined && mayBorrowShared(deps.clock, deps, sessionId)
178
+ ? deps.clock
179
+ : undefined;
162
180
  }
163
181
  /** The session's turn budget; the same precedence as {@link sessionClock}. */
164
182
  export function sessionBudget(deps, sessionId) {
@@ -170,7 +188,23 @@ export function sessionBudget(deps, sessionId) {
170
188
  catch {
171
189
  // Fall through to the shared instance, subject to the ownership rule.
172
190
  }
173
- return deps.budget !== undefined && mayBorrowShared(deps, sessionId) ? deps.budget : undefined;
191
+ return deps.budget !== undefined && mayBorrowShared(deps.budget, deps, sessionId)
192
+ ? deps.budget
193
+ : undefined;
194
+ }
195
+ /**
196
+ * Whether a substitution budget is wired at all.
197
+ *
198
+ * `undefined` from {@link sessionBudget} has two meanings and callers must not
199
+ * conflate them (row 21): with neither `deps.budget` nor a per-session runtime,
200
+ * the embedder never asked for a cap, so the substitution is unconstrained; a
201
+ * wired budget that this session may not borrow is the opposite — the shared
202
+ * instance belongs to another session and the step cannot be paid. Treating the
203
+ * refusal as "no cap" let an unbudgeted substitution through without ever
204
+ * incrementing the live budget.
205
+ */
206
+ export function budgetWired(deps) {
207
+ return deps.budget !== undefined || deps.sessions !== undefined;
174
208
  }
175
209
  /** The session's usage ledger; the same precedence as {@link sessionClock}. */
176
210
  export function sessionUsage(deps, sessionId) {
@@ -182,15 +216,30 @@ export function sessionUsage(deps, sessionId) {
182
216
  catch {
183
217
  // Fall through to the shared instance, subject to the ownership rule.
184
218
  }
185
- return deps.usage !== undefined && mayBorrowShared(deps, sessionId) ? deps.usage : undefined;
219
+ return deps.usage !== undefined && mayBorrowShared(deps.usage, deps, sessionId)
220
+ ? deps.usage
221
+ : undefined;
186
222
  }
187
- /** The session's anchors without ever failing the request. */
223
+ /**
224
+ * The session's anchors, distinguishing a record from a cold store.
225
+ *
226
+ * A substitution is only legitimate against the receipt the model still holds,
227
+ * so every missing answer is a lost anchor (row 28). `post-execute` writes
228
+ * anchors with every commit, so a live queue entry with no record means that
229
+ * write failed; synthesizing then would skip the only verification the design
230
+ * has. A throwing read is equally unverifiable. Both fail closed through the
231
+ * ordinary reset path instead of substituting on unseen evidence.
232
+ */
188
233
  function anchorsOf(deps, sessionId) {
234
+ const store = deps.anchors;
235
+ if (store == null || typeof store.get !== 'function')
236
+ return { kind: 'unreadable' };
189
237
  try {
190
- return deps.anchors?.get(sessionId);
238
+ const anchors = store.get(sessionId);
239
+ return anchors == null ? { kind: 'cold' } : { kind: 'record', anchors };
191
240
  }
192
241
  catch {
193
- return undefined;
242
+ return { kind: 'unreadable' };
194
243
  }
195
244
  }
196
245
  /**
@@ -348,6 +397,42 @@ function ownProjectionCarries(body, anchor) {
348
397
  const tail = tailLen > 0 ? points.slice(-tailLen).join('') : '';
349
398
  return body.startsWith(`${match[0]}\n${head}`) && (tailLen === 0 || body.endsWith(tail));
350
399
  }
400
+ /**
401
+ * Whether `body` is the anchor rewritten **by the host pruner**, and nothing
402
+ * else.
403
+ *
404
+ * `looksPruned` alone is not identity: any body that embeds the anchor's pruned
405
+ * text — a `read` of a session log, the case `invalidate` documents as
406
+ * occurring — carries the marker too, so a receipt that was compacted away
407
+ * could be declared "still carried" and a decision would survive on evidence
408
+ * the model no longer holds.
409
+ *
410
+ * The pruner replaces the middle with a single marker and keeps a head and a
411
+ * tail, so a genuine carrier is `head + marker + tail` with nothing before the
412
+ * head and nothing after the tail. Requiring the carrier to *start* with the
413
+ * anchor's first `head` code points and *end* with its last `tail` ones — the
414
+ * same windows `ownProjectionCarries` derives for our own rewrite — rejects the
415
+ * wrapper: its head is the wrapper's opening line, not the anchor's.
416
+ */
417
+ function prunedCarries(body, anchor) {
418
+ const marker = body.indexOf(PRUNE_MARKER);
419
+ if (marker === -1)
420
+ return false;
421
+ // Exactly one marker: two would mean this is not one pruned receipt.
422
+ if (body.indexOf(PRUNE_MARKER, marker + PRUNE_MARKER.length) !== -1)
423
+ return false;
424
+ const head = body.slice(0, marker);
425
+ const tail = body.slice(marker + PRUNE_MARKER.length);
426
+ if (head.length === 0 || tail.length === 0)
427
+ return false;
428
+ const points = Array.from(anchor);
429
+ const headPoints = Array.from(head);
430
+ const tailPoints = Array.from(tail);
431
+ if (headPoints.length + tailPoints.length >= points.length)
432
+ return false;
433
+ return (points.slice(0, headPoints.length).join('') === head &&
434
+ points.slice(-tailPoints.length).join('') === tail);
435
+ }
351
436
  /**
352
437
  * Whether `body` still carries `anchor`, verbatim or as a verified in-place
353
438
  * rewrite.
@@ -387,7 +472,7 @@ function carriesAnchor(body, anchor, probe) {
387
472
  return false;
388
473
  if (tailLen > 0 && body.indexOf(tail, start + headLen) === -1)
389
474
  return false;
390
- return looksPruned(body) || ownProjectionCarries(body, anchor);
475
+ return prunedCarries(body, anchor) || ownProjectionCarries(body, anchor);
391
476
  }
392
477
  /**
393
478
  * Why the decision's anchored receipts are no longer trustworthy, if they are.
@@ -396,24 +481,30 @@ function carriesAnchor(body, anchor, probe) {
396
481
  * pruner literal means `pruned-reset`, because the pruner is the common case
397
482
  * and it leaves the receipt in place. Only when no anchor was pruned does a
398
483
  * missing carrier mean `compaction-reset` (the history was replaced by a
399
- * summary). No anchors at all is vacuously intact: `post-execute` always writes
400
- * them with a commit, but a missing evidence store must not block substitution
401
- * forever.
402
- *
403
- * An empty anchor cannot be located in any message and is skipped; `invalidate`
404
- * treats an empty anchor as a real value, but the stream hook has no receipt
405
- * body to compare it with, and refusing every substitution on one empty tool
406
- * result would be worse than the uncertainty.
484
+ * summary).
485
+ *
486
+ * A missing record, a record with no entries and a record whose shape cannot be
487
+ * read are all unverifiable and therefore `compaction-reset` (row 28): the
488
+ * commit that parked the decision writes anchors, so their absence is a failed
489
+ * write, not a reason to substitute with zero verification. An empty anchor
490
+ * string is a real value the check skips; `invalidate` treats an empty anchor
491
+ * as a real value, and the stream hook has no receipt body to compare it with.
407
492
  */
408
493
  function anchorResetOf(anchors, messages, probe) {
409
494
  if (anchors == null)
410
- return undefined;
411
- const record = anchors.anchors;
495
+ return 'compaction-reset';
496
+ let record;
497
+ try {
498
+ record = anchors.anchors;
499
+ }
500
+ catch {
501
+ return 'compaction-reset';
502
+ }
412
503
  if (typeof record !== 'object' || record === null)
413
- return undefined;
504
+ return 'compaction-reset';
414
505
  const values = Object.values(record);
415
506
  if (values.length === 0)
416
- return undefined;
507
+ return 'compaction-reset';
417
508
  const texts = values.filter((text) => typeof text === 'string' && text.length > 0);
418
509
  if (texts.length === 0) {
419
510
  // An empty anchor is a real value the check skips (it cannot be located in
@@ -437,6 +528,15 @@ function anchorResetOf(anchors, messages, probe) {
437
528
  }
438
529
  return compacted ? 'compaction-reset' : undefined;
439
530
  }
531
+ /** The anchored step id a reset receipt names, never throwing on a foreign shape. */
532
+ function anchorStepIdOf(anchors) {
533
+ try {
534
+ return typeof anchors.stepId === 'string' ? anchors.stepId : 'root';
535
+ }
536
+ catch {
537
+ return 'root';
538
+ }
539
+ }
440
540
  /**
441
541
  * Per-session synthesis history, keyed by `HookDeps` first so two plugin
442
542
  * instances in one process cannot read each other's session. It holds one turn
@@ -496,8 +596,10 @@ function synthesisBlocked(deps, sessionId, entry, turn) {
496
596
  if (current !== undefined && current.callIds.has(entry.callId))
497
597
  return true;
498
598
  const budget = sessionBudget(deps, sessionId);
599
+ // Row 21: a wired budget that was not lent to this session is unpayable, not
600
+ // "no cap". Only a completely unwired budget leaves the step unconstrained.
499
601
  if (budget === undefined)
500
- return false;
602
+ return budgetWired(deps);
501
603
  let spent;
502
604
  try {
503
605
  spent = budget.count();
@@ -536,14 +638,6 @@ function resolveStreamPlan(deps, options) {
536
638
  const entry = peekPending(deps, sessionId);
537
639
  if (entry === undefined)
538
640
  return { kind: 'pass', sessionId };
539
- const turn = clockTurnOf(deps, sessionId);
540
- if (turn === undefined || !sameTurn(entry.turn, turn)) {
541
- return {
542
- kind: 'pass',
543
- sessionId,
544
- abandon: { entry, clearQueue: true, clearAnchors: false },
545
- };
546
- }
547
641
  // A blank call id cannot be dispatched and must not be "repaired" here: the
548
642
  // execute-side tamper check would compare a different id and reject silently.
549
643
  if (typeof entry.callId !== 'string' || entry.callId.trim().length === 0) {
@@ -553,22 +647,29 @@ function resolveStreamPlan(deps, options) {
553
647
  abandon: { entry, clearQueue: true, clearAnchors: false },
554
648
  };
555
649
  }
556
- const anchors = anchorsOf(deps, sessionId);
557
- const reset = anchorResetOf(anchors, options?.messages, anchorProbe(deps));
650
+ const turn = clockTurnOf(deps, sessionId);
651
+ // The reset receipt pairs with the entry's own turn when the clock cannot
652
+ // name a real one; a turn-0 commit still wrote (or failed to write) anchors.
653
+ const resetTurn = turn ?? turnNumberOf(entry.turn);
654
+ // Row 28: anchor evidence is checked before turn validity. A decision whose
655
+ // anchors were never written (the commit's `set` failed), a store that holds
656
+ // no record for a live entry, and a throwing read are all unverifiable;
657
+ // dropping them as an ordinary stale entry would hide the lost evidence.
658
+ // Intact anchors on a stale turn still take the silent abandon below: there
659
+ // the decision is dead by identity, not because its evidence vanished.
660
+ const anchorRead = anchorsOf(deps, sessionId);
661
+ if (anchorRead.kind !== 'record') {
662
+ return resetPlan(sessionId, entry, 'compaction-reset', 'root', resetTurn);
663
+ }
664
+ const reset = anchorResetOf(anchorRead.anchors, options?.messages, anchorProbe(deps));
558
665
  if (reset !== undefined) {
666
+ return resetPlan(sessionId, entry, reset, anchorStepIdOf(anchorRead.anchors), resetTurn);
667
+ }
668
+ if (turn === undefined || !sameTurn(entry.turn, turn)) {
559
669
  return {
560
670
  kind: 'pass',
561
671
  sessionId,
562
- abandon: {
563
- entry,
564
- clearQueue: true,
565
- clearAnchors: true,
566
- reset: {
567
- code: reset,
568
- stepId: typeof anchors?.stepId === 'string' ? anchors.stepId : 'root',
569
- turn: turn,
570
- },
571
- },
672
+ abandon: { entry, clearQueue: true, clearAnchors: false },
572
673
  };
573
674
  }
574
675
  // Re-check the step immediately before synthesizing. The turn/step checks
@@ -586,6 +687,24 @@ function resolveStreamPlan(deps, options) {
586
687
  }
587
688
  return { kind: 'synthesize', sessionId, entry };
588
689
  }
690
+ /**
691
+ * Abandon a decision whose anchored evidence cannot be verified.
692
+ *
693
+ * The reset receipt explains the lost anchor; the queue and anchors go with it,
694
+ * and the usage mark is settled as discarded by `applyAbandon`.
695
+ */
696
+ function resetPlan(sessionId, entry, code, stepId, turn) {
697
+ return {
698
+ kind: 'pass',
699
+ sessionId,
700
+ abandon: {
701
+ entry,
702
+ clearQueue: true,
703
+ clearAnchors: true,
704
+ reset: { code, stepId, turn },
705
+ },
706
+ };
707
+ }
589
708
  /**
590
709
  * A pass plan that abandons the session's pending entry, when one exists.
591
710
  *
@@ -629,30 +748,28 @@ export function settleDiscarded(deps, sessionId) {
629
748
  /**
630
749
  * Plan/guards evidence parked between synthesis and settlement.
631
750
  *
632
- * The settled receipt is written at the first real request after the
633
- * synthesized turn, when the queue entry is already gone; the synth-time
634
- * receipt has it from the entry, this park hands it to the settle. Keyed by
635
- * `HookDeps` first so two plugin instances in one process cannot read each
636
- * other's session, and a `WeakMap` lets the whole map die with the assembly.
751
+ * Keyed by `HookDeps` first so two plugin instances in one process cannot read
752
+ * each other's session, and a `WeakMap` lets the whole map die with the
753
+ * assembly.
637
754
  */
638
755
  const PENDING_EVIDENCE = new WeakMap();
639
- /** Park the evidence for the session's next settlement, replacing any earlier. */
640
- function rememberEvidence(deps, sessionId, evidence) {
756
+ /** Park the fact for the session's next settlement, replacing any earlier. */
757
+ function rememberEvidence(deps, sessionId, fact) {
641
758
  let bySession = PENDING_EVIDENCE.get(deps);
642
759
  if (bySession === undefined) {
643
760
  bySession = new Map();
644
761
  PENDING_EVIDENCE.set(deps, bySession);
645
762
  }
646
- bySession.set(sessionId, evidence);
763
+ bySession.set(sessionId, fact);
647
764
  }
648
- /** Take (and forget) the evidence one settlement consumes. */
765
+ /** Take (and forget) the fact one settlement consumes. */
649
766
  function takeEvidence(deps, sessionId) {
650
767
  const bySession = PENDING_EVIDENCE.get(deps);
651
- const evidence = bySession?.get(sessionId);
768
+ const fact = bySession?.get(sessionId);
652
769
  bySession?.delete(sessionId);
653
- return evidence;
770
+ return fact;
654
771
  }
655
- /** Drop evidence that no settlement will ever consume. */
772
+ /** Drop a fact that no settlement will ever consume. */
656
773
  function forgetEvidence(deps, sessionId) {
657
774
  PENDING_EVIDENCE.get(deps)?.delete(sessionId);
658
775
  }
@@ -701,26 +818,34 @@ async function settlePendingMark(deps, sessionId) {
701
818
  let settled;
702
819
  try {
703
820
  const pending = ledger.pendingCandidateId();
704
- if (typeof pending !== 'string' || pending.length === 0)
821
+ if (typeof pending !== 'string' || pending.length === 0) {
822
+ // A parked fact without a mark can never be settled; drop it rather than
823
+ // let a later, unrelated mark pick up this turn's evidence.
824
+ forgetEvidence(deps, sessionId);
705
825
  return;
826
+ }
706
827
  candidateId = pending;
707
828
  settled = ledger.settle();
708
829
  }
709
830
  catch {
831
+ forgetEvidence(deps, sessionId);
710
832
  return;
711
833
  }
834
+ const fact = takeEvidence(deps, sessionId);
712
835
  await appendReceipt(deps, {
713
836
  version: 1,
714
837
  ts: new Date().toISOString(),
715
838
  sessionId,
716
- turn: clockTurnOf(deps, sessionId) ?? 0,
839
+ // Row 26: the synthesized turn is the substitution's turn; the request that
840
+ // happens to settle it may already be the next one.
841
+ turn: fact?.turn ?? clockTurnOf(deps, sessionId) ?? 0,
717
842
  stepId: 'root',
718
843
  kind: 'substitute-settled',
719
844
  code: 'substitute-settled',
720
845
  candidateId,
721
846
  // G2 applies to the settled line too: the goal and guards are the same
722
847
  // evidence the synth-time line carried, parked when that line was written.
723
- ...(takeEvidence(deps, sessionId) ?? {}),
848
+ ...(fact?.evidence ?? {}),
724
849
  saved_input_tokens: settled.saved_input_tokens,
725
850
  cache_hit_delta: settled.cache_hit_delta,
726
851
  output_tokens: settled.output_tokens,
@@ -804,27 +929,35 @@ async function applyAbandon(deps, sessionId, abandon) {
804
929
  * `next()` has already been called by the hook body, so the upstream iterable
805
930
  * exists before the generator starts. Usage is mapped and a pending mark is
806
931
  * settled at the `usage` chunk (which the host emits before `finish`); text is
807
- * accumulated for `parsePlan`. A consumer that cancels the stream never reaches
808
- * the plan store, because the code after the loop only runs on normal
809
- * completion.
932
+ * accumulated for `parsePlan`. Settlement also runs in `finally`: an upstream
933
+ * that throws before the `usage` chunk (a provider 500 mid-stream) or a
934
+ * consumer that cancels the stream still ends the request, and a mark left
935
+ * pending there would pair with the next, unrelated request and report that
936
+ * request's tokens as a saving. An errored stream has no usable sample, so
937
+ * `settle()` reports `'unknown'`. The plan store is still reached only on
938
+ * normal completion, so a partial response never becomes a plan.
810
939
  */
811
940
  async function* observeBranch(deps, sessionId, upstream) {
812
941
  let text = '';
813
- for await (const chunk of upstream) {
814
- if (chunk?.type === 'text-delta' && typeof chunk.text === 'string')
815
- text += chunk.text;
816
- if (chunk?.type === 'usage' && sessionId !== undefined) {
817
- recordUsage(deps, sessionId, chunk.usage);
818
- await settlePendingMark(deps, sessionId);
942
+ try {
943
+ for await (const chunk of upstream) {
944
+ if (chunk?.type === 'text-delta' && typeof chunk.text === 'string')
945
+ text += chunk.text;
946
+ if (chunk?.type === 'usage' && sessionId !== undefined) {
947
+ recordUsage(deps, sessionId, chunk.usage);
948
+ await settlePendingMark(deps, sessionId);
949
+ }
950
+ yield chunk;
819
951
  }
820
- yield chunk;
821
- }
822
- // The host emits `usage` before `finish`, so this is normally a no-op; a
823
- // route that omits the chunk still ends the request, and the mark must be
824
- // consumed there rather than attributed to a later request (`'unknown'`,
825
- // which status does not count).
826
- if (sessionId !== undefined)
827
- await settlePendingMark(deps, sessionId);
952
+ }
953
+ finally {
954
+ // The host emits `usage` before `finish`, so this is normally a no-op; a
955
+ // route that omits the chunk still ends the request, and the mark must be
956
+ // consumed there rather than attributed to a later request (`'unknown'`,
957
+ // which status does not count).
958
+ if (sessionId !== undefined)
959
+ await settlePendingMark(deps, sessionId);
960
+ }
828
961
  rememberPlan(deps, sessionId, text);
829
962
  }
830
963
  /**
@@ -852,11 +985,19 @@ async function* synthesizeBranch(deps, sessionId, entry, upstream) {
852
985
  return;
853
986
  }
854
987
  markSubstitutedFor(deps, sessionId, current.candidateId);
855
- // Park the plan evidence for the settled line, which runs at the next real
856
- // request after this entry has left the queue.
988
+ // Park the plan evidence and the synthesis turn for the settled line, which
989
+ // runs at the next real request after this entry has left the queue.
857
990
  const evidence = current.evidence;
858
- if (evidence !== undefined)
859
- rememberEvidence(deps, sessionId, evidence);
991
+ const turn = turnNumberOf(current.turn);
992
+ rememberEvidence(deps, sessionId, {
993
+ turn,
994
+ ...(evidence === undefined ? {} : { evidence }),
995
+ });
996
+ // Row 27: mirror the synthesis fact now, not when `tools/execute` accepts the
997
+ // dispatch. A call denied by `tools/pre-execute` never reaches that hook, and
998
+ // a rollover must still report the model round this turn skipped. The mirror
999
+ // only feeds the settle path; it never claims a measured saving.
1000
+ deps.noteSynthesis?.(sessionId, turn, current.candidateId, evidence);
860
1001
  await appendSubstituteReceipt(deps, sessionId, current);
861
1002
  yield* synthesizeTurn(current);
862
1003
  }
@@ -976,7 +1117,8 @@ export function streamHook(deps) {
976
1117
  * `clear`, but it does not own the queue, the anchors, the plan map or the
977
1118
  * per-session runtime, so the caller would otherwise have to know four separate
978
1119
  * clears. A pending usage mark is settled first so a mark cannot outlive the
979
- * session it belongs to.
1120
+ * session it belongs to, and the shared-instance loans this session holds are
1121
+ * released so a disposed owner does not refuse every later session forever.
980
1122
  */
981
1123
  export function clearSession(deps, sessionId) {
982
1124
  settleDiscarded(deps, sessionId);
@@ -1004,5 +1146,16 @@ export function clearSession(deps, sessionId) {
1004
1146
  catch {
1005
1147
  // Same.
1006
1148
  }
1149
+ releaseSharedLoans(deps, sessionId);
1150
+ // The rollover path mirrors an accepted dispatch so a late tool result can
1151
+ // still settle a substitution that ran. A disposed session has no late
1152
+ // result coming, so the mirror would otherwise sit in the per-deps map for
1153
+ // the plugin's lifetime — one entry per disposed session.
1154
+ try {
1155
+ deps.clearAcceptedDispatch?.(sessionId);
1156
+ }
1157
+ catch {
1158
+ // A disposal hook that throws must not abort the rest of the teardown.
1159
+ }
1007
1160
  }
1008
1161
  //# sourceMappingURL=llm-stream.js.map