Skip to content

fix: branch conflict — check remote refs, not just local #14

@stackbilt-admin

Description

@stackbilt-admin

Problem

The branch cleanup logic in taskrunner.sh only checks local refs (refs/heads/${branch}) for existing branches before creating a task branch. When a prior task run pushed a branch to remote but the local ref was cleaned up by worktree removal, the stale remote branch goes undetected.

This causes:

  1. git push -u origin "$branch" silently fails (remote branch exists with different history)
  2. The task is classified as branch_conflict and fails as non-retryable

In AEGIS, this was the #2 failure mode (6 failures in 7 days).

Root Cause

# Line 676 — only checks LOCAL refs
if git -C "$repo_path" rev-parse --verify "refs/heads/${branch}" >/dev/null 2>&1; then

Worktree cleanup removes local branches, but the remote branch persists. Next run skips the entire cleanup block.

Fix Applied in AEGIS Fork

  1. Fetch + check both local AND remote refs before branch creation:
git -C "$repo_path" fetch origin --prune --quiet 2>/dev/null || true
local has_local_branch=false has_remote_branch=false
git -C "$repo_path" rev-parse --verify "refs/heads/${branch}" >/dev/null 2>&1 && has_local_branch=true
git -C "$repo_path" show-ref --verify --quiet "refs/remotes/origin/${branch}" 2>/dev/null && has_remote_branch=true
  1. Auto-close stale PRs instead of refusing to proceed:
if [[ "$pr_state" == "OPEN" ]]; then
  gh pr close "$branch" --repo "$repo_slug_check" --comment "Superseded by task re-run" 2>/dev/null || true
fi
  1. Push with --force-with-lease for clean branch reuse (safe — branch was just created fresh from base):
git push --force-with-lease -u origin "$branch" 2>/dev/null || true

Ref

Applied in Stackbilt-dev/aegis commit d3945d8.

Metadata

Metadata

Assignees

No one assigned

    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