Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Reuse a pull request's passed full test run when it lands on master as one squashed commit (#9385)
Garry Tan7a18fddddb78
diff --git a/changelog.d/capy-ci-reuse-squash.md b/changelog.d/capy-ci-reuse-squash.mdnew file mode 100644index 0000000000..ba4471f4bb--- /dev/null+++ b/changelog.d/capy-ci-reuse-squash.md@@ -0,0 +1,5 @@+**A squashed landing on master reuses the pull request's passed full test run instead of running the whole suite again.**++### Changed++- `scripts/ci-reuse.ts`: a one-parent push to master (a pull request landed as one squashed commit) now reuses the landed pull request's own run: the complete, passed full run of Test or E2E on a head GitHub names for the commit with the same tree, under the same artifact, job and repository checks a merge commit gets. Every run on those heads is read and the newest one decides: if it failed or is still running, the push runs in full, and a cancelled run or one that skipped its full job is passed over. A one-parent push no pull request names (a hand revert, a direct push), one whose lookup fails, one whose tree matches no full run, and a root commit still run in full. docs/ci-event-parity.md, "Master-push reuse", says so.diff --git a/docs/ci-event-parity.md b/docs/ci-event-parity.mdindex 4b2b9690b6..3b881ad968 100644--- a/docs/ci-event-parity.md+++ b/docs/ci-event-parity.md@@ -133,7 +133,16 @@ complete full-suite run of the same workflow (`scripts/ci-reuse.ts`, in Testprofile `full`).- The recorded tree equals both the push tree and the tree of the run's head.-The push itself must be a merge commit. Anything else, an API error or a+The push must be a merge commit or a one-parent commit. A merge commit tries the+runs on its merged head first, newest first, then other heads with the same+tree. A one-parent commit (a squashed landing) has no merged head, so only the+landed pull request's own run counts: the heads GitHub names for the commit,+with the push's tree. Every run on those heads is read, and the newest one+decides, as in the+[remote gate](#remote-gate-reuse): if that run failed or is still running, the+push runs in full; a cancelled run, or one that skipped its full job, is passed+over for the next older one. A one-parent commit no pull request names (a hand+revert, a direct push) runs in full. Anything else, an API error or apartial, cancelled or diagnostic run included, leaves reuse unset, so the pushruns in full. A reusing push skips every full lane and both aggregates, records thereused run in its summary, and runs the `reused-full-suite` job with thediff --git a/scripts/ci-reuse.ts b/scripts/ci-reuse.tsindex 3c6fa3a8fc..b56862dbb7 100644--- a/scripts/ci-reuse.ts+++ b/scripts/ci-reuse.ts@@ -5,13 +5,14 @@* bun scripts/ci-reuse.ts --workflow test|e2e --sha <push sha> --repo <owner/name>* --output <GITHUB_OUTPUT> [--summary <GITHUB_STEP_SUMMARY>] [--record <reuse.json>]*- * A push to master is a merge commit M. Its run may reuse the full run of the- * same workflow that already ran the complete full suite on M's tree: the- * workflow_dispatch on the PR head branch (full-ci.yml, or a hand-run- * `gh workflow run`) or an earlier push run. Pull-request runs are the cheap- * tier and never candidates. Fail-closed: reuse=true only when- * (a) M is a merge commit (two parents) whose tree equals the candidate's- * recorded tree;+ * A push to master is a merge commit M or a one-parent (squashed) commit S. Its+ * run may reuse the full run of the same workflow that already ran the complete+ * full suite on its tree: the workflow_dispatch on the PR head branch+ * (full-ci.yml, or a hand-run `gh workflow run`) or an earlier push run.+ * Pull-request runs are the cheap tier and never candidates. Fail-closed:+ * reuse=true only when+ * (a) the push is a merge commit (two parents) or a one-parent commit, and+ * its tree equals the candidate's recorded tree;* (b) the candidate is a same-repository workflow_dispatch or push run* whose LATEST attempt completed with conclusion success;* (c) its full-suite job (`full-suite` / `e2e-full-suite`, by name, exactly@@ -23,9 +24,15 @@* A diagnostic dispatch (test.yml native_only, race_hunt, stress_*; e2e.yml* full_corpus, order_hunt) skips the full-suite job, so (c) rejects it.* Candidates on head M^2 (the merged PR head) come first, newest first; then- * recent successful dispatch and push runs whose head tree equals M's tree. Any- * API error, cancelled, failed or in-progress attempt, missing or cheap- * artifact, tree mismatch or non-merge push gives reuse=false with a reason.+ * recent successful dispatch and push runs whose head tree equals M's tree. S+ * has no merged head, so only the landed pull request's own run counts: a head+ * GitHub names for S (commits/<sha>/pulls) with S's tree, whose newest dispatch+ * or push run decides, as in the remote gate: a newer failed or in-progress+ * full run gives no reuse, a cancelled run or one that skipped the full job+ * looks further back. S with no pull request behind it, or whose lookup fails,+ * runs in full. Any API error, cancelled, failed or in-progress attempt,+ * missing or cheap artifact, tree mismatch or root commit gives reuse=false+ * with a reason.** Writes reuse, source_run_id, source_run_attempt, source_sha, tree and reason* to --output, one summary line (the reused run URL, or the fallback reason)@@ -64,6 +71,8 @@ export interface ReuseDeps {commit(sha: string): Promise<Commit>;runsForHead(file: string, sha: string): Promise<FullRun[]>;recentSuccessfulRuns(file: string): Promise<FullRun[]>;+ /** Head commits of the pull requests GitHub associates with commit `sha` (a squashed landing names its pull request); a fork head's runs are refused like any fork run. */+ pullHeads(sha: string): Promise<string[]>;jobs(runId: number, attempt: number): Promise<JobRow[]>;artifact(runId: number, name: string): Promise<{ value?: unknown; reason?: string }>;}@@ -105,7 +114,8 @@ export async function decide(deps: ReuseDeps, p: Profile, sha: string, repo: strtry {const push = await deps.commit(sha);tree = push.tree;- if (push.parents.length !== 2) return { reuse: false, reason: 'not_merge_commit', detail: `push ${sha} has ${push.parents.length} parent(s); only a two-parent merge commit can reuse its merged head's run`, tree, rejected };+ if (push.parents.length === 1) return await decideOneParent(deps, p, sha, tree, repo, rejected);+ if (push.parents.length !== 2) return { reuse: false, reason: 'not_merge_commit', detail: `push ${sha} has ${push.parents.length} parent(s); only a merge commit or a one-parent commit can reuse a run`, tree, rejected };const head = push.parents[1]!;const headTree = (await deps.commit(head)).tree;const primary = (await deps.runsForHead(p.file, head)).filter(r => r.head_sha === head && isCandidate(r)).sort((a, b) => b.id - a.id);@@ -134,6 +144,34 @@ 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 };}+/**+ * A one-parent push (a squashed landing) has no merged head, so only the landed pull request's own run can stand in:+ * the heads GitHub names for the commit, with the push's tree. Every run on those heads is read by head, and the+ * newest one decides. A push no pull request names (a hand revert, a direct push) always runs in full.+ */+async function decideOneParent(deps: ReuseDeps, p: Profile, sha: string, tree: string, repo: string, rejected: Rejection[]): Promise<Decision> {+ const heads: string[] = [];+ for (const head of new Set(await deps.pullHeads(sha))) {+ if (head !== sha && (await deps.commit(head)).tree === tree) heads.push(head);+ }+ const runs = (await Promise.all(heads.map(async head => (await deps.runsForHead(p.file, head)).filter(r => r.head_sha === head && isCandidate(r) && r.head_repository?.full_name === repo)))).flat().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 accept(run, tree, rejected, `head ${run.head_sha.slice(0, 12)} (same tree as the one-parent push)`);+ rejected.push({ run_id: run.id, ...why });+ if (why.reason === 'run_in_progress' || why.reason === 'run_failed' || why.reason === 'full_suite_not_success') {+ return { reuse: false, reason: why.reason, detail: `the newest full run on tree ${tree} decides: ${why.detail}`, tree, rejected };+ }+ }+ if (!rejected.length) return { reuse: false, reason: 'no_candidate', detail: `no pull request GitHub names for one-parent push ${sha} has a head with tree ${tree} and a ${p.workflow} workflow_dispatch or push run`, tree, rejected };+ return { reuse: false, reason: rejected[0]!.reason, detail: rejected.map(r => r.detail).join('; '), tree, rejected };+}+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 };}@@ -293,6 +331,9 @@ export function apiDeps(client: GitHubClient): ReuseDeps {const pages = await Promise.all(CANDIDATE_EVENTS.map(event => client.request<{ workflow_runs: FullRun[] }>('GET', `repos/{repo}/actions/workflows/${file}/runs?event=${event}&status=success&per_page=100`)));return pages.flatMap(p => p.workflow_runs);},+ async pullHeads(sha) {+ return (await client.request<Array<{ head: { sha: string } }>>('GET', `repos/{repo}/commits/${sha}/pulls`)).map(pr => pr.head.sha);+ },jobs: (runId, attempt) => attemptJobs(client, runId, attempt),artifact: (runId, name) => fetchArtifactJson(client, runId, name, TREE_FILE),};diff --git a/test/scripts/ci-reuse.test.ts b/test/scripts/ci-reuse.test.tsindex 452126aafc..6091b92c04 100644--- a/test/scripts/ci-reuse.test.ts+++ b/test/scripts/ci-reuse.test.ts@@ -5,8 +5,10 @@* tested-tree artifact (profile full) records the push commit's tree.* Pull-request runs (the cheap tier) are never candidates, and a diagnostic* dispatch skips full-suite. Every other case (API error, cancelled, partial,- * re-run in progress, artifact missing or cheap, tree mismatch, non-merge- * push) falls back to a full run with a reason code. Fixtures only; no network.+ * re-run in progress, artifact missing or cheap, tree mismatch, root-commit+ * push) falls back to a full run with a reason code. A one-parent (squashed)+ * push has no merged head, so the newest full run on its tree decides. Fixtures+ * only; no network.*/import { afterAll, describe, expect, test } from 'bun:test';import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';@@ -36,7 +38,8 @@ interface Fixture {runs?: FullRun[];jobs?: Record<string, JobRow[]>;artifacts?: Record<number, { value?: unknown; reason?: string }>;- fail?: 'runs' | 'jobs' | 'commit';+ fail?: 'runs' | 'jobs' | 'commit' | 'pulls';+ pulls?: Record<string, string[]>;}function fixture(f: Fixture): ReuseDeps & { jobCalls: string[] } {@@ -55,6 +58,10 @@ function fixture(f: Fixture): ReuseDeps & { jobCalls: string[] } {return runs.filter(r => r.head_sha === sha);},async recentSuccessfulRuns() { return runs.filter(r => r.conclusion === 'success'); },+ async pullHeads(sha) {+ if (f.fail === 'pulls') throw new Error(`GitHub API GET commits/${sha}/pulls -> 502: bad gateway`);+ return f.pulls?.[sha] ?? [];+ },async jobs(runId, attempt) {if (f.fail === 'jobs') throw new Error('GitHub API GET jobs -> 500');jobCalls.push(`${runId}/${attempt}`);@@ -172,9 +179,9 @@ describe('ci-reuse decision', () => {}});- test('a push that is not a merge commit falls back', async () => {+ test('a root commit push (no parent) falls back', async () => {const run = prRun();- const deps = fixture({ commits: { [M]: { tree: T, parents: [BASE] }, [H]: { tree: T, parents: [BASE] } }, runs: [run], artifacts: { [run.id]: { value: treeJson(run) } } });+ const deps = fixture({ commits: { [M]: { tree: T, parents: [] }, [H]: { tree: T, parents: [BASE] } }, runs: [run], artifacts: { [run.id]: { value: treeJson(run) } } });expect(await decide(deps, PROFILES.test, M, REPO)).toMatchObject({ reuse: false, reason: 'not_merge_commit' });});@@ -224,6 +231,80 @@ describe('ci-reuse decision', () => {expect(d.rejected.map(r => r.reason)).toEqual(['run_cancelled', 'artifact_missing']);});+ describe('a one-parent (squashed) push', () => {+ const squash = (runs: FullRun[], over: Partial<Fixture> = {}) => fixture({+ commits: { [M]: { tree: T, parents: [BASE] }, [H]: { tree: T, parents: [BASE] }, [H2]: { tree: T, parents: [BASE] }, [BASE]: { tree: OTHER, parents: [] } },+ runs, artifacts: Object.fromEntries(runs.map(r => [r.id, { value: treeJson(r) }])), pulls: { [M]: [H] }, ...over,+ });++ test('reuses the passed full run of the pull request head whose tree equals the squash commit tree', async () => {+ const run = prRun();+ expect(await decide(squash([run]), PROFILES.test, M, REPO)).toMatchObject({ reuse: true, reason: 'identical_tree_full_run', tree: T, source: { run_id: 500, run_attempt: 1, sha: H } });+ });++ test('a one-parent push whose tree matches no full run still runs in full', async () => {+ const unrelated = prRun({ id: 710, head_sha: BASE });+ const d = await decide(squash([unrelated]), PROFILES.test, M, REPO);+ expect(d).toMatchObject({ reuse: false, reason: 'no_candidate' });+ expect(await decide(squash([]), PROFILES.test, M, REPO)).toMatchObject({ reuse: false, reason: 'no_candidate' });+ });++ test('a newer failed or in-progress full run on the tree refuses even when an older one passed', async () => {+ const passed = prRun({ id: 500 });+ const failed = prRun({ id: 600, conclusion: 'failure' });+ expect(await decide(squash([passed, failed]), PROFILES.test, M, REPO)).toMatchObject({ reuse: false, reason: 'run_failed' });+ const running = prRun({ id: 600, status: 'in_progress', conclusion: null });+ expect(await decide(squash([passed, running]), PROFILES.test, M, REPO)).toMatchObject({ reuse: false, reason: 'run_in_progress' });+ const redSuite = prRun({ id: 600 });+ const deps = squash([passed, redSuite], { jobs: { '600/1': [{ name: 'full-suite', status: 'completed', conclusion: 'failure' }] } });+ expect(await decide(deps, PROFILES.test, M, REPO)).toMatchObject({ reuse: false, reason: 'full_suite_not_success' });+ });++ test('a newer cancelled run or one that skipped the full job looks further back', async () => {+ const passed = prRun({ id: 500 });+ const cancelled = prRun({ id: 600, conclusion: 'cancelled' });+ const diagnostic = prRun({ id: 610 });+ const deps = squash([passed, cancelled, diagnostic], { jobs: { '610/1': [{ name: 'full-suite', status: 'completed', conclusion: 'skipped' }] } });+ const d = await decide(deps, PROFILES.test, M, REPO);+ expect(d).toMatchObject({ reuse: true, source: { run_id: 500 } });+ expect(d.rejected.map(r => r.reason)).toEqual(['full_suite_missing', 'run_cancelled']);+ });++ test('fork runs, pull_request runs and runs of the push commit itself never count', async () => {+ const fork = prRun({ id: 600, head_repository: { full_name: 'someone-example/repo-example' } });+ const cheap = prRun({ id: 610, event: 'pull_request' });+ const itself = prRun({ id: 620, head_sha: M, event: 'push' });+ expect(await decide(squash([fork, cheap, itself]), PROFILES.test, M, REPO)).toMatchObject({ reuse: false, reason: 'no_candidate' });+ const failedFork = prRun({ id: 630, head_repository: { full_name: 'someone-example/repo-example' }, conclusion: 'failure' });+ expect(await decide(squash([prRun(), failedFork]), PROFILES.test, M, REPO)).toMatchObject({ reuse: true, source: { run_id: 500 } });+ });++ test('a passed run on a same-tree head GitHub does not name for the commit runs in full', async () => {+ const elsewhere = prRun({ head_sha: H2 });+ expect(await decide(squash([elsewhere]), PROFILES.test, M, REPO)).toMatchObject({ reuse: false, reason: 'no_candidate' });+ expect(await decide(squash([elsewhere], { pulls: { [M]: [H2] } }), PROFILES.test, M, REPO)).toMatchObject({ reuse: true, source: { run_id: 500, sha: H2 } });+ });++ test('a one-parent push with no pull request behind it (a hand revert, a direct push) runs in full', async () => {+ expect(await decide(squash([prRun()], { pulls: {} }), PROFILES.test, M, REPO)).toMatchObject({ reuse: false, reason: 'no_candidate' });+ });++ test('a named head with another tree never counts', async () => {+ const deps = squash([prRun()], { commits: { [M]: { tree: T, parents: [BASE] }, [H]: { tree: OTHER, parents: [BASE] } } });+ expect(await decide(deps, PROFILES.test, M, REPO)).toMatchObject({ reuse: false, reason: 'no_candidate' });+ });++ test('a pull request lookup error runs in full', async () => {+ expect(await decide(squash([prRun()], { fail: 'pulls' }), PROFILES.test, M, REPO)).toMatchObject({ reuse: false, reason: 'api_error' });+ });++ test('a passed run whose artifact records another tree is rejected, never reused', async () => {+ const run = prRun();+ const deps = squash([run], { artifacts: { 500: { value: treeJson(run, { tree: OTHER }) } } });+ expect(await decide(deps, PROFILES.test, M, REPO)).toMatchObject({ reuse: false, reason: 'tree_mismatch' });+ });+ });+test('e2e uses its own job and artifact names', () => {expect(PROFILES.e2e).toMatchObject({ file: 'e2e.yml', workflow: 'E2E Tests', job: 'e2e-full-suite', artifact: 'e2e-full-suite-tree' });expect(PROFILES.test).toMatchObject({ file: 'test.yml', workflow: 'Test', job: 'full-suite', artifact: 'full-suite-tree' });@@ -346,6 +427,7 @@ describe('ci-reuse remote-gate decision', () => {async runsForHead(file, sha) { return o.runs.filter(r => r.file === file && r.head_sha === sha); },async recentSuccessfulRuns(file) { return o.runs.filter(r => r.file === file && r.conclusion === 'success'); },async recentRuns(file) { return o.runs.filter(r => r.file === file); },+ async pullHeads() { return []; },async jobs(runId, attempt) {const r = o.runs.find(x => x.id === runId)!;return o.jobs?.[`${runId}/${attempt}`] ?? [{ name: files[r.file].job, status: 'completed', conclusion: 'success' }];