debtmap 0.10.0

Code complexity and technical debt analyzer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
# DebtMap Architecture

## Overview

DebtMap is a high-performance technical debt analyzer focused exclusively on Rust code analysis. The architecture is designed for deep Rust language integration, optimal performance, and comprehensive static analysis capabilities.

## Core Components

### 1. Language Analyzers
- **FileAnalyzer**: Trait-based abstraction for language-specific analysis
- **RustAnalyzer**: Rust-specific implementation using syn for native AST parsing and comprehensive Rust language support
- **Focus**: Rust-only analysis with deep language integration for maximum accuracy

### 2. Unified Analysis Engine
- **UnifiedAnalysis**: Coordinates all analysis phases
- **ParallelUnifiedAnalysis**: High-performance parallel implementation
- **DebtAggregator**: Aggregates metrics across functions and files

### 3. Metrics Collection
- **Cyclomatic Complexity**: Control flow complexity measurement
- **Cognitive Complexity**: Human readability assessment
- **Function Metrics**: Lines of code, parameters, nesting depth
- **File Metrics**: Module-level aggregation
- **Test Coverage**: Integration with lcov data via indexed lookups

## Effect System (Spec 207)

### Philosophy: Pure Core, Imperative Shell

DebtMap uses the [Stillwater](https://crates.io/crates/stillwater) effects library to implement a **pure core, imperative shell** architecture. This pattern strictly separates:

- **Pure Core**: Business logic functions that never perform I/O
- **Imperative Shell**: I/O operations wrapped in effects at system boundaries
- **Effect Composition**: Type-safe chains of operations with compile-time guarantees

This enables:
- **Testability**: Pure functions need no mocks or test infrastructure
- **Reliability**: Deterministic computations with explicit side effects
- **Composability**: Build complex pipelines from simple, reusable effects
- **Maintainability**: Clear boundaries between logic and I/O

### Core Types

#### Effect Types

```rust
// Generic effect type for analysis operations
pub type AnalysisEffect<T> = BoxedEffect<T, AnalysisError, RealEnv>;

// Validation type that accumulates ALL errors instead of failing fast
pub type AnalysisValidation<T> = Validation<T, NonEmptyVec<AnalysisError>>;

// Error collection for comprehensive validation reporting
pub type AnalysisErrors = NonEmptyVec<AnalysisError>;
```

#### Environment

The `RealEnv` type provides I/O capabilities to effects:
- File system access via `FileSystem` trait
- Configuration via `AnalysisEnv` trait
- Testable with `DebtmapTestEnv` for unit tests

### Module Organization

Effects are organized by domain:

```
src/
├── effects.rs              # Core effect types and helpers
├── io/effects.rs          # File system I/O effects
├── analyzers/effects.rs   # Code analysis effects
├── analysis/effects.rs    # Multi-pass analysis effects
├── risk/effects.rs        # Risk assessment effects
└── complexity/effects_wrappers.rs  # Complexity calculation effects
```

### I/O Effect Constructors

**File Operations** (`src/io/effects.rs`):
- `read_file_effect(path)` - Read file contents as string
- `read_file_bytes_effect(path)` - Read file as bytes
- `walk_dir_effect(path)` - Discover files recursively
- `exists_effect(path)` - Check file existence

**Analysis Operations** (`src/analyzers/effects.rs`):
- `analyze_file_effect(path, content, language)` - Analyze single file
- `analyze_files_effect(files)` - Batch file analysis
- `analyze_file_auto_effect(path, content)` - Auto-detect language and analyze

**Coverage Operations** (`src/risk/effects.rs`):
- `load_coverage_effect(lcov_path)` - Load test coverage data
- `parse_lcov_effect(content)` - Parse LCOV format

### Effect Composition Patterns

#### Basic Composition

```rust
use debtmap::effects::AnalysisEffect;
use debtmap::io::effects::read_file_effect;
use debtmap::analyzers::effects::analyze_file_auto_effect;

// Chain effects with .and_then()
fn analyze_path(path: PathBuf) -> AnalysisEffect<FileMetrics> {
    read_file_effect(path.clone())
        .and_then(move |content| analyze_file_auto_effect(path, content))
}
```

#### Pure Transformations

```rust
// Inject pure functions with .map()
read_file_effect(path)
    .map(|content| content.lines().count())  // Pure transformation
    .and_then(|line_count| validate_size(line_count))  // Back to effects
```

#### Parallel Execution

```rust
// Process multiple files concurrently
use rayon::prelude::*;

fn analyze_all(paths: Vec<PathBuf>) -> AnalysisEffect<Vec<FileMetrics>> {
    effect_from_fn(|env| {
        paths.par_iter()
            .map(|p| analyze_path(p.clone()).run(env))
            .collect()
    })
}
```

### Reader Pattern (Spec 199)

The Reader pattern eliminates configuration parameter threading by providing config access through the environment:

#### Config Access Helpers

```rust
use debtmap::effects::{asks_config, asks_thresholds, asks_scoring};

// Query entire config
let effect = asks_config(|config| config.get_ignore_patterns());

// Query specific section
let effect = asks_thresholds(|thresholds| {
    thresholds.and_then(|t| t.complexity)
});

// Query scoring weights
let effect = asks_scoring(|scoring| {
    scoring.map(|s| s.coverage).unwrap_or(0.5)
});
```

#### Local Config Override

```rust
use debtmap::effects::local_with_config;

// Run analysis with temporarily modified config
fn analyze_strict(path: PathBuf) -> AnalysisEffect<FileMetrics> {
    local_with_config(
        |config| {
            let mut strict = config.clone();
            if let Some(ref mut thresholds) = strict.thresholds {
                thresholds.complexity = Some(thresholds.complexity.unwrap_or(10) / 2);
            }
            strict
        },
        analyze_file_effect(path)
    )
}
```

### Retry Pattern (Spec 205)

Automatic retry of transient failures with configurable backoff:

```rust
use debtmap::effects::with_retry;
use debtmap::config::RetryConfig;

// Wrap effect with retry logic
fn read_file_resilient(path: PathBuf) -> AnalysisEffect<String> {
    let retry_config = RetryConfig {
        max_retries: 3,
        base_delay_ms: 100,
        ..Default::default()
    };

    with_retry(
        move || read_file_effect(path.clone()),
        retry_config
    )
}
```

Only errors where `error.is_retryable()` returns `true` trigger retries. Non-retryable errors (parse errors, validation errors) cause immediate failure.

### Validation Pattern

Validation accumulates ALL errors instead of failing at the first one:

```rust
use debtmap::effects::{AnalysisValidation, validation_success, validation_failure};

fn validate_thresholds(complexity: u32, lines: usize) -> AnalysisValidation<()> {
    let v1 = if complexity <= 50 {
        validation_success(())
    } else {
        validation_failure(AnalysisError::validation("Complexity too high"))
    };

    let v2 = if lines <= 1000 {
        validation_success(())
    } else {
        validation_failure(AnalysisError::validation("File too long"))
    };

    // Combine validations - collects ALL errors
    v1.and(v2)
}
```

### Running Effects

#### Synchronous Execution

```rust
use debtmap::effects::run_effect;
use debtmap::config::DebtmapConfig;

let config = DebtmapConfig::default();
let result = run_effect(analyze_effect(), config)?;
```

#### Asynchronous Execution

```rust
use debtmap::effects::run_effect_async;

let config = DebtmapConfig::default();
let result = run_effect_async(analyze_effect(), config).await?;
```

#### With Custom Environment

```rust
use debtmap::effects::run_effect_with_env;
use debtmap::testkit::DebtmapTestEnv;

let env = DebtmapTestEnv::new();
let result = run_effect_with_env(analyze_effect(), &env)?;
```

### Testing with Effects

#### Pure Function Tests

```rust
// Pure functions are trivially testable - no effects needed
#[test]
fn test_pure_logic() {
    let result = calculate_complexity(&ast);
    assert_eq!(result, 42);
}
```

#### Effect Tests with Mock Environment

```rust
use debtmap::testkit::DebtmapTestEnv;

#[tokio::test]
async fn test_file_analysis() {
    let env = DebtmapTestEnv::new()
        .with_file("test.rs", "fn main() {}");

    let effect = analyze_path("test.rs".into());
    let metrics = effect.run(&env).await.unwrap();

    assert_eq!(metrics.functions.len(), 1);
}
```

### Best Practices

#### Do: Separate I/O from Logic

```rust
// Pure transformation
fn calculate_score(metrics: &FileMetrics) -> f64 {
    metrics.complexity as f64 * 0.5
}

// I/O wrapped in effect
fn load_and_score(path: PathBuf) -> AnalysisEffect<f64> {
    analyze_path(path)
        .map(|metrics| calculate_score(&metrics))
}
```

#### Don't: Mix I/O with Logic

```rust
// Bad: I/O mixed with calculation
fn calculate_score_bad(path: PathBuf) -> f64 {
    let content = std::fs::read_to_string(&path).unwrap();  // I/O!
    let metrics = analyze(&content);
    metrics.complexity as f64 * 0.5
}
```

#### Do: Compose Effects Before Running

```rust
// Good: Build pipeline, execute once
let pipeline = discover_files(&path, &langs)
    .and_then(|files| analyze_files_effect(files))
    .map(|metrics| aggregate_metrics(metrics));

let result = run_effect(pipeline, config)?;
```

#### Don't: Execute Effects Eagerly

```rust
// Bad: Running effects too early
let files = run_effect(discover_files(&path, &langs), config)?;
let metrics = run_effect(analyze_files_effect(files), config)?;
```

### Migration Strategy

The effect system allows gradual migration:

1. **Phase 1**: Core effects infrastructure (✓ Complete)
2. **Phase 2**: Migrate I/O operations to effects (✓ Complete)
3. **Phase 3**: Extract pure analysis functions (Spec 208)
4. **Phase 4**: Composable pipeline architecture (Spec 209)

Existing code continues to work unchanged during migration via compatibility helpers like `run_effect`.

## Composable Pipeline Architecture (Spec 209)

### Overview

The pipeline architecture provides a type-safe, composable framework for building analysis workflows. It enables flexible composition of analysis stages while maintaining compile-time type safety and runtime performance.

**Location**: `src/pipeline/`

### Core Concepts

#### Pipeline Stages

Stages are the building blocks of analysis pipelines. Each stage has:
- An **input type**: What data it expects
- An **output type**: What data it produces
- An **error type**: How it can fail
- A **name**: For progress reporting

Two stage types are provided:

**PureStage**: For transformations without side effects
```rust
let stage = PureStage::new("Calculate Metrics", |ast| {
    calculate_complexity(&ast)
});
```

**FallibleStage**: For operations that can fail
```rust
let stage = FallibleStage::new("Parse Source", |source| {
    parse_source(&source)  // Returns Result<AST, ParseError>
});
```

#### Pipeline Builder

The builder provides a fluent API for composing stages:

```rust
use debtmap::pipeline::{PipelineBuilder, stage::PureStage};

let pipeline = PipelineBuilder::new()
    .stage(file_discovery)     // Output: Vec<PathBuf>
    .stage(parsing)            // Input: Vec<PathBuf>, Output: Vec<FunctionMetrics>
    .stage(call_graph)         // Input: Vec<FunctionMetrics>, Output: CallGraph
    .when(config.coverage, |p| {
        p.stage(coverage)      // Conditional stage
    })
    .with_progress()           // Enable progress reporting
    .build();

let result = pipeline.execute()?;
```

#### Type Safety

The type system ensures stages can only be composed when their types align:

```rust
// This compiles - types match
PipelineBuilder::new()
    .stage(stage_a)  // Output: Vec<String>
    .stage(stage_b)  // Input: Vec<String>, Output: i32
    .build();

// This won't compile - type mismatch
PipelineBuilder::new()
    .stage(stage_a)  // Output: Vec<String>
    .stage(stage_c)  // Input: HashMap<String, i32> - ERROR!
    .build();
```

### Pipeline Stages

The `src/pipeline/stages/` module provides reusable analysis stages:

#### Filtering Stages
- `filter_by_complexity(threshold)`: Keep functions above complexity threshold
- `filter_by_length(max_lines)`: Keep functions within length limit
- `filter_test_functions()`: Remove test functions from analysis

#### Aggregation Stages
- `average_complexity()`: Calculate mean complexity across functions
- `count_high_complexity(threshold)`: Count functions exceeding threshold
- `group_by_file()`: Group function metrics by file

#### Analysis Stages
- `build_call_graph()`: Construct function call relationships
- `propagate_purity()`: Determine pure vs impure functions
- `detect_complexity_debt()`: Identify high-complexity debt items
- `prioritize_debt()`: Score and rank debt items

### Usage Examples

#### Simple Pipeline

```rust
use debtmap::pipeline::{PipelineBuilder, stage::PureStage};

let pipeline = PipelineBuilder::new()
    .stage(PureStage::new("Generate", |()| vec![1, 2, 3]))
    .stage(PureStage::new("Double", |nums: Vec<i32>| {
        nums.into_iter().map(|n| n * 2).collect::<Vec<_>>()
    }))
    .stage(PureStage::new("Sum", |nums: Vec<i32>| {
        nums.into_iter().sum::<i32>()
    }))
    .build();

let result = pipeline.execute()?;  // Returns: 12
```

#### Conditional Stages

```rust
let pipeline = PipelineBuilder::new()
    .stage(discover_files)
    .stage(parse_metrics)
    .when(config.enable_coverage, |p| {
        p.stage(load_coverage)
    })
    .when(config.enable_context, |p| {
        p.stage(load_project_context)
    })
    .stage(detect_debt)
    .build();
```

#### Timing and Progress

```rust
let pipeline = PipelineBuilder::new()
    .stage(stage1)
    .stage(stage2)
    .stage(stage3)
    .with_progress()
    .build();

let (result, timings) = pipeline.execute_with_timing()?;

// Print performance breakdown
for timing in timings {
    println!("{}", timing.format());  // "Stage Name: 0.42s"
}
```

### Design Principles

1. **Pure Functions First**: Stages should be pure transformations when possible
2. **Type-Driven Composition**: Let the compiler guide correct stage ordering
3. **Single Responsibility**: Each stage does one thing well
4. **Immutable Data Flow**: Stages transform data without mutation
5. **Reusable Components**: Build libraries of composable stages

### Future Work

The pipeline architecture is **foundational**. Future work will:

1. **Migrate Existing Analysis**: Replace `perform_unified_analysis_computation` with pipeline
2. **Add Effect Stages**: Integrate with Stillwater effects for I/O operations
3. **Standard Configurations**: Provide `standard_pipeline()`, `fast_pipeline()`, etc.
4. **Parallel Execution**: Support parallel stage execution where safe
5. **Pipeline Visualization**: Show pipeline structure and data flow

See `examples/pipeline_demo.rs` for comprehensive examples.

## Parallel Processing Architecture

### Overview
The parallel processing system leverages Rayon for CPU-bound parallel execution, enabling analysis of large codebases in sub-second time for typical projects.

### Parallelization Strategy

#### Phase 1: Initialization (Parallel)
All initialization tasks run concurrently using Rayon's parallel iterators:
- **Data Flow Graph Construction**: Build control and data flow graphs
- **Purity Analysis**: Identify pure vs impure functions
- **Test Detection**: Optimized O(n) detection with caching
- **Initial Debt Aggregation**: Baseline metric collection

#### Phase 2: Analysis (Parallel with Batching)
- **Function Analysis**: Process functions in configurable batches
- **File Analysis**: Parallel file-level metric aggregation
- **Batch Size**: Default 100 items, tunable via options

#### Phase 3: Aggregation (Sequential)
- **Result Merging**: Combine parallel results
- **Sorting**: Priority-based ranking
- **Final Scoring**: Apply weights and thresholds

### Performance Optimizations

#### Test Detection Optimization
```rust
// Original O(n²) approach
for function in functions {
    for test in tests {
        // Check if function is called by test
    }
}

// Optimized O(n) approach with caching
let test_cache = build_test_cache(&tests);
functions.par_iter().map(|f| {
    test_cache.is_tested(f)  // O(1) lookup
})
```

#### AST Parsing Optimization (Spec 132)
Eliminates redundant parsing in call graph construction by parsing files once and reusing ASTs.

**Before**: Files parsed twice (once for content, again for analysis) = 2N parse operations
**After**: Files parsed once, ASTs cloned for subsequent use = N parse operations

**Performance Gains**:
- Parse + clone: 1.33ms per file (44% faster than re-parsing)
- Cloning overhead: ~0.15ms vs re-parse cost: ~1.07ms saved
- Total speedup: **1.8x faster** for call graph construction
- Memory impact: <100MB for 400-file projects

See `docs/spec-132-benchmark-results.md` for detailed benchmarks.

#### Parallel Configuration
- **Default**: Uses all available CPU cores
- **Configurable**: `--jobs N` flag for explicit control
- **Adaptive**: Batch size adjusts based on workload

### Thread Safety

#### Shared State Management
- **Arc<RwLock>**: For read-heavy shared data (call graphs, metrics)
- **Arc<Mutex>**: For write-heavy operations (progress tracking)
- **Immutable Structures**: Prefer immutable data where possible

#### Lock-Free Operations
- Use atomic operations for counters
- Batch updates to reduce contention
- Local accumulation with final merge

### Performance Targets

| Codebase Size | Target Time | Actual (Parallel) | Actual (Sequential) |
|---------------|-------------|-------------------|---------------------|
| 50 files      | <0.5s       | ~0.3s            | ~1.2s              |
| 250 files     | <1s         | ~0.8s            | ~5s                |
| 1000 files    | <5s         | ~3.5s            | ~20s               |

### Memory Management

#### Streaming Architecture
- Process files in batches to control memory usage
- Release intermediate results after aggregation
- Use iterators over collections where possible

#### Cache Efficiency
- Test detection cache reduces redundant computation
- Function signature caching for call graph
- Metric result caching for unchanged files
- Coverage index for O(1) coverage lookups

### Multi-Index Lookup Architecture

DebtMap uses a multi-index architecture for the call graph to enable fast lookups across different matching strategies without sacrificing memory efficiency.

#### Index Structure

The `CallGraph` maintains four complementary indexes:

1. **Primary Index** (`nodes: HashMap<FunctionId, FunctionNode>`)
   - **Purpose**: Exact lookups with full metadata
   - **Key**: Complete `FunctionId` (file, name, line, module_path)
   - **Complexity**: O(1)
   - **Use**: 92% of lookups hit this index

2. **Fuzzy Index** (`fuzzy_index: HashMap<FuzzyFunctionKey, Vec<FunctionId>>`)
   - **Purpose**: Match by name + file, ignoring line numbers
   - **Key**: `(canonical_file, normalized_name)`
   - **Complexity**: O(1) lookup + O(k) disambiguation (k = candidates)
   - **Use**: Generic functions, line drift scenarios

3. **Name Index** (`name_index: HashMap<String, Vec<FunctionId>>`)
   - **Purpose**: Cross-file lookups by function name only
   - **Key**: Normalized function name (generics stripped)
   - **Complexity**: O(1) lookup + O(n) disambiguation (n = all matching functions)
   - **Use**: Rare cases with incomplete metadata

4. **Caller/Callee Indexes** (`caller_index`, `callee_index`)
   - **Purpose**: Efficient traversal of call graph edges
   - **Key**: `FunctionId`
   - **Value**: `HashSet<FunctionId>` of connected functions
   - **Complexity**: O(1) lookup + O(d) iteration (d = degree of node)
   - **Use**: Reachability analysis, transitive closure

#### Index Maintenance

All indexes are kept in sync automatically:

```rust
pub fn add_function(&mut self, id: FunctionId, ...) {
    // 1. Add to primary index
    self.nodes.insert(id.clone(), node);

    // 2. Populate fuzzy index
    let fuzzy_key = id.fuzzy_key();
    self.fuzzy_index.entry(fuzzy_key).or_default().push(id.clone());

    // 3. Populate name index
    let normalized_name = FunctionId::normalize_name(&id.name);
    self.name_index.entry(normalized_name).or_default().push(id);
}
```

**Invariants Maintained**:
- Every `FunctionId` in `nodes` appears in exactly one `fuzzy_index` entry
- Every `FunctionId` in `nodes` appears in exactly one `name_index` entry
- All `FunctionId` references in `caller_index`/`callee_index` exist in `nodes`

#### Memory Overhead Analysis

**Primary Index**:
- ~200 bytes per function (FunctionId + FunctionNode)
- For 10,000 functions: ~2 MB

**Fuzzy Index**:
- ~100 bytes per unique (file, name) pair
- Typically 90-95% as many entries as primary index (few duplicates)
- For 10,000 functions: ~1 MB

**Name Index**:
- ~80 bytes per unique function name
- Much fewer entries (many functions share names across files)
- For 10,000 functions: ~200 KB

**Caller/Callee Indexes**:
- ~150 bytes per edge
- Typical call graph has 2-3x as many edges as nodes
- For 10,000 functions with 25,000 edges: ~3.75 MB

**Total Overhead**: ~7 MB for a 10,000 function codebase (acceptable)

#### Build Time Performance

Index construction is incremental during graph building:

- **Primary index update**: O(1) per function
- **Fuzzy index update**: O(1) amortized (hash table insertion)
- **Name index update**: O(1) amortized
- **Caller/callee index update**: O(1) per edge

**Overall Complexity**: O(n + e) where n = nodes, e = edges

**Measured Performance** (on debtmap self-analysis):
- 1,200 functions, 3,500 edges
- Index build time: ~8ms (< 5% of total analysis time)

#### Lookup Performance Guarantee

The multi-index architecture provides performance guarantees for all lookup patterns:

| Lookup Pattern | Strategy Used | Worst-Case Complexity |
|---------------|---------------|----------------------|
| Exact match | Primary index | O(1) |
| Same function, different line | Fuzzy index | O(1) + O(k) where k ≈ 2-3 |
| Generic instantiation | Fuzzy index | O(1) + O(1) (single candidate) |
| Cross-file by name | Name index | O(1) + O(m) where m = overloads |
| Find all callers | Caller index | O(1) + O(d) where d = in-degree |
| Find all callees | Callee index | O(1) + O(d) where d = out-degree |

**Key Insight**: The worst-case disambiguation factor (k, m) is bounded by practical limits:
- k ≤ 10 (rarely more than 10 functions with same name in one file)
- m ≤ 50 (rarely more than 50 functions with identical name across codebase)

#### Serialization Strategy

**Challenge**: The fuzzy and name indexes are derived data - they can be rebuilt from the primary index.

**Solution**: Skip serialization of derived indexes to reduce JSON size:

```rust
#[derive(Serialize, Deserialize)]
pub struct CallGraph {
    #[serde(with = "function_id_map")]
    pub nodes: HashMap<FunctionId, FunctionNode>,  // Serialized

    #[serde(skip)]
    pub fuzzy_index: HashMap<FuzzyFunctionKey, Vec<FunctionId>>,  // Rebuilt on load

    #[serde(skip)]
    pub name_index: HashMap<String, Vec<FunctionId>>,  // Rebuilt on load
}
```

**Benefits**:
- 40% smaller serialized size (only primary data stored)
- Faster deserialization (less JSON to parse)
- Rebuild cost is negligible (~8ms for 1,200 functions)

#### Parallel Lookup Safety

All indexes are immutable after construction during the analysis phase:

- **During construction**: Single-threaded, indexes mutated via `add_function()`
- **During analysis**: Multi-threaded, all indexes are read-only

This enables lock-free parallel lookups across all indexes without synchronization overhead.

#### Future Optimizations

**Potential Improvements**:
1. **Compact Index**: Use integer IDs instead of full `FunctionId` in secondary indexes (50% space reduction)
2. **Lazy Name Index**: Build name index on-demand for rare cross-file lookups (save 200 KB)
3. **Bloom Filters**: Add bloom filter for fast negative lookups (eliminate futile searches)
4. **Incremental Updates**: Support adding functions without full rebuild

**Trade-off Analysis**:
- Current design prioritizes simplicity and correctness
- Memory overhead is acceptable for projects up to 100K functions
- Optimization effort should focus on analysis algorithms, not indexing

## Call Graph

### FunctionId Matching Strategies

DebtMap uses a sophisticated multi-level matching strategy to resolve function references in the call graph, enabling accurate call graph construction even when exact metadata (line numbers, module paths) is unavailable or inconsistent.

#### The Problem

Call graph construction faces several challenges:

1. **Generic Functions**: Same function with different type parameters (e.g., `map<T>` vs `map<String>`)
2. **Line Number Drift**: AST line numbers may differ from call site line numbers due to macros, attributes, or comments
3. **Cross-Module Calls**: Calls to functions in other files may lack full metadata
4. **Incomplete Information**: Some analysis passes may only have function names, not full context

Traditional exact matching (all fields must match) causes false negatives in these scenarios, resulting in incomplete call graphs and inaccurate reachability analysis.

#### Three-Tier Matching Strategy

DebtMap implements a fallback chain with three matching strategies:

##### 1. Exact Match (Fastest)
- **Key**: `(file, name, line, module_path)` - all fields must match
- **Use Case**: Most common case when full metadata is available
- **Complexity**: O(1) hash lookup
- **Example**: Looking up `foo` at `src/main.rs:100` with full context

##### 2. Fuzzy Match (Moderate)
- **Key**: `(canonical_file, normalized_name)` - ignores line and module path
- **Normalization**: Strips generic type parameters and whitespace
  - `map<T>``map`
  - `process< A , B >``process`
- **Use Case**: Generic instantiations, line number drift
- **Complexity**: O(1) hash lookup + O(n) disambiguation if multiple candidates
- **Example**: `map<String>` at line 150 finds `map` defined at line 100

**Disambiguation**: If multiple candidates found (e.g., overloaded functions), choose by:
- **Line Proximity**: Select function closest to query line number
- **Module Path**: Prefer function with matching module path

##### 3. Name-Only Match (Slowest)
- **Key**: `normalized_name` - only function name matters
- **Use Case**: Cross-file calls, incomplete metadata
- **Complexity**: O(1) hash lookup + O(n) disambiguation across all matching functions
- **Example**: Call to `parse_config` without file context finds all `parse_config` functions

**Disambiguation**: Prioritize by:
1. **Module Path Match**: If query has module path, prefer exact match
2. **Line Proximity**: Choose function with closest line number

#### Name Normalization

Function name normalization ensures consistent matching across generic instantiations:

```rust
// Before normalization:
"map<T>"           // Generic parameter
"map<String>"      // Concrete type
"process< A , B >" // Whitespace variation

// After normalization (FunctionId::normalize_name):
"map"              // Generic parameter stripped
"map"              // Concrete type stripped
"process"          // Whitespace and generics stripped
```

**Preserved Elements**:
- Namespace qualifiers: `std::vec::Vec``std::vec::Vec`
- Module paths: `crate::module::function``crate::module::function`

#### Lookup Flow

```
Query: FunctionId { file: "src/main.rs", name: "map<String>", line: 150, ... }
[1. Exact Lookup]
    nodes.get(query) → None (no exact match)
[2. Fuzzy Lookup]
    fuzzy_key = (canonical_path("src/main.rs"), normalize("map<String>"))
              = (src/main.rs, "map")
    fuzzy_index.get(fuzzy_key) → [map@100]
    Single candidate → Return map@100 ✓
```

If multiple candidates:
```
[2. Fuzzy Lookup]
    fuzzy_index.get(fuzzy_key) → [map@100, map@200]
    disambiguate_by_line(candidates, 150)
        → abs_diff(100, 150) = 50
        → abs_diff(200, 150) = 50
        → Return map@100 (first match in tie) ✓
```

If fuzzy fails:
```
[3. Name-Only Lookup]
    name_index.get("map") → [src/main.rs:map@100, src/util.rs:map@50]
    disambiguate_by_module(candidates, "main")
        → src/main.rs:map@100 has module "main" → Return ✓
```

#### Performance Characteristics

| Strategy | Lookup Complexity | Disambiguation | Accuracy |
|----------|-------------------|----------------|----------|
| Exact | O(1) | None | 100% (when metadata available) |
| Fuzzy | O(1) + O(k) | k = candidates in same file | 95% (handles generics, line drift) |
| Name-Only | O(1) + O(n) | n = all functions with name | 80% (cross-file, may be ambiguous) |

**Typical Distribution** (empirical data from debtmap self-analysis):
- 92% resolved by exact match
- 7% resolved by fuzzy match
- 1% resolved by name-only match

#### Integration with Call Graph Construction

When adding a function call, the matching strategy determines the target:

```rust
// Example: Processing a call to "map<String>"
let query = FunctionId::new(file, "map<String>".to_string(), 150);
let target = graph.find_function(&query);

match target {
    Some(func_id) => graph.add_call(caller, func_id, CallType::Direct),
    None => {
        // Function not in graph - may be external dependency
        log::warn!("Unresolved call to {}", query.name);
    }
}
```

#### Benefits

- **Reduced False Negatives**: Generic functions and line drift no longer break call graph
- **Improved Reachability**: Cross-file calls correctly identified
- **Graceful Degradation**: Falls back to less precise matching when exact data unavailable
- **Minimal Performance Cost**: Indexing overhead is ~5% of total analysis time

#### Testing

Comprehensive unit tests validate all matching strategies:

- `test_exact_lookup`: Verifies O(1) exact matching
- `test_fuzzy_lookup_different_line`: Line number drift handling
- `test_fuzzy_lookup_generic_function`: Generic type parameter normalization
- `test_name_only_lookup`: Cross-file resolution
- `test_disambiguate_by_line_proximity`: Tie-breaking by line distance
- `test_disambiguate_by_module_path`: Module path preference

See `src/priority/call_graph/graph_operations.rs:367-484` for test implementations.

## Call Graph Debug and Validation Infrastructure

DebtMap includes comprehensive debugging and validation tools for the call graph system, enabling developers and users to understand, troubleshoot, and validate function resolution.

### Architecture Components

#### CallGraphDebugger

Located in `src/analyzers/call_graph/debug.rs`, the debugger provides detailed insights into call resolution:

**Core Responsibilities:**
- Record resolution attempts (successful and failed)
- Track resolution strategies and their effectiveness
- Measure performance metrics (timing percentiles)
- Generate detailed reports in text or JSON format

**Data Structures:**

```rust
pub struct CallGraphDebugger {
    attempts: Vec<ResolutionAttempt>,      // All resolution attempts
    trace_functions: HashSet<String>,       // Functions to trace
    stats: ResolutionStatistics,            // Aggregate statistics
    config: DebugConfig,                    // Output configuration
}

pub struct ResolutionAttempt {
    caller: FunctionId,                     // Calling function
    callee_name: String,                    // Target function name
    strategy_attempts: Vec<StrategyAttempt>, // Strategies tried
    result: Option<FunctionId>,             // Final resolution
    duration: Duration,                     // Time spent
}

pub enum ResolutionStrategy {
    Exact,      // Exact name and location match
    Fuzzy,      // Normalized name with disambiguation
    NameOnly,   // Name-only match across all files
}
```

**Output Formats:**
- **Text**: Human-readable report with sections, statistics, and recommendations
- **JSON**: Machine-parsable format for tooling integration

**Statistics Tracked:**
- Total resolution attempts
- Success/failure rates
- Strategy effectiveness (which strategies work best)
- Performance percentiles (p50, p95, p99)
- Common failure patterns

#### CallGraphValidator

Located in `src/analyzers/call_graph/validation.rs`, the validator checks structural integrity:

**Core Responsibilities:**
- Detect structural issues (dangling edges, orphaned nodes, duplicates)
- Identify heuristic warnings (suspicious patterns)
- Calculate overall health score (0-100)
- Generate actionable validation reports

**Validation Checks:**

1. **Structural Issues** (Critical):
   - **Dangling Edges**: Calls to non-existent functions
   - **Orphaned Nodes**: Functions with no incoming or outgoing edges
   - **Duplicate Nodes**: Same function registered multiple times

2. **Heuristic Warnings** (Suspicious Patterns):
   - **High Fan-In**: Functions with >50 callers (potential bottlenecks)
   - **High Fan-Out**: Functions calling >50 others (potential god objects)
   - **Files with No Calls**: All functions in a file are uncalled (potential dead code)
   - **Unused Public Functions**: Public functions with no callers

**Health Score Calculation:**
```rust
health_score = 100
    - (structural_issues_count × 10)  // Critical: -10 points each
    - (warnings_count × 2)             // Minor: -2 points each
```

**Interpretation:**
- **95-100**: Excellent call graph quality
- **85-94**: Good, acceptable for production
- **<85**: Needs attention, high unresolved rate

#### Integration with Analysis Pipeline

The debug and validation infrastructure integrates into the analyze command at `src/commands/analyze.rs`:

```rust
// After unified analysis completes
if config.debug_call_graph || config.validate_call_graph {
    handle_call_graph_diagnostics(&unified_analysis, &config)?;
}

fn handle_call_graph_diagnostics(...) {
    // 1. Run validation if requested
    if config.validate_call_graph {
        let report = CallGraphValidator::validate(call_graph);
        // Output validation report to stderr
    }

    // 2. Run debug output if requested
    if config.debug_call_graph {
        let mut debugger = CallGraphDebugger::new(config);
        debugger.finalize_statistics();
        debugger.write_report(&mut stdout)?;
    }

    // 3. Show statistics if requested
    if config.call_graph_stats_only {
        // Output quick statistics
    }
}
```

### CLI Flags

**Debug Flags:**
- `--debug-call-graph`: Enable debug mode with detailed resolution reports
- `--debug-format <text|json>`: Output format (default: text)
- `--trace-function <name>`: Trace specific functions (repeatable)

**Validation Flags:**
- `--validate-call-graph`: Run structural validation checks
- `--call-graph-stats-only`: Show only statistics (fast, minimal output)

**Verbosity:**
- `-v`: Show validation warnings in addition to structural issues
- `-vv`: Show successful resolutions in debug output

### Performance Considerations

**Debug Mode Overhead:**
- Baseline: <5% overhead (primarily I/O for report generation)
- With tracing: 10-15% overhead (depends on trace scope)
- Target: <20% overhead per spec 149

**Optimization Strategies:**
1. **Lazy Statistics**: Only calculate percentiles when finalized
2. **Selective Tracing**: Filter by function name to reduce recording
3. **Stream Output**: Write reports incrementally rather than buffering
4. **Minimal Recording**: Record only essential data during resolution

**Memory Usage:**
- Debug mode stores resolution attempts (typically <10MB for 1000 functions)
- Validation mode operates in-place with minimal allocation
- Statistics use aggregated counters, not raw data

### Future Enhancements

**Potential Improvements:**

1. **Deep CallResolver Integration**: Currently the debugger is invoked after analysis completes and reports on the final call graph structure. Future work could instrument `CallResolver::resolve_call()` to record individual resolution attempts with timing and strategy details, providing more granular debugging information.

2. **Interactive Debug Mode**: Real-time resolution tracing with breakpoints

3. **Visual Call Graph**: Generate GraphViz/DOT files for visualization

4. **Resolution Confidence Scores**: Assign confidence levels to resolved calls

5. **Automated Fixes**: Suggest code changes to improve resolution

6. **Continuous Monitoring**: Track resolution quality over time in CI/CD

### Testing

**Integration Tests:** `tests/call_graph_debug_output_test.rs`
- Debug flag produces expected output format
- Validation report includes health score
- JSON format is valid and parseable
- Text format is human-readable
- Performance overhead stays within bounds
- Trace function filtering works correctly
- Combined debug+validate flags work together

**Unit Tests:**
- `src/analyzers/call_graph/debug.rs`: Debugger functionality
- `src/analyzers/call_graph/validation.rs`: Validator checks

### Documentation

**User Documentation:** `README.md` - "Debugging Call Graph Issues" section
- Command examples for common scenarios
- Interpretation guide for health scores and statistics
- Performance considerations for large codebases
- Troubleshooting common issues

**Architecture Documentation:** This section
- Component responsibilities and data structures
- Integration points and control flow
- Performance characteristics and optimization strategies
- Future enhancement opportunities

## Coverage Indexing System

### Overview
The coverage indexing system provides high-performance test coverage lookups during file analysis with minimal overhead. It transforms O(n) linear searches through LCOV data into O(1) hash lookups and O(log n) range queries.

### Design

#### Two-Level Index Architecture
The `CoverageIndex` uses a dual indexing strategy:

1. **Primary Index (HashMap)**: O(1) exact lookups
   - Key: `(PathBuf, String)` - file path and function name
   - Value: `FunctionCoverage` - coverage data including percentage and uncovered lines
   - Use case: When exact function name is known from AST analysis

2. **Secondary Index (BTreeMap)**: O(log n) line-based lookups
   - Outer: `HashMap<PathBuf, BTreeMap<usize, FunctionCoverage>>`
   - Inner BTreeMap: Maps start line → function coverage
   - Use case: Fallback when function names mismatch between AST and LCOV

#### Performance Characteristics

| Operation | Complexity | Use Case |
|-----------|-----------|----------|
| Index Build | O(n) | Once at startup, where n = coverage records |
| Exact Name Lookup | O(1) | Primary lookup method |
| Line-Based Lookup | O(log m) | Fallback, where m = functions in file |
| Batch Parallel Lookup | O(n/p) | Multiple lookups, where p = CPU cores |

#### Memory Footprint
- **Estimated**: ~200 bytes per coverage record
- **Typical**: 1-2 MB for medium projects (5000 functions)
- **Large**: 10-20 MB for large projects (50000 functions)
- **Trade-off**: Acceptable memory overhead for massive performance gain

### Thread Safety

#### Arc-Wrapped Sharing
The coverage index is wrapped in `Arc<CoverageIndex>` for lock-free sharing across parallel threads:

```rust
pub struct LcovData {
    coverage_index: Arc<CoverageIndex>,
    // ...
}
```

#### Benefits
- **Zero-cost sharing**: No mutex locks during reads
- **Clone-friendly**: Arc clone is cheap (atomic refcount increment)
- **Parallel-safe**: Multiple threads can query simultaneously without contention

### Performance Targets

The coverage indexing system maintains performance overhead within acceptable limits:

| Metric | Target | Measured |
|--------|--------|----------|
| Index build time | <50ms for 5000 records | ~20-30ms |
| Lookup time (exact) | <1μs per lookup | ~0.5μs |
| Lookup time (line-based) | <10μs per lookup | ~5-8μs |
| Analysis overhead | ≤3x baseline | ~2.5x actual |

**Baseline**: File analysis without coverage lookups (~53ms for 100 files)
**Target**: File analysis with coverage lookups (≤160ms)
**Actual**: Typically achieves ~130-140ms with indexed lookups

### Usage Patterns

#### During LCOV Parsing
```rust
let data = parse_lcov_file(path)?;
// Index is automatically built at end of parsing
// data.coverage_index is ready for use
```

#### During File Analysis (Parallel)
```rust
files.par_iter().for_each(|file| {
    // Each thread can query the shared Arc<CoverageIndex>
    let coverage = data.get_function_coverage(file, function_name);
    // O(1) lookup with no lock contention
});
```

#### Batch Queries for Efficiency
```rust
let queries = collect_all_function_queries();
let results = data.batch_get_function_coverage(&queries);
// Parallel batch processing using rayon
```

### Implementation Notes

#### Name Matching Strategies
The system tries multiple strategies to match functions:
1. Exact name match (primary)
2. Line-based match with tolerance (±2 lines)
3. Boundary-based match for accurate AST ranges

#### Tolerance for AST/LCOV Discrepancies
Line numbers may differ between AST and LCOV due to:
- Comment handling differences
- Macro expansion
- Attribute processing

The 2-line tolerance handles most real-world cases.

#### Trait Method Coverage Matching with Name Variants (Spec 181)

**Challenge**: Function names in Rust code differ between how debtmap stores them (from AST analysis) and how LCOV stores them (from demangled symbols).

**Example Mismatch**:
- **Debtmap stores**: `RecursiveMatchDetector::visit_expr` (includes impl type name)
- **LCOV stores**: `visit_expr` (method name only, from demangled symbol)

**Solution**: Multi-variant name matching strategy

When looking up coverage for trait implementation methods, the system tries multiple name variants in order:

1. **Full qualified name** (e.g., `RecursiveMatchDetector::visit_expr`)
   - Most specific match
   - Handles exact matches where LCOV includes full path

2. **Method name only** (e.g., `visit_expr`)
   - Catches LCOV's simplified naming from symbol demangling
   - Primary solution for trait methods

3. **Trait-qualified name** (e.g., `Visit::visit_expr`)
   - Handles alternative demangling strategies
   - Future-proofs against LCOV format changes

**Performance Impact**:
- Adds at most O(k) overhead where k ≤ 3 (number of variants)
- Still O(1) hash lookups for each variant attempt
- Measured impact: <2% increase in coverage lookup time
- Line-based fallback remains O(log n) if all variants fail

**Scope**:
- Applies only to trait implementation methods
- Regular functions and inherent impl methods use single name (no overhead)
- Automatically detects trait methods during AST analysis

**Benefits**:
- Eliminates false-positive "no coverage data" reports for trait methods
- Correctly reports 90%+ coverage instead of 0% for well-tested trait impls
- No manual configuration required
- Backward compatible with existing LCOV files

See: `src/risk/coverage_index.rs`, Spec 181

### Future Optimizations
- **Incremental updates**: Rebuild only changed files
- **Compressed storage**: Use compact representations for large datasets
- **Lazy loading**: Build index on-demand per file
- **Persistent cache**: Serialize index to disk for faster startup

## Metric Categories (Spec 118)

### Overview

Debtmap distinguishes between two fundamental categories of metrics to help users understand which metrics are precise measurements versus heuristic estimates. This distinction is critical for proper usage in CI/CD pipelines and decision-making.

### Measured Metrics

**Definition**: Metrics computed directly from Abstract Syntax Tree (AST) analysis.

**Characteristics**:
- **Deterministic**: Same code always produces the same value
- **Precise**: Exact counts from syntax parsing, not approximations
- **Language-specific**: Uses syn for native Rust AST parsing with full language support
- **Suitable for thresholds**: Reliable for quality gates and CI/CD enforcement

**Examples**:

| Metric | Description | Computation Method |
|--------|-------------|-------------------|
| `cyclomatic_complexity` | Decision point count | Count if, match, while, for, && , \|\| , ? |
| `cognitive_complexity` | Readability measure | Weighted nesting and control flow analysis |
| `nesting_depth` | Maximum nesting levels | Track depth during AST traversal |
| `loc` | Lines of code | Physical line count from source |
| `parameter_count` | Function parameters | Count items in function signature |

**Usage in CI/CD**:
```bash
# GOOD: Use measured metrics for quality gates
debtmap validate . --threshold-complexity 15 --max-critical 0

# These thresholds are precise and repeatable
```

### Estimated Metrics

**Definition**: Heuristic approximations calculated using formulas, not direct AST measurements.

**Characteristics**:
- **Heuristic**: Based on mathematical formulas and assumptions
- **Approximate**: Close estimates, not exact counts
- **Useful for prioritization**: Help estimate effort and risk
- **Not suitable for hard thresholds**: Use for relative comparisons, not absolute gates

**Examples**:

| Metric | Formula | Purpose | Limitations |
|--------|---------|---------|-------------|
| `est_branches` | `max(nesting, 1) × cyclomatic ÷ 3` | Estimate test cases needed | Project-specific, not comparable across codebases |

**Formula Rationale**:
- **Nesting multiplier**: Deeper nesting creates exponentially more path combinations
- **Cyclomatic base**: More decision points → more paths
- **÷ 3 adjustment**: Empirical factor based on typical branch coverage patterns

**Usage in Analysis**:
```rust
// Internal calculation (example from recommendation.rs)
let est_branches = func.nesting.max(1) * cyclomatic / 3;

// Used in recommendations:
// "With ~12 estimated branches and complexity 15/8,
//  this represents high risk. Minimum 8 test cases needed."
```

### Terminology Evolution

#### Before Spec 118: "branches"
- Displayed as `branches=8` in terminal output
- Caused user confusion:
  - Assumed to be precise AST measurement
  - Confused with cyclomatic complexity
  - Unclear that it was formula-based

#### After Spec 118: "est_branches"
- Renamed to `est_branches=8` to make estimation explicit
- Benefits:
  - **Clear intent**: "est_" prefix indicates approximation
  - **Avoid confusion**: Distinct from cyclomatic complexity
  - **Correct expectations**: Users know it's a heuristic

**Implementation Changes**:
```rust
// Before (misleading):
format!("branches={}", branch_count)

// After (clear):
format!("est_branches={}", branch_count)  // Estimation made explicit

// Added documentation comments:
// est_branches: Estimated execution paths (heuristic)
// Formula: max(nesting, 1) × cyclomatic ÷ 3
// Note: This is an ESTIMATE, not a count from the AST
```

### Design Principles

#### Principle 1: Precision Transparency
Users must know whether a metric is measured or estimated.

**Bad**:
```
complexity=12, branches=8  # Ambiguous: Is "branches" measured or estimated?
```

**Good**:
```
cyclomatic=12, est_branches=8  # Clear: "est_" indicates estimation
```

#### Principle 2: Appropriate Usage
Measured metrics for enforcement, estimated metrics for guidance.

**Measured metrics**:
- CI/CD quality gates
- Code review standards
- Cross-project comparisons
- Compliance requirements

**Estimated metrics**:
- Prioritization heuristics
- Effort estimation
- Risk assessment
- Testing guidance

#### Principle 3: Formula Documentation
All estimated metrics must document their formula and rationale.

Example from `print_metrics_explanation()`:
```rust
println!("### Estimated Metrics");
println!("  • est_branches: Estimated execution paths");
println!("    Formula: max(nesting_depth, 1) × cyclomatic_complexity ÷ 3");
println!("    Purpose: Estimate test cases needed for branch coverage");
println!("    Note: This is an ESTIMATE, not a count from the AST");
```

### Data Flow Integration

```
File Analysis
[AST Parsing]
MEASURED METRICS:
  ├─ cyclomatic_complexity (count decision points)
  ├─ cognitive_complexity (weighted readability)
  ├─ nesting_depth (track max nesting)
  ├─ loc (count lines)
  └─ parameter_count (count params)
ESTIMATED METRICS:
  └─ est_branches = f(nesting, cyclomatic)  [calculated on-demand]
Risk Scoring & Prioritization
Output Formatting
  ├─ Terminal: Shows est_branches
  ├─ JSON: Only measured metrics serialized
  └─ Verbose: Explains formulas
```

### Future Enhancements

**Planned estimated metrics**:
- `est_test_cases`: Estimated test cases for full coverage
- `est_effort_hours`: Estimated refactoring effort
- `est_bug_density`: Predicted bug probability

**Validation framework**:
- Empirical validation of estimation formulas
- A/B testing of formula variations
- Confidence intervals for estimates

**Metric metadata**:
```rust
pub struct MetricMetadata {
    name: String,
    category: MetricCategory,  // Measured | Estimated
    formula: Option<String>,   // For estimated metrics
    suitable_for_thresholds: bool,
    documentation_url: String,
}
```

### References

- **User Documentation**: `book/src/metrics-reference.md`
- **CLI Help**: `debtmap analyze --explain-metrics`
- **FAQ**: `book/src/faq.md#measured-vs-estimated`
- **Implementation**: `src/priority/scoring/recommendation.rs`

## Data Structures

### FunctionId Keys and Indexes

The call graph uses specialized key types to enable efficient multi-strategy lookups while maintaining type safety and clarity.

#### Core Types

##### FunctionId (Primary Identifier)

```rust
pub struct FunctionId {
    pub file: PathBuf,
    pub name: String,
    pub line: usize,
    pub module_path: String,
}
```

**Purpose**: Uniquely identifies a function in the codebase with complete metadata.

**Design Decisions**:
- **PathBuf for file**: Supports platform-specific paths and canonicalization
- **String for name**: Generic instantiations stored as `map<T>`, `map<String>`, etc.
- **usize for line**: AST-reported line number (1-indexed)
- **String for module_path**: Rust module hierarchy (e.g., `crate::analysis::complexity`)

**Usage**: Primary key in `CallGraph.nodes` HashMap

##### ExactFunctionKey (Exact Match)

```rust
pub struct ExactFunctionKey {
    pub file: PathBuf,
    pub name: String,
    pub line: usize,
    pub module_path: String,
}
```

**Purpose**: Key for exact matching - all fields must match.

**Relationship to FunctionId**: Identical structure but semantically distinct (key vs identifier).

**Generation**: `func_id.exact_key()` clones all fields

**Hash/Eq Implementation**: Derives hash and equality from all four fields

##### FuzzyFunctionKey (Fuzzy Match)

```rust
pub struct FuzzyFunctionKey {
    pub canonical_file: PathBuf,
    pub normalized_name: String,
}
```

**Purpose**: Key for fuzzy matching - ignores line numbers and module paths.

**Normalization**:
- **canonical_file**: Canonicalized path (resolves symlinks, relative paths)
- **normalized_name**: Generic parameters stripped (`map<T>``map`)

**Generation**: `func_id.fuzzy_key()`
```rust
FuzzyFunctionKey {
    canonical_file: FunctionId::canonicalize_path(&self.file),
    normalized_name: FunctionId::normalize_name(&self.name),
}
```

**Hash/Eq Implementation**: Only considers file and normalized name

**Example**:
```rust
// These two FunctionIds produce the same FuzzyFunctionKey
let id1 = FunctionId::new("src/main.rs", "map<T>", 100);
let id2 = FunctionId::new("src/main.rs", "map<String>", 150);

assert_eq!(id1.fuzzy_key(), id2.fuzzy_key());
```

##### SimpleFunctionKey (Name-Only Match)

```rust
pub struct SimpleFunctionKey {
    pub normalized_name: String,
}
```

**Purpose**: Key for name-only matching - ignores file, line, and module path.

**Normalization**: Same as `FuzzyFunctionKey` (strips generics)

**Generation**: `func_id.simple_key()`
```rust
SimpleFunctionKey {
    normalized_name: FunctionId::normalize_name(&self.name),
}
```

**Hash/Eq Implementation**: Only considers normalized name

**Example**:
```rust
// These FunctionIds in different files produce the same SimpleFunctionKey
let id1 = FunctionId::new("src/main.rs", "parse_config", 100);
let id2 = FunctionId::new("src/util.rs", "parse_config", 200);

assert_eq!(id1.simple_key(), id2.simple_key());
```

#### Index Data Structures

##### Primary Index
```rust
nodes: im::HashMap<FunctionId, FunctionNode>
```

- **Key Type**: Complete `FunctionId`
- **Value Type**: `FunctionNode` with metadata (complexity, test status, etc.)
- **Lookup**: `nodes.get(&func_id)` - O(1)
- **Purpose**: Exact match lookups

##### Fuzzy Index
```rust
fuzzy_index: std::collections::HashMap<FuzzyFunctionKey, Vec<FunctionId>>
```

- **Key Type**: `FuzzyFunctionKey` (file + normalized name)
- **Value Type**: `Vec<FunctionId>` - multiple functions with same name in file
- **Lookup**: `fuzzy_index.get(&fuzzy_key)` - O(1) + O(k) disambiguation
- **Purpose**: Handle generic functions and line number drift

**Value is Vec because**:
- Multiple functions with same base name in one file (e.g., overloads in trait impls)
- Disambiguation needed via line proximity or module path

##### Name Index
```rust
name_index: std::collections::HashMap<String, Vec<FunctionId>>
```

- **Key Type**: Normalized function name (String)
- **Value Type**: `Vec<FunctionId>` - all functions with this name across all files
- **Lookup**: `name_index.get(&normalized_name)` - O(1) + O(n) disambiguation
- **Purpose**: Cross-file lookups when file information unavailable

**Value is Vec because**:
- Same function name appears in multiple files
- Disambiguation needed via module path or line proximity

#### Type Safety Benefits

**Compile-Time Guarantees**:
1. **No key confusion**: Cannot accidentally use `FuzzyFunctionKey` with exact match logic
2. **Explicit normalization**: `normalize_name()` clearly shows where normalization occurs
3. **Immutable keys**: All key types are `Clone + Hash + Eq` with no mutation methods

**Example - Type System Prevents Errors**:
```rust
// Compile error: cannot use FunctionId directly as fuzzy key
let bad_key: FuzzyFunctionKey = func_id;  // ❌ Type mismatch

// Must explicitly request fuzzy key
let good_key: FuzzyFunctionKey = func_id.fuzzy_key();  // ✓ Explicit conversion
```

#### Memory Layout Optimization

**Key Size Analysis**:
```
FunctionId:         ~150 bytes (PathBuf + 2 Strings + usize)
ExactFunctionKey:   ~150 bytes (identical layout)
FuzzyFunctionKey:   ~100 bytes (PathBuf + String)
SimpleFunctionKey:  ~50 bytes  (String only)
```

**Index Storage**:
- Primary index: `FunctionId``FunctionNode` (~350 bytes per entry)
- Fuzzy index: `FuzzyFunctionKey``Vec<FunctionId>` (~100 + 150k bytes)
- Name index: `String``Vec<FunctionId>` (~50 + 150n bytes)

**Trade-off**: Larger key types for type safety, but overall memory overhead is acceptable (<10 MB for large codebases).

#### Serialization Format

**Challenge**: Keys are derived from `FunctionId`, so we only need to serialize the primary index.

**Implementation**:
```rust
#[derive(Serialize, Deserialize)]
pub struct CallGraph {
    #[serde(with = "function_id_map")]
    pub nodes: HashMap<FunctionId, FunctionNode>,  // ✓ Serialized

    #[serde(skip)]
    pub fuzzy_index: HashMap<FuzzyFunctionKey, Vec<FunctionId>>,  // ✗ Skipped

    #[serde(skip)]
    pub name_index: HashMap<String, Vec<FunctionId>>,  // ✗ Skipped
}
```

**Rationale**:
- Fuzzy and name indexes are deterministic transforms of the primary index
- Rebuild cost is negligible (~8ms for 1,200 functions)
- JSON size reduced by 40% (only essential data serialized)

**Rebuild Logic**:
```rust
impl CallGraph {
    fn rebuild_indexes(&mut self) {
        for (func_id, _) in &self.nodes {
            // Populate fuzzy index
            let fuzzy_key = func_id.fuzzy_key();
            self.fuzzy_index.entry(fuzzy_key).or_default().push(func_id.clone());

            // Populate name index
            let name = FunctionId::normalize_name(&func_id.name);
            self.name_index.entry(name).or_default().push(func_id.clone());
        }
    }
}
```

#### Testing Strategy

**Property Tests** (using `proptest`):
```rust
proptest! {
    // Generic functions should have equal fuzzy keys
    fn generic_normalization_idempotent(base_name: String) {
        let name1 = format!("{}<T>", base_name);
        let name2 = format!("{}<String>", base_name);
        assert_eq!(
            FunctionId::normalize_name(&name1),
            FunctionId::normalize_name(&name2)
        );
    }

    // Fuzzy keys ignore line differences
    fn fuzzy_key_line_independence(name: String, line1: usize, line2: usize) {
        let id1 = FunctionId::new("test.rs".into(), name.clone(), line1);
        let id2 = FunctionId::new("test.rs".into(), name, line2);
        assert_eq!(id1.fuzzy_key(), id2.fuzzy_key());
    }
}
```

**Unit Tests**: See `src/priority/call_graph/types.rs:225-282` for comprehensive key equality tests.

### Call Graph Cross-File Resolution

The call graph uses a two-phase approach for resolving cross-file calls that optimizes performance through parallelization while maintaining data structure consistency.

#### Phase 1: Parallel Resolution

The first phase processes unresolved calls concurrently using Rayon's parallel iterators. This phase is read-only and operates on immutable data, making it safe for concurrent execution across multiple CPU cores.

**Key characteristics:**
- **Pure functional resolution**: The `resolve_call_with_advanced_matching()` function is a pure, static method that takes immutable references and returns new data without side effects
- **Parallel iteration**: Uses `par_iter()` to distribute resolution work across available CPU cores
- **Batch collection**: All successful resolutions are collected into a vector of `(original_call, resolved_callee)` tuples
- **Thread safety**: No shared mutable state during resolution eliminates the need for locks or synchronization

**Performance scaling:**
- 2 cores: ~8% speedup
- 4 cores: ~12% speedup
- 8 cores: ~15% speedup (diminishing returns due to batching overhead)

#### Phase 2: Sequential Updates

The second phase applies all resolved calls to the graph sequentially, updating caller/callee indexes and edges in batch while maintaining data structure consistency.

**Key characteristics:**
- **Batch updates**: Processes all resolutions collected from the parallel phase
- **Index consistency**: Maintains synchronization between caller_index, callee_index, and edges
- **Deterministic**: Produces identical results regardless of parallel execution order
- **Memory efficient**: Temporary resolutions vector adds only ~200-400KB overhead for typical projects

**Data flow:**
```
Unresolved Calls
    ↓
[Parallel Phase - Read-Only]
par_iter() → resolve_call_with_advanced_matching()
    ↓
Vector<(FunctionCall, FunctionId)>
    ↓
[Sequential Phase - Mutation]
for (call, resolved) in resolutions {
    apply_call_resolution()
}
    ↓
Updated Call Graph
```

#### Performance Impact

This two-phase architecture achieves **10-15% speedup** compared to sequential resolution on multi-core systems. The speedup comes from parallelizing the CPU-intensive resolution logic while keeping the fast update phase sequential.

**Measured performance** (392-file codebase with ~1500 unresolved calls):
- Sequential resolution: ~100ms
- Parallel resolution (4 cores): ~87.5ms (12.5% improvement)
- Parallel resolution (8 cores): ~85ms (15% improvement)

**Memory overhead**: <10MB additional memory for the resolutions vector, even for large projects with thousands of unresolved calls.

#### Thread Safety Guarantees

The parallel resolution phase is thread-safe because:
1. **Immutable inputs**: All function data (`all_functions` vector) is cloned before parallel processing
2. **No shared mutation**: Each thread operates on independent call resolution logic
3. **Independent operations**: Call resolutions have no dependencies on each other
4. **Result collection**: Rayon safely collects results from parallel threads into a single vector

The sequential update phase requires no synchronization since it runs single-threaded after parallel resolution completes.

## Data Flow

```
Input Files
[Parallel] Parse AST
[Parallel] Extract Metrics
[Parallel] Build Call Graph
[Parallel] Detect Tests
[Parallel] Load & Index Coverage (if --lcov provided)
[Parallel] Calculate Debt with Coverage Lookups
[Sequential] Aggregate Results
[Sequential] Apply Weights
Output Report
```

## Configuration

### Performance Tuning Options

#### Command Line Flags
- `--jobs N`: Number of parallel jobs (default: CPU count)
- `--batch-size N`: Items per batch (default: 100)
- `--no-parallel`: Disable parallel processing
- `--progress`: Show progress indicators

#### Environment Variables
- `RAYON_NUM_THREADS`: Override thread pool size
- `DEBTMAP_BATCH_SIZE`: Default batch size

### Adaptive Behavior
The system automatically adjusts based on:
- Available CPU cores
- System memory
- Codebase size
- File complexity distribution

### Single-Stage Filtering (Spec 243)

DebtMap uses a **single-stage filtering** approach where all filtering happens during item construction. There are no post-filtering stages - items that pass thresholds are added to `UnifiedAnalysis`, and those that don't are never created.

#### Configuration Precedence

Filtering configuration follows a strict precedence chain:

1. **CLI arguments** (highest priority)
   - `--min-score <value>`: Minimum unified score threshold
   - Direct command-line flags always win

2. **Environment variables**
   - `DEBTMAP_MIN_SCORE_THRESHOLD`: Minimum score (0-100 scale)
   - `DEBTMAP_MIN_CYCLOMATIC`: Minimum cyclomatic complexity
   - `DEBTMAP_MIN_COGNITIVE`: Minimum cognitive complexity
   - `DEBTMAP_MIN_RISK`: Minimum risk score (0-1 scale)

3. **Config file** (`.debtmap.toml`)
   - `thresholds.min_score_threshold`: Default minimum score
   - Other threshold configurations

4. **Hardcoded defaults** (lowest priority)
   - `min_score: 3.0` (default threshold)
   - Conservative defaults to reduce noise

#### Implementation

The `ItemFilterConfig` struct (`src/priority/filter_config.rs`) centralizes all filtering configuration:

```rust
pub struct ItemFilterConfig {
    pub min_score: f64,           // Minimum unified score (0-100)
    pub min_cyclomatic: u32,      // Minimum cyclomatic complexity
    pub min_cognitive: u32,       // Minimum cognitive complexity
    pub min_risk: f64,            // Minimum risk score (0-1)
    pub show_t4_items: bool,      // Show low-priority items
}
```

Filtering happens during item creation in `UnifiedAnalysis::add_item()` and `UnifiedAnalysis::add_file_item()`. Items below thresholds are never added to the analysis.

#### Consistency Across Output Modes

Both TUI and non-TUI output modes use the **same** `UnifiedAnalysis` result. There is no additional filtering in either code path:

- **TUI mode**: Interactive results explorer (`ResultsExplorer`) displays `analysis.items`
- **Non-TUI mode**: Traditional output (`output_unified_priorities_with_config`) displays `analysis.items`

This architecture guarantees that users see identical item lists regardless of output mode.

#### Empty Results Handling

When filtering removes all items, the system provides helpful feedback:

```
No technical debt items found matching current thresholds.
Try adjusting filters:
  - Use --min-score <value> to lower the score threshold
  - Current min_score threshold: 3.0 (default)
  - Use DEBTMAP_MIN_SCORE_THRESHOLD=0 to see all items
```

This guides users to adjust thresholds when their codebase doesn't have high-scoring debt items.

## Extension Points

### Extending Rust Analysis
1. Extend `RustAnalyzer` with new patterns or metrics
2. Leverage syn's AST capabilities for deeper analysis
3. Add Rust-specific complexity patterns or debt detectors
4. Integrate new analysis passes into the pipeline

### Custom Metrics
1. Extend `FunctionMetrics` or `FileMetrics`
2. Add calculation in analyzer implementation
3. Update aggregation logic
4. Modify weight configuration

### Analysis Plugins
1. Implement analysis phase interface
2. Register in unified analysis pipeline
3. Ensure thread-safety for parallel execution
4. Add configuration options

## Testing Strategy

### Unit Tests
- Individual component testing
- Mock dependencies for isolation
- Property-based testing for algorithms

### Integration Tests
- End-to-end analysis validation
- Performance regression tests
- Parallel vs sequential consistency checks

### Benchmarks
- Micro-benchmarks for critical paths
- Macro-benchmarks on real codebases
- Performance comparison suite

## Future Enhancements

### Planned Optimizations
- Incremental analysis with file watching
- Distributed analysis across machines
- GPU acceleration for graph algorithms
- Advanced caching strategies

### Scalability Improvements
- Streaming parser for huge files
- Database backend for enterprise scale
- Cloud-native deployment options
- Real-time analysis integration

## Module Dependency Graph and Dependency Injection

### Module Structure
The codebase follows a layered architecture with dependency injection for reduced coupling:

```mermaid
graph TD
    %% Core Layer
    subgraph "Core Layer"
        core_types[core::types]
        core_traits[core::traits]
        core_cache[core::cache]
        core_injection[core::injection]
        core_adapters[core::adapters]
    end

    %% Analyzer Layer
    subgraph "Analyzer Layer"
        analyzers[analyzers]
        rust_analyzer[analyzers::rust]
        python_analyzer[analyzers::python]
        js_analyzer[analyzers::javascript]
        implementations[analyzers::implementations]
    end

    %% Dependencies
    core_adapters --> core_traits
    core_adapters --> core_cache
    core_injection --> core_traits

    implementations --> rust_analyzer
    implementations --> python_analyzer
    implementations --> js_analyzer
```

### Dependency Injection Architecture

#### Container Pattern
The `AppContainer` in `core::injection` provides centralized dependency management:
- All analyzers created through factories
- Dependencies injected at construction
- Trait boundaries for loose coupling

#### Factory Pattern
`AnalyzerFactory` creates language-specific analyzers:
- `create_rust_analyzer()` - Returns boxed trait object
- `create_python_analyzer()` - Returns boxed trait object
- `create_javascript_analyzer()` - Returns boxed trait object
- `create_typescript_analyzer()` - Returns boxed trait object

#### Adapter Pattern
`CacheAdapter` wraps the concrete `AnalysisCache`:
- Implements generic `Cache` trait
- Provides abstraction boundary
- Enables testing with mock caches

### Module Coupling Improvements
After implementing dependency injection:
- **Direct dependencies reduced by ~40%** through trait boundaries
- **Circular dependencies eliminated** via proper layering
- **Interface segregation** - modules depend only on required traits
- **Dependency inversion** - high-level modules independent of low-level details

## Scoring Architecture

### Unified Scoring Model

DebtMap uses a sophisticated scoring system to prioritize technical debt items based on multiple factors:

#### Base Score Calculation

The base score uses a **weighted sum model** that combines three primary factors:

- **Coverage Factor (40% weight)**: Measures test coverage gaps
- **Complexity Factor (40% weight)**: Assesses code complexity
- **Dependency Factor (20% weight)**: Evaluates impact based on call graph position

**Formula**:
```
base_score = (coverage_score × 0.4) + (complexity_score × 0.4) + (dependency_score × 0.2)
```

#### Two-Stage Role Adjustment Mechanism

DebtMap employs a two-stage role adjustment mechanism to accurately score functions based on their architectural role and testing expectations. This prevents false positives (e.g., entry points flagged for low unit test coverage) while still accounting for role-based importance.

**Stage 1: Role-Based Coverage Weighting**

**Design Decision**: Not all functions need the same level of unit test coverage. Entry points (CLI handlers, HTTP routes, main functions) are typically integration tested rather than unit tested, while pure business logic should have comprehensive unit tests.

**Implementation**: Role-based coverage weights adjust the coverage penalty based on function role:

```rust
// From unified_scorer.rs:236
let adjusted_coverage_pct = 1.0 - ((1.0 - coverage_pct) * coverage_weight_multiplier);
```

**Default Weights** (configurable in `.debtmap.toml` under `[scoring.role_coverage_weights]`):

| Function Role    | Coverage Weight | Rationale                                    |
|------------------|-----------------|----------------------------------------------|
| Entry Point      | 0.6             | Integration tested, orchestrates other code  |
| Orchestrator     | 0.8             | Coordinates logic, partially integration tested |
| Pure Logic       | 1.2             | Should be thoroughly unit tested             |
| I/O Wrapper      | 0.7             | Often tested via integration tests           |
| Pattern Match    | 1.0             | Standard weight                              |
| Unknown          | 1.0             | Default weight                               |

**Example**: An entry point with 0% coverage receives `1.0 - ((1.0 - 0.0) × 0.6) = 0.4` adjusted coverage (40% penalty reduction), while a pure logic function with 0% coverage gets the full penalty.

**Benefits**:
- Prevents entry points from dominating priority lists due to low unit test coverage
- Focuses testing efforts on pure business logic where unit tests provide most value
- Recognizes different testing strategies (unit vs integration) as equally valid

**Stage 2: Role Multiplier**

A role-based multiplier is applied to the final score to reflect function importance and architectural significance:

```rust
// From unified_scorer.rs:261-262
let clamped_role_multiplier = role_multiplier.clamp(clamp_min, clamp_max);
let role_adjusted_score = base_score * clamped_role_multiplier;
```

**Configuration** (`.debtmap.toml` under `[scoring.role_multiplier]`):

```toml
[scoring.role_multiplier]
clamp_min = 0.3           # Minimum multiplier (default: 0.3)
clamp_max = 1.8           # Maximum multiplier (default: 1.8)
enable_clamping = true    # Enable clamping (default: true)
```

**Clamp Range Rationale**:
- **Default [0.3, 1.8]**: Allows significant differentiation without extreme swings
- **Lower bound (0.3)**: Prevents I/O wrappers from becoming invisible (minimum 30% of base score)
- **Upper bound (1.8)**: Prevents critical entry points from overwhelming other issues (maximum 180% of base score)
- **Configurable**: Projects can adjust range based on their priorities

**When to Disable Clamping**:
- **Prototyping**: Testing extreme multiplier values for custom scoring strategies
- **Special cases**: Very specific project needs requiring wide multiplier ranges
- **Not recommended** for production use as it can lead to unstable prioritization

**Key Distinction: Two-Stage Approach**

The separation of coverage weight adjustment and role multiplier ensures they work together without interfering:

1. **Coverage weight** (Stage 1, applied early): Adjusts coverage expectations by role
   - Modifies how much coverage gaps penalize different function types
   - Pure logic gets full coverage penalty (1.2x), entry points get reduced penalty (0.6x)

2. **Role multiplier** (Stage 2, applied late): Small final adjustment for role importance
   - Applied after all other scoring factors are computed
   - Clamped to prevent extreme values (default: [0.3, 1.8])
   - Fine-tunes final priority based on architectural significance

**Example Workflow**:
```
1. Calculate base score from complexity and dependencies
2. Apply coverage weight based on role → adjusted coverage penalty
3. Combine into preliminary score
4. Apply clamped role multiplier → final score
```

This two-stage approach ensures:
- Role-based coverage adjustments don't interfere with the role multiplier
- Both mechanisms contribute independently to the final score
- Clamping prevents extreme multiplier values from distorting priorities

### Multi-Debt Type Accumulation (Spec 228)

DebtMap accumulates multiple independent debt types for a single function, providing comprehensive technical debt assessment.

#### Design Philosophy

Traditional debt classification uses early-return logic, stopping at the first match:
```rust
// Legacy approach (single debt type)
if has_testing_gap() { return TestingGap }
if is_complex() { return ComplexityHotspot }
if is_dead_code() { return DeadCode }
```

Multi-debt accumulation applies all independent checks:
```rust
// Multi-debt approach (accumulates all applicable types)
let debt_types = [
    check_testing_gap(),
    check_complexity_hotspot(),
    check_dead_code(),
].into_iter().flatten().collect()
```

#### Independent Debt Classifications

Three debt types are evaluated independently:

1. **Testing Gaps**: Coverage-based testing debt
   - Low test coverage (< 20% direct coverage)
   - Complex untested code (cyclomatic > 5, coverage < 80%)
   - Independent of complexity or usage

2. **Complexity Hotspots**: Code complexity issues
   - High cyclomatic complexity (> 10)
   - High cognitive complexity (> 15)
   - Independent of coverage or usage

3. **Dead Code**: Unused code detection
   - No incoming calls in call graph
   - Not excluded by framework patterns
   - Independent of complexity or coverage

#### Behavior

Multi-debt accumulation is always enabled. Functions with multiple independent issues will appear once for each debt type detected.

#### Implementation

Located in `src/priority/scoring/classification.rs`:

- `classify_all_debt_types()`: Functional composition of all debt checks
- `classify_debt_type_with_exclusions()`: Public API with env var gate
- Individual predicates: `check_testing_gap_predicate()`, `check_complexity_hotspot_predicate()`, `check_dead_code_with_exclusions_predicate()`

#### Benefits

- **Comprehensive assessment**: No hidden issues due to early-return logic
- **Better prioritization**: Functions with multiple issues get appropriate attention
- **Gradual rollout**: Opt-in flag allows A/B testing and validation
- **Functional purity**: All predicates are pure functions, easily testable

#### Testing

Integration tests in `tests/multi_debt_integration_test.rs` verify:
- Multi-debt accumulation with env var enabled
- Legacy single-debt behavior with env var disabled
- Correct identification of multiple independent debt types
- Environment variable handling ("true" and "1" both enable)
- Configuration flexibility for different project needs

#### Function Role Detection

Function roles are detected automatically through heuristic analysis:

**Entry Point Detection**:
- Name patterns: `main`, `run_*`, `handle_*`, `execute_*`
- Attributes: `#[tokio::main]`, `#[actix_web::main]`, CLI command annotations
- Call graph position: No callers or called only by test harnesses

**Pure Logic Detection**:
- No file I/O operations
- No network calls
- No database access
- Deterministic (no randomness, no system time)
- Returns value without side effects

**Orchestrator Detection**:
- High ratio of function calls to logic statements
- Coordinates multiple sub-operations
- Thin logic wrapper over other functions

**I/O Wrapper Detection**:
- Dominated by I/O operations (file, network, database)
- Thin abstraction over external resources

### Entropy-Based Complexity Adjustment

Debtmap distinguishes between genuinely complex code and pattern-based repetitive code using information theory:

- **Entropy Score**: Measures randomness/diversity in code patterns
- **Pattern Repetition**: Detects repeated structures (e.g., 10 similar match arms)
- **Dampening Factor**: Reduces complexity score for highly repetitive code

This prevents false positives from large but simple pattern-matching code.

## Score-Based Prioritization System (Spec 171)

DebtMap uses a pure score-based ranking system to prioritize technical debt items. This system replaces traditional tier-based ranking (Critical/High/Medium/Low) with continuous numerical scores that provide finer-grained prioritization and better separation between items of different severities.

### Design Philosophy

**Pure Score-Based Ranking**: Items are ranked by their final calculated score without bucketing into discrete priority tiers. This provides:
- **Finer granularity**: Distinguishes between items that would otherwise share the same tier
- **Natural ordering**: Scores reflect actual severity without artificial boundaries
- **Better separation**: High-severity items stand out more clearly from medium-severity ones

**Two-Stage Amplification**: The system uses a two-stage approach to amplify scores for high-severity items:
1. **Exponential scaling** based on pattern type
2. **Risk boosting** based on architectural position

### Exponential Scaling

Exponential scaling amplifies high scores more than low scores, creating better visual separation in the priority list. Unlike linear multipliers, exponential scaling grows the gap between high and low severity items.

**Implementation** (src/priority/scoring/scaling.rs):

```rust
pub struct ScalingConfig {
    pub god_object: ScalingParams,      // Default: exponent 1.4
    pub long_function: ScalingParams,   // Default: exponent 1.3
    pub complex_function: ScalingParams,
    // ... other patterns
}

pub struct ScalingParams {
    pub exponent: f64,          // Exponential scaling factor
    pub min_threshold: f64,     // Minimum score to apply scaling
    pub max_threshold: f64,     // Maximum score to cap at
}

// Scaling formula
scaled_score = base_score.powf(exponent)
```

**Example - God Object Scaling (exponent = 1.4)**:
- Score 10 → 10^1.4 = 25.1 (2.5x amplification)
- Score 50 → 50^1.4 = 279.5 (5.6x amplification)
- Score 100 → 100^1.4 = 1000 (10x amplification)

**Why Exponential vs Linear**:
- Linear multiplier (e.g., 2x): Creates uniform gaps (score 50 becomes 100, score 100 becomes 200)
- Exponential scaling (e.g., ^1.4): Creates growing gaps that make critical issues stand out
- High-severity items get much higher scores, making them impossible to miss
- Low-severity items remain low, preventing clutter at the top

**Pattern-Specific Exponents**:
- **God Objects (1.4)**: Highest amplification - architectural issues deserve top priority
- **Long Functions (1.3)**: High amplification - major refactoring candidates
- **Complex Functions (1.2)**: Moderate amplification - complexity issues
- **Primitive Obsession (1.1)**: Light amplification - design smell but lower urgency

### Risk Boosting

After exponential scaling, risk factors provide additional boosts based on architectural position:

**Risk Multipliers**:
```rust
// Applied multiplicatively to scaled score
let risk_boosted = scaled_score * risk_multiplier;

// Risk factors:
- High dependency count (10+ callers): 1.2x boost
- Entry point (main, CLI handlers): 1.15x boost
- Low test coverage (<30%): 1.1x boost
```

**Rationale**:
- Entry points affect all downstream code - failures cascade
- High-dependency functions are harder to refactor safely
- Untested code is riskier to modify

### Complete Scoring Pipeline

```
1. Base Score Calculation
   ↓ (weighted sum of coverage, complexity, dependencies)
2. Exponential Scaling
   ↓ (pattern-specific exponent applied)
3. Risk Boosting
   ↓ (architectural position multipliers)
4. Final Score
   ↓ (used for ranking without tier bucketing)
5. Sort by Score
   ↓ (descending order for output)
```

### Configuration

Override default scaling parameters in `.debtmap.toml`:

```toml
[priority.scaling.god_object]
exponent = 1.5              # Increase amplification for God Objects
min_threshold = 30.0        # Only scale scores above 30
max_threshold = 500.0       # Cap scaled scores at 500

[priority.scaling.long_function]
exponent = 1.3              # Default amplification
min_threshold = 0.0         # No minimum threshold
max_threshold = 1000.0      # High cap for extreme cases
```

### Benefits

1. **Clear Priority Separation**: Critical items have dramatically higher scores than medium items
2. **No Arbitrary Thresholds**: Score-based ranking eliminates debate about tier boundaries
3. **Natural Clustering**: Similar-severity items cluster together in the ranked list
4. **Actionable Ordering**: Work through the list from top to bottom
5. **Configurable Amplification**: Tune exponents to match project priorities

### Implementation Location

- **Core implementation**: `src/priority/scoring/scaling.rs`
- **Pattern configs**: `src/priority/scoring/mod.rs`
- **Risk boosting**: `src/priority/scoring/risk.rs`
- **Integration**: Applied in `src/priority/prioritizer.rs` before output

### Migration from Tier-Based Ranking

For compatibility with tools expecting Priority enums, scores can be mapped to tiers:
- Score ≥ 200: Critical
- Score ≥ 100: High
- Score ≥ 50: Medium
- Score < 50: Low

However, the primary output uses raw scores for better granularity.

## Test File Detection (Spec 166)

Debtmap automatically identifies test files and test functions across multiple programming languages, enabling context-aware scoring adjustments that reduce false positives from test-specific patterns.

### Multi-Language Detection Strategies

#### Rust Test Detection

**File-Level Detection**:
- Files in `tests/` directory
- Files ending with `_test.rs` or `_tests.rs`
- Modules with `#[cfg(test)]` annotation

**Function-Level Detection**:
- Functions with `#[test]` attribute
- Functions with `#[tokio::test]` or async test attributes
- Functions in modules marked with `#[cfg(test)]`

```rust
#[cfg(test)]
mod tests {
    #[test]  // Detected as test function
    fn test_parse_input() {
        // Test complexity not penalized
    }
}
```

#### Rust Test Detection Extensions

**Beyond Basic Detection** (covered earlier):
- Custom test harnesses using `#[test_case]` or `#[rstest]` attributes
- Property-based tests with `proptest` or `quickcheck` macros
- Benchmark functions with `#[bench]` attribute
- Integration tests in `tests/` directory with complex setup

**Advanced Test Patterns**:
```rust
#[test_case("input1"; "case 1")]
#[test_case("input2"; "case 2")]
fn test_parameterized(input: &str) {  // Detected as test
    // Parameterized tests may have higher complexity
    // due to handling multiple cases
}

#[cfg(test)]
mod tests {
    proptest! {
        #[test]
        fn test_property(value in 0..100) {  // Detected as test
            // Property tests often have complex assertions
        }
    }
}
```

### Context-Aware Scoring Adjustments

When a file or function is identified as a test, debtmap applies these adjustments:

#### 1. Complexity Score Reduction

Test code often requires high cyclomatic complexity to cover edge cases:

```rust
// Test scoring adjustment
baseline_score = cyclomatic * weight + cognitive * weight
test_adjusted_score = baseline_score * 0.6  // 40% reduction
```

**Rationale**: A test function with cyclomatic complexity of 15 (testing many branches) is normal and maintainable, whereas production code with the same complexity indicates refactoring needs.

#### 2. Priority Level Adjustment

Test debt items receive lower priority than production code debt:

```rust
match priority {
    Priority::Critical => Priority::High,    // Downgrade by one level
    Priority::High => Priority::Medium,
    Priority::Medium => Priority::Low,
    Priority::Low => Priority::Low,          // Floor at Low
}
```

**Rationale**: Fixing high-complexity production code has greater immediate impact on system maintainability than refactoring test code.

#### 3. Coverage Expectation Changes

Test files themselves don't need test coverage:

```rust
if file_context.is_test_file {
    skip_coverage_analysis = true;  // Tests don't test tests
}
```

**Rationale**: Expecting tests to be covered by other tests creates infinite regression and provides minimal value.

#### 4. Test-Specific Recommendations

Instead of generic refactoring advice, test files receive test-specific guidance:

**Production Code Recommendation**:
```
ACTION: Extract complex branches into focused functions
WHY: High cyclomatic complexity (15) makes code hard to understand
```

**Test Code Recommendation**:
```
ACTION: Extract test helper functions for reusable setup
WHY: Test complexity (15) is acceptable, but helpers improve maintainability
```

### Implementation Details

#### FileContext Storage

Test detection results are stored in `FileContext` for efficient reuse:

```rust
pub struct FileContext {
    pub path: PathBuf,
    pub is_test_file: bool,           // File-level test detection
    pub test_functions: HashSet<String>,  // Function-level test detection
    pub language: Language,
}
```

Stored at `AnalysisResults.file_contexts` for cross-module access.

#### Detection Flow

```
1. Parse file → Extract AST
2. Language-specific detection:
   - Check file path patterns
   - Check imports/attributes
   - Identify test functions
3. Store in FileContext
4. Apply scoring adjustments when generating debt items
```

#### Performance Considerations

- **File-level caching**: Test status cached per file, not re-detected
- **Lazy evaluation**: Only detect test context when scoring debt
- **Parallel processing**: Test detection runs in parallel during file analysis

### Benefits

1. **Fewer False Positives**: Test complexity doesn't dominate production priorities
2. **Better Recommendations**: Test-specific refactoring guidance
3. **Language Consistency**: Works uniformly across Rust, Python, JavaScript, TypeScript
4. **Zero Configuration**: Automatic detection using standard conventions
5. **Performance**: Minimal overhead (<2% analysis time increase)

### Configuration

Override default test detection patterns in `.debtmap.toml`:

```toml
[test_detection]
# Additional file patterns for custom test conventions
rust_test_patterns = ["*_spec.rs", "spec_*.rs"]
python_test_patterns = ["test*.py", "*test.py"]
js_test_patterns = ["*.test.jsx", "*.spec.tsx"]

# Scoring adjustment factors
complexity_reduction = 0.6  # Reduce complexity score by 40%
priority_downgrade = true   # Lower priority for test debt
skip_coverage = true        # Don't expect coverage for test files
```

## State Field Detection (Spec 202)

Debtmap identifies state-related fields in functions to detect state machine and coordinator patterns with higher accuracy. The enhanced state field detection uses multiple strategies to reduce false negatives when analyzing non-standard naming conventions.

### Multi-Strategy Detection

State field detection combines three complementary strategies:

#### 1. Keyword-Based Detection (Baseline)

Direct matching against known state-related terms:

**Primary Keywords**:
- Core state terms: `state`, `status`, `mode`, `phase`, `stage`
- State machine terms: `fsm`, `transition`, `lifecycle`
- Context terms: `ctx`, `context`

**Compound Patterns**:
- `state_machine`, `flow_control`, `lifecycle_phase`
- `connection_state`, `request_status`, `task_mode`

```rust
// Detected by keyword matching
self.state           // ✓ Direct keyword
self.status          // ✓ Direct keyword
self.fsm             // ✓ FSM abbreviation
self.lifecycle_phase // ✓ Compound pattern
```

#### 2. Semantic Pattern Recognition

Detects state fields through semantic naming patterns:

**Prefix Patterns**:
- `current_*` - Indicates current value in sequence (e.g., `current_action`)
- `next_*` - Indicates upcoming value (e.g., `next_step`)
- `active_*` - Indicates active selection (e.g., `active_process`)

**Suffix Patterns**:
- `*_type` - Type discrimination (e.g., `connection_type`)
- `*_kind` - Variant selection (e.g., `operation_kind`)
- `*_stage` - Phase indicator (e.g., `request_stage`)

```rust
// Detected by semantic patterns
self.current_action    // ✓ current_ prefix
self.next_step         // ✓ next_ prefix
self.connection_type   // ✓ _type suffix
self.operation_kind    // ✓ _kind suffix
```

#### 3. Type-Based Analysis

Examines field types to identify state-related structures:

**Enum Detection**:
- Enums with ≥3 variants likely represent state
- Enum names ending in "State", "Status", "Mode"
- Enum variant names suggesting transitions

```rust
// Type analysis detects state fields
enum ConnectionState {
    Idle, Connecting, Connected, Disconnected
}

struct Handler {
    connection: ConnectionState,  // ✓ Detected via type analysis
}
```

**Type Patterns**:
- `Option<T>` for optional states
- `Result<T, E>` for fallible state
- Enums with lifecycle-related variants

### Confidence Scoring

Each strategy contributes to an overall confidence score:

```rust
total_confidence = keyword_score      // 0.0 - 0.5
                 + pattern_score      // 0.0 - 0.3
                 + type_score         // 0.0 - 0.4
                 + frequency_score    // 0.0 - 0.2

// Classification thresholds
High:   confidence >= 0.7  // Strong evidence
Medium: confidence >= 0.4  // Multiple weak signals
Low:    confidence <  0.4  // Insufficient evidence
```

**Example Scoring**:
```rust
self.fsm_state
  → keyword_score = 0.5    (compound pattern "fsm_state")
  → pattern_score = 0.3    (_state suffix)
  → type_score = 0.0       (no type info available)
  → frequency_score = 0.0  (first occurrence)
  → total = 0.8 → HIGH confidence
```

### Configuration

Customize state detection in `.debtmap.toml`:

```toml
[state_detection]
# Enable/disable detection strategies
use_type_analysis = true           # Analyze field types
use_frequency_analysis = true      # Track usage patterns
use_pattern_recognition = true     # Apply semantic patterns

# Threshold for enum state detection
min_enum_variants = 3              # Enums with ≥3 variants

# Add domain-specific keywords
custom_keywords = ["workflow", "step", "scenario"]

# Add domain-specific compound patterns
custom_patterns = ["active_workflow", "current_scenario"]
```

**Example with Custom Keywords**:
```rust
struct WorkflowEngine {
    workflow: WorkflowState,     // ✓ Detected via custom keyword
    current_scenario: Scenario,  // ✓ Detected via custom pattern
    step: usize,                 // ✓ Detected via custom keyword
}
```

### Performance Characteristics

**Overhead**: < 5ms per-function for state detection
**Accuracy**: ≥40% reduction in false negatives vs baseline keyword-only detection

Benchmarks validate performance requirements:
```
$ cargo bench --bench state_field_detection_bench
baseline_keyword_detection          time: 12.3 μs
enhanced_multi_strategy_detection   time: 18.7 μs  (+52% overhead, well within 5ms target)
single_field_detection              time: 0.89 μs  (individual field)
```

### Integration with Pattern Detection

State field detection powers higher-level pattern recognition:

#### State Machine Detection

Functions with multiple state field accesses suggest state machine behavior:

```rust
fn handle_request(&mut self, req: Request) -> Response {
    match self.state {                    // State field access #1
        State::Idle => {
            self.state = State::Processing;  // State transition
            self.process(req)
        }
        State::Processing => {
            if self.status.is_ready() {      // State field access #2
                self.finalize()
            }
        }
    }
}
// ✓ Detected as state machine (multiple state fields + transitions)
```

#### Coordinator Detection

Functions accessing multiple state fields from different objects:

```rust
fn orchestrate(&self) -> Result<()> {
    if self.db.status.is_connected()           // External state #1
        && self.cache.state == CacheState::Ready  // External state #2
        && self.mode == Mode::Active {         // Internal state #3
        self.execute_workflow()
    }
}
// ✓ Detected as coordinator (accesses multiple external states)
```

### Implementation Location

- **Core detection**: `src/analyzers/state_field_detector.rs`
- **Pattern integration**: `src/analyzers/state_machine_pattern_detector.rs`
- **Config loading**: `src/config/accessors.rs::get_state_detection_config()`
- **Benchmarks**: `benches/state_field_detection_bench.rs`

### Validation

False negative reduction validated through test corpus:

```rust
// Test corpus of non-standard state fields
test_cases = [
    "current_action",     // Semantic prefix
    "connection_type",    // Semantic suffix
    "operation_kind",     // Semantic suffix
    "fsm_state",          // Compound pattern
    "flow_control",       // Compound pattern
    "ctx",                // Context abbreviation
    // ... 12 total test cases
]

// Validation results (from test suite)
Baseline detected:  3/12 (25.0%)  → 9 false negatives
Enhanced detected:  8/12 (66.7%)  → 4 false negatives
Reduction: 55.6% (exceeds 40% requirement)
```

## God Object Detection

### Understanding God Object vs God Module Detection

Debtmap distinguishes between two fundamentally different organizational problems that both manifest as large files:

#### GOD OBJECT: A Struct/Class with Too Many Methods

**Definition**: A single struct or class that has accumulated too many methods and too many fields, violating the Single Responsibility Principle.

**Classification Criteria**:
- More than 20 methods on a single struct/class
- More than 5 fields in the struct/class
- Methods operate on shared mutable state (the fields)

**Example (Rust)**:
```rust
// GOD OBJECT detected
pub struct MassiveController {
    // 8 fields
    db_connection: DbPool,
    cache: Cache,
    logger: Logger,
    config: Config,
    session: Session,
    auth: AuthService,
    metrics: Metrics,
    queue: MessageQueue,
}

impl MassiveController {
    // 50 methods operating on the fields above
    pub fn handle_user_login(&mut self, ...) { ... }
    pub fn validate_session(&self, ...) { ... }
    pub fn update_cache(&mut self, ...) { ... }
    pub fn send_notification(&self, ...) { ... }
    // ... 46 more methods
}
```

**Why It's Problematic**:
- Violates Single Responsibility Principle (one class doing too much)
- Methods share mutable state (fields), creating tight coupling
- Hard to test in isolation (need to mock all dependencies)
- Changes to one responsibility affect the entire class
- Difficult to refactor without breaking many dependents

**Recommended Fix**:
- Extract logical groups of methods into separate structs
- Move related fields to the new structs
- Use composition instead of putting everything in one class
- Apply the Single Responsibility Principle

**Example Refactoring**:
```rust
// Split into focused components
pub struct AuthController {
    auth: AuthService,
    session: Session,
}

pub struct CacheController {
    cache: Cache,
    db_connection: DbPool,
}

pub struct NotificationController {
    queue: MessageQueue,
    logger: Logger,
}
```

#### GOD MODULE: A File with Too Many Diverse Functions

**Definition**: A module (file) containing many top-level functions that don't share state but represent diverse, unrelated responsibilities.

**Classification Criteria**:
- More than 20 module-level functions
- Does NOT meet GOD OBJECT criteria (no single struct with >20 methods AND >5 fields)
- Functions serve diverse purposes (not cohesive)

**Example (Rust)**:
```rust
// GOD MODULE detected: utils.rs
// 50 diverse module-level functions, no dominant struct

pub fn parse_json(input: &str) -> Result<Value> { ... }
pub fn validate_email(email: &str) -> bool { ... }
pub fn format_currency(amount: f64) -> String { ... }
pub fn hash_password(password: &str) -> String { ... }
pub fn send_http_request(url: &str) -> Result<Response> { ... }
pub fn compress_data(data: &[u8]) -> Vec<u8> { ... }
// ... 44 more unrelated utility functions
```

**Why It's Problematic**:
- Lacks cohesion (functions serve unrelated purposes)
- Hard to navigate and understand module purpose
- Violates module-level Single Responsibility Principle
- Encourages "dumping ground" for miscellaneous functions
- Changes to one function may require rebuilding entire module

**Recommended Fix**:
- Group related functions into focused modules
- Create domain-specific utility modules
- Use submodules to organize by feature/domain

**Example Refactoring**:
```rust
// Split into cohesive modules
// src/parsing.rs
pub fn parse_json(input: &str) -> Result<Value> { ... }
pub fn parse_xml(input: &str) -> Result<Document> { ... }

// src/validation.rs
pub fn validate_email(email: &str) -> bool { ... }
pub fn validate_url(url: &str) -> bool { ... }

// src/formatting.rs
pub fn format_currency(amount: f64) -> String { ... }
pub fn format_date(date: DateTime) -> String { ... }

// src/crypto.rs
pub fn hash_password(password: &str) -> String { ... }
pub fn verify_hash(password: &str, hash: &str) -> bool { ... }
```

#### Key Distinction Summary

| Aspect | GOD OBJECT | GOD MODULE |
|--------|-----------|-----------|
| **Structure** | One struct/class with many methods | Many module-level functions |
| **State** | Methods share mutable state (fields) | Functions are independent, no shared state |
| **Threshold** | >20 methods AND >5 fields on one struct | >20 module-level functions, NOT a god object |
| **Detection** | Count methods per struct + field count | Count total functions in file |
| **Problem Type** | Object-oriented design issue | Module organization issue |
| **Fix Strategy** | Extract classes, apply SRP | Split into cohesive modules |

#### How Debtmap Classifies Files

Debtmap uses a priority-based classification algorithm:

1. **Check for GOD OBJECT first**:
   - Find the largest struct/class in the file
   - If it has >20 methods AND >5 fields → classify as **GOD OBJECT**
   - Output shows: "GOD OBJECT: MyStruct (50 methods, 8 fields)"

2. **If not a GOD OBJECT, check for GOD MODULE**:
   - Count total module-level functions (excluding test functions)
   - If >20 functions → classify as **GOD MODULE**
   - Output shows: "GOD MODULE (50 module functions)"

3. **Otherwise**:
   - File is not classified as either pattern

#### Output Examples

**GOD OBJECT Detection**:
```
#3 SCORE: 7.5 [HIGH]
├─ GOD OBJECT: src/controller.rs
├─ TYPE: UserController (52 methods, 8 fields)
├─ ACTION: Extract responsibilities into focused classes
├─ WHY: Single class with too many methods and fields
└─ Methods: handle_user_login, validate_session, update_cache, ... (52 total)
```

**GOD MODULE Detection**:
```
#5 SCORE: 6.8 [HIGH]
├─ GOD MODULE: src/utils.rs
├─ TYPE: Module with 47 diverse functions
├─ ACTION: Split into cohesive submodules by domain
├─ WHY: Module lacks focus, contains unrelated utilities
└─ Module Functions: parse_json, validate_email, format_currency, ... (47 total)
```

#### Implementation Details

**Module Structure** (Spec 181i - Functional Refactoring):

The god object detection system has been refactored into a modular, functional architecture. All components are organized under `src/organization/god_object/`:

```
src/organization/god_object/
├── mod.rs              # Public API and module coordination
├── types.rs            # Core data structures (GodObjectAnalysis, StructMetrics, etc.)
├── thresholds.rs       # Configuration and threshold definitions
├── predicates.rs       # Pure predicate functions for classification logic
├── scoring.rs          # Pure scoring calculations (god object score, ratios)
├── classifier.rs       # Domain classification and responsibility grouping
├── recommender.rs      # Recommendation generation (module splits, refactoring advice)
├── detector.rs         # Orchestration layer (coordinates pure functions)
└── ast_visitor.rs      # I/O shell (AST traversal and data extraction)
```

**Design Principles**:
- **Pure Functions**: All business logic is implemented as pure, testable functions
- **Separation of Concerns**: I/O (AST visiting) is separated from computation (scoring, classification)
- **Immutable Data Flow**: Data structures are transformed through functional pipelines
- **Type Safety**: Strong typing ensures correctness at compile time
- **Composability**: Small, focused functions compose into complex analysis

**Key Components**:

1. **types.rs**: Core data structures representing analysis results
   - `GodObjectAnalysis`: Complete analysis result
   - `StructMetrics`: Metrics for individual structs
   - `ModuleSplit`: Recommended module split structure

2. **predicates.rs**: Boolean classification functions
   - `is_god_object()`: Determines if metrics exceed thresholds
   - `is_high_priority()`: Priority classification
   - Pure functions returning `bool` based on metrics

3. **scoring.rs**: Numeric scoring calculations
   - `calculate_god_object_score()`: Main scoring algorithm
   - `calculate_struct_ratio()`: Ratio calculations
   - Pure functions returning scores/ratios

4. **classifier.rs**: Domain and responsibility classification
   - `classify_struct_domain()`: Domain classification logic
   - `group_methods_by_responsibility()`: Method grouping
   - Pure functions operating on method/field data

5. **recommender.rs**: Recommendation generation
   - `recommend_module_splits()`: Generate split recommendations
   - `suggest_module_splits_by_domain()`: Domain-based splitting
   - Pure functions producing actionable advice

6. **detector.rs**: Orchestration layer
   - Coordinates calls to pure functions
   - Manages analysis workflow
   - Minimal logic, mostly function composition

7. **ast_visitor.rs**: I/O boundary
   - Traverses Rust AST using `syn` crate
   - Extracts data into pure data structures
   - Only component with side effects

**Historical Note**: Previously implemented in monolithic files (`god_object_detector.rs`, `god_object_analysis.rs`) with mixed concerns. Refactored in spec 181i to achieve functional purity and improved testability.

**Classification Logic**:
```rust
// Simplified algorithm
fn classify_file(file: &FileMetrics) -> Classification {
    // Priority 1: Check for god objects
    for struct_info in &file.structs {
        if struct_info.methods.len() > 20 && struct_info.fields.len() > 5 {
            return Classification::GodObject {
                struct_name: struct_info.name,
                method_count: struct_info.methods.len(),
                field_count: struct_info.fields.len(),
            };
        }
    }

    // Priority 2: Check for god module
    let module_functions = file.functions.iter()
        .filter(|f| !f.is_test && !f.is_method)
        .count();

    if module_functions > 20 {
        return Classification::GodModule {
            function_count: module_functions,
        };
    }

    Classification::Normal
}
```

**Verbose Output**:
When running with `--verbose`, debtmap shows the classification decision process:

```
Analyzing: src/processor.rs
  Checking for GOD OBJECT...
    Largest struct: DataProcessor (12 methods, 4 fields) - below threshold
  Checking for GOD MODULE...
    Module functions: 35 (threshold: 20) - GOD MODULE detected
  Classification: GOD MODULE
```

### Git Context Analysis for God Objects (Spec 248)

God objects can be enriched with file-level git context to provide historical risk assessment. This feature analyzes the git history of files containing god objects to surface patterns like high churn, bug-proneness, and multi-author complexity.

#### File-Level Git Context Approach

**Key Design Decision**: Git context is analyzed at the **file level** for god objects, not at the member (method/field) level.

**Rationale**:
1. **God objects ARE files**: When a struct dominates a file with >20 methods and >5 fields, the file and the god object are effectively the same unit of analysis
2. **Aggregation is unnecessary**: For god objects, file-level metrics directly represent the god object's risk profile
3. **Simplicity**: Direct file analysis avoids complex member-level git blame and aggregation logic
4. **Accuracy**: File-level metrics (commits, authors, age) meaningfully describe god object evolution

**Data Flow**:
```
1. God Object Detection
   └─> Identify file containing god object (e.g., src/controller.rs)

2. File-Level Git Context Analysis
   └─> analyze_file_git_context(file_path, risk_analyzer, project_root)
       └─> Returns ContextualRisk with git_history metrics

3. Attach to God Object
   └─> UnifiedDebtItem.contextual_risk = Some(contextual_risk)

4. Display in TUI
   └─> Show git metrics in god object detail view
```

**Implementation Location**: `src/builders/unified_analysis.rs:1707`

**Key Function**:
```rust
pub fn analyze_file_git_context(
    file_path: &Path,
    risk_analyzer: &risk::RiskAnalyzer,
    project_root: &Path,
) -> Option<risk::context::ContextualRisk>
```

This pure function:
- Takes a file path and risk analyzer with git context provider
- Returns `None` if git context is disabled or unavailable
- Returns `Some(ContextualRisk)` with git history metrics when available
- Is called during god object aggregation to enrich items with contextual risk

#### Git Metrics for God Objects

When git context is enabled (`--enable-context`), god objects display the following file-level metrics:

| Metric | Description | Risk Indication |
|--------|-------------|-----------------|
| **change_frequency** | Commits per day over file lifetime | High frequency = active pain point |
| **bug_density** | Ratio of bug-fix commits to total commits | High density = error-prone code |
| **author_count** | Number of distinct authors | High count = complex ownership |
| **age_days** | Days since file creation | Young + complex = rapid growth issues |

**Example Output** (with git context):
```
#3 SCORE: 7.5 [HIGH]
├─ GOD OBJECT: src/controller.rs
├─ TYPE: UserController (52 methods, 8 fields)
├─ Git Context:
│  ├─ Change Frequency: 2.5 commits/day (HIGH CHURN)
│  ├─ Bug Density: 0.35 (35% of commits fix bugs)
│  ├─ Authors: 8 contributors
│  └─ Age: 180 days (active development)
├─ ACTION: Extract responsibilities into focused classes
└─ WHY: Single class with too many methods, high churn indicates pain
```

#### Integration with Risk Scoring

File-level git context contributes to the overall risk score through the `contextual_risk` field:
- **Risk boost**: High change frequency or bug density can increase priority
- **Context type**: `git_history` context type indicates historical risk
- **Confidence**: Git metrics have high confidence when sufficient history exists (>10 commits)

This contextual information helps prioritize god objects that are not just large, but also actively causing problems as evidenced by their git history.

### Semantic Module Naming (Spec 191)

When splitting god objects, debtmap automatically generates descriptive, meaningful module names based on the methods in each split. This feature ensures that refactored code has clear, domain-appropriate naming without manual intervention.

**Location**: `src/organization/semantic_naming/`

**Design Goal**: Eliminate generic names like `utils`, `misc`, `helpers` and generate specific, confidence-scored names that reflect the actual responsibilities of each split.

#### Architecture

The semantic naming system uses a multi-strategy pipeline:

```rust
pub struct SemanticNameGenerator {
    domain_extractor: DomainTermExtractor,      // Strategy 1
    pattern_recognizer: PatternRecognizer,      // Strategy 2
    specificity_scorer: SpecificityScorer,      // Validation
    uniqueness_validator: NameUniquenessValidator, // Uniqueness
}
```

#### Naming Strategies

**1. Domain Term Extraction** (`domain_extractor.rs`):
- Tokenizes method names (handles snake_case, camelCase, PascalCase, and mixed)
- Counts term frequencies across all methods in a split
- Identifies dominant domain terms (appear in >30% of methods)
- Generates verb-noun pairs when appropriate (e.g., "format_coverage")

Example:
```rust
Methods: ["format_coverage_status", "format_coverage_factor", "calculate_coverage"]
Tokens: ["format", "coverage", "status", "factor", "calculate", "coverage"]
Dominant term: "coverage" (frequency: 0.67)
Generated name: "coverage" (confidence: 0.85)
```

**2. Behavioral Pattern Recognition** (`pattern_recognizer.rs`):
- Recognizes common software patterns across methods
- Patterns: formatting, validation, parsing, computation, transformation, serialization, persistence, events, lifecycle
- Uses verb detection with word boundary awareness (avoids false matches like "formatting" matching "format")
- Requires 60% of methods to match pattern for confidence

Supported patterns:
- **Formatting**: format, display, render, print, show
- **Validation**: validate, verify, check, ensure, assert
- **Parsing**: parse, extract, read, decode, interpret
- **Computation**: calculate, compute, evaluate, measure, analyze
- **Transformation**: convert, transform, map, translate
- **Serialization**: serialize, deserialize, encode, decode
- **Persistence**: save, load, store, fetch, retrieve
- **Events**: handle, process, dispatch, trigger, emit
- **Lifecycle**: initialize, setup, teardown, cleanup, destroy

**3. Specificity Scoring** (`specificity_scorer.rs`):
- Evaluates name quality on scale of 0.0 (generic) to 1.0 (highly specific)
- Rejects generic terms: "unknown", "misc", "utils", "helpers", "data", "types"
- Bonuses for:
  - Domain-specific terms (+0.12-0.15)
  - Compound names with underscore (+0.10)
  - Specific action verbs (+0.10)
  - Longer descriptive names (+0.04-0.06)
- Penalties for:
  - Very short names (-0.15)
  - Containing generic terms (-0.10)
  - Fallback names (set to 0.4)

Score thresholds:
- `>= 0.85`: Excellent
- `>= 0.60`: Good
- `>= 0.40`: Acceptable
- `< 0.40`: Rejected (try alternative)

**4. Uniqueness Validation** (`uniqueness_validator.rs`):
- Tracks used names per directory to prevent collisions
- Disambiguates conflicts by appending suffix (e.g., "validation_2")
- Tries alternative candidates before falling back to numbered suffixes
- Clears validation state per directory for independent namespacing

#### Name Generation Flow

```rust
pub fn generate_names(
    &self,
    methods: &[String],
    responsibility: Option<&str>,
) -> Vec<NameCandidate> {
    let mut candidates = Vec::new();

    // Strategy 1: Extract from method names
    if let Some(name) = self.domain_extractor.generate_domain_name(methods) {
        if self.is_valid_candidate(&name) {
            candidates.push(name);
        }
    }

    // Strategy 2: Recognize behavioral pattern
    if let Some(name) = self.pattern_recognizer.recognize_pattern(methods) {
        if self.is_valid_candidate(&name) {
            candidates.push(name);
        }
    }

    // Strategy 3: Extract from responsibility description
    if let Some(resp) = responsibility {
        if let Some(name) = self.domain_extractor.extract_from_description(resp) {
            if self.is_valid_candidate(&name) {
                candidates.push(name);
            }
        }
    }

    // Fallback: Generate descriptive placeholder
    if candidates.is_empty() {
        candidates.push(self.generate_fallback_name(methods));
    }

    // Sort by confidence and return top 3
    candidates.sort_by(|a, b| b.confidence.partial_cmp(&a.confidence).unwrap());
    candidates.truncate(3);
    candidates
}
```

#### Output Format

Each generated name includes:
- **module_name**: Proposed name (without `.rs` extension)
- **confidence**: Score 0.0-1.0 indicating naming confidence
- **specificity_score**: Quality score 0.0-1.0 (rejects generic terms)
- **reasoning**: Human-readable explanation of name derivation
- **strategy**: Which strategy generated the name (DomainTerms, BehavioralPattern, DescriptiveFallback)

Example:
```rust
NameCandidate {
    module_name: "formatting",
    confidence: 0.85,
    specificity_score: 0.72,
    reasoning: "Recognized behavioral pattern: formatting (8/10 methods match)",
    strategy: NamingStrategy::BehavioralPattern,
}
```

#### Performance Impact

- **Target**: <10% overhead on god object analysis
- **Implementation**: O(n) single-pass tokenization and pattern matching
- **No external dependencies**: Pure Rust, no NLP libraries
- **Parallel-safe**: Thread-local validation state per analysis run

#### Testing

**Unit Tests** (`src/organization/semantic_naming/*/tests`):
- Tokenization accuracy (camelCase, snake_case, mixed, acronyms)
- Domain term extraction and frequency analysis
- Pattern recognition with verb boundary detection
- Specificity scoring for various name types
- Uniqueness validation and disambiguation

**Integration Tests** (`tests/semantic_naming_integration_test.rs`):
- No generic names in output (rejects "utils", "misc", etc.)
- Name uniqueness across multiple splits
- High-confidence names for clear patterns
- Real-world method pattern recognition

### Complexity-Weighted Scoring

**Design Problem**: Traditional god object detection relies on raw method counts, which creates false positives for well-refactored code. A file with 100 simple helper functions (complexity 1-3) should not rank higher than a file with 10 highly complex functions (complexity 17+).

**Solution**: DebtMap uses complexity-weighted god object scoring that assigns each function a weight based on its cyclomatic complexity, ensuring that complex functions contribute more to the god object score than simple ones.

#### Weighting Formula

Each function contributes to the god object score based on this formula:

```
weight = (max(1, complexity) / 3)^1.5
```

**Examples**:
- Complexity 1 (simple getter): weight ≈ 0.19
- Complexity 3 (baseline): weight = 1.0
- Complexity 9 (moderate): weight ≈ 5.2
- Complexity 17 (needs refactoring): weight ≈ 13.5
- Complexity 33 (critical): weight ≈ 36.5

**Key Properties**:
- **Non-linear scaling**: Higher complexity functions are weighted disproportionately more
- **Baseline normalization**: Complexity 3 is normalized to weight 1.0 (typical simple function)
- **Power law**: The 1.5 exponent ensures exponential growth for high complexity

#### God Object Score Calculation

The complexity-weighted god object score combines multiple factors:

```rust
weighted_method_count = sum(calculate_complexity_weight(fn.complexity) for fn in functions)
complexity_penalty = if avg_complexity > 10.0 { 1.5 } else if avg_complexity < 3.0 { 0.7 } else { 1.0 }

god_object_score = (
    (weighted_method_count / thresholds.weighted_methods_high) * 40.0 +
    (fields / thresholds.max_fields) * 20.0 +
    (responsibilities / thresholds.max_responsibilities) * 15.0 +
    (lines_of_code / 500) * 25.0
) * complexity_penalty
```

**Threshold**: A file is considered a god object if `god_object_score >= 70.0`

**Benefits**:
- Files with many simple functions score lower than files with fewer complex functions
- Reduces false positives on utility modules with many small helpers
- Focuses refactoring efforts on truly problematic large, complex modules
- Aligns with actual maintainability concerns (complexity matters more than count)

#### Comparison: Raw vs Weighted

**Example**: Comparing two files

| File | Method Count | Avg Complexity | Raw Approach | Weighted Approach |
|------|--------------|----------------|--------------|-------------------|
| shared_cache.rs | 100 | 1.5 | God object (100 methods) | Normal (weighted: 19.0) |
| legacy_parser.rs | 10 | 17.0 | Borderline (10 methods) | God object (weighted: 135.0) |

The weighted approach correctly identifies `legacy_parser.rs` as the real problem despite having fewer methods.

#### Implementation Details

**Location**: `src/organization/complexity_weighting.rs`

**Key Functions**:
- `calculate_complexity_weight(complexity: u32) -> f64`: Pure function to calculate weight for a single function
- `aggregate_weighted_complexity(functions: &[FunctionComplexityInfo]) -> f64`: Sum weights across all non-test functions
- `calculate_avg_complexity(functions: &[FunctionComplexityInfo]) -> f64`: Calculate average complexity for penalty calculation
- `calculate_complexity_penalty(avg_complexity: f64) -> f64`: Apply bonus/penalty based on average complexity

**Integration**: The god object detector in `src/organization/god_object_detector.rs` automatically uses complexity-weighted scoring when cyclomatic complexity data is available, falling back to raw count scoring otherwise.

**Testing**: Comprehensive unit tests validate the weighting formula and ensure that files with many simple functions score significantly lower than files with fewer complex functions.

### Purity-Weighted God Object Scoring

**Design Problem**: Traditional complexity-weighted scoring treats all functions equally regardless of their design quality. A module with 100 pure, composable helper functions (functional programming style) should not be penalized as heavily as a module with 100 stateful, side-effecting functions (procedural style).

**Solution**: DebtMap extends complexity-weighted scoring with purity analysis, applying differential weights to pure vs impure functions. This rewards functional programming patterns while still identifying truly problematic god objects.

#### Purity Analysis Architecture

**Location**: `src/organization/purity_analyzer.rs`

**Analysis Pipeline**:
```
Function AST
Analyze Signature (parameters, return type)
Analyze Body (side effects, mutations, I/O)
Determine Purity Classification
Apply Purity Weight to Complexity Score
```

**Classification Algorithm**:

The purity analyzer examines both function signatures and implementations:

1. **Signature Analysis**:
   - Mutable parameters (`&mut`) → Impure
   - No return value → Likely impure (unless proven otherwise)
   - Return type suggests computation → Potentially pure

2. **Body Analysis** (detects side effects):
   - File I/O operations (`std::fs`, `tokio::fs`)
   - Network calls (`reqwest`, `hyper`, sockets)
   - Database access (SQL, ORM operations)
   - Global state mutation (static mut, unsafe)
   - Logging/printing (`println!`, `log::`)
   - System calls (`std::process`, `Command`)
   - Random number generation
   - Time/clock access

3. **Purity Determination**:
   - **Pure**: No detected side effects, immutable parameters, returns value
   - **Impure**: Any side effect detected or mutable state access

#### Purity Weights

Pure functions receive a reduced weight multiplier:

```rust
// From src/organization/purity_analyzer.rs
const PURE_FUNCTION_WEIGHT: f64 = 0.3;    // 30% weight
const IMPURE_FUNCTION_WEIGHT: f64 = 1.0;  // 100% weight (baseline)
```

**Rationale**:
- **Pure functions** are easier to test, reason about, and maintain
- **Many small pure helpers** indicate good functional decomposition
- **Impure functions** carry inherent complexity beyond their cyclomatic complexity

#### Integration with God Object Detection

The god object detector applies purity weights during weighted complexity calculation:

```rust
// Pseudo-code from god_object_detector.rs
for function in functions {
    complexity_weight = calculate_complexity_weight(function.complexity);
    purity_weight = if is_pure(function) { 0.3 } else { 1.0 };
    total_weighted_complexity += complexity_weight * purity_weight;
}
```

**Combined Weighting**:
- Simple pure function (complexity 1): `0.19 × 0.3 = 0.057`
- Simple impure function (complexity 1): `0.19 × 1.0 = 0.19`
- Complex pure function (complexity 17): `13.5 × 0.3 = 4.05`
- Complex impure function (complexity 17): `13.5 × 1.0 = 13.5`

#### Example Scenario

**Functional Module** (70 pure helpers, 30 impure orchestrators):
```
Pure functions:    70 × avg_weight(2.0) × 0.3 = 42.0
Impure functions:  30 × avg_weight(8.0) × 1.0 = 240.0
Total weighted: 282.0
God object score: ~45.0 (below threshold)
```

**Procedural Module** (100 impure functions):
```
Impure functions:  100 × avg_weight(8.0) × 1.0 = 800.0
Total weighted: 800.0
God object score: ~125.0 (god object detected)
```

The functional module avoids god object classification despite having more total functions, because its pure helpers contribute minimally to the weighted score.

#### Benefits

- **Rewards functional programming**: Modules using functional patterns score lower
- **Penalizes stateful design**: Modules with many side effects score higher
- **Accurate problem detection**: Focuses on truly problematic modules, not well-refactored functional code
- **Encourages refactoring**: Incentivizes extracting pure functions from complex impure ones

#### Verbose Output

When running with `--verbose`, the god object analysis includes purity distribution:

```
GOD OBJECT ANALYSIS: src/core/processor.rs
  Total functions: 107
  PURITY DISTRIBUTION:
    Pure: 70 functions (65%) → complexity weight: 6.3
    Impure: 37 functions (35%) → complexity weight: 14.0
    Total weighted complexity: 20.3
  God object score: 12.0 (threshold: 70.0)
  Status: ✓ Not a god object (functional design)
```

#### Data Flow

The purity analysis integrates into the existing analysis pipeline:

```
File Analysis
Extract Functions
Calculate Cyclomatic Complexity (existing)
[NEW] Perform Purity Analysis
[NEW] Apply Purity Weights
Calculate Weighted Complexity
God Object Detection
Generate Report
```

#### Testing

**Unit Tests** (`src/organization/purity_analyzer.rs`):
- Pure function detection accuracy
- Impure function detection (all side effect types)
- Edge cases (empty functions, trait implementations)

**Integration Tests** (`tests/purity_weighted_god_object.rs`):
- Functional modules score lower than procedural modules
- Purity distribution appears in verbose output
- God object threshold calibration with purity weights

**Property Tests**:
- Purity classification is deterministic
- Pure function weight < Impure function weight (always)
- Total weighted complexity >= raw complexity count

### Inter-Procedural Purity Propagation (Spec 156)

**Problem**: Intrinsic (local) purity analysis misses 40-60% of pure functions that call other pure functions, leading to false negatives and suboptimal refactoring recommendations.

**Solution**: DebtMap implements two-phase purity analysis that propagates purity information through the call graph, achieving <15% false negative rate.

#### Two-Phase Analysis Workflow

**Location**: `src/analysis/purity_propagation/mod.rs`

**Analysis Pipeline**:
```
Phase 1: Intrinsic Analysis
  Function AST → Detect Local Side Effects → PurityResult
Phase 2: Call Graph Propagation
  Build Call Graph → Topological Sort → Bottom-Up Propagation
                                      Updated PurityResult with Confidence
```

**Phase 1: Intrinsic Analysis**

Each function is analyzed in isolation using existing `PurityAnalyzer`:
- Detects I/O operations
- Identifies mutable state access
- Checks for unsafe blocks
- Analyzes FFI calls

Result: Initial `PurityResult { level, confidence, reason }`

**Phase 2: Bottom-Up Propagation**

Functions are analyzed in dependency order (callees before callers):

1. **Topological Sort**: Order functions by call dependencies
2. **Recursive Detection**: Identify and handle recursive cycles
3. **Purity Propagation**:
   - If all dependencies are pure → function is pure (with adjusted confidence)
   - If any dependency is impure → function is impure
   - Unknown dependencies reduce confidence
4. **Confidence Adjustment**: Reduce confidence for:
   - Propagation depth (0.9x per level)
   - Recursive functions (0.7x penalty)
   - Unknown dependencies (0.3 base confidence)

#### Propagation Algorithm

```rust
for each function in topological_order:
    if is_in_cycle(function):
        // Recursive function handling
        if intrinsically_pure(function):
            classify_as(RecursivePure)
            reduce_confidence(0.7)  // 30% penalty
        else:
            classify_as(RecursiveWithSideEffects)
    else:
        deps = get_dependencies(function)
        if all_pure(deps):
            classify_as(PropagatedFromDeps)
            confidence = min(dep_confidences) * 0.9^depth
        else:
            classify_as(Impure)
```

#### Purity Reasons

The `purity_reason` field documents the classification source:

| Reason | Description | Confidence Impact |
|--------|-------------|-------------------|
| `Intrinsic` | No side effects or calls | 1.0 (highest) |
| `PropagatedFromDeps` | All dependencies pure | 0.9^depth |
| `RecursivePure` | Pure structural recursion | 0.7x multiplier |
| `RecursiveWithSideEffects` | Recursive with I/O | 0.95 (high certainty) |
| `SideEffects` | Contains I/O or mutations | 1.0 (certain impurity) |
| `UnknownDeps` | Cannot analyze dependencies | 0.3 (low confidence) |

#### Integration with Analysis Pipeline

The purity propagator is integrated into the unified analysis workflow:

```rust
// In src/builders/unified_analysis.rs

// 1. Build call graph
let call_graph = build_call_graph(metrics);

// 2. Populate call graph data
let enriched_metrics = populate_call_graph_data(metrics, &call_graph);

// 3. Run purity propagation (NEW in spec 156)
let propagated_metrics = run_purity_propagation(&enriched_metrics, &call_graph);

// 4. Continue with unified analysis
create_unified_analysis(&propagated_metrics, &call_graph, ...)
```

#### Caching and Invalidation

**Cache Strategy**:
- Purity results cached per function using `DashMap<FunctionId, PurityResult>`
- Thread-safe concurrent access during parallel analysis
- Cache persists across single analysis run only

**Invalidation**: Cache is cleared when:
- Source files modified (detected by file hash)
- Call graph structure changes
- Analysis restart

#### Scoring Integration

Propagated purity results integrate with the unified scoring system (`src/priority/unified_scorer.rs`):

```rust
fn calculate_purity_adjustment(func: &FunctionMetrics) -> f64 {
    if func.is_pure == Some(true) {
        if func.purity_confidence.unwrap_or(0.0) > 0.8 {
            0.70  // High confidence: 30% complexity reduction
        } else {
            0.85  // Medium confidence: 15% reduction
        }
    } else {
        1.0  // No adjustment for impure functions
    }
}
```

**Impact on Debt Scoring**:
- Pure functions with high complexity become better refactoring targets
- Easier to test (no mocks needed)
- Safer to parallelize
- Lower maintenance burden

#### Cross-File Propagation

Purity propagates across file boundaries automatically:

```rust
// file1.rs
pub fn helper(x: i32) -> i32 {
    x * 2  // Pure: Intrinsic
}

// file2.rs
use file1::helper;

pub fn caller(items: &[i32]) -> Vec<i32> {
    items.iter().map(|x| helper(*x)).collect()
    // Pure: PropagatedFromDeps(depth: 1)
}
```

This enables whole-program purity inference across module boundaries.

#### Testing

**Unit Tests** (`tests/inter_procedural_purity_test.rs`):
- Pure function calling pure function (high confidence maintained)
- Pure recursive functions (confidence reduced)
- Impure recursive functions (classified as impure)
- Confidence decreases with call depth
- Cross-file purity propagation

**Integration Tests**:
- End-to-end propagation in real codebases
- Performance benchmarks for large call graphs
- Cache hit/miss ratios

**Property Tests**:
- Purity propagation is deterministic
- Confidence never increases through propagation
- Recursive purity confidence < non-recursive

#### Performance Characteristics

**Time Complexity**: O(V + E) where V = functions, E = calls
- Topological sort: O(V + E)
- Propagation: O(V) single pass

**Space Complexity**: O(V) for cache storage

**Benchmarks** (on typical Rust project):
- 1000 functions: ~10ms
- 10000 functions: ~100ms
- Negligible overhead vs call graph construction

#### Limitations and Future Work

**Current Limitations**:
- Dynamic dispatch reduces confidence
- Macro-generated code requires special handling
- FFI calls assumed impure (conservative)
- Trait method purity depends on implementations

**Future Enhancements**:
- User-provided purity annotations (`#[pure]`)
- Effect system integration (Rust RFC #2237)
- Better trait method handling
- IDE integration for real-time feedback

### VarId Translation Layer (Spec 247)

**Problem**: CFG-based data flow analysis uses numeric `VarId { name_id: u32, version: u32 }` for efficiency during analysis, but users need human-readable variable names like "buffer", "result", "user_input" in reports.

**Solution**: DebtMap implements a lightweight translation layer that maps VarIds back to variable names with <10% memory overhead, enabling efficient analysis with user-friendly output.

#### Architecture

**Location**: `src/data_flow/mod.rs`

**Core Types**:

```rust
/// CFG-based analysis with variable name translation context
pub struct CfgAnalysisWithContext {
    /// The data flow analysis results (uses VarId internally)
    pub analysis: DataFlowAnalysis,
    /// Variable name mapping (VarId.name_id -> variable name)
    pub var_names: Vec<String>,
}
```

**Design Rationale**:
- **During Analysis**: Use numeric VarIds for efficiency (no string comparisons, compact memory)
- **During Reporting**: Translate VarIds to names on-demand (lazy evaluation)
- **Memory Trade-off**: Small `Vec<String>` overhead vs large `HashMap<VarId, String>`

#### Translation API

**Single Variable Translation**:
```rust
let var_id = VarId { name_id: 0, version: 0 };
let name = ctx.var_name(var_id);  // "buffer"
```

**Batch Translation**:
```rust
let dead_stores = ctx.analysis.liveness.dead_stores.iter().copied();
let names = ctx.var_names_for(dead_stores);  // ["temp", "unused"]
```

**High-Level Translation** (via DataFlowGraph):
```rust
// Translate dead stores
let dead_store_names = graph.get_dead_store_names(&func_id);

// Translate escaping variables
let escaping_names = graph.get_escaping_var_names(&func_id);

// Translate return dependencies
let return_dep_names = graph.get_return_dependency_names(&func_id);

// Translate tainted variables
let tainted_names = graph.get_tainted_var_names(&func_id);
```

#### Memory Overhead Strategy

**Memory Layout**:
- `DataFlowAnalysis`: Uses `VarId` (8 bytes) in all sets and maps
- `Vec<String>`: One entry per unique variable name (typically 10-100 per function)
- Total overhead: `size_of::<String>() * num_vars` ≈ 24 bytes × N

**Optimization Techniques**:
1. **Compact VarId Representation**: `u32` instead of `String` in analysis
2. **Shared Ownership**: `String` in vector, not duplicated per VarId occurrence
3. **Lazy Translation**: Only translate on user-facing operations, not internal analysis
4. **No Reverse Mapping**: No `HashMap<String, VarId>` (only forward translation needed)

**Benchmark Verification**:
See `benches/varid_translation_memory.rs` for memory overhead measurements:
- Baseline: DataFlowAnalysis alone
- With translation: CfgAnalysisWithContext
- Target: <10% overhead (NFR1 from spec 247)

#### Integration with Data Flow Analysis

**Creation Pattern**:
```rust
use debtmap::analysis::data_flow::ControlFlowGraph;

// 1. Build CFG from function AST
let cfg = ControlFlowGraph::from_block(&function_block);

// 2. Extract variable names from CFG
let var_names = cfg.variables.clone();  // Vec<String>

// 3. Run data flow analysis (uses VarId internally)
let analysis = DataFlowAnalysis::analyze(&cfg);

// 4. Create context with translation capability
let ctx = CfgAnalysisWithContext::new(var_names, analysis);

// 5. Store in DataFlowGraph
data_flow_graph.set_cfg_analysis_with_context(func_id, ctx);
```

**Usage in Reports**:
```rust
// Get human-readable names for reporting
let dead_stores = graph.get_dead_store_names(&func_id);
println!("Dead stores: {:?}", dead_stores);  // ["temp", "unused_result"]

let escaping = graph.get_escaping_var_names(&func_id);
println!("Escaping: {:?}", escaping);  // ["result", "error"]
```

#### Translation Guarantees

**Correctness**:
- Valid VarId always maps to a name (may be "unknown_N" for out-of-bounds)
- Translation is deterministic (same VarId → same name)
- Version numbers preserved in analysis, not exposed to users

**Performance**:
- Single translation: O(1) vector lookup
- Batch translation: O(n) where n = number of VarIds
- No allocation overhead (returns borrowed `String`)

**Memory Safety**:
- No VarId lifetime issues (uses `Copy` trait)
- Variable names stored once, referenced many times
- Translation happens on-demand, not eagerly

#### Limitations and Design Decisions

**Current Limitations**:
1. **No reverse translation**: Cannot convert "buffer" back to VarId (not needed)
2. **Version numbers hidden**: Users see "buffer", not "buffer_v2" (simplicity)
3. **Unknown variables**: Out-of-bounds name_id returns "unknown_N" (defensive)

**Design Decisions**:
- **Vec over HashMap**: O(1) indexed access vs O(1) hashed access, simpler memory model
- **Lazy translation**: Translate on reporting, not during analysis (performance)
- **Context wrapper**: Combine analysis + names instead of polluting DataFlowAnalysis
- **Skipped serialization**: Translation context not serialized (ephemeral, can be rebuilt)

**Memory Trade-offs**:
- **Accepted overhead**: Small `Vec<String>` per function (~200-500 bytes typical)
- **Rejected alternative**: Store names in every VarId occurrence (10x memory increase)
- **Rejected alternative**: Global string table (complex lifetime management)

#### Testing

**Unit Tests** (`src/data_flow/mod.rs`):
- `test_varid_translation`: Basic VarId → name translation
- `test_translation_with_missing_id`: Out-of-bounds handling ("unknown_N")
- `test_dead_store_translation`: Dead store name translation
- `test_escaping_var_translation`: Escaping variable translation
- `test_return_dependency_translation`: Return dependency translation
- `test_tainted_var_translation`: Tainted variable translation

**Benchmarks** (`benches/varid_translation_memory.rs`):
- Memory overhead measurements for various function sizes
- Translation performance for different variable counts
- DataFlowGraph integration overhead

**Property Tests** (future):
- Translation determinism (same VarId always → same name)
- Memory overhead < 10% for all realistic input sizes
- No allocation during translation (borrowed strings only)

#### Future Enhancements

**Potential Improvements**:
- String interning for common variable names ("self", "result", "error")
- Compressed variable name storage (prefix compression)
- Optional version number display in verbose mode
- Custom variable name formatters (e.g., "buffer_v2" for SSA debugging)

### God Object Detection - Recommendation Strategy

When a god object or god module is detected, DebtMap provides actionable refactoring recommendations. The recommendation strategy adapts to the file's characteristics to provide the most relevant split suggestions.

#### Analysis Method Selection

DebtMap uses different analysis strategies based on the file's composition:

**Domain-Based Analysis** (for struct-heavy files):
- **Trigger conditions**:
  - Struct count ≥ 5
  - Distinct semantic domains ≥ 3
  - Struct-to-function ratio > 0.3
- **Analysis approach**: Groups structs by semantic domain (e.g., `ScoreConfig`, `ScoreCalculator` → "score" domain)
- **Recommendations**: Suggests domain-specific module splits (e.g., `config/scoring.rs`, `config/thresholds.rs`)
- **Rationale**: Struct-heavy files benefit from semantic grouping rather than responsibility-based splitting

**Responsibility-Based Analysis** (for method-heavy files):
- **Trigger conditions**: Does not meet domain-based criteria (few structs or low ratio)
- **Analysis approach**: Groups functions by inferred responsibility patterns (e.g., parsing, formatting, validation)
- **Recommendations**: Suggests responsibility-based splits (e.g., `parsing.rs`, `formatting.rs`, `validation.rs`)
- **Rationale**: Method-heavy files benefit from separating different functional responsibilities

#### Selection Priority

The recommendation engine applies these strategies in order:

1. **Domain-Based (Primary)**: If struct-heavy conditions are met, use domain analysis
2. **Responsibility-Based (Fallback)**: Otherwise, use responsibility pattern analysis
3. **Hybrid (Optional)**: For files with both characteristics, may provide both types of recommendations

#### Severity Determination

Recommendations are assigned severity levels based on multiple factors:

```rust
fn determine_cross_domain_severity(
    struct_count: usize,
    domain_count: usize,
    lines: usize,
    is_god_object: bool,
) -> RecommendationSeverity {
    // CRITICAL: God object with cross-domain mixing
    if is_god_object && domain_count >= 3 {
        return Critical;
    }

    // CRITICAL: Massive cross-domain mixing
    if struct_count > 15 && domain_count >= 5 {
        return Critical;
    }

    // HIGH: Significant cross-domain issues
    if struct_count >= 10 && domain_count >= 4 {
        return High;
    }

    if lines > 800 && domain_count >= 3 {
        return High;
    }

    // MEDIUM: Proactive improvement opportunity
    if struct_count >= 8 || lines > 400 {
        return Medium;
    }

    // LOW: Informational only
    Low
}
```

**Severity Levels**:
- **Critical**: Immediate action recommended (god object + cross-domain issues)
- **High**: High priority refactoring (significant complexity or size)
- **Medium**: Proactive improvement opportunity (approaching problematic thresholds)
- **Low**: Informational suggestion (minor organizational improvements)

#### Domain Classification

The domain classifier examines struct names to identify semantic domains:

**Common Patterns**:
- Prefixes: `CacheConfig`, `CacheManager` → "cache" domain
- Suffixes: `ScoreCalculator`, `ScoreValidator` → "score" domain
- Base words: `ThresholdConfig`, `ThresholdFactory` → "threshold" domain

**Algorithm**:
```rust
fn classify_struct_domain(name: &str) -> String {
    // Extract domain from camelCase or snake_case names
    // Examples:
    //   "ScoreConfig" → "score"
    //   "ThresholdValidator" → "threshold"
    //   "DetectionEngine" → "detection"
}
```

#### Recommendation Output

Each recommendation includes:

**ModuleSplit Structure**:
- `suggested_name`: Target module path (e.g., `config/scoring.rs`)
- `structs_to_move`: List of structs to relocate to this module
- `methods_to_move`: List of functions to relocate (for responsibility-based)
- `responsibility`: Description of the module's purpose
- `domain`: Semantic domain name (for domain-based splits)
- `rationale`: Explanation of why this split is recommended
- `method`: Analysis method used (`CrossDomain` or `ResponsibilityBased`)
- `severity`: Priority level for this recommendation
- `estimated_lines`: Approximate size of the new module

**Example Output**:
```
GOD OBJECT DETECTED: src/config.rs
  Recommendation: Split by semantic domain (10 structs across 3 domains)
  Severity: High

  Suggested splits:
    1. config/scoring.rs (3 structs: ScoreConfig, ScoreCalculator, ScoreValidator)
       Domain: scoring
       Estimated lines: ~150

    2. config/thresholds.rs (4 structs: ThresholdConfig, ThresholdValidator, ThresholdManager, ThresholdFactory)
       Domain: threshold
       Estimated lines: ~200

    3. config/detection.rs (3 structs: DetectionConfig, DetectionEngine, DetectionResult)
       Domain: detection
       Estimated lines: ~120
```

#### Implementation Details

**Location**: `src/organization/god_object_analysis.rs`

**Key Functions**:
- `count_distinct_domains(structs: &[StructMetrics]) -> usize`: Count unique semantic domains
- `calculate_struct_ratio(struct_count: usize, total_functions: usize) -> f64`: Calculate struct-to-function ratio
- `determine_cross_domain_severity(...)`: Assign severity to recommendations
- `suggest_module_splits_by_domain(structs: &[StructMetrics])`: Generate domain-based split suggestions
- `classify_struct_domain(name: &str) -> String`: Extract semantic domain from struct name

**Integration**:
The recommendation strategy is integrated into `analyze_god_object_with_recommendations()` which:
1. Analyzes file structure (struct count, function count, domains)
2. Selects appropriate analysis method (domain-based vs responsibility-based)
3. Generates recommendations with severity levels
4. Populates `structs_to_move` or `methods_to_move` fields based on strategy

**Testing**:
- Unit tests: `tests::test_count_distinct_domains()`, `test_calculate_struct_ratio()`, `test_determine_cross_domain_severity()`
- Integration tests: `tests/god_object_struct_recommendations.rs`

## Observer Pattern Detection

### Overview

DebtMap includes sophisticated observer pattern detection that identifies event-driven dispatch patterns across the call graph, reducing false positives in dead code detection for event handlers and callbacks.

### Architecture Components

#### Pattern Recognition
- **Observer Registry Detection**: Identifies registration functions that store callbacks/handlers
- **Observer Dispatch Detection**: Detects loops that notify registered observers
- **Call Graph Integration**: Marks detected patterns in the unified call graph

#### Data Flow

```
File Analysis
Extract Functions & Classes
[Pattern Recognition]
Identify Observer Registration Patterns
[Observer Registry]
Build Registry of Observer Collections
[Observer Dispatch Detector]
Detect Dispatch Loops
[Call Graph Integration]
Mark Functions as Dispatchers
Enhanced Call Graph Analysis
```

### Detection Algorithm

#### Phase 1: Observer Registry Detection

Identifies collections that store callbacks:

**Detection Criteria**:
- Collection fields storing function pointers, closures, or trait objects
- Field names matching observer patterns: `listeners`, `handlers`, `observers`, `callbacks`, `subscribers`
- Type patterns: `Vec<Box<dyn Trait>>`, `Vec<Fn(...)>`, `HashMap<K, Vec<Handler>>`

**Example Detected Patterns**:
```rust
// Simple vector of handlers
pub struct EventBus {
    listeners: Vec<Box<dyn EventHandler>>,  // ← Detected
}

// HashMap of event types to handlers
pub struct Dispatcher {
    handlers: HashMap<EventType, Vec<Callback>>,  // ← Detected
}

// Closure storage
pub struct Notifier {
    callbacks: Vec<Box<dyn Fn(&Event)>>,  // ← Detected
}
```

#### Phase 2: Observer Dispatch Detection

Identifies loops that invoke stored callbacks:

**Detection Criteria**:
1. **Loop Pattern**: Function contains `for` loop iterating over observer collection
2. **Collection Reference**: Loop iterates over field from observer registry
3. **Dispatch Call**: Loop body contains method call or function invocation on iterator element
4. **No Early Exit**: Loop completes all iterations (no `break` statements)

**Example Detected Patterns**:
```rust
// Standard observer loop
fn notify(&self, event: &Event) {
    for listener in &self.listeners {  // ← Loop over registry
        listener.handle(event);        // ← Dispatch call
    }
}

// Inline notification with filter
fn notify_matching(&self, predicate: impl Fn(&Handler) -> bool) {
    for handler in self.handlers.iter().filter(predicate) {
        handler.execute();  // ← Dispatch
    }
}

// HashMap dispatch
fn dispatch(&self, event_type: EventType, data: &Data) {
    if let Some(handlers) = self.handlers.get(&event_type) {
        for handler in handlers {  // ← Nested loop detected
            handler.call(data);    // ← Dispatch call
        }
    }
}
```

#### Phase 3: Call Graph Enhancement

Detected observer dispatch functions are marked in the call graph:

```rust
pub struct CallGraphNode {
    // ... existing fields
    pub is_observer_dispatcher: bool,  // ← Enhanced metadata
}
```

**Integration Points**:
- **Dead Code Detection**: Accounts for dynamic dispatch through observer patterns
- **Complexity Analysis**: Recognizes observer loops as coordination logic (lower complexity penalty)
- **Risk Assessment**: Factors in dynamic call graph expansion from observers

### Class Hierarchy Support

The detection system handles inheritance and trait implementations:

**Scenario**: Observer registry in base class, dispatch in derived class
```rust
struct Base {
    listeners: Vec<Box<dyn Listener>>,  // ← Registry in base
}

struct Derived {
    base: Base,  // ← Inherited field
}

impl Derived {
    fn notify(&self) {
        for listener in &self.base.listeners {  // ← Detected via field access
            listener.on_event();
        }
    }
}
```

**Detection Strategy**:
- Track field access chains: `self.base.listeners`
- Match against registry collections even through indirection
- Support nested field patterns: `self.inner.dispatcher.handlers`

### Performance Characteristics

| Operation | Complexity | Notes |
|-----------|-----------|-------|
| Registry Detection | O(f × c) | f = functions, c = avg fields per class |
| Dispatch Detection | O(f × l) | f = functions, l = avg loops per function |
| Call Graph Enhancement | O(n) | n = call graph nodes |
| Overall Impact | <5% overhead | Measured on medium codebases (1000+ functions) |

### Benefits

**False Positive Reduction**:
- Event handlers no longer flagged as dead code
- Callbacks correctly identified as reachable via dispatch
- Dynamic invocation patterns recognized

**Accuracy Improvement**:
- 80% reduction in false positives for event-driven architectures
- 100% retention of true positives (no regression in callback detection)
- Better call graph completeness for observer-based systems

### Integration with Existing Systems

**Unified Analysis Pipeline**:
```
Parse Files
Extract Metrics (existing)
Build Call Graph (existing)
[NEW] Detect Observer Patterns
[NEW] Enhance Call Graph with Dispatch Info
Dead Code Detection (enhanced)
Technical Debt Scoring
```

**Configuration Options**:
```toml
# .debtmap.toml
[observer_detection]
enabled = true
registry_field_patterns = ["listeners", "handlers", "observers", "callbacks"]
min_confidence = 0.8
```

### Testing Strategy

**Unit Tests**:
- Observer registry detection accuracy
- Dispatch loop pattern recognition
- Class hierarchy field access tracking

**Integration Tests**:
- End-to-end observer pattern detection
- Call graph enhancement validation
- False positive reduction measurement

**Regression Tests**:
- Ensure existing callback detection works
- Verify no true positives lost
- Validate performance impact stays <5%

### Limitations and Future Work

**Current Limitations**:
- Requires explicit loops (doesn't detect `map`/`for_each` patterns yet)
- Limited to Rust syntax patterns
- Doesn't track cross-module observer registration

**Planned Enhancements**:
- Functional iterator pattern detection (`for_each`, `map`)
- Multi-language support (Python, TypeScript)
- Inter-module observer tracking via type analysis
- Confidence scoring for ambiguous patterns

## Struct Initialization Pattern Detection

### Overview

DebtMap includes specialized detection for struct initialization/conversion functions where high cyclomatic complexity arises from conditional field assignment rather than complex algorithmic logic. These functions are often incorrectly flagged as overly complex by traditional metrics.

### Problem Statement

Functions that construct structs from configuration or convert between types often exhibit:
- **High cyclomatic complexity** from field-level conditionals (`unwrap_or`, `match` on `Option<T>`)
- **Many simple branches** rather than deep algorithmic complexity
- **Initialization-focused logic** rather than business logic

Traditional cyclomatic complexity metrics penalize these patterns unfairly, treating them as equivalently complex to nested algorithmic logic.

### Detection Strategy

#### Pattern Recognition
The detector identifies functions matching:
- **Field count threshold**: ≥15 fields in struct literal
- **Initialization ratio**: ≥70% of function lines dedicated to field initialization
- **Low nesting depth**: ≤4 levels (characteristic of simple field mapping)
- **Result wrapping**: Returns `Result<StructName, E>` or `StructName` directly

#### Field-Based Complexity Metric

Instead of cyclomatic complexity, we calculate a field-based complexity score:

```rust
field_score = match field_count {
    0..=20 => 1.0,
    21..=40 => 2.0,
    41..=60 => 3.5,
    _ => 5.0,
} + (max_nesting_depth * 0.5) + (complex_fields.len() * 1.0)
```

This provides a more appropriate complexity measure for initialization patterns.

#### Complex Field Detection
Fields requiring >10 lines of initialization logic are flagged as "complex fields" that may benefit from extraction into helper functions.

#### Field Dependency Analysis
The detector tracks which fields reference other local variables/fields to identify:
- **Interdependencies**: Fields that depend on computed values
- **Derived fields**: Fields calculated from other fields
- **Simple mappings**: Direct parameter-to-field assignments

### Confidence Scoring

Confidence is calculated based on multiple factors:
- **Initialization ratio** (0.35 max): Higher ratio = higher confidence
- **Field count** (0.25 max): More fields = more likely initialization
- **Low nesting** (0.20 max): Shallow nesting typical of initialization
- **Struct name patterns** (0.10 max): Names like `Args`, `Config`, `Options`
- **Complex field penalty**: Many complex fields suggest mixed logic

Threshold: Only patterns with ≥60% confidence are reported.

### Recommendations

Based on detected patterns, the detector provides actionable recommendations:

| Field Count | Max Nesting | Complex Fields | Recommendation |
|-------------|-------------|----------------|----------------|
| >50         | any         | any            | Consider builder pattern |
| any         | any         | >5             | Extract complex field initializations |
| any         | >3          | any            | Reduce nesting depth |
| ≤50         | ≤3          | ≤5             | Appropriately complex |

### Integration

The detector is integrated into DebtMap's Rust analyzer as an `OrganizationDetector`, running alongside other anti-pattern detectors (God Object, Feature Envy, etc.).

Output includes:
- Function name and struct being initialized
- Field count and cyclomatic complexity (for comparison)
- Field-based complexity score
- Confidence percentage
- Specific recommendation

### Example Output

```
Struct initialization pattern in 'from_low_args' - 42 fields,
cyclomatic: 38, field complexity: 2.5, confidence: 85%

Recommendation: Initialization is appropriately complex for field count
(Use field-based complexity 2.5 instead of cyclomatic 38)
```

### Limitations

- **Source content dependency**: Requires file content for span analysis
- **Rust-specific**: Currently only implemented for Rust (syn AST)
- **Simple heuristics**: May miss complex initialization patterns

### Testing

**Unit Tests**: Core detection logic, field dependency analysis, confidence scoring
**Integration Tests**: Real-world struct initialization patterns, false positive prevention
**Property Tests**: Planned for invariant verification

## Dependencies

### Core Dependencies
- **rayon**: Parallel execution framework
- **syn**: Native Rust AST parsing with full language support
- **serde**: Serialization
- **clap**: CLI argument parsing
- **quote**: Rust code generation utilities

### Development Dependencies
- **cargo-modules**: Module dependency analysis and visualization
- **proptest**: Property-based testing
- **criterion**: Benchmarking framework
- **tempfile**: Test file management

## Priority Formatter: Pure Core, Imperative Shell Architecture

### Design Philosophy

The priority formatter implements the **Pure Core, Imperative Shell** pattern to separate formatting logic from I/O operations. This architectural pattern enables better testability, composability, and maintainability.

**Location**: `src/priority/formatter/`

### Architecture Layers

```
┌─────────────────────────────────────────┐
│  Imperative Shell (I/O Boundary)        │
│  - writer::write_priority_item()        │
│  - File operations                       │
│  - String mutations                      │
└──────────────────┬──────────────────────┘
┌─────────────────────────────────────────┐
│  Pure Core (Business Logic)             │
│  - pure::format_priority_item()         │
│  - Data transformations                  │
│  - No side effects                       │
└─────────────────────────────────────────┘
```

### Module Organization

#### Pure Core (`pure.rs`)

Contains pure functions that transform data without side effects:

```rust
// Pure function: takes inputs, returns structured data
pub fn format_priority_item(
    rank: usize,
    item: &UnifiedDebtItem,
    has_coverage_data: bool,
) -> FormattedPriorityItem {
    // Pure transformations only
    // No I/O, no mutations
    // Easily testable
}
```

**Characteristics**:
- Deterministic: same inputs → same outputs
- No side effects (no I/O, no mutations)
- Easily testable with unit tests
- Composable and reusable
- Returns structured data types

#### Imperative Shell (`writer.rs`)

Handles I/O operations and applies formatted data to output:

```rust
// I/O function: takes formatted data, performs side effects
pub fn write_priority_item(
    output: &mut String,
    formatted: &FormattedPriorityItem,
) -> std::fmt::Result {
    // I/O at the boundary
    // Applies pure transformations to output
}
```

**Characteristics**:
- Performs I/O operations
- Mutates output buffers
- Thin layer over pure functions
- Minimal logic, maximum effects

### Data Flow

```
Input (UnifiedDebtItem)
Pure Transformation (format_priority_item)
Structured Data (FormattedPriorityItem)
I/O Application (write_priority_item)
Output (String with formatted text)
```

### API Usage

#### New API (Recommended)

```rust
use crate::priority::formatter::pure;
use crate::priority::formatter::writer;

// Step 1: Pure transformation
let formatted = pure::format_priority_item(rank, item, has_coverage_data);

// Step 2: I/O operation
let mut output = String::new();
writer::write_priority_item(&mut output, &formatted)?;
```

#### Legacy API (Deprecated)

```rust
use crate::priority::formatter;

// Before refactoring: mixed logic with I/O
// (Legacy approach has been removed in v1.0)
```

### Benefits

1. **Testability**: Pure functions are trivial to test
   - No mocks needed
   - No I/O setup/teardown
   - Fast unit tests

2. **Composability**: Pure functions compose naturally
   ```rust
   let formatted = items
       .iter()
       .map(|(rank, item)| format_priority_item(*rank, item, true))
       .collect::<Vec<_>>();
   ```

3. **Parallelization**: Pure functions are thread-safe
   ```rust
   items.par_iter()
       .map(|(rank, item)| format_priority_item(*rank, item, true))
       .collect()
   ```

4. **Maintainability**: Clear separation of concerns
   - Business logic isolated from I/O
   - Easy to modify formatting without touching I/O
   - Easy to change output targets without touching logic

### Testing Strategy

#### Pure Core Tests

```rust
#[test]
fn test_format_priority_item_deterministic() {
    let item = create_test_item();
    let result1 = format_priority_item(1, &item, true);
    let result2 = format_priority_item(1, &item, true);
    assert_eq!(result1, result2); // Deterministic
}
```

#### Property-Based Tests

```rust
proptest! {
    #[test]
    fn rank_preserved(rank in 1usize..1000) {
        let item = create_test_item();
        let formatted = format_priority_item(rank, &item, true);
        assert_eq!(formatted.rank, rank);
    }
}
```

### Migration Guide

To migrate existing code:

1. Replace direct `format_priority_item` calls with two-step process:
   ```rust
   // Before:
   format_priority_item(&mut output, rank, item, has_coverage);

   // After:
   let formatted = pure::format_priority_item(rank, item, has_coverage);
   writer::write_priority_item(&mut output, &formatted)?;
   ```

2. Update tests to use pure functions for better isolation

### Formatter Module Structure (Spec 205)

The formatter module has been organized into focused submodules, each with a single clear responsibility:

#### Module Organization

```
src/priority/formatter/
├── mod.rs (163 lines)          # Public API and module orchestration
├── orchestrators.rs            # Thin formatting workflow coordinators
├── pure.rs                     # Pure formatting functions (no I/O)
├── writer.rs                   # I/O operations for formatted output
├── summary.rs                  # Tiered summary display formatting
├── recommendations.rs          # Detailed recommendation formatting
├── sections.rs                 # Section-based formatting logic
├── context.rs                  # Format context creation
├── dependencies.rs             # Dependency information formatting
└── helpers.rs                  # Shared utility functions
```

#### Module Responsibilities

**mod.rs** (Public API):
- Exports public formatting functions
- Declares and organizes submodules
- Re-exports helper functions from submodules
- Contains minimal orchestration code (~163 lines)

**orchestrators.rs** (Workflow Coordination):
- `format_default_with_config()` - Delegates to recommendations module
- `format_tail_with_config()` - Formats bottom N priority items
- Thin wrappers that coordinate between public API and specialized modules

**pure.rs** (Pure Core):
- Pure functions with no side effects
- Data transformations only
- Easily testable, composable functions

**writer.rs** (Imperative Shell):
- I/O operations at system boundary
- Applies formatted data to output buffers
- Minimal logic, maximum effects

**summary.rs** (Tiered Display):
- `format_summary_terminal()` - Entry point for summary mode
- Terminal formatting for tiered priority display
- Compact item formatting for grouped display

**recommendations.rs** (Detailed Formatting):
- Detailed recommendation generation
- Context-aware formatting based on debt patterns
- Integration with evidence formatting

**sections.rs** (Section Formatting):
- Section-based formatting composition
- Modular formatting pipeline
- Composable formatting transformations

**context.rs** (Context Creation):
- Creates formatting context from debt items
- Extracts relevant information for formatting
- Provides unified interface for formatters

**dependencies.rs** (Dependency Formatting):
- Formats upstream/downstream dependency information
- Call graph visualization in output
- Dependency impact display

**helpers.rs** (Utilities):
- Shared formatting utilities
- Color and severity helpers
- Common formatting functions

#### Module Boundaries

- **No circular dependencies**: All modules follow acyclic dependency graph
- **Clear interfaces**: Each module exports minimal public API
- **Single responsibility**: Each module focuses on one aspect of formatting
- **File size limit**: No file exceeds 500 lines (spec 205)
- **Pure core separation**: I/O isolated to writer.rs and specific output modules

## Error Handling

### Resilience Strategy
- Graceful degradation on parser errors
- Partial results on analysis failure
- Detailed error reporting with context
- Recovery mechanisms for parallel failures

### Monitoring
- Performance metrics collection
- Error rate tracking
- Resource usage monitoring
- Analysis quality metrics