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
feat(ui): show what a file's score lost to code shape, change history and test coverage (#4124)
Raghav Chamadiyaa78b520d42d6
diff --git a/packages/ui/__tests__/health/file-signals-panel.test.tsx b/packages/ui/__tests__/health/file-signals-panel.test.tsxindex 1760714f1..bd4c08115 100644--- a/packages/ui/__tests__/health/file-signals-panel.test.tsx+++ b/packages/ui/__tests__/health/file-signals-panel.test.tsx@@ -1,7 +1,7 @@import { describe, it, expect } from "vitest";import { render, screen } from "@testing-library/react";import type { FileSignals } from "@repowise-dev/types/health";-import { FileSignalsPanel } from "../../src/health/file-signals-panel.js";+import { FileSignalsPanel, hasAnySignal } from "../../src/health/file-signals-panel.js";function sig(partial: Partial<FileSignals>): FileSignals {return {@@ -145,3 +145,16 @@ describe("FileSignalsPanel magnet recency guard", () => {expect(screen.queryByText("Bug magnet")).not.toBeInTheDocument();});});++describe("FileSignalsPanel short history", () => {+ it("says the history is too short to count, even when it is the only signal", () => {+ render(<FileSignalsPanel signals={sig({ history: "short" })} />);+ expect(screen.getByText("Short history")).toBeInTheDocument();+ expect(screen.getByText(/fewer than 5 commits recorded/)).toBeInTheDocument();+ });++ it("counts short history as a process signal only", () => {+ expect(hasAnySignal(sig({ history: "short" }), ["process"])).toBe(true);+ expect(hasAnySignal(sig({ history: "short" }), ["topology"])).toBe(false);+ });+});diff --git a/packages/ui/__tests__/health/score-split.test.tsx b/packages/ui/__tests__/health/score-split.test.tsxnew file mode 100644index 000000000..a7c417b44--- /dev/null+++ b/packages/ui/__tests__/health/score-split.test.tsx@@ -0,0 +1,139 @@+import { describe, it, expect } from "vitest";+import { render, screen } from "@testing-library/react";+import { HealthFileDrawer } from "../../src/health/health-file-drawer.js";+import { scoreSplit, type SplitCategory } from "../../src/health/score-split.js";+import { FilePageHeader } from "../../src/files/file-page-header.js";+import type { FileDetailResponse } from "@repowise-dev/types/files";+import { drawerMetric, drawerSignals } from "./drawer-test-utils.js";++function category(name: string, applied: number): SplitCategory {+ return { category: name, applied_deduction: applied };+}++const CATEGORIES = [+ category("structural_complexity", 1.2),+ category("size_and_complexity", 0.5),+ category("organizational", 0.8),+ category("test_coverage", 0.3),+ category("test_coverage_gradient", 0.2),+];++function breakdown(categories = CATEGORIES) {+ const total = categories.reduce((s, c) => s + c.applied_deduction, 0);+ return {+ score: 10 - total,+ total_deduction: total,+ categories: categories.map((c) => ({+ ...c,+ cap: 2,+ raw_deduction: c.applied_deduction,+ capped: false,+ finding_count: 1,+ findings: [],+ })),+ };+}++describe("scoreSplit", () => {+ it("puts history in its own part, coverage in its own, and the rest in code shape", () => {+ const split = scoreSplit(CATEGORIES);+ expect(split.shape).toBeCloseTo(1.7);+ expect(split.history).toBeCloseTo(0.8);+ expect(split.coverage).toBeCloseTo(0.5);+ });++ it("counts a category it does not know as code shape", () => {+ expect(scoreSplit([category("new_kind", 0.4)]).shape).toBeCloseTo(0.4);+ });+});++describe("HealthFileDrawer score split", () => {+ it("shows the three parts of the deduction beside the score", () => {+ render(+ <HealthFileDrawer+ open+ onClose={() => {}}+ metric={drawerMetric({ score: 7.0 })}+ breakdown={breakdown()}+ />,+ );+ const split = screen.getByRole("group", { name: "What the score lost" });+ expect(split).toHaveTextContent("Code shape");+ expect(split).toHaveTextContent("−1.70");+ expect(split).toHaveTextContent("Change history");+ expect(split).toHaveTextContent("−0.80");+ expect(split).toHaveTextContent("Test coverage");+ expect(split).toHaveTextContent("−0.50");+ });++ it("says when the file's history is too short to count", () => {+ render(+ <HealthFileDrawer+ open+ onClose={() => {}}+ metric={drawerMetric({ score: 7.0 })}+ breakdown={breakdown()}+ signals={drawerSignals({ history: "short" })}+ />,+ );+ expect(screen.getByText(/fewer than 5 commits/)).toBeInTheDocument();+ });++ it("renders nothing without a breakdown or a short history", () => {+ render(<HealthFileDrawer open onClose={() => {}} metric={drawerMetric({ score: 7.0 })} />);+ expect(screen.queryByRole("group", { name: "What the score lost" })).not.toBeInTheDocument();+ });++ it("shows a part that lost nothing without a minus sign", () => {+ render(+ <HealthFileDrawer+ open+ onClose={() => {}}+ metric={drawerMetric({ score: 8.8 })}+ breakdown={breakdown([category("structural_complexity", 1.2)])}+ />,+ );+ const split = screen.getByRole("group", { name: "What the score lost" });+ expect(split).toHaveTextContent("Change history0.00");+ });++ it("stays quiet about history when it is long enough", () => {+ render(+ <HealthFileDrawer+ open+ onClose={() => {}}+ metric={drawerMetric({ score: 7.0 })}+ breakdown={breakdown()}+ signals={drawerSignals()}+ />,+ );+ expect(screen.queryByText(/fewer than 5 commits/)).not.toBeInTheDocument();+ });+});++describe("FilePageHeader score split", () => {+ it("shows the split and the short-history note under the score", () => {+ const data = {+ file_path: "src/a.py",+ wiki_page: null,+ health: {+ metric: { ...drawerMetric({ score: 7.0 }), line_coverage_pct: null, duplication_pct: null },+ breakdown: breakdown(),+ findings: [],+ trend: null,+ signals: drawerSignals({ history: "short" }),+ },+ git: null,+ coverage: null,+ graph: null,+ symbols: [],+ function_blame: [],+ governing_decisions: [],+ dead_code: [],+ } as unknown as FileDetailResponse;+ render(<FilePageHeader data={data} linkPrefix="/repos/r1" />);+ const split = screen.getByRole("group", { name: "What the score lost" });+ expect(split).toHaveTextContent("−1.70");+ expect(screen.getByText(/fewer than 5 commits/)).toBeInTheDocument();+ });+});diff --git a/packages/ui/src/files/file-page-header.tsx b/packages/ui/src/files/file-page-header.tsxindex 177a034ba..a57887346 100644--- a/packages/ui/src/files/file-page-header.tsx+++ b/packages/ui/src/files/file-page-header.tsx@@ -5,6 +5,7 @@ import { AskAboutThis } from "../chat/ask-about-this";import { PageLede } from "../shared/page-lede";import { StatRibbon, type RibbonStat } from "../stats/stat-ribbon";import { healthBandColor, coverageTextColor } from "../health/tokens";+import { ScoreSplit } from "../health/score-split";import { formatLOC, formatNumber } from "../lib/format";import { FileMarks } from "./file-marks";@@ -108,6 +109,12 @@ export function FilePageHeader({color: healthBandColor(bandForScore(score)),}}layout="beside"+ figureFooter={+ <ScoreSplit+ categories={data.health.breakdown?.categories}+ signals={data.health.signals}+ />+ }><FileProse data={data} linkPrefix={linkPrefix} LinkComponent={A} /></PageLede>diff --git a/packages/ui/src/health/file-drawer/verdict.tsx b/packages/ui/src/health/file-drawer/verdict.tsxindex 26348f48e..4e861afbf 100644--- a/packages/ui/src/health/file-drawer/verdict.tsx+++ b/packages/ui/src/health/file-drawer/verdict.tsx@@ -9,10 +9,11 @@import { ExternalLink, Wrench } from "lucide-react";import { bandForScore, formatScore, HEALTH_BAND_LABEL } from "@repowise-dev/types/health";-import type { FileHealthTrend } from "@repowise-dev/types/health";+import type { FileHealthTrend, FileSignals } from "@repowise-dev/types/health";import { AiPromptButton } from "../ai-prompt-button";import { AiPromptModal, fileChatContext } from "../ai-prompt-modal";import { buildFileHealthAiPrompt } from "../ai-prompt-builder";+import { ScoreSplit } from "../score-split";import { Sparkline } from "../sparkline";import { HEALTH_UNSUPPORTED_LABEL } from "../map/lens";import { deltaColor, formatDelta, healthBandColor } from "../tokens";@@ -27,6 +28,8 @@ import { QUIET_LINK } from "./shared";export function VerdictSection({metric,trend,+ breakdown,+ signals,model,planTarget,fullPage,@@ -35,6 +38,8 @@ export function VerdictSection({}: {metric: HealthDrawerMetric;trend: FileHealthTrend | null | undefined;+ breakdown: HealthFileDrawerProps["breakdown"];+ signals: FileSignals | null | undefined;model: DrawerModel;planTarget: string | null;fullPage: string | undefined;@@ -44,6 +49,9 @@ export function VerdictSection({return (<section aria-label="Verdict" className="flex flex-col gap-3"><ScoreRow metric={metric} trend={trend} />+ {metric.score != null ? (+ <ScoreSplit categories={breakdown?.categories} signals={signals} />+ ) : null}<VerdictSentenceverdict={model.verdict}show={metric.score != null || model.findings.length > 0}diff --git a/packages/ui/src/health/file-signals-panel.tsx b/packages/ui/src/health/file-signals-panel.tsxindex 611578a6e..93c12557c 100644--- a/packages/ui/src/health/file-signals-panel.tsx+++ b/packages/ui/src/health/file-signals-panel.tsx@@ -2,6 +2,7 @@import type { FileSignals } from "@repowise-dev/types/health";import { formatRelativeTimeOrNull, ordinal } from "../lib/format";+import { SHORT_HISTORY_DETAIL, SHORT_HISTORY_LABEL } from "./score-split";export interface FileSignalsPanelProps {signals: FileSignals | null | undefined;@@ -76,6 +77,9 @@ export function FileSignalsPanel({caption="first seen in git history"present={signals.age_days != null}/>+ {signals.history === "short" ? (+ <Row value={SHORT_HISTORY_LABEL} caption={SHORT_HISTORY_DETAIL} present />+ ) : null}</Group>) : null}@@ -137,7 +141,8 @@ export function hasAnySignal((s.prior_defect_count != null ||s.change_entropy_pct != null ||s.commit_count_90d != null ||- s.age_days != null)) ||+ s.age_days != null ||+ s.history === "short")) ||(groups.includes("people") &&(s.primary_owner_name != null || s.recent_owner_name != null)) ||(groups.includes("topology") && (s.in_degree != null || s.out_degree != null))diff --git a/packages/ui/src/health/health-file-drawer.tsx b/packages/ui/src/health/health-file-drawer.tsxindex 228f401de..1cf48220f 100644--- a/packages/ui/src/health/health-file-drawer.tsx+++ b/packages/ui/src/health/health-file-drawer.tsx@@ -316,6 +316,8 @@ function DrawerBody(props: HealthFileDrawerProps & { metric: HealthDrawerMetric<VerdictSectionmetric={metric}trend={props.trend}+ breakdown={props.breakdown}+ signals={signals}model={model}planTarget={planTarget}fullPage={props.permalinkHref ?? props.fileViewHref}diff --git a/packages/ui/src/health/score-split.tsx b/packages/ui/src/health/score-split.tsxnew file mode 100644index 000000000..8c3192b94--- /dev/null+++ b/packages/ui/src/health/score-split.tsx@@ -0,0 +1,91 @@+import type { FileSignals } from "@repowise-dev/types/health";+import { InfoTip } from "../shared/info-tip";+import { HISTORY_CATEGORY } from "./biomarker-glossary";++/** The fields of a breakdown category the split reads. */+export interface SplitCategory {+ category: string;+ applied_deduction: number;+}++export interface ScoreSplitParts {+ shape: number;+ history: number;+ coverage: number;+}++export const SHORT_HISTORY_LABEL = "Short history";+export const SHORT_HISTORY_DETAIL =+ "fewer than 5 commits recorded, too few to count, so this score is mostly about code shape";++const COVERAGE_CATEGORIES = new Set(["test_coverage", "test_coverage_gradient"]);++/** A file's deduction in three parts: code shape, change history, test coverage. */+export function scoreSplit(categories: readonly SplitCategory[]): ScoreSplitParts {+ const parts: ScoreSplitParts = { shape: 0, history: 0, coverage: 0 };+ for (const c of categories) {+ const key =+ c.category === HISTORY_CATEGORY+ ? "history"+ : COVERAGE_CATEGORIES.has(c.category)+ ? "coverage"+ : "shape";+ parts[key] += c.applied_deduction;+ }+ return parts;+}++const PARTS: { key: keyof ScoreSplitParts; label: string; tip: string }[] = [+ {+ key: "shape",+ label: "Code shape",+ tip: "Lost to how the code is written: complexity, size, nesting, duplication, error handling. A refactor moves this part.",+ },+ {+ key: "history",+ label: "Change history",+ tip: "Lost to how the file has been changed: past bug fixes, churn, many hands. Editing the code does not move this part; time does.",+ },+ {+ key: "coverage",+ label: "Test coverage",+ tip: "Lost to missing tests, or to lines a coverage report never saw run.",+ },+];++export interface ScoreSplitProps {+ categories: readonly SplitCategory[] | null | undefined;+ signals?: Pick<FileSignals, "history"> | null | undefined;+}++/** What the score lost, split by where it came from, plus the short-history note. */+export function ScoreSplit({ categories, signals }: ScoreSplitProps) {+ const parts = categories && categories.length > 0 ? scoreSplit(categories) : null;+ const short = signals?.history === "short";+ if (!parts && !short) return null;+ return (+ <div role="group" aria-label="What the score lost" className="flex flex-col gap-1.5">+ {parts ? (+ <dl className="flex flex-wrap gap-x-4 gap-y-1 text-xs">+ {PARTS.map((p) => (+ <div key={p.key} className="inline-flex items-center gap-1.5">+ <dt className="inline-flex items-center gap-1 text-[var(--color-text-secondary)]">+ {p.label}+ <InfoTip content={p.tip} label={`About ${p.label.toLowerCase()}`} />+ </dt>+ <dd className="tabular-nums text-[var(--color-text-primary)]">+ {parts[p.key] > 0 ? "−" : ""}+ {parts[p.key].toFixed(2)}+ </dd>+ </div>+ ))}+ </dl>+ ) : null}+ {short ? (+ <p className="text-xs text-[var(--color-text-tertiary)]">+ {SHORT_HISTORY_LABEL}: {SHORT_HISTORY_DETAIL}.+ </p>+ ) : null}+ </div>+ );+}