-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbrief.ts
More file actions
176 lines (160 loc) · 5.13 KB
/
brief.ts
File metadata and controls
176 lines (160 loc) · 5.13 KB
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
import {
findDistinctCallers,
findFileNodes,
findImportDependents,
findImportSources,
findImportTargets,
findNodesByFile,
openReadonlyOrFail,
} from '../../db/index.js';
import { loadConfig } from '../../infrastructure/config.js';
import { isTestFile } from '../../infrastructure/test-filter.js';
import type { BetterSqlite3Database, ImportEdgeRow, NodeRow, RelatedNodeRow } from '../../types.js';
/** Symbol kinds meaningful for a file brief — excludes parameters, properties, constants. */
const BRIEF_KINDS = new Set([
'function',
'method',
'class',
'interface',
'type',
'struct',
'enum',
'trait',
'record',
'module',
]);
/**
* Compute file risk tier from symbol roles and max fan-in.
*/
function computeRiskTier(
symbols: Array<{ role: string | null; callerCount: number }>,
highThreshold = 10,
mediumThreshold = 3,
): 'high' | 'medium' | 'low' {
let maxCallers = 0;
let hasCoreRole = false;
for (const s of symbols) {
if (s.callerCount > maxCallers) maxCallers = s.callerCount;
if (s.role === 'core') hasCoreRole = true;
}
if (maxCallers >= highThreshold || hasCoreRole) return 'high';
if (maxCallers >= mediumThreshold) return 'medium';
return 'low';
}
/**
* BFS to count transitive callers for a single node.
* Lightweight variant — only counts, does not collect details.
*/
function countTransitiveCallers(
db: BetterSqlite3Database,
startId: number,
noTests: boolean,
maxDepth = 5,
): number {
const visited = new Set([startId]);
let frontier = [startId];
for (let d = 1; d <= maxDepth; d++) {
const nextFrontier: number[] = [];
for (const fid of frontier) {
const callers = findDistinctCallers(db, fid) as RelatedNodeRow[];
for (const c of callers) {
if (!visited.has(c.id) && (!noTests || !isTestFile(c.file))) {
visited.add(c.id);
nextFrontier.push(c.id);
}
}
}
frontier = nextFrontier;
if (frontier.length === 0) break;
}
return visited.size - 1;
}
/**
* Count transitive file-level import dependents via BFS.
* Depth-bounded to match countTransitiveCallers and keep hook latency predictable.
*/
function countTransitiveImporters(
db: BetterSqlite3Database,
fileNodeIds: number[],
noTests: boolean,
maxDepth = 5,
): number {
const visited = new Set(fileNodeIds);
let frontier = [...fileNodeIds];
for (let d = 1; d <= maxDepth; d++) {
const nextFrontier: number[] = [];
for (const current of frontier) {
const dependents = findImportDependents(db, current) as RelatedNodeRow[];
for (const dep of dependents) {
if (!visited.has(dep.id) && (!noTests || !isTestFile(dep.file))) {
visited.add(dep.id);
nextFrontier.push(dep.id);
}
}
}
frontier = nextFrontier;
if (frontier.length === 0) break;
}
return visited.size - fileNodeIds.length;
}
/**
* Produce a token-efficient file brief: symbols with roles and caller counts,
* importer info with transitive count, and file risk tier.
*/
export function briefData(
file: string,
customDbPath: string,
// biome-ignore lint/suspicious/noExplicitAny: config shape is dynamic
opts: { noTests?: boolean; config?: any } = {},
) {
const db = openReadonlyOrFail(customDbPath);
try {
const noTests = opts.noTests || false;
const config = opts.config || loadConfig();
const callerDepth = config.analysis?.briefCallerDepth ?? 5;
const importerDepth = config.analysis?.briefImporterDepth ?? 5;
const highRiskCallers = config.analysis?.briefHighRiskCallers ?? 10;
const mediumRiskCallers = config.analysis?.briefMediumRiskCallers ?? 3;
const fileNodes = findFileNodes(db, `%${file}%`) as NodeRow[];
if (fileNodes.length === 0) {
return { file, results: [] };
}
const results = fileNodes.map((fn) => {
// Direct importers
let importedBy = findImportSources(db, fn.id) as ImportEdgeRow[];
if (noTests) importedBy = importedBy.filter((i) => !isTestFile(i.file));
const directImporters = [...new Set(importedBy.map((i) => i.file))];
// Transitive importer count
const totalImporterCount = countTransitiveImporters(db, [fn.id], noTests, importerDepth);
// Direct imports
let importsTo = findImportTargets(db, fn.id) as ImportEdgeRow[];
if (noTests) importsTo = importsTo.filter((i) => !isTestFile(i.file));
// Symbol definitions with roles and caller counts
const defs = (findNodesByFile(db, fn.file) as NodeRow[]).filter((d) =>
BRIEF_KINDS.has(d.kind),
);
const symbols = defs.map((d) => {
const callerCount = countTransitiveCallers(db, d.id, noTests, callerDepth);
return {
name: d.name,
kind: d.kind,
line: d.line,
role: d.role || null,
callerCount,
};
});
const riskTier = computeRiskTier(symbols, highRiskCallers, mediumRiskCallers);
return {
file: fn.file,
risk: riskTier,
imports: importsTo.map((i) => i.file),
importedBy: directImporters,
totalImporterCount,
symbols,
};
});
return { file, results };
} finally {
db.close();
}
}