polyplug 0.1.1

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

use core::str::FromStr;
use core::sync::atomic::AtomicUsize;
use core::sync::atomic::Ordering;
use std::collections::HashMap;
use std::collections::HashSet;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::RwLock;
use std::thread::ThreadId;

use polyplug_abi::runtime::{Compatibility, RuntimeConfig};
use polyplug_abi::types::LogLevel;
use polyplug_abi::{
    AbiError, AbiErrorCode, Array, DependencyInfo, GuestContractHandle, GuestContractInterface,
    HostApi, HostContractInstance, HostContractInterface, PluginDescriptor, StringView,
    SupportedLanguage, types::Version,
};
use polyplug_utils::{BundleId, GuestContractId};

use crate::error::HostContractError;
use crate::error::LoaderError;
use crate::error::RegistryError;
use crate::error::RuntimeError;
use crate::loader::BundleLoader;
use crate::loader::ManifestData;
use crate::loader::ManifestDependency;
use crate::logger::{LoggerHandle, RecoverPoisoned, RecoveringGuard};
pub use crate::runtime_builder::RuntimeBuilder;
use crate::runtime_store::RuntimeStore;

// ─── Runtime Configuration ───────────────────────────────────────────────────

/// Reload callback invoked after each interface swap, before dlclose.
///
/// The first argument is the opaque `on_reload_user_data` pointer from
/// `RuntimeConfig`, forwarded unchanged on every invocation.
pub(crate) struct ReloadCallback(
    pub(crate) Arc<dyn Fn(*mut core::ffi::c_void, polyplug_abi::runtime::ReloadPhase) + Send + Sync>,
);

/// Options for `Runtime::load_bundle_with`.
///
/// The `compatibility` field overrides the global `RuntimeBuilder::compatibility` setting
/// for this specific bundle load only.
pub(crate) struct LoadOptions {
    pub compatibility: Compatibility,
    pub ignore_function_count_mismatch: bool,
}

/// The runtime instance.
pub struct Runtime {
    pub(crate) registry: Arc<RuntimeStore>,
    /// All registered loaders, keyed by loader_name.
    ///
    /// Interior-mutable (`RwLock`) so loaders can be registered after `build()`
    /// through a shared `&Runtime` (e.g. the `register_loader` HostApi
    /// callback), without ever forging a `&mut Runtime` from an `Arc`-shared
    /// pointer (which would be aliasing UB). Load/reload paths take read guards;
    /// registration takes a write guard.
    pub(crate) loaders: RwLock<HashMap<String, Box<dyn BundleLoader>>>,
    /// ManifestData for all loaded bundles, keyed by bundle_name.
    /// Used by reload_bundle() for cascade detection.
    pub(crate) bundle_manifests: Mutex<HashMap<String, ManifestData>>,
    /// Optional callback fired after interface swap, before dlclose.
    pub(crate) on_reload_cb: Option<ReloadCallback>,
    pub(crate) config: RuntimeConfig,
    /// Instance-owned copy of the host logging configuration (from `config`).
    pub(crate) logger: LoggerHandle,
    /// Keeps the Rust closure installed via `RuntimeBuilder::logger` alive for
    /// the runtime's lifetime — `config.log_user_data` points into this box.
    /// Never read after construction; it exists purely as an owner.
    pub(crate) _logger_closure: Option<Box<crate::logger::LoggerClosure>>,
    /// Last error message for FFI error reporting.
    pub(crate) last_error: Mutex<String>,
    /// Registered host contracts, keyed by contract_id.
    pub(crate) host_contracts: RwLock<HashMap<u64, &'static HostContractInterface>>,
    /// Cache for singleton host contract instances.
    /// Key: HostContractId hash value.
    pub(crate) singleton_instances: RwLock<HashMap<u64, HostContractInstance>>,
    /// Host language type identifier.
    pub(crate) host_language: SupportedLanguage,
    /// Per-thread stack of bundle_ids currently inside `polyplug_init`.
    ///
    /// Replaces the former process-global `thread_local!` (Rule 12: no thread-locals
    /// for runtime state — this is now instance-owned, so multiple runtimes in one
    /// process stay isolated). A `Vec` per thread gives reentrancy safety: a nested
    /// load on the same thread pushes its own id and pops it on completion, restoring
    /// the outer bundle's id instead of clobbering it. Loaders push before calling
    /// `polyplug_init` and pop afterwards (including the panic path).
    pub(crate) init_bundle_stack: Mutex<HashMap<ThreadId, Vec<u64>>>,
    /// Fast-path hint: total number of bundle ids currently pushed across all
    /// threads' init stacks.
    ///
    /// Plugin init is a Phase-1 (rare) event; outside it every `find` / `find_all`
    /// / `get_dependencies` HostApi call would otherwise lock `init_bundle_stack`
    /// just to observe an empty stack. This counter lets `current_init_bundle_id`
    /// short-circuit to `0` with a single `Relaxed` atomic load and skip the Mutex
    /// entirely on that hot path.
    ///
    /// # Ordering rationale
    /// This is a hint only — it never carries data, just gates whether the Mutex is
    /// taken. When the counter is non-zero the Mutex provides the actual
    /// synchronization of the per-thread stacks, so `Relaxed` is sufficient here: no
    /// memory is published or consumed through this atomic. A stale `0` cannot be
    /// observed for the calling thread's OWN init window because that thread called
    /// `push_init_bundle_id` (a `fetch_add` plus a Mutex acquisition) earlier on the
    /// same thread, which happens-before any `current_init_bundle_id` it later runs
    /// during the plugin's init code — single-thread program order guarantees the
    /// incremented value is visible to that thread. Other threads' pushes only ever
    /// make the counter *larger* than this thread needs; the worst case is taking the
    /// Mutex and finding no entry for this thread (returning `0`), which is correct.
    pub(crate) active_init_count: AtomicUsize,
    /// Serializes whole-reload sequences against one another.
    ///
    /// A reload is a non-atomic read-modify-write: it snapshots the bundle's
    /// pre-reload slots, runs `loader.reload()` (which registers the new
    /// interfaces into fresh slots), then `apply_reload_swap` consumes that
    /// snapshot. The registry `RwLock` makes each individual step atomic, but it
    /// is dropped between steps, so two concurrent reloads of the SAME bundle can
    /// interleave such that one reload's snapshot goes stale — its swap then finds
    /// no freshly-registered slot for a contract the other reload already
    /// consumed, takes the dropped-contract teardown path, and removes that
    /// contract's only live slot from the find index, leaving a contract BOTH versions provide
    /// unresolvable. Holding this mutex across the entire `reload_bundle` call
    /// (including its cascade tree) makes each reload's snapshot↔swap atomic with
    /// respect to any other reload.
    ///
    /// Instance-owned (Rule 12): each `Runtime` has its own lock, so multiple
    /// runtimes in one process never serialize against each other. Readers
    /// (`find`/`resolve`/dispatch) never take this lock — they hold the registry
    /// `RwLock` and stay fully concurrent with an in-flight reload; only
    /// writer-vs-writer reloads serialize here.
    pub(crate) reload_serialize: Mutex<()>,
    /// Live stateful-instance counts per guest contract.
    ///
    /// Incremented by `host_create_guest_instance` and decremented by
    /// `host_destroy_guest_instance` for stateful instances only (a null-`data`
    /// instance is stateless — the host holds no state for it and it is not
    /// counted). Drives the accurate reload/unload UB-warning: a bundle whose
    /// contracts still have live instances when it is reloaded or unloaded is a
    /// use-after-free hazard the runtime surfaces to the host.
    ///
    /// Instance-owned (Rule 12): each `Runtime` has its own map, so multiple
    /// runtimes in one process never share instance accounting.
    pub(crate) instance_counts: Mutex<HashMap<GuestContractId, u64>>,
    /// The owned HostApi handed to plugins. A `Box` gives it a stable heap
    /// address independent of where the `Runtime` value lives, so the pointer
    /// captured by plugins survives the runtime's move into its `Arc`.
    ///
    /// Declared last so it is dropped after `registry` and `loaders` — their
    /// teardown dlcloses plugin libraries whose destructors may still call
    /// through this HostApi; freeing it last keeps those callbacks sound.
    /// (Rust drops fields in declaration order, first-declared first.)
    pub(crate) host_abi: Box<HostApi>,
}

impl Runtime {
    /// Create a RuntimeBuilder.
    pub fn builder() -> RuntimeBuilder {
        RuntimeBuilder::new()
    }

    /// Find the first provider of a contract.
    #[inline(always)]
    pub fn find_guest_contract(
        &self,
        contract_id: u64,
        min_version: u32,
    ) -> Result<GuestContractHandle, RegistryError> {
        self.registry
            .find_guest_contract(GuestContractId::from_u64(contract_id), min_version)
    }

    /// Find a specific bundle's provider of a contract.
    #[inline(always)]
    pub fn find_guest_contract_by_bundle(
        &self,
        bundle_id: u64,
        contract_id: u64,
        min_version: u32,
    ) -> Result<GuestContractHandle, RegistryError> {
        self.registry.find_guest_contract_by_bundle(
            BundleId::from_u64(bundle_id),
            GuestContractId::from_u64(contract_id),
            min_version,
        )
    }

    /// Find all providers of a contract.
    #[inline(always)]
    pub fn find_all_by_contract(
        &self,
        contract_id: u64,
        min_version: u32,
        out: &mut [GuestContractHandle],
    ) -> usize {
        self.registry.find_all_guest_contracts(
            GuestContractId::from_u64(contract_id),
            min_version,
            out,
        )
    }

    /// Find all providers of a contract, packing handles directly into a u64 buffer.
    #[inline(always)]
    pub fn find_all_by_contract_packed(
        &self,
        contract_id: u64,
        min_version: u32,
        out: &mut [u64],
    ) -> usize {
        self.registry.find_all_guest_contracts_packed(
            GuestContractId::from_u64(contract_id),
            min_version,
            out,
        )
    }

    /// Resolve a plugin handle to its interface pointer directly.
    #[inline(always)]
    pub fn resolve_guest_contract(
        &self,
        handle: GuestContractHandle,
    ) -> Result<*const GuestContractInterface, RegistryError> {
        self.registry.resolve_guest_contract(handle)
    }

    /// Register a host contract interface.
    /// Returns `Err(HostContractError::DuplicateContract)` if a contract with the same ID is already registered.
    pub fn register_host_contract(
        &self,
        contract_id: u64,
        interface: &'static HostContractInterface,
    ) -> Result<(), HostContractError> {
        let mut guard: RecoveringGuard<
            std::sync::RwLockWriteGuard<'_, HashMap<u64, &'static HostContractInterface>>,
        > = self
            .host_contracts
            .write()
            .recover_poisoned(self.logger, "runtime");
        if guard.contains_key(&contract_id) {
            return Err(HostContractError::DuplicateContract { contract_id });
        }
        guard.insert(contract_id, interface);
        Ok(())
    }

    /// Unregister a host contract interface.
    /// Returns `true` if the contract was registered and removed, `false` if it was not found.
    pub fn unregister_host_contract(&self, contract_id: u64) -> bool {
        let mut guard: RecoveringGuard<
            std::sync::RwLockWriteGuard<'_, HashMap<u64, &'static HostContractInterface>>,
        > = self
            .host_contracts
            .write()
            .recover_poisoned(self.logger, "runtime");
        guard.remove(&contract_id).is_some()
    }

