@aiready/context-analyzer 0.9.23 → 0.9.25

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.
@@ -0,0 +1,1422 @@
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 c of counts) {
404
+ const p = c / total;
405
+ entropy -= p * Math.log2(p);
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 p of probs) {
674
+ entropy -= p * Math.log2(p);
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 count of domainCounts.values()) {
715
+ const p = count / total;
716
+ if (p > 0) {
717
+ entropy -= p * Math.log2(p);
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
+ const uniqueDomains = domains.filter((d) => d !== "unknown");
735
+ const hasSingleDomain = uniqueDomains.length <= 1;
736
+ const hasReasonableCohesion = cohesionScore >= 0.5;
737
+ if (hasSingleDomain) {
738
+ return "cohesive-module";
739
+ }
740
+ if (isUtilityFile(node)) {
741
+ return "cohesive-module";
742
+ }
743
+ const hasMultipleDomains = uniqueDomains.length > 1;
744
+ const hasLowCohesion = cohesionScore < 0.4;
745
+ if (hasMultipleDomains && hasLowCohesion) {
746
+ return "mixed-concerns";
747
+ }
748
+ if (cohesionScore >= 0.5) {
749
+ return "cohesive-module";
750
+ }
751
+ return "unknown";
752
+ }
753
+ function isBarrelExport(node) {
754
+ const { file, exports, imports, linesOfCode } = node;
755
+ const fileName = file.split("/").pop()?.toLowerCase();
756
+ const isIndexFile = fileName === "index.ts" || fileName === "index.js" || fileName === "index.tsx" || fileName === "index.jsx";
757
+ const hasReExports = exports.length > 0 && imports.length > 0;
758
+ const highExportToLinesRatio = exports.length > 3 && linesOfCode < exports.length * 5;
759
+ const sparseCode = linesOfCode > 0 && linesOfCode < 50 && exports.length >= 2;
760
+ if (isIndexFile && hasReExports) {
761
+ return true;
762
+ }
763
+ if (highExportToLinesRatio && imports.length >= exports.length * 0.5) {
764
+ return true;
765
+ }
766
+ if (sparseCode && imports.length > 0) {
767
+ return true;
768
+ }
769
+ return false;
770
+ }
771
+ function isTypeDefinitionFile(node) {
772
+ const { file, exports } = node;
773
+ const fileName = file.split("/").pop()?.toLowerCase();
774
+ const isTypesFile = fileName?.includes("types") || fileName?.includes(".d.ts") || fileName === "types.ts" || fileName === "interfaces.ts";
775
+ const typeExports = exports.filter((e) => e.type === "type" || e.type === "interface");
776
+ const runtimeExports = exports.filter((e) => e.type === "function" || e.type === "class" || e.type === "const");
777
+ const mostlyTypes = exports.length > 0 && typeExports.length > runtimeExports.length && typeExports.length / exports.length > 0.7;
778
+ return isTypesFile || mostlyTypes;
779
+ }
780
+ function isConfigOrSchemaFile(node) {
781
+ const { file, exports } = node;
782
+ const fileName = file.split("/").pop()?.toLowerCase();
783
+ const configPatterns = [
784
+ "config",
785
+ "schema",
786
+ "settings",
787
+ "options",
788
+ "constants",
789
+ "env",
790
+ "environment",
791
+ ".config.",
792
+ "-config.",
793
+ "_config."
794
+ ];
795
+ const isConfigName = configPatterns.some(
796
+ (pattern) => fileName?.includes(pattern) || fileName?.startsWith(pattern) || fileName?.endsWith(`${pattern}.ts`)
797
+ );
798
+ const isConfigPath = file.toLowerCase().includes("/config/") || file.toLowerCase().includes("/schemas/") || file.toLowerCase().includes("/settings/");
799
+ const hasSchemaExports = exports.some(
800
+ (e) => e.name.toLowerCase().includes("table") || e.name.toLowerCase().includes("schema") || e.name.toLowerCase().includes("config") || e.name.toLowerCase().includes("setting")
801
+ );
802
+ return isConfigName || isConfigPath || hasSchemaExports;
803
+ }
804
+ function isUtilityFile(node) {
805
+ const { file, exports } = node;
806
+ const fileName = file.split("/").pop()?.toLowerCase();
807
+ const utilityPatterns = [
808
+ "util",
809
+ "utility",
810
+ "utilities",
811
+ "helper",
812
+ "helpers",
813
+ "common",
814
+ "shared",
815
+ "lib",
816
+ "toolbox",
817
+ "toolkit",
818
+ ".util.",
819
+ "-util.",
820
+ "_util."
821
+ ];
822
+ const isUtilityName = utilityPatterns.some(
823
+ (pattern) => fileName?.includes(pattern)
824
+ );
825
+ const isUtilityPath = file.toLowerCase().includes("/utils/") || file.toLowerCase().includes("/helpers/") || file.toLowerCase().includes("/lib/") || file.toLowerCase().includes("/common/");
826
+ const hasManySmallExports = exports.length >= 3 && exports.every(
827
+ (e) => e.type === "function" || e.type === "const"
828
+ );
829
+ return isUtilityName || isUtilityPath || hasManySmallExports;
830
+ }
831
+ function adjustFragmentationForClassification(baseFragmentation, classification) {
832
+ switch (classification) {
833
+ case "barrel-export":
834
+ return 0;
835
+ case "type-definition":
836
+ return 0;
837
+ case "cohesive-module":
838
+ return baseFragmentation * 0.3;
839
+ case "mixed-concerns":
840
+ return baseFragmentation;
841
+ default:
842
+ return baseFragmentation * 0.7;
843
+ }
844
+ }
845
+ function getClassificationRecommendations(classification, file, issues) {
846
+ switch (classification) {
847
+ case "barrel-export":
848
+ return [
849
+ "Barrel export file detected - multiple domains are expected here",
850
+ "Consider if this barrel export improves or hinders discoverability"
851
+ ];
852
+ case "type-definition":
853
+ return [
854
+ "Type definition file - centralized types improve consistency",
855
+ "Consider splitting if file becomes too large (>500 lines)"
856
+ ];
857
+ case "cohesive-module":
858
+ return [
859
+ "Module has good cohesion despite its size",
860
+ "Consider documenting the module boundaries for AI assistants"
861
+ ];
862
+ case "mixed-concerns":
863
+ return [
864
+ "Consider splitting this file by domain",
865
+ "Identify independent responsibilities and extract them",
866
+ "Review import dependencies to understand coupling"
867
+ ];
868
+ default:
869
+ return issues;
870
+ }
871
+ }
872
+
873
+ // src/scoring.ts
874
+ function calculateContextScore(summary) {
875
+ const {
876
+ avgContextBudget,
877
+ maxContextBudget,
878
+ avgImportDepth,
879
+ maxImportDepth,
880
+ avgFragmentation,
881
+ criticalIssues,
882
+ majorIssues
883
+ } = summary;
884
+ const budgetScore = avgContextBudget < 5e3 ? 100 : Math.max(0, 100 - (avgContextBudget - 5e3) / 150);
885
+ const depthScore = avgImportDepth < 5 ? 100 : Math.max(0, 100 - (avgImportDepth - 5) * 10);
886
+ const fragmentationScore = avgFragmentation < 0.3 ? 100 : Math.max(0, 100 - (avgFragmentation - 0.3) * 200);
887
+ const criticalPenalty = criticalIssues * 10;
888
+ const majorPenalty = majorIssues * 3;
889
+ const maxBudgetPenalty = maxContextBudget > 15e3 ? Math.min(20, (maxContextBudget - 15e3) / 500) : 0;
890
+ const rawScore = budgetScore * 0.4 + depthScore * 0.3 + fragmentationScore * 0.3;
891
+ const finalScore = rawScore - criticalPenalty - majorPenalty - maxBudgetPenalty;
892
+ const score = Math.max(0, Math.min(100, Math.round(finalScore)));
893
+ const factors = [
894
+ {
895
+ name: "Context Budget",
896
+ impact: Math.round(budgetScore * 0.4 - 40),
897
+ description: `Avg ${Math.round(avgContextBudget)} tokens per file ${avgContextBudget < 5e3 ? "(excellent)" : avgContextBudget < 1e4 ? "(acceptable)" : "(high)"}`
898
+ },
899
+ {
900
+ name: "Import Depth",
901
+ impact: Math.round(depthScore * 0.3 - 30),
902
+ description: `Avg ${avgImportDepth.toFixed(1)} levels ${avgImportDepth < 5 ? "(excellent)" : avgImportDepth < 8 ? "(acceptable)" : "(deep)"}`
903
+ },
904
+ {
905
+ name: "Fragmentation",
906
+ impact: Math.round(fragmentationScore * 0.3 - 30),
907
+ description: `${(avgFragmentation * 100).toFixed(0)}% fragmentation ${avgFragmentation < 0.3 ? "(well-organized)" : avgFragmentation < 0.5 ? "(moderate)" : "(high)"}`
908
+ }
909
+ ];
910
+ if (criticalIssues > 0) {
911
+ factors.push({
912
+ name: "Critical Issues",
913
+ impact: -criticalPenalty,
914
+ description: `${criticalIssues} critical context issue${criticalIssues > 1 ? "s" : ""}`
915
+ });
916
+ }
917
+ if (majorIssues > 0) {
918
+ factors.push({
919
+ name: "Major Issues",
920
+ impact: -majorPenalty,
921
+ description: `${majorIssues} major context issue${majorIssues > 1 ? "s" : ""}`
922
+ });
923
+ }
924
+ if (maxBudgetPenalty > 0) {
925
+ factors.push({
926
+ name: "Extreme File Detected",
927
+ impact: -Math.round(maxBudgetPenalty),
928
+ description: `One file requires ${Math.round(maxContextBudget)} tokens (very high)`
929
+ });
930
+ }
931
+ const recommendations = [];
932
+ if (avgContextBudget > 1e4) {
933
+ const estimatedImpact = Math.min(15, Math.round((avgContextBudget - 1e4) / 1e3));
934
+ recommendations.push({
935
+ action: "Reduce file dependencies to lower context requirements",
936
+ estimatedImpact,
937
+ priority: "high"
938
+ });
939
+ }
940
+ if (avgImportDepth > 8) {
941
+ const estimatedImpact = Math.min(10, Math.round((avgImportDepth - 8) * 2));
942
+ recommendations.push({
943
+ action: "Flatten import chains to reduce depth",
944
+ estimatedImpact,
945
+ priority: avgImportDepth > 10 ? "high" : "medium"
946
+ });
947
+ }
948
+ if (avgFragmentation > 0.5) {
949
+ const estimatedImpact = Math.min(12, Math.round((avgFragmentation - 0.5) * 40));
950
+ recommendations.push({
951
+ action: "Consolidate related code into cohesive modules",
952
+ estimatedImpact,
953
+ priority: "medium"
954
+ });
955
+ }
956
+ if (maxContextBudget > 2e4) {
957
+ recommendations.push({
958
+ action: `Split large file (${Math.round(maxContextBudget)} tokens) into smaller modules`,
959
+ estimatedImpact: 8,
960
+ priority: "high"
961
+ });
962
+ }
963
+ return {
964
+ toolName: "context-analyzer",
965
+ score,
966
+ rawMetrics: {
967
+ avgContextBudget: Math.round(avgContextBudget),
968
+ maxContextBudget: Math.round(maxContextBudget),
969
+ avgImportDepth: Math.round(avgImportDepth * 10) / 10,
970
+ maxImportDepth,
971
+ avgFragmentation: Math.round(avgFragmentation * 100) / 100,
972
+ criticalIssues,
973
+ majorIssues
974
+ },
975
+ factors,
976
+ recommendations
977
+ };
978
+ }
979
+
980
+ // src/index.ts
981
+ async function getSmartDefaults(directory, userOptions) {
982
+ const files = await scanFiles({
983
+ rootDir: directory,
984
+ include: userOptions.include,
985
+ exclude: userOptions.exclude
986
+ });
987
+ const estimatedBlocks = files.length;
988
+ let maxDepth;
989
+ let maxContextBudget;
990
+ let minCohesion;
991
+ let maxFragmentation;
992
+ if (estimatedBlocks < 100) {
993
+ maxDepth = 4;
994
+ maxContextBudget = 8e3;
995
+ minCohesion = 0.5;
996
+ maxFragmentation = 0.5;
997
+ } else if (estimatedBlocks < 500) {
998
+ maxDepth = 5;
999
+ maxContextBudget = 15e3;
1000
+ minCohesion = 0.45;
1001
+ maxFragmentation = 0.6;
1002
+ } else if (estimatedBlocks < 2e3) {
1003
+ maxDepth = 7;
1004
+ maxContextBudget = 25e3;
1005
+ minCohesion = 0.4;
1006
+ maxFragmentation = 0.7;
1007
+ } else {
1008
+ maxDepth = 10;
1009
+ maxContextBudget = 4e4;
1010
+ minCohesion = 0.35;
1011
+ maxFragmentation = 0.8;
1012
+ }
1013
+ return {
1014
+ maxDepth,
1015
+ maxContextBudget,
1016
+ minCohesion,
1017
+ maxFragmentation,
1018
+ focus: "all",
1019
+ includeNodeModules: false,
1020
+ rootDir: userOptions.rootDir || directory,
1021
+ include: userOptions.include,
1022
+ exclude: userOptions.exclude
1023
+ };
1024
+ }
1025
+ async function analyzeContext(options) {
1026
+ const {
1027
+ maxDepth = 5,
1028
+ maxContextBudget = 1e4,
1029
+ minCohesion = 0.6,
1030
+ maxFragmentation = 0.5,
1031
+ focus = "all",
1032
+ includeNodeModules = false,
1033
+ ...scanOptions
1034
+ } = options;
1035
+ const files = await scanFiles({
1036
+ ...scanOptions,
1037
+ // Only add node_modules to exclude if includeNodeModules is false
1038
+ // The DEFAULT_EXCLUDE already includes node_modules, so this is only needed
1039
+ // if user overrides the default exclude list
1040
+ exclude: includeNodeModules && scanOptions.exclude ? scanOptions.exclude.filter((pattern) => pattern !== "**/node_modules/**") : scanOptions.exclude
1041
+ });
1042
+ const pythonFiles = files.filter((f) => f.toLowerCase().endsWith(".py"));
1043
+ const tsJsFiles = files.filter((f) => !f.toLowerCase().endsWith(".py"));
1044
+ const fileContents = await Promise.all(
1045
+ files.map(async (file) => ({
1046
+ file,
1047
+ content: await readFileContent(file)
1048
+ }))
1049
+ );
1050
+ const graph = buildDependencyGraph(fileContents.filter((f) => !f.file.toLowerCase().endsWith(".py")));
1051
+ let pythonResults = [];
1052
+ if (pythonFiles.length > 0) {
1053
+ const { analyzePythonContext } = await import("./python-context-UOPTQH44.mjs");
1054
+ const pythonMetrics = await analyzePythonContext(pythonFiles, scanOptions.rootDir || options.rootDir || ".");
1055
+ pythonResults = pythonMetrics.map((metric) => {
1056
+ const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
1057
+ file: metric.file,
1058
+ importDepth: metric.importDepth,
1059
+ contextBudget: metric.contextBudget,
1060
+ cohesionScore: metric.cohesion,
1061
+ fragmentationScore: 0,
1062
+ // Python analyzer doesn't calculate fragmentation yet
1063
+ maxDepth,
1064
+ maxContextBudget,
1065
+ minCohesion,
1066
+ maxFragmentation,
1067
+ circularDeps: metric.metrics.circularDependencies.map((cycle) => cycle.split(" \u2192 "))
1068
+ });
1069
+ return {
1070
+ file: metric.file,
1071
+ tokenCost: Math.floor(metric.contextBudget / (1 + metric.imports.length || 1)),
1072
+ // Estimate
1073
+ linesOfCode: metric.metrics.linesOfCode,
1074
+ importDepth: metric.importDepth,
1075
+ dependencyCount: metric.imports.length,
1076
+ dependencyList: metric.imports.map((imp) => imp.resolvedPath || imp.source),
1077
+ circularDeps: metric.metrics.circularDependencies.map((cycle) => cycle.split(" \u2192 ")),
1078
+ cohesionScore: metric.cohesion,
1079
+ domains: ["python"],
1080
+ // Generic for now
1081
+ exportCount: metric.exports.length,
1082
+ contextBudget: metric.contextBudget,
1083
+ fragmentationScore: 0,
1084
+ relatedFiles: [],
1085
+ fileClassification: "unknown",
1086
+ // Python files not yet classified
1087
+ severity,
1088
+ issues,
1089
+ recommendations,
1090
+ potentialSavings
1091
+ };
1092
+ });
1093
+ }
1094
+ const circularDeps = detectCircularDependencies(graph);
1095
+ const useLogScale = files.length >= 500;
1096
+ const clusters = detectModuleClusters(graph, { useLogScale });
1097
+ const fragmentationMap = /* @__PURE__ */ new Map();
1098
+ for (const cluster of clusters) {
1099
+ for (const file of cluster.files) {
1100
+ fragmentationMap.set(file, cluster.fragmentationScore);
1101
+ }
1102
+ }
1103
+ const results = [];
1104
+ for (const { file } of fileContents) {
1105
+ const node = graph.nodes.get(file);
1106
+ if (!node) continue;
1107
+ const importDepth = focus === "depth" || focus === "all" ? calculateImportDepth(file, graph) : 0;
1108
+ const dependencyList = focus === "depth" || focus === "all" ? getTransitiveDependencies(file, graph) : [];
1109
+ const contextBudget = focus === "all" ? calculateContextBudget(file, graph) : node.tokenCost;
1110
+ const cohesionScore = focus === "cohesion" || focus === "all" ? calculateCohesion(node.exports, file) : 1;
1111
+ const fragmentationScore = fragmentationMap.get(file) || 0;
1112
+ const relatedFiles = [];
1113
+ for (const cluster of clusters) {
1114
+ if (cluster.files.includes(file)) {
1115
+ relatedFiles.push(...cluster.files.filter((f) => f !== file));
1116
+ break;
1117
+ }
1118
+ }
1119
+ const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
1120
+ file,
1121
+ importDepth,
1122
+ contextBudget,
1123
+ cohesionScore,
1124
+ fragmentationScore,
1125
+ maxDepth,
1126
+ maxContextBudget,
1127
+ minCohesion,
1128
+ maxFragmentation,
1129
+ circularDeps
1130
+ });
1131
+ const domains = [
1132
+ ...new Set(node.exports.map((e) => e.inferredDomain || "unknown"))
1133
+ ];
1134
+ const fileClassification = classifyFile(node, cohesionScore, domains);
1135
+ const adjustedFragmentationScore = adjustFragmentationForClassification(
1136
+ fragmentationScore,
1137
+ fileClassification
1138
+ );
1139
+ const classificationRecommendations = getClassificationRecommendations(
1140
+ fileClassification,
1141
+ file,
1142
+ issues
1143
+ );
1144
+ const {
1145
+ severity: adjustedSeverity,
1146
+ issues: adjustedIssues,
1147
+ recommendations: finalRecommendations,
1148
+ potentialSavings: adjustedSavings
1149
+ } = analyzeIssues({
1150
+ file,
1151
+ importDepth,
1152
+ contextBudget,
1153
+ cohesionScore,
1154
+ fragmentationScore: adjustedFragmentationScore,
1155
+ maxDepth,
1156
+ maxContextBudget,
1157
+ minCohesion,
1158
+ maxFragmentation,
1159
+ circularDeps
1160
+ });
1161
+ results.push({
1162
+ file,
1163
+ tokenCost: node.tokenCost,
1164
+ linesOfCode: node.linesOfCode,
1165
+ importDepth,
1166
+ dependencyCount: dependencyList.length,
1167
+ dependencyList,
1168
+ circularDeps: circularDeps.filter((cycle) => cycle.includes(file)),
1169
+ cohesionScore,
1170
+ domains,
1171
+ exportCount: node.exports.length,
1172
+ contextBudget,
1173
+ fragmentationScore: adjustedFragmentationScore,
1174
+ relatedFiles,
1175
+ fileClassification,
1176
+ severity: adjustedSeverity,
1177
+ issues: adjustedIssues,
1178
+ recommendations: [...finalRecommendations, ...classificationRecommendations.slice(0, 1)],
1179
+ potentialSavings: adjustedSavings
1180
+ });
1181
+ }
1182
+ const allResults = [...results, ...pythonResults];
1183
+ const sorted = allResults.sort((a, b) => {
1184
+ const severityOrder = { critical: 0, major: 1, minor: 2, info: 3 };
1185
+ const severityDiff = severityOrder[a.severity] - severityOrder[b.severity];
1186
+ if (severityDiff !== 0) return severityDiff;
1187
+ return b.contextBudget - a.contextBudget;
1188
+ });
1189
+ return sorted;
1190
+ }
1191
+ function generateSummary(results) {
1192
+ if (results.length === 0) {
1193
+ return {
1194
+ totalFiles: 0,
1195
+ totalTokens: 0,
1196
+ avgContextBudget: 0,
1197
+ maxContextBudget: 0,
1198
+ avgImportDepth: 0,
1199
+ maxImportDepth: 0,
1200
+ deepFiles: [],
1201
+ avgFragmentation: 0,
1202
+ fragmentedModules: [],
1203
+ avgCohesion: 0,
1204
+ lowCohesionFiles: [],
1205
+ criticalIssues: 0,
1206
+ majorIssues: 0,
1207
+ minorIssues: 0,
1208
+ totalPotentialSavings: 0,
1209
+ topExpensiveFiles: []
1210
+ };
1211
+ }
1212
+ const totalFiles = results.length;
1213
+ const totalTokens = results.reduce((sum, r) => sum + r.tokenCost, 0);
1214
+ const totalContextBudget = results.reduce(
1215
+ (sum, r) => sum + r.contextBudget,
1216
+ 0
1217
+ );
1218
+ const avgContextBudget = totalContextBudget / totalFiles;
1219
+ const maxContextBudget = Math.max(...results.map((r) => r.contextBudget));
1220
+ const avgImportDepth = results.reduce((sum, r) => sum + r.importDepth, 0) / totalFiles;
1221
+ const maxImportDepth = Math.max(...results.map((r) => r.importDepth));
1222
+ 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);
1223
+ const avgFragmentation = results.reduce((sum, r) => sum + r.fragmentationScore, 0) / totalFiles;
1224
+ const moduleMap = /* @__PURE__ */ new Map();
1225
+ for (const result of results) {
1226
+ for (const domain of result.domains) {
1227
+ if (!moduleMap.has(domain)) {
1228
+ moduleMap.set(domain, []);
1229
+ }
1230
+ moduleMap.get(domain).push(result);
1231
+ }
1232
+ }
1233
+ const fragmentedModules = [];
1234
+ for (const [domain, files] of moduleMap.entries()) {
1235
+ let jaccard2 = function(a, b) {
1236
+ const s1 = new Set(a || []);
1237
+ const s2 = new Set(b || []);
1238
+ if (s1.size === 0 && s2.size === 0) return 0;
1239
+ const inter = new Set([...s1].filter((x) => s2.has(x)));
1240
+ const uni = /* @__PURE__ */ new Set([...s1, ...s2]);
1241
+ return uni.size === 0 ? 0 : inter.size / uni.size;
1242
+ };
1243
+ var jaccard = jaccard2;
1244
+ if (files.length < 2) continue;
1245
+ const fragmentationScore = files.reduce((sum, f) => sum + f.fragmentationScore, 0) / files.length;
1246
+ if (fragmentationScore < 0.3) continue;
1247
+ const totalTokens2 = files.reduce((sum, f) => sum + f.tokenCost, 0);
1248
+ const avgCohesion2 = files.reduce((sum, f) => sum + f.cohesionScore, 0) / files.length;
1249
+ const targetFiles = Math.max(1, Math.ceil(files.length / 3));
1250
+ const filePaths = files.map((f) => f.file);
1251
+ const pathEntropy = calculatePathEntropy(filePaths);
1252
+ const directoryDistance = calculateDirectoryDistance(filePaths);
1253
+ let importSimTotal = 0;
1254
+ let importPairs = 0;
1255
+ for (let i = 0; i < files.length; i++) {
1256
+ for (let j = i + 1; j < files.length; j++) {
1257
+ importSimTotal += jaccard2(files[i].dependencyList || [], files[j].dependencyList || []);
1258
+ importPairs++;
1259
+ }
1260
+ }
1261
+ const importCohesion = importPairs > 0 ? importSimTotal / importPairs : 0;
1262
+ fragmentedModules.push({
1263
+ domain,
1264
+ files: files.map((f) => f.file),
1265
+ totalTokens: totalTokens2,
1266
+ fragmentationScore,
1267
+ pathEntropy,
1268
+ directoryDistance,
1269
+ importCohesion,
1270
+ avgCohesion: avgCohesion2,
1271
+ suggestedStructure: {
1272
+ targetFiles,
1273
+ consolidationPlan: [
1274
+ `Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s)`,
1275
+ `Current token cost: ${totalTokens2.toLocaleString()}`,
1276
+ `Estimated savings: ${Math.floor(totalTokens2 * 0.3).toLocaleString()} tokens (30%)`
1277
+ ]
1278
+ }
1279
+ });
1280
+ }
1281
+ fragmentedModules.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
1282
+ const avgCohesion = results.reduce((sum, r) => sum + r.cohesionScore, 0) / totalFiles;
1283
+ 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);
1284
+ const criticalIssues = results.filter((r) => r.severity === "critical").length;
1285
+ const majorIssues = results.filter((r) => r.severity === "major").length;
1286
+ const minorIssues = results.filter((r) => r.severity === "minor").length;
1287
+ const totalPotentialSavings = results.reduce(
1288
+ (sum, r) => sum + r.potentialSavings,
1289
+ 0
1290
+ );
1291
+ const topExpensiveFiles = results.sort((a, b) => b.contextBudget - a.contextBudget).slice(0, 10).map((r) => ({
1292
+ file: r.file,
1293
+ contextBudget: r.contextBudget,
1294
+ severity: r.severity
1295
+ }));
1296
+ return {
1297
+ totalFiles,
1298
+ totalTokens,
1299
+ avgContextBudget,
1300
+ maxContextBudget,
1301
+ avgImportDepth,
1302
+ maxImportDepth,
1303
+ deepFiles,
1304
+ avgFragmentation,
1305
+ fragmentedModules: fragmentedModules.slice(0, 10),
1306
+ avgCohesion,
1307
+ lowCohesionFiles,
1308
+ criticalIssues,
1309
+ majorIssues,
1310
+ minorIssues,
1311
+ totalPotentialSavings,
1312
+ topExpensiveFiles
1313
+ };
1314
+ }
1315
+ function analyzeIssues(params) {
1316
+ const {
1317
+ file,
1318
+ importDepth,
1319
+ contextBudget,
1320
+ cohesionScore,
1321
+ fragmentationScore,
1322
+ maxDepth,
1323
+ maxContextBudget,
1324
+ minCohesion,
1325
+ maxFragmentation,
1326
+ circularDeps
1327
+ } = params;
1328
+ const issues = [];
1329
+ const recommendations = [];
1330
+ let severity = "info";
1331
+ let potentialSavings = 0;
1332
+ if (circularDeps.length > 0) {
1333
+ severity = "critical";
1334
+ issues.push(
1335
+ `Part of ${circularDeps.length} circular dependency chain(s)`
1336
+ );
1337
+ recommendations.push("Break circular dependencies by extracting interfaces or using dependency injection");
1338
+ potentialSavings += contextBudget * 0.2;
1339
+ }
1340
+ if (importDepth > maxDepth * 1.5) {
1341
+ severity = severity === "critical" ? "critical" : "critical";
1342
+ issues.push(`Import depth ${importDepth} exceeds limit by 50%`);
1343
+ recommendations.push("Flatten dependency tree or use facade pattern");
1344
+ potentialSavings += contextBudget * 0.3;
1345
+ } else if (importDepth > maxDepth) {
1346
+ severity = severity === "critical" ? "critical" : "major";
1347
+ issues.push(`Import depth ${importDepth} exceeds recommended maximum ${maxDepth}`);
1348
+ recommendations.push("Consider reducing dependency depth");
1349
+ potentialSavings += contextBudget * 0.15;
1350
+ }
1351
+ if (contextBudget > maxContextBudget * 1.5) {
1352
+ severity = severity === "critical" ? "critical" : "critical";
1353
+ issues.push(`Context budget ${contextBudget.toLocaleString()} tokens is 50% over limit`);
1354
+ recommendations.push("Split into smaller modules or reduce dependency tree");
1355
+ potentialSavings += contextBudget * 0.4;
1356
+ } else if (contextBudget > maxContextBudget) {
1357
+ severity = severity === "critical" || severity === "major" ? severity : "major";
1358
+ issues.push(`Context budget ${contextBudget.toLocaleString()} exceeds ${maxContextBudget.toLocaleString()}`);
1359
+ recommendations.push("Reduce file size or dependencies");
1360
+ potentialSavings += contextBudget * 0.2;
1361
+ }
1362
+ if (cohesionScore < minCohesion * 0.5) {
1363
+ severity = severity === "critical" ? "critical" : "major";
1364
+ issues.push(`Very low cohesion (${(cohesionScore * 100).toFixed(0)}%) - mixed concerns`);
1365
+ recommendations.push("Split file by domain - separate unrelated functionality");
1366
+ potentialSavings += contextBudget * 0.25;
1367
+ } else if (cohesionScore < minCohesion) {
1368
+ severity = severity === "critical" || severity === "major" ? severity : "minor";
1369
+ issues.push(`Low cohesion (${(cohesionScore * 100).toFixed(0)}%)`);
1370
+ recommendations.push("Consider grouping related exports together");
1371
+ potentialSavings += contextBudget * 0.1;
1372
+ }
1373
+ if (fragmentationScore > maxFragmentation) {
1374
+ severity = severity === "critical" || severity === "major" ? severity : "minor";
1375
+ issues.push(`High fragmentation (${(fragmentationScore * 100).toFixed(0)}%) - scattered implementation`);
1376
+ recommendations.push("Consolidate with related files in same domain");
1377
+ potentialSavings += contextBudget * 0.3;
1378
+ }
1379
+ if (issues.length === 0) {
1380
+ issues.push("No significant issues detected");
1381
+ recommendations.push("File is well-structured for AI context usage");
1382
+ }
1383
+ if (isBuildArtifact(file)) {
1384
+ issues.push("Detected build artifact (bundled/output file)");
1385
+ recommendations.push("Exclude build outputs (e.g., cdk.out, dist, build, .next) from analysis");
1386
+ severity = downgradeSeverity(severity);
1387
+ potentialSavings = 0;
1388
+ }
1389
+ return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
1390
+ }
1391
+ function isBuildArtifact(filePath) {
1392
+ const lower = filePath.toLowerCase();
1393
+ 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);
1394
+ }
1395
+ function downgradeSeverity(s) {
1396
+ switch (s) {
1397
+ case "critical":
1398
+ return "minor";
1399
+ case "major":
1400
+ return "minor";
1401
+ case "minor":
1402
+ return "info";
1403
+ default:
1404
+ return "info";
1405
+ }
1406
+ }
1407
+
1408
+ export {
1409
+ buildCoUsageMatrix,
1410
+ buildTypeGraph,
1411
+ findSemanticClusters,
1412
+ calculateDomainConfidence,
1413
+ inferDomainFromSemantics,
1414
+ getCoUsageData,
1415
+ findConsolidationCandidates,
1416
+ classifyFile,
1417
+ adjustFragmentationForClassification,
1418
+ calculateContextScore,
1419
+ getSmartDefaults,
1420
+ analyzeContext,
1421
+ generateSummary
1422
+ };