Ci poll

From d-o-hub/rust-self-learning-memory · View interactive version

Ci poll is an AI agent skill from the GitHub repository d-o-hub/rust-self-learning-memory. Poll GitHub CI status with exponential backoff until all checks complete. Use after pushing to a PR branch to monitor CI until merge-ready or failure. Prefer gh pr checks --watch and gh run watch over bare sleep loops.

How to use this skill

Skill file

CI Poll Skill

Poll GitHub Actions CI status until all checks resolve. Prefer typed gh waiters from the GitHub CLI manual over manual sleep loops.

When to Use

Preferred: `gh pr checks --watch` (R-H3)

# Required checks only; blocks until complete.
# Exit 0 = the *listed* rows finished — NOT proof of merge-readiness:
#   observed exit 0 with 12 of 21 rows still pending, because --required
#   prints a subset and a ruleset change shrinks it (LESSON-072).
# Exit 8 = still pending (when not using --watch)
# Non-zero (other) = failure
gh pr checks <PR> --required --watch --interval 30

# Never conclude from the exit code alone — re-read the rollup:
gh pr view <PR> --json mergeStateStatus,statusCheckRollup \
  -q '"state=" + .mergeStateStatus + " pending=" +
      ([.statusCheckRollup[] | select(.status=="IN_PROGRESS" or .status=="QUEUED")] | length | tostring)'

# Structured one-shot status
gh pr checks <PR> --required --json name,state,bucket,workflow,link

| bucket (JSON) | Meaning |

|-----------------|---------|

| pass | Check succeeded |

| fail | Check failed |

| pending | Still running / queued |

| skipping | Skipped (often OK) |

| cancel | Cancelled — investigate / re-run |

Cancelled ≠ failed. gh pr checks prints CANCELLED rows as fail with a duration, which reads as a genuine failure (LESSON-041). Classify every suspect row through the jobs API before reporting it or starting a diagnosis:

gh api repos/{owner}/{repo}/actions/jobs/<job_id> --jq '.status, .conclusion'
# completed/cancelled => not a code failure;  completed/failure => real failure

Never cancel runs of a PR you intend to merge: a cancelled required check keeps mergeStateStatus away from CLEAN, so the merge gate refuses. Re-trigger with a push or gh pr update-branch instead.

Preferred: `gh run watch` for a single workflow

# List runs for this branch / commit
gh run list --branch "$(git branch --show-current)" --limit 5 \
  --json databaseId,name,status,conclusion,url

# Block until one run finishes
gh run watch <run_id>

# Diagnose failures
gh run view <run_id> --log-failed
gh run rerun <run_id> --failed

Quick status (no watch)

gh pr view <PR> --json mergeStateStatus,statusCheckRollup,mergeable \
  --jq '{state: .mergeStateStatus, mergeable: .mergeable, pending: [.statusCheckRollup[]? | select(.status != "COMPLETED") | .name], failing: [.statusCheckRollup[]? | select(.status == "COMPLETED" and .conclusion != "SUCCESS" and .conclusion != "SKIPPED") | {name: .name, conclusion: .conclusion}]}'

Poll the current head (strict up-to-date checks)

Ruleset main-protection sets strict_required_status_checks_policy=true: one merge onto main re-queues every required run on every other open branch (LESSON-038, LESSON-050). While a queue drains the head SHA is ephemeral — a SHA captured at the start of a wait, or pinned in a doc or note, goes stale within minutes.

Re-read the head at the START of every polling cycle:

gh pr view <PR> --json headRefOid,mergeStateStatus \
  --jq '{sha: .headRefOid, state: .mergeStateStatus}'

Polling a superseded SHA creates a permanent "still red" illusion: the old run never improves and the current one is never looked at.

statusCheckRollup keeps every check run for the head SHA, so a re-run leaves a stale FAILURE row sitting next to a newer SUCCESS (LESSON-052). Reduce to latest run per check name on that SHA before reporting anything as failed — and make the reduction answer three questions, not one:

