Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(ci): attempt job listings fall back to 30 per page when GitHub 5xxs at 100

garrytan201a31a2f538
diff --git a/scripts/ci-reuse.ts b/scripts/ci-reuse.ts
index 64c7f1524c..0788ec76b0 100644
--- a/scripts/ci-reuse.ts
+++ b/scripts/ci-reuse.ts
@@ -133,15 +133,27 @@ function accept(run: FullRun, tree: string, rejected: Rejection[], where: string
return { reuse: true, reason: 'identical_tree_full_run', detail: `run ${run.id} attempt ${run.run_attempt} on ${where} ran the complete full suite on tree ${tree}`, tree, source: { run_id: run.id, run_attempt: run.run_attempt, sha: run.head_sha }, rejected };
}
-/** Every job of one attempt (the jobs API pages at 100). */
+/**
+ * Every job of one attempt. The jobs API pages at up to 100, but GitHub has answered a 100-job page of a rerun attempt
+ * with a 502 on every retry while 30-job pages of the same attempt answered (Release run 39712021085, v0.60.361.0), so a
+ * 5xx that outlives the client's retries restarts the listing at 30 per page.
+ */
export async function attemptJobs(client: GitHubClient, runId: number, attempt: number): Promise<JobRow[]> {
- const out: JobRow[] = [];
- for (let page = 1; page <= 10; page++) {
- const r = await client.request<{ total_count: number; jobs: JobRow[] }>('GET', `repos/{repo}/actions/runs/${runId}/attempts/${attempt}/jobs?per_page=100&page=${page}`);
- out.push(...r.jobs);
- if (r.jobs.length < 100 || out.length >= r.total_count) return out;
+ for (const perPage of [100, 30]) {
+ const out: JobRow[] = [];
+ try {
+ for (let page = 1; page * perPage <= 1000; page++) {
+ const r = await client.request<{ total_count: number; jobs: JobRow[] }>('GET', `repos/{repo}/actions/runs/${runId}/attempts/${attempt}/jobs?per_page=${perPage}&page=${page}`);
+ out.push(...r.jobs);
+ if (r.jobs.length < perPage || out.length >= r.total_count) return out;
+ }
+ } catch (e) {
+ if (perPage === 30 || ((e as { status?: number }).status ?? 0) < 500) throw e;
+ continue;
+ }
+ throw new Error(`run ${runId} attempt ${attempt} lists more than 1000 jobs`);
}
- throw new Error(`run ${runId} attempt ${attempt} lists more than 1000 jobs`);
+ throw new Error('unreachable');
}
export function apiDeps(client: GitHubClient): ReuseDeps {
diff --git a/test/scripts/ci-reuse.test.ts b/test/scripts/ci-reuse.test.ts
index 2f1f9a1b7b..8f87205216 100644
--- a/test/scripts/ci-reuse.test.ts
+++ b/test/scripts/ci-reuse.test.ts
@@ -12,8 +12,8 @@ import { afterAll, describe, expect, test } from 'bun:test';
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
-import { apiDeps, checkRecord, decide, PROFILES, writeDecision, type Commit, type JobRow, type FullRun, type ReuseDeps } from '../../scripts/ci-reuse.ts';
-import type { GitHubClient } from '../../scripts/lib/gh-issue.ts';
+import { apiDeps, attemptJobs, checkRecord, decide, PROFILES, writeDecision, type Commit, type JobRow, type FullRun, type ReuseDeps } from '../../scripts/ci-reuse.ts';
+import { GET_RETRY_DELAYS_MS, restClient, type GitHubClient } from '../../scripts/lib/gh-issue.ts';
const REPO = 'owner-example/repo-example';
const M = '1'.repeat(40);
@@ -235,6 +235,38 @@ describe('ci-reuse API layer', () => {
expect(paths.filter(p => p.startsWith('repos/{repo}/actions/runs/500/attempts/2/jobs'))).toHaveLength(2);
expect((await api.artifact(500, 'full-suite-tree')).reason).toContain('cannot download');
});
+
+ test('a job listing that 502s at 100 per page after the client retries is read again at 30 per page (Release run 39712021085)', async () => {
+ const all = Array.from({ length: 109 }, (_, i) => ({ name: `job ${i}`, conclusion: 'success' }));
+ const urls: string[] = []; const slept: number[] = [];
+ const realFetch = globalThis.fetch;
+ globalThis.fetch = (async (input: string | URL | Request) => {
+ const url = new URL(String(input)); urls.push(url.pathname + url.search);
+ const per = Number(url.searchParams.get('per_page')), page = Number(url.searchParams.get('page'));
+ if (per === 100) return new Response('<html>502 Bad Gateway</html>', { status: 502 });
+ return Response.json({ total_count: all.length, jobs: all.slice((page - 1) * per, page * per) });
+ }) as typeof fetch;
+ try {
+ const jobs = await attemptJobs(restClient('acme-example/repo', 'token-example', async ms => { slept.push(ms); }), 500, 2);
+ expect(jobs.map(j => j.name)).toEqual(all.map(j => j.name));
+ } finally { globalThis.fetch = realFetch; }
+ expect(slept).toEqual(GET_RETRY_DELAYS_MS);
+ expect(urls).toEqual([
+ ...Array(GET_RETRY_DELAYS_MS.length + 1).fill('/repos/acme-example/repo/actions/runs/500/attempts/2/jobs?per_page=100&page=1'),
+ ...[1, 2, 3, 4].map(page => `/repos/acme-example/repo/actions/runs/500/attempts/2/jobs?per_page=30&page=${page}`),
+ ]);
+ });
+
+ test('a job listing still refused at 30 per page, or refused with a client error, surfaces the error', async () => {
+ const refusing = (status: (per: number) => number): GitHubClient => ({
+ async request<T>(_m: string, path: string): Promise<T> {
+ const code = status(Number(new URL(path, 'https://x.invalid/').searchParams.get('per_page')));
+ throw Object.assign(new Error(`GitHub API GET ${path} -> ${code}`), { status: code });
+ },
+ });
+ await expect(attemptJobs(refusing(() => 502), 500, 2)).rejects.toThrow('per_page=30&page=1 -> 502');
+ await expect(attemptJobs(refusing(() => 404), 500, 2)).rejects.toThrow('per_page=100&page=1 -> 404');
+ });
});
describe('ci-reuse outputs', () => {