@@ -18,13 +18,18 @@ import { APPROVALS_SCOPE, WRITE_SCOPE } from "./method-scopes.js";
1818import type { GatewayMethodRegistry } from "./methods/registry.js" ;
1919import { dispatchGatewayRequestInProcess } from "./server-in-process-dispatch.js" ;
2020import type {
21+ GatewayInstanceAgentDispatchOptions ,
2122 GatewayInstanceRuntime ,
2223 GatewayRecoveryRuntime ,
2324} from "./server-instance-runtime.types.js" ;
2425import type { AgentRunRequest } from "./server-methods/agent-request-types.js" ;
2526import type { GatewayRequestContext } from "./server-methods/types.js" ;
2627import { createSyntheticPluginRuntimeClient } from "./server-plugin-runtime-client.js" ;
2728import { registerGatewayRecoveryRuntime } from "./server-recovery-runtime-context.js" ;
29+ import {
30+ cancelSubagentCompletionToolHandoff ,
31+ registerSubagentCompletionToolHandoff ,
32+ } from "./subagent-completion-tool-handoff.js" ;
2833
2934const loadOutboundMessageRuntime = createLazyRuntimeModule (
3035 ( ) => import ( "../infra/outbound/message.js" ) ,
@@ -92,20 +97,49 @@ export function createGatewayInstanceRuntime(
9297 dispatchAgent : async < T > (
9398 payload : AgentRunRequest ,
9499 timeoutMs ?: number ,
95- dispatchOptions ?: { allowModelOverride ?: boolean ; scopes ?: string [ ] } ,
100+ dispatchOptions : GatewayInstanceAgentDispatchOptions = { } ,
96101 ) => {
97102 assertDispatchAvailable ( "agent" ) ;
98- const agentTurns = dispatchOptions
103+ const delegatedToolPolicyHandoffId = dispatchOptions . delegatedToolPolicyHandoff
104+ ? registerSubagentCompletionToolHandoff ( dispatchOptions . delegatedToolPolicyHandoff )
105+ : undefined ;
106+ const needsDedicatedPrincipal = Boolean (
107+ dispatchOptions . allowModelOverride === true ||
108+ dispatchOptions . allowSyntheticModelOverride === true ||
109+ dispatchOptions . allowSyntheticCronRunContinuation === true ||
110+ dispatchOptions . internalDeliveryMediaUrls ||
111+ dispatchOptions . internalDeliverySuppressText === true ||
112+ delegatedToolPolicyHandoffId ||
113+ dispatchOptions . scopes ||
114+ dispatchOptions . syntheticScopes ,
115+ ) ;
116+ const agentTurns = needsDedicatedPrincipal
99117 ? createInternalAgentTurnFacade ( {
100118 client : createSyntheticPluginRuntimeClient ( {
101- allowModelOverride : dispatchOptions . allowModelOverride ,
102- scopes : dispatchOptions . scopes ,
119+ allowModelOverride :
120+ dispatchOptions . allowModelOverride === true ||
121+ dispatchOptions . allowSyntheticModelOverride === true ,
122+ cronRunContinuation : dispatchOptions . allowSyntheticCronRunContinuation === true ,
123+ internalDeliveryMediaUrls : dispatchOptions . internalDeliveryMediaUrls ,
124+ internalDeliverySuppressText : dispatchOptions . internalDeliverySuppressText ,
125+ delegatedToolPolicyHandoffId,
126+ scopes : dispatchOptions . scopes ?? dispatchOptions . syntheticScopes ,
103127 } ) ,
104128 getContext : options . getContext ,
105129 getMethodRegistry : options . getMethodRegistry ,
106130 } )
107131 : recoveryAgentTurns ;
108- return await agentTurns . dispatch < T > ( payload , timeoutMs ) ;
132+ try {
133+ return await agentTurns . dispatch < T > ( payload , {
134+ expectFinal : dispatchOptions . expectFinal ,
135+ onAccepted : dispatchOptions . onAccepted ,
136+ onSignalAbort : dispatchOptions . onSignalAbort ,
137+ signal : dispatchOptions . signal ,
138+ timeoutMs,
139+ } ) ;
140+ } finally {
141+ cancelSubagentCompletionToolHandoff ( delegatedToolPolicyHandoffId ) ;
142+ }
109143 } ,
110144 waitForAgent : async < T > ( payload : AgentWaitParams , timeoutMs ?: number ) => {
111145 assertDispatchAvailable ( "agent.wait" ) ;
0 commit comments