ripr 0.5.0

Find Rust test-oracle gaps before mutation testing with static RIPR evidence
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
//! Test-grip evidence per RIPR-SPEC-0005, v1.
//!
//! For each `RepoSeam`, build per-stage evidence (reach / activate /
//! propagate / observe / discriminate) using the existing `RustIndex`
//! facts. This is **not** classification: the output is a per-stage
//! evidence record, not a `SeamGripClass`. The classification PR
//! (`analysis/repo-ripr-classification-v1`) consumes these records.
//!
//! Determinism: `evidence_for_seams` sorts by `seam_id`. Within each
//! evidence record, `related_tests` are deduped and ranked by relation
//! confidence, relation reason, oracle strength, activation overlap,
//! then stable file/name/line tie-breakers.

use super::rust_index::{
    self, FunctionSummary, OracleFact, RustIndex, TestSummary, extract_identifier_tokens,
};
use super::seams::{ExpectedSink, RepoSeam, SeamId, SeamKind};
use crate::domain::{
    Confidence, MissingDiscriminatorFact, OracleKind, OracleStrength, StageEvidence, StageState,
    ValueContext, ValueFact,
};
use serde::{Deserialize, Serialize};
use std::cell::{OnceCell, RefCell};
use std::cmp::Reverse;
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

/// Per-seam test-grip evidence record.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct TestGripEvidence {
    pub(crate) seam_id: SeamId,
    pub(crate) related_tests: Vec<RelatedTestGrip>,
    pub(crate) reach: StageEvidence,
    pub(crate) activate: StageEvidence,
    pub(crate) propagate: StageEvidence,
    pub(crate) observe: StageEvidence,
    pub(crate) discriminate: StageEvidence,
    pub(crate) observed_values: Vec<ValueFact>,
    pub(crate) missing_discriminators: Vec<MissingDiscriminatorFact>,
}

const COMPACT_RELATED_TEST_LIMIT: usize = 12;
const LATENCY_TRACE_ENV: &str = "RIPR_REPO_EXPOSURE_LATENCY_TRACE";
const EVIDENCE_PROGRESS_CHUNK: usize = 500;

/// Per-related-test grip facts attached to a `TestGripEvidence`.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct RelatedTestGrip {
    pub(crate) test_name: String,
    pub(crate) file: PathBuf,
    pub(crate) line: usize,
    pub(crate) oracle_kind: OracleKind,
    pub(crate) oracle_strength: OracleStrength,
    pub(crate) evidence_summary: String,
    pub(crate) relation_reason: RelationReason,
    pub(crate) relation_confidence: RelationConfidence,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct OracleSemantics {
    pub(crate) observes: String,
    pub(crate) missing: String,
    pub(crate) upgrade_suggestion: Option<String>,
}

/// Precomputed per-test facts for repo seam evidence consumers. This
/// avoids repeatedly tokenizing the same test assertions and import
/// lines while classifying every seam in a workspace.
pub(crate) struct CompactGripContext<'a> {
    index: &'a RustIndex,
    tests: Vec<CompactTest<'a>>,
    tests_by_call_name: BTreeMap<String, Vec<usize>>,
    tests_by_assertion_token: BTreeMap<String, Vec<usize>>,
    tests_by_file_stem: BTreeMap<String, Vec<usize>>,
    tests_by_import_token: BTreeMap<String, Vec<usize>>,
    owner_named_cache: RefCell<BTreeMap<String, Vec<usize>>>,
    same_module_cache: RefCell<BTreeMap<String, Vec<usize>>>,
}

struct CompactTest<'a> {
    test: &'a TestSummary,
    path_normalized: String,
    module_path: Option<String>,
    name_lower: String,
    call_names: BTreeSet<String>,
    code_lines: Vec<String>,
    value_facts: OnceCell<super::value_resolution::ValueEnvFacts>,
}

impl<'a> CompactGripContext<'a> {
    pub(crate) fn new(index: &'a RustIndex) -> Self {
        let mut tests_by_call_name: BTreeMap<String, Vec<usize>> = BTreeMap::new();
        let mut tests_by_assertion_token: BTreeMap<String, Vec<usize>> = BTreeMap::new();
        let mut tests_by_file_stem: BTreeMap<String, Vec<usize>> = BTreeMap::new();
        let mut tests_by_import_token: BTreeMap<String, Vec<usize>> = BTreeMap::new();
        let tests = index
            .tests
            .iter()
            .enumerate()
            .map(|(test_index, test)| {
                let call_names = test
                    .calls
                    .iter()
                    .map(|call| call.name.clone())
                    .collect::<BTreeSet<_>>();
                let mut assertion_tokens = BTreeSet::new();
                for assertion in &test.assertions {
                    for token in extract_identifier_tokens(&assertion.text) {
                        assertion_tokens.insert(token);
                    }
                }
                let code_lines = test
                    .body
                    .lines()
                    .map(strip_comments_and_strings)
                    .collect::<Vec<_>>();
                for call_name in &call_names {
                    tests_by_call_name
                        .entry(call_name.clone())
                        .or_default()
                        .push(test_index);
                }
                for token in &assertion_tokens {
                    tests_by_assertion_token
                        .entry(token.clone())
                        .or_default()
                        .push(test_index);
                }
                if let Some(stem) = test.file.file_stem().and_then(|stem| stem.to_str()) {
                    tests_by_file_stem
                        .entry(stem.to_string())
                        .or_default()
                        .push(test_index);
                }
                for token in import_affinity_tokens(&code_lines) {
                    tests_by_import_token
                        .entry(token)
                        .or_default()
                        .push(test_index);
                }
                CompactTest {
                    test,
                    path_normalized: normalize_path(&test.file),
                    module_path: module_path_for(&test.file),
                    name_lower: test.name.to_ascii_lowercase(),
                    call_names,
                    code_lines,
                    value_facts: OnceCell::new(),
                }
            })
            .collect();
        Self {
            index,
            tests,
            tests_by_call_name,
            tests_by_assertion_token,
            tests_by_file_stem,
            tests_by_import_token,
            owner_named_cache: RefCell::new(BTreeMap::new()),
            same_module_cache: RefCell::new(BTreeMap::new()),
        }
    }

    fn owner_named_indices(&self, owner_name_lower: &str) -> Vec<usize> {
        if owner_name_lower.is_empty() {
            return Vec::new();
        }
        if let Some(indices) = self.owner_named_cache.borrow().get(owner_name_lower) {
            return indices.clone();
        }
        let indices = self
            .tests
            .iter()
            .enumerate()
            .filter_map(|(index, test)| test.name_lower.contains(owner_name_lower).then_some(index))
            .collect::<Vec<_>>();
        self.owner_named_cache
            .borrow_mut()
            .insert(owner_name_lower.to_string(), indices.clone());
        indices
    }

    fn same_module_indices(&self, owner_module: &str) -> Vec<usize> {
        if owner_module.is_empty() {
            return Vec::new();
        }
        if let Some(indices) = self.same_module_cache.borrow().get(owner_module) {
            return indices.clone();
        }
        let indices = self
            .tests
            .iter()
            .enumerate()
            .filter_map(|(index, test)| {
                test.module_path
                    .as_deref()
                    .is_some_and(|test_module| same_module(owner_module, test_module))
                    .then_some(index)
            })
            .collect::<Vec<_>>();
        self.same_module_cache
            .borrow_mut()
            .insert(owner_module.to_string(), indices.clone());
        indices
    }
}

/// Why this test is related to the seam. v1: a single highest-priority
/// reason per test (no multi-reason public shape). Priority is pinned
/// by `RelationReason::priority` and exercised by ranking tests.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum RelationReason {
    DirectOwnerCall,
    AssertionTargetAffinity,
    SameTestFile,
    SameModule,
    OwnerNamedTest,
    ImportPathAffinity,
    FixtureOwnerAffinity,
}

impl RelationReason {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::DirectOwnerCall => "direct_owner_call",
            Self::AssertionTargetAffinity => "assertion_target_affinity",
            Self::SameTestFile => "same_test_file",
            Self::SameModule => "same_module",
            Self::OwnerNamedTest => "owner_named_test",
            Self::ImportPathAffinity => "import_path_affinity",
            Self::FixtureOwnerAffinity => "fixture_owner_affinity",
        }
    }

    /// Lower value sorts first. Stable contract pinned by tests.
    fn priority(self) -> u8 {
        match self {
            Self::DirectOwnerCall => 0,
            Self::AssertionTargetAffinity => 1,
            Self::SameTestFile => 2,
            Self::SameModule => 3,
            Self::OwnerNamedTest => 4,
            Self::ImportPathAffinity => 5,
            Self::FixtureOwnerAffinity => 6,
        }
    }

    fn confidence(self) -> RelationConfidence {
        match self {
            Self::DirectOwnerCall | Self::AssertionTargetAffinity => RelationConfidence::High,
            Self::SameTestFile
            | Self::SameModule
            | Self::OwnerNamedTest
            | Self::ImportPathAffinity => RelationConfidence::Medium,
            Self::FixtureOwnerAffinity => RelationConfidence::Low,
        }
    }
}

/// Confidence that the related test grips the seam. Independent of
/// oracle strength: a `Low` relation can still carry a strong oracle.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum RelationConfidence {
    High,
    Medium,
    Low,
    Opaque,
}

impl RelationConfidence {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::High => "high",
            Self::Medium => "medium",
            Self::Low => "low",
            Self::Opaque => "opaque",
        }
    }

    /// Lower value sorts first (highest confidence first).
    fn rank(self) -> u8 {
        match self {
            Self::High => 0,
            Self::Medium => 1,
            Self::Low => 2,
            Self::Opaque => 3,
        }
    }
}

/// Build evidence records for a slice of seams. Output is sorted by
/// `seam_id` so two runs over the same input produce identical bytes.
pub(crate) fn evidence_for_seams(seams: &[RepoSeam], index: &RustIndex) -> Vec<TestGripEvidence> {
    let context_started = Instant::now();
    let context = CompactGripContext::new(index);
    trace_latency_phase(
        "evidence_context",
        &format!("tests_{}_seams_{}", context.tests.len(), seams.len()),
        context_started.elapsed(),
    );

    let evidence_started = Instant::now();
    let mut out: Vec<TestGripEvidence> = Vec::with_capacity(seams.len());
    for (index, seam) in seams.iter().enumerate() {
        out.push(evidence_for_seam_with_context(seam, &context));
        let processed = index + 1;
        if processed % EVIDENCE_PROGRESS_CHUNK == 0 || processed == seams.len() {
            trace_latency_phase(
                "evidence_for_seams_progress",
                &format!("processed_{processed}_of_{}", seams.len()),
                evidence_started.elapsed(),
            );
        }
    }
    out.sort_by(|a, b| a.seam_id.as_str().cmp(b.seam_id.as_str()));
    out
}

/// Build evidence for a single seam.
#[cfg(test)]
pub(crate) fn evidence_for_seam(seam: &RepoSeam, index: &RustIndex) -> TestGripEvidence {
    let context = CompactGripContext::new(index);
    evidence_for_seam_with_context(seam, &context)
}

fn evidence_for_seam_with_context(
    seam: &RepoSeam,
    context: &CompactGripContext<'_>,
) -> TestGripEvidence {
    let mut related_with_reason = find_related_tests_with_context(seam, context);
    sort_related_tests_for_seam(seam, context, &mut related_with_reason);
    let related_indexed: Vec<&CompactTest<'_>> = related_with_reason
        .iter()
        .map(|(indexed, _reason)| *indexed)
        .collect();
    let owner_fn = find_owner_function(seam, context.index);

    let related: Vec<&TestSummary> = related_indexed.iter().map(|indexed| indexed.test).collect();

    let reach = reach_evidence(seam, &related);
    let (activate, observed_values, missing_discriminators) =
        activate_evidence(seam, &related_indexed, context.index, owner_fn);
    let propagate = propagate_evidence(seam, &related);
    let observe = observe_evidence(&related);
    let discriminate = discriminate_evidence(seam, &related);

    let related_tests: Vec<RelatedTestGrip> = related_with_reason
        .iter()
        .map(|(indexed, reason)| related_test_grip(seam, indexed.test, *reason))
        .collect();

    TestGripEvidence {
        seam_id: seam.id().clone(),
        related_tests,
        reach,
        activate,
        propagate,
        observe,
        discriminate,
        observed_values,
        missing_discriminators,
    }
}

fn trace_latency_phase(phase: &str, status: &str, duration: Duration) {
    if std::env::var_os(LATENCY_TRACE_ENV).is_some() {
        eprintln!("{}", latency_trace_line(phase, status, duration));
    }
}

fn latency_trace_line(phase: &str, status: &str, duration: Duration) -> String {
    format!(
        "ripr_repo_exposure_latency phase={phase} status={status} duration_ms={}",
        duration.as_millis()
    )
}

/// Build compact evidence for a single seam. The returned
/// `TestGripEvidence` preserves the stage states used by classification,
/// but intentionally omits related-test detail and observed-value
/// payloads because repo badges only need per-class counts.
pub(crate) fn compact_evidence_for_seam(
    seam: &RepoSeam,
    context: &CompactGripContext<'_>,
) -> TestGripEvidence {
    let related_indexed = find_related_tests_compact(seam, context);
    let related: Vec<&TestSummary> = related_indexed.iter().map(|indexed| indexed.test).collect();
    let owner_fn = find_owner_function(seam, context.index);

    let reach = reach_evidence(seam, &related);
    let (activate, missing_discriminators) =
        compact_activate_evidence(seam, &related_indexed, context.index, owner_fn);
    let propagate = propagate_evidence(seam, &related);
    let observe = observe_evidence(&related);
    let discriminate = discriminate_evidence(seam, &related);

    TestGripEvidence {
        seam_id: seam.id().clone(),
        related_tests: Vec::new(),
        reach,
        activate,
        propagate,
        observe,
        discriminate,
        observed_values: Vec::new(),
        missing_discriminators,
    }
}

