net-mesh 0.23.0

High-performance, schema-agnostic, backend-agnostic event bus
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
//! Phase 4I: Safety Envelope Enforcement (SAFETY)
//!
//! This module provides hard safety limits that cannot be bypassed:
//! - Resource quotas (concurrent requests, tokens, memory, time)
//! - Rate limits (global, per-source, token-based)
//! - Content policies (pattern filtering, size limits, external hooks)
//! - Audit logging with configurable sinks
//! - Kill switches for emergency shutdown
//!
//! All enforcement is designed for the hot path with minimal overhead.

use dashmap::DashMap;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use super::metadata::NodeId;

// ============================================================================
// Safety Envelope Configuration
// ============================================================================

/// Safety envelope configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetyEnvelope {
    /// Unique envelope ID
    pub id: String,
    /// Resource limits
    pub resource_limits: ResourceEnvelope,
    /// Rate limits
    pub rate_limits: RateEnvelope,
    /// Content policies
    pub content_policies: Vec<ContentPolicy>,
    /// Audit configuration
    pub audit: AuditConfig,
    /// Kill switch state
    pub kill_switch: KillSwitchConfig,
    /// Enforcement mode
    pub mode: EnforcementMode,
}

impl Default for SafetyEnvelope {
    fn default() -> Self {
        Self {
            id: "default".to_string(),
            resource_limits: ResourceEnvelope::default(),
            rate_limits: RateEnvelope::default(),
            content_policies: Vec::new(),
            audit: AuditConfig::default(),
            kill_switch: KillSwitchConfig::default(),
            mode: EnforcementMode::Enforce,
        }
    }
}

/// Resource limits envelope
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceEnvelope {
    /// Max concurrent requests
    pub max_concurrent: u32,
    /// Max tokens per request
    pub max_tokens_per_request: u32,
    /// Max memory per request (MB)
    pub max_memory_gb: u32,
    /// Max execution time (ms)
    pub max_time_ms: u32,
    /// Max total cost per hour (in cents)
    pub max_cost_per_hour_cents: u32,
}

impl Default for ResourceEnvelope {
    fn default() -> Self {
        Self {
            max_concurrent: 1000,
            max_tokens_per_request: 128_000,
            max_memory_gb: 16,
            max_time_ms: 300_000,            // 5 minutes
            max_cost_per_hour_cents: 10_000, // $100/hour
        }
    }
}

/// Rate limits envelope
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RateEnvelope {
    /// Requests per minute (global)
    pub global_rpm: u32,
    /// Requests per minute (per source)
    pub per_source_rpm: u32,
    /// Tokens per minute
    pub tokens_per_minute: u64,
    /// Burst multiplier (allows temporary burst above limit)
    pub burst_multiplier: f32,
}

impl Default for RateEnvelope {
    fn default() -> Self {
        Self {
            global_rpm: 10_000,
            per_source_rpm: 1_000,
            tokens_per_minute: 10_000_000,
            burst_multiplier: 2.0,
        }
    }
}

/// Content policy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContentPolicy {
    /// Policy ID
    pub id: String,
    /// Check to perform
    pub check: ContentCheck,
    /// Action on violation
    pub action: PolicyAction,
    /// Whether policy is enabled
    pub enabled: bool,
}

/// Content check types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ContentCheck {
    /// Block specific patterns (regex)
    BlockPatterns(Vec<String>),
    /// Require patterns to be present
    RequirePatterns(Vec<String>),
    /// Maximum content size in bytes
    MaxSize(usize),
    /// Custom validation (placeholder for external hooks)
    Custom {
        /// Identifier of the external validator
        validator_id: String,
    },
}

/// Policy action on violation
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum PolicyAction {
    /// Block the request
    Block,
    /// Warn but allow
    Warn,
    /// Log only
    Log,
    /// Redact matched content
    Redact,
}

/// Audit configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditConfig {
    /// Whether audit logging is enabled
    pub enabled: bool,
    /// Log successful requests
    pub log_success: bool,
    /// Log blocked requests
    pub log_blocked: bool,
    /// Log warnings
    pub log_warnings: bool,
    /// Maximum entries to keep in memory
    pub max_entries: usize,
    /// Flush interval in milliseconds
    pub flush_interval_ms: u64,
}

impl Default for AuditConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            log_success: false,
            log_blocked: true,
            log_warnings: true,
            max_entries: 10_000,
            flush_interval_ms: 5_000,
        }
    }
}

/// Kill switch configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct KillSwitchConfig {
    /// Whether kill switch is currently active
    pub enabled: bool,
    /// Reason for activation
    pub reason: Option<String>,
    /// Auto-reset after seconds (None = manual reset required)
    pub auto_reset_secs: Option<u32>,
}

/// Enforcement mode
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum EnforcementMode {
    /// Enforce all limits
    #[default]
    Enforce,
    /// Log violations but don't block (audit mode)
    AuditOnly,
    /// Completely disabled
    Disabled,
}

// ============================================================================
// Safety Violations
// ============================================================================

/// Safety violation error
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SafetyViolation {
    /// Kill switch is active
    KillSwitchActive {
        /// Reason the kill switch was activated
        reason: String,
    },
    /// Rate limit exceeded
    RateLimitExceeded {
        /// Type of rate limit that was exceeded
        limit_type: RateLimitType,
        /// Current usage count
        current: u64,
        /// Configured limit
        limit: u64,
    },
    /// Resource limit exceeded
    ResourceLimitExceeded {
        /// Type of resource that was exceeded
        resource: ResourceType,
        /// Amount of resource requested
        requested: u64,
        /// Amount of resource available
        available: u64,
    },
    /// Content policy violation
    ContentPolicyViolation {
        /// Identifier of the violated policy
        policy_id: String,
        /// Human-readable violation details
        details: String,
    },
    /// Request timeout
    Timeout {
        /// Elapsed time in milliseconds
        elapsed_ms: u64,
        /// Configured timeout limit in milliseconds
        limit_ms: u64,
    },
}

impl std::fmt::Display for SafetyViolation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::KillSwitchActive { reason } => {
                write!(f, "kill switch active: {}", reason)
            }
            Self::RateLimitExceeded {
                limit_type,
                current,
                limit,
            } => {
                write!(
                    f,
                    "rate limit exceeded: {:?} ({}/{})",
                    limit_type, current, limit
                )
            }
            Self::ResourceLimitExceeded {
                resource,
                requested,
                available,
            } => {
                write!(
                    f,
                    "resource limit exceeded: {:?} (requested {}, available {})",
                    resource, requested, available
                )
            }
            Self::ContentPolicyViolation { policy_id, details } => {
                write!(f, "content policy violation [{}]: {}", policy_id, details)
            }
            Self::Timeout {
                elapsed_ms,
                limit_ms,
            } => {
                write!(f, "timeout: {}ms elapsed, limit {}ms", elapsed_ms, limit_ms)
            }
        }
    }
}

impl std::error::Error for SafetyViolation {}

/// Rate limit type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RateLimitType {
    /// Global requests per minute
    GlobalRpm,
    /// Per-source requests per minute
    PerSourceRpm,
    /// Tokens per minute
    TokensPerMinute,
}

/// Resource type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResourceType {
    /// Concurrent requests
    Concurrent,
    /// Tokens
    Tokens,
    /// Memory (MB)
    Memory,
    /// Time (ms)
    Time,
    /// Cost (cents)
    Cost,
}

// ============================================================================
// Resource Claim & Guard
// ============================================================================

/// Resource claim for a request
#[derive(Debug, Clone, Default)]
pub struct ResourceClaim {
    /// Number of concurrent slots
    pub concurrent_slots: u32,
    /// Estimated tokens
    pub tokens: u32,
    /// Estimated memory (GB)
    pub memory_gb: u32,
    /// Estimated time (ms)
    pub time_ms: u32,
    /// Estimated cost (cents)
    pub cost_cents: u32,
}

impl ResourceClaim {
    /// Create a new resource claim
    pub fn new() -> Self {
        Self::default()
    }

    /// Set concurrent slots
    pub fn with_concurrent(mut self, slots: u32) -> Self {
        self.concurrent_slots = slots;
        self
    }

    /// Set tokens
    pub fn with_tokens(mut self, tokens: u32) -> Self {
        self.tokens = tokens;
        self
    }

    /// Set memory
    pub fn with_memory_gb(mut self, gb: u32) -> Self {
        self.memory_gb = gb;
        self
    }

    /// Set time
    pub fn with_time_ms(mut self, ms: u32) -> Self {
        self.time_ms = ms;
        self
    }

    /// Set cost
    pub fn with_cost_cents(mut self, cents: u32) -> Self {
        self.cost_cents = cents;
        self
    }
}

/// RAII guard for acquired resources
pub struct ResourceGuard {
    enforcer: Arc<SafetyEnforcer>,
    claim: ResourceClaim,
    acquired_at: Instant,
}

impl ResourceGuard {
    /// Get elapsed time since acquisition
    pub fn elapsed(&self) -> Duration {
        self.acquired_at.elapsed()
    }

    /// Get the resource claim
    pub fn claim(&self) -> &ResourceClaim {
        &self.claim
    }

    /// Update the claim (e.g., after actual token count is known)
    pub fn update_tokens(&mut self, actual_tokens: u32) {
        let diff = actual_tokens as i64 - self.claim.tokens as i64;
        if diff > 0 {
            self.enforcer
                .usage
                .tokens
                .fetch_add(diff as u64, Ordering::Relaxed);
        } else if diff < 0 {
            // Use fetch_update with saturating subtraction to prevent
            // underflow wrapping the u64 counter to near-MAX, which
            // would permanently lock out all subsequent requests.
            let sub = (-diff) as u64;
            let _ = self.enforcer.usage.tokens.fetch_update(
                Ordering::Relaxed,
                Ordering::Relaxed,
                |current| Some(current.saturating_sub(sub)),
            );
        }
        self.claim.tokens = actual_tokens;
    }
}

impl Drop for ResourceGuard {
    fn drop(&mut self) {
        self.enforcer.release(&self.claim);
    }
}

// ============================================================================
// Rate Limiter
// ============================================================================

/// Per-source rate-limit bucket with built-in window tracking.
///
/// Stores `(window_floor, count)` packed in a single `AtomicU64`
/// so a single CAS atomically resets the count when the window
/// rolls over. Eliminates a lost-write race: under the older
/// design, `RateLimiter::maybe_reset` called
/// `per_source.clear()` at minute boundaries; an in-flight
/// `record_request` could fetch_add into the OLD `AtomicU64` just
/// as `clear()` removed it from the map, orphaning the increment
/// on the next request's freshly-inserted bucket — brief
/// per-source RPM cap exceedance at every minute boundary.
///
/// Post-fix, the bucket lives forever (until the periodic GC
/// sweep evicts long-stale entries) and self-resets on access:
/// each `try_acquire` / `current_count` checks whether the
/// stored `window_floor` matches the current floor; if not, the
/// CAS atomically resets count to 1 (or 0 for read) and bumps
/// the floor.
///
/// Bit layout: high 32 = `window_floor` (seconds-since-RateLimiter
/// creation, divided by reset_interval — wraps every ~136 years
/// at the default 60s window); low 32 = `count` (saturates at
/// `u32::MAX`, well above any realistic per-source RPM cap).
struct RateBucket {
    packed: AtomicU64,
}

