Skip to content

principal-review auto-merge: stale branch (BEHIND) not distinguished from real merge failure #246

Closed

Description

@libravault-xyz

What

The "Auto-merge if approved" step in principal-review.yml treats any gh pr merge failure the same way — conflict, branch protection, or a merely-stale branch all fall straight through to status:needs-human-merge with a generic "see the log" comment. In a busy pipeline, `dev` routinely advances between principal review's approval and the actual merge call (other PRs from the same pipeline landing concurrently), which makes gh pr merge fail with "the head branch is not up to date with the base branch" — a transient, self-resolvable race, not a real problem with the PR.

Live repro

Hit this manually while merging PR #239 (a risk:high PR, so it went through human-merge rather than this auto-merge path, but the underlying race is identical): the branch went BEHIND twice within about 15 minutes purely from ordinary pipeline throughput (issues #238/#240/#242/#243 all landing on dev in that window), each requiring a manual git merge origin/dev && git push before gh pr merge would succeed. An auto-merge-approved (risk:low) PR hitting the same race today would silently fall through to status:needs-human-merge for no real reason — routing a clean, already-approved PR to a human purely because it lost a timing race.

Acceptance criteria

  • Before attempting gh pr merge, check the PR's mergeStateStatus.
    • BEHIND → request GitHub's server-side "Update branch" (PUT /pulls/{n}/update-branch) and retry, bounded (not an infinite loop).
    • DIRTY (real conflict) → don't retry, route to status:needs-human-merge immediately as today.
    • UNKNOWN (mergeability still computing) → brief bounded wait, then re-check.
  • The whole merge attempt is retried a bounded number of times so a burst of concurrent merges doesn't defeat auto-merge, but a genuinely stuck/failing merge still terminates and routes to a human rather than spinning indefinitely.
  • Existing kill-switch checks (AGENTS_PAUSED, AUTO_MERGE_DISABLED, status:blocked) and the CI-wait loop above this step are unaffected.
  • YAML parses cleanly; the step's bash is traced by hand and passes bash -n/shellcheck (no standalone test harness exists for inline workflow bash, same precedent as PR fix(ci): route dev-agent triage failures to needs-info when a PR was found #239/fix(ci): detect a dev-agent crash mid-triage, not just mid-revise #212).

Manual gh pr merge by a human on a status:needs-human-merge PR (like the risk:high case that surfaced this) is out of scope — there's no automated step to retry there, a human just re-runs it.

