Skip to content

ci-gate: wait out an exhausted API budget; keep the ref when there is no verdict - #200

Merged
SSFSKIM merged 3 commits into
mainfrom
ci-gate-wait-out-ratelimit
Nov 10, 2026
Merged

SSFSKIM merged 3 commits into
mainfrom
ci-gate-wait-out-ratelimit

Conversation

@SSFSKIM

@SSFSKIM SSFSKIM commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Why

Every session on the account shares one 5,000-request/hour REST budget. At 0, ci-gate counted each 403 as a failed read. Discovery gave up with an exhausted-budget ERROR, the watch ran into maxUnreadable, and either way the ci/ ref was deleted while the run was still going. That happened five times in two days (friction entry parallel-gh-watchers-exhaust-owner-api-budget).

What changes

Waiting out the limit (cli/pace.mjs, pacer.read):

  • A 403 or 429 whose message names the rate limit is waited out, not counted as a failure.
  • ci-gate sleeps until the later of retry-after and, when 0 remain, x-ratelimit-reset, plus 5 s. If the answer gives neither, it waits a minute (GitHub's documented rule).
  • It then checks GET /rate_limit, which is not counted against the budget. If that still says 0, it sleeps again until the new reset: the published reset moves when the window refills and drains again.
  • Discovery, the watch and the failed gate's log fetch all read through it. Discovery's 5-minute deadline does not count the time spent waiting.
  • lib/github.mjs: GitHubError now carries the error response's remaining/reset/retry-after; adds isRateLimited and rateLimit().

Bound: 90 minutes of continuous exhaustion, measured as elapsed time on ci-gate's clock, never from the published reset. Each sleep is cut to what the bound has left. That covers one hourly window plus a reset that moves once. A successful read starts the bound over. Past it, ci-gate ends in ERROR … the GitHub API rate limit stayed exhausted for 90 min (resets <ISO>).

Stdout: each wait prints THROTTLE remaining=<n> until=<ISO>, the same keyword with a new field. I found no consumer that parses THROTTLE: MAWS's scripts/ci-gate.sh is its own bash watcher and never reads ci-kit's output. The healthy path is unchanged: no /rate_limit call and no new line.

The ref (cli/ci-gate.mjs):

  • Removed after GREEN or RED, and when no run appeared within the deadline. Such a push has nothing to rerun, and keeping its ref would let a late run start that no one is watching.
  • Kept after an ERROR or an interruption while a run exists, and as soon as the budget first hides the run from discovery (a run most likely exists, so a Ctrl-C during the wait keeps it too). One stderr note names the ref with its delete command: kept <ref>: … remove it with: git push origin --delete <ref>.
  • Kept refs are removed by hand. No janitor sweeps ci/* refs, and a kept ref is harmless: a branch nobody pushes to starts nothing. An automatic 24-hour sweep was built in this PR and dropped in review, because each round found another edge in it for a rare event. A server-side sweep is tracked in ci-gate: deferred edge cases (tech debt) #201.
  • The SIGINT handler is detached once the call has its result, so a late Ctrl-C cannot follow a verdict with ERROR interrupted.

Tests

  • test/pace.test.mjs (new): the healthy pass-through, waiting to the reset, retry-after beating the reset, a secondary limit, the 60 s default when the answer gives no time, a moved reset, a failed /rate_limit check, the 90-min bound (elapsed time, with each sleep cut to the bound), a reset stuck in the past, a reset far past the bound, the bound starting over after a successful read, onLimited firing before the wait, and non-limit errors being thrown at once.
  • test/discover.test.mjs: the old give-up test is replaced by waiting to the reset, the deadline not counting the wait, the bound giving an ERROR, and onHidden firing at the first 403.
  • test/watch.test.mjs: a rate-limited watch reads past maxUnreadable to GREEN; the bound gives an ERROR naming the rate limit.
  • test/github.test.mjs: the error's rate-limit fields, isRateLimited, rateLimit().

Verification (ci-gate on ci-kit)

  1. Started at budget 0 (reset 11:22:04Z, first head d9a854d): one 403, THROTTLE remaining=0 until=2026-11-10T11:22:04Z, sleep, one /rate_limit, then PASS test and GREEN (run 42806419239). 13 requests; ref removed.
  2. Healthy budget, final head 25d5e6a: PASS test, GREEN (run 42811688111), no THROTTLE line, ref removed. (The healthy path making no /rate_limit call was shown with CI_GATE_DEBUG=1 at d9a854d, run 42807148591; that path is unchanged since.)
  3. Ctrl-C after the run was found, 25d5e6a (run 42812287970): run cancelled, ERROR interrupted, exit 130, the keep note printed, ref still on the remote (git ls-remote). The printed command then removed it.

… no verdict

Every session on the account shares one 5,000-request/hour REST budget.
At 0, ci-gate counted each 403 as a failed read: discovery gave up with
an exhausted-budget ERROR and the watch hit maxUnreadable, and either way
the ci/ ref was deleted while the run still ran (five calls in two days).

A 403 or 429 that names the rate limit is now waited out (pace.mjs read):
sleep until the later of retry-after and, at 0 remaining, the published
reset (a minute when neither is given), plus 5 s; check GET /rate_limit,
which is not counted, and sleep again while it still says 0, since the
reset moves when the window refills and drains. Each wait prints
THROTTLE remaining=<n> until=<ISO>. Discovery, the watch and the failed
log's fetch read through it; discovery's 5-minute deadline does not count
the wait. After 90 min of continuous exhaustion, ERROR as before. The
healthy path makes no extra request and prints nothing new.

The ref is removed after GREEN or RED, and when no run appeared. After an
ERROR or an interruption while a run exists (or when the budget hid it),
it stays, recorded under the clone's git directory; a later ci-gate in
that clone deletes recorded refs older than 24 h, compare-and-delete.
@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-10T11:31:12.344058Z d9a854d 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: d9a854d5a8

ℹ️ 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 cli/pace.mjs Outdated
Comment on lines +49 to +50
if (until - exhaustedSince > LIMIT_WAIT_MS) {
throw new BudgetExhausted(`the GitHub API rate limit stayed exhausted for ${LIMIT_WAIT_MS / 60_000} min (resets ${iso(until)})`);

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 Enforce the exhaustion bound using elapsed time

If /rate_limit continues returning zero remaining with an unchanged reset timestamp that is now in the past, this condition never reaches the 90-minute bound: until stays fixed while the loop sleeps another minute indefinitely. With an injected clock and a constant exhausted response, the pacer was still polling after 99 minutes instead of throwing BudgetExhausted. Check elapsed time against exhaustedSince as well as the proposed resume time so an overdue reset cannot leave discovery or the watch hanging forever.

Useful? React with 👍 / 👎.

Comment thread cli/kept.mjs Outdated
Comment on lines +58 to +59
const removed = remove(entry);
rmSync(file, { force: true });

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 Retain ledger entries when remote deletion fails transiently

When a sweep encounters a network, authentication, or remote-server failure, deleteRef() in cli/git.mjs returns false, just as it does for a moved ref. Unconditionally deleting the ledger entry here permanently forgets the still-existing remote ref, so later calls cannot retry its cleanup; sweepOldRefs() also misleadingly reports that it is gone or moved. Since these automatically kept refs have no other janitor, retain the entry on an unconfirmed deletion failure, or distinguish a confirmed absent/moved ref from a failed removal before forgetting it.

Useful? React with 👍 / 👎.

- The 90-min bound is elapsed time on ci-gate's clock, not the published
  reset, and each sleep is cut to what the bound has left. A /rate_limit
  that kept saying 0 with a reset already past never tripped it.
- Discovery marks that a run may exist when the budget first hides it,
  before the wait (pacer.read onLimited), so a Ctrl-C during the hour's
  wait keeps the pushed ref instead of deleting it under a live run.
- The sweep reads each due ref's newest run first (one request, skipped
  while the budget is low) and leaves the ref while a run on it is queued
  or going: a rerun keeps the nonce commit, so compare-and-delete alone
  would delete the branch under it.
- A record is dropped only when the delete succeeds or ls-remote shows
  the ref gone or moved; a failed push is tried again by a later call.
@SSFSKIM

SSFSKIM commented Nov 10, 2026

Copy link
Copy Markdown
Owner Author

Review fixes in 796f336, each with a regression test:

  1. The 90-min bound is elapsed time on now(), not the published reset, and each sleep is cut to what the bound has left. Tests: a /rate_limit that keeps saying 0 with a reset already past gives up at exactly 90 min; a reset far past the bound is slept only until the bound.
  2. Discovery calls onHidden when the budget first hides the run, before the wait (pacer.read(fn, { onLimited })), so a Ctrl-C during the wait keeps the ref. Tests: onHidden fires at the first 403's time; onLimited fires before any sleep.
  3. Before deleting a kept ref, the sweep reads its newest run (latestRunOnBranch, one request, skipped while the budget is low) and leaves the ref while that run is not completed. Tests: a run that is going again leaves the ref; runs that can't be read leave it; once the run is done, the ref is removed.
  4. deleteKeptRef returns removed / gone / moved / failed, checked with ls-remote after a refused delete. Only failed keeps the record. Tests: all four outcomes against a local bare remote, and a failed record retried by the next sweep.

Focused tests: 82/82. ci-gate on 796f336: GREEN (run 42809717282). The sweep made one branch-run read and dropped a backdated probe record.

…emoved by hand

Review kept finding edges in the automatic 24-hour sweep (an active rerun
of an older run, a push URL that differs from the fetch URL, a Ctrl-C
after the verdict) for a rare event, and a kept ci/ ref is harmless: a
branch nobody pushes to starts nothing. The ledger in the git directory,
the sweep, latestRunOnBranch and deleteKeptRef go, with their tests.

An exit without a verdict while the run exists, or may, keeps the ref and
prints one stderr note with its delete command. The SIGINT handler is
detached once the call has its result, so a late Ctrl-C cannot follow a
verdict with ERROR interrupted.
@SSFSKIM
SSFSKIM merged commit 1074ced into main Nov 10, 2026
3 checks passed
@SSFSKIM
SSFSKIM deleted the ci-gate-wait-out-ratelimit branch November 10, 2026 12:06
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