AI coding agents
Git
code review

How to Recover from a Bad Branch or Workspace Drift

Recover from AI agent branch drift by stopping the run, preserving evidence, separating changes, and restarting from a reviewable state.

Junction TeamJunction Panel4 min read
On this page

Drift Is Not Always Failure

Branch drift happens when a Claude Code or Codex run ends up on the wrong branch, edits files outside the task, mixes unrelated changes, or continues from stale workspace state. It is frustrating, but it does not always mean the work is useless.

The recovery goal is simple: stop making the diff worse, preserve enough evidence to understand what happened, and move useful changes back into a reviewable path.

Junction helps because you can inspect live output, Git state, diffs, and session history from the same control surface. The key is to act before the drift becomes a giant mystery branch.

Stop First

When you notice drift, stop or pause the run. Do not keep steering while the workspace is in an unclear state. More prompts can make the agent rationalize the wrong branch or stack unrelated fixes.

Stop when:

  • The branch name is wrong.
  • The agent edits unrelated files.
  • The diff is larger than the task.
  • The agent starts solving a different problem.
  • The worktree contains pre-existing changes the agent did not create.
  • The agent cannot explain why a change belongs.

Stopping is not a punishment. It is a checkpoint.

Preserve Evidence

Before cleaning anything up, capture:

  • Current branch.
  • Changed file list.
  • Latest agent summary.
  • The original prompt or Linear issue.
  • Any test output.
  • Any error that caused the drift.

You do not need a long report. You need enough information to decide which changes are useful.

Classify The Diff

Group changed files into three buckets:

Bucket Action
Clearly belongs to task Preserve or cherry-pick
Maybe useful but out of scope Move to a follow-up issue
Wrong or risky Discard after review

Do not ask the agent to """clean everything up""" until you have classified the shape of the problem. A messy branch needs human judgment first.

Recover Useful Work

There are several recovery paths:

  • Keep the branch and narrow the diff manually.
  • Create a new branch from the correct base and cherry-pick useful commits.
  • Copy a small patch into a clean worktree.
  • Ask the agent to recreate the fix from the clean branch using the observed lesson.
  • Abandon the run and write a better issue.

The right choice depends on how much useful work exists. If the agent changed one correct file and three wrong ones, salvage may be easy. If it rewrote half the repository, restart.

Restart With A Better Prompt

When restarting, make the boundary explicit:

Restart this task from the clean branch feature/billing-empty-state.
Use only packages/site.
Do not modify API code, pricing logic, or database files.
First inspect the prior failed run summary, then propose a plan before edits.

The new prompt should include what went wrong. Agents can avoid a known failure mode if the boundary is clear.

Prevent The Same Drift

After recovery, tighten the next run:

  • Use a dedicated worktree.
  • Start on the right branch.
  • Name the session after the repo and task.
  • Use plan mode for broad changes.
  • Keep acceptance criteria close to the prompt.
  • Review changed files before approving more work.

Branch suggestions and worktree isolation are preventative tools. They are not bureaucracy. They keep agent runs reviewable.

Example: Wrong Base Branch

An agent starts from main instead of feature/new-settings. It makes a good settings UI fix, but the branch lacks the recent state management changes.

Recovery path:

  1. Stop the run.
  2. Save the useful diff.
  3. Create a clean branch from feature/new-settings.
  4. Reapply the focused change.
  5. Run tests.
  6. Open a PR from the correct base.

Do not continue the original branch and hope review catches the base mismatch.

Tradeoffs

Recovery takes time. Restarting can feel wasteful. But a clean restart is often cheaper than reviewing a contaminated branch.

The main judgment call is whether to salvage or abandon. Salvage when the useful diff is small and obvious. Abandon when the agent""'s path is hard to explain.

Where Junction Fits

Junction makes drift easier to catch because live output, file changes, branch state, and review context are visible while the run is happening. That gives you a chance to stop before a bad branch becomes a bad PR.

For prevention, read Use Branch Suggestions to Keep Agent Runs Reviewable and Git Worktree Isolation for AI Coding Agents. For merge conflict recovery, read Send Conflict-Fix Instructions Back to Agents.