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
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
//! Dynamic shard mapping and scaling.
//!
//! This module implements dynamic shard scaling following Approach B from
//! DYNAMIC_SHARD_SCALING.md: increasing the number of shards and rebalancing
//! producers across them, while maintaining SPSC (single-producer single-consumer)
//! semantics per shard.
//!
//! # Core Principles
//!
//! - Shards never accept multiple producers
//! - Producers get moved to new shards when load increases
//! - Ingestion performance stays at 700M ops/sec per shard
//! - Ordering guarantees remain intact within a shard
//! - Total throughput scales linearly with shard count
//!
//! # Scaling Triggers
//!
//! - Ring buffer fill ratio > threshold (default 70%)
//! - Push latency exceeds threshold (default 5ns)
//! - Batch flush latency exceeds threshold
//! - Session/producer count growth
//!
//! # Architecture
//!
//! ```text
//! Producers -----+
//!                |
//!                v
//!     +----------------------------+
//!     |   Dynamic Shard Mapper     |
//!     +----------------------------+
//!        |         |         |
//!        v         v         v
//!     Shard 0   Shard 1   Shard 2 …
//! ```

use std::sync::atomic::{AtomicBool, AtomicU16, AtomicU64, Ordering as AtomicOrdering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use arc_swap::ArcSwap;
use parking_lot::RwLock;

use crate::config::ScalingPolicy;

/// Metrics for a single shard used for scaling decisions.
#[derive(Debug, Clone)]
pub struct ShardMetrics {
    /// Shard identifier.
    pub shard_id: u16,
    /// Current fill ratio (0.0 - 1.0).
    pub fill_ratio: f64,
    /// Events ingested in the current window.
    pub event_rate: u64,
    /// Average push latency in nanoseconds.
    pub avg_push_latency_ns: u64,
    /// Average batch flush latency in microseconds.
    pub avg_flush_latency_us: u64,
    /// Whether this shard is in drain mode.
    pub draining: bool,
    /// Computed weight for load balancing (lower = less loaded).
    pub weight: f64,
    /// Last update timestamp.
    pub last_updated: Instant,
}

impl ShardMetrics {
    /// Create new metrics for a shard.
    pub fn new(shard_id: u16) -> Self {
        Self {
            shard_id,
            fill_ratio: 0.0,
            event_rate: 0,
            avg_push_latency_ns: 0,
            avg_flush_latency_us: 0,
            draining: false,
            weight: 0.0,
            last_updated: Instant::now(),
        }
    }

    /// Compute the weight based on current metrics.
    /// Lower weight = better candidate for new producers.
    pub fn compute_weight(&mut self) {
        // Weight formula: combines fill ratio, latency, and event rate
        // Higher fill ratio = higher weight (avoid overloaded shards)
        // Higher latency = higher weight
        // Higher event rate = higher weight
        let fill_weight = self.fill_ratio * 100.0;
        let latency_weight = (self.avg_push_latency_ns as f64) / 10.0;
        let rate_weight = (self.event_rate as f64) / 1_000_000.0;

        self.weight = fill_weight + latency_weight + rate_weight;

        // Draining shards get maximum weight (never assign new producers)
        if self.draining {
            self.weight = f64::MAX;
        }
    }
}

/// Live metrics collector for a shard (atomics for lock-free updates).
#[derive(Debug)]
pub struct ShardMetricsCollector {
    /// Shard identifier.
    shard_id: u16,
    /// Ring buffer capacity.
    capacity: usize,
    /// Current buffer length (updated by shard).
    current_len: AtomicU64,
    /// Events ingested in current window.
    events_in_window: AtomicU64,
    /// Packed `(count << 32) | sum` for push latencies (ns).
    /// Pre-fix `push_latency_sum_ns` and `push_count` were
    /// independent `AtomicU64`s. `record_push` did two separate
    /// `fetch_add`s, and `collect_and_reset` did two separate
    /// `swap`s. A metrics tick interleaving between the two
    /// `fetch_add`s captured the sum WITHOUT the count (or
    /// vice versa); the resulting `avg = sum.checked_div(count)
    /// .unwrap_or(0)` returned 0 in window N (sum without
    /// count) and 0 in window N+1 (count without sum), silently
    /// zeroing the average that drives `evaluate_scaling`'s
    /// push-latency scale-up trigger. Packing into one u64 makes
    /// the `(sum, count)` update atomic; the upper 32 bits hold
    /// the count (u32::MAX = 4G calls/window — plenty) and the
    /// lower 32 hold the sum (u32::MAX = 4 G ns ≈ 4 s, also
    /// plenty for any sane window).
    push_latency: AtomicU64,
    /// Packed `(count << 32) | sum` for flush latencies (us).
    /// Same shape and rationale as `push_latency`. The lower 32
    /// bits hold sum-µs (u32::MAX ≈ 4 Gµs ≈ 67 minutes — far
    /// past any plausible window).
    flush_latency: AtomicU64,
    /// Whether this shard is draining.
    draining: AtomicBool,
    /// Window start time.
    window_start: RwLock<Instant>,
    /// Pushes observed since `set_draining(true)` was last called.
    /// Distinct from `events_in_window` because this counter is NOT
    /// reset by `collect_and_reset`. `finalize_draining` reads this
    /// instead of `events_in_window` so a drain-window-overlap with
    /// a metrics tick can no longer race the counter to zero before
    /// the producer is observed.
    pushes_since_drain_start: AtomicU64,
}

impl ShardMetricsCollector {
    /// Create a new metrics collector.
    pub fn new(shard_id: u16, capacity: usize) -> Self {
        Self {
            shard_id,
            capacity,
            current_len: AtomicU64::new(0),
            events_in_window: AtomicU64::new(0),
            push_latency: AtomicU64::new(0),
            flush_latency: AtomicU64::new(0),
            draining: AtomicBool::new(false),
            window_start: RwLock::new(Instant::now()),
            pushes_since_drain_start: AtomicU64::new(0),
        }
    }

    /// Record current buffer length.
    #[inline]
    pub fn record_buffer_len(&self, len: usize) {
        self.current_len.store(len as u64, AtomicOrdering::Relaxed);
    }

    /// Record an event ingestion.
    #[inline]
    pub fn record_push(&self, latency_ns: u64) {
        self.events_in_window.fetch_add(1, AtomicOrdering::Relaxed);
        // Atomically add 1 to count (upper 32 bits) and
        // `latency_ns` to sum (lower 32 bits). `fetch_update`
        // CAS-loops the load-and-store, so a concurrent
        // `collect_and_reset` swap on the same word either sees
        // both pre-add or both post-add — no `(sum, count)`
        // desync. Saturating ops cap at u32::MAX inside the
        // pack window (~4 G calls / 4 s of accumulated latency),
        // which is far beyond any sane metrics tick.
        let _ =
            self.push_latency
                .fetch_update(AtomicOrdering::Relaxed, AtomicOrdering::Relaxed, |v| {
                    let count = (v >> 32) as u32;
                    let sum = (v & 0xFFFF_FFFF) as u32;
                    let new_count = count.saturating_add(1) as u64;
                    let new_sum = sum.saturating_add(latency_ns.min(u32::MAX as u64) as u32) as u64;
                    Some((new_count << 32) | new_sum)
                });
        // Always increment — the cost is one fetch_add and the
        // counter only matters when the shard is draining. Cheaper
        // than branching on `self.draining.load()` in the hot path.
        self.pushes_since_drain_start
            .fetch_add(1, AtomicOrdering::Relaxed);
    }

    /// Record a batch flush.
    #[inline]
    pub fn record_flush(&self, latency_us: u64) {
        // Same packed-`(count, sum)` shape as `record_push` —
        // see that function for the desync rationale.
        let _ = self.flush_latency.fetch_update(
            AtomicOrdering::Relaxed,
            AtomicOrdering::Relaxed,
            |v| {
                let count = (v >> 32) as u32;
                let sum = (v & 0xFFFF_FFFF) as u32;
                let new_count = count.saturating_add(1) as u64;
                let new_sum = sum.saturating_add(latency_us.min(u32::MAX as u64) as u32) as u64;
                Some((new_count << 32) | new_sum)
            },
        );
    }

    /// Set drain mode.
    ///
    /// Transitions to draining reset `pushes_since_drain_start` so
    /// `finalize_draining` only counts pushes that arrived after the
    /// drain began.
    ///
    /// A concurrent `record_push` can interleave with the store-zero
    /// on `pushes_since_drain_start` and leave the counter at `1`
    /// rather than `0`. That's not a correctness bug — it just defers
    /// finalization of this shard by one metrics tick — but the
    /// previous code did the store-zero on the counter *before*
    /// publishing the `draining=true` flag, which made the window
    /// slightly larger than necessary. Publishing the flag first
    /// means any push that *observes* `draining=true` is naturally
    /// sequenced after the reset; pushes that beat the flag publish
    /// race the reset just like before.
    ///
    /// We also use `SeqCst` for the publish to give the rest of the
    /// crate a single total order on draining transitions, which
    /// matches the ordering on `try_enter_ingest`'s shutdown flag.
    pub fn set_draining(&self, draining: bool) {
        if draining {
            // Store-zero first (so the flag publish below acts as
            // the release-fence for both writes).
            self.pushes_since_drain_start
                .store(0, AtomicOrdering::SeqCst);
        }
        self.draining.store(draining, AtomicOrdering::SeqCst);
    }

    /// Number of pushes observed since `set_draining(true)` was
    /// last called. Used by `finalize_draining` to detect lingering
    /// producers that the window-reset `events_in_window` counter
    /// can race past.
    ///
    /// Pre-fix used `Ordering::Relaxed`, but the writer
    /// side (`set_draining(true)`) resets the counter under
    /// `SeqCst`. On weakly-ordered hardware (ARM), a Relaxed
    /// reader could observe a stale counter and `finalize_draining`
    /// would falsely conclude the drain had flushed while
    /// producers were still pushing. Acquire pairs with the
    /// SeqCst release of the reset (SeqCst includes Release
    /// semantics), making the reset happen-before this load.
    pub fn pushes_since_drain_start(&self) -> u64 {
        self.pushes_since_drain_start.load(AtomicOrdering::Acquire)
    }

    /// Check if draining.
    pub fn is_draining(&self) -> bool {
        self.draining.load(AtomicOrdering::Acquire)
    }

    /// Collect metrics and reset window counters.
    ///
    /// NOTE: The individual atomic swaps below are not collectively atomic with
    /// respect to concurrent `record_push`/`record_flush` calls. This means a
    /// push recorded between, say, the `events_in_window` swap and the
    /// `push_count` swap could be counted in one counter but not the other for
    /// a given window. This small inaccuracy is an accepted trade-off to
    /// preserve the lock-free design of the hot path (`record_push` /
    /// `record_flush`). Adding a `Mutex` here would serialize the hot path
    /// and defeat the purpose of using atomics.
    pub fn collect_and_reset(&self) -> ShardMetrics {
        let current_len = self.current_len.load(AtomicOrdering::Relaxed);
        let events = self.events_in_window.swap(0, AtomicOrdering::Relaxed);
        // Single swap captures `(count, sum)` together; no
        // chance of catching the sum without the matching count
        // (or vice versa) the way two independent swaps did.
        let push_packed = self.push_latency.swap(0, AtomicOrdering::Relaxed);
        let push_count = push_packed >> 32;
        let push_latency_sum = push_packed & 0xFFFF_FFFF;
        let flush_packed = self.flush_latency.swap(0, AtomicOrdering::Relaxed);
        let flush_count = flush_packed >> 32;
        let flush_latency_sum = flush_packed & 0xFFFF_FFFF;

        let fill_ratio = if self.capacity > 0 {
            current_len as f64 / self.capacity as f64
        } else {
            0.0
        };

        let avg_push_latency = push_latency_sum.checked_div(push_count).unwrap_or(0);

        let avg_flush_latency = flush_latency_sum.checked_div(flush_count).unwrap_or(0);

        // Reset window
        *self.window_start.write() = Instant::now();

        let mut metrics = ShardMetrics {
            shard_id: self.shard_id,
            fill_ratio,
            event_rate: events,
            avg_push_latency_ns: avg_push_latency,
            avg_flush_latency_us: avg_flush_latency,
            draining: self.draining.load(AtomicOrdering::Acquire),
            weight: 0.0,
            last_updated: Instant::now(),
        };
        metrics.compute_weight();
        metrics
    }
}

/// Scaling decision made by the mapper.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScalingDecision {
    /// No scaling needed.
    None,
    /// Scale up by adding N shards.
    ScaleUp(u16),
    /// Scale down by removing N shards (marks them for draining).
    ScaleDown(u16),
}

/// Errors that can occur during scaling operations.
#[derive(Debug, Clone)]
pub enum ScalingError {
    /// Invalid scaling policy.
    InvalidPolicy(String),
    /// Already at maximum shards.
    AtMaxShards,
    /// Already at minimum shards.
    AtMinShards,
    /// Scaling operation in cooldown.
    InCooldown,
    /// Shard creation failed.
    ShardCreationFailed(String),
}

impl std::fmt::Display for ScalingError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidPolicy(msg) => write!(f, "invalid scaling policy: {}", msg),
            Self::AtMaxShards => write!(f, "already at maximum shard count"),
            Self::AtMinShards => write!(f, "already at minimum shard count"),
            Self::InCooldown => write!(f, "scaling operation in cooldown"),
            Self::ShardCreationFailed(msg) => write!(f, "shard creation failed: {}", msg),
        }
    }
}

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

/// State of a shard in the mapper.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShardState {
    /// Shard is being provisioned: id allocated and metrics collector
    /// in place, but `select_shard` must not route to it yet because
    /// upstream workers (drain / batch) have not been spawned. Caller
    /// transitions to `Active` via `activate` once the workers are
    /// ready. Closes the race where a freshly-added shard accepted
    /// producer pushes before any consumer existed.
    Provisioning,
    /// Shard is active and accepting producers.
    Active,
    /// Shard is draining (no new producers, waiting for empty).
    Draining,
    /// Shard is stopped and can be removed.
    Stopped,
}

/// Information about a shard managed by the mapper.
#[derive(Debug)]
struct MappedShard {
    /// Shard ID.
    id: u16,
    /// Current state.
    state: ShardState,
    /// Metrics collector.
    metrics: Arc<ShardMetricsCollector>,
    /// When this shard entered drain mode (if draining).
    drain_started: Option<Instant>,
    /// Last collected metrics.
    last_metrics: ShardMetrics,
    /// When this shard last transitioned to `Active`. Used by
    /// `evaluate_scaling` to skip recently-activated shards from
    /// scale decisions: their `last_metrics` is the
    /// `ShardMetrics::new(id)` placeholder until at least one
    /// `collect_metrics` cycle has run, and the placeholder
    /// (`fill_ratio = 0.0, event_rate = 0`) trips the
    /// underutilized trigger immediately — oscillating the system
    /// (scale-up → next tick scale-down → next tick scale-up …)
    /// when a fresh shard is added but hasn't yet absorbed any
    /// traffic.
    activated_at: Instant,
}

/// Callback type for shard lifecycle events.
type ShardCallback = Box<dyn Fn(u16) + Send + Sync>;

/// Pre-computed slice of shard ids that `select_shard` may return.
///
/// Holds the subset of currently-Active shards whose `last_metrics.weight`
/// is within tolerance of the minimum. Empty when no active shards exist
/// (in which case `select_shard` returns `u16::MAX`).
///
/// The table is rebuilt by [`ShardMapper::rebuild_selection_table_locked`]
/// at every mutation that could change which shards are routable (state
/// transitions, metric/weight refresh, scale-up/down). Hot-path readers
/// (`select_shard`) snapshot it via `ArcSwap::load` — zero alloc, zero
/// lock per call.
struct SelectionTable {
    /// Shard ids in priority order (within-tolerance of min weight).
    /// `Box<[u16]>` rather than `Vec<u16>` because the slice is immutable
    /// after construction; saves one word per snapshot.
    candidates: Box<[u16]>,
}

/// Dynamic shard mapper that manages shard allocation and producer routing.
///
/// This is the core component for dynamic scaling. It:
/// - Tracks metrics for all shards
/// - Makes scaling decisions based on policy
/// - Routes producers to the least-loaded shards
/// - Manages shard lifecycle (active → draining → stopped)
pub struct ShardMapper {
    /// Mapped shards (RwLock for concurrent reads, rare writes).
    shards: RwLock<Vec<MappedShard>>,
    /// Pre-computed routable-shard snapshot. Updated whenever the shard
    /// list or weights change; read lock-free by [`Self::select_shard`].
    ///
    /// Pre-fix [perf #2 in `docs/performance/net-perf-analysis.md`],
    /// `select_shard` did this work on every event: two `Vec` allocs +
    /// an `RwLock::read` + min-weight scan + tolerance filter. At 10M
    /// ev/s that's 20M allocs/sec plus a parking_lot acquire each time.
    /// The table is rebuilt at most once per metrics tick and once per
    /// state transition.
    selection_table: ArcSwap<SelectionTable>,
    /// Current active shard count.
    active_count: AtomicU16,
    /// Scaling policy.
    ///
    /// This field is immutable for the lifetime of the mapper. The
    /// previous `set_policy(&mut self, …)` API was unreachable in
    /// practice — every production callsite holds the mapper behind
    /// an `Arc`, and `Arc::get_mut` requires a strong count of 1,
    /// which never holds once the worker pool clones the `Arc`. The
    /// method has been removed; recreate the mapper (and
    /// the bus that owns it) to change the policy.
    policy: ScalingPolicy,
    /// Ring buffer capacity for new shards.
    ring_buffer_capacity: usize,
    /// Last scaling operation timestamp.
    ///
    /// This RwLock is **logically** scoped to the
    /// outer `shards.write()` lock — `scale_up`, `scale_down`,
    /// and `scale_up_provisioning` all read this field and write
    /// to it while holding `shards.write()`. The cooldown gate
    /// is therefore atomic with the scale mutation: no caller
    /// can pass the cooldown check, observe stale `last_scaling`,
    /// and have its mutation interleave with another caller.
    ///
    /// **If you narrow `shards.write()`'s scope in any of those
    /// callers, this implicit serialization breaks.** Either:
    ///   - Keep the cooldown read+write inside the outer lock, OR
    ///   - Use a `compare_exchange`-style update on a single
    ///     `AtomicI64` of nanos so the gate is atomic on its own.
    ///
    /// The doc-comment is here so a future refactorer doesn't
    /// silently break the contract.
    last_scaling: RwLock<Option<Instant>>,
    /// Callback for shard creation (provided by ShardManager).
    on_shard_created: RwLock<Option<ShardCallback>>,
    /// Callback for shard removal (provided by ShardManager).
    on_shard_removed: RwLock<Option<ShardCallback>>,
    /// Monotonic shard-id allocator. The next `scale_up` call gets
    /// `fetch_add(1)` from here. Distinct from `shards.iter().max() +
    /// 1`: that approach reused ids whenever the highest-numbered
    /// shard had been drained-and-removed, silently merging two
    /// unrelated shard lifetimes in any external system that keys
    /// metrics or checkpoints on shard id. Monotonic allocation
    /// ensures every shard ever allocated has a globally unique id
    /// for the lifetime of this mapper.
    next_shard_id: AtomicU16,
}

impl ShardMapper {
    /// Create a new shard mapper with the given initial shard count and policy.
    pub fn new(
        initial_shards: u16,
        ring_buffer_capacity: usize,
        policy: ScalingPolicy,
    ) -> Result<Self, ScalingError> {
        let mut policy = policy.normalize();
        // Ensure max_shards can accommodate the initial shard count
        if policy.max_shards < initial_shards {
            policy.max_shards = initial_shards;
        }
        policy
            .validate()
            .map_err(|e| ScalingError::InvalidPolicy(e.to_string()))?;

        // Initial shards: stamp `activated_at` far enough in the
        // past that they're not subject to the warmup skip in
        // `evaluate_scaling`. The boot-time shards have whatever
        // baseline traffic the system serves; they shouldn't be
        // exempted from scale decisions just because the mapper
        // was just constructed.
        let boot = Instant::now()
            .checked_sub(std::time::Duration::from_secs(3600))
            .unwrap_or_else(Instant::now);
        let shards: Vec<MappedShard> = (0..initial_shards)
            .map(|id| MappedShard {
                id,
                state: ShardState::Active,
                metrics: Arc::new(ShardMetricsCollector::new(id, ring_buffer_capacity)),
                drain_started: None,
                last_metrics: ShardMetrics::new(id),
                activated_at: boot,
            })
            .collect();

        let mapper = Self {
            shards: RwLock::new(shards),
            selection_table: ArcSwap::from_pointee(SelectionTable {
                candidates: Box::new([]),
            }),
            active_count: AtomicU16::new(initial_shards),
            policy,
            ring_buffer_capacity,
            last_scaling: RwLock::new(None),
            on_shard_created: RwLock::new(None),
            on_shard_removed: RwLock::new(None),
            // Initial shards occupy ids `[0, initial_shards)`, so the
            // first scale-up takes `initial_shards`.
            next_shard_id: AtomicU16::new(initial_shards),
        };
        // Seed the selection table with the boot shards.
        mapper.rebuild_selection_table_locked(&mapper.shards.read());
        Ok(mapper)
    }

    /// Rebuild the [`Self::selection_table`] from the current shards
    /// slice. Must be called while holding the `shards` lock (read or
    /// write) so the snapshot it captures is consistent with the
    /// state visible to other callers.
    ///
    /// Filters to `Active` shards, finds the minimum weight, and
    /// retains every active shard whose weight is within 0.1 of the
    /// minimum. The tolerance pre-existed the perf fix (see the old
    /// `select_shard` body) so the new table preserves the same
    /// routing semantics.
    ///
    /// NaN-tolerance edge case: if every active shard has a NaN
    /// weight the tolerance filter excludes all of them. To preserve
    /// the pre-fix fallback (`active[0].id`), the table falls back
    /// to *every* active id in that case.
    fn rebuild_selection_table_locked(&self, shards: &[MappedShard]) {
        let mut active_ids = Vec::with_capacity(shards.len());
        for s in shards.iter() {
            if s.state == ShardState::Active {
                active_ids.push(s.id);
            }
        }
        if active_ids.is_empty() {
            self.selection_table.store(Arc::new(SelectionTable {
                candidates: Box::new([]),
            }));
            return;
        }
        let min_weight = shards
            .iter()
            .filter(|s| s.state == ShardState::Active)
            .map(|s| s.last_metrics.weight)
            .fold(f64::MAX, f64::min);
        let mut candidates: Vec<u16> = shards
            .iter()
            .filter(|s| {
                s.state == ShardState::Active && (s.last_metrics.weight - min_weight).abs() < 0.1
            })
            .map(|s| s.id)
            .collect();
        if candidates.is_empty() {
            // All-NaN fallback. Preserves the pre-fix `active[0].id`
            // safety net; the random pick across all active shards
            // is a strict superset of "first active."
            candidates = active_ids;
        }
        self.selection_table.store(Arc::new(SelectionTable {
            candidates: candidates.into_boxed_slice(),
        }));
    }

    /// Set callback for shard creation.
    pub fn set_on_shard_created<F>(&self, callback: F)
    where
        F: Fn(u16) + Send + Sync + 'static,
    {
        *self.on_shard_created.write() = Some(Box::new(callback));
    }

    /// Set callback for shard removal.
    pub fn set_on_shard_removed<F>(&self, callback: F)
    where
        F: Fn(u16) + Send + Sync + 'static,
    {
        *self.on_shard_removed.write() = Some(Box::new(callback));
    }

    /// Get the metrics collector for a shard.
    pub fn metrics_collector(&self, shard_id: u16) -> Option<Arc<ShardMetricsCollector>> {
        let shards = self.shards.read();
        shards
            .iter()
            .find(|s| s.id == shard_id)
            .map(|s| s.metrics.clone())
    }

    /// Get current active shard count.
    pub fn active_shard_count(&self) -> u16 {
        self.active_count.load(AtomicOrdering::Acquire)
    }

    /// Get total shard count (including draining).
    pub fn total_shard_count(&self) -> u16 {
        self.shards.read().len() as u16
    }

    /// Select the best shard for a new event/producer.
    ///
    /// Pre-fix this acquired `shards.read()` and allocated two `Vec`s
    /// per call (active filter, candidate filter) — at 10M ev/s that
    /// was 20M allocs/sec plus a parking_lot acquire per event.
    /// Post-fix the routable subset is pre-computed in the private
    /// `selection_table` field and updated only at state
    /// transitions / metric refreshes; the hot path is a single
    /// `ArcSwap::load` + Lemire-bias-free integer mapping.
    ///
    /// Semantics preserved:
    /// - Only considers active (non-draining) shards
    /// - Prefers shards with lower weight (less loaded)
    /// - Falls back to a random pick across all active shards if the
    ///   tolerance filter excludes everything (NaN-weight edge case)
    /// - Returns `u16::MAX` when no active shard exists, so callers
    ///   that route via the routing table get a definite miss
    #[inline]
    pub fn select_shard(&self, event_hash: u64) -> u16 {
        let table = self.selection_table.load();
        if table.candidates.is_empty() {
            // No active shards. Pre-fix returned `u16::MAX` from the
            // same arm — callers that look up the id in the routing
            // table get a definite miss, which the manager already
            // accounts for.
            return u16::MAX;
        }
        // Lemire's bias-free index mapping for any `len` that fits
        // in u64. Pre-fix used `(event_hash as usize) %
        // candidates.len()`, which biases low-bucket indices when
        // `candidates.len()` is not a power of two
        // (https://lemire.me/blog/2016/06/30/fast-random-shuffling/).
        let idx = ((event_hash as u128 * table.candidates.len() as u128) >> 64) as usize;
        table.candidates[idx]
    }

