Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
GBRA-346: the daily stable check backfills a missing stable.json automatically
garrytan10a48181860d
diff --git a/.github/workflows/stable-daily.yml b/.github/workflows/stable-daily.ymlindex a6e74a5c1e..6d9f352e07 100644--- a/.github/workflows/stable-daily.yml+++ b/.github/workflows/stable-daily.yml@@ -9,6 +9,9 @@ name: Stable daily# release, and a later run promotes it. It never promotes without that soak,# never moves stable backward, pauses while stable is a rollback, and does# nothing while the STABLE_AUTO_PROMOTE repository variable is "off".+# When the published stable.json is missing or names another commit than+# stable, a run first writes the record for the current stable from its tag+# message (no tag moves) and stops (GBRA-346).## A schedule event runs only this file as merged on the default branch, so a# scheduled run promotes by rule without a maintainerdiff --git a/CHANGELOG.md b/CHANGELOG.mdindex c325c7df80..4da2655cc4 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -28,7 +28,7 @@ gbrain publishes a release for nearly every merged change, more than a hundred a| Update cache and snooze | one file for every channel; a snooze ended when a newer release shipped | one per channel, tagged with the evidence policy; a snooze holds its whole window (24 h, 48 h, 7 days); a stable rollback below it clears it || `gbrain upgrade --dry-run` | refused without `--restart-only` | reports channel, its source, current and target version and the action, installing nothing || doctor `self_upgrade_health` | — | names the channel and its source, warns on an invalid channel or a failed stable check |-| `Promote stable` workflow | moved the `stable` tag | also commits the evidence record in the same atomic push; new `withdraw` input adds versions to `known_bad`; promoting the current stable again writes its record from the stable tag's message, moving no tag (the one-time backfill for releases promoted before the record existed) |+| `Promote stable` workflow | moved the `stable` tag | also commits the evidence record in the same atomic push; new `withdraw` input adds versions to `known_bad`; promoting the current stable again writes its record from the stable tag's message, moving no tag (the one-time backfill for releases promoted before the record existed; the daily stable check does it on its first scheduled run) |Install commands now name `stable` (`bun install -g github:garrytan/gbrain#stable`), so a new install starts on the channel it follows. Every install surface goes through one constant (`src/core/install-ref.ts`), checked by `bun run check:install-ref`; `latest-stable` still installs the newest release for anyone who pins it.diff --git a/docs/RELEASING.md b/docs/RELEASING.mdindex 4ed908bd32..4483f7c6ce 100644--- a/docs/RELEASING.md+++ b/docs/RELEASING.md@@ -656,9 +656,12 @@ gh workflow run promote-stable.yml -f version=<VERSION> \- **Record backfill (one time, GBRA-346).** Promotions made before thisscript wrote the evidence record moved `stable` with no `stable.json`,and a client that has the stable channel reads only the record, so it- would report `stable_missing`. Before the first release with the channel- client publishes, write the record for the current stable by promoting it- again with the soak run its tag names:+ would report `stable_missing`. The backfill is automatic on the first+ scheduled run of the [daily stable check](#daily-stable-check) after the+ channel client lands: when `stable.json` is missing or names another+ commit than `stable`, that run writes the record for the current stable+ and stops. The manual dispatch still works, for example to write the+ record before the first release with the channel client publishes:```bashgit fetch --tags --force origin@@ -671,10 +674,9 @@ gh workflow run promote-stable.yml -f version=<VERSION> \name the same release, tree and soak run, writes `stable.json` from it(`known_bad` carried forward, `withdraw` accepted) and pushes only therecord branch, leasing `stable` unchanged. Running it from a branch before- the client merges is safe: older clients never read the record. Repeat it- after any promotion a pre-record copy of this script makes; a record that- already names the current stable makes the run a no-op refusal. Every- later promotion writes the record itself.+ the client merges is safe: older clients never read the record. A record+ that already names the current stable makes the run a no-op refusal.+ Every later promotion, manual or scheduled, writes the record itself.<a id="daily-stable-check"></a>### Daily stable check@@ -704,6 +706,13 @@ and keeps stable between one day and one week behind what matters:manual dispatch defaults to `dry_run` (report only); `dry_run=false` needs anadmin or maintainer. It shares the `promote-stable` concurrency group, so itnever races a manual promotion.+- **Record first.** Before the reason check, when `stable.json` is missing or+ names another commit than `stable`, it writes the record for the current+ stable from the stable tag's message through the same re-promotion path as+ the manual backfill (no tag moves; release, exact tree, soak run and leases+ checked; `known_bad` carried forward) and stops; a dry run reports+ `would: backfill the record for v<version>`. A record that already names+ stable changes nothing. The kill switch and a rollback pause skip it.- **Stop it.** Set the repository variable `STABLE_AUTO_PROMOTE=off` (Settings →Secrets and variables → Actions → Variables). It also pauses by itself whilethe current stable record says `rollback: true`, until a maintainer promotes bydiff --git a/scripts/promote-stable.ts b/scripts/promote-stable.tsindex 460f0d0a0a..26895f8c78 100644--- a/scripts/promote-stable.ts+++ b/scripts/promote-stable.ts@@ -272,10 +272,10 @@ export async function currentStable(deps: PromoteDeps): Promise<{ version: strinreturn { version: versions.sort(compareVersions).at(-1)!, tagObject };}-interface PriorRecord { commit: string; version: string | null; releaseCommit: string | null; knownBad: string[] }+export interface PriorRecord { commit: string; version: string | null; releaseCommit: string | null; knownBad: string[] }/** The published evidence record (the remote stable-record head), or null before the first record. */-async function readPriorRecord(deps: PromoteDeps): Promise<PriorRecord | null> {+export async function readPriorRecord(deps: PromoteDeps): Promise<PriorRecord | null> {await deps.git(['fetch', '--no-tags', 'origin', `+${RECORD_BRANCH_REF}:${RECORD_REMOTE_REF}`]);if (!(await refExists(deps, `${RECORD_REMOTE_REF}^{commit}`))) return null;const commit = await gitOut(deps, ['rev-parse', `${RECORD_REMOTE_REF}^{commit}`], `Could not resolve ${RECORD_REMOTE_REF}; re-run this workflow.`);diff --git a/scripts/stable-daily.ts b/scripts/stable-daily.tsindex 2e218c4e74..92bdfff814 100644--- a/scripts/stable-daily.ts+++ b/scripts/stable-daily.ts@@ -9,6 +9,12 @@* 1. Off when the STABLE_AUTO_PROMOTE repository variable is `off`, and paused* while the current stable record says `rollback: true` (a maintainer* promotes by hand to resume).+ * 1b. When the published evidence record (stable.json on stable-record) is+ * missing or names another commit than stable, it writes the record for+ * the current stable first, from the stable tag's message, through+ * promote()'s re-promotion path (no tag moves; the release, exact tree,+ * soak run and leases are checked as for any promotion), and stops there+ * (GBRA-346: stable was promoted before the record existed).* 2. A reason must exist: a merged PR labelled `stable:important` (security,* regression, data-loss or blocking fix, set by the merge lane) that is in a* published release but not in stable, or stable older than 7 days with a@@ -30,7 +36,7 @@ import { appendFileSync, mkdtempSync, readdirSync, readFileSync, rmSync } from 'import { tmpdir } from 'node:os';import { join } from 'node:path';import {- compareVersions, currentStable, processDeps, promote, Refusal, requireSoakWorkflow, scheduleAuthorized, STABLE_REF,+ compareVersions, currentStable, DOCS, processDeps, promote, readPriorRecord, Refusal, requireSoakWorkflow, scheduleAuthorized, STABLE_REF,type PromoteDeps, type Promotion,} from './promote-stable.ts';@@ -53,7 +59,7 @@ export interface DailyDeps extends PromoteDeps {export type DailyResult =| { action: 'off' | 'paused' | 'none'; why: string }- | { action: 'promoted'; why: string; promotion: Promotion }+ | { action: 'promoted' | 'backfilled'; why: string; promotion: Promotion }| { action: 'soak_dispatched' | 'waiting'; why: string; version: string }| { action: 'dry_run'; why: string; would: string };@@ -104,6 +110,15 @@ export async function daily(deps: DailyDeps): Promise<DailyResult> {return { action: 'paused', why: `stable v${current.version} is a rollback (${record.reason ?? '-'}); the daily check stays paused until a maintainer promotes by hand.` };}const stableCommit = await ok(deps, ['rev-parse', `${STABLE_REF}^{commit}`]);++ const published = await readPriorRecord(deps);+ if (published?.releaseCommit !== stableCommit) {+ const why = `stable.json ${published ? `names ${published.version ? `v${published.version}` : 'another release'}` : 'is not published'}, not stable v${current.version}`;+ if (deps.dryRun) return { action: 'dry_run', why, would: `backfill the record for v${current.version}` };+ if (!record.soak_run) throw new Refusal(`stable v${current.version}'s tag message names no soak_run, so its stable.json cannot be written from it; write the record by hand (${DOCS}).`);+ const promotion = await promote(deps, { version: current.version, soakRunUrl: record.soak_run, scoreboardUrl: '', rollback: false, reason: '' });+ return { action: 'backfilled', why, promotion };+ }const promotedAt = Date.parse(record.promoted_at ?? '');const ageDays = Number.isFinite(promotedAt) ? (deps.now().getTime() - promotedAt) / DAY_MS : Infinity;@@ -204,6 +219,7 @@ export function dailyDeps(env: NodeJS.ProcessEnv = process.env, cwd = process.cwexport function renderDailySummary(r: DailyResult): string {const detail = r.action === 'promoted' ? `Promoted v${r.promotion.version} (previous v${r.promotion.previous ?? 'none'}).`+ : r.action === 'backfilled' ? `Wrote stable.json for v${r.promotion.version}; no tag moved.`: r.action === 'dry_run' ? `Dry run: would ${r.would}.`: 'version' in r ? `Release v${r.version}.` : '';return [`## Daily stable check: ${r.action}`, '', r.why, '', detail, ''].join('\n');diff --git a/test/scripts/stable-daily.test.ts b/test/scripts/stable-daily.test.tsindex cde41e2ec0..23d9b9652e 100644--- a/test/scripts/stable-daily.test.ts+++ b/test/scripts/stable-daily.test.ts@@ -16,7 +16,7 @@ import { describe, expect, test } from 'bun:test';import { readFileSync } from 'node:fs';import { join } from 'node:path';import { load } from 'js-yaml';-import { promote, scheduleAuthorized, Refusal, type GitResult } from '../../scripts/promote-stable.ts';+import { promote, renderTagMessage, scheduleAuthorized, Refusal, type GitResult } from '../../scripts/promote-stable.ts';import { daily, integrityFailures, parseRecord, type DailyDeps, type Scoreboard, type SoakRunRow } from '../../scripts/stable-daily.ts';import { parseStableRecord } from '../../src/core/stable-channel.ts';@@ -42,6 +42,8 @@ interface World {autoPromote?: string;dryRun?: boolean;scheduled?: boolean;+ /** The published stable.json; default: a record naming the current stable. null: none published yet. */+ published?: string | null;}const stableMessage = (version: string, promotedAt: string, rollback = false) =>@@ -66,6 +68,8 @@ function world(w: World) {/** The evidence record promote() commits (GBRA-224 W1): blob, tree and commit objects, and the pushed stable-record head. */const objects: Record<string, string> = {};const record: { head?: string } = {};+ const published = w.published === undefined ? JSON.stringify({ schema: 1, version: w.stable.version, commit: sha(w.stable.commit), known_bad: [] }) : w.published;+ if (published !== null) { objects['rec-published'] = published; record.head = 'rec-published'; }const git = async (args: string[], input?: string): Promise<GitResult> => {const [cmd, ...rest] = args;if (cmd === 'hash-object') { const id = `blob-${Object.keys(objects).length}`; objects[id] = input ?? ''; return ok(id); }@@ -237,7 +241,7 @@ describe('daily: important fixes', () => {const r = await daily(w.deps);expect(r.action).toBe('promoted');expect(w.pushes).toHaveLength(1);- expect(w.pushes[0]).toMatch(/^--atomic .*--force-with-lease=refs\/heads\/stable-record: origin .*refs\/tags\/stable:refs\/tags\/stable rec-\d+:refs\/heads\/stable-record$/);+ expect(w.pushes[0]).toMatch(/^--atomic .*--force-with-lease=refs\/heads\/stable-record:rec-published origin .*refs\/tags\/stable:refs\/tags\/stable rec-\d+:refs\/heads\/stable-record$/);expect(parseStableRecord(w.record()!)).toMatchObject({ ok: true, record: { version: '1.0.3', commit: sha(3), tree: tree(3), soak_run: `https://github.com/${REPO}/actions/runs/9`, previous: '1.0.1', rollback: false, known_bad: [] } });expect(JSON.parse(w.record()!)).toMatchObject({ promoted_by: 'schedule (.github/workflows/stable-daily.yml)' });});@@ -297,6 +301,71 @@ describe('daily: weekly floor', () => {});});+describe('daily: stable.json backfill (GBRA-346)', () => {+ const SOAK_7 = `https://github.com/${REPO}/actions/runs/7`;+ const tagged = (version: string, commit: number, rollback = false) => renderTagMessage({+ version, commit: sha(commit), tree: tree(commit), soakRun: SOAK_7, scoreboard: SOAK_7, promotedBy: 'alice-example',+ promotedAt: '2026-11-08T05:17:00Z', previous: null, rollback, reason: rollback ? 'sync regression' : '',+ });+ const unrecorded = (over: Partial<World> = {}) => base({ stable: { version: '1.0.1', commit: 1, message: tagged('1.0.1', 1) }, runs: [soak(7, 1)], published: null, ...over });++ test('a scheduled run writes the missing record for the current stable from its tag message, moving no tag, and stops', async () => {+ const w = world(unrecorded({ importantPrs: [{ number: 30, merge: 3 }], runs: [soak(7, 1), soak(9, 3)] }));+ const r = await daily(w.deps);+ expect(r).toMatchObject({ action: 'backfilled', why: 'stable.json is not published, not stable v1.0.1' });+ expect(w.pushes).toHaveLength(1);+ expect(w.pushes[0]).toBe('--atomic --force-with-lease=refs/tags/stable:tagobj-stable --force-with-lease=refs/heads/stable-record: origin refs/tags/stable:refs/tags/stable ' + w.pushes[0]!.split(' ').at(-1));+ expect(w.pushes[0]!.split(' ').at(-1)).toMatch(/^rec-.*:refs.heads.stable-record$/);+ expect(parseStableRecord(w.record()!)).toMatchObject({ ok: true, record: { version: '1.0.1', commit: sha(1), tree: tree(1), soak_run: SOAK_7, previous: null, known_bad: [] } });+ expect(w.tags.stable!.message).toBe(tagged('1.0.1', 1));+ expect(w.api.some(a => a.startsWith('search/issues'))).toBe(false);++ const stale = world(unrecorded({ published: JSON.stringify({ schema: 1, version: '1.0.0', commit: sha(0), known_bad: ['0.9.0'] }) }));+ expect((await daily(stale.deps)).why).toBe('stable.json names v1.0.0, not stable v1.0.1');+ expect(JSON.parse(stale.record()!)).toMatchObject({ version: '1.0.1', commit: sha(1), known_bad: ['0.9.0'] });+ expect(stale.pushes[0]).toContain('--force-with-lease=refs/heads/stable-record:rec-published');+ });++ test('a dry run reports the backfill and writes nothing', async () => {+ const w = world(unrecorded({ dryRun: true }));+ expect(await daily(w.deps)).toMatchObject({ action: 'dry_run', would: 'backfill the record for v1.0.1' });+ expect(w.pushes).toEqual([]);+ expect(w.posts).toEqual([]);+ });++ test('a record that already names stable is a no-op, and the run goes on to the reason check', async () => {+ const quiet = world(base({ runs: [soak(9, 4)] }));+ expect((await daily(quiet.deps)).action).toBe('none');+ expect(quiet.api.some(a => a.startsWith('search/issues'))).toBe(true);+ expect(quiet.pushes).toEqual([]);+ const important = world(base({ importantPrs: [{ number: 30, merge: 3 }], runs: [soak(9, 3)] }));+ expect((await daily(important.deps)).action).toBe('promoted');+ });++ test('the kill switch and a rollback pause both win over the backfill; a tag message with no soak run refuses', async () => {+ const off = world(unrecorded({ autoPromote: 'off' }));+ expect((await daily(off.deps)).action).toBe('off');+ const paused = world(unrecorded({ stable: { version: '1.0.1', commit: 1, message: tagged('1.0.1', 1, true) } }));+ expect((await daily(paused.deps)).action).toBe('paused');+ expect([...off.pushes, ...paused.pushes]).toEqual([]);+ const bare = world(unrecorded({ stable: { version: '1.0.1', commit: 1, message: stableMessage('1.0.1', '2026-11-08T05:17:00Z') } }));+ const error = await daily(bare.deps).then(() => null, e => e);+ expect(error).toBeInstanceOf(Refusal);+ expect((error as Error).message).toContain('names no soak_run, so its stable.json cannot be written from it');+ expect(bare.pushes).toEqual([]);+ });++ test('a maintainer dispatch with dry_run=false backfills; anyone else is refused by the role check', async () => {+ const maintainer = world(unrecorded({ scheduled: false }));+ const ghApi = maintainer.deps.ghApi;+ maintainer.deps.ghApi = async path => (path.endsWith('/permission') ? { permission: 'write', role_name: 'maintain' } : ghApi(path));+ expect((await daily(maintainer.deps)).action).toBe('backfilled');+ const other = world(unrecorded({ scheduled: false }));+ await expect(daily(other.deps)).rejects.toThrow(/has the none role/);+ expect(other.pushes).toEqual([]);+ });+});+describe('stable-daily.yml', () => {const wf = load(readFileSync(join(ROOT, '.github/workflows/stable-daily.yml'), 'utf8')) as {on: { schedule: Array<{ cron: string }>; workflow_dispatch: { inputs: Record<string, { default: unknown; type: string }> } };