From 97dcc1a78b89b796bf743318919df92914b1c010 Mon Sep 17 00:00:00 2001 From: hugocasa Date: Wed, 25 Nov 2026 16:51:57 +0100 Subject: [PATCH] chore: scope the PR review waiter to its own PR's runs (#12659) * fix(pr-skill): scope the review waiter to its own PR's runs The waiter waited on every PR Review Commands run after its trigger, from any PR, so one stuck reviewer elsewhere held every round to the 45-minute deadline. Match runs on the PR's title, and add waiting rules to the skill. Co-Authored-By: Claude Opus 5.5 * fix(pr-skill): keep a seen run from reading as missing, scope the manual check Co-Authored-By: Claude Opus 5.5 * chore(pr-skill): keep the trigger-time comment next to its line Co-Authored-By: Claude Opus 5.5 * fix(pr-skill): stop the waiter once verdicts are in, at 20 min name stuck jobs Co-Authored-By: Claude Opus 5.5 * chore(pr-skill): match verdicts on the bot author, refresh round durations Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .agents/skills/pr/SKILL.md | 7 ++- .agents/skills/pr/review-round.sh | 73 ++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/.agents/skills/pr/SKILL.md b/.agents/skills/pr/SKILL.md index f2a9ec8dad..e83dca2b84 100644 --- a/.agents/skills/pr/SKILL.md +++ b/.agents/skills/pr/SKILL.md @@ -135,7 +135,7 @@ sufficient — see "Flip, or ask first" below. The one standing exception is an leave that PR in draft (usually so it can be tested first) — honour it for that PR, and don't carry it over to the next one. -1. **Trigger a round and wait for it**: launch the waiter as a background Bash task (a round takes 10–30 min; you are woken when it exits — do not stop the session or poll in the foreground while it runs): +1. **Trigger a round and wait for it**: launch the waiter as a background Bash task (a round takes about 10 min, 20 at most; you are woken when it exits — do not stop the session or poll in the foreground while it runs): ```bash bash .agents/skills/pr/review-round.sh @@ -143,6 +143,11 @@ carry it over to the next one. It comments `/review` on the PR — which runs the Codex, Claude and Pi CI reviewers even on a draft — waits for the spawned `PR Review Commands` workflow run(s) to complete, then prints one verdict line per reviewer and saves the full review comments to files. + Waiting rules — a session that breaks one sits idle on results the user can already see: + - **Use the waiter, not a loop of your own.** It always exits: as soon as the Codex, Claude and Pi verdicts are posted, once the round's run completes (failed jobs included), at a 20-minute deadline with whatever was posted, or non-zero when no run appeared or GitHub kept erroring — treat a non-zero exit as "look yourself", not "no reviews". A hand-written loop waiting for "a comment from each reviewer" never ends when one reviewer dies (capacity errors, cancelled jobs), since a dead job posts nothing. + - **If your harness does not wake you when a background command exits, run the waiter in the foreground** with a timeout of at least 25 minutes. Backgrounding it and ending your turn then means nobody ever reads the result. + - **Act on a missing reviewer immediately.** Reviewers that never start or hang are common. The waiter names each reviewer with no review and the jobs that did not finish cleanly; re-trigger each missing one at once with a `/codex`, `/claude` or `/pi` PR comment and run the waiter again, rather than waiting longer. + `/review` (and `/codex`) are **idempotent per head SHA**: if a running or successful review already covers the current head, they skip that agent and post nothing new — the waiter reads the existing verdict for that head, so a skipped agent is *not* a missing one. A cancelled/failed head run is re-run in place; a fresh run is launched only when nothing covers the head. So an unchanged-head re-review is a near no-op, not a new round — push a commit to get genuinely fresh reviews. 2. **Judge the round.** Codex is mandatory; Claude, Pi and cubic count whenever they posted. Every review starts with one of the three `REVIEW.md` verdicts: diff --git a/.agents/skills/pr/review-round.sh b/.agents/skills/pr/review-round.sh index abd94005f6..adc7c72214 100755 --- a/.agents/skills/pr/review-round.sh +++ b/.agents/skills/pr/review-round.sh @@ -7,19 +7,20 @@ # Comments `/review` on the PR (works on drafts), waits for the spawned # "PR Review Commands" workflow run(s) to complete, then prints one verdict # line per reviewer and saves each full review comment to a file. A round -# takes 10-30 minutes: run this in the background and act on its output when +# takes about 10 minutes, 20 at most: run this in the background and act on its output when # it exits, per the pr skill ("Review rounds"). set -euo pipefail REPO=${REPO:-$(gh repo view --json nameWithOwner --jq .nameWithOwner)} PR=${1:-$(gh pr view --json number --jq .number)} +PR_TITLE=$(gh pr view "$PR" --repo "$REPO" --json title --jq .title) # Timestamp of the trigger comment, straight from GitHub, so local clock skew # can't make the run/comment filters below miss part of the round. TRIGGER_TIME=$(gh api "repos/$REPO/issues/$PR/comments" -f body='/review' --jq .created_at) echo "Review round triggered on $REPO#$PR at $TRIGGER_TIME" -# Retry wrapper for one-off gh/API hiccups: a 45-minute wait must not die on +# Retry wrapper for one-off gh/API hiccups: a 20-minute wait must not die on # a single transient failure. retry() { local attempt @@ -31,21 +32,39 @@ retry() { } # The /review comment spawns one "PR Review Commands" run holding the -# claude/codex/pi jobs. Runs aren't linked to a PR, so wait on every run of -# that workflow created after the trigger: a concurrent round on another PR -# can only delay the answer, never truncate it. Every issue comment on any PR -# spawns a fast-completing parse run of the same workflow, so the round's own -# run may briefly lag the listing while unrelated runs already show completed: -# require the all-completed state to hold past a floor and across two -# consecutive polls before trusting it. -DEADLINE=$(( $(date +%s) + 45 * 60 )) +# claude/codex/pi jobs. Runs aren't linked to a PR, but an issue_comment run's +# display title is the PR's title: match on it, since waiting on every run +# after the trigger means a stuck reviewer on any other PR holds this round +# to the deadline. Every comment on this PR (the reviewers' own +# included) spawns a fast-completing parse run, so the round's own run may +# briefly lag the listing while those already show completed: require the +# all-completed state to hold past a floor and across two consecutive polls. +# +# Reviews land 6-10 minutes in. A reviewer that never starts or hangs is common +# and only a human re-trigger fixes it, so stop early: as soon as all three +# verdicts are posted, else at 20 minutes, naming the jobs still unfinished. +VERDICT_RE='(Good to merge|Mergeable, but should ideally address nits|Should address issues before merging)' +# Claude edits a placeholder comment in place, hence updated_at. +all_verdicts_in() { + gh api "repos/$REPO/issues/$PR/comments?per_page=100" --paginate 2>/dev/null \ + | jq -s -e --arg t "$TRIGGER_TIME" --arg re "$VERDICT_RE" ' + [.[][] | select(.updated_at > $t) | select(.body // "" | test($re))] as $c + | ($c | any(.user.login == "github-actions[bot]" and (.body | startswith("## Codex Review")))) + and ($c | any(.user.login == "github-actions[bot]" and (.body | startswith("## Pi Review")))) + and ($c | any(.user.login == "claude[bot]"))' >/dev/null +} +DEADLINE=$(( $(date +%s) + 20 * 60 )) NO_RUN_DEADLINE=$(( $(date +%s) + 5 * 60 )) MIN_WAIT_UNTIL=$(( $(date +%s) + 3 * 60 )) STABLE=0 +SEEN=0 +ALL_IN=0 +ROUND_RUNS='[]' FAILURES=0 while :; do if RUNS=$(gh run list --repo "$REPO" --workflow=pr-review-commands.yml \ - --created ">=$TRIGGER_TIME" --limit 100 --json status); then + --created ">=$TRIGGER_TIME" --limit 500 --json databaseId,status,displayTitle \ + --jq "[.[] | select(.displayTitle == $(jq -Rn --arg t "$PR_TITLE" '$t'))]"); then FAILURES=0 else FAILURES=$(( FAILURES + 1 )) @@ -58,6 +77,13 @@ while :; do continue fi TOTAL=$(jq length <<<"$RUNS") + # The limit applies before the title filter, so on a busy repo this PR's run + # can age out of the listing: once seen, never report it as missing. + if [ "$TOTAL" -gt 0 ]; then SEEN=1; ROUND_RUNS=$RUNS; fi + if all_verdicts_in; then + ALL_IN=1 + break + fi PENDING=$(jq '[.[] | select(.status != "completed")] | length' <<<"$RUNS") NOW=$(date +%s) if [ "$TOTAL" -gt 0 ] && [ "$PENDING" -eq 0 ] && [ "$NOW" -gt "$MIN_WAIT_UNTIL" ]; then @@ -68,12 +94,12 @@ while :; do else STABLE=0 fi - if [ "$TOTAL" -eq 0 ] && [ "$NOW" -gt "$NO_RUN_DEADLINE" ]; then + if [ "$SEEN" -eq 0 ] && [ "$NOW" -gt "$NO_RUN_DEADLINE" ]; then echo "ERROR: no 'PR Review Commands' run appeared within 5 minutes of the /review comment; check that the comment author has write access and the workflow is enabled." >&2 exit 1 fi if [ "$NOW" -gt "$DEADLINE" ]; then - echo "WARNING: review round still pending after 45 minutes; reporting whatever has been posted so far." >&2 + echo "WARNING: review round still pending after 20 minutes; reporting whatever has been posted so far." >&2 break fi sleep 60 @@ -100,7 +126,7 @@ head_run_state() { # A re-run-in-place review lands on a head-tied run that finishes after the fast # pr-review-commands run, so let those settle before reading verdicts. for wf in codex-pr-review.yml pi-pr-review.yml pr-ready-review.yml; do - while :; do + while [ "$ALL_IN" -eq 0 ]; do case "$(head_run_state "$wf")" in ""|"completed "*) break ;; *) if [ "$(date +%s)" -gt "$DEADLINE" ]; then break; fi; sleep 30 ;; @@ -121,8 +147,6 @@ REVIEWS_RAW=$(retry gh api "repos/$REPO/pulls/$PR/reviews?per_page=100" --pagina jq -s --arg t "$TRIGGER_TIME" '[.[][] | select((.submitted_at // "") > $t)]' \ <<<"$REVIEWS_RAW" > "$OUT_DIR/pr-reviews.json" -VERDICT_RE='(Good to merge|Mergeable, but should ideally address nits|Should address issues before merging)' - body_by_header() { # jq -r --arg h "$2" '[.[] | select(.body // "" | contains($h))] | last | .body // empty' "$1" } @@ -144,7 +168,11 @@ verdict_body() { # report() { # local name=$1 body=$2 verdict if [ -z "$body" ]; then - echo "$name: (no review posted for this head)" + if [ "$name" = cubic ]; then + echo "$name: (no review posted for this head)" + else + echo "$name: (no review posted for this head; re-trigger now with a '/$name' PR comment)" + fi return fi printf '%s\n' "$body" > "$OUT_DIR/$name.md" @@ -167,6 +195,17 @@ fi report cubic "$CUBIC_BODY" echo echo "Full round output: $OUT_DIR (comments.json, pr-reviews.json, one .md per reviewer)" +if [ "$ALL_IN" -eq 0 ]; then + UNFINISHED=$(for id in $(jq -r '.[].databaseId' <<<"$ROUND_RUNS"); do + gh run view "$id" --repo "$REPO" --json jobs --jq '.jobs[] + | select(.status != "completed" or (.conclusion | IN("success", "skipped") | not)) + | " \(.name): \(.status) \(.conclusion // "") since \(.startedAt // "-")"' 2>/dev/null || true + done) + if [ -n "$UNFINISHED" ]; then + echo "Review jobs that did not finish cleanly:" + printf '%s\n' "$UNFINISHED" + fi +fi if [ -z "$CODEX_BODY" ]; then echo "WARNING: no Codex verdict for $HEAD_SHA - its head run is not green (cancelled/failed/absent, not merely skipped-because-already-reviewed). Re-trigger with a '/codex' PR comment (re-runs the interrupted run in place, or launches one) and wait again." >&2 fi