@@ -176,6 +176,13 @@ export interface RequestExecutionBudget extends TransientSendBudget {
176176 * twice. It is NOT a send budget: an authorised replacement still has to fit inside
177177 * `remainingBaseSends` like every other send.
178178 *
179+ * `limit` is the ceiling the ASKING leg is authorised to present, and the request keeps the
180+ * smallest one any leg has presented. A leg reads it from `route.provider`, which credential
181+ * rotation, OAuth refresh, transport resolution and a combo target all reassign mid-request,
182+ * so a per-call ceiling meant the number of duplicate inferences a request could make
183+ * depended on which row happened to ask last: a row granting one, then a row granting two,
184+ * bought a second replacement of a turn that may already have run.
185+ *
179186 * Optional so a hand-written stub that satisfies the shape test keeps typechecking; a caller
180187 * that cannot reach it has no operator override, which is the fail-closed answer.
181188 */
@@ -208,16 +215,48 @@ interface SharedSendLedger {
208215 spent : number ;
209216 pendingExternalSends : number ;
210217 /**
211- * Replacements this logical request has already spent on ambiguous failures . Beside `spent`
218+ * Spend one of this logical request's replacements for an ambiguous failure . Beside `spent`
212219 * for the same reason `pendingExternalSends` is: a derived scope that shared one without the
213220 * other would hand the request a second grant.
221+ *
222+ * A function rather than the raw count, because the count is not the whole state. The
223+ * ceiling belongs to the request too, and a bridged scope has no counter of its own to keep
224+ * it in -- it has to ask whoever holds the request's grant.
214225 */
215- ambiguousResendsClaimed : number ;
226+ claimAmbiguousResend ( limit : number ) : boolean ;
216227 readonly observer ?: RequestSendObserver ;
217228}
218229
219230const sharedSendLedgers = new WeakMap < RequestExecutionBudget , SharedSendLedger > ( ) ;
220231
232+ /**
233+ * One logical request's replacement grant: how many it has spent, and the ceiling it is held
234+ * to.
235+ *
236+ * The ceiling is the SMALLEST any leg has presented rather than whatever the current leg
237+ * presents. Each leg reads its number from the provider row it is running against, and that
238+ * row changes inside one request -- credential rotation, OAuth refresh, transport resolution
239+ * and each combo target reassign it. Taking the asking leg's number let a request that had
240+ * already spent the one replacement a strict row granted buy another as soon as a more
241+ * permissive row asked, which is a second duplicate inference of one turn.
242+ */
243+ function createAmbiguousResendGrant ( ) : ( limit : number ) => boolean {
244+ let claimed = 0 ;
245+ let ceiling : number | undefined ;
246+ return ( limit : number ) : boolean => {
247+ const presented = Number . isFinite ( limit ) ? Math . trunc ( limit ) : 0 ;
248+ // A zero or nonsense ceiling refuses on its own and leaves the request's alone. It is a
249+ // caller that cannot state a grant, not an operator narrowing this request: a leg with no
250+ // policy is refused before it ever claims, so binding the request to a malformed number
251+ // would only let such a caller cancel a grant an opted-in row really made.
252+ if ( presented <= 0 ) return false ;
253+ ceiling = ceiling === undefined ? presented : Math . min ( ceiling , presented ) ;
254+ if ( claimed >= ceiling ) return false ;
255+ claimed += 1 ;
256+ return true ;
257+ } ;
258+ }
259+
221260function createRequestExecutionBudgetWithLedger (
222261 policy : RequestExecutionBudgetPolicy ,
223262 logicalRequestId : string | undefined ,
@@ -261,10 +300,7 @@ function createRequestExecutionBudgetWithLedger(
261300 return Math . max ( 0 , Math . min ( capped , policy . baseSendAllowance - counter . spent ) ) ;
262301 } ,
263302 claimAmbiguousResend ( limit : number ) : boolean {
264- const ceiling = Number . isFinite ( limit ) ? Math . trunc ( limit ) : 0 ;
265- if ( counter . ambiguousResendsClaimed >= ceiling ) return false ;
266- counter . ambiguousResendsClaimed += 1 ;
267- return true ;
303+ return counter . claimAmbiguousResend ( limit ) ;
268304 } ,
269305 reserveDispatch ( intent : DispatchIntent ) : DispatchDecision {
270306 if ( intent . replaySafe === false ) return { allowed : false , reason : "not-replay-safe" } ;
@@ -363,7 +399,7 @@ export function createRequestExecutionBudget(
363399 return createRequestExecutionBudgetWithLedger ( policy , logicalRequestId , {
364400 spent : 0 ,
365401 pendingExternalSends : 0 ,
366- ambiguousResendsClaimed : 0 ,
402+ claimAmbiguousResend : createAmbiguousResendGrant ( ) ,
367403 ...( observer ? { observer } : { } ) ,
368404 } ) ;
369405}
@@ -398,14 +434,19 @@ function ledgerFor(parent: RequestExecutionBudget): SharedSendLedger {
398434 const existing = sharedSendLedgers . get ( parent ) ;
399435 if ( existing ) return existing ;
400436 let pendingExternalSends = 0 ;
401- let ambiguousResendsClaimed = 0 ;
402437 return {
403438 get spent ( ) : number { return parent . used ; } ,
404439 set spent ( next : number ) { parent . used = next ; } ,
405440 get pendingExternalSends ( ) : number { return pendingExternalSends ; } ,
406441 set pendingExternalSends ( next : number ) { pendingExternalSends = next ; } ,
407- get ambiguousResendsClaimed ( ) : number { return ambiguousResendsClaimed ; } ,
408- set ambiguousResendsClaimed ( next : number ) { ambiguousResendsClaimed = next ; } ,
442+ // Asked of the parent rather than counted here. A local counter is a SECOND grant: two
443+ // scopes derived from one bridged parent, or one scope beside the parent it was derived
444+ // from, each replaced an unknown-state send once. Pending bookings and the durable-spend
445+ // observer genuinely cannot cross this boundary because they are private to the factory,
446+ // but the grant can -- `claimAmbiguousResend` is public on the parent. A parent that does
447+ // not implement it grants nothing, which is the fail-closed answer for a send whose
448+ // upstream state is unknown.
449+ claimAmbiguousResend : ( limit : number ) : boolean => parent . claimAmbiguousResend ?.( limit ) === true ,
409450 } ;
410451}
411452
0 commit comments