From d67d903b613126ac331b3e2ed27e9fb0b8d12fcd Mon Sep 17 00:00:00 2001 From: jif Date: Wed, 2 Dec 2026 16:43:19 +0000 Subject: [PATCH] Rebuild Guardian classifier history when the transcript profile changes (#65299) ## Why A classifier catalog refresh can change which transcript entries count toward the cursor while the parent model and conversation stay the same. Reusing the old cursor can skip evidence that the new profile includes. ## What changed Include the transcript's `ContextProfile` in the classifier conversation reuse key so a profile change rebuilds history with the current profile. ## Testing Add a regression test that enables `tool_outputs` after scoring with only `tool_calls` and verifies that the next request includes both calls and the newly included output in transcript order. GitOrigin-RevId: 855f89b476c3c826b1e363e99d2f9605f8181a55 --- .../src/async_scorer/conversation.rs | 5 +- .../src/async_scorer/conversation_tests.rs | 82 +++++++++++++++++++ .../src/async_scorer/transcript.rs | 2 +- 3 files changed, 87 insertions(+), 2 deletions(-) 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 59aacd7984..b68258ec25 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/conversation.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/conversation.rs @@ -24,6 +24,7 @@ use codex_core::CodexThread; use codex_core::config::Config; use codex_extension_api::ExtensionData; use codex_extension_api::ExtensionMetrics; +use codex_guardian_context::ContextProfile; use codex_guardian_reviewer::ConversationState; use codex_protocol::models::ResponseItem; use tokio::sync::OwnedSemaphorePermit; @@ -96,10 +97,11 @@ pub(super) struct PreparedConversation { sampled_at: SystemTime, } -// Authorization covers model settings; local config changes replace the backend. #[derive(PartialEq)] struct ReuseKey { authorization: ScoreAuthorization, + // A catalog refresh can change which entries the cursor counts without changing the parent model. + profile: ContextProfile, instructions: RenderedFragment, parent_compaction: Option, } @@ -264,6 +266,7 @@ impl Reservation { } let key = ReuseKey { authorization: request.authorization, + profile: request.evidence.profile.clone(), instructions: request.sampling.instructions.clone(), parent_compaction: request.sampling.parent_compaction.clone(), }; 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 34c63abb4d..10e5e77da9 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 @@ -374,6 +374,88 @@ async fn policy_revisions_keep_separate_classifier_history() -> Result<()> { Ok(()) } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn changing_transcript_sources_rebuilds_classifier_history() -> Result<()> { + skip_if_no_network!(Ok(())); + let fixture = GuardianFailureFixture::new().await?; + let store = fixture.test.codex.thread_extension_data(); + let server = responses::start_mock_server().await; + let events = ["seed", "next"] + .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 backend = ConversationBackend::new( + Arc::new(LunaSampler::new(sampler_config(server.uri()))), + /*revision*/ 0, + /*metrics*/ None, + ); + let mut first = responses::ev_function_call("a", "read_file", r#"{"path":"a"}"#); + let mut second = responses::ev_function_call("b", "read_file", r#"{"path":"b"}"#); + let history = vec![ + serde_json::from_value(first["item"].take())?, + ResponseItem::FunctionCallOutput { + id: None, + call_id: Some("a".to_owned()), + name: None, + namespace: None, + output: FunctionCallOutputPayload::from_text("contents of a".to_owned()), + internal_chat_message_metadata_passthrough: None, + }, + serde_json::from_value(second["item"].take())?, + ]; + fixture.test.codex.inject_response_items(history).await?; + + // A classifier catalog refresh can enable outputs without changing the parent model or history. + let config = store.get::().unwrap(); + for sources in [ + vec!["tool_calls".into()], + vec!["tool_calls".into(), "tool_outputs".into()], + ] { + let model = GuardianV2ModelConfig { + transcript: Some(GuardianV2TranscriptModelConfig { + sources: Some(sources), + ..Default::default() + }), + ..Default::default() + }; + store.insert(config.with_model_defaults(Some(&model)).unwrap()); + let (mut request, score) = pending_request(&fixture, "parent").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"); + } + + let evidence = mock + .requests() + .into_iter() + .map(|request| { + request + .message_input_texts("user") + .into_iter() + .filter(|text| text.contains("tool read_file ")) + .collect::>() + }) + .collect::>(); + assert_eq!( + evidence, + [ + vec![ + "[1] tool read_file call: {\"path\":\"a\"}\n", + "[2] tool read_file call: {\"path\":\"b\"}\n" + ], + vec![ + "[1] tool read_file call: {\"path\":\"a\"}\n", + "[2] tool read_file result: contents of a\n", + "[3] tool read_file call: {\"path\":\"b\"}\n" + ], + ] + ); + fixture.test.codex.shutdown_and_wait().await?; + Ok(()) +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn delayed_preparation_does_not_block_newer_requests_and_capacity_is_bounded() -> Result<()> { skip_if_no_network!(Ok(())); diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/transcript.rs b/codex-rs/ext/guardian-v2/src/async_scorer/transcript.rs index ee1cbeb400..aeb082983b 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/transcript.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/transcript.rs @@ -159,7 +159,7 @@ impl TranscriptConfig { /// Captured evidence stays typed until the owning conversation selects its delta. pub(super) struct CollectedTranscript { context: CollectedContext, - profile: ContextProfile, + pub(super) profile: ContextProfile, history_version: u64, pub(super) elastic: Option, }