Wire shared runtime authentication through threads and API clients (#59177)

## What changed

- Share a lazily registered runtime across threads, model discovery, and extension clients when `runtime_agent_instances` is enabled, while honoring each thread's authentication mode and credential owner.
- Bind model catalogs to the effective provider and authentication context while preserving cache policy and live API-key discovery settings.
- Resolve credentials, routing, and transport policy together for each request. Preserve proxy and TLS settings, validate runtime account selectors, and prevent pooled connections from crossing runtime identities.
- Accept supported ChatGPT subdomains when selecting the registration environment.

## Testing

Add integration coverage for shared runtime registration, dual-header and assertion-only modes, disabled and third-party providers, credential replacement, subagents, and delegated review. Add coverage for catalog cache policy and runtime-aware connection reuse.

GitOrigin-RevId: 99dd9d987c29c93597a648f6586f98ac6a932c3c
This commit is contained in:
cooper-oai
2026-11-03 15:02:57 +00:00
committed by copyberry
parent f7167ac895
commit 079e17a422
71 changed files with 2636 additions and 820 deletions
+53
View File
@@ -0,0 +1,53 @@
//! Construct and share the harness-owned runtime credential context.
use crate::config::Config;
use codex_features::Feature;
use codex_login::AuthManager;
use codex_runtime_agent_instance::RuntimeAuthManager;
use codex_runtime_agent_instance::RuntimeRegistrationMetadata;
use codex_runtime_agent_instance::RuntimeRole;
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_chatgpt(
credentials,
installation_id.to_owned(),
config.chatgpt_base_url.clone(),
RuntimeRegistrationMetadata {
harness: Some("codex".to_string()),
harness_version: Some(env!("CARGO_PKG_VERSION").to_string()),
role: RuntimeRole::Orchestrator,
},
))
}))
})
}