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
"use client";
/**
* Coverage view, on the section design language.
*
* The shape is: a lede that leads with the coverage percentage and says in
* prose what it is built from, then the health × coverage map, then the files
* where a gap costs something, the module rollup and the per-file table, each
* grouped by a hairline rather than boxed. Rows link to the file page's
* coverage tab (the line-level heatmap).
*
* What it replaces: five `MetricCard`s in a grid above a bordered chart, a
* tinted warning panel, a bordered module list and a collapsed table section.
* Six containers at near-identical weight behind five uppercase labels, which
* is the box-soup failure the section style exists to fix.
*
* Presentation + orchestration only: the host injects data fetching, links,
* and navigation through a {@link CodeHealthAdapter}.
*/
import { useMemo, useState } from "react";
import { ArrowUpRight } from "lucide-react";
import useSWR from "swr";
import { GOOD_MIN } from "@repowise-dev/types/health";
import type {
CoverageFileRow,
HealthCoverageResponse,
HealthFinding,
InferredTestMap,
ReachedFileRow,
} from "@repowise-dev/types/health";
import { Skeleton } from "../ui/skeleton";
import { EmptyState } from "../shared/empty-state";
import { CleanState } from "../shared/clean-state";
import { formatNumber } from "../lib/format";
import { ApiError } from "../shared/api-error";
import { toFriendlyMessage } from "../lib/errors";
import { ResponsiveTable, type ResponsiveColumn } from "../shared/responsive-table";
import { ResultsFooter } from "../shared/results-footer";
import { OverviewSection } from "../overview/section";
import { fileChatContext } from "./ai-prompt-modal";
import { AGENT_PROMPT_LABEL, useAiPromptAction } from "./ai-prompt-action";
import { PromptForRowsButton, RowPromptAction } from "./row-prompt-action";
import { CoverageLede } from "./coverage-lede";
import { CoverageBar } from "./coverage-bar";
import { ModuleCoverageList } from "./module-coverage-list";
import {
UntestedHotspotWarning,
type UntestedHotspotEntry,
} from "./untested-hotspot-warning";
import { RiskCoverageScatter } from "./risk-coverage-scatter";
import { InferredTestsView } from "./inferred-tests-view";
import {
buildCoverageBatchAiPrompt,
type CoverageFilePromptInput,
} from "./ai-prompt-builder";
import { bandForScore, isWeakHealthBand } from "@repowise-dev/types/health";
import { scoreTextColor } from "./tokens";
/** Plain figure; the band colour appears only where health is actually weak. */
function quietScoreText(score: number): string {
return isWeakHealthBand(bandForScore(score))
? scoreTextColor(score)
: "text-[var(--color-text-secondary)]";
}
import { COVERAGE_REPORT_FORMATS_LABEL } from "./coverage-formats";
import { CiHint } from "../shared/ci-hint";
import type { CodeHealthAdapter } from "./code-health-adapter";
/** Copy a host can override: the agent-prompt action and the clean state. */
export interface CoverageViewCopy {
/** The primary row action, handing the file's gaps to an agent. */
promptLabel?: string;
/** The header action over every row a list shows, for `n` files. */
batchPromptLabel?: (count: number) => string;
/** "No untested hotspots across N measured files". */
cleanTitle?: (measuredFiles: number) => string;
cleanDescription?: string;
/** "No test reaches these through the graph": the inferred hero and list. */
unreachedTitle?: string;
/** Shown when the server could not compute which files tests reach. */
unavailableTitle?: string;
unavailableBody?: string;
}
/** The prompt input for one file row. */
function promptInputFor(f: CoverageFileRow): CoverageFilePromptInput {
return {
file_path: f.file_path,
line_coverage_pct: f.line_coverage_pct,
branch_coverage_pct: f.branch_coverage_pct,
total_coverable_lines: f.total_coverable_lines,
...(f.covered_lines && { covered_lines: f.covered_lines }),
source_format: f.source_format,
health_score: f.health_score ?? null,
nloc: f.nloc ?? null,
};
}
export function CoverageView({
adapter,
copy,
}: {
adapter: CodeHealthAdapter;
copy?: CoverageViewCopy | undefined;
}) {
const { openPrompt, promptModal } = useAiPromptAction();
// One file from a row, or every file a list shows, through the shared modal.
const promptFor = (rows: CoverageFilePromptInput[]) => {
const one = rows.length === 1 ? rows[0]!.file_path : undefined;
openPrompt({
title: "AI test prompt",
description:
"A ready-to-paste prompt that asks your AI coding agent to add tests for the uncovered lines and branches.",
filePath: one ?? null,
...(one ? { chatContext: fileChatContext(one)! } : {}),
getPrompt: (flavor) => buildCoverageBatchAiPrompt({ rows, flavor }),
});
};
const { data, isLoading, error, mutate } = useSWR<HealthCoverageResponse>(
`code-health-coverage:${adapter.cacheKey}`,
() => adapter.getCoverage({ limit: 5000 }),
{ revalidateOnFocus: false },
);
// The untested_hotspot findings carry dependents_count / commit_count_90d
// in their details — join them into the warning entries.
const { data: untestedFindings } = useSWR<HealthFinding[]>(
`code-health-untested-findings:${adapter.cacheKey}`,
() =>
adapter
.listFindings({ biomarker_type: "untested_hotspot", limit: 50 })
.catch(() => []),
{ revalidateOnFocus: false },
);
const openFilePage = (path: string) =>
adapter.navigate(`${adapter.fileHref(path)}?tab=coverage`);
return (
<div className="flex flex-col gap-6 sm:gap-8">
{isLoading ? (
<CoverageSkeleton />
) : error ? (
<ApiError
title="Couldn't load coverage data"
message={toFriendlyMessage(error)}
onRetry={() => void mutate()}
/>
) : data?.basis === "unavailable" ? (
// The graph walk failed server-side. Unknown, so never the "no tests
// found" state below.
<ApiError
title={copy?.unavailableTitle ?? "Couldn't work out which files tests reach"}
message={
copy?.unavailableBody ??
"The dependency graph walk failed on the server, so this page cannot say either way. The server log has the error."
}
onRetry={() => void mutate()}
/>
) : data?.basis === "inferred" && data.inferred ? (
// No report was ever ingested, and the graph can answer anyway. This
// branch never renders a measured field: the two bases are separate
// objects in the payload precisely so one cannot leak into the other's
// code path.
<InferredTestsView
data={data}
untestedFindings={untestedFindings ?? []}
onOpenFile={openFilePage}
unreachedTitle={copy?.unreachedTitle}
/>
) : !data || data.summary.file_count === 0 ? (
<NoCoverageState />
) : (
// A report was ingested. `data.inferred` may also be present: when the
// report never mentioned some files, the graph answers for exactly
// those, and a separate section renders it. The measured body and the
// inferred gap stay apart — no percentage is ever derived from the
// inferred map.
<CoverageBody
data={data}
untestedFindings={untestedFindings ?? []}
onOpenFile={openFilePage}
onGeneratePrompt={promptFor}
copy={copy}
/>
)}
{promptModal}
</div>
);
}
function CoverageBody({
data,
untestedFindings,
onOpenFile,
onGeneratePrompt,
copy,
}: {
data: HealthCoverageResponse;
untestedFindings: HealthFinding[];
onOpenFile: (path: string) => void;
/** One file or several; the modal builds a single or a batch prompt. */
onGeneratePrompt: (rows: CoverageFilePromptInput[]) => void;
copy?: CoverageViewCopy | undefined;
}) {
const promptLabel = copy?.promptLabel ?? AGENT_PROMPT_LABEL;
const { summary, files, modules } = data;
// The true count, not the length of what came back. They differ only if the
// rollup was capped, but the lede claims to describe the repo.
const moduleCount = data.modules_total ?? modules.length;
const [search, setSearch] = useState("");
const [sortField, setSortField] = useState<string>("line_coverage_pct");
const [sortOrder, setSortOrder] = useState<"asc" | "desc">("asc");
const PAGE = 50;
const [visible, setVisible] = useState(PAGE);
// Branch coverage is absent for common lcov flows (pytest emits none); only
// surface the card + column when at least one file actually reports it.
const hasBranch = summary.branch_coverage_pct != null ||
files.some((f) => f.branch_coverage_pct != null);
// A file with zero coverable lines (empty __init__, re-export shim) has no
// meaningful percentage — treat it as null so it renders "—" and sinks in
// worst-first sorts instead of masquerading as 0% covered.
const covOf = (f: CoverageFileRow): number | null =>
f.total_coverable_lines > 0 ? f.line_coverage_pct : null;
const num = (v: unknown): number | null =>
typeof v === "number" && Number.isFinite(v) ? v : null;
// Untested hotspots: prefer the real biomarker findings (which carry
// dependents/commit context); fall back to the low-coverage × low-score
// heuristic when the biomarker never fired (e.g. coverage just ingested).
const filesByPath = useMemo(() => new Map(files.map((f) => [f.file_path, f])), [files]);
const untested = useMemo<UntestedHotspotEntry[]>(() => {
const covByPath = filesByPath;
if (untestedFindings.length > 0) {
return untestedFindings.slice(0, 10).map((f) => {
const cov = covByPath.get(f.file_path);
const d = f.details ?? {};
const entry: UntestedHotspotEntry = {
file_path: f.file_path,
line_coverage_pct:
num(d.line_coverage_pct) ?? cov?.line_coverage_pct ?? null,
commit_count_90d: num(d.commit_count_90d),
};
const dependents = num(d.dependents_count);
if (dependents != null) entry.dependents_count = dependents;
if (cov?.health_score != null) entry.health_score = cov.health_score;
return entry;
});
}
return files
.filter(
(f) =>
f.total_coverable_lines > 0 &&
f.line_coverage_pct != null &&
f.line_coverage_pct < 30 &&
(f.health_score == null || f.health_score < GOOD_MIN),
)
.slice(0, 10)
.map((f) => {
const entry: UntestedHotspotEntry = {
file_path: f.file_path,
line_coverage_pct: f.line_coverage_pct,
};
if (f.health_score != null) entry.health_score = f.health_score;
return entry;
});
}, [files, filesByPath, untestedFindings]);
// The untested entries that have a measured row, which is what a prompt needs.
const untestedRows = useMemo(
() => untested.flatMap((e) => filesByPath.get(e.file_path) ?? []),
[untested, filesByPath],
);
const scatterPoints = useMemo(
() =>
files
// Same rule as `covOf`: a file with no coverable lines has no coverage
// percentage. It used to plot at 0% and pile up empty `__init__` files
// against the left edge, exactly where "critical untested" is written.
.filter(
(f) =>
f.health_score != null &&
f.line_coverage_pct != null &&
f.total_coverable_lines > 0,
)
.map((f) => ({
file_path: f.file_path,
health_score: f.health_score!,
line_coverage_pct: f.line_coverage_pct,
nloc: f.nloc ?? 0,
})),
[files],
);
const filteredFiles = useMemo(() => {
if (!search) return files;
const s = search.toLowerCase();
return files.filter((f) => f.file_path.toLowerCase().includes(s));
}, [files, search]);
const sortedFiles = useMemo(() => {
const dir = sortOrder === "asc" ? 1 : -1;
const val = (f: CoverageFileRow): number | string | null => {
switch (sortField) {
case "file_path":
return f.file_path;
case "branch_coverage_pct":
return f.branch_coverage_pct;
case "total_coverable_lines":
return f.total_coverable_lines;
case "health_score":
return f.health_score ?? null;
default:
return covOf(f);
}
};
return [...filteredFiles].sort((a, b) => {
const av = val(a);
const bv = val(b);
// Nulls (no data / no coverable lines) always sink, regardless of order.
if (av == null && bv == null) return 0;
if (av == null) return 1;
if (bv == null) return -1;
if (typeof av === "string" && typeof bv === "string") {
return dir * av.localeCompare(bv);
}
return dir * ((av as number) - (bv as number));
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filteredFiles, sortField, sortOrder]);
const visibleFiles = sortedFiles.slice(0, visible);
const onSort = (key: string) => {
if (key === sortField) {
setSortOrder((o) => (o === "asc" ? "desc" : "asc"));
} else {
setSortField(key);
// Coverage/health default to worst-first (asc); everything else desc.
setSortOrder(
key === "line_coverage_pct" || key === "health_score" ? "asc" : "desc",
);
}
setVisible(PAGE);
};
const columns: (ResponsiveColumn<CoverageFileRow> | null)[] = [
{
key: "file_path",
header: "File",
priority: 1,
sortable: true,
render: (f) => (
<span className="inline-flex items-center gap-1.5 font-mono text-xs text-[var(--color-text-primary)]">
<span className="truncate max-w-[420px]" title={f.file_path}>
{f.file_path}
</span>
<ArrowUpRight className="h-3 w-3 shrink-0 text-[var(--color-text-tertiary)]" />
</span>
),
},
{
key: "line_coverage_pct",
header: "Line coverage",
priority: 1,
sortable: true,
render: (f) => <CoverageBar value={covOf(f)} size="sm" />,
},
hasBranch
? {
key: "branch_coverage_pct",
header: "Branch",
priority: 3,
align: "right",
sortable: true,
render: (f) => (
<span className="tabular-nums text-[var(--color-text-secondary)]">
{f.branch_coverage_pct == null
? "—"
: `${f.branch_coverage_pct.toFixed(0)}%`}
</span>
),
}
: null,
{
key: "total_coverable_lines",
header: "Lines",
priority: 3,
align: "right",
sortable: true,
render: (f) => (
<span className="tabular-nums text-[var(--color-text-tertiary)]">
{f.total_coverable_lines === 0 ? "—" : f.total_coverable_lines}
</span>
),
},
{
key: "health_score",
header: "Code health",
priority: 2,
align: "right",
sortable: true,
render: (f) =>
f.health_score == null ? (
<span className="text-[var(--color-text-tertiary)]">—</span>
) : (
<span className={`text-xs font-medium tabular-nums ${quietScoreText(f.health_score)}`}>
{f.health_score.toFixed(1)}
</span>
),
},
{
key: "actions",
header: "",
priority: 1,
align: "right",
render: (f) => (
<RowPromptAction
label={promptLabel}
subject={f.file_path}
onClick={() => onGeneratePrompt([promptInputFor(f)])}
/>
),
},
];
const activeColumns = columns.filter(
(c): c is ResponsiveColumn<CoverageFileRow> => c !== null,
);
const fetchTruncated = summary.file_count > files.length;
return (
<div className="flex flex-col gap-6 sm:gap-8">
<CoverageLede
summary={summary}
files={files}
moduleCount={moduleCount}
history={data.history}
/>
<OverviewSection
title="Health against coverage"
description="Every instrumented file placed by its 0–10 defect-health score (higher is healthier) and its line coverage, sized by lines of code. The bottom-left quadrant is the one that costs money: code we score as weak, with no test watching it. Click a file to open its line-level heatmap."
>
<RiskCoverageScatter
points={scatterPoints}
onSelect={(p) => onOpenFile(p.file_path)}
/>
</OverviewSection>
<OverviewSection
title="Untested hotspots"
description="Files that change often or that much of the codebase depends on, with little or no coverage. A gap here is worth more than the same gap in code nobody touches."
action={
<PromptForRowsButton
count={untestedRows.length}
onClick={() => onGeneratePrompt(untestedRows.map(promptInputFor))}
{...(copy?.batchPromptLabel ? { label: copy.batchPromptLabel } : {})}
/>
}
>
{untested.length > 0 ? (
<UntestedHotspotWarning
entries={untested}
onSelect={onOpenFile}
onGeneratePrompt={(path) => {
const row = filesByPath.get(path);
if (row) onGeneratePrompt([promptInputFor(row)]);
}}
promptLabel={promptLabel}
/>
) : (
<CleanState
align="start"
title={
copy?.cleanTitle?.(summary.file_count) ??
`No untested hotspots across ${formatNumber(summary.file_count)} measured files`
}
description={
copy?.cleanDescription ??
"Every file that changes often or that much of the code depends on has tests watching it."
}
/>
)}
</OverviewSection>
<OverviewSection
title="Module coverage"
description="Directories rolled up under their top-level package, weighted by coverable lines and worst covered first. A directory with no coverable lines reads as no data rather than as a red zero."
>
<ModuleCoverageList modules={modules} />
</OverviewSection>
<OverviewSection
title="Every file"
description="Sorted worst first. Click a row for the line-level heatmap, or copy a prompt that has your agent write the missing tests."
action={
<div className="flex flex-wrap items-center gap-2">
<PromptForRowsButton
count={sortedFiles.length}
onClick={() => onGeneratePrompt(sortedFiles.map(promptInputFor))}
{...(copy?.batchPromptLabel ? { label: copy.batchPromptLabel } : {})}
/>
<input
value={search}
onChange={(e) => {
setSearch(e.target.value);
setVisible(PAGE);
}}
placeholder="Filter path…"
aria-label="Filter files by path"
className="w-48 rounded-md border border-[var(--color-border-default)] bg-[var(--color-bg-surface)] px-2 py-1.5 text-xs focus:border-[var(--color-border-hover)] focus:outline-none sm:w-56"
/>
</div>
}
>
<div className="border-t border-[var(--color-border-default)]">
<ResponsiveTable
columns={activeColumns}
rows={visibleFiles}
rowKey={(f) => f.file_path}
onRowClick={(f) => onOpenFile(f.file_path)}
sortField={sortField}
sortOrder={sortOrder}
onSort={onSort}
stacked="sm"
bare
empty={
search ? (
<EmptyState
tone="filtered"
title="No files match"
description="Adjust the path filter to see coverage rows."
action={{
label: "Clear filters",
onClick: () => {
setSearch("");
setVisible(PAGE);
},
}}
/>
) : (
<EmptyState size="compact" title="No files to list" />
)
}
/>
{sortedFiles.length > 0 ? (
<ResultsFooter
shown={visibleFiles.length}
total={sortedFiles.length}
hasMore={visible < sortedFiles.length}
onLoadMore={() => setVisible((v) => v + PAGE)}
noun="files"
/>
) : null}
</div>
{fetchTruncated ? (
<p className="text-xs text-[var(--color-text-tertiary)]">
Showing {files.length.toLocaleString()} of{" "}
{summary.file_count.toLocaleString()} instrumented files, capped so one
tab does not pull the whole repository.
</p>
) : null}
</OverviewSection>
{data.inferred ? <CoverageGap data={data} onOpenFile={onOpenFile} /> : null}
</div>
);
}
/**
* The files the ingested coverage report never mentioned, answered by the graph.
*
* This is the hybrid shape: a report exists (so `basis` is `measured` and the
* measured body above renders it), but the report's lcov source covered only a
* subset of the repo. Every other file was invisible on the Tests tab — not
* unindexed and not untested, just never named by the report. The graph knows
* whether a test reaches those files, and this section says that.
*
* It honours the same rules as the pure-inferred view: counts only, no bar and
* no percentage (reaching is a file-level fact with no line attribution), and
* no health-band colour. `files_total` here is the count of non-measured files,
* stated beside `measured_file_count` so the split is explicit.
*/
function CoverageGap({
data,
onOpenFile,
}: {
data: HealthCoverageResponse;
onOpenFile: (path: string) => void;
}) {
const map = data.inferred as InferredTestMap;
const measured = map.measured_file_count ?? 0;
const unreached = map.files.filter((f) => !f.reached);
const paths = data.summary.report_paths;
const missed = paths ? paths.unmatched + paths.ambiguous : 0;
return (
<OverviewSection
title="Files the report didn't cover"
description={
map.files_total > 0
? `Your coverage report named ${measured.toLocaleString()} files. The dependency graph answers for the other ${map.files_total.toLocaleString()}: ${map.files_reached.toLocaleString()} are reached by a test, and it finds no test for ${map.files_not_reached.toLocaleString()}. Treat that as a floor, not a measurement: it misses code a framework invokes, such as routes, CLI commands and plugin registries.`
: "The dependency graph could not answer for the files the coverage report left out."
}
>
{/* Why a file can be missing here: the report named it under a path
that did not map to this repository. */}
{paths && paths.total > 0 ? (
<p className="text-xs text-[var(--color-text-tertiary)]">
<span className="tabular-nums">
{paths.matched.toLocaleString()} of {paths.total.toLocaleString()}
</span>{" "}
report paths matched a file in this repository
{missed > 0 ? (
<>
; {missed.toLocaleString()} did not
{paths.unmatched_sample[0] ? (
<>
{" "}(for example{" "}
<span className="font-mono">{paths.unmatched_sample[0]}</span>). Set{" "}
<span className="font-mono">coverage.strip_prefix</span> or{" "}
<span className="font-mono">coverage.path_prefix</span> if the
paths are off
</>
) : null}
</>
) : null}
.
</p>
) : null}
<div className="border-t border-[var(--color-border-default)]">
<ResponsiveTable
columns={gapColumns}
rows={unreached.slice(0, 50)}
rowKey={(f) => f.file_path}
onRowClick={(f) => onOpenFile(f.file_path)}
stacked="sm"
bare
empty={
map.files_total > 0 ? (
<EmptyState
tone="positive"
title="Every file is reached"
description="The graph found a test that reaches every file the coverage report didn't name."
/>
) : (
<EmptyState size="compact" title="No other files to check" />
)
}
/>
</div>
</OverviewSection>
);
}
const gapColumns: ResponsiveColumn<ReachedFileRow>[] = [
{
key: "file_path",
header: "File",
priority: 1,
render: (f) => (
<span
className="block truncate font-mono text-xs text-[var(--color-text-primary)]"
title={f.file_path}
>
{f.file_path}
</span>
),
},
{
key: "reached",
header: "Reached",
priority: 1,
render: (f) => (
<span className="text-xs text-[var(--color-text-secondary)]">
{f.reached ? "yes" : "no"}
</span>
),
},
{
key: "nloc",
header: "Lines",
priority: 3,
align: "right",
render: (f) => (
<span className="tabular-nums text-[var(--color-text-tertiary)]">
{f.nloc ?? "—"}
</span>
),
},
{
key: "health_score",
header: "Code health",
priority: 2,
align: "right",
render: (f) =>
f.health_score == null ? (
<span className="text-[var(--color-text-tertiary)]">—</span>
) : (
<span className={`text-xs font-medium tabular-nums ${quietScoreText(f.health_score)}`}>
{f.health_score.toFixed(1)}
</span>
),
},
];
/**
* The last resort: no report, and the graph had nothing to say either — an
* unindexed repository, or one with no test files at all. When the graph *can*
* answer, `InferredTestsView` renders instead and the reader never reaches here.
*
* It used to be the only thing behind "no coverage", and it claimed "Nothing is
* inferred: we read the lines your tests really executed" — which stopped being
* true once the graph could answer, and turned a solved question into a wall.
* What is left is the honest version: this is genuinely unknown, and here is
* what would fill it.
*/
function NoCoverageState() {
return (
<div className="flex max-w-[62ch] flex-col gap-3">
<h2 className="text-base font-semibold text-[var(--color-text-primary)]">
Nothing here can say whether your code is tested
</h2>
<p className="text-[15px] leading-relaxed text-[var(--color-text-secondary)] [text-wrap:pretty]">
No coverage report has been ingested, and the dependency graph found no
test files to trace either. Either would fill this page: a report gives the
lines your tests executed, and the graph alone can name which tests reach
which files with no setup at all. Run your suite with coverage on and hand
us the report.
</p>
<pre className="w-fit overflow-x-auto rounded-md bg-[var(--color-bg-inset)] px-3 py-2 font-mono text-xs text-[var(--color-text-primary)]">
pytest --cov --cov-report=lcov{"\n"}
repowise coverage add coverage.lcov
</pre>
<p className="font-mono text-[10px] uppercase tracking-[0.12em] text-[var(--color-text-tertiary)]">
{COVERAGE_REPORT_FORMATS_LABEL}
</p>
<CiHint command="repowise coverage check" checks="the lines each change touched" />
</div>
);
}
/** Shapes and widths match the real layout, so nothing reflows when data lands. */
function CoverageSkeleton() {
return (
<div className="flex flex-col gap-6 sm:gap-8">
<div className="flex flex-col gap-5 lg:flex-row lg:gap-12">
<Skeleton className="h-[92px] w-full rounded-lg lg:w-[220px]" />
<Skeleton className="h-[92px] w-full max-w-[62ch] rounded-lg" />
</div>
<Skeleton className="h-[74px] w-full" />
<Skeleton className="h-[340px] w-full rounded-lg" />
<Skeleton className="h-48 w-full rounded-lg" />
</div>
);
}