From 8c03179f7d10f8d68f154232bf09b75da4d1a662 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Mon, 19 Jan 2026 09:49:08 -0800 Subject: [PATCH] fix --- codex-rs/core/Cargo.toml | 2 +- codex-rs/core/src/codex.rs | 8 ++++++-- codex-rs/core/src/context_manager/history.rs | 19 +++++++------------ codex-rs/core/src/state/session.rs | 10 +--------- 4 files changed, 15 insertions(+), 24 deletions(-) diff --git a/codex-rs/core/Cargo.toml b/codex-rs/core/Cargo.toml index 8ee2848a98..e6b404cd8d 100644 --- a/codex-rs/core/Cargo.toml +++ b/codex-rs/core/Cargo.toml @@ -93,7 +93,7 @@ tracing = { workspace = true, features = ["log"] } tree-sitter = { workspace = true } tree-sitter-bash = { workspace = true } url = { workspace = true } -uuid = { workspace = true, features = ["serde", "v4", "v5"] } +uuid = { workspace = true, features = ["serde", "v4", "v5", "v7"] } which = { workspace = true } wildmatch = { workspace = true } diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 2cdc3bdc2a..785ce3e9f7 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -699,7 +699,7 @@ impl Session { .await .map(Arc::new); } - let state = SessionState::new(session_configuration.clone(), conversation_id); + let state = SessionState::new(session_configuration.clone()); let services = SessionServices { mcp_connection_manager: Arc::new(RwLock::new(McpConnectionManager::default())), @@ -1352,7 +1352,7 @@ impl Session { turn_context: &TurnContext, rollout_items: &[RolloutItem], ) -> Vec { - let mut history = ContextManager::new(); + let mut history = ContextManager::new(&self.codex_home().await); for item in rollout_items { match item { RolloutItem::ResponseItem(response_item) => { @@ -1571,6 +1571,10 @@ impl Session { self.send_event(turn_context, event).await; } + pub(crate) async fn codex_home(&self) -> PathBuf { + self.state.lock().await.session_configuration.codex_home().clone() + } + pub(crate) async fn set_total_tokens_full(&self, turn_context: &TurnContext) { if let Some(context_window) = turn_context.client.get_model_context_window() { let mut state = self.state.lock().await; diff --git a/codex-rs/core/src/context_manager/history.rs b/codex-rs/core/src/context_manager/history.rs index 26e5bf86cd..3dfd6c7f48 100644 --- a/codex-rs/core/src/context_manager/history.rs +++ b/codex-rs/core/src/context_manager/history.rs @@ -26,15 +26,15 @@ pub(crate) struct ContextManager { /// The oldest items are at the beginning of the vector. items: Vec, token_info: Option, - user_message_dir: Option, + codex_home: PathBuf, } impl ContextManager { - pub(crate) fn new() -> Self { + pub(crate) fn new(codex_home: &PathBuf) -> Self { Self { items: Vec::new(), token_info: TokenUsageInfo::new_or_append(&None, &None, None), - user_message_dir: None, + codex_home: codex_home.clone(), } } @@ -46,10 +46,6 @@ impl ContextManager { self.token_info = info; } - pub(crate) fn set_user_message_dir(&mut self, dir: Option) { - self.user_message_dir = dir; - } - pub(crate) fn set_token_usage_full(&mut self, context_window: i64) { match &mut self.token_info { Some(info) => info.fill_to_context_window(context_window), @@ -331,10 +327,9 @@ impl ContextManager { return item.clone(); } - let Some(user_message_dir) = self.user_message_dir.as_ref() else { - return item.clone(); - }; - let Some(path) = write_message_to_user_dir(&text, user_message_dir) else { + let id_str = Uuid::now_v7().to_string(); + let user_message_dir = self.codex_home.join("context").join("usermsgs").join(&id_str); + let Some(path) = write_message_to_user_dir(&text, &user_message_dir) else { return item.clone(); }; @@ -390,7 +385,7 @@ fn write_message_to_user_dir(text: &str, user_message_dir: &Path) -> Option Self { - let mut history = ContextManager::new(); - let user_message_dir = session_configuration - .codex_home() - .join("context") - .join("usermsgs") - .join(conversation_id.to_string()); - history.set_user_message_dir(Some(user_message_dir)); + let history = ContextManager::new(session_configuration.codex_home()); Self { session_configuration, history,