mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
wip
This commit is contained in:
@@ -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<F, Fut>(
|
||||
&self,
|
||||
items: Vec<ResponseItem>,
|
||||
validate: F,
|
||||
) -> Result<(), Vec<ResponseItem>>
|
||||
where
|
||||
F: FnOnce() -> Fut + Send,
|
||||
Fut: Future<Output = bool> + 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<String>,
|
||||
|
||||
@@ -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<Self>,
|
||||
input: Vec<ResponseItem>,
|
||||
) -> Result<(), Vec<ResponseItem>> {
|
||||
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<F, Fut>(
|
||||
self: &Arc<Self>,
|
||||
input: Vec<ResponseItem>,
|
||||
validate: F,
|
||||
) -> Result<(), Vec<ResponseItem>>
|
||||
where
|
||||
F: FnOnce() -> Fut + Send,
|
||||
Fut: Future<Output = bool> + 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;
|
||||
|
||||
@@ -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::<TurnInput>::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;
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user