mod delegate; mod execute_handler; pub(crate) mod execute_spec; mod output; mod response_adapter; mod telemetry; mod tool_definitions; mod wait_handler; pub(crate) mod wait_spec; use std::sync::Arc; use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; use std::time::Duration; use codex_code_mode::CellId; use codex_code_mode::CodeModeNestedToolCall; use codex_code_mode::CodeModeSession; use codex_code_mode::CodeModeSessionProvider; use codex_code_mode::CodeModeToolKind; use codex_code_mode::RuntimeResponse; use codex_protocol::ThreadId; use codex_protocol::models::FunctionCallOutputContentItem; use futures::future::join_all; use serde_json::Value as JsonValue; use tokio::sync::OnceCell; use tokio_util::sync::CancellationToken; use crate::config::CodeModeConfig; use crate::function_tool::FunctionCallError; use crate::original_image_detail::can_request_original_image_detail; use crate::original_image_detail::sanitize_original_image_detail as sanitize_image_detail_items; use crate::session::session::Session; use crate::session::step_context::StepContext; use crate::session::turn_context::TurnContext; use crate::tools::ExecutedToolCalls; use crate::tools::call_trace; use crate::tools::context::FunctionToolOutput; use crate::tools::context::SharedTurnDiffTracker; use crate::tools::context::ToolPayload; use crate::tools::parallel::ToolCallRuntime; use crate::tools::router::ToolCall; use crate::tools::router::ToolCallSource; use crate::unified_exec::resolve_max_tokens; use codex_protocol::openai_models::ToolMode; use codex_tools::ToolName; use codex_utils_audio::estimate_audio_token_count; use codex_utils_output_truncation::TruncationPolicy; use codex_utils_output_truncation::formatted_truncate_text_content_items_with_policy; use codex_utils_output_truncation::truncate_function_output_items_with_policy; use delegate::CodeModeCellDelegate; use delegate::CodeModeDispatchBroker; use delegate::CodeModeDispatchWorker; pub(crate) use execute_handler::CodeModeExecuteHandler; use output::CodeModeToolOutput; use response_adapter::into_function_call_output_content_items; pub(crate) use tool_definitions::prepare_code_mode_tool_definitions; pub(crate) use wait_handler::CodeModeWaitHandler; pub(crate) const PUBLIC_TOOL_NAME: &str = codex_code_mode::PUBLIC_TOOL_NAME; pub(crate) const WAIT_TOOL_NAME: &str = codex_code_mode::WAIT_TOOL_NAME; pub(crate) const DEFAULT_WAIT_YIELD_TIME_MS: u64 = codex_code_mode::DEFAULT_WAIT_YIELD_TIME_MS; /// Returns true for the code-mode `exec` tool in the default namespace. pub(crate) fn is_exec_tool_name(tool_name: &ToolName) -> bool { tool_name.is_default_namespace() && tool_name.name == PUBLIC_TOOL_NAME } #[derive(Clone)] pub(crate) struct ExecContext { pub(super) session: Arc, pub(super) turn: Arc, } pub(crate) struct CodeModeService { session: OnceCell>, session_provider: Arc, availability: Result<(), String>, dispatch_broker: Arc, default_exec_yield_time_ms: u64, shutdown_token: CancellationToken, unavailable_warning_emitted: AtomicBool, } impl CodeModeService { pub(crate) fn new( thread_id: ThreadId, session_provider: Arc, config: &CodeModeConfig, executed_tool_calls: ExecutedToolCalls, ) -> Self { let dispatch_broker = Arc::new(CodeModeDispatchBroker::new(thread_id, executed_tool_calls)); let availability = session_provider.availability(); Self { session: OnceCell::new(), session_provider, availability, dispatch_broker, default_exec_yield_time_ms: config.default_exec_yield_time_ms, shutdown_token: CancellationToken::new(), unavailable_warning_emitted: AtomicBool::new(false), } } pub(crate) fn is_available(&self) -> bool { self.availability.is_ok() } pub(crate) fn take_unavailable_warning(&self, tool_mode: ToolMode) -> Option { let error = self.availability.as_ref().err()?; let behavior = match tool_mode { ToolMode::Direct => "Falling back to direct tools", ToolMode::CodeMode | ToolMode::CodeModeOnly => "Code mode will fail closed", }; (!self .unavailable_warning_emitted .swap(true, Ordering::Relaxed)) .then(|| { format!( "Code Mode is unavailable because {error}. {behavior}; enable `features.code_mode_host` and install `codex-code-mode-host`." ) }) } pub(crate) fn session_provider(&self) -> Arc { Arc::clone(&self.session_provider) } pub(crate) async fn execute( &self, mut request: codex_code_mode::ExecuteRequest, step_context: Arc, ) -> Result { request .yield_time_ms .get_or_insert(self.default_exec_yield_time_ms); let preempt = step_context.preempt.clone(); let delegate = Arc::new(CodeModeCellDelegate { broker: Arc::clone(&self.dispatch_broker), step_context, }); self.session() .await? .execute(request, delegate, preempt) .await } pub(crate) async fn wait( &self, request: codex_code_mode::WaitRequest, preempt: Option, ) -> Result { self.session().await?.wait(request, preempt).await } pub(crate) async fn terminate( &self, cell_id: CellId, ) -> Result { self.session().await?.terminate(cell_id).await } pub(crate) async fn interrupt_active_cells(&self) { let Some(session) = self.session.get() else { return; }; join_all( self.dispatch_broker .active_cell_ids() .into_iter() .map(|cell_id| async move { if let Err(error) = session.terminate(cell_id.clone()).await { tracing::warn!(%cell_id, %error, "failed to terminate interrupted code-mode cell"); } }), ) .await; } pub(crate) async fn shutdown(&self) -> Result<(), String> { self.shutdown_token.cancel(); // Join any initialization already in progress without initializing an unused service. match self .session .get_or_try_init(|| async { Err::, String>( "code mode session is shutting down".to_string(), ) }) .await { Ok(session) => session.shutdown().await, Err(_) => Ok(()), } } pub(crate) fn mark_cell_ready_for_dispatch( &self, cell_id: &codex_code_mode::CellId, originating_call: Option, ) { self.dispatch_broker .mark_cell_ready_for_dispatch(cell_id, originating_call); } pub(crate) fn cell_originating_call( &self, cell_id: &codex_code_mode::CellId, ) -> Option { self.dispatch_broker.cell_originating_call(cell_id) } pub(crate) fn finish_cell_dispatch(&self, cell_id: &CellId) { self.dispatch_broker.close_cell(cell_id); } pub(crate) fn start_turn_worker( &self, session: &Arc, step_context: Arc, tracker: SharedTurnDiffTracker, ) -> Option { if !step_context.tool_router.requires_code_mode_worker() { return None; } Some( self.dispatch_broker .start_turn_worker(Arc::clone(session), step_context, tracker), ) } pub(crate) async fn session(&self) -> Result, String> { if self.shutdown_token.is_cancelled() { return Err("code mode session is shutting down".to_string()); } self.session .get_or_try_init(|| async { if self.shutdown_token.is_cancelled() { return Err("code mode session is shutting down".to_string()); } let session = tokio::select! { biased; _ = self.shutdown_token.cancelled() => { return Err("code mode session is shutting down".to_string()); } session = self .session_provider .create_session() => session?, }; if self.shutdown_token.is_cancelled() { let _ = session.shutdown().await; return Err("code mode session is shutting down".to_string()); } Ok(session) }) .await .map(Arc::clone) } } fn handle_runtime_response( model_info: &codex_protocol::openai_models::ModelInfo, response: RuntimeResponse, max_output_tokens: Option, wall_time: Duration, experimental_show_cell_overhead: bool, ) -> CodeModeToolOutput { let script_status = format_script_status(&response); let supports_original = can_request_original_image_detail(model_info); let host_duration = response .code_mode_host_duration() .filter(|_| experimental_show_cell_overhead); let (content_items, error_text) = match response { RuntimeResponse::Yielded { content_items, .. } | RuntimeResponse::Terminated { content_items, .. } => (content_items, None), RuntimeResponse::Result { content_items, error_text, .. } => (content_items, error_text), }; let mut content_items = into_function_call_output_content_items(content_items); sanitize_image_detail_items(supports_original, &mut content_items); let success = error_text.is_none(); if let Some(error_text) = error_text { content_items.push(FunctionCallOutputContentItem::InputText { text: format!("Script error:\n{error_text}"), }); } content_items = truncate_code_mode_result(content_items, max_output_tokens); CodeModeToolOutput::new( FunctionToolOutput::from_content(content_items, Some(success)), script_status, wall_time, host_duration, ) } fn format_script_status(response: &RuntimeResponse) -> String { match response { RuntimeResponse::Yielded { cell_id, .. } => { format!("Script running with cell ID {cell_id}") } RuntimeResponse::Terminated { .. } => "Script terminated".to_string(), RuntimeResponse::Result { error_text, .. } => { if error_text.is_none() { "Script completed".to_string() } else { "Script failed".to_string() } } } } fn truncate_code_mode_result( items: Vec, max_output_tokens: Option, ) -> Vec { let max_output_tokens = resolve_max_tokens(max_output_tokens); let policy = TruncationPolicy::Tokens(max_output_tokens); if items .iter() .all(|item| matches!(item, FunctionCallOutputContentItem::InputText { .. })) { let (truncated_items, _) = formatted_truncate_text_content_items_with_policy(&items, policy); return truncated_items; } truncate_function_output_items_with_policy(&items, policy, estimate_audio_token_count) } // Submit synchronously so the recorder sees the call before the cell's dispatch gate closes. fn submit_nested_tool( session: Arc, step_context: Arc, tool_runtime: ToolCallRuntime, invocation: CodeModeNestedToolCall, call_id: String, cancellation_token: CancellationToken, ) -> Result< impl std::future::Future> + Send + 'static, FunctionCallError, > { let CodeModeNestedToolCall { cell_id, runtime_tool_call_id, tool_name, tool_kind, input, } = invocation; let thread_id = session.thread_id; let turn_id = step_context.turn.sub_id.clone(); let tool_name = tool_name.with_default_namespace(); // A cell can outlive a turn; the broker records arrival before a dispatching turn is known. tracing::event!( name: "codex.code_mode.nested_tool_dispatched", target: "codex_otel.trace_safe", tracing::Level::INFO, event.name = "codex.code_mode.nested_tool_dispatched", conversation.id = %thread_id, turn_id = turn_id.as_str(), cell.id = telemetry::trace_id(cell_id.as_str()), runtime_tool_call_id = telemetry::trace_id(&runtime_tool_call_id), call_id = call_id.as_str(), ); let payload = if is_exec_tool_name(&tool_name) { Err(format!("{PUBLIC_TOOL_NAME} cannot invoke itself")) } else { build_nested_tool_payload(tool_kind, &tool_name, input) }; let payload = match payload { Ok(payload) => payload, Err(error) => { call_trace::result_ready( thread_id, &turn_id, &tool_name, &call_id, call_trace::Source::CodeMode, ); return Err(FunctionCallError::RespondToModel(error)); } }; let call = ToolCall { tool_name, call_id, payload, encrypted_function_args: None, }; session .services .analytics_events_client .track_code_mode_tool_call(codex_analytics::CodeModeToolCallFact::ChildStarted { thread_id: session.thread_id.to_string(), turn_id: step_context.turn.sub_id.clone(), call_id: call.call_id.clone(), cell_id: cell_id.to_string(), }); let result = tool_runtime.handle_tool_call_with_source( step_context, call, ToolCallSource::CodeMode { cell_id: cell_id.to_string(), runtime_tool_call_id, }, cancellation_token, Arc::default(), ); Ok(async move { Ok(result.await?.code_mode_result()) }) } fn build_nested_tool_payload( tool_kind: CodeModeToolKind, tool_name: &ToolName, input: Option, ) -> Result { match tool_kind { CodeModeToolKind::Function => build_function_tool_payload(tool_name, input), CodeModeToolKind::Freeform => build_freeform_tool_payload(tool_name, input), } } fn build_function_tool_payload( tool_name: &ToolName, input: Option, ) -> Result { let arguments = serialize_function_tool_arguments(tool_name, input)?; Ok(ToolPayload::Function { arguments }) } fn serialize_function_tool_arguments( tool_name: &ToolName, input: Option, ) -> Result { match input { None => Ok("{}".to_string()), Some(JsonValue::Object(map)) => serde_json::to_string(&JsonValue::Object(map)) .map_err(|err| format!("failed to serialize tool `{tool_name}` arguments: {err}")), Some(_) => Err(format!( "tool `{tool_name}` expects a JSON object for arguments" )), } } fn build_freeform_tool_payload( tool_name: &ToolName, input: Option, ) -> Result { match input { Some(JsonValue::String(input)) => Ok(ToolPayload::Custom { input }), _ => Err(format!("tool `{tool_name}` expects a string input")), } } #[cfg(test)] mod tests { use std::collections::BTreeMap; use std::sync::Arc; use super::build_nested_tool_payload; use super::truncate_code_mode_result; use crate::session::step_context::StepContext; use crate::session::tests::make_session_and_context; use crate::tools::context::ToolPayload; use crate::tools::registry::ToolRegistry; use crate::tools::router::ToolRouter; use crate::turn_diff_tracker::TurnDiffTracker; use codex_code_mode::CodeModeToolKind; use codex_protocol::models::FunctionCallOutputContentItem; use codex_protocol::openai_models::ToolMode; use codex_tools::ToolName; use serde_json::json; #[tokio::test] async fn turn_worker_uses_step_router_mode_instead_of_admitted_turn() { let (session, turn) = make_session_and_context().await; assert_eq!( crate::tools::effective_tool_mode(&turn, turn.model_info()), ToolMode::Direct ); let session = Arc::new(session); let step_context = StepContext::for_test(Arc::new(turn)); let router = Arc::new(ToolRouter::from_parts( ToolRegistry::empty_for_test(), Vec::new(), ToolMode::CodeModeOnly, BTreeMap::new(), /*tool_namespaces_info*/ None, &[], )); let step_context = step_context.with_tool_router_for_test(router); let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())); let worker = session .services .code_mode_service .start_turn_worker(&session, step_context, tracker); assert!(worker.is_some()); } #[test] fn build_nested_tool_payload_uses_function_kind() { let payload = build_nested_tool_payload( CodeModeToolKind::Function, &ToolName::plain("example"), Some(json!({ "value": 1 })), ) .expect("function payload should serialize"); match payload { ToolPayload::Function { arguments } => { assert_eq!(arguments, r#"{"value":1}"#.to_string()); } other => panic!("expected function payload, got {other:?}"), } } #[test] fn build_nested_tool_payload_uses_freeform_kind() { let payload = build_nested_tool_payload( CodeModeToolKind::Freeform, &ToolName::plain("example"), Some(json!("hello")), ) .expect("freeform payload should preserve string input"); match payload { ToolPayload::Custom { input } => { assert_eq!(input, "hello".to_string()); } other => panic!("expected freeform payload, got {other:?}"), } } #[test] fn truncated_text_output_starts_with_warning() { let items = vec![FunctionCallOutputContentItem::InputText { text: "0123456789012345678901234567890123456789".to_string(), }]; assert_eq!( truncate_code_mode_result(items, Some(5)), vec![FunctionCallOutputContentItem::InputText { text: concat!( "Warning: truncated output (original token count: 10)\n", "Total output lines: 1\n\n", "0123456789…5 tokens truncated…0123456789" ) .to_string(), }] ); } #[test] fn over_budget_audio_output_is_omitted() { let items = vec![FunctionCallOutputContentItem::InputAudio { audio_url: format!("data:audio/wav;base64,{}", "A".repeat(100)), }]; assert_eq!( truncate_code_mode_result(items, Some(5)), vec![FunctionCallOutputContentItem::InputText { text: "[omitted 1 audio items ...]".to_string(), }] ); } }