mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
Defer Windows network validation during cloud bootstrap
This commit is contained in:
@@ -148,6 +148,17 @@ impl ConfigManager {
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn load_config_for_cloud_config_bootstrap(&self) -> std::io::Result<Config> {
|
||||
self.load_with_cli_overrides_inner(
|
||||
&self.current_cli_overrides(),
|
||||
/*request_overrides*/ None,
|
||||
ConfigOverrides::default(),
|
||||
/*fallback_cwd*/ None,
|
||||
/*defer_windows_network_validation*/ true,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn load_latest_config_for_thread(
|
||||
&self,
|
||||
thread_config: &Config,
|
||||
@@ -215,11 +226,29 @@ impl ConfigManager {
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
pub(crate) async fn load_with_cli_overrides(
|
||||
&self,
|
||||
cli_overrides: &[(String, TomlValue)],
|
||||
request_overrides: Option<HashMap<String, serde_json::Value>>,
|
||||
typesafe_overrides: ConfigOverrides,
|
||||
fallback_cwd: Option<PathBuf>,
|
||||
) -> std::io::Result<Config> {
|
||||
self.load_with_cli_overrides_inner(
|
||||
cli_overrides,
|
||||
request_overrides,
|
||||
typesafe_overrides,
|
||||
fallback_cwd,
|
||||
/*defer_windows_network_validation*/ false,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn load_with_cli_overrides_inner(
|
||||
&self,
|
||||
cli_overrides: &[(String, TomlValue)],
|
||||
request_overrides: Option<HashMap<String, serde_json::Value>>,
|
||||
mut typesafe_overrides: ConfigOverrides,
|
||||
fallback_cwd: Option<PathBuf>,
|
||||
defer_windows_network_validation: bool,
|
||||
) -> std::io::Result<Config> {
|
||||
let mut request_overrides = request_overrides.unwrap_or_default();
|
||||
if let Some(value) = request_overrides.remove("bypass_hook_trust") {
|
||||
@@ -240,7 +269,7 @@ impl ConfigManager {
|
||||
)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut config = codex_core::config::ConfigBuilder::default()
|
||||
let mut builder = codex_core::config::ConfigBuilder::default()
|
||||
.codex_home(self.codex_home.clone())
|
||||
.cli_overrides(merged_cli_overrides)
|
||||
.loader_overrides(self.loader_overrides.clone())
|
||||
@@ -248,9 +277,11 @@ impl ConfigManager {
|
||||
.harness_overrides(typesafe_overrides)
|
||||
.fallback_cwd(fallback_cwd)
|
||||
.cloud_config_bundle(self.current_cloud_config_bundle())
|
||||
.thread_config_loader(self.current_thread_config_loader())
|
||||
.build()
|
||||
.await?;
|
||||
.thread_config_loader(self.current_thread_config_loader());
|
||||
if defer_windows_network_validation {
|
||||
builder = builder.defer_windows_network_validation_for_bootstrap();
|
||||
}
|
||||
let mut config = builder.build().await?;
|
||||
self.apply_runtime_feature_enablement(&mut config);
|
||||
self.apply_arg0_paths(&mut config);
|
||||
Ok(config)
|
||||
|
||||
@@ -305,26 +305,6 @@ fn is_unrecoverable_windows_network_config_error(err: &std::io::Error) -> bool {
|
||||
.is_some_and(ConstraintError::is_windows_network_configuration_error)
|
||||
}
|
||||
|
||||
async fn recover_config_for_cloud_config_bootstrap(
|
||||
config_manager: &ConfigManager,
|
||||
err: std::io::Error,
|
||||
) -> IoResult<Option<Config>> {
|
||||
warn!(error = %err, "Failed to preload config for cloud config bundle");
|
||||
if is_unrecoverable_windows_network_config_error(&err) {
|
||||
// The elevated-only requirement may itself come from the cloud bundle.
|
||||
// Use defaults only to bootstrap auth; the authoritative load below
|
||||
// still validates the effective config after installing the loader.
|
||||
Config::load_default_with_cli_overrides_for_codex_home(
|
||||
config_manager.codex_home().to_path_buf(),
|
||||
Vec::new(),
|
||||
)
|
||||
.await
|
||||
.map(Some)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn exec_policy_warning_location(err: &ExecPolicyError) -> (Option<String>, Option<AppTextRange>) {
|
||||
match err {
|
||||
ExecPolicyError::ParsePolicy { path, source } => {
|
||||
@@ -521,20 +501,26 @@ pub async fn run_main_with_transport_options(
|
||||
arg0_paths.clone(),
|
||||
Arc::new(NoopThreadConfigLoader),
|
||||
);
|
||||
let bootstrap_config = match config_manager
|
||||
.load_latest_config(/*fallback_cwd*/ None)
|
||||
match config_manager
|
||||
.load_config_for_cloud_config_bootstrap()
|
||||
.await
|
||||
{
|
||||
Ok(config) => Some(config),
|
||||
Err(err) => recover_config_for_cloud_config_bootstrap(&config_manager, err).await?,
|
||||
Ok(config) => {
|
||||
let discovered_thread_config_loader = configured_thread_config_loader(&config);
|
||||
config_manager
|
||||
.replace_thread_config_loader(Arc::clone(&discovered_thread_config_loader));
|
||||
let auth_manager =
|
||||
AuthManager::shared_from_config(&config, /*enable_codex_api_key_env*/ false).await;
|
||||
config_manager
|
||||
.replace_cloud_config_bundle_loader(auth_manager, config.chatgpt_base_url);
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(error = %err, "Failed to preload config for cloud config bundle");
|
||||
// TODO: Decide whether bootstrap config preload failures should block startup.
|
||||
// If this fails, we cannot install cloud/thread config loaders, so non-strict
|
||||
// startup may continue without managed cloud config.
|
||||
}
|
||||
};
|
||||
if let Some(config) = bootstrap_config {
|
||||
let discovered_thread_config_loader = configured_thread_config_loader(&config);
|
||||
config_manager.replace_thread_config_loader(Arc::clone(&discovered_thread_config_loader));
|
||||
let auth_manager =
|
||||
AuthManager::shared_from_config(&config, /*enable_codex_api_key_env*/ false).await;
|
||||
config_manager.replace_cloud_config_bundle_loader(auth_manager, config.chatgpt_base_url);
|
||||
}
|
||||
let mut config_warnings = Vec::new();
|
||||
let (mut config, should_run_personality_migration) = match config_manager
|
||||
.load_latest_config(/*fallback_cwd*/ None)
|
||||
@@ -1389,8 +1375,6 @@ mod tests {
|
||||
#[cfg(debug_assertions)]
|
||||
use super::loader_overrides_with_test_user_config_file;
|
||||
#[cfg(target_os = "windows")]
|
||||
use super::recover_config_for_cloud_config_bootstrap;
|
||||
#[cfg(target_os = "windows")]
|
||||
use crate::config_manager::ConfigManager;
|
||||
#[cfg(debug_assertions)]
|
||||
use codex_config::LoaderOverrides;
|
||||
@@ -1400,20 +1384,47 @@ mod tests {
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
#[tokio::test]
|
||||
async fn cloud_config_bootstrap_tolerates_missing_network_requirements() {
|
||||
async fn cloud_config_bootstrap_defers_network_validation_and_preserves_cli_overrides() {
|
||||
let codex_home = tempfile::tempdir().expect("create Codex home");
|
||||
let config_manager =
|
||||
ConfigManager::without_managed_config_for_tests(codex_home.path().to_path_buf());
|
||||
let err = std::io::Error::from(
|
||||
codex_config::ConstraintError::NetworkProxyRequiresElevatedWindowsSandboxRequirement,
|
||||
std::fs::write(
|
||||
codex_home.path().join(codex_config::CONFIG_TOML_FILE),
|
||||
r#"
|
||||
sandbox_mode = "workspace-write"
|
||||
|
||||
[sandbox_workspace_write]
|
||||
network_access = true
|
||||
|
||||
[features]
|
||||
network_proxy = true
|
||||
|
||||
[windows]
|
||||
sandbox = "elevated"
|
||||
"#,
|
||||
)
|
||||
.expect("write config");
|
||||
let chatgpt_base_url = "https://cloud-config.example.test".to_string();
|
||||
let config_manager = ConfigManager::new_for_tests(
|
||||
codex_home.path().to_path_buf(),
|
||||
vec![(
|
||||
"chatgpt_base_url".to_string(),
|
||||
toml::Value::String(chatgpt_base_url.clone()),
|
||||
)],
|
||||
codex_config::LoaderOverrides::without_managed_config_for_tests(),
|
||||
codex_config::CloudConfigBundleLoader::default(),
|
||||
);
|
||||
|
||||
let config = recover_config_for_cloud_config_bootstrap(&config_manager, err)
|
||||
let config = config_manager
|
||||
.load_config_for_cloud_config_bootstrap()
|
||||
.await
|
||||
.expect("load bootstrap config")
|
||||
.expect("network configuration error should use bootstrap defaults");
|
||||
.expect("bootstrap config should defer Windows network validation");
|
||||
assert_eq!(config.chatgpt_base_url, chatgpt_base_url);
|
||||
assert!(config.permissions.network.is_some());
|
||||
|
||||
assert!(config.permissions.network.is_none());
|
||||
let err = config_manager
|
||||
.load_latest_config(/*fallback_cwd*/ None)
|
||||
.await
|
||||
.expect_err("authoritative config should enforce Windows network validation");
|
||||
assert!(is_unrecoverable_windows_network_config_error(&err));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1287,6 +1287,7 @@ pub struct ConfigBuilder {
|
||||
cloud_config_bundle: CloudConfigBundleLoader,
|
||||
thread_config_loader: Option<Arc<dyn ThreadConfigLoader>>,
|
||||
fallback_cwd: Option<PathBuf>,
|
||||
defer_windows_network_validation: bool,
|
||||
}
|
||||
|
||||
impl ConfigBuilder {
|
||||
@@ -1333,6 +1334,13 @@ impl ConfigBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
/// Defers Windows network compatibility checks until an authoritative
|
||||
/// config load that includes cloud requirements.
|
||||
pub fn defer_windows_network_validation_for_bootstrap(mut self) -> Self {
|
||||
self.defer_windows_network_validation = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn build(self) -> std::io::Result<Config> {
|
||||
// Keep the large config-loading future off small runtime thread stacks.
|
||||
Box::pin(self.build_inner()).await
|
||||
@@ -1348,6 +1356,7 @@ impl ConfigBuilder {
|
||||
cloud_config_bundle,
|
||||
thread_config_loader,
|
||||
fallback_cwd,
|
||||
defer_windows_network_validation,
|
||||
} = self;
|
||||
let codex_home = match codex_home {
|
||||
Some(codex_home) => AbsolutePathBuf::from_absolute_path(codex_home)?,
|
||||
@@ -1423,12 +1432,13 @@ impl ConfigBuilder {
|
||||
config_layer_stack.requirements().clone(),
|
||||
config_layer_stack.requirements_toml().clone(),
|
||||
)?;
|
||||
let mut config = Config::load_config_with_layer_stack(
|
||||
let mut config = Config::load_config_with_layer_stack_and_options(
|
||||
LOCAL_FS.as_ref(),
|
||||
lock_config_toml,
|
||||
harness_overrides,
|
||||
codex_home,
|
||||
lock_config_layer_stack,
|
||||
defer_windows_network_validation,
|
||||
)
|
||||
.await?;
|
||||
config.config_lock_toml = Some(Arc::new(expected_lock_config));
|
||||
@@ -1437,12 +1447,13 @@ impl ConfigBuilder {
|
||||
save_fields_resolved_from_model_catalog;
|
||||
return Ok(config);
|
||||
}
|
||||
Config::load_config_with_layer_stack(
|
||||
Config::load_config_with_layer_stack_and_options(
|
||||
LOCAL_FS.as_ref(),
|
||||
config_toml,
|
||||
harness_overrides,
|
||||
codex_home,
|
||||
config_layer_stack,
|
||||
defer_windows_network_validation,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -2991,6 +3002,25 @@ impl Config {
|
||||
overrides: ConfigOverrides,
|
||||
codex_home: AbsolutePathBuf,
|
||||
config_layer_stack: ConfigLayerStack,
|
||||
) -> std::io::Result<Self> {
|
||||
Self::load_config_with_layer_stack_and_options(
|
||||
fs,
|
||||
cfg,
|
||||
overrides,
|
||||
codex_home,
|
||||
config_layer_stack,
|
||||
/*defer_windows_network_validation*/ false,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn load_config_with_layer_stack_and_options(
|
||||
fs: &dyn ExecutorFileSystem,
|
||||
cfg: ConfigToml,
|
||||
overrides: ConfigOverrides,
|
||||
codex_home: AbsolutePathBuf,
|
||||
config_layer_stack: ConfigLayerStack,
|
||||
defer_windows_network_validation: bool,
|
||||
) -> std::io::Result<Self> {
|
||||
// Keep the large config-construction future off small test thread stacks.
|
||||
Box::pin(async move {
|
||||
@@ -3788,15 +3818,17 @@ impl Config {
|
||||
network_requirements,
|
||||
&network_permission_profile,
|
||||
)?;
|
||||
let network_proxy_enabled = network.as_ref().is_some_and(NetworkProxySpec::enabled);
|
||||
validate_windows_network_proxy_requirements(
|
||||
config_layer_stack.requirements_toml(),
|
||||
network_proxy_enabled,
|
||||
)?;
|
||||
validate_windows_sandbox_network_proxy_compatibility(
|
||||
windows_sandbox_level,
|
||||
network_proxy_enabled,
|
||||
)?;
|
||||
if !defer_windows_network_validation {
|
||||
let network_proxy_enabled = network.as_ref().is_some_and(NetworkProxySpec::enabled);
|
||||
validate_windows_network_proxy_requirements(
|
||||
config_layer_stack.requirements_toml(),
|
||||
network_proxy_enabled,
|
||||
)?;
|
||||
validate_windows_sandbox_network_proxy_compatibility(
|
||||
windows_sandbox_level,
|
||||
network_proxy_enabled,
|
||||
)?;
|
||||
}
|
||||
let mut helper_readable_roots = get_readable_roots_required_for_codex_runtime(
|
||||
&codex_home,
|
||||
zsh_path.as_ref(),
|
||||
|
||||
Reference in New Issue
Block a user