Skip to content

Fix stale branch causing create_pull_request patch failures #24

@mrjf

Description

@mrjf

Problem

When a program's long-running branch (autoloop/{program-name}) has its draft PR merged into main, the branch retains its old commits. On the next iteration, the agent merges main back into the branch (per Step 3), but the old commits remain in the branch history.

The create_pull_request safe output handler generates a patch via git format-patch covering all commits since the branch diverged from main. This includes the already-merged commits, which attempt to re-create files that already exist on main. git am then fails:

error: .github/workflows/ci.yml: already exists in index
error: AGENTS.md: already exists in index
error: CLAUDE.md: already exists in index
error: biome.json: already exists in index
error: package.json: already exists in index
... (20+ files)

The --3way fallback also fails because the SHA1 info from the original commits doesn't match the current tree. The entire create_pull_request operation fails and all remaining safe outputs are cancelled.

Example failure: https://github.com/githubnext/tsessebe/actions/runs/23970468437 (iteration 4 of build-tsb-pandas-typescript-migration — prior iterations' commits were already on main).

Root cause

Step 3 item 1 in .github/workflows/autoloop.md currently says:

Check out the program's long-running branch autoloop/{program-name}. If the branch does not yet exist, create it from the default branch. If it does exist, ensure it is up to date with the default branch (merge the default branch into it).

Merging main into the branch does not remove the old commits — it just adds a merge commit on top. The branch still carries commits whose diffs duplicate what's already on main, so format-patch produces a patch that replays them.

Proposed fix

Replace Step 3 item 1 with a reset-if-merged approach:

1. Check out the program's long-running branch `autoloop/{program-name}`.
   If the branch does not yet exist, create it from the default branch.
   If it does exist:
   - Fetch the default branch: `git fetch origin main`.
   - Check whether the branch's changes have already been merged into
     main. If `git diff origin/main..autoloop/{program-name}` produces
     no output (i.e., every change on the branch is already on main),
     the branch is stale — **reset it to `origin/main`**:
     `git reset --hard origin/main`.
   - Otherwise, merge the default branch into the long-running branch
     to pick up any upstream changes.

This ensures that after a PR merge, the next iteration starts from a clean main with no duplicate commits. The patch produced by create_pull_request will only contain the current iteration's changes.

File to change

.github/workflows/autoloop.md — Step 3: Implement, item 1 (currently line ~694 in the file).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions