Add merge queue CI optimisation
The problem
We use stacks a lot, and often mid stack PRs have some CI failures that are fixed above.
When I merge a stack of 5 PRs, I don't need to run CI in each of them, it's OK to skip 1-4 and only run CI in 5th. It saves A LOT of wasted runner minutes.
Current state
You have this checkbox "Only merge non-failing pull requests":
Imagine we have added 5 stacked PRs to a merge queue, with CI like this PR 1 pass ✅ PR 2 pass ✅ PR 3 fail ❌ PR 4 pass ✅ PR 5 fail ❌
Checkbox set: It will run CI on 5 pull requests, merge 1-2 Checkbox not set: It will run CI on 5 pull requests, merge 1-4
Proposal
It is greedy to merge something now. I want it to merge a queued atomic stack instead.
In order to make it work, you need another checkbox "merge a queued stack atomically and only run CI on the last PR", where it will only run CI on PR 5, and it will not merge 1-2 or 1-4 of I queued 1-5.
However, if I only added 1-2 or 1-4 to the merge queue - it will merge, because the last queued PR succeeded.
For context: it is not rare for us to hit a 50 PRs limit in a graphite, and 20 PRs stacks are very usual. Saving 20x runner minutes is a huge deal.
We now have a working - although slightly cursed - GitHub Actions workaround for this.
The prerequisite is disabling “Only merge non-failing pull requests” (HEADGREEN).
For every merge_group, we run a cheap required preflight before expensive CI:
-
Checkout fetches all active
gh-readonly-queuerefs. -
Those refs form a commit chain:
gh-readonly-queue/<base>/pr-<PR number>-<previous candidate SHA> -
Given the current candidate’s head SHA, we find the ref that names it as its predecessor and verify that ref’s first parent. This identifies the immediate successor that GitHub is actively building.
-
We query the current and successor PRs’
stackandstackEntrymetadata. -
If they belong to the same stack and have consecutive stack positions, we intentionally fail the current candidate’s preflight and skip its expensive CI.
-
The final candidate runs full CI. Because it contains all preceding changes, its green result carries the deliberately red predecessors under
HEADGREEN.
For a contiguous stack:
A1 ❌
A2 ❌
A3 ✅ full CI
This reduces a stack of N PRs from N full CI runs to one full run plus N cheap preflights.
We initially tried to implement this through the API:
repository.mergeQueuereturnedResource not accessible by integrationwith the ephemeralGITHUB_TOKEN.PullRequest.mergeQueueEntrywas accessible, but concurrent merge-group runs received inconsistent/incomplete queue views- even though every runner had already fetched the complete candidate-ref chain.- Sleeping would not solve that, so we use Git’s fetched queue refs for adjacency and the API only for stable stack metadata.
This works, but it relies on GitHub’s internal queue-ref naming and deliberately produces red checks on intermediate PRs. A native “validate and merge this queued stack atomically using only its final candidate” option would be much safer, clearer, and less brittle.
Six PRs are open in this repo. Two can't merge into main without a rebase first and one is already up to date, so only three carry real work — but those three don't have to be tested one after another.
Of the 15 possible pairs, 7 don't share a single file and can run at the same time. The other 8 share at least one, and cmd/utils.go alone is touched by 4 of the 6. That's 4 independent batches instead of 6 serial runs, assuming each branch is rebased onto main first.
Sharing no file is necessary, not sufficient — a repo-wide test can still couple two of them. Otherwise ignore this; no reply needed.