Use runtime authentication for TUI WebRTC calls (#62870)

## Why

The TUI creates WebRTC calls locally, and that request path did not use
runtime agent authentication even when runtime agent instances were enabled.

## What changed

- Reuse local credential and runtime managers across calls while the login
  configuration stays the same. Reload first-party credentials before
  creating a call and keep network policy under the primary account owner's
  control.
- Use the shared provider request resolver for call authentication and HTTP
  transport. Apply the WebRTC base URL override before resolving credentials
  so runtime authentication is selected for the actual destination.

## Testing

Add mock-server coverage for concurrent calls sharing a runtime with fresh
assertions, credential changes, and both dual-header and assertion-only
modes. Cover the feature toggle, custom call destinations, network policy
permits, and provider credentials when the local login file is malformed.

GitOrigin-RevId: 6503b3375c79d3126911d1d7c1aea898face37b9
This commit is contained in:
cooper-oai
2026-11-21 00:20:14 +00:00
committed by copyberry
parent cf8992e940
commit e346540867
11 changed files with 520 additions and 83 deletions
+2
View File
@@ -9,6 +9,7 @@ mod combined_auth;
mod gateway_auth;
mod gateway_export;
mod gcp;
mod local_auth;
mod models_endpoint;
mod models_identity;
mod provider;
@@ -48,6 +49,7 @@ pub use codex_model_provider_info::GCP_PROVIDER_ID;
pub use codex_model_provider_info::RemoteCompactionSupport;
pub use codex_protocol::account::ProviderAccount;
pub use gateway_auth::GatewayAuthManager;
pub use local_auth::LocalProviderAuth;
pub use provider::ModelProvider;
pub use provider::ModelProviderFuture;
pub use provider::ProviderAccountError;
+82
View File
@@ -0,0 +1,82 @@
//! Local credential and runtime ownership for frontends that prepare provider requests.
use crate::DEFAULT_CHATGPT_BASE_URL;
use crate::ProviderAuthContext;
use crate::SharedModelProvider;
use crate::provider::provider_uses_first_party_auth_path;
use codex_features::RuntimeAuthMode;
use codex_login::AuthConfig;
use codex_login::AuthManager;
use codex_model_provider_info::ModelProviderInfo;
use codex_runtime_agent_instance::RuntimeAuthManager;
use codex_runtime_agent_instance::resolve_installation_id;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::sync::OnceCell;
/// Reuse local credentials and their runtime while the login configuration stays the same.
#[derive(Default)]
pub struct LocalProviderAuth {
auth: Mutex<Option<Arc<LocalAuth>>>,
}
struct LocalAuth {
config: AuthConfig,
credentials: OnceCell<Arc<AuthManager>>,
runtime: OnceCell<Arc<RuntimeAuthManager>>,
}
impl LocalProviderAuth {
pub async fn model_provider(
&self,
info: ModelProviderInfo,
config: AuthConfig,
runtime_auth_mode: Option<RuntimeAuthMode>,
) -> std::io::Result<SharedModelProvider> {
let auth = {
let mut cached = self.auth.lock().await;
if cached.as_ref().is_some_and(|auth| auth.config != config) {
*cached = None;
}
Arc::clone(cached.get_or_insert_with(|| {
Arc::new(LocalAuth {
config,
credentials: OnceCell::new(),
runtime: OnceCell::new(),
})
}))
};
let credentials = auth
.credentials
.get_or_try_init(|| AuthManager::shared_for_auxiliary_client(auth.config.clone()))
.await?;
if provider_uses_first_party_auth_path(&info) {
credentials.reload_with_cached_auth().await?;
}
let runtime_auth = if let Some(mode) = runtime_auth_mode {
let runtime = auth
.runtime
.get_or_try_init(|| async {
let codex_home = auth.config.codex_home.clone().try_into()?;
let installation_id = resolve_installation_id(&codex_home).await?;
Ok::<_, std::io::Error>(Arc::new(RuntimeAuthManager::for_codex(
Arc::clone(credentials),
auth.config
.chatgpt_base_url
.as_deref()
.unwrap_or(DEFAULT_CHATGPT_BASE_URL),
&installation_id,
)))
})
.await?;
Some((Arc::clone(runtime), mode))
} else {
None
};
Ok(ProviderAuthContext {
credentials: Arc::clone(credentials),
runtime_auth,
}
.model_provider(info))
}
}