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
20 changes: 13 additions & 7 deletions heart/checks/unit_timings.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,12 @@

# The legacy "Unit-test timing" section says "(>3× baseline)" / "(>1.5×)" in its
# own summary wording, which it inherited from the dev-box check's rolling
# median. These two constants exist ONLY to keep that wording truthful when the
# section is fed from CI rows; the rows' own advisory `warn` state is the
# both-gates rule above, and nothing here changes it.
# median. These two constants only split rows that ALREADY failed the
# both-gates rule above (their own `warn` state) into the section's red and
# yellow tiers; a row the policy calls `ok` is green, whatever its ratio. Before
# 2026-11-15 the tiers read `ratio` alone, so a 28 ms change on a 15 ms test was
# a "3× regression" and turned monitoring RED while the row itself said ok
# (PyAutoMind draft/bug/pyautoheart/unit_timing_monitoring_ignores_min_delta.md).
LEGACY_RED_FACTOR = 3.0
LEGACY_YELLOW_FACTOR = 1.5

Expand Down Expand Up @@ -784,12 +787,15 @@ def legacy_summaries(
"run_speed_factor": row.get("run_speed_factor", 1.0),
"normalized_seconds": row.get("normalized_seconds", row.get("seconds")),
}
if ratio > LEGACY_RED_FACTOR:
# One policy: the row's own both-gates state (`slow_factor` AND
# `min_delta_s`, config/repos.yaml) decides whether this is drift at
# all; the legacy factors only tier what it already called `warn`.
if row.get("state") != "warn":
unit["green"].append(entry)
elif ratio > LEGACY_RED_FACTOR:
unit["red"].append(entry)
elif ratio > LEGACY_YELLOW_FACTOR:
unit["yellow"].append(entry)
else:
unit["green"].append(entry)
unit["yellow"].append(entry)

evidence_times = [str(r.get("at")) for r in repos if r.get("at")]
evidence_ts = min(evidence_times) if evidence_times else None
Expand Down
34 changes: 34 additions & 0 deletions tests/test_unit_timings.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,40 @@ def test_the_legacy_unit_buckets_keep_the_sections_wording_truthful(tmp_path):
assert unit["yellow_count"] == 1 and unit["red_count"] == 0


def test_a_large_ratio_under_the_absolute_floor_is_green_not_a_regression(tmp_path):
"""2026-11-15: PyAutoNerves test_config 0.015 s -> 0.043 s (3.01x) turned
monitoring RED while its own row said ok; locally it measured 5 ms. The
policy's `min_delta_s` floor decides, the ratio alone never does."""
side = _sidecar(tmp_path, junit=_junit(_case(time="0.050")))
roll = ut.aggregate([side], "T", THRESHOLDS, record_prev_rows=_prev_rows(0.015))
(row,) = roll["tests"]
assert row["state"] == "ok" and row["ratio"] > ut.LEGACY_RED_FACTOR
unit, _ = ut.legacy_summaries(roll, THRESHOLDS)
assert unit["red_count"] == 0 and unit["yellow_count"] == 0
assert unit["green_count"] == 1


def test_a_slowdown_under_the_slow_factor_is_green_even_past_the_floor(tmp_path):
"""1.55x and +5.6 s (a JAX step0 test normalised on a fast runner) is no
drift: under `slow_factor`, whatever the absolute delta."""
side = _sidecar(tmp_path, junit=_junit(_case(time="15.8")))
roll = ut.aggregate([side], "T", THRESHOLDS, record_prev_rows=_prev_rows(10.2))
unit, _ = ut.legacy_summaries(roll, THRESHOLDS)
assert unit["red_count"] == 0 and unit["yellow_count"] == 0


def test_only_rows_that_pass_both_gates_reach_the_red_and_yellow_tiers(tmp_path):
side = _sidecar(tmp_path, junit=_junit(_case(time="2.5")))
roll = ut.aggregate([side], "T", THRESHOLDS, record_prev_rows=_prev_rows(1.0))
unit, _ = ut.legacy_summaries(roll, THRESHOLDS)
# 2.5x but only +1.5 s: under the 2 s floor, so not a regression.
assert unit["yellow_count"] == 0 and unit["green_count"] == 1
side = _sidecar(tmp_path, sub="red", junit=_junit(_case(time="9.0")))
roll = ut.aggregate([side], "T", THRESHOLDS, record_prev_rows=_prev_rows(2.0))
unit, _ = ut.legacy_summaries(roll, THRESHOLDS)
assert unit["red_count"] == 1


def test_an_unavailable_repo_is_listed_never_counted_as_measured(tmp_path):
dead = ut.build_sidecar("RepoB", "libraries", OWNER, [], "T", error="boom")
roll = ut.aggregate([_sidecar(tmp_path), dead], "T", THRESHOLDS)
Expand Down
Loading