Files
codex/codex-rs/core/src/apply_patch_tests.rs
Abhinav a26f1806a4 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
2026-08-26 06:51:58 +00:00

71 lines
2.5 KiB
Rust

use super::*;
use crate::session::tests::make_session_and_context;
use crate::session::tests::update_selected_settings_for_test;
use codex_protocol::models::PermissionProfile;
use codex_protocol::models::PermissionProfileSnapshot;
use codex_protocol::protocol::AskForApproval;
use codex_utils_path_uri::PathUri;
use pretty_assertions::assert_eq;
use std::sync::Arc;
use tempfile::tempdir;
#[test]
fn convert_apply_patch_maps_add_variant() {
let tmp = tempdir().expect("tmp");
let path = tmp.path().join("a.txt");
let path_uri = PathUri::from_host_native_path(&path).expect("absolute test path");
let action = ApplyPatchAction::new_add_for_test(&path_uri, "hello".to_string());
let got = convert_apply_patch_to_protocol(&action);
assert_eq!(
got.get(path.as_path()),
Some(&FileChange::Add {
content: "hello".to_string()
})
);
}
#[tokio::test]
async fn prepare_apply_patch_uses_action_policy_before_turn_policy() {
let (_, mut turn) = make_session_and_context().await;
Arc::make_mut(&mut turn.config)
.permissions
.approval_policy
.set(AskForApproval::Never)
.expect("set admitted turn approval policy");
let mut step = StepContext::for_test(Arc::new(turn));
let step_settings = &mut Arc::get_mut(&mut step)
.expect("unshared test step")
.settings;
update_selected_settings_for_test(Arc::make_mut(step_settings), |selected| {
selected
.approval_policy
.set(AskForApproval::OnRequest)
.expect("set captured approval policy");
});
let tmp = tempdir().expect("tmp");
let path = tmp.path().join("outside.txt");
let path_uri = PathUri::from_host_native_path(&path).expect("absolute test path");
let action = ApplyPatchAction::new_add_for_test(&path_uri, "hello".to_string());
let permission_profile = PermissionProfile::read_only();
let file_system_policy = permission_profile.file_system_sandbox_policy();
let mut environment = step
.environments
.primary()
.expect("primary environment")
.clone();
environment.config_mut().permission_profile =
PermissionProfileSnapshot::legacy(permission_profile);
let prepared = prepare_apply_patch(&step, &environment, &file_system_policy, action)
.expect("issuing action policy should request approval");
assert!(!prepared.auto_approved);
assert!(matches!(
prepared.exec_approval_requirement,
ExecApprovalRequirement::NeedsApproval { .. }
));
}