Skip to content

The agent that grows with you

fix(code-health): replay stops counting unmeasured PRs as clean

Teknium0017264c02c4
diff --git a/scripts/code_health/replay.py b/scripts/code_health/replay.py
index 4879d1a8e42..f1525ef52fa 100644
--- a/scripts/code_health/replay.py
+++ b/scripts/code_health/replay.py
@@ -7,7 +7,8 @@ The sample is every PR merged into main inside a closed date window, in PR-numbe
same window always selects the same PRs (a new comment on an old PR cannot reshuffle it). Each run
writes ``<dir>/manifest.json`` (number, base, head per PR); ``--manifest`` replays exactly those
ranges, e.g. to compare two engine versions on identical input. Writes ``<dir>/replay.jsonl`` (one
-row per PR) and prints per-rule totals plus how many PRs had at least one blocking finding. Use it
+row per PR) and prints per-rule totals plus how many measured PRs had at least one blocking finding;
+a PR whose range cannot be resolved or measured is listed and fails the run. Use it
before promoting a rule to blocking or changing a target: a rule whose hits on merged PRs are
mostly legitimate code ships as a warning until its checker is fixed.
"""
@@ -119,20 +120,30 @@ def main(argv: list[str] | None = None) -> int:
per_rule: Counter[str] = Counter()
prs_per_rule: Counter[str] = Counter()
blocked = 0
+ # A range that was never measured is no evidence either way: it stays out of the totals and
+ # the denominator, and fails the run, so a "0 of N" verdict always means N measured PRs.
+ unmeasured: list[int] = []
with (out_dir / "replay.jsonl").open("w", encoding="utf-8") as fh:
for row in manifest:
try:
- findings = replay_one(repo, measurer, row["base"], row["head"]) if row["base"] else []
+ if not row["base"]:
+ raise RuntimeError(row["head"] or "no base revision")
+ findings = replay_one(repo, measurer, row["base"], row["head"])
except RuntimeError as exc:
- findings = [{"error": str(exc)}]
+ unmeasured.append(row["number"])
+ fh.write(json.dumps({**row, "error": str(exc)}) + "\n")
+ continue
fh.write(json.dumps({**row, "findings": findings}) + "\n")
- hits = [f for f in findings if "rule" in f]
- per_rule.update(f["rule"] for f in hits)
- prs_per_rule.update({f["rule"] for f in hits})
- blocked += any(f["blocking"] for f in hits)
+ per_rule.update(f["rule"] for f in findings)
+ prs_per_rule.update({f["rule"] for f in findings})
+ blocked += any(f["blocking"] for f in findings)
for rule, count in per_rule.most_common():
print(f"{rule:<11} findings {count:>4} PRs {prs_per_rule[rule]:>4}")
- print(f"{blocked} of {len(manifest)} PRs had at least one blocking finding")
+ print(f"{blocked} of {len(manifest) - len(unmeasured)} measured PRs had at least one blocking finding")
+ if unmeasured:
+ print(f"{len(unmeasured)} of {len(manifest)} PRs could not be measured: "
+ + ", ".join(f"#{n}" for n in unmeasured) + f" (errors in {out_dir / 'replay.jsonl'})")
+ return 1
return 0
diff --git a/tests/scripts/test_code_health.py b/tests/scripts/test_code_health.py
index 6f04eb1dada..7cee072e16b 100644
--- a/tests/scripts/test_code_health.py
+++ b/tests/scripts/test_code_health.py
@@ -2,12 +2,14 @@
from __future__ import annotations
+import json
import shutil
import subprocess
from pathlib import Path
import pytest
+from scripts.code_health import replay
from scripts.code_health.cli import run
REPO = Path(__file__).resolve().parents[2]
@@ -169,3 +171,34 @@ def test_verdicts_follow_ownership_deadlines_and_import_execution(tmp_path, caps
base = _commit(repo, extra_base)
code, out = _verdict(repo, base, files, capsys)
assert code == (1 if blocks else 0), out
+
+
+# --- replay: a range it could not measure is reported and fails the run, never counted clean ---
+
+
+def test_replay_reports_unmeasured_prs_and_fails(tmp_path, monkeypatch, capsys):
+ repo, base = _repo(tmp_path)
+ head = _commit(repo, {"pkg/b.py": _SWALLOW.replace("other", "fresh")})
+
+ def pr(number: int, oid: str) -> dict:
+ return {"number": number, "title": f"pr {number}", "mergeCommit": {"oid": oid},
+ "commits": {"totalCount": 1, "nodes": [{"commit": {"messageHeadline": "elsewhere"}}]}}
+
+ # #2's merge commit is not in the clone: building the manifest cannot resolve its range
+ monkeypatch.setattr(replay, "merged_prs", lambda *_: [pr(1, head), pr(2, "1" * 40)])
+ monkeypatch.chdir(repo)
+ out_dir = tmp_path / "out"
+ assert replay.main(["--merged", "2026-09-01..2026-09-30", "--out", str(out_dir)]) == 1
+ out = capsys.readouterr().out
+ assert "1 of 1 measured PRs had at least one blocking finding" in out, out
+ assert "1 of 2 PRs could not be measured: #2" in out, out
+
+ # a frozen manifest whose range no longer exists cannot be replayed either
+ manifest = json.loads((out_dir / "manifest.json").read_text(encoding="utf-8"))
+ manifest.append({"number": 3, "title": "pr 3", "base": "0" * 40, "head": "1" * 40})
+ (tmp_path / "frozen.json").write_text(json.dumps(manifest), encoding="utf-8")
+ assert replay.main(["--manifest", str(tmp_path / "frozen.json"), "--out", str(out_dir)]) == 1
+ out = capsys.readouterr().out
+ assert "1 of 1 measured PRs had at least one blocking finding" in out, out
+ assert "2 of 3 PRs could not be measured: #2, #3" in out, out
+