diff --git a/codex-rs/config/src/merge.rs b/codex-rs/config/src/merge.rs index f2cb510345..71cbc310a2 100644 --- a/codex-rs/config/src/merge.rs +++ b/codex-rs/config/src/merge.rs @@ -21,6 +21,10 @@ fn merge_toml_values_at_path(base: &mut TomlValue, overlay: &TomlValue, path: &m normalize_network_domain_keys(base_table); normalize_network_domain_keys(&mut overlay_table); } + if is_shell_environment_filters_path(path) { + normalize_case_insensitive_keys(base_table); + normalize_case_insensitive_keys(&mut overlay_table); + } for (key, value) in overlay_table { path.push(key.clone()); @@ -51,14 +55,21 @@ fn prepare_shell_environment_policy_merge( return; }; - // Ordinary config keeps accepting legacy arrays while `rules` is the + // Ordinary config keeps accepting legacy arrays while `filters` is the // canonical keyed form. Reconcile the two shapes only at this boundary so // layer precedence remains correct and array compatibility can be removed // cleanly after the migration. - for (legacy_field, opposite_field, action) in [ - ("exclude", "include_only", "exclude"), - ("include_only", "exclude", "include"), - ] { + let overlay_has_filters = overlay.contains_key("filters"); + let overlay_has_legacy = + overlay.contains_key("exclude") || overlay.contains_key("include_only"); + if overlay_has_filters && !overlay_has_legacy { + convert_legacy_to_filters(base); + } else if overlay_has_legacy && !overlay_has_filters { + convert_filters_to_legacy(base); + } + + for (legacy_field, opposite_field) in [("exclude", "include_only"), ("include_only", "exclude")] + { let Some(patterns) = overlay .get(legacy_field) .and_then(TomlValue::as_array) @@ -71,21 +82,70 @@ fn prepare_shell_environment_policy_merge( else { continue; }; - - if let Some(base_rules) = base.get_mut("rules").and_then(TomlValue::as_table_mut) { - base_rules.retain(|pattern, value| { - value.as_str() != Some(action) && !patterns.contains(&pattern) - }); - } remove_patterns_from_legacy_array(base, opposite_field, &patterns); } +} - let Some(overlay_rules) = overlay.get("rules").and_then(TomlValue::as_table) else { +fn convert_legacy_to_filters(table: &mut toml::map::Map) { + let mut filters = match table.get("filters") { + Some(TomlValue::Table(filters)) => filters.clone(), + Some(_) => return, + None => toml::map::Map::new(), + }; + table.remove("filters"); + normalize_case_insensitive_keys(&mut filters); + for (field, action) in [("exclude", "exclude"), ("include_only", "include")] { + let Some(TomlValue::Array(patterns)) = table.get(field).cloned() else { + continue; + }; + table.remove(field); + for pattern in patterns { + if let TomlValue::String(pattern) = pattern { + filters + .entry(pattern.to_ascii_lowercase()) + .or_insert_with(|| TomlValue::String(action.to_string())); + } + } + } + if !filters.is_empty() { + table.insert("filters".to_string(), TomlValue::Table(filters)); + } +} + +fn convert_filters_to_legacy(table: &mut toml::map::Map) { + let Some(TomlValue::Table(mut filters)) = table.get("filters").cloned() else { return; }; - let patterns = overlay_rules.keys().map(String::as_str).collect::>(); - remove_patterns_from_legacy_array(base, "exclude", &patterns); - remove_patterns_from_legacy_array(base, "include_only", &patterns); + table.remove("filters"); + normalize_case_insensitive_keys(&mut filters); + for (pattern, action) in filters { + match action.as_str() { + Some("exclude") => push_legacy_pattern(table, "exclude", "include_only", pattern), + Some("include") => push_legacy_pattern(table, "include_only", "exclude", pattern), + _ => {} + } + } +} + +fn push_legacy_pattern( + table: &mut toml::map::Map, + field: &str, + opposite_field: &str, + pattern: String, +) { + remove_patterns_from_legacy_array(table, opposite_field, &[pattern.as_str()]); + let items = table + .entry(field.to_string()) + .or_insert_with(|| TomlValue::Array(Vec::new())); + let Some(items) = items.as_array_mut() else { + return; + }; + if !items.iter().any(|item| { + item.as_str() + .is_some_and(|candidate| candidate.eq_ignore_ascii_case(&pattern)) + }) { + items.push(TomlValue::String(pattern)); + } } fn remove_patterns_from_legacy_array( @@ -100,11 +160,22 @@ fn remove_patterns_from_legacy_array( return; } items.retain(|item| { - item.as_str() - .is_none_or(|pattern| !patterns.contains(&pattern)) + item.as_str().is_none_or(|candidate| { + !patterns + .iter() + .any(|pattern| candidate.eq_ignore_ascii_case(pattern)) + }) }); } +fn is_shell_environment_filters_path(path: &[String]) -> bool { + matches!( + path, + [policy, filters] + if policy == "shell_environment_policy" && filters == "filters" + ) +} + fn is_permission_network_domains_path(path: &[String]) -> bool { matches!( path, @@ -120,6 +191,13 @@ fn normalize_network_domain_keys(table: &mut toml::map::Map) } } +fn normalize_case_insensitive_keys(table: &mut toml::map::Map) { + let entries = std::mem::take(table); + for (key, value) in entries { + table.insert(key.to_ascii_lowercase(), value); + } +} + #[cfg(test)] #[path = "merge_tests.rs"] mod tests; diff --git a/codex-rs/config/src/merge_tests.rs b/codex-rs/config/src/merge_tests.rs index 271f6c75dc..81636b1533 100644 --- a/codex-rs/config/src/merge_tests.rs +++ b/codex-rs/config/src/merge_tests.rs @@ -147,19 +147,19 @@ exclude = ["HIGH_*"] } #[test] -fn shell_environment_policy_rules_overlay_merges_by_key() { +fn shell_environment_policy_filters_overlay_merges_by_key_case_insensitively() { let mut base = parse_toml( r#" -[shell_environment_policy.rules] +[shell_environment_policy.filters] "FLIP_*" = "exclude" "KEEP_*" = "include" "#, ); let overlay = parse_toml( r#" -[shell_environment_policy.rules] +[shell_environment_policy.filters] "ADD_*" = "exclude" -"FLIP_*" = "include" +"flip_*" = "include" "#, ); @@ -169,17 +169,17 @@ fn shell_environment_policy_rules_overlay_merges_by_key() { base, parse_toml( r#" -[shell_environment_policy.rules] -"ADD_*" = "exclude" -"FLIP_*" = "include" -"KEEP_*" = "include" +[shell_environment_policy.filters] +"add_*" = "exclude" +"flip_*" = "include" +"keep_*" = "include" "#, ) ); } #[test] -fn shell_environment_policy_rules_override_lower_legacy_arrays_by_pattern() { +fn shell_environment_policy_filters_override_lower_legacy_arrays_by_pattern() { let mut base = parse_toml( r#" [shell_environment_policy] @@ -189,7 +189,7 @@ include_only = ["FLIP_TO_EXCLUDE", "KEEP_INCLUDED"] ); let overlay = parse_toml( r#" -[shell_environment_policy.rules] +[shell_environment_policy.filters] "ADD_INCLUDED" = "include" "FLIP_TO_EXCLUDE" = "exclude" "FLIP_TO_INCLUDE" = "include" @@ -202,14 +202,12 @@ include_only = ["FLIP_TO_EXCLUDE", "KEEP_INCLUDED"] base, parse_toml( r#" -[shell_environment_policy] -exclude = ["KEEP_EXCLUDED"] -include_only = ["KEEP_INCLUDED"] - -[shell_environment_policy.rules] -"ADD_INCLUDED" = "include" -"FLIP_TO_EXCLUDE" = "exclude" -"FLIP_TO_INCLUDE" = "include" +[shell_environment_policy.filters] +"add_included" = "include" +"flip_to_exclude" = "exclude" +"flip_to_include" = "include" +"keep_excluded" = "exclude" +"keep_included" = "include" "#, ) ); @@ -219,13 +217,13 @@ include_only = ["KEEP_INCLUDED"] codex_protocol::config_types::ShellEnvironmentPolicy::from(config.shell_environment_policy), codex_protocol::config_types::ShellEnvironmentPolicy::from(ShellEnvironmentPolicyToml { exclude: Some(vec![ - "KEEP_EXCLUDED".to_string(), - "FLIP_TO_EXCLUDE".to_string(), + "flip_to_exclude".to_string(), + "keep_excluded".to_string(), ]), include_only: Some(vec![ - "KEEP_INCLUDED".to_string(), - "ADD_INCLUDED".to_string(), - "FLIP_TO_INCLUDE".to_string(), + "add_included".to_string(), + "flip_to_include".to_string(), + "keep_included".to_string(), ]), ..Default::default() }) @@ -233,10 +231,10 @@ include_only = ["KEEP_INCLUDED"] } #[test] -fn shell_environment_policy_legacy_array_replaces_lower_rules_for_its_action() { +fn shell_environment_policy_legacy_array_replaces_lower_filters_for_its_action() { let mut base = parse_toml( r#" -[shell_environment_policy.rules] +[shell_environment_policy.filters] "FLIP_TO_EXCLUDE" = "include" "LOW_EXCLUDED" = "exclude" "KEEP_INCLUDED" = "include" @@ -257,9 +255,7 @@ exclude = ["FLIP_TO_EXCLUDE", "HIGH_EXCLUDED"] r#" [shell_environment_policy] exclude = ["FLIP_TO_EXCLUDE", "HIGH_EXCLUDED"] - -[shell_environment_policy.rules] -"KEEP_INCLUDED" = "include" +include_only = ["keep_included"] "#, ) ); diff --git a/codex-rs/config/src/types.rs b/codex-rs/config/src/types.rs index 319bb4e450..dc68290e27 100644 --- a/codex-rs/config/src/types.rs +++ b/codex-rs/config/src/types.rs @@ -18,8 +18,8 @@ pub use codex_protocol::config_types::ModeKind; pub use codex_protocol::config_types::Personality; pub use codex_protocol::config_types::ServiceTier; use codex_protocol::config_types::ShellEnvironmentPolicy; +use codex_protocol::config_types::ShellEnvironmentPolicyFilter; use codex_protocol::config_types::ShellEnvironmentPolicyInherit; -use codex_protocol::config_types::ShellEnvironmentPolicyRule; pub use codex_protocol::config_types::WebSearchMode; use codex_utils_absolute_path::AbsolutePathBuf; use std::collections::BTreeMap; @@ -929,7 +929,7 @@ impl From for codex_app_server_protocol::SandboxSettings } /// Policy for building the `env` when spawning a process via shell-like tools. -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] +#[derive(Serialize, Debug, Clone, PartialEq, Default, JsonSchema)] #[schemars(deny_unknown_fields)] pub struct ShellEnvironmentPolicyToml { pub inherit: Option, @@ -949,11 +949,46 @@ pub struct ShellEnvironmentPolicyToml { /// Ordinary config keeps accepting the legacy arrays above during the /// migration. Requirements accept only this keyed form, keeping array /// compatibility isolated so the legacy fields can be deprecated later. - pub rules: Option>, + pub filters: Option>, pub experimental_use_profile: Option, } +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct ShellEnvironmentPolicyTomlRaw { + inherit: Option, + ignore_default_excludes: Option, + exclude: Option>, + r#set: Option>, + include_only: Option>, + filters: Option>, + experimental_use_profile: Option, +} + +impl<'de> Deserialize<'de> for ShellEnvironmentPolicyToml { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let raw = ShellEnvironmentPolicyTomlRaw::deserialize(deserializer)?; + if raw.filters.is_some() && (raw.exclude.is_some() || raw.include_only.is_some()) { + return Err(serde::de::Error::custom( + "cannot mix `filters` with legacy `exclude` or `include_only`", + )); + } + Ok(Self { + inherit: raw.inherit, + ignore_default_excludes: raw.ignore_default_excludes, + exclude: raw.exclude, + r#set: raw.r#set, + include_only: raw.include_only, + filters: raw.filters, + experimental_use_profile: raw.experimental_use_profile, + }) + } +} + impl From for ShellEnvironmentPolicy { fn from(toml: ShellEnvironmentPolicyToml) -> Self { // Default to inheriting the full environment when not specified. @@ -961,12 +996,12 @@ impl From for ShellEnvironmentPolicy { let ignore_default_excludes = toml.ignore_default_excludes.unwrap_or(true); let mut exclude = toml.exclude.unwrap_or_default(); let mut include_only = toml.include_only.unwrap_or_default(); - for (pattern, rule) in toml.rules.unwrap_or_default() { + for (pattern, filter) in toml.filters.unwrap_or_default() { exclude.retain(|candidate| candidate != &pattern); include_only.retain(|candidate| candidate != &pattern); - match rule { - ShellEnvironmentPolicyRule::Include => include_only.push(pattern), - ShellEnvironmentPolicyRule::Exclude => exclude.push(pattern), + match filter { + ShellEnvironmentPolicyFilter::Include => include_only.push(pattern), + ShellEnvironmentPolicyFilter::Exclude => exclude.push(pattern), } } let exclude = exclude diff --git a/codex-rs/config/src/types_tests.rs b/codex-rs/config/src/types_tests.rs index ac1b1bcb5a..2bffcdc442 100644 --- a/codex-rs/config/src/types_tests.rs +++ b/codex-rs/config/src/types_tests.rs @@ -2,7 +2,7 @@ use super::*; use pretty_assertions::assert_eq; #[test] -fn shell_environment_policy_accepts_legacy_lists_and_rules() { +fn shell_environment_policy_accepts_legacy_lists_or_filters() { let legacy: ShellEnvironmentPolicyToml = toml::from_str( r#" exclude = ["LEGACY_*", "SHARED_*"] @@ -19,57 +19,59 @@ include_only = ["PATH", "HOME"] } ); - let ruled: ShellEnvironmentPolicyToml = toml::from_str( + let filtered: ShellEnvironmentPolicyToml = toml::from_str( r#" -exclude = ["KEEP_EXCLUDED", "FLIP_TO_INCLUDE"] -include_only = ["KEEP_INCLUDED", "FLIP_TO_EXCLUDE"] - -[rules] +[filters] "FLIP_TO_EXCLUDE" = "exclude" "FLIP_TO_INCLUDE" = "include" "#, ) - .expect("rules should be valid in config.toml"); + .expect("filters should be valid in config.toml"); assert_eq!( - ruled, + filtered, ShellEnvironmentPolicyToml { - exclude: Some(vec![ - "KEEP_EXCLUDED".to_string(), - "FLIP_TO_INCLUDE".to_string(), - ]), - include_only: Some(vec![ - "KEEP_INCLUDED".to_string(), - "FLIP_TO_EXCLUDE".to_string(), - ]), - rules: Some(BTreeMap::from([ + filters: Some(BTreeMap::from([ ( "FLIP_TO_EXCLUDE".to_string(), - ShellEnvironmentPolicyRule::Exclude, + ShellEnvironmentPolicyFilter::Exclude, ), ( "FLIP_TO_INCLUDE".to_string(), - ShellEnvironmentPolicyRule::Include, + ShellEnvironmentPolicyFilter::Include, ), ])), ..Default::default() } ); assert_eq!( - ShellEnvironmentPolicy::from(ruled), + ShellEnvironmentPolicy::from(filtered), ShellEnvironmentPolicy::from(ShellEnvironmentPolicyToml { - exclude: Some(vec![ - "KEEP_EXCLUDED".to_string(), - "FLIP_TO_EXCLUDE".to_string(), - ]), - include_only: Some(vec![ - "KEEP_INCLUDED".to_string(), - "FLIP_TO_INCLUDE".to_string(), - ]), + exclude: Some(vec!["FLIP_TO_EXCLUDE".to_string()]), + include_only: Some(vec!["FLIP_TO_INCLUDE".to_string()]), ..Default::default() }) ); } +#[test] +fn shell_environment_policy_rejects_mixed_legacy_lists_and_filters() { + let error = toml::from_str::( + r#" +exclude = ["LEGACY_*"] + +[filters] +"CANONICAL_*" = "include" +"#, + ) + .expect_err("one config layer must not mix legacy lists and filters"); + + assert!( + error + .to_string() + .contains("cannot mix `filters` with legacy `exclude` or `include_only`") + ); +} + #[test] fn deserialize_skill_config_with_name_selector() { let cfg: SkillConfig = toml::from_str( diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 761f2949aa..22034964a0 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -2595,6 +2595,14 @@ ], "type": "string" }, + "ShellEnvironmentPolicyFilter": { + "description": "Assigns a shell environment variable pattern to the include-only or exclude set. Includes do not re-add variables removed by another exclude pattern.", + "enum": [ + "include", + "exclude" + ], + "type": "string" + }, "ShellEnvironmentPolicyInherit": { "oneOf": [ { @@ -2620,14 +2628,6 @@ } ] }, - "ShellEnvironmentPolicyRule": { - "description": "Assigns a shell environment variable pattern to the include-only or exclude set. Includes do not re-add variables removed by another exclude pattern.", - "enum": [ - "include", - "exclude" - ], - "type": "string" - }, "ShellEnvironmentPolicyToml": { "additionalProperties": false, "description": "Policy for building the `env` when spawning a process via shell-like tools.", @@ -2642,6 +2642,13 @@ "experimental_use_profile": { "type": "boolean" }, + "filters": { + "additionalProperties": { + "$ref": "#/definitions/ShellEnvironmentPolicyFilter" + }, + "description": "Pattern actions used by the canonical table representation.\n\nOrdinary config keeps accepting the legacy arrays above during the migration. Requirements accept only this keyed form, keeping array compatibility isolated so the legacy fields can be deprecated later.", + "type": "object" + }, "ignore_default_excludes": { "type": "boolean" }, @@ -2655,13 +2662,6 @@ "inherit": { "$ref": "#/definitions/ShellEnvironmentPolicyInherit" }, - "rules": { - "additionalProperties": { - "$ref": "#/definitions/ShellEnvironmentPolicyRule" - }, - "description": "Pattern actions used by the canonical table representation.\n\nOrdinary config keeps accepting the legacy arrays above during the migration. Requirements accept only this keyed form, keeping array compatibility isolated so the legacy fields can be deprecated later.", - "type": "object" - }, "set": { "additionalProperties": { "type": "string" @@ -5215,10 +5215,10 @@ "default": { "exclude": null, "experimental_use_profile": null, + "filters": null, "ignore_default_excludes": null, "include_only": null, "inherit": null, - "rules": null, "set": null } }, diff --git a/codex-rs/protocol/src/config_types.rs b/codex-rs/protocol/src/config_types.rs index fe1545ee09..4e54301d91 100644 --- a/codex-rs/protocol/src/config_types.rs +++ b/codex-rs/protocol/src/config_types.rs @@ -204,7 +204,7 @@ pub enum ShellEnvironmentPolicyInherit { #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)] #[serde(rename_all = "lowercase")] #[ts(export_to = "v2/")] -pub enum ShellEnvironmentPolicyRule { +pub enum ShellEnvironmentPolicyFilter { Include, Exclude, }