claude-code - 💡(How to fix) Fix Agent invents branch names in foreign repos instead of using EnterWorktree, breaking session harness [1 comments, 2 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#55660Fetched 2026-05-03 04:47:45
View on GitHub
Comments
1
Participants
2
Timeline
3
Reactions
0
Participants
Timeline (top)
labeled ×2commented ×1
RAW_BUFFERClick to expand / collapse

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:

  1. cd into repo B
  2. Run git checkout -b <invented-name> (e.g. pattern-matching from git log to invent rb/<slug>, feat/<slug>, etc.)
  3. 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.md includes a "Worktree and branch naming" section, but it is scoped to renaming the auto-generated claude/<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.
  • EnterWorktree is a deferred tool — its schema is not loaded by default. The agent has to know to load it via ToolSearch before 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 log actively encourages the wrong behavior: short, prefixed branches like feat/foo look conventional, so the agent mimics them.

Concrete repro

  1. Launch a Claude Code session in repo A (e.g. a config/dotfiles repo).
  2. Ask the agent to make a non-trivial code change in repo B (e.g. a separate product repo on disk).
  3. Observe the agent run cd /path/to/repoB && git checkout -b <made-up-name> instead of calling EnterWorktree.

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 call EnterWorktree before any edit, commit, or branch operation. Do not invent branch names. Do not run git checkout -b in 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.

extent analysis

TL;DR

To fix the issue, enforce a hard rule in the system prompt or a hook that requires calling EnterWorktree before editing files in a foreign git repository.

Guidance

  • Identify situations where the agent needs to edit files in a repository outside the current session's working directory and ensure EnterWorktree is called before any edit, commit, or branch operation.
  • Implement a pre-tool hook on Bash to catch git checkout -b or git branch commands outside the session cwd and refuse with a pointer to EnterWorktree.
  • Review the agent's behavior and adjust its logic to avoid inventing branch names based on pattern-matching from the target repo's git log.
  • Consider adding a check to ensure the agent is aware of the EnterWorktree tool and its usage before attempting to edit files in a foreign repository.

Example

No code snippet is provided as the issue does not imply a specific code change, but rather a change in the agent's behavior and the addition of a hook or rule.

Notes

The suggested fix relies on the implementation of a hard rule or hook to enforce the correct behavior, and it may require adjustments to the agent's logic and the system prompt.

Recommendation

Apply a workaround by implementing a pre-tool hook on Bash to catch and prevent incorrect git checkout -b or git branch commands, and ensure the agent calls EnterWorktree before editing files in a foreign repository. This will help mitigate the recurring failure mode until a more permanent fix can be implemented.

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…

Still need to ship something?

×6

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

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Agent invents branch names in foreign repos instead of using EnterWorktree, breaking session harness [1 comments, 2 participants]