Skip to content

ci: cancel doomed PR runs after required job failures - #28591

Closed
XuPeng-SH wants to merge 6 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/cancel-doomed-pr-ci
Closed

XuPeng-SH wants to merge 6 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/cancel-doomed-pr-ci

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Related to #28419

What this PR does / why we need it:

When one dependency of CI Required has already failed, the PR workflow cannot become green, but sibling test jobs can keep consuming runners for tens of minutes.

This adds a repository-owned watchdog that scans every five minutes and cancels doomed, in-progress PR workflows. Cancellation fails closed: it checks trigger-time metadata, matching local gate-policy blobs, exact required-job names, an approved immutable reusable-workflow revision, and the final run attempt/status before acting. Failures in non-gating jobs such as Utils, Upgrade, Shared Build, CodeQL, and TKE are ignored. Old runs, 3.0-dev runs, changed policies, and unknown external revisions are left untouched and reported.

The controller runs only from the default branch, checks out trusted CI code with persisted credentials disabled, and has only contents: read plus actions: write. Manual dispatch defaults to dry-run. Its bounded summary reports cancellations, races, skips, inspection errors, failed job, and conclusion. The currently approved MatrixOrigin CI workflow revision is e43e7077e87ebdcefa66b12e3b1778b984cc3ce2; unsupported revisions fail closed.

Design: docs/design/ci_required_fail_fast_cancellation.md

Validation on base 4f9d170a22deaabc2eee6b239a51a8cb40a39f9f:

  • node --test .github/ci/change-scope.test.cjs .github/ci/cancel-doomed-runs.test.cjs (28/28 passed)
  • GOWORK=off go run github.com/rhysd/actionlint/cmd/[email protected] .github/workflows/entrypoint.yaml .github/workflows/cancel-doomed-pr-ci.yaml
  • git diff --check origin/main...HEAD
  • GPT-6 medium design and final review: PASS, no blockers
  • No live cancellation API call was made from the feature branch

@XuPeng-SH
XuPeng-SH requested a review from fengttt as a code owner September 10, 2026 04:08
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Force review of ac9441b against base/merge-base 2a6fe7f. COMMENT only (self-authored PR).

Design decision: PASS for docs/design/ci_required_fail_fast_cancellation.md at this exact head, Git blob 0f3e580256e3015668418d377db114307d0acd31. I reviewed the design before completing implementation acceptance; the separate design-first commit is 46bfd5b. This comment records the precise independent decision rather than relying solely on the PR body's earlier unlinked PASS assertion. The required-job authority, default-branch trust boundary, rollout exclusions, policy-drift behavior, bounded controller lifetime, and non-atomic cancellation limitation are explicit and acceptable for the stated best-effort resource-saving contract.

Implementation conclusion: no concrete blocker found. Cancellation requires an exact authorized terminal failed job in the same run attempt, trigger-time metadata, matching local gate blobs, and (for external jobs) the reviewed immutable reusable-workflow SHA. Pending/in-progress, skipped, neutral, unrelated job names, old attempts, unsupported workflow revisions and 3.0-dev do not grant authority. The unchanged scope guards ensure active authorized jobs belong to that run's gate closure. A single failed required dependency is sufficient to doom the AND gate; other dependencies need not finish first. Existing CI Required display name/dependencies are preserved.

I checked the supported external workflow revision 2ab1af3a85ca3c4e4eab7483201aca3e1913dd99 and the authorized job failure propagation. I also inspected real Actions run metadata: run.head_sha and referenced_workflows path/SHA fields match the representation consumed here. The final run status/attempt/head recheck rejects stale decisions; 409 is treated as a race, other errors are surfaced, and processing continues safely for other runs. There is deliberately no API call against a different run/workflow or PR-selected code. Default-branch-only dispatch, pinned controller actions, sparse trusted checkout, disabled persisted credentials and contents:read/actions:write preserve the intended boundary.

Q1/Q2/Q3: no persistent external worker is introduced; scans are serialized, jobs/runs are paginated, policy lookups are cached per base SHA, mutation is sequential, and the workflow has a five-minute timeout. Summary rows are capped at 100. Large backlogs or API throttling may reduce progress before timeout, but do not authorize uncertain cancellations. Cancellation sacrifices remaining diagnostic/coverage work only in a run already unable to satisfy the required gate; it does not claim those interrupted tests passed.

