irontide-session 0.165.0

BitTorrent session management: peers, torrents, and piece selection
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
//! Concurrent piece reservation state for per-peer dispatch.
//!
//! Each peer exclusively owns pieces it is downloading. Block dispatch within
//! a reserved piece is sequential (block 0, 1, 2, ...). When a peer disconnects
//! mid-piece, the piece is released and another peer can re-request all blocks.

use parking_lot::Mutex;
use std::collections::BTreeSet;
use std::collections::VecDeque;
use std::net::SocketAddr;
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, AtomicU32, AtomicU64, Ordering};

use irontide_core::Lengths;
use irontide_storage::Bitfield;
use rustc_hash::FxHashMap;

/// A block request to send to a peer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct BlockRequest {
    /// Piece index.
    pub piece: u32,
    /// Byte offset within the piece.
    pub begin: u32,
    /// Length in bytes.
    pub length: u32,
}

/// Piece-level state for lock-free CAS reservation.
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PieceState {
    Available = 0,
    Reserved = 1,
    Complete = 2,
    Unwanted = 3,
    Endgame = 4,
}

impl PieceState {
    fn from_u8(v: u8) -> Self {
        match v {
            0 => Self::Available,
            1 => Self::Reserved,
            2 => Self::Complete,
            3 => Self::Unwanted,
            4 => Self::Endgame,
            _ => Self::Available, // defensive fallback
        }
    }
}

/// Lock-free per-piece state array. Shared across all peer tasks via `Arc`.
///
/// Each piece occupies one `AtomicU8`. Peers reserve pieces via CAS
/// (compare-and-swap) without any mutex or RwLock.
#[derive(Debug)]
pub(crate) struct AtomicPieceStates {
    states: Vec<AtomicU8>,
}

#[allow(dead_code)]
impl AtomicPieceStates {
    pub fn new(num_pieces: u32, we_have: &Bitfield, wanted: &Bitfield) -> Self {
        let states: Vec<AtomicU8> = (0..num_pieces)
            .map(|i| {
                let val = if we_have.get(i) {
                    PieceState::Complete as u8
                } else if !wanted.get(i) {
                    PieceState::Unwanted as u8
                } else {
                    PieceState::Available as u8
                };
                AtomicU8::new(val)
            })
            .collect();
        Self { states }
    }

    /// Attempt to reserve a piece via CAS.
    pub fn try_reserve(&self, index: u32) -> bool {
        let atom = &self.states[index as usize];
        if atom
            .compare_exchange(
                PieceState::Available as u8,
                PieceState::Reserved as u8,
                Ordering::Relaxed,
                Ordering::Relaxed,
            )
            .is_ok()
        {
            return true;
        }
        let current = atom.load(Ordering::Relaxed);
        current == PieceState::Endgame as u8
    }

    pub fn mark_complete(&self, index: u32) {
        self.states[index as usize].store(PieceState::Complete as u8, Ordering::Relaxed);
    }

    pub fn release(&self, index: u32) {
        self.states[index as usize].store(PieceState::Available as u8, Ordering::Relaxed);
    }

    pub fn transition_to_endgame(&self, index: u32) {
        self.states[index as usize].store(PieceState::Endgame as u8, Ordering::Relaxed);
    }

    pub fn mark_unwanted(&self, index: u32) {
        self.states[index as usize].store(PieceState::Unwanted as u8, Ordering::Relaxed);
    }

    pub fn mark_available(&self, index: u32) {
        let _ = self.states[index as usize].compare_exchange(
            PieceState::Unwanted as u8,
            PieceState::Available as u8,
            Ordering::Relaxed,
            Ordering::Relaxed,
        );
    }

    pub fn get(&self, index: u32) -> PieceState {
        PieceState::from_u8(self.states[index as usize].load(Ordering::Relaxed))
    }

    pub fn force_reserved(&self, index: u32) {
        self.states[index as usize].store(PieceState::Reserved as u8, Ordering::Relaxed);
    }

    pub fn len(&self) -> u32 {
        self.states.len() as u32
    }

    /// Count pieces in Reserved or Endgame state (i.e., in-flight).
    ///
    /// This is the authoritative in-flight count — unlike `piece_owner` which
    /// only learns about reservations when chunks arrive, this reflects CAS
    /// reservations immediately.
    pub fn in_flight_count(&self) -> u32 {
        self.states
            .iter()
            .filter(|a| {
                let v = a.load(Ordering::Relaxed);
                v == PieceState::Reserved as u8 || v == PieceState::Endgame as u8
            })
            .count() as u32
    }
}

/// Pre-computed rarest-first ordering, cheaply cloneable via `Arc`.
///
/// Built by TorrentActor on a 500ms timer (or significant events like
/// peer join/leave). Uses bucket sort -- O(n) where n = num_pieces.
#[derive(Debug)]
pub(crate) struct AvailabilitySnapshot {
    /// Pieces ordered rarest-first. Walk from index 0 for rarest pieces.
    pub order: Vec<u32>,
    /// Monotonically increasing counter. Peer tasks compare against their
    /// cached generation to detect when the snapshot was rebuilt.
    pub generation: u64,
}

impl AvailabilitySnapshot {
    /// Build a new snapshot using bucket sort.
    ///
    /// `availability[i]` = number of peers that have piece `i`.
    /// `atomic_states` is consulted to skip Complete/Unwanted/Reserved pieces.
    /// `priority_pieces` are placed at the front regardless of availability.
    ///
    /// Bucket sort is O(n + max_avail) -- much cheaper than the O(n log n)
    /// sort in the old `rebuild_candidates()`.
    pub fn build(
        availability: &[u32],
        atomic_states: &AtomicPieceStates,
        priority_pieces: &BTreeSet<u32>,
        generation: u64,
    ) -> Self {
        let num_pieces = availability.len();
        if num_pieces == 0 {
            return Self {
                order: Vec::new(),
                generation,
            };
        }

        // Find max availability for bucket allocation.
        let max_avail = availability.iter().copied().max().unwrap_or(0) as usize;

        // Bucket sort: buckets[a] = list of pieces with availability `a`.
        let mut buckets: Vec<Vec<u32>> = vec![Vec::new(); max_avail + 1];
        for (piece, &avail_count) in availability.iter().enumerate() {
            let state = atomic_states.get(piece as u32);
            match state {
                PieceState::Complete | PieceState::Unwanted | PieceState::Reserved => continue,
                PieceState::Available | PieceState::Endgame => {}
            }
            buckets[avail_count as usize].push(piece as u32);
        }

        // Flatten buckets: rarest (lowest availability) first.
        // Priority pieces go to the very front.
        let mut order = Vec::with_capacity(num_pieces);
        let mut non_priority = Vec::with_capacity(num_pieces);

        for bucket in &buckets {
            for &piece in bucket {
                if priority_pieces.contains(&piece) {
                    order.push(piece);
                } else {
                    non_priority.push(piece);
                }
            }
        }
        order.append(&mut non_priority);

        Self { order, generation }
    }
}

/// Per-peer dispatch state. Owned entirely by a single peer task -- no
/// sharing, no locks, no atomics on the hot path.
pub(crate) struct PeerDispatchState {
    /// Current snapshot (cheaply cloned via Arc).
    snapshot: Arc<AvailabilitySnapshot>,
    /// Cursor position within `snapshot.order`.
    cursor: usize,
    /// The piece currently being block-dispatched, if any.
    current_piece: Option<CurrentPiece>,
    /// Lengths for block arithmetic.
    lengths: Lengths,
    /// Shared block-level request/receipt tracking for steal visibility.
    block_maps: Option<Arc<BlockMaps>>,
    /// Shared queue of pieces available for block-level stealing.
    steal_candidates: Option<Arc<StealCandidates>>,
    /// M120: Per-piece write guards to prevent steal/write races.
    piece_write_guards: Option<Arc<PieceWriteGuards>>,
}

/// Tracks block-by-block progress within a single piece.
struct CurrentPiece {
    piece: u32,
    next_block: u32,
    total_blocks: u32,
}

#[allow(dead_code)]
impl PeerDispatchState {
    pub fn new(snapshot: Arc<AvailabilitySnapshot>, lengths: Lengths) -> Self {
        Self {
            snapshot,
            cursor: 0,
            current_piece: None,
            lengths,
            block_maps: None,
            steal_candidates: None,
            piece_write_guards: None,
        }
    }

    /// Set the shared block maps for steal visibility.
    pub fn set_block_maps(&mut self, bm: Arc<BlockMaps>) {
        self.block_maps = Some(bm);
    }

    /// Set the shared steal candidates queue.
    pub fn set_steal_candidates(&mut self, sc: Arc<StealCandidates>) {
        self.steal_candidates = Some(sc);
    }

    /// Set the per-piece write guards for steal/write race prevention.
    pub fn set_piece_write_guards(&mut self, pwg: Arc<PieceWriteGuards>) {
        self.piece_write_guards = Some(pwg);
    }

