Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions heart/checks/ci_timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@
look faster the more it breaks. The conclusion *mix* is recorded beside the
median so the coverage behind the number stays visible.
* **``cancelled`` is disambiguated, not counted.** The concurrency block
cancels superseded PR runs by design — benign. A ``cancelled`` run on ``main``,
or one with no newer run of the same workflow on the same branch, is a
kill/hang suspect and becomes an *event*. ``timed_out`` is always an event.
cancels a superseded run by design — benign, on ``main`` pushes as much as on
PRs. The signature is a newer run of the same workflow on the same branch
created no later than the cancelled run ended (``updated_at``, plus
``SUPERSEDE_SLACK_S``): the arrival of that run is what cancelled it. A
``cancelled`` run with no such successor is a kill/hang suspect and becomes
an *event* (``pull_request`` runs also keep the older, looser "any newer run
on the PR branch" test). ``timed_out`` is always an event.
* **Drift needs BOTH gates.** A gate is flagged ``warn`` only when today's
median is ≥ ``yellow_factor`` × its recent history *and* ≥ ``min_delta_s``
above it in absolute terms — the profiling conductor's doctrine. The ratio
Expand Down Expand Up @@ -104,6 +108,15 @@
# as `superseded`, so the mix always adds up to what the API actually said.
TRACKED_CONCLUSIONS = ("success", "failure", "cancelled", "timed_out")

# How long after a cancelled run's recorded end (``updated_at``) its successor
# may still be created and count as the run that superseded it. A concurrency
# cancel is caused BY the successor's arrival, so the successor's created_at
# normally precedes the cancelled run's updated_at (observed: 1-2 s before it,
# e.g. a library Tests main-push burst, 2026-10-17 20:01). The slack only absorbs second-level
# stamp granularity and API write ordering; it is far shorter than any push
# cadence, so a real kill followed by an unrelated later push stays a suspect.
SUPERSEDE_SLACK_S = 60

# Fallback when config/repos.yaml is unreadable; kept in sync with the YAML.
DEFAULT_CI_TIMING_THRESHOLDS = {
"yellow_factor": 1.5,
Expand Down Expand Up @@ -236,26 +249,37 @@ def queue_s(run: dict[str, Any]) -> float | None:
def classify_cancelled(run: dict[str, Any], siblings: list[dict[str, Any]]) -> str:
"""``"superseded"`` (benign) or ``"suspect"`` (a kill/hang candidate).

The concurrency block cancels superseded PR runs by design, so a cancelled
``pull_request`` run with a NEWER run of the same workflow on the same
``head_branch`` is benign and is only counted. A cancelled run on ``main``
— or one with no successor — is nobody's design: it is a kill-timer or hang
suspect and becomes an event with its run URL.
A workflow's concurrency group cancels the in-flight run when a newer run
of the same workflow on the same ``head_branch`` arrives — on ``main``
pushes as well as on PRs. So a cancelled run of ANY event is benign when
such a sibling was created after it and no later than its own end
(``updated_at`` + ``SUPERSEDE_SLACK_S``): that sibling is what cancelled
it. A cancelled ``pull_request`` run (not on ``main``) additionally stays
benign under the original rule — any newer run on the PR branch — so PR
handling is no looser and no stricter than before. Anything else — no
successor at all, or one created long after the run had already ended —
is nobody's design: a kill-timer or hang suspect, and an event.
"""
if str(run.get("head_branch") or "") == "main":
return "suspect"
if str(run.get("event") or "") != "pull_request":
return "suspect"
branch = run.get("head_branch")
workflow = run.get("workflow")
mine = run.get("created_at") or run.get("run_started_at") or ""
mine_dt = _parse_iso(mine)
ended = _parse_iso(run.get("updated_at"))
deadline = (ended + datetime.timedelta(seconds=SUPERSEDE_SLACK_S)
if ended is not None else None)
legacy_pr = (str(run.get("event") or "") == "pull_request"
and str(branch or "") != "main")
for other in siblings:
if other is run:
continue
if other.get("workflow") != workflow or other.get("head_branch") != branch:
continue
theirs = other.get("created_at") or other.get("run_started_at") or ""
if theirs > mine:
if legacy_pr and theirs > mine:
return "superseded"
theirs_dt = _parse_iso(theirs)
if (mine_dt is not None and theirs_dt is not None and deadline is not None
and mine_dt < theirs_dt <= deadline):
return "superseded"
return "suspect"

Expand Down Expand Up @@ -296,6 +320,10 @@ def time_workflow(
r for r in runs
if r.get("workflow") == workflow and r.get("conclusion") and r.get("run_started_at")
]
# The run that cancelled a superseded one is often still in flight when
# we look, so supersession is judged against EVERY run of the workflow,
# not only the completed ones being timed.
siblings = [r for r in runs if r.get("workflow") == workflow]
durations: list[float] = []
pr_durations: list[float] = []
queues: list[float] = []
Expand Down Expand Up @@ -327,7 +355,7 @@ def time_workflow(
if concl == "timed_out":
kind = "timed_out"
elif concl == "cancelled":
if classify_cancelled(run, mine) == "superseded":
if classify_cancelled(run, siblings) == "superseded":
superseded += 1
else:
kind = "suspect_cancelled"
Expand Down
90 changes: 89 additions & 1 deletion tests/test_ci_timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from __future__ import annotations

import datetime
import json

from heart.checks import ci_timing as ct
Expand Down Expand Up @@ -139,7 +140,9 @@ def test_cancelled_pr_run_with_newer_same_branch_run_is_superseded(tmp_path):
assert side["workflows"]["Gate One"]["conclusions"]["cancelled"] == 1


def test_cancelled_on_main_is_always_suspect(tmp_path):
def test_cancelled_main_run_whose_successor_came_long_after_it_ended_is_suspect(tmp_path):
"""A newer run created ten minutes AFTER the cancelled one had already
ended cannot be what cancelled it: a real kill, still an event."""
payload = {"workflow_runs": [
_run("Gate One", "cancelled", branch="main",
created="2026-08-20T09:00:00Z", updated="2026-08-20T09:50:00Z"),
Expand All @@ -154,6 +157,91 @@ def test_cancelled_on_main_is_always_suspect(tmp_path):
assert side["workflows"]["Gate One"]["superseded"] == 0


def test_superseded_main_push_burst_is_benign(tmp_path):
"""Modelled on a library Tests burst, 2026-10-17 20:01: four main pushes in
seventeen seconds; the concurrency group cancelled the middle two the
moment the next push's run was created (1 s before each cancelled run's
recorded end). Benign supersession, not kill-timer events."""
payload = {"workflow_runs": [
_run("Gate One", "success", branch="main",
created="2026-10-17T20:01:19Z", started="2026-10-17T20:01:19Z",
updated="2026-10-17T20:16:22Z", url="https://github.invalid/run/4"),
_run("Gate One", "cancelled", branch="main",
created="2026-10-17T20:01:13Z", started="2026-10-17T20:01:13Z",
updated="2026-10-17T20:01:20Z", url="https://github.invalid/run/3"),
_run("Gate One", "cancelled", branch="main",
created="2026-10-17T20:01:07Z", started="2026-10-17T20:01:07Z",
updated="2026-10-17T20:01:14Z", url="https://github.invalid/run/2"),
_run("Gate One", "success", branch="main",
created="2026-10-17T20:01:02Z", started="2026-10-17T20:01:02Z",
updated="2026-10-17T20:08:40Z", url="https://github.invalid/run/1"),
]}
side = ct.build_sidecar(REPO, "libraries", "OwnerX", payload, "T",
config_path=_cfg(tmp_path))
assert side["events"] == []
assert side["workflows"]["Gate One"]["superseded"] == 2
assert side["workflows"]["Gate One"]["conclusions"]["cancelled"] == 2


def test_superseded_main_run_counts_an_in_flight_successor(tmp_path):
"""The run that cancelled a main push is often still running when Heart
looks; it has no conclusion yet but still proves the supersession."""
payload = {"workflow_runs": [
_run("Gate One", "", status="in_progress", branch="main",
created="2026-10-17T20:01:19Z", started="2026-10-17T20:01:19Z",
updated="2026-10-17T20:05:00Z"),
_run("Gate One", "cancelled", branch="main",
created="2026-10-17T20:01:13Z", started="2026-10-17T20:01:13Z",
updated="2026-10-17T20:01:20Z"),
]}
side = ct.build_sidecar(REPO, "libraries", "OwnerX", payload, "T",
config_path=_cfg(tmp_path))
assert side["events"] == []
assert side["workflows"]["Gate One"]["superseded"] == 1


def test_successor_within_the_slack_after_end_is_superseded():
"""Stamp granularity/ordering slack: a successor created just after the
recorded end still counts; one minutes later does not."""
cancelled = ct.normalize_runs([_run(
"Gate One", "cancelled", branch="main",
created="2026-10-17T20:00:00Z", updated="2026-10-17T20:01:00Z")])[0]
def successor(created):
return ct.normalize_runs([_run("Gate One", "success", branch="main",
created=created)])[0]
end = datetime.datetime(2026, 10, 17, 20, 1, tzinfo=datetime.timezone.utc)
inside = (end + datetime.timedelta(seconds=ct.SUPERSEDE_SLACK_S - 1)
).isoformat().replace("+00:00", "Z")
assert ct.classify_cancelled(cancelled, [cancelled, successor(inside)]) == "superseded"
assert ct.classify_cancelled(
cancelled, [cancelled, successor("2026-10-17T20:05:00Z")]) == "suspect"


def test_lone_cancelled_main_run_is_suspect(tmp_path):
"""No successor at all on main: a real kill or hang, an event."""
payload = {"workflow_runs": [
_run("Gate One", "cancelled", branch="main",
created="2026-10-17T20:00:00Z", started="2026-10-17T20:00:00Z",
updated="2026-10-17T20:45:00Z", url="https://github.invalid/run/5"),
_run("Gate One", "success", branch="main",
created="2026-10-17T19:00:00Z", started="2026-10-17T19:00:00Z",
updated="2026-10-17T19:10:00Z"),
# a newer run on ANOTHER branch, or of ANOTHER workflow, proves nothing
_run("Gate One", "success", branch="feat/z",
created="2026-10-17T20:10:00Z", started="2026-10-17T20:10:00Z",
updated="2026-10-17T20:20:00Z"),
_run("Gate Two", "success", branch="main",
created="2026-10-17T20:10:00Z", started="2026-10-17T20:10:00Z",
updated="2026-10-17T20:20:00Z"),
]}
side = ct.build_sidecar(REPO, "libraries", "OwnerX", payload, "T",
config_path=_cfg(tmp_path))
(event,) = side["events"]
assert event["kind"] == "suspect_cancelled"
assert event["run_url"] == "https://github.invalid/run/5"
assert side["workflows"]["Gate One"]["superseded"] == 0


def test_cancelled_pr_run_with_no_successor_is_suspect(tmp_path):
payload = {"workflow_runs": [
_run("Gate One", "cancelled", event="pull_request", branch="feat/y",
Expand Down
Loading