diff --git a/codex-rs/core/src/tools/parallel.rs b/codex-rs/core/src/tools/parallel.rs index c40886b56c..ece130faf9 100644 --- a/codex-rs/core/src/tools/parallel.rs +++ b/codex-rs/core/src/tools/parallel.rs @@ -251,6 +251,7 @@ mod tests { use codex_protocol::models::FunctionCallOutputPayload; use pretty_assertions::assert_eq; use tokio::sync::Notify; + use tokio::sync::mpsc; use tokio::sync::oneshot; struct ImmediateHandler { @@ -285,6 +286,56 @@ mod tests { impl CoreToolRuntime for ImmediateHandler {} + struct BlockingHandler { + tool_name: codex_tools::ToolName, + supports_parallel: bool, + started: mpsc::UnboundedSender, + release: Arc, + } + + impl ToolExecutor for BlockingHandler { + fn tool_name(&self) -> codex_tools::ToolName { + self.tool_name.clone() + } + + fn spec(&self) -> codex_tools::ToolSpec { + codex_tools::ToolSpec::Function(codex_tools::ResponsesApiTool { + name: self.tool_name.name.clone(), + description: "Blocking test tool.".to_string(), + strict: false, + defer_loading: None, + parameters: codex_tools::JsonSchema::default(), + output_schema: None, + }) + } + + fn supports_parallel_tool_calls(&self) -> bool { + self.supports_parallel + } + + fn handle(&self, invocation: ToolInvocation) -> codex_tools::ToolExecutorFuture<'_> { + Box::pin(self.handle_call(invocation)) + } + } + + impl BlockingHandler { + async fn handle_call( + &self, + invocation: ToolInvocation, + ) -> Result, FunctionCallError> { + self.started + .send(invocation.call_id) + .expect("test receiver should remain open"); + self.release.notified().await; + Ok( + Box::new(FunctionToolOutput::from_text("ok".to_string(), Some(true))) + as Box, + ) + } + } + + impl CoreToolRuntime for BlockingHandler {} + struct CancellationCleanupHandler { tool_name: codex_tools::ToolName, started: std::sync::Mutex>>, @@ -401,6 +452,116 @@ mod tests { } } + #[tokio::test] + async fn runtime_allows_only_parallel_capable_tools_to_overlap() -> anyhow::Result<()> { + let (session, turn_context) = crate::session::tests::make_session_and_context().await; + let session = Arc::new(session); + let turn_context = Arc::new(turn_context); + + assert_tool_start_overlap( + Arc::clone(&session), + Arc::clone(&turn_context), + /*supports_parallel*/ true, + /*expect_overlap*/ true, + ) + .await?; + assert_tool_start_overlap( + session, + turn_context, + /*supports_parallel*/ false, + /*expect_overlap*/ false, + ) + .await?; + + Ok(()) + } + + async fn assert_tool_start_overlap( + session: Arc, + turn_context: Arc, + supports_parallel: bool, + expect_overlap: bool, + ) -> anyhow::Result<()> { + let tool_name = codex_tools::ToolName::plain(if supports_parallel { + "parallel_blocking_tool" + } else { + "serial_blocking_tool" + }); + let (started_tx, mut started_rx) = mpsc::unbounded_channel(); + let release = Arc::new(Notify::new()); + let handler = Arc::new(BlockingHandler { + tool_name: tool_name.clone(), + supports_parallel, + started: started_tx, + release: Arc::clone(&release), + }) as Arc; + let router = Arc::new(ToolRouter::from_parts( + ToolRegistry::from_tools([handler]), + Vec::new(), + )); + let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())); + let runtime = ToolCallRuntime::new( + router, + Arc::clone(&session), + Arc::clone(&turn_context), + tracker, + ); + let cancellation_token = CancellationToken::new(); + let first = tokio::spawn(runtime.clone().handle_tool_call( + blocking_call(&tool_name, "call-1"), + cancellation_token.clone(), + )); + let second = tokio::spawn( + runtime.handle_tool_call(blocking_call(&tool_name, "call-2"), cancellation_token), + ); + + let first_started = tokio::time::timeout(Duration::from_secs(1), started_rx.recv()) + .await? + .expect("first tool call should start"); + assert_eq!(first_started, "call-1"); + + let second_started_before_release = + tokio::time::timeout(Duration::from_millis(100), started_rx.recv()).await; + if expect_overlap { + assert_eq!( + second_started_before_release? + .expect("second tool call should start before release"), + "call-2" + ); + } else { + assert!( + second_started_before_release.is_err(), + "non-parallel tool call should wait for the write lock" + ); + } + + release.notify_waiters(); + if !expect_overlap { + assert_eq!( + tokio::time::timeout(Duration::from_secs(1), started_rx.recv()) + .await? + .expect("second tool call should start after first release"), + "call-2" + ); + release.notify_waiters(); + } + + first.await??; + second.await??; + + Ok(()) + } + + fn blocking_call(tool_name: &codex_tools::ToolName, call_id: &str) -> ToolCall { + ToolCall { + tool_name: tool_name.clone(), + call_id: call_id.to_string(), + payload: ToolPayload::Function { + arguments: "{}".to_string(), + }, + } + } + #[tokio::test] async fn cancellation_after_handler_finishes_preserves_completed_lifecycle() -> anyhow::Result<()> { diff --git a/codex-rs/ext/image-generation/src/tests.rs b/codex-rs/ext/image-generation/src/tests.rs index fe915746f9..b042e1a185 100644 --- a/codex-rs/ext/image-generation/src/tests.rs +++ b/codex-rs/ext/image-generation/src/tests.rs @@ -4,9 +4,12 @@ use codex_api::ImageGenerationRequest; use codex_api::ImageQuality; use codex_api::ImageUrl; use codex_core::context::extension_image_generation_output_hint; +use codex_extension_api::ToolExecutor; use codex_extension_api::ToolOutput; use codex_extension_api::ToolPayload; use codex_extension_api::ToolSpec; +use codex_model_provider::create_model_provider; +use codex_model_provider_info::ModelProviderInfo; use codex_protocol::models::ContentItem; use codex_protocol::models::DEFAULT_IMAGE_DETAIL; use codex_protocol::models::FunctionCallOutputBody; @@ -18,12 +21,14 @@ use codex_tools::ResponsesApiNamespaceTool; use pretty_assertions::assert_eq; use super::GeneratedImageOutput; +use super::ImageGenerationTool; use super::ImageRequest; use super::ImagegenArgs; use super::imagegen_tool_spec; use super::request_for_call_args; use crate::IMAGE_GEN_NAMESPACE; use crate::IMAGEGEN_TOOL_NAME; +use crate::backend::CodexImagesBackend; const RESULT: &str = "cG5n"; @@ -37,6 +42,22 @@ fn uses_reserved_image_gen_namespace() { assert_eq!(function.name, IMAGEGEN_TOOL_NAME); } +#[test] +fn imagegen_tool_supports_parallel_calls() { + let tool = ImageGenerationTool::new( + CodexImagesBackend::new(create_model_provider( + ModelProviderInfo::create_openai_provider(/*base_url*/ None), + /*auth_manager*/ None, + )), + "/tmp/codex-home" + .try_into() + .expect("test path should be absolute"), + "thread-id".to_string(), + ); + + assert!(tool.supports_parallel_tool_calls()); +} + #[tokio::test] async fn omitted_references_generate_with_fixed_defaults() { assert_eq!( diff --git a/codex-rs/ext/image-generation/src/tool.rs b/codex-rs/ext/image-generation/src/tool.rs index 3e2cfd1792..7b1ae7929f 100644 --- a/codex-rs/ext/image-generation/src/tool.rs +++ b/codex-rs/ext/image-generation/src/tool.rs @@ -96,6 +96,10 @@ impl ToolExecutor for ImageGenerationTool { ToolExposure::Direct } + fn supports_parallel_tool_calls(&self) -> bool { + true + } + /// Executes the selected image operation and returns the completed image result. fn handle(&self, call: ToolCall) -> codex_extension_api::ToolExecutorFuture<'_> { Box::pin(self.handle_call(call))