Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: a manual release run publishes only the commit that last changed VERSION
garrytan8d2565f817c9
diff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex 95b1bc1f51..c9a667aa40 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -27,8 +27,9 @@ name: Release# and the job fails with the next step; master push runs are keyed by commit,# so no newer push cancels them, and no later commit is published in the# release commit's place (it may carry unreleased changes under the same-# VERSION). workflow_dispatch backfills master HEAD's VERSION through the-# same gate.+# VERSION). workflow_dispatch backfills only while master HEAD is the release+# commit (the commit that last changed VERSION); otherwise it refuses and names+# the re-run of that commit's push Release run.## Releases per batch (docs/RELEASING.md#releases-per-batch): PRs land with# changelog.d fragments and no version; a release PR moves VERSION, so this@@ -39,7 +40,7 @@ on:push:branches: [master]paths: [VERSION]- workflow_dispatch: {} # manual first run / backfill of the current VERSION+ workflow_dispatch: {} # backfill of the current VERSION while master HEAD is its release commitpermissions:contents: read@@ -112,7 +113,7 @@ jobs: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"+ run: bun scripts/release-gate.ts --sha "$GITHUB_SHA" --version "$RELEASE_VERSION" --ref "$GITHUB_REF" --event "$GITHUB_EVENT_NAME" --repo "$GITHUB_REPOSITORY" --output "$GITHUB_OUTPUT"build:needs: [version, ci-gate]diff --git a/scripts/release-gate.ts b/scripts/release-gate.tsindex 6aa801f9b0..a64c4795ad 100644--- a/scripts/release-gate.ts+++ b/scripts/release-gate.ts@@ -3,7 +3,7 @@* 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>]+ * [--timeout-minutes 110] [--poll-seconds 60] [--output <file>] [--event push|workflow_dispatch]** 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.@@ -20,9 +20,11 @@* - still running, or not created yet: wait, up to --timeout-minutes, then* fail naming the re-run of this Release run, which gates the same commit.*- * A workflow_dispatch backfill runs the same gate for master's HEAD, so it is- * right only while HEAD is the release commit; every failure names the re-run- * of the Release run instead. Writes+ * A workflow_dispatch backfill (--event workflow_dispatch) resolves the+ * release commit, the first-parent commit that last changed VERSION, and+ * gates only that commit: when master HEAD is a later commit it publishes+ * nothing and names the re-run of the release commit's push Release run, so+ * later landings are never published under the release's notes. Writes* `publish=true|false` and `sha=<commit>` to --output (GITHUB_OUTPUT).** Master-push reuse (scripts/ci-reuse.ts, docs/ci-event-parity.md#master-push-reuse):@@ -43,7 +45,7 @@ import { fetchArtifactJson, restClient, type GitHubClient } from './lib/gh-issueexport 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 RERUN = `re-run this Release run (gh run rerun ${process.env.GITHUB_RUN_ID ?? '<this Release run id>'}), which gates the same release commit; a dispatch on master would gate master HEAD instead`;+const RERUN = `re-run this Release run (gh run rerun ${process.env.GITHUB_RUN_ID ?? '<this Release run id>'}), which gates the same release commit; a dispatch on master refuses once master HEAD has moved past it`;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 }@@ -59,6 +61,28 @@ export interface GateDeps {runsFor(file: string, sha: string): Promise<GateRun[]>;provenance(file: string, run: GateRun): Promise<Provenance>;}+export interface CommitDeps {+ firstParent(sha: string): Promise<string | null>;+ versionAt(sha: string): Promise<string>;+}+const WALK_LIMIT = 500;++/** What a workflow_dispatch on master HEAD may gate: the release commit (the first-parent commit that last changed VERSION) when HEAD is it, else nothing. */+export async function dispatchTarget(deps: CommitDeps, head: string, version: string): Promise<{ kind: 'gate'; sha: string } | { kind: 'fail'; sha: string; why: string; next: string }> {+ let release = head;+ for (let i = 0; ; i++) {+ if (i === WALK_LIMIT) throw new Error(`no commit changed VERSION to ${version} within ${WALK_LIMIT} first parents of ${head.slice(0, 12)}`);+ const parent = await deps.firstParent(release);+ if (!parent || (await deps.versionAt(parent)) !== version) break;+ release = parent;+ }+ if (release === head) return { kind: 'gate', sha: release };+ return {+ kind: 'fail', sha: release,+ why: `this dispatch runs on master HEAD ${head.slice(0, 12)}, but v${version}'s release commit is ${release.slice(0, 12)} (the commit that last changed VERSION). The commits after it landed later and their notes are not in v${version}, so a dispatch never publishes them under that version`,+ next: `re-run the push Release run of the release commit: gh run rerun "$(gh run list --workflow release.yml --commit ${release} --event push --limit 1 --json databaseId --jq '.[0].databaseId')". Changes landed after it ship in the next release (gstack-release-stamp open)`,+ };+}const newest = (runs: GateRun[]) => [...runs].sort((a, b) => b.run_number - a.run_number || b.run_attempt - a.run_attempt)[0];@@ -97,8 +121,14 @@ export async function evaluate(deps: GateDeps, sha: string, version: string): Prreturn { kind: 'publish', sha, sources, why: `Test and E2E Tests both succeeded on ${sha.slice(0, 12)}: ${how}` };}-export function apiDeps(client: GitHubClient): GateDeps {+export function apiDeps(client: GitHubClient): GateDeps & CommitDeps {return {+ async firstParent(sha) {+ return (await client.request<{ parents: Array<{ sha: string }> }>('GET', `repos/{repo}/commits/${sha}`)).parents[0]?.sha ?? null;+ },+ async versionAt(sha) {+ return Buffer.from((await client.request<{ content: string }>('GET', `repos/{repo}/contents/VERSION?ref=${sha}`)).content, 'base64').toString('utf8').trim();+ },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;},@@ -144,8 +174,9 @@ if (import.meta.main) {const timeoutMs = Number(flag('--timeout-minutes') ?? 110) * 60_000;const pollMs = Number(flag('--poll-seconds') ?? 60) * 1000;const output = flag('--output');+ const event = flag('--event') ?? 'push';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>]');+ 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>] [--event push|workflow_dispatch]');process.exit(2);}const write = (publish: boolean, at: string) => { if (output) appendFileSync(output, `publish=${publish}\nsha=${at}\n`); };@@ -155,7 +186,16 @@ if (import.meta.main) {process.exit(1);}try {- const v = await gate(apiDeps(restClient(repo)), { sha, version, timeoutMs, pollMs, log: line => console.log(line) });+ const api = apiDeps(restClient(repo));+ if (event === 'workflow_dispatch') {+ const d = await dispatchTarget(api, sha, version);+ if (d.kind === 'fail') {+ console.log(`::error::Release v${version} not published: ${d.why}. Next: ${d.next}. Docs: ${DOCS}`);+ write(false, d.sha);+ process.exit(1);+ }+ }+ const v = await gate(api, { 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);diff --git a/test/scripts/release-gate.test.ts b/test/scripts/release-gate.test.tsindex e0a9da43d3..5c20bcf41a 100644--- a/test/scripts/release-gate.test.ts+++ b/test/scripts/release-gate.test.ts@@ -10,7 +10,7 @@ import { readFileSync } from 'node:fs';import { join } from 'node:path';import { deflateRawSync } from 'node:zlib';import { load } from 'js-yaml';-import { apiDeps, evaluate, gate, GATED, reuseOutputs, type GateDeps, type GateRun, type Provenance } from '../../scripts/release-gate.ts';+import { apiDeps, dispatchTarget, evaluate, gate, GATED, reuseOutputs, type CommitDeps, 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);@@ -109,6 +109,52 @@ describe('release gate', () => {});});+describe('a workflow_dispatch gates only the release commit', () => {+ const D = 'd'.repeat(40);+ const E = 'e'.repeat(40);+ /** Master's first-parent line, newest first: C and B landed after the release commit A of 0.60.71.0; D is 0.60.70.0. */+ const history = (): CommitDeps & { reads: string[] } => {+ const parent: Record<string, string | null> = { [C]: B, [B]: A, [A]: D, [D]: E, [E]: null };+ const version: Record<string, string> = { [C]: '0.60.71.0', [B]: '0.60.71.0', [A]: '0.60.71.0', [D]: '0.60.70.0', [E]: '0.60.69.0' };+ const reads: string[] = [];+ return { reads, async firstParent(sha) { reads.push(sha.slice(0, 1)); return parent[sha]!; }, async versionAt(sha) { return version[sha]!; } };+ };++ test('a dispatch whose HEAD is the commit that last changed VERSION gates that commit', async () => {+ expect(await dispatchTarget(history(), A, '0.60.71.0')).toEqual({ kind: 'gate', sha: A });+ });++ test('a dispatch on a later master HEAD resolves the release commit and refuses with the re-run of its Release run', async () => {+ const h = history();+ const v = await dispatchTarget(h, C, '0.60.71.0');+ expect(v).toMatchObject({ kind: 'fail', sha: A });+ expect(h.reads).toEqual(['c', 'b', 'a']);+ if (v.kind !== 'fail') return;+ expect(v.why).toContain(`master HEAD ${C.slice(0, 12)}`);+ expect(v.why).toContain(`v0.60.71.0's release commit is ${A.slice(0, 12)}`);+ expect(v.next).toContain(`gh run list --workflow release.yml --commit ${A} --event push`);+ expect(v.next).toContain('gh run rerun');+ expect(v.next).not.toContain('gh workflow run release.yml');+ });++ test('the API deps read the first parent and VERSION at a commit', async () => {+ const paths: string[] = [];+ const client: GitHubClient = {+ async request<T>(_m: string, path: string): Promise<T> {+ paths.push(path);+ if (path === `repos/{repo}/commits/${C}`) return { parents: [{ sha: B }, { sha: D }] } as T;+ if (path === `repos/{repo}/commits/${E}`) return { parents: [] } as T;+ if (path === `repos/{repo}/contents/VERSION?ref=${B}`) return { encoding: 'base64', content: Buffer.from('0.60.71.0\n').toString('base64') } as T;+ throw new Error(`unexpected ${path}`);+ },+ };+ const api = apiDeps(client);+ expect(await api.firstParent(C)).toBe(B);+ expect(await api.firstParent(E)).toBeNull();+ expect(await api.versionAt(B)).toBe('0.60.71.0');+ });+});+/** A one-entry zip, enough for readZipEntry. */function zipOf(name: string, content: string): Uint8Array {const data = Buffer.from(content);@@ -169,7 +215,7 @@ describe('release.yml waits for the CI gate', () => {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);+ expect(gateJob.steps.some(s => s.run?.includes('bun scripts/release-gate.ts --sha "$GITHUB_SHA"') && s.run.includes('--ref "$GITHUB_REF"') && s.run.includes('--event "$GITHUB_EVENT_NAME"'))).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'");