diff --git a/codex-rs/ext/guardian-v2/BUILD.bazel b/codex-rs/ext/guardian-v2/BUILD.bazel index e10e27c050..c573d611cd 100644 --- a/codex-rs/ext/guardian-v2/BUILD.bazel +++ b/codex-rs/ext/guardian-v2/BUILD.bazel @@ -11,4 +11,5 @@ codex_rust_crate( crate_name = "codex_guardian_v2", extra_binaries = ["//codex-rs/code-mode-host:codex-code-mode-host"], test_data_extra = glob(["src/**/snapshots/**"]), + unit_test_srcs = ["src/async_scorer/conversation_admission_tests.rs"], ) diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/approval.rs b/codex-rs/ext/guardian-v2/src/async_scorer/approval.rs index dedf89cd92..4656b551e6 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/approval.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/approval.rs @@ -4,11 +4,11 @@ use super::authorization::ScoreAuthorization; use super::config::GuardianV2Config; +use super::conversation::ConversationBackend; use super::metrics::TOOL_CALL_LAG_METRIC; use super::metrics::record_fast_decision; use super::parent_compaction::select_parent_compaction; use super::sampler::LunaSampler; -use super::score::GuardianV2ScoreProgress; use codex_core::CodexThread; use codex_core::ThreadManager; use codex_core::context::GuardianContextMode; @@ -144,10 +144,11 @@ impl GuardianApprovalReviewer { if mode == GuardianReviewMode::Adaptive && !input.require_fresh_review && let Some(config) = guardian_config.as_ref() - && let Some(progress) = input.thread_store.get::() + && let Some(cache) = + ConversationBackend::for_turn(input.thread_store, input.turn_store) { // Subscribe before checking so a concurrently published score is not lost. - let mut updates = progress.updates.subscribe(); + let mut updates = cache.progress.updates.subscribe(); loop { if let Ok(true) = cached_evidence(thread, input, config, &policy, /*metrics*/ None).await @@ -207,7 +208,7 @@ async fn cached_evidence( metrics: Option<&dyn ExtensionMetrics>, ) -> Result { let store = input.thread_store; - let Some(progress) = store.get::() else { + let Some(cache) = ConversationBackend::for_turn(store, input.turn_store) else { if let Some(metrics) = metrics { metrics.counter( "codex.guardian_v2.score_readiness", @@ -225,7 +226,7 @@ async fn cached_evidence( .is_ok_and(|action| action.len().saturating_add(1) <= max_action_bytes); let history = thread.conversation_history_snapshot().await; let permitted = constrained_live_policy(thread, store, policy).await; - let cached = progress.inspect(input.tool_call_id); + let cached = cache.progress.inspect(input.tool_call_id); if let Some(metrics) = metrics { metrics.counter( "codex.guardian_v2.score_readiness", @@ -295,7 +296,7 @@ async fn cached_evidence( let current = ScoreAuthorization::current(thread, permissions).await; // Classification may publish or fail while authorization and live policy are collected. let permitted = constrained_live_policy(thread, store, policy).await; - let cached = progress.inspect(input.tool_call_id); + let cached = cache.progress.inspect(input.tool_call_id); if let Some(metrics) = metrics && let Some(sampled_at) = cached.sampled_at { @@ -329,6 +330,10 @@ async fn cached_evidence( // Reuse the latest thread score within the lag limit, even across categories // and while the current action's async score is still in flight. let (reason, label) = match cached.action_risk { + None if cached.has_unscored_failure => { + (GuardianReviewReason::ScoringFailure, "scoring_failure") + } + None => (GuardianReviewReason::MissingScore, "missing_score"), _ if lag > config.max_tool_call_lag => { if let Some(metrics) = metrics { metrics.counter( @@ -342,7 +347,6 @@ async fn cached_evidence( _ if cached.has_unscored_failure => { (GuardianReviewReason::ScoringFailure, "scoring_failure") } - None => (GuardianReviewReason::MissingScore, "missing_score"), Some(score) if score < config.review_threshold => { if cached.authorization.as_ref() != Some(¤t) { ( diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/budget_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/budget_tests.rs index 9b6d245810..6d07b9cdbc 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/budget_tests.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/budget_tests.rs @@ -1,5 +1,7 @@ //! Contributor integration tests use the normal catalog-backed lifecycle setup. +use crate::async_scorer::conversation::ConversationBackend; + use super::*; use crate::async_scorer::authorization::ScoreAuthorization; use codex_protocol::models::ImageReference; @@ -210,7 +212,9 @@ async fn assert_catalog_budget(evidence: BudgetEvidence) -> Result<()> { sampled_at: None, }, ); - let progress = thread_store.get::().unwrap(); + let progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); let authorization = ScoreAuthorization::current(&fixture.test.codex, &Default::default()).await; seed_cached_score(&progress, thread_store, /*index*/ 0, authorization); @@ -253,7 +257,7 @@ async fn assert_catalog_budget(evidence: BudgetEvidence) -> Result<()> { // Raw injection supplies no live checkpoint provenance, regardless of the sample. "incompatible_compaction" } else { - "elevated_risk" + "scoring_failure" }; fixture.assert_fails_closed(reason).await?; assert!( @@ -267,7 +271,9 @@ async fn assert_catalog_budget(evidence: BudgetEvidence) -> Result<()> { fixture.test.codex.shutdown_and_wait().await?; continue; } - let progress = thread_store.get::().unwrap(); + let progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { while progress.inspect(/*call_id*/ None).lag > 0 { tokio::task::yield_now().await; diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/classification.rs b/codex-rs/ext/guardian-v2/src/async_scorer/classification.rs index 8ee74c543f..117004013b 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/classification.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/classification.rs @@ -145,6 +145,7 @@ impl Classification { score_authorization, } = self; let generation = reservation.generation.clone(); + let active = Arc::clone(&reservation.active); let pending_score = PendingScore { progress: Arc::clone(&score_progress), index: tool_call_index, @@ -266,7 +267,7 @@ impl Classification { let evidence = match transcript { Ok(transcript) => transcript, Err(error) => { - score_progress.fail_closed(sampled_at); + score_progress.fail_closed(sampled_at, tool_call_index); record_classification( metrics.as_deref(), context_mode, @@ -393,6 +394,7 @@ impl Classification { sampled_at: Some(sampled_at.into()), }; if !score_authorization.is_current(&thread).await + || !active.load(std::sync::atomic::Ordering::Acquire) || generation.strong_count() == 0 { return Ok(ClassificationOutcome::Superseded); @@ -433,7 +435,7 @@ impl Classification { } .await; if result.is_err() { - score_progress.fail_closed(sampled_at); + score_progress.fail_closed(sampled_at, tool_call_index); } let duration = classification_finished_at .map(|finished_at: Instant| finished_at.duration_since(classification_started_at)) diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/conversation.rs b/codex-rs/ext/guardian-v2/src/async_scorer/conversation.rs index 581175c371..b0d1fd7a6c 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/conversation.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/conversation.rs @@ -1,8 +1,10 @@ +//! Owns policy-scoped score progress and retained classifier conversations. //! Concurrent classifications fork the latest compatible completed conversation. //! Admission assigns order and reserves capacity atomically before async preparation. //! Only newer completions replace history. //! Permits bound preparation and sampling through full response completion. -//! Backend replacement invalidates its generation. +//! Policy caches isolate history and generations while sharing admission and action progress. +//! Thread stop retires every retained policy backend. //! Oversized retained requests rebuild from fresh evidence before the model's hard limit. //! Preparation exposes the complete input without starting model execution. //! Each request owns its fork; failed or cancelled requests leave committed history intact. @@ -13,10 +15,14 @@ use std::sync::Arc; use std::sync::Mutex; use std::sync::Weak; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; use std::time::SystemTime; use codex_context_fragments::RenderedFragment; use codex_core::CodexThread; +use codex_core::config::Config; +use codex_extension_api::ExtensionData; use codex_extension_api::ExtensionMetrics; use codex_guardian_reviewer::ConversationState; use codex_history::ResponseItemEnvelope; @@ -32,16 +38,20 @@ use super::sampler::LunaSampler; use super::sampler::LunaSamplerError; use super::sampler::LunaSamplingRequest; use super::sampler::SampledScore; +use super::score::GuardianV2ScoreProgress; use super::score::PendingScore; use super::transcript::CollectedTranscript; const MAX_OUTSTANDING: usize = 16; pub(super) struct ConversationBackend { + revision: u64, + pub(super) progress: Arc, history: Arc>>>, sampler: Arc, - capacity: Mutex>, + capacity: Arc>>, generation: Arc<()>, + active: Arc, } struct CompletedConversation { @@ -56,6 +66,7 @@ pub(super) struct Reservation { sampler: Arc, _permit: OwnedSemaphorePermit, pub(super) generation: Weak<()>, + pub(super) active: Arc, } pub(super) struct ConversationRequest { @@ -93,15 +104,98 @@ struct ReuseKey { } impl ConversationBackend { - pub(super) fn new(sampler: Arc) -> Self { + pub(super) fn new( + sampler: Arc, + revision: u64, + metrics: Option>, + ) -> Self { Self { + revision, + progress: Arc::new(GuardianV2ScoreProgress::new(metrics, Arc::default())), history: Arc::default(), sampler, - capacity: Mutex::new(Arc::new(Semaphore::new(MAX_OUTSTANDING))), + capacity: Arc::new(Mutex::new(Arc::new(Semaphore::new(MAX_OUTSTANDING)))), generation: Arc::new(()), + active: Arc::new(AtomicBool::new(/*v*/ true)), } } + pub(super) fn install(thread: &ExtensionData, backend: Self) { + Self::stop(thread); + thread.insert(Mutex::new(Arc::new(backend))); + } + + pub(super) fn stop(thread: &ExtensionData) { + if let Some(owner) = thread.remove::>>() { + owner + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .active + .store(/*val*/ false, Ordering::Release); + } + } + + pub(super) fn is_current_for_thread(&self, thread: &ExtensionData) -> bool { + thread.get::>>().is_some_and(|owner| { + let current = owner + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + std::ptr::eq(self, current.as_ref()) + }) + } + + pub(super) fn for_turn(thread: &ExtensionData, turn: &ExtensionData) -> Option> { + let config = turn.get::()?; + if let Some(backend) = turn.get::() + && backend.revision == config.guardian_policy_revision + { + return backend.is_active().then_some(backend); + } + let owner = thread.get::>>()?; + let mut latest = owner + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + if !latest.is_active() { + return None; + } + // Serialize first use so all producers and consumers of one turn share its backend. + if let Some(backend) = turn.get::() + && backend.revision == config.guardian_policy_revision + { + return Some(backend); + } + let backend = if latest.revision == config.guardian_policy_revision { + Arc::clone(&latest) + } else { + Arc::new(latest.for_policy(config.guardian_policy_revision)) + }; + if backend.revision > latest.revision { + *latest = Arc::clone(&backend); + } + turn.insert_arc(Arc::clone(&backend)); + Some(backend) + } + + /// Starts fresh score and history state while preserving the thread's admission limit. + pub(super) fn for_policy(&self, revision: u64) -> Self { + Self { + revision, + progress: Arc::new(GuardianV2ScoreProgress::new( + self.progress.metrics.clone(), + Arc::clone(&self.progress.actions), + )), + history: Arc::default(), + sampler: Arc::clone(&self.sampler), + capacity: Arc::clone(&self.capacity), + generation: Arc::new(()), + active: Arc::clone(&self.active), + } + } + + fn is_active(&self) -> bool { + self.active.load(Ordering::Acquire) + } + pub(super) fn reserve( &self, observe: impl FnOnce() -> usize, @@ -122,6 +216,7 @@ impl ConversationBackend { sampler: Arc::clone(&self.sampler), _permit: permit, generation: Arc::downgrade(&self.generation), + active: Arc::clone(&self.active), }) })(); (index, reservation) @@ -134,7 +229,8 @@ impl Reservation { mut request: ConversationRequest, ) -> Option { request.sampling.pending_score = Some(request.pending_score.clone()); - if self.generation.strong_count() == 0 + if !self.active.load(Ordering::Acquire) + || self.generation.strong_count() == 0 || !request.authorization.is_current(&request.thread).await || request.pending_score.is_expired() { @@ -223,6 +319,7 @@ impl PreparedConversation { .is_some_and(|history| history.index > self.reservation.index); // Keep requests that can still contribute either a score or retained history. if !authorization_is_current + || !self.reservation.active.load(Ordering::Acquire) || self.reservation.generation.strong_count() == 0 || self.pending_score.is_expired() || (history_superseded @@ -248,8 +345,9 @@ impl PreparedConversation { history = self.reservation.sampler.sample_retained(sample, self.ready) => history, }; if let Some(history) = history - && self.reservation.generation.strong_count() > 0 && self.key.authorization.is_current(&self.thread).await + && self.reservation.active.load(Ordering::Acquire) + && self.reservation.generation.strong_count() > 0 { let mut truncations = super::truncation::ClassificationTruncations::default(); truncations.extend(pending_truncations); @@ -276,3 +374,7 @@ impl PreparedConversation { drop(self.reservation._permit); } } + +#[cfg(test)] +#[path = "conversation_admission_tests.rs"] +mod admission_tests; diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/conversation_admission_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/conversation_admission_tests.rs new file mode 100644 index 0000000000..355bdf70ba --- /dev/null +++ b/codex-rs/ext/guardian-v2/src/async_scorer/conversation_admission_tests.rs @@ -0,0 +1,36 @@ +#![cfg(test)] +//! Verifies that both policy backends share admission while recording an observation. + +use std::sync::Arc; +use std::sync::TryLockError; + +use super::ConversationBackend; +use super::LunaSampler; +use super::LunaSamplerError; +use super::MAX_OUTSTANDING; +use crate::async_scorer::sampler::tests::sampler_config; + +#[test] +fn earlier_observation_keeps_the_last_available_slot() { + let sampler = Arc::new(LunaSampler::new(sampler_config( + "http://localhost".to_owned(), + ))); + let previous = ConversationBackend::new(sampler, /*revision*/ 0, /*metrics*/ None); + let current = previous.for_policy(/*revision*/ 1); + let _occupied = (0..MAX_OUTSTANDING - 1) + .map(|index| previous.reserve(|| index).1.expect("available slot")) + .collect::>(); + + let (_, reservation) = current.reserve(|| { + assert!(matches!( + previous.capacity.try_lock(), + Err(TryLockError::WouldBlock) + )); + MAX_OUTSTANDING - 1 + }); + assert!(reservation.is_ok()); + assert!(matches!( + previous.reserve(|| MAX_OUTSTANDING).1, + Err(LunaSamplerError::QueueFull) + )); +} diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/conversation_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/conversation_tests.rs index 74a47fec10..d94d7f03a6 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/conversation_tests.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/conversation_tests.rs @@ -53,10 +53,14 @@ async fn pending_request( let (ready, score) = tokio::sync::oneshot::channel(); let authorization = ScoreAuthorization::current(&thread, &Default::default()).await; let pending_score = PendingScore { - progress: thread - .thread_extension_data() - .get::() - .unwrap(), + progress: Arc::clone( + &ConversationBackend::for_turn( + thread.thread_extension_data(), + thread.thread_extension_data(), + ) + .unwrap() + .progress, + ), index: 0, max_lag: config.max_tool_call_lag, }; @@ -101,12 +105,17 @@ async fn configured_reset_limit_rebuilds_history_without_rejecting_fresh_evidenc .await?; let store = fixture.test.codex.thread_extension_data(); let metrics = Arc::new(RecordingMetrics::default()); - store.insert(GuardianV2ScoreProgress::new(Some(metrics.clone()))); store.insert(LunaSampler::new(sampler_config(server.uri()))); - store.insert(ConversationBackend::new( - store.get::().unwrap(), - )); - let backend = store.get::().unwrap(); + store.remove::(); + ConversationBackend::install( + store, + ConversationBackend::new( + store.get::().unwrap(), + /*revision*/ 0, + Some(metrics.clone()), + ), + ); + let backend = ConversationBackend::for_turn(store, store).unwrap(); let events = responses::sse(vec![ ev_assistant_message("score", "low"), ev_completed("score"), @@ -236,51 +245,54 @@ async fn configured_reset_limit_rebuilds_history_without_rejecting_fresh_evidenc } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn earlier_observation_keeps_the_last_available_slot() -> Result<()> { - let sampler = Arc::new(LunaSampler::new(sampler_config( - "http://localhost".to_owned(), - ))); - let backend = Arc::new(ConversationBackend::new(sampler)); - let occupied = (0..15) - .map(|index| backend.reserve(|| index).1.expect("available slot")) - .collect::>(); - let (observed, first_observed) = tokio::sync::oneshot::channel(); - let (release_first, release) = std::sync::mpsc::channel(); - let first_backend = Arc::clone(&backend); - let first = tokio::task::spawn_blocking(move || { - first_backend.reserve(|| { - observed.send(()).expect("first observation receiver"); - release.recv().expect("release first observation"); - 15 - }) - }); - first_observed.await?; +async fn policy_revisions_keep_separate_classifier_history() -> Result<()> { + skip_if_no_network!(Ok(())); + const OLD_SCORE: &str = "old-policy-score"; + const NEW_SCORE: &str = "new-policy-score"; + const RESUMED_SCORE: &str = "resumed-policy-score"; - let (started, later_started) = tokio::sync::oneshot::channel(); - let (admitted, later_admitted) = tokio::sync::oneshot::channel(); - let later = tokio::task::spawn_blocking(move || { - started.send(()).expect("later admission receiver"); - let reservation = backend.reserve(|| 16); - let _ = admitted.send(()); - reservation - }); - later_started.await?; - // The later admission must wait for the earlier observation to reserve its slot. - let later_waited = tokio::time::timeout(Duration::from_millis(250), later_admitted) - .await - .is_err(); - // Always release the blocking task before assertions, even if admission was reordered. - release_first.send(())?; - let (first_index, first_reservation) = first.await?; - let (later_index, later_reservation) = later.await?; - assert!(later_waited); - assert_eq!((first_index, later_index), (15, 16)); - assert!(first_reservation.is_ok()); - assert!(matches!( - later_reservation, - Err(LunaSamplerError::QueueFull) - )); - drop(occupied); + let fixture = GuardianFailureFixture::new().await?; + let store = fixture.test.codex.thread_extension_data(); + let server = responses::start_mock_server().await; + let outputs = [OLD_SCORE, NEW_SCORE, RESUMED_SCORE]; + let events = + outputs.map(|id| responses::sse(vec![ev_assistant_message(id, "low"), ev_completed(id)])); + let mock = responses::mount_sse_sequence(&server, Vec::from(events)).await; + let previous = ConversationBackend::new( + Arc::new(LunaSampler::new(sampler_config(server.uri()))), + /*revision*/ 0, + /*metrics*/ None, + ); + let classify = async |backend: &ConversationBackend, label| -> Result<()> { + let (mut request, score) = pending_request(&fixture, label).await?; + let (index, reservation) = + backend.reserve(|| observe_unscored_call(&backend.progress, store)); + request.pending_score.progress = Arc::clone(&backend.progress); + request.pending_score.index = index; + reservation?.prepare(request).await.unwrap().run().await; + assert_eq!(score.await??, "low"); + Ok(()) + }; + + // Each sample commits before the next starts, with the same instructions and authorization. + classify(&previous, OLD_SCORE).await?; + let current = previous.for_policy(/*revision*/ 1); + classify(¤t, NEW_SCORE).await?; + classify(&previous, RESUMED_SCORE).await?; + + let retained_outputs = mock + .requests() + .into_iter() + .map(|request| { + request + .input() + .into_iter() + .filter_map(|item| outputs.into_iter().find(|id| item["id"] == *id)) + .collect::>() + }) + .collect::>(); + assert_eq!(retained_outputs, [vec![], vec![], vec![OLD_SCORE]]); + fixture.test.codex.shutdown_and_wait().await?; Ok(()) } @@ -297,7 +309,11 @@ async fn delayed_preparation_does_not_block_newer_requests_and_capacity_is_bound .test .codex .thread_extension_data() - .insert(ConversationBackend::new(sampler)); + .remove::(); + ConversationBackend::install( + fixture.test.codex.thread_extension_data(), + ConversationBackend::new(sampler, /*revision*/ 0, /*metrics*/ None), + ); let events = ["C", "B", "A", "D"].map(|label| { responses::sse(vec![ ev_assistant_message(&format!("score-{label}"), "low"), @@ -305,16 +321,15 @@ async fn delayed_preparation_does_not_block_newer_requests_and_capacity_is_bound ]) }); let mock = responses::mount_sse_sequence(&server, events.to_vec()).await; - let backend = fixture - .test - .codex - .thread_extension_data() - .get::() - .unwrap(); + let backend = ConversationBackend::for_turn( + fixture.test.codex.thread_extension_data(), + fixture.test.codex.thread_extension_data(), + ) + .unwrap(); let store = fixture.test.codex.thread_extension_data(); let turn_store = ExtensionData::new("approval-turn"); turn_store.insert_arc(store.get::().expect("captured test config")); - let progress = store.get::().unwrap(); + let progress = &backend.progress; let mut reservations = (0..16) .map(|index| backend.reserve(|| index).1.unwrap()) .collect::>(); @@ -322,7 +337,7 @@ async fn delayed_preparation_does_not_block_newer_requests_and_capacity_is_bound let reservation = reservations.pop().unwrap(); let prepared = reservation.prepare(request).await.unwrap(); assert!(matches!( - backend.reserve(|| 16).1, + backend.for_policy(/*revision*/ 1).reserve(|| 0).1, Err(LunaSamplerError::QueueFull) )); drop(prepared); @@ -413,9 +428,13 @@ async fn stale_authorization_expiry_and_stopped_generations_reject_preparation() .test .codex .thread_extension_data() - .insert(ConversationBackend::new(sampler)); + .remove::(); + ConversationBackend::install( + fixture.test.codex.thread_extension_data(), + ConversationBackend::new(sampler, /*revision*/ 0, /*metrics*/ None), + ); let thread_store = fixture.test.codex.thread_extension_data(); - let backend = thread_store.get::().unwrap(); + let backend = ConversationBackend::for_turn(thread_store, thread_store).unwrap(); let (mut request, score) = pending_request(&fixture, "expired").await?; request.pending_score.max_lag = 0; let progress = Arc::clone(&request.pending_score.progress); @@ -478,9 +497,13 @@ async fn prepared_reviews_recheck_expiry_authorization_and_stopped_generations() .test .codex .thread_extension_data() - .insert(ConversationBackend::new(sampler)); + .remove::(); + ConversationBackend::install( + fixture.test.codex.thread_extension_data(), + ConversationBackend::new(sampler, /*revision*/ 0, /*metrics*/ None), + ); let thread_store = fixture.test.codex.thread_extension_data(); - let backend = thread_store.get::().unwrap(); + let backend = ConversationBackend::for_turn(thread_store, thread_store).unwrap(); let (mut request, score) = pending_request(&fixture, "expired").await?; request.pending_score.max_lag = 0; let progress = Arc::clone(&request.pending_score.progress); @@ -564,8 +587,11 @@ async fn preflight_skips_only_after_score_and_history_are_superseded() -> Result ])); } let mock = responses::mount_sse_sequence(&server, events).await; - let backend = - ConversationBackend::new(Arc::new(LunaSampler::new(sampler_config(server.uri())))); + let backend = ConversationBackend::new( + Arc::new(LunaSampler::new(sampler_config(server.uri()))), + /*revision*/ 0, + /*metrics*/ None, + ); let (request, older_score) = pending_request(&fixture, "older").await?; let older = backend .reserve(|| 0) @@ -586,7 +612,9 @@ async fn preflight_skips_only_after_score_and_history_are_superseded() -> Result .run() .await; assert_eq!(newer_score.await??, "low"); - let progress = store.get::().unwrap(); + let progress = ConversationBackend::for_turn(store, store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); if publish_newer { seed_cached_score(&progress, store, /*index*/ 1, authorization); } @@ -657,16 +685,22 @@ async fn delayed_classification_skips_after_newer_score_and_history_complete() - .await; let store = fixture.test.codex.thread_extension_data(); store.insert(LunaSampler::new(sampler_config(server.uri()))); - store.insert(ConversationBackend::new( - store.get::().unwrap(), - )); - let backend = store.get::().unwrap(); + store.remove::(); + ConversationBackend::install( + store, + ConversationBackend::new( + store.get::().unwrap(), + /*revision*/ 0, + Some(store.get::().unwrap()), + ), + ); + let backend = ConversationBackend::for_turn(store, store).unwrap(); // Leave two slots for real classifications. The newer request releases its slot // only after committing history, so admission below provides a completion barrier. let held = (0..14) .map(|_| backend.reserve(|| 0).1.unwrap()) .collect::>(); - let progress = store.get::().unwrap(); + let progress = &backend.progress; let mut updates = progress.updates.subscribe(); let metrics = store.get::().unwrap(); let turn_store = ExtensionData::new("turn-1"); @@ -802,7 +836,7 @@ async fn concurrent_requests_retain_only_the_newest_completed_history() -> Resul let mut config = sampler_config(format!("{}/v1", server.uri())); config.metrics = Some(metrics.clone()); let sampler = Arc::new(LunaSampler::new(config)); - let backend = ConversationBackend::new(sampler); + let backend = ConversationBackend::new(sampler, /*revision*/ 0, /*metrics*/ None); let (request, score) = pending_request(&fixture, "seed").await?; let reservation = backend.reserve(|| 0).1.unwrap(); let prepared = reservation.prepare(request).await.unwrap(); @@ -953,7 +987,9 @@ async fn expiry_preserves_the_completed_checkpoint_and_transcript_cursor() -> Re ) .await?; let store = fixture.test.codex.thread_extension_data(); - let progress = store.get::().unwrap(); + let progress = ConversationBackend::for_turn(store, store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); let (release_expired, finish_expired) = tokio::sync::oneshot::channel(); let (server, _completions) = start_streaming_sse_server(vec![ vec![StreamingSseChunk { @@ -986,10 +1022,14 @@ async fn expiry_preserves_the_completed_checkpoint_and_transcript_cursor() -> Re }], ]) .await; - let backend = ConversationBackend::new(Arc::new(LunaSampler::new(sampler_config(format!( - "{}/v1", - server.uri() - ))))); + let backend = ConversationBackend::new( + Arc::new(LunaSampler::new(sampler_config(format!( + "{}/v1", + server.uri() + )))), + /*revision*/ 0, + /*metrics*/ None, + ); let (mut request, score) = pending_request(&fixture, "checkpoint").await?; let (index, reservation) = backend.reserve(|| observe_unscored_call(&progress, store)); request.pending_score.index = index; @@ -1079,7 +1119,11 @@ async fn expiry_discards_a_queued_score_without_recording_success() -> Result<() let metrics = Arc::new(crate::async_scorer::sampler::tests::RecordingMetrics::default()); let mut config = sampler_config(server.uri()); config.metrics = Some(metrics.clone()); - let backend = ConversationBackend::new(Arc::new(LunaSampler::new(config))); + let backend = ConversationBackend::new( + Arc::new(LunaSampler::new(config)), + /*revision*/ 0, + /*metrics*/ None, + ); let (mut request, score) = pending_request(&fixture, "queued").await?; request.pending_score.max_lag = 0; let (index, reservation) = diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/decisions_lifecycle_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/decisions_lifecycle_tests.rs index 36617855c7..757313c871 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/decisions_lifecycle_tests.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/decisions_lifecycle_tests.rs @@ -1,4 +1,5 @@ //! Decisions outcomes cannot affect the real baseline score/publication path. + use super::*; use crate::async_scorer::conversation::ConversationBackend; use crate::async_scorer::decisions::DecisionsCredential; @@ -45,6 +46,7 @@ async fn decisions_disagreement_failures_and_disabled_flag_preserve_baseline() - let mut initial_model = adaptive_model.as_ref().clone(); initial_model.guardian = Some(Default::default()); store.insert(initial_model); + store.remove::(); registry.thread_lifecycle_contributors()[0] .on_thread_start(ThreadStartInput { config: &config, @@ -212,14 +214,19 @@ async fn guardian_budget_rejects_both_backends_before_sending() -> Result<()> { let server = responses::start_mock_server().await; let store = fixture.test.codex.thread_extension_data(); let metrics = Arc::new(RecordingMetrics::default()); - store.insert(GuardianV2ScoreProgress::new(Some(metrics.clone()))); // Replace the conversation backend to force the budget failure. let mut sampler_config = crate::async_scorer::sampler::tests::sampler_config(server.uri()); sampler_config.max_input_tokens = 256; store.insert(LunaSampler::new(sampler_config)); - store.insert(ConversationBackend::new( - store.get::().unwrap(), - )); + store.remove::(); + ConversationBackend::install( + store, + ConversationBackend::new( + store.get::().unwrap(), + /*revision*/ 0, + Some(metrics.clone()), + ), + ); if enabled { store.insert(DecisionsSampler::new( HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault), @@ -229,7 +236,7 @@ async fn guardian_budget_rejects_both_backends_before_sending() -> Result<()> { )?); } fixture.score_tool(ToolName::plain("read_file")).await; - fixture.assert_fails_closed("elevated_risk").await?; + fixture.assert_fails_closed("scoring_failure").await?; tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { loop { let completed = { diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs b/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs index 7283dacdb3..05689ceb7b 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs @@ -30,7 +30,6 @@ use codex_protocol::protocol::has_full_access; use super::config::GuardianV2Config; use super::conversation::ConversationBackend; use super::sampler::LunaSampler; -use super::score::GuardianV2ScoreProgress; use super::trusted_skills::TrustedSkillRoots; #[derive(Clone)] @@ -76,14 +75,18 @@ impl ThreadLifecycleContributor for GuardianV2Extension { .get_or_init(NodeReplReviewEvidence::default) .enable_image_capture(); } - input.thread_store.remove::(); input.thread_store.remove::(); let sampler = input .thread_store .get_or_init(|| LunaSampler::new(sampler_config)); - input - .thread_store - .insert(ConversationBackend::new(Arc::clone(&sampler))); + ConversationBackend::install( + input.thread_store, + ConversationBackend::new( + Arc::clone(&sampler), + input.config.guardian_policy_revision, + input.extension_metrics.clone(), + ), + ); input .thread_store .remove::(); @@ -97,9 +100,7 @@ impl ThreadLifecycleContributor for GuardianV2Extension { input.thread_store.insert(decisions_sampler); } input.thread_store.insert(guardian_config); - input.thread_store.insert(GuardianV2ScoreProgress::new( - input.extension_metrics.clone(), - )); + // Preserve the answer path selected by the host for this thread. input .thread_store @@ -132,7 +133,7 @@ impl ThreadLifecycleContributor for GuardianV2Extension { } fn on_thread_stop<'a>(&'a self, input: ThreadStopInput<'a>) -> ExtensionFuture<'a, ()> { Box::pin(async move { - input.thread_store.remove::(); + ConversationBackend::stop(input.thread_store); }) } } @@ -168,8 +169,8 @@ impl ToolLifecycleContributor for GuardianV2Extension { fn on_tool_finish<'a>(&'a self, input: ToolFinishInput<'a>) -> ToolLifecycleFuture<'a> { Box::pin(async move { - if let Some(progress) = input.thread_store.get::() { - progress.finish(input.call_id); + if let Some(cache) = input.turn_store.get::() { + cache.progress.finish(input.call_id); } }) } diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/extension_cached_delivery_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/extension_cached_delivery_tests.rs index 9efd50cef0..4a30337412 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/extension_cached_delivery_tests.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/extension_cached_delivery_tests.rs @@ -1,5 +1,7 @@ //! Verifies that confirmed Code Mode messaging invalidates cached approvals in both root and worker. +use crate::async_scorer::conversation::ConversationBackend; + use super::*; use codex_core::TurnInputRequest; use codex_protocol::user_input::UserInput; @@ -126,7 +128,9 @@ async fn confirmed_root_delivery_invalidates_root_and_worker_cached_approvals() wait_for_mcp_server(&test.codex, "codex_apps").await?; allow_messaging_guardian_review(&thread_server).await; let root_store = test.codex.thread_extension_data(); - let root_progress = root_store.get::().unwrap(); + let root_progress = ConversationBackend::for_turn(root_store, root_store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { while cached_score(root_store).is_none() || root_progress.inspect(/*call_id*/ None).lag > 0 { @@ -232,7 +236,9 @@ async fn confirmed_root_delivery_invalidates_root_and_worker_cached_approvals() let store = thread.thread_extension_data(); let turn_store = ExtensionData::new("approval-turn"); turn_store.insert_arc(store.get::().expect("captured test config")); - let progress = store.get::().unwrap(); + let progress = ConversationBackend::for_turn(store, store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); seed_cached_score(&progress, store, /*index*/ 1_000, before.clone()); assert_eq!( cached_approval( @@ -314,7 +320,9 @@ async fn confirmed_root_delivery_invalidates_root_and_worker_cached_approvals() let store = worker.thread_extension_data(); let turn_store = ExtensionData::new("approval-turn"); turn_store.insert_arc(store.get::().expect("captured test config")); - let progress = store.get::().unwrap(); + let progress = ConversationBackend::for_turn(store, store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); let authorization = ScoreAuthorization::current(&worker, &Default::default()).await; assert!(authorization.local.retained_context_complete); seed_cached_score(&progress, store, /*index*/ 1_000, authorization); diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs index 3d5fb8d944..722d9079a4 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs @@ -1,3 +1,4 @@ +use crate::async_scorer::conversation::ConversationBackend; use std::collections::BTreeMap; use std::sync::Arc; use std::sync::Mutex; @@ -344,8 +345,8 @@ async fn installed_extension_keeps_parent_program_out_of_websocket_and_http_revi .expect("Guardian v2 should initialize") .wait_for_prewarm(PREWARM_TIMEOUT) .await?; - let progress = thread_store - .get::() + let progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) .expect("Guardian v2 should initialize"); let turn_store = ExtensionData::new("turn-1"); turn_store.insert_arc(thread_store.get::().expect("captured test config")); @@ -631,8 +632,8 @@ async fn sandboxed_shell_classification_respects_review_scope() -> Result<()> { let mut score = cached_score(thread_store).expect("fixture should publish a score"); score.scores.insert("action_risk".to_owned(), 0.0); set_cached_score(thread_store, score); - let score_progress = thread_store - .get::() + let score_progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) .expect("Guardian v2 should track score progress per thread"); let mut expected = score_progress.inspect(/*call_id*/ None); let turn_store = ExtensionData::new("turn-1"); @@ -678,6 +679,8 @@ async fn sandboxed_shell_classification_respects_review_scope() -> Result<()> { expected.has_unscored_failure = true; assert_eq!(score_progress.inspect(/*call_id*/ None), expected); fixture.assert_fails_closed("scoring_failure").await?; + score_progress.clear_score(); + fixture.assert_fails_closed("scoring_failure").await?; Ok(()) } @@ -731,8 +734,8 @@ async fn computer_use_only_scores_cannot_approve_other_actions() -> Result<()> { }, ); thread_store.insert(RecordingMetrics::default()); - let progress = thread_store - .get::() + let progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) .expect("Guardian v2 should track score progress per thread"); // The seeded low score belongs to the model selected above. let authorization = ScoreAuthorization::current(&fixture.test.codex, &Default::default()).await; @@ -900,6 +903,113 @@ async fn computer_use_only_scores_cannot_approve_other_actions() -> Result<()> { ); } + // Retain a cell under an allowlist that disables adaptive review, then relax it. + let mut old_config = thread_store.get::().unwrap().as_ref().clone(); + old_config.guardian_policy_revision += 1; + let mut requirements = old_config.config_layer_stack.requirements().clone(); + *requirements.approvals_reviewer = + codex_config::Constrained::allow_only(ApprovalsReviewer::AutoReview); + old_config.config_layer_stack = + codex_config::ConfigLayerStack::new(Vec::new(), requirements, Default::default())?; + let old_turn = ExtensionData::new("restricted-turn"); + old_turn.insert(old_config); + let old_backend = ConversationBackend::for_turn(thread_store, &old_turn).unwrap(); + let ignored_calls = thread_store + .get::() + .unwrap() + .max_tool_call_lag + + 1; + for _ in 0..ignored_calls { + observe_unscored_call(&old_backend.progress, thread_store); + } + let failed_index = observe_unscored_call(&old_backend.progress, thread_store); + old_backend.progress.invalidate(failed_index); + let mut config = thread_store.get::().unwrap().as_ref().clone(); + config.guardian_policy_revision = + old_turn.get::().unwrap().guardian_policy_revision + 1; + thread_store.insert(config.clone()); + let new_turn = ExtensionData::new("relaxed-turn"); + new_turn.insert(config); + let current = ConversationBackend::for_turn(thread_store, &new_turn).unwrap(); + let action = + r#"{"id":"unscored","tool":"mcp_tool_call","server":"node_repl","tool_name":"js"}"#; + let metrics = Arc::new(RecordingMetrics::default()); + assert_eq!( + cached_approval( + &fixture.registry, + thread_store, + &new_turn, + action, + Some(metrics.clone()) + ) + .await, + None + ); + assert!( + metrics + .0 + .lock() + .unwrap() + .contains(&fast_decision_metric("deferred", "missing_score")) + ); + + let index = observe_unscored_call(¤t.progress, thread_store); + seed_cached_score( + ¤t.progress, + thread_store, + index, + ScoreAuthorization::current(&fixture.test.codex, &Default::default()).await, + ); + assert_eq!( + cached_approval( + &fixture.registry, + thread_store, + &new_turn, + action, + /*metrics*/ None + ) + .await, + Some(ReviewDecision::Approved) + ); + for _ in 0..ignored_calls { + fixture.registry.tool_lifecycle_contributors()[0] + .on_tool_start(ToolStartInput { + serving_assessment: None, + permissions: Box::pin(async { Some(Default::default()) }), + session_store: &fixture.session_store, + thread_store, + turn_store: &old_turn, + turn_id: old_turn.level_id(), + root_turn_id: None, + call_id: "old-js", + originating_item_id: None, + tool_name: &ToolName::namespaced("mcp__node_repl__", "js"), + mcp_tool: None, + payload: &payload, + conversation_history: fixture.test.codex.conversation_history_snapshot().await, + source: ToolCallSource::CodeMode { + cell_id: "old-cell".to_owned(), + runtime_tool_call_id: "old-js".to_owned(), + }, + }) + .await; + } + observe_unscored_call(¤t.progress, thread_store); + assert_eq!( + current.progress.inspect(Some("unscored")).lag, + ignored_calls + 1 + ); + assert_eq!( + cached_approval( + &fixture.registry, + thread_store, + &new_turn, + action, + /*metrics*/ None + ) + .await, + None + ); Ok(()) } @@ -1207,8 +1317,8 @@ async fn unscored_tools_invalidate_cached_scores() -> Result<()> { thread_store.insert(model); let turn_store = ExtensionData::new("approval-turn"); turn_store.insert_arc(thread_store.get::().expect("captured test config")); - let progress = thread_store - .get::() + let progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) .expect("score progress"); let mut expected = progress.inspect(/*call_id*/ None); expected.action_risk = Some(0.25); @@ -1259,8 +1369,8 @@ impl GuardianFailureFixture { ) .await?; let thread_store = test.codex.thread_extension_data(); - let score_progress = thread_store - .get::() + let score_progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) .expect("Guardian v2 should track score progress per thread"); tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { while cached_score(thread_store).is_none() @@ -1320,8 +1430,8 @@ impl GuardianFailureFixture { let thread_store = self.test.codex.thread_extension_data(); let turn_store = ExtensionData::new("approval-turn"); turn_store.insert_arc(thread_store.get::().expect("captured test config")); - let score_progress = thread_store - .get::() + let score_progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) .expect("Guardian v2 should track score progress per thread"); tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { while cached_score(thread_store) @@ -1404,7 +1514,7 @@ async fn contributor_fails_closed_when_model_configuration_is_invalid() -> Resul .insert_arc(parent_model.model); fixture.score_tool(ToolName::plain("read_file")).await; - fixture.assert_fails_closed("elevated_risk").await + fixture.assert_fails_closed("scoring_failure").await } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] @@ -1425,6 +1535,11 @@ async fn contributor_fails_closed_when_luna_classification_fails() -> Result<()> server.uri().trim_start_matches("ws://") ))); config.features.enable(Feature::GuardianV2)?; + fixture + .test + .codex + .thread_extension_data() + .remove::(); fixture.registry.thread_lifecycle_contributors()[0] .on_thread_start(ThreadStartInput { config: &config, @@ -1447,7 +1562,122 @@ async fn contributor_fails_closed_when_luna_classification_fails() -> Result<()> .await?; fixture.score_tool(ToolName::plain("read_file")).await; - fixture.assert_fails_closed("elevated_risk").await + fixture.assert_fails_closed("scoring_failure").await +} + +#[tokio::test(flavor = "multi_thread")] +async fn late_turn_actions_invalidate_the_current_policy_score() -> Result<()> { + skip_if_no_network!(Ok(())); + let fixture = GuardianFailureFixture::new().await?; + let server = responses::start_mock_server().await; + let responses = ["low", "high", "invalid"].map(|risk| { + responses::sse(vec![ + responses::ev_assistant_message("score", risk), + responses::ev_completed("score"), + ]) + }); + let mock = responses::mount_sse_sequence(&server, responses.to_vec()).await; + let store = fixture.test.codex.thread_extension_data(); + store.insert(LunaSampler::new( + crate::async_scorer::sampler::tests::sampler_config(server.uri()), + )); + ConversationBackend::install( + store, + ConversationBackend::new( + store.get::().unwrap(), + /*revision*/ 0, + /*metrics*/ None, + ), + ); + let old_turn = ExtensionData::new("old-turn"); + old_turn.insert_arc(store.get::().unwrap()); + let old_backend = ConversationBackend::for_turn(store, &old_turn).unwrap(); + old_backend.progress.observe_js_execution(); + let executions = old_backend.progress.inspect(/*call_id*/ None).js_executions; + let mut config = store.get::().unwrap().as_ref().clone(); + config.guardian_policy_revision = 1; + config.guardian_extra_policy = Some("Changed policy".to_owned()); + store.insert(config); + let new_turn = ExtensionData::new("new-turn"); + new_turn.insert_arc(store.get::().unwrap()); + let new_backend = ConversationBackend::for_turn(store, &new_turn).unwrap(); + assert!(!Arc::ptr_eq(&old_backend, &new_backend)); + assert_eq!( + new_backend.progress.inspect(/*call_id*/ None).js_executions, + executions + ); + old_backend.progress.observe_js_execution(); + assert_eq!( + new_backend.progress.inspect(/*call_id*/ None).js_executions, + executions + 1 + ); + let mut expected: Option = None; + for (turn, call_id, risk, expected_approval) in [ + (&new_turn, "new-score", 0.0, Some(ReviewDecision::Approved)), + ( + &old_turn, + "late-old-score", + 1.0, + Some(ReviewDecision::Approved), + ), + (&old_turn, "late-old-failure", 1.0, None), + ] { + let backend = ConversationBackend::for_turn(store, turn).unwrap(); + let mut updates = backend.progress.updates.subscribe(); + fixture.registry.tool_lifecycle_contributors()[0] + .on_tool_start(ToolStartInput { + serving_assessment: None, + permissions: Box::pin(async { Some(Default::default()) }), + session_store: &fixture.session_store, + thread_store: store, + turn_store: turn, + turn_id: turn.level_id(), + root_turn_id: None, + call_id, + originating_item_id: Some(&ResponseItemId::from_server(turn.level_id().to_owned())), + tool_name: &ToolName::plain("read_file"), + mcp_tool: None, + payload: &ToolPayload::Function { + arguments: "{}".to_owned(), + }, + conversation_history: fixture.test.codex.conversation_history_snapshot().await, + source: ToolCallSource::CodeMode { + cell_id: turn.level_id().to_owned(), + runtime_tool_call_id: call_id.to_owned(), + }, + }) + .await; + tokio::time::timeout(ASYNC_TEST_TIMEOUT, updates.changed()).await??; + assert_eq!( + backend.progress.inspect(/*call_id*/ None).action_risk, + Some(risk) + ); + let current = new_backend.progress.inspect(/*call_id*/ None); + if let Some(expected) = expected.as_mut() { + expected.lag += 1; + expected.has_unscored_failure = expected_approval.is_none(); + } else { + assert_eq!(current.action_risk, Some(0.0)); + expected = Some(current); + } + assert_eq!( + cached_approval( + &fixture.registry, + store, + &new_turn, + r#"{"id":"new-score","tool":"read_file"}"#, + /*metrics*/ None + ) + .await, + expected_approval + ); + } + assert_eq!( + new_backend.progress.inspect(/*call_id*/ None), + expected.unwrap() + ); + assert_eq!(mock.requests().len(), responses.len()); + Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] @@ -1495,7 +1725,9 @@ classifier_instructions = "Predict future violations.\n# Security Policy\n{{ ten let store = test.codex.thread_extension_data(); let turn_store = ExtensionData::new("approval-turn"); turn_store.insert_arc(store.get::().expect("captured test config")); - let progress = store.get::().unwrap(); + let progress = ConversationBackend::for_turn(store, store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); let mut updates = progress.updates.subscribe(); tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { while progress.inspect(/*call_id*/ None).authorization.is_none() { @@ -1682,8 +1914,8 @@ max_recent_non_user_entries = 8 let thread_store = test.codex.thread_extension_data(); let turn_store = ExtensionData::new("approval-turn"); turn_store.insert_arc(thread_store.get::().expect("captured test config")); - let score_progress = thread_store - .get::() + let score_progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) .expect("Guardian v2 should track score progress per thread"); let metrics = thread_store.get::().unwrap(); tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { @@ -2417,7 +2649,9 @@ async fn contributor_samples_tool_calls_with_the_existing_luna_pool() -> Result< Some(ReviewDecision::Approved) ); - let progress = thread_store.get::().unwrap(); + let progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); for permissions in [ codex_guardian_context::PermissionContext { environment_id: Some("other-computer".to_owned()), @@ -2607,8 +2841,8 @@ async fn contributor_skips_required_models_in_standard_scope() -> Result<()> { // A late prewarm preview must leave the active model's review requirements intact. let _ = codex_core::guardian_review::prepare_review_prewarm(&test.codex).await?; let authorization = ScoreAuthorization::current(&test.codex, &Default::default()).await; - let progress = thread_store - .get::() + let progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) .expect("Guardian v2 should track score progress per thread"); seed_cached_score(&progress, thread_store, /*index*/ 0, authorization); set_cached_score( @@ -2711,7 +2945,9 @@ async fn cached_score_survives_compaction_and_internal_context_but_not_user_inpu let thread_store = test.codex.thread_extension_data(); let turn_store = ExtensionData::new("approval-turn"); turn_store.insert_arc(thread_store.get::().expect("captured test config")); - let progress = thread_store.get::().unwrap(); + let progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { while progress.inspect(/*call_id*/ None).authorization.is_none() { tokio::task::yield_now().await; @@ -2791,7 +3027,9 @@ async fn stale_review_context_blocks_cached_approval_without_changing_authorizat let store = fixture.test.codex.thread_extension_data(); let turn_store = ExtensionData::new("captured-turn"); turn_store.insert_arc(store.get::().expect("captured turn config")); - let progress = store.get::().unwrap(); + let progress = ConversationBackend::for_turn(store, store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); let mut score = cached_score(store).unwrap(); score.scores.insert("action_risk".to_owned(), 0.25); set_cached_score(store, score); @@ -2895,8 +3133,8 @@ async fn incompatible_compaction_blocks_cached_score_and_initial_cua_allowance() ); let score_authorization = ScoreAuthorization::current(&fixture.test.codex, &Default::default()).await; - let progress = thread_store - .get::() + let progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) .expect("score progress"); seed_cached_score( &progress, @@ -2955,8 +3193,8 @@ async fn contributor_counts_failed_thread_lookups_toward_score_lag() -> Result<( let thread_store = test.codex.thread_extension_data(); let turn_store = ExtensionData::new("turn-1"); turn_store.insert_arc(thread_store.get::().expect("captured test config")); - let score_progress = thread_store - .get::() + let score_progress = ConversationBackend::for_turn(thread_store, thread_store) + .map(|cache| Arc::clone(&cache.progress)) .expect("Guardian v2 should track score progress per thread"); tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { while score_progress @@ -3722,7 +3960,9 @@ async fn cached_approval_respects_action_order_and_wrapper_lag() -> Result<()> { let store = fixture.test.codex.thread_extension_data(); let turn_store = ExtensionData::new("approval-turn"); turn_store.insert_arc(store.get::().expect("captured test config")); - let progress = store.get::().unwrap(); + let progress = ConversationBackend::for_turn(store, store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); let mut score = cached_score(store).unwrap(); score.scores.insert("action_risk".to_owned(), 0.0); set_cached_score(store, score); @@ -3975,12 +4215,14 @@ async fn cached_score_publication_rejects_delayed_results_without_changing_cover skip_if_no_network!(Ok(())); let fixture = GuardianFailureFixture::new().await?; let store = fixture.test.codex.thread_extension_data(); - let progress = store.get::().unwrap(); + let progress = ConversationBackend::for_turn(store, store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); let authorization = ScoreAuthorization::current(&fixture.test.codex, &Default::default()).await; seed_cached_score(&progress, store, /*index*/ 1, authorization.clone()); let score = cached_score(store).unwrap(); observe_unscored_call(&progress, store); - observe_unscored_call(&progress, store); + let failed_index = observe_unscored_call(&progress, store); let cached = progress.inspect(/*call_id*/ None); let mut outdated_authorization = authorization.clone(); outdated_authorization.local.user_message_revision += 1; @@ -3990,7 +4232,7 @@ async fn cached_score_publication_rejects_delayed_results_without_changing_cover assert_eq!(cached_score(store).as_ref(), Some(&score)); let failed_at = std::time::SystemTime::now() + Duration::from_secs(1); - progress.fail_closed(failed_at); + progress.fail_closed(failed_at, failed_index); let mut delayed_score = score; delayed_score.sampled_at = Some(failed_at.into()); assert!(!progress.publish( @@ -3999,24 +4241,34 @@ async fn cached_score_publication_rejects_delayed_results_without_changing_cover /*index*/ 2 )); - delayed_score.sampled_at = Some((failed_at + Duration::from_secs(1)).into()); + let refreshed_at = failed_at + Duration::from_secs(1); + delayed_score.sampled_at = Some(refreshed_at.into()); + let refreshed_index = observe_unscored_call(&progress, store); delayed_score.scores.insert("action_risk".to_owned(), 0.0); progress.mark_oversized("active-overflow", /*index*/ 2); assert!(progress.publish( delayed_score.clone(), authorization.clone(), - /*index*/ 3 + refreshed_index )); assert_eq!(cached_score(store).as_ref(), Some(&delayed_score)); let mut expected = cached; expected.lag = 0; expected.action_risk = Some(0.0); expected.sampled_at = delayed_score.sampled_at.map(std::time::SystemTime::from); - expected.authorization = Some(authorization); + expected.authorization = Some(authorization.clone()); assert_eq!(progress.inspect(/*call_id*/ None), expected); assert!(progress.inspect(Some("active-overflow")).oversized); progress.finish("active-overflow"); assert!(!progress.inspect(Some("active-overflow")).oversized); + + // A newer timestamp can replace the score with an earlier action's LOW. + // Its source must still cover the failure, even though the coverage high-water is newer. + delayed_score.sampled_at = Some((refreshed_at + Duration::from_secs(1)).into()); + assert!(progress.publish(delayed_score.clone(), authorization, /*index*/ 1)); + expected.sampled_at = delayed_score.sampled_at.map(std::time::SystemTime::from); + expected.has_unscored_failure = true; + assert_eq!(progress.inspect(/*call_id*/ None), expected); Ok(()) } diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/observation.rs b/codex-rs/ext/guardian-v2/src/async_scorer/observation.rs index c1475eadf0..3c0329761d 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/observation.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/observation.rs @@ -34,7 +34,6 @@ use super::metrics::record_classification; use super::parent_compaction::ParentCompactionError; use super::parent_compaction::select_parent_compaction; use super::sampler::LunaSampler; -use super::score::GuardianV2ScoreProgress; use codex_protocol::openai_models::GuardianUnscoredAction as UnscoredAction; impl GuardianV2Extension { @@ -62,9 +61,12 @@ impl GuardianV2Extension { let Some(guardian_config) = input.thread_store.get::() else { return; }; - let Some(score_progress) = input.thread_store.get::() else { + let Some(conversation) = + ConversationBackend::for_turn(input.thread_store, input.turn_store) + else { return; }; + let score_progress = Arc::clone(&conversation.progress); let is_js_execution = input.mcp_tool.is_some_and(|tool| { let info = tool.tool_info(); is_node_repl_backed_connector(info.server_name(), info.connector_id.as_deref()) @@ -136,8 +138,10 @@ impl GuardianV2Extension { } if !scores_tool(&policy, input.tool_name, input.payload, scope) { match policy.unscored_action { - UnscoredAction::Ignore => {} - UnscoredAction::AgeScore => { + // An older policy cannot hide actions from the current policy's cache. + UnscoredAction::Ignore + if conversation.is_current_for_thread(input.thread_store) => {} + UnscoredAction::Ignore | UnscoredAction::AgeScore => { let index = score_progress.observe(&input); // Unscored permission widening must not reuse an earlier approval score. if input.tool_name.is_default_namespace() @@ -163,20 +167,13 @@ impl GuardianV2Extension { let context_mode = GuardianContextMode::from_history(input.conversation_history.as_ref()); let analytics = input.session_store.get::(); let sampled_at = SystemTime::now(); - let Some(conversation) = input.thread_store.get::() else { - let index = score_progress.observe(&input); - score_progress.invalidate(index); - score_progress.fail_closed(sampled_at); - return; - }; let (tool_call_index, reservation) = conversation.reserve(|| score_progress.observe(&input)); tracing::Span::current().record("tool_call_index", tool_call_index); let reservation = match reservation { Ok(reservation) => reservation, Err(error) => { - score_progress.invalidate(tool_call_index); - score_progress.fail_closed(sampled_at); + score_progress.fail_closed(sampled_at, tool_call_index); record_classification( metrics.as_deref(), context_mode, @@ -265,7 +262,7 @@ impl GuardianV2Extension { let guardian_config = match guardian_config.with_model_defaults(model_defaults) { Ok(config) => config, Err(error) => { - score_progress.fail_closed(sampled_at); + score_progress.fail_closed(sampled_at, tool_call_index); record_classification( metrics.as_deref(), context_mode, @@ -317,7 +314,7 @@ impl GuardianV2Extension { ("failure", Some("parent_compaction_unusable")) } }; - score_progress.fail_closed(sampled_at); + score_progress.fail_closed(sampled_at, tool_call_index); record_classification( metrics.as_deref(), context_mode, @@ -339,7 +336,7 @@ impl GuardianV2Extension { Ok(text) => text, Err(ActionRenderError::TooLarge { .. }) => { score_progress.mark_oversized(input.call_id, tool_call_index); - score_progress.fail_closed(sampled_at); + score_progress.fail_closed(sampled_at, tool_call_index); record_classification( metrics.as_deref(), context_mode, @@ -350,8 +347,7 @@ impl GuardianV2Extension { return; } Err(error) => { - score_progress.invalidate(tool_call_index); - score_progress.fail_closed(sampled_at); + score_progress.fail_closed(sampled_at, tool_call_index); record_classification( metrics.as_deref(), context_mode, @@ -396,7 +392,7 @@ impl GuardianV2Extension { }; let Some(permissions) = input.permissions.await else { - score_progress.fail_closed(sampled_at); + score_progress.fail_closed(sampled_at, tool_call_index); record_classification( metrics.as_deref(), context_mode, diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/pending_score_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/pending_score_tests.rs index b72c23d522..84a65a91db 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/pending_score_tests.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/pending_score_tests.rs @@ -1,7 +1,8 @@ //! Exercises lag expiry through tool admission and real classifier transports. -use super::*; use crate::async_scorer::conversation::ConversationBackend; + +use super::*; use crate::async_scorer::sampler::tests::RecordingMetrics; use crate::async_scorer::sampler::tests::sampler_config; use pretty_assertions::assert_eq; @@ -50,8 +51,18 @@ async fn stale_classifications_close_the_transport_without_invalidating_scores() store.insert(LunaSampler::new(config)); let sampler = store.get::().unwrap(); sampler.prewarm().await; - store.insert(ConversationBackend::new(Arc::clone(&sampler))); - let progress = store.get::().unwrap(); + store.remove::(); + ConversationBackend::install( + store, + ConversationBackend::new( + Arc::clone(&sampler), + /*revision*/ 0, + /*metrics*/ None, + ), + ); + let progress = ConversationBackend::for_turn(store, store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap(); let mut updates = progress.updates.subscribe(); fixture.score_tool(ToolName::plain("read_file")).await; if early_score { diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/score.rs b/codex-rs/ext/guardian-v2/src/async_scorer/score.rs index 555e06ddb7..c2523fd7ce 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/score.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/score.rs @@ -1,6 +1,6 @@ //! Owns cached-score publication, observation coverage, and reuse snapshots. -//! The cached score, authorization, and coverage are published under one lock; -//! failures never replace a newer sample. +//! Scores, authorization, and coverage stay policy-specific. Action freshness, wrapper +//! tracking, and JS allowances span generations; late results cannot refresh another policy. //! Pending classifications expire only when no active action can reuse their score. use std::collections::BTreeMap; @@ -19,8 +19,9 @@ use super::wrapper_lag::WrapperLag; #[derive(Default)] pub(super) struct GuardianV2ScoreProgress { state: Mutex, + pub(super) actions: Arc>, + initial_failure_index: usize, pub(super) updates: tokio::sync::watch::Sender<()>, - observations: tokio::sync::watch::Sender<()>, pub(super) metrics: Option>, } @@ -35,7 +36,7 @@ impl PendingScore { pub(super) fn is_expired(&self) -> bool { let state = self .progress - .state + .actions .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); state @@ -47,7 +48,13 @@ impl PendingScore { pub(super) async fn expired(&self) { // Subscribe before checking so admission cannot race past the expiry check. - let mut observations = self.progress.observations.subscribe(); + let mut observations = self + .progress + .actions + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .updates + .subscribe(); while !self.is_expired() { if observations.changed().await.is_err() { return; @@ -60,13 +67,19 @@ impl PendingScore { struct ScoreState { score: Option, score_index: usize, + latest_scored_tool_call: usize, + authorization: Option, +} + +/// Observations of the shared thread, independent of the policy scoring them. +#[derive(Default)] +pub(super) struct ActionState { wrapper_lag: WrapperLag, latest_tool_call: usize, - js_executions: usize, - latest_scored_tool_call: usize, latest_failed_tool_call: usize, oversized_tool_calls: BTreeSet, - authorization: Option, + js_executions: usize, + updates: tokio::sync::watch::Sender<()>, } /// A consistent view of the published score and the observations it covers. @@ -83,37 +96,46 @@ pub(super) struct CachedScore { } impl GuardianV2ScoreProgress { - pub(super) fn new(metrics: Option>) -> Self { + pub(super) fn new( + metrics: Option>, + actions: Arc>, + ) -> Self { + let initial_failure_index = actions + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .latest_failed_tool_call; Self { metrics, + actions, + initial_failure_index, ..Default::default() } } pub(super) fn observe(&self, input: &ToolStartInput<'_>) -> usize { let mut state = self - .state + .actions .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); state.latest_tool_call = state.latest_tool_call.saturating_add(/*rhs*/ 1); let index = state.latest_tool_call; state.wrapper_lag.record(input, index); - self.observations.send_replace(()); + state.updates.send_replace(()); index } pub(super) fn observe_js_execution(&self) { + // Setup/reset calls must not consume the first JS execution allowance. let mut state = self - .state + .actions .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); - // Setup/reset calls must not consume the first JS execution allowance. state.js_executions = state.js_executions.saturating_add(/*rhs*/ 1); } pub(super) fn invalidate(&self, index: usize) { let mut state = self - .state + .actions .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); state.latest_failed_tool_call = state.latest_failed_tool_call.max(index); @@ -121,7 +143,7 @@ impl GuardianV2ScoreProgress { pub(super) fn mark_oversized(&self, call_id: &str, index: usize) { let mut state = self - .state + .actions .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); state.oversized_tool_calls.insert(call_id.to_owned()); @@ -131,12 +153,12 @@ impl GuardianV2ScoreProgress { pub(super) fn finish(&self, call_id: &str) { // Overflow belongs to each active call even after a newer score succeeds. let mut state = self - .state + .actions .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); state.oversized_tool_calls.remove(call_id); state.wrapper_lag.finish(call_id); - self.observations.send_replace(()); + state.updates.send_replace(()); } pub(super) fn publish( @@ -163,7 +185,9 @@ impl GuardianV2ScoreProgress { accepted } - pub(super) fn fail_closed(&self, sampled_at: SystemTime) { + /// Invalidates this action across policies before publishing a local fail-closed score. + pub(super) fn fail_closed(&self, sampled_at: SystemTime, index: usize) { + self.invalidate(index); let mut state = self .state .lock() @@ -182,6 +206,7 @@ impl GuardianV2ScoreProgress { { state.score = Some(score); } + self.updates.send_replace(()); } pub(super) fn clear_score(&self) { @@ -192,6 +217,10 @@ impl GuardianV2ScoreProgress { } pub(super) fn inspect(&self, call_id: Option<&str>) -> CachedScore { + let actions = self + .actions + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); let state = self .state .lock() @@ -203,21 +232,24 @@ impl GuardianV2ScoreProgress { .and_then(|score| score.sampled_at) .map(SystemTime::from), // Use the accepted score's source, not the coverage high-water mark. - score_at_or_before_action: state + score_at_or_before_action: actions .wrapper_lag .get(call_id) .is_some_and(|start| state.score_index <= start.index), - lag: state + lag: actions .latest_tool_call .saturating_sub(state.latest_scored_tool_call) .saturating_sub( - state + actions .wrapper_lag .discount(call_id, state.latest_scored_tool_call), ), - has_unscored_failure: state.latest_failed_tool_call > state.latest_scored_tool_call, - js_executions: state.js_executions, - oversized: call_id.is_some_and(|id| state.oversized_tool_calls.contains(id)), + // Only failures newly recorded since cache creation can invalidate it. + // A published score covers those failures through its own action index. + has_unscored_failure: actions.latest_failed_tool_call + > self.initial_failure_index.max(state.score_index), + js_executions: actions.js_executions, + oversized: call_id.is_some_and(|id| actions.oversized_tool_calls.contains(id)), action_risk: state .score .as_ref() diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/score_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/score_tests.rs index 2489f13cc1..9cd5860579 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/score_tests.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/score_tests.rs @@ -1,4 +1,6 @@ +use crate::async_scorer::conversation::ConversationBackend; use std::collections::BTreeMap; +use std::sync::Arc; use std::time::Duration; use std::time::SystemTime; @@ -9,14 +11,18 @@ use pretty_assertions::assert_eq; use super::GuardianV2ScoreProgress; pub(in crate::async_scorer) fn cached_score(store: &ExtensionData) -> Option { - let progress = store.get::()?; + let progress = ConversationBackend::for_turn(store, store) + .map(|cache| Arc::clone(&cache.progress)) + .or_else(|| store.get::())?; let state = progress.state.lock().unwrap(); state.score.clone() } // Replace only the score so fixtures can exercise authorization and coverage independently. pub(in crate::async_scorer) fn set_cached_score(store: &ExtensionData, score: SecurityRiskScore) { - let progress = store.get_or_init(GuardianV2ScoreProgress::default); + let progress = ConversationBackend::for_turn(store, store) + .map(|cache| Arc::clone(&cache.progress)) + .unwrap_or_else(|| store.get_or_init(GuardianV2ScoreProgress::default)); progress.state.lock().unwrap().score = Some(score); } @@ -34,12 +40,12 @@ fn fail_closed_score_preserves_classification_order() { }; set_cached_score(&thread_store, newer_score.clone()); - progress.fail_closed(SystemTime::UNIX_EPOCH); + progress.fail_closed(SystemTime::UNIX_EPOCH, /*index*/ 0); assert_eq!(cached_score(&thread_store).as_ref(), Some(&newer_score)); for sampled_at in [newer_sampled_at, newest_sampled_at] { set_cached_score(&thread_store, newer_score.clone()); - progress.fail_closed(sampled_at); + progress.fail_closed(sampled_at, /*index*/ 0); let fail_closed_score = SecurityRiskScore { scores: BTreeMap::from([("action_risk".to_owned(), 1.0)]), call_id: None, diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/wrapper_lag.rs b/codex-rs/ext/guardian-v2/src/async_scorer/wrapper_lag.rs index 4e258cbd0e..f941db65b8 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/wrapper_lag.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/wrapper_lag.rs @@ -1,6 +1,6 @@ //! Tracks action order and discounts an approval's own unscored code-mode wrapper. //! Missing provenance keeps the full lag; bounded history never changes scoring or failure order. -//! The score owner's state lock protects this history. +//! The thread's action-state lock protects this history. //! Active nested actions retain their wrapper allowance when expiring pending scores. use std::collections::VecDeque;