Main-loop shell cwd silently relocates into a background `isolation: worktree` agent's worktree — git status shows clean, uncommitted changes appear "lost"

Resolved 💬 2 comments Opened Jun 8, 2026 by TKMD Closed Jul 15, 2026

Summary

Two issues observed in one session (Claude Code, model claude-opus-4-8, Linux).

Bug 1 — Main-loop Bash working directory hijacked by a background worktree agent (high impact)

After spawning background sub-agents with isolation: "worktree" (Agent tool, run_in_background: true), the main loop's persistent Bash cwd silently moved from the repo root into one of those agents' worktrees (<repo>/.claude/worktrees/agent-<id>, a detached HEAD at the same commit).

Consequence:

  • git status from the main loop reported working tree clean for that detached worktree, while the actual feature branch had legitimate uncommitted changes in the real checkout.
  • It looked like a sub-agent's edits were lost. They were safe in the main checkout; only found via git worktree list + git -C <repo-root> status.
  • Recovered by explicitly cd-ing back to the repo root.

Why it's dangerous: an automated agent trusting its own git status would conclude "nothing to commit", skip the commit, or operate against the wrong tree. The cwd move is silent — no signal the shell relocated.

Likely trigger: one of the background isolation: worktree agents reported a worktreePath on completion and the main shell's cwd ended up inside it.

Expected: the main loop's Bash cwd must never be relocated into a sub-agent's worktree; worktree isolation should stay contained to the sub-agent.

Bug 2 — Intermittent "tool call was malformed and could not be parsed"

Several tool calls failed with Your tool call was malformed and could not be parsed. Please retry., repeatedly on calls with large arguments (e.g. a long SendMessage body). Retries eventually succeeded but interrupted multi-step flows.

Environment

  • Claude Code (CLI), model claude-opus-4-8, Linux, gh 2.93.0
  • git repo using linked worktrees; multiple background isolation: worktree agents active

Repro (Bug 1, approximate)

  1. From repo root on a feature branch with uncommitted changes, spawn >=1 background agent via the Agent tool with isolation: "worktree", run_in_background: true.
  2. Let it complete (it returns a worktreePath).
  3. Keep running git status from the main loop's Bash.
  4. Observe cwd is now inside .claude/worktrees/agent-<id> (detached HEAD); status shows clean; the branch's real changes are only visible via git -C <repo-root>.

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