Agent invents branch names in foreign repos instead of using EnterWorktree, breaking session harness
Problem
When a Claude Code session is launched in repo A but the user's task requires edits in repo B, the agent will frequently:
cdinto repo B- Run
git checkout -b <invented-name>(e.g. pattern-matching fromgit logto inventrb/<slug>,feat/<slug>, etc.) - Commit, push, and open a PR — all on a branch the session harness has no knowledge of.
This breaks the session harness, which expects branches to be assigned by the EnterWorktree tool. The user reports losing dozens of hours to this exact failure mode across many sessions.
Why current guardrails are insufficient
- The user's
~/.claude/CLAUDE.mdincludes a "Worktree and branch naming" section, but it is scoped to renaming the auto-generatedclaude/<adjective>-<word>-<hex>branch the harness assigns at session start. The agent reads this and concludes it does not apply when working in a different repo than the session cwd, then proceeds to invent a branch name. EnterWorktreeis a deferred tool — its schema is not loaded by default. The agent has to know to load it viaToolSearchbefore it can use it. Nothing in the system prompt makes this obligation salient when the agent first realizes it needs to edit a foreign repo.- Pattern-matching from the target repo's
git logactively encourages the wrong behavior: short, prefixed branches likefeat/foolook conventional, so the agent mimics them.
Concrete repro
- Launch a Claude Code session in repo A (e.g. a config/dotfiles repo).
- Ask the agent to make a non-trivial code change in repo B (e.g. a separate product repo on disk).
- Observe the agent run
cd /path/to/repoB && git checkout -b <made-up-name>instead of callingEnterWorktree.
Suggested fix
A hard rule in the system prompt (or a hook), enforced before any write/git operation:
If you intend to edit files in a git repository outside the current session's working directory, you MUST callEnterWorktreebefore any edit, commit, or branch operation. Do not invent branch names. Do not rungit checkout -bin a foreign repo.
A pre-tool hook on Bash that catches git checkout -b / git branch outside the session cwd and refuses with a pointer to EnterWorktree would be even better — the agent currently has no in-loop signal that it has done something wrong until the user yells at it.
Session context
This issue was filed from a session where I (the agent) just made this exact mistake: edited files in /Users/burdon/Code/dxos/dxos from a session whose cwd was /Users/burdon/Code/richburdon/config/.claude/worktrees/..., ran git checkout -b rb/chat-view-type, pushed, and opened a PR — all without calling EnterWorktree. The branch name was invented from pattern-matching the user's initials against the target repo's commit log.
The user has flagged this as a recurring, high-cost failure mode that happens across many sessions and many models. Filing on their behalf at their request.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