UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines 2.69 kB
import{createLogger,logError}from"#internal/logging.js";import{BundleKey}from"#runtime/sessions/runtime-context-keys.js";import{cancelRun,getWorld}from"#internal/workflow/runtime.js";import{deserializeContext}from"#context/serialize.js";import{readDurableSession}from"#execution/durable-session-store.js";import{resetRemoteAgentSession,resolveRemoteAgentForAction,resolveRemoteAgentStreamHeaders}from"#subagents/remote-dispatch.js";import{getAgentHandleStore}from"#subagents/handles/store.js";import{getDynamicSubagentSelection}from"#context/dynamic-subagent-lifecycle.js";import{cancelAllIndexedSessionTasksStep}from"#execution/cancel-indexed-session-tasks-step.js";const log=createLogger(`execution.terminate-child-sessions`);async function terminateChildSessionsStep(e){"use step";let t;try{t=await readDurableSession(e.sessionState)}catch(t){logError(log,`failed to read child sessions for termination`,t,{parentSessionId:e.sessionState.sessionId});return}let n=getAgentHandleStore(t.state)?.handles??[],r=n.some(e=>`address`in e&&e.address.kind===`agent/remote`),i;if(await cancelAllIndexedSessionTasksStep({serializedContext:e.serializedContext,sessionState:e.sessionState}),r){if(e.serializedContext===void 0)throw Error(`Child finalization requires serialized runtime context.`);let t=await deserializeContext(e.serializedContext);i={bundle:t.require(BundleKey),ctx:t}}if(n.length!==0)for(let e of n){if(!(`address`in e)){log.debug(`skipping unaddressed child`,{agentId:e.identity.id,parentSessionId:t.sessionId});continue}try{if(e.address.kind===`agent/remote`){if(e.address.credentialResolver!==void 0){let t=e.address.credentialResolver.resolverId,n=t===void 0?{}:await resolveRemoteAgentStreamHeaders({bundle:i.bundle,name:e.identity.name,resolverId:t,url:e.address.url});await resetRemoteAgentSession({headers:n,remote:{name:e.identity.name,url:e.address.url},sessionId:e.address.sessionId})}else{let t=getDynamicSubagentSelection(i.ctx,e.identity.nodeId),n=resolveRemoteAgentForAction({dynamicRemoteAgent:t?.kind===`remote`?t.remoteAgent:void 0,nodeId:e.identity.nodeId,registry:i.bundle.subagentRegistry.subagentsByNodeId,remoteAgentName:e.identity.name});n.url===e.address.url?await resetRemoteAgentSession({remote:n,sessionId:e.address.sessionId}):await resetRemoteAgentSession({headers:{},remote:{name:e.identity.name,url:e.address.url},sessionId:e.address.sessionId})}}else await cancelRun(await getWorld(),e.address.sessionId,{cancelReason:`Parent session ended`})}catch(n){logError(log,`failed to terminate child session`,n,{agentId:e.identity.id,childSessionId:e.address.sessionId,kind:e.address.kind,parentSessionId:t.sessionId})}}}export{terminateChildSessionsStep};