ci: cancel wasted macOS demand when the PR pool queue backs up

Merged
TE
2mo ago
+1077-0

Problem

PR macOS jobs route to blacksmith-6vcpu-macos-15 (ci-macos.yml runs-on, #13658), a pool that tops out around 9 concurrent jobs. At 17:00 UTC today it had 9 running and 22 queued, and the oldest queued job had waited 171 min. Part of that queue was demand nobody would read:

  • push-triggered experiment workflows on exp/* branches (the "Xcode incremental generation canary" held 8 macOS jobs for about 3h);
  • runs for PRs that are closed or merged, or whose head SHA has since moved on;
  • full-suite runs on PRs that no longer carry full-ci (labeled/unlabeled events deliberately don't cancel the in-flight run; see the concurrency comment in ci.yml).

Resulting behavior

A new CI queue janitor workflow runs every 10 minutes on vars.LINUX_RUNNER. It lists in-flight runs, counts queued macOS jobs, and does nothing until that count exceeds vars.CI_JANITOR_QUEUE_THRESHOLD (default 6). Above the threshold it cancels waste in this order:

  1. experiment: push runs on exp/*.
  2. stale-pr: PR runs whose PR is closed or merged, or whose head is no longer the PR head.
  3. label-dropped: full-suite ci.yml runs whose PR no longer has full-ci, only under CI_PULL_REQUEST_SUITE=compile-only, only when a newer CI run for that PR is already waiting to replace it, and never while that run's macOS compile admission is mid-flight. ci.yml keeps that compile alive on purpose so the queued run can reuse its product. Whether the old run was full-suite is read from the PR's label timeline at the run's creation time.

Draft PRs are deliberately not a category. A draft can be an active integration branch that other work depends on, and ci.yml has no ready_for_review trigger to replace a cancelled ci-status. A draft is still eligible under (2) if its PR is closed, merged or superseded.

It stops once the projected queue (queued minus the macOS jobs each cancel frees) is back at or below the threshold, with a hard cap of 10 cancels per sweep. It re-reads each run right before cancelling and skips it if the run already finished or its head changed.

These are never candidates, whatever their state: merge_group; push to main or rc/**; schedule/workflow_dispatch on main; release/tag refs and events; and any workflow whose name or path mentions release, nightly, TestFlight, App Store, publish or notarization. They still count toward the queue depth.

Every decision goes to the step summary with the run URL, reason, queued age, and the queued/running macOS jobs it held. The policy is pure functions in scripts/ci/queue_janitor.py over fetched JSON.

  • API budget: one list call per in-flight status (queued, in_progress, pending, up to 3 pages each). Jobs are fetched only for runs that can hold macOS work: workflows on main with no macOS runner are skipped, and so are day-old ghost queued runs. PR state, head SHA, labels and label timeline come from one batched GraphQL query covering only PR branches that hold macOS jobs. The live sweeps took 29-40 calls, roughly 175-240/hour at this cadence.
  • Controls: a manual dispatch defaults to dry_run: true. Scheduled sweeps cancel for real unless vars.CI_JANITOR_DRY_RUN == 'true', which works as a kill switch.
  • Permissions: actions: write, pull-requests: read, contents: read. There is one ci-queue-janitor concurrency group, and it never cancels an in-flight sweep.

Relation to existing guards. ci-stale-run-janitor.yml / cleanup-stale-runs.py (#13143) already reports closed-PR runs older than 24h. It is report-only on schedule, looks up PRs one commit at a time, and ignores queue pressure. This PR leaves it as is. The two overlap only on closed/merged PRs, and cancelling twice is harmless. merge-group-fail-fast only acts on merge groups, which this janitor never touches. persistent-macos-router only routes compiles. ci-status-fallback is a manual diagnostic.

Validation

Live dry run against manaflow-ai/cmux at 17:25 UTC today (python3 scripts/ci/queue_janitor.py --dry-run, CI_PULL_REQUEST_SUITE=compile-only). Nothing was cancelled:

## CI queue janitor (dry run)

Queued macOS jobs: **10** (threshold 6); running macOS jobs: 15.

| Decision | Run | Workflow | Reason | Queued age | macOS jobs (queued/running) |
| --- | --- | --- | --- | --- | --- |
| would cancel | https://github.com/manaflow-ai/cmux/actions/runs/35739420347 | CI | (stale-pr) superseded: PR #13579 head is now 53ab30c22 | - | 0/1 |
| would cancel | https://github.com/manaflow-ai/cmux/actions/runs/35755524387 | CI | (stale-pr) superseded: PR #13706 head is now f07a9ef5e | 42m | 1/0 |
| would cancel | https://github.com/manaflow-ai/cmux/actions/runs/35758007988 | CI | (stale-pr) superseded: PR #13713 head is now 91bc1ddba | - | 0/1 |
| would cancel | https://github.com/manaflow-ai/cmux/actions/runs/35758492563 | CI | (stale-pr) superseded: PR #13715 head is now 7a10ca281 | - | 0/1 |

_125 in-flight runs, 36 job listings, 11 PR branches, 40 API calls._

I checked all four by hand: each run's head SHA differs from its PR's current head (for example #13706's head is f07a9ef5e and the queued run is on 8ecd7f257). The draft #13643 no longer appears. The exp canary from this morning had already finished, so no experiment runs showed up at that moment.

Local checks:

  • python3 tests/test_ci_queue_janitor.py: 33 tests pass. They cover protected events, each category, PR resolution (including forks and reused branch names), open drafts never being planned, the threshold/priority/cap stop rule, the label-timeline decision, API fetch selection, the GraphQL batching and the workflow's shape.
  • python3 tests/test_ci_linux_guard_routing.py: OK.
  • bash tests/test_ci_self_hosted_guard.sh: exit 0, including "no workflow pins a bare GitHub-hosted runner".
  • python3 tests/test_ci_workflow_guards_are_wired.py, tests/test_ci_guard_workflow_structure.py, tests/test_ci_actionlint_covers_every_workflow.py, tests/test_cleanup_stale_runs.py: OK.
  • actionlint on the new workflow and on ci-guards.yml: clean.
  • python3 scripts/ci/validate_test_execution_registry.py: the new test is registered (linux-guard) and isn't flagged. The validator still exits 1 on the same 9 unregistered tests as main at c895b03af7, none of which this PR adds.

The new test runs in the ci group of workflow-guard-tests (a step in ci-guards.yml). PATH_OWNERS maps scripts/ci/queue_janitor.py and the workflow to ci, so editing either one reruns the test.

Remaining gaps

  • Live cancel path: only the dry-run mode has run against the real API. The live path (re-read the run, then POST .../cancel) is covered only by review, until the first scheduled sweep after merge. Setting CI_JANITOR_DRY_RUN=true before merge would give a few observed sweeps first.
  • macOS job detection: any job label containing macos counts, so the Warp and macOS 26 pools count toward the threshold along with the Blacksmith PR pool.

🤖 Generated with Claude Code