Repository navigation
fix(ci): self-heal stale branch during principal-review auto-merge - #247
Conversation
"Auto-merge if approved" treated any gh pr merge failure the same way -- conflict, branch protection, or a merely-stale branch all fell through to status:needs-human-merge with a generic error message. In a busy pipeline, dev routinely advances between approval and the actual merge call (other PRs from the same pipeline landing concurrently), which makes gh pr merge fail on "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-hit manually merging PR #239 (issue #246): the branch went BEHIND twice within ~15 minutes from ordinary pipeline throughput, each requiring a manual git-merge-and-push before gh pr merge would succeed. Before each merge attempt, check mergeStateStatus: - BEHIND -> request GitHub's server-side "Update branch" (PUT /pulls/{n}/update-branch) and retry, bounded. - DIRTY (real conflict) -> don't retry, route to status:needs-human-merge immediately, same as before. - UNKNOWN (mergeability still computing) -> brief bounded wait, then re-check. Bounded outer retry (5 attempts) so a burst of concurrent merges doesn't defeat auto-merge, while a genuinely stuck/failing merge still terminates and routes to a human instead of spinning indefinitely.
|
This PR was opened outside the automated pipeline (author isn't |
QA verification — PR #247 vs issue #246Scope: single-file change, Acceptance criteria (from #246)
Gap found — real, reproduced
I reproduced this directly: a mocked This is inconsistent with this same file's own established pattern for this exact risk: the CI-wait loop a few lines above (line 326) guards its This also means the PR description's claim of having "manually traced all Everything else — retry bounds, DIRTY/BEHIND/UNKNOWN handling, kill-switch placement, YAML/shellcheck validity — checks out. QA verdict: FAIL |
| QA failed its first round — routing back to the dev agent for one automated retry. |
Fix for QA's gap — blocked on push permissions, routing to a humanQA's finding is correct and I've written the fix, but I can't get it onto this branch: pushing to This is GitHub's own hard-coded protection on GitHub App-authored pushes to What QA found
The fix (verified, ready to apply)Same guard pattern already used a few lines above for the for _ in $(seq 1 12); do
- MERGE_STATE=$(gh pr view "$PR_NUMBER" --json mergeStateStatus -q .mergeStateStatus)
+ # Guarded against transient `gh pr view` failures (API
+ # error/timeout/rate-limit) — under `set -euo pipefail`, an
+ # unguarded command substitution here would kill the whole
+ # step before ever reaching the DIRTY/needs-human-merge
+ # fallback below, leaving the PR silently stuck on
+ # status:approved-auto-merge with no comment. Falling into
+ # UNKNOWN routes it through the existing bounded
+ # wait-and-recheck branch instead of crashing.
+ MERGE_STATE=$(gh pr view "$PR_NUMBER" --json mergeStateStatus -q .mergeStateStatus 2>/dev/null || echo "UNKNOWN")
case "$MERGE_STATE" in
BEHIND)
echo "Branch is behind dev (attempt $attempt/$MAX_MERGE_ATTEMPTS) — requesting GitHub's server-side update-branch." >&2Re-validated after the change:
I have this committed locally ( Routing to |
📱 iOS Build Status: PASSED✅ iOS Simulator Build & Unit Tests - success iOS UI tests run in a separate, informational job and are not reflected above — check "iOS UI Tests (informational)" for those. |
QA on PR #247 (issue #246) found a real gap: MERGE_STATE=$(gh pr view ... -q .mergeStateStatus) is an unguarded command substitution under set -euo pipefail. A transient gh API failure there (timeout, rate-limit) kills the whole step before it ever reaches the DIRTY/status:needs-human-merge fallback, leaving the PR silently stuck on status:approved-auto-merge with no comment -- ironic given this step exists specifically to add resilience against transient failures. Guard it the same way the CI-wait loop above already guards its own gh pr checks call (2>/dev/null || echo "pending"): a failed lookup now falls into the existing bounded UNKNOWN wait-and-recheck branch instead of crashing the step. The dev agent diagnosed and wrote this exact fix during PR #247's automated revise round but couldn't push it -- GitHub blocks App tokens from pushing to .github/workflows/** without an explicit `workflows` permission grant this pipeline's installation doesn't have (same standing gap noted in issue #246's history). Applying it manually as the human it routed to.
|
Pushed the guard fix the dev agent diagnosed and wrote but couldn't push (App token lacks |
📱 iOS Build Status: PASSED✅ iOS Simulator Build & Unit Tests - success iOS UI tests run in a separate, informational job and are not reflected above — check "iOS UI Tests (informational)" for those. |
QA verification — PR #247 vs issue #246Checked out branch, ran real commands (not just read the diff):
Acceptance criteria (issue #246):
Non-blocking observation: the new No regressions found in the CI-wait loop, kill-switch re-checks, or the final merge/labeling logic. QA verdict: PASS |
Principal review: PR #247Verdict: human-merge (routing purely on Risk classification
Verification performed (not just reading the diff)
Minor, non-blockingThe PR description doesn't explicitly use the words "sensitive path" / "risk:high" the way Bottom lineNo CONFIRMED findings — the fix is correct, bounded, and consistent with the precedent set by #239. This routes to a human purely because it touches |
What
The "Auto-merge if approved" step in principal-review.yml treated any
gh pr mergefailure identically — conflict, branch protection, or a merely-stale branch all fell straight through tostatus:needs-human-merge. In a busy pipeline,devroutinely advances between approval and the actual merge call, which makesgh pr mergefail on "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 (risk:high, so it went through human-merge, but the underlying race is identical): the branch went
BEHINDtwice within ~15 minutes from ordinary pipeline throughput (issues #238/#240/#242/#243 all landing ondevin that window), each requiring a manualgit merge origin/dev && git pushbeforegh pr mergewould succeed. An auto-merge-approved (risk:low) PR hitting the same race today would fall through tostatus:needs-human-mergefor no real reason.Fix
Before each merge attempt, check
mergeStateStatus:BEHIND→ request GitHub's server-side "Update branch" (PUT /pulls/{n}/update-branch) and retry, bounded.DIRTY(real conflict) → don't retry, route tostatus:needs-human-mergeimmediately, same as before.UNKNOWN(mergeability still computing) → brief bounded wait, then re-check.Bounded outer retry (5 attempts, each with its own bounded inner wait for staleness to resolve) so a burst of concurrent merges doesn't defeat auto-merge, while a genuinely stuck/failing merge still terminates and routes to a human rather than spinning indefinitely.
Closes #246
Testing
Same situation as #239: this is inline
run:bash in workflow YAML, no standalone script to unit-test against (no.bats/shellspec in the repo). Validated:python3 -c "import yaml; yaml.safe_load(...)"— parses cleanly.run:block, stripped${{ }}expressions, ranbash -n(no syntax errors) andshellcheck(only the same expected false positives from expression-stripping seen on fix(ci): route dev-agent triage failures to needs-info when a PR was found #239's review — no real findings).mergeStateStatusoutcomes (CLEAN/UNSTABLE, BEHIND, DIRTY, UNKNOWN, and a merge that fails for an unrelated reason) through the retry loop.No live pipeline run to verify against without a real auto-merge-approved PR racing a concurrent
devpush — this can't be manufactured deterministically in a one-off test.