Lightweight coding agent that runs in your terminal
Rust
Flush completed Guardian reviews before delivering decisions (#46509)
jif36430b36881c
diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rsindex 60e61dc0ec..2875c5f9ad 100644--- a/codex-rs/core/src/session/mod.rs+++ b/codex-rs/core/src/session/mod.rs@@ -2291,6 +2291,14 @@ impl Session {.get::<GuardianReviewSessionManager>()}+ pub(crate) async fn is_private_guardian_reviewer(&self) -> bool {+ self.state+ .lock()+ .await+ .session_configuration+ .trusted_guardian_reviewer+ }+pub(crate) async fn emit_turn_started(&self, turn_context: &TurnContext) {let event = TurnStartedEvent {turn_id: turn_context.sub_id.clone(),@@ -2609,6 +2617,9 @@ impl Session {}async fn send_event_raw_with_persistence(&self, event: Event, persist: bool) {+ let flush_guardian_completion = persist+ && matches!(event.msg, EventMsg::TurnComplete(_))+ && self.is_private_guardian_reviewer().await;// Keep realtime reduction, canonical append, and delivery in the same order.// This lock must not acquire SessionState or ActiveTurn: event producers can// already hold those locks. Host presentation policies are synchronous.@@ -2648,6 +2659,11 @@ impl Session {{warn!("failed to persist realtime history: {error}");}+ // Save the completed review and its terminal event together before the parent sees+ // the decision. Turn completion skips its separate before/after barriers for this case.+ if flush_guardian_completion && let Err(err) = self.flush_rollout().await {+ warn!("failed to flush completed Guardian review: {err}");+ }self.deliver_event_raw(event).await;}diff --git a/codex-rs/core/src/tasks/mod.rs b/codex-rs/core/src/tasks/mod.rsindex 502739aea8..3caff155a6 100644--- a/codex-rs/core/src/tasks/mod.rs+++ b/codex-rs/core/src/tasks/mod.rs@@ -369,7 +369,13 @@ impl Session {.instrument(trace_span!("session_task.run")).await;let sess = Arc::clone(&session);- if let Err(err) = sess.flush_rollout().await {+ // Private reviewers save their transcript together with the terminal event.+ // Errors and cancellation retain their existing save path.+ if (!sess.is_private_guardian_reviewer().await+ || task_cancellation_token.is_cancelled()+ || task_result.is_err())+ && let Err(err) = sess.flush_rollout().await+ {warn!("failed to flush rollout before completing turn: {err}");sess.send_event(ctx_for_finish.as_ref(),@@ -824,7 +830,11 @@ impl Session {time_to_first_token_ms,})};- self.send_event(turn_context.as_ref(), event).await;+ let saved_guardian_completion =+ matches!(event, EventMsg::TurnComplete(_)) && self.is_private_guardian_reviewer().await;+ if !saved_guardian_completion {+ self.send_event(turn_context.as_ref(), event.clone()).await;+ }let cleared_active_turn = {let mut active = self.active_turn.lock().await;@@ -838,12 +848,16 @@ impl Session {false}};+ if saved_guardian_completion {+ // The parent can request another review as soon as it receives this event.+ self.send_event(turn_context.as_ref(), event).await;+ }if cleared_active_turn {self.emit_thread_idle_lifecycle_if_idle(idle_cause).await;}- // Regular items were flushed before this terminal event was appended; buffering- // thread writers may not flush it without another explicit barrier.- if let Err(err) = self.flush_rollout().await {+ // Private reviewers already flushed the terminal event before delivering it.+ // Other buffering writers still need a barrier for the terminal event.+ if !saved_guardian_completion && let Err(err) = self.flush_rollout().await {warn!("failed to flush rollout after emitting terminal turn event: {err}");}if cleared_active_turn {diff --git a/codex-rs/core/tests/suite/guardian_persistence_tests.rs b/codex-rs/core/tests/suite/guardian_persistence_tests.rsnew file mode 100644index 0000000000..f8fec99fb8--- /dev/null+++ b/codex-rs/core/tests/suite/guardian_persistence_tests.rs@@ -0,0 +1,234 @@+//! Checks the save boundary between a private Guardian decision and the reviewed action.++use codex_core::TurnInputRequest;+use codex_core::config::Constrained;+use codex_history::RolloutItem;+use codex_protocol::ThreadId;+use codex_protocol::config_types::ApprovalsReviewer;+use codex_protocol::models::ResponseItem;+use codex_protocol::protocol::AskForApproval;+use codex_protocol::protocol::EventMsg;+use codex_protocol::protocol::ThreadHistoryMode;+use codex_protocol::protocol::ThreadSource;+use codex_protocol::user_input::UserInput;+use codex_thread_store::AppendThreadItemsParams;+use codex_thread_store::ArchiveThreadParams;+use codex_thread_store::CreateThreadParams;+use codex_thread_store::DeleteThreadParams;+use codex_thread_store::InMemoryThreadStore;+use codex_thread_store::ListThreadsParams;+use codex_thread_store::LoadThreadHistoryParams;+use codex_thread_store::PersistContext;+use codex_thread_store::ReadThreadByRolloutPathParams;+use codex_thread_store::ReadThreadParams;+use codex_thread_store::ResumeThreadParams;+use codex_thread_store::StoredThread;+use codex_thread_store::StoredThreadHistory;+use codex_thread_store::ThreadPage;+use codex_thread_store::ThreadStore;+use codex_thread_store::ThreadStoreFuture;+use codex_thread_store::UpdateThreadMetadataParams;+use core_test_support::responses;+use core_test_support::test_codex::test_codex;+use core_test_support::wait_for_event;+use pretty_assertions::assert_eq;+use serde_json::json;+use std::any::Any;+use std::sync::Arc;+use std::time::Duration;+use tokio::sync::Mutex;+use tokio::sync::mpsc;+use tokio::sync::oneshot;+use tokio::time::timeout;++struct PendingSave {+ history: StoredThreadHistory,+ complete: oneshot::Sender<()>,+}++#[derive(Default)]+struct ReviewerSaves {+ thread_id: Option<ThreadId>,+ reviews: usize,+}++struct GatedReviewerStore {+ inner: InMemoryThreadStore,+ reviewer: Mutex<ReviewerSaves>,+ saves: mpsc::UnboundedSender<PendingSave>,+}++macro_rules! delegate_store_methods {+ ($(fn $name:ident($param:ident: $params:ty) -> $result:ty;)*) => {+ $(fn $name(&self, $param: $params) -> ThreadStoreFuture<'_, $result> {+ ThreadStore::$name(&self.inner, $param)+ })*+ };+}++impl ThreadStore for GatedReviewerStore {+ fn as_any(&self) -> &dyn Any {+ self+ }++ delegate_store_methods! {+ fn resume_thread(params: ResumeThreadParams) -> ();+ fn append_items(params: AppendThreadItemsParams) -> ();+ fn discard_thread(thread_id: ThreadId) -> ();+ fn load_history(params: LoadThreadHistoryParams) -> StoredThreadHistory;+ fn read_thread(params: ReadThreadParams) -> StoredThread;+ fn read_thread_by_rollout_path(params: ReadThreadByRolloutPathParams) -> StoredThread;+ fn list_threads(params: ListThreadsParams) -> ThreadPage;+ fn update_thread_metadata(params: UpdateThreadMetadataParams) -> Option<StoredThread>;+ fn archive_thread(params: ArchiveThreadParams) -> ();+ fn unarchive_thread(params: ArchiveThreadParams) -> StoredThread;+ fn delete_thread(params: DeleteThreadParams) -> ();+ fn shutdown_thread(thread_id: ThreadId) -> ();+ }++ fn create_thread(&self, params: CreateThreadParams) -> ThreadStoreFuture<'_, ()> {+ Box::pin(async move {+ if params.thread_source == Some(ThreadSource::GuardianReview) {+ self.reviewer.lock().await.thread_id = Some(params.thread_id);+ }+ ThreadStore::create_thread(&self.inner, params).await+ })+ }++ fn persist_thread(+ &self,+ thread_id: ThreadId,+ context: PersistContext,+ ) -> ThreadStoreFuture<'_, ()> {+ self.inner.persist_thread(thread_id, context)+ }++ fn flush_thread(&self, thread_id: ThreadId) -> ThreadStoreFuture<'_, ()> {+ Box::pin(async move {+ let is_reviewer = self.reviewer.lock().await.thread_id == Some(thread_id);+ let pending = if is_reviewer {+ let history = ThreadStore::load_history(+ &self.inner,+ LoadThreadHistoryParams {+ thread_id,+ include_archived: true,+ },+ )+ .await?;+ let reviews = history.items.iter().filter(|item| matches!(+ item, RolloutItem::ResponseItem(item)+ if matches!(&item.item, ResponseItem::Message { role, .. } if role == "assistant")+ )).count();+ let mut reviewer = self.reviewer.lock().await;+ if reviews > reviewer.reviews {+ reviewer.reviews = reviews;+ Some(history)+ } else {+ None+ }+ } else {+ None+ };+ if let Some(history) = pending {+ let (complete, completed) = oneshot::channel();+ self.saves+ .send(PendingSave { history, complete })+ .expect("save receiver");+ completed.await.expect("release the reviewer save");+ }+ self.inner.flush_thread(thread_id).await+ })+ }+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn guardian_saves_each_completed_review_before_releasing_its_action() -> anyhow::Result<()> {+ core_test_support::skip_if_no_network!(Ok(()));+ core_test_support::skip_if_wine_exec!(Ok(()), "uses the Unix command approval harness");+ let server = responses::start_mock_server().await;+ let (saves, mut pending_saves) = mpsc::unbounded_channel();+ let store = Arc::new(GatedReviewerStore {+ inner: InMemoryThreadStore::default(),+ reviewer: Mutex::new(ReviewerSaves::default()),+ saves,+ });+ let test = test_codex()+ .with_thread_store(store)+ .with_history_mode(ThreadHistoryMode::Legacy)+ .with_config(|config| {+ config.permissions.approval_policy = Constrained::allow_any(AskForApproval::OnRequest);+ config.approvals_reviewer = ApprovalsReviewer::AutoReview;+ })+ .build_with_auto_env(&server)+ .await?;+ let command = json!({+ "cmd": "true", "sandbox_permissions": "require_escalated",+ "justification": "Run the authorized command.",+ })+ .to_string();+ let approval = r#"{"risk_level":"low","user_authorization":"high","outcome":"allow"}"#;+ let mut turns = Vec::new();+ for index in 0..2 {+ turns.push(responses::sse(vec![+ responses::ev_function_call(&format!("command-{index}"), "exec_command", &command),+ responses::ev_completed(&format!("parent-{index}")),+ ]));+ turns.push(responses::sse(vec![+ responses::ev_assistant_message(&format!("review-{index}"), approval),+ responses::ev_completed(&format!("guardian-{index}")),+ ]));+ }+ turns.push(responses::sse(vec![responses::ev_completed("parent-done")]));+ let mock = responses::mount_sse_sequence(&server, turns).await;+ test.codex+ .start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {+ text: "Run both commands.".to_owned(),+ text_elements: Vec::new(),+ }]))+ .await?;++ let mut reviewer_id = None;+ for review in 1..=2 {+ let pending = timeout(Duration::from_secs(10), pending_saves.recv())+ .await?+ .expect("review save");+ assert_eq!(+ *reviewer_id.get_or_insert(pending.history.thread_id),+ pending.history.thread_id+ );+ assert_eq!(+ pending+ .history+ .items+ .iter()+ .filter(|item| matches!(item, RolloutItem::EventMsg(EventMsg::TurnComplete(_))))+ .count(),+ review,+ "the first save after inference must include turn completion"+ );+ // The parent cannot consume the approval while its review is still being saved.+ assert!(+ timeout(+ Duration::from_millis(50),+ wait_for_event(&test.codex, |event| {+ matches!(event, EventMsg::ExecCommandBegin(_))+ })+ )+ .await+ .is_err()+ );+ assert_eq!(mock.requests().len(), review * 2);+ pending.complete.send(()).expect("finish save");+ wait_for_event(&test.codex, |event| {+ matches!(event, EventMsg::ExecCommandEnd(_))+ })+ .await;+ }+ wait_for_event(&test.codex, |event| {+ matches!(event, EventMsg::TurnComplete(_))+ })+ .await;+ assert_eq!(mock.requests().len(), 5);+ test.codex.shutdown_and_wait().await?;+ Ok(())+}diff --git a/codex-rs/core/tests/suite/mod.rs b/codex-rs/core/tests/suite/mod.rsindex 52c92e77be..2e0e3914d9 100644--- a/codex-rs/core/tests/suite/mod.rs+++ b/codex-rs/core/tests/suite/mod.rs@@ -85,6 +85,9 @@ mod gateway_auth;mod guardian_context_budget;mod guardian_history;mod guardian_mcp_elicitation;+#[cfg(not(target_os = "windows"))]+#[path = "guardian_persistence_tests.rs"]+mod guardian_persistence;mod guardian_retained_context;mod guardian_retry;#[cfg(not(target_os = "windows"))]