/// Walk `index.tests` and return tests that plausibly relate to `seam`,
/// each tagged with the single highest-priority `RelationReason` it
/// satisfies. The two-step "match then rank" replaces the old binary
/// `calls_owner || same_file_or_named` check from earlier campaigns.
///
/// Detection per reason — strict ordering: the first reason that fires
/// wins, so e.g. a test that both `calls owner` and `is in same file`
/// carries `direct_owner_call`, never `same_test_file`.
fn find_related_tests_with_context<'context, 'index>(
    seam: &RepoSeam,
    context: &'context CompactGripContext<'index>,
) -> Vec<(&'context CompactTest<'index>, RelationReason)> {
    let owner_fn = find_owner_function(seam, context.index);
    let owner_name = owner_fn.map(|f| f.name.as_str()).unwrap_or("");
    let owner_name_lower = owner_name.to_ascii_lowercase();
    let owner_file = owner_fn.map(|f| f.file.as_path());
    let owner_file_stem = owner_file
        .and_then(|p| p.file_stem())
        .and_then(|s| s.to_str())
        .unwrap_or("");
    let owner_module_path = owner_file.and_then(module_path_for);
    let prefix = owner_fn.and_then(|f| package_prefix(&f.file));
    let fixture_names = owner_file
        .and_then(|file| context.index.files.get(file))
        .map(fixture_names_for_owner_file)
        .unwrap_or_default();

    // Tokens from `RequiredDiscriminator` and `ExpectedSink` for
    // `assertion_target_affinity`. Filtered through
    // `extract_identifier_tokens`, so common stop-words and short
    // tokens are already excluded — the residual set is what a test
    // assertion would have to mention to count.
    let discriminator_tokens = required_discriminator_tokens(seam);
    let sink_tokens = extract_identifier_tokens(seam.expected_sink().as_str());
    let target_tokens: BTreeSet<String> = discriminator_tokens
        .into_iter()
        .chain(sink_tokens)
        .collect();

    let mut candidate_reasons: BTreeMap<usize, RelationReason> = BTreeMap::new();
    let prefix = prefix.as_deref();

    if !owner_name.is_empty()
        && let Some(indices) = context.tests_by_call_name.get(owner_name)
    {
        for test_index in indices {
            insert_related_candidate(
                &mut candidate_reasons,
                context,
                prefix,
                *test_index,
                RelationReason::DirectOwnerCall,
            );
        }
    }

    for token in &target_tokens {
        if let Some(indices) = context.tests_by_assertion_token.get(token) {
            for test_index in indices {
                insert_related_candidate(
                    &mut candidate_reasons,
                    context,
                    prefix,
                    *test_index,
                    RelationReason::AssertionTargetAffinity,
                );
            }
        }
    }

    if !owner_file_stem.is_empty() {
        for stem in [
            owner_file_stem.to_string(),
            format!("{owner_file_stem}_test"),
            format!("{owner_file_stem}_tests"),
        ] {
            if let Some(indices) = context.tests_by_file_stem.get(&stem) {
                for test_index in indices {
                    insert_related_candidate(
                        &mut candidate_reasons,
                        context,
                        prefix,
                        *test_index,
                        RelationReason::SameTestFile,
                    );
                }
            }
        }
    }

    if let Some(owner_module_path) = owner_module_path.as_deref() {
        for test_index in context.same_module_indices(owner_module_path) {
            insert_related_candidate(
                &mut candidate_reasons,
                context,
                prefix,
                test_index,
                RelationReason::SameModule,
            );
        }
    }

    for test_index in context.owner_named_indices(&owner_name_lower) {
        insert_related_candidate(
            &mut candidate_reasons,
            context,
            prefix,
            test_index,
            RelationReason::OwnerNamedTest,
        );
    }

    if !owner_name.is_empty()
        && let Some(indices) = context.tests_by_import_token.get(owner_name)
    {
        for test_index in indices {
            if !context
                .tests
                .get(*test_index)
                .is_some_and(|indexed| test_imports_owner_compact(indexed, owner_name))
            {
                continue;
            }
            insert_related_candidate(
                &mut candidate_reasons,
                context,
                prefix,
                *test_index,
                RelationReason::ImportPathAffinity,
            );
        }
    }

    for fixture_name in &fixture_names {
        if let Some(indices) = context.tests_by_call_name.get(fixture_name) {
            for test_index in indices {
                insert_related_candidate(
                    &mut candidate_reasons,
                    context,
                    prefix,
                    *test_index,
                    RelationReason::FixtureOwnerAffinity,
                );
            }
        }
    }

    let mut related: Vec<(&'context CompactTest<'index>, RelationReason)> = Vec::new();
    let mut seen: std::collections::HashSet<(String, std::path::PathBuf, usize)> =
        std::collections::HashSet::new();

    for (test_index, reason) in candidate_reasons {
        let Some(indexed) = context.tests.get(test_index) else {
            continue;
        };
        let key = (
            indexed.test.name.clone(),
            indexed.test.file.clone(),
            indexed.test.start_line,
        );
        if seen.insert(key) {
            related.push((indexed, reason));
        }
    }
    related
}

fn insert_related_candidate(
    candidate_reasons: &mut BTreeMap<usize, RelationReason>,
    context: &CompactGripContext<'_>,
    prefix: Option<&str>,
    test_index: usize,
    reason: RelationReason,
) {
    if candidate_reasons.contains_key(&test_index) {
        return;
    }
    let Some(indexed) = context.tests.get(test_index) else {
        return;
    };
    if let Some(prefix) = prefix
        && !indexed.path_normalized.starts_with(prefix)
    {
        return;
    }
    candidate_reasons.insert(test_index, reason);
}

fn find_related_tests_compact<'a>(
    seam: &RepoSeam,
    context: &'a CompactGripContext<'_>,
) -> Vec<&'a CompactTest<'a>> {
    let mut related = find_related_tests_with_context(seam, context);
    sort_related_tests_for_seam(seam, context, &mut related);
    related
        .into_iter()
        .take(COMPACT_RELATED_TEST_LIMIT)
        .map(|(indexed, _reason)| indexed)
        .collect()
}

#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
struct RelatedTestRankKey {
    relation_confidence: u8,
    relation_reason: u8,
    oracle_strength: Reverse<u8>,
    activation_overlap: Reverse<usize>,
    file: PathBuf,
    test_name: String,
    line: usize,
}

fn sort_related_tests_for_seam(
    seam: &RepoSeam,
    context: &CompactGripContext<'_>,
    related: &mut [(&CompactTest<'_>, RelationReason)],
) {
    related.sort_by_cached_key(|entry| {
        let (indexed, reason) = *entry;
        related_test_rank_key(seam, context, indexed, reason)
    });
}

fn related_test_rank_key(
    seam: &RepoSeam,
    context: &CompactGripContext<'_>,
    indexed: &CompactTest<'_>,
    reason: RelationReason,
) -> RelatedTestRankKey {
    let (_oracle_kind, oracle_strength) = best_oracle(indexed.test, seam);
    RelatedTestRankKey {
        relation_confidence: reason.confidence().rank(),
        relation_reason: reason.priority(),
        oracle_strength: Reverse(oracle_strength.rank()),
        activation_overlap: Reverse(activation_overlap_score(seam, context, indexed)),
        file: indexed.test.file.clone(),
        test_name: indexed.test.name.clone(),
        line: indexed.test.start_line,
    }
}

fn fixture_names_for_owner_file(facts: &rust_index::FileFacts) -> BTreeSet<String> {
    facts
        .functions
        .iter()
        .filter(|f| !f.is_test && (is_fixture_named(&f.name) || f.body.contains("#[fixture]")))
        .map(|f| f.name.clone())
        .collect()
}

/// Tokens drawn from a `RepoSeam`'s `RequiredDiscriminator`. Filtered
/// through `extract_identifier_tokens` so common short words and
/// stop-tokens are already excluded.
fn required_discriminator_tokens(seam: &RepoSeam) -> Vec<String> {
    extract_identifier_tokens(required_discriminator_text(seam))
}

fn required_discriminator_text(seam: &RepoSeam) -> &str {
    use super::seams::RequiredDiscriminator;
    match seam.required_discriminator() {
        RequiredDiscriminator::BoundaryValue { description }
        | RequiredDiscriminator::ReturnValue { description } => description.as_str(),
        RequiredDiscriminator::ErrorVariant { variant } => variant.as_str(),
        RequiredDiscriminator::FieldValue { field } => field.as_str(),
        RequiredDiscriminator::Effect { sink } => sink.as_str(),
        RequiredDiscriminator::MatchArmTaken { arm } => arm.as_str(),
        RequiredDiscriminator::CallSite { target } => target.as_str(),
    }
}

/// Token-aware: does any assertion text in `test` contain at least one
/// of `tokens` as a whole identifier? Substring match would let
/// `discount` accidentally match `discount_threshold`; we want exact
/// identifier hits.
#[cfg(test)]
fn assertion_targets_seam(test: &TestSummary, tokens: &[String]) -> bool {
    if tokens.is_empty() {
        return false;
    }
    for assertion in &test.assertions {
        let assertion_tokens = extract_identifier_tokens(&assertion.text);
        if assertion_tokens
            .iter()
            .any(|at| tokens.iter().any(|t| at == t))
        {
            return true;
        }
    }
    false
}

#[cfg(test)]
fn same_test_file(test_file: &Path, owner_stem: &str) -> bool {
    let stem = match test_file.file_stem().and_then(|s| s.to_str()) {
        Some(s) => s,
        None => return false,
    };
    if stem == owner_stem {
        return true;
    }
    // Suffix check avoids the allocation that `stem == format!("{owner_stem}_test")`
    // would do per call. Two suffix variants cover the common naming
    // conventions: `*_test.rs` and `*_tests.rs`.
    if let Some(prefix) = stem.strip_suffix("_test")
        && prefix == owner_stem
    {
        return true;
    }
    if let Some(prefix) = stem.strip_suffix("_tests")
        && prefix == owner_stem
    {
        return true;
    }
    false
}

/// Module path slug for a Rust source file: the path components below
/// `src/` or `tests/`, joined by `/`, dropping the file extension.
/// Returns `None` for files that do not sit under one of those roots.
/// Examples (Unix-style after normalize):
/// - `crates/ripr/src/auth/login.rs` → `auth/login`
/// - `tests/cli_smoke.rs`            → `cli_smoke`
fn module_path_for(file: &Path) -> Option<String> {
    let normalized = normalize_path(file);
    let body = normalized
        .rfind("/src/")
        .map(|idx| &normalized[idx + "/src/".len()..])
        .or_else(|| {
            normalized
                .rfind("/tests/")
                .map(|idx| &normalized[idx + "/tests/".len()..])
        })
        .or_else(|| normalized.strip_prefix("src/"))
        .or_else(|| normalized.strip_prefix("tests/"))?;
    let trimmed = body.strip_suffix(".rs").unwrap_or(body);
    if trimmed.is_empty() {
        None
    } else {
        Some(trimmed.to_string())
    }
}

/// Two files share a module if any non-leaf segment of the owner's
/// module path appears as a prefix of the test's module path. The leaf
/// stem is excluded so this does not duplicate `same_test_file`.
fn same_module(owner_module: &str, test_module: &str) -> bool {
    let parent = match owner_module.rsplit_once('/') {
        Some((parent, _leaf)) => parent,
        None => return false,
    };
    if parent.is_empty() {
        return false;
    }
    test_module == parent
        || test_module.starts_with(&format!("{parent}/"))
        || test_module.starts_with(&format!("{}/", parent.replace('/', "_")))
}

/// Body mentions the owner via an explicit qualified-path or `use`
/// shape — without calling it. The direct-call check has already
/// excluded callers, so this fires for tests that import the symbol
/// (or qualify it via a path) but route through some wrapper (common
/// in integration tests).
///
/// Tightened per #310 review: pure token co-occurrence
/// (owner_name appearing as a bare identifier somewhere in the body)
/// was too easy to satisfy with local bindings, comments, or
/// unrelated identifiers. The detector now requires either:
///
/// 1. a `module::owner_name` qualified path anywhere in the body
///    (catches `crate::pricing::discounted_total`,
///    `super::pricing::discounted_total`, `pricing::discounted_total`
///    — they all contain `::owner_name`); or
/// 2. an inline `use ... owner_name` line in the test body. File-
///    scope `use` lines are not in `test.body` so this only covers
///    in-function imports.
fn test_imports_owner_compact(test: &CompactTest<'_>, owner_name: &str) -> bool {
    if owner_name.is_empty() {
        return false;
    }
    let qualified = format!("::{owner_name}");
    for code in &test.code_lines {
        if code.contains(&qualified) {
            return true;
        }
        if code.trim_start().starts_with("use ")
            && extract_identifier_tokens(code)
                .iter()
                .any(|token| token == owner_name)
        {
            return true;
        }
    }
    false
}

fn import_affinity_tokens(code_lines: &[String]) -> BTreeSet<String> {
    let mut tokens = BTreeSet::new();
    for code in code_lines {
        let trimmed = code.trim_start();
        if code.contains("::") || trimmed.starts_with("use ") {
            tokens.extend(extract_identifier_tokens(code));
        }
    }
    tokens
}

/// Drop everything after a `//` line comment and replace string-literal
/// contents with empty strings. v1 best-effort: handles `"..."` with
/// `\\` and `\"` escapes; raw strings (`r#"..."#`), char literals
/// (`'a'`), and block comments (`/* ... */`) are out of scope — those
/// shapes are rare inside test bodies and treating them as code is a
/// safe over-match (the previous helper accepted them all).
fn strip_comments_and_strings(line: &str) -> String {
    // Strip `//` line comments first; everything after is non-code.
    let without_comment = match line.find("//") {
        Some(idx) => &line[..idx],
        None => line,
    };
    let mut out = String::with_capacity(without_comment.len());
    let mut in_string = false;
    let mut escaped = false;
    for ch in without_comment.chars() {
        if in_string {
            if escaped {
                escaped = false;
                continue;
            }
            match ch {
                '\\' => escaped = true,
                '"' => in_string = false,
                _ => {}
            }
            continue;
        }
        if ch == '"' {
            in_string = true;
            continue;
        }
        out.push(ch);
    }
    out
}

fn is_fixture_named(name: &str) -> bool {
    let prefixes = ["fixture_", "setup_", "make_", "build_", "new_", "mock_"];
    let suffixes = ["_fixture", "_factory"];
    prefixes.iter().any(|p| name.starts_with(p)) || suffixes.iter().any(|s| name.ends_with(s))
}

fn find_owner_function<'a>(seam: &RepoSeam, index: &'a RustIndex) -> Option<&'a FunctionSummary> {
    rust_index::find_owner_function(index, seam.file(), seam.display_line())
}

fn normalize_path(path: &Path) -> String {
    path.to_string_lossy()
        .replace('\\', "/")
        .trim_start_matches("./")
        .to_string()
}

fn package_prefix(path: &Path) -> Option<String> {
    let normalized = normalize_path(path);
    if let Some(rest) = normalized.strip_prefix("crates/")
        && let Some((crate_name, crate_relative)) = rest.split_once('/')
        && (crate_relative.starts_with("src/") || crate_relative.starts_with("tests/"))
    {
        return Some(format!("crates/{crate_name}/"));
    }
    for marker in ["/src/", "/tests/"] {
        if let Some(idx) = normalized.rfind(marker) {
            let prefix = &normalized[..idx];
            if prefix.is_empty() {
                return None;
            }
            return Some(format!("{prefix}/"));
        }
    }
    None
}

