Skip to content

Windows command_execution does not emit PreToolUse hooks, even with matcher "*" #24453

Closed

Description

@Stef9332

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"
$LASTEXITCODE

Observed 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:

  1. Windows command_execution should emit PreToolUse hooks, and matcher: "*" should fire before the command runs.
  2. If command_execution uses 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.
  3. If Windows command_execution is 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

  1. added
    bug
    CLI
    windows-os
    hooks
    exec
    on May 25, 2026
  2. github-actions commented on May 25, 2026

    @github-actions
    Contributor

    Potential duplicates detected. Please review them and close your issue if it is a duplicate.

    Powered by Codex Action

  3. Stef9332 commented on May 25, 2026

    @Stef9332
    Author

    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 shell command_execution path:

    {
      "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. Even matcher: "*" 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 Windows command_execution case should be tracked explicitly there.

  4. leonidrysev commented on Aug 22, 2026

    @leonidrysev

    Fresh reproduction from Codex Desktop on Windows (2026-08-22).

    I tested a trusted user-level %USERPROFILE%\.codex\hooks.json registration with exactly one PreToolUse entry:

    • matcher: Bash
    • command hook: valid; direct invocation returns hookSpecificOutput.permissionDecision="deny"
    • fixed deny marker: [NO-POLLING-LOOPS-GUARD]
    • no trust bypass
    • genuinely fresh Codex Desktop task

    Bounded live probe:

    Write-Output 'CODEX_GUARD_FRESH_ALLOW_20260822'

    This executed normally.

    Then:

    do { Test-NetConnection 127.0.0.1 -Port 1 -InformationLevel Quiet; Start-Sleep -Milliseconds 100 } until ($true)

    Expected: PreToolUse should deny before execution and surface [NO-POLLING-LOOPS-GUARD].

    Actual: the command executed, exited 0, and produced:

    WARNING: TCP connect to (127.0.0.1 : 1) failed
    False
    

    No hook marker appeared, and the Test-NetConnection output 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 .js execution or a synthetic payload. The same hook command/configuration was independently verified to return the intended deny when invoked directly with a Codex-shaped PreToolUse payload, so this still looks like missing persistent hook dispatch on the Windows Desktop command path.

  5. charle-z commented on Aug 23, 2026

    @charle-z

    I re-traced this again against current main at c9b19deb09c1841ce7acc33ddb96276030936a29.

    The current Windows/unified shell tool path is already wired for PreToolUse before execution:

    • ExecCommandHandler::pre_tool_use_payload() parses ExecCommandArgs and emits canonical hook name Bash with { "command": args.cmd }.
    • ToolRegistry::dispatch_any_with_terminal_outcome() calls run_pre_tool_use_hooks(...) before handle_any_tool(...) and returns early when the hook result is Blocked.
    • user hook discovery still resolves through the normal Codex config layer / $CODEX_HOME/hooks.json path.

    So I would not add another command_execution/exec_command PreToolUse 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.exe

    Codex 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:USERPROFILE is the PowerShell form). A hook can therefore fail before dcg.exe is invoked even though direct execution of the executable succeeds.

    That matters because current PreToolUse failure semantics are fail-open. In pre_tool_use::parse_completed(), spawn errors, nonzero exits other than a valid blocking exit-2 result, invalid JSON-like output, and similar failures produce HookRunStatus::Failed while leaving should_block = false; run_pre_tool_use_hooks() consequently returns Continue. 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 current ExecCommandHandler/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_closed policy) plus tests for spawn failure, timeout, malformed output, ordinary nonzero exit, and multiple hooks.

    Current conclusion: no missing exec_command PreToolUse wiring found on main; no safe local patch for #24453 without an expansion-free Desktop reproduction or an explicit fail-closed policy decision.

  6. charle-z commented on Aug 27, 2026

    @charle-z

    Revalidated this on current upstream main (102ae5e2e6ff63d2e7346138182e7eec2fc0711a) and on native Windows with Codex 0.150.0-alpha.13.

    The normal Windows agent shell path is not missing PreToolUse dispatch. With a clean hooks.json, matcher Bash, and a hook executable referenced by an absolute literal path, the observed sequence is:

    hook/started -> hook returns permissionDecision: deny -> hook/completed with status: blocked -> command is not executed.

    The hook receives tool_name: "Bash" and the expected raw tool_input.command. The same result holds through app-server using a trusted hook (hooks/list -> persist currentHash -> trustStatus: trusted) and with permissions: ":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_execution does 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 to exec_command, since that dispatch already exists and works.

    Separately, thread/shellCommand is an app-server user-shell escape hatch that does bypass ToolRegistry/PreToolUse and emits commandExecution with source: "userShell". That is a distinct API/product question and should not be conflated with this issue's normal agent exec_command path.

    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 PreToolUse dispatch. A global fail-closed policy change would be a separate product/API decision.

  7. rbcjf1231-a11y commented on Aug 30, 2026

    @rbcjf1231-a11y

    Fresh Windows discriminator: Desktop nested execution and fresh CLI direct execution

    Environment:

    • Windows
    • Codex Desktop 26.825.5331.0
    • bundled codex-cli 0.151.0-alpha.7.1
    • project PreToolUse hook reported Installed / Active / Trusted
    • hook feature enabled; the project hook definition and pinned handler digest were unchanged throughout the probes

    This 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:

    git unknown-policy-helper
    

    returns the repository marker and blocks with exit 2. The policy intentionally rejects an unknown Git helper.

    On the live paths:

    1. Codex Desktop code-mode exec called nested tools.exec_command with the same command.
    2. A fresh CLI run used direct shell execution from the same trusted project.

    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 CommandExecution child event appeared; the request was rejected earlier by a generic host/CreateProcess policy message. No directory was deleted.

    This leaves two distinct boundaries:

    • the project policy handler blocks correctly when invoked;
    • the affected Desktop/CLI execution paths do not expose the command to that handler, while recursive deletion is handled by a separate host policy.

    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 exec handler.

    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.

  8. fmilz commented on Aug 30, 2026

    @fmilz
  9. fmilz commented on Aug 31, 2026

    @fmilz
  10. carlosrises commented on Aug 31, 2026

    @carlosrises

    Fresh follow-up from a newly created Codex Desktop task on Windows (2026-08-31).

    Environment:

    • Codex Desktop / bundled codex-cli 0.151.0-alpha.7.2
    • hooks feature: stable/enabled
    • trusted installed bundle: 22 handlers, 0 modified
    • hook commands use literal absolute executable paths (no %VAR%, $env:, wrapper shell, or expansion)
    • fresh normal task: thread_source=agent_created_thread, source=vscode

    Two disposable black-box probes were run:

    1. ApplyPatchHandler created a sentinel file although the configured PreToolUse timer guard should have denied before the write.
    2. A nested command execution ran git commit --allow-empty --no-verify -m pretooluse-probe in a disposable repository. It exited 0 and created HEAD, although the configured no-verify guard is an always-deny guard.

    The sentinel and disposable repository were then removed.

    Additional discriminator:

    • the installed SessionStart hook has no matcher, but no session panel was observed;
    • the Desktop log for the task contains zero hook/started, zero hook/completed, and no reference to the installed bundle digest;
    • there was no trust, schema, spawn, or parse error in that interval;
    • direct invocation of the installed handler with canonical payloads still returns the expected deny;
    • later calls from a thread_source=subagent boundary did emit hook lifecycle events (rendered as an unknown conversation), while the normal agent_created_thread task 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 assert HookStarted -> HookCompleted(blocked) plus absent child effects.

  11. CtrlCarlitos commented on Sep 20, 2026

    @CtrlCarlitos

    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

  12. CtrlCarlitos commented on Sep 23, 2026

    @CtrlCarlitos

    Confirmed on Windows with Guardrail v0.23.0-dev. The PreToolUse hook from %USERPROFILE%\.codex\hooks.json uses 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-Location and git status --short. Both commands completed, but the Guardrail handler received no records.

    Evidence:

    selected_session=01a0cd25-0789-7fc0-93c4-a0f338cdd0fb
    started=2026-09-23T07:22:31.301Z
    selected=0
    eligible=0
    diagnostic class: transport
    raw evidence: session=01a0cd25-0789-7fc0-93c4-a0f338cdd0fb hook_records=0
    codex: newest known Codex session is silent in the selected audit window
    codex: live mediation not yet observed; approval-proposal gate remains closed
    

    Direct handler execution succeeds, so this remains consistent with a Windows runtime-dispatch gap rather than a trust, configuration, handler, or policy-evaluation failure.

  13. CtrlCarlitos commented on Sep 30, 2026

    @CtrlCarlitos

    Update: on codex-cli 0.159.0 (Windows 11, PowerShell 7, 2026-09-30), PreToolUse now dispatches for command execution.

    • The same user hook (%USERPROFILE%\.codex\hooks.json, matcher *) fired for real codex exec runs; the CLI prints hook: PreToolUse / hook: PreToolUse Completed.
    • Codex applied the hook's updatedInput: the rewritten command ran in pwsh.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.

  14. etraut-openai commented on Oct 12, 2026

    @etraut-openai
    Contributor

    (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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLIbugexechookswindows-os

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions