Skip to content

The agent that grows with you

feat(code-health): heuristic rules warn until a frozen replay shows precision

Tekniumbaa07694a8e3
diff --git a/scripts/code_health/config.py b/scripts/code_health/config.py
index 680081b2d12..c8fb158a30b 100644
--- a/scripts/code_health/config.py
+++ b/scripts/code_health/config.py
@@ -9,8 +9,10 @@ from __future__ import annotations
from dataclasses import dataclass
from fnmatch import fnmatch
-# Master switch. Read from the BASE revision, so a PR cannot relax its own check, and honoured
-# everywhere the ratchet runs (CI, `scripts/check`, the git hooks). "blocking": findings fail;
+# Master switch, honoured everywhere the ratchet runs (CI, `scripts/check`, the git hooks). It is
+# read from main, never from the branch under test, so a PR cannot relax its own check: CI reads
+# the merge commit's first parent; local runs read `origin/main` (falling back to the merge-base).
+# A flip reaches an open PR on its next CI run (a re-run reuses the old merge commit). "blocking": findings fail;
# "advisory": findings print, the run passes; "off": the ratchet is skipped. Flip it with a
# one-line commit to main; a single rule can instead be demoted with ``blocking=False`` below.
ENFORCEMENT = "blocking"
@@ -54,6 +56,11 @@ class Rule:
pattern_id: str = "" # regex rules: id in scripts/ci/profile_scope_patterns.json
+# Heuristic rules print as warnings until a frozen replay (`python -m scripts.code_health.replay`)
+# shows most of their hits on merged PRs are real problems; flip a rule to blocking with that
+# evidence in the PR. Reviewers' samples put these between ~12% and ~60% precision.
+WARN_UNTIL_REPLAYED = False
+
RULES: tuple[Rule, ...] = (
# Swallowed exceptions: the top statically-catchable bug cause in the fix sample.
Rule("S110", "try/except/pass", "log it or narrow the except; a silent pass hides the"
@@ -96,6 +103,7 @@ RULES: tuple[Rule, ...] = (
" `display_hermes_home()` for user-facing text (`hermes_constants`); a path that"
" deliberately lives under the user's home, not the profile (e.g. the profiles root),"
" takes `# health: allow HX001 -- <why>`", "ast",
+ blocking=WARN_UNTIL_REPLAYED,
exclude=SINGLE_PROFILE + ("hermes_constants.py",)),
Rule("HX002", "new HERMES_* environment variable", "behavioural settings go in"
" config.yaml, secrets through the secret scope; `.env` is for credentials only",
@@ -109,21 +117,24 @@ RULES: tuple[Rule, ...] = (
exclude=SINGLE_PROFILE),
Rule("HX012", "raw threading.Thread", "`spawn_context_thread(...)` so the thread keeps"
" the caller's profile scope (ContextVars do not cross a bare Thread)", "ast",
+ blocking=WARN_UNTIL_REPLAYED,
exclude=SINGLE_PROFILE),
Rule("PS-P05", "child env built from os.environ", "`served_profile_child_env()`; the"
" child otherwise inherits the launch profile's home and secrets", "regex",
- exclude=SINGLE_PROFILE, pattern_id="P05"),
+ exclude=SINGLE_PROFILE, blocking=WARN_UNTIL_REPLAYED, pattern_id="P05"),
Rule("PS-P06", "raw platform credential getenv", "read it through the profile's secret"
- " scope (`get_secret`)", "regex", exclude=SINGLE_PROFILE, pattern_id="P06"),
+ " scope (`get_secret`)", "regex", exclude=SINGLE_PROFILE,
+ blocking=WARN_UNTIL_REPLAYED, pattern_id="P06"),
# Process identity.
Rule("HX003", "process identity from argv substrings", "use"
" `gateway.status.looks_like_gateway_command_line` /"
- " `hermes_cli.update_cmd._hermes_holder_subcommand` and match full cmdlines", "ast"),
+ " `hermes_cli.update_cmd._hermes_holder_subcommand` and match full cmdlines", "ast",
+ blocking=WARN_UNTIL_REPLAYED),
# Config truthiness.
Rule("HX010", "bool() of a config/env string", "`bool(\"false\")` is True. Is it set?"
" `os.getenv(\"X\") is not None` (or `!= \"\"` to treat empty as unset). A boolean"
" flag? `is_truthy_value(...)`, which is False for any non-flag string such as a key",
- "ast"),
+ "ast", blocking=WARN_UNTIL_REPLAYED),
# Structure.
Rule("HX011", "if/elif ladder on one name", "use a dict/table -> handler (`_SLASH_DISPATCH`"
" is the shape)", "ast", exclude=()),
diff --git a/tests/scripts/test_code_health_cli.py b/tests/scripts/test_code_health_cli.py
index b30aaeac599..883bab15b88 100644
--- a/tests/scripts/test_code_health_cli.py
+++ b/tests/scripts/test_code_health_cli.py
@@ -31,6 +31,8 @@ _SUPPORT = ("scripts/ci/profile_scope_patterns.json", *_GUARD_SCRIPTS)
_LEGACY = "def legacy(x):\n" + "".join(f" if x == {i}:\n return {i}\n" for i in range(21))
_GROWN = _LEGACY + " if x == 99:\n return 99\n"
_ENV_COPY = "import os\n\n\ndef child_env():\n env = os.environ.copy()\n return env\n"
+# A violation of a rule that always blocks (PS-P05 is warning-only until replayed).
+_SWALLOW = "def swallow():\n try:\n pass\n except Exception:\n pass\n"
_SWITCH = "scripts/code_health/config.py"
@@ -128,8 +130,8 @@ def _ratchet_repo(tmp_path: Path, switch: str | None = None) -> tuple[Path, str]
'"id": "P05",', '"id": "P05",\n "path_regex": "^nowhere/",')),
# the engine itself, edited to exempt pkg/ from PS-P05
("scripts/code_health/config.py", lambda t: t.replace(
- 'exclude=SINGLE_PROFILE, pattern_id="P05"',
- 'exclude=SINGLE_PROFILE + ("pkg/*",), pattern_id="P05"')),
+ 'exclude=SINGLE_PROFILE, blocking=WARN_UNTIL_REPLAYED, pattern_id="P05"',
+ 'exclude=SINGLE_PROFILE + ("pkg/*",), blocking=WARN_UNTIL_REPLAYED, pattern_id="P05"')),
])
def test_staged_health_ignores_unstaged_policy_and_engine(tmp_path, unstaged):
repo = _engine_repo(tmp_path)
@@ -137,7 +139,7 @@ def test_staged_health_ignores_unstaged_policy_and_engine(tmp_path, unstaged):
_git(repo, "add", "--", "pkg/c.py")
tree = _git(repo, "write-tree")
first = _check(repo, "--staged", "--only", "health", "--base", "HEAD")
- assert first.returncode == 1 and "PS-P05" in first.stdout, first.stdout + first.stderr
+ assert first.returncode == 0 and "PS-P05" in first.stdout, first.stdout + first.stderr
rel, edit = unstaged
original = (repo / rel).read_text(encoding="utf-8")
@@ -145,7 +147,7 @@ def test_staged_health_ignores_unstaged_policy_and_engine(tmp_path, unstaged):
assert edited != original
(repo / rel).write_text(edited, encoding="utf-8")
again = _check(repo, "--staged", "--only", "health", "--base", "HEAD")
- assert again.returncode == 1 and "PS-P05" in again.stdout, again.stdout + again.stderr
+ assert again.stdout == first.stdout, again.stdout + again.stderr
# the judged artifact and the user's unstaged work are both untouched
assert _git(repo, "write-tree") == tree
assert (repo / rel).read_text(encoding="utf-8") == edited
@@ -165,7 +167,7 @@ def test_unknown_or_empty_selector_is_a_usage_error(selector):
def test_valid_selector_runs_its_check_and_keeps_its_status(tmp_path):
repo = _engine_repo(tmp_path)
- _write(repo, {"pkg/c.py": _ENV_COPY})
+ _write(repo, {"pkg/c.py": _SWALLOW})
_git(repo, "add", "--", "pkg/c.py")
proc = _check(repo, "--staged", "--only", "health,shebangs", "--base", "HEAD")
assert proc.returncode == 1 and "2 checks, FAILED: health" in proc.stdout, proc.stdout
@@ -224,7 +226,7 @@ def _remote_ref(remote: Path, branch: str) -> str:
def test_pre_push_judges_a_checker_branch_pushed_from_an_older_checkout(tmp_path):
repo, remote = _pushable(tmp_path)
- _branch_with_checker(repo, "feature", {"pkg/c.py": _ENV_COPY})
+ _branch_with_checker(repo, "feature", {"pkg/c.py": _SWALLOW})
good = _branch_with_checker(repo, "clean", {"pkg/d.py": "def d():\n return 4\n"})
assert _check(repo, "--install-hook", "pre-push").returncode == 0
_git(repo, "checkout", "-q", "main")
@@ -232,7 +234,7 @@ def test_pre_push_judges_a_checker_branch_pushed_from_an_older_checkout(tmp_path
push = _sh(repo, "git", "push", "origin", "feature")
assert push.returncode != 0, push.stdout + push.stderr
- assert "PS-P05" in push.stdout + push.stderr
+ assert "BLE001" in push.stdout + push.stderr
assert _remote_ref(remote, "feature") == "" # destination untouched
# controls: a clean checker branch passes, a branch predating the checker is not judged,
@@ -240,7 +242,7 @@ def test_pre_push_judges_a_checker_branch_pushed_from_an_older_checkout(tmp_path
push = _sh(repo, "git", "push", "origin", "clean")
assert push.returncode == 0 and _remote_ref(remote, "clean") == good, push.stderr
_git(repo, "checkout", "-q", "-b", "old", "main")
- old = _commit(repo, {"pkg/c.py": _ENV_COPY})
+ old = _commit(repo, {"pkg/c.py": _SWALLOW})
assert _sh(repo, "git", "push", "origin", "old").returncode == 0
assert _remote_ref(remote, "old") == old
assert _sh(repo, "git", "push", "origin", "--delete", "clean").returncode == 0
@@ -359,7 +361,7 @@ def test_ci_only_guards_run_in_scripts_check(tmp_path, job, files):
_FUNC = "import os\n\n\ndef build(cmd):\n{} return cmd\n"
[email protected]("body, blocks", [
[email protected]("body, flagged", [
(" # Avoid env = os.environ.copy(); use the scoped builder.\n", False),
(' """Avoid env = os.environ.copy(); use the scoped builder."""\n', False),
(' """Builder.\n\n Never env = os.environ.copy() here.\n """\n', False),
@@ -371,13 +373,13 @@ _FUNC = "import os\n\n\ndef build(cmd):\n{} return cmd\n"
(" token = os.getenv(\"DISCORD_TOKEN\")\n cmd = (cmd, token)\n", True),
(" env = dict(os.environ) # copy it\n cmd = (cmd, env)\n", True),
])
-def test_profile_regex_rules_ignore_comments_and_docstrings(tmp_path, capsys, body, blocks):
+def test_profile_regex_rules_ignore_comments_and_docstrings(tmp_path, capsys, body, flagged):
repo, base = _ratchet_repo(tmp_path)
head = _commit(repo, {"pkg/c.py": _FUNC.format(body)})
code = cli.run(repo, base, head)
out = capsys.readouterr().out
- assert code == (1 if blocks else 0), out
- assert ("PS-P05" in out or "PS-P06" in out) == blocks, out
+ assert code == 0, out # PS-P05/P06 are warnings until replayed
+ assert ("PS-P05" in out or "PS-P06" in out) == flagged, out
# --- allow comments may list several rules ----------------------------------------------------