Add system proxy fallback for login and startup requests (#46562)

## Why

Login and enterprise configuration bootstrap can fail when their endpoints are reachable only through the system proxy, before cloud configuration can enable `respect_system_proxy`.

## What changed

- Enable `features.system_proxy_fallback` by default, honor managed feature requirements, and prevent project configuration from changing it.
- Retry account discovery and cloud configuration GETs through the system proxy after connection failures or timeouts, bounding the initial request and body read to five seconds.
- Retry OAuth authorization-code exchange only on connection failures before any redirect, with a ten-second connection timeout. Reuse the successful client for the subsequent API-key exchange to avoid changing routes.
- Increase workspace discovery and cloud configuration timeouts to 15 and 20 seconds, respectively, to accommodate fallback.

## Testing

Add coverage for proxy-only browser login through the first completed turn, including cloud configuration enabling `respect_system_proxy` without a restart. Cover stalled GET bodies, preserved request headers, disabled fallback, managed requirements, and project configuration restrictions. Verify that OAuth HTTP errors, redirects followed by connection failures, and failures after sending the POST do not trigger proxy retries, while successful redirects remain supported.

GitOrigin-RevId: 854eeeac186bb4865218ce645019c1e4bff85425
This commit is contained in:
Abhinav
2026-09-19 01:37:14 +00:00
committed by copyberry
parent 888be42a20
commit 3bb0a530d1
23 changed files with 1134 additions and 71 deletions
+113
View File
@@ -2055,6 +2055,10 @@ respect_system_proxy = true
.outbound_proxy_policy(),
codex_http_client::OutboundProxyPolicy::ReqwestDefault
);
assert!(
resolve_bootstrap_http_client_factory(&configured, Some(&disabled))?
.allows_system_proxy_fallback()
);
let configured = ConfigToml::default();
let enabled = Sourced::new(
@@ -2073,6 +2077,115 @@ respect_system_proxy = true
.outbound_proxy_policy(),
codex_http_client::OutboundProxyPolicy::RespectSystemProxy
);
assert!(
!resolve_bootstrap_http_client_factory(&configured, Some(&enabled))?
.allows_system_proxy_fallback()
);
assert!(
resolve_bootstrap_http_client_factory(&configured, /*feature_requirements*/ None)?
.allows_system_proxy_fallback()
);
Ok(())
}
#[tokio::test]
async fn system_proxy_fallback_config_matches_bootstrap() -> std::io::Result<()> {
for (features, expected_fallback, expected_policy) in [
(
"",
true,
codex_http_client::OutboundProxyPolicy::ReqwestDefault,
),
(
"respect_system_proxy = false",
true,
codex_http_client::OutboundProxyPolicy::ReqwestDefault,
),
(
"system_proxy_fallback = false",
false,
codex_http_client::OutboundProxyPolicy::ReqwestDefault,
),
(
"respect_system_proxy = true",
false,
codex_http_client::OutboundProxyPolicy::RespectSystemProxy,
),
(
"respect_system_proxy = true\nsystem_proxy_fallback = false",
false,
codex_http_client::OutboundProxyPolicy::RespectSystemProxy,
),
] {
let codex_home = TempDir::new()?;
std::fs::write(
codex_home.path().join(CONFIG_TOML_FILE),
format!("[features]\n{features}\n"),
)?;
let config = ConfigBuilder::without_managed_config_for_tests()
.codex_home(codex_home.path().to_path_buf())
.build()
.await?;
let bootstrap = ConfigToml {
features: Some(toml::from_str(features).expect("valid feature configuration")),
..Default::default()
};
let factory =
resolve_bootstrap_http_client_factory(&bootstrap, /*feature_requirements*/ None)?;
assert_eq!(
factory.allows_system_proxy_fallback(),
expected_fallback,
"{features}"
);
assert_eq!(
factory.outbound_proxy_policy(),
expected_policy,
"{features}"
);
assert_eq!(config.http_client_factory(), factory, "{features}");
assert_eq!(
config.auth_route_config().http_client_factory(),
&factory,
"{features}"
);
}
Ok(())
}
#[tokio::test]
async fn system_proxy_fallback_honors_feature_requirements() -> std::io::Result<()> {
for (configured, required) in [(true, false), (false, true)] {
let cfg = ConfigToml {
features: Some(
toml::from_str(&format!("system_proxy_fallback = {configured}"))
.expect("valid features"),
),
..Default::default()
};
let requirements = Sourced::new(
FeatureRequirementsToml {
entries: BTreeMap::from([("system_proxy_fallback".to_string(), required)]),
},
RequirementSource::Unknown,
);
let bootstrap = resolve_bootstrap_http_client_factory(&cfg, Some(&requirements))?;
assert_eq!(bootstrap.allows_system_proxy_fallback(), required);
let codex_home = TempDir::new()?;
std::fs::write(
codex_home.path().join(CONFIG_TOML_FILE),
format!("[features]\nsystem_proxy_fallback = {configured}\n"),
)?;
let config = ConfigBuilder::without_managed_config_for_tests()
.codex_home(codex_home.path().to_path_buf())
.cloud_config_bundle(
CloudConfigBundleFixture::loader_with_enterprise_requirement(format!(
"[features]\nsystem_proxy_fallback = {required}\n"
)),
)
.build()
.await?;
assert_eq!(config.http_client_factory(), bootstrap);
}
Ok(())
}
+28 -20
View File
@@ -1650,7 +1650,10 @@ impl Config {
} else {
OutboundProxyPolicy::ReqwestDefault
};
let factory = HttpClientFactory::new(outbound_proxy_policy);
let mut factory = HttpClientFactory::new(outbound_proxy_policy);
if !self.respect_system_proxy && self.features.enabled(Feature::SystemProxyFallback) {
factory = factory.with_system_proxy_fallback();
}
if self.features.enabled(Feature::Psp) {
factory.with_chatgpt_cookies([HeaderValue::from_static("oai-chat-psp=true")])
} else {
@@ -2976,17 +2979,8 @@ pub fn resolve_bootstrap_respect_system_proxy(
cfg: &ConfigToml,
feature_requirements: Option<&Sourced<FeatureRequirementsToml>>,
) -> std::io::Result<bool> {
let configured_features = Features::from_sources(
FeatureConfigSource {
features: cfg.features.as_ref(),
experimental_use_unified_exec_tool: cfg.experimental_use_unified_exec_tool,
},
FeatureConfigSource::default(),
FeatureOverrides::default(),
);
let features =
ManagedFeatures::from_configured(configured_features, feature_requirements.cloned())?;
Ok(features.get().enabled(Feature::RespectSystemProxy))
resolve_bootstrap_http_client_factory(cfg, feature_requirements)
.map(|factory| factory.outbound_proxy_policy() == OutboundProxyPolicy::RespectSystemProxy)
}
/// Resolves auth route settings for the initial cloud-config bootstrap.
@@ -3003,14 +2997,28 @@ pub fn resolve_bootstrap_http_client_factory(
cfg: &ConfigToml,
feature_requirements: Option<&Sourced<FeatureRequirementsToml>>,
) -> std::io::Result<HttpClientFactory> {
resolve_bootstrap_respect_system_proxy(cfg, feature_requirements).map(|respect_system_proxy| {
let outbound_proxy_policy = if respect_system_proxy {
OutboundProxyPolicy::RespectSystemProxy
} else {
OutboundProxyPolicy::ReqwestDefault
};
HttpClientFactory::new(outbound_proxy_policy)
})
let configured_features = Features::from_sources(
FeatureConfigSource {
features: cfg.features.as_ref(),
experimental_use_unified_exec_tool: cfg.experimental_use_unified_exec_tool,
},
FeatureConfigSource::default(),
FeatureOverrides::default(),
);
let features =
ManagedFeatures::from_configured(configured_features, feature_requirements.cloned())?;
let outbound_proxy_policy = if features.enabled(Feature::RespectSystemProxy) {
OutboundProxyPolicy::RespectSystemProxy
} else {
OutboundProxyPolicy::ReqwestDefault
};
let mut factory = HttpClientFactory::new(outbound_proxy_policy);
if outbound_proxy_policy == OutboundProxyPolicy::ReqwestDefault
&& features.enabled(Feature::SystemProxyFallback)
{
factory = factory.with_system_proxy_fallback();
}
Ok(factory)
}
pub(crate) fn resolve_web_search_mode_for_turn(