Windows: Codex commandWindows uses cmd.exe syntax, but Codex runs hooks in the session shell (PowerShell), so both hooks fail
Summary
On Windows, the Codex hook entries that impeccable writes to ~/.codex/hooks.json use cmd.exe syntax in commandWindows:
if exist "C:\\Users\\<user>\\.agents\\skills\\impeccable\\scripts\\impeccable.cmd" ("C:\\Users\\<user>\\.agents\\skills\\impeccable\\scripts\\impeccable.cmd" hook & exit /b)Codex does not run hook commands with cmd.exe when the session has a shell. It runs them with the session's shell, which on our Windows machine is PowerShell 7. PowerShell cannot parse if exist "…" (…), so both Impeccable hooks (PostToolUse Edit|Write|apply_patch and Stop) fail on every trigger and Impeccable never runs inside Codex.
Environment
- Windows 11, Codex CLI 0.154.0, session shell PowerShell 7 (
"shell":"powershell"in the session rollout) - Impeccable skill 4.3.1 (
metadata.versionin SKILL.md), user-scope install under~/.agents/skills/impeccable, with the bundledscripts/bin/windows-x64/impeccable.exe
What we observed
In one codex exec run, every apply_patch produced exactly one failed PostToolUse hook (5 of 5), and the Stop event produced a failed hook as well. Running the two commandWindows strings through PowerShell's parser ([System.Management.Automation.Language.Parser]::ParseInput) without executing them returns the equivalent of Missing '(' after 'if' in if statement (our system locale reports it in Spanish). Every other hook in the same hooks.json parses.
Why cmd.exe syntax does not work here
In Codex rust-v0.154.0, the hook runner takes its shell from the turn environment:
codex-rs/core/src/session/mod.rs(lines 4711-4719):hook_shell_program/hook_shell_argvcome fromenvironment.shell.derive_exec_args(...), i.e. the same shell the session uses for commands.codex-rs/hooks/src/registry.rs(lines 130-133): that program and its args become the hookCommandShell.codex-rs/hooks/src/engine/command_runner.rs(lines 390-433):%COMSPEC% /C(cmd.exe) is used only whenshell.programis empty, i.e. when the session has no shell.
The same hook_shell_program wiring is already in rust-v0.146.0, so the cmd.exe behaviour described in #452 is the fallback path, not what a PowerShell session does.
Test matrix
We ran each form the way Codex launches it: pwsh -Command, Windows PowerShell 5.1 -Command, and the fallback %COMSPEC% /C "<line>". Each form called a stub .cmd that echoes its arguments and exits with 3, stored in a path that contains a space, and a missing path.
commandWindows form |
pwsh 7 | PowerShell 5.1 | cmd.exe fallback |
|---|---|---|---|
current: if exist "<p>" ("<p>" hook & exit /b) |
parse error, exit 1 | parse error, exit 1 | runs, exit 3; missing file: exit 0 |
if (Test-Path -LiteralPath '<p>') { & '<p>' hook; exit $LASTEXITCODE } |
runs, exit 3; missing file: exit 0 | runs, exit 3; missing file: exit 0 | fails, exit 1 |
cmd /d /s /c "if exist "<p>" ("<p>" hook)" |
exit 0 without running the stub (quoting mangled) | same | runs, exit 3 |
We found no single string that works under both shells.
Suggested fix
When writing Codex hooks on Windows, emit the PowerShell form in commandWindows:
if (Test-Path -LiteralPath '<path>\impeccable.cmd') { & '<path>\impeccable.cmd' hook; exit $LASTEXITCODE }It is a no-op when the skill has been removed, propagates the hook's exit code, and works in both pwsh 7 and Windows PowerShell 5.1. A single quote inside <path> has to be doubled (''). The trade-off is that a Codex session with no shell falls back to cmd.exe and this form fails there. We applied this form locally and both hooks now run (exit 0 in under 0.6 s with the bundled binary). A reinstall of the skill would revert it, which is why we're reporting it.
AI assistance was used to investigate, test and write this report; the Codex source references and the test matrix were checked by running them.
Source: pbakaus/impeccable