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(coverage): say why a .coverage gives no per-file coverage, the same way in init and coverage add (#4320)
Raghav Chamadiyafb5ae5ab823c
diff --git a/docs/layers/TEST_INTELLIGENCE.md b/docs/layers/TEST_INTELLIGENCE.mdindex 3cb21679b..a02c84f1c 100644--- a/docs/layers/TEST_INTELLIGENCE.md+++ b/docs/layers/TEST_INTELLIGENCE.md@@ -20,7 +20,7 @@ pytest --cov --cov-report=lcov:coverage.lcovpytest --cov=<package> --cov-context=test # also records which test ran each line (needs pytest-cov)# 2. Ingest it.repowise coverage add coverage.lcov-repowise coverage add .coverage # per-file coverage + per-test map+repowise coverage add .coverage # per-test maprepowise coverage status # files, line and branch %, map counts# 3. Use it.repowise health # untested hotspots now light updiff --git a/docs/reference/CLI_REFERENCE.md b/docs/reference/CLI_REFERENCE.mdindex d020c7e7b..1d5dd430d 100644--- a/docs/reference/CLI_REFERENCE.md+++ b/docs/reference/CLI_REFERENCE.md@@ -1415,15 +1415,17 @@ successful ingest also syncs the opt-in coverage re-ingest hook(`hooks.coverage_reingest`).`add` ingests per-file line/branch coverage from LCOV, Cobertura, Clover,-JaCoCo XML, a Go coverprofile (`go test -coverprofile`), or a coverage.py-`.coverage` file. It auto-discovers `coverage/lcov.info`, `.coverage`,+JaCoCo XML, or a Go coverprofile (`go test -coverprofile`). A coverage.py+`.coverage` file records the lines that ran but not which lines are executable,+so it gives the per-test map only; export it with `coverage lcov` for per-file+coverage. It auto-discovers `coverage/lcov.info`, `.coverage`,`coverage.out`, `target/site/jacoco/jacoco.xml`, and similar reports when nopath is given, andmerges multiple reports (hit wins). When the report carries per-test contexts,a coverage.py `.coverage` recorded with per-test contexts(`pytest --cov=<package> --cov-context=test`, needs pytest-cov), or aper-test lcov, `add` also builds the per-test *test-to-code map*, which test-covers which source lines. A report without contexts still ingests the+covers which source lines. A text report without contexts still ingests theper-file coverage; it just skips the map.```bash@@ -1434,7 +1436,7 @@ repowise coverage add 'artifacts/**/lcov.info' # every shard's reportrepowise coverage add web/coverage/lcov.info=web # report paths are relative to web/repowise coverage add --verbose # show ingestion debug logspytest --cov=<package> --cov-context=test # produce .coverage with contexts (needs pytest-cov)-repowise coverage add .coverage # per-file coverage + per-test map+repowise coverage add .coverage # per-test maprepowise coverage status # coverage summary + "Test-to-code map" countsrepowise coverage status --format json # machine-readable (note: the --format on `coverage add` names the input parser instead)```diff --git a/packages/cli/src/repowise/cli/agent_targets/_data/codex_prompts/repowise-coverage.md b/packages/cli/src/repowise/cli/agent_targets/_data/codex_prompts/repowise-coverage.mdindex c04b6e7cf..c476064d7 100644--- a/packages/cli/src/repowise/cli/agent_targets/_data/codex_prompts/repowise-coverage.md+++ b/packages/cli/src/repowise/cli/agent_targets/_data/codex_prompts/repowise-coverage.md@@ -1,5 +1,5 @@----description: Ingest or inspect test-coverage reports (LCOV, Cobertura, Clover, Go cover profiles, JaCoCo, repowise JSON, or coverage.py .coverage; builds the per-test map when contexts are present), or gate a change on its patch coverage in CI.+description: Ingest or inspect test-coverage reports (LCOV, Cobertura, Clover, Go cover profiles, JaCoCo, or repowise JSON; a coverage.py .coverage builds the per-test map only), or gate a change on its patch coverage in CI.---# Repowise Coveragediff --git a/packages/cli/src/repowise/cli/agent_targets/_data/codex_prompts/repowise-health.md b/packages/cli/src/repowise/cli/agent_targets/_data/codex_prompts/repowise-health.mdindex 555432f0b..be0c94ba3 100644--- a/packages/cli/src/repowise/cli/agent_targets/_data/codex_prompts/repowise-health.md+++ b/packages/cli/src/repowise/cli/agent_targets/_data/codex_prompts/repowise-health.md@@ -35,7 +35,7 @@ Handle `$ARGUMENTS`:having been found; the default stays `all`)- "code shape" / "ignore churn" → `repowise health --counts code_shape` (leavesout the git-derived half: churn, co-change, ownership, prior fixes)-- a coverage file (e.g. `cov.lcov`, `coverage.xml`, `.coverage`) → `repowise coverage add <file>` to ingest it (folds into health markers, and builds the per-test map when the report has contexts), then `repowise health --recompute`+- a coverage file (e.g. `cov.lcov`, `coverage.xml`) → `repowise coverage add <file>` to ingest it (folds into health markers, and builds the per-test map when the report has contexts), then `repowise health --recompute`; a `.coverage` builds the per-test map onlyOther flags: `--format json` for machine-readable output, `-v, --verbose` forpipeline debug logs, `--repo <alias>` / `--no-workspace` in workspace mode.diff --git a/packages/cli/src/repowise/cli/commands/coverage_cmd.py b/packages/cli/src/repowise/cli/commands/coverage_cmd.pyindex bb08f7a7d..d0770f99f 100644--- a/packages/cli/src/repowise/cli/commands/coverage_cmd.py+++ b/packages/cli/src/repowise/cli/commands/coverage_cmd.py@@ -1,9 +1,9 @@"""``repowise coverage`` — ingest and inspect test-coverage reports.The single entry point for coverage. ``coverage add`` populates per-file-line/branch coverage (which clears ``untested_hotspot`` for tested files) and,-when a report carries contexts (a coverage.py ``.coverage`` or a per-test-lcov), also builds the per-test "test-to-code" map. ``repowise health+line/branch coverage from a text report (which clears ``untested_hotspot`` for+tested files) and, when a report carries contexts (a coverage.py ``.coverage``+or a per-test lcov), also builds the per-test "test-to-code" map. ``repowise health--recompute`` (or the next ``repowise update``) then folds in whatever was ingested here - it no longer takes a coverage flag ofits own."""@@ -27,7 +27,10 @@ from repowise.cli.helpers import ()from repowise.cli.output import emit_json, format_option, notice_consolefrom repowise.core.analysis.health.coverage import PARSERS as COVERAGE_PARSERS-from repowise.core.analysis.health.coverage.contexts import PER_TEST_COVERAGE_COMMAND+from repowise.core.analysis.health.coverage.contexts import (+ PER_TEST_COVERAGE_COMMAND,+ is_coverage_data_file,+)from repowise.core.workspace.update import get_head_commit@@ -109,11 +112,12 @@ def coverage_add(re-ingest hook to .claude/settings.local.json (removed again when the flagis not true).- Stores per-file line/branch coverage, and additionally the per-test+ Stores per-file line/branch coverage from text reports, and the per-test"test-to-code" map when a report carries contexts - a coverage.py``.coverage`` recorded with ``pytest --cov --cov-context=test``, or a per-test- lcov. The map is what answers "which tests exercise this change"; reports- without contexts ingest per-file coverage only.+ lcov. The map is what answers "which tests exercise this change". A+ ``.coverage`` gives the map only: it does not record which lines are+ executable, so export it with ``coverage lcov`` for per-file coverage.Examples:@@ -171,11 +175,6 @@ def coverage_add(+ ", ".join(p.name for p in report_paths[:5]))- # The aggregate parsers read text (lcov/xml/json); the coverage.py- # ``.coverage`` sqlite only feeds the per-test map, so keep it out of the- # text-parsing path (its binary bytes would not decode as UTF-8).- agg_paths = [p for p in report_paths if not _is_sqlite(p)]-async def _do() -> bool:from repowise.core.persistence import (create_engine,@@ -223,71 +222,74 @@ def coverage_add(agg_matched = 0unmapped = 0mapping_partial = False- if agg_paths:- resolved, errors = build_coverage_map(- repo_path,- agg_paths,- repo_keys,- coverage_format=coverage_format or cfg.format,- strip_prefix=cfg.strip_prefix,- path_prefix=cfg.path_prefix,- report_prefixes=report_prefixes,- ignore=cfg.ignore,+ # The same read init runs; a ``.coverage`` comes back in *errors*+ # with why it has no per-file coverage, then feeds the map below.+ resolved, errors = build_coverage_map(+ repo_path,+ report_paths,+ repo_keys,+ coverage_format=coverage_format or cfg.format,+ strip_prefix=cfg.strip_prefix,+ path_prefix=cfg.path_prefix,+ report_prefixes=report_prefixes,+ ignore=cfg.ignore,+ )+ for path, err in errors:+ console.print(f"[yellow] {path.name}: {err}[/yellow]")+ # Counted from `resolved`, not from inside the `resolved.files`+ # branch below: total mapping loss leaves `files` empty, so+ # reading the count in there reported 0 report files unmapped+ # for the one run where every single one of them was.+ skipped = resolved.unmatched + resolved.ambiguous+ unmapped = len(skipped)+ mapping_partial = resolved.mapping_partial+ if resolved.files:+ await save_coverage_files(+ session,+ repo_row.id,+ resolved.files,+ source_format=resolved.source_format or "lcov",+ ingested_commit_sha=head_sha,+ provenance=resolved.provenance,+ )+ agg_matched = resolved.matched+ console.print(+ f"[green]Ingested coverage for {resolved.matched} file(s)[/green] "+ f"({resolved.matched_exact} exact, {resolved.matched_suffix} resolved).")- for path, err in errors:- console.print(f"[yellow] {path.name}: {err}[/yellow]")- # Counted from `resolved`, not from inside the `resolved.files`- # branch below: total mapping loss leaves `files` empty, so- # reading the count in there reported 0 report files unmapped- # for the one run where every single one of them was.- skipped = resolved.unmatched + resolved.ambiguous- unmapped = len(skipped)- mapping_partial = resolved.mapping_partial- if resolved.files:- await save_coverage_files(- session,- repo_row.id,- resolved.files,- source_format=resolved.source_format or "lcov",- ingested_commit_sha=head_sha,- provenance=resolved.provenance,+ if skipped:+ sample = ", ".join(skipped[:5])+ console.print(+ f"[yellow]{len(skipped)} report file(s) did not map to the "+ f"repo tree[/yellow] (e.g. {sample}).")- agg_matched = resolved.matched+ if mapping_partial:console.print(- f"[green]Ingested coverage for {resolved.matched} file(s)[/green] "- f"({resolved.matched_exact} exact, {resolved.matched_suffix} resolved)."+ "[red]More than half the report did not map to the repo "+ "tree.[/red] The stored coverage describes only a fragment "+ "of the repository. Fix [cyan]coverage.strip_prefix[/cyan] "+ "or [cyan]coverage.path_prefix[/cyan] in "+ ".repowise/config.yaml and re-run.")- if skipped:- sample = ", ".join(skipped[:5])- console.print(- f"[yellow]{len(skipped)} report file(s) did not map to the "- f"repo tree[/yellow] (e.g. {sample})."- )- if mapping_partial:- console.print(- "[red]More than half the report did not map to the repo "- "tree.[/red] The stored coverage describes only a fragment "- "of the repository. Fix [cyan]coverage.strip_prefix[/cyan] "- "or [cyan]coverage.path_prefix[/cyan] in "- ".repowise/config.yaml and re-run."- )# --- Per-test map, from any report that carries contexts.map_records: list = []map_format: str | None = None+ map_readable = Falsefor report_path in report_paths:creport = parse_contexts_file(report_path)if not creport.has_contexts:# A plain lcov without TN is normal (handled as aggregate# above); a .coverage without contexts is worth calling out# so the user knows why no map was built.- if _is_sqlite(report_path):+ if is_coverage_data_file(report_path):console.print(f"[yellow]{report_path.name}: no per-test contexts "f"(recorded without per-test contexts); per-test map "f"skipped.[/yellow]")continue+ map_readable = Truertc = resolve_test_reports(creport,repo_keys,@@ -317,6 +319,10 @@ def coverage_add()if not agg_matched and not map_records:+ if len(errors) == len(report_paths) and not map_readable:+ # No report was readable as coverage; the lines above say why.+ console.print("[red]Nothing was stored.[/red]")+ return Falseconsole.print("[red]No report files mapped to indexed source files.[/red] ""If paths look prefixed (e.g. build/…), set "@@ -354,18 +360,6 @@ def coverage_add(sync_repo_hook(repo_path, console)-_SQLITE_MAGIC = b"SQLite format 3\x00"---def _is_sqlite(path: Path) -> bool:- """True when *path* is a coverage.py ``.coverage`` sqlite (binary magic)."""- try:- with path.open("rb") as fh:- return fh.read(len(_SQLITE_MAGIC)) == _SQLITE_MAGIC- except OSError:- return False--def _discover_context_reports(repo_path: Path) -> list[Path]:"""Find per-test coverage artifacts under the repo root.diff --git a/packages/core/src/repowise/core/analysis/health/coverage/contexts.py b/packages/core/src/repowise/core/analysis/health/coverage/contexts.pyindex 75c70fae6..075c98506 100644--- a/packages/core/src/repowise/core/analysis/health/coverage/contexts.py+++ b/packages/core/src/repowise/core/analysis/health/coverage/contexts.py@@ -38,6 +38,23 @@ PER_TEST_COVERAGE_COMMAND = "pytest --cov --cov-context=test"# coverage.py sqlite files start with the standard SQLite magic header._SQLITE_MAGIC = b"SQLite format 3\x00"+#: Why a ``.coverage`` gives no per-file coverage: its schema stores the lines+#: that ran, never the executable set, so no percentage can be taken from it.+COVERAGE_DATA_FILE_NOTE = (+ "a coverage.py data file records the lines that ran but not which lines are "+ "executable, so it gives no per-file coverage; export it with `coverage lcov` "+ "or `coverage xml` and add that report"+)+++def is_coverage_data_file(path: str | Path) -> bool:+ """True when *path* is a coverage.py ``.coverage`` sqlite (binary magic)."""+ try:+ with Path(path).open("rb") as fh:+ return fh.read(len(_SQLITE_MAGIC)) == _SQLITE_MAGIC+ except OSError:+ return False+def parse_lcov_contexts(text: str) -> ContextCoverageReport:"""Parse per-test records from an lcov report's ``TN:`` names.@@ -203,15 +220,9 @@ def parse_contexts_file(path: str | Path) -> ContextCoverageReport:empty aggregate-only report rather than raising."""p = Path(path)- try:- with p.open("rb") as fh:- head = fh.read(len(_SQLITE_MAGIC))- except OSError:- return ContextCoverageReport(source_format="unknown", has_contexts=False)-- if head == _SQLITE_MAGIC:+ if is_coverage_data_file(p):return parse_coverage_sqlite(p)try:return parse_lcov_contexts(p.read_text(encoding="utf-8"))- except OSError:+ except (OSError, UnicodeDecodeError):return ContextCoverageReport(source_format="unknown", has_contexts=False)diff --git a/packages/core/src/repowise/core/analysis/health/coverage/discovery.py b/packages/core/src/repowise/core/analysis/health/coverage/discovery.pyindex 0d5c81d90..0c54310e9 100644--- a/packages/core/src/repowise/core/analysis/health/coverage/discovery.py+++ b/packages/core/src/repowise/core/analysis/health/coverage/discovery.py@@ -34,6 +34,7 @@ import pathspecfrom repowise.core.fs_walk import PRUNED_DIRS, WalkSnapshot, iter_glob+from .contexts import COVERAGE_DATA_FILE_NOTE, is_coverage_data_filefrom .detector import parse as parse_coveragefrom .model import (ContextCoverageReport,@@ -1140,14 +1141,15 @@ def build_coverage_map(parsed: list[CoverageReport] = []errors: list[tuple[Path, str]] = []for path in report_paths:+ if is_coverage_data_file(path):+ errors.append((path, COVERAGE_DATA_FILE_NOTE))+ continuetry:text = path.read_text(encoding="utf-8")except OSError as exc:errors.append((path, f"could not read: {exc}"))continueexcept UnicodeDecodeError:- # A binary artifact, most often a coverage.py ``.coverage`` database:- # export it with ``coverage lcov`` or ``coverage xml`` first.errors.append((path, "not a text coverage report"))continuereport = parse_coverage(text, format=coverage_format)diff --git a/plugins/claude-code/commands/coverage.md b/plugins/claude-code/commands/coverage.mdindex 2ec9187f5..62c5213ef 100644--- a/plugins/claude-code/commands/coverage.md+++ b/plugins/claude-code/commands/coverage.md@@ -1,5 +1,5 @@----description: Ingest or inspect test-coverage reports (LCOV, Cobertura, Clover, Go cover profiles, JaCoCo, repowise JSON, or coverage.py .coverage; builds the per-test map when contexts are present), or gate a change on its patch coverage in CI.+description: Ingest or inspect test-coverage reports (LCOV, Cobertura, Clover, Go cover profiles, JaCoCo, or repowise JSON; a coverage.py .coverage builds the per-test map only), or gate a change on its patch coverage in CI.allowed-tools: Bash(repowise coverage:*), Read---diff --git a/plugins/claude-code/commands/health.md b/plugins/claude-code/commands/health.mdindex 8a793cc4a..13e31416a 100644--- a/plugins/claude-code/commands/health.md+++ b/plugins/claude-code/commands/health.md@@ -36,7 +36,7 @@ Handle `$ARGUMENTS`:having been found; the default stays `all`)- "code shape" / "ignore churn" → `repowise health --counts code_shape` (leavesout the git-derived half: churn, co-change, ownership, prior fixes)-- a coverage file (e.g. `cov.lcov`, `coverage.xml`, `.coverage`) → `repowise coverage add <file>` to ingest it (folds into health markers, and builds the per-test map when the report has contexts), then `repowise health --recompute`+- a coverage file (e.g. `cov.lcov`, `coverage.xml`) → `repowise coverage add <file>` to ingest it (folds into health markers, and builds the per-test map when the report has contexts), then `repowise health --recompute`; a `.coverage` builds the per-test map onlyOther flags: `--format json` for machine-readable output, `-v, --verbose` forpipeline debug logs, `--repo <alias>` / `--no-workspace` in workspace mode.diff --git a/plugins/shared/commands/coverage.md b/plugins/shared/commands/coverage.mdindex 312fd7523..c023ed78e 100644--- a/plugins/shared/commands/coverage.md+++ b/plugins/shared/commands/coverage.md@@ -1,6 +1,6 @@---frontmatter: |- description: Ingest or inspect test-coverage reports (LCOV, Cobertura, Clover, Go cover profiles, JaCoCo, repowise JSON, or coverage.py .coverage; builds the per-test map when contexts are present), or gate a change on its patch coverage in CI.+ description: Ingest or inspect test-coverage reports (LCOV, Cobertura, Clover, Go cover profiles, JaCoCo, or repowise JSON; a coverage.py .coverage builds the per-test map only), or gate a change on its patch coverage in CI.allowed-tools: Bash(repowise coverage:*), Read---diff --git a/plugins/shared/commands/health.md b/plugins/shared/commands/health.mdindex 1ca755093..5e1cdb35b 100644--- a/plugins/shared/commands/health.md+++ b/plugins/shared/commands/health.md@@ -37,7 +37,7 @@ Handle `$ARGUMENTS`:having been found; the default stays `all`)- "code shape" / "ignore churn" → `repowise health --counts code_shape` (leavesout the git-derived half: churn, co-change, ownership, prior fixes)-- a coverage file (e.g. `cov.lcov`, `coverage.xml`, `.coverage`) → `repowise coverage add <file>` to ingest it (folds into health markers, and builds the per-test map when the report has contexts), then `repowise health --recompute`+- a coverage file (e.g. `cov.lcov`, `coverage.xml`) → `repowise coverage add <file>` to ingest it (folds into health markers, and builds the per-test map when the report has contexts), then `repowise health --recompute`; a `.coverage` builds the per-test map onlyOther flags: `--format json` for machine-readable output, `-v, --verbose` forpipeline debug logs, `--repo <alias>` / `--no-workspace` in workspace mode.diff --git a/tests/unit/cli/test_coverage_cmd.py b/tests/unit/cli/test_coverage_cmd.pyindex 4a29a6c21..d03852f23 100644--- a/tests/unit/cli/test_coverage_cmd.py+++ b/tests/unit/cli/test_coverage_cmd.py@@ -351,3 +351,52 @@ def test_init_and_update_keep_the_repo_hook_in_step(update_command._refresh_editor_stamp(tmp_path, None)assert calls == [tmp_path]assert "sync_repo_hook(repo_path, console)" in inspect.getsource(init_finish._write_editor_files)+++def _coverage_data(path, context: str | None, source) -> None:+ """A real coverage.py ``.coverage`` with lines 1-2 of *source* run under *context*."""+ coverage = pytest.importorskip("coverage")+ data = coverage.CoverageData(basename=str(path))+ if context:+ data.set_context(context)+ data.add_lines({str(source): [1, 2]})+ data.write()+++def test_coverage_add_dot_coverage_builds_the_map_and_says_why_no_per_file(+ monkeypatch: pytest.MonkeyPatch, tmp_path+) -> None:+ stored = _fake_ingest(monkeypatch, tmp_path, {"src/a.py"})+ mapped: list = []++ async def _save_map(session, repository_id, records, **kwargs):+ mapped.extend(records)+ return len(records)++ monkeypatch.setattr("repowise.core.persistence.crud.save_test_coverage", _save_map)+ _coverage_data(tmp_path / ".coverage", "tests/test_a.py::test_one|run", tmp_path / "src" / "a.py")++ result = CliRunner().invoke(cli, ["coverage", "add", ".coverage"])++ assert result.exit_code == 0, result.output+ # No executable-line set in the file, so no per-file row; the map is built.+ assert stored == []+ assert [(r.file_path, r.covered_lines) for r in mapped] == [("src/a.py", [1, 2])]+ assert "coverage lcov" in " ".join(result.output.split())+++def test_coverage_add_dot_coverage_without_contexts_stores_nothing(+ monkeypatch: pytest.MonkeyPatch, tmp_path+) -> None:+ stored = _fake_ingest(monkeypatch, tmp_path, {"src/a.py"})+ _coverage_data(tmp_path / ".coverage", None, tmp_path / "src" / "a.py")++ result = CliRunner().invoke(cli, ["coverage", "add", ".coverage"])++ output = " ".join(result.output.split())+ assert result.exit_code == 1, result.output+ assert stored == []+ assert "coverage lcov" in output+ assert "Nothing was stored." in output+ # The paths were never the problem, so no prefix advice.+ assert "strip_prefix" not in outputdiff --git a/tests/unit/pipeline/test_coverage_ingestion.py b/tests/unit/pipeline/test_coverage_ingestion.pyindex 579daf5f2..bbbfd0f54 100644--- a/tests/unit/pipeline/test_coverage_ingestion.py+++ b/tests/unit/pipeline/test_coverage_ingestion.py@@ -10,6 +10,8 @@ from __future__ import annotationsfrom pathlib import Pathfrom types import SimpleNamespace+import pytest+from repowise.core.pipeline.phases.analysis import _build_pipeline_coverage@@ -93,3 +95,23 @@ def test_strip_prefix_from_config(tmp_path: Path) -> None:)assert "web/index.ts" in coverage_mapassert "api/index.ts" not in coverage_map+++def test_dot_coverage_says_why_it_has_no_per_file_coverage(tmp_path: Path) -> None:+ from repowise.core.analysis.health.coverage.contexts import COVERAGE_DATA_FILE_NOTE++ coverage = pytest.importorskip("coverage")+ db = tmp_path / ".coverage"+ data = coverage.CoverageData(basename=str(db))+ data.add_lines({str(tmp_path / "src" / "a.py"): [1, 2]})+ data.write()+ messages: list[tuple[str, str]] = []+ progress = SimpleNamespace(on_message=lambda level, msg: messages.append((level, msg)))++ coverage_map, files, _fmt, _provenance = _build_pipeline_coverage(+ tmp_path, [_parsed("src/a.py")], [db], progress=progress+ )++ assert coverage_map == {}+ assert files == []+ assert ("warning", f" ↳ coverage .coverage: {COVERAGE_DATA_FILE_NOTE}") in messages