Skip to content

Fix merge race condition — add retry loop on push failure in mergeWorktree #168

@xliry

Description

@xliry

Problem

mergeWorktree() in src/worktree.ts has a TOCTOU race condition. Two agents finish within seconds of each other, both call mergeWorktree() on the same workspace:

  1. Agent A pulls main, merges its branch
  2. Agent B pulls main at the same moment, merges its branch
  3. Agent A pushes successfully
  4. Agent B's push fails because main moved forward
  5. Silent data loss — Agent B's code never reaches main

The function returns success: false but the task was already marked completed by Claude. The work is lost.

What to do

In src/worktree.ts, mergeWorktree() function:

  1. After push fails, add a retry loop (max 3 attempts):
    • git reset --hard HEAD~1 (undo the merge)
    • git pull --ff-only origin main (get latest)
    • git merge <branch> --no-edit (re-merge)
    • git push origin HEAD (try again)
  2. If all 3 retries fail, return success: false with a clear error
  3. Log each retry attempt

Acceptance

  • Push failure triggers retry with fresh pull
  • Max 3 retry attempts before giving up
  • No silent data loss — either code reaches main or error is clearly reported
  • Build passes: npm run build

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions