Fall back to manual approval when Guardian times out

This commit is contained in:
Katia Bazzi
2026-06-10 17:01:57 -07:00
parent 72667f4f41
commit 2eff458cd9
17 changed files with 500 additions and 27 deletions
+8 -1
View File
@@ -231,6 +231,7 @@ pub struct TestCodexBuilder {
user_shell_override: Option<Shell>,
exec_server_url: Option<String>,
extensions: Arc<ExtensionRegistry<Config>>,
session_source: SessionSource,
}
impl TestCodexBuilder {
@@ -320,6 +321,11 @@ impl TestCodexBuilder {
self
}
pub fn with_session_source(mut self, session_source: SessionSource) -> Self {
self.session_source = session_source;
self
}
pub fn with_windows_cmd_shell(self) -> Self {
if cfg!(windows) {
self.with_user_shell(get_shell_by_model_provided_path(&PathBuf::from("cmd.exe")))
@@ -511,7 +517,7 @@ impl TestCodexBuilder {
let thread_manager = ThreadManager::new(
&config,
codex_core::test_support::auth_manager_from_auth(auth.clone()),
SessionSource::Exec,
self.session_source.clone(),
Arc::clone(&environment_manager),
Arc::clone(&self.extensions),
/*analytics_events_client*/ None,
@@ -1084,6 +1090,7 @@ pub fn test_codex() -> TestCodexBuilder {
user_shell_override: None,
exec_server_url: None,
extensions: empty_extension_registry(),
session_source: SessionSource::Exec,
}
}
@@ -7,7 +7,9 @@ use codex_protocol::config_types::ApprovalsReviewer;
use codex_protocol::protocol::AskForApproval;
use codex_protocol::protocol::EventMsg;
use codex_protocol::protocol::Op;
use codex_protocol::protocol::ReviewDecision;
use codex_protocol::protocol::SandboxPolicy;
use codex_protocol::protocol::SessionSource;
use codex_protocol::user_input::UserInput;
use core_test_support::fs_wait;
use core_test_support::responses::ev_assistant_message;
@@ -19,6 +21,8 @@ use core_test_support::responses::sse;
use core_test_support::responses::start_mock_server;
use core_test_support::skip_if_no_network;
use core_test_support::skip_if_sandbox;
use core_test_support::streaming_sse::StreamingSseChunk;
use core_test_support::streaming_sse::start_streaming_sse_server;
use core_test_support::test_codex::local_selections;
use core_test_support::test_codex::test_codex;
use core_test_support::wait_for_event;
@@ -29,6 +33,143 @@ use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::time::Duration;
use tempfile::TempDir;
use tokio::sync::oneshot;
fn parent_tool_response(call_id: &str, tool_args: &Value) -> String {
let serialized_tool_args = serde_json::to_string(tool_args).unwrap_or_else(|err| {
panic!("serialize tool args failed: {err}");
});
sse(vec![
ev_response_created("resp-parent-tool"),
ev_function_call(call_id, "exec_command", &serialized_tool_args),
ev_completed("resp-parent-tool"),
])
}
fn parent_done_response() -> String {
sse(vec![
ev_response_created("resp-parent-done"),
ev_assistant_message("msg-parent-done", "done"),
ev_completed("resp-parent-done"),
])
}
#[tokio::test(flavor = "current_thread")]
async fn guardian_timeout_falls_back_to_manual_approval_end_to_end() -> Result<()> {
skip_if_no_network!(Ok(()));
skip_if_sandbox!(Ok(()));
let approval_policy = AskForApproval::OnRequest;
let sandbox_policy = SandboxPolicy::WorkspaceWrite {
writable_roots: vec![],
network_access: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
let sandbox_policy_for_config = sandbox_policy.clone();
let mut builder = test_codex()
.with_session_source(SessionSource::Cli)
.with_config(move |config| {
config.permissions.approval_policy = Constrained::allow_any(approval_policy);
config
.set_legacy_sandbox_policy(sandbox_policy_for_config)
.expect("set sandbox policy");
});
let output_dir = TempDir::new()?;
let output_file = output_dir.path().join("guardian-timeout-fallback.txt");
let command = format!("printf guardian-manual > {}", output_file.display());
let tool_args = json!({
"cmd": command,
"yield_time_ms": 1_000_u64,
"sandbox_permissions": SandboxPermissions::RequireEscalated,
"justification": "Exercise Guardian timeout fallback.",
});
let (_guardian_gate_tx, guardian_gate_rx) = oneshot::channel();
let (server, _completions) = start_streaming_sse_server(vec![
vec![StreamingSseChunk {
gate: None,
body: parent_tool_response("exec-call", &tool_args),
}],
vec![
StreamingSseChunk {
gate: None,
body: sse(vec![ev_response_created("resp-guardian-timeout")]),
},
StreamingSseChunk {
gate: Some(guardian_gate_rx),
body: sse(vec![ev_completed("resp-guardian-timeout")]),
},
],
vec![StreamingSseChunk {
gate: None,
body: parent_done_response(),
}],
])
.await;
let test = builder.build_with_streaming_server(&server).await?;
test.codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "run a command that requires Guardian review".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
additional_context: Default::default(),
thread_settings: codex_protocol::protocol::ThreadSettingsOverrides {
environments: Some(local_selections(test.config.cwd.clone())),
approval_policy: Some(approval_policy),
approvals_reviewer: Some(ApprovalsReviewer::AutoReview),
sandbox_policy: Some(sandbox_policy),
..Default::default()
},
})
.await?;
server.wait_for_request_count(/*count*/ 2).await;
tokio::time::pause();
tokio::time::advance(Duration::from_secs(91)).await;
tokio::time::resume();
let approval = loop {
match wait_for_event(&test.codex, |_| true).await {
EventMsg::ExecApprovalRequest(approval) => break approval,
EventMsg::TurnComplete(_) => {
panic!("expected manual approval request before completion")
}
_ => {}
}
};
assert_eq!(approval.effective_approval_id(), "exec-call");
assert!(
approval
.reason
.as_deref()
.is_some_and(|reason| reason.contains("did not finish before its deadline"))
);
test.codex
.submit(Op::ExecApproval {
id: approval.effective_approval_id(),
turn_id: None,
decision: ReviewDecision::Approved,
})
.await?;
loop {
if matches!(
wait_for_event(&test.codex, |_| true).await,
EventMsg::TurnComplete(_)
) {
break;
}
}
assert_eq!(fs::read_to_string(&output_file)?, "guardian-manual");
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn guardian_review_session_does_not_inherit_legacy_notify() -> Result<()> {