11import {
22 captureGatewayRootWorkAdmissionContinuationScope ,
3+ isGatewayRestartDraining ,
34 type GatewayRootWorkAdmissionContinuationScope ,
45} from "../process/gateway-work-admission.js" ;
56import { NODE_INVOKE_PAIRING_CHANGED_ABORT } from "./node-registry-private-token.js" ;
@@ -36,6 +37,7 @@ export type PendingInvoke = {
3637 nextInputSeq : number ;
3738 removeAbortListener ?: ( ) => void ;
3839 admissionContinuation ?: GatewayRootWorkAdmissionContinuationScope ;
40+ isCompletionAuthorized ?: ( ) => boolean ;
3941} ;
4042
4143export type NodeInvokeProgressParams = {
@@ -119,19 +121,8 @@ export class NodeInvokeStreamController {
119121 }
120122
121123 handleResult ( params : NodeInvokeResultParams ) : boolean {
122- const pending = this . options . pendingInvokes . get ( params . id ) ;
123- if (
124- ! pending ||
125- pending . nodeId !== params . nodeId ||
126- pending . connId !== params . connId ||
127- ! this . options . isConnectionActive ( pending )
128- ) {
129- return false ;
130- }
131- if ( this . settleIfExpired ( params . id , pending ) ) {
132- return false ;
133- }
134- if ( ! this . takePending ( params . id , pending ) ) {
124+ const pending = this . getPending ( params . id , params . nodeId , params . connId ) ;
125+ if ( ! pending || ! this . takePending ( params . id , pending ) ) {
135126 return false ;
136127 }
137128 if ( ! params . ok ) {
@@ -203,17 +194,8 @@ export class NodeInvokeStreamController {
203194 }
204195
205196 handleProgress ( params : NodeInvokeProgressParams ) : boolean {
206- const pending = this . options . pendingInvokes . get ( params . invokeId ) ;
207- if (
208- ! pending ||
209- pending . nodeId !== params . nodeId ||
210- pending . connId !== params . connId ||
211- ! this . options . isConnectionActive ( pending ) ||
212- params . seq < pending . nextProgressSeq
213- ) {
214- return false ;
215- }
216- if ( this . settleIfExpired ( params . invokeId , pending ) ) {
197+ const pending = this . getPending ( params . invokeId , params . nodeId , params . connId ) ;
198+ if ( ! pending || params . seq < pending . nextProgressSeq ) {
217199 return false ;
218200 }
219201 // Receipt proves execution even without a stream consumer. Keep ignored
@@ -243,7 +225,7 @@ export class NodeInvokeStreamController {
243225 if ( chunk === undefined ) {
244226 break ;
245227 }
246- if ( this . settleIfExpired ( params . invokeId , pending ) ) {
228+ if ( ! this . getPending ( params . invokeId , params . nodeId , params . connId ) ) {
247229 break ;
248230 }
249231 pending . progressChunks . delete ( pending . nextProgressSeq ) ;
@@ -263,14 +245,52 @@ export class NodeInvokeStreamController {
263245 pending . progressChunks . clear ( ) ;
264246 break ;
265247 }
266- if ( this . settleIfExpired ( params . invokeId , pending ) ) {
248+ if ( ! this . getPending ( params . invokeId , params . nodeId , params . connId ) ) {
267249 break ;
268250 }
269251 this . resetIdleTimer ( params . invokeId , pending ) ;
270252 }
271253 return true ;
272254 }
273255
256+ runPendingContinuation < T > ( params : {
257+ invokeId : string ;
258+ nodeId : string ;
259+ connId : string | undefined ;
260+ run : ( ) => Promise < T > ;
261+ } ) : Promise < T > | null {
262+ const pending = this . getPending ( params . invokeId , params . nodeId , params . connId ) ;
263+ if ( ! pending ) {
264+ return null ;
265+ }
266+ if ( pending . admissionContinuation ) {
267+ return pending . admissionContinuation . run ( params . run ) ;
268+ }
269+ // Shutdown cleanup has no request root. Its live private owner grants only
270+ // settlement; do not mint admission or revive a released captured root.
271+ return isGatewayRestartDraining ( ) && pending . isCompletionAuthorized ? params . run ( ) : null ;
272+ }
273+
274+ private getPending ( id : string , nodeId : string , connId : string | undefined ) {
275+ const pending = this . options . pendingInvokes . get ( id ) ;
276+ if (
277+ ! pending ||
278+ pending . nodeId !== nodeId ||
279+ pending . connId !== connId ||
280+ ! this . options . isConnectionActive ( pending ) ||
281+ this . settleIfExpired ( id , pending )
282+ ) {
283+ return undefined ;
284+ }
285+ // Recheck at settlement as handler loading may await after router admission.
286+ // Some lifecycle owners assert by throwing; either form must fail closed.
287+ try {
288+ return pending . isCompletionAuthorized ?.( ) === false ? undefined : pending ;
289+ } catch {
290+ return undefined ;
291+ }
292+ }
293+
274294 clearTimers ( pending : PendingInvoke ) : void {
275295 if ( pending . hardTimer ) {
276296 clearTimeout ( pending . hardTimer ) ;
0 commit comments