Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

release: publish only after Test and E2E Tests pass on the release commit

garrytan28e9b0cbe8fe
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index c796227747..1a61547703 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -18,6 +18,15 @@ name: Release
#
# The asset names are a contract with expectedAssetName() in
# src/core/binary-self-update.ts, pinned by test/release-workflow.test.ts.
+#
+# CI gate (docs/RELEASING.md#release-ci-gate): the `ci-gate` job waits for
+# the push-to-master runs of Test and E2E Tests on the release commit
+# (scripts/release-gate.ts). Build, publication and the latest-stable move
+# run only after both succeed. A failed run publishes nothing and the job
+# fails with the next step; a run cancelled by a newer push is followed to
+# the next master commit with the same VERSION (published there) or marks
+# this version superseded; workflow_dispatch backfills master HEAD's VERSION
+# through the same gate.
on:
push:
@@ -68,9 +77,36 @@ jobs:
echo "exists=false" >> "$GITHUB_OUTPUT"
fi
- build:
+ ci-gate:
needs: version
if: needs.version.outputs.exists == 'false'
+ runs-on: ubuntu-latest
+ timeout-minutes: 120
+ permissions:
+ contents: read
+ actions: read
+ outputs:
+ publish: ${{ steps.gate.outputs.publish }}
+ sha: ${{ steps.gate.outputs.sha }}
+ steps:
+ - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
+ with:
+ persist-credentials: false
+ sparse-checkout: |
+ scripts
+ - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2
+ with:
+ bun-version: 1.4.2
+ - id: gate
+ name: Wait for Test and E2E Tests on the release commit
+ env:
+ GH_TOKEN: ${{ github.token }}
+ RELEASE_VERSION: ${{ needs.version.outputs.version }}
+ run: bun scripts/release-gate.ts --sha "$GITHUB_SHA" --version "$RELEASE_VERSION" --ref "$GITHUB_REF" --repo "$GITHUB_REPOSITORY" --output "$GITHUB_OUTPUT"
+
+ build:
+ needs: [version, ci-gate]
+ if: needs.version.outputs.exists == 'false' && needs.ci-gate.outputs.publish == 'true'
strategy:
matrix:
include:
@@ -88,6 +124,8 @@ jobs:
attestations: write # for attest-build-provenance
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
+ with:
+ ref: ${{ needs.ci-gate.outputs.sha }}
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2
with:
bun-version: 1.4.2
@@ -139,14 +177,16 @@ jobs:
path: bin/${{ matrix.artifact }}
release:
- needs: [version, build]
- if: needs.version.outputs.exists == 'false'
+ needs: [version, ci-gate, build]
+ if: needs.version.outputs.exists == 'false' && needs.ci-gate.outputs.publish == 'true'
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: write # create the tag + release (scoped to this job only)
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
+ with:
+ ref: ${{ needs.ci-gate.outputs.sha }}
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
path: artifacts
@@ -166,7 +206,7 @@ jobs:
with:
tag_name: v${{ needs.version.outputs.version }}
name: v${{ needs.version.outputs.version }}
- target_commitish: ${{ github.sha }}
+ target_commitish: ${{ needs.ci-gate.outputs.sha }}
body_path: /tmp/release-notes.md
fail_on_unmatched_files: true
files: |
@@ -188,7 +228,9 @@ jobs:
# the release already exists with all assets):
# git push origin "+refs/tags/v<VERSION>^{commit}:refs/tags/latest-stable"
- name: Advance latest-stable to this release commit
- run: git push origin "+${GITHUB_SHA}:refs/tags/latest-stable"
+ env:
+ RELEASE_SHA: ${{ needs.ci-gate.outputs.sha }}
+ run: git push origin "+${RELEASE_SHA}:refs/tags/latest-stable"
# [G7/S3#4] Publishes the rendered agent-workspace template repo (the
# GitHub "Use this template" door) from CI ONLY — no human pushes it by
@@ -206,8 +248,8 @@ jobs:
# template repository ONLY — no other repositories, no other permissions.
# Documented in docs/RELEASING.md.
publish-template:
- needs: [version, release]
- if: needs.version.outputs.exists == 'false'
+ needs: [version, ci-gate, release]
+ if: needs.version.outputs.exists == 'false' && needs.ci-gate.outputs.publish == 'true'
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
@@ -218,6 +260,8 @@ jobs:
TEMPLATE_REPO: ${{ vars.TEMPLATE_REPO || 'garrytan/gbrain-agent-template' }}
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
+ with:
+ ref: ${{ needs.ci-gate.outputs.sha }}
- id: gate
name: Gate on PAT + vendored template tree
run: |
@@ -302,14 +346,15 @@ jobs:
# by construction. The repo-root source form keeps working for from-source
# installs. Same trust model as the force-advanced latest-stable tag.
publish-codex-plugin:
- needs: [version, release]
- if: needs.version.outputs.exists == 'false'
+ needs: [version, ci-gate, release]
+ if: needs.version.outputs.exists == 'false' && needs.ci-gate.outputs.publish == 'true'
runs-on: ubuntu-latest
permissions:
contents: write # force-push the codex-plugin branch of THIS repo
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
+ ref: ${{ needs.ci-gate.outputs.sha }}
# This job holds contents:write; bun install + the generator run
# before the push, so the checkout token must not persist into
# .git/config — the push step supplies GH_TOKEN explicitly.
diff --git a/scripts/release-gate.ts b/scripts/release-gate.ts
new file mode 100644
index 0000000000..a1963b5afe
--- /dev/null
+++ b/scripts/release-gate.ts
@@ -0,0 +1,157 @@
+#!/usr/bin/env bun
+/**
+ * Release CI gate (green master wave, Garry-approved release gating).
+ *
+ * bun scripts/release-gate.ts --sha <sha> --version <v> [--ref refs/heads/master] [--repo owner/name]
+ * [--timeout-minutes 110] [--poll-seconds 60] [--output <file>]
+ *
+ * release.yml publishes a release, and moves `latest-stable`, only for a
+ * commit whose push-to-master runs of Test and E2E Tests both succeeded.
+ * The gate polls those runs for the release SHA:
+ *
+ * - both green: publish that SHA;
+ * - either failed (failure, timed out, startup failure): no publish; the
+ * job fails naming the run, the master-red issue and the next command;
+ * - either cancelled by a newer push: follow the next push-to-master run of
+ * that workflow. When its commit contains the release SHA and carries the
+ * same VERSION, the gate re-evaluates both workflows there and publishes
+ * that commit; when VERSION moved on, this version is superseded (the
+ * newer VERSION's own release run publishes; exit 0, publish=false);
+ * - still running, or not created yet: wait, up to --timeout-minutes, then
+ * fail with the dispatch backfill command.
+ *
+ * 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).
+ * 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';
+
+export const GATED = [{ file: 'test.yml', name: 'Test' }, { file: 'e2e.yml', name: 'E2E Tests' }] 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 type Verdict =
+ | { kind: 'publish'; sha: string; why: string }
+ | { kind: 'superseded'; sha: string; why: string }
+ | { kind: 'wait'; sha: string; why: string }
+ | { kind: 'fail'; sha: string; why: string; next: string };
+
+export interface GateDeps {
+ runsFor(file: string, sha: string): Promise<GateRun[]>;
+ newerRuns(file: string, after: number): Promise<GateRun[]>;
+ versionAt(sha: string): Promise<string>;
+ contains(base: string, head: string): Promise<boolean>;
+}
+
+const newest = (runs: GateRun[]) => [...runs].sort((a, b) => b.run_number - a.run_number || b.run_attempt - a.run_attempt)[0];
+
+/** One evaluation of the gate for a release SHA (pure over its dependencies). */
+export async function evaluate(deps: GateDeps, sha: string, version: string): Promise<Verdict> {
+ let candidate = sha;
+ for (let hop = 0; hop < MAX_HOPS; hop++) {
+ const runs = await Promise.all(GATED.map(async wf => ({ wf, run: newest((await deps.runsFor(wf.file, candidate)).filter(r => r.event === 'push' && r.head_branch === 'master')) })));
+ const failed = runs.find(r => r.run && r.run.status === 'completed' && RED.has(r.run.conclusion ?? ''));
+ if (failed) {
+ return {
+ kind: 'fail', sha: candidate,
+ why: `${failed.wf.name} run ${failed.run!.id} (${failed.run!.html_url ?? `run ${failed.run!.run_number}`}) ended ${failed.run!.conclusion} on ${candidate.slice(0, 12)}, so v${version} is not published and latest-stable does not move. Releases publish only commits master CI passed.`,
+ next: `Repair master (the open "Master red: ${failed.wf.name}" issue names the failing tests; docs/RELEASING.md#master-red-issues). Once a later push-to-master run of Test and E2E Tests is green, publish the current VERSION with: gh workflow run release.yml --ref master`,
+ };
+ }
+ const cancelled = runs.find(r => r.run && r.run.status === 'completed' && (r.run.conclusion === 'cancelled' || r.run.conclusion === 'skipped'));
+ if (cancelled) {
+ const successor = (await deps.newerRuns(cancelled.wf.file, cancelled.run!.run_number))
+ .filter(r => r.event === 'push' && r.head_branch === 'master' && r.head_sha !== candidate)
+ .sort((a, b) => a.run_number - b.run_number)[0];
+ if (!successor) return { kind: 'wait', sha: candidate, why: `${cancelled.wf.name} run ${cancelled.run!.id} was cancelled (superseded by a newer push); waiting for the newer push-to-master run` };
+ const [moved, inside] = [await deps.versionAt(successor.head_sha), await deps.contains(sha, successor.head_sha)];
+ if (!inside) return { kind: 'fail', sha: candidate, why: `${cancelled.wf.name} on ${candidate.slice(0, 12)} was cancelled and the next master run (${successor.head_sha.slice(0, 12)}) does not contain the release commit`, next: 'Re-run the gate after master settles: gh workflow run release.yml --ref master' };
+ if (moved !== version) return { kind: 'superseded', sha: successor.head_sha, why: `${cancelled.wf.name} on ${candidate.slice(0, 12)} was cancelled by a newer push that carries VERSION ${moved}; v${version} is superseded and v${moved}'s own release run publishes` };
+ candidate = successor.head_sha;
+ continue;
+ }
+ 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)`}` };
+ }
+ 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' };
+}
+
+export function apiDeps(client: GitHubClient): GateDeps {
+ return {
+ async runsFor(file, sha) {
+ return (await client.request<{ workflow_runs: GateRun[] }>('GET', `repos/{repo}/actions/workflows/${file}/runs?head_sha=${sha}&event=push&per_page=20`)).workflow_runs;
+ },
+ async newerRuns(file, after) {
+ return (await client.request<{ workflow_runs: GateRun[] }>('GET', `repos/{repo}/actions/workflows/${file}/runs?event=push&branch=master&per_page=30`)).workflow_runs.filter(r => r.run_number > after);
+ },
+ async versionAt(sha) {
+ const file = await client.request<{ content: string }>('GET', `repos/{repo}/contents/VERSION?ref=${sha}`);
+ return Buffer.from(file.content, 'base64').toString('utf8').trim();
+ },
+ async contains(base, head) {
+ if (base === head) return true;
+ const cmp = await client.request<{ status: string }>('GET', `repos/{repo}/compare/${base}...${head}`);
+ return cmp.status === 'ahead' || cmp.status === 'identical';
+ },
+ };
+}
+
+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)));
+ const deadline = now() + opts.timeoutMs;
+ for (;;) {
+ const v = await evaluate(deps, opts.sha, opts.version);
+ if (v.kind !== 'wait') return v;
+ if (now() >= deadline) {
+ return { kind: 'fail', sha: v.sha, why: `Test and E2E Tests did not finish on ${v.sha.slice(0, 12)} within ${Math.round(opts.timeoutMs / 60_000)} minutes (${v.why})`, next: 'When they finish green, publish with: gh workflow run release.yml --ref master' };
+ }
+ opts.log(`[release-gate] waiting: ${v.why}`);
+ await sleep(opts.pollMs);
+ }
+}
+
+if (import.meta.main) {
+ const flag = (name: string) => { const i = process.argv.indexOf(name); return i >= 0 ? process.argv[i + 1] : undefined; };
+ const sha = flag('--sha') ?? '';
+ const version = flag('--version') ?? '';
+ const ref = flag('--ref') ?? 'refs/heads/master';
+ const repo = flag('--repo') ?? process.env.GITHUB_REPOSITORY ?? 'garrytan/gbrain';
+ const timeoutMs = Number(flag('--timeout-minutes') ?? 110) * 60_000;
+ const pollMs = Number(flag('--poll-seconds') ?? 60) * 1000;
+ const output = flag('--output');
+ if (!/^[0-9a-f]{40}$/.test(sha) || !/^\d+\.\d+\.\d+(\.\d+)?$/.test(version) || !(timeoutMs > 0) || !(pollMs > 0)) {
+ console.log('Usage: bun scripts/release-gate.ts --sha <40-hex sha> --version <VERSION> [--ref refs/heads/master] [--repo owner/name] [--timeout-minutes 110] [--poll-seconds 60] [--output <file>]');
+ process.exit(2);
+ }
+ const write = (publish: boolean, at: string) => { if (output) appendFileSync(output, `publish=${publish}\nsha=${at}\n`); };
+ if (ref !== 'refs/heads/master') {
+ console.log(`::error::Release v${version} not published: this run is on ${ref}. Why: releases publish only master commits that master CI passed. Fix: gh workflow run release.yml --ref master. Docs: ${DOCS}`);
+ write(false, sha);
+ process.exit(1);
+ }
+ try {
+ const v = await gate(apiDeps(restClient(repo)), { sha, version, timeoutMs, pollMs, log: line => console.log(line) });
+ if (v.kind === 'publish') {
+ console.log(`[release-gate] publish v${version} at ${v.sha}: ${v.why}.`);
+ write(true, v.sha);
+ } else if (v.kind === 'superseded') {
+ console.log(`::notice::Release v${version} skipped: ${v.why}. Nothing to do. Docs: ${DOCS}`);
+ write(false, v.sha);
+ } else {
+ console.log(`::error::Release v${version} not published: ${v.kind === 'fail' ? v.why : 'gate ended while waiting'}. Next: ${v.kind === 'fail' ? v.next : 'gh workflow run release.yml --ref master'}. Docs: ${DOCS}`);
+ write(false, v.sha);
+ process.exit(1);
+ }
+ } catch (e) {
+ console.log(`::error::Release v${version} not published: the CI gate could not read the actions API (${e instanceof Error ? e.message : String(e)}). Why: publication waits for Test and E2E Tests success on the release commit. Fix: re-run this workflow (gh run rerun <this run id>) or gh workflow run release.yml --ref master. Docs: ${DOCS}`);
+ write(false, sha);
+ process.exit(1);
+ }
+}
diff --git a/test/check-bootstrap-guards.test.ts b/test/check-bootstrap-guards.test.ts
index 8cbbc86b01..5a346d745f 100644
--- a/test/check-bootstrap-guards.test.ts
+++ b/test/check-bootstrap-guards.test.ts
@@ -448,7 +448,8 @@ describe('verify + workflow wiring', () => {
test('release.yml advances latest-stable only as the final release step [C1]', () => {
const wf = require('node:fs').readFileSync(join(ROOT, '.github/workflows/release.yml'), 'utf8');
- expect(wf).toContain('git push origin "+${GITHUB_SHA}:refs/tags/latest-stable"');
+ expect(wf).toContain('git push origin "+${RELEASE_SHA}:refs/tags/latest-stable"');
+ expect(wf).toContain('RELEASE_SHA: ${{ needs.ci-gate.outputs.sha }}');
// The advance comes AFTER the release publish step in the same job.
expect(wf.indexOf('refs/tags/latest-stable')).toBeGreaterThan(wf.indexOf('Create release'));
});
diff --git a/test/scripts/release-gate.test.ts b/test/scripts/release-gate.test.ts
new file mode 100644
index 0000000000..f02f5a82d2
--- /dev/null
+++ b/test/scripts/release-gate.test.ts
@@ -0,0 +1,113 @@
+/**
+ * scripts/release-gate.ts + release.yml wiring: publication and the
+ * latest-stable move wait for Test and E2E Tests success on the release
+ * commit; a failed run publishes nothing with the next step; a run cancelled
+ * by a newer push is followed to the next master commit with the same
+ * VERSION (or marks the version superseded); dispatch backfills through the
+ * same gate; non-master refs are refused.
+ */
+import { describe, expect, test } from 'bun:test';
+import { readFileSync } from 'node:fs';
+import { join } from 'node:path';
+import { safeLoad } from 'js-yaml';
+import { evaluate, gate, GATED, type GateDeps, type GateRun } from '../../scripts/release-gate.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[] } {
+ const calls: string[] = [];
+ return {
+ calls,
+ async runsFor(file, sha) { calls.push(`runs ${file} ${sha.slice(0, 1)}`); return (runs[file] ?? []).filter(r => r.head_sha === sha); },
+ 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); },
+ };
+}
+
+describe('release gate', () => {
+ test('publishes the release commit only when Test and E2E Tests both succeeded on it', async () => {
+ expect(GATED.map(g => g.file)).toEqual(['test.yml', 'e2e.yml']);
+ const green = deps({ 'test.yml': [run(1, 10, A, 'completed', 'success')], 'e2e.yml': [run(2, 20, A, 'completed', 'success')] });
+ expect(await evaluate(green, A, '0.60.70.0')).toMatchObject({ kind: 'publish', sha: A });
+ const pending = deps({ 'test.yml': [run(1, 10, A, 'in_progress', null)], 'e2e.yml': [run(2, 20, A, 'completed', 'success')] });
+ expect(await evaluate(pending, A, '0.60.70.0')).toMatchObject({ kind: 'wait' });
+ const missing = deps({ 'e2e.yml': [run(2, 20, A, 'completed', 'success')] });
+ expect((await evaluate(missing, A, '0.60.70.0')).why).toContain('Test has no push run yet');
+ });
+
+ test('a failed run publishes nothing and names the repair and the backfill command', async () => {
+ const red = deps({ 'test.yml': [run(1, 10, A, 'completed', 'failure')], 'e2e.yml': [run(2, 20, A, 'completed', 'success')] });
+ const v = await evaluate(red, A, '0.60.70.0');
+ expect(v.kind).toBe('fail');
+ if (v.kind !== 'fail') return;
+ expect(v.why).toContain('v0.60.70.0 is not published and latest-stable does not move');
+ expect(v.next).toContain('Master red: Test');
+ expect(v.next).toContain('gh workflow run release.yml --ref master');
+ const rerunGreen = deps({ 'test.yml': [run(1, 10, A, 'completed', 'failure'), run(1, 10, A, 'completed', 'success', 2)], 'e2e.yml': [run(2, 20, A, 'completed', 'success')] });
+ expect((await evaluate(rerunGreen, A, '0.60.70.0')).kind).toBe('publish');
+ });
+
+ test('a run cancelled by a newer push is followed to the next master commit with the same VERSION, which is published', async () => {
+ const d = deps({
+ 'test.yml': [run(1, 10, A, 'completed', 'cancelled'), run(3, 11, B, 'completed', 'success')],
+ 'e2e.yml': [run(2, 20, A, 'completed', 'success'), run(4, 21, B, 'completed', 'success')],
+ });
+ const v = await evaluate(d, A, '0.60.70.0');
+ expect(v).toMatchObject({ kind: 'publish', sha: B });
+ expect(v.why).toContain('same VERSION');
+ const notYet = deps({ 'test.yml': [run(1, 10, A, 'completed', 'cancelled')], 'e2e.yml': [run(2, 20, A, 'completed', 'success')] });
+ expect((await evaluate(notYet, A, '0.60.70.0')).why).toContain('waiting for the newer push-to-master run');
+ const redSuccessor = deps({ 'test.yml': [run(1, 10, A, 'completed', 'cancelled'), run(3, 11, B, 'completed', 'failure')], 'e2e.yml': [run(4, 21, B, 'completed', 'success')] });
+ expect(await evaluate(redSuccessor, A, '0.60.70.0')).toMatchObject({ kind: 'fail', sha: B });
+ });
+
+ test('a cancellation by a push that moved VERSION marks this version superseded; a successor that lacks the release commit fails', async () => {
+ const moved = deps({ 'test.yml': [run(1, 10, A, 'completed', 'cancelled'), run(3, 11, B, 'in_progress', null)], 'e2e.yml': [] }, { [B]: '0.60.71.0' });
+ expect(await evaluate(moved, A, '0.60.70.0')).toMatchObject({ kind: 'superseded', sha: B });
+ const stray = deps({ 'test.yml': [run(1, 10, A, 'completed', 'cancelled'), run(3, 11, C, 'completed', 'success')], 'e2e.yml': [] }, {}, [C]);
+ expect((await evaluate(stray, A, '0.60.70.0')).kind).toBe('fail');
+ });
+
+ 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[] = [];
+ const states = [run(1, 10, A, 'queued', null), run(1, 10, A, 'in_progress', null), run(1, 10, A, 'completed', 'success')];
+ let i = 0;
+ 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; },
+ };
+ 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');
+ expect(lines).toHaveLength(2);
+ const stuck = deps({ 'test.yml': [run(1, 10, A, 'in_progress', null)], 'e2e.yml': [] });
+ t = 0;
+ const late = await gate(stuck, { sha: A, version: '0.60.70.0', timeoutMs: 3 * 60_000, pollMs: 60_000, log: () => {}, sleep: async ms => { t += ms; }, now: () => t });
+ expect(late.kind).toBe('fail');
+ if (late.kind === 'fail') expect(late.next).toContain('gh workflow run release.yml --ref master');
+ });
+});
+
+describe('release.yml waits for the CI gate', () => {
+ const wf = safeLoad(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', () => {
+ expect(wf.on.workflow_dispatch).toBeDefined();
+ const gateJob = wf.jobs['ci-gate']!;
+ expect(gateJob.permissions).toEqual({ contents: 'read', actions: 'read' });
+ expect(gateJob.steps.some(s => s.run?.includes('bun scripts/release-gate.ts --sha "$GITHUB_SHA"') && s.run.includes('--ref "$GITHUB_REF"'))).toBe(true);
+ for (const job of ['build', 'release', 'publish-template', 'publish-codex-plugin']) {
+ expect(wf.jobs[job]!.needs).toContain('ci-gate');
+ expect(wf.jobs[job]!.if).toContain("needs.ci-gate.outputs.publish == 'true'");
+ expect(wf.jobs[job]!.steps[0]!.with?.ref).toBe('${{ needs.ci-gate.outputs.sha }}');
+ }
+ const steps = wf.jobs.release!.steps;
+ expect(steps.find(s => s.with?.target_commitish)!.with!.target_commitish).toBe('${{ needs.ci-gate.outputs.sha }}');
+ const advance = steps.find(s => s.run?.includes('refs/tags/latest-stable'))!;
+ expect(advance.run).toContain('+${RELEASE_SHA}:refs/tags/latest-stable');
+ expect(advance.env!.RELEASE_SHA).toBe('${{ needs.ci-gate.outputs.sha }}');
+ });
+});