@aiready/context-analyzer 0.22.9 → 0.22.11

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 (302) hide show
  1. package/.turbo/turbo-build.log +33 -32
  2. package/.turbo/turbo-format-check.log +1 -1
  3. package/.turbo/turbo-lint.log +8 -0
  4. package/dist/chunk-HD4Y3GYL.mjs +124 -0
  5. package/dist/{chunk-M2EGQ36M.mjs → chunk-ISWPFG2C.mjs} +43 -98
  6. package/dist/{chunk-BQCISA2F.mjs → chunk-QW6ULRML.mjs} +32 -6
  7. package/dist/{chunk-WHB7QI7N.mjs → chunk-T733LS62.mjs} +23 -4
  8. package/dist/{cli-action-CXIHOVAC.mjs → cli-action-FV4G4OB6.mjs} +3 -3
  9. package/dist/{cli-action-3CWN7PBE.mjs → cli-action-HYSWE7F4.mjs} +3 -3
  10. package/dist/{cli-action-MLFCIW2O.mjs → cli-action-VIQZSWSP.mjs} +3 -3
  11. package/dist/{cli-action-E7UGP4KE.mjs → cli-action-W7TESWAV.mjs} +6 -8
  12. package/dist/cli.js +236 -191
  13. package/dist/cli.mjs +1 -1
  14. package/dist/console-report-AP4JYNQY.mjs +74 -0
  15. package/dist/html-report-ULELSIYG.mjs +73 -0
  16. package/dist/index.d.mts +69 -337
  17. package/dist/index.d.ts +69 -337
  18. package/dist/index.js +90 -452
  19. package/dist/index.mjs +9 -157
  20. package/dist/interactive-setup-JGFBFI3M.mjs +75 -0
  21. package/dist/{orchestrator-3ERQS3NW.mjs → orchestrator-QNE2E4TE.mjs} +2 -2
  22. package/dist/summary-GQRWW3A2.mjs +7 -0
  23. package/dist/summary-JTBS7CPM.mjs +7 -0
  24. package/dist/summary-TZFB6ZFM.mjs +7 -0
  25. package/package.json +2 -2
  26. package/src/__tests__/file-classification.fixtures.ts +57 -0
  27. package/src/__tests__/file-classification.test.ts +24 -929
  28. package/src/__tests__/provider.test.ts +6 -72
  29. package/src/classifier.ts +42 -42
  30. package/src/cli-action.ts +11 -6
  31. package/src/index.ts +30 -27
  32. package/src/provider.ts +1 -1
  33. package/src/summary.ts +65 -10
  34. package/dist/__tests__/analyzer.test.d.ts +0 -2
  35. package/dist/__tests__/analyzer.test.d.ts.map +0 -1
  36. package/dist/__tests__/analyzer.test.js +0 -198
  37. package/dist/__tests__/analyzer.test.js.map +0 -1
  38. package/dist/__tests__/auto-detection.test.d.ts +0 -2
  39. package/dist/__tests__/auto-detection.test.d.ts.map +0 -1
  40. package/dist/__tests__/auto-detection.test.js +0 -132
  41. package/dist/__tests__/auto-detection.test.js.map +0 -1
  42. package/dist/__tests__/cluster-detector.test.d.ts +0 -2
  43. package/dist/__tests__/cluster-detector.test.d.ts.map +0 -1
  44. package/dist/__tests__/cluster-detector.test.js +0 -121
  45. package/dist/__tests__/cluster-detector.test.js.map +0 -1
  46. package/dist/__tests__/contract.test.d.ts +0 -2
  47. package/dist/__tests__/contract.test.d.ts.map +0 -1
  48. package/dist/__tests__/contract.test.js +0 -59
  49. package/dist/__tests__/contract.test.js.map +0 -1
  50. package/dist/__tests__/enhanced-cohesion.test.d.ts +0 -2
  51. package/dist/__tests__/enhanced-cohesion.test.d.ts.map +0 -1
  52. package/dist/__tests__/enhanced-cohesion.test.js +0 -119
  53. package/dist/__tests__/enhanced-cohesion.test.js.map +0 -1
  54. package/dist/__tests__/file-classification.test.d.ts +0 -2
  55. package/dist/__tests__/file-classification.test.d.ts.map +0 -1
  56. package/dist/__tests__/file-classification.test.js +0 -749
  57. package/dist/__tests__/file-classification.test.js.map +0 -1
  58. package/dist/__tests__/fragmentation-advanced.test.d.ts +0 -2
  59. package/dist/__tests__/fragmentation-advanced.test.d.ts.map +0 -1
  60. package/dist/__tests__/fragmentation-advanced.test.js +0 -44
  61. package/dist/__tests__/fragmentation-advanced.test.js.map +0 -1
  62. package/dist/__tests__/fragmentation-coupling.test.d.ts +0 -2
  63. package/dist/__tests__/fragmentation-coupling.test.d.ts.map +0 -1
  64. package/dist/__tests__/fragmentation-coupling.test.js +0 -52
  65. package/dist/__tests__/fragmentation-coupling.test.js.map +0 -1
  66. package/dist/__tests__/fragmentation-log.test.d.ts +0 -2
  67. package/dist/__tests__/fragmentation-log.test.d.ts.map +0 -1
  68. package/dist/__tests__/fragmentation-log.test.js +0 -29
  69. package/dist/__tests__/fragmentation-log.test.js.map +0 -1
  70. package/dist/__tests__/provider.test.d.ts +0 -2
  71. package/dist/__tests__/provider.test.d.ts.map +0 -1
  72. package/dist/__tests__/provider.test.js +0 -72
  73. package/dist/__tests__/provider.test.js.map +0 -1
  74. package/dist/__tests__/remediation.test.d.ts +0 -2
  75. package/dist/__tests__/remediation.test.d.ts.map +0 -1
  76. package/dist/__tests__/remediation.test.js +0 -61
  77. package/dist/__tests__/remediation.test.js.map +0 -1
  78. package/dist/__tests__/scoring.test.d.ts +0 -2
  79. package/dist/__tests__/scoring.test.d.ts.map +0 -1
  80. package/dist/__tests__/scoring.test.js +0 -298
  81. package/dist/__tests__/scoring.test.js.map +0 -1
  82. package/dist/__tests__/structural-cohesion.test.d.ts +0 -2
  83. package/dist/__tests__/structural-cohesion.test.d.ts.map +0 -1
  84. package/dist/__tests__/structural-cohesion.test.js +0 -35
  85. package/dist/__tests__/structural-cohesion.test.js.map +0 -1
  86. package/dist/analyzer.d.ts +0 -37
  87. package/dist/analyzer.d.ts.map +0 -1
  88. package/dist/analyzer.js +0 -283
  89. package/dist/analyzer.js.map +0 -1
  90. package/dist/analyzers/python-context.d.ts +0 -38
  91. package/dist/analyzers/python-context.d.ts.map +0 -1
  92. package/dist/analyzers/python-context.js +0 -234
  93. package/dist/analyzers/python-context.js.map +0 -1
  94. package/dist/ast-utils.d.ts +0 -16
  95. package/dist/ast-utils.d.ts.map +0 -1
  96. package/dist/ast-utils.js +0 -81
  97. package/dist/ast-utils.js.map +0 -1
  98. package/dist/chunk-22ZO4EKZ.mjs +0 -1297
  99. package/dist/chunk-2HE27YEV.mjs +0 -1739
  100. package/dist/chunk-45P4RDYP.mjs +0 -607
  101. package/dist/chunk-474DEGWW.mjs +0 -1792
  102. package/dist/chunk-4SYIJ7CU.mjs +0 -1538
  103. package/dist/chunk-4U4LDWGF.mjs +0 -360
  104. package/dist/chunk-4XQVYYPC.mjs +0 -1470
  105. package/dist/chunk-5CLU3HYU.mjs +0 -1475
  106. package/dist/chunk-5K73Q3OQ.mjs +0 -1520
  107. package/dist/chunk-5N5DCJOV.mjs +0 -583
  108. package/dist/chunk-6AVS4KTM.mjs +0 -1536
  109. package/dist/chunk-6FQYIG6I.mjs +0 -1298
  110. package/dist/chunk-6I4552YB.mjs +0 -1467
  111. package/dist/chunk-6LPITDKG.mjs +0 -1539
  112. package/dist/chunk-72QC5QUS.mjs +0 -549
  113. package/dist/chunk-736QSHJP.mjs +0 -1807
  114. package/dist/chunk-7LUSCLGR.mjs +0 -2058
  115. package/dist/chunk-7VK3XTSH.mjs +0 -1756
  116. package/dist/chunk-7ZEJGWLN.mjs +0 -1363
  117. package/dist/chunk-AECWO7NQ.mjs +0 -1539
  118. package/dist/chunk-AEK3MZC5.mjs +0 -709
  119. package/dist/chunk-AJC3FR6G.mjs +0 -1509
  120. package/dist/chunk-AMPK6SWS.mjs +0 -1754
  121. package/dist/chunk-BA7QGUHN.mjs +0 -1722
  122. package/dist/chunk-BCEZGRXI.mjs +0 -1297
  123. package/dist/chunk-BD4NWUVG.mjs +0 -1242
  124. package/dist/chunk-BEZPBI5C.mjs +0 -1829
  125. package/dist/chunk-BHCRDEE4.mjs +0 -1745
  126. package/dist/chunk-BW463GQB.mjs +0 -1767
  127. package/dist/chunk-CAX2MOUZ.mjs +0 -1801
  128. package/dist/chunk-CBWM3EK5.mjs +0 -1854
  129. package/dist/chunk-CCBNKQYB.mjs +0 -1812
  130. package/dist/chunk-CDIVYADN.mjs +0 -2110
  131. package/dist/chunk-CVGIDSMN.mjs +0 -1522
  132. package/dist/chunk-D25B5LZR.mjs +0 -1739
  133. package/dist/chunk-D3SIHB2V.mjs +0 -2118
  134. package/dist/chunk-DD7UVNE3.mjs +0 -678
  135. package/dist/chunk-DMRZMS2U.mjs +0 -964
  136. package/dist/chunk-DXG5NIYL.mjs +0 -1527
  137. package/dist/chunk-EBXG2Q5Y.mjs +0 -2059
  138. package/dist/chunk-EH3PMNZQ.mjs +0 -569
  139. package/dist/chunk-EMYD7NS6.mjs +0 -137
  140. package/dist/chunk-EVX2W2BK.mjs +0 -1896
  141. package/dist/chunk-EWFR366Y.mjs +0 -1740
  142. package/dist/chunk-EX7HCWAO.mjs +0 -625
  143. package/dist/chunk-FNPSK3CG.mjs +0 -1760
  144. package/dist/chunk-FO6YT6RG.mjs +0 -1751
  145. package/dist/chunk-FYI56A5M.mjs +0 -1892
  146. package/dist/chunk-G3CCJCBI.mjs +0 -1521
  147. package/dist/chunk-G7PO3DNK.mjs +0 -1072
  148. package/dist/chunk-GFADGYXZ.mjs +0 -1752
  149. package/dist/chunk-GTRIBVS6.mjs +0 -1467
  150. package/dist/chunk-GXTGOLZT.mjs +0 -92
  151. package/dist/chunk-H4HWBQU6.mjs +0 -1530
  152. package/dist/chunk-HDFXSEFW.mjs +0 -605
  153. package/dist/chunk-HOUDVRG2.mjs +0 -1422
  154. package/dist/chunk-HQNHM2X7.mjs +0 -997
  155. package/dist/chunk-I54HL4FZ.mjs +0 -781
  156. package/dist/chunk-I77HFFZU.mjs +0 -1876
  157. package/dist/chunk-IKRP7ECY.mjs +0 -1754
  158. package/dist/chunk-ILMLGJGI.mjs +0 -1295
  159. package/dist/chunk-IPIE5TXJ.mjs +0 -1741
  160. package/dist/chunk-IRWCPDWD.mjs +0 -779
  161. package/dist/chunk-J3MUOWHC.mjs +0 -1747
  162. package/dist/chunk-J5TA3AZU.mjs +0 -1795
  163. package/dist/chunk-JH535NPP.mjs +0 -1619
  164. package/dist/chunk-JUHHOSHG.mjs +0 -1808
  165. package/dist/chunk-JZ2SE4DB.mjs +0 -1116
  166. package/dist/chunk-K2WFOBAZ.mjs +0 -1821
  167. package/dist/chunk-K6U64EL3.mjs +0 -517
  168. package/dist/chunk-KDUUZQBK.mjs +0 -1692
  169. package/dist/chunk-KGFWKSGJ.mjs +0 -1442
  170. package/dist/chunk-KGVMS4R5.mjs +0 -1750
  171. package/dist/chunk-KWIS5FQP.mjs +0 -1739
  172. package/dist/chunk-KYSZF5N6.mjs +0 -1876
  173. package/dist/chunk-LERPI33Y.mjs +0 -2060
  174. package/dist/chunk-M64RHH4D.mjs +0 -1896
  175. package/dist/chunk-MBE4AQP5.mjs +0 -1362
  176. package/dist/chunk-MR7WXHIE.mjs +0 -1833
  177. package/dist/chunk-MZP3G7TF.mjs +0 -2118
  178. package/dist/chunk-N2GQWNFG.mjs +0 -1527
  179. package/dist/chunk-N6XBOOVA.mjs +0 -564
  180. package/dist/chunk-NJUW6VED.mjs +0 -610
  181. package/dist/chunk-NOHK5DLU.mjs +0 -2173
  182. package/dist/chunk-NQA3F2HJ.mjs +0 -1532
  183. package/dist/chunk-NXXQ2U73.mjs +0 -1467
  184. package/dist/chunk-OP4G6GLN.mjs +0 -1876
  185. package/dist/chunk-ORLC5Y4J.mjs +0 -1787
  186. package/dist/chunk-OTCQL7DY.mjs +0 -2045
  187. package/dist/chunk-OUYSZZ7X.mjs +0 -1846
  188. package/dist/chunk-OZE3FVZT.mjs +0 -1089
  189. package/dist/chunk-P3T3H27S.mjs +0 -1895
  190. package/dist/chunk-P5YV5WIX.mjs +0 -1803
  191. package/dist/chunk-P74BO725.mjs +0 -1296
  192. package/dist/chunk-PDN74MG3.mjs +0 -1834
  193. package/dist/chunk-PJD4VCIH.mjs +0 -1722
  194. package/dist/chunk-PVVCCE6W.mjs +0 -755
  195. package/dist/chunk-PVVMK56C.mjs +0 -1793
  196. package/dist/chunk-Q2GDZ2FZ.mjs +0 -1794
  197. package/dist/chunk-QDGPR3L6.mjs +0 -1518
  198. package/dist/chunk-RQCIJO5Z.mjs +0 -1116
  199. package/dist/chunk-RRB2C34Q.mjs +0 -1738
  200. package/dist/chunk-RYIB5CWD.mjs +0 -781
  201. package/dist/chunk-S6OPP4L5.mjs +0 -1791
  202. package/dist/chunk-SAVOSPM3.mjs +0 -1522
  203. package/dist/chunk-SFK6XTJE.mjs +0 -2110
  204. package/dist/chunk-SIX4KMF2.mjs +0 -1468
  205. package/dist/chunk-SPAM2YJE.mjs +0 -1537
  206. package/dist/chunk-T6ZCOPPI.mjs +0 -538
  207. package/dist/chunk-TPF75CNP.mjs +0 -581
  208. package/dist/chunk-TWWPY7FD.mjs +0 -1754
  209. package/dist/chunk-U5R2FTCR.mjs +0 -1803
  210. package/dist/chunk-UG7OPVHB.mjs +0 -1521
  211. package/dist/chunk-UMZTAWDA.mjs +0 -1812
  212. package/dist/chunk-UU4HZ7ZT.mjs +0 -1849
  213. package/dist/chunk-UXC6QUZ7.mjs +0 -1801
  214. package/dist/chunk-VBWXHKGD.mjs +0 -1895
  215. package/dist/chunk-VIJTZPBI.mjs +0 -1470
  216. package/dist/chunk-VLV6MXPL.mjs +0 -1750
  217. package/dist/chunk-VTALAPQZ.mjs +0 -1241
  218. package/dist/chunk-W2KNBN6W.mjs +0 -1849
  219. package/dist/chunk-W37E7MW5.mjs +0 -1403
  220. package/dist/chunk-W76FEISE.mjs +0 -1538
  221. package/dist/chunk-WCFQYXQA.mjs +0 -1532
  222. package/dist/chunk-WKOZOHOU.mjs +0 -2060
  223. package/dist/chunk-WTQJNY4U.mjs +0 -1785
  224. package/dist/chunk-XBFM2Z4O.mjs +0 -1792
  225. package/dist/chunk-XIXAWCMS.mjs +0 -1760
  226. package/dist/chunk-XTAXUNQN.mjs +0 -1742
  227. package/dist/chunk-XY77XABG.mjs +0 -1545
  228. package/dist/chunk-XZ645X5U.mjs +0 -1425
  229. package/dist/chunk-Y6FXYEAI.mjs +0 -10
  230. package/dist/chunk-YCGDIGOG.mjs +0 -1467
  231. package/dist/chunk-YYB6NZE3.mjs +0 -1869
  232. package/dist/chunk-Z5WY6A4P.mjs +0 -1754
  233. package/dist/chunk-ZAMFWKIB.mjs +0 -1842
  234. package/dist/classifier.d.ts +0 -114
  235. package/dist/classifier.d.ts.map +0 -1
  236. package/dist/classifier.js +0 -439
  237. package/dist/classifier.js.map +0 -1
  238. package/dist/cli-action-SA7SCYNV.mjs +0 -95
  239. package/dist/cli-action-VCXBZGZP.mjs +0 -95
  240. package/dist/cli-action-YAJOJCXJ.mjs +0 -95
  241. package/dist/cli.d.ts.map +0 -1
  242. package/dist/cli.js.map +0 -1
  243. package/dist/cluster-detector.d.ts +0 -8
  244. package/dist/cluster-detector.d.ts.map +0 -1
  245. package/dist/cluster-detector.js +0 -70
  246. package/dist/cluster-detector.js.map +0 -1
  247. package/dist/defaults.d.ts +0 -7
  248. package/dist/defaults.d.ts.map +0 -1
  249. package/dist/defaults.js +0 -54
  250. package/dist/defaults.js.map +0 -1
  251. package/dist/graph-builder.d.ts +0 -33
  252. package/dist/graph-builder.d.ts.map +0 -1
  253. package/dist/graph-builder.js +0 -225
  254. package/dist/graph-builder.js.map +0 -1
  255. package/dist/index.d.ts.map +0 -1
  256. package/dist/index.js.map +0 -1
  257. package/dist/metrics.d.ts +0 -34
  258. package/dist/metrics.d.ts.map +0 -1
  259. package/dist/metrics.js +0 -170
  260. package/dist/metrics.js.map +0 -1
  261. package/dist/orchestrator-3L3NAZYP.mjs +0 -10
  262. package/dist/orchestrator-5AL3XBPZ.mjs +0 -10
  263. package/dist/orchestrator-KMAKMHTD.mjs +0 -10
  264. package/dist/orchestrator-MONOZHVW.mjs +0 -10
  265. package/dist/orchestrator-R6MZT4Z2.mjs +0 -10
  266. package/dist/orchestrator-ZR7JSKWI.mjs +0 -10
  267. package/dist/provider.d.ts +0 -6
  268. package/dist/provider.d.ts.map +0 -1
  269. package/dist/provider.js +0 -48
  270. package/dist/provider.js.map +0 -1
  271. package/dist/python-context-3GZKN3LR.mjs +0 -162
  272. package/dist/python-context-GOH747QU.mjs +0 -202
  273. package/dist/python-context-O2EN3M6Z.mjs +0 -162
  274. package/dist/python-context-PAETRLDY.mjs +0 -185
  275. package/dist/python-context-TBI5FVFY.mjs +0 -203
  276. package/dist/python-context-UOPTQH44.mjs +0 -192
  277. package/dist/remediation.d.ts +0 -25
  278. package/dist/remediation.d.ts.map +0 -1
  279. package/dist/remediation.js +0 -98
  280. package/dist/remediation.js.map +0 -1
  281. package/dist/scoring.d.ts +0 -9
  282. package/dist/scoring.d.ts.map +0 -1
  283. package/dist/scoring.js +0 -142
  284. package/dist/scoring.js.map +0 -1
  285. package/dist/semantic-analysis.d.ts +0 -33
  286. package/dist/semantic-analysis.d.ts.map +0 -1
  287. package/dist/semantic-analysis.js +0 -303
  288. package/dist/semantic-analysis.js.map +0 -1
  289. package/dist/summary-7PZVW72O.mjs +0 -7
  290. package/dist/summary-LKUCJAIS.mjs +0 -7
  291. package/dist/summary.d.ts +0 -6
  292. package/dist/summary.d.ts.map +0 -1
  293. package/dist/summary.js +0 -92
  294. package/dist/summary.js.map +0 -1
  295. package/dist/types.d.ts +0 -124
  296. package/dist/types.d.ts.map +0 -1
  297. package/dist/types.js +0 -2
  298. package/dist/types.js.map +0 -1
  299. package/dist/utils/output-formatter.d.ts +0 -14
  300. package/dist/utils/output-formatter.d.ts.map +0 -1
  301. package/dist/utils/output-formatter.js +0 -338
  302. package/dist/utils/output-formatter.js.map +0 -1
