@aiready/context-analyzer 0.19.18 → 0.19.21

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