From 8bbcbdc0edbae74e976d7be0cc36e3bc85984fc3 Mon Sep 17 00:00:00 2001 From: jimmyfraiture Date: Wed, 24 Sep 2025 14:26:13 +0100 Subject: [PATCH] V6 --- codex-rs/core/src/state/session.rs | 14 +---- codex-rs/core/src/state/turn.rs | 86 +++++++++++++++++++----------- 2 files changed, 56 insertions(+), 44 deletions(-) diff --git a/codex-rs/core/src/state/session.rs b/codex-rs/core/src/state/session.rs index 00ff07fa4f..11132aa83c 100644 --- a/codex-rs/core/src/state/session.rs +++ b/codex-rs/core/src/state/session.rs @@ -11,6 +11,7 @@ use crate::protocol::RateLimitSnapshot; use crate::protocol::ReviewDecision; use crate::protocol::TokenUsageInfo; +#[derive(Default)] pub(crate) struct SessionState { pub(crate) approved_commands: HashSet>, pub(crate) pending_approvals: HashMap>, @@ -33,16 +34,3 @@ impl SessionState { self.readiness_queue.clear(); } } - -impl Default for SessionState { - fn default() -> Self { - Self { - approved_commands: HashSet::new(), - pending_approvals: HashMap::new(), - history: ConversationHistory::new(), - token_info: None, - latest_rate_limits: None, - readiness_queue: VecDeque::new(), - } - } -} diff --git a/codex-rs/core/src/state/turn.rs b/codex-rs/core/src/state/turn.rs index ee60ad35c5..61e8a709f7 100644 --- a/codex-rs/core/src/state/turn.rs +++ b/codex-rs/core/src/state/turn.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::collections::VecDeque; use std::path::PathBuf; use std::sync::Arc; @@ -19,6 +18,8 @@ use crate::turn_diff_tracker::TurnDiffTracker; use codex_protocol::models::ResponseInputItem; use codex_protocol::models::ResponseItem; +use std::collections::VecDeque; + #[derive(Debug)] pub(crate) struct TurnContext { pub(crate) client: ModelClient, @@ -44,10 +45,57 @@ impl TurnContext { } } -struct TurnRuntime { +struct TurnMailbox { initial_input: Option, - current_readiness: Option>, + latest_readiness: Option>, pending: VecDeque, +} + +impl TurnMailbox { + fn new( + initial_input: Option, + readiness: Option>, + ) -> Self { + Self { + initial_input, + latest_readiness: readiness, + pending: VecDeque::new(), + } + } + + fn take_initial_input(&mut self) -> Option { + self.initial_input.take() + } + + fn enqueue(&mut self, items: Vec, readiness: Option>) { + if items.is_empty() && readiness.is_none() { + return; + } + + if let Some(flag) = readiness { + self.latest_readiness = Some(flag); + } + + if items.is_empty() { + return; + } + + self.pending.push_back(items.into()); + } + + fn drain(&mut self) -> (Vec, Option>) { + let items = self + .pending + .drain(..) + .map(ResponseItem::from) + .collect::>(); + let readiness = self.latest_readiness.clone(); + (items, readiness) + } +} + +struct TurnRuntime { + mailbox: TurnMailbox, review_history: Vec, last_agent_message: Option, auto_compact_recently_attempted: bool, @@ -60,9 +108,7 @@ impl TurnRuntime { readiness: Option>, ) -> Self { Self { - initial_input, - current_readiness: readiness, - pending: VecDeque::new(), + mailbox: TurnMailbox::new(initial_input, readiness), review_history: Vec::new(), last_agent_message: None, auto_compact_recently_attempted: false, @@ -107,18 +153,12 @@ impl TurnState { pub(crate) async fn take_initial_input(&self) -> Option { let mut runtime = self.runtime.lock().await; - runtime.initial_input.take() + runtime.mailbox.take_initial_input() } pub(crate) async fn drain_mailbox(&self) -> (Vec, Option>) { let mut runtime = self.runtime.lock().await; - let items = runtime - .pending - .drain(..) - .map(ResponseItem::from) - .collect::>(); - let readiness = runtime.current_readiness.clone(); - (items, readiness) + runtime.mailbox.drain() } pub(crate) async fn enqueue_user_input( @@ -126,24 +166,8 @@ impl TurnState { items: Vec, readiness: Option>, ) { - if readiness.is_some() { - let mut runtime = self.runtime.lock().await; - runtime.current_readiness = readiness; - if items.is_empty() { - return; - } - let response: ResponseInputItem = items.into(); - runtime.pending.push_back(response); - return; - } - - if items.is_empty() { - return; - } - let mut runtime = self.runtime.lock().await; - let response: ResponseInputItem = items.into(); - runtime.pending.push_back(response); + runtime.mailbox.enqueue(items, readiness); } pub(crate) async fn set_review_history(&self, history: Vec) {