|
| 1 | +import { |
| 2 | + findDistinctCallers, |
| 3 | + findFileNodes, |
| 4 | + findImportDependents, |
| 5 | + findImportSources, |
| 6 | + findImportTargets, |
| 7 | + findNodesByFile, |
| 8 | + openReadonlyOrFail, |
| 9 | +} from '../../db/index.js'; |
| 10 | +import { isTestFile } from '../../infrastructure/test-filter.js'; |
| 11 | + |
| 12 | +/** Symbol kinds meaningful for a file brief — excludes parameters, properties, constants. */ |
| 13 | +const BRIEF_KINDS = new Set([ |
| 14 | + 'function', |
| 15 | + 'method', |
| 16 | + 'class', |
| 17 | + 'interface', |
| 18 | + 'type', |
| 19 | + 'struct', |
| 20 | + 'enum', |
| 21 | + 'trait', |
| 22 | + 'record', |
| 23 | + 'module', |
| 24 | +]); |
| 25 | + |
| 26 | +/** |
| 27 | + * Compute file risk tier from symbol roles and max fan-in. |
| 28 | + * @param {{ role: string|null, callerCount: number }[]} symbols |
| 29 | + * @returns {'high'|'medium'|'low'} |
| 30 | + */ |
| 31 | +function computeRiskTier(symbols) { |
| 32 | + let maxCallers = 0; |
| 33 | + let hasCoreRole = false; |
| 34 | + for (const s of symbols) { |
| 35 | + if (s.callerCount > maxCallers) maxCallers = s.callerCount; |
| 36 | + if (s.role === 'core') hasCoreRole = true; |
| 37 | + } |
| 38 | + if (maxCallers >= 10 || hasCoreRole) return 'high'; |
| 39 | + if (maxCallers >= 3) return 'medium'; |
| 40 | + return 'low'; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * BFS to count transitive callers for a single node. |
| 45 | + * Lightweight variant — only counts, does not collect details. |
| 46 | + */ |
| 47 | +function countTransitiveCallers(db, startId, noTests, maxDepth = 5) { |
| 48 | + const visited = new Set([startId]); |
| 49 | + let frontier = [startId]; |
| 50 | + |
| 51 | + for (let d = 1; d <= maxDepth; d++) { |
| 52 | + const nextFrontier = []; |
| 53 | + for (const fid of frontier) { |
| 54 | + const callers = findDistinctCallers(db, fid); |
| 55 | + for (const c of callers) { |
| 56 | + if (!visited.has(c.id) && (!noTests || !isTestFile(c.file))) { |
| 57 | + visited.add(c.id); |
| 58 | + nextFrontier.push(c.id); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + frontier = nextFrontier; |
| 63 | + if (frontier.length === 0) break; |
| 64 | + } |
| 65 | + |
| 66 | + return visited.size - 1; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Count transitive file-level import dependents via BFS. |
| 71 | + * Depth-bounded to match countTransitiveCallers and keep hook latency predictable. |
| 72 | + */ |
| 73 | +function countTransitiveImporters(db, fileNodeIds, noTests, maxDepth = 5) { |
| 74 | + const visited = new Set(fileNodeIds); |
| 75 | + let frontier = [...fileNodeIds]; |
| 76 | + |
| 77 | + for (let d = 1; d <= maxDepth; d++) { |
| 78 | + const nextFrontier = []; |
| 79 | + for (const current of frontier) { |
| 80 | + const dependents = findImportDependents(db, current); |
| 81 | + for (const dep of dependents) { |
| 82 | + if (!visited.has(dep.id) && (!noTests || !isTestFile(dep.file))) { |
| 83 | + visited.add(dep.id); |
| 84 | + nextFrontier.push(dep.id); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + frontier = nextFrontier; |
| 89 | + if (frontier.length === 0) break; |
| 90 | + } |
| 91 | + |
| 92 | + return visited.size - fileNodeIds.length; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Produce a token-efficient file brief: symbols with roles and caller counts, |
| 97 | + * importer info with transitive count, and file risk tier. |
| 98 | + * |
| 99 | + * @param {string} file - File path (partial match) |
| 100 | + * @param {string} customDbPath - Path to graph.db |
| 101 | + * @param {{ noTests?: boolean }} opts |
| 102 | + * @returns {{ file: string, results: object[] }} |
| 103 | + */ |
| 104 | +export function briefData(file, customDbPath, opts = {}) { |
| 105 | + const db = openReadonlyOrFail(customDbPath); |
| 106 | + try { |
| 107 | + const noTests = opts.noTests || false; |
| 108 | + const fileNodes = findFileNodes(db, `%${file}%`); |
| 109 | + if (fileNodes.length === 0) { |
| 110 | + return { file, results: [] }; |
| 111 | + } |
| 112 | + |
| 113 | + const results = fileNodes.map((fn) => { |
| 114 | + // Direct importers |
| 115 | + let importedBy = findImportSources(db, fn.id); |
| 116 | + if (noTests) importedBy = importedBy.filter((i) => !isTestFile(i.file)); |
| 117 | + const directImporters = [...new Set(importedBy.map((i) => i.file))]; |
| 118 | + |
| 119 | + // Transitive importer count |
| 120 | + const totalImporterCount = countTransitiveImporters(db, [fn.id], noTests); |
| 121 | + |
| 122 | + // Direct imports |
| 123 | + let importsTo = findImportTargets(db, fn.id); |
| 124 | + if (noTests) importsTo = importsTo.filter((i) => !isTestFile(i.file)); |
| 125 | + |
| 126 | + // Symbol definitions with roles and caller counts |
| 127 | + const defs = findNodesByFile(db, fn.file).filter((d) => BRIEF_KINDS.has(d.kind)); |
| 128 | + const symbols = defs.map((d) => { |
| 129 | + const callerCount = countTransitiveCallers(db, d.id, noTests); |
| 130 | + return { |
| 131 | + name: d.name, |
| 132 | + kind: d.kind, |
| 133 | + line: d.line, |
| 134 | + role: d.role || null, |
| 135 | + callerCount, |
| 136 | + }; |
| 137 | + }); |
| 138 | + |
| 139 | + const riskTier = computeRiskTier(symbols); |
| 140 | + |
| 141 | + return { |
| 142 | + file: fn.file, |
| 143 | + risk: riskTier, |
| 144 | + imports: importsTo.map((i) => i.file), |
| 145 | + importedBy: directImporters, |
| 146 | + totalImporterCount, |
| 147 | + symbols, |
| 148 | + }; |
| 149 | + }); |
| 150 | + |
| 151 | + return { file, results }; |
| 152 | + } finally { |
| 153 | + db.close(); |
| 154 | + } |
| 155 | +} |
0 commit comments