Activity

  1. added
    bug
    on Aug 17, 2026
  2. added
    status:ready-for-dev
    status:in-progress
    on Aug 17, 2026
  3. github-actions commented on Aug 17, 2026

    @github-actions
    Contributor

    Triage

    Confirmed the repro path against principal-review.yml's "Auto-merge if approved" step: gh pr merge (line ~373) is a single unconditional call — any failure (real conflict, transient BEHIND race, branch protection) falls through to the same generic status:needs-human-merge handling. Not security/release-blocker — this is a pipeline-throughput correctness bug, not a vulnerability or a shipped-release issue.

    Scope is clear, so I implemented the fix rather than stopping at ready-for-dev: wrapped the merge call in a bounded (5-attempt) retry loop that checks mergeStateStatus first —

    • BEHIND → PUT /pulls/{n}/update-branch via gh api, then re-check
    • UNKNOWN → brief wait, then re-check
    • DIRTY → break immediately, no retry (same as today)
    • anything else → attempt gh pr merge as before

    Verified bash -n and shellcheck clean on the extracted step (GH Actions ${{ }} expressions substituted with placeholders first, same approach as the precedent PRs #239/#212 — no standalone test harness exists for inline workflow bash) and that the YAML still parses.

    Blocked on push permissions — routing to a human

    I can't get this onto a branch on the remote: both the default GITHUB_TOKEN and the libravault-pipeline-bot App token are rejected by GitHub when pushing a change to .github/workflows/**, independent of anything about the diff itself:

    ! [remote rejected] fix/principal-review-behind-branch-retry -> fix/principal-review-behind-branch-retry
      (refusing to allow a GitHub App to create or update workflow
      `.github/workflows/principal-review.yml` without `workflows` permission)
    

    This is GitHub's own hard-coded protection on GitHub App-authored pushes to .github/workflows/** — it requires the App to hold an explicit workflows permission grant at the installation level, separate from contents/pull-requests. Per docs/agent-team-pipeline.md, libravault-pipeline-bot is installed with "read/write on code, issues, and pull requests" only — no workflows permission — so this isn't something either credential available to this pipeline can do, regardless of agent-policy.yml's risk:high force-classification (that only governs merge routing after a PR exists; it doesn't help me get a PR opened in the first place here). The three prior PRs that touched workflow files (#239, #240, #242) were all authored by the human account libravault-xyz, not this pipeline — consistent with this being a real, standing gap rather than something specific to this change.

    I have the fix fully written and verified locally; a human just needs to actually push it. Here's the diff, ready to apply directly (git apply from repo root, or by hand):

    --- a/.github/workflows/principal-review.yml
    +++ b/.github/workflows/principal-review.yml
    @@ -370,10 +370,57 @@ jobs:
                 exit 0
               fi
     
    -          if ! gh pr merge "$PR_NUMBER" --squash --delete-branch=false; then
    -            echo "gh pr merge failed (conflict, branch protection, or stale approval) — see the log above for the exact error." >&2
    +          # dev routinely advances between principal-review approval and
    +          # this merge call (concurrent pipeline throughput — see issue
    +          # #246), which makes a plain `gh pr merge` fail transiently with
    +          # "head branch is not up to date with base branch". Check
    +          # mergeStateStatus before attempting the merge so that race can
    +          # resolve itself (server-side update-branch + retry) instead of
    +          # falling straight through to status:needs-human-merge like a
    +          # real conflict would. Bounded so a genuinely stuck PR (real
    +          # DIRTY conflict, branch protection failure, etc.) still
    +          # terminates and routes to a human rather than spinning forever.
    +          MAX_MERGE_ATTEMPTS=5
    +          MERGE_OK=false
    +          attempt=1
    +          while [ "$attempt" -le "$MAX_MERGE_ATTEMPTS" ]; do
    +            MERGE_STATE=$(gh pr view "$PR_NUMBER" --json mergeStateStatus -q .mergeStateStatus)
    +            echo "mergeStateStatus (attempt $attempt/$MAX_MERGE_ATTEMPTS): $MERGE_STATE"
    +
    +            if [ "$MERGE_STATE" = "DIRTY" ]; then
    +              echo "PR has a real merge conflict (DIRTY) — not retrying." >&2
    +              break
    +            fi
    +
    +            if [ "$MERGE_STATE" = "UNKNOWN" ]; then
    +              echo "GitHub is still computing mergeability (UNKNOWN) — waiting before re-check." >&2
    +              sleep 15
    +              attempt=$((attempt + 1))
    +              continue
    +            fi
    +
    +            if [ "$MERGE_STATE" = "BEHIND" ]; then
    +              echo "Branch is BEHIND dev — requesting a server-side update-branch." >&2
    +              gh api -X PUT "repos/${{ github.repository }}/pulls/$PR_NUMBER/update-branch" >/dev/null 2>&1 || true
    +              sleep 15
    +              attempt=$((attempt + 1))
    +              continue
    +            fi
    +
    +            if gh pr merge "$PR_NUMBER" --squash --delete-branch=false; then
    +              MERGE_OK=true
    +              break
    +            fi
    +
    +            echo "gh pr merge attempt $attempt failed (mergeStateStatus was $MERGE_STATE) — see the log above." >&2
    +            attempt=$((attempt + 1))
    +            sleep 10
    +          done
    +
    +          if [ "$MERGE_OK" != "true" ]; then
    +            echo "gh pr merge did not succeed within $MAX_MERGE_ATTEMPTS attempts (conflict, branch protection, or stale approval) — see the log above for the exact error." >&2
                 gh pr edit "$PR_NUMBER" --remove-label status:approved-auto-merge --add-label status:needs-human-merge
    -            gh pr comment "$PR_NUMBER" --body "Auto-merge failed (see the workflow run log for the exact error) — routed to a human instead of leaving this stuck on status:approved-auto-merge."
    +            gh pr comment "$PR_NUMBER" --body "Auto-merge failed after retrying stale-branch races up to $MAX_MERGE_ATTEMPTS times (see the workflow run log for the exact error) — routed to a human instead of leaving this stuck on status:approved-auto-merge."
                 exit 0
               fi
               echo "Merged #$PR_NUMBER."

    Beyond just this one issue: if workflow files are meant to stay in-pipeline-scope going forward (several past ones clearly have been, just always by a human pushing on the pipeline's behalf), libravault-pipeline-bot's GitHub App installation would need the workflows permission added — that's an App-settings change only an org admin can make, out of scope for me to request via API.

    Routing to status:needs-info since I can't get a PR open.

  4. added
    status:needs-info
    and removed
    status:ready-for-dev
    status:in-progress
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugstatus:needs-info

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions