Skip to content

feat(ci): the merge bus — batch the ready PRs and prove the combination green - #448

Merged
athvin merged 2 commits into
mainfrom
bm/ci-merge-bus
Nov 5, 2026
Merged

athvin merged 2 commits into
mainfrom
bm/ci-merge-bus

Conversation

@athvin

@athvin athvin commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Why

main's protection has strict: false — "require branches up to date before
merging" is off — so a PR merges on a green it earned against a base that may since
have moved. With five or six PRs open at once that is not theoretical: four PRs each
green against an older main can all merge and leave main red, because no run ever
examined the combination.

This adds that run.

What it does

merge-bus.yml (dispatch only, dry_run: true by default):

  1. Selects open PRs carrying the merge-bus label, oldest first, capped at
    max_batch (input, else the MERGE_BUS_MAX_BATCH variable, else 4).
  2. Builds the combination on merge-bus/<run_id> cut from main's tip.
  3. Opens an ordinary PR for it, so the existing gate judges the combination.
  4. Green → squash-merges the members in order. Red → merges nothing.

Why not GitHub's merge queue

It is this feature, and it cannot be enabled here: merge queue is organization-only,
with private repositories needing Enterprise Cloud. Probed rather than assumed —
gh api repos/:owner/:repo/rulesets returns [], and
repository(…).mergeQueue(branch:"main") returns null.

The self-hosted options are worse rather than better. bors-ng — the original,
which batches and bisects — was archived 2024-04-04 and its README now points at
GitHub's merge queue. rust-lang/bors and Prow's tide are alive and capable
but each is a long-running GitHub App wanting a hosted service, a public webhook
endpoint and an App private key: more standing credential than a workflow file, not
less. Each also replaces branch protection as the merge authority, restating the
required checks in bot config where they can drift from it silently.

