Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

ci: gate and report jobs wait out the shared API rate limit, and skip reads they don't need (#9227)

Cindy Zhange1ea78189a36
diff --git a/.github/scripts/ci-heavy-lanes.test.mjs b/.github/scripts/ci-heavy-lanes.test.mjs
index 8a002a54d8..7bf6a310d2 100644
--- a/.github/scripts/ci-heavy-lanes.test.mjs
+++ b/.github/scripts/ci-heavy-lanes.test.mjs
@@ -9,10 +9,16 @@
*/
import fs from 'node:fs';
-import {describe, expect, it} from 'vitest';
+import {describe, expect, it, vi} from 'vitest';
import yaml from 'yaml';
import {ungated} from './ci-pr-gate.mjs';
+// The raw file host is offline here, so the dispatcher reads its rate-limit
+// helper through the API.
+vi.stubGlobal('fetch', async () => {
+ throw new Error('offline');
+});
+
const read = name =>
yaml.parse(
fs.readFileSync(new URL(`../workflows/${name}`, import.meta.url), 'utf8'),
@@ -165,7 +171,16 @@ describe("main's release-check, about once an hour (DEC-11)", () => {
const calls = {dispatch: [], notices: [], listed: []};
const github = {
rest: {
- repos: {getBranch: async () => ({data: {commit: {sha: tip}}})},
+ repos: {
+ getBranch: async () => ({data: {commit: {sha: tip}}}),
+ getContent: async ({path}) => ({
+ data: {
+ content: fs
+ .readFileSync(new URL(`../../${path}`, import.meta.url))
+ .toString('base64'),
+ },
+ }),
+ },
actions: {
listJobsForWorkflowRun: async ({run_id}) => ({
data: {jobs: runs.find(item => item.id === run_id)?.jobs ?? []},
diff --git a/.github/scripts/lib/rate-limit-backoff.cjs b/.github/scripts/lib/rate-limit-backoff.cjs
new file mode 100644
index 0000000000..1c6ca1af65
--- /dev/null
+++ b/.github/scripts/lib/rate-limit-backoff.cjs
@@ -0,0 +1,121 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use strict';
+
+/**
+ * @file Waits out the repository's shared GitHub API rate limit.
+ *
+ * Every workflow in the repository shares one hourly REST budget for its job
+ * token. A gate or report script that hits it used to fail at once; with this
+ * hook installed on its client, a rate-limited call waits for the reset, with
+ * jitter so that waiting jobs do not all retry at once, and is retried. A wait
+ * that would pass the job's bound (five minutes by default) fails at once
+ * instead, with a message that says when the limit resets and to re-run.
+ * Any other error, a permission 403 included, is thrown as before.
+ *
+ * @input the job's GitHub client (actions/github-script `github`) and `core`
+ * @output the same client, retrying rate-limited requests; a count of the
+ * requests the job made, logged when the job's process exits
+ * @position loaded by the review, spec-owner, re-request and PR report jobs
+ */
+
+const DEFAULT_MAX_WAIT_MS = 5 * 60 * 1000;
+const MAX_JITTER_MS = 15 * 1000;
+// A secondary limit can arrive without a reset time.
+const UNTIMED_WAIT_MS = 60 * 1000;
+
+/**
+ * How long to wait before retrying a request that failed on a rate limit, or
+ * null when the failure is not a rate limit.
+ */
+function rateLimitWaitMs(error, now) {
+ const status = Number(error?.status);
+ if (status !== 403 && status !== 429) return null;
+ const headers = error?.response?.headers ?? {};
+ const header = name => {
+ const value = headers[name] ?? headers[name.toLowerCase()];
+ return value == null ? null : String(value);
+ };
+ const exhausted = header('x-ratelimit-remaining') === '0';
+ const retryAfter = header('retry-after');
+ const message = String(error?.message ?? '');
+ if (
+ status === 403 &&
+ !exhausted &&
+ retryAfter == null &&
+ !/rate limit/i.test(message)
+ ) {
+ return null;
+ }
+ if (retryAfter != null && Number.isFinite(Number(retryAfter))) {
+ return Math.max(1000, Number(retryAfter) * 1000);
+ }
+ // Every response carries the hourly reset; it is the wait only when the
+ // hourly budget is spent, not for a secondary limit.
+ const reset = Number(header('x-ratelimit-reset'));
+ if (exhausted && Number.isFinite(reset) && reset > 0) {
+ return Math.max(1000, reset * 1000 - now);
+ }
+ return UNTIMED_WAIT_MS;
+}
+
+/**
+ * Installs the wait-and-retry hook on `github` and returns the job's request
+ * count. Idempotent: a client that already has it keeps the first.
+ */
+function installRateLimitBackoff({
+ github,
+ core,
+ maxWaitMs = DEFAULT_MAX_WAIT_MS,
+ now = Date.now,
+ sleep = ms => new Promise(resolve => setTimeout(resolve, ms)),
+ random = Math.random,
+ logOnExit = true,
+}) {
+ if (github.__rateLimitBackoff) return github.__rateLimitBackoff;
+ const deadline = now() + maxWaitMs;
+ const counter = {requests: 0, waits: 0};
+ // A client without request hooks (a test double) is left as it is.
+ if (typeof github.hook?.wrap !== 'function') return counter;
+ github.hook.wrap('request', async (request, options) => {
+ for (;;) {
+ counter.requests += 1;
+ try {
+ return await request(options);
+ } catch (error) {
+ const wait = rateLimitWaitMs(error, now());
+ if (wait == null) throw error;
+ const jittered = wait + Math.floor(random() * MAX_JITTER_MS);
+ const resetAt = new Date(now() + wait).toISOString();
+ if (now() + jittered > deadline) {
+ const limited = new Error(
+ `GitHub API rate limit reached for this repository's workflows (resets at ${resetAt}). Re-run this job after that. (${error.message})`,
+ );
+ limited.status = error.status;
+ limited.rateLimited = true;
+ throw limited;
+ }
+ counter.waits += 1;
+ core.warning(
+ `GitHub API rate limit reached: waiting ${Math.ceil(jittered / 1000)} s for the reset at ${resetAt}, then retrying ${options.method} ${options.url}.`,
+ );
+ await sleep(jittered);
+ }
+ }
+ });
+ if (logOnExit) {
+ process.once('exit', () =>
+ core.info(
+ `GitHub API requests made by this step: ${counter.requests} (rate-limit waits: ${counter.waits}).`,
+ ),
+ );
+ }
+ Object.defineProperty(github, '__rateLimitBackoff', {value: counter});
+ return counter;
+}
+
+module.exports = {
+ DEFAULT_MAX_WAIT_MS,
+ installRateLimitBackoff,
+ rateLimitWaitMs,
+};
diff --git a/.github/scripts/lib/rate-limit-backoff.test.mjs b/.github/scripts/lib/rate-limit-backoff.test.mjs
new file mode 100644
index 0000000000..2ae6a02214
--- /dev/null
+++ b/.github/scripts/lib/rate-limit-backoff.test.mjs
@@ -0,0 +1,263 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file The shared rate-limit wait (lib/rate-limit-backoff.cjs).
+ *
+ * @input a fake client whose `hook.wrap` installs the hook, and requests that
+ * fail with GitHub's rate-limit responses
+ * @output proofs that a rate-limited request waits for the reset with jitter
+ * and is retried, that a wait past the bound fails at once and says when to
+ * re-run, and that every other error passes through unchanged
+ * @position focused suite for .github/scripts/lib/rate-limit-backoff.cjs
+ */
+
+import {createRequire} from 'node:module';
+
+import {describe, expect, it} from 'vitest';
+
+const require = createRequire(import.meta.url);
+const {
+ DEFAULT_MAX_WAIT_MS,
+ installRateLimitBackoff,
+ rateLimitWaitMs,
+} = require('./rate-limit-backoff.cjs');
+
+const NOW = 1_800_000_000_000;
+
+function rateLimited({status = 403, headers = {}, message} = {}) {
+ return Object.assign(
+ new Error(message ?? 'API rate limit exceeded for installation.'),
+ {status, response: {headers}},
+ );
+}
+
+/** A client whose requests answer from `responses` in order. */
+function client(responses, {maxWaitMs} = {}) {
+ let wrapped;
+ const github = {hook: {wrap: (_, fn) => (wrapped = fn)}};
+ const log = {warnings: [], sleeps: [], info: []};
+ let clock = NOW;
+ const counter = installRateLimitBackoff({
+ github,
+ core: {
+ warning: message => log.warnings.push(message),
+ info: message => log.info.push(message),
+ },
+ maxWaitMs,
+ now: () => clock,
+ sleep: async ms => {
+ log.sleeps.push(ms);
+ clock += ms;
+ },
+ random: () => 0.5,
+ logOnExit: false,
+ });
+ const queue = [...responses];
+ const request = async () => {
+ const next = queue.shift();
+ if (next instanceof Error) throw next;
+ return next;
+ };
+ const call = () =>
+ wrapped(request, {method: 'GET', url: '/repos/{owner}/{repo}/pulls/{n}'});
+ return {github, call, log, counter};
+}
+
+describe('rateLimitWaitMs', () => {
+ it('waits until the reset of an exhausted limit', () => {
+ const error = rateLimited({
+ headers: {
+ 'x-ratelimit-remaining': '0',
+ 'x-ratelimit-reset': String(NOW / 1000 + 90),
+ },
+ });
+ expect(rateLimitWaitMs(error, NOW)).toBe(90_000);
+ });
+
+ it('honours retry-after on a 429 or a secondary limit', () => {
+ expect(
+ rateLimitWaitMs(
+ rateLimited({status: 429, headers: {'retry-after': '30'}}),
+ NOW,
+ ),
+ ).toBe(30_000);
+ });
+
+ it('waits a minute for a secondary limit while the hourly budget remains', () => {
+ expect(
+ rateLimitWaitMs(
+ rateLimited({
+ message: 'You have exceeded a secondary rate limit.',
+ headers: {
+ 'x-ratelimit-remaining': '4000',
+ 'x-ratelimit-reset': String(NOW / 1000 + 3000),
+ },
+ }),
+ NOW,
+ ),
+ ).toBe(60_000);
+ });
+
+ it('waits a minute for a rate-limit message without a reset time', () => {
+ expect(
+ rateLimitWaitMs(
+ rateLimited({message: 'You have exceeded a secondary rate limit.'}),
+ NOW,
+ ),
+ ).toBe(60_000);
+ });
+
+ it('is not a rate limit for a permission 403 or another status', () => {
+ expect(
+ rateLimitWaitMs(
+ Object.assign(new Error('Resource not accessible by integration'), {
+ status: 403,
+ response: {headers: {'x-ratelimit-remaining': '4999'}},
+ }),
+ NOW,
+ ),
+ ).toBeNull();
+ expect(
+ rateLimitWaitMs(
+ Object.assign(new Error('Not Found'), {status: 404}),
+ NOW,
+ ),
+ ).toBeNull();
+ });
+});
+
+describe('installRateLimitBackoff', () => {
+ const resetIn = seconds => ({
+ 'x-ratelimit-remaining': '0',
+ 'x-ratelimit-reset': String(NOW / 1000 + seconds),
+ });
+
+ it('waits for the reset with jitter, then retries', async () => {
+ const {call, log, counter} = client([
+ rateLimited({headers: resetIn(60)}),
+ {data: 'ok'},
+ ]);
+ await expect(call()).resolves.toEqual({data: 'ok'});
+ // Sixty seconds to the reset, plus half the 15-second jitter.
+ expect(log.sleeps).toEqual([67_500]);
+ expect(log.warnings[0]).toMatch(
+ /^GitHub API rate limit reached: waiting 68 s for the reset at .+, then retrying GET /,
+ );
+ expect(counter).toEqual({requests: 2, waits: 1});
+ });
+
+ it('fails at once, saying when to re-run, when the reset is past the bound', async () => {
+ const {call, log} = client([rateLimited({headers: resetIn(1800)})]);
+ const error = await call().catch(caught => caught);
+ expect(log.sleeps).toEqual([]);
+ expect(error.rateLimited).toBe(true);
+ expect(error.status).toBe(403);
+ expect(error.message).toMatch(
+ /^GitHub API rate limit reached for this repository's workflows \(resets at .+\)\. Re-run this job after that\./,
+ );
+ });
+
+ it('stops waiting once the job has spent its bound', async () => {
+ const {call, log} = client(
+ [
+ rateLimited({headers: resetIn(100)}),
+ // 107.5 s in, the next reset is 292.5 s away: past the 300 s bound.
+ rateLimited({headers: resetIn(400)}),
+ ],
+ {maxWaitMs: 300_000},
+ );
+ const error = await call().catch(caught => caught);
+ expect(log.sleeps).toEqual([107_500]);
+ expect(error.rateLimited).toBe(true);
+ });
+
+ it('passes every other error through unchanged', async () => {
+ const denied = Object.assign(
+ new Error('Resource not accessible by integration'),
+ {
+ status: 403,
+ response: {headers: {}},
+ },
+ );
+ const {call, log} = client([denied]);
+ await expect(call()).rejects.toBe(denied);
+ expect(log.sleeps).toEqual([]);
+ });
+
+ it('leaves a client without request hooks as it is', () => {
+ expect(
+ installRateLimitBackoff({github: {}, core: {}, logOnExit: false}),
+ ).toEqual({requests: 0, waits: 0});
+ });
+
+ it('installs once per client', () => {
+ const {github, counter} = client([]);
+ expect(installRateLimitBackoff({github, core: {}, logOnExit: false})).toBe(
+ counter,
+ );
+ });
+});
+
+describe('the gate and report jobs that share the API budget wait it out', () => {
+ const fs = require('node:fs');
+ const path = require('node:path');
+ const YAML = require('yaml');
+ const root = path.resolve(import.meta.dirname, '../../..');
+ const scripts = (workflow, job) =>
+ YAML.parse(
+ fs.readFileSync(path.join(root, '.github/workflows', workflow), 'utf8'),
+ )
+ .jobs[job].steps.filter(step =>
+ step.uses?.startsWith('actions/github-script@'),
+ )
+ .map(step => step.with.script);
+
+ it.each([
+ ['review-signal.yml', 'flag'],
+ ['review-signal.yml', 'spec-owner'],
+ ['review-clear.yml', 'clear'],
+ ['rerequest-review-on-update.yml', 'rerequest-review'],
+ ['pr-comment.yml', 'resolve'],
+ ['pr-comment.yml', 'spec-only-reconcile'],
+ ['pr-comment.yml', 'comment'],
+ ['main-heavy-lanes.yml', 'dispatch'],
+ ])('%s %s installs the wait before its first request', (workflow, job) => {
+ const steps = scripts(workflow, job);
+ expect(steps.length).toBeGreaterThan(0);
+ for (const script of steps) {
+ expect(script.trimStart()).toMatch(
+ /^\/\/ Wait out the repository's shared API rate limit/,
+ );
+ expect(script).toContain('installRateLimitBackoff({github, core})');
+ }
+ });
+
+ it("gives main's dispatcher room to finish a full wait", () => {
+ const workflow = YAML.parse(
+ fs.readFileSync(
+ path.join(root, '.github/workflows/main-heavy-lanes.yml'),
+ 'utf8',
+ ),
+ );
+ expect(workflow.jobs.dispatch['timeout-minutes']).toBeGreaterThan(
+ DEFAULT_MAX_WAIT_MS / 60000 + 2,
+ );
+ });
+
+ it('reads the helper from a trusted commit, never the pull request, without spending the budget first', () => {
+ for (const [workflow, job] of [
+ ['review-signal.yml', 'flag'],
+ ['review-clear.yml', 'clear'],
+ ['rerequest-review-on-update.yml', 'rerequest-review'],
+ ['main-heavy-lanes.yml', 'dispatch'],
+ ]) {
+ const script = scripts(workflow, job)[0];
+ expect(script).toContain(
+ 'https://raw.githubusercontent.com/${context.repo.owner}/${context.repo.repo}/${context.sha}/${helperPath}',
+ );
+ expect(script).toContain('ref: context.sha, path: helperPath');
+ // The raw read comes before any API request.
+ expect(script.indexOf('fetch(')).toBeLessThan(script.indexOf('github.'));
+ }
+ });
+});
diff --git a/.github/scripts/review-clear-workflow.test.mjs b/.github/scripts/review-clear-workflow.test.mjs
index 63a96aa192..f154430cde 100644
--- a/.github/scripts/review-clear-workflow.test.mjs
+++ b/.github/scripts/review-clear-workflow.test.mjs
@@ -2,9 +2,14 @@
import fs from 'node:fs';
import path from 'node:path';
-import {describe, expect, it} from 'vitest';
+import {describe, expect, it, vi} from 'vitest';
import YAML from 'yaml';
+// The raw file host is offline here, so the job reads its helper through the API.
+vi.stubGlobal('fetch', async () => {
+ throw new Error('offline');
+});
+
const root = path.resolve(import.meta.dirname, '../..');
const workflowSource = fs.readFileSync(
path.join(root, '.github/workflows/review-clear.yml'),
@@ -110,6 +115,17 @@ function harness(
},
repos: {
getContent: async ({ref, path: filePath}) => {
+ // The shared rate-limit wait, read before anything else.
+ if (filePath === '.github/scripts/lib/rate-limit-backoff.cjs') {
+ return {
+ data: {
+ type: 'file',
+ content: fs
+ .readFileSync(path.join(root, filePath))
+ .toString('base64'),
+ },
+ };
+ }
calls.push(`read:${ref}:${filePath}`);
if (!helperAt.includes(ref)) {
throw Object.assign(new Error('Not Found'), {status: 404});
diff --git a/.github/scripts/review-signal-workflow.test.mjs b/.github/scripts/review-signal-workflow.test.mjs
index 915082538e..a7dbd32697 100644
--- a/.github/scripts/review-signal-workflow.test.mjs
+++ b/.github/scripts/review-signal-workflow.test.mjs
@@ -13,6 +13,11 @@ import path from 'node:path';
import {describe, expect, it, vi} from 'vitest';
import YAML from 'yaml';
+// The raw file host is offline here, so jobs read their helpers through the API.
+vi.stubGlobal('fetch', async () => {
+ throw new Error('offline');
+});
+
const root = path.resolve(import.meta.dirname, '../..');
const workflow = fs.readFileSync(
path.join(root, '.github/workflows/review-signal.yml'),
@@ -23,6 +28,7 @@ const script = parsed.jobs.flag.steps.find(
step => step.name === 'Detect signals and route',
).with.script;
const HELPER_PATH = '.github/scripts/review-signal-decision.cjs';
+const BACKOFF_PATH = '.github/scripts/lib/rate-limit-backoff.cjs';
const WORKFLOW_SHA = '1'.repeat(40);
const OLD_BASE = '2'.repeat(40);
const HEAD = '3'.repeat(40);
@@ -66,7 +72,11 @@ async function runSignal({
graphql: vi.fn(),
};
const getContent = vi.fn(async ({path: filePath, ref}) => {
- if (filePath === HELPER_PATH) {
+ if (filePath === BACKOFF_PATH) {
+ if (ref !== WORKFLOW_SHA) {
+ throw new Error(`Unexpected content read: ${ref}:${filePath}`);
+ }
+ } else if (filePath === HELPER_PATH) {
if (ref !== WORKFLOW_SHA || helperMissing) {
throw Object.assign(new Error('Not Found'), {status: 404});
}
diff --git a/.github/scripts/spec-owner-reconcile.cjs b/.github/scripts/spec-owner-reconcile.cjs
index 881188618f..f430008e3d 100644
--- a/.github/scripts/spec-owner-reconcile.cjs
+++ b/.github/scripts/spec-owner-reconcile.cjs
@@ -245,7 +245,9 @@ async function reconcileSpecOwnerGate({
return !(await restoreNewerStatus(headSha));
}
- async function removeLabel(name) {
+ async function removeLabel(name, pr = null) {
+ // A pull request read in this run that lacks the label needs no call.
+ if (pr && !(pr.labels ?? []).some(label => label?.name === name)) return;
try {
await github.rest.issues.removeLabel({
owner,
@@ -499,37 +501,34 @@ async function reconcileSpecOwnerGate({
async function fetchSnapshot() {
const before = await getPullRequest();
const headSha = before.head.sha;
- const [files, reviews, comments, timeline, statuses] = await Promise.all([
+ const [files, statuses] = await Promise.all([
github.paginate(github.rest.pulls.listFiles, {
owner,
repo,
pull_number: pullNumber,
per_page: 100,
}),
- github.paginate(github.rest.pulls.listReviews, {
- owner,
- repo,
- pull_number: pullNumber,
- per_page: 100,
- }),
- github.paginate(github.rest.issues.listComments, {
- owner,
- repo,
- issue_number: pullNumber,
- per_page: 100,
- }),
- github.paginate(github.rest.issues.listEventsForTimeline, {
- owner,
- repo,
- issue_number: pullNumber,
- per_page: 100,
- }),
listStatuses(headSha),
]);
const after = await getPullRequest();
if (after.head.sha !== headSha) return null;
const scope = classifyChanges(files, {expectedCount: after.changed_files});
+ // Owner reviews, commands and dismissals only decide a change to
+ // knowledge records; every other pull request skips those reads, which
+ // spend the repository's shared API budget on each event. The head is
+ // confirmed again after them, below.
+ const list = (method, target) =>
+ scope.touchesKnowledgeRecords
+ ? github.paginate(method, {owner, repo, ...target, per_page: 100})
+ : Promise.resolve([]);
+ const [reviews, comments, timeline] = await Promise.all([
+ list(github.rest.pulls.listReviews, {pull_number: pullNumber}),
+ list(github.rest.issues.listComments, {issue_number: pullNumber}),
+ list(github.rest.issues.listEventsForTimeline, {
+ issue_number: pullNumber,
+ }),
+ ]);
const knowledgeChanges = files.filter(
file =>
scope.complete &&
@@ -627,7 +626,7 @@ async function reconcileSpecOwnerGate({
: null;
if (!scope.touchesKnowledgeRecords) {
- await removeLabel(env.REVIEW_LABEL);
+ await removeLabel(env.REVIEW_LABEL, pr);
await setFinalStatus(
initialHead,
'success',
diff --git a/.github/scripts/spec-owner-reconcile.test.mjs b/.github/scripts/spec-owner-reconcile.test.mjs
index 470bc96808..e81be3885b 100644
--- a/.github/scripts/spec-owner-reconcile.test.mjs
+++ b/.github/scripts/spec-owner-reconcile.test.mjs
@@ -349,6 +349,57 @@ function createDesignHarness(options = {}) {
}
describe('spec owner workflow reconciliation', () => {
+ it('skips the owner reads and the label call for a pull request that changes no records', async () => {
+ const harness = createHarness({
+ changedFile: {filename: 'README.md', status: 'modified'},
+ });
+ const reads = [];
+ for (const [group, name] of [
+ ['pulls', 'listReviews'],
+ ['issues', 'listComments'],
+ ['issues', 'listEventsForTimeline'],
+ ['issues', 'removeLabel'],
+ ]) {
+ harness.github.rest[group][name] = async () => {
+ reads.push(name);
+ return {data: []};
+ };
+ }
+
+ await run(harness, context({runId: 100n, action: 'synchronize'}));
+
+ expect(reads).toEqual([]);
+ expect(latestGateStatus(harness.state)).toMatchObject({
+ state: 'success',
+ });
+ });
+
+ it('still reaches a terminal status when the changed-file count moves after the file list', async () => {
+ const harness = createHarness({
+ changedFile: {filename: 'README.md', status: 'modified'},
+ onPullGet: (count, state) => {
+ if (count >= 2) state.pr.changed_files = 2;
+ },
+ });
+
+ await run(harness, context({runId: 100n, action: 'synchronize'}));
+
+ expect(latestGateStatus(harness.state).description).not.toMatch(
+ /^Reconciling exact head/,
+ );
+ });
+
+ it('still removes a stale review label from a pull request that changes no records', async () => {
+ const harness = createHarness({
+ changedFile: {filename: 'README.md', status: 'modified'},
+ labels: [env.REVIEW_LABEL],
+ });
+
+ await run(harness, context({runId: 100n, action: 'synchronize'}));
+
+ expect(harness.state.calls).toContain(`remove-label:${env.REVIEW_LABEL}`);
+ });
+
it('clears the gate on an exact-head ENGOWNER review', async () => {
const harness = createApprovedHarness();
diff --git a/.github/workflows/main-heavy-lanes.yml b/.github/workflows/main-heavy-lanes.yml
index 5ac6ec453d..e17fca6af1 100644
--- a/.github/workflows/main-heavy-lanes.yml
+++ b/.github/workflows/main-heavy-lanes.yml
@@ -27,7 +27,8 @@ concurrency:
jobs:
dispatch:
runs-on: ubuntu-slim
- timeout-minutes: 5
+ # Room for the rate-limit wait (at most five minutes) and the dispatch.
+ timeout-minutes: 10
permissions:
actions: write
contents: read
@@ -38,6 +39,27 @@ jobs:
SHA: ${{ github.sha }}
with:
script: |
+ // Wait out the repository's shared API rate limit, with the helper
+ // read from this workflow's own trusted commit (no checkout). The
+ // raw file host spends none of that budget; the API is the fallback.
+ {
+ const helperPath = '.github/scripts/lib/rate-limit-backoff.cjs';
+ let helperSource;
+ try {
+ const response = await fetch(`https://raw.githubusercontent.com/${context.repo.owner}/${context.repo.repo}/${context.sha}/${helperPath}`);
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
+ helperSource = await response.text();
+ } catch {
+ const {data: helper} = await github.rest.repos.getContent({...context.repo, ref: context.sha, path: helperPath});
+ helperSource = Buffer.from(helper.content, 'base64').toString('utf8');
+ }
+ const helperModule = {exports: {}};
+ new Function('module', 'exports', 'require', helperSource)(
+ helperModule, helperModule.exports,
+ () => { throw new Error('rate-limit-backoff.cjs must stay dependency-free'); },
+ );
+ helperModule.exports.installRateLimitBackoff({github, core});
+ }
// A check starts at most once in this window; the schedule's
// quarter-hour cadence adds up to 15 minutes to it.
const WINDOW_MINUTES = 55;
diff --git a/.github/workflows/pr-comment.yml b/.github/workflows/pr-comment.yml
index 34ed7d92f6..2d7e81bce5 100644
--- a/.github/workflows/pr-comment.yml
+++ b/.github/workflows/pr-comment.yml
@@ -62,6 +62,9 @@ jobs:
with:
retries: 3
script: |
+ // Wait out the repository's shared API rate limit (lib/rate-limit-backoff.cjs).
+ require(require('node:path').join(process.env.GITHUB_WORKSPACE, '.github/scripts/lib/rate-limit-backoff.cjs'))
+ .installRateLimitBackoff({github, core});
const {pathToFileURL} = require('node:url');
const {resolveWorkflowRunPullRequest} = await import(
pathToFileURL(`${process.env.GITHUB_WORKSPACE}/.github/scripts/lib/pr-preview.mjs`).href
@@ -158,6 +161,9 @@ jobs:
with:
retries: 3
script: |
+ // Wait out the repository's shared API rate limit (lib/rate-limit-backoff.cjs).
+ require(require('node:path').join(process.env.GITHUB_WORKSPACE, '.github/scripts/lib/rate-limit-backoff.cjs'))
+ .installRateLimitBackoff({github, core});
const {pathToFileURL} = require('node:url');
const {reconcilePrComment} = await import(
pathToFileURL(`${process.env.GITHUB_WORKSPACE}/.github/scripts/lib/pr-preview.mjs`).href
@@ -336,6 +342,9 @@ jobs:
with:
retries: 3
script: |
+ // Wait out the repository's shared API rate limit (lib/rate-limit-backoff.cjs).
+ require(require('node:path').join(process.env.GITHUB_WORKSPACE, '.github/scripts/lib/rate-limit-backoff.cjs'))
+ .installRateLimitBackoff({github, core});
const {pathToFileURL} = require('node:url');
const {confirmOrStandDown} = await import(
pathToFileURL(`${process.env.GITHUB_WORKSPACE}/.github/scripts/lib/pr-preview.mjs`).href
@@ -478,6 +487,9 @@ jobs:
with:
retries: 3
script: |
+ // Wait out the repository's shared API rate limit (lib/rate-limit-backoff.cjs).
+ require(require('node:path').join(process.env.GITHUB_WORKSPACE, '.github/scripts/lib/rate-limit-backoff.cjs'))
+ .installRateLimitBackoff({github, core});
const {pathToFileURL} = require('node:url');
const {reconcilePrComment} = await import(
pathToFileURL(`${process.env.GITHUB_WORKSPACE}/.github/scripts/lib/pr-preview.mjs`).href
diff --git a/.github/workflows/rerequest-review-on-update.yml b/.github/workflows/rerequest-review-on-update.yml
index 213dbdbe72..d80d34328c 100644
--- a/.github/workflows/rerequest-review-on-update.yml
+++ b/.github/workflows/rerequest-review-on-update.yml
@@ -10,7 +10,7 @@ name: Re-request review on update
on:
pull_request_target:
- branches: ["main"]
+ branches: ['main']
types: [synchronize, ready_for_review, reopened]
permissions: {}
@@ -31,6 +31,27 @@ jobs:
uses: actions/github-script@v9
with:
script: |
+ // Wait out the repository's shared API rate limit, with the helper
+ // read from this workflow's own trusted commit (no checkout). The
+ // raw file host spends none of that budget; the API is the fallback.
+ {
+ const helperPath = '.github/scripts/lib/rate-limit-backoff.cjs';
+ let helperSource;
+ try {
+ const response = await fetch(`https://raw.githubusercontent.com/${context.repo.owner}/${context.repo.repo}/${context.sha}/${helperPath}`);
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
+ helperSource = await response.text();
+ } catch {
+ const {data: helper} = await github.rest.repos.getContent({...context.repo, ref: context.sha, path: helperPath});
+ helperSource = Buffer.from(helper.content, 'base64').toString('utf8');
+ }
+ const helperModule = {exports: {}};
+ new Function('module', 'exports', 'require', helperSource)(
+ helperModule, helperModule.exports,
+ () => { throw new Error('rate-limit-backoff.cjs must stay dependency-free'); },
+ );
+ helperModule.exports.installRateLimitBackoff({github, core});
+ }
const {owner, repo} = context.repo;
const pull_number = context.payload.pull_request.number;
diff --git a/.github/workflows/review-clear.yml b/.github/workflows/review-clear.yml
index 8813cd1e81..3e8bb94ac8 100644
--- a/.github/workflows/review-clear.yml
+++ b/.github/workflows/review-clear.yml
@@ -54,6 +54,27 @@ jobs:
# be the last word on whether the gate clears.
retries: 3
script: |
+ // Wait out the repository's shared API rate limit, with the helper
+ // read from this workflow's own trusted commit (no checkout). The
+ // raw file host spends none of that budget; the API is the fallback.
+ {
+ const helperPath = '.github/scripts/lib/rate-limit-backoff.cjs';
+ let helperSource;
+ try {
+ const response = await fetch(`https://raw.githubusercontent.com/${context.repo.owner}/${context.repo.repo}/${context.sha}/${helperPath}`);
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
+ helperSource = await response.text();
+ } catch {
+ const {data: helper} = await github.rest.repos.getContent({...context.repo, ref: context.sha, path: helperPath});
+ helperSource = Buffer.from(helper.content, 'base64').toString('utf8');
+ }
+ const helperModule = {exports: {}};
+ new Function('module', 'exports', 'require', helperSource)(
+ helperModule, helperModule.exports,
+ () => { throw new Error('rate-limit-backoff.cjs must stay dependency-free'); },
+ );
+ helperModule.exports.installRateLimitBackoff({github, core});
+ }
const { owner, repo } = context.repo;
const run = context.payload.workflow_run;
const CODE = 'needs:code-review';
diff --git a/.github/workflows/review-signal.yml b/.github/workflows/review-signal.yml
index 85fa65a4ed..72f999924a 100644
--- a/.github/workflows/review-signal.yml
+++ b/.github/workflows/review-signal.yml
@@ -131,6 +131,27 @@ jobs:
# "Expected — waiting for status" (#5149, #5152).
retries: 3
script: |
+ // Wait out the repository's shared API rate limit, with the helper
+ // read from this workflow's own trusted commit (no checkout). The
+ // raw file host spends none of that budget; the API is the fallback.
+ {
+ const helperPath = '.github/scripts/lib/rate-limit-backoff.cjs';
+ let helperSource;
+ try {
+ const response = await fetch(`https://raw.githubusercontent.com/${context.repo.owner}/${context.repo.repo}/${context.sha}/${helperPath}`);
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
+ helperSource = await response.text();
+ } catch {
+ const {data: helper} = await github.rest.repos.getContent({...context.repo, ref: context.sha, path: helperPath});
+ helperSource = Buffer.from(helper.content, 'base64').toString('utf8');
+ }
+ const helperModule = {exports: {}};
+ new Function('module', 'exports', 'require', helperSource)(
+ helperModule, helperModule.exports,
+ () => { throw new Error('rate-limit-backoff.cjs must stay dependency-free'); },
+ );
+ helperModule.exports.installRateLimitBackoff({github, core});
+ }
const { owner, repo } = context.repo;
const CODE = process.env.CODE_LABEL;
const DESIGN = process.env.DESIGN_LABEL;
@@ -462,7 +483,11 @@ jobs:
}
return Buffer.from(data.content, 'base64').toString('utf8');
}
- for (const file of coreRuntimeFiles) {
+ // The source bytes only prove the appearance-only exception
+ // a design owner may self-serve; an engineering owner
+ // self-serves every code change, so their pull requests skip
+ // these reads (two per changed Core file).
+ for (const file of authorIsEng ? [] : coreRuntimeFiles) {
const previousPath = file.previous_filename || file.filename;
if (file.status !== 'modified' || previousPath !== file.filename) {
continue;
@@ -763,6 +788,9 @@ jobs:
with:
retries: 3
script: |
+ // Wait out the repository's shared API rate limit (lib/rate-limit-backoff.cjs).
+ require(require('node:path').join(process.env.GITHUB_WORKSPACE, '.github/scripts/lib/rate-limit-backoff.cjs'))
+ .installRateLimitBackoff({github, core});
const path = require('node:path');
const {reconcileSpecOwnerGate} = require(path.join(
process.env.GITHUB_WORKSPACE,