impl RateBucket {
    const FLOOR_SHIFT: u64 = 32;
    const COUNT_MASK: u64 = 0xFFFF_FFFF;

    fn new(initial_floor: u32) -> Self {
        Self {
            packed: AtomicU64::new((initial_floor as u64) << Self::FLOOR_SHIFT),
        }
    }

    #[inline]
    fn split(packed: u64) -> (u32, u32) {
        let floor = (packed >> Self::FLOOR_SHIFT) as u32;
        let count = (packed & Self::COUNT_MASK) as u32;
        (floor, count)
    }

    #[inline]
    fn pack(floor: u32, count: u32) -> u64 {
        ((floor as u64) << Self::FLOOR_SHIFT) | (count as u64)
    }

    /// Try to acquire one slot at `current_floor`, capped at
    /// `effective_limit`. Resets the count atomically if the
    /// window has rolled over since the last access. Returns
    /// `Ok(new_count)` on success, `Err(observed_count)` if at
    /// or over the cap (no mutation in that case).
    fn try_acquire(&self, current_floor: u32, effective_limit: u64) -> Result<u32, u32> {
        let mut last_observed = 0u32;
        match self
            .packed
            .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| {
                let (cur_floor, cur_count) = Self::split(current);
                if cur_floor != current_floor {
                    // Window rolled over for this entry; this
                    // firing is the first of the new window.
                    Some(Self::pack(current_floor, 1))
                } else if (cur_count as u64) >= effective_limit {
                    last_observed = cur_count;
                    None
                } else {
                    Some(Self::pack(cur_floor, cur_count.saturating_add(1)))
                }
            }) {
            Ok(prev) => {
                // Compute the count our CAS produced from `prev`
                // (the value BEFORE our update) — re-loading
                // `self.packed` here would observe any subsequent
                // racing CAS, returning a count that doesn't
                // reflect what THIS acquire committed. Mirrors
                // the closure logic so the Ok value is "the count
                // assigned to this caller's slot", which is what
                // the regression tests assert.
                let (cur_floor, cur_count) = Self::split(prev);
                let new_count = if cur_floor != current_floor {
                    1
                } else {
                    cur_count.saturating_add(1)
                };
                Ok(new_count)
            }
            Err(_) => Err(last_observed),
        }
    }

    /// Read the current count for `current_floor`. Returns 0 if
    /// the entry's stored floor is stale (window rolled over but
    /// no `try_acquire` has refreshed yet).
    fn current_count(&self, current_floor: u32) -> u32 {
        let (cur_floor, cur_count) = Self::split(self.packed.load(Ordering::Acquire));
        if cur_floor == current_floor {
            cur_count
        } else {
            0
        }
    }

    /// Roll back one acquisition. Decrements count if the entry's
    /// stored floor still matches `current_floor`; otherwise
    /// no-op (the window already rolled and the bucket reset, so
    /// the slot we'd be "rolling back" is in a prior window).
    fn rollback(&self, current_floor: u32) {
        let _ = self
            .packed
            .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| {
                let (cur_floor, cur_count) = Self::split(current);
                if cur_floor == current_floor && cur_count > 0 {
                    Some(Self::pack(cur_floor, cur_count - 1))
                } else {
                    None
                }
            });
    }

    /// The stored window floor — used by the GC sweep to evict
    /// long-idle entries.
    fn floor(&self) -> u32 {
        Self::split(self.packed.load(Ordering::Relaxed)).0
    }
}

/// Token bucket rate limiter
struct RateLimiter {
    /// Global request count (resets each minute)
    global_requests: AtomicU64,
    /// Global token count (resets each minute)
    global_tokens: AtomicU64,
    /// Per-source request counts. Each bucket self-resets on
    /// access via packed `(window_floor, count)` — see
    /// [`RateBucket`] for the audit-#125 lost-write-race fix.
    per_source: DashMap<NodeId, RateBucket>,
    /// Last time the global counters were reset (per-source
    /// buckets self-reset; this only governs the global
    /// counters and the periodic GC sweep).
    last_reset: RwLock<Instant>,
    /// Anchor for converting `Instant`s to integer window
    /// floors. Set once at construction.
    created_at: Instant,
    /// Reset interval
    reset_interval: Duration,
}

impl RateLimiter {
    fn new() -> Self {
        let now = Instant::now();
        Self {
            global_requests: AtomicU64::new(0),
            global_tokens: AtomicU64::new(0),
            per_source: DashMap::new(),
            last_reset: RwLock::new(now),
            created_at: now,
            reset_interval: Duration::from_secs(60),
        }
    }

    /// Current window floor (window number since `created_at`).
    /// Per-source buckets compare their stored floor against this
    /// to detect rollover.
    #[inline]
    fn current_floor(&self) -> u32 {
        let secs = self.created_at.elapsed().as_secs();
        let interval_secs = self.reset_interval.as_secs().max(1);
        // Saturate at u32::MAX (~136 years at 60s windows). Wrap
        // would only matter at that horizon — past the lifetime
        // of any process.
        u32::try_from(secs / interval_secs).unwrap_or(u32::MAX)
    }

    fn maybe_reset(&self) {
        let should_reset = {
            let last = self.last_reset.read();
            last.elapsed() >= self.reset_interval
        };

        if should_reset {
            let mut last = self.last_reset.write();
            if last.elapsed() >= self.reset_interval {
                self.global_requests.store(0, Ordering::Relaxed);
                self.global_tokens.store(0, Ordering::Relaxed);
                // per_source buckets self-reset on access — no
                // `clear()` call here. Periodically sweep stale
                // entries (more than 5 windows old) so the map
                // doesn't grow unbounded under long-tail source
                // churn.
                self.gc_per_source_stale();
                *last = Instant::now();
            }
        }
    }

    /// Evict per_source entries whose stored window_floor is
    /// more than `gc_age_windows` behind the current floor.
    /// Called from `maybe_reset` (so amortized to once per
    /// reset_interval).
    fn gc_per_source_stale(&self) {
        let cur = self.current_floor();
        const GC_AGE_WINDOWS: u32 = 5;
        let cutoff = cur.saturating_sub(GC_AGE_WINDOWS);
        self.per_source.retain(|_, bucket| bucket.floor() >= cutoff);
    }

    fn check_global_rpm(&self, limit: u32, burst: f32) -> Result<(), SafetyViolation> {
        self.maybe_reset();
        let current = self.global_requests.load(Ordering::Relaxed);
        let effective_limit = (limit as f32 * burst) as u64;
        if current >= effective_limit {
            return Err(SafetyViolation::RateLimitExceeded {
                limit_type: RateLimitType::GlobalRpm,
                current,
                limit: effective_limit,
            });
        }
        Ok(())
    }

    fn check_source_rpm(
        &self,
        source: &NodeId,
        limit: u32,
        burst: f32,
    ) -> Result<(), SafetyViolation> {
        self.maybe_reset();
        let cur_floor = self.current_floor();
        let effective_limit = (limit as f32 * burst) as u64;
        // Only consult an existing bucket — `check_*` is read-only
        // (the corresponding write happens in `try_acquire_*`).
        // Avoid `entry().or_insert_with(...)` so a benign
        // never-acquired source doesn't bloat the per_source map.
        let current = if let Some(bucket) = self.per_source.get(source) {
            bucket.current_count(cur_floor) as u64
        } else {
            0
        };
        if current >= effective_limit {
            return Err(SafetyViolation::RateLimitExceeded {
                limit_type: RateLimitType::PerSourceRpm,
                current,
                limit: effective_limit,
            });
        }
        Ok(())
    }

    fn check_tokens(&self, tokens: u64, limit: u64, burst: f32) -> Result<(), SafetyViolation> {
        self.maybe_reset();
        let current = self.global_tokens.load(Ordering::Relaxed);
        let effective_limit = (limit as f64 * burst as f64) as u64;
        // `checked_add` guards against attacker-influenced `tokens`
        // plus accumulated `current` wrapping `u64::MAX`. In debug
        // builds the raw `current + tokens` panics (DoS); in release
        // it wraps and silently bypasses the check. Treat overflow
        // as definitely over the limit.
        let would_be = match current.checked_add(tokens) {
            Some(sum) => sum,
            None => {
                return Err(SafetyViolation::RateLimitExceeded {
                    limit_type: RateLimitType::TokensPerMinute,
                    current: u64::MAX,
                    limit: effective_limit,
                });
            }
        };
        if would_be > effective_limit {
            return Err(SafetyViolation::RateLimitExceeded {
                limit_type: RateLimitType::TokensPerMinute,
                current: would_be,
                limit: effective_limit,
            });
        }
        Ok(())
    }

    #[allow(dead_code)] // retained for tests; production path uses try_acquire_*
    fn record_request(&self, source: Option<&NodeId>, tokens: u64) {
        self.global_requests.fetch_add(1, Ordering::Relaxed);
        self.global_tokens.fetch_add(tokens, Ordering::Relaxed);
        if let Some(src) = source {
            let cur_floor = self.current_floor();
            let bucket = self
                .per_source
                .entry(*src)
                .or_insert_with(|| RateBucket::new(cur_floor));
            // No cap here — `record_request` is the explicit
            // "record without checking" path. Pass u64::MAX so
            // the CAS always commits the increment.
            let _ = bucket.try_acquire(cur_floor, u64::MAX);
        }
    }

