CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | /**
* AI Technical Debt Analyzer.
*
* Scans a repository's files, extracts static metrics, then uses Claude
* Sonnet to score the top 20 files by technical debt. Returns a DebtReport
* with per-file scores, issue lists, and estimated cleanup hours.
*/
import { join } from "path";
import { config } from "./config";
import { getAnthropic, MODEL_SONNET, extractText, parseJsonResponse } from "./ai-client";
// ─── Types ────────────────────────────────────────────────────────────────────
export interface DebtNode {
path: string;
lines: number;
debtScore: number; // 0-100, higher = more debt
issues: string[]; // e.g. ["Long functions (avg 87 lines)", "Deep nesting"]
estimatedHours: number; // Claude's estimate to clean up
imports: string[]; // files this file imports (resolved relative paths)
}
export interface DebtReport {
repoId: string;
commitSha: string;
nodes: DebtNode[];
totalDebtHours: number;
analyzedAt: string;
}
// ─── Constants ────────────────────────────────────────────────────────────────
const CODE_EXTENSIONS = new Set([
".ts", ".tsx", ".js", ".jsx",
".py", ".go", ".rs", ".java", ".rb", ".php",
]);
const SKIP_PATTERNS = ["node_modules/", "dist/", ".min.", "vendor/"];
const MAX_FILES = 150;
const MAX_AI_FILES = 20;
// ─── Helpers ─────────────────────────────────────────────────────────────────
/** Run a git command inside the bare repo directory for `owner/repo`. */
async function gitExec(
repoPath: string,
args: string[]
): Promise<string> {
const proc = Bun.spawn(["git", ...args], {
cwd: repoPath,
stdout: "pipe",
stderr: "pipe",
});
const text = await new Response(proc.stdout).text();
await proc.exited;
return text;
}
/** Return true if a file path should be skipped. */
function shouldSkip(path: string): boolean {
for (const pat of SKIP_PATTERNS) {
if (path.includes(pat)) return true;
}
return false;
}
/** Return true if the file has a code extension we want to analyze. */
function isCodeFile(path: string): boolean {
const dot = path.lastIndexOf(".");
if (dot === -1) return false;
return CODE_EXTENSIONS.has(path.slice(dot));
}
/** Count lines in a string. */
function countLines(content: string): number {
if (!content) return 0;
return content.split("\n").length;
}
/** Count TODO/FIXME/HACK/XXX occurrences. */
function countTodos(content: string): number {
return (content.match(/\bTODO\b|\bFIXME\b|\bHACK\b|\bXXX\b/g) || []).length;
}
/** Rough complexity hint: count function/def/fn keywords vs line count. */
function complexityHint(content: string, lines: number): string {
const fnCount = (
content.match(/\b(function|def |fn |func |async function|const \w+ = \(|=> \{)/g) || []
).length;
if (lines === 0) return "empty";
if (lines > 800) return "very large file";
if (lines > 400) return "large file";
if (fnCount > 0) {
const avgFnLen = Math.round(lines / fnCount);
if (avgFnLen > 60) return `avg function ${avgFnLen} lines`;
}
return "normal";
}
/**
* Extract import paths from a file's content.
* Handles: import/from (ES modules), require() calls.
* Returns only local relative imports (starting with . or /).
*/
function extractImports(content: string, filePath: string): string[] {
const imports: string[] = [];
const dir = filePath.includes("/")
? filePath.slice(0, filePath.lastIndexOf("/"))
: "";
// ES import: import ... from '...' or import '...'
const esImports = content.matchAll(/(?:import|from)\s+['"]([^'"]+)['"]/g);
for (const m of esImports) {
imports.push(m[1]);
}
// require(): require('...')
const requireImports = content.matchAll(/require\s*\(\s*['"]([^'"]+)['"]\s*\)/g);
for (const m of requireImports) {
imports.push(m[1]);
}
// Filter to relative/local only, resolve to plausible path
const resolved: string[] = [];
for (const imp of imports) {
if (!imp.startsWith(".") && !imp.startsWith("/")) continue;
// Simple resolution
let resolved_path = imp.startsWith("/")
? imp.slice(1)
: dir
? `${dir}/${imp}`
: imp;
// Normalize .. segments (very roughly)
const parts = resolved_path.split("/").filter(Boolean);
const stack: string[] = [];
for (const p of parts) {
if (p === "..") stack.pop();
else if (p !== ".") stack.push(p);
}
resolved_path = stack.join("/");
if (resolved_path) resolved.push(resolved_path);
}
return [...new Set(resolved)];
}
/** Heuristic debt score for files not sent to Claude. */
function heuristicScore(todos: number, lines: number): number {
return Math.min(100, todos * 5 + Math.min(50, Math.round(lines / 20)));
}
// ─── Main analyzer ────────────────────────────────────────────────────────────
export async function analyzeRepo(
repoId: string,
owner: string,
repo: string
): Promise<DebtReport> {
const repoPath = join(config.gitReposPath, owner, `${repo}.git`);
// 1. List all files at HEAD
const lsOutput = await gitExec(repoPath, ["ls-tree", "-r", "--name-only", "HEAD"]);
const allFiles = lsOutput
.split("\n")
.map((f) => f.trim())
.filter((f) => f.length > 0 && isCodeFile(f) && !shouldSkip(f))
.slice(0, MAX_FILES);
// 2. Get HEAD commit SHA
const sha = (await gitExec(repoPath, ["rev-parse", "HEAD"])).trim();
// 3. Read each file, gather static metrics
interface FileInfo {
path: string;
content: string;
lines: number;
todos: number;
hint: string;
imports: string[];
}
const fileInfos: FileInfo[] = [];
for (const path of allFiles) {
const content = await gitExec(repoPath, ["show", `HEAD:${path}`]).catch(() => "");
const lines = countLines(content);
const todos = countTodos(content);
const hint = complexityHint(content, lines);
const imports = extractImports(content, path);
fileInfos.push({ path, content, lines, todos, hint, imports });
}
// 4. Sort by line count desc; pick top 20 for Claude
fileInfos.sort((a, b) => b.lines - a.lines);
const topFiles = fileInfos.slice(0, MAX_AI_FILES);
const restFiles = fileInfos.slice(MAX_AI_FILES);
// 5. Call Claude for the top 20
let aiResults: Map<string, { debtScore: number; issues: string[]; estimatedHours: number }> =
new Map();
if (topFiles.length > 0) {
const prompt = `You are a senior engineer assessing technical debt. Rate each file's debt 0-100 and estimate cleanup hours.
Return a JSON array only, no prose: [{"path":"...","debtScore":N,"issues":["..."],"estimatedHours":N}, ...]
Files:
${JSON.stringify(
topFiles.map((f) => ({
path: f.path,
lines: f.lines,
todos: f.todos,
complexityHint: f.hint,
}))
)}`;
try {
const client = getAnthropic();
const msg = await client.messages.create({
model: MODEL_SONNET,
max_tokens: 4096,
messages: [{ role: "user", content: prompt }],
});
const text = extractText(msg);
type AiRow = { path: string; debtScore: number; issues: string[]; estimatedHours: number };
const parsed = parseJsonResponse<AiRow[]>(text);
if (Array.isArray(parsed)) {
for (const row of parsed) {
if (row && typeof row.path === "string") {
aiResults.set(row.path, {
debtScore: Math.min(100, Math.max(0, Number(row.debtScore) || 0)),
issues: Array.isArray(row.issues)
? row.issues.map(String)
: [],
estimatedHours: Math.max(0, Number(row.estimatedHours) || 0),
});
}
}
}
} catch {
// Claude unavailable — fall through to heuristics for all
}
}
// 6. Build nodes
const nodes: DebtNode[] = [];
for (const f of topFiles) {
const ai = aiResults.get(f.path);
if (ai) {
nodes.push({
path: f.path,
lines: f.lines,
debtScore: ai.debtScore,
issues: ai.issues,
estimatedHours: ai.estimatedHours,
imports: f.imports,
});
} else {
const score = heuristicScore(f.todos, f.lines);
nodes.push({
path: f.path,
lines: f.lines,
debtScore: score,
issues: buildHeuristicIssues(f.todos, f.lines, f.hint),
estimatedHours: Math.round(score / 10),
imports: f.imports,
});
}
}
for (const f of restFiles) {
const score = heuristicScore(f.todos, f.lines);
nodes.push({
path: f.path,
lines: f.lines,
debtScore: score,
issues: buildHeuristicIssues(f.todos, f.lines, f.hint),
estimatedHours: Math.round(score / 10),
imports: f.imports,
});
}
const totalDebtHours = nodes.reduce((sum, n) => sum + n.estimatedHours, 0);
return {
repoId,
commitSha: sha,
nodes,
totalDebtHours,
analyzedAt: new Date().toISOString(),
};
}
function buildHeuristicIssues(todos: number, lines: number, hint: string): string[] {
const issues: string[] = [];
if (todos > 0) issues.push(`${todos} TODO/FIXME comment${todos > 1 ? "s" : ""}`);
if (lines > 800) issues.push("Very large file (>800 lines)");
else if (lines > 400) issues.push("Large file (>400 lines)");
if (hint !== "normal" && hint !== "empty" && hint !== "large file" && hint !== "very large file") {
issues.push(hint.charAt(0).toUpperCase() + hint.slice(1));
}
return issues;
}
|