Lightweight coding agent that runs in your terminal
Rust
Refresh Guardian reviews when review context changes (#53797)
jif2b2d7923055f
diff --git a/codex-rs/core/src/guardian/review_request.rs b/codex-rs/core/src/guardian/review_request.rsindex 798c8806e1..43bfae0312 100644--- a/codex-rs/core/src/guardian/review_request.rs+++ b/codex-rs/core/src/guardian/review_request.rs@@ -62,15 +62,19 @@ impl ReviewAuthorization {authorization_changed || self.user_message_revision != history.user_message_revision();let review_context_changed = root_review_changed|| self.review_context_revision != history.guardian_review_context_revision();- // An actual stop or history reset wins over a concurrent authorization change.+ // An actual stop or history reset wins over a concurrent evidence change.if self.history_reset.is_cancelled()|| cancellation.is_some_and(CancellationToken::is_cancelled)|| root_history_changed- || review_context_changed{Some(GuardianReviewSessionOutcome::Aborted)- } else if authorization_changed {- tracing::info!(thread_id = %session.thread_id, "Guardian approval invalidated by an authorization change");+ } else if authorization_changed || review_context_changed {+ tracing::info!(+ thread_id = %session.thread_id,+ authorization_changed,+ review_context_changed,+ "Guardian approval invalidated by changed review evidence"+ );Some(GuardianReviewSessionOutcome::StaleAuthorization)} else {Nonediff --git a/codex-rs/core/tests/suite/guardian_pending_root_delivery.rs b/codex-rs/core/tests/suite/guardian_pending_root_delivery.rsindex c3c1564e8b..d4b668bcf1 100644--- a/codex-rs/core/tests/suite/guardian_pending_root_delivery.rs+++ b/codex-rs/core/tests/suite/guardian_pending_root_delivery.rs@@ -3,14 +3,21 @@use super::*;use anyhow::Context;use codex_history::RetainedContextEntry;+use codex_protocol::approvals::GuardianAssessmentAction;+use codex_protocol::approvals::GuardianAssessmentOutcome;use codex_protocol::protocol::GuardianAssessmentStatus;use core_test_support::streaming_sse::StreamingSseChunk;use core_test_support::streaming_sse::start_streaming_sse_server;use pretty_assertions::assert_eq;+use test_case::test_case;use tokio::sync::oneshot;+#[test_case(GuardianAssessmentOutcome::Allow; "allow after refresh")]+#[test_case(GuardianAssessmentOutcome::Deny; "deny after refresh")]#[tokio::test(flavor = "multi_thread", worker_threads = 4)]-async fn confirmed_root_delivery_invalidates_pending_worker_allow() -> Result<()> {+async fn confirmed_root_delivery_refreshes_pending_worker_allow(+ refreshed_outcome: GuardianAssessmentOutcome,+) -> Result<()> {skip_if_no_network!(Ok(()));skip_if_wine_exec!(Ok(()),@@ -33,6 +40,7 @@ async fn confirmed_root_delivery_invalidates_pending_worker_allow() -> Result<().await?;let root = test.session_configured.thread_id;let mut created_threads = test.thread_manager.subscribe_thread_created();+ let output_file = test.workspace_path_uri("refreshed-worker-allow.txt")?;mount_sse_once_match(&server,@@ -81,7 +89,8 @@ async fn confirmed_root_delivery_invalidates_pending_worker_allow() -> Result<()WORKER_CALL_ID,"exec_command",&json!({- "cmd": "true", "sandbox_permissions": "require_escalated",+ "cmd": "echo executed >> refreshed-worker-allow.txt",+ "sandbox_permissions": "require_escalated","justification": "Review the production deployment."}).to_string(),@@ -102,20 +111,36 @@ async fn confirmed_root_delivery_invalidates_pending_worker_allow() -> Result<().mount(&server).await;let (release_review, review_gate) = oneshot::channel();- let (review_stream, _) = start_streaming_sse_server(vec![vec![StreamingSseChunk {- gate: Some(review_gate),- body: sse(vec![+ let (release_refreshed_review, refreshed_review_gate) = oneshot::channel();+ let (review_stream, _) = start_streaming_sse_server(vec![+ vec![StreamingSseChunk {+ gate: Some(review_gate),+ body: sse(vec![+ ev_assistant_message(+ "guardian-assessment",+ &json!({+ "risk_level": "low", "user_authorization": "high", "outcome": "allow",+ "rationale": "The original root context authorized the command."+ })+ .to_string(),+ ),+ ev_completed("guardian-response"),+ ]),+ }],+ vec![StreamingSseChunk {+ gate: Some(refreshed_review_gate),+ body: sse(vec![ev_assistant_message(- "guardian-assessment",+ "refreshed-guardian-assessment",&json!({- "risk_level": "low", "user_authorization": "high", "outcome": "allow",- "rationale": "The original root context authorized the command."- })- .to_string(),+ "risk_level": "low", "user_authorization": "high", "outcome": refreshed_outcome,+ "rationale": "Decision using the newly delivered assistant context."+ }).to_string(),),- ev_completed("guardian-response"),+ ev_completed("refreshed-guardian-response"),]),- }]])+ }],+ ]).await;wiremock::Mock::given(wiremock::matchers::method("POST")).and(wiremock::matchers::path("/v1/responses"))@@ -128,10 +153,19 @@ async fn confirmed_root_delivery_invalidates_pending_worker_allow() -> Result<().insert_header("location", format!("{}/v1/responses", review_stream.uri())),).with_priority(/*priority*/ 2)- .up_to_n_times(/*n*/ 1)+ .up_to_n_times(/*n*/ 2).mount(&server).await;+ mount_sse_once_match(+ &server,+ move |request: &wiremock::Request| {+ is_worker_request(request, root) && has_call_output(request, WORKER_CALL_ID)+ },+ sse(vec![ev_completed("worker-done")]),+ )+ .await;+test.submit_text_turn(INITIAL_PROMPT).await?;let worker = test.thread_manager@@ -178,6 +212,38 @@ async fn confirmed_root_delivery_invalidates_pending_worker_allow() -> Result<().await.context("confirmed root delivery did not revise worker review context")?;release_review.send(()).expect("release Guardian allow");+ tokio::time::timeout(+ Duration::from_secs(/*secs*/ 10),+ review_stream.wait_for_request_count(/*count*/ 2),+ )+ .await+ .context("delivered assistant context did not trigger a fresh review")?;+ let requests = server.received_requests().await.expect("recorded requests");+ let reviews = requests+ .iter()+ .filter_map(request_body)+ .filter(|body| {+ body["client_metadata"]["x-openai-subagent"] == "guardian"+ && !is_hosted_messaging_review_body(body)+ })+ .collect::<Vec<_>>();+ assert_eq!(reviews.len(), 2);+ let original = &reviews[0];+ let refreshed = &reviews[1];+ let delivered_message = format!("assistant: {ROOT_ASSISTANT_REPLY}");+ assert!(!original.to_string().contains(&delivered_message));+ assert!(refreshed.to_string().contains(&delivered_message));+ assert_eq!(+ test.fs()+ .get_metadata(&output_file, Default::default(), /*sandbox*/ None)+ .await+ .expect_err("stale allow executed the command")+ .kind(),+ std::io::ErrorKind::NotFound,+ );+ release_refreshed_review+ .send(())+ .expect("release fresh Guardian decision");let status = wait_for_event_match(&worker, |event| match event {EventMsg::GuardianAssessment(assessment)if assessment.status != GuardianAssessmentStatus::InProgress =>@@ -187,7 +253,32 @@ async fn confirmed_root_delivery_invalidates_pending_worker_allow() -> Result<()_ => None,}).await;- assert_eq!(status, GuardianAssessmentStatus::Aborted);+ let expected_status = match refreshed_outcome {+ GuardianAssessmentOutcome::Allow => GuardianAssessmentStatus::Approved,+ GuardianAssessmentOutcome::Deny => GuardianAssessmentStatus::Denied,+ };+ assert_eq!(status, expected_status);+ wait_for_event(&worker, |event| matches!(event, EventMsg::TurnComplete(_))).await;+ match refreshed_outcome {+ GuardianAssessmentOutcome::Allow => {+ let contents = test+ .fs()+ .read_file_text(&output_file, Default::default(), /*sandbox*/ None)+ .await?;+ assert_eq!(contents.lines().collect::<Vec<_>>(), vec!["executed"]);+ }+ GuardianAssessmentOutcome::Deny => {+ assert_eq!(+ test.fs()+ .get_metadata(&output_file, Default::default(), /*sandbox*/ None)+ .await+ .expect_err("denied action executed")+ .kind(),+ std::io::ErrorKind::NotFound,+ );+ }+ }+ assert_eq!(review_stream.requests().await.len(), 2);let shutdown = test.thread_manager.shutdown_all_threads_bounded(Duration::from_secs(/*secs*/ 10))@@ -198,8 +289,12 @@ async fn confirmed_root_delivery_invalidates_pending_worker_allow() -> Result<()Ok(())}+#[test_case(GuardianAssessmentOutcome::Allow; "allow after refresh")]+#[test_case(GuardianAssessmentOutcome::Deny; "deny after refresh")]#[tokio::test(flavor = "multi_thread", worker_threads = 4)]-async fn confirmed_delivery_invalidates_pending_local_allow() -> Result<()> {+async fn confirmed_delivery_refreshes_pending_local_allow(+ refreshed_outcome: GuardianAssessmentOutcome,+) -> Result<()> {skip_if_no_network!(Ok(()));skip_if_wine_exec!(Ok(()),@@ -222,14 +317,13 @@ async fn confirmed_delivery_invalidates_pending_local_allow() -> Result<()> {}).await?;let root = test.session_configured.thread_id;- let output_file = test.cwd.path().join("stale-local-allow.txt");- let output_path = shlex::try_join([output_file.to_string_lossy().as_ref()])?;+ let output_file = test.workspace_path_uri("refreshed-local-allow.txt")?;let messaging_tool = code_mode_name_for_tool_name(&ToolName::namespaced("mcp__codex_apps__user_message","_send_message",));let command = json!({- "cmd": format!("printf should-not-run > {output_path}"),+ "cmd": "echo executed >> refreshed-local-allow.txt","sandbox_permissions": "require_escalated","justification": "Review the production deployment."});@@ -253,20 +347,36 @@ async fn confirmed_delivery_invalidates_pending_local_allow() -> Result<()> {mount_completion(&server, root, MESSAGE_CALL_ID).await;let (release_review, review_gate) = oneshot::channel();- let (review_stream, _) = start_streaming_sse_server(vec![vec![StreamingSseChunk {- gate: Some(review_gate),- body: sse(vec![+ let (release_refreshed_review, refreshed_review_gate) = oneshot::channel();+ let (review_stream, _) = start_streaming_sse_server(vec![+ vec![StreamingSseChunk {+ gate: Some(review_gate),+ body: sse(vec![+ ev_assistant_message(+ "local-guardian-allow",+ &json!({+ "risk_level": "low", "user_authorization": "high", "outcome": "allow",+ "rationale": "The original context authorized the command."+ })+ .to_string(),+ ),+ ev_completed("local-guardian-allow"),+ ]),+ }],+ vec![StreamingSseChunk {+ gate: Some(refreshed_review_gate),+ body: sse(vec![ev_assistant_message(- "local-guardian-allow",+ "refreshed-guardian-assessment",&json!({- "risk_level": "low", "user_authorization": "high", "outcome": "allow",- "rationale": "The original context authorized the command."- })- .to_string(),+ "risk_level": "low", "user_authorization": "high", "outcome": refreshed_outcome,+ "rationale": "Decision using the newly delivered assistant context."+ }).to_string(),),- ev_completed("local-guardian-allow"),+ ev_completed("refreshed-guardian-response"),]),- }]])+ }],+ ]).await;wiremock::Mock::given(wiremock::matchers::method("POST")).and(wiremock::matchers::path("/v1/responses"))@@ -281,7 +391,7 @@ async fn confirmed_delivery_invalidates_pending_local_allow() -> Result<()> {.insert_header("location", format!("{}/v1/responses", review_stream.uri())),).with_priority(/*priority*/ 2)- .up_to_n_times(/*n*/ 1)+ .up_to_n_times(/*n*/ 2).mount(&server).await;@@ -325,21 +435,77 @@ async fn confirmed_delivery_invalidates_pending_local_allow() -> Result<()> {.await.context("confirmed delivery was not retained during local review")?;release_review.send(()).expect("release stale local allow");+ tokio::time::timeout(+ Duration::from_secs(/*secs*/ 10),+ review_stream.wait_for_request_count(/*count*/ 2),+ )+ .await+ .context("delivered assistant context did not trigger a fresh review")?;+ let requests = server.received_requests().await.expect("recorded requests");+ let reviews = requests+ .iter()+ .filter_map(request_body)+ .filter(|body| {+ body["client_metadata"]["x-openai-subagent"] == "guardian"+ && !is_hosted_messaging_review_body(body)+ })+ .collect::<Vec<_>>();+ assert_eq!(reviews.len(), 2);+ let original = &reviews[0];+ let refreshed = &reviews[1];+ let delivered_message = format!("assistant: {ROOT_ASSISTANT_REPLY}");+ assert!(!original.to_string().contains(&delivered_message));+ assert!(refreshed.to_string().contains(&delivered_message));+ assert_eq!(+ test.fs()+ .get_metadata(&output_file, Default::default(), /*sandbox*/ None)+ .await+ .expect_err("stale allow executed the command")+ .kind(),+ std::io::ErrorKind::NotFound,+ );+ release_refreshed_review+ .send(())+ .expect("release fresh Guardian decision");let status = wait_for_event_match(&test.codex, |event| match event {EventMsg::GuardianAssessment(assessment)- if assessment.status == GuardianAssessmentStatus::Aborted =>+ if assessment.status != GuardianAssessmentStatus::InProgress+ && matches!(assessment.action, GuardianAssessmentAction::Command { .. }) =>{Some(assessment.status)}_ => None,}).await;- assert_eq!(status, GuardianAssessmentStatus::Aborted);+ let expected_status = match refreshed_outcome {+ GuardianAssessmentOutcome::Allow => GuardianAssessmentStatus::Approved,+ GuardianAssessmentOutcome::Deny => GuardianAssessmentStatus::Denied,+ };+ assert_eq!(status, expected_status);wait_for_event(&test.codex, |event| {matches!(event, EventMsg::TurnComplete(_))}).await;- assert!(!output_file.exists(), "stale allow executed the command");+ match refreshed_outcome {+ GuardianAssessmentOutcome::Allow => {+ let contents = test+ .fs()+ .read_file_text(&output_file, Default::default(), /*sandbox*/ None)+ .await?;+ assert_eq!(contents.lines().collect::<Vec<_>>(), vec!["executed"]);+ }+ GuardianAssessmentOutcome::Deny => {+ assert_eq!(+ test.fs()+ .get_metadata(&output_file, Default::default(), /*sandbox*/ None)+ .await+ .expect_err("denied action executed")+ .kind(),+ std::io::ErrorKind::NotFound,+ );+ }+ }+ assert_eq!(review_stream.requests().await.len(), 2);test.codex.shutdown_and_wait().await?;review_stream.shutdown().await;Ok(())