Skip to main content

CompiledGraph

Struct CompiledGraph 

Source
pub struct CompiledGraph<S: State> { /* private fields */ }
Expand description

A validated, executable graph. Produced by StateGraph::compile().

This is the primary execution handle. Use invoke() to run a graph from initial state, resume() to continue after an interrupt, and get_state() to inspect the current checkpoint.

§Example

let outcome = graph.invoke(initial_state, GraphConfig::default()).await?;
match outcome {
    ExecutionOutcome::Completed(state) => println!("Done: {:?}", state),
    ExecutionOutcome::Interrupted { state, request } => {
        println!("Paused: {}", request.reason);
    }
}

Implementations§

Source§

impl<S: State> CompiledGraph<S>

Source

pub fn with_checkpointer(self, cp: impl Checkpointer + 'static) -> Self

Attach a checkpointer for durable state persistence.

Without a checkpointer, resume() and get_state() will return errors.

Source

pub fn with_agent(self, agent: Agent) -> Self

Bind an agent to this compiled graph.

The agent’s identity, system prompt, and boundaries are preserved on the compiled graph for runtime inspection and enforcement.

Source

pub fn agent(&self) -> Option<&Agent>

Get the bound agent, if any.

Source

pub fn with_checkpointer_arc(self, cp: Arc<dyn Checkpointer>) -> Self

Attach a shared checkpointer (already behind Arc).

Source

pub fn with_matrix_hook(self, hook: MatrixHookHandle) -> Self

Attach a matrix layer hook for convergence tracking and learned routing.

When attached, the Pregel engine will:

  • Record ConvergenceSignal metadata via the hook
  • Consult the hook for conditional edge routing decisions
  • Record transitions for learning

Without a hook, NodeResult::Converge degrades to Update and conditional edges use the user’s router function directly.

Source

pub fn matrix_hook(&self) -> Option<&MatrixHookHandle>

Get a reference to the matrix hook (if attached).

Source

pub async fn invoke( &self, state: S, config: GraphConfig, ) -> Result<ExecutionOutcome<S>, PeError>

Run graph from START with initial state.

If config.checkpoint_id is set and a checkpointer is attached, the graph resumes from that specific checkpoint (time travel) instead of running from the provided state.

Executes the BSP loop until END, interrupt, or recursion limit.

Source

pub async fn invoke_with_lobe_runtime_services( &self, state: S, config: GraphConfig, lobe_runtime_service_factory: Option<Arc<dyn LobeRuntimeServiceFactory>>, ) -> Result<ExecutionOutcome<S>, PeError>

Run graph from START with optional runtime-owned services for lobes.

Source

pub async fn invoke_with_observer_and_lobe_runtime_services( &self, state: S, config: GraphConfig, observer: Option<Arc<dyn NodeObserver>>, tool_observer: Option<Arc<dyn ToolObserver>>, lobe_runtime_service_factory: Option<Arc<dyn LobeRuntimeServiceFactory>>, ) -> Result<ExecutionOutcome<S>, PeError>

Run graph from START with optional observer, tool observer, and runtime-owned services.

Source

pub async fn invoke_with_stream( &self, state: S, config: GraphConfig, stream_sender: Arc<dyn Any + Send + Sync>, ) -> Result<ExecutionOutcome<S>, PeError>

Run graph from START with streaming support.

Like invoke, but injects a type-erased stream sender into every NodeContext and an optional NodeObserver for phase lifecycle events.

Supports time-travel: if config.checkpoint_id is set, loads and resumes from that checkpoint (mirroring invoke).

Called by pe-runtime’s streaming layer — not typically used directly.

Source

pub async fn invoke_with_stream_and_observer( &self, state: S, config: GraphConfig, stream_sender: Arc<dyn Any + Send + Sync>, observer: Option<Arc<dyn NodeObserver>>, tool_observer: Option<Arc<dyn ToolObserver>>, ) -> Result<ExecutionOutcome<S>, PeError>

Run graph with streaming and a NodeObserver for lifecycle events.

The observer receives on_node_start / on_node_complete / on_node_error callbacks. pe-runtime provides StreamingObserver which converts these to StreamEvent.

Source

pub async fn invoke_with_stream_observer_and_lobe_runtime_services( &self, state: S, config: GraphConfig, stream_sender: Arc<dyn Any + Send + Sync>, observer: Option<Arc<dyn NodeObserver>>, tool_observer: Option<Arc<dyn ToolObserver>>, lobe_runtime_service_factory: Option<Arc<dyn LobeRuntimeServiceFactory>>, ) -> Result<ExecutionOutcome<S>, PeError>

Run graph with streaming, observers, and optional runtime-owned lobe services.

Source

pub async fn resume( &self, thread_id: &str, input: S::Update, config: GraphConfig, ) -> Result<ExecutionOutcome<S>, PeError>

Resume a previously interrupted graph with human input.

Loads the latest checkpoint for the thread, applies the input update, and continues execution from where it paused.

Source

pub async fn resume_with( &self, thread_id: &str, command: Command, config: GraphConfig, ) -> Result<ExecutionOutcome<S>, PeError>

Resume a previously interrupted graph using a Command.

This is the preferred resume API. It loads the latest checkpoint, applies the command (human input, goto, or state update), and continues execution.

For Command::Resume, the human input is stored in the phase state so nodes can access it via PhaseStateStore::get::<HumanInput>().

§Example
let cmd = Command::resume(HumanInput { approved: true, feedback: None, data: None });
let outcome = graph.resume_with("thread-1", cmd, config).await?;
Source

pub async fn resume_with_stream( &self, thread_id: &str, command: Command, config: GraphConfig, stream_sender: Arc<dyn Any + Send + Sync>, observer: Option<Arc<dyn NodeObserver>>, tool_observer: Option<Arc<dyn ToolObserver>>, ) -> Result<ExecutionOutcome<S>, PeError>

Resume with streaming and observer support.

Like resume_with, but injects a stream sender and optional observer for lifecycle events.

Source

pub async fn get_state( &self, thread_id: &str, ) -> Result<Option<StateSnapshot<S>>, PeError>

Get the current state snapshot for a thread.

Returns None if no checkpoints exist for this thread.

Source

pub async fn get_state_history( &self, thread_id: &str, ) -> Result<Vec<StateSnapshot<S>>, PeError>

Get full history of all checkpoints for a thread (time travel).

Returns snapshots oldest-first. Each snapshot contains the full state at that point, which nodes were scheduled next, and metadata.

Trait Implementations§

Source§

impl<S: State> Debug for CompiledGraph<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> !RefUnwindSafe for CompiledGraph<S>

§

impl<S> !UnwindSafe for CompiledGraph<S>

§

impl<S> Freeze for CompiledGraph<S>

§

impl<S> Send for CompiledGraph<S>

§

impl<S> Sync for CompiledGraph<S>

§

impl<S> Unpin for CompiledGraph<S>

§

impl<S> UnsafeUnpin for CompiledGraph<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more