Skip to main content

Session

Struct Session 

Source
pub struct Session { /* private fields */ }
Expand description

One driven agent session. Owns the conversation history across runs: a second Session::run on the same session continues the same conversation (ADR-0016) — the exact call shape an interactive frontend needs for follow-up turns.

Construct with Session::new, then call Session::run (or Session::run_text) to drive one run to a terminal state. run is infallible — every terminal condition (including provider and Fatal tool errors) is captured in the returned Report’s status/error, so a caller gets a structured result every time (locode-exec maps status → exit code).

Each Report is per-run: turns/usage/tool_calls count the current run only (a cumulative view is derivable from the event stream). Continuing after a failed run is allowed unconditionally — for ModelError the history simply didn’t advance, and for Error the transcript was fully paired before the break; the pre-send pairing repair heals any residue on the next sample.

Implementations§

Source§

impl Session

Source

pub fn new( provider: Arc<dyn Provider>, registry: Registry, preamble: Vec<Message>, config: EngineConfig, sink: Box<dyn EventSink>, ) -> Session

Assemble a session from its parts.

preamble is the base System + Developer messages (the pack supplies these); provider/sink are trait objects so the binary can select them at runtime.

Source

pub fn with_approver(self, approver: Arc<dyn Approver>) -> Session

Install an Approver consulted before every tool call (ADR-0017).

Builder-style so Session::new’s signature stays intact. The default is AllowAll — headless consumers are unchanged without this call.

Source

pub fn history(&self) -> &[Message]

The conversation so far: the preamble plus every appended turn across all runs on this session (ADR-0016). Lets a frontend render the transcript after a run without replaying the event stream.

Source

pub fn cancel_handle(&self) -> CancellationToken

The cancellation handle for the current run (ADR-0018).

Clone it before calling Session::run (mandatory — run takes &mut self, so nothing is callable mid-run) and move it into an Esc handler, signal handler, or timeout. Firing it stops the run at the next observation point — mid-sample (the in-flight request is aborted), between batch calls (the rest of the batch is paired synthetically), or at the loop top — and the run returns a report with Status::Cancelled. Partial work is preserved: with session continuity, the next run() continues the same conversation.

The token is per-run, replaced when run returns: a cancel landing after the run ended hits the retired token — a harmless no-op — so the Esc-lands-late race is resolved by construction. Re-fetch the handle each turn. cancel() is idempotent; there is no reset.

Source

pub async fn run(&mut self, user: Vec<ContentBlock>) -> Report

Drive the loop to a terminal state and return the run’s Report.

Source

pub async fn run_text(&mut self, prompt: impl Into<String>) -> Report

Convenience: drive with a plain-text user prompt.

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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