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:
git push -u origin "$branch" silently fails (remote branch exists with different history)
- 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
- 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
- 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
- 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.
Problem
The branch cleanup logic in
taskrunner.shonly 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:
git push -u origin "$branch"silently fails (remote branch exists with different history)branch_conflictand fails as non-retryableIn AEGIS, this was the #2 failure mode (6 failures in 7 days).
Root Cause
Worktree cleanup removes local branches, but the remote branch persists. Next run skips the entire cleanup block.
Fix Applied in AEGIS Fork
--force-with-leasefor clean branch reuse (safe — branch was just created fresh from base):Ref
Applied in Stackbilt-dev/aegis commit
d3945d8.