Design notes worth reviewing

  • It opens a PR rather than emulating merge_group, and that is why it cost no
    edits elsewhere.
    The three required workflows trigger on bare pull_request, so
    a batch branch earns every required context with correct detect behaviour. The
    native queue would have been more work: merge_group is not a pull_request
    event, so all three detect jobs fall through to their fail-safe default
    (code=true, frontend=true, "assuming infra changed") and every merge-group
    build would run every gate at full cost.

  • Eligibility asks GitHub instead of restating the required set. With reviews at
    zero, mergeStateStatus == CLEAN is "every required check is satisfied", so no
    context is enumerated here and nothing can drift from protection — and no
    admin-scoped protection read is needed, which GITHUB_TOKEN does not have.

  • DIRTY is rejected by name. A conflicted PR runs zero workflows and reports
    "no checks reported", so any predicate phrased as "nothing is failing" reads it as
    green. Related: gh pr list returns mergeStateStatus: UNKNOWN and does not
    trigger the computation
    — only gh pr view does, so the authoritative read is
    per-PR with a retry.

  • Two invariants make the landing sound. main must still be at the SHA the
    batch was cut from, or the green no longer describes what merging would produce.
    And after the squashes, main's tree must equal the tree the gate examined —
    because state: MERGED is not evidence. A stacked PR whose base branch is deleted
    gets marked MERGED with a real mergeCommit while its code never reaches main
    (observed on fix(deploy): drain on SIGTERM instead of dying mid-response #438). For the same reason the bus retargets stacked children onto
    main before merging their parent
    , which is the only point at which
    gh pr edit --base still works.

  • It stops on red and does not bisect. The member comments say the combination
    failed; they do not accuse any one PR. The bus PR is left open because its branch
    is the only place the failing combination exists. A conflict is ejected instead
    of stopping the batch — that is found before any CI is spent and names the culprit
    exactly.

Required status checks

merge bus must never become one. It runs on workflow_dispatch only, so it can
never report on a PR, and a required context that never reports blocks every PR
forever. The generated required-checks.md is regenerated in this commit, which is
mandatory rather than tidy: scripts/skills-check.sh derives that table from
.github/workflows/*.yml, and its drift check runs inside the required
fmt + clippy + docs job.

Scopes

This is the first hand-written workflow here with contents: write and
pull-requests: write; the other four are contents: read plus id-token: write.
It adds no repository secret — GITHUB_TOKEN is minted per run. It is
deliberately not an admin, which tightens rather than loosens things: every merge
it makes must satisfy the required checks, whereas a human running
gh pr merge --admin bypasses all of them.

Repository configuration this depends on

Applied out-of-band (none of it a secret):

gh api -X PUT repos/:owner/:repo/actions/permissions/workflow \
  -f default_workflow_permissions=read -F can_approve_pull_request_reviews=true
gh api -X PATCH repos/:owner/:repo/branches/main/protection/required_pull_request_reviews \
  -F required_approving_review_count=0
gh label create merge-bus / bus-failed / bus-conflict
gh variable set MERGE_BUS_MAX_BATCH --body 4

The first is needed because GITHUB_TOKEN cannot open a PR while "Allow GitHub
Actions to create and approve pull requests" is off.

Verification

Done locally:

Still to do, per this repo's rule that a gate is trusted only after it has been
observed failing
: watch the red path, the main-moved abort, and a real green
landing at N=2 before raising max_batch or adding a schedule:.

🤖 Generated with Claude Code

@athvin

athvin commented Nov 3, 2026

Copy link
Copy Markdown
Owner Author

Rebased onto current main (picking up the new 12th required context, postgres.js driver contract — which this PR will report as skipped, since frontend.yml's detect is an allowlist on frontend/ and this diff touches none of it).

Added: the bus now tells a bad batch apart from a starved account

While verifying this, the account ran out of Actions minutes. Every job on every branch — main included — began failing within seconds with steps: [] and a BlobNotFound log, while githubstatus.com read operational throughout. It has been that way for hours.

That is a problem for the bus as originally written: it would have seen FAILURE on every required context and reported "the batch went red", sending someone to debug a diff that was never compiled.

So the bus now reads execution rather than conclusion — a starved run and a legitimately failing one are both failure, but only one of them ran a step:

gh api repos/:owner/:repo/actions/runs/<id>/jobs --jq '[.jobs[].steps | length] | add'

Measured on this repo: starved runs sum to 0 steps, real ones to 5, 5 and 202. Two uses:

  • a pre-flight that refuses to build a batch when none of the last five completed runs executed a step, so no branch or PR is created during an outage;
  • a verdict classification of starved rather than red if capacity runs out mid-validation — no bus-failed label, and no comments on the members, because during an outage every PR in the repo is equally stuck.

Cause 0 is now documented in the ci-and-supply-chain skill, which had no entry for it at all.

Verified offline (no CI required)

Both gates were observed working on real failures, not just passing:

Blocked on

Nothing in this PR. The gate cannot go green until the account's Actions spending limit is raised or the monthly allowance resets — the three runs on this head failed with 0 steps executed, same as every other branch.

@athvin

athvin commented Nov 3, 2026

Copy link
Copy Markdown
Owner Author

Added: a simulator, and mutation testing that found a real blind spot

workflow_dispatch only fires for a workflow already on the default branch, so the bus's live path cannot be exercised until it is merged. That is a poor place to have no tests for 540 lines of bash that merges to main. So: bash scripts/ci/merge-bus-test.sh.

It runs the workflow's real step scripts with a runner's semantics for the parts that carry bugs — per-step $GITHUB_OUTPUT, ${{ }} resolution in env:, if: gating, a failed step halting the job — against a local bare repo seeded with branches and the refs/pull/<n>/head refs GitHub synthesizes. gh is stubbed; git is genuine.

Ten scenarios, 45 assertions, covering the green path and every failure path: green stacked red starved preflight moved partial reject collide lying. This is the first time the land step has ever executed — the retarget, the squash loop, the BASE_SHA abort and the tree assertion were previously only reasoned about. Notable results:

  • stacked asserts on ordering: pr edit 201 --base main must appear in the call log before pr merge 101, because after the parent merges it is too late.
  • moved lands an unrelated commit on origin/main the moment the bus believes the batch is green, and the abort fires.
  • partial fails a mid-sequence merge and confirms the report names which PRs landed and which did not.
  • starved confirms no bus-failed label and no comments on members — during an outage every PR is equally stuck.

Mutation testing, and the blind spot it caught

Every guard was deliberately deleted from the workflow to confirm the suite goes red. Seven mutants, seven caught — but only after a fix:

mutant caught?
no-basesha, no-retarget, starved-blames, no-dirty-reject, no-preflight, no-eject-continue yes, first time
no-tree-assert no — every scenario still passed

Deleting the land step's tree assertion changed nothing, because in all nine scenarios the tree matched. The check could not fail, so it was not yet a check — exactly the defect this repo's "observe it failing" rule exists to catch, in my own work.

The lying scenario closes it: gh pr merge reports success while landing nothing, which is the real shape of the trap the assertion guards (a stacked PR marked MERGED, with a real mergeCommit, whose code never reached main). With it, the mutant is caught and the assertion is load-bearing.

Hand-run, like a skill's evals.md, and deliberately not a CI job: it needs python3 with PyYAML, and a required check may not be added here before it has been observed failing.

Still blocked

Unchanged — the account's Actions quota. Every run on every branch, main included, still fails with 0 steps executed.

bmoore813 and others added 2 commits November 4, 2026 19:09
…on green

`main` has `strict: false`, so a PR merges on a green it earned against a base
that may since have moved. With five or six PRs open at once that is not
theoretical: four PRs each green against an older `main` can all merge and leave
`main` red, because no run ever examined the combination.

`merge-bus.yml` is that run. It batches the PRs carrying the `merge-bus` label,
opens an ordinary PR for the combination so the existing gate judges it, and
squash-merges the members in order only if it goes green. Red merges nothing.

GitHub's merge queue is exactly this feature and cannot be enabled here — it is
organization-only, private repositories needing Enterprise Cloud. Probed rather
than assumed: `rulesets` is `[]` and `mergeQueue(branch:"main")` is `null`. The
self-hosted options are worse: bors-ng is archived, and rust-lang/bors and Prow's
`tide` are GitHub Apps wanting a hosted service, a webhook endpoint and an App
key, each restating the required checks where they can drift from protection.

Opening a PR rather than emulating `merge_group` is what made this cost no edits
elsewhere: the three required workflows trigger on bare `pull_request`, so a
batch branch earns every context with correct `detect` behaviour. The native
queue would have been more work — `merge_group` is not a `pull_request` event, so
all three `detect` jobs fall through to their fail-safe default and every
merge-group build would run every gate at full cost.

Eligibility asks GitHub instead of restating the required set: with reviews at
zero, `mergeStateStatus == CLEAN` IS "every required check is satisfied", so no
context is enumerated here and nothing can drift. `DIRTY` is rejected by name,
because a conflicted PR runs zero workflows and reports no checks — "nothing is
failing" would read that as green. `gh pr list` returns UNKNOWN and does not
trigger the computation, so the authoritative read is a per-PR `gh pr view`.

Two invariants make the landing sound. `main` must still be at the SHA the batch
was cut from, or the green no longer describes what merging would produce. And
after the squashes `main`'s TREE must equal the tree the gate examined — because
`state: MERGED` is not evidence: a stacked PR whose base branch is deleted is
marked merged, with a real mergeCommit, while its code never reaches `main`. For
that reason the bus also retargets stacked children onto `main` before merging
their parent, which is the only time `gh pr edit --base` still works.

It also tells a bad batch apart from an account that has run out of Actions
minutes. That state fails every job in seconds with `steps: []` and no log,
repository-wide, while githubstatus reads operational — so a bus run during one
would otherwise accuse the batch of a failure nothing compiled. The bus reads
EXECUTION rather than conclusion (measured here: starved runs sum to 0 steps,
real ones to 5, 5 and 202), refuses to build when the last five completed runs
all executed nothing, and reports `starved` rather than red if capacity runs out
mid-validation — no label, and no comment on the members, because during an
outage every PR is equally stuck. Cause 0 is now documented in the skill, which
had no entry for it.

Dispatch-only and `dry_run: true` by default, so the predicate can be exercised
before it is trusted. `merge bus` must never become a required status check.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`merge-bus.yml` is bash that merges to `main`, and `workflow_dispatch` fires
only for a workflow already on the default branch — so its live path cannot be
exercised until it is merged. That is a bad place to have no tests.

`scripts/ci/merge-bus-test.sh` runs the workflow's REAL step scripts with a
runner's semantics for the parts that carry bugs: per-step `$GITHUB_OUTPUT`,
`${{ }}` resolution in `env:`, `if:` gating, and a failed step halting the job.
`gh` is stubbed; git operations are genuine, against a local bare repo seeded
with branches and the `refs/pull/<n>/head` refs GitHub synthesizes.

Ten scenarios cover the green path and every failure path: green, stacked, red,
starved, preflight, moved, partial, reject, collide, lying. 45 assertions.

Every scenario was mutation-tested — the matching guard deleted from the
workflow, the suite confirmed red. Seven mutants, seven caught: no-basesha,
no-retarget, starved-blames, no-dirty-reject, no-tree-assert, no-preflight,
no-eject-continue.

That exercise earned its keep immediately. Deleting the land step's tree
assertion left EVERY scenario still green, because in all of them the tree
matched — the check could not fail, so it was not yet a check. The `lying`
scenario closes it by having `gh pr merge` report success while landing
nothing, which is the real shape of the trap it guards: a stacked PR marked
MERGED, with a real mergeCommit, whose code never reached `main`. With it, the
mutant is caught and the assertion is load-bearing.

Hand-run, like a skill's `evals.md`, and deliberately not a CI job: it needs
python3 with PyYAML, and a required check may not be added here before it has
been observed failing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@athvin
athvin merged commit 2df35a3 into main Nov 5, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants