diff --git a/changelog.d/land-ci-reuse-follow-ups.md b/changelog.d/land-ci-reuse-follow-ups.md new file mode 100644 index 000000000..b791ecbbc --- /dev/null +++ b/changelog.d/land-ci-reuse-follow-ups.md @@ -0,0 +1 @@ +**A land that waits on an earlier CI run now decides on what that run actually did.** If a green run was rerun at the moment land picked it up, a red or still-running rerun used to be ignored and land would start another run. Now the rerun's result counts. Main's own Free Tests run also skips a cancelled run when it looks for the run a land already did, so it reuses the earlier green one instead of running the whole suite again. diff --git a/docs/pregate.md b/docs/pregate.md index 40b94002e..32eadb8d4 100644 --- a/docs/pregate.md +++ b/docs/pregate.md @@ -1600,7 +1600,7 @@ workflow on the merged commit, on the scratch branch `gstack-land/` ([ship-policy.md](ship-policy.md#land-direct)), and GitHub's merge has that same tree unless main moved again before the merge. So when the head's tree differs, `find` takes the newest `workflow_dispatch` run on `gstack-land/` -and reuses it only when `verifyRun` accepts it and its `tested-tree` record +that wasn't cancelled (land passes cancelled runs over too) and reuses it only when `verifyRun` accepts it and its `tested-tree` record names the pushed tree (`no_verified_run` or `tree_mismatch` otherwise; a branch with no such run keeps `tree_mismatch`). Only the newest run counts, newest by run id rather than start time, so a red retry on the same tree is never hidden diff --git a/lib/land-merged-ci.ts b/lib/land-merged-ci.ts index c4108986f..8eec5185b 100644 --- a/lib/land-merged-ci.ts +++ b/lib/land-merged-ci.ts @@ -37,8 +37,8 @@ export interface CiDeps { now(): number; url(id: number): string; } -/** `run`: the run the refusal is about: a red run, or one still going that the next land on the same merged tree waits on. */ -export type CiResult = { ok: true; line: string } | { ok: false; red: boolean; detail: string; run?: number }; +/** `run`: the run the refusal is about: a red run, or one still going that the next land on the same merged tree waits on. `unproved`: the run finished green but does not prove the merged tree. */ +export type CiResult = { ok: true; line: string } | { ok: false; red: boolean; detail: string; run?: number; unproved?: true }; type CiInput = { pr: string; commit: string; tree: string; ci: MergedCi; timeoutMs?: number }; /** The newest workflow_dispatch run of `ci.workflow` on the scratch branch or at the merged commit whose head commit has the merged tree, unless it was cancelled. */ @@ -62,8 +62,8 @@ function settle(deps: CiDeps, o: CiInput, branch: string, first: ApiRun | undefi const at = `${o.ci.workflow} run ${deps.url(run.id)} on the merged tree ${o.tree.slice(0, 12)}`; if (run.conclusion !== 'success') return { ok: false, red: true, run: run.id, detail: `${at} concluded ${run.conclusion ?? 'unknown'}` }; const v = deps.verify(run.id); - if (!v.ok) return { ok: false, red: false, detail: `${at} passed, but it does not prove the tree under ${o.ci.profile}: ${v.detail}` }; - if (v.head !== run.head_sha || v.tree !== o.tree) return { ok: false, red: false, detail: `${at} tested ${v.head.slice(0, 12)} (tree ${v.tree.slice(0, 12)}), not the merged commit ${o.commit.slice(0, 12)} or a commit with its tree` }; + if (!v.ok) return { ok: false, red: false, unproved: true, detail: `${at} passed, but it does not prove the tree under ${o.ci.profile}: ${v.detail}` }; + if (v.head !== run.head_sha || v.tree !== o.tree) return { ok: false, red: false, unproved: true, detail: `${at} tested ${v.head.slice(0, 12)} (tree ${v.tree.slice(0, 12)}), not the merged commit ${o.commit.slice(0, 12)} or a commit with its tree` }; const earlier = run.head_sha === o.commit ? '' : `; run by an earlier land at ${run.head_sha.slice(0, 12)}, not dispatched again`; return { ok: true, line: `MERGED_CI: pass ${at} (profile ${o.ci.profile}${earlier})` }; } @@ -74,7 +74,7 @@ export function runMergedCi(deps: CiDeps, o: CiInput): CiResult { const earlier = earlierRun(deps, o, branch); if (earlier) { const r = settle(deps, o, branch, earlier, deps.now()); - if (r.ok || earlier.status !== 'completed' || earlier.conclusion !== 'success') { deps.remove(branch); return r; } + if (r.ok || !r.unproved || earlier.status !== 'completed') { deps.remove(branch); return r; } } const pushed = deps.push(o.commit, branch); if (pushed) return { ok: false, red: false, detail: `cannot push the merged commit ${o.commit.slice(0, 12)} to ${branch}: ${pushed}` }; diff --git a/scripts/ci-tree-reuse.ts b/scripts/ci-tree-reuse.ts index c8263eeba..f1d0826b0 100755 --- a/scripts/ci-tree-reuse.ts +++ b/scripts/ci-tree-reuse.ts @@ -19,7 +19,7 @@ * first one lib/run-evidence.ts verifyRun accepts under the profile in the pushed tree's * .gstack/pregate.json. A pull_request run counts only when its merge commit's tree is its head's tree * (verifyRun). When the head's tree differs (land merged a stale-base pin), the only candidate is the newest - * workflow_dispatch run on landBranch(), the run land.merged_ci gated the merge on, and it counts only when + * workflow_dispatch run on landBranch() that was not cancelled, the run land.merged_ci gated the merge on, and it counts only when * verifyRun accepts it with the pushed tree as its tested tree (newest by run id, so a re-run of an older land run * never outranks a later red one): that run executed this same tree's workflow, so * the rules it ran under are the ones now on main. It writes reused=true, run_id, run_url and tree to GITHUB_OUTPUT, or reused=false and a @@ -79,7 +79,7 @@ export function findReuse(deps: ReuseDeps, o: { repo: string; base: string; push const mismatch = `#${pr.number}'s head ${pr.head.sha.slice(0, 12)} has tree ${headTree.slice(0, 12)}, the push has ${pushTree.slice(0, 12)}`; const branch = landBranch(pr.number); const land = events.includes('workflow_dispatch') - ? deps.runsForBranch(o.workflow, branch).filter(r => r.event === 'workflow_dispatch' && r.head_branch === branch).sort((a, b) => b.id - a.id)[0] + ? deps.runsForBranch(o.workflow, branch).filter(r => r.event === 'workflow_dispatch' && r.head_branch === branch && r.conclusion !== 'cancelled').sort((a, b) => b.id - a.id)[0] : undefined; if (!land) return refuse('tree_mismatch', `${mismatch}, and ${branch} has no workflow_dispatch run of ${o.workflow}`); const v = verifyRun(deps, { repo: o.repo, runRef: String(land.id), profileName: o.profileName, profile: o.profile, now: o.now }); diff --git a/test/ci-tree-reuse.test.ts b/test/ci-tree-reuse.test.ts index 1e0bde385..3c7318a4c 100644 --- a/test/ci-tree-reuse.test.ts +++ b/test/ci-tree-reuse.test.ts @@ -101,6 +101,11 @@ describe('findReuse: candidate discovery for a main push', () => { expect(rerunOlder).toMatchObject({ reused: false, reason: 'no_verified_run', detail: expect.stringContaining('land run 301 on gstack-land/42 reason=run_not_success') }); }); + test('a cancelled land run is passed over, as land itself passes it over: the older green run on the same tree is reused', () => { + const LAND2 = '7'.repeat(40); + expect(find(stale([landRun(), landRun({ id: 301, head: LAND2, conclusion: 'cancelled', started: '2026-10-30T10:40:00Z' })], { [LAND2]: TREE }))).toMatchObject({ reused: true, run_id: 300 }); + }); + test('a land run that tested another tree (main moved before the merge) is refused, and the suite runs', () => { const v = find(stale([landRun({ record: { tree: OTHER } })], { [LAND]: OTHER })); expect(v).toMatchObject({ reused: false, reason: 'tree_mismatch' }); diff --git a/test/land-merged-ci.test.ts b/test/land-merged-ci.test.ts index 6a9c98ee4..03c0f6cf2 100644 --- a/test/land-merged-ci.test.ts +++ b/test/land-merged-ci.test.ts @@ -178,6 +178,17 @@ describe('land.merged_ci reuses an earlier run on the merged tree (GST-3634)', ( expect(calls.at(-1)).toBe('remove gstack-land/42'); }); + test('a green run that is rerun between listing and polling decides on what land polled: red refuses as red and a timeout names it, never a fresh dispatch', () => { + const listed = earlier({}); + const rerun = { ...listed, run_attempt: 2, status: 'in_progress', conclusion: null }; + const red = fake({ earlier: [listed], runs: [[rerun], [{ ...rerun, status: 'completed', conclusion: 'failure' }]], verify: provesOld }); + expect(run(red.deps)).toMatchObject({ ok: false, red: true, run: 9 }); + expect(red.calls.some(c => c.startsWith('push ') || c.startsWith('dispatch '))).toBe(false); + const slow = fake({ earlier: [listed], runs: [[rerun]], verify: provesOld }); + expect(run(slow.deps, 60_000)).toMatchObject({ ok: false, red: false, run: 9 }); + expect(slow.calls.some(c => c.startsWith('push ') || c.startsWith('dispatch '))).toBe(false); + }); + test('a timed-out wait names the run the next land adopts, and that land adopts it after the scratch branch was deleted', () => { const going: ApiRun = { ...queued, id: 77, head_branch: 'gstack-land/42', head_commit: { tree_id: TREE } }; const first = fake({ runs: [[going]] });