    /// CAS-based "check and increment" for the global RPM cap. The
    /// request commits ONLY if the post-increment value still
    /// honors the cap; otherwise nothing is mutated and an Err is
    /// returned. Without this, the `check_global_rpm` + later
    /// `record_request` flow lets N concurrent acquirers all
    /// observe the pre-add value, all pass `check`, and all
    /// `record_request` past the cap.
    fn try_acquire_global_rpm(&self, limit: u32, burst: f32) -> Result<(), SafetyViolation> {
        self.maybe_reset();
        let effective_limit = (limit as f32 * burst) as u64;
        match self
            .global_requests
            .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| {
                if current >= effective_limit {
                    None
                } else {
                    Some(current + 1)
                }
            }) {
            Ok(_) => Ok(()),
            Err(current) => Err(SafetyViolation::RateLimitExceeded {
                limit_type: RateLimitType::GlobalRpm,
                current,
                limit: effective_limit,
            }),
        }
    }

    /// CAS-based "check and increment" for per-source RPM. Same
    /// commit-or-rollback contract as `try_acquire_global_rpm`.
    /// Each per-source bucket carries its own
    /// `(window_floor, count)` packed atomic and self-resets when
    /// the floor advances — no global `clear()` race window.
    fn try_acquire_source_rpm(
        &self,
        source: &NodeId,
        limit: u32,
        burst: f32,
    ) -> Result<(), SafetyViolation> {
        self.maybe_reset();
        let cur_floor = self.current_floor();
        let bucket = self
            .per_source
            .entry(*source)
            .or_insert_with(|| RateBucket::new(cur_floor));
        let effective_limit = (limit as f32 * burst) as u64;
        match bucket.try_acquire(cur_floor, effective_limit) {
            Ok(_) => Ok(()),
            Err(current) => Err(SafetyViolation::RateLimitExceeded {
                limit_type: RateLimitType::PerSourceRpm,
                current: current as u64,
                limit: effective_limit,
            }),
        }
    }

    /// CAS-based "check and add" for the tokens-per-minute counter.
    /// Treats `current + tokens` overflow as "definitely over limit"
    /// to avoid wrap-around DoS.
    fn try_acquire_tokens(
        &self,
        tokens: u64,
        limit: u64,
        burst: f32,
    ) -> Result<(), SafetyViolation> {
        self.maybe_reset();
        let effective_limit = (limit as f64 * burst as f64) as u64;
        match self
            .global_tokens
            .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| {
                let next = current.checked_add(tokens)?;
                if next > effective_limit {
                    None
                } else {
                    Some(next)
                }
            }) {
            Ok(_) => Ok(()),
            Err(current) => Err(SafetyViolation::RateLimitExceeded {
                limit_type: RateLimitType::TokensPerMinute,
                current,
                limit: effective_limit,
            }),
        }
    }

    /// Roll back a previous successful `try_acquire_*` commit.
    /// Called from `acquire()` when a later step fails so the
    /// counter doesn't overcount.
    fn rollback_global_rpm(&self) {
        self.global_requests.fetch_sub(1, Ordering::Relaxed);
    }

    fn rollback_source_rpm(&self, source: &NodeId) {
        if let Some(bucket) = self.per_source.get(source) {
            bucket.rollback(self.current_floor());
        }
    }

    #[allow(dead_code)] // symmetric with rollback_global_rpm / rollback_source_rpm
    fn rollback_tokens(&self, tokens: u64) {
        self.global_tokens.fetch_sub(tokens, Ordering::Relaxed);
    }
}

// ============================================================================
// Resource Usage Tracking
// ============================================================================

/// Atomic resource usage counters
struct AtomicResourceUsage {
    concurrent: AtomicU32,
    tokens: AtomicU64,
    memory_gb: AtomicU32,
    cost_cents_per_hour: AtomicU32,
    hour_start: RwLock<Instant>,
}

impl AtomicResourceUsage {
    fn new() -> Self {
        Self {
            concurrent: AtomicU32::new(0),
            tokens: AtomicU64::new(0),
            memory_gb: AtomicU32::new(0),
            cost_cents_per_hour: AtomicU32::new(0),
            hour_start: RwLock::new(Instant::now()),
        }
    }

    fn maybe_reset_hourly(&self) {
        let should_reset = {
            let start = self.hour_start.read();
            start.elapsed() >= Duration::from_secs(3600)
        };

        if should_reset {
            let mut start = self.hour_start.write();
            if start.elapsed() >= Duration::from_secs(3600) {
                self.cost_cents_per_hour.store(0, Ordering::Relaxed);
                *start = Instant::now();
            }
        }
    }
}

/// Usage statistics snapshot
#[derive(Debug, Clone, Default)]
pub struct UsageStats {
    /// Current concurrent requests
    pub concurrent: u32,
    /// Total tokens used (current window)
    pub tokens: u64,
    /// Current memory usage (MB)
    pub memory_gb: u32,
    /// Cost this hour (cents)
    pub cost_cents_per_hour: u32,
    /// Global requests this minute
    pub requests_per_minute: u64,
    /// Tokens this minute
    pub tokens_per_minute: u64,
}

// ============================================================================
// Audit Trail
// ============================================================================

/// Audit log entry
#[derive(Debug, Clone, Serialize)]
pub struct AuditEntry {
    /// Timestamp in nanoseconds
    pub timestamp_ns: u64,
    /// Event type
    pub event_type: AuditEventType,
    /// Source node (if applicable)
    pub source_node: Option<NodeId>,
    /// Request ID (if applicable)
    pub request_id: Option<u128>,
    /// Event details
    pub details: HashMap<String, String>,
    /// Outcome
    pub outcome: AuditOutcome,
}

/// Audit event type
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
pub enum AuditEventType {
    /// Request received
    RequestReceived,
    /// Request allowed
    RequestAllowed,
    /// Request blocked
    RequestBlocked,
    /// Rate limit hit
    RateLimitHit,
    /// Resource limit hit
    ResourceLimitHit,
    /// Content policy violation
    ContentPolicyViolation,
    /// Kill switch triggered
    KillSwitchTriggered,
    /// Kill switch reset
    KillSwitchReset,
    /// Envelope updated
    EnvelopeUpdated,
}

/// Audit outcome
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
pub enum AuditOutcome {
    /// Success
    Success,
    /// Blocked
    Blocked,
    /// Warning issued
    Warning,
    /// Error occurred
    Error,
}

/// Audit sink trait for external logging
pub trait AuditSink: Send + Sync {
    /// Write an audit entry
    fn write(&self, entry: &AuditEntry);
    /// Flush pending entries
    fn flush(&self);
}

/// In-memory audit log
struct AuditLog {
    entries: RwLock<VecDeque<AuditEntry>>,
    config: AuditConfig,
    sink: Option<Box<dyn AuditSink>>,
}

impl AuditLog {
    fn new(config: AuditConfig) -> Self {
        Self {
            entries: RwLock::new(VecDeque::with_capacity(config.max_entries)),
            config,
            sink: None,
        }
    }

    fn log(&self, entry: AuditEntry) {
        if !self.config.enabled {
            return;
        }

        // Check if we should log this event
        let should_log = match entry.outcome {
            AuditOutcome::Success => self.config.log_success,
            AuditOutcome::Blocked => self.config.log_blocked,
            AuditOutcome::Warning => self.config.log_warnings,
            AuditOutcome::Error => true,
        };

        if !should_log {
            return;
        }

        // Write to sink if available
        if let Some(ref sink) = self.sink {
            sink.write(&entry);
        }

        // Store in memory (O(1) eviction via VecDeque)
        let mut entries = self.entries.write();
        if entries.len() >= self.config.max_entries {
            entries.pop_front();
        }
        entries.push_back(entry);
    }

    fn get_entries(&self, limit: usize) -> Vec<AuditEntry> {
        let entries = self.entries.read();
        entries.iter().rev().take(limit).cloned().collect()
    }

    fn clear(&self) {
        self.entries.write().clear();
    }
}

// ============================================================================
// Request Context
// ============================================================================

/// Request context for safety checks
#[derive(Debug, Clone, Default)]
pub struct SafetyRequest {
    /// Source node
    pub source_node: Option<NodeId>,
    /// Request ID
    pub request_id: Option<u128>,
    /// Content to check (optional)
    pub content: Option<String>,
    /// Content size in bytes
    pub content_size: usize,
    /// Estimated tokens
    pub estimated_tokens: u32,
    /// Custom metadata
    pub metadata: HashMap<String, String>,
}

impl SafetyRequest {
    /// Create a new safety request
    pub fn new() -> Self {
        Self::default()
    }

    /// Set source node
    pub fn with_source(mut self, node: NodeId) -> Self {
        self.source_node = Some(node);
        self
    }

    /// Set request ID
    pub fn with_request_id(mut self, id: u128) -> Self {
        self.request_id = Some(id);
        self
    }

    /// Set content
    pub fn with_content(mut self, content: impl Into<String>) -> Self {
        let content = content.into();
        self.content_size = content.len();
        self.content = Some(content);
        self
    }

    /// Set content size only (without content)
    pub fn with_content_size(mut self, size: usize) -> Self {
        self.content_size = size;
        self
    }

    /// Set estimated tokens
    pub fn with_tokens(mut self, tokens: u32) -> Self {
        self.estimated_tokens = tokens;
        self
    }
}

// ============================================================================
// Safety Enforcer
// ============================================================================

/// Safety enforcer (hot path optimized)
pub struct SafetyEnforcer {
    /// Current envelope
    envelope: RwLock<SafetyEnvelope>,
    /// Resource usage
    usage: AtomicResourceUsage,
    /// Rate limiter
    rate_limiter: RateLimiter,
    /// Audit log
    audit_log: AuditLog,
    /// Kill switch state
    kill_switch: AtomicBool,
    /// Kill switch timestamp
    kill_switch_at: RwLock<Option<Instant>>,
    /// Kill switch reason
    kill_switch_reason: RwLock<Option<String>>,
    /// Compiled content patterns (for hot path)
    #[allow(dead_code)]
    compiled_patterns: RwLock<Vec<(String, regex::Regex)>>,
}

impl SafetyEnforcer {
    /// Create a new safety enforcer with default envelope
    pub fn new() -> Self {
        Self::with_envelope(SafetyEnvelope::default())
    }

    /// Create with a specific envelope
    pub fn with_envelope(envelope: SafetyEnvelope) -> Self {
        let audit_log = AuditLog::new(envelope.audit.clone());
        let kill_switch = envelope.kill_switch.enabled;

        Self {
            envelope: RwLock::new(envelope),
            usage: AtomicResourceUsage::new(),
            rate_limiter: RateLimiter::new(),
            audit_log,
            kill_switch: AtomicBool::new(kill_switch),
            kill_switch_at: RwLock::new(None),
            kill_switch_reason: RwLock::new(None),
            compiled_patterns: RwLock::new(Vec::new()),
        }
    }

    /// Update the envelope
    pub fn update_envelope(&self, envelope: SafetyEnvelope) {
        *self.envelope.write() = envelope;
        self.log_event(AuditEventType::EnvelopeUpdated, None, AuditOutcome::Success);
    }

    /// Check if a request is allowed (hot path)
    pub fn check(&self, req: &SafetyRequest) -> Result<(), SafetyViolation> {
        let envelope = self.envelope.read();

        // Fast path: disabled mode
        if envelope.mode == EnforcementMode::Disabled {
            return Ok(());
        }

        // Check kill switch first
        self.check_kill_switch()?;

        // Check rate limits
        self.check_rate_limits(req, &envelope)?;

        // Check content policies
        self.check_content_policies(req, &envelope)?;

        // Log success if in audit-only mode
        if envelope.mode == EnforcementMode::AuditOnly {
            self.log_event(
                AuditEventType::RequestAllowed,
                req.source_node,
                AuditOutcome::Success,
            );
        }

        Ok(())
    }

    /// Acquire resources for a request
    ///
    /// Previously this did `load + compare` in
    /// `check_resource_limits`, then unconditionally `fetch_add`'d
    /// each counter. N concurrent acquirers all observed `current=0`
    /// and all proceeded past the cap — the kill-switch / safety
    /// envelope was breakable under load. The fix uses `fetch_update`
    /// (compare-and-swap loop) for each cumulative counter so the
    /// check + add is atomic per resource, and rolls back any partial
    /// successes if a later resource fails. `tokens` is per-request
    /// (not cumulative) so it stays as a straight load.
    pub fn acquire(
        self: &Arc<Self>,
        req: &SafetyRequest,
        claim: ResourceClaim,
    ) -> Result<ResourceGuard, SafetyViolation> {
        let envelope = self.envelope.read();

        // Fast path: disabled mode
        if envelope.mode == EnforcementMode::Disabled {
            return Ok(ResourceGuard {
                enforcer: Arc::clone(self),
                claim,
                acquired_at: Instant::now(),
            });
        }

        // Check kill switch
        self.check_kill_switch()?;

        let limits = &envelope.resource_limits;
        let enforce = envelope.mode == EnforcementMode::Enforce;

        // tokens is per-request (not cumulative) so a plain
        // compare against the per-request cap is fine.
        if enforce && claim.tokens > limits.max_tokens_per_request {
            return Err(SafetyViolation::ResourceLimitExceeded {
                resource: ResourceType::Tokens,
                requested: claim.tokens as u64,
                available: limits.max_tokens_per_request as u64,
            });
        }

        // Reset the cost window before the cost CAS so a stale
        // accumulator doesn't reject a legitimate request right
        // after the hour rollover.
        self.usage.maybe_reset_hourly();

        // Helper: atomically `fetch_update` an `AtomicU32`
        // counter so that `add` only commits if `current + add
        // <= max`. Returns Err with the current value on cap
        // exceeded.
        fn try_fetch_add_capped_u32(
            counter: &std::sync::atomic::AtomicU32,
            add: u32,
            max: u32,
        ) -> Result<(), u32> {
            counter
                .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| {
                    let next = current.saturating_add(add);
                    if next > max {
                        None
                    } else {
                        Some(next)
                    }
                })
                .map(|_| ())
        }

        // 1. Concurrent slots.
        if enforce {
            if let Err(cur) = try_fetch_add_capped_u32(
                &self.usage.concurrent,
                claim.concurrent_slots,
                limits.max_concurrent,
            ) {
                return Err(SafetyViolation::ResourceLimitExceeded {
                    resource: ResourceType::Concurrent,
                    requested: claim.concurrent_slots as u64,
                    available: limits.max_concurrent.saturating_sub(cur) as u64,
                });
            }
        } else {
            self.usage
                .concurrent
                .fetch_add(claim.concurrent_slots, Ordering::Relaxed);
        }

        // 2. Memory. On failure, roll back concurrent.
        if enforce {
            if let Err(cur) = try_fetch_add_capped_u32(
                &self.usage.memory_gb,
                claim.memory_gb,
                limits.max_memory_gb,
            ) {
                self.usage
                    .concurrent
                    .fetch_sub(claim.concurrent_slots, Ordering::Relaxed);
                return Err(SafetyViolation::ResourceLimitExceeded {
                    resource: ResourceType::Memory,
                    requested: claim.memory_gb as u64,
                    available: limits.max_memory_gb.saturating_sub(cur) as u64,
                });
            }
        } else {
            self.usage
                .memory_gb
                .fetch_add(claim.memory_gb, Ordering::Relaxed);
        }

        // 3. Hourly cost. On failure, roll back concurrent + memory.
        if enforce {
            if let Err(cur) = try_fetch_add_capped_u32(
                &self.usage.cost_cents_per_hour,
                claim.cost_cents,
                limits.max_cost_per_hour_cents,
            ) {
                self.usage
                    .concurrent
                    .fetch_sub(claim.concurrent_slots, Ordering::Relaxed);
                self.usage
                    .memory_gb
                    .fetch_sub(claim.memory_gb, Ordering::Relaxed);
                return Err(SafetyViolation::ResourceLimitExceeded {
                    resource: ResourceType::Cost,
                    requested: claim.cost_cents as u64,
                    available: limits.max_cost_per_hour_cents.saturating_sub(cur) as u64,
                });
            }
        } else {
            self.usage
                .cost_cents_per_hour
                .fetch_add(claim.cost_cents, Ordering::Relaxed);
        }

        // 4. Rate limits — global RPM, per-source RPM, tokens-per-
        //    minute. Previously these were checked only in `check()`
        //    (load + compare) with the increment happening separately
        //    via `record_request`. N concurrent acquirers could all
        //    pass `check()`, then all `record_request` past the cap
        //    — same TOCTOU as the resource limits. CAS-ifying the
        //    check + add per counter (with cross-counter rollback)
        //    closes the race.
        let rate = &envelope.rate_limits;
        let rate_burst = rate.burst_multiplier;

        if enforce {
            if let Err(e) = self
                .rate_limiter
                .try_acquire_global_rpm(rate.global_rpm, rate_burst)
            {
                self.usage
                    .concurrent
                    .fetch_sub(claim.concurrent_slots, Ordering::Relaxed);
                self.usage
                    .memory_gb
                    .fetch_sub(claim.memory_gb, Ordering::Relaxed);
                self.usage
                    .cost_cents_per_hour
                    .fetch_sub(claim.cost_cents, Ordering::Relaxed);
                self.log_event(
                    AuditEventType::RateLimitHit,
                    req.source_node,
                    AuditOutcome::Blocked,
                );
                return Err(e);
            }
        }

        if enforce {
            if let Some(ref source) = req.source_node {
                if let Err(e) = self.rate_limiter.try_acquire_source_rpm(
                    source,
                    rate.per_source_rpm,
                    rate_burst,
                ) {
                    self.rate_limiter.rollback_global_rpm();
                    self.usage
                        .concurrent
                        .fetch_sub(claim.concurrent_slots, Ordering::Relaxed);
                    self.usage
                        .memory_gb
                        .fetch_sub(claim.memory_gb, Ordering::Relaxed);
                    self.usage
                        .cost_cents_per_hour
                        .fetch_sub(claim.cost_cents, Ordering::Relaxed);
                    self.log_event(
                        AuditEventType::RateLimitHit,
                        req.source_node,
                        AuditOutcome::Blocked,
                    );
                    return Err(e);
                }
            }
        }

        if enforce {
            if let Err(e) = self.rate_limiter.try_acquire_tokens(
                claim.tokens as u64,
                rate.tokens_per_minute,
                rate_burst,
            ) {
                if let Some(ref source) = req.source_node {
                    self.rate_limiter.rollback_source_rpm(source);
                }
                self.rate_limiter.rollback_global_rpm();
                self.usage
                    .concurrent
                    .fetch_sub(claim.concurrent_slots, Ordering::Relaxed);
                self.usage
                    .memory_gb
                    .fetch_sub(claim.memory_gb, Ordering::Relaxed);
                self.usage
                    .cost_cents_per_hour
                    .fetch_sub(claim.cost_cents, Ordering::Relaxed);
                self.log_event(
                    AuditEventType::RateLimitHit,
                    req.source_node,
                    AuditOutcome::Blocked,
                );
                return Err(e);
            }
        } else {
            // Audit-only: still increment so observers see realistic
            // counters without any commit failing.
            //
            // Saturating CAS rather than `fetch_add` so a long-lived
            // process can't tip the audit counter into wrap (release)
            // or panic (debug). The audit-only path takes no commit
            // failure on overflow — by definition this counter only
            // drives observability dashboards — so wrap is silent
            // corruption (operators see the counter reset to ~0 mid-
            // window and conclude traffic dropped). `fetch_update`
            // with saturating_add inside is the standard pattern.
            let _ = self.rate_limiter.global_tokens.fetch_update(
                Ordering::Relaxed,
                Ordering::Relaxed,
                |v| Some(v.saturating_add(claim.tokens as u64)),
            );
        }

        // tokens (per-request `usage` counter) — free-running,
        // already-bounded by the per-request cap above.
        self.usage
            .tokens
            .fetch_add(claim.tokens as u64, Ordering::Relaxed);

        if !enforce {
            // Audit-only / Disabled: bump RPM counters too so observed
            // rates still reflect actual traffic.
            self.rate_limiter
                .global_requests
                .fetch_add(1, Ordering::Relaxed);
            if let Some(ref source) = req.source_node {
                let cur_floor = self.rate_limiter.current_floor();
                let bucket = self
                    .rate_limiter
                    .per_source
                    .entry(*source)
                    .or_insert_with(|| RateBucket::new(cur_floor));
                // Audit-only path: record without enforcing a cap
                // (pass u64::MAX so the CAS commits unconditionally).
                let _ = bucket.try_acquire(cur_floor, u64::MAX);
            }
        }

        // Log acquisition
        self.log_event(
            AuditEventType::RequestAllowed,
            req.source_node,
            AuditOutcome::Success,
        );

