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
@@ -39,9 +39,10 @@ use crate::outbound_proxy::AuthRouteConfig;
|
||||
pub static USER_AGENT_SUFFIX: LazyLock<Mutex<Option<String>>> = LazyLock::new(|| Mutex::new(None));
|
||||
pub const DEFAULT_ORIGINATOR: &str = "codex_cli_rs";
|
||||
pub const CODEX_INTERNAL_ORIGINATOR_OVERRIDE_ENV_VAR: &str = "CODEX_INTERNAL_ORIGINATOR_OVERRIDE";
|
||||
pub const RESIDENCY_HEADER_NAME: &str = "x-openai-internal-codex-residency";
|
||||
|
||||
pub use codex_config::ResidencyRequirement;
|
||||
pub use codex_model_provider_info::RESIDENCY_HEADER_NAME;
|
||||
pub use codex_model_provider_info::ResidencyRequirement;
|
||||
pub use codex_model_provider_info::read_managed_residency_requirement as read_default_client_residency_requirement;
|
||||
pub use codex_model_provider_info::set_managed_residency_requirement as set_default_client_residency_requirement;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Originator {
|
||||
@@ -49,8 +50,6 @@ pub struct Originator {
|
||||
pub header_value: HeaderValue,
|
||||
}
|
||||
static ORIGINATOR: LazyLock<RwLock<Option<Originator>>> = LazyLock::new(|| RwLock::new(None));
|
||||
static REQUIREMENTS_RESIDENCY: LazyLock<RwLock<Option<ResidencyRequirement>>> =
|
||||
LazyLock::new(|| RwLock::new(None));
|
||||
static ROUTE_AWARE_CLIENT_BUILD_PERMIT: tokio::sync::Semaphore =
|
||||
tokio::sync::Semaphore::const_new(1);
|
||||
|
||||
@@ -96,19 +95,6 @@ pub fn set_default_originator(value: String) -> Result<(), SetOriginatorError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_default_client_residency_requirement(enforce_residency: Option<ResidencyRequirement>) {
|
||||
let Ok(mut guard) = REQUIREMENTS_RESIDENCY.write() else {
|
||||
tracing::warn!("Failed to acquire requirements residency lock");
|
||||
return;
|
||||
};
|
||||
*guard = enforce_residency;
|
||||
}
|
||||
|
||||
/// Returns the current process-wide residency requirement.
|
||||
pub fn read_default_client_residency_requirement() -> Option<ResidencyRequirement> {
|
||||
REQUIREMENTS_RESIDENCY.read().ok().and_then(|guard| *guard)
|
||||
}
|
||||
|
||||
pub fn originator() -> Originator {
|
||||
if let Ok(guard) = ORIGINATOR.read()
|
||||
&& let Some(originator) = guard.as_ref()
|
||||
@@ -352,10 +338,7 @@ pub fn default_headers() -> HeaderMap {
|
||||
if let Ok(user_agent) = HeaderValue::from_str(&get_codex_user_agent()) {
|
||||
headers.insert(USER_AGENT, user_agent);
|
||||
}
|
||||
if let Ok(guard) = REQUIREMENTS_RESIDENCY.read()
|
||||
&& let Some(requirement) = guard.as_ref()
|
||||
&& !headers.contains_key(RESIDENCY_HEADER_NAME)
|
||||
{
|
||||
if let Some(requirement) = read_default_client_residency_requirement() {
|
||||
let value = match requirement {
|
||||
ResidencyRequirement::Us => HeaderValue::from_static("us"),
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user