From 346070a424f8cebcbc42d05081ec5e3ba82965b2 Mon Sep 17 00:00:00 2001 From: Clark DuVall Date: Wed, 6 May 2026 12:42:45 -0700 Subject: [PATCH 1/9] Route opted-in MCP elicitations through Guardian (#19431) # Motivation Browser Use origin-access prompts are MCP elicitations, not direct tool-call approval prompts, so they were bypassing the Guardian approval path. We need a generic opt-in that lets eligible MCP elicitations use Guardian when the current turn already routes approvals there. # Description Add a generic elicitation reviewer hook in codex-mcp and wire codex-core to pass a Guardian reviewer callback when creating the MCP connection manager. The reviewer validates explicit mcp_tool_call opt-in metadata, builds a Guardian MCP tool-call review request from server/tool/connector metadata and tool params, and maps Guardian approval, denial, timeout, and cancellation decisions back to MCP elicitation responses. The new option to trigger this in the `_meta` object is: ``` "codex_request_type": "approval_request", ``` # Testing - RUST_MIN_STACK=8388608 NEXTEST_STATUS_LEVEL=leak cargo nextest run --no-fail-fast --cargo-profile ci-test --test-threads 2 - cargo clippy --tests -- -D warnings - cargo fmt -- --config imports_granularity=Item --check - cargo shear - pnpm run format - python3 .github/scripts/verify_cargo_workspace_manifests.py - python3 .github/scripts/verify_tui_core_boundary.py - python3 .github/scripts/verify_bazel_clippy_lints.py - git diff --check --- codex-rs/codex-mcp/src/connection_manager.rs | 10 +- .../codex-mcp/src/connection_manager_tests.rs | 14 +- codex-rs/codex-mcp/src/elicitation.rs | 33 ++ codex-rs/codex-mcp/src/lib.rs | 3 + codex-rs/codex-mcp/src/mcp/mod.rs | 2 + codex-rs/core/src/connectors.rs | 1 + codex-rs/core/src/mcp_skill_dependencies.rs | 13 +- codex-rs/core/src/mcp_tool_call.rs | 28 +- codex-rs/core/src/mcp_tool_call_tests.rs | 1 + codex-rs/core/src/session/handlers.rs | 10 +- codex-rs/core/src/session/mcp.rs | 289 +++++++++++++++++- codex-rs/core/src/session/mcp_tests.rs | 212 +++++++++++++ codex-rs/core/src/session/session.rs | 1 + codex-rs/core/src/session/tests.rs | 2 +- codex-rs/core/src/session/turn.rs | 1 + codex-rs/protocol/src/lib.rs | 1 + codex-rs/protocol/src/mcp_approval_meta.rs | 19 ++ .../src/bottom_pane/mcp_server_elicitation.rs | 18 +- 18 files changed, 621 insertions(+), 37 deletions(-) create mode 100644 codex-rs/core/src/session/mcp_tests.rs create mode 100644 codex-rs/protocol/src/mcp_approval_meta.rs diff --git a/codex-rs/codex-mcp/src/connection_manager.rs b/codex-rs/codex-mcp/src/connection_manager.rs index 58b5566e23..f93ac5e089 100644 --- a/codex-rs/codex-mcp/src/connection_manager.rs +++ b/codex-rs/codex-mcp/src/connection_manager.rs @@ -17,6 +17,7 @@ use crate::codex_apps::CodexAppsToolsCacheContext; use crate::codex_apps::CodexAppsToolsCacheKey; use crate::codex_apps::write_cached_codex_apps_tools_if_needed; use crate::elicitation::ElicitationRequestManager; +use crate::elicitation::ElicitationReviewerHandle; use crate::mcp::CODEX_APPS_MCP_SERVER_NAME; use crate::mcp::ToolPluginProvenance; use crate::rmcp_client::AsyncManagedClient; @@ -87,6 +88,7 @@ impl McpConnectionManager { elicitation_requests: ElicitationRequestManager::new( approval_policy.value(), permission_profile.get().clone(), + /*reviewer*/ None, ), startup_cancellation_token: CancellationToken::new(), } @@ -157,13 +159,17 @@ impl McpConnectionManager { host_owned_codex_apps_enabled: bool, tool_plugin_provenance: ToolPluginProvenance, auth: Option<&CodexAuth>, + elicitation_reviewer: Option, ) -> (Self, CancellationToken) { let cancel_token = CancellationToken::new(); let mut clients = HashMap::new(); let mut server_origins = HashMap::new(); let mut join_set = JoinSet::new(); - let elicitation_requests = - ElicitationRequestManager::new(approval_policy.value(), initial_permission_profile); + let elicitation_requests = ElicitationRequestManager::new( + approval_policy.value(), + initial_permission_profile, + elicitation_reviewer, + ); let tool_plugin_provenance = Arc::new(tool_plugin_provenance); let startup_submit_id = submit_id.clone(); let codex_apps_auth_provider = auth diff --git a/codex-rs/codex-mcp/src/connection_manager_tests.rs b/codex-rs/codex-mcp/src/connection_manager_tests.rs index 69ff10e023..f2dc85cde0 100644 --- a/codex-rs/codex-mcp/src/connection_manager_tests.rs +++ b/codex-rs/codex-mcp/src/connection_manager_tests.rs @@ -203,8 +203,11 @@ fn elicitation_granular_policy_respects_never_and_config() { #[tokio::test] async fn disabled_permissions_auto_accept_elicitation_with_empty_form_schema() { - let manager = - ElicitationRequestManager::new(AskForApproval::Never, PermissionProfile::Disabled); + let manager = ElicitationRequestManager::new( + AskForApproval::Never, + PermissionProfile::Disabled, + /*reviewer*/ None, + ); let (tx_event, _rx_event) = async_channel::bounded(1); let sender = manager.make_sender("server".to_string(), tx_event); @@ -233,8 +236,11 @@ async fn disabled_permissions_auto_accept_elicitation_with_empty_form_schema() { #[tokio::test] async fn disabled_permissions_do_not_auto_accept_elicitation_with_requested_fields() { - let manager = - ElicitationRequestManager::new(AskForApproval::Never, PermissionProfile::Disabled); + let manager = ElicitationRequestManager::new( + AskForApproval::Never, + PermissionProfile::Disabled, + /*reviewer*/ None, + ); let (tx_event, _rx_event) = async_channel::bounded(1); let sender = manager.make_sender("server".to_string(), tx_event); diff --git a/codex-rs/codex-mcp/src/elicitation.rs b/codex-rs/codex-mcp/src/elicitation.rs index 43c9deb246..a51cd7c623 100644 --- a/codex-rs/codex-mcp/src/elicitation.rs +++ b/codex-rs/codex-mcp/src/elicitation.rs @@ -24,6 +24,7 @@ use codex_protocol::protocol::Event; use codex_protocol::protocol::EventMsg; use codex_rmcp_client::ElicitationResponse; use codex_rmcp_client::SendElicitation; +use futures::future::BoxFuture; use futures::future::FutureExt; use rmcp::model::CreateElicitationRequestParams; use rmcp::model::ElicitationAction; @@ -31,24 +32,43 @@ use rmcp::model::RequestId; use tokio::sync::Mutex; use tokio::sync::oneshot; +#[derive(Debug, Clone)] +pub struct ElicitationReviewRequest { + pub server_name: String, + pub request_id: RequestId, + pub elicitation: CreateElicitationRequestParams, +} + +pub trait ElicitationReviewer: Send + Sync { + fn review( + &self, + request: ElicitationReviewRequest, + ) -> BoxFuture<'static, Result>>; +} + +pub type ElicitationReviewerHandle = Arc; + #[derive(Clone)] pub(crate) struct ElicitationRequestManager { requests: Arc>, pub(crate) approval_policy: Arc>, pub(crate) permission_profile: Arc>, auto_deny: Arc>, + reviewer: Option, } impl ElicitationRequestManager { pub(crate) fn new( approval_policy: AskForApproval, permission_profile: PermissionProfile, + reviewer: Option, ) -> Self { Self { requests: Arc::new(Mutex::new(HashMap::new())), approval_policy: Arc::new(StdMutex::new(approval_policy)), permission_profile: Arc::new(StdMutex::new(permission_profile)), auto_deny: Arc::new(StdMutex::new(false)), + reviewer, } } @@ -89,6 +109,7 @@ impl ElicitationRequestManager { let approval_policy = self.approval_policy.clone(); let permission_profile = self.permission_profile.clone(); let auto_deny = self.auto_deny.clone(); + let reviewer = self.reviewer.clone(); Box::new(move |id, elicitation| { let elicitation_requests = elicitation_requests.clone(); let tx_event = tx_event.clone(); @@ -96,6 +117,7 @@ impl ElicitationRequestManager { let approval_policy = approval_policy.clone(); let permission_profile = permission_profile.clone(); let auto_deny = auto_deny.clone(); + let reviewer = reviewer.clone(); async move { let auto_deny = auto_deny .lock() @@ -138,6 +160,17 @@ impl ElicitationRequestManager { }); } + if let Some(reviewer) = reviewer.as_ref() { + let request = ElicitationReviewRequest { + server_name: server_name.clone(), + request_id: id.clone(), + elicitation: elicitation.clone(), + }; + if let Some(response) = reviewer.review(request).await? { + return Ok(response); + } + } + let request = match elicitation { CreateElicitationRequestParams::FormElicitationParams { meta, diff --git a/codex-rs/codex-mcp/src/lib.rs b/codex-rs/codex-mcp/src/lib.rs index 2080a5c319..83dc65dd06 100644 --- a/codex-rs/codex-mcp/src/lib.rs +++ b/codex-rs/codex-mcp/src/lib.rs @@ -1,4 +1,7 @@ pub use connection_manager::McpConnectionManager; +pub use elicitation::ElicitationReviewRequest; +pub use elicitation::ElicitationReviewer; +pub use elicitation::ElicitationReviewerHandle; pub use rmcp_client::MCP_SANDBOX_STATE_META_CAPABILITY; pub use runtime::McpRuntimeEnvironment; pub use runtime::SandboxState; diff --git a/codex-rs/codex-mcp/src/mcp/mod.rs b/codex-rs/codex-mcp/src/mcp/mod.rs index 03a5d1a068..ae667c698a 100644 --- a/codex-rs/codex-mcp/src/mcp/mod.rs +++ b/codex-rs/codex-mcp/src/mcp/mod.rs @@ -264,6 +264,7 @@ pub async fn read_mcp_resource( host_owned_codex_apps_enabled, tool_plugin_provenance(config), auth, + /*elicitation_reviewer*/ None, ) .await; @@ -331,6 +332,7 @@ pub async fn collect_mcp_server_status_snapshot_with_detail( host_owned_codex_apps_enabled, tool_plugin_provenance, auth, + /*elicitation_reviewer*/ None, ) .await; diff --git a/codex-rs/core/src/connectors.rs b/codex-rs/core/src/connectors.rs index e2154db17e..0bd53a50ee 100644 --- a/codex-rs/core/src/connectors.rs +++ b/codex-rs/core/src/connectors.rs @@ -283,6 +283,7 @@ pub async fn list_accessible_connectors_from_mcp_tools_with_environment_manager( host_owned_codex_apps_enabled, ToolPluginProvenance::default(), auth.as_ref(), + /*elicitation_reviewer*/ None, ) .await; diff --git a/codex-rs/core/src/mcp_skill_dependencies.rs b/codex-rs/core/src/mcp_skill_dependencies.rs index c24a6f3a48..aefc783cb6 100644 --- a/codex-rs/core/src/mcp_skill_dependencies.rs +++ b/codex-rs/core/src/mcp_skill_dependencies.rs @@ -19,6 +19,7 @@ use crate::SkillMetadata; use crate::session::session::Session; use crate::session::turn_context::TurnContext; use crate::skills::model::SkillToolDependency; +use codex_mcp::ElicitationReviewerHandle; use codex_mcp::McpOAuthLoginSupport; use codex_mcp::McpPermissionPromptAutoApproveContext; use codex_mcp::mcp_permission_prompt_is_auto_approved; @@ -35,6 +36,7 @@ pub(crate) async fn maybe_prompt_and_install_mcp_dependencies( turn_context: &TurnContext, cancellation_token: &CancellationToken, mentioned_skills: &[SkillMetadata], + elicitation_reviewer: Option, ) { let originator_value = originator().value; if !is_first_party_originator(originator_value.as_str()) { @@ -69,7 +71,14 @@ pub(crate) async fn maybe_prompt_and_install_mcp_dependencies( if should_install_mcp_dependencies(sess, turn_context, &unprompted_missing, cancellation_token) .await { - maybe_install_mcp_dependencies(sess, turn_context, config.as_ref(), mentioned_skills).await; + maybe_install_mcp_dependencies( + sess, + turn_context, + config.as_ref(), + mentioned_skills, + elicitation_reviewer, + ) + .await; } } @@ -78,6 +87,7 @@ pub(crate) async fn maybe_install_mcp_dependencies( turn_context: &TurnContext, config: &crate::config::Config, mentioned_skills: &[SkillMetadata], + elicitation_reviewer: Option, ) { if mentioned_skills.is_empty() || !config @@ -194,6 +204,7 @@ pub(crate) async fn maybe_install_mcp_dependencies( turn_context, refresh_servers, config.mcp_oauth_credentials_store_mode, + elicitation_reviewer, ) .await; } diff --git a/codex-rs/core/src/mcp_tool_call.rs b/codex-rs/core/src/mcp_tool_call.rs index a6557b3d67..c7e665cb02 100644 --- a/codex-rs/core/src/mcp_tool_call.rs +++ b/codex-rs/core/src/mcp_tool_call.rs @@ -54,6 +54,20 @@ use codex_protocol::items::McpToolCallItem; use codex_protocol::items::McpToolCallStatus; use codex_protocol::items::TurnItem; use codex_protocol::mcp::CallToolResult; +use codex_protocol::mcp_approval_meta::APPROVAL_KIND_KEY as MCP_TOOL_APPROVAL_KIND_KEY; +use codex_protocol::mcp_approval_meta::APPROVAL_KIND_MCP_TOOL_CALL as MCP_TOOL_APPROVAL_KIND_MCP_TOOL_CALL; +use codex_protocol::mcp_approval_meta::CONNECTOR_DESCRIPTION_KEY as MCP_TOOL_APPROVAL_CONNECTOR_DESCRIPTION_KEY; +use codex_protocol::mcp_approval_meta::CONNECTOR_ID_KEY as MCP_TOOL_APPROVAL_CONNECTOR_ID_KEY; +use codex_protocol::mcp_approval_meta::CONNECTOR_NAME_KEY as MCP_TOOL_APPROVAL_CONNECTOR_NAME_KEY; +use codex_protocol::mcp_approval_meta::PERSIST_ALWAYS as MCP_TOOL_APPROVAL_PERSIST_ALWAYS; +use codex_protocol::mcp_approval_meta::PERSIST_KEY as MCP_TOOL_APPROVAL_PERSIST_KEY; +use codex_protocol::mcp_approval_meta::PERSIST_SESSION as MCP_TOOL_APPROVAL_PERSIST_SESSION; +use codex_protocol::mcp_approval_meta::SOURCE_CONNECTOR as MCP_TOOL_APPROVAL_SOURCE_CONNECTOR; +use codex_protocol::mcp_approval_meta::SOURCE_KEY as MCP_TOOL_APPROVAL_SOURCE_KEY; +use codex_protocol::mcp_approval_meta::TOOL_DESCRIPTION_KEY as MCP_TOOL_APPROVAL_TOOL_DESCRIPTION_KEY; +use codex_protocol::mcp_approval_meta::TOOL_PARAMS_DISPLAY_KEY as MCP_TOOL_APPROVAL_TOOL_PARAMS_DISPLAY_KEY; +use codex_protocol::mcp_approval_meta::TOOL_PARAMS_KEY as MCP_TOOL_APPROVAL_TOOL_PARAMS_KEY; +use codex_protocol::mcp_approval_meta::TOOL_TITLE_KEY as MCP_TOOL_APPROVAL_TOOL_TITLE_KEY; use codex_protocol::openai_models::InputModality; use codex_protocol::protocol::AskForApproval; use codex_protocol::protocol::McpInvocation; @@ -1097,20 +1111,6 @@ pub(crate) const MCP_TOOL_APPROVAL_ACCEPT_FOR_SESSION: &str = "Allow for this se pub(crate) const MCP_TOOL_APPROVAL_DECLINE_SYNTHETIC: &str = "__codex_mcp_decline__"; const MCP_TOOL_APPROVAL_ACCEPT_AND_REMEMBER: &str = "Allow and don't ask me again"; const MCP_TOOL_APPROVAL_CANCEL: &str = "Cancel"; -const MCP_TOOL_APPROVAL_KIND_KEY: &str = "codex_approval_kind"; -const MCP_TOOL_APPROVAL_KIND_MCP_TOOL_CALL: &str = "mcp_tool_call"; -const MCP_TOOL_APPROVAL_PERSIST_KEY: &str = "persist"; -const MCP_TOOL_APPROVAL_PERSIST_SESSION: &str = "session"; -const MCP_TOOL_APPROVAL_PERSIST_ALWAYS: &str = "always"; -const MCP_TOOL_APPROVAL_SOURCE_KEY: &str = "source"; -const MCP_TOOL_APPROVAL_SOURCE_CONNECTOR: &str = "connector"; -const MCP_TOOL_APPROVAL_CONNECTOR_ID_KEY: &str = "connector_id"; -const MCP_TOOL_APPROVAL_CONNECTOR_NAME_KEY: &str = "connector_name"; -const MCP_TOOL_APPROVAL_CONNECTOR_DESCRIPTION_KEY: &str = "connector_description"; -const MCP_TOOL_APPROVAL_TOOL_TITLE_KEY: &str = "tool_title"; -const MCP_TOOL_APPROVAL_TOOL_DESCRIPTION_KEY: &str = "tool_description"; -const MCP_TOOL_APPROVAL_TOOL_PARAMS_KEY: &str = "tool_params"; -const MCP_TOOL_APPROVAL_TOOL_PARAMS_DISPLAY_KEY: &str = "tool_params_display"; const MCP_TOOL_CALL_ARC_MONITOR_CALLSITE_DEFAULT: &str = "mcp_tool_call__default"; const MCP_TOOL_CALL_ARC_MONITOR_CALLSITE_ALWAYS_ALLOW: &str = "mcp_tool_call__always_allow"; diff --git a/codex-rs/core/src/mcp_tool_call_tests.rs b/codex-rs/core/src/mcp_tool_call_tests.rs index 225ee0cf70..a556e228ac 100644 --- a/codex-rs/core/src/mcp_tool_call_tests.rs +++ b/codex-rs/core/src/mcp_tool_call_tests.rs @@ -1124,6 +1124,7 @@ async fn install_host_owned_codex_apps_manager(session: &Session, turn_context: /*host_owned_codex_apps_enabled*/ true, codex_mcp::ToolPluginProvenance::default(), auth.as_ref(), + /*elicitation_reviewer*/ None, ) .await; *session.services.mcp_connection_manager.write().await = manager; diff --git a/codex-rs/core/src/session/handlers.rs b/codex-rs/core/src/session/handlers.rs index 1dc68a69f3..03f29bac53 100644 --- a/codex-rs/core/src/session/handlers.rs +++ b/codex-rs/core/src/session/handlers.rs @@ -255,8 +255,11 @@ pub(super) async fn user_input_or_turn_inner( .set_responsesapi_client_metadata(responsesapi_client_metadata); } current_context.session_telemetry.user_prompt(&items); - sess.refresh_mcp_servers_if_requested(¤t_context) - .await; + sess.refresh_mcp_servers_if_requested( + ¤t_context, + Some(sess.mcp_elicitation_reviewer()), + ) + .await; let accepted_items = items.clone(); sess.spawn_task( Arc::clone(¤t_context), @@ -679,7 +682,8 @@ pub async fn review( let turn_context = sess.new_default_turn_with_sub_id(sub_id.clone()).await; sess.maybe_emit_unknown_model_warning_for_turn(turn_context.as_ref()) .await; - sess.refresh_mcp_servers_if_requested(&turn_context).await; + sess.refresh_mcp_servers_if_requested(&turn_context, Some(sess.mcp_elicitation_reviewer())) + .await; match resolve_review_request(review_request, &turn_context.cwd) { Ok(resolved) => { spawn_review_thread( diff --git a/codex-rs/core/src/session/mcp.rs b/codex-rs/core/src/session/mcp.rs index 38ade77ca3..ad04d30c66 100644 --- a/codex-rs/core/src/session/mcp.rs +++ b/codex-rs/core/src/session/mcp.rs @@ -1,6 +1,66 @@ use super::*; +use codex_mcp::ElicitationReviewRequest; +use codex_mcp::ElicitationReviewer; +use codex_mcp::ElicitationReviewerHandle; +use codex_protocol::config_types::ApprovalsReviewer; +use codex_protocol::mcp_approval_meta::APPROVAL_KIND_KEY as MCP_ELICITATION_APPROVAL_KIND_KEY; +use codex_protocol::mcp_approval_meta::APPROVAL_KIND_MCP_TOOL_CALL as MCP_ELICITATION_APPROVAL_KIND_MCP_TOOL_CALL; +use codex_protocol::mcp_approval_meta::APPROVALS_REVIEWER_KEY as MCP_ELICITATION_APPROVALS_REVIEWER_KEY; +use codex_protocol::mcp_approval_meta::CONNECTOR_DESCRIPTION_KEY as MCP_ELICITATION_CONNECTOR_DESCRIPTION_KEY; +use codex_protocol::mcp_approval_meta::CONNECTOR_ID_KEY as MCP_ELICITATION_CONNECTOR_ID_KEY; +use codex_protocol::mcp_approval_meta::CONNECTOR_NAME_KEY as MCP_ELICITATION_CONNECTOR_NAME_KEY; +use codex_protocol::mcp_approval_meta::REQUEST_TYPE_APPROVAL_REQUEST as MCP_ELICITATION_REQUEST_TYPE_APPROVAL_REQUEST; +use codex_protocol::mcp_approval_meta::REQUEST_TYPE_KEY as MCP_ELICITATION_REQUEST_TYPE_KEY; +use codex_protocol::mcp_approval_meta::TOOL_DESCRIPTION_KEY as MCP_ELICITATION_TOOL_DESCRIPTION_KEY; +use codex_protocol::mcp_approval_meta::TOOL_NAME_KEY as MCP_ELICITATION_TOOL_NAME_KEY; +use codex_protocol::mcp_approval_meta::TOOL_PARAMS_KEY as MCP_ELICITATION_TOOL_PARAMS_KEY; +use codex_protocol::mcp_approval_meta::TOOL_TITLE_KEY as MCP_ELICITATION_TOOL_TITLE_KEY; +use rmcp::model::CreateElicitationRequestParams; +use rmcp::model::ElicitationAction; +use rmcp::model::Meta; +use serde_json::Map; + +const MCP_ELICITATION_DECLINE_MESSAGE_KEY: &str = "message"; + +#[derive(Debug, PartialEq)] +enum GuardianElicitationReview { + NotRequested, + Decline(&'static str), + ApprovalRequest(Box), +} + +struct GuardianMcpElicitationReviewer { + session: std::sync::Weak, +} + +impl GuardianMcpElicitationReviewer { + fn new(session: &Arc) -> Self { + Self { + session: Arc::downgrade(session), + } + } +} + +impl ElicitationReviewer for GuardianMcpElicitationReviewer { + fn review( + &self, + request: ElicitationReviewRequest, + ) -> BoxFuture<'static, anyhow::Result>> { + let session = self.session.clone(); + Box::pin(async move { + let Some(session) = session.upgrade() else { + return Ok(None); + }; + review_guardian_mcp_elicitation(session, request).await + }) + } +} impl Session { + pub(crate) fn mcp_elicitation_reviewer(self: &Arc) -> ElicitationReviewerHandle { + Arc::new(GuardianMcpElicitationReviewer::new(self)) + } + #[expect( clippy::await_holding_invalid_type, reason = "active turn checks and turn state updates must remain atomic" @@ -221,6 +281,7 @@ impl Session { turn_context: &TurnContext, mcp_servers: HashMap, store_mode: OAuthCredentialsStoreMode, + elicitation_reviewer: Option, ) { let auth = self.services.auth_manager.auth().await; let config = self.get_config().await; @@ -269,6 +330,7 @@ impl Session { host_owned_codex_apps_enabled, tool_plugin_provenance, auth.as_ref(), + elicitation_reviewer, ) .await; { @@ -290,7 +352,11 @@ impl Session { old_manager.shutdown().await; } - pub(crate) async fn refresh_mcp_servers_if_requested(&self, turn_context: &TurnContext) { + pub(crate) async fn refresh_mcp_servers_if_requested( + &self, + turn_context: &TurnContext, + elicitation_reviewer: Option, + ) { let refresh_config = { self.pending_mcp_server_refresh_config.lock().await.take() }; let Some(refresh_config) = refresh_config else { return; @@ -319,7 +385,7 @@ impl Session { } }; - self.refresh_mcp_servers_inner(turn_context, mcp_servers, store_mode) + self.refresh_mcp_servers_inner(turn_context, mcp_servers, store_mode, elicitation_reviewer) .await; } @@ -328,8 +394,9 @@ impl Session { turn_context: &TurnContext, mcp_servers: HashMap, store_mode: OAuthCredentialsStoreMode, + elicitation_reviewer: Option, ) { - self.refresh_mcp_servers_inner(turn_context, mcp_servers, store_mode) + self.refresh_mcp_servers_inner(turn_context, mcp_servers, store_mode, elicitation_reviewer) .await; } @@ -350,3 +417,219 @@ impl Session { .cancel(); } } + +async fn review_guardian_mcp_elicitation( + session: Arc, + request: ElicitationReviewRequest, +) -> anyhow::Result> { + let Some((turn_context, _cancellation_token)) = + session.active_turn_context_and_cancellation_token().await + else { + return Ok(None); + }; + + if !crate::guardian::routes_approval_to_guardian(turn_context.as_ref()) { + return Ok(None); + } + + let guardian_request = match guardian_elicitation_review_request(&request) { + GuardianElicitationReview::NotRequested => return Ok(None), + GuardianElicitationReview::Decline(reason) => { + warn!( + server_name = %request.server_name, + request_id = %mcp_elicitation_request_id(&request.request_id), + reason, + "declining Guardian MCP elicitation before review" + ); + return Ok(Some(mcp_elicitation_decline_without_message())); + } + GuardianElicitationReview::ApprovalRequest(guardian_request) => *guardian_request, + }; + + let review_id = crate::guardian::new_guardian_review_id(); + let decision = crate::guardian::review_approval_request( + &session, + &turn_context, + review_id.clone(), + guardian_request, + /*retry_reason*/ None, + ) + .await; + Ok(Some( + mcp_elicitation_response_from_guardian_decision(session.as_ref(), &review_id, decision) + .await, + )) +} + +fn guardian_elicitation_review_request( + request: &ElicitationReviewRequest, +) -> GuardianElicitationReview { + let (meta, requested_schema) = match &request.elicitation { + CreateElicitationRequestParams::FormElicitationParams { + meta, + requested_schema, + .. + } => (meta, Some(requested_schema)), + CreateElicitationRequestParams::UrlElicitationParams { meta, .. } => { + return if meta_requests_approval_request(meta) { + GuardianElicitationReview::Decline( + "guardian MCP elicitation review only supports form elicitations", + ) + } else { + GuardianElicitationReview::NotRequested + }; + } + }; + + let Some(meta) = meta.as_ref().map(|meta| &meta.0) else { + return GuardianElicitationReview::NotRequested; + }; + if metadata_str(meta, MCP_ELICITATION_REQUEST_TYPE_KEY) + != Some(MCP_ELICITATION_REQUEST_TYPE_APPROVAL_REQUEST) + { + return GuardianElicitationReview::NotRequested; + } + if metadata_str(meta, MCP_ELICITATION_APPROVAL_KIND_KEY) + != Some(MCP_ELICITATION_APPROVAL_KIND_MCP_TOOL_CALL) + { + return GuardianElicitationReview::Decline( + "guardian MCP elicitation metadata must declare mcp_tool_call approval kind", + ); + } + if requested_schema.is_some_and(|schema| !schema.properties.is_empty()) { + return GuardianElicitationReview::Decline( + "guardian MCP elicitation review only supports empty form schemas", + ); + } + + let Some(tool_name) = metadata_owned_string(meta, MCP_ELICITATION_TOOL_NAME_KEY) else { + return GuardianElicitationReview::Decline( + "guardian MCP elicitation metadata must include a non-empty tool_name", + ); + }; + let arguments = match meta.get(MCP_ELICITATION_TOOL_PARAMS_KEY) { + Some(value @ Value::Object(_)) => Some(value.clone()), + Some(_) => { + return GuardianElicitationReview::Decline( + "guardian MCP elicitation tool_params must be an object", + ); + } + None => Some(Value::Object(Map::new())), + }; + + GuardianElicitationReview::ApprovalRequest(Box::new( + crate::guardian::GuardianApprovalRequest::McpToolCall { + id: format!( + "mcp_elicitation:{}:{}", + request.server_name, + mcp_elicitation_request_id(&request.request_id) + ), + server: request.server_name.clone(), + tool_name, + arguments, + connector_id: metadata_owned_string(meta, MCP_ELICITATION_CONNECTOR_ID_KEY), + connector_name: metadata_owned_string(meta, MCP_ELICITATION_CONNECTOR_NAME_KEY), + connector_description: metadata_owned_string( + meta, + MCP_ELICITATION_CONNECTOR_DESCRIPTION_KEY, + ), + tool_title: metadata_owned_string(meta, MCP_ELICITATION_TOOL_TITLE_KEY), + tool_description: metadata_owned_string(meta, MCP_ELICITATION_TOOL_DESCRIPTION_KEY), + annotations: None, + }, + )) +} + +fn meta_requests_approval_request(meta: &Option) -> bool { + meta.as_ref() + .and_then(|meta| metadata_str(&meta.0, MCP_ELICITATION_REQUEST_TYPE_KEY)) + == Some(MCP_ELICITATION_REQUEST_TYPE_APPROVAL_REQUEST) +} + +fn metadata_str<'a>(meta: &'a Map, key: &str) -> Option<&'a str> { + meta.get(key).and_then(Value::as_str) +} + +fn metadata_owned_string(meta: &Map, key: &str) -> Option { + metadata_str(meta, key) + .map(str::trim) + .filter(|value| !value.is_empty()) + .map(ToOwned::to_owned) +} + +fn mcp_elicitation_request_id(id: &RequestId) -> String { + match id { + rmcp::model::NumberOrString::String(value) => value.to_string(), + rmcp::model::NumberOrString::Number(value) => value.to_string(), + } +} + +async fn mcp_elicitation_response_from_guardian_decision( + session: &Session, + review_id: &str, + decision: ReviewDecision, +) -> ElicitationResponse { + let denial_message = match decision { + ReviewDecision::Denied => { + Some(crate::guardian::guardian_rejection_message(session, review_id).await) + } + _ => None, + }; + mcp_elicitation_response_from_guardian_decision_parts(decision, denial_message) +} + +fn mcp_elicitation_response_from_guardian_decision_parts( + decision: ReviewDecision, + denial_message: Option, +) -> ElicitationResponse { + match decision { + ReviewDecision::Approved + | ReviewDecision::ApprovedForSession + | ReviewDecision::ApprovedExecpolicyAmendment { .. } + | ReviewDecision::NetworkPolicyAmendment { .. } => ElicitationResponse { + action: ElicitationAction::Accept, + content: Some(serde_json::json!({})), + meta: Some(mcp_elicitation_auto_meta()), + }, + ReviewDecision::Denied => mcp_elicitation_decline_with_message( + denial_message.unwrap_or_else(|| "Guardian denied this request.".to_string()), + ), + ReviewDecision::TimedOut => { + mcp_elicitation_decline_with_message(crate::guardian::guardian_timeout_message()) + } + ReviewDecision::Abort => ElicitationResponse { + action: ElicitationAction::Cancel, + content: None, + meta: Some(mcp_elicitation_auto_meta()), + }, + } +} + +fn mcp_elicitation_decline_with_message(message: String) -> ElicitationResponse { + ElicitationResponse { + action: ElicitationAction::Decline, + content: None, + meta: Some(serde_json::json!({ + MCP_ELICITATION_DECLINE_MESSAGE_KEY: message, + MCP_ELICITATION_APPROVALS_REVIEWER_KEY: ApprovalsReviewer::AutoReview, + })), + } +} + +fn mcp_elicitation_decline_without_message() -> ElicitationResponse { + ElicitationResponse { + action: ElicitationAction::Decline, + content: None, + meta: Some(mcp_elicitation_auto_meta()), + } +} + +fn mcp_elicitation_auto_meta() -> serde_json::Value { + serde_json::json!({ + MCP_ELICITATION_APPROVALS_REVIEWER_KEY: ApprovalsReviewer::AutoReview, + }) +} + +#[cfg(test)] +#[path = "mcp_tests.rs"] +mod tests; diff --git a/codex-rs/core/src/session/mcp_tests.rs b/codex-rs/core/src/session/mcp_tests.rs new file mode 100644 index 0000000000..31b304faa5 --- /dev/null +++ b/codex-rs/core/src/session/mcp_tests.rs @@ -0,0 +1,212 @@ +use super::*; +use rmcp::model::BooleanSchema; +use rmcp::model::ElicitationSchema; +use rmcp::model::PrimitiveSchema; +use serde_json::json; + +fn meta(value: Value) -> Option { + let Value::Object(map) = value else { + panic!("metadata must be an object"); + }; + Some(Meta(map)) +} + +fn guardian_meta(tool_params: Option) -> Option { + let mut value = json!({ + "codex_approval_kind": "mcp_tool_call", + "codex_request_type": "approval_request", + "connector_id": "browser-use", + "connector_name": "Browser Use", + "tool_name": "access_browser_origin", + "tool_title": "Access browser origin", + }); + if let Some(tool_params) = tool_params { + value["tool_params"] = tool_params; + } + meta(value) +} + +fn form_request(meta: Option) -> ElicitationReviewRequest { + ElicitationReviewRequest { + server_name: "browser-use".to_string(), + request_id: rmcp::model::NumberOrString::Number(7), + elicitation: CreateElicitationRequestParams::FormElicitationParams { + meta, + message: "Allow origin?".to_string(), + requested_schema: ElicitationSchema::builder() + .build() + .expect("schema should build"), + }, + } +} + +#[test] +fn guardian_elicitation_review_request_builds_mcp_tool_call() { + let request = form_request(guardian_meta(Some(json!({ + "origin": "https://example.com", + })))); + + let GuardianElicitationReview::ApprovalRequest(guardian_request) = + guardian_elicitation_review_request(&request) + else { + panic!("expected Guardian MCP tool call request"); + }; + let crate::guardian::GuardianApprovalRequest::McpToolCall { + id, + server, + tool_name, + arguments, + connector_id, + connector_name, + connector_description, + tool_title, + tool_description, + annotations, + } = *guardian_request + else { + panic!("expected Guardian MCP tool call request"); + }; + + assert_eq!(id, "mcp_elicitation:browser-use:7"); + assert_eq!(server, "browser-use"); + assert_eq!(tool_name, "access_browser_origin"); + assert_eq!(arguments, Some(json!({ "origin": "https://example.com" }))); + assert_eq!(connector_id.as_deref(), Some("browser-use")); + assert_eq!(connector_name.as_deref(), Some("Browser Use")); + assert_eq!(connector_description, None); + assert_eq!(tool_title.as_deref(), Some("Access browser origin")); + assert_eq!(tool_description, None); + assert_eq!(annotations, None); +} + +#[test] +fn guardian_elicitation_review_request_defaults_missing_tool_params() { + let request = form_request(guardian_meta(/*tool_params*/ None)); + + let GuardianElicitationReview::ApprovalRequest(guardian_request) = + guardian_elicitation_review_request(&request) + else { + panic!("expected Guardian MCP tool call request"); + }; + let crate::guardian::GuardianApprovalRequest::McpToolCall { arguments, .. } = *guardian_request + else { + panic!("expected Guardian MCP tool call request"); + }; + + assert_eq!(arguments, Some(json!({}))); +} + +#[test] +fn guardian_elicitation_review_request_requires_opt_in() { + let request = form_request(meta(json!({ + "codex_approval_kind": "mcp_tool_call", + "tool_name": "access_browser_origin", + }))); + + assert_eq!( + guardian_elicitation_review_request(&request), + GuardianElicitationReview::NotRequested + ); +} + +#[test] +fn guardian_elicitation_review_request_declines_unsupported_opt_in_shapes() { + let url_request = ElicitationReviewRequest { + server_name: "browser-use".to_string(), + request_id: rmcp::model::NumberOrString::Number(8), + elicitation: CreateElicitationRequestParams::UrlElicitationParams { + meta: guardian_meta(Some(json!({}))), + message: "Open URL".to_string(), + url: "https://example.com".to_string(), + elicitation_id: "elicit-1".to_string(), + }, + }; + assert!(matches!( + guardian_elicitation_review_request(&url_request), + GuardianElicitationReview::Decline(_) + )); + + let non_empty_schema_request = ElicitationReviewRequest { + server_name: "browser-use".to_string(), + request_id: rmcp::model::NumberOrString::Number(9), + elicitation: CreateElicitationRequestParams::FormElicitationParams { + meta: guardian_meta(Some(json!({}))), + message: "Allow origin?".to_string(), + requested_schema: ElicitationSchema::builder() + .required_property("confirmed", PrimitiveSchema::Boolean(BooleanSchema::new())) + .build() + .expect("schema should build"), + }, + }; + assert!(matches!( + guardian_elicitation_review_request(&non_empty_schema_request), + GuardianElicitationReview::Decline(_) + )); + + let missing_tool_name_request = form_request(meta(json!({ + "codex_approval_kind": "mcp_tool_call", + "codex_request_type": "approval_request", + }))); + assert!(matches!( + guardian_elicitation_review_request(&missing_tool_name_request), + GuardianElicitationReview::Decline(_) + )); +} + +#[test] +fn guardian_decisions_map_to_elicitation_responses_without_session_state() { + assert_eq!( + mcp_elicitation_response_from_guardian_decision_parts( + ReviewDecision::Approved, + /*denial_message*/ None, + ), + ElicitationResponse { + action: ElicitationAction::Accept, + content: Some(json!({})), + meta: Some(json!({ + "approvals_reviewer": ApprovalsReviewer::AutoReview, + })), + } + ); + assert_eq!( + mcp_elicitation_response_from_guardian_decision_parts( + ReviewDecision::Denied, + Some("Denied by Guardian".to_string()), + ), + ElicitationResponse { + action: ElicitationAction::Decline, + content: None, + meta: Some(json!({ + "approvals_reviewer": ApprovalsReviewer::AutoReview, + "message": "Denied by Guardian", + })), + } + ); + assert_eq!( + mcp_elicitation_response_from_guardian_decision_parts( + ReviewDecision::TimedOut, + /*denial_message*/ None, + ), + ElicitationResponse { + action: ElicitationAction::Decline, + content: None, + meta: Some(json!({ + "approvals_reviewer": ApprovalsReviewer::AutoReview, + "message": crate::guardian::guardian_timeout_message(), + })), + } + ); + assert_eq!( + mcp_elicitation_response_from_guardian_decision_parts( + ReviewDecision::Abort, + /*denial_message*/ None, + ), + ElicitationResponse { + action: ElicitationAction::Cancel, + content: None, + meta: Some(json!({ + "approvals_reviewer": ApprovalsReviewer::AutoReview, + })), + } + ); +} diff --git a/codex-rs/core/src/session/session.rs b/codex-rs/core/src/session/session.rs index 6ed0c3fc1a..2ce01e81b3 100644 --- a/codex-rs/core/src/session/session.rs +++ b/codex-rs/core/src/session/session.rs @@ -976,6 +976,7 @@ impl Session { host_owned_codex_apps_enabled, tool_plugin_provenance, auth, + Some(sess.mcp_elicitation_reviewer()), ) .instrument(info_span!( "session_init.mcp_manager_init", diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index a5c5ec0ca4..4cce19f6bc 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -5620,7 +5620,7 @@ async fn refresh_mcp_servers_is_deferred_until_next_turn() { ); session - .refresh_mcp_servers_if_requested(&turn_context) + .refresh_mcp_servers_if_requested(&turn_context, /*elicitation_reviewer*/ None) .await; assert!(old_token.is_cancelled()); diff --git a/codex-rs/core/src/session/turn.rs b/codex-rs/core/src/session/turn.rs index 7aa64aae57..effbf5372f 100644 --- a/codex-rs/core/src/session/turn.rs +++ b/codex-rs/core/src/session/turn.rs @@ -240,6 +240,7 @@ pub(crate) async fn run_turn( turn_context.as_ref(), &cancellation_token, &mentioned_skills, + Some(sess.mcp_elicitation_reviewer()), ) .await; diff --git a/codex-rs/protocol/src/lib.rs b/codex-rs/protocol/src/lib.rs index 56248fd27e..63053159c6 100644 --- a/codex-rs/protocol/src/lib.rs +++ b/codex-rs/protocol/src/lib.rs @@ -15,6 +15,7 @@ pub mod error; pub mod exec_output; pub mod items; pub mod mcp; +pub mod mcp_approval_meta; pub mod memory_citation; pub mod models; pub mod network_policy; diff --git a/codex-rs/protocol/src/mcp_approval_meta.rs b/codex-rs/protocol/src/mcp_approval_meta.rs new file mode 100644 index 0000000000..7a8695a9b6 --- /dev/null +++ b/codex-rs/protocol/src/mcp_approval_meta.rs @@ -0,0 +1,19 @@ +pub const APPROVAL_KIND_KEY: &str = "codex_approval_kind"; +pub const APPROVAL_KIND_MCP_TOOL_CALL: &str = "mcp_tool_call"; +pub const APPROVAL_KIND_TOOL_SUGGESTION: &str = "tool_suggestion"; +pub const REQUEST_TYPE_KEY: &str = "codex_request_type"; +pub const REQUEST_TYPE_APPROVAL_REQUEST: &str = "approval_request"; +pub const APPROVALS_REVIEWER_KEY: &str = "approvals_reviewer"; +pub const PERSIST_KEY: &str = "persist"; +pub const PERSIST_SESSION: &str = "session"; +pub const PERSIST_ALWAYS: &str = "always"; +pub const SOURCE_KEY: &str = "source"; +pub const SOURCE_CONNECTOR: &str = "connector"; +pub const CONNECTOR_ID_KEY: &str = "connector_id"; +pub const CONNECTOR_NAME_KEY: &str = "connector_name"; +pub const CONNECTOR_DESCRIPTION_KEY: &str = "connector_description"; +pub const TOOL_NAME_KEY: &str = "tool_name"; +pub const TOOL_TITLE_KEY: &str = "tool_title"; +pub const TOOL_DESCRIPTION_KEY: &str = "tool_description"; +pub const TOOL_PARAMS_KEY: &str = "tool_params"; +pub const TOOL_PARAMS_DISPLAY_KEY: &str = "tool_params_display"; diff --git a/codex-rs/tui/src/bottom_pane/mcp_server_elicitation.rs b/codex-rs/tui/src/bottom_pane/mcp_server_elicitation.rs index f307c28d72..95e8e8ca1e 100644 --- a/codex-rs/tui/src/bottom_pane/mcp_server_elicitation.rs +++ b/codex-rs/tui/src/bottom_pane/mcp_server_elicitation.rs @@ -12,6 +12,15 @@ use codex_app_server_protocol::McpServerElicitationRequest; use codex_app_server_protocol::McpServerElicitationRequestParams; use codex_app_server_protocol::RequestId as AppServerRequestId; use codex_protocol::ThreadId; +use codex_protocol::mcp_approval_meta::APPROVAL_KIND_KEY as APPROVAL_META_KIND_KEY; +use codex_protocol::mcp_approval_meta::APPROVAL_KIND_MCP_TOOL_CALL as APPROVAL_META_KIND_MCP_TOOL_CALL; +use codex_protocol::mcp_approval_meta::APPROVAL_KIND_TOOL_SUGGESTION as APPROVAL_META_KIND_TOOL_SUGGESTION; +use codex_protocol::mcp_approval_meta::PERSIST_ALWAYS as APPROVAL_PERSIST_ALWAYS_VALUE; +use codex_protocol::mcp_approval_meta::PERSIST_KEY as APPROVAL_PERSIST_KEY; +use codex_protocol::mcp_approval_meta::PERSIST_SESSION as APPROVAL_PERSIST_SESSION_VALUE; +use codex_protocol::mcp_approval_meta::TOOL_NAME_KEY; +use codex_protocol::mcp_approval_meta::TOOL_PARAMS_DISPLAY_KEY as APPROVAL_TOOL_PARAMS_DISPLAY_KEY; +use codex_protocol::mcp_approval_meta::TOOL_PARAMS_KEY as APPROVAL_TOOL_PARAMS_KEY; use codex_protocol::user_input::TextElement; use crossterm::event::KeyCode; use crossterm::event::KeyEvent; @@ -55,19 +64,10 @@ const APPROVAL_ACCEPT_SESSION_VALUE: &str = "accept_session"; const APPROVAL_ACCEPT_ALWAYS_VALUE: &str = "accept_always"; const APPROVAL_DECLINE_VALUE: &str = "decline"; const APPROVAL_CANCEL_VALUE: &str = "cancel"; -const APPROVAL_META_KIND_KEY: &str = "codex_approval_kind"; -const APPROVAL_META_KIND_MCP_TOOL_CALL: &str = "mcp_tool_call"; -const APPROVAL_META_KIND_TOOL_SUGGESTION: &str = "tool_suggestion"; -const APPROVAL_PERSIST_KEY: &str = "persist"; -const APPROVAL_PERSIST_SESSION_VALUE: &str = "session"; -const APPROVAL_PERSIST_ALWAYS_VALUE: &str = "always"; -const APPROVAL_TOOL_PARAMS_KEY: &str = "tool_params"; -const APPROVAL_TOOL_PARAMS_DISPLAY_KEY: &str = "tool_params_display"; const APPROVAL_TOOL_PARAM_DISPLAY_LIMIT: usize = 3; const APPROVAL_TOOL_PARAM_VALUE_TRUNCATE_GRAPHEMES: usize = 60; const TOOL_TYPE_KEY: &str = "tool_type"; const TOOL_ID_KEY: &str = "tool_id"; -const TOOL_NAME_KEY: &str = "tool_name"; const TOOL_SUGGEST_SUGGEST_TYPE_KEY: &str = "suggest_type"; const TOOL_SUGGEST_REASON_KEY: &str = "suggest_reason"; const TOOL_SUGGEST_INSTALL_URL_KEY: &str = "install_url"; From f9063045e1df683b58541c6e7600903752092a28 Mon Sep 17 00:00:00 2001 From: "Christoph Paasch (OpenAI)" Date: Wed, 6 May 2026 12:49:13 -0700 Subject: [PATCH 2/9] Avoid noisy OTEL diagnostics in codex exec (#21107) `codex exec` should not print OpenTelemetry exporter self-diagnostics to stderr by default. Suppress the SDK and OTLP exporter targets unless callers explicitly opt in with `RUST_LOG`. Also stop defaulting the trace exporter to the log exporter, since OTLP HTTP endpoints are signal-specific and a logs endpoint is not valid for spans. Co-authored-by: Codex Co-authored-by: Codex --- codex-rs/core/src/config/config_tests.rs | 33 +++++++++++++ codex-rs/core/src/config/mod.rs | 5 +- codex-rs/exec/src/lib.rs | 19 ++++---- codex-rs/exec/src/lib_tests.rs | 59 ++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 10 deletions(-) diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index 4c6d1a87e0..9b785ff337 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -7113,6 +7113,39 @@ async fn metrics_exporter_defaults_to_statsig_when_missing() -> std::io::Result< Ok(()) } +#[tokio::test] +async fn trace_exporter_defaults_to_none_when_log_exporter_is_set() -> std::io::Result<()> { + let fixture = create_test_fixture()?; + let mut cfg = fixture.cfg.clone(); + cfg.otel = Some(OtelConfigToml { + exporter: Some(OtelExporterKind::OtlpHttp { + endpoint: "http://localhost:14318/v1/logs".to_string(), + headers: HashMap::new(), + protocol: codex_config::types::OtelHttpProtocol::Binary, + tls: None, + }), + metrics_exporter: Some(OtelExporterKind::None), + ..Default::default() + }); + + let config = Config::load_from_base_config_with_overrides( + cfg, + ConfigOverrides { + cwd: Some(fixture.cwd_path()), + ..Default::default() + }, + fixture.codex_home(), + ) + .await?; + + assert!(matches!( + config.otel.exporter, + OtelExporterKind::OtlpHttp { .. } + )); + assert_eq!(config.otel.trace_exporter, OtelExporterKind::None); + Ok(()) +} + #[tokio::test] async fn explicit_null_service_tier_override_sets_fast_default_opt_out() -> std::io::Result<()> { let fixture = create_test_fixture()?; diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index 6dbc9519a9..75d99f4b18 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -3209,7 +3209,10 @@ impl Config { .environment .unwrap_or(DEFAULT_OTEL_ENVIRONMENT.to_string()); let exporter = t.exporter.unwrap_or(OtelExporterKind::None); - let trace_exporter = t.trace_exporter.unwrap_or_else(|| exporter.clone()); + // OTLP HTTP endpoints are signal-specific in our config, so + // enabling log export must not implicitly send spans to a + // /v1/logs endpoint. + let trace_exporter = t.trace_exporter.unwrap_or(OtelExporterKind::None); let metrics_exporter = t.metrics_exporter.unwrap_or(OtelExporterKind::Statsig); OtelConfig { log_user_prompt, diff --git a/codex-rs/exec/src/lib.rs b/codex-rs/exec/src/lib.rs index a26dc525e6..b035a19517 100644 --- a/codex-rs/exec/src/lib.rs +++ b/codex-rs/exec/src/lib.rs @@ -156,6 +156,7 @@ use crate::cli::Command as ExecCommand; use crate::event_processor::EventProcessor; const DEFAULT_ANALYTICS_ENABLED: bool = true; +const EXEC_DEFAULT_LOG_FILTER: &str = "error,opentelemetry_sdk=off,opentelemetry_otlp=off"; enum InitialOperation { UserTurn { @@ -222,6 +223,14 @@ fn exec_root_span() -> tracing::Span { ) } +fn exec_stderr_env_filter() -> EnvFilter { + // OTEL export is best-effort; keep exporter self-diagnostics out of + // headless command output unless the caller opts in with RUST_LOG. + EnvFilter::try_from_default_env() + .or_else(|_| EnvFilter::try_new(EXEC_DEFAULT_LOG_FILTER)) + .unwrap_or_else(|_| EnvFilter::new("error")) +} + pub async fn run_main(cli: Cli, arg0_paths: Arg0DispatchPaths) -> anyhow::Result<()> { #[allow(clippy::print_stderr)] if let Some(message) = cli.removed_full_auto_warning() { @@ -268,18 +277,10 @@ pub async fn run_main(cli: Cli, arg0_paths: Arg0DispatchPaths) -> anyhow::Result supports_color::on_cached(Stream::Stderr).is_some(), ), }; - // Build fmt layer (existing logging) to compose with OTEL layer. - let default_level = "error"; - - // Build env_filter separately and attach via with_filter. - let env_filter = EnvFilter::try_from_default_env() - .or_else(|_| EnvFilter::try_new(default_level)) - .unwrap_or_else(|_| EnvFilter::new(default_level)); - let fmt_layer = tracing_subscriber::fmt::layer() .with_ansi(stderr_with_ansi) .with_writer(std::io::stderr) - .with_filter(env_filter); + .with_filter(exec_stderr_env_filter()); let sandbox_mode = if removed_full_auto { Some(SandboxMode::WorkspaceWrite) diff --git a/codex-rs/exec/src/lib_tests.rs b/codex-rs/exec/src/lib_tests.rs index caa2921d19..c12b483e89 100644 --- a/codex-rs/exec/src/lib_tests.rs +++ b/codex-rs/exec/src/lib_tests.rs @@ -8,6 +8,10 @@ use opentelemetry::trace::TraceId; use opentelemetry::trace::TracerProvider as _; use opentelemetry_sdk::trace::SdkTracerProvider; use pretty_assertions::assert_eq; +use std::io; +use std::io::Write; +use std::sync::Arc; +use std::sync::Mutex; use tempfile::tempdir; use tracing_opentelemetry::OpenTelemetrySpanExt; @@ -22,6 +26,61 @@ fn exec_defaults_analytics_to_enabled() { assert_eq!(DEFAULT_ANALYTICS_ENABLED, true); } +#[derive(Clone)] +struct TestLogWriter { + buffer: Arc>>, +} + +struct TestLogSink { + buffer: Arc>>, +} + +impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for TestLogWriter { + type Writer = TestLogSink; + + fn make_writer(&'a self) -> Self::Writer { + TestLogSink { + buffer: Arc::clone(&self.buffer), + } + } +} + +impl Write for TestLogSink { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.buffer.lock().expect("log buffer lock").extend(buf); + Ok(buf.len()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +#[test] +fn exec_default_stderr_filter_suppresses_otel_self_diagnostics() { + let buffer = Arc::new(Mutex::new(Vec::new())); + let writer = TestLogWriter { + buffer: Arc::clone(&buffer), + }; + let subscriber = tracing_subscriber::registry().with( + tracing_subscriber::fmt::layer() + .with_ansi(false) + .with_writer(writer) + .with_filter(EnvFilter::try_new(EXEC_DEFAULT_LOG_FILTER).expect("default filter")), + ); + + tracing::subscriber::with_default(subscriber, || { + tracing::error!(target: "opentelemetry_sdk", "telemetry export failed"); + tracing::error!(target: "opentelemetry_otlp", "telemetry request failed"); + tracing::error!(target: "codex_exec_test", "real exec error"); + }); + + let logs = String::from_utf8(buffer.lock().expect("log buffer lock").clone()).expect("utf8"); + assert!(!logs.contains("telemetry export failed")); + assert!(!logs.contains("telemetry request failed")); + assert!(logs.contains("real exec error")); +} + #[test] fn exec_root_span_can_be_parented_from_trace_context() { let subscriber = test_tracing_subscriber(); From 63a27ad6c6749dbc5e35205cb4619a8957f3fd19 Mon Sep 17 00:00:00 2001 From: starr-openai Date: Wed, 6 May 2026 12:54:26 -0700 Subject: [PATCH 3/9] Avoid hard-coded environment context shell (#21390) ## Summary - make resolved turn environment shell metadata optional instead of hard-coding bash - render environment context shells from explicit environment metadata when present, falling back to the existing session shell - update environment context tests for inherited PowerShell-style fallback and explicit per-environment shell override ## Testing - Not run (not requested; formatted with `just fmt`). Co-authored-by: Codex --- .../core/src/context/environment_context.rs | 14 +++--- .../src/context/environment_context_tests.rs | 43 ++++++++++++++++++ codex-rs/core/src/context_manager/updates.rs | 2 +- codex-rs/core/src/environment_selection.rs | 5 +-- codex-rs/core/src/session/mod.rs | 3 +- codex-rs/core/src/session/tests.rs | 45 ++++++++++++++++++- codex-rs/core/src/session/turn_context.rs | 2 +- codex-rs/core/tests/suite/prompt_caching.rs | 3 +- 8 files changed, 103 insertions(+), 14 deletions(-) diff --git a/codex-rs/core/src/context/environment_context.rs b/codex-rs/core/src/context/environment_context.rs index c647550ee2..ca1ac5f2fb 100644 --- a/codex-rs/core/src/context/environment_context.rs +++ b/codex-rs/core/src/context/environment_context.rs @@ -1,4 +1,6 @@ use crate::session::turn_context::TurnContext; +use crate::session::turn_context::TurnEnvironment; +use crate::shell::Shell; use codex_protocol::protocol::TurnContextItem; use codex_protocol::protocol::TurnContextNetworkItem; use codex_utils_absolute_path::AbsolutePathBuf; @@ -30,15 +32,16 @@ impl EnvironmentContextEnvironment { } } - fn from_turn_environments( - environments: &[crate::session::turn_context::TurnEnvironment], - ) -> Vec { + fn from_turn_environments(environments: &[TurnEnvironment], shell: &Shell) -> Vec { environments .iter() .map(|environment| Self { id: environment.environment_id.clone(), cwd: environment.cwd.clone(), - shell: environment.shell.clone(), + shell: environment + .shell + .clone() + .unwrap_or_else(|| shell.name().to_string()), }) .collect() } @@ -174,10 +177,11 @@ impl EnvironmentContext { ) } - pub(crate) fn from_turn_context(turn_context: &TurnContext) -> Self { + pub(crate) fn from_turn_context(turn_context: &TurnContext, shell: &Shell) -> Self { Self::new( EnvironmentContextEnvironment::from_turn_environments( &turn_context.environments.turn_environments, + shell, ), turn_context.current_date.clone(), turn_context.timezone.clone(), diff --git a/codex-rs/core/src/context/environment_context_tests.rs b/codex-rs/core/src/context/environment_context_tests.rs index 24ff4bbfff..bc0a17ca5d 100644 --- a/codex-rs/core/src/context/environment_context_tests.rs +++ b/codex-rs/core/src/context/environment_context_tests.rs @@ -259,3 +259,46 @@ fn serialize_environment_context_with_multiple_selected_environments() { assert_eq!(context.render(), expected); } + +#[test] +fn serialize_environment_context_prefers_environment_shell_when_present() { + let local_cwd = test_path_buf("/repo/local"); + let remote_cwd = test_path_buf("/repo/remote"); + let context = EnvironmentContext::new( + vec![ + EnvironmentContextEnvironment { + id: "local".to_string(), + cwd: local_cwd.abs(), + shell: "powershell".to_string(), + }, + EnvironmentContextEnvironment { + id: "remote".to_string(), + cwd: remote_cwd.abs(), + shell: "cmd".to_string(), + }, + ], + /*current_date*/ None, + /*timezone*/ None, + /*network*/ None, + /*subagents*/ None, + ); + + let expected = format!( + r#" + + + {} + powershell + + + {} + cmd + + +"#, + local_cwd.display(), + remote_cwd.display() + ); + + assert_eq!(context.render(), expected); +} diff --git a/codex-rs/core/src/context_manager/updates.rs b/codex-rs/core/src/context_manager/updates.rs index db7850008f..1bc2cb0895 100644 --- a/codex-rs/core/src/context_manager/updates.rs +++ b/codex-rs/core/src/context_manager/updates.rs @@ -29,7 +29,7 @@ fn build_environment_update_item( let prev = previous?; let prev_context = EnvironmentContext::from_turn_context_item(prev, shell.name().to_string()); - let next_context = EnvironmentContext::from_turn_context(next); + let next_context = EnvironmentContext::from_turn_context(next, shell); if prev_context.equals_except_shell(&next_context) { return None; } diff --git a/codex-rs/core/src/environment_selection.rs b/codex-rs/core/src/environment_selection.rs index e9d617cbf4..b4bd9cbe89 100644 --- a/codex-rs/core/src/environment_selection.rs +++ b/codex-rs/core/src/environment_selection.rs @@ -75,9 +75,7 @@ pub(crate) fn resolve_environment_selections( environment_id, environment, cwd: selected_environment.cwd.clone(), - // TODO(starr): Resolve shell metadata per environment instead of - // hardcoding bash. - shell: "bash".to_string(), + shell: None, }); } @@ -176,5 +174,6 @@ mod tests { .environment_id, "local" ); + assert_eq!(resolved.primary().expect("primary environment").shell, None); } } diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index bacd1708eb..8782f14b3d 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -2703,13 +2703,14 @@ impl Session { ); } if turn_context.config.include_environment_context { + let shell = self.user_shell(); let subagents = self .services .agent_control .format_environment_context_subagents(self.conversation_id) .await; contextual_user_sections.push( - crate::context::EnvironmentContext::from_turn_context(turn_context) + crate::context::EnvironmentContext::from_turn_context(turn_context, shell.as_ref()) .with_subagents(subagents) .render(), ); diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index 4cce19f6bc..cae3e1f978 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -3057,7 +3057,7 @@ fn turn_environments_for_tests( environment_id: codex_exec_server::LOCAL_ENVIRONMENT_ID.to_string(), environment: Arc::clone(environment), cwd: cwd.clone(), - shell: "bash".to_string(), + shell: None, }] } @@ -4803,7 +4803,7 @@ async fn primary_environment_uses_first_turn_environment() { environment_id: "second".to_string(), environment: Arc::clone(&first_environment.environment), cwd: second_cwd.clone(), - shell: "bash".to_string(), + shell: None, }); assert_eq!( @@ -5749,6 +5749,47 @@ async fn build_settings_update_items_emits_environment_item_for_network_changes( assert!(environment_update.contains("blocked.example.com")); } +#[tokio::test] +async fn environment_context_uses_session_shell_when_environment_shell_is_absent() { + let (mut session, mut turn_context) = make_session_and_context().await; + session.services.user_shell = Arc::new(crate::shell::Shell { + shell_type: crate::shell::ShellType::PowerShell, + shell_path: PathBuf::from("powershell"), + shell_snapshot: crate::shell::empty_shell_snapshot_receiver(), + }); + for environment in &mut turn_context.environments.turn_environments { + environment.shell = None; + } + + let session_shell = session.user_shell(); + let environment_context = crate::context::EnvironmentContext::from_turn_context( + &turn_context, + session_shell.as_ref(), + ) + .render(); + assert!( + environment_context.contains("powershell"), + "{environment_context}" + ); + + let primary_environment = turn_context + .environments + .turn_environments + .first_mut() + .expect("primary environment"); + primary_environment.shell = Some("cmd".to_string()); + + let environment_context = crate::context::EnvironmentContext::from_turn_context( + &turn_context, + session_shell.as_ref(), + ) + .render(); + assert!( + environment_context.contains("cmd"), + "{environment_context}" + ); +} + #[tokio::test] async fn build_settings_update_items_emits_environment_item_for_time_changes() { let (session, previous_context) = make_session_and_context().await; diff --git a/codex-rs/core/src/session/turn_context.rs b/codex-rs/core/src/session/turn_context.rs index 6d8443e933..769953475e 100644 --- a/codex-rs/core/src/session/turn_context.rs +++ b/codex-rs/core/src/session/turn_context.rs @@ -39,7 +39,7 @@ pub(crate) struct TurnEnvironment { pub(crate) environment_id: String, pub(crate) environment: Arc, pub(crate) cwd: AbsolutePathBuf, - pub(crate) shell: String, + pub(crate) shell: Option, } impl TurnEnvironment { diff --git a/codex-rs/core/tests/suite/prompt_caching.rs b/codex-rs/core/tests/suite/prompt_caching.rs index cc8e57f0f8..b81bb06bb9 100644 --- a/codex-rs/core/tests/suite/prompt_caching.rs +++ b/codex-rs/core/tests/suite/prompt_caching.rs @@ -1,6 +1,7 @@ #![allow(clippy::unwrap_used)] use codex_apply_patch::APPLY_PATCH_TOOL_INSTRUCTIONS; +use codex_core::shell::default_user_shell; use codex_features::Feature; use codex_protocol::config_types::CollaborationMode; use codex_protocol::config_types::ModeKind; @@ -54,7 +55,7 @@ fn assert_default_env_context(text: &str, cwd: &str) { "expected cwd in environment context: {text}" ); assert!( - text.contains("bash"), + text.contains(&format!("{}", default_user_shell().name())), "expected shell in environment context: {text}" ); assert!( From d5f0b6d63af4008ebf782bc6ed34f7cfb418be2e Mon Sep 17 00:00:00 2001 From: canvrno-oai Date: Wed, 6 May 2026 13:11:44 -0700 Subject: [PATCH 4/9] [codex] Dedupe fallback model metadata warnings (#21090) Fixes #21070. This is a small cleanup around model metadata handling for gateway/provider model names. It follows the report and proposed direction from @dkbush by keeping the fallback metadata warning useful without repeating it every turn, and by tightening the existing provider-prefix lookup path. - Track fallback metadata warning slugs in session state so each unresolved model warns once per session. - Keep warning emission outside the session-state lock and preserve the existing warning text. - Allow one-segment provider prefixes with hyphenated provider IDs, while preserving the multi-segment rejection behavior. - Add focused coverage for warning dedupe and hyphenated provider-prefix metadata matching. Testing: - Ran `just fmt`. - Ran `git diff --check`. - Added tests for the new warning dedupe and provider-prefix lookup behavior. --- codex-rs/models-manager/src/manager.rs | 11 ++++---- codex-rs/models-manager/src/manager_tests.rs | 15 ++++++++++ codex-rs/tui/src/chatwidget.rs | 10 ++++++- codex-rs/tui/src/chatwidget/tests/helpers.rs | 1 + .../src/chatwidget/tests/status_and_layout.rs | 28 +++++++++++++++++++ codex-rs/tui/src/chatwidget/warnings.rs | 23 +++++++++++++++ 6 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 codex-rs/tui/src/chatwidget/warnings.rs diff --git a/codex-rs/models-manager/src/manager.rs b/codex-rs/models-manager/src/manager.rs index 5859830310..517bb0abbb 100644 --- a/codex-rs/models-manager/src/manager.rs +++ b/codex-rs/models-manager/src/manager.rs @@ -421,15 +421,16 @@ fn find_model_by_longest_prefix(model: &str, candidates: &[ModelInfo]) -> Option fn find_model_by_namespaced_suffix(model: &str, candidates: &[ModelInfo]) -> Option { // Retry metadata lookup for a single namespaced slug like `namespace/model-name`. // - // This only strips one leading namespace segment and only when the namespace is ASCII - // alphanumeric/underscore (`\w+`) to avoid broadly matching arbitrary aliases. + // This only strips one leading namespace segment and only when the namespace looks + // like a simple provider id to avoid broadly matching arbitrary aliases. let (namespace, suffix) = model.split_once('/')?; if suffix.contains('/') { return None; } - if !namespace - .chars() - .all(|c| c.is_ascii_alphanumeric() || c == '_') + if namespace.is_empty() + || !namespace + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') { return None; } diff --git a/codex-rs/models-manager/src/manager_tests.rs b/codex-rs/models-manager/src/manager_tests.rs index 24ae9f3591..9df9fb09c9 100644 --- a/codex-rs/models-manager/src/manager_tests.rs +++ b/codex-rs/models-manager/src/manager_tests.rs @@ -295,6 +295,21 @@ async fn get_model_info_matches_namespaced_suffix() { assert!(!model_info.used_fallback_model_metadata); } +#[tokio::test] +async fn get_model_info_matches_hyphenated_provider_namespace_suffix() { + let config = ModelsManagerConfig::default(); + let remote = remote_model("gpt-image", "Image", /*priority*/ 0); + let manager = static_manager_for_tests(ModelsResponse { + models: vec![remote], + }); + let namespaced_model = "openai-codex/gpt-image".to_string(); + + let model_info = manager.get_model_info(&namespaced_model, &config).await; + + assert_eq!(model_info.slug, namespaced_model); + assert!(!model_info.used_fallback_model_metadata); +} + #[tokio::test] async fn get_model_info_rejects_multi_segment_namespace_suffix_matching() { let codex_home = tempdir().expect("temp dir"); diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 0dbf0d235a..adaba76b24 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -351,6 +351,8 @@ use self::status_surfaces::TerminalTitleStatusKind; mod user_messages; use self::user_messages::PendingSteerCompareKey; use self::user_messages::UserMessageDisplay; +mod warnings; +use self::warnings::WarningDisplayState; pub(crate) use crate::branch_summary::StatusLineGitSummary; use crate::streaming::chunking::AdaptiveChunkingPolicy; use crate::streaming::commit_tick::CommitTickScope; @@ -780,6 +782,7 @@ pub(crate) struct ChatWidget { plan_type: Option, codex_rate_limit_reached_type: Option, rate_limit_warnings: RateLimitWarningState, + warning_display_state: WarningDisplayState, rate_limit_switch_prompt: RateLimitSwitchPromptState, add_credits_nudge_email_in_flight: Option, adaptive_chunking: AdaptiveChunkingPolicy, @@ -3133,7 +3136,11 @@ impl ChatWidget { } fn on_warning(&mut self, message: impl Into) { - self.add_to_history(history_cell::new_warning_event(message.into())); + let message = message.into(); + if !self.warning_display_state.should_display(&message) { + return; + } + self.add_to_history(history_cell::new_warning_event(message)); self.request_redraw(); } @@ -4941,6 +4948,7 @@ impl ChatWidget { plan_type: initial_plan_type, codex_rate_limit_reached_type: None, rate_limit_warnings: RateLimitWarningState::default(), + warning_display_state: WarningDisplayState::default(), rate_limit_switch_prompt: RateLimitSwitchPromptState::default(), add_credits_nudge_email_in_flight: None, adaptive_chunking: AdaptiveChunkingPolicy::default(), diff --git a/codex-rs/tui/src/chatwidget/tests/helpers.rs b/codex-rs/tui/src/chatwidget/tests/helpers.rs index 0d781a6a38..307e9d7fab 100644 --- a/codex-rs/tui/src/chatwidget/tests/helpers.rs +++ b/codex-rs/tui/src/chatwidget/tests/helpers.rs @@ -211,6 +211,7 @@ pub(super) async fn make_chatwidget_manual( plan_type: None, codex_rate_limit_reached_type: None, rate_limit_warnings: RateLimitWarningState::default(), + warning_display_state: WarningDisplayState::default(), rate_limit_switch_prompt: RateLimitSwitchPromptState::default(), add_credits_nudge_email_in_flight: None, adaptive_chunking: crate::streaming::chunking::AdaptiveChunkingPolicy::default(), diff --git a/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs b/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs index dcb72ed3b2..89bb715be5 100644 --- a/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs +++ b/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs @@ -1323,6 +1323,34 @@ async fn warning_event_adds_warning_history_cell() { ); } +#[tokio::test] +async fn repeated_model_metadata_warning_is_hidden_for_same_slug() { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + let warning = "Model metadata for `unknown-model` not found. Defaulting to fallback metadata; this can degrade performance and cause issues."; + + handle_warning(&mut chat, warning); + handle_warning(&mut chat, warning); + + let cells = drain_insert_history(&mut rx); + assert_eq!(cells.len(), 1, "expected one warning history cell"); + let rendered = lines_to_single_string(&cells[0]); + assert!( + rendered.contains("unknown-model"), + "warning cell missing model slug: {rendered}" + ); +} + +#[tokio::test] +async fn repeated_generic_warning_is_not_hidden() { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + + handle_warning(&mut chat, "test warning message"); + handle_warning(&mut chat, "test warning message"); + + let cells = drain_insert_history(&mut rx); + assert_eq!(cells.len(), 2, "expected both warning history cells"); +} + #[tokio::test] async fn status_line_invalid_items_warn_once() { let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; diff --git a/codex-rs/tui/src/chatwidget/warnings.rs b/codex-rs/tui/src/chatwidget/warnings.rs new file mode 100644 index 0000000000..ef9660dcff --- /dev/null +++ b/codex-rs/tui/src/chatwidget/warnings.rs @@ -0,0 +1,23 @@ +use std::collections::HashSet; + +const FALLBACK_MODEL_METADATA_WARNING_PREFIX: &str = "Model metadata for `"; +const FALLBACK_MODEL_METADATA_WARNING_SUFFIX: &str = + "` not found. Defaulting to fallback metadata; this can degrade performance and cause issues."; + +#[derive(Default)] +pub(super) struct WarningDisplayState { + fallback_model_metadata_slugs: HashSet, +} + +impl WarningDisplayState { + pub(super) fn should_display(&mut self, message: &str) -> bool { + fallback_model_metadata_warning_slug(message) + .is_none_or(|slug| self.fallback_model_metadata_slugs.insert(slug.to_string())) + } +} + +fn fallback_model_metadata_warning_slug(message: &str) -> Option<&str> { + message + .strip_prefix(FALLBACK_MODEL_METADATA_WARNING_PREFIX)? + .strip_suffix(FALLBACK_MODEL_METADATA_WARNING_SUFFIX) +} From b9c50a53d7cf45a0c937f7f313bead536d2974d1 Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Wed, 6 May 2026 13:12:24 -0700 Subject: [PATCH 5/9] [codex] Split tool handlers into separate files (#21395) ## Why Several tool handler modules still bundled multiple `ToolHandler` implementations in one file. That made the handler directory harder to navigate and made otherwise local handler edits land in large shared modules. ## What - Split grouped tool handlers into one handler file each for agent jobs, goals, MCP resources, shell tools, and unified exec. - Kept shared parsing, payload, and runtime helpers in the existing parent modules, with re-exports preserving the existing handler import paths. - Updated the shell handler tests to construct `ShellCommandHandler` through the existing `ShellCommandBackendConfig` conversion now that the backend detail lives with the shell-command handler. ## Validation - `cargo check -p codex-core` - `cargo clippy -p codex-core --lib -- -D warnings` - `git diff --check -- codex-rs/core/src/tools/handlers` Targeted `codex-core` handler tests did not run locally because `core_test_support` currently fails to compile before reaching these tests due to an unresolved `similar` import. --- .../core/src/tools/handlers/agent_jobs.rs | 371 +------- .../agent_jobs/report_agent_job_result.rs | 86 ++ .../agent_jobs/spawn_agents_on_csv.rs | 284 ++++++ codex-rs/core/src/tools/handlers/goal.rs | 164 +--- .../src/tools/handlers/goal/create_goal.rs | 72 ++ .../core/src/tools/handlers/goal/get_goal.rs | 45 + .../src/tools/handlers/goal/update_goal.rs | 75 ++ .../core/src/tools/handlers/mcp_resource.rs | 403 +-------- .../list_mcp_resource_templates.rs | 160 ++++ .../mcp_resource/list_mcp_resources.rs | 158 ++++ .../mcp_resource/read_mcp_resource.rs | 141 +++ codex-rs/core/src/tools/handlers/shell.rs | 850 ++++-------------- .../tools/handlers/shell/container_exec.rs | 101 +++ .../src/tools/handlers/shell/local_shell.rs | 102 +++ .../src/tools/handlers/shell/shell_command.rs | 215 +++++ .../src/tools/handlers/shell/shell_handler.rs | 130 +++ .../core/src/tools/handlers/shell_tests.rs | 8 +- .../core/src/tools/handlers/unified_exec.rs | 393 +------- .../handlers/unified_exec/exec_command.rs | 309 +++++++ .../handlers/unified_exec/write_stdin.rs | 104 +++ 20 files changed, 2208 insertions(+), 1963 deletions(-) create mode 100644 codex-rs/core/src/tools/handlers/agent_jobs/report_agent_job_result.rs create mode 100644 codex-rs/core/src/tools/handlers/agent_jobs/spawn_agents_on_csv.rs create mode 100644 codex-rs/core/src/tools/handlers/goal/create_goal.rs create mode 100644 codex-rs/core/src/tools/handlers/goal/get_goal.rs create mode 100644 codex-rs/core/src/tools/handlers/goal/update_goal.rs create mode 100644 codex-rs/core/src/tools/handlers/mcp_resource/list_mcp_resource_templates.rs create mode 100644 codex-rs/core/src/tools/handlers/mcp_resource/list_mcp_resources.rs create mode 100644 codex-rs/core/src/tools/handlers/mcp_resource/read_mcp_resource.rs create mode 100644 codex-rs/core/src/tools/handlers/shell/container_exec.rs create mode 100644 codex-rs/core/src/tools/handlers/shell/local_shell.rs create mode 100644 codex-rs/core/src/tools/handlers/shell/shell_command.rs create mode 100644 codex-rs/core/src/tools/handlers/shell/shell_handler.rs create mode 100644 codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs create mode 100644 codex-rs/core/src/tools/handlers/unified_exec/write_stdin.rs diff --git a/codex-rs/core/src/tools/handlers/agent_jobs.rs b/codex-rs/core/src/tools/handlers/agent_jobs.rs index 93380d5e4b..4c3cd34c2e 100644 --- a/codex-rs/core/src/tools/handlers/agent_jobs.rs +++ b/codex-rs/core/src/tools/handlers/agent_jobs.rs @@ -6,20 +6,14 @@ use crate::config::Config; use crate::function_tool::FunctionCallError; use crate::session::session::Session; use crate::session::turn_context::TurnContext; -use crate::tools::context::FunctionToolOutput; -use crate::tools::context::ToolInvocation; -use crate::tools::context::ToolPayload; use crate::tools::handlers::multi_agents::build_agent_spawn_config; use crate::tools::handlers::parse_arguments; -use crate::tools::registry::ToolHandler; -use crate::tools::registry::ToolKind; use codex_protocol::ThreadId; use codex_protocol::error::CodexErr; use codex_protocol::protocol::AgentStatus; use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::SubAgentSource; use codex_protocol::user_input::UserInput; -use codex_tools::ToolName; use codex_utils_absolute_path::AbsolutePathBuf; use futures::StreamExt; use futures::stream::FuturesUnordered; @@ -36,8 +30,11 @@ use tokio::time::Instant; use tokio::time::timeout; use uuid::Uuid; -pub struct SpawnAgentsOnCsvHandler; -pub struct ReportAgentJobResultHandler; +mod report_agent_job_result; +mod spawn_agents_on_csv; + +pub use report_agent_job_result::ReportAgentJobResultHandler; +pub use spawn_agents_on_csv::SpawnAgentsOnCsvHandler; const DEFAULT_AGENT_JOB_CONCURRENCY: usize = 16; const MAX_AGENT_JOB_CONCURRENCY: usize = 64; @@ -101,364 +98,6 @@ struct ActiveJobItem { status_rx: Option>, } -impl ToolHandler for SpawnAgentsOnCsvHandler { - type Output = FunctionToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain("spawn_agents_on_csv") - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - fn matches_kind(&self, payload: &ToolPayload) -> bool { - matches!(payload, ToolPayload::Function { .. }) - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - payload, - .. - } = invocation; - - let arguments = match payload { - ToolPayload::Function { arguments } => arguments, - _ => { - return Err(FunctionCallError::RespondToModel( - "agent jobs handler received unsupported payload".to_string(), - )); - } - }; - - spawn_agents_on_csv::handle(session, turn, arguments).await - } -} - -impl ToolHandler for ReportAgentJobResultHandler { - type Output = FunctionToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain("report_agent_job_result") - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - fn matches_kind(&self, payload: &ToolPayload) -> bool { - matches!(payload, ToolPayload::Function { .. }) - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, payload, .. - } = invocation; - - let arguments = match payload { - ToolPayload::Function { arguments } => arguments, - _ => { - return Err(FunctionCallError::RespondToModel( - "report_agent_job_result handler received unsupported payload".to_string(), - )); - } - }; - - report_agent_job_result::handle(session, arguments).await - } -} - -mod spawn_agents_on_csv { - use super::*; - - /// Create a new agent job from a CSV and run it to completion. - /// - /// Each CSV row becomes a job item. The instruction string is a template where `{column}` - /// placeholders are filled with values from that row. Results are reported by workers via - /// `report_agent_job_result`, then exported to CSV on completion. - pub async fn handle( - session: Arc, - turn: Arc, - arguments: String, - ) -> Result { - let args: SpawnAgentsOnCsvArgs = parse_arguments(arguments.as_str())?; - if args.instruction.trim().is_empty() { - return Err(FunctionCallError::RespondToModel( - "instruction must be non-empty".to_string(), - )); - } - - let db = required_state_db(&session)?; - let input_path = turn.resolve_path(Some(args.csv_path)); - let input_path_display = input_path.display().to_string(); - let csv_content = tokio::fs::read_to_string(&input_path) - .await - .map_err(|err| { - FunctionCallError::RespondToModel(format!( - "failed to read csv input {input_path_display}: {err}" - )) - })?; - let (headers, rows) = parse_csv(csv_content.as_str()).map_err(|err| { - FunctionCallError::RespondToModel(format!("failed to parse csv input: {err}")) - })?; - if headers.is_empty() { - return Err(FunctionCallError::RespondToModel( - "csv input must include a header row".to_string(), - )); - } - ensure_unique_headers(headers.as_slice())?; - - let id_column_index = args.id_column.as_ref().map_or(Ok(None), |column_name| { - headers - .iter() - .position(|header| header == column_name) - .map(Some) - .ok_or_else(|| { - FunctionCallError::RespondToModel(format!( - "id_column {column_name} was not found in csv headers" - )) - }) - })?; - - let mut items = Vec::with_capacity(rows.len()); - let mut seen_ids = HashSet::new(); - for (idx, row) in rows.into_iter().enumerate() { - if row.len() != headers.len() { - let row_index = idx + 2; - let row_len = row.len(); - let header_len = headers.len(); - return Err(FunctionCallError::RespondToModel(format!( - "csv row {row_index} has {row_len} fields but header has {header_len}" - ))); - } - - let source_id = id_column_index - .and_then(|index| row.get(index).cloned()) - .filter(|value| !value.trim().is_empty()); - let row_index = idx + 1; - let base_item_id = source_id - .clone() - .unwrap_or_else(|| format!("row-{row_index}")); - let mut item_id = base_item_id.clone(); - let mut suffix = 2usize; - while !seen_ids.insert(item_id.clone()) { - item_id = format!("{base_item_id}-{suffix}"); - suffix = suffix.saturating_add(1); - } - - let row_object = headers - .iter() - .zip(row.iter()) - .map(|(header, value)| (header.clone(), Value::String(value.clone()))) - .collect::>(); - items.push(codex_state::AgentJobItemCreateParams { - item_id, - row_index: idx as i64, - source_id, - row_json: Value::Object(row_object), - }); - } - - let job_id = Uuid::new_v4().to_string(); - let output_csv_path = args.output_csv_path.map_or_else( - || default_output_csv_path(&input_path, job_id.as_str()), - |path| turn.resolve_path(Some(path)), - ); - let job_suffix = &job_id[..8]; - let job_name = format!("agent-job-{job_suffix}"); - let max_runtime_seconds = normalize_max_runtime_seconds( - args.max_runtime_seconds - .or(turn.config.agent_job_max_runtime_seconds), - )?; - let _job = db - .create_agent_job( - &codex_state::AgentJobCreateParams { - id: job_id.clone(), - name: job_name, - instruction: args.instruction, - auto_export: true, - max_runtime_seconds, - output_schema_json: args.output_schema, - input_headers: headers, - input_csv_path: input_path.display().to_string(), - output_csv_path: output_csv_path.display().to_string(), - }, - items.as_slice(), - ) - .await - .map_err(|err| { - FunctionCallError::RespondToModel(format!("failed to create agent job: {err}")) - })?; - - let requested_concurrency = args.max_concurrency.or(args.max_workers); - let options = match build_runner_options(&session, &turn, requested_concurrency).await { - Ok(options) => options, - Err(err) => { - let error_message = err.to_string(); - let _ = db - .mark_agent_job_failed(job_id.as_str(), error_message.as_str()) - .await; - return Err(err); - } - }; - db.mark_agent_job_running(job_id.as_str()) - .await - .map_err(|err| { - FunctionCallError::RespondToModel(format!( - "failed to transition agent job {job_id} to running: {err}" - )) - })?; - if let Err(err) = run_agent_job_loop( - session.clone(), - turn.clone(), - db.clone(), - job_id.clone(), - options, - ) - .await - { - let error_message = format!("job runner failed: {err}"); - let _ = db - .mark_agent_job_failed(job_id.as_str(), error_message.as_str()) - .await; - return Err(FunctionCallError::RespondToModel(format!( - "agent job {job_id} failed: {err}" - ))); - } - - let job = db - .get_agent_job(job_id.as_str()) - .await - .map_err(|err| { - FunctionCallError::RespondToModel(format!( - "failed to load agent job {job_id}: {err}" - )) - })? - .ok_or_else(|| { - FunctionCallError::RespondToModel(format!("agent job {job_id} not found")) - })?; - let output_path = PathBuf::from(job.output_csv_path.clone()); - if !tokio::fs::try_exists(&output_path).await.unwrap_or(false) { - export_job_csv_snapshot(db.clone(), &job) - .await - .map_err(|err| { - FunctionCallError::RespondToModel(format!( - "failed to export output csv {job_id}: {err}" - )) - })?; - } - let progress = db - .get_agent_job_progress(job_id.as_str()) - .await - .map_err(|err| { - FunctionCallError::RespondToModel(format!( - "failed to load agent job progress {job_id}: {err}" - )) - })?; - let mut job_error = job.last_error.clone().filter(|err| !err.trim().is_empty()); - let failed_item_errors = if progress.failed_items > 0 { - let items = db - .list_agent_job_items( - job_id.as_str(), - Some(codex_state::AgentJobItemStatus::Failed), - Some(5), - ) - .await - .unwrap_or_default(); - let summaries: Vec<_> = items - .into_iter() - .filter_map(|item| { - let last_error = item.last_error.unwrap_or_default(); - if last_error.trim().is_empty() { - return None; - } - Some(AgentJobFailureSummary { - item_id: item.item_id, - source_id: item.source_id, - last_error, - }) - }) - .collect(); - if summaries.is_empty() { - if job_error.is_none() { - job_error = Some( - "agent job has failed items but no error details were recorded".to_string(), - ); - } - None - } else { - Some(summaries) - } - } else { - None - }; - let content = serde_json::to_string(&SpawnAgentsOnCsvResult { - job_id, - status: job.status.as_str().to_string(), - output_csv_path: job.output_csv_path, - total_items: progress.total_items, - completed_items: progress.completed_items, - failed_items: progress.failed_items, - job_error, - failed_item_errors, - }) - .map_err(|err| { - FunctionCallError::Fatal(format!( - "failed to serialize spawn_agents_on_csv result: {err}" - )) - })?; - Ok(FunctionToolOutput::from_text(content, Some(true))) - } -} - -mod report_agent_job_result { - use super::*; - - pub async fn handle( - session: Arc, - arguments: String, - ) -> Result { - let args: ReportAgentJobResultArgs = parse_arguments(arguments.as_str())?; - if !args.result.is_object() { - return Err(FunctionCallError::RespondToModel( - "result must be a JSON object".to_string(), - )); - } - let db = required_state_db(&session)?; - let reporting_thread_id = session.conversation_id.to_string(); - let accepted = db - .report_agent_job_item_result( - args.job_id.as_str(), - args.item_id.as_str(), - reporting_thread_id.as_str(), - &args.result, - ) - .await - .map_err(|err| { - let job_id = args.job_id.as_str(); - let item_id = args.item_id.as_str(); - FunctionCallError::RespondToModel(format!( - "failed to record agent job result for {job_id} / {item_id}: {err}" - )) - })?; - if accepted && args.stop.unwrap_or(false) { - let message = "cancelled by worker request"; - let _ = db - .mark_agent_job_cancelled(args.job_id.as_str(), message) - .await; - } - let content = - serde_json::to_string(&ReportAgentJobResultToolResult { accepted }).map_err(|err| { - FunctionCallError::Fatal(format!( - "failed to serialize report_agent_job_result result: {err}" - )) - })?; - Ok(FunctionToolOutput::from_text(content, Some(true))) - } -} - fn required_state_db( session: &Arc, ) -> Result, FunctionCallError> { diff --git a/codex-rs/core/src/tools/handlers/agent_jobs/report_agent_job_result.rs b/codex-rs/core/src/tools/handlers/agent_jobs/report_agent_job_result.rs new file mode 100644 index 0000000000..90cde7d446 --- /dev/null +++ b/codex-rs/core/src/tools/handlers/agent_jobs/report_agent_job_result.rs @@ -0,0 +1,86 @@ +use crate::function_tool::FunctionCallError; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use codex_tools::ToolName; + +use super::*; + +pub struct ReportAgentJobResultHandler; + +impl ToolHandler for ReportAgentJobResultHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain("report_agent_job_result") + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + fn matches_kind(&self, payload: &ToolPayload) -> bool { + matches!(payload, ToolPayload::Function { .. }) + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, payload, .. + } = invocation; + + let arguments = match payload { + ToolPayload::Function { arguments } => arguments, + _ => { + return Err(FunctionCallError::RespondToModel( + "report_agent_job_result handler received unsupported payload".to_string(), + )); + } + }; + + handle(session, arguments).await + } +} + +pub async fn handle( + session: Arc, + arguments: String, +) -> Result { + let args: ReportAgentJobResultArgs = parse_arguments(arguments.as_str())?; + if !args.result.is_object() { + return Err(FunctionCallError::RespondToModel( + "result must be a JSON object".to_string(), + )); + } + let db = required_state_db(&session)?; + let reporting_thread_id = session.conversation_id.to_string(); + let accepted = db + .report_agent_job_item_result( + args.job_id.as_str(), + args.item_id.as_str(), + reporting_thread_id.as_str(), + &args.result, + ) + .await + .map_err(|err| { + let job_id = args.job_id.as_str(); + let item_id = args.item_id.as_str(); + FunctionCallError::RespondToModel(format!( + "failed to record agent job result for {job_id} / {item_id}: {err}" + )) + })?; + if accepted && args.stop.unwrap_or(false) { + let message = "cancelled by worker request"; + let _ = db + .mark_agent_job_cancelled(args.job_id.as_str(), message) + .await; + } + let content = + serde_json::to_string(&ReportAgentJobResultToolResult { accepted }).map_err(|err| { + FunctionCallError::Fatal(format!( + "failed to serialize report_agent_job_result result: {err}" + )) + })?; + Ok(FunctionToolOutput::from_text(content, Some(true))) +} diff --git a/codex-rs/core/src/tools/handlers/agent_jobs/spawn_agents_on_csv.rs b/codex-rs/core/src/tools/handlers/agent_jobs/spawn_agents_on_csv.rs new file mode 100644 index 0000000000..911f1a5eef --- /dev/null +++ b/codex-rs/core/src/tools/handlers/agent_jobs/spawn_agents_on_csv.rs @@ -0,0 +1,284 @@ +use crate::function_tool::FunctionCallError; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use codex_tools::ToolName; + +use super::*; + +pub struct SpawnAgentsOnCsvHandler; + +impl ToolHandler for SpawnAgentsOnCsvHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain("spawn_agents_on_csv") + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + fn matches_kind(&self, payload: &ToolPayload) -> bool { + matches!(payload, ToolPayload::Function { .. }) + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + payload, + .. + } = invocation; + + let arguments = match payload { + ToolPayload::Function { arguments } => arguments, + _ => { + return Err(FunctionCallError::RespondToModel( + "agent jobs handler received unsupported payload".to_string(), + )); + } + }; + + handle(session, turn, arguments).await + } +} + +/// Create a new agent job from a CSV and run it to completion. +/// +/// Each CSV row becomes a job item. The instruction string is a template where `{column}` +/// placeholders are filled with values from that row. Results are reported by workers via +/// `report_agent_job_result`, then exported to CSV on completion. +pub async fn handle( + session: Arc, + turn: Arc, + arguments: String, +) -> Result { + let args: SpawnAgentsOnCsvArgs = parse_arguments(arguments.as_str())?; + if args.instruction.trim().is_empty() { + return Err(FunctionCallError::RespondToModel( + "instruction must be non-empty".to_string(), + )); + } + + let db = required_state_db(&session)?; + let input_path = turn.resolve_path(Some(args.csv_path)); + let input_path_display = input_path.display().to_string(); + let csv_content = tokio::fs::read_to_string(&input_path) + .await + .map_err(|err| { + FunctionCallError::RespondToModel(format!( + "failed to read csv input {input_path_display}: {err}" + )) + })?; + let (headers, rows) = parse_csv(csv_content.as_str()).map_err(|err| { + FunctionCallError::RespondToModel(format!("failed to parse csv input: {err}")) + })?; + if headers.is_empty() { + return Err(FunctionCallError::RespondToModel( + "csv input must include a header row".to_string(), + )); + } + ensure_unique_headers(headers.as_slice())?; + + let id_column_index = args.id_column.as_ref().map_or(Ok(None), |column_name| { + headers + .iter() + .position(|header| header == column_name) + .map(Some) + .ok_or_else(|| { + FunctionCallError::RespondToModel(format!( + "id_column {column_name} was not found in csv headers" + )) + }) + })?; + + let mut items = Vec::with_capacity(rows.len()); + let mut seen_ids = HashSet::new(); + for (idx, row) in rows.into_iter().enumerate() { + if row.len() != headers.len() { + let row_index = idx + 2; + let row_len = row.len(); + let header_len = headers.len(); + return Err(FunctionCallError::RespondToModel(format!( + "csv row {row_index} has {row_len} fields but header has {header_len}" + ))); + } + + let source_id = id_column_index + .and_then(|index| row.get(index).cloned()) + .filter(|value| !value.trim().is_empty()); + let row_index = idx + 1; + let base_item_id = source_id + .clone() + .unwrap_or_else(|| format!("row-{row_index}")); + let mut item_id = base_item_id.clone(); + let mut suffix = 2usize; + while !seen_ids.insert(item_id.clone()) { + item_id = format!("{base_item_id}-{suffix}"); + suffix = suffix.saturating_add(1); + } + + let row_object = headers + .iter() + .zip(row.iter()) + .map(|(header, value)| (header.clone(), Value::String(value.clone()))) + .collect::>(); + items.push(codex_state::AgentJobItemCreateParams { + item_id, + row_index: idx as i64, + source_id, + row_json: Value::Object(row_object), + }); + } + + let job_id = Uuid::new_v4().to_string(); + let output_csv_path = args.output_csv_path.map_or_else( + || default_output_csv_path(&input_path, job_id.as_str()), + |path| turn.resolve_path(Some(path)), + ); + let job_suffix = &job_id[..8]; + let job_name = format!("agent-job-{job_suffix}"); + let max_runtime_seconds = normalize_max_runtime_seconds( + args.max_runtime_seconds + .or(turn.config.agent_job_max_runtime_seconds), + )?; + let _job = db + .create_agent_job( + &codex_state::AgentJobCreateParams { + id: job_id.clone(), + name: job_name, + instruction: args.instruction, + auto_export: true, + max_runtime_seconds, + output_schema_json: args.output_schema, + input_headers: headers, + input_csv_path: input_path.display().to_string(), + output_csv_path: output_csv_path.display().to_string(), + }, + items.as_slice(), + ) + .await + .map_err(|err| { + FunctionCallError::RespondToModel(format!("failed to create agent job: {err}")) + })?; + + let requested_concurrency = args.max_concurrency.or(args.max_workers); + let options = match build_runner_options(&session, &turn, requested_concurrency).await { + Ok(options) => options, + Err(err) => { + let error_message = err.to_string(); + let _ = db + .mark_agent_job_failed(job_id.as_str(), error_message.as_str()) + .await; + return Err(err); + } + }; + db.mark_agent_job_running(job_id.as_str()) + .await + .map_err(|err| { + FunctionCallError::RespondToModel(format!( + "failed to transition agent job {job_id} to running: {err}" + )) + })?; + if let Err(err) = run_agent_job_loop( + session.clone(), + turn.clone(), + db.clone(), + job_id.clone(), + options, + ) + .await + { + let error_message = format!("job runner failed: {err}"); + let _ = db + .mark_agent_job_failed(job_id.as_str(), error_message.as_str()) + .await; + return Err(FunctionCallError::RespondToModel(format!( + "agent job {job_id} failed: {err}" + ))); + } + + let job = db + .get_agent_job(job_id.as_str()) + .await + .map_err(|err| { + FunctionCallError::RespondToModel(format!("failed to load agent job {job_id}: {err}")) + })? + .ok_or_else(|| { + FunctionCallError::RespondToModel(format!("agent job {job_id} not found")) + })?; + let output_path = PathBuf::from(job.output_csv_path.clone()); + if !tokio::fs::try_exists(&output_path).await.unwrap_or(false) { + export_job_csv_snapshot(db.clone(), &job) + .await + .map_err(|err| { + FunctionCallError::RespondToModel(format!( + "failed to export output csv {job_id}: {err}" + )) + })?; + } + let progress = db + .get_agent_job_progress(job_id.as_str()) + .await + .map_err(|err| { + FunctionCallError::RespondToModel(format!( + "failed to load agent job progress {job_id}: {err}" + )) + })?; + let mut job_error = job.last_error.clone().filter(|err| !err.trim().is_empty()); + let failed_item_errors = if progress.failed_items > 0 { + let items = db + .list_agent_job_items( + job_id.as_str(), + Some(codex_state::AgentJobItemStatus::Failed), + Some(5), + ) + .await + .unwrap_or_default(); + let summaries: Vec<_> = items + .into_iter() + .filter_map(|item| { + let last_error = item.last_error.unwrap_or_default(); + if last_error.trim().is_empty() { + return None; + } + Some(AgentJobFailureSummary { + item_id: item.item_id, + source_id: item.source_id, + last_error, + }) + }) + .collect(); + if summaries.is_empty() { + if job_error.is_none() { + job_error = Some( + "agent job has failed items but no error details were recorded".to_string(), + ); + } + None + } else { + Some(summaries) + } + } else { + None + }; + let content = serde_json::to_string(&SpawnAgentsOnCsvResult { + job_id, + status: job.status.as_str().to_string(), + output_csv_path: job.output_csv_path, + total_items: progress.total_items, + completed_items: progress.completed_items, + failed_items: progress.failed_items, + job_error, + failed_item_errors, + }) + .map_err(|err| { + FunctionCallError::Fatal(format!( + "failed to serialize spawn_agents_on_csv result: {err}" + )) + })?; + Ok(FunctionToolOutput::from_text(content, Some(true))) +} diff --git a/codex-rs/core/src/tools/handlers/goal.rs b/codex-rs/core/src/tools/handlers/goal.rs index 6a7b304ce4..28e33f2be4 100644 --- a/codex-rs/core/src/tools/handlers/goal.rs +++ b/codex-rs/core/src/tools/handlers/goal.rs @@ -5,28 +5,20 @@ //! the existing goal complete. use crate::function_tool::FunctionCallError; -use crate::goals::CreateGoalRequest; -use crate::goals::GoalRuntimeEvent; -use crate::goals::SetGoalRequest; use crate::tools::context::FunctionToolOutput; -use crate::tools::context::ToolInvocation; -use crate::tools::context::ToolPayload; -use crate::tools::handlers::parse_arguments; -use crate::tools::registry::ToolHandler; -use crate::tools::registry::ToolKind; use codex_protocol::protocol::ThreadGoal; use codex_protocol::protocol::ThreadGoalStatus; -use codex_tools::CREATE_GOAL_TOOL_NAME; -use codex_tools::GET_GOAL_TOOL_NAME; -use codex_tools::ToolName; -use codex_tools::UPDATE_GOAL_TOOL_NAME; use serde::Deserialize; use serde::Serialize; use std::fmt::Write as _; -pub struct GetGoalHandler; -pub struct CreateGoalHandler; -pub struct UpdateGoalHandler; +mod create_goal; +mod get_goal; +mod update_goal; + +pub use create_goal::CreateGoalHandler; +pub use get_goal::GetGoalHandler; +pub use update_goal::UpdateGoalHandler; #[derive(Debug, Deserialize)] #[serde(rename_all = "snake_case")] @@ -76,148 +68,6 @@ impl GoalToolResponse { } } -impl ToolHandler for GetGoalHandler { - type Output = FunctionToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain(GET_GOAL_TOOL_NAME) - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, payload, .. - } = invocation; - - match payload { - ToolPayload::Function { .. } => { - let goal = session - .get_thread_goal() - .await - .map_err(|err| FunctionCallError::RespondToModel(format_goal_error(err)))?; - goal_response(goal, CompletionBudgetReport::Omit) - } - _ => Err(FunctionCallError::RespondToModel( - "get_goal handler received unsupported payload".to_string(), - )), - } - } -} - -impl ToolHandler for CreateGoalHandler { - type Output = FunctionToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain(CREATE_GOAL_TOOL_NAME) - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - payload, - .. - } = invocation; - - let arguments = match payload { - ToolPayload::Function { arguments } => arguments, - _ => { - return Err(FunctionCallError::RespondToModel( - "goal handler received unsupported payload".to_string(), - )); - } - }; - - let args: CreateGoalArgs = parse_arguments(&arguments)?; - let goal = session - .create_thread_goal( - turn.as_ref(), - CreateGoalRequest { - objective: args.objective, - token_budget: args.token_budget, - }, - ) - .await - .map_err(|err| { - if err - .chain() - .any(|cause| cause.to_string().contains("already has a goal")) - { - FunctionCallError::RespondToModel( - "cannot create a new goal because this thread already has a goal; use update_goal only when the existing goal is complete" - .to_string(), - ) - } else { - FunctionCallError::RespondToModel(format_goal_error(err)) - } - })?; - goal_response(Some(goal), CompletionBudgetReport::Omit) - } -} - -impl ToolHandler for UpdateGoalHandler { - type Output = FunctionToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain(UPDATE_GOAL_TOOL_NAME) - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - payload, - .. - } = invocation; - - let arguments = match payload { - ToolPayload::Function { arguments } => arguments, - _ => { - return Err(FunctionCallError::RespondToModel( - "update_goal handler received unsupported payload".to_string(), - )); - } - }; - - let args: UpdateGoalArgs = parse_arguments(&arguments)?; - if args.status != ThreadGoalStatus::Complete { - return Err(FunctionCallError::RespondToModel( - "update_goal can only mark the existing goal complete; pause, resume, and budget-limited status changes are controlled by the user or system" - .to_string(), - )); - } - session - .goal_runtime_apply(GoalRuntimeEvent::ToolCompletedGoal { - turn_context: turn.as_ref(), - }) - .await - .map_err(|err| FunctionCallError::RespondToModel(format_goal_error(err)))?; - let goal = session - .set_thread_goal( - turn.as_ref(), - SetGoalRequest { - objective: None, - status: Some(ThreadGoalStatus::Complete), - token_budget: None, - }, - ) - .await - .map_err(|err| FunctionCallError::RespondToModel(format_goal_error(err)))?; - goal_response(Some(goal), CompletionBudgetReport::Include) - } -} - fn format_goal_error(err: anyhow::Error) -> String { let mut message = err.to_string(); for cause in err.chain().skip(1) { diff --git a/codex-rs/core/src/tools/handlers/goal/create_goal.rs b/codex-rs/core/src/tools/handlers/goal/create_goal.rs new file mode 100644 index 0000000000..88297cc1af --- /dev/null +++ b/codex-rs/core/src/tools/handlers/goal/create_goal.rs @@ -0,0 +1,72 @@ +use crate::function_tool::FunctionCallError; +use crate::goals::CreateGoalRequest; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::handlers::parse_arguments; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use codex_tools::CREATE_GOAL_TOOL_NAME; +use codex_tools::ToolName; + +use super::CompletionBudgetReport; +use super::CreateGoalArgs; +use super::format_goal_error; +use super::goal_response; + +pub struct CreateGoalHandler; + +impl ToolHandler for CreateGoalHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain(CREATE_GOAL_TOOL_NAME) + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + payload, + .. + } = invocation; + + let arguments = match payload { + ToolPayload::Function { arguments } => arguments, + _ => { + return Err(FunctionCallError::RespondToModel( + "goal handler received unsupported payload".to_string(), + )); + } + }; + + let args: CreateGoalArgs = parse_arguments(&arguments)?; + let goal = session + .create_thread_goal( + turn.as_ref(), + CreateGoalRequest { + objective: args.objective, + token_budget: args.token_budget, + }, + ) + .await + .map_err(|err| { + if err + .chain() + .any(|cause| cause.to_string().contains("already has a goal")) + { + FunctionCallError::RespondToModel( + "cannot create a new goal because this thread already has a goal; use update_goal only when the existing goal is complete" + .to_string(), + ) + } else { + FunctionCallError::RespondToModel(format_goal_error(err)) + } + })?; + goal_response(Some(goal), CompletionBudgetReport::Omit) + } +} diff --git a/codex-rs/core/src/tools/handlers/goal/get_goal.rs b/codex-rs/core/src/tools/handlers/goal/get_goal.rs new file mode 100644 index 0000000000..ab023f3014 --- /dev/null +++ b/codex-rs/core/src/tools/handlers/goal/get_goal.rs @@ -0,0 +1,45 @@ +use crate::function_tool::FunctionCallError; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use codex_tools::GET_GOAL_TOOL_NAME; +use codex_tools::ToolName; + +use super::CompletionBudgetReport; +use super::format_goal_error; +use super::goal_response; + +pub struct GetGoalHandler; + +impl ToolHandler for GetGoalHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain(GET_GOAL_TOOL_NAME) + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, payload, .. + } = invocation; + + match payload { + ToolPayload::Function { .. } => { + let goal = session + .get_thread_goal() + .await + .map_err(|err| FunctionCallError::RespondToModel(format_goal_error(err)))?; + goal_response(goal, CompletionBudgetReport::Omit) + } + _ => Err(FunctionCallError::RespondToModel( + "get_goal handler received unsupported payload".to_string(), + )), + } + } +} diff --git a/codex-rs/core/src/tools/handlers/goal/update_goal.rs b/codex-rs/core/src/tools/handlers/goal/update_goal.rs new file mode 100644 index 0000000000..6c43484ec9 --- /dev/null +++ b/codex-rs/core/src/tools/handlers/goal/update_goal.rs @@ -0,0 +1,75 @@ +use crate::function_tool::FunctionCallError; +use crate::goals::GoalRuntimeEvent; +use crate::goals::SetGoalRequest; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::handlers::parse_arguments; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use codex_protocol::protocol::ThreadGoalStatus; +use codex_tools::ToolName; +use codex_tools::UPDATE_GOAL_TOOL_NAME; + +use super::CompletionBudgetReport; +use super::UpdateGoalArgs; +use super::format_goal_error; +use super::goal_response; + +pub struct UpdateGoalHandler; + +impl ToolHandler for UpdateGoalHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain(UPDATE_GOAL_TOOL_NAME) + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + payload, + .. + } = invocation; + + let arguments = match payload { + ToolPayload::Function { arguments } => arguments, + _ => { + return Err(FunctionCallError::RespondToModel( + "update_goal handler received unsupported payload".to_string(), + )); + } + }; + + let args: UpdateGoalArgs = parse_arguments(&arguments)?; + if args.status != ThreadGoalStatus::Complete { + return Err(FunctionCallError::RespondToModel( + "update_goal can only mark the existing goal complete; pause, resume, and budget-limited status changes are controlled by the user or system" + .to_string(), + )); + } + session + .goal_runtime_apply(GoalRuntimeEvent::ToolCompletedGoal { + turn_context: turn.as_ref(), + }) + .await + .map_err(|err| FunctionCallError::RespondToModel(format_goal_error(err)))?; + let goal = session + .set_thread_goal( + turn.as_ref(), + SetGoalRequest { + objective: None, + status: Some(ThreadGoalStatus::Complete), + token_budget: None, + }, + ) + .await + .map_err(|err| FunctionCallError::RespondToModel(format_goal_error(err)))?; + goal_response(Some(goal), CompletionBudgetReport::Include) + } +} diff --git a/codex-rs/core/src/tools/handlers/mcp_resource.rs b/codex-rs/core/src/tools/handlers/mcp_resource.rs index b03de53ae1..630a1cccb4 100644 --- a/codex-rs/core/src/tools/handlers/mcp_resource.rs +++ b/codex-rs/core/src/tools/handlers/mcp_resource.rs @@ -1,18 +1,14 @@ use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; -use std::time::Instant; use codex_protocol::items::McpToolCallError; use codex_protocol::items::McpToolCallItem; use codex_protocol::items::McpToolCallStatus; use codex_protocol::items::TurnItem; use codex_protocol::mcp::CallToolResult; -use codex_protocol::models::function_call_output_content_items_to_text; use rmcp::model::ListResourceTemplatesResult; use rmcp::model::ListResourcesResult; -use rmcp::model::PaginatedRequestParams; -use rmcp::model::ReadResourceRequestParams; use rmcp::model::ReadResourceResult; use rmcp::model::Resource; use rmcp::model::ResourceTemplate; @@ -25,16 +21,15 @@ use crate::function_tool::FunctionCallError; use crate::session::session::Session; use crate::session::turn_context::TurnContext; use crate::tools::context::FunctionToolOutput; -use crate::tools::context::ToolInvocation; -use crate::tools::context::ToolPayload; -use crate::tools::registry::ToolHandler; -use crate::tools::registry::ToolKind; use codex_protocol::protocol::McpInvocation; -use codex_tools::ToolName; -pub struct ListMcpResourcesHandler; -pub struct ListMcpResourceTemplatesHandler; -pub struct ReadMcpResourceHandler; +mod list_mcp_resource_templates; +mod list_mcp_resources; +mod read_mcp_resource; + +pub use list_mcp_resource_templates::ListMcpResourceTemplatesHandler; +pub use list_mcp_resources::ListMcpResourcesHandler; +pub use read_mcp_resource::ReadMcpResourceHandler; #[derive(Debug, Deserialize, Default)] struct ListResourcesArgs { @@ -181,390 +176,6 @@ struct ReadResourcePayload { result: ReadResourceResult, } -impl ToolHandler for ListMcpResourcesHandler { - type Output = FunctionToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain("list_mcp_resources") - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - #[expect( - clippy::await_holding_invalid_type, - reason = "MCP resource listing reads through the session-owned manager guard" - )] - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - call_id, - payload, - .. - } = invocation; - - let arguments = match payload { - ToolPayload::Function { arguments } => arguments, - _ => { - return Err(FunctionCallError::RespondToModel( - "list_mcp_resources handler received unsupported payload".to_string(), - )); - } - }; - - let arguments = parse_arguments(arguments.as_str())?; - let args: ListResourcesArgs = parse_args_with_default(arguments.clone())?; - let ListResourcesArgs { server, cursor } = args; - let server = normalize_optional_string(server); - let cursor = normalize_optional_string(cursor); - - let invocation = McpInvocation { - server: server.clone().unwrap_or_else(|| "codex".to_string()), - tool: "list_mcp_resources".to_string(), - arguments: arguments.clone(), - }; - - emit_tool_call_begin(&session, turn.as_ref(), &call_id, invocation.clone()).await; - let start = Instant::now(); - - let payload_result: Result = async { - if let Some(server_name) = server.clone() { - let params = cursor.clone().map(|value| PaginatedRequestParams { - meta: None, - cursor: Some(value), - }); - let result = session - .list_resources(&server_name, params) - .await - .map_err(|err| { - FunctionCallError::RespondToModel(format!("resources/list failed: {err:#}")) - })?; - Ok(ListResourcesPayload::from_single_server( - server_name, - result, - )) - } else { - if cursor.is_some() { - return Err(FunctionCallError::RespondToModel( - "cursor can only be used when a server is specified".to_string(), - )); - } - - let resources = session - .services - .mcp_connection_manager - .read() - .await - .list_all_resources() - .await; - Ok(ListResourcesPayload::from_all_servers(resources)) - } - } - .await; - - match payload_result { - Ok(payload) => match serialize_function_output(payload) { - Ok(output) => { - let content = function_call_output_content_items_to_text(&output.body) - .unwrap_or_default(); - let duration = start.elapsed(); - emit_tool_call_end( - &session, - turn.as_ref(), - &call_id, - invocation, - duration, - Ok(call_tool_result_from_content(&content, output.success)), - ) - .await; - Ok(output) - } - Err(err) => { - let duration = start.elapsed(); - let message = err.to_string(); - emit_tool_call_end( - &session, - turn.as_ref(), - &call_id, - invocation, - duration, - Err(message.clone()), - ) - .await; - Err(err) - } - }, - Err(err) => { - let duration = start.elapsed(); - let message = err.to_string(); - emit_tool_call_end( - &session, - turn.as_ref(), - &call_id, - invocation, - duration, - Err(message.clone()), - ) - .await; - Err(err) - } - } - } -} - -impl ToolHandler for ListMcpResourceTemplatesHandler { - type Output = FunctionToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain("list_mcp_resource_templates") - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - #[expect( - clippy::await_holding_invalid_type, - reason = "MCP resource template listing reads through the session-owned manager guard" - )] - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - call_id, - payload, - .. - } = invocation; - - let arguments = match payload { - ToolPayload::Function { arguments } => arguments, - _ => { - return Err(FunctionCallError::RespondToModel( - "list_mcp_resource_templates handler received unsupported payload".to_string(), - )); - } - }; - - let arguments = parse_arguments(arguments.as_str())?; - let args: ListResourceTemplatesArgs = parse_args_with_default(arguments.clone())?; - let ListResourceTemplatesArgs { server, cursor } = args; - let server = normalize_optional_string(server); - let cursor = normalize_optional_string(cursor); - - let invocation = McpInvocation { - server: server.clone().unwrap_or_else(|| "codex".to_string()), - tool: "list_mcp_resource_templates".to_string(), - arguments: arguments.clone(), - }; - - emit_tool_call_begin(&session, turn.as_ref(), &call_id, invocation.clone()).await; - let start = Instant::now(); - - let payload_result: Result = async { - if let Some(server_name) = server.clone() { - let params = cursor.clone().map(|value| PaginatedRequestParams { - meta: None, - cursor: Some(value), - }); - let result = session - .list_resource_templates(&server_name, params) - .await - .map_err(|err| { - FunctionCallError::RespondToModel(format!( - "resources/templates/list failed: {err:#}" - )) - })?; - Ok(ListResourceTemplatesPayload::from_single_server( - server_name, - result, - )) - } else { - if cursor.is_some() { - return Err(FunctionCallError::RespondToModel( - "cursor can only be used when a server is specified".to_string(), - )); - } - - let templates = session - .services - .mcp_connection_manager - .read() - .await - .list_all_resource_templates() - .await; - Ok(ListResourceTemplatesPayload::from_all_servers(templates)) - } - } - .await; - - match payload_result { - Ok(payload) => match serialize_function_output(payload) { - Ok(output) => { - let content = function_call_output_content_items_to_text(&output.body) - .unwrap_or_default(); - let duration = start.elapsed(); - emit_tool_call_end( - &session, - turn.as_ref(), - &call_id, - invocation, - duration, - Ok(call_tool_result_from_content(&content, output.success)), - ) - .await; - Ok(output) - } - Err(err) => { - let duration = start.elapsed(); - let message = err.to_string(); - emit_tool_call_end( - &session, - turn.as_ref(), - &call_id, - invocation, - duration, - Err(message.clone()), - ) - .await; - Err(err) - } - }, - Err(err) => { - let duration = start.elapsed(); - let message = err.to_string(); - emit_tool_call_end( - &session, - turn.as_ref(), - &call_id, - invocation, - duration, - Err(message.clone()), - ) - .await; - Err(err) - } - } - } -} - -impl ToolHandler for ReadMcpResourceHandler { - type Output = FunctionToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain("read_mcp_resource") - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - call_id, - payload, - .. - } = invocation; - - let arguments = match payload { - ToolPayload::Function { arguments } => arguments, - _ => { - return Err(FunctionCallError::RespondToModel( - "read_mcp_resource handler received unsupported payload".to_string(), - )); - } - }; - - let arguments = parse_arguments(arguments.as_str())?; - let args: ReadResourceArgs = parse_args(arguments.clone())?; - let ReadResourceArgs { server, uri } = args; - let server = normalize_required_string("server", server)?; - let uri = normalize_required_string("uri", uri)?; - - let invocation = McpInvocation { - server: server.clone(), - tool: "read_mcp_resource".to_string(), - arguments: arguments.clone(), - }; - - emit_tool_call_begin(&session, turn.as_ref(), &call_id, invocation.clone()).await; - let start = Instant::now(); - - let payload_result: Result = async { - let result = session - .read_resource( - &server, - ReadResourceRequestParams { - meta: None, - uri: uri.clone(), - }, - ) - .await - .map_err(|err| { - FunctionCallError::RespondToModel(format!("resources/read failed: {err:#}")) - })?; - - Ok(ReadResourcePayload { - server, - uri, - result, - }) - } - .await; - - match payload_result { - Ok(payload) => match serialize_function_output(payload) { - Ok(output) => { - let content = function_call_output_content_items_to_text(&output.body) - .unwrap_or_default(); - let duration = start.elapsed(); - emit_tool_call_end( - &session, - turn.as_ref(), - &call_id, - invocation, - duration, - Ok(call_tool_result_from_content(&content, output.success)), - ) - .await; - Ok(output) - } - Err(err) => { - let duration = start.elapsed(); - let message = err.to_string(); - emit_tool_call_end( - &session, - turn.as_ref(), - &call_id, - invocation, - duration, - Err(message.clone()), - ) - .await; - Err(err) - } - }, - Err(err) => { - let duration = start.elapsed(); - let message = err.to_string(); - emit_tool_call_end( - &session, - turn.as_ref(), - &call_id, - invocation, - duration, - Err(message.clone()), - ) - .await; - Err(err) - } - } - } -} - fn call_tool_result_from_content(content: &str, success: Option) -> CallToolResult { CallToolResult { content: vec![serde_json::json!({"type": "text", "text": content})], diff --git a/codex-rs/core/src/tools/handlers/mcp_resource/list_mcp_resource_templates.rs b/codex-rs/core/src/tools/handlers/mcp_resource/list_mcp_resource_templates.rs new file mode 100644 index 0000000000..2c87edf0c7 --- /dev/null +++ b/codex-rs/core/src/tools/handlers/mcp_resource/list_mcp_resource_templates.rs @@ -0,0 +1,160 @@ +use std::time::Instant; + +use crate::function_tool::FunctionCallError; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use codex_protocol::models::function_call_output_content_items_to_text; +use codex_protocol::protocol::McpInvocation; +use codex_tools::ToolName; + +use rmcp::model::PaginatedRequestParams; + +use super::ListResourceTemplatesArgs; +use super::ListResourceTemplatesPayload; +use super::call_tool_result_from_content; +use super::emit_tool_call_begin; +use super::emit_tool_call_end; +use super::normalize_optional_string; +use super::parse_args_with_default; +use super::parse_arguments; +use super::serialize_function_output; + +pub struct ListMcpResourceTemplatesHandler; + +impl ToolHandler for ListMcpResourceTemplatesHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain("list_mcp_resource_templates") + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + #[expect( + clippy::await_holding_invalid_type, + reason = "MCP resource template listing reads through the session-owned manager guard" + )] + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + call_id, + payload, + .. + } = invocation; + + let arguments = match payload { + ToolPayload::Function { arguments } => arguments, + _ => { + return Err(FunctionCallError::RespondToModel( + "list_mcp_resource_templates handler received unsupported payload".to_string(), + )); + } + }; + + let arguments = parse_arguments(arguments.as_str())?; + let args: ListResourceTemplatesArgs = parse_args_with_default(arguments.clone())?; + let ListResourceTemplatesArgs { server, cursor } = args; + let server = normalize_optional_string(server); + let cursor = normalize_optional_string(cursor); + + let invocation = McpInvocation { + server: server.clone().unwrap_or_else(|| "codex".to_string()), + tool: "list_mcp_resource_templates".to_string(), + arguments: arguments.clone(), + }; + + emit_tool_call_begin(&session, turn.as_ref(), &call_id, invocation.clone()).await; + let start = Instant::now(); + + let payload_result: Result = async { + if let Some(server_name) = server.clone() { + let params = cursor.clone().map(|value| PaginatedRequestParams { + meta: None, + cursor: Some(value), + }); + let result = session + .list_resource_templates(&server_name, params) + .await + .map_err(|err| { + FunctionCallError::RespondToModel(format!( + "resources/templates/list failed: {err:#}" + )) + })?; + Ok(ListResourceTemplatesPayload::from_single_server( + server_name, + result, + )) + } else { + if cursor.is_some() { + return Err(FunctionCallError::RespondToModel( + "cursor can only be used when a server is specified".to_string(), + )); + } + + let templates = session + .services + .mcp_connection_manager + .read() + .await + .list_all_resource_templates() + .await; + Ok(ListResourceTemplatesPayload::from_all_servers(templates)) + } + } + .await; + + match payload_result { + Ok(payload) => match serialize_function_output(payload) { + Ok(output) => { + let content = function_call_output_content_items_to_text(&output.body) + .unwrap_or_default(); + let duration = start.elapsed(); + emit_tool_call_end( + &session, + turn.as_ref(), + &call_id, + invocation, + duration, + Ok(call_tool_result_from_content(&content, output.success)), + ) + .await; + Ok(output) + } + Err(err) => { + let duration = start.elapsed(); + let message = err.to_string(); + emit_tool_call_end( + &session, + turn.as_ref(), + &call_id, + invocation, + duration, + Err(message.clone()), + ) + .await; + Err(err) + } + }, + Err(err) => { + let duration = start.elapsed(); + let message = err.to_string(); + emit_tool_call_end( + &session, + turn.as_ref(), + &call_id, + invocation, + duration, + Err(message.clone()), + ) + .await; + Err(err) + } + } + } +} diff --git a/codex-rs/core/src/tools/handlers/mcp_resource/list_mcp_resources.rs b/codex-rs/core/src/tools/handlers/mcp_resource/list_mcp_resources.rs new file mode 100644 index 0000000000..ed62852141 --- /dev/null +++ b/codex-rs/core/src/tools/handlers/mcp_resource/list_mcp_resources.rs @@ -0,0 +1,158 @@ +use std::time::Instant; + +use crate::function_tool::FunctionCallError; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use codex_protocol::models::function_call_output_content_items_to_text; +use codex_protocol::protocol::McpInvocation; +use codex_tools::ToolName; + +use rmcp::model::PaginatedRequestParams; + +use super::ListResourcesArgs; +use super::ListResourcesPayload; +use super::call_tool_result_from_content; +use super::emit_tool_call_begin; +use super::emit_tool_call_end; +use super::normalize_optional_string; +use super::parse_args_with_default; +use super::parse_arguments; +use super::serialize_function_output; + +pub struct ListMcpResourcesHandler; + +impl ToolHandler for ListMcpResourcesHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain("list_mcp_resources") + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + #[expect( + clippy::await_holding_invalid_type, + reason = "MCP resource listing reads through the session-owned manager guard" + )] + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + call_id, + payload, + .. + } = invocation; + + let arguments = match payload { + ToolPayload::Function { arguments } => arguments, + _ => { + return Err(FunctionCallError::RespondToModel( + "list_mcp_resources handler received unsupported payload".to_string(), + )); + } + }; + + let arguments = parse_arguments(arguments.as_str())?; + let args: ListResourcesArgs = parse_args_with_default(arguments.clone())?; + let ListResourcesArgs { server, cursor } = args; + let server = normalize_optional_string(server); + let cursor = normalize_optional_string(cursor); + + let invocation = McpInvocation { + server: server.clone().unwrap_or_else(|| "codex".to_string()), + tool: "list_mcp_resources".to_string(), + arguments: arguments.clone(), + }; + + emit_tool_call_begin(&session, turn.as_ref(), &call_id, invocation.clone()).await; + let start = Instant::now(); + + let payload_result: Result = async { + if let Some(server_name) = server.clone() { + let params = cursor.clone().map(|value| PaginatedRequestParams { + meta: None, + cursor: Some(value), + }); + let result = session + .list_resources(&server_name, params) + .await + .map_err(|err| { + FunctionCallError::RespondToModel(format!("resources/list failed: {err:#}")) + })?; + Ok(ListResourcesPayload::from_single_server( + server_name, + result, + )) + } else { + if cursor.is_some() { + return Err(FunctionCallError::RespondToModel( + "cursor can only be used when a server is specified".to_string(), + )); + } + + let resources = session + .services + .mcp_connection_manager + .read() + .await + .list_all_resources() + .await; + Ok(ListResourcesPayload::from_all_servers(resources)) + } + } + .await; + + match payload_result { + Ok(payload) => match serialize_function_output(payload) { + Ok(output) => { + let content = function_call_output_content_items_to_text(&output.body) + .unwrap_or_default(); + let duration = start.elapsed(); + emit_tool_call_end( + &session, + turn.as_ref(), + &call_id, + invocation, + duration, + Ok(call_tool_result_from_content(&content, output.success)), + ) + .await; + Ok(output) + } + Err(err) => { + let duration = start.elapsed(); + let message = err.to_string(); + emit_tool_call_end( + &session, + turn.as_ref(), + &call_id, + invocation, + duration, + Err(message.clone()), + ) + .await; + Err(err) + } + }, + Err(err) => { + let duration = start.elapsed(); + let message = err.to_string(); + emit_tool_call_end( + &session, + turn.as_ref(), + &call_id, + invocation, + duration, + Err(message.clone()), + ) + .await; + Err(err) + } + } + } +} diff --git a/codex-rs/core/src/tools/handlers/mcp_resource/read_mcp_resource.rs b/codex-rs/core/src/tools/handlers/mcp_resource/read_mcp_resource.rs new file mode 100644 index 0000000000..91d5a4317e --- /dev/null +++ b/codex-rs/core/src/tools/handlers/mcp_resource/read_mcp_resource.rs @@ -0,0 +1,141 @@ +use std::time::Instant; + +use crate::function_tool::FunctionCallError; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use codex_protocol::models::function_call_output_content_items_to_text; +use codex_protocol::protocol::McpInvocation; +use codex_tools::ToolName; + +use rmcp::model::ReadResourceRequestParams; + +use super::ReadResourceArgs; +use super::ReadResourcePayload; +use super::call_tool_result_from_content; +use super::emit_tool_call_begin; +use super::emit_tool_call_end; +use super::normalize_required_string; +use super::parse_args; +use super::parse_arguments; +use super::serialize_function_output; + +pub struct ReadMcpResourceHandler; + +impl ToolHandler for ReadMcpResourceHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain("read_mcp_resource") + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + call_id, + payload, + .. + } = invocation; + + let arguments = match payload { + ToolPayload::Function { arguments } => arguments, + _ => { + return Err(FunctionCallError::RespondToModel( + "read_mcp_resource handler received unsupported payload".to_string(), + )); + } + }; + + let arguments = parse_arguments(arguments.as_str())?; + let args: ReadResourceArgs = parse_args(arguments.clone())?; + let ReadResourceArgs { server, uri } = args; + let server = normalize_required_string("server", server)?; + let uri = normalize_required_string("uri", uri)?; + + let invocation = McpInvocation { + server: server.clone(), + tool: "read_mcp_resource".to_string(), + arguments: arguments.clone(), + }; + + emit_tool_call_begin(&session, turn.as_ref(), &call_id, invocation.clone()).await; + let start = Instant::now(); + + let payload_result: Result = async { + let result = session + .read_resource( + &server, + ReadResourceRequestParams { + meta: None, + uri: uri.clone(), + }, + ) + .await + .map_err(|err| { + FunctionCallError::RespondToModel(format!("resources/read failed: {err:#}")) + })?; + + Ok(ReadResourcePayload { + server, + uri, + result, + }) + } + .await; + + match payload_result { + Ok(payload) => match serialize_function_output(payload) { + Ok(output) => { + let content = function_call_output_content_items_to_text(&output.body) + .unwrap_or_default(); + let duration = start.elapsed(); + emit_tool_call_end( + &session, + turn.as_ref(), + &call_id, + invocation, + duration, + Ok(call_tool_result_from_content(&content, output.success)), + ) + .await; + Ok(output) + } + Err(err) => { + let duration = start.elapsed(); + let message = err.to_string(); + emit_tool_call_end( + &session, + turn.as_ref(), + &call_id, + invocation, + duration, + Err(message.clone()), + ) + .await; + Err(err) + } + }, + Err(err) => { + let duration = start.elapsed(); + let message = err.to_string(); + emit_tool_call_end( + &session, + turn.as_ref(), + &call_id, + invocation, + duration, + Err(message.clone()), + ) + .await; + Err(err) + } + } + } +} diff --git a/codex-rs/core/src/tools/handlers/shell.rs b/codex-rs/core/src/tools/handlers/shell.rs index dfc3c3e5b9..469c0a0799 100644 --- a/codex-rs/core/src/tools/handlers/shell.rs +++ b/codex-rs/core/src/tools/handlers/shell.rs @@ -1,17 +1,13 @@ -use codex_protocol::ThreadId; +use codex_features::Feature; use codex_protocol::models::ShellCommandToolCallParams; use codex_protocol::models::ShellToolCallParams; use serde_json::Value as JsonValue; use std::sync::Arc; -use crate::exec::ExecCapturePolicy; use crate::exec::ExecParams; -use crate::exec_env::create_env; use crate::exec_policy::ExecApprovalRequest; use crate::function_tool::FunctionCallError; -use crate::maybe_emit_implicit_skill_invocation; use crate::session::turn_context::TurnContext; -use crate::shell::Shell; use crate::tools::context::FunctionToolOutput; use crate::tools::context::ToolInvocation; use crate::tools::context::ToolOutput; @@ -23,38 +19,26 @@ use crate::tools::handlers::apply_patch::intercept_apply_patch; use crate::tools::handlers::implicit_granted_permissions; use crate::tools::handlers::normalize_and_validate_additional_permissions; use crate::tools::handlers::parse_arguments; -use crate::tools::handlers::parse_arguments_with_base_path; -use crate::tools::handlers::resolve_workdir_base_path; use crate::tools::hook_names::HookToolName; use crate::tools::orchestrator::ToolOrchestrator; use crate::tools::registry::PostToolUsePayload; use crate::tools::registry::PreToolUsePayload; -use crate::tools::registry::ToolHandler; -use crate::tools::registry::ToolKind; use crate::tools::runtimes::shell::ShellRequest; use crate::tools::runtimes::shell::ShellRuntime; use crate::tools::runtimes::shell::ShellRuntimeBackend; use crate::tools::sandboxing::ToolCtx; -use codex_features::Feature; use codex_protocol::models::AdditionalPermissionProfile; use codex_protocol::protocol::ExecCommandSource; -use codex_shell_command::is_safe_command::is_known_safe_command; -use codex_tools::ShellCommandBackendConfig; -use codex_tools::ToolName; -pub struct ShellHandler; -pub struct ContainerExecHandler; -pub struct LocalShellHandler; +mod container_exec; +mod local_shell; +mod shell_command; +mod shell_handler; -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -enum ShellCommandBackend { - Classic, - ZshFork, -} - -pub struct ShellCommandHandler { - backend: ShellCommandBackend, -} +pub use container_exec::ContainerExecHandler; +pub use local_shell::LocalShellHandler; +pub use shell_command::ShellCommandHandler; +pub use shell_handler::ShellHandler; fn shell_function_payload_command(payload: &ToolPayload) -> Option { let ToolPayload::Function { arguments } = payload else { @@ -100,333 +84,6 @@ struct RunExecLikeArgs { shell_runtime_backend: ShellRuntimeBackend, } -impl ShellHandler { - fn to_exec_params( - params: &ShellToolCallParams, - turn_context: &TurnContext, - thread_id: ThreadId, - ) -> ExecParams { - ExecParams { - command: params.command.clone(), - cwd: turn_context.resolve_path(params.workdir.clone()), - expiration: params.timeout_ms.into(), - capture_policy: ExecCapturePolicy::ShellTool, - env: create_env(&turn_context.shell_environment_policy, Some(thread_id)), - network: turn_context.network.clone(), - sandbox_permissions: params.sandbox_permissions.unwrap_or_default(), - windows_sandbox_level: turn_context.windows_sandbox_level, - windows_sandbox_private_desktop: turn_context - .config - .permissions - .windows_sandbox_private_desktop, - justification: params.justification.clone(), - arg0: None, - } - } -} - -impl ShellCommandHandler { - fn shell_runtime_backend(&self) -> ShellRuntimeBackend { - match self.backend { - ShellCommandBackend::Classic => ShellRuntimeBackend::ShellCommandClassic, - ShellCommandBackend::ZshFork => ShellRuntimeBackend::ShellCommandZshFork, - } - } - - fn resolve_use_login_shell( - login: Option, - allow_login_shell: bool, - ) -> Result { - if !allow_login_shell && login == Some(true) { - return Err(FunctionCallError::RespondToModel( - "login shell is disabled by config; omit `login` or set it to false.".to_string(), - )); - } - - Ok(login.unwrap_or(allow_login_shell)) - } - - fn base_command(shell: &Shell, command: &str, use_login_shell: bool) -> Vec { - shell.derive_exec_args(command, use_login_shell) - } - - fn to_exec_params( - params: &ShellCommandToolCallParams, - session: &crate::session::session::Session, - turn_context: &TurnContext, - thread_id: ThreadId, - allow_login_shell: bool, - ) -> Result { - let shell = session.user_shell(); - let use_login_shell = Self::resolve_use_login_shell(params.login, allow_login_shell)?; - let command = Self::base_command(shell.as_ref(), ¶ms.command, use_login_shell); - - Ok(ExecParams { - command, - cwd: turn_context.resolve_path(params.workdir.clone()), - expiration: params.timeout_ms.into(), - capture_policy: ExecCapturePolicy::ShellTool, - env: create_env(&turn_context.shell_environment_policy, Some(thread_id)), - network: turn_context.network.clone(), - sandbox_permissions: params.sandbox_permissions.unwrap_or_default(), - windows_sandbox_level: turn_context.windows_sandbox_level, - windows_sandbox_private_desktop: turn_context - .config - .permissions - .windows_sandbox_private_desktop, - justification: params.justification.clone(), - arg0: None, - }) - } -} - -impl From for ShellCommandHandler { - fn from(config: ShellCommandBackendConfig) -> Self { - let backend = match config { - ShellCommandBackendConfig::Classic => ShellCommandBackend::Classic, - ShellCommandBackendConfig::ZshFork => ShellCommandBackend::ZshFork, - }; - Self { backend } - } -} - -impl ToolHandler for ShellHandler { - type Output = FunctionToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain("shell") - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - fn matches_kind(&self, payload: &ToolPayload) -> bool { - matches!(payload, ToolPayload::Function { .. }) - } - - async fn is_mutating(&self, invocation: &ToolInvocation) -> bool { - let ToolPayload::Function { arguments } = &invocation.payload else { - return true; - }; - - serde_json::from_str::(arguments) - .map(|params| !is_known_safe_command(¶ms.command)) - .unwrap_or(true) - } - - fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { - shell_function_pre_tool_use_payload(invocation) - } - - fn post_tool_use_payload( - &self, - invocation: &ToolInvocation, - result: &Self::Output, - ) -> Option { - shell_function_post_tool_use_payload(invocation, result) - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - tracker, - call_id, - payload, - .. - } = invocation; - - let arguments = match payload { - ToolPayload::Function { arguments } => arguments, - _ => { - return Err(FunctionCallError::RespondToModel( - "unsupported payload for shell handler".to_string(), - )); - } - }; - - let cwd = resolve_workdir_base_path(&arguments, &turn.cwd)?; - let params: ShellToolCallParams = parse_arguments_with_base_path(&arguments, &cwd)?; - let prefix_rule = params.prefix_rule.clone(); - let exec_params = - ShellHandler::to_exec_params(¶ms, turn.as_ref(), session.conversation_id); - ShellHandler::run_exec_like(RunExecLikeArgs { - tool_name: "shell".to_string(), - exec_params, - hook_command: codex_shell_command::parse_command::shlex_join(¶ms.command), - additional_permissions: params.additional_permissions.clone(), - prefix_rule, - session, - turn, - tracker, - call_id, - freeform: false, - shell_runtime_backend: ShellRuntimeBackend::Generic, - }) - .await - } -} - -impl ToolHandler for ContainerExecHandler { - type Output = FunctionToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain("container.exec") - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - fn matches_kind(&self, payload: &ToolPayload) -> bool { - matches!(payload, ToolPayload::Function { .. }) - } - - async fn is_mutating(&self, invocation: &ToolInvocation) -> bool { - let ToolPayload::Function { arguments } = &invocation.payload else { - return true; - }; - - serde_json::from_str::(arguments) - .map(|params| !is_known_safe_command(¶ms.command)) - .unwrap_or(true) - } - - fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { - shell_function_pre_tool_use_payload(invocation) - } - - fn post_tool_use_payload( - &self, - invocation: &ToolInvocation, - result: &Self::Output, - ) -> Option { - shell_function_post_tool_use_payload(invocation, result) - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - tracker, - call_id, - payload, - .. - } = invocation; - - let arguments = match payload { - ToolPayload::Function { arguments } => arguments, - _ => { - return Err(FunctionCallError::RespondToModel( - "unsupported payload for container.exec handler".to_string(), - )); - } - }; - - let cwd = resolve_workdir_base_path(&arguments, &turn.cwd)?; - let params: ShellToolCallParams = parse_arguments_with_base_path(&arguments, &cwd)?; - let prefix_rule = params.prefix_rule.clone(); - let exec_params = - ShellHandler::to_exec_params(¶ms, turn.as_ref(), session.conversation_id); - ShellHandler::run_exec_like(RunExecLikeArgs { - tool_name: "container.exec".to_string(), - exec_params, - hook_command: codex_shell_command::parse_command::shlex_join(¶ms.command), - additional_permissions: params.additional_permissions.clone(), - prefix_rule, - session, - turn, - tracker, - call_id, - freeform: false, - shell_runtime_backend: ShellRuntimeBackend::Generic, - }) - .await - } -} - -impl ToolHandler for LocalShellHandler { - type Output = FunctionToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain("local_shell") - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - fn matches_kind(&self, payload: &ToolPayload) -> bool { - matches!(payload, ToolPayload::LocalShell { .. }) - } - - async fn is_mutating(&self, invocation: &ToolInvocation) -> bool { - let ToolPayload::LocalShell { params } = &invocation.payload else { - return true; - }; - - !is_known_safe_command(¶ms.command) - } - - fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { - local_shell_payload_command(&invocation.payload).map(|command| PreToolUsePayload { - tool_name: HookToolName::bash(), - tool_input: serde_json::json!({ "command": command }), - }) - } - - fn post_tool_use_payload( - &self, - invocation: &ToolInvocation, - result: &Self::Output, - ) -> Option { - let tool_response = - result.post_tool_use_response(&invocation.call_id, &invocation.payload)?; - let command = local_shell_payload_command(&invocation.payload)?; - Some(PostToolUsePayload { - tool_name: HookToolName::bash(), - tool_use_id: invocation.call_id.clone(), - tool_input: serde_json::json!({ "command": command }), - tool_response, - }) - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - tracker, - call_id, - payload, - .. - } = invocation; - - let ToolPayload::LocalShell { params } = payload else { - return Err(FunctionCallError::RespondToModel( - "unsupported payload for local_shell handler".to_string(), - )); - }; - - let exec_params = - ShellHandler::to_exec_params(¶ms, turn.as_ref(), session.conversation_id); - ShellHandler::run_exec_like(RunExecLikeArgs { - tool_name: "local_shell".to_string(), - exec_params, - hook_command: codex_shell_command::parse_command::shlex_join(¶ms.command), - additional_permissions: None, - prefix_rule: None, - session, - turn, - tracker, - call_id, - freeform: false, - shell_runtime_backend: ShellRuntimeBackend::Generic, - }) - .await - } -} - fn shell_function_pre_tool_use_payload(invocation: &ToolInvocation) -> Option { shell_function_payload_command(&invocation.payload).map(|command| PreToolUsePayload { tool_name: HookToolName::bash(), @@ -448,316 +105,203 @@ fn shell_function_post_tool_use_payload( }) } -impl ToolHandler for ShellCommandHandler { - type Output = FunctionToolOutput; +async fn run_exec_like(args: RunExecLikeArgs) -> Result { + let RunExecLikeArgs { + tool_name, + exec_params, + hook_command, + additional_permissions, + prefix_rule, + session, + turn, + tracker, + call_id, + freeform, + shell_runtime_backend, + } = args; - fn tool_name(&self) -> ToolName { - ToolName::plain("shell_command") + let mut exec_params = exec_params; + let Some(turn_environment) = turn.environments.primary() else { + return Err(FunctionCallError::RespondToModel( + "shell is unavailable in this session".to_string(), + )); + }; + let fs = turn_environment.environment.get_filesystem(); + + let dependency_env = session.dependency_env().await; + if !dependency_env.is_empty() { + exec_params.env.extend(dependency_env.clone()); } - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - fn matches_kind(&self, payload: &ToolPayload) -> bool { - matches!(payload, ToolPayload::Function { .. }) - } - - async fn is_mutating(&self, invocation: &ToolInvocation) -> bool { - let ToolPayload::Function { arguments } = &invocation.payload else { - return true; - }; - - serde_json::from_str::(arguments) - .map(|params| { - let use_login_shell = match Self::resolve_use_login_shell( - params.login, - invocation.turn.tools_config.allow_login_shell, - ) { - Ok(use_login_shell) => use_login_shell, - Err(_) => return true, - }; - let shell = invocation.session.user_shell(); - let command = Self::base_command(shell.as_ref(), ¶ms.command, use_login_shell); - !is_known_safe_command(&command) - }) - .unwrap_or(true) - } - - fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { - shell_command_payload_command(&invocation.payload).map(|command| PreToolUsePayload { - tool_name: HookToolName::bash(), - tool_input: serde_json::json!({ "command": command }), - }) - } - - fn post_tool_use_payload( - &self, - invocation: &ToolInvocation, - result: &Self::Output, - ) -> Option { - let tool_response = - result.post_tool_use_response(&invocation.call_id, &invocation.payload)?; - let command = shell_command_payload_command(&invocation.payload)?; - Some(PostToolUsePayload { - tool_name: HookToolName::bash(), - tool_use_id: invocation.call_id.clone(), - tool_input: serde_json::json!({ "command": command }), - tool_response, - }) - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - tracker, - call_id, - payload, - .. - } = invocation; - - let ToolPayload::Function { arguments } = payload else { - return Err(FunctionCallError::RespondToModel(format!( - "unsupported payload for shell_command handler: {}", - self.tool_name().display() - ))); - }; - - let cwd = resolve_workdir_base_path(&arguments, &turn.cwd)?; - let params: ShellCommandToolCallParams = parse_arguments_with_base_path(&arguments, &cwd)?; - let workdir = turn.resolve_path(params.workdir.clone()); - maybe_emit_implicit_skill_invocation( - session.as_ref(), - turn.as_ref(), - ¶ms.command, - &workdir, - ) - .await; - let prefix_rule = params.prefix_rule.clone(); - let exec_params = Self::to_exec_params( - ¶ms, - session.as_ref(), - turn.as_ref(), - session.conversation_id, - turn.tools_config.allow_login_shell, - )?; - ShellHandler::run_exec_like(RunExecLikeArgs { - tool_name: self.tool_name().display(), - exec_params, - hook_command: params.command, - additional_permissions: params.additional_permissions.clone(), - prefix_rule, - session, - turn, - tracker, - call_id, - freeform: true, - shell_runtime_backend: self.shell_runtime_backend(), - }) - .await - } -} - -impl ShellHandler { - async fn run_exec_like(args: RunExecLikeArgs) -> Result { - let RunExecLikeArgs { - tool_name, - exec_params, - hook_command, - additional_permissions, - prefix_rule, - session, - turn, - tracker, - call_id, - freeform, - shell_runtime_backend, - } = args; - - let mut exec_params = exec_params; - let Some(turn_environment) = turn.environments.primary() else { - return Err(FunctionCallError::RespondToModel( - "shell is unavailable in this session".to_string(), - )); - }; - let fs = turn_environment.environment.get_filesystem(); - - let dependency_env = session.dependency_env().await; - if !dependency_env.is_empty() { - exec_params.env.extend(dependency_env.clone()); + let mut explicit_env_overrides = turn.shell_environment_policy.r#set.clone(); + for key in dependency_env.keys() { + if let Some(value) = exec_params.env.get(key) { + explicit_env_overrides.insert(key.clone(), value.clone()); } + } - let mut explicit_env_overrides = turn.shell_environment_policy.r#set.clone(); - for key in dependency_env.keys() { - if let Some(value) = exec_params.env.get(key) { - explicit_env_overrides.insert(key.clone(), value.clone()); - } - } + let exec_permission_approvals_enabled = + session.features().enabled(Feature::ExecPermissionApprovals); + let requested_additional_permissions = additional_permissions.clone(); + let effective_additional_permissions = apply_granted_turn_permissions( + session.as_ref(), + turn.cwd.as_path(), + exec_params.sandbox_permissions, + additional_permissions, + ) + .await; + let additional_permissions_allowed = exec_permission_approvals_enabled + || (session.features().enabled(Feature::RequestPermissionsTool) + && effective_additional_permissions.permissions_preapproved); + let normalized_additional_permissions = implicit_granted_permissions( + exec_params.sandbox_permissions, + requested_additional_permissions.as_ref(), + &effective_additional_permissions, + ) + .map_or_else( + || { + normalize_and_validate_additional_permissions( + additional_permissions_allowed, + turn.approval_policy.value(), + effective_additional_permissions.sandbox_permissions, + effective_additional_permissions.additional_permissions, + effective_additional_permissions.permissions_preapproved, + &exec_params.cwd, + ) + }, + |permissions| Ok(Some(permissions)), + ) + .map_err(FunctionCallError::RespondToModel)?; - let exec_permission_approvals_enabled = - session.features().enabled(Feature::ExecPermissionApprovals); - let requested_additional_permissions = additional_permissions.clone(); - let effective_additional_permissions = apply_granted_turn_permissions( - session.as_ref(), - turn.cwd.as_path(), - exec_params.sandbox_permissions, - additional_permissions, + // Approval policy guard for explicit escalation in non-OnRequest modes. + // Sticky turn permissions have already been approved, so they should + // continue through the normal exec approval flow for the command. + if effective_additional_permissions + .sandbox_permissions + .requests_sandbox_override() + && !effective_additional_permissions.permissions_preapproved + && !matches!( + turn.approval_policy.value(), + codex_protocol::protocol::AskForApproval::OnRequest ) - .await; - let additional_permissions_allowed = exec_permission_approvals_enabled - || (session.features().enabled(Feature::RequestPermissionsTool) - && effective_additional_permissions.permissions_preapproved); - let normalized_additional_permissions = implicit_granted_permissions( - exec_params.sandbox_permissions, - requested_additional_permissions.as_ref(), - &effective_additional_permissions, - ) - .map_or_else( - || { - normalize_and_validate_additional_permissions( - additional_permissions_allowed, - turn.approval_policy.value(), - effective_additional_permissions.sandbox_permissions, - effective_additional_permissions.additional_permissions, - effective_additional_permissions.permissions_preapproved, - &exec_params.cwd, - ) + { + let approval_policy = turn.approval_policy.value(); + return Err(FunctionCallError::RespondToModel(format!( + "approval policy is {approval_policy:?}; reject command — you should not ask for escalated permissions if the approval policy is {approval_policy:?}" + ))); + } + + // Intercept apply_patch if present. + if let Some(output) = intercept_apply_patch( + &exec_params.command, + &exec_params.cwd, + fs.as_ref(), + session.clone(), + turn.clone(), + Some(&tracker), + &call_id, + tool_name.as_str(), + ) + .await? + { + return Ok(output); + } + + let source = ExecCommandSource::Agent; + let emitter = ToolEmitter::shell( + exec_params.command.clone(), + exec_params.cwd.clone(), + source, + freeform, + ); + let event_ctx = ToolEventCtx::new( + session.as_ref(), + turn.as_ref(), + &call_id, + /*turn_diff_tracker*/ None, + ); + emitter.begin(event_ctx).await; + + let file_system_sandbox_policy = turn.file_system_sandbox_policy(); + let exec_approval_requirement = session + .services + .exec_policy + .create_exec_approval_requirement_for_command(ExecApprovalRequest { + command: &exec_params.command, + approval_policy: turn.approval_policy.value(), + permission_profile: turn.permission_profile(), + file_system_sandbox_policy: &file_system_sandbox_policy, + sandbox_cwd: turn.cwd.as_path(), + sandbox_permissions: if effective_additional_permissions.permissions_preapproved { + codex_protocol::models::SandboxPermissions::UseDefault + } else { + effective_additional_permissions.sandbox_permissions }, - |permissions| Ok(Some(permissions)), - ) - .map_err(FunctionCallError::RespondToModel)?; - - // Approval policy guard for explicit escalation in non-OnRequest modes. - // Sticky turn permissions have already been approved, so they should - // continue through the normal exec approval flow for the command. - if effective_additional_permissions - .sandbox_permissions - .requests_sandbox_override() - && !effective_additional_permissions.permissions_preapproved - && !matches!( - turn.approval_policy.value(), - codex_protocol::protocol::AskForApproval::OnRequest - ) - { - let approval_policy = turn.approval_policy.value(); - return Err(FunctionCallError::RespondToModel(format!( - "approval policy is {approval_policy:?}; reject command — you should not ask for escalated permissions if the approval policy is {approval_policy:?}" - ))); - } - - // Intercept apply_patch if present. - if let Some(output) = intercept_apply_patch( - &exec_params.command, - &exec_params.cwd, - fs.as_ref(), - session.clone(), - turn.clone(), - Some(&tracker), - &call_id, - tool_name.as_str(), - ) - .await? - { - return Ok(output); - } - - let source = ExecCommandSource::Agent; - let emitter = ToolEmitter::shell( - exec_params.command.clone(), - exec_params.cwd.clone(), - source, - freeform, - ); - let event_ctx = ToolEventCtx::new( - session.as_ref(), - turn.as_ref(), - &call_id, - /*turn_diff_tracker*/ None, - ); - emitter.begin(event_ctx).await; - - let file_system_sandbox_policy = turn.file_system_sandbox_policy(); - let exec_approval_requirement = session - .services - .exec_policy - .create_exec_approval_requirement_for_command(ExecApprovalRequest { - command: &exec_params.command, - approval_policy: turn.approval_policy.value(), - permission_profile: turn.permission_profile(), - file_system_sandbox_policy: &file_system_sandbox_policy, - sandbox_cwd: turn.cwd.as_path(), - sandbox_permissions: if effective_additional_permissions.permissions_preapproved { - codex_protocol::models::SandboxPermissions::UseDefault - } else { - effective_additional_permissions.sandbox_permissions - }, - prefix_rule, - }) - .await; - - let req = ShellRequest { - command: exec_params.command.clone(), - hook_command, - cwd: exec_params.cwd.clone(), - timeout_ms: exec_params.expiration.timeout_ms(), - env: exec_params.env.clone(), - explicit_env_overrides, - network: exec_params.network.clone(), - sandbox_permissions: effective_additional_permissions.sandbox_permissions, - additional_permissions: normalized_additional_permissions, - #[cfg(unix)] - additional_permissions_preapproved: effective_additional_permissions - .permissions_preapproved, - justification: exec_params.justification.clone(), - exec_approval_requirement, - }; - let mut orchestrator = ToolOrchestrator::new(); - let mut runtime = { - use ShellRuntimeBackend::*; - match shell_runtime_backend { - Generic => ShellRuntime::new(), - backend @ (ShellCommandClassic | ShellCommandZshFork) => { - ShellRuntime::for_shell_command(backend) - } - } - }; - let tool_ctx = ToolCtx { - session: session.clone(), - turn: turn.clone(), - call_id: call_id.clone(), - tool_name, - }; - let out = orchestrator - .run( - &mut runtime, - &req, - &tool_ctx, - &turn, - turn.approval_policy.value(), - ) - .await - .map(|result| result.output); - let event_ctx = ToolEventCtx::new( - session.as_ref(), - turn.as_ref(), - &call_id, - /*turn_diff_tracker*/ None, - ); - let post_tool_use_response = out - .as_ref() - .ok() - .map(|output| crate::tools::format_exec_output_str(output, turn.truncation_policy)) - .map(JsonValue::String); - let content = emitter.finish(event_ctx, out).await?; - Ok(FunctionToolOutput { - body: vec![ - codex_protocol::models::FunctionCallOutputContentItem::InputText { text: content }, - ], - success: Some(true), - post_tool_use_response, + prefix_rule, }) - } + .await; + + let req = ShellRequest { + command: exec_params.command.clone(), + hook_command, + cwd: exec_params.cwd.clone(), + timeout_ms: exec_params.expiration.timeout_ms(), + env: exec_params.env.clone(), + explicit_env_overrides, + network: exec_params.network.clone(), + sandbox_permissions: effective_additional_permissions.sandbox_permissions, + additional_permissions: normalized_additional_permissions, + #[cfg(unix)] + additional_permissions_preapproved: effective_additional_permissions + .permissions_preapproved, + justification: exec_params.justification.clone(), + exec_approval_requirement, + }; + let mut orchestrator = ToolOrchestrator::new(); + let mut runtime = { + use ShellRuntimeBackend::*; + match shell_runtime_backend { + Generic => ShellRuntime::new(), + backend @ (ShellCommandClassic | ShellCommandZshFork) => { + ShellRuntime::for_shell_command(backend) + } + } + }; + let tool_ctx = ToolCtx { + session: session.clone(), + turn: turn.clone(), + call_id: call_id.clone(), + tool_name, + }; + let out = orchestrator + .run( + &mut runtime, + &req, + &tool_ctx, + &turn, + turn.approval_policy.value(), + ) + .await + .map(|result| result.output); + let event_ctx = ToolEventCtx::new( + session.as_ref(), + turn.as_ref(), + &call_id, + /*turn_diff_tracker*/ None, + ); + let post_tool_use_response = out + .as_ref() + .ok() + .map(|output| crate::tools::format_exec_output_str(output, turn.truncation_policy)) + .map(JsonValue::String); + let content = emitter.finish(event_ctx, out).await?; + Ok(FunctionToolOutput { + body: vec![ + codex_protocol::models::FunctionCallOutputContentItem::InputText { text: content }, + ], + success: Some(true), + post_tool_use_response, + }) } #[cfg(test)] diff --git a/codex-rs/core/src/tools/handlers/shell/container_exec.rs b/codex-rs/core/src/tools/handlers/shell/container_exec.rs new file mode 100644 index 0000000000..70bf56fb4d --- /dev/null +++ b/codex-rs/core/src/tools/handlers/shell/container_exec.rs @@ -0,0 +1,101 @@ +use codex_protocol::models::ShellToolCallParams; +use codex_shell_command::is_safe_command::is_known_safe_command; +use codex_tools::ToolName; + +use crate::function_tool::FunctionCallError; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::handlers::parse_arguments_with_base_path; +use crate::tools::handlers::resolve_workdir_base_path; +use crate::tools::registry::PostToolUsePayload; +use crate::tools::registry::PreToolUsePayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use crate::tools::runtimes::shell::ShellRuntimeBackend; + +use super::RunExecLikeArgs; +use super::run_exec_like; +use super::shell_function_post_tool_use_payload; +use super::shell_function_pre_tool_use_payload; +use super::shell_handler::ShellHandler; + +pub struct ContainerExecHandler; + +impl ToolHandler for ContainerExecHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain("container.exec") + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + fn matches_kind(&self, payload: &ToolPayload) -> bool { + matches!(payload, ToolPayload::Function { .. }) + } + + async fn is_mutating(&self, invocation: &ToolInvocation) -> bool { + let ToolPayload::Function { arguments } = &invocation.payload else { + return true; + }; + + serde_json::from_str::(arguments) + .map(|params| !is_known_safe_command(¶ms.command)) + .unwrap_or(true) + } + + fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { + shell_function_pre_tool_use_payload(invocation) + } + + fn post_tool_use_payload( + &self, + invocation: &ToolInvocation, + result: &Self::Output, + ) -> Option { + shell_function_post_tool_use_payload(invocation, result) + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + tracker, + call_id, + payload, + .. + } = invocation; + + let arguments = match payload { + ToolPayload::Function { arguments } => arguments, + _ => { + return Err(FunctionCallError::RespondToModel( + "unsupported payload for container.exec handler".to_string(), + )); + } + }; + + let cwd = resolve_workdir_base_path(&arguments, &turn.cwd)?; + let params: ShellToolCallParams = parse_arguments_with_base_path(&arguments, &cwd)?; + let prefix_rule = params.prefix_rule.clone(); + let exec_params = + ShellHandler::to_exec_params(¶ms, turn.as_ref(), session.conversation_id); + run_exec_like(RunExecLikeArgs { + tool_name: "container.exec".to_string(), + exec_params, + hook_command: codex_shell_command::parse_command::shlex_join(¶ms.command), + additional_permissions: params.additional_permissions.clone(), + prefix_rule, + session, + turn, + tracker, + call_id, + freeform: false, + shell_runtime_backend: ShellRuntimeBackend::Generic, + }) + .await + } +} diff --git a/codex-rs/core/src/tools/handlers/shell/local_shell.rs b/codex-rs/core/src/tools/handlers/shell/local_shell.rs new file mode 100644 index 0000000000..bdb70e9368 --- /dev/null +++ b/codex-rs/core/src/tools/handlers/shell/local_shell.rs @@ -0,0 +1,102 @@ +use codex_shell_command::is_safe_command::is_known_safe_command; +use codex_tools::ToolName; + +use crate::function_tool::FunctionCallError; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolOutput; +use crate::tools::context::ToolPayload; +use crate::tools::hook_names::HookToolName; +use crate::tools::registry::PostToolUsePayload; +use crate::tools::registry::PreToolUsePayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use crate::tools::runtimes::shell::ShellRuntimeBackend; + +use super::RunExecLikeArgs; +use super::local_shell_payload_command; +use super::run_exec_like; +use super::shell_handler::ShellHandler; + +pub struct LocalShellHandler; + +impl ToolHandler for LocalShellHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain("local_shell") + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + fn matches_kind(&self, payload: &ToolPayload) -> bool { + matches!(payload, ToolPayload::LocalShell { .. }) + } + + async fn is_mutating(&self, invocation: &ToolInvocation) -> bool { + let ToolPayload::LocalShell { params } = &invocation.payload else { + return true; + }; + + !is_known_safe_command(¶ms.command) + } + + fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { + local_shell_payload_command(&invocation.payload).map(|command| PreToolUsePayload { + tool_name: HookToolName::bash(), + tool_input: serde_json::json!({ "command": command }), + }) + } + + fn post_tool_use_payload( + &self, + invocation: &ToolInvocation, + result: &Self::Output, + ) -> Option { + let tool_response = + result.post_tool_use_response(&invocation.call_id, &invocation.payload)?; + let command = local_shell_payload_command(&invocation.payload)?; + Some(PostToolUsePayload { + tool_name: HookToolName::bash(), + tool_use_id: invocation.call_id.clone(), + tool_input: serde_json::json!({ "command": command }), + tool_response, + }) + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + tracker, + call_id, + payload, + .. + } = invocation; + + let ToolPayload::LocalShell { params } = payload else { + return Err(FunctionCallError::RespondToModel( + "unsupported payload for local_shell handler".to_string(), + )); + }; + + let exec_params = + ShellHandler::to_exec_params(¶ms, turn.as_ref(), session.conversation_id); + run_exec_like(RunExecLikeArgs { + tool_name: "local_shell".to_string(), + exec_params, + hook_command: codex_shell_command::parse_command::shlex_join(¶ms.command), + additional_permissions: None, + prefix_rule: None, + session, + turn, + tracker, + call_id, + freeform: false, + shell_runtime_backend: ShellRuntimeBackend::Generic, + }) + .await + } +} diff --git a/codex-rs/core/src/tools/handlers/shell/shell_command.rs b/codex-rs/core/src/tools/handlers/shell/shell_command.rs new file mode 100644 index 0000000000..69f965b51e --- /dev/null +++ b/codex-rs/core/src/tools/handlers/shell/shell_command.rs @@ -0,0 +1,215 @@ +use codex_protocol::ThreadId; +use codex_protocol::models::ShellCommandToolCallParams; +use codex_shell_command::is_safe_command::is_known_safe_command; +use codex_tools::ShellCommandBackendConfig; +use codex_tools::ToolName; + +use crate::exec::ExecCapturePolicy; +use crate::exec::ExecParams; +use crate::exec_env::create_env; +use crate::function_tool::FunctionCallError; +use crate::maybe_emit_implicit_skill_invocation; +use crate::session::turn_context::TurnContext; +use crate::shell::Shell; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolOutput; +use crate::tools::context::ToolPayload; +use crate::tools::handlers::parse_arguments_with_base_path; +use crate::tools::handlers::resolve_workdir_base_path; +use crate::tools::hook_names::HookToolName; +use crate::tools::registry::PostToolUsePayload; +use crate::tools::registry::PreToolUsePayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use crate::tools::runtimes::shell::ShellRuntimeBackend; + +use super::RunExecLikeArgs; +use super::run_exec_like; +use super::shell_command_payload_command; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +enum ShellCommandBackend { + Classic, + ZshFork, +} + +pub struct ShellCommandHandler { + backend: ShellCommandBackend, +} + +impl ShellCommandHandler { + fn shell_runtime_backend(&self) -> ShellRuntimeBackend { + match self.backend { + ShellCommandBackend::Classic => ShellRuntimeBackend::ShellCommandClassic, + ShellCommandBackend::ZshFork => ShellRuntimeBackend::ShellCommandZshFork, + } + } + + pub(super) fn resolve_use_login_shell( + login: Option, + allow_login_shell: bool, + ) -> Result { + if !allow_login_shell && login == Some(true) { + return Err(FunctionCallError::RespondToModel( + "login shell is disabled by config; omit `login` or set it to false.".to_string(), + )); + } + + Ok(login.unwrap_or(allow_login_shell)) + } + + pub(super) fn base_command(shell: &Shell, command: &str, use_login_shell: bool) -> Vec { + shell.derive_exec_args(command, use_login_shell) + } + + pub(super) fn to_exec_params( + params: &ShellCommandToolCallParams, + session: &crate::session::session::Session, + turn_context: &TurnContext, + thread_id: ThreadId, + allow_login_shell: bool, + ) -> Result { + let shell = session.user_shell(); + let use_login_shell = Self::resolve_use_login_shell(params.login, allow_login_shell)?; + let command = Self::base_command(shell.as_ref(), ¶ms.command, use_login_shell); + + Ok(ExecParams { + command, + cwd: turn_context.resolve_path(params.workdir.clone()), + expiration: params.timeout_ms.into(), + capture_policy: ExecCapturePolicy::ShellTool, + env: create_env(&turn_context.shell_environment_policy, Some(thread_id)), + network: turn_context.network.clone(), + sandbox_permissions: params.sandbox_permissions.unwrap_or_default(), + windows_sandbox_level: turn_context.windows_sandbox_level, + windows_sandbox_private_desktop: turn_context + .config + .permissions + .windows_sandbox_private_desktop, + justification: params.justification.clone(), + arg0: None, + }) + } +} + +impl From for ShellCommandHandler { + fn from(config: ShellCommandBackendConfig) -> Self { + let backend = match config { + ShellCommandBackendConfig::Classic => ShellCommandBackend::Classic, + ShellCommandBackendConfig::ZshFork => ShellCommandBackend::ZshFork, + }; + Self { backend } + } +} + +impl ToolHandler for ShellCommandHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain("shell_command") + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + fn matches_kind(&self, payload: &ToolPayload) -> bool { + matches!(payload, ToolPayload::Function { .. }) + } + + async fn is_mutating(&self, invocation: &ToolInvocation) -> bool { + let ToolPayload::Function { arguments } = &invocation.payload else { + return true; + }; + + serde_json::from_str::(arguments) + .map(|params| { + let use_login_shell = match Self::resolve_use_login_shell( + params.login, + invocation.turn.tools_config.allow_login_shell, + ) { + Ok(use_login_shell) => use_login_shell, + Err(_) => return true, + }; + let shell = invocation.session.user_shell(); + let command = Self::base_command(shell.as_ref(), ¶ms.command, use_login_shell); + !is_known_safe_command(&command) + }) + .unwrap_or(true) + } + + fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { + shell_command_payload_command(&invocation.payload).map(|command| PreToolUsePayload { + tool_name: HookToolName::bash(), + tool_input: serde_json::json!({ "command": command }), + }) + } + + fn post_tool_use_payload( + &self, + invocation: &ToolInvocation, + result: &Self::Output, + ) -> Option { + let tool_response = + result.post_tool_use_response(&invocation.call_id, &invocation.payload)?; + let command = shell_command_payload_command(&invocation.payload)?; + Some(PostToolUsePayload { + tool_name: HookToolName::bash(), + tool_use_id: invocation.call_id.clone(), + tool_input: serde_json::json!({ "command": command }), + tool_response, + }) + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + tracker, + call_id, + payload, + .. + } = invocation; + + let ToolPayload::Function { arguments } = payload else { + return Err(FunctionCallError::RespondToModel(format!( + "unsupported payload for shell_command handler: {}", + self.tool_name().display() + ))); + }; + + let cwd = resolve_workdir_base_path(&arguments, &turn.cwd)?; + let params: ShellCommandToolCallParams = parse_arguments_with_base_path(&arguments, &cwd)?; + let workdir = turn.resolve_path(params.workdir.clone()); + maybe_emit_implicit_skill_invocation( + session.as_ref(), + turn.as_ref(), + ¶ms.command, + &workdir, + ) + .await; + let prefix_rule = params.prefix_rule.clone(); + let exec_params = Self::to_exec_params( + ¶ms, + session.as_ref(), + turn.as_ref(), + session.conversation_id, + turn.tools_config.allow_login_shell, + )?; + run_exec_like(RunExecLikeArgs { + tool_name: self.tool_name().display(), + exec_params, + hook_command: params.command, + additional_permissions: params.additional_permissions.clone(), + prefix_rule, + session, + turn, + tracker, + call_id, + freeform: true, + shell_runtime_backend: self.shell_runtime_backend(), + }) + .await + } +} diff --git a/codex-rs/core/src/tools/handlers/shell/shell_handler.rs b/codex-rs/core/src/tools/handlers/shell/shell_handler.rs new file mode 100644 index 0000000000..30220d3db0 --- /dev/null +++ b/codex-rs/core/src/tools/handlers/shell/shell_handler.rs @@ -0,0 +1,130 @@ +use codex_protocol::ThreadId; +use codex_protocol::models::ShellToolCallParams; +use codex_shell_command::is_safe_command::is_known_safe_command; +use codex_tools::ToolName; + +use crate::exec::ExecCapturePolicy; +use crate::exec::ExecParams; +use crate::exec_env::create_env; +use crate::function_tool::FunctionCallError; +use crate::session::turn_context::TurnContext; +use crate::tools::context::FunctionToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::handlers::parse_arguments_with_base_path; +use crate::tools::handlers::resolve_workdir_base_path; +use crate::tools::registry::PostToolUsePayload; +use crate::tools::registry::PreToolUsePayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use crate::tools::runtimes::shell::ShellRuntimeBackend; + +use super::RunExecLikeArgs; +use super::run_exec_like; +use super::shell_function_post_tool_use_payload; +use super::shell_function_pre_tool_use_payload; + +pub struct ShellHandler; + +impl ShellHandler { + pub(super) fn to_exec_params( + params: &ShellToolCallParams, + turn_context: &TurnContext, + thread_id: ThreadId, + ) -> ExecParams { + ExecParams { + command: params.command.clone(), + cwd: turn_context.resolve_path(params.workdir.clone()), + expiration: params.timeout_ms.into(), + capture_policy: ExecCapturePolicy::ShellTool, + env: create_env(&turn_context.shell_environment_policy, Some(thread_id)), + network: turn_context.network.clone(), + sandbox_permissions: params.sandbox_permissions.unwrap_or_default(), + windows_sandbox_level: turn_context.windows_sandbox_level, + windows_sandbox_private_desktop: turn_context + .config + .permissions + .windows_sandbox_private_desktop, + justification: params.justification.clone(), + arg0: None, + } + } +} + +impl ToolHandler for ShellHandler { + type Output = FunctionToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain("shell") + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + fn matches_kind(&self, payload: &ToolPayload) -> bool { + matches!(payload, ToolPayload::Function { .. }) + } + + async fn is_mutating(&self, invocation: &ToolInvocation) -> bool { + let ToolPayload::Function { arguments } = &invocation.payload else { + return true; + }; + + serde_json::from_str::(arguments) + .map(|params| !is_known_safe_command(¶ms.command)) + .unwrap_or(true) + } + + fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { + shell_function_pre_tool_use_payload(invocation) + } + + fn post_tool_use_payload( + &self, + invocation: &ToolInvocation, + result: &Self::Output, + ) -> Option { + shell_function_post_tool_use_payload(invocation, result) + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + tracker, + call_id, + payload, + .. + } = invocation; + + let arguments = match payload { + ToolPayload::Function { arguments } => arguments, + _ => { + return Err(FunctionCallError::RespondToModel( + "unsupported payload for shell handler".to_string(), + )); + } + }; + + let cwd = resolve_workdir_base_path(&arguments, &turn.cwd)?; + let params: ShellToolCallParams = parse_arguments_with_base_path(&arguments, &cwd)?; + let prefix_rule = params.prefix_rule.clone(); + let exec_params = + ShellHandler::to_exec_params(¶ms, turn.as_ref(), session.conversation_id); + run_exec_like(RunExecLikeArgs { + tool_name: "shell".to_string(), + exec_params, + hook_command: codex_shell_command::parse_command::shlex_join(¶ms.command), + additional_permissions: params.additional_permissions.clone(), + prefix_rule, + session, + turn, + tracker, + call_id, + freeform: false, + shell_runtime_backend: ShellRuntimeBackend::Generic, + }) + .await + } +} diff --git a/codex-rs/core/src/tools/handlers/shell_tests.rs b/codex-rs/core/src/tools/handlers/shell_tests.rs index 8a32e5404b..a7e6dae35c 100644 --- a/codex-rs/core/src/tools/handlers/shell_tests.rs +++ b/codex-rs/core/src/tools/handlers/shell_tests.rs @@ -247,9 +247,7 @@ async fn shell_command_pre_tool_use_payload_uses_raw_command() { arguments: json!({ "command": "printf shell command" }).to_string(), }; let (session, turn) = make_session_and_context().await; - let handler = ShellCommandHandler { - backend: super::ShellCommandBackend::Classic, - }; + let handler = ShellCommandHandler::from(codex_tools::ShellCommandBackendConfig::Classic); assert_eq!( handler.pre_tool_use_payload(&ToolInvocation { @@ -279,9 +277,7 @@ async fn build_post_tool_use_payload_uses_tool_output_wire_value() { success: Some(true), post_tool_use_response: Some(json!("shell output")), }; - let handler = ShellCommandHandler { - backend: super::ShellCommandBackend::Classic, - }; + let handler = ShellCommandHandler::from(codex_tools::ShellCommandBackendConfig::Classic); let (session, turn) = make_session_and_context().await; let invocation = ToolInvocation { session: session.into(), diff --git a/codex-rs/core/src/tools/handlers/unified_exec.rs b/codex-rs/core/src/tools/handlers/unified_exec.rs index c257240a4d..80e85ccd47 100644 --- a/codex-rs/core/src/tools/handlers/unified_exec.rs +++ b/codex-rs/core/src/tools/handlers/unified_exec.rs @@ -1,5 +1,3 @@ -use crate::function_tool::FunctionCallError; -use crate::maybe_emit_implicit_skill_invocation; use crate::sandboxing::SandboxPermissions; use crate::shell::Shell; use crate::shell::get_shell_by_model_provided_path; @@ -7,42 +5,24 @@ use crate::tools::context::ExecCommandToolOutput; use crate::tools::context::ToolInvocation; use crate::tools::context::ToolOutput; use crate::tools::context::ToolPayload; -use crate::tools::handlers::apply_granted_turn_permissions; -use crate::tools::handlers::apply_patch::intercept_apply_patch; -use crate::tools::handlers::implicit_granted_permissions; -use crate::tools::handlers::normalize_and_validate_additional_permissions; -use crate::tools::handlers::parse_arguments; -use crate::tools::handlers::parse_arguments_with_base_path; -use crate::tools::handlers::resolve_tool_environment; use crate::tools::hook_names::HookToolName; use crate::tools::registry::PostToolUsePayload; -use crate::tools::registry::PreToolUsePayload; -use crate::tools::registry::ToolHandler; -use crate::tools::registry::ToolKind; -use crate::unified_exec::ExecCommandRequest; -use crate::unified_exec::UnifiedExecContext; -use crate::unified_exec::UnifiedExecError; -use crate::unified_exec::UnifiedExecProcessManager; -use crate::unified_exec::WriteStdinRequest; -use crate::unified_exec::generate_chunk_id; use crate::unified_exec::resolve_max_tokens; -use codex_features::Feature; -use codex_otel::SessionTelemetry; -use codex_otel::TOOL_CALL_UNIFIED_EXEC_METRIC; use codex_protocol::models::AdditionalPermissionProfile; -use codex_protocol::protocol::EventMsg; -use codex_protocol::protocol::TerminalInteractionEvent; -use codex_shell_command::is_safe_command::is_known_safe_command; -use codex_tools::ToolName; use codex_tools::UnifiedExecShellMode; use codex_utils_output_truncation::TruncationPolicy; -use codex_utils_output_truncation::approx_token_count; use serde::Deserialize; use std::path::PathBuf; use std::sync::Arc; -pub struct ExecCommandHandler; -pub struct WriteStdinHandler; +#[cfg(test)] +use crate::tools::handlers::parse_arguments; + +mod exec_command; +mod write_stdin; + +pub use exec_command::ExecCommandHandler; +pub use write_stdin::WriteStdinHandler; #[derive(Debug, Deserialize)] pub(crate) struct ExecCommandArgs { @@ -79,18 +59,6 @@ struct ExecCommandEnvironmentArgs { workdir: Option, } -#[derive(Debug, Deserialize)] -struct WriteStdinArgs { - // The model is trained on `session_id`. - session_id: i32, - #[serde(default)] - chars: String, - #[serde(default = "default_write_stdin_yield_time_ms")] - yield_time_ms: u64, - #[serde(default)] - max_output_tokens: Option, -} - fn default_exec_yield_time_ms() -> u64 { 10_000 } @@ -110,343 +78,6 @@ fn effective_max_output_tokens( resolve_max_tokens(max_output_tokens).min(truncation_policy.token_budget()) } -impl ToolHandler for ExecCommandHandler { - type Output = ExecCommandToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain("exec_command") - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - fn matches_kind(&self, payload: &ToolPayload) -> bool { - matches!(payload, ToolPayload::Function { .. }) - } - - async fn is_mutating(&self, invocation: &ToolInvocation) -> bool { - let ToolPayload::Function { arguments } = &invocation.payload else { - tracing::error!( - "This should never happen, invocation payload is wrong: {:?}", - invocation.payload - ); - return true; - }; - - let Ok(params) = parse_arguments::(arguments) else { - return true; - }; - let command = match get_command( - ¶ms, - invocation.session.user_shell(), - &invocation.turn.tools_config.unified_exec_shell_mode, - invocation.turn.tools_config.allow_login_shell, - ) { - Ok(command) => command, - Err(_) => return true, - }; - !is_known_safe_command(&command) - } - - fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { - let ToolPayload::Function { arguments } = &invocation.payload else { - return None; - }; - - parse_arguments::(arguments) - .ok() - .map(|args| PreToolUsePayload { - tool_name: HookToolName::bash(), - tool_input: serde_json::json!({ "command": args.cmd }), - }) - } - - fn post_tool_use_payload( - &self, - invocation: &ToolInvocation, - result: &Self::Output, - ) -> Option { - post_unified_exec_tool_use_payload(invocation, result) - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - tracker, - call_id, - payload, - .. - } = invocation; - - let arguments = match payload { - ToolPayload::Function { arguments } => arguments, - _ => { - return Err(FunctionCallError::RespondToModel( - "exec_command handler received unsupported payload".to_string(), - )); - } - }; - - let manager: &UnifiedExecProcessManager = &session.services.unified_exec_manager; - let context = UnifiedExecContext::new(session.clone(), turn.clone(), call_id.clone()); - let environment_args: ExecCommandEnvironmentArgs = parse_arguments(&arguments)?; - let Some(turn_environment) = - resolve_tool_environment(turn.as_ref(), environment_args.environment_id.as_deref())? - else { - return Err(FunctionCallError::RespondToModel( - "unified exec is unavailable in this session".to_string(), - )); - }; - let cwd = environment_args - .workdir - .as_deref() - .filter(|workdir| !workdir.is_empty()) - .map_or_else( - || turn_environment.cwd.clone(), - |workdir| turn_environment.cwd.join(workdir), - ); - let environment = Arc::clone(&turn_environment.environment); - let fs = environment.get_filesystem(); - let args: ExecCommandArgs = parse_arguments_with_base_path(&arguments, &cwd)?; - let hook_command = args.cmd.clone(); - maybe_emit_implicit_skill_invocation( - session.as_ref(), - context.turn.as_ref(), - &hook_command, - &cwd, - ) - .await; - let process_id = manager.allocate_process_id().await; - let command = get_command( - &args, - session.user_shell(), - &turn.tools_config.unified_exec_shell_mode, - turn.tools_config.allow_login_shell, - ) - .map_err(FunctionCallError::RespondToModel)?; - let command_for_display = codex_shell_command::parse_command::shlex_join(&command); - - let ExecCommandArgs { - tty, - yield_time_ms, - max_output_tokens, - sandbox_permissions, - additional_permissions, - justification, - prefix_rule, - .. - } = args; - let max_output_tokens = - effective_max_output_tokens(max_output_tokens, turn.truncation_policy); - - let exec_permission_approvals_enabled = - session.features().enabled(Feature::ExecPermissionApprovals); - let requested_additional_permissions = additional_permissions.clone(); - let effective_additional_permissions = apply_granted_turn_permissions( - context.session.as_ref(), - cwd.as_path(), - sandbox_permissions, - additional_permissions, - ) - .await; - let additional_permissions_allowed = exec_permission_approvals_enabled - || (session.features().enabled(Feature::RequestPermissionsTool) - && effective_additional_permissions.permissions_preapproved); - - // Sticky turn permissions have already been approved, so they should - // continue through the normal exec approval flow for the command. - if effective_additional_permissions - .sandbox_permissions - .requests_sandbox_override() - && !effective_additional_permissions.permissions_preapproved - && !matches!( - context.turn.approval_policy.value(), - codex_protocol::protocol::AskForApproval::OnRequest - ) - { - let approval_policy = context.turn.approval_policy.value(); - manager.release_process_id(process_id).await; - return Err(FunctionCallError::RespondToModel(format!( - "approval policy is {approval_policy:?}; reject command — you cannot ask for escalated permissions if the approval policy is {approval_policy:?}" - ))); - } - - let normalized_additional_permissions = match implicit_granted_permissions( - sandbox_permissions, - requested_additional_permissions.as_ref(), - &effective_additional_permissions, - ) - .map_or_else( - || { - normalize_and_validate_additional_permissions( - additional_permissions_allowed, - context.turn.approval_policy.value(), - effective_additional_permissions.sandbox_permissions, - effective_additional_permissions.additional_permissions, - effective_additional_permissions.permissions_preapproved, - &cwd, - ) - }, - |permissions| Ok(Some(permissions)), - ) { - Ok(normalized) => normalized, - Err(err) => { - manager.release_process_id(process_id).await; - return Err(FunctionCallError::RespondToModel(err)); - } - }; - - if let Some(output) = intercept_apply_patch( - &command, - &cwd, - fs.as_ref(), - context.session.clone(), - context.turn.clone(), - Some(&tracker), - &context.call_id, - "exec_command", - ) - .await? - { - manager.release_process_id(process_id).await; - return Ok(ExecCommandToolOutput { - event_call_id: String::new(), - chunk_id: String::new(), - wall_time: std::time::Duration::ZERO, - raw_output: output.into_text().into_bytes(), - max_output_tokens: Some(max_output_tokens), - process_id: None, - exit_code: None, - original_token_count: None, - hook_command: None, - }); - } - - emit_unified_exec_tty_metric(&turn.session_telemetry, tty); - match manager - .exec_command( - ExecCommandRequest { - command, - hook_command: hook_command.clone(), - process_id, - yield_time_ms, - max_output_tokens: Some(max_output_tokens), - cwd, - environment, - network: context.turn.network.clone(), - tty, - sandbox_permissions: effective_additional_permissions.sandbox_permissions, - additional_permissions: normalized_additional_permissions, - additional_permissions_preapproved: effective_additional_permissions - .permissions_preapproved, - justification, - prefix_rule, - }, - &context, - ) - .await - { - Ok(response) => Ok(response), - Err(UnifiedExecError::SandboxDenied { output, .. }) => { - let output_text = output.aggregated_output.text; - let original_token_count = approx_token_count(&output_text); - Ok(ExecCommandToolOutput { - event_call_id: context.call_id.clone(), - chunk_id: generate_chunk_id(), - wall_time: output.duration, - raw_output: output_text.into_bytes(), - max_output_tokens: Some(max_output_tokens), - // Sandbox denial is terminal, so there is no live - // process for write_stdin to resume. - process_id: None, - exit_code: Some(output.exit_code), - original_token_count: Some(original_token_count), - hook_command: Some(hook_command), - }) - } - Err(err) => Err(FunctionCallError::RespondToModel(format!( - "exec_command failed for `{command_for_display}`: {err:?}" - ))), - } - } -} - -impl ToolHandler for WriteStdinHandler { - type Output = ExecCommandToolOutput; - - fn tool_name(&self) -> ToolName { - ToolName::plain("write_stdin") - } - - fn kind(&self) -> ToolKind { - ToolKind::Function - } - - fn matches_kind(&self, payload: &ToolPayload) -> bool { - matches!(payload, ToolPayload::Function { .. }) - } - - async fn is_mutating(&self, _invocation: &ToolInvocation) -> bool { - true - } - - fn post_tool_use_payload( - &self, - invocation: &ToolInvocation, - result: &Self::Output, - ) -> Option { - post_unified_exec_tool_use_payload(invocation, result) - } - - async fn handle(&self, invocation: ToolInvocation) -> Result { - let ToolInvocation { - session, - turn, - payload, - .. - } = invocation; - - let arguments = match payload { - ToolPayload::Function { arguments } => arguments, - _ => { - return Err(FunctionCallError::RespondToModel( - "write_stdin handler received unsupported payload".to_string(), - )); - } - }; - - let args: WriteStdinArgs = parse_arguments(&arguments)?; - let max_output_tokens = - effective_max_output_tokens(args.max_output_tokens, turn.truncation_policy); - let response = session - .services - .unified_exec_manager - .write_stdin(WriteStdinRequest { - process_id: args.session_id, - input: &args.chars, - yield_time_ms: args.yield_time_ms, - max_output_tokens: Some(max_output_tokens), - }) - .await - .map_err(|err| { - FunctionCallError::RespondToModel(format!("write_stdin failed: {err}")) - })?; - - let interaction = TerminalInteractionEvent { - call_id: response.event_call_id.clone(), - process_id: args.session_id.to_string(), - stdin: args.chars.clone(), - }; - session - .send_event(turn.as_ref(), EventMsg::TerminalInteraction(interaction)) - .await; - - Ok(response) - } -} - fn post_unified_exec_tool_use_payload( invocation: &ToolInvocation, result: &ExecCommandToolOutput, @@ -470,14 +101,6 @@ fn post_unified_exec_tool_use_payload( }) } -fn emit_unified_exec_tty_metric(session_telemetry: &SessionTelemetry, tty: bool) { - session_telemetry.counter( - TOOL_CALL_UNIFIED_EXEC_METRIC, - /*inc*/ 1, - &[("tty", if tty { "true" } else { "false" })], - ); -} - pub(crate) fn get_command( args: &ExecCommandArgs, session_shell: Arc, diff --git a/codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs b/codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs new file mode 100644 index 0000000000..75ae3fea29 --- /dev/null +++ b/codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs @@ -0,0 +1,309 @@ +use std::sync::Arc; + +use crate::function_tool::FunctionCallError; +use crate::maybe_emit_implicit_skill_invocation; +use crate::tools::context::ExecCommandToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::handlers::apply_granted_turn_permissions; +use crate::tools::handlers::apply_patch::intercept_apply_patch; +use crate::tools::handlers::implicit_granted_permissions; +use crate::tools::handlers::normalize_and_validate_additional_permissions; +use crate::tools::handlers::parse_arguments; +use crate::tools::handlers::parse_arguments_with_base_path; +use crate::tools::handlers::resolve_tool_environment; +use crate::tools::hook_names::HookToolName; +use crate::tools::registry::PostToolUsePayload; +use crate::tools::registry::PreToolUsePayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use crate::unified_exec::ExecCommandRequest; +use crate::unified_exec::UnifiedExecContext; +use crate::unified_exec::UnifiedExecError; +use crate::unified_exec::UnifiedExecProcessManager; +use crate::unified_exec::generate_chunk_id; +use codex_features::Feature; +use codex_otel::SessionTelemetry; +use codex_otel::TOOL_CALL_UNIFIED_EXEC_METRIC; +use codex_shell_command::is_safe_command::is_known_safe_command; +use codex_tools::ToolName; +use codex_utils_output_truncation::approx_token_count; + +use super::ExecCommandArgs; +use super::ExecCommandEnvironmentArgs; +use super::effective_max_output_tokens; +use super::get_command; +use super::post_unified_exec_tool_use_payload; + +pub struct ExecCommandHandler; + +impl ToolHandler for ExecCommandHandler { + type Output = ExecCommandToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain("exec_command") + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + fn matches_kind(&self, payload: &ToolPayload) -> bool { + matches!(payload, ToolPayload::Function { .. }) + } + + async fn is_mutating(&self, invocation: &ToolInvocation) -> bool { + let ToolPayload::Function { arguments } = &invocation.payload else { + tracing::error!( + "This should never happen, invocation payload is wrong: {:?}", + invocation.payload + ); + return true; + }; + + let Ok(params) = parse_arguments::(arguments) else { + return true; + }; + let command = match get_command( + ¶ms, + invocation.session.user_shell(), + &invocation.turn.tools_config.unified_exec_shell_mode, + invocation.turn.tools_config.allow_login_shell, + ) { + Ok(command) => command, + Err(_) => return true, + }; + !is_known_safe_command(&command) + } + + fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { + let ToolPayload::Function { arguments } = &invocation.payload else { + return None; + }; + + parse_arguments::(arguments) + .ok() + .map(|args| PreToolUsePayload { + tool_name: HookToolName::bash(), + tool_input: serde_json::json!({ "command": args.cmd }), + }) + } + + fn post_tool_use_payload( + &self, + invocation: &ToolInvocation, + result: &Self::Output, + ) -> Option { + post_unified_exec_tool_use_payload(invocation, result) + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + tracker, + call_id, + payload, + .. + } = invocation; + + let arguments = match payload { + ToolPayload::Function { arguments } => arguments, + _ => { + return Err(FunctionCallError::RespondToModel( + "exec_command handler received unsupported payload".to_string(), + )); + } + }; + + let manager: &UnifiedExecProcessManager = &session.services.unified_exec_manager; + let context = UnifiedExecContext::new(session.clone(), turn.clone(), call_id.clone()); + let environment_args: ExecCommandEnvironmentArgs = parse_arguments(&arguments)?; + let Some(turn_environment) = + resolve_tool_environment(turn.as_ref(), environment_args.environment_id.as_deref())? + else { + return Err(FunctionCallError::RespondToModel( + "unified exec is unavailable in this session".to_string(), + )); + }; + let cwd = environment_args + .workdir + .as_deref() + .filter(|workdir| !workdir.is_empty()) + .map_or_else( + || turn_environment.cwd.clone(), + |workdir| turn_environment.cwd.join(workdir), + ); + let environment = Arc::clone(&turn_environment.environment); + let fs = environment.get_filesystem(); + let args: ExecCommandArgs = parse_arguments_with_base_path(&arguments, &cwd)?; + let hook_command = args.cmd.clone(); + maybe_emit_implicit_skill_invocation( + session.as_ref(), + context.turn.as_ref(), + &hook_command, + &cwd, + ) + .await; + let process_id = manager.allocate_process_id().await; + let command = get_command( + &args, + session.user_shell(), + &turn.tools_config.unified_exec_shell_mode, + turn.tools_config.allow_login_shell, + ) + .map_err(FunctionCallError::RespondToModel)?; + let command_for_display = codex_shell_command::parse_command::shlex_join(&command); + + let ExecCommandArgs { + tty, + yield_time_ms, + max_output_tokens, + sandbox_permissions, + additional_permissions, + justification, + prefix_rule, + .. + } = args; + let max_output_tokens = + effective_max_output_tokens(max_output_tokens, turn.truncation_policy); + + let exec_permission_approvals_enabled = + session.features().enabled(Feature::ExecPermissionApprovals); + let requested_additional_permissions = additional_permissions.clone(); + let effective_additional_permissions = apply_granted_turn_permissions( + context.session.as_ref(), + cwd.as_path(), + sandbox_permissions, + additional_permissions, + ) + .await; + let additional_permissions_allowed = exec_permission_approvals_enabled + || (session.features().enabled(Feature::RequestPermissionsTool) + && effective_additional_permissions.permissions_preapproved); + + // Sticky turn permissions have already been approved, so they should + // continue through the normal exec approval flow for the command. + if effective_additional_permissions + .sandbox_permissions + .requests_sandbox_override() + && !effective_additional_permissions.permissions_preapproved + && !matches!( + context.turn.approval_policy.value(), + codex_protocol::protocol::AskForApproval::OnRequest + ) + { + let approval_policy = context.turn.approval_policy.value(); + manager.release_process_id(process_id).await; + return Err(FunctionCallError::RespondToModel(format!( + "approval policy is {approval_policy:?}; reject command — you cannot ask for escalated permissions if the approval policy is {approval_policy:?}" + ))); + } + + let normalized_additional_permissions = match implicit_granted_permissions( + sandbox_permissions, + requested_additional_permissions.as_ref(), + &effective_additional_permissions, + ) + .map_or_else( + || { + normalize_and_validate_additional_permissions( + additional_permissions_allowed, + context.turn.approval_policy.value(), + effective_additional_permissions.sandbox_permissions, + effective_additional_permissions.additional_permissions, + effective_additional_permissions.permissions_preapproved, + &cwd, + ) + }, + |permissions| Ok(Some(permissions)), + ) { + Ok(normalized) => normalized, + Err(err) => { + manager.release_process_id(process_id).await; + return Err(FunctionCallError::RespondToModel(err)); + } + }; + + if let Some(output) = intercept_apply_patch( + &command, + &cwd, + fs.as_ref(), + context.session.clone(), + context.turn.clone(), + Some(&tracker), + &context.call_id, + "exec_command", + ) + .await? + { + manager.release_process_id(process_id).await; + return Ok(ExecCommandToolOutput { + event_call_id: String::new(), + chunk_id: String::new(), + wall_time: std::time::Duration::ZERO, + raw_output: output.into_text().into_bytes(), + max_output_tokens: Some(max_output_tokens), + process_id: None, + exit_code: None, + original_token_count: None, + hook_command: None, + }); + } + + emit_unified_exec_tty_metric(&turn.session_telemetry, tty); + match manager + .exec_command( + ExecCommandRequest { + command, + hook_command: hook_command.clone(), + process_id, + yield_time_ms, + max_output_tokens: Some(max_output_tokens), + cwd, + environment, + network: context.turn.network.clone(), + tty, + sandbox_permissions: effective_additional_permissions.sandbox_permissions, + additional_permissions: normalized_additional_permissions, + additional_permissions_preapproved: effective_additional_permissions + .permissions_preapproved, + justification, + prefix_rule, + }, + &context, + ) + .await + { + Ok(response) => Ok(response), + Err(UnifiedExecError::SandboxDenied { output, .. }) => { + let output_text = output.aggregated_output.text; + let original_token_count = approx_token_count(&output_text); + Ok(ExecCommandToolOutput { + event_call_id: context.call_id.clone(), + chunk_id: generate_chunk_id(), + wall_time: output.duration, + raw_output: output_text.into_bytes(), + max_output_tokens: Some(max_output_tokens), + // Sandbox denial is terminal, so there is no live + // process for write_stdin to resume. + process_id: None, + exit_code: Some(output.exit_code), + original_token_count: Some(original_token_count), + hook_command: Some(hook_command), + }) + } + Err(err) => Err(FunctionCallError::RespondToModel(format!( + "exec_command failed for `{command_for_display}`: {err:?}" + ))), + } + } +} + +fn emit_unified_exec_tty_metric(session_telemetry: &SessionTelemetry, tty: bool) { + session_telemetry.counter( + TOOL_CALL_UNIFIED_EXEC_METRIC, + /*inc*/ 1, + &[("tty", if tty { "true" } else { "false" })], + ); +} diff --git a/codex-rs/core/src/tools/handlers/unified_exec/write_stdin.rs b/codex-rs/core/src/tools/handlers/unified_exec/write_stdin.rs new file mode 100644 index 0000000000..1e9c68f227 --- /dev/null +++ b/codex-rs/core/src/tools/handlers/unified_exec/write_stdin.rs @@ -0,0 +1,104 @@ +use crate::function_tool::FunctionCallError; +use crate::tools::context::ExecCommandToolOutput; +use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolPayload; +use crate::tools::handlers::parse_arguments; +use crate::tools::registry::PostToolUsePayload; +use crate::tools::registry::ToolHandler; +use crate::tools::registry::ToolKind; +use crate::unified_exec::WriteStdinRequest; +use codex_protocol::protocol::EventMsg; +use codex_protocol::protocol::TerminalInteractionEvent; +use codex_tools::ToolName; +use serde::Deserialize; + +use super::effective_max_output_tokens; +use super::post_unified_exec_tool_use_payload; + +#[derive(Debug, Deserialize)] +struct WriteStdinArgs { + // The model is trained on `session_id`. + session_id: i32, + #[serde(default)] + chars: String, + #[serde(default = "super::default_write_stdin_yield_time_ms")] + yield_time_ms: u64, + #[serde(default)] + max_output_tokens: Option, +} + +pub struct WriteStdinHandler; + +impl ToolHandler for WriteStdinHandler { + type Output = ExecCommandToolOutput; + + fn tool_name(&self) -> ToolName { + ToolName::plain("write_stdin") + } + + fn kind(&self) -> ToolKind { + ToolKind::Function + } + + fn matches_kind(&self, payload: &ToolPayload) -> bool { + matches!(payload, ToolPayload::Function { .. }) + } + + async fn is_mutating(&self, _invocation: &ToolInvocation) -> bool { + true + } + + fn post_tool_use_payload( + &self, + invocation: &ToolInvocation, + result: &Self::Output, + ) -> Option { + post_unified_exec_tool_use_payload(invocation, result) + } + + async fn handle(&self, invocation: ToolInvocation) -> Result { + let ToolInvocation { + session, + turn, + payload, + .. + } = invocation; + + let arguments = match payload { + ToolPayload::Function { arguments } => arguments, + _ => { + return Err(FunctionCallError::RespondToModel( + "write_stdin handler received unsupported payload".to_string(), + )); + } + }; + + let args: WriteStdinArgs = parse_arguments(&arguments)?; + let max_output_tokens = + effective_max_output_tokens(args.max_output_tokens, turn.truncation_policy); + let response = session + .services + .unified_exec_manager + .write_stdin(WriteStdinRequest { + process_id: args.session_id, + input: &args.chars, + yield_time_ms: args.yield_time_ms, + max_output_tokens: Some(max_output_tokens), + }) + .await + .map_err(|err| { + FunctionCallError::RespondToModel(format!("write_stdin failed: {err}")) + })?; + + let interaction = TerminalInteractionEvent { + call_id: response.event_call_id.clone(), + process_id: args.session_id.to_string(), + stdin: args.chars.clone(), + }; + session + .send_event(turn.as_ref(), EventMsg::TerminalInteraction(interaction)) + .await; + + Ok(response) + } +} From 21295f47e2acda34007d61aabba28f3e26538249 Mon Sep 17 00:00:00 2001 From: rhan-oai Date: Wed, 6 May 2026 13:18:41 -0700 Subject: [PATCH 6/9] [codex-tui] pass thread source for tui threads (#21401) ## Summary - mark TUI-created thread starts and forks with explicit `thread_source = user` - add focused coverage for embedded and remote lifecycle request builders ## Why Thread analytics now consume an explicit thread-level source classification instead of inferring it from `session_source`. The TUI still omitted that field, so TUI-created interactive threads would continue to land as `null` even after the new analytics plumbing shipped. ## Validation - `cargo test -p codex-tui app_server_session --lib` --- codex-rs/tui/src/app_server_session.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/codex-rs/tui/src/app_server_session.rs b/codex-rs/tui/src/app_server_session.rs index 6a170beec7..9f1e0f8101 100644 --- a/codex-rs/tui/src/app_server_session.rs +++ b/codex-rs/tui/src/app_server_session.rs @@ -87,6 +87,7 @@ use codex_app_server_protocol::ThreadSetNameParams; use codex_app_server_protocol::ThreadSetNameResponse; use codex_app_server_protocol::ThreadShellCommandParams; use codex_app_server_protocol::ThreadShellCommandResponse; +use codex_app_server_protocol::ThreadSource; use codex_app_server_protocol::ThreadStartParams; use codex_app_server_protocol::ThreadStartResponse; use codex_app_server_protocol::ThreadStartSource; @@ -1195,6 +1196,7 @@ fn thread_start_params_from_config( config: config_request_overrides_from_config(config), ephemeral: Some(config.ephemeral), session_start_source, + thread_source: Some(ThreadSource::User), persist_extended_history: false, ..ThreadStartParams::default() } @@ -1260,6 +1262,7 @@ fn thread_fork_params_from_config( base_instructions: config.base_instructions.clone(), developer_instructions: config.developer_instructions.clone(), ephemeral: config.ephemeral, + thread_source: Some(ThreadSource::User), persist_extended_history: false, ..ThreadForkParams::default() } @@ -1561,6 +1564,7 @@ mod tests { .map(permissions_selection_from_active_profile) ); assert_eq!(params.model_provider, Some(config.model_provider_id)); + assert_eq!(params.thread_source, Some(ThreadSource::User)); } #[tokio::test] @@ -1677,6 +1681,8 @@ mod tests { assert_eq!(start.permissions, None); assert_eq!(resume.permissions, None); assert_eq!(fork.permissions, None); + assert_eq!(start.thread_source, Some(ThreadSource::User)); + assert_eq!(fork.thread_source, Some(ThreadSource::User)); } #[test] @@ -1782,6 +1788,8 @@ mod tests { assert_eq!(start.permissions, None); assert_eq!(resume.permissions, None); assert_eq!(fork.permissions, None); + assert_eq!(start.thread_source, Some(ThreadSource::User)); + assert_eq!(fork.thread_source, Some(ThreadSource::User)); } #[tokio::test] From fbdbc6b2fea7522e9fc1fc87d88168b945507ad4 Mon Sep 17 00:00:00 2001 From: rhan-oai Date: Wed, 6 May 2026 13:27:41 -0700 Subject: [PATCH 7/9] [codex-analytics] emit tool item events from item lifecycle (#17090) ## Why After the tool-item schemas are in place, analytics needs to emit them from the app-server item lifecycle rather than requiring bespoke tracking at each callsite. The reducer should also reuse the shared thread analytics context introduced below it in the stack so later event families do not repeat the same reducer joins or missing-state ladder. ## What changed - Tracks tool-item completion notifications and emits the matching tool analytics event when a terminal item arrives. - Derives event-specific payload details for command execution, file changes, MCP calls, dynamic tools, collaboration tools, web search, and image generation. - Denormalizes thread, app-server client, runtime, and subagent provenance metadata through the shared thread analytics context. - Adds reducer coverage for item lifecycle emission and subagent metadata inheritance. ## Duration semantics `duration_ms` is computed from the app-server item lifecycle timestamps: `completed_at_ms - started_at_ms`. That makes it the duration of the lifecycle Codex observed locally, not necessarily the upstream provider's full execution time. - Web search usually has a meaningful observed lifecycle because Responses can send `response.output_item.added` before `response.output_item.done`; in that case `started_at_ms` comes from the added event and `completed_at_ms` comes from the done event. - Image generation can be much less precise. In the current observed stream, image generation often arrives only as a completed `response.output_item.done`; when there is no earlier added event, Codex synthesizes the started item immediately before completion, so `duration_ms` can be `0` even though upstream image generation took longer. - Standalone web search and standalone image generation work is expected to land after this stack. Those paths may introduce more direct lifecycle events or timing points, so the current web-search/image-generation duration semantics should be treated as the best available item-lifecycle approximation, not the final latency contract for those tool families. - `execution_duration_ms` is populated only where the completed item already carries a native execution duration; otherwise it remains `null` while `duration_ms` still reflects the local lifecycle interval. ## Currently placeholder / partial fields Some fields are included in the schema for the intended steady-state contract, but this PR does not yet populate them from real approval/review state: - `review_count`, `guardian_review_count`, and `user_review_count` currently default to `0`. - `final_approval_outcome` currently defaults to `unknown`. - `requested_additional_permissions` and `requested_network_access` currently default to `false`. ## Verification - `cargo test -p codex-analytics` --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/openai/codex/pull/17090). * #18748 * #18747 * __->__ #17090 * #17089 * #20514 --- .../analytics/src/analytics_client_tests.rs | 274 +++++++- codex-rs/analytics/src/client.rs | 19 +- codex-rs/analytics/src/events.rs | 23 +- codex-rs/analytics/src/lib.rs | 24 + codex-rs/analytics/src/reducer.rs | 660 +++++++++++++++++- .../app-server/src/bespoke_event_handling.rs | 42 -- codex-rs/app-server/src/message_processor.rs | 1 - codex-rs/app-server/src/outgoing_message.rs | 7 +- .../request_processors/thread_lifecycle.rs | 3 - .../request_processors/thread_processor.rs | 5 - .../src/request_processors/turn_processor.rs | 1 - 11 files changed, 980 insertions(+), 79 deletions(-) diff --git a/codex-rs/analytics/src/analytics_client_tests.rs b/codex-rs/analytics/src/analytics_client_tests.rs index 121a378566..fd294385e5 100644 --- a/codex-rs/analytics/src/analytics_client_tests.rs +++ b/codex-rs/analytics/src/analytics_client_tests.rs @@ -12,7 +12,6 @@ use crate::events::CodexPluginUsedEventRequest; use crate::events::CodexRuntimeMetadata; use crate::events::CodexToolItemEventBase; use crate::events::CodexTurnEventRequest; -use crate::events::CommandExecutionSource; use crate::events::GuardianApprovalRequestSource; use crate::events::GuardianReviewDecision; use crate::events::GuardianReviewEventParams; @@ -67,8 +66,13 @@ use codex_app_server_protocol::ClientInfo; use codex_app_server_protocol::ClientRequest; use codex_app_server_protocol::ClientResponsePayload; use codex_app_server_protocol::CodexErrorInfo; +use codex_app_server_protocol::CommandAction; +use codex_app_server_protocol::CommandExecutionSource; +use codex_app_server_protocol::CommandExecutionStatus; use codex_app_server_protocol::InitializeCapabilities; use codex_app_server_protocol::InitializeParams; +use codex_app_server_protocol::ItemCompletedNotification; +use codex_app_server_protocol::ItemStartedNotification; use codex_app_server_protocol::JSONRPCErrorError; use codex_app_server_protocol::NonSteerableTurnKind; use codex_app_server_protocol::RequestId; @@ -78,6 +82,7 @@ use codex_app_server_protocol::SessionSource as AppServerSessionSource; use codex_app_server_protocol::Thread; use codex_app_server_protocol::ThreadArchiveParams; use codex_app_server_protocol::ThreadArchiveResponse; +use codex_app_server_protocol::ThreadItem; use codex_app_server_protocol::ThreadResumeResponse; use codex_app_server_protocol::ThreadSource as AppServerThreadSource; use codex_app_server_protocol::ThreadStartResponse; @@ -598,6 +603,90 @@ async fn ingest_turn_prerequisites( } } +async fn ingest_tool_review_prerequisites( + reducer: &mut AnalyticsReducer, + events: &mut Vec, +) { + reducer + .ingest(sample_initialize_fact(/*connection_id*/ 7), events) + .await; + reducer + .ingest( + AnalyticsFact::ClientResponse { + connection_id: 7, + request_id: RequestId::Integer(1), + response: Box::new(sample_thread_start_response( + "thread-1", /*ephemeral*/ false, "gpt-5", + )), + }, + events, + ) + .await; + events.clear(); +} + +fn sample_initialize_fact(connection_id: u64) -> AnalyticsFact { + AnalyticsFact::Initialize { + connection_id, + params: InitializeParams { + client_info: ClientInfo { + name: "codex-tui".to_string(), + title: None, + version: "1.0.0".to_string(), + }, + capabilities: Some(InitializeCapabilities { + experimental_api: false, + opt_out_notification_methods: None, + }), + }, + product_client_id: DEFAULT_ORIGINATOR.to_string(), + runtime: CodexRuntimeMetadata { + codex_rs_version: "0.99.0".to_string(), + runtime_os: "linux".to_string(), + runtime_os_version: "24.04".to_string(), + runtime_arch: "x86_64".to_string(), + }, + rpc_transport: AppServerRpcTransport::Websocket, + } +} + +fn sample_command_execution_item( + status: CommandExecutionStatus, + exit_code: Option, + duration_ms: Option, +) -> ThreadItem { + ThreadItem::CommandExecution { + id: "item-1".to_string(), + command: "echo hi".to_string(), + cwd: test_path_buf("/tmp").abs(), + process_id: Some("pid-1".to_string()), + source: CommandExecutionSource::Agent, + status, + command_actions: Vec::new(), + aggregated_output: None, + exit_code, + duration_ms, + } +} + +fn sample_command_execution_item_with_actions( + status: CommandExecutionStatus, + exit_code: Option, + duration_ms: Option, + command_actions: Vec, +) -> ThreadItem { + let mut item = sample_command_execution_item(status, exit_code, duration_ms); + let ThreadItem::CommandExecution { + command_actions: item_command_actions, + .. + } = &mut item + else { + unreachable!("sample command execution item should be CommandExecution"); + }; + *item_command_actions = command_actions; + item +} + fn expected_absolute_path(path: &PathBuf) -> String { std::fs::canonicalize(path) .unwrap_or_else(|_| path.to_path_buf()) @@ -930,6 +1019,7 @@ fn command_execution_event_serializes_expected_shape() { started_at_ms: 123_000, completed_at_ms: 125_000, duration_ms: Some(2000), + execution_duration_ms: Some(1900), review_count: 0, guardian_review_count: 0, user_review_count: 0, @@ -978,6 +1068,7 @@ fn command_execution_event_serializes_expected_shape() { "started_at_ms": 123000, "completed_at_ms": 125000, "duration_ms": 2000, + "execution_duration_ms": 1900, "review_count": 0, "guardian_review_count": 0, "user_review_count": 0, @@ -1404,6 +1495,114 @@ async fn guardian_review_event_ingests_custom_fact_with_optional_target_item() { assert_eq!(payload[0]["event_params"]["review_timeout_ms"], 90_000); } +#[tokio::test] +async fn item_lifecycle_notifications_publish_command_execution_event() { + let mut reducer = AnalyticsReducer::default(); + let mut events = Vec::new(); + + ingest_tool_review_prerequisites(&mut reducer, &mut events).await; + reducer + .ingest( + AnalyticsFact::Notification(Box::new(ServerNotification::ItemStarted( + ItemStartedNotification { + thread_id: "thread-1".to_string(), + turn_id: "turn-1".to_string(), + started_at_ms: 1_000, + item: sample_command_execution_item( + CommandExecutionStatus::InProgress, + /*exit_code*/ None, + /*duration_ms*/ None, + ), + }, + ))), + &mut events, + ) + .await; + assert!( + events.is_empty(), + "tool item event should emit on completion" + ); + + reducer + .ingest( + AnalyticsFact::Notification(Box::new(ServerNotification::ItemCompleted( + ItemCompletedNotification { + thread_id: "thread-1".to_string(), + turn_id: "turn-1".to_string(), + completed_at_ms: 1_045, + item: sample_command_execution_item_with_actions( + CommandExecutionStatus::Completed, + Some(0), + Some(42), + vec![ + CommandAction::Read { + command: "cat README.md".to_string(), + name: "README.md".to_string(), + path: test_path_buf("/tmp/README.md").abs(), + }, + CommandAction::ListFiles { + command: "ls".to_string(), + path: None, + }, + CommandAction::Search { + command: "rg TODO".to_string(), + query: Some("TODO".to_string()), + path: None, + }, + CommandAction::Unknown { + command: "cargo test".to_string(), + }, + ], + ), + }, + ))), + &mut events, + ) + .await; + + let payload = serde_json::to_value(&events).expect("serialize events"); + assert_eq!(payload.as_array().expect("events array").len(), 1); + assert_eq!(payload[0]["event_type"], "codex_command_execution_event"); + assert_eq!(payload[0]["event_params"]["thread_id"], "thread-1"); + assert_eq!(payload[0]["event_params"]["turn_id"], "turn-1"); + assert_eq!(payload[0]["event_params"]["item_id"], "item-1"); + assert_eq!(payload[0]["event_params"]["tool_name"], "shell"); + assert_eq!( + payload[0]["event_params"]["command_execution_source"], + "agent" + ); + assert_eq!(payload[0]["event_params"]["terminal_status"], "completed"); + assert_eq!( + payload[0]["event_params"]["final_approval_outcome"], + "unknown" + ); + assert_eq!( + payload[0]["event_params"]["failure_kind"], + serde_json::Value::Null + ); + assert_eq!(payload[0]["event_params"]["exit_code"], 0); + assert_eq!(payload[0]["event_params"]["command_total_action_count"], 4); + assert_eq!(payload[0]["event_params"]["command_read_action_count"], 1); + assert_eq!( + payload[0]["event_params"]["command_list_files_action_count"], + 1 + ); + assert_eq!(payload[0]["event_params"]["command_search_action_count"], 1); + assert_eq!( + payload[0]["event_params"]["command_unknown_action_count"], + 1 + ); + assert_eq!(payload[0]["event_params"]["started_at_ms"], 1_000); + assert_eq!(payload[0]["event_params"]["completed_at_ms"], 1_045); + assert_eq!(payload[0]["event_params"]["duration_ms"], 45); + assert_eq!(payload[0]["event_params"]["execution_duration_ms"], 42); + assert_eq!( + payload[0]["event_params"]["app_server_client"]["client_name"], + "codex-tui" + ); + assert_eq!(payload[0]["event_params"]["thread_source"], "user"); +} + #[test] fn subagent_thread_started_review_serializes_expected_shape() { let event = TrackEventRequest::ThreadInitialized(subagent_thread_started_event_request( @@ -1687,6 +1886,79 @@ async fn subagent_thread_started_inherits_parent_connection_for_new_thread() { ); } +#[tokio::test] +async fn subagent_tool_items_inherit_parent_connection_metadata() { + let mut reducer = AnalyticsReducer::default(); + let mut events = Vec::new(); + + ingest_tool_review_prerequisites(&mut reducer, &mut events).await; + reducer + .ingest( + AnalyticsFact::Custom(CustomAnalyticsFact::SubAgentThreadStarted( + SubAgentThreadStartedInput { + thread_id: "thread-subagent".to_string(), + parent_thread_id: Some("thread-1".to_string()), + product_client_id: "codex-tui".to_string(), + client_name: "codex-tui".to_string(), + client_version: "1.0.0".to_string(), + model: "gpt-5".to_string(), + ephemeral: false, + subagent_source: SubAgentSource::Review, + created_at: 128, + }, + )), + &mut events, + ) + .await; + events.clear(); + + reducer + .ingest( + AnalyticsFact::Notification(Box::new(ServerNotification::ItemStarted( + ItemStartedNotification { + thread_id: "thread-subagent".to_string(), + turn_id: "turn-subagent".to_string(), + started_at_ms: 1_000, + item: sample_command_execution_item( + CommandExecutionStatus::InProgress, + /*exit_code*/ None, + /*duration_ms*/ None, + ), + }, + ))), + &mut events, + ) + .await; + reducer + .ingest( + AnalyticsFact::Notification(Box::new(ServerNotification::ItemCompleted( + ItemCompletedNotification { + thread_id: "thread-subagent".to_string(), + turn_id: "turn-subagent".to_string(), + completed_at_ms: 1_042, + item: sample_command_execution_item( + CommandExecutionStatus::Completed, + Some(0), + Some(42), + ), + }, + ))), + &mut events, + ) + .await; + + let payload = serde_json::to_value(&events).expect("serialize events"); + assert_eq!(payload.as_array().expect("events array").len(), 1); + assert_eq!(payload[0]["event_type"], "codex_command_execution_event"); + assert_eq!(payload[0]["event_params"]["thread_source"], "subagent"); + assert_eq!(payload[0]["event_params"]["subagent_source"], "review"); + assert_eq!(payload[0]["event_params"]["parent_thread_id"], "thread-1"); + assert_eq!( + payload[0]["event_params"]["app_server_client"]["client_name"], + "codex-tui" + ); +} + #[test] fn plugin_used_event_serializes_expected_shape() { let tracking = TrackEventsContext { diff --git a/codex-rs/analytics/src/client.rs b/codex-rs/analytics/src/client.rs index d54c53ede9..6d6d446560 100644 --- a/codex-rs/analytics/src/client.rs +++ b/codex-rs/analytics/src/client.rs @@ -333,10 +333,6 @@ impl AnalyticsEventsClient { }); } - pub fn track_notification(&self, notification: ServerNotification) { - self.record_fact(AnalyticsFact::Notification(Box::new(notification))); - } - pub fn track_server_request(&self, connection_id: u64, request: ServerRequest) { self.record_fact(AnalyticsFact::ServerRequest { connection_id, @@ -349,6 +345,21 @@ impl AnalyticsEventsClient { response: Box::new(response), }); } + + pub fn track_notification(&self, notification: ServerNotification) { + if !matches!( + notification, + ServerNotification::TurnStarted(_) + | ServerNotification::TurnCompleted(_) + | ServerNotification::ItemStarted(_) + | ServerNotification::ItemCompleted(_) + | ServerNotification::ItemGuardianApprovalReviewStarted(_) + | ServerNotification::ItemGuardianApprovalReviewCompleted(_) + ) { + return; + } + self.record_fact(AnalyticsFact::Notification(Box::new(notification))); + } } async fn send_track_events( diff --git a/codex-rs/analytics/src/events.rs b/codex-rs/analytics/src/events.rs index 383cf6857e..2232c88d39 100644 --- a/codex-rs/analytics/src/events.rs +++ b/codex-rs/analytics/src/events.rs @@ -20,6 +20,7 @@ use crate::facts::TurnSteerResult; use crate::facts::TurnSubmissionType; use crate::now_unix_seconds; use codex_app_server_protocol::CodexErrorInfo; +use codex_app_server_protocol::CommandExecutionSource; use codex_login::default_client::originator; use codex_plugin::PluginTelemetryMetadata; use codex_protocol::approvals::NetworkApprovalProtocol; @@ -62,19 +63,12 @@ pub(crate) enum TrackEventRequest { Compaction(Box), TurnEvent(Box), TurnSteer(CodexTurnSteerEventRequest), - #[allow(dead_code)] CommandExecution(CodexCommandExecutionEventRequest), - #[allow(dead_code)] FileChange(CodexFileChangeEventRequest), - #[allow(dead_code)] McpToolCall(CodexMcpToolCallEventRequest), - #[allow(dead_code)] DynamicToolCall(CodexDynamicToolCallEventRequest), - #[allow(dead_code)] CollabAgentToolCall(CodexCollabAgentToolCallEventRequest), - #[allow(dead_code)] WebSearch(CodexWebSearchEventRequest), - #[allow(dead_code)] ImageGeneration(CodexImageGenerationEventRequest), PluginUsed(CodexPluginUsedEventRequest), PluginInstalled(CodexPluginEventRequest), @@ -454,7 +448,10 @@ pub(crate) struct CodexToolItemEventBase { pub(crate) tool_name: String, pub(crate) started_at_ms: u64, pub(crate) completed_at_ms: u64, + // Observed item lifecycle duration. This may undercount end-to-end execution + // for tools where app-server only sees part of the upstream flow. pub(crate) duration_ms: Option, + pub(crate) execution_duration_ms: Option, pub(crate) review_count: u64, pub(crate) guardian_review_count: u64, pub(crate) user_review_count: u64, @@ -465,17 +462,6 @@ pub(crate) struct CodexToolItemEventBase { pub(crate) requested_network_access: bool, } -#[allow(dead_code)] -#[derive(Clone, Copy, Debug, Serialize)] -#[serde(rename_all = "snake_case")] -pub(crate) enum CommandExecutionSource { - Agent, - UserShell, - UnifiedExecStartup, - UnifiedExecInteraction, -} - -#[allow(dead_code)] #[derive(Clone, Copy, Debug, Serialize)] #[serde(rename_all = "snake_case")] pub(crate) enum WebSearchActionKind { @@ -592,7 +578,6 @@ pub(crate) struct CodexWebSearchEventRequest { pub(crate) struct CodexImageGenerationEventParams { #[serde(flatten)] pub(crate) base: CodexToolItemEventBase, - pub(crate) image_generation_status: String, pub(crate) revised_prompt_present: bool, pub(crate) saved_path_present: bool, } diff --git a/codex-rs/analytics/src/lib.rs b/codex-rs/analytics/src/lib.rs index ed0f1036ca..2fb23199cb 100644 --- a/codex-rs/analytics/src/lib.rs +++ b/codex-rs/analytics/src/lib.rs @@ -51,3 +51,27 @@ pub fn now_unix_seconds() -> u64 { .unwrap_or_default() .as_secs() } + +pub fn now_unix_millis() -> u64 { + u64::try_from( + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_millis(), + ) + .unwrap_or(u64::MAX) +} + +pub(crate) fn serialize_enum_as_string(value: &T) -> Option { + serde_json::to_value(value) + .ok() + .and_then(|value| value.as_str().map(str::to_string)) +} + +pub(crate) fn usize_to_u64(value: usize) -> u64 { + u64::try_from(value).unwrap_or(u64::MAX) +} + +pub(crate) fn option_i64_to_u64(value: Option) -> Option { + value.and_then(|value| u64::try_from(value).ok()) +} diff --git a/codex-rs/analytics/src/reducer.rs b/codex-rs/analytics/src/reducer.rs index 772bb6c624..81530444de 100644 --- a/codex-rs/analytics/src/reducer.rs +++ b/codex-rs/analytics/src/reducer.rs @@ -2,15 +2,30 @@ use crate::events::AppServerRpcTransport; use crate::events::CodexAppMentionedEventRequest; use crate::events::CodexAppServerClientMetadata; use crate::events::CodexAppUsedEventRequest; +use crate::events::CodexCollabAgentToolCallEventParams; +use crate::events::CodexCollabAgentToolCallEventRequest; +use crate::events::CodexCommandExecutionEventParams; +use crate::events::CodexCommandExecutionEventRequest; use crate::events::CodexCompactionEventRequest; +use crate::events::CodexDynamicToolCallEventParams; +use crate::events::CodexDynamicToolCallEventRequest; +use crate::events::CodexFileChangeEventParams; +use crate::events::CodexFileChangeEventRequest; use crate::events::CodexHookRunEventRequest; +use crate::events::CodexImageGenerationEventParams; +use crate::events::CodexImageGenerationEventRequest; +use crate::events::CodexMcpToolCallEventParams; +use crate::events::CodexMcpToolCallEventRequest; use crate::events::CodexPluginEventRequest; use crate::events::CodexPluginUsedEventRequest; use crate::events::CodexRuntimeMetadata; +use crate::events::CodexToolItemEventBase; use crate::events::CodexTurnEventParams; use crate::events::CodexTurnEventRequest; use crate::events::CodexTurnSteerEventParams; use crate::events::CodexTurnSteerEventRequest; +use crate::events::CodexWebSearchEventParams; +use crate::events::CodexWebSearchEventRequest; use crate::events::GuardianReviewEventParams; use crate::events::GuardianReviewEventPayload; use crate::events::GuardianReviewEventRequest; @@ -18,7 +33,11 @@ use crate::events::SkillInvocationEventParams; use crate::events::SkillInvocationEventRequest; use crate::events::ThreadInitializedEvent; use crate::events::ThreadInitializedEventParams; +use crate::events::ToolItemFailureKind; +use crate::events::ToolItemFinalApprovalOutcome; +use crate::events::ToolItemTerminalStatus; use crate::events::TrackEventRequest; +use crate::events::WebSearchActionKind; use crate::events::codex_app_metadata; use crate::events::codex_compaction_event_params; use crate::events::codex_hook_run_metadata; @@ -47,14 +66,30 @@ use crate::facts::TurnSteerRejectionReason; use crate::facts::TurnSteerResult; use crate::facts::TurnTokenUsageFact; use crate::now_unix_seconds; +use crate::option_i64_to_u64; +use crate::serialize_enum_as_string; +use crate::usize_to_u64; use codex_app_server_protocol::ClientRequest; use codex_app_server_protocol::ClientResponse; use codex_app_server_protocol::CodexErrorInfo; +use codex_app_server_protocol::CollabAgentStatus; +use codex_app_server_protocol::CollabAgentTool; +use codex_app_server_protocol::CollabAgentToolCallStatus; +use codex_app_server_protocol::CommandAction; +use codex_app_server_protocol::CommandExecutionSource; +use codex_app_server_protocol::CommandExecutionStatus; +use codex_app_server_protocol::DynamicToolCallOutputContentItem; +use codex_app_server_protocol::DynamicToolCallStatus; use codex_app_server_protocol::InitializeParams; +use codex_app_server_protocol::McpToolCallStatus; +use codex_app_server_protocol::PatchApplyStatus; +use codex_app_server_protocol::PatchChangeKind; use codex_app_server_protocol::RequestId; use codex_app_server_protocol::ServerNotification; +use codex_app_server_protocol::ThreadItem; use codex_app_server_protocol::TurnSteerResponse; use codex_app_server_protocol::UserInput; +use codex_app_server_protocol::WebSearchAction; use codex_git_utils::collect_git_info; use codex_git_utils::get_git_repo_root; use codex_login::default_client::originator; @@ -76,6 +111,7 @@ pub(crate) struct AnalyticsReducer { turns: HashMap, connections: HashMap, threads: HashMap, + tool_items_started_at_ms: HashMap, } struct ConnectionState { @@ -119,6 +155,19 @@ impl<'a> AnalyticsDropSite<'a> { } } + fn tool_item( + notification: &'a codex_app_server_protocol::ItemCompletedNotification, + item_id: &'a str, + ) -> Self { + Self { + event_name: "tool item", + thread_id: ¬ification.thread_id, + turn_id: Some(¬ification.turn_id), + review_id: None, + item_id: Some(item_id), + } + } + fn turn_steer(thread_id: &'a str) -> Self { Self { event_name: "turn steer", @@ -218,6 +267,13 @@ struct TurnState { steer_count: usize, } +#[derive(Hash, Eq, PartialEq)] +struct ToolItemKey { + thread_id: String, + turn_id: String, + item_id: String, +} + impl AnalyticsReducer { pub(crate) async fn ingest(&mut self, input: AnalyticsFact, out: &mut Vec) { match input { @@ -690,6 +746,62 @@ impl AnalyticsReducer { out: &mut Vec, ) { match notification { + ServerNotification::ItemStarted(notification) => { + let Some(item_id) = tracked_tool_item_id(¬ification.item) else { + return; + }; + let Some(started_at_ms) = option_i64_to_u64(Some(notification.started_at_ms)) + else { + return; + }; + self.tool_items_started_at_ms.insert( + ToolItemKey { + thread_id: notification.thread_id, + turn_id: notification.turn_id, + item_id: item_id.to_string(), + }, + started_at_ms, + ); + } + ServerNotification::ItemCompleted(notification) => { + let Some(item_id) = tracked_tool_item_id(¬ification.item) else { + return; + }; + let key = ToolItemKey { + thread_id: notification.thread_id.clone(), + turn_id: notification.turn_id.clone(), + item_id: item_id.to_string(), + }; + let Some(started_at_ms) = self.tool_items_started_at_ms.remove(&key) else { + tracing::warn!( + thread_id = %notification.thread_id, + turn_id = %notification.turn_id, + item_id, + "dropping tool item analytics event: missing item started notification" + ); + return; + }; + let Some(completed_at_ms) = option_i64_to_u64(Some(notification.completed_at_ms)) + else { + return; + }; + let Some((connection_state, thread_metadata)) = self + .thread_context_or_warn(AnalyticsDropSite::tool_item(¬ification, item_id)) + else { + return; + }; + if let Some(event) = tool_item_event( + ¬ification.thread_id, + ¬ification.turn_id, + ¬ification.item, + started_at_ms, + completed_at_ms, + connection_state, + thread_metadata, + ) { + out.push(event); + } + } ServerNotification::TurnStarted(notification) => { let turn_state = self.turns.entry(notification.turn.id).or_insert(TurnState { connection_id: None, @@ -779,7 +891,7 @@ impl AnalyticsReducer { ephemeral: thread.ephemeral, thread_source: thread_metadata.thread_source, initialization_mode, - subagent_source: thread_metadata.subagent_source, + subagent_source: thread_metadata.subagent_source.clone(), parent_thread_id: thread_metadata.parent_thread_id, created_at: u64::try_from(thread.created_at).unwrap_or_default(), }, @@ -983,6 +1095,552 @@ fn warn_missing_analytics_context( ); } +fn tracked_tool_item_id(item: &ThreadItem) -> Option<&str> { + match item { + ThreadItem::CommandExecution { id, .. } + | ThreadItem::FileChange { id, .. } + | ThreadItem::McpToolCall { id, .. } + | ThreadItem::DynamicToolCall { id, .. } + | ThreadItem::CollabAgentToolCall { id, .. } + | ThreadItem::WebSearch { id, .. } + | ThreadItem::ImageGeneration { id, .. } => Some(id), + ThreadItem::UserMessage { .. } + | ThreadItem::HookPrompt { .. } + | ThreadItem::AgentMessage { .. } + | ThreadItem::Plan { .. } + | ThreadItem::Reasoning { .. } + | ThreadItem::ImageView { .. } + | ThreadItem::EnteredReviewMode { .. } + | ThreadItem::ExitedReviewMode { .. } + | ThreadItem::ContextCompaction { .. } => None, + } +} + +fn tool_item_event( + thread_id: &str, + turn_id: &str, + item: &ThreadItem, + started_at_ms: u64, + completed_at_ms: u64, + connection_state: &ConnectionState, + thread_metadata: &ThreadMetadataState, +) -> Option { + let context = ToolItemContext { + started_at_ms, + completed_at_ms, + connection_state, + thread_metadata, + }; + match item { + ThreadItem::CommandExecution { + id, + source, + status, + command_actions, + exit_code, + duration_ms, + .. + } => { + let (terminal_status, failure_kind) = command_execution_outcome(status)?; + let action_counts = command_action_counts(command_actions); + let base = tool_item_base( + thread_id, + turn_id, + id.clone(), + command_execution_tool_name(*source).to_string(), + ToolItemOutcome { + terminal_status, + failure_kind, + execution_duration_ms: option_i64_to_u64(*duration_ms), + }, + context, + ); + Some(TrackEventRequest::CommandExecution( + CodexCommandExecutionEventRequest { + event_type: "codex_command_execution_event", + event_params: CodexCommandExecutionEventParams { + base, + command_execution_source: *source, + exit_code: *exit_code, + command_total_action_count: action_counts.total, + command_read_action_count: action_counts.read, + command_list_files_action_count: action_counts.list_files, + command_search_action_count: action_counts.search, + command_unknown_action_count: action_counts.unknown, + }, + }, + )) + } + ThreadItem::FileChange { + id, + changes, + status, + } => { + let (terminal_status, failure_kind) = patch_apply_outcome(status)?; + let counts = file_change_counts(changes); + let base = tool_item_base( + thread_id, + turn_id, + id.clone(), + "apply_patch".to_string(), + ToolItemOutcome { + terminal_status, + failure_kind, + execution_duration_ms: None, + }, + context, + ); + Some(TrackEventRequest::FileChange(CodexFileChangeEventRequest { + event_type: "codex_file_change_event", + event_params: CodexFileChangeEventParams { + base, + file_change_count: usize_to_u64(changes.len()), + file_add_count: counts.add, + file_update_count: counts.update, + file_delete_count: counts.delete, + file_move_count: counts.move_, + }, + })) + } + ThreadItem::McpToolCall { + id, + server, + tool, + status, + error, + duration_ms, + .. + } => { + let (terminal_status, failure_kind) = mcp_tool_call_outcome(status)?; + let base = tool_item_base( + thread_id, + turn_id, + id.clone(), + tool.clone(), + ToolItemOutcome { + terminal_status, + failure_kind, + execution_duration_ms: option_i64_to_u64(*duration_ms), + }, + context, + ); + Some(TrackEventRequest::McpToolCall( + CodexMcpToolCallEventRequest { + event_type: "codex_mcp_tool_call_event", + event_params: CodexMcpToolCallEventParams { + base, + mcp_server_name: server.clone(), + mcp_tool_name: tool.clone(), + mcp_error_present: error.is_some(), + }, + }, + )) + } + ThreadItem::DynamicToolCall { + id, + tool, + status, + content_items, + success, + duration_ms, + .. + } => { + let (terminal_status, failure_kind) = dynamic_tool_call_outcome(status)?; + let counts = content_items + .as_ref() + .map(|items| dynamic_content_counts(items)); + let base = tool_item_base( + thread_id, + turn_id, + id.clone(), + tool.clone(), + ToolItemOutcome { + terminal_status, + failure_kind, + execution_duration_ms: option_i64_to_u64(*duration_ms), + }, + context, + ); + Some(TrackEventRequest::DynamicToolCall( + CodexDynamicToolCallEventRequest { + event_type: "codex_dynamic_tool_call_event", + event_params: CodexDynamicToolCallEventParams { + base, + dynamic_tool_name: tool.clone(), + success: *success, + output_content_item_count: counts.map(|counts| counts.total), + output_text_item_count: counts.map(|counts| counts.text), + output_image_item_count: counts.map(|counts| counts.image), + }, + }, + )) + } + ThreadItem::CollabAgentToolCall { + id, + tool, + status, + sender_thread_id, + receiver_thread_ids, + model, + reasoning_effort, + agents_states, + .. + } => { + let (terminal_status, failure_kind) = collab_tool_call_outcome(status)?; + let base = tool_item_base( + thread_id, + turn_id, + id.clone(), + collab_agent_tool_name(tool).to_string(), + ToolItemOutcome { + terminal_status, + failure_kind, + execution_duration_ms: None, + }, + context, + ); + Some(TrackEventRequest::CollabAgentToolCall( + CodexCollabAgentToolCallEventRequest { + event_type: "codex_collab_agent_tool_call_event", + event_params: CodexCollabAgentToolCallEventParams { + base, + sender_thread_id: sender_thread_id.clone(), + receiver_thread_count: usize_to_u64(receiver_thread_ids.len()), + receiver_thread_ids: Some(receiver_thread_ids.clone()), + requested_model: model.clone(), + requested_reasoning_effort: reasoning_effort + .as_ref() + .and_then(serialize_enum_as_string), + agent_state_count: Some(usize_to_u64(agents_states.len())), + completed_agent_count: Some(usize_to_u64( + agents_states + .values() + .filter(|state| state.status == CollabAgentStatus::Completed) + .count(), + )), + failed_agent_count: Some(usize_to_u64( + agents_states + .values() + .filter(|state| { + matches!( + state.status, + CollabAgentStatus::Errored + | CollabAgentStatus::Shutdown + | CollabAgentStatus::NotFound + ) + }) + .count(), + )), + }, + }, + )) + } + ThreadItem::WebSearch { id, query, action } => { + let base = tool_item_base( + thread_id, + turn_id, + id.clone(), + "web_search".to_string(), + ToolItemOutcome { + terminal_status: ToolItemTerminalStatus::Completed, + failure_kind: None, + execution_duration_ms: None, + }, + context, + ); + Some(TrackEventRequest::WebSearch(CodexWebSearchEventRequest { + event_type: "codex_web_search_event", + event_params: CodexWebSearchEventParams { + base, + web_search_action: action.as_ref().map(web_search_action_kind), + query_present: !query.trim().is_empty(), + query_count: web_search_query_count(query, action.as_ref()), + }, + })) + } + ThreadItem::ImageGeneration { + id, + status, + revised_prompt, + saved_path, + .. + } => { + let (terminal_status, failure_kind) = image_generation_outcome(status.as_str()); + let base = tool_item_base( + thread_id, + turn_id, + id.clone(), + "image_generation".to_string(), + ToolItemOutcome { + terminal_status, + failure_kind, + execution_duration_ms: None, + }, + context, + ); + Some(TrackEventRequest::ImageGeneration( + CodexImageGenerationEventRequest { + event_type: "codex_image_generation_event", + event_params: CodexImageGenerationEventParams { + base, + revised_prompt_present: revised_prompt.is_some(), + saved_path_present: saved_path.is_some(), + }, + }, + )) + } + _ => None, + } +} + +struct ToolItemOutcome { + terminal_status: ToolItemTerminalStatus, + failure_kind: Option, + execution_duration_ms: Option, +} + +#[derive(Default)] +struct CommandActionCounts { + total: u64, + read: u64, + list_files: u64, + search: u64, + unknown: u64, +} + +fn command_action_counts(command_actions: &[CommandAction]) -> CommandActionCounts { + let mut counts = CommandActionCounts { + total: usize_to_u64(command_actions.len()), + ..Default::default() + }; + for action in command_actions { + match action { + CommandAction::Read { .. } => counts.read += 1, + CommandAction::ListFiles { .. } => counts.list_files += 1, + CommandAction::Search { .. } => counts.search += 1, + CommandAction::Unknown { .. } => counts.unknown += 1, + } + } + counts +} + +#[derive(Clone, Copy)] +struct ToolItemContext<'a> { + started_at_ms: u64, + completed_at_ms: u64, + connection_state: &'a ConnectionState, + thread_metadata: &'a ThreadMetadataState, +} + +fn tool_item_base( + thread_id: &str, + turn_id: &str, + item_id: String, + tool_name: String, + outcome: ToolItemOutcome, + context: ToolItemContext<'_>, +) -> CodexToolItemEventBase { + let thread_metadata = context.thread_metadata; + CodexToolItemEventBase { + thread_id: thread_id.to_string(), + turn_id: turn_id.to_string(), + item_id, + app_server_client: context.connection_state.app_server_client.clone(), + runtime: context.connection_state.runtime.clone(), + thread_source: thread_metadata.thread_source.map(ThreadSource::as_str), + subagent_source: thread_metadata.subagent_source.clone(), + parent_thread_id: thread_metadata.parent_thread_id.clone(), + tool_name, + started_at_ms: context.started_at_ms, + completed_at_ms: context.completed_at_ms, + // duration_ms reflects item lifecycle observed by app-server. For web + // search and image generation in particular, that can be narrower than + // full upstream execution time. + duration_ms: observed_duration_ms(context.started_at_ms, context.completed_at_ms), + execution_duration_ms: outcome.execution_duration_ms, + review_count: 0, + guardian_review_count: 0, + user_review_count: 0, + final_approval_outcome: ToolItemFinalApprovalOutcome::Unknown, + terminal_status: outcome.terminal_status, + failure_kind: outcome.failure_kind, + requested_additional_permissions: false, + requested_network_access: false, + } +} + +fn observed_duration_ms(started_at_ms: u64, completed_at_ms: u64) -> Option { + completed_at_ms.checked_sub(started_at_ms) +} + +fn command_execution_tool_name(source: CommandExecutionSource) -> &'static str { + match source { + CommandExecutionSource::UnifiedExecStartup + | CommandExecutionSource::UnifiedExecInteraction => "unified_exec", + CommandExecutionSource::UserShell => "user_shell", + CommandExecutionSource::Agent => "shell", + } +} + +fn command_execution_outcome( + status: &CommandExecutionStatus, +) -> Option<(ToolItemTerminalStatus, Option)> { + match status { + CommandExecutionStatus::InProgress => None, + CommandExecutionStatus::Completed => Some((ToolItemTerminalStatus::Completed, None)), + CommandExecutionStatus::Failed => Some(( + ToolItemTerminalStatus::Failed, + Some(ToolItemFailureKind::ToolError), + )), + CommandExecutionStatus::Declined => Some(( + ToolItemTerminalStatus::Rejected, + Some(ToolItemFailureKind::ApprovalDenied), + )), + } +} + +fn patch_apply_outcome( + status: &PatchApplyStatus, +) -> Option<(ToolItemTerminalStatus, Option)> { + match status { + PatchApplyStatus::InProgress => None, + PatchApplyStatus::Completed => Some((ToolItemTerminalStatus::Completed, None)), + PatchApplyStatus::Failed => Some(( + ToolItemTerminalStatus::Failed, + Some(ToolItemFailureKind::ToolError), + )), + PatchApplyStatus::Declined => Some(( + ToolItemTerminalStatus::Rejected, + Some(ToolItemFailureKind::ApprovalDenied), + )), + } +} + +fn mcp_tool_call_outcome( + status: &McpToolCallStatus, +) -> Option<(ToolItemTerminalStatus, Option)> { + match status { + McpToolCallStatus::InProgress => None, + McpToolCallStatus::Completed => Some((ToolItemTerminalStatus::Completed, None)), + McpToolCallStatus::Failed => Some(( + ToolItemTerminalStatus::Failed, + Some(ToolItemFailureKind::ToolError), + )), + } +} + +fn dynamic_tool_call_outcome( + status: &DynamicToolCallStatus, +) -> Option<(ToolItemTerminalStatus, Option)> { + match status { + DynamicToolCallStatus::InProgress => None, + DynamicToolCallStatus::Completed => Some((ToolItemTerminalStatus::Completed, None)), + DynamicToolCallStatus::Failed => Some(( + ToolItemTerminalStatus::Failed, + Some(ToolItemFailureKind::ToolError), + )), + } +} + +fn collab_tool_call_outcome( + status: &CollabAgentToolCallStatus, +) -> Option<(ToolItemTerminalStatus, Option)> { + match status { + CollabAgentToolCallStatus::InProgress => None, + CollabAgentToolCallStatus::Completed => Some((ToolItemTerminalStatus::Completed, None)), + CollabAgentToolCallStatus::Failed => Some(( + ToolItemTerminalStatus::Failed, + Some(ToolItemFailureKind::ToolError), + )), + } +} + +fn image_generation_outcome(status: &str) -> (ToolItemTerminalStatus, Option) { + match status { + "failed" | "error" => ( + ToolItemTerminalStatus::Failed, + Some(ToolItemFailureKind::ToolError), + ), + _ => (ToolItemTerminalStatus::Completed, None), + } +} + +fn collab_agent_tool_name(tool: &CollabAgentTool) -> &'static str { + match tool { + CollabAgentTool::SpawnAgent => "spawn_agent", + CollabAgentTool::SendInput => "send_input", + CollabAgentTool::ResumeAgent => "resume_agent", + CollabAgentTool::Wait => "wait_agent", + CollabAgentTool::CloseAgent => "close_agent", + } +} + +#[derive(Default)] +struct FileChangeCounts { + add: u64, + update: u64, + delete: u64, + move_: u64, +} + +fn file_change_counts(changes: &[codex_app_server_protocol::FileUpdateChange]) -> FileChangeCounts { + let mut counts = FileChangeCounts::default(); + for change in changes { + match &change.kind { + PatchChangeKind::Add => counts.add += 1, + PatchChangeKind::Delete => counts.delete += 1, + PatchChangeKind::Update { move_path: Some(_) } => counts.move_ += 1, + PatchChangeKind::Update { move_path: None } => counts.update += 1, + } + } + counts +} + +#[derive(Clone, Copy)] +struct DynamicContentCounts { + total: u64, + text: u64, + image: u64, +} + +fn dynamic_content_counts(items: &[DynamicToolCallOutputContentItem]) -> DynamicContentCounts { + let mut text = 0; + let mut image = 0; + for item in items { + match item { + DynamicToolCallOutputContentItem::InputText { .. } => text += 1, + DynamicToolCallOutputContentItem::InputImage { .. } => image += 1, + } + } + DynamicContentCounts { + total: usize_to_u64(items.len()), + text, + image, + } +} + +fn web_search_action_kind(action: &WebSearchAction) -> WebSearchActionKind { + match action { + WebSearchAction::Search { .. } => WebSearchActionKind::Search, + WebSearchAction::OpenPage { .. } => WebSearchActionKind::OpenPage, + WebSearchAction::FindInPage { .. } => WebSearchActionKind::FindInPage, + WebSearchAction::Other => WebSearchActionKind::Other, + } +} + +fn web_search_query_count(query: &str, action: Option<&WebSearchAction>) -> Option { + match action { + Some(WebSearchAction::Search { query, queries }) => queries + .as_ref() + .map(|queries| usize_to_u64(queries.len())) + .or_else(|| query.as_ref().map(|_| 1)), + Some(WebSearchAction::OpenPage { .. }) + | Some(WebSearchAction::FindInPage { .. }) + | Some(WebSearchAction::Other) => None, + None => (!query.trim().is_empty()).then_some(1), + } +} + fn codex_turn_event_params( app_server_client: CodexAppServerClientMetadata, runtime: CodexRuntimeMetadata, diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs index e2d8fd587d..d9ff9d1551 100644 --- a/codex-rs/app-server/src/bespoke_event_handling.rs +++ b/codex-rs/app-server/src/bespoke_event_handling.rs @@ -10,7 +10,6 @@ use crate::thread_state::TurnSummary; use crate::thread_state::resolve_server_request_on_thread_listener; use crate::thread_status::ThreadWatchActiveGuard; use crate::thread_status::ThreadWatchManager; -use codex_analytics::AnalyticsEventsClient; use codex_app_server_protocol::AccountRateLimitsUpdatedNotification; use codex_app_server_protocol::AdditionalPermissionProfile as V2AdditionalPermissionProfile; use codex_app_server_protocol::CodexErrorInfo as V2CodexErrorInfo; @@ -137,7 +136,6 @@ pub(crate) async fn apply_bespoke_event_handling( conversation_id: ThreadId, conversation: Arc, thread_manager: Arc, - analytics_events_client: Option, outgoing: ThreadScopedOutgoingMessageSender, thread_state: Arc>, thread_watch_manager: ThreadWatchManager, @@ -175,10 +173,6 @@ pub(crate) async fn apply_bespoke_event_handling( thread_id: conversation_id.to_string(), turn, }; - if let Some(analytics_events_client) = analytics_events_client.as_ref() { - analytics_events_client - .track_notification(ServerNotification::TurnStarted(notification.clone())); - } outgoing .send_server_notification(ServerNotification::TurnStarted(notification)) .await; @@ -195,7 +189,6 @@ pub(crate) async fn apply_bespoke_event_handling( conversation_id, event_turn_id, turn_complete_event, - analytics_events_client.as_ref(), &outgoing, &thread_state, ) @@ -237,10 +230,6 @@ pub(crate) async fn apply_bespoke_event_handling( thread_id: Some(conversation_id.to_string()), message: warning_event.message, }; - if let Some(analytics_events_client) = analytics_events_client.as_ref() { - analytics_events_client - .track_notification(ServerNotification::Warning(notification.clone())); - } outgoing .send_server_notification(ServerNotification::Warning(notification)) .await; @@ -250,10 +239,6 @@ pub(crate) async fn apply_bespoke_event_handling( thread_id: conversation_id.to_string(), message: warning_event.message, }; - if let Some(analytics_events_client) = analytics_events_client.as_ref() { - analytics_events_client - .track_notification(ServerNotification::GuardianWarning(notification.clone())); - } outgoing .send_server_notification(ServerNotification::GuardianWarning(notification)) .await; @@ -1138,7 +1123,6 @@ pub(crate) async fn apply_bespoke_event_handling( conversation_id, event_turn_id, turn_aborted_event, - analytics_events_client.as_ref(), &outgoing, &thread_state, ) @@ -1291,7 +1275,6 @@ async fn emit_turn_completed_with_status( conversation_id: ThreadId, event_turn_id: String, turn_completion_metadata: TurnCompletionMetadata, - analytics_events_client: Option<&AnalyticsEventsClient>, outgoing: &ThreadScopedOutgoingMessageSender, ) { let notification = TurnCompletedNotification { @@ -1307,10 +1290,6 @@ async fn emit_turn_completed_with_status( duration_ms: turn_completion_metadata.duration_ms, }, }; - if let Some(analytics_events_client) = analytics_events_client { - analytics_events_client - .track_notification(ServerNotification::TurnCompleted(notification.clone())); - } outgoing .send_server_notification(ServerNotification::TurnCompleted(notification)) .await; @@ -1474,7 +1453,6 @@ async fn handle_turn_complete( conversation_id: ThreadId, event_turn_id: String, turn_complete_event: TurnCompleteEvent, - analytics_events_client: Option<&AnalyticsEventsClient>, outgoing: &ThreadScopedOutgoingMessageSender, thread_state: &Arc>, ) { @@ -1495,7 +1473,6 @@ async fn handle_turn_complete( completed_at: turn_complete_event.completed_at, duration_ms: turn_complete_event.duration_ms, }, - analytics_events_client, outgoing, ) .await; @@ -1505,7 +1482,6 @@ async fn handle_turn_interrupted( conversation_id: ThreadId, event_turn_id: String, turn_aborted_event: TurnAbortedEvent, - analytics_events_client: Option<&AnalyticsEventsClient>, outgoing: &ThreadScopedOutgoingMessageSender, thread_state: &Arc>, ) { @@ -1521,7 +1497,6 @@ async fn handle_turn_interrupted( completed_at: turn_aborted_event.completed_at, duration_ms: turn_aborted_event.duration_ms, }, - analytics_events_client, outgoing, ) .await; @@ -2095,7 +2070,6 @@ mod tests { use codex_app_server_protocol::GuardianApprovalReviewStatus; use codex_app_server_protocol::JSONRPCErrorError; use codex_app_server_protocol::TurnPlanStepStatus; - use codex_login::AuthManager; use codex_login::CodexAuth; use codex_protocol::items::HookPromptFragment; use codex_protocol::items::build_hook_prompt_message; @@ -2301,7 +2275,6 @@ mod tests { outgoing: ThreadScopedOutgoingMessageSender, thread_state: Arc>, thread_watch_manager: ThreadWatchManager, - analytics_events_client: AnalyticsEventsClient, } impl GuardianAssessmentTestContext { @@ -2315,7 +2288,6 @@ mod tests { self.conversation_id, self.conversation.clone(), self.thread_manager.clone(), - Some(self.analytics_events_client.clone()), self.outgoing.clone(), self.thread_state.clone(), self.thread_watch_manager.clone(), @@ -2649,13 +2621,6 @@ mod tests { outgoing: outgoing.clone(), thread_state: thread_state.clone(), thread_watch_manager: thread_watch_manager.clone(), - analytics_events_client: AnalyticsEventsClient::new( - AuthManager::from_auth_for_testing( - CodexAuth::create_dummy_chatgpt_auth_for_testing(), - ), - "http://localhost".to_string(), - Some(false), - ), }; guardian_context @@ -3263,7 +3228,6 @@ mod tests { conversation_id, conversation, thread_manager, - /*analytics_events_client*/ None, outgoing, thread_state, thread_watch_manager, @@ -3320,7 +3284,6 @@ mod tests { conversation_id, event_turn_id.clone(), turn_complete_event(&event_turn_id), - /*analytics_events_client*/ None, &outgoing, &thread_state, ) @@ -3374,7 +3337,6 @@ mod tests { conversation_id, event_turn_id.clone(), turn_aborted_event(&event_turn_id), - /*analytics_events_client*/ None, &outgoing, &thread_state, ) @@ -3425,7 +3387,6 @@ mod tests { conversation_id, event_turn_id.clone(), turn_complete_event(&event_turn_id), - /*analytics_events_client*/ None, &outgoing, &thread_state, ) @@ -3660,7 +3621,6 @@ mod tests { conversation_a, a_turn1.clone(), turn_complete_event(&a_turn1), - /*analytics_events_client*/ None, &outgoing, &thread_state, ) @@ -3682,7 +3642,6 @@ mod tests { conversation_b, b_turn1.clone(), turn_complete_event(&b_turn1), - /*analytics_events_client*/ None, &outgoing, &thread_state, ) @@ -3694,7 +3653,6 @@ mod tests { conversation_a, a_turn2.clone(), turn_complete_event(&a_turn2), - /*analytics_events_client*/ None, &outgoing, &thread_state, ) diff --git a/codex-rs/app-server/src/message_processor.rs b/codex-rs/app-server/src/message_processor.rs index a3c3877fdc..1ac635685f 100644 --- a/codex-rs/app-server/src/message_processor.rs +++ b/codex-rs/app-server/src/message_processor.rs @@ -391,7 +391,6 @@ impl MessageProcessor { auth_manager.clone(), Arc::clone(&thread_manager), outgoing.clone(), - analytics_events_client.clone(), arg0_paths.clone(), Arc::clone(&config), config_manager.clone(), diff --git a/codex-rs/app-server/src/outgoing_message.rs b/codex-rs/app-server/src/outgoing_message.rs index 2807d2532e..a7420f8c78 100644 --- a/codex-rs/app-server/src/outgoing_message.rs +++ b/codex-rs/app-server/src/outgoing_message.rs @@ -140,6 +140,9 @@ impl ThreadScopedOutgoingMessageSender { } pub(crate) async fn send_server_notification(&self, notification: ServerNotification) { + self.outgoing + .analytics_events_client + .track_notification(notification.clone()); if self.connection_ids.is_empty() { return; } @@ -526,7 +529,7 @@ impl OutgoingMessageSender { targeted_connections = connection_ids.len(), "app-server event: {notification}" ); - let outgoing_message = OutgoingMessage::AppServerNotification(notification); + let outgoing_message = OutgoingMessage::AppServerNotification(notification.clone()); if connection_ids.is_empty() { if let Err(err) = self .sender @@ -560,7 +563,7 @@ impl OutgoingMessageSender { notification: ServerNotification, ) { tracing::trace!("app-server event: {notification}"); - let outgoing_message = OutgoingMessage::AppServerNotification(notification); + let outgoing_message = OutgoingMessage::AppServerNotification(notification.clone()); let (write_complete_tx, write_complete_rx) = oneshot::channel(); if let Err(err) = self .sender diff --git a/codex-rs/app-server/src/request_processors/thread_lifecycle.rs b/codex-rs/app-server/src/request_processors/thread_lifecycle.rs index 81e09c7d5c..ef44a2b178 100644 --- a/codex-rs/app-server/src/request_processors/thread_lifecycle.rs +++ b/codex-rs/app-server/src/request_processors/thread_lifecycle.rs @@ -8,7 +8,6 @@ pub(super) struct ListenerTaskContext { pub(super) thread_state_manager: ThreadStateManager, pub(super) outgoing: Arc, pub(super) pending_thread_unloads: Arc>>, - pub(super) analytics_events_client: AnalyticsEventsClient, pub(super) thread_watch_manager: ThreadWatchManager, pub(super) thread_list_state_permit: Arc, pub(super) fallback_model_provider: String, @@ -239,7 +238,6 @@ pub(super) async fn ensure_listener_task_running( thread_manager, thread_state_manager, pending_thread_unloads, - analytics_events_client, thread_watch_manager, thread_list_state_permit, fallback_model_provider, @@ -315,7 +313,6 @@ pub(super) async fn ensure_listener_task_running( conversation_id, conversation.clone(), thread_manager.clone(), - Some(analytics_events_client.clone()), thread_outgoing, thread_state.clone(), thread_watch_manager.clone(), diff --git a/codex-rs/app-server/src/request_processors/thread_processor.rs b/codex-rs/app-server/src/request_processors/thread_processor.rs index 783e53ccb0..4042197a76 100644 --- a/codex-rs/app-server/src/request_processors/thread_processor.rs +++ b/codex-rs/app-server/src/request_processors/thread_processor.rs @@ -305,7 +305,6 @@ pub(crate) struct ThreadRequestProcessor { pub(super) auth_manager: Arc, pub(super) thread_manager: Arc, pub(super) outgoing: Arc, - pub(super) analytics_events_client: AnalyticsEventsClient, pub(super) arg0_paths: Arg0DispatchPaths, pub(super) config: Arc, pub(super) config_manager: ConfigManager, @@ -325,7 +324,6 @@ impl ThreadRequestProcessor { auth_manager: Arc, thread_manager: Arc, outgoing: Arc, - analytics_events_client: AnalyticsEventsClient, arg0_paths: Arg0DispatchPaths, config: Arc, config_manager: ConfigManager, @@ -341,7 +339,6 @@ impl ThreadRequestProcessor { auth_manager, thread_manager, outgoing, - analytics_events_client, arg0_paths, config, config_manager, @@ -741,7 +738,6 @@ impl ThreadRequestProcessor { thread_state_manager: self.thread_state_manager.clone(), outgoing: Arc::clone(&self.outgoing), pending_thread_unloads: Arc::clone(&self.pending_thread_unloads), - analytics_events_client: self.analytics_events_client.clone(), thread_watch_manager: self.thread_watch_manager.clone(), thread_list_state_permit: self.thread_list_state_permit.clone(), fallback_model_provider: self.config.model_provider_id.clone(), @@ -839,7 +835,6 @@ impl ThreadRequestProcessor { thread_state_manager: self.thread_state_manager.clone(), outgoing: Arc::clone(&self.outgoing), pending_thread_unloads: Arc::clone(&self.pending_thread_unloads), - analytics_events_client: self.analytics_events_client.clone(), thread_watch_manager: self.thread_watch_manager.clone(), thread_list_state_permit: self.thread_list_state_permit.clone(), fallback_model_provider: self.config.model_provider_id.clone(), diff --git a/codex-rs/app-server/src/request_processors/turn_processor.rs b/codex-rs/app-server/src/request_processors/turn_processor.rs index b207fde940..bdc5847b0d 100644 --- a/codex-rs/app-server/src/request_processors/turn_processor.rs +++ b/codex-rs/app-server/src/request_processors/turn_processor.rs @@ -1083,7 +1083,6 @@ impl TurnRequestProcessor { thread_state_manager: self.thread_state_manager.clone(), outgoing: Arc::clone(&self.outgoing), pending_thread_unloads: Arc::clone(&self.pending_thread_unloads), - analytics_events_client: self.analytics_events_client.clone(), thread_watch_manager: self.thread_watch_manager.clone(), thread_list_state_permit: self.thread_list_state_permit.clone(), fallback_model_provider: self.config.model_provider_id.clone(), From 123e78b97b74d7288be553973361c34dac6830a6 Mon Sep 17 00:00:00 2001 From: iceweasel-oai Date: Wed, 6 May 2026 14:08:45 -0700 Subject: [PATCH 8/9] [codex] Fix Windows sandbox git safe.directory for worktrees (#21409) ## Why Windows sandboxed commands run as a sandbox user, while workspace repositories are usually owned by the real user. The sandbox compensates by injecting a temporary Git `safe.directory` entry into the child environment. That injection was still broken for linked worktrees because the helper followed the `.git` file's `gitdir:` pointer and injected the internal `.git/worktrees/...` location. Git's dubious-ownership check expects the worktree root instead, so sandboxed Git commands still failed in worktree-based Codex checkouts. ## What changed - Treat any `.git` marker, directory or file, as the worktree root for `safe.directory` injection. - Keep the safe-directory logic in `windows-sandbox-rs/src/sandbox_utils.rs` and have the one-shot elevated path reuse it. - Add regression coverage for both normal `.git` directories and gitfile-based worktrees. ## Validation - `cargo test -p codex-windows-sandbox sandbox_utils::tests` - `cargo test -p codex-windows-sandbox` built and ran; the new `sandbox_utils` tests passed, while two pre-existing legacy sandbox tests failed locally with `Access is denied`: `session::tests::legacy_non_tty_cmd_emits_output` and `spawn_prep::tests::legacy_spawn_env_applies_offline_network_rewrite`. --- .../windows-sandbox-rs/src/elevated_impl.rs | 67 +------------- .../windows-sandbox-rs/src/sandbox_utils.rs | 89 ++++++++++++++----- 2 files changed, 72 insertions(+), 84 deletions(-) diff --git a/codex-rs/windows-sandbox-rs/src/elevated_impl.rs b/codex-rs/windows-sandbox-rs/src/elevated_impl.rs index 2c1c4f79ce..be9f1cfb98 100644 --- a/codex-rs/windows-sandbox-rs/src/elevated_impl.rs +++ b/codex-rs/windows-sandbox-rs/src/elevated_impl.rs @@ -37,72 +37,11 @@ mod windows_impl { use crate::policy::SandboxPolicy; use crate::policy::parse_policy; use crate::runner_client::spawn_runner_transport; + use crate::sandbox_utils::ensure_codex_home_exists; + use crate::sandbox_utils::inject_git_safe_directory; use crate::token::convert_string_sid_to_sid; use anyhow::Result; - use std::collections::HashMap; use std::path::Path; - use std::path::PathBuf; - - /// Ensures the parent directory of a path exists before writing to it. - /// Walks upward from `start` to locate the git worktree root, following gitfile redirects. - fn find_git_root(start: &Path) -> Option { - let mut cur = dunce::canonicalize(start).ok()?; - loop { - let marker = cur.join(".git"); - if marker.is_dir() { - return Some(cur); - } - if marker.is_file() { - if let Ok(txt) = std::fs::read_to_string(&marker) - && let Some(rest) = txt.trim().strip_prefix("gitdir:") - { - let gitdir = rest.trim(); - let resolved = if Path::new(gitdir).is_absolute() { - PathBuf::from(gitdir) - } else { - cur.join(gitdir) - }; - return resolved.parent().map(Path::to_path_buf).or(Some(cur)); - } - return Some(cur); - } - let parent = cur.parent()?; - if parent == cur { - return None; - } - cur = parent.to_path_buf(); - } - } - - /// Creates the sandbox user's Codex home directory if it does not already exist. - fn ensure_codex_home_exists(p: &Path) -> Result<()> { - std::fs::create_dir_all(p)?; - Ok(()) - } - - /// Adds a git safe.directory entry to the environment when running inside a repository. - /// git will not otherwise allow the Sandbox user to run git commands on the repo directory - /// which is owned by the primary user. - fn inject_git_safe_directory( - env_map: &mut HashMap, - cwd: &Path, - _logs_base_dir: Option<&Path>, - ) { - if let Some(git_root) = find_git_root(cwd) { - let mut cfg_count: usize = env_map - .get("GIT_CONFIG_COUNT") - .and_then(|v| v.parse::().ok()) - .unwrap_or(0); - let git_path = git_root.to_string_lossy().replace("\\\\", "/"); - env_map.insert( - format!("GIT_CONFIG_KEY_{cfg_count}"), - "safe.directory".to_string(), - ); - env_map.insert(format!("GIT_CONFIG_VALUE_{cfg_count}"), git_path); - cfg_count += 1; - env_map.insert("GIT_CONFIG_COUNT".to_string(), cfg_count.to_string()); - } - } pub use crate::windows_impl::CaptureResult; @@ -130,7 +69,7 @@ mod windows_impl { normalize_null_device_env(&mut env_map); ensure_non_interactive_pager(&mut env_map); inherit_path_env(&mut env_map); - inject_git_safe_directory(&mut env_map, cwd, None); + inject_git_safe_directory(&mut env_map, cwd); // Use a temp-based log dir that the sandbox user can write. let sandbox_base = codex_home.join(".sandbox"); ensure_codex_home_exists(&sandbox_base)?; diff --git a/codex-rs/windows-sandbox-rs/src/sandbox_utils.rs b/codex-rs/windows-sandbox-rs/src/sandbox_utils.rs index 5d64e5f844..fa08309550 100644 --- a/codex-rs/windows-sandbox-rs/src/sandbox_utils.rs +++ b/codex-rs/windows-sandbox-rs/src/sandbox_utils.rs @@ -8,28 +8,12 @@ use anyhow::Result; use std::collections::HashMap; use std::path::Path; -use std::path::PathBuf; -/// Walk upward from `start` to locate the git worktree root (supports gitfile redirects). -fn find_git_root(start: &Path) -> Option { +/// Walk upward from `start` to locate the git worktree root for `safe.directory`. +fn find_git_worktree_root_for_safe_directory(start: &Path) -> Option { let mut cur = dunce::canonicalize(start).ok()?; loop { - let marker = cur.join(".git"); - if marker.is_dir() { - return Some(cur); - } - if marker.is_file() { - if let Ok(txt) = std::fs::read_to_string(&marker) - && let Some(rest) = txt.trim().strip_prefix("gitdir:") - { - let gitdir = rest.trim(); - let resolved = if Path::new(gitdir).is_absolute() { - PathBuf::from(gitdir) - } else { - cur.join(gitdir) - }; - return resolved.parent().map(Path::to_path_buf).or(Some(cur)); - } + if cur.join(".git").exists() { return Some(cur); } let parent = cur.parent()?; @@ -50,7 +34,7 @@ pub fn ensure_codex_home_exists(p: &Path) -> Result<()> { /// git will not otherwise allow the Sandbox user to run git commands on the repo directory /// which is owned by the primary user. pub fn inject_git_safe_directory(env_map: &mut HashMap, cwd: &Path) { - if let Some(git_root) = find_git_root(cwd) { + if let Some(git_root) = find_git_worktree_root_for_safe_directory(cwd) { let mut cfg_count: usize = env_map .get("GIT_CONFIG_COUNT") .and_then(|v| v.parse::().ok()) @@ -65,3 +49,68 @@ pub fn inject_git_safe_directory(env_map: &mut HashMap, cwd: &Pa env_map.insert("GIT_CONFIG_COUNT".to_string(), cfg_count.to_string()); } } + +#[cfg(test)] +mod tests { + use super::inject_git_safe_directory; + use pretty_assertions::assert_eq; + use std::collections::HashMap; + use std::fs; + use std::path::Path; + use tempfile::TempDir; + + fn safe_directory_value(path: &Path) -> String { + dunce::canonicalize(path) + .expect("canonicalize path") + .to_string_lossy() + .replace("\\\\", "/") + } + + #[test] + fn injects_safe_directory_for_git_directory() { + let temp = TempDir::new().expect("tempdir"); + let repo = temp.path().join("repo"); + let nested = repo.join("nested"); + fs::create_dir_all(repo.join(".git")).expect("create .git"); + fs::create_dir_all(&nested).expect("create nested dir"); + + let mut env_map = HashMap::new(); + inject_git_safe_directory(&mut env_map, &nested); + + let expected = HashMap::from([ + ("GIT_CONFIG_COUNT".to_string(), "1".to_string()), + ("GIT_CONFIG_KEY_0".to_string(), "safe.directory".to_string()), + ( + "GIT_CONFIG_VALUE_0".to_string(), + safe_directory_value(&repo), + ), + ]); + assert_eq!(env_map, expected); + } + + #[test] + fn injects_worktree_root_for_gitfile() { + let temp = TempDir::new().expect("tempdir"); + let repo = temp.path().join("repo"); + let nested = repo.join("nested"); + fs::create_dir_all(&nested).expect("create nested dir"); + fs::write( + repo.join(".git"), + "gitdir: C:/Users/example/repo/.git/worktrees/codex3\n", + ) + .expect("write .git file"); + + let mut env_map = HashMap::new(); + inject_git_safe_directory(&mut env_map, &nested); + + let expected = HashMap::from([ + ("GIT_CONFIG_COUNT".to_string(), "1".to_string()), + ("GIT_CONFIG_KEY_0".to_string(), "safe.directory".to_string()), + ( + "GIT_CONFIG_VALUE_0".to_string(), + safe_directory_value(&repo), + ), + ]); + assert_eq!(env_map, expected); + } +} From 8f5d68f9d25237d0e52ebb8bdd9e7ada01d586c9 Mon Sep 17 00:00:00 2001 From: Brian Henzelmann Date: Wed, 6 May 2026 16:14:50 -0500 Subject: [PATCH 9/9] Document Codex git commit attribution config (#21379) ## Summary - document that commit attribution for generated git commit messages is gated by the `codex_git_commit` feature flag - add an example `config.toml` snippet showing `commit_attribution` with `[features].codex_git_commit = true` - update the config schema description so the reference docs explain that `commit_attribution` only takes effect when the feature is enabled Fixes #19799. ## Validation - `cargo run -p codex-core --bin codex-write-config-schema` - `cargo test -p codex-config` - `cargo test -p codex-features` - `cargo fmt --check` - `git diff --check` ## Notes - `cargo test -p codex-core config_schema_matches_fixture` currently fails before reaching the schema test because `core_test_support` imports `similar` without a linked crate in this checkout. The narrower package checks above avoid that unrelated test-support build failure. --- codex-rs/config/src/config_toml.rs | 3 +++ codex-rs/core/config.schema.json | 2 +- codex-rs/core/src/config/mod.rs | 2 ++ docs/config.md | 21 +++++++++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/codex-rs/config/src/config_toml.rs b/codex-rs/config/src/config_toml.rs index 89eb30b798..d2eeaf927f 100644 --- a/codex-rs/config/src/config_toml.rs +++ b/codex-rs/config/src/config_toml.rs @@ -176,7 +176,10 @@ pub struct ConfigToml { pub compact_prompt: Option, /// Optional commit attribution text for commit message co-author trailers. + /// This top-level setting only takes effect when `[features].codex_git_commit` + /// is enabled. /// + /// When enabled and unset, Codex uses `Codex `. /// Set to an empty string to disable automatic commit attribution. pub commit_attribution: Option, diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 17343ed86f..fe3e48a2df 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -3838,7 +3838,7 @@ "description": "Preferred backend for storing CLI auth credentials. file (default): Use a file in the Codex home directory. keyring: Use an OS-specific keyring service. auto: Use the keyring if available, otherwise use a file." }, "commit_attribution": { - "description": "Optional commit attribution text for commit message co-author trailers.\n\nSet to an empty string to disable automatic commit attribution.", + "description": "Optional commit attribution text for commit message co-author trailers. This top-level setting only takes effect when `[features].codex_git_commit` is enabled.\n\nWhen enabled and unset, Codex uses `Codex `. Set to an empty string to disable automatic commit attribution.", "type": "string" }, "compact_prompt": { diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index 75d99f4b18..ed355a0ae7 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -476,6 +476,8 @@ pub struct Config { pub compact_prompt: Option, /// Optional commit attribution text for commit message co-author trailers. + /// This top-level setting only takes effect when `[features].codex_git_commit` + /// is enabled. /// /// - `None`: use default attribution (`Codex `) /// - `Some("")` or whitespace-only: disable commit attribution diff --git a/docs/config.md b/docs/config.md index 950a631fc5..e0a0c5fc2f 100644 --- a/docs/config.md +++ b/docs/config.md @@ -5,3 +5,24 @@ For basic configuration instructions, see [this documentation](https://developer For advanced configuration instructions, see [this documentation](https://developers.openai.com/codex/config-advanced). For a full configuration reference, see [this documentation](https://developers.openai.com/codex/config-reference). + +## Commit attribution + +Codex can add a [git trailer](https://git-scm.com/docs/git-interpret-trailers) to +generated commit messages so commits make Codex's involvement explicit. This +behavior is gated by the `codex_git_commit` feature flag; the top-level +`commit_attribution` setting is only used when that feature is enabled. + +Add the following to `~/.codex/config.toml`: + +```toml +commit_attribution = "Codex " + +[features] +codex_git_commit = true +``` + +When enabled, Codex appends a `Co-authored-by:` trailer using the configured +attribution value. If `commit_attribution` is omitted, Codex uses +`Codex `. Set `commit_attribution = ""` to disable the +trailer while leaving the feature flag enabled.