        Ok(ResourceGuard {
            enforcer: Arc::clone(self),
            claim,
            acquired_at: Instant::now(),
        })
    }

    /// Release resources (called by ResourceGuard on drop)
    fn release(&self, claim: &ResourceClaim) {
        // Use `fetch_update` + `saturating_sub` rather than raw
        // `fetch_sub` on `concurrent` and `memory_gb`. `acquire()`
        // short-circuits in `EnforcementMode::Disabled` and returns
        // a guard WITHOUT incrementing those counters; a raw
        // `fetch_sub` from a counter at 0 would wrap to ~`u32::MAX`,
        // and the next `Enforce`-mode `acquire` would see
        // `current.saturating_add(claim) > max_concurrent` and reject
        // every request forever (mode is hot-swappable via
        // `update_envelope`, so warm-up in `Disabled` then flip to
        // `Enforce` is the real-world trigger). The matching
        // tokens/cost paths already use `fetch_update` +
        // `saturating_sub` for exactly this reason.
        //
        // Use `AcqRel` (not `Relaxed`) to mirror the acquire path's
        // `try_fetch_add_capped_u32` ordering. Pre-fix the
        // asymmetric `Relaxed` release on weakly-ordered cores
        // (ARM / RISC-V) let a subsequent acquirer observe the
        // post-release counter while the release-side caller's
        // prior reads of the freed resource were still visible to
        // its CPU only — a window where the resource looked
        // available to the acquirer while the previous owner was
        // still touching it. The total counter eventually
        // converges, but the ordering mismatch produced
        // observable drift on metrics readers.
        let _ =
            self.usage
                .concurrent
                .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| {
                    Some(current.saturating_sub(claim.concurrent_slots))
                });
        let _ = self
            .usage
            .memory_gb
            .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| {
                Some(current.saturating_sub(claim.memory_gb))
            });
        // Release tokens and cost that were acquired — without this,
        // both counters grow monotonically, hitting limits prematurely.
        let _ = self
            .usage
            .tokens
            .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| {
                Some(current.saturating_sub(claim.tokens as u64))
            });
        let _ = self.usage.cost_cents_per_hour.fetch_update(
            Ordering::AcqRel,
            Ordering::Acquire,
            |current| Some(current.saturating_sub(claim.cost_cents)),
        );
    }

    /// Trigger the kill switch
    pub fn kill(&self, reason: impl Into<String>) {
        let reason = reason.into();
        self.kill_switch.store(true, Ordering::SeqCst);
        *self.kill_switch_at.write() = Some(Instant::now());
        *self.kill_switch_reason.write() = Some(reason.clone());

        self.log_event_with_details(
            AuditEventType::KillSwitchTriggered,
            None,
            AuditOutcome::Success,
            [("reason".to_string(), reason)].into_iter().collect(),
        );
    }

    /// Reset the kill switch
    pub fn reset(&self) {
        self.kill_switch.store(false, Ordering::SeqCst);
        *self.kill_switch_at.write() = None;
        *self.kill_switch_reason.write() = None;

        self.log_event(AuditEventType::KillSwitchReset, None, AuditOutcome::Success);
    }

    /// Check if kill switch is active
    pub fn is_killed(&self) -> bool {
        self.kill_switch.load(Ordering::Relaxed)
    }

    /// Get current usage statistics
    pub fn usage(&self) -> UsageStats {
        UsageStats {
            concurrent: self.usage.concurrent.load(Ordering::Relaxed),
            tokens: self.usage.tokens.load(Ordering::Relaxed),
            memory_gb: self.usage.memory_gb.load(Ordering::Relaxed),
            cost_cents_per_hour: self.usage.cost_cents_per_hour.load(Ordering::Relaxed),
            requests_per_minute: self.rate_limiter.global_requests.load(Ordering::Relaxed),
            tokens_per_minute: self.rate_limiter.global_tokens.load(Ordering::Relaxed),
        }
    }

    /// Get recent audit entries
    pub fn audit_entries(&self, limit: usize) -> Vec<AuditEntry> {
        self.audit_log.get_entries(limit)
    }

    /// Clear audit log
    pub fn clear_audit(&self) {
        self.audit_log.clear();
    }

    /// Get the current envelope
    pub fn envelope(&self) -> SafetyEnvelope {
        self.envelope.read().clone()
    }

    // Internal methods

    fn check_kill_switch(&self) -> Result<(), SafetyViolation> {
        if !self.kill_switch.load(Ordering::Relaxed) {
            return Ok(());
        }

        // Check auto-reset
        let envelope = self.envelope.read();
        if let Some(auto_reset_secs) = envelope.kill_switch.auto_reset_secs {
            if let Some(killed_at) = *self.kill_switch_at.read() {
                if killed_at.elapsed() >= Duration::from_secs(auto_reset_secs as u64) {
                    drop(envelope);
                    self.reset();
                    return Ok(());
                }
            }
        }

        let reason = self
            .kill_switch_reason
            .read()
            .clone()
            .unwrap_or_else(|| "kill switch active".to_string());

        Err(SafetyViolation::KillSwitchActive { reason })
    }

    fn check_rate_limits(
        &self,
        req: &SafetyRequest,
        envelope: &SafetyEnvelope,
    ) -> Result<(), SafetyViolation> {
        let limits = &envelope.rate_limits;
        let burst = limits.burst_multiplier;
        let outcome = match envelope.mode {
            EnforcementMode::Enforce => AuditOutcome::Blocked,
            EnforcementMode::AuditOnly => AuditOutcome::Warning,
            EnforcementMode::Disabled => AuditOutcome::Warning,
        };

        // Check global RPM
        if let Err(e) = self.rate_limiter.check_global_rpm(limits.global_rpm, burst) {
            self.log_event(AuditEventType::RateLimitHit, req.source_node, outcome);
            if envelope.mode == EnforcementMode::Enforce {
                return Err(e);
            }
        }

        // Check per-source RPM
        if let Some(ref source) = req.source_node {
            if let Err(e) = self
                .rate_limiter
                .check_source_rpm(source, limits.per_source_rpm, burst)
            {
                self.log_event(AuditEventType::RateLimitHit, req.source_node, outcome);
                if envelope.mode == EnforcementMode::Enforce {
                    return Err(e);
                }
            }
        }

        // Check tokens per minute
        if let Err(e) = self.rate_limiter.check_tokens(
            req.estimated_tokens as u64,
            limits.tokens_per_minute,
            burst,
        ) {
            self.log_event(AuditEventType::RateLimitHit, req.source_node, outcome);
            if envelope.mode == EnforcementMode::Enforce {
                return Err(e);
            }
        }

        Ok(())
    }

    #[allow(dead_code)]
    fn check_resource_limits(
        &self,
        claim: &ResourceClaim,
        envelope: &SafetyEnvelope,
    ) -> Result<(), SafetyViolation> {
        let limits = &envelope.resource_limits;

        // Check concurrent
        // Use saturating arithmetic to prevent underflow when current > max
        // (possible if limits were reduced while requests are in-flight).
        let current_concurrent = self.usage.concurrent.load(Ordering::Relaxed);
        if current_concurrent.saturating_add(claim.concurrent_slots) > limits.max_concurrent
            && envelope.mode == EnforcementMode::Enforce
        {
            return Err(SafetyViolation::ResourceLimitExceeded {
                resource: ResourceType::Concurrent,
                requested: claim.concurrent_slots as u64,
                available: limits.max_concurrent.saturating_sub(current_concurrent) as u64,
            });
        }

        // Check tokens
        if claim.tokens > limits.max_tokens_per_request && envelope.mode == EnforcementMode::Enforce
        {
            return Err(SafetyViolation::ResourceLimitExceeded {
                resource: ResourceType::Tokens,
                requested: claim.tokens as u64,
                available: limits.max_tokens_per_request as u64,
            });
        }

        // Check memory
        let current_memory = self.usage.memory_gb.load(Ordering::Relaxed);
        if current_memory.saturating_add(claim.memory_gb) > limits.max_memory_gb
            && envelope.mode == EnforcementMode::Enforce
        {
            return Err(SafetyViolation::ResourceLimitExceeded {
                resource: ResourceType::Memory,
                requested: claim.memory_gb as u64,
                available: limits.max_memory_gb.saturating_sub(current_memory) as u64,
            });
        }

        // Check hourly cost
        self.usage.maybe_reset_hourly();
        let current_cost = self.usage.cost_cents_per_hour.load(Ordering::Relaxed);
        if current_cost.saturating_add(claim.cost_cents) > limits.max_cost_per_hour_cents
            && envelope.mode == EnforcementMode::Enforce
        {
            return Err(SafetyViolation::ResourceLimitExceeded {
                resource: ResourceType::Cost,
                requested: claim.cost_cents as u64,
                available: limits.max_cost_per_hour_cents.saturating_sub(current_cost) as u64,
            });
        }

        Ok(())
    }

    fn check_content_policies(
        &self,
        req: &SafetyRequest,
        envelope: &SafetyEnvelope,
    ) -> Result<(), SafetyViolation> {
        for policy in &envelope.content_policies {
            if !policy.enabled {
                continue;
            }

            if let Err(violation) = self.check_policy(req, policy) {
                match policy.action {
                    PolicyAction::Block => {
                        if envelope.mode == EnforcementMode::Enforce {
                            self.log_event(
                                AuditEventType::ContentPolicyViolation,
                                req.source_node,
                                AuditOutcome::Blocked,
                            );
                            return Err(violation);
                        }
                    }
                    PolicyAction::Warn => {
                        self.log_event(
                            AuditEventType::ContentPolicyViolation,
                            req.source_node,
                            AuditOutcome::Warning,
                        );
                    }
                    PolicyAction::Log => {
                        self.log_event(
                            AuditEventType::ContentPolicyViolation,
                            req.source_node,
                            AuditOutcome::Warning,
                        );
                    }
                    PolicyAction::Redact => {
                        // Redaction would need mutable access to content
                        // For now, just log
                        self.log_event(
                            AuditEventType::ContentPolicyViolation,
                            req.source_node,
                            AuditOutcome::Warning,
                        );
                    }
                }
            }
        }

        Ok(())
    }

    fn check_policy(
        &self,
        req: &SafetyRequest,
        policy: &ContentPolicy,
    ) -> Result<(), SafetyViolation> {
        match &policy.check {
            ContentCheck::MaxSize(max_size) => {
                if req.content_size > *max_size {
                    return Err(SafetyViolation::ContentPolicyViolation {
                        policy_id: policy.id.clone(),
                        details: format!(
                            "content size {} exceeds max {}",
                            req.content_size, max_size
                        ),
                    });
                }
            }
            ContentCheck::BlockPatterns(patterns) => {
                if let Some(ref content) = req.content {
                    for pattern in patterns {
                        if content.contains(pattern) {
                            return Err(SafetyViolation::ContentPolicyViolation {
                                policy_id: policy.id.clone(),
                                details: format!("blocked pattern found: {}", pattern),
                            });
                        }
                    }
                }
            }
            ContentCheck::RequirePatterns(patterns) => {
                if let Some(ref content) = req.content {
                    for pattern in patterns {
                        if !content.contains(pattern) {
                            return Err(SafetyViolation::ContentPolicyViolation {
                                policy_id: policy.id.clone(),
                                details: format!("required pattern not found: {}", pattern),
                            });
                        }
                    }
                }
            }
            ContentCheck::Custom { validator_id } => {
                // Custom validators would be registered externally
                // For now, this is a placeholder
                let _ = validator_id;
            }
        }

        Ok(())
    }

    fn log_event(
        &self,
        event_type: AuditEventType,
        source_node: Option<NodeId>,
        outcome: AuditOutcome,
    ) {
        self.log_event_with_details(event_type, source_node, outcome, HashMap::new());
    }

    fn log_event_with_details(
        &self,
        event_type: AuditEventType,
        source_node: Option<NodeId>,
        outcome: AuditOutcome,
        details: HashMap<String, String>,
    ) {
        let entry = AuditEntry {
            timestamp_ns: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos() as u64)
                .unwrap_or(0),
            event_type,
            source_node,
            request_id: None,
            details,
            outcome,
        };
        self.audit_log.log(entry);
    }
}

impl Default for SafetyEnforcer {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn make_node_id(n: u8) -> NodeId {
        let mut id = [0u8; 32];
        id[0] = n;
        id
    }

    #[test]
    fn test_default_envelope() {
        let envelope = SafetyEnvelope::default();
        assert_eq!(envelope.mode, EnforcementMode::Enforce);
        assert_eq!(envelope.resource_limits.max_concurrent, 1000);
        assert_eq!(envelope.rate_limits.global_rpm, 10_000);
    }

    #[test]
    fn test_safety_enforcer_check_passes() {
        let enforcer = SafetyEnforcer::new();
        let req = SafetyRequest::new().with_tokens(100);

        let result = enforcer.check(&req);
        assert!(result.is_ok());
    }

