Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Skip conversation scoring superseded by newer scores and history (#55913)

jifa6839a481223
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 06271d30ef..cdff6dc2cc 100644
--- a/codex-rs/ext/guardian-v2/src/async_scorer/classification.rs
+++ b/codex-rs/ext/guardian-v2/src/async_scorer/classification.rs
@@ -402,6 +402,7 @@ impl Classification {
thread: Arc::clone(&thread),
metrics: metrics.clone(),
pending_score: pending_score.clone(),
+ sampled_at,
};
let mut sampling_started = None;
if let Some(prepared) = reservation.prepare(request).await {
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 bb689df5ce..c2dfd98390 100644
--- a/codex-rs/ext/guardian-v2/src/async_scorer/conversation.rs
+++ b/codex-rs/ext/guardian-v2/src/async_scorer/conversation.rs
@@ -8,10 +8,12 @@
//! Each request owns its fork; failed or cancelled requests leave committed history intact.
//! Host-only instruction delivery metadata is committed with its completed input messages.
//! Expired classifications release their transport without committing partial history.
+//! Execution preflight skips work superseded in both score and history before sampling starts.
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::Weak;
+use std::time::SystemTime;
use codex_context_fragments::RenderedFragment;
use codex_core::CodexThread;
@@ -65,6 +67,7 @@ pub(super) struct ConversationRequest {
pub(super) ready: oneshot::Sender<Result<SampledScore, LunaSamplerError>>,
pub(super) metrics: Option<Arc<dyn ExtensionMetrics>>,
pub(super) pending_score: PendingScore,
+ pub(super) sampled_at: SystemTime,
}
/// Owns an admitted history fork and its permit until execution completes or it is dropped.
@@ -78,6 +81,7 @@ pub(super) struct PreparedConversation {
ready: oneshot::Sender<Result<SampledScore, LunaSamplerError>>,
metrics: Option<Arc<dyn ExtensionMetrics>>,
pending_score: PendingScore,
+ sampled_at: SystemTime,
}
// Authorization covers model settings; local config changes replace the backend.
@@ -201,6 +205,7 @@ impl Reservation {
ready: request.ready,
metrics: request.metrics,
pending_score: request.pending_score,
+ sampled_at: request.sampled_at,
})
}
}
@@ -209,9 +214,25 @@ impl PreparedConversation {
/// Publishes the first score, then drains and commits only valid completed history.
pub(super) async fn run(mut self) {
// A caller can retain prepared input while authorization or admission expires.
- if self.reservation.generation.strong_count() == 0
- || !self.key.authorization.is_current(&self.thread).await
+ let authorization_is_current = self.key.authorization.is_current(&self.thread).await;
+ let history_superseded = self
+ .reservation
+ .history
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .as_ref()
+ .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.generation.strong_count() == 0
|| self.pending_score.is_expired()
+ || (history_superseded
+ && self
+ .pending_score
+ .progress
+ .inspect(/*call_id*/ None)
+ .sampled_at
+ .is_some_and(|sampled_at| sampled_at > self.sampled_at))
{
let _ = self.ready.send(Err(LunaSamplerError::Superseded));
return;
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 ea0ffe6087..d5d008eef7 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
@@ -66,6 +66,7 @@ async fn pending_request(
ready,
metrics: None,
pending_score,
+ sampled_at: std::time::SystemTime::now(),
sampling: LunaSamplingRequest {
instructions,
input: Vec::new(),
@@ -517,6 +518,222 @@ async fn prepared_reviews_recheck_expiry_authorization_and_stopped_generations()
Ok(())
}
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn preflight_skips_only_after_score_and_history_are_superseded() -> Result<()> {
+ skip_if_no_network!(Ok(()));
+ for (retain_newer, publish_newer, risk) in [
+ (true, false, 0.0),
+ (false, true, 0.0),
+ (true, true, 0.0),
+ (true, true, 1.0),
+ ] {
+ let fixture = GuardianFailureFixture::with_config(
+ "[features.guardianv2]\nasync_classifier_mode = 'conversation'",
+ )
+ .await?;
+ let store = fixture.test.codex.thread_extension_data();
+ let mut cached = cached_score(store).unwrap();
+ cached.scores.insert("action_risk".to_owned(), risk);
+ set_cached_score(store, cached);
+ let server = responses::start_mock_server().await;
+ let mut events = vec![responses::sse(vec![
+ responses::ev_output_text_delta("low"),
+ ev_assistant_message("newer", if retain_newer { "low" } else { "lowhigh" }),
+ ev_completed("newer"),
+ ])];
+ if !(retain_newer && publish_newer) {
+ events.push(responses::sse(vec![
+ ev_assistant_message("older", "low"),
+ ev_completed("older"),
+ ]));
+ }
+ let mock = responses::mount_sse_sequence(&server, events).await;
+ let backend =
+ ConversationBackend::new(Arc::new(LunaSampler::new(sampler_config(server.uri()))));
+ let (request, older_score) = pending_request(&fixture, "older").await?;
+ let older = backend
+ .reserve(|| 0)
+ .1
+ .unwrap()
+ .prepare(request)
+ .await
+ .unwrap();
+ let (request, newer_score) = pending_request(&fixture, "newer").await?;
+ let authorization = request.authorization.clone();
+ backend
+ .reserve(|| 1)
+ .1
+ .unwrap()
+ .prepare(request)
+ .await
+ .unwrap()
+ .run()
+ .await;
+ assert_eq!(newer_score.await??, "low");
+ let progress = store.get::<GuardianV2ScoreProgress>().unwrap();
+ if publish_newer {
+ seed_cached_score(&progress, store, /*index*/ 1, authorization);
+ }
+ let before = progress.inspect(/*call_id*/ None);
+ let approval_before = cached_approval(
+ &fixture.registry,
+ store,
+ r#"{"tool":"mcp_tool_call","server":"node_repl"}"#,
+ /*metrics*/ None,
+ )
+ .await;
+ assert_eq!(
+ approval_before,
+ if risk == 0.0 {
+ Some(ReviewDecision::Approved)
+ } else {
+ None
+ }
+ );
+ older.run().await;
+ if retain_newer && publish_newer {
+ assert!(matches!(
+ older_score.await?,
+ Err(LunaSamplerError::Superseded)
+ ));
+ assert_eq!(mock.requests().len(), 1);
+ } else {
+ assert_eq!(older_score.await??, "low");
+ let requests = mock.requests();
+ assert_eq!(requests.len(), 2);
+ assert!(requests[1].input().iter().all(|item| item["id"] != "newer"));
+ }
+ // Skipping inference must not publish an approval, age coverage, or fail closed.
+ assert_eq!(progress.inspect(/*call_id*/ None), before);
+ assert_eq!(
+ cached_approval(
+ &fixture.registry,
+ store,
+ r#"{"tool":"mcp_tool_call","server":"node_repl"}"#,
+ /*metrics*/ None
+ )
+ .await,
+ approval_before
+ );
+ fixture.test.codex.shutdown_and_wait().await?;
+ }
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn delayed_classification_skips_after_newer_score_and_history_complete() -> Result<()> {
+ skip_if_no_network!(Ok(()));
+ let fixture = GuardianFailureFixture::with_config(
+ "[features.guardianv2]\nasync_classifier_mode = 'conversation'",
+ )
+ .await?;
+ let server = responses::start_mock_server().await;
+ let mock = responses::mount_sse_once(
+ &server,
+ responses::sse(vec![
+ responses::ev_output_text_delta("low"),
+ ev_assistant_message("newer", "low"),
+ ev_completed("newer"),
+ ]),
+ )
+ .await;
+ let store = fixture.test.codex.thread_extension_data();
+ store.insert(LunaSampler::new(sampler_config(server.uri())));
+ store.insert(ConversationBackend::new(
+ store.get::<LunaSampler>().unwrap(),
+ ));
+ let backend = store.get::<ConversationBackend>().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::<Vec<_>>();
+ let progress = store.get::<GuardianV2ScoreProgress>().unwrap();
+ let mut updates = progress.updates.subscribe();
+ let metrics = store.get::<RecordingMetrics>().unwrap();
+ let turn_store = ExtensionData::new("turn-1");
+ let tool_name = ToolName::plain("read_file");
+ let payload = ToolPayload::Function {
+ arguments: r#"{"path":"README.md"}"#.to_owned(),
+ };
+ let history = fixture.test.codex.conversation_history_snapshot().await;
+ let observe = |call_id, permissions| {
+ fixture.registry.tool_lifecycle_contributors()[0].on_tool_start(ToolStartInput {
+ permissions,
+ session_store: &fixture.session_store,
+ thread_store: store,
+ turn_store: &turn_store,
+ turn_id: "turn-1",
+ root_turn_id: None,
+ call_id,
+ originating_item_id: None,
+ tool_name: &tool_name,
+ mcp_tool: None,
+ payload: &payload,
+ conversation_history: Arc::clone(&history),
+ source: ToolCallSource::Direct,
+ })
+ };
+ let (entered, waiting) = tokio::sync::oneshot::channel();
+ let (release, delayed) = tokio::sync::oneshot::channel();
+ let older = observe(
+ "older",
+ Box::pin(async {
+ entered.send(()).unwrap();
+ delayed.await.unwrap();
+ Some(Default::default())
+ }),
+ );
+ tokio::pin!(older);
+ tokio::select! {
+ result = waiting => result?,
+ () = &mut older => panic!("older observation must wait for permissions"),
+ }
+ observe("newer", Box::pin(async { Some(Default::default()) })).await;
+ tokio::time::timeout(ASYNC_TEST_TIMEOUT, async {
+ while cached_score(store)
+ .and_then(|score| score.call_id)
+ .as_deref()
+ != Some("newer")
+ {
+ updates.changed().await.unwrap();
+ }
+ loop {
+ match backend.reserve(|| 0).1 {
+ Ok(completed) => break drop(completed),
+ Err(LunaSamplerError::QueueFull) => tokio::task::yield_now().await,
+ Err(error) => panic!("unexpected admission failure: {error}"),
+ }
+ }
+ })
+ .await?;
+ let score_before = cached_score(store);
+ let progress_before = progress.inspect(/*call_id*/ None);
+ release.send(()).unwrap();
+ older.await;
+ // Wait for the real Classification::run task to finish its superseded path.
+ tokio::time::timeout(ASYNC_TEST_TIMEOUT, async {
+ loop {
+ let finished = metrics.0.lock().unwrap().iter().any(|metric| {
+ matches!(metric, RecordedMetric::Counter(name, _, tags)
+ if name == CLASSIFICATION_METRIC
+ && tags.iter().any(|(key, value)| key == "outcome" && value == "superseded"))
+ });
+ if finished {
+ break;
+ }
+ tokio::task::yield_now().await;
+ }
+ })
+ .await?;
+ assert_eq!(mock.requests().len(), 1);
+ assert_eq!(cached_score(store), score_before);
+ assert_eq!(progress.inspect(/*call_id*/ None), progress_before);
+ drop(held);
+ fixture.test.codex.shutdown_and_wait().await?;
+ Ok(())
+}
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn concurrent_requests_retain_only_the_newest_completed_history() -> Result<()> {
skip_if_no_network!(Ok(()));