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
import type { PerformanceOpportunity } from "@repowise-dev/types/health";
import { biomarkerInfo, CATEGORY_LABEL, splitByOrigin } from "../biomarker-glossary";
import { performanceCauseLine } from "./performance-causes";
import { findingEntries, historyContextBlock, rankByImpact, remainderRollup } from "./findings";
import {
bulletList,
explorationCloser,
FLAVOR_PREAMBLE,
joinSections,
pluralS,
repoSuffix,
type AiPromptFlavor,
} from "./shared";
// ─────────────────────────────────────────────────────────────────────
// File health prompt
// ─────────────────────────────────────────────────────────────────────
/**
* Everything the file drawer knows about one file. Structural rather than the
* drawer's own types, which would be an import cycle.
*/
export interface FileHealthPromptInput {
file_path: string;
/** `null` when health has no dialect for the file's language. */
score: number | null;
nloc?: number | null;
module?: string | null;
defect_score?: number | null;
maintainability_score?: number | null;
performance_score?: number | null;
line_coverage_pct?: number | null;
has_test_file?: boolean;
duplication_pct?: number | null;
max_ccn?: number | null;
primary_biomarker?: string | null;
primary_reason?: string | null;
total_deduction?: number | null;
}
export interface FileHealthPromptFinding {
id: string;
biomarker_type: string;
severity: string;
function_name: string | null;
line_start?: number | null;
line_end?: number | null;
health_impact: number;
reason: string;
// Explicit `undefined`: the drawer forwards absent fields and the package
// compiles with exactOptionalPropertyTypes.
status?: string | undefined;
details?: Record<string, unknown> | null | undefined;
}
export interface FileHealthPromptCategory {
category: string;
cap?: number | null;
applied_deduction: number;
capped: boolean;
finding_count: number;
}
/** The process and topology facts the drawer's signals panel shows. */
export interface FileHealthPromptSignals {
commit_count_90d?: number | null;
change_entropy_pct?: number | null;
prior_defect_count?: number | null;
bug_magnet?: boolean | null;
last_fix_at?: string | null;
in_degree?: number | null;
out_degree?: number | null;
age_days?: number | null;
}
export interface BuildFileHealthPromptOptions {
file: FileHealthPromptInput;
findings?: FileHealthPromptFinding[];
categories?: FileHealthPromptCategory[];
signals?: FileHealthPromptSignals | null;
/** Open performance causes, when the host fetched them. */
performance?: { items: PerformanceOpportunity[]; total: number } | null;
/** Score movement between the last two snapshots, when known. */
trendDelta?: number | null;
suggestions?: Record<string, string>;
flavor?: AiPromptFlavor;
repoName?: string;
}
/** Spelled out in full; the rest roll up, so a noisy file stays affordable. */
const MAX_FILE_HEALTH_FINDINGS = 10;
/** Causes listed by name. Past this, the queue is where to read them. */
const MAX_FILE_HEALTH_CAUSES = 6;
const CONSTRAINTS = [
"**Read first, edit second.** Read the file, its callers, its tests, and any obvious helpers before proposing a change.",
"Group the findings by root cause before you start. Several markers on one function are usually one problem, and fixing them separately produces churn without improving the file.",
"Preserve runtime behaviour. No new features, and no opportunistic rewrites in unrelated regions.",
"Do **not** change public signatures or exported names unless a verified finding requires it. Call it out explicitly if you must.",
"Keep test coverage at least as high as before. If you change logic, add or update tests.",
"Match the existing style of the file and its neighbours.",
"If a finding turns out to be a false positive once you have read the code, skip it and say why. That is a useful answer, not a failure.",
];
const EXPECTED = [
"1. A short triage: which findings share a root cause, which are independent, and which you believe are false positives.",
"2. A plan of 3 to 6 bullets for the ones worth acting on, ordered so the riskiest change is not the first.",
"3. The edits, scoped to this file plus its tests and any tightly coupled helper.",
"4. A summary of what changed, and which specific findings each change should clear.",
];
/** A titled section with its own leading blank line, or "" when it has no body. */
function section(heading: string, body: string | null | undefined): string {
return body ? `\n${heading}\n\n${body}` : "";
}
/** Open findings: the detailed ones, the rolled-up tail, and history for context. */
function openFindings(findings: FileHealthPromptFinding[]) {
// Findings already triaged away are not open work.
const open = findings.filter(
(f) => f.status !== "resolved" && f.status !== "false_positive",
);
// Split before ranking: history markers go to context, not open work.
const { codeShape, history } = splitByOrigin(open);
const ranked = rankByImpact(codeShape);
return {
detailed: ranked.slice(0, MAX_FILE_HEALTH_FINDINGS),
remainder: remainderRollup(
ranked.slice(MAX_FILE_HEALTH_FINDINGS),
"Handle these only after the ranked items above.",
),
history,
};
}
function scoredDimensions(file: FileHealthPromptInput): string {
return bulletList([
file.defect_score != null ? `Code health: **${file.defect_score.toFixed(1)}/10**` : null,
file.maintainability_score != null
? `Maintainability: **${file.maintainability_score.toFixed(1)}/10**`
: null,
file.performance_score != null
? `Performance: **${file.performance_score.toFixed(1)}/10**`
: null,
]);
}
function deductionLine(
totalDeduction: number | null | undefined,
categoryCount: number,
): string | null {
if (totalDeduction == null) return null;
const categories = categoryCount === 1 ? "category" : "categories";
return `Total deduction: **−${totalDeduction.toFixed(2)}** across ${categoryCount} scoring ${categories}`;
}
function trendLine(trendDelta: number | null | undefined): string | null {
if (trendDelta == null || trendDelta === 0) return null;
return `Score moved ${trendDelta > 0 ? "up" : "down"} ${Math.abs(trendDelta).toFixed(2)} since the previous snapshot`;
}
function healthSnapshot(
file: FileHealthPromptInput,
categoryCount: number,
trendDelta: number | null | undefined,
): string {
return bulletList([
file.score == null
? "Health score: not analysed (no health support for this language yet)"
: `Health score: **${file.score.toFixed(1)}/10** (lower is worse; 10.0 is clean)`,
deductionLine(file.total_deduction, categoryCount),
trendLine(trendDelta),
file.nloc != null ? `Size: ${file.nloc} NLOC` : null,
file.module ? `Module: \`${file.module}\`` : null,
file.max_ccn != null ? `Highest cyclomatic complexity in the file: ${file.max_ccn}` : null,
file.duplication_pct != null ? `Duplication: ${file.duplication_pct.toFixed(1)}%` : null,
file.line_coverage_pct != null
? `Line coverage: ${Math.round(file.line_coverage_pct)}%`
: "Line coverage: not reported",
file.has_test_file === false
? "No test file was found for this file, so treat any behaviour change as unverified until you add one."
: null,
]);
}
function leadingCause(file: FileHealthPromptInput): string | null {
if (!file.primary_biomarker) return null;
const reason = file.primary_reason ? ` ${file.primary_reason}` : "";
return `**${biomarkerInfo(file.primary_biomarker).label}.**${reason}`;
}
function categoryLine(c: FileHealthPromptCategory): string {
const label = CATEGORY_LABEL[c.category as keyof typeof CATEGORY_LABEL] ?? c.category;
const cap = c.cap != null ? `, bounded at −${c.cap.toFixed(1)}` : "";
// A capped category's figure is a floor, not a measurement.
const note = c.capped ? " — **at its ceiling**, so this understates the category" : "";
return `${label}: −${c.applied_deduction.toFixed(2)} from ${c.finding_count} finding${pluralS(
c.finding_count,
)}${cap}${note}`;
}
/** Where the deduction comes from, largest category first. */
function categoryBreakdown(categories: FileHealthPromptCategory[]): string | null {
if (categories.length === 0) return null;
return bulletList(
categories
.slice()
.sort((a, b) => b.applied_deduction - a.applied_deduction)
.map(categoryLine),
);
}
/** How often the file changes and how many of those changes were fixes. */
function churnFacts(signals: FileHealthPromptSignals): (string | null)[] {
const lastFix = signals.last_fix_at ? `, most recently ${signals.last_fix_at.slice(0, 10)}` : "";
return [
signals.commit_count_90d != null
? `${signals.commit_count_90d} commits in the last 90 days`
: null,
signals.change_entropy_pct != null
? `Change entropy ${Math.round(signals.change_entropy_pct)}%, which is how scattered those edits were across the file`
: null,
signals.prior_defect_count != null && signals.prior_defect_count > 0
? `${signals.prior_defect_count} prior bug fixes have landed here${lastFix}`
: null,
signals.bug_magnet
? "Flagged a **bug magnet**: fixes keep landing in this file, so treat a regression here as likely rather than unlucky."
: null,
];
}
/** Where the file sits in the import graph, and how long it has existed. */
function topologyFacts(signals: FileHealthPromptSignals): (string | null)[] {
return [
signals.in_degree != null
? `${signals.in_degree} file${pluralS(signals.in_degree)} import this one, so changing its public surface reaches all of them`
: null,
signals.out_degree != null ? `It imports ${signals.out_degree} others` : null,
signals.age_days != null ? `First seen ${signals.age_days} days ago` : null,
];
}
function processSignals(signals: FileHealthPromptSignals | null | undefined): string | null {
if (!signals) return null;
return bulletList([...churnFacts(signals), ...topologyFacts(signals)]);
}
function performanceCauses(causes: PerformanceOpportunity[]): string | null {
if (causes.length === 0) return null;
return [
bulletList(causes.slice(0, MAX_FILE_HEALTH_CAUSES).map(performanceCauseLine)),
causes.length > MAX_FILE_HEALTH_CAUSES
? `…and ${causes.length - MAX_FILE_HEALTH_CAUSES} more open on this file.`
: null,
"These are static reads of the code, not measurements of a running system. Confirm a path is actually hot before optimising it.",
]
.filter(Boolean)
.join("\n\n");
}
/**
* One file, everything code health has on it, as a prompt. Unlike
* `buildAiPrompt` it hands over every signal and asks which share a root
* cause, rather than ordering a refactor.
*/
export function buildFileHealthAiPrompt({
file,
findings = [],
categories = [],
signals,
performance,
trendDelta,
suggestions = {},
flavor = "generic",
repoName,
}: BuildFileHealthPromptOptions): string {
const { detailed, remainder, history } = openFindings(findings);
const behaviour = [processSignals(signals), historyContextBlock(history)]
.filter(Boolean)
.join("\n\n");
return joinSections([
FLAVOR_PREAMBLE[flavor],
`\n## Target file${repoSuffix(repoName)}\n`,
`\`${file.file_path}\``,
`\n## Current health snapshot\n`,
healthSnapshot(file, categories.length, trendDelta),
section("### Scored dimensions", scoredDimensions(file)),
section("### Leading cause", leadingCause(file)),
section("## Where the deduction comes from", categoryBreakdown(categories)),
section("## Open findings (ranked by impact)", findingEntries(detailed, suggestions)),
remainder ? `\n${remainder}` : "",
section("## Open performance causes", performanceCauses(performance?.items ?? [])),
section("## How this file behaves over time", behaviour),
`\n## Hard constraints\n`,
bulletList(CONSTRAINTS),
`\n## What I expect back\n`,
EXPECTED.join("\n"),
`\n${explorationCloser(flavor, file.file_path, "file-health")}`,
]);
}