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
297 changes: 297 additions & 0 deletions .github/workflows/lib-tests.yml

Large diffs are not rendered by default.

272 changes: 261 additions & 11 deletions .github/workflows/smoke-tests.yml

Large diffs are not rendered by default.

143 changes: 114 additions & 29 deletions heart/checks/smoke_timings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,29 @@
render produces, so a Pages gap loses it. The board stays as the fallback,
and the ``performance.scripts.rows`` block is written either way.
* **Drift is never classified across two known but different cache states.**
The smoke workflow restores a JAX compile cache and the simulated datasets,
and writes ``cache_state.json`` beside the timings dataset in the same
artifact. A cold run against a hot baseline is a recompile and a hot run
against a cold one is the cache landing; neither is a change anybody made, so
both yield ``ok`` with no ratio. An ``unknown`` on either side — every
artifact older than the sidecar — compares exactly as it always did.
The smoke workflow restores a JAX compile cache, a numba function cache and
the simulated datasets, and writes ``cache_state.json`` beside the timings
dataset in the same artifact. A cold run against a hot baseline is a
recompile and a hot run against a cold one is the cache landing; neither is a
change anybody made, so both yield ``ok`` with no ratio. An ``unknown`` on
either side — every artifact older than the sidecar — compares exactly as it
always did.

The sidecar carries one section per cache (``jax``, ``numba``, ``datasets``),
each ``{"hit", "exact", "restored_key", "size_mb_before/after",
"entries_before/after"}``, plus the ``epoch`` salt those keys were built
with and ``setup_s`` — the seconds from job start to the first script, i.e.
this gate's FIXED overhead (checkout, dependency-chain clone, python setup,
install), measured by the workflow's two mark steps and differenced by its
recorder. It rides here rather than in the timings dataset because it is a
property of the leg, not of any script in it, and because it is the number
the cache work is judged on. ``null``/absent is ``None``, never 0. ``jax`` and ``datasets`` have been written on every run since the
sidecar existed, so a MISSING section of either is a miss; ``numba`` was
added later, so a missing ``numba`` section is ``unknown`` — an older
emitter, not a run that restored nothing. Which cache a comparison turns on
is the consumer's business: the script rows compare on ``jax`` alone
(``classify_drift``'s ``prev_cache_key``), the unit rows on jax and numba
combined (``heart.timings.unit_cache_state``).
* **Drift is advisory.** Rows are ``ok`` or ``warn``, never ``fail``; only
TIMEOUT entries are hard rows. The readiness verdict is untouched — this is
a dashboard leg, not a gate.
Expand All @@ -80,7 +97,8 @@
"head_branch": "feat/x", "head_sha": "abc...",
"at": "2026-09-01T10:00:00Z", # the artifact's created_at
"env_profile": "smoke", "error": "",
"cache": {"jax": "hit", "datasets": "miss", "epoch": "1"},
"cache": {"jax": "hit", "datasets": "miss", "numba": "hit",
"epoch": "1"},
"entries": [{"entry": "imaging/x.py", "kind": "script",
"status": "passed", "seconds": 12.5, "cap_s": 600.0,
"exit_code": 0}],
Expand Down Expand Up @@ -134,10 +152,13 @@
TIMINGS_FILENAME = "smoke_timings.json"
TIMINGS_SCHEMA = "smoke_timings/1"

# The cache-state sidecar the smoke workflow writes beside the timings dataset
# and uploads in the same artifact: whether this leg ran with a restored JAX
# compile cache and a restored dataset tree, or cold. A run that recompiled
# everything is not a slower run, it is a differently-conditioned one.
# The cache-state sidecar BOTH reusable workflows write beside their timings
# dataset and upload in the same artifact: whether this leg ran with a restored
# JAX compile cache, a restored numba function cache and (in the smoke gate) a
# restored dataset tree, or cold. A run that recompiled everything is not a
# slower run, it is a differently-conditioned one. The schema string is
# unchanged by the numba section: it is purely additive, and an older sidecar
# without it stays readable.
CACHE_STATE_FILENAME = "cache_state.json"
CACHE_STATE_SCHEMA = "cache_state/1"

Expand Down Expand Up @@ -321,15 +342,45 @@ def parse_timings(text: str) -> tuple[dict[str, Any] | None, str]:
)


def _setup_seconds(value: Any) -> float | None:
"""A sidecar's ``setup_s`` as a float, or ``None`` for every other reading.

Written by another organ on a runner we do not control, so the same
defensive shape as everything else here: a bool, a string, a NaN, a
negative difference or an absent field are all "we do not know how long
this leg spent getting ready", which is exactly what ``None`` says.
"""
if isinstance(value, bool) or not isinstance(value, (int, float)):
return None
seconds = float(value)
if seconds != seconds or seconds in (float("inf"), float("-inf")):
return None
return seconds if seconds >= 0 else None


