Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(actions): judge a fresh regression by the open finding as it reads today (#3543)
Raghav Chamadiya3a3f17cba940
diff --git a/packages/core/src/repowise/core/analysis/actions/build.py b/packages/core/src/repowise/core/analysis/actions/build.pyindex 1aa896a5b..2652373ea 100644--- a/packages/core/src/repowise/core/analysis/actions/build.py+++ b/packages/core/src/repowise/core/analysis/actions/build.py@@ -317,23 +317,21 @@ def build_recent(open_findings: Rows,files: Mapping[str, FileFacts],) -> dict[str, Any]:- """Findings a commit in the last week wrote that are still open."""+ """Findings a commit in the last week wrote that are still open and serious,+ as the open finding reads today."""if week is None:return {}candidates = _recent_candidates(rows, week=week, files=files)if not candidates:return {"recent_findings": ()}- open_keys = {- (field(h, "file_path"), field(h, "biomarker_type"), field(h, "function_name"))- for h in open_findings- if _open(h)- }+ serious = _serious_open(open_findings)recent = []seen: set[tuple[str, str, str | None]] = set()for f, at in sorted(candidates, key=lambda c: c[1], reverse=True):key = (field(f, "file_path"), field(f, "biomarker_type"), field(f, "symbol"))- # Still open, and counted once however many commits touched it.- if key not in open_keys or key in seen:+ # Still serious today, and counted once however many commits touched it.+ now = serious.get(key)+ if now is None or key in seen:continueseen.add(key)recent.append(@@ -344,15 +342,43 @@ def build_recent(file_path=field(f, "file_path"),symbol=field(f, "symbol"),biomarker=field(f, "biomarker_type"),- severity=field(f, "severity"),+ severity=field(now, "severity"),change_kind=field(f, "change_kind"),- line=field(f, "line_start"),- reason=field(f, "reason") or "",+ line=field(now, "line_start"),+ reason=field(now, "reason") or "",))return {"recent_findings": tuple(recent)}+def _serious_open(open_findings: Rows) -> dict[tuple[str, str, str | None], Any]:+ """The worst open critical/high finding per (file, marker, function).++ A commit's row records the finding as that commit left it; the open row+ is what is true today, so it decides whether the finding still counts and+ what the action says about it. Ties go to the higher impact, then the+ earlier line, then the reason, so the pick does not hang on row order.+ """++ def worse(h: Any) -> tuple:+ return (+ field(h, "severity") == "critical",+ field(h, "health_impact") or 0.0,+ -(field(h, "line_start") or 0),+ field(h, "reason") or "",+ )++ out: dict[tuple[str, str, str | None], Any] = {}+ for h in open_findings:+ if not _open(h) or field(h, "severity") not in ("critical", "high"):+ continue+ key = (field(h, "file_path"), field(h, "biomarker_type"), field(h, "function_name"))+ held = out.get(key)+ if held is None or worse(h) > worse(held):+ out[key] = h+ return out++def build_fix_first_store(rows: Mapping[str, Rows]) -> dict[str, Any]:"""The head of the Fix-first queue, from the rows its own builder reads."""return {"fix_first": build_fix_first(**rows, limit=FIX_FIRST_ACTIONS).items}diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/actions.py b/packages/core/src/repowise/core/persistence/crud/analysis/actions.pyindex e65ccdb6d..5ae13a6bb 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis/actions.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/actions.py@@ -228,6 +228,7 @@ async def _recent(select(*_HEALTH_FINDING_COLUMNS).where(HealthFinding.repository_id == repo_id,HealthFinding.status == "open",+ HealthFinding.severity.in_(("critical", "high")),HealthFinding.file_path.in_({r.file_path for r in rows}),))diff --git a/tests/unit/persistence/test_actions_build.py b/tests/unit/persistence/test_actions_build.pyindex 2a3c1e75e..0bfa5d0f1 100644--- a/tests/unit/persistence/test_actions_build.py+++ b/tests/unit/persistence/test_actions_build.py@@ -246,4 +246,54 @@ def test_only_a_recent_authored_code_shape_regression_is_a_candidate() -> None:assert _recent_candidates(rows, week=None, files=files) == []# The week's own instant is inside it.edge = {**kept, "committed_at": week.isoformat()}- assert len(_recent_candidates([edge], week=week, files=files)) == 1\ No newline at end of file+ assert len(_recent_candidates([edge], week=week, files=files)) == 1++def test_a_recent_finding_reads_as_the_open_finding_does_today() -> None:+ """The commit names who wrote it; the open finding decides whether it still+ counts and what severity, line and reason the action quotes."""+ from repowise.core.analysis.actions.build import build_recent++ week = _T - timedelta(days=7)+ wrote = {"sha": "s1", "subject": "feat: x", "committed_at": _iso(timedelta(days=2)),+ "change_kind": "introduced", "severity": "high", "attribution_basis": "added_lines",+ "biomarker_type": "primitive_obsession", "line_start": 622,+ "reason": "takes 9 parameters"}+ rows = [+ {**wrote, "file_path": "src/a.py", "symbol": "eased"},+ {**wrote, "file_path": "src/a.py", "symbol": "kept"},+ {**wrote, "file_path": "src/a.py", "symbol": "gone"},+ ]+ today = {"file_path": "src/a.py", "biomarker_type": "primitive_obsession"}+ open_findings = [+ {**today, "function_name": "eased", "severity": "low", "line_start": 620,+ "reason": "takes 5 parameters"},+ {**today, "function_name": "kept", "severity": "high", "line_start": 700,+ "reason": "takes 10 parameters"},+ {**today, "function_name": "kept", "severity": "critical", "line_start": 701,+ "reason": "takes 12 parameters"},+ ]++ (found,) = build_recent(rows, week=week, open_findings=open_findings, files={})[+ "recent_findings"+ ]+ assert (found.symbol, found.sha, found.change_kind) == ("kept", "s1", "introduced")+ assert (found.severity, found.line, found.reason) == ("critical", 701, "takes 12 parameters")+++def test_equally_serious_open_matches_pick_the_same_one_in_any_order() -> None:+ from repowise.core.analysis.actions.build import build_recent++ week = _T - timedelta(days=7)+ row = {"sha": "s1", "committed_at": _iso(timedelta(days=2)), "change_kind": "worsened",+ "severity": "high", "attribution_basis": "added_lines", "file_path": "src/a.py",+ "biomarker_type": "complex_method", "symbol": "f"}+ today = {"file_path": "src/a.py", "biomarker_type": "complex_method", "function_name": "f",+ "severity": "high", "health_impact": 1.0}+ a = {**today, "line_start": 40, "reason": "CCN 18"}+ b = {**today, "line_start": 10, "reason": "CCN 16"}++ for open_findings in ([a, b], [b, a]):+ (found,) = build_recent([row], week=week, open_findings=open_findings, files={})[+ "recent_findings"+ ]+ assert (found.line, found.reason) == (10, "CCN 16")