    /// Update the snapshot reference. Resets cursor if generation changed.
    pub fn update_snapshot(&mut self, snapshot: Arc<AvailabilitySnapshot>) {
        if snapshot.generation != self.snapshot.generation {
            self.cursor = 0;
        }
        self.snapshot = snapshot;
    }

    /// Get the next block request for this peer.
    ///
    /// Hot path: if a current piece has remaining blocks, returns immediately
    /// with no atomic operations at all.
    ///
    /// Cold path: walks the snapshot cursor, calling `try_reserve()` (CAS) on
    /// each candidate the peer has. First CAS success becomes the new
    /// `current_piece`.
    ///
    /// Returns `None` when the cursor is exhausted (caller should wait on
    /// `piece_notify`).
    pub fn next_block(
        &mut self,
        peer_bitfield: &Bitfield,
        atomic_states: &AtomicPieceStates,
    ) -> Option<BlockRequest> {
        // 1. Hot path: current piece has blocks remaining.
        if let Some(ref mut cp) = self.current_piece {
            if cp.next_block < cp.total_blocks {
                let piece = cp.piece;
                let block_idx = cp.next_block;
                cp.next_block += 1;
                // Register in block maps for steal visibility.
                if let Some(ref bm) = self.block_maps {
                    bm.mark_requested(piece, block_idx);
                }
                return Some(self.make_block_request(piece, block_idx));
            }
            // Piece fully dispatched -- drop it.
            self.current_piece = None;
        }

        // 2. Cold path: walk snapshot for a new piece.
        while self.cursor < self.snapshot.order.len() {
            let piece = self.snapshot.order[self.cursor];
            self.cursor += 1;

            if !peer_bitfield.get(piece) {
                continue;
            }

            if atomic_states.try_reserve(piece) {
                let total_blocks = self.lengths.chunks_in_piece(piece);
                if total_blocks == 0 {
                    continue;
                }
                // Mark block 0 as requested in block maps.
                if let Some(ref bm) = self.block_maps {
                    bm.mark_requested(piece, 0);
                }
                self.current_piece = Some(CurrentPiece {
                    piece,
                    next_block: 1,
                    total_blocks,
                });
                return Some(self.make_block_request(piece, 0));
            }
        }

        // 3. Steal path: find unrequested blocks in other peers' pieces.
        // Bound attempts to avoid spinning when queue has many exhausted pieces.
        const MAX_STEAL_ATTEMPTS: usize = 32;
        if let (Some(bm), Some(sc)) = (&self.block_maps, &self.steal_candidates) {
            for _ in 0..MAX_STEAL_ATTEMPTS {
                let Some(piece) = sc.pop() else { break };

                // Skip if peer doesn't have this piece.
                if !peer_bitfield.get(piece) {
                    sc.push(piece); // put it back for other peers
                    continue;
                }

                // Skip if piece is already Complete or Unwanted.
                let state = atomic_states.get(piece);
                if state == PieceState::Complete || state == PieceState::Unwanted {
                    continue; // don't put back — it's done
                }

                // M120/M128: Skip if a write is in-flight for this piece.
                if let Some(ref pwg) = self.piece_write_guards
                    && !pwg.is_idle(piece)
                {
                    sc.push(piece); // put back — write in progress
                    continue;
                }

                let total_blocks = self.lengths.chunks_in_piece(piece);

                // Find an unrequested block.
                if let Some(block_idx) = bm.next_unrequested(piece, total_blocks) {
                    // Atomically claim it.
                    let was_already_set = bm.mark_requested(piece, block_idx);
                    if was_already_set {
                        // Another peer beat us — put piece back and try again.
                        sc.push(piece);
                        continue;
                    }
                    // Check if piece still has more unrequested blocks.
                    if bm.next_unrequested(piece, total_blocks).is_some() {
                        sc.push(piece); // still has work — put back for others
                    }
                    // DON'T set current_piece — stolen blocks are one-at-a-time.
                    // (we don't "own" this piece, so we shouldn't dispatch sequential blocks)
                    return Some(self.make_block_request(piece, block_idx));
                }
                // No unrequested blocks — piece is fully requested, don't put back.
            }
        }

        None // all phases exhausted
    }

    /// Build a `BlockRequest` using `Lengths::chunk_info`.
    fn make_block_request(&self, piece: u32, block_idx: u32) -> BlockRequest {
        let (begin, length) = self
            .lengths
            .chunk_info(piece, block_idx)
            .expect("block_idx out of range for piece");
        BlockRequest {
            piece,
            begin,
            length,
        }
    }

    /// Get the currently active piece, if any (for release on disconnect).
    pub fn current_piece_index(&self) -> Option<u32> {
        self.current_piece.as_ref().map(|cp| cp.piece)
    }

    /// Clear current piece (e.g., on choke -- peer must re-reserve).
    pub fn clear_current_piece(&mut self) {
        self.current_piece = None;
    }
}

use slab::Slab;

/// Arena-allocated peer index for compact piece tracking.
///
/// Maps between dense u16 slot indices and SocketAddr values.
/// Managed by TorrentActor (single-threaded, no lock needed).
pub(crate) struct PeerSlab {
    inner: Slab<SocketAddr>,
    addr_to_slot: FxHashMap<SocketAddr, u16>,
}

#[allow(dead_code)]
impl PeerSlab {
    pub fn new() -> Self {
        PeerSlab {
            inner: Slab::with_capacity(64),
            addr_to_slot: FxHashMap::default(),
        }
    }

    /// Insert a peer, returning its compact slot index.
    ///
    /// Panics if the slab exceeds u16::MAX entries (65535 peers).
    pub fn insert(&mut self, addr: SocketAddr) -> u16 {
        let key = self.inner.insert(addr);
        assert!(
            key <= u16::MAX as usize,
            "peer slab overflow: {key} > u16::MAX"
        );
        let slot = key as u16;
        self.addr_to_slot.insert(addr, slot);
        slot
    }

    /// Remove a peer by slot index.
    pub fn remove(&mut self, slot: u16) -> SocketAddr {
        let addr = self.inner.remove(slot as usize);
        self.addr_to_slot.remove(&addr);
        addr
    }

    /// Remove a peer by address. Returns the slot if found.
    pub fn remove_by_addr(&mut self, addr: &SocketAddr) -> Option<u16> {
        if let Some(slot) = self.addr_to_slot.remove(addr) {
            self.inner.remove(slot as usize);
            Some(slot)
        } else {
            None
        }
    }

    /// Look up peer address by slot index.
    pub fn get(&self, slot: u16) -> Option<&SocketAddr> {
        self.inner.get(slot as usize)
    }

    /// Look up slot index by peer address.
    pub fn slot_of(&self, addr: &SocketAddr) -> Option<u16> {
        self.addr_to_slot.get(addr).copied()
    }

    /// Check if a slot is occupied.
    pub fn contains(&self, slot: u16) -> bool {
        self.inner.contains(slot as usize)
    }

    /// Number of active peers.
    pub fn len(&self) -> usize {
        self.inner.len()
    }
}

/// Pre-allocated atomic bit arrays for tracking per-block request and receipt
/// status across all pieces.
///
/// Enables multiple peers to claim individual blocks within the same piece via
/// lock-free atomic operations. Each piece has two bit arrays (`requested` and
/// `received`), stored as flat `Vec<AtomicU64>` indexed by piece and block.
///
/// This is the core data structure for M103 per-block stealing: when a piece
/// is in steal mode, peers race to claim individual blocks via `mark_requested`
/// (atomic `fetch_or`), achieving zero-contention dispatch.
#[derive(Debug)]
pub(crate) struct BlockMaps {
    /// Bit array tracking which blocks have been requested (one bit per block).
    requested: Vec<AtomicU64>,
    /// Bit array tracking which blocks have been received (one bit per block).
    received: Vec<AtomicU64>,
    /// Number of `AtomicU64` words per piece. Computed as `ceil(max_blocks / 64)`.
    words_per_piece: u32,
}

#[allow(dead_code)]
impl BlockMaps {
    /// Create a new `BlockMaps` for the given torrent geometry.
    ///
    /// Pre-allocates atomic bit arrays for all pieces. Each piece gets
    /// `ceil(max_blocks_per_piece / 64)` words, where max blocks is derived
    /// from the first (full-size) piece.
    pub fn new(num_pieces: u32, lengths: &Lengths) -> Self {
        let max_blocks = if num_pieces == 0 {
            0
        } else {
            lengths.chunks_in_piece(0)
        };
        let words_per_piece = max_blocks.saturating_add(63) / 64;
        let total_words = (num_pieces as usize).saturating_mul(words_per_piece as usize);

        let mut requested = Vec::with_capacity(total_words);
        let mut received = Vec::with_capacity(total_words);
        for _ in 0..total_words {
            requested.push(AtomicU64::new(0));
            received.push(AtomicU64::new(0));
        }

        Self {
            requested,
            received,
            words_per_piece,
        }
    }

