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>, ) -> Self

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>) -> Self

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 set_model(&mut self, provider: Arc<dyn Provider>, model: &str) -> Message

Switch the model this session samples with, mid-conversation.

The caller rebuilds the provider (the registry’s factory already takes a model override) and hands it in; this swaps it and updates the config so the trace’s later records name the model actually in use.

The preamble is not rewritten. A pack’s system prompt may name the model — Claude Code’s env block does, and so does our port — and after a switch that line is stale. Rewriting the System message would desync the transcript from the trace, whose Init record already captured the original preamble: a resumed session would replay one preamble while the live session had another. So the change is announced instead, as an appended <system-reminder> — the same never-mutate-history discipline project instructions and skills already follow.

Returns the announcement, which the caller appends and emits like any other message. (Doing it here would bypass the sink the caller owns.)

Source

pub fn add_root(&mut self, root: PathBuf)

Register another discovery root, so the next turn’s instruction and skill rescans see that directory’s AGENTS.md and .agents/skills.

Only the config changes here — widening the tool jail is the host’s job (Host::add_root), and the caller does both. Both discoveries already re-run per turn (ADR-0023 whole-body diff, ADR-0025 post-run rescan), so nothing needs re-injecting by hand: the added root simply appears in the next scan.

Source

pub fn input_queue(&self) -> InputQueue

A clonable handle to this session’s mid-run input queue (ADR-0028).

Clone it before calling Session::runrun takes &mut self, so nothing on the session is reachable while a turn is in flight. Same shape as Session::cancel_handle and for the same reason.

Source

pub fn set_effort(&mut self, effort: Option<ReasoningEffort>)

Set the reasoning effort subsequent turns sample with.

Unlike Session::set_model this announces nothing: effort changes how hard the model thinks, not who it is, so there is no stale statement in the transcript to correct — and injecting a reminder would cost a cache breakpoint for no gain.

Source

pub fn announce(&mut self, message: Message)

Append a message to the conversation and emit it, exactly as a turn would.

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