eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 3.06 kB
JavaScript
import{isObject}from"#shared/guards.js";import{getSandboxEnvironmentRuntime}from"#shared/sandbox-environment.js";import{BundleKey}from"#runtime/sessions/runtime-context-keys.js";import{resumeHook}from"#internal/workflow/runtime.js";import{deserializeContext}from"#context/serialize.js";import{readDurableSession}from"#execution/durable-session-store.js";import{getWorkflowToolRuns,readWorkflowTaskView}from"#harness/workflow-tool-runs.js";import{getAgentHandleStore}from"#subagents/handles/store.js";import{getResolvedRuntimeAgentNode}from"#runtime/graph.js";import{SESSION_CHECKPOINT_VERSION}from"#execution/session/handoff.js";function isSessionStateIdleForHandoff(e){let{state:t}=readDurableSession(e),n=getWorkflowToolRuns(t);for(let e of n)e.lifetime===`session`&&readWorkflowTaskView(e.task);let r=getAgentHandleStore(t);if([`eve.runtime.pendingAuthorization`,`eve.runtime.pendingInputBatch`,`eve.runtime.pendingCoordinationBatch`,`eve.runtime.deferredStepInput`,`eve.harness.pendingWorkflowInterrupt`].some(e=>t?.[e]!==void 0))return!1;let i=t?.[`eve.runtime.pendingInputBatches`];if(i!==void 0&&(!Array.isArray(i)||i.length>0))return!1;let a=t?.[`eve.runtime.proxyInputRequests`];return a!==void 0&&(!isObject(a)||Object.keys(a).length>0)?!1:(r===void 0||r.handles.every(e=>e.phase===`parked`||e.phase===`available`))&&n.every(e=>e.lifetime===`session`&&e.task.outcome!==void 0)}async function isSessionIdleForHandoffStep(e){"use step";return isSessionStateIdleForHandoff(e.sessionState)}async function validateSessionCheckpointStep(e){"use step";let{checkpoint:t}=e;if(t.version!==SESSION_CHECKPOINT_VERSION)throw Error(`Unsupported session checkpoint version ${JSON.stringify(t.version)}; this deployment reads version ${SESSION_CHECKPOINT_VERSION}. Start a new session on this deployment.`);let n=t.sessionTimeoutMs;if(n!==!1&&(typeof n!=`number`||!Number.isFinite(n)||n<0))throw Error(`Session checkpoint contains an invalid timeout duration.`);let r=(await deserializeContext(t.serializedContext)).require(BundleKey),i=readDurableSession(t.sessionState).sandboxState?.session;if(i!=null){let e=getResolvedRuntimeAgentNode(r.graph,r.nodeId).sandboxRegistry.sandbox.inheritance?.definition??getResolvedRuntimeAgentNode(r.graph,r.nodeId).sandboxRegistry.sandbox.definition;if(e.kind!==`independent`)throw Error(`Session checkpoint sandbox has no resolved provider.`);let t=getSandboxEnvironmentRuntime(e.environment);if(i.providerName!==t.providerName||i.stateProtocolVersion!==t.stateProtocolVersion)throw Error(`Session checkpoint sandbox provider state is incompatible.`)}if(!isSessionStateIdleForHandoff(t.sessionState))throw Error(`Session checkpoint contains pending work and cannot be handed off.`)}async function signalSessionOwnerActivationStep(e){"use step";await resumeHook(e.token,e.activation)}async function signalSessionAnchorStep(e){"use step";await resumeHook(e.token,e.result)}export{isSessionIdleForHandoffStep,isSessionStateIdleForHandoff,signalSessionAnchorStep,signalSessionOwnerActivationStep,validateSessionCheckpointStep};