Repository navigation
ci(queue janitor): skip superseded nightly, DEV and promote runs whose build never started #26200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d0ee903
c9a96ce
a190f8b
7c56577
51460e1
c42081d
a617f88
e50101c
6873353
a343471
09683c1
dd37ba7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -160,6 +160,16 @@ | |
| (CI_JANITOR_ORPHAN_MINUTES, 120 by default) and never re-runs; a run (e) | ||
| cancels this sweep is excluded from it. An orphan at an open pull request's | ||
| head, whatever its event, is skipped like every category's (open_pr_at_head()). | ||
|
|
||
| Superseded delivery runs are one more pass (skip_superseded_delivery): "When | ||
| nightly runs for older commits are stuck waiting (queued for a runner or a | ||
| gate), skip them and build from the tip: cancel the stale runs" (Leo, | ||
| 2026-10-26). A nightly, DEV or promote run is cancelled only when | ||
| delivery_workflows.superseded_unstarted() says so: its build has not started | ||
| and a newer head of its branch has its own run of the same workflow. The | ||
| agents' cancel guard reads that same function, so the two never disagree. A | ||
| run's jobs are read again right before its cancel, and a run whose build | ||
| started in between is kept. Its own cap; no force-cancel. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
@@ -194,7 +204,7 @@ | |
| import owned_warm_state # noqa: E402 | ||
| import ui_tests_dispatch # noqa: E402 | ||
| import starved_labels # noqa: E402 | ||
| from delivery_workflows import is_delivery_run # noqa: E402 | ||
| from delivery_workflows import is_delivery_run, build_started, superseded_unstarted # noqa: E402 | ||
|
|
||
|
|
||
| API = "https://api.github.com" | ||
|
|
@@ -2285,6 +2295,117 @@ def orphan_protected_reason(run: Mapping[str, Any]) -> str | None: | |
| PULL_REQUEST_EVENTS = ("pull_request", "pull_request_target") | ||
|
|
||
|
|
||
| # --- superseded delivery runs ---------------------------------------------------------------------------------- | ||
| SUPERSEDED_MAX_CANCELS = 10 | ||
| # A candidate with no newer run among the in-flight ones gets one listing of its workflow's runs on its branch | ||
| # (the newer run may have finished already); at most this many listings a sweep. | ||
| SUPERSEDED_BRANCH_LISTINGS = 3 | ||
|
|
||
|
|
||
| def superseded_candidates(runs: Sequence[Mapping[str, Any]], | ||
| jobs_by_run: Mapping[int, Sequence[Mapping[str, Any]]]) -> list[Mapping[str, Any]]: | ||
| """In-flight nightly, DEV or promote runs whose build has not started (no I/O), oldest first.""" | ||
| found = [run for run in runs | ||
| if run.get("status") in IN_FLIGHT_RUN_STATUSES and run.get("id") in jobs_by_run | ||
| and is_delivery_run(run.get("path"), run.get("name"), run.get("display_title")) | ||
| and build_started(jobs_by_run[run["id"]]) is None] | ||
| return sorted(found, key=lambda run: (str(run.get("created_at") or ""), run["id"])) | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class SupersededDecision: | ||
| run: Mapping[str, Any] | ||
| action: str # "cancel" or "keep" | ||
| note: str | ||
|
|
||
|
|
||
| def skip_superseded_delivery( | ||
| github: "GitHub", | ||
| candidates: Sequence[Mapping[str, Any]], | ||
| runs: Sequence[Mapping[str, Any]], | ||
| jobs_by_run: Mapping[int, Sequence[Mapping[str, Any]]], | ||
| prs_by_branch: Mapping[str, Sequence[Mapping[str, Any]]], | ||
| *, | ||
| dry_run: bool, | ||
| max_cancels: int = SUPERSEDED_MAX_CANCELS, | ||
| ) -> tuple[list[SupersededDecision], int]: | ||
| """Cancel each candidate delivery_workflows.superseded_unstarted() allows, re-reading its jobs first.""" | ||
| decisions: list[SupersededDecision] = [] | ||
| failures = 0 | ||
| listed: dict[tuple[Any, str], list[Mapping[str, Any]]] = {} | ||
| cancels = 0 | ||
| for run in candidates: | ||
| run_id = run["id"] | ||
| ok, why = superseded_unstarted(run, jobs_by_run.get(run_id), runs) | ||
| if not ok and why.startswith("no newer head"): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| key = (run.get("workflow_id") or run.get("path"), str(run.get("head_branch") or "")) | ||
| if key not in listed and len(listed) < SUPERSEDED_BRANCH_LISTINGS: | ||
| try: | ||
| listed[key] = github.branch_runs(run) | ||
| except RateLimited: | ||
| raise | ||
| except RuntimeError as error: | ||
| listed[key] = [] | ||
| print(f"queue-janitor: runs of {key[1]} for run {run_id}: {error}", file=sys.stderr) | ||
| ok, why = superseded_unstarted(run, jobs_by_run.get(run_id), [*runs, *listed.get(key, [])]) | ||
| if not ok: | ||
| decisions.append(SupersededDecision(run, "keep", why)) | ||
| continue | ||
| at_head = open_pr_at_head(run, prs_by_branch.get(str(run.get("head_branch") or ""), ())) | ||
| if at_head is not None: | ||
| decisions.append(SupersededDecision(run, "keep", f"at the head of open PR #{at_head.get('number')}; " | ||
| f"{OPEN_HEAD_RULE}")) | ||
| continue | ||
| if cancels >= max_cancels: | ||
| decisions.append(SupersededDecision(run, "keep", f"cap of {max_cancels} reached")) | ||
| continue | ||
| if dry_run: | ||
| decisions.append(SupersededDecision(run, "cancel", f"would cancel: {why}")) | ||
| cancels += 1 | ||
| continue | ||
| # The jobs again, right before the cancel: a run whose build started since is kept. | ||
| try: | ||
| current, jobs = github.run(run_id), github.jobs(run_id) | ||
| except RateLimited: | ||
| raise | ||
| except RuntimeError as error: | ||
| failures += 1 | ||
| decisions.append(SupersededDecision(run, "keep", f"failed to re-read: {error}")) | ||
| continue | ||
| ok, why = superseded_unstarted(current, jobs, [*runs, *[r for rs in listed.values() for r in rs]]) | ||
| if not ok: | ||
| decisions.append(SupersededDecision(run, "keep", f"on re-read: {why}")) | ||
| continue | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| try: | ||
| github.cancel(run_id) | ||
| except RuntimeError as error: | ||
| if not _cancel_refused(error): | ||
| failures += 1 | ||
| decisions.append(SupersededDecision(run, "keep", f"cancel failed: {error}")) | ||
| continue | ||
| decisions.append(SupersededDecision(run, "cancel", f"cancelled: {why}")) | ||
| cancels += 1 | ||
| return decisions, failures | ||
|
Comment on lines
+2390
to
+2448
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
| def render_superseded_summary(decisions: Sequence[SupersededDecision]) -> str: | ||
| lines = ["", "### Superseded nightly, DEV and promote runs", "", | ||
| "A delivery run whose build never started, at a head a newer run of its workflow on the same branch " | ||
| "has superseded, is skipped so the tip builds (delivery_workflows.superseded_unstarted()).", ""] | ||
| if not decisions: | ||
| lines.append("None waiting.") | ||
| return "\n".join(lines) + "\n" | ||
| lines.append("| Decision | Run | Workflow | Head | Why |") | ||
| lines.append("| --- | --- | --- | --- | --- |") | ||
| for decision in decisions: | ||
| run = decision.run | ||
| url = run.get("html_url") or f"run {run.get('id')}" | ||
| name = str(run.get("name") or run.get("path") or "").replace("|", "\\|") | ||
| lines.append(f"| {decision.action} | {url} | {name} | {str(run.get('head_sha') or '')[:12]} | " | ||
| f"{decision.note.replace('|', '/')} |") | ||
| return "\n".join(lines) + "\n" | ||
|
|
||
|
|
||
| def left_to_github(orphan: Orphan, now: dt.datetime) -> bool: | ||
| """A ghost past GHOST_GIVE_UP_AGE that no human was asked to look at.""" | ||
| return (orphan.job_name is None and orphan.queued_since is not None | ||
|
|
@@ -2812,6 +2933,13 @@ def runs_at_head(self, run: Mapping[str, Any]) -> list[dict[str, Any]]: | |
| payload = self.request("GET", f"/repos/{self.repo}/actions/workflows/{urllib.parse.quote(workflow)}/runs?{query}") | ||
| return [other for other in payload.get("workflow_runs") or [] if isinstance(other, Mapping)] | ||
|
|
||
| def branch_runs(self, run: Mapping[str, Any]) -> list[dict[str, Any]]: | ||
| """One page of the newest runs of `run`'s workflow on its branch (skip_superseded_delivery()).""" | ||
| workflow = str(run.get("path") or "").rsplit("/", 1)[-1] | ||
| query = urllib.parse.urlencode({"branch": run.get("head_branch") or "", "per_page": 20}) | ||
| payload = self.request("GET", f"/repos/{self.repo}/actions/workflows/{urllib.parse.quote(workflow)}/runs?{query}") | ||
| return [other for other in payload.get("workflow_runs") or [] if isinstance(other, Mapping)] | ||
|
|
||
| def completed_runs(self, workflow: str, since: str) -> list[dict[str, Any]]: | ||
| """One page of `workflow`'s completed runs created at or after `since`, newest first.""" | ||
| query = urllib.parse.urlencode({"status": "completed", "created": f">={since}", | ||
|
|
@@ -3008,7 +3136,9 @@ def main(argv: Sequence[str] | None = None) -> int: | |
| # Orphans read only what is already fetched; their PR branches join | ||
| # the one batched GraphQL lookup for the no-janitor label. | ||
| orphans = find_orphans(runs, jobs_by_run, min_age=orphan_age, now=now) | ||
| superseded = superseded_candidates(runs, jobs_by_run) | ||
| branches = sorted(set(branches_to_resolve(runs, jobs_by_run)) | set(orphan_branches(orphans, now)) | ||
| | {str(run["head_branch"]) for run in superseded if run.get("head_branch")} | ||
| | set(lost_branches(runs, {**lost, **unclaimed}, pending or []))) | ||
| prs_by_branch = github.pull_requests(branches) if branches else {} | ||
| except RuntimeError as error: | ||
|
|
@@ -3173,10 +3303,19 @@ def save_lost_reruns(entries: Sequence[Mapping[str, Any]]) -> None: | |
| orphan_results, orphan_failures = cancel_orphans(github, orphan_decisions) | ||
| failures += orphan_failures | ||
|
|
||
| try: | ||
| superseded_decisions, superseded_failures = skip_superseded_delivery( | ||
| github, superseded, runs, jobs_by_run, prs_by_branch, dry_run=args.dry_run) | ||
| except RuntimeError as error: # a rate limit: what was cancelled before it is in the next sweep's inventory | ||
| print(f"queue-janitor: superseded delivery runs: {error}", file=sys.stderr) | ||
| superseded_decisions, superseded_failures = [], 1 | ||
|
Comment on lines
+3381
to
+3383
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| failures += superseded_failures | ||
|
|
||
| summary = render_summary(plan, dry_run=args.dry_run, now=now, results=results) | ||
| summary += render_lost_reruns(rerun_decisions, dry_run=args.dry_run, results=rerun_results) | ||
| summary += render_orphan_summary(orphan_decisions, dry_run=args.dry_run, now=now, min_age=orphan_age, | ||
| results=orphan_results) | ||
| summary += render_superseded_summary(superseded_decisions) | ||
| summary += f"\n_{len(runs)} in-flight runs, {len(jobs_by_run)} job listings, {len(branches)} PR branches, " \ | ||
| f"{len(orphans)} orphans, {len(lost)} lost assignments, {len(unclaimed)} unclaimed legs, " \ | ||
| f"{head_reads} branch head reads, {len(evidence_jobs)} evidence job listings, " \ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: manaflow-ai/cmux
Length of output: 2315
Script executed:
Repository: manaflow-ai/cmux
Length of output: 41774
Script executed:
Repository: manaflow-ai/cmux
Length of output: 41663
Script executed:
Repository: manaflow-ai/cmux
Length of output: 42304
Filter the superseding run to delivery runs.
newer_head_runmatches only_workflow_keyand branch. A newer ordinarycmux-next.ymlpush run onfeat-cmux-nextcan therefore supersede a queued DEV daily run, even though it is not a DEV delivery run.Suggested fix
Add a regression test with a
cmux-next.ymlDEV daily run and a newer ordinarycmux-next.ymlrun.superseded_unstartedshould returnFalse.Committable suggestion