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): skip the file page line tint when the file changed after coverage was measured (#4336)
Raghav Chamadiyacf3be03f6a6a
diff --git a/packages/core/src/repowise/core/analysis/git_cli.py b/packages/core/src/repowise/core/analysis/git_cli.pyindex 3b4d16cca..926446002 100644--- a/packages/core/src/repowise/core/analysis/git_cli.py+++ b/packages/core/src/repowise/core/analysis/git_cli.py@@ -104,7 +104,12 @@ def _git(args: list[str], cwd: str, *, check: bool = True) -> str:return proc.stdout-def untracked_paths(repo_path: str, exclude_patterns: tuple[str, ...] = ()) -> list[str]:+def untracked_paths(+ repo_path: str,+ exclude_patterns: tuple[str, ...] = (),+ *,+ pathspecs: Sequence[str] = (":/",),+) -> list[str]:"""Untracked, not-ignored files, root-relative like ``git diff``'s paths.The one source of untracked files for every working-tree reader (the score,@@ -112,9 +117,10 @@ def untracked_paths(repo_path: str, exclude_patterns: tuple[str, ...] = ()) -> lrepowise writes whole are left out here, before anything reads them: anunignored index directory holds thousands of files. *exclude_patterns*(gitignore-style) drops a caller's excluded paths at the same point.+ *pathspecs* narrows the listing (the whole repo by default)."""listing = _git(- ["ls-files", "--others", "--exclude-standard", "-z", "--full-name", "--", ":/"],+ ["ls-files", "--others", "--exclude-standard", "-z", "--full-name", "--", *pathspecs],repo_path,check=False,)diff --git a/packages/core/src/repowise/core/analysis/patch_coverage/__init__.py b/packages/core/src/repowise/core/analysis/patch_coverage/__init__.pyindex 9f6f8e6a0..7d3d57969 100644--- a/packages/core/src/repowise/core/analysis/patch_coverage/__init__.py+++ b/packages/core/src/repowise/core/analysis/patch_coverage/__init__.py@@ -64,6 +64,7 @@ from .risk import ()from .stored import (attach_history_delta,+ file_on_disk_freshness,history_delta,ingest_scope,ingest_totals,@@ -100,6 +101,7 @@ __all__ = ["build_hints","cause_words","compute_patch_coverage",+ "file_on_disk_freshness","first_hint","fmt_pct","format_ranges",diff --git a/packages/core/src/repowise/core/analysis/patch_coverage/stored.py b/packages/core/src/repowise/core/analysis/patch_coverage/stored.pyindex 89a47e846..98fb8c8e8 100644--- a/packages/core/src/repowise/core/analysis/patch_coverage/stored.py+++ b/packages/core/src/repowise/core/analysis/patch_coverage/stored.py@@ -10,7 +10,7 @@ from __future__ import annotationsimport asyncioimport jsonimport subprocess-from collections.abc import Iterable, Mapping+from collections.abc import Iterable, Mapping, Sequencefrom dataclasses import replacefrom typing import TYPE_CHECKING, Any@@ -18,7 +18,11 @@ from sqlalchemy.ext.asyncio import AsyncSessionfrom ..git_cli import _git, untracked_pathsfrom ..health.coverage.discovery import CoverageScope-from ..health.coverage.freshness import coverage_freshness, working_tree_freshness+from ..health.coverage.freshness import (+ FreshnessStatus,+ coverage_freshness,+ working_tree_freshness,+)from .compute import BranchData, PatchCoverage, PatchScope, compute_patch_coveragefrom .delta import ProjectDelta, ProjectTotals, incomparable_reasonsfrom .risk import IndexFacts@@ -119,19 +123,50 @@ async def _stale_files(return files if since is None else files & since-def _changed_since(repo_path: str, since: str, until: str | None) -> set[str] | None:+async def file_on_disk_freshness(+ repo_path: str | None, measured_commit: str | None, file_path: str+) -> FreshnessStatus:+ """Whether coverage measured at *measured_commit* still lines up with *file_path* on disk.++ The ``stale_file`` rule for one file read from the working tree: ``stale``+ when git shows it differs from the measured commit or is untracked,+ ``unknown`` when git cannot tell (no *repo_path*, no measured commit, a+ commit this clone lacks). *file_path* is relative to *repo_path*.+ """+ if not repo_path or not measured_commit:+ return "unknown"+ since = await asyncio.to_thread(+ _changed_since, repo_path, measured_commit, None, (file_path,)+ )+ if since is None:+ return "unknown"+ return "stale" if file_path in since else "current"+++def _changed_since(+ repo_path: str, since: str, until: str | None, paths: Sequence[str] = ()+) -> set[str] | None:"""Files that differ between *since* and *until* (the working tree when ``None``).- ``None`` when git cannot tell. Two dots: any content difference moves the- report's line numbers, whichever side made it.+ Relative to *repo_path*, like the stored coverage rows, even when it sits+ below the git top level; limited to *paths* when given, so one file costs+ one file. ``None`` when git cannot tell. Two dots: any content difference+ moves the report's line numbers, whichever side made it."""tail = [until] if until else []+ spec = [f":(literal){p}" for p in paths]try:- listing = _git(["diff", "--name-only", "-z", "--no-renames", since, *tail], repo_path)+ prefix = _git(["rev-parse", "--show-prefix"], repo_path).strip()+ listing = _git(+ ["diff", "--name-only", "-z", "--no-renames", since, *tail, "--", *spec], repo_path+ )except (subprocess.SubprocessError, OSError):return None- paths = {p for p in listing.split("\0") if p}- return paths | set(untracked_paths(repo_path)) if until is None else paths+ # git names paths from its top level; the prefix is where *repo_path* sits.+ changed = {p for p in listing.split("\0") if p}+ if until is None:+ changed |= set(untracked_paths(repo_path, pathspecs=spec or (":/",)))+ return {p[len(prefix) :] for p in changed if p.startswith(prefix)}async def _stored_branch_data(diff --git a/packages/server/src/repowise/server/routers/files.py b/packages/server/src/repowise/server/routers/files.pyindex 3af7209a2..8140355eb 100644--- a/packages/server/src/repowise/server/routers/files.py+++ b/packages/server/src/repowise/server/routers/files.py@@ -23,6 +23,7 @@ from repowise.core.analysis.finding_registry import excluded_types, split_gatedfrom repowise.core.analysis.health.aggregation import NLOC_NULL_REASONfrom repowise.core.analysis.health.signals import file_signalsfrom repowise.core.analysis.health.trends import file_trend+from repowise.core.analysis.patch_coverage import file_on_disk_freshnessfrom repowise.core.attribution import attribution_enabledfrom repowise.core.ids import is_externalfrom repowise.core.ingestion.models import FILE_DEPENDENCY_EDGE_TYPES@@ -365,6 +366,11 @@ async def file_detail(if coverage_rows:coverage = crud.coverage_row_dict(coverage_rows[0], include_covered_lines=not slim)coverage.setdefault("covered_lines", [])+ # The file page shows the source on disk; covered line numbers from+ # another version of it would tint the wrong lines.+ coverage["freshness"] = await file_on_disk_freshness(+ repo.local_path, coverage["ingested_commit_sha"], file_path+ )# --- Graph context ------------------------------------------------------graph: dict | None = Nonediff --git a/packages/types/src/files.ts b/packages/types/src/files.tsindex 9432f6412..f16c3b7d4 100644--- a/packages/types/src/files.ts+++ b/packages/types/src/files.ts@@ -156,6 +156,13 @@ export interface FileDetailCoverage {source_format: string;ingested_at: string | null;ingested_commit_sha: string | null;+ /**+ * Whether `covered_lines` still lines up with the file on disk: `stale` when+ * git shows the file changed since `ingested_commit_sha`, `unknown` when it+ * cannot tell. Only `stale` withholds the line tint. Optional so older+ * payloads parse.+ */+ freshness?: "current" | "stale" | "unknown";}export interface FileGraphNeighbor {diff --git a/packages/ui/__tests__/files/file-coverage-tab.test.tsx b/packages/ui/__tests__/files/file-coverage-tab.test.tsxnew file mode 100644index 000000000..0ccc4e52a--- /dev/null+++ b/packages/ui/__tests__/files/file-coverage-tab.test.tsx@@ -0,0 +1,51 @@+import { describe, it, expect } from "vitest";+import { render, screen } from "@testing-library/react";+import { FileCoverageTab } from "../../src/files/file-coverage-tab.js";+import type { FileDetailCoverage } from "@repowise-dev/types/files";++const HTML = '<pre><code><span class="line" data-covered="y" data-line="1">x = 1</span></code></pre>';++function makeCoverage(overrides: Partial<FileDetailCoverage> = {}): FileDetailCoverage {+ return {+ line_coverage_pct: 75,+ branch_coverage_pct: null,+ total_coverable_lines: 4,+ covered_line_count: 3,+ covered_lines: [1, 2, 3],+ source_format: "lcov",+ ingested_at: null,+ ingested_commit_sha: "abcdef1234567",+ ...overrides,+ };+}++describe("FileCoverageTab", () => {+ it("states measured coverage as lines the tests ran, without claiming exactness", () => {+ const { container } = render(<FileCoverageTab coverage={makeCoverage()} />);+ const text = container.textContent ?? "";+ expect(text).toContain("Tests ran");+ expect(text).toContain("that no test executed");+ expect(text).not.toContain("Tests reach");+ expect(text).not.toContain("exact");+ });++ it("tints the source when the coverage is current", () => {+ const { container } = render(+ <FileCoverageTab coverage={makeCoverage({ freshness: "current" })} coverageCodeHtml={HTML} />,+ );+ expect(container.querySelector('[data-covered="y"]')).not.toBeNull();+ expect(screen.queryByText(/changed after coverage was measured/)).toBeNull();+ });++ it("shows no tint and says why when the file changed after the run", () => {+ const { container } = render(+ <FileCoverageTab coverage={makeCoverage({ freshness: "stale" })} coverageCodeHtml={HTML} />,+ );+ expect(container.querySelector("[data-covered]")).toBeNull();+ expect(+ screen.getByText(+ "This file changed after coverage was measured; re-run tests with coverage to refresh.",+ ),+ ).toBeTruthy();+ });+});diff --git a/packages/ui/src/files/file-coverage-tab.tsx b/packages/ui/src/files/file-coverage-tab.tsxindex 043434c9e..aeddac2ea 100644--- a/packages/ui/src/files/file-coverage-tab.tsx+++ b/packages/ui/src/files/file-coverage-tab.tsx@@ -65,6 +65,10 @@ export function FileCoverageTab({const band = coverageBand(pct);const coveredCount = coverage.covered_line_count ?? coverage.covered_lines.length;const uncovered = Math.max(0, coverage.total_coverable_lines - coveredCount);+ // Covered line numbers from an earlier version of the file would tint the+ // wrong lines, so a stale file shows no tinted source at all.+ const stale = coverage.freshness === "stale";+ const codeHtml = stale ? undefined : coverageCodeHtml;return (<div>@@ -95,19 +99,18 @@ export function FileCoverageTab({}><p>- Tests reach{" "}+ Tests ran{" "}<Fig>{formatNumber(coveredCount)} of {formatNumber(coverage.total_coverable_lines)}{" "}coverable lines</Fig>- , leaving <Fig>{formatNumber(uncovered)}</Fig> with nothing executing them+ , leaving <Fig>{formatNumber(uncovered)}</Fig> that no test executed{coverage.branch_coverage_pct != null && (<>. Branch coverage is <Fig>{coverage.branch_coverage_pct.toFixed(1)}%</Fig></>)}- . These are the lines your own test run executed, so this figure is- line-level and exact.+ . Measured by your own test run, not inferred from the dependency graph.</p><p className="mt-2.5">Read from a{" "}@@ -134,12 +137,14 @@ export function FileCoverageTab({<FileSectiontitle="Source"description={- coverageCodeHtml+ codeHtml? "Every coverable line, tinted by whether the last report saw it run."- : "The highlighted source appears here for files under the render cap once a coverage report has been ingested for them."+ : stale+ ? "This file changed after coverage was measured; re-run tests with coverage to refresh."+ : "The highlighted source appears here for files under the render cap once a coverage report has been ingested for them."}>- {coverageCodeHtml ? (+ {codeHtml ? (<divdata-chat-selection=""{...(filePath ? { "data-chat-selection-path": filePath } : {})}@@ -147,7 +152,7 @@ export function FileCoverageTab({>{/* Highlighted by the host through the shared shiki path; thedata-covered line attributes drive the gutter tint below. */}- <div dangerouslySetInnerHTML={{ __html: coverageCodeHtml /* shiki output; repowise-security-ignore: unsafe_inner_html */ }} />+ <div dangerouslySetInnerHTML={{ __html: codeHtml /* shiki output; repowise-security-ignore: unsafe_inner_html */ }} /><style>{`.coverage-code pre { margin: 0; padding: 0.75rem 0; background: transparent !important; }.coverage-code code { display: block; }@@ -158,7 +163,9 @@ export function FileCoverageTab({</div>) : (<p className="text-sm text-[var(--color-text-tertiary)]">- Source preview unavailable for this file.+ {stale+ ? "Source preview hidden until coverage is measured again."+ : "Source preview unavailable for this file."}</p>)}</FileSection>diff --git a/packages/ui/src/health/coverage-lede.tsx b/packages/ui/src/health/coverage-lede.tsxindex 6af68a5d3..819d8cbd6 100644--- a/packages/ui/src/health/coverage-lede.tsx+++ b/packages/ui/src/health/coverage-lede.tsx@@ -83,7 +83,7 @@ export function CoverageLede({}><p>- Tests reach{" "}+ Tests ran{" "}<strong className="font-semibold text-[var(--color-text-primary)]">{formatNumber(summary.covered_lines)} of {formatNumber(summary.total_lines)}{" "}coverable lines@@ -91,9 +91,9 @@ export function CoverageLede({across {formatNumber(summary.file_count)} instrumented files{moduleCount > 0 ? ` in ${formatNumber(moduleCount)} directories` : ""}.{band ? ` We rate that ${band.label.toLowerCase()}.` : ""} These are the- lines your own test run executed, so every figure on this page is- line-level and exact. Without a report we can still name which tests- reach which files, but not how much of a file they run.+ lines your own test run executed, measured rather than inferred. Without+ a report we can still name which tests reach which files, but not how+ much of a file they run.</p><p className="mt-2.5">diff --git a/packages/web/src/app/repos/[id]/files/[...path]/page.tsx b/packages/web/src/app/repos/[id]/files/[...path]/page.tsxindex 1e52c0879..a16292e1c 100644--- a/packages/web/src/app/repos/[id]/files/[...path]/page.tsx+++ b/packages/web/src/app/repos/[id]/files/[...path]/page.tsx@@ -69,6 +69,9 @@ async function renderCoverageCode(): Promise<string | undefined> {if (!content) return undefined;if (!detail.coverage || detail.coverage.covered_lines.length === 0) return undefined;+ // Line numbers measured on another version of the file would tint the+ // wrong lines; the tab says why the source is untinted instead.+ if (detail.coverage.freshness === "stale") return undefined;if (content.length > MAX_HIGHLIGHTED_BYTES) return undefined;// Resolve the grammar before highlighting rather than catching a throw and@@ -161,7 +164,9 @@ export default async function FileEntityPage({ params, searchParams }: Props) {}const covered = detail.coverage;const highlightPossible =- !!covered && (covered.covered_line_count ?? covered.covered_lines.length) > 0;+ !!covered &&+ covered.freshness !== "stale" &&+ (covered.covered_line_count ?? covered.covered_lines.length) > 0;// Gated on not already being here. Land on `?tab=coverage` for a file over// the highlight size cap and `coverageCodeHtml` is undefined for good — so// without this, every click on the tab you are already on re-ran the wholediff --git a/tests/unit/persistence/test_coverage_loaders.py b/tests/unit/persistence/test_coverage_loaders.pyindex aa2800d30..122699b68 100644--- a/tests/unit/persistence/test_coverage_loaders.py+++ b/tests/unit/persistence/test_coverage_loaders.py@@ -218,6 +218,45 @@ async def test_stale_coverage_keeps_figures_only_for_files_untouched_since(assert working.patch_coverage_pct is None+async def test_stale_files_resolve_against_a_checkout_below_the_git_top_level(+ async_session, repo, tmp_path+) -> None:+ from repowise.core.analysis.patch_coverage import stored_patch_coverage++ # The checkout is ``svc/`` inside a larger repository; its coverage rows+ # are named from ``svc/``, while git names paths from the top level.+ top = tmp_path / "mono"+ for rel in ("svc/src/a.py", "svc/src/b.py", "src/b.py"):+ (top / rel).parent.mkdir(parents=True, exist_ok=True)+ (top / rel).write_text("x = 1\ny = 2\n", encoding="utf-8")+ _git(top, "init", "-q")+ _git(top, "config", "user.email", "[email protected]")+ _git(top, "config", "user.name", "t")+ _git(top, "add", ".")+ _git(top, "commit", "-qm", "measured")+ measured = _git(top, "rev-parse", "HEAD")+ # svc/src/a.py changes; so does a same-named file outside the checkout.+ for rel in ("svc/src/a.py", "src/b.py"):+ (top / rel).write_text("x = 1\ny = 3\n", encoding="utf-8")+ _git(top, "commit", "-qam", "head")+ head = _git(top, "rev-parse", "HEAD")+ files = [file_coverage("src/a.py", [1], [1, 2]), file_coverage("src/b.py", [1], [1, 2])]+ await save_coverage_files(+ async_session, repo.id, files, source_format="lcov", ingested_commit_sha=measured+ )++ stored = await stored_patch_coverage(+ async_session, repo.id, {"src/a.py": {2}, "src/b.py": {2}}, head_commit=head,+ repo_path=str(top / "svc"),+ )++ assert stored is not None+ assert [(f.file_path, f.status) for f in stored.files] == [+ ("src/a.py", "stale_file"),+ ("src/b.py", "measured"),+ ]++async def test_stored_patch_coverage_is_none_without_coverage(async_session, repo) -> None:from repowise.core.analysis.patch_coverage import stored_patch_coveragediff --git a/tests/unit/server/test_file_detail_coverage_freshness.py b/tests/unit/server/test_file_detail_coverage_freshness.pynew file mode 100644index 000000000..8c3214f4c--- /dev/null+++ b/tests/unit/server/test_file_detail_coverage_freshness.py@@ -0,0 +1,131 @@+"""File detail ``coverage.freshness``: do the stored covered lines still fit the file on disk?++The file page paints ``covered_lines`` onto the source it reads from disk, so+a file that changed after the measured commit must say ``stale``.+"""++from __future__ import annotations++import subprocess+from pathlib import Path++import pytest+from httpx import AsyncClient++from repowise.core.analysis.patch_coverage import file_on_disk_freshness+from repowise.core.persistence.crud import save_coverage_files, save_health_metrics+from tests.unit.server.conftest import create_test_repo++_PATH = "src/a.py"+++def _git(args: list[str], cwd: Path) -> str:+ return subprocess.run(+ ["git", *args], cwd=cwd, check=True, capture_output=True, text=True+ ).stdout.strip()+++def _commit(repo: Path, content: str, message: str) -> str:+ (repo / _PATH).parent.mkdir(parents=True, exist_ok=True)+ (repo / _PATH).write_text(content, encoding="utf-8")+ _git(["add", _PATH], repo)+ _git(["-c", "user.name=Dev", "-c", "[email protected]", "commit", "-q", "-m", message], repo)+ return _git(["rev-parse", "HEAD"], repo)+++def git_repo(tmp_path: Path) -> Path:+ repo = tmp_path / "test-repo"+ repo.mkdir()+ _git(["init", "-q"], repo)+ return repo+++async def _detail(client: AsyncClient, session, tmp_path: Path, commit: str | None) -> dict:+ repo = await create_test_repo(client, tmp_path)+ files = [+ {+ "file_path": _PATH,+ "line_coverage_pct": 50.0,+ "covered_lines": [1],+ "total_coverable_lines": 2,+ "branch_coverage_pct": None,+ }+ ]+ await save_coverage_files(+ session, repo["id"], files, source_format="lcov", ingested_commit_sha=commit+ )+ await save_health_metrics(+ session,+ repo["id"],+ [{"file_path": _PATH, "score": 9.0, "max_ccn": 1, "max_nesting": 1, "nloc": 2}],+ )+ await session.commit()+ resp = await client.get(f"/api/repos/{repo['id']}/files/{_PATH}")+ assert resp.status_code == 200+ return resp.json()["coverage"]+++async def test_current_when_file_unchanged_since_measured_commit(+ client: AsyncClient, session, git_repo: Path, tmp_path: Path+) -> None:+ measured = _commit(git_repo, "x = 1\ny = 2\n", "feat: add a")+ coverage = await _detail(client, session, tmp_path, measured)+ assert coverage["freshness"] == "current"+ assert coverage["covered_lines"] == [1]+++async def test_stale_when_file_committed_after_measured_commit(+ client: AsyncClient, session, git_repo: Path, tmp_path: Path+) -> None:+ measured = _commit(git_repo, "x = 1\ny = 2\n", "feat: add a")+ _commit(git_repo, "w = 0\nx = 1\ny = 2\n", "feat: grow a")+ coverage = await _detail(client, session, tmp_path, measured)+ assert coverage["freshness"] == "stale"+++async def test_stale_when_file_edited_on_disk_after_measured_commit(+ client: AsyncClient, session, git_repo: Path, tmp_path: Path+) -> None:+ measured = _commit(git_repo, "x = 1\ny = 2\n", "feat: add a")+ (git_repo / _PATH).write_text("w = 0\nx = 1\ny = 2\n", encoding="utf-8")+ coverage = await _detail(client, session, tmp_path, measured)+ assert coverage["freshness"] == "stale"+++async def test_unknown_without_a_measured_commit(+ client: AsyncClient, session, git_repo: Path, tmp_path: Path+) -> None:+ _commit(git_repo, "x = 1\ny = 2\n", "feat: add a")+ coverage = await _detail(client, session, tmp_path, None)+ assert coverage["freshness"] == "unknown"+++async def test_unknown_for_a_commit_the_clone_lacks(+ client: AsyncClient, session, git_repo: Path, tmp_path: Path+) -> None:+ _commit(git_repo, "x = 1\ny = 2\n", "feat: add a")+ coverage = await _detail(client, session, tmp_path, "0" * 40)+ assert coverage["freshness"] == "unknown"+++async def test_paths_resolve_against_a_checkout_below_the_git_top_level(tmp_path: Path) -> None:+ top = tmp_path / "mono"+ (top / "svc").mkdir(parents=True)+ _git(["init", "-q"], top)+ (top / "svc" / "a.py").write_text("x = 1\n", encoding="utf-8")+ _git(["add", "svc/a.py"], top)+ _git(["-c", "user.name=Dev", "-c", "[email protected]", "commit", "-q", "-m", "seed"], top)+ measured = _git(["rev-parse", "HEAD"], top)+ sub = str(top / "svc")+ assert await file_on_disk_freshness(sub, measured, "a.py") == "current"+ (top / "svc" / "a.py").write_text("w = 0\nx = 1\n", encoding="utf-8")+ assert await file_on_disk_freshness(sub, measured, "a.py") == "stale"+ (top / "svc" / "b.py").write_text("new\n", encoding="utf-8")+ assert await file_on_disk_freshness(sub, measured, "b.py") == "stale"