    /// Atomically mark a block as requested.
    ///
    /// Returns `true` if the bit was **already set** (another peer won the
    /// race). Returns `false` if this caller claimed the block.
    ///
    /// Uses `AtomicU64::fetch_or` on the appropriate word, then checks the
    /// old value's bit to determine whether we were first.
    pub fn mark_requested(&self, piece: u32, block: u32) -> bool {
        let (word_idx, bit_mask) = self.word_and_mask(piece, block);
        let old = self.requested[word_idx].fetch_or(bit_mask, Ordering::Relaxed);
        old & bit_mask != 0
    }

    /// Mark a block as received.
    pub fn mark_received(&self, piece: u32, block: u32) {
        let (word_idx, bit_mask) = self.word_and_mask(piece, block);
        self.received[word_idx].fetch_or(bit_mask, Ordering::Relaxed);
    }

    /// Find the first block index in `[0, total_blocks)` where the requested
    /// bit is **not** set.
    ///
    /// Returns `None` if all blocks have been requested.
    pub fn next_unrequested(&self, piece: u32, total_blocks: u32) -> Option<u32> {
        let base = (piece as usize).checked_mul(self.words_per_piece as usize)?;

        for block in 0..total_blocks {
            let word_offset = block / 64;
            let bit = block % 64;
            let word_idx = base.checked_add(word_offset as usize)?;
            let word = self.requested.get(word_idx)?.load(Ordering::Relaxed);
            if word & (1u64 << bit) == 0 {
                return Some(block);
            }
        }
        None
    }

    /// Check if all blocks in `[0, total_blocks)` have their received bit set.
    pub fn all_received(&self, piece: u32, total_blocks: u32) -> bool {
        let Some(base) = (piece as usize).checked_mul(self.words_per_piece as usize) else {
            return total_blocks == 0;
        };

        for block in 0..total_blocks {
            let word_offset = block / 64;
            let bit = block % 64;
            let Some(atom) = self.received.get(base + word_offset as usize) else {
                return false;
            };
            let word = atom.load(Ordering::Relaxed);
            if word & (1u64 << bit) == 0 {
                return false;
            }
        }
        true
    }

    /// Zero all bits for a piece (both requested and received arrays).
    ///
    /// Called on piece completion or hash failure to reset block tracking.
    pub fn clear(&self, piece: u32, total_blocks: u32) {
        let base = (piece as usize).saturating_mul(self.words_per_piece as usize);
        let num_words = total_blocks.saturating_add(63) / 64;

        for w in 0..num_words as usize {
            let idx = base + w;
            if let Some(atom) = self.requested.get(idx) {
                atom.store(0, Ordering::Relaxed);
            }
            if let Some(atom) = self.received.get(idx) {
                atom.store(0, Ordering::Relaxed);
            }
        }
    }

    /// Compute the flat array index and bit mask for a (piece, block) pair.
    fn word_and_mask(&self, piece: u32, block: u32) -> (usize, u64) {
        let base = (piece as usize).saturating_mul(self.words_per_piece as usize);
        let word_offset = (block / 64) as usize;
        let idx = base + word_offset;
        debug_assert!(
            idx < self.requested.len(),
            "BlockMaps: piece={piece} block={block} out of range (idx={idx}, len={})",
            self.requested.len()
        );
        let bit = block % 64;
        (idx, 1u64 << bit)
    }
}

/// Shared queue of pieces available for block-level stealing.
///
/// Maintained by `TorrentActor`. When a piece has unrequested blocks available
/// for stealing (e.g., original owner disconnected mid-piece), it is pushed
/// here. Peer tasks pop from the front to find steal work.
///
/// Uses `parking_lot::Mutex` (not tokio) because the lock is never held across
/// an await point — operations are trivially fast (push/pop/linear scan).
#[derive(Debug)]
pub(crate) struct StealCandidates {
    inner: Mutex<VecDeque<u32>>,
    timing: crate::timed_lock::LockTimingSettings,
}

#[allow(dead_code)]
impl StealCandidates {
    /// Create an empty steal queue.
    pub fn new() -> Self {
        Self {
            inner: Mutex::new(VecDeque::new()),
            timing: crate::timed_lock::LockTimingSettings::default(),
        }
    }

    /// Create an empty steal queue with custom lock timing.
    pub fn with_timing(timing: crate::timed_lock::LockTimingSettings) -> Self {
        Self {
            inner: Mutex::new(VecDeque::new()),
            timing,
        }
    }

    /// Add a piece to the back of the steal queue (no-op if already present).
    pub fn push(&self, piece: u32) {
        let mut guard =
            crate::timed_lock::TimedGuard::new(self.inner.lock(), &self.timing, "steal_candidates");
        if !guard.contains(&piece) {
            guard.push_back(piece);
        }
    }

    /// Batch-add pieces to the steal queue (single lock acquisition).
    /// Skips pieces already present.
    pub fn push_batch(&self, pieces: &[u32]) {
        let mut guard =
            crate::timed_lock::TimedGuard::new(self.inner.lock(), &self.timing, "steal_candidates");
        for &piece in pieces {
            if !guard.contains(&piece) {
                guard.push_back(piece);
            }
        }
    }

    /// Take a piece from the front of the steal queue.
    pub fn pop(&self) -> Option<u32> {
        let mut guard =
            crate::timed_lock::TimedGuard::new(self.inner.lock(), &self.timing, "steal_candidates");
        guard.pop_front()
    }

    /// Remove a specific piece from the queue (linear scan).
    ///
    /// This is O(n) but acceptable because the steal queue is small — it only
    /// contains pieces that are partially downloaded and available for stealing.
    pub fn remove(&self, piece: u32) {
        let mut guard =
            crate::timed_lock::TimedGuard::new(self.inner.lock(), &self.timing, "steal_candidates");
        if let Some(pos) = guard.iter().position(|&p| p == piece) {
            guard.remove(pos);
        }
    }
}

/// Per-piece write guards to prevent steal/write races.
///
/// The write path (synchronous pwrite in `on_piece_sync`) calls `begin_write()`
/// which increments an atomic ref count — multiple concurrent block writes to
/// the same piece are fine since they target different offsets.
///
/// The steal path (`next_block` Phase 3) calls `is_idle()` — if any write
/// is in-flight for a piece, the count is > 0 and the steal skips it.
///
/// This is lock-free: an `AtomicU32` per piece replaces the former
/// `parking_lot::RwLock<()>` per piece, eliminating lock contention on the
/// steal retry path (1-5ms `try_write()` failures).
#[derive(Debug)]
pub(crate) struct PieceWriteGuards {
    /// Per-piece active-writer count. 0 = idle, >0 = writes in flight.
    writers: Vec<AtomicU32>,
}

impl PieceWriteGuards {
    /// Create guards for `num_pieces` pieces.
    pub fn new(num_pieces: u32) -> Self {
        Self {
            writers: (0..num_pieces).map(|_| AtomicU32::new(0)).collect(),
        }
    }

    /// Begin a write — increment active writer count.
    /// Returns a guard that decrements on drop, or `None` for out-of-bounds.
    #[inline]
    pub fn begin_write(&self, piece: u32) -> Option<WriteGuard<'_>> {
        let counter = self.writers.get(piece as usize)?;
        counter.fetch_add(1, Ordering::Relaxed);
        Some(WriteGuard { counter })
    }

    /// Check if a piece has no active writers.
    /// Used by steal path — returns false if any write in progress or out-of-bounds.
    #[inline]
    pub fn is_idle(&self, piece: u32) -> bool {
        self.writers
            .get(piece as usize)
            .is_some_and(|c| c.load(Ordering::Acquire) == 0)
    }
}

/// RAII guard that decrements the writer count on drop.
#[derive(Debug)]
pub(crate) struct WriteGuard<'a> {
    counter: &'a AtomicU32,
}

