diff --git a/codex-rs/core/src/guardian/runtime.rs b/codex-rs/core/src/guardian/runtime.rs index 9fa7889b91..ac534dbd52 100644 --- a/codex-rs/core/src/guardian/runtime.rs +++ b/codex-rs/core/src/guardian/runtime.rs @@ -2,6 +2,7 @@ //! Tool-call provenance is separate from the item's display identity. use codex_protocol::protocol::ReviewDecision; +use codex_utils_path_uri::PathConvention; use std::sync::Arc; use tokio_util::sync::CancellationToken; @@ -11,6 +12,7 @@ use super::GuardianReviewContext; use super::GuardianReviewOptions; use super::approval_request::guardian_approval_request_to_json; use crate::session::session::Session; +use crate::tools::sandboxing::ApprovalAction; /// Carries the original action even when the legacy synchronous renderer cannot handle its paths. /// This lets the extension return AskUser before invoking that renderer. @@ -49,9 +51,32 @@ impl From for ReviewAction { impl ReviewAction { pub(crate) fn from_approval_action( - action: crate::tools::sandboxing::ApprovalAction, - exec_command_cwd_convention: Option, + action: ApprovalAction, + context: &GuardianReviewContext, ) -> Self { + let exec_command_cwd_convention = match &action { + ApprovalAction::ExecCommand { + environment_id, + cwd, + .. + } => Some( + match context + .environments() + .turn_environments() + .find(|environment| environment.selection.environment_id == *environment_id) + .and_then(|environment| environment.executor_platform_os.as_deref()) + { + Some("windows") => PathConvention::Windows, + Some(_) => PathConvention::Posix, + // Legacy executors did not report their OS. Preserve the + // previous spelling-based behavior for those executors. + None => cwd + .infer_path_convention() + .unwrap_or_else(PathConvention::native), + }, + ), + _ => None, + }; let category = action.guardian_scope(); let original = serde_json::to_value(&action).map_err(|error| error.to_string()); match action.into_guardian_request(exec_command_cwd_convention) { diff --git a/codex-rs/core/src/guardian/tests.rs b/codex-rs/core/src/guardian/tests.rs index 071a61716e..8594964be4 100644 --- a/codex-rs/core/src/guardian/tests.rs +++ b/codex-rs/core/src/guardian/tests.rs @@ -1478,12 +1478,22 @@ fn guardian_write_stdin_preserves_input_and_foreign_cwd( Ok(()) } -#[test_case::test_case(codex_utils_path_uri::PathConvention::Windows, r"C:\workspace"; "windows")] -#[test_case::test_case(codex_utils_path_uri::PathConvention::Posix, "/C:/workspace"; "posix")] -fn guardian_exec_command_uses_executor_cwd_convention( - cwd_convention: codex_utils_path_uri::PathConvention, +#[test_case::test_case(Some("windows"), r"C:\workspace"; "windows")] +#[test_case::test_case(Some("linux"), "/C:/workspace"; "posix")] +#[test_case::test_case(None, r"C:\workspace"; "legacy")] +#[tokio::test] +async fn guardian_exec_command_uses_executor_cwd_convention( + platform_os: Option<&str>, expected_cwd: &str, ) -> serde_json::Result<()> { + let (_session, mut turn) = crate::session::tests::make_session_and_context().await; + let mut executor = turn.initial_environments.primary().unwrap().clone(); + executor.selection.environment_id = codex_exec_server::REMOTE_ENVIRONMENT_ID.to_string(); + executor.executor_platform_os = platform_os.map(str::to_owned); + turn.initial_environments + .environments + .push(TurnEnvironmentState::Ready(executor)); + let context = GuardianReviewContext::from(Arc::new(turn)); let cwd = PathUri::parse("file:///C:/workspace").expect("valid executor cwd"); let action = crate::tools::sandboxing::ApprovalAction::ExecCommand { id: "command-1".to_string(), @@ -1496,9 +1506,10 @@ fn guardian_exec_command_uses_executor_cwd_convention( justification: None, tty: false, proposed_execpolicy_amendment: None, - } - .into_guardian_request(Some(cwd_convention)) - .expect("Guardian request should render executor cwd"); + }; + let action = ReviewAction::from_approval_action(action, &context) + .request + .expect("Guardian request should render executor cwd"); assert_eq!( guardian_approval_request_to_json(&action)?, diff --git a/codex-rs/core/src/tools/approvals.rs b/codex-rs/core/src/tools/approvals.rs index e7e846c439..bf1ed4f94b 100644 --- a/codex-rs/core/src/tools/approvals.rs +++ b/codex-rs/core/src/tools/approvals.rs @@ -623,34 +623,7 @@ impl Session { } let is_network_approval = matches!(&action, ApprovalAction::NetworkAccess { .. }); let review_id = new_guardian_review_id(); - let exec_command_cwd_convention = match &action { - ApprovalAction::ExecCommand { - environment_id, - cwd, - .. - } => Some( - match ctx - .review_context - .environments() - .turn_environments() - .find(|environment| environment.selection.environment_id == *environment_id) - .and_then(|environment| environment.executor_platform_os.as_deref()) - { - Some("windows") => PathConvention::Windows, - Some(_) => PathConvention::Posix, - // Legacy executors did not report their OS. Preserve the - // previous spelling-based behavior for those executors. - None => cwd - .infer_path_convention() - .unwrap_or_else(PathConvention::native), - }, - ), - _ => None, - }; - let action = crate::guardian::ReviewAction::from_approval_action( - action, - exec_command_cwd_convention, - ); + let action = crate::guardian::ReviewAction::from_approval_action(action, &context); let reasons = ApprovalRequestReasons { approval: ctx.approval_reason.clone(),