Add Windows hook command overrides (#22159)

# Why

Managed hook configs need a shared cross-platform shape without making
the existing `command` field polymorphic. The common case is still one
command string, with Windows needing a different entrypoint only when
the runtime is actually Windows.

Keeping `command` as the portable/default path and adding an optional
Windows override keeps the config easier to read, preserves the existing
scalar shape for non-Windows users, and avoids forcing every caller into
a `{ unix, windows }` object when only one platform needs special
handling.

# What

- Add optional `command_windows` / `commandWindows` alongside the
existing hook `command` field.
- Resolve `command_windows` only on Windows during hook discovery; other
platforms continue to use `command` unchanged.
- Keep trust hashing aligned to the effective command selected for the
current runtime.

# Docs

The Codex hooks/config reference should document `command_windows` as
the Windows-only override for command hooks.
This commit is contained in:
Abhinav authored and GitHub committed 2026-05-11 22:22:29 +00:00
1 parent a175ddacc0
commit 9ab7f4e6ac
16 files changed
+245 -3

No files matched your search

+74
View File
@@ -40,6 +40,7 @@ fn hooks_file_deserializes_existing_json_shape() {
matcher: Some("^Bash$".to_string()),
hooks: vec![HookHandlerConfig::Command {
command: "python3 /tmp/pre.py".to_string(),
command_windows: None,
timeout_sec: Some(10),
r#async: false,
status_message: Some("checking".to_string()),
@@ -74,6 +75,7 @@ statusMessage = "checking"
matcher: Some("^Bash$".to_string()),
hooks: vec![HookHandlerConfig::Command {
command: "python3 /tmp/pre.py".to_string(),
command_windows: None,
timeout_sec: Some(10),
r#async: false,
status_message: Some("checking".to_string()),
@@ -110,6 +112,7 @@ command = "python3 /tmp/pre.py"
matcher: Some("^Bash$".to_string()),
hooks: vec![HookHandlerConfig::Command {
command: "python3 /tmp/pre.py".to_string(),
command_windows: None,
timeout_sec: None,
r#async: false,
status_message: None,
@@ -154,6 +157,7 @@ command = "python3 /enterprise/place/pre.py"
matcher: Some("^Bash$".to_string()),
hooks: vec![HookHandlerConfig::Command {
command: "python3 /enterprise/place/pre.py".to_string(),
command_windows: None,
timeout_sec: None,
r#async: false,
status_message: None,
@@ -164,3 +168,73 @@ command = "python3 /enterprise/place/pre.py"
}
);
}
#[test]
fn hook_events_deserialize_windows_override_from_toml() {
let parsed: HookEventsToml = toml::from_str(
r#"
[[PreToolUse]]
matcher = "^Bash$"
[[PreToolUse.hooks]]
type = "command"
command = "bash /enterprise/hooks/pre.sh"
command_windows = "powershell -File C:\\enterprise\\hooks\\pre.ps1"
"#,
)
.expect("hook command Windows override TOML should deserialize");
assert_eq!(
parsed,
HookEventsToml {
pre_tool_use: vec![MatcherGroup {
matcher: Some("^Bash$".to_string()),
hooks: vec![HookHandlerConfig::Command {
command: "bash /enterprise/hooks/pre.sh".to_string(),
command_windows: Some(
r"powershell -File C:\enterprise\hooks\pre.ps1".to_string(),
),
timeout_sec: None,
r#async: false,
status_message: None,
}],
}],
..Default::default()
}
);
}
#[test]
fn hook_events_deserialize_camel_case_windows_override_from_toml() {
let parsed: HookEventsToml = toml::from_str(
r#"
[[PreToolUse]]
matcher = "^Bash$"
[[PreToolUse.hooks]]
type = "command"
command = "bash /enterprise/hooks/pre.sh"
commandWindows = "powershell -File C:\\enterprise\\hooks\\pre.ps1"
"#,
)
.expect("camelCase hook command Windows override TOML should deserialize");
assert_eq!(
parsed,
HookEventsToml {
pre_tool_use: vec![MatcherGroup {
matcher: Some("^Bash$".to_string()),
hooks: vec![HookHandlerConfig::Command {
command: "bash /enterprise/hooks/pre.sh".to_string(),
command_windows: Some(
r"powershell -File C:\enterprise\hooks\pre.ps1".to_string(),
),
timeout_sec: None,
r#async: false,
status_message: None,
}],
}],
..Default::default()
}
);
}