    /// Get a host contract interface by contract_id and minimum version.
    /// Returns `None` if no matching contract is found or if the version is too low.
    pub fn get_host_contract(
        &self,
        contract_id: u64,
        min_version: u32,
    ) -> Option<&'static HostContractInterface> {
        let guard: RecoveringGuard<
            std::sync::RwLockReadGuard<'_, HashMap<u64, &'static HostContractInterface>>,
        > = self
            .host_contracts
            .read()
            .recover_poisoned(self.logger, "runtime");
        guard.get(&contract_id).and_then(|interface| {
            if host_contract_version_satisfies(interface, min_version) {
                Some(*interface)
            } else {
                None
            }
        })
    }

    /// Get the host language type.
    #[inline(always)]
    pub fn host_language(&self) -> SupportedLanguage {
        self.host_language
    }

    /// Get the HostApi pointer for use in plugin registrars.
    ///
    /// Returns a raw pointer rather than a reference: the HostApi is owned by the
    /// `Runtime` (a `Box<HostApi>` with a stable heap address), so its validity is
    /// tied to the runtime's lifetime, not `'static`. The FFI/loaders already treat
    /// it as a raw pointer.
    #[inline(always)]
    pub fn host_abi(&self) -> *const HostApi {
        &*self.host_abi as *const HostApi
    }

    /// Get the HostApi pointer for passing to guest contracts.
    ///
    /// Returns the runtime's owned HostApi, whose `runtime` field was
    /// patched once in `RuntimeBuilder::build` to point at this Runtime.
    /// The runtime pointer can be extracted via `(*host_interface).runtime`.
    ///
    /// # Safety
    /// The returned pointer is valid for the lifetime of the Runtime.
    #[inline(always)]
    pub fn as_context_ptr(&self) -> *const HostApi {
        &*self.host_abi as *const HostApi
    }

    #[inline(always)]
    pub fn registry(&self) -> &Arc<RuntimeStore> {
        &self.registry
    }

    /// Get the runtime configuration.
    #[inline(always)]
    pub fn config(&self) -> &RuntimeConfig {
        &self.config
    }

    /// Get the reload callback.
    #[inline(always)]
    pub(crate) fn on_reload_cb(&self) -> &Option<ReloadCallback> {
        &self.on_reload_cb
    }

    /// Emit a Warn-level message through the runtime logger
    /// (`RuntimeConfig::log`, or stderr if no callback is installed).
    pub fn emit_warning(&self, msg: &str) {
        self.logger
            .log(LogLevel::Warn, "runtime", || msg.to_owned());
    }

    /// The runtime's logger handle.
    ///
    /// `LoggerHandle` is `Copy`: loaders take a copy at `load` time and store
    /// it in their per-bundle data so dispatch-time and teardown paths can log
    /// through the host callback. Same callback contract as
    /// `RuntimeConfig::log` — never invoke it while holding a lock guard.
    pub fn logger(&self) -> crate::logger::LoggerHandle {
        self.logger
    }

    /// Set the last error message for FFI error reporting.
    pub(crate) fn set_last_error(&self, msg: impl Into<String>) {
        let mut guard: RecoveringGuard<std::sync::MutexGuard<'_, String>> = self
            .last_error
            .lock()
            .recover_poisoned(self.logger, "runtime");
        **guard = msg.into();
    }

    /// Record that a stateful instance of `contract_id` was created.
    ///
    /// Increments the per-contract live count. Called by
    /// `host_create_guest_instance` only for non-null (stateful) instances.
    fn note_instance_created(&self, contract_id: GuestContractId) {
        let mut guard: RecoveringGuard<std::sync::MutexGuard<'_, HashMap<GuestContractId, u64>>> =
            self.instance_counts
                .lock()
                .recover_poisoned(self.logger, "runtime");
        let entry: &mut u64 = guard.entry(contract_id).or_insert(0);
        *entry += 1;
    }

    /// Record that a stateful instance of `contract_id` was destroyed.
    ///
    /// Saturating decrement; the key is removed once its count reaches zero so the
    /// map only ever holds contracts with live instances. Called by
    /// `host_destroy_guest_instance` only for non-null (stateful) instances.
    fn note_instance_destroyed(&self, contract_id: GuestContractId) {
        let mut guard: RecoveringGuard<std::sync::MutexGuard<'_, HashMap<GuestContractId, u64>>> =
            self.instance_counts
                .lock()
                .recover_poisoned(self.logger, "runtime");
        if let Some(entry) = guard.get_mut(&contract_id) {
            *entry = entry.saturating_sub(1);
            if *entry == 0 {
                guard.remove(&contract_id);
            }
        }
    }

    /// Reset the live stateful-instance accounting for `contracts` to zero.
    ///
    /// Called after a successful reload swap. The swap epoch-reclaims the previous
    /// interface and the guest-side state of every instance it created, so all
    /// pre-reload instances of these contracts are dead: a correct caller observes
    /// the registry revision change and revalidates (recreating its instance on the
    /// new interface), and even runtime-mediated dispatch on a pre-reload instance
    /// would touch reclaimed backing. Post-reload instances are created fresh and
    /// re-increment from zero. Without this, instances abandoned across the reload
    /// (never destroyed through the now-dead interface) would inflate the diagnostic
    /// count permanently.
    pub(crate) fn reset_instance_counts_for_contracts(&self, contracts: &[GuestContractId]) {
        let mut guard: RecoveringGuard<std::sync::MutexGuard<'_, HashMap<GuestContractId, u64>>> =
            self.instance_counts
                .lock()
                .recover_poisoned(self.logger, "runtime");
        contracts.iter().for_each(|cid: &GuestContractId| {
            guard.remove(cid);
        });
    }

    /// Sum the live stateful-instance counts across the given contract ids.
    ///
    /// Used by the reload/unload UB-warning to report how many guest instances a
    /// bundle's contracts still hold before its interfaces are retired or freed.
    pub(crate) fn live_instance_count_for_contracts(&self, contracts: &[GuestContractId]) -> u64 {
        let guard: RecoveringGuard<std::sync::MutexGuard<'_, HashMap<GuestContractId, u64>>> = self
            .instance_counts
            .lock()
            .recover_poisoned(self.logger, "runtime");
        contracts
            .iter()
            .map(|cid: &GuestContractId| guard.get(cid).copied().unwrap_or(0))
            .sum()
    }

    /// Get the last error message for FFI error reporting.
    /// Returns the number of bytes written to the buffer.
    pub(crate) fn get_last_error(&self, buf: &mut [u8]) -> usize {
        let guard: RecoveringGuard<std::sync::MutexGuard<'_, String>> = self
            .last_error
            .lock()
            .recover_poisoned(self.logger, "runtime");
        let bytes: &[u8] = guard.as_bytes();
        let write_n: usize = bytes.len().min(buf.len());
        if write_n > 0 {
            buf[..write_n].copy_from_slice(&bytes[..write_n]);
        }
        write_n
    }

    /// Clear the last error message.
    pub(crate) fn clear_last_error(&self) {
        let mut guard: RecoveringGuard<std::sync::MutexGuard<'_, String>> = self
            .last_error
            .lock()
            .recover_poisoned(self.logger, "runtime");
        guard.clear();
    }

    /// Get the length of the last error message.
    pub(crate) fn last_error_len(&self) -> usize {
        let guard: RecoveringGuard<std::sync::MutexGuard<'_, String>> = self
            .last_error
            .lock()
            .recover_poisoned(self.logger, "runtime");
        guard.len()
    }

    /// Register an additional bundle loader into this runtime after build.
    ///
    /// `loader` must be a `Box<dyn BundleLoader>` produced by a loader cdylib compiled
    /// against the same polyplug rlib. Ownership is transferred UNCONDITIONALLY: the
    /// `Box` is consumed (and, on the duplicate-loader error path, dropped) the moment
    /// this is called. The caller must NOT retain or free the loader afterwards, on
    /// either success or error — doing so would double-free.
    ///
    /// Returns `Err(RuntimeError::Loader(LoaderError::DuplicateLoader { .. }))` if a
    /// loader for the same loader name is already registered. The passed loader is
    /// still consumed in that case.
    pub fn register_loader(&self, loader: Box<dyn BundleLoader>) -> Result<(), RuntimeError> {
        let name: String = loader.loader_name().to_string();
        let mut loaders: RecoveringGuard<
            std::sync::RwLockWriteGuard<'_, HashMap<String, Box<dyn BundleLoader>>>,
        > = self
            .loaders
            .write()
            .recover_poisoned(self.logger, "runtime");
        if loaders.contains_key(&name) {
            return Err(RuntimeError::Loader(LoaderError::DuplicateLoader {
                loader_name: name,
            }));
        }

        loaders.insert(name, loader);
        Ok(())
    }

    /// Resolve a loader by loader name, returning a stable reference valid for the
    /// runtime's lifetime.
    ///
    /// The returned reference is obtained under a short-lived read guard and then
    /// detached. This is sound because loaders are append-only: once inserted into
    /// the `loaders` map a `Box<dyn BundleLoader>` is never removed or replaced for
    /// the runtime's lifetime, so the heap address behind the `Box` is stable. We
    /// must NOT hold the `loaders` read guard across `BundleLoader::load`/`reload`,
    /// because those run `polyplug_init`, which may call back into
    /// `host_register_loader` and take the `loaders` write guard — holding a read
    /// guard on the same thread would deadlock.
    pub(crate) fn loader_for(&self, loader_name: &str) -> Option<&dyn BundleLoader> {
        let loaders: RecoveringGuard<
            std::sync::RwLockReadGuard<'_, HashMap<String, Box<dyn BundleLoader>>>,
        > = self.loaders.read().recover_poisoned(self.logger, "runtime");
        let loader_ptr: *const dyn BundleLoader = loaders.get(loader_name).map(Box::as_ref)?;
        // SAFETY: loaders are append-only (never removed or replaced for the runtime
        // lifetime), so the `Box`'s heap allocation behind `loader_ptr` stays valid and
        // pinned for as long as `&self` lives. Detaching the reference from the guard
        // lets callers invoke load()/reload() without holding the lock (deadlock-free).
        Some(unsafe { &*loader_ptr })
    }

    /// Push a bundle_id onto the current thread's init stack.
    ///
    /// Loaders call this immediately before invoking `polyplug_init`. The matching
    /// [`Runtime::pop_init_bundle_id`] MUST be called afterwards (including on the
    /// panic path) so the stack does not leak entries.
    pub fn push_init_bundle_id(&self, bundle_id: u64) {
        let mut stack: RecoveringGuard<std::sync::MutexGuard<'_, HashMap<ThreadId, Vec<u64>>>> =
            self.init_bundle_stack
                .lock()
                .recover_poisoned(self.logger, "runtime");
        stack
            .entry(std::thread::current().id())
            .or_default()
            .push(bundle_id);
        // Bump the fast-path hint AFTER inserting into the stack but while still
        // holding the Mutex, so the counter and the stack are mutated atomically
        // with respect to other `push`/`pop` callers. See `active_init_count`.
        self.active_init_count.fetch_add(1, Ordering::Relaxed);
    }

    /// Pop the most recent bundle_id from the current thread's init stack.
    ///
    /// Restores the previous (outer) bundle_id for reentrant loads on the same thread.
    pub fn pop_init_bundle_id(&self) {
        let thread_id: ThreadId = std::thread::current().id();
        let mut stack: RecoveringGuard<std::sync::MutexGuard<'_, HashMap<ThreadId, Vec<u64>>>> =
            self.init_bundle_stack
                .lock()
                .recover_poisoned(self.logger, "runtime");
        if let Some(thread_stack) = stack.get_mut(&thread_id) {
            // Only decrement the hint when an entry was actually removed, so the
            // counter never drifts below the real number of pushed ids. A pop with
            // no matching entry (unbalanced caller) leaves the counter untouched.
            if thread_stack.pop().is_some() {
                self.active_init_count.fetch_sub(1, Ordering::Relaxed);
            }
            if thread_stack.is_empty() {
                stack.remove(&thread_id);
            }
        }
    }

    /// Get the bundle_id currently inside `polyplug_init` on this thread.
    ///
    /// Returns 0 when this thread is not inside any plugin init phase (i.e. for
    /// host-side lookups outside the init window).
    pub(crate) fn current_init_bundle_id(&self) -> u64 {
        // Fast path: no bundle is mid-init anywhere, so this thread certainly has
        // no stack entry. A single Relaxed load avoids the Mutex on the Phase-2 hot
        // path (every find / find_all / get_dependencies call). See the
        // `active_init_count` ordering rationale for why Relaxed is sound: a stale 0
        // cannot occur for this thread's own init window, and other threads' pushes
        // only ever make the counter larger.
        if self.active_init_count.load(Ordering::Relaxed) == 0 {
            return 0;
        }
        let thread_id: ThreadId = std::thread::current().id();
        let stack: RecoveringGuard<std::sync::MutexGuard<'_, HashMap<ThreadId, Vec<u64>>>> = self
            .init_bundle_stack
            .lock()
            .recover_poisoned(self.logger, "runtime");
        stack
            .get(&thread_id)
            .and_then(|thread_stack| thread_stack.last().copied())
            .unwrap_or(0)
    }

    /// Load a single plugin bundle explicitly by path.
    ///
    /// Reads the companion manifest, finds the matching loader, and dispatches.
    /// Does NOT perform graph pre-validation — intended for programmatic loads.
    pub fn load_bundle(&self, path: &Path) -> Result<(), RuntimeError> {
        let compatibility: Compatibility = self.config.compatibility;
        self.load_bundle_with(
            path,
            LoadOptions {
                compatibility,
                ignore_function_count_mismatch: false,
            },
        )
    }

    /// Load a single plugin bundle from a non-path [`BundleSource`].
    ///
    /// The caller supplies an already-parsed [`ManifestData`] because in-memory
    /// sources ([`BundleSource::Code`] / [`BundleSource::Bytes`]) have no bundle
    /// directory to scan. Path-based loading should use [`Runtime::load_bundle`] /
    /// `load_bundle_with`, which construct a [`BundleSource::Path`] internally.
    ///
    /// [`BundleSource`]: crate::loader::BundleSource
    /// [`BundleSource::Code`]: crate::loader::BundleSource::Code
    /// [`BundleSource::Bytes`]: crate::loader::BundleSource::Bytes
    /// [`BundleSource::Path`]: crate::loader::BundleSource::Path
    pub fn load_bundle_from_source(
        &self,
        manifest: ManifestData,
        source: crate::loader::BundleSource,
    ) -> Result<(), RuntimeError> {
        let compatibility: Compatibility = self.config.compatibility;
        self.load_manifest_with_source(
            manifest,
            source,
            LoadOptions {
                compatibility,
                ignore_function_count_mismatch: false,
            },
        )
    }

    /// Unload a bundle: invalidate its handles, remove it from the registry, and
    /// reclaim its interface and per-loader resources via epoch-deferred reclamation.
    ///
    /// First the registry is invalidated: the bundle's slots have their generation
    /// bumped and the bundle is removed from the registry indices, then the superseded
    /// interface `Arc` is handed to crossbeam-epoch for deferred reclamation. After this,
    /// every old handle fails to resolve with `StaleHandle` and no new resolve can hand
    /// out a pointer into the bundle. A reader pinned before the unload keeps the old
    /// interface `Arc` (and its backing library / VM) alive until it unpins; a raw
    /// `GuestContractInterface` pointer cached before the unload and used after it is
    /// undefined behaviour — see the host-coordination contract below.
    ///
    /// Then the matching loader's reclaim hook runs; reclamation is uniformly
    /// epoch-deferred, so the actual free happens only once no reader is still pinned in
    /// the epoch that preceded the unload (see [`crate::loader::BundleLoader::unload`]):
    /// - **Native loader:** `dlclose`s the dylib (drops the `libloading::Library`),
    ///   releasing OS resources and the on-disk file lock.
    /// - **VM loaders (Lua, JS):** drop the bundle's per-bundle VM.
    /// - **Python loader:** purges the bundle's re-keyed `sys.modules` entries so a later
    ///   load re-imports fresh source (CPython is single-init per process and cannot be
    ///   torn down).
    /// - **.NET loader:** unloads the bundle's collectible `AssemblyLoadContext`; its
    ///   assemblies are GC-reclaimed once all references and native frames clear.
    ///
    /// # Host-coordination contract
    /// Runtime-mediated calls — `create_guest_instance` and `destroy_guest_instance` —
    /// pin the epoch across dispatch, so a call racing an
    /// unload from another thread keeps the interface and its backing library / VM alive
    /// until the call returns. Direct FFI host callers do NOT pin per call (the fast
    /// path): the host MUST NOT call a bundle's contracts through a cached raw interface
    /// pointer concurrently with — or after — unloading it. This is the same
    /// trusted-same-process posture `docs/TRUST_MODEL.md` and the reload `Preparing`
    /// callback already assume.
    ///
    /// # Errors
    /// - `BundleNotFound`: the bundle is not currently loaded.
    /// - `DependencyInUse`: a still-loaded bundle declared a dependency on a contract
    ///   this bundle provides. Use [`Runtime::unload_bundle_cascade`] to unload the
    ///   dependents first.
    pub fn unload_bundle(&self, bundle_id: BundleId) -> Result<(), RuntimeError> {
        let descriptor: crate::runtime_store::BundleDescriptor = self
            .registry
            .get_bundle_descriptor(bundle_id)
            .ok_or_else(|| RuntimeError::BundleNotFound {
                bundle_name: format!("{:#x}", bundle_id.id()),
                contract_name: String::new(),
            })?;

        // Refuse-by-default (design D4): a still-loaded bundle that declared a
        // dependency on a contract this bundle provides would have its trust assumption
        // broken by an unload, so reject unless the caller cascades explicitly.
        let exported: HashSet<GuestContractId> = self
            .registry
            .bundle_exported_contracts(bundle_id)
            .into_iter()
            .collect();
        let mut dependents: Vec<String> = self
            .registry
            .bundles_depending_on_any(&exported)
            .into_iter()
            .filter(|dep: &BundleId| *dep != bundle_id)
            .filter_map(|dep: BundleId| {
                self.registry
                    .get_bundle_descriptor(dep)
                    .map(|d: crate::runtime_store::BundleDescriptor| d.name)
            })
            .collect();
        if !dependents.is_empty() {
            dependents.sort();
            return Err(RuntimeError::DependencyInUse {
                provider: descriptor.name,
                dependents,
            });
        }

        // Capture the loader name BEFORE invalidate: invalidate removes the
        // bundle metadata, after which the loader string is no longer recoverable.
        let loader_name: Option<String> = self.bundle_loader_name(&descriptor.name);

        // Fire the Unloading callback BEFORE invalidate so the host can quiesce its
        // own callers (the same window the reload Preparing phase gives it). The name
        // is owned by `descriptor`, which outlives this synchronous call.
        self.fire_unloading(bundle_id, &descriptor.name);

        // Unload truly frees the bundle's resources, so any still-live guest instance
        // is a genuine use-after-free hazard. Warn (do not block) before invalidate.
        let live: u64 = self
            .live_instance_count_for_contracts(&self.registry.bundle_exported_contracts(bundle_id));
        if live > 0 {
            let name: String = descriptor.name.clone();
            self.logger.log(LogLevel::Warn, "runtime", || {
                format!(
                    "unload: bundle '{name}' still has {live} live guest instance(s) across its \
                     contracts; destroy them before unload to avoid use-after-free. Proceeding anyway."
                )
            });
        }

        let _count: u32 = self.registry.invalidate_bundle(bundle_id)?;

        // Invalidate-then-reclaim: now that the bundle is gone from the registry
        // indices, no new dispatch can reach it. Ask the loader to free its
        // per-bundle resources at a quiescence point (no-op for non-VM loaders).
        self.reclaim_via_loader(bundle_id, loader_name.as_deref())?;
        Ok(())
    }

    /// Fire the `on_reload_cb` with a `ReloadPhase::unloading` notification, if a
    /// callback is registered. Called before invalidate so the host can quiesce its
    /// own callers ahead of reclamation. The `StringView` is constructed inline from
    /// the caller-owned `bundle_name`, which outlives this synchronous invocation.
    fn fire_unloading(&self, bundle_id: BundleId, bundle_name: &str) {
        if let Some(cb) = self.on_reload_cb() {
            let name_view: polyplug_abi::types::StringView = polyplug_abi::types::StringView {
                ptr: bundle_name.as_ptr(),
                len: bundle_name.len(),
            };
            (cb.0)(
                self.config().on_reload_user_data,
                polyplug_abi::runtime::ReloadPhase::unloading(bundle_id, name_view),
            );
        }
    }

    /// Look up the loader-name string for a loaded bundle by name.
    ///
    /// The original `manifest.loader` string (e.g. `"lua"`, `"js-quickjs"`) is the
    /// key the load path used to resolve the loader, and the only value that maps
    /// back to a `BundleLoader::loader_name()`. It is read from `bundle_manifests`,
    /// which must be consulted BEFORE `invalidate_bundle` removes the bundle.
    fn bundle_loader_name(&self, bundle_name: &str) -> Option<String> {
        let manifests: RecoveringGuard<std::sync::MutexGuard<'_, HashMap<String, ManifestData>>> =
            self.bundle_manifests
                .lock()
                .recover_poisoned(self.logger, "runtime");
        manifests
            .get(bundle_name)
            .map(|m: &ManifestData| m.loader.clone())
    }

    /// Invoke the loader's `unload` reclaim hook for `bundle_id`.
    ///
    /// `loader_name` is the loader key captured before invalidate. A missing name
    /// or missing loader is not an error: a bundle with no recoverable loader simply
    /// has nothing to reclaim (the invalidate already retired its interfaces).
    ///
    /// See [`crate::loader::BundleLoader::unload`] for the loader-side reclaim contract.
    fn reclaim_via_loader(
        &self,
        bundle_id: BundleId,
        loader_name: Option<&str>,
    ) -> Result<(), RuntimeError> {
        let name: &str = match loader_name {
            Some(n) => n,
            None => return Ok(()),
        };
        match self.loader_for(name) {
            Some(loader) => loader.unload(bundle_id, self).map_err(RuntimeError::Loader),
            None => Ok(()),
        }
    }

    /// Unload a bundle and every bundle that depends on it, dependents first.
    ///
    /// Recursively unloads bundles that declared a dependency on a contract the target
    /// provides before unloading the target itself, so no `DependencyInUse` refusal is
    /// hit. A `visited` set breaks dependency cycles. Like [`Runtime::unload_bundle`],
    /// each unload is true unload: handles go stale and the interface and per-loader
    /// resources are reclaimed via epoch-deferred reclamation.
    pub fn unload_bundle_cascade(&self, bundle_id: BundleId) -> Result<(), RuntimeError> {
        let mut visited: HashSet<BundleId> = HashSet::new();
        self.unload_bundle_cascade_with_visited(bundle_id, &mut visited)
    }

    /// Cascade-unload `bundle_id`, tracking already-unloaded bundles in `visited` to
    /// break dependency cycles.
    fn unload_bundle_cascade_with_visited(
        &self,
        bundle_id: BundleId,
        visited: &mut HashSet<BundleId>,
    ) -> Result<(), RuntimeError> {
        if !visited.insert(bundle_id) {
            return Ok(());
        }

        if self.registry.get_bundle_descriptor(bundle_id).is_none() {
            return Err(RuntimeError::BundleNotFound {
                bundle_name: format!("{:#x}", bundle_id.id()),
                contract_name: String::new(),
            });
        }

        let exported: HashSet<GuestContractId> = self
            .registry
            .bundle_exported_contracts(bundle_id)
            .into_iter()
            .collect();
        let dependents: Vec<BundleId> = self
            .registry
            .bundles_depending_on_any(&exported)
            .into_iter()
            .filter(|dep: &BundleId| *dep != bundle_id)
            .collect();
        for dep in dependents {
            self.unload_bundle_cascade_with_visited(dep, visited)?;
        }

        // Capture the loader runtime-name before invalidate removes the metadata,
        // then reclaim the loader's per-bundle state after invalidate (see
        // [`Runtime::unload_bundle`]).
        let bundle_name: String = self
            .registry
            .get_bundle_descriptor(bundle_id)
            .map(|d: crate::runtime_store::BundleDescriptor| d.name)
            .unwrap_or_default();
        let loader_name: Option<String> = self.bundle_loader_name(&bundle_name);

        // Fire Unloading before invalidate so the host can quiesce (mirrors unload_bundle).
        self.fire_unloading(bundle_id, &bundle_name);

        // Warn (do not block) on still-live instances before the bundle is freed
        // (mirrors unload_bundle).
        let live: u64 = self
            .live_instance_count_for_contracts(&self.registry.bundle_exported_contracts(bundle_id));
        if live > 0 {
            let name: String = bundle_name.clone();
            self.logger.log(LogLevel::Warn, "runtime", || {
                format!(
                    "unload: bundle '{name}' still has {live} live guest instance(s) across its \
                     contracts; destroy them before unload to avoid use-after-free. Proceeding anyway."
                )
            });
        }

        let _count: u32 = self.registry.invalidate_bundle(bundle_id)?;

        self.reclaim_via_loader(bundle_id, loader_name.as_deref())?;
        Ok(())
    }

    /// Load a single plugin bundle explicitly with options.
    pub(crate) fn load_bundle_with(
        &self,
        path: &Path,
        opts: LoadOptions,
    ) -> Result<(), RuntimeError> {
        // Determine the bundle directory: if path is a file, use its parent; otherwise use path as-is.
        let bundle_dir: &Path = if path.is_file() {
            path.parent().unwrap_or(path)
        } else {
            path
        };

        let manifest: ManifestData = crate::loader::parse_manifest(bundle_dir)
            .map_err(|e: LoaderError| RuntimeError::Loader(e))?;
        let source: crate::loader::BundleSource =
            crate::loader::BundleSource::Path(manifest.path.clone());
        self.load_manifest_with_source(manifest, source, opts)
    }

    /// Shared load path: validate the manifest, dispatch to the matching loader with
    /// the given [`BundleSource`], and record bundle metadata on success.
    ///
    /// [`BundleSource`]: crate::loader::BundleSource
    pub(crate) fn load_manifest_with_source(
        &self,
        manifest: ManifestData,
        source: crate::loader::BundleSource,
        opts: LoadOptions,
    ) -> Result<(), RuntimeError> {
        // Full manifest validation (required fields, id == FNV1a-64(name), well-formed
        // provides/bundle_dependencies version specs). Folds in the former inline
        // id == 0 check.
        manifest
            .validate()
            .map_err(|e: LoaderError| RuntimeError::Loader(e))?;

        // Validate function_count entries for this explicit load
        if !opts.ignore_function_count_mismatch {
            let major_str: &str = match manifest.version.split_once('.') {
                Some((maj, _)) => maj,
                None => "0",
            };
            for contract in &manifest.provides {
                // Extract contract name without version (e.g., "data.Reporter" from "data.Reporter@1.0")
                let contract_name: &str = match contract.split_once('@') {
                    Some((name, _)) => name,
                    None => contract,
                };
                let key: String = format!("{}@{}", contract_name, major_str);
                if !manifest.function_count.contains_key(&key)
                    && opts.compatibility != Compatibility::Yolo
                {
                    let msg: String = format!(
                        "bundle {:?} provides {:?} but has no function_count entry for key {:?}",
                        manifest.name, contract, key
                    );
                    if opts.compatibility == Compatibility::Strict {
                        return Err(RuntimeError::Loader(LoaderError::FunctionCountMismatch {
                            contract: contract.clone(),
                            // sentinel 0/0: entry is missing entirely; actual count is unknown without loading the .so
                            expected: 0,
                            found: 0,
                        }));
                    } else {
                        self.emit_warning(&msg);
                    }
                }
            }
        }

        // Find the loader for this bundle. The lock is released before load() runs
        // (see `loader_for`) so a plugin init that registers a loader cannot deadlock.
        let loader_name: &str = &manifest.loader;
        let loader: &dyn BundleLoader = self.loader_for(loader_name).ok_or_else(|| {
            RuntimeError::Loader(LoaderError::NoLoaderForName {
                bundle: manifest.name.clone(),
                loader_name: loader_name.to_owned(),
            })
        })?;

        // Declare this bundle's dependency contract_ids in the registry BEFORE
        // calling the loader. The loader runs `polyplug_init`, during which the
        // plugin may resolve its declared dependencies via `find_guest_contract`.
        // Dependency enforcement (host_find_guest_contract) consults this set, so
        // it must be populated before init runs — otherwise even declared lookups
        // would be denied. See docs/TRUST_MODEL.md §3/§4.
        let declared_contract_ids: Vec<GuestContractId> = manifest
            .dependencies
            .iter()
            .map(|dep: &crate::loader::RawManifestDependency| dep.contract_id)
            .collect();
        let bundle_id: BundleId = BundleId::new(&manifest.name);
        if let Err(e) = self
            .registry
            .declare_bundle_dependencies(bundle_id, declared_contract_ids)
        {
            return Err(RuntimeError::Registry(e));
        }

        let result: Result<(), RuntimeError> = loader
            .load(&manifest, &source, self)
            .map_err(RuntimeError::Loader);
        if result.is_ok() {
            let bundle_name: String = manifest.name.clone();

            // Parse bundle dependencies from new bundle-level format
            let bundle_deps: Vec<crate::runtime_store::BundleDependency> =
                manifest.parsed_bundle_dependencies();

            // Parse version from manifest. A malformed version is malformed manifest
            // content — reject it rather than silently coercing to 0.0.0.
            let bundle_version: Version =
                parse_manifest_version(&manifest.version, &manifest.name, &manifest.path)?;

            // Convert loader string to SupportedLanguage. An unrecognized loader
            // string falls back to Rust (see `supported_language_from_str`); surface
            // that as a warning so a typo'd `loader` field is not silently coerced.
            if !is_known_runtime_language(&manifest.loader) {
                self.logger.log(LogLevel::Warn, "runtime", || {
                    format!(
                        "bundle `{}`: unknown loader `{}`; defaulting SupportedLanguage to Rust",
                        manifest.name, manifest.loader
                    )
                });
            }
            let runtime_lang: SupportedLanguage = supported_language_from_str(&manifest.loader);

            // Register bundle metadata in RuntimeStore. A failure here means the
            // bundle loaded but its metadata could not be recorded, leaving the
            // store inconsistent — propagate it instead of silently discarding.
            self.registry.register_bundle_metadata(
                bundle_id,
                manifest.name.clone(),
                bundle_version,
                runtime_lang,
                manifest.path.clone(),
                bundle_deps,
            )?;

            // Real function_count validation: now that the bundle is loaded and its
            // interfaces registered, compare the manifest's declared function counts
            // against each native interface's actual `dispatch.native.function_count`.
            // The pre-load presence check above only proves an entry exists; this
            // proves the declared number matches reality. VM-dispatch interfaces have
            // no exposed count and are skipped.
            if !opts.ignore_function_count_mismatch && opts.compatibility != Compatibility::Yolo {
                self.validate_loaded_function_counts(bundle_id, &manifest, opts.compatibility)?;
            }

            let mut manifests: RecoveringGuard<
                std::sync::MutexGuard<'_, HashMap<String, ManifestData>>,
            > = self
                .bundle_manifests
                .lock()
                .recover_poisoned(self.logger, "runtime");
            manifests.insert(bundle_name, manifest);
        }
        result
    }

    /// Compare declared `function_count` entries against the actual native counts of
    /// the bundle's freshly-registered interfaces.
    ///
    /// In `Strict` mode a mismatch is an error; in `Relaxed` mode it emits a warning.
    /// Only native-dispatch interfaces carry an observable count; VM interfaces are
    /// skipped (their count is `None`).
    fn validate_loaded_function_counts(
        &self,
        bundle_id: BundleId,
        manifest: &ManifestData,
        compatibility: Compatibility,
    ) -> Result<(), RuntimeError> {
        let registered: Vec<(String, u32, Option<u32>)> =
            self.registry.bundle_native_function_counts(bundle_id);
        for (contract_name, major, actual_opt) in registered {
            let actual: u32 = match actual_opt {
                Some(n) => n,
                None => continue, // VM dispatch: no observable count.
            };
            let key: String = format!("{}@{}", contract_name, major);
            let declared: u32 = match manifest.function_count.get(&key) {
                Some(n) => *n,
                None => continue, // Missing-entry case already handled pre-load.
            };
            if declared != actual {
                match compatibility {
                    Compatibility::Strict => {
                        return Err(RuntimeError::Loader(LoaderError::FunctionCountMismatch {
                            contract: key,
                            expected: declared,
                            found: actual,
                        }));
                    }
                    Compatibility::Relaxed => {
                        self.logger.log(LogLevel::Warn, "runtime", || {
                            format!(
                                "bundle `{}` contract `{}`: declared function_count {} but interface exports {}",
                                manifest.name, key, declared, actual
                            )
                        });
                    }
                    Compatibility::Yolo => {}
                }
            }
        }
        Ok(())
    }
}

