Skip to main content

claude_agent/session/state/
mod.rs

1//! Session state management.
2
3mod config;
4mod enums;
5mod ids;
6mod message;
7mod policy;
8
9pub use config::SessionConfig;
10pub use enums::{SessionMode, SessionState, SessionType};
11pub use ids::{MessageId, SessionId};
12pub use message::{MessageMetadata, SessionMessage, ThinkingMetadata, ToolResultMeta};
13pub use policy::{PermissionMode, PermissionPolicy, ToolLimits};
14
15use std::collections::HashMap;
16
17use chrono::{DateTime, Utc};
18use serde::{Deserialize, Serialize};
19
20use crate::session::types::{CompactRecord, Plan, TodoItem, TodoStatus};
21use crate::types::{CacheControl, CacheTtl, ContentBlock, Message, Role, TokenUsage, Usage};
22
23const MAX_COMPACT_HISTORY_SIZE: usize = 50;
24
25#[derive(Clone, Debug, Serialize, Deserialize)]
26pub struct Session {
27    pub id: SessionId,
28    pub parent_id: Option<SessionId>,
29    pub session_type: SessionType,
30    pub tenant_id: Option<String>,
31    pub mode: SessionMode,
32    pub state: SessionState,
33    pub config: SessionConfig,
34    pub permission_policy: PermissionPolicy,
35    pub messages: Vec<SessionMessage>,
36    pub current_leaf_id: Option<MessageId>,
37    pub summary: Option<String>,
38    pub total_usage: TokenUsage,
39    #[serde(default)]
40    pub current_input_tokens: u64,
41    pub total_cost_usd: f64,
42    pub static_context_hash: Option<String>,
43    pub created_at: DateTime<Utc>,
44    pub updated_at: DateTime<Utc>,
45    pub expires_at: Option<DateTime<Utc>>,
46    pub error: Option<String>,
47    #[serde(default)]
48    pub todos: Vec<TodoItem>,
49    #[serde(default)]
50    pub current_plan: Option<Plan>,
51    #[serde(default)]
52    pub compact_history: Vec<CompactRecord>,
53}
54
55impl Session {
56    pub fn new(config: SessionConfig) -> Self {
57        Self::with_id(SessionId::new(), config)
58    }
59
60    pub fn with_id(id: SessionId, config: SessionConfig) -> Self {
61        Self::init(id, None, SessionType::Main, config)
62    }
63
64    pub fn new_subagent(
65        parent_id: SessionId,
66        agent_type: impl Into<String>,
67        description: impl Into<String>,
68        config: SessionConfig,
69    ) -> Self {
70        let session_type = SessionType::Subagent {
71            agent_type: agent_type.into(),
72            description: description.into(),
73        };
74        Self::init(SessionId::new(), Some(parent_id), session_type, config)
75    }
76
77    fn init(
78        id: SessionId,
79        parent_id: Option<SessionId>,
80        session_type: SessionType,
81        config: SessionConfig,
82    ) -> Self {
83        let now = Utc::now();
84        let expires_at = config
85            .ttl_secs
86            .map(|ttl| now + chrono::Duration::seconds(ttl as i64));
87
88        Self {
89            id,
90            parent_id,
91            session_type,
92            tenant_id: None,
93            mode: config.mode,
94            state: SessionState::Created,
95            permission_policy: config.permission_policy.clone(),
96            config,
97            messages: Vec::with_capacity(32),
98            current_leaf_id: None,
99            summary: None,
100            total_usage: TokenUsage::default(),
101            current_input_tokens: 0,
102            total_cost_usd: 0.0,
103            static_context_hash: None,
104            created_at: now,
105            updated_at: now,
106            expires_at,
107            error: None,
108            todos: Vec::with_capacity(8),
109            current_plan: None,
110            compact_history: Vec::new(),
111        }
112    }
113
114    pub fn is_subagent(&self) -> bool {
115        matches!(self.session_type, SessionType::Subagent { .. })
116    }
117
118    pub fn is_running(&self) -> bool {
119        matches!(
120            self.state,
121            SessionState::Active | SessionState::WaitingForTools
122        )
123    }
124
125    pub fn is_terminal(&self) -> bool {
126        matches!(
127            self.state,
128            SessionState::Completed | SessionState::Failed | SessionState::Cancelled
129        )
130    }
131
132    pub fn is_expired(&self) -> bool {
133        self.expires_at.is_some_and(|expires| Utc::now() > expires)
134    }
135
136    pub fn add_message(&mut self, mut message: SessionMessage) {
137        if let Some(leaf) = &self.current_leaf_id {
138            message.parent_id = Some(leaf.clone());
139        }
140        self.current_leaf_id = Some(message.id.clone());
141        if let Some(usage) = &message.usage {
142            self.total_usage.add(usage);
143        }
144        self.messages.push(message);
145        self.updated_at = Utc::now();
146    }
147
148    pub fn current_branch(&self) -> Vec<&SessionMessage> {
149        let index: HashMap<&MessageId, &SessionMessage> =
150            self.messages.iter().map(|m| (&m.id, m)).collect();
151
152        let mut result = Vec::new();
153        let mut current_id = self.current_leaf_id.as_ref();
154
155        while let Some(id) = current_id {
156            if let Some(&msg) = index.get(id) {
157                result.push(msg);
158                current_id = msg.parent_id.as_ref();
159            } else {
160                break;
161            }
162        }
163
164        result.reverse();
165        result
166    }
167
168    /// Convert session messages to API format with default caching (5m TTL).
169    pub fn to_api_messages(&self) -> Vec<Message> {
170        self.to_api_messages_with_cache(Some(CacheTtl::FiveMinutes))
171    }
172
173    /// Convert session messages to API format with optional caching.
174    ///
175    /// Per Anthropic best practices, caches the last user message with the specified TTL.
176    /// Pass `None` to disable caching.
177    pub fn to_api_messages_with_cache(&self, ttl: Option<CacheTtl>) -> Vec<Message> {
178        let branch = self.current_branch();
179        if branch.is_empty() {
180            return Vec::new();
181        }
182
183        let mut messages: Vec<Message> = branch.iter().map(|m| m.to_api_message()).collect();
184
185        if let Some(ttl) = ttl {
186            self.apply_cache_breakpoint(&mut messages, ttl);
187        }
188
189        messages
190    }
191
192    /// Apply cache breakpoint to the last user message.
193    ///
194    /// Per Anthropic best practices for multi-turn conversations,
195    /// only the last user message needs cache_control to enable
196    /// caching of the entire conversation history before it.
197    fn apply_cache_breakpoint(&self, messages: &mut [Message], ttl: CacheTtl) {
198        let last_user_idx = messages
199            .iter()
200            .enumerate()
201            .rev()
202            .find(|(_, m)| m.role == Role::User)
203            .map(|(i, _)| i);
204
205        if let Some(idx) = last_user_idx {
206            messages[idx].set_cache_on_last_block(CacheControl::ephemeral().with_ttl(ttl));
207        }
208    }
209
210    pub fn set_state(&mut self, state: SessionState) {
211        self.state = state;
212        self.updated_at = Utc::now();
213    }
214
215    pub fn set_todos(&mut self, todos: Vec<TodoItem>) {
216        self.todos = todos;
217        self.updated_at = Utc::now();
218    }
219
220    pub fn todos_in_progress_count(&self) -> usize {
221        self.todos
222            .iter()
223            .filter(|t| t.status == TodoStatus::InProgress)
224            .count()
225    }
226
227    pub fn enter_plan_mode(&mut self, name: Option<String>) -> &Plan {
228        let mut plan = Plan::new(self.id);
229        if let Some(n) = name {
230            plan = plan.with_name(n);
231        }
232        self.updated_at = Utc::now();
233        self.current_plan.insert(plan)
234    }
235
236    pub fn update_plan_content(&mut self, content: String) {
237        if let Some(ref mut plan) = self.current_plan {
238            plan.content = content;
239            self.updated_at = Utc::now();
240        }
241    }
242
243    pub fn exit_plan_mode(&mut self) -> Option<Plan> {
244        if let Some(ref mut plan) = self.current_plan {
245            plan.approve();
246            self.updated_at = Utc::now();
247        }
248        self.current_plan.take()
249    }
250
251    pub fn cancel_plan(&mut self) -> Option<Plan> {
252        if let Some(ref mut plan) = self.current_plan {
253            plan.cancel();
254            self.updated_at = Utc::now();
255        }
256        self.current_plan.take()
257    }
258
259    pub fn is_in_plan_mode(&self) -> bool {
260        self.current_plan
261            .as_ref()
262            .is_some_and(|p| !p.status.is_terminal())
263    }
264
265    pub fn record_compact(&mut self, record: CompactRecord) {
266        if self.compact_history.len() >= MAX_COMPACT_HISTORY_SIZE {
267            self.compact_history.remove(0);
268        }
269        self.compact_history.push(record);
270        self.updated_at = Utc::now();
271    }
272
273    pub fn update_summary(&mut self, summary: impl Into<String>) {
274        self.summary = Some(summary.into());
275        self.updated_at = Utc::now();
276    }
277
278    pub fn add_user_message(&mut self, content: impl Into<String>) {
279        let msg = SessionMessage::user(vec![ContentBlock::text(content.into())]);
280        self.add_message(msg);
281    }
282
283    pub fn add_assistant_message(&mut self, content: Vec<ContentBlock>, usage: Option<Usage>) {
284        let mut msg = SessionMessage::assistant(content);
285        if let Some(u) = usage {
286            msg = msg.with_usage(TokenUsage {
287                input_tokens: u.input_tokens as u64,
288                output_tokens: u.output_tokens as u64,
289                cache_read_input_tokens: u.cache_read_input_tokens.unwrap_or(0) as u64,
290                cache_creation_input_tokens: u.cache_creation_input_tokens.unwrap_or(0) as u64,
291            });
292        }
293        self.add_message(msg);
294    }
295
296    pub fn add_tool_results(&mut self, results: Vec<crate::types::ToolResultBlock>) {
297        let content: Vec<ContentBlock> =
298            results.into_iter().map(ContentBlock::ToolResult).collect();
299        let msg = SessionMessage::user(content);
300        self.add_message(msg);
301    }
302
303    pub fn should_compact(&self, max_tokens: u64, threshold: f32, keep_messages: usize) -> bool {
304        self.messages.len() > keep_messages
305            && self.current_input_tokens as f32 > max_tokens as f32 * threshold
306    }
307
308    pub fn update_usage(&mut self, usage: &Usage) {
309        self.current_input_tokens = usage.context_usage() as u64;
310        self.total_usage.add_usage(usage);
311    }
312
313    pub async fn compact(
314        &mut self,
315        client: &crate::Client,
316        keep_messages: usize,
317    ) -> crate::Result<crate::types::CompactResult> {
318        use crate::client::ModelType;
319        use crate::client::messages::CreateMessageRequest;
320        use crate::types::CompactResult;
321
322        if self.messages.len() <= keep_messages {
323            return Ok(CompactResult::NotNeeded);
324        }
325
326        let tokens_before = self.current_input_tokens;
327        let split_point = self.messages.len() - keep_messages;
328        let to_summarize: Vec<_> = self.messages[..split_point].to_vec();
329        let to_keep: Vec<_> = self.messages[split_point..].to_vec();
330
331        let summary_prompt = Self::format_for_summary(&to_summarize);
332        let model = client.adapter().model(ModelType::Small).to_string();
333        let request = CreateMessageRequest::new(&model, vec![Message::user(&summary_prompt)])
334            .with_max_tokens(2000);
335        let response = client.send(request).await?;
336        let summary = response.text();
337
338        let original_count = self.messages.len();
339
340        self.messages.clear();
341        self.current_leaf_id = None;
342
343        let summary_msg = SessionMessage::user(vec![ContentBlock::text(format!(
344            "[Previous conversation summary]\n{}",
345            summary
346        ))])
347        .as_compact_summary();
348        self.add_message(summary_msg);
349
350        for mut msg in to_keep {
351            msg.parent_id = self.current_leaf_id.clone();
352            self.current_leaf_id = Some(msg.id.clone());
353            self.messages.push(msg);
354        }
355
356        // Reset to 0: actual value will be set by next API call's update_usage().
357        // This also prevents immediate re-compaction since should_compact() returns false when 0.
358        self.current_input_tokens = 0;
359        self.summary = Some(summary.clone());
360        self.updated_at = Utc::now();
361
362        let record = CompactRecord::new(self.id)
363            .with_counts(original_count, self.messages.len())
364            .with_summary(summary.clone())
365            .with_saved_tokens(tokens_before as usize);
366        self.record_compact(record);
367
368        Ok(CompactResult::Compacted {
369            original_count,
370            new_count: self.messages.len(),
371            saved_tokens: tokens_before as usize,
372            summary,
373        })
374    }
375
376    fn format_for_summary(messages: &[SessionMessage]) -> String {
377        let estimated_capacity = messages.len() * 500 + 200;
378        let mut formatted = String::with_capacity(estimated_capacity.min(32768));
379        formatted.push_str(
380            "Summarize this conversation concisely. \
381             Preserve key decisions, code changes, file paths, and important context:\n\n",
382        );
383
384        for msg in messages {
385            let role = match msg.role {
386                Role::User => "User",
387                Role::Assistant => "Assistant",
388            };
389            formatted.push_str(role);
390            formatted.push_str(":\n");
391
392            for block in &msg.content {
393                if let Some(text) = block.as_text() {
394                    if text.len() > 800 {
395                        formatted.push_str(&text[..800]);
396                        formatted.push_str("... [truncated]\n");
397                    } else {
398                        formatted.push_str(text);
399                        formatted.push('\n');
400                    }
401                }
402            }
403            formatted.push('\n');
404        }
405
406        formatted
407    }
408
409    pub fn clear_messages(&mut self) {
410        self.messages.clear();
411        self.current_leaf_id = None;
412        self.updated_at = Utc::now();
413    }
414}
415
416#[cfg(test)]
417mod tests {
418    use super::*;
419    use crate::types::{ContentBlock, Role};
420
421    #[test]
422    fn test_session_creation() {
423        let config = SessionConfig::default();
424        let session = Session::new(config);
425
426        assert_eq!(session.state, SessionState::Created);
427        assert!(session.messages.is_empty());
428        assert!(session.current_leaf_id.is_none());
429    }
430
431    #[test]
432    fn test_add_message() {
433        let mut session = Session::new(SessionConfig::default());
434
435        let msg1 = SessionMessage::user(vec![ContentBlock::text("Hello")]);
436        session.add_message(msg1);
437
438        assert_eq!(session.messages.len(), 1);
439        assert!(session.current_leaf_id.is_some());
440    }
441
442    #[test]
443    fn test_message_tree() {
444        let mut session = Session::new(SessionConfig::default());
445
446        let user_msg = SessionMessage::user(vec![ContentBlock::text("Hello")]);
447        session.add_message(user_msg);
448
449        let assistant_msg = SessionMessage::assistant(vec![ContentBlock::text("Hi there!")]);
450        session.add_message(assistant_msg);
451
452        let branch = session.current_branch();
453        assert_eq!(branch.len(), 2);
454        assert_eq!(branch[0].role, Role::User);
455        assert_eq!(branch[1].role, Role::Assistant);
456    }
457
458    #[test]
459    fn test_session_expiry() {
460        let config = SessionConfig {
461            ttl_secs: Some(0),
462            ..Default::default()
463        };
464        let session = Session::new(config);
465
466        std::thread::sleep(std::time::Duration::from_millis(10));
467        assert!(session.is_expired());
468    }
469
470    #[test]
471    fn test_token_usage_accumulation() {
472        let mut session = Session::new(SessionConfig::default());
473
474        let msg1 = SessionMessage::assistant(vec![ContentBlock::text("Response 1")]).with_usage(
475            TokenUsage {
476                input_tokens: 100,
477                output_tokens: 50,
478                ..Default::default()
479            },
480        );
481        session.add_message(msg1);
482
483        let msg2 = SessionMessage::assistant(vec![ContentBlock::text("Response 2")]).with_usage(
484            TokenUsage {
485                input_tokens: 150,
486                output_tokens: 75,
487                ..Default::default()
488            },
489        );
490        session.add_message(msg2);
491
492        assert_eq!(session.total_usage.input_tokens, 250);
493        assert_eq!(session.total_usage.output_tokens, 125);
494    }
495
496    #[test]
497    fn test_compact_history_limit() {
498        let mut session = Session::new(SessionConfig::default());
499
500        for i in 0..MAX_COMPACT_HISTORY_SIZE + 10 {
501            let record = CompactRecord::new(session.id).with_summary(format!("Summary {}", i));
502            session.record_compact(record);
503        }
504
505        assert_eq!(session.compact_history.len(), MAX_COMPACT_HISTORY_SIZE);
506        assert!(session.compact_history[0].summary.contains("10"));
507    }
508
509    #[test]
510    fn test_exit_plan_mode_takes_ownership() {
511        let mut session = Session::new(SessionConfig::default());
512        session.enter_plan_mode(Some("Test Plan".to_string()));
513
514        let plan = session.exit_plan_mode();
515        assert!(plan.is_some());
516        assert!(session.current_plan.is_none());
517    }
518
519    #[test]
520    fn test_message_caching_applies_to_last_user_turn() {
521        let mut session = Session::new(SessionConfig::default());
522
523        session.add_user_message("First question");
524        session.add_message(SessionMessage::assistant(vec![ContentBlock::text(
525            "First answer",
526        )]));
527        session.add_user_message("Second question");
528
529        let messages = session.to_api_messages();
530
531        assert_eq!(messages.len(), 3);
532        assert!(!messages[0].has_cache_control());
533        assert!(!messages[1].has_cache_control());
534        assert!(messages[2].has_cache_control());
535    }
536
537    #[test]
538    fn test_message_caching_disabled() {
539        let mut session = Session::new(SessionConfig::default());
540
541        session.add_user_message("Question");
542
543        // Pass None to disable caching
544        let messages = session.to_api_messages_with_cache(None);
545
546        assert_eq!(messages.len(), 1);
547        assert!(!messages[0].has_cache_control());
548    }
549
550    #[test]
551    fn test_message_caching_empty_session() {
552        let session = Session::new(SessionConfig::default());
553        let messages = session.to_api_messages();
554        assert!(messages.is_empty());
555    }
556
557    #[test]
558    fn test_message_caching_assistant_only() {
559        let mut session = Session::new(SessionConfig::default());
560        session.add_message(SessionMessage::assistant(vec![ContentBlock::text("Hi")]));
561
562        let messages = session.to_api_messages();
563
564        assert_eq!(messages.len(), 1);
565        assert!(!messages[0].has_cache_control());
566    }
567}