Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

release-gate: record the full-suite run each workflow reused

garrytan29b6724e89bf
diff --git a/scripts/release-gate.ts b/scripts/release-gate.ts
index a1963b5afe..023ea0ed04 100644
--- a/scripts/release-gate.ts
+++ b/scripts/release-gate.ts
@@ -23,20 +23,34 @@
* A workflow_dispatch backfill runs the same gate for master's HEAD, so it
* publishes the current VERSION once master's CI for HEAD is green. Writes
* `publish=true|false` and `sha=<commit>` to --output (GITHUB_OUTPUT).
+ *
+ * Master-push reuse (scripts/ci-reuse.ts, docs/TESTING.md#master-push-reuse):
+ * a green push run whose latest attempt has a succeeded `reused-full-suite`
+ * job reused a pull request run's full suite. On publish the gate reads that
+ * push run's `reused-full-suite` artifact (reuse.json, written by
+ * ci-reuse.ts --record), prints the reused run per workflow and writes
+ * `test_reused_run_id` / `e2e_reused_run_id` (empty when the push ran in
+ * full). A reused job whose record is missing or names another workflow or
+ * commit publishes nothing.
* Exit 0 publish or superseded, 1 no publish (failed, timed out, not master),
* 2 usage. Docs: docs/RELEASING.md#release-ci-gate
*/
import { appendFileSync } from 'node:fs';
-import { restClient, type GitHubClient } from './lib/gh-issue.ts';
+import { attemptJobs, checkRecord, REUSE_ARTIFACT, REUSE_FILE, REUSE_JOB } from './ci-reuse.ts';
+import { fetchArtifactJson, restClient, type GitHubClient } from './lib/gh-issue.ts';
-export const GATED = [{ file: 'test.yml', name: 'Test' }, { file: 'e2e.yml', name: 'E2E Tests' }] as const;
+export const GATED = [{ file: 'test.yml', name: 'Test', key: 'test' }, { file: 'e2e.yml', name: 'E2E Tests', key: 'e2e' }] as const;
export const DOCS = 'docs/RELEASING.md#release-ci-gate';
const RED = new Set(['failure', 'timed_out', 'startup_failure', 'action_required']);
const MAX_HOPS = 10;
export interface GateRun { id: number; run_number: number; run_attempt: number; status: string; conclusion: string | null; head_sha: string; head_branch: string; event: string; html_url?: string }
+export interface ReusedRun { run_id: number; run_attempt: number; sha: string }
+/** Whether a green push run reused a pull request run's full suite (`problem`: it did, but its record cannot name the run). */
+export type Provenance = { reused: false } | ({ reused: true } & ReusedRun) | { problem: string };
+export interface Source { workflow: string; key: string; run_id: number; reused?: ReusedRun }
export type Verdict =
- | { kind: 'publish'; sha: string; why: string }
+ | { kind: 'publish'; sha: string; why: string; sources: Source[] }
| { kind: 'superseded'; sha: string; why: string }
| { kind: 'wait'; sha: string; why: string }
| { kind: 'fail'; sha: string; why: string; next: string };
@@ -46,6 +60,7 @@ export interface GateDeps {
newerRuns(file: string, after: number): Promise<GateRun[]>;
versionAt(sha: string): Promise<string>;
contains(base: string, head: string): Promise<boolean>;
+ provenance(file: string, run: GateRun): Promise<Provenance>;
}
const newest = (runs: GateRun[]) => [...runs].sort((a, b) => b.run_number - a.run_number || b.run_attempt - a.run_attempt)[0];
@@ -77,7 +92,18 @@ export async function evaluate(deps: GateDeps, sha: string, version: string): Pr
}
const pending = runs.filter(r => !r.run || r.run.status !== 'completed');
if (pending.length) return { kind: 'wait', sha: candidate, why: pending.map(r => `${r.wf.name} ${r.run ? `run ${r.run.id} is ${r.run.status}` : 'has no push run yet'} on ${candidate.slice(0, 12)}`).join('; ') };
- return { kind: 'publish', sha: candidate, why: `Test and E2E Tests both succeeded on ${candidate.slice(0, 12)}${candidate === sha ? '' : ` (the newer master commit that replaced the cancelled runs of ${sha.slice(0, 12)}; same VERSION)`}` };
+ const proven = await Promise.all(runs.map(async r => ({ wf: r.wf, run: r.run!, prov: await deps.provenance(r.wf.file, r.run!) })));
+ const unproven = proven.find(p => 'problem' in p.prov);
+ if (unproven && 'problem' in unproven.prov) {
+ return {
+ kind: 'fail', sha: candidate,
+ why: `${unproven.wf.name} run ${unproven.run.id} reused an earlier full-suite run on ${candidate.slice(0, 12)}, but its reuse record cannot name that run (${unproven.prov.problem}), so v${version} is not published. A release names the run that proved its tree.`,
+ next: `Re-run that push run (gh run rerun ${unproven.run.id}); it decides reuse again and records it, or runs the full suite. Once it is green: gh workflow run release.yml --ref master`,
+ };
+ }
+ const sources: Source[] = proven.map(p => ({ workflow: p.wf.name, key: p.wf.key, run_id: p.run.id, ...('run_id' in p.prov ? { reused: { run_id: p.prov.run_id, run_attempt: p.prov.run_attempt, sha: p.prov.sha } } : {}) }));
+ const how = sources.map(s => s.reused ? `${s.workflow} reused full-suite run ${s.reused.run_id} attempt ${s.reused.run_attempt} (pull request head ${s.reused.sha.slice(0, 12)}, identical tree)` : `${s.workflow} ran in full in run ${s.run_id}`).join('; ');
+ return { kind: 'publish', sha: candidate, sources, why: `Test and E2E Tests both succeeded on ${candidate.slice(0, 12)}${candidate === sha ? '' : ` (the newer master commit that replaced the cancelled runs of ${sha.slice(0, 12)}; same VERSION)`}: ${how}` };
}
return { kind: 'fail', sha: candidate, why: `followed ${MAX_HOPS} cancelled runs without a completed one`, next: 'Wait for master to settle, then: gh workflow run release.yml --ref master' };
}
@@ -99,9 +125,24 @@ export function apiDeps(client: GitHubClient): GateDeps {
const cmp = await client.request<{ status: string }>('GET', `repos/{repo}/compare/${base}...${head}`);
return cmp.status === 'ahead' || cmp.status === 'identical';
},
+ async provenance(file, run) {
+ const name = GATED.find(g => g.file === file)?.name ?? file;
+ const marker = (await attemptJobs(client, run.id, run.run_attempt)).filter(j => j.name === REUSE_JOB);
+ if (marker.length === 0 || marker.every(j => j.conclusion === 'skipped')) return { reused: false };
+ if (marker.length > 1 || marker[0]!.conclusion !== 'success') return { problem: `run ${run.id}'s ${REUSE_JOB} job concluded ${marker.map(j => j.conclusion).join(', ')}` };
+ const got = await fetchArtifactJson(client, run.id, REUSE_ARTIFACT, REUSE_FILE);
+ if (got.reason) return { problem: got.reason };
+ const rec = checkRecord(got.value, name, run.head_sha);
+ return typeof rec === 'string' ? { problem: rec } : { reused: true, ...rec };
+ },
};
}
+/** GITHUB_OUTPUT lines naming the run each workflow reused (empty when the push ran in full). */
+export function reuseOutputs(sources: Source[]): string {
+ return GATED.map(g => `${g.key}_reused_run_id=${sources.find(s => s.key === g.key)?.reused?.run_id ?? ''}\n`).join('');
+}
+
export async function gate(deps: GateDeps, opts: { sha: string; version: string; timeoutMs: number; pollMs: number; log: (line: string) => void; sleep?: (ms: number) => Promise<void>; now?: () => number }): Promise<Verdict> {
const now = opts.now ?? Date.now;
const sleep = opts.sleep ?? (ms => new Promise(r => setTimeout(r, ms)));
@@ -141,6 +182,7 @@ if (import.meta.main) {
if (v.kind === 'publish') {
console.log(`[release-gate] publish v${version} at ${v.sha}: ${v.why}.`);
write(true, v.sha);
+ if (output) appendFileSync(output, reuseOutputs(v.sources));
} else if (v.kind === 'superseded') {
console.log(`::notice::Release v${version} skipped: ${v.why}. Nothing to do. Docs: ${DOCS}`);
write(false, v.sha);
diff --git a/test/scripts/release-gate.test.ts b/test/scripts/release-gate.test.ts
index df624974f7..15f9ffd3c1 100644
--- a/test/scripts/release-gate.test.ts
+++ b/test/scripts/release-gate.test.ts
@@ -9,15 +9,17 @@
import { describe, expect, test } from 'bun:test';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
+import { deflateRawSync } from 'node:zlib';
import { load } from 'js-yaml';
-import { evaluate, gate, GATED, type GateDeps, type GateRun } from '../../scripts/release-gate.ts';
+import { apiDeps, evaluate, gate, GATED, reuseOutputs, type GateDeps, type GateRun, type Provenance } from '../../scripts/release-gate.ts';
+import type { GitHubClient } from '../../scripts/lib/gh-issue.ts';
const A = 'a'.repeat(40);
const B = 'b'.repeat(40);
const C = 'c'.repeat(40);
const run = (id: number, n: number, sha: string, status: string, conclusion: string | null, attempt = 1): GateRun => ({ id, run_number: n, run_attempt: attempt, status, conclusion, head_sha: sha, head_branch: 'master', event: 'push' });
-function deps(runs: Record<string, GateRun[]>, versions: Record<string, string> = {}, outside: string[] = []): GateDeps & { calls: string[] } {
+function deps(runs: Record<string, GateRun[]>, versions: Record<string, string> = {}, outside: string[] = [], reused: Record<number, Provenance> = {}): GateDeps & { calls: string[] } {
const calls: string[] = [];
return {
calls,
@@ -25,6 +27,7 @@ function deps(runs: Record<string, GateRun[]>, versions: Record<string, string>
async newerRuns(file, after) { return (runs[file] ?? []).filter(r => r.run_number > after); },
async versionAt(sha) { return versions[sha] ?? '0.60.70.0'; },
async contains(_base, head) { return !outside.includes(head); },
+ async provenance(_file, r) { return reused[r.id] ?? { reused: false }; },
};
}
@@ -72,6 +75,28 @@ describe('release gate', () => {
expect((await evaluate(stray, A, '0.60.70.0')).kind).toBe('fail');
});
+ test('a publish names the full-suite run each workflow reused, and writes both ids', async () => {
+ const d = deps({ 'test.yml': [run(1, 10, A, 'completed', 'success')], 'e2e.yml': [run(2, 20, A, 'completed', 'success')] }, {}, [], { 1: { reused: true, run_id: 900, run_attempt: 2, sha: B } });
+ const v = await evaluate(d, A, '0.60.70.0');
+ expect(v).toMatchObject({ kind: 'publish', sha: A, sources: [{ workflow: 'Test', run_id: 1, reused: { run_id: 900, run_attempt: 2, sha: B } }, { workflow: 'E2E Tests', run_id: 2 }] });
+ expect(v.why).toContain('Test reused full-suite run 900 attempt 2');
+ expect(v.why).toContain('E2E Tests ran in full in run 2');
+ if (v.kind !== 'publish') return;
+ expect(reuseOutputs(v.sources)).toBe('test_reused_run_id=900\ne2e_reused_run_id=\n');
+ });
+
+ test('a reused push run whose reuse record cannot be read publishes nothing and names the re-run', async () => {
+ const d = deps({ 'test.yml': [run(1, 10, A, 'completed', 'success')], 'e2e.yml': [run(2, 20, A, 'completed', 'success')] }, {}, [], { 2: { problem: "run 2 has no 'reused-full-suite' artifact" } });
+ const v = await evaluate(d, A, '0.60.70.0');
+ expect(v.kind).toBe('fail');
+ if (v.kind !== 'fail') return;
+ expect(v.why).toContain('E2E Tests run 2');
+ expect(v.why).toContain("no 'reused-full-suite' artifact");
+ expect(v.why).toContain('v0.60.70.0 is not published');
+ expect(v.next).toContain('gh run rerun 2');
+ expect(v.next).toContain('gh workflow run release.yml --ref master');
+ });
+
test('the poller waits while CI runs and fails with the backfill command at its deadline (dispatch backfill uses the same gate)', async () => {
let t = 0;
const lines: string[] = [];
@@ -80,6 +105,7 @@ describe('release gate', () => {
const d: GateDeps = {
async runsFor(file) { return file === 'test.yml' ? [states[Math.min(i++, 2)]!] : [run(2, 20, A, 'completed', 'success')]; },
async newerRuns() { return []; }, async versionAt() { return '0.60.70.0'; }, async contains() { return true; },
+ async provenance() { return { reused: false }; },
};
const v = await gate(d, { sha: A, version: '0.60.70.0', timeoutMs: 10 * 60_000, pollMs: 60_000, log: l => lines.push(l), sleep: async ms => { t += ms; }, now: () => t });
expect(v.kind).toBe('publish');
@@ -92,6 +118,60 @@ describe('release gate', () => {
});
});
+/** A one-entry zip, enough for readZipEntry. */
+function zipOf(name: string, content: string): Uint8Array {
+ const data = Buffer.from(content);
+ const body = deflateRawSync(data);
+ const file = Buffer.from(name);
+ const local = Buffer.alloc(30);
+ local.writeUInt32LE(0x04034b50, 0); local.writeUInt16LE(8, 8); local.writeUInt32LE(body.length, 18); local.writeUInt32LE(data.length, 22); local.writeUInt16LE(file.length, 26);
+ const central = Buffer.alloc(46);
+ central.writeUInt32LE(0x02014b50, 0); central.writeUInt16LE(8, 10); central.writeUInt32LE(body.length, 20); central.writeUInt32LE(data.length, 24); central.writeUInt16LE(file.length, 28); central.writeUInt32LE(0, 42);
+ const end = Buffer.alloc(22);
+ end.writeUInt32LE(0x06054b50, 0); end.writeUInt16LE(1, 8); end.writeUInt16LE(1, 10); end.writeUInt32LE(46 + file.length, 12); end.writeUInt32LE(30 + file.length + body.length, 16);
+ return new Uint8Array(Buffer.concat([local, file, body, central, file, end]));
+}
+
+describe('release gate reads reuse provenance from the push run', () => {
+ const client = (jobs: Array<{ name: string; conclusion: string | null }>, record?: unknown): GitHubClient & { paths: string[] } => {
+ const paths: string[] = [];
+ return {
+ paths,
+ async request<T>(_m: string, path: string): Promise<T> {
+ paths.push(path);
+ if (path.includes('/attempts/3/jobs')) return { total_count: jobs.length, jobs } as T;
+ if (path.includes('/artifacts?name=reused-full-suite')) return { artifacts: record === undefined ? [] : [{ id: 77, name: 'reused-full-suite', expired: false }] } as T;
+ throw new Error(`unexpected ${path}`);
+ },
+ async download() { return zipOf('reuse.json', JSON.stringify(record)); },
+ };
+ };
+ const pushRun = run(5, 50, A, 'completed', 'success', 3);
+ const rec = { reuse: true, workflow: 'Test', push_sha: A, tree: C, source_run_id: 900, source_run_attempt: 1, source_sha: B };
+
+ test('a succeeded reused-full-suite job in the latest attempt yields the validated source run', async () => {
+ const c = client([{ name: 'test-status', conclusion: 'success' }, { name: 'reused-full-suite', conclusion: 'success' }], rec);
+ expect(await apiDeps(c).provenance('test.yml', pushRun)).toEqual({ reused: true, run_id: 900, run_attempt: 1, sha: B });
+ expect(c.paths[0]).toBe('repos/{repo}/actions/runs/5/attempts/3/jobs?per_page=100&page=1');
+ });
+
+ test('no reused-full-suite job, or a skipped one, means the push ran in full', async () => {
+ expect(await apiDeps(client([{ name: 'test-status', conclusion: 'success' }])).provenance('test.yml', pushRun)).toEqual({ reused: false });
+ expect(await apiDeps(client([{ name: 'reused-full-suite', conclusion: 'skipped' }])).provenance('test.yml', pushRun)).toEqual({ reused: false });
+ });
+
+ test('a reused job without a valid record for this workflow and push is a problem, never a full run', async () => {
+ const missing = await apiDeps(client([{ name: 'reused-full-suite', conclusion: 'success' }])).provenance('test.yml', pushRun);
+ expect('problem' in missing && missing.problem).toContain("no 'reused-full-suite' artifact");
+ const otherPush = await apiDeps(client([{ name: 'reused-full-suite', conclusion: 'success' }], { ...rec, push_sha: B })).provenance('test.yml', pushRun);
+ expect('problem' in otherPush && otherPush.problem).toContain('not ' + A);
+ const otherWorkflow = await apiDeps(client([{ name: 'reused-full-suite', conclusion: 'success' }], rec)).provenance('e2e.yml', pushRun);
+ expect('problem' in otherWorkflow && otherWorkflow.problem).toContain('not E2E Tests');
+ const failed = await apiDeps(client([{ name: 'reused-full-suite', conclusion: 'failure' }], rec)).provenance('test.yml', pushRun);
+ expect('problem' in failed && failed.problem).toContain('concluded failure');
+ });
+});
+
describe('release.yml waits for the CI gate', () => {
const wf = load(readFileSync(join(import.meta.dir, '../../.github/workflows/release.yml'), 'utf8')) as { on: Record<string, unknown>; jobs: Record<string, { needs?: string[] | string; if?: string; steps: Array<{ run?: string; with?: Record<string, string>; env?: Record<string, string> }>; permissions?: Record<string, string> }> };
test('build, release, latest-stable and the publish jobs need ci-gate and use its commit', () => {