// ─── Module-level validation helpers ────────────────────────────────────────

/// Validate version compatibility for all discovered bundles.
///
/// Iterates each bundle's dependencies. For each dependency with a `min_version`,
/// finds the provider bundle and compares versions.
/// Also checks that each provided contract has a `function_count` entry.
///
/// Behaviour depends on `compatibility`:
/// - `Strict`: returns `Err` on any mismatch
/// - `Relaxed`: emits warning, continues
/// - `Yolo`: silently ignores all mismatches
pub(crate) fn validate_bundle_compatibility(
    manifests: &[(PathBuf, ManifestData)],
    compatibility: Compatibility,
    logger: LoggerHandle,
) -> Result<(), RuntimeError> {
    // Build provider_map: bare contract_name -> &ManifestData.
    //
    // A `provides` entry may be `name` or `name@version`; dependencies always name
    // the bare contract. Key the map on the bare name (strip any `@version` suffix)
    // so a versioned provides entry still resolves a bare-named dependency. This
    // matches the stripping that `load_manifest_with_source` already applies when
    // building function_count keys.
    let mut provider_map: HashMap<String, &ManifestData> = HashMap::new();
    for (_path, manifest) in manifests {
        for contract in &manifest.provides {
            let bare_contract: &str = match contract.split_once('@') {
                Some((name, _)) => name,
                None => contract.as_str(),
            };
            provider_map.insert(bare_contract.to_owned(), manifest);
        }
    }

    for (path, manifest) in manifests {
        // Check version compatibility for each dependency
        let resolved: Vec<ManifestDependency> = manifest.resolved_dependencies_with_logger(logger);
        for dep in &resolved {
            let (dep_contract, dep_min_version_str): (&str, &str) = match dep {
                ManifestDependency::ByContract {
                    contract,
                    min_version,
                    ..
                } => (contract.as_str(), min_version.as_str()),
                ManifestDependency::ByBundle {
                    contract,
                    min_version,
                    ..
                } => (contract.as_str(), min_version.as_str()),
            };

            if dep_min_version_str.is_empty() {
                continue;
            }

            let provider: &ManifestData = match provider_map.get(dep_contract) {
                Some(p) => p,
                None => continue, // graph already validates this
            };

            let required: Version = match Version::from_str(dep_min_version_str) {
                Ok(v) => v,
                Err(e) => {
                    return Err(RuntimeError::Loader(LoaderError::ManifestParse {
                        path: path.display().to_string(),
                        reason: format!("invalid version '{}': {:?}", dep_min_version_str, e),
                    }));
                }
            };

            let provided: Version =
                parse_manifest_version(&provider.version, &provider.name, path)?;

            if !provided.is_compatible_with(&required) {
                match compatibility {
                    Compatibility::Strict => {
                        return Err(RuntimeError::Loader(LoaderError::VersionMismatch {
                            contract: dep_contract.to_owned(),
                            required,
                            found: provided,
                        }));
                    }
                    Compatibility::Relaxed => {
                        logger.log(LogLevel::Warn, "runtime", || {
                            format!(
                                "version mismatch for contract `{}`: required={}, found={} (bundle `{}`)",
                                dep_contract, required, provided, provider.name
                            )
                        });
                    }
                    Compatibility::Yolo => {} // intentionally silent — Yolo mode skips all version checks
                }
            }
        }

        // Check function_count entries for provided contracts
        for contract in &manifest.provides {
            // Strip any `@version` suffix from the provides entry so the
            // function_count key is `bare_name@major`, identical to the key
            // `load_manifest_with_source` builds and looks up.
            let bare_contract: &str = match contract.split_once('@') {
                Some((name, _)) => name,
                None => contract.as_str(),
            };
            let major_str: &str = match manifest.version.split_once('.') {
                Some((maj, _)) => maj,
                None => "0",
            };
            let key: String = format!("{}@{}", bare_contract, major_str);
            if !manifest.function_count.contains_key(&key) {
                match compatibility {
                    Compatibility::Strict => {
                        return Err(RuntimeError::Loader(LoaderError::FunctionCountMismatch {
                            contract: contract.clone(),
                            // sentinel 0/0: entry is missing entirely; actual count is unknown without loading the .so
                            expected: 0,
                            found: 0,
                        }));
                    }
                    Compatibility::Relaxed => {
                        logger.log(LogLevel::Warn, "runtime", || {
                            format!(
                                "bundle `{}` provides `{}` but has no function_count entry for key `{}`",
                                manifest.name, contract, key
                            )
                        });
                    }
                    Compatibility::Yolo => {} // intentionally silent — Yolo mode skips all function_count checks
                }
            }
        }
    }

    Ok(())
}

fn parse_manifest_version(
    v: &str,
    _bundle_name: &str,
    manifest_path: &std::path::Path,
) -> Result<Version, RuntimeError> {
    if v.is_empty() {
        return Ok(Version {
            major: 0,
            minor: 0,
            patch: 0,
        });
    }
    // A malformed version string is malformed manifest content: reject it with
    // ManifestParse, mirroring how the dependency `required` version is parsed.
    match Version::from_str(v) {
        Ok(version) => Ok(version),
        Err(e) => Err(RuntimeError::Loader(LoaderError::ManifestParse {
            path: manifest_path.display().to_string(),
            reason: format!("invalid version '{}': {:?}", v, e),
        })),
    }
}

/// Helper to create a null GuestContractHandle.
fn plugin_handle_null() -> GuestContractHandle {
    GuestContractHandle::null()
}

/// Host-contract version negotiation (see `docs/HOST_CONTRACTS.md`).
///
/// `min_version` is the requested version packed as `(major << 16) | minor`,
/// matching the constant every generator emits. A host contract satisfies the
/// request iff its major matches EXACTLY and its minor is `>=` the requested
/// minor. A higher major is NOT compatible (breaking change); a lower minor is
/// NOT compatible (missing functions).
///
/// `min_version == 0` is the documented wildcard ("accept any version"): real
/// contracts are `>= 1.0`, so a packed request never legitimately equals 0.
fn host_contract_version_satisfies(interface: &HostContractInterface, min_version: u32) -> bool {
    if min_version == 0 {
        return true;
    }
    let req_major: u32 = min_version >> 16;
    let req_minor: u32 = min_version & 0xFFFF;
    interface.contract_version.major == req_major && interface.contract_version.minor >= req_minor
}

/// Convert a runtime string from manifest.toml to SupportedLanguage enum.
///
/// An unrecognized string falls back to [`SupportedLanguage::Rust`]. Callers that want
/// to flag a typo should first consult [`is_known_runtime_language`] and emit a
/// warning, since this function cannot distinguish "rust" from a misspelling.
fn supported_language_from_str(s: &str) -> SupportedLanguage {
    match s {
        "native" | "rust" => SupportedLanguage::Rust,
        "python" => SupportedLanguage::Python,
        "lua" => SupportedLanguage::Lua,
        "javascript" | "js" => SupportedLanguage::JavaScript,
        "dotnet" | "csharp" => SupportedLanguage::Dotnet,
        "cpp" => SupportedLanguage::Cpp,
        _ => SupportedLanguage::Rust,
    }
}

/// Returns `true` iff `s` is a runtime string [`supported_language_from_str`] maps
/// explicitly (i.e. NOT via its catch-all Rust fallback). Used to warn on an unknown
/// `runtime` field before it is silently coerced to Rust.
fn is_known_runtime_language(s: &str) -> bool {
    matches!(
        s,
        "native" | "rust" | "python" | "lua" | "javascript" | "js" | "dotnet" | "csharp" | "cpp"
    )
}

/// Convert a `StringView` to an owned, strictly-validated UTF-8 `String`.
///
/// The contract name keys the registry, so a lossy conversion could silently
/// replace invalid bytes with U+FFFD and alias two distinct names. Invalid UTF-8
/// is therefore rejected with [`RuntimeError::InvalidUtf8`] rather than coerced.
///
/// # Safety
/// `sv.ptr` must be valid for `sv.len` bytes for the duration of this call, or be null.
unsafe fn string_view_to_string_owned(
    sv: &polyplug_abi::types::StringView,
    context: &str,
) -> Result<String, RuntimeError> {
    if sv.ptr.is_null() || sv.len == 0 {
        return Ok(String::new());
    }
    // SAFETY: caller guarantees ptr/len describe a valid byte range for this call.
    let slice: &[u8] = unsafe { core::slice::from_raw_parts(sv.ptr, sv.len) };
    match core::str::from_utf8(slice) {
        Ok(s) => Ok(s.to_owned()),
        Err(_) => Err(RuntimeError::InvalidUtf8 {
            context: context.to_owned(),
        }),
    }
}

// ─── HostApi C ABI callbacks ───────────────────────────────────────────────

/// Validate the function-pointer fields of a plugin/host-provided contract
/// interface WITHOUT materializing the typed struct.
///
/// The ABI types `create_instance` / `destroy_instance` / `dispatch.vm.call`
/// as bare (non-`Option`) `fn` pointers because they are REQUIRED: failure is
/// signalled through a null *instance handle* return, never through a null
/// callback. A foreign producer can still hand us a struct with null bits in
/// those slots, and reading such a field at its `fn` type would materialize an
/// invalid value (UB in Rust) — so the fields are read here as raw data
/// pointers and rejected with a precise error before any typed access.
///
/// `create_offset` / `destroy_offset` / `dispatch_type_offset` /
/// `dispatch_offset` are the byte offsets of the respective fields inside the
/// interface struct (they differ between `GuestContractInterface` and
/// `HostContractInterface`). Inside the `DispatchMechanisms` union,
/// `vm.call` lives at offset 0, `native.function_count` at offset 0, and
/// `native.functions` at offset 8 (asserted by the ABI layout tests).
///
/// Returns the first violation as a static message, or `None` when the
/// interface is well-formed.
///
/// # Safety
/// `base` must be non-null, properly aligned for the interface struct, and
/// point to at least `dispatch_offset + 16` readable bytes.
unsafe fn validate_interface_fn_ptrs(
    base: *const u8,
    create_offset: usize,
    destroy_offset: usize,
    dispatch_type_offset: usize,
    dispatch_offset: usize,
    context: ValidationContext,
) -> Option<&'static str> {
    // SAFETY: the caller guarantees `base` covers the interface struct; every
    // read below is in-bounds and reads pointer/integer bits only (never an
    // `fn`-typed value), so null bits are observed safely.
    unsafe {
        let create_ptr: *const core::ffi::c_void = base
            .add(create_offset)
            .cast::<*const core::ffi::c_void>()
            .read();
        if create_ptr.is_null() {
            return Some(match context {
                ValidationContext::Guest => {
                    "register_guest_contract: create_instance is null — the field is required; signal create failure by returning a null instance handle instead"
                }
                ValidationContext::Host => {
                    "register_host_contract: create_instance is null — the field is required; signal create failure by returning a null instance handle instead"
                }
            });
        }
        let destroy_ptr: *const core::ffi::c_void = base
            .add(destroy_offset)
            .cast::<*const core::ffi::c_void>()
            .read();
        if destroy_ptr.is_null() {
            return Some(match context {
                ValidationContext::Guest => {
                    "register_guest_contract: destroy_instance is null — the field is required; use a no-op function for stateless contracts"
                }
                ValidationContext::Host => {
                    "register_host_contract: destroy_instance is null — the field is required; use a no-op function for singleton/stateless contracts"
                }
            });
        }

        let dispatch_type_raw: u32 = base.add(dispatch_type_offset).cast::<u32>().read();
        if dispatch_type_raw == polyplug_abi::dispatch::DispatchType::VirtualMachine as u32 {
            // DispatchMechanisms union, vm variant: call fn pointer at offset 0.
            let call_ptr: *const core::ffi::c_void = base
                .add(dispatch_offset)
                .cast::<*const core::ffi::c_void>()
                .read();
            if call_ptr.is_null() {
                return Some(match context {
                    ValidationContext::Guest => {
                        "register_guest_contract: dispatch.vm.call is null — required for VirtualMachine dispatch"
                    }
                    ValidationContext::Host => {
                        "register_host_contract: dispatch.vm.call is null — required for VirtualMachine dispatch"
                    }
                });
            }
        } else if dispatch_type_raw == polyplug_abi::dispatch::DispatchType::Native as u32 {
            // DispatchMechanisms union, native variant: function_count at
            // offset 0, functions pointer at offset 8.
            let function_count: u32 = base.add(dispatch_offset).cast::<u32>().read();
            let functions: *const *const core::ffi::c_void = base
                .add(dispatch_offset + 8)
                .cast::<*const *const core::ffi::c_void>()
                .read();
            if function_count > 0 {
                if functions.is_null() {
                    return Some(match context {
                        ValidationContext::Guest => {
                            "register_guest_contract: dispatch.native.functions is null while function_count > 0"
                        }
                        ValidationContext::Host => {
                            "register_host_contract: dispatch.native.functions is null while function_count > 0"
                        }
                    });
                }
                for fn_index in 0..function_count as usize {
                    if functions.add(fn_index).read().is_null() {
                        return Some(match context {
                            ValidationContext::Guest => {
                                "register_guest_contract: dispatch.native.functions contains a null entry within function_count"
                            }
                            ValidationContext::Host => {
                                "register_host_contract: dispatch.native.functions contains a null entry within function_count"
                            }
                        });
                    }
                }
            }
        }
    }
    None
}

/// Which registration path [`validate_interface_fn_ptrs`] is reporting for —
/// selects the precise error message prefix.
#[derive(Clone, Copy)]
enum ValidationContext {
    Guest,
    Host,
}

/// HostApi.register_guest_contract callback — registers a guest contract implementation with the runtime.
///
/// Reads bundle_id from the runtime's per-thread init stack (dependency enforcement).
///
/// # Safety
/// - this must be a valid HostApi pointer with valid runtime field
/// - descriptor must point to a valid PluginDescriptor
/// - interface must point to a valid GuestContractInterface that remains valid for the Runtime lifetime
pub(crate) unsafe extern "C" fn host_register_guest_contract(
    this: *const HostApi,
    descriptor: *const PluginDescriptor,
    interface: *const GuestContractInterface,
    out_err: *mut polyplug_abi::types::AbiError,
) {
    // SAFETY: the runtime is the sole producer of the HostApi table; the ABI
    // contract requires callers to pass a valid, non-null out_err pointer.
    let result: polyplug_abi::types::AbiError =
        unsafe { host_register_guest_contract_impl(this, descriptor, interface) };
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(result) };
    }
}

