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(health): keep coverage markers off empty files and code that does not ship (#4765)
Raghav Chamadiyafa8379dd53e1
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/base.py b/packages/core/src/repowise/core/analysis/health/biomarkers/base.pyindex a220f9f08..91b00ae80 100644--- a/packages/core/src/repowise/core/analysis/health/biomarkers/base.py+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/base.py@@ -31,6 +31,8 @@ class FileContext:# ``None`` for a context built without it, which falls back to the path# rules; read it through :attr:`is_test_material`.is_test: bool | None = None+ # ``code_origin.code_origin`` for the file; ``None`` when not computed.+ code_origin: str | None = None# Every walked function in this file, in document order. Not keyed by# name: a name key drops all but one of a file's anonymous ``it``# callbacks, and every same-named method on a second class beside it.@@ -153,6 +155,12 @@ class FileContext:return self.is_testreturn is_test_related_path(self.file_path, self.language)+ @property+ def outside_coverage_scope(self) -> bool:+ """Test material, or code that does not ship (tooling, build, vendored,+ generated, docs examples): no test is owed for its coverage."""+ return self.is_test_material or self.code_origin not in (None, "production")+# A repo whose trailing-90-day window has at most this many active human# contributors is a "small team": concentrated ownership there is the normaldiff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.py b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.pyindex d0e7a6167..2dad3cd23 100644--- a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.py+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.py@@ -5,8 +5,8 @@ Fires when a file has coverage data and:- line_coverage_pct < 60 **and** uncovered lines ≥ 25, OR- line_coverage_pct < 30 **and** total_coverable_lines ≥ 50.-Test files themselves are exempt (we don't care that-``test_foo.py`` has no coverage). Without coverage data the biomarker+Test files and code that does not ship (tooling, build, vendored,+generated, docs examples) are exempt. Without coverage data the biomarkerdoes nothing — the absence-of-coverage case is the``untested_hotspot`` biomarker's territory."""@@ -32,7 +32,7 @@ class CoverageGapDetector:return []if ctx.total_coverable_lines <= 0:return []- if ctx.is_test_material or ctx.reexport_only:+ if ctx.outside_coverage_scope or ctx.reexport_only:return []cov = ctx.line_coverage_pctdiff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.py b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.pyindex af3ae93ed..365a7bb19 100644--- a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.py+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.py@@ -17,8 +17,8 @@ adds zero walk cost (it reads already-parsed coverage).- **Absent coverage ≠ zero coverage.** When no coverage report was ingested(``line_coverage_pct is None``) the biomarker is silent - it never imputesuncovered for missing data.-- Test files are exempt (we don't penalise ``test_foo.py`` for being uncovered),- matching ``coverage_gap``.+- Test files and code that does not ship (tooling, build, vendored, generated,+ docs examples) are exempt, matching ``coverage_gap``.It is intentionally distinct from the binary gates and lives in its own cappedcategory so the additive continuous signal neither squeezes nor is squeezed by@@ -58,7 +58,7 @@ class CoverageGradientDetector:if cov is None:# No coverage data -> silent. Absent is not the same as uncovered.return []- if ctx.is_test_material or ctx.reexport_only:+ if ctx.outside_coverage_scope or ctx.reexport_only:return []uncovered_fraction = max(0.0, (100.0 - float(cov)) / 100.0)diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_complexity.py b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_complexity.pyindex 2116c2d65..52e74c4e2 100644--- a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_complexity.py+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_complexity.py@@ -7,7 +7,7 @@ tests reach it, or neither), and the finding says so and never quotes apercentage. Severity: medium from 30, high from 50, critical from 100, theupper bands for measured coverage only. A function with complexity of 30 ormore stays above the bar at full coverage, so its finding asks for a-refactor rather than tests. Test files are exempt.+refactor rather than tests. Test files and code that does not ship are exempt."""from __future__ import annotations@@ -64,7 +64,7 @@ class UntestedComplexityDetector:category = "test_coverage"def detect(self, ctx: FileContext) -> list[BiomarkerResult]:- if ctx.is_test_material or ctx.reexport_only or not ctx.all_functions:+ if ctx.outside_coverage_scope or ctx.reexport_only or not ctx.all_functions:return []out: list[BiomarkerResult] = []for fn, measured in zip(ctx.all_functions, _measured(ctx), strict=True):diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.pyindex 86bf88678..149d28e4e 100644--- a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py@@ -59,7 +59,7 @@ class UntestedHotspotDetector:# A test file is not undertested. Coverage reports rarely instrument# the suite itself, so where coverage is ingested every test file reads# as 0% covered and a churning one would be accused of it.- if ctx.is_test_material:+ if ctx.outside_coverage_scope:return []if not _has_something_to_test(ctx):return []diff --git a/packages/core/src/repowise/core/analysis/health/coverage/model.py b/packages/core/src/repowise/core/analysis/health/coverage/model.pyindex 95a2fa9cd..c0439c308 100644--- a/packages/core/src/repowise/core/analysis/health/coverage/model.py+++ b/packages/core/src/repowise/core/analysis/health/coverage/model.py@@ -90,9 +90,14 @@ def parse_xml(text: str) -> ET.Element | None:def coverage_map_entry(fc: FileCoverage, source_format: str | None) -> dict:- """The per-file dict ``HealthAnalyzer`` reads from its ``coverage_map``."""+ """The per-file dict ``HealthAnalyzer`` reads from its ``coverage_map``.++ A file with no coverable lines (lcov ``LF:0``) and no stated percentage+ has nothing measured, so its percentage is ``None``, never 0%.+ """+ measured = fc.total_coverable_lines > 0 or fc.line_coverage_pct > 0return {- "line_coverage_pct": fc.line_coverage_pct,+ "line_coverage_pct": fc.line_coverage_pct if measured else None,"branch_coverage_pct": fc.branch_coverage_pct,"covered_lines": list(fc.covered_lines),"total_coverable_lines": fc.total_coverable_lines,diff --git a/packages/core/src/repowise/core/analysis/health/file_evaluator.py b/packages/core/src/repowise/core/analysis/health/file_evaluator.pyindex 7509c2e98..24d3062d7 100644--- a/packages/core/src/repowise/core/analysis/health/file_evaluator.py+++ b/packages/core/src/repowise/core/analysis/health/file_evaluator.py@@ -282,6 +282,7 @@ class FileEvaluator:or pf.file_info.is_testor fcx.has_inline_tests,is_test=bool(pf.file_info.is_test),+ code_origin=self._code_origin(pf),# Separate from has_test_file (import or name pairing) so each signal stays visible.reached_by_tests=file_path in analyzer.test_index.reached_files(),module=_module_for(file_path, self.package_roots),@@ -407,9 +408,13 @@ class FileEvaluator:duplication_pct=ctx.duplication_pct,**file_score_fields(analysed, scores, findings),is_test=bool(pf.file_info.is_test),- code_origin=self.analyzer._origins.get(ctx.file_path) or self.analyzer._origin(pf),+ code_origin=ctx.code_origin,)+ def _code_origin(self, pf: Any) -> str:+ """The origin the walk recorded, else one read from the path alone."""+ return self.analyzer._origins.get(pf.file_info.path) or self.analyzer._origin(pf)+def _suggestions(self,pf: Any,diff --git a/tests/unit/health/test_coverage_marker_scope.py b/tests/unit/health/test_coverage_marker_scope.pynew file mode 100644index 000000000..c8835795c--- /dev/null+++ b/tests/unit/health/test_coverage_marker_scope.py@@ -0,0 +1,113 @@+"""Coverage markers fire only on shipping code that has lines to measure."""++from __future__ import annotations++import networkx as nx+import pytest++from repowise.core.analysis.health import HealthAnalyzer+from repowise.core.analysis.health.biomarkers import FileContext+from repowise.core.analysis.health.biomarkers.coverage_gap import BIOMARKER as GAP+from repowise.core.analysis.health.biomarkers.coverage_gradient import BIOMARKER as GRADIENT+from repowise.core.analysis.health.biomarkers.untested_complexity import (+ BIOMARKER as COMPLEXITY,+)+from repowise.core.analysis.health.biomarkers.untested_hotspot import BIOMARKER as HOTSPOT+from repowise.core.analysis.health.complexity.models import FunctionComplexity+from repowise.core.analysis.health.coverage import parse_lcov, parse_repowise_json+from repowise.core.analysis.health.coverage.model import coverage_map_entry, file_coverage+from repowise.core.ingestion.parser import parse_file+from repowise.core.ingestion.traverser import FileTraverser++_COVERAGE_MARKERS = {"coverage_gradient", "coverage_gap", "untested_complexity"}++_CODE = "".join(+ f"def f{i}(a, b):\n"+ + "".join(f" if a == {j}:\n return b + {j}\n" for j in range(12))+ + " return 0\n\n\n"+ for i in range(3)+)+++def _report(tmp_path, files: dict[str, str], coverage: dict[str, dict]):+ for rel, text in files.items():+ (tmp_path / rel).parent.mkdir(parents=True, exist_ok=True)+ (tmp_path / rel).write_text(text, encoding="utf-8")+ parsed = [+ parse_file(f, (tmp_path / f.path).read_bytes())+ for f in FileTraverser(tmp_path).traverse()+ ]+ return HealthAnalyzer(nx.DiGraph(), parsed_files=parsed, coverage_map=coverage).analyze()+++def _uncovered(path: str, text: str) -> dict:+ lines = range(1, text.count("\n") + 1)+ return coverage_map_entry(file_coverage(path, (), lines), "lcov")+++def test_only_production_code_gets_coverage_findings(tmp_path) -> None:+ files = {+ "src/app.py": _CODE,+ "scripts/release.py": _CODE,+ "docs/examples/demo.py": _CODE,+ }+ coverage = {p: _uncovered(p, t) for p, t in files.items()}+ report = _report(tmp_path, files, coverage)++ flagged = {f.file_path for f in report.findings if f.biomarker_type in _COVERAGE_MARKERS}+ assert flagged == {"src/app.py"}+ origins = {m.file_path: m.code_origin for m in report.metrics}+ assert origins["scripts/release.py"] == "tooling"+ assert origins["src/app.py"] == "production"+++def test_a_file_with_no_executable_lines_is_not_zero_percent_covered(tmp_path) -> None:+ files = {+ "src/notes.py": "# A module of notes.\n# Nothing here runs.\n",+ "src/limits.py": "MAX = 3\nMIN = 1\n",+ }+ lcov = "".join(f"SF:{p}\nLF:0\nLH:0\nend_of_record\n" for p in files)+ coverage = {fc.file_path: coverage_map_entry(fc, "lcov") for fc in parse_lcov(lcov).files}+ report = _report(tmp_path, files, coverage)++ assert [f for f in report.findings if f.biomarker_type in _COVERAGE_MARKERS] == []+ assert {m.file_path: m.line_coverage_pct for m in report.metrics} == {+ "src/notes.py": None,+ "src/limits.py": None,+ }+++def test_a_stated_percentage_without_a_total_is_kept() -> None:+ (fc,) = parse_repowise_json('{"files": [{"path": "a.py", "line_coverage_pct": 40}]}').files+ assert fc.total_coverable_lines == 0+ assert coverage_map_entry(fc, "repowise-json")["line_coverage_pct"] == 40.0+++_FN = (FunctionComplexity(name="run", start_line=1, end_line=40, ccn=20, max_nesting=1, cognitive=0, nloc=38),)+++ "origin", ["tooling", "build", "vendored", "generated", "docs_example", "test"]+)+def test_every_coverage_marker_skips_code_that_does_not_ship(origin: str) -> None:+ ctx = FileContext(+ file_path="src/x.py",+ language="python",+ nloc=200,+ has_test_file=False,+ module=None,+ is_test=False,+ code_origin=origin,+ all_functions=_FN,+ git_meta={"is_hotspot": True},+ dependents_count=12,+ line_coverage_pct=5.0,+ covered_lines={1},+ total_coverable_lines=200,+ coverable_lines=set(range(1, 201)),+ )+ for marker in (GRADIENT, GAP, COMPLEXITY, HOTSPOT):+ assert marker.detect(ctx) == [], marker.name+ ctx.code_origin = "production"+ for marker in (GRADIENT, GAP, COMPLEXITY, HOTSPOT):+ assert marker.detect(ctx), marker.name