diff --git a/codex-rs/core/src/tools/orchestrator.rs b/codex-rs/core/src/tools/orchestrator.rs index 46fd29d030..a1a88e4454 100644 --- a/codex-rs/core/src/tools/orchestrator.rs +++ b/codex-rs/core/src/tools/orchestrator.rs @@ -1,15 +1,13 @@ -/* -Module: orchestrator - -Central place for approvals + sandbox selection + retry semantics. Drives a -simple sequence for any ToolRuntime: approval → select sandbox → attempt → -retry with an escalated sandbox strategy on denial (no re‑approval thanks to -caching). -*/ +//! Coordinates tool approval, sandbox selection, and retries after sandbox denial. +//! Guardian approval applies to the reviewed authority; escalating a sandboxed +//! attempt requires a fresh review of the retry. use crate::config::NetworkProxySpec; use crate::guardian::GuardianReviewContext; use crate::network_policy_decision::network_approval_context_from_payload; +use crate::sandboxing::SandboxPermissions; +use crate::tools::approvals::ApprovalAction; use crate::tools::approvals::ApprovalContext; +use crate::tools::approvals::ApprovalResolutionSource; use crate::tools::flat_tool_name; use crate::tools::network_approval::ActiveNetworkApproval; use crate::tools::network_approval::DeferredNetworkApproval; @@ -140,7 +138,7 @@ impl ToolOrchestrator { let otel_ci = &tool_ctx.call_id; let strict_auto_review = turn_ctx.strict_auto_review_enabled(); // 1) Approval - let mut already_approved = false; + let mut initial_approval_source = None; let environment = tool.turn_environment(req); let sandbox_manager = SandboxManager::new(); @@ -184,12 +182,12 @@ impl ToolOrchestrator { retry_reason: None, network_approval_context: None, }; - tool_ctx + let approval = tool_ctx .session .request_approval(action, approval_ctx) - .await - .result?; - already_approved = true; + .await; + approval.result?; + initial_approval_source = approval.source; } else { otel.tool_decision( &tool_ctx.tool_name, @@ -218,12 +216,12 @@ impl ToolOrchestrator { retry_reason: None, network_approval_context: None, }; - tool_ctx + let approval = tool_ctx .session .request_approval(action, approval_ctx) - .await - .result?; - already_approved = true; + .await; + approval.result?; + initial_approval_source = approval.source; } } @@ -233,7 +231,7 @@ impl ToolOrchestrator { tool.sandbox_permissions(req), &requirement, &file_system_sandbox_policy, - already_approved, + /*already_approved*/ initial_approval_source.is_some(), ); let (escalated_profile, writable_root_metadata) = if sandbox_override == SandboxOverride::EscalatedSandboxWithRestrictions @@ -483,10 +481,14 @@ impl ToolOrchestrator { build_denial_reason_from_output(output.as_ref()) }; - // Strict auto-review approval covers the sandboxed attempt only; - // retrying without the sandbox requires a fresh guardian review. + // Human and policy approvals keep their existing retry scope. A + // Guardian verdict only covers the authority of the reviewed action. + let retry_preapproved = matches!( + initial_approval_source, + Some(ApprovalResolutionSource::User | ApprovalResolutionSource::Hook) + ); let bypass_retry_approval = !strict_auto_review - && tool.should_bypass_approval(approval_policy, already_approved) + && tool.should_bypass_approval(approval_policy, retry_preapproved) && network_approval_context.is_none(); if !bypass_retry_approval { let approval_reason = match &requirement { @@ -494,11 +496,25 @@ impl ToolOrchestrator { ExecApprovalRequirement::Skip { .. } | ExecApprovalRequirement::Forbidden { .. } => None, }; - let action = tool - .approval_action(req, &tool_ctx.call_id) - .map_err(|err| { - ToolError::Rejected(format!("could not prepare approval action: {err}")) - })?; + let mut action = + tool.approval_action(req, &tool_ctx.call_id) + .map_err(|err| { + ToolError::Rejected(format!( + "could not prepare approval action: {err}" + )) + })?; + if unsandboxed_allowed + && let ApprovalAction::ExecCommand { + sandbox_permissions, + additional_permissions, + .. + } = &mut action + { + // Review and cache the authority of the actual retry, not + // the narrower permissions requested for the first attempt. + *sandbox_permissions = SandboxPermissions::RequireEscalated; + *additional_permissions = None; + } let approval_ctx = ApprovalContext { review_context: GuardianReviewContext::from(&tool_ctx.step_context), cancellation_token: Some(tool_ctx.cancellation_token.clone()),