ci: debounce macOS admission on pull request pushes

Merged
TE
2mo ago
+61-1

Part of #13095, the CI cost RFC.

Summary

When an agent pushes several commits to a PR in a row, each push cancels the previous CI run. By then the old run has usually started its macOS jobs, and those minutes are billed for nothing.

This PR adds a Linux job, macos-debounce, between the change router and the macos workflow call. On pull_request runs it waits CI_MACOS_ADMISSION_DEBOUNCE_SECONDS (default 180), so a follow-up push in that window cancels the run before any Mac starts. After the wait it checks that the PR head SHA is unchanged. If the head moved, the stale run fails. If the API call itself errors, macOS is admitted anyway (fail open). Merge groups, dispatches, reruns (run_attempt > 1) and runs routed away from macOS skip the wait. Setting the variable to 0 turns the wait off. Values above 600 are capped at 600.

Data (24 h, 2026-09-21 12:45 to 2026-09-22 12:45 UTC)

  • 1,253 CI runs. 739 were cancelled (59%).

  • Per-job timings for 975 of those runs (the API rate limit cut off the rest):

    • All macOS jobs: 19,539 billed minutes.
    • Cancelled runs: 3,407 minutes (17%). That extrapolates to about 4,400 minutes/day across all runs.
    • Failed runs: 11,840 minutes (61%).
    • Successful runs: 4,292 minutes.
  • Where the cancelled minutes went, by job: compile admission 1,752, app-host unit tests 933, swift-package-tests 250, release-build 173.

  • How long a run lived before a newer run replaced it:

    Replaced afterRunsmacOS min
    < 2 min20956
    2–5 min105211
    5–10 min89760
    10–30 min811,278
    > 30 min8953
  • Labels are not the cause. Only 2 cancellations came from a same-SHA rerun (a label change or reopen), costing 2 minutes in total. Across 286 recently active PRs, 36 of the 38 label events were full-ci. Filtering labeled/unlabeled would save close to nothing. It also could not prevent the cancellation, because the concurrency group cancels the old run before any job-level if is evaluated.

  • Drafts are not the cause either: 10 of those 286 PRs are drafts.

Expected savings

I replayed the 24 h of job timings with macOS admission gated at "changes finished + 180 s":

  • About 14% of cancelled-run macOS minutes saved: 456 in the sample, about 590/day.

  • The green signal on a finished run arrives 1.4 min later at the median and 3.1 min later at p90. A successful PR run currently takes 23 min.

  • Other settings of the variable, for comparison:

    WaitShare of cancelled minutes savedPer dayMedian added latency
    120 s9%~3600.4 min
    240 s20%~8302.4 min
    300 s26%~1,0703.4 min

This is a modest win. Most cancelled minutes come from runs replaced 5–30 min in, which no short wait can catch. Also, 12 cancelled full-suite runs account for 1,641 of the 3,407 cancelled minutes. And 76 failed full-suite runs, against 1 successful one, used 11,214 minutes: 57% of all macOS minutes in the window. Those failures are the larger target for a follow-up.

If CI_PERSISTENT_MAC_COMPILE is turned on, its route request is published from changes before the wait, so that path is not debounced. The variable is unset today.

Testing

  • New test_macos_admission_waits_for_pull_request_debounce in tests/test_ci_change_areas.py pins the job's wiring, including that ci-status waits for it.
  • These pass locally:
    • tests/test_ci_change_areas.py (all tests, with RUNNER_TEMP set as it is in CI)
    • test_ci_linux_guard_routing.py
    • test_ci_guard_workflow_structure.py
    • test_ci_merge_queue_required_checks.py
    • test_ci_reusable_workflow_permissions.py
    • test_ci_persistent_mac_compile.py
    • the quality, app-host and release guard-structure tests
    • test_ci_notification_semantics.py
    • test_ci_pull_request_caches_are_read_only.py
  • The workflow YAML parses. actionlint was not available locally; CI's workflow guard runs it.

Checklist

  • I tested the change locally

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Debounces macOS admission on pull request pushes so a quick follow-up push cancels the stale run before any Mac starts, saving billed macOS minutes (part of the CI cost RFC, #13095).

  • Adds a macos-debounce Linux job between the change router and the macos workflow that waits CI_MACOS_ADMISSION_DEBOUNCE_SECONDS (default 180, 0 disables, capped at 600), then fails if the PR head moved; API errors admit macOS.
  • Applies only to first-attempt pull_request runs. Merge groups, dispatches and reruns skip the wait.
  • A 24-hour replay of job timings shows roughly 14% of cancelled-run macOS minutes saved (~590/day) at default settings, with a median added latency of 1.4 minutes on the green signal.

Summary by CodeRabbit

  • CI Improvements

    • Added a short admission delay for macOS checks on pull requests, helping cancel superseded runs before macOS resources are used.
    • The macOS checks now proceed only when the pull request commit remains current after the delay.
    • Updated CI status reporting to account for the new macOS admission step.
  • Tests

    • Added coverage verifying the macOS admission delay, cancellation conditions, and CI job dependencies.