Report the local command working directory in PreToolUse hooks (#57198)

## Why

`PreToolUse` hooks receive the host working directory even when a local
`exec_command` call specifies a different `workdir`. This prevents hooks from
seeing the directory in which the command will execute.

## What changed

- Share working-directory resolution between command execution and hook input,
  using the selected environment and the command's `workdir`.
- Report the resolved local directory in the hook input's `cwd`. Keep hook
  processes in the host directory and retain the host `cwd` for remote commands.
- Reject invalid environment selections or paths before running hooks and record
  failures in tool-result telemetry.

## Testing

Add unit and integration coverage for default, empty, relative, and absolute
working directories; Windows path conventions; remote commands; hook denial and
input rewriting; and invalid arguments that fail without running hooks.

GitOrigin-RevId: 97f71f6d3e21a5d95a7abf5384f06f4993241797
This commit is contained in:
Andrei Eternal
2026-10-26 19:11:28 +00:00
committed by copyberry
parent 60e4d10ad9
commit fe15bd38de
9 changed files with 395 additions and 13 deletions
+2
View File
@@ -196,6 +196,7 @@ pub(crate) async fn run_pre_tool_use_hooks(
tool_use_id: String,
tool_name: &HookToolName,
tool_input: &Value,
tool_cwd: Option<codex_utils_path_uri::PathUri>,
target_environments: &[&str],
) -> PreToolUseHookResult {
let turn_context = &step_context.turn;
@@ -204,6 +205,7 @@ pub(crate) async fn run_pre_tool_use_hooks(
turn_id: turn_context.sub_id.clone(),
subagent: thread_spawn_subagent_hook_context(sess, turn_context),
cwd: tool_hook_cwd(&step_context.environments, turn_context),
tool_cwd,
transcript_path: sess.hook_transcript_path().await,
model: step_context.settings.model_info.slug.clone(),
permission_mode: hook_permission_mode(step_context.settings.approval_policy()),