Read approval policy from the current turn configuration (#36912)

## Why

Thread settings can update the approval policy after a turn context is created.
Keeping a separate copy on `TurnContext` could leave tool approval checks using
the previous policy.

## What changed

- Remove the duplicated approval-policy field from `TurnContext`.
- Resolve the policy through the turn's current configuration everywhere it is
  needed, including tool execution, Guardian routing, MCP handling, and
  permission requests.

## Testing

- Update the shell escalation test to apply a thread-level policy override and
  verify that the next turn rejects an escalation using the updated policy.

GitOrigin-RevId: e5966ba08f179d53fd76871ad904762958c0f5ea
This commit is contained in:
jif authored and copyberry committed 2026-08-04 15:32:31 +00:00
1 parent ee46c5ba0e
commit 6a828ca26f
25 files changed
+119 -89

No files matched your search

@@ -572,7 +572,7 @@ async fn execute_verified_patch(
&request,
&tool_ctx,
tool_ctx.turn.as_ref(),
tool_ctx.turn.approval_policy.value(),
tool_ctx.turn.approval_policy(),
)
.await
.map(|result| result.output);
@@ -237,7 +237,7 @@ pub(crate) fn apply_spawn_agent_runtime_overrides(
config
.permissions
.approval_policy
.set(turn.approval_policy.value())
.set(turn.approval_policy())
.map_err(|err| {
FunctionCallError::RespondToModel(format!("approval_policy is invalid: {err}"))
})?;
@@ -273,9 +273,6 @@ async fn spawn_agent_uses_explorer_role_and_preserves_approval_policy() {
.approval_policy
.set(AskForApproval::OnRequest)
.expect("approval policy should be set");
turn.approval_policy
.set(AskForApproval::OnRequest)
.expect("approval policy should be set");
turn.provider = create_model_provider(provider_info, turn.auth_manager.clone());
turn.config = Arc::new(config);
@@ -2258,7 +2255,9 @@ async fn spawn_agent_reapplies_runtime_sandbox_after_role_config() {
&expected_file_system_sandbox_policy,
expected_network_sandbox_policy,
);
turn.approval_policy
Arc::make_mut(&mut turn.config)
.permissions
.approval_policy
.set(AskForApproval::OnRequest)
.expect("approval policy should be set");
let mut config = (*turn.config).clone();
@@ -4440,7 +4439,9 @@ async fn build_agent_spawn_config_uses_turn_context_values() {
network_sandbox_policy,
);
turn.permission_profile = permission_profile.clone();
turn.approval_policy
Arc::make_mut(&mut turn.config)
.permissions
.approval_policy
.set(AskForApproval::OnRequest)
.expect("approval policy set");
@@ -4474,7 +4475,9 @@ async fn build_agent_resume_config_clears_base_instructions() {
let mut base_config = (*turn.config).clone();
base_config.base_instructions = Some("caller-base".to_string());
turn.config = Arc::new(base_config);
turn.approval_policy
Arc::make_mut(&mut turn.config)
.permissions
.approval_policy
.set(AskForApproval::OnRequest)
.expect("approval policy set");
+5 -11
View File
@@ -108,7 +108,7 @@ async fn run_exec_like(args: RunExecLikeArgs) -> Result<FunctionToolOutput, Func
|| {
normalize_and_validate_additional_permissions(
additional_permissions_allowed,
turn.approval_policy.value(),
turn.approval_policy(),
effective_additional_permissions.sandbox_permissions,
effective_additional_permissions.additional_permissions,
effective_additional_permissions.permissions_preapproved,
@@ -127,11 +127,11 @@ async fn run_exec_like(args: RunExecLikeArgs) -> Result<FunctionToolOutput, Func
.requests_sandbox_override()
&& !effective_additional_permissions.permissions_preapproved
&& !matches!(
turn.approval_policy.value(),
turn.approval_policy(),
codex_protocol::protocol::AskForApproval::OnRequest
)
{
let approval_policy = turn.approval_policy.value();
let approval_policy = turn.approval_policy();
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:?}"
)));
@@ -177,7 +177,7 @@ async fn run_exec_like(args: RunExecLikeArgs) -> Result<FunctionToolOutput, Func
.exec_policy
.create_exec_approval_requirement_for_command(ExecApprovalRequest {
command: &exec_params.command,
approval_policy: turn.approval_policy.value(),
approval_policy: turn.approval_policy(),
permission_profile: turn.permission_profile(),
windows_sandbox_level: turn.windows_sandbox_level,
sandbox_permissions: if effective_additional_permissions.permissions_preapproved {
@@ -217,13 +217,7 @@ async fn run_exec_like(args: RunExecLikeArgs) -> Result<FunctionToolOutput, Func
tool_name,
};
let out = orchestrator
.run(
&mut runtime,
&req,
&tool_ctx,
&turn,
turn.approval_policy.value(),
)
.run(&mut runtime, &req, &tool_ctx, &turn, turn.approval_policy())
.await
.map(|result| result.output);
let event_ctx = ToolEventCtx::new(
@@ -277,11 +277,11 @@ impl ExecCommandHandler {
.requests_sandbox_override()
&& !effective_additional_permissions.permissions_preapproved
&& !matches!(
context.turn.approval_policy.value(),
context.turn.approval_policy(),
codex_protocol::protocol::AskForApproval::OnRequest
)
{
let approval_policy = context.turn.approval_policy.value();
let approval_policy = context.turn.approval_policy();
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:?}"
@@ -297,7 +297,7 @@ impl ExecCommandHandler {
|| {
normalize_and_validate_additional_permissions(
additional_permissions_allowed,
context.turn.approval_policy.value(),
context.turn.approval_policy(),
effective_additional_permissions.sandbox_permissions,
effective_additional_permissions.additional_permissions,
effective_additional_permissions.permissions_preapproved,