Lightweight coding agent that runs in your terminal
Rust
Use cached login credentials for Guardian decisions (#53389)
swastik-oai27e6192e84fa
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.rsindex 57823748e9..af6a783356 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@@ -117,6 +117,45 @@ fn adapter_preserves_trusted_tool_authority_and_scope() {}}+#[tokio::test]+async fn decisions_key_lookup_honors_override_or_reloaded_login() {+ let home = tempfile::tempdir().unwrap();+ let manager = codex_login::AuthManager::from_auth_for_testing_with_home(+ codex_login::CodexAuth::from_api_key("old-account-key"),+ home.path().to_path_buf(),+ );+ codex_login::login_with_api_key(+ home.path(),+ "synthetic-key",+ codex_login::AuthCredentialsStoreMode::File,+ codex_login::AuthKeyringBackendKind::default(),+ )+ .unwrap();+ manager.reload().await;+ let default =+ super::super::startup::decisions_api_key("openai", /*base_url*/ None, &manager);+ let custom_url = super::super::startup::decisions_api_key(+ "openai",+ Some("https://gateway.example/v1"),+ &manager,+ );+ let expected = match std::env::var("CODEX_GUARDIAN_DECISIONS_API_KEY") {+ Ok(key) => (Ok(key.clone()), Ok(key)),+ Err(std::env::VarError::NotPresent) => {+ (Ok("synthetic-key".into()), Err(DecisionsError::Credentials))+ }+ Err(std::env::VarError::NotUnicode(_)) => (+ Err(DecisionsError::Credentials),+ Err(DecisionsError::Credentials),+ ),+ };+ // Never print credentials on assertion failure.+ assert!(+ (default, custom_url) == expected,+ "the Guardian override must win, or only the default provider may use the reloaded key"+ );+}+#[tokio::test]async fn http_contract_and_untrusted_response_validation() {core_test_support::skip_if_no_network!();diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs b/codex-rs/ext/guardian-v2/src/async_scorer/extension.rsindex 59eb05894f..94ea7f1cf9 100644--- a/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs+++ b/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs@@ -109,7 +109,8 @@ impl ThreadLifecycleContributor<Config> for GuardianV2Extension {.config.features.enabled(Feature::GuardianV2DecisionsComparison)- && let Some(decisions_sampler) = super::startup::decisions_sampler(&input)+ && let Some(decisions_sampler) =+ super::startup::decisions_sampler(&input, &self.auth_manager){input.thread_store.insert(decisions_sampler);}diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/startup.rs b/codex-rs/ext/guardian-v2/src/async_scorer/startup.rsindex e7a1b300c2..f355cd2869 100644--- a/codex-rs/ext/guardian-v2/src/async_scorer/startup.rs+++ b/codex-rs/ext/guardian-v2/src/async_scorer/startup.rs@@ -11,25 +11,26 @@ use codex_extension_api::ThreadStartInput;use codex_features::Feature;use codex_login::AgentIdentityAuthPolicy;use codex_login::AuthManager;+use codex_login::CodexAuth;use codex_model_provider::create_model_provider;+use super::decisions::DecisionsError;+use super::decisions::DecisionsSampler;+use super::decisions::URL;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<super::decisions::DecisionsSampler> {- use super::decisions::DecisionsError;- use super::decisions::DecisionsSampler;- use super::decisions::URL;+ auth_manager: &AuthManager,+) -> Option<DecisionsSampler> {use codex_http_client::ClientRouteClass;let result = decisions_api_key(&input.config.model_provider_id,input.config.model_provider.base_url.as_deref(),- |name| std::env::var(name),+ auth_manager,)- .map_err(|_| DecisionsError::Credentials).and_then(|key| {let client = input.config@@ -53,17 +54,25 @@ pub(super) fn decisions_sampler(}}-fn decisions_api_key(+pub(super) fn decisions_api_key(provider_id: &str,base_url: Option<&str>,- read_env: impl Fn(&str) -> Result<String, std::env::VarError>,-) -> Result<String, std::env::VarError> {- read_env("CODEX_GUARDIAN_DECISIONS_API_KEY").or_else(|error| match error {- std::env::VarError::NotPresent if provider_id == "openai" && base_url.is_none() => {- read_env("OPENAI_API_KEY")+ auth_manager: &AuthManager,+) -> Result<String, DecisionsError> {+ match std::env::var("CODEX_GUARDIAN_DECISIONS_API_KEY") {+ Ok(key) => Ok(key),+ Err(std::env::VarError::NotPresent) if provider_id == "openai" && base_url.is_none() => {+ auth_manager+ .auth_cached()+ .as_ref()+ .and_then(CodexAuth::api_key)+ .map(str::to_owned)+ .ok_or(DecisionsError::Credentials)}- std::env::VarError::NotPresent | std::env::VarError::NotUnicode(_) => Err(error),- })+ Err(std::env::VarError::NotPresent | std::env::VarError::NotUnicode(_)) => {+ Err(DecisionsError::Credentials)+ }+ }}pub(super) async fn sampler_config(@@ -119,7 +128,3 @@ pub(super) async fn sampler_config(metrics: input.extension_metrics.clone(),}}--#[cfg(test)]-#[path = "startup_tests.rs"]-mod tests;diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/startup_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/startup_tests.rsdeleted file mode 100644index e0e3a996b3..0000000000--- a/codex-rs/ext/guardian-v2/src/async_scorer/startup_tests.rs+++ /dev/null@@ -1,32 +0,0 @@-//! Credential selection honors dedicated keys and restricts ambient-key fallback.--use super::decisions_api_key;-use pretty_assertions::assert_eq;-use std::env::VarError;--#[test]-fn decisions_key_fallback_preserves_precedence_and_provider_boundary() {- for (provider, base_url, dedicated, expected) in [- ("openai", None, None, Some("standard")),- ("openai", None, Some("dedicated"), Some("dedicated")),- ("openai", None, Some(""), Some("")),- ("custom", None, None, None),- ("openai", Some("https://gateway.example/v1"), None, None),- ("custom", None, Some("dedicated"), Some("dedicated")),- ] {- let result = decisions_api_key(provider, base_url, |name| match name {- "CODEX_GUARDIAN_DECISIONS_API_KEY" => {- dedicated.map(str::to_owned).ok_or(VarError::NotPresent)- }- "OPENAI_API_KEY" => {- assert_eq!((provider, base_url, dedicated), ("openai", None, None));- Ok("standard".into())- }- _ => panic!("unexpected environment variable: {name}"),- });- assert_eq!(- result,- expected.map(str::to_owned).ok_or(VarError::NotPresent)- );- }-}