Accepted operational limitations: GitHub's cancel API has no attempt precondition, so completion plus rerun between the final GET and POST remains a documented residual race, not atomic compare-and-cancel. A change to local gate blobs or any unapproved external revision pauses watchdog authority until reviewed. Scheduled enforcement is active after merge; perform the documented trusted dry-run promptly and monitor watchdog errors rather than interpreting them as product-test failures.

Validation: all 21 exact-head Node tests passed using read-only git-object loading; git diff --check passed. Inspected current diff, commit sequence, PR/issue comments and live API shapes. No live cancellation, full CI wait, or feature-branch execution with actions:write was performed. No performance saving is asserted beyond the design's expected earlier release of doomed-run resources.

@mergify

mergify Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

XuPeng-SH added a commit that referenced this pull request Sep 11, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Related to #28419 (CI Required / coverage failure reporting)
Related to #27291 and #27293 through #28529's CI startup blocker

## What this PR does / why we need it:

`pull_request_target` currently checks out `.github/ci` from the pull
request base SHA in the `change-scope` and `ci-required` jobs. A
`4.2-dev` pull request can therefore load a workflow that references
`change-scope.cjs` while its base commit does not contain that helper,
failing before product tests start with `Cannot find module
'.github/ci/change-scope.cjs'`.

Both jobs now load the repository-owned CI helper from `${{
github.workflow_sha }}`, the commit that supplied the running workflow.
The PR base and head SHAs remain event metadata only, and all checkout
credentials remain disabled. A checkout-independent provenance step
writes the workflow, base, and head SHAs before loading the helper, so
missing-helper or checkout failures retain diagnostics. No test routing,
required-check policy, or product code is changed.

The existing `3.0-dev` route and failure semantics remain unchanged.
This PR is intentionally separate from #28591, whose purpose is
cancelling doomed PR runs.

## Validation

