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
+66 -27
View File
@@ -28,8 +28,6 @@ use std::future::Future;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio::sync::MutexGuard;
@@ -165,7 +163,7 @@ pub trait ModelsManager: fmt::Debug + Send + Sync {
None
}
/// Supply startup API-key discovery policy; live changes require a new session.
/// Update API-key discovery for this catalog and its derived catalogs.
/// Static catalogs ignore this setting.
fn set_api_key_model_discovery_enabled(&self, _enabled: bool) {}
@@ -344,16 +342,54 @@ impl CatalogSource {
}
}
/// Cache policy and live feature state shared by catalogs with different request credentials.
#[derive(Clone, Debug, Default)]
pub struct ModelCatalogSettings {
cache: Option<Arc<dyn ModelsCache>>,
pub api_key_model_discovery_default: bool,
// Before the host publishes a live change, each thread uses its configured default.
live_discovery: Arc<std::sync::Mutex<Option<bool>>>,
}
impl ModelCatalogSettings {
pub fn disk(codex_home: PathBuf) -> Self {
Self::with_cache(Arc::new(FileModelsCache::new(
codex_home.join(MODEL_CACHE_FILE),
DEFAULT_MODEL_CACHE_TTL,
)))
}
pub fn with_cache(cache: Arc<dyn ModelsCache>) -> Self {
Self {
cache: Some(cache),
..Self::default()
}
}
pub fn set_api_key_model_discovery_enabled(&self, enabled: bool) {
*self
.live_discovery
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(enabled);
}
fn api_key_model_discovery_enabled(&self) -> bool {
self.live_discovery
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.unwrap_or(self.api_key_model_discovery_default)
}
}
/// OpenAI-compatible model manager backed by bundled models, cache, and `/models`.
#[derive(Debug)]
pub struct OpenAiModelsManager {
originator_managers: std::sync::Mutex<HashMap<ModelCatalogOriginator, SharedModelsManager>>,
remote_models: RwLock<CatalogState>,
catalog_updates: watch::Sender<Option<Arc<RefreshedModelCatalog>>>,
cache: Option<Arc<dyn ModelsCache>>,
settings: ModelCatalogSettings,
endpoint_client: SharedModelsEndpointClient,
catalog_source: CatalogSource,
api_key_model_discovery_enabled: Arc<AtomicBool>,
auth_manager: Option<Arc<AuthManager>>,
}
@@ -372,12 +408,8 @@ impl OpenAiModelsManager {
endpoint_client: Arc<dyn ModelsEndpointClient>,
auth_manager: Option<Arc<AuthManager>>,
) -> Self {
let cache_path = codex_home.join(MODEL_CACHE_FILE);
Self::new_with_optional_cache(
Some(Arc::new(FileModelsCache::new(
cache_path,
DEFAULT_MODEL_CACHE_TTL,
))),
Self::with_settings(
ModelCatalogSettings::disk(codex_home),
endpoint_client,
auth_manager,
)
@@ -388,7 +420,11 @@ impl OpenAiModelsManager {
endpoint_client: Arc<dyn ModelsEndpointClient>,
auth_manager: Option<Arc<AuthManager>>,
) -> Self {
Self::new_with_optional_cache(/*cache*/ None, endpoint_client, auth_manager)
Self::with_settings(
ModelCatalogSettings::default(),
endpoint_client,
auth_manager,
)
}
/// Constructs an OpenAI-compatible model manager with a caller-provided cache.
@@ -400,11 +436,15 @@ impl OpenAiModelsManager {
endpoint_client: Arc<dyn ModelsEndpointClient>,
auth_manager: Option<Arc<AuthManager>>,
) -> Self {
Self::new_with_optional_cache(Some(cache), endpoint_client, auth_manager)
Self::with_settings(
ModelCatalogSettings::with_cache(cache),
endpoint_client,
auth_manager,
)
}
fn new_with_optional_cache(
cache: Option<Arc<dyn ModelsCache>>,
pub fn with_settings(
settings: ModelCatalogSettings,
endpoint_client: Arc<dyn ModelsEndpointClient>,
auth_manager: Option<Arc<AuthManager>>,
) -> Self {
@@ -418,8 +458,7 @@ impl OpenAiModelsManager {
identity: endpoint_client.identity(),
models: load_remote_models_from_file().unwrap_or_default(),
})),
cache,
api_key_model_discovery_enabled: Arc::new(AtomicBool::new(false)),
settings,
endpoint_client,
catalog_source: CatalogSource::Default,
auth_manager,
@@ -503,6 +542,7 @@ impl ModelsManager for OpenAiModelsManager {
state.model_catalog(self.models_for_entry(&state.entry))
})
}
fn for_originator(&self, originator: &str) -> Option<SharedModelsManager> {
let originator = ModelCatalogOriginator::from_originator(originator)?;
let mut managers = self
@@ -514,23 +554,22 @@ impl ModelsManager for OpenAiModelsManager {
}
let endpoint = self.endpoint_client.for_originator(originator)?;
// A backend that cannot partition must not overwrite the default catalog.
let cache = self
let mut settings = self.settings.clone();
settings.cache = settings
.cache
.as_ref()
.and_then(|cache| cache.for_originator(originator));
let mut manager = Self::new_with_optional_cache(cache, endpoint, self.auth_manager.clone());
let mut manager = Self::with_settings(settings, endpoint, self.auth_manager.clone());
if matches!(self.catalog_source, CatalogSource::ExplicitProvider(_)) {
manager = manager.with_provider_catalog();
}
manager.api_key_model_discovery_enabled = Arc::clone(&self.api_key_model_discovery_enabled);
let manager: SharedModelsManager = Arc::new(manager);
managers.insert(originator, Arc::clone(&manager));
Some(manager)
}
fn set_api_key_model_discovery_enabled(&self, enabled: bool) {
self.api_key_model_discovery_enabled
.store(enabled, Ordering::SeqCst);
self.settings.set_api_key_model_discovery_enabled(enabled);
}
fn refresh_after_auth_change(
@@ -657,7 +696,7 @@ impl OpenAiModelsManager {
&& Some(&identity) == self.endpoint_client.identity().as_ref()
&& current_etag.as_deref() == Some(etag.as_str())
{
if let Some(cache) = self.cache.as_ref()
if let Some(cache) = self.settings.cache.as_ref()
&& let Err(err) = cache
.refresh_ttl(&crate::client_version_to_whole(), &identity, &etag)
.await
@@ -761,7 +800,7 @@ impl OpenAiModelsManager {
self.catalog_updates.send_replace(None);
}
if let Some(identity) = request_identity
&& let Some(cache) = self.cache.as_ref()
&& let Some(cache) = self.settings.cache.as_ref()
{
let invalidation = async {
if let Some(mut entry) = cache.load(&client_version).await?
@@ -798,7 +837,7 @@ impl OpenAiModelsManager {
identity: Some(identity),
models,
};
if let Some(cache) = self.cache.as_ref()
if let Some(cache) = self.settings.cache.as_ref()
&& let Err(err) = cache.store(&entry).await
{
error!("failed to write models cache: {err}");
@@ -821,7 +860,7 @@ impl OpenAiModelsManager {
self.uses_api_key_auth()
&& !self.endpoint_client.has_command_auth()
&& (!self.endpoint_client.supports_api_key_models()
|| !self.api_key_model_discovery_enabled.load(Ordering::SeqCst))
|| !self.settings.api_key_model_discovery_enabled())
}
fn uses_api_key_auth(&self) -> bool {
@@ -874,7 +913,7 @@ impl OpenAiModelsManager {
/// Attempt to satisfy the refresh from the cache when it matches the provider and TTL.
async fn try_load_cache(&self) -> bool {
let Some(cache) = self.cache.as_ref() else {
let Some(cache) = self.settings.cache.as_ref() else {
return false;
};
let _timer =