Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(coverage): re-read a coverage report on update when it was written after the last ingest (#4327)

Raghav Chamadiyabd0231cefd61
diff --git a/docs/layers/TEST_INTELLIGENCE.md b/docs/layers/TEST_INTELLIGENCE.md
index 480e35ea1..bfd7f4626 100644
--- a/docs/layers/TEST_INTELLIGENCE.md
+++ b/docs/layers/TEST_INTELLIGENCE.md
@@ -254,7 +254,7 @@ coverage:
format: lcov # skip format sniffing
strip_prefix: "/build/src/" # trim an absolute prefix from report paths
ignore: ["**/*_pb2.py"] # gitignore-style globs coverage leaves out
- reingest_on_update: false
+ reingest_on_update: false # never re-read on update (default: when a report is newer)
fail_under: 80 # patch-coverage gate for `coverage check` (0-100)
min_coverable_lines: 5 # small-change tolerance for that gate
max_drop: 0.5 # most project coverage may fall from the base, in points
@@ -262,10 +262,11 @@ coverage:
- {name: api, paths: ["/services/api/"], fail_under: 85}
```
-`init` and `update` also discover and ingest coverage;
+`init` and `update` also discover and ingest coverage; `update` re-reads a report
+written after the last ingest;
`repowise init --coverage-report <path>` takes explicit reports (repeatable).
-`--coverage-report` is test coverage; `--coverage` controls documentation breadth. Full
-block: [CONFIG.md](../reference/CONFIG.md).
+`--coverage-report` is test coverage; `--coverage` controls documentation breadth. When
+`update` re-reads reports: [CONFIG.md](../reference/CONFIG.md#the-coverage-block).
## Accuracy and limits
diff --git a/docs/reference/CONFIG.md b/docs/reference/CONFIG.md
index d437d43a9..b37b8b68c 100644
--- a/docs/reference/CONFIG.md
+++ b/docs/reference/CONFIG.md
@@ -16,6 +16,7 @@ The `.repowise/` directory, provider setup, API keys, and what's customizable.
[The `editor_files:` block](#the-editor_files-block) ·
[The `decisions:` block](#the-decisions-block) ·
[`policies.yaml`](#repowisepoliciesyaml) ·
+[The `coverage:` block](#the-coverage-block) ·
[The `security:` block](#the-security-block) ·
[The `attribution:` block](#the-attribution-block) ·
[The `events:` block](#the-events-block) ·
@@ -640,6 +641,16 @@ spec:
members: [ana-acme, li-acme]
```
+### The `coverage:` block
+
+Where coverage reports live and how their paths map; the full block is in
+[test intelligence](../layers/TEST_INTELLIGENCE.md#configuration).
+`repowise update` re-reads the reports when one in `coverage.paths`, or at a
+fixed discovery location such as `coverage/lcov.info`, was written after the
+last ingest, replacing coverage added by hand from another report. A report
+only a `**` glob finds is not watched; name it in `coverage.paths`. `reingest_on_update: true` re-reads them on
+every update, and `false` keeps the stored coverage until `repowise coverage add`.
+
### The `security:` block
Secret shapes of your own for `repowise security check`.
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
index 6e6fa0412..aa324902f 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
@@ -133,23 +133,23 @@ async def _coverage_for_rescore(
) -> RescoreCoverage:
"""Coverage to feed a health re-score, preserved across updates.
- Default: reload the rows already persisted (no re-parse). When
- ``coverage.reingest_on_update`` is set, re-discover and re-resolve a
- fresh report instead.
+ Re-discover and re-resolve the reports when one was written after the
+ last ingest (``CoverageConfig.reingest``; ``coverage.reingest_on_update``
+ forces it on or off), else reload the rows already persisted.
"""
from repowise.core.analysis.health.coverage import (
CoverageConfig,
build_coverage_map,
)
from repowise.core.persistence.crud import load_coverage_map
+ from repowise.core.pipeline.incremental.loaders import coverage_reports_to_reread
from repowise.core.repo_config import load_repo_config
cfg = CoverageConfig.from_repo_config(load_repo_config(repo_path))
- # Without paths or discovery there is nothing to re-read, so the stored
- # rows (e.g. from `coverage add`) stay authoritative.
- if cfg.reingest_on_update and (cfg.paths or cfg.auto_discover):
- reports = cfg.reports(repo_path)
+ # ``None`` keeps the stored rows (e.g. from `coverage add`).
+ reports = await coverage_reports_to_reread(session, repo_id, Path(repo_path), cfg)
+ if reports is not None:
if reports:
repo_keys = {pf.file_info.path for pf in parsed_files}
resolved, _errors = build_coverage_map(
diff --git a/packages/core/src/repowise/core/analysis/health/coverage/discovery.py b/packages/core/src/repowise/core/analysis/health/coverage/discovery.py
index 44fabc709..cd44bba00 100644
--- a/packages/core/src/repowise/core/analysis/health/coverage/discovery.py
+++ b/packages/core/src/repowise/core/analysis/health/coverage/discovery.py
@@ -23,9 +23,11 @@ Two jobs that make ingested coverage *actually line up* with the repo:
from __future__ import annotations
+import time
from collections import Counter, defaultdict
from collections.abc import Iterable, Iterator, Mapping, Sequence
from dataclasses import dataclass, field
+from datetime import UTC, datetime
from itertools import chain
from pathlib import Path
from typing import Any
@@ -127,9 +129,10 @@ class CoverageConfig:
strip_prefix: str | None = None
# Prepend this prefix to report paths before matching.
path_prefix: str | None = None
- # Re-discover + re-parse reports on every ``repowise update`` (default:
- # reuse the rows already in the DB; only re-ingest if a report is found).
- reingest_on_update: bool = False
+ # Re-read reports on ``repowise update``: ``True`` every time, ``False``
+ # never (keep the stored rows), unset when one was written after the last
+ # ingest (:meth:`reingest`).
+ reingest_on_update: bool | None = None
# Patch-coverage gate for ``repowise coverage check`` (percent, 0-100).
fail_under: float | None = None
# Small-change tolerance: a change with fewer changed executable lines
@@ -172,7 +175,11 @@ class CoverageConfig:
format=block.get("format") or None,
strip_prefix=block.get("strip_prefix") or None,
path_prefix=block.get("path_prefix") or None,
- reingest_on_update=bool(block.get("reingest_on_update", False)),
+ reingest_on_update=(
+ None
+ if block.get("reingest_on_update") is None
+ else bool(block["reingest_on_update"])
+ ),
fail_under=_percent(block.get("fail_under")),
min_coverable_lines=_line_count(block.get("min_coverable_lines")),
gates=gates,
@@ -182,6 +189,32 @@ class CoverageConfig:
fail_under_branches=_percent(block.get("fail_under_branches")),
)
+ def reingest(
+ self,
+ newest_report_mtime: float | None,
+ last_ingest_at: datetime | None,
+ now: float | None = None,
+ ) -> bool:
+ """Whether ``update`` re-reads the reports instead of keeping the stored rows.
+
+ *newest_report_mtime* is the newest report's modification time (epoch
+ seconds, ``None`` with no report); *last_ingest_at* is when coverage was
+ last stored (naive means UTC, ``None`` when never); *now* defaults to
+ the clock. Unset, a report written after the last ingest, or any report
+ before a first ingest, is re-read. A time later than *now* (clock skew,
+ an unpacked artifact) proves nothing, or every update would re-read it.
+ """
+ if self.reingest_on_update is not None:
+ return self.reingest_on_update
+ if newest_report_mtime is None:
+ return False
+ if last_ingest_at is None:
+ return True
+ if last_ingest_at.tzinfo is None:
+ last_ingest_at = last_ingest_at.replace(tzinfo=UTC)
+ now = time.time() if now is None else now
+ return last_ingest_at.timestamp() < newest_report_mtime <= now
+
def reports(self, repo_root: Path) -> dict[Path, str | None]:
"""Each report this config names, mapped to its per-report prefix (or ``None``).
@@ -199,6 +232,21 @@ class CoverageConfig:
return dict.fromkeys(discover_artifacts(repo_root, globs=self.artifacts or None))
return {}
+ def watched_reports(self, repo_root: Path) -> list[Path]:
+ """The reports whose age ``update`` checks: :meth:`reports` without a tree walk.
+
+ Explicit ``paths`` as written; else the glob-free discovery entries,
+ each a single stat. Ceiling: a report only a wildcard glob finds is not
+ watched, since the walk costs seconds on a large repository on every
+ update; name it in ``coverage.paths`` to have it watched.
+ """
+ if self.paths:
+ return list(self.reports(repo_root))
+ if not self.auto_discover:
+ return []
+ literal = [g for g in self.artifacts or DEFAULT_DISCOVERY_GLOBS if not _has_magic(g)]
+ return [repo_root / g for g in literal if (repo_root / g).is_file()]
+
def configured_coverage(repo_root: Path | str) -> CoverageConfig:
"""*repo_root*'s ``coverage:`` config, the defaults when it cannot be read.
diff --git a/packages/core/src/repowise/core/pipeline/incremental/loaders.py b/packages/core/src/repowise/core/pipeline/incremental/loaders.py
index ca560fe21..dfd5d41a1 100644
--- a/packages/core/src/repowise/core/pipeline/incremental/loaders.py
+++ b/packages/core/src/repowise/core/pipeline/incremental/loaders.py
@@ -197,10 +197,11 @@ async def load_stored_coverage_map(repo_path: Any, *, log: LogFn | None = None)
(issue #1739).
Returns ``{path: coverage-dict}`` by reloading the persisted ``CoverageFile``
- rows, mirroring ``_coverage_for_rescore`` in the CLI update path. Empty dict
- when no coverage is stored, the store is unreadable, or the repository row
- is missing — the analyzer then scores without coverage, matching the
- established safe default.
+ rows, mirroring ``_coverage_for_rescore`` in the CLI update path. A report
+ :func:`coverage_reports_to_reread` names is read and stored first, so the
+ rows describe the newest report. Empty dict when no coverage is stored, the
+ store is unreadable, or the repository row is missing — the analyzer then
+ scores without coverage, matching the established safe default.
"""
log = log or _noop_log
@@ -219,8 +220,10 @@ async def load_stored_coverage_map(repo_path: Any, *, log: LogFn | None = None)
from repowise.core.persistence.database import resolve_db_url
engine = create_engine(resolve_db_url(repo_path))
+ factory = create_session_factory(engine)
try:
- async with get_session(create_session_factory(engine)) as session:
+ await _refresh_from_newer_reports(factory, Path(repo_path), log)
+ async with get_session(factory) as session:
repo = await get_repository_by_path(session, str(repo_path))
if repo is None:
return {}
@@ -231,3 +234,94 @@ async def load_stored_coverage_map(repo_path: Any, *, log: LogFn | None = None)
except Exception as exc:
log(f"[yellow]Stored coverage unavailable: {exc}[/yellow]")
return {}
+
+
+async def coverage_reports_to_reread(
+ session: Any, repo_id: str, repo_path: Path, cfg: Any
+) -> dict[Path, str | None] | None:
+ """The reports an update re-reads, each with its prefix; ``None`` keeps the stored rows.
+
+ *cfg* is the repo's ``CoverageConfig``. The decision is its ``reingest``: a
+ report written after the last ingest by default, every update or never
+ when ``coverage.reingest_on_update`` says so. An empty dict is a forced
+ re-read that found no report. Without ``paths`` or discovery there is
+ nothing to re-read. By default only ``watched_reports`` are stat-ed; the
+ discovery walk runs only for a re-read that is due.
+ """
+ from repowise.core.persistence.crud.analysis import load_newest_ingest
+
+ if cfg.reingest_on_update is False or not (cfg.paths or cfg.auto_discover):
+ return None
+ if cfg.reingest_on_update is None:
+ watched = cfg.watched_reports(repo_path)
+ if not watched:
+ return None
+ newest = await load_newest_ingest(session, repo_id)
+ last_ingest = newest.ingested_at if newest is not None else None
+ if not cfg.reingest(_newest_mtime(watched), last_ingest):
+ return None
+ return cfg.reports(repo_path)
+
+
+def _newest_mtime(reports: Any) -> float | None:
+ """The newest modification time among *reports*, ``None`` when none can be read."""
+ times = []
+ for path in reports:
+ try:
+ times.append(Path(path).stat().st_mtime)
+ except OSError:
+ continue
+ return max(times, default=None)
+
+
+async def _refresh_from_newer_reports(factory: Any, repo_path: Path, log: LogFn) -> None:
+ """Store the reports :func:`coverage_reports_to_reread` names as the coverage.
+
+ Its own session, so a report that fails to read rolls back only this write
+ and the stored rows still load. Nothing is written before anything is
+ indexed: with no indexed files every report would read as unmapped.
+ """
+ from repowise.core.analysis.health.coverage import CoverageConfig, build_coverage_map
+ from repowise.core.persistence import get_session
+ from repowise.core.persistence.crud import (
+ get_health_metrics,
+ get_repository_by_path,
+ save_coverage_files,
+ )
+ from repowise.core.repo_config import load_repo_config
+ from repowise.core.workspace.update import get_head_commit
+
+ try:
+ cfg = CoverageConfig.from_repo_config(load_repo_config(repo_path))
+ async with get_session(factory) as session:
+ repo = await get_repository_by_path(session, str(repo_path))
+ if repo is None:
+ return
+ reports = await coverage_reports_to_reread(session, repo.id, repo_path, cfg)
+ if not reports:
+ return
+ keys = {m.file_path for m in await get_health_metrics(session, repo.id)}
+ if not keys:
+ return
+ resolved, _errors = build_coverage_map(
+ repo_path,
+ list(reports),
+ keys,
+ coverage_format=cfg.format,
+ strip_prefix=cfg.strip_prefix,
+ path_prefix=cfg.path_prefix,
+ report_prefixes=reports,
+ ignore=cfg.ignore,
+ )
+ # Live HEAD, as every other writer stamps (issue #1747).
+ await save_coverage_files(
+ session,
+ repo.id,
+ resolved.files,
+ source_format=resolved.source_format or "lcov",
+ ingested_commit_sha=get_head_commit(repo_path),
+ provenance=resolved.provenance,
+ )
+ log(f"Re-read coverage from {len(reports)} report(s): {len(resolved.files)} file(s) stored")
+ except Exception as exc:
+ log(f"[yellow]Coverage re-read skipped: {exc}[/yellow]")
diff --git a/tests/unit/cli/test_update_persist_reliability.py b/tests/unit/cli/test_update_persist_reliability.py
index eec0129a7..36e895479 100644
--- a/tests/unit/cli/test_update_persist_reliability.py
+++ b/tests/unit/cli/test_update_persist_reliability.py
@@ -318,6 +318,49 @@ def _write_lcov(path: Path, *sources: str) -> None:
path.write_text("".join(f"SF:{s}\nDA:1,1\nend_of_record\n" for s in sources), encoding="utf-8")
+ ("setting", "report_age", "reread"),
+ [
+ (None, +60, True), # written after the last ingest
+ (None, -60, False), # already ingested
+ (False, +60, False), # opted out
+ ],
+)
+def test_update_rereads_a_report_newer_than_the_last_ingest(
+ tmp_path: Path, monkeypatch, setting, report_age: int, reread: bool
+) -> None:
+ import os
+
+ from repowise.cli.commands.update_cmd import persistence
+
+ block: dict = {"paths": ["c.lcov"]}
+ if setting is not None:
+ block["reingest_on_update"] = setting
+ monkeypatch.setattr(
+ "repowise.core.repo_config.load_repo_config", lambda _path: {"coverage": block}
+ )
+ last = datetime(2026, 10, 1, 12, 0, tzinfo=UTC)
+
+ async def _newest(_session, _repo_id):
+ return SimpleNamespace(ingested_at=last.replace(tzinfo=None))
+
+ async def _stored(_session, _repo_id):
+ return {"a.py": {"source_format": "stored"}}
+
+ monkeypatch.setattr("repowise.core.persistence.crud.analysis.load_newest_ingest", _newest)
+ monkeypatch.setattr("repowise.core.persistence.crud.load_coverage_map", _stored)
+ report = tmp_path / "c.lcov"
+ _write_lcov(report, "a.py")
+ stamp = last.timestamp() + report_age
+ os.utime(report, (stamp, stamp))
+ parsed = [SimpleNamespace(file_info=SimpleNamespace(path="a.py"))]
+
+ coverage = asyncio.run(persistence._coverage_for_rescore(object(), "repo", tmp_path, parsed))
+
+ assert coverage.authoritative is reread
+ assert coverage.source_format == ("lcov" if reread else "stored")
+
+
# ---------------------------------------------------------------------------
# A retirement has to reach an index nobody re-indexes
# ---------------------------------------------------------------------------
diff --git a/tests/unit/health/test_coverage_discovery.py b/tests/unit/health/test_coverage_discovery.py
index 8d2736312..8958a4ae3 100644
--- a/tests/unit/health/test_coverage_discovery.py
+++ b/tests/unit/health/test_coverage_discovery.py
@@ -382,6 +382,44 @@ def test_coverage_config_parses_block() -> None:
assert cfg.reingest_on_update is True
+def test_reingest_rereads_a_report_written_after_the_last_ingest() -> None:
+ from datetime import UTC, datetime
+
+ last = datetime(2026, 10, 1, 12, 0, tzinfo=UTC)
+ before, after = last.timestamp() - 60, last.timestamp() + 60
+ auto = CoverageConfig.from_repo_config({"coverage": {}})
+ assert auto.reingest_on_update is None
+ assert auto.reingest(after, last) is True
+ assert auto.reingest(before, last) is False
+ # A naive stored time is UTC.
+ assert auto.reingest(after, last.replace(tzinfo=None)) is True
+ # Nothing stored yet: any report is read; no report: nothing to read.
+ assert auto.reingest(before, None) is True
+ assert auto.reingest(None, last) is False
+ # A report dated in the future is not newer, or every update would re-read it.
+ assert auto.reingest(after, last, now=last.timestamp() + 30) is False
+ # The setting forces either way.
+ off = CoverageConfig.from_repo_config({"coverage": {"reingest_on_update": False}})
+ on = CoverageConfig.from_repo_config({"coverage": {"reingest_on_update": True}})
+ assert off.reingest(after, last) is False
+ assert on.reingest(before, last) is True
+
+
+def test_watched_reports_stat_fixed_locations_without_a_walk(tmp_path: Path) -> None:
+ (tmp_path / "coverage").mkdir()
+ (tmp_path / "coverage" / "lcov.info").write_text("SF:a.py\nend_of_record\n")
+ (tmp_path / "pkg").mkdir()
+ (tmp_path / "pkg" / "cobertura.xml").write_text("<coverage/>")
+ cfg = CoverageConfig.from_repo_config(None)
+ # The nested report needs the ``**/cobertura.xml`` walk, so only discovery finds it.
+ assert cfg.watched_reports(tmp_path) == [tmp_path / "coverage" / "lcov.info"]
+ assert len(cfg.reports(tmp_path)) == 2
+ named = CoverageConfig.from_repo_config({"coverage": {"paths": ["pkg/cobertura.xml"]}})
+ assert named.watched_reports(tmp_path) == [tmp_path / "pkg" / "cobertura.xml"]
+ off = CoverageConfig.from_repo_config({"coverage": {"auto_discover": False}})
+ assert off.watched_reports(tmp_path) == []
+
+
def test_coverage_config_paths_take_globs_and_per_report_prefixes(tmp_path: Path) -> None:
for rel in ("web/coverage/lcov.info", "api/coverage/lcov.info", "root.lcov"):
(tmp_path / rel).parent.mkdir(parents=True, exist_ok=True)
diff --git a/tests/unit/pipeline/test_load_stored_coverage_map.py b/tests/unit/pipeline/test_load_stored_coverage_map.py
index 70b243201..5209cf705 100644
--- a/tests/unit/pipeline/test_load_stored_coverage_map.py
+++ b/tests/unit/pipeline/test_load_stored_coverage_map.py
@@ -78,3 +78,80 @@ async def test_loader_no_store_returns_empty(tmp_path):
"""Reading must never conjure a store, and an absent repo is no coverage."""
assert await load_stored_coverage_map(tmp_path) == {}
assert not (tmp_path / ".repowise").exists()
+
+
+def _newer_report(repo_path, source: str) -> None:
+ """A report written now, after the ingest :func:`_seed` recorded."""
+ report = repo_path / "coverage" / "lcov.info"
+ report.parent.mkdir()
+ report.write_text(f"SF:{source}\nDA:1,1\nDA:2,0\nend_of_record\n", encoding="utf-8")
+
+
+async def test_loader_rereads_a_report_written_after_the_last_ingest(tmp_path, monkeypatch):
+ from types import SimpleNamespace
+
+ repo_path = tmp_path / "repo"
+ repo_path.mkdir()
+ await _seed(repo_path, [{"file_path": "src/a.py", "line_coverage_pct": 0.0, "total_coverable_lines": 2}])
+ _newer_report(repo_path, "src/a.py")
+
+ async def _metrics(_session, _repo_id):
+ return [SimpleNamespace(file_path="src/a.py")]
+
+ monkeypatch.setattr("repowise.core.persistence.crud.get_health_metrics", _metrics)
+ from repowise.core.analysis.health import coverage
+
+ reads: list = []
+ real_build = coverage.build_coverage_map
+
+ def _counted(*args, **kwargs):
+ reads.append(args[1])
+ return real_build(*args, **kwargs)
+
+ monkeypatch.setattr(coverage, "build_coverage_map", _counted)
+
+ cm = await load_stored_coverage_map(repo_path)
+ assert cm["src/a.py"]["line_coverage_pct"] == 50.0
+ # Stored with a newer ingest, so the next update keeps it without a re-read.
+ again = await load_stored_coverage_map(repo_path)
+ assert again["src/a.py"]["line_coverage_pct"] == 50.0
+ assert len(reads) == 1
+
+
+async def test_loader_keeps_stored_rows_when_reingest_is_off(tmp_path, monkeypatch):
+ repo_path = tmp_path / "repo"
+ repo_path.mkdir()
+ await _seed(repo_path, [{"file_path": "src/a.py", "line_coverage_pct": 0.0, "total_coverable_lines": 2}])
+ _newer_report(repo_path, "src/a.py")
+ monkeypatch.setattr(
+ "repowise.core.repo_config.load_repo_config",
+ lambda _p: {"coverage": {"reingest_on_update": False}},
+ )
+
+ cm = await load_stored_coverage_map(repo_path)
+ assert cm["src/a.py"]["line_coverage_pct"] == 0.0
+
+
+async def test_a_failed_reread_still_loads_the_stored_rows(tmp_path, monkeypatch):
+ from types import SimpleNamespace
+
+ from repowise.core.analysis.health import coverage
+
+ repo_path = tmp_path / "repo"
+ repo_path.mkdir()
+ await _seed(repo_path, [{"file_path": "src/a.py", "line_coverage_pct": 0.0, "total_coverable_lines": 2}])
+ _newer_report(repo_path, "src/a.py")
+
+ async def _metrics(_session, _repo_id):
+ return [SimpleNamespace(file_path="src/a.py")]
+
+ def _broken(*_args, **_kwargs):
+ raise OSError("report vanished")
+
+ monkeypatch.setattr("repowise.core.persistence.crud.get_health_metrics", _metrics)
+ monkeypatch.setattr(coverage, "build_coverage_map", _broken)
+ logged: list[str] = []
+
+ cm = await load_stored_coverage_map(repo_path, log=logged.append)
+ assert cm["src/a.py"]["line_coverage_pct"] == 0.0
+ assert any("Coverage re-read skipped" in line for line in logged)