← ClaudeAtlas

main-bash-cwd-persists-nested-worktreelisted

Prevent and diagnose nested git worktrees created at the wrong filesystem path when orchestrating parallel work from the main Claude Code agent. Use when (1) you ran `git worktree add .claude/worktrees/<name> ...` from the main agent's Bash tool, (2) `git worktree list` shows the new worktree at an unexpected nested path like `.claude/worktrees/<previous-worktree>/.claude/worktrees/<name>` instead of the intended top-level location, (3) a subagent dispatched to the intended path reports "worktree path mismatch" or operates from a longer nested path. Root cause: the main agent's Bash tool **persists cwd between calls** (per its docstring: "The working directory persists between commands"). An earlier `cd /abs/path && cmd` changes the main shell's cwd; a later `git worktree add <relative-path> ...` resolves against that persisted cwd rather than the project root, silently creating nested layouts. This is the **inverse** of the subagent variant (subagent-bash-cd-wrong-worktree) — subagent shells reset per call,
wan-huiyan/agent-traffic-control · ★ 2 · AI & Automation · score 79
Install: claude install-skill wan-huiyan/agent-traffic-control
# Main-agent Bash cwd persistence → nested worktrees at wrong path ## Problem You're orchestrating multiple parallel-dispatch subagents, each in its own git worktree. From the main agent's Bash tool you do: ```bash git worktree add .claude/worktrees/track-A origin/main -b feat/track-A # ... dispatch Track A subagent ... # ... investigate codebase ... cd /path/to/repo/.claude/worktrees/track-A && grep -rn "foo" src/ # persists cwd! # ... time passes, you dispatch Track B ... git worktree add .claude/worktrees/track-B origin/main -b feat/track-B ``` You expect `track-B` at `<repo>/.claude/worktrees/track-B`. Instead `git worktree list` reports it at `<repo>/.claude/worktrees/track-A/.claude/worktrees/track-B` — nested inside the Track A worktree. Branches, commits, and pushes still work (git resolves them via `.git/worktrees/<name>` metadata regardless of disk location), but: - Dispatched subagents pointed at the intended path will report "worktree path mismatch." - Cleanup (`git worktree remove .claude/worktrees/track-B`) from the project root won't find it. - Per-worktree caches, venvs, and uploads end up under the wrong parent directory. ## Context / Trigger conditions - Main agent runs more than one `git worktree add` in the same session. - Between worktree-add calls, the main agent ran a Bash command that included a `cd` to a path other than the project root. - The later `git worktree add` uses a **relative** path (e.g., `.claude/worktrees/<name>`). - Symptom: `gi