claude-code - 💡(How to fix) Fix Feature request: Worktree-spawned agents should inherit parent session's hooks (PreToolUse/PostToolUse) [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#46279Fetched 2026-04-11 06:24:30
View on GitHub
Comments
0
Participants
1
Timeline
7
Reactions
0
Author
Participants
Timeline (top)
labeled ×4referenced ×3

Root Cause

We are building an evaluation framework (Get Anything Done) that tests AI coding agents under different workflow conditions. We need to capture behavioral data (tool calls, skill invocations, file mutations) from each eval run to compare agent behavior across conditions.

The isolation: "worktree" feature is perfect for eval isolation — each agent gets a clean working tree. But without hooks firing, we get the code output with zero process data. We can see WHAT the agent built but not HOW it built it.

Fix Action

Fix / Workaround

Workaround attempted

RAW_BUFFERClick to expand / collapse

Problem

When using the Agent tool with isolation: "worktree", the spawned subagent runs in a git worktree directory. However, the subagent does not inherit the parent session's hook configuration from .claude/settings.json.

This means:

  • PreToolUse and PostToolUse hooks do not fire for any tool calls made by the worktree agent
  • No trace events are captured for the subagent's work
  • The parent session has no visibility into what tools the subagent used, what files it modified, or how it behaved

Why this matters

We are building an evaluation framework (Get Anything Done) that tests AI coding agents under different workflow conditions. We need to capture behavioral data (tool calls, skill invocations, file mutations) from each eval run to compare agent behavior across conditions.

The isolation: "worktree" feature is perfect for eval isolation — each agent gets a clean working tree. But without hooks firing, we get the code output with zero process data. We can see WHAT the agent built but not HOW it built it.

Current behavior

  1. Parent session has .claude/settings.json with PreToolUse/PostToolUse hooks configured
  2. Parent spawns Agent tool with isolation: "worktree"
  3. Worktree agent runs, makes 50-100+ tool calls
  4. None of those tool calls trigger the hooks
  5. Parent gets the agent's result text but no hook event data

Expected behavior

Worktree agents should inherit the parent session's hook configuration. Every tool call in the worktree should fire the same PreToolUse/PostToolUse hooks as the parent session, writing events to the hook handler.

Alternatively: expose a way to configure hooks for worktree agents specifically (e.g., an env parameter on the Agent tool that sets environment variables the hook handler can read).

Environment

  • Claude Code version: latest (as of April 2026)
  • OS: Windows 11
  • Hook handler: Custom Node.js script at an absolute path in .claude/settings.json
  • The hook handler works correctly in the parent session — only worktree agents are affected

Workaround attempted

We tried setting GAD_EVAL_TRACE_DIR as an environment variable in the agent's prompt (asking it to export the var). This doesn't work because:

  1. The agent's export runs in a bash subprocess, not in Claude Code's process
  2. The hook handler runs in Claude Code's process, not in the agent's bash
  3. Even if the env var were set, the hooks don't fire at all for worktree agents

Impact

This blocks our ability to collect behavioral traces from isolated eval runs, which is the core measurement capability of our evaluation framework. We currently have to choose between:

  • Isolation (worktree, clean data, no traces)
  • Tracing (same session, hooks fire, but potential contamination between runs)

We need both.

extent analysis

TL;DR

  • Inherit the parent session's hook configuration in the worktree agent or expose a way to configure hooks for worktree agents specifically.

Guidance

  • Investigate the Agent tool's configuration options to see if there's an existing way to pass the parent session's hook configuration to the worktree agent.
  • Consider modifying the Agent tool to accept an additional parameter, such as hookConfig, that allows specifying the hook configuration for the worktree agent.
  • Look into using environment variables that can be set by the parent session and inherited by the worktree agent, which can then be used by the hook handler.
  • Evaluate the possibility of implementing a custom solution that uses inter-process communication to forward hook events from the worktree agent to the parent session's hook handler.

Example

  • No code snippet is provided as the issue does not contain sufficient information about the Agent tool's API or the hook handler's implementation.

Notes

  • The solution may require modifications to the Agent tool or the hook handler, which could have implications for the overall system architecture.
  • The use of environment variables may not be a reliable solution due to the differences in process environments between the parent session and the worktree agent.

Recommendation

  • Apply workaround: Modify the Agent tool to accept an additional parameter that allows specifying the hook configuration for the worktree agent, or use a custom inter-process communication solution to forward hook events.
  • Reason: This approach allows for a more flexible and reliable solution that can be tailored to the specific requirements of the evaluation framework.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Worktree agents should inherit the parent session's hook configuration. Every tool call in the worktree should fire the same PreToolUse/PostToolUse hooks as the parent session, writing events to the hook handler.

Alternatively: expose a way to configure hooks for worktree agents specifically (e.g., an env parameter on the Agent tool that sets environment variables the hook handler can read).

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING