Skip to main content

SessionManager

Struct SessionManager 

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

Manages a single session’s entries and persistence.

Raw persisted entries are always retained in entries. Active model-visible history may differ from the raw branch when a SessionEntry::Compaction exists on the current branch. In that case, callers should prefer get_active_messages() over get_messages() when assembling context for an LLM request.

Implementations§

Source§

impl SessionManager

Source

pub fn new(cwd: &Path, session_dir: &Path) -> Result<Self>

Create a new session. Writes the header to disk immediately.

Source

pub fn open(path: &Path) -> Result<Self>

Open an existing session file, skipping malformed lines.

Source

pub fn in_memory() -> Self

In-memory session (no persistence).

Source

pub fn continue_recent(cwd: &Path, session_dir: &Path) -> Result<Option<Self>>

Find the most recently modified session for a given cwd.

Source

pub fn name(&self) -> Option<&str>

Get the session name.

Source

pub fn summary(&self) -> Option<&str>

Get the session summary.

Source

pub fn set_name(&mut self, name: &str)

Set the session name.

Source

pub fn set_summary(&mut self, summary: impl Into<String>)

Set the session summary.

Source

pub fn clear_summary(&mut self)

Clear the session summary.

Source

pub fn title(&self, max_chars: usize) -> Option<String>

A short, single-line chat title derived from persisted session metadata or message history.

Source

pub fn append(&mut self, entry: SessionEntry) -> Result<()>

Append an entry. Sets parent_id to current leaf_id, updates leaf_id, and writes to file if persisted.

Source

pub fn append_assistant_turn( &mut self, model: &Model, turn_index: u32, message: AssistantMessage, ) -> Result<(String, Option<String>)>

Append an assistant turn and, when available, its canonical usage record.

Source

pub fn append_assistant_turn_with_model_meta( &mut self, model_meta: &ModelMeta, turn_index: u32, message: AssistantMessage, ) -> Result<(String, Option<String>)>

Append an assistant turn and, when available, its canonical usage record.

Source

pub fn append_tool_result_message( &mut self, result: ToolResultMessage, ) -> Result<String>

Append a tool result message and return the persisted entry id.

Source

pub fn persist_agent_event_entries( &mut self, model: &Model, event: &AgentEvent, ) -> Result<Vec<&'static str>>

Persist the session entries implied by an agent event.

Returns a short description of what was written so callers can surface best-effort persistence diagnostics without owning the persistence logic.

Source

pub fn persist_agent_event_entries_with_model_meta( &mut self, model_meta: &ModelMeta, event: &AgentEvent, ) -> Result<Vec<&'static str>>

Persist the session entries implied by an agent event.

Returns a short description of what was written so callers can surface best-effort persistence diagnostics without owning the persistence logic.

Source

pub fn append_canonical_usage_for_assistant_turn( &mut self, model: &Model, assistant_message_id: &str, turn_index: u32, message: &AssistantMessage, ) -> Result<Option<String>>

Append a canonical usage entry for an assistant turn, if the turn reports usage and no equivalent canonical record already exists.

This is best-effort metadata persistence: callers should treat errors as non-fatal to the main agent flow.

Source

pub fn append_canonical_usage_for_assistant_turn_with_model_meta( &mut self, model_meta: &ModelMeta, assistant_message_id: &str, turn_index: u32, message: &AssistantMessage, ) -> Result<Option<String>>

Append a canonical usage entry for an assistant turn, if the turn reports usage and no equivalent canonical record already exists.

This is best-effort metadata persistence: callers should treat errors as non-fatal to the main agent flow.

Source

pub fn usage_records(&self) -> Vec<SessionUsageRecord>

Read canonical usage rows attached to this session.

Source

pub fn append_checkpoint_record( &mut self, record: SessionCheckpointRecord, ) -> Result<String>

Source

pub fn checkpoint_records(&self) -> Vec<SessionCheckpointRecord>

Source

pub fn find_checkpoint_record( &self, needle: &str, ) -> Option<SessionCheckpointRecord>

Source

pub fn restore_checkpoint( &self, checkpoint_state: &CheckpointState, needle: &str, ) -> Result<Vec<PathBuf>>

Source

pub fn has_canonical_usage_request_id(&self, request_id: &str) -> bool

Check whether a canonical usage record already exists for the given request id.

Source

pub fn has_canonical_usage_for_assistant_message( &self, assistant_message_id: &str, ) -> bool

Check whether a canonical usage record already exists for the given assistant turn.

Source

pub fn get_branch(&self) -> Vec<&SessionEntry>

Walk parent_ids from leaf_id to root, return raw entries in chronological order.

This is the durable branch as persisted on disk. It may include SessionEntry::Compaction markers plus raw pre-compaction messages. Callers building model-visible context should prefer get_active_messages().

Source

pub fn get_messages(&self) -> Vec<&Message>

Get raw message entries for the current branch.

This reflects the durable branch exactly and intentionally ignores compaction semantics. For model-visible history after a compaction, prefer get_active_messages().

Source

pub fn latest_compaction(&self) -> Option<&SessionEntry>

Return the latest compaction entry on the active branch, if any.

Source

pub fn get_active_messages(&self) -> Vec<Message>

Build the model-visible message history for the active branch.

Compaction semantics are branch-local and replacement-based:

  • if there is no compaction entry on the branch, this returns the raw branch messages;
  • if a compaction entry exists, all raw messages before that boundary are replaced by a synthetic user summary message derived from the latest compaction entry, followed by the raw messages from first_kept_id onward that are still on the active branch.

Raw persisted entries remain intact on disk and are still available via get_branch() / get_messages().

Source

pub fn active_message_count(&self) -> usize

Get the active model-visible branch entries.

This is a convenience wrapper over get_active_messages() for callers that still want borrowed-like iteration semantics at the message layer.

Source

pub fn get_tree(&self) -> Vec<TreeNode>

Build the full tree structure from all entries.

Source

pub fn navigate(&mut self, target_id: &str) -> Result<()>

Change the current position in the tree to a different entry.

Source

pub fn fork(&self, entry_id: &str, new_path: &Path) -> Result<SessionManager>

Create a new session file containing only entries up to (and including) the given entry_id, following its branch from root.

Source

pub fn entries(&self) -> &[SessionEntry]

Get all entries.

Source

pub fn path(&self) -> Option<&Path>

Get the session file path.

Source

pub fn leaf_id(&self) -> Option<&str>

Get the current leaf id.

Source

pub fn session_id(&self) -> Option<String>

Get the stable session id derived from the persisted file name, if any.

Source

pub fn list(session_dir: &Path) -> Result<Vec<SessionInfo>>

List available sessions in a directory.

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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: 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> Same for T

Source§

type Output = T

Should always be Self
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