From 4014b0f4c8838fa59bdcba412109bbf76d149fc8 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Wed, 21 Oct 2026 11:06:27 +0100 Subject: [PATCH 1/2] fix(ci_timing): treat concurrency-superseded cancels on main as benign classify_cancelled flagged every cancelled main run as a kill-timer suspect, so main pushes cancelled by the workflow's concurrency group (e.g. PyAutoFit Tests 2026-10-17 20:01) held the board red. A cancelled run of any event is now superseded when a newer run of the same workflow on the same head_branch was created no later than its end (updated_at + SUPERSEDE_SLACK_S = 60 s). PR runs keep the original "any newer run on the PR branch" rule. Supersession is judged against all runs of the workflow, including an in-flight successor. Refs PyAutoLabs/PyAutoHeart#311 Co-Authored-By: Claude Opus 5.5 --- heart/checks/ci_timing.py | 56 ++++++++++++++++++------ tests/test_ci_timing.py | 90 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 131 insertions(+), 15 deletions(-) diff --git a/heart/checks/ci_timing.py b/heart/checks/ci_timing.py index 79a5869c..677eb8c2 100644 --- a/heart/checks/ci_timing.py +++ b/heart/checks/ci_timing.py @@ -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 @@ -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. PyAutoFit Tests 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, @@ -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" @@ -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] = [] @@ -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" diff --git a/tests/test_ci_timing.py b/tests/test_ci_timing.py index e3f1ff90..2c497629 100644 --- a/tests/test_ci_timing.py +++ b/tests/test_ci_timing.py @@ -6,6 +6,7 @@ from __future__ import annotations +import datetime import json from heart.checks import ci_timing as ct @@ -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"), @@ -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 PyAutoFit Tests, 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", From 089f8fa1eeedc91ff3dd1145b86ee7e1e4e21f4d Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Wed, 21 Oct 2026 11:15:47 +0100 Subject: [PATCH 2/2] fix(ci_timing): keep instance names out of organ code (tenant firewall) Co-Authored-By: Claude Opus 5.5 --- heart/checks/ci_timing.py | 2 +- tests/test_ci_timing.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/heart/checks/ci_timing.py b/heart/checks/ci_timing.py index 677eb8c2..1aef9a1a 100644 --- a/heart/checks/ci_timing.py +++ b/heart/checks/ci_timing.py @@ -112,7 +112,7 @@ # 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. PyAutoFit Tests 2026-10-17 20:01). The slack only absorbs second-level +# 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 diff --git a/tests/test_ci_timing.py b/tests/test_ci_timing.py index 2c497629..943c39f2 100644 --- a/tests/test_ci_timing.py +++ b/tests/test_ci_timing.py @@ -158,7 +158,7 @@ def test_cancelled_main_run_whose_successor_came_long_after_it_ended_is_suspect( def test_superseded_main_push_burst_is_benign(tmp_path): - """Modelled on PyAutoFit Tests, 2026-10-17 20:01: four main pushes in + """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."""