Skip to main content

ManagedAgentRuntime

Trait ManagedAgentRuntime 

Source
pub trait ManagedAgentRuntime: Send + Sync {
    // Required methods
    fn create<'life0, 'async_trait>(
        &'life0 self,
        def: ManagedAgentDef,
    ) -> Pin<Box<dyn Future<Output = Result<AgentHandle, RuntimeError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn start_session<'life0, 'life1, 'async_trait>(
        &'life0 self,
        agent: &'life1 AgentHandle,
        env: Option<EnvironmentConfig>,
    ) -> Pin<Box<dyn Future<Output = Result<SessionHandle, RuntimeError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn send_event<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session: &'life1 SessionHandle,
        event: UserEvent,
    ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn stream_events<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session: &'life1 SessionHandle,
        from_seq: Option<u64>,
    ) -> Pin<Box<dyn Future<Output = Result<BoxStream<'static, SessionEvent>, RuntimeError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn interrupt<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session: &'life1 SessionHandle,
    ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn pause<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session: &'life1 SessionHandle,
    ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn resume<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session: &'life1 SessionHandle,
    ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn status<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session: &'life1 SessionHandle,
    ) -> Pin<Box<dyn Future<Output = Result<SessionStatus, RuntimeError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn archive<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session: &'life1 SessionHandle,
    ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn delete_session<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session: &'life1 SessionHandle,
    ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

The central async trait defining the managed agent lifecycle.

Implementations of this trait encapsulate the full agent lifecycle: creating agents from declarative definitions, starting durable sessions, sending/receiving events, pausing/resuming, interrupting, and archiving.

The trait is provider-agnostic: it takes a ManagedAgentDef with a ModelRef and behaves identically regardless of which LLM provider powers the agent.

§Implementors

  • DefaultManagedAgentRuntime — the default implementation composed from Runner + pluggable SessionService + optional sandbox/memory.

§Design Notes

  • All methods return Result<_, RuntimeError> for structured error handling.
  • stream_events returns a BoxStream for SSE-compatible event delivery.
  • from_seq on stream_events enables Last-Event-ID reconnection.
  • The runtime is Send + Sync for use across async task boundaries.

Required Methods§

Source

fn create<'life0, 'async_trait>( &'life0 self, def: ManagedAgentDef, ) -> Pin<Box<dyn Future<Output = Result<AgentHandle, RuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Create a managed agent from a declarative definition.

Resolves the ModelRef, builds a runnable agent, and stores it in the internal registry. Returns an opaque handle for use with start_session.

Source

fn start_session<'life0, 'life1, 'async_trait>( &'life0 self, agent: &'life1 AgentHandle, env: Option<EnvironmentConfig>, ) -> Pin<Box<dyn Future<Output = Result<SessionHandle, RuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Start a new session for the given agent.

Creates a session in Queued status, initializes the session loop, and returns a handle for event interaction. The optional EnvironmentConfig provides env vars and working directory.

Source

fn send_event<'life0, 'life1, 'async_trait>( &'life0 self, session: &'life1 SessionHandle, event: UserEvent, ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Send an event from the client to the agent session.

Dispatches the UserEvent to the session loop. The event type determines behavior:

  • user.message — enqueues a message for processing
  • user.interrupt — signals the session to stop at next boundary
  • user.custom_tool_result — delivers a result to a parked tool call
  • user.tool_confirmation — approves or denies a pending tool use
Source

fn stream_events<'life0, 'life1, 'async_trait>( &'life0 self, session: &'life1 SessionHandle, from_seq: Option<u64>, ) -> Pin<Box<dyn Future<Output = Result<BoxStream<'static, SessionEvent>, RuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Subscribe to the session’s event stream.

Returns a stream of SessionEvents. If from_seq is provided, replays all events with seq > from_seq before attaching to the live broadcast (enabling SSE Last-Event-ID reconnection).

Source

fn interrupt<'life0, 'life1, 'async_trait>( &'life0 self, session: &'life1 SessionHandle, ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Interrupt the session at the next safe boundary.

Signals the session loop’s cancellation token. The loop will stop processing at the next inter-event boundary and emit status.idle.

Source

fn pause<'life0, 'life1, 'async_trait>( &'life0 self, session: &'life1 SessionHandle, ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Pause the session, checkpointing current state.

Stops consuming new input and persists the current run-state. The session transitions to Paused status.

Source

fn resume<'life0, 'life1, 'async_trait>( &'life0 self, session: &'life1 SessionHandle, ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Resume a paused session from its last checkpoint.

Clears the pause flag, rehydrates state if needed, and returns the session to active processing.

Source

fn status<'life0, 'life1, 'async_trait>( &'life0 self, session: &'life1 SessionHandle, ) -> Pin<Box<dyn Future<Output = Result<SessionStatus, RuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Query the current status of a session.

Source

fn archive<'life0, 'life1, 'async_trait>( &'life0 self, session: &'life1 SessionHandle, ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Archive a session (terminal state).

Sets the session to Archived status and stops the session loop. Archived sessions retain their event log for read access.

Source

fn delete_session<'life0, 'life1, 'async_trait>( &'life0 self, session: &'life1 SessionHandle, ) -> Pin<Box<dyn Future<Output = Result<(), RuntimeError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete a session and its associated data.

Archives the session (if not already terminal) and removes all persisted data including events and checkpoints.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§