From 4089259f14a233dbe2c929b41c70000bba107d31 Mon Sep 17 00:00:00 2001 From: Adam Perry Date: Sun, 7 Jun 2026 02:03:51 +0000 Subject: [PATCH] test(core): make mixed tool overlap deterministic --- codex-rs/core/src/test_support.rs | 12 ++++ codex-rs/core/src/tools/handlers/test_sync.rs | 35 +++++++--- codex-rs/core/tests/suite/tool_parallelism.rs | 66 +++++++++++++++---- 3 files changed, 91 insertions(+), 22 deletions(-) diff --git a/codex-rs/core/src/test_support.rs b/codex-rs/core/src/test_support.rs index e491ca7413..7f1fc2b7ff 100644 --- a/codex-rs/core/src/test_support.rs +++ b/codex-rs/core/src/test_support.rs @@ -6,6 +6,7 @@ use std::path::PathBuf; use std::sync::Arc; +use std::time::Duration; use codex_exec_server::EnvironmentManager; use codex_login::AuthManager; @@ -25,6 +26,7 @@ use once_cell::sync::Lazy; use crate::ThreadManager; use crate::config::Config; use crate::thread_manager; +use crate::tools::handlers::TestSyncHandler; use crate::unified_exec; static TEST_MODEL_PRESETS: Lazy> = Lazy::new(|| { @@ -44,6 +46,16 @@ pub fn set_deterministic_process_ids(enabled: bool) { unified_exec::set_deterministic_process_ids_for_tests(enabled); } +pub async fn wait_on_test_sync_barrier( + id: &str, + participants: usize, + timeout: Duration, +) -> Result<(), String> { + TestSyncHandler::wait_on_barrier_for_tests(id, participants, timeout) + .await + .map_err(|err| err.to_string()) +} + pub fn auth_manager_from_auth(auth: CodexAuth) -> Arc { AuthManager::from_auth_for_testing(auth) } diff --git a/codex-rs/core/src/tools/handlers/test_sync.rs b/codex-rs/core/src/tools/handlers/test_sync.rs index 0e5fe894ad..c229dcdb8b 100644 --- a/codex-rs/core/src/tools/handlers/test_sync.rs +++ b/codex-rs/core/src/tools/handlers/test_sync.rs @@ -95,7 +95,12 @@ impl ToolExecutor for TestSyncHandler { } if let Some(barrier) = args.barrier { - wait_on_barrier(barrier).await?; + wait_on_barrier( + barrier.id, + barrier.participants, + Duration::from_millis(barrier.timeout_ms), + ) + .await?; } if let Some(delay) = args.sleep_after_ms @@ -113,26 +118,39 @@ impl ToolExecutor for TestSyncHandler { impl CoreToolRuntime for TestSyncHandler {} -async fn wait_on_barrier(args: BarrierArgs) -> Result<(), FunctionCallError> { - if args.participants == 0 { +impl TestSyncHandler { + pub(crate) async fn wait_on_barrier_for_tests( + id: &str, + participants: usize, + timeout: Duration, + ) -> Result<(), FunctionCallError> { + wait_on_barrier(id.to_string(), participants, timeout).await + } +} + +async fn wait_on_barrier( + barrier_id: String, + participants: usize, + timeout: Duration, +) -> Result<(), FunctionCallError> { + if participants == 0 { return Err(FunctionCallError::RespondToModel( "barrier participants must be greater than zero".to_string(), )); } - if args.timeout_ms == 0 { + if timeout.is_zero() { return Err(FunctionCallError::RespondToModel( "barrier timeout must be greater than zero".to_string(), )); } - let barrier_id = args.id.clone(); let barrier = { let mut map = barrier_map().lock().await; match map.entry(barrier_id.clone()) { Entry::Occupied(entry) => { let state = entry.get(); - if state.participants != args.participants { + if state.participants != participants { let existing = state.participants; return Err(FunctionCallError::RespondToModel(format!( "barrier {barrier_id} already registered with {existing} participants" @@ -141,17 +159,16 @@ async fn wait_on_barrier(args: BarrierArgs) -> Result<(), FunctionCallError> { state.barrier.clone() } Entry::Vacant(entry) => { - let barrier = Arc::new(Barrier::new(args.participants)); + let barrier = Arc::new(Barrier::new(participants)); entry.insert(BarrierState { barrier: barrier.clone(), - participants: args.participants, + participants, }); barrier } } }; - let timeout = Duration::from_millis(args.timeout_ms); let wait_result = tokio::time::timeout(timeout, barrier.wait()) .await .map_err(|_| { diff --git a/codex-rs/core/tests/suite/tool_parallelism.rs b/codex-rs/core/tests/suite/tool_parallelism.rs index d3f435ed53..3fb9c141f0 100644 --- a/codex-rs/core/tests/suite/tool_parallelism.rs +++ b/codex-rs/core/tests/suite/tool_parallelism.rs @@ -6,6 +6,7 @@ use std::fs; use std::time::Duration; use std::time::Instant; +use codex_core::test_support::wait_on_test_sync_barrier; use codex_features::Feature; use codex_protocol::models::PermissionProfile; use codex_protocol::protocol::AskForApproval; @@ -242,17 +243,38 @@ async fn mixed_parallel_tools_run_in_parallel() -> anyhow::Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let test = build_codex_with_test_tool(&server).await?; + // Exercise the standalone async shell handler alongside the sync test tool. + let mut builder = test_codex() + .with_model("test-gpt-5.1-codex") + .with_config(|config| { + config + .features + .disable(Feature::UnifiedExec) + .expect("test config should allow feature update"); + }); + let test = builder.build(&server).await?; + const BARRIER_ID: &str = "mixed-parallel-test-sync"; + const SHELL_STARTED_FILE: &str = "mixed-parallel-shell-started"; let sync_args = json!({ - "sleep_after_ms": 300 + "barrier": { + "id": BARRIER_ID, + "participants": 2, + "timeout_ms": 60_000, + } }) .to_string(); + let shell_started = test.cwd.path().join(SHELL_STARTED_FILE); + let command = if cfg!(windows) { + format!("Set-Content -Path '{SHELL_STARTED_FILE}' -Value started; Start-Sleep -Seconds 60") + } else { + format!("touch '{SHELL_STARTED_FILE}' && sleep 60") + }; let shell_args = serde_json::to_string(&json!({ - "command": "sleep 0.25", - // Avoid user-specific shell startup cost in timing assertions. + "command": command, "login": false, - "timeout_ms": 1_000, + "timeout_ms": 120_000, + "workdir": test.cwd.path(), }))?; let first_response = sse(vec![ @@ -261,16 +283,34 @@ async fn mixed_parallel_tools_run_in_parallel() -> anyhow::Result<()> { ev_function_call("call-2", "shell_command", &shell_args), ev_completed("resp-1"), ]); - let second_response = sse(vec![ - ev_assistant_message("msg-1", "done"), - ev_completed("resp-2"), - ]); - mount_sse_sequence(&server, vec![first_response, second_response]).await; + mount_sse_once(&server, first_response).await; - let duration = run_turn_and_measure(&test, "mix tools").await?; - assert_test_tool_parallel_duration(duration); + submit_turn(&test, "mix tools").await?; - Ok(()) + let overlap_result = tokio::time::timeout(Duration::from_secs(30), async { + while !shell_started.exists() { + tokio::time::sleep(Duration::from_millis(10)).await; + } + + wait_on_test_sync_barrier(BARRIER_ID, /*participants*/ 2, Duration::from_secs(60)) + .await + .map_err(anyhow::Error::msg) + }) + .await; + + test.codex.submit(Op::Interrupt).await?; + let aborted = wait_for_event_with_timeout( + &test.codex, + |event| matches!(event, EventMsg::TurnAborted(_)), + Duration::from_secs(30), + ) + .await; + let EventMsg::TurnAborted(aborted) = aborted else { + unreachable!("event predicate only accepts turn-aborted events"); + }; + assert_eq!(aborted.reason, TurnAbortReason::Interrupted); + + overlap_result.map_err(|_| anyhow::anyhow!("timed out waiting for mixed tools to overlap"))? } #[tokio::test(flavor = "multi_thread", worker_threads = 2)]