Skip to content

PreToolUse hooks are not invoked for spawn_agent, so a deny cannot block sub-agent spawns #49736

Open

Description

@Eazy-K

What version of Codex CLI is running?

codex-cli 0.159.2

What subscription do you have?

Not specified

Which model were you using?

gpt-6-sol (reasoning effort high)

What platform is your computer?

Windows 11 Pro (10.0.26200), x64

What terminal emulator and version are you using (if applicable)?

Windows Terminal / PowerShell

Codex doctor report

n/a

What issue are you seeing?

A user-level PreToolUse hook whose matcher covers spawn_agent is never invoked when the model spawns a sub-agent in an interactive session, so a permissionDecision: "deny" from the hook cannot block the spawn.

Other hooks in the same ~/.codex/hooks.json do run in the same session (a UserPromptSubmit hook and a PostToolUse hook both fire and their additional_context shows up in the rollout as hooks.additional_context developer messages), so hooks are loaded and trusted. Only the PreToolUse hook for spawn_agent never runs.

This looks like the same bug class as #23411 (Code Mode exec doesn't fire PreToolUse) and #18391 (apply_patch): the spawn_agent handler (namespace: "collaboration") apparently doesn't provide a pre-tool-use payload, so run_pre_tool_use_hooks is never called for it.

What steps can reproduce the bug?

  1. In ~/.codex/hooks.json add:
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "^(Agent|(.*[._:/])?spawn_agent)$",
        "hooks": [{ "type": "command", "command": "python /path/to/guard.py" }]
      }
    ]
  }
}

where guard.py appends one line to a log file and prints:

{"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "deny", "permissionDecisionReason": "blocked by test hook"}}
  1. Start a new interactive codex session and ask it to spawn a sub-agent (e.g. spawn_agent with agent_type = a configured role, message "say hello").
  2. The rollout contains the function_call spawn_agent (namespace: "collaboration") and the sub-agent runs and answers.
  3. The hook's log file gets no line and the rollout shows no hook output for that call. Piping the same payload ({"hook_event_name":"PreToolUse","tool_name":"spawn_agent","tool_input":{...}}) into guard.py by hand does produce the log line and the deny.

What is the expected behavior?

PreToolUse hooks run for spawn_agent (and the other collaboration tools) like they do for Bash/apply_patch, and a deny decision prevents the sub-agent from starting. This is needed to enforce policies such as "only these agent roles / models may be spawned".

Additional information

Activity

  1. added
    bug
    CLI
    windows-os
    hooks
    subagent
    on Sep 30, 2026
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

    CLIbughookssubagentwindows-os

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions