Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(nightly-watch): list attempt jobs 50 per page (GitHub 502s every 100-job page of a large attempt) and retry GETs that got no response; 4xx still fails at once
garrytan4ca9aae73222
diff --git a/scripts/lib/ci-run.ts b/scripts/lib/ci-run.tsindex 1fca487d2b..d66c7c01b0 100644--- a/scripts/lib/ci-run.ts+++ b/scripts/lib/ci-run.ts@@ -117,14 +117,21 @@ export function reproduceCommand(file: string, postgres: boolean): string | undereturn `bun run test:stress ${file} --iterations 10${postgres ? ' --postgres' : ''}`;}+/**+ * GitHub answers a 100-job page of a large run's attempt with a 502 every time (its+ * server-side render times out near 10s), so retrying that page never succeeds;+ * 50-job pages of the same attempt return in a few seconds.+ */+export const JOBS_PER_PAGE = 50;+export async function pagedJobs(client: GitHubClient, run: RunInfo): Promise<{ jobs: JobInfo[]; complete: boolean }> {const jobs: JobInfo[] = [];let total = 0;- for (let page = 1; page <= 5; page++) {- const r = await client.request<{ jobs: JobInfo[]; total_count: number }>('GET', `repos/{repo}/actions/runs/${run.id}/attempts/${run.run_attempt}/jobs?per_page=100&page=${page}`);+ for (let page = 1; page <= 500 / JOBS_PER_PAGE; page++) {+ const r = await client.request<{ jobs: JobInfo[]; total_count: number }>('GET', `repos/{repo}/actions/runs/${run.id}/attempts/${run.run_attempt}/jobs?per_page=${JOBS_PER_PAGE}&page=${page}`);jobs.push(...r.jobs);total = r.total_count;- if (jobs.length >= r.total_count || r.jobs.length < 100) break;+ if (jobs.length >= r.total_count || r.jobs.length < JOBS_PER_PAGE) break;}return { jobs, complete: jobs.length >= total };}diff --git a/scripts/lib/gh-issue.ts b/scripts/lib/gh-issue.tsindex 811e747570..fe6952c9a7 100644--- a/scripts/lib/gh-issue.ts+++ b/scripts/lib/gh-issue.ts@@ -63,16 +63,16 @@ export function inertBlock(value: unknown, max = 1500): string {return s;}-/** Delays before each retry of a GET that failed with a 5xx (GitHub's transient 502/503/504); writes are never retried. */-export const GET_RETRY_DELAYS_MS = [1_000, 3_000];+/** Delays before each retry of a GET that failed with a 5xx (GitHub's transient 502/503/504) or got no response at all; a 4xx fails at once and writes are never retried. */+export const GET_RETRY_DELAYS_MS = [1_000, 3_000, 9_000];async function retryingGet<T>(method: string, sleep: (ms: number) => Promise<unknown>, call: () => Promise<T>): Promise<T> {for (let attempt = 0; ; attempt++) {try {return await call();} catch (e) {- const status = (e as { status?: number }).status ?? 0;- if (method !== 'GET' || status < 500 || attempt >= GET_RETRY_DELAYS_MS.length) throw e;+ const { status, network } = e as { status?: number; network?: boolean };+ if (method !== 'GET' || !((status ?? 0) >= 500 || network) || attempt >= GET_RETRY_DELAYS_MS.length) throw e;await sleep(GET_RETRY_DELAYS_MS[attempt]!);}}@@ -98,6 +98,8 @@ function tokenClient(resolve: (path: string) => string, token: string): GitHubCl...(body === undefined ? {} : { 'content-type': 'application/json' }),},body: body === undefined ? undefined : JSON.stringify(body),+ }).catch((e: unknown) => {+ throw Object.assign(new Error(`GitHub API ${method} ${url} -> no response: ${e instanceof Error ? e.message : String(e)}`), { network: true });});if (!res.ok) {const text = await res.text().catch(() => '');diff --git a/test/scripts/nightly-issue.test.ts b/test/scripts/nightly-issue.test.tsindex d2fb603205..b84befc059 100644--- a/test/scripts/nightly-issue.test.ts+++ b/test/scripts/nightly-issue.test.ts@@ -15,6 +15,7 @@ import { loadReceiptDir } from '../../scripts/ci-executed-counts.ts';import { buildManifest, checkManifest, failureSignature, junitMessages, MANIFEST_ARTIFACT, MANIFEST_SCHEMA, type CiManifest } from '../../scripts/ci-manifest.ts';import { failureSignature as gateSignature, parseExemptionBlocks } from '../fixtures/nightly-watch/stress-exemption-contract.ts';import { GET_RETRY_DELAYS_MS, inert, jsonBlock, labelNames, readJsonBlock, readZipEntry, restClient, type GitHubClient, type Issue } from '../../scripts/lib/gh-issue.ts';+import { pagedJobs } from '../../scripts/lib/ci-run.ts';import { FLAKE_MARKER, MR_MARKER, PUSH_ALLOWLIST, type FlakeRecord, type MasterRedState } from '../../scripts/lib/master-red.ts';import {JSON_MARKER, parseKnownRed, resolveTarget, routeRun, validateKnownRed, watch, watchRun, type IncidentRecord, type JobInfo, type KnownRow, type RunInfo,@@ -673,6 +674,55 @@ describe('master-red: push-to-master runs', () => {}});+ test('restClient retries a GET whose connection dropped (no response), and a 4xx after a network blip still fails without more retries', async () => {+ const real = globalThis.fetch;+ let script: Array<number | 'drop'> = [];+ let calls = 0;+ globalThis.fetch = (async () => {+ calls++;+ const next = script.shift() ?? 200;+ if (next === 'drop') throw new TypeError('fetch failed');+ return new Response(next === 200 ? JSON.stringify({ ok: true }) : 'nope', { status: next });+ }) as unknown as typeof fetch;+ const slept: number[] = [];+ const client = restClient('o/r', 'token', async ms => { slept.push(ms); });+ try {+ script = ['drop', 200];+ expect(await client.request<unknown>('GET', 'repos/{repo}/actions/runs/1')).toEqual({ ok: true });+ expect({ calls, slept }).toEqual({ calls: 2, slept: [GET_RETRY_DELAYS_MS[0]!] });++ calls = 0; slept.length = 0;+ script = ['drop', 404];+ expect(await client.request('GET', 'repos/{repo}/actions/runs/9').then(() => 0, e => (e as { status?: number }).status)).toBe(404);+ expect(calls).toBe(2);++ calls = 0;+ script = ['drop'];+ expect(await client.request('POST', 'repos/{repo}/issues', { title: 't' }).then(() => 'ok', e => (e as { network?: boolean }).network)).toBe(true);+ expect(calls).toBe(1);+ } finally {+ globalThis.fetch = real;+ }+ });++ test('pagedJobs lists a 98-job attempt GitHub refuses to render 100 jobs at a time (a 502 on every per_page=100 page, runs 39059772114 / 39103163273)', async () => {+ const all = Array.from({ length: 98 }, (_, i) => ({ id: i + 1, name: `job ${i + 1}`, conclusion: 'success' }));+ const seen: string[] = [];+ const client: GitHubClient = {+ async request<T>(_method: string, path: string): Promise<T> {+ seen.push(path);+ const q = new URLSearchParams(path.split('?')[1]);+ const perPage = Number(q.get('per_page')), page = Number(q.get('page'));+ if (perPage > 50) throw Object.assign(new Error('GitHub API GET -> 502: server error'), { status: 502 });+ return { jobs: all.slice((page - 1) * perPage, page * perPage), total_count: all.length } as T;+ },+ };+ const run = { id: 39052886222, run_attempt: 2 } as RunInfo;+ const { jobs, complete } = await pagedJobs(client, run);+ expect({ count: jobs.length, complete }).toEqual({ count: 98, complete: true });+ expect(seen.every(p => p.startsWith('repos/{repo}/actions/runs/39052886222/attempts/2/jobs?'))).toBe(true);+ });+test('more than three failures are all listed; a database URL in run text is redacted and never reaches a reproduce command', async () => {const run: RunInfo = { ...T.r8320, id: 999001, run_number: 8321, head_sha: 'b'.repeat(40) };const w = testWorld(T.g8315, T.r8320);