mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
Enforce exact managed config values
This commit is contained in:
@@ -8330,6 +8330,12 @@ async fn test_requirements_web_search_mode_allowlist_does_not_warn_when_unset()
|
||||
let fixture = create_test_fixture()?;
|
||||
|
||||
let requirements_toml = codex_config::ConfigRequirementsToml {
|
||||
sqlite_home: None,
|
||||
log_dir: None,
|
||||
model_catalog_json: None,
|
||||
check_for_update_on_startup: None,
|
||||
allow_login_shell: None,
|
||||
feedback: None,
|
||||
allowed_approval_policies: None,
|
||||
allowed_approvals_reviewers: None,
|
||||
allowed_sandbox_modes: None,
|
||||
@@ -10848,3 +10854,77 @@ fn test_tui_notification_condition_rejects_unknown_value() {
|
||||
"unexpected error: {err}"
|
||||
);
|
||||
}
|
||||
|
||||
async fn load_with_enterprise_requirement(
|
||||
codex_home: &TempDir,
|
||||
requirements: impl Into<String>,
|
||||
) -> std::io::Result<Config> {
|
||||
ConfigBuilder::without_managed_config_for_tests()
|
||||
.codex_home(codex_home.path().to_path_buf())
|
||||
.fallback_cwd(Some(codex_home.path().to_path_buf()))
|
||||
.cloud_config_bundle(
|
||||
CloudConfigBundleFixture::loader_with_enterprise_requirement(requirements),
|
||||
)
|
||||
.build()
|
||||
.await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn exact_requirements_override_only_managed_leaves() -> std::io::Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
let catalog_path = codex_home.path().join("required-models.json");
|
||||
let mut catalog = bundled_models_response()
|
||||
.unwrap_or_else(|err| panic!("bundled models.json should parse: {err}"));
|
||||
catalog.models = catalog.models.into_iter().take(1).collect();
|
||||
std::fs::write(
|
||||
&catalog_path,
|
||||
serde_json::to_string(&catalog).expect("serialize catalog"),
|
||||
)?;
|
||||
std::fs::write(
|
||||
codex_home.path().join(CONFIG_TOML_FILE),
|
||||
r#"
|
||||
check_for_update_on_startup = true
|
||||
allow_login_shell = true
|
||||
|
||||
[feedback]
|
||||
enabled = true
|
||||
|
||||
[windows]
|
||||
sandbox_private_desktop = true
|
||||
"#,
|
||||
)?;
|
||||
|
||||
let required_sqlite_home = codex_home.path().join("required-state");
|
||||
let required_log_dir = codex_home.path().join("required-logs");
|
||||
let requirements = format!(
|
||||
r#"
|
||||
sqlite_home = {:?}
|
||||
log_dir = {:?}
|
||||
model_catalog_json = {:?}
|
||||
check_for_update_on_startup = false
|
||||
allow_login_shell = false
|
||||
|
||||
[feedback]
|
||||
enabled = false
|
||||
|
||||
[windows]
|
||||
sandbox_private_desktop = false
|
||||
"#,
|
||||
required_sqlite_home.display(),
|
||||
required_log_dir.display(),
|
||||
catalog_path.display(),
|
||||
);
|
||||
let config = load_with_enterprise_requirement(&codex_home, requirements).await?;
|
||||
|
||||
assert_eq!(config.sqlite_home, required_sqlite_home);
|
||||
assert_eq!(config.log_dir, required_log_dir);
|
||||
assert_eq!(config.model_catalog, Some(catalog));
|
||||
assert!(!config.check_for_update_on_startup);
|
||||
assert!(!config.permissions.allow_login_shell);
|
||||
assert!(!config.feedback_enabled);
|
||||
assert!(!config.permissions.windows_sandbox_private_desktop);
|
||||
assert!(config.startup_warnings.iter().any(|warning| {
|
||||
warning.contains("Configured value for `check_for_update_on_startup` is overridden")
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -145,6 +145,7 @@ mod managed_features;
|
||||
mod network_proxy_spec;
|
||||
mod otel;
|
||||
mod permissions;
|
||||
mod requirements;
|
||||
mod resolved_permission_profile;
|
||||
#[cfg(test)]
|
||||
mod schema;
|
||||
@@ -2642,7 +2643,7 @@ impl Config {
|
||||
|
||||
pub(crate) async fn load_config_with_layer_stack(
|
||||
fs: &dyn ExecutorFileSystem,
|
||||
cfg: ConfigToml,
|
||||
mut cfg: ConfigToml,
|
||||
overrides: ConfigOverrides,
|
||||
codex_home: AbsolutePathBuf,
|
||||
config_layer_stack: ConfigLayerStack,
|
||||
@@ -2658,13 +2659,30 @@ impl Config {
|
||||
|
||||
validate_model_providers(&cfg.model_providers)
|
||||
.map_err(|message| std::io::Error::new(std::io::ErrorKind::InvalidInput, message))?;
|
||||
// Ensure that every field of ConfigRequirements is applied to the final
|
||||
// Config.
|
||||
let mut startup_warnings = config_layer_stack
|
||||
.startup_warnings()
|
||||
.unwrap_or_default()
|
||||
.to_vec();
|
||||
requirements::apply_to_config(
|
||||
&mut cfg,
|
||||
config_layer_stack.requirements(),
|
||||
&mut startup_warnings,
|
||||
)?;
|
||||
|
||||
// Destructure every field to ensure ConfigRequirements additions are
|
||||
// either applied above or handled while constructing the final Config.
|
||||
let ConfigRequirements {
|
||||
sqlite_home: _,
|
||||
log_dir: _,
|
||||
model_catalog_json: _,
|
||||
check_for_update_on_startup: _,
|
||||
allow_login_shell: _,
|
||||
feedback: _,
|
||||
approval_policy: mut constrained_approval_policy,
|
||||
approvals_reviewer: mut constrained_approvals_reviewer,
|
||||
permission_profile: mut constrained_permission_profile,
|
||||
windows_sandbox_mode: mut constrained_windows_sandbox_mode,
|
||||
windows_sandbox_private_desktop: _,
|
||||
web_search_mode: mut constrained_web_search_mode,
|
||||
allow_managed_hooks_only: _,
|
||||
allow_appshots: _,
|
||||
@@ -2681,11 +2699,6 @@ impl Config {
|
||||
guardian_policy_config_source: _,
|
||||
} = config_layer_stack.requirements().clone();
|
||||
|
||||
let mut startup_warnings = config_layer_stack
|
||||
.startup_warnings()
|
||||
.unwrap_or_default()
|
||||
.to_vec();
|
||||
|
||||
// Destructure ConfigOverrides fully to ensure all overrides are applied.
|
||||
let ConfigOverrides {
|
||||
model,
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
use codex_config::ConfigRequirements;
|
||||
use codex_config::RequirementSource;
|
||||
use codex_config::Sourced;
|
||||
use codex_config::config_toml::ConfigToml;
|
||||
use codex_config::types::FeedbackConfigToml;
|
||||
|
||||
/// Applies managed requirements to regular config before final config construction.
|
||||
///
|
||||
/// Managed values replace their configured counterparts, and conflicts produce
|
||||
/// source-aware startup warnings.
|
||||
pub(super) fn apply_to_config(
|
||||
config: &mut ConfigToml,
|
||||
requirements: &ConfigRequirements,
|
||||
startup_warnings: &mut Vec<String>,
|
||||
) -> std::io::Result<()> {
|
||||
macro_rules! apply_exact {
|
||||
($field:ident) => {
|
||||
apply_exact_requirement(
|
||||
stringify!($field),
|
||||
&mut config.$field,
|
||||
requirements.$field.as_ref(),
|
||||
startup_warnings,
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
apply_exact!(sqlite_home);
|
||||
apply_exact!(log_dir);
|
||||
apply_exact!(model_catalog_json);
|
||||
apply_exact!(check_for_update_on_startup);
|
||||
apply_exact!(allow_login_shell);
|
||||
apply_feedback_requirement(
|
||||
&mut config.feedback,
|
||||
requirements.feedback.as_ref(),
|
||||
startup_warnings,
|
||||
);
|
||||
if let Some(requirement) = requirements.windows_sandbox_private_desktop.as_ref() {
|
||||
apply_exact_requirement(
|
||||
"windows.sandbox_private_desktop",
|
||||
&mut config
|
||||
.windows
|
||||
.get_or_insert_default()
|
||||
.sandbox_private_desktop,
|
||||
Some(requirement),
|
||||
startup_warnings,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_exact_requirement<T>(
|
||||
field_name: &'static str,
|
||||
configured_value: &mut Option<T>,
|
||||
requirement: Option<&Sourced<T>>,
|
||||
startup_warnings: &mut Vec<String>,
|
||||
) where
|
||||
T: Clone + PartialEq + std::fmt::Debug,
|
||||
{
|
||||
let Some(Sourced { value, source }) = requirement else {
|
||||
return;
|
||||
};
|
||||
if configured_value
|
||||
.as_ref()
|
||||
.is_some_and(|configured| configured != value)
|
||||
{
|
||||
tracing::warn!(
|
||||
?source,
|
||||
?value,
|
||||
"configured value is overridden by an exact requirement for {field_name}"
|
||||
);
|
||||
startup_warnings.push(format!(
|
||||
"Configured value for `{field_name}` is overridden by the required value {value:?} from {source}."
|
||||
));
|
||||
}
|
||||
*configured_value = Some(value.clone());
|
||||
}
|
||||
|
||||
pub(super) fn replace_required_leaf<T: Clone + PartialEq>(
|
||||
configured: &mut Option<T>,
|
||||
required: &Option<T>,
|
||||
) -> bool {
|
||||
let Some(required) = required else {
|
||||
return false;
|
||||
};
|
||||
let conflict = configured
|
||||
.as_ref()
|
||||
.is_some_and(|configured| configured != required);
|
||||
*configured = Some(required.clone());
|
||||
conflict
|
||||
}
|
||||
|
||||
fn apply_feedback_requirement(
|
||||
configured: &mut Option<FeedbackConfigToml>,
|
||||
requirement: Option<&Sourced<FeedbackConfigToml>>,
|
||||
startup_warnings: &mut Vec<String>,
|
||||
) {
|
||||
let Some(Sourced { value, source }) = requirement else {
|
||||
return;
|
||||
};
|
||||
let FeedbackConfigToml { enabled } = value;
|
||||
let configured = configured.get_or_insert_default();
|
||||
let conflict = replace_required_leaf(&mut configured.enabled, enabled);
|
||||
push_structured_requirement_override_warning("feedback", conflict, source, startup_warnings);
|
||||
}
|
||||
|
||||
/// Emits one source-aware warning when a structured requirement replaces one
|
||||
/// or more configured values.
|
||||
pub(super) fn push_structured_requirement_override_warning(
|
||||
field_name: &str,
|
||||
conflict: bool,
|
||||
source: &RequirementSource,
|
||||
startup_warnings: &mut Vec<String>,
|
||||
) {
|
||||
if !conflict {
|
||||
return;
|
||||
}
|
||||
tracing::warn!(
|
||||
?source,
|
||||
"configured values are overridden by requirements for {field_name}"
|
||||
);
|
||||
startup_warnings.push(format!(
|
||||
"Configured values under `{field_name}` are overridden by requirements from {source}."
|
||||
));
|
||||
}
|
||||
Reference in New Issue
Block a user