Simplify session env file handling

This commit is contained in:
Abhinav Vedmala
2026-05-28 23:09:12 -07:00
parent 09c8e31c90
commit 59d6d03cbd
10 changed files with 82 additions and 220 deletions
+2 -16
View File
@@ -121,7 +121,7 @@ fn build_command(shell: &CommandShell, handler: &ConfiguredHandler) -> Command {
command.env_remove(CODEX_ENV_FILE_ENV_VAR);
command.env_remove(CLAUDE_ENV_FILE_ENV_VAR);
if handler.event_name == HookEventName::SessionStart {
command.envs(&shell.env);
command.envs(&shell.session_start_env);
}
command
}
@@ -165,7 +165,7 @@ mod tests {
CommandShell {
program: "hook-shell".to_string(),
args: Vec::new(),
env: HashMap::from([
session_start_env: HashMap::from([
(
CODEX_ENV_FILE_ENV_VAR.to_string(),
"session-owned-env-file".to_string(),
@@ -207,18 +207,4 @@ mod tests {
assert_eq!(command_env(&command, CODEX_ENV_FILE_ENV_VAR), Some(None));
assert_eq!(command_env(&command, CLAUDE_ENV_FILE_ENV_VAR), Some(None));
}
#[test]
fn session_start_hook_receives_session_owned_env_file_paths() {
let command = build_command(&shell(), &handler(HookEventName::SessionStart));
assert_eq!(
command_env(&command, CODEX_ENV_FILE_ENV_VAR),
Some(Some(OsStr::new("session-owned-env-file")))
);
assert_eq!(
command_env(&command, CLAUDE_ENV_FILE_ENV_VAR),
Some(Some(OsStr::new("session-owned-env-file")))
);
}
}
+1 -1
View File
@@ -36,7 +36,7 @@ use std::collections::HashMap;
pub(crate) struct CommandShell {
pub program: String,
pub args: Vec<String>,
pub env: HashMap<String, String>,
pub session_start_env: HashMap<String, String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
+17 -17
View File
@@ -203,7 +203,7 @@ with Path(r"{log_path}").open("a", encoding="utf-8") as handle:
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -219,7 +219,7 @@ with Path(r"{log_path}").open("a", encoding="utf-8") as handle:
plugin_hook_load_warnings: Vec::new(),
shell_program: None,
shell_args: Vec::new(),
command_env: HashMap::new(),
session_start_env: HashMap::new(),
});
assert!(listed.hooks[0].is_managed);
let cwd = cwd();
@@ -308,7 +308,7 @@ async fn requirements_managed_hooks_execute_windows_command_override() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -388,7 +388,7 @@ fn unknown_requirement_source_hooks_stay_managed() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -471,7 +471,7 @@ fn user_disablement_filters_non_managed_hooks_but_not_managed_hooks() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -535,7 +535,7 @@ fn user_disablement_does_not_filter_managed_layer_hooks() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -697,7 +697,7 @@ fn requirements_managed_hooks_load_when_managed_dir_is_missing() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -754,7 +754,7 @@ fn allow_managed_hooks_only_false_keeps_unmanaged_hooks() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -809,7 +809,7 @@ fn allow_managed_hooks_only_in_config_toml_does_not_enable_policy() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -880,7 +880,7 @@ fn allow_managed_hooks_only_skips_unmanaged_json_and_toml_hooks() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -920,7 +920,7 @@ fn allow_managed_hooks_only_skips_unmanaged_plugin_hooks() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -993,7 +993,7 @@ fn allow_managed_hooks_only_keeps_managed_requirement_and_config_layer_hooks() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -1104,7 +1104,7 @@ fn discovers_hooks_from_json_and_toml_in_the_same_layer() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -1198,7 +1198,7 @@ print(json.dumps({
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -1227,7 +1227,7 @@ print(json.dumps({
plugin_hook_load_warnings: Vec::new(),
shell_program: None,
shell_args: Vec::new(),
command_env: HashMap::new(),
session_start_env: HashMap::new(),
});
assert_eq!(
listed.hooks[0].plugin_id.as_deref(),
@@ -1314,7 +1314,7 @@ fn plugin_hook_sources_expand_plugin_placeholders() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
@@ -1359,7 +1359,7 @@ fn plugin_hook_load_warnings_are_startup_warnings() {
CommandShell {
program: String::new(),
args: Vec::new(),
env: HashMap::new(),
session_start_env: HashMap::new(),
},
);
+5 -3
View File
@@ -182,8 +182,10 @@ pub(crate) async fn run(
};
let results = if event_name == HookEventName::SessionStart
&& (shell.env.contains_key(CODEX_ENV_FILE_ENV_VAR)
|| shell.env.contains_key(CLAUDE_ENV_FILE_ENV_VAR))
&& (shell.session_start_env.contains_key(CODEX_ENV_FILE_ENV_VAR)
|| shell
.session_start_env
.contains_key(CLAUDE_ENV_FILE_ENV_VAR))
{
// Serialize hooks that share the session env file to preserve configured order.
let mut results = Vec::with_capacity(matched.len());
@@ -544,7 +546,7 @@ mod tests {
let shell = CommandShell {
program: "sh".to_string(),
args: vec!["-c".to_string()],
env: HashMap::from([(
session_start_env: HashMap::from([(
CODEX_ENV_FILE_ENV_VAR.to_string(),
env_file.path().display().to_string(),
)]),
+2 -2
View File
@@ -37,7 +37,7 @@ pub struct HooksConfig {
pub plugin_hook_load_warnings: Vec<String>,
pub shell_program: Option<String>,
pub shell_args: Vec<String>,
pub command_env: HashMap<String, String>,
pub session_start_env: HashMap<String, String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
@@ -75,7 +75,7 @@ impl Hooks {
CommandShell {
program: config.shell_program.unwrap_or_default(),
args: config.shell_args,
env: config.command_env,
session_start_env: config.session_start_env,
},
);
Self {