config: add override config foundation

# Conflicts:
#	codex-rs/config/src/loader/mod.rs
This commit is contained in:
Brent Traut
2026-05-19 13:29:23 -07:00
parent 5c43a64e2b
commit ed68380a48
27 changed files with 1814 additions and 125 deletions
+3 -1
View File
@@ -27,7 +27,9 @@ pub fn hook_states_from_stack(
) {
if !matches!(
layer.name,
ConfigLayerSource::User { .. } | ConfigLayerSource::SessionFlags
ConfigLayerSource::User { .. }
| ConfigLayerSource::UserOverride { .. }
| ConfigLayerSource::SessionFlags
) {
continue;
}
+25 -4
View File
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs;
use std::path::Path;
@@ -92,6 +93,7 @@ pub(crate) fn discover_handlers(
policy,
);
let mut loaded_hooks_json_folders = HashSet::new();
for layer in config_layer_stack.get_layers(
ConfigLayerStackOrdering::LowestPrecedenceFirst,
/*include_disabled*/ false,
@@ -111,7 +113,14 @@ pub(crate) fn discover_handlers(
if !policy.allows(&policy_source) {
continue;
}
let json_hooks = load_hooks_json(layer.hooks_config_folder().as_deref(), &mut warnings);
let hooks_config_folder = layer.hooks_config_folder();
let json_hooks = if hooks_config_folder.as_ref().is_some_and(|folder| {
loaded_hooks_json_folders.insert(folder.as_path().to_path_buf())
}) {
load_hooks_json(hooks_config_folder.as_deref(), &mut warnings)
} else {
None
};
let toml_hooks = load_toml_hooks_from_layer(layer, &mut warnings);
if let (Some((json_source_path, json_events)), Some((toml_source_path, toml_events))) =
@@ -352,12 +361,18 @@ fn load_toml_hooks_from_layer(
fn config_toml_source_path(layer: &ConfigLayerEntry) -> AbsolutePathBuf {
match &layer.name {
ConfigLayerSource::System { file }
| ConfigLayerSource::SystemOverride { file }
| ConfigLayerSource::User { file, .. }
| ConfigLayerSource::UserOverride { file }
| ConfigLayerSource::LegacyManagedConfigTomlFromFile { file } => file.clone(),
ConfigLayerSource::Project { dot_codex_folder } => layer
.hooks_config_folder()
.unwrap_or_else(|| dot_codex_folder.clone())
.join(CONFIG_TOML_FILE),
ConfigLayerSource::ProjectOverride { dot_codex_folder } => layer
.hooks_config_folder()
.unwrap_or_else(|| dot_codex_folder.clone())
.join(codex_config::CONFIG_OVERRIDE_TOML_FILE),
ConfigLayerSource::Mdm { domain, key } => {
synthetic_layer_path(&format!("<mdm:{domain}:{key}>/{CONFIG_TOML_FILE}"))
}
@@ -582,9 +597,15 @@ fn hook_trusted_hash(is_managed: bool, state: Option<&HookStateToml>) -> Option<
fn hook_metadata_for_config_layer_source(source: &ConfigLayerSource) -> (HookSource, bool) {
match source {
ConfigLayerSource::System { .. } => (HookSource::System, true),
ConfigLayerSource::User { .. } => (HookSource::User, false),
ConfigLayerSource::Project { .. } => (HookSource::Project, false),
ConfigLayerSource::System { .. } | ConfigLayerSource::SystemOverride { .. } => {
(HookSource::System, true)
}
ConfigLayerSource::User { .. } | ConfigLayerSource::UserOverride { .. } => {
(HookSource::User, false)
}
ConfigLayerSource::Project { .. } | ConfigLayerSource::ProjectOverride { .. } => {
(HookSource::Project, false)
}
ConfigLayerSource::Mdm { .. } => (HookSource::Mdm, true),
ConfigLayerSource::SessionFlags => (HookSource::SessionFlags, false),
ConfigLayerSource::LegacyManagedConfigTomlFromFile { .. } => {
+75
View File
@@ -1123,6 +1123,81 @@ fn discovers_hooks_from_json_and_toml_in_the_same_layer() {
assert_eq!(preview[1].source_path, config_path);
}
#[test]
fn override_layers_do_not_duplicate_shared_hooks_json() {
let temp = tempdir().expect("create temp dir");
let config_path =
AbsolutePathBuf::try_from(temp.path().join("config.toml")).expect("absolute config path");
let override_path = AbsolutePathBuf::try_from(temp.path().join("config.override.toml"))
.expect("absolute override path");
let hooks_json_path =
AbsolutePathBuf::try_from(temp.path().join("hooks.json")).expect("absolute hooks path");
fs::write(
hooks_json_path.as_path(),
r#"{
"hooks": {
"PreToolUse": [
{
"matcher": "^Bash$",
"hooks": [
{
"type": "command",
"command": "python3 /tmp/json-hook.py"
}
]
}
]
}
}"#,
)
.expect("write hooks.json");
let config_layer_stack = ConfigLayerStack::new(
vec![
ConfigLayerEntry::new(
ConfigLayerSource::System { file: config_path },
TomlValue::Table(Default::default()),
),
ConfigLayerEntry::new(
ConfigLayerSource::SystemOverride {
file: override_path,
},
TomlValue::Table(Default::default()),
),
],
ConfigRequirements::default(),
ConfigRequirementsToml::default(),
)
.expect("config layer stack");
let engine = ClaudeHooksEngine::new(
/*enabled*/ true,
/*bypass_hook_trust*/ false,
Some(&config_layer_stack),
Vec::new(),
Vec::new(),
CommandShell {
program: String::new(),
args: Vec::new(),
},
);
assert_eq!(engine.handlers.len(), 1);
let preview = engine.preview_pre_tool_use(&PreToolUseRequest {
session_id: ThreadId::new(),
turn_id: "turn-1".to_string(),
cwd: cwd(),
transcript_path: None,
model: "gpt-test".to_string(),
permission_mode: "default".to_string(),
tool_name: "Bash".to_string(),
matcher_aliases: Vec::new(),
tool_use_id: "tool-1".to_string(),
tool_input: serde_json::json!({ "command": "echo hello" }),
});
assert_eq!(preview.len(), 1);
assert_eq!(preview[0].source_path, hooks_json_path);
}
#[tokio::test]
async fn plugin_hook_sources_run_with_plugin_env_and_plugin_source() {
let temp = tempdir().expect("create temp dir");