@@ -11,20 +11,27 @@ import {
1111} from "./reply-dispatcher.js" ;
1212import type { ReplyDispatchKind , ReplyDispatcher } from "./reply-dispatcher.types.js" ;
1313
14+ // Failsafe for transports that never settle: the completion barrier bounds
15+ // delivery waits at the operation layer, and finalization must not out-wait it.
16+ const SETTLE_QUEUED_TIMEOUT_MS = 30_000 ;
17+
1418type LedgerQueuedSend = {
1519 queued : boolean ;
1620 /** Present only when the core dispatcher exposes this payload's settlement. */
1721 outcome ?: Promise < ReplyDispatchDeliveryOutcome > ;
1822} ;
1923
24+ type LedgerSettleResult = "settled" | "aborted" | "timed-out" ;
25+
2026type ReplyTurnLedger = {
2127 /** Enqueue on the dispatcher and record the payload's settled visibility. */
2228 sendQueued : ( kind : ReplyDispatchKind , payload : ReplyPayload ) => LedgerQueuedSend ;
2329 /** Record a routed transport result; routed sends settle at their call site. */
2430 recordRoutedDelivery : ( payload : ReplyPayload , delivered : boolean ) => void ;
2531 /** Resolve every admitted payload's outcome so the fallback gate decides after
26- * beforeDeliver hooks and transport delivery, not at admission. */
27- settleQueued : ( abortSignal ?: AbortSignal ) => Promise < void > ;
32+ * beforeDeliver hooks and transport delivery, not at admission. Only a
33+ * "settled" result proves the visibility verdict is complete. */
34+ settleQueued : ( abortSignal ?: AbortSignal ) => Promise < LedgerSettleResult > ;
2835 /** True once any settled, contentful, non-suppressed delivery exists. */
2936 hasVisibleDelivery : ( ) => boolean ;
3037 /** True when the dispatcher admitted payloads the dispatch pipeline never sent
@@ -69,7 +76,11 @@ export function createReplyTurnLedger(dispatcher: ReplyDispatcher): ReplyTurnLed
6976 }
7077 pendingOutcomes . push (
7178 capture . promise . then ( ( outcome ) => {
72- if ( contentful && outcome === "delivered" ) {
79+ // "failed-deliver" means the transport send started and then rejected;
80+ // chunked sends may already have shown partial content, so only
81+ // outcomes that never reached the transport ("cancelled",
82+ // "failed-before-deliver") count as proven-invisible.
83+ if ( contentful && outcome !== "cancelled" && outcome !== "failed-before-deliver" ) {
7384 visibleDeliveries += 1 ;
7485 }
7586 } ) ,
@@ -83,33 +94,54 @@ export function createReplyTurnLedger(dispatcher: ReplyDispatcher): ReplyTurnLed
8394 } ,
8495 async settleQueued ( abortSignal ) {
8596 // Outcome promises resolve when the dispatcher send chain settles each
86- // payload; the wait is bounded by the per-stage beforeDeliver deadlines and
97+ // payload, normally bounded by the per-stage beforeDeliver deadlines and
8798 // the transport sends the turn's completion barrier already waits for.
8899 // Late progress stragglers can admit payloads while earlier deliveries
89100 // settle, so drain until no new outcomes appeared mid-wait.
90- let settledCount = 0 ;
91- while ( settledCount < pendingOutcomes . length ) {
92- if ( abortSignal ?. aborted ) {
93- return ;
94- }
95- const batch = pendingOutcomes . slice ( settledCount ) ;
96- settledCount = pendingOutcomes . length ;
97- const settled = Promise . all ( batch ) . then ( ( ) => undefined ) ;
98- if ( ! abortSignal ) {
99- await settled ;
100- continue ;
101+ let timedOut = false ;
102+ let timer : ReturnType < typeof setTimeout > | undefined ;
103+ const deadline = new Promise < void > ( ( resolve ) => {
104+ timer = setTimeout ( ( ) => {
105+ timedOut = true ;
106+ resolve ( ) ;
107+ } , SETTLE_QUEUED_TIMEOUT_MS ) ;
108+ timer . unref ?.( ) ;
109+ } ) ;
110+ let removeAbortListener : ( ( ) => void ) | undefined ;
111+ const aborted = abortSignal
112+ ? new Promise < void > ( ( resolve ) => {
113+ const onAbort = ( ) => resolve ( ) ;
114+ abortSignal . addEventListener ( "abort" , onAbort , { once : true } ) ;
115+ removeAbortListener = ( ) => abortSignal . removeEventListener ( "abort" , onAbort ) ;
116+ } )
117+ : undefined ;
118+ try {
119+ let settledCount = 0 ;
120+ while ( settledCount < pendingOutcomes . length ) {
121+ if ( abortSignal ?. aborted ) {
122+ return "aborted" ;
123+ }
124+ if ( timedOut ) {
125+ return "timed-out" ;
126+ }
127+ const batch = pendingOutcomes . slice ( settledCount ) ;
128+ const batchTarget = pendingOutcomes . length ;
129+ const settled = Promise . all ( batch ) . then ( ( ) => undefined ) ;
130+ await Promise . race ( [ settled , deadline , ...( aborted ? [ aborted ] : [ ] ) ] ) ;
131+ if ( abortSignal ?. aborted ) {
132+ return "aborted" ;
133+ }
134+ if ( timedOut ) {
135+ return "timed-out" ;
136+ }
137+ settledCount = batchTarget ;
101138 }
102- let removeAbortListener : ( ( ) => void ) | undefined ;
103- const aborted = new Promise < void > ( ( resolve ) => {
104- const onAbort = ( ) => resolve ( ) ;
105- abortSignal . addEventListener ( "abort" , onAbort , { once : true } ) ;
106- removeAbortListener = ( ) => abortSignal . removeEventListener ( "abort" , onAbort ) ;
107- } ) ;
108- try {
109- await Promise . race ( [ settled , aborted ] ) ;
110- } finally {
111- removeAbortListener ?.( ) ;
139+ return "settled" ;
140+ } finally {
141+ if ( timer ) {
142+ clearTimeout ( timer ) ;
112143 }
144+ removeAbortListener ?.( ) ;
113145 }
114146 } ,
115147 hasVisibleDelivery : ( ) => visibleDeliveries > 0 ,
0 commit comments