Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: remote-gate reuse skips non-full runs and decides each same-tree head by its newest full run
garrytan5f5681988c66
diff --git a/docs/ci-event-parity.md b/docs/ci-event-parity.mdindex 5af2d49d5c..4ba97f7a55 100644--- a/docs/ci-event-parity.md+++ b/docs/ci-event-parity.md@@ -109,8 +109,11 @@ complete full runs of both Test and E2E under the master-push rules above. Ifboth did, the receipt comes from them: the same `PREGATE_REMOTE: tree= tests=failed=` line, with the runs' executed test identities as the counts, and no VMstarts. A full run on the tree still in progress is waited for (90 minutes at-most, a progress line every minute). A failed newest run on the head refuses-with no receipt. Every other case runs the gate: a dirty tree, a commit GitHub+most, a progress line every minute). A failed newest full run refuses with no+receipt. The newest full run on the head decides, then the newest full run on+each other head with the same tree, so an empty commit cannot skip a newer+failure; a run that did not run the full job (a diagnostic dispatch, a push that+reused) is skipped. Every other case runs the gate: a dirty tree, a commit GitHubdoesn't have, a missing workflow, an API error, or `GBRAIN_PREGATE_REUSE=0`.Only the identical tree reuses. A head that /ship changed after full-ci has adiff --git a/scripts/ci-reuse.ts b/scripts/ci-reuse.tsindex 493bc84bd5..9dd63dbd00 100644--- a/scripts/ci-reuse.ts+++ b/scripts/ci-reuse.ts@@ -114,8 +114,19 @@ export async function decide(deps: ReuseDeps, p: Profile, sha: string, repo: strif (!why) return accept(run, tree, rejected, 'the merged head');rejected.push({ run_id: run.id, ...why });}- const other = await sameTreeRun(deps, p, repo, tree, new Set(primary.map(r => r.id)), [head, sha], rejected);- if (other) return accept(other, tree, rejected, `head ${other.head_sha.slice(0, 12)} (same tree as the merged head)`);+ const tried = new Set(primary.map(r => r.id));+ const heads = new Map<string, string>();+ for (const run of (await deps.recentSuccessfulRuns(p.file)).sort((a, b) => b.id - a.id)) {+ if (tried.has(run.id) || !isCandidate(run) || run.head_sha === head || run.head_sha === sha || run.head_repository?.full_name !== repo) continue;+ if (!heads.has(run.head_sha)) {+ if (heads.size >= MAX_FALLBACK_HEADS) continue;+ heads.set(run.head_sha, (await deps.commit(run.head_sha)).tree);+ }+ if (heads.get(run.head_sha) !== tree) continue;+ const why = await reject(deps, p, run, repo, tree, tree);+ if (!why) return accept(run, tree, rejected, `head ${run.head_sha.slice(0, 12)} (same tree as the merged head)`);+ rejected.push({ run_id: run.id, ...why });+ }} catch (e) {return { reuse: false, reason: 'api_error', detail: `reading the push commit, runs, jobs or artifacts failed: ${e instanceof Error ? e.message : String(e)}`, tree, rejected };}@@ -123,23 +134,6 @@ export async function decide(deps: ReuseDeps, p: Profile, sha: string, repo: strreturn { reuse: false, reason: rejected[0]!.reason, detail: rejected.map(r => r.detail).join('; '), tree, rejected };}-/** The newest passed full run of `p` on another head whose commit tree is `tree` (at most MAX_FALLBACK_HEADS heads read). */-async function sameTreeRun(deps: ReuseDeps, p: Profile, repo: string, tree: string, tried: Set<number>, skipShas: string[], rejected: Rejection[]): Promise<FullRun | undefined> {- const heads = new Map<string, string>();- for (const run of (await deps.recentSuccessfulRuns(p.file)).sort((a, b) => b.id - a.id)) {- if (tried.has(run.id) || !isCandidate(run) || skipShas.includes(run.head_sha) || run.head_repository?.full_name !== repo) continue;- if (!heads.has(run.head_sha)) {- if (heads.size >= MAX_FALLBACK_HEADS) continue;- heads.set(run.head_sha, (await deps.commit(run.head_sha)).tree);- }- if (heads.get(run.head_sha) !== tree) continue;- const why = await reject(deps, p, run, repo, tree, tree);- if (!why) return run;- rejected.push({ run_id: run.id, ...why });- }- return undefined;-}-function accept(run: FullRun, tree: string, rejected: Rejection[], where: string): Decision {return { reuse: true, reason: 'identical_tree_full_run', detail: `run ${run.id} attempt ${run.run_attempt} on ${where} ran the complete full suite on tree ${tree}`, tree, source: { run_id: run.id, run_attempt: run.run_attempt, sha: run.head_sha }, rejected };}@@ -150,8 +144,9 @@ function accept(run: FullRun, tree: string, rejected: Rejection[], where: string* already passed the complete full run of a workflow needs no Ubicloud gate. Same fail-closed checks as a push:* the run's latest attempt and its full-suite job succeeded and its tested-tree artifact records `tree`. Only* the identical tree reuses; a stamp-only difference goes through gstack-pregate's stamp rule, never through here.- * The newest run on `head` decides: in progress waits, a failure refuses (no receipt, no VMs), cancelled or- * not a full run looks further back; anything else is `none` and the caller runs the VM gate.+ * The newest full run on `head` decides, then the same for each other head with the same tree (newest first): in+ * progress waits, a failure refuses (no receipt, no VMs), cancelled or not a full run looks further back; anything else+ * is `none` and the caller runs the VM gate.*/export type GateState = 'reuse' | 'pending' | 'failed' | 'none';export interface GateDecision { state: GateState; detail: string; tests?: number; failed?: number; url?: string }@@ -165,22 +160,44 @@ export async function decideGate(deps: GateDeps, p: Profile, head: string, tree:try {const headTree = (await deps.commit(head)).tree;if (headTree !== tree) return { state: 'none', detail: `${p.workflow}: commit ${head} has tree ${headTree} on GitHub, not the working tree ${tree}` };- const primary = (await deps.runsForHead(p.file, head)).filter(r => r.head_sha === head && isCandidate(r)).sort((a, b) => b.id - a.id);- for (const run of primary) {- const why = await reject(deps, p, run, repo, tree, headTree);- if (!why) return gateAccept(deps, p, run, tree, repo);- if (why.reason === 'run_in_progress') return { state: 'pending', detail: `${p.workflow} ${why.detail}` };- if (why.reason === 'run_failed' || why.reason === 'full_suite_not_success') return { state: 'failed', detail: `${p.workflow} ${why.detail}`, url: runUrl(repo, run) };- rejected.push({ run_id: run.id, ...why });+ const own = await headGate(deps, p, head, tree, repo, rejected);+ if (own) return own;+ const heads = new Map<string, string>();+ for (const run of (await deps.recentSuccessfulRuns(p.file)).sort((a, b) => b.id - a.id)) {+ if (!isCandidate(run) || run.head_sha === head || run.head_repository?.full_name !== repo || heads.has(run.head_sha)) continue;+ if (heads.size >= MAX_FALLBACK_HEADS) break;+ heads.set(run.head_sha, (await deps.commit(run.head_sha)).tree);+ if (heads.get(run.head_sha) !== tree) continue;+ const other = await headGate(deps, p, run.head_sha, tree, repo, rejected);+ if (other) return other;}- const other = await sameTreeRun(deps, p, repo, tree, new Set(primary.map(r => r.id)), [head], rejected);- if (other) return gateAccept(deps, p, other, tree, repo);} catch (e) {return { state: 'none', detail: `${p.workflow}: reading commits, runs, jobs or artifacts failed: ${e instanceof Error ? e.message : String(e)}` };}return { state: 'none', detail: rejected.length ? `${p.workflow}: ${rejected.map(r => r.detail).join('; ')}` : `${p.workflow}: no workflow_dispatch or push run on ${head} or on another head with tree ${tree}` };}+/**+ * The newest full run of `p` on commit `sha` (whose tree is `tree`) decides; a run that did not run the full job+ * (a diagnostic dispatch, a push that reused) is skipped. Undefined when `sha` has no deciding run.+ */+async function headGate(deps: GateDeps, p: Profile, sha: string, tree: string, repo: string, rejected: Rejection[]): Promise<GateDecision | undefined> {+ const runs = (await deps.runsForHead(p.file, sha)).filter(r => r.head_sha === sha && isCandidate(r)).sort((a, b) => b.id - a.id);+ for (const run of runs) {+ const full = (await deps.jobs(run.id, run.run_attempt)).filter(j => j.name === p.job);+ if (full.some(j => j.conclusion === 'skipped') || (run.status === 'completed' && full.length === 0)) {+ rejected.push({ run_id: run.id, reason: 'full_suite_missing', detail: `run ${run.id} attempt ${run.run_attempt} did not run ${p.job} (not a full-profile run)` });+ continue;+ }+ const why = await reject(deps, p, run, repo, tree, tree);+ if (!why) return gateAccept(deps, p, run, tree, repo);+ if (why.reason === 'run_in_progress') return { state: 'pending', detail: `${p.workflow} ${why.detail}` };+ if (why.reason === 'run_failed' || why.reason === 'full_suite_not_success') return { state: 'failed', detail: `${p.workflow} ${why.detail}`, url: runUrl(repo, run) };+ rejected.push({ run_id: run.id, ...why });+ }+ return undefined;+}+const runUrl = (repo: string, run: FullRun) => `https://github.com/${repo}/actions/runs/${run.id}/attempts/${run.run_attempt}`;async function gateAccept(deps: GateDeps, p: Profile, run: FullRun, tree: string, repo: string): Promise<GateDecision> {diff --git a/test/scripts/ci-reuse.test.ts b/test/scripts/ci-reuse.test.tsindex b47f07fe6d..41cab32b48 100644--- a/test/scripts/ci-reuse.test.ts+++ b/test/scripts/ci-reuse.test.ts@@ -436,6 +436,29 @@ describe('ci-reuse remote-gate decision', () => {}});+ test('a newer run on the head that did not run the full job (a diagnostic dispatch, a reusing push) is skipped, never a failed full run', async () => {+ const skipped = (id: number) => ({ [`${id}/1`]: [{ name: PROFILES.test.job, status: 'completed', conclusion: 'skipped' }, { name: 'reused-full-suite', status: 'completed', conclusion: 'success' }] });+ // A diagnostic dispatch that passed, a push that reused (full-suite skipped), and a diagnostic dispatch that failed.+ for (const over of [{ id: 630 }, { id: 630, event: 'push' }, { id: 630, conclusion: 'failure' }]) {+ const deps = gate({ runs: [run('test.yml', over), run('test.yml'), run('e2e.yml')], jobs: skipped(630) });+ expect(await decideGate(deps, PROFILES.test, A, T, REPO)).toMatchObject({ state: 'reuse' });+ expect((await go(deps)).code).toBe(0);+ }+ // One still in progress whose full job is already skipped does not hold the gate.+ const live = gate({ runs: [run('test.yml', { id: 630, status: 'in_progress', conclusion: null }), run('test.yml'), run('e2e.yml')], jobs: skipped(630) });+ expect(await decideGate(live, PROFILES.test, A, T, REPO)).toMatchObject({ state: 'reuse' });+ });++ test('another commit with the same tree inherits that head\'s newest full run: a newer failure there still refuses', async () => {+ const C2 = '8'.repeat(40);+ const commits = { [A]: { tree: T, parents: [BASE] }, [C2]: { tree: T, parents: [A] } };+ const deps = gate({ commits, runs: [run('test.yml', { id: 610, conclusion: 'failure' }), run('test.yml'), run('e2e.yml')] });+ expect(await decideGate(deps, PROFILES.test, C2, T, REPO)).toMatchObject({ state: 'failed' });+ expect((await go(deps, C2)).code).toBe(3);+ const busy = gate({ commits, runs: [run('test.yml', { id: 610, status: 'in_progress', conclusion: null }), run('test.yml'), run('e2e.yml')] });+ expect(await decideGate(busy, PROFILES.test, C2, T, REPO)).toMatchObject({ state: 'pending' });+ });+test('a cancelled newest run looks further back; a passed run of the same tree on another head (a merge commit) reuses', async () => {expect((await go(gate({ runs: [run('test.yml', { id: 620, conclusion: 'cancelled' }), run('test.yml'), run('e2e.yml')] }))).code).toBe(0);const M2 = '7'.repeat(40);