    #[test]
    fn test_kill_switch() {
        let enforcer = SafetyEnforcer::new();
        let req = SafetyRequest::new();

        // Initially should pass
        assert!(enforcer.check(&req).is_ok());
        assert!(!enforcer.is_killed());

        // Trigger kill switch
        enforcer.kill("test kill");
        assert!(enforcer.is_killed());

        // Should now fail
        let result = enforcer.check(&req);
        assert!(matches!(
            result,
            Err(SafetyViolation::KillSwitchActive { .. })
        ));

        // Reset
        enforcer.reset();
        assert!(!enforcer.is_killed());
        assert!(enforcer.check(&req).is_ok());
    }

    #[test]
    fn test_resource_acquisition() {
        let enforcer = Arc::new(SafetyEnforcer::new());
        let req = SafetyRequest::new();
        let claim = ResourceClaim::new().with_concurrent(1).with_tokens(100);

        // Acquire resources
        let guard = enforcer.acquire(&req, claim).unwrap();
        assert_eq!(enforcer.usage().concurrent, 1);
        assert_eq!(enforcer.usage().tokens, 100);

        // Release resources (drop guard)
        drop(guard);
        assert_eq!(enforcer.usage().concurrent, 0);
    }

    #[test]
    fn test_concurrent_limit() {
        let envelope = SafetyEnvelope {
            resource_limits: ResourceEnvelope {
                max_concurrent: 2,
                ..Default::default()
            },
            ..Default::default()
        };
        let enforcer = Arc::new(SafetyEnforcer::with_envelope(envelope));
        let req = SafetyRequest::new();
        let claim = ResourceClaim::new().with_concurrent(1);

        // Acquire 2 slots
        let _guard1 = enforcer.acquire(&req, claim.clone()).unwrap();
        let _guard2 = enforcer.acquire(&req, claim.clone()).unwrap();

        // Third should fail
        let result = enforcer.acquire(&req, claim);
        assert!(matches!(
            result,
            Err(SafetyViolation::ResourceLimitExceeded {
                resource: ResourceType::Concurrent,
                ..
            })
        ));
    }

    #[test]
    fn test_content_policy_max_size() {
        let envelope = SafetyEnvelope {
            content_policies: vec![ContentPolicy {
                id: "max-size".to_string(),
                check: ContentCheck::MaxSize(100),
                action: PolicyAction::Block,
                enabled: true,
            }],
            ..Default::default()
        };
        let enforcer = SafetyEnforcer::with_envelope(envelope);

        // Small content should pass
        let req = SafetyRequest::new().with_content_size(50);
        assert!(enforcer.check(&req).is_ok());

        // Large content should fail
        let req = SafetyRequest::new().with_content_size(200);
        assert!(matches!(
            enforcer.check(&req),
            Err(SafetyViolation::ContentPolicyViolation { .. })
        ));
    }

    #[test]
    fn test_content_policy_block_patterns() {
        let envelope = SafetyEnvelope {
            content_policies: vec![ContentPolicy {
                id: "block-bad".to_string(),
                check: ContentCheck::BlockPatterns(vec!["bad_word".to_string()]),
                action: PolicyAction::Block,
                enabled: true,
            }],
            ..Default::default()
        };
        let enforcer = SafetyEnforcer::with_envelope(envelope);

        // Clean content should pass
        let req = SafetyRequest::new().with_content("hello world");
        assert!(enforcer.check(&req).is_ok());

        // Content with blocked pattern should fail
        let req = SafetyRequest::new().with_content("this has a bad_word in it");
        assert!(matches!(
            enforcer.check(&req),
            Err(SafetyViolation::ContentPolicyViolation { .. })
        ));
    }

    #[test]
    fn test_audit_only_mode() {
        let envelope = SafetyEnvelope {
            mode: EnforcementMode::AuditOnly,
            content_policies: vec![ContentPolicy {
                id: "max-size".to_string(),
                check: ContentCheck::MaxSize(100),
                action: PolicyAction::Block,
                enabled: true,
            }],
            ..Default::default()
        };
        let enforcer = SafetyEnforcer::with_envelope(envelope);

        // Should pass even with violation (audit only)
        let req = SafetyRequest::new().with_content_size(200);
        assert!(enforcer.check(&req).is_ok());
    }

    #[test]
    fn test_disabled_mode() {
        let envelope = SafetyEnvelope {
            mode: EnforcementMode::Disabled,
            ..Default::default()
        };
        let enforcer = SafetyEnforcer::with_envelope(envelope);

        // Should pass even with kill switch (disabled mode)
        enforcer.kill("test");
        let req = SafetyRequest::new();
        assert!(enforcer.check(&req).is_ok());
    }

    #[test]
    fn test_usage_stats() {
        let enforcer = Arc::new(SafetyEnforcer::new());
        let req = SafetyRequest::new();
        let claim = ResourceClaim::new()
            .with_concurrent(5)
            .with_tokens(1000)
            .with_memory_gb(8);

        let _guard = enforcer.acquire(&req, claim).unwrap();

        let stats = enforcer.usage();
        assert_eq!(stats.concurrent, 5);
        assert_eq!(stats.tokens, 1000);
        assert_eq!(stats.memory_gb, 8);
    }

    #[test]
    fn test_audit_entries() {
        let envelope = SafetyEnvelope {
            audit: AuditConfig {
                enabled: true,
                log_success: true,
                log_blocked: true,
                log_warnings: true,
                max_entries: 100,
                flush_interval_ms: 5000,
            },
            ..Default::default()
        };
        let enforcer = Arc::new(SafetyEnforcer::with_envelope(envelope));
        let req = SafetyRequest::new();
        let claim = ResourceClaim::new().with_concurrent(1);

        // Acquire and release
        let _guard = enforcer.acquire(&req, claim).unwrap();
        drop(_guard);

        // Check audit log
        let entries = enforcer.audit_entries(10);
        assert!(!entries.is_empty());
    }

    #[test]
    fn test_rate_limiting() {
        let envelope = SafetyEnvelope {
            rate_limits: RateEnvelope {
                global_rpm: 2,
                per_source_rpm: 1,
                tokens_per_minute: 1000,
                burst_multiplier: 1.0,
            },
            ..Default::default()
        };
        let enforcer = SafetyEnforcer::with_envelope(envelope);
        let source = make_node_id(1);

        // First request should pass
        let req = SafetyRequest::new().with_source(source).with_tokens(100);
        assert!(enforcer.check(&req).is_ok());
        enforcer.rate_limiter.record_request(Some(&source), 100);

        // Second request from same source should hit per-source limit
        let result = enforcer.check(&req);
        assert!(matches!(
            result,
            Err(SafetyViolation::RateLimitExceeded {
                limit_type: RateLimitType::PerSourceRpm,
                ..
            })
        ));
    }

    /// Pin: in `AuditOnly` mode every rate-limit violation must
    /// still produce a `RateLimitHit` audit entry (with
    /// `Warning` outcome) — pre-fix the violation was silently
    /// dropped because the `log_event` call was nested inside
    /// the `if mode == Enforce` branch, contradicting the
    /// envelope's documented "log violations but don't block"
    /// semantics.
    #[test]
    fn audit_only_mode_logs_rate_limit_violations_as_warnings() {
        let envelope = SafetyEnvelope {
            mode: EnforcementMode::AuditOnly,
            rate_limits: RateEnvelope {
                global_rpm: 1,
                per_source_rpm: 1,
                tokens_per_minute: 1000,
                burst_multiplier: 1.0,
            },
            audit: AuditConfig {
                enabled: true,
                log_success: false,
                log_blocked: true,
                log_warnings: true,
                max_entries: 100,
                flush_interval_ms: 5000,
            },
            ..Default::default()
        };
        let enforcer = SafetyEnforcer::with_envelope(envelope);
        let source = make_node_id(7);
        let req = SafetyRequest::new().with_source(source).with_tokens(100);

        // Burn the first request through the limiter so the
        // second exceeds it.
        assert!(enforcer.check(&req).is_ok());
        enforcer.rate_limiter.record_request(Some(&source), 100);

        // Second request: violation under AuditOnly. Must NOT
        // return Err (audit-only doesn't block) AND must log a
        // Warning-outcome RateLimitHit.
        assert!(
            enforcer.check(&req).is_ok(),
            "AuditOnly must not block the request"
        );

        let entries = enforcer.audit_entries(100);
        let hits: Vec<_> = entries
            .iter()
            .filter(|e| e.event_type == AuditEventType::RateLimitHit)
            .collect();
        assert!(
            !hits.is_empty(),
            "AuditOnly mode must emit a RateLimitHit audit entry on violation; \
             pre-fix the entry was suppressed because logging was gated on \
             Enforce mode. Entries: {:?}",
            entries,
        );
        assert!(
            hits.iter().all(|e| e.outcome == AuditOutcome::Warning),
            "AuditOnly violations must be logged with Warning outcome \
             (Blocked is reserved for the Enforce path that actually \
             returns Err). Outcomes: {:?}",
            hits.iter().map(|e| e.outcome).collect::<Vec<_>>(),
        );
    }

    /// Regression for BUG_AUDIT_2026_04_30_CORE.md #102: pre-fix
    /// `release()` used raw `fetch_sub` on `concurrent` and
    /// `memory_gb`. `acquire()` short-circuits in `Disabled`
    /// mode WITHOUT incrementing those counters; the matching
    /// release would then `fetch_sub` from 0, wrapping `u32` to
    /// ~4 billion. The next `Enforce`-mode `acquire` would see
    /// the wrapped value, decide the cap was exceeded, and reject
    /// every request forever (envelope is hot-swappable at
    /// runtime — operators warm-up in `Disabled` then flip).
    ///
    /// We pin the fix by:
    ///   1. Building an enforcer in `Disabled` mode.
    ///   2. Acquiring + dropping a guard with non-zero claim.
    ///   3. Asserting `concurrent` and `memory_gb` are still 0
    ///      (saturating_sub kept them clamped).
    ///   4. Switching to `Enforce` mode and acquiring again to
    ///      confirm the next acquire path doesn't see a wrapped
    ///      counter.
    #[test]
    fn release_does_not_underflow_concurrent_or_memory_in_disabled_mode() {
        let enforcer = Arc::new(SafetyEnforcer::with_envelope(SafetyEnvelope {
            mode: EnforcementMode::Disabled,
            ..Default::default()
        }));
        let req = SafetyRequest::new();
        let claim = ResourceClaim::new().with_concurrent(5).with_memory_gb(100);

        // Acquire (no-op in Disabled — counters stay at 0) +
        // drop (release runs, would have wrapped u32 to ~4B
        // pre-fix).
        let guard = enforcer.acquire(&req, claim).unwrap();
        drop(guard);

        let stats = enforcer.usage();
        assert_eq!(
            stats.concurrent, 0,
            "concurrent must stay clamped at 0 when releasing in \
             Disabled mode (pre-fix this wrapped to u32::MAX-4)"
        );
        assert_eq!(
            stats.memory_gb, 0,
            "memory_gb must stay clamped at 0 when releasing in \
             Disabled mode (pre-fix this wrapped to u32::MAX-99)"
        );

        // Hot-swap to Enforce. The next acquire must NOT see a
        // wrapped counter — it must see 0 and admit the request.
        let new_envelope = SafetyEnvelope {
            mode: EnforcementMode::Enforce,
            ..Default::default()
        };
        enforcer.update_envelope(new_envelope);

        let req2 = SafetyRequest::new();
        let claim2 = ResourceClaim::new().with_concurrent(1);
        // Pre-fix: this would error with `ResourceLimitExceeded`
        // because the wrapped counter exceeded `max_concurrent`.
        let guard2 = enforcer
            .acquire(&req2, claim2)
            .expect("Enforce-mode acquire after a Disabled-mode release must succeed");
        drop(guard2);
    }

