Use issuing step settings for approval decisions (#40821)

## Why

Tool actions and approval retries can outlive the step that issued them. If approval handling reads newer turn settings, a delayed action can use the wrong approval policy or reviewer.

## What changed

- Capture the approval policy and reviewer in `GuardianReviewContext` from the issuing step.
- Use step settings for command, patch, permission, sandbox-retry, and remote-network approval decisions.
- Use the active task's published settings for background network approvals and long-lived Unix shell interception.
- Preserve explicit MCP reviewer overrides.

## Testing

Added regression coverage for permission requests, same-turn network policy changes, delayed commands after a new turn starts, sandbox retries, patch policy selection, and MCP reviewer overrides.

GitOrigin-RevId: b7e84a2e1e79a71502272b15b5ef5b0e5643f8b0
This commit is contained in:
Abhinav authored and copyberry committed 2026-08-26 06:51:58 +00:00
1 parent 21c58c90f2
commit a26f1806a4
16 files changed
+671 -60

No files matched your search

@@ -1,4 +1,5 @@
use super::*;
use crate::session::tests::make_session_and_context_with_rx;
use codex_models_manager::model_info::model_info_from_slug;
use codex_protocol::approvals::NetworkPolicyAmendment;
use pretty_assertions::assert_eq;
@@ -94,3 +95,60 @@ fn guardian_cwd_rejects_foreign_remote_path() {
assert!(guardian_cwd(codex_exec_server::REMOTE_ENVIRONMENT_ID, cwd).is_err());
}
#[tokio::test]
async fn explicit_mcp_reviewer_override_takes_precedence_over_action_context() {
let (session, turn, events) = make_session_and_context_with_rx().await;
let action = ApprovalAction::McpToolCall {
id: "mcp-override".to_string(),
server: "example".to_string(),
tool_name: "dangerous".to_string(),
arguments: None,
connector_id: None,
connector_name: None,
connector_description: None,
connected_account_email: None,
tool_title: None,
tool_description: None,
annotations: None,
hook_tool_name: HookToolName::new("mcp__example__dangerous"),
approval_policy: AskForApproval::OnRequest,
reviewer: ApprovalsReviewer::User,
approval_mode: AppToolApproval::Prompt,
allow_session_remember: false,
allow_persistent_approval: false,
};
let mut review_context = GuardianReviewContext::from(&turn);
review_context.approval_policy = AskForApproval::OnRequest;
review_context.approvals_reviewer = ApprovalsReviewer::AutoReview;
let context = ApprovalContext {
review_context,
cancellation_token: None,
call_id: "mcp-override".to_string(),
tool_name: ToolName::plain("dangerous"),
strict_auto_review: false,
approval_reason: None,
retry_reason: None,
network_approval_context: None,
};
tokio::select! {
resolution = session.request_reviewer_approval(action, &context) => {
panic!("expected a user approval request, got {resolution:?}");
}
event = events.recv() => {
let codex_protocol::protocol::EventMsg::ElicitationRequest(request) =
event.expect("receive user approval request").msg
else {
panic!("expected an MCP user approval request");
};
assert_eq!(request.server_name, "example");
assert_eq!(
request.id,
codex_protocol::mcp::RequestId::String(
"mcp_tool_call_approval_mcp-override".to_string()
)
);
}
}
}