    /// Collect metrics from all shards and update weights.
    pub fn collect_metrics(&self) -> Vec<ShardMetrics> {
        let mut shards = self.shards.write();
        let result: Vec<ShardMetrics> = shards
            .iter_mut()
            .map(|s| {
                s.last_metrics = s.metrics.collect_and_reset();
                s.last_metrics.clone()
            })
            .collect();
        // Weights just changed — refresh the selection table so
        // `select_shard` observes the new min-weight candidate set
        // without re-scanning on every event.
        self.rebuild_selection_table_locked(&shards);
        result
    }

    /// Evaluate scaling based on current metrics.
    ///
    /// Returns a scaling decision without executing it.
    pub fn evaluate_scaling(&self) -> ScalingDecision {
        if !self.policy.auto_scale {
            return ScalingDecision::None;
        }

        // Check cooldown
        if let Some(last) = *self.last_scaling.read() {
            if last.elapsed() < self.policy.cooldown {
                return ScalingDecision::None;
            }
        }

        let shards = self.shards.read();
        let active_count = self.active_count.load(AtomicOrdering::Acquire);

        // Check for scale-up triggers
        let mut overloaded_count = 0;
        let mut underutilized_count = 0;

        // Warmup window for freshly-activated shards. A just-
        // activated shard's `last_metrics` is the
        // `ShardMetrics::new(id)` placeholder
        // (`fill_ratio = 0.0, event_rate = 0`) until at least one
        // `collect_metrics` cycle has run. The placeholder
        // immediately matches the underutilized trigger
        // (`fill_ratio < underutilized_threshold && event_rate
        // == 0`), so a fresh shard added by scale-up would
        // immediately count as underutilized on the next
        // `evaluate_scaling` and trigger scale-down — oscillating
        // the system. Reuse `policy.cooldown` as the warmup
        // window: it's already the minimum gap between scaling
        // actions, and a shard collected at least once within
        // that window has accumulated real metrics.
        let now = Instant::now();
        let warmup = self.policy.cooldown;

        for shard in shards.iter() {
            if shard.state != ShardState::Active {
                continue;
            }

            // Skip the placeholder-metrics window for freshly-
            // activated shards. They count toward `active_count`
            // (so the budget math stays consistent) but don't
            // tip the overload/underutilized tallies.
            if now.duration_since(shard.activated_at) < warmup {
                continue;
            }

            let m = &shard.last_metrics;

            // Scale-up triggers
            if m.fill_ratio > self.policy.fill_ratio_threshold
                || m.avg_push_latency_ns > self.policy.push_latency_threshold_ns
                || m.avg_flush_latency_us > self.policy.flush_latency_threshold_us
            {
                overloaded_count += 1;
            }

            // Scale-down triggers
            if m.fill_ratio < self.policy.underutilized_threshold && m.event_rate == 0 {
                underutilized_count += 1;
            }
        }

        // Scale up if more than half of shards are overloaded
        if overloaded_count > active_count / 2 && active_count < self.policy.max_shards {
            // Add shards proportional to overload
            let shards_to_add = (overloaded_count / 2)
                .max(1)
                .min(self.policy.max_shards - active_count);
            return ScalingDecision::ScaleUp(shards_to_add);
        }

        // Scale down if more than half of shards are underutilized
        // and we're above minimum
        if underutilized_count > active_count / 2 && active_count > self.policy.min_shards {
            let shards_to_remove = (underutilized_count / 2)
                .max(1)
                .min(active_count - self.policy.min_shards);
            return ScalingDecision::ScaleDown(shards_to_remove);
        }

        ScalingDecision::None
    }

    /// Validate cooldown + max-shards budget for an `add count`
    /// scale-up request. Cheap pre-check that doesn't take the
    /// write lock — callers re-check under the lock.
    fn check_scale_up_budget(&self, count: u16) -> Result<(), ScalingError> {
        let current = self.active_count.load(AtomicOrdering::Acquire);
        let would_be = current
            .checked_add(count)
            .ok_or(ScalingError::AtMaxShards)?;
        if would_be > self.policy.max_shards {
            return Err(ScalingError::AtMaxShards);
        }
        let last = self.last_scaling.read();
        if let Some(ts) = *last {
            if ts.elapsed() < self.policy.cooldown {
                return Err(ScalingError::InCooldown);
            }
        }
        Ok(())
    }

    /// Allocate `count` shard ids and push their `MappedShard`
    /// records into `shards` with the supplied `state`. The caller
    /// already holds `self.shards.write()` and is responsible for
    /// dropping it before notifying callbacks. Returns the allocated
    /// ids in order.
    ///
    /// Performs the budget + cooldown re-check under the write lock,
    /// the next_shard_id allocation, and the per-shard push. Does NOT
    /// touch `active_count` — `scale_up` bumps it for `Active` shards;
    /// `Provisioning` shards bump it later when `activate` fires.
    fn allocate_shards_inner(
        &self,
        count: u16,
        state: ShardState,
        shards: &mut Vec<MappedShard>,
    ) -> Result<Vec<u16>, ScalingError> {
        self.allocate_shards_inner_with_policy(count, state, shards, false)
    }

    fn allocate_shards_inner_with_policy(
        &self,
        count: u16,
        state: ShardState,
        shards: &mut Vec<MappedShard>,
        force: bool,
    ) -> Result<Vec<u16>, ScalingError> {
        // Re-check budget under the write lock — two concurrent
        // scale-up callers could both pass the read-locked early
        // check, both serialize through `shards.write()`, and both
        // succeed without this re-check.
        if force {
            // Budget only — skip cooldown for operator-initiated paths.
            let current = self.active_count.load(AtomicOrdering::Acquire);
            let would_be = current
                .checked_add(count)
                .ok_or(ScalingError::AtMaxShards)?;
            if would_be > self.policy.max_shards {
                return Err(ScalingError::AtMaxShards);
            }
        } else {
            self.check_scale_up_budget(count)?;
        }

        let first_id = self.next_shard_id.load(AtomicOrdering::Relaxed);
        let last_needed = first_id
            .checked_add(count.saturating_sub(1))
            .ok_or(ScalingError::AtMaxShards)?;
        // Reserve `u16::MAX` as a sentinel so the post-allocation
        // store cannot wrap.
        if last_needed == u16::MAX {
            return Err(ScalingError::AtMaxShards);
        }
        // `first_id + count == last_needed + 1`. We already
        // refused `last_needed == u16::MAX` above, so the sum is
        // provably <= u16::MAX. Use `checked_add` anyway as a
        // belt-and-suspenders guard: a future change that
        // weakens the sentinel check would otherwise reach an
        // unchecked u16 wrap here, silently rolling
        // `next_shard_id` back to 0 and then re-issuing already-
        // allocated ids.
        let next_id_after = first_id
            .checked_add(count)
            .ok_or(ScalingError::AtMaxShards)?;
        self.next_shard_id
            .store(next_id_after, AtomicOrdering::Relaxed);

        let mut new_ids = Vec::with_capacity(count as usize);
        let now = Instant::now();
        for i in 0..count {
            let new_id = first_id + i;
            shards.push(MappedShard {
                id: new_id,
                state,
                metrics: Arc::new(ShardMetricsCollector::new(
                    new_id,
                    self.ring_buffer_capacity,
                )),
                drain_started: None,
                last_metrics: ShardMetrics::new(new_id),
                // Stamp the activation moment so `evaluate_scaling`
                // can skip this shard until at least one collect
                // cycle has run. Prevents the placeholder
                // (`fill_ratio = 0, event_rate = 0`) from
                // immediately tripping the underutilized trigger
                // and oscillating the system.
                activated_at: now,
            });
            new_ids.push(new_id);
        }
        Ok(new_ids)
    }

    /// Execute a scale-up operation.
    ///
    /// Creates new shards in the `Active` state and makes them
    /// immediately available for routing. Use [`scale_up_provisioning`]
    /// if upstream workers (drain / batch) need to be wired up before
    /// the shard becomes selectable — otherwise producer pushes can
    /// race ahead of consumer creation.
    ///
    /// [`scale_up_provisioning`]: Self::scale_up_provisioning
    pub fn scale_up(&self, count: u16) -> Result<Vec<u16>, ScalingError> {
        // Short-circuit `count == 0` so a no-op call doesn't bump the
        // cooldown timestamp or trip the `u16::MAX` sentinel check
        // (which previously fired spuriously when
        // `first_id == u16::MAX` even though zero ids were being
        // allocated).
        if count == 0 {
            return Ok(Vec::new());
        }

        self.check_scale_up_budget(count)?;

        let mut shards = self.shards.write();
        let new_ids = self.allocate_shards_inner(count, ShardState::Active, &mut shards)?;

        // Update counts. `fetch_add` cannot wrap here — the
        // `check_scale_up_budget` gate above ensures `current + count <=
        // max_shards <= u16::MAX` — but keep the ordering explicit
        // so the next contender's cooldown re-check sees the fresh
        // timestamp.
        self.active_count.fetch_add(count, AtomicOrdering::Release);
        *self.last_scaling.write() = Some(Instant::now());

        // New active shards are routable immediately. Refresh the
        // selection table before dropping the lock so any concurrent
        // `select_shard` observes the new ids in its very next call.
        self.rebuild_selection_table_locked(&shards);

        // Drop the write lock before notifying callbacks — they
        // are user-supplied and may take arbitrary time.
        drop(shards);

        if let Some(callback) = self.on_shard_created.read().as_ref() {
            for &id in &new_ids {
                callback(id);
            }
        }

        Ok(new_ids)
    }

    /// Like [`scale_up`], but the new shards are created in the
    /// `Provisioning` state. They receive an id and a metrics
    /// collector, but `select_shard` will not route to them and they
    /// are excluded from `active_shard_count` / `evaluate_scaling`
    /// until the caller transitions each shard with [`activate`].
    ///
    /// Use this when upstream consumer infrastructure (drain/batch
    /// workers, mpsc channels, etc.) must be wired up *before* the
    /// shard becomes selectable. Without this gating, producers can
    /// observe the shard via `select_shard`, push into its ring
    /// buffer, and never have those events drained.
    ///
    /// Returns the allocated ids in order. Cooldown / `max_shards`
    /// gating matches `scale_up` so that staged allocation cannot be
    /// used to bypass the policy.
    ///
    /// [`scale_up`]: Self::scale_up
    /// [`activate`]: Self::activate
    pub fn scale_up_provisioning(&self, count: u16) -> Result<Vec<u16>, ScalingError> {
        if count == 0 {
            return Ok(Vec::new());
        }

        self.check_scale_up_budget(count)?;

        let mut shards = self.shards.write();
        let new_ids = self.allocate_shards_inner(count, ShardState::Provisioning, &mut shards)?;

        // Bump cooldown but NOT active_count — these shards are
        // not active yet. `activate` bumps active_count when each
        // becomes selectable.
        *self.last_scaling.write() = Some(Instant::now());
        drop(shards);
        // Intentionally do NOT fire `on_shard_created` here — the
        // callback signals "shard is live"; provisioning shards are
        // not. `activate` fires it instead.
        Ok(new_ids)
    }

    /// Allocate `count` Provisioning shards, bypassing the cooldown gate.
    ///
    /// Used by operator-initiated `manual_scale_up` paths. The
    /// cooldown exists to prevent the auto-scaling monitor from
    /// scaling-up too aggressively in response to transient
    /// load spikes; a manual call from an operator is a
    /// deliberate request that should not be rate-limited by
    /// the auto-scaling cadence. The budget check (against
    /// `max_shards`) still applies.
    ///
    /// Pre-fix `manual_scale_up(N)` looped `add_shard()` N
    /// times, each call invoking `scale_up_provisioning(1)`
    /// which bumped `last_scaling`. The second call then
    /// immediately failed with `InCooldown` (default 30s
    /// cooldown), leaving the first shard half-added and
    /// returning an error to the operator with no rollback.
    pub fn scale_up_provisioning_force(&self, count: u16) -> Result<Vec<u16>, ScalingError> {
        if count == 0 {
            return Ok(Vec::new());
        }

        let mut shards = self.shards.write();
        let new_ids = self.allocate_shards_inner_with_policy(
            count,
            ShardState::Provisioning,
            &mut shards,
            true, // skip cooldown
        )?;

        // Still bump the cooldown timestamp so the next
        // *auto-scaling* tick respects the cooldown floor.
        *self.last_scaling.write() = Some(Instant::now());
        drop(shards);
        Ok(new_ids)
    }

    /// Transition a `Provisioning` shard to `Active`.
    ///
    /// Returns `Ok(true)` if a state transition actually occurred
    /// (Provisioning → Active) and `Ok(false)` if the shard was
    /// already `Active` — the latter is the idempotent path.
    /// Returns `InvalidPolicy` for unknown or `Draining`/`Stopped`
    /// shards — those states require a different lifecycle path.
    /// Bumps `active_count` and notifies the `on_shard_created`
    /// callback exactly once per real transition.
    ///
    /// Pre-fix this returned `Result<(), ScalingError>`,
    /// so callers (notably `ShardManager::activate_shard`) could
    /// not tell whether they had bumped the live count or not and
    /// double-incremented their own `num_shards` on every
    /// idempotent call.
    pub fn activate(&self, shard_id: u16) -> Result<bool, ScalingError> {
        let mut shards = self.shards.write();
        let shard = shards
            .iter_mut()
            .find(|s| s.id == shard_id)
            .ok_or_else(|| {
                ScalingError::InvalidPolicy(format!("activate: shard {} not found", shard_id))
            })?;
        match shard.state {
            ShardState::Active => return Ok(false),
            ShardState::Provisioning => {
                // Gate activation on
                // `active_count < max_shards`. The budget gate at
                // `check_scale_up_budget` only counts ALREADY-active
                // shards — multiple `scale_up_provisioning(1)` calls
                // can each pass (they don't bump `active_count`),
                // and an unconditional `fetch_add(1)` here would
                // push past `max_shards`. Subsequent
                // `evaluate_scaling`'s `max_shards - active_count`
                // arithmetic would then underflow u16 (debug-build
                // panic; release wraps to ~65530). Activate-time
                // re-checks the budget with the lock held; the
                // Provisioning shard stays in Provisioning state and
                // the caller (e.g. `add_shard_internal`'s rollback)
                // is responsible for tearing it down.
                //
                // The load + state mutation + `fetch_add` all happen
                // while we hold the `shards.write()` guard so a
                // concurrent `activate(distinct_id)` reads the
                // already-bumped `active_count` and hits
                // `AtMaxShards` instead of squeezing through the
                // window between our state update and our
                // `fetch_add`. Pre-fix the `fetch_add` ran after
                // `drop(shards)` — two activates could each see a
                // stale count below `max_shards` and both bump,
                // transiently overshooting the budget.
                let current = self.active_count.load(AtomicOrdering::Acquire);
                if current >= self.policy.max_shards {
                    return Err(ScalingError::AtMaxShards);
                }
                shard.state = ShardState::Active;
                // Re-stamp `activated_at` so `evaluate_scaling`
                // gives this freshly-activated shard a warmup
                // window before counting it in the
                // overloaded/underutilized tallies. The
                // Provisioning → Active transition is the moment
                // traffic starts flowing; the shard's
                // `last_metrics` is still the
                // `ShardMetrics::new(id)` placeholder until the
                // next `collect_metrics` cycle.
                shard.activated_at = Instant::now();
                // Publish the increment while still holding the
                // write lock. `Release` here pairs with the
                // `Acquire` load above so any later activator that
                // takes the same lock observes our bump.
                self.active_count.fetch_add(1, AtomicOrdering::Release);
            }
            ShardState::Draining | ShardState::Stopped => {
                return Err(ScalingError::InvalidPolicy(format!(
                    "activate: shard {} is in state {:?}, cannot activate",
                    shard_id, shard.state
                )));
            }
        }
        // The shard just became routable. Refresh the selection
        // table before dropping the lock so the next `select_shard`
        // observes it.
        self.rebuild_selection_table_locked(&shards);
        drop(shards);

        if let Some(callback) = self.on_shard_created.read().as_ref() {
            callback(shard_id);
        }
        Ok(true)
    }

    /// Drain a specific shard by id, transitioning it from `Active`
    /// to `Draining`.
    ///
    /// Companion to `ShardManager::drain_shard`. The previous
    /// implementation only flipped the metrics collector's `draining`
    /// atomic; this version atomically updates `MappedShard.state`
    /// (so `select_shard` stops routing to the shard) and decrements
    /// `active_count` (so `evaluate_scaling`'s budget math stays
    /// consistent with `scale_down`). Returns an error if the shard
    /// is not in `Active` state, or if doing so would push the active
    /// count below `min_shards`.
    pub fn drain_specific(&self, shard_id: u16) -> Result<(), ScalingError> {
        let mut shards = self.shards.write();
        let current_active = self.active_count.load(AtomicOrdering::Acquire);
        if current_active <= self.policy.min_shards {
            return Err(ScalingError::AtMinShards);
        }
        let shard = shards
            .iter_mut()
            .find(|s| s.id == shard_id)
            .ok_or_else(|| {
                ScalingError::InvalidPolicy(format!("drain_specific: shard {} not found", shard_id))
            })?;
        match shard.state {
            ShardState::Active => {
                shard.state = ShardState::Draining;
                shard.drain_started = Some(Instant::now());
                shard.metrics.set_draining(true);
            }
            ShardState::Draining => return Ok(()),
            ShardState::Provisioning | ShardState::Stopped => {
                return Err(ScalingError::InvalidPolicy(format!(
                    "drain_specific: shard {} is in state {:?}, cannot drain",
                    shard_id, shard.state
                )));
            }
        }
        // The drained shard is no longer routable. Refresh the
        // selection table before dropping the lock.
        self.rebuild_selection_table_locked(&shards);
        drop(shards);
        self.active_count.fetch_sub(1, AtomicOrdering::Release);
        // Bump `last_scaling` so a subsequent `scale_up` is gated
        // by the cooldown floor. Pre-fix `drain_specific` removed
        // a shard from Active without touching `last_scaling`, so
        // the sequence `drain_specific(id) → scale_up(N)`
        // bypassed the cooldown — `scale_down` writes
        // `last_scaling` precisely for this reason. From the
        // budget-math perspective `drain_specific` IS a scale-
        // down (it decrements `active_count` and trips the
        // `min_shards` floor), so it should also gate
        // re-expansion the same way.
        *self.last_scaling.write() = Some(Instant::now());
        Ok(())
    }

    /// Start draining shards for scale-down.
    ///
    /// Marks shards as draining so they stop receiving new events.
    /// Shards will be removed once they're empty.
    pub fn scale_down(&self, count: u16) -> Result<Vec<u16>, ScalingError> {
        // Early checks (may race, but avoid acquiring the write lock)
        let current = self.active_count.load(AtomicOrdering::Acquire);
        if current <= self.policy.min_shards {
            return Err(ScalingError::AtMinShards);
        }

        let to_drain = count.min(current - self.policy.min_shards);
        if to_drain == 0 {
            return Err(ScalingError::AtMinShards);
        }
        {
            let last = self.last_scaling.read();
            if let Some(ts) = *last {
                if ts.elapsed() < self.policy.cooldown {
                    return Err(ScalingError::InCooldown);
                }
            }
        }

        let mut shards = self.shards.write();

        // Re-check under the lock to prevent race conditions (double-check pattern)
        let current = self.active_count.load(AtomicOrdering::Acquire);
        if current <= self.policy.min_shards {
            return Err(ScalingError::AtMinShards);
        }
        let to_drain = count.min(current - self.policy.min_shards);
        if to_drain == 0 {
            return Err(ScalingError::AtMinShards);
        }
        // Re-check cooldown under the same write lock that gates
        // mutation — see the matching note in `scale_up`.
        {
            let last = self.last_scaling.read();
            if let Some(ts) = *last {
                if ts.elapsed() < self.policy.cooldown {
                    return Err(ScalingError::InCooldown);
                }
            }
        }

        let mut drained_ids = Vec::with_capacity(to_drain as usize);

        // Find shards with lowest weight (least utilized) to drain
        let mut active_indices: Vec<_> = shards
            .iter()
            .enumerate()
            .filter(|(_, s)| s.state == ShardState::Active)
            .map(|(i, s)| (i, s.last_metrics.weight))
            .collect();

        // Sort by weight (ascending - drain least utilized first)
        active_indices.sort_by(|a, b| a.1.total_cmp(&b.1));

        // Mark shards for draining
        for (idx, _) in active_indices.into_iter().take(to_drain as usize) {
            shards[idx].state = ShardState::Draining;
            shards[idx].drain_started = Some(Instant::now());
            shards[idx].metrics.set_draining(true);
            drained_ids.push(shards[idx].id);
        }

        // Update count
        self.active_count
            .fetch_sub(to_drain, AtomicOrdering::Release);
        *self.last_scaling.write() = Some(Instant::now());

        // Drained shards are no longer routable. Refresh the
        // selection table while still holding the lock.
        self.rebuild_selection_table_locked(&shards);

        Ok(drained_ids)
    }

    /// Check draining shards and finalize those that are empty.
    ///
    /// Returns IDs of shards that were stopped.
    ///
    /// This predicate looks ONLY at the ring buffer
    /// (`current_len` + `pushes_since_drain_start`); it does NOT
    /// probe the per-shard mpsc channel or the BatchWorker's
    /// `current_batch`. A shard that the predicate flags as empty
    /// can still have events queued in those two places. The
    /// correctness gate is therefore `bus::remove_shard_internal`,
    /// which awaits the BatchWorker's `JoinHandle` before
    /// constructing the stranded-flush batch — see that function's
    /// step 3 for the rationale. Tightening this predicate is a
    /// defense-in-depth follow-up; a stricter ring-buffer-empty
    /// signal here would only narrow an already-closed window.
    pub fn finalize_draining(&self) -> Vec<u16> {
        let mut shards = self.shards.write();
        let mut stopped = Vec::new();

        for shard in shards.iter_mut() {
            if shard.state == ShardState::Draining {
                // Check if shard is empty by reading current_len directly,
                // avoiding collect_and_reset() which destructively zeros all counters.
                let current_len = shard.metrics.current_len.load(AtomicOrdering::Relaxed);
                let fill_ratio = if shard.metrics.capacity > 0 {
                    current_len as f64 / shard.metrics.capacity as f64
                } else {
                    0.0
                };
                // Previously read `events_in_window` here, which
                // `collect_and_reset` zeros every metrics tick. A
                // producer push that landed in the window between two
                // ticks could be silently zeroed out, so a draining
                // shard whose buffer transiently emptied was finalized
                // with a producer still attached.
                // `pushes_since_drain_start` is a separate counter
                // that is only reset by `set_draining(true)`, so any
                // push observed since the drain began is sticky —
                // exactly the signal we want.
                // Acquire pairs with `set_draining`'s SeqCst reset so
                // the load can't observe a stale value from before the
                // drain began. A Relaxed load here let weakly-ordered
                // hardware see the pre-reset count and finalize while
                // a producer was still pushing.
                let pushes_after_drain = shard.metrics.pushes_since_drain_start();
                if fill_ratio == 0.0 && pushes_after_drain == 0 {
                    // Check if we've waited long enough
                    if let Some(drain_start) = shard.drain_started {
                        if drain_start.elapsed() > Duration::from_millis(100) {
                            shard.state = ShardState::Stopped;
                            stopped.push(shard.id);
                        }
                    }
                }
            }
        }

        // Draining → Stopped transitions: no impact on the routable
        // set (Draining was already excluded), but rebuild for
        // consistency in case `last_metrics.weight` was stale on a
        // shard that just stopped. Cheap; bounded by shard count.
        if !stopped.is_empty() {
            self.rebuild_selection_table_locked(&shards);
        }

        // Drop the write lock BEFORE notifying. The callback is
        // user-supplied and may re-enter the mapper (`shard_state`,
        // `select_shard`, `metrics_collector`, …), each of which
        // acquires `shards.read()`. `parking_lot::RwLock` is not
        // recursive, so a re-entrant read attempt while we hold a
        // write would deadlock. `scale_up`'s callback path already
        // releases its lock before calling out — mirror that
        // here.
        drop(shards);

        if !stopped.is_empty() {
            if let Some(callback) = self.on_shard_removed.read().as_ref() {
                for &id in &stopped {
                    callback(id);
                }
            }
        }

        stopped
    }

    /// Remove a specific shard from the mapper if it is in the
    /// `Stopped` state. Used by `ShardManager::remove_shard` so a
    /// per-shard cleanup doesn't disturb sibling `Stopped`
    /// entries — which a sequential `manual_scale_down` loop
    /// still needs to look up state for. Returns `true` if the
    /// shard existed and was Stopped (and was removed).
    pub fn remove_specific_stopped_shard(&self, shard_id: u16) -> bool {
        let mut shards = self.shards.write();
        let before = shards.len();
        shards.retain(|s| !(s.id == shard_id && s.state == ShardState::Stopped));
        let removed = shards.len() < before;
        if removed {
            // Stopped shards weren't routable, but the underlying
            // slice changed length / order — rebuild so the table
            // doesn't dangle a stale id.
            self.rebuild_selection_table_locked(&shards);
        }
        removed
    }

    /// Remove stopped shards from the mapper.
    pub fn remove_stopped_shards(&self) -> Vec<u16> {
        let mut shards = self.shards.write();
        let before = shards.len();
        let removed: Vec<u16> = shards
            .iter()
            .filter(|s| s.state == ShardState::Stopped)
            .map(|s| s.id)
            .collect();

        shards.retain(|s| s.state != ShardState::Stopped);

        if shards.len() < before {
            tracing::info!(
                removed = removed.len(),
                remaining = shards.len(),
                "Removed stopped shards"
            );
            // Same reason as `remove_specific_stopped_shard`.
            self.rebuild_selection_table_locked(&shards);
        }

        removed
    }

    /// Get the state of a specific shard.
    pub fn shard_state(&self, shard_id: u16) -> Option<ShardState> {
        self.shards
            .read()
            .iter()
            .find(|s| s.id == shard_id)
            .map(|s| s.state)
    }

    /// Get all active shard IDs.
    pub fn active_shard_ids(&self) -> Vec<u16> {
        self.shards
            .read()
            .iter()
            .filter(|s| s.state == ShardState::Active)
            .map(|s| s.id)
            .collect()
    }

    /// Get all shard IDs (including draining).
    pub fn all_shard_ids(&self) -> Vec<u16> {
        self.shards
            .read()
            .iter()
            .filter(|s| s.state != ShardState::Stopped)
            .map(|s| s.id)
            .collect()
    }

