mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Enforce managed residency when constructing API providers (#46011)
## Why Realtime connections construct API providers directly, bypassing the residency override applied by higher-level model provider code. Configured provider headers could therefore take precedence over managed residency requirements. ## What changed Apply the shared process-wide residency requirement in `ModelProviderInfo::to_api_provider`. With `enforce_residency = "us"`, the managed value overrides both static and environment-supplied residency headers, including for realtime WebSocket connections and WebRTC calls and sideband connections. Preserve unrelated headers and configured residency values when no managed requirement is set. Share the residency policy with default HTTP headers, remove redundant enforcement at callers, and recover the stored policy from poisoned locks. ## Testing Add a provider unit test for managed and unmanaged header behavior without mutating provider configuration. Add realtime integration coverage across WebSocket and WebRTC transports, managed and unmanaged residency, and static and environment-supplied headers. GitOrigin-RevId: 98ceb456cc97f88241df5a829c72288aa5e66aec
This commit is contained in:
1 parent
3a589370a4
commit
bee042d119
8 files changed
+246
-51
No files matched your search
@@ -4,6 +4,9 @@
|
||||
//! 1. Built-in defaults compiled into the binary so Codex works out-of-the-box.
|
||||
//! 2. User-defined entries inside `~/.codex/config.toml` under the `model_providers`
|
||||
//! key. These override or extend the defaults at runtime.
|
||||
//!
|
||||
//! API provider construction applies the process-wide managed residency policy also
|
||||
//! used by default HTTP headers.
|
||||
|
||||
use codex_api::Provider as ApiProvider;
|
||||
use codex_api::RetryConfig as ApiRetryConfig;
|
||||
@@ -24,8 +27,35 @@ use std::fmt;
|
||||
use std::num::NonZeroU64;
|
||||
use std::path::Component;
|
||||
use std::path::Path;
|
||||
use std::sync::PoisonError;
|
||||
use std::sync::RwLock;
|
||||
use std::time::Duration;
|
||||
|
||||
pub const RESIDENCY_HEADER_NAME: &str = "x-openai-internal-codex-residency";
|
||||
|
||||
#[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum ResidencyRequirement {
|
||||
Us,
|
||||
}
|
||||
|
||||
static REQUIREMENTS_RESIDENCY: RwLock<Option<ResidencyRequirement>> = RwLock::new(None);
|
||||
|
||||
/// Sets the process-wide residency requirement loaded from managed configuration.
|
||||
pub fn set_managed_residency_requirement(enforce_residency: Option<ResidencyRequirement>) {
|
||||
// Recover the stored policy if the lock is poisoned rather than silently disabling it.
|
||||
*REQUIREMENTS_RESIDENCY
|
||||
.write()
|
||||
.unwrap_or_else(PoisonError::into_inner) = enforce_residency;
|
||||
}
|
||||
|
||||
/// Returns the current process-wide managed residency requirement.
|
||||
pub fn read_managed_residency_requirement() -> Option<ResidencyRequirement> {
|
||||
*REQUIREMENTS_RESIDENCY
|
||||
.read()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
|
||||
const DEFAULT_STREAM_IDLE_TIMEOUT_MS: u64 = 300_000;
|
||||
const DEFAULT_STREAM_MAX_RETRIES: u64 = 5;
|
||||
const DEFAULT_REQUEST_MAX_RETRIES: u64 = 4;
|
||||
@@ -367,6 +397,7 @@ other non-default provider fields are not supported"
|
||||
Ok(headers)
|
||||
}
|
||||
|
||||
/// Builds an API provider with managed residency taking precedence over configured headers.
|
||||
pub fn to_api_provider(&self, auth_mode: Option<AuthMode>) -> CodexResult<ApiProvider> {
|
||||
let default_base_url = if matches!(
|
||||
auth_mode,
|
||||
@@ -387,7 +418,13 @@ other non-default provider fields are not supported"
|
||||
.clone()
|
||||
.unwrap_or_else(|| default_base_url.to_string());
|
||||
|
||||
let headers = self.build_header_map()?;
|
||||
let mut headers = self.build_header_map()?;
|
||||
if let Some(requirement) = read_managed_residency_requirement() {
|
||||
let value = match requirement {
|
||||
ResidencyRequirement::Us => HeaderValue::from_static("us"),
|
||||
};
|
||||
headers.insert(RESIDENCY_HEADER_NAME, value);
|
||||
}
|
||||
let retry = ApiRetryConfig {
|
||||
max_attempts: self.request_max_retries(),
|
||||
base_delay: Duration::from_millis(200),
|
||||
|
||||
@@ -5,6 +5,54 @@ use pretty_assertions::assert_eq;
|
||||
use std::num::NonZeroU64;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn test_api_provider_applies_current_managed_residency() {
|
||||
let info = ModelProviderInfo {
|
||||
http_headers: Some(maplit::hashmap! {
|
||||
"X-OpenAI-Internal-Codex-Residency".to_string() => "eu".into(),
|
||||
"x-provider-header".to_string() => "preserved".into(),
|
||||
}),
|
||||
..ModelProviderInfo::create_openai_provider(/*base_url*/ None)
|
||||
};
|
||||
let original_info = info.clone();
|
||||
let previous_requirement = read_managed_residency_requirement();
|
||||
set_managed_residency_requirement(Some(ResidencyRequirement::Us));
|
||||
let managed = info.to_api_provider(/*auth_mode*/ None);
|
||||
set_managed_residency_requirement(/*enforce_residency*/ None);
|
||||
let unmanaged = info.to_api_provider(/*auth_mode*/ None);
|
||||
set_managed_residency_requirement(previous_requirement);
|
||||
|
||||
assert_eq!(
|
||||
managed.expect("managed provider should resolve").headers,
|
||||
HeaderMap::from_iter([
|
||||
(
|
||||
HeaderName::from_static(RESIDENCY_HEADER_NAME),
|
||||
HeaderValue::from_static("us")
|
||||
),
|
||||
(
|
||||
HeaderName::from_static("x-provider-header"),
|
||||
HeaderValue::from_static("preserved")
|
||||
),
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
unmanaged
|
||||
.expect("unmanaged provider should resolve")
|
||||
.headers,
|
||||
HeaderMap::from_iter([
|
||||
(
|
||||
HeaderName::from_static(RESIDENCY_HEADER_NAME),
|
||||
HeaderValue::from_static("eu")
|
||||
),
|
||||
(
|
||||
HeaderName::from_static("x-provider-header"),
|
||||
HeaderValue::from_static("preserved")
|
||||
),
|
||||
])
|
||||
);
|
||||
assert_eq!(info, original_info);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_ollama_model_provider_toml() {
|
||||
let azure_provider_toml = r#"
|
||||
|
||||
Reference in new issue
Block a user