Skip to content

ci-gate.sh: wait out an exhausted API budget, poll the run cheaply, keep the ref without a verdict - #554

Merged
SSFSKIM merged 2 commits into
masterfrom
ci-gate-ratelimit
Nov 10, 2026
Merged

SSFSKIM merged 2 commits into
masterfrom
ci-gate-ratelimit

Conversation

@SSFSKIM

@SSFSKIM SSFSKIM commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Why

Every session on the owner's account shares one 5,000-request/hour REST budget. It has run out five times in two days. When it did, await_run's gh run watch and gh run view both failed immediately, and after 20 failures (about 5 min) the script decided the run cannot be read. finish 1 then deleted the ci/ ref while the run was still going. That is what happened to pr553's landing gate (run 42802241165, 2026-11-10 11:02Z).

The watcher was also expensive in its own right. Between 11:22 and 11:31Z the budget drained at about 4,200/h with about 15 watchers running, and four of them were this script's gh run watch --interval 30 loops.

What changes

  • scripts/ci-gate-pace.sh, paced_gh (same approach as ci-kit's cli/pace.mjs, ci-gate: wait out an exhausted API budget; keep the ref when there is no verdict ci-kit#200). A failure where gh's stderr says API rate limit exceeded or secondary rate limit together with HTTP 403 or 429 no longer counts as a failed read. Instead the script:

    • reads GET /rate_limit, which costs nothing;
    • sleeps until the reset plus 5 s, or 60 s if some budget remains (a secondary limit) or the budget can't be read;
    • prints one line with the reset time;
    • reads the budget again on waking (the reset can move) and retries the call.

    The wait is bounded. If a wait would end more than 90 min into continuous exhaustion, it is not started, and the caller fails the way it does today. The run discovery after the push, the status poll, settle_run's reads, the job rerun, the attempt poll, both run downloads and --log-failed all go through it. The wait lines go to a descriptor of their own, so they reach the transcript even from a read whose stderr is redirected.

  • await_run polls cheaply. It reads only the run's status (gh api -i … --jq .status) and sends If-None-Match with the last ETag. An unchanged run answers 304, which costs no budget (gh exits 1 on a 304 after printing the headers, and the script reads the status line). It polls every 60 s for five polls, then every 120 s. The jobs are still read once, when the run completes, in settle_run. Failed reads are still bounded at 20, 15 s apart.

  • finish keeps the ref when there is no verdict. That covers three cases: the run can't be read, the call is interrupted while the run exists, and the spent budget hid the run during discovery. In each, the ref is left in place and the script prints kept ci/…, as this call ends without the run's verdict; delete it with: git -C … push origin --delete refs/heads/ci/…. A verdict (green or red) still removes the ref, and so does a push that never produced a run. --keep behaves as before.

Request rate per call (measured on run 42811586568, a static gate)

gh run watch --interval 30 (before) conditional status poll (after)
start 19 requests (run, workflow, graphql, run, workflow, jobs, plus annotations for each of 13 jobs) 1
steady 3 per 30 s (run, workflow, jobs) = 360/h 1 per 60 s for 5 min, then 1 per 120 s, and a 304 costs 0: ≤ 30/h, less while the run is unchanged
this ~4 min run ~44 3 counted (5 polls, 2 of them 304s)

Before: GH_DEBUG=api gh run watch 42811586568 --compact --interval 30 over 100 s made requests at t = 0–5 s (19), then 3 each at t = 35, 66 and 97 s. After: the script's polls, logged through a gh wrapper, got 200, 200, 304, 304, 200 (completed). Separately, 8 conditional reads left X-RateLimit-Used unchanged, while 8 plain reads raised it by 8. Both versions make the same reads once the run completes (attempt, jobs, flaky).

Tests

  • New scripts/ci-gate-pace.test.mjs (9 tests, fake gh, and a fake clock that only sleep moves). Covers:
    • the limit refused twice, then waited out until the reset;
    • a secondary limit;
    • a reset that moves;
    • an unreadable budget;
    • the 90-min bound, and later limited answers returning immediately;
    • a success restarting the bound;
    • a non-limit 403, 502 or 304 passed straight through;
    • a stale mark under a reused pid.
  • scripts/ci-gate.test.mjs gets 7 new end-to-end tests:
    • a poll refused twice, waited out, then the verdict, which removes the ref;
    • the conditional ETag polling and its 60/120 s cadence;
    • a red verdict removes the ref;
    • an unreadable run keeps the ref and prints the delete command;
    • an interrupt while the run exists cancels the run and keeps the ref;
    • a push with no run removes the ref;
    • a budget that hides the run past the bound keeps the ref.
  • Fakes in the preemptions, hosted and identity tests updated for the new status poll.

Locally: pace 9/9, preemptions 16/16, hosted 2/2, refusals 22/22, identity and pool-demand 28/28. ci-gate is 32/34: the 2 failures are the spec-import coverage tests, which need TypeScript from node_modules. They fail the same way on master in a worktree with no dependencies installed. shellcheck is clean apart from the existing SC2329 info on print_paths.

Verified with the script from this branch: scripts/ci-gate.sh "$PWD" --gates static --label cigate-rl → run 42811586568 success, removed ci/cigate-rl-5368fbf6.

…eep the ref without a verdict

Every session on the owner's account shares one 5,000-request/hour REST
budget. At 0, await_run's `gh run watch` and `gh run view` both failed at
once, 20 failures in about 5 min read as a run that cannot be read, and
finish deleted the ci/ ref while the run still ran (pr553's landing gate,
run 42802241165, 2026-11-10).

- scripts/ci-gate-pace.sh: paced_gh. A gh answer naming the rate limit
  (API rate limit exceeded, or a secondary rate limit, with HTTP 403 or
  429) is waited out: read GET /rate_limit (free), sleep to its reset
  plus 5 s, or 60 s when budget remains (a secondary limit), say so in
  one line with the reset, read the budget again on waking, retry. A
  wait that would end past 90 min of continuous exhaustion is not begun:
  the caller ends as on any failed read. Discovery, the status poll,
  settle_run's reads, the rerun, the reports' downloads and --log-failed
  read through it, as ci-kit's ci-gate does (ci-kit d9a854d).
- await_run polls the run's status alone with If-None-Match on the last
  ETag: an unchanged run answers 304, which costs no budget (measured:
  8 conditional reads left X-RateLimit-Used unchanged; 8 plain reads
  moved it 8), where gh run watch read the run, its jobs and each running
  job's annotations every 30 s. Every 60 s for five polls, then 120 s.
- finish keeps the ref when the call ends without a verdict while the run
  exists (unreadable, interrupted) or the spent budget hid it, and prints
  the delete command. A verdict, green or red, and a push no run came of
  still remove it.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T12:15:19.292816Z 5368fbf PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5368fbf6b3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread scripts/ci-gate-pace.sh Outdated
Comment on lines +39 to +40
if ((status == 0)); then
rm -f "$PACE_MARK"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clear the exhaustion marker after a successful conditional read

When a rate-limited status poll recovers with HTTP 304, gh still exits nonzero, so this condition leaves PACE_MARK intact even though await_run correctly treats that response as a successful read. Subsequent unchanged polls also leave the marker intact. If the shared budget is exhausted again before the run changes, pace_wait measures its 90-minute bound from the previous exhaustion, including the intervening healthy polling period, and can reject the new wait immediately. The gate then exits without a verdict after its failed-read retries instead of waiting out the fresh limit. Clear the exhaustion marker for accepted 304 responses as well as exit-zero responses.

Useful? React with 👍 / 👎.

…included

paced_gh cleared PACE_MARK only on exit 0, and gh exits 1 on a 304, so a
limit the status poll recovered from with a 304 left the mark set. A
later, separate limit then inherited the first one's start, could pass
the 90-minute bound at once, and 20 failed polls later the gate ended
without a verdict. Any answer that is not the limit, a failure or a 304,
now removes the mark. Regression test: limit, recovery by a 304, then a
separate limit, which waits under its own bound.
@SSFSKIM
SSFSKIM merged commit 14a2dd1 into master Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant