mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
Enable parallel imagegen tool calls
Mark the image generation extension as parallel-safe so code mode can overlap concurrent imagegen calls through the existing tool runtime path. Add coverage for the imagegen capability bit and for the core runtime behavior that parallel-capable tools may overlap while non-parallel tools remain serialized.
This commit is contained in:
@@ -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<String>,
|
||||
release: Arc<Notify>,
|
||||
}
|
||||
|
||||
impl ToolExecutor<ToolInvocation> 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<Box<dyn crate::tools::context::ToolOutput>, 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<dyn crate::tools::context::ToolOutput>,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl CoreToolRuntime for BlockingHandler {}
|
||||
|
||||
struct CancellationCleanupHandler {
|
||||
tool_name: codex_tools::ToolName,
|
||||
started: std::sync::Mutex<Option<oneshot::Sender<()>>>,
|
||||
@@ -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<Session>,
|
||||
turn_context: Arc<TurnContext>,
|
||||
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<dyn CoreToolRuntime>;
|
||||
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<()> {
|
||||
|
||||
@@ -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!(
|
||||
|
||||
@@ -96,6 +96,10 @@ impl ToolExecutor<ToolCall> 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))
|
||||
|
||||
Reference in New Issue
Block a user