Align sandbox retries with the permissions reviewed for them (#63683)

## What changed

Build the retry from the initial sandbox attempt. When policy permits an
unsandboxed retry, clear the sandbox settings and mark the command as
`RequireEscalated` for approval. When policy requires the sandbox to stay
in place, reuse the initial attempt so a network retry preserves file read
restrictions.

## Testing

Cover both Guardian verdicts after a sandboxed attempt. An approved retry
reaches the exec server without a sandbox, and a denied retry is blocked
before it reaches the executor.

GitOrigin-RevId: 5bf36dd4f43b7c0379457b88b8ddbf3dbd482b87
This commit is contained in:
jif
2026-11-24 17:19:40 +00:00
committed by copyberry
parent a009fc6a4e
commit f67868d050
2 changed files with 61 additions and 74 deletions
+37 -64
View File
@@ -83,25 +83,13 @@ impl ToolOrchestrator {
tool_name: tool_ctx.tool_name.clone(),
};
let attempt_with_network_approval = SandboxAttempt {
sandbox_override: attempt.sandbox_override,
local_selection: attempt.local_selection,
sandbox_requested: attempt.sandbox_requested,
permissions: attempt.permissions,
exec_server_permissions: attempt.exec_server_permissions,
enforce_managed_network: attempt.enforce_managed_network,
manager: attempt.manager,
sandbox_cwd: attempt.sandbox_cwd,
workspace_roots: attempt.workspace_roots,
sandbox_exe: attempt.sandbox_exe,
use_legacy_landlock: attempt.use_legacy_landlock,
windows_sandbox_type: attempt.windows_sandbox_type,
windows_sandbox_level: attempt.windows_sandbox_level,
network_denial_cancellation_token: network_approval
.as_ref()
.map(ActiveNetworkApproval::cancellation_token),
network_proxy: network_approval
.as_ref()
.map(ActiveNetworkApproval::execution_proxy),
..*attempt
};
let run_result = tool
.run(req, &attempt_with_network_approval, &attempt_tool_ctx)
@@ -503,32 +491,48 @@ impl ToolOrchestrator {
&& !strict_auto_review
&& tool.should_bypass_approval(approval_policy, retry_preapproved)
&& network_approval_context.is_none();
if !bypass_retry_approval {
let mut retry_action = if bypass_retry_approval {
None
} else {
Some(
tool.approval_action(req, &tool_ctx.call_id)
.map_err(|err| {
ToolError::Rejected(format!(
"could not prepare approval action: {err}"
))
})?,
)
};
let retry_attempt = if unsandboxed_allowed {
// Review and cache the same escalation that the executor will run.
if let Some(ApprovalAction::ExecCommand {
sandbox_permissions,
additional_permissions,
..
}) = &mut retry_action
{
*sandbox_permissions = SandboxPermissions::RequireEscalated;
*additional_permissions = None;
}
SandboxAttempt {
local_selection: SandboxSelection::Explicit(SandboxType::None),
sandbox_requested: false,
sandbox_exe: None,
..initial_attempt
}
} else {
// A network retry must preserve the sandbox enforcing denied reads.
initial_attempt
};
if let Some(action) = retry_action {
let approval_reason = match &requirement {
ExecApprovalRequirement::NeedsApproval { reason, .. }
| ExecApprovalRequirement::NeedsHumanApproval { reason } => reason.clone(),
ExecApprovalRequirement::Skip { .. }
| ExecApprovalRequirement::Forbidden { .. } => None,
};
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 {
exec_requirement: Some(requirement.clone()),
review_context: GuardianReviewContext::from(&tool_ctx.step_context),
@@ -557,37 +561,6 @@ impl ToolOrchestrator {
.result?;
}
let retry_sandbox_requested = !unsandboxed_allowed && policy_requires_sandbox;
let local_selection = if retry_sandbox_requested {
platform_selection
} else {
SandboxSelection::Explicit(SandboxType::None)
};
let retry_sandbox_exe = if unsandboxed_allowed {
None
} else {
codex_sandbox_exe
};
let retry_attempt = SandboxAttempt {
sandbox_override,
local_selection,
sandbox_requested: retry_sandbox_requested,
permissions: initial_permissions,
exec_server_permissions: escalated_profile
.as_ref()
.unwrap_or(permission_profile),
enforce_managed_network: managed_network_active,
manager: &sandbox_manager,
sandbox_cwd: &sandbox_policy_cwd,
workspace_roots,
sandbox_exe: retry_sandbox_exe,
use_legacy_landlock: sandbox_config.use_legacy_landlock,
windows_sandbox_type,
windows_sandbox_level: sandbox_config.windows_sandbox_level,
network_denial_cancellation_token: None,
network_proxy: None,
};
// Second attempt.
let network_approval_spec = tool.network_approval_spec(req, tool_ctx);
let escalated_attempt_start = Instant::now();
@@ -30,6 +30,15 @@ use tokio::sync::oneshot;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn guardian_rechecks_unsandboxed_retry_after_sandboxed_approval() -> Result<()> {
check_guardian_retry(/*allow_retry*/ false).await
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn guardian_approved_retry_runs_without_the_sandbox() -> Result<()> {
check_guardian_retry(/*allow_retry*/ true).await
}
async fn check_guardian_retry(allow_retry: bool) -> Result<()> {
let server = start_mock_server().await;
let listener = TcpListener::bind("127.0.0.1:0").await?;
let exec_server_url = format!("ws://{}", listener.local_addr()?);
@@ -120,7 +129,11 @@ async fn guardian_rechecks_unsandboxed_retry_after_sandboxed_approval() -> Resul
sse(vec![
ev_assistant_message(
"retry-review",
r#"{"outcome":"deny","rationale":"The unsandboxed retry is not authorized."}"#,
if allow_retry {
r#"{"outcome":"allow"}"#
} else {
r#"{"outcome":"deny","rationale":"The unsandboxed retry is not authorized."}"#
},
),
ev_completed("retry-review"),
]),
@@ -167,11 +180,16 @@ async fn guardian_rechecks_unsandboxed_retry_after_sandboxed_approval() -> Resul
let output = responses
.function_call_output_text("sandbox-retry")
.context("tool result")?;
assert_eq!(
starts.len(),
1,
"the denied retry must not reach the executor: {output}"
);
if allow_retry {
assert_eq!(starts.len(), 2, "{output}");
assert_eq!(starts[1]["sandbox"], Value::Null);
} else {
assert_eq!(starts.len(), 1, "{output}");
assert!(
output.contains("The unsandboxed retry is not authorized."),
"{output}"
);
}
assert!(
starts[0]["sandbox"].is_object(),
"the first attempt must be sandboxed"
@@ -201,9 +219,5 @@ async fn guardian_rechecks_unsandboxed_retry_after_sandboxed_approval() -> Resul
let mut retry = action.clone();
retry["sandbox_permissions"] = json!("require_escalated");
assert_eq!(actions, vec![action, retry]);
assert!(
output.contains("The unsandboxed retry is not authorized."),
"{output}"
);
Ok(())
}