diff --git a/codex-rs/core/src/agent/control.rs b/codex-rs/core/src/agent/control.rs index 529615a7a5..164619a0f9 100644 --- a/codex-rs/core/src/agent/control.rs +++ b/codex-rs/core/src/agent/control.rs @@ -15,6 +15,7 @@ use crate::shell_snapshot::ShellSnapshot; use crate::thread_manager::ThreadManagerState; use crate::thread_rollout_truncation::truncate_rollout_to_last_n_fork_turns; use codex_features::Feature; +use codex_mcp::mcp_connection_manager::McpConnectionManager; use codex_protocol::AgentPath; use codex_protocol::ThreadId; use codex_protocol::error::CodexErr; @@ -36,6 +37,7 @@ use std::collections::HashMap; use std::collections::VecDeque; use std::sync::Arc; use std::sync::Weak; +use tokio::sync::RwLock; use tokio::sync::watch; use tracing::warn; @@ -192,6 +194,12 @@ impl AgentControl { let inherited_exec_policy = self .inherited_exec_policy_for_source(&state, session_source.as_ref(), &config) .await; + let inherited_prompt_cache_key = self + .inherited_prompt_cache_key_for_source(&state, session_source.as_ref()) + .await; + let inherited_mcp_connection_manager = self + .inherited_mcp_connection_manager_for_source(&state, session_source.as_ref()) + .await; let (session_source, mut agent_metadata) = match session_source { Some(SessionSource::SubAgent(SubAgentSource::ThreadSpawn { parent_thread_id, @@ -225,6 +233,8 @@ impl AgentControl { &options, inherited_shell_snapshot, inherited_exec_policy, + inherited_prompt_cache_key, + inherited_mcp_connection_manager, ) .await? } @@ -238,6 +248,8 @@ impl AgentControl { /*metrics_service_name*/ None, inherited_shell_snapshot, inherited_exec_policy, + inherited_prompt_cache_key, + inherited_mcp_connection_manager, ) .await? } @@ -331,6 +343,8 @@ impl AgentControl { options: &SpawnAgentOptions, inherited_shell_snapshot: Option>, inherited_exec_policy: Option>, + inherited_prompt_cache_key: Option, + inherited_mcp_connection_manager: Option>>, ) -> CodexResult { if options.fork_parent_spawn_call_id.is_none() { return Err(CodexErr::Fatal( @@ -396,6 +410,8 @@ impl AgentControl { /*persist_extended_history*/ false, inherited_shell_snapshot, inherited_exec_policy, + inherited_prompt_cache_key, + inherited_mcp_connection_manager, ) .await } @@ -524,6 +540,12 @@ impl AgentControl { let inherited_exec_policy = self .inherited_exec_policy_for_source(&state, Some(&session_source), &config) .await; + let inherited_prompt_cache_key = self + .inherited_prompt_cache_key_for_source(&state, Some(&session_source)) + .await; + let inherited_mcp_connection_manager = self + .inherited_mcp_connection_manager_for_source(&state, Some(&session_source)) + .await; let rollout_path = match find_thread_path_by_id_str(config.codex_home.as_path(), &thread_id.to_string()) .await? @@ -545,6 +567,8 @@ impl AgentControl { session_source, inherited_shell_snapshot, inherited_exec_policy, + inherited_prompt_cache_key, + inherited_mcp_connection_manager, ) .await?; let mut agent_metadata = agent_metadata; @@ -1055,6 +1079,40 @@ impl AgentControl { )) } + async fn inherited_prompt_cache_key_for_source( + &self, + state: &Arc, + session_source: Option<&SessionSource>, + ) -> Option { + let Some(SessionSource::SubAgent(SubAgentSource::ThreadSpawn { + parent_thread_id, .. + })) = session_source + else { + return None; + }; + + let parent_thread = state.get_thread(*parent_thread_id).await.ok()?; + Some(parent_thread.codex.session.prompt_cache_key()) + } + + async fn inherited_mcp_connection_manager_for_source( + &self, + state: &Arc, + session_source: Option<&SessionSource>, + ) -> Option>> { + let Some(SessionSource::SubAgent(SubAgentSource::ThreadSpawn { + parent_thread_id, .. + })) = session_source + else { + return None; + }; + + let parent_thread = state.get_thread(*parent_thread_id).await.ok()?; + Some(Arc::clone( + &parent_thread.codex.session.services.mcp_connection_manager, + )) + } + async fn open_thread_spawn_children( &self, parent_thread_id: ThreadId, diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index d09672acfb..b1887daef3 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -144,6 +144,7 @@ struct ModelClientState { conversation_id: ThreadId, window_generation: AtomicU64, installation_id: String, + prompt_cache_key: ThreadId, provider: ModelProviderInfo, auth_env_telemetry: AuthEnvTelemetry, session_source: SessionSource, @@ -266,6 +267,7 @@ impl ModelClient { auth_manager: Option>, conversation_id: ThreadId, installation_id: String, + prompt_cache_key: ThreadId, provider: ModelProviderInfo, session_source: SessionSource, model_verbosity: Option, @@ -284,6 +286,7 @@ impl ModelClient { conversation_id, window_generation: AtomicU64::new(0), installation_id, + prompt_cache_key, provider, auth_env_telemetry, session_source, @@ -331,6 +334,10 @@ impl ModelClient { format!("{conversation_id}:{window_generation}") } + pub(crate) fn prompt_cache_key(&self) -> ThreadId { + self.state.prompt_cache_key + } + fn take_cached_websocket_session(&self) -> WebsocketSession { let mut cached_websocket_session = self .state @@ -803,7 +810,7 @@ impl ModelClientSession { None }; let text = create_text_param_for_request(verbosity, &prompt.output_schema); - let prompt_cache_key = Some(self.client.state.conversation_id.to_string()); + let prompt_cache_key = Some(self.client.state.prompt_cache_key.to_string()); let request = ResponsesApiRequest { model: model_info.slug.clone(), instructions: instructions.clone(), diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index f3b7a143bd..5ad7aae654 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -426,6 +426,8 @@ pub(crate) struct CodexSpawnArgs { pub(crate) metrics_service_name: Option, pub(crate) inherited_shell_snapshot: Option>, pub(crate) inherited_exec_policy: Option>, + pub(crate) inherited_prompt_cache_key: Option, + pub(crate) inherited_mcp_connection_manager: Option>>, pub(crate) user_shell_override: Option, pub(crate) parent_trace: Option, } @@ -481,6 +483,8 @@ impl Codex { inherited_shell_snapshot, user_shell_override, inherited_exec_policy, + inherited_prompt_cache_key, + inherited_mcp_connection_manager, parent_trace: _, } = args; let (tx_sub, rx_sub) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY); @@ -646,9 +650,11 @@ impl Codex { app_server_client_name: None, app_server_client_version: None, session_source, + prompt_cache_key: inherited_prompt_cache_key, dynamic_tools, persist_extended_history, inherited_shell_snapshot, + inherited_mcp_connection_manager, user_shell_override, }; @@ -1131,9 +1137,11 @@ pub(crate) struct SessionConfiguration { app_server_client_version: Option, /// Source of the session (cli, vscode, exec, mcp, ...) session_source: SessionSource, + prompt_cache_key: Option, dynamic_tools: Vec, persist_extended_history: bool, inherited_shell_snapshot: Option>, + inherited_mcp_connection_manager: Option>>, user_shell_override: Option, } @@ -1580,6 +1588,11 @@ impl Session { .unwrap_or(u64::MAX), InitialHistory::New | InitialHistory::Forked(_) => 0, }; + session_configuration.prompt_cache_key = Some( + session_configuration + .prompt_cache_key + .unwrap_or(conversation_id), + ); let state_builder = match &initial_history { InitialHistory::Resumed(resumed) => metadata::builder_from_items( resumed.history.as_slice(), @@ -1916,16 +1929,21 @@ impl Session { let installation_id = resolve_installation_id(&config.codex_home).await?; let services = SessionServices { - // Initialize the MCP connection manager with an uninitialized - // instance. It will be replaced with one created via - // McpConnectionManager::new() once all its constructor args are - // available. This also ensures `SessionConfigured` is emitted - // before any MCP-related events. It is reasonable to consider - // changing this to use Option or OnceCell, though the current - // setup is straightforward enough and performs well. - mcp_connection_manager: Arc::new(RwLock::new(McpConnectionManager::new_uninitialized( - &config.permissions.approval_policy, - ))), + mcp_connection_manager: session_configuration + .inherited_mcp_connection_manager + .clone() + .unwrap_or_else(|| { + // Initialize the MCP connection manager with an uninitialized + // instance. It will be replaced with one created via + // McpConnectionManager::new() once all its constructor args are + // available. This also ensures `SessionConfigured` is emitted + // before any MCP-related events. It is reasonable to consider + // changing this to use Option or OnceCell, though the current + // setup is straightforward enough and performs well. + Arc::new(RwLock::new(McpConnectionManager::new_uninitialized( + &config.permissions.approval_policy, + ))) + }), mcp_startup_cancellation_token: Mutex::new(CancellationToken::new()), unified_exec_manager: UnifiedExecProcessManager::new( config.background_terminal_max_timeout, @@ -1959,6 +1977,9 @@ impl Session { Some(Arc::clone(&auth_manager)), conversation_id, installation_id, + session_configuration + .prompt_cache_key + .unwrap_or(conversation_id), session_configuration.provider.clone(), session_configuration.session_source.clone(), config.model_verbosity, @@ -2035,80 +2056,86 @@ impl Session { // Start the watcher after SessionConfigured so it cannot emit earlier events. sess.start_skills_watcher_listener(); - // Construct sandbox_state before MCP startup so it can be sent to each - // MCP server immediately after it becomes ready (avoiding blocking). - let sandbox_state = SandboxState { - sandbox_policy: session_configuration.sandbox_policy.get().clone(), - codex_linux_sandbox_exe: config.codex_linux_sandbox_exe.clone(), - sandbox_cwd: session_configuration.cwd.to_path_buf(), - use_legacy_landlock: config.features.use_legacy_landlock(), - }; - let mut required_mcp_servers: Vec = mcp_servers - .iter() - .filter(|(_, server)| server.enabled && server.required) - .map(|(name, _)| name.clone()) - .collect(); - required_mcp_servers.sort(); - let enabled_mcp_server_count = mcp_servers.values().filter(|server| server.enabled).count(); - let required_mcp_server_count = required_mcp_servers.len(); - let tool_plugin_provenance = mcp_manager.tool_plugin_provenance(config.as_ref()); + if session_configuration + .inherited_mcp_connection_manager + .is_none() { - let mut cancel_guard = sess.services.mcp_startup_cancellation_token.lock().await; - cancel_guard.cancel(); - *cancel_guard = CancellationToken::new(); - } - let (mcp_connection_manager, cancel_token) = McpConnectionManager::new( - &mcp_servers, - config.mcp_oauth_credentials_store_mode, - auth_statuses.clone(), - &session_configuration.approval_policy, - INITIAL_SUBMIT_ID.to_owned(), - tx_event.clone(), - sandbox_state, - config.codex_home.clone(), - codex_apps_tools_cache_key(auth), - tool_plugin_provenance, - ) - .instrument(info_span!( - "session_init.mcp_manager_init", - otel.name = "session_init.mcp_manager_init", - session_init.enabled_mcp_server_count = enabled_mcp_server_count, - session_init.required_mcp_server_count = required_mcp_server_count, - )) - .await; - { - let mut manager_guard = sess.services.mcp_connection_manager.write().await; - *manager_guard = mcp_connection_manager; - } - { - let mut cancel_guard = sess.services.mcp_startup_cancellation_token.lock().await; - if cancel_guard.is_cancelled() { - cancel_token.cancel(); + // Construct sandbox_state before MCP startup so it can be sent to each + // MCP server immediately after it becomes ready (avoiding blocking). + let sandbox_state = SandboxState { + sandbox_policy: session_configuration.sandbox_policy.get().clone(), + codex_linux_sandbox_exe: config.codex_linux_sandbox_exe.clone(), + sandbox_cwd: session_configuration.cwd.to_path_buf(), + use_legacy_landlock: config.features.use_legacy_landlock(), + }; + let mut required_mcp_servers: Vec = mcp_servers + .iter() + .filter(|(_, server)| server.enabled && server.required) + .map(|(name, _)| name.clone()) + .collect(); + required_mcp_servers.sort(); + let enabled_mcp_server_count = + mcp_servers.values().filter(|server| server.enabled).count(); + let required_mcp_server_count = required_mcp_servers.len(); + let tool_plugin_provenance = mcp_manager.tool_plugin_provenance(config.as_ref()); + { + let mut cancel_guard = sess.services.mcp_startup_cancellation_token.lock().await; + cancel_guard.cancel(); + *cancel_guard = CancellationToken::new(); } - *cancel_guard = cancel_token; - } - if !required_mcp_servers.is_empty() { - let failures = sess - .services - .mcp_connection_manager - .read() - .await - .required_startup_failures(&required_mcp_servers) - .instrument(info_span!( - "session_init.required_mcp_wait", - otel.name = "session_init.required_mcp_wait", - session_init.required_mcp_server_count = required_mcp_server_count, - )) - .await; - if !failures.is_empty() { - let details = failures - .iter() - .map(|failure| format!("{}: {}", failure.server, failure.error)) - .collect::>() - .join("; "); - return Err(anyhow::anyhow!( - "required MCP servers failed to initialize: {details}" - )); + let (mcp_connection_manager, cancel_token) = McpConnectionManager::new( + &mcp_servers, + config.mcp_oauth_credentials_store_mode, + auth_statuses.clone(), + &session_configuration.approval_policy, + INITIAL_SUBMIT_ID.to_owned(), + tx_event.clone(), + sandbox_state, + config.codex_home.clone(), + codex_apps_tools_cache_key(auth), + tool_plugin_provenance, + ) + .instrument(info_span!( + "session_init.mcp_manager_init", + otel.name = "session_init.mcp_manager_init", + session_init.enabled_mcp_server_count = enabled_mcp_server_count, + session_init.required_mcp_server_count = required_mcp_server_count, + )) + .await; + { + let mut manager_guard = sess.services.mcp_connection_manager.write().await; + *manager_guard = mcp_connection_manager; + } + { + let mut cancel_guard = sess.services.mcp_startup_cancellation_token.lock().await; + if cancel_guard.is_cancelled() { + cancel_token.cancel(); + } + *cancel_guard = cancel_token; + } + if !required_mcp_servers.is_empty() { + let failures = sess + .services + .mcp_connection_manager + .read() + .await + .required_startup_failures(&required_mcp_servers) + .instrument(info_span!( + "session_init.required_mcp_wait", + otel.name = "session_init.required_mcp_wait", + session_init.required_mcp_server_count = required_mcp_server_count, + )) + .await; + if !failures.is_empty() { + let details = failures + .iter() + .map(|failure| format!("{}: {}", failure.server, failure.error)) + .collect::>() + .join("; "); + return Err(anyhow::anyhow!( + "required MCP servers failed to initialize: {details}" + )); + } } } sess.schedule_startup_prewarm(session_configuration.base_instructions.clone()) @@ -2144,6 +2171,10 @@ impl Session { self.services.state_db.clone() } + pub(crate) fn prompt_cache_key(&self) -> ThreadId { + self.services.model_client.prompt_cache_key() + } + /// Ensure rollout file writes are durably flushed. pub(crate) async fn flush_rollout(&self) { let recorder = { diff --git a/codex-rs/core/src/codex_delegate.rs b/codex-rs/core/src/codex_delegate.rs index aef0a92d76..6b3ce6e304 100644 --- a/codex-rs/core/src/codex_delegate.rs +++ b/codex-rs/core/src/codex_delegate.rs @@ -94,6 +94,10 @@ pub(crate) async fn run_codex_thread_interactive( inherited_shell_snapshot: None, user_shell_override: None, inherited_exec_policy: Some(Arc::clone(&parent_session.services.exec_policy)), + inherited_prompt_cache_key: Some(parent_session.prompt_cache_key()), + inherited_mcp_connection_manager: Some(Arc::clone( + &parent_session.services.mcp_connection_manager, + )), parent_trace: None, }) .await?; diff --git a/codex-rs/core/src/codex_tests.rs b/codex-rs/core/src/codex_tests.rs index d57d92c1ec..6a8c81628c 100644 --- a/codex-rs/core/src/codex_tests.rs +++ b/codex-rs/core/src/codex_tests.rs @@ -1875,6 +1875,7 @@ async fn set_rate_limits_retains_previous_credits() { dynamic_tools: Vec::new(), persist_extended_history: false, inherited_shell_snapshot: None, + inherited_mcp_connection_manager: None, user_shell_override: None, }; @@ -1977,6 +1978,7 @@ async fn set_rate_limits_updates_plan_type_when_present() { dynamic_tools: Vec::new(), persist_extended_history: false, inherited_shell_snapshot: None, + inherited_mcp_connection_manager: None, user_shell_override: None, }; @@ -2326,6 +2328,7 @@ pub(crate) async fn make_session_configuration_for_tests() -> SessionConfigurati dynamic_tools: Vec::new(), persist_extended_history: false, inherited_shell_snapshot: None, + inherited_mcp_connection_manager: None, user_shell_override: None, } } @@ -2589,6 +2592,7 @@ async fn session_new_fails_when_zsh_fork_enabled_without_zsh_path() { dynamic_tools: Vec::new(), persist_extended_history: false, inherited_shell_snapshot: None, + inherited_mcp_connection_manager: None, user_shell_override: None, }; @@ -2692,6 +2696,7 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) { dynamic_tools: Vec::new(), persist_extended_history: false, inherited_shell_snapshot: None, + inherited_mcp_connection_manager: None, user_shell_override: None, }; let per_turn_config = Session::build_per_turn_config(&session_configuration); @@ -3533,6 +3538,7 @@ pub(crate) async fn make_session_and_context_with_dynamic_tools_and_rx( dynamic_tools, persist_extended_history: false, inherited_shell_snapshot: None, + inherited_mcp_connection_manager: None, user_shell_override: None, }; let per_turn_config = Session::build_per_turn_config(&session_configuration); diff --git a/codex-rs/core/src/codex_tests_guardian.rs b/codex-rs/core/src/codex_tests_guardian.rs index 4f60c2f28e..63024a16f4 100644 --- a/codex-rs/core/src/codex_tests_guardian.rs +++ b/codex-rs/core/src/codex_tests_guardian.rs @@ -455,6 +455,8 @@ async fn guardian_subagent_does_not_inherit_parent_exec_policy_rules() { metrics_service_name: None, inherited_shell_snapshot: None, inherited_exec_policy: Some(Arc::new(parent_exec_policy)), + inherited_prompt_cache_key: None, + inherited_mcp_connection_manager: None, user_shell_override: None, parent_trace: None, }) diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index 2dd4ad5532..c348ec4519 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -20,6 +20,7 @@ use codex_app_server_protocol::TurnStatus; use codex_exec_server::EnvironmentManager; use codex_login::AuthManager; use codex_login::CodexAuth; +use codex_mcp::mcp_connection_manager::McpConnectionManager; use codex_model_provider_info::ModelProviderInfo; use codex_model_provider_info::OPENAI_PROVIDER_ID; use codex_models_manager::collaboration_mode_presets::CollaborationModesConfig; @@ -756,6 +757,8 @@ impl ThreadManagerState { /*metrics_service_name*/ None, /*inherited_shell_snapshot*/ None, /*inherited_exec_policy*/ None, + /*inherited_prompt_cache_key*/ None, + /*inherited_mcp_connection_manager*/ None, )) .await } @@ -770,6 +773,8 @@ impl ThreadManagerState { metrics_service_name: Option, inherited_shell_snapshot: Option>, inherited_exec_policy: Option>, + inherited_prompt_cache_key: Option, + inherited_mcp_connection_manager: Option>>, ) -> CodexResult { Box::pin(self.spawn_thread_with_source( config, @@ -782,6 +787,8 @@ impl ThreadManagerState { metrics_service_name, inherited_shell_snapshot, inherited_exec_policy, + inherited_prompt_cache_key, + inherited_mcp_connection_manager, /*parent_trace*/ None, /*user_shell_override*/ None, )) @@ -796,6 +803,8 @@ impl ThreadManagerState { session_source: SessionSource, inherited_shell_snapshot: Option>, inherited_exec_policy: Option>, + inherited_prompt_cache_key: Option, + inherited_mcp_connection_manager: Option>>, ) -> CodexResult { let initial_history = RolloutRecorder::get_rollout_history(&rollout_path).await?; Box::pin(self.spawn_thread_with_source( @@ -809,6 +818,8 @@ impl ThreadManagerState { /*metrics_service_name*/ None, inherited_shell_snapshot, inherited_exec_policy, + inherited_prompt_cache_key, + inherited_mcp_connection_manager, /*parent_trace*/ None, /*user_shell_override*/ None, )) @@ -825,6 +836,8 @@ impl ThreadManagerState { persist_extended_history: bool, inherited_shell_snapshot: Option>, inherited_exec_policy: Option>, + inherited_prompt_cache_key: Option, + inherited_mcp_connection_manager: Option>>, ) -> CodexResult { Box::pin(self.spawn_thread_with_source( config, @@ -837,6 +850,8 @@ impl ThreadManagerState { /*metrics_service_name*/ None, inherited_shell_snapshot, inherited_exec_policy, + inherited_prompt_cache_key, + inherited_mcp_connection_manager, /*parent_trace*/ None, /*user_shell_override*/ None, )) @@ -868,6 +883,8 @@ impl ThreadManagerState { metrics_service_name, /*inherited_shell_snapshot*/ None, /*inherited_exec_policy*/ None, + /*inherited_prompt_cache_key*/ None, + /*inherited_mcp_connection_manager*/ None, parent_trace, user_shell_override, )) @@ -887,6 +904,8 @@ impl ThreadManagerState { metrics_service_name: Option, inherited_shell_snapshot: Option>, inherited_exec_policy: Option>, + inherited_prompt_cache_key: Option, + inherited_mcp_connection_manager: Option>>, parent_trace: Option, user_shell_override: Option, ) -> CodexResult { @@ -914,6 +933,8 @@ impl ThreadManagerState { metrics_service_name, inherited_shell_snapshot, inherited_exec_policy, + inherited_prompt_cache_key, + inherited_mcp_connection_manager, user_shell_override, parent_trace, })