Skip to main content

SessionRecorder

Struct SessionRecorder 

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

Records every AgentEvent into a structured tree of Sessions and LoopRecords.

Call on_event for every event emitted on the agent’s tx channel, then flush before shutdown or saving.

§Session grouping

Sessions are keyed by session_id. Every AgentStart event that carries a session_id the recorder has not seen before opens a new Session; all subsequent loops with the same session_id are appended to that session.

The recorder never rotates sessions on its own. If you want a new session to start after a period of inactivity, call BasicAgent::check_and_rotate (or BasicAgent::new_session) before the next prompt. The next AgentStart will carry the new session_id and the recorder will open a fresh Session automatically, with SessionFormation::InactivityTimeout or SessionFormation::FirstLoop as the recorded reason.

§Example

use phi_core::session::{SessionRecorder, SessionRecorderConfig};
use phi_core::AgentEvent;

let mut recorder = SessionRecorder::new(SessionRecorderConfig::default());
// Feed events as they arrive:
// recorder.on_event(event);
recorder.flush();

Implementations§

Source§

impl SessionRecorder

Source

pub fn new(config: SessionRecorderConfig) -> Self

Create a new recorder with the given configuration.

Source

pub fn on_event(&mut self, event: AgentEvent)

Feed one event into the recorder.

Must be called for every event emitted on the agent’s tx channel.

Source

pub fn flush(&mut self)

Finalize all open LoopRecords (status → LoopStatus::Aborted) and move them into their sessions.

Call before saving or on process shutdown.

Source

pub fn checkpoint(&mut self) -> usize

Promote sessions that have no remaining open loops to the completed list, without aborting any running loops.

A session is eligible when every loop belonging to it has already received an AgentEnd event (i.e. it has no entry in the internal open-loops map). Sessions that still have active loops are left in place.

This is intended for periodic checkpointing in production: save finished sessions to disk while leaving in-flight agent runs untouched. In contrast, flush first aborts all open loops and then promotes everything.

Returns the number of sessions that were promoted.

Source

pub fn drain_completed(&mut self) -> Vec<Session>

Drain all completed sessions out of the recorder (consuming them).

Useful for periodic checkpointing. Call flush first if you want to include in-progress sessions, or checkpoint to drain only fully-finished sessions without aborting active loops.

Source

pub fn sessions(&self) -> impl Iterator<Item = &Session>

All sessions known to this recorder (completed and in-progress).

Source

pub fn get_session(&self, session_id: &str) -> Option<&Session>

Look up a session by session_id.

Source

pub fn current_loop(&self, loop_id: &str) -> Option<&LoopRecord>

Look up an in-progress LoopRecord by loop_id.

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<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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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, 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