Add keyed shell environment policy filters (#34590)

## What changed

- Add `shell_environment_policy.filters`, mapping environment-variable patterns to `include` or `exclude`, while continuing to accept the legacy `exclude` and `include_only` arrays.
- Merge filter keys case-insensitively across config layers so higher-precedence entries can override individual patterns. Switching representations replaces the other representation instead of combining them.
- Reject mixed, duplicate, or malformed filter definitions in enabled layers, config writes, and reloads, while retaining the previous session configuration when a reload is invalid.
- Make config-manager reads and writes representation-aware and preserve existing formatting when updating individual policy values.

## Testing

Add coverage for parsing, schema constraints, layered merging, validation and diagnostics, config writes, override metadata, and invalid session reloads.

GitOrigin-RevId: e15464bca53e6e7ef31fcc97537fda60ed5d670a
This commit is contained in:
Abhinav authored and copyberry committed 2026-07-21 18:23:26 +00:00
1 parent 65f8bf6853
commit 7442f5f932
29 files changed
+1662 -115

No files matched your search

+29 -4
View File
@@ -1,5 +1,7 @@
use crate::CONFIG_TOML_FILE;
use crate::config_requirements::ConfigRequirements;
use crate::config_requirements::ConfigRequirementsToml;
use crate::format_config_layer_source;
use super::fingerprint::record_origins;
use super::fingerprint::version_for_toml;
@@ -10,6 +12,7 @@ use crate::ConfigLayer;
use crate::ConfigLayerMetadata;
use crate::ConfigLayerSource;
use crate::ProfileV2Name;
use crate::shell_environment_policy::validate_shell_environment_policy_filter_config;
use codex_utils_absolute_path::AbsolutePathBuf;
use serde_json::Value as JsonValue;
use std::collections::HashMap;
@@ -275,6 +278,7 @@ impl ConfigLayerStack {
requirements: ConfigRequirements,
requirements_toml: ConfigRequirementsToml,
) -> std::io::Result<Self> {
validate_enabled_config_layers(&layers)?;
let user_layer_index = verify_layer_ordering(&layers)?;
Ok(Self {
layers,
@@ -376,7 +380,11 @@ impl ConfigLayerStack {
/// based on precedence rules. When the stack has both base and profile-v2
/// user layers, this updates only the layer whose file matches
/// `config_toml`.
pub fn with_user_config(&self, config_toml: &AbsolutePathBuf, user_config: TomlValue) -> Self {
pub fn with_user_config(
&self,
config_toml: &AbsolutePathBuf,
user_config: TomlValue,
) -> std::io::Result<Self> {
let profile = self.layers.iter().find_map(|layer| match &layer.name {
ConfigLayerSource::User { file, profile } if file == config_toml => profile
.as_deref()
@@ -391,7 +399,7 @@ impl ConfigLayerStack {
config_toml: &AbsolutePathBuf,
profile: Option<&ProfileV2Name>,
user_config: TomlValue,
) -> Self {
) -> std::io::Result<Self> {
let user_layer = ConfigLayerEntry::new(
ConfigLayerSource::User {
file: config_toml.clone(),
@@ -399,6 +407,7 @@ impl ConfigLayerStack {
},
user_config,
);
validate_enabled_config_layers(std::slice::from_ref(&user_layer))?;
let mut layers = self.layers.clone();
if let Some(index) = layers.iter().position(|layer| {
@@ -423,7 +432,7 @@ impl ConfigLayerStack {
None
}
});
Self {
Ok(Self {
layers,
user_layer_index,
requirements: self.requirements.clone(),
@@ -431,7 +440,7 @@ impl ConfigLayerStack {
ignore_user_and_project_exec_policy_rules: self
.ignore_user_and_project_exec_policy_rules,
startup_warnings: self.startup_warnings.clone(),
}
})
}
/// Returns a new stack with the user layer copied from `other`, preserving
@@ -539,6 +548,22 @@ impl ConfigLayerStack {
}
}
/// Validates before merging so mixed forms and malformed filter entries cannot be normalized away.
pub(crate) fn validate_enabled_config_layers(layers: &[ConfigLayerEntry]) -> std::io::Result<()> {
for layer in layers.iter().filter(|layer| !layer.is_disabled()) {
validate_shell_environment_policy_filter_config(&layer.config).map_err(|error| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!(
"invalid shell environment policy in {}: {error}",
format_config_layer_source(&layer.name, CONFIG_TOML_FILE)
),
)
})?;
}
Ok(())
}
/// Ensures precedence ordering of config layers is correct. Returns the index
/// of the active user config layer, if any.
fn verify_layer_ordering(layers: &[ConfigLayerEntry]) -> std::io::Result<Option<usize>> {