From 59f18e8133f2c1d3427677658752ac46697d41b7 Mon Sep 17 00:00:00 2001 From: swastik-oai Date: Thu, 1 Oct 2026 20:47:27 +0000 Subject: [PATCH] Add opt-in Decisions comparison for Guardian V2 (#50099) ## What changed Add the disabled-by-default `guardianv2_decisions_comparison` feature to run Decisions alongside Guardian V2 snapshot classification using the same policy and evidence. Initialize the comparison sampler with `CODEX_GUARDIAN_DECISIONS_API_KEY`. Publish the baseline score before awaiting comparison results, and keep comparison results and failures from changing approvals. Apply Guardian's input token budget, skip conversation evidence, and cancel comparison work when the baseline classification is superseded. Record success, failure, and skip outcomes with bounded diagnostic reasons, including setup failures. ## Testing Add lifecycle tests covering disabled comparison, disagreeing classifications, backend failures, unchanged published scores, matching policy and evidence, and rejection of over-budget requests before either backend sends them. GitOrigin-RevId: 743d34c8ee440362da94f001058366027b39bd11 --- codex-rs/core/config.schema.json | 6 + .../src/async_scorer/classification.rs | 43 ++++ .../guardian-v2/src/async_scorer/decisions.rs | 68 +++++- .../async_scorer/decisions_lifecycle_tests.rs | 217 ++++++++++++++++++ .../src/async_scorer/decisions_tests.rs | 21 +- .../guardian-v2/src/async_scorer/extension.rs | 11 + .../src/async_scorer/extension_tests.rs | 3 + .../guardian-v2/src/async_scorer/metrics.rs | 38 +++ .../ext/guardian-v2/src/async_scorer/mod.rs | 2 - .../src/async_scorer/observation.rs | 3 + .../guardian-v2/src/async_scorer/startup.rs | 33 +++ codex-rs/features/src/lib.rs | 8 + 12 files changed, 436 insertions(+), 17 deletions(-) create mode 100644 codex-rs/ext/guardian-v2/src/async_scorer/decisions_lifecycle_tests.rs diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 6f6a95837a..1c736947d8 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -920,6 +920,9 @@ "guardianv2": { "$ref": "#/definitions/FeatureToml_for_GuardianV2ConfigToml" }, + "guardianv2_decisions_comparison": { + "type": "boolean" + }, "hooks": { "type": "boolean" }, @@ -7016,6 +7019,9 @@ "guardianv2": { "$ref": "#/definitions/FeatureToml_for_GuardianV2ConfigToml" }, + "guardianv2_decisions_comparison": { + "type": "boolean" + }, "hooks": { "type": "boolean" }, 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 8e33cef6b9..ecabb3dd23 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/classification.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/classification.rs @@ -59,6 +59,7 @@ pub(super) struct Classification { pub(super) reservation: Option, pub(super) classification_started_at: Instant, pub(super) sampler: Arc, + pub(super) decisions_sampler: Option>, pub(super) guardian_config: GuardianV2Config, pub(super) score_progress: Arc, pub(super) parent_model: Option>, @@ -110,6 +111,7 @@ impl Classification { reservation, classification_started_at, sampler, + decisions_sampler, guardian_config, score_progress, parent_model, @@ -263,6 +265,7 @@ impl Classification { truncations.extend(std::mem::take(&mut context.truncations)); super::metrics::record_section_costs(metrics.as_deref(), context.section_costs()); } + let mut decisions_task = None; let mut failure_reason = "invalid_output"; let mut classification_risk = None; let mut classification_finished_at = None; @@ -307,12 +310,37 @@ impl Classification { let result = match transcript { ClassificationContext::Snapshot(context) => { sampling.input = context.into_messages(); + if let Some(decisions_sampler) = decisions_sampler.as_ref() { + decisions_task = + Some(decisions_sampler.spawn(&sampling, sampler.max_input_tokens())); + } else if config + .features + .enabled(codex_features::Feature::GuardianV2DecisionsComparison) + { + super::metrics::record_decisions_comparison_outcome( + metrics.as_deref(), + "skipped", + "not_initialized", + ); + } sampler.sample(sampling).await } + ClassificationContext::Conversation { evidence, reservation, } => { + // The retained prefix is owned by the conversation backend, not this snapshot. + if config + .features + .enabled(codex_features::Feature::GuardianV2DecisionsComparison) + { + super::metrics::record_decisions_comparison_outcome( + metrics.as_deref(), + "skipped", + "unsupported_evidence", + ); + } let (ready, score) = tokio::sync::oneshot::channel(); reservation.submit(ConversationRequest { evidence, @@ -430,6 +458,16 @@ impl Classification { if matches!(result, Ok(ClassificationOutcome::Scored)) { truncations.emit(metrics.as_deref()); } + if matches!(result, Ok(ClassificationOutcome::Superseded)) + && let Some(task) = decisions_task.take() + { + drop(task); + super::metrics::record_decisions_comparison_outcome( + metrics.as_deref(), + "skipped", + "superseded", + ); + } if let Err(error) = result { event_sink.emit_warning(ExtensionWarning { thread_id, @@ -437,5 +475,10 @@ impl Classification { message: format!("Guardian V2 risk scoring failed: {error}"), }); } + if let Some(decisions_task) = decisions_task { + decisions_task + .finish_and_record_outcome(metrics.as_deref()) + .await; + } } } diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/decisions.rs b/codex-rs/ext/guardian-v2/src/async_scorer/decisions.rs index 14f1516426..579150e148 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/decisions.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/decisions.rs @@ -3,8 +3,13 @@ //! Unsupported evidence is rejected intact; errors never contain credentials or wire bodies. //! The caller records measurements after baseline publication; dropping its task aborts Decisions work. +use super::metrics::decisions_failure_reason; +use super::metrics::record_decisions_comparison_outcome; +use super::sampler::LunaSampler; use super::sampler::LunaSamplingRequest; use codex_context_fragments::RenderedFragment; +use codex_extension_api::ExtensionMetrics; +use codex_history::ResponseItemEnvelope; use codex_http_client::HttpClient; use codex_protocol::models::ContentItem; use codex_protocol::models::ImageReference; @@ -23,6 +28,7 @@ use tokio::task::AbortHandle; use tokio::task::JoinHandle; const MAX_CONCURRENT_REQUESTS: usize = 16; +pub(super) const URL: &str = "https://api.openai.com/v1/decisions"; const MODEL: &str = "gpt-6-luna"; // Provisional local rollout budgets, not Decisions API limits or model context limits. // Guardian owns the text/context budget; bound HTTP lifetime, image bytes and response buffering here. @@ -35,6 +41,8 @@ const MAX_RESPONSE_BYTES: usize = 16 * 1024; pub(super) enum DecisionsError { #[error("Decisions credentials are missing or empty")] Credentials, + #[error("Decisions HTTP client setup failed")] + ClientSetup, #[error("Decisions cannot represent the complete classifier evidence")] UnsupportedEvidence, #[error("Decisions input exceeds the request limit")] @@ -81,10 +89,35 @@ impl Drop for DecisionsTask { } impl DecisionsTask { - pub(super) async fn finish( + pub(super) async fn finish_and_record_outcome( mut self, - ) -> Result<(Result<&'static str, DecisionsError>, Duration), tokio::task::JoinError> { - (&mut self.handle).await + metrics: Option<&dyn ExtensionMetrics>, + ) { + let (result, _duration) = match (&mut self.handle).await { + Ok(result) => result, + Err(error) => { + let (outcome, reason) = if error.is_cancelled() { + ("skipped", "superseded") + } else { + ("failure", "task_error") + }; + record_decisions_comparison_outcome(metrics, outcome, reason); + return; + } + }; + let outcome = match &result { + Ok(_) => "success", + Err(DecisionsError::UnsupportedEvidence | DecisionsError::InputTooLarge) => "skipped", + Err(_) => "failure", + }; + record_decisions_comparison_outcome( + metrics, + outcome, + result + .as_ref() + .err() + .map_or("none", |error| decisions_failure_reason(*error)), + ); } } @@ -108,12 +141,35 @@ impl DecisionsSampler { } /// Retain the newest requests; the caller joins after baseline publication. - pub(super) fn start(self: &Arc, request: &LunaSamplingRequest) -> DecisionsTask { - let body = match request_body( + pub(super) fn spawn( + self: &Arc, + request: &LunaSamplingRequest, + max_input_tokens: usize, + ) -> DecisionsTask { + let body = request_body( &request.instructions, &request.input, request.parent_compaction.as_ref(), - ) { + ) + .and_then(|body| { + // Reuse Guardian's complete budget without changing its sampling path. + // The temporary copy is dropped before admission; Responses prepares again. + let (_, tokens) = LunaSampler::prepare_input( + request, + /*history*/ None, + request + .input + .iter() + .cloned() + .map(ResponseItemEnvelope::new) + .collect(), + ); + if tokens > max_input_tokens.saturating_sub(/*rhs*/ 256) { + return Err(DecisionsError::InputTooLarge); + } + Ok(body) + }); + let body = match body { Ok(body) => body, Err(error) => { return DecisionsTask { 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 new file mode 100644 index 0000000000..518420c08c --- /dev/null +++ b/codex-rs/ext/guardian-v2/src/async_scorer/decisions_lifecycle_tests.rs @@ -0,0 +1,217 @@ +//! Decisions outcomes cannot affect the real baseline score/publication path. +use super::*; +use crate::async_scorer::decisions::DecisionsSampler; +use codex_http_client::HttpClientBuilder; +use pretty_assertions::assert_eq; + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn decisions_disagreement_failures_and_disabled_flag_preserve_baseline() -> Result<()> { + skip_if_no_network!(Ok(())); + for (case, enabled, status, choice, baseline, expected_risk) in [ + ("disabled", false, 200, "high", "low", 0.0), + ("cannot_escalate", true, 200, "high", "low", 0.0), + ("cannot_lower", true, 200, "low", "high", 1.0), + ("candidate_failure", true, 403, "low", "low", 0.0), + ("baseline_failure", true, 200, "low", "invalid", 1.0), + ] { + let server = responses::start_mock_server().await; + let fixture = GuardianFailureFixture::new().await?; + let test = &fixture.test; + let mut config = test.config.clone(); + config.features.enable(Feature::GuardianApproval)?; + config.features.enable(Feature::GuardianV2)?; + if enabled { + config + .features + .enable(Feature::GuardianV2DecisionsComparison)?; + } + config.model_provider = + ModelProviderInfo::create_openai_provider(Some(format!("{}/v1", server.uri()))); + config.model_provider.supports_websockets = false; + let metrics = Arc::new(RecordingMetrics::default()); + let registry = &fixture.registry; + let session = &fixture.session_store; + let store = test.codex.thread_extension_data(); + let adaptive_model = store.get::().unwrap(); + let mut initial_model = adaptive_model.as_ref().clone(); + initial_model.guardian = Some(Default::default()); + store.insert(initial_model); + registry.thread_lifecycle_contributors()[0] + .on_thread_start(ThreadStartInput { + config: &config, + session_source: &SessionSource::Exec, + persistent_thread_state_available: false, + environments: &[], + mcp_resource_client: None, + extension_metrics: Some(metrics.clone()), + session_store: session, + thread_store: store, + }) + .await; + store.insert(adaptive_model.as_ref().clone()); + let baseline_mock = responses::mount_sse_once( + &server, + responses::sse(vec![ + ev_assistant_message("sample", baseline), + ev_completed("baseline-response"), + ]), + ) + .await; + let decisions_server = responses::start_mock_server().await; + wiremock::Mock::given(wiremock::matchers::method("POST")) + .respond_with( + wiremock::ResponseTemplate::new(status).set_body_json(json!({ + "answers":[{"type":"choice", "name":"guardian_risk", + "choice":choice}] + })), + ) + .expect(/*r*/ u64::from(enabled)) + .mount(&decisions_server) + .await; + // Inject only the transport, using the existing mock HTTP server instead of credentials. + if enabled { + store.insert(DecisionsSampler::new( + HttpClientBuilder::new().build_direct()?, + "synthetic-key".into(), + decisions_server.uri(), + )?); + } else { + assert!(store.get::().is_none()); + } + metrics.0.lock().unwrap().clear(); + fixture.score_tool(ToolName::plain("read_file")).await; + let score = tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { + loop { + if let Some(score) = cached_score(store) + // The fixture's initial score has no sampling timestamp. + && score.sampled_at.is_some() + { + break score; + } + tokio::task::yield_now().await; + } + }) + .await; + let score = score?; + assert_eq!( + score.scores, + BTreeMap::from([("action_risk".into(), expected_risk)]), + "{case}" + ); + let published = score.clone(); + if enabled { + tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { + loop { + if metrics.0.lock().unwrap().iter().any(|sample| matches!(sample, + RecordedMetric::Counter(name, _, _) if name == "codex.guardian_v2.decisions_comparison")) { break; } + tokio::task::yield_now().await; + } + }).await?; + if case == "cannot_escalate" { + let requests = decisions_server.received_requests().await.unwrap(); + let body: serde_json::Value = serde_json::from_slice(&requests[0].body)?; + let baseline_body = baseline_mock.single_request().body_json(); + let baseline_input = baseline_body["input"].as_array().unwrap(); + let policy = baseline_input + .iter() + .find(|item| item["role"] == "developer" && item["type"] == "message") + .unwrap(); + assert_eq!( + body["questions"][0]["instructions"], + policy["content"][0]["text"] + ); + let evidence = baseline_input + .iter() + .filter(|item| item["role"] == "user") + .map(|item| json!({"role": item["role"], "content":item["content"]})) + .collect::>(); + assert_eq!(body["input"], json!(evidence)); + } + let reason = if status == 403 { "http_403" } else { "none" }; + let outcomes = metrics.0.lock().unwrap().iter().filter(|sample| matches!(sample, + RecordedMetric::Counter(name, _, _) if name == "codex.guardian_v2.decisions_comparison")) + .cloned().collect::>(); + assert_eq!( + outcomes, + vec![RecordedMetric::Counter( + "codex.guardian_v2.decisions_comparison".into(), + 1, + vec![ + ( + "outcome".into(), + if reason == "none" { + "success" + } else { + "failure" + } + .into() + ), + ("reason".into(), reason.into()) + ] + )], + "{case}" + ); + } + assert_eq!(cached_score(store), Some(published), "{case}"); + decisions_server.verify().await; + assert_eq!(baseline_mock.requests().len(), 1); + } + Ok(()) +} + +#[tokio::test] +async fn guardian_budget_rejects_both_backends_before_sending() -> Result<()> { + skip_if_no_network!(Ok(())); + let fixture = GuardianFailureFixture::new().await?; + let server = responses::start_mock_server().await; + let store = fixture.test.codex.thread_extension_data(); + let mut config = fixture.test.config.clone(); + config.features.enable(Feature::GuardianApproval)?; + config.features.enable(Feature::GuardianV2)?; + config + .features + .enable(Feature::GuardianV2DecisionsComparison)?; + let metrics = Arc::new(RecordingMetrics::default()); + fixture.registry.thread_lifecycle_contributors()[0] + .on_thread_start(ThreadStartInput { + config: &config, + session_source: &SessionSource::Exec, + persistent_thread_state_available: false, + environments: &[], + mcp_resource_client: None, + extension_metrics: Some(metrics.clone()), + session_store: &fixture.session_store, + thread_store: store, + }) + .await; + 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(DecisionsSampler::new( + HttpClientBuilder::new().build_direct()?, + "synthetic-key".into(), + server.uri(), + )?); + metrics.0.lock().unwrap().clear(); + fixture.score_tool(ToolName::plain("read_file")).await; + fixture.assert_fails_closed("elevated_risk").await?; + tokio::time::timeout(ASYNC_TEST_TIMEOUT, async { + loop { + if metrics.0.lock().unwrap().iter().any(|sample| matches!(sample, + RecordedMetric::Counter(name, _, _) if name == "codex.guardian_v2.decisions_comparison")) { + break; + } + tokio::task::yield_now().await; + } + }).await?; + assert!(server.received_requests().await.unwrap().is_empty()); + assert!(metrics.0.lock().unwrap().contains(&RecordedMetric::Counter( + "codex.guardian_v2.decisions_comparison".into(), + 1, + vec![ + ("outcome".into(), "skipped".into()), + ("reason".into(), "input_too_large".into()) + ], + ))); + Ok(()) +} diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/decisions_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/decisions_tests.rs index b5d32b3225..dc6976fd75 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/decisions_tests.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/decisions_tests.rs @@ -76,7 +76,9 @@ async fn http_contract_and_untrusted_response_validation() { ) .unwrap(); let mut sampler = Arc::new(sampler); - assert_eq!(sampler.start(&request).finish().await.unwrap().0, Ok("low")); + let mut task = sampler.spawn(&request, /*max_input_tokens*/ 128_000); + assert_eq!((&mut task.handle).await.unwrap().0, Ok("low")); + drop(task); let mut invalid = response; invalid["answers"][0]["choice"] = json!("untrusted server text"); assert_eq!(parse_answer(&invalid), Err(DecisionsError::InvalidResponse)); @@ -112,17 +114,17 @@ async fn decisions_admits_newest_request_by_cancelling_oldest() { .acquire_many(MAX_CONCURRENT_REQUESTS as u32) .await .unwrap(); - let oldest = sampler.start(&request); + let mut oldest = sampler.spawn(&request, /*max_input_tokens*/ 128_000); let mut remaining = (1..MAX_CONCURRENT_REQUESTS) - .map(|_| sampler.start(&request)) + .map(|_| sampler.spawn(&request, /*max_input_tokens*/ 128_000)) .collect::>(); - remaining.push(sampler.start(&request)); - assert!(oldest.finish().await.unwrap_err().is_cancelled()); + remaining.push(sampler.spawn(&request, /*max_input_tokens*/ 128_000)); + assert!((&mut oldest.handle).await.unwrap_err().is_cancelled()); assert!(server.received_requests().await.unwrap().is_empty()); drop(permits); - for task in remaining { + for mut task in remaining { assert_eq!( - task.finish().await.unwrap().0, + (&mut task.handle).await.unwrap().0, Err(DecisionsError::Http(403)) ); } @@ -191,12 +193,13 @@ async fn cancelling_a_decisions_request_releases_thread_capacity() { ) .unwrap(), ); - let task = sampler.start(&super::super::sampler::tests::sample_request("turn")); + let request = super::super::sampler::tests::sample_request("turn"); + let task = sampler.spawn(&request, /*max_input_tokens*/ 128_000); tokio::time::timeout(Duration::from_secs(5), received.notified()) .await .unwrap(); { - let recording = task.finish(); + let recording = task.finish_and_record_outcome(/*metrics*/ None); tokio::pin!(recording); std::future::poll_fn(|cx| { assert!(std::future::Future::poll(recording.as_mut(), cx).is_pending()); 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 86020d125e..59eb05894f 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs @@ -102,6 +102,17 @@ impl ThreadLifecycleContributor for GuardianV2Extension { .thread_store .insert(ConversationBackend::new(Arc::clone(&sampler))); } + input + .thread_store + .remove::(); + if input + .config + .features + .enabled(Feature::GuardianV2DecisionsComparison) + && let Some(decisions_sampler) = super::startup::decisions_sampler(&input) + { + input.thread_store.insert(decisions_sampler); + } input.thread_store.insert(guardian_config); input.thread_store.insert(GuardianV2ScoreProgress::new( input.extension_metrics.clone(), 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 2aac990be2..70b5f18a73 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 @@ -3618,3 +3618,6 @@ async fn cached_score_publication_rejects_delayed_results_without_changing_cover assert!(!progress.inspect(Some("active-overflow")).oversized); Ok(()) } + +#[path = "decisions_lifecycle_tests.rs"] +mod decisions_lifecycle_tests; diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/metrics.rs b/codex-rs/ext/guardian-v2/src/async_scorer/metrics.rs index fcea468a4d..92ad375621 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/metrics.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/metrics.rs @@ -7,6 +7,7 @@ use codex_api::TransportError; use codex_core::context::GuardianContextMode; use codex_extension_api::ExtensionMetrics; +use super::decisions::DecisionsError; use super::sampler::LunaSamplerError; pub(super) const CLASSIFICATION_METRIC: &str = "codex.guardian_v2.classification"; @@ -156,3 +157,40 @@ pub(super) fn record_request_tokens( ); } } + +pub(super) fn record_decisions_comparison_outcome( + metrics: Option<&dyn ExtensionMetrics>, + outcome: &str, + reason: &str, +) { + if let Some(metrics) = metrics { + metrics.counter( + "codex.guardian_v2.decisions_comparison", + /*inc*/ 1, + &[("outcome", outcome), ("reason", reason)], + ); + } +} + +// Keep all backend diagnostics bounded and free of raw transport data. +pub(super) fn decisions_failure_reason(error: DecisionsError) -> &'static str { + match error { + DecisionsError::Credentials => "provider_error", + DecisionsError::ClientSetup => "request_build_error", + DecisionsError::UnsupportedEvidence => "decisions_unsupported_evidence", + DecisionsError::InputTooLarge => "input_too_large", + DecisionsError::Timeout => "transport_timeout", + // The adapter erases transport details; do not claim a specific network failure. + DecisionsError::Transport => "decisions_transport", + DecisionsError::Http(status) => match status { + 401 => "http_401", + 403 => "http_403", + 429 => "http_429", + 400..=499 => "http_4xx", + 500..=599 => "http_5xx", + _ => "http_other", + }, + DecisionsError::ResponseTooLarge => "response_too_large", + DecisionsError::InvalidResponse => "invalid_output", + } +} diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs b/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs index e71c6cc57b..275cc126ff 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs @@ -5,8 +5,6 @@ mod classification; mod config; mod conversation; mod coverage; -// Compile the transport in tests until the next stack stage wires its runtime caller. -#[cfg(test)] mod decisions; mod extension; mod metrics; 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 3c13957a90..65eafd5200 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/observation.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/observation.rs @@ -353,6 +353,9 @@ impl GuardianV2Extension { reservation, classification_started_at, sampler, + decisions_sampler: input + .thread_store + .get::(), guardian_config, score_progress, parent_model, diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/startup.rs b/codex-rs/ext/guardian-v2/src/async_scorer/startup.rs index 96d8c046a5..3bc743a5d7 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/startup.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/startup.rs @@ -15,6 +15,39 @@ use codex_model_provider::create_model_provider; use super::sampler::LunaSamplerConfig; use super::sampler::MODEL; +// Decisions setup failures are telemetry only; they never affect the baseline sampler. +pub(super) fn decisions_sampler( + input: &ThreadStartInput<'_, Config>, +) -> Option { + use super::decisions::DecisionsError; + use super::decisions::DecisionsSampler; + use super::decisions::URL; + use codex_http_client::ClientRouteClass; + let result = std::env::var("CODEX_GUARDIAN_DECISIONS_API_KEY") + .map_err(|_| DecisionsError::Credentials) + .and_then(|key| { + let client = input + .config + .http_client_factory() + .build_client_without_request_logging(URL, ClientRouteClass::Api) + .map_err(|_| DecisionsError::ClientSetup)?; + DecisionsSampler::new(client, key, URL.to_owned()) + }); + match result { + Ok(sampler) => Some(sampler), + Err(error) => { + if let Some(metrics) = input.extension_metrics.as_deref() { + metrics.counter( + "codex.guardian_v2.decisions_comparison.setup_failure", + /*inc*/ 1, + &[("reason", super::metrics::decisions_failure_reason(error))], + ); + } + None + } + } +} + pub(super) async fn sampler_config( input: &ThreadStartInput<'_, Config>, auth_manager: Arc, diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index 620a118037..8213bbfe79 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -359,6 +359,8 @@ pub enum Feature { GuardianConversationHistoryTools, /// Enable Guardian V2 automatic approval reviews. GuardianV2, + /// Run Decisions alongside Guardian V2 for measurement without changing approvals. + GuardianV2DecisionsComparison, /// Removed compatibility flag for the unused Guardian extension prototype. GuardianExt, /// Enable persisted thread goals and automatic goal continuation. @@ -1752,6 +1754,12 @@ pub const FEATURES: &[FeatureSpec] = &[ stage: Stage::UnderDevelopment, default_enabled: false, }, + FeatureSpec { + id: Feature::GuardianV2DecisionsComparison, + key: "guardianv2_decisions_comparison", + stage: Stage::UnderDevelopment, + default_enabled: false, + }, FeatureSpec { id: Feature::GuardianExt, key: "guardian_ext",