axon-lang 2.69.0

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

pub mod checker;
pub mod effects;
pub mod generate;
pub mod proof_term;

pub use checker::{
    check_bundle, check_call_soundness_certificate, check_proof, BundleReport, CheckOutcome,
    ProofCheck,
};
pub use generate::{
    artifact_digest, derive_capability_containment_witness, derive_capability_isolation_witness,
    derive_channel_egress_witness, derive_compliance_coverage_witness,
    derive_effect_row_soundness_witness,
    derive_endpoint_retry_witness, derive_interruptible_session_witness,
    derive_parked_residual_witness,
    derive_shield_halt_witness, derive_socket_credit_witness,
    derive_tool_call_soundness_witness, generate_all_proofs, generate_call_soundness_certificate,
    generate_capability_containment_proofs, generate_capability_isolation_proofs,
    generate_channel_egress_soundness_proofs, generate_compliance_coverage_proofs,
    // §Fase 89.c — the every_boundary_is_guarded coverage obligation.
    derive_authorization_coverage_witness, generate_authorization_coverage_proofs,
    // §Fase 90.b — the every_requirement_is_grantable obligation (the dual).
    derive_capability_grantability_witness, generate_capability_grantability_proofs,
    generate_effect_row_soundness_proofs,
    generate_interruptible_session_soundness_proofs,
    generate_parked_residual_soundness_proofs,
    generate_resource_bounds_proofs, generate_shield_halt_guarantee_proofs,
    generate_tool_call_soundness_proofs,
    // §Fase 80 — the outbound-vendor projection obligation.
    derive_upstream_projection_witness, generate_upstream_projection_soundness_proofs,
    // §Fase 83.c — the whole-program CORS-reference/consistency obligation.
    derive_cors_policy_consistency_witness, generate_cors_policy_consistency_proofs,
    // §Fase 91.c — the time_is_an_explicit_input cognitive obligation.
    derive_temporal_context_soundness_witness, generate_temporal_context_soundness_proofs,
    // §Fase 92.d — the authority_only_attenuates static obligation.
    derive_credential_attenuation_witness, generate_credential_attenuation_proofs,
    // §Fase 94.e — the rotation_without_revelation static obligation.
    derive_secret_custody_witness, generate_secret_custody_proofs,
    // §Fase 98.d — the web-acquisition provenance obligation (born-Untrusted
    // + the content-injection barrier).
    derive_scrape_provenance_soundness_witness, generate_scrape_provenance_soundness_proofs,
    derive_document_provenance_soundness_witness, generate_document_provenance_soundness_proofs,
    // §Fase 105 — the CRM-delivery provenance obligation (T920 egress barrier).
    derive_delivery_provenance_soundness_witness, generate_delivery_provenance_soundness_proofs,
    // §Fase 107 — the QUERY-safety obligation (T927; RFC 10008 §2 made a proof).
    derive_dataspace_schema_soundness_witness, derive_gradient_soundness_witness,
    derive_query_safety_soundness_witness,
    generate_dataspace_schema_soundness_proofs, generate_gradient_soundness_proofs,
    generate_notification_provenance_soundness_proofs,
    generate_query_safety_soundness_proofs,
    derive_document_ingestion_soundness_witness, generate_document_ingestion_soundness_proofs,
    derive_inferred_ceiling_soundness_witness, generate_inferred_ceiling_soundness_proofs,
};
pub use proof_term::{
    CallSoundnessCertificate, CapabilityContainmentWitness, CapabilityGrantabilityWitness,
    CapabilityIsolationWitness,
    CredentialAttenuationWitness,
    ChannelEgressSoundnessWitness,
    ComplianceCoverageWitness,
    CorsPolicyConsistencyWitness,
    EffectRowSoundnessWitness, InterruptibleSessionSoundnessWitness, ParkedResidualSoundnessWitness,
    ProofBundle, ProofTerm,
    PropertyClass, ResourceBoundsWitness,
    ScrapeProvenanceSoundnessWitness,
    DocumentProvenanceSoundnessWitness,
    DeliveryProvenanceSoundnessWitness,
    DataspaceSchemaSoundnessWitness, GradientSoundnessWitness,
    NotificationProvenanceSoundnessWitness, QuerySafetySoundnessWitness,
    DocumentIngestionSoundnessWitness,
    InferredCeilingSoundnessWitness,
    SecretCustodySoundnessWitness,
    ShieldHaltGuaranteeWitness, TemporalContextSoundnessWitness, ToolCallSoundnessWitness,
    UpstreamProjectionSoundnessWitness,
    Witness, CALL_INTERRUPT_CAUSES,
    MAX_RETRIES, VALID_BREACH_POLICIES, VALID_SIGN_ALGORITHMS,
};

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir_nodes::{IRAxonEndpoint, IRProgram, IRShield, IRToolSpec};

    const VERSION: &str = "2.4.0-test";

    fn empty_ir() -> IRProgram {
        IRProgram::new()
    }

    fn endpoint(name: &str, compliance: &[&str], shield_ref: &str) -> IRAxonEndpoint {
        IRAxonEndpoint {
            node_type: "endpoint",
            source_line: 1,
            source_column: 1,
            name: name.to_string(),
            method: "POST".to_string(),
            path: format!("/{name}"),
            body_type: String::new(),
            execute_flow: "F".to_string(),
            output_type: String::new(),
            shield_ref: shield_ref.to_string(),
            cors_ref: String::new(),
            retries: 0,
            timeout: String::new(),
            compliance: compliance.iter().map(|s| s.to_string()).collect(),
            path_params: Vec::new(),
            query_params: Vec::new(),
            requires_capabilities: Vec::new(),
            public: false,
        }
    }

    /// Shield providing the given regulatory classes (its `compliance:`
    /// set — the §ESK Fase 6.1 covered-classes field).
    fn shield(name: &str, provides: &[&str]) -> IRShield {
        IRShield {
            node_type: "shield",
            source_line: 1,
            source_column: 1,
            name: name.to_string(),
            scan: vec!["pii_leak".to_string()],
            strategy: String::new(),
            on_breach: "halt".to_string(),
            severity: "high".to_string(),
            quarantine: String::new(),
            max_retries: 0,
            confidence_threshold: 0.0,
            allow_tools: Vec::new(),
            deny_tools: Vec::new(),
            sandbox: false,
            redact: Vec::new(),
            log: String::new(),
            deflect_message: String::new(),
            taint: String::new(),
            compliance: provides.iter().map(|s| s.to_string()).collect(),
            sign: String::new(),
        }
    }

    /// Happy path: an apx declaring known classes + a present shield
    /// generates a proof the independent checker VERIFIES.
    #[test]
    fn covered_endpoint_proof_verifies() {
        let mut ir = empty_ir();
        ir.shields.push(shield("PhiGate", &["HIPAA", "GDPR"]));
        ir.endpoints
            .push(endpoint("ChatEndpoint", &["HIPAA", "GDPR"], "PhiGate"));

        let proofs = generate_compliance_coverage_proofs(&ir, VERSION);
        assert_eq!(
            proofs.len(),
            1,
            "one compliance-bearing endpoint => one proof"
        );
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// Endpoints with no compliance declaration produce no proof
    /// (nothing to certify).
    #[test]
    fn no_compliance_no_proof() {
        let mut ir = empty_ir();
        ir.shields.push(shield("PhiGate", &["HIPAA"]));
        ir.endpoints.push(endpoint("Plain", &[], "PhiGate"));
        assert!(generate_compliance_coverage_proofs(&ir, VERSION).is_empty());
    }

    /// Honest defect: the shield resolves but does NOT provide every
    /// required class (endpoint requires HIPAA+GDPR, shield covers only
    /// HIPAA) → Refuted naming the uncovered gap. This is the core
    /// `covers()` semantic the proof certifies.
    #[test]
    fn shield_missing_required_class_is_refuted() {
        let mut ir = empty_ir();
        ir.shields.push(shield("PartialGate", &["HIPAA"]));
        ir.endpoints
            .push(endpoint("Gap", &["HIPAA", "GDPR"], "PartialGate"));
        let proofs = generate_compliance_coverage_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("does not provide"), "got: {reason}");
                assert!(reason.contains("GDPR"), "got: {reason}");
            }
            other => panic!("expected coverage-gap Refuted, got {other:?}"),
        }
    }

    /// Honest defect: compliance declared but no shield attached →
    /// the checker REFUTES (you cannot claim regulatory coverage with
    /// zero enforcement).
    #[test]
    fn compliance_without_shield_is_refuted() {
        let mut ir = empty_ir();
        ir.endpoints.push(endpoint("NoGuard", &["HIPAA"], "")); // empty shield_ref
        let proofs = generate_compliance_coverage_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("no resolvable shield"), "got: {reason}");
            }
            other => panic!("expected Refuted, got {other:?}"),
        }
    }

    /// Honest defect: shield_ref names a shield that isn't in the IR →
    /// dangling reference → Refuted.
    #[test]
    fn dangling_shield_ref_is_refuted() {
        let mut ir = empty_ir();
        ir.endpoints
            .push(endpoint("Dangling", &["HIPAA"], "GhostShield"));
        let proofs = generate_compliance_coverage_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("no resolvable shield"), "got: {reason}");
            }
            other => panic!("expected Refuted, got {other:?}"),
        }
    }

    /// Honest defect: phantom regulatory class (typo'd `HIPPA`) →
    /// Refuted naming the unknown class.
    #[test]
    fn phantom_regulatory_class_is_refuted() {
        let mut ir = empty_ir();
        ir.shields.push(shield("PhiGate", &["HIPAA"]));
        ir.endpoints.push(endpoint("Typo", &["HIPPA"], "PhiGate")); // HIPPA, not HIPAA
        let proofs = generate_compliance_coverage_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("unknown regulatory class"), "got: {reason}");
                assert!(reason.contains("HIPPA"), "got: {reason}");
            }
            other => panic!("expected Refuted, got {other:?}"),
        }
    }

    /// ADVERSARIAL (D51.2): a forged witness claiming `shield_present:
    /// true` for an endpoint whose shield_ref is empty is REJECTED —
    /// the checker recomputes shield_present from the artifact and
    /// finds the witness lies.
    #[test]
    fn forged_shield_present_rejected() {
        let mut ir = empty_ir();
        ir.endpoints.push(endpoint("Forge", &["HIPAA"], ""));
        let mut proofs = generate_compliance_coverage_proofs(&ir, VERSION);
        // Tamper: flip shield_present to true (the lie).
        if let Witness::ComplianceCoverage(ref mut w) = proofs[0].witness {
            w.shield_present = true;
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("disagrees with artifact"), "got: {reason}");
            }
            other => panic!("expected forged-witness Refuted, got {other:?}"),
        }
    }

    /// ADVERSARIAL (D51.2): a forged witness hiding a phantom class
    /// (omitting it from `unknown_classes`) is REJECTED — the checker
    /// recomputes the unknown set.
    #[test]
    fn forged_hidden_unknown_class_rejected() {
        let mut ir = empty_ir();
        ir.shields.push(shield("PhiGate", &["HIPAA"]));
        ir.endpoints
            .push(endpoint("Hide", &["NOTACLASS"], "PhiGate"));
        let mut proofs = generate_compliance_coverage_proofs(&ir, VERSION);
        // Tamper: pretend there are no unknown classes.
        if let Witness::ComplianceCoverage(ref mut w) = proofs[0].witness {
            w.unknown_classes.clear();
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("disagrees with artifact"), "got: {reason}");
            }
            other => panic!("expected forged-witness Refuted, got {other:?}"),
        }
    }

    /// ADVERSARIAL (D51.1): a proof minted for program A checked
    /// against program B (different digest) is REJECTED.
    #[test]
    fn digest_mismatch_rejected() {
        let mut ir_a = empty_ir();
        ir_a.shields.push(shield("PhiGate", &["HIPAA"]));
        ir_a.endpoints
            .push(endpoint("ChatEndpoint", &["HIPAA"], "PhiGate"));
        let proofs = generate_compliance_coverage_proofs(&ir_a, VERSION);

        // Program B: same endpoint + shield, but an extra endpoint
        // changes the IR digest. Built fresh (IRProgram is not Clone).
        let mut ir_b = empty_ir();
        ir_b.shields.push(shield("PhiGate", &["HIPAA"]));
        ir_b.endpoints
            .push(endpoint("ChatEndpoint", &["HIPAA"], "PhiGate"));
        ir_b.endpoints.push(endpoint("Extra", &["GDPR"], "PhiGate"));

        assert_eq!(
            check_proof(&proofs[0], &ir_b),
            CheckOutcome::DigestMismatch,
            "a proof for program A must not verify against program B"
        );
    }

    /// ADVERSARIAL: a proof whose witness names an endpoint absent from
    /// the artifact is Refuted (not a panic). Exercises the
    /// never-panic guarantee on a stale/forged endpoint reference.
    #[test]
    fn proof_for_absent_endpoint_refuted_not_panic() {
        let mut ir = empty_ir();
        ir.shields.push(shield("PhiGate", &["HIPAA"]));
        ir.endpoints.push(endpoint("Real", &["HIPAA"], "PhiGate"));
        let mut proofs = generate_compliance_coverage_proofs(&ir, VERSION);
        // Tamper the endpoint name to one not in the IR, and re-stamp
        // the digest so we get past the digest gate to the endpoint
        // lookup (isolates the absent-endpoint path).
        if let Witness::ComplianceCoverage(ref mut w) = proofs[0].witness {
            w.endpoint_name = "Ghost".to_string();
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("not present in artifact"), "got: {reason}");
            }
            other => panic!("expected absent-endpoint Refuted, got {other:?}"),
        }
    }

    /// The proof term round-trips through JSON (it travels as JSON
    /// alongside the artifact). A round-tripped proof still verifies.
    #[test]
    fn proof_term_json_round_trips_and_verifies() {
        let mut ir = empty_ir();
        ir.shields.push(shield("PhiGate", &["HIPAA"]));
        ir.endpoints
            .push(endpoint("ChatEndpoint", &["HIPAA"], "PhiGate"));
        let proofs = generate_compliance_coverage_proofs(&ir, VERSION);
        let json = serde_json::to_string(&proofs[0]).expect("serialize");
        let restored: ProofTerm = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(restored, proofs[0]);
        assert_eq!(check_proof(&restored, &ir), CheckOutcome::Verified);
    }

    /// Witness canonicalization: declared classes in any order +
    /// duplicates produce the same (sorted, deduped) witness, so the
    /// checker's re-derivation compares equal regardless of source
    /// ordering.
    #[test]
    fn class_ordering_and_dupes_canonicalized() {
        let mut ir = empty_ir();
        ir.shields.push(shield("PhiGate", &["HIPAA", "GDPR"]));
        ir.endpoints.push(endpoint(
            "Multi",
            &["GDPR", "HIPAA", "GDPR"], // unsorted + duplicate
            "PhiGate",
        ));
        let proofs = generate_compliance_coverage_proofs(&ir, VERSION);
        if let Witness::ComplianceCoverage(ref w) = proofs[0].witness {
            assert_eq!(
                w.required_classes,
                vec!["GDPR".to_string(), "HIPAA".to_string()]
            );
        } else {
            panic!("expected ComplianceCoverage witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// §51.a property-class slug is stable (wire contract).
    #[test]
    fn property_class_slug_stable() {
        assert_eq!(
            PropertyClass::ComplianceCoverage.slug(),
            "compliance_coverage"
        );
        assert_eq!(
            PropertyClass::EffectRowSoundness.slug(),
            "effect_row_soundness"
        );
        assert_eq!(
            PropertyClass::CapabilityIsolation.slug(),
            "capability_isolation"
        );
        assert_eq!(PropertyClass::ResourceBounds.slug(), "resource_bounds");
        assert_eq!(
            PropertyClass::ShieldHaltGuarantee.slug(),
            "shield_halt_guarantee"
        );
        assert_eq!(
            PropertyClass::CapabilityContainment.slug(),
            "capability_containment"
        );
        assert_eq!(
            PropertyClass::ToolCallSoundness.slug(),
            "tool_call_soundness"
        );
        assert_eq!(PropertyClass::EffectBudgeted.slug(), "effect_budgeted");
        assert_eq!(
            PropertyClass::JsonShapeSoundness.slug(),
            "json_shape_soundness"
        );
        assert_eq!(
            PropertyClass::ChannelDeliverySoundness.slug(),
            "channel_delivery_soundness"
        );
        assert_eq!(
            PropertyClass::AggregateSoundness.slug(),
            "aggregate_soundness"
        );
        assert_eq!(
            PropertyClass::ChannelEgressSoundness.slug(),
            "channel_egress_soundness"
        );
    }

    // ── §Fase 51.b — EffectRowSoundness ──────────────────────────────

    /// Tool declaring the given effect-row entries.
    fn tool(name: &str, effects: &[&str]) -> IRToolSpec {
        IRToolSpec {
            node_type: "tool",
            source_line: 1,
            source_column: 1,
            name: name.to_string(),
            provider: "native".to_string(),
            max_results: None,
            filter_expr: String::new(),
            timeout: String::new(),
            runtime: String::new(),
            resource_ref: String::new(),
            sandbox: None,
            input_schema: Vec::new(),
            output_schema: String::new(),
            parameters: Vec::new(),
            output_type: None,
            secret: String::new(),
            secret_partition: String::new(),
            effect_row: effects.iter().map(|s| s.to_string()).collect(),
            target: None,
            risk: None,
            argv: Vec::new(),
            cache: String::new(),
            scrape: None,
        }
    }

    /// Happy path: a tool with well-formed effects verifies.
    #[test]
    fn well_formed_effect_row_verifies() {
        let mut ir = empty_ir();
        ir.tools.push(tool("Fetch", &["network", "storage"]));
        let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// A tool with no declared effects produces no proof.
    #[test]
    fn no_effects_no_proof() {
        let mut ir = empty_ir();
        ir.tools.push(tool("Plain", &[]));
        assert!(generate_effect_row_soundness_proofs(&ir, VERSION).is_empty());
    }

    /// Phantom base effect (typo'd `netwrok`) → Refuted.
    #[test]
    fn phantom_effect_base_refuted() {
        let mut ir = empty_ir();
        ir.tools.push(tool("Typo", &["netwrok"]));
        let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("unknown base"), "got: {reason}");
                assert!(reason.contains("netwrok"), "got: {reason}");
            }
            other => panic!("expected phantom-effect Refuted, got {other:?}"),
        }
    }

    /// Bare `stream` without a backpressure qualifier → Refuted
    /// (unenforceable).
    #[test]
    fn bare_stream_missing_qualifier_refuted() {
        let mut ir = empty_ir();
        ir.tools.push(tool("Streamer", &["stream"]));
        let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("without a qualifier"), "got: {reason}");
            }
            other => panic!("expected missing-qualifier Refuted, got {other:?}"),
        }
    }

    /// `stream:<valid policy>` verifies (qualifier from the backpressure
    /// catalog).
    #[test]
    fn valid_stream_qualifier_verifies() {
        let mut ir = empty_ir();
        ir.tools.push(tool("Streamer", &["stream:drop_oldest"]));
        let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// `stream:<bogus>` (qualifier not in the backpressure catalog) →
    /// Refuted.
    #[test]
    fn invalid_stream_qualifier_refuted() {
        let mut ir = empty_ir();
        ir.tools.push(tool("Streamer", &["stream:explode"]));
        let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(
                    reason.contains("invalid backpressure policy"),
                    "got: {reason}"
                );
            }
            other => panic!("expected invalid-qualifier Refuted, got {other:?}"),
        }
    }

    // ── §Fase 53.d — extension-declared provenance bases ─────────────

    /// Build an `effects`-category extension with the given members
    /// (no metadata).
    fn effects_ext(name: &str, members: &[&str]) -> crate::ir_nodes::IRExtension {
        crate::ir_nodes::IRExtension {
            node_type: "extension",
            source_line: 1,
            source_column: 1,
            name: name.to_string(),
            category: "effects".to_string(),
            members: members
                .iter()
                .map(|m| crate::ir_nodes::IRExtensionMember {
                    name: m.to_string(),
                    semantics: None,
                    default_confidence: None,
                })
                .collect(),
        }
    }

    /// §53.d — a tool using an extension-declared provenance base
    /// VERIFIES (the proof is self-contained: the verifier re-derives
    /// the provenance set from the artifact's own `extensions`).
    #[test]
    fn extension_declared_provenance_base_verifies() {
        let mut ir = empty_ir();
        ir.extensions
            .push(effects_ext("risk_axis", &["risk:elevated"]));
        ir.tools.push(tool("Fetch", &["network", "risk:elevated"]));
        let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// §53.d — a custom base that NO extension declares still REFUTES
    /// (only the declared members are honored — the catalog is not
    /// silently opened).
    #[test]
    fn undeclared_custom_base_refuted() {
        let mut ir = empty_ir();
        // extension declares `risk:elevated`, NOT `risk:guess`.
        ir.extensions
            .push(effects_ext("risk_axis", &["risk:elevated"]));
        ir.tools.push(tool("Fetch", &["risk:guess"]));
        let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("unknown base"), "got: {reason}");
                assert!(reason.contains("risk:guess"), "got: {reason}");
            }
            other => panic!("expected undeclared-base Refuted, got {other:?}"),
        }
    }

    /// §53.d (invariant #2, PCC independence) — the checker's provenance
    /// set EXCLUDES a member whose base is a canonical enforceable base.
    /// PCC enforces this itself; it does not trust that the type-checker
    /// ran. (`io:bypass` is excluded; `risk:elevated` is included.)
    #[test]
    fn pcc_provenance_set_excludes_canonical_shadow() {
        let mut ir = empty_ir();
        ir.extensions
            .push(effects_ext("mixed", &["io:bypass_shield", "risk:elevated"]));
        let set = super::generate::extension_effect_members(&ir);
        assert!(
            !set.contains("io:bypass_shield"),
            "a member shadowing the canonical enforceable base `io` must NOT \
             become a provenance member (invariant #2)"
        );
        assert!(
            set.contains("risk:elevated"),
            "a genuinely custom base must be a provenance member"
        );
    }

    // ── §Fase 53.c.2 — built-in `epistemic:<level>` provenance axis ──

    /// §53.c.2 — a tool declaring the built-in `epistemic:<level>` axis
    /// VERIFIES (no `extension` needed). This is the Kivi brief #15 case:
    /// pre-§53.c.2 the IR re-injected `epistemic:believe` into the effect
    /// row and PCC refuted it as an unknown base, forcing the strip.
    #[test]
    fn builtin_epistemic_level_verifies() {
        for level in ["believe", "doubt", "know", "speculate"] {
            let mut ir = empty_ir();
            ir.tools
                .push(tool("Fetch", &["network", &format!("epistemic:{level}")]));
            let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
            assert_eq!(proofs.len(), 1);
            assert_eq!(
                check_proof(&proofs[0], &ir),
                CheckOutcome::Verified,
                "epistemic:{level} must verify (built-in provenance axis)"
            );
        }
    }

    /// §53.c.2 — an `epistemic:<bogus>` level (not in the closed catalog)
    /// still REFUTES (the axis is closed, not a wildcard prefix).
    #[test]
    fn unknown_epistemic_level_refuted() {
        let mut ir = empty_ir();
        ir.tools.push(tool("X", &["epistemic:guess"]));
        let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("unknown base"), "got: {reason}");
                assert!(reason.contains("epistemic:guess"), "got: {reason}");
            }
            other => panic!("expected unknown-epistemic-level Refuted, got {other:?}"),
        }
    }

    /// `pure` alongside another effect → purity contradiction → Refuted.
    #[test]
    fn purity_violation_refuted() {
        let mut ir = empty_ir();
        ir.tools.push(tool("FakePure", &["pure", "network"]));
        let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("pure"), "got: {reason}");
                assert!(reason.contains("cannot be effectful"), "got: {reason}");
            }
            other => panic!("expected purity Refuted, got {other:?}"),
        }
    }

    /// `pure` alone verifies.
    #[test]
    fn pure_alone_verifies() {
        let mut ir = empty_ir();
        ir.tools.push(tool("TrulyPure", &["pure"]));
        let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// ADVERSARIAL (D51.2): a forged witness hiding a phantom base
    /// (clearing `unknown_bases`) is rejected — the checker recomputes.
    #[test]
    fn forged_hidden_unknown_base_rejected() {
        let mut ir = empty_ir();
        ir.tools.push(tool("Sneaky", &["netwrok"]));
        let mut proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        if let Witness::EffectRowSoundness(ref mut w) = proofs[0].witness {
            w.unknown_bases.clear(); // the lie
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("disagrees with artifact"), "got: {reason}");
            }
            other => panic!("expected forged Refuted, got {other:?}"),
        }
    }

    /// ADVERSARIAL: a proof whose `property` and `witness` variants
    /// disagree (ComplianceCoverage property carrying an
    /// EffectRowSoundness witness) is UnknownProperty — neither
    /// property is actually witnessed, so no silent accept.
    #[test]
    fn mismatched_property_witness_is_unknown_property() {
        let mut ir = empty_ir();
        ir.tools.push(tool("Fetch", &["network"]));
        let digest = artifact_digest(&ir);
        let bogus = ProofTerm {
            property: PropertyClass::ComplianceCoverage, // mismatch
            artifact_digest: digest,
            witness: Witness::EffectRowSoundness(derive_effect_row_soundness_witness(
                "Fetch",
                &["network".to_string()],
                &std::collections::HashSet::new(),
            )),
            axon_version: VERSION.to_string(),
        };
        assert_eq!(check_proof(&bogus, &ir), CheckOutcome::UnknownProperty);
    }

    /// Effect-row proof round-trips through JSON and still verifies.
    #[test]
    fn effect_proof_json_round_trips_and_verifies() {
        let mut ir = empty_ir();
        ir.tools.push(tool("Fetch", &["network", "storage"]));
        let proofs = generate_effect_row_soundness_proofs(&ir, VERSION);
        let json = serde_json::to_string(&proofs[0]).expect("serialize");
        let restored: ProofTerm = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(restored, proofs[0]);
        assert_eq!(check_proof(&restored, &ir), CheckOutcome::Verified);
    }

    /// Digest binding holds for effect-row proofs too.
    #[test]
    fn effect_proof_digest_mismatch_rejected() {
        let mut ir_a = empty_ir();
        ir_a.tools.push(tool("Fetch", &["network"]));
        let proofs = generate_effect_row_soundness_proofs(&ir_a, VERSION);

        let mut ir_b = empty_ir();
        ir_b.tools.push(tool("Fetch", &["network"]));
        ir_b.tools.push(tool("Other", &["storage"])); // changes digest

        assert_eq!(check_proof(&proofs[0], &ir_b), CheckOutcome::DigestMismatch);
    }

    // ── §Fase 51.c — CapabilityIsolation ─────────────────────────────

    /// `axonstore` with the given Pillar IV capability gate slug.
    fn store(name: &str, capability: &str) -> crate::ir_nodes::IRAxonStore {
        crate::ir_nodes::IRAxonStore {
            node_type: "axonstore",
            source_line: 1,
            source_column: 1,
            name: name.to_string(),
            backend: "postgresql".to_string(),
            connection: String::new(),
            confidence_floor: None,
            isolation: String::new(),
            on_breach: String::new(),
            capability: capability.to_string(),
            class: String::new(),
            column_schema: None,
            resource_ref: String::new(),
        }
    }

    /// Happy path: a well-formed §32.g gate slug verifies.
    #[test]
    fn well_formed_gate_verifies() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Ledger", "legal.read"));
        let proofs = generate_capability_isolation_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// A store with no capability gate produces no proof.
    #[test]
    fn no_gate_no_proof() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Open", ""));
        assert!(generate_capability_isolation_proofs(&ir, VERSION).is_empty());
    }

    /// A malformed gate slug (uppercase violates the §32.g grammar) →
    /// Refuted.
    #[test]
    fn malformed_gate_refuted() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Broken", "Legal.Read")); // uppercase
        let proofs = generate_capability_isolation_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(
                    reason.contains("malformed capability gate"),
                    "got: {reason}"
                );
                assert!(reason.contains("Legal.Read"), "got: {reason}");
            }
            other => panic!("expected malformed-gate Refuted, got {other:?}"),
        }
    }

    /// Dotted multi-segment scope (`hipaa.phi.read`) is well-formed.
    #[test]
    fn deep_dotted_gate_verifies() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Phi", "hipaa.phi.read"));
        let proofs = generate_capability_isolation_proofs(&ir, VERSION);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// ADVERSARIAL (D51.2): a forged witness claiming `malformed:
    /// false` for a broken gate is rejected — the checker re-runs the
    /// grammar validator and finds the witness lies.
    #[test]
    fn forged_malformed_flag_rejected() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Sneaky", "Bad..Slug"));
        let mut proofs = generate_capability_isolation_proofs(&ir, VERSION);
        if let Witness::CapabilityIsolation(ref mut w) = proofs[0].witness {
            w.malformed = false; // the lie
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("disagrees with artifact"), "got: {reason}");
            }
            other => panic!("expected forged Refuted, got {other:?}"),
        }
    }

    /// Digest binding holds for capability proofs.
    #[test]
    fn capability_proof_digest_mismatch_rejected() {
        let mut ir_a = empty_ir();
        ir_a.axonstore_specs.push(store("Ledger", "legal.read"));
        let proofs = generate_capability_isolation_proofs(&ir_a, VERSION);

        let mut ir_b = empty_ir();
        ir_b.axonstore_specs.push(store("Ledger", "legal.read"));
        ir_b.axonstore_specs.push(store("Extra", "fin.read")); // changes digest

        assert_eq!(check_proof(&proofs[0], &ir_b), CheckOutcome::DigestMismatch);
    }

    /// Capability proof round-trips through JSON and still verifies.
    #[test]
    fn capability_proof_json_round_trips_and_verifies() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Ledger", "legal.read"));
        let proofs = generate_capability_isolation_proofs(&ir, VERSION);
        let json = serde_json::to_string(&proofs[0]).expect("serialize");
        let restored: ProofTerm = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(restored, proofs[0]);
        assert_eq!(check_proof(&restored, &ir), CheckOutcome::Verified);
    }

    // ── §Fase 51.d — ResourceBounds ──────────────────────────────────

    /// An endpoint with the given `retries` (reuses the base builder).
    fn endpoint_retries(name: &str, retries: i64) -> IRAxonEndpoint {
        let mut e = endpoint(name, &[], "");
        e.retries = retries;
        e
    }

    /// A socket with the given (optional) backpressure credit window.
    fn socket(name: &str, credit: Option<i64>) -> crate::ir_nodes::IRSocket {
        crate::ir_nodes::IRSocket {
            node_type: "socket",
            source_line: 1,
            source_column: 1,
            name: name.to_string(),
            protocol: "ChatProtocol".to_string(),
            backpressure_credit: credit,
            reconnect: false,
            legal_basis: None,
        }
    }

    /// Retry counts within `[0, MAX_RETRIES]` verify — including both
    /// boundaries (0 and the ceiling).
    #[test]
    fn retry_in_bounds_verifies() {
        let mut ir = empty_ir();
        ir.endpoints.push(endpoint_retries("Mid", 3));
        ir.endpoints.push(endpoint_retries("Zero", 0));
        ir.endpoints.push(endpoint_retries("Ceiling", MAX_RETRIES));
        let proofs = generate_resource_bounds_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 3);
        for p in &proofs {
            assert_eq!(check_proof(p, &ir), CheckOutcome::Verified);
        }
    }

    /// Negative retries → Refuted (nonsensical).
    #[test]
    fn retry_negative_refuted() {
        let mut ir = empty_ir();
        ir.endpoints.push(endpoint_retries("Neg", -1));
        let proofs = generate_resource_bounds_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("retries=-1"), "got: {reason}");
                assert!(reason.contains("outside the bound"), "got: {reason}");
            }
            other => panic!("expected negative-retry Refuted, got {other:?}"),
        }
    }

    /// Retry count above the ceiling → Refuted (retry storm).
    #[test]
    fn retry_storm_refuted() {
        let mut ir = empty_ir();
        ir.endpoints
            .push(endpoint_retries("Storm", MAX_RETRIES + 1));
        let proofs = generate_resource_bounds_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("retry storm"), "got: {reason}");
            }
            other => panic!("expected retry-storm Refuted, got {other:?}"),
        }
    }

    /// A socket with a positive declared credit verifies.
    #[test]
    fn socket_positive_credit_verifies() {
        let mut ir = empty_ir();
        ir.sockets.push(socket("Chat", Some(8)));
        let proofs = generate_resource_bounds_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// A socket declaring credit(0) → Refuted (deadlock per §41.b).
    #[test]
    fn socket_zero_credit_refuted() {
        let mut ir = empty_ir();
        ir.sockets.push(socket("Dead", Some(0)));
        let proofs = generate_resource_bounds_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("deadlock"), "got: {reason}");
            }
            other => panic!("expected zero-credit Refuted, got {other:?}"),
        }
    }

    /// A socket with UNSPECIFIED credit produces no proof (legitimate
    /// type state, not a bound to certify).
    #[test]
    fn socket_unspecified_credit_no_proof() {
        let mut ir = empty_ir();
        ir.sockets.push(socket("Unspecified", None));
        // No endpoints either, so the whole proof set is empty.
        assert!(generate_resource_bounds_proofs(&ir, VERSION).is_empty());
    }

    /// ADVERSARIAL (D51.2): a forged retry witness claiming
    /// `in_bounds: true` for a negative retry count is rejected — the
    /// checker recomputes the bound.
    #[test]
    fn forged_retry_in_bounds_rejected() {
        let mut ir = empty_ir();
        ir.endpoints.push(endpoint_retries("Liar", -5));
        let mut proofs = generate_resource_bounds_proofs(&ir, VERSION);
        if let Witness::ResourceBounds(ResourceBoundsWitness::EndpointRetry {
            ref mut in_bounds,
            ..
        }) = proofs[0].witness
        {
            *in_bounds = true; // the lie
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("disagrees with artifact"), "got: {reason}");
            }
            other => panic!("expected forged Refuted, got {other:?}"),
        }
    }

    /// ADVERSARIAL: a SocketCredit witness for a socket that has NO
    /// declared credit in the artifact is rejected (forged / stale).
    #[test]
    fn forged_socket_credit_for_unspecified_rejected() {
        let mut ir = empty_ir();
        ir.sockets.push(socket("Ghost", None)); // unspecified in artifact
        let digest = artifact_digest(&ir);
        let bogus = ProofTerm {
            property: PropertyClass::ResourceBounds,
            artifact_digest: digest,
            witness: Witness::ResourceBounds(derive_socket_credit_witness("Ghost", 8)),
            axon_version: VERSION.to_string(),
        };
        match check_proof(&bogus, &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(
                    reason.contains("no declared backpressure credit"),
                    "got: {reason}"
                );
            }
            other => panic!("expected forged-socket Refuted, got {other:?}"),
        }
    }

    /// Digest binding holds for resource-bound proofs.
    #[test]
    fn resource_proof_digest_mismatch_rejected() {
        let mut ir_a = empty_ir();
        ir_a.endpoints.push(endpoint_retries("E", 2));
        let proofs = generate_resource_bounds_proofs(&ir_a, VERSION);

        let mut ir_b = empty_ir();
        ir_b.endpoints.push(endpoint_retries("E", 2));
        ir_b.endpoints.push(endpoint_retries("Extra", 1)); // changes digest

        assert_eq!(check_proof(&proofs[0], &ir_b), CheckOutcome::DigestMismatch);
    }

    /// Resource-bound proof round-trips through JSON and still verifies.
    #[test]
    fn resource_proof_json_round_trips_and_verifies() {
        let mut ir = empty_ir();
        ir.sockets.push(socket("Chat", Some(16)));
        let proofs = generate_resource_bounds_proofs(&ir, VERSION);
        let json = serde_json::to_string(&proofs[0]).expect("serialize");
        let restored: ProofTerm = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(restored, proofs[0]);
        assert_eq!(check_proof(&restored, &ir), CheckOutcome::Verified);
    }

    // ── §Fase 51.e — ShieldHaltGuarantee ─────────────────────────────

    /// Shield with the given `on_breach` policy + `scan` categories.
    fn shield_breach(name: &str, on_breach: &str, scan: &[&str]) -> IRShield {
        let mut s = shield(name, &[]); // reuse base builder (compliance empty)
        s.on_breach = on_breach.to_string();
        s.scan = scan.iter().map(|c| c.to_string()).collect();
        s
    }

    /// Happy path: a halt shield that actually scans verifies.
    #[test]
    fn valid_halt_with_scan_verifies() {
        let mut ir = empty_ir();
        ir.shields
            .push(shield_breach("Guard", "halt", &["pii_leak"]));
        let proofs = generate_shield_halt_guarantee_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// A non-halt policy (deflect) with an empty scan verifies — the
    /// vacuous check is halt-specific; deflect makes no halt guarantee.
    #[test]
    fn non_halt_policy_with_empty_scan_verifies() {
        let mut ir = empty_ir();
        ir.shields.push(shield_breach("Soft", "deflect", &[]));
        let proofs = generate_shield_halt_guarantee_proofs(&ir, VERSION);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// §Fase 77.a (D77.6) — a sign-only egress shield (`sign: hmac_sha256`,
    /// no `scan:`) is a NON-vacuous halt: the signature is its enforcement
    /// (a breach = a delivery it refuses to sign). The brief-#51 Q3.d case.
    #[test]
    fn sign_only_halt_shield_verifies() {
        let mut ir = empty_ir();
        let mut s = shield_breach("WebhookEgress", "halt", &[]);
        s.sign = "hmac_sha256".to_string();
        ir.shields.push(s);
        let proofs = generate_shield_halt_guarantee_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// §Fase 77.a — with NEITHER `scan:` nor `sign:`, a halt stays vacuous
    /// and the proof still refutes (the pre-§77 guarantee is unchanged).
    #[test]
    fn halt_with_neither_scan_nor_sign_still_refutes() {
        let mut ir = empty_ir();
        ir.shields.push(shield_breach("Hollow", "halt", &[]));
        let proofs = generate_shield_halt_guarantee_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1);
        assert!(matches!(
            check_proof(&proofs[0], &ir),
            CheckOutcome::Refuted { .. }
        ));
    }

    /// A shield with no declared `on_breach` produces no proof.
    #[test]
    fn no_breach_policy_no_proof() {
        let mut ir = empty_ir();
        ir.shields.push(shield_breach("None", "", &["pii_leak"]));
        assert!(generate_shield_halt_guarantee_proofs(&ir, VERSION).is_empty());
    }

    /// Unknown breach policy (typo'd `hault`, which the PARSER does NOT
    /// reject — it reads `on_breach` as a bare identifier) → Refuted.
    #[test]
    fn unknown_breach_policy_refuted() {
        let mut ir = empty_ir();
        ir.shields
            .push(shield_breach("Typo", "hault", &["pii_leak"]));
        let proofs = generate_shield_halt_guarantee_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("unknown on_breach policy"), "got: {reason}");
                assert!(reason.contains("hault"), "got: {reason}");
            }
            other => panic!("expected unknown-policy Refuted, got {other:?}"),
        }
    }

    /// VACUOUS HALT: a shield declaring `on_breach: halt` with an empty
    /// `scan: []` → Refuted (the halt can never fire — security
    /// theater). This defect is NOT enforced upstream.
    #[test]
    fn vacuous_halt_refuted() {
        let mut ir = empty_ir();
        ir.shields.push(shield_breach("Theater", "halt", &[]));
        let proofs = generate_shield_halt_guarantee_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("vacuous"), "got: {reason}");
                assert!(reason.contains("never fire"), "got: {reason}");
            }
            other => panic!("expected vacuous-halt Refuted, got {other:?}"),
        }
    }

    /// ADVERSARIAL (D51.2): a forged witness claiming `vacuous_halt:
    /// false` for a halt shield that scans nothing is rejected — the
    /// checker recomputes from the artifact.
    #[test]
    fn forged_vacuous_halt_rejected() {
        let mut ir = empty_ir();
        ir.shields.push(shield_breach("Sneaky", "halt", &[]));
        let mut proofs = generate_shield_halt_guarantee_proofs(&ir, VERSION);
        if let Witness::ShieldHaltGuarantee(ref mut w) = proofs[0].witness {
            w.vacuous_halt = false; // the lie
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("disagrees with artifact"), "got: {reason}");
            }
            other => panic!("expected forged Refuted, got {other:?}"),
        }
    }

    /// Digest binding holds for shield-halt proofs.
    #[test]
    fn shield_halt_digest_mismatch_rejected() {
        let mut ir_a = empty_ir();
        ir_a.shields
            .push(shield_breach("Guard", "halt", &["pii_leak"]));
        let proofs = generate_shield_halt_guarantee_proofs(&ir_a, VERSION);

        let mut ir_b = empty_ir();
        ir_b.shields
            .push(shield_breach("Guard", "halt", &["pii_leak"]));
        ir_b.shields
            .push(shield_breach("Other", "deflect", &["toxicity"])); // changes digest

        assert_eq!(check_proof(&proofs[0], &ir_b), CheckOutcome::DigestMismatch);
    }

    /// Shield-halt proof round-trips through JSON and still verifies.
    #[test]
    fn shield_halt_json_round_trips_and_verifies() {
        let mut ir = empty_ir();
        ir.shields
            .push(shield_breach("Guard", "halt", &["pii_leak", "jailbreak"]));
        let proofs = generate_shield_halt_guarantee_proofs(&ir, VERSION);
        let json = serde_json::to_string(&proofs[0]).expect("serialize");
        let restored: ProofTerm = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(restored, proofs[0]);
        assert_eq!(check_proof(&restored, &ir), CheckOutcome::Verified);
    }

    // ── §Fase 51.x — CapabilityContainment ───────────────────────────

    /// An endpoint with the given execute_flow + declared requires.
    fn endpoint_requires(name: &str, execute_flow: &str, requires: &[&str]) -> IRAxonEndpoint {
        let mut e = endpoint(name, &[], "");
        e.execute_flow = execute_flow.to_string();
        e.requires_capabilities = requires.iter().map(|s| s.to_string()).collect();
        e
    }

    fn retrieve_step(store_name: &str) -> crate::ir_nodes::IRFlowNode {
        crate::ir_nodes::IRFlowNode::Retrieve(crate::ir_nodes::IRRetrieveStep {
            node_type: "retrieve",
            source_line: 1,
            source_column: 1,
            store_name: store_name.to_string(),
            where_expr: String::new(),
            alias: String::new(),
            order_by: String::new(),
            limit_expr: String::new(),
            aggregate: String::new(),
            group_by: String::new(),
            cache: String::new(),
        })
    }

    /// A flow whose top-level steps each retrieve from the given stores.
    fn flow_reaching(name: &str, store_names: &[&str]) -> crate::ir_nodes::IRFlow {
        crate::ir_nodes::IRFlow {
            node_type: "flow",
            source_line: 1,
            source_column: 1,
            name: name.to_string(),
            parameters: Vec::new(),
            return_type_name: String::new(),
            return_type_generic: String::new(),
            return_type_optional: false,
            steps: store_names.iter().map(|s| retrieve_step(s)).collect(),
            edges: Vec::new(),
            execution_levels: Vec::new(),
        }
    }

    /// A flow that reaches `store_name` ONLY inside a conditional
    /// then-branch (exercises the recursive store walk).
    fn flow_reaching_in_conditional(name: &str, store_name: &str) -> crate::ir_nodes::IRFlow {
        let cond = crate::ir_nodes::IRFlowNode::Conditional(crate::ir_nodes::IRConditional {
            node_type: "conditional",
            source_line: 1,
            source_column: 1,
            condition: "x".to_string(),
            comparison_op: "==".to_string(),
            comparison_value: "1".to_string(),
            then_body: vec![retrieve_step(store_name)],
            else_body: Vec::new(),
            conditions: Vec::new(),
            conjunctor: String::new(),
            cond: None,
        });
        let mut f = flow_reaching(name, &[]);
        f.steps = vec![cond];
        f
    }

    /// Happy path: the flow reaches a gated store, and the endpoint
    /// declares requiring that exact gate → contained → Verified.
    #[test]
    fn covered_containment_verifies() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Ledger", "data.read"));
        ir.flows.push(flow_reaching("Chat", &["Ledger"]));
        ir.endpoints
            .push(endpoint_requires("ChatEndpoint", "Chat", &["data.read"]));
        let proofs = generate_capability_containment_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// CAPABILITY LEAK: the flow reaches a gated store but the endpoint
    /// declares no covering requires → uncovered gate → Refuted.
    #[test]
    fn uncovered_gate_refuted() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Ledger", "data.read"));
        ir.flows.push(flow_reaching("Chat", &["Ledger"]));
        ir.endpoints.push(endpoint_requires("Leaky", "Chat", &[])); // declares nothing
        let proofs = generate_capability_containment_proofs(&ir, VERSION);
        assert_eq!(
            proofs.len(),
            1,
            "reached-gate-with-no-requires must produce a proof"
        );
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("capability leak"), "got: {reason}");
                assert!(reason.contains("data.read"), "got: {reason}");
            }
            other => panic!("expected leak Refuted, got {other:?}"),
        }
    }

    /// The recursive walk catches a store reached only inside a
    /// conditional branch → still a leak when uncovered.
    #[test]
    fn nested_conditional_reach_is_caught() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Secret", "admin.read"));
        ir.flows
            .push(flow_reaching_in_conditional("Branchy", "Secret"));
        ir.endpoints
            .push(endpoint_requires("NestedLeak", "Branchy", &[]));
        let proofs = generate_capability_containment_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("admin.read"), "got: {reason}");
            }
            other => panic!("expected nested-reach Refuted, got {other:?}"),
        }
    }

    /// An unresolvable execute_flow → Refuted (cannot certify
    /// containment for a flow not in the artifact).
    #[test]
    fn unresolved_flow_refuted() {
        let mut ir = empty_ir();
        ir.endpoints
            .push(endpoint_requires("Ghost", "NoSuchFlow", &["x.read"]));
        let proofs = generate_capability_containment_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(
                    reason.contains("not present in the artifact"),
                    "got: {reason}"
                );
            }
            other => panic!("expected unresolved-flow Refuted, got {other:?}"),
        }
    }

    /// Declaring MORE requires than reached gates is fine (verified) —
    /// containment is `reached ⊆ declared`, not equality.
    #[test]
    fn over_declared_requires_verifies() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Ledger", "data.read"));
        ir.flows.push(flow_reaching("Chat", &["Ledger"]));
        ir.endpoints.push(endpoint_requires(
            "Strict",
            "Chat",
            &["data.read", "extra.write"], // extra is harmless
        ));
        let proofs = generate_capability_containment_proofs(&ir, VERSION);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// An endpoint with no requires reaching only UNGATED stores has
    /// nothing to certify → no proof.
    #[test]
    fn trivial_no_requires_no_gates_no_proof() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Open", "")); // ungated
        ir.flows.push(flow_reaching("Chat", &["Open"]));
        ir.endpoints.push(endpoint_requires("Plain", "Chat", &[]));
        assert!(generate_capability_containment_proofs(&ir, VERSION).is_empty());
    }

    /// ADVERSARIAL (D51.2): a forged witness hiding an uncovered gate
    /// (clearing `uncovered_gates`) is rejected — the checker
    /// recomputes from the artifact.
    #[test]
    fn forged_hidden_uncovered_rejected() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Ledger", "data.read"));
        ir.flows.push(flow_reaching("Chat", &["Ledger"]));
        ir.endpoints.push(endpoint_requires("Sneaky", "Chat", &[]));
        let mut proofs = generate_capability_containment_proofs(&ir, VERSION);
        if let Witness::CapabilityContainment(ref mut w) = proofs[0].witness {
            w.uncovered_gates.clear(); // the lie
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("disagrees with artifact"), "got: {reason}");
            }
            other => panic!("expected forged Refuted, got {other:?}"),
        }
    }

    /// Digest binding holds for containment proofs.
    #[test]
    fn containment_digest_mismatch_rejected() {
        let mut ir_a = empty_ir();
        ir_a.axonstore_specs.push(store("Ledger", "data.read"));
        ir_a.flows.push(flow_reaching("Chat", &["Ledger"]));
        ir_a.endpoints
            .push(endpoint_requires("E", "Chat", &["data.read"]));
        let proofs = generate_capability_containment_proofs(&ir_a, VERSION);

        let mut ir_b = empty_ir();
        ir_b.axonstore_specs.push(store("Ledger", "data.read"));
        ir_b.flows.push(flow_reaching("Chat", &["Ledger"]));
        ir_b.endpoints
            .push(endpoint_requires("E", "Chat", &["data.read"]));
        ir_b.endpoints
            .push(endpoint_requires("Extra", "Chat", &["data.read"])); // changes digest

        assert_eq!(check_proof(&proofs[0], &ir_b), CheckOutcome::DigestMismatch);
    }

    /// Containment proof round-trips through JSON and still verifies.
    #[test]
    fn containment_json_round_trips_and_verifies() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Ledger", "data.read"));
        ir.flows.push(flow_reaching("Chat", &["Ledger"]));
        ir.endpoints
            .push(endpoint_requires("E", "Chat", &["data.read"]));
        let proofs = generate_capability_containment_proofs(&ir, VERSION);
        let json = serde_json::to_string(&proofs[0]).expect("serialize");
        let restored: ProofTerm = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(restored, proofs[0]);
        assert_eq!(check_proof(&restored, &ir), CheckOutcome::Verified);
    }

    // ── §Fase 52.a — check_bundle deployability aggregate ────────────

    /// Build the full proof bundle for an IR (mirrors the CLI/handler
    /// `generate_all_proofs` → `ProofBundle` path).
    fn bundle_for(ir: &crate::ir_nodes::IRProgram) -> ProofBundle {
        ProofBundle {
            axon_version: VERSION.to_string(),
            artifact_digest: artifact_digest(ir),
            proofs: generate_all_proofs(ir, VERSION),
        }
    }

    /// A clean program (covered compliance + halting shield) → every
    /// proof verifies → `all_verified()` true, no refutations.
    #[test]
    fn bundle_report_all_verified_for_clean_program() {
        let mut ir = empty_ir();
        ir.shields
            .push(shield_breach("Guard", "halt", &["pii_leak"]));
        ir.axonstore_specs.push(store("Ledger", "data.read"));
        ir.flows.push(flow_reaching("Chat", &["Ledger"]));
        ir.endpoints
            .push(endpoint_requires("ChatEndpoint", "Chat", &["data.read"]));

        let report = check_bundle(&bundle_for(&ir), &ir);
        assert!(
            !report.results.is_empty(),
            "clean program still yields proofs"
        );
        assert!(
            report.all_verified(),
            "every proof must verify: {:?}",
            report.refutations()
        );
        assert!(report.refutations().is_empty());
    }

    /// A leaky program (a flow reaches a gated store the endpoint does
    /// NOT declare) → the containment proof refutes → `all_verified()`
    /// false, and the refutation names the leaked capability. This is
    /// the deploy-gate signal §52.c rejects on.
    #[test]
    fn bundle_report_flags_capability_leak() {
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Ledger", "data.read"));
        ir.flows.push(flow_reaching("Chat", &["Ledger"]));
        ir.endpoints.push(endpoint_requires("Leaky", "Chat", &[])); // declares nothing

        let report = check_bundle(&bundle_for(&ir), &ir);
        assert!(
            !report.all_verified(),
            "a capability leak must be non-deployable"
        );
        let refs = report.refutations();
        assert!(!refs.is_empty());
        assert!(
            refs.iter()
                .any(|r| r.property == PropertyClass::CapabilityContainment),
            "the leak must surface as a CapabilityContainment refutation"
        );
        assert!(
            refs.iter().any(|r| matches!(&r.outcome, CheckOutcome::Refuted { reason } if reason.contains("data.read"))),
            "the refutation must name the leaked capability"
        );
    }

    /// The aggregate agrees with per-proof `check_proof`, in order — the
    /// report is just a faithful fold, no hidden policy.
    #[test]
    fn bundle_report_matches_per_proof_check() {
        let mut ir = empty_ir();
        ir.shields
            .push(shield_breach("Guard", "halt", &["pii_leak"]));
        ir.endpoints.push(endpoint("E", &["HIPAA"], "Guard"));
        let bundle = bundle_for(&ir);
        let report = check_bundle(&bundle, &ir);
        assert_eq!(report.results.len(), bundle.proofs.len());
        for (r, proof) in report.results.iter().zip(&bundle.proofs) {
            assert_eq!(r.outcome, check_proof(proof, &ir));
            assert_eq!(r.property, proof.property);
            assert_eq!(r.subject, proof.witness.subject_name());
        }
    }

    /// A bundle whose proofs are about a DIFFERENT artifact → every
    /// check is `DigestMismatch` → non-deployable (fail-closed).
    #[test]
    fn bundle_report_rejects_foreign_bundle() {
        let mut ir_a = empty_ir();
        ir_a.endpoints.push(endpoint("E", &["HIPAA"], ""));
        let bundle = bundle_for(&ir_a);

        let mut ir_b = empty_ir();
        ir_b.endpoints.push(endpoint("E", &["HIPAA"], ""));
        ir_b.endpoints.push(endpoint("Other", &["PCI_DSS"], "")); // different digest

        let report = check_bundle(&bundle, &ir_b);
        assert!(!report.all_verified());
        assert!(report
            .results
            .iter()
            .all(|r| r.outcome == CheckOutcome::DigestMismatch));
    }

    // ── §Fase 51.x.3 — no-silent-gap reachability invariant ─────────

    /// The reachability walk's match is exhaustive: it must NOT contain
    /// a `_` wildcard arm. This is the maintainable proxy for the
    /// compiler-enforced invariant (a wildcard would let a future
    /// `IRFlowNode` variant carrying a store ref / nested body be
    /// silently missed). A refactor that reintroduces a wildcard fails
    /// HERE — the §51.x.3 no-silent-gap gate.
    #[test]
    fn reachability_walk_has_no_wildcard_arm() {
        const GEN_SRC: &str = include_str!("generate.rs");
        let start = GEN_SRC
            .find("fn collect_store_accesses(")
            .expect("collect_store_accesses present");
        // Bound the slice to the function body (up to the next top-level
        // `pub fn`, which is derive_capability_containment_witness).
        let rest = &GEN_SRC[start..];
        let end = rest
            .find("\npub fn derive_capability_containment_witness")
            .expect("derive_capability_containment_witness follows");
        let body = &rest[..end];
        assert!(
            !body.contains("_ => {}") && !body.contains("_ =>"),
            "collect_store_accesses must stay an EXHAUSTIVE match with no \
             `_` wildcard — a wildcard re-opens the §51.x.3 silent-gap risk \
             (a future IRFlowNode variant could carry a store ref / nested \
             body and be missed)"
        );
    }

    /// The walk documents where transitive cross-flow reachability must
    /// be reopened if a flow-invocation node ever enters `IRFlowNode`
    /// (today `IRRun` is top-level only, so the case is vacuous — see
    /// §51.x.3 recon). Pin the sentinel so the note can't be silently
    /// dropped.
    #[test]
    fn reachability_walk_documents_transitive_reopen() {
        const GEN_SRC: &str = include_str!("generate.rs");
        assert!(
            GEN_SRC.contains("REOPENED (§51.x.3)") || GEN_SRC.contains("REOPEN"),
            "the walk must keep the note on where to reopen transitive \
             cross-flow reachability if a flow-invocation node ever joins \
             IRFlowNode"
        );
    }

    /// Leaf nodes (here: a payload-free `Break`) contribute NOTHING to
    /// the reachable-gate set — only the store op is counted. Confirms
    /// the leaf classification is inert.
    #[test]
    fn leaf_nodes_contribute_no_gates() {
        use crate::ir_nodes::{IRBreakStep, IRFlowNode};
        let mut ir = empty_ir();
        ir.axonstore_specs.push(store("Ledger", "data.read"));
        // A flow whose body interleaves a leaf node with the one store op.
        let mut flow = flow_reaching("Chat", &["Ledger"]);
        flow.steps.push(IRFlowNode::Break(IRBreakStep {
            node_type: "break",
            source_line: 1,
            source_column: 1,
        }));
        ir.flows.push(flow);
        let w = derive_capability_containment_witness("E", "Chat", &["data.read".to_string()], &ir);
        // Only the gated store contributes; the leaf adds nothing.
        assert_eq!(w.reached_gates, vec!["data.read".to_string()]);
        assert!(w.uncovered_gates.is_empty());
    }

    /// An empty bundle is vacuously deployable (nothing to refute).
    #[test]
    fn empty_bundle_is_vacuously_verified() {
        let ir = empty_ir();
        let bundle = ProofBundle {
            axon_version: VERSION.to_string(),
            artifact_digest: artifact_digest(&ir),
            proofs: Vec::new(),
        };
        let report = check_bundle(&bundle, &ir);
        assert!(report.all_verified());
        assert!(report.refutations().is_empty());
    }

    // ── §Fase 58.i — ToolCallSoundness ───────────────────────────────

    /// A tool declaring the given `(name, type, optional)` input schema.
    fn tool_with_params(name: &str, params: &[(&str, &str, bool)]) -> IRToolSpec {
        let mut t = tool(name, &[]); // reuse base (effect_row empty)
        t.provider = "http".to_string();
        t.parameters = params
            .iter()
            .map(|(n, ty, opt)| crate::ir_nodes::IRToolParam {
                name: n.to_string(),
                type_name: ty.to_string(),
                optional: *opt,
            })
            .collect();
        t
    }

    /// A flow-level `use <Tool>(k = v, …)` call node.
    fn use_named(tool_name: &str, args: &[(&str, &str)]) -> crate::ir_nodes::IRFlowNode {
        crate::ir_nodes::IRFlowNode::UseTool(crate::ir_nodes::IRUseToolStep {
            node_type: "use_tool",
            source_line: 1,
            source_column: 1,
            tool_name: tool_name.to_string(),
            argument: String::new(),
            named_args: args
                .iter()
                .map(|(n, v)| crate::ir_nodes::IRNamedArg {
                    name: n.to_string(),
                    value: v.to_string(),
                    value_kind: "literal".to_string(),
                })
                .collect(),
        })
    }

    /// The legacy positional `use <Tool> on <arg>` call node (no named args).
    fn use_legacy(tool_name: &str, arg: &str) -> crate::ir_nodes::IRFlowNode {
        crate::ir_nodes::IRFlowNode::UseTool(crate::ir_nodes::IRUseToolStep {
            node_type: "use_tool",
            source_line: 1,
            source_column: 1,
            tool_name: tool_name.to_string(),
            argument: arg.to_string(),
            named_args: Vec::new(),
        })
    }

    /// A flow whose body is the given step nodes.
    fn flow_with_steps(name: &str, steps: Vec<crate::ir_nodes::IRFlowNode>) -> crate::ir_nodes::IRFlow {
        let mut f = flow_reaching(name, &[]);
        f.steps = steps;
        f
    }

    /// The canonical schema for the tests: company:String, max_results:Int,
    /// active:Bool? (active optional).
    fn crm_tool() -> IRToolSpec {
        tool_with_params(
            "CrmRadar",
            &[
                ("company", "String", false),
                ("max_results", "Int", false),
                ("active", "Bool", true),
            ],
        )
    }

    /// Happy path: a structured call satisfying the schema verifies. The
    /// bare-identifier `company` arg is runtime-resolved (skipped); the
    /// Int + Bool literals align; the optional `active` may be omitted.
    #[test]
    fn sound_tool_call_verifies() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![use_named("CrmRadar", &[("company", "company"), ("max_results", "5")])],
        ));
        let proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1, "one schema-full structured call => one proof");
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// An unknown argument name → Refuted.
    #[test]
    fn unknown_arg_refuted() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![use_named(
                "CrmRadar",
                &[("company", "x"), ("max_results", "5"), ("bogus", "1")],
            )],
        ));
        let proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("does not declare"), "got: {reason}");
                assert!(reason.contains("bogus"), "got: {reason}");
            }
            other => panic!("expected unknown-arg Refuted, got {other:?}"),
        }
    }

    /// A duplicate argument → Refuted.
    #[test]
    fn duplicate_arg_refuted() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![use_named(
                "CrmRadar",
                &[("company", "a"), ("company", "b"), ("max_results", "5")],
            )],
        ));
        let proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("duplicate"), "got: {reason}");
                assert!(reason.contains("company"), "got: {reason}");
            }
            other => panic!("expected duplicate Refuted, got {other:?}"),
        }
    }

    /// A missing required argument → Refuted.
    #[test]
    fn missing_required_refuted() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        // omit `max_results` (required); supply only `company`.
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![use_named("CrmRadar", &[("company", "x")])],
        ));
        let proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("omits required"), "got: {reason}");
                assert!(reason.contains("max_results"), "got: {reason}");
            }
            other => panic!("expected missing-required Refuted, got {other:?}"),
        }
    }

    /// An optional param omitted verifies (not "missing required").
    #[test]
    fn optional_param_omitted_verifies() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        // `active` is optional → omitting it is fine.
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![use_named("CrmRadar", &[("company", "x"), ("max_results", "5")])],
        ));
        let proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// A literal type mismatch (Int literal into a String param) → Refuted.
    #[test]
    fn literal_type_mismatch_refuted() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        // company:String given the Int literal `5`.
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![use_named("CrmRadar", &[("company", "5"), ("max_results", "5")])],
        ));
        let proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("type mismatch"), "got: {reason}");
                assert!(reason.contains("company:String:Int"), "got: {reason}");
            }
            other => panic!("expected type-mismatch Refuted, got {other:?}"),
        }
    }

    /// Int coerces into a Float parameter (no mismatch).
    #[test]
    fn int_into_float_param_verifies() {
        let mut ir = empty_ir();
        ir.tools
            .push(tool_with_params("Calc", &[("ratio", "Float", false)]));
        ir.flows.push(flow_with_steps(
            "Run",
            vec![use_named("Calc", &[("ratio", "5")])], // Int literal into Float
        ));
        let proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    /// A schema-less tool (no `parameters:`) produces NO proof — there is
    /// no arg contract to certify.
    #[test]
    fn schema_less_tool_no_proof() {
        let mut ir = empty_ir();
        ir.tools.push(tool("Plain", &["network"])); // no parameters
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![use_named("Plain", &[("anything", "1")])],
        ));
        assert!(generate_tool_call_soundness_proofs(&ir, VERSION).is_empty());
    }

    /// The legacy `use <Tool> on <arg>` form produces NO proof (schema-
    /// less by construction — D5 back-compat).
    #[test]
    fn legacy_on_form_no_proof() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        ir.flows
            .push(flow_with_steps("Scan", vec![use_legacy("CrmRadar", "${q}")]));
        assert!(generate_tool_call_soundness_proofs(&ir, VERSION).is_empty());
    }

    /// Two calls to the SAME tool (one sound, one defective) are
    /// distinguished by `call_index`: the sound one verifies, the
    /// defective one refutes.
    #[test]
    fn two_calls_same_tool_distinguished() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![
                use_named("CrmRadar", &[("company", "x"), ("max_results", "5")]), // sound
                use_named("CrmRadar", &[("company", "x"), ("bogus", "1")]),       // defective
            ],
        ));
        let proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 2);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
        match check_proof(&proofs[1], &ir) {
            CheckOutcome::Refuted { reason } => {
                // proofs[1] is the defective call: it omits `max_results`
                // AND passes the unknown `bogus`.
                assert!(
                    reason.contains("bogus") || reason.contains("max_results"),
                    "got: {reason}"
                );
            }
            other => panic!("expected Refuted for the defective call, got {other:?}"),
        }
    }

    /// A defective call nested inside a conditional branch is caught (the
    /// recursive walk descends into then/else bodies).
    #[test]
    fn nested_conditional_call_is_caught() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        let cond = crate::ir_nodes::IRFlowNode::Conditional(crate::ir_nodes::IRConditional {
            node_type: "conditional",
            source_line: 1,
            source_column: 1,
            condition: "x".to_string(),
            comparison_op: "==".to_string(),
            comparison_value: "1".to_string(),
            then_body: vec![use_named("CrmRadar", &[("company", "x"), ("ghost", "1")])],
            else_body: Vec::new(),
            conditions: Vec::new(),
            conjunctor: String::new(),
            cond: None,
        });
        ir.flows.push(flow_with_steps("Branchy", vec![cond]));
        let proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1, "the nested call must be discovered");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("ghost"), "got: {reason}");
            }
            other => panic!("expected nested-call Refuted, got {other:?}"),
        }
    }

    /// ADVERSARIAL (D51.2): a forged witness hiding an unknown argument
    /// (clearing `unknown_args`) is rejected — the checker recomputes.
    #[test]
    fn forged_hidden_unknown_arg_rejected() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![use_named(
                "CrmRadar",
                &[("company", "x"), ("max_results", "5"), ("sneaky", "1")],
            )],
        ));
        let mut proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        if let Witness::ToolCallSoundness(ref mut w) = proofs[0].witness {
            w.unknown_args.clear(); // the lie
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("disagrees with artifact"), "got: {reason}");
            }
            other => panic!("expected forged Refuted, got {other:?}"),
        }
    }

    /// ADVERSARIAL: a witness naming a call index absent from the artifact
    /// is Refuted (not a panic).
    #[test]
    fn proof_for_absent_call_index_refuted() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![use_named("CrmRadar", &[("company", "x"), ("max_results", "5")])],
        ));
        let mut proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        if let Witness::ToolCallSoundness(ref mut w) = proofs[0].witness {
            w.call_index = 9; // no such call
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("no structured `use` call at index"), "got: {reason}");
            }
            other => panic!("expected absent-call Refuted, got {other:?}"),
        }
    }

    /// Digest binding holds for tool-call proofs.
    #[test]
    fn tool_call_proof_digest_mismatch_rejected() {
        let mut ir_a = empty_ir();
        ir_a.tools.push(crm_tool());
        ir_a.flows.push(flow_with_steps(
            "Scan",
            vec![use_named("CrmRadar", &[("company", "x"), ("max_results", "5")])],
        ));
        let proofs = generate_tool_call_soundness_proofs(&ir_a, VERSION);

        let mut ir_b = empty_ir();
        ir_b.tools.push(crm_tool());
        ir_b.flows.push(flow_with_steps(
            "Scan",
            vec![use_named("CrmRadar", &[("company", "x"), ("max_results", "5")])],
        ));
        ir_b.tools.push(tool("Extra", &["network"])); // changes digest

        assert_eq!(check_proof(&proofs[0], &ir_b), CheckOutcome::DigestMismatch);
    }

    /// Tool-call proof round-trips through JSON and still verifies.
    #[test]
    fn tool_call_proof_json_round_trips_and_verifies() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![use_named(
                "CrmRadar",
                &[("company", "x"), ("max_results", "5"), ("active", "true")],
            )],
        ));
        let proofs = generate_tool_call_soundness_proofs(&ir, VERSION);
        let json = serde_json::to_string(&proofs[0]).expect("serialize");
        let restored: ProofTerm = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(restored, proofs[0]);
        assert_eq!(check_proof(&restored, &ir), CheckOutcome::Verified);
    }

    /// `generate_all_proofs` includes the tool-call-soundness class, and a
    /// defective call makes the bundle non-deployable (the §52 deploy-gate
    /// signal).
    #[test]
    fn all_proofs_includes_tool_call_soundness_and_flags_defect() {
        let mut ir = empty_ir();
        ir.tools.push(crm_tool());
        ir.flows.push(flow_with_steps(
            "Scan",
            vec![use_named("CrmRadar", &[("company", "5"), ("max_results", "5")])], // Int into String
        ));
        let bundle = bundle_for(&ir);
        assert!(
            bundle
                .proofs
                .iter()
                .any(|p| p.property == PropertyClass::ToolCallSoundness),
            "generate_all_proofs must include the tool-call-soundness class"
        );
        let report = check_bundle(&bundle, &ir);
        assert!(!report.all_verified(), "a literal type mismatch is non-deployable");
        assert!(report
            .refutations()
            .iter()
            .any(|r| r.property == PropertyClass::ToolCallSoundness));
    }

    // ── §Fase 72.f — EffectBudgeted ──────────────────────────────────────

    /// Parse → IR (no type-check, so a deliberately-defective budget survives to
    /// the checker, which is the independent verifier under test).
    fn ir_from_source(src: &str) -> crate::ir_nodes::IRProgram {
        let toks = axon_frontend::lexer::Lexer::new(src, "<pcc-test>").tokenize().unwrap();
        let prog = axon_frontend::parser::Parser::new(toks).parse().unwrap();
        axon_frontend::ir_generator::IRGenerator::new().generate(&prog)
    }

    const BUDGETED_DAEMON: &str = "tool TelnyxCall { provider: http timeout: 5s }\n\
         flow SendBatch() -> Unit { step S { ask: \"x\" output: Unit } }\n\
         daemon OutboundScheduler {\n\
           requires: [flow.execute]\n\
           budget {\n\
             rate: 8 per hour on Tool(TelnyxCall)\n\
             max: 50 per day on Tool(TelnyxCall)\n\
             on_exhausted: defer\n\
           }\n\
           listen \"cron:*/5 * * * *\" as t { run SendBatch() }\n\
         }";

    #[test]
    fn effect_budgeted_round_trips_and_verifies() {
        let ir = ir_from_source(BUDGETED_DAEMON);
        let proofs = super::generate::generate_effect_budgeted_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one proof per budgeted daemon");
        assert_eq!(proofs[0].property, PropertyClass::EffectBudgeted);
        // The independent checker verifies a well-formed budget.
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    // ── §87.g — SavantSoundness ──────────────────────────────────────────────

    const VALID_SAVANT: &str = "memory ResearchStore { store: persistent }\n\
         savant DeepAnalyst {\n\
           domain: \"quantum error correction\"\n\
           cognition { depth: hyper, entropic_threshold: 0.001, divergence: high }\n\
           memory { backend: ResearchStore, corpus_graph: true }\n\
           budget { max_iterations: 50000 }\n\
           mandate m { objective: \"synthesise papers\", output: Report }\n\
         }";

    #[test]
    fn savant_soundness_round_trips_and_verifies() {
        let ir = ir_from_source(VALID_SAVANT);
        let proofs = super::generate::generate_savant_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one proof per savant");
        assert_eq!(proofs[0].property, PropertyClass::SavantSoundness);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
        assert!(
            generate_all_proofs(&ir, "test")
                .iter()
                .any(|p| p.property == PropertyClass::SavantSoundness),
            "generate_all_proofs must include the savant-soundness class"
        );
    }

    #[test]
    fn savant_soundness_refutes_unbudgeted() {
        // Parses + lowers, but declares no budget → the PCC checker REFUTES it
        // (T877 — an unbounded autonomous loop is fail-open).
        let ir = ir_from_source(
            "savant S { domain: \"d\" mandate m { objective: \"o\", output: T } }",
        );
        let proofs = super::generate::generate_savant_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        assert!(matches!(
            check_proof(&proofs[0], &ir),
            CheckOutcome::Refuted { .. }
        ));
    }

    #[test]
    fn savant_soundness_refutes_undefined_memory() {
        let ir = ir_from_source(
            "savant S { domain: \"d\" memory { backend: Nope } budget { max_iterations: 5 } \
             mandate m { objective: \"o\", output: T } }",
        );
        let proofs = super::generate::generate_savant_soundness_proofs(&ir, "test");
        assert!(matches!(
            check_proof(&proofs[0], &ir),
            CheckOutcome::Refuted { .. }
        ));
    }

    #[test]
    fn savant_soundness_refutes_forged_witness() {
        let ir = ir_from_source(VALID_SAVANT);
        let mut proof = super::generate::generate_savant_soundness_proofs(&ir, "test").remove(0);
        // Tamper: claim a budget bound the artifact does not carry.
        if let Witness::SavantSoundness(ref mut w) = proof.witness {
            w.max_iterations = 999_999;
        }
        assert!(matches!(
            check_proof(&proof, &ir),
            CheckOutcome::Refuted { .. }
        ));
    }

    // ── §88.e — WardenSoundness ──────────────────────────────────────────────

    const VALID_WARDEN: &str = "scope InternalAudit {\n\
           targets: [ \"svc://payments\" ]\n\
           depth: static_artifact\n\
           approver: requires \"security.lead\"\n\
         }\n\
         flow Audit() -> Unit {\n\
           warden(payments) within InternalAudit { step S { ask: \"x\" output: Unit } }\n\
         }";

    #[test]
    fn warden_soundness_round_trips_and_verifies() {
        let ir = ir_from_source(VALID_WARDEN);
        let proofs = super::generate::generate_warden_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one proof per warden block");
        assert_eq!(proofs[0].property, PropertyClass::WardenSoundness);
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
        assert!(
            generate_all_proofs(&ir, "test")
                .iter()
                .any(|p| p.property == PropertyClass::WardenSoundness),
            "generate_all_proofs must include the warden-soundness class"
        );
    }

    #[test]
    fn warden_soundness_refutes_undefined_scope() {
        // Parses + lowers, but `within Ghost` resolves to no declared scope → the
        // PCC checker REFUTES it (T887 — no authorization scope, no analysis).
        let ir = ir_from_source(
            "flow Audit() -> Unit { warden(t) within Ghost { step S { ask: \"x\" output: Unit } } }",
        );
        let proofs = super::generate::generate_warden_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        assert!(matches!(
            check_proof(&proofs[0], &ir),
            CheckOutcome::Refuted { .. }
        ));
    }

    #[test]
    fn warden_soundness_refutes_forged_witness() {
        let ir = ir_from_source(VALID_WARDEN);
        let mut proof = super::generate::generate_warden_soundness_proofs(&ir, "test").remove(0);
        // Tamper: claim the scope resolves to a different name than the artifact.
        if let Witness::WardenSoundness(ref mut w) = proof.witness {
            w.approver_present = false;
        }
        assert!(matches!(
            check_proof(&proof, &ir),
            CheckOutcome::Refuted { .. }
        ));
    }

    #[test]
    fn budgetless_daemon_carries_no_effect_budgeted_proof() {
        let ir = ir_from_source(
            "flow SendBatch() -> Unit { step S { ask: \"x\" output: Unit } }\n\
             daemon Plain {\n\
               requires: [flow.execute]\n\
               listen \"cron:*/5 * * * *\" as t { run SendBatch() }\n\
             }",
        );
        assert!(super::generate::generate_effect_budgeted_proofs(&ir, "test").is_empty());
    }

    #[test]
    fn effect_budgeted_refutes_an_undefined_tool() {
        // A budget targeting an undeclared tool PARSES (the type-checker would
        // T830 it, but we skip that) — the independent PCC checker REFUTES it.
        let ir = ir_from_source(
            "flow SendBatch() -> Unit { step S { ask: \"x\" output: Unit } }\n\
             daemon D {\n\
               requires: [flow.execute]\n\
               budget { rate: 8 per hour on Tool(NoSuchTool) }\n\
               listen \"cron:*/5 * * * *\" as t { run SendBatch() }\n\
             }",
        );
        let proofs = super::generate::generate_effect_budgeted_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("NoSuchTool"), "{reason}"),
            other => panic!("expected Refuted, got {other:?}"),
        }
    }

    #[test]
    fn effect_budgeted_refutes_a_forged_witness() {
        let ir = ir_from_source(BUDGETED_DAEMON);
        let mut proofs = super::generate::generate_effect_budgeted_proofs(&ir, "test");
        // Forge the witness: claim a clean budget had an unresolved effect.
        if let Witness::EffectBudgeted(ref mut w) = proofs[0].witness {
            w.unresolved_effects = vec!["Ghost".to_string()];
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("disagrees"), "{reason}")
            }
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    // ── §Fase 73.g — JsonShapeSoundness ──────────────────────────────────

    const LENS_STORE: &str = "type UserEvent { name: String age: Int }\n\
         axonstore Events {\n\
           backend: postgresql\n\
           connection: \"env:DB\"\n\
           schema {\n\
             id: Uuid primary_key\n\
             profile: Json<UserEvent>\n\
           }\n\
         }";

    #[test]
    fn json_shape_soundness_round_trips_and_verifies() {
        let ir = ir_from_source(LENS_STORE);
        let proofs = super::generate::generate_json_shape_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one proof per store with a lens column");
        assert_eq!(proofs[0].property, PropertyClass::JsonShapeSoundness);
        // The shape `UserEvent` is a declared struct → the checker verifies.
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn open_json_store_carries_no_json_shape_proof() {
        // A store whose only Json column is OPEN (no `<T>` lens) declares no
        // shape contract → no proof.
        let ir = ir_from_source(
            "axonstore Events {\n\
               backend: postgresql\n\
               connection: \"env:DB\"\n\
               schema { id: Uuid primary_key  payload: Json }\n\
             }",
        );
        assert!(super::generate::generate_json_shape_soundness_proofs(&ir, "test").is_empty());
    }

    #[test]
    fn json_shape_soundness_refutes_an_undeclared_shape() {
        // `Json<Ghost>` with no `type Ghost` PARSES (the type-checker would
        // axon-T840 it, but we skip that) — the independent PCC checker REFUTES.
        let ir = ir_from_source(
            "axonstore Events {\n\
               backend: postgresql\n\
               connection: \"env:DB\"\n\
               schema { id: Uuid primary_key  profile: Json<Ghost> }\n\
             }",
        );
        let proofs = super::generate::generate_json_shape_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("Ghost"), "{reason}")
            }
            other => panic!("expected Refuted, got {other:?}"),
        }
    }

    #[test]
    fn json_shape_soundness_refutes_a_forged_witness() {
        let ir = ir_from_source(LENS_STORE);
        let mut proofs = super::generate::generate_json_shape_soundness_proofs(&ir, "test");
        // Forge: claim a clean store had an unresolved shape.
        if let Witness::JsonShapeSoundness(ref mut w) = proofs[0].witness {
            w.unresolved_shapes = vec!["profile:Ghost".to_string()];
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("disagrees"), "{reason}")
            }
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    // ── §Fase 74.g — ChannelDeliverySoundness ────────────────────────────

    const PRODUCED_CHANNEL: &str = "type Hib { tenant_id: String }\n\
         channel HibCh { message: Hib  qos: at_least_once  persistence: persistent_axonstore }\n\
         flow Produce(tenant_id: String) -> Unit { emit HibCh(tenant_id) }\n\
         flow Learn(tenant_id: String) -> Unit { probe p }\n\
         daemon IntentLearner {\n\
           requires: [flow.execute]\n\
           listen HibCh as ev { run Learn() }\n\
         }";

    #[test]
    fn channel_delivery_round_trips_and_verifies() {
        let ir = ir_from_source(PRODUCED_CHANNEL);
        let proofs = super::generate::generate_channel_delivery_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one proof per listened channel");
        assert_eq!(proofs[0].property, PropertyClass::ChannelDeliverySoundness);
        // The channel has a producer (`emit HibCh`) → verifies.
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn unlistened_channel_carries_no_delivery_proof() {
        // A channel emitted to but with NO `listen`er → no delivery contract.
        let ir = ir_from_source(
            "type Hib { tenant_id: String }\n\
             channel HibCh { message: Hib }\n\
             flow Produce(tenant_id: String) -> Unit { emit HibCh(tenant_id) }",
        );
        assert!(super::generate::generate_channel_delivery_soundness_proofs(&ir, "test").is_empty());
    }

    #[test]
    fn channel_delivery_refutes_a_listener_with_no_producer() {
        // The Kivi brief #39 defect: a daemon listens on a channel NOTHING
        // emits to → the listener can never fire → REFUTED.
        let ir = ir_from_source(
            "type Hib { tenant_id: String }\n\
             channel HibCh { message: Hib  persistence: persistent_axonstore }\n\
             flow Learn() -> Unit { probe p }\n\
             daemon IntentLearner {\n\
               requires: [flow.execute]\n\
               listen HibCh as ev { run Learn() }\n\
             }",
        );
        let proofs = super::generate::generate_channel_delivery_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("no producer") || reason.contains("never fire"), "{reason}")
            }
            other => panic!("expected Refuted (no producer), got {other:?}"),
        }
    }

    #[test]
    fn channel_delivery_refutes_a_forged_witness() {
        let ir = ir_from_source(PRODUCED_CHANNEL);
        let mut proofs = super::generate::generate_channel_delivery_soundness_proofs(&ir, "test");
        // Forge: claim there was no producer when the artifact has one.
        if let Witness::ChannelDeliverySoundness(ref mut w) = proofs[0].witness {
            w.has_producer = false;
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("disagrees"), "{reason}"),
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    // ── §Fase 76.e — AggregateSoundness ──────────────────────────────────

    const AGGREGATE_RETRIEVE: &str = "axonstore Tenants {\n\
           backend: postgresql\n\
           connection: \"env:DB\"\n\
         }\n\
         flow PlatformStats() -> Unit {\n\
           retrieve Tenants {\n\
             where: \"tokens > 0\"\n\
             aggregate: \"sum(tokens)\"\n\
             group_by: \"industry\"\n\
             as: stats\n\
           }\n\
         }";

    #[test]
    fn aggregate_soundness_round_trips_and_verifies() {
        let ir = ir_from_source(AGGREGATE_RETRIEVE);
        let proofs = super::generate::generate_aggregate_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one proof per aggregate-retrieve site");
        assert_eq!(proofs[0].property, PropertyClass::AggregateSoundness);
        if let Witness::AggregateSoundness(ref w) = proofs[0].witness {
            assert_eq!(w.function, "sum");
            assert_eq!(w.column, "tokens");
            assert_eq!(w.group_columns, vec!["industry".to_string()]);
            assert!(w.violations.is_empty());
        } else {
            panic!("expected an AggregateSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn plain_retrieve_carries_no_aggregate_proof() {
        let ir = ir_from_source(
            "axonstore Tenants {\n\
               backend: postgresql\n\
               connection: \"env:DB\"\n\
             }\n\
             flow List() -> Unit {\n\
               retrieve Tenants { where: \"tokens > 0\"  as: rows }\n\
             }",
        );
        assert!(super::generate::generate_aggregate_soundness_proofs(&ir, "test").is_empty());
    }

    #[test]
    fn aggregate_soundness_refutes_an_unsound_clause() {
        // group_by without an aggregate — a T845-class violation the
        // runtime parser records; the proof generates but REFUTES, so the
        // §52 deploy gate rejects the bundle fail-closed.
        let ir = ir_from_source(
            "axonstore Tenants {\n\
               backend: postgresql\n\
               connection: \"env:DB\"\n\
             }\n\
             flow Bad() -> Unit {\n\
               retrieve Tenants { group_by: \"industry\"  as: rows }\n\
             }",
        );
        let proofs = super::generate::generate_aggregate_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("UNSOUND"), "{reason}")
            }
            other => panic!("expected Refuted (unsound clause), got {other:?}"),
        }
    }

    #[test]
    fn aggregate_soundness_refutes_a_forged_witness() {
        let ir = ir_from_source(AGGREGATE_RETRIEVE);
        let mut proofs = super::generate::generate_aggregate_soundness_proofs(&ir, "test");
        // Forge: claim the aggregate was a different clause than declared.
        if let Witness::AggregateSoundness(ref mut w) = proofs[0].witness {
            w.aggregate = "count".to_string();
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("forged") || reason.contains("no aggregate-retrieve"), "{reason}")
            }
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    // ── §Fase 77.b — ChannelEgressSoundness ──────────────────────────────

    /// The brief-#51 target program: a durable channel egress-published
    /// under a sign-only shield.
    const EGRESS_CHANNEL: &str = "type SkillResult { task_id: String }\n\
         shield WebhookEgress { sign: hmac_sha256  on_breach: halt }\n\
         channel SkillCompleted { message: SkillResult  qos: at_least_once  \
           lifetime: affine  persistence: persistent_axonstore  shield: WebhookEgress }\n\
         flow CompleteSkill(task_id: String) -> Unit {\n\
           emit SkillCompleted(task_id)\n\
           publish SkillCompleted within WebhookEgress\n\
         }";

    #[test]
    fn channel_egress_round_trips_and_verifies() {
        let ir = ir_from_source(EGRESS_CHANNEL);
        // The lowering stamped the handle (§77.b Phase 1.5)…
        assert_eq!(ir.channels[0].egress_sign, "hmac_sha256");
        // …and the proof re-derives + verifies it.
        let proofs = super::generate::generate_channel_egress_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one proof per egress channel");
        assert_eq!(proofs[0].property, PropertyClass::ChannelEgressSoundness);
        if let Witness::ChannelEgressSoundness(ref w) = proofs[0].witness {
            assert_eq!(w.declared_egress_sign, "hmac_sha256");
            assert_eq!(w.derived_sign, "hmac_sha256");
            assert_eq!(w.shield_ref, "WebhookEgress");
            assert!(w.durable);
        } else {
            panic!("expected a ChannelEgressSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn non_signing_publish_carries_no_egress_proof() {
        // A publish under a SCANNING shield is pure π-calc capability
        // extrusion — no egress contract, no proof (pre-§77 semantics).
        let ir = ir_from_source(
            "type T { id: String }\n\
             shield Gate { scan: [pii_leak]  on_breach: halt }\n\
             channel C { message: T  shield: Gate }\n\
             flow F(id: String) -> Unit { publish C within Gate }",
        );
        assert!(ir.channels[0].egress_sign.is_empty());
        assert!(
            super::generate::generate_channel_egress_soundness_proofs(&ir, "test").is_empty()
        );
    }

    #[test]
    fn channel_egress_refutes_an_ephemeral_channel() {
        // Signed egress on a non-durable channel — the promise dies with
        // the process (D77.6). The compile-time mirror is axon-T848; the
        // proof REFUTES independently so a handcrafted IR cannot sneak by.
        let ir = ir_from_source(
            "type T { id: String }\n\
             shield WebhookEgress { sign: hmac_sha256  on_breach: halt }\n\
             channel C { message: T  shield: WebhookEgress }\n\
             flow F(id: String) -> Unit { publish C within WebhookEgress }",
        );
        let proofs = super::generate::generate_channel_egress_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("persistent_axonstore"), "{reason}")
            }
            other => panic!("expected Refuted (ephemeral egress), got {other:?}"),
        }
    }

    #[test]
    fn channel_egress_refutes_a_forged_handle() {
        // Forge the CHANNEL HANDLE (what the enterprise egress worker
        // reads): stamp an egress marking the program's publish sites do
        // not derive. The checker recomputes from the shields — refuted.
        let mut ir = ir_from_source(
            "type T { id: String }\n\
             shield Gate { scan: [pii_leak]  on_breach: halt }\n\
             channel C { message: T  persistence: persistent_axonstore  shield: Gate }\n\
             flow F(id: String) -> Unit { publish C within Gate }",
        );
        ir.channels[0].egress_sign = "hmac_sha256".to_string();
        let proofs = super::generate::generate_channel_egress_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "the forged handle now carries a contract");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("disagrees") || reason.contains("derivable"), "{reason}")
            }
            other => panic!("expected Refuted (forged handle), got {other:?}"),
        }
    }

    #[test]
    fn channel_egress_refutes_a_forged_witness() {
        let ir = ir_from_source(EGRESS_CHANNEL);
        let mut proofs = super::generate::generate_channel_egress_soundness_proofs(&ir, "test");
        // Forge: claim the egress was ephemeral-safe by lying about
        // persistence.
        if let Witness::ChannelEgressSoundness(ref mut w) = proofs[0].witness {
            w.persistence = "ephemeral".to_string();
            w.durable = false;
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("disagrees"), "{reason}"),
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    // ── §Fase 83.c — CorsPolicyConsistency ───────────────────────────────

    const CORS_PROGRAM: &str = "flow Chat() -> Unit { step S { ask: \"hi\" } }\n\
         cors PublicWebCors {\n\
           allow_origins: [\"https://app.example.com\"]\n\
           allow_methods: [GET, POST]\n\
           allow_credentials: false\n\
         }\n\
         axonendpoint E { method: POST path: \"/chat\" execute: Chat cors: PublicWebCors }";

    #[test]
    fn cors_policy_consistency_round_trips_and_verifies() {
        let ir = ir_from_source(CORS_PROGRAM);
        let proofs = super::generate::generate_cors_policy_consistency_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program proof when a cors contract exists");
        assert_eq!(proofs[0].property, PropertyClass::CorsPolicyConsistency);
        if let Witness::CorsPolicyConsistency(ref w) = proofs[0].witness {
            assert_eq!(w.declared_cors_names, vec!["PublicWebCors".to_string()]);
            assert_eq!(
                w.endpoint_cors_refs,
                vec![("E".to_string(), "PublicWebCors".to_string())]
            );
            assert!(w.all_references_resolve);
            assert!(w.wildcard_credential_violations.is_empty());
            assert!(w.cross_method_conflicts.is_empty());
        } else {
            panic!("expected a CorsPolicyConsistency witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn cors_less_program_carries_no_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_cors_policy_consistency_proofs(&ir, "test").is_empty(),
            "no contract → no proof"
        );
    }

    #[test]
    fn cors_policy_consistency_refutes_a_forged_reference() {
        // Forge: the witness claims a reference the artifact doesn't have.
        let ir = ir_from_source(CORS_PROGRAM);
        let mut proofs = super::generate::generate_cors_policy_consistency_proofs(&ir, "test");
        if let Witness::CorsPolicyConsistency(ref mut w) = proofs[0].witness {
            w.endpoint_cors_refs.push(("Ghost".to_string(), "PublicWebCors".to_string()));
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("disagrees"), "{reason}"),
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    #[test]
    fn cors_policy_consistency_refutes_an_unresolved_reference() {
        // Hand-craft an IR (bypassing the checker that would have caught
        // this at compile time, axon-T856) where the endpoint's cors_ref
        // names a declaration that doesn't exist.
        let mut ir = ir_from_source(CORS_PROGRAM);
        ir.endpoints[0].cors_ref = "Ghost".to_string();
        let proofs = super::generate::generate_cors_policy_consistency_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T856"), "{reason}"),
            other => panic!("expected Refuted (unresolved reference), got {other:?}"),
        }
    }

    #[test]
    fn cors_policy_consistency_refutes_wildcard_plus_credentials() {
        // Hand-craft an IR with the forbidden pairing (bypassing axon-T853).
        let mut ir = ir_from_source(CORS_PROGRAM);
        ir.cors_policies[0].allow_origins = vec!["*".to_string()];
        ir.cors_policies[0].allow_credentials = true;
        let proofs = super::generate::generate_cors_policy_consistency_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T853"), "{reason}"),
            other => panic!("expected Refuted (wildcard+credentials), got {other:?}"),
        }
    }

    #[test]
    fn cors_policy_consistency_refutes_a_cross_method_conflict() {
        // Hand-craft an IR with two endpoints on one path disagreeing on
        // cors_ref (bypassing axon-T857).
        let mut ir = ir_from_source(CORS_PROGRAM);
        let mut second = ir.endpoints[0].clone();
        second.name = "E2".to_string();
        second.method = "GET".to_string();
        second.cors_ref = String::new();
        ir.endpoints.push(second);
        let proofs = super::generate::generate_cors_policy_consistency_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T857"), "{reason}"),
            other => panic!("expected Refuted (cross-method conflict), got {other:?}"),
        }
    }

    // ── §Fase 91.c — TemporalContextSoundness ────────────────────────────

    const TEMPORAL_PROGRAM: &str = "context Scheduling { now: \"UTC\" }\n\
         flow Plan() -> Unit {\n\
             step Triage { now: \"America/Bogota\" ask: \"slots\" }\n\
             step Confirm { ask: \"confirm\" }\n\
         }\n";

    #[test]
    fn temporal_context_soundness_round_trips_and_verifies() {
        let ir = ir_from_source(TEMPORAL_PROGRAM);
        let proofs =
            super::generate::generate_temporal_context_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program proof when a temporal contract exists");
        assert_eq!(proofs[0].property, PropertyClass::TemporalContextSoundness);
        if let Witness::TemporalContextSoundness(ref w) = proofs[0].witness {
            assert_eq!(
                w.declarations,
                vec![
                    ("context".to_string(), "Scheduling".to_string(), "UTC".to_string()),
                    ("step".to_string(), "Triage".to_string(), "America/Bogota".to_string()),
                ]
            );
            assert!(w.format_violations.is_empty());
            assert!(w.unknown_zones.is_empty());
        } else {
            panic!("expected a TemporalContextSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn temporal_less_program_carries_no_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_temporal_context_soundness_proofs(&ir, "test").is_empty(),
            "no temporal contract → no proof"
        );
    }

    #[test]
    fn temporal_context_soundness_refutes_an_unknown_zone() {
        // `Fake/Zone` passes the frontend's SHAPE law (axon-T892 sees a
        // plausible IANA form) — only the tz-database membership check the
        // proof carries can catch it before the runtime fails closed.
        let ir = ir_from_source(
            "flow Plan() -> Unit { step S { now: \"Fake/Zone\" ask: \"hi\" } }\n",
        );
        let proofs =
            super::generate::generate_temporal_context_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("tz database"), "{reason}");
                assert!(reason.contains("Fake/Zone"), "{reason}");
            }
            other => panic!("expected Refuted (unknown zone), got {other:?}"),
        }
    }

    #[test]
    fn temporal_context_soundness_refutes_a_shape_violation() {
        // Hand-craft an IR with a malformed zone (bypassing the compile-time
        // axon-T892 — a hand-edited or version-drifted deployment).
        let mut ir = ir_from_source(TEMPORAL_PROGRAM);
        ir.contexts[0].now_tz = Some("Bogota".to_string());
        let proofs =
            super::generate::generate_temporal_context_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T892"), "{reason}"),
            other => panic!("expected Refuted (shape violation), got {other:?}"),
        }
    }

    #[test]
    fn temporal_context_soundness_refutes_a_forged_witness() {
        // Forge: the witness claims a zone the artifact doesn't declare.
        let ir = ir_from_source(TEMPORAL_PROGRAM);
        let mut proofs =
            super::generate::generate_temporal_context_soundness_proofs(&ir, "test");
        if let Witness::TemporalContextSoundness(ref mut w) = proofs[0].witness {
            w.declarations
                .push(("step".to_string(), "Ghost".to_string(), "UTC".to_string()));
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("disagrees"), "{reason}"),
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    #[test]
    fn temporal_context_soundness_rides_generate_all_proofs() {
        let ir = ir_from_source(TEMPORAL_PROGRAM);
        let proofs = super::generate::generate_all_proofs(&ir, "test");
        assert!(
            proofs
                .iter()
                .any(|p| p.property == PropertyClass::TemporalContextSoundness),
            "self-contained → registered in the default generation set"
        );
    }

    // ── §Fase 92.d — CredentialAttenuation ───────────────────────────────

    const CREDENTIAL_PROGRAM: &str = "credential WidgetSession {\n\
             ttl: 15m\n\
             grants: [chat.invoke]\n\
         }\n\
         flow Bootstrap() -> Unit {\n\
             mint WidgetSession as tok\n\
             step S { ask: \"hi\" }\n\
         }\n";

    #[test]
    fn credential_attenuation_round_trips_and_verifies() {
        let ir = ir_from_source(CREDENTIAL_PROGRAM);
        let proofs = super::generate::generate_credential_attenuation_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program proof when a contract exists");
        assert_eq!(proofs[0].property, PropertyClass::CredentialAttenuation);
        if let Witness::CredentialAttenuation(ref w) = proofs[0].witness {
            assert_eq!(
                w.contracts,
                vec![("WidgetSession".to_string(), 900, vec!["chat.invoke".to_string()])]
            );
            assert_eq!(
                w.mints,
                vec![(
                    "Bootstrap".to_string(),
                    "WidgetSession".to_string(),
                    "tok".to_string()
                )]
            );
            assert!(w.unresolved_mints.is_empty());
            assert!(w.invalid_contracts.is_empty());
        } else {
            panic!("expected a CredentialAttenuation witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn credential_less_program_carries_no_attenuation_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_credential_attenuation_proofs(&ir, "test").is_empty(),
            "no contract → no proof"
        );
    }

    #[test]
    fn credential_attenuation_refutes_an_unresolved_mint() {
        // Hand-craft an IR (bypassing compile-time axon-T895) where the mint
        // names a contract that doesn't exist.
        let mut ir = ir_from_source(CREDENTIAL_PROGRAM);
        ir.credentials.clear();
        let proofs = super::generate::generate_credential_attenuation_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T895"), "{reason}"),
            other => panic!("expected Refuted (unresolved mint), got {other:?}"),
        }
    }

    #[test]
    fn credential_attenuation_refutes_an_ill_formed_contract() {
        // Hand-craft an IR with a TTL above the ephemeral ceiling
        // (bypassing compile-time axon-T894).
        let mut ir = ir_from_source(CREDENTIAL_PROGRAM);
        ir.credentials[0].ttl_secs = 7 * 86_400;
        let proofs = super::generate::generate_credential_attenuation_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T894"), "{reason}"),
            other => panic!("expected Refuted (ill-formed contract), got {other:?}"),
        }
    }

    #[test]
    fn credential_attenuation_refutes_a_forged_witness() {
        let ir = ir_from_source(CREDENTIAL_PROGRAM);
        let mut proofs = super::generate::generate_credential_attenuation_proofs(&ir, "test");
        if let Witness::CredentialAttenuation(ref mut w) = proofs[0].witness {
            // Forge: claim a broader grant set than the artifact declares.
            w.contracts[0].2.push("tenant.update".to_string());
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("disagrees"), "{reason}"),
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    #[test]
    fn credential_attenuation_rides_generate_all_proofs() {
        let ir = ir_from_source(CREDENTIAL_PROGRAM);
        let proofs = super::generate::generate_all_proofs(&ir, "test");
        assert!(
            proofs
                .iter()
                .any(|p| p.property == PropertyClass::CredentialAttenuation),
            "self-contained → registered in the default generation set"
        );
    }

    // ── §Fase 94.e — SecretCustodySoundness ──────────────────────────────

    const CUSTODY_PROGRAM: &str = "axonstore CrmTokens {\n\
             backend: secrets\n\
             class: crm\n\
         }\n\
         tool RefreshCrmToken {\n\
             endpoint: \"/tools/crm/refresh\"\n\
         }\n\
         flow RotateExpiring() -> Unit {\n\
             rotate CrmTokens where \"expires_at < now() + interval '10 minutes'\" with RefreshCrmToken as result\n\
             step S { ask: \"${result}\" }\n\
         }\n";

    #[test]
    fn secret_custody_round_trips_and_verifies() {
        let ir = ir_from_source(CUSTODY_PROGRAM);
        let proofs = super::generate::generate_secret_custody_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program proof when custody exists");
        assert_eq!(proofs[0].property, PropertyClass::SecretCustodySoundness);
        if let Witness::SecretCustodySoundness(ref w) = proofs[0].witness {
            assert_eq!(
                w.stores,
                vec![("CrmTokens".to_string(), "crm".to_string())]
            );
            assert_eq!(
                w.rotates,
                vec![(
                    "RotateExpiring".to_string(),
                    "CrmTokens".to_string(),
                    "RefreshCrmToken".to_string(),
                    "result".to_string()
                )]
            );
            assert!(w.unresolved_stores.is_empty());
            assert!(w.unresolved_tools.is_empty());
            assert!(w.invalid_classes.is_empty());
            assert!(w.write_violations.is_empty());
        } else {
            panic!("expected a SecretCustodySoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn custody_less_program_carries_no_custody_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_secret_custody_proofs(&ir, "test").is_empty(),
            "no custody → no proof"
        );
    }

    #[test]
    fn secret_custody_refutes_a_ghost_rotation_tool() {
        // Hand-craft an IR (bypassing compile-time axon-T899) where the
        // rotate names a tool that doesn't exist.
        let mut ir = ir_from_source(CUSTODY_PROGRAM);
        ir.tools.clear();
        let proofs = super::generate::generate_secret_custody_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T899"), "{reason}"),
            other => panic!("expected Refuted (ghost tool), got {other:?}"),
        }
    }

    #[test]
    fn secret_custody_refutes_a_class_less_store() {
        // Hand-craft an IR with an empty class (bypassing axon-T900).
        let mut ir = ir_from_source(CUSTODY_PROGRAM);
        for s in &mut ir.axonstore_specs {
            if s.backend == "secrets" {
                s.class = String::new();
            }
        }
        let proofs = super::generate::generate_secret_custody_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T900"), "{reason}"),
            other => panic!("expected Refuted (class-less store), got {other:?}"),
        }
    }

    #[test]
    fn secret_custody_refutes_a_forged_witness() {
        let ir = ir_from_source(CUSTODY_PROGRAM);
        let mut proofs = super::generate::generate_secret_custody_proofs(&ir, "test");
        if let Witness::SecretCustodySoundness(ref mut w) = proofs[0].witness {
            // Forge: claim a broader class than the artifact declares.
            w.stores[0].1 = "llm".to_string();
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("disagrees"), "{reason}"),
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    #[test]
    fn secret_custody_rides_generate_all_proofs() {
        let ir = ir_from_source(CUSTODY_PROGRAM);
        let proofs = super::generate::generate_all_proofs(&ir, "test");
        assert!(
            proofs
                .iter()
                .any(|p| p.property == PropertyClass::SecretCustodySoundness),
            "self-contained → registered in the default generation set"
        );
    }

    // ── §Fase 84.c — TechnicianCommandSafety ─────────────────────────────

    const TECH_PROGRAM: &str = concat!(
        "type Command { line: String }\n",
        "type CommandResult { stdout: String, stderr: String, exit_code: Int }\n",
        "type DenyReason { detail: String }\n",
        "session TechSafe {\n",
        "  server: [ send Command, receive CommandResult, end ]\n",
        "  client: [ receive Command, send CommandResult, end ]\n",
        "}\n",
        "session TechConfirm {\n",
        "  server: [ send Command, select { approved: [ receive CommandResult, end ], denied: [ receive DenyReason, end ] } ]\n",
        "  client: [ receive Command, branch { approved: [ send CommandResult, end ], denied: [ send DenyReason, end ] } ]\n",
        "}\n",
        "socket TechSafeWS { protocol: TechSafe }\n",
        "socket TechConfirmWS { protocol: TechConfirm }\n",
        "tool Ping { provider: bash target: TechSafeWS risk: safe parameters: { count: Int, host: String } argv: [\"ping\", \"-c\", \"${count}\", \"${host}\"] }\n",
        "tool DeleteFile { provider: bash target: TechConfirmWS risk: destructive parameters: { path: String } argv: [\"rm\", \"${path}\"] }\n",
    );

    #[test]
    fn technician_command_safety_round_trips_and_verifies() {
        let ir = ir_from_source(TECH_PROGRAM);
        let proofs = super::generate::generate_technician_command_safety_proofs(&ir, "test");
        assert_eq!(proofs.len(), 2, "one proof per target-bound tool");
        for p in &proofs {
            assert_eq!(p.property, PropertyClass::TechnicianCommandSafety);
            assert_eq!(check_proof(p, &ir), CheckOutcome::Verified);
        }
        // The destructive tool's witness records its reachable confirm branch.
        let del = proofs
            .iter()
            .find(|p| p.witness.subject_name() == "DeleteFile")
            .expect("DeleteFile proof");
        if let Witness::TechnicianCommandSafety(ref w) = del.witness {
            assert_eq!(w.risk, "destructive");
            assert!(w.confirm_branch_reachable);
            assert!(w.argv_present);
            assert!(w.unbound_placeholders.is_empty());
            assert!(w.partial_tokens.is_empty());
        } else {
            panic!("expected a TechnicianCommandSafety witness");
        }
    }

    #[test]
    fn non_technician_program_carries_no_technician_proof() {
        let ir = ir_from_source("tool WebSearch { provider: http max_results: 5 }\n");
        assert!(
            super::generate::generate_technician_command_safety_proofs(&ir, "test").is_empty(),
            "no target-bound tool → no proof"
        );
    }

    #[test]
    fn technician_command_safety_refutes_a_forged_witness() {
        let ir = ir_from_source(TECH_PROGRAM);
        let mut proofs = super::generate::generate_technician_command_safety_proofs(&ir, "test");
        if let Witness::TechnicianCommandSafety(ref mut w) = proofs[0].witness {
            w.argv.push("--forged".to_string());
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("disagrees"), "{reason}"),
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    #[test]
    fn technician_command_safety_refutes_an_unbound_placeholder() {
        // Hand-craft an IR (bypassing axon-T859) where the argv references a
        // placeholder that is not a declared parameter.
        let mut ir = ir_from_source(TECH_PROGRAM);
        let ping = ir.tools.iter_mut().find(|t| t.name == "Ping").unwrap();
        ping.argv = vec!["ping".to_string(), "${ghost}".to_string()];
        let proofs = super::generate::generate_technician_command_safety_proofs(&ir, "test");
        let ping_proof = proofs
            .iter()
            .find(|p| p.witness.subject_name() == "Ping")
            .unwrap();
        match check_proof(ping_proof, &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T859"), "{reason}"),
            other => panic!("expected Refuted (unbound placeholder), got {other:?}"),
        }
    }

    #[test]
    fn technician_command_safety_refutes_destructive_without_branch() {
        // Hand-craft an IR (bypassing axon-T860) where a destructive tool is
        // re-pointed to the confirmation-less socket.
        let mut ir = ir_from_source(TECH_PROGRAM);
        let del = ir.tools.iter_mut().find(|t| t.name == "DeleteFile").unwrap();
        del.target = Some("TechSafeWS".to_string());
        let proofs = super::generate::generate_technician_command_safety_proofs(&ir, "test");
        let del_proof = proofs
            .iter()
            .find(|p| p.witness.subject_name() == "DeleteFile")
            .unwrap();
        match check_proof(del_proof, &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T860"), "{reason}"),
            other => panic!("expected Refuted (destructive w/o branch), got {other:?}"),
        }
    }

    // ── §Fase 85.c — CacheSoundness ──────────────────────────────────────

    const CACHE_PROGRAM: &str = concat!(
        "flow Chat() -> Unit { step S { ask: \"hi\" } }\n",
        "type WeatherEvent { city: String }\n",
        "channel WeatherUpdated { message: WeatherEvent }\n",
        "tool Fingerprint { provider: http effects: <pure> parameters: { input: String } }\n",
        "tool Weather { provider: http effects: <network> parameters: { city: String } cache: WeatherCache }\n",
        "cache DefaultPure { default: true }\n",
        "cache WeatherCache { backend: redis ttl: 5m apply_to_effects: [pure, network] invalidate_on: [WeatherUpdated] }\n",
    );

    #[test]
    fn cache_soundness_round_trips_and_verifies() {
        let ir = ir_from_source(CACHE_PROGRAM);
        let proofs = super::generate::generate_cache_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program cache proof");
        assert_eq!(proofs[0].property, PropertyClass::CacheSoundness);
        if let Witness::CacheSoundness(ref w) = proofs[0].witness {
            assert_eq!(w.default_count, 1);
            assert!(w.widened_without_ttl.is_empty());
            assert!(w.unresolved_refs.is_empty());
        } else {
            panic!("expected a CacheSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn cache_less_program_carries_no_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_cache_soundness_proofs(&ir, "test").is_empty(),
            "no contract → no proof"
        );
    }

    #[test]
    fn cache_soundness_refutes_two_defaults() {
        let mut ir = ir_from_source(CACHE_PROGRAM);
        if let Some(wc) = ir.caches.iter_mut().find(|c| c.name == "WeatherCache") {
            wc.default_policy = true;
        }
        let proofs = super::generate::generate_cache_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T863"), "{reason}"),
            other => panic!("expected Refuted (two defaults), got {other:?}"),
        }
    }

    #[test]
    fn cache_soundness_refutes_widened_without_ttl() {
        let mut ir = ir_from_source(CACHE_PROGRAM);
        if let Some(wc) = ir.caches.iter_mut().find(|c| c.name == "WeatherCache") {
            wc.ttl = None;
        }
        let proofs = super::generate::generate_cache_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T865"), "{reason}"),
            other => panic!("expected Refuted (widened w/o ttl), got {other:?}"),
        }
    }

    #[test]
    fn cache_soundness_refutes_unresolved_reference() {
        let mut ir = ir_from_source(CACHE_PROGRAM);
        if let Some(t) = ir.tools.iter_mut().find(|t| t.name == "Weather") {
            t.cache = "Ghost".to_string();
        }
        let proofs = super::generate::generate_cache_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T864"), "{reason}"),
            other => panic!("expected Refuted (unresolved ref), got {other:?}"),
        }
    }

    // ── §Fase 98.d — ScrapeProvenanceSoundness ───────────────────────────

    const SCRAPE_PROGRAM: &str = concat!(
        "type RawPage { status: Int, body: String }\n",
        "type Summary { text: String }\n",
        "persona Analyst { domain: [\"news\"] }\n",
        "shield NewsShield { scan: [prompt_injection] on_breach: quarantine severity: high }\n",
        "tool FetchNews { provider: scrape_http parameters: { url: String } ",
        "output_type: RawPage effects: <network, web> scrape: { engine: impersonate } }\n",
        "flow Digest() -> Summary {\n",
        "  use FetchNews(url = \"https://ex.com/news\")\n",
        "  shield NewsShield on page -> RawPage\n",
        "  step Summarize { given: Digest ask: \"Summarize\" output: Summary }\n",
        "}\n",
    );

    #[test]
    fn scrape_provenance_round_trips_and_verifies() {
        let ir = ir_from_source(SCRAPE_PROGRAM);
        let proofs = super::generate::generate_scrape_provenance_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program scrape proof");
        assert_eq!(proofs[0].property, PropertyClass::ScrapeProvenanceSoundness);
        if let Witness::ScrapeProvenanceSoundness(ref w) = proofs[0].witness {
            assert_eq!(w.scrape_tools.len(), 1);
            assert!(w.tools_missing_web.is_empty());
            assert!(w.dom_tools_with_network.is_empty());
            assert!(w.unshielded_flows.is_empty(), "shielded flow → no barrier violation");
        } else {
            panic!("expected a ScrapeProvenanceSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn scrape_less_program_carries_no_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_scrape_provenance_soundness_proofs(&ir, "test").is_empty(),
            "no scrape tool → no proof"
        );
    }

    #[test]
    fn scrape_provenance_refutes_missing_web() {
        // Tamper the stored IR: strip `web` from the scrape tool's effect row.
        let mut ir = ir_from_source(SCRAPE_PROGRAM);
        if let Some(t) = ir.tools.iter_mut().find(|t| t.name == "FetchNews") {
            t.effect_row.retain(|e| e != "web");
        }
        let proofs = super::generate::generate_scrape_provenance_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T904"), "{reason}"),
            other => panic!("expected Refuted (missing web), got {other:?}"),
        }
    }

    #[test]
    fn scrape_provenance_refutes_unshielded_flow() {
        // Tamper: drop the shield step, leaving web content → belief unscanned.
        let mut ir = ir_from_source(SCRAPE_PROGRAM);
        for flow in ir.flows.iter_mut() {
            flow.steps
                .retain(|s| !matches!(s, crate::ir_nodes::IRFlowNode::ShieldApply(_)));
        }
        let proofs = super::generate::generate_scrape_provenance_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T908"), "{reason}"),
            other => panic!("expected Refuted (content-injection barrier), got {other:?}"),
        }
    }

    // ── §Fase 99.d — DocumentProvenanceSoundness ─────────────────────────

    const DOC_PROGRAM: &str = concat!(
        "document quarterly_report {\n",
        "  target: docx\n",
        "  provenance: embedded\n",
        "  effects: <io, storage>\n",
        "  section {\n",
        "    heading: \"Q3\"\n",
        "    para { text: \"Audited.\" }\n",
        "    para { text: revenue_summary  attribute: analyst_agent }\n",
        "  }\n",
        "}\n",
    );

    #[test]
    fn document_provenance_round_trips_and_verifies() {
        let ir = ir_from_source(DOC_PROGRAM);
        let proofs = super::generate::generate_document_provenance_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program document proof");
        assert_eq!(proofs[0].property, PropertyClass::DocumentProvenanceSoundness);
        if let Witness::DocumentProvenanceSoundness(ref w) = proofs[0].witness {
            assert_eq!(w.documents.len(), 1);
            assert!(w.bad_targets.is_empty());
            assert!(w.sensitive_without_legal.is_empty());
            assert!(w.unattributed_slots.is_empty(), "attributed ⇒ no barrier violation");
        } else {
            panic!("expected a DocumentProvenanceSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn document_less_program_carries_no_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_document_provenance_soundness_proofs(&ir, "test").is_empty(),
            "no document → no proof"
        );
    }

    #[test]
    fn document_provenance_refutes_unattributed_slot() {
        // Tamper the stored IR: strip the `attribute` field, laundering an
        // unattributed flow value into an assertive slot.
        let mut ir = ir_from_source(DOC_PROGRAM);
        for d in ir.documents.iter_mut() {
            for section in d.blocks.iter_mut() {
                for block in section.children.iter_mut() {
                    block.fields.retain(|f| f.name != "attribute");
                }
            }
        }
        let proofs = super::generate::generate_document_provenance_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T916"), "{reason}"),
            other => panic!("expected Refuted (assertion-laundering barrier), got {other:?}"),
        }
    }

    // ── §Fase 105 — DeliveryProvenanceSoundness ──────────────────────────

    const DELIVER_PROGRAM: &str = concat!(
        "deliver push_lead {\n",
        "  target: crm\n",
        "  provenance: attached\n",
        "  secret: crm_api_key\n",
        "  effects: <web>\n",
        "  upsert_contact { key: resolved_email  email: resolved_email }\n",
        "}\n",
    );

    #[test]
    fn delivery_provenance_round_trips_and_verifies() {
        let ir = ir_from_source(DELIVER_PROGRAM);
        let proofs = super::generate::generate_delivery_provenance_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program delivery proof");
        assert_eq!(proofs[0].property, PropertyClass::DeliveryProvenanceSoundness);
        if let Witness::DeliveryProvenanceSoundness(ref w) = proofs[0].witness {
            assert_eq!(w.deliveries.len(), 1);
            assert!(w.bad_targets.is_empty());
            assert!(w.sensitive_without_legal.is_empty());
            assert!(w.laundered_deliveries.is_empty(), "attached ⇒ no barrier violation");
        } else {
            panic!("expected a DeliveryProvenanceSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn delivery_less_program_carries_no_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_delivery_provenance_soundness_proofs(&ir, "test").is_empty(),
            "no deliver → no proof"
        );
    }

    #[test]
    fn delivery_provenance_refutes_laundered_cleared_delivery() {
        // Tamper the stored IR: flip provenance to `cleared`, laundering a flow
        // value into the CRM as a bare fact with no epistemic vouch.
        let mut ir = ir_from_source(DELIVER_PROGRAM);
        for d in ir.deliveries.iter_mut() {
            d.provenance = "cleared".to_string();
        }
        let proofs = super::generate::generate_delivery_provenance_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T920"), "{reason}"),
            other => panic!("expected Refuted (provenance-stripping barrier), got {other:?}"),
        }
    }

    // ── §Fase 110.b — NotificationProvenanceSoundness ────────────────────

    const NOTIFY_PROGRAM: &str = concat!(
        "notify LowSales {\n",
        "  channel: sms\n",
        "  to: secret(ops.oncall_phone)\n",
        "  template: \"Ventas 7d: ${resumen}\"\n",
        "  window: 4h\n",
        "  provenance: attached\n",
        "  effects: <web>\n",
        "}\n",
    );

    #[test]
    fn notification_round_trips_and_verifies() {
        let ir = ir_from_source(NOTIFY_PROGRAM);
        let proofs =
            super::generate::generate_notification_provenance_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        assert_eq!(
            proofs[0].property,
            PropertyClass::NotificationProvenanceSoundness
        );
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn notification_refutes_a_hand_edited_laundering() {
        // Forge: clear the provenance on a slot-binding notify with no vouch.
        let mut ir = ir_from_source(NOTIFY_PROGRAM);
        ir.notifications[0].provenance = "cleared".to_string();
        let proofs =
            super::generate::generate_notification_provenance_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("laundered"), "{reason}")
            }
            other => panic!("expected Refuted (laundered notification), got {other:?}"),
        }
    }

    #[test]
    fn notification_refutes_a_dropped_window() {
        let mut ir = ir_from_source(NOTIFY_PROGRAM);
        ir.notifications[0].window = String::new();
        let proofs =
            super::generate::generate_notification_provenance_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("windows"), "{reason}")
            }
            other => panic!("expected Refuted (unbounded interruption), got {other:?}"),
        }
    }

    // ── §Fase 109.b — GradientSoundness (the proof-carrying derivative) ──

    const GRAD_PROGRAM: &str = concat!(
        "flow Score(x: Float, y: Float) -> Text {\n",
        "  let total = 3.0 * x + y * y\n",
        "  grad total wrt [x, y] as g\n",
        "  return g\n",
        "}\n",
    );

    #[test]
    fn gradient_round_trips_and_verifies() {
        let ir = ir_from_source(GRAD_PROGRAM);
        let proofs = super::generate::generate_gradient_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program gradient proof");
        assert_eq!(proofs[0].property, PropertyClass::GradientSoundness);
        if let Witness::GradientSoundness(ref w) = proofs[0].witness {
            assert_eq!(w.grads.len(), 1);
            assert_eq!(w.grads[0].1, "total");
            assert_eq!(w.grads[0].2, "x,y");
            assert!(w.violations.is_empty(), "{:?}", w.violations);
        } else {
            panic!("expected a GradientSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn gradient_refutes_a_hand_edited_derivative() {
        // Forge: swap a stored derivative for a flattering constant. The
        // checker re-differentiates the original and refutes.
        let mut ir = ir_from_source(GRAD_PROGRAM);
        for flow in &mut ir.flows {
            for step in &mut flow.steps {
                if let crate::ir_nodes::IRFlowNode::Grad(g) = step {
                    g.derivatives[0] = crate::ir_nodes::IRExpr::Lit {
                        lit: crate::ir_nodes::IRExprLit::Float { value: 999.0 },
                    };
                }
            }
        }
        let proofs = super::generate::generate_gradient_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("derivative-mismatch"), "{reason}")
            }
            other => panic!("expected Refuted (forged gradient), got {other:?}"),
        }
    }

    #[test]
    fn grad_less_program_carries_no_gradient_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_gradient_soundness_proofs(&ir, "test").is_empty(),
            "no grad step → no proof"
        );
    }

    // ── §Fase 108.d — DataspaceSchemaSoundness (T928/T930, made a proof) ──

    const DATASPACE_PROGRAM: &str = concat!(
        "dataspace Sales {\n",
        "  column region: Text\n",
        "  column amount: Float\n",
        "}\n",
        "flow Report(raw: Text) -> Text {\n",
        "  ingest raw into Sales { format: csv }\n",
        "  aggregate Sales { group_by: [region], compute: [count, sum(amount)], as: r }\n",
        "  return r\n",
        "}\n",
    );

    #[test]
    fn dataspace_schema_round_trips_and_verifies() {
        let ir = ir_from_source(DATASPACE_PROGRAM);
        let proofs =
            super::generate::generate_dataspace_schema_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program dataspace-schema proof");
        assert_eq!(proofs[0].property, PropertyClass::DataspaceSchemaSoundness);
        if let Witness::DataspaceSchemaSoundness(ref w) = proofs[0].witness {
            assert_eq!(w.dataspaces.len(), 1);
            assert_eq!(w.dataspaces[0].0, "Sales");
            assert!(w.violations.is_empty(), "{:?}", w.violations);
        } else {
            panic!("expected a DataspaceSchemaSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn dataspace_schema_refutes_a_hand_edited_ghost_column() {
        // A stale/hand-edited artifact whose aggregate reads a column the
        // schema never declared MUST be refuted at deploy — the T930 law,
        // re-derived. Forge it by mutating the compiled IR.
        let mut ir = ir_from_source(DATASPACE_PROGRAM);
        for flow in &mut ir.flows {
            for step in &mut flow.steps {
                if let crate::ir_nodes::IRFlowNode::Aggregate(a) = step {
                    a.group_by = vec!["ghost_region".to_string()];
                }
            }
        }
        let proofs =
            super::generate::generate_dataspace_schema_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("ghost-column"), "{reason}")
            }
            other => panic!("expected Refuted (ghost column), got {other:?}"),
        }
    }

    #[test]
    fn dataspace_less_program_carries_no_schema_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_dataspace_schema_soundness_proofs(&ir, "test")
                .is_empty(),
            "no dataspace surface → no proof"
        );
    }

    // ── §Fase 107 — QuerySafetySoundness (RFC 10008 §2, made a proof) ────

    const QUERY_PROGRAM: &str = concat!(
        "axonstore mem { backend: in_memory }\n",
        "flow Search() -> Unit {\n",
        "  retrieve mem { where: \"kind = 'lead'\" as: hits }\n",
        "}\n",
        "axonendpoint E { method: QUERY path: \"/search\" execute: Search backend: stub }\n",
    );

    #[test]
    fn query_safety_round_trips_and_verifies() {
        let ir = ir_from_source(QUERY_PROGRAM);
        let proofs = super::generate::generate_query_safety_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program QUERY-safety proof");
        assert_eq!(proofs[0].property, PropertyClass::QuerySafetySoundness);
        if let Witness::QuerySafetySoundness(ref w) = proofs[0].witness {
            assert_eq!(w.query_endpoints.len(), 1);
            assert!(w.unsafe_queries.is_empty(), "a read-only QUERY is safe");
            assert!(w.egress_declarations.is_empty());
        } else {
            panic!("expected a QuerySafetySoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn query_less_program_carries_no_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_query_safety_soundness_proofs(&ir, "test").is_empty(),
            "no QUERY endpoint → no proof"
        );
    }

    #[test]
    fn query_safety_refutes_a_write_smuggled_into_a_safe_method() {
        // Tamper the stored IR: append a `persist` to the QUERY's flow — the exact
        // attack the class exists to stop (a write hiding behind a method that
        // caches and proxies are entitled to retry).
        let mut ir = ir_from_source(QUERY_PROGRAM);
        let persist = ir_from_source(
            "axonstore mem { backend: in_memory }\n\
             flow W() -> Unit { persist into mem { kind: \"x\" content: \"y\" } }\n",
        )
        .flows
        .iter()
        .find(|f| f.name == "W")
        .expect("W")
        .steps
        .first()
        .expect("the persist node")
        .clone();
        for f in ir.flows.iter_mut() {
            if f.name == "Search" {
                f.steps.push(persist.clone());
            }
        }
        let proofs = super::generate::generate_query_safety_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T927"), "{reason}"),
            other => panic!("expected Refuted (QUERY-safety), got {other:?}"),
        }
    }

    // ── §Fase 100.e — DocumentIngestionSoundness ─────────────────────────

    const INGEST_PROGRAM: &str = concat!(
        "type Doc { text: String }\n",
        "type Summary { text: String }\n",
        "tool DocReader { provider: native parameters: { path: String } output_type: Doc ",
        "effects: <io, ingest:parsed> }\n",
        "persona Analyst { domain: [\"docs\"] }\n",
        "shield DocShield { scan: [prompt_injection] on_breach: quarantine severity: high }\n",
        "flow Read() -> Summary {\n",
        "  use DocReader(path = \"in.docx\")\n",
        "  shield DocShield on doc -> Doc\n",
        "  step Summarize { given: Read ask: \"Summarize\" output: Summary }\n",
        "}\n",
    );

    #[test]
    fn document_ingestion_round_trips_and_verifies() {
        let ir = ir_from_source(INGEST_PROGRAM);
        let proofs = super::generate::generate_document_ingestion_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program ingestion proof");
        assert_eq!(proofs[0].property, PropertyClass::DocumentIngestionSoundness);
        if let Witness::DocumentIngestionSoundness(ref w) = proofs[0].witness {
            assert_eq!(w.ingest_tools.len(), 1);
            assert_eq!(w.ingest_tools[0].1, "parsed");
            assert!(w.inferred_ceiling_violations.is_empty());
            assert!(w.unshielded_flows.is_empty(), "shielded flow → no barrier violation");
        } else {
            panic!("expected a DocumentIngestionSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn ingestion_less_program_carries_no_proof() {
        let ir = ir_from_source("flow Chat() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_document_ingestion_soundness_proofs(&ir, "test").is_empty(),
            "no ingesting tool → no proof"
        );
    }

    #[test]
    fn no_inferred_producer_ships_in_this_fase() {
        // D100.14 — the vacuum: the built-in stdlib catalog has NO tool that
        // declares `ingest:inferred`, so the Inferred ceiling (T1001) is
        // vacuously satisfied until §101 adds a producer.
        let has_inferred = crate::stdlib::TOOLS.iter().any(|t| {
            // native tools carry no declared ingest class in the catalog; the
            // OSS `DocumentReader` produces Parsed only (asserted in ooxml_read).
            t.name == "DocumentReaderInferred" // no such tool exists
        });
        assert!(!has_inferred, "no built-in Inferred producer may ship in §100 (D100.14)");
    }

    #[test]
    fn ingestion_refutes_unshielded_flow() {
        // Tamper: drop the shield, leaving ingested content → belief unscanned.
        let mut ir = ir_from_source(INGEST_PROGRAM);
        for flow in ir.flows.iter_mut() {
            flow.steps
                .retain(|s| !matches!(s, crate::ir_nodes::IRFlowNode::ShieldApply(_)));
        }
        let proofs = super::generate::generate_document_ingestion_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T908"), "{reason}"),
            other => panic!("expected Refuted (ingestion barrier), got {other:?}"),
        }
    }

    // ── §Fase 101.b — InferredCeilingSoundness ───────────────────────────

    // An `ingest:inferred` PRODUCER (an OCR reader) — the state §100 forbade
    // (D100.14) and §101 creates. It declares NO `epistemic:know` (ceiling holds)
    // and its output is shielded before reasoning.
    const INFER_PROGRAM: &str = concat!(
        "type Doc { text: String }\n",
        "type Summary { text: String }\n",
        "tool OcrReader { provider: native parameters: { img: String } output_type: Doc ",
        "effects: <io, ingest:inferred> }\n",
        "persona Analyst { domain: [\"docs\"] }\n",
        "shield DocShield { scan: [prompt_injection] on_breach: quarantine severity: high }\n",
        "flow Read() -> Summary {\n",
        "  use OcrReader(img = \"scan.png\")\n",
        "  shield DocShield on doc -> Doc\n",
        "  step Summarize { given: Read ask: \"Summarize\" output: Summary }\n",
        "}\n",
    );

    #[test]
    fn inferred_ceiling_round_trips_and_verifies() {
        let ir = ir_from_source(INFER_PROGRAM);
        let proofs = super::generate::generate_inferred_ceiling_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one whole-program inferred-ceiling proof");
        assert_eq!(proofs[0].property, PropertyClass::InferredCeilingSoundness);
        if let Witness::InferredCeilingSoundness(ref w) = proofs[0].witness {
            assert_eq!(w.inferred_producers, vec!["OcrReader".to_string()]);
            assert!(w.ceiling_violations.is_empty(), "no epistemic:know → ceiling holds");
            assert!(w.unshielded_flows.is_empty(), "shielded flow → no barrier violation");
        } else {
            panic!("expected an InferredCeilingSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn parsed_only_program_carries_no_inferred_ceiling_proof() {
        // The §100 vacuum: a program with only `ingest:parsed` tools owes NO
        // inferred-ceiling proof (no producer exists).
        let ir = ir_from_source(INGEST_PROGRAM);
        assert!(
            super::generate::generate_inferred_ceiling_soundness_proofs(&ir, "test").is_empty(),
            "no `ingest:inferred` producer → no proof (the §100 vacuum holds)"
        );
    }

    #[test]
    fn inferred_ceiling_refutes_epistemic_know() {
        // Tamper: inject `epistemic:know` into the inferred producer's effect row.
        // A re-derivation catches T1001 — an inferred read can never be `know`.
        let mut ir = ir_from_source(INFER_PROGRAM);
        for t in ir.tools.iter_mut() {
            if t.name == "OcrReader" {
                t.effect_row.push("epistemic:know".to_string());
            }
        }
        let proofs = super::generate::generate_inferred_ceiling_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T1001"), "{reason}"),
            other => panic!("expected Refuted (ceiling), got {other:?}"),
        }
    }

    #[test]
    fn inferred_ceiling_refutes_unshielded_flow() {
        // Tamper: drop the shield — an inferred read reaches belief unscanned.
        let mut ir = ir_from_source(INFER_PROGRAM);
        for flow in ir.flows.iter_mut() {
            flow.steps
                .retain(|s| !matches!(s, crate::ir_nodes::IRFlowNode::ShieldApply(_)));
        }
        let proofs = super::generate::generate_inferred_ceiling_soundness_proofs(&ir, "test");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T908"), "{reason}"),
            other => panic!("expected Refuted (barrier), got {other:?}"),
        }
    }

    // ── §Fase 86.c — ForgeSoundness ──────────────────────────────────────

    const FORGE_PROGRAM: &str = concat!(
        "anchor GoldenRatio { require: aesthetic_harmony confidence_floor: 0.70 }\n",
        "flow CreateVisualConcept(brief: String) -> Visual {\n",
        "  forge Artwork(seed: \"aurora borealis over ancient ruins\") -> Visual {\n",
        "    mode: transformational\n",
        "    novelty: 0.85\n",
        "    constraints: GoldenRatio\n",
        "    depth: 4\n",
        "    branches: 7\n",
        "  }\n",
        "}\n",
    );

    #[test]
    fn forge_soundness_round_trips_and_verifies() {
        let ir = ir_from_source(FORGE_PROGRAM);
        let proofs = super::generate::generate_forge_soundness_proofs(&ir, "test");
        assert_eq!(proofs.len(), 1, "one proof per forge block");
        assert_eq!(proofs[0].property, PropertyClass::ForgeSoundness);
        if let Witness::ForgeSoundness(ref w) = proofs[0].witness {
            assert_eq!(w.forge_name, "Artwork");
            assert_eq!(w.novelty_milli, 850);
            assert!(w.mode_ok && w.novelty_in_range && w.bounds_ok);
            assert!(w.seed_and_type_present && w.constraints_ok);
        } else {
            panic!("expected a ForgeSoundness witness");
        }
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn forge_less_program_carries_no_proof() {
        let ir = ir_from_source("flow F() -> Unit { step S { ask: \"hi\" } }\n");
        assert!(
            super::generate::generate_forge_soundness_proofs(&ir, "test").is_empty(),
            "no forge → no proof"
        );
    }

    #[test]
    fn forge_soundness_refutes_a_forged_witness() {
        let ir = ir_from_source(FORGE_PROGRAM);
        let mut proofs = super::generate::generate_forge_soundness_proofs(&ir, "test");
        if let Witness::ForgeSoundness(ref mut w) = proofs[0].witness {
            w.novelty_milli = 1500; // claim a novelty the artifact doesn't have
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("disagrees"), "{reason}"),
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    // ── §Fase 80 — UpstreamProjectionSoundness ───────────────────────────

    const STT_UPSTREAM: &str = "session SttDialogue {\n\
             axon:   [ send AudioChunk, receive Transcript, loop ]\n\
             vendor: [ receive AudioChunk, send Transcript, loop ]\n\
         }\n\
         upstream DeepgramSTT {\n\
             transport: websocket\n\
             protocol: SttDialogue\n\
             role: axon\n\
             resolve: upstream.deepgram.url\n\
             secret: upstream.deepgram.api_key\n\
             auth: header(\"Authorization\", \"Token \")\n\
             map: [\n\
                 send AudioChunk as binary,\n\
                 receive Transcript as json when \"type\" = \"Results\",\n\
             ]\n\
             reconnect: { backoff_ms: 500, max_attempts: 5, on_exhausted: fail }\n\
             overflow: drop_oldest\n\
         }";

    #[test]
    fn upstream_projection_soundness_verifies_a_total_projection() {
        let ir = ir_from_source(STT_UPSTREAM);
        let proofs = super::generate::generate_upstream_projection_soundness_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1, "one proof per upstream");
        assert_eq!(proofs[0].property.slug(), "upstream_projection_soundness");
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
        // …and it rides the all-proofs bundle (`axon pcc prove`).
        assert!(generate_all_proofs(&ir, VERSION)
            .iter()
            .any(|p| p.property == PropertyClass::UpstreamProjectionSoundness));
    }

    #[test]
    fn upstream_projection_refutes_a_partial_projection() {
        // No receive rule for Transcript — a message would cross the
        // boundary untranscoded. (No type-check ran, so the defect
        // survives to the independent checker — which is the point.)
        let ir = ir_from_source(&STT_UPSTREAM.replace(
            "receive Transcript as json when \"type\" = \"Results\",\n",
            "",
        ));
        let proofs = super::generate::generate_upstream_projection_soundness_proofs(&ir, VERSION);
        assert_eq!(proofs.len(), 1, "the defective upstream still gets its refutable proof");
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("untranscoded") && reason.contains("T849"), "{reason}")
            }
            other => panic!("expected Refuted (partial projection), got {other:?}"),
        }
    }

    #[test]
    fn upstream_projection_refutes_a_literal_shaped_secret() {
        // An uppercase dotted path parses, but production custody would
        // reject it — the T850 law re-derived by the checker.
        let ir = ir_from_source(&STT_UPSTREAM.replace(
            "secret: upstream.deepgram.api_key",
            "secret: Upstream.Deepgram.ApiKey",
        ));
        let proofs = super::generate::generate_upstream_projection_soundness_proofs(&ir, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("T850"), "{reason}"),
            other => panic!("expected Refuted (literal-shaped key), got {other:?}"),
        }
    }

    #[test]
    fn upstream_projection_refutes_a_forged_witness() {
        let ir = ir_from_source(&STT_UPSTREAM.replace(
            "receive Transcript as json when \"type\" = \"Results\",\n",
            "",
        ));
        let mut proofs = super::generate::generate_upstream_projection_soundness_proofs(&ir, VERSION);
        // Forge: claim totality for the partial projection.
        if let Witness::UpstreamProjectionSoundness(ref mut w) = proofs[0].witness {
            w.projection_total = true;
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("disagrees"), "{reason}"),
            other => panic!("expected Refuted (forgery), got {other:?}"),
        }
    }

    // ── §Fase 90.b — CapabilityGrantability (every_requirement_is_grantable) ──

    /// A representative RBAC catalog (colon) + the reserved dotted caps —
    /// the grantable authority manifest the enterprise supplies.
    fn catalog() -> Vec<String> {
        [
            "flow:execute", "flow:deploy", "tenant:update", "secret:read", "secret:write",
            "warden:execute", "tech:dispatch", "tech:approve", "daemon:run",
            "store.platform_read", "store.platform_write",
        ]
        .iter()
        .map(|s| s.to_string())
        .collect()
    }

    #[test]
    fn grantability_verifies_when_requires_projects_from_the_catalog() {
        let mut ir = empty_ir();
        // requires the DOTTED `flow.execute` — grantable because the catalog
        // has the colon `flow:execute`, which π projects to it.
        ir.endpoints
            .push(endpoint_requires("Deploy", "DeployFlow", &["flow.execute"]));
        let proofs = generate_capability_grantability_proofs(&ir, &catalog(), VERSION);
        assert_eq!(proofs.len(), 1, "one whole-program grantability proof");
        assert_eq!(proofs[0].property.slug(), "capability_grantability");
        assert_eq!(check_proof(&proofs[0], &ir), CheckOutcome::Verified);
    }

    #[test]
    fn grantability_refutes_the_briefs_dead_requirement() {
        // Kivi brief #55: `requires: [tenant.write]` — the catalog has
        // `tenant:update`, NOT `tenant:write`. A dead boundary → axon-T891.
        let mut ir = empty_ir();
        ir.endpoints
            .push(endpoint_requires("Write", "WriteFlow", &["tenant.write"]));
        let proofs = generate_capability_grantability_proofs(&ir, &catalog(), VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("axon-T891"), "{reason}");
                assert!(reason.contains("tenant.write"), "{reason}");
            }
            other => panic!("expected Refuted (dead requirement), got {other:?}"),
        }
    }

    #[test]
    fn grantability_refutes_a_fractured_catalog() {
        // If the catalog gained a `store:platform_read` colon perm it would
        // collide with the reserved dotted `store.platform_read` under π.
        let mut ir = empty_ir();
        ir.endpoints
            .push(endpoint_requires("Plat", "PlatFlow", &["store.platform_read"]));
        let mut authorities = catalog();
        authorities.push("store:platform_read".to_string());
        let proofs = generate_capability_grantability_proofs(&ir, &authorities, VERSION);
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => {
                assert!(reason.contains("fractured"), "{reason}");
                assert!(reason.contains("store.platform_read"), "{reason}");
            }
            other => panic!("expected Refuted (fracture), got {other:?}"),
        }
    }

    #[test]
    fn grantability_refutes_a_forged_requires_set() {
        // Forge: drop the dead requirement from the witness to fake
        // all_grantable. The checker re-derives `required` from the IR and
        // catches the disagreement.
        let mut ir = empty_ir();
        ir.endpoints
            .push(endpoint_requires("Write", "WriteFlow", &["tenant.write"]));
        let mut proofs = generate_capability_grantability_proofs(&ir, &catalog(), VERSION);
        if let Witness::CapabilityGrantability(ref mut w) = proofs[0].witness {
            w.required = Vec::new();
            w.all_grantable = true;
        }
        match check_proof(&proofs[0], &ir) {
            CheckOutcome::Refuted { reason } => assert!(reason.contains("disagrees"), "{reason}"),
            other => panic!("expected Refuted (forged requires-set), got {other:?}"),
        }
    }

    #[test]
    fn grantability_emits_no_proof_without_requirements() {
        // A program whose dispatching endpoints declare no `requires:` has
        // no grantability obligation.
        let mut ir = empty_ir();
        ir.endpoints
            .push(endpoint_requires("Public", "PublicFlow", &[]));
        let proofs = generate_capability_grantability_proofs(&ir, &catalog(), VERSION);
        assert!(proofs.is_empty(), "no requires → no grantability proof");
    }
}