@@ -1,1242 +0,0 @@
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
-
724
- // src/scoring.ts
725
- function calculateContextScore(summary) {
726
- const {
727
- avgContextBudget,
728
- maxContextBudget,
729
- avgImportDepth,
730
- maxImportDepth,
731
- avgFragmentation,
732
- criticalIssues,
733
- majorIssues
734
- } = summary;
735
- const budgetScore = avgContextBudget < 5e3 ? 100 : Math.max(0, 100 - (avgContextBudget - 5e3) / 150);
736
- const depthScore = avgImportDepth < 5 ? 100 : Math.max(0, 100 - (avgImportDepth - 5) * 10);
737
- const fragmentationScore = avgFragmentation < 0.3 ? 100 : Math.max(0, 100 - (avgFragmentation - 0.3) * 200);
738
- const criticalPenalty = criticalIssues * 10;
739
- const majorPenalty = majorIssues * 3;
740
- const maxBudgetPenalty = maxContextBudget > 15e3 ? Math.min(20, (maxContextBudget - 15e3) / 500) : 0;
741
- const rawScore = budgetScore * 0.4 + depthScore * 0.3 + fragmentationScore * 0.3;
742
- const finalScore = rawScore - criticalPenalty - majorPenalty - maxBudgetPenalty;
743
- const score = Math.max(0, Math.min(100, Math.round(finalScore)));
744
- const factors = [
745
- {
746
- name: "Context Budget",
747
- impact: Math.round(budgetScore * 0.4 - 40),
748
- description: `Avg ${Math.round(avgContextBudget)} tokens per file ${avgContextBudget < 5e3 ? "(excellent)" : avgContextBudget < 1e4 ? "(acceptable)" : "(high)"}`
749
- },
750
- {
751
- name: "Import Depth",
752
- impact: Math.round(depthScore * 0.3 - 30),
753
- description: `Avg ${avgImportDepth.toFixed(1)} levels ${avgImportDepth < 5 ? "(excellent)" : avgImportDepth < 8 ? "(acceptable)" : "(deep)"}`
754
- },
755
- {
756
- name: "Fragmentation",
757
- impact: Math.round(fragmentationScore * 0.3 - 30),
758
- description: `${(avgFragmentation * 100).toFixed(0)}% fragmentation ${avgFragmentation < 0.3 ? "(well-organized)" : avgFragmentation < 0.5 ? "(moderate)" : "(high)"}`
759
- }
760
- ];
761
- if (criticalIssues > 0) {
762
- factors.push({
763
- name: "Critical Issues",
764
- impact: -criticalPenalty,
765
- description: `${criticalIssues} critical context issue${criticalIssues > 1 ? "s" : ""}`
766
- });
767
- }
768
- if (majorIssues > 0) {
769
- factors.push({
770
- name: "Major Issues",
771
- impact: -majorPenalty,
772
- description: `${majorIssues} major context issue${majorIssues > 1 ? "s" : ""}`
773
- });
774
- }
775
- if (maxBudgetPenalty > 0) {
776
- factors.push({
777
- name: "Extreme File Detected",
778
- impact: -Math.round(maxBudgetPenalty),
779
- description: `One file requires ${Math.round(maxContextBudget)} tokens (very high)`
780
- });
781
- }
782
- const recommendations = [];
783
- if (avgContextBudget > 1e4) {
784
- const estimatedImpact = Math.min(15, Math.round((avgContextBudget - 1e4) / 1e3));
785
- recommendations.push({
786
- action: "Reduce file dependencies to lower context requirements",
787
- estimatedImpact,
788
- priority: "high"
789
- });
790
- }
791
- if (avgImportDepth > 8) {
792
- const estimatedImpact = Math.min(10, Math.round((avgImportDepth - 8) * 2));
793
- recommendations.push({
794
- action: "Flatten import chains to reduce depth",
795
- estimatedImpact,
796
- priority: avgImportDepth > 10 ? "high" : "medium"
797
- });
798
- }
799
- if (avgFragmentation > 0.5) {
800
- const estimatedImpact = Math.min(12, Math.round((avgFragmentation - 0.5) * 40));
801
- recommendations.push({
802
- action: "Consolidate related code into cohesive modules",
803
- estimatedImpact,
804
- priority: "medium"
805
- });
806
- }
807
- if (maxContextBudget > 2e4) {
808
- recommendations.push({
809
- action: `Split large file (${Math.round(maxContextBudget)} tokens) into smaller modules`,
810
- estimatedImpact: 8,
811
- priority: "high"
812
- });
813
- }
814
- return {
815
- toolName: "context-analyzer",
816
- score,
817
- rawMetrics: {
818
- avgContextBudget: Math.round(avgContextBudget),
819
- maxContextBudget: Math.round(maxContextBudget),
820
- avgImportDepth: Math.round(avgImportDepth * 10) / 10,
821
- maxImportDepth,
822
- avgFragmentation: Math.round(avgFragmentation * 100) / 100,
823
- criticalIssues,
824
- majorIssues
825
- },
826
- factors,
827
- recommendations
828
- };
829
- }
830
-
831
- // src/index.ts
832
- async function getSmartDefaults(directory, userOptions) {
833
- const files = await scanFiles({
834
- rootDir: directory,
835
- include: userOptions.include,
836
- exclude: userOptions.exclude
837
- });
838
- const estimatedBlocks = files.length;
839
- let maxDepth;
840
- let maxContextBudget;
841
- let minCohesion;
842
- let maxFragmentation;
843
- if (estimatedBlocks < 100) {
844
- maxDepth = 4;
845
- maxContextBudget = 8e3;
846
- minCohesion = 0.5;
847
- maxFragmentation = 0.5;
848
- } else if (estimatedBlocks < 500) {
849
- maxDepth = 5;
850
- maxContextBudget = 15e3;
851
- minCohesion = 0.45;
852
- maxFragmentation = 0.6;
853
- } else if (estimatedBlocks < 2e3) {
854
- maxDepth = 7;
855
- maxContextBudget = 25e3;
856
- minCohesion = 0.4;
857
- maxFragmentation = 0.7;
858
- } else {
859
- maxDepth = 10;
860
- maxContextBudget = 4e4;
861
- minCohesion = 0.35;
862
- maxFragmentation = 0.8;
863
- }
864
- return {
865
- maxDepth,
866
- maxContextBudget,
867
- minCohesion,
868
- maxFragmentation,
869
- focus: "all",
870
- includeNodeModules: false,
871
- rootDir: userOptions.rootDir || directory,
872
- include: userOptions.include,
873
- exclude: userOptions.exclude
874
- };
875
- }
876
- async function analyzeContext(options) {
877
- const {
878
- maxDepth = 5,
879
- maxContextBudget = 1e4,
880
- minCohesion = 0.6,
881
- maxFragmentation = 0.5,
882
- focus = "all",
883
- includeNodeModules = false,
884
- ...scanOptions
885
- } = options;
886
- const files = await scanFiles({
887
- ...scanOptions,
888
- // Only add node_modules to exclude if includeNodeModules is false
889
- // The DEFAULT_EXCLUDE already includes node_modules, so this is only needed
890
- // if user overrides the default exclude list
891
- exclude: includeNodeModules && scanOptions.exclude ? scanOptions.exclude.filter((pattern) => pattern !== "**/node_modules/**") : scanOptions.exclude
892
- });
893
- const pythonFiles = files.filter((f) => f.toLowerCase().endsWith(".py"));
894
- const tsJsFiles = files.filter((f) => !f.toLowerCase().endsWith(".py"));
895
- const fileContents = await Promise.all(
896
- files.map(async (file) => ({
897
- file,
898
- content: await readFileContent(file)
899
- }))
900
- );
901
- const graph = buildDependencyGraph(fileContents.filter((f) => !f.file.toLowerCase().endsWith(".py")));
902
- let pythonResults = [];
903
- if (pythonFiles.length > 0) {
904
- const { analyzePythonContext } = await import("./python-context-UOPTQH44.mjs");
905
- const pythonMetrics = await analyzePythonContext(pythonFiles, scanOptions.rootDir || options.rootDir || ".");
906
- pythonResults = pythonMetrics.map((metric) => {
907
- const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
908
- file: metric.file,
909
- importDepth: metric.importDepth,
910
- contextBudget: metric.contextBudget,
911
- cohesionScore: metric.cohesion,
912
- fragmentationScore: 0,
913
- // Python analyzer doesn't calculate fragmentation yet
914
- maxDepth,
915
- maxContextBudget,
916
- minCohesion,
917
- maxFragmentation,
918
- circularDeps: metric.metrics.circularDependencies.map((cycle) => cycle.split(" \u2192 "))
919
- });
920
- return {
921
- file: metric.file,
922
- tokenCost: Math.floor(metric.contextBudget / (1 + metric.imports.length || 1)),
923
- // Estimate
924
- linesOfCode: metric.metrics.linesOfCode,
925
- importDepth: metric.importDepth,
926
- dependencyCount: metric.imports.length,
927
- dependencyList: metric.imports.map((imp) => imp.resolvedPath || imp.source),
928
- circularDeps: metric.metrics.circularDependencies.map((cycle) => cycle.split(" \u2192 ")),
929
- cohesionScore: metric.cohesion,
930
- domains: ["python"],
931
- // Generic for now
932
- exportCount: metric.exports.length,
933
- contextBudget: metric.contextBudget,
934
- fragmentationScore: 0,
935
- relatedFiles: [],
936
- severity,
937
- issues,
938
- recommendations,
939
- potentialSavings
940
- };
941
- });
942
- }
943
- const circularDeps = detectCircularDependencies(graph);
944
- const useLogScale = files.length >= 500;
945
- const clusters = detectModuleClusters(graph, { useLogScale });
946
- const fragmentationMap = /* @__PURE__ */ new Map();
947
- for (const cluster of clusters) {
948
- for (const file of cluster.files) {
949
- fragmentationMap.set(file, cluster.fragmentationScore);
950
- }
951
- }
952
- const results = [];
953
- for (const { file } of fileContents) {
954
- const node = graph.nodes.get(file);
955
- if (!node) continue;
956
- const importDepth = focus === "depth" || focus === "all" ? calculateImportDepth(file, graph) : 0;
957
- const dependencyList = focus === "depth" || focus === "all" ? getTransitiveDependencies(file, graph) : [];
958
- const contextBudget = focus === "all" ? calculateContextBudget(file, graph) : node.tokenCost;
959
- const cohesionScore = focus === "cohesion" || focus === "all" ? calculateCohesion(node.exports, file) : 1;
960
- const fragmentationScore = fragmentationMap.get(file) || 0;
961
- const relatedFiles = [];
962
- for (const cluster of clusters) {
963
- if (cluster.files.includes(file)) {
964
- relatedFiles.push(...cluster.files.filter((f) => f !== file));
965
- break;
966
- }
967
- }
968
- const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
969
- file,
970
- importDepth,
971
- contextBudget,
972
- cohesionScore,
973
- fragmentationScore,
974
- maxDepth,
975
- maxContextBudget,
976
- minCohesion,
977
- maxFragmentation,
978
- circularDeps
979
- });
980
- const domains = [
981
- ...new Set(node.exports.map((e) => e.inferredDomain || "unknown"))
982
- ];
983
- results.push({
984
- file,
985
- tokenCost: node.tokenCost,
986
- linesOfCode: node.linesOfCode,
987
- importDepth,
988
- dependencyCount: dependencyList.length,
989
- dependencyList,
990
- circularDeps: circularDeps.filter((cycle) => cycle.includes(file)),
991
- cohesionScore,
992
- domains,
993
- exportCount: node.exports.length,
994
- contextBudget,
995
- fragmentationScore,
996
- relatedFiles,
997
- severity,
998
- issues,
999
- recommendations,
1000
- potentialSavings
1001
- });
1002
- }
1003
- const allResults = [...results, ...pythonResults];
1004
- const issuesOnly = allResults.filter((r) => r.severity !== "info");
1005
- const sorted = issuesOnly.sort((a, b) => {
1006
- const severityOrder = { critical: 0, major: 1, minor: 2, info: 3 };
1007
- const severityDiff = severityOrder[a.severity] - severityOrder[b.severity];
1008
- if (severityDiff !== 0) return severityDiff;
1009
- return b.contextBudget - a.contextBudget;
1010
- });
1011
- return sorted.length > 0 ? sorted : results;
1012
- }
1013
- function generateSummary(results) {
1014
- if (results.length === 0) {
1015
- return {
1016
- totalFiles: 0,
1017
- totalTokens: 0,
1018
- avgContextBudget: 0,
1019
- maxContextBudget: 0,
1020
- avgImportDepth: 0,
1021
- maxImportDepth: 0,
1022
- deepFiles: [],
1023
- avgFragmentation: 0,
1024
- fragmentedModules: [],
1025
- avgCohesion: 0,
1026
- lowCohesionFiles: [],
1027
- criticalIssues: 0,
1028
- majorIssues: 0,
1029
- minorIssues: 0,
1030
- totalPotentialSavings: 0,
1031
- topExpensiveFiles: []
1032
- };
1033
- }
1034
- const totalFiles = results.length;
1035
- const totalTokens = results.reduce((sum, r) => sum + r.tokenCost, 0);
1036
- const totalContextBudget = results.reduce(
1037
- (sum, r) => sum + r.contextBudget,
1038
- 0
1039
- );
1040
- const avgContextBudget = totalContextBudget / totalFiles;
1041
- const maxContextBudget = Math.max(...results.map((r) => r.contextBudget));
1042
- const avgImportDepth = results.reduce((sum, r) => sum + r.importDepth, 0) / totalFiles;
1043
- const maxImportDepth = Math.max(...results.map((r) => r.importDepth));
1044
- 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);
1045
- const avgFragmentation = results.reduce((sum, r) => sum + r.fragmentationScore, 0) / totalFiles;
1046
- const moduleMap = /* @__PURE__ */ new Map();
1047
- for (const result of results) {
1048
- for (const domain of result.domains) {
1049
- if (!moduleMap.has(domain)) {
1050
- moduleMap.set(domain, []);
1051
- }
1052
- moduleMap.get(domain).push(result);
1053
- }
1054
- }
1055
- const fragmentedModules = [];
1056
- for (const [domain, files] of moduleMap.entries()) {
1057
- let jaccard2 = function(a, b) {
1058
- const s1 = new Set(a || []);
1059
- const s2 = new Set(b || []);
1060
- if (s1.size === 0 && s2.size === 0) return 0;
1061
- const inter = new Set([...s1].filter((x) => s2.has(x)));
1062
- const uni = /* @__PURE__ */ new Set([...s1, ...s2]);
1063
- return uni.size === 0 ? 0 : inter.size / uni.size;
1064
- };
1065
- var jaccard = jaccard2;
1066
- if (files.length < 2) continue;
1067
- const fragmentationScore = files.reduce((sum, f) => sum + f.fragmentationScore, 0) / files.length;
1068
- if (fragmentationScore < 0.3) continue;
1069
- const totalTokens2 = files.reduce((sum, f) => sum + f.tokenCost, 0);
1070
- const avgCohesion2 = files.reduce((sum, f) => sum + f.cohesionScore, 0) / files.length;
1071
- const targetFiles = Math.max(1, Math.ceil(files.length / 3));
1072
- const filePaths = files.map((f) => f.file);
1073
- const pathEntropy = calculatePathEntropy(filePaths);
1074
- const directoryDistance = calculateDirectoryDistance(filePaths);
1075
- let importSimTotal = 0;
1076
- let importPairs = 0;
1077
- for (let i = 0; i < files.length; i++) {
1078
- for (let j = i + 1; j < files.length; j++) {
1079
- importSimTotal += jaccard2(files[i].dependencyList || [], files[j].dependencyList || []);
1080
- importPairs++;
1081
- }
1082
- }
1083
- const importCohesion = importPairs > 0 ? importSimTotal / importPairs : 0;
1084
- fragmentedModules.push({
1085
- domain,
1086
- files: files.map((f) => f.file),
1087
- totalTokens: totalTokens2,
1088
- fragmentationScore,
1089
- pathEntropy,
1090
- directoryDistance,
1091
- importCohesion,
1092
- avgCohesion: avgCohesion2,
1093
- suggestedStructure: {
1094
- targetFiles,
1095
- consolidationPlan: [
1096
- `Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s)`,
1097
- `Current token cost: ${totalTokens2.toLocaleString()}`,
1098
- `Estimated savings: ${Math.floor(totalTokens2 * 0.3).toLocaleString()} tokens (30%)`
1099
- ]
1100
- }
1101
- });
1102
- }
1103
- fragmentedModules.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
1104
- const avgCohesion = results.reduce((sum, r) => sum + r.cohesionScore, 0) / totalFiles;
1105
- 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);
1106
- const criticalIssues = results.filter((r) => r.severity === "critical").length;
1107
- const majorIssues = results.filter((r) => r.severity === "major").length;
1108
- const minorIssues = results.filter((r) => r.severity === "minor").length;
1109
- const totalPotentialSavings = results.reduce(
1110
- (sum, r) => sum + r.potentialSavings,
1111
- 0
1112
- );
1113
- const topExpensiveFiles = results.sort((a, b) => b.contextBudget - a.contextBudget).slice(0, 10).map((r) => ({
1114
- file: r.file,
1115
- contextBudget: r.contextBudget,
1116
- severity: r.severity
1117
- }));
1118
- return {
1119
- totalFiles,
1120
- totalTokens,
1121
- avgContextBudget,
1122
- maxContextBudget,
1123
- avgImportDepth,
1124
- maxImportDepth,
1125
- deepFiles,
1126
- avgFragmentation,
1127
- fragmentedModules: fragmentedModules.slice(0, 10),
1128
- avgCohesion,
1129
- lowCohesionFiles,
1130
- criticalIssues,
1131
- majorIssues,
1132
- minorIssues,
1133
- totalPotentialSavings,
1134
- topExpensiveFiles
1135
- };
1136
- }
1137
- function analyzeIssues(params) {
1138
- const {
1139
- file,
1140
- importDepth,
1141
- contextBudget,
1142
- cohesionScore,
1143
- fragmentationScore,
1144
- maxDepth,
1145
- maxContextBudget,
1146
- minCohesion,
1147
- maxFragmentation,
1148
- circularDeps
1149
- } = params;
1150
- const issues = [];
1151
- const recommendations = [];
1152
- let severity = "info";
1153
- let potentialSavings = 0;
1154
- if (circularDeps.length > 0) {
1155
- severity = "critical";
1156
- issues.push(
1157
- `Part of ${circularDeps.length} circular dependency chain(s)`
1158
- );
1159
- recommendations.push("Break circular dependencies by extracting interfaces or using dependency injection");
1160
- potentialSavings += contextBudget * 0.2;
1161
- }
1162
- if (importDepth > maxDepth * 1.5) {
1163
- severity = severity === "critical" ? "critical" : "critical";
1164
- issues.push(`Import depth ${importDepth} exceeds limit by 50%`);
1165
- recommendations.push("Flatten dependency tree or use facade pattern");
1166
- potentialSavings += contextBudget * 0.3;
1167
- } else if (importDepth > maxDepth) {
1168
- severity = severity === "critical" ? "critical" : "major";
1169
- issues.push(`Import depth ${importDepth} exceeds recommended maximum ${maxDepth}`);
1170
- recommendations.push("Consider reducing dependency depth");
1171
- potentialSavings += contextBudget * 0.15;
1172
- }
1173
- if (contextBudget > maxContextBudget * 1.5) {
1174
- severity = severity === "critical" ? "critical" : "critical";
1175
- issues.push(`Context budget ${contextBudget.toLocaleString()} tokens is 50% over limit`);
1176
- recommendations.push("Split into smaller modules or reduce dependency tree");
1177
- potentialSavings += contextBudget * 0.4;
1178
- } else if (contextBudget > maxContextBudget) {
1179
- severity = severity === "critical" || severity === "major" ? severity : "major";
1180
- issues.push(`Context budget ${contextBudget.toLocaleString()} exceeds ${maxContextBudget.toLocaleString()}`);
1181
- recommendations.push("Reduce file size or dependencies");
1182
- potentialSavings += contextBudget * 0.2;
1183
- }
1184
- if (cohesionScore < minCohesion * 0.5) {
1185
- severity = severity === "critical" ? "critical" : "major";
1186
- issues.push(`Very low cohesion (${(cohesionScore * 100).toFixed(0)}%) - mixed concerns`);
1187
- recommendations.push("Split file by domain - separate unrelated functionality");
1188
- potentialSavings += contextBudget * 0.25;
1189
- } else if (cohesionScore < minCohesion) {
1190
- severity = severity === "critical" || severity === "major" ? severity : "minor";
1191
- issues.push(`Low cohesion (${(cohesionScore * 100).toFixed(0)}%)`);
1192
- recommendations.push("Consider grouping related exports together");
1193
- potentialSavings += contextBudget * 0.1;
1194
- }
1195
- if (fragmentationScore > maxFragmentation) {
1196
- severity = severity === "critical" || severity === "major" ? severity : "minor";
1197
- issues.push(`High fragmentation (${(fragmentationScore * 100).toFixed(0)}%) - scattered implementation`);
1198
- recommendations.push("Consolidate with related files in same domain");
1199
- potentialSavings += contextBudget * 0.3;
1200
- }
1201
- if (issues.length === 0) {
1202
- issues.push("No significant issues detected");
1203
- recommendations.push("File is well-structured for AI context usage");
1204
- }
1205
- if (isBuildArtifact(file)) {
1206
- issues.push("Detected build artifact (bundled/output file)");
1207
- recommendations.push("Exclude build outputs (e.g., cdk.out, dist, build, .next) from analysis");
1208
- severity = downgradeSeverity(severity);
1209
- potentialSavings = 0;
1210
- }
1211
- return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
1212
- }
1213
- function isBuildArtifact(filePath) {
1214
- const lower = filePath.toLowerCase();
1215
- 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);
1216
- }
1217
- function downgradeSeverity(s) {
1218
- switch (s) {
1219
- case "critical":
1220
- return "minor";
1221
- case "major":
1222
- return "minor";
1223
- case "minor":
1224
- return "info";
1225
- default:
1226
- return "info";
1227
- }
1228
- }
1229
-
1230
- export {
1231
- buildCoUsageMatrix,
1232
- buildTypeGraph,
1233
- findSemanticClusters,
1234
- calculateDomainConfidence,
1235
- inferDomainFromSemantics,
1236
- getCoUsageData,
1237
- findConsolidationCandidates,
1238
- calculateContextScore,
1239
- getSmartDefaults,
1240
- analyzeContext,
1241
- generateSummary
1242
- };