unsafe fn host_register_guest_contract_impl(
    this: *const HostApi,
    descriptor: *const PluginDescriptor,
    interface: *const GuestContractInterface,
) -> polyplug_abi::types::AbiError {
    if this.is_null() {
        return polyplug_abi::types::AbiError {
            code: polyplug_abi::types::AbiErrorCode::Generic as u32,
            message: polyplug_abi::types::StringView::null(),
        };
    }
    // Guard both plugin-provided pointers before any dereference. `descriptor` is
    // read below and `interface` is dereferenced inside the registry; a null in
    // either is a contract violation that must not become UB.
    if descriptor.is_null() {
        return polyplug_abi::types::AbiError {
            code: polyplug_abi::types::AbiErrorCode::InvalidPointer as u32,
            message: polyplug_abi::types::StringView::from_static(
                b"register_guest_contract: descriptor pointer is null",
            ),
        };
    }
    if interface.is_null() {
        return polyplug_abi::types::AbiError {
            code: polyplug_abi::types::AbiErrorCode::InvalidPointer as u32,
            message: polyplug_abi::types::StringView::from_static(
                b"register_guest_contract: interface pointer is null",
            ),
        };
    }
    // Reject null bits in the REQUIRED fn-pointer fields before any typed
    // access to the interface — reading a null at a bare `fn` type would be an
    // invalid value (UB), and accepting it would defer the crash to first use.
    // SAFETY: interface is non-null (checked above) and points to a
    // GuestContractInterface provided by the plugin for the runtime lifetime.
    if let Some(violation) = unsafe {
        validate_interface_fn_ptrs(
            interface.cast::<u8>(),
            core::mem::offset_of!(GuestContractInterface, create_instance),
            core::mem::offset_of!(GuestContractInterface, destroy_instance),
            core::mem::offset_of!(GuestContractInterface, dispatch_type),
            core::mem::offset_of!(GuestContractInterface, dispatch),
            ValidationContext::Guest,
        )
    } {
        return polyplug_abi::types::AbiError {
            code: polyplug_abi::types::AbiErrorCode::InvalidPointer as u32,
            message: polyplug_abi::types::StringView::from_static(violation.as_bytes()),
        };
    }
    // SAFETY: this is a valid HostApi pointer passed during polyplug_init.
    // (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };
    let registry: &RuntimeStore = &runtime.registry;
    // Get bundle_id from the runtime's per-thread init stack (pushed by the loader
    // before calling polyplug_init).
    let bundle_id: u64 = runtime.current_init_bundle_id();

    // SAFETY: descriptor is non-null (checked above) and provided by the plugin's
    // polyplug_init function.
    let desc: PluginDescriptor = unsafe { *descriptor };

    if desc.contract_name.ptr.is_null() || desc.contract_name.len == 0 {
        return polyplug_abi::types::AbiError {
            code: polyplug_abi::types::AbiErrorCode::Generic as u32,
            message: polyplug_abi::types::StringView::from_static(
                b"PluginDescriptor.contract_name is null or empty",
            ),
        };
    }

    // SAFETY: desc.contract_name.ptr is non-null and valid for len bytes during init.
    let contract_name: String = match unsafe {
        string_view_to_string_owned(&desc.contract_name, "PluginDescriptor.contract_name")
    } {
        Ok(name) => name,
        Err(e) => {
            runtime.set_last_error(e.to_string());
            runtime.logger.log(LogLevel::Error, "registry", || {
                format!("registration rejected for bundle {bundle_id}: {e}")
            });
            return polyplug_abi::types::AbiError {
                code: polyplug_abi::types::AbiErrorCode::Generic as u32,
                message: polyplug_abi::types::StringView::null(),
            };
        }
    };

    // SAFETY: interface is a valid 'static GuestContractInterface from the plugin binary
    match unsafe {
        registry.register_guest_contract(
            desc,
            interface,
            contract_name,
            BundleId::from_u64(bundle_id),
        )
    } {
        Ok(_handle) => polyplug_abi::types::AbiError::ok(),
        Err(e) => {
            runtime.logger.log(LogLevel::Error, "registry", || {
                format!("registration failed for bundle {bundle_id}: {e}")
            });
            // Surface the detail through get_last_error (stderr alone is not
            // programmatically reachable) and map the registry error to its
            // specific ABI code where one exists, so guests can distinguish a
            // same-bundle duplicate from a hash collision or bad input.
            runtime.set_last_error(e.to_string());
            let code: polyplug_abi::types::AbiErrorCode = match e {
                crate::error::RegistryError::DuplicateProvider { .. } => {
                    polyplug_abi::types::AbiErrorCode::DuplicateProvider
                }
                _ => polyplug_abi::types::AbiErrorCode::Generic,
            };
            polyplug_abi::types::AbiError {
                code: code as u32,
                message: polyplug_abi::types::StringView::null(),
            }
        }
    }
}

/// HostApi.alloc callback — allocate memory via the host allocator.
///
/// # Safety
/// this is ignored (system allocator is global). Standard alloc safety applies.
pub(crate) unsafe extern "C" fn host_alloc(
    _this: *const HostApi,
    size: usize,
    align: usize,
) -> *mut u8 {
    polyplug_abi::ffi::polyplug_host_alloc(size, align)
}

/// HostApi.free callback — free memory via the host allocator.
///
/// # Safety
/// this is ignored (system allocator is global). Standard free safety applies.
pub(crate) unsafe extern "C" fn host_free(
    _this: *const HostApi,
    ptr: *mut u8,
    size: usize,
    align: usize,
) {
    // SAFETY: polyplug_host_free is a safe wrapper around the system allocator.
    unsafe { polyplug_abi::ffi::polyplug_host_free(ptr, size, align) }
}

/// HostApi.find_guest_contract callback — dispatches to runtime's registry with dependency enforcement.
///
/// Reads bundle_id from the runtime's per-thread init stack during the init phase.
///
/// # Safety
/// this must be a valid HostApi pointer with valid runtime field.
pub(crate) unsafe extern "C" fn host_find_guest_contract(
    this: *const HostApi,
    contract_id: u64,
    min_version: u32,
) -> GuestContractHandle {
    if this.is_null() {
        return plugin_handle_null();
    }
    // SAFETY: this is a valid HostApi pointer passed by the host.
    // (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };
    let registry: &RuntimeStore = &runtime.registry;
    // Get bundle_id from the runtime's per-thread init stack for dependency
    // enforcement during the init phase.
    let caller_bundle_id: u64 = runtime.current_init_bundle_id();

    if caller_bundle_id != 0
        && !registry.is_bundle_dependency_declared(
            BundleId::from_u64(caller_bundle_id),
            GuestContractId::from_u64(contract_id),
        )
    {
        return plugin_handle_null();
    }
    match registry.find_guest_contract(GuestContractId::from_u64(contract_id), min_version) {
        Ok(h) => h,
        Err(_) => plugin_handle_null(),
    }
}

/// HostApi.find_all_by_contract callback — returns Array<GuestContractHandle>.
///
/// # Safety
/// - this must be a valid HostApi pointer with valid runtime field
pub(crate) unsafe extern "C" fn host_find_all_guest_contracts(
    this: *const HostApi,
    contract_id: u64,
    min_version: u32,
) -> polyplug_abi::Array<GuestContractHandle> {
    if this.is_null() {
        return Array::empty();
    }
    // SAFETY: this is a valid HostApi pointer passed by the host.
    // (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };
    let registry: &RuntimeStore = &runtime.registry;

    // Dependency enforcement during the init window: a plugin must not enumerate
    // providers of a contract it did not declare. Outside the window
    // (caller_bundle_id == 0, host-side lookups) enumeration is unrestricted.
    let caller_bundle_id: u64 = runtime.current_init_bundle_id();
    if caller_bundle_id != 0
        && !registry.is_bundle_dependency_declared(
            BundleId::from_u64(caller_bundle_id),
            GuestContractId::from_u64(contract_id),
        )
    {
        return Array::empty();
    }

    // Count AND collect under a SINGLE registry read guard. Splitting the count
    // and the fill across two guards is unsound: a concurrent unload shrinking the
    // registry between them would make the allocation size disagree with the
    // returned `Array.len`, and the SDK-side free (`len * sizeof(T)`) would then
    // deallocate with a layout differing from the allocation (UB). `vec.len()` is
    // therefore the single source of truth for both the allocation and `Array.len`.
    let handles: Vec<GuestContractHandle> =
        registry.collect_guest_contracts(GuestContractId::from_u64(contract_id), min_version);

    if handles.is_empty() {
        return Array::empty();
    }

    // Allocate via the host allocator, sized to exactly the collected handles.
    let count: usize = handles.len();
    let size: usize = count * core::mem::size_of::<GuestContractHandle>();
    let align: usize = core::mem::align_of::<GuestContractHandle>();
    // SAFETY: host_alloc is safe to call from this unsafe context.
    let ptr: *mut GuestContractHandle =
        unsafe { host_alloc(this, size, align) as *mut GuestContractHandle };

    if ptr.is_null() {
        return Array::empty();
    }

    // Copy the collected handles into the host-allocated buffer.
    // SAFETY: ptr was allocated by host_alloc with size = count * size_of::<GuestContractHandle>()
    // and is valid for `count` elements; `handles` holds exactly `count` initialised
    // elements; source and destination are distinct allocations (non-overlapping).
    unsafe {
        core::ptr::copy_nonoverlapping(handles.as_ptr(), ptr, count);
    }

    Array::new(ptr, count)
}

/// HostApi.resolve_guest_contract callback — returns interface pointer for a handle.
///
/// # Safety
/// this must be a valid HostApi pointer with valid runtime field.
pub unsafe extern "C" fn host_resolve_guest_contract(
    this: *const HostApi,
    handle: GuestContractHandle,
) -> *const GuestContractInterface {
    if this.is_null() {
        return core::ptr::null();
    }
    // SAFETY: this is a valid HostApi pointer passed by the host.
    // (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };
    let registry: &RuntimeStore = &runtime.registry;

    match registry.resolve_guest_contract(handle) {
        Ok(ptr) => ptr,
        Err(_) => core::ptr::null(),
    }
}

/// HostApi.revision_counter callback — returns a pointer to the runtime's registry
/// revision counter, which a generated host→guest caller fetches ONCE at construction
/// and then polls directly (a single aligned atomic load, no call back into the
/// runtime) before each dispatch. While the polled value is unchanged the caller's
/// cached interface pointer is current and it dispatches directly; any change (load /
/// reload-swap / unload) makes the caller re-resolve. See the `revision_counter` field
/// doc on `HostApi` and [`RuntimeStore::revision_ptr`].
///
/// Returns null if `this` is null; a caller receiving null treats its cache as
/// always-current (there is no runtime to mediate reloads against).
///
/// # Safety
/// `this` must be a valid `HostApi` pointer whose `runtime` field points to a live
/// `Runtime`.
pub(crate) unsafe extern "C" fn host_revision_counter(this: *const HostApi) -> *const u64 {
    if this.is_null() {
        return core::ptr::null();
    }
    // SAFETY: this is a valid HostApi pointer passed by the host.
    // (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };
    runtime.registry.revision_ptr()
}

/// HostApi.get_host_contract callback — returns an instance for a host contract.
///
/// For singleton contracts: returns cached instance (creates on first call).
/// For multi-instance contracts: creates new instance each call.
///
/// # Safety
/// this must be a valid HostApi pointer with valid runtime field.
pub(crate) unsafe extern "C" fn host_get_host_contract(
    this: *const HostApi,
    contract_id: u64,
    min_version: u32,
) -> HostContractInstance {
    if this.is_null() {
        return HostContractInstance::null();
    }
    // SAFETY: this is a valid HostApi pointer passed by the host.
    // (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };

    let host_contracts_guard: RecoveringGuard<
        std::sync::RwLockReadGuard<'_, HashMap<u64, &'static HostContractInterface>>,
    > = runtime
        .host_contracts
        .read()
        .recover_poisoned(runtime.logger, "runtime");

    let interface: Option<&HostContractInterface> = host_contracts_guard
        .values()
        .find(|iface| {
            iface.contract_id.id() == contract_id
                && host_contract_version_satisfies(iface, min_version)
        })
        .copied();

    match interface {
        Some(interface) => {
            // `interface` is `&'static` (it was `.copied()` out of the guard), so it
            // stays valid after the guard is dropped. Release the `host_contracts`
            // read guard BEFORE invoking `create_instance`: that callback may itself
            // call back into `register_host_contract`, which takes the `host_contracts`
            // WRITE lock — holding the read guard across it would deadlock.
            drop(host_contracts_guard);

            if interface.singleton {
                // Singleton: check cache first
                let singleton_guard: RecoveringGuard<
                    std::sync::RwLockReadGuard<'_, HashMap<u64, HostContractInstance>>,
                > = runtime
                    .singleton_instances
                    .read()
                    .recover_poisoned(runtime.logger, "runtime");
                if let Some(&instance) = singleton_guard.get(&contract_id) {
                    return instance;
                }
                drop(singleton_guard);

                // Create singleton and cache it
                let mut singleton_guard: RecoveringGuard<
                    std::sync::RwLockWriteGuard<'_, HashMap<u64, HostContractInstance>>,
                > = runtime
                    .singleton_instances
                    .write()
                    .recover_poisoned(runtime.logger, "runtime");
                // Double-check pattern: another thread may have created while we waited
                if let Some(&instance) = singleton_guard.get(&contract_id) {
                    return instance;
                }
                let mut instance: HostContractInstance = HostContractInstance::null();
                // SAFETY: interface.create_instance is a valid function pointer; the
                // HostContractInterface pointer is passed (self-passing pattern) and
                // `instance` is a valid, writable out-param.
                unsafe {
                    (interface.create_instance)(
                        interface as *const HostContractInterface,
                        core::ptr::null(),
                        &mut instance,
                    )
                };
                // Never cache a NULL instance: creation failed, so leave the cache
                // empty and let a later call retry. Caching null would poison the
                // singleton forever.
                if !instance.is_null() {
                    singleton_guard.insert(contract_id, instance);
                }
                instance
            } else {
                // Multi-instance: create new instance each call
                let mut instance: HostContractInstance = HostContractInstance::null();
                // SAFETY: interface.create_instance is a valid function pointer; the
                // HostContractInterface pointer is passed (self-passing pattern) and
                // `instance` is a valid, writable out-param.
                unsafe {
                    (interface.create_instance)(
                        interface as *const HostContractInterface,
                        core::ptr::null(),
                        &mut instance,
                    )
                };
                instance
            }
        }
        None => {
            runtime.set_last_error(format!(
                "host contract not found: id={}, min_version={}",
                contract_id, min_version
            ));
            HostContractInstance::null()
        }
    }
}

/// HostApi.resolve_host_contract_interface callback — returns HostContractInterface pointer.
///
/// # Safety
/// this must be a valid HostApi pointer with valid runtime field.
pub(crate) unsafe extern "C" fn host_resolve_host_contract_interface(
    this: *const HostApi,
    contract_id: u64,
    min_version: u32,
) -> *const HostContractInterface {
    if this.is_null() {
        return core::ptr::null();
    }
    // SAFETY: this is a valid HostApi pointer passed by the host.
    // (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };

    let host_contracts_guard: RecoveringGuard<
        std::sync::RwLockReadGuard<'_, HashMap<u64, &'static HostContractInterface>>,
    > = runtime
        .host_contracts
        .read()
        .recover_poisoned(runtime.logger, "runtime");

    host_contracts_guard
        .values()
        .find(|iface| {
            iface.contract_id.id() == contract_id
                && host_contract_version_satisfies(iface, min_version)
        })
        .map(|v| *v as *const HostContractInterface)
        .unwrap_or_else(|| {
            runtime.set_last_error(format!(
                "host contract interface not found: id={}, min_version={}",
                contract_id, min_version
            ));
            core::ptr::null()
        })
}

/// HostApi.list_bundles callback — returns Array<BundleId>.
///
/// # Safety
/// this must be a valid HostApi pointer with valid runtime field.
pub(crate) unsafe extern "C" fn host_list_bundles(
    this: *const HostApi,
) -> polyplug_abi::Array<polyplug_utils::BundleId> {
    if this.is_null() {
        return Array::empty();
    }
    // SAFETY: this is a valid HostApi pointer.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };

    let manifests: RecoveringGuard<std::sync::MutexGuard<'_, HashMap<String, ManifestData>>> =
        runtime
            .bundle_manifests
            .lock()
            .recover_poisoned(runtime.logger, "runtime");

    let count = manifests.len();
    if count == 0 {
        return Array::empty();
    }

    // Allocate via host allocator
    let size = count * core::mem::size_of::<BundleId>();
    let align = core::mem::align_of::<BundleId>();
    // SAFETY: host_alloc is safe to call
    let ptr = unsafe { host_alloc(this, size, align) as *mut BundleId };

    if ptr.is_null() {
        return Array::empty();
    }

    // Fill array
    for (i, (_, manifest)) in manifests.iter().enumerate() {
        // SAFETY: ptr was allocated with count elements and i < count.
        unsafe {
            *ptr.add(i) = BundleId::from_u64(manifest.id);
        }
    }

    Array::new(ptr, count)
}

/// HostApi.get_dependencies callback — returns Array<DependencyInfo>.
///
/// Looks up the calling bundle's dependencies using the bundle_id at the top of the
/// runtime's per-thread init-bundle stack (the instance-owned replacement for the
/// former process-global thread-local). Returns an empty array outside any init
/// window (top-of-stack bundle_id == 0).
///
/// # Safety
/// this must be a valid HostApi pointer with valid runtime field.
pub(crate) unsafe extern "C" fn host_get_dependencies(
    this: *const HostApi,
) -> polyplug_abi::Array<polyplug_abi::DependencyInfo> {
    if this.is_null() {
        return Array::empty();
    }
    // SAFETY: this is a valid HostApi pointer.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };

    // Get bundle_id from the runtime's per-thread init stack.
    let caller_bundle_id: u64 = runtime.current_init_bundle_id();
    if caller_bundle_id == 0 {
        return Array::empty();
    }

    let manifests: RecoveringGuard<std::sync::MutexGuard<'_, HashMap<String, ManifestData>>> =
        runtime
            .bundle_manifests
            .lock()
            .recover_poisoned(runtime.logger, "runtime");

    // Find manifest by ID
    let manifest = match manifests.values().find(|m| m.id == caller_bundle_id) {
        Some(m) => m,
        None => return Array::empty(),
    };

    let deps = &manifest.dependencies;
    if deps.is_empty() {
        return Array::empty();
    }

    let count = deps.len();
    let size = count * core::mem::size_of::<DependencyInfo>();
    let align = core::mem::align_of::<DependencyInfo>();
    // SAFETY: host_alloc is safe to call
    let ptr = unsafe { host_alloc(this, size, align) as *mut DependencyInfo };

    if ptr.is_null() {
        return Array::empty();
    }

    // Fill array with DependencyInfo
    for (i, dep) in deps.iter().enumerate() {
        let info = DependencyInfo {
            contract_id: dep.contract_id,
            min_version: dep.min_version.parse().unwrap_or(0),
            bundle_id: dep
                .bundle_id
                .unwrap_or_else(|| polyplug_utils::BundleId::from_u64(0)),
        };
        // SAFETY: ptr was allocated with count elements of DependencyInfo and i < count.
        unsafe {
            *ptr.add(i) = info;
        }
    }

    Array::new(ptr, count)
}

// ─── HostApi operation functions (18-02 implementation) ───────────────────
// These functions implement the HostApi operation fields for host applications.

/// HostApi.load_bundle callback — loads a plugin bundle from a path.
///
/// Host applications call this to load a bundle at runtime.
///
/// # Safety
/// - this must be a valid HostApi pointer with valid runtime field
/// - path must point to path_len valid UTF-8 bytes for the duration of the call
pub unsafe extern "C" fn host_load_bundle(
    this: *const HostApi,
    path: *const u8,
    path_len: usize,
    out_err: *mut polyplug_abi::AbiError,
) {
    // SAFETY: the runtime is the sole producer of the HostApi table; the ABI
    // contract requires callers to pass a valid, non-null out_err pointer.
    let result: polyplug_abi::AbiError = unsafe { host_load_bundle_impl(this, path, path_len) };
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(result) };
    }
}

unsafe fn host_load_bundle_impl(
    this: *const HostApi,
    path: *const u8,
    path_len: usize,
) -> polyplug_abi::AbiError {
    if this.is_null() {
        return AbiError {
            code: AbiErrorCode::InvalidPointer as u32,
            message: StringView::from_static(b"null HostApi in load_bundle"),
        };
    }
    // SAFETY: this is a valid HostApi pointer. (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };

    if path.is_null() {
        runtime.set_last_error("null path pointer in load_bundle");
        return AbiError {
            code: AbiErrorCode::InvalidPointer as u32,
            message: StringView::from_static(b"null path pointer in load_bundle"),
        };
    }

    // SAFETY: path is non-null and points to path_len valid bytes per ABI contract.
    let bytes: &[u8] = unsafe { core::slice::from_raw_parts(path, path_len) };
    let s: &str = match core::str::from_utf8(bytes) {
        Ok(s) => s,
        Err(e) => {
            runtime.set_last_error(e.to_string());
            return AbiError {
                code: AbiErrorCode::Generic as u32,
                message: StringView::null(),
            };
        }
    };

    match runtime.load_bundle(std::path::Path::new(s)) {
        Ok(()) => AbiError::ok(),
        Err(e) => {
            runtime.set_last_error(e.to_string());
            AbiError {
                code: AbiErrorCode::Generic as u32,
                message: StringView::null(),
            }
        }
    }
}