    #[test]
    fn test_regression_release_decrements_tokens_and_cost() {
        // Regression: release() only decremented concurrent slots and
        // memory, but not tokens or cost_cents_per_hour. Both counters
        // grew monotonically, hitting limits prematurely.
        let enforcer = Arc::new(SafetyEnforcer::new());
        let source = make_node_id(1);
        let req = SafetyRequest::new().with_source(source).with_tokens(500);
        let claim = ResourceClaim {
            tokens: 500,
            concurrent_slots: 1,
            memory_gb: 8,
            time_ms: 0,
            cost_cents: 50,
        };

        let guard = enforcer.acquire(&req, claim).unwrap();

        // Tokens and cost should be nonzero after acquire
        assert!(enforcer.usage.tokens.load(Ordering::Relaxed) >= 500);
        assert!(enforcer.usage.cost_cents_per_hour.load(Ordering::Relaxed) >= 50);

        // Drop the guard (triggers release)
        drop(guard);

        // Tokens and cost should be decremented back
        assert_eq!(
            enforcer.usage.tokens.load(Ordering::Relaxed),
            0,
            "tokens should be released on drop"
        );
        assert_eq!(
            enforcer.usage.cost_cents_per_hour.load(Ordering::Relaxed),
            0,
            "cost should be released on drop"
        );
    }

    #[test]
    fn test_regression_update_tokens_no_underflow() {
        // Regression: update_tokens with a lower actual count used
        // fetch_sub on the global AtomicU64 counter, which wraps to
        // u64::MAX on underflow — permanently locking out all requests.
        let enforcer = Arc::new(SafetyEnforcer::new());
        let source = make_node_id(1);
        let req = SafetyRequest::new().with_source(source).with_tokens(100);
        let claim = ResourceClaim {
            tokens: 100,
            concurrent_slots: 1,
            memory_gb: 10,
            time_ms: 0,
            cost_cents: 0,
        };

        let mut guard = enforcer.acquire(&req, claim).unwrap();

        // Simulate actual usage being lower than estimated
        guard.update_tokens(30);

        // Counter should reflect the difference (subtracted 70)
        let tokens = enforcer.usage.tokens.load(Ordering::Relaxed);
        assert!(
            tokens < u64::MAX / 2,
            "token counter should not have underflowed (got {})",
            tokens
        );

        drop(guard);

        // After release, tokens should be 0 (saturating)
        let final_tokens = enforcer.usage.tokens.load(Ordering::Relaxed);
        assert_eq!(
            final_tokens, 0,
            "tokens should be 0 after release, not underflowed"
        );
    }

    #[test]
    fn test_regression_check_tokens_overflow_is_rejected() {
        // Regression (MEDIUM, BUGS.md): `check_tokens` computed
        // `current + tokens` on two u64 values without an overflow
        // guard. Under high accumulated `current` the addition
        // panicked in debug (DoS) or wrapped in release (bypass).
        //
        // Fix: use `checked_add` and treat overflow as "over limit".
        let limiter = RateLimiter::new();
        // Seed the counter to near-saturation so the next `tokens`
        // value would wrap.
        limiter
            .global_tokens
            .store(u64::MAX - 10, Ordering::Relaxed);

        // Asking to add 100 more tokens would overflow u64.
        let result = limiter.check_tokens(100, 1_000_000, 1.0);
        assert!(
            matches!(
                result,
                Err(SafetyViolation::RateLimitExceeded {
                    limit_type: RateLimitType::TokensPerMinute,
                    ..
                })
            ),
            "overflow must be rejected, got {:?}",
            result
        );
    }

    /// Regression: BUG_REPORT.md #8 — `acquire` previously did
    /// `load + compare` (`check_resource_limits`) then
    /// `fetch_add`. N concurrent acquirers all observed the same
    /// pre-add value and all proceeded past the cap. The fix
    /// uses `fetch_update` per cumulative resource so the check +
    /// add is atomic per counter.
    ///
    /// We pin this by spawning many threads that each try to
    /// acquire 1 concurrent slot against a cap of K. Pre-fix,
    /// the final `concurrent` counter could exceed K. Post-fix,
    /// the cap is honored exactly: we see at most K successful
    /// `acquire`s and the rest fail with `ResourceLimitExceeded`.
    #[test]
    fn acquire_concurrent_cap_is_atomic_under_contention() {
        use std::sync::Arc;
        use std::sync::Barrier;
        use std::thread;

        const CAP: u32 = 5;
        const ATTEMPTS: usize = 100;

        // Build an enforcer with `max_concurrent = CAP` and very
        // permissive other limits, so the race surfaces only on
        // `concurrent`.
        let limits = ResourceEnvelope {
            max_concurrent: CAP,
            max_tokens_per_request: 1_000_000,
            max_memory_gb: 1_000_000,
            max_time_ms: 1_000_000,
            max_cost_per_hour_cents: u32::MAX,
        };
        let envelope = SafetyEnvelope {
            mode: EnforcementMode::Enforce,
            resource_limits: limits,
            ..Default::default()
        };
        let enforcer = Arc::new(SafetyEnforcer::with_envelope(envelope));

        let barrier = Arc::new(Barrier::new(ATTEMPTS));
        let handles: Vec<_> = (0..ATTEMPTS)
            .map(|_| {
                let enf = Arc::clone(&enforcer);
                let b = Arc::clone(&barrier);
                thread::spawn(move || {
                    b.wait();
                    let req = SafetyRequest::new();
                    let claim = ResourceClaim {
                        concurrent_slots: 1,
                        tokens: 1,
                        memory_gb: 0,
                        time_ms: 0,
                        cost_cents: 0,
                    };
                    enf.acquire(&req, claim)
                })
            })
            .collect();

        let results: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
        let successes: Vec<_> = results.into_iter().filter_map(|r| r.ok()).collect();

        // The crucial invariant: no more than CAP concurrent
        // claims actually committed. Pre-fix this would routinely
        // exceed CAP under high contention.
        assert!(
            successes.len() as u32 <= CAP,
            "TOCTOU regression (#8): {} concurrent acquires committed against \
             cap of {}",
            successes.len(),
            CAP
        );

        // And the counter itself reflects exactly that — never
        // higher than CAP.
        assert!(
            enforcer.usage.concurrent.load(Ordering::Relaxed) <= CAP,
            "concurrent counter exceeds cap"
        );
    }

    /// Regression: the rate-limit half of #8. Previously the
    /// global / per-source RPM and tokens-per-minute checks were
    /// load+compare in `check()` while the increment was a
    /// separate `record_request` in `acquire()`. Multiple
    /// concurrent acquirers could all pass the load+compare,
    /// then all increment past the cap. The fix CAS-ifies the
    /// check + add inside `acquire()` so the rate-limit cap is
    /// honored exactly under contention.
    #[test]
    fn acquire_global_rpm_cap_is_atomic_under_contention() {
        use std::sync::Arc;
        use std::sync::Barrier;
        use std::thread;

        const RPM_CAP: u32 = 5;
        const ATTEMPTS: usize = 100;

        let envelope = SafetyEnvelope {
            mode: EnforcementMode::Enforce,
            // Loose resource limits so concurrent / memory / cost
            // never trip; we want only the RPM cap to be the
            // contended counter.
            resource_limits: ResourceEnvelope {
                max_concurrent: u32::MAX,
                max_tokens_per_request: 1_000_000,
                max_memory_gb: u32::MAX,
                max_time_ms: u32::MAX,
                max_cost_per_hour_cents: u32::MAX,
            },
            rate_limits: RateEnvelope {
                global_rpm: RPM_CAP,
                per_source_rpm: u32::MAX,
                tokens_per_minute: u64::MAX,
                burst_multiplier: 1.0,
            },
            ..Default::default()
        };
        let enforcer = Arc::new(SafetyEnforcer::with_envelope(envelope));

        let barrier = Arc::new(Barrier::new(ATTEMPTS));
        let handles: Vec<_> = (0..ATTEMPTS)
            .map(|_| {
                let enf = Arc::clone(&enforcer);
                let b = Arc::clone(&barrier);
                thread::spawn(move || {
                    b.wait();
                    let req = SafetyRequest::new();
                    let claim = ResourceClaim {
                        concurrent_slots: 1,
                        tokens: 1,
                        memory_gb: 0,
                        time_ms: 0,
                        cost_cents: 0,
                    };
                    enf.acquire(&req, claim)
                })
            })
            .collect();

        let results: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
        let successes: Vec<_> = results.into_iter().filter_map(|r| r.ok()).collect();

        assert!(
            successes.len() as u32 <= RPM_CAP,
            "RPM TOCTOU regression (#8): {} acquires committed against cap {}",
            successes.len(),
            RPM_CAP,
        );
        assert!(
            enforcer
                .rate_limiter
                .global_requests
                .load(Ordering::Relaxed)
                <= RPM_CAP as u64,
            "global_requests counter exceeds RPM cap",
        );
    }

    /// Per-source RPM bucket self-resets on window rollover via
    /// packed `(window_floor, count)` atomic — no global
    /// `clear()` race window where a concurrent
    /// `record_request`'s `fetch_add` lands in an AtomicU64
    /// that's about to be removed from the map.
    ///
    /// This test pins the bucket-level invariant directly: an
    /// initial floor stamps count to 1; a stale-floor read sees
    /// 0; a stale-floor try_acquire resets to 1 atomically.
    #[test]
    fn rate_bucket_self_resets_on_window_rollover() {
        let bucket = RateBucket::new(0);
        // Initial: floor=0, count=0; current_count(0) reads 0.
        assert_eq!(bucket.current_count(0), 0);

        // Acquire under floor=0 with cap 5 — succeeds; count=1.
        assert!(matches!(bucket.try_acquire(0, 5), Ok(1)));
        assert_eq!(bucket.current_count(0), 1);

        // Acquire 4 more — at cap; the 5th fails.
        for _ in 0..4 {
            assert!(bucket.try_acquire(0, 5).is_ok());
        }
        assert_eq!(bucket.current_count(0), 5);
        assert!(matches!(bucket.try_acquire(0, 5), Err(5)));

        // Window rolls over to floor=1: read sees 0 (bucket
        // stored floor=0 != 1).
        assert_eq!(bucket.current_count(1), 0);

        // try_acquire under new floor resets atomically: count=1
        // and stored floor advances to 1.
        assert!(matches!(bucket.try_acquire(1, 5), Ok(1)));
        assert_eq!(bucket.current_count(1), 1);
        // Old floor's view: stale, reads 0.
        assert_eq!(bucket.current_count(0), 0);
    }