# set -o pipefail: without it the pipeline exits with jq's status, so a 422 from
# gh is invisible and a poll that fetched nothing still "succeeded".
set -o pipefail
# gh needs --json before -q; a bare `-q` prints an empty SHA, the URL 422s, and
# the poll reads nothing at all.
HEAD=$(gh pr view <PR> --json headRefOid -q .headRefOid)
gh api "repos/{owner}/{repo}/commits/$HEAD/check-runs?per_page=100" --paginate \
  | jq -sr '
      [.[] | (.check_runs // [])[]]                   as $rows
      | ($rows | group_by(.name) | map(max_by(.id))) as $latest
      | if ($latest | length) == 0 then
          "NO_ROWS - a poll that saw nothing is a failed poll, not a green one"
        else
          ($latest | map(select(.status == "completed"
                                and ((.conclusion | IN("success","skipped","neutral")) | not))
                               | "\(.name) => \(.conclusion)")) as $bad
        | ($latest | map(select(.status != "completed")) | length) as $pending
        | "names=\($latest | length) pending=\($pending) failed=\($bad | length)",
          ($bad[] | "  FAIL " + .)
        end'

Four details in that command are load-bearing, each one measured rather than assumed:

Status and conclusion vocabulary

From the status-checks reference:

| Meaning | Values |

|---------|--------|

| Not terminal — keep waiting | queued, pending, in_progress, requested, waiting, expected |

| Terminal success | success; neutral and skipped count as success for dependent checks |

| Terminal non-success | failure, cancelled, timed_out, stale, action_required |

stale means GitHub marked the run stale because it took too long — not a code failure, and not green. Re-trigger and re-poll.

A skipped job reports its status as "Success" and does not block a required check (official note, verbatim: "A job that is skipped will report its status as 'Success'. It will not prevent a pull request from merging, even if it is a required check"). So pending=0 failed=0 proves the checks reported, never that they ran. When the work inside a job is what you depend on, read the step:

gh run view <run_id> --json jobs \
  -q '.jobs[].steps[] | select(.name == "<the step that performs the effect>") | .conclusion'

duration_ms: 0 on that step is a failed operation wearing a green job (LESSON-053).

Prefer gh pr checks --watch / gh run watch; use this scripted reduction only when their output is ambiguous.

Batch pushes — a polling cost, not tidiness. Every branch push restarts the entire required suite (LESSON-049), so N single-file edits cost N full CI cycles; land docs/skills edits as one push. PR body edits are free: release-drift.yml triggers on opened, synchronize, reopened, labeled, unlabeled — not edited.

Fallback polling (only if watch is unavailable)

CI rarely completes in under 60s. Use bounded sleep + re-check (max 5 rounds):

| Round | Sleep |

|-------|-------|

| 1 | 60s |

| 2 | 120s |

| 3 | 300s |

| 4+ | 600s |

# 0. Every round starts here — last round's SHA may already be superseded
gh pr view <PR> --json headRefOid,mergeStateStatus --jq '.headRefOid'
sleep 60
gh pr checks <PR> --required --json name,state,bucket

A changed headRefOid (or mergeStateStatus flipping to BEHIND) means the suite re-queued under strict up-to-date checks — not that CI regressed. Re-run the reduction above on the new SHA before reporting.

Interpret merge readiness

| mergeStateStatus | Meaning | Action |

|--------------------|---------|--------|

| CLEAN | Checks OK, no conflicts | Ready to merge (still address PR comments) |

| BEHIND | Branch behind base | gh pr update-branch <PR> |

| BLOCKED | Required checks pending | Keep watching |

| UNSTABLE | Non-required checks failing | Inspect whether blocking |

| DIRTY | Merge conflicts | Resolve conflicts |

Terminal conditions (stop)

  1. Never terminal, always fix first: an empty reduction, a 422, or a headRefOid that came back empty is a broken poll, not a green one. Repair the query before reporting any state — silence has no conclusion.
  2. mergeStateStatus = CLEAN and required checks pass on the current headRefOid, with pending=0 in the reduction
  3. Required check bucket=fail → confirm it is fail and not cancel, and that the row survived latest-run-per-name reduction on the current head, then fix and re-push
  4. Max 5 rounds / policy timeout → report state
  5. DIRTY → stop and fix conflicts

Branch update (prefer typed CLI)

# Preferred (R-H4)
gh pr update-branch <PR>

# Fallback only if gh version lacks update-branch
gh api repos/{owner}/{repo}/pulls/<PR>/update-branch -X PUT -f update_method=merge

Common pitfalls

Codacy

codacy pull-request gh <owner> <repo> <PR> --reanalyze-and-wait 2>&1

See also

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.