ci: cancel stale pull request runs - #694
Merged
Merged
Conversation
XuPeng-SH
force-pushed
the
ci/cancel-stale-pr-runs
branch
from
September 3, 2026 17:16
3947533 to
9b59f33
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
External fork
pull_requestruns are not admitted into workflow concurrency until a maintainer approves them. During that approval window,cancel-in-progress: truecannot supersede the previous head, so stale CI keeps consuming runners.This change closes that gap without giving fork code additional authority:
pull_request_targetcontroller that checks out the exact trustedgithub.workflow_shaand only calls the Actions API;beforeSHA as the sole cancellable generation, so an old controller can never cancel itsafterhead or a later push;before -> aftertransition must cancel its own prior generation during rapid pushes;head_sha, then require the exact source-repository ID and source ref because GitHub currently returns an emptypull_requestsarray for these fork runs;always()with!cancelled()in Test Suite and Static Checks, preserving fail-safe execution after scope-classifier failure while allowing a superseded workflow to terminate;scripts/ci/test_*.pycontract in Static Checks and document the behavior.Related issue
N/A. The failure was observed directly on Astra PR #693. Run
33779474796started at 16:34 UTC and continued until 16:51; its online and CLI jobs substantially completed before the run became cancelled. The replacement run started its jobs at 16:51. This also exposed that job-levelalways()resisted cancellation.Change type
User and compatibility impact
Fork PR updates stop spending runner time on superseded heads without waiting for replacement-run approval. Current and later heads are never cancellation candidates. Internal PRs incur no controller job. No product API, configuration, or runtime behavior changes.
Architecture and complexity delta
pull_request_target/!cancelled()patterns.always()conditions.Security boundary: the controller has only
actions: writeandcontents: read. It checks out exactlygithub.workflow_shawith credential persistence disabled, never checks out or executes the PR head, uses no repository secrets, sends its token only to the configured HTTPS API origin, and rejects redirects.Generation model: each synchronize event owns only its exact GitHub-provided
beforeSHA. Controllers are not coalesced, so processing any permutation of rapid updates cancels every prior generation while making the latest generation unreachable to older controllers.Verification
python3 -m unittest discover -s scripts/ci -p 'test_*.py'— PASS, 38 tests selected.python3 -m py_compile scripts/ci/cancel_stale_pr_runs.py scripts/ci/test_cancel_stale_pr_runs.py— PASS.python3 scripts/ci/validate_repository.py— PASS.go run github.com/rhysd/actionlint/cmd/actionlint@v1.7.12 .github/workflows/*.yml— PASS.git diff --check origin/main...HEAD— PASS after rebase onto87856b74e.head_repository.id,head_branch, andhead_shafields.pull_request_targetworkflow only from the default branch, so live acceptance begins after merge.github.workflow_sha; cancellation-resistant job conditions.Post-merge live acceptance plan:
Supersede stale PR runsstarts without external-contributor approval and cancels the exact prior Test Suite/Static Checks runs while preserving the new pending head.always()work to complete.developand verify the controller executes the script co-located with its trusted workflow revision.Final checklist