fn reach_evidence(seam: &RepoSeam, related: &[&TestSummary]) -> StageEvidence {
    if related.is_empty() {
        return StageEvidence::new(
            StageState::No,
            Confidence::Medium,
            format!(
                "No static test path found for seam owner `{}`",
                seam.owner()
            ),
        );
    }
    let names: Vec<&str> = related.iter().take(3).map(|t| t.name.as_str()).collect();
    StageEvidence::new(
        StageState::Yes,
        Confidence::Medium,
        format!(
            "Related tests appear to reach `{}`: {}",
            seam.owner(),
            names.join(", ")
        ),
    )
}

/// Activation evidence.
///
/// Returns `(stage, observed_values, missing_discriminators)`. The
/// observed values come from the seam's owner-call argument lists
/// across all related tests. The missing-discriminator set is the
/// per-kind required value or shape minus what we observed.
fn activate_evidence(
    seam: &RepoSeam,
    related: &[&CompactTest<'_>],
    index: &RustIndex,
    owner_fn: Option<&FunctionSummary>,
) -> (StageEvidence, Vec<ValueFact>, Vec<MissingDiscriminatorFact>) {
    let owner_name = owner_fn.map(|f| f.name.as_str()).unwrap_or("");
    let mut observed: Vec<ValueFact> = Vec::new();

    if !owner_name.is_empty() {
        for indexed in related {
            observed.extend(observed_value_facts_for_test(
                seam, indexed, index, owner_name,
            ));
        }
    }
    sort_value_facts(&mut observed);

    let missing = missing_discriminators_for(seam, &observed);

    let state = if related.is_empty() {
        StageState::No
    } else if !observed.is_empty() {
        StageState::Yes
    } else {
        // Reach exists but no concrete value seen — most often a helper
        // call that hides the activation, or an integration test.
        StageState::Unknown
    };
    let stage = StageEvidence::new(
        state,
        if observed.is_empty() {
            Confidence::Low
        } else {
            Confidence::Medium
        },
        if observed.is_empty() {
            format!(
                "No concrete activation values observed for seam `{}`",
                seam.expression()
                    .lines()
                    .next()
                    .unwrap_or(seam.expression())
            )
        } else {
            format!(
                "Observed {} concrete activation value(s) for seam `{}`",
                observed.len(),
                seam.expression()
                    .lines()
                    .next()
                    .unwrap_or(seam.expression())
            )
        },
    );
    (stage, observed, missing)
}

fn observed_value_facts_for_test(
    seam: &RepoSeam,
    indexed: &CompactTest<'_>,
    index: &RustIndex,
    owner_name: &str,
) -> Vec<ValueFact> {
    let mut observed: Vec<ValueFact> = Vec::new();
    // Per-test resolution facts (let bindings, rstest cases, table
    // rows, same-file consts) are built lazily and then reused across
    // all owner calls in this test. Per `analysis/value-extraction-v2`.
    let value_facts = indexed
        .value_facts
        .get_or_init(|| super::value_resolution::ValueEnvFacts::build(indexed.test, index));
    let env = super::value_resolution::ValueEnv::new(seam, value_facts);
    let observed_argument_indices = observed_argument_indices(seam, index, owner_name);
    for call in &indexed.test.calls {
        if call.name != owner_name {
            continue;
        }
        let Some(args) = call_arguments(&call.text, owner_name) else {
            continue;
        };
        for (arg_index, arg) in args.into_iter().enumerate() {
            if let Some(indices) = &observed_argument_indices
                && !indices.contains(&arg_index)
            {
                continue;
            }
            let mut emitted = false;
            // Direct literal first (matches pre-v2 behavior).
            for value in scalar_values(&arg) {
                observed.push(ValueFact {
                    line: call.line,
                    text: call.text.clone(),
                    value,
                    context: ValueContext::FunctionArgument,
                });
                emitted = true;
            }
            if emitted {
                continue;
            }
            // value-extraction-v2: try to resolve the arg through the
            // priority chain (let / rstest case / table row /
            // same-file const / Some/Ok/Err).
            for (value, context) in env.resolve(&arg) {
                observed.push(ValueFact {
                    line: call.line,
                    text: call.text.clone(),
                    value,
                    context,
                });
            }
        }
    }
    // Builder-method values (e.g.,
    // `Quote::new().amount(100).threshold(100)`) - collected
    // separately because they don't fit the per-arg shape. These only
    // count when method names align with seam tokens; the env enforces
    // that filter.
    observed.extend(env.builder_facts());
    observed
}

fn observed_argument_indices(
    seam: &RepoSeam,
    index: &RustIndex,
    owner_name: &str,
) -> Option<Vec<usize>> {
    if seam.kind() != SeamKind::PredicateBoundary {
        return None;
    }
    let owner_fn = find_owner_function(seam, index)?;
    if owner_fn.name != owner_name {
        return None;
    }
    let (left, right) = comparison_operands(seam.expression())?;
    let parameters = function_parameters(owner_fn);
    if let Some(left_index) = parameters.iter().position(|param| param == &left) {
        return Some(vec![left_index]);
    }
    parameters
        .iter()
        .position(|param| param == &right)
        .map(|right_index| vec![right_index])
}

fn activation_overlap_score(
    seam: &RepoSeam,
    context: &CompactGripContext<'_>,
    indexed: &CompactTest<'_>,
) -> usize {
    let Some(owner_fn) = find_owner_function(seam, context.index) else {
        return 0;
    };
    let owner_name = owner_fn.name.as_str();
    if owner_name.is_empty() {
        return 0;
    }

    let mut score = boundary_equality_overlap_score(seam, indexed, context.index, owner_fn);
    let required_text = required_discriminator_text(seam);
    score += observed_value_facts_for_test(seam, indexed, context.index, owner_name)
        .iter()
        .filter(|fact| observed_value_matches_required_discriminator(&fact.value, required_text))
        .count();
    score
}

fn observed_value_matches_required_discriminator(value: &str, required_text: &str) -> bool {
    let value = value.trim();
    let required_text = required_text.trim();
    !value.is_empty()
        && !required_text.is_empty()
        && (value == required_text
            || value.contains(required_text)
            || required_text.contains(value))
}

fn boundary_equality_overlap_score(
    seam: &RepoSeam,
    indexed: &CompactTest<'_>,
    index: &RustIndex,
    owner_fn: &FunctionSummary,
) -> usize {
    if seam.kind() != SeamKind::PredicateBoundary {
        return 0;
    }
    let Some((left, right)) = comparison_operands(seam.expression()) else {
        return 0;
    };
    let parameters = function_parameters(owner_fn);
    let Some(left_index) = parameters.iter().position(|param| param == &left) else {
        return 0;
    };
    let Some(right_index) = parameters.iter().position(|param| param == &right) else {
        return 0;
    };

    let mut score = 0;
    for call in &indexed.test.calls {
        if call.name != owner_fn.name {
            continue;
        }
        let Some(args) = call_arguments(&call.text, &owner_fn.name) else {
            continue;
        };
        let Some(left_arg) = args.get(left_index) else {
            continue;
        };
        let Some(right_arg) = args.get(right_index) else {
            continue;
        };
        if arguments_overlap_at_boundary(seam, indexed, index, left_arg, right_arg) {
            score += 1;
        }
    }
    score
}

fn arguments_overlap_at_boundary(
    seam: &RepoSeam,
    indexed: &CompactTest<'_>,
    index: &RustIndex,
    left_arg: &str,
    right_arg: &str,
) -> bool {
    if left_arg.trim() == right_arg.trim() && !left_arg.trim().is_empty() {
        return true;
    }
    let left_values = resolved_argument_values(seam, indexed, index, left_arg);
    let right_values = resolved_argument_values(seam, indexed, index, right_arg);
    left_values.iter().any(|left| {
        let left = comparable_value(left);
        right_values
            .iter()
            .any(|right| left == comparable_value(right))
    })
}

fn resolved_argument_values(
    seam: &RepoSeam,
    indexed: &CompactTest<'_>,
    index: &RustIndex,
    arg: &str,
) -> Vec<String> {
    let values = scalar_values(arg);
    if !values.is_empty() {
        return values;
    }
    let value_facts = indexed
        .value_facts
        .get_or_init(|| super::value_resolution::ValueEnvFacts::build(indexed.test, index));
    let env = super::value_resolution::ValueEnv::new(seam, value_facts);
    env.resolve(arg)
        .into_iter()
        .map(|(value, _context)| value)
        .collect()
}

fn compact_activate_evidence(
    seam: &RepoSeam,
    related: &[&CompactTest<'_>],
    index: &RustIndex,
    owner_fn: Option<&FunctionSummary>,
) -> (StageEvidence, Vec<MissingDiscriminatorFact>) {
    if seam.kind() == SeamKind::PredicateBoundary {
        let (stage, _observed, missing) = activate_evidence(seam, related, index, owner_fn);
        return (stage, missing);
    }

    let owner_name = owner_fn.map(|f| f.name.as_str()).unwrap_or("");
    let direct_owner_call = !owner_name.is_empty()
        && related
            .iter()
            .any(|indexed| indexed.call_names.contains(owner_name));
    let state = if related.is_empty() {
        StageState::No
    } else if direct_owner_call {
        StageState::Yes
    } else {
        StageState::Unknown
    };
    let stage = StageEvidence::new(
        state.clone(),
        if direct_owner_call {
            Confidence::Medium
        } else {
            Confidence::Low
        },
        format!(
            "Compact activation evidence for seam `{}` is `{}`",
            seam.expression()
                .lines()
                .next()
                .unwrap_or(seam.expression()),
            state.as_str()
        ),
    );
    (stage, Vec::new())
}

fn missing_discriminators_for(
    seam: &RepoSeam,
    observed: &[ValueFact],
) -> Vec<MissingDiscriminatorFact> {
    match seam.kind() {
        SeamKind::PredicateBoundary => {
            // Without a value model we cannot prove the boundary value is
            // tested. Surface a hypothesis if the predicate uses a
            // strict-or-equal operator and at least one observed value is
            // strictly above or below.
            let expression = seam.expression();
            if !boundary_predicate_uses_equal_op(expression) {
                return Vec::new();
            }
            let boundary_token = boundary_rhs_token(expression);
            if boundary_token.is_empty() {
                return Vec::new();
            }
            let any_observed = !observed.is_empty();
            if !any_observed {
                return vec![MissingDiscriminatorFact {
                    value: format!("{boundary_token} (boundary value)"),
                    reason: "no observed activation values for boundary predicate".to_string(),
                    flow_sink: None,
                }];
            }
            // We do not yet know the literal value of `boundary_token`,
            // so we can only flag that the equality boundary is not
            // explicitly named in the observed value set.
            //
            // Use exact equality rather than `contains` to avoid false
            // matches like `boundary_token = "10"` matching observed
            // value `"100"`. Observed values are literal scalars produced
            // by `scalar_values`, so byte-for-byte equality is the right
            // contract here.
            let equality_seen = observed
                .iter()
                .any(|v| v.value.as_str() == boundary_token.as_str());
            if equality_seen {
                Vec::new()
            } else {
                vec![MissingDiscriminatorFact {
                    value: format!("{boundary_token} (equality boundary)"),
                    reason:
                        "observed values do not include the equality-boundary case for this predicate"
                            .to_string(),
                    flow_sink: None,
                }]
            }
        }
        SeamKind::ErrorVariant => Vec::new(),
        SeamKind::ReturnValue
        | SeamKind::FieldConstruction
        | SeamKind::SideEffect
        | SeamKind::MatchArm
        | SeamKind::CallPresence => Vec::new(),
    }
}

fn boundary_predicate_uses_equal_op(expression: &str) -> bool {
    expression.contains(" >= ")
        || expression.contains(" <= ")
        || expression.contains(" == ")
        || expression.contains(" != ")
}

/// Best-effort right-hand-side identifier for a boundary predicate.
/// Returns empty if we cannot pick one out heuristically.
fn boundary_rhs_token(expression: &str) -> String {
    for op in [" >= ", " <= ", " == ", " != ", " > ", " < "] {
        if let Some(idx) = expression.find(op) {
            let rhs = expression[idx + op.len()..].trim();
            // Take up to the first non-identifier char.
            let token: String = rhs
                .chars()
                .take_while(|c| c.is_alphanumeric() || *c == '_')
                .collect();
            if !token.is_empty() {
                return token;
            }
        }
    }
    String::new()
}

fn function_parameters(function: &FunctionSummary) -> Vec<String> {
    let signature = function
        .body
        .lines()
        .next()
        .unwrap_or(function.body.as_str());
    let Some(open) = signature.find('(') else {
        return Vec::new();
    };
    let after_open = &signature[open + 1..];
    let Some(close) = after_open.find(')') else {
        return Vec::new();
    };
    split_top_level_commas(&after_open[..close])
        .into_iter()
        .filter_map(|argument| {
            argument
                .split_once(':')
                .map(|(name, _type)| name.trim().to_string())
        })
        .filter(|name| !name.is_empty() && name != "self" && name != "&self" && name != "mut self")
        .collect()
}

fn comparison_operands(expression: &str) -> Option<(String, String)> {
    for operator in [">=", "<=", "==", "!=", ">", "<"] {
        if let Some((left, right)) = expression.split_once(operator) {
            let left = clean_operand(left);
            let right = clean_operand(right);
            if !left.is_empty() && !right.is_empty() {
                return Some((left, right));
            }
        }
    }
    None
}

fn clean_operand(operand: &str) -> String {
    let cleaned = operand
        .trim()
        .trim_start_matches("if ")
        .trim_end_matches('{')
        .trim_end_matches(';')
        .trim();
    cleaned
        .split_once('{')
        .map(|(before, _after)| before.trim())
        .unwrap_or(cleaned)
        .to_string()
}

fn comparable_value(value: &str) -> String {
    value
        .trim()
        .trim_matches('"')
        .chars()
        .filter(|ch| *ch != '_')
        .collect()
}

fn propagate_evidence(seam: &RepoSeam, related: &[&TestSummary]) -> StageEvidence {
    if related.is_empty() {
        return StageEvidence::new(
            StageState::No,
            Confidence::Medium,
            "No related tests; cannot infer propagation",
        );
    }
    // Static heuristic: if any related test contains an oracle that
    // matches the expected sink class (e.g., return value -> assert_eq!),
    // call it Yes. Otherwise Unknown.
    let any_oracle = related.iter().any(|t| !t.assertions.is_empty());
    let any_matching_sink = related
        .iter()
        .any(|t| oracles_match_sink(&t.assertions, seam.expected_sink()));
    let state = match (any_oracle, any_matching_sink) {
        (true, true) => StageState::Yes,
        (true, false) => StageState::Unknown,
        (false, _) => StageState::Unknown,
    };
    let summary = format!(
        "Static propagation to `{}` sink is {}",
        seam.expected_sink().as_str(),
        state.as_str()
    );
    StageEvidence::new(state, Confidence::Low, summary)
}

fn oracles_match_sink(oracles: &[OracleFact], sink: ExpectedSink) -> bool {
    oracles.iter().any(|oracle| match sink {
        ExpectedSink::ReturnValue | ExpectedSink::OutputField => matches!(
            oracle.kind,
            OracleKind::ExactValue
                | OracleKind::WholeObjectEquality
                | OracleKind::Snapshot
                | OracleKind::RelationalCheck
        ),
        ExpectedSink::ErrorChannel => matches!(
            oracle.kind,
            OracleKind::ExactErrorVariant | OracleKind::BroadError
        ),
        ExpectedSink::SideEffect => matches!(oracle.kind, OracleKind::MockExpectation),
    })
}

fn observe_evidence(related: &[&TestSummary]) -> StageEvidence {
    if related.is_empty() {
        return StageEvidence::new(
            StageState::No,
            Confidence::Medium,
            "No related tests; nothing observes the seam",
        );
    }
    let any_oracle = related.iter().any(|t| !t.assertions.is_empty());
    let any_smoke_only = related.iter().all(|t| {
        !t.assertions.is_empty() && t.assertions.iter().all(|o| o.kind == OracleKind::SmokeOnly)
    });
    let state = if !any_oracle {
        StageState::No
    } else if any_smoke_only {
        StageState::Weak
    } else {
        StageState::Yes
    };
    let summary = format!("Observation evidence is `{}`", state.as_str());
    StageEvidence::new(state, Confidence::Medium, summary)
}

fn discriminate_evidence(seam: &RepoSeam, related: &[&TestSummary]) -> StageEvidence {
    if related.is_empty() {
        return StageEvidence::new(
            StageState::No,
            Confidence::Medium,
            "No related tests; oracle cannot discriminate",
        );
    }
    let mut best = OracleStrength::None;
    let mut best_kind_matches_seam = false;
    for test in related {
        for oracle in &test.assertions {
            if oracle.strength.rank() > best.rank() {
                best = oracle.strength.clone();
            }
            if oracle_kind_matches_seam(seam, &oracle.kind) {
                best_kind_matches_seam = true;
            }
        }
    }
    let state = match (best_kind_matches_seam, &best) {
        (_, OracleStrength::None) => StageState::No,
        (_, OracleStrength::Unknown) => StageState::Unknown,
        (_, OracleStrength::Weak | OracleStrength::Smoke) => StageState::Weak,
        (true, OracleStrength::Strong | OracleStrength::Medium) => StageState::Yes,
        (false, OracleStrength::Strong | OracleStrength::Medium) => StageState::Weak,
    };
    let summary = format!(
        "Strongest oracle for seam kind `{}` is `{}` (kind-match {})",
        seam.kind().as_str(),
        best.as_str(),
        best_kind_matches_seam
    );
    StageEvidence::new(state, Confidence::Medium, summary)
}

fn oracle_kind_matches_seam(seam: &RepoSeam, oracle: &OracleKind) -> bool {
    match seam.kind() {
        SeamKind::PredicateBoundary
        | SeamKind::ReturnValue
        | SeamKind::MatchArm
        | SeamKind::FieldConstruction => matches!(
            oracle,
            OracleKind::ExactValue
                | OracleKind::WholeObjectEquality
                | OracleKind::Snapshot
                | OracleKind::RelationalCheck
        ),
        SeamKind::ErrorVariant => matches!(oracle, OracleKind::ExactErrorVariant),
        SeamKind::SideEffect | SeamKind::CallPresence => {
            matches!(oracle, OracleKind::MockExpectation)
        }
    }
}

pub(crate) fn oracle_semantics_for(
    kind: &OracleKind,
    strength: &OracleStrength,
    seam_kind: SeamKind,
) -> OracleSemantics {
    if matches!(strength, OracleStrength::None) {
        return OracleSemantics {
            observes: "no recognized test oracle".to_string(),
            missing: "an observable discriminator for this seam".to_string(),
            upgrade_suggestion: Some(upgrade_suggestion_for_seam(seam_kind).to_string()),
        };
    }

    match kind {
        OracleKind::ExactValue => OracleSemantics {
            observes: "the exact value or value pattern asserted by the test".to_string(),
            missing: "no obvious value-shape discriminator gap under static scope".to_string(),
            upgrade_suggestion: None,
        },
        OracleKind::ExactErrorVariant => OracleSemantics {
            observes: "the exact error variant".to_string(),
            missing: "error payload details if the changed behavior depends on payload".to_string(),
            upgrade_suggestion: Some(
                "assert the payload inside the matched error variant when payload behavior changed"
                    .to_string(),
            ),
        },
        OracleKind::WholeObjectEquality => OracleSemantics {
            observes: "whole output object equality".to_string(),
            missing:
                "field-specific intent only if the whole-object assertion is too broad to review"
                    .to_string(),
            upgrade_suggestion: None,
        },
        OracleKind::Snapshot => OracleSemantics {
            observes: "a snapshot of rendered or debug output".to_string(),
            missing: "a small explicit discriminator if the snapshot is too broad to review"
                .to_string(),
            upgrade_suggestion: Some(
                "add an exact assertion for the changed field or value when the snapshot is broad"
                    .to_string(),
            ),
        },
        OracleKind::RelationalCheck => OracleSemantics {
            observes: "a partial relationship or broad predicate about the result".to_string(),
            missing: "the exact changed value or boundary discriminator".to_string(),
            upgrade_suggestion: Some(upgrade_suggestion_for_seam(seam_kind).to_string()),
        },
        OracleKind::BroadError => OracleSemantics {
            observes: "some error occurred".to_string(),
            missing:
                "the exact error variant or payload that would discriminate the changed behavior"
                    .to_string(),
            upgrade_suggestion: Some(upgrade_suggestion_for_seam(seam_kind).to_string()),
        },
        OracleKind::SmokeOnly => OracleSemantics {
            observes: "the call completed or returned a broad ok/some/none shape".to_string(),
            missing: "the output value, error variant, field, effect, or call discriminator"
                .to_string(),
            upgrade_suggestion: Some(upgrade_suggestion_for_seam(seam_kind).to_string()),
        },
        OracleKind::MockExpectation => OracleSemantics {
            observes: "an expected call, event, state write, or persistence effect".to_string(),
            missing:
                "effect payload, count, order, or state details if those discriminate the behavior"
                    .to_string(),
            upgrade_suggestion: None,
        },
        OracleKind::Unknown => OracleSemantics {
            observes: "no recognized concrete oracle shape".to_string(),
            missing: "a discriminator assertion for the seam's observable behavior".to_string(),
            upgrade_suggestion: Some(upgrade_suggestion_for_seam(seam_kind).to_string()),
        },
    }
}

fn upgrade_suggestion_for_seam(seam_kind: SeamKind) -> &'static str {
    match seam_kind {
        SeamKind::PredicateBoundary => {
            "add an exact returned-value assertion at the missing boundary value"
        }
        SeamKind::ErrorVariant => "assert the exact error variant with matches! or assert_matches!",
        SeamKind::ReturnValue => "add an exact returned-value assertion for the changed output",
        SeamKind::FieldConstruction => {
            "assert the specific output field that carries the changed behavior"
        }
        SeamKind::SideEffect => {
            "assert the event, state write, persistence effect, or mock expectation payload"
        }
        SeamKind::MatchArm => "assert the exact enum or value produced by the changed match arm",
        SeamKind::CallPresence => "assert the expected call happened with the relevant arguments",
    }
}

fn related_test_grip(
    seam: &RepoSeam,
    test: &TestSummary,
    reason: RelationReason,
) -> RelatedTestGrip {
    let (kind, strength) = best_oracle(test, seam);
    let summary = if matches!(strength, OracleStrength::None) {
        "no oracle in test body".to_string()
    } else {
        match kind {
            OracleKind::ExactValue => "exact value assertion".to_string(),
            OracleKind::ExactErrorVariant => "exact error-variant assertion".to_string(),
            OracleKind::WholeObjectEquality => "whole-object equality".to_string(),
            OracleKind::Snapshot => "snapshot oracle".to_string(),
            OracleKind::RelationalCheck => "relational check".to_string(),
            OracleKind::BroadError => "is_err / broad-error assertion".to_string(),
            OracleKind::SmokeOnly => "smoke-only assertion".to_string(),
            OracleKind::MockExpectation => "mock expectation".to_string(),
            OracleKind::Unknown => "no recognised oracle".to_string(),
        }
    };
    let confidence = reason.confidence();
    RelatedTestGrip {
        test_name: test.name.clone(),
        file: test.file.clone(),
        line: test.start_line,
        oracle_kind: kind,
        oracle_strength: strength,
        evidence_summary: summary,
        relation_reason: reason,
        relation_confidence: confidence,
    }
}

fn best_oracle(test: &TestSummary, seam: &RepoSeam) -> (OracleKind, OracleStrength) {
    let mut best_kind = OracleKind::Unknown;
    let mut best_strength = OracleStrength::None;
    for oracle in &test.assertions {
        if oracle.strength.rank() > best_strength.rank() {
            best_strength = oracle.strength.clone();
            best_kind = oracle.kind.clone();
        } else if oracle.strength.rank() == best_strength.rank()
            && oracle_kind_matches_seam(seam, &oracle.kind)
        {
            best_kind = oracle.kind.clone();
        }
    }
    (best_kind, best_strength)
}

// --- Argument-extraction helpers, lifted from analysis::classifier and
// trimmed to the shape this module needs. The classifier originals stay
// authoritative for diff-scoped findings; copying keeps the seam path
// from getting tangled in `Probe`-flavored helpers.

fn call_arguments(text: &str, callee: &str) -> Option<Vec<String>> {
    let needle = format!("{callee}(");
    let start = text.find(&needle)? + callee.len();
    let inside = delimited_contents_at(text, start)?;
    Some(split_top_level_commas(&inside))
}

fn delimited_contents_at(text: &str, start: usize) -> Option<String> {
    let bytes = text.as_bytes();
    let open = *bytes.get(start)?;
    let close = match open {
        b'(' => b')',
        b'[' => b']',
        b'{' => b'}',
        _ => return None,
    };
    let open = char::from(open);
    let close = char::from(close);
    let mut depth = 0i32;
    let mut in_string = false;
    let mut escaped = false;
    let mut content_start = None;
    for (offset, ch) in text[start..].char_indices() {
        let idx = start + offset;
        if in_string {
            if escaped {
                escaped = false;
            } else if ch == '\\' {
                escaped = true;
            } else if ch == '"' {
                in_string = false;
            }
            continue;
        }
        match ch {
            '"' => in_string = true,
            c if c == open => {
                depth += 1;
                if depth == 1 {
                    content_start = Some(idx + ch.len_utf8());
                }
            }
            c if c == close => {
                depth -= 1;
                if depth == 0 {
                    let content_start = content_start?;
                    return text.get(content_start..idx).map(str::to_string);
                }
            }
            _ => {}
        }
    }
    None
}

fn split_top_level_commas(input: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut depth = 0i32;
    let mut current = String::new();
    for ch in input.chars() {
        match ch {
            '(' | '[' | '{' => {
                depth += 1;
                current.push(ch);
            }
            ')' | ']' | '}' => {
                depth -= 1;
                current.push(ch);
            }
            ',' if depth == 0 => {
                out.push(current.trim().to_string());
                current.clear();
            }
            _ => current.push(ch),
        }
    }
    let trailing = current.trim().to_string();
    if !trailing.is_empty() {
        out.push(trailing);
    }
    out
}

/// Extract literal scalar values from a single call argument.
///
/// Identifiers are intentionally rejected: a value-fact reflects a
/// concrete activation seen at the call site. A bare identifier (e.g.,
/// `amount`, `t`) means the test gets the value through a helper, so
/// the activation is opaque and should not be counted as observed.
fn scalar_values(arg: &str) -> Vec<String> {
    let trimmed = arg.trim().trim_end_matches([',', ';']);
    if trimmed.is_empty() {
        return Vec::new();
    }
    // String / char literal.
    if trimmed.starts_with('"') || trimmed.starts_with('\'') {
        return vec![trimmed.to_string()];
    }
    // Numeric literal (optionally negative, decimal, with `_` separators).
    let numeric_body = trimmed.strip_prefix('-').unwrap_or(trimmed);
    if !numeric_body.is_empty()
        && numeric_body
            .chars()
            .next()
            .is_some_and(|c| c.is_ascii_digit())
        && numeric_body
            .chars()
            .all(|c| c.is_ascii_digit() || c == '_' || c == '.')
    {
        return vec![trimmed.to_string()];
    }
    // Path-shaped enum-variant literal, e.g. `Color::Red` or
    // `AuthError::RevokedToken`. Must contain `::` and otherwise be
    // identifier-shaped.
    if trimmed.contains("::")
        && trimmed
            .chars()
            .all(|c| c.is_alphanumeric() || c == '_' || c == ':')
    {
        return vec![trimmed.to_string()];
    }
    Vec::new()
}

fn sort_value_facts(values: &mut Vec<ValueFact>) {
    values.sort_by(|a, b| {
        a.line
            .cmp(&b.line)
            .then(a.value.cmp(&b.value))
            .then(a.text.cmp(&b.text))
    });
    values.dedup_by(|a, b| a.line == b.line && a.value == b.value && a.text == b.text);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analysis::rust_index::{RaRustSyntaxAdapter, RustSyntaxAdapter};
    use crate::analysis::seam_inventory::inventory_seams_from_index;

    fn index_from_files(files: &[(PathBuf, &str)]) -> Result<RustIndex, String> {
        let adapter = RaRustSyntaxAdapter;
        let mut index = RustIndex::default();
        for (path, source) in files {
            let facts = adapter.summarize_file(path, source)?;
            index.tests.extend(facts.tests.iter().cloned());
            index.functions.extend(facts.functions.iter().cloned());
            index.files.insert(path.clone(), facts);
        }
        Ok(index)
    }

    #[test]
    fn latency_trace_line_uses_repo_exposure_trace_shape() {
        let line = latency_trace_line(
            "evidence_for_seams_progress",
            "processed_500_of_12337",
            Duration::from_millis(42),
        );

        assert_eq!(
            line,
            "ripr_repo_exposure_latency phase=evidence_for_seams_progress status=processed_500_of_12337 duration_ms=42"
        );
    }

    #[test]
    fn oracle_semantics_explains_broad_error_gap_and_upgrade() {
        let semantics = oracle_semantics_for(
            &OracleKind::BroadError,
            &OracleStrength::Weak,
            SeamKind::ErrorVariant,
        );

        assert_eq!(semantics.observes, "some error occurred");
        assert_eq!(
            semantics.missing,
            "the exact error variant or payload that would discriminate the changed behavior"
        );
        assert_eq!(
            semantics.upgrade_suggestion.as_deref(),
            Some("assert the exact error variant with matches! or assert_matches!")
        );
    }

    #[test]
    fn oracle_semantics_explains_smoke_only_boundary_gap() {
        let semantics = oracle_semantics_for(
            &OracleKind::SmokeOnly,
            &OracleStrength::Smoke,
            SeamKind::PredicateBoundary,
        );

        assert_eq!(
            semantics.observes,
            "the call completed or returned a broad ok/some/none shape"
        );
        assert_eq!(
            semantics.missing,
            "the output value, error variant, field, effect, or call discriminator"
        );
        assert_eq!(
            semantics.upgrade_suggestion.as_deref(),
            Some("add an exact returned-value assertion at the missing boundary value")
        );
    }

    #[test]
    fn oracle_semantics_keeps_exact_value_without_extra_upgrade() {
        let semantics = oracle_semantics_for(
            &OracleKind::ExactValue,
            &OracleStrength::Strong,
            SeamKind::ReturnValue,
        );

        assert_eq!(
            semantics.observes,
            "the exact value or value pattern asserted by the test"
        );
        assert_eq!(
            semantics.missing,
            "no obvious value-shape discriminator gap under static scope"
        );
        assert!(semantics.upgrade_suggestion.is_none());
    }

    #[test]
    fn oracle_semantics_covers_supported_oracle_families() {
        let cases = [
            (
                OracleKind::ExactErrorVariant,
                OracleStrength::Strong,
                SeamKind::ErrorVariant,
                "the exact error variant",
                Some(
                    "assert the payload inside the matched error variant when payload behavior changed",
                ),
            ),
            (
                OracleKind::WholeObjectEquality,
                OracleStrength::Strong,
                SeamKind::ReturnValue,
                "whole output object equality",
                None,
            ),
            (
                OracleKind::Snapshot,
                OracleStrength::Medium,
                SeamKind::ReturnValue,
                "a snapshot of rendered or debug output",
                Some(
                    "add an exact assertion for the changed field or value when the snapshot is broad",
                ),
            ),
            (
                OracleKind::RelationalCheck,
                OracleStrength::Weak,
                SeamKind::MatchArm,
                "a partial relationship or broad predicate about the result",
                Some("assert the exact enum or value produced by the changed match arm"),
            ),
            (
                OracleKind::MockExpectation,
                OracleStrength::Medium,
                SeamKind::SideEffect,
                "an expected call, event, state write, or persistence effect",
                None,
            ),
            (
                OracleKind::Unknown,
                OracleStrength::Unknown,
                SeamKind::CallPresence,
                "no recognized concrete oracle shape",
                Some("assert the expected call happened with the relevant arguments"),
            ),
            (
                OracleKind::Unknown,
                OracleStrength::None,
                SeamKind::FieldConstruction,
                "no recognized test oracle",
                Some("assert the specific output field that carries the changed behavior"),
            ),
        ];

        for (kind, strength, seam_kind, observes, upgrade) in cases {
            let semantics = oracle_semantics_for(&kind, &strength, seam_kind);
            assert_eq!(semantics.observes, observes);
            assert_eq!(semantics.upgrade_suggestion.as_deref(), upgrade);
        }
    }

    #[test]
    fn given_boundary_seam_when_tests_skip_equal_value_then_evidence_reports_missing_boundary_discriminator()
    -> Result<(), String> {
        // Production predicate compares amount >= threshold.
        let prod = PathBuf::from("src/pricing.rs");
        let prod_src = r#"
pub fn discounted_total(amount: i32, threshold: i32) -> i32 {
    if amount >= threshold { amount - 10 } else { amount }
}
"#;
        // Test calls owner with values strictly above and strictly below
        // the threshold but never with the equality case.
        let tests = PathBuf::from("tests/pricing_tests.rs");
        let tests_src = r#"
#[test]
fn below_threshold_has_no_discount() {
    assert_eq!(discounted_total(50, 100), 50);
}

#[test]
fn far_above_threshold_discounts() {
    assert_eq!(discounted_total(10000, 100), 9990);
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "expected predicate seam".to_string())?;

        let evidence = evidence_for_seam(predicate, &index);
        if evidence.related_tests.is_empty() {
            return Err("expected reach evidence to find related tests".to_string());
        }
        if evidence.missing_discriminators.is_empty() {
            return Err(format!(
                "expected at least one missing-discriminator hypothesis for boundary seam `{}`",
                predicate.expression()
            ));
        }
        let mentions_threshold = evidence
            .missing_discriminators
            .iter()
            .any(|fact| fact.value.contains("threshold"));
        if !mentions_threshold {
            return Err(format!(
                "missing-discriminator hypothesis should name the boundary identifier; got {:?}",
                evidence
                    .missing_discriminators
                    .iter()
                    .map(|f| f.value.clone())
                    .collect::<Vec<_>>()
            ));
        }
        Ok(())
    }

    #[test]
    fn given_boundary_seam_when_test_uses_equal_value_and_exact_assertion_then_discriminate_evidence_is_yes()
    -> Result<(), String> {
        let prod = PathBuf::from("src/pricing.rs");
        let prod_src = r#"
pub fn discounted_total(amount: i32, threshold: i32) -> i32 {
    if amount >= threshold { amount - 10 } else { amount }
}
"#;
        let tests = PathBuf::from("tests/pricing_tests.rs");
        let tests_src = r#"
#[test]
fn equality_boundary_returns_discount() {
    assert_eq!(discounted_total(100, 100), 90);
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "expected predicate seam".to_string())?;

        let evidence = evidence_for_seam(predicate, &index);
        if evidence.discriminate.state != StageState::Yes {
            return Err(format!(
                "expected discriminate=Yes, got {} ({})",
                evidence.discriminate.state.as_str(),
                evidence.discriminate.summary
            ));
        }
        Ok(())
    }

    #[test]
    fn given_error_variant_seam_when_test_only_asserts_is_err_then_discriminate_evidence_is_weak()
    -> Result<(), String> {
        let prod = PathBuf::from("src/parse.rs");
        let prod_src = r#"
pub enum AuthError { RevokedToken, Expired }

pub fn parse(value: &str) -> Result<i32, AuthError> {
    if value.is_empty() {
        return Err(AuthError::RevokedToken);
    }
    Ok(0)
}
"#;
        let tests = PathBuf::from("tests/parse_tests.rs");
        let tests_src = r#"
#[test]
fn parse_rejects_empty() {
    assert!(parse("").is_err());
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/parse.rs")], &index);
        let error_seam = seams
            .iter()
            .find(|s| s.kind() == SeamKind::ErrorVariant)
            .ok_or_else(|| "expected error_variant seam".to_string())?;

        let evidence = evidence_for_seam(error_seam, &index);
        if evidence.discriminate.state != StageState::Weak
            && evidence.discriminate.state != StageState::Unknown
        {
            return Err(format!(
                "expected discriminate=Weak|Unknown for is_err-only oracle, got {}",
                evidence.discriminate.state.as_str()
            ));
        }
        Ok(())
    }

    #[test]
    fn given_error_variant_seam_when_test_asserts_exact_variant_then_discriminate_evidence_is_yes()
    -> Result<(), String> {
        let prod = PathBuf::from("src/parse.rs");
        let prod_src = r#"
pub enum AuthError { RevokedToken, Expired }

pub fn parse(value: &str) -> Result<i32, AuthError> {
    if value.is_empty() {
        return Err(AuthError::RevokedToken);
    }
    Ok(0)
}
"#;
        let tests = PathBuf::from("tests/parse_tests.rs");
        let tests_src = r#"
#[test]
fn parse_returns_revoked_token_on_empty() {
    assert!(matches!(parse(""), Err(AuthError::RevokedToken)));
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/parse.rs")], &index);
        let error_seam = seams
            .iter()
            .find(|s| s.kind() == SeamKind::ErrorVariant)
            .ok_or_else(|| "expected error_variant seam".to_string())?;

        let evidence = evidence_for_seam(error_seam, &index);
        if evidence.discriminate.state != StageState::Yes {
            return Err(format!(
                "expected discriminate=Yes for matches!(...AuthError::RevokedToken), got {} ({})",
                evidence.discriminate.state.as_str(),
                evidence.discriminate.summary
            ));
        }
        Ok(())
    }

    #[test]
    fn given_side_effect_seam_when_no_effect_observer_exists_then_observe_evidence_is_weak_or_unknown()
    -> Result<(), String> {
        let prod = PathBuf::from("src/publish.rs");
        // The production function calls `service.publish(...)` — a method
        // whose name matches `is_effect_call_name`, so the parser emits
        // a side_effect probe shape on the call site.
        let prod_src = r#"
pub struct Service;
pub struct Event;

impl Service {
    pub fn publish(&mut self, _event: Event) {}
}

pub fn publish_message(service: &mut Service, event: Event) {
    service.publish(event);
}
"#;
        let tests = PathBuf::from("tests/publish_tests.rs");
        // Test reaches `publish_message` but does not observe the
        // side-effect (no mock, no assertion that the publish happened).
        let tests_src = r#"
#[test]
fn publish_runs_without_panic() {
    let mut service = Service;
    publish_message(&mut service, Event);
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/publish.rs")], &index);
        let side_effect = seams
            .iter()
            .find(|s| s.kind() == SeamKind::SideEffect)
            .ok_or_else(|| {
                format!(
                    "expected side_effect seam, got kinds: {:?}",
                    seams.iter().map(|s| s.kind().as_str()).collect::<Vec<_>>()
                )
            })?;

        let evidence = evidence_for_seam(side_effect, &index);
        match evidence.observe.state {
            StageState::No | StageState::Weak | StageState::Unknown => Ok(()),
            other => Err(format!(
                "expected observe in {{No, Weak, Unknown}} for side-effect with no observer, got {}",
                other.as_str()
            )),
        }
    }

    #[test]
    fn given_side_effect_seam_when_event_assertion_exists_then_oracle_observes_effect()
    -> Result<(), String> {
        let prod = PathBuf::from("src/publish.rs");
        let prod_src = r#"
pub struct Service;
pub struct Event;

impl Service {
    pub fn publish(&mut self, _event: Event) {}
}

pub fn publish_message(service: &mut Service, event: Event) {
    service.publish(event);
}
"#;
        let tests = PathBuf::from("tests/publish_tests.rs");
        let tests_src = r#"
#[test]
fn publish_records_event() {
    let mut service = Service;
    publish_message(&mut service, Event);
    assert!(service.published_events().contains(&"message"));
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/publish.rs")], &index);
        let side_effect = seams
            .iter()
            .find(|s| s.kind() == SeamKind::SideEffect)
            .ok_or_else(|| "expected side_effect seam".to_string())?;

        let evidence = evidence_for_seam(side_effect, &index);
        assert_eq!(evidence.observe.state, StageState::Yes);
        assert_eq!(evidence.propagate.state, StageState::Yes);
        assert_eq!(evidence.discriminate.state, StageState::Yes);
        assert!(
            evidence
                .related_tests
                .iter()
                .any(|test| test.oracle_kind == OracleKind::MockExpectation)
        );
        Ok(())
    }

    #[test]
    fn given_opaque_helper_when_values_cannot_be_seen_then_evidence_records_static_limitation()
    -> Result<(), String> {
        // Test reaches the owner only through a helper, so no concrete
        // activation values are visible. Activation should not be Yes.
        let prod = PathBuf::from("src/pricing.rs");
        let prod_src = r#"
pub fn discounted_total(amount: i32, threshold: i32) -> i32 {
    if amount >= threshold { amount - 10 } else { amount }
}
"#;
        let tests = PathBuf::from("tests/pricing_tests.rs");
        let tests_src = r#"
fn make_input() -> (i32, i32) { (50, 100) }

#[test]
fn helper_path_runs() {
    let (a, t) = make_input();
    let _ = discounted_total(a, t);
    assert!(true);
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "expected predicate seam".to_string())?;

        let evidence = evidence_for_seam(predicate, &index);
        if evidence.activate.state == StageState::Yes {
            return Err(format!(
                "expected activate != Yes for helper-supplied values, got {} ({})",
                evidence.activate.state.as_str(),
                evidence.activate.summary
            ));
        }
        Ok(())
    }

    #[test]
    fn evidence_for_seams_is_deterministic_across_input_order() -> Result<(), String> {
        let prod = PathBuf::from("src/pricing.rs");
        let prod_src = r#"
pub fn discounted_total(amount: i32, threshold: i32) -> i32 {
    if amount >= threshold { amount - 10 } else { amount }
}
"#;
        let tests = PathBuf::from("tests/pricing_tests.rs");
        let tests_src = r#"
#[test]
fn boundary_case() {
    assert_eq!(discounted_total(100, 100), 90);
}
#[test]
fn below_case() {
    assert_eq!(discounted_total(50, 100), 50);
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let mut seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let forward_ids: Vec<String> = evidence_for_seams(&seams, &index)
            .iter()
            .map(|e| e.seam_id.as_str().to_string())
            .collect();
        seams.reverse();
        let reversed_ids: Vec<String> = evidence_for_seams(&seams, &index)
            .iter()
            .map(|e| e.seam_id.as_str().to_string())
            .collect();
        if forward_ids != reversed_ids {
            return Err(format!(
                "evidence order is not stable:\n  forward: {forward_ids:?}\n  reversed: {reversed_ids:?}"
            ));
        }
        Ok(())
    }

    #[test]
    fn evidence_for_seams_matches_single_seam_evidence_while_reusing_context() -> Result<(), String>
    {
        let prod = PathBuf::from("src/pricing.rs");
        let prod_src = r#"
pub fn discounted_total(amount: i32, threshold: i32) -> i32 {
    if amount >= threshold { amount - 10 } else { amount }
}
"#;
        let tests = PathBuf::from("tests/pricing_tests.rs");
        let tests_src = r#"
#[test]
fn equality_boundary_returns_discount() {
    assert_eq!(discounted_total(100, 100), 90);
}
#[test]
fn import_only_mentions_owner() {
    use crate::pricing::discounted_total;
    assert_eq!(1, 1);
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let batch = evidence_for_seams(&seams, &index);

        for seam in &seams {
            let single = evidence_for_seam(seam, &index);
            let Some(from_batch) = batch.iter().find(|entry| entry.seam_id == *seam.id()) else {
                return Err(format!(
                    "batch evidence missing seam {}",
                    seam.id().as_str()
                ));
            };
            let single_json =
                serde_json::to_string(&single).map_err(|err| format!("encode single: {err}"))?;
            let batch_json =
                serde_json::to_string(from_batch).map_err(|err| format!("encode batch: {err}"))?;
            assert_eq!(single_json, batch_json);
        }
        Ok(())
    }

    #[test]
    fn given_compact_evidence_when_direct_owner_call_reaches_error_seam_then_activation_is_yes()
    -> Result<(), String> {
        let prod = PathBuf::from("src/parse.rs");
        let prod_src = r#"
pub enum AuthError { RevokedToken, Expired }

pub fn parse(value: &str) -> Result<i32, AuthError> {
    if value.is_empty() {
        return Err(AuthError::RevokedToken);
    }
    Ok(0)
}
"#;
        let tests = PathBuf::from("tests/parse_tests.rs");
        let tests_src = r#"
#[test]
fn parse_rejects_empty() {
    assert!(parse("").is_err());
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/parse.rs")], &index);
        let error_seam = seams
            .iter()
            .find(|s| s.kind() == SeamKind::ErrorVariant)
            .ok_or_else(|| "expected error_variant seam".to_string())?;
        let context = CompactGripContext::new(&index);

        let evidence = compact_evidence_for_seam(error_seam, &context);

        assert_eq!(evidence.reach.state, StageState::Yes);
        assert_eq!(evidence.activate.state, StageState::Yes);
        assert_eq!(evidence.related_tests.len(), 0);
        assert_eq!(evidence.observed_values.len(), 0);
        assert_eq!(evidence.missing_discriminators.len(), 0);
        Ok(())
    }

    #[test]
    fn given_compact_evidence_when_import_affinity_has_no_owner_call_then_activation_is_unknown()
    -> Result<(), String> {
        let prod = PathBuf::from("src/parse.rs");
        let prod_src = r#"
pub enum AuthError { RevokedToken, Expired }

pub fn parse(value: &str) -> Result<i32, AuthError> {
    if value.is_empty() {
        return Err(AuthError::RevokedToken);
    }
    Ok(0)
}
"#;
        let tests = PathBuf::from("tests/wrapper_tests.rs");
        let tests_src = r#"
fn helper() -> Result<i32, AuthError> { Err(AuthError::RevokedToken) }

#[test]
fn wrapper_rejects_empty() {
    use crate::parse;
    assert!(helper().is_err());
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/parse.rs")], &index);
        let error_seam = seams
            .iter()
            .find(|s| s.kind() == SeamKind::ErrorVariant)
            .ok_or_else(|| "expected error_variant seam".to_string())?;
        let context = CompactGripContext::new(&index);

        let related = find_related_tests_compact(error_seam, &context);
        assert_eq!(related.len(), 1);
        assert_eq!(related[0].test.name, "wrapper_rejects_empty");

        let evidence = compact_evidence_for_seam(error_seam, &context);
        assert_eq!(evidence.reach.state, StageState::Yes);
        assert_eq!(evidence.activate.state, StageState::Unknown);
        Ok(())
    }

    #[test]
    fn given_compact_related_tests_when_more_than_limit_match_then_results_are_capped()
    -> Result<(), String> {
        let prod = PathBuf::from("src/pricing.rs");
        let prod_src = r#"
pub fn discounted_total(amount: i32, threshold: i32) -> i32 {
    if amount >= threshold { amount - 10 } else { amount }
}
"#;
        let mut tests_src = String::new();
        for idx in 0..14 {
            tests_src.push_str(&format!(
                "#[test]\nfn direct_{idx:02}() {{ assert_eq!(discounted_total(100, 100), 90); }}\n"
            ));
        }
        let tests = PathBuf::from("tests/pricing_tests.rs");
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src.as_str())])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "expected predicate seam".to_string())?;
        let context = CompactGripContext::new(&index);

        let related = find_related_tests_compact(predicate, &context);

        assert_eq!(related.len(), COMPACT_RELATED_TEST_LIMIT);
        assert_eq!(related[0].test.name, "direct_00");
        assert_eq!(
            related[COMPACT_RELATED_TEST_LIMIT - 1].test.name,
            "direct_11"
        );
        Ok(())
    }

    #[test]
    fn given_compact_import_affinity_when_owner_only_in_comment_or_string_then_no_relation_is_found()
    -> Result<(), String> {
        let prod = PathBuf::from("src/parse.rs");
        let prod_src = r#"
pub enum AuthError { RevokedToken, Expired }

pub fn parse(value: &str) -> Result<i32, AuthError> {
    if value.is_empty() {
        return Err(AuthError::RevokedToken);
    }
    Ok(0)
}
"#;
        let tests = PathBuf::from("tests/noise_tests.rs");
        let tests_src = r#"
#[test]
fn wrapper_mentions_owner_only_in_non_code() {
    // use crate::parse;
    let _path = "crate::parse";
    assert!(helper().is_err());
}
"#;
        let index = index_from_files(&[(prod, prod_src), (tests, tests_src)])?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/parse.rs")], &index);
        let error_seam = seams
            .iter()
            .find(|s| s.kind() == SeamKind::ErrorVariant)
            .ok_or_else(|| "expected error_variant seam".to_string())?;
        let context = CompactGripContext::new(&index);

        let related = find_related_tests_compact(error_seam, &context);

        assert_eq!(related.len(), 0);
        Ok(())
    }

    // -- relation_reason / relation_confidence ranking ----------------
    //
    // Pins the ranking contract:
    //   confidence (high first) → reason priority → file → name → line.
    // Reason detection is exercised here through `find_related_tests`
    // via `evidence_for_seam`. Each test fabricates a small index and
    // inspects the first emitted RelatedTestGrip per seam.

    fn first_grip_for(
        seam_file: &str,
        prod_src: &str,
        tests: &[(&str, &str)],
    ) -> Result<RelatedTestGrip, String> {
        let mut files: Vec<(PathBuf, &str)> = vec![(PathBuf::from(seam_file), prod_src)];
        for (path, src) in tests {
            files.push((PathBuf::from(*path), *src));
        }
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(&[PathBuf::from(seam_file)], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);
        evidence
            .related_tests
            .into_iter()
            .next()
            .ok_or_else(|| "at least one related test".to_string())
    }

    #[test]
    fn given_direct_owner_call_and_same_file_match_when_related_tests_are_ranked_then_direct_call_is_first()
    -> Result<(), String> {
        // One test in the same file (would match same_test_file) plus
        // one that calls the owner directly. Ranking must put the
        // direct-call test first.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        // Test in pricing_tests.rs has the same file stem as src/pricing.rs.
        let same_file_only = (
            "tests/pricing_tests.rs",
            "#[test] fn pricing_smoke() { assert_eq!(1, 1); }\n",
        );
        // Test in unrelated.rs calls the owner directly.
        let direct = (
            "tests/unrelated.rs",
            "#[test] fn calls_owner() { assert_eq!(discounted_total(100, 100), 90); }\n",
        );

        let files: Vec<(PathBuf, &str)> = vec![
            (PathBuf::from("src/pricing.rs"), prod_src),
            (PathBuf::from(same_file_only.0), same_file_only.1),
            (PathBuf::from(direct.0), direct.1),
        ];
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);

        let first = evidence
            .related_tests
            .first()
            .ok_or_else(|| "at least one related test".to_string())?;
        let labels: Vec<_> = evidence
            .related_tests
            .iter()
            .map(|g| (g.test_name.clone(), g.relation_reason))
            .collect();
        assert_eq!(
            first.relation_reason,
            RelationReason::DirectOwnerCall,
            "direct owner call must outrank same-file affinity; got grips {labels:?}"
        );
        assert_eq!(first.relation_confidence, RelationConfidence::High);
        Ok(())
    }

    #[test]
    fn given_owner_named_test_without_call_when_related_tests_are_ranked_then_confidence_is_medium()
    -> Result<(), String> {
        // Test name embeds the owner name but does not call it and is
        // not in the same module / file. Should classify as
        // owner_named_test with medium confidence.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/billing.rs",
            "#[test] fn discounted_total_smoke() { assert_eq!(1, 1); }\n",
        );
        let grip = first_grip_for("src/pricing.rs", prod_src, &[test])?;
        assert_eq!(grip.relation_reason, RelationReason::OwnerNamedTest);
        assert_eq!(grip.relation_confidence, RelationConfidence::Medium);
        Ok(())
    }

    #[test]
    fn given_fixture_only_affinity_when_related_tests_are_ranked_then_confidence_is_low()
    -> Result<(), String> {
        // Test calls a fixture-named helper in the owner's source file
        // but never the owner itself, and the test name does not embed
        // the owner. Should classify as fixture_owner_affinity with
        // exactly Low confidence (Opaque is reserved for cases the
        // detector does not yet emit).
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n\
                        pub fn make_quote() -> i32 { 100 }\n";
        let test = (
            "tests/integration.rs",
            "#[test] fn quote_smoke() { let _ = make_quote(); assert!(true); }\n",
        );
        let grip = first_grip_for("src/pricing.rs", prod_src, &[test])?;
        assert_eq!(grip.relation_reason, RelationReason::FixtureOwnerAffinity);
        assert_eq!(grip.relation_confidence, RelationConfidence::Low);
        Ok(())
    }

    #[test]
    fn given_assertion_target_affinity_uses_token_aware_match_not_substring() -> Result<(), String>
    {
        // The seam's required-discriminator description contains the
        // identifier `discount_threshold`. A test whose assertion uses
        // `discount_threshold_factor` (a longer identifier that contains
        // the discriminator string as a substring) must NOT be
        // classified as assertion_target_affinity — token-aware matching
        // requires whole-identifier hits, not substring contains.
        //
        // The test calls a different function (no direct_owner_call)
        // and lives in an unrelated file (no same_test_file/module),
        // and its name does not embed the owner.
        let prod_src = "pub fn discounted_total(amount: i32, discount_threshold: i32) -> i32 \
                        { if amount >= discount_threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/billing.rs",
            "fn other() -> i32 { 0 }\n\
             #[test] fn smoke() { let discount_threshold_factor = 5; assert_eq!(other(), 0); let _ = discount_threshold_factor; }\n",
        );
        let files: Vec<(PathBuf, &str)> = vec![
            (PathBuf::from("src/pricing.rs"), prod_src),
            (PathBuf::from(test.0), test.1),
        ];
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);
        // The test must not appear as assertion_target_affinity. It is
        // OK for it to be excluded entirely (no reason fires) — the
        // contract is "do not falsely classify substring hits".
        for grip in &evidence.related_tests {
            assert_ne!(
                grip.relation_reason,
                RelationReason::AssertionTargetAffinity,
                "substring hit (`discount_threshold_factor`) must not match \
                 assertion_target_affinity; got {grip:?}"
            );
        }
        Ok(())
    }

    #[test]
    fn given_related_tests_with_same_confidence_when_sorted_then_order_is_stable_by_file_name_line()
    -> Result<(), String> {
        // Two tests with the same reason (both owner_named_test) but
        // different (file, name). Sort tie-break must be deterministic:
        // file → name → line.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test_a = (
            "tests/zeta.rs",
            "#[test] fn discounted_total_one() { assert_eq!(1, 1); }\n",
        );
        let test_b = (
            "tests/alpha.rs",
            "#[test] fn discounted_total_two() { assert_eq!(1, 1); }\n",
        );
        let files: Vec<(PathBuf, &str)> = vec![
            (PathBuf::from("src/pricing.rs"), prod_src),
            (PathBuf::from(test_a.0), test_a.1),
            (PathBuf::from(test_b.0), test_b.1),
        ];
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);
        assert!(
            evidence.related_tests.len() >= 2,
            "expected at least 2 related tests, got {}",
            evidence.related_tests.len()
        );
        // alpha.rs sorts before zeta.rs.
        assert_eq!(evidence.related_tests[0].file, Path::new("tests/alpha.rs"));
        assert_eq!(evidence.related_tests[1].file, Path::new("tests/zeta.rs"));
        Ok(())
    }

    #[test]
    fn given_higher_confidence_related_test_when_sorted_then_it_comes_before_lower_confidence()
    -> Result<(), String> {
        // Two tests, one with high confidence (direct_owner_call) and
        // one with low confidence (fixture_owner_affinity via a fixture
        // helper). High must come first regardless of file/name order.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n\
                        pub fn make_quote() -> i32 { 100 }\n";
        // The fixture user lives in 'a_first.rs' (alphabetically before)
        // so without confidence ordering it would naively sort first.
        let fixture_user = (
            "tests/a_first.rs",
            "#[test] fn fx() { let _ = make_quote(); assert!(true); }\n",
        );
        // The direct caller lives in 'z_last.rs'.
        let direct_caller = (
            "tests/z_last.rs",
            "#[test] fn caller() { assert_eq!(discounted_total(100, 100), 90); }\n",
        );
        let files: Vec<(PathBuf, &str)> = vec![
            (PathBuf::from("src/pricing.rs"), prod_src),
            (PathBuf::from(fixture_user.0), fixture_user.1),
            (PathBuf::from(direct_caller.0), direct_caller.1),
        ];
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);
        let first = evidence
            .related_tests
            .first()
            .ok_or_else(|| "at least one related test".to_string())?;
        assert_eq!(first.relation_reason, RelationReason::DirectOwnerCall);
        assert_eq!(first.relation_confidence, RelationConfidence::High);
        Ok(())
    }

    #[test]
    fn given_related_tests_with_same_relation_when_ranked_then_strong_oracle_precedes_smoke_oracle()
    -> Result<(), String> {
        // Both tests are direct owner calls. The strong exact-value
        // oracle lives in an alphabetically later file, so the v2
        // ranking must use oracle strength before file/name tie-breaks.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> Result<i32, ()> \
                        { if amount >= threshold { Ok(amount - 10) } else { Ok(amount) } }\n";
        let smoke = (
            "tests/a_smoke.rs",
            "#[test] fn smoke_owner_call() { assert!(discounted_total(100, 100).is_ok()); }\n",
        );
        let strong = (
            "tests/z_exact.rs",
            "#[test] fn exact_owner_call() { assert_eq!(discounted_total(100, 100).unwrap(), 90); }\n",
        );
        let files: Vec<(PathBuf, &str)> = vec![
            (PathBuf::from("src/pricing.rs"), prod_src),
            (PathBuf::from(smoke.0), smoke.1),
            (PathBuf::from(strong.0), strong.1),
        ];
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);
        let first = evidence
            .related_tests
            .first()
            .ok_or_else(|| "at least one related test".to_string())?;

        assert_eq!(first.test_name, "exact_owner_call");
        assert_eq!(first.relation_reason, RelationReason::DirectOwnerCall);
        assert_eq!(first.oracle_strength, OracleStrength::Strong);
        Ok(())
    }

    #[test]
    fn given_related_tests_with_same_relation_and_oracle_when_ranked_then_activation_overlap_precedes_file_order()
    -> Result<(), String> {
        // Both tests are direct owner calls with strong exact-value
        // oracles. The equality-boundary call lives in an
        // alphabetically later file; it should still be the nearest
        // imitation target because its activation values overlap the
        // predicate boundary.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let above = (
            "tests/a_above.rs",
            "#[test] fn above_boundary() { let actual = discounted_total(101, 100); assert_eq!(actual, 91); }\n",
        );
        let equality = (
            "tests/z_equal.rs",
            "#[test] fn equality_boundary() { let actual = discounted_total(100, 100); assert_eq!(actual, 90); }\n",
        );
        let files: Vec<(PathBuf, &str)> = vec![
            (PathBuf::from("src/pricing.rs"), prod_src),
            (PathBuf::from(above.0), above.1),
            (PathBuf::from(equality.0), equality.1),
        ];
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);
        let first = evidence
            .related_tests
            .first()
            .ok_or_else(|| "at least one related test".to_string())?;

        assert_eq!(first.test_name, "equality_boundary");
        assert_eq!(first.relation_reason, RelationReason::DirectOwnerCall);
        assert_eq!(first.oracle_strength, OracleStrength::Strong);
        Ok(())
    }

    // -- import_path_affinity tightening (#310 review) ---------------
    //
    // The detector requires explicit `module::owner_name` qualified-
    // path syntax or an inline `use ... owner_name` line — pure token
    // co-occurrence (owner_name + module token both present in the
    // body without path syntax) must NOT fire.

    #[test]
    fn given_import_path_affinity_without_direct_call_when_related_tests_are_ranked_then_confidence_is_medium()
    -> Result<(), String> {
        // Test references `crate::pricing::discounted_total` as a
        // function value (no parens → not a CallFact, so
        // direct_owner_call cannot fire). The qualified path satisfies
        // the tightened import_path_affinity detector. The test name
        // does not contain "discounted_total" and the file is not
        // pricing-flavoured, so no other reason fires either.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/integration_smoke.rs",
            "#[test] fn smoke() { let _f = crate::pricing::discounted_total; assert_eq!(1, 1); }\n",
        );
        let grip = first_grip_for("src/pricing.rs", prod_src, &[test])?;
        assert_eq!(grip.relation_reason, RelationReason::ImportPathAffinity);
        assert_eq!(grip.relation_confidence, RelationConfidence::Medium);
        Ok(())
    }

    #[test]
    fn given_qualified_owner_path_only_in_comment_or_string_when_related_tests_are_ranked_then_import_path_affinity_does_not_fire()
    -> Result<(), String> {
        // Per CodeRabbit on #310: `test_imports_owner` previously did
        // a raw `body.contains("::owner")` which matched substrings
        // inside `// ...` comments and `"..."` string literals. That
        // re-introduced the noise the detector was meant to avoid.
        // After the fix, neither shape should match.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        // Comment carries the qualified path; code does not. Test name
        // and file are both neutral so no other reason fires.
        let comment_only = (
            "tests/integration_a.rs",
            "#[test] fn smoke_a() { \
                // see crate::pricing::discounted_total for background \n\
                assert_eq!(1, 1); \
            }\n",
        );
        // String literal carries the qualified path.
        let string_only = (
            "tests/integration_b.rs",
            "#[test] fn smoke_b() { \
                let _doc = \"crate::pricing::discounted_total\"; \
                let _ = _doc; assert_eq!(1, 1); \
            }\n",
        );
        for (path, src) in [comment_only, string_only] {
            let files: Vec<(PathBuf, &str)> = vec![
                (PathBuf::from("src/pricing.rs"), prod_src),
                (PathBuf::from(path), src),
            ];
            let index = index_from_files(&files)?;
            let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
            let predicate = seams
                .iter()
                .find(|s| s.kind() == SeamKind::PredicateBoundary)
                .ok_or_else(|| "predicate seam present".to_string())?;
            let evidence = evidence_for_seam(predicate, &index);
            for grip in &evidence.related_tests {
                assert_ne!(
                    grip.relation_reason,
                    RelationReason::ImportPathAffinity,
                    "qualified path inside comment/string in {path} must not match \
                     ImportPathAffinity; got {grip:?}"
                );
            }
        }
        Ok(())
    }

    #[test]
    fn given_owner_and_module_tokens_without_import_path_when_related_tests_are_ranked_then_import_path_affinity_does_not_fire()
    -> Result<(), String> {
        // Body contains `pricing` and `discounted_total` as bare
        // identifiers but never as a `::path::owner_name` shape and
        // never on a `use ...` line. The pre-tightening detector
        // would have fired (owner token + parent dir token both
        // present); the tightened detector must not.
        //
        // The test name embeds "discounted_total" — that is OK because
        // it triggers `owner_named_test`, a *different* reason. The
        // contract under test is "ImportPathAffinity does not fire on
        // mere token co-occurrence".
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/billing.rs",
            "#[test] fn discounted_total_token_smoke() { \
                let pricing = \"pricing\"; let discounted_total = 5; \
                let _ = (pricing, discounted_total); assert_eq!(1, 1); \
            }\n",
        );
        let files: Vec<(PathBuf, &str)> = vec![
            (PathBuf::from("src/pricing.rs"), prod_src),
            (PathBuf::from(test.0), test.1),
        ];
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);
        for grip in &evidence.related_tests {
            assert_ne!(
                grip.relation_reason,
                RelationReason::ImportPathAffinity,
                "token co-occurrence (`pricing` + `discounted_total` in body without \
                 `::` path syntax) must not match ImportPathAffinity; got {grip:?}"
            );
        }
        Ok(())
    }

    #[test]
    fn given_same_module_test_without_direct_call_when_related_tests_are_ranked_then_confidence_is_medium()
    -> Result<(), String> {
        // Owner sits in `src/pricing/discount.rs`; test sits in
        // `tests/pricing/integration.rs`. Different file stem (no
        // same_test_file). Same parent module (`pricing`) so
        // `same_module` is the right reason. No direct call, no
        // owner-named test, no qualified path / use line.
        let prod_src = "pub fn apply_discount(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/pricing/integration.rs",
            "#[test] fn module_neighbour() { assert_eq!(1, 1); }\n",
        );
        let files: Vec<(PathBuf, &str)> = vec![
            (PathBuf::from("src/pricing/discount.rs"), prod_src),
            (PathBuf::from(test.0), test.1),
        ];
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing/discount.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);
        let grip = evidence.related_tests.first().ok_or_else(|| {
            "expected at least one related test for same-module pairing".to_string()
        })?;
        assert_eq!(grip.relation_reason, RelationReason::SameModule);
        assert_eq!(grip.relation_confidence, RelationConfidence::Medium);
        Ok(())
    }

    // -- helper coverage ---------------------------------------------
    //
    // Targeted unit tests for the small private helpers introduced by
    // analysis/related-test-precision-v1. The integration BDD tests
    // above exercise the most common paths through `find_related_tests`,
    // but each helper has a few branches that are not naturally hit by
    // a single BDD scenario. The tests below pin those branches so
    // codecov coverage reflects intent rather than scenario count.

    #[test]
    fn relation_reason_as_str_priority_and_confidence_are_pinned_per_variant() {
        // Pin the (variant -> "string", priority, confidence) mapping
        // for every reason. Catches accidental swaps in the match arms
        // of `as_str` / `priority` / `confidence`.
        let table = [
            (
                RelationReason::DirectOwnerCall,
                "direct_owner_call",
                0u8,
                RelationConfidence::High,
            ),
            (
                RelationReason::AssertionTargetAffinity,
                "assertion_target_affinity",
                1,
                RelationConfidence::High,
            ),
            (
                RelationReason::SameTestFile,
                "same_test_file",
                2,
                RelationConfidence::Medium,
            ),
            (
                RelationReason::SameModule,
                "same_module",
                3,
                RelationConfidence::Medium,
            ),
            (
                RelationReason::OwnerNamedTest,
                "owner_named_test",
                4,
                RelationConfidence::Medium,
            ),
            (
                RelationReason::ImportPathAffinity,
                "import_path_affinity",
                5,
                RelationConfidence::Medium,
            ),
            (
                RelationReason::FixtureOwnerAffinity,
                "fixture_owner_affinity",
                6,
                RelationConfidence::Low,
            ),
        ];
        for (reason, name, prio, conf) in table {
            assert_eq!(reason.as_str(), name, "{reason:?}.as_str()");
            assert_eq!(reason.priority(), prio, "{reason:?}.priority()");
            assert_eq!(reason.confidence(), conf, "{reason:?}.confidence()");
        }
    }

    #[test]
    fn relation_confidence_as_str_and_rank_are_pinned_per_variant() {
        let table = [
            (RelationConfidence::High, "high", 0u8),
            (RelationConfidence::Medium, "medium", 1),
            (RelationConfidence::Low, "low", 2),
            (RelationConfidence::Opaque, "opaque", 3),
        ];
        for (conf, name, rank) in table {
            assert_eq!(conf.as_str(), name, "{conf:?}.as_str()");
            assert_eq!(conf.rank(), rank, "{conf:?}.rank()");
        }
    }

    #[test]
    fn required_discriminator_tokens_extracts_text_from_every_variant() {
        use crate::analysis::seams::{ExpectedSink, RepoSeam, RequiredDiscriminator};
        let make = |rd: RequiredDiscriminator| {
            RepoSeam::new(
                "src/x.rs",
                "x::owner",
                SeamKind::PredicateBoundary,
                0,
                1,
                "irrelevant",
                rd,
                ExpectedSink::ReturnValue,
            )
        };
        // Each arm carries a distinctive token so we can confirm the
        // right field was picked. Tokens longer than 2 chars survive
        // `is_interesting_token`.
        let cases: Vec<(RequiredDiscriminator, &str)> = vec![
            (
                RequiredDiscriminator::BoundaryValue {
                    description: "boundary_token".to_string(),
                },
                "boundary_token",
            ),
            (
                RequiredDiscriminator::ReturnValue {
                    description: "returnval_token".to_string(),
                },
                "returnval_token",
            ),
            (
                RequiredDiscriminator::ErrorVariant {
                    variant: "errvar_token".to_string(),
                },
                "errvar_token",
            ),
            (
                RequiredDiscriminator::FieldValue {
                    field: "fieldval_token".to_string(),
                },
                "fieldval_token",
            ),
            (
                RequiredDiscriminator::Effect {
                    sink: "effect_token".to_string(),
                },
                "effect_token",
            ),
            (
                RequiredDiscriminator::MatchArmTaken {
                    arm: "matcharm_token".to_string(),
                },
                "matcharm_token",
            ),
            (
                RequiredDiscriminator::CallSite {
                    target: "callsite_token".to_string(),
                },
                "callsite_token",
            ),
        ];
        for (rd, expected_token) in cases {
            let seam = make(rd.clone());
            let tokens = required_discriminator_tokens(&seam);
            assert!(
                tokens.iter().any(|t| t == expected_token),
                "{rd:?} -> tokens {tokens:?} must contain {expected_token}"
            );
        }
    }

    #[test]
    fn same_test_file_accepts_stem_match_and_test_suffixes() {
        assert!(same_test_file(Path::new("tests/foo.rs"), "foo"));
        assert!(same_test_file(Path::new("tests/foo_test.rs"), "foo"));
        assert!(same_test_file(Path::new("tests/foo_tests.rs"), "foo"));
        assert!(!same_test_file(Path::new("tests/bar.rs"), "foo"));
        assert!(!same_test_file(Path::new(""), "foo"));
    }

    #[test]
    fn module_path_for_handles_every_root_shape() {
        let cases: Vec<(&str, Option<&str>)> = vec![
            ("src/foo.rs", Some("foo")),
            ("tests/cli_smoke.rs", Some("cli_smoke")),
            ("crates/ripr/src/auth/login.rs", Some("auth/login")),
            ("crates/ripr/tests/integration.rs", Some("integration")),
            ("docs/note.rs", None),
            // `body = ".rs"` after stripping `src/`; trimmed = "" → None.
            ("src/.rs", None),
        ];
        for (input, expected) in cases {
            let got = module_path_for(Path::new(input));
            let want = expected.map(str::to_string);
            assert_eq!(got, want, "module_path_for({input})");
        }
    }

    #[test]
    fn same_module_matches_parent_prefix_and_underscore_form() {
        assert!(same_module("pricing/discount", "pricing/integration"));
        assert!(same_module("a/b/c", "a_b/d"));
        assert!(!same_module("flat", "anything"));
        assert!(!same_module("pricing/discount", "billing/integration"));
    }

    #[test]
    fn is_fixture_named_recognises_each_prefix_and_suffix() {
        let positives = [
            "fixture_quote",
            "setup_db",
            "make_quote",
            "build_request",
            "new_user",
            "mock_clock",
            "quote_fixture",
            "quote_factory",
        ];
        for name in positives {
            assert!(is_fixture_named(name), "{name} should be fixture-named");
        }
        for name in ["compute_total", "discount", "verify"] {
            assert!(
                !is_fixture_named(name),
                "{name} should NOT be fixture-named"
            );
        }
    }

    #[test]
    fn given_assertion_target_token_in_test_assertion_when_related_tests_are_ranked_then_assertion_target_affinity_fires()
    -> Result<(), String> {
        // Positive case for `assertion_target_affinity`: the seam's
        // `RequiredDiscriminator::BoundaryValue.description` carries
        // the identifier `discount_threshold`; a test assertion that
        // mentions `discount_threshold` as a whole identifier matches.
        // The test does not call the owner directly, the test file
        // stem is unrelated, and the test name does not embed the
        // owner — so this is the only reason that fires.
        let prod_src = "pub fn discounted_total(amount: i32, discount_threshold: i32) -> i32 \
                        { if amount >= discount_threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/billing.rs",
            "fn other() -> i32 { 0 }\n\
             #[test] fn smoke() { let discount_threshold = 5; assert_eq!(discount_threshold, 5); }\n",
        );
        let grip = first_grip_for("src/pricing.rs", prod_src, &[test])?;
        assert_eq!(
            grip.relation_reason,
            RelationReason::AssertionTargetAffinity
        );
        assert_eq!(grip.relation_confidence, RelationConfidence::High);
        Ok(())
    }

    #[test]
    fn assertion_targets_seam_returns_false_for_empty_token_list() {
        // The `tokens.is_empty()` early-return is the cheap escape
        // hatch when a seam's `RequiredDiscriminator` carries no
        // interesting tokens (e.g. a one-character variable name).
        use crate::analysis::rust_index::TestFact;
        let test = TestFact {
            name: "synth".to_string(),
            file: PathBuf::from("tests/x.rs"),
            start_line: 1,
            end_line: 5,
            body: "assert_eq!(1, 1);".to_string(),
            calls: Vec::new(),
            assertions: Vec::new(),
            literals: Vec::new(),
            attrs: Vec::new(),
        };
        assert!(!assertion_targets_seam(&test, &[]));
    }

    #[test]
    fn package_prefix_resolves_crates_and_nested_src_tests_layouts() {
        // `crates/<name>/src/...` form returns the `crates/<name>/` prefix.
        assert_eq!(
            package_prefix(Path::new("crates/ripr/src/auth/login.rs")).as_deref(),
            Some("crates/ripr/")
        );
        // `crates/<name>/tests/...` form (the second branch of the
        // strip_prefix-and-or guard) also returns the package prefix.
        assert_eq!(
            package_prefix(Path::new("crates/ripr/tests/integration.rs")).as_deref(),
            Some("crates/ripr/")
        );
        // Nested workspace path (rfind branch): the marker scan falls
        // through to the `/src/` rfind path.
        assert_eq!(
            package_prefix(Path::new("workspaces/foo/src/auth/login.rs")).as_deref(),
            Some("workspaces/foo/")
        );
        // Bare `src/...` returns None (prefix would be empty).
        assert_eq!(package_prefix(Path::new("src/foo.rs")), None);
        // Path under neither root.
        assert_eq!(package_prefix(Path::new("docs/note.rs")), None);
    }

    #[test]
    fn given_owner_in_workspace_crate_when_test_is_in_other_crate_then_it_is_filtered_out()
    -> Result<(), String> {
        // Owner lives in `crates/ripr_pricing/src/discount.rs`; a test
        // in a different package (`crates/ripr_other/tests/x.rs`)
        // must not appear as a related test, even if it would
        // otherwise satisfy a reason. Exercises the package-prefix
        // skip branch in `find_related_tests`.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let other_pkg_test = (
            "crates/ripr_other/tests/x.rs",
            "#[test] fn discounted_total_other_pkg() { assert_eq!(1, 1); }\n",
        );
        let files: Vec<(PathBuf, &str)> = vec![
            (
                PathBuf::from("crates/ripr_pricing/src/discount.rs"),
                prod_src,
            ),
            (PathBuf::from(other_pkg_test.0), other_pkg_test.1),
        ];
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(
            &[PathBuf::from("crates/ripr_pricing/src/discount.rs")],
            &index,
        );
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);
        for grip in &evidence.related_tests {
            assert_ne!(
                grip.file,
                Path::new("crates/ripr_other/tests/x.rs"),
                "test in unrelated package should be filtered by package_prefix; \
                 got {grip:?}"
            );
        }
        Ok(())
    }

    #[test]
    fn given_test_calls_helper_with_fixture_attribute_then_fixture_owner_affinity_fires()
    -> Result<(), String> {
        // `test_uses_owner_fixture` accepts EITHER a fixture-named
        // helper OR a helper whose body contains `#[fixture]`. The
        // earlier `given_fixture_only_affinity_…` test exercises the
        // name-based branch (`make_quote`); this one exercises the
        // body-marker branch by using a non-fixture helper name but
        // placing the `#[fixture]` marker as an inline comment inside
        // the body. `FunctionFact.body` slices from the `fn` keyword
        // to the end of the function, so attributes ABOVE the `fn`
        // line are not captured — the marker must live inside the
        // body block.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n\
                        pub fn provide_quote() -> i32 {\n    // #[fixture]\n    100\n}\n";
        let test = (
            "tests/integration.rs",
            "#[test] fn quote_smoke() { let _ = provide_quote(); assert!(true); }\n",
        );
        let grip = first_grip_for("src/pricing.rs", prod_src, &[test])?;
        assert_eq!(grip.relation_reason, RelationReason::FixtureOwnerAffinity);
        Ok(())
    }

    // -- value-extraction-v2 ------------------------------------------
    //
    // Each test exercises one resolution path through `activate_evidence`:
    // a related test calls the seam owner, the call arg is something
    // `scalar_values` would reject (bare identifier, builder method,
    // table row, rstest case, Some/Err wrapper), and the resolver in
    // `analysis::value_resolution` should turn it into observed values
    // - which `evidence_for_seam` then exposes via
    // `TestGripEvidence.observed_values`. The negative tests pin the
    // false-positive guards for comment/string shadows and unrelated
    // identifiers.

    fn observed_values_for(prod_src: &str, tests: &[(&str, &str)]) -> Result<Vec<String>, String> {
        let mut files: Vec<(PathBuf, &str)> = vec![(PathBuf::from("src/pricing.rs"), prod_src)];
        for (path, src) in tests {
            files.push((PathBuf::from(*path), *src));
        }
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);
        Ok(evidence
            .observed_values
            .into_iter()
            .map(|v| v.value)
            .collect())
    }

    #[test]
    fn given_let_binding_values_when_owner_call_uses_identifiers_then_observed_values_are_resolved()
    -> Result<(), String> {
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/pricing_tests.rs",
            "#[test] fn at_threshold() { let amount = 100; let threshold = 100; \
             assert_eq!(discounted_total(amount, threshold), 90); }\n",
        );
        let values = observed_values_for(prod_src, &[test])?;
        assert!(
            values.iter().any(|v| v == "100"),
            "let-resolved 100 must appear in observed values; got {values:?}"
        );
        Ok(())
    }

    #[test]
    fn given_boundary_owner_call_when_threshold_is_parameter_then_observed_values_stay_on_input_operand()
    -> Result<(), String> {
        let prod_src = "pub fn discounted_total(amount: i32, discount_threshold: i32) -> i32 \
                        { if amount >= discount_threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/pricing_tests.rs",
            "#[test] fn below_threshold() { \
                 assert_eq!(discounted_total(50, 100), 50); \
             }\n",
        );
        let values = observed_values_for(prod_src, &[test])?;
        assert_eq!(
            values,
            vec!["50".to_string()],
            "observed values should describe the tested input operand, not the boundary parameter"
        );
        Ok(())
    }

    #[test]
    fn given_same_file_const_when_owner_call_uses_identifier_then_observed_value_is_resolved()
    -> Result<(), String> {
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/pricing_tests.rs",
            "const THRESHOLD: i32 = 100;\n\
             #[test] fn at_threshold() { \
                 assert_eq!(discounted_total(THRESHOLD, THRESHOLD), 90); \
             }\n",
        );
        let values = observed_values_for(prod_src, &[test])?;
        assert!(
            values.iter().any(|v| v == "100"),
            "const-resolved 100 must appear; got {values:?}"
        );
        Ok(())
    }

    #[test]
    fn given_table_driven_cases_when_owner_call_uses_row_values_then_each_case_value_is_recorded()
    -> Result<(), String> {
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/pricing_tests.rs",
            "#[test] fn table() { \
                 for (amount, threshold, expected) in [(50, 100, 50), (100, 100, 90)] { \
                     assert_eq!(discounted_total(amount, threshold), expected); \
                 } \
             }\n",
        );
        let values = observed_values_for(prod_src, &[test])?;
        assert!(
            values.iter().any(|v| v == "50"),
            "table row value 50 must appear; got {values:?}"
        );
        assert!(
            values.iter().any(|v| v == "100"),
            "table row value 100 must appear; got {values:?}"
        );
        Ok(())
    }

    #[test]
    fn given_option_result_constructor_when_owner_call_uses_shape_then_inner_value_is_recorded()
    -> Result<(), String> {
        // Owner takes a wrapped value; test calls with Some(literal).
        // Resolver should peel one level and emit the inner literal.
        let prod_src = "pub fn process(value: Option<i32>, threshold: i32) -> i32 \
                        { match value { Some(v) if v >= threshold => v - 10, _ => 0 } }\n";
        let test = (
            "tests/pricing_tests.rs",
            "#[test] fn at_boundary() { \
                 assert_eq!(process(Some(100), 100), 90); \
             }\n",
        );
        // The seam in this case is the predicate inside `process`.
        let mut files: Vec<(PathBuf, &str)> = vec![(PathBuf::from("src/pricing.rs"), prod_src)];
        files.push((PathBuf::from(test.0), test.1));
        let index = index_from_files(&files)?;
        let seams = inventory_seams_from_index(&[PathBuf::from("src/pricing.rs")], &index);
        let predicate = seams
            .iter()
            .find(|s| s.kind() == SeamKind::PredicateBoundary)
            .ok_or_else(|| "predicate seam present".to_string())?;
        let evidence = evidence_for_seam(predicate, &index);
        let values: Vec<String> = evidence
            .observed_values
            .iter()
            .map(|v| v.value.clone())
            .collect();
        assert!(
            values.iter().any(|v| v == "100"),
            "Some(100) must unwrap and contribute 100; got {values:?}"
        );
        Ok(())
    }

    #[test]
    fn given_builder_methods_matching_parameter_tokens_then_observed_values_are_recorded()
    -> Result<(), String> {
        // The seam's required-discriminator description carries the
        // identifiers `amount` and `discount_threshold`. A test that
        // builds a value via `.amount(100).discount_threshold(100)`
        // should have those literals counted as observed via the
        // BuilderMethod context. Owner name unused inside the builder
        // call — the test references the owner directly elsewhere so
        // it qualifies as related.
        let prod_src = "pub fn discounted_total(amount: i32, discount_threshold: i32) -> i32 \
                        { if amount >= discount_threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/pricing_tests.rs",
            "#[test] fn via_builder() { \
                 let q = Quote::new().amount(100).discount_threshold(100).build(); \
                 assert_eq!(discounted_total(q.amount, q.discount_threshold), 90); \
             }\n",
        );
        let values = observed_values_for(prod_src, &[test])?;
        // `amount` and `discount_threshold` are seam-discriminator
        // tokens, so the builder method facts should land.
        assert!(
            values.iter().filter(|v| v.as_str() == "100").count() >= 1,
            "builder method 100 must be recorded; got {values:?}"
        );
        Ok(())
    }

    #[test]
    fn given_fixture_factory_override_methods_matching_seam_tokens_then_values_are_recorded()
    -> Result<(), String> {
        // Fixture factories often use explicit override method names
        // like `with_amount`. These should count when the wrapped
        // method token aligns with the changed seam.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/pricing_tests.rs",
            "#[test] fn via_fixture_override() { \
                 let q = QuoteFixture::default().with_amount(100).with_threshold(100).build(); \
                 assert_eq!(discounted_total(q.amount, q.threshold), 90); \
             }\n",
        );
        let values = observed_values_for(prod_src, &[test])?;
        assert!(
            values.iter().filter(|v| v.as_str() == "100").count() >= 1,
            "fixture override 100 must be recorded; got {values:?}"
        );
        Ok(())
    }

    #[test]
    fn given_builder_method_with_unrelated_name_then_value_is_not_counted_for_seam_activation()
    -> Result<(), String> {
        // `.with_seed(42)` is a builder method whose name does NOT
        // align with any seam token. The value 42 must NOT appear
        // among observed values for this seam, even though the test
        // directly calls the owner.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/pricing_tests.rs",
            "#[test] fn via_unrelated_builder() { \
                 let _q = Foo::new().with_seed(42).build(); \
                 assert_eq!(discounted_total(50, 100), 50); \
             }\n",
        );
        let values = observed_values_for(prod_src, &[test])?;
        assert!(
            !values.iter().any(|v| v == "42"),
            "unrelated builder literal 42 must NOT count; got {values:?}"
        );
        Ok(())
    }

    #[test]
    fn given_unrelated_string_literal_mentions_value_when_extracting_values_then_no_observed_discriminator_is_recorded()
    -> Result<(), String> {
        // String literal in the body mentions `100` and `threshold`
        // but the call site uses an unresolved identifier. v2 must
        // not pull literals out of strings.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/pricing_tests.rs",
            "#[test] fn string_only() { \
                 let _doc = \"threshold = 100\"; \
                 let unresolved = make_amount(); \
                 assert_eq!(discounted_total(unresolved, unresolved), 0); \
             }\n",
        );
        let values = observed_values_for(prod_src, &[test])?;
        assert!(
            !values.iter().any(|v| v == "100"),
            "string literal 100 must NOT be observed; got {values:?}"
        );
        Ok(())
    }

    #[test]
    fn given_shared_fixture_module_constant_when_extracting_v2_values_then_no_cross_file_value_is_resolved()
    -> Result<(), String> {
        // Strict syntactic scope: cross-file constants must NOT
        // resolve. The const lives in tests/common/mod.rs; the test
        // lives in tests/pricing_tests.rs. v2 is single-file scope -
        // cross-file resolution is a future item and must not creep
        // in via "helpful" expansion.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let common = (
            "tests/common/mod.rs",
            "pub const SHARED_THRESHOLD: i32 = 100;\n",
        );
        let test = (
            "tests/pricing_tests.rs",
            "#[test] fn cross_file() { \
                 assert_eq!(discounted_total(SHARED_THRESHOLD, SHARED_THRESHOLD), 90); \
             }\n",
        );
        let values = observed_values_for(prod_src, &[test, common])?;
        assert!(
            !values.iter().any(|v| v == "100"),
            "cross-file SHARED_THRESHOLD = 100 must NOT resolve in v2; got {values:?}"
        );
        Ok(())
    }

    #[test]
    fn given_let_binding_shadowed_by_comment_when_extracting_then_real_binding_wins()
    -> Result<(), String> {
        // Mirrors #310's comment-stripping defense: a `// let amount = 999;`
        // comment must NOT shadow the real `let amount = 100;` binding.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/pricing_tests.rs",
            "#[test] fn at_threshold() { \
                 // let amount = 999; let threshold = 999;\n\
                 let amount = 100; let threshold = 100; \
                 assert_eq!(discounted_total(amount, threshold), 90); \
             }\n",
        );
        let values = observed_values_for(prod_src, &[test])?;
        assert!(
            values.iter().any(|v| v == "100"),
            "real let binding 100 must be observed; got {values:?}"
        );
        assert!(
            !values.iter().any(|v| v == "999"),
            "commented-out let binding 999 must NOT be observed; got {values:?}"
        );
        Ok(())
    }

    #[test]
    fn given_unresolved_identifier_arg_when_extracting_values_then_no_observed_value_is_recorded()
    -> Result<(), String> {
        // Identifier resolved through a helper call (no `let` binding,
        // no const, no rstest case, no table row, no Some wrapper).
        // Must stay opaque — the previous behavior is preserved for
        // the unresolved case.
        let prod_src = "pub fn discounted_total(amount: i32, threshold: i32) -> i32 \
                        { if amount >= threshold { amount - 10 } else { amount } }\n";
        let test = (
            "tests/pricing_tests.rs",
            "#[test] fn opaque() { \
                 let amount = make_amount(); \
                 let threshold = make_threshold(); \
                 assert_eq!(discounted_total(amount, threshold), 0); \
             }\n",
        );
        let values = observed_values_for(prod_src, &[test])?;
        // The let RHS isn't a literal, so the binding shouldn't
        // resolve. observed_values for these args should stay empty.
        assert!(
            values.is_empty()
                || values
                    .iter()
                    .all(|v| !matches!(v.as_str(), "100" | "0" | "make_amount")),
            "opaque args must not produce a fake observed value; got {values:?}"
        );
        Ok(())
    }
}