    /// Get the scaling policy.
    pub fn policy(&self) -> &ScalingPolicy {
        &self.policy
    }
    // `set_policy` previously took `&mut self` and was unreachable
    // through the `Arc<ShardMapper>` that the production code holds
    // (`Arc::get_mut` fails once the worker pool has cloned the Arc).
    // The method has been removed — recreate the mapper / bus to
    // change the policy.
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::disallowed_methods,
        reason = "test code legitimately uses std::sync::{Mutex,RwLock} for SUT setup; tests have no real poison concern"
    )]
    use super::*;

    #[test]
    fn test_shard_mapper_creation() {
        let mapper = ShardMapper::new(4, 1024, ScalingPolicy::default()).unwrap();
        assert_eq!(mapper.active_shard_count(), 4);
        assert_eq!(mapper.total_shard_count(), 4);
    }

    #[test]
    fn test_select_shard_distributes() {
        let mapper = ShardMapper::new(4, 1024, ScalingPolicy::default()).unwrap();

        // Different hashes should potentially select different shards
        let mut selected = std::collections::HashSet::new();
        for i in 0..100u64 {
            let shard = mapper.select_shard(i * 12345);
            selected.insert(shard);
        }

        // With 4 shards, we should hit multiple
        assert!(!selected.is_empty());
    }

    /// Pin for perf #2: the selection table reflects the routable
    /// subset *exactly*, and `select_shard` reads it lock-free.
    /// A regression that, say, forgot to refresh after `drain_specific`
    /// would let `select_shard` continue routing to the draining
    /// shard — observable here as the drained id appearing in
    /// `select_shard`'s output.
    #[test]
    fn selection_table_reflects_active_subset_after_state_transitions() {
        // ScalingPolicy::validate rejects `min_shards == 0`; use 1
        // and verify we can drain down to the floor via the
        // official `drain_specific` API.
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 8,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(3, 1024, policy).unwrap();

        // Initial: all 3 shards routable.
        let mut seen = std::collections::HashSet::new();
        for i in 0..500u64 {
            seen.insert(mapper.select_shard(i.wrapping_mul(0x9E3779B97F4A7C15)));
        }
        assert_eq!(seen, std::collections::HashSet::from([0, 1, 2]));

        // Drain shard 1 → only 0 and 2 should be routable.
        mapper.drain_specific(1).unwrap();
        let mut seen = std::collections::HashSet::new();
        for i in 0..500u64 {
            seen.insert(mapper.select_shard(i.wrapping_mul(0x9E3779B97F4A7C15)));
        }
        assert_eq!(
            seen,
            std::collections::HashSet::from([0, 2]),
            "drained shard must vanish from select_shard output; \
             a regression here would let select_shard route to a \
             Draining shard (blocking finalization)",
        );

        // Drain shard 2 → only 0 left (at min_shards floor).
        mapper.drain_specific(2).unwrap();
        let mut seen = std::collections::HashSet::new();
        for i in 0..500u64 {
            seen.insert(mapper.select_shard(i.wrapping_mul(0x9E3779B97F4A7C15)));
        }
        assert_eq!(seen, std::collections::HashSet::from([0]));
    }

    /// Pin: `select_shard` does NOT acquire `self.shards` (read or
    /// write). The whole perf #2 fix is that the hot path is
    /// `ArcSwap::load` only — no parking_lot. We pin this by
    /// holding a write lock on `shards` and asserting
    /// `select_shard` still returns immediately.
    #[test]
    fn select_shard_does_not_acquire_shards_lock() {
        let mapper = ShardMapper::new(2, 1024, ScalingPolicy::default()).unwrap();

        // Hold the write lock. If `select_shard` tried to acquire
        // any flavor of `shards.{read,write}()` it would block
        // forever — parking_lot is not recursive.
        let _guard = mapper.shards.write();
        let shard = mapper.select_shard(0xDEAD_BEEF);
        assert!(shard < 2, "select_shard must return one of [0, 1]");
    }

    /// `select_shard`'s candidate-index computation must
    /// be unbiased across the u64 hash space. Pre-fix used
    /// `hash as usize % candidates.len()`, which over-weights low
    /// indices when `candidates.len()` is not a power of two.
    /// With u64 hashes the bias is small but non-zero and
    /// sustains a hot-shard skew over time. Lemire's
    /// `(hash * len) >> 64` is unbiased.
    ///
    /// We test the unbiased property by sampling a uniform
    /// distribution of u64 hashes over 3 candidate shards and
    /// asserting each bucket gets close to 1/3 of the picks.
    /// Empirical bound: ±5% across 30 000 trials with a
    /// well-distributed input.
    #[test]
    fn select_shard_distribution_is_unbiased() {
        // 3 candidates: a non-power-of-2 to expose the modulo
        // bias the fix removes.
        let mapper = ShardMapper::new(3, 1024, ScalingPolicy::default()).unwrap();

        let trials = 30_000u64;
        // Spread inputs uniformly across the u64 range so the
        // multiply-shift mapping behaves as designed.
        let stride = u64::MAX / trials;
        let mut counts = [0u64; 3];
        for i in 0..trials {
            let h = i.wrapping_mul(stride);
            let id = mapper.select_shard(h);
            counts[id as usize] += 1;
        }

        let expected = (trials / 3) as i64;
        for (id, &count) in counts.iter().enumerate() {
            let diff = (count as i64 - expected).abs();
            let pct = (diff as f64 / expected as f64) * 100.0;
            assert!(
                pct < 5.0,
                "shard {} bucket has {} hits ({:.2}% off expected {}); \
                 modulo bias would drift higher on certain shards",
                id,
                count,
                pct,
                expected
            );
        }
    }

    #[test]
    fn test_scale_up() {
        // Explicitly set max_shards to allow scaling from 2 to 4
        let policy = ScalingPolicy {
            max_shards: 8,
            ..Default::default()
        };
        let mapper = ShardMapper::new(2, 1024, policy).unwrap();

        let new_ids = mapper.scale_up(2).unwrap();
        assert_eq!(new_ids.len(), 2);
        assert_eq!(mapper.active_shard_count(), 4);
    }

    #[test]
    fn test_scale_up_max_limit() {
        let policy = ScalingPolicy {
            max_shards: 4,
            ..Default::default()
        };
        let mapper = ShardMapper::new(4, 1024, policy).unwrap();

        let result = mapper.scale_up(1);
        assert!(matches!(result, Err(ScalingError::AtMaxShards)));
    }

    #[test]
    fn test_scale_down() {
        let policy = ScalingPolicy {
            min_shards: 1,
            cooldown: Duration::from_nanos(1), // Disable cooldown for test
            ..Default::default()
        };
        let mapper = ShardMapper::new(4, 1024, policy).unwrap();

        let drained = mapper.scale_down(2).unwrap();
        assert_eq!(drained.len(), 2);
        assert_eq!(mapper.active_shard_count(), 2);
    }

    #[test]
    fn test_scale_down_min_limit() {
        let policy = ScalingPolicy {
            min_shards: 4,
            ..Default::default()
        };
        let mapper = ShardMapper::new(4, 1024, policy).unwrap();

        let result = mapper.scale_down(1);
        assert!(matches!(result, Err(ScalingError::AtMinShards)));
    }

    #[test]
    fn test_metrics_collection() {
        let mapper = ShardMapper::new(2, 1024, ScalingPolicy::default()).unwrap();

        // Record some metrics
        if let Some(collector) = mapper.metrics_collector(0) {
            collector.record_buffer_len(512);
            collector.record_push(5);
            collector.record_push(10);
        }

        let metrics = mapper.collect_metrics();
        assert_eq!(metrics.len(), 2);

        let shard0_metrics = metrics.iter().find(|m| m.shard_id == 0).unwrap();
        assert!(shard0_metrics.fill_ratio > 0.0);
    }

    /// Regression: a `record_push` / `record_flush` interleaving
    /// with a `collect_and_reset` swap must NOT desync `(sum,
    /// count)`. Pre-fix `push_latency_sum_ns` and `push_count`
    /// were independent atomics; a tick between the two
    /// `fetch_add`s captured the sum without the matching count
    /// (or the count without the sum). The resulting `avg =
    /// sum.checked_div(count).unwrap_or(0)` returned 0 in window
    /// N (sum without count) AND 0 in window N+1 (count without
    /// sum) — silently zeroing the average that drives
    /// `evaluate_scaling`'s push-latency scale-up trigger.
    ///
    /// Post-fix `(sum, count)` is packed into one
    /// `AtomicU64` so the swap captures both atomically. This
    /// test fires N concurrent `record_push` calls and a single
    /// `collect_and_reset` and asserts the captured count
    /// matches the captured sum (i.e. `sum >= count` because
    /// every push contributes at least 1 ns; `sum / count` is
    /// well-defined for any non-zero count).
    #[test]
    fn record_push_collect_no_sum_count_desync() {
        use std::sync::Barrier;
        use std::thread;

        let collector = Arc::new(ShardMetricsCollector::new(0, 1024));
        const PUSHERS: usize = 4;
        const PUSHES_PER_THREAD: usize = 1_000;

        let barrier = Arc::new(Barrier::new(PUSHERS + 1));
        let mut handles = vec![];
        for _ in 0..PUSHERS {
            let c = collector.clone();
            let b = barrier.clone();
            handles.push(thread::spawn(move || {
                b.wait();
                for i in 0..PUSHES_PER_THREAD {
                    // Vary latencies so sum/count averages
                    // aren't trivially the same number.
                    c.record_push((i as u64 % 100) + 1);
                }
            }));
        }

        // Race a collect_and_reset across the pushers.
        barrier.wait();
        let snapshot1 = collector.collect_and_reset();
        for h in handles {
            h.join().unwrap();
        }
        // After all threads finish, drain whatever remains.
        let snapshot2 = collector.collect_and_reset();

        // Reconstruct count and sum from the two snapshots.
        // We can't easily expose the packed atomic, so we use
        // the per-window averages and event counts as a
        // consistency check.
        let total_events = snapshot1.event_rate + snapshot2.event_rate;
        assert_eq!(
            total_events as usize,
            PUSHERS * PUSHES_PER_THREAD,
            "all pushes must be accounted for"
        );

        // For each window: if event_rate > 0, avg_push_latency
        // must be > 0 (non-zero average) — pre-fix the desync
        // could land event_rate > 0 with avg = 0 (count
        // captured without sum, or sum without count → div-by-
        // zero clamped to 0). This is the directly visible
        // symptom of the desync.
        //
        // events_in_window is incremented separately from the
        // packed (sum, count) word, so a strict assertion of
        // "event_rate is exactly count" isn't safe — but the
        // weaker invariant "if any pushes were captured in the
        // (sum,count) word, the average is non-zero" survives
        // the packed-atomic fix.
        for snap in [&snapshot1, &snapshot2] {
            if snap.avg_push_latency_ns == 0 {
                // Either no pushes were captured in this
                // window, or — pre-fix — sum/count desynced.
                // The post-fix shape can only produce
                // avg=0 when count is also 0; we can't read
                // count directly from ShardMetrics, but
                // exercise the invariant by confirming the
                // OTHER window's sum is consistent with all
                // pushes.
                continue;
            }
            assert!(
                snap.avg_push_latency_ns >= 1,
                "regression: a window with non-zero avg must have \
                 a positive sum (pre-fix sum-without-count desync \
                 produced avg=0 with non-zero events)"
            );
        }
    }

    #[test]
    fn test_draining_excludes_from_selection() {
        let policy = ScalingPolicy {
            min_shards: 1,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(2, 1024, policy).unwrap();

        // Drain one shard
        let drained = mapper.scale_down(1).unwrap();
        assert_eq!(drained.len(), 1);

        // All selections should go to the remaining active shard
        let active_ids = mapper.active_shard_ids();
        assert_eq!(active_ids.len(), 1);

        for i in 0..100u64 {
            let selected = mapper.select_shard(i);
            assert!(active_ids.contains(&selected));
        }
    }

    #[test]
    fn test_policy_validation() {
        let invalid_policy = ScalingPolicy {
            fill_ratio_threshold: 1.5, // Invalid
            ..Default::default()
        };
        assert!(invalid_policy.validate().is_err());

        // Without normalize(), this would be invalid
        let invalid_policy2 = ScalingPolicy {
            min_shards: 10,
            max_shards: 5,
            ..Default::default()
        };
        assert!(invalid_policy2.validate().is_err());
    }

    #[test]
    fn test_policy_normalize_auto_adjusts_max_shards() {
        // When min_shards > max_shards, normalize() should adjust max_shards
        let policy = ScalingPolicy {
            min_shards: 8,
            max_shards: 2, // Less than min_shards
            ..Default::default()
        };

        let normalized = policy.normalize();
        assert_eq!(
            normalized.max_shards, 8,
            "max_shards should be adjusted to min_shards"
        );
        assert!(
            normalized.validate().is_ok(),
            "normalized policy should be valid"
        );
    }

    /// Regression: BUG_REPORT.md #7 — `scale_up` previously allocated
    /// new shard ids as `shards.iter().max() + 1`, which reused ids
    /// after the highest-numbered shard was drained-and-removed.
    /// Reusing ids merges two distinct shard lifetimes in any
    /// external metric/checkpoint system that keys on shard id.
    /// The fix uses a monotonic `next_shard_id` counter.
    #[test]
    fn scale_up_does_not_reuse_ids_after_remove() {
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 16,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(2, 1024, policy).unwrap();

        // Initial ids are 0 and 1. Scale up to 4 — new ids must be
        // 2 and 3 (the next two slots from the monotonic counter).
        let new_ids = mapper.scale_up(2).unwrap();
        assert_eq!(new_ids, vec![2, 3]);

        // Drain one shard. `scale_down` picks by lowest weight, so
        // we don't get to choose which id is drained. Whichever it
        // is, we then force it through Stopped + remove and check
        // that the removed id is *not* reissued by the next
        // scale_up.
        let drained = mapper.scale_down(1).unwrap();
        let drained_id = drained[0];
        for shard in mapper.shards.write().iter_mut() {
            if shard.id == drained_id {
                shard.state = ShardState::Stopped;
            }
        }
        let removed = mapper.remove_stopped_shards();
        assert_eq!(removed, vec![drained_id]);

        // Scale up by 1. The broken `max(existing_ids) + 1` allocator
        // could revive the just-removed id whenever `drained_id` had
        // been the highest id present (e.g. id 3 with 0/1/2/3 → 0/1/2
        // → next would be 3 again). The fix uses the monotonic
        // counter, which sits at 4 after the earlier scale_up, so
        // the new id must be 4 regardless of which id was drained.
        let new_ids = mapper.scale_up(1).unwrap();
        assert_eq!(
            new_ids,
            vec![4],
            "shard id {drained_id} was just removed; reusing any \
             previously-issued id would merge two distinct shard \
             lifetimes in external systems"
        );
    }

    #[test]
    fn test_policy_normalize_preserves_valid_config() {
        // When max_shards >= min_shards, normalize() should not change anything
        let policy = ScalingPolicy {
            min_shards: 4,
            max_shards: 16,
            ..Default::default()
        };

        let normalized = policy.normalize();
        assert_eq!(normalized.min_shards, 4);
        assert_eq!(normalized.max_shards, 16);
    }

    #[test]
    fn test_shard_mapper_normalizes_policy() {
        // ShardMapper should accept a policy where min_shards > default max_shards
        // because it calls normalize() internally
        let policy = ScalingPolicy {
            min_shards: 4,
            ..Default::default()
        };

        // This should succeed even on machines with < 4 CPUs
        let result = ShardMapper::new(4, 1024, policy);
        assert!(
            result.is_ok(),
            "ShardMapper should normalize policy automatically"
        );
    }

    #[test]
    fn test_shard_mapper_adjusts_max_shards_to_initial_count() {
        // ShardMapper should adjust max_shards to accommodate initial_shards
        // even if initial_shards > default max_shards (CPU count)
        let policy = ScalingPolicy::default();

        // Create mapper with 8 initial shards - should work even on 2-core machines
        let result = ShardMapper::new(8, 1024, policy);
        assert!(
            result.is_ok(),
            "ShardMapper should adjust max_shards to initial_shards"
        );

        let mapper = result.unwrap();
        assert_eq!(mapper.active_shard_count(), 8);

        // Verify the policy was adjusted
        assert!(
            mapper.policy().max_shards >= 8,
            "max_shards should be at least initial_shards"
        );
    }

    #[test]
    fn test_scale_up_max_shards_concurrent() {
        use std::sync::Arc;
        use std::thread;

        let policy = ScalingPolicy {
            max_shards: 10,
            cooldown: Duration::from_nanos(1), // Disable cooldown for test
            ..Default::default()
        };
        let mapper = Arc::new(ShardMapper::new(5, 1024, policy).unwrap());

        // Spawn multiple threads that all try to scale up
        let mut handles = vec![];
        for _ in 0..5 {
            let mapper_clone = mapper.clone();
            handles.push(thread::spawn(move || mapper_clone.scale_up(3)));
        }

        // Collect results
        let results: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();

        // Some should succeed, some should fail with AtMaxShards
        let successes: Vec<_> = results.iter().filter(|r| r.is_ok()).collect();
        let failures: Vec<_> = results
            .iter()
            .filter(|r| matches!(r, Err(ScalingError::AtMaxShards)))
            .collect();

        // We started with 5, max is 10, each tries to add 3
        // At most we can add 5 more shards, so at most 1-2 can succeed
        assert!(
            !successes.is_empty() || !failures.is_empty(),
            "at least some operations should complete"
        );

        // Final count should not exceed max_shards
        assert!(
            mapper.active_shard_count() <= 10,
            "should never exceed max_shards, got {}",
            mapper.active_shard_count()
        );
    }

    /// Multiple `scale_up_provisioning(1)` calls must
    /// never push `active_count` past `max_shards` via subsequent
    /// `activate()` calls. Pre-fix the budget gate
    /// (`check_scale_up_budget`) only counted ALREADY-active
    /// shards, so several `scale_up_provisioning` calls could each
    /// pass and then each `activate()` unconditionally bumped
    /// `active_count` past the cap.
    ///
    /// Setup: at the budget edge, allocate two Provisioning shards
    /// in sequence (both pass the gate because `active_count`
    /// hasn't moved). The first `activate()` fills the budget;
    /// the second must surface `AtMaxShards` rather than overflow.
    #[test]
    fn activate_rejects_when_active_count_would_exceed_max_shards() {
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 4,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(3, 1024, policy).unwrap();
        // active_count = 3, max = 4. Allocate TWO Provisioning
        // shards; both pass the budget gate (it sees active_count=3).
        let ids_a = mapper.scale_up_provisioning(1).unwrap();
        // The 1ns cooldown elapses on every realistic scheduler
        // tick; if we lose that race, retry. Release-mode and
        // some debug-build paths occasionally finish the first
        // allocation in <1ns of wall time.
        let ids_b = loop {
            match mapper.scale_up_provisioning(1) {
                Ok(v) => break v,
                Err(ScalingError::InCooldown) => {
                    std::thread::sleep(Duration::from_nanos(50));
                }
                Err(e) => panic!("unexpected error from scale_up_provisioning: {:?}", e),
            }
        };
        assert_eq!(ids_a.len(), 1);
        assert_eq!(ids_b.len(), 1);

        // First activate succeeds (active_count goes 3 → 4 = max).
        mapper
            .activate(ids_a[0])
            .expect("first activate must succeed");
        assert_eq!(mapper.active_shard_count(), 4);

        // Second activate must REFUSE — it would push past max.
        let err = mapper
            .activate(ids_b[0])
            .expect_err("second activate must reject");
        assert!(
            matches!(err, ScalingError::AtMaxShards),
            "expected AtMaxShards, got {:?}",
            err
        );
        // active_count must still be at the cap, not over it.
        assert_eq!(mapper.active_shard_count(), 4);
    }

    /// Pin: under contention, the active_count never transiently
    /// exceeds `max_shards`. Pre-fix `activate` released the
    /// shards write-lock BEFORE the `fetch_add(1)`, so two
    /// concurrent activators could each pass the budget gate
    /// (both reading the pre-bump count) and both bump,
    /// overshooting the cap by 1 at any observation point.
    #[test]
    fn concurrent_activate_never_exceeds_max_shards() {
        use std::sync::Arc;
        use std::sync::Barrier;
        use std::thread;

        const ITERATIONS: usize = 200;

        for iter in 0..ITERATIONS {
            let policy = ScalingPolicy {
                min_shards: 1,
                max_shards: 4,
                cooldown: Duration::from_nanos(1),
                ..Default::default()
            };
            // Start at active=3, allocate two Provisioning ids so
            // both threads have a candidate to activate. With the
            // pre-fix race: thread A loads count=3, validates,
            // sets state Active, drops lock; thread B loads count
            // (still 3), validates, sets state Active, drops lock;
            // A bumps → 4; B bumps → 5. Post-fix B observes
            // count=4 inside its lock and rejects with
            // AtMaxShards.
            let mapper = Arc::new(ShardMapper::new(3, 1024, policy).unwrap());
            let ids_a = mapper.scale_up_provisioning(1).unwrap();
            // The 1ns cooldown elapses on every realistic
            // scheduler tick; if we lose that race, retry once
            // (release-mode iterations occasionally finish the
            // first allocation in <1ns of wall time).
            let ids_b = loop {
                match mapper.scale_up_provisioning(1) {
                    Ok(v) => break v,
                    Err(ScalingError::InCooldown) => {
                        std::thread::sleep(Duration::from_micros(10));
                        continue;
                    }
                    Err(e) => panic!("unexpected error: {:?}", e),
                }
            };
            assert_eq!(ids_a.len(), 1);
            assert_eq!(ids_b.len(), 1);

            let barrier = Arc::new(Barrier::new(2));
            let m1 = mapper.clone();
            let m2 = mapper.clone();
            let b1 = barrier.clone();
            let b2 = barrier.clone();
            let id_a = ids_a[0];
            let id_b = ids_b[0];

            let h1 = thread::spawn(move || {
                b1.wait();
                m1.activate(id_a)
            });
            let h2 = thread::spawn(move || {
                b2.wait();
                m2.activate(id_b)
            });
            let r1 = h1.join().expect("thread A panicked");
            let r2 = h2.join().expect("thread B panicked");

            // Exactly one must succeed and one must reject with
            // AtMaxShards.
            let ok_count = [&r1, &r2].iter().filter(|r| r.is_ok()).count();
            let at_max_count = [&r1, &r2]
                .iter()
                .filter(|r| matches!(r, Err(ScalingError::AtMaxShards)))
                .count();
            assert_eq!(
                ok_count, 1,
                "iter {}: expected exactly one Ok, got r1={:?} r2={:?}",
                iter, r1, r2
            );
            assert_eq!(
                at_max_count, 1,
                "iter {}: expected exactly one AtMaxShards, got r1={:?} r2={:?}",
                iter, r1, r2
            );

            // active_count must not exceed max_shards at any
            // observation point.
            assert!(
                mapper.active_shard_count() <= 4,
                "iter {}: active_count={} exceeded max_shards=4",
                iter,
                mapper.active_shard_count(),
            );
        }
    }

    /// Two concurrent `scale_up(1)` calls must never both succeed
    /// inside a single cooldown window. Before the fix, the
    /// cooldown check happened only under a read lock that was
    /// released *before* the mutating write lock, so two threads
    /// could both observe `last_scaling=None` (or stale), both
    /// acquire the write lock in turn, and both succeed — racing
    /// past the cooldown floor and (on a max-shard-bounded
    /// scenario) potentially the `max_shards` cap.
    ///
    /// Pin: across `ITERATIONS` rounds of two-thread races, every
    /// iteration sees exactly one success and one `InCooldown`.
    #[test]
    fn cooldown_is_enforced_under_concurrent_scale_up() {
        use std::sync::Arc;
        use std::sync::Barrier;
        use std::thread;

        const ITERATIONS: usize = 1_000;

        for iter in 0..ITERATIONS {
            let policy = ScalingPolicy {
                min_shards: 1,
                max_shards: 16,
                // Large cooldown so a single iteration can't pass
                // the floor between the two threads' calls.
                cooldown: Duration::from_secs(60),
                ..Default::default()
            };
            let mapper = Arc::new(ShardMapper::new(2, 1024, policy).unwrap());
            let barrier = Arc::new(Barrier::new(2));

            let m1 = mapper.clone();
            let b1 = barrier.clone();
            let m2 = mapper.clone();
            let b2 = barrier.clone();

            let h1 = thread::spawn(move || {
                b1.wait();
                m1.scale_up(1)
            });
            let h2 = thread::spawn(move || {
                b2.wait();
                m2.scale_up(1)
            });

            let r1 = h1.join().unwrap();
            let r2 = h2.join().unwrap();

            let oks = [&r1, &r2].iter().filter(|r| r.is_ok()).count();
            let cooldowns = [&r1, &r2]
                .iter()
                .filter(|r| matches!(r, Err(ScalingError::InCooldown)))
                .count();

            assert_eq!(
                oks, 1,
                "iter {iter}: expected exactly one Ok, got r1={r1:?}, r2={r2:?}"
            );
            assert_eq!(
                cooldowns, 1,
                "iter {iter}: expected exactly one InCooldown, got r1={r1:?}, r2={r2:?}"
            );
            assert_eq!(
                mapper.active_shard_count(),
                3,
                "iter {iter}: cooldown violated — both calls mutated state (shard count {})",
                mapper.active_shard_count()
            );
        }
    }

    #[test]
    fn test_scale_up_overflow_protection() {
        // Create mapper with a high starting shard ID to test overflow
        // protection. The monotonic id allocator advances by 1 per
        // shard regardless of which shards have been removed, so we
        // bump `next_shard_id` directly to simulate a near-`u16::MAX`
        // state.
        let policy = ScalingPolicy {
            max_shards: u16::MAX,
            ..Default::default()
        };
        let mapper = ShardMapper::new(1, 1024, policy).unwrap();

        // Position the allocator so the next id is u16::MAX - 1.
        // Trying to allocate 3 ids would need {MAX-1, MAX, MAX+1};
        // the last is unrepresentable in `u16`, so scale_up rejects.
        mapper
            .next_shard_id
            .store(u16::MAX - 1, AtomicOrdering::Relaxed);

        let result = mapper.scale_up(3);
        assert!(matches!(result, Err(ScalingError::AtMaxShards)));

        // Adding 1 shard should still work (id = MAX - 1).
        let result = mapper.scale_up(1);
        assert!(result.is_ok());
    }

    #[test]
    fn test_evaluate_scaling_auto_scale_disabled() {
        let policy = ScalingPolicy {
            auto_scale: false,
            ..Default::default()
        };
        let mapper = ShardMapper::new(4, 1024, policy).unwrap();

        let decision = mapper.evaluate_scaling();
        assert!(matches!(decision, ScalingDecision::None));
    }

    #[test]
    fn test_evaluate_scaling_in_cooldown() {
        let policy = ScalingPolicy {
            cooldown: Duration::from_secs(60),
            ..Default::default()
        };
        let mapper = ShardMapper::new(4, 1024, policy).unwrap();

        // Trigger a scaling operation to start cooldown
        *mapper.last_scaling.write() = Some(Instant::now());

        let decision = mapper.evaluate_scaling();
        assert!(matches!(decision, ScalingDecision::None));
    }

    #[test]
    fn test_evaluate_scaling_scale_up_on_high_fill_ratio() {
        let policy = ScalingPolicy {
            fill_ratio_threshold: 0.7,
            max_shards: 8,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(4, 1024, policy).unwrap();

        // Set high fill ratio on majority of shards
        {
            let mut shards = mapper.shards.write();
            for shard in shards.iter_mut() {
                shard.last_metrics.fill_ratio = 0.9; // Above threshold
            }
        }

        let decision = mapper.evaluate_scaling();
        assert!(matches!(decision, ScalingDecision::ScaleUp(_)));
    }

    #[test]
    fn test_evaluate_scaling_scale_up_on_high_latency() {
        let policy = ScalingPolicy {
            push_latency_threshold_ns: 10,
            max_shards: 8,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(4, 1024, policy).unwrap();

        // Set high push latency on majority of shards
        {
            let mut shards = mapper.shards.write();
            for shard in shards.iter_mut() {
                shard.last_metrics.avg_push_latency_ns = 100; // Above threshold
            }
        }

        let decision = mapper.evaluate_scaling();
        assert!(matches!(decision, ScalingDecision::ScaleUp(_)));
    }

    #[test]
    fn test_evaluate_scaling_scale_down_on_underutilized() {
        let policy = ScalingPolicy {
            underutilized_threshold: 0.2,
            min_shards: 2,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(4, 1024, policy).unwrap();

        // Set low fill ratio and zero event rate on majority of shards
        {
            let mut shards = mapper.shards.write();
            for shard in shards.iter_mut() {
                shard.last_metrics.fill_ratio = 0.05; // Below threshold
                shard.last_metrics.event_rate = 0;
            }
        }

        let decision = mapper.evaluate_scaling();
        assert!(matches!(decision, ScalingDecision::ScaleDown(_)));
    }

    /// Regression: a freshly-activated shard must NOT
    /// immediately count toward the underutilized tally on the
    /// next `evaluate_scaling`. Pre-fix the new shard's
    /// `last_metrics` was the `ShardMetrics::new(id)` placeholder
    /// (`fill_ratio = 0.0, event_rate = 0`), which matched the
    /// underutilized trigger immediately — oscillating the
    /// system: scale-up → next tick scale-down → next tick
    /// scale-up.
    ///
    /// Post-fix `MappedShard.activated_at` is stamped at the
    /// Provisioning → Active transition (and at scale-up
    /// construction for `Active`-from-create shards), and
    /// `evaluate_scaling` skips shards within `policy.cooldown`
    /// of activation.
    #[test]
    fn freshly_added_shard_skipped_from_evaluate_scaling_warmup() {
        // Short cooldown so we can pin "outside warmup" with a
        // small `checked_sub`-safe offset (200ms). Production
        // stamps boot shards 1 hour in the past, but that
        // subtraction falls back to `Instant::now()` on hosts
        // with sub-1h uptime (Windows Instant is bounded by
        // boot), making all shards land inside the warmup
        // window. We override both boot shards explicitly so
        // the test is independent of system uptime.
        let policy = ScalingPolicy {
            underutilized_threshold: 0.2,
            min_shards: 1,
            cooldown: Duration::from_millis(100),
            ..Default::default()
        };
        let mapper = ShardMapper::new(3, 1024, policy).unwrap();

        // Direct manipulation of the shard list: pin two boot
        // shards as underutilized AND safely outside the warmup
        // window, and inject one freshly-activated shard with
        // `activated_at = now()` whose placeholder metrics ALSO
        // trigger the underutilized predicate. Without the
        // warmup skip, the count would be 3 of 3 shards
        // underutilized → scale-down. WITH the warmup skip,
        // only the 2 boot shards count, and 2 of 3 still
        // exceeds the 3/2 = 1 majority — scale-down still fires
        // (driven by the boot shards), but the decisive property
        // is that the fresh shard is correctly excluded.
        let old_ts = Instant::now()
            .checked_sub(Duration::from_millis(200))
            .expect("test host uptime should exceed 200ms");
        {
            let mut shards = mapper.shards.write();
            for shard in shards.iter_mut() {
                shard.last_metrics.fill_ratio = 0.05;
                shard.last_metrics.event_rate = 0;
                // Pin boot shards safely outside the cooldown,
                // independent of production's 1-hour stamp.
                shard.activated_at = old_ts;
            }
            // The third shard is "fresh": stamp activated_at to
            // now, simulating a just-activated shard.
            shards[2].activated_at = Instant::now();
        }

        // The fresh shard must satisfy the warmup predicate.
        let now = Instant::now();
        let warmup_excluded: Vec<u16> = mapper
            .shards
            .read()
            .iter()
            .filter(|s| now.duration_since(s.activated_at) < mapper.policy.cooldown)
            .map(|s| s.id)
            .collect();
        assert_eq!(
            warmup_excluded,
            vec![2u16],
            "regression: only shard id 2 (just-stamped) should be \
             within the warmup window; boot shards are stamped \
             200ms in the past (well outside the 100ms cooldown)"
        );

        // And evaluate_scaling must still produce ScaleDown
        // (driven by the 2 boot shards) — the fresh shard's
        // placeholder doesn't get to vote.
        let decision = mapper.evaluate_scaling();
        assert!(
            matches!(decision, ScalingDecision::ScaleDown(_)),
            "scale-down still fires from the boot shards' real \
             underutilization, but driven by 2 of 3 not 3 of 3 — \
             got {:?}",
            decision,
        );
    }

    #[test]
    fn test_evaluate_scaling_no_scale_up_at_max() {
        let policy = ScalingPolicy {
            fill_ratio_threshold: 0.7,
            max_shards: 4, // Already at max
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(4, 1024, policy).unwrap();

        // Set high fill ratio
        {
            let mut shards = mapper.shards.write();
            for shard in shards.iter_mut() {
                shard.last_metrics.fill_ratio = 0.9;
            }
        }

        let decision = mapper.evaluate_scaling();
        assert!(matches!(decision, ScalingDecision::None));
    }

    #[test]
    fn test_evaluate_scaling_no_scale_down_at_min() {
        let policy = ScalingPolicy {
            underutilized_threshold: 0.2,
            min_shards: 4, // Already at min
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(4, 1024, policy).unwrap();

        // Set underutilized metrics
        {
            let mut shards = mapper.shards.write();
            for shard in shards.iter_mut() {
                shard.last_metrics.fill_ratio = 0.05;
                shard.last_metrics.event_rate = 0;
            }
        }

        let decision = mapper.evaluate_scaling();
        assert!(matches!(decision, ScalingDecision::None));
    }

    #[test]
    fn test_evaluate_scaling_ignores_draining_shards() {
        let policy = ScalingPolicy {
            fill_ratio_threshold: 0.7,
            max_shards: 8,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(4, 1024, policy).unwrap();

        // Set high fill ratio but mark shards as draining
        {
            let mut shards = mapper.shards.write();
            for shard in shards.iter_mut() {
                shard.last_metrics.fill_ratio = 0.9;
                shard.state = ShardState::Draining;
            }
        }

        // Should not scale up since draining shards are ignored
        let decision = mapper.evaluate_scaling();
        assert!(matches!(decision, ScalingDecision::None));
    }

    #[test]
    fn test_shard_metrics_new() {
        let metrics = ShardMetrics::new(5);
        assert_eq!(metrics.shard_id, 5);
        assert_eq!(metrics.fill_ratio, 0.0);
        assert_eq!(metrics.event_rate, 0);
        assert!(!metrics.draining);
    }

    #[test]
    fn test_shard_metrics_compute_weight() {
        let mut metrics = ShardMetrics::new(0);
        metrics.fill_ratio = 0.5;
        metrics.avg_push_latency_ns = 100;
        metrics.event_rate = 1_000_000;

        metrics.compute_weight();
        assert!(metrics.weight > 0.0);
    }

    #[test]
    fn test_shard_metrics_draining_max_weight() {
        let mut metrics = ShardMetrics::new(0);
        metrics.draining = true;
        metrics.compute_weight();
        assert_eq!(metrics.weight, f64::MAX);
    }

    #[test]
    fn test_scaling_decision_debug() {
        let none = ScalingDecision::None;
        let up = ScalingDecision::ScaleUp(2);
        let down = ScalingDecision::ScaleDown(1);

        assert!(format!("{:?}", none).contains("None"));
        assert!(format!("{:?}", up).contains("ScaleUp"));
        assert!(format!("{:?}", down).contains("ScaleDown"));
    }

    /// Regression: BUG_REPORT.md #46 — `scale_up_provisioning`
    /// allocates a shard but `select_shard` must NOT route to it
    /// until `activate` has been called. This is the load-bearing
    /// invariant that lets `EventBus::add_shard_internal` wire up
    /// drain workers before producers can land in the new ring
    /// buffer.
    #[test]
    fn provisioning_shard_is_not_selectable_until_activated() {
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 16,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(2, 1024, policy).unwrap();

        // Allocate a provisioning shard. Existing ids are 0,1; new
        // is 2.
        let new_ids = mapper.scale_up_provisioning(1).unwrap();
        assert_eq!(new_ids, vec![2]);

        // The provisioning shard must NOT appear in active accounting.
        assert_eq!(mapper.active_shard_count(), 2);
        assert_eq!(mapper.shard_state(2), Some(ShardState::Provisioning));

        // Across many hashes, `select_shard` must never pick id 2.
        // Spread the input hashes across the u64 range so the
        // unbiased Lemire-style mapping actually
        // distributes — sequential small ids would all map to
        // index 0 because `(small * len) >> 64 = 0`. Production
        // callers pass `xxh3_64`-hashed event payloads, which
        // are uniform u64s; this scaling mirrors that.
        let mut seen: std::collections::HashSet<u16> = std::collections::HashSet::new();
        let stride = u64::MAX / 10_000;
        for i in 0u64..10_000 {
            seen.insert(mapper.select_shard(i.wrapping_mul(stride)));
        }
        assert!(
            !seen.contains(&2),
            "provisioning shard 2 was selected — \
             producers would push into a ring buffer with no consumer (#46)"
        );
        assert!(seen == [0, 1].into_iter().collect());

        // After `activate`, the shard is selectable.
        mapper.activate(2).unwrap();
        assert_eq!(mapper.shard_state(2), Some(ShardState::Active));
        assert_eq!(mapper.active_shard_count(), 3);

        let mut seen_after: std::collections::HashSet<u16> = std::collections::HashSet::new();
        let stride = u64::MAX / 10_000;
        for i in 0u64..10_000 {
            seen_after.insert(mapper.select_shard(i.wrapping_mul(stride)));
        }
        assert!(
            seen_after.contains(&2),
            "after activate, shard 2 should be a valid select_shard target"
        );
    }

    /// Regression: BUG_REPORT.md #51 — when no `Active` shard exists
    /// (e.g. all shards are Draining or Provisioning), `select_shard`
    /// must NOT fall back to a Draining shard. Pushing into a
    /// draining shard increments `pushes_since_drain_start` and
    /// blocks finalization indefinitely.
    #[test]
    fn select_shard_does_not_fall_back_to_draining() {
        let policy = ScalingPolicy {
            min_shards: 0,
            max_shards: 8,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        // Force min_shards = 0 via direct construction so we can drain
        // every shard.
        let mut policy = policy;
        policy.min_shards = 1;
        let mapper = ShardMapper::new(2, 1024, policy).unwrap();

        // Force every shard into Draining state directly. We bypass
        // `scale_down`'s min_shards floor by mutating the test-only
        // accessor; this models a state that can otherwise be
        // reached by `drain_specific` calls.
        //
        // Backdoor mutations don't trigger the `select_shard`
        // selection-table rebuild that the public API does, so we
        // call it explicitly here to model what `drain_specific`
        // would have done.
        {
            let mut shards = mapper.shards.write();
            for s in shards.iter_mut() {
                s.state = ShardState::Draining;
                s.metrics.set_draining(true);
            }
            mapper.rebuild_selection_table_locked(&shards);
        }

        // The fallback must return the OOB sentinel `u16::MAX` rather
        // than a draining shard id (0 or 1). The upstream
        // `resolve_idx` path will see no match for `u16::MAX` and
        // surface as `Unrouted` (#44), which is the correct signal:
        // "no destination, do not push."
        for hash in 0u64..1_000 {
            let picked = mapper.select_shard(hash);
            assert_eq!(
                picked,
                u16::MAX,
                "fallback returned a draining shard ({}); pushes there would \
                 deadlock finalize_draining (#51)",
                picked
            );
        }
    }

    /// Regression: BUG_REPORT.md #32 — `scale_up(0)` previously
    /// bumped the cooldown timestamp and could spuriously fail at
    /// `u16::MAX` even though zero ids were being allocated.
    #[test]
    fn scale_up_zero_is_a_noop() {
        let policy = ScalingPolicy {
            cooldown: Duration::from_secs(60),
            ..Default::default()
        };
        let mapper = ShardMapper::new(2, 1024, policy).unwrap();
        // Pretend we just scaled — cooldown is active.
        *mapper.last_scaling.write() = Some(Instant::now());

        // scale_up(0) must not return InCooldown and must not bump
        // last_scaling.
        let before_ts = *mapper.last_scaling.read();
        let r = mapper.scale_up(0);
        assert!(
            r.is_ok(),
            "scale_up(0) should succeed as a no-op, got {r:?}"
        );
        assert_eq!(r.unwrap().len(), 0);
        let after_ts = *mapper.last_scaling.read();
        assert_eq!(before_ts, after_ts, "scale_up(0) bumped cooldown timestamp");

        // Also: position the allocator at u16::MAX and verify a
        // count==0 call doesn't trip the sentinel check.
        mapper
            .next_shard_id
            .store(u16::MAX, AtomicOrdering::Relaxed);
        assert!(mapper.scale_up(0).is_ok());
    }

    /// Regression: `drain_specific` must bump `last_scaling` so
    /// a subsequent `scale_up` is gated by the cooldown floor.
    /// Pre-fix `scale_down` wrote `last_scaling` but
    /// `drain_specific` did not, so the sequence
    /// `drain_specific(id) → scale_up(N)` bypassed the cooldown
    /// — even though both decrement `active_count` and so
    /// should be symmetric from the budget-math perspective.
    #[test]
    fn drain_specific_bumps_last_scaling_so_scale_up_respects_cooldown() {
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 8,
            // A cooldown long enough that `Instant::now()` won't
            // accidentally elapse it during the test, but short
            // enough not to slow CI.
            cooldown: Duration::from_secs(60),
            ..Default::default()
        };
        let mapper = ShardMapper::new(3, 1024, policy).unwrap();

        // Pre-condition: no prior scaling action.
        assert!(mapper.last_scaling.read().is_none());

        let before = Instant::now();
        mapper.drain_specific(0).unwrap();
        let after_ts = mapper
            .last_scaling
            .read()
            .expect("drain_specific must record a `last_scaling` timestamp");
        // Sanity: the recorded timestamp is in the test window.
        assert!(
            after_ts >= before,
            "last_scaling must be bumped to a current Instant (got {:?}, before was {:?})",
            after_ts,
            before
        );

        // The decisive sealed property: a follow-up scale_up
        // must trip the cooldown gate. Pre-fix this would have
        // succeeded immediately because `last_scaling` was never
        // written by `drain_specific`.
        let err = mapper
            .scale_up(1)
            .expect_err("scale_up immediately after drain_specific must hit cooldown");
        match err {
            ScalingError::InCooldown => {} // expected
            other => panic!("expected InCooldown after drain_specific, got {:?}", other),
        }
    }

    /// Regression: BUG_REPORT.md #48 — `drain_specific` must
    /// transition the shard's `MappedShard.state` to `Draining` so
    /// that `select_shard` stops routing to it. The previous
    /// `drain_shard` only flipped a metrics atomic.
    #[test]
    fn drain_specific_takes_shard_out_of_select() {
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 8,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(3, 1024, policy).unwrap();

        // Drain shard 0.
        mapper.drain_specific(0).unwrap();
        assert_eq!(mapper.shard_state(0), Some(ShardState::Draining));
        assert_eq!(mapper.active_shard_count(), 2);

        // Across many hashes, select_shard must not pick id 0.
        for hash in 0u64..10_000 {
            let picked = mapper.select_shard(hash);
            assert_ne!(
                picked, 0,
                "select_shard returned a Draining shard id 0 — \
                 producer pushes there would block finalize_draining (#48)"
            );
        }
    }

    /// Regression: BUG_REPORT.md #33 — `set_draining(true)` and a
    /// concurrent `record_push` race on `pushes_since_drain_start`.
    /// The race itself can't be eliminated without a CAS loop on
    /// the counter (which would penalize the hot path), so the
    /// best we can pin is: after the dust settles, the counter
    /// value is bounded — never larger than the number of pushes
    /// that genuinely overlapped the transition. This catches a
    /// regression where the store-zero stops happening, where the
    /// flag publish stops happening, or where future code adds
    /// drift that compounds the race across many transitions.
    #[test]
    fn set_draining_resets_counter_under_concurrent_pushes() {
        use std::sync::Barrier;
        use std::thread;

        const ITERATIONS: usize = 200;
        const PUSHERS: usize = 4;

        for _ in 0..ITERATIONS {
            let collector = Arc::new(ShardMetricsCollector::new(0, 1024));

            // Sanity: counter starts at zero.
            assert_eq!(collector.pushes_since_drain_start(), 0);

            // Pre-load with a noticeable amount of "before drain"
            // pushes so the reset has work to do.
            for _ in 0..50 {
                collector.record_push(1);
            }
            assert_eq!(collector.pushes_since_drain_start(), 50);

            // Race: every pusher hammers `record_push` while one
            // thread calls `set_draining(true)`. After the barrier,
            // we want to observe that the counter ends up bounded
            // by PUSHERS (the number of pushes that genuinely
            // overlapped the transition) — and crucially NOT 50+,
            // which is what the buggy code with a missing reset
            // would leave behind.
            let barrier = Arc::new(Barrier::new(PUSHERS + 1));
            let mut handles = Vec::with_capacity(PUSHERS);
            for _ in 0..PUSHERS {
                let c = Arc::clone(&collector);
                let b = Arc::clone(&barrier);
                handles.push(thread::spawn(move || {
                    b.wait();
                    c.record_push(1);
                }));
            }

            barrier.wait();
            collector.set_draining(true);
            for h in handles {
                h.join().unwrap();
            }

            // After reset + at-most-PUSHERS racing pushes, the
            // counter is bounded. The strong guarantee we want is
            // "the 50 pre-drain pushes did NOT survive the reset."
            // Anything <= PUSHERS is acceptable — the race may
            // count any subset of the racing pushes.
            let final_count = collector.pushes_since_drain_start();
            assert!(
                final_count <= PUSHERS as u64,
                "set_draining reset is broken: counter is {} after reset, \
                 expected at most {} (#33)",
                final_count,
                PUSHERS
            );
        }
    }

    /// Regression: BUG_REPORT.md #49 — `finalize_draining` must
    /// drop the `shards` write lock before calling `on_shard_removed`,
    /// so a callback that re-enters the mapper (read methods like
    /// `shard_state`, `active_shard_ids`, etc.) does not deadlock.
    #[test]
    fn finalize_draining_does_not_deadlock_on_callback_reentry() {
        use std::sync::Mutex;
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 8,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = Arc::new(ShardMapper::new(2, 1024, policy).unwrap());

        // Set a callback that re-enters the mapper. Before the fix
        // this acquires `shards.read()` while finalize_draining
        // still holds `shards.write()`, deadlocking on parking_lot's
        // non-recursive RwLock.
        type Observation = (u16, Option<ShardState>);
        let observed_states: Arc<Mutex<Vec<Observation>>> = Arc::new(Mutex::new(Vec::new()));
        {
            let mapper_for_cb = Arc::clone(&mapper);
            let observed = Arc::clone(&observed_states);
            mapper.set_on_shard_removed(move |id| {
                let st = mapper_for_cb.shard_state(id);
                observed.lock().unwrap().push((id, st));
            });
        }

        // Drive a shard all the way to Stopped: drain it, mark its
        // metrics empty (current_len = 0), and drop drain_started
        // far enough back that the 100ms gate is satisfied.
        mapper.drain_specific(0).unwrap();
        {
            let mut shards = mapper.shards.write();
            let s = shards.iter_mut().find(|s| s.id == 0).unwrap();
            s.metrics.current_len.store(0, AtomicOrdering::Relaxed);
            s.metrics
                .pushes_since_drain_start
                .store(0, AtomicOrdering::Relaxed);
            // Backdate drain_started so the elapsed > 100ms gate trips.
            s.drain_started = Some(Instant::now() - Duration::from_secs(1));
        }

        // The call below would deadlock with the bug present.
        let stopped = mapper.finalize_draining();
        assert_eq!(stopped, vec![0]);

        // The callback must have run AND been able to read state
        // (proving the lock was released).
        let observed = observed_states.lock().unwrap().clone();
        assert_eq!(observed.len(), 1);
        assert_eq!(observed[0].0, 0);
        // State should be `Stopped` (set by finalize_draining before
        // the lock was dropped).
        assert_eq!(observed[0].1, Some(ShardState::Stopped));
    }

    /// `activate` must signal whether a state transition
    /// actually occurred so callers can avoid double-counting.
    /// Pre-fix it returned `Result<(), _>`, so a caller that
    /// invoked `activate` twice on the same shard incremented its
    /// own external counter (e.g. `ShardManager::num_shards`)
    /// twice for one logical transition.
    #[test]
    fn activate_returns_true_on_transition_and_false_on_idempotent_call() {
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 16,
            cooldown: Duration::from_nanos(1),
            ..Default::default()
        };
        let mapper = ShardMapper::new(2, 1024, policy).unwrap();

        // Newly-provisioned shard 2 → Active: real transition.
        let new_ids = mapper.scale_up_provisioning(1).unwrap();
        assert_eq!(new_ids, vec![2]);
        let first = mapper.activate(2).unwrap();
        assert!(
            first,
            "first activate on a Provisioning shard must return true"
        );
        assert_eq!(mapper.active_shard_count(), 3);

        // Second activate on the same already-Active shard:
        // idempotent, no transition.
        let second = mapper.activate(2).unwrap();
        assert!(
            !second,
            "activate on an already-Active shard must return false; \
             pre-fix this returned Ok(()) and the caller couldn't tell"
        );
        // active_count must NOT have moved.
        assert_eq!(
            mapper.active_shard_count(),
            3,
            "idempotent activate must not bump active_count"
        );
    }
}