@aiready/context-analyzer 0.9.26 → 0.9.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +11 -11
- package/.turbo/turbo-test.log +21 -20
- package/dist/chunk-FYI56A5M.mjs +1892 -0
- package/dist/chunk-I77HFFZU.mjs +1876 -0
- package/dist/chunk-KYSZF5N6.mjs +1876 -0
- package/dist/chunk-M64RHH4D.mjs +1896 -0
- package/dist/chunk-OP4G6GLN.mjs +1876 -0
- package/dist/chunk-P3T3H27S.mjs +1895 -0
- package/dist/chunk-VBWXHKGD.mjs +1895 -0
- package/dist/cli.js +197 -36
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +6 -2
- package/dist/index.d.ts +6 -2
- package/dist/index.js +202 -32
- package/dist/index.mjs +1 -1
- package/package.json +2 -2
- package/src/analyzer.ts +168 -34
- package/src/index.ts +1 -1
- package/src/scoring.ts +28 -1
|
@@ -0,0 +1,1892 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { scanFiles, readFileContent } from "@aiready/core";
|
|
3
|
+
|
|
4
|
+
// src/analyzer.ts
|
|
5
|
+
import { estimateTokens, parseFileExports } from "@aiready/core";
|
|
6
|
+
|
|
7
|
+
// src/semantic-analysis.ts
|
|
8
|
+
function buildCoUsageMatrix(graph) {
|
|
9
|
+
const coUsageMatrix = /* @__PURE__ */ new Map();
|
|
10
|
+
for (const [sourceFile, node] of graph.nodes) {
|
|
11
|
+
const imports = node.imports;
|
|
12
|
+
for (let i = 0; i < imports.length; i++) {
|
|
13
|
+
const fileA = imports[i];
|
|
14
|
+
if (!coUsageMatrix.has(fileA)) {
|
|
15
|
+
coUsageMatrix.set(fileA, /* @__PURE__ */ new Map());
|
|
16
|
+
}
|
|
17
|
+
for (let j = i + 1; j < imports.length; j++) {
|
|
18
|
+
const fileB = imports[j];
|
|
19
|
+
const fileAUsage = coUsageMatrix.get(fileA);
|
|
20
|
+
fileAUsage.set(fileB, (fileAUsage.get(fileB) || 0) + 1);
|
|
21
|
+
if (!coUsageMatrix.has(fileB)) {
|
|
22
|
+
coUsageMatrix.set(fileB, /* @__PURE__ */ new Map());
|
|
23
|
+
}
|
|
24
|
+
const fileBUsage = coUsageMatrix.get(fileB);
|
|
25
|
+
fileBUsage.set(fileA, (fileBUsage.get(fileA) || 0) + 1);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return coUsageMatrix;
|
|
30
|
+
}
|
|
31
|
+
function buildTypeGraph(graph) {
|
|
32
|
+
const typeGraph = /* @__PURE__ */ new Map();
|
|
33
|
+
for (const [file, node] of graph.nodes) {
|
|
34
|
+
for (const exp of node.exports) {
|
|
35
|
+
if (exp.typeReferences) {
|
|
36
|
+
for (const typeRef of exp.typeReferences) {
|
|
37
|
+
if (!typeGraph.has(typeRef)) {
|
|
38
|
+
typeGraph.set(typeRef, /* @__PURE__ */ new Set());
|
|
39
|
+
}
|
|
40
|
+
typeGraph.get(typeRef).add(file);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return typeGraph;
|
|
46
|
+
}
|
|
47
|
+
function findSemanticClusters(coUsageMatrix, minCoUsage = 3) {
|
|
48
|
+
const clusters = /* @__PURE__ */ new Map();
|
|
49
|
+
const visited = /* @__PURE__ */ new Set();
|
|
50
|
+
for (const [file, coUsages] of coUsageMatrix) {
|
|
51
|
+
if (visited.has(file)) continue;
|
|
52
|
+
const cluster = [file];
|
|
53
|
+
visited.add(file);
|
|
54
|
+
for (const [relatedFile, count] of coUsages) {
|
|
55
|
+
if (count >= minCoUsage && !visited.has(relatedFile)) {
|
|
56
|
+
cluster.push(relatedFile);
|
|
57
|
+
visited.add(relatedFile);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (cluster.length > 1) {
|
|
61
|
+
clusters.set(file, cluster);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return clusters;
|
|
65
|
+
}
|
|
66
|
+
function calculateDomainConfidence(signals) {
|
|
67
|
+
const weights = {
|
|
68
|
+
coUsage: 0.35,
|
|
69
|
+
// Strongest signal: actual usage patterns
|
|
70
|
+
typeReference: 0.3,
|
|
71
|
+
// Strong signal: shared types
|
|
72
|
+
exportName: 0.15,
|
|
73
|
+
// Medium signal: identifier semantics
|
|
74
|
+
importPath: 0.1,
|
|
75
|
+
// Weaker signal: path structure
|
|
76
|
+
folderStructure: 0.1
|
|
77
|
+
// Weakest signal: organization convention
|
|
78
|
+
};
|
|
79
|
+
let confidence = 0;
|
|
80
|
+
if (signals.coUsage) confidence += weights.coUsage;
|
|
81
|
+
if (signals.typeReference) confidence += weights.typeReference;
|
|
82
|
+
if (signals.exportName) confidence += weights.exportName;
|
|
83
|
+
if (signals.importPath) confidence += weights.importPath;
|
|
84
|
+
if (signals.folderStructure) confidence += weights.folderStructure;
|
|
85
|
+
return confidence;
|
|
86
|
+
}
|
|
87
|
+
function inferDomainFromSemantics(file, exportName, graph, coUsageMatrix, typeGraph, exportTypeRefs) {
|
|
88
|
+
const assignments = [];
|
|
89
|
+
const domainSignals = /* @__PURE__ */ new Map();
|
|
90
|
+
const coUsages = coUsageMatrix.get(file) || /* @__PURE__ */ new Map();
|
|
91
|
+
const strongCoUsages = Array.from(coUsages.entries()).filter(([_, count]) => count >= 3).map(([coFile]) => coFile);
|
|
92
|
+
for (const coFile of strongCoUsages) {
|
|
93
|
+
const coNode = graph.nodes.get(coFile);
|
|
94
|
+
if (coNode) {
|
|
95
|
+
for (const exp of coNode.exports) {
|
|
96
|
+
if (exp.inferredDomain && exp.inferredDomain !== "unknown") {
|
|
97
|
+
const domain = exp.inferredDomain;
|
|
98
|
+
if (!domainSignals.has(domain)) {
|
|
99
|
+
domainSignals.set(domain, {
|
|
100
|
+
coUsage: false,
|
|
101
|
+
typeReference: false,
|
|
102
|
+
exportName: false,
|
|
103
|
+
importPath: false,
|
|
104
|
+
folderStructure: false
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
domainSignals.get(domain).coUsage = true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (exportTypeRefs) {
|
|
113
|
+
for (const typeRef of exportTypeRefs) {
|
|
114
|
+
const filesWithType = typeGraph.get(typeRef);
|
|
115
|
+
if (filesWithType) {
|
|
116
|
+
for (const typeFile of filesWithType) {
|
|
117
|
+
if (typeFile !== file) {
|
|
118
|
+
const typeNode = graph.nodes.get(typeFile);
|
|
119
|
+
if (typeNode) {
|
|
120
|
+
for (const exp of typeNode.exports) {
|
|
121
|
+
if (exp.inferredDomain && exp.inferredDomain !== "unknown") {
|
|
122
|
+
const domain = exp.inferredDomain;
|
|
123
|
+
if (!domainSignals.has(domain)) {
|
|
124
|
+
domainSignals.set(domain, {
|
|
125
|
+
coUsage: false,
|
|
126
|
+
typeReference: false,
|
|
127
|
+
exportName: false,
|
|
128
|
+
importPath: false,
|
|
129
|
+
folderStructure: false
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
domainSignals.get(domain).typeReference = true;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
for (const [domain, signals] of domainSignals) {
|
|
142
|
+
const confidence = calculateDomainConfidence(signals);
|
|
143
|
+
if (confidence >= 0.3) {
|
|
144
|
+
assignments.push({ domain, confidence, signals });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
assignments.sort((a, b) => b.confidence - a.confidence);
|
|
148
|
+
return assignments;
|
|
149
|
+
}
|
|
150
|
+
function getCoUsageData(file, coUsageMatrix) {
|
|
151
|
+
const coImportedWith = coUsageMatrix.get(file) || /* @__PURE__ */ new Map();
|
|
152
|
+
const sharedImporters = [];
|
|
153
|
+
return {
|
|
154
|
+
file,
|
|
155
|
+
coImportedWith,
|
|
156
|
+
sharedImporters
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function findConsolidationCandidates(graph, coUsageMatrix, typeGraph, minCoUsage = 5, minSharedTypes = 2) {
|
|
160
|
+
const candidates = [];
|
|
161
|
+
for (const [fileA, coUsages] of coUsageMatrix) {
|
|
162
|
+
const nodeA = graph.nodes.get(fileA);
|
|
163
|
+
if (!nodeA) continue;
|
|
164
|
+
for (const [fileB, coUsageCount] of coUsages) {
|
|
165
|
+
if (fileB <= fileA) continue;
|
|
166
|
+
if (coUsageCount < minCoUsage) continue;
|
|
167
|
+
const nodeB = graph.nodes.get(fileB);
|
|
168
|
+
if (!nodeB) continue;
|
|
169
|
+
const typesA = new Set(nodeA.exports.flatMap((e) => e.typeReferences || []));
|
|
170
|
+
const typesB = new Set(nodeB.exports.flatMap((e) => e.typeReferences || []));
|
|
171
|
+
const sharedTypes = Array.from(typesA).filter((t) => typesB.has(t));
|
|
172
|
+
if (sharedTypes.length >= minSharedTypes) {
|
|
173
|
+
const strength = coUsageCount / 10 + sharedTypes.length / 5;
|
|
174
|
+
candidates.push({
|
|
175
|
+
files: [fileA, fileB],
|
|
176
|
+
reason: `High co-usage (${coUsageCount}x) and ${sharedTypes.length} shared types`,
|
|
177
|
+
strength
|
|
178
|
+
});
|
|
179
|
+
} else if (coUsageCount >= minCoUsage * 2) {
|
|
180
|
+
const strength = coUsageCount / 10;
|
|
181
|
+
candidates.push({
|
|
182
|
+
files: [fileA, fileB],
|
|
183
|
+
reason: `Very high co-usage (${coUsageCount}x)`,
|
|
184
|
+
strength
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
candidates.sort((a, b) => b.strength - a.strength);
|
|
190
|
+
return candidates;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// src/analyzer.ts
|
|
194
|
+
function extractDomainKeywordsFromPaths(files) {
|
|
195
|
+
const folderNames = /* @__PURE__ */ new Set();
|
|
196
|
+
for (const { file } of files) {
|
|
197
|
+
const segments = file.split("/");
|
|
198
|
+
const skipFolders = /* @__PURE__ */ new Set(["src", "lib", "dist", "build", "node_modules", "test", "tests", "__tests__", "spec", "e2e", "scripts", "components", "utils", "helpers", "util", "helper", "api", "apis"]);
|
|
199
|
+
for (const segment of segments) {
|
|
200
|
+
const normalized = segment.toLowerCase();
|
|
201
|
+
if (normalized && !skipFolders.has(normalized) && !normalized.includes(".")) {
|
|
202
|
+
const singular = singularize(normalized);
|
|
203
|
+
folderNames.add(singular);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return Array.from(folderNames);
|
|
208
|
+
}
|
|
209
|
+
function singularize(word) {
|
|
210
|
+
const irregulars = {
|
|
211
|
+
people: "person",
|
|
212
|
+
children: "child",
|
|
213
|
+
men: "man",
|
|
214
|
+
women: "woman"
|
|
215
|
+
};
|
|
216
|
+
if (irregulars[word]) {
|
|
217
|
+
return irregulars[word];
|
|
218
|
+
}
|
|
219
|
+
if (word.endsWith("ies")) {
|
|
220
|
+
return word.slice(0, -3) + "y";
|
|
221
|
+
}
|
|
222
|
+
if (word.endsWith("ses")) {
|
|
223
|
+
return word.slice(0, -2);
|
|
224
|
+
}
|
|
225
|
+
if (word.endsWith("s") && word.length > 3) {
|
|
226
|
+
return word.slice(0, -1);
|
|
227
|
+
}
|
|
228
|
+
return word;
|
|
229
|
+
}
|
|
230
|
+
function buildDependencyGraph(files) {
|
|
231
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
232
|
+
const edges = /* @__PURE__ */ new Map();
|
|
233
|
+
const autoDetectedKeywords = extractDomainKeywordsFromPaths(files);
|
|
234
|
+
for (const { file, content } of files) {
|
|
235
|
+
const imports = extractImportsFromContent(content);
|
|
236
|
+
const exports = extractExportsWithAST(content, file, { domainKeywords: autoDetectedKeywords }, imports);
|
|
237
|
+
const tokenCost = estimateTokens(content);
|
|
238
|
+
const linesOfCode = content.split("\n").length;
|
|
239
|
+
nodes.set(file, {
|
|
240
|
+
file,
|
|
241
|
+
imports,
|
|
242
|
+
exports,
|
|
243
|
+
tokenCost,
|
|
244
|
+
linesOfCode
|
|
245
|
+
});
|
|
246
|
+
edges.set(file, new Set(imports));
|
|
247
|
+
}
|
|
248
|
+
const graph = { nodes, edges };
|
|
249
|
+
const coUsageMatrix = buildCoUsageMatrix(graph);
|
|
250
|
+
const typeGraph = buildTypeGraph(graph);
|
|
251
|
+
graph.coUsageMatrix = coUsageMatrix;
|
|
252
|
+
graph.typeGraph = typeGraph;
|
|
253
|
+
for (const [file, node] of nodes) {
|
|
254
|
+
for (const exp of node.exports) {
|
|
255
|
+
const semanticAssignments = inferDomainFromSemantics(
|
|
256
|
+
file,
|
|
257
|
+
exp.name,
|
|
258
|
+
graph,
|
|
259
|
+
coUsageMatrix,
|
|
260
|
+
typeGraph,
|
|
261
|
+
exp.typeReferences
|
|
262
|
+
);
|
|
263
|
+
exp.domains = semanticAssignments;
|
|
264
|
+
if (semanticAssignments.length > 0) {
|
|
265
|
+
exp.inferredDomain = semanticAssignments[0].domain;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return graph;
|
|
270
|
+
}
|
|
271
|
+
function extractImportsFromContent(content) {
|
|
272
|
+
const imports = [];
|
|
273
|
+
const patterns = [
|
|
274
|
+
/import\s+.*?\s+from\s+['"](.+?)['"]/g,
|
|
275
|
+
// import ... from '...'
|
|
276
|
+
/import\s+['"](.+?)['"]/g,
|
|
277
|
+
// import '...'
|
|
278
|
+
/require\(['"](.+?)['"]\)/g
|
|
279
|
+
// require('...')
|
|
280
|
+
];
|
|
281
|
+
for (const pattern of patterns) {
|
|
282
|
+
let match;
|
|
283
|
+
while ((match = pattern.exec(content)) !== null) {
|
|
284
|
+
const importPath = match[1];
|
|
285
|
+
if (importPath && !importPath.startsWith("node:")) {
|
|
286
|
+
imports.push(importPath);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return [...new Set(imports)];
|
|
291
|
+
}
|
|
292
|
+
function calculateImportDepth(file, graph, visited = /* @__PURE__ */ new Set(), depth = 0) {
|
|
293
|
+
if (visited.has(file)) {
|
|
294
|
+
return depth;
|
|
295
|
+
}
|
|
296
|
+
const dependencies = graph.edges.get(file);
|
|
297
|
+
if (!dependencies || dependencies.size === 0) {
|
|
298
|
+
return depth;
|
|
299
|
+
}
|
|
300
|
+
visited.add(file);
|
|
301
|
+
let maxDepth = depth;
|
|
302
|
+
for (const dep of dependencies) {
|
|
303
|
+
const depDepth = calculateImportDepth(dep, graph, visited, depth + 1);
|
|
304
|
+
maxDepth = Math.max(maxDepth, depDepth);
|
|
305
|
+
}
|
|
306
|
+
visited.delete(file);
|
|
307
|
+
return maxDepth;
|
|
308
|
+
}
|
|
309
|
+
function getTransitiveDependencies(file, graph, visited = /* @__PURE__ */ new Set()) {
|
|
310
|
+
if (visited.has(file)) {
|
|
311
|
+
return [];
|
|
312
|
+
}
|
|
313
|
+
visited.add(file);
|
|
314
|
+
const dependencies = graph.edges.get(file);
|
|
315
|
+
if (!dependencies || dependencies.size === 0) {
|
|
316
|
+
return [];
|
|
317
|
+
}
|
|
318
|
+
const allDeps = [];
|
|
319
|
+
for (const dep of dependencies) {
|
|
320
|
+
allDeps.push(dep);
|
|
321
|
+
allDeps.push(...getTransitiveDependencies(dep, graph, visited));
|
|
322
|
+
}
|
|
323
|
+
return [...new Set(allDeps)];
|
|
324
|
+
}
|
|
325
|
+
function calculateContextBudget(file, graph) {
|
|
326
|
+
const node = graph.nodes.get(file);
|
|
327
|
+
if (!node) return 0;
|
|
328
|
+
let totalTokens = node.tokenCost;
|
|
329
|
+
const deps = getTransitiveDependencies(file, graph);
|
|
330
|
+
for (const dep of deps) {
|
|
331
|
+
const depNode = graph.nodes.get(dep);
|
|
332
|
+
if (depNode) {
|
|
333
|
+
totalTokens += depNode.tokenCost;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return totalTokens;
|
|
337
|
+
}
|
|
338
|
+
function detectCircularDependencies(graph) {
|
|
339
|
+
const cycles = [];
|
|
340
|
+
const visited = /* @__PURE__ */ new Set();
|
|
341
|
+
const recursionStack = /* @__PURE__ */ new Set();
|
|
342
|
+
function dfs(file, path) {
|
|
343
|
+
if (recursionStack.has(file)) {
|
|
344
|
+
const cycleStart = path.indexOf(file);
|
|
345
|
+
if (cycleStart !== -1) {
|
|
346
|
+
cycles.push([...path.slice(cycleStart), file]);
|
|
347
|
+
}
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (visited.has(file)) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
visited.add(file);
|
|
354
|
+
recursionStack.add(file);
|
|
355
|
+
path.push(file);
|
|
356
|
+
const dependencies = graph.edges.get(file);
|
|
357
|
+
if (dependencies) {
|
|
358
|
+
for (const dep of dependencies) {
|
|
359
|
+
dfs(dep, [...path]);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
recursionStack.delete(file);
|
|
363
|
+
}
|
|
364
|
+
for (const file of graph.nodes.keys()) {
|
|
365
|
+
if (!visited.has(file)) {
|
|
366
|
+
dfs(file, []);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return cycles;
|
|
370
|
+
}
|
|
371
|
+
function calculateCohesion(exports, filePath, options) {
|
|
372
|
+
return calculateEnhancedCohesion(exports, filePath, options);
|
|
373
|
+
}
|
|
374
|
+
function isTestFile(filePath) {
|
|
375
|
+
const lower = filePath.toLowerCase();
|
|
376
|
+
return lower.includes("test") || lower.includes("spec") || lower.includes("mock") || lower.includes("fixture") || lower.includes("__tests__") || lower.includes(".test.") || lower.includes(".spec.");
|
|
377
|
+
}
|
|
378
|
+
function calculateFragmentation(files, domain, options) {
|
|
379
|
+
if (files.length <= 1) return 0;
|
|
380
|
+
const directories = new Set(files.map((f) => f.split("/").slice(0, -1).join("/")));
|
|
381
|
+
const uniqueDirs = directories.size;
|
|
382
|
+
if (options?.useLogScale) {
|
|
383
|
+
if (uniqueDirs <= 1) return 0;
|
|
384
|
+
const total = files.length;
|
|
385
|
+
const base = options.logBase || Math.E;
|
|
386
|
+
const num = Math.log(uniqueDirs) / Math.log(base);
|
|
387
|
+
const den = Math.log(total) / Math.log(base);
|
|
388
|
+
return den > 0 ? num / den : 0;
|
|
389
|
+
}
|
|
390
|
+
return (uniqueDirs - 1) / (files.length - 1);
|
|
391
|
+
}
|
|
392
|
+
function calculatePathEntropy(files) {
|
|
393
|
+
if (!files || files.length === 0) return 0;
|
|
394
|
+
const dirCounts = /* @__PURE__ */ new Map();
|
|
395
|
+
for (const f of files) {
|
|
396
|
+
const dir = f.split("/").slice(0, -1).join("/") || ".";
|
|
397
|
+
dirCounts.set(dir, (dirCounts.get(dir) || 0) + 1);
|
|
398
|
+
}
|
|
399
|
+
const counts = Array.from(dirCounts.values());
|
|
400
|
+
if (counts.length <= 1) return 0;
|
|
401
|
+
const total = counts.reduce((s, v) => s + v, 0);
|
|
402
|
+
let entropy = 0;
|
|
403
|
+
for (const count of counts) {
|
|
404
|
+
const prob = count / total;
|
|
405
|
+
entropy -= prob * Math.log2(prob);
|
|
406
|
+
}
|
|
407
|
+
const maxEntropy = Math.log2(counts.length);
|
|
408
|
+
return maxEntropy > 0 ? entropy / maxEntropy : 0;
|
|
409
|
+
}
|
|
410
|
+
function calculateDirectoryDistance(files) {
|
|
411
|
+
if (!files || files.length <= 1) return 0;
|
|
412
|
+
function pathSegments(p) {
|
|
413
|
+
return p.split("/").filter(Boolean);
|
|
414
|
+
}
|
|
415
|
+
function commonAncestorDepth(a, b) {
|
|
416
|
+
const minLen = Math.min(a.length, b.length);
|
|
417
|
+
let i = 0;
|
|
418
|
+
while (i < minLen && a[i] === b[i]) i++;
|
|
419
|
+
return i;
|
|
420
|
+
}
|
|
421
|
+
let totalNormalized = 0;
|
|
422
|
+
let comparisons = 0;
|
|
423
|
+
for (let i = 0; i < files.length; i++) {
|
|
424
|
+
for (let j = i + 1; j < files.length; j++) {
|
|
425
|
+
const segA = pathSegments(files[i]);
|
|
426
|
+
const segB = pathSegments(files[j]);
|
|
427
|
+
const shared = commonAncestorDepth(segA, segB);
|
|
428
|
+
const maxDepth = Math.max(segA.length, segB.length);
|
|
429
|
+
const normalizedShared = maxDepth > 0 ? shared / maxDepth : 0;
|
|
430
|
+
totalNormalized += 1 - normalizedShared;
|
|
431
|
+
comparisons++;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return comparisons > 0 ? totalNormalized / comparisons : 0;
|
|
435
|
+
}
|
|
436
|
+
function detectModuleClusters(graph, options) {
|
|
437
|
+
const domainMap = /* @__PURE__ */ new Map();
|
|
438
|
+
for (const [file, node] of graph.nodes.entries()) {
|
|
439
|
+
const domains = node.exports.map((e) => e.inferredDomain || "unknown");
|
|
440
|
+
const primaryDomain = domains[0] || "unknown";
|
|
441
|
+
if (!domainMap.has(primaryDomain)) {
|
|
442
|
+
domainMap.set(primaryDomain, []);
|
|
443
|
+
}
|
|
444
|
+
domainMap.get(primaryDomain).push(file);
|
|
445
|
+
}
|
|
446
|
+
const clusters = [];
|
|
447
|
+
for (const [domain, files] of domainMap.entries()) {
|
|
448
|
+
if (files.length < 2) continue;
|
|
449
|
+
const totalTokens = files.reduce((sum, file) => {
|
|
450
|
+
const node = graph.nodes.get(file);
|
|
451
|
+
return sum + (node?.tokenCost || 0);
|
|
452
|
+
}, 0);
|
|
453
|
+
const baseFragmentation = calculateFragmentation(files, domain, { useLogScale: !!options?.useLogScale });
|
|
454
|
+
let importSimilarityTotal = 0;
|
|
455
|
+
let importComparisons = 0;
|
|
456
|
+
for (let i = 0; i < files.length; i++) {
|
|
457
|
+
for (let j = i + 1; j < files.length; j++) {
|
|
458
|
+
const f1 = files[i];
|
|
459
|
+
const f2 = files[j];
|
|
460
|
+
const n1 = graph.nodes.get(f1)?.imports || [];
|
|
461
|
+
const n2 = graph.nodes.get(f2)?.imports || [];
|
|
462
|
+
const similarity = n1.length === 0 && n2.length === 0 ? 0 : calculateJaccardSimilarity(n1, n2);
|
|
463
|
+
importSimilarityTotal += similarity;
|
|
464
|
+
importComparisons++;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
const importCohesion = importComparisons > 0 ? importSimilarityTotal / importComparisons : 0;
|
|
468
|
+
const couplingDiscountFactor = 1 - 0.2 * importCohesion;
|
|
469
|
+
const fragmentationScore = baseFragmentation * couplingDiscountFactor;
|
|
470
|
+
const pathEntropy = calculatePathEntropy(files);
|
|
471
|
+
const directoryDistance = calculateDirectoryDistance(files);
|
|
472
|
+
const avgCohesion = files.reduce((sum, file) => {
|
|
473
|
+
const node = graph.nodes.get(file);
|
|
474
|
+
return sum + (node ? calculateCohesion(node.exports, file, { coUsageMatrix: graph.coUsageMatrix }) : 0);
|
|
475
|
+
}, 0) / files.length;
|
|
476
|
+
const targetFiles = Math.max(1, Math.ceil(files.length / 3));
|
|
477
|
+
const consolidationPlan = generateConsolidationPlan(
|
|
478
|
+
domain,
|
|
479
|
+
files,
|
|
480
|
+
targetFiles
|
|
481
|
+
);
|
|
482
|
+
clusters.push({
|
|
483
|
+
domain,
|
|
484
|
+
files,
|
|
485
|
+
totalTokens,
|
|
486
|
+
fragmentationScore,
|
|
487
|
+
pathEntropy,
|
|
488
|
+
directoryDistance,
|
|
489
|
+
importCohesion,
|
|
490
|
+
avgCohesion,
|
|
491
|
+
suggestedStructure: {
|
|
492
|
+
targetFiles,
|
|
493
|
+
consolidationPlan
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
return clusters.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
|
|
498
|
+
}
|
|
499
|
+
function extractExports(content, filePath, domainOptions, fileImports) {
|
|
500
|
+
const exports = [];
|
|
501
|
+
const patterns = [
|
|
502
|
+
/export\s+function\s+(\w+)/g,
|
|
503
|
+
/export\s+class\s+(\w+)/g,
|
|
504
|
+
/export\s+const\s+(\w+)/g,
|
|
505
|
+
/export\s+type\s+(\w+)/g,
|
|
506
|
+
/export\s+interface\s+(\w+)/g,
|
|
507
|
+
/export\s+default/g
|
|
508
|
+
];
|
|
509
|
+
const types = [
|
|
510
|
+
"function",
|
|
511
|
+
"class",
|
|
512
|
+
"const",
|
|
513
|
+
"type",
|
|
514
|
+
"interface",
|
|
515
|
+
"default"
|
|
516
|
+
];
|
|
517
|
+
patterns.forEach((pattern, index) => {
|
|
518
|
+
let match;
|
|
519
|
+
while ((match = pattern.exec(content)) !== null) {
|
|
520
|
+
const name = match[1] || "default";
|
|
521
|
+
const type = types[index];
|
|
522
|
+
const inferredDomain = inferDomain(name, filePath, domainOptions, fileImports);
|
|
523
|
+
exports.push({ name, type, inferredDomain });
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
return exports;
|
|
527
|
+
}
|
|
528
|
+
function inferDomain(name, filePath, domainOptions, fileImports) {
|
|
529
|
+
const lower = name.toLowerCase();
|
|
530
|
+
const tokens = Array.from(
|
|
531
|
+
new Set(
|
|
532
|
+
lower.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[^a-z0-9]+/gi, " ").split(" ").filter(Boolean)
|
|
533
|
+
)
|
|
534
|
+
);
|
|
535
|
+
const defaultKeywords = [
|
|
536
|
+
"authentication",
|
|
537
|
+
"authorization",
|
|
538
|
+
"payment",
|
|
539
|
+
"invoice",
|
|
540
|
+
"customer",
|
|
541
|
+
"product",
|
|
542
|
+
"order",
|
|
543
|
+
"cart",
|
|
544
|
+
"user",
|
|
545
|
+
"admin",
|
|
546
|
+
"repository",
|
|
547
|
+
"controller",
|
|
548
|
+
"service",
|
|
549
|
+
"config",
|
|
550
|
+
"model",
|
|
551
|
+
"view",
|
|
552
|
+
"auth"
|
|
553
|
+
];
|
|
554
|
+
const domainKeywords = domainOptions?.domainKeywords && domainOptions.domainKeywords.length ? [...domainOptions.domainKeywords, ...defaultKeywords] : defaultKeywords;
|
|
555
|
+
for (const keyword of domainKeywords) {
|
|
556
|
+
if (tokens.includes(keyword)) {
|
|
557
|
+
return keyword;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
for (const keyword of domainKeywords) {
|
|
561
|
+
if (lower.includes(keyword)) {
|
|
562
|
+
return keyword;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
if (fileImports && fileImports.length > 0) {
|
|
566
|
+
for (const importPath of fileImports) {
|
|
567
|
+
const allSegments = importPath.split("/");
|
|
568
|
+
const relevantSegments = allSegments.filter((s) => {
|
|
569
|
+
if (!s) return false;
|
|
570
|
+
if (s === "." || s === "..") return false;
|
|
571
|
+
if (s.startsWith("@") && s.length === 1) return false;
|
|
572
|
+
return true;
|
|
573
|
+
}).map((s) => s.startsWith("@") ? s.slice(1) : s);
|
|
574
|
+
for (const segment of relevantSegments) {
|
|
575
|
+
const segLower = segment.toLowerCase();
|
|
576
|
+
const singularSegment = singularize(segLower);
|
|
577
|
+
for (const keyword of domainKeywords) {
|
|
578
|
+
if (singularSegment === keyword || segLower === keyword || segLower.includes(keyword)) {
|
|
579
|
+
return keyword;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
if (filePath) {
|
|
586
|
+
const pathSegments = filePath.toLowerCase().split("/");
|
|
587
|
+
for (const segment of pathSegments) {
|
|
588
|
+
const singularSegment = singularize(segment);
|
|
589
|
+
for (const keyword of domainKeywords) {
|
|
590
|
+
if (singularSegment === keyword || segment === keyword || segment.includes(keyword)) {
|
|
591
|
+
return keyword;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return "unknown";
|
|
597
|
+
}
|
|
598
|
+
function generateConsolidationPlan(domain, files, targetFiles) {
|
|
599
|
+
const plan = [];
|
|
600
|
+
if (files.length <= targetFiles) {
|
|
601
|
+
return [`No consolidation needed for ${domain}`];
|
|
602
|
+
}
|
|
603
|
+
plan.push(
|
|
604
|
+
`Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s):`
|
|
605
|
+
);
|
|
606
|
+
const dirGroups = /* @__PURE__ */ new Map();
|
|
607
|
+
for (const file of files) {
|
|
608
|
+
const dir = file.split("/").slice(0, -1).join("/");
|
|
609
|
+
if (!dirGroups.has(dir)) {
|
|
610
|
+
dirGroups.set(dir, []);
|
|
611
|
+
}
|
|
612
|
+
dirGroups.get(dir).push(file);
|
|
613
|
+
}
|
|
614
|
+
plan.push(`1. Create unified ${domain} module file`);
|
|
615
|
+
plan.push(
|
|
616
|
+
`2. Move related functionality from ${files.length} scattered files`
|
|
617
|
+
);
|
|
618
|
+
plan.push(`3. Update imports in dependent files`);
|
|
619
|
+
plan.push(
|
|
620
|
+
`4. Remove old files after consolidation (verify with tests first)`
|
|
621
|
+
);
|
|
622
|
+
return plan;
|
|
623
|
+
}
|
|
624
|
+
function extractExportsWithAST(content, filePath, domainOptions, fileImports) {
|
|
625
|
+
try {
|
|
626
|
+
const { exports: astExports } = parseFileExports(content, filePath);
|
|
627
|
+
return astExports.map((exp) => ({
|
|
628
|
+
name: exp.name,
|
|
629
|
+
type: exp.type,
|
|
630
|
+
inferredDomain: inferDomain(exp.name, filePath, domainOptions, fileImports),
|
|
631
|
+
imports: exp.imports,
|
|
632
|
+
dependencies: exp.dependencies
|
|
633
|
+
}));
|
|
634
|
+
} catch (error) {
|
|
635
|
+
return extractExports(content, filePath, domainOptions, fileImports);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
function calculateEnhancedCohesion(exports, filePath, options) {
|
|
639
|
+
if (exports.length === 0) return 1;
|
|
640
|
+
if (exports.length === 1) return 1;
|
|
641
|
+
if (filePath && isTestFile(filePath)) {
|
|
642
|
+
return 1;
|
|
643
|
+
}
|
|
644
|
+
const domainCohesion = calculateDomainCohesion(exports);
|
|
645
|
+
const hasImportData = exports.some((e) => e.imports && e.imports.length > 0);
|
|
646
|
+
const importCohesion = hasImportData ? calculateImportBasedCohesion(exports) : void 0;
|
|
647
|
+
const coUsageMatrix = options?.coUsageMatrix;
|
|
648
|
+
const structuralCohesion = filePath && coUsageMatrix ? calculateStructuralCohesionFromCoUsage(filePath, coUsageMatrix) : void 0;
|
|
649
|
+
const defaultWeights = { importBased: 0.5, structural: 0.3, domainBased: 0.2 };
|
|
650
|
+
const weights = { ...defaultWeights, ...options?.weights || {} };
|
|
651
|
+
const signals = [];
|
|
652
|
+
if (importCohesion !== void 0) signals.push({ score: importCohesion, weight: weights.importBased });
|
|
653
|
+
if (structuralCohesion !== void 0) signals.push({ score: structuralCohesion, weight: weights.structural });
|
|
654
|
+
signals.push({ score: domainCohesion, weight: weights.domainBased });
|
|
655
|
+
const totalWeight = signals.reduce((s, el) => s + el.weight, 0);
|
|
656
|
+
if (totalWeight === 0) return domainCohesion;
|
|
657
|
+
const combined = signals.reduce((sum, el) => sum + el.score * (el.weight / totalWeight), 0);
|
|
658
|
+
return combined;
|
|
659
|
+
}
|
|
660
|
+
function calculateStructuralCohesionFromCoUsage(file, coUsageMatrix) {
|
|
661
|
+
if (!coUsageMatrix) return 1;
|
|
662
|
+
const coUsages = coUsageMatrix.get(file);
|
|
663
|
+
if (!coUsages || coUsages.size === 0) return 1;
|
|
664
|
+
let total = 0;
|
|
665
|
+
for (const count of coUsages.values()) total += count;
|
|
666
|
+
if (total === 0) return 1;
|
|
667
|
+
const probs = [];
|
|
668
|
+
for (const count of coUsages.values()) {
|
|
669
|
+
if (count > 0) probs.push(count / total);
|
|
670
|
+
}
|
|
671
|
+
if (probs.length <= 1) return 1;
|
|
672
|
+
let entropy = 0;
|
|
673
|
+
for (const prob of probs) {
|
|
674
|
+
entropy -= prob * Math.log2(prob);
|
|
675
|
+
}
|
|
676
|
+
const maxEntropy = Math.log2(probs.length);
|
|
677
|
+
return maxEntropy > 0 ? 1 - entropy / maxEntropy : 1;
|
|
678
|
+
}
|
|
679
|
+
function calculateImportBasedCohesion(exports) {
|
|
680
|
+
const exportsWithImports = exports.filter((e) => e.imports && e.imports.length > 0);
|
|
681
|
+
if (exportsWithImports.length < 2) {
|
|
682
|
+
return 1;
|
|
683
|
+
}
|
|
684
|
+
let totalSimilarity = 0;
|
|
685
|
+
let comparisons = 0;
|
|
686
|
+
for (let i = 0; i < exportsWithImports.length; i++) {
|
|
687
|
+
for (let j = i + 1; j < exportsWithImports.length; j++) {
|
|
688
|
+
const exp1 = exportsWithImports[i];
|
|
689
|
+
const exp2 = exportsWithImports[j];
|
|
690
|
+
const similarity = calculateJaccardSimilarity(exp1.imports, exp2.imports);
|
|
691
|
+
totalSimilarity += similarity;
|
|
692
|
+
comparisons++;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
return comparisons > 0 ? totalSimilarity / comparisons : 1;
|
|
696
|
+
}
|
|
697
|
+
function calculateJaccardSimilarity(arr1, arr2) {
|
|
698
|
+
if (arr1.length === 0 && arr2.length === 0) return 1;
|
|
699
|
+
if (arr1.length === 0 || arr2.length === 0) return 0;
|
|
700
|
+
const set1 = new Set(arr1);
|
|
701
|
+
const set2 = new Set(arr2);
|
|
702
|
+
const intersection = new Set([...set1].filter((x) => set2.has(x)));
|
|
703
|
+
const union = /* @__PURE__ */ new Set([...set1, ...set2]);
|
|
704
|
+
return intersection.size / union.size;
|
|
705
|
+
}
|
|
706
|
+
function calculateDomainCohesion(exports) {
|
|
707
|
+
const domains = exports.map((e) => e.inferredDomain || "unknown");
|
|
708
|
+
const domainCounts = /* @__PURE__ */ new Map();
|
|
709
|
+
for (const domain of domains) {
|
|
710
|
+
domainCounts.set(domain, (domainCounts.get(domain) || 0) + 1);
|
|
711
|
+
}
|
|
712
|
+
const total = domains.length;
|
|
713
|
+
let entropy = 0;
|
|
714
|
+
for (const domainCount of domainCounts.values()) {
|
|
715
|
+
const prob = domainCount / total;
|
|
716
|
+
if (prob > 0) {
|
|
717
|
+
entropy -= prob * Math.log2(prob);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
const maxEntropy = Math.log2(total);
|
|
721
|
+
return maxEntropy > 0 ? 1 - entropy / maxEntropy : 1;
|
|
722
|
+
}
|
|
723
|
+
function classifyFile(node, cohesionScore, domains) {
|
|
724
|
+
const { exports, imports, linesOfCode, file } = node;
|
|
725
|
+
if (isBarrelExport(node)) {
|
|
726
|
+
return "barrel-export";
|
|
727
|
+
}
|
|
728
|
+
if (isTypeDefinitionFile(node)) {
|
|
729
|
+
return "type-definition";
|
|
730
|
+
}
|
|
731
|
+
if (isConfigOrSchemaFile(node)) {
|
|
732
|
+
return "cohesive-module";
|
|
733
|
+
}
|
|
734
|
+
if (isLambdaHandler(node)) {
|
|
735
|
+
return "lambda-handler";
|
|
736
|
+
}
|
|
737
|
+
if (isDataAccessFile(node)) {
|
|
738
|
+
return "cohesive-module";
|
|
739
|
+
}
|
|
740
|
+
if (isEmailTemplate(node)) {
|
|
741
|
+
return "email-template";
|
|
742
|
+
}
|
|
743
|
+
if (isParserFile(node)) {
|
|
744
|
+
return "parser-file";
|
|
745
|
+
}
|
|
746
|
+
if (isServiceFile(node)) {
|
|
747
|
+
return "service-file";
|
|
748
|
+
}
|
|
749
|
+
if (isSessionFile(node)) {
|
|
750
|
+
return "cohesive-module";
|
|
751
|
+
}
|
|
752
|
+
if (isNextJsPage(node)) {
|
|
753
|
+
return "nextjs-page";
|
|
754
|
+
}
|
|
755
|
+
if (isUtilityFile(node)) {
|
|
756
|
+
return "utility-module";
|
|
757
|
+
}
|
|
758
|
+
const uniqueDomains = domains.filter((d) => d !== "unknown");
|
|
759
|
+
const hasSingleDomain = uniqueDomains.length <= 1;
|
|
760
|
+
if (hasSingleDomain) {
|
|
761
|
+
return "cohesive-module";
|
|
762
|
+
}
|
|
763
|
+
if (allExportsShareEntityNoun(exports)) {
|
|
764
|
+
return "cohesive-module";
|
|
765
|
+
}
|
|
766
|
+
const hasMultipleDomains = uniqueDomains.length > 1;
|
|
767
|
+
const hasLowCohesion = cohesionScore < 0.4;
|
|
768
|
+
if (hasMultipleDomains && hasLowCohesion) {
|
|
769
|
+
return "mixed-concerns";
|
|
770
|
+
}
|
|
771
|
+
if (cohesionScore >= 0.5) {
|
|
772
|
+
return "cohesive-module";
|
|
773
|
+
}
|
|
774
|
+
return "unknown";
|
|
775
|
+
}
|
|
776
|
+
function isBarrelExport(node) {
|
|
777
|
+
const { file, exports, imports, linesOfCode } = node;
|
|
778
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
779
|
+
const isIndexFile = fileName === "index.ts" || fileName === "index.js" || fileName === "index.tsx" || fileName === "index.jsx";
|
|
780
|
+
const hasReExports = exports.length > 0 && imports.length > 0;
|
|
781
|
+
const highExportToLinesRatio = exports.length > 3 && linesOfCode < exports.length * 5;
|
|
782
|
+
const sparseCode = linesOfCode > 0 && linesOfCode < 50 && exports.length >= 2;
|
|
783
|
+
if (isIndexFile && hasReExports) {
|
|
784
|
+
return true;
|
|
785
|
+
}
|
|
786
|
+
if (highExportToLinesRatio && imports.length >= exports.length * 0.5) {
|
|
787
|
+
return true;
|
|
788
|
+
}
|
|
789
|
+
if (sparseCode && imports.length > 0) {
|
|
790
|
+
return true;
|
|
791
|
+
}
|
|
792
|
+
return false;
|
|
793
|
+
}
|
|
794
|
+
function isTypeDefinitionFile(node) {
|
|
795
|
+
const { file, exports } = node;
|
|
796
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
797
|
+
const isTypesFile = fileName?.includes("types") || fileName?.includes(".d.ts") || fileName === "types.ts" || fileName === "interfaces.ts";
|
|
798
|
+
const lowerPath = file.toLowerCase();
|
|
799
|
+
const isTypesPath = lowerPath.includes("/types/") || lowerPath.includes("/typings/") || lowerPath.includes("/@types/") || lowerPath.startsWith("types/") || lowerPath.startsWith("typings/");
|
|
800
|
+
const typeExports = exports.filter((e) => e.type === "type" || e.type === "interface");
|
|
801
|
+
const runtimeExports = exports.filter((e) => e.type === "function" || e.type === "class" || e.type === "const");
|
|
802
|
+
const mostlyTypes = exports.length > 0 && typeExports.length > runtimeExports.length && typeExports.length / exports.length > 0.7;
|
|
803
|
+
const pureTypeFile = exports.length > 0 && typeExports.length === exports.length;
|
|
804
|
+
const emptyOrReExportInTypesDir = isTypesPath && exports.length === 0;
|
|
805
|
+
return isTypesFile || isTypesPath || mostlyTypes || pureTypeFile || emptyOrReExportInTypesDir;
|
|
806
|
+
}
|
|
807
|
+
function isConfigOrSchemaFile(node) {
|
|
808
|
+
const { file, exports } = node;
|
|
809
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
810
|
+
const configPatterns = [
|
|
811
|
+
"config",
|
|
812
|
+
"schema",
|
|
813
|
+
"settings",
|
|
814
|
+
"options",
|
|
815
|
+
"constants",
|
|
816
|
+
"env",
|
|
817
|
+
"environment",
|
|
818
|
+
".config.",
|
|
819
|
+
"-config.",
|
|
820
|
+
"_config."
|
|
821
|
+
];
|
|
822
|
+
const isConfigName = configPatterns.some(
|
|
823
|
+
(pattern) => fileName?.includes(pattern) || fileName?.startsWith(pattern) || fileName?.endsWith(`${pattern}.ts`)
|
|
824
|
+
);
|
|
825
|
+
const isConfigPath = file.toLowerCase().includes("/config/") || file.toLowerCase().includes("/schemas/") || file.toLowerCase().includes("/settings/");
|
|
826
|
+
const hasSchemaExports = exports.some(
|
|
827
|
+
(e) => e.name.toLowerCase().includes("table") || e.name.toLowerCase().includes("schema") || e.name.toLowerCase().includes("config") || e.name.toLowerCase().includes("setting")
|
|
828
|
+
);
|
|
829
|
+
return isConfigName || isConfigPath || hasSchemaExports;
|
|
830
|
+
}
|
|
831
|
+
function isUtilityFile(node) {
|
|
832
|
+
const { file, exports } = node;
|
|
833
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
834
|
+
const utilityPatterns = [
|
|
835
|
+
"util",
|
|
836
|
+
"utility",
|
|
837
|
+
"utilities",
|
|
838
|
+
"helper",
|
|
839
|
+
"helpers",
|
|
840
|
+
"common",
|
|
841
|
+
"shared",
|
|
842
|
+
"toolbox",
|
|
843
|
+
"toolkit",
|
|
844
|
+
".util.",
|
|
845
|
+
"-util.",
|
|
846
|
+
"_util.",
|
|
847
|
+
"-utils.",
|
|
848
|
+
".utils."
|
|
849
|
+
];
|
|
850
|
+
const isUtilityName = utilityPatterns.some(
|
|
851
|
+
(pattern) => fileName?.includes(pattern)
|
|
852
|
+
);
|
|
853
|
+
const isUtilityPath = file.toLowerCase().includes("/utils/") || file.toLowerCase().includes("/helpers/") || file.toLowerCase().includes("/common/") || file.toLowerCase().endsWith("-utils.ts") || file.toLowerCase().endsWith("-util.ts") || file.toLowerCase().endsWith("-helper.ts") || file.toLowerCase().endsWith("-helpers.ts");
|
|
854
|
+
const hasManySmallExportsInUtilityContext = exports.length >= 3 && exports.every((e) => e.type === "function" || e.type === "const") && (isUtilityName || isUtilityPath);
|
|
855
|
+
return isUtilityName || isUtilityPath || hasManySmallExportsInUtilityContext;
|
|
856
|
+
}
|
|
857
|
+
function splitCamelCase(name) {
|
|
858
|
+
return name.replace(/([A-Z])/g, " $1").trim().toLowerCase().split(/[\s_-]+/).filter(Boolean);
|
|
859
|
+
}
|
|
860
|
+
var SKIP_WORDS = /* @__PURE__ */ new Set([
|
|
861
|
+
"get",
|
|
862
|
+
"set",
|
|
863
|
+
"create",
|
|
864
|
+
"update",
|
|
865
|
+
"delete",
|
|
866
|
+
"fetch",
|
|
867
|
+
"save",
|
|
868
|
+
"load",
|
|
869
|
+
"parse",
|
|
870
|
+
"format",
|
|
871
|
+
"validate",
|
|
872
|
+
"convert",
|
|
873
|
+
"transform",
|
|
874
|
+
"build",
|
|
875
|
+
"generate",
|
|
876
|
+
"render",
|
|
877
|
+
"send",
|
|
878
|
+
"receive",
|
|
879
|
+
"find",
|
|
880
|
+
"list",
|
|
881
|
+
"add",
|
|
882
|
+
"remove",
|
|
883
|
+
"insert",
|
|
884
|
+
"upsert",
|
|
885
|
+
"put",
|
|
886
|
+
"read",
|
|
887
|
+
"write",
|
|
888
|
+
"check",
|
|
889
|
+
"handle",
|
|
890
|
+
"process",
|
|
891
|
+
"compute",
|
|
892
|
+
"calculate",
|
|
893
|
+
"init",
|
|
894
|
+
"reset",
|
|
895
|
+
"clear",
|
|
896
|
+
"pending",
|
|
897
|
+
"active",
|
|
898
|
+
"current",
|
|
899
|
+
"new",
|
|
900
|
+
"old",
|
|
901
|
+
"all",
|
|
902
|
+
"by",
|
|
903
|
+
"with",
|
|
904
|
+
"from",
|
|
905
|
+
"to",
|
|
906
|
+
"and",
|
|
907
|
+
"or",
|
|
908
|
+
"is",
|
|
909
|
+
"has",
|
|
910
|
+
"in",
|
|
911
|
+
"on",
|
|
912
|
+
"of",
|
|
913
|
+
"the"
|
|
914
|
+
]);
|
|
915
|
+
function simpleSingularize(word) {
|
|
916
|
+
if (word.endsWith("ies") && word.length > 3) return word.slice(0, -3) + "y";
|
|
917
|
+
if (word.endsWith("ses") && word.length > 4) return word.slice(0, -2);
|
|
918
|
+
if (word.endsWith("s") && word.length > 3) return word.slice(0, -1);
|
|
919
|
+
return word;
|
|
920
|
+
}
|
|
921
|
+
function extractEntityNouns(name) {
|
|
922
|
+
return splitCamelCase(name).filter((token) => !SKIP_WORDS.has(token) && token.length > 2).map(simpleSingularize);
|
|
923
|
+
}
|
|
924
|
+
function allExportsShareEntityNoun(exports) {
|
|
925
|
+
if (exports.length < 2 || exports.length > 30) return false;
|
|
926
|
+
const nounSets = exports.map((e) => new Set(extractEntityNouns(e.name)));
|
|
927
|
+
if (nounSets.some((s) => s.size === 0)) return false;
|
|
928
|
+
const [first, ...rest] = nounSets;
|
|
929
|
+
const commonNouns = Array.from(first).filter(
|
|
930
|
+
(noun) => rest.every((s) => s.has(noun))
|
|
931
|
+
);
|
|
932
|
+
return commonNouns.length > 0;
|
|
933
|
+
}
|
|
934
|
+
function isDataAccessFile(node) {
|
|
935
|
+
const { file, exports } = node;
|
|
936
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
937
|
+
const dalPatterns = [
|
|
938
|
+
"dynamo",
|
|
939
|
+
"database",
|
|
940
|
+
"repository",
|
|
941
|
+
"repo",
|
|
942
|
+
"dao",
|
|
943
|
+
"firestore",
|
|
944
|
+
"postgres",
|
|
945
|
+
"mysql",
|
|
946
|
+
"mongo",
|
|
947
|
+
"redis",
|
|
948
|
+
"sqlite",
|
|
949
|
+
"supabase",
|
|
950
|
+
"prisma"
|
|
951
|
+
];
|
|
952
|
+
const isDalName = dalPatterns.some((p) => fileName?.includes(p));
|
|
953
|
+
const isDalPath = file.toLowerCase().includes("/repositories/") || file.toLowerCase().includes("/dao/") || file.toLowerCase().includes("/data/");
|
|
954
|
+
const hasDalExportPattern = exports.length >= 1 && exports.length <= 10 && allExportsShareEntityNoun(exports);
|
|
955
|
+
return isDalName || isDalPath || isDalName && hasDalExportPattern;
|
|
956
|
+
}
|
|
957
|
+
function isLambdaHandler(node) {
|
|
958
|
+
const { file, exports } = node;
|
|
959
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
960
|
+
const handlerPatterns = [
|
|
961
|
+
"handler",
|
|
962
|
+
".handler.",
|
|
963
|
+
"-handler.",
|
|
964
|
+
"lambda",
|
|
965
|
+
".lambda.",
|
|
966
|
+
"-lambda."
|
|
967
|
+
];
|
|
968
|
+
const isHandlerName = handlerPatterns.some(
|
|
969
|
+
(pattern) => fileName?.includes(pattern)
|
|
970
|
+
);
|
|
971
|
+
const isHandlerPath = file.toLowerCase().includes("/handlers/") || file.toLowerCase().includes("/lambdas/") || file.toLowerCase().includes("/lambda/") || file.toLowerCase().includes("/functions/");
|
|
972
|
+
const hasHandlerExport = exports.some(
|
|
973
|
+
(e) => e.name.toLowerCase() === "handler" || e.name.toLowerCase() === "main" || e.name.toLowerCase() === "lambdahandler" || e.name.toLowerCase().endsWith("handler")
|
|
974
|
+
);
|
|
975
|
+
const hasSingleEntryInHandlerContext = exports.length === 1 && (exports[0].type === "function" || exports[0].name === "default") && (isHandlerPath || isHandlerName);
|
|
976
|
+
return isHandlerName || isHandlerPath || hasHandlerExport || hasSingleEntryInHandlerContext;
|
|
977
|
+
}
|
|
978
|
+
function isServiceFile(node) {
|
|
979
|
+
const { file, exports } = node;
|
|
980
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
981
|
+
const servicePatterns = [
|
|
982
|
+
"service",
|
|
983
|
+
".service.",
|
|
984
|
+
"-service.",
|
|
985
|
+
"_service."
|
|
986
|
+
];
|
|
987
|
+
const isServiceName = servicePatterns.some(
|
|
988
|
+
(pattern) => fileName?.includes(pattern)
|
|
989
|
+
);
|
|
990
|
+
const isServicePath = file.toLowerCase().includes("/services/");
|
|
991
|
+
const hasServiceNamedExport = exports.some(
|
|
992
|
+
(e) => e.name.toLowerCase().includes("service") || e.name.toLowerCase().endsWith("service")
|
|
993
|
+
);
|
|
994
|
+
const hasClassExport = exports.some((e) => e.type === "class");
|
|
995
|
+
return isServiceName || isServicePath || hasServiceNamedExport && hasClassExport;
|
|
996
|
+
}
|
|
997
|
+
function isEmailTemplate(node) {
|
|
998
|
+
const { file, exports } = node;
|
|
999
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
1000
|
+
const emailTemplatePatterns = [
|
|
1001
|
+
"-email-",
|
|
1002
|
+
".email.",
|
|
1003
|
+
"_email_",
|
|
1004
|
+
"-template",
|
|
1005
|
+
".template.",
|
|
1006
|
+
"_template",
|
|
1007
|
+
"-mail.",
|
|
1008
|
+
".mail."
|
|
1009
|
+
];
|
|
1010
|
+
const isEmailTemplateName = emailTemplatePatterns.some(
|
|
1011
|
+
(pattern) => fileName?.includes(pattern)
|
|
1012
|
+
);
|
|
1013
|
+
const isSpecificTemplateName = fileName?.includes("receipt") || fileName?.includes("invoice-email") || fileName?.includes("welcome-email") || fileName?.includes("notification-email") || fileName?.includes("writer") && fileName.includes("receipt");
|
|
1014
|
+
const isEmailPath = file.toLowerCase().includes("/emails/") || file.toLowerCase().includes("/mail/") || file.toLowerCase().includes("/notifications/");
|
|
1015
|
+
const hasTemplateFunction = exports.some(
|
|
1016
|
+
(e) => e.type === "function" && (e.name.toLowerCase().startsWith("render") || e.name.toLowerCase().startsWith("generate") || e.name.toLowerCase().includes("template") && e.name.toLowerCase().includes("email"))
|
|
1017
|
+
);
|
|
1018
|
+
const hasEmailExport = exports.some(
|
|
1019
|
+
(e) => e.name.toLowerCase().includes("template") && e.type === "function" || e.name.toLowerCase().includes("render") && e.type === "function" || e.name.toLowerCase().includes("email") && e.type !== "class"
|
|
1020
|
+
);
|
|
1021
|
+
return isEmailPath || isEmailTemplateName || isSpecificTemplateName || hasTemplateFunction && hasEmailExport;
|
|
1022
|
+
}
|
|
1023
|
+
function isParserFile(node) {
|
|
1024
|
+
const { file, exports } = node;
|
|
1025
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
1026
|
+
const parserPatterns = [
|
|
1027
|
+
"parser",
|
|
1028
|
+
".parser.",
|
|
1029
|
+
"-parser.",
|
|
1030
|
+
"_parser.",
|
|
1031
|
+
"transform",
|
|
1032
|
+
".transform.",
|
|
1033
|
+
"-transform.",
|
|
1034
|
+
"converter",
|
|
1035
|
+
".converter.",
|
|
1036
|
+
"-converter.",
|
|
1037
|
+
"mapper",
|
|
1038
|
+
".mapper.",
|
|
1039
|
+
"-mapper.",
|
|
1040
|
+
"serializer",
|
|
1041
|
+
".serializer.",
|
|
1042
|
+
"deterministic"
|
|
1043
|
+
// For base-parser-deterministic.ts pattern
|
|
1044
|
+
];
|
|
1045
|
+
const isParserName = parserPatterns.some(
|
|
1046
|
+
(pattern) => fileName?.includes(pattern)
|
|
1047
|
+
);
|
|
1048
|
+
const isParserPath = file.toLowerCase().includes("/parsers/") || file.toLowerCase().includes("/transformers/") || file.toLowerCase().includes("/converters/") || file.toLowerCase().includes("/mappers/");
|
|
1049
|
+
const hasParserExport = exports.some(
|
|
1050
|
+
(e) => e.name.toLowerCase().includes("parse") || e.name.toLowerCase().includes("transform") || e.name.toLowerCase().includes("convert") || e.name.toLowerCase().includes("map") || e.name.toLowerCase().includes("serialize") || e.name.toLowerCase().includes("deserialize")
|
|
1051
|
+
);
|
|
1052
|
+
const hasParseFunction = exports.some(
|
|
1053
|
+
(e) => e.type === "function" && (e.name.toLowerCase().startsWith("parse") || e.name.toLowerCase().startsWith("transform") || e.name.toLowerCase().startsWith("convert") || e.name.toLowerCase().startsWith("map") || e.name.toLowerCase().startsWith("extract"))
|
|
1054
|
+
);
|
|
1055
|
+
return isParserName || isParserPath || hasParserExport || hasParseFunction;
|
|
1056
|
+
}
|
|
1057
|
+
function isSessionFile(node) {
|
|
1058
|
+
const { file, exports } = node;
|
|
1059
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
1060
|
+
const sessionPatterns = [
|
|
1061
|
+
"session",
|
|
1062
|
+
".session.",
|
|
1063
|
+
"-session.",
|
|
1064
|
+
"state",
|
|
1065
|
+
".state.",
|
|
1066
|
+
"-state.",
|
|
1067
|
+
"context",
|
|
1068
|
+
".context.",
|
|
1069
|
+
"-context.",
|
|
1070
|
+
"store",
|
|
1071
|
+
".store.",
|
|
1072
|
+
"-store."
|
|
1073
|
+
];
|
|
1074
|
+
const isSessionName = sessionPatterns.some(
|
|
1075
|
+
(pattern) => fileName?.includes(pattern)
|
|
1076
|
+
);
|
|
1077
|
+
const isSessionPath = file.toLowerCase().includes("/sessions/") || file.toLowerCase().includes("/state/") || file.toLowerCase().includes("/context/") || file.toLowerCase().includes("/store/");
|
|
1078
|
+
const hasSessionExport = exports.some(
|
|
1079
|
+
(e) => e.name.toLowerCase().includes("session") || e.name.toLowerCase().includes("state") || e.name.toLowerCase().includes("context") || e.name.toLowerCase().includes("manager") || e.name.toLowerCase().includes("store")
|
|
1080
|
+
);
|
|
1081
|
+
return isSessionName || isSessionPath || hasSessionExport;
|
|
1082
|
+
}
|
|
1083
|
+
function isNextJsPage(node) {
|
|
1084
|
+
const { file, exports } = node;
|
|
1085
|
+
const lowerPath = file.toLowerCase();
|
|
1086
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
1087
|
+
const isInAppDir = lowerPath.includes("/app/") || lowerPath.startsWith("app/");
|
|
1088
|
+
const isPageFile = fileName === "page.tsx" || fileName === "page.ts";
|
|
1089
|
+
if (!isInAppDir || !isPageFile) {
|
|
1090
|
+
return false;
|
|
1091
|
+
}
|
|
1092
|
+
const exportNames = exports.map((e) => e.name.toLowerCase());
|
|
1093
|
+
const hasDefaultExport = exports.some((e) => e.type === "default");
|
|
1094
|
+
const nextJsExports = ["metadata", "generatemetadata", "faqjsonld", "jsonld", "icon", "viewport", "dynamic"];
|
|
1095
|
+
const hasNextJsExports = exportNames.some(
|
|
1096
|
+
(name) => nextJsExports.includes(name) || name.includes("jsonld")
|
|
1097
|
+
);
|
|
1098
|
+
return hasDefaultExport || hasNextJsExports;
|
|
1099
|
+
}
|
|
1100
|
+
function adjustCohesionForClassification(baseCohesion, classification, node) {
|
|
1101
|
+
switch (classification) {
|
|
1102
|
+
case "barrel-export":
|
|
1103
|
+
return 1;
|
|
1104
|
+
case "type-definition":
|
|
1105
|
+
return 1;
|
|
1106
|
+
case "utility-module": {
|
|
1107
|
+
if (node) {
|
|
1108
|
+
const exportNames = node.exports.map((e) => e.name.toLowerCase());
|
|
1109
|
+
const hasRelatedNames = hasRelatedExportNames(exportNames);
|
|
1110
|
+
if (hasRelatedNames) {
|
|
1111
|
+
return Math.max(0.8, Math.min(1, baseCohesion + 0.45));
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
return Math.max(0.75, Math.min(1, baseCohesion + 0.35));
|
|
1115
|
+
}
|
|
1116
|
+
case "service-file": {
|
|
1117
|
+
if (node?.exports.some((e) => e.type === "class")) {
|
|
1118
|
+
return Math.max(0.78, Math.min(1, baseCohesion + 0.4));
|
|
1119
|
+
}
|
|
1120
|
+
return Math.max(0.72, Math.min(1, baseCohesion + 0.3));
|
|
1121
|
+
}
|
|
1122
|
+
case "lambda-handler": {
|
|
1123
|
+
if (node) {
|
|
1124
|
+
const hasSingleEntry = node.exports.length === 1 || node.exports.some((e) => e.name.toLowerCase() === "handler");
|
|
1125
|
+
if (hasSingleEntry) {
|
|
1126
|
+
return Math.max(0.8, Math.min(1, baseCohesion + 0.45));
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
return Math.max(0.75, Math.min(1, baseCohesion + 0.35));
|
|
1130
|
+
}
|
|
1131
|
+
case "email-template": {
|
|
1132
|
+
if (node) {
|
|
1133
|
+
const hasTemplateFunc = node.exports.some(
|
|
1134
|
+
(e) => e.name.toLowerCase().includes("render") || e.name.toLowerCase().includes("generate") || e.name.toLowerCase().includes("template")
|
|
1135
|
+
);
|
|
1136
|
+
if (hasTemplateFunc) {
|
|
1137
|
+
return Math.max(0.75, Math.min(1, baseCohesion + 0.4));
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
return Math.max(0.72, Math.min(1, baseCohesion + 0.3));
|
|
1141
|
+
}
|
|
1142
|
+
case "parser-file": {
|
|
1143
|
+
if (node) {
|
|
1144
|
+
const hasParseFunc = node.exports.some(
|
|
1145
|
+
(e) => e.name.toLowerCase().startsWith("parse") || e.name.toLowerCase().startsWith("transform") || e.name.toLowerCase().startsWith("convert")
|
|
1146
|
+
);
|
|
1147
|
+
if (hasParseFunc) {
|
|
1148
|
+
return Math.max(0.75, Math.min(1, baseCohesion + 0.4));
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
return Math.max(0.7, Math.min(1, baseCohesion + 0.3));
|
|
1152
|
+
}
|
|
1153
|
+
case "nextjs-page":
|
|
1154
|
+
return 1;
|
|
1155
|
+
case "cohesive-module":
|
|
1156
|
+
return Math.max(baseCohesion, 0.7);
|
|
1157
|
+
case "mixed-concerns":
|
|
1158
|
+
return baseCohesion;
|
|
1159
|
+
default:
|
|
1160
|
+
return Math.min(1, baseCohesion + 0.1);
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
function hasRelatedExportNames(exportNames) {
|
|
1164
|
+
if (exportNames.length < 2) return true;
|
|
1165
|
+
const stems = /* @__PURE__ */ new Set();
|
|
1166
|
+
const domains = /* @__PURE__ */ new Set();
|
|
1167
|
+
for (const name of exportNames) {
|
|
1168
|
+
const verbs = ["get", "set", "create", "update", "delete", "fetch", "save", "load", "parse", "format", "validate", "convert", "transform", "build", "generate", "render", "send", "receive"];
|
|
1169
|
+
for (const verb of verbs) {
|
|
1170
|
+
if (name.startsWith(verb) && name.length > verb.length) {
|
|
1171
|
+
stems.add(name.slice(verb.length).toLowerCase());
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
const domainPatterns = ["user", "order", "product", "session", "email", "file", "db", "s3", "dynamo", "api", "config"];
|
|
1175
|
+
for (const domain of domainPatterns) {
|
|
1176
|
+
if (name.includes(domain)) {
|
|
1177
|
+
domains.add(domain);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
if (stems.size === 1 && exportNames.length >= 2) return true;
|
|
1182
|
+
if (domains.size === 1 && exportNames.length >= 2) return true;
|
|
1183
|
+
const prefixes = exportNames.map((name) => {
|
|
1184
|
+
const match = name.match(/^([a-z]+)/);
|
|
1185
|
+
return match ? match[1] : "";
|
|
1186
|
+
}).filter((p) => p.length >= 3);
|
|
1187
|
+
if (prefixes.length >= 2) {
|
|
1188
|
+
const uniquePrefixes = new Set(prefixes);
|
|
1189
|
+
if (uniquePrefixes.size === 1) return true;
|
|
1190
|
+
}
|
|
1191
|
+
const nounSets = exportNames.map((name) => {
|
|
1192
|
+
const tokens = name.replace(/([A-Z])/g, " $1").trim().toLowerCase().split(/[\s_-]+/).filter(Boolean);
|
|
1193
|
+
const skip = /* @__PURE__ */ new Set([
|
|
1194
|
+
"get",
|
|
1195
|
+
"set",
|
|
1196
|
+
"create",
|
|
1197
|
+
"update",
|
|
1198
|
+
"delete",
|
|
1199
|
+
"fetch",
|
|
1200
|
+
"save",
|
|
1201
|
+
"load",
|
|
1202
|
+
"parse",
|
|
1203
|
+
"format",
|
|
1204
|
+
"validate",
|
|
1205
|
+
"convert",
|
|
1206
|
+
"transform",
|
|
1207
|
+
"build",
|
|
1208
|
+
"generate",
|
|
1209
|
+
"render",
|
|
1210
|
+
"send",
|
|
1211
|
+
"receive",
|
|
1212
|
+
"find",
|
|
1213
|
+
"list",
|
|
1214
|
+
"add",
|
|
1215
|
+
"remove",
|
|
1216
|
+
"insert",
|
|
1217
|
+
"upsert",
|
|
1218
|
+
"put",
|
|
1219
|
+
"read",
|
|
1220
|
+
"write",
|
|
1221
|
+
"check",
|
|
1222
|
+
"handle",
|
|
1223
|
+
"process",
|
|
1224
|
+
"pending",
|
|
1225
|
+
"active",
|
|
1226
|
+
"current",
|
|
1227
|
+
"new",
|
|
1228
|
+
"old",
|
|
1229
|
+
"all"
|
|
1230
|
+
]);
|
|
1231
|
+
const singularize2 = (w) => w.endsWith("s") && w.length > 3 ? w.slice(0, -1) : w;
|
|
1232
|
+
return new Set(tokens.filter((t) => !skip.has(t) && t.length > 2).map(singularize2));
|
|
1233
|
+
});
|
|
1234
|
+
if (nounSets.length >= 2 && nounSets.every((s) => s.size > 0)) {
|
|
1235
|
+
const [first, ...rest] = nounSets;
|
|
1236
|
+
const commonNouns = Array.from(first).filter((n) => rest.every((s) => s.has(n)));
|
|
1237
|
+
if (commonNouns.length > 0) return true;
|
|
1238
|
+
}
|
|
1239
|
+
return false;
|
|
1240
|
+
}
|
|
1241
|
+
function adjustFragmentationForClassification(baseFragmentation, classification) {
|
|
1242
|
+
switch (classification) {
|
|
1243
|
+
case "barrel-export":
|
|
1244
|
+
return 0;
|
|
1245
|
+
case "type-definition":
|
|
1246
|
+
return 0;
|
|
1247
|
+
case "utility-module":
|
|
1248
|
+
case "service-file":
|
|
1249
|
+
case "lambda-handler":
|
|
1250
|
+
case "email-template":
|
|
1251
|
+
case "parser-file":
|
|
1252
|
+
case "nextjs-page":
|
|
1253
|
+
return baseFragmentation * 0.2;
|
|
1254
|
+
case "cohesive-module":
|
|
1255
|
+
return baseFragmentation * 0.3;
|
|
1256
|
+
case "mixed-concerns":
|
|
1257
|
+
return baseFragmentation;
|
|
1258
|
+
default:
|
|
1259
|
+
return baseFragmentation * 0.7;
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
function getClassificationRecommendations(classification, file, issues) {
|
|
1263
|
+
switch (classification) {
|
|
1264
|
+
case "barrel-export":
|
|
1265
|
+
return [
|
|
1266
|
+
"Barrel export file detected - multiple domains are expected here",
|
|
1267
|
+
"Consider if this barrel export improves or hinders discoverability"
|
|
1268
|
+
];
|
|
1269
|
+
case "type-definition":
|
|
1270
|
+
return [
|
|
1271
|
+
"Type definition file - centralized types improve consistency",
|
|
1272
|
+
"Consider splitting if file becomes too large (>500 lines)"
|
|
1273
|
+
];
|
|
1274
|
+
case "cohesive-module":
|
|
1275
|
+
return [
|
|
1276
|
+
"Module has good cohesion despite its size",
|
|
1277
|
+
"Consider documenting the module boundaries for AI assistants"
|
|
1278
|
+
];
|
|
1279
|
+
case "utility-module":
|
|
1280
|
+
return [
|
|
1281
|
+
"Utility module detected - multiple domains are acceptable here",
|
|
1282
|
+
"Consider grouping related utilities by prefix or domain for better discoverability"
|
|
1283
|
+
];
|
|
1284
|
+
case "service-file":
|
|
1285
|
+
return [
|
|
1286
|
+
"Service file detected - orchestration of multiple dependencies is expected",
|
|
1287
|
+
"Consider documenting service boundaries and dependencies"
|
|
1288
|
+
];
|
|
1289
|
+
case "lambda-handler":
|
|
1290
|
+
return [
|
|
1291
|
+
"Lambda handler detected - coordination of services is expected",
|
|
1292
|
+
"Ensure handler has clear single responsibility"
|
|
1293
|
+
];
|
|
1294
|
+
case "email-template":
|
|
1295
|
+
return [
|
|
1296
|
+
"Email template detected - references multiple domains for rendering",
|
|
1297
|
+
"Template structure is cohesive by design"
|
|
1298
|
+
];
|
|
1299
|
+
case "parser-file":
|
|
1300
|
+
return [
|
|
1301
|
+
"Parser/transformer file detected - handles multiple data sources",
|
|
1302
|
+
"Consider documenting input/output schemas"
|
|
1303
|
+
];
|
|
1304
|
+
case "nextjs-page":
|
|
1305
|
+
return [
|
|
1306
|
+
"Next.js App Router page detected - metadata/JSON-LD/component pattern is cohesive",
|
|
1307
|
+
"Multiple exports (metadata, faqJsonLd, default) serve single page purpose"
|
|
1308
|
+
];
|
|
1309
|
+
case "mixed-concerns":
|
|
1310
|
+
return [
|
|
1311
|
+
"Consider splitting this file by domain",
|
|
1312
|
+
"Identify independent responsibilities and extract them",
|
|
1313
|
+
"Review import dependencies to understand coupling"
|
|
1314
|
+
];
|
|
1315
|
+
default:
|
|
1316
|
+
return issues;
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
// src/scoring.ts
|
|
1321
|
+
import {
|
|
1322
|
+
calculateMonthlyCost,
|
|
1323
|
+
calculateProductivityImpact,
|
|
1324
|
+
DEFAULT_COST_CONFIG
|
|
1325
|
+
} from "@aiready/core";
|
|
1326
|
+
function calculateContextScore(summary, costConfig) {
|
|
1327
|
+
const {
|
|
1328
|
+
avgContextBudget,
|
|
1329
|
+
maxContextBudget,
|
|
1330
|
+
avgImportDepth,
|
|
1331
|
+
maxImportDepth,
|
|
1332
|
+
avgFragmentation,
|
|
1333
|
+
criticalIssues,
|
|
1334
|
+
majorIssues
|
|
1335
|
+
} = summary;
|
|
1336
|
+
const budgetScore = avgContextBudget < 5e3 ? 100 : Math.max(0, 100 - (avgContextBudget - 5e3) / 150);
|
|
1337
|
+
const depthScore = avgImportDepth < 5 ? 100 : Math.max(0, 100 - (avgImportDepth - 5) * 10);
|
|
1338
|
+
const fragmentationScore = avgFragmentation < 0.3 ? 100 : Math.max(0, 100 - (avgFragmentation - 0.3) * 200);
|
|
1339
|
+
const criticalPenalty = criticalIssues * 10;
|
|
1340
|
+
const majorPenalty = majorIssues * 3;
|
|
1341
|
+
const maxBudgetPenalty = maxContextBudget > 15e3 ? Math.min(20, (maxContextBudget - 15e3) / 500) : 0;
|
|
1342
|
+
const rawScore = budgetScore * 0.4 + depthScore * 0.3 + fragmentationScore * 0.3;
|
|
1343
|
+
const finalScore = rawScore - criticalPenalty - majorPenalty - maxBudgetPenalty;
|
|
1344
|
+
const score = Math.max(0, Math.min(100, Math.round(finalScore)));
|
|
1345
|
+
const factors = [
|
|
1346
|
+
{
|
|
1347
|
+
name: "Context Budget",
|
|
1348
|
+
impact: Math.round(budgetScore * 0.4 - 40),
|
|
1349
|
+
description: `Avg ${Math.round(avgContextBudget)} tokens per file ${avgContextBudget < 5e3 ? "(excellent)" : avgContextBudget < 1e4 ? "(acceptable)" : "(high)"}`
|
|
1350
|
+
},
|
|
1351
|
+
{
|
|
1352
|
+
name: "Import Depth",
|
|
1353
|
+
impact: Math.round(depthScore * 0.3 - 30),
|
|
1354
|
+
description: `Avg ${avgImportDepth.toFixed(1)} levels ${avgImportDepth < 5 ? "(excellent)" : avgImportDepth < 8 ? "(acceptable)" : "(deep)"}`
|
|
1355
|
+
},
|
|
1356
|
+
{
|
|
1357
|
+
name: "Fragmentation",
|
|
1358
|
+
impact: Math.round(fragmentationScore * 0.3 - 30),
|
|
1359
|
+
description: `${(avgFragmentation * 100).toFixed(0)}% fragmentation ${avgFragmentation < 0.3 ? "(well-organized)" : avgFragmentation < 0.5 ? "(moderate)" : "(high)"}`
|
|
1360
|
+
}
|
|
1361
|
+
];
|
|
1362
|
+
if (criticalIssues > 0) {
|
|
1363
|
+
factors.push({
|
|
1364
|
+
name: "Critical Issues",
|
|
1365
|
+
impact: -criticalPenalty,
|
|
1366
|
+
description: `${criticalIssues} critical context issue${criticalIssues > 1 ? "s" : ""}`
|
|
1367
|
+
});
|
|
1368
|
+
}
|
|
1369
|
+
if (majorIssues > 0) {
|
|
1370
|
+
factors.push({
|
|
1371
|
+
name: "Major Issues",
|
|
1372
|
+
impact: -majorPenalty,
|
|
1373
|
+
description: `${majorIssues} major context issue${majorIssues > 1 ? "s" : ""}`
|
|
1374
|
+
});
|
|
1375
|
+
}
|
|
1376
|
+
if (maxBudgetPenalty > 0) {
|
|
1377
|
+
factors.push({
|
|
1378
|
+
name: "Extreme File Detected",
|
|
1379
|
+
impact: -Math.round(maxBudgetPenalty),
|
|
1380
|
+
description: `One file requires ${Math.round(maxContextBudget)} tokens (very high)`
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1383
|
+
const recommendations = [];
|
|
1384
|
+
if (avgContextBudget > 1e4) {
|
|
1385
|
+
const estimatedImpact = Math.min(15, Math.round((avgContextBudget - 1e4) / 1e3));
|
|
1386
|
+
recommendations.push({
|
|
1387
|
+
action: "Reduce file dependencies to lower context requirements",
|
|
1388
|
+
estimatedImpact,
|
|
1389
|
+
priority: "high"
|
|
1390
|
+
});
|
|
1391
|
+
}
|
|
1392
|
+
if (avgImportDepth > 8) {
|
|
1393
|
+
const estimatedImpact = Math.min(10, Math.round((avgImportDepth - 8) * 2));
|
|
1394
|
+
recommendations.push({
|
|
1395
|
+
action: "Flatten import chains to reduce depth",
|
|
1396
|
+
estimatedImpact,
|
|
1397
|
+
priority: avgImportDepth > 10 ? "high" : "medium"
|
|
1398
|
+
});
|
|
1399
|
+
}
|
|
1400
|
+
if (avgFragmentation > 0.5) {
|
|
1401
|
+
const estimatedImpact = Math.min(12, Math.round((avgFragmentation - 0.5) * 40));
|
|
1402
|
+
recommendations.push({
|
|
1403
|
+
action: "Consolidate related code into cohesive modules",
|
|
1404
|
+
estimatedImpact,
|
|
1405
|
+
priority: "medium"
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1408
|
+
if (maxContextBudget > 2e4) {
|
|
1409
|
+
recommendations.push({
|
|
1410
|
+
action: `Split large file (${Math.round(maxContextBudget)} tokens) into smaller modules`,
|
|
1411
|
+
estimatedImpact: 8,
|
|
1412
|
+
priority: "high"
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
const cfg = { ...DEFAULT_COST_CONFIG, ...costConfig };
|
|
1416
|
+
const totalContextBudget = avgContextBudget * summary.totalFiles;
|
|
1417
|
+
const estimatedMonthlyCost = calculateMonthlyCost(totalContextBudget, cfg);
|
|
1418
|
+
const issues = [
|
|
1419
|
+
...Array(criticalIssues).fill({ severity: "critical" }),
|
|
1420
|
+
...Array(majorIssues).fill({ severity: "major" })
|
|
1421
|
+
];
|
|
1422
|
+
const productivityImpact = calculateProductivityImpact(issues);
|
|
1423
|
+
return {
|
|
1424
|
+
toolName: "context-analyzer",
|
|
1425
|
+
score,
|
|
1426
|
+
rawMetrics: {
|
|
1427
|
+
avgContextBudget: Math.round(avgContextBudget),
|
|
1428
|
+
maxContextBudget: Math.round(maxContextBudget),
|
|
1429
|
+
avgImportDepth: Math.round(avgImportDepth * 10) / 10,
|
|
1430
|
+
maxImportDepth,
|
|
1431
|
+
avgFragmentation: Math.round(avgFragmentation * 100) / 100,
|
|
1432
|
+
criticalIssues,
|
|
1433
|
+
majorIssues,
|
|
1434
|
+
// Business value metrics
|
|
1435
|
+
estimatedMonthlyCost,
|
|
1436
|
+
estimatedDeveloperHours: productivityImpact.totalHours
|
|
1437
|
+
},
|
|
1438
|
+
factors,
|
|
1439
|
+
recommendations
|
|
1440
|
+
};
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
// src/index.ts
|
|
1444
|
+
async function getSmartDefaults(directory, userOptions) {
|
|
1445
|
+
const files = await scanFiles({
|
|
1446
|
+
rootDir: directory,
|
|
1447
|
+
include: userOptions.include,
|
|
1448
|
+
exclude: userOptions.exclude
|
|
1449
|
+
});
|
|
1450
|
+
const estimatedBlocks = files.length;
|
|
1451
|
+
let maxDepth;
|
|
1452
|
+
let maxContextBudget;
|
|
1453
|
+
let minCohesion;
|
|
1454
|
+
let maxFragmentation;
|
|
1455
|
+
if (estimatedBlocks < 100) {
|
|
1456
|
+
maxDepth = 4;
|
|
1457
|
+
maxContextBudget = 8e3;
|
|
1458
|
+
minCohesion = 0.5;
|
|
1459
|
+
maxFragmentation = 0.5;
|
|
1460
|
+
} else if (estimatedBlocks < 500) {
|
|
1461
|
+
maxDepth = 5;
|
|
1462
|
+
maxContextBudget = 15e3;
|
|
1463
|
+
minCohesion = 0.45;
|
|
1464
|
+
maxFragmentation = 0.6;
|
|
1465
|
+
} else if (estimatedBlocks < 2e3) {
|
|
1466
|
+
maxDepth = 7;
|
|
1467
|
+
maxContextBudget = 25e3;
|
|
1468
|
+
minCohesion = 0.4;
|
|
1469
|
+
maxFragmentation = 0.7;
|
|
1470
|
+
} else {
|
|
1471
|
+
maxDepth = 10;
|
|
1472
|
+
maxContextBudget = 4e4;
|
|
1473
|
+
minCohesion = 0.35;
|
|
1474
|
+
maxFragmentation = 0.8;
|
|
1475
|
+
}
|
|
1476
|
+
return {
|
|
1477
|
+
maxDepth,
|
|
1478
|
+
maxContextBudget,
|
|
1479
|
+
minCohesion,
|
|
1480
|
+
maxFragmentation,
|
|
1481
|
+
focus: "all",
|
|
1482
|
+
includeNodeModules: false,
|
|
1483
|
+
rootDir: userOptions.rootDir || directory,
|
|
1484
|
+
include: userOptions.include,
|
|
1485
|
+
exclude: userOptions.exclude
|
|
1486
|
+
};
|
|
1487
|
+
}
|
|
1488
|
+
async function analyzeContext(options) {
|
|
1489
|
+
const {
|
|
1490
|
+
maxDepth = 5,
|
|
1491
|
+
maxContextBudget = 1e4,
|
|
1492
|
+
minCohesion = 0.6,
|
|
1493
|
+
maxFragmentation = 0.5,
|
|
1494
|
+
focus = "all",
|
|
1495
|
+
includeNodeModules = false,
|
|
1496
|
+
...scanOptions
|
|
1497
|
+
} = options;
|
|
1498
|
+
const files = await scanFiles({
|
|
1499
|
+
...scanOptions,
|
|
1500
|
+
// Only add node_modules to exclude if includeNodeModules is false
|
|
1501
|
+
// The DEFAULT_EXCLUDE already includes node_modules, so this is only needed
|
|
1502
|
+
// if user overrides the default exclude list
|
|
1503
|
+
exclude: includeNodeModules && scanOptions.exclude ? scanOptions.exclude.filter((pattern) => pattern !== "**/node_modules/**") : scanOptions.exclude
|
|
1504
|
+
});
|
|
1505
|
+
const pythonFiles = files.filter((f) => f.toLowerCase().endsWith(".py"));
|
|
1506
|
+
const tsJsFiles = files.filter((f) => !f.toLowerCase().endsWith(".py"));
|
|
1507
|
+
const fileContents = await Promise.all(
|
|
1508
|
+
files.map(async (file) => ({
|
|
1509
|
+
file,
|
|
1510
|
+
content: await readFileContent(file)
|
|
1511
|
+
}))
|
|
1512
|
+
);
|
|
1513
|
+
const graph = buildDependencyGraph(fileContents.filter((f) => !f.file.toLowerCase().endsWith(".py")));
|
|
1514
|
+
let pythonResults = [];
|
|
1515
|
+
if (pythonFiles.length > 0) {
|
|
1516
|
+
const { analyzePythonContext } = await import("./python-context-UOPTQH44.mjs");
|
|
1517
|
+
const pythonMetrics = await analyzePythonContext(pythonFiles, scanOptions.rootDir || options.rootDir || ".");
|
|
1518
|
+
pythonResults = pythonMetrics.map((metric) => {
|
|
1519
|
+
const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
|
|
1520
|
+
file: metric.file,
|
|
1521
|
+
importDepth: metric.importDepth,
|
|
1522
|
+
contextBudget: metric.contextBudget,
|
|
1523
|
+
cohesionScore: metric.cohesion,
|
|
1524
|
+
fragmentationScore: 0,
|
|
1525
|
+
// Python analyzer doesn't calculate fragmentation yet
|
|
1526
|
+
maxDepth,
|
|
1527
|
+
maxContextBudget,
|
|
1528
|
+
minCohesion,
|
|
1529
|
+
maxFragmentation,
|
|
1530
|
+
circularDeps: metric.metrics.circularDependencies.map((cycle) => cycle.split(" \u2192 "))
|
|
1531
|
+
});
|
|
1532
|
+
return {
|
|
1533
|
+
file: metric.file,
|
|
1534
|
+
tokenCost: Math.floor(metric.contextBudget / (1 + metric.imports.length || 1)),
|
|
1535
|
+
// Estimate
|
|
1536
|
+
linesOfCode: metric.metrics.linesOfCode,
|
|
1537
|
+
importDepth: metric.importDepth,
|
|
1538
|
+
dependencyCount: metric.imports.length,
|
|
1539
|
+
dependencyList: metric.imports.map((imp) => imp.resolvedPath || imp.source),
|
|
1540
|
+
circularDeps: metric.metrics.circularDependencies.map((cycle) => cycle.split(" \u2192 ")),
|
|
1541
|
+
cohesionScore: metric.cohesion,
|
|
1542
|
+
domains: ["python"],
|
|
1543
|
+
// Generic for now
|
|
1544
|
+
exportCount: metric.exports.length,
|
|
1545
|
+
contextBudget: metric.contextBudget,
|
|
1546
|
+
fragmentationScore: 0,
|
|
1547
|
+
relatedFiles: [],
|
|
1548
|
+
fileClassification: "unknown",
|
|
1549
|
+
// Python files not yet classified
|
|
1550
|
+
severity,
|
|
1551
|
+
issues,
|
|
1552
|
+
recommendations,
|
|
1553
|
+
potentialSavings
|
|
1554
|
+
};
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1557
|
+
const circularDeps = detectCircularDependencies(graph);
|
|
1558
|
+
const useLogScale = files.length >= 500;
|
|
1559
|
+
const clusters = detectModuleClusters(graph, { useLogScale });
|
|
1560
|
+
const fragmentationMap = /* @__PURE__ */ new Map();
|
|
1561
|
+
for (const cluster of clusters) {
|
|
1562
|
+
for (const file of cluster.files) {
|
|
1563
|
+
fragmentationMap.set(file, cluster.fragmentationScore);
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
const results = [];
|
|
1567
|
+
for (const { file } of fileContents) {
|
|
1568
|
+
const node = graph.nodes.get(file);
|
|
1569
|
+
if (!node) continue;
|
|
1570
|
+
const importDepth = focus === "depth" || focus === "all" ? calculateImportDepth(file, graph) : 0;
|
|
1571
|
+
const dependencyList = focus === "depth" || focus === "all" ? getTransitiveDependencies(file, graph) : [];
|
|
1572
|
+
const contextBudget = focus === "all" ? calculateContextBudget(file, graph) : node.tokenCost;
|
|
1573
|
+
const cohesionScore = focus === "cohesion" || focus === "all" ? calculateCohesion(node.exports, file, { coUsageMatrix: graph.coUsageMatrix }) : 1;
|
|
1574
|
+
const fragmentationScore = fragmentationMap.get(file) || 0;
|
|
1575
|
+
const relatedFiles = [];
|
|
1576
|
+
for (const cluster of clusters) {
|
|
1577
|
+
if (cluster.files.includes(file)) {
|
|
1578
|
+
relatedFiles.push(...cluster.files.filter((f) => f !== file));
|
|
1579
|
+
break;
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
|
|
1583
|
+
file,
|
|
1584
|
+
importDepth,
|
|
1585
|
+
contextBudget,
|
|
1586
|
+
cohesionScore,
|
|
1587
|
+
fragmentationScore,
|
|
1588
|
+
maxDepth,
|
|
1589
|
+
maxContextBudget,
|
|
1590
|
+
minCohesion,
|
|
1591
|
+
maxFragmentation,
|
|
1592
|
+
circularDeps
|
|
1593
|
+
});
|
|
1594
|
+
const domains = [
|
|
1595
|
+
...new Set(node.exports.map((e) => e.inferredDomain || "unknown"))
|
|
1596
|
+
];
|
|
1597
|
+
const fileClassification = classifyFile(node, cohesionScore, domains);
|
|
1598
|
+
const adjustedCohesionScore = adjustCohesionForClassification(
|
|
1599
|
+
cohesionScore,
|
|
1600
|
+
fileClassification,
|
|
1601
|
+
node
|
|
1602
|
+
);
|
|
1603
|
+
const adjustedFragmentationScore = adjustFragmentationForClassification(
|
|
1604
|
+
fragmentationScore,
|
|
1605
|
+
fileClassification
|
|
1606
|
+
);
|
|
1607
|
+
const classificationRecommendations = getClassificationRecommendations(
|
|
1608
|
+
fileClassification,
|
|
1609
|
+
file,
|
|
1610
|
+
issues
|
|
1611
|
+
);
|
|
1612
|
+
const {
|
|
1613
|
+
severity: adjustedSeverity,
|
|
1614
|
+
issues: adjustedIssues,
|
|
1615
|
+
recommendations: finalRecommendations,
|
|
1616
|
+
potentialSavings: adjustedSavings
|
|
1617
|
+
} = analyzeIssues({
|
|
1618
|
+
file,
|
|
1619
|
+
importDepth,
|
|
1620
|
+
contextBudget,
|
|
1621
|
+
cohesionScore: adjustedCohesionScore,
|
|
1622
|
+
// Use adjusted cohesion
|
|
1623
|
+
fragmentationScore: adjustedFragmentationScore,
|
|
1624
|
+
maxDepth,
|
|
1625
|
+
maxContextBudget,
|
|
1626
|
+
minCohesion,
|
|
1627
|
+
maxFragmentation,
|
|
1628
|
+
circularDeps
|
|
1629
|
+
});
|
|
1630
|
+
results.push({
|
|
1631
|
+
file,
|
|
1632
|
+
tokenCost: node.tokenCost,
|
|
1633
|
+
linesOfCode: node.linesOfCode,
|
|
1634
|
+
importDepth,
|
|
1635
|
+
dependencyCount: dependencyList.length,
|
|
1636
|
+
dependencyList,
|
|
1637
|
+
circularDeps: circularDeps.filter((cycle) => cycle.includes(file)),
|
|
1638
|
+
cohesionScore: adjustedCohesionScore,
|
|
1639
|
+
// Report adjusted cohesion
|
|
1640
|
+
domains,
|
|
1641
|
+
exportCount: node.exports.length,
|
|
1642
|
+
contextBudget,
|
|
1643
|
+
fragmentationScore: adjustedFragmentationScore,
|
|
1644
|
+
relatedFiles,
|
|
1645
|
+
fileClassification,
|
|
1646
|
+
severity: adjustedSeverity,
|
|
1647
|
+
issues: adjustedIssues,
|
|
1648
|
+
recommendations: [...finalRecommendations, ...classificationRecommendations.slice(0, 1)],
|
|
1649
|
+
potentialSavings: adjustedSavings
|
|
1650
|
+
});
|
|
1651
|
+
}
|
|
1652
|
+
const allResults = [...results, ...pythonResults];
|
|
1653
|
+
const sorted = allResults.sort((a, b) => {
|
|
1654
|
+
const severityOrder = { critical: 0, major: 1, minor: 2, info: 3 };
|
|
1655
|
+
const severityDiff = severityOrder[a.severity] - severityOrder[b.severity];
|
|
1656
|
+
if (severityDiff !== 0) return severityDiff;
|
|
1657
|
+
return b.contextBudget - a.contextBudget;
|
|
1658
|
+
});
|
|
1659
|
+
return sorted;
|
|
1660
|
+
}
|
|
1661
|
+
function generateSummary(results) {
|
|
1662
|
+
if (results.length === 0) {
|
|
1663
|
+
return {
|
|
1664
|
+
totalFiles: 0,
|
|
1665
|
+
totalTokens: 0,
|
|
1666
|
+
avgContextBudget: 0,
|
|
1667
|
+
maxContextBudget: 0,
|
|
1668
|
+
avgImportDepth: 0,
|
|
1669
|
+
maxImportDepth: 0,
|
|
1670
|
+
deepFiles: [],
|
|
1671
|
+
avgFragmentation: 0,
|
|
1672
|
+
fragmentedModules: [],
|
|
1673
|
+
avgCohesion: 0,
|
|
1674
|
+
lowCohesionFiles: [],
|
|
1675
|
+
criticalIssues: 0,
|
|
1676
|
+
majorIssues: 0,
|
|
1677
|
+
minorIssues: 0,
|
|
1678
|
+
totalPotentialSavings: 0,
|
|
1679
|
+
topExpensiveFiles: []
|
|
1680
|
+
};
|
|
1681
|
+
}
|
|
1682
|
+
const totalFiles = results.length;
|
|
1683
|
+
const totalTokens = results.reduce((sum, r) => sum + r.tokenCost, 0);
|
|
1684
|
+
const totalContextBudget = results.reduce(
|
|
1685
|
+
(sum, r) => sum + r.contextBudget,
|
|
1686
|
+
0
|
|
1687
|
+
);
|
|
1688
|
+
const avgContextBudget = totalContextBudget / totalFiles;
|
|
1689
|
+
const maxContextBudget = Math.max(...results.map((r) => r.contextBudget));
|
|
1690
|
+
const avgImportDepth = results.reduce((sum, r) => sum + r.importDepth, 0) / totalFiles;
|
|
1691
|
+
const maxImportDepth = Math.max(...results.map((r) => r.importDepth));
|
|
1692
|
+
const deepFiles = results.filter((r) => r.importDepth >= 5).map((r) => ({ file: r.file, depth: r.importDepth })).sort((a, b) => b.depth - a.depth).slice(0, 10);
|
|
1693
|
+
const avgFragmentation = results.reduce((sum, r) => sum + r.fragmentationScore, 0) / totalFiles;
|
|
1694
|
+
const moduleMap = /* @__PURE__ */ new Map();
|
|
1695
|
+
for (const result of results) {
|
|
1696
|
+
for (const domain of result.domains) {
|
|
1697
|
+
if (!moduleMap.has(domain)) {
|
|
1698
|
+
moduleMap.set(domain, []);
|
|
1699
|
+
}
|
|
1700
|
+
moduleMap.get(domain).push(result);
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
const fragmentedModules = [];
|
|
1704
|
+
for (const [domain, files] of moduleMap.entries()) {
|
|
1705
|
+
let jaccard2 = function(a, b) {
|
|
1706
|
+
const s1 = new Set(a || []);
|
|
1707
|
+
const s2 = new Set(b || []);
|
|
1708
|
+
if (s1.size === 0 && s2.size === 0) return 0;
|
|
1709
|
+
const inter = new Set([...s1].filter((x) => s2.has(x)));
|
|
1710
|
+
const uni = /* @__PURE__ */ new Set([...s1, ...s2]);
|
|
1711
|
+
return uni.size === 0 ? 0 : inter.size / uni.size;
|
|
1712
|
+
};
|
|
1713
|
+
var jaccard = jaccard2;
|
|
1714
|
+
if (files.length < 2) continue;
|
|
1715
|
+
const fragmentationScore = files.reduce((sum, f) => sum + f.fragmentationScore, 0) / files.length;
|
|
1716
|
+
if (fragmentationScore < 0.3) continue;
|
|
1717
|
+
const totalTokens2 = files.reduce((sum, f) => sum + f.tokenCost, 0);
|
|
1718
|
+
const avgCohesion2 = files.reduce((sum, f) => sum + f.cohesionScore, 0) / files.length;
|
|
1719
|
+
const targetFiles = Math.max(1, Math.ceil(files.length / 3));
|
|
1720
|
+
const filePaths = files.map((f) => f.file);
|
|
1721
|
+
const pathEntropy = calculatePathEntropy(filePaths);
|
|
1722
|
+
const directoryDistance = calculateDirectoryDistance(filePaths);
|
|
1723
|
+
let importSimTotal = 0;
|
|
1724
|
+
let importPairs = 0;
|
|
1725
|
+
for (let i = 0; i < files.length; i++) {
|
|
1726
|
+
for (let j = i + 1; j < files.length; j++) {
|
|
1727
|
+
importSimTotal += jaccard2(files[i].dependencyList || [], files[j].dependencyList || []);
|
|
1728
|
+
importPairs++;
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
const importCohesion = importPairs > 0 ? importSimTotal / importPairs : 0;
|
|
1732
|
+
fragmentedModules.push({
|
|
1733
|
+
domain,
|
|
1734
|
+
files: files.map((f) => f.file),
|
|
1735
|
+
totalTokens: totalTokens2,
|
|
1736
|
+
fragmentationScore,
|
|
1737
|
+
pathEntropy,
|
|
1738
|
+
directoryDistance,
|
|
1739
|
+
importCohesion,
|
|
1740
|
+
avgCohesion: avgCohesion2,
|
|
1741
|
+
suggestedStructure: {
|
|
1742
|
+
targetFiles,
|
|
1743
|
+
consolidationPlan: [
|
|
1744
|
+
`Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s)`,
|
|
1745
|
+
`Current token cost: ${totalTokens2.toLocaleString()}`,
|
|
1746
|
+
`Estimated savings: ${Math.floor(totalTokens2 * 0.3).toLocaleString()} tokens (30%)`
|
|
1747
|
+
]
|
|
1748
|
+
}
|
|
1749
|
+
});
|
|
1750
|
+
}
|
|
1751
|
+
fragmentedModules.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
|
|
1752
|
+
const avgCohesion = results.reduce((sum, r) => sum + r.cohesionScore, 0) / totalFiles;
|
|
1753
|
+
const lowCohesionFiles = results.filter((r) => r.cohesionScore < 0.6).map((r) => ({ file: r.file, score: r.cohesionScore })).sort((a, b) => a.score - b.score).slice(0, 10);
|
|
1754
|
+
const criticalIssues = results.filter((r) => r.severity === "critical").length;
|
|
1755
|
+
const majorIssues = results.filter((r) => r.severity === "major").length;
|
|
1756
|
+
const minorIssues = results.filter((r) => r.severity === "minor").length;
|
|
1757
|
+
const totalPotentialSavings = results.reduce(
|
|
1758
|
+
(sum, r) => sum + r.potentialSavings,
|
|
1759
|
+
0
|
|
1760
|
+
);
|
|
1761
|
+
const topExpensiveFiles = results.sort((a, b) => b.contextBudget - a.contextBudget).slice(0, 10).map((r) => ({
|
|
1762
|
+
file: r.file,
|
|
1763
|
+
contextBudget: r.contextBudget,
|
|
1764
|
+
severity: r.severity
|
|
1765
|
+
}));
|
|
1766
|
+
return {
|
|
1767
|
+
totalFiles,
|
|
1768
|
+
totalTokens,
|
|
1769
|
+
avgContextBudget,
|
|
1770
|
+
maxContextBudget,
|
|
1771
|
+
avgImportDepth,
|
|
1772
|
+
maxImportDepth,
|
|
1773
|
+
deepFiles,
|
|
1774
|
+
avgFragmentation,
|
|
1775
|
+
fragmentedModules: fragmentedModules.slice(0, 10),
|
|
1776
|
+
avgCohesion,
|
|
1777
|
+
lowCohesionFiles,
|
|
1778
|
+
criticalIssues,
|
|
1779
|
+
majorIssues,
|
|
1780
|
+
minorIssues,
|
|
1781
|
+
totalPotentialSavings,
|
|
1782
|
+
topExpensiveFiles
|
|
1783
|
+
};
|
|
1784
|
+
}
|
|
1785
|
+
function analyzeIssues(params) {
|
|
1786
|
+
const {
|
|
1787
|
+
file,
|
|
1788
|
+
importDepth,
|
|
1789
|
+
contextBudget,
|
|
1790
|
+
cohesionScore,
|
|
1791
|
+
fragmentationScore,
|
|
1792
|
+
maxDepth,
|
|
1793
|
+
maxContextBudget,
|
|
1794
|
+
minCohesion,
|
|
1795
|
+
maxFragmentation,
|
|
1796
|
+
circularDeps
|
|
1797
|
+
} = params;
|
|
1798
|
+
const issues = [];
|
|
1799
|
+
const recommendations = [];
|
|
1800
|
+
let severity = "info";
|
|
1801
|
+
let potentialSavings = 0;
|
|
1802
|
+
if (circularDeps.length > 0) {
|
|
1803
|
+
severity = "critical";
|
|
1804
|
+
issues.push(
|
|
1805
|
+
`Part of ${circularDeps.length} circular dependency chain(s)`
|
|
1806
|
+
);
|
|
1807
|
+
recommendations.push("Break circular dependencies by extracting interfaces or using dependency injection");
|
|
1808
|
+
potentialSavings += contextBudget * 0.2;
|
|
1809
|
+
}
|
|
1810
|
+
if (importDepth > maxDepth * 1.5) {
|
|
1811
|
+
severity = severity === "critical" ? "critical" : "critical";
|
|
1812
|
+
issues.push(`Import depth ${importDepth} exceeds limit by 50%`);
|
|
1813
|
+
recommendations.push("Flatten dependency tree or use facade pattern");
|
|
1814
|
+
potentialSavings += contextBudget * 0.3;
|
|
1815
|
+
} else if (importDepth > maxDepth) {
|
|
1816
|
+
severity = severity === "critical" ? "critical" : "major";
|
|
1817
|
+
issues.push(`Import depth ${importDepth} exceeds recommended maximum ${maxDepth}`);
|
|
1818
|
+
recommendations.push("Consider reducing dependency depth");
|
|
1819
|
+
potentialSavings += contextBudget * 0.15;
|
|
1820
|
+
}
|
|
1821
|
+
if (contextBudget > maxContextBudget * 1.5) {
|
|
1822
|
+
severity = severity === "critical" ? "critical" : "critical";
|
|
1823
|
+
issues.push(`Context budget ${contextBudget.toLocaleString()} tokens is 50% over limit`);
|
|
1824
|
+
recommendations.push("Split into smaller modules or reduce dependency tree");
|
|
1825
|
+
potentialSavings += contextBudget * 0.4;
|
|
1826
|
+
} else if (contextBudget > maxContextBudget) {
|
|
1827
|
+
severity = severity === "critical" || severity === "major" ? severity : "major";
|
|
1828
|
+
issues.push(`Context budget ${contextBudget.toLocaleString()} exceeds ${maxContextBudget.toLocaleString()}`);
|
|
1829
|
+
recommendations.push("Reduce file size or dependencies");
|
|
1830
|
+
potentialSavings += contextBudget * 0.2;
|
|
1831
|
+
}
|
|
1832
|
+
if (cohesionScore < minCohesion * 0.5) {
|
|
1833
|
+
severity = severity === "critical" ? "critical" : "major";
|
|
1834
|
+
issues.push(`Very low cohesion (${(cohesionScore * 100).toFixed(0)}%) - mixed concerns`);
|
|
1835
|
+
recommendations.push("Split file by domain - separate unrelated functionality");
|
|
1836
|
+
potentialSavings += contextBudget * 0.25;
|
|
1837
|
+
} else if (cohesionScore < minCohesion) {
|
|
1838
|
+
severity = severity === "critical" || severity === "major" ? severity : "minor";
|
|
1839
|
+
issues.push(`Low cohesion (${(cohesionScore * 100).toFixed(0)}%)`);
|
|
1840
|
+
recommendations.push("Consider grouping related exports together");
|
|
1841
|
+
potentialSavings += contextBudget * 0.1;
|
|
1842
|
+
}
|
|
1843
|
+
if (fragmentationScore > maxFragmentation) {
|
|
1844
|
+
severity = severity === "critical" || severity === "major" ? severity : "minor";
|
|
1845
|
+
issues.push(`High fragmentation (${(fragmentationScore * 100).toFixed(0)}%) - scattered implementation`);
|
|
1846
|
+
recommendations.push("Consolidate with related files in same domain");
|
|
1847
|
+
potentialSavings += contextBudget * 0.3;
|
|
1848
|
+
}
|
|
1849
|
+
if (issues.length === 0) {
|
|
1850
|
+
issues.push("No significant issues detected");
|
|
1851
|
+
recommendations.push("File is well-structured for AI context usage");
|
|
1852
|
+
}
|
|
1853
|
+
if (isBuildArtifact(file)) {
|
|
1854
|
+
issues.push("Detected build artifact (bundled/output file)");
|
|
1855
|
+
recommendations.push("Exclude build outputs (e.g., cdk.out, dist, build, .next) from analysis");
|
|
1856
|
+
severity = downgradeSeverity(severity);
|
|
1857
|
+
potentialSavings = 0;
|
|
1858
|
+
}
|
|
1859
|
+
return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
|
|
1860
|
+
}
|
|
1861
|
+
function isBuildArtifact(filePath) {
|
|
1862
|
+
const lower = filePath.toLowerCase();
|
|
1863
|
+
return lower.includes("/node_modules/") || lower.includes("/dist/") || lower.includes("/build/") || lower.includes("/out/") || lower.includes("/output/") || lower.includes("/cdk.out/") || lower.includes("/.next/") || /\/asset\.[^/]+\//.test(lower);
|
|
1864
|
+
}
|
|
1865
|
+
function downgradeSeverity(s) {
|
|
1866
|
+
switch (s) {
|
|
1867
|
+
case "critical":
|
|
1868
|
+
return "minor";
|
|
1869
|
+
case "major":
|
|
1870
|
+
return "minor";
|
|
1871
|
+
case "minor":
|
|
1872
|
+
return "info";
|
|
1873
|
+
default:
|
|
1874
|
+
return "info";
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
export {
|
|
1879
|
+
buildCoUsageMatrix,
|
|
1880
|
+
buildTypeGraph,
|
|
1881
|
+
findSemanticClusters,
|
|
1882
|
+
calculateDomainConfidence,
|
|
1883
|
+
inferDomainFromSemantics,
|
|
1884
|
+
getCoUsageData,
|
|
1885
|
+
findConsolidationCandidates,
|
|
1886
|
+
classifyFile,
|
|
1887
|
+
adjustFragmentationForClassification,
|
|
1888
|
+
calculateContextScore,
|
|
1889
|
+
getSmartDefaults,
|
|
1890
|
+
analyzeContext,
|
|
1891
|
+
generateSummary
|
|
1892
|
+
};
|