Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Return async Guardian approvals before review cleanup finishes (#54039)

jif538858165396
diff --git a/codex-rs/ext/guardian-reviewer/src/outcome.rs b/codex-rs/ext/guardian-reviewer/src/outcome.rs
index 9f9aca5918..738ae555f2 100644
--- a/codex-rs/ext/guardian-reviewer/src/outcome.rs
+++ b/codex-rs/ext/guardian-reviewer/src/outcome.rs
@@ -7,7 +7,7 @@ use tokio::time::Instant;
#[derive(Debug)]
pub enum GuardianReviewOutcome {
- /// Approved by current async evidence after the redundant attempt was drained.
+ /// Approved by current async evidence; the redundant attempt is cancelled and drained.
CachedApproval,
Completed(GuardianAssessment),
Error(GuardianReviewError),
diff --git a/codex-rs/ext/guardian-reviewer/src/pool_tests.rs b/codex-rs/ext/guardian-reviewer/src/pool_tests.rs
index 8db04a23c3..c392e2301f 100644
--- a/codex-rs/ext/guardian-reviewer/src/pool_tests.rs
+++ b/codex-rs/ext/guardian-reviewer/src/pool_tests.rs
@@ -3,6 +3,81 @@ use pretty_assertions::assert_eq;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
+#[tokio::test]
+async fn tracked_review_stays_reserved_until_cleanup_finishes() {
+ let runtime = Arc::new(ReviewerTasks::default());
+ let next_id = Arc::new(AtomicUsize::new(/*v*/ 0));
+ let pool = Arc::new(ReviewerPool::new(
+ Arc::clone(&runtime),
+ move |_, _, _, _, _| {
+ let id = next_id.fetch_add(/*val*/ 1, Ordering::SeqCst);
+ Box::pin(async move { Ok(Session(id)) })
+ },
+ ));
+ let (selected, mut selections) = tokio::sync::mpsc::unbounded_channel();
+ let release = Arc::new(Semaphore::new(/*permits*/ 0));
+ let (finished, done) = tokio::sync::oneshot::channel();
+ let pending = {
+ let pool = Arc::clone(&pool);
+ let request = Request {
+ fresh: false,
+ selected: selected.clone(),
+ release: Some(Arc::clone(&release)),
+ };
+ runtime.tasks.spawn(async move {
+ pool.review(request).await;
+ finished.send(()).unwrap();
+ })
+ };
+ assert_eq!(selections.recv().await, Some(0));
+ drop(pending);
+ assert_eq!(runtime.tasks.len(), 1);
+
+ // A concurrent request must fork while the original attempt is still draining.
+ pool.review(Request {
+ fresh: false,
+ selected: selected.clone(),
+ release: None,
+ })
+ .await;
+ assert_eq!(selections.recv().await, Some(1));
+ release.add_permits(/*n*/ 1);
+ done.await.unwrap();
+ pool.review(Request {
+ fresh: false,
+ selected: selected.clone(),
+ release: None,
+ })
+ .await;
+ assert_eq!(selections.recv().await, Some(0));
+
+ // Shutdown must also join an attempt whose caller has stopped waiting.
+ let release = Arc::new(Semaphore::new(/*permits*/ 0));
+ let pending = {
+ let pool = Arc::clone(&pool);
+ let request = Request {
+ fresh: false,
+ selected,
+ release: Some(Arc::clone(&release)),
+ };
+ runtime.tasks.spawn(async move {
+ pool.review(request).await;
+ })
+ };
+ assert_eq!(selections.recv().await, Some(0));
+ drop(pending);
+ let shutdown = pool.shutdown();
+ tokio::pin!(shutdown);
+ tokio::select! {
+ biased;
+ () = &mut shutdown => panic!("shutdown must join the pending attempt"),
+ () = std::future::ready(()) => {}
+ }
+ release.add_permits(/*n*/ 1);
+ shutdown.await;
+ assert_eq!(runtime.tasks.len(), 0);
+}
+
struct Session(usize);
impl ReviewerSession for Session {
diff --git a/codex-rs/ext/guardian-reviewer/src/review.rs b/codex-rs/ext/guardian-reviewer/src/review.rs
index 10e3e65fd0..a2625ebbd7 100644
--- a/codex-rs/ext/guardian-reviewer/src/review.rs
+++ b/codex-rs/ext/guardian-reviewer/src/review.rs
@@ -1,5 +1,6 @@
//! Owns synchronous review orchestration, reporting and outcome accounting.
//! The host binds the original action, captures evidence and enforces authorization.
+//! Async approvals can return while cancelled attempts finish under the parent's task tracker.
use crate::GuardianReviewError;
use crate::GuardianReviewOutcome;
@@ -21,16 +22,20 @@ use codex_protocol::protocol::ReviewDecision;
use codex_protocol::protocol::WarningEvent;
use std::future::Future;
use std::sync::Arc;
+use std::sync::atomic::AtomicI64;
+use std::sync::atomic::Ordering;
use tokio::time::Instant;
use tokio_util::sync::CancellationToken;
/// Operations bound to one immutable action and its issuing context.
/// Hosts validate authority, publish supplied events and interrupt only the selected turn;
/// they do not select Guardian outcomes, retry policy or reporting effects.
-pub trait ReviewHost: Send + Sync {
- type Prepared: Send + Sync;
+/// Clones retain the same action and context. A cancelled attempt may keep its host and
+/// prepared state alive under the parent's task tracker after approval returns.
+pub trait ReviewHost: Clone + Send + Sync + 'static {
+ type Prepared: Send + Sync + 'static;
/// Evidence captured for one completed assessment, never reused by another attempt.
- type Evidence: Send;
+ type Evidence: Send + 'static;
/// Returns the target's rules, or no evidence when they cannot be resolved.
fn permissions(&self) -> Option<codex_guardian_context::PermissionContext>;
/// Captures the turn currently servicing reviews, which may differ from a yielded cell's origin.
@@ -106,24 +111,49 @@ impl<H: ReviewHost> SynchronousApprovalReviewer for ReviewRequest<'_, H> {
self.host
.emit(EventMsg::GuardianAssessment(report.started_event()))
.await;
+ let prepared = Arc::new(prepared);
let cancellation = self.cancellation.child_token();
- let mut review = Box::pin(crate::run_with_retry(
- GuardianReviewSessionLimits {
- max_attempts: crate::MAX_REVIEW_ATTEMPTS,
- deadline,
- },
- Some(&cancellation),
- |deadline| self.host.attempt(&prepared, deadline, &cancellation),
- ));
+ let attempts = Arc::new(AtomicI64::new(/*v*/ 0));
+ let mut review = Box::pin({
+ let host = self.host.clone();
+ let prepared = Arc::clone(&prepared);
+ let cancellation = cancellation.clone();
+ let attempts = Arc::clone(&attempts);
+ async move {
+ crate::run_with_retry(
+ GuardianReviewSessionLimits {
+ max_attempts: crate::MAX_REVIEW_ATTEMPTS,
+ deadline,
+ },
+ Some(&cancellation),
+ |deadline| {
+ attempts.fetch_add(/*val*/ 1, Ordering::Relaxed);
+ host.attempt(&prepared, deadline, &cancellation)
+ },
+ )
+ .await
+ }
+ });
let (outcome, analytics, evidence) = if let Some(async_approval) = async_approval {
tokio::select! {
biased;
result = &mut review => result,
() = async_approval => {
- // The async approval won. Finish cancellation through the existing
- // reviewer cleanup; the retry loop is never restarted.
cancellation.cancel();
- let (_, analytics, _) = review.await;
+ let analytics = if let Some(runtime) = self.thread_store.get::<crate::ReviewerTasks>() {
+ // Keep driving the same attempt: its pool reservation stays held
+ // until it drains, and parent shutdown joins the tracked cleanup.
+ runtime.tasks.spawn(async move { let _ = review.await; });
+ // Final session and usage metadata are not available yet.
+ GuardianReviewAnalyticsResult {
+ attempt_count: attempts.load(Ordering::Relaxed),
+ ..GuardianReviewAnalyticsResult::without_session()
+ }
+ } else {
+ // Hosts without a lifetime tracker must finish cleanup here.
+ let (_, analytics, _) = review.await;
+ analytics
+ };
let outcome = if !self.host.cached_approval_is_current(&prepared).await
|| self.cancellation.is_cancelled() {
GuardianReviewOutcome::Error(GuardianReviewError::Cancelled)
@@ -207,3 +237,7 @@ impl<H: ReviewHost> SynchronousApprovalReviewer for ReviewRequest<'_, H> {
})
}
}
+
+#[cfg(test)]
+#[path = "review_tests.rs"]
+mod tests;
diff --git a/codex-rs/ext/guardian-reviewer/src/review_tests.rs b/codex-rs/ext/guardian-reviewer/src/review_tests.rs
new file mode 100644
index 0000000000..f4660f048e
--- /dev/null
+++ b/codex-rs/ext/guardian-reviewer/src/review_tests.rs
@@ -0,0 +1,216 @@
+//! Exercises async approval and authorization at the host boundary while cleanup is gated.
+
+use super::*;
+use crate::ReviewMetadata;
+use codex_analytics::AnalyticsEventsClient;
+use codex_analytics::GuardianApprovalRequestSource;
+use codex_analytics::GuardianReviewedAction;
+use codex_extension_api::ExtensionData;
+use codex_otel::SessionTelemetry;
+use codex_protocol::ThreadId;
+use codex_protocol::approvals::GuardianAssessmentAction;
+use codex_protocol::config_types::ApprovalsReviewer;
+use codex_protocol::items::ModelInvocationContext;
+use codex_protocol::openai_models::GuardianScope;
+use codex_protocol::protocol::AskForApproval;
+use codex_protocol::protocol::SessionSource;
+use pretty_assertions::assert_eq;
+use serde_json::json;
+use std::sync::atomic::AtomicBool;
+use std::sync::atomic::AtomicUsize;
+use std::sync::atomic::Ordering;
+use tokio::sync::Notify;
+use tokio::sync::Semaphore;
+
+#[derive(Clone)]
+struct GatedHost {
+ started: Arc<Notify>,
+ cleaning: Arc<Notify>,
+ cleanup: Arc<Semaphore>,
+ cleaned: Arc<AtomicBool>,
+ evidence_recorded: Arc<AtomicBool>,
+ current: bool,
+ validations: Arc<AtomicUsize>,
+}
+
+impl ReviewHost for GatedHost {
+ type Prepared = ();
+ type Evidence = ();
+
+ fn permissions(&self) -> Option<codex_guardian_context::PermissionContext> {
+ None
+ }
+ async fn servicing_turn(&self) -> Option<(String, Arc<ModelInfo>)> {
+ None
+ }
+ fn validate_action(&self) -> Result<(&str, Option<&str>), ReviewDecision> {
+ Ok(("turn", Some("call")))
+ }
+ async fn prepare(
+ &self,
+ approval_id: &str,
+ reason: GuardianReviewReason,
+ _: Instant,
+ _: &CancellationToken,
+ ) -> Result<((), ReviewReport), ReviewDecision> {
+ Ok((
+ (),
+ ReviewReport::new(ReviewMetadata {
+ thread_id: "thread".into(),
+ turn_id: "turn".into(),
+ review_id: approval_id.into(),
+ target_item_id: Some("call".into()),
+ plugin_id: None,
+ script_path: None,
+ approval_request_source: GuardianApprovalRequestSource::MainTurn,
+ reviewed_action: GuardianReviewedAction::RequestPermissions {},
+ action: GuardianAssessmentAction::RequestPermissions {
+ reason: None,
+ permissions: Default::default(),
+ },
+ review_reason: reason,
+ model_context: ModelInvocationContext {
+ model_slug: "test".into(),
+ reasoning_effort: None,
+ },
+ }),
+ ))
+ }
+ async fn attempt(
+ &self,
+ _: &(),
+ _: Instant,
+ cancellation: &CancellationToken,
+ ) -> (
+ GuardianReviewOutcome,
+ GuardianReviewAnalyticsResult,
+ Option<()>,
+ ) {
+ self.started.notify_one();
+ cancellation.cancelled().await;
+ self.cleaning.notify_one();
+ let _permit = self.cleanup.acquire().await.unwrap();
+ self.cleaned.store(true, Ordering::SeqCst);
+ // A terminal verdict racing with cancellation must not publish late evidence.
+ (
+ GuardianReviewOutcome::Completed(
+ crate::parse_guardian_assessment(r#"{"outcome":"deny"}"#).unwrap(),
+ ),
+ GuardianReviewAnalyticsResult::without_session(),
+ Some(()),
+ )
+ }
+ async fn cached_approval_is_current(&self, _: &()) -> bool {
+ self.validations.fetch_add(/*val*/ 1, Ordering::SeqCst);
+ self.current
+ }
+ async fn emit(&self, _: EventMsg) {}
+ async fn record_evidence(&self, _: &(), _: (), _: &GuardianAssessmentEvent) {
+ self.evidence_recorded.store(true, Ordering::SeqCst);
+ }
+ async fn interrupt(&self, _: &str, _: EventMsg, _: ErrorEvent) {
+ panic!("cached approval must not interrupt the parent");
+ }
+}
+
+#[derive(Clone, Copy)]
+enum ApprovalState {
+ Current,
+ Invalidated,
+ Cancelled,
+}
+
+#[test_case::test_case(ApprovalState::Current; "current action")]
+#[test_case::test_case(ApprovalState::Invalidated; "invalidated action")]
+#[test_case::test_case(ApprovalState::Cancelled; "cancelled action")]
+#[tokio::test]
+async fn async_approval_revalidates_without_waiting_for_tracked_cleanup(state: ApprovalState) {
+ let host = GatedHost {
+ started: Arc::new(Notify::new()),
+ cleaning: Arc::new(Notify::new()),
+ cleanup: Arc::new(Semaphore::new(/*permits*/ 0)),
+ cleaned: Arc::new(AtomicBool::new(/*v*/ false)),
+ evidence_recorded: Arc::new(AtomicBool::new(/*v*/ false)),
+ current: !matches!(state, ApprovalState::Invalidated),
+ validations: Arc::new(AtomicUsize::new(/*v*/ 0)),
+ };
+ let thread_id = ThreadId::new();
+ let store = ExtensionData::new(thread_id.to_string());
+ let runtime = store.get_or_init(crate::ReviewerTasks::default);
+ let model: ModelInfo = serde_json::from_value(json!({
+ "slug":"test", "display_name":"test", "supported_reasoning_levels":[],
+ "shell_type":"unified_exec", "visibility":"list", "supported_in_api":true,
+ "priority":0, "support_verbosity":false,
+ "truncation_policy":{"mode":"bytes","limit":10000}, "experimental_supported_tools":[]
+ }))
+ .unwrap();
+ let telemetry = SessionTelemetry::new(
+ thread_id,
+ "test",
+ "test",
+ /*account_id*/ None,
+ /*account_email*/ None,
+ /*auth_mode*/ None,
+ "test".into(),
+ /*log_user_prompts*/ false,
+ "test".into(),
+ SessionSource::Exec,
+ );
+ let analytics = AnalyticsEventsClient::disabled();
+ let action = json!({});
+ let request = ReviewRequest {
+ host: host.clone(),
+ approval_id: "approval",
+ tool_call_id: Some("call"),
+ action: Some(&action),
+ thread_id,
+ thread_store: &store,
+ category: GuardianScope::Permissions,
+ approval_policy: AskForApproval::OnRequest,
+ approvals_reviewer: ApprovalsReviewer::AutoReview,
+ require_guardian: false,
+ require_synchronous_review: false,
+ model_requires_review: false,
+ async_enabled: true,
+ retried: false,
+ escalated_exec: false,
+ full_access: false,
+ cancellation: CancellationToken::new(),
+ model: &model,
+ telemetry: &telemetry,
+ log_assessments: false,
+ analytics: &analytics,
+ metrics: None,
+ };
+ let decision = request.review(
+ GuardianReviewReason::MissingScore,
+ Some(Box::pin(async {
+ host.started.notified().await;
+ if matches!(state, ApprovalState::Cancelled) {
+ request.cancellation.cancel();
+ }
+ })),
+ );
+ tokio::pin!(decision);
+ let decision = tokio::select! {
+ biased;
+ decision = &mut decision => decision,
+ () = host.cleaning.notified() => panic!("async decision waited for the cancelled review to drain"),
+ };
+ assert_eq!(
+ decision,
+ Some(match state {
+ ApprovalState::Current => ReviewDecision::Approved,
+ ApprovalState::Invalidated | ApprovalState::Cancelled => ReviewDecision::Abort,
+ })
+ );
+ assert_eq!(host.validations.load(Ordering::SeqCst), 1);
+ host.cleaning.notified().await;
+ assert!(!host.cleaned.load(Ordering::SeqCst));
+ assert_eq!(runtime.tasks.len(), 1);
+ runtime.tasks.close();
+ host.cleanup.add_permits(/*n*/ 1);
+ runtime.tasks.wait().await;
+ assert!(host.cleaned.load(Ordering::SeqCst));
+ assert!(!host.evidence_recorded.load(Ordering::SeqCst));
+}