From 37aeac06b3f49ab4b96dea01c974971d9391c2ef Mon Sep 17 00:00:00 2001 From: verma-oai Date: Tue, 24 Nov 2026 09:29:16 +0000 Subject: [PATCH] Add opt-in deferred image generation in code mode (#63594) ## What changed Add `features.defer_image_generation` so code mode can discover the image generation tool through `ALL_TOOLS` without including its definition in the initial model request. Direct exposure remains the default and applies when `image_gen` is excluded from code mode. ## Testing Update the app-server GIF edit test to discover and invoke the deferred tool through code mode. Verify that the initial request omits the tool definition, the discovered definition includes the GIF option, and the edit preserves animation and API defaults. GitOrigin-RevId: 899f689bae0b86099a0ca3736d1cd2576bc3a7a9 --- .../tests/suite/v2/imagegen_extension.rs | 20 +++++++++++-------- codex-rs/core/config.schema.json | 6 ++++++ .../ext/image-generation/src/extension.rs | 15 ++++++++++++++ codex-rs/ext/image-generation/src/tool.rs | 6 ++++-- codex-rs/features/src/lib.rs | 12 +++++++++++ 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/codex-rs/app-server/tests/suite/v2/imagegen_extension.rs b/codex-rs/app-server/tests/suite/v2/imagegen_extension.rs index a472b658f1..8a47571199 100644 --- a/codex-rs/app-server/tests/suite/v2/imagegen_extension.rs +++ b/codex-rs/app-server/tests/suite/v2/imagegen_extension.rs @@ -1130,7 +1130,7 @@ generatedImage(result); #[cfg(not(windows))] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn gif_edit_from_code_mode_preserves_animation_and_api_defaults() -> Result<()> { +async fn deferred_gif_edit_from_code_mode_preserves_animation_and_api_defaults() -> Result<()> { let call_id = "code-mode-gif-edit"; let server = responses::start_mock_server().await; Mock::given(method("POST")) @@ -1152,13 +1152,15 @@ async fn gif_edit_from_code_mode_preserves_animation_and_api_defaults() -> Resul call_id, "exec", r#" -const result = await tools.image_gen__imagegen({ +const imagegen = ALL_TOOLS.find(tool => tool.name === "image_gen__imagegen"); +const result = await tools[imagegen.name]({ prompt: "animate the attached image", gif: {}, num_last_images_to_include: 1, }); text(result.image_url); generatedImage(result); +text(imagegen.description); "#, ), responses::ev_completed("resp-1"), @@ -1190,11 +1192,12 @@ generatedImage(result); start_turn( &mut mcp, ThreadStartParams { + model: Some("gpt-5.5".into()), config: Some( - [( - "features.image_generation_animation".to_string(), - json!(true), - )] + [ + ("features.image_generation_animation".into(), json!(true)), + ("features.defer_image_generation".into(), json!(true)), + ] .into(), ), ..Default::default() @@ -1274,7 +1277,8 @@ generatedImage(result); let requests = response_mock.requests(); assert_eq!(requests.len(), 2); - assert!(requests[0].body_contains_text("gif?:")); + assert!(!requests[0].body_contains_text("image_gen__imagegen(args:")); + assert!(requests[1].body_contains_text("gif?:")); let output = requests[1].custom_tool_call_output(call_id); assert_eq!( output["output"][1], @@ -1295,7 +1299,7 @@ generatedImage(result); .as_str() .is_some_and(|hint| hint.contains(&saved_path.display().to_string())) ); - assert_eq!(output["output"].as_array().map(Vec::len), Some(4)); + assert_eq!(output["output"].as_array().map(Vec::len), Some(5)); Ok(()) } diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index fe925445c8..c096bcd89e 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -1015,6 +1015,9 @@ "default_mode_request_user_input": { "type": "boolean" }, + "defer_image_generation": { + "type": "boolean" + }, "defer_mailbox_preemption": { "type": "boolean" }, @@ -8425,6 +8428,9 @@ "default_mode_request_user_input": { "type": "boolean" }, + "defer_image_generation": { + "type": "boolean" + }, "defer_mailbox_preemption": { "type": "boolean" }, diff --git a/codex-rs/ext/image-generation/src/extension.rs b/codex-rs/ext/image-generation/src/extension.rs index e240e4a807..c5bd7dd167 100644 --- a/codex-rs/ext/image-generation/src/extension.rs +++ b/codex-rs/ext/image-generation/src/extension.rs @@ -14,8 +14,10 @@ use codex_extension_api::ToolExecutor; use codex_http_client::HttpClientFactory; use codex_login::AuthManager; use codex_model_provider_info::ModelProviderInfo; +use codex_tools::ToolExposure; use codex_utils_absolute_path::AbsolutePathBuf; +use crate::IMAGE_GEN_NAMESPACE; use crate::backend::CodexImagesBackend; use crate::tool::ImageGenerationTool; @@ -31,6 +33,7 @@ type SaveRootResolver = dyn Fn(&Config) -> Option + Send + Sync struct ImageGenerationExtensionConfig { available: bool, gif_enabled: bool, + defer_image_generation: bool, http_client_factory: HttpClientFactory, provider: ModelProviderInfo, save_root: Option, @@ -44,6 +47,12 @@ impl ImageGenerationExtensionConfig { || config.model_provider.requires_openai_auth || config.model_provider.uses_openai_actor_authorization(), gif_enabled: config.features.image_generation_animation_enabled(), + defer_image_generation: config.features.defer_image_generation_enabled() + && !config + .code_mode + .excluded_tool_namespaces + .iter() + .any(|namespace| namespace == IMAGE_GEN_NAMESPACE), http_client_factory: config.http_client_factory(), provider: config.model_provider.clone(), save_root: resolve_save_root(config), @@ -106,6 +115,11 @@ impl ToolContributor for ImageGenerationExtension { }) }); + let exposure = if config.defer_image_generation { + ToolExposure::Deferred + } else { + ToolExposure::Direct + }; vec![Arc::new(ImageGenerationTool::new( CodexImagesBackend::new( auth.model_provider(config.provider.clone()), @@ -117,6 +131,7 @@ impl ToolContributor for ImageGenerationExtension { config.save_root.clone(), thread_store.level_id().to_string(), config.gif_enabled, + exposure, ))] } } diff --git a/codex-rs/ext/image-generation/src/tool.rs b/codex-rs/ext/image-generation/src/tool.rs index d4a479b2f6..c555cdd130 100644 --- a/codex-rs/ext/image-generation/src/tool.rs +++ b/codex-rs/ext/image-generation/src/tool.rs @@ -81,6 +81,7 @@ pub(crate) struct ImageGenerationTool { save_root: Option, thread_id: String, gif_enabled: bool, + exposure: ToolExposure, } impl ImageGenerationTool { @@ -90,12 +91,14 @@ impl ImageGenerationTool { save_root: Option, thread_id: String, gif_enabled: bool, + exposure: ToolExposure, ) -> Self { Self { backend, save_root, thread_id, gif_enabled, + exposure, } } } @@ -148,9 +151,8 @@ impl<'call> ToolExecutor> for ImageGenerationTool { imagegen_tool_spec(self.gif_enabled) } - /// Exposes image generation directly and through the nested code-mode tool surface. fn exposure(&self) -> ToolExposure { - ToolExposure::Direct + self.exposure } /// Executes the selected image operation and returns the completed image result. diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index e859d31f24..e09ca21985 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -416,6 +416,8 @@ pub enum Feature { ExternalMigration, /// Enable extension-backed image generation. ImageGeneration, + /// Load the image generation tool definition on demand in code mode. + DeferImageGeneration, /// Allow the image-generation tool to request a backend-assembled GIF. ImageGenerationAnimation, /// Omit inline image and audio content from app-server item notifications. @@ -684,6 +686,10 @@ impl Features { self.enabled.contains(&f) } + pub fn defer_image_generation_enabled(&self) -> bool { + self.enabled(Feature::DeferImageGeneration) + } + /// Whether to advertise GIF output in the image-generation tool. pub fn image_generation_animation_enabled(&self) -> bool { self.enabled(Feature::ImageGenerationAnimation) @@ -2130,6 +2136,12 @@ pub const FEATURES: &[FeatureSpec] = &[ stage: Stage::Stable, default_enabled: true, }, + FeatureSpec { + id: Feature::DeferImageGeneration, + key: "defer_image_generation", + stage: Stage::UnderDevelopment, + default_enabled: false, + }, FeatureSpec { id: Feature::ImageGenerationAnimation, key: "image_generation_animation",