impl Drop for WriteGuard<'_> {
    fn drop(&mut self) {
        self.counter.fetch_sub(1, Ordering::Release);
    }
}

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

    /// Test lengths: 128 KiB total, 32 KiB pieces, 16 KiB chunks.
    /// → 4 pieces, 2 blocks per piece.
    fn test_lengths() -> Lengths {
        Lengths::new(128 * 1024, 32768, 16384)
    }

    fn all_pieces_wanted(num_pieces: u32) -> Bitfield {
        let mut bf = Bitfield::new(num_pieces);
        for i in 0..num_pieces {
            bf.set(i);
        }
        bf
    }

    fn peer_has_all(num_pieces: u32) -> Bitfield {
        all_pieces_wanted(num_pieces)
    }

    fn addr(port: u16) -> SocketAddr {
        use std::net::{IpAddr, Ipv4Addr};
        SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port)
    }

    #[test]
    fn atomic_reserve_release_cycle() {
        let num_pieces = 8u32;
        let we_have = Bitfield::new(num_pieces);
        let wanted = all_pieces_wanted(num_pieces);
        let states = AtomicPieceStates::new(num_pieces, &we_have, &wanted);

        // All pieces start Available
        for i in 0..num_pieces {
            assert_eq!(states.get(i), PieceState::Available);
        }

        // Reserve piece 3
        assert!(states.try_reserve(3));
        assert_eq!(states.get(3), PieceState::Reserved);

        // Second reserve on same piece fails
        assert!(!states.try_reserve(3));

        // Release piece 3
        states.release(3);
        assert_eq!(states.get(3), PieceState::Available);

        // Can reserve again after release
        assert!(states.try_reserve(3));
        assert_eq!(states.get(3), PieceState::Reserved);

        // Mark complete — try_reserve fails
        states.mark_complete(3);
        assert_eq!(states.get(3), PieceState::Complete);
        assert!(!states.try_reserve(3));

        // force_reserved works
        states.force_reserved(3);
        assert_eq!(states.get(3), PieceState::Reserved);
    }

    #[test]
    fn atomic_unwanted_transitions() {
        let num_pieces = 4u32;
        let we_have = Bitfield::new(num_pieces);
        let mut wanted = Bitfield::new(num_pieces);
        wanted.set(0);
        wanted.set(1);
        // pieces 2,3 are unwanted

        let states = AtomicPieceStates::new(num_pieces, &we_have, &wanted);

        assert_eq!(states.get(0), PieceState::Available);
        assert_eq!(states.get(1), PieceState::Available);
        assert_eq!(states.get(2), PieceState::Unwanted);
        assert_eq!(states.get(3), PieceState::Unwanted);

        // Cannot reserve unwanted pieces
        assert!(!states.try_reserve(2));

        // mark_available only transitions Unwanted -> Available
        states.mark_available(2);
        assert_eq!(states.get(2), PieceState::Available);
        assert!(states.try_reserve(2));

        // mark_available on Reserved is a no-op (CAS fails safely)
        states.mark_available(2);
        assert_eq!(states.get(2), PieceState::Reserved);
    }

    #[test]
    fn atomic_we_have_initialized_complete() {
        let num_pieces = 4u32;
        let mut we_have = Bitfield::new(num_pieces);
        we_have.set(0);
        we_have.set(2);
        let wanted = all_pieces_wanted(num_pieces);

        let states = AtomicPieceStates::new(num_pieces, &we_have, &wanted);

        assert_eq!(states.get(0), PieceState::Complete);
        assert_eq!(states.get(1), PieceState::Available);
        assert_eq!(states.get(2), PieceState::Complete);
        assert_eq!(states.get(3), PieceState::Available);

        assert!(!states.try_reserve(0));
        assert!(states.try_reserve(1));
    }

    #[test]
    fn atomic_len() {
        let states = AtomicPieceStates::new(42, &Bitfield::new(42), &all_pieces_wanted(42));
        assert_eq!(states.len(), 42);
    }

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

        let states = Arc::new(AtomicPieceStates::new(
            1,
            &Bitfield::new(1),
            &all_pieces_wanted(1),
        ));

        let results: Vec<bool> = std::thread::scope(|s| {
            let handles: Vec<_> = (0..2)
                .map(|_| {
                    let st = Arc::clone(&states);
                    s.spawn(move || st.try_reserve(0))
                })
                .collect();
            handles.into_iter().map(|h| h.join().unwrap()).collect()
        });

        // Exactly one thread wins the CAS
        let wins: usize = results.iter().filter(|&&r| r).count();
        assert_eq!(wins, 1, "exactly one CAS should succeed, got {wins}");
        assert_eq!(states.get(0), PieceState::Reserved);
    }

    #[test]
    fn atomic_endgame_allows_multiple_reservations() {
        let states = AtomicPieceStates::new(4, &Bitfield::new(4), &all_pieces_wanted(4));

        // Reserve piece 1 normally
        assert!(states.try_reserve(1));
        assert_eq!(states.get(1), PieceState::Reserved);

        // Transition to endgame
        states.transition_to_endgame(1);
        assert_eq!(states.get(1), PieceState::Endgame);

        // Multiple "reservations" succeed in endgame
        assert!(states.try_reserve(1));
        assert!(states.try_reserve(1));
        assert!(states.try_reserve(1));

        // State remains Endgame (not transitioned to Reserved)
        assert_eq!(states.get(1), PieceState::Endgame);
    }

    #[test]
    fn atomic_endgame_state_transitions() {
        let states = AtomicPieceStates::new(4, &Bitfield::new(4), &all_pieces_wanted(4));

        // Reserve pieces 0 and 1
        assert!(states.try_reserve(0));
        assert!(states.try_reserve(1));

        // Transition both to endgame
        states.transition_to_endgame(0);
        states.transition_to_endgame(1);

        // force_reserved brings piece back from Endgame
        states.force_reserved(0);
        assert_eq!(states.get(0), PieceState::Reserved);

        // release from Endgame (for pieces without an owner)
        states.release(1);
        assert_eq!(states.get(1), PieceState::Available);
    }

    #[test]
    fn peer_slab_insert_remove_lookup() {
        let mut slab = PeerSlab::new();
        let a1 = addr(1000);
        let a2 = addr(2000);
        let a3 = addr(3000);

        let s1 = slab.insert(a1);
        let s2 = slab.insert(a2);
        let s3 = slab.insert(a3);

        assert_eq!(slab.len(), 3);
        assert_eq!(slab.get(s1), Some(&a1));
        assert_eq!(slab.get(s2), Some(&a2));
        assert_eq!(slab.slot_of(&a2), Some(s2));
        assert!(slab.contains(s1));

        // Remove by slot
        let removed = slab.remove(s2);
        assert_eq!(removed, a2);
        assert_eq!(slab.len(), 2);
        assert_eq!(slab.get(s2), None);
        assert_eq!(slab.slot_of(&a2), None);

        // Remove by addr
        let slot = slab.remove_by_addr(&a3);
        assert_eq!(slot, Some(s3));
        assert_eq!(slab.len(), 1);

        // Remove nonexistent
        assert_eq!(slab.remove_by_addr(&a3), None);
    }

    #[test]
    fn peer_slab_slot_reuse() {
        let mut slab = PeerSlab::new();
        let a1 = addr(1000);
        let a2 = addr(2000);

        let s1 = slab.insert(a1);
        slab.remove(s1);

        // Slab reuses freed slot
        let s2 = slab.insert(a2);
        assert_eq!(s2, s1); // same slot index reused
        assert_eq!(slab.get(s2), Some(&a2));
    }

    #[test]
    fn snapshot_bucket_sort_rarest_first() {
        let num_pieces = 5u32;
        // availability: piece 0=3, piece 1=1, piece 2=2, piece 3=1, piece 4=3
        let availability = vec![3u32, 1, 2, 1, 3];
        let states = AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        );
        let priority = BTreeSet::new();

        let snap = AvailabilitySnapshot::build(&availability, &states, &priority, 1);

        assert_eq!(snap.generation, 1);
        // Rarest pieces (availability=1) should come first: pieces 1 and 3
        // Then availability=2: piece 2
        // Then availability=3: pieces 0 and 4
        assert_eq!(snap.order.len(), 5);
        // First two must be the rarest (1 and 3 in some order)
        let first_two: std::collections::HashSet<u32> = snap.order[..2].iter().copied().collect();
        assert!(first_two.contains(&1));
        assert!(first_two.contains(&3));
        // Third must be piece 2 (availability=2)
        assert_eq!(snap.order[2], 2);
        // Last two are pieces 0 and 4 (availability=3)
        let last_two: std::collections::HashSet<u32> = snap.order[3..].iter().copied().collect();
        assert!(last_two.contains(&0));
        assert!(last_two.contains(&4));
    }

    #[test]
    fn snapshot_skips_complete_reserved_unwanted() {
        let num_pieces = 5u32;
        let availability = vec![1u32; 5];
        let states = AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        );

        // Mark some pieces with non-Available states
        states.mark_complete(0);
        assert!(states.try_reserve(2)); // Reserved
        states.mark_unwanted(4);

        let snap = AvailabilitySnapshot::build(&availability, &states, &BTreeSet::new(), 1);

        // Only pieces 1 and 3 should be in the snapshot (Available)
        assert_eq!(snap.order.len(), 2);
        assert!(snap.order.contains(&1));
        assert!(snap.order.contains(&3));
    }

    #[test]
    fn snapshot_includes_endgame_pieces() {
        let num_pieces = 3u32;
        let availability = vec![1u32; 3];
        let states = AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        );

        assert!(states.try_reserve(1));
        states.transition_to_endgame(1);

        let snap = AvailabilitySnapshot::build(&availability, &states, &BTreeSet::new(), 1);

        // Pieces 0 (Available) and 1 (Endgame) should be included, piece 2 (Available) too
        assert_eq!(snap.order.len(), 3);
        assert!(snap.order.contains(&0));
        assert!(snap.order.contains(&1));
        assert!(snap.order.contains(&2));
    }

    #[test]
    fn snapshot_empty_torrent() {
        let snap = AvailabilitySnapshot::build(
            &[],
            &AtomicPieceStates::new(0, &Bitfield::new(0), &Bitfield::new(0)),
            &BTreeSet::new(),
            0,
        );
        assert!(snap.order.is_empty());
        assert_eq!(snap.generation, 0);
    }

    #[test]
    fn snapshot_priority_pieces_sort_first() {
        let num_pieces = 5u32;
        // piece 2 has the HIGHEST availability (least rare)
        let availability = vec![2u32, 3, 10, 1, 5];
        let states = AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        );
        let mut priority = BTreeSet::new();
        priority.insert(2); // piece 2 is priority despite high availability

        let snap = AvailabilitySnapshot::build(&availability, &states, &priority, 1);

        // Piece 2 must be at the very front despite availability=10
        assert_eq!(snap.order[0], 2, "priority piece should be first");
        // Remaining pieces in rarest-first order: 3 (avail=1), 0 (avail=2), 1 (avail=3), 4 (avail=5)
        assert_eq!(snap.order[1], 3); // avail=1
        assert_eq!(snap.order[2], 0); // avail=2
        assert_eq!(snap.order[3], 1); // avail=3
        assert_eq!(snap.order[4], 4); // avail=5
    }

    // ---- PeerDispatchState tests ----

    #[test]
    fn dispatch_state_hot_path_returns_blocks() {
        let lengths = test_lengths(); // 128 KiB total, 32 KiB pieces, 16 KiB chunks -> 4 pieces, 2 blocks each
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        let peer_bf = peer_has_all(num_pieces);

        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);

        // First call: cold path (no current piece) -> reserves a piece, returns block 0
        let b0 = ds.next_block(&peer_bf, &states).unwrap();
        assert_eq!(b0.begin, 0);
        assert_eq!(b0.length, 16384);
        let piece = b0.piece;

        // Second call: hot path -> returns block 1 of same piece
        let b1 = ds.next_block(&peer_bf, &states).unwrap();
        assert_eq!(b1.piece, piece);
        assert_eq!(b1.begin, 16384);
        assert_eq!(b1.length, 16384);

        // Third call: piece fully dispatched, cold path -> reserves next piece
        let b2 = ds.next_block(&peer_bf, &states).unwrap();
        assert_ne!(b2.piece, piece, "should get a different piece");
        assert_eq!(b2.begin, 0);
    }

    #[test]
    fn dispatch_state_cursor_exhausted_returns_none() {
        let lengths = test_lengths();
        let num_pieces = 2u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        let peer_bf = peer_has_all(num_pieces);

        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);

        // Exhaust all pieces (2 pieces x 2 blocks = 4 blocks)
        for _ in 0..4 {
            assert!(ds.next_block(&peer_bf, &states).is_some());
        }

        // Cursor exhausted
        assert!(ds.next_block(&peer_bf, &states).is_none());
    }

    #[test]
    fn dispatch_state_cursor_reset_on_generation_change() {
        let lengths = test_lengths();
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let availability = vec![1u32; num_pieces as usize];
        let snap1 = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        let peer_bf = peer_has_all(num_pieces);

        let mut ds = PeerDispatchState::new(Arc::clone(&snap1), lengths.clone());

        // Reserve piece from first snapshot
        let b0 = ds.next_block(&peer_bf, &states).unwrap();
        let first_piece = b0.piece;

        // Exhaust first piece
        ds.next_block(&peer_bf, &states).unwrap(); // block 1

        // Release the first piece (simulating actor releasing it)
        states.release(first_piece);

        // New snapshot with different generation
        let snap2 = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            2, // generation changed
        ));
        ds.update_snapshot(snap2);

        // Cursor should have been reset -- can see pieces from the start again
        let b_new = ds.next_block(&peer_bf, &states).unwrap();
        // The released piece should be available again at the front
        assert_eq!(b_new.begin, 0, "cursor should have reset to start");
    }

    #[test]
    fn dispatch_state_skips_pieces_peer_lacks() {
        let lengths = test_lengths();
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        // Peer only has piece 2
        let mut peer_bf = Bitfield::new(num_pieces);
        peer_bf.set(2);

        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));

        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);

        let b = ds.next_block(&peer_bf, &states).unwrap();
        assert_eq!(b.piece, 2, "should only get piece 2 which peer has");

        // Exhaust piece 2 (2 blocks)
        ds.next_block(&peer_bf, &states).unwrap();

        // No more pieces available for this peer
        assert!(ds.next_block(&peer_bf, &states).is_none());
    }

    #[test]
    fn dispatch_state_cas_failure_advances_cursor() {
        let lengths = test_lengths();
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let peer_bf = peer_has_all(num_pieces);

        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));

        // Pre-reserve all but the last piece externally (simulating other peers)
        let first_three: Vec<u32> = snap.order[..3].to_vec();
        for &p in &first_three {
            assert!(states.try_reserve(p));
        }

        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);

        // Should skip the first 3 (CAS fails) and get the 4th
        let b = ds.next_block(&peer_bf, &states).unwrap();
        assert!(
            !first_three.contains(&b.piece),
            "should skip pre-reserved pieces"
        );
    }

    #[test]
    fn dispatch_state_current_piece_and_clear() {
        let lengths = test_lengths();
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let peer_bf = peer_has_all(num_pieces);
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));

        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);

        // No current piece initially
        assert!(ds.current_piece_index().is_none());

        // Reserve a piece
        let b = ds.next_block(&peer_bf, &states).unwrap();
        assert_eq!(ds.current_piece_index(), Some(b.piece));

        // Clear current piece (simulating choke)
        ds.clear_current_piece();
        assert!(ds.current_piece_index().is_none());
    }

    #[test]
    fn snapshot_atomic_consistency_gap() {
        // Verify that stale snapshots don't cause double reservation.
        // Build a snapshot showing piece 2 as Available. Then another
        // "thread" reserves piece 2 before this peer walks to it.
        // The peer's CAS should fail, and it should advance to the next piece.
        let lengths = test_lengths();
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        let peer_bf = peer_has_all(num_pieces);

        // Pre-reserve the first piece in the snapshot order (simulating another peer)
        let contested_piece = snap.order[0];
        assert!(states.try_reserve(contested_piece));

        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);

        // Peer should skip the contested piece (CAS fails) and get the next one
        let b = ds.next_block(&peer_bf, &states).unwrap();
        assert_ne!(
            b.piece, contested_piece,
            "should skip piece reserved by another thread"
        );
    }

    #[test]
    fn multi_peer_dispatch_no_double_reservation() {
        // Two PeerDispatchStates share the same AtomicPieceStates.
        // Each should get unique pieces (no overlap).
        let lengths = test_lengths();
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        let peer_bf = peer_has_all(num_pieces);

        let mut ds1 = PeerDispatchState::new(Arc::clone(&snap), lengths.clone());
        let mut ds2 = PeerDispatchState::new(Arc::clone(&snap), lengths);

        let mut reserved = std::collections::HashSet::new();

        // Each peer reserves one piece (2 blocks each, but just check the piece)
        let b1 = ds1.next_block(&peer_bf, &states).unwrap();
        reserved.insert(b1.piece);
        let b2 = ds2.next_block(&peer_bf, &states).unwrap();
        reserved.insert(b2.piece);

        assert_eq!(
            reserved.len(),
            2,
            "two peers should get two different pieces"
        );
        assert_ne!(b1.piece, b2.piece);
    }

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

        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let availability = vec![2u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        let peer_bf = peer_has_all(num_pieces);

        // Simulate 4 peer tasks each trying to get a piece
        let reserved_pieces: Vec<u32> = std::thread::scope(|s| {
            let handles: Vec<_> = (0..4)
                .map(|_| {
                    let st = Arc::clone(&states);
                    let sn = Arc::clone(&snap);
                    let bf = peer_bf.clone();
                    let l = lengths.clone();
                    s.spawn(move || {
                        let mut ds = PeerDispatchState::new(sn, l);
                        ds.next_block(&bf, &st).map(|b| b.piece)
                    })
                })
                .collect();
            handles
                .into_iter()
                .filter_map(|h| h.join().unwrap())
                .collect()
        });

        // All 4 pieces should be reserved by exactly one peer each
        let unique: std::collections::HashSet<u32> = reserved_pieces.iter().copied().collect();
        assert_eq!(unique.len(), 4, "all 4 pieces should be uniquely reserved");
        assert_eq!(reserved_pieces.len(), 4);
    }

    #[test]
    fn integration_snapshot_rebuild_during_dispatch() {
        let lengths = test_lengths();
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let availability = vec![1u32; num_pieces as usize];
        let snap1 = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        let peer_bf = peer_has_all(num_pieces);

        let mut ds = PeerDispatchState::new(Arc::clone(&snap1), lengths.clone());

        // Get first piece
        let b0 = ds.next_block(&peer_bf, &states).unwrap();
        let first_piece = b0.piece;
        // Consume remaining block
        ds.next_block(&peer_bf, &states).unwrap();

        // Simulate: first piece completes, actor marks it Complete
        states.mark_complete(first_piece);

        // Actor rebuilds snapshot (Complete pieces excluded)
        let snap2 = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            2,
        ));
        ds.update_snapshot(snap2);

        // Peer should be able to continue dispatching remaining pieces
        let mut remaining = Vec::new();
        while let Some(b) = ds.next_block(&peer_bf, &states) {
            if b.begin == 0 {
                remaining.push(b.piece);
            }
        }

        // Should get the 3 remaining pieces (first_piece was Completed)
        assert_eq!(remaining.len(), 3);
        assert!(!remaining.contains(&first_piece));
    }

    #[test]
    fn integration_endgame_multi_reservation() {
        let lengths = test_lengths();
        let num_pieces = 2u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let availability = vec![3u32; num_pieces as usize];

        // Peer 1 reserves piece 0 normally
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        let peer_bf = peer_has_all(num_pieces);
        let mut ds1 = PeerDispatchState::new(Arc::clone(&snap), lengths.clone());
        let b1 = ds1.next_block(&peer_bf, &states).unwrap();
        assert_eq!(states.get(b1.piece), PieceState::Reserved);

        // Actor transitions to endgame
        states.transition_to_endgame(b1.piece);

        // Rebuild snapshot (Endgame pieces are included)
        let snap2 = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            2,
        ));

        // Peer 2 can also "reserve" the endgame piece
        let mut ds2 = PeerDispatchState::new(snap2, lengths.clone());
        let b2 = ds2.next_block(&peer_bf, &states).unwrap();

        // Both peers got pieces (peer 2 might get the endgame piece or the other one)
        // The key invariant: no deadlock, no panic, both get blocks
        assert!(b2.length > 0);
    }

    // ---- BlockMaps tests ----

    #[test]
    fn block_maps_mark_requested() {
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let bm = BlockMaps::new(4, &lengths);

        // First mark: we win the race (bit was not set).
        let was_set = bm.mark_requested(0, 0);
        assert!(
            !was_set,
            "first mark_requested should return false (we claimed it)"
        );

        // Second mark on same block: someone already claimed it.
        let was_set = bm.mark_requested(0, 0);
        assert!(
            was_set,
            "second mark_requested should return true (already set)"
        );
    }

    #[test]
    fn block_maps_mark_received() {
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let bm = BlockMaps::new(4, &lengths);

        // Not all received initially.
        assert!(!bm.all_received(0, 2));

        // Mark block 0 received — still not all.
        bm.mark_received(0, 0);
        assert!(!bm.all_received(0, 2));

        // Mark block 1 received — now all received.
        bm.mark_received(0, 1);
        assert!(bm.all_received(0, 2));
    }

    #[test]
    fn block_maps_next_unrequested() {
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let bm = BlockMaps::new(4, &lengths);

        // Initially block 0 is unrequested.
        assert_eq!(bm.next_unrequested(1, 2), Some(0));

        // Request block 0 — next unrequested is block 1.
        bm.mark_requested(1, 0);
        assert_eq!(bm.next_unrequested(1, 2), Some(1));

        // Request block 1 — no unrequested blocks remain.
        bm.mark_requested(1, 1);
        assert_eq!(bm.next_unrequested(1, 2), None);
    }

    #[test]
    fn block_maps_all_requested() {
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let bm = BlockMaps::new(4, &lengths);

        // Request all blocks in piece 2.
        bm.mark_requested(2, 0);
        bm.mark_requested(2, 1);

        // next_unrequested should return None.
        assert_eq!(bm.next_unrequested(2, 2), None);
    }

    #[test]
    fn block_maps_all_received() {
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let bm = BlockMaps::new(4, &lengths);

        // Mark all blocks in piece 3 as received.
        bm.mark_received(3, 0);
        bm.mark_received(3, 1);

        assert!(bm.all_received(3, 2));
    }

    #[test]
    fn block_maps_partial_received() {
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let bm = BlockMaps::new(4, &lengths);

        // Mark only block 0 of piece 1 as received.
        bm.mark_received(1, 0);

        assert!(
            !bm.all_received(1, 2),
            "should be false with only 1 of 2 blocks received"
        );
    }

    #[test]
    fn block_maps_clear_resets_bits() {
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let bm = BlockMaps::new(4, &lengths);

        // Set some bits.
        bm.mark_requested(0, 0);
        bm.mark_requested(0, 1);
        bm.mark_received(0, 0);

        // Clear piece 0.
        bm.clear(0, 2);

        // All bits should be zeroed.
        assert_eq!(bm.next_unrequested(0, 2), Some(0));
        assert!(!bm.all_received(0, 2));
        // mark_requested should succeed again (bit was cleared).
        assert!(!bm.mark_requested(0, 0));
    }

    #[test]
    fn block_maps_independent_pieces() {
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let bm = BlockMaps::new(4, &lengths);

        // Request all blocks in piece 0, none in piece 1.
        bm.mark_requested(0, 0);
        bm.mark_requested(0, 1);

        // Piece 0 fully requested, piece 1 untouched.
        assert_eq!(bm.next_unrequested(0, 2), None);
        assert_eq!(bm.next_unrequested(1, 2), Some(0));
    }

    // ---- StealCandidates tests ----

    #[test]
    fn steal_candidates_push_pop_fifo() {
        let sc = StealCandidates::new();

        sc.push(10);
        sc.push(20);
        sc.push(30);

        assert_eq!(sc.pop(), Some(10));
        assert_eq!(sc.pop(), Some(20));
        assert_eq!(sc.pop(), Some(30));
        assert_eq!(sc.pop(), None);
    }

    #[test]
    fn steal_candidates_remove() {
        let sc = StealCandidates::new();

        sc.push(5);
        sc.push(10);
        sc.push(15);

        // Remove middle element.
        sc.remove(10);

        assert_eq!(sc.pop(), Some(5));
        assert_eq!(sc.pop(), Some(15));
        assert_eq!(sc.pop(), None);
    }

    #[test]
    fn steal_candidates_remove_nonexistent() {
        let sc = StealCandidates::new();
        sc.push(42);

        // Removing a nonexistent element is a no-op.
        sc.remove(999);

        assert_eq!(sc.pop(), Some(42));
    }

    // ---- Phase 3 steal dispatch tests ----

    #[test]
    fn next_block_phase3_steal() {
        // Peer A reserves piece 0 (via Phase 2 CAS). Peer B has Phase 2 exhausted.
        // Peer B should enter Phase 3 and steal an unrequested block from piece 0.
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));

        // Peer A reserves piece 0 via CAS.
        assert!(states.try_reserve(0));

        // Set up block maps and mark block 0 of piece 0 as requested (peer A's work).
        let bm = Arc::new(BlockMaps::new(num_pieces, &lengths));
        bm.mark_requested(0, 0);

        // Put piece 0 in steal candidates.
        let sc = Arc::new(StealCandidates::new());
        sc.push(0);

        // Reserve all other pieces so peer B's Phase 2 is exhausted.
        assert!(states.try_reserve(1));
        assert!(states.try_reserve(2));
        assert!(states.try_reserve(3));

        // Build snapshot (all pieces are Reserved, so snapshot.order is empty).
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        assert!(
            snap.order.is_empty(),
            "all pieces reserved — snapshot should be empty"
        );

        let peer_bf = peer_has_all(num_pieces);

        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);
        ds.set_block_maps(Arc::clone(&bm));
        ds.set_steal_candidates(Arc::clone(&sc));

        // Phase 2 is exhausted (empty snapshot). Phase 3 should steal block 1 of piece 0.
        let stolen = ds.next_block(&peer_bf, &states);
        assert!(
            stolen.is_some(),
            "Phase 3 should find an unrequested block to steal"
        );
        let stolen = stolen.expect("just checked is_some");
        assert_eq!(stolen.piece, 0, "should steal from piece 0");
        assert_eq!(stolen.begin, 16384, "should steal block 1 (offset 16384)");

        // current_piece should NOT be set (stolen blocks are one-at-a-time).
        assert!(
            ds.current_piece_index().is_none(),
            "steal should not set current_piece"
        );
    }

    #[test]
    fn next_block_steal_skips_complete() {
        // Put a piece in steal_candidates. Mark it Complete.
        // Phase 3 should skip it and return None.
        let lengths = test_lengths();
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));

        // Mark piece 0 complete.
        states.mark_complete(0);

        let bm = Arc::new(BlockMaps::new(num_pieces, &lengths));
        let sc = Arc::new(StealCandidates::new());
        sc.push(0); // complete piece in the steal queue

        // Reserve other pieces to exhaust Phase 2.
        assert!(states.try_reserve(1));
        assert!(states.try_reserve(2));
        assert!(states.try_reserve(3));

        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));

        let peer_bf = peer_has_all(num_pieces);
        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);
        ds.set_block_maps(Arc::clone(&bm));
        ds.set_steal_candidates(Arc::clone(&sc));

        // Phase 3 should skip the complete piece.
        assert!(ds.next_block(&peer_bf, &states).is_none());

        // The complete piece should NOT have been put back.
        assert!(
            sc.pop().is_none(),
            "complete piece should not be returned to queue"
        );
    }

    #[test]
    fn next_block_steal_requires_peer_has() {
        // Put a piece in steal_candidates. Peer's bitfield does NOT have this piece.
        // Phase 3 should skip it and put it back for other peers.
        let lengths = test_lengths();
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));

        // Reserve piece 0 (so it's in Reserved state) and put it in steal queue.
        assert!(states.try_reserve(0));
        let bm = Arc::new(BlockMaps::new(num_pieces, &lengths));
        bm.mark_requested(0, 0); // simulate some blocks requested
        let sc = Arc::new(StealCandidates::new());
        sc.push(0);

        // Reserve other pieces to exhaust Phase 2.
        assert!(states.try_reserve(1));
        assert!(states.try_reserve(2));
        assert!(states.try_reserve(3));

        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));

        // Peer does NOT have piece 0.
        let mut peer_bf = Bitfield::new(num_pieces);
        peer_bf.set(1);
        peer_bf.set(2);
        peer_bf.set(3);
        // peer_bf does NOT have piece 0

        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);
        ds.set_block_maps(Arc::clone(&bm));
        ds.set_steal_candidates(Arc::clone(&sc));

        // Phase 3 should skip piece 0 (peer doesn't have it).
        assert!(ds.next_block(&peer_bf, &states).is_none());

        // Piece 0 should have been put back for other peers.
        assert_eq!(
            sc.pop(),
            Some(0),
            "piece should be returned to queue for other peers"
        );
    }

    #[test]
    fn next_block_phase1_with_block_maps() {
        // Verify Phase 1 hot path works correctly when block_maps is set —
        // mark_requested should register the block.
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let bm = Arc::new(BlockMaps::new(num_pieces, &lengths));
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        let peer_bf = peer_has_all(num_pieces);

        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);
        ds.set_block_maps(Arc::clone(&bm));

        // First call: Phase 2 cold path — reserves a piece, returns block 0.
        let b0 = ds
            .next_block(&peer_bf, &states)
            .expect("should get block 0");
        let piece = b0.piece;

        // Block 0 should be marked in block_maps (set by Phase 2).
        assert!(
            bm.mark_requested(piece, 0),
            "block 0 should already be marked requested by Phase 2"
        );

        // Second call: Phase 1 hot path — returns block 1.
        let b1 = ds
            .next_block(&peer_bf, &states)
            .expect("should get block 1");
        assert_eq!(b1.piece, piece);
        assert_eq!(b1.begin, 16384);

        // Block 1 should be marked in block_maps (set by Phase 1).
        assert!(
            bm.mark_requested(piece, 1),
            "block 1 should already be marked requested by Phase 1"
        );
    }

    #[test]
    fn next_block_phase2_with_block_maps() {
        // Verify Phase 2 cold path marks block 0 in block_maps.
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));
        let bm = Arc::new(BlockMaps::new(num_pieces, &lengths));
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        let peer_bf = peer_has_all(num_pieces);

        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);
        ds.set_block_maps(Arc::clone(&bm));

        // Phase 2 cold path: reserves first piece, returns block 0.
        let b0 = ds
            .next_block(&peer_bf, &states)
            .expect("should get block from Phase 2");
        let piece = b0.piece;
        assert_eq!(b0.begin, 0, "first block should be at offset 0");

        // Block 0 should already be marked in block_maps by Phase 2.
        let was_set = bm.mark_requested(piece, 0);
        assert!(
            was_set,
            "Phase 2 should have marked block 0 as requested in block_maps"
        );

        // Block 1 should NOT be marked yet (Phase 1 hasn't dispatched it).
        let was_set = bm.mark_requested(piece, 1);
        assert!(
            !was_set,
            "block 1 should not be marked yet — Phase 1 hasn't run for it"
        );
    }

    #[test]
    fn no_steal_when_disabled() {
        // Scenario: Phase 2 is exhausted, steal candidates exist in the swarm,
        // but this peer's PeerDispatchState has block_maps=None (simulating
        // use_block_stealing=false). Phase 3 should NOT activate.
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));

        // Reserve all pieces so Phase 2 is exhausted.
        assert!(states.try_reserve(0));
        assert!(states.try_reserve(1));
        assert!(states.try_reserve(2));
        assert!(states.try_reserve(3));

        // Build an empty snapshot (all pieces reserved).
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        assert!(
            snap.order.is_empty(),
            "all pieces reserved — snapshot should be empty"
        );

        let peer_bf = peer_has_all(num_pieces);

        // Construct PeerDispatchState WITHOUT block_maps or steal_candidates
        // (simulates use_block_stealing=false — torrent.rs passes None).
        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);
        // Explicitly do NOT call ds.set_block_maps() or ds.set_steal_candidates().

        // Phase 2 exhausted, no Phase 3 possible → should return None.
        assert!(
            ds.next_block(&peer_bf, &states).is_none(),
            "next_block must return None when block stealing is disabled (no block_maps)"
        );
    }

    // ---- M103 integration tests: block-stealing components working together ----

    /// 4 pieces, 4 blocks per piece (256 KiB total, 64 KiB pieces, 16 KiB chunks).
    fn test_lengths_4blocks() -> Lengths {
        Lengths::new(256 * 1024, 64 * 1024, 16384)
    }

    #[test]
    fn steal_completes_slow_peer_piece() {
        // Peer A reserves piece 0, dispatches blocks 0 and 1 (out of 4).
        // Peer B has Phase 2 exhausted and enters Phase 3 to steal a block.
        let lengths = test_lengths_4blocks(); // 4 pieces, 4 blocks each
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));

        // Peer A reserves piece 0 via CAS.
        assert!(states.try_reserve(0));

        // Set up block maps: blocks 0 and 1 of piece 0 are already requested by peer A.
        let bm = Arc::new(BlockMaps::new(num_pieces, &lengths));
        bm.mark_requested(0, 0);
        bm.mark_requested(0, 1);

        // Put piece 0 in steal candidates.
        let sc = Arc::new(StealCandidates::new());
        sc.push(0);

        // Reserve all other pieces so peer B's Phase 2 is exhausted.
        assert!(states.try_reserve(1));
        assert!(states.try_reserve(2));
        assert!(states.try_reserve(3));

        // Build empty snapshot (all pieces Reserved).
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        assert!(snap.order.is_empty());

        let peer_bf = peer_has_all(num_pieces);

        let mut ds_b = PeerDispatchState::new(Arc::clone(&snap), lengths);
        ds_b.set_block_maps(Arc::clone(&bm));
        ds_b.set_steal_candidates(Arc::clone(&sc));

        // Peer B should steal block 2 (the first unrequested block).
        let stolen = ds_b
            .next_block(&peer_bf, &states)
            .expect("Phase 3 should steal a block from piece 0");
        assert_eq!(stolen.piece, 0, "should steal from piece 0");
        assert_eq!(
            stolen.begin,
            2 * 16384,
            "should steal block 2 (offset 32768)"
        );

        // Block 2 should already be claimed by Phase 3 steal.
        assert!(
            bm.mark_requested(0, 2),
            "block 2 should already be claimed (Phase 3 stole it)"
        );
    }

    #[test]
    fn steal_coexists_with_owner() {
        // Peer A owns piece 0 (dispatches blocks sequentially).
        // Peer B steals block 2 from piece 0.
        // Peer A continues and dispatches block 1 via Phase 1 hot path.
        // Both peers contribute blocks — no conflict.
        let lengths = test_lengths_4blocks(); // 4 pieces, 4 blocks each
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));

        let bm = Arc::new(BlockMaps::new(num_pieces, &lengths));
        let sc = Arc::new(StealCandidates::new());
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));
        let peer_bf = peer_has_all(num_pieces);

        // Peer A: set up with block maps, dispatch block 0 (Phase 2 cold path).
        let mut ds_a = PeerDispatchState::new(Arc::clone(&snap), lengths.clone());
        ds_a.set_block_maps(Arc::clone(&bm));

        let a_block0 = ds_a
            .next_block(&peer_bf, &states)
            .expect("peer A should get block 0");
        let piece = a_block0.piece;
        assert_eq!(a_block0.begin, 0);

        // Mark piece as a steal candidate (simulating actor seeing it partially done).
        sc.push(piece);

        // Reserve remaining pieces so peer B's Phase 2 is exhausted.
        for p in 0..num_pieces {
            if p != piece {
                let _ = states.try_reserve(p);
            }
        }

        // Rebuild snapshot (all Reserved now).
        let snap2 = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            2,
        ));

        // Peer B: Phase 2 exhausted, enters Phase 3 and steals a block.
        let mut ds_b = PeerDispatchState::new(Arc::clone(&snap2), lengths.clone());
        ds_b.set_block_maps(Arc::clone(&bm));
        ds_b.set_steal_candidates(Arc::clone(&sc));

        let b_stolen = ds_b
            .next_block(&peer_bf, &states)
            .expect("peer B should steal a block");
        assert_eq!(b_stolen.piece, piece, "peer B should steal from same piece");

        // Peer A continues: Phase 1 hot path dispatches block 1.
        let a_block1 = ds_a
            .next_block(&peer_bf, &states)
            .expect("peer A should get block 1 via hot path");
        assert_eq!(a_block1.piece, piece, "peer A should continue same piece");
        assert_eq!(a_block1.begin, 16384, "peer A should get block 1");

        // Verify all three blocks are marked in block_maps.
        assert!(
            bm.mark_requested(piece, 0),
            "block 0 should already be claimed (peer A Phase 2)"
        );
        assert!(
            bm.mark_requested(piece, 1),
            "block 1 should already be claimed (peer A Phase 1)"
        );
        // The stolen block (either 2 or 3) should also be claimed.
        let stolen_block_idx = b_stolen.begin / 16384;
        assert!(
            bm.mark_requested(piece, stolen_block_idx),
            "stolen block should already be claimed (peer B Phase 3)"
        );
    }

    #[test]
    fn steal_handles_duplicate_claim() {
        // Two peers race to steal the same block. Only one should succeed
        // (the other sees was_already_set=true from fetch_or).
        let lengths = test_lengths_4blocks(); // 4 pieces, 4 blocks each
        let num_pieces = 4u32;
        let bm = Arc::new(BlockMaps::new(num_pieces, &lengths));

        // Mark blocks 0, 1, 2 as already requested. Block 3 is the only
        // unrequested block — both threads will race for it.
        bm.mark_requested(0, 0);
        bm.mark_requested(0, 1);
        bm.mark_requested(0, 2);

        // Two threads race on mark_requested for block 3.
        let results: Vec<bool> = std::thread::scope(|s| {
            let handles: Vec<_> = (0..2)
                .map(|_| {
                    let bm_ref = Arc::clone(&bm);
                    s.spawn(move || bm_ref.mark_requested(0, 3))
                })
                .collect();
            handles
                .into_iter()
                .map(|h| h.join().expect("thread panicked"))
                .collect()
        });

        // Exactly one thread should have won the race (was_already_set=false).
        let winners = results.iter().filter(|&&was_set| !was_set).count();
        let losers = results.iter().filter(|&&was_set| was_set).count();
        assert_eq!(winners, 1, "exactly one thread should claim the block");
        assert_eq!(
            losers, 1,
            "exactly one thread should see it already claimed"
        );

        // Block 3 must be marked as requested now.
        assert!(
            bm.mark_requested(0, 3),
            "block 3 should be marked after the race"
        );
    }

    #[test]
    fn block_maps_concurrent_access() {
        // Spawn N threads, each calling next_unrequested and then mark_requested
        // on the same piece. Verify no panics, data races, or incorrect results.
        let lengths = test_lengths_4blocks(); // 4 pieces, 4 blocks each
        let num_pieces = 4u32;
        let total_blocks = lengths.chunks_in_piece(0);
        let bm = Arc::new(BlockMaps::new(num_pieces, &lengths));

        // Spawn 8 threads, each trying to claim a block from piece 0.
        let claimed: Vec<Option<u32>> = std::thread::scope(|s| {
            let handles: Vec<_> = (0..8)
                .map(|_| {
                    let bm_ref = Arc::clone(&bm);
                    s.spawn(move || {
                        // Find an unrequested block and try to claim it.
                        if let Some(block_idx) = bm_ref.next_unrequested(0, total_blocks) {
                            let was_set = bm_ref.mark_requested(0, block_idx);
                            if !was_set {
                                return Some(block_idx);
                            }
                        }
                        None
                    })
                })
                .collect();
            handles
                .into_iter()
                .map(|h| h.join().expect("thread panicked"))
                .collect()
        });

        // Collect successful claims.
        let successful: Vec<u32> = claimed.into_iter().flatten().collect();

        // With 4 blocks and 8 threads, at most 4 unique claims are possible.
        assert!(
            successful.len() <= total_blocks as usize,
            "cannot claim more blocks than exist: got {} claims for {} blocks",
            successful.len(),
            total_blocks
        );

        // All claimed block indices must be unique and in-range.
        let unique: std::collections::HashSet<u32> = successful.iter().copied().collect();
        assert_eq!(
            unique.len(),
            successful.len(),
            "all claimed blocks must be unique"
        );
        for &idx in &successful {
            assert!(
                idx < total_blocks,
                "claimed block index {idx} out of range (total={total_blocks})"
            );
        }
    }

    #[test]
    fn endgame_fallback_when_steal_disabled() {
        // When block_maps is None (steal disabled), Endgame pieces should
        // still be dispatchable via Phase 2 (try_reserve returns true for
        // Endgame state).
        let lengths = test_lengths(); // 4 pieces, 2 blocks each
        let num_pieces = 4u32;
        let states = Arc::new(AtomicPieceStates::new(
            num_pieces,
            &Bitfield::new(num_pieces),
            &all_pieces_wanted(num_pieces),
        ));

        // Reserve piece 0 normally, then transition it to Endgame.
        assert!(states.try_reserve(0));
        states.transition_to_endgame(0);

        // Reserve pieces 1 and 2 so they're not available.
        assert!(states.try_reserve(1));
        assert!(states.try_reserve(2));

        // Leave piece 3 Available.

        // Build snapshot: includes piece 0 (Endgame) and piece 3 (Available).
        let availability = vec![1u32; num_pieces as usize];
        let snap = Arc::new(AvailabilitySnapshot::build(
            &availability,
            &states,
            &BTreeSet::new(),
            1,
        ));

        let peer_bf = peer_has_all(num_pieces);

        // Create PeerDispatchState WITHOUT block_maps (steal disabled).
        let mut ds = PeerDispatchState::new(Arc::clone(&snap), lengths);

        // Collect all dispatched pieces.
        let mut dispatched_pieces = std::collections::HashSet::new();
        while let Some(b) = ds.next_block(&peer_bf, &states) {
            dispatched_pieces.insert(b.piece);
        }

        // Should have dispatched from Endgame piece 0 and Available piece 3.
        assert!(
            dispatched_pieces.contains(&0),
            "Endgame piece 0 should be dispatched via Phase 2"
        );
        assert!(
            dispatched_pieces.contains(&3),
            "Available piece 3 should be dispatched via Phase 2"
        );
        assert_eq!(
            dispatched_pieces.len(),
            2,
            "only pieces 0 (Endgame) and 3 (Available) should be dispatched"
        );
    }

    // ── M128: PieceWriteGuards atomic ref count tests ───────────────────

    #[test]
    fn write_guard_increments_counter() {
        let guards = PieceWriteGuards::new(4);
        assert!(guards.is_idle(0));
        let _g = guards.begin_write(0);
        assert!(!guards.is_idle(0));
    }

    #[test]
    fn write_guard_drop_decrements() {
        let guards = PieceWriteGuards::new(4);
        {
            let _g = guards.begin_write(0);
        }
        assert!(guards.is_idle(0));
    }

    #[test]
    fn concurrent_writers_not_idle() {
        let guards = PieceWriteGuards::new(4);
        let g1 = guards.begin_write(0);
        let _g2 = guards.begin_write(0);
        drop(g1);
        assert!(!guards.is_idle(0), "still one writer active");
    }

    #[test]
    fn concurrent_writers_all_dropped_idle() {
        let guards = PieceWriteGuards::new(4);
        let g1 = guards.begin_write(0);
        let g2 = guards.begin_write(0);
        drop(g1);
        drop(g2);
        assert!(guards.is_idle(0));
    }

    #[test]
    fn steal_candidates_no_poison_after_panic() {
        // parking_lot Mutex does not poison — usable after a panic.
        let sc = StealCandidates::new();
        sc.push(42);

        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _guard = sc.inner.lock();
            panic!("intentional panic while holding lock");
        }));
        assert!(result.is_err(), "should have panicked");

        // Lock should still be usable after panic
        sc.push(99);
        assert_eq!(sc.pop(), Some(42));
        assert_eq!(sc.pop(), Some(99));
        assert_eq!(sc.pop(), None);
    }
}