Subagent MCP tool result not delivered after WorktreeCreate hook in non-git workspace
Summary
When Claude Code runs from a non-git workspace using a plugin-provided WorktreeCreate hook, an Agent(isolation: \"worktree\") can start successfully, but a subagent MCP tool call may never be delivered back to the subagent transcript.
In the observed case, the MCP tool (context-mode ctx_batch_execute) completed successfully according to the Claude Code debug log, but the subagent transcript stopped at assistant stop_reason: tool_use and never received the corresponding tool_result. The parent Agent call never returned, and WorktreeRemove was never triggered.
This does not reproduce in a normal git workspace using Claude Code's built-in git worktree handling.
Environment
- Claude Code:
2.1.133 - Platform: macOS Darwin 24.6.0 arm64
- Claude Code binary: native Mach-O executable at
~/.local/share/claude/versions/2.1.133 - MCP server involved:
context-mode1.0.111 - Node used by context-mode wrapper: Node
v20.20.0, ABI115 - Test plugin: local Claude Code plugin providing
WorktreeCreateandWorktreeRemovecommand hooks
Plugin behavior
The local plugin registers:
WorktreeCreate: creates a directory under~/.claude/non-git-worktree/worktrees/<safe-id>/, runsgit init, and returns top-level JSON:
{
"worktreePath": "/Users/.../.claude/non-git-worktree/worktrees/agent-..."
}
WorktreeRemove: safely removes only direct child directories under the plugin-owned worktree root.
The hook script itself returns successfully. The created path exists, contains .git, has no lock files, and remains clean.
Reproduction steps
- Start from a non-git directory:
rm -rf /tmp/cc-context-node20-retest
mkdir -p /tmp/cc-context-node20-retest
cd /tmp/cc-context-node20-retest
printf "context node20 retest\n" > README.md
- Start Claude Code with a local plugin that provides
WorktreeCreate/WorktreeRemovehooks:
claude --plugin-dir "/path/to/non-git-worktree-plugin" --debug hooks
- In Claude Code, ask for a subagent that uses MCP inside
Agent(isolation: \"worktree\"), for example:
Please start an Agent(isolation: "worktree") for a read-only investigation. The subagent may use context-mode MCP tools and Bash. It should inspect the current directory, README content, Node version, context-mode version, and report whether there are better-sqlite3 / NODE_MODULE_VERSION errors. Do not modify files.
- Observe that
WorktreeCreatesucceeds andctx_batch_executesucceeds, but the Agent never returns.
Observed debug log timeline
Relevant debug log: ~/.claude/debug/ab196228-7fce-432c-8f05-100080794a4a.txt
Key events:
MCP server "context-mode": Successfully connected
Hook PreToolUse ... returned permissionDecision: allow
WorktreeCreate ... completed with status 0
"worktreePath": "/Users/.../.claude/non-git-worktree/worktrees/agent-ad33b70c131934aed-20260508233939-82006"
ToolSearchTool: selected mcp__context-mode__ctx_batch_execute
Permission suggestions for mcp__context-mode__ctx_batch_execute
[Stall] tool_dispatch_start tool=mcp__context-mode__ctx_batch_execute ... permissionDecisionMs=24256
MCP server "context-mode": Calling MCP tool: ctx_batch_execute
MCP server "context-mode": Tool 'ctx_batch_execute' completed successfully in 225ms
[Stall] tool_dispatch_end tool=mcp__context-mode__ctx_batch_execute ... outcome=ok durationMs=225
After that line, there are no further relevant events:
- no
tool_resultrecorded in the subagent transcript - no subagent final response
- no parent
Agenttool result - no
WorktreeRemove - no timeout or error in the debug log
Transcript evidence
The subagent transcript exists at a path like:
~/.claude/projects/-private-tmp-cc-context-node20-retest/<session-id>/subagents/agent-<id>.jsonl
Its final event is an assistant message with:
stop_reason: tool_use
工具名: mcp__context-mode__ctx_batch_execute
There is no corresponding tool_result event after it, even though the debug log says the MCP tool completed successfully.
The parent transcript similarly stops at the Agent tool use and never receives an Agent tool result.
Expected behavior
After the MCP tool completes successfully, Claude Code should deliver the MCP tool_result back into the subagent transcript, allow the subagent to continue/finalize, then return the parent Agent result and trigger WorktreeRemove cleanup.
Actual behavior
The MCP tool completes successfully according to debug logs, but the result is not delivered to the subagent transcript. The Agent hangs indefinitely and WorktreeRemove is not triggered.
Comparison with normal git workspace
In a normal git workspace, Agent(isolation: "worktree") does not show this failure in the same environment.
Separately, an A/B test showed that with the non-git plugin path, the subagent shell pwd remains the original non-git directory rather than the returned worktreePath. This suggests the hook-provided worktreePath may be treated as lifecycle metadata rather than the actual subagent shell cwd.
Impact
This makes plugin-provided WorktreeCreate hooks usable only as a startup compatibility shim for non-git workspaces. If a subagent uses MCP tools in this mode, the session can hang and the cleanup hook may never run, leaving orphan plugin-created worktree directories.
Notes
An earlier native module ABI issue with context-mode / better-sqlite3 was fixed and is not the current failure. In the current repro, the debug log shows ctx_batch_execute completing successfully with outcome=ok durationMs=225, but no tool_result reaches the subagent transcript.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