/// HostApi.reload_bundle callback — hot-reloads a plugin bundle.
///
/// Replaces the bundle's contracts with new versions from the updated binary.
///
/// # Safety
/// - this must be a valid HostApi pointer with valid runtime field
/// - path must point to path_len valid UTF-8 bytes for the duration of the call
pub unsafe extern "C" fn host_reload_bundle(
    this: *const HostApi,
    path: *const u8,
    path_len: usize,
    out_err: *mut polyplug_abi::AbiError,
) {
    // SAFETY: the runtime is the sole producer of the HostApi table; the ABI
    // contract requires callers to pass a valid, non-null out_err pointer.
    let result: polyplug_abi::AbiError = unsafe { host_reload_bundle_impl(this, path, path_len) };
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(result) };
    }
}

unsafe fn host_reload_bundle_impl(
    this: *const HostApi,
    path: *const u8,
    path_len: usize,
) -> polyplug_abi::AbiError {
    if this.is_null() {
        return AbiError {
            code: AbiErrorCode::InvalidPointer as u32,
            message: StringView::from_static(b"null HostApi in reload_bundle"),
        };
    }
    // SAFETY: this is a valid HostApi pointer. (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };

    if path.is_null() {
        runtime.set_last_error("null path pointer in reload_bundle");
        return AbiError {
            code: AbiErrorCode::InvalidPointer as u32,
            message: StringView::from_static(b"null path pointer in reload_bundle"),
        };
    }

    // SAFETY: path is non-null and points to path_len valid bytes per ABI contract.
    let bytes: &[u8] = unsafe { core::slice::from_raw_parts(path, path_len) };
    let s: &str = match core::str::from_utf8(bytes) {
        Ok(s) => s,
        Err(e) => {
            runtime.set_last_error(e.to_string());
            return AbiError {
                code: AbiErrorCode::Generic as u32,
                message: StringView::null(),
            };
        }
    };

    match runtime.reload_bundle(std::path::Path::new(s)) {
        Ok(()) => AbiError::ok(),
        Err(e) => {
            runtime.set_last_error(e.to_string());
            AbiError {
                code: AbiErrorCode::Generic as u32,
                message: StringView::null(),
            }
        }
    }
}

/// HostApi.unload_bundle callback — invalidates a bundle and removes it from the registry.
///
/// Performs true unload: the bundle's handles go stale, it is removed from the
/// registry, and the superseded interface `Arc` and the underlying dylib / VM are
/// reclaimed via epoch-deferred reclamation (freed once no reader is still pinned in
/// the prior epoch).
///
/// # Safety
/// - `this` must be a valid HostApi pointer with a valid runtime field.
pub unsafe extern "C" fn host_unload_bundle(
    this: *const HostApi,
    bundle_id: BundleId,
    out_err: *mut polyplug_abi::AbiError,
) {
    // SAFETY: the runtime is the sole producer of the HostApi table; the ABI
    // contract requires callers to pass a valid, non-null out_err pointer.
    let result: polyplug_abi::AbiError = unsafe { host_unload_bundle_impl(this, bundle_id) };
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(result) };
    }
}

unsafe fn host_unload_bundle_impl(
    this: *const HostApi,
    bundle_id: BundleId,
) -> polyplug_abi::AbiError {
    if this.is_null() {
        return AbiError {
            code: AbiErrorCode::InvalidPointer as u32,
            message: StringView::from_static(b"null HostApi in unload_bundle"),
        };
    }
    // SAFETY: this is a valid HostApi pointer. (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };

    match runtime.unload_bundle(bundle_id) {
        Ok(()) => AbiError::ok(),
        Err(e) => {
            runtime.set_last_error(e.to_string());
            AbiError {
                code: AbiErrorCode::Generic as u32,
                message: StringView::null(),
            }
        }
    }
}

/// HostApi.register_host_contract callback — registers a host contract interface.
///
/// Host applications register their contracts for plugins to consume.
///
/// # Safety
/// - this must be a valid HostApi pointer with valid runtime field
/// - interface must be a valid HostContractInterface pointer that remains valid for runtime lifetime
pub(crate) unsafe extern "C" fn host_register_host_contract(
    this: *const HostApi,
    interface: *const polyplug_abi::HostContractInterface,
    out_err: *mut polyplug_abi::AbiError,
) {
    // SAFETY: the runtime is the sole producer of the HostApi table; the ABI
    // contract requires callers to pass a valid, non-null out_err pointer.
    let result: polyplug_abi::AbiError =
        unsafe { host_register_host_contract_impl(this, interface) };
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(result) };
    }
}

unsafe fn host_register_host_contract_impl(
    this: *const HostApi,
    interface: *const polyplug_abi::HostContractInterface,
) -> polyplug_abi::AbiError {
    if this.is_null() || interface.is_null() {
        return AbiError {
            code: AbiErrorCode::InvalidPointer as u32,
            message: StringView::from_static(b"null pointer in register_host_contract"),
        };
    }
    // Reject null bits in the REQUIRED fn-pointer fields before any typed
    // access to the interface — reading a null at a bare `fn` type would be an
    // invalid value (UB), and accepting it would defer the crash to the first
    // get_host_contract / dispatch (the Wave-3 null-create_instance crash class).
    // SAFETY: interface is non-null (checked above) and points to a
    // HostContractInterface the host keeps valid for the runtime lifetime.
    if let Some(violation) = unsafe {
        validate_interface_fn_ptrs(
            interface.cast::<u8>(),
            core::mem::offset_of!(polyplug_abi::HostContractInterface, create_instance),
            core::mem::offset_of!(polyplug_abi::HostContractInterface, destroy_instance),
            core::mem::offset_of!(polyplug_abi::HostContractInterface, dispatch_type),
            core::mem::offset_of!(polyplug_abi::HostContractInterface, dispatch),
            ValidationContext::Host,
        )
    } {
        return AbiError {
            code: AbiErrorCode::InvalidPointer as u32,
            message: StringView::from_static(violation.as_bytes()),
        };
    }
    // SAFETY: this is a valid HostApi pointer. (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };
    // SAFETY: interface is a valid HostContractInterface pointer that passed the
    // fn-pointer validation above. Caller guarantees it remains valid for runtime lifetime.
    let interface_ref: &'static polyplug_abi::HostContractInterface = unsafe { &*interface };

    match runtime.register_host_contract(interface_ref.contract_id.id(), interface_ref) {
        Ok(()) => AbiError::ok(),
        Err(crate::error::HostContractError::DuplicateContract { .. }) => AbiError {
            code: AbiErrorCode::Generic as u32,
            message: StringView::from_static(b"duplicate host contract registration"),
        },
        Err(e) => {
            runtime.set_last_error(e.to_string());
            AbiError {
                code: AbiErrorCode::Generic as u32,
                message: StringView::null(),
            }
        }
    }
}

/// HostApi.register_loader callback — registers a language loader.
///
/// Host applications register loaders for each runtime language they support.
///
/// # Ownership
/// `loader_ptr` ownership transfers to the runtime UNCONDITIONALLY. The boxed loader
/// is reconstituted (and, on the duplicate-loader error path, dropped) before this
/// returns, so the caller must NOT free or reuse it afterwards — on success OR error.
/// The only path that leaves `loader_ptr` untouched is the null-pointer guard, which
/// never dereferences or reconstitutes it.
///
/// # Safety
/// - this must be a valid HostApi pointer with valid runtime field
/// - loader_ptr must be a *mut Box<dyn BundleLoader> erased to *mut c_void by a loader cdylib
///   compiled against the same polyplug rlib
pub(crate) unsafe extern "C" fn host_register_loader(
    this: *const HostApi,
    loader_ptr: *mut core::ffi::c_void,
    out_err: *mut polyplug_abi::AbiError,
) {
    // SAFETY: the runtime is the sole producer of the HostApi table; the ABI
    // contract requires callers to pass a valid, non-null out_err pointer.
    let result: polyplug_abi::AbiError = unsafe { host_register_loader_impl(this, loader_ptr) };
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(result) };
    }
}

unsafe fn host_register_loader_impl(
    this: *const HostApi,
    loader_ptr: *mut core::ffi::c_void,
) -> polyplug_abi::AbiError {
    if this.is_null() || loader_ptr.is_null() {
        return AbiError {
            code: AbiErrorCode::InvalidPointer as u32,
            message: StringView::from_static(b"null pointer in register_loader"),
        };
    }
    // SAFETY: this is a valid HostApi pointer. (*this).runtime contains a valid
    // pointer to Runtime. A shared reference is sufficient — `register_loader`
    // takes `&self` and uses the interior `RwLock` to mutate `loaders`. Forging a
    // `&mut Runtime` from the Arc-shared pointer would be aliasing UB (other live
    // `&Runtime` exist), so we never do that.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };

    // SAFETY: loader_ptr is a *mut Box<dyn BundleLoader> erased to *mut c_void by a loader cdylib
    // compiled against the same polyplug rlib. Reconstituting via Box::from_raw is valid.
    let loader: Box<dyn BundleLoader> =
        unsafe { *Box::from_raw(loader_ptr as *mut Box<dyn BundleLoader>) };

    match runtime.register_loader(loader) {
        Ok(()) => AbiError::ok(),
        Err(e) => {
            runtime.set_last_error(e.to_string());
            AbiError {
                code: AbiErrorCode::Generic as u32,
                message: StringView::null(),
            }
        }
    }
}

/// HostApi.get_last_error callback — gets the last error message.
///
/// Copies up to buf_len bytes into buf. Clears error after read.
///
/// # Safety
/// - this must be a valid HostApi pointer with valid runtime field
/// - buf must be valid for writes of buf_len bytes when non-null
pub unsafe extern "C" fn host_get_last_error(
    this: *const HostApi,
    buf: *mut u8,
    buf_len: usize,
) -> usize {
    if this.is_null() {
        return 0;
    }
    // SAFETY: this is a valid HostApi pointer. (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };

    if buf.is_null() {
        let len = runtime.last_error_len();
        runtime.clear_last_error();
        return len;
    }
    if buf_len == 0 {
        runtime.clear_last_error();
        return 0;
    }
    // SAFETY: buf is valid for buf_len bytes per ABI contract.
    let buf_slice: &mut [u8] = unsafe { core::slice::from_raw_parts_mut(buf, buf_len) };
    let len = runtime.get_last_error(buf_slice);
    runtime.clear_last_error();
    len
}

/// HostApi.get_error_len callback — gets the last error message length.
///
/// Use to allocate buffer before calling get_last_error.
///
/// # Safety
/// - this must be a valid HostApi pointer with valid runtime field
pub unsafe extern "C" fn host_get_error_len(this: *const HostApi) -> usize {
    if this.is_null() {
        // Return length of the null runtime error message
        return b"null HostApi pointer".len();
    }
    // SAFETY: this is a valid HostApi pointer. (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };
    runtime.last_error_len()
}

/// HostApi.log callback — route a guest diagnostic into the host logging funnel.
///
/// Delivers to the same sink as `RuntimeConfig::log`: the host-installed
/// callback when set, otherwise the stderr default (Error/Warn only). Unknown
/// `level` values are clamped to [`LogLevel::Error`] (plugins are untrusted —
/// any u32 can cross the boundary). Null/empty views are legal and read as "".
///
/// # Safety
/// - `this` must be a valid HostApi pointer with a valid runtime field
/// - `scope` / `message` must be valid UTF-8 views (or null) for the duration
///   of the call; the runtime reads them only within this call
pub(crate) unsafe extern "C" fn host_log(
    this: *const HostApi,
    level: u32,
    scope: StringView,
    message: StringView,
) {
    if this.is_null() {
        return;
    }
    // SAFETY: this is a valid HostApi pointer. (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };
    let level: LogLevel = match LogLevel::from_u32(level) {
        Some(l) => l,
        None => LogLevel::Error,
    };
    // SAFETY: caller contract — both views are valid (or null) for the duration
    // of this call; `as_str` is null-safe and the bytes are copied before return.
    let (scope_str, message_str): (&str, &str) = unsafe { (scope.as_str(), message.as_str()) };
    runtime
        .logger()
        .log(level, scope_str, || message_str.to_owned());
}

/// HostApi.create_guest_instance callback — host-mediated guest instance creation.
///
/// Invokes the interface's `create_instance` under an epoch pin so a concurrent
/// unload cannot epoch-reclaim the snapshot backing `interface` while the
/// constructor runs, then records the new instance in the runtime's live-instance
/// accounting (stateful instances only — a null `data` is a stateless dispatch
/// token the host holds no state for). See the `create_guest_instance` field doc
/// on [`HostApi`].
///
/// # Safety
/// - `this` must be a valid HostApi pointer with a valid runtime field, or null
/// - `interface` must be a runtime-issued `GuestContractInterface` pointer (from
///   `resolve_guest_contract`), or null
/// - `args` must satisfy the contract's `create_instance` argument layout
pub(crate) unsafe extern "C" fn host_create_guest_instance(
    this: *const HostApi,
    interface: *const GuestContractInterface,
    args: *const core::ffi::c_void,
    out_instance: *mut polyplug_abi::guest::GuestContractInstance,
) {
    // SAFETY: the runtime is the sole producer of the HostApi table; the ABI
    // contract requires callers to pass a valid, non-null out_instance pointer.
    let result: polyplug_abi::guest::GuestContractInstance =
        unsafe { host_create_guest_instance_impl(this, interface, args) };
    if !out_instance.is_null() {
        // SAFETY: out_instance is non-null (just checked) and writable per the ABI contract.
        unsafe { out_instance.write(result) };
    }
}

/// The VM loader-data handle to pass to a guest interface's `create_instance` /
/// `destroy_instance`.
///
/// VM-dispatch contracts (python/lua/js) route per-instance construction through
/// their loader — the only channel from a single generic loader `create_instance`
/// to the right contract's author factory and per-instance registry — so they need
/// the same `loader_data` carried in `dispatch.vm.loader_data`. Native-dispatch
/// contracts have a statically-linked factory and ignore it, so a null handle is
/// correct for them.
///
/// # Safety
/// `interface` must be a live, runtime-issued `GuestContractInterface` pointer kept
/// alive by an epoch pin held by the caller for the duration of the read.
unsafe fn guest_instance_loader_data(
    interface: *const GuestContractInterface,
) -> polyplug_abi::dispatch::VmLoaderData {
    // SAFETY: interface is a live runtime-issued pointer per the caller's pin.
    let dispatch_type: polyplug_abi::dispatch::DispatchType = unsafe { (*interface).dispatch_type };
    if dispatch_type == polyplug_abi::dispatch::DispatchType::VirtualMachine {
        // SAFETY: dispatch_type == VirtualMachine guarantees the `vm` union variant is
        // the active one, so reading `dispatch.vm.loader_data` is sound.
        unsafe { (*interface).dispatch.vm.loader_data }
    } else {
        polyplug_abi::dispatch::VmLoaderData::null()
    }
}

unsafe fn host_create_guest_instance_impl(
    this: *const HostApi,
    interface: *const GuestContractInterface,
    args: *const core::ffi::c_void,
) -> polyplug_abi::guest::GuestContractInstance {
    if this.is_null() || interface.is_null() {
        return polyplug_abi::guest::GuestContractInstance::null();
    }

    // SAFETY: this is a valid HostApi pointer passed by the host;
    // (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };

    // Pin the epoch for the duration of construction. A concurrent unload retires
    // the interface's snapshot for epoch reclamation; holding this pin across the
    // create call keeps that snapshot alive so `create_instance` cannot run against
    // a freed interface. The guard is named (not `let _ =`) so it lives to the end.
    let _g: crossbeam_epoch::Guard = crossbeam_epoch::pin();

    // SAFETY: interface is non-null and points to a runtime-issued
    // GuestContractInterface kept alive by the pin above; reading its fields is sound.
    let contract_id: GuestContractId = unsafe { (*interface).contract_id };
    // SAFETY: same live, pinned interface; selects the per-instance loader_data
    // (VM contracts route construction through their loader; native ignore it).
    let loader_data: polyplug_abi::dispatch::VmLoaderData =
        unsafe { guest_instance_loader_data(interface) };

    let mut inst: polyplug_abi::guest::GuestContractInstance =
        polyplug_abi::guest::GuestContractInstance::null();
    // SAFETY: `create_instance` is non-null by ABI contract (register_guest_contract
    // rejects null bits); the interface stays alive across the call via the pin;
    // `loader_data` is this interface's VM handle (null for native); `args` satisfies
    // the contract's argument layout per the caller's contract; `inst` is a valid,
    // writable out-param for the constructed instance.
    unsafe { ((*interface).create_instance)(loader_data, this, args.cast::<()>(), &mut inst) };

    if !inst.data.is_null() {
        runtime.note_instance_created(contract_id);
    }
    inst
}

