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
/**
* The Code Health page's opening read: one figure, and the sentences that make
* it mean something.
*
* One rather than two. It carried Code health and Maintainability side by side,
* both scores out of ten, both largely about code shape — so the screen
* answered "which number do I steer by?" twice, with two different numbers. The
* page's Counts control answers it instead: the single figure either includes
* change history or does not, and the reader picks. Maintainability keeps its
* place in the ribbon, beside the other cuts of the same scoring.
*
* Every figure carries an explainer. A number a reader cannot define is a
* number they cannot act on, and "Performance, fix now 4" beside "Hotspot health
* 4.5" reads as two scores when one is a count.
*
* The prose is not decoration. "329 risks" reads as alarming on its own;
* "329 static performance risks, scored separately and never blended into the
* health number" reads as informative. The accuracy claim in particular only
* means anything next to its base rate: a 72% hit rate is excellent against a
* 20% baseline and unremarkable against a 70% one, so it is a sentence rather
* than a badge.
*/
import {
bandForScore,
formatScore,
type DefectAccuracy,
type HealthDistribution,
type HealthOverviewSummary,
type PerformanceCauses,
} from "@repowise-dev/types/health";
import { PageLede } from "../shared/page-lede";
import { StatRibbon, type RibbonStat } from "../stats/stat-ribbon";
import { formatNumber } from "../lib/format";
import { healthBand, healthBandColor, scoreTextColor } from "./tokens";
import { HealthDistributionBar } from "./health-distribution-bar";
import { HEALTH_UNSUPPORTED_NOTICE } from "./map/lens";
import { Info } from "lucide-react";
/** The lede's two quiet toggles: "Breakdown" under the figure, "How it is scored" under the prose. */
const DISCLOSURE =
"inline-flex cursor-pointer list-none items-center gap-1 rounded text-[11px] font-medium text-[var(--color-text-tertiary)] hover:text-[var(--color-text-secondary)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-focus-ring)] [&::-webkit-details-marker]:hidden";
const HEALTH_HINT =
"Fitted against real bug history to predict where defects appear. Built from " +
"code shape — complexity, duplication, coverage — and from what git says about " +
"each file: how often it changes, alongside what, and how many people touch it. " +
"1 to 10, higher is better.";
const CODE_SHAPE_HINT =
"The same score with its change-history half removed: complexity, " +
"duplication and coverage, but not churn, co-change, ownership or prior " +
"fixes. It answers what the code is like rather than what the repository " +
"has been through, so it moves when you refactor. Not the calibrated " +
"bug-risk figure — the badge and the leaderboard keep reporting that one.";
const FILES_HINT =
"Files scored. Only code is scored, so markdown, JSON, YAML, lockfiles and " +
"other non-code carry no score and are not counted here.";
const MAINTAINABILITY_HINT =
"Code shape alone: complexity, duplication and error handling, weighted for " +
"how hard the code is to work with rather than for bug risk. It counts no " +
"tests and no change history, so it is the figure a refactor moves.";
/** The performance stat's explainer, with the counts that give "now" its context. */
function performanceHint(causes: PerformanceCauses | null | undefined): string {
const context = causes
? ` Of ${formatNumber(causes.production)} open in production code, ` +
`${formatNumber(causes.all)} overall.`
: "";
return (
"Performance causes worth fixing now, a count rather than a score: the " +
"Performance view's default queue, where a database, network, filesystem or " +
"subprocess call inside a loop has a cost we can show." +
context +
" Never blended into the health score."
);
}
/** For a summary that predates `performance_causes` and carries only the raw count. */
const OPEN_PERFORMANCE_HINT =
"A count of open performance risks, not a score. Each one is a database, " +
"network, filesystem or subprocess call inside a loop, traced across function " +
"boundaries. Never blended into the health score.";
const HOTSPOT_HINT =
"Code health averaged over the repo's churn hotspots — the files you change " +
"most often. Lower than the overall figure on most repos, because " +
"heavily-changed files carry the most change-history deduction.";
const HOTSPOT_HINT_CODE_SHAPE =
"Code shape averaged over the repo's churn hotspots — the files you change " +
"most often. With change history excluded this asks whether the code you " +
"touch most is well built, rather than how much it has moved.";
/** Which figure the page's current selection describes; marks it in the ribbon. */
export type LedePillar = "health" | "maintainability";
export interface CodeHealthLedeProps {
summary: HealthOverviewSummary;
/** Null when the repo lacks the defect history to make an honest claim. */
accuracy?: DefectAccuracy | null;
/** NLOC-weighted split across the bands, shown under the score. */
distribution?: HealthDistribution | null;
/**
* The figure the map is currently coloured by. It highlights one of the two
* and never changes either value: a lens is a way of looking at the repo,
* not a different repo.
*/
pillar?: LedePillar;
/** Rendered under the prose, for the host's pillar deep-links. */
action?: React.ReactNode;
/**
* `secondary` when something else leads the page (Fix first on Code
* Health): one line with the score and its band, and the prose and the
* ribbon behind "More". `lead` is the full opening read.
*/
variant?: "lead" | "secondary";
}
/** "3 months" / "1 month", from a day count. */
function UnanalysedLede({ action }: { action?: React.ReactNode }) {
return (
<PageLede label="Code health" value="Not analysed" action={action}>
<p>{HEALTH_UNSUPPORTED_NOTICE}</p>
</PageLede>
);
}
/** The files a scored figure left out because health has no dialect for them. */
function UnanalysedNote({ count }: { count: number }) {
if (count <= 0) return null;
const one = count === 1;
return (
<>
{" "}
{formatNumber(count)} more {one ? "file is" : "files are"} in a language health does not
analyse yet, so {one ? "it is" : "they are"} left out of this figure.
</>
);
}
function windowLabel(days: number): string {
const months = Math.max(1, Math.round(days / 30));
return months === 1 ? "month" : `${months} months`;
}
export function CodeHealthLede({
summary,
accuracy,
distribution,
pillar = "health",
action,
variant = "lead",
}: CodeHealthLedeProps) {
const health = summary.average_health;
// No file scored: every file is in a language health has no dialect for.
// There is no figure to lead with, so the lede says that instead of a 10.
if (health == null) return <UnanalysedLede action={action} />;
const maint = summary.maintainability_average;
const perf = summary.performance_average;
const perfCauses = summary.performance_causes;
// An older summary carries only the raw open count; show it under its own
// label rather than a "fix now" zero.
const perfOpen = perfCauses == null ? (summary.performance_findings ?? null) : null;
const hotspot = summary.hotspot_health;
const worstTestPath = summary.worst_test_path;
const worstTestScore = summary.worst_test_score;
// Read off the response, not the page's control: the two disagree while a
// request is in flight, and a figure captioned by the mode the reader just
// asked for rather than the one it was computed under is the whole bug this
// page exists to remove.
const codeShape = summary.counts === "code_shape";
const unscored = summary.unscored_files ?? 0;
const structure = summary.structure_average;
const historyDeduction = summary.history_average;
// How much of the deduction is git history rather than code. Stated as a
// share, not as a second score out of 10: a rival 0-10 figure beside
// Maintainability made the page answer "which number do I steer by?" twice.
const deduction = (structure ?? 0) + (historyDeduction ?? 0);
const historyShare =
structure == null || historyDeduction == null || deduction <= 0
? null
: Math.round((historyDeduction / deduction) * 100);
// The band words were fitted against the full, bug-predicting score, so
// they are not a verdict this projection has earned: clamp(10 - structure)
// reads systematically higher, and almost every repo would print "Excellent"
// under it. The figure and the spread still say where the repo sits.
// A `null` band means the server withheld it: too few code files were
// analysed for the average to speak for the repo. Absent on an older server.
const healthChip = codeShape || summary.band === null ? undefined : healthBand(health);
const unanalysed = summary.unanalysed_file_count ?? 0;
// The share is stated whenever files were left out, so a figure over a few
// files never reads as the whole repo.
const filesLabel =
unanalysed > 0
? `${formatNumber(summary.file_count)} of ${formatNumber(summary.file_count + unanalysed)} code files`
: `${formatNumber(summary.file_count)} files`;
const stats: RibbonStat[] = [
{ label: "Files", value: formatNumber(summary.file_count), hint: FILES_HINT },
{
label: "Maintainability",
value: maint == null ? "" : formatScore(maint),
valueColor: maint == null ? undefined : scoreTextColor(maint),
hint: MAINTAINABILITY_HINT,
// The map's lens marks its figure here now that the lede carries one
// number. Dimming the sole headline when the lens moved would have said
// the page was describing something else.
highlighted: pillar === "maintainability",
},
perfOpen != null
? {
label: "Performance risk",
value: perf == null ? "" : formatNumber(perfOpen),
hint: OPEN_PERFORMANCE_HINT,
}
: {
label: "Performance, fix now",
value: perf == null ? "" : formatNumber(perfCauses?.now ?? 0),
hint: performanceHint(perfCauses),
},
{
label: "Hotspot health",
value: hotspot == null ? "" : formatScore(hotspot),
valueColor: hotspot == null ? undefined : scoreTextColor(hotspot),
hint: codeShape ? HOTSPOT_HINT_CODE_SHAPE : HOTSPOT_HINT,
},
];
const full = (
<div className="flex flex-col gap-6">
<PageLede
label="Code health"
labelHint={codeShape ? CODE_SHAPE_HINT : HEALTH_HINT}
value={formatScore(health)}
valueColor={healthChip?.color}
unit="out of 10"
{...(healthChip ? { band: healthChip } : {})}
action={action}
layout="beside"
// The two second reads that belong to this number: how the repo's code
// volume splits across the bands, and how much of the deduction no
// refactor can reach. Both sit behind "Breakdown" so the first read is
// the figure and its band; native <details>, so keyboard and screen
// readers get the toggle for free.
figureFooter={
<>
{codeShape && (
<p className="text-[11px] leading-snug text-[var(--color-text-tertiary)]">
Change history excluded.
</p>
)}
{(distribution || codeShape || historyShare != null) && (
<details className="mt-1.5">
<summary className={DISCLOSURE}>
<Info aria-hidden className="h-3 w-3" />
Breakdown
</summary>
<div className="mt-2.5 flex flex-col gap-3">
{distribution && (
<HealthDistributionBar distribution={distribution} height="sm" />
)}
{codeShape ? (
<p className="text-[11px] leading-snug text-[var(--color-text-tertiary)]">
This scores the code alone, and every finding on this page
counts toward it.
{unscored > 0 && (
<>
{" "}
{formatNumber(unscored)} files are not scored here, having
no recorded split yet.
</>
)}
</p>
) : (
historyShare != null && (
<p className="text-[11px] leading-snug text-[var(--color-text-tertiary)]">
<span className="tabular-nums text-[var(--color-text-secondary)]">
{historyShare}%
</span>{" "}
of this comes from change history, not from the code. No
edit to these files clears it.
</p>
)
)}
</div>
</details>
)}
</>
}
>
<p>
Across{" "}
<strong className="font-semibold text-[var(--color-text-primary)]">
{filesLabel}
</strong>
, weighted by lines of code.
{hotspot != null && (
<>
{" "}
The files you change most average{" "}
<strong
className="font-semibold"
style={{ color: healthBandColor(bandForScore(hotspot)) }}
>
{formatScore(hotspot)}
</strong>
, {describeGap(hotspot, health)}
</>
)}
</p>
{accuracy && !codeShape && (
<p className="mt-2.5">
Ranked against real bug-fix history:{" "}
<strong className="font-semibold text-[var(--color-text-primary)]">
{accuracy.hits} of the {accuracy.k} files
</strong>{" "}
it scores worst had a fix in the last {windowLabel(accuracy.window_days)},{" "}
{Math.round(accuracy.precision * 100)}% against a{" "}
{Math.round(accuracy.base_rate * 100)}% base rate
{accuracy.lift != null && (
<>
, so{" "}
<strong className="font-semibold text-[var(--color-text-primary)]">
{accuracy.lift}× better
</strong>{" "}
than picking at random
</>
)}
.
</p>
)}
{/* The rest qualifies the figure rather than stating it, so it waits
behind one toggle instead of filling the first screen. */}
<details className="mt-2.5 break-inside-avoid">
<summary className={DISCLOSURE}>How it is scored</summary>
<div className="mt-2 flex flex-col gap-2">
<p>
Built from complexity, duplication, coverage
{codeShape ? "" : ", churn and ownership"}.
<UnanalysedNote count={unanalysed} />
</p>
{perf != null && (
// Not "performance risk": this is a score on the same ladder as
// the health number, so a risk noun inverts it. "risk" belongs to
// the findings count in the ribbon. Matches HealthLede.
<p>
Static performance is scored separately at {formatScore(perf)} out
of 10 and never blended into the health score.
</p>
)}
{/* Tests are ranked apart from production files, so the weakest
one would otherwise never be named on this page. */}
{worstTestPath && worstTestScore != null && (
<p>
Test files are ranked apart. The lowest scoring is{" "}
<span className="break-all font-mono text-[12px] text-[var(--color-text-primary)]">
{worstTestPath}
</span>{" "}
at{" "}
<strong
className="font-semibold"
style={{ color: healthBandColor(bandForScore(worstTestScore)) }}
>
{formatScore(worstTestScore)}
</strong>
.
</p>
)}
</div>
</details>
</PageLede>
<StatRibbon stats={stats} />
</div>
);
if (variant === "lead") return full;
return (
<details className="group/lede">
<summary className="flex cursor-pointer list-none flex-wrap items-baseline gap-x-2.5 gap-y-1 rounded focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-focus-ring)] [&::-webkit-details-marker]:hidden">
<span className="font-mono text-[10px] uppercase tracking-[0.12em] text-[var(--color-text-tertiary)]">
Code health
</span>
<span
className="text-[15px] font-semibold tabular-nums text-[var(--color-text-primary)]"
{...(healthChip ? { style: { color: healthChip.color } } : {})}
>
{formatScore(health)}
</span>
<span className="text-xs text-[var(--color-text-tertiary)]">
out of 10 across {filesLabel}
</span>
{healthChip ? (
<span className="inline-flex items-center gap-1.5 text-xs text-[var(--color-text-secondary)]">
<span
aria-hidden
className="inline-block h-1.5 w-1.5 rounded-full"
style={{ background: healthChip.color }}
/>
{healthChip.label}
</span>
) : null}
<span className="text-xs font-medium text-[var(--color-accent-primary)] group-open/lede:hidden">
More
</span>
<span className="hidden text-xs font-medium text-[var(--color-accent-primary)] group-open/lede:inline">
Less
</span>
</summary>
<div className="mt-5">{full}</div>
</details>
);
}
/**
* How the hotspot average sits against the repo average, in words.
*
* Worth a sentence rather than a delta chip: hotspot health below the repo
* average is the finding that actually changes what someone does next, and
* "6.2 (-1.1)" does not say which direction is bad.
*/
function describeGap(hotspot: number, average: number): string {
const gap = hotspot - average;
if (Math.abs(gap) < 0.25) return "in line with the codebase overall.";
return gap < 0
? `${Math.abs(gap).toFixed(1)} below the codebase overall.`
: `${gap.toFixed(1)} above the codebase overall.`;
}