Honor configured and saved approvals in codex exec (#63515)

## Why

`codex exec` could force the approval policy to `never`, overriding
configured approvals on new threads and saved approvals when continuing
a thread.

## What changed

New local and remote threads use their configured approval settings.
Resumed and forked threads inherit the source thread's approval policy
and reviewer independently. An explicit local CLI or profile override
changes the supplied setting and saves it on the resulting thread.

Before an interactive approval can be requested, `codex exec` warns on
stderr when the effective settings call for a human reviewer. Exec
rejects those requests immediately because it runs non-interactively.

## Testing

Regression tests cover approval outcomes, warnings, and saved settings
across local and remote runs. They also cover managed sandbox validation
against an inherited approval policy.

GitOrigin-RevId: 5d3499f7a59ce16b8d909e669e9f74682f6c3b69
This commit is contained in:
Benjamin Carlsson
2026-11-24 03:13:18 +00:00
committed by copyberry
parent 5716c2e228
commit 6b776372fd
8 changed files with 496 additions and 168 deletions
+48 -6
View File
@@ -4,7 +4,10 @@
use core_test_support::responses;
use core_test_support::test_codex_exec::test_codex_exec;
async fn run_exec_with_config(config_toml: &str, extra_args: &[&str]) -> anyhow::Result<String> {
async fn run_exec_with_config(
config_toml: &str,
extra_args: &[&str],
) -> anyhow::Result<(String, responses::ResponseMock)> {
let test = test_codex_exec();
std::fs::write(test.home_path().join("config.toml"), config_toml)?;
@@ -14,7 +17,7 @@ async fn run_exec_with_config(config_toml: &str, extra_args: &[&str]) -> anyhow:
responses::ev_assistant_message("response_1", "done"),
responses::ev_completed("response_1"),
]);
responses::mount_sse_once(&server, body).await;
let mock = responses::mount_sse_once(&server, body).await;
let mut cmd = test.cmd_with_server(&server);
let output = cmd
@@ -25,18 +28,42 @@ async fn run_exec_with_config(config_toml: &str, extra_args: &[&str]) -> anyhow:
assert!(output.status.success(), "exec run failed: {output:?}");
Ok(String::from_utf8(output.stderr)?)
Ok((String::from_utf8(output.stderr)?, mock))
}
async fn run_exec_with_auto_review_config(extra_args: &[&str]) -> anyhow::Result<String> {
run_exec_with_config(
let (stderr, _) = run_exec_with_config(
r#"
approval_policy = "on-request"
approvals_reviewer = "auto_review"
"#,
extra_args,
)
.await
.await?;
Ok(stderr)
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn exec_resume_last_without_a_thread_uses_fresh_exec_policy() -> anyhow::Result<()> {
let (stderr, _) =
run_exec_with_config("approval_policy = 'on-request'", &["resume", "--last"]).await?;
assert!(stderr.contains("approval: on-request"), "{stderr}");
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn exec_warns_about_human_approval_for_granular_policy() -> anyhow::Result<()> {
for request_permissions in [true, false] {
let policy = format!(
"approval_policy={{granular={{sandbox_approval=false,rules=false,request_permissions={request_permissions},mcp_elicitations=false}}}}"
);
let (stderr, _) = run_exec_with_config("", &["-c", &policy]).await?;
assert!(
stderr.contains("WARNING: This thread is configured to ask a human"),
"{stderr}"
);
}
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -52,7 +79,7 @@ async fn exec_preserves_on_request_for_auto_review_config() -> anyhow::Result<()
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn exec_approve_for_me_flag_sets_approval_mode_and_sandbox() -> anyhow::Result<()> {
let stderr = run_exec_with_config("", &["--approve-for-me"]).await?;
let (stderr, _) = run_exec_with_config("", &["--approve-for-me"]).await?;
assert!(
stderr.contains("approval: on-request"),
"stderr missing --approve-for-me approval mode: {stderr}"
@@ -76,3 +103,18 @@ async fn exec_bypass_preserves_never_for_auto_review_config() -> anyhow::Result<
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn exec_review_keeps_parent_approval_policy_and_disables_delegate_approvals()
-> anyhow::Result<()> {
let (stderr, mock) =
run_exec_with_config("approval_policy = 'on-request'", &["review"]).await?;
assert!(stderr.contains("approval: on-request"), "{stderr}");
assert!(
mock.single_request()
.message_input_texts("developer")
.iter()
.any(|text| text.contains("Approval policy is currently never"))
);
Ok(())
}
+11
View File
@@ -1226,6 +1226,12 @@ async fn exec_fork_creates_distinct_threads_with_and_without_a_prompt() -> anyho
test.cmd_with_server(&server)
.arg("--skip-git-repo-check")
.args([
"-c",
"approval_policy='on-request'",
"-c",
"approvals_reviewer='user'",
])
.arg("--thread-source")
.arg("source_feature")
.arg(format!("echo {source_marker}"))
@@ -1274,6 +1280,11 @@ async fn exec_fork_creates_distinct_threads_with_and_without_a_prompt() -> anyho
"promptless fork failed: {}",
String::from_utf8_lossy(&promptless_output.stderr)
);
let stderr = String::from_utf8(promptless_output.stderr)?;
assert!(
!stderr.contains("WARNING: This thread is configured to ask a human"),
"{stderr}"
);
let promptless_events = String::from_utf8(promptless_output.stdout)?
.lines()
.map(serde_json::from_str::<Value>)