def parse_cache_state(text: str) -> tuple[dict[str, Any] | None, str]:
"""One ``cache_state.json`` → (state, "") or (None, reason).

The same defensive shape as ``parse_timings``: the file is written by
another organ on a runner we do not control, so a surprising value degrades
to a miss rather than raising out of an unattended check. A section is a
``hit`` exactly when it says so — an absent or malformed section is a miss,
because the workflow writes both sections on every run and their absence is
not evidence that anything was restored.
``hit`` exactly when it says so — an absent or malformed ``jax`` or
``datasets`` section is a miss, because the workflows have written both on
every run since the sidecar existed and their absence is not evidence that
anything was restored.

``numba`` is the exception, and deliberately so: it was added to the
schema later, so a sidecar without it is an OLDER EMITTER rather than a run
that restored nothing, and the honest reading of its absence is
``unknown``. Reading it as a miss would date-stamp every pre-numba artifact
as cold and quietly suppress the drift comparisons those legs can still
make.

``setup_s`` follows the same additive rule and the same honesty: a float
when the emitter measured the gate's fixed overhead, ``None`` when it did
not (an older emitter, a mark step that could not write) or when the value
is not a finite non-negative number. Never 0 — a gate with no fixed cost is
not a thing that happens, so a fabricated zero would be a claim.
"""
try:
data = json.loads(text)
Expand All @@ -348,27 +399,49 @@ def _section(key: str) -> tuple[str, str]:

jax_state, jax_key = _section("jax")
ds_state, ds_key = _section("datasets")
numba_block = data.get("numba")
if isinstance(numba_block, dict):
numba_state = "hit" if numba_block.get("hit") else "miss"
numba_key = str(numba_block.get("restored_key") or "")
else:
# Additive section, absent from every sidecar written before it: not a
# miss, which would be a claim about a cache the emitter never had.
numba_state, numba_key = UNKNOWN_CACHE_STATE, ""
return (
{
"jax": jax_state,
"datasets": ds_state,
"numba": numba_state,
"setup_s": _setup_seconds(data.get("setup_s")),
# The manual salt in force. A bump means every key changed at once,
# so a miss on both sides is expected rather than a finding.
"epoch": str(data.get("epoch") or ""),
"jax_restored_key": jax_key,
"datasets_restored_key": ds_key,
"numba_restored_key": numba_key,
},
"",
)


def cache_view(cache: Any) -> dict[str, str]:
"""A cache state normalised to the three keys every consumer carries.
def cache_view(cache: Any) -> dict[str, Any]:
"""A cache state normalised to the five keys every consumer carries.

Anything that is not one of the two known states reads ``unknown``: a leg
from before the sidecar existed, a truncated file, a future schema. The
epoch rides along because a comparison across an epoch bump is comparing
two different worlds, and the reader needs to be able to see that.
from before the sidecar existed, a truncated file, a future schema, a
``numba`` section the emitter did not write. The epoch rides along because
a comparison across an epoch bump is comparing two different worlds, and
the reader needs to be able to see that.

``datasets`` is carried for every leg including the libraries', whose
workflow has no dataset cache at all: it is the sidecar's own reading, and
the unit consumers simply do not read that key (see
``heart.timings.unit_cache_state``).

``setup_s`` rides the same block for the same reason the states do — it is
a property of the leg, taken at the same moment, by the same emitter — and
is ``None`` wherever it is absent or unreadable. It is the only non-string
value here, so a consumer that renders this block reads it by name.
"""
src = cache if isinstance(cache, dict) else {}

Expand All @@ -377,10 +450,11 @@ def _state(key: str) -> str:
return value if value in CACHE_STATES else UNKNOWN_CACHE_STATE

return {"jax": _state("jax"), "datasets": _state("datasets"),
"epoch": str(src.get("epoch") or "")}
"numba": _state("numba"), "epoch": str(src.get("epoch") or ""),
"setup_s": _setup_seconds(src.get("setup_s"))}


def read_cache_state(directory: Path | str) -> dict[str, str]:
def read_cache_state(directory: Path | str) -> dict[str, Any]:
"""The first parseable ``cache_state.json`` under one extracted artifact.

