Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | 217x 217x 217x 374x 374x 374x 408x 405x 405x 405x 405x 405x 74x 106x 22x 74x 16x 10x 286x 162x 3x 82x 17x 167x 167x 167x 17x 17x 17x 17x 17x 17x 34x 39x 66x 59x 53x 53x 53x 39x 17x 14x 14x 74x 74x 40x 40x 40x 14x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 1x 2x 1x 1x 1x 3x 3x 1x 1x 2x 1x 2x 2x 1x 1x 222x 222x 222x 4404x 4404x 63x 4341x 222x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 287x 65x 65x 65x 65x 65x 65x 65x 65x 1x 1x 2x 1x 42x 42x 42x 42x 42x 42x 42x 42x 1x 7x 7x 15x 42x 65x 65x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 65x 4x 4x 4x 4x 10x 4x 65x 65x 65x 65x 65x 65x 14x 14x 14x 13x 13x 13x 65x 24x 24x 24x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 65x 65x 65x 65x 284x 284x 284x 65x 10x 10x 10x 10x 65x | import fs from 'node:fs';
import path from 'node:path';
import { PILLAR_CATEGORIES, SEVERITY_ORDER } from './types.js';
import type { ReportAnalysisSummary } from './report-analysis.js';
import type { Finding } from './types.js';
export function severityBreakdown(findings: Finding[]): Record<string, number> {
const counts: Record<string, number> = { critical: 0, high: 0, medium: 0, low: 0, info: 0 };
for (const f of findings) counts[f.severity] = (counts[f.severity] || 0) + 1;
return counts;
}
export function categoryBreakdown(findings: Finding[]): Record<string, number> {
const counts: Record<string, number> = {};
for (const f of findings) counts[f.category] = (counts[f.category] || 0) + 1;
return counts;
}
export function computeHealthScore(findings: Finding[], totalFiles: number): number {
if (totalFiles === 0) return 100;
const weights = { critical: 25, high: 10, medium: 3, low: 1, info: 0 };
let penalty = 0;
for (const f of findings) penalty += weights[f.severity] || 0;
const normalized = (penalty / totalFiles) * 10;
return Math.max(0, Math.round(100 - normalized));
}
export function collectTagCloud(findings: Finding[]): { tag: string; count: number }[] {
const tagCounts = new Map<string, number>();
for (const f of findings) {
if (!f.tags) continue;
for (const tag of f.tags) {
tagCounts.set(tag, (tagCounts.get(tag) || 0) + 1);
}
}
return [...tagCounts.entries()]
.map(([tag, count]) => ({ tag, count }))
.sort((a, b) => b.count - a.count);
}
export function formatFileSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
type FindingLike = Omit<Finding, 'id'> & { id?: string };
export function diversifyFindings<T extends FindingLike>(sorted: T[], limit: number): T[] {
if (!Number.isFinite(limit) || limit >= sorted.length) return sorted;
const groups = new Map<string, T[]>();
for (const f of sorted) {
const cat = f.category;
if (!groups.has(cat)) groups.set(cat, []);
groups.get(cat)!.push(f);
}
const categoryOrder = [...groups.entries()].sort((a, b) => {
const aTop = SEVERITY_ORDER[a[1][0].severity] ?? 0;
const bTop = SEVERITY_ORDER[b[1][0].severity] ?? 0;
return bTop - aTop;
});
const result: T[] = [];
const cursors = new Map<string, number>();
for (const [cat] of categoryOrder) cursors.set(cat, 0);
while (result.length < limit) {
let picked = false;
for (const [cat, items] of categoryOrder) {
if (result.length >= limit) break;
const cursor = cursors.get(cat)!;
if (cursor < items.length) {
result.push(items[cursor]);
cursors.set(cat, cursor + 1);
picked = true;
}
}
Iif (!picked) break;
}
return result;
}
export function diverseTopRecommendations(findings: Finding[], limit: number = 20, maxPerCategory: number = 2): Finding[] {
const result: Finding[] = [];
const countByCategory = new Map<string, number>();
for (const f of findings) {
const catCount = countByCategory.get(f.category) || 0;
if (catCount >= maxPerCategory) continue;
result.push(f);
countByCategory.set(f.category, catCount + 1);
if (result.length >= limit) break;
}
return result;
}
export interface SummaryMdOptions {
dir: string;
report: import('./report-writer.js').FullReport;
outputFiles: Record<string, string>;
architectureFindings: Finding[];
codeQualityFindings: Finding[];
deadCodeFindings: Finding[];
hotFiles?: import('./types.js').HotFile[];
activeFeatures?: Set<string> | null;
scope?: string[] | null;
root?: string;
scopeSymbols?: Map<string, string[]> | null;
semanticEnabled?: boolean;
securityFindings?: Finding[];
testQualityFindings?: Finding[];
reportAnalysis?: ReportAnalysisSummary;
}
export function generateSummaryMd(opts: SummaryMdOptions): string {
const {
dir, report, outputFiles,
architectureFindings, codeQualityFindings, deadCodeFindings,
hotFiles = [], activeFeatures = null, scope = null,
root = process.cwd(), scopeSymbols = null,
semanticEnabled = false, securityFindings = [], testQualityFindings = [],
reportAnalysis = null,
} = opts;
const allFindings = report.optimizationFindings || [];
const sev = severityBreakdown(allFindings);
const summary = report.summary as Record<string, unknown>;
const agentOutput = report.agentOutput as Record<string, unknown>;
const depGraph = report.dependencyGraph;
const lines: string[] = [];
lines.push('# Code Quality Scan Report\n');
lines.push(`**Generated**: ${report.generatedAt} `);
lines.push(`**Root**: \`${report.repoRoot}\`\n`);
lines.push('## Scan Scope\n');
lines.push(`| Metric | Count |`);
lines.push(`|--------|-------|`);
lines.push(`| Files analyzed | ${summary.totalFiles ?? '—'} |`);
lines.push(`| Functions | ${summary.totalFunctions ?? '—'} |`);
lines.push(`| Flow nodes | ${summary.totalFlows ?? '—'} |`);
lines.push(`| Dependency files | ${summary.totalDependencyFiles ?? '—'} |`);
lines.push(`| Packages | ${summary.totalPackages ?? '—'} |`);
lines.push('');
lines.push('## Findings Overview\n');
lines.push(`| Severity | Count |`);
lines.push(`|----------|-------|`);
lines.push(`| Critical | ${sev.critical} |`);
lines.push(`| High | ${sev.high} |`);
lines.push(`| Medium | ${sev.medium} |`);
lines.push(`| Low | ${sev.low} |`);
lines.push(`| **Total** | **${allFindings.length}** |`);
lines.push('');
const totalBefore = (agentOutput as Record<string, unknown>)?.totalBeforeTruncation as number | undefined;
const dropped = (agentOutput as Record<string, unknown>)?.droppedCategories as string[] | undefined;
if (totalBefore && totalBefore > allFindings.length) {
lines.push(`> **Truncated**: Showing ${allFindings.length} of ${totalBefore} findings (\`--findings-limit ${allFindings.length}\`).`);
if (dropped && dropped.length > 0) {
lines.push(`> Dropped categories: ${dropped.map((c) => `\`${c}\``).join(', ')}`);
}
lines.push('');
}
if (activeFeatures) {
lines.push(`> **Features filter**: \`--features=${[...activeFeatures].join(',')}\``);
lines.push('');
}
if (scope && scope.length > 0) {
const scopeDisplay = scope.map((s) => path.relative(root, s)).filter(Boolean);
if (scopeDisplay.length > 0) {
let scopeLabel = scopeDisplay.map((p) => `\`${p}\``).join(', ');
if (scopeSymbols && scopeSymbols.size > 0) {
const symParts: string[] = [];
for (const [absFile, names] of scopeSymbols) {
const rel = path.relative(root, absFile);
symParts.push(...names.map((n) => `\`${rel}:${n}\``));
}
scopeLabel = symParts.join(', ');
}
lines.push(`> **Scoped scan**: Only showing findings for: ${scopeLabel}`);
lines.push('');
}
}
if (semanticEnabled) {
lines.push('> **Semantic analysis**: TypeChecker + LanguageService enabled (14 additional categories)');
lines.push('');
}
const renderPillarCategories = (
pillarKey: string,
findings: Finding[],
): void => {
const breakdown = categoryBreakdown(findings);
const pillarCats = PILLAR_CATEGORIES[pillarKey] || [];
const isFiltered = activeFeatures !== null;
for (const cat of pillarCats) {
const count = breakdown[cat] || 0;
const skipped = isFiltered && !activeFeatures!.has(cat);
if (skipped) {
lines.push(`- \`${cat}\`: — *(skipped)*`);
} else {
lines.push(`- \`${cat}\`: ${count}`);
}
}
lines.push('');
};
const totalFiles = (summary.totalFiles as number) || 1;
const overallHealth = computeHealthScore(allFindings, totalFiles);
const archHealth = computeHealthScore(architectureFindings, totalFiles);
const qualHealth = computeHealthScore(codeQualityFindings, totalFiles);
const deadHealth = computeHealthScore(deadCodeFindings, totalFiles);
const secHealth = computeHealthScore(securityFindings, totalFiles);
const testHealth = computeHealthScore(testQualityFindings, totalFiles);
lines.push('## Health Scores\n');
lines.push('| Pillar | Score | Grade |');
lines.push('|--------|-------|-------|');
const grade = (s: number) => s >= 80 ? 'A' : s >= 60 ? 'B' : s >= 40 ? 'C' : s >= 20 ? 'D' : 'F';
lines.push(`| **Overall** | **${overallHealth}/100** | **${grade(overallHealth)}** |`);
lines.push(`| Architecture | ${archHealth}/100 | ${grade(archHealth)} |`);
lines.push(`| Code Quality | ${qualHealth}/100 | ${grade(qualHealth)} |`);
lines.push(`| Dead Code & Hygiene | ${deadHealth}/100 | ${grade(deadHealth)} |`);
if (securityFindings.length > 0) lines.push(`| Security | ${secHealth}/100 | ${grade(secHealth)} |`);
if (testQualityFindings.length > 0) lines.push(`| Test Quality | ${testHealth}/100 | ${grade(testHealth)} |`);
lines.push('');
const tagCloud = collectTagCloud(allFindings);
if (tagCloud.length > 0) {
lines.push('## Top Concern Tags\n');
lines.push('Searchable tags across all findings — use to filter `findings.json` with `jq`.\n');
for (const { tag, count } of tagCloud.slice(0, 12)) {
lines.push(`- \`${tag}\`: ${count} findings`);
}
lines.push('');
}
if (reportAnalysis) {
lines.push('## Analysis Signals\n');
lines.push(`- **Graph Signal**: ${reportAnalysis.strongestGraphSignal?.summary || 'No dominant graph signal in this scan.'}`);
lines.push(`- **AST Signal**: ${reportAnalysis.strongestAstSignal?.summary || 'No dominant AST signal in this scan.'}`);
lines.push(`- **Combined Interpretation**: ${reportAnalysis.combinedInterpretation?.summary || 'No combined interpretation available yet.'}`);
lines.push(`- **Confidence**: ${reportAnalysis.combinedInterpretation?.confidence || reportAnalysis.strongestGraphSignal?.confidence || reportAnalysis.strongestAstSignal?.confidence || 'low'}`);
const validationSummary = reportAnalysis.recommendedValidation
? `${reportAnalysis.recommendedValidation.summary} (tools: ${reportAnalysis.recommendedValidation.tools.join(' -> ')})`
: 'Use Octocode local tools to confirm the strongest signal before presenting it as fact.';
lines.push(`- **Recommended Validation**: ${validationSummary}`);
const megaFolderSignal = reportAnalysis.graphSignals.find((signal) => signal.kind === 'mega-folder-cluster');
if (megaFolderSignal) {
lines.push(`- **Structural Layout Alert**: ${megaFolderSignal.summary}`);
}
if (reportAnalysis.investigationPrompts.length > 0) {
lines.push('');
lines.push('**Investigation Prompts**');
for (const prompt of reportAnalysis.investigationPrompts.slice(0, 4)) {
lines.push(`- ${prompt}`);
}
}
lines.push('');
}
lines.push('## Architecture Health\n');
lines.push(`> ${architectureFindings.length} findings (score: ${archHealth}/100) — see [\`architecture.json\`](./architecture.json)\n`);
if (depGraph) {
lines.push(`| Metric | Value |`);
lines.push(`|--------|-------|`);
lines.push(`| Modules | ${depGraph.totalModules} |`);
lines.push(`| Import edges | ${depGraph.totalEdges} |`);
lines.push(`| Cycles | ${depGraph.cycles?.length ?? 0} |`);
lines.push(`| Critical paths | ${depGraph.criticalPaths?.length ?? 0} |`);
lines.push(`| Root modules | ${depGraph.rootsCount} |`);
lines.push(`| Leaf modules | ${depGraph.leavesCount} |`);
lines.push(`| Test-only modules | ${depGraph.testOnlyModules?.length ?? 0} |`);
lines.push(`| Unresolved imports | ${depGraph.unresolvedEdgeCount} |`);
lines.push('');
}
renderPillarCategories('architecture', architectureFindings);
if (hotFiles.length > 0) {
lines.push('## Change Risk Hotspots\n');
lines.push('Files most dangerous to change — high fan-in, complexity, or cycle membership.\n');
lines.push('| File | Risk | Fan-In | Fan-Out | Complexity | Exports | Cycle | Critical Path |');
lines.push('|------|------|--------|---------|------------|---------|-------|---------------|');
for (const hf of hotFiles.slice(0, 15)) {
lines.push(`| \`${hf.file}\` | ${hf.riskScore} | ${hf.fanIn} | ${hf.fanOut} | ${hf.complexityScore} | ${hf.exportCount} | ${hf.inCycle ? 'Y' : '-'} | ${hf.onCriticalPath ? 'Y' : '-'} |`);
}
lines.push('');
}
lines.push('## Code Quality\n');
lines.push(`> ${codeQualityFindings.length} findings (score: ${qualHealth}/100) — see [\`code-quality.json\`](./code-quality.json)\n`);
renderPillarCategories('code-quality', codeQualityFindings);
lines.push('## Dead Code & Hygiene\n');
lines.push(`> ${deadCodeFindings.length} findings (score: ${deadHealth}/100) — see [\`dead-code.json\`](./dead-code.json)\n`);
renderPillarCategories('dead-code', deadCodeFindings);
if (securityFindings.length > 0) {
lines.push('## Security\n');
lines.push(`> ${securityFindings.length} findings (score: ${secHealth}/100) — see [\`security.json\`](./security.json)\n`);
renderPillarCategories('security', securityFindings);
}
if (testQualityFindings.length > 0) {
lines.push('## Test Quality\n');
lines.push(`> ${testQualityFindings.length} findings (score: ${testHealth}/100) — see [\`test-quality.json\`](./test-quality.json)\n`);
renderPillarCategories('test-quality', testQualityFindings);
}
const topRecs = (agentOutput?.topRecommendations ?? []) as Array<{ severity: string; title: string; file: string; category: string }>;
if (topRecs.length > 0) {
lines.push('## Top Recommendations\n');
for (const rec of topRecs.slice(0, 10)) {
lines.push(`- **[${rec.severity.toUpperCase()}]** \`${rec.file}\` — ${rec.title} *(${rec.category})* `);
}
lines.push('');
}
if (outputFiles.astTrees) {
lines.push('## AST Trees (`ast-trees.txt`)\n');
lines.push('Compact indented text format — each node is `Kind[startLine:endLine]`, nesting = indentation.\n');
lines.push('```');
lines.push('SourceFile[1:152]');
lines.push(' ImportDeclaration[1]');
lines.push(' FunctionDeclaration[3:20]');
lines.push(' Block[4:19]');
lines.push(' IfStatement[5:12] ...');
lines.push('```\n');
lines.push('**Smart navigation:**\n');
lines.push('| Goal | Command |');
lines.push('|------|---------|');
lines.push('| List all files | `grep "^##" ast-trees.txt` |');
lines.push('| Find functions | `grep -E "FunctionDeclaration\\|function_declaration\\|ArrowFunction\\|arrow_function" ast-trees.txt` |');
lines.push('| Find classes | `grep -E "ClassDeclaration\\|class_declaration" ast-trees.txt` |');
lines.push('| Find control flow | `grep -E "IfStatement\\|SwitchStatement\\|ForStatement\\|WhileStatement" ast-trees.txt` |');
lines.push('| Deep nesting (>3) | `grep -E "^\\s{8,}" ast-trees.txt` |');
lines.push('| Truncated subtrees | `grep "\\.\\.\\.$" ast-trees.txt` |');
lines.push('| Large spans (regex) | Use pattern `\\[(\\d+):(\\d+)\\]` — subtract to find span size |');
lines.push('');
}
lines.push('## Output Files\n');
lines.push('| File | Size | Description |');
lines.push('|------|------|-------------|');
const descriptions: Record<string, string> = {
summary: 'Scan metadata, agent output, parse errors',
architecture: 'Dependency graph, cycles, critical paths, architecture findings',
codeQuality: 'Duplicate detection, complexity, god modules/functions',
deadCode: 'Dead files/exports/re-exports, unused deps, boundary violations',
fileInventory: 'Per-file function/flow/dependency details',
findings: 'All findings across all categories (master list)',
graph: 'Mermaid dependency graph',
astTrees: 'AST tree snapshots (compact indented text — grep/regex friendly)',
summaryMd: 'This file — human-readable overview',
};
for (const [key, file] of Object.entries(outputFiles)) {
let size = '—';
try { size = formatFileSize(fs.statSync(path.join(dir, file)).size); } catch { size = '—'; }
lines.push(`| [\`${file}\`](./${file}) | ${size} | ${descriptions[key] || key} |`);
}
lines.push('');
if (report.parseErrors?.length > 0) {
lines.push('## Parse Errors\n');
lines.push(`${report.parseErrors.length} file(s) failed to parse:\n`);
for (const err of report.parseErrors.slice(0, 10)) {
lines.push(`- \`${err.file}\`: ${err.message}`);
}
lines.push('');
}
return lines.join('\n');
}
|