Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 102 additions & 25 deletions scripts/dev-lead-fix-reviews.sh
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,50 @@ fetch_pr_context() {
# (e.g., review-changes in dry-run where the PR API call is skipped).
CI_STATUS_JSON="[]"
if [ -n "${HEAD_SHA:-}" ]; then
# Two-stage check-run dedup:
# Stage 1: group by (name, app.id, check_suite.id) and keep the highest-id
# run per suite. check_suite discriminates distinct workflow runs, so two
# workflows that happen to share a job name are kept as separate entries.
# id is always present and monotonically increasing, so it reliably picks
# the newest run even when started_at is absent (e.g. queued runs).
# Stage 2: drop cancelled/timed_out runs when a newer run (higher id, same
# name+app) has a terminal non-cancelled conclusion. This collapses a
# concurrency-cancelled run from an earlier suite once its replacement
# succeeds, without hiding a genuinely failing check from a distinct
# workflow. A lone cancelled/timed_out run (no newer replacement) still
# counts as a Tier-1 blocker (issue #461).
#
# Stage 2 deliberately matches on (name, app) ACROSS suites — wider than
# Stage 1's key. The asymmetry is load-bearing: a concurrency-cancelled
# run is superseded by a run from a *different* event, which always lives
# in a *different* check suite (the PR #453 incident shape), so requiring
# suite equality here would never drop anything and would reintroduce the
# endless 30-minute retry loop. The check-runs API exposes no workflow
# identity, so a cancelled check from a sibling workflow sharing a
# name+app with a newer success is also dropped — accepted trade-off:
# GitHub's own required-check gate keys on the latest same-named run
# (PR #453 merged with stale cancelled `review` runs still on its head
# SHA), so such a PR is not actually merge-blocked. Failures are exempt
# from Stage 2 (conservative: a real failure from a sibling workflow
# stays visible to the agent even when GitHub would let the merge pass).
if ! CI_STATUS_JSON=$(gh api --paginate "repos/${REPO}/commits/${HEAD_SHA}/check-runs?per_page=100" \
2>/dev/null \
| jq -s '[.[].check_runs[]? | {name:.name, status:.status, conclusion:.conclusion, details_url:.details_url}]' \
| jq -s '[.[].check_runs[]?]
| group_by([.name, (.app.id // null), (.check_suite.id // null)])
| map(sort_by([.id // 0]) | last)
| . as $runs
| map(select(
(.conclusion != "cancelled" and .conclusion != "timed_out")
or (. as $r | ($runs | any(
.name == $r.name
and (.app.id // null) == ($r.app.id // null)
and (.id // 0) > ($r.id // 0)
Comment thread
don-petry marked this conversation as resolved.
Resolved
and .conclusion != null
and .conclusion != "cancelled"
and .conclusion != "timed_out"
)) | not)
))
| map({name:.name, status:.status, conclusion:.conclusion, details_url:.details_url})' \
2>/dev/null); then
echo "::error::fetch_pr_context: failed to fetch CI check-runs for ${HEAD_SHA} — cannot assess PR state" >&2
return 1
Expand Down Expand Up @@ -682,8 +723,26 @@ has_reviews_rate_limited_marker() {
# For retryable intents (fix-reviews, review-changes, rebase), the cron will re-dispatch.
# For non-retryable intents (on-mention, fix-bot-comment), asks the user to re-trigger
# since USER_INSTRUCTION/COMMENT_BODY cannot be reconstructed at retry time.
#
# $2 (reason) selects the user-facing wording:
# rate-limit (default) — all AI engines genuinely rate-limited (engine exit 2)
# blocked — engine ran fine but the PR still has hard blockers
# (failing/cancelled checks or CHANGES_REQUESTED reviews);
# schedules a 30-minute backoff retry (issue #461)
# Both reasons post the same machine-readable `status=rate-limited` marker token —
# dev-lead-retry.sh keys its re-dispatch scan on that string — only the visible
# text differs, so users are no longer told "rate-limited" when the real cause
# is PR blockers.
post_reviews_rate_limited() {
local intent="$1"
local reason="${2:-rate-limit}"

# The blocked path owns its backoff: a fixed 30-minute reset so the retry cron
# backs off instead of re-dispatching immediately. The rate-limit path's reset
# is parsed from engine output (parse_reset_time) before this function is called.
if [ "$reason" = "blocked" ]; then
printf '%s' "$(date -u -d '+30 minutes' +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || true)" > /tmp/dev-lead-rate-limit-reset
fi

# Expire any stale terminal markers (applied/no-changes/failed) for this SHA+intent
# so the retry cron is not masked by a prior terminal that predates the current blocker.
Expand Down Expand Up @@ -725,32 +784,45 @@ post_reviews_rate_limited() {
sha_detail=" sha=${HEAD_SHA}"
fi

local marker="${REVIEWS_MARKER_PREFIX}${PR_NUMBER}${sha_detail} intent=${intent} status=rate-limited${reset_detail} -->"
# `reason=` is informational (visible-text selection + marker forensics); the
# retry cron and marker dedup patterns match on `status=rate-limited` and are
# unaffected by the extra field.
local marker="${REVIEWS_MARKER_PREFIX}${PR_NUMBER}${sha_detail} intent=${intent} status=rate-limited reason=${reason}${reset_detail} -->"

# Retry message depends on whether the intent can be re-dispatched automatically
local retry_msg
case "$intent" in
fix-reviews|review-changes|rebase)
retry_msg="The retry cron will re-attempt automatically."
;;
on-mention|fix-bot-comment)
retry_msg="Please re-trigger manually (re-mention \`@dev-lead\`) when the rate limit clears — the original request cannot be reconstructed automatically."
;;
*)
retry_msg="Manual re-trigger may be required."
;;
esac
if [ -n "$reset_time" ]; then
retry_msg="${retry_msg} Rate limit resets at: \`${reset_time}\`"
# Retry message depends on the reason and on whether the intent can be
# re-dispatched automatically.
local heading retry_msg
if [ "$reason" = "blocked" ]; then
heading="## Dev-Lead — waiting on PR blockers (intent: ${intent})"
retry_msg="No changes were committed, but the PR still has blocking checks or reviews (failing or cancelled checks, or changes-requested reviews). The retry cron will re-attempt automatically."
if [ -n "$reset_time" ]; then
retry_msg="${retry_msg} Next attempt after: \`${reset_time}\`"
fi
else
heading="## Dev-Lead — rate-limited (intent: ${intent})"
case "$intent" in
fix-reviews|review-changes|rebase)
retry_msg="The retry cron will re-attempt automatically."
;;
on-mention|fix-bot-comment)
retry_msg="Please re-trigger manually (re-mention \`@dev-lead\`) when the rate limit clears — the original request cannot be reconstructed automatically."
;;
*)
retry_msg="Manual re-trigger may be required."
;;
esac
if [ -n "$reset_time" ]; then
retry_msg="${retry_msg} Rate limit resets at: \`${reset_time}\`"
fi
fi

local marker_body="${marker}
## Dev-Lead — rate-limited (intent: ${intent})
${heading}
**PR:** #${PR_NUMBER}
${retry_msg}"

if [ "$DEV_LEAD_DRY_RUN" = "true" ]; then
echo "[dry-run] would post rate-limited marker for intent=${intent}"
echo "[dry-run] would post rate-limited marker for intent=${intent} reason=${reason}"
echo "$marker_body"
else
# Post the new marker FIRST, then remove old marker(s) only after the replacement
Expand All @@ -774,11 +846,18 @@ ${retry_msg}"
local actor_mention=""
[ -n "${ACTOR:-}" ] && actor_mention="@${ACTOR} "
local reset_display="${reset_time:-unknown}"
local ack_body="> [!NOTE]
local ack_body
if [ "$reason" = "blocked" ]; then
ack_body="> [!NOTE]
> ${actor_mention}I reviewed this PR and no code changes were needed, but it still has blocking checks or reviews (failing or cancelled checks, or changes-requested reviews), so I cannot mark it done yet. I'll re-check automatically.
> Next attempt after: \`${reset_display}\`"
else
ack_body="> [!NOTE]
> ${actor_mention}I received your request but all AI engines are currently rate-limited. I'll retry automatically once the rate limit clears.
> Rate limit resets at: \`${reset_display}\`"
fi
if [ "$DEV_LEAD_DRY_RUN" = "true" ]; then
echo "[dry-run] would post user-visible rate-limit acknowledgment"
echo "[dry-run] would post user-visible ${reason} acknowledgment"
echo "$ack_body"
else
gh pr comment "$PR_NUMBER" --repo "$REPO" --body "$ack_body"
Expand Down Expand Up @@ -851,8 +930,7 @@ case "$INTENT_TYPE" in
notify_coderabbit_resolve
if has_hard_blockers; then
echo "::warning::Tier-1 blockers still present (failing CI or CHANGES_REQUESTED reviews) — posting retry marker with backoff"
printf '%s' "$(date -u -d '+30 minutes' +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || true)" > /tmp/dev-lead-rate-limit-reset
post_reviews_rate_limited "fix-reviews"
post_reviews_rate_limited "fix-reviews" "blocked"
elif has_tier1_blockers; then
echo "::notice::Unresolved bot review threads remain — not posting no-changes terminal to allow future retries"
else
Expand Down Expand Up @@ -948,8 +1026,7 @@ case "$INTENT_TYPE" in
notify_coderabbit_resolve
if has_hard_blockers; then
echo "::warning::Tier-1 blockers still present (failing CI or CHANGES_REQUESTED reviews) — posting retry marker with backoff"
printf '%s' "$(date -u -d '+30 minutes' +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || true)" > /tmp/dev-lead-rate-limit-reset
post_reviews_rate_limited "review-changes"
post_reviews_rate_limited "review-changes" "blocked"
elif has_tier1_blockers; then
echo "::notice::Unresolved bot review threads remain — not posting no-changes terminal to allow future retries"
else
Expand Down
Loading
Loading