Sorted path order, first file that parses wins — the same rule the timings
Expand Down Expand Up @@ -607,6 +681,7 @@ def classify_drift(
thresholds: dict[str, float],
run_id: Any = None,
cache: Any = None,
prev_cache_key: str = "cache_jax",
) -> tuple[str, float | None, float | None]:
"""(state, ratio, delta_s) — ``warn`` needs BOTH gates, else ``ok``.

Expand All @@ -616,13 +691,23 @@ def classify_drift(
comparing a run against itself would report a reassuring 1.0× that means
nothing. Never ``fail`` — a slowed script is advisory by construction.

``cache`` is THIS run's JAX cache state (``"hit"``/``"miss"``/``"unknown"``)
and is compared against the previous row's ``cache_jax``. Two KNOWN and
DIFFERENT states are not a comparison: a cold run against a hot baseline is
a recompile, and a hot run against a cold one is the cache landing. Neither
is a change anybody made, and reporting either as drift would cry wolf on
exactly the runs where the cache is doing its job. An unknown on either
side compares as it always did — every artifact older than the sidecar.
``cache`` is THIS run's cache state (``"hit"``/``"miss"``/``"unknown"``)
and is compared against the previous row's, read under ``prev_cache_key``.
Two KNOWN and DIFFERENT states are not a comparison: a cold run against a
hot baseline is a recompile, and a hot run against a cold one is the cache
landing. Neither is a change anybody made, and reporting either as drift
would cry wolf on exactly the runs where the cache is doing its job. An
unknown on either side compares as it always did — every artifact older
than the sidecar.

``prev_cache_key`` exists because the two callers turn on different
states and must say so in the row rather than in a comment. A script row's
baseline records the JAX cache alone under ``cache_jax`` (the default,
unchanged); a unit row's records jax and numba COMBINED under
``cache_state``, and calling that field ``cache_jax`` would name it after
half of what it means. One parameter is the smaller honest change: the
alternative — writing the combined state into a field called ``cache_jax``
— would make every reader of the record wrong.
"""
now = _as_float(seconds)
if not isinstance(prev_row, dict):
Expand All @@ -634,7 +719,7 @@ def classify_drift(
if run_id is not None and prev_run is not None and prev_run == run_id:
return "ok", None, None
now_cache = str(cache or "")
prev_cache = str(prev_row.get("cache_jax") or "")
prev_cache = str(prev_row.get(prev_cache_key) or "")
if (now_cache in CACHE_STATES and prev_cache in CACHE_STATES
and now_cache != prev_cache):
return "ok", None, None
Expand Down
51 changes: 43 additions & 8 deletions heart/checks/unit_timings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
literally the same function (``classify_test_drift`` IS
``smoke_timings.classify_drift``). Unit tests run 0.1–30 s, so the absolute
floor in ``config/repos.yaml`` is 2 s rather than smoke's 5 s.
* **Nor across two known but different cache states.** ``lib-tests.yml``
restores a JAX compile cache and a numba function cache and writes the same
``cache_state.json`` sidecar the smoke gate writes, in the same artifact. A
leg is ``hit`` only when it had BOTH and ``miss`` only when it had NEITHER
(``heart.timings.unit_cache_state``); every mixture is ``unknown`` and
compares as it always did. The combined state rides the record — and
therefore the previous row — under ``cache_state``, which is why
``classify_drift`` is called with ``prev_cache_key="cache_state"`` rather
than its scripts-side default of ``cache_jax``.
* **A run is never compared against itself.** Every row carries its ``run_id``;
a previous row from the SAME run yields ``ok`` with no ratio rather than a
reassuring 1.0×.
Expand Down Expand Up @@ -78,6 +87,8 @@
"head_branch": "feat/x", "head_sha": "abc...",
"at": "2026-09-01T10:00:00Z", # the artifact's created_at
"error": "",
"cache": {"jax": "hit", "datasets": "miss", "numba": "hit",
"epoch": "1"},
"suite": {"tests": 1500, "failures": 0, "errors": 0, "skipped": 3,
"wall_s": 412.0},
"slowest": [{"nodeid": "tests/foo/test_bar.py::test_x",
Expand All @@ -90,16 +101,18 @@

{"ts",
"repos": [{repo, python, run_id, run_url, head_branch, head_sha, at,
tests, wall_s, import_s, package, error, suite, slowest}],
tests, wall_s, import_s, package, error, cache, suite,
slowest}],
"tests": [{repo, python, nodeid, seconds, run_id, run_url, prev_s,
prev_run_id, ratio, delta_s, state, prompt}],
"imports": [{repo, package, python, seconds, run_id, run_url, baseline_s,
samples, ratio, state, prompt}],
"slowed_tests": [...], "slowed_imports": [...],
"errors": [{repo, error}], "thresholds": {...}}

``repos[]`` carries ``suite`` and ``slowest`` whole so ``heart.timings`` can
build the committed record from the rollup alone, without re-reading a sidecar.
``repos[]`` carries ``suite``, ``slowest`` and ``cache`` whole so
``heart.timings`` can build the committed record from the rollup alone, without
re-reading a sidecar.

Every actionable row — a slowed test, a slowed import — carries its own
ready-to-paste prompt string. The producer writes the prompt; the renderer
Expand All @@ -121,8 +134,13 @@
# Reused, never duplicated: the both-gates drift rule and the artifact selector
# are one implementation with two callers.
from heart.checks.import_time import classify as _classify_ratio
from heart.checks.smoke_timings import cache_view, read_cache_state
from heart.checks.smoke_timings import classify_drift as classify_test_drift
from heart.checks.smoke_timings import select_artifacts as _select_artifacts
# `heart.timings` imports nothing from `heart`, so this direction is the one
# with no cycle: the record owns the rule for combining the two cache states
# because the record is what writes it into every previous row.
from heart.timings import unit_cache_state

HEART_HOME = Path(__file__).resolve().parents[2]
CONFIG_PATH = HEART_HOME / "config" / "repos.yaml"
Expand Down Expand Up @@ -333,15 +351,22 @@ def parse_import_time(text: str) -> tuple[dict[str, Any] | None, str]:

def read_downloaded_leg(
directory: Path | str,
) -> tuple[list[dict[str, Any]], dict[str, Any], dict[str, Any] | None, str, dict[str, str]]:
) -> tuple[list[dict[str, Any]], dict[str, Any], dict[str, Any] | None, str, dict[str, Any]]:
"""One extracted artifact → ``(tests, suite, import_row, error, meta)``.

The two halves are read independently ON PURPOSE. An install that died
before pytest ran leaves the import file and no junit; an import that
crashed leaves the junit and a null-seconds import row. Either half alone is
still data, and losing it because its sibling is missing would be a hole in
the record for no reason. Only when BOTH are absent is the artifact an
honest ``no unit-timings dataset``.
honest ``no unit-timings dataset`` — and that verdict is taken BEFORE the
cache sidecar is looked at, because a sidecar without a dataset beside it
is cache state for a measurement that does not exist.

``meta["cache"]`` is the leg's cache state, read from the same
``cache_state.json`` sidecar the smoke gate writes and by the same reader —
``unknown`` throughout when no sidecar rode along, which is every artifact
older than it.
"""
root = Path(directory)
tests: list[dict[str, Any]] = []
Expand Down Expand Up @@ -373,7 +398,8 @@ def read_downloaded_leg(

if not junit_paths and not import_paths:
return [], {}, None, NO_DATASET_ERROR, {}
meta = {"python": str((import_row or {}).get("python") or "")}
meta = {"python": str((import_row or {}).get("python") or ""),
"cache": read_cache_state(root)}
return tests, suite, import_row, "; ".join(errors), meta


Expand Down Expand Up @@ -454,7 +480,7 @@ def build_sidecar(
tests: list[dict[str, Any]] = []
suite: dict[str, Any] = {}
import_row: dict[str, Any] | None = None
meta: dict[str, str] = {}
meta: dict[str, Any] = {}
if not leg_error:
directory = leg.get("dir")
if directory:
Expand All @@ -472,6 +498,9 @@ def build_sidecar(
"head_sha": str(art.get("head_sha") or ""),
"at": str(art.get("created_at") or ""),
"error": leg_error,
# A leg whose download failed has no sidecar either: "unknown"
# on every side, never a fabricated miss.
"cache": meta.get("cache") or cache_view(None),
"suite": suite,
"slowest": slowest[: max(int(top_n or 0), 0)],
"import": (
Expand Down Expand Up @@ -573,6 +602,10 @@ def _name_of(side: Any) -> str:
run_url = str(leg.get("run_url") or "")
at = str(leg.get("at") or "")
leg_error = str(leg.get("error") or "")
leg_cache = cache_view(leg.get("cache"))
# jax AND numba, combined: a suite is comparable to its baseline
# only when both ran the same way (see `unit_cache_state`).
leg_cache_state = unit_cache_state(leg_cache)
if leg_error:
# Provenance survives: which run we could not read, and why.
errors.append({"repo": repo, "error": f"{python or '?'}: {leg_error}"})
Expand All @@ -588,7 +621,8 @@ def _name_of(side: Any) -> str:
continue
prev_row = prev_rows.get((repo, python, nodeid))
state, ratio, delta = classify_test_drift(
seconds, prev_row, thr, run_id=run_id
seconds, prev_row, thr, run_id=run_id,
cache=leg_cache_state, prev_cache_key="cache_state",
)
prev_s = _as_float((prev_row or {}).get("seconds"))
prev_run_id = (prev_row or {}).get("run_id")
Expand Down Expand Up @@ -648,6 +682,7 @@ def _name_of(side: Any) -> str:
"error": leg_error,
# Carried whole so `heart.timings` can build the committed
# record from the rollup alone, without re-reading a sidecar.
"cache": leg_cache,
"suite": dict(suite),
"slowest": [{"nodeid": str(row.get("nodeid") or ""),
"seconds": _as_float(row.get("seconds"))}
Expand Down
Loading
Loading