- Baseline red check: the old workflow fails the trusted workflow-SHA
checkout contract.
- `node --test .github/ci/change-scope.test.cjs` (10/10 passed)
- The fixture parses the actual checkout configuration for both jobs,
resolves the event expressions against separate base/workflow/head
trees, and executes the selected workflow helper. The base tree lacks
the helper, the workflow tree contains the trusted classifier and
verifier, and the head tree contains a malicious same-name helper. Both
jobs select the workflow tree; the classifier and `verifyResults` are
executed from that same trusted version.
- The contract test rejects a floating `main` ref, requires provenance
before checkout, and executes the provenance shell against a simulated
checkout failure to verify the summary remains available.
- `actionlint v1.7.12 .github/workflows/entrypoint.yaml` (passed)
- `git diff --check` (passed)
- The first CI run was correctly blocked by an invalid PR reference to
#28529 (a PR, not an issue). The body now references valid issues
#28419, #27291, and #27293.
- After this PR merges, GitHub's `pull_request_target` behavior uses the
repository default-branch workflow source; a fresh #28529 run can
therefore be created directly without a `4.2-dev` CI-file backport. That
run must prove the workflow SHA contains this fix, classification
succeeds, and required tests actually start. See [GitHub's
change](https://github.blog/changelog/2025-11-07-actions-pull_request_target-and-environment-branch-protections-changes/).

QA required: no; this is a workflow/test-only change and does not alter
MatrixOne runtime behavior. Complete CI startup and classification
validation is a post-merge check.

---------

Co-authored-by: XuPeng-SH <[email protected]>
@XuPeng-SH
XuPeng-SH force-pushed the codex/cancel-doomed-pr-ci branch from ac9441b to 05eb70b Compare September 12, 2026 06:06
@XuPeng-SH
XuPeng-SH force-pushed the codex/cancel-doomed-pr-ci branch from 05eb70b to 0a76bde Compare September 12, 2026 08:22

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep review of b73dfc45befd756a794e345bc51f7f148c91a39b (self-authored PR; comment only).

P2 blocker: compare policy at the workflow revision that actually ran, not the PR target branch tip.

cancel-doomed-runs.cjs obtains local policy blobs at metadata.baseSha (the run name records github.event.pull_request.base.sha). But this pull_request_target entrypoint loads its preflight and gate scripts from github.workflow_sha; the event executes the trusted default-branch workflow. Those revisions are not interchangeable.

Concrete case: current 4.2-dev tip is 7d150ee116c705c47a9106ad5ff86aed7e592b7d; it has no .github/ci/change-scope.cjs (Contents API returns 404). After merge, a PR targeting 4.2-dev runs the main/default-branch gate, but this watchdog looks up the release-branch blob and rejects every run before inspecting jobs. Thus doomed release-target CI runs are never cancelled, and the watchdog reports errors for healthy runs too.

Please include workflow_sha=${{ github.workflow_sha }} in strict run metadata and compare the policy blobs at that immutable SHA. Keep baseRef for the 3.0-dev exclusion and baseSha only for diagnostics. Add a test where workflow SHA differs from base SHA: matching blobs at workflow SHA must authorize; a mismatch at workflow SHA must fail closed even if base blobs match. Update the design's provenance wording.

This review otherwise found the exact-head required-job policy, attempt checks, permissions boundary, cancellation race handling, and tests coherent. Exact-head CI and scope contract are green; the issue is specifically the policy provenance mismatch above.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT-6 medium deep review at head dc2e50cdd64e26de9ba556f3a7194349b3e853ca: no blockers or material gaps found. Reviewed caller/gate conditions, rerun and cancellation races, trust boundaries, and Mergify interaction. The CI scope contract reports 42/42 tests passed, including watchdog tests; no additional unhandled race was verified beyond the documented GitHub cancel endpoint window.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Pure rebase update: started from remote head dc2e50cdd64e26de9ba556f3a7194349b3e853ca and rebased onto latest main c61ed374a8; new head 0f2741de5bc5911b1252fd7b6dcae4335614d867. No semantic changes were made; git diff --check passes. This PR had no new terminal failure or unresolved review blocker. Only the personal feature branch was force-with-lease updated; CI intentionally not awaited.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT-6 medium deep review at exact head 0f2741d.

I reviewed the doomed-run cancellation/watchdog path and its tests; I found no verified blocker on this head. The watchdog test is 1.13 ms and the Node suite passed. This is a comment-only review because the PR is self-authored.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Post-rebase status: the PR branch is now at 2e5be160fa81195262e291ef2d24b0bb5161958f, rebased onto main afbb411d5d and pushed to the existing XuPeng-SH feature ref. The latest review found no blocker; Node controller tests remain green. CI is intentionally not awaited.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT-6 medium deep review at 2e5be160fa81195262e291ef2d24b0bb5161958f — PASS; no blocking findings.

I reviewed the watchdog’s trigger-time provenance, local policy-blob equality, exact required-job classification, external reusable-workflow revision allowlist, latest-attempt filtering, and pre-cancel run-state recheck. The final revision correctly keys local policy validation to the workflow SHA recorded by the PR-target run, rather than the target branch’s base SHA; unknown or drifted policy revisions fail closed. The scheduled workflow is restricted to the default branch and grants only actions:write/contents:read; manual dispatch defaults to dry-run. Current-head CI is green. No live cancellation was exercised during this review.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PASS — fresh Astra medium deep review of 8b33652c453031baab87e96c2f439b47e8757b55; COMMENT only because this is self-authored. No actionable P0–P2 findings.

Reviewed the complete watchdog/controller/reusable-workflow chain and the delta from the previous reviewed head 2e5be160; the six PR files are unchanged. Verified strict policy/workflow-SHA validation, exact required-job classification, latest-attempt filtering, reusable-workflow provenance, final run-state recheck, scoped cancellation, and fail-closed behavior. Contract validation passed 42/42 on the identical tree; git diff --check passed.

Residual evidence gaps: no live dry-run/cancellation exercise; pagination/rate-limit behavior is mocked; the documented GET→POST rerun race remains. Current external CI revision bff80f0f… is not allowlisted, so cancellation remains disabled for that revision until reviewed.

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement kind/test-ci size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants