Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
ci: weekly CI health report
garrytanced0bb68c8e5
diff --git a/.github/workflows/ci-health.yml b/.github/workflows/ci-health.ymlnew file mode 100644index 0000000000..5d188cc8d0--- /dev/null+++ b/.github/workflows/ci-health.yml@@ -0,0 +1,50 @@+name: CI health++# Weekly CI health report (scripts/ci-health-report.ts): first-attempt pass+# rate of same-repository PR runs, the test files with the most failure+# annotations, shard spread with the weights miner command, new serial files+# and nightly-watch's own status, upserted as one `ci-health` issue per week.+# Reads runs, jobs and check-run annotations only (no logs, no fork runs or+# artifacts). nightly-watch watches this workflow like any scheduled one.++on:+ schedule:+ - cron: '0 10 * * 1' # weekly, Monday 10:00 UTC+ workflow_dispatch:+ inputs:+ dry_run:+ description: Print the report instead of writing the issue+ type: boolean+ default: true++permissions: {}++concurrency:+ group: ci-health+ cancel-in-progress: false++jobs:+ report:+ runs-on: ubicloud-standard-2-ubuntu-2404+ timeout-minutes: 15+ permissions:+ contents: read+ issues: write+ actions: read+ checks: read+ steps:+ - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4+ with:+ persist-credentials: false+ # git history for the "serial files added this week" line.+ fetch-depth: 0+ - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2+ with:+ bun-version: 1.4.2+ - name: Upsert the weekly CI health issue+ env:+ GH_TOKEN: ${{ github.token }}+ DRY_RUN: ${{ inputs.dry_run && '--dry-run' || '' }}+ run: |+ # shellcheck disable=SC2086 # DRY_RUN is empty or one flag+ bun scripts/ci-health-report.ts --repo "$GITHUB_REPOSITORY" $DRY_RUNdiff --git a/scripts/ci-health-report.ts b/scripts/ci-health-report.tsnew file mode 100644index 0000000000..6db4182e9f--- /dev/null+++ b/scripts/ci-health-report.ts@@ -0,0 +1,243 @@+#!/usr/bin/env bun+/**+ * Weekly CI health report (GBRA-47 V-6, ENG-14).+ *+ * bun scripts/ci-health-report.ts [--days 7] [--repo owner/name] [--dry-run]+ *+ * Reads the last N days of same-repository pull-request runs of test.yml and+ * e2e.yml through the REST API (runs, jobs and check-run annotations only;+ * never logs, never fork runs or fork artifacts) and upserts one issue titled+ * `CI health (week of <date>)` (label `ci-health`) with:+ *+ * - first-attempt pass rate per workflow (a run that needed a re-run failed+ * its first attempt);+ * - the test files with the most failure annotations across failed runs;+ * - unit and Selected E2E shard spread (slowest shard / mean, per run) with+ * the weights miner command when it passes 1.25;+ * - serial files added in the window (`*.serial.test.ts`, from git history);+ * - nightly-watch's own latest status.+ *+ * API calls are capped (pages of runs, failed runs sampled for annotations,+ * runs sampled for shard spread); any cap that truncated data is stated in+ * the issue. --dry-run reads only and prints the issue.+ */+import { readdirSync, statSync } from 'node:fs';+import { join, relative, resolve } from 'node:path';+import { commentIssue, createIssue, ensureLabel, findIssueByTitle, inert, jsonBlock, restClient, updateIssue, type GitHubClient } from './lib/gh-issue.ts';++const ROOT = resolve(import.meta.dir, '..');+export const LABEL = 'ci-health';+export const SPREAD_LIMIT = 1.25;+export const CAPS = { runPages: 3, failedRunsSampled: 15, spreadRunsSampled: 10 };+const WORKFLOWS = [+ { file: 'test.yml', shard: /^test \(\d+\)$/, lane: 'unit' },+ { file: 'e2e.yml', shard: /^Selected E2E \(diff-relevant\)/, lane: 'e2e' },+] as const;++export interface Run {+ id: number;+ event: string;+ status: string;+ conclusion: string | null;+ run_attempt: number;+ created_at: string;+ head_repository?: { full_name: string } | null;+ repository?: { full_name: string };+}+export interface Job { id: number; name: string; conclusion: string | null; started_at?: string | null; completed_at?: string | null }+export interface Annotation { annotation_level: string; path?: string; title?: string | null; message: string }++export interface WorkflowHealth {+ workflow: string;+ runs: number;+ fork_runs_excluded: number;+ first_attempt_pass: number;+ first_attempt_rate: number | null;+ spread: { runs: number; mean_ratio: number | null; worst_ratio: number | null };+ miner: string | null;+}+export interface HealthData {+ since: string;+ workflows: WorkflowHealth[];+ failing_files: Array<{ file: string; count: number; sample: string }>;+ serial_added: string[] | null;+ serial_total: number;+ nightly_watch: string;+ truncated: string[];+}++export function sameRepo(run: Run): boolean {+ return !run.head_repository || !run.repository || run.head_repository.full_name === run.repository.full_name;+}++export function firstAttempt(runs: Run[]): { runs: number; pass: number; rate: number | null } {+ const done = runs.filter(r => r.status === 'completed' && r.conclusion !== 'cancelled' && r.conclusion !== 'skipped');+ const pass = done.filter(r => r.conclusion === 'success' && r.run_attempt === 1).length;+ return { runs: done.length, pass, rate: done.length ? pass / done.length : null };+}++/** Slowest shard / mean shard duration for one run's shard jobs; null under two timed shards. */+export function shardRatio(jobs: Job[], shard: RegExp): number | null {+ const secs = jobs.filter(j => shard.test(j.name) && j.started_at && j.completed_at)+ .map(j => (Date.parse(j.completed_at!) - Date.parse(j.started_at!)) / 1000).filter(s => s > 0);+ if (secs.length < 2) return null;+ const mean = secs.reduce((a, b) => a + b, 0) / secs.length;+ return Math.max(...secs) / mean;+}++export function topFailingFiles(annotations: Annotation[], limit = 10): HealthData['failing_files'] {+ const counts = new Map<string, { count: number; sample: string }>();+ for (const a of annotations) {+ if (a.annotation_level !== 'failure' || !a.path || !/\.test\.ts$/.test(a.path)) continue;+ const entry = counts.get(a.path) ?? { count: 0, sample: inert(a.title || a.message, 160) };+ entry.count++;+ counts.set(a.path, entry);+ }+ return [...counts].map(([file, v]) => ({ file: inert(file, 160), ...v })).sort((a, b) => b.count - a.count || a.file.localeCompare(b.file)).slice(0, limit);+}++export function serialFiles(dir = join(ROOT, 'test')): string[] {+ const out: string[] = [];+ for (const name of readdirSync(dir)) {+ const path = join(dir, name);+ if (statSync(path).isDirectory()) out.push(...serialFiles(path));+ else if (name.endsWith('.serial.test.ts')) out.push(relative(ROOT, path));+ }+ return out.sort();+}++/** Serial test files added since the date, or null when the clone is shallow (history unknown). */+export function serialAddedSince(since: string): string[] | null {+ const shallow = Bun.spawnSync(['git', 'rev-parse', '--is-shallow-repository'], { cwd: ROOT, stdout: 'pipe' }).stdout.toString().trim();+ if (shallow !== 'false') return null;+ const r = Bun.spawnSync(['git', 'log', `--since=${since}`, '--diff-filter=A', '--name-only', '--format=', '--', '*.serial.test.ts'], { cwd: ROOT, stdout: 'pipe' });+ return [...new Set(r.stdout.toString().split('\n').filter(Boolean))].sort();+}++const pct = (n: number | null) => (n === null ? 'n/a' : `${Math.round(n * 100)}%`);+const ratio = (n: number | null) => (n === null ? 'n/a' : n.toFixed(2));++export function renderHealth(data: HealthData): string {+ const steps: string[] = [];+ for (const f of data.failing_files.slice(0, 3)) steps.push(`Fix the flake or regression in \`${f.file}\` (${f.count} failure annotations this week); reproduce with \`bun test ${f.file}\`.`);+ for (const w of data.workflows) {+ if (w.miner && (w.spread.mean_ratio ?? 0) > SPREAD_LIMIT) steps.push(`Shards in ${w.workflow} are unbalanced (mean slowest/mean ${ratio(w.spread.mean_ratio)} > ${SPREAD_LIMIT}): \`${w.miner}\`, then commit the weights.`);+ }+ if (data.serial_added?.length) steps.push(`Review the ${data.serial_added.length} new serial file(s) below: each needs a reason it cannot run in parallel (\`bash scripts/check-test-isolation.sh <file>\`).`);+ if (data.nightly_watch !== 'success') steps.push(`nightly-watch's latest run is \`${inert(data.nightly_watch, 40)}\`: read it with \`gh run list --workflow nightly-watch.yml --limit 3\` before trusting nightly issues.`);+ const lines = [+ `Same-repository pull-request runs since ${data.since}.`,+ '',+ '| workflow | completed runs | first-attempt pass | shard spread (mean / worst slowest-to-mean) | fork runs not read |',+ '|---|---|---|---|---|',+ ...data.workflows.map(w => `| ${w.workflow} | ${w.runs} | ${w.first_attempt_pass} (${pct(w.first_attempt_rate)}) | ${ratio(w.spread.mean_ratio)} / ${ratio(w.spread.worst_ratio)} over ${w.spread.runs} runs | ${w.fork_runs_excluded} |`),+ '',+ '**Most failure annotations by test file** (failed runs sampled):',+ ...(data.failing_files.length ? data.failing_files.map(f => `- \`${f.file}\`: ${f.count} (e.g. \`${f.sample}\`)`) : ['- none']),+ '',+ `**Serial files:** ${data.serial_total} total; added in the window: ${data.serial_added === null ? 'unknown (shallow clone; the workflow checks out full history)'+ : data.serial_added.length ? `${data.serial_added.slice(0, 20).map(f => `\`${inert(f)}\``).join(', ')}${data.serial_added.length > 20 ? ` and ${data.serial_added.length - 20} more` : ''}` : 'none'}.`,+ `**nightly-watch latest run:** ${inert(data.nightly_watch, 40)}.`,+ ...(data.truncated.length ? ['', `**Truncated:** ${data.truncated.join('; ')}.`] : []),+ '',+ '### Next step for the agent',+ '',+ ...(steps.length ? steps.map((s, i) => `${i + 1}. ${s}`) : ['Nothing to do this week.']),+ '',+ jsonBlock('ci-health:json', data),+ '',+ ];+ return lines.join('\n');+}++export async function collectHealth(client: GitHubClient, since: string): Promise<HealthData> {+ const truncated: string[] = [];+ const workflows: WorkflowHealth[] = [];+ const annotations: Annotation[] = [];+ for (const wf of WORKFLOWS) {+ const runs: Run[] = [];+ for (let page = 1; page <= CAPS.runPages; page++) {+ const r = await client.request<{ workflow_runs: Run[] }>('GET', `repos/{repo}/actions/workflows/${wf.file}/runs?event=pull_request&created=${encodeURIComponent(`>=${since}`)}&per_page=100&page=${page}`);+ runs.push(...r.workflow_runs);+ if (r.workflow_runs.length < 100) break;+ if (page === CAPS.runPages) truncated.push(`${wf.file}: only the newest ${CAPS.runPages * 100} runs read`);+ }+ const mine = runs.filter(sameRepo);+ const fa = firstAttempt(mine);+ const failed = mine.filter(r => r.conclusion === 'failure' || r.run_attempt > 1);+ if (failed.length > CAPS.failedRunsSampled) truncated.push(`${wf.file}: annotations from ${CAPS.failedRunsSampled} of ${failed.length} failed runs`);+ const spreadRuns = mine.filter(r => r.conclusion === 'success').slice(0, CAPS.spreadRunsSampled);+ const ratios: number[] = [];+ const jobsOf = (run: Run, attempt: number) => client.request<{ jobs: Job[] }>('GET', `repos/{repo}/actions/runs/${run.id}/attempts/${attempt}/jobs?per_page=100`).then(r => r.jobs);+ for (const run of spreadRuns) {+ const r = shardRatio(await jobsOf(run, run.run_attempt), wf.shard);+ if (r !== null) ratios.push(r);+ }+ for (const run of failed.slice(0, CAPS.failedRunsSampled)) {+ const jobs = await jobsOf(run, 1);+ for (const job of jobs.filter(j => j.conclusion === 'failure').slice(0, 3)) {+ annotations.push(...await client.request<Annotation[]>('GET', `repos/{repo}/check-runs/${job.id}/annotations?per_page=50`).catch(() => []));+ }+ }+ const latestGreen = await client.request<{ workflow_runs: Array<{ id: number }> }>('GET', `repos/{repo}/actions/workflows/${wf.file}/runs?event=push&status=success&per_page=1`)+ .catch(() => ({ workflow_runs: [] }));+ const greenId = latestGreen.workflow_runs[0]?.id;+ workflows.push({+ workflow: wf.file,+ runs: fa.runs,+ fork_runs_excluded: runs.length - mine.length,+ first_attempt_pass: fa.pass,+ first_attempt_rate: fa.rate,+ spread: { runs: ratios.length, mean_ratio: ratios.length ? ratios.reduce((a, b) => a + b, 0) / ratios.length : null, worst_ratio: ratios.length ? Math.max(...ratios) : null },+ miner: greenId ? `bun run weights:mine --lane ${wf.lane} --run ${greenId}` : null,+ });+ }+ const watch = await client.request<{ workflow_runs: Array<{ conclusion: string | null; status: string }> }>('GET', 'repos/{repo}/actions/workflows/nightly-watch.yml/runs?per_page=1')+ .catch(() => ({ workflow_runs: [] }));+ const latest = watch.workflow_runs[0];+ return {+ since,+ workflows,+ failing_files: topFailingFiles(annotations),+ serial_added: serialAddedSince(since),+ serial_total: serialFiles().length,+ nightly_watch: latest ? (latest.conclusion ?? latest.status) : 'no runs yet',+ truncated,+ };+}++if (import.meta.main) {+ const flag = (name: string) => { const i = process.argv.indexOf(name); return i >= 0 ? process.argv[i + 1] : undefined; };+ const days = Number(flag('--days') ?? 7);+ const repo = flag('--repo') ?? process.env.GITHUB_REPOSITORY ?? 'garrytan/gbrain';+ const dryRun = process.argv.includes('--dry-run');+ if (!Number.isInteger(days) || days < 1 || days > 31) {+ console.log('Usage: bun scripts/ci-health-report.ts [--days 1-31] [--repo owner/name] [--dry-run]');+ process.exit(2);+ }+ const now = new Date();+ const since = new Date(now.getTime() - days * 86_400_000).toISOString().slice(0, 10);+ try {+ const client = restClient(repo);+ const data = await collectHealth(client, since);+ const body = renderHealth(data);+ const title = `CI health (week of ${since})`;+ if (dryRun) {+ console.log(`--- title: ${title}\n--- body:\n${body}`);+ process.exit(0);+ }+ await ensureLabel(client, LABEL, '0e8a16', 'Weekly CI health report (scripts/ci-health-report.ts)');+ const existing = await findIssueByTitle(client, LABEL, title);+ if (existing) {+ await updateIssue(client, existing.number, { body, state: 'open' });+ await commentIssue(client, existing.number, 'Report refreshed.');+ console.log(`[ci-health] updated #${existing.number}`);+ } else {+ console.log(`[ci-health] opened #${(await createIssue(client, title, body, [LABEL])).number}`);+ }+ } catch (e) {+ console.log(`[ci-health] FAIL: ${e instanceof Error ? e.message : String(e)}`);+ console.log('[ci-health] Fix: check the token scopes (actions:read, checks:read, issues:write) and rerun: bun scripts/ci-health-report.ts --dry-run');+ process.exit(1);+ }+}diff --git a/test/scripts/ci-health-report.test.ts b/test/scripts/ci-health-report.test.tsnew file mode 100644index 0000000000..848ff7f119--- /dev/null+++ b/test/scripts/ci-health-report.test.ts@@ -0,0 +1,74 @@+/**+ * scripts/ci-health-report.ts (GBRA-47 V-6, ENG-14): same-repo runs only,+ * first-attempt pass rate, shard spread with the miner command, failing test+ * files from annotations (inert), capped pagination stated as truncation.+ */+import { describe, expect, test } from 'bun:test';+import type { GitHubClient } from '../../scripts/lib/gh-issue.ts';+import { CAPS, collectHealth, firstAttempt, renderHealth, shardRatio, topFailingFiles, type Annotation, type Job, type Run } from '../../scripts/ci-health-report.ts';++const repo = { full_name: 'garrytan/gbrain' };+const run = (id: number, conclusion: string, attempt = 1, fork = false): Run => ({+ id, event: 'pull_request', status: 'completed', conclusion, run_attempt: attempt, created_at: '2026-10-01T00:00:00Z',+ repository: repo, head_repository: fork ? { full_name: 'someone/gbrain' } : repo,+});+const shard = (id: number, name: string, secs: number, conclusion = 'success'): Job => ({+ id, name, conclusion, started_at: '2026-10-01T00:00:00Z', completed_at: new Date(Date.parse('2026-10-01T00:00:00Z') + secs * 1000).toISOString(),+});++describe('ci-health-report', () => {+ test('first-attempt pass counts only runs that passed without a re-run; cancelled runs are ignored', () => {+ expect(firstAttempt([run(1, 'success'), run(2, 'success', 2), run(3, 'failure'), run(4, 'cancelled')])).toEqual({ runs: 3, pass: 1, rate: 1 / 3 });+ });++ test('shard ratio is slowest over mean among matching jobs', () => {+ const jobs = [shard(1, 'test (1)', 100), shard(2, 'test (2)', 200), shard(3, 'verify', 900)];+ expect(shardRatio(jobs, /^test \(\d+\)$/)).toBeCloseTo(200 / 150);+ expect(shardRatio([shard(1, 'test (1)', 100)], /^test \(\d+\)$/)).toBeNull();+ });++ test('failing test files are counted from failure annotations and rendered inert', () => {+ const anns: Annotation[] = [+ { annotation_level: 'failure', path: 'test/a.test.ts', title: 'error: boom @someone https://evil.example', message: 'x' },+ { annotation_level: 'failure', path: 'test/a.test.ts', title: 'error: boom', message: 'x' },+ { annotation_level: 'failure', path: 'test/b.test.ts', message: 'Process completed with exit code 1.' },+ { annotation_level: 'warning', path: 'test/c.test.ts', message: 'Node.js 20 is deprecated' },+ { annotation_level: 'failure', message: 'Process completed with exit code 1.' },+ ];+ const top = topFailingFiles(anns);+ expect(top.map(t => [t.file, t.count])).toEqual([['test/a.test.ts', 2], ['test/b.test.ts', 1]]);+ expect(top[0]!.sample).not.toMatch(/@someone|https:\/\/evil/);+ });++ test('collect reads same-repo runs only, samples jobs and annotations, names the miner command and states truncation', async () => {+ const calls: string[] = [];+ const page = (n: number) => Array.from({ length: n }, (_, i) => run(1000 + i, i % 4 === 0 ? 'failure' : 'success', 1, i % 10 === 9));+ const client: GitHubClient = {+ async request<T>(_m: string, path: string): Promise<T> {+ calls.push(path);+ if (path.includes('/workflows/nightly-watch.yml/runs')) return { workflow_runs: [{ conclusion: 'success', status: 'completed' }] } as T;+ if (path.includes('event=push&status=success')) return { workflow_runs: [{ id: 555 }] } as T;+ if (path.includes('/workflows/test.yml/runs')) return { workflow_runs: page(100) } as T;+ if (path.includes('/workflows/e2e.yml/runs')) return { workflow_runs: page(3) } as T;+ if (path.includes('/jobs')) return { jobs: [shard(1, 'test (1)', 100), shard(2, 'test (2)', 300, 'failure'), shard(3, 'Selected E2E (diff-relevant) 1', 50)] } as T;+ if (path.includes('/annotations')) return [{ annotation_level: 'failure', path: 'test/flaky.test.ts', title: 'error: timeout', message: '' }] as T;+ throw new Error(`unexpected ${path}`);+ },+ };+ const data = await collectHealth(client, '2026-09-27');+ const unit = data.workflows.find(w => w.workflow === 'test.yml')!;+ expect(unit.fork_runs_excluded).toBe(CAPS.runPages * 10);+ expect(unit.runs).toBe(CAPS.runPages * 90);+ expect(unit.miner).toBe('bun run weights:mine --lane unit --run 555');+ expect(unit.spread.mean_ratio).toBeCloseTo(1.5);+ expect(data.truncated.join(' ')).toContain('test.yml: only the newest 300 runs read');+ expect(data.truncated.join(' ')).toContain(`annotations from ${CAPS.failedRunsSampled} of`);+ expect(data.failing_files[0]!.file).toBe('test/flaky.test.ts');+ expect(calls.every(c => !c.includes('/logs'))).toBe(true);+ expect(calls.some(c => c.includes('created=%3E%3D2026-09-27'))).toBe(true);+ const body = renderHealth(data);+ expect(body).toContain('`bun run weights:mine --lane unit --run 555`');+ expect(body).toContain('**Truncated:**');+ expect(body).toContain('### Next step for the agent');+ });+});