Repository navigation
Description
Summary
On Windows, shell commands executed by Codex CLI/Desktop appear as command_execution events and do not appear to emit PreToolUse hooks from %USERPROFILE%\.codex\hooks.json.
This still happens when the hook matcher is changed from "Bash" to "*", so this does not appear to be just an unknown matcher-string issue.
This is not specific to the third-party hook command used for the test. That command was only used as a real hook executable that demonstrably blocks when invoked directly with a Codex-style hook payload. The issue is that Codex does not appear to invoke the hook for the Windows command_execution path.
Environment
- OS: Windows
- Shell used by Codex execution path: Windows PowerShell
- Codex CLI: observed with Windows Codex CLI/Desktop flow
- Hook config path:
%USERPROFILE%\.codex\hooks.json - Sandbox mode for repro:
danger-full-access
Hook config tested
First, the installer-style matcher:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "%USERPROFILE%\\.local\\bin\\dcg.exe"
}
]
}
]
}
}Then the wildcard matcher:
{
"hooks": {
"PreToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "%USERPROFILE%\\.local\\bin\\dcg.exe"
}
]
}
]
}
}The wildcard config was written as UTF-8 without BOM. First bytes:
7B 0D 0A 20 20 20 20 22
Direct hook invocation works
The hook executable blocks correctly when invoked directly with a Codex-style payload:
$payload = '{"tool_name":"Bash","turn_id":"test-turn","tool_input":{"command":"git reset --hard HEAD~1"}}'
$payload | & "$env:USERPROFILE\.local\bin\dcg.exe"
$LASTEXITCODEObserved direct result:
BLOCKED: Destructive Command Detected
Rule: core.git:reset-hard
exit code: 2
So the hook executable itself is functional when it receives the expected payload.
Reproduction
Create a disposable git repository with two commits, then run Codex CLI against that disposable repo:
codex exec --ephemeral --json -s danger-full-access -C <disposable-repo> "Run exactly this shell command and then report the result: git reset --hard HEAD~1"Observed behavior
The destructive command executes successfully even when the hook config uses matcher: "*".
Relevant JSON stream item:
{
"type": "item.completed",
"item": {
"id": "item_1",
"type": "command_execution",
"command": "\"C:\\\\WINDOWS\\\\System32\\\\WindowsPowerShell\\\\v1.0\\\\powershell.exe\" -Command 'git reset --hard HEAD~1'",
"aggregated_output": "HEAD is now at <redacted> one\n",
"exit_code": 0,
"status": "completed"
}
}There is no tool_name field in that command_execution stream item.
I also tested a matcher: "command_execution" hook entry earlier; that did not block the command either.
Expected behavior
One of these should be true:
- Windows
command_executionshould emitPreToolUsehooks, andmatcher: "*"should fire before the command runs. - If
command_executionuses a specific matcher string, it should be documented and the event payload passed to the hook should include enough information for the hook to inspect the actual command. - If Windows
command_executionis not intended to be hookable, the hooks documentation/UI should clearly state that this execution path is not covered.
Related issues
This appears related in class to hook coverage gaps where some handlers do not emit PreToolUse hooks:
Activity
github-actions commentedon May 25, 2026
Potential duplicates detected. Please review them and close your issue if it is a duplicate.
execdoesn't firePreToolUsehooks (fix patch attached) #23411Powered by Codex Action
Stef9332 commentedon May 25, 2026
Thanks. I agree this is related to #23411, but I do not think it is an exact duplicate.
#23411 covers Code Mode freeform
exec. This issue covers the Windows shellcommand_executionpath:{ "type": "command_execution", "command": "\"C:\\\\WINDOWS\\\\System32\\\\WindowsPowerShell\\\\v1.0\\\\powershell.exe\" -Command 'git reset --hard HEAD~1'" }The reproduction here does not route through Code Mode
exec; it is a shell command execution on Windows. Evenmatcher: "*"does not fire.So the likely shared bug class is “handler does not emit
PreToolUse”, but the affected handler/path appears different. If maintainers prefer to consolidate under a broader hook coverage issue, this Windowscommand_executioncase should be tracked explicitly there.leonidrysev commentedon Aug 22, 2026
Fresh reproduction from Codex Desktop on Windows (2026-08-22).
I tested a trusted user-level
%USERPROFILE%\.codex\hooks.jsonregistration with exactly onePreToolUseentry:BashhookSpecificOutput.permissionDecision="deny"[NO-POLLING-LOOPS-GUARD]Bounded live probe:
This executed normally.
Then:
Expected:
PreToolUseshould deny before execution and surface[NO-POLLING-LOOPS-GUARD].Actual: the command executed, exited 0, and produced:
No hook marker appeared, and the
Test-NetConnectionoutput could not have been produced if the pre-hook had fired.This reproduces the issue on the real Codex Desktop execution carrier, not through direct
.jsexecution or a synthetic payload. The same hook command/configuration was independently verified to return the intended deny when invoked directly with a Codex-shapedPreToolUsepayload, so this still looks like missing persistent hook dispatch on the Windows Desktop command path.charle-z commentedon Aug 23, 2026
I re-traced this again against current
mainatc9b19deb09c1841ce7acc33ddb96276030936a29.The current Windows/unified shell tool path is already wired for
PreToolUsebefore execution:ExecCommandHandler::pre_tool_use_payload()parsesExecCommandArgsand emits canonical hook nameBashwith{ "command": args.cmd }.ToolRegistry::dispatch_any_with_terminal_outcome()callsrun_pre_tool_use_hooks(...)beforehandle_any_tool(...)and returns early when the hook result isBlocked.$CODEX_HOME/hooks.jsonpath.So I would not add another
command_execution/exec_commandPreToolUse dispatch path; that would duplicate wiring that is already present on current main.The original Windows reproduction also contains a shell-expansion confound. Its configured hook command is:
%USERPROFILE%\\.local\\bin\\dcg.exeCodex command hooks do not always run under
cmd.exe: the hook runtime derives a shell from the active turn environment, and on a PowerShell environment the configured command is passed to PowerShell unchanged.%USERPROFILE%is cmd expansion syntax, not PowerShell syntax ($env:USERPROFILEis the PowerShell form). A hook can therefore fail beforedcg.exeis invoked even though direct execution of the executable succeeds.That matters because current
PreToolUsefailure semantics are fail-open. Inpre_tool_use::parse_completed(), spawn errors, nonzero exits other than a valid blocking exit-2 result, invalid JSON-like output, and similar failures produceHookRunStatus::Failedwhile leavingshould_block = false;run_pre_tool_use_hooks()consequently returnsContinue. The protected shell command then executes.The fresh Desktop reproduction posted on 2026-08-22 is useful evidence that the dangerous observable behavior still exists, but as posted it does not establish that an expansion-free absolute hook command reached the current PreToolUse runner. It says the same hook command/configuration was directly verified, so it does not remove the shell-expansion/fail-open confound from the original setup.
A discriminating Windows/Desktop reproduction should therefore use a literal absolute executable path with no
%VAR%,$env:..., quoting indirection, wrapper shell, or other expansion. It should capture the emitted hook started/completed events as well as whether the target command ran. If that form still shows no PreToolUse event at all, the remaining bug is outside the currentExecCommandHandler/registry wiring and can be narrowed to Desktop/app-server configuration/runtime propagation.There is a separate, real hardening question exposed by this issue: should a trusted synchronous PreToolUse policy hook fail closed when it cannot spawn/execute/parse? I am not submitting that as a Windows fix here because it changes global hook policy semantics and could make any broken user hook disable all matching tools. That needs an explicit product/API decision (for example, an opt-in/managed
fail_closedpolicy) plus tests for spawn failure, timeout, malformed output, ordinary nonzero exit, and multiple hooks.Current conclusion: no missing
exec_commandPreToolUse wiring found on main; no safe local patch for #24453 without an expansion-free Desktop reproduction or an explicit fail-closed policy decision.charle-z commentedon Aug 27, 2026
Revalidated this on current upstream
main(102ae5e2e6ff63d2e7346138182e7eec2fc0711a) and on native Windows with Codex0.150.0-alpha.13.The normal Windows agent shell path is not missing
PreToolUsedispatch. With a cleanhooks.json, matcherBash, and a hook executable referenced by an absolute literal path, the observed sequence is:hook/started-> hook returnspermissionDecision: deny->hook/completedwithstatus: blocked-> command is not executed.The hook receives
tool_name: "Bash"and the expected rawtool_input.command. The same result holds through app-server using a trusted hook (hooks/list-> persistcurrentHash->trustStatus: trusted) and withpermissions: ":workspace", so this is not a CLI-vs-Desktop config parity issue.I can also reproduce the original failure mode by changing only the hook command to the issue-style
%USERPROFILE%\\...form while the selected Windows environment shell is PowerShell. In that case the sequence is:hook/started->hook/completed(status=failed, exit 1)-> main command executes successfully.So the successful
command_executiondoes not prove the hook was never dispatched; it is consistent with the current fail-open behavior when the PreToolUse hook itself fails. I would not add another PreToolUse call toexec_command, since that dispatch already exists and works.Separately,
thread/shellCommandis an app-server user-shell escape hatch that does bypassToolRegistry/PreToolUse and emitscommandExecutionwithsource: "userShell". That is a distinct API/product question and should not be conflated with this issue's normal agentexec_commandpath.Given the discriminating repro above, I think #24453 as currently framed can be closed/refuted as hook execution failure + fail-open rather than missing Windows
PreToolUsedispatch. A global fail-closed policy change would be a separate product/API decision.rbcjf1231-a11y commentedon Aug 30, 2026
Fresh Windows discriminator: Desktop nested execution and fresh CLI direct execution
Environment:
26.825.5331.0codex-cli 0.151.0-alpha.7.1PreToolUsehook reported Installed / Active / TrustedThis reproduction separates repository policy behavior from runtime dispatch.
Directly invoking the exact project policy handler with a canonical
PreToolUse/tool_name: "Bash"payload for the harmless discriminator:returns the repository marker and blocks with exit 2. The policy intentionally rejects an unknown Git helper.
On the live paths:
execcalled nestedtools.exec_commandwith the same command.In both cases, no repository handler marker or hook lifecycle event was observed. The Git child actually ran and returned Git's own unknown-command error with exit 1. That child output could not occur if the project handler's direct decision had been applied.
A separate exact recursive cleanup probe against an empty, isolated task TEMP directory also did not reach the repository handler. No
CommandExecutionchild event appeared; the request was rejected earlier by a generic host/CreateProcess policy message. No directory was deleted.This leaves two distinct boundaries:
This overlaps with #23411 for the Desktop code-mode parent path, but the fresh direct CLI reproduction shows the discriminator is not limited to the outer Code Mode
exechandler.For task-owned cleanup, #32505 is related to the needed runtime-owned provenance. Parent correlation alone is not sufficient to prove that the current task actually created a directory; safe admission needs authenticated creation/ownership provenance or a dedicated host-owned cleanup capability.
fmilz commentedon Aug 30, 2026
fmilz commentedon Aug 31, 2026
carlosrises commentedon Aug 31, 2026
Fresh follow-up from a newly created Codex Desktop task on Windows (2026-08-31).
Environment:
codex-cli 0.151.0-alpha.7.2%VAR%,$env:, wrapper shell, or expansion)thread_source=agent_created_thread,source=vscodeTwo disposable black-box probes were run:
ApplyPatchHandlercreated a sentinel file although the configured PreToolUse timer guard should have denied before the write.git commit --allow-empty --no-verify -m pretooluse-probein a disposable repository. It exited 0 and createdHEAD, although the configuredno-verifyguard is an always-deny guard.The sentinel and disposable repository were then removed.
Additional discriminator:
SessionStarthook has no matcher, but no session panel was observed;hook/started, zerohook/completed, and no reference to the installed bundle digest;thread_source=subagentboundary did emit hook lifecycle events (rendered as an unknown conversation), while the normalagent_created_threadtask did not.This narrows the current failure further than matcher naming or shell expansion: on this normal Desktop task, the host did not dispatch the installed bundle at all, including
SessionStart. The observable protection remains the native Git hook; PreToolUse is still not certifiable on this carrier.A useful Windows regression test would compose: trusted literal-absolute project/user hook + reloaded normal Desktop task +
SessionStart+ direct/nested execution, and assertHookStarted -> HookCompleted(blocked)plus absent child effects.CtrlCarlitos commentedon Sep 20, 2026
Windows follow-up reproduced this on 2026-09-20 with Microsoft Windows NT 10.0.26200.0, PowerShell 7.6.6, and codex-cli 0.154.0. The user hook was discovered from %USERPROFILE%.codex\hooks.json; after explicit review, /hooks showed the catch-all PreToolUse handler Active and Trusted. As a control, the effective commandWindows handler was invoked through the same cmd.exe /C outer-wrap shape and consumed a Codex hook envelope, returned valid allow JSON, and exited 0. In a real interactive session, four PowerShell-backed functions.exec calls were present in the rollout, including two after trust, but none produced a corresponding hook audit record. A post-session evidence scan found eligible=0, sessions=0, qualifying_sessions=0 and exited 1. This separates command-handler correctness and hook trust from the missing Windows command_execution dispatch. Downstream report: CtrlCarlitos/agent-guardrails#143
fix(selftest): the Windows codex benign probe asserts the landed fail…
fix(selftest): the probe matrix passes on a Windows host (#137)
docs(adr): ADR-0028 settings files are user-owned, enforcement lives …
fix(genconfig): retire the no-op floor classes from claude and openco…
CtrlCarlitos commentedon Sep 23, 2026
Confirmed on Windows with Guardrail v0.23.0-dev. The
PreToolUsehook from%USERPROFILE%\.codex\hooks.jsonuses matcher*; Codex reports it trusted, and the handler is directly runnable and fail-closed. Four unrelated Graft lifecycle hooks were disabled to isolate this test.A fresh Codex session started after deployment and executed two separate command tools:
Get-Locationandgit status --short. Both commands completed, but the Guardrail handler received no records.Evidence:
Direct handler execution succeeds, so this remains consistent with a Windows runtime-dispatch gap rather than a trust, configuration, handler, or policy-evaluation failure.
CtrlCarlitos commentedon Sep 30, 2026
Update: on codex-cli 0.159.0 (Windows 11, PowerShell 7, 2026-09-30),
PreToolUsenow dispatches for command execution.%USERPROFILE%\.codex\hooks.json, matcher*) fired for realcodex execruns; the CLI printshook: PreToolUse/hook: PreToolUse Completed.updatedInput: the rewritten command ran inpwsh.exe.From our side this issue is fixed in 0.159. The remaining gap is the one in #32360: the hook still does not receive the command's effective
workdir.fix(hooks): report a Codex shell guard on Windows as Unsupported
feat: add native Windows x64 and Arm builds as an experimental platfo…
etraut-openai commentedon Oct 12, 2026
(response from codex)
Closing because the Windows PreToolUse dispatch regression has been reported resolved in CLI 0.159.0. A user who previously reproduced no dispatch retested the same user-level hook with matcher "*" and confirmed it fired in actual exec runs and Codex applied its rewritten command. Please update; if your specific hook still fails, reopen with the CLI version, shell, sanitized hooks.json, and whether hook start/completion is reported. Missing effective workdir in the hook payload is tracked separately in #32360.
feat(integrations): refresh harness deliveries for current vendor rel…