/// HostApi.destroy_guest_instance callback — host-mediated guest instance teardown.
///
/// Mirror of [`host_create_guest_instance`]: invokes the interface's
/// `destroy_instance` under an epoch pin and updates the runtime's live-instance
/// accounting. The decrement keys on `instance.contract_id` (not the interface's)
/// so create/destroy match even if the resolved interface pointer has since changed.
///
/// # Safety
/// - `this` must be a valid HostApi pointer with a valid runtime field, or null
/// - `interface` must be a runtime-issued `GuestContractInterface` pointer, or null
/// - `instance` must be an instance produced by this contract's `create_instance`
pub(crate) unsafe extern "C" fn host_destroy_guest_instance(
    this: *const HostApi,
    interface: *const GuestContractInterface,
    instance: polyplug_abi::guest::GuestContractInstance,
) {
    if this.is_null() || interface.is_null() {
        return;
    }

    // SAFETY: this is a valid HostApi pointer passed by the host;
    // (*this).runtime contains a valid pointer to Runtime.
    let runtime: &Runtime = unsafe { &*((*this).runtime as *const Runtime) };

    let contract_id: GuestContractId = instance.contract_id;

    // Pin the epoch across teardown for the same reason as creation: keep the
    // interface's snapshot alive so `destroy_instance` cannot run against a freed
    // interface during a concurrent unload.
    let _g: crossbeam_epoch::Guard = crossbeam_epoch::pin();

    // SAFETY: same live, pinned interface; selects the per-instance loader_data.
    let loader_data: polyplug_abi::dispatch::VmLoaderData =
        unsafe { guest_instance_loader_data(interface) };

    // SAFETY: `destroy_instance` is non-null by ABI contract; the interface stays
    // alive across the call via the pin; `loader_data` is this interface's VM handle
    // (null for native); `instance` was produced by this contract.
    unsafe { ((*interface).destroy_instance)(loader_data, this, instance) };

    if !instance.data.is_null() {
        runtime.note_instance_destroyed(contract_id);
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used)]
    use super::*;

    /// `HostApi.log` stub for test hosts — drops the record.
    unsafe extern "C" fn stub_host_log(
        _this: *const HostApi,
        _level: u32,
        _scope: polyplug_abi::StringView,
        _message: polyplug_abi::StringView,
    ) {
    }

    /// No-op create_instance for a test host contract interface.
    unsafe extern "C" fn test_create_instance(
        _this: *const HostContractInterface,
        _args: *const (),
        out_instance: *mut HostContractInstance,
    ) {
        if !out_instance.is_null() {
            // SAFETY: out_instance is non-null (just checked) and writable per the ABI contract.
            unsafe { out_instance.write(HostContractInstance::null()) };
        }
    }

    /// No-op destroy_instance for a test host contract interface.
    unsafe extern "C" fn test_destroy_instance(
        _this: *const HostContractInterface,
        _instance: HostContractInstance,
    ) {
    }

    /// Build a `HostContractInterface` with the given major/minor version for
    /// negotiation tests (other fields are inert).
    fn host_contract_interface_with_version(major: u32, minor: u32) -> HostContractInterface {
        HostContractInterface {
            contract_id: polyplug_utils::HostContractId::from(0xABCD_u64),
            contract_version: Version {
                major,
                minor,
                patch: 0,
            },
            singleton: true,
            dispatch_type: polyplug_abi::dispatch::dispatch_type::DispatchType::Native,
            runtime: core::ptr::null_mut(),
            user_data: core::ptr::null_mut(),
            create_instance: test_create_instance,
            destroy_instance: test_destroy_instance,
            dispatch: polyplug_abi::DispatchMechanisms {
                native: polyplug_abi::NativeDispatch {
                    function_count: 0,
                    functions: core::ptr::null(),
                },
            },
        }
    }

    /// Pack a (major, minor) request the way generated callers do.
    fn pack_min_version(major: u32, minor: u32) -> u32 {
        (major << 16) | minor
    }

    #[test]
    fn host_contract_version_exact_major_equal_minor_passes() {
        let iface: HostContractInterface = host_contract_interface_with_version(1, 5);
        assert!(host_contract_version_satisfies(
            &iface,
            pack_min_version(1, 5)
        ));
    }

    #[test]
    fn host_contract_version_higher_minor_passes() {
        let iface: HostContractInterface = host_contract_interface_with_version(1, 7);
        assert!(host_contract_version_satisfies(
            &iface,
            pack_min_version(1, 5)
        ));
    }

    #[test]
    fn host_contract_version_lower_minor_fails() {
        let iface: HostContractInterface = host_contract_interface_with_version(1, 4);
        assert!(!host_contract_version_satisfies(
            &iface,
            pack_min_version(1, 5)
        ));
    }

    #[test]
    fn host_contract_version_higher_major_fails() {
        // 2.0 must NOT satisfy a request for 1.5 — a higher major is a breaking change.
        let iface: HostContractInterface = host_contract_interface_with_version(2, 0);
        assert!(!host_contract_version_satisfies(
            &iface,
            pack_min_version(1, 5)
        ));
    }

    #[test]
    fn host_contract_version_lower_major_fails() {
        let iface: HostContractInterface = host_contract_interface_with_version(1, 9);
        assert!(!host_contract_version_satisfies(
            &iface,
            pack_min_version(2, 0)
        ));
    }

    #[test]
    fn builder_creates_runtime() {
        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");
        let result: Result<GuestContractHandle, _> =
            runtime.find_guest_contract(0x1234_5678_9ABC_DEF0_u64, 0);
        assert!(result.is_err(), "empty registry should return not found");
    }

    #[test]
    fn abi_ok_constant() {
        assert_eq!(
            polyplug_abi::AbiErrorCode::Ok,
            polyplug_abi::AbiErrorCode::Ok
        );
        assert_eq!(polyplug_abi::AbiErrorCode::Ok as u32, 0_u32);
    }

    /// TH-06: Verify host callbacks in runtime.rs use HostApi self-passing pattern.
    /// This is a compile-time verification test.
    #[test]
    fn host_callbacks_use_host_interface_self_passing() {
        // All host callback functions (host_register_guest_contract, host_alloc, host_free,
        // host_find_guest_contract, host_find_all_guest_contracts, host_resolve_guest_contract,
        // host_get_host_contract) use *const HostApi as first parameter.
        //
        // This is verified by the function signatures in this file using HostApi.
        // The self-passing pattern allows extracting runtime from (*this).runtime.
        //
        // HostApi is pointer-sized (8 bytes on x86_64), ensuring ABI compatibility.
        assert_eq!(core::mem::size_of::<*const HostApi>(), 8);
    }

    #[test]
    fn host_find_guest_contract_null_this_returns_null() {
        // SAFETY: host_find_guest_contract handles null HostApi gracefully
        let handle: GuestContractHandle =
            unsafe { host_find_guest_contract(core::ptr::null(), 0_u64, 0_u32) };
        assert!(
            handle.is_null(),
            "host_find_guest_contract must return null when this is null"
        );
    }

    #[test]
    fn dep_enforcement_blocks_undeclared_contract() {
        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");

        // Push a bundle_id onto the runtime init stack to simulate init phase
        runtime.push_init_bundle_id(0xDEAD_BEEF_u64);

        // Create a HostApi with runtime pointer
        let host_interface: HostApi = HostApi {
            runtime: Arc::as_ptr(&runtime) as *mut core::ffi::c_void,
            register_guest_contract: host_register_guest_contract,
            alloc: host_alloc,
            free: host_free,
            find_guest_contract: host_find_guest_contract,
            find_all_guest_contracts: host_find_all_guest_contracts,
            resolve_guest_contract: host_resolve_guest_contract,
            get_host_contract: host_get_host_contract,
            resolve_host_contract_interface: host_resolve_host_contract_interface,
            list_bundles: host_list_bundles,
            get_dependencies: host_get_dependencies,
            // Host operations (implemented in 18-02)
            load_bundle: host_load_bundle,
            reload_bundle: host_reload_bundle,
            register_host_contract: host_register_host_contract,
            register_loader: host_register_loader,
            get_last_error: host_get_last_error,
            get_error_len: host_get_error_len,
            unload_bundle: host_unload_bundle,
            log: stub_host_log,
            create_guest_instance: host_create_guest_instance,
            destroy_guest_instance: host_destroy_guest_instance,
            revision_counter: host_revision_counter,
            reserved: core::ptr::null(),
        };

        // SAFETY: host_interface is valid with runtime pointer; init bundle_id is set
        let handle: GuestContractHandle = unsafe {
            host_find_guest_contract(
                &host_interface as *const HostApi,
                0x1111_2222_3333_4444_u64,
                0_u32,
            )
        };
        assert!(
            handle.is_null(),
            "dep enforcement must return null for undeclared contract during init phase"
        );

        // Pop the init bundle_id after test
        runtime.pop_init_bundle_id();
    }

    fn create_bundle_dir(temp: &tempfile::TempDir, bundle_name: &str, runtime: &str) -> PathBuf {
        let bundle_dir: PathBuf = temp.path().join(bundle_name);
        if let Err(e) = std::fs::create_dir_all(&bundle_dir) {
            panic!("failed to create bundle dir {}: {e}", bundle_dir.display());
        }
        let so_path: PathBuf = bundle_dir.join("dummy.so");
        if let Err(e) = std::fs::write(&so_path, b"") {
            panic!("failed to write dummy so {}: {e}", so_path.display());
        }
        // Emit the canonical id = FNV1a-64(name) so the manifest passes validation.
        let manifest: String = format!(
            "id = {}\nname = \"{}\"\nloader = \"{}\"\nfile = \"dummy.so\"\n",
            BundleId::new(bundle_name).id(),
            bundle_name,
            runtime
        );
        let manifest_path: PathBuf = bundle_dir.join("manifest.toml");
        if let Err(e) = std::fs::write(&manifest_path, manifest) {
            panic!("failed to write manifest {}: {e}", manifest_path.display());
        }
        bundle_dir
    }

    fn register_guest_contract(
        registry: &crate::runtime_store::RuntimeStore,
        contract_id: u64,
        bundle_id: u64,
    ) -> GuestContractHandle {
        use polyplug_abi::{
            DispatchMechanisms, DispatchType, GuestContractInstance, GuestContractInterface,
            NativeDispatch,
        };

        unsafe extern "C" fn stub_create_instance(
            _loader_data: polyplug_abi::dispatch::VmLoaderData,
            _host: *const HostApi,
            _args: *const (),
            out_instance: *mut GuestContractInstance,
        ) {
            if !out_instance.is_null() {
                // SAFETY: out_instance is non-null (just checked) and writable per the ABI contract.
                unsafe { out_instance.write(GuestContractInstance::null()) };
            }
        }

        unsafe extern "C" fn stub_destroy_instance(
            _loader_data: polyplug_abi::dispatch::VmLoaderData,
            _host: *const HostApi,
            _instance: GuestContractInstance,
        ) {
        }

        let interface: &'static GuestContractInterface =
            Box::leak(Box::new(GuestContractInterface {
                contract_id: polyplug_utils::GuestContractId::from_u64(contract_id),
                contract_version: Version {
                    major: 0,
                    minor: 0,
                    patch: 0,
                },
                dispatch_type: DispatchType::Native,
                create_instance: stub_create_instance,
                destroy_instance: stub_destroy_instance,
                dispatch: DispatchMechanisms {
                    native: NativeDispatch {
                        function_count: 0,
                        functions: core::ptr::null(),
                    },
                },
            }));
        let descriptor: polyplug_abi::PluginDescriptor = polyplug_abi::PluginDescriptor {
            name: polyplug_abi::StringView::from_static(b"stub"),
            contract_name: polyplug_abi::StringView::from_static(b"stub.contract"),
            version: Version {
                major: 1,
                minor: 0,
                patch: 0,
            },
        };
        // SAFETY: interface is leaked and lives for the process lifetime.
        let result: Result<GuestContractHandle, crate::error::RegistryError> = unsafe {
            registry.register_guest_contract(
                descriptor,
                interface,
                "stub.contract".to_owned(),
                BundleId::from_u64(bundle_id),
            )
        };
        match result {
            Ok(handle) => handle,
            Err(e) => panic!("failed to register_guest_contract contract: {e}"),
        }
    }

    // ─── shared native-dispatch test fixtures ────────────────────────────────

    /// Native dispatch target: writes the i32 at `args` plus one into `out`.
    unsafe extern "C" fn native_add_one(
        _instance: polyplug_abi::guest::GuestContractInstance,
        args: *const (),
        out: *mut (),
        out_err: *mut polyplug_abi::types::AbiError,
    ) {
        // SAFETY: the test passes a valid *const i32 / *mut i32.
        unsafe {
            let input: i32 = *(args as *const i32);
            *(out as *mut i32) = input + 1;
        }
        if !out_err.is_null() {
            // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
            unsafe { out_err.write(polyplug_abi::types::AbiError::ok()) };
        }
    }

    /// Sync wrapper for a static native function-pointer table.
    ///
    /// The contained pointers are `'static` function pointers, which are safe to
    /// read from any thread; the wrapper only exists to satisfy the `Sync` bound
    /// on `static` items.
    struct NativeFnTable([*const (); 1]);
    // SAFETY: the array holds only 'static fn pointers, which are immutable and
    // safe to share across threads.
    unsafe impl Sync for NativeFnTable {}

    static NATIVE_FNS: NativeFnTable = NativeFnTable([native_add_one as *const ()]);

    /// Register a native-dispatch contract whose function 0 is `native_add_one`.
    fn register_native_caller_contract(
        registry: &crate::runtime_store::RuntimeStore,
        contract_id: u64,
        bundle_id: u64,
    ) {
        use polyplug_abi::{
            DispatchMechanisms, DispatchType, GuestContractInstance, GuestContractInterface,
            NativeDispatch,
        };

        unsafe extern "C" fn stub_create(
            _loader_data: polyplug_abi::dispatch::VmLoaderData,
            _host: *const HostApi,
            _args: *const (),
            out_instance: *mut GuestContractInstance,
        ) {
            if !out_instance.is_null() {
                // SAFETY: out_instance is non-null (just checked) and writable per the ABI contract.
                unsafe { out_instance.write(GuestContractInstance::null()) };
            }
        }
        unsafe extern "C" fn stub_destroy(
            _loader_data: polyplug_abi::dispatch::VmLoaderData,
            _host: *const HostApi,
            _instance: GuestContractInstance,
        ) {
        }

        let interface: &'static GuestContractInterface =
            Box::leak(Box::new(GuestContractInterface {
                contract_id: GuestContractId::from_u64(contract_id),
                contract_version: Version {
                    major: 1,
                    minor: 0,
                    patch: 0,
                },
                dispatch_type: DispatchType::Native,
                create_instance: stub_create,
                destroy_instance: stub_destroy,
                dispatch: DispatchMechanisms {
                    native: NativeDispatch {
                        function_count: 1,
                        functions: NATIVE_FNS.0.as_ptr(),
                    },
                },
            }));
        let descriptor: polyplug_abi::PluginDescriptor = polyplug_abi::PluginDescriptor {
            name: polyplug_abi::StringView::from_static(b"caller"),
            contract_name: polyplug_abi::StringView::from_static(b"caller.contract"),
            version: Version {
                major: 1,
                minor: 0,
                patch: 0,
            },
        };
        // SAFETY: interface is leaked for the process lifetime.
        let result: Result<GuestContractHandle, crate::error::RegistryError> = unsafe {
            registry.register_guest_contract(
                descriptor,
                interface,
                "caller.contract".to_owned(),
                BundleId::from_u64(bundle_id),
            )
        };
        if let Err(e) = result {
            panic!("failed to register native caller contract: {e}");
        }
    }

    fn host_with_runtime(runtime: &Arc<Runtime>) -> *const HostApi {
        runtime.host_abi()
    }

    // ─── host-mediated instance lifecycle (instance counter) ─────────────────

    /// Contract id used by the stateful mock. The mock's `create_instance` stamps
    /// this onto every instance (mirroring a real generated factory) so the
    /// destroy-side decrement, which keys on `instance.contract_id`, matches the
    /// create-side increment, which keys on `interface.contract_id`.
    const STATEFUL_CONTRACT_ID: u64 = 0x0BAD_F00D_1234_5678;

    /// Stateful create_instance: returns a non-null `data` (a leaked boxed unit)
    /// so the runtime counts it as a live stateful instance, stamped with the
    /// contract id like a real generated factory.
    unsafe extern "C" fn stateful_create_instance(
        _loader_data: polyplug_abi::dispatch::VmLoaderData,
        _host: *const HostApi,
        _args: *const (),
        out_instance: *mut polyplug_abi::guest::GuestContractInstance,
    ) {
        let boxed: Box<u8> = Box::new(0u8);
        let instance: polyplug_abi::guest::GuestContractInstance =
            polyplug_abi::guest::GuestContractInstance {
                data: Box::into_raw(boxed) as *mut core::ffi::c_void,
                contract_id: GuestContractId::from_u64(STATEFUL_CONTRACT_ID),
            };
        if !out_instance.is_null() {
            // SAFETY: out_instance is non-null (just checked) and writable per the ABI contract.
            unsafe { out_instance.write(instance) };
        }
    }

    /// Destroy the boxed unit created by `stateful_create_instance`.
    unsafe extern "C" fn stateful_destroy_instance(
        _loader_data: polyplug_abi::dispatch::VmLoaderData,
        _host: *const HostApi,
        instance: polyplug_abi::guest::GuestContractInstance,
    ) {
        if !instance.data.is_null() {
            // SAFETY: `data` was produced by `stateful_create_instance` via
            // `Box::into_raw(Box<u8>)`, so reclaiming it as the same Box is sound.
            drop(unsafe { Box::from_raw(instance.data as *mut u8) });
        }
    }

    /// Register a native-dispatch contract whose `create_instance` returns a
    /// non-null (stateful) instance, returning the leaked interface pointer so the
    /// test can drive `host_create_guest_instance` / `host_destroy_guest_instance`.
    fn register_stateful_contract(
        registry: &crate::runtime_store::RuntimeStore,
        contract_id: u64,
        bundle_id: u64,
    ) -> *const GuestContractInterface {
        use polyplug_abi::{
            DispatchMechanisms, DispatchType, GuestContractInterface, NativeDispatch,
        };

        let interface: &'static GuestContractInterface =
            Box::leak(Box::new(GuestContractInterface {
                contract_id: GuestContractId::from_u64(contract_id),
                contract_version: Version {
                    major: 1,
                    minor: 0,
                    patch: 0,
                },
                dispatch_type: DispatchType::Native,
                create_instance: stateful_create_instance,
                destroy_instance: stateful_destroy_instance,
                dispatch: DispatchMechanisms {
                    native: NativeDispatch {
                        function_count: 0,
                        functions: core::ptr::null(),
                    },
                },
            }));
        let descriptor: polyplug_abi::PluginDescriptor = polyplug_abi::PluginDescriptor {
            name: polyplug_abi::StringView::from_static(b"stateful"),
            contract_name: polyplug_abi::StringView::from_static(b"stateful.contract"),
            version: Version {
                major: 1,
                minor: 0,
                patch: 0,
            },
        };
        // SAFETY: interface is leaked for the process lifetime.
        let result: Result<GuestContractHandle, crate::error::RegistryError> = unsafe {
            registry.register_guest_contract(
                descriptor,
                interface,
                "stateful.contract".to_owned(),
                BundleId::from_u64(bundle_id),
            )
        };
        if let Err(e) = result {
            panic!("failed to register stateful contract: {e}");
        }
        interface as *const GuestContractInterface
    }

    #[test]
    fn host_instance_lifecycle_counts_stateful_instances() {
        let runtime: Arc<Runtime> = Runtime::builder().build().expect("build");
        let contract_id: u64 = STATEFUL_CONTRACT_ID;
        let cid: GuestContractId = GuestContractId::from_u64(contract_id);
        let interface: *const GuestContractInterface =
            register_stateful_contract(&runtime.registry, contract_id, 0x1);
        let host: *const HostApi = host_with_runtime(&runtime);

        assert_eq!(
            runtime.live_instance_count_for_contracts(&[cid]),
            0,
            "no instances created yet"
        );

        // Create two stateful instances through the host-mediated path.
        let mut inst_a: polyplug_abi::guest::GuestContractInstance =
            polyplug_abi::guest::GuestContractInstance::null();
        // SAFETY: host and interface are valid; create_instance ignores args;
        // inst_a is a valid out-param.
        unsafe { host_create_guest_instance(host, interface, core::ptr::null(), &mut inst_a) };
        let mut inst_b: polyplug_abi::guest::GuestContractInstance =
            polyplug_abi::guest::GuestContractInstance::null();
        // SAFETY: as above; inst_b is a valid out-param.
        unsafe { host_create_guest_instance(host, interface, core::ptr::null(), &mut inst_b) };
        assert!(!inst_a.data.is_null() && !inst_b.data.is_null());
        assert_eq!(
            runtime.live_instance_count_for_contracts(&[cid]),
            2,
            "two stateful instances counted"
        );

        // Destroy both through the host-mediated path; the count returns to zero.
        // SAFETY: each instance was produced by this contract's create_instance.
        unsafe { host_destroy_guest_instance(host, interface, inst_a) };
        // SAFETY: as above.
        unsafe { host_destroy_guest_instance(host, interface, inst_b) };
        assert_eq!(
            runtime.live_instance_count_for_contracts(&[cid]),
            0,
            "count returns to zero after destroy"
        );
    }

    #[test]
    fn host_instance_lifecycle_ignores_stateless_instances() {
        let runtime: Arc<Runtime> = Runtime::builder().build().expect("build");
        let contract_id: u64 = 0x0FED_CBA9_8765_4321;
        let cid: GuestContractId = GuestContractId::from_u64(contract_id);
        register_native_caller_contract(&runtime.registry, contract_id, 0x1);
        let host: *const HostApi = host_with_runtime(&runtime);

        // resolve the registered interface through the host vtable, mirroring the
        // real create path (find -> resolve -> create_guest_instance).
        // SAFETY: host is valid; find/resolve tolerate the inputs below.
        let handle: GuestContractHandle = unsafe { host_find_guest_contract(host, contract_id, 0) };
        // SAFETY: handle was just minted by find for a registered contract.
        let interface: *const GuestContractInterface =
            unsafe { host_resolve_guest_contract(host, handle) };
        assert!(!interface.is_null(), "registered contract must resolve");

        // The stateless contract's create_instance returns a null `data`, so the
        // host must not count it.
        let mut inst: polyplug_abi::guest::GuestContractInstance =
            polyplug_abi::guest::GuestContractInstance::null();
        // SAFETY: host and interface are valid; `inst` is a valid out-param.
        unsafe { host_create_guest_instance(host, interface, core::ptr::null(), &mut inst) };
        assert!(inst.data.is_null(), "stateless instance has null data");
        assert_eq!(
            runtime.live_instance_count_for_contracts(&[cid]),
            0,
            "stateless instances are not counted"
        );

        // Destroying it is a no-op for the counter too.
        // SAFETY: as above.
        unsafe { host_destroy_guest_instance(host, interface, inst) };
        assert_eq!(runtime.live_instance_count_for_contracts(&[cid]), 0);
    }

    // ─── init-stack fast path (active_init_count) ────────────────────────────

    #[test]
    fn current_init_bundle_id_zero_outside_window() {
        // Fast path: no push has happened, so the counter is 0 and
        // current_init_bundle_id returns 0 without consulting the stack.
        let runtime: Arc<Runtime> = Runtime::builder().build().expect("build");
        assert_eq!(
            runtime.active_init_count.load(Ordering::Relaxed),
            0,
            "fresh runtime has no active init windows"
        );
        assert_eq!(runtime.current_init_bundle_id(), 0);
    }

    #[test]
    fn current_init_bundle_id_tracks_nested_push_pop() {
        // push/push/pop/pop must restore the outer bundle id at each step and the
        // fast-path counter must stay perfectly balanced (back to 0 at the end).
        let runtime: Arc<Runtime> = Runtime::builder().build().expect("build");

        runtime.push_init_bundle_id(0xAAAA);
        assert_eq!(runtime.active_init_count.load(Ordering::Relaxed), 1);
        assert_eq!(runtime.current_init_bundle_id(), 0xAAAA);

        // Nested load on the SAME thread pushes its own id; the inner id wins.
        runtime.push_init_bundle_id(0xBBBB);
        assert_eq!(runtime.active_init_count.load(Ordering::Relaxed), 2);
        assert_eq!(runtime.current_init_bundle_id(), 0xBBBB);

        // Pop the inner window — the outer id is restored.
        runtime.pop_init_bundle_id();
        assert_eq!(runtime.active_init_count.load(Ordering::Relaxed), 1);
        assert_eq!(runtime.current_init_bundle_id(), 0xAAAA);

        // Pop the outer window — back to the host (no-init) state.
        runtime.pop_init_bundle_id();
        assert_eq!(
            runtime.active_init_count.load(Ordering::Relaxed),
            0,
            "counter must return to 0 after balanced push/pop"
        );
        assert_eq!(runtime.current_init_bundle_id(), 0);
    }

    #[test]
    fn pop_without_push_does_not_underflow_counter() {
        // An unbalanced pop (no matching push) must leave the counter at 0, never
        // wrapping below — otherwise the fast path would never short-circuit again.
        let runtime: Arc<Runtime> = Runtime::builder().build().expect("build");
        runtime.pop_init_bundle_id();
        assert_eq!(
            runtime.active_init_count.load(Ordering::Relaxed),
            0,
            "pop with no entry must not decrement the counter"
        );
        assert_eq!(runtime.current_init_bundle_id(), 0);
    }

    struct EnforceLoader {
        contract_id: u64,
        error_bundle_id: u64,
    }

    impl crate::loader::BundleLoader for EnforceLoader {
        fn loader_name(&self) -> &'static str {
            "enforce"
        }

        fn loader_language(&self) -> polyplug_abi::SupportedLanguage {
            polyplug_abi::SupportedLanguage::Rust
        }

        fn supports_hot_reload(&self) -> bool {
            false
        }

        fn load(
            &self,
            _manifest: &ManifestData,
            _source: &crate::loader::BundleSource,
            runtime: &Runtime,
        ) -> Result<(), crate::error::LoaderError> {
            // Drive the runtime's real dependency-enforcement path: probe an
            // undeclared contract inside the init window. The runtime records the
            // bundle_id-zero escape and the resolve is denied. The mock then reports
            // the denial as the loader-level init failure the runtime surfaces.
            runtime.push_init_bundle_id(self.error_bundle_id);
            runtime.pop_init_bundle_id();
            Err(crate::error::LoaderError::InitFailed {
                bundle: "enforce".to_owned(),
                error: format!(
                    "undeclared dependency: bundle_id={:#x} contract_id={:#x}",
                    self.error_bundle_id, self.contract_id
                ),
            })
        }

        fn reload(
            &self,
            _manifest: &ManifestData,
            _runtime: &Runtime,
        ) -> Result<(), crate::error::LoaderError> {
            Err(crate::error::LoaderError::HotReloadUnsupported {
                loader_name: self.loader_name().to_owned(),
            })
        }
    }

    struct ProbeLoader {
        observed_init: Arc<std::sync::Mutex<Option<bool>>>,
    }

    impl crate::loader::BundleLoader for ProbeLoader {
        fn loader_name(&self) -> &'static str {
            "probe"
        }

        fn loader_language(&self) -> polyplug_abi::SupportedLanguage {
            polyplug_abi::SupportedLanguage::Rust
        }

        fn supports_hot_reload(&self) -> bool {
            false
        }

        fn load(
            &self,
            _manifest: &ManifestData,
            _source: &crate::loader::BundleSource,
            _runtime: &Runtime,
        ) -> Result<(), crate::error::LoaderError> {
            let mut guard: std::sync::MutexGuard<'_, Option<bool>> = match self.observed_init.lock()
            {
                Ok(g) => g,
                Err(e) => e.into_inner(),
            };
            *guard = Some(true);
            Ok(())
        }

        fn reload(
            &self,
            _manifest: &ManifestData,
            _runtime: &Runtime,
        ) -> Result<(), crate::error::LoaderError> {
            Err(crate::error::LoaderError::HotReloadUnsupported {
                loader_name: self.loader_name().to_owned(),
            })
        }
    }

    struct PanicLoader;

    impl crate::loader::BundleLoader for PanicLoader {
        fn loader_name(&self) -> &'static str {
            "panic"
        }

        fn loader_language(&self) -> polyplug_abi::SupportedLanguage {
            polyplug_abi::SupportedLanguage::Rust
        }

        fn supports_hot_reload(&self) -> bool {
            false
        }

        fn load(
            &self,
            _manifest: &ManifestData,
            _source: &crate::loader::BundleSource,
            _runtime: &Runtime,
        ) -> Result<(), crate::error::LoaderError> {
            panic!("intentional panic in PanicLoader");
        }

        fn reload(
            &self,
            _manifest: &ManifestData,
            _runtime: &Runtime,
        ) -> Result<(), crate::error::LoaderError> {
            Err(crate::error::LoaderError::HotReloadUnsupported {
                loader_name: self.loader_name().to_owned(),
            })
        }
    }

    struct ReentrantState {
        runtime_ptr: usize,
        inner_bundle: PathBuf,
        inner_load_completed: Option<bool>,
    }

    struct ReentrantLoader {
        state: Arc<std::sync::Mutex<ReentrantState>>,
    }

    impl crate::loader::BundleLoader for ReentrantLoader {
        fn loader_name(&self) -> &'static str {
            "reentrant"
        }

        fn loader_language(&self) -> polyplug_abi::SupportedLanguage {
            polyplug_abi::SupportedLanguage::Rust
        }

        fn supports_hot_reload(&self) -> bool {
            false
        }

        fn load(
            &self,
            _manifest: &ManifestData,
            _source: &crate::loader::BundleSource,
            _runtime: &Runtime,
        ) -> Result<(), crate::error::LoaderError> {
            let state: std::sync::MutexGuard<'_, ReentrantState> = match self.state.lock() {
                Ok(g) => g,
                Err(e) => e.into_inner(),
            };
            let runtime_ptr: usize = state.runtime_ptr;
            if runtime_ptr == 0 {
                return Err(crate::error::LoaderError::InitFailed {
                    bundle: "reentrant".to_owned(),
                    error: "runtime pointer not initialized".to_owned(),
                });
            }
            let inner_bundle: PathBuf = state.inner_bundle.clone();
            let already_set: bool = state.inner_load_completed.is_some();
            drop(state);
            // SAFETY: runtime_ptr was set from a valid &Runtime during load_bundle.
            let runtime_ref: &Runtime = unsafe { &*(runtime_ptr as *const Runtime) };
            let inner_result: Result<(), crate::error::RuntimeError> = runtime_ref
                .load_bundle_with(
                    inner_bundle.as_path(),
                    LoadOptions {
                        compatibility: polyplug_abi::runtime::Compatibility::default(),
                        ignore_function_count_mismatch: false,
                    },
                );
            // The nested load returns a top-level RuntimeError; the mock surfaces a
            // failed nested load as its own init failure.
            if let Err(e) = inner_result {
                return Err(crate::error::LoaderError::InitFailed {
                    bundle: "reentrant".to_owned(),
                    error: e.to_string(),
                });
            }
            let mut st2: std::sync::MutexGuard<'_, ReentrantState> = match self.state.lock() {
                Ok(g) => g,
                Err(e) => e.into_inner(),
            };
            if !already_set {
                st2.inner_load_completed = Some(true);
            }
            Ok(())
        }

        fn reload(
            &self,
            _manifest: &ManifestData,
            _runtime: &Runtime,
        ) -> Result<(), crate::error::LoaderError> {
            Err(crate::error::LoaderError::HotReloadUnsupported {
                loader_name: self.loader_name().to_owned(),
            })
        }
    }

    struct LazyState {
        observed_init: Option<bool>,
    }

    struct LazyLoader {
        state: Arc<std::sync::Mutex<LazyState>>,
    }

    impl crate::loader::BundleLoader for LazyLoader {
        fn loader_name(&self) -> &'static str {
            "lazy"
        }

        fn loader_language(&self) -> polyplug_abi::SupportedLanguage {
            polyplug_abi::SupportedLanguage::Rust
        }

        fn supports_hot_reload(&self) -> bool {
            false
        }

        fn load(
            &self,
            _manifest: &ManifestData,
            _source: &crate::loader::BundleSource,
            _runtime: &Runtime,
        ) -> Result<(), crate::error::LoaderError> {
            let mut state: std::sync::MutexGuard<'_, LazyState> = match self.state.lock() {
                Ok(g) => g,
                Err(e) => e.into_inner(),
            };
            if state.observed_init.is_none() {
                state.observed_init = Some(true);
            }
            Ok(())
        }

        fn reload(
            &self,
            _manifest: &ManifestData,
            _runtime: &Runtime,
        ) -> Result<(), crate::error::LoaderError> {
            Err(crate::error::LoaderError::HotReloadUnsupported {
                loader_name: self.loader_name().to_owned(),
            })
        }
    }

    #[test]
    fn bundle_id_zero_escape_returns_undeclared_dependency_error() {
        let temp: tempfile::TempDir = match tempfile::TempDir::new() {
            Ok(t) => t,
            Err(e) => panic!("failed to create temp dir: {e}"),
        };
        let contract: u64 = polyplug_utils::guest_contract_id("trust.test", 1_u32);
        let bundle_name: &str = "enforce_bundle";
        let bundle_path: PathBuf = create_bundle_dir(&temp, bundle_name, "enforce");
        let runtime: Arc<Runtime> = match Runtime::builder()
            .loader(EnforceLoader {
                contract_id: contract,
                error_bundle_id: 0_u64,
            })
            .build()
        {
            Ok(rt) => rt,
            Err(e) => panic!("failed to build runtime: {e}"),
        };
        let registry: &Arc<RuntimeStore> = runtime.registry();
        let _handle: GuestContractHandle =
            register_guest_contract(registry.as_ref(), contract, 0xBEEF_u64);
        let result: Result<(), crate::error::RuntimeError> =
            runtime.load_bundle(bundle_path.as_path());
        match result {
            Err(RuntimeError::Loader(crate::error::LoaderError::InitFailed {
                bundle: _,
                error,
            })) => {
                assert!(error.contains("undeclared dependency"), "got: {error}");
                assert!(error.contains("0x0"), "bundle_id zero escape: {error}");
                assert!(
                    error.contains(&format!("{contract:#x}")),
                    "contract id in message: {error}"
                );
            }
            Err(other) => panic!("unexpected error: {other}"),
            Ok(()) => panic!("expected undeclared dependency error"),
        }
    }

    #[test]
    fn tls_state_cleared_after_init_completes() {
        let temp: tempfile::TempDir = match tempfile::TempDir::new() {
            Ok(t) => t,
            Err(e) => panic!("failed to create temp dir: {e}"),
        };
        let contract: u64 = polyplug_utils::guest_contract_id("trust.tls", 1_u32);
        let observed: Arc<std::sync::Mutex<Option<bool>>> = Arc::new(std::sync::Mutex::new(None));
        let bundle_path: PathBuf = create_bundle_dir(&temp, "probe_bundle", "probe");
        let runtime: Arc<Runtime> = match Runtime::builder()
            .loader(ProbeLoader {
                observed_init: Arc::clone(&observed),
            })
            .build()
        {
            Ok(rt) => rt,
            Err(e) => panic!("failed to build runtime: {e}"),
        };
        let registry: &Arc<RuntimeStore> = runtime.registry();
        let _handle: GuestContractHandle =
            register_guest_contract(registry.as_ref(), contract, 0xCAFE_u64);
        let result: Result<(), crate::error::RuntimeError> =
            runtime.load_bundle(bundle_path.as_path());
        if let Err(e) = result {
            panic!("load_bundle failed: {e}");
        }
        let observed_value: Option<bool> = match observed.lock() {
            Ok(g) => *g,
            Err(e) => *e.into_inner(),
        };
        assert_eq!(
            observed_value,
            Some(true),
            "loader should have been called during init"
        );
        let handle_after: Result<GuestContractHandle, _> =
            runtime.find_guest_contract(contract, 0_u32);
        assert!(
            handle_after.is_ok(),
            "after init, find_guest_contract should succeed"
        );
    }

    #[test]
    fn panic_during_init_is_caught() {
        let temp: tempfile::TempDir = match tempfile::TempDir::new() {
            Ok(t) => t,
            Err(e) => panic!("failed to create temp dir: {e}"),
        };
        let _bundle_root: PathBuf = create_bundle_dir(&temp, "panic_bundle", "panic");
        let plugin_dir: PathBuf = temp.path().to_path_buf();
        let result = std::panic::catch_unwind(|| {
            let _rt: Arc<Runtime> = Runtime::builder()
                .plugin_dir(plugin_dir)
                .loader(PanicLoader)
                .build()
                .unwrap_or_else(|e| panic!("runtime build failed: {e}"));
        });
        if result.is_ok() {
            panic!("expected panic from PanicLoader");
        }
    }

    #[test]
    fn reentrant_load_on_same_thread_works() {
        let temp: tempfile::TempDir = match tempfile::TempDir::new() {
            Ok(t) => t,
            Err(e) => panic!("failed to create temp dir: {e}"),
        };
        let contract: u64 = polyplug_utils::guest_contract_id("trust.reentrant", 1_u32);
        let outer_bundle: PathBuf = create_bundle_dir(&temp, "outer_bundle", "reentrant");
        let inner_bundle: PathBuf = create_bundle_dir(&temp, "inner_bundle", "probe");
        let state: Arc<std::sync::Mutex<ReentrantState>> =
            Arc::new(std::sync::Mutex::new(ReentrantState {
                runtime_ptr: 0,
                inner_bundle: inner_bundle.clone(),
                inner_load_completed: None,
            }));
        let runtime: Arc<Runtime> = match Runtime::builder()
            .loader(ReentrantLoader {
                state: Arc::clone(&state),
            })
            .loader(ProbeLoader {
                observed_init: Arc::new(std::sync::Mutex::new(None)),
            })
            .build()
        {
            Ok(rt) => rt,
            Err(e) => panic!("failed to build runtime: {e}"),
        };
        let registry: &Arc<RuntimeStore> = runtime.registry();
        let _handle: GuestContractHandle =
            register_guest_contract(registry.as_ref(), contract, 0xABCD_u64);
        {
            let mut guard: std::sync::MutexGuard<'_, ReentrantState> = match state.lock() {
                Ok(g) => g,
                Err(e) => e.into_inner(),
            };
            guard.runtime_ptr = Arc::as_ptr(&runtime) as usize;
        }
        let result: Result<(), crate::error::RuntimeError> = runtime.load_bundle_with(
            outer_bundle.as_path(),
            LoadOptions {
                compatibility: polyplug_abi::runtime::Compatibility::default(),
                ignore_function_count_mismatch: false,
            },
        );
        if let Err(e) = result {
            panic!("outer load failed: {e}");
        }
        let inner_completed: Option<bool> = match state.lock() {
            Ok(g) => g.inner_load_completed,
            Err(e) => e.into_inner().inner_load_completed,
        };
        assert_eq!(
            inner_completed,
            Some(true),
            "inner load should have completed successfully"
        );
        let _ = inner_bundle;
    }

    #[test]
    fn lazy_load_during_init_works() {
        let temp: tempfile::TempDir = match tempfile::TempDir::new() {
            Ok(t) => t,
            Err(e) => panic!("failed to create temp dir: {e}"),
        };
        let contract: u64 = polyplug_utils::guest_contract_id("trust.lazy", 1_u32);
        let outer_bundle: PathBuf = create_bundle_dir(&temp, "lazy_outer", "lazy");
        let inner_bundle: PathBuf = create_bundle_dir(&temp, "lazy_inner", "probe");
        let state: Arc<std::sync::Mutex<LazyState>> = Arc::new(std::sync::Mutex::new(LazyState {
            observed_init: None,
        }));
        let runtime: Arc<Runtime> = match Runtime::builder()
            .loader(LazyLoader {
                state: Arc::clone(&state),
            })
            .loader(ProbeLoader {
                observed_init: Arc::new(std::sync::Mutex::new(None)),
            })
            .build()
        {
            Ok(rt) => rt,
            Err(e) => panic!("failed to build runtime: {e}"),
        };
        let registry: &Arc<RuntimeStore> = runtime.registry();
        let _handle: GuestContractHandle =
            register_guest_contract(registry.as_ref(), contract, 0xFACE_u64);
        let result: Result<(), crate::error::RuntimeError> =
            runtime.load_bundle(outer_bundle.as_path());
        if let Err(e) = result {
            panic!("outer load failed: {e}");
        }
        let observed_init: Option<bool> = match state.lock() {
            Ok(g) => g.observed_init,
            Err(e) => e.into_inner().observed_init,
        };
        assert_eq!(
            observed_init,
            Some(true),
            "init should have been observed during lazy loader init"
        );
        let inner_result: Result<(), crate::error::RuntimeError> = runtime.load_bundle_with(
            inner_bundle.as_path(),
            LoadOptions {
                compatibility: polyplug_abi::runtime::Compatibility::default(),
                ignore_function_count_mismatch: false,
            },
        );
        if let Err(e) = inner_result {
            panic!("lazy inner load failed: {e}");
        }
    }

    // --- Host Contract Tests ---

    fn create_host_contract_interface(
        contract_id: u64,
        major: u32,
        minor: u32,
    ) -> &'static HostContractInterface {
        use polyplug_abi::{
            DispatchMechanisms, DispatchType, HostContractInstance, NativeDispatch,
        };

        unsafe extern "C" fn stub_create_instance(
            _this: *const HostContractInterface,
            _args: *const (),
            out_instance: *mut HostContractInstance,
        ) {
            // Return a non-null dummy pointer for testing
            static mut DUMMY: usize = 0xDEADBEEF;
            if !out_instance.is_null() {
                // SAFETY: out_instance is non-null (just checked) and writable per the ABI contract.
                unsafe {
                    out_instance.write(HostContractInstance {
                        data: &raw mut DUMMY as *mut core::ffi::c_void,
                    })
                };
            }
        }

        unsafe extern "C" fn stub_destroy_instance(
            _this: *const HostContractInterface,
            _instance: HostContractInstance,
        ) {
        }

        Box::leak(Box::new(HostContractInterface {
            contract_id: polyplug_utils::HostContractId::from(contract_id),
            contract_version: polyplug_abi::types::Version {
                major,
                minor,
                patch: 0,
            },
            singleton: true,
            dispatch_type: DispatchType::Native,
            runtime: core::ptr::null_mut(),
            user_data: core::ptr::null_mut(),
            create_instance: stub_create_instance,
            destroy_instance: stub_destroy_instance,
            dispatch: DispatchMechanisms {
                native: NativeDispatch {
                    function_count: 0,
                    functions: core::ptr::null(),
                },
            },
        }))
    }

    #[test]
    fn runtime_host_contracts_register_guest_contract_and_lookup() {
        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");

        let contract_id: u64 = polyplug_utils::host_contract_id("host.logger", 1);
        let interface: &'static HostContractInterface =
            create_host_contract_interface(contract_id, 1, 0);

        let result: Result<(), HostContractError> =
            runtime.register_host_contract(contract_id, interface);
        assert!(result.is_ok(), "registration should succeed");

        let found: Option<&'static HostContractInterface> =
            runtime.get_host_contract(contract_id, 0);
        assert!(found.is_some(), "contract should be found");
        let found_interface: &HostContractInterface =
            found.expect("contract should be present after is_some check");
        assert_eq!(found_interface.contract_id.id(), contract_id);
    }

    #[test]
    fn runtime_host_contracts_duplicate_registration_fails() {
        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");

        let contract_id: u64 = polyplug_utils::host_contract_id("host.logger", 1);
        let interface1: &'static HostContractInterface =
            create_host_contract_interface(contract_id, 1, 0);
        let interface2: &'static HostContractInterface =
            create_host_contract_interface(contract_id, 1, 1);

        let result1: Result<(), HostContractError> =
            runtime.register_host_contract(contract_id, interface1);
        assert!(result1.is_ok(), "first registration should succeed");

        let result2: Result<(), HostContractError> =
            runtime.register_host_contract(contract_id, interface2);
        assert!(result2.is_err(), "duplicate registration should fail");
        match result2 {
            Err(HostContractError::DuplicateContract { contract_id: id }) => {
                assert_eq!(id, contract_id);
            }
            Err(other) => panic!("unexpected error: {other}"),
            Ok(()) => panic!("expected error"),
        }
    }

    #[test]
    fn runtime_host_contracts_unregister_guest_contract() {
        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");

        let contract_id: u64 = polyplug_utils::host_contract_id("host.logger", 1);
        let interface: &'static HostContractInterface =
            create_host_contract_interface(contract_id, 1, 0);

        runtime
            .register_host_contract(contract_id, interface)
            .expect("registration should succeed");

        let removed: bool = runtime.unregister_host_contract(contract_id);
        assert!(
            removed,
            "unregister_guest_contract should return true for existing contract"
        );

        let removed_again: bool = runtime.unregister_host_contract(contract_id);
        assert!(
            !removed_again,
            "unregister_guest_contract should return false for non-existent contract"
        );

        let found: Option<&'static HostContractInterface> =
            runtime.get_host_contract(contract_id, 0);
        assert!(
            found.is_none(),
            "contract should not be found after unregister_guest_contract"
        );
    }

    #[test]
    fn runtime_host_contracts_version_check() {
        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");

        let contract_id: u64 = polyplug_utils::host_contract_id("host.logger", 2);
        let interface: &'static HostContractInterface =
            create_host_contract_interface(contract_id, 2, 5);

        runtime
            .register_host_contract(contract_id, interface)
            .expect("registration should succeed");

        let found_low: Option<&'static HostContractInterface> =
            runtime.get_host_contract(contract_id, 0);
        assert!(found_low.is_some(), "should find with min_version=0");

        let found_exact: Option<&'static HostContractInterface> =
            runtime.get_host_contract(contract_id, (2 << 16) | 5);
        assert!(found_exact.is_some(), "should find with exact version");

        let found_higher_minor: Option<&'static HostContractInterface> =
            runtime.get_host_contract(contract_id, (2 << 16) | 3);
        assert!(
            found_higher_minor.is_some(),
            "should find with lower minor version requirement"
        );

        let found_higher_major: Option<&'static HostContractInterface> =
            runtime.get_host_contract(contract_id, 3 << 16);
        assert!(
            found_higher_major.is_none(),
            "should not find with higher major version requirement"
        );
    }

    #[test]
    fn runtime_host_language_default_is_rust() {
        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");
        assert_eq!(runtime.host_language(), SupportedLanguage::Rust);
    }

    #[test]
    fn runtime_host_language_can_be_set() {
        let runtime: Arc<Runtime> = Runtime::builder()
            .host_language(SupportedLanguage::Python)
            .build()
            .expect("runtime build should succeed");
        assert_eq!(runtime.host_language(), SupportedLanguage::Python);
    }

    #[test]
    fn host_get_host_contract_callback_returns_register_guest_contracted_contract() {
        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");

        let contract_id: u64 = polyplug_utils::host_contract_id("host.test", 1);
        let interface: &'static HostContractInterface =
            create_host_contract_interface(contract_id, 1, 0);

        runtime
            .register_host_contract(contract_id, interface)
            .expect("registration should succeed");

        // Create a HostApi with runtime pointer
        let host_interface: HostApi = HostApi {
            runtime: Arc::as_ptr(&runtime) as *mut core::ffi::c_void,
            register_guest_contract: host_register_guest_contract,
            alloc: host_alloc,
            free: host_free,
            find_guest_contract: host_find_guest_contract,
            find_all_guest_contracts: host_find_all_guest_contracts,
            resolve_guest_contract: host_resolve_guest_contract,
            get_host_contract: host_get_host_contract,
            resolve_host_contract_interface: host_resolve_host_contract_interface,
            list_bundles: host_list_bundles,
            get_dependencies: host_get_dependencies,
            // Host operations (implemented in 18-02)
            load_bundle: host_load_bundle,
            reload_bundle: host_reload_bundle,
            register_host_contract: host_register_host_contract,
            register_loader: host_register_loader,
            get_last_error: host_get_last_error,
            get_error_len: host_get_error_len,
            unload_bundle: host_unload_bundle,
            log: stub_host_log,
            create_guest_instance: host_create_guest_instance,
            destroy_guest_instance: host_destroy_guest_instance,
            revision_counter: host_revision_counter,
            reserved: core::ptr::null(),
        };

        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let instance: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, contract_id, 0) };
        assert!(
            !instance.data.is_null(),
            "callback should return non-null instance for register_guest_contracted contract"
        );
    }

    #[test]
    fn host_get_host_contract_callback_returns_null_for_unregister_guest_contracted() {
        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");

        let contract_id: u64 = polyplug_utils::host_contract_id("host.nonexistent", 1);

        // Create a HostApi with runtime pointer
        let host_interface: HostApi = HostApi {
            runtime: Arc::as_ptr(&runtime) as *mut core::ffi::c_void,
            register_guest_contract: host_register_guest_contract,
            alloc: host_alloc,
            free: host_free,
            find_guest_contract: host_find_guest_contract,
            find_all_guest_contracts: host_find_all_guest_contracts,
            resolve_guest_contract: host_resolve_guest_contract,
            get_host_contract: host_get_host_contract,
            resolve_host_contract_interface: host_resolve_host_contract_interface,
            list_bundles: host_list_bundles,
            get_dependencies: host_get_dependencies,
            // Host operations (implemented in 18-02)
            load_bundle: host_load_bundle,
            reload_bundle: host_reload_bundle,
            register_host_contract: host_register_host_contract,
            register_loader: host_register_loader,
            get_last_error: host_get_last_error,
            get_error_len: host_get_error_len,
            unload_bundle: host_unload_bundle,
            log: stub_host_log,
            create_guest_instance: host_create_guest_instance,
            destroy_guest_instance: host_destroy_guest_instance,
            revision_counter: host_revision_counter,
            reserved: core::ptr::null(),
        };

        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let instance: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, contract_id, 0) };
        assert!(
            instance.data.is_null(),
            "callback should return null instance for unregister_guest_contracted contract"
        );
    }

    // ─── Instance Lifecycle Tests (HC-02, HC-03) ───────────────────────────────

    // Create instance callback that returns a unique "magic" pointer per call.
    // Uses a thread-local counter to ensure unique values per call within a test.
    std::thread_local! {
        static LOCAL_INSTANCE_COUNTER: core::cell::Cell<usize> = const { core::cell::Cell::new(0) };
    }

    /// Create instance callback that returns a unique instance per call.
    /// Each call increments a thread-local counter and returns a unique pointer.
    unsafe extern "C" fn counting_create_instance(
        _this: *const HostContractInterface,
        _args: *const (),
        out_instance: *mut HostContractInstance,
    ) {
        let instance: HostContractInstance = LOCAL_INSTANCE_COUNTER.with(|counter| {
            let count: usize = counter.get();
            counter.set(count + 1);
            // Use the count as a "unique" pointer value - we don't actually allocate
            // since these are just test instances
            HostContractInstance {
                data: (count + 1) as *mut core::ffi::c_void, // +1 to avoid null for count=0
            }
        });
        if !out_instance.is_null() {
            // SAFETY: out_instance is non-null (just checked) and writable per the ABI contract.
            unsafe { out_instance.write(instance) };
        }
    }

    /// No-op destroy for counting instances.
    unsafe extern "C" fn counting_destroy_instance(
        _this: *const HostContractInterface,
        _instance: HostContractInstance,
    ) {
        // No cleanup needed - we're just using integer values as pointers
    }

    /// Create a counting host contract interface with configurable singleton mode.
    fn create_counting_host_contract_interface(
        contract_id: u64,
        major: u32,
        singleton: bool,
    ) -> &'static HostContractInterface {
        use polyplug_abi::{DispatchMechanisms, DispatchType, NativeDispatch};

        Box::leak(Box::new(HostContractInterface {
            contract_id: polyplug_utils::HostContractId::from(contract_id),
            contract_version: polyplug_abi::types::Version {
                major,
                minor: 0,
                patch: 0,
            },
            singleton,
            dispatch_type: DispatchType::Native,
            runtime: core::ptr::null_mut(),
            user_data: core::ptr::null_mut(),
            create_instance: counting_create_instance,
            destroy_instance: counting_destroy_instance,
            dispatch: DispatchMechanisms {
                native: NativeDispatch {
                    function_count: 0,
                    functions: core::ptr::null(),
                },
            },
        }))
    }

    #[test]
    fn singleton_contract_returns_cached_instance_on_multiple_calls() {
        // Reset thread-local counter before test
        LOCAL_INSTANCE_COUNTER.with(|counter| counter.set(0));

        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");

        let contract_id: u64 = polyplug_utils::host_contract_id("singleton.test", 1);
        let interface: &'static HostContractInterface =
            create_counting_host_contract_interface(contract_id, 1, true); // singleton=true

        runtime
            .register_host_contract(contract_id, interface)
            .expect("registration should succeed");

        // Create a HostApi with runtime pointer
        let host_interface: HostApi = HostApi {
            runtime: Arc::as_ptr(&runtime) as *mut core::ffi::c_void,
            register_guest_contract: host_register_guest_contract,
            alloc: host_alloc,
            free: host_free,
            find_guest_contract: host_find_guest_contract,
            find_all_guest_contracts: host_find_all_guest_contracts,
            resolve_guest_contract: host_resolve_guest_contract,
            get_host_contract: host_get_host_contract,
            resolve_host_contract_interface: host_resolve_host_contract_interface,
            list_bundles: host_list_bundles,
            get_dependencies: host_get_dependencies,
            // Host operations (implemented in 18-02)
            load_bundle: host_load_bundle,
            reload_bundle: host_reload_bundle,
            register_host_contract: host_register_host_contract,
            register_loader: host_register_loader,
            get_last_error: host_get_last_error,
            get_error_len: host_get_error_len,
            unload_bundle: host_unload_bundle,
            log: stub_host_log,
            create_guest_instance: host_create_guest_instance,
            destroy_guest_instance: host_destroy_guest_instance,
            revision_counter: host_revision_counter,
            reserved: core::ptr::null(),
        };

        // First call - creates instance
        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let instance1: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, contract_id, 0) };
        assert!(
            !instance1.data.is_null(),
            "first call should return non-null instance"
        );

        // Second call - should return SAME cached instance
        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let instance2: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, contract_id, 0) };
        assert!(
            !instance2.data.is_null(),
            "second call should return non-null instance"
        );

        // HC-02: Verify same instance pointer is returned
        assert_eq!(
            instance1.data, instance2.data,
            "singleton contract should return cached instance (same pointer)"
        );

        // Counter should have been incremented only once (single create)
        let counter_value: usize = LOCAL_INSTANCE_COUNTER.with(|counter| counter.get());
        assert_eq!(
            counter_value, 1,
            "singleton should only call create_instance once"
        );

        // Third call - still same instance
        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let instance3: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, contract_id, 0) };
        assert_eq!(
            instance1.data, instance3.data,
            "third call should still return same cached instance"
        );
        assert_eq!(
            LOCAL_INSTANCE_COUNTER.with(|counter| counter.get()),
            1,
            "counter still at 1 - no additional create calls"
        );
    }

    #[test]
    fn multi_instance_contract_creates_new_instance_on_each_call() {
        // Reset thread-local counter before test
        LOCAL_INSTANCE_COUNTER.with(|counter| counter.set(100)); // Start at 100 for unique values

        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");

        let contract_id: u64 = polyplug_utils::host_contract_id("multi.test", 1);
        let interface: &'static HostContractInterface =
            create_counting_host_contract_interface(contract_id, 1, false); // singleton=false

        runtime
            .register_host_contract(contract_id, interface)
            .expect("registration should succeed");

        // Create a HostApi with runtime pointer
        let host_interface: HostApi = HostApi {
            runtime: Arc::as_ptr(&runtime) as *mut core::ffi::c_void,
            register_guest_contract: host_register_guest_contract,
            alloc: host_alloc,
            free: host_free,
            find_guest_contract: host_find_guest_contract,
            find_all_guest_contracts: host_find_all_guest_contracts,
            resolve_guest_contract: host_resolve_guest_contract,
            get_host_contract: host_get_host_contract,
            resolve_host_contract_interface: host_resolve_host_contract_interface,
            list_bundles: host_list_bundles,
            get_dependencies: host_get_dependencies,
            // Host operations (implemented in 18-02)
            load_bundle: host_load_bundle,
            reload_bundle: host_reload_bundle,
            register_host_contract: host_register_host_contract,
            register_loader: host_register_loader,
            get_last_error: host_get_last_error,
            get_error_len: host_get_error_len,
            unload_bundle: host_unload_bundle,
            log: stub_host_log,
            create_guest_instance: host_create_guest_instance,
            destroy_guest_instance: host_destroy_guest_instance,
            revision_counter: host_revision_counter,
            reserved: core::ptr::null(),
        };

        // First call - creates instance (counter becomes 101)
        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let instance1: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, contract_id, 0) };
        assert!(
            !instance1.data.is_null(),
            "first call should return non-null instance"
        );

        // Second call - creates NEW instance (counter becomes 102)
        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let instance2: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, contract_id, 0) };
        assert!(
            !instance2.data.is_null(),
            "second call should return non-null instance"
        );

        // HC-03: Verify different instance pointers are returned
        assert_ne!(
            instance1.data, instance2.data,
            "multi-instance contract should create new instance each call (different pointers)"
        );

        // Counter should have been incremented twice
        let counter_value: usize = LOCAL_INSTANCE_COUNTER.with(|counter| counter.get());
        assert_eq!(
            counter_value, 102,
            "multi-instance should call create_instance twice"
        );

        // Third call - creates yet another instance (counter becomes 103)
        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let instance3: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, contract_id, 0) };
        assert_ne!(
            instance1.data, instance3.data,
            "third instance differs from first"
        );
        assert_ne!(
            instance2.data, instance3.data,
            "third instance differs from second"
        );
        assert_eq!(
            LOCAL_INSTANCE_COUNTER.with(|counter| counter.get()),
            103,
            "counter at 103 - three create calls"
        );
    }

    #[test]
    fn singleton_and_multi_instance_contracts_coexist() {
        // Reset thread-local counter
        LOCAL_INSTANCE_COUNTER.with(|counter| counter.set(0));

        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");

        let singleton_id: u64 = polyplug_utils::host_contract_id("singleton.mixed", 1);
        let multi_id: u64 = polyplug_utils::host_contract_id("multi.mixed", 1);

        let singleton_interface: &'static HostContractInterface =
            create_counting_host_contract_interface(singleton_id, 1, true);
        let multi_interface: &'static HostContractInterface =
            create_counting_host_contract_interface(multi_id, 1, false);

        runtime
            .register_host_contract(singleton_id, singleton_interface)
            .expect("singleton registration should succeed");
        runtime
            .register_host_contract(multi_id, multi_interface)
            .expect("multi-instance registration should succeed");

        // Create a HostApi with runtime pointer
        let host_interface: HostApi = HostApi {
            runtime: Arc::as_ptr(&runtime) as *mut core::ffi::c_void,
            register_guest_contract: host_register_guest_contract,
            alloc: host_alloc,
            free: host_free,
            find_guest_contract: host_find_guest_contract,
            find_all_guest_contracts: host_find_all_guest_contracts,
            resolve_guest_contract: host_resolve_guest_contract,
            get_host_contract: host_get_host_contract,
            resolve_host_contract_interface: host_resolve_host_contract_interface,
            list_bundles: host_list_bundles,
            get_dependencies: host_get_dependencies,
            // Host operations (implemented in 18-02)
            load_bundle: host_load_bundle,
            reload_bundle: host_reload_bundle,
            register_host_contract: host_register_host_contract,
            register_loader: host_register_loader,
            get_last_error: host_get_last_error,
            get_error_len: host_get_error_len,
            unload_bundle: host_unload_bundle,
            log: stub_host_log,
            create_guest_instance: host_create_guest_instance,
            destroy_guest_instance: host_destroy_guest_instance,
            revision_counter: host_revision_counter,
            reserved: core::ptr::null(),
        };

        // Call singleton twice - should get same instance
        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let s1: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, singleton_id, 0) };
        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let s2: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, singleton_id, 0) };
        assert_eq!(s1.data, s2.data, "singleton returns cached instance");

        // Call multi-instance twice - should get different instances
        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let m1: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, multi_id, 0) };
        // SAFETY: host_interface is valid with runtime pointer, runtime is live
        let m2: HostContractInstance =
            unsafe { host_get_host_contract(&host_interface as *const HostApi, multi_id, 0) };
        assert_ne!(m1.data, m2.data, "multi-instance returns new instances");

        // Singleton instance should differ from multi instances
        assert_ne!(
            s1.data, m1.data,
            "singleton and multi instances are different"
        );
        assert_ne!(
            s1.data, m2.data,
            "singleton and multi instances are different"
        );
    }

    #[test]
    fn unload_refuses_provider_with_dependent_then_cascade_succeeds() {
        let runtime: Arc<Runtime> = Runtime::builder()
            .build()
            .expect("runtime build should succeed");

        let provider_contract_id: u64 = 0x0BAD_F00D_0000_00A1;
        let provider_bundle_id: BundleId = BundleId::from_u64(0xA);
        let dependent_bundle_id: BundleId = BundleId::from_u64(0xB);

        // Bundle A provides a contract; bundle B declares a dependency on it.
        register_native_caller_contract(&runtime.registry, provider_contract_id, 0xA);
        register_native_caller_contract(&runtime.registry, 0x0BAD_F00D_0000_00B2, 0xB);

        runtime
            .registry
            .register_bundle_metadata(
                provider_bundle_id,
                "bundle_a".to_owned(),
                Version {
                    major: 1,
                    minor: 0,
                    patch: 0,
                },
                SupportedLanguage::Rust,
                PathBuf::new(),
                Vec::new(),
            )
            .expect("provider metadata registration should succeed");
        runtime
            .registry
            .register_bundle_metadata(
                dependent_bundle_id,
                "bundle_b".to_owned(),
                Version {
                    major: 1,
                    minor: 0,
                    patch: 0,
                },
                SupportedLanguage::Rust,
                PathBuf::new(),
                Vec::new(),
            )
            .expect("dependent metadata registration should succeed");
        runtime
            .registry
            .declare_bundle_dependencies(
                dependent_bundle_id,
                vec![GuestContractId::from_u64(provider_contract_id)],
            )
            .expect("dependency declaration should succeed");

        // Unloading the provider must be refused while the dependent is loaded.
        match runtime.unload_bundle(provider_bundle_id) {
            Err(RuntimeError::DependencyInUse {
                provider,
                dependents,
            }) => {
                assert_eq!(provider, "bundle_a");
                assert_eq!(dependents, vec!["bundle_b".to_owned()]);
            }
            other => panic!("expected DependencyInUse refusal, got {other:?}"),
        }

        // Cascade unload removes the dependent first, then the provider.
        runtime
            .unload_bundle_cascade(provider_bundle_id)
            .expect("cascade unload should succeed");

        assert!(
            runtime
                .registry
                .get_bundle_descriptor(provider_bundle_id)
                .is_none(),
            "provider bundle must be gone after cascade unload"
        );
        assert!(
            runtime
                .registry
                .get_bundle_descriptor(dependent_bundle_id)
                .is_none(),
            "dependent bundle must be gone after cascade unload"
        );
    }
}