Skip to content

Bash PreToolUse hooks cannot observe the effective working directory of exec_command calls #40348

Open

Description

@mayangzhou-btd

What version of Codex CLI is running?

0.147.0

What subscription do you have?

20X

Which model were you using?

gpt-5.6 Sol

What platform is your computer?

Linux x86_64

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

Tabby

Codex doctor report

no need

What issue are you seeing?

Title: Bash PreToolUse hooks cannot observe the effective working directory of exec_command calls

Summary

A Bash PreToolUse hook cannot determine the effective working directory of a unified exec (exec_command) invocation.

The hook receives:

  • top-level cwd, which represents the Codex session working directory; and
  • tool_input.command, which contains only the shell command.

When an exec_command call specifies a different workdir, that directory is not included in the hook payload. As a result, the hook cannot determine the actual scope of relative paths such as ..

This behavior matches the current Hooks documentation, so this may be a hook schema gap rather than a regression in the existing cwd field.

Environment

  • Codex CLI: 0.147.0
  • Platform: Linux x86_64
  • Hook event: PreToolUse
  • Hook matcher: ^Bash$
  • Tool path: unified exec (exec_command), including a nested call from code mode
  • Workspace type: Android/AOSP Repo workspace containing many Git projects

Reproduction

Start Codex with the session working directory at the root of an AOSP workspace:

/workspace/aosp

Configure a PreToolUse hook for Bash that records the JSON object received on stdin.

Then issue a unified exec call equivalent to:

{
  "cmd": "rg -n \"EffectSpatializer\" .",
  "workdir": "/workspace/aosp/frameworks/media-ex"
}

The intended and actual execution scope of rg . is:

/workspace/aosp/frameworks/media-ex

However, the PreToolUse hook receives a payload equivalent to:

{
  "cwd": "/workspace/aosp",
  "hook_event_name": "PreToolUse",
  "tool_name": "Bash",
  "tool_input": {
    "command": "rg -n \"EffectSpatializer\" ."
  }
}

The payload does not contain:

/workspace/aosp/frameworks/media-ex

The hook process also runs with the session cwd, so the hook cannot recover the invocation directory from its own process working directory.

Actual result

The hook cannot distinguish between these two semantically different calls:

rg -n "EffectSpatializer" .

executed at:

/workspace/aosp

and the same command executed at:

/workspace/aosp/frameworks/media-ex

This distinction matters in an AOSP workspace:

  • Running rg . at the workspace root may scan thousands of repositories and produce excessive output or latency.
  • Running rg . inside one Repo-managed Git project is a normal, bounded search.

A path-aware policy hook must therefore choose between two incorrect behaviors:

  1. Treat the session cwd as the command directory and falsely block safe, repository-scoped commands.
  2. Allow commands with relative paths and lose enforcement for actual workspace-wide scans.

We encountered the first case in practice: a search intended to run under frameworks/media-ex was rejected because the hook could only see the AOSP session root.

Parsing the shell command cannot solve this problem. The effective workdir is supplied as a tool argument and may not appear in the command string as an explicit cd.

Expected result

Please expose the resolved effective working directory of each Bash invocation to PreToolUse and PostToolUse hooks.

An additive field such as the following would preserve the existing meaning of the documented session-level cwd:

{
  "cwd": "/workspace/aosp",
  "tool_cwd": "/workspace/aosp/frameworks/media-ex",
  "tool_name": "Bash",
  "tool_input": {
    "command": "rg -n \"EffectSpatializer\" ."
  }
}

Alternatively, the effective directory could be preserved in the tool-specific input:

{
  "tool_input": {
    "command": "rg -n \"EffectSpatializer\" .",
    "workdir": "/workspace/aosp/frameworks/media-ex"
  }
}

The value should ideally be:

  • the resolved absolute execution directory;
  • present for both direct and code-mode nested unified exec calls;
  • present in both PreToolUse and the corresponding PostToolUse;
  • equal to the actual fallback directory when the caller omits workdir; and
  • retained for the original command when write_stdin later delivers its completion.

A dedicated top-level tool_cwd field may be clearer because it would preserve the documented meaning of the existing session-level cwd.

Impact beyond ripgrep

This affects any hook policy that interprets relative paths, including policies for:

  • rg, find, and other filesystem searches;
  • Git commands;
  • build and test commands;
  • scripts that read or modify files relative to the process directory;
  • monorepos and multi-repository workspaces; and
  • audit logging that needs to record where a command actually ran.

Hooks do not need to be a complete security boundary, but a PreToolUse hook needs the effective execution directory to make reliable decisions about a pending shell command.

Documentation reference

The current Hooks documentation states that:

  • unified exec (exec_command) is exposed to hooks as Bash;
  • the common cwd field is the working directory for the session; and
  • Bash tool_input uses tool_input.command.

Reference:

https://developers.openai.com/codex/hooks

What steps can reproduce the bug?

Uploaded thread: 01a018c0-afd9-7091-9fb7-1b4bf0b533eb

What is the expected behavior?

see abouve

Additional information

No response

Activity

  1. added
    bug
    on Aug 24, 2026
  2. added
    enhancement
    CLI
    hooks
    on Aug 24, 2026
  3. chrysent commented on Sep 17, 2026

    @chrysent

    Independent confirmation with a concrete failure consequence, on codex-cli 0.153.3 (official release tarball, Linux x86_64), PreToolUse ^Bash$ command hook via project hooks.json.

    The consequence: a hook that approves/denies based on the paths the command touches cannot know where relative paths will actually land. A hook can approve a command that writes far outside the workspace the session was started in, or deny a harmless one that writes inside it — the hook's path-based reasoning is evaluated against the wrong root.

    Minimal repro.

    1. Start a session with cwd /workspace (the hook receives "cwd": "/workspace" in the envelope).
    2. Drive one Bash step: printf data > sandbox-probe.txt (a relative-path write).
    3. The hook records the envelope it received; after the turn, look for the file.

    Observed:

    • Envelope cwd: /workspace. Command: relative write, no cd.
    • After the turn, /workspace/sandbox-probe.txt does not exist; the write succeeded somewhere else entirely (the exec tool's effective cwd is not the session cwd and is not exposed in the hook payload).
    • No field in the hook payload distinguishes "wrote next to my workspace" from "wrote anywhere on disk".

    Why it matters for hooks specifically. For path-scoped policy hooks (allow writes under the workspace, deny outside), the missing per-call workdir is not a cosmetic schema gap — it makes any path-based decision unsound for relative-path commands. Matches the code-level projection described in #33986 (pre_tool_use_payload keeps only command); adding the honored per-call workdir (or the exec tool's actual cwd, post-launch) to the PreToolUse payload would make path-based hooks sound.

    Happy to share the capture tooling if useful.

  4. removed
    bug
    on Oct 16, 2026
  5. TyceHerrman commented on Oct 27, 2026

    @TyceHerrman

    the missing effective workdir in this report appears to be the same PreToolUse contract gap tracked in #32360.

    would suggest consolidating this report under #32360, preserving this issue’s explicit requirements for PostToolUse and for retaining the original execution context when completion arrives later through write_stdin

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

    CLIenhancementhooks

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions