Files
codex/codex-rs/core/src/runtime_auth.rs
T
cooper-oai cce9d3228b Use runtime authentication for Analytics requests (#60101)
## What changed

- Honor `RuntimeAgentInstances` and the configured runtime auth mode for Analytics plan discovery and report requests to OpenAI endpoints.
- Share installation ID resolution and Codex runtime registration setup through `codex-runtime-agent-instance`.
- Preserve typed runtime authentication errors so expired credentials retain the “Sign in again to load Analytics” guidance.

## Testing

Add coverage for both runtime auth modes across regional and staging endpoints, fresh assertions for each request, sign-in guidance after bootstrap failure, redirect rejection, and invalid organization/project selectors.

GitOrigin-RevId: 50e0d959b3abd81ebe7a6a584e272f14cf52e504
2026-11-07 10:41:36 +00:00

65 lines
1.9 KiB
Rust

//! Construct and share the harness-owned runtime credential context.
use crate::config::Config;
use codex_features::Feature;
use codex_login::AuthManager;
use codex_protocol::error::Result as CodexResult;
use codex_runtime_agent_instance::RuntimeAuthManager;
use std::sync::Arc;
use std::sync::OnceLock;
// A later thread may enable runtime auth even when harness startup left it disabled.
pub(crate) type RuntimeAuthSlot = Arc<OnceLock<Arc<RuntimeAuthManager>>>;
/// Share this owner between thread sessions and first-party API clients.
/// Construction does not register a runtime or generate its key.
pub fn build_runtime_auth_manager(
config: &Config,
credentials: Arc<AuthManager>,
installation_id: &str,
) -> Option<Arc<RuntimeAuthManager>> {
runtime_for_config(
&RuntimeAuthSlot::default(),
config,
credentials,
installation_id,
)
}
pub(crate) fn runtime_for_config(
slot: &RuntimeAuthSlot,
config: &Config,
credentials: Arc<AuthManager>,
installation_id: &str,
) -> Option<Arc<RuntimeAuthManager>> {
config
.features
.enabled(Feature::RuntimeAgentInstances)
.then(|| {
Arc::clone(slot.get_or_init(|| {
Arc::new(RuntimeAuthManager::for_codex(
credentials,
&config.chatgpt_base_url,
installation_id,
))
}))
})
}
pub(crate) fn is_openai_realtime_url(base_url: &str) -> CodexResult<bool> {
let Ok(mut url) = url::Url::parse(base_url) else {
return Ok(false);
};
if url.scheme() == "wss" && url.set_scheme("https").is_err() {
return Ok(false);
}
codex_model_provider::validate_runtime_destination(Some(url.as_str()))?;
Ok(codex_runtime_agent_instance::is_openai_api_url(
url.as_str(),
))
}
#[cfg(test)]
#[path = "runtime_auth_tests.rs"]
mod tests;