    /// End-to-end: under concurrent rate-limit pressure across
    /// multiple sources, no source ever exceeds its per-source
    /// cap by more than 1 (the single race window is between
    /// fetch_update's load and CAS, NOT the previously-unbounded
    /// clear-and-reinsert race).
    ///
    /// Pre-fix this test would intermittently fail because the
    /// minute-boundary `per_source.clear()` could land between
    /// a concurrent acquirer's check and its fetch_add — the
    /// new bucket's count would start from 0 and admit cap+N
    /// firings, where N is the racing-acquirer count.
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn per_source_cap_respected_under_contention_no_clear_race() {
        const PER_SOURCE_CAP: u32 = 50;
        const N_THREADS: u32 = 16;
        const N_PER_THREAD: u32 = 100;

        let mut envelope = SafetyEnvelope::default();
        envelope.rate_limits.per_source_rpm = PER_SOURCE_CAP;
        envelope.rate_limits.global_rpm = u32::MAX;
        envelope.rate_limits.tokens_per_minute = u64::MAX;
        let enforcer = Arc::new(SafetyEnforcer::with_envelope(envelope));
        let source: NodeId = [0xAA; 32];

        let mut handles = Vec::new();
        let success_count = Arc::new(AtomicU64::new(0));
        for _ in 0..N_THREADS {
            let e = enforcer.clone();
            let sc = success_count.clone();
            handles.push(tokio::task::spawn_blocking(move || {
                for _ in 0..N_PER_THREAD {
                    if e.rate_limiter
                        .try_acquire_source_rpm(&source, PER_SOURCE_CAP, 1.0)
                        .is_ok()
                    {
                        sc.fetch_add(1, Ordering::Relaxed);
                    }
                }
            }));
        }
        for h in handles {
            h.await.unwrap();
        }

        let total = success_count.load(Ordering::Relaxed);
        assert!(
            total <= PER_SOURCE_CAP as u64,
            "per-source cap regression (#125): {} acquires committed against cap {} \
             (no global clear race should let any over-commit happen — bucket \
             self-resets via packed atomic CAS)",
            total,
            PER_SOURCE_CAP,
        );
    }

    // ---------- Acquire rollback paths ----------
    //
    // `acquire()` is a chain of independent CAS counters: concurrent
    // → memory → cost → global RPM → per-source RPM → tokens. If
    // step N+1 fails the implementation must roll back every counter
    // it already committed in steps 1..N. A regression here leaks
    // resources permanently (counters never released) and silently
    // throttles the enforcer to zero capacity. These tests pin each
    // rollback branch.

    #[test]
    fn memory_limit_failure_rolls_back_concurrent() {
        let envelope = SafetyEnvelope {
            resource_limits: ResourceEnvelope {
                max_concurrent: 100,
                max_memory_gb: 1,
                ..Default::default()
            },
            ..Default::default()
        };
        let enforcer = Arc::new(SafetyEnforcer::with_envelope(envelope));
        let req = SafetyRequest::new();
        let claim = ResourceClaim::new().with_concurrent(1).with_memory_gb(2);

        // ResourceGuard isn't Debug, so we can't use unwrap_err here.
        let err = match enforcer.acquire(&req, claim) {
            Err(e) => e,
            Ok(_) => panic!("expected memory limit failure"),
        };
        assert!(matches!(
            err,
            SafetyViolation::ResourceLimitExceeded {
                resource: ResourceType::Memory,
                ..
            }
        ));
        // Concurrent must have been rolled back — leaking it would
        // permanently consume a slot.
        assert_eq!(enforcer.usage().concurrent, 0);
        assert_eq!(enforcer.usage().memory_gb, 0);
    }

    #[test]
    fn cost_limit_failure_rolls_back_concurrent_and_memory() {
        let envelope = SafetyEnvelope {
            resource_limits: ResourceEnvelope {
                max_concurrent: 100,
                max_memory_gb: 100,
                max_cost_per_hour_cents: 10,
                ..Default::default()
            },
            ..Default::default()
        };
        let enforcer = Arc::new(SafetyEnforcer::with_envelope(envelope));
        let req = SafetyRequest::new();
        let claim = ResourceClaim::new()
            .with_concurrent(1)
            .with_memory_gb(1)
            .with_cost_cents(100);

        let err = match enforcer.acquire(&req, claim) {
            Err(e) => e,
            Ok(_) => panic!("expected cost limit failure"),
        };
        assert!(matches!(
            err,
            SafetyViolation::ResourceLimitExceeded {
                resource: ResourceType::Cost,
                ..
            }
        ));
        assert_eq!(enforcer.usage().concurrent, 0);
        assert_eq!(enforcer.usage().memory_gb, 0);
        assert_eq!(enforcer.usage().cost_cents_per_hour, 0);
    }

    #[test]
    fn per_source_rpm_failure_rolls_back_global_and_resources() {
        let envelope = SafetyEnvelope {
            rate_limits: RateEnvelope {
                global_rpm: 100,
                per_source_rpm: 1,
                tokens_per_minute: 1_000_000,
                burst_multiplier: 1.0,
            },
            ..Default::default()
        };
        let enforcer = Arc::new(SafetyEnforcer::with_envelope(envelope));
        let source = make_node_id(11);
        let req = SafetyRequest::new().with_source(source).with_tokens(10);
        let claim = ResourceClaim::new().with_concurrent(1).with_memory_gb(1);

        // First acquire from this source succeeds; second trips
        // per-source RPM after global RPM has been committed.
        let _guard = enforcer.acquire(&req, claim.clone()).unwrap();
        let err = match enforcer.acquire(&req, claim) {
            Err(e) => e,
            Ok(_) => panic!("expected per-source RPM failure"),
        };
        assert!(matches!(
            err,
            SafetyViolation::RateLimitExceeded {
                limit_type: RateLimitType::PerSourceRpm,
                ..
            }
        ));

        // The failed acquire must not leave resource counters
        // bumped — only the guard from the FIRST acquire holds 1.
        let usage = enforcer.usage();
        assert_eq!(usage.concurrent, 1);
        assert_eq!(usage.memory_gb, 1);
        // Global RPM counter must also be 1 (only the first
        // request committed), not 2 — proves global rollback fired.
        // `requests_per_minute` is the public surface for the
        // internal `rate_limiter.global_requests` counter.
        assert_eq!(usage.requests_per_minute, 1);
    }

    #[test]
    fn tokens_failure_rolls_back_source_and_global_and_resources() {
        let envelope = SafetyEnvelope {
            rate_limits: RateEnvelope {
                global_rpm: 100,
                per_source_rpm: 100,
                tokens_per_minute: 10,
                burst_multiplier: 1.0,
            },
            ..Default::default()
        };
        let enforcer = Arc::new(SafetyEnforcer::with_envelope(envelope));
        let source = make_node_id(22);
        let req = SafetyRequest::new().with_source(source);
        // acquire() rate-checks `claim.tokens`, not `req.tokens`.
        let claim = ResourceClaim::new()
            .with_concurrent(1)
            .with_memory_gb(1)
            .with_tokens(100);

        let err = match enforcer.acquire(&req, claim) {
            Err(e) => e,
            Ok(_) => panic!("expected tokens-per-minute failure"),
        };
        assert!(matches!(
            err,
            SafetyViolation::RateLimitExceeded {
                limit_type: RateLimitType::TokensPerMinute,
                ..
            }
        ));

        // All five rollbacks must have fired: source RPM, global
        // RPM, concurrent, memory, cost. We observe global +
        // resource counters; the source bucket is internal but
        // gets the same treatment.
        let usage = enforcer.usage();
        assert_eq!(usage.concurrent, 0);
        assert_eq!(usage.memory_gb, 0);
        assert_eq!(usage.cost_cents_per_hour, 0);
        assert_eq!(usage.requests_per_minute, 0);
    }

    // ---------- Content-policy non-block actions ----------
    //
    // Block is well-covered. Warn / Log / Redact share a single
    // outcome (log a Warning audit entry, don't reject) but each
    // takes a distinct match arm in `check_content_policies` — a
    // future refactor could collapse them, but until then we want
    // the per-arm behavior pinned.

    fn content_policy_envelope(action: PolicyAction) -> SafetyEnvelope {
        SafetyEnvelope {
            content_policies: vec![ContentPolicy {
                id: "warn-on-bad".into(),
                check: ContentCheck::BlockPatterns(vec!["bad".into()]),
                action,
                enabled: true,
            }],
            audit: AuditConfig {
                enabled: true,
                log_success: false,
                log_blocked: true,
                log_warnings: true,
                max_entries: 100,
                flush_interval_ms: 5000,
            },
            ..Default::default()
        }
    }

    #[test]
    fn content_policy_warn_logs_warning_without_blocking() {
        let enforcer = SafetyEnforcer::with_envelope(content_policy_envelope(PolicyAction::Warn));
        let req = SafetyRequest::new().with_content("this is bad");

        assert!(enforcer.check(&req).is_ok(), "Warn must not block");
        let warnings: Vec<_> = enforcer
            .audit_entries(100)
            .into_iter()
            .filter(|e| {
                e.event_type == AuditEventType::ContentPolicyViolation
                    && e.outcome == AuditOutcome::Warning
            })
            .collect();
        assert!(
            !warnings.is_empty(),
            "Warn action must log a Warning-outcome audit entry",
        );
    }

    #[test]
    fn content_policy_log_logs_warning_without_blocking() {
        let enforcer = SafetyEnforcer::with_envelope(content_policy_envelope(PolicyAction::Log));
        let req = SafetyRequest::new().with_content("this is bad");

        assert!(enforcer.check(&req).is_ok(), "Log must not block");
        let warnings: Vec<_> = enforcer
            .audit_entries(100)
            .into_iter()
            .filter(|e| {
                e.event_type == AuditEventType::ContentPolicyViolation
                    && e.outcome == AuditOutcome::Warning
            })
            .collect();
        assert!(!warnings.is_empty());
    }

    #[test]
    fn content_policy_redact_logs_warning_without_blocking() {
        let enforcer = SafetyEnforcer::with_envelope(content_policy_envelope(PolicyAction::Redact));
        let req = SafetyRequest::new().with_content("this is bad");

        assert!(enforcer.check(&req).is_ok(), "Redact must not block");
        let warnings: Vec<_> = enforcer
            .audit_entries(100)
            .into_iter()
            .filter(|e| {
                e.event_type == AuditEventType::ContentPolicyViolation
                    && e.outcome == AuditOutcome::Warning
            })
            .collect();
        assert!(!warnings.is_empty());
    }
}