Skip to main content

codetether_agent/session/
mod.rs

1//! # Session Management
2//!
3//! A [`Session`] tracks the conversation history and execution state for a
4//! single agent interaction. It is the primary abstraction used by the CLI,
5//! TUI, HTTP server, and A2A worker to drive the agentic loop: send a user
6//! message, let the model call tools until it converges, and return the
7//! final answer.
8//!
9//! ## Quick Start
10//!
11//! ```rust,no_run
12//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
13//! use codetether_agent::session::Session;
14//!
15//! let mut session = Session::new().await.unwrap();
16//! let result = session.prompt("List the files in the current directory").await.unwrap();
17//! println!("Assistant: {}", result.text);
18//! println!("Session ID: {}", result.session_id);
19//! # });
20//! ```
21//!
22//! ## Architecture
23//!
24//! Sessions are stored as JSON files in the platform data directory
25//! (`~/.local/share/codetether/sessions` on Linux). Each session has a UUID,
26//! an ordered list of [`Message`](crate::provider::Message)s, a record of
27//! all [`ToolUse`](crate::agent::ToolUse) events, aggregated token
28//! [`Usage`](crate::provider::Usage), and a [`SessionMetadata`] block.
29//!
30//! ## Module Layout
31//!
32//! The [`Session`] type is implemented across several single-responsibility
33//! submodules and exposed here as a single facade:
34//!
35//! - [`types`] — [`Session`], [`SessionMetadata`], [`ImageAttachment`].
36//! - [`events`] — [`SessionResult`], [`SessionEvent`].
37//! - [`lifecycle`] — constructor, agent/provenance, message append.
38//! - [`persistence`] — save / load / delete / directory lookup.
39//! - [`title`] — title generation and context-change hook.
40//! - [`prompt_api`] — public [`prompt`](Session::prompt) entry points.
41//! - [`context`] — [`DerivedContext`] + [`derive_context`]: produce the
42//!   per-step LLM context from the append-only chat history without
43//!   mutating [`Session::messages`]. See the Phase A plan.
44//! - [`helper`] — the agentic loop implementation (non-public details).
45
46mod bus;
47mod event_compaction;
48mod event_rlm;
49mod event_token;
50mod events;
51mod header;
52pub(crate) mod history_files;
53mod lifecycle;
54mod persistence;
55mod prompt_api;
56mod tail_load;
57mod tail_seed;
58mod title;
59mod types;
60mod workspace_index;
61mod workspace_index_io;
62
63pub mod codex_import;
64pub mod context;
65pub mod delegation;
66pub mod delegation_skills;
67pub mod derive_policy;
68pub mod eval;
69pub mod faults;
70pub mod helper;
71pub mod history;
72pub mod history_sink;
73pub mod journal;
74pub mod listing;
75mod listing_all;
76pub mod oracle;
77pub mod pages;
78pub mod relevance;
79pub mod tasks;
80
81pub use self::bus::{DurableSink, NoopSink, SessionBus};
82pub use self::codex_import::{
83    import_codex_session_by_id, import_codex_sessions_for_directory, load_or_import_session,
84};
85pub use self::context::{DerivedContext, derive_context, derive_with_policy, effective_policy};
86pub use self::delegation::{BetaPosterior, DelegationConfig, DelegationState};
87pub use self::derive_policy::DerivePolicy;
88pub use self::eval::{PolicyRunResult, pareto_frontier, reuse_rate};
89pub use self::event_compaction::{
90    CompactionFailure, CompactionOutcome, CompactionStart, ContextTruncation, FallbackStrategy,
91};
92pub use self::event_rlm::{RlmCompletion, RlmOutcome, RlmProgressEvent, RlmSubcallFallback};
93pub use self::event_token::{TokenDelta, TokenEstimate, TokenSource};
94pub use self::events::{SessionEvent, SessionResult};
95pub use self::faults::Fault;
96pub use self::history::History;
97pub use self::history_sink::{HistorySinkConfig, PointerHandle};
98pub use self::journal::{JournalEntry, Op, RejectReason, TxnId, WritebackJournal};
99pub use self::listing::{SessionSummary, list_sessions};
100pub use self::listing_all::list_all_sessions_for_directory;
101pub use self::oracle::{OracleReport, replay_oracle};
102pub use self::pages::{PageKind, ResidencyLevel};
103pub use self::relevance::{
104    Bucket, Dependency, Difficulty, RelevanceMeta, ToolUse, bucket_for_messages,
105};
106pub use self::tail_load::TailLoad;
107pub use self::tasks::{TaskEvent, TaskLog, TaskState, TaskStatus};
108pub use self::types::{DEFAULT_MAX_STEPS, ImageAttachment, Session, SessionMetadata};
109
110#[cfg(test)]
111mod tests;