//! Session-wide mutable state. use codex_protocol::models::ResponseItem; use std::collections::HashMap; use std::collections::HashSet; use crate::codex::SessionConfiguration; use crate::context_manager::ContextManager; use crate::protocol::RateLimitSnapshot; use crate::protocol::TokenUsage; use crate::protocol::TokenUsageInfo; use crate::tasks::RegularTask; use crate::truncate::TruncationPolicy; /// Persistent, session-scoped state previously stored directly on `Session`. pub(crate) struct SessionState { pub(crate) session_configuration: SessionConfiguration, pub(crate) history: ContextManager, pub(crate) latest_rate_limits: Option, pub(crate) server_reasoning_included: bool, pub(crate) dependency_env: HashMap, pub(crate) mcp_dependency_prompted: HashSet, /// Whether the session's initial context has been seeded into history. /// /// TODO(owen): This is a temporary solution to avoid updating a thread's updated_at /// timestamp when resuming a session. Remove this once SQLite is in place. pub(crate) initial_context_seeded: bool, /// Previous rollout model for one-shot model-switch handling on first turn after resume. pub(crate) pending_resume_previous_model: Option, /// Startup regular task pre-created during session initialization. pub(crate) startup_regular_task: Option, pub(crate) active_mcp_tool_selection: Option>, } impl SessionState { /// Create a new session state mirroring previous `State::default()` semantics. pub(crate) fn new(session_configuration: SessionConfiguration) -> Self { let history = ContextManager::new(); Self { session_configuration, history, latest_rate_limits: None, server_reasoning_included: false, dependency_env: HashMap::new(), mcp_dependency_prompted: HashSet::new(), initial_context_seeded: false, pending_resume_previous_model: None, startup_regular_task: None, active_mcp_tool_selection: None, } } // History helpers pub(crate) fn record_items(&mut self, items: I, policy: TruncationPolicy) where I: IntoIterator, I::Item: std::ops::Deref, { self.history.record_items(items, policy); } pub(crate) fn clone_history(&self) -> ContextManager { self.history.clone() } pub(crate) fn replace_history(&mut self, items: Vec) { self.history.replace(items); } pub(crate) fn set_token_info(&mut self, info: Option) { self.history.set_token_info(info); } // Token/rate limit helpers pub(crate) fn update_token_info_from_usage( &mut self, usage: &TokenUsage, model_context_window: Option, ) { self.history.update_token_info(usage, model_context_window); } pub(crate) fn token_info(&self) -> Option { self.history.token_info() } pub(crate) fn set_rate_limits(&mut self, snapshot: RateLimitSnapshot) { self.latest_rate_limits = Some(merge_rate_limit_fields( self.latest_rate_limits.as_ref(), snapshot, )); } pub(crate) fn token_info_and_rate_limits( &self, ) -> (Option, Option) { (self.token_info(), self.latest_rate_limits.clone()) } pub(crate) fn set_token_usage_full(&mut self, context_window: i64) { self.history.set_token_usage_full(context_window); } pub(crate) fn get_total_token_usage(&self, server_reasoning_included: bool) -> i64 { self.history .get_total_token_usage(server_reasoning_included) } pub(crate) fn set_server_reasoning_included(&mut self, included: bool) { self.server_reasoning_included = included; } pub(crate) fn server_reasoning_included(&self) -> bool { self.server_reasoning_included } pub(crate) fn record_mcp_dependency_prompted(&mut self, names: I) where I: IntoIterator, { self.mcp_dependency_prompted.extend(names); } pub(crate) fn mcp_dependency_prompted(&self) -> HashSet { self.mcp_dependency_prompted.clone() } pub(crate) fn set_dependency_env(&mut self, values: HashMap) { for (key, value) in values { self.dependency_env.insert(key, value); } } pub(crate) fn dependency_env(&self) -> HashMap { self.dependency_env.clone() } pub(crate) fn set_startup_regular_task(&mut self, task: RegularTask) { self.startup_regular_task = Some(task); } pub(crate) fn take_startup_regular_task(&mut self) -> Option { self.startup_regular_task.take() } pub(crate) fn merge_mcp_tool_selection(&mut self, tool_names: Vec) -> Vec { if tool_names.is_empty() { return self.active_mcp_tool_selection.clone().unwrap_or_default(); } let mut merged = self.active_mcp_tool_selection.take().unwrap_or_default(); let mut seen: HashSet = merged.iter().cloned().collect(); for tool_name in tool_names { if seen.insert(tool_name.clone()) { merged.push(tool_name); } } self.active_mcp_tool_selection = Some(merged.clone()); merged } pub(crate) fn get_mcp_tool_selection(&self) -> Option> { self.active_mcp_tool_selection.clone() } pub(crate) fn clear_mcp_tool_selection(&mut self) { self.active_mcp_tool_selection = None; } } // Merge partial rate-limit updates: new fields overwrite existing values; // missing fields retain prior values. If `limit_id` is absent everywhere, // default it to `"codex"`. fn merge_rate_limit_fields( previous: Option<&RateLimitSnapshot>, mut snapshot: RateLimitSnapshot, ) -> RateLimitSnapshot { if snapshot.limit_id.is_none() { snapshot.limit_id = previous .and_then(|prior| prior.limit_id.clone()) .or_else(|| Some("codex".to_string())); } if snapshot.limit_name.is_none() { snapshot.limit_name = previous.and_then(|prior| prior.limit_name.clone()); } if snapshot.credits.is_none() { snapshot.credits = previous.and_then(|prior| prior.credits.clone()); } if snapshot.plan_type.is_none() { snapshot.plan_type = previous.and_then(|prior| prior.plan_type); } snapshot } #[cfg(test)] mod tests { use super::*; use crate::codex::make_session_configuration_for_tests; use crate::protocol::RateLimitWindow; use pretty_assertions::assert_eq; #[tokio::test] async fn merge_mcp_tool_selection_deduplicates_and_preserves_order() { let session_configuration = make_session_configuration_for_tests().await; let mut state = SessionState::new(session_configuration); let merged = state.merge_mcp_tool_selection(vec![ "mcp__rmcp__echo".to_string(), "mcp__rmcp__image".to_string(), "mcp__rmcp__echo".to_string(), ]); assert_eq!( merged, vec![ "mcp__rmcp__echo".to_string(), "mcp__rmcp__image".to_string(), ] ); let merged = state.merge_mcp_tool_selection(vec![ "mcp__rmcp__image".to_string(), "mcp__rmcp__search".to_string(), ]); assert_eq!( merged, vec![ "mcp__rmcp__echo".to_string(), "mcp__rmcp__image".to_string(), "mcp__rmcp__search".to_string(), ] ); } #[tokio::test] async fn merge_mcp_tool_selection_empty_input_is_noop() { let session_configuration = make_session_configuration_for_tests().await; let mut state = SessionState::new(session_configuration); state.merge_mcp_tool_selection(vec![ "mcp__rmcp__echo".to_string(), "mcp__rmcp__image".to_string(), ]); let merged = state.merge_mcp_tool_selection(Vec::new()); assert_eq!( merged, vec![ "mcp__rmcp__echo".to_string(), "mcp__rmcp__image".to_string(), ] ); assert_eq!( state.get_mcp_tool_selection(), Some(vec![ "mcp__rmcp__echo".to_string(), "mcp__rmcp__image".to_string(), ]) ); } #[tokio::test] async fn clear_mcp_tool_selection_removes_selection() { let session_configuration = make_session_configuration_for_tests().await; let mut state = SessionState::new(session_configuration); state.merge_mcp_tool_selection(vec!["mcp__rmcp__echo".to_string()]); state.clear_mcp_tool_selection(); assert_eq!(state.get_mcp_tool_selection(), None); } #[tokio::test] async fn set_rate_limits_defaults_limit_id_to_codex_when_missing() { let session_configuration = make_session_configuration_for_tests().await; let mut state = SessionState::new(session_configuration); state.set_rate_limits(RateLimitSnapshot { limit_id: None, limit_name: None, primary: Some(RateLimitWindow { used_percent: 12.0, window_minutes: Some(60), resets_at: Some(100), }), secondary: None, credits: None, plan_type: None, }); assert_eq!( state .latest_rate_limits .as_ref() .and_then(|v| v.limit_id.clone()), Some("codex".to_string()) ); } #[tokio::test] async fn set_rate_limits_preserves_previous_limit_id_when_missing() { let session_configuration = make_session_configuration_for_tests().await; let mut state = SessionState::new(session_configuration); state.set_rate_limits(RateLimitSnapshot { limit_id: Some("codex_other".to_string()), limit_name: Some("codex_other".to_string()), primary: Some(RateLimitWindow { used_percent: 20.0, window_minutes: Some(60), resets_at: Some(200), }), secondary: None, credits: None, plan_type: None, }); state.set_rate_limits(RateLimitSnapshot { limit_id: None, limit_name: None, primary: Some(RateLimitWindow { used_percent: 30.0, window_minutes: Some(60), resets_at: Some(300), }), secondary: None, credits: None, plan_type: None, }); assert_eq!( state .latest_rate_limits .as_ref() .and_then(|v| v.limit_id.clone()), Some("codex_other".to_string()) ); } #[tokio::test] async fn set_rate_limits_accepts_new_limit_id_bucket() { let session_configuration = make_session_configuration_for_tests().await; let mut state = SessionState::new(session_configuration); state.set_rate_limits(RateLimitSnapshot { limit_id: Some("codex".to_string()), limit_name: Some("codex".to_string()), primary: Some(RateLimitWindow { used_percent: 10.0, window_minutes: Some(60), resets_at: Some(100), }), secondary: None, credits: Some(crate::protocol::CreditsSnapshot { has_credits: true, unlimited: false, balance: Some("50".to_string()), }), plan_type: Some(codex_protocol::account::PlanType::Plus), }); state.set_rate_limits(RateLimitSnapshot { limit_id: Some("codex_other".to_string()), limit_name: Some("codex_other".to_string()), primary: Some(RateLimitWindow { used_percent: 30.0, window_minutes: Some(120), resets_at: Some(200), }), secondary: None, credits: None, plan_type: None, }); assert_eq!( state.latest_rate_limits, Some(RateLimitSnapshot { limit_id: Some("codex_other".to_string()), limit_name: Some("codex_other".to_string()), primary: Some(RateLimitWindow { used_percent: 30.0, window_minutes: Some(120), resets_at: Some(200), }), secondary: None, credits: Some(crate::protocol::CreditsSnapshot { has_credits: true, unlimited: false, balance: Some("50".to_string()), }), plan_type: Some(codex_protocol::account::PlanType::Plus), }) ); } }