Skip to content

CodexHooks

RunOptions.hooks and refused tools on Codex, built on its command hooks.

Codex runs a command for each hook, with the call as JSON on stdin, so the functions in RunOptions.hooks are reached through a relay: a one-line script, run by this process’s own runtime (node or bun), that sends the call over a unix socket and prints the answer. The socket’s path reaches it in an environment variable, so the command is the same for every session.

A tool that can’t be removed is refused by a hook that needs no relay: a shell command that exits 2, matched by tool name. The model still sees the tool, and gets the refusal as its result.

RunOptions.mcp is checked through the relay, so that the session’s own process decides each MCP call, but by a command that fails closed: it lets the call run only if the session says so, and refuses it if the session can’t be reached, or the runtime can’t start. Codex names an MCP tool mcp__<server>__<tool> in a hook, even where the model sees it in a namespace or (with features.non_prefixed_mcp_tool_names) unprefixed, and runs the hook for calls from code mode’s exec too.

Hooks can’t be set per thread, so they are -c flags on app-server. Codex skips hooks it doesn’t trust: hooks.state records a hook’s hash as trusted (undocumented; --dangerously-bypass-hook-trust isn’t accepted by app-server, and would trust a repository’s hooks too). Only hooks from these flags are named, so hooks in the user’s config keep whatever trust they have. Where features.hooks is off, Codex lists none of these hooks. As on Claude, whose disableAllHooks stops hooks from settings but not a session’s own, they run anyway and the user’s stay off: hooks go on for this process, and hooks.state disables every hook but these and managed ones. A managed policy that stops these from running can’t be overridden, unlike on Claude: open fails, and a detached session that needs them for neither hooks nor disable opens without them. The agent’s commands inherit the variable and could call the socket; they run with full access anyway, as the same user. beforeTool’s refusal is PreToolUse’s JSON deny, through the same relay. All of this was verified on 0.162.1 by test/codex-hooks-e2e.test.ts.

Not covered, because Codex runs no hooks for them: hosted tools such as web search (which has its own switch), and write_stdin (likewise). A failed apply_patch runs no PostToolUse, so afterTool doesn’t see it.