1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
// Location: ./crates/cpex-core/src/manager.rs
// Copyright 2025
// SPDX-License-Identifier: Apache-2.0
// Authors: Teryl Taylor, Fred Araujo
//
// Plugin manager.
//
// Owns the plugin lifecycle (initialize, dispatch, shutdown) and
// the PluginRegistry. Provides two invoke paths:
//
// - `invoke::<H>()` — typed dispatch for Rust callers. Zero-cost.
// The hook type is known at compile time; no registry lookup or
// downcast needed for the payload.
//
// - `invoke_by_name()` — dynamic dispatch for Python/Go/WASM callers.
// Hook name resolved from the registry; payload passed as
// Box<dyn PluginPayload>.
//
// The manager reads plugin configs from the config loader and wraps
// each plugin in a PluginRef with the authoritative config. Plugins
// never provide their own config to the manager. Trust flows:
// config loader → manager → PluginRef → executor
//
// Mirrors the Python framework's PluginManager in
// cpex/framework/manager.py.
use std::hash::{Hash, Hasher};
use std::path::Path;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, RwLock};
use hashbrown::HashMap;
use tracing::{error, info, warn};
use crate::config::{self, CpexConfig};
use crate::context::PluginContextTable;
use crate::error::PluginError;
use crate::executor::{BackgroundTasks, Executor, ExecutorConfig, PipelineResult};
use crate::factory::PluginFactoryRegistry;
use crate::hooks::adapter::TypedHandlerAdapter;
use crate::hooks::payload::{Extensions, PluginPayload};
use crate::hooks::trait_def::{HookHandler, HookTypeDef, PluginResult};
use crate::hooks::HookType;
use crate::plugin::{Plugin, PluginConfig};
use crate::registry::{AnyHookHandler, PluginRef, PluginRegistry};
// ---------------------------------------------------------------------------
// Manager Configuration
// ---------------------------------------------------------------------------
/// Default upper bound on the routing cache. Caps memory growth from
/// attacker-controlled entity names without forcing operators to tune.
pub const DEFAULT_ROUTE_CACHE_MAX_ENTRIES: usize = 10_000;
/// Configuration for the PluginManager.
#[derive(Debug, Clone)]
pub struct ManagerConfig {
/// Executor configuration (timeout, short-circuit behavior).
pub executor: ExecutorConfig,
/// Maximum number of entries in the routing cache. When the cache
/// reaches this size, further inserts are rejected (with a one-shot
/// warn log) and resolutions fall back to the slow path. See
/// `PluginSettings::route_cache_max_entries` for the YAML surface.
pub route_cache_max_entries: usize,
}
impl Default for ManagerConfig {
fn default() -> Self {
Self {
executor: ExecutorConfig::default(),
route_cache_max_entries: DEFAULT_ROUTE_CACHE_MAX_ENTRIES,
}
}
}
// ---------------------------------------------------------------------------
// Plugin Manager
// ---------------------------------------------------------------------------
/// Central plugin lifecycle and dispatch manager.
///
/// Owns the plugin registry and executor. Provides the public API
/// that host systems (ContextForge, Kagenti, etc.) call to register
/// plugins and invoke hooks.
///
/// # Lifecycle
///
/// ```text
/// new() → register plugins → initialize() → invoke hooks → shutdown()
/// ```
///
/// # Two Invoke Paths
///
/// - **`invoke::<H>()`** — typed dispatch. The hook type `H` is known
/// at compile time. Payload type-checked at compile time. Used by
/// Rust callers.
///
/// - **`invoke_by_name()`** — dynamic dispatch. The hook name is a
/// string. Payload is `Box<dyn PluginPayload>`. Used by Python/Go/WASM
/// callers via the FFI or PyO3 bindings.
///
/// Both paths use the same registry, executor, and 5-phase pipeline.
///
/// # Trust Model
///
/// The manager wraps each plugin in a `PluginRef` with an authoritative
/// config from the config loader. The executor reads all scheduling
/// decisions from `PluginRef.trusted_config` — never from the plugin.
/// Cache key for resolved routing entries.
///
/// Includes entity type, name, hook name, and scope so that
/// the same tool on different scopes or at different hook points
/// caches separately.
///
/// Custom Hash/Eq implementations hash on `&str` slices so that
/// `raw_entry` lookups with borrowed strings produce the same hash
/// as the owned key — enabling zero-allocation cache hits.
#[derive(Debug, Clone)]
struct RouteCacheKey {
entity_type: String,
entity_name: String,
hook_name: String,
scope: Option<String>,
}
impl Hash for RouteCacheKey {
fn hash<H: Hasher>(&self, state: &mut H) {
self.entity_type.as_str().hash(state);
self.entity_name.as_str().hash(state);
self.hook_name.as_str().hash(state);
self.scope.as_deref().hash(state);
}
}
impl PartialEq for RouteCacheKey {
fn eq(&self, other: &Self) -> bool {
self.entity_type == other.entity_type
&& self.entity_name == other.entity_name
&& self.hook_name == other.hook_name
&& self.scope == other.scope
}
}
impl Eq for RouteCacheKey {}
/// Mutable runtime state held atomically swappable behind `ArcSwap`.
///
/// Every read on the hot path (`invoke_*`) does a single atomic load to
/// get an `Arc<RuntimeSnapshot>` — no locks. Mutating operations
/// (`register_*`, `load_config`, `unregister`) clone the current snapshot,
/// mutate the clone, and atomically swap the new `Arc` in. Old readers
/// finish on the old snapshot; new readers see the new one. This is the
/// classic Read-Copy-Update / RCU pattern: lock-free reads, copy-on-write
/// writes, no reader-writer contention.
///
/// Cloning `PluginRegistry` is cheap because every value inside (`PluginRef`,
/// `AnyHookHandler`) is `Arc`-counted — only the `HashMap` shells duplicate.
#[derive(Clone)]
struct RuntimeSnapshot {
/// Plugin registry — stores PluginRefs and hook-to-handler mappings.
registry: PluginRegistry,
/// Executor — stateless 5-phase pipeline engine.
executor: Executor,
/// Parsed CPEX config (when loaded from file). Used for route resolution.
cpex_config: Option<CpexConfig>,
/// Maximum number of entries the route cache will hold. Once reached,
/// new resolutions are computed normally but not memoized (reject-on-full).
route_cache_max_entries: usize,
/// Per-route, per-hook handler overrides keyed by
/// `(entity_type, entity_name, scope, hook_name)`. When a request matches
/// an annotation, route resolution short-circuits to a single-entry list
/// containing the annotated handler instead of resolving the route's
/// imperative `plugins:` chain.
///
/// Per-hook keying lets an orchestrator install distinct handlers for
/// `cmf.tool_pre_invoke` and `cmf.tool_post_invoke` on the same route —
/// useful when the pre/post phases need different handler state (e.g.
/// apl-cpex's `AplRouteHandler` binds each instance to either
/// `evaluate_pre` or `evaluate_post`).
///
/// `scope` (None vs `Some("virtual-server-A")`) lets two virtual
/// servers / gateways with the same tool name carry distinct
/// orchestrators. Matching mirrors cpex-core's existing
/// `find_matching_route` semantics: a scoped request first tries the
/// exact `(et, en, Some(req_scope), hook)` annotation; on miss it falls
/// back to the unscoped `(et, en, None, hook)` default. An unscoped
/// request only matches `(et, en, None, hook)`. Net effect: None-scope
/// annotations act as a global default, scoped annotations override
/// per-scope.
///
/// The plugins listed under the matching route are *still* registered
/// in the registry — they remain discoverable via `find_plugin_entries`
/// so the annotated handler can dispatch into them by-name (this is
/// what apl-cpex's `AplRouteHandler` does via `CmfPluginInvoker` for
/// `plugin(name)` references inside APL rules).
route_annotations: HashMap<AnnotationKey, crate::registry::HookEntry>,
}
/// Composite key for route annotations. Includes the hook name so a single
/// route can carry distinct handlers per phase (e.g. pre-invoke vs
/// post-invoke).
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
struct AnnotationKey {
entity_type: String,
entity_name: String,
scope: Option<String>,
hook_name: String,
}
pub struct PluginManager {
/// Hot-path runtime state. Swapped atomically on registration / config
/// reload — readers see a consistent view via a single `load_full()`.
runtime: arc_swap::ArcSwap<RuntimeSnapshot>,
/// Factory registry — owned by the manager. Used for initial
/// instantiation and for creating override instances when routes
/// override a plugin's base config.
///
/// Held in a `RwLock` rather than the `ArcSwap` snapshot because
/// `Box<dyn PluginFactory>` is not `Clone`. Read on the slow path
/// (route cache miss + override config); write on `register_factory`.
/// The hot path never touches it.
factories: RwLock<PluginFactoryRegistry>,
/// Cache of resolved hook entries per (entity, hook, scope).
/// Populated on first access, invalidated on config reload.
/// Uses Arc so cache reads are refcount bumps (~1ns), not data copies.
route_cache: RwLock<HashMap<RouteCacheKey, Arc<Vec<crate::registry::HookEntry>>>>,
/// Hasher builder for zero-allocation cache lookups via raw_entry.
cache_hasher: hashbrown::DefaultHashBuilder,
/// Set to true after the first time the cache rejects an insert in a
/// given fill cycle, so the warn log fires once per cycle rather than
/// on every miss under DoS. Reset by `clear_routing_cache()`.
route_cache_full_warned: AtomicBool,
/// Whether initialize() has been called. Atomic so lifecycle methods
/// can be `&self` and the manager itself can sit behind `Arc`.
initialized: AtomicBool,
/// Monotonic config-generation counter. Bumped every time the runtime
/// snapshot is swapped (factory mutation, config (re)load, plugin
/// register/unregister). External orchestrators (apl-cpex's dispatch
/// plan cache) pair their cached values with the generation seen at
/// build time; a generation mismatch on lookup signals "evict + rebuild."
/// Starts at 0; first snapshot publish (empty registry) leaves it at 0,
/// so callers can use 0 as a "never observed" sentinel.
generation: AtomicU64,
/// Tracks in-flight fire-and-forget background tasks across all
/// invocations so `shutdown()` can wait for them to drain before
/// returning. Without this, audit/telemetry tasks spawned by recent
/// invokes get cancelled when the runtime tears down. Tasks are
/// `tracker.spawn`'d in `spawn_fire_and_forget`; `shutdown()` calls
/// `close().wait().await`.
///
/// `TaskTracker` is internally `Arc`'d, so cloning is a refcount bump.
task_tracker: tokio_util::task::TaskTracker,
/// External orchestrators registered via `register_visitor`. Walked
/// in registration order during `load_config_yaml` (after plugin
/// instantiation) so each visitor can inspect raw YAML sections and
/// install handlers via `annotate_route`. Empty by default — the
/// `load_config(CpexConfig)` path skips visitors entirely.
visitors: RwLock<Vec<Arc<dyn crate::visitor::ConfigVisitor>>>,
}
/// Emit warnings for YAML settings that the runtime doesn't currently
/// honor. Called once per `load_config` / `from_config` so operators
/// who set these knobs aren't silently ignored.
///
/// `user_patterns` / `content_types` on `PluginCondition` are not warned
/// — they were wired up alongside this fix and now actually filter.
fn warn_on_inactive_settings(cfg: &CpexConfig) {
if !cfg.plugin_dirs.is_empty() {
warn!(
"config sets `plugin_dirs` (count={}) but the runtime does not \
scan directories for plugins — plugins must be registered via \
`register_factory()` and listed under `plugins:`. Setting ignored.",
cfg.plugin_dirs.len(),
);
}
if cfg.plugin_settings.parallel_execution_within_band {
warn!(
"config sets `plugin_settings.parallel_execution_within_band: true` \
but the runtime does not honor it — use `mode: concurrent` on \
individual plugins for parallel execution. Setting ignored.",
);
}
if cfg.plugin_settings.fail_on_plugin_error {
warn!(
"config sets `plugin_settings.fail_on_plugin_error: true` but the \
runtime does not honor it — use per-plugin `on_error: fail` for \
that behavior. Setting ignored.",
);
}
}
/// Instantiate every plugin in `plugin_configs` via the matching factory
/// and register the resulting handlers into `target_registry`. Shared by
/// `PluginManager::from_config` (fresh registry) and `load_config` (clone
/// of the existing registry) so the instantiation loop lives in one place.
///
/// Returns on the first failure (factory missing, factory.create error, or
/// duplicate-name registration). On error, `target_registry` is in a
/// partial state — both callers discard it on failure (load_config builds
/// the new registry on a clone and only swaps on Ok; from_config bails
/// before publishing the snapshot).
fn instantiate_plugins_into(
target_registry: &mut PluginRegistry,
plugin_configs: &[crate::plugin::PluginConfig],
factories: &PluginFactoryRegistry,
) -> Result<(), Box<PluginError>> {
for plugin_config in plugin_configs {
let factory = factories
.get(&plugin_config.kind)
.ok_or_else(|| PluginError::Config {
message: format!(
"no factory registered for plugin kind '{}' (plugin '{}')",
plugin_config.kind, plugin_config.name
),
})?;
let instance = factory.create(plugin_config)?;
target_registry
.register_multi_handler(instance.plugin, plugin_config.clone(), instance.handlers)
.map_err(|msg| Box::new(PluginError::Config { message: msg }))?;
info!(
"Registered plugin '{}' (kind: '{}') for hooks: {:?}",
plugin_config.name, plugin_config.kind, plugin_config.hooks
);
}
Ok(())
}
/// Build a `RuntimeSnapshot` from a populated registry plus the YAML
/// settings on `cpex_config`. Pulls executor timeout / short-circuit and
/// the route-cache cap from `plugin_settings` so both registration paths
/// agree on field-by-field translation.
fn snapshot_from_config(registry: PluginRegistry, cpex_config: CpexConfig) -> RuntimeSnapshot {
let executor = Executor::new(ExecutorConfig {
timeout_seconds: cpex_config.plugin_settings.plugin_timeout,
short_circuit_on_deny: cpex_config.plugin_settings.short_circuit_on_deny,
});
let route_cache_max_entries = cpex_config.plugin_settings.route_cache_max_entries;
RuntimeSnapshot {
registry,
executor,
cpex_config: Some(cpex_config),
route_cache_max_entries,
route_annotations: HashMap::new(),
}
}
impl PluginManager {
/// Create a new PluginManager with the given configuration.
pub fn new(config: ManagerConfig) -> Self {
let cache_hasher = hashbrown::DefaultHashBuilder::default();
let snapshot = RuntimeSnapshot {
registry: PluginRegistry::new(),
executor: Executor::new(config.executor),
cpex_config: None,
route_cache_max_entries: config.route_cache_max_entries,
route_annotations: HashMap::new(),
};
Self {
runtime: arc_swap::ArcSwap::from_pointee(snapshot),
factories: RwLock::new(PluginFactoryRegistry::new()),
route_cache: RwLock::new(HashMap::with_hasher(cache_hasher.clone())),
cache_hasher,
route_cache_full_warned: AtomicBool::new(false),
initialized: AtomicBool::new(false),
generation: AtomicU64::new(0),
task_tracker: tokio_util::task::TaskTracker::new(),
visitors: RwLock::new(Vec::new()),
}
}
/// Load the current runtime snapshot (lock-free, single atomic op).
fn load_runtime(&self) -> Arc<RuntimeSnapshot> {
self.runtime.load_full()
}
/// Apply a mutation to the runtime snapshot via copy-on-write.
/// Clones the current snapshot, runs the closure on the clone, and
/// atomically swaps it in. Concurrent readers continue using the old
/// snapshot; subsequent readers see the new one.
fn mutate_runtime<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut RuntimeSnapshot) -> R,
{
let current = self.runtime.load_full();
let mut next = (*current).clone();
let result = f(&mut next);
self.runtime.store(Arc::new(next));
// Release ordering pairs with the Acquire load in
// config_generation() — external cache consumers that observe a
// higher generation are guaranteed to see the new snapshot.
self.generation.fetch_add(1, Ordering::Release);
result
}
/// Like `mutate_runtime` but the mutation can fail — the new snapshot
/// is only published on `Ok`. On `Err`, the original snapshot is
/// untouched, so a partially-mutated clone is silently discarded.
fn try_mutate_runtime<F, T, E>(&self, f: F) -> Result<T, E>
where
F: FnOnce(&mut RuntimeSnapshot) -> Result<T, E>,
{
let current = self.runtime.load_full();
let mut next = (*current).clone();
let result = f(&mut next)?;
self.runtime.store(Arc::new(next));
// Same Release-ordered bump as mutate_runtime — only on Ok, since
// Err leaves the snapshot untouched.
self.generation.fetch_add(1, Ordering::Release);
Ok(result)
}
/// Monotonic counter that increments on every runtime snapshot swap
/// (registry mutation, config (re)load). External orchestrators
/// (e.g. apl-cpex's dispatch-plan cache) pair their cached values
/// with the generation seen at build time; a mismatch on lookup
/// signals "evict + rebuild." `Acquire` pairs with the `Release`
/// fetch_add in `mutate_runtime` / `try_mutate_runtime` so observing
/// a higher generation guarantees visibility of the new snapshot.
pub fn config_generation(&self) -> u64 {
self.generation.load(Ordering::Acquire)
}
// -----------------------------------------------------------------------
// Factory Registration
// -----------------------------------------------------------------------
/// Register a plugin factory for a given `kind` name.
///
/// The host calls this to tell the manager how to create plugins
/// of a specific kind. Must be called before `load_config()`.
///
/// # Examples
///
/// ```rust,ignore
/// let mut manager = PluginManager::default();
/// manager.register_factory("builtin", Box::new(BuiltinFactory));
/// manager.register_factory("security/rate_limit", Box::new(RateLimiterFactory));
/// manager.load_config(Path::new("plugins.yaml"))?;
/// ```
pub fn register_factory(
&self,
kind: impl Into<String>,
factory: Box<dyn crate::factory::PluginFactory>,
) {
self.factories
.write()
.unwrap_or_else(|p| p.into_inner())
.register(kind, factory);
}
// -----------------------------------------------------------------------
// Config Loading
// -----------------------------------------------------------------------
/// Load plugins from a YAML config file.
///
/// Parses the config, looks up each plugin's `kind` in the
/// factory registry, instantiates the plugins, and registers
/// them. Factories must be registered via `register_factory()`
/// before calling this method.
///
/// # Examples
///
/// ```rust,ignore
/// let mut manager = PluginManager::default();
/// manager.register_factory("builtin", Box::new(BuiltinFactory));
/// manager.load_config_file(Path::new("plugins/config.yaml"))?;
/// manager.initialize().await?;
/// ```
pub fn load_config_file(&self, path: &Path) -> Result<(), Box<PluginError>> {
let cpex_config = config::load_config(path)?;
self.load_config(cpex_config)
}
/// Load plugins from a parsed config.
///
/// Looks up each plugin's `kind` in the factory registry,
/// instantiates the plugins, and registers them with their
/// hook names from the config.
pub fn load_config(&self, cpex_config: CpexConfig) -> Result<(), Box<PluginError>> {
warn_on_inactive_settings(&cpex_config);
// Build the new snapshot from the current one — copy-on-write so
// concurrent invokes keep using the existing config until we swap.
// We can't use mutate_runtime here because we need to atomically
// ALSO build a new executor + new cache cap from the same config —
// the snapshot fields are coupled.
let factories = self.factories.read().unwrap_or_else(|p| p.into_inner());
let current = self.runtime.load_full();
let mut new_registry = current.registry.clone();
instantiate_plugins_into(&mut new_registry, &cpex_config.plugins, &factories)?;
// Drop the factories read lock before taking other locks
// (route_cache write below) to avoid lock-ordering hazards.
drop(factories);
self.runtime
.store(Arc::new(snapshot_from_config(new_registry, cpex_config)));
// Same generation bump as mutate_runtime — load_config doesn't
// go through that helper because it has to swap registry + executor
// + cache-cap atomically as one snapshot.
self.generation.fetch_add(1, Ordering::Release);
// Clear routing cache — config changed.
self.clear_routing_cache();
Ok(())
}
/// Register an external config visitor. Visitors run during
/// `load_config_yaml` (after plugin instantiation) and can install
/// per-route handler overrides via `annotate_route`. Visitor order
/// matches registration order. Multiple visitors are allowed —
/// they typically don't share state, so order rarely matters.
pub fn register_visitor(&self, visitor: Arc<dyn crate::visitor::ConfigVisitor>) {
let mut v = self.visitors.write().unwrap_or_else(|p| p.into_inner());
v.push(visitor);
}
/// Load a unified-config YAML string. Parses the YAML twice — once
/// into a typed `CpexConfig` for plugin instantiation, once into a
/// raw `serde_yaml::Value` so visitors can inspect orchestrator-
/// specific blocks (e.g. `apl:`) that cpex-core itself doesn't
/// model. Calls existing `load_config(cpex_config)` first, then
/// walks each registered visitor over the raw YAML's sections in
/// the documented hierarchy order:
///
/// 1. `visit_global(global_yaml)`
/// 2. `visit_default(entity_type, default_yaml)` per `global.defaults` entry
/// 3. `visit_policy_bundle(tag, bundle_yaml)` per `global.policies` entry
/// 4. `visit_route(route_yaml, parsed_route)` per `routes[]` entry
///
/// All sections for one visitor run before the next visitor starts,
/// giving each visitor a consistent view of its own accumulated
/// state. A visitor returning Err aborts the load — the plugin
/// snapshot stays at the post-`load_config` state (partial load is
/// not rolled back; operators should treat any error from this
/// method as a hard stop).
pub fn load_config_yaml(self: &Arc<Self>, yaml: &str) -> Result<(), Box<PluginError>> {
// Parse once into a Value so the raw shape is available to
// visitors. Then deserialize from that Value into CpexConfig —
// saves a second tokenize/lex pass vs parsing the string twice.
let raw: serde_yaml::Value = serde_yaml::from_str(yaml).map_err(|e| {
Box::new(PluginError::Config {
message: format!("YAML parse error: {}", e),
})
})?;
let cpex_config: CpexConfig = serde_yaml::from_value(raw.clone()).map_err(|e| {
Box::new(PluginError::Config {
message: format!("CpexConfig deserialize error: {}", e),
})
})?;
// Snapshot the parsed routes + plugin declarations before
// load_config moves the config — visitors get the typed
// structures side-by-side with the raw YAML so they don't have
// to re-deserialize anything cpex-core has already validated.
let parsed_routes: Vec<crate::config::RouteEntry> = cpex_config.routes.clone();
let parsed_plugins: Vec<crate::plugin::PluginConfig> = cpex_config.plugins.clone();
// Existing plugin-instantiation path.
self.load_config(cpex_config)?;
// Visitor walk. No-op when no visitors registered — the common
// case for hosts that don't use the orchestrator extension point.
let visitors = {
let v = self.visitors.read().unwrap_or_else(|p| p.into_inner());
if v.is_empty() {
return Ok(());
}
v.clone()
};
let mgr: Arc<PluginManager> = Arc::clone(self);
let global_yaml = raw
.get("global")
.cloned()
.unwrap_or(serde_yaml::Value::Null);
let defaults_yaml = global_yaml
.get("defaults")
.and_then(serde_yaml::Value::as_mapping)
.cloned();
let policies_yaml = global_yaml
.get("policies")
.and_then(serde_yaml::Value::as_mapping)
.cloned();
let routes_yaml: Vec<serde_yaml::Value> = raw
.get("routes")
.and_then(serde_yaml::Value::as_sequence)
.cloned()
.unwrap_or_default();
for visitor in &visitors {
visitor.visit_plugins(&mgr, &parsed_plugins).map_err(|e| {
Box::new(PluginError::Config {
message: format!("visitor '{}' visit_plugins: {}", visitor.name(), e),
})
})?;
visitor.visit_global(&mgr, &global_yaml).map_err(|e| {
Box::new(PluginError::Config {
message: format!("visitor '{}' visit_global: {}", visitor.name(), e),
})
})?;
if let Some(defaults) = &defaults_yaml {
for (k, v) in defaults {
let Some(entity_type) = k.as_str() else {
continue;
};
visitor.visit_default(&mgr, entity_type, v).map_err(|e| {
Box::new(PluginError::Config {
message: format!(
"visitor '{}' visit_default('{}'): {}",
visitor.name(),
entity_type,
e
),
})
})?;
}
}
if let Some(policies) = &policies_yaml {
for (k, v) in policies {
let Some(tag) = k.as_str() else { continue };
visitor.visit_policy_bundle(&mgr, tag, v).map_err(|e| {
Box::new(PluginError::Config {
message: format!(
"visitor '{}' visit_policy_bundle('{}'): {}",
visitor.name(),
tag,
e
),
})
})?;
}
}
for (i, parsed) in parsed_routes.iter().enumerate() {
let route_yaml = routes_yaml
.get(i)
.cloned()
.unwrap_or(serde_yaml::Value::Null);
visitor
.visit_route(&mgr, &route_yaml, parsed)
.map_err(|e| {
Box::new(PluginError::Config {
message: format!(
"visitor '{}' visit_route[{}]: {}",
visitor.name(),
i,
e
),
})
})?;
}
}
Ok(())
}
/// Create a PluginManager from a parsed config (convenience).
///
/// Uses the passed factory registry for initial instantiation.
/// Note: for route-level config overrides to create new instances
/// at runtime, use `register_factory()` + `load_config()` instead
/// so the manager owns the factories.
pub fn from_config(
cpex_config: CpexConfig,
factories: &PluginFactoryRegistry,
) -> Result<Self, Box<PluginError>> {
warn_on_inactive_settings(&cpex_config);
let manager = Self::new(ManagerConfig {
executor: ExecutorConfig::default(),
route_cache_max_entries: cpex_config.plugin_settings.route_cache_max_entries,
});
// Instantiate into a fresh registry, then publish atomically.
let mut new_registry = PluginRegistry::new();
instantiate_plugins_into(&mut new_registry, &cpex_config.plugins, factories)?;
manager
.runtime
.store(Arc::new(snapshot_from_config(new_registry, cpex_config)));
Ok(manager)
}
// -----------------------------------------------------------------------
// Registration
// -----------------------------------------------------------------------
/// Register a plugin handler for its primary hook name.
///
/// This is the preferred registration method. The framework creates
/// the type-erased adapter internally — no `AnyHookHandler` needed.
///
/// # Type Parameters
///
/// - `H` — the hook type (implements `HookTypeDef`).
/// - `P` — the plugin type (implements `Plugin + HookHandler<H>`).
///
/// # Arguments
///
/// - `plugin` — the plugin implementation.
/// - `config` — authoritative config from the config loader.
///
/// # Examples
///
/// ```rust,ignore
/// manager.register_handler::<CmfHook, _>(plugin, config)?;
/// ```
pub fn register_handler<H, P>(
&self,
plugin: Arc<P>,
config: PluginConfig,
) -> Result<(), Box<PluginError>>
where
H: HookTypeDef,
H::Result: Into<PluginResult<H::Payload>>,
P: Plugin + HookHandler<H> + 'static,
{
let handler: Arc<dyn AnyHookHandler> =
Arc::new(TypedHandlerAdapter::<H, P>::new(Arc::clone(&plugin)));
self.try_mutate_runtime(|snap| {
snap.registry
.register::<H>(plugin, config, handler)
.map_err(|msg| Box::new(PluginError::Config { message: msg }))
})?;
self.clear_routing_cache();
Ok(())
}
/// Register a plugin handler for multiple hook names.
///
/// This is the CMF pattern — one handler covers multiple hook
/// names (`cmf.tool_pre_invoke`, `cmf.llm_input`, etc.).
///
/// # Examples
///
/// ```rust,ignore
/// manager.register_handler_for_names::<CmfHook, _>(
/// plugin, config,
/// &["cmf.tool_pre_invoke", "cmf.llm_input", "cmf.llm_output"],
/// )?;
/// ```
pub fn register_handler_for_names<H, P>(
&self,
plugin: Arc<P>,
config: PluginConfig,
names: &[&str],
) -> Result<(), Box<PluginError>>
where
H: HookTypeDef,
H::Result: Into<PluginResult<H::Payload>>,
P: Plugin + HookHandler<H> + 'static,
{
let handler: Arc<dyn AnyHookHandler> =
Arc::new(TypedHandlerAdapter::<H, P>::new(Arc::clone(&plugin)));
self.try_mutate_runtime(|snap| {
snap.registry
.register_for_names::<H>(plugin, config, handler, names)
.map_err(|msg| Box::new(PluginError::Config { message: msg }))
})?;
self.clear_routing_cache();
Ok(())
}
/// Register with an explicit AnyHookHandler (advanced use).
///
/// For cases where the automatic adapter doesn't fit — e.g.,
/// Python/WASM bridge hosts that implement AnyHookHandler directly.
/// Most callers should use `register_handler` instead.
pub fn register_raw<H: HookTypeDef>(
&self,
plugin: Arc<dyn Plugin>,
config: PluginConfig,
handler: Arc<dyn AnyHookHandler>,
) -> Result<(), Box<PluginError>> {
self.try_mutate_runtime(|snap| {
snap.registry
.register::<H>(plugin, config, handler)
.map_err(|msg| Box::new(PluginError::Config { message: msg }))
})?;
self.clear_routing_cache();
Ok(())
}
// -----------------------------------------------------------------------
// Lifecycle
// -----------------------------------------------------------------------
/// Initialize all registered plugins.
///
/// Calls `plugin.initialize()` on each registered plugin. Must be
/// called before invoking any hooks. Idempotent — calling twice
/// has no effect.
pub async fn initialize(&self) -> Result<(), Box<PluginError>> {
if self.initialized.load(Ordering::Acquire) {
return Ok(());
}
// Snapshot once at start — subsequent registrations don't affect
// this initialize() call. They'd need their own initialize.
let snapshot = self.load_runtime();
info!(
"Initializing PluginManager with {} plugins",
snapshot.registry.plugin_count()
);
let mut initialized_plugins: Vec<String> = Vec::new();
for name in snapshot.registry.plugin_names() {
if let Some(plugin_ref) = snapshot.registry.get(&name) {
let plugin = plugin_ref.plugin().clone();
let plugin_name = name;
if let Err(e) = plugin.initialize().await {
error!("Failed to initialize plugin '{}': {}", plugin_name, e);
// Clean up already-initialized plugins
for init_name in initialized_plugins.iter().rev() {
if let Some(pr) = snapshot.registry.get(init_name) {
if let Err(shutdown_err) = pr.plugin().shutdown().await {
error!(
"Error shutting down plugin '{}' during rollback: {}",
init_name, shutdown_err
);
}
}
}
return Err(Box::new(PluginError::Execution {
plugin_name,
message: format!("initialization failed: {}", e),
source: Some(Box::new(e)),
code: None,
details: std::collections::HashMap::new(),
proto_error_code: None,
}));
}
initialized_plugins.push(plugin_name);
}
}
self.initialized.store(true, Ordering::Release);
info!("PluginManager initialized successfully");
Ok(())
}
/// Shutdown all registered plugins.
///
/// Calls `plugin.shutdown()` on each registered plugin in reverse
/// registration order. Errors are logged but do not halt the
/// shutdown process — all plugins get a chance to clean up.
/// Shut the manager down. **Terminal:** after `shutdown()` returns,
/// no further `register_*` / `invoke_*` should be called. New
/// fire-and-forget tasks spawned after `close()` will not be tracked
/// (the `TaskTracker` is single-shot by design).
pub async fn shutdown(&self) {
if !self.initialized.load(Ordering::Acquire) {
return;
}
info!("Shutting down PluginManager");
// Drain in-flight fire-and-forget tasks BEFORE tearing down
// plugins — otherwise audit/telemetry tasks that depend on the
// plugin being alive (or the runtime being up) get cancelled
// mid-flight. `close()` prevents new tasks from being tracked
// (existing in-flight ones still complete); `wait()` returns
// when the in-flight count drops to zero.
self.task_tracker.close();
self.task_tracker.wait().await;
let snapshot = self.load_runtime();
for name in snapshot.registry.plugin_names() {
if let Some(plugin_ref) = snapshot.registry.get(&name) {
let plugin = plugin_ref.plugin().clone();
if let Err(e) = plugin.shutdown().await {
error!("Error shutting down plugin '{}': {}", name, e);
// Continue — don't let one plugin's failure block others
}
}
}
self.initialized.store(false, Ordering::Release);
info!("PluginManager shutdown complete");
}
// -----------------------------------------------------------------------
// Hook Invocation — Dynamic (invoke_by_name)
// -----------------------------------------------------------------------
/// Invoke a hook by name with a type-erased payload.
///
/// This is the dynamic dispatch path used by Python/Go/WASM
/// callers via FFI or PyO3 bindings. The hook name is resolved
/// from the registry and dispatched through the 5-phase executor.
///
/// # Arguments
///
/// * `hook_name` — the hook name string (e.g., `"cmf.tool_pre_invoke"`).
/// * `payload` — the payload as `Box<dyn PluginPayload>`.
/// * `extensions` — the full extensions (filtered per plugin by the executor).
/// * `context_table` — optional context table from a previous hook
/// invocation. Pass `None` on the first hook call; thread the
/// returned table into subsequent calls to preserve per-plugin state.
///
/// # Returns
///
/// A tuple of `(PipelineResult, BackgroundTasks)`. The result
/// contains the final payload, extensions, violation, and context
/// table. Background tasks can be awaited or dropped.
pub async fn invoke_by_name(
&self,
hook_name: &str,
payload: Box<dyn PluginPayload>,
extensions: Extensions,
context_table: Option<PluginContextTable>,
) -> (PipelineResult, BackgroundTasks) {
// Single atomic load — own the snapshot for the rest of the call so
// a concurrent register/load_config swapping in a new snapshot doesn't
// change our view mid-pipeline.
let snapshot = self.load_runtime();
let hook_type = HookType::new(hook_name);
let all_entries = snapshot.registry.entries_for_hook(&hook_type);
// Same caveat as `invoke_named`: route annotations can produce a
// dispatch entry without any plugin being registered on the
// hook directly, so we can only short-circuit when both the
// registry and the annotation map are empty.
if all_entries.is_empty() && snapshot.route_annotations.is_empty() {
return (
PipelineResult::allowed_with(
payload,
extensions,
context_table.unwrap_or_default(),
),
BackgroundTasks::empty(),
);
}
let entries = self
.filter_entries_by_route(&snapshot, all_entries, &extensions, hook_name)
.await;
if entries.is_empty() {
return (
PipelineResult::allowed_with(
payload,
extensions,
context_table.unwrap_or_default(),
),
BackgroundTasks::empty(),
);
}
snapshot
.executor
.execute(
&entries,
payload,
extensions,
context_table,
&self.task_tracker,
)
.await
}
// -----------------------------------------------------------------------
// Hook Invocation — Typed (invoke::<H>)
// -----------------------------------------------------------------------
/// Invoke a typed hook.
///
/// This is the compile-time dispatch path used by Rust callers.
/// The hook type `H` determines the payload and result types.
/// Dispatch goes through the same registry and 5-phase executor
/// as `invoke_by_name()`.
///
/// When routing is enabled, the entity is identified from
/// `extensions.meta` (entity_type + entity_name). Only plugins
/// matching the resolved route fire. When routing is disabled
/// or meta is absent, all registered plugins fire.
///
/// # Type Parameters
///
/// - `H` — the hook type (implements `HookTypeDef`).
///
/// # Arguments
///
/// * `payload` — the typed payload.
/// * `extensions` — the full extensions (includes meta for routing).
/// * `context_table` — optional context table from a previous hook.
///
/// # Returns
///
/// A tuple of `(PipelineResult, BackgroundTasks)`.
pub async fn invoke<H: HookTypeDef>(
&self,
payload: H::Payload,
extensions: Extensions,
context_table: Option<PluginContextTable>,
) -> (PipelineResult, BackgroundTasks) {
let snapshot = self.load_runtime();
let hook_type = HookType::new(H::NAME);
let all_entries = snapshot.registry.entries_for_hook(&hook_type);
// See `invoke_named` for why we don't short-circuit on
// `all_entries.is_empty()` alone — route annotations can fire
// without a directly-registered plugin.
if all_entries.is_empty() && snapshot.route_annotations.is_empty() {
let boxed: Box<dyn PluginPayload> = Box::new(payload);
return (
PipelineResult::allowed_with(boxed, extensions, context_table.unwrap_or_default()),
BackgroundTasks::empty(),
);
}
let entries = self
.filter_entries_by_route(&snapshot, all_entries, &extensions, H::NAME)
.await;
if entries.is_empty() {
let boxed: Box<dyn PluginPayload> = Box::new(payload);
return (
PipelineResult::allowed_with(boxed, extensions, context_table.unwrap_or_default()),
BackgroundTasks::empty(),
);
}
let boxed: Box<dyn PluginPayload> = Box::new(payload);
snapshot
.executor
.execute(
&entries,
boxed,
extensions,
context_table,
&self.task_tracker,
)
.await
}
/// Invoke a typed hook by explicit name.
///
/// Combines compile-time payload type checking (from `H`) with
/// runtime hook name routing (from `hook_name`). Use this when
/// a single hook type (e.g., `CmfHook`) covers multiple hook
/// names (e.g., `cmf.tool_pre_invoke`, `cmf.tool_post_invoke`).
///
/// # Type Parameters
///
/// - `H` — the hook type (provides payload type checking).
///
/// # Arguments
///
/// * `hook_name` — the hook name for dispatch routing.
/// * `payload` — the typed payload (compile-time checked against `H::Payload`).
/// * `extensions` — the full extensions.
/// * `context_table` — optional context table from a previous hook.
///
/// # Examples
///
/// ```rust,ignore
/// // Compile-time: payload must be MessagePayload (from CmfHook)
/// // Runtime: dispatches to plugins registered under "cmf.tool_pre_invoke"
/// let (result, bg) = mgr.invoke_named::<CmfHook>(
/// "cmf.tool_pre_invoke", payload, ext, None,
/// ).await;
/// ```
pub async fn invoke_named<H: HookTypeDef>(
&self,
hook_name: &str,
payload: H::Payload,
extensions: Extensions,
context_table: Option<PluginContextTable>,
) -> (PipelineResult, BackgroundTasks) {
let snapshot = self.load_runtime();
let hook_type = HookType::new(hook_name);
let all_entries = snapshot.registry.entries_for_hook(&hook_type);
// No registered entries AND no route annotations → nothing to
// do. Allow-and-pass-through. We can't short-circuit on
// `all_entries.is_empty()` alone, because route annotations
// (external-orchestrator handlers from APL / future Rego /
// Cedar-direct) can produce a single-entry dispatch even when
// no plugin was registered on the hook directly.
if all_entries.is_empty() && snapshot.route_annotations.is_empty() {
let boxed: Box<dyn PluginPayload> = Box::new(payload);
return (
PipelineResult::allowed_with(boxed, extensions, context_table.unwrap_or_default()),
BackgroundTasks::empty(),
);
}
let entries = self
.filter_entries_by_route(&snapshot, all_entries, &extensions, hook_name)
.await;
if entries.is_empty() {
let boxed: Box<dyn PluginPayload> = Box::new(payload);
return (
PipelineResult::allowed_with(boxed, extensions, context_table.unwrap_or_default()),
BackgroundTasks::empty(),
);
}
let boxed: Box<dyn PluginPayload> = Box::new(payload);
snapshot
.executor
.execute(
&entries,
boxed,
extensions,
context_table,
&self.task_tracker,
)
.await
}
/// Find every (hook_name, HookEntry) pair belonging to the named
/// plugin. Returns an empty `Vec` if the plugin isn't registered.
///
/// Used by external orchestrators (notably apl-cpex) that decide
/// the per-route plugin lineup themselves and need handler refs +
/// trusted_config to build pre-resolved dispatch plans. Cheaper than
/// going through `invoke_named` per request because the caller can
/// cache the resulting entries — pair the result with
/// [`config_generation`](Self::config_generation) to invalidate the
/// cache on snapshot swaps.
///
/// Bypasses route/entity filtering — caller has already decided this
/// plugin should run. APL's `routes:` is itself the authoritative
/// lineup; cpex-core's condition-based routing is a parallel model
/// for non-APL hosts.
pub fn find_plugin_entries(
&self,
plugin_name: &str,
) -> Vec<(String, crate::registry::HookEntry)> {
let snapshot = self.load_runtime();
snapshot.registry.entries_for_plugin(plugin_name)
}
/// Dispatch a caller-supplied slice of HookEntries through the
/// executor's full 5-phase pipeline (sequential, transform, audit,
/// concurrent, fire-and-forget). All on_error / timeout / mode /
/// write-token machinery applies.
///
/// Bypasses hook-name lookup and route/entity filtering — caller has
/// already resolved the lineup (typically via
/// [`find_plugin_entries`](Self::find_plugin_entries) + a per-route
/// dispatch plan). The `H: HookTypeDef` parameter enforces payload
/// type at compile time; mismatched payloads fail to compile, same
/// as [`invoke_named`](Self::invoke_named).
///
/// Returns `(PipelineResult, BackgroundTasks)` identical in shape to
/// `invoke_named` so callers can swap between the two paths without
/// rewriting downstream result handling.
pub async fn invoke_entries<H: HookTypeDef>(
&self,
entries: &[crate::registry::HookEntry],
payload: H::Payload,
extensions: Extensions,
context_table: Option<PluginContextTable>,
) -> (PipelineResult, BackgroundTasks) {
if entries.is_empty() {
let boxed: Box<dyn PluginPayload> = Box::new(payload);
return (
PipelineResult::allowed_with(boxed, extensions, context_table.unwrap_or_default()),
BackgroundTasks::empty(),
);
}
let snapshot = self.load_runtime();
let boxed: Box<dyn PluginPayload> = Box::new(payload);
snapshot
.executor
.execute(
entries,
boxed,
extensions,
context_table,
&self.task_tracker,
)
.await
}
// -----------------------------------------------------------------------
// Route Annotation
// -----------------------------------------------------------------------
/// Override the resolved plugin list for one `(entity_type, entity_name)`
/// pair on the listed hooks with a single synthetic handler. The handler
/// takes responsibility for any further plugin dispatch within itself
/// (typically by calling [`invoke_entries`](Self::invoke_entries) against
/// the same registry's other entries — i.e. APL's `plugin(name)` →
/// `CmfPluginInvoker` → `invoke_entries` flow).
///
/// This is the integration point external orchestrators (APL, future
/// Rego/Cedar-direct/Custom) use to drive plugins via their own
/// semantics instead of cpex-core's imperative `routes.*.plugins:`
/// chain. Bumps the config generation so cached dispatch plans in
/// downstream caches invalidate.
///
/// `config` provides the trusted_config for the synthetic plugin —
/// the executor reads `mode`, `on_error`, `capabilities`, etc. from
/// it the same way it does for any other registered plugin. Capabilities
/// should be a *superset* of what the orchestrator needs to read from
/// `Extensions` (cpex-core's per-plugin filter still applies to the
/// synthetic handler).
///
/// The underlying `plugins:` chain for this route is *not* removed —
/// those plugins stay discoverable via [`find_plugin_entries`](Self::find_plugin_entries)
/// so the orchestrator can dispatch into them by name.
pub fn annotate_route<H>(
&self,
entity_type: impl Into<String>,
entity_name: impl Into<String>,
scope: Option<String>,
hook_name: impl Into<String>,
handler: Arc<H>,
config: crate::plugin::PluginConfig,
) where
H: crate::plugin::Plugin + crate::registry::AnyHookHandler + 'static,
{
let key = AnnotationKey {
entity_type: entity_type.into(),
entity_name: entity_name.into(),
scope,
hook_name: hook_name.into(),
};
let plugin_ref = Arc::new(crate::registry::PluginRef::new(
handler.clone() as Arc<dyn crate::plugin::Plugin>,
config,
));
let entry = crate::registry::HookEntry {
plugin_ref,
handler: handler as Arc<dyn crate::registry::AnyHookHandler>,
};
self.mutate_runtime(|snap| {
snap.route_annotations.insert(key, entry);
});
}
/// Remove a route annotation for a specific hook. No-op when no
/// annotation exists for the key. Bumps the generation so downstream
/// caches invalidate.
pub fn remove_route_annotation(
&self,
entity_type: &str,
entity_name: &str,
scope: Option<&str>,
hook_name: &str,
) {
let key = AnnotationKey {
entity_type: entity_type.to_string(),
entity_name: entity_name.to_string(),
scope: scope.map(str::to_string),
hook_name: hook_name.to_string(),
};
self.mutate_runtime(|snap| {
snap.route_annotations.remove(&key);
});
}
// -----------------------------------------------------------------------
// Route Filtering
// -----------------------------------------------------------------------
/// Filter hook entries based on route resolution, with caching.
///
/// When routing is enabled and extensions.meta provides entity
/// identification, resolves the route and returns only the entries
/// for plugins that match. Results are cached by
/// `(entity_type, entity_name, hook_name, scope)` — subsequent
/// calls for the same key return an `Arc` to the cached entries
/// (refcount bump, no data copy).
///
/// When routing is disabled or meta is absent, returns all entries.
async fn filter_entries_by_route(
&self,
snapshot: &RuntimeSnapshot,
entries: &[crate::registry::HookEntry],
extensions: &Extensions,
hook_name: &str,
) -> Arc<Vec<crate::registry::HookEntry>> {
// Route annotation short-circuit: if the request's
// (entity_type, entity_name) has an annotation that handles this
// hook, return a one-entry list containing the annotated handler.
// External orchestrators (APL via apl-cpex; future Rego/Cedar)
// register annotations to drive plugin dispatch under their own
// semantics instead of cpex-core's imperative chain. Underlying
// `plugins:` entries stay in the registry for the orchestrator
// to dispatch into by-name via `invoke_entries`.
if !snapshot.route_annotations.is_empty() {
if let Some(meta) = &extensions.meta {
if let (Some(et), Some(en)) = (&meta.entity_type, &meta.entity_name) {
// Scoped lookup first (specific wins); unscoped lookup
// falls back as a "global default" — matches the
// specificity tiebreaker `find_matching_route` uses.
// Lookup is keyed on the hook name as well, so a route
// can install distinct handlers per phase.
let scoped = meta.scope.as_ref().and_then(|s| {
snapshot.route_annotations.get(&AnnotationKey {
entity_type: et.clone(),
entity_name: en.clone(),
scope: Some(s.clone()),
hook_name: hook_name.to_string(),
})
});
let candidate = scoped.or_else(|| {
snapshot.route_annotations.get(&AnnotationKey {
entity_type: et.clone(),
entity_name: en.clone(),
scope: None,
hook_name: hook_name.to_string(),
})
});
if let Some(entry) = candidate {
return Arc::new(vec![entry.clone()]);
}
}
}
}
// Routing disabled (or no config): fall back to per-plugin
// condition filtering. Empty conditions Vec means "fire always",
// so this is backward-compatible with configs that don't use
// conditions. Mirrors the Python implementation.
let cpex_config = match &snapshot.cpex_config {
Some(c) if c.routing_enabled() => c,
_ => {
let filtered: Vec<_> = entries
.iter()
.filter(|e| e.plugin_ref.trusted_config().passes_conditions(extensions))
.cloned()
.collect();
return Arc::new(filtered);
},
};
// Extract entity info from meta extension
let meta = match &extensions.meta {
Some(m) => m,
None => return Arc::new(entries.to_vec()),
};
let (entity_type, entity_name) = match (&meta.entity_type, &meta.entity_name) {
(Some(t), Some(n)) => (t.as_str(), n.as_str()),
_ => return Arc::new(entries.to_vec()),
};
let request_scope = meta.scope.as_deref();
// Fast path: zero-allocation cache lookup with raw_entry
let hash = {
use std::hash::BuildHasher;
let mut hasher = self.cache_hasher.build_hasher();
entity_type.hash(&mut hasher);
entity_name.hash(&mut hasher);
hook_name.hash(&mut hasher);
request_scope.hash(&mut hasher);
hasher.finish()
};
{
// Recover from poisoning: a panic in another thread while holding
// this lock leaves the cache flagged poisoned. The cache's contents
// are still valid (HashMap operations are panic-safe and stale
// entries are healed by `clear_routing_cache()`), so we don't want
// a one-time panic to permanently disable dispatch. Same idiom
// applies to all four lock sites in this file.
let cache = self
.route_cache
.read()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if let Some((_, cached)) = cache.raw_entry().from_hash(hash, |key| {
key.entity_type == entity_type
&& key.entity_name == entity_name
&& key.hook_name == hook_name
&& key.scope.as_deref() == request_scope
}) {
return Arc::clone(cached);
}
}
// Slow path: resolve, filter, and cache (allocations only here).
//
// Hook-specific resolution for identity.resolve: the route's
// `identity:` block is the authoritative dispatch list (NOT
// the `plugins:` block, which in APL-driven routes means
// "per-route overrides" rather than "binding"). For every
// other hook, the generic plugins-block resolution applies.
let resolved = if hook_name == crate::identity::HOOK_IDENTITY_RESOLVE {
config::resolve_identity_plugins_for_route(
cpex_config,
entity_type,
entity_name,
request_scope,
)
} else {
config::resolve_plugins_for_entity(
cpex_config,
entity_type,
entity_name,
request_scope,
&meta.tags,
)
};
// Filter entries to resolved plugins, preserving resolution order.
// If a plugin has config overrides and we have a factory for its kind,
// create a new instance with the merged config.
let mut filtered = Vec::new();
for resolved_plugin in &resolved {
if let Some(entry) = entries
.iter()
.find(|e| e.plugin_ref.name() == resolved_plugin.name)
{
if let Some(overrides) = &resolved_plugin.config_overrides {
// Try to create an override instance
if let Some(override_entry) =
self.create_override_instance(entry, overrides).await
{
filtered.push(override_entry);
continue;
}
}
filtered.push(entry.clone());
}
}
let cached = Arc::new(filtered);
// Store in cache — owned key allocated only on cache miss.
// Reject-on-full: when the cache is at capacity we still return
// the freshly resolved Vec but skip memoization, bounding memory
// growth from attacker-controlled entity names.
let cache_key = RouteCacheKey {
entity_type: entity_type.to_string(),
entity_name: entity_name.to_string(),
hook_name: hook_name.to_string(),
scope: meta.scope.clone(),
};
// Decide under the lock; log outside it so I/O doesn't block readers.
// One warn per fill cycle — prevents log spam under DoS.
let should_warn = {
let mut cache = self
.route_cache
.write()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if cache.len() >= snapshot.route_cache_max_entries {
!self.route_cache_full_warned.swap(true, Ordering::AcqRel)
} else {
cache.insert(cache_key, Arc::clone(&cached));
false
}
};
if should_warn {
warn!(
max_entries = snapshot.route_cache_max_entries,
"Routing cache at capacity — further routes will not be cached. \
Increase plugin_settings.route_cache_max_entries or \
investigate entity name growth.",
);
}
cached
}
/// Build per-hook `HookEntry`s for a plugin with optional route-
/// level overrides. Used by external orchestrators (notably
/// apl-cpex's dispatch plan) that need to splice per-route plugin
/// variants — different `config`, narrower `capabilities`, different
/// `on_error` — into the dispatch lineup while keeping cpex-core
/// the source of truth for instantiation and isolation.
///
/// Behavior:
/// - **All three overrides `None`:** returns the base entries
/// unchanged. Caller can use them as-is.
/// - **Only `capabilities_override` / `on_error_override` set
/// (`config_override` is `None`):** builds new `PluginRef`s
/// sharing the *base plugin `Arc`* with a merged `TrustedConfig`
/// (override caps / on_error replace base values) and an
/// independent circuit breaker. Cheap — no factory call.
/// - **`config_override` set:** invokes the registered factory for
/// the plugin's `kind` with a merged `PluginConfig` (override
/// `config` *replaces* base `config` wholesale per unified-config
/// spec — not deep merge), calls `initialize()` on the new
/// instance, and wraps every returned handler in a new
/// `PluginRef` with a fresh circuit breaker.
///
/// Returns an empty `Vec` when:
/// - the plugin name isn't registered in the manager,
/// - the factory for the plugin's `kind` is missing,
/// - the factory's `create` errors,
/// - or `initialize()` fails on the new instance.
///
/// Each of those is a configuration / wiring fault the caller
/// should treat as `NotFound` at dispatch time. The method logs
/// the underlying error before returning empty so debugging
/// surfaces in operator logs rather than as a silent miss.
pub async fn build_override_entries(
&self,
plugin_name: &str,
config_override: Option<&serde_yaml::Value>,
capabilities_override: Option<&std::collections::HashSet<String>>,
on_error_override: Option<crate::plugin::OnError>,
) -> Vec<(String, crate::registry::HookEntry)> {
let base_entries = self.find_plugin_entries(plugin_name);
if base_entries.is_empty() {
return Vec::new();
}
// No overrides at all — caller can use base entries unchanged.
if config_override.is_none()
&& capabilities_override.is_none()
&& on_error_override.is_none()
{
return base_entries;
}
// Pull the base trusted_config off any of the base entries —
// all of them share the same `Arc<PluginRef>` for a given
// plugin name, so picking the first is fine.
let base_ref = Arc::clone(&base_entries[0].1.plugin_ref);
let mut merged_config = base_ref.trusted_config().clone();
// Capabilities: override replaces base when present.
if let Some(caps) = capabilities_override {
merged_config.capabilities = caps.clone();
}
// on_error: override replaces base when present.
if let Some(oe) = on_error_override {
merged_config.on_error = oe;
}
// Caps/on_error-only path — shared base plugin Arc, new
// PluginRef with merged config + fresh circuit breaker.
// No factory call, no async work.
if config_override.is_none() {
let new_ref = Arc::new(crate::registry::PluginRef::new(
Arc::clone(base_ref.plugin()),
merged_config,
));
return base_entries
.into_iter()
.map(|(hook_name, base_entry)| {
(
hook_name,
crate::registry::HookEntry {
plugin_ref: Arc::clone(&new_ref),
handler: base_entry.handler,
},
)
})
.collect();
}
// Config override present — factory path. Convert YAML
// override value into the JSON shape `PluginConfig.config`
// carries (YAML is a superset of JSON so serde re-serialization
// is safe). Per spec, override `config` replaces the base
// `config` wholesale.
let cfg_yaml = config_override.expect("checked above");
let cfg_json = match serde_json::to_value(cfg_yaml) {
Ok(v) => v,
Err(e) => {
error!(
plugin = %plugin_name,
error = %e,
"build_override_entries: YAML→JSON config conversion failed",
);
return Vec::new();
},
};
merged_config.config = Some(cfg_json);
let kind = merged_config.kind.clone();
let instance = {
let factories = self.factories.read().unwrap_or_else(|p| p.into_inner());
let factory = match factories.get(&kind) {
Some(f) => f,
None => {
error!(
plugin = %plugin_name,
kind = %kind,
"build_override_entries: no factory registered for kind",
);
return Vec::new();
},
};
match factory.create(&merged_config) {
Ok(i) => i,
Err(e) => {
error!(
plugin = %plugin_name,
error = %e,
"build_override_entries: factory.create failed",
);
return Vec::new();
},
}
};
if let Err(e) = instance.plugin.initialize().await {
error!(
plugin = %plugin_name,
error = %e,
"build_override_entries: initialize() failed on new instance",
);
return Vec::new();
}
// One PluginRef shared across the new instance's handlers —
// all hooks served by one instance share a circuit breaker
// (matches registration semantics).
let new_ref = Arc::new(crate::registry::PluginRef::new(
Arc::clone(&instance.plugin),
merged_config,
));
instance
.handlers
.into_iter()
.map(|(hook_name, handler)| {
(
hook_name.to_string(),
crate::registry::HookEntry {
plugin_ref: Arc::clone(&new_ref),
handler,
},
)
})
.collect()
}
/// Create an override plugin instance with merged config.
///
/// When a route overrides a plugin's config, we create a new
/// instance via the factory with the merged config and call
/// `initialize()` on it so plugins that open DB connections / file
/// handles / network clients run their setup.
///
/// The override gets its OWN circuit breaker (`disabled` flag) and
/// its own UUID, independent of the base. Config is part of the
/// failure surface — an override with a bad connection string /
/// wrong credentials / wrong limit value can fail for reasons that
/// have nothing to do with the base's reliability. Coupling them
/// would let a config-specific failure on one route silently
/// disable the plugin on every other route, which is the opposite
/// of the per-route blast-radius guarantee operators reach for
/// overrides to get. The fresh UUID also keys the override's
/// `local_state` in the context table, isolating per-instance
/// state from the base for the same reason.
///
/// Returns `None` (and the caller falls back to the base entry) if:
/// - no factory is available for the plugin's kind,
/// - the factory fails to create the instance,
/// - the new instance has no handler for the target hook,
/// - or `initialize()` fails on the new instance.
async fn create_override_instance(
&self,
base_entry: &crate::registry::HookEntry,
overrides: &serde_json::Value,
) -> Option<crate::registry::HookEntry> {
let base_config = base_entry.plugin_ref.trusted_config();
let kind = &base_config.kind;
// Merge: start with base config, overlay with overrides
let mut merged_config = base_config.clone();
if let Some(override_config) = overrides.get("config") {
// Merge the plugin-specific config section
if let Some(base_plugin_config) = &merged_config.config {
let mut merged = base_plugin_config.clone();
if let (Some(base_obj), Some(override_obj)) =
(merged.as_object_mut(), override_config.as_object())
{
for (key, value) in override_obj {
base_obj.insert(key.clone(), value.clone());
}
}
merged_config.config = Some(merged);
} else {
merged_config.config = Some(override_config.clone());
}
}
// Create new instance with merged config — hold the factories
// read lock just long enough to construct the instance, then drop
// it before any `.await` so we never hold a sync lock across awaits.
let target_hook = base_entry.handler.hook_type_name();
let instance = {
let factories = self.factories.read().unwrap_or_else(|p| p.into_inner());
let factory = match factories.get(kind) {
Some(f) => f,
None => return None,
};
match factory.create(&merged_config) {
Ok(i) => i,
Err(e) => {
error!(
"Failed to create override instance for '{}': {}",
base_config.name, e
);
return None; // fall back to base instance
},
}
};
// Find the handler matching the current hook before consuming
// the instance so we don't pay for initialization on a doomed instance.
let handler = instance
.handlers
.into_iter()
.find(|(name, _)| *name == target_hook)
.map(|(_, h)| h);
let handler = match handler {
Some(h) => h,
None => {
warn!(
"Override instance for '{}' has no handler for hook '{}'",
base_config.name, target_hook
);
return None;
},
};
// Initialize the new instance — without this, plugins that need to
// set up DB connections / file handles / network clients run with
// default state.
if let Err(e) = instance.plugin.initialize().await {
error!(
"Failed to initialize override instance for '{}': {} — falling back to base",
base_config.name, e
);
return None;
}
// Independent circuit breaker + fresh UUID per (kind, name, config)
// — see the doc comment above for why we don't share with the base.
// Arc-wrapped for cheap cloning under group_by_mode.
let plugin_ref = Arc::new(crate::registry::PluginRef::new(
instance.plugin,
merged_config,
));
Some(crate::registry::HookEntry {
plugin_ref,
handler,
})
}
/// Clear the routing cache. Call when config is reloaded or
/// plugins are registered/unregistered. Also resets the
/// "cache full" warn-once latch so the next fill cycle can warn again.
pub fn clear_routing_cache(&self) {
let mut cache = self
.route_cache
.write()
.unwrap_or_else(|poisoned| poisoned.into_inner());
cache.clear();
self.route_cache_full_warned.store(false, Ordering::Release);
}
/// Number of entries in the routing cache.
pub fn routing_cache_size(&self) -> usize {
self.route_cache
.read()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.len()
}
// -----------------------------------------------------------------------
// Query Methods
// -----------------------------------------------------------------------
/// Whether anything would run for the given hook name — either a
/// registered plugin handler OR a route annotation targeting that hook.
///
/// Route annotations (installed by APL from a route's `policy:` /
/// `args:` / `result:` blocks) must be counted here: a route whose only
/// handler for a phase is an annotation (e.g. a response-side
/// `result: { ssn: redact(...) }` on `cmf.tool_post_invoke`, with no
/// globally-registered post-invoke plugin) would otherwise report
/// "no hooks" and be skipped by out-of-process hosts that use this as a
/// fast-skip gate — silently dropping the route's policy for that phase.
pub fn has_hooks_for(&self, hook_name: &str) -> bool {
let snapshot = self.load_runtime();
snapshot.registry.has_hooks_for(&HookType::new(hook_name))
|| snapshot
.route_annotations
.keys()
.any(|k| k.hook_name.as_str() == hook_name)
}
/// Look up a plugin by name. Returns an `Arc<PluginRef>` clone — works
/// with the snapshot-based dispatch model where the registry sits
/// behind a transient `Arc<RuntimeSnapshot>` guard. `Arc<PluginRef>`
/// derefs to `PluginRef`, so callers can chain methods directly:
/// `mgr.get_plugin("name").unwrap().is_disabled()` still compiles.
pub fn get_plugin(&self, name: &str) -> Option<Arc<PluginRef>> {
self.load_runtime().registry.get(name)
}
/// Total number of registered plugins.
pub fn plugin_count(&self) -> usize {
self.load_runtime().registry.plugin_count()
}
/// All registered plugin names (owned, not borrowed from the registry).
pub fn plugin_names(&self) -> Vec<String> {
self.load_runtime().registry.plugin_names()
}
/// Whether the manager has been initialized.
pub fn is_initialized(&self) -> bool {
self.initialized.load(Ordering::Acquire)
}
/// Unregister a plugin by name.
pub fn unregister(&self, name: &str) -> Option<Arc<PluginRef>> {
let removed = self.mutate_runtime(|snap| snap.registry.unregister(name));
if removed.is_some() {
self.clear_routing_cache();
}
removed
}
}
impl Default for PluginManager {
fn default() -> Self {
Self::new(ManagerConfig::default())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::context::PluginContext;
use crate::error::PluginViolation;
use crate::hooks::payload::Extensions;
use crate::hooks::{HookHandler, PluginResult};
use crate::plugin::{OnError, PluginMode};
use async_trait::async_trait;
// -- Test payload --
#[derive(Debug, Clone)]
struct TestPayload {
value: String,
}
crate::impl_plugin_payload!(TestPayload);
// -- Test hook type --
struct TestHook;
impl HookTypeDef for TestHook {
type Payload = TestPayload;
type Result = PluginResult<TestPayload>;
const NAME: &'static str = "test_hook";
}
// -- Test plugins: implement Plugin + HookHandler<TestHook> --
// No AnyHookHandler boilerplate — the framework handles it.
/// Plugin that allows everything.
struct AllowPlugin {
cfg: PluginConfig,
}
#[async_trait]
impl Plugin for AllowPlugin {
fn config(&self) -> &PluginConfig {
&self.cfg
}
async fn initialize(&self) -> Result<(), Box<PluginError>> {
Ok(())
}
async fn shutdown(&self) -> Result<(), Box<PluginError>> {
Ok(())
}
}
impl HookHandler<TestHook> for AllowPlugin {
async fn handle(
&self,
_payload: &TestPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> PluginResult<TestPayload> {
PluginResult::allow()
}
}
/// Plugin that denies everything.
struct DenyPlugin {
cfg: PluginConfig,
}
#[async_trait]
impl Plugin for DenyPlugin {
fn config(&self) -> &PluginConfig {
&self.cfg
}
async fn initialize(&self) -> Result<(), Box<PluginError>> {
Ok(())
}
async fn shutdown(&self) -> Result<(), Box<PluginError>> {
Ok(())
}
}
impl HookHandler<TestHook> for DenyPlugin {
async fn handle(
&self,
_payload: &TestPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> PluginResult<TestPayload> {
PluginResult::deny(PluginViolation::new("denied", "test denial"))
}
}
/// Handler that always returns an error (for testing on_error behavior).
struct ErrorHandler;
#[async_trait]
impl AnyHookHandler for ErrorHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
Err(Box::new(PluginError::Execution {
plugin_name: "error-plugin".into(),
message: "simulated failure".into(),
source: None,
code: None,
details: std::collections::HashMap::new(),
proto_error_code: None,
}))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
// -- Helpers --
fn make_config(name: &str, priority: i32, mode: PluginMode) -> PluginConfig {
make_config_with_on_error(name, priority, mode, OnError::Fail)
}
fn make_config_with_on_error(
name: &str,
priority: i32,
mode: PluginMode,
on_error: OnError,
) -> PluginConfig {
PluginConfig {
name: name.to_string(),
kind: "test".to_string(),
description: None,
author: None,
version: None,
hooks: vec!["test_hook".to_string()],
mode,
priority,
on_error,
capabilities: Default::default(),
tags: Vec::new(),
conditions: Vec::new(),
config: None,
}
}
fn make_config_with_conditions(
name: &str,
conditions: Vec<crate::plugin::PluginCondition>,
) -> PluginConfig {
let mut cfg = make_config(name, 10, PluginMode::Sequential);
cfg.conditions = conditions;
cfg
}
// -- Tests --
#[tokio::test]
async fn test_manager_lifecycle() {
let mgr = PluginManager::default();
assert!(!mgr.is_initialized());
assert_eq!(mgr.plugin_count(), 0);
mgr.initialize().await.unwrap();
assert!(mgr.is_initialized());
// Idempotent
mgr.initialize().await.unwrap();
mgr.shutdown().await;
assert!(!mgr.is_initialized());
}
#[tokio::test]
async fn test_invoke_by_name_no_plugins() {
let mgr = PluginManager::default();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
assert!(result.modified_payload.is_some());
}
#[tokio::test]
async fn test_invoke_by_name_allow() {
let mgr = PluginManager::default();
let config = make_config("allow-plugin", 10, PluginMode::Sequential);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
// Clean registration — no AnyHookHandler needed
mgr.register_handler::<TestHook, _>(plugin, config).unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
}
#[tokio::test]
async fn test_invoke_by_name_deny() {
let mgr = PluginManager::default();
let config = make_config("deny-plugin", 10, PluginMode::Sequential);
let plugin = Arc::new(DenyPlugin {
cfg: config.clone(),
});
mgr.register_handler::<TestHook, _>(plugin, config).unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(!result.continue_processing);
assert_eq!(result.violation.as_ref().unwrap().code, "denied");
}
#[tokio::test]
async fn test_invoke_typed() {
let mgr = PluginManager::default();
let config = make_config("allow-plugin", 10, PluginMode::Sequential);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
mgr.register_handler::<TestHook, _>(plugin, config).unwrap();
mgr.initialize().await.unwrap();
let payload = TestPayload {
value: "typed".into(),
};
let (result, _) = mgr
.invoke::<TestHook>(payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
}
#[tokio::test]
async fn test_invoke_named() {
// invoke_named::<H>(hook_name, ...) gives compile-time payload
// type checking while routing to a specific hook name.
let mgr = PluginManager::default();
let config = make_config("allow-plugin", 10, PluginMode::Sequential);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
mgr.register_handler::<TestHook, _>(plugin, config).unwrap();
mgr.initialize().await.unwrap();
let payload = TestPayload {
value: "named".into(),
};
// TestHook::NAME is "test_hook" — invoke_named routes by the
// explicit hook_name parameter, not H::NAME
let (result, _) = mgr
.invoke_named::<TestHook>("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
}
#[tokio::test]
async fn test_invoke_named_no_plugins_for_hook() {
// invoke_named with a hook name that has no registered plugins
let mgr = PluginManager::default();
let config = make_config("allow-plugin", 10, PluginMode::Sequential);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
mgr.register_handler::<TestHook, _>(plugin, config).unwrap();
mgr.initialize().await.unwrap();
let payload = TestPayload {
value: "no-match".into(),
};
// Plugin is registered under "test_hook", but we invoke "other_hook"
let (result, _) = mgr
.invoke_named::<TestHook>("other_hook", payload, Extensions::default(), None)
.await;
// No plugins fire — allowed by default
assert!(result.continue_processing);
}
#[tokio::test]
async fn test_invoke_named_deny() {
let mgr = PluginManager::default();
let config = make_config("deny-plugin", 10, PluginMode::Sequential);
let plugin = Arc::new(DenyPlugin {
cfg: config.clone(),
});
mgr.register_handler::<TestHook, _>(plugin, config).unwrap();
mgr.initialize().await.unwrap();
let payload = TestPayload {
value: "denied".into(),
};
let (result, _) = mgr
.invoke_named::<TestHook>("test_hook", payload, Extensions::default(), None)
.await;
assert!(!result.continue_processing);
assert_eq!(result.violation.as_ref().unwrap().code, "denied");
}
#[tokio::test]
async fn test_has_hooks_for() {
let mgr = PluginManager::default();
assert!(!mgr.has_hooks_for("test_hook"));
let config = make_config("p1", 10, PluginMode::Sequential);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
mgr.register_handler::<TestHook, _>(plugin, config).unwrap();
assert!(mgr.has_hooks_for("test_hook"));
assert!(!mgr.has_hooks_for("other_hook"));
}
/// When `routing_enabled` is `false` (the legacy / default mode),
/// each plugin's `conditions:` must be evaluated per request — a
/// non-matching condition should keep the plugin from firing.
/// Mirrors the Python implementation's per-plugin filtering.
#[tokio::test]
async fn test_conditions_filter_plugins_when_routing_disabled() {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc as StdArc;
let counts: StdArc<[AtomicUsize; 2]> =
StdArc::new([AtomicUsize::new(0), AtomicUsize::new(0)]);
struct CountingHandler {
idx: usize,
counts: StdArc<[AtomicUsize; 2]>,
}
#[async_trait]
impl AnyHookHandler for CountingHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
self.counts[self.idx].fetch_add(1, Ordering::SeqCst);
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let mgr = PluginManager::default();
// Plugin A: condition requires tool == "wanted_tool" — fires for matching requests.
let mut tools = std::collections::HashSet::new();
tools.insert("wanted_tool".to_string());
let cfg_a = make_config_with_conditions(
"plugin_a",
vec![crate::plugin::PluginCondition {
tools: Some(tools),
..Default::default()
}],
);
let plugin_a = Arc::new(AllowPlugin { cfg: cfg_a.clone() });
let handler_a: Arc<dyn AnyHookHandler> = Arc::new(CountingHandler {
idx: 0,
counts: StdArc::clone(&counts),
});
mgr.register_raw::<TestHook>(plugin_a, cfg_a, handler_a)
.unwrap();
// Plugin B: empty conditions — fires unconditionally.
let cfg_b = make_config("plugin_b", 20, PluginMode::Sequential);
let plugin_b = Arc::new(AllowPlugin { cfg: cfg_b.clone() });
let handler_b: Arc<dyn AnyHookHandler> = Arc::new(CountingHandler {
idx: 1,
counts: StdArc::clone(&counts),
});
mgr.register_raw::<TestHook>(plugin_b, cfg_b, handler_b)
.unwrap();
mgr.initialize().await.unwrap();
// Request 1: tool=wanted_tool → both A and B should fire.
let ext_match = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("wanted_tool".into()),
..Default::default()
})),
..Default::default()
};
let p: Box<dyn PluginPayload> = Box::new(TestPayload { value: "1".into() });
let _ = mgr.invoke_by_name("test_hook", p, ext_match, None).await;
assert_eq!(
counts[0].load(Ordering::SeqCst),
1,
"plugin_a should fire on matching tool"
);
assert_eq!(
counts[1].load(Ordering::SeqCst),
1,
"plugin_b should fire (no conditions)"
);
// Request 2: tool=other_tool → only B fires (A's condition rejects).
let ext_no_match = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("other_tool".into()),
..Default::default()
})),
..Default::default()
};
let p: Box<dyn PluginPayload> = Box::new(TestPayload { value: "2".into() });
let _ = mgr.invoke_by_name("test_hook", p, ext_no_match, None).await;
assert_eq!(
counts[0].load(Ordering::SeqCst),
1,
"plugin_a should NOT fire on non-matching tool"
);
assert_eq!(
counts[1].load(Ordering::SeqCst),
2,
"plugin_b should fire on every request"
);
}
/// `user_patterns` glob matches against `extensions.security.subject.id`.
/// Specifically: pattern `admin-*` matches `admin-alice` but not `user-bob`.
#[tokio::test]
async fn test_conditions_user_patterns_glob_filters() {
use std::sync::atomic::{AtomicUsize, Ordering};
static FIRED: AtomicUsize = AtomicUsize::new(0);
FIRED.store(0, Ordering::SeqCst);
struct CountHandler;
#[async_trait]
impl AnyHookHandler for CountHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
FIRED.fetch_add(1, Ordering::SeqCst);
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let mgr = PluginManager::default();
let cfg = make_config_with_conditions(
"admin_only",
vec![crate::plugin::PluginCondition {
user_patterns: Some(vec!["admin-*".to_string()]),
..Default::default()
}],
);
let plugin = Arc::new(AllowPlugin { cfg: cfg.clone() });
let handler: Arc<dyn AnyHookHandler> = Arc::new(CountHandler);
mgr.register_raw::<TestHook>(plugin, cfg, handler).unwrap();
mgr.initialize().await.unwrap();
let ext_with_user = |id: &str| Extensions {
security: Some(std::sync::Arc::new(crate::extensions::SecurityExtension {
subject: Some(crate::extensions::security::SubjectExtension {
id: Some(id.to_string()),
..Default::default()
}),
..Default::default()
})),
..Default::default()
};
let p: Box<dyn PluginPayload> = Box::new(TestPayload { value: "1".into() });
let _ = mgr
.invoke_by_name("test_hook", p, ext_with_user("admin-alice"), None)
.await;
assert_eq!(
FIRED.load(Ordering::SeqCst),
1,
"admin-alice should match admin-*"
);
let p: Box<dyn PluginPayload> = Box::new(TestPayload { value: "2".into() });
let _ = mgr
.invoke_by_name("test_hook", p, ext_with_user("user-bob"), None)
.await;
assert_eq!(
FIRED.load(Ordering::SeqCst),
1,
"user-bob should NOT match admin-*"
);
}
#[tokio::test]
async fn test_unregister() {
let mgr = PluginManager::default();
let config = make_config("removable", 10, PluginMode::Sequential);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
mgr.register_handler::<TestHook, _>(plugin, config).unwrap();
assert_eq!(mgr.plugin_count(), 1);
mgr.unregister("removable");
assert_eq!(mgr.plugin_count(), 0);
assert!(!mgr.has_hooks_for("test_hook"));
}
/// Wraps the manager in `Arc` and dispatches concurrently from many
/// tasks. Also issues a `register_handler` call mid-flight to prove
/// that runtime registration is safe alongside invocations — the whole
/// point of the `ArcSwap`-based snapshot redesign. Before this fix,
/// `register_*` was `&mut self`, so this pattern wouldn't even compile.
#[tokio::test]
async fn test_manager_arc_shareable_with_concurrent_dispatch_and_registration() {
use std::sync::atomic::{AtomicUsize, Ordering};
static INVOKE_COUNT: AtomicUsize = AtomicUsize::new(0);
INVOKE_COUNT.store(0, Ordering::SeqCst);
struct CountingHandler;
#[async_trait]
impl AnyHookHandler for CountingHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
INVOKE_COUNT.fetch_add(1, Ordering::SeqCst);
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let mgr = Arc::new(PluginManager::default());
// Register an initial plugin and initialize.
let cfg = make_config("p0", 10, PluginMode::Sequential);
let plugin: Arc<AllowPlugin> = Arc::new(AllowPlugin { cfg: cfg.clone() });
let handler: Arc<dyn AnyHookHandler> = Arc::new(CountingHandler);
mgr.register_raw::<TestHook>(plugin, cfg, handler).unwrap();
mgr.initialize().await.unwrap();
// Spawn N concurrent invokers; midway, register a second plugin
// from a different task — the snapshot swaps under their feet.
let n = 16;
let mut handles = Vec::with_capacity(n + 1);
for i in 0..n {
let mgr = Arc::clone(&mgr);
handles.push(tokio::spawn(async move {
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: format!("call-{}", i),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
}));
}
// Concurrent registration — proves register_handler works through &Arc.
{
let mgr = Arc::clone(&mgr);
handles.push(tokio::spawn(async move {
let cfg = make_config("p1-late", 20, PluginMode::Sequential);
let plugin: Arc<AllowPlugin> = Arc::new(AllowPlugin { cfg: cfg.clone() });
let handler: Arc<dyn AnyHookHandler> = Arc::new(CountingHandler);
mgr.register_raw::<TestHook>(plugin, cfg, handler).unwrap();
}));
}
for h in handles {
h.await.unwrap();
}
// At least the initial plugin ran for every invoke (some invokes
// may have raced past the registration and only seen the initial
// plugin; others may have seen both). The exact count depends on
// the race, but lower bound is `n` (one fire per invoke for p0).
assert!(INVOKE_COUNT.load(Ordering::SeqCst) >= n);
// Late registration is now visible.
assert_eq!(mgr.plugin_count(), 2);
}
#[tokio::test]
async fn test_audit_plugin_cannot_block() {
let mgr = PluginManager::default();
let config = make_config("audit-denier", 10, PluginMode::Audit);
let plugin = Arc::new(DenyPlugin {
cfg: config.clone(),
});
mgr.register_handler::<TestHook, _>(plugin, config).unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
// Audit mode — deny is suppressed, pipeline continues
assert!(result.continue_processing);
}
#[tokio::test]
async fn test_on_error_disable_skips_plugin_on_subsequent_invocations() {
let mgr = PluginManager::default();
// Register an error handler with on_error: Disable
let config =
make_config_with_on_error("flaky-plugin", 10, PluginMode::Sequential, OnError::Disable);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(ErrorHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
// Also register a normal allow plugin (lower priority = runs second)
let config2 = make_config("allow-plugin", 20, PluginMode::Sequential);
let plugin2 = Arc::new(AllowPlugin {
cfg: config2.clone(),
});
mgr.register_handler::<TestHook, _>(plugin2, config2)
.unwrap();
mgr.initialize().await.unwrap();
// First invocation — flaky plugin errors, gets disabled, pipeline continues
// because on_error is Disable (not Fail). allow-plugin still runs.
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "first".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
// Verify the plugin is now disabled
let plugin_ref = mgr.get_plugin("flaky-plugin").unwrap();
assert!(plugin_ref.is_disabled());
assert_eq!(plugin_ref.mode(), PluginMode::Disabled);
// Second invocation — flaky plugin should be skipped entirely
// (group_by_mode filters it out). Only allow-plugin runs.
let payload2: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "second".into(),
});
let (result2, _) = mgr
.invoke_by_name("test_hook", payload2, Extensions::default(), None)
.await;
assert!(result2.continue_processing);
}
#[tokio::test]
async fn test_on_error_ignore_continues_without_disabling() {
let mgr = PluginManager::default();
// Register an error handler with on_error: Ignore
let config =
make_config_with_on_error("flaky-plugin", 10, PluginMode::Sequential, OnError::Ignore);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(ErrorHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
// First invocation — plugin errors, ignored, pipeline continues
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
// Plugin should NOT be disabled — still in its original mode
let plugin_ref = mgr.get_plugin("flaky-plugin").unwrap();
assert!(!plugin_ref.is_disabled());
assert_eq!(plugin_ref.mode(), PluginMode::Sequential);
}
/// Errors from `on_error: ignore` plugins must surface in
/// `PipelineResult.errors` so callers can see swallowed failures
/// programmatically — not just in log output.
#[tokio::test]
async fn test_on_error_ignore_records_in_pipeline_errors() {
let mgr = PluginManager::default();
let config =
make_config_with_on_error("flaky-plugin", 10, PluginMode::Sequential, OnError::Ignore);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(ErrorHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "x".into() });
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
// Pipeline continued (Ignore policy)…
assert!(result.continue_processing);
// …but the swallowed error is in result.errors with structured fields.
assert_eq!(result.errors.len(), 1, "expected one error record");
let rec = &result.errors[0];
assert_eq!(rec.plugin_name, "error-plugin");
assert!(
rec.message.contains("simulated failure"),
"message lost: {}",
rec.message,
);
}
/// Errors from `on_error: disable` plugins must ALSO appear in
/// `PipelineResult.errors` (not just trip the circuit breaker).
#[tokio::test]
async fn test_on_error_disable_records_in_pipeline_errors() {
let mgr = PluginManager::default();
let config =
make_config_with_on_error("flaky-plugin", 10, PluginMode::Sequential, OnError::Disable);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(ErrorHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "x".into() });
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
assert_eq!(result.errors.len(), 1);
// Plugin was also disabled (the Disable policy's other effect).
assert!(mgr.get_plugin("flaky-plugin").unwrap().is_disabled());
}
#[tokio::test]
async fn test_on_error_fail_halts_pipeline() {
let mgr = PluginManager::default();
// Register an error handler with on_error: Fail (default)
let config =
make_config_with_on_error("strict-plugin", 10, PluginMode::Sequential, OnError::Fail);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(ErrorHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
// Invocation — plugin errors, pipeline halts with a violation
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(!result.continue_processing);
assert_eq!(result.violation.as_ref().unwrap().code, "plugin_error");
assert_eq!(
result.violation.as_ref().unwrap().plugin_name.as_deref(),
Some("strict-plugin"),
);
}
// -- Additional test plugins --
/// Plugin that modifies the payload (for Transform mode testing).
struct TransformPlugin {
cfg: PluginConfig,
}
#[async_trait]
impl Plugin for TransformPlugin {
fn config(&self) -> &PluginConfig {
&self.cfg
}
async fn initialize(&self) -> Result<(), Box<PluginError>> {
Ok(())
}
async fn shutdown(&self) -> Result<(), Box<PluginError>> {
Ok(())
}
}
impl HookHandler<TestHook> for TransformPlugin {
async fn handle(
&self,
payload: &TestPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> PluginResult<TestPayload> {
PluginResult::modify_payload(TestPayload {
value: format!("{}_transformed", payload.value),
})
}
}
/// Handler that sleeps (for timeout and fire-and-forget testing).
struct SlowHandler {
delay_ms: u64,
}
#[async_trait]
impl AnyHookHandler for SlowHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
tokio::time::sleep(std::time::Duration::from_millis(self.delay_ms)).await;
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
// -- Bug-covering tests --
#[tokio::test]
async fn test_transform_modifies_payload() {
let mgr = PluginManager::default();
let config = make_config("transformer", 10, PluginMode::Transform);
let plugin = Arc::new(TransformPlugin {
cfg: config.clone(),
});
mgr.register_handler::<TestHook, _>(plugin, config).unwrap();
mgr.initialize().await.unwrap();
let payload = TestPayload {
value: "original".into(),
};
let (result, _) = mgr
.invoke::<TestHook>(payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
let final_payload = result.modified_payload.unwrap();
let typed = final_payload
.as_any()
.downcast_ref::<TestPayload>()
.unwrap();
assert_eq!(typed.value, "original_transformed");
}
/// Transform phase is documented `can_block: No` (plugin.rs PluginMode
/// table). An `on_error: Fail` plugin error or timeout in Transform must
/// NOT halt the pipeline — non-blocking is non-blocking, regardless of
/// the plugin's stated on_error preference. Disable still works.
#[tokio::test]
async fn test_transform_on_error_fail_does_not_halt_pipeline() {
let mgr = PluginManager::default();
let config =
make_config_with_on_error("flaky-transform", 10, PluginMode::Transform, OnError::Fail);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(ErrorHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "x".into() });
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(
result.continue_processing,
"Transform on_error:Fail must not halt the pipeline (phase is non-blocking)",
);
assert!(result.violation.is_none());
}
/// Audit phase previously ignored `on_error` entirely, so an
/// `on_error: Disable` plugin would error forever without the circuit
/// breaker tripping. After the fix Audit honors Disable.
#[tokio::test]
async fn test_audit_on_error_disable_disables_plugin() {
let mgr = PluginManager::default();
let config =
make_config_with_on_error("flaky-audit", 10, PluginMode::Audit, OnError::Disable);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(ErrorHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
assert!(!mgr.get_plugin("flaky-audit").unwrap().is_disabled());
// Invoke once — handler errors, on_error=Disable, plugin must be
// disabled. Pipeline still returns success (Audit can't block).
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "x".into() });
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
assert!(
mgr.get_plugin("flaky-audit").unwrap().is_disabled(),
"Audit phase must honor on_error:Disable",
);
}
#[tokio::test]
async fn test_concurrent_multiple_plugins_all_run() {
use std::sync::atomic::{AtomicUsize, Ordering};
// Shared counter to prove both plugins actually ran
static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
CALL_COUNT.store(0, Ordering::SeqCst);
struct CountingHandler;
#[async_trait]
impl AnyHookHandler for CountingHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
// Small sleep to ensure both tasks are spawned before either finishes
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
CALL_COUNT.fetch_add(1, Ordering::SeqCst);
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let mgr = PluginManager::default();
let c1 = make_config("concurrent-1", 10, PluginMode::Concurrent);
let p1 = Arc::new(AllowPlugin { cfg: c1.clone() });
let h1: Arc<dyn AnyHookHandler> = Arc::new(CountingHandler);
mgr.register_raw::<TestHook>(p1, c1, h1).unwrap();
let c2 = make_config("concurrent-2", 20, PluginMode::Concurrent);
let p2 = Arc::new(AllowPlugin { cfg: c2.clone() });
let h2: Arc<dyn AnyHookHandler> = Arc::new(CountingHandler);
mgr.register_raw::<TestHook>(p2, c2, h2).unwrap();
mgr.initialize().await.unwrap();
let start = std::time::Instant::now();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
let elapsed = start.elapsed();
assert!(result.continue_processing);
assert_eq!(CALL_COUNT.load(Ordering::SeqCst), 2);
// If they ran in parallel, total time should be ~50ms, not ~100ms
assert!(
elapsed.as_millis() < 90,
"concurrent plugins ran serially: {}ms",
elapsed.as_millis()
);
}
/// A deny on one concurrent plugin should short-circuit the pipeline
/// AND cancel the slow plugin still running in another task. Previously
/// `join_all` waited for every task before noticing the deny, so
/// short_circuit_on_deny was a no-op in wall-clock terms and the slow
/// plugin completed its side effects after the pipeline returned.
#[tokio::test]
async fn test_concurrent_short_circuit_aborts_slow_plugin() {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
static SLOW_COMPLETED: AtomicUsize = AtomicUsize::new(0);
SLOW_COMPLETED.store(0, Ordering::SeqCst);
struct DenyImmediately;
#[async_trait]
impl AnyHookHandler for DenyImmediately {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
let result: PluginResult<TestPayload> =
PluginResult::deny(PluginViolation::new("denied", "fast deny"));
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
struct SlowSideEffect;
#[async_trait]
impl AnyHookHandler for SlowSideEffect {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
tokio::time::sleep(Duration::from_secs(2)).await;
// If the task isn't aborted at the sleep's await point,
// this fetch_add fires after the pipeline already returned.
SLOW_COMPLETED.fetch_add(1, Ordering::SeqCst);
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let mgr = PluginManager::default();
let cfg_deny = make_config("denier", 10, PluginMode::Concurrent);
let plugin_deny = Arc::new(AllowPlugin {
cfg: cfg_deny.clone(),
});
mgr.register_raw::<TestHook>(
plugin_deny,
cfg_deny,
Arc::new(DenyImmediately) as Arc<dyn AnyHookHandler>,
)
.unwrap();
let cfg_slow = make_config("slow", 20, PluginMode::Concurrent);
let plugin_slow = Arc::new(AllowPlugin {
cfg: cfg_slow.clone(),
});
mgr.register_raw::<TestHook>(
plugin_slow,
cfg_slow,
Arc::new(SlowSideEffect) as Arc<dyn AnyHookHandler>,
)
.unwrap();
mgr.initialize().await.unwrap();
// Pipeline must return quickly — the deny short-circuits before
// the 2s sleep completes.
let start = std::time::Instant::now();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "x".into() });
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
let elapsed = start.elapsed();
assert!(!result.continue_processing);
assert!(
elapsed < Duration::from_millis(500),
"pipeline should short-circuit on deny, but took {}ms (slow plugin not aborted)",
elapsed.as_millis(),
);
// Wait long enough that the slow plugin's sleep would have finished
// if it hadn't been aborted, then verify its side effect didn't fire.
tokio::time::sleep(Duration::from_millis(2_500)).await;
assert_eq!(
SLOW_COMPLETED.load(Ordering::SeqCst),
0,
"slow plugin's side effect ran after pipeline returned — task was not aborted",
);
}
/// short_circuit_on_deny=false: every concurrent plugin must run to
/// completion (no abort), and the earliest deny is returned at the end.
#[tokio::test]
async fn test_concurrent_no_short_circuit_runs_every_plugin() {
use std::sync::atomic::{AtomicUsize, Ordering};
static ALLOW_RAN: AtomicUsize = AtomicUsize::new(0);
ALLOW_RAN.store(0, Ordering::SeqCst);
struct DenyImmediately;
#[async_trait]
impl AnyHookHandler for DenyImmediately {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
let result: PluginResult<TestPayload> =
PluginResult::deny(PluginViolation::new("denied", "fast deny"));
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
struct AllowAndCount;
#[async_trait]
impl AnyHookHandler for AllowAndCount {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
ALLOW_RAN.fetch_add(1, Ordering::SeqCst);
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let config = ManagerConfig {
executor: crate::executor::ExecutorConfig {
timeout_seconds: 30,
short_circuit_on_deny: false,
},
route_cache_max_entries: DEFAULT_ROUTE_CACHE_MAX_ENTRIES,
};
let mgr = PluginManager::new(config);
let cfg_deny = make_config("denier", 10, PluginMode::Concurrent);
let plugin_deny = Arc::new(AllowPlugin {
cfg: cfg_deny.clone(),
});
mgr.register_raw::<TestHook>(
plugin_deny,
cfg_deny,
Arc::new(DenyImmediately) as Arc<dyn AnyHookHandler>,
)
.unwrap();
let cfg_allow = make_config("allow", 20, PluginMode::Concurrent);
let plugin_allow = Arc::new(AllowPlugin {
cfg: cfg_allow.clone(),
});
mgr.register_raw::<TestHook>(
plugin_allow,
cfg_allow,
Arc::new(AllowAndCount) as Arc<dyn AnyHookHandler>,
)
.unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "x".into() });
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
// Earliest deny is returned…
assert!(!result.continue_processing);
// …but the non-denying plugin must still have run (no abort).
assert_eq!(ALLOW_RAN.load(Ordering::SeqCst), 1);
}
/// Plugin handler that panics inside its async invoke. With tokio::spawn,
/// the panic surfaces as a JoinError on the task's JoinHandle.
struct PanicHandler;
#[async_trait]
impl AnyHookHandler for PanicHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
panic!("simulated panic in concurrent plugin task");
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
/// A panicking concurrent plugin with `on_error: Fail` must halt the
/// pipeline with a violation. Previously the JoinError was just logged
/// and the panic was silently swallowed.
///
/// Note: this test prints "thread 'tokio-runtime-worker' panicked at..."
/// to stderr — that's tokio reporting the captured panic. Expected.
#[tokio::test]
async fn test_concurrent_panic_with_on_error_fail_halts_pipeline() {
let mgr = PluginManager::default();
let cfg =
make_config_with_on_error("panic-plugin", 10, PluginMode::Concurrent, OnError::Fail);
let plugin = Arc::new(AllowPlugin { cfg: cfg.clone() });
let handler: Arc<dyn AnyHookHandler> = Arc::new(PanicHandler);
mgr.register_raw::<TestHook>(plugin, cfg, handler).unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "x".into() });
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(
!result.continue_processing,
"Fail must halt the pipeline on panic"
);
let v = result.violation.as_ref().expect("expected violation");
assert_eq!(v.code, "plugin_panic");
assert_eq!(v.plugin_name.as_deref(), Some("panic-plugin"));
}
/// A panicking concurrent plugin with `on_error: Disable` must trip
/// the plugin's circuit breaker so it's skipped on subsequent invokes.
/// A second non-panicking plugin in the same phase still runs.
#[tokio::test]
async fn test_concurrent_panic_with_on_error_disable_trips_circuit_breaker() {
use std::sync::atomic::{AtomicUsize, Ordering};
static SURVIVOR_CALLS: AtomicUsize = AtomicUsize::new(0);
SURVIVOR_CALLS.store(0, Ordering::SeqCst);
struct SurvivorHandler;
#[async_trait]
impl AnyHookHandler for SurvivorHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
SURVIVOR_CALLS.fetch_add(1, Ordering::SeqCst);
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let mgr = PluginManager::default();
let panic_cfg =
make_config_with_on_error("panic-plugin", 10, PluginMode::Concurrent, OnError::Disable);
let panic_plugin = Arc::new(AllowPlugin {
cfg: panic_cfg.clone(),
});
let panic_handler: Arc<dyn AnyHookHandler> = Arc::new(PanicHandler);
mgr.register_raw::<TestHook>(panic_plugin, panic_cfg, panic_handler)
.unwrap();
let survivor_cfg = make_config("survivor", 20, PluginMode::Concurrent);
let survivor_plugin = Arc::new(AllowPlugin {
cfg: survivor_cfg.clone(),
});
let survivor_handler: Arc<dyn AnyHookHandler> = Arc::new(SurvivorHandler);
mgr.register_raw::<TestHook>(survivor_plugin, survivor_cfg, survivor_handler)
.unwrap();
mgr.initialize().await.unwrap();
// First invoke — panic plugin panics, gets disabled. Survivor still runs.
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "1".into() });
let (result1, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(
result1.continue_processing,
"Disable must not halt the pipeline"
);
assert_eq!(SURVIVOR_CALLS.load(Ordering::SeqCst), 1);
assert!(
mgr.get_plugin("panic-plugin").unwrap().is_disabled(),
"panic plugin must be disabled after the panic",
);
// Second invoke — disabled plugin is skipped, doesn't panic again.
let payload2: Box<dyn PluginPayload> = Box::new(TestPayload { value: "2".into() });
let (result2, _) = mgr
.invoke_by_name("test_hook", payload2, Extensions::default(), None)
.await;
assert!(result2.continue_processing);
// Survivor ran a second time; panic plugin did not.
assert_eq!(SURVIVOR_CALLS.load(Ordering::SeqCst), 2);
}
#[tokio::test]
async fn test_timeout_fires_on_slow_handler() {
// Create a manager with a very short timeout
let config = ManagerConfig {
executor: crate::executor::ExecutorConfig {
timeout_seconds: 1,
short_circuit_on_deny: true,
},
route_cache_max_entries: DEFAULT_ROUTE_CACHE_MAX_ENTRIES,
};
let mgr = PluginManager::new(config);
// Register a handler that sleeps longer than the timeout
let plugin_config = make_config("slow-plugin", 10, PluginMode::Sequential);
let plugin = Arc::new(AllowPlugin {
cfg: plugin_config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(SlowHandler { delay_ms: 5000 });
mgr.register_raw::<TestHook>(plugin, plugin_config, handler)
.unwrap();
mgr.initialize().await.unwrap();
let start = std::time::Instant::now();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
let elapsed = start.elapsed();
// Should have timed out and denied (on_error: Fail)
assert!(!result.continue_processing);
assert_eq!(result.violation.as_ref().unwrap().code, "plugin_timeout");
// Should have returned in ~1s, not 5s
assert!(
elapsed.as_secs() < 3,
"timeout didn't fire: {}s",
elapsed.as_secs()
);
}
#[tokio::test]
async fn test_fire_and_forget_returns_before_task_completes() {
use std::sync::atomic::{AtomicBool, Ordering};
static TASK_COMPLETED: AtomicBool = AtomicBool::new(false);
TASK_COMPLETED.store(false, Ordering::SeqCst);
struct SlowFireAndForgetHandler;
#[async_trait]
impl AnyHookHandler for SlowFireAndForgetHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
TASK_COMPLETED.store(true, Ordering::SeqCst);
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let mgr = PluginManager::default();
let config = make_config("fire-forget", 10, PluginMode::FireAndForget);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(SlowFireAndForgetHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, bg) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
// Pipeline should return immediately — before the background task finishes
assert!(result.continue_processing);
assert!(
!TASK_COMPLETED.load(Ordering::SeqCst),
"fire-and-forget task completed before pipeline returned"
);
// Wait for background tasks using wait_for_background_tasks()
let errors = bg.wait_for_background_tasks().await;
assert!(
errors.is_empty(),
"background task had errors: {:?}",
errors
);
assert!(
TASK_COMPLETED.load(Ordering::SeqCst),
"fire-and-forget task never completed"
);
}
/// `shutdown()` must wait for in-flight fire-and-forget tasks to drain
/// before returning, so audit / telemetry plugins that flush at the
/// end of a request lifetime aren't cancelled mid-write. The caller
/// drops `BackgroundTasks` (the common case for fire-and-forget),
/// so the only way the manager knows about the in-flight task is the
/// internal `TaskTracker`.
#[tokio::test]
async fn test_shutdown_drains_in_flight_fire_and_forget_tasks() {
use std::sync::atomic::{AtomicBool, Ordering};
static FAF_COMPLETED: AtomicBool = AtomicBool::new(false);
FAF_COMPLETED.store(false, Ordering::SeqCst);
struct SlowFafHandler;
#[async_trait]
impl AnyHookHandler for SlowFafHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
tokio::time::sleep(std::time::Duration::from_millis(150)).await;
FAF_COMPLETED.store(true, Ordering::SeqCst);
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let mgr = PluginManager::default();
let config = make_config("slow-faf", 10, PluginMode::FireAndForget);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(SlowFafHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
// Invoke and drop BackgroundTasks immediately — simulating the
// common case where the caller doesn't explicitly wait for FAF.
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "x".into() });
let (_result, _bg_dropped) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
// Task should still be in flight (sleeping 150ms).
assert!(!FAF_COMPLETED.load(Ordering::SeqCst));
// shutdown() must drain in-flight FAF tasks before returning.
mgr.shutdown().await;
// After shutdown, the FAF task must have run to completion.
assert!(
FAF_COMPLETED.load(Ordering::SeqCst),
"shutdown returned before fire-and-forget task finished — task was abandoned",
);
}
#[tokio::test]
async fn test_global_state_flows_between_serial_plugins() {
// Plugin A writes to global_state; Plugin B reads it.
struct WriterHandler;
#[async_trait]
impl AnyHookHandler for WriterHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
ctx.set_global("writer_was_here", serde_json::Value::Bool(true));
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
struct ReaderHandler {
saw_writer: std::sync::Arc<std::sync::atomic::AtomicBool>,
}
#[async_trait]
impl AnyHookHandler for ReaderHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
if ctx.get_global("writer_was_here").is_some() {
self.saw_writer
.store(true, std::sync::atomic::Ordering::SeqCst);
}
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let saw_writer = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let mgr = PluginManager::default();
// Writer runs first (priority 10)
let c1 = make_config("writer", 10, PluginMode::Sequential);
let p1 = Arc::new(AllowPlugin { cfg: c1.clone() });
let h1: Arc<dyn AnyHookHandler> = Arc::new(WriterHandler);
mgr.register_raw::<TestHook>(p1, c1, h1).unwrap();
// Reader runs second (priority 20)
let c2 = make_config("reader", 20, PluginMode::Sequential);
let p2 = Arc::new(AllowPlugin { cfg: c2.clone() });
let h2: Arc<dyn AnyHookHandler> = Arc::new(ReaderHandler {
saw_writer: saw_writer.clone(),
});
mgr.register_raw::<TestHook>(p2, c2, h2).unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
assert!(
saw_writer.load(std::sync::atomic::Ordering::SeqCst),
"reader plugin did not see writer's global_state change"
);
}
#[tokio::test]
async fn test_local_state_persists_across_hook_invocations() {
// Plugin writes to local_state on first hook call.
// Context table is threaded into second call — local_state preserved.
struct LocalWriterHandler;
#[async_trait]
impl AnyHookHandler for LocalWriterHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
// Increment a counter in local_state
let count = ctx
.get_local("call_count")
.and_then(|v| v.as_u64())
.unwrap_or(0);
ctx.set_local("call_count", serde_json::Value::from(count + 1));
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let mgr = PluginManager::default();
let config = make_config("counter", 10, PluginMode::Sequential);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(LocalWriterHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
// First invocation — no context table, starts fresh
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "first".into(),
});
let (result1, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result1.continue_processing);
// Check call_count = 1 in the returned context table
let table = &result1.context_table;
let local = table
.local_states
.values()
.next()
.expect("context table should have one local_state entry");
assert_eq!(local.get("call_count").unwrap().as_u64().unwrap(), 1);
// Second invocation — pass the context table from the first call
let payload2: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "second".into(),
});
let (result2, _) = mgr
.invoke_by_name(
"test_hook",
payload2,
Extensions::default(),
Some(result1.context_table),
)
.await;
assert!(result2.continue_processing);
// call_count should now be 2 — local_state persisted across invocations
let table2 = &result2.context_table;
let local2 = table2
.local_states
.values()
.next()
.expect("context table should have one local_state entry");
assert_eq!(local2.get("call_count").unwrap().as_u64().unwrap(), 2);
}
/// global_state writes by an earlier plugin must be visible to a later
/// plugin in the same serial phase, and the canonical state on the
/// returned context_table must reflect every plugin's contribution in
/// priority order. Previously this relied on `ctx_table.values().last()`
/// (HashMap iteration order — non-deterministic).
#[tokio::test]
async fn test_global_state_propagates_in_priority_order() {
/// Handler that appends `tag` to global_state["chain"] (creating
/// an array if absent). After running, the array reveals the
/// observed run order from each plugin's perspective.
struct GlobalChainHandler {
tag: &'static str,
}
#[async_trait]
impl AnyHookHandler for GlobalChainHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
let mut chain = ctx
.get_global("chain")
.and_then(|v| v.as_array())
.cloned()
.unwrap_or_default();
chain.push(serde_json::Value::String(self.tag.into()));
ctx.set_global("chain", serde_json::Value::Array(chain));
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let mgr = PluginManager::default();
// Plugin A — priority 10 (runs first)
let cfg_a = make_config("plugin_a", 10, PluginMode::Sequential);
let plugin_a = Arc::new(AllowPlugin { cfg: cfg_a.clone() });
let handler_a: Arc<dyn AnyHookHandler> = Arc::new(GlobalChainHandler { tag: "a" });
mgr.register_raw::<TestHook>(plugin_a, cfg_a, handler_a)
.unwrap();
// Plugin B — priority 20 (runs second)
let cfg_b = make_config("plugin_b", 20, PluginMode::Sequential);
let plugin_b = Arc::new(AllowPlugin { cfg: cfg_b.clone() });
let handler_b: Arc<dyn AnyHookHandler> = Arc::new(GlobalChainHandler { tag: "b" });
mgr.register_raw::<TestHook>(plugin_b, cfg_b, handler_b)
.unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "x".into() });
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
// Canonical global_state on the returned table must contain both
// contributions in priority order — proving plugin B observed plugin
// A's write, and the table holds the merged result, not an arbitrary
// plugin's snapshot.
let chain = result
.context_table
.global_state
.get("chain")
.and_then(|v| v.as_array())
.expect("global_state.chain should be an array");
let tags: Vec<&str> = chain.iter().filter_map(|v| v.as_str()).collect();
assert_eq!(tags, vec!["a", "b"]);
}
/// All five phases (Sequential, Transform, Audit, Concurrent,
/// FireAndForget) execute in the documented order, with payload
/// modifications from earlier phases visible in later ones. Closes
/// the review's "no multi-phase combination test" gap.
#[tokio::test]
async fn test_all_five_phases_run_in_order_with_payload_chaining() {
use std::sync::Arc as StdArc;
use std::sync::Mutex as StdMutex;
let log: StdArc<StdMutex<Vec<&'static str>>> = StdArc::new(StdMutex::new(Vec::new()));
// Sequential — modifies payload, logs "seq".
struct SeqHandler {
log: StdArc<StdMutex<Vec<&'static str>>>,
}
#[async_trait]
impl AnyHookHandler for SeqHandler {
async fn invoke(
&self,
payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
self.log.lock().unwrap().push("seq");
let typed = payload.as_any().downcast_ref::<TestPayload>().unwrap();
let modified = TestPayload {
value: format!("{}|seq", typed.value),
};
let result: PluginResult<TestPayload> = PluginResult::modify_payload(modified);
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
// Transform — modifies payload, logs "transform".
struct TransformLogger {
log: StdArc<StdMutex<Vec<&'static str>>>,
}
#[async_trait]
impl AnyHookHandler for TransformLogger {
async fn invoke(
&self,
payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
self.log.lock().unwrap().push("transform");
let typed = payload.as_any().downcast_ref::<TestPayload>().unwrap();
let modified = TestPayload {
value: format!("{}|transform", typed.value),
};
let result: PluginResult<TestPayload> = PluginResult::modify_payload(modified);
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
// Logger that asserts the payload it observes contains both prior
// phases' marks (proving payload chaining made it this far).
struct ObserverHandler {
tag: &'static str,
log: StdArc<StdMutex<Vec<&'static str>>>,
expected_payload: &'static str,
}
#[async_trait]
impl AnyHookHandler for ObserverHandler {
async fn invoke(
&self,
payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
let typed = payload.as_any().downcast_ref::<TestPayload>().unwrap();
assert_eq!(
typed.value, self.expected_payload,
"{} observed unexpected payload: got '{}', expected '{}'",
self.tag, typed.value, self.expected_payload,
);
self.log.lock().unwrap().push(self.tag);
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let mgr = PluginManager::default();
let cfg_seq = make_config("seq", 10, PluginMode::Sequential);
mgr.register_raw::<TestHook>(
Arc::new(AllowPlugin {
cfg: cfg_seq.clone(),
}),
cfg_seq,
Arc::new(SeqHandler {
log: StdArc::clone(&log),
}),
)
.unwrap();
let cfg_transform = make_config("transform", 10, PluginMode::Transform);
mgr.register_raw::<TestHook>(
Arc::new(AllowPlugin {
cfg: cfg_transform.clone(),
}),
cfg_transform,
Arc::new(TransformLogger {
log: StdArc::clone(&log),
}),
)
.unwrap();
let cfg_audit = make_config("audit", 10, PluginMode::Audit);
mgr.register_raw::<TestHook>(
Arc::new(AllowPlugin {
cfg: cfg_audit.clone(),
}),
cfg_audit,
Arc::new(ObserverHandler {
tag: "audit",
log: StdArc::clone(&log),
expected_payload: "start|seq|transform",
}),
)
.unwrap();
let cfg_concurrent = make_config("concurrent", 10, PluginMode::Concurrent);
mgr.register_raw::<TestHook>(
Arc::new(AllowPlugin {
cfg: cfg_concurrent.clone(),
}),
cfg_concurrent,
Arc::new(ObserverHandler {
tag: "concurrent",
log: StdArc::clone(&log),
expected_payload: "start|seq|transform",
}),
)
.unwrap();
let cfg_faf = make_config("faf", 10, PluginMode::FireAndForget);
mgr.register_raw::<TestHook>(
Arc::new(AllowPlugin {
cfg: cfg_faf.clone(),
}),
cfg_faf,
Arc::new(ObserverHandler {
tag: "faf",
log: StdArc::clone(&log),
expected_payload: "start|seq|transform",
}),
)
.unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "start".into(),
});
let (result, bg) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
// Final payload should have both modify-phase marks.
let final_payload = result.modified_payload.unwrap();
let typed = final_payload
.as_any()
.downcast_ref::<TestPayload>()
.unwrap();
assert_eq!(typed.value, "start|seq|transform");
// Drain the FAF task before checking ordering — its log entry
// races the rest of the function otherwise.
let _ = bg.wait_for_background_tasks().await;
let log = log.lock().unwrap();
// Sequential, Transform, Audit are guaranteed in order (serial phases).
assert_eq!(log[0], "seq", "first should be sequential phase");
assert_eq!(log[1], "transform", "second should be transform phase");
assert_eq!(log[2], "audit", "third should be audit phase");
// Concurrent runs before invoke returns; FAF was waited on above.
// Their relative order with each other is not strictly guaranteed
// (FAF spawns *after* concurrent finishes, but tokio scheduling
// can interleave). Just check both present in indices 3 / 4.
let post_audit: std::collections::HashSet<&&'static str> = log[3..].iter().collect();
assert!(
post_audit.contains(&"concurrent"),
"concurrent phase must run"
);
assert!(post_audit.contains(&"faf"), "fire-and-forget must run");
assert_eq!(log.len(), 5, "all five phases should have logged");
}
/// Routing must work for `resource`, `prompt`, and `llm` entity types
/// — not just `tool`. Closes the review's "no test verifying entity
/// types other than tool in routing" gap.
#[tokio::test]
async fn test_routing_works_for_all_entity_types() {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc as StdArc;
// One counter per entity-type test; each plugin only fires when
// the route resolves to it.
struct CountHandler {
counter: StdArc<AtomicUsize>,
}
#[async_trait]
impl AnyHookHandler for CountHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
self.counter.fetch_add(1, Ordering::SeqCst);
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
// Each row: (entity_type, route field name, route value, request entity_name, should_match)
// We build a fresh manager per entity type so routes don't bleed.
for (entity_type, route_field, route_value, request_name, should_match) in [
("resource", "resource", "my_resource", "my_resource", true),
(
"resource",
"resource",
"my_resource",
"other_resource",
false,
),
("prompt", "prompt", "my_prompt", "my_prompt", true),
("prompt", "prompt", "my_prompt", "other_prompt", false),
("llm", "llm", "gpt-4", "gpt-4", true),
("llm", "llm", "gpt-4", "claude", false),
] {
let yaml = format!(
r#"
plugin_settings:
routing_enabled: true
plugins:
- name: target
kind: test/allow
hooks: [test_hook]
mode: sequential
routes:
- {route_field}: {route_value}
plugins:
- target
"#
);
let cpex_config = crate::config::parse_config(&yaml).unwrap();
let mgr = PluginManager::default();
let counter = StdArc::new(AtomicUsize::new(0));
// Custom factory that hands out a CountHandler with our shared counter.
struct ParamFactory(StdArc<AtomicUsize>);
impl crate::factory::PluginFactory for ParamFactory {
fn create(
&self,
config: &PluginConfig,
) -> Result<crate::factory::PluginInstance, Box<PluginError>> {
Ok(crate::factory::PluginInstance {
plugin: Arc::new(AllowPlugin {
cfg: config.clone(),
}),
handlers: vec![(
"test_hook",
Arc::new(CountHandler {
counter: StdArc::clone(&self.0),
}),
)],
})
}
}
mgr.register_factory(
"test/allow",
Box::new(ParamFactory(StdArc::clone(&counter))),
);
mgr.load_config(cpex_config).unwrap();
mgr.initialize().await.unwrap();
let p: Box<dyn PluginPayload> = Box::new(TestPayload { value: "x".into() });
let ext = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some(entity_type.into()),
entity_name: Some(request_name.into()),
..Default::default()
})),
..Default::default()
};
let _ = mgr.invoke_by_name("test_hook", p, ext, None).await;
let expected = if should_match { 1 } else { 0 };
assert_eq!(
counter.load(Ordering::SeqCst),
expected,
"entity_type={} route_field={} route_value={} request_name={} expected fire={}",
entity_type,
route_field,
route_value,
request_name,
should_match,
);
}
}
/// `initialize()` must roll back already-initialized plugins by
/// calling `shutdown()` on each, in reverse order, when a later
/// plugin's `initialize()` fails. Closes the review's "no test for
/// initialize() rollback path" gap.
#[tokio::test]
async fn test_initialize_rollback_on_failure() {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc as StdArc;
// Track per-plugin init / shutdown invocations.
let init_count_a = StdArc::new(AtomicUsize::new(0));
let shutdown_count_a = StdArc::new(AtomicUsize::new(0));
let init_count_b = StdArc::new(AtomicUsize::new(0));
let shutdown_count_b = StdArc::new(AtomicUsize::new(0));
let init_count_c = StdArc::new(AtomicUsize::new(0));
let shutdown_count_c = StdArc::new(AtomicUsize::new(0));
struct LifecyclePlugin {
cfg: PluginConfig,
init_counter: StdArc<AtomicUsize>,
shutdown_counter: StdArc<AtomicUsize>,
fail_init: bool,
}
#[async_trait]
impl Plugin for LifecyclePlugin {
fn config(&self) -> &PluginConfig {
&self.cfg
}
async fn initialize(&self) -> Result<(), Box<PluginError>> {
self.init_counter.fetch_add(1, Ordering::SeqCst);
if self.fail_init {
Err(Box::new(PluginError::Config {
message: "intentional init failure".into(),
}))
} else {
Ok(())
}
}
async fn shutdown(&self) -> Result<(), Box<PluginError>> {
self.shutdown_counter.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}
impl HookHandler<TestHook> for LifecyclePlugin {
async fn handle(
&self,
_payload: &TestPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> PluginResult<TestPayload> {
PluginResult::allow()
}
}
let mgr = PluginManager::default();
// Plugin A: initializes successfully (priority 10, registered first).
let cfg_a = make_config("a", 10, PluginMode::Sequential);
let plugin_a = Arc::new(LifecyclePlugin {
cfg: cfg_a.clone(),
init_counter: StdArc::clone(&init_count_a),
shutdown_counter: StdArc::clone(&shutdown_count_a),
fail_init: false,
});
mgr.register_handler::<TestHook, _>(plugin_a, cfg_a)
.unwrap();
// Plugin B: initialize() returns Err — should trigger rollback.
let cfg_b = make_config("b", 20, PluginMode::Sequential);
let plugin_b = Arc::new(LifecyclePlugin {
cfg: cfg_b.clone(),
init_counter: StdArc::clone(&init_count_b),
shutdown_counter: StdArc::clone(&shutdown_count_b),
fail_init: true,
});
mgr.register_handler::<TestHook, _>(plugin_b, cfg_b)
.unwrap();
// Plugin C: never reached (init aborts at B).
let cfg_c = make_config("c", 30, PluginMode::Sequential);
let plugin_c = Arc::new(LifecyclePlugin {
cfg: cfg_c.clone(),
init_counter: StdArc::clone(&init_count_c),
shutdown_counter: StdArc::clone(&shutdown_count_c),
fail_init: false,
});
mgr.register_handler::<TestHook, _>(plugin_c, cfg_c)
.unwrap();
let result = mgr.initialize().await;
assert!(
result.is_err(),
"initialize() must propagate the init failure"
);
// The registry iterates plugins in `HashMap` order, which is
// randomized — so we don't know whether A and C were reached
// before B failed. The rollback invariants are order-independent:
//
// - For non-failing plugins (A, C): if init() was called, shutdown()
// must have been called too (rolled back). If init() was not
// called (B happened to iterate first), shutdown() shouldn't
// have either. In both cases, init_count == shutdown_count.
// - B's init() was called and failed, so its shutdown() must NOT
// run — failed-init plugins are not part of the rollback set.
let assert_pair_invariant =
|init: &AtomicUsize, shutdown: &AtomicUsize, tag: &str| {
let i = init.load(Ordering::SeqCst);
let s = shutdown.load(Ordering::SeqCst);
assert!(
(i == 0 && s == 0) || (i == 1 && s == 1),
"{}: init/shutdown should be paired (both 0 or both 1), got init={} shutdown={}",
tag, i, s,
);
};
assert_pair_invariant(&init_count_a, &shutdown_count_a, "A");
assert_pair_invariant(&init_count_c, &shutdown_count_c, "C");
// B specifically: init was called and failed; no shutdown for it.
assert_eq!(
init_count_b.load(Ordering::SeqCst),
1,
"B's initialize was called",
);
assert_eq!(
shutdown_count_b.load(Ordering::SeqCst),
0,
"B failed to initialize; shutdown should not run for it",
);
// Manager must report not-initialized after the failure.
assert!(!mgr.is_initialized());
}
// -- Factory-based tests --
/// A test factory that creates AllowPlugin instances.
struct AllowPluginFactory;
impl crate::factory::PluginFactory for AllowPluginFactory {
fn create(
&self,
config: &PluginConfig,
) -> Result<crate::factory::PluginInstance, Box<PluginError>> {
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> =
Arc::new(TypedHandlerAdapter::<TestHook, AllowPlugin>::new(
Arc::clone(&plugin),
));
Ok(crate::factory::PluginInstance {
plugin,
handlers: vec![("test_hook", handler)],
})
}
}
/// A test factory that creates DenyPlugin instances.
struct DenyPluginFactory;
impl crate::factory::PluginFactory for DenyPluginFactory {
fn create(
&self,
config: &PluginConfig,
) -> Result<crate::factory::PluginInstance, Box<PluginError>> {
let plugin = Arc::new(DenyPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> =
Arc::new(TypedHandlerAdapter::<TestHook, DenyPlugin>::new(
Arc::clone(&plugin),
));
Ok(crate::factory::PluginInstance {
plugin,
handlers: vec![("test_hook", handler)],
})
}
}
#[tokio::test]
async fn test_from_config_creates_manager() {
let yaml = r#"
plugins:
- name: allow_plugin
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 10
plugin_settings:
plugin_timeout: 60
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mut factories = PluginFactoryRegistry::new();
factories.register("test/allow", Box::new(AllowPluginFactory));
let mgr = PluginManager::from_config(cpex_config, &factories).unwrap();
mgr.initialize().await.unwrap();
assert_eq!(mgr.plugin_count(), 1);
assert!(mgr.has_hooks_for("test_hook"));
}
#[tokio::test]
async fn test_from_config_invokes_correctly() {
let yaml = r#"
plugins:
- name: denier
kind: test/deny
hooks: [test_hook]
mode: sequential
priority: 10
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mut factories = PluginFactoryRegistry::new();
factories.register("test/deny", Box::new(DenyPluginFactory));
let mgr = PluginManager::from_config(cpex_config, &factories).unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
// context_table = None (first invocation)
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(!result.continue_processing);
assert_eq!(result.violation.as_ref().unwrap().code, "denied");
}
#[tokio::test]
async fn test_from_config_unknown_kind_rejected() {
let yaml = r#"
plugins:
- name: mystery
kind: unknown/type
hooks: [test_hook]
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let factories = PluginFactoryRegistry::new(); // empty — no factories
let result = PluginManager::from_config(cpex_config, &factories);
match result {
Err(e) => assert!(
e.to_string().contains("no factory registered"),
"got: {}",
e
),
Ok(_) => panic!("expected error for unknown kind"),
}
}
#[tokio::test]
async fn test_from_config_multiple_plugins() {
let yaml = r#"
plugins:
- name: gate
kind: test/deny
hooks: [test_hook]
mode: sequential
priority: 5
- name: fallback
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 10
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mut factories = PluginFactoryRegistry::new();
factories.register("test/allow", Box::new(AllowPluginFactory));
factories.register("test/deny", Box::new(DenyPluginFactory));
let mgr = PluginManager::from_config(cpex_config, &factories).unwrap();
mgr.initialize().await.unwrap();
assert_eq!(mgr.plugin_count(), 2);
// Deny plugin has higher priority (5 < 10), so it fires first
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
// context_table = None (first invocation)
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(!result.continue_processing); // gate denied before fallback could allow
}
// -- Routing cache tests --
#[tokio::test]
async fn test_routing_cache_populated_on_first_invoke() {
let yaml = r#"
plugin_settings:
routing_enabled: true
global:
policies:
all:
plugins: [allow_plugin]
plugins:
- name: allow_plugin
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 10
routes:
- tool: get_compensation
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mut factories = PluginFactoryRegistry::new();
factories.register("test/allow", Box::new(AllowPluginFactory));
let mgr = PluginManager::from_config(cpex_config, &factories).unwrap();
mgr.initialize().await.unwrap();
assert_eq!(mgr.routing_cache_size(), 0);
// First invoke — populates cache
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let ext = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("get_compensation".into()),
..Default::default()
})),
..Default::default()
};
// context_table = None (first invocation)
mgr.invoke_by_name("test_hook", payload, ext, None).await;
assert_eq!(mgr.routing_cache_size(), 1);
// Second invoke — cache hit, still size 1
let payload2: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test2".into(),
});
let ext2 = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("get_compensation".into()),
..Default::default()
})),
..Default::default()
};
mgr.invoke_by_name("test_hook", payload2, ext2, None).await;
assert_eq!(mgr.routing_cache_size(), 1); // cache hit — no new entry
}
#[tokio::test]
async fn test_routing_cache_different_entities_separate() {
let yaml = r#"
plugin_settings:
routing_enabled: true
global:
policies:
all:
plugins: [allow_plugin]
plugins:
- name: allow_plugin
kind: test/allow
hooks: [test_hook]
mode: sequential
routes:
- tool: get_compensation
- tool: send_email
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mut factories = PluginFactoryRegistry::new();
factories.register("test/allow", Box::new(AllowPluginFactory));
let mgr = PluginManager::from_config(cpex_config, &factories).unwrap();
mgr.initialize().await.unwrap();
// context_table = None (first invocation)
// Invoke for get_compensation
let p1: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let e1 = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("get_compensation".into()),
..Default::default()
})),
..Default::default()
};
mgr.invoke_by_name("test_hook", p1, e1, None).await;
// Invoke for send_email
let p2: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let e2 = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("send_email".into()),
..Default::default()
})),
..Default::default()
};
mgr.invoke_by_name("test_hook", p2, e2, None).await;
assert_eq!(mgr.routing_cache_size(), 2);
}
#[tokio::test]
async fn test_routing_cache_cleared() {
let yaml = r#"
plugin_settings:
routing_enabled: true
global:
policies:
all:
plugins: [allow_plugin]
plugins:
- name: allow_plugin
kind: test/allow
hooks: [test_hook]
mode: sequential
routes:
- tool: get_compensation
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mut factories = PluginFactoryRegistry::new();
factories.register("test/allow", Box::new(AllowPluginFactory));
let mgr = PluginManager::from_config(cpex_config, &factories).unwrap();
mgr.initialize().await.unwrap();
// context_table = None (first invocation)
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let ext = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("get_compensation".into()),
..Default::default()
})),
..Default::default()
};
mgr.invoke_by_name("test_hook", payload, ext, None).await;
assert_eq!(mgr.routing_cache_size(), 1);
mgr.clear_routing_cache();
assert_eq!(mgr.routing_cache_size(), 0);
}
#[tokio::test]
async fn test_unregister_invalidates_routing_cache() {
let yaml = r#"
plugin_settings:
routing_enabled: true
global:
policies:
all:
plugins: [allow_plugin]
plugins:
- name: allow_plugin
kind: test/allow
hooks: [test_hook]
mode: sequential
routes:
- tool: get_compensation
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mut factories = PluginFactoryRegistry::new();
factories.register("test/allow", Box::new(AllowPluginFactory));
let mgr = PluginManager::from_config(cpex_config, &factories).unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let ext = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("get_compensation".into()),
..Default::default()
})),
..Default::default()
};
mgr.invoke_by_name("test_hook", payload, ext, None).await;
assert_eq!(mgr.routing_cache_size(), 1);
// Unregister should invalidate the cache so removed plugins
// don't continue firing from stale cached entries.
mgr.unregister("allow_plugin");
assert_eq!(mgr.routing_cache_size(), 0);
}
#[test]
fn test_routing_cache_recovers_from_poisoned_lock() {
// A panic while holding the cache lock poisons it. Before the fix,
// every subsequent read()/write() would unwrap a PoisonError and
// panic, permanently breaking dispatch. With unwrap_or_else +
// into_inner, the cache stays usable.
//
// Note: this test intentionally panics inside catch_unwind, which
// prints "thread 'manager::tests::...' panicked at..." to test
// output even though the panic is caught. That's expected.
use std::panic::AssertUnwindSafe;
let mgr = PluginManager::default();
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
let _guard = mgr.route_cache.write().unwrap();
panic!("simulated panic while holding cache lock");
}));
assert!(result.is_err(), "expected the panic to be caught");
assert!(
mgr.route_cache.is_poisoned(),
"lock should be poisoned after the panic",
);
// All four lock sites must now succeed despite the poison flag.
assert_eq!(mgr.routing_cache_size(), 0);
mgr.clear_routing_cache();
assert_eq!(mgr.routing_cache_size(), 0);
}
#[tokio::test]
async fn test_routing_cache_rejects_inserts_at_capacity() {
// Cap of 2 — verifies bound holds AND uncached requests still resolve correctly.
let yaml = r#"
plugin_settings:
routing_enabled: true
route_cache_max_entries: 2
global:
policies:
all:
plugins: [allow_plugin]
plugins:
- name: allow_plugin
kind: test/allow
hooks: [test_hook]
mode: sequential
routes:
- tool: a
- tool: b
- tool: c
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mut factories = PluginFactoryRegistry::new();
factories.register("test/allow", Box::new(AllowPluginFactory));
let mgr = PluginManager::from_config(cpex_config, &factories).unwrap();
mgr.initialize().await.unwrap();
let invoke_for = |entity: &'static str| -> (Box<dyn PluginPayload>, Extensions) {
let p: Box<dyn PluginPayload> = Box::new(TestPayload {
value: entity.into(),
});
let e = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some(entity.into()),
..Default::default()
})),
..Default::default()
};
(p, e)
};
// Fill to cap (2 distinct entities).
let (p1, e1) = invoke_for("a");
let (r1, _) = mgr.invoke_by_name("test_hook", p1, e1, None).await;
assert!(r1.continue_processing);
assert_eq!(mgr.routing_cache_size(), 1);
let (p2, e2) = invoke_for("b");
let (r2, _) = mgr.invoke_by_name("test_hook", p2, e2, None).await;
assert!(r2.continue_processing);
assert_eq!(mgr.routing_cache_size(), 2);
// Third entity — cache is full, insert is rejected.
// Pipeline must still run correctly (slow path resolves the route).
let (p3, e3) = invoke_for("c");
let (r3, _) = mgr.invoke_by_name("test_hook", p3, e3, None).await;
assert!(
r3.continue_processing,
"slow path must still resolve when cache is full"
);
assert_eq!(mgr.routing_cache_size(), 2, "cache must not exceed cap");
// Repeated request for the same uncached entity also works.
let (p4, e4) = invoke_for("c");
let (r4, _) = mgr.invoke_by_name("test_hook", p4, e4, None).await;
assert!(r4.continue_processing);
assert_eq!(mgr.routing_cache_size(), 2);
// Clearing the cache lets new entries memoize again.
mgr.clear_routing_cache();
let (p5, e5) = invoke_for("c");
mgr.invoke_by_name("test_hook", p5, e5, None).await;
assert_eq!(mgr.routing_cache_size(), 1);
}
#[tokio::test]
async fn test_register_handler_invalidates_routing_cache() {
let yaml = r#"
plugin_settings:
routing_enabled: true
global:
policies:
all:
plugins: [allow_plugin]
plugins:
- name: allow_plugin
kind: test/allow
hooks: [test_hook]
mode: sequential
routes:
- tool: get_compensation
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mut factories = PluginFactoryRegistry::new();
factories.register("test/allow", Box::new(AllowPluginFactory));
let mgr = PluginManager::from_config(cpex_config, &factories).unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let ext = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("get_compensation".into()),
..Default::default()
})),
..Default::default()
};
mgr.invoke_by_name("test_hook", payload, ext, None).await;
assert_eq!(mgr.routing_cache_size(), 1);
// Registering a new handler must invalidate the cache so the
// new plugin is visible to subsequent route resolutions.
let extra_cfg = make_config("late_plugin", 20, PluginMode::Sequential);
let extra = Arc::new(AllowPlugin {
cfg: extra_cfg.clone(),
});
mgr.register_handler::<TestHook, _>(extra, extra_cfg)
.unwrap();
assert_eq!(mgr.routing_cache_size(), 0);
}
#[tokio::test]
async fn test_routing_cache_scope_creates_separate_entries() {
let yaml = r#"
plugin_settings:
routing_enabled: true
global:
policies:
all:
plugins: [allow_plugin]
plugins:
- name: allow_plugin
kind: test/allow
hooks: [test_hook]
mode: sequential
routes:
- tool: get_compensation
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mut factories = PluginFactoryRegistry::new();
factories.register("test/allow", Box::new(AllowPluginFactory));
let mgr = PluginManager::from_config(cpex_config, &factories).unwrap();
mgr.initialize().await.unwrap();
// context_table = None (first invocation)
// Same entity, different scopes → separate cache entries
let p1: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let e1 = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("get_compensation".into()),
scope: Some("hr-server".into()),
..Default::default()
})),
..Default::default()
};
mgr.invoke_by_name("test_hook", p1, e1, None).await;
let p2: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let e2 = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("get_compensation".into()),
scope: Some("billing-server".into()),
..Default::default()
})),
..Default::default()
};
mgr.invoke_by_name("test_hook", p2, e2, None).await;
assert_eq!(mgr.routing_cache_size(), 2); // different scopes → different cache entries
}
// -- Override instance tests --
#[tokio::test]
async fn test_route_override_creates_new_instance() {
let yaml = r#"
plugin_settings:
routing_enabled: true
plugins:
- name: rate_limiter
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 10
config:
max_requests: 100
routes:
- tool: get_compensation
plugins:
- rate_limiter:
config:
max_requests: 10
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
// Use register_factory + load_config so manager owns factories
let mgr = PluginManager::default();
mgr.register_factory("test/allow", Box::new(AllowPluginFactory));
mgr.load_config(cpex_config).unwrap();
mgr.initialize().await.unwrap();
// Invoke with routing — should create override instance
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let ext = Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some("tool".into()),
entity_name: Some("get_compensation".into()),
..Default::default()
})),
..Default::default()
};
// context_table = None (first invocation)
let (result, _) = mgr.invoke_by_name("test_hook", payload, ext, None).await;
// Plugin executed (allow plugin returns allowed)
assert!(result.continue_processing);
// Cache populated
assert_eq!(mgr.routing_cache_size(), 1);
}
/// Override instances must have `initialize()` called so plugins that
/// open DB connections / file handles / network clients on init don't
/// run with default state. Uses a tracking factory whose plugin
/// increments a counter inside its `initialize()`.
#[tokio::test]
async fn test_route_override_initializes_new_instance() {
use std::sync::atomic::{AtomicUsize, Ordering};
static INIT_COUNT: AtomicUsize = AtomicUsize::new(0);
INIT_COUNT.store(0, Ordering::SeqCst);
struct InitTrackingPlugin {
cfg: PluginConfig,
}
#[async_trait]
impl Plugin for InitTrackingPlugin {
fn config(&self) -> &PluginConfig {
&self.cfg
}
async fn initialize(&self) -> Result<(), Box<PluginError>> {
INIT_COUNT.fetch_add(1, Ordering::SeqCst);
Ok(())
}
async fn shutdown(&self) -> Result<(), Box<PluginError>> {
Ok(())
}
}
impl HookHandler<TestHook> for InitTrackingPlugin {
async fn handle(
&self,
_payload: &TestPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> PluginResult<TestPayload> {
PluginResult::allow()
}
}
struct InitTrackingFactory;
impl crate::factory::PluginFactory for InitTrackingFactory {
fn create(
&self,
config: &PluginConfig,
) -> Result<crate::factory::PluginInstance, Box<PluginError>> {
let plugin = Arc::new(InitTrackingPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> =
Arc::new(TypedHandlerAdapter::<TestHook, InitTrackingPlugin>::new(
Arc::clone(&plugin),
));
Ok(crate::factory::PluginInstance {
plugin,
handlers: vec![("test_hook", handler)],
})
}
}
let yaml = r#"
plugin_settings:
routing_enabled: true
plugins:
- name: tracker
kind: test/init_tracking
hooks: [test_hook]
mode: sequential
priority: 10
config:
max_requests: 100
routes:
- tool: get_compensation
plugins:
- tracker:
config:
max_requests: 10
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mgr = PluginManager::default();
mgr.register_factory("test/init_tracking", Box::new(InitTrackingFactory));
mgr.load_config(cpex_config).unwrap();
mgr.initialize().await.unwrap();
// Base plugin was initialized exactly once during mgr.initialize().
assert_eq!(INIT_COUNT.load(Ordering::SeqCst), 1);
// Invoke with route override — creates a new instance via factory.
// That new instance must also be initialized.
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let (result, _) = mgr
.invoke_by_name(
"test_hook",
payload,
make_meta("tool", "get_compensation", None, &[]),
None,
)
.await;
assert!(result.continue_processing);
assert_eq!(
INIT_COUNT.load(Ordering::SeqCst),
2,
"override instance must have initialize() called",
);
}
/// Override and base must have INDEPENDENT circuit breakers. A failure
/// on an override-only route (e.g., bad credentials in the merged
/// config) must not silently disable the plugin for every other route
/// using the base config — config is part of the failure surface, and
/// per-route blast radius is the point of having overrides.
#[tokio::test]
async fn test_route_override_circuit_breaker_isolated_from_base() {
struct ErrorOnInvokeFactory;
impl crate::factory::PluginFactory for ErrorOnInvokeFactory {
fn create(
&self,
config: &PluginConfig,
) -> Result<crate::factory::PluginInstance, Box<PluginError>> {
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(ErrorHandler);
Ok(crate::factory::PluginInstance {
plugin,
handlers: vec![("test_hook", handler)],
})
}
}
let yaml = r#"
plugin_settings:
routing_enabled: true
plugins:
- name: flaky
kind: test/error_on_invoke
hooks: [test_hook]
mode: sequential
priority: 10
on_error: disable
routes:
- tool: get_compensation
plugins:
- flaky:
config:
something: changed
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mgr = PluginManager::default();
mgr.register_factory("test/error_on_invoke", Box::new(ErrorOnInvokeFactory));
mgr.load_config(cpex_config).unwrap();
mgr.initialize().await.unwrap();
assert!(
!mgr.get_plugin("flaky").unwrap().is_disabled(),
"should start enabled"
);
// Invoke a route that uses the override. The override's handler
// errors with `on_error: Disable`, so the executor calls disable()
// on the *override's* plugin_ref. Independent circuit breakers
// mean the base must stay enabled.
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let _ = mgr
.invoke_by_name(
"test_hook",
payload,
make_meta("tool", "get_compensation", None, &[]),
None,
)
.await;
assert!(
!mgr.get_plugin("flaky").unwrap().is_disabled(),
"base must NOT be disabled when an override trips its own circuit breaker",
);
}
#[tokio::test]
async fn test_register_factory_then_load_config() {
let yaml = r#"
plugins:
- name: my_plugin
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 10
plugin_settings:
plugin_timeout: 45
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mgr = PluginManager::default();
mgr.register_factory("test/allow", Box::new(AllowPluginFactory));
mgr.load_config(cpex_config).unwrap();
mgr.initialize().await.unwrap();
assert_eq!(mgr.plugin_count(), 1);
assert!(mgr.has_hooks_for("test_hook"));
let payload: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
// context_table = None (first invocation)
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
}
// -- End-to-end routing tests --
/// Helper to build meta extensions for routing tests.
fn make_meta(
entity_type: &str,
entity_name: &str,
scope: Option<&str>,
tags: &[&str],
) -> Extensions {
let mut tag_set = std::collections::HashSet::new();
for t in tags {
tag_set.insert(t.to_string());
}
Extensions {
meta: Some(std::sync::Arc::new(crate::hooks::payload::MetaExtension {
entity_type: Some(entity_type.into()),
entity_name: Some(entity_name.into()),
scope: scope.map(String::from),
tags: tag_set,
..Default::default()
})),
..Default::default()
}
}
#[tokio::test]
async fn test_routing_full_flow_different_tools_different_plugins() {
// Setup: identity fires for all, apl_policy fires for pii tools,
// rate_limiter fires only for get_compensation route
let yaml = r#"
plugin_settings:
routing_enabled: true
global:
policies:
all:
plugins: [identity]
pii:
plugins: [apl_policy]
plugins:
- name: identity
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 1
- name: apl_policy
kind: test/deny
hooks: [test_hook]
mode: sequential
priority: 10
- name: rate_limiter
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 5
routes:
- tool: get_compensation
meta:
tags: [pii]
plugins:
- rate_limiter
- tool: send_email
plugins:
- rate_limiter
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mgr = PluginManager::default();
mgr.register_factory("test/allow", Box::new(AllowPluginFactory));
mgr.register_factory("test/deny", Box::new(DenyPluginFactory));
mgr.load_config(cpex_config).unwrap();
mgr.initialize().await.unwrap();
// context_table = None (first invocation)
// get_compensation: identity (all) + apl_policy (pii tag) + rate_limiter (route)
// apl_policy denies → overall denied
let p1: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let (r1, _) = mgr
.invoke_by_name(
"test_hook",
p1,
make_meta("tool", "get_compensation", None, &[]),
None,
)
.await;
assert!(!r1.continue_processing); // apl_policy (deny) fires due to pii tag
// send_email: identity (all) + rate_limiter (route) — no pii tag
// both allow → overall allowed
let p2: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let (r2, _) = mgr
.invoke_by_name(
"test_hook",
p2,
make_meta("tool", "send_email", None, &[]),
None,
)
.await;
assert!(r2.continue_processing); // no deny plugin fires
}
#[tokio::test]
async fn test_routing_disabled_fires_all_plugins() {
// Same plugins but routing disabled — all fire regardless of entity
let yaml = r#"
plugins:
- name: denier
kind: test/deny
hooks: [test_hook]
mode: sequential
priority: 10
- name: allower
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 20
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mgr = PluginManager::default();
mgr.register_factory("test/allow", Box::new(AllowPluginFactory));
mgr.register_factory("test/deny", Box::new(DenyPluginFactory));
mgr.load_config(cpex_config).unwrap();
mgr.initialize().await.unwrap();
// context_table = None (first invocation)
// Even with meta, routing disabled → all plugins fire → denier wins
let p: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let (result, _) = mgr
.invoke_by_name(
"test_hook",
p,
make_meta("tool", "anything", None, &[]),
None,
)
.await;
assert!(!result.continue_processing); // denier fires (all plugins active)
}
#[tokio::test]
async fn test_routing_no_meta_fires_all_plugins() {
// Routing enabled but no meta on extensions → fallback to all
let yaml = r#"
plugin_settings:
routing_enabled: true
global:
policies:
all:
plugins: [allower]
plugins:
- name: allower
kind: test/allow
hooks: [test_hook]
mode: sequential
- name: denier
kind: test/deny
hooks: [test_hook]
mode: sequential
routes:
- tool: get_compensation
plugins:
- denier
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mgr = PluginManager::default();
mgr.register_factory("test/allow", Box::new(AllowPluginFactory));
mgr.register_factory("test/deny", Box::new(DenyPluginFactory));
mgr.load_config(cpex_config).unwrap();
mgr.initialize().await.unwrap();
// context_table = None (first invocation)
// No meta → all plugins fire (both allower and denier)
let p: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let (result, _) = mgr
.invoke_by_name("test_hook", p, Extensions::default(), None)
.await;
// No meta → no route resolution → both plugins fire. The denier
// running is observable (the deny propagates to the result), so
// assert that — proves route filtering didn't accidentally hide it.
assert!(
!result.continue_processing,
"denier should run when no meta is provided (route filtering bypassed)",
);
assert!(
result.violation.is_some(),
"deny should produce a violation"
);
}
#[tokio::test]
async fn test_routing_wildcard_catches_unmatched() {
let yaml = r#"
plugin_settings:
routing_enabled: true
global:
policies:
all:
plugins: [identity]
plugins:
- name: identity
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 1
- name: specific_plugin
kind: test/deny
hooks: [test_hook]
mode: sequential
priority: 10
- name: fallback_plugin
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 10
routes:
- tool: get_compensation
plugins:
- specific_plugin
- tool: "*"
plugins:
- fallback_plugin
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mgr = PluginManager::default();
mgr.register_factory("test/allow", Box::new(AllowPluginFactory));
mgr.register_factory("test/deny", Box::new(DenyPluginFactory));
mgr.load_config(cpex_config).unwrap();
mgr.initialize().await.unwrap();
// context_table = None (first invocation)
// get_compensation matches exact route → specific_plugin (deny)
let p1: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let (r1, _) = mgr
.invoke_by_name(
"test_hook",
p1,
make_meta("tool", "get_compensation", None, &[]),
None,
)
.await;
assert!(!r1.continue_processing); // specific_plugin denies
// unknown_tool matches wildcard → fallback_plugin (allow)
let p2: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let (r2, _) = mgr
.invoke_by_name(
"test_hook",
p2,
make_meta("tool", "unknown_tool", None, &[]),
None,
)
.await;
assert!(r2.continue_processing); // fallback_plugin allows
}
#[tokio::test]
async fn test_routing_host_tags_activate_policy_groups() {
let yaml = r#"
plugin_settings:
routing_enabled: true
global:
policies:
all:
plugins: [identity]
urgent:
plugins: [denier]
plugins:
- name: identity
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 1
- name: denier
kind: test/deny
hooks: [test_hook]
mode: sequential
priority: 10
routes:
- tool: get_compensation
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mgr = PluginManager::default();
mgr.register_factory("test/allow", Box::new(AllowPluginFactory));
mgr.register_factory("test/deny", Box::new(DenyPluginFactory));
mgr.load_config(cpex_config).unwrap();
mgr.initialize().await.unwrap();
// context_table = None (first invocation)
// Without urgent tag → only identity fires → allowed
let p1: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let (r1, _) = mgr
.invoke_by_name(
"test_hook",
p1,
make_meta("tool", "get_compensation", None, &[]),
None,
)
.await;
assert!(r1.continue_processing);
// Clear cache so new tags take effect
mgr.clear_routing_cache();
// With urgent tag from host → denier also fires → denied
let p2: Box<dyn PluginPayload> = Box::new(TestPayload { value: "t".into() });
let (r2, _) = mgr
.invoke_by_name(
"test_hook",
p2,
make_meta("tool", "get_compensation", None, &["urgent"]),
None,
)
.await;
assert!(!r2.continue_processing);
}
#[tokio::test]
async fn test_routing_works_with_typed_invoke() {
let yaml = r#"
plugin_settings:
routing_enabled: true
global:
policies:
all:
plugins: [allower]
pii:
plugins: [denier]
plugins:
- name: allower
kind: test/allow
hooks: [test_hook]
mode: sequential
priority: 1
- name: denier
kind: test/deny
hooks: [test_hook]
mode: sequential
priority: 10
routes:
- tool: get_compensation
meta:
tags: [pii]
- tool: send_email
"#;
let cpex_config = crate::config::parse_config(yaml).unwrap();
let mgr = PluginManager::default();
mgr.register_factory("test/allow", Box::new(AllowPluginFactory));
mgr.register_factory("test/deny", Box::new(DenyPluginFactory));
mgr.load_config(cpex_config).unwrap();
mgr.initialize().await.unwrap();
// context_table = None (first invocation)
// Typed invoke for get_compensation — pii tag activates denier → denied
let (r1, _) = mgr
.invoke::<TestHook>(
TestPayload { value: "t".into() },
make_meta("tool", "get_compensation", None, &[]),
None,
)
.await;
assert!(!r1.continue_processing);
// Typed invoke for send_email — no pii tag → only allower → allowed
let (r2, _) = mgr
.invoke::<TestHook>(
TestPayload { value: "t".into() },
make_meta("tool", "send_email", None, &[]),
None,
)
.await;
assert!(r2.continue_processing);
}
// -- Executor tier validation tests --
/// Handler that modifies extensions via cow_copy — adds a label.
struct LabelAdderHandler;
#[async_trait]
impl AnyHookHandler for LabelAdderHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
let mut ext = extensions.cow_copy();
if let Some(ref mut sec) = ext.security {
sec.add_label("PLUGIN_ADDED");
}
let mut result: PluginResult<TestPayload> = PluginResult::allow();
result.modified_extensions = Some(ext);
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
/// Handler that tampers with an immutable extension slot.
struct ImmutableTampererHandler;
#[async_trait]
impl AnyHookHandler for ImmutableTampererHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
let mut ext = extensions.cow_copy();
// Tamper: replace the immutable request extension
ext.request = Some(std::sync::Arc::new(crate::extensions::RequestExtension {
request_id: Some("TAMPERED".into()),
..Default::default()
}));
let mut result: PluginResult<TestPayload> = PluginResult::allow();
result.modified_extensions = Some(ext);
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
#[tokio::test]
async fn test_executor_accepts_valid_label_addition() {
let mgr = PluginManager::default();
let mut config = make_config("label-adder", 10, PluginMode::Sequential);
config.capabilities = ["append_labels".to_string(), "read_labels".to_string()].into();
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(LabelAdderHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
// Build extensions with a security label
let mut security = crate::extensions::SecurityExtension::default();
security.add_label("ORIGINAL");
let ext = Extensions {
security: Some(Arc::new(security)),
..Default::default()
};
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr.invoke_by_name("test_hook", payload, ext, None).await;
assert!(result.continue_processing);
// The plugin added "PLUGIN_ADDED" — should be accepted (monotonic superset)
let modified = result.modified_extensions.as_ref().unwrap();
let sec = modified.security.as_ref().unwrap();
assert!(sec.has_label("ORIGINAL"));
assert!(sec.has_label("PLUGIN_ADDED"));
}
#[tokio::test]
async fn test_executor_rejects_immutable_tampering() {
let mgr = PluginManager::default();
let config = make_config("tamperer", 10, PluginMode::Sequential);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(ImmutableTampererHandler);
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
// Build extensions with a request extension
let ext = Extensions {
request: Some(std::sync::Arc::new(crate::extensions::RequestExtension {
request_id: Some("original-req-id".into()),
..Default::default()
})),
..Default::default()
};
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr.invoke_by_name("test_hook", payload, ext, None).await;
assert!(result.continue_processing);
// Extensions should NOT be modified — the tampered immutable was rejected
// The result should have no modified_extensions (rejected by validation)
if let Some(ref modified) = result.modified_extensions {
// If modified extensions exist, the request should still be the original
assert_eq!(
modified.request.as_ref().unwrap().request_id.as_deref(),
Some("original-req-id"),
);
}
}
#[tokio::test]
async fn test_capability_filtering_hides_security_from_plugin() {
// Plugin has NO security capabilities — security should be None
struct SecurityCheckerHandler {
saw_security: std::sync::Arc<std::sync::atomic::AtomicBool>,
}
#[async_trait]
impl AnyHookHandler for SecurityCheckerHandler {
async fn invoke(
&self,
_payload: &dyn PluginPayload,
extensions: &Extensions,
_ctx: &mut PluginContext,
) -> Result<Box<dyn std::any::Any + Send + Sync>, Box<PluginError>> {
// Check if security is visible
if extensions.security.is_some() {
self.saw_security
.store(true, std::sync::atomic::Ordering::SeqCst);
}
let result: PluginResult<TestPayload> = PluginResult::allow();
Ok(crate::executor::erase_result(result))
}
fn hook_type_name(&self) -> &'static str {
"test_hook"
}
}
let saw_security = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let mgr = PluginManager::default();
// No security capabilities declared
let config = make_config("no-sec-caps", 10, PluginMode::Sequential);
let plugin = Arc::new(AllowPlugin {
cfg: config.clone(),
});
let handler: Arc<dyn AnyHookHandler> = Arc::new(SecurityCheckerHandler {
saw_security: saw_security.clone(),
});
mgr.register_raw::<TestHook>(plugin, config, handler)
.unwrap();
mgr.initialize().await.unwrap();
// Build extensions WITH security data
let mut security = crate::extensions::SecurityExtension::default();
security.add_label("SECRET");
security.subject = Some(crate::extensions::security::SubjectExtension {
id: Some("alice".into()),
..Default::default()
});
let ext = Extensions {
security: Some(Arc::new(security)),
..Default::default()
};
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr.invoke_by_name("test_hook", payload, ext, None).await;
assert!(result.continue_processing);
// Plugin should NOT have seen security — no capabilities declared
// Security is still there but labels and subject are empty/none
// (filter_extensions strips gated fields)
// The saw_security flag checks if the security Option itself was Some
// With filter_extensions, security IS Some but with empty labels and no subject
// So saw_security will be true, but the content is filtered
}
// -----------------------------------------------------------------------
// Awaiting handler tests
//
// `HookHandler<H>` is async by design. These tests cover handlers
// that genuinely `.await` inside the body — sleeps, yields, and
// co-registration with handlers whose body has no `.await` at all.
// -----------------------------------------------------------------------
/// Plugin that genuinely awaits inside its handler. Increments a
/// shared counter after the await resolves so the test can verify
/// the handler ran end-to-end and observed its async point.
struct AsyncCounterPlugin {
cfg: PluginConfig,
counter: Arc<std::sync::atomic::AtomicU64>,
}
#[async_trait]
impl Plugin for AsyncCounterPlugin {
fn config(&self) -> &PluginConfig {
&self.cfg
}
}
impl HookHandler<TestHook> for AsyncCounterPlugin {
async fn handle(
&self,
_payload: &TestPayload,
_extensions: &Extensions,
_ctx: &mut PluginContext,
) -> PluginResult<TestPayload> {
tokio::task::yield_now().await;
tokio::time::sleep(std::time::Duration::from_micros(1)).await;
self.counter
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
PluginResult::allow()
}
}
/// Verifies that a handler that genuinely `.await`s gets driven
/// to completion before its result is observed.
#[tokio::test]
async fn test_async_handler_registers_and_invokes() {
let mgr = PluginManager::default();
let counter = Arc::new(std::sync::atomic::AtomicU64::new(0));
let cfg = make_config("async-counter", 10, PluginMode::Sequential);
let plugin = Arc::new(AsyncCounterPlugin {
cfg: cfg.clone(),
counter: counter.clone(),
});
// Same call path as sync plugins — no `register_async_handler`.
mgr.register_handler::<TestHook, _>(plugin, cfg).unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
assert!(result.violation.is_none());
// Counter increments only after the await resolves, so a non-zero
// value proves the future was actually driven to completion.
assert_eq!(
counter.load(std::sync::atomic::Ordering::SeqCst),
1,
"async handler should have run once",
);
}
/// A handler with no `.await` (AllowPlugin) and a handler that
/// genuinely awaits (AsyncCounterPlugin) co-register on the same
/// hook via the same `register_handler` call. Both run in priority
/// order.
#[tokio::test]
async fn test_mixed_sync_and_async_handlers_in_same_hook() {
let mgr = PluginManager::default();
let counter = Arc::new(std::sync::atomic::AtomicU64::new(0));
let sync_cfg = make_config("sync-allow", 10, PluginMode::Sequential);
let sync_plugin = Arc::new(AllowPlugin {
cfg: sync_cfg.clone(),
});
mgr.register_handler::<TestHook, _>(sync_plugin, sync_cfg)
.unwrap();
let async_cfg = make_config("async-counter", 20, PluginMode::Sequential);
let async_plugin = Arc::new(AsyncCounterPlugin {
cfg: async_cfg.clone(),
counter: counter.clone(),
});
mgr.register_handler::<TestHook, _>(async_plugin, async_cfg)
.unwrap();
mgr.initialize().await.unwrap();
let payload: Box<dyn PluginPayload> = Box::new(TestPayload {
value: "test".into(),
});
let (result, _) = mgr
.invoke_by_name("test_hook", payload, Extensions::default(), None)
.await;
assert!(result.continue_processing);
assert_eq!(
counter.load(std::sync::atomic::Ordering::SeqCst),
1,
"awaiting plugin should have run alongside the non-awaiting plugin",
);
}
}