From cdcab36a493a215ba5e55bbbb3d6ba69bb777642 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Mon, 1 Jun 2026 14:11:48 +0100 Subject: [PATCH] wip --- codex-rs/core/src/codex_thread.rs | 18 ++++++++++++++++++ codex-rs/core/src/session/inject.rs | 21 +++++++++++++++++++++ codex-rs/core/src/session/tests.rs | 18 ++++++++++++++++++ codex-rs/ext/goal/src/runtime.rs | 28 +++++++++++++++++++++++++++- 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/codex-rs/core/src/codex_thread.rs b/codex-rs/core/src/codex_thread.rs index 7a6bc69c5c..39c0d1dc31 100644 --- a/codex-rs/core/src/codex_thread.rs +++ b/codex-rs/core/src/codex_thread.rs @@ -44,6 +44,7 @@ use codex_utils_absolute_path::AbsolutePathBuf; use rmcp::model::ReadResourceRequestParams; use std::collections::BTreeMap; use std::collections::HashMap; +use std::future::Future; use std::path::PathBuf; use std::sync::Arc; use tokio::sync::Mutex; @@ -286,6 +287,23 @@ impl CodexThread { self.codex.session.try_start_turn_if_idle(items).await } + /// Starts a regular turn with model-visible items only if the thread is idle + /// and the caller's validation still says the synthetic work is current. + pub async fn try_start_turn_if_idle_if_current( + &self, + items: Vec, + validate: F, + ) -> Result<(), Vec> + where + F: FnOnce() -> Fut + Send, + Fut: Future + Send, + { + self.codex + .session + .try_start_turn_if_idle_if_current(items, validate) + .await + } + pub async fn set_app_server_client_info( &self, app_server_client_name: Option, diff --git a/codex-rs/core/src/session/inject.rs b/codex-rs/core/src/session/inject.rs index 64d4aaade1..d9f01cf2b2 100644 --- a/codex-rs/core/src/session/inject.rs +++ b/codex-rs/core/src/session/inject.rs @@ -5,6 +5,7 @@ use crate::state::ActiveTurn; use crate::state::TurnState; use crate::tasks::RegularTask; use codex_protocol::models::ResponseItem; +use std::future::Future; use std::sync::Arc; impl Session { @@ -37,6 +38,21 @@ impl Session { self: &Arc, input: Vec, ) -> Result<(), Vec> { + self.try_start_turn_if_idle_if_current(input, || async { true }) + .await + } + + /// Starts a regular turn with the provided items only if the session is idle + /// and the caller's validation still says the synthetic work is current. + pub(crate) async fn try_start_turn_if_idle_if_current( + self: &Arc, + input: Vec, + validate: F, + ) -> Result<(), Vec> + where + F: FnOnce() -> Fut + Send, + Fut: Future + Send, + { if input.is_empty() { return Ok(()); } @@ -59,6 +75,11 @@ impl Session { return Err(input); } + if !validate().await { + self.clear_reserved_idle_turn(&turn_state).await; + return Err(input); + } + let turn_context = self .new_default_turn_with_sub_id(uuid::Uuid::new_v4().to_string()) .await; diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index 953f830f4c..4ae298ed7e 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -8448,6 +8448,24 @@ async fn try_start_turn_if_idle_rejects_active_turn_without_injecting() { sess.abort_all_tasks(TurnAbortReason::Interrupted).await; } +#[tokio::test] +async fn try_start_turn_if_idle_if_current_clears_reservation_when_validation_fails() { + let (sess, _tc, _rx) = make_session_and_context_with_rx().await; + + let item = user_message("synthetic idle input"); + let err = sess + .try_start_turn_if_idle_if_current(vec![item.clone()], || async { false }) + .await + .expect_err("stale idle input should be rejected"); + + assert_eq!(vec![item], err); + assert!(sess.active_turn.lock().await.is_none()); + assert_eq!( + Vec::::new(), + sess.input_queue.get_pending_input(&sess.active_turn).await + ); +} + #[tokio::test] async fn steer_input_requires_active_turn() { let (sess, _tc, _rx) = make_session_and_context_with_rx().await; diff --git a/codex-rs/ext/goal/src/runtime.rs b/codex-rs/ext/goal/src/runtime.rs index 8d79391c95..fbe0722072 100644 --- a/codex-rs/ext/goal/src/runtime.rs +++ b/codex-rs/ext/goal/src/runtime.rs @@ -297,6 +297,7 @@ impl GoalRuntimeHandle { return Ok(()); } + let goal_id = goal.goal_id.clone(); let item = continuation_steering_item(&protocol_goal_from_state(goal)); let Some(thread_manager) = self.inner.thread_manager.upgrade() else { tracing::debug!("skipping goal continuation because thread manager is unavailable"); @@ -307,7 +308,32 @@ impl GoalRuntimeHandle { return Ok(()); }; - if thread.try_start_turn_if_idle(vec![item]).await.is_err() { + let state_dbs = Arc::clone(&self.inner.state_dbs); + let thread_id = self.inner.thread_id; + if thread + .try_start_turn_if_idle_if_current(vec![item], move || async move { + match state_dbs.thread_goals().get_thread_goal(thread_id).await { + Ok(Some(goal)) + if goal.goal_id == goal_id + && goal.status == codex_state::ThreadGoalStatus::Active => + { + true + } + Ok(Some(_)) | Ok(None) => { + tracing::debug!( + "skipping active goal continuation because the goal changed before launch" + ); + false + } + Err(err) => { + tracing::warn!("failed to re-read thread goal before continuation: {err}"); + false + } + } + }) + .await + .is_err() + { tracing::debug!("skipping goal continuation because the thread is no longer idle"); }