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 index;
74pub mod index_produce;
75pub mod journal;
76pub mod listing;
77mod listing_all;
78pub mod oracle;
79pub mod oracle_replay;
80pub mod pages;
81pub mod relevance;
82pub mod tasks;
83
84pub use self::bus::{DurableSink, NoopSink, SessionBus};
85pub use self::codex_import::{
86    import_codex_session_by_id, import_codex_sessions_for_directory, load_or_import_session,
87};
88pub use self::context::{DerivedContext, derive_context, derive_with_policy, effective_policy};
89pub use self::delegation::{BetaPosterior, DelegationConfig, DelegationState};
90pub use self::derive_policy::DerivePolicy;
91pub use self::eval::{PolicyRunResult, pareto_frontier, reuse_rate};
92pub use self::event_compaction::{
93    CompactionFailure, CompactionOutcome, CompactionStart, ContextTruncation, FallbackStrategy,
94};
95pub use self::event_rlm::{RlmCompletion, RlmOutcome, RlmProgressEvent, RlmSubcallFallback};
96pub use self::event_token::{TokenDelta, TokenEstimate, TokenSource};
97pub use self::events::{SessionEvent, SessionResult};
98pub use self::faults::Fault;
99pub use self::history::History;
100pub use self::history_sink::{HistorySinkConfig, PointerHandle};
101pub use self::index::{Granularity, SummaryIndex, SummaryNode, SummaryRange};
102pub use self::journal::{JournalEntry, Op, RejectReason, TxnId, WritebackJournal};
103pub use self::listing::{SessionSummary, list_sessions};
104pub use self::listing_all::list_all_sessions_for_directory;
105pub use self::oracle::{OracleReport, replay_oracle};
106pub use self::pages::{PageKind, ResidencyLevel};
107pub use self::relevance::{
108    Bucket, Dependency, Difficulty, RelevanceMeta, ToolUse, bucket_for_messages,
109};
110pub use self::tail_load::TailLoad;
111pub use self::tasks::{TaskEvent, TaskLog, TaskState, TaskStatus};
112pub use self::types::{DEFAULT_MAX_STEPS, ImageAttachment, Session, SessionMetadata};
113
114#[cfg(test)]
115mod tests;