olympipe-rs 1.1.0

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

const DEFAULT_BUFFER: usize = 4;

/// Most items placed in one channel message when the producer is not stalling.
///
/// A message is flushed earlier when the next input is not already available,
/// so a trickle source still travels one item at a time.
const MAX_CHUNK: usize = 64;

/// A source pull at least this slow ends packet coalescing.
///
/// Tight iterators (`0..n`) return in nanoseconds and stay coalesced. The
/// trickle benchmark sleeps 200µs inside `next`, which flushes the current
/// item before the following sleep. One clock read per full packet, not per item.
const SLOW_PULL: Duration = Duration::from_micros(64);

/// Items per channel message for this buffer size.
///
/// The channel holds `buffer_size` messages. Buffers of 1 or 2 stay at one
/// item per message so a stalled consumer stops the producer within a few
/// items. Larger buffers coalesce up to [`MAX_CHUNK`] items per message, and
/// therefore retain at most `buffer_size * MAX_CHUNK` items between stages.
fn chunk_limit(buffer_size: usize) -> usize {
    if buffer_size <= 2 { 1 } else { MAX_CHUNK }
}

/// Published by [`FeederRelease`] when the pipeline is first used or dropped.
///
/// The feeder thread is spawned in [`Pipeline::new`] / [`Pipeline::new_with_buffer`]
/// but waits here, so [`Pipeline::with_max_packet`] can still change its packet
/// size. Without the wait, a fast source would fill the default 64-item packets
/// before the caller could opt out.
struct FeederGate {
    limit: Mutex<Option<usize>>,
    cv: Condvar,
}

impl FeederGate {
    fn new() -> Arc<Self> {
        Arc::new(Self {
            limit: Mutex::new(None),
            cv: Condvar::new(),
        })
    }

    fn open(&self, limit: usize) {
        let mut guard = lock(&self.limit);
        if guard.is_none() {
            *guard = Some(limit);
            self.cv.notify_one();
        }
    }

    fn wait(&self) -> usize {
        let mut guard = lock(&self.limit);
        while guard.is_none() {
            guard = self.cv.wait(guard).unwrap_or_else(|err| err.into_inner());
        }
        (*guard).expect("packet size is published before wait returns")
    }
}

/// Opens [`FeederGate`] with `limit` on drop.
struct FeederRelease {
    gate: Arc<FeederGate>,
    limit: usize,
}

impl Drop for FeederRelease {
    fn drop(&mut self) {
        self.gate.open(self.limit);
    }
}

/// Failure of a running pipeline.
///
/// The first failure is kept; later ones are ignored. Workers stop pulling new
/// items once a failure is recorded, and terminal methods surface it.
///
/// [`Pipeline::task_or`] is the per-item callback form: an `Err` from its
/// closure does not produce a [`PipelineError`]. [`Pipeline::try_task`] in
/// [`ErrorMode::FailFast`] records [`PipelineError::TaskFailed`] instead.
/// [`ErrorMode::Skip`], [`ErrorMode::SkipAndLog`], and [`ErrorMode::Replace`]
/// absorb both `Err` and, unlike `task_or`, a panic raised on that item.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum PipelineError {
    /// A worker thread panicked. `stage` names the stage that failed
    /// (`"task"`, `"filter"`, `"split"`, `"reduce"`, `"checkpoint"`, `"feeder"`, …).
    /// `message` is the panic payload (`&str` or `String`).
    #[error("worker panicked in stage {stage}: {message}")]
    WorkerPanicked {
        /// Stage that panicked.
        stage: &'static str,
        /// Panic payload, or `"(unknown panic)"` when it was neither `&str` nor `String`.
        message: String,
    },
    /// [`Pipeline::try_task`] returned `Err` while in [`ErrorMode::FailFast`].
    ///
    /// `stage` is `"try_task"`. `message` is the error's [`Display`](std::fmt::Display)
    /// text. The pipeline is cancelled, the same way a panic is.
    ///
    /// ```
    /// use olympipe_rs::{ErrorMode, Pipeline, PipelineError};
    ///
    /// let err = Pipeline::new(vec![1, -1])
    ///     .try_task(
    ///         |x| if x < 0 { Err("negative") } else { Ok(x) },
    ///         1,
    ///         ErrorMode::FailFast,
    ///     )
    ///     .try_collect()
    ///     .unwrap_err();
    /// match err {
    ///     PipelineError::TaskFailed { stage, message } => {
    ///         assert_eq!(stage, "try_task");
    ///         assert_eq!(message, "negative");
    ///     }
    ///     other => panic!("unexpected {other:?}"),
    /// }
    /// ```
    #[error("task failed in stage {stage}: {message}")]
    TaskFailed {
        /// Stage that returned the error. [`Pipeline::try_task`] uses `"try_task"`.
        /// `cached_task` (feature `cache`) uses `"cached_task"` when its input
        /// cannot be serialized.
        stage: &'static str,
        /// `Display` text of the `Err` value.
        message: String,
    },
    /// [`Pipeline::timeout`] elapsed between two items.
    #[error("timeout after {0:?}")]
    Timeout(Duration),
}

/// What [`Pipeline::try_task`] and [`Pipeline::task_with_mode`] do with a
/// per-item failure.
///
/// [`ErrorMode::FailFast`] is the default. New modes may be added later.
///
/// ```
/// use olympipe_rs::ErrorMode;
///
/// let mode: ErrorMode<i32> = ErrorMode::default();
/// assert!(matches!(mode, ErrorMode::FailFast));
/// let _replaced = ErrorMode::Replace(0);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum ErrorMode<U> {
    /// Stop the pipeline on the first failure.
    ///
    /// For [`Pipeline::try_task`], the first `Err` becomes
    /// [`PipelineError::TaskFailed`] and a panic stays
    /// [`PipelineError::WorkerPanicked`] (nothing is caught). For
    /// [`Pipeline::task_with_mode`], this is [`Pipeline::task`]: a panic fails
    /// the pipeline with stage `"task"`.
    #[default]
    FailFast,
    /// Drop the failed item and keep going.
    Skip,
    /// Drop the failed item, write one line to stderr, and keep going.
    ///
    /// The line is `olympipe-rs: [try_task] skipped item: {err}` or
    /// `olympipe-rs: [task_with_mode] skipped item: {err}`. `{err}` is the
    /// `Display` text of a returned error, or the panic payload.
    SkipAndLog,
    /// Emit a clone of this value in place of the failed item.
    ///
    /// `U: Clone` is required by the methods that accept a mode, because each
    /// failure clones `U` again.
    Replace(U),
}

#[derive(Default)]
struct FinishState {
    started: bool,
    done: bool,
    error: Option<PipelineError>,
}

/// Handles, the first recorded failure, and the cancellation flag shared by
/// every stage of a pipeline graph (both branches of a `split` share one).
struct Shared {
    handles: Mutex<Vec<JoinHandle<()>>>,
    error: Mutex<Option<PipelineError>>,
    /// Cheap per-item flag. Set after the first failure; workers load it with
    /// `Relaxed` ordering and leave their loop.
    cancelled: AtomicBool,
    /// Gathered pipelines. `Weak` so the link is not a reference cycle.
    peers: Mutex<Vec<Weak<Shared>>>,
    /// Strong refs to gathered sources, held until shutdown has read their error.
    retained: Mutex<Vec<Arc<Shared>>>,
    /// Two terminals (the two branches of a split) may finish concurrently.
    /// Exactly one joins the handles; the other waits here so it cannot miss
    /// an error that is still being recorded, and so no handle is joined twice.
    finish: Mutex<FinishState>,
    finish_cv: Condvar,
}

fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    mutex.lock().unwrap_or_else(|err| err.into_inner())
}

fn panic_message(payload: &(dyn Any + Send)) -> String {
    if let Some(msg) = payload.downcast_ref::<&str>() {
        return (*msg).to_owned();
    }
    if let Some(msg) = payload.downcast_ref::<String>() {
        return msg.clone();
    }
    "(unknown panic)".to_owned()
}

/// One channel message. `One` is the trickle / tight-backpressure path and
/// does not allocate. `Many` is a burst the producer already had in hand.
pub(crate) enum Packet<T> {
    One(T),
    Many(Vec<T>),
}

/// Outcome of pulling one burst from an input channel.
#[derive(PartialEq, Eq)]
enum Step {
    /// Input would now block. Flush partial output so latency does not wait
    /// for a full packet.
    Yield,
    /// Item callback asked to stop (limit reached, or a send failed).
    Halted,
    /// Upstream disconnected.
    Closed,
    /// Cancellation flag observed. Drop partial aggregates.
    Cancelled,
}

/// Coalesces items into [`Packet`]s. `push` sends a full packet; `flush`
/// sends a partial one (or nothing).
struct BatchTx<T> {
    tx: Sender<Packet<T>>,
    buf: Vec<T>,
    limit: usize,
}

impl<T> BatchTx<T> {
    fn new(tx: Sender<Packet<T>>, limit: usize) -> Self {
        let limit = limit.max(1);
        Self {
            tx,
            buf: Vec::with_capacity(limit.min(MAX_CHUNK)),
            limit,
        }
    }

    fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    /// `false` when the downstream channel is disconnected.
    #[inline]
    fn push(&mut self, item: T) -> bool {
        self.buf.push(item);
        if self.buf.len() >= self.limit {
            self.flush()
        } else {
            true
        }
    }

    #[inline]
    fn flush(&mut self) -> bool {
        if self.buf.is_empty() {
            return true;
        }
        let packet = if self.buf.len() == 1 {
            Packet::One(self.buf.pop().expect("len checked"))
        } else {
            Packet::Many(std::mem::replace(
                &mut self.buf,
                Vec::with_capacity(self.limit.min(MAX_CHUNK)),
            ))
        };
        self.tx.send(packet).is_ok()
    }
}

/// Apply `on_item` to every element. `false` means the callback rejected an
/// item or the pipeline was cancelled between items.
#[inline]
fn feed_packet<T>(packet: Packet<T>, shared: &Shared, on_item: &mut impl FnMut(T) -> bool) -> bool {
    match packet {
        Packet::One(item) => !shared.is_cancelled() && on_item(item),
        Packet::Many(items) => {
            for item in items {
                if shared.is_cancelled() || !on_item(item) {
                    return false;
                }
            }
            true
        }
    }
}

/// Blocking receive of one packet, then (when `drain`) every packet already
/// queued. Returns once the next receive would block, so the caller can flush.
fn pull<T>(
    rx: &Receiver<Packet<T>>,
    shared: &Shared,
    drain: bool,
    mut on_item: impl FnMut(T) -> bool,
) -> Step {
    if shared.is_cancelled() {
        return Step::Cancelled;
    }
    let packet = match rx.recv() {
        Ok(packet) => packet,
        Err(_) => return Step::Closed,
    };
    if !feed_packet(packet, shared, &mut on_item) {
        return if shared.is_cancelled() {
            Step::Cancelled
        } else {
            Step::Halted
        };
    }
    if drain {
        loop {
            match rx.try_recv() {
                Ok(packet) => {
                    if !feed_packet(packet, shared, &mut on_item) {
                        return if shared.is_cancelled() {
                            Step::Cancelled
                        } else {
                            Step::Halted
                        };
                    }
                }
                Err(TryRecvError::Empty) => break,
                Err(TryRecvError::Disconnected) => return Step::Closed,
            }
        }
    }
    if shared.is_cancelled() {
        Step::Cancelled
    } else {
        Step::Yield
    }
}

fn map_out<T, U>(
    rx: Receiver<Packet<T>>,
    tx: Sender<Packet<U>>,
    shared: &Shared,
    limit: usize,
    drain: bool,
    mut on_item: impl FnMut(T, &mut BatchTx<U>) -> bool,
) {
    let mut out = BatchTx::new(tx, limit);
    // Coalesce only after two fast callbacks with no idle gap. A slow
    // callback (the timeout bench sleeps inside `task`) is never entered
    // while the previous result is still unsent, and a trickle gap resets
    // the run so each item is published on its own.
    let mut hold = false;
    let mut prev_fast = false;
    loop {
        let step = pull(&rx, shared, drain, |item| {
            let started = if hold { None } else { Some(Instant::now()) };
            if !on_item(item, &mut out) {
                return false;
            }
            let Some(started) = started else {
                return true;
            };
            let fast = started.elapsed() < SLOW_PULL;
            if !fast {
                prev_fast = false;
                hold = false;
                return out.flush();
            }
            if prev_fast {
                hold = true;
            }
            prev_fast = true;
            hold || out.flush()
        });
        match step {
            Step::Yield => {
                hold = false;
                prev_fast = false;
                if !out.flush() {
                    break;
                }
            }
            Step::Closed | Step::Halted => {
                if !shared.is_cancelled() {
                    let _ = out.flush();
                }
                break;
            }
            Step::Cancelled => break,
        }
    }
}

/// Feeder: coalesce only after two fast pulls in a row, and only while the
/// buffer is non-empty. A slow `next` (trickle) is always shipped alone,
/// before the following pull can block.
fn feed<I, T>(iter: I, tx: Sender<Packet<T>>, shared: &Shared, limit: usize)
where
    I: IntoIterator<Item = T>,
{
    let mut iter = iter.into_iter();
    if limit <= 1 {
        for item in iter {
            if shared.is_cancelled() || tx.send(Packet::One(item)).is_err() {
                break;
            }
        }
        return;
    }
    let mut out = BatchTx::new(tx, limit);
    let mut prev_fast = false;
    let mut hold = false;
    loop {
        if shared.is_cancelled() {
            break;
        }
        let time_this = out.is_empty();
        let started = time_this.then(Instant::now);
        let Some(item) = iter.next() else {
            break;
        };
        if let Some(started) = started {
            let fast = started.elapsed() < SLOW_PULL;
            if !fast {
                prev_fast = false;
                hold = false;
                if !out.push(item) || !out.flush() {
                    break;
                }
                continue;
            }
            if prev_fast {
                hold = true;
            }
            prev_fast = true;
            if !out.push(item) {
                break;
            }
            if !hold && !out.flush() {
                break;
            }
        } else if !out.push(item) {
            break;
        }
    }
    let _ = out.flush();
}

impl Shared {
    fn new() -> Arc<Self> {
        Arc::new(Self {
            handles: Mutex::new(Vec::new()),
            error: Mutex::new(None),
            cancelled: AtomicBool::new(false),
            peers: Mutex::new(Vec::new()),
            retained: Mutex::new(Vec::new()),
            finish: Mutex::new(FinishState::default()),
            finish_cv: Condvar::new(),
        })
    }

    #[inline]
    fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::Relaxed)
    }

    fn spawn(self: &Arc<Self>, stage: &'static str, body: impl FnOnce() + Send + 'static) {
        let shared = Arc::clone(self);
        let handle = thread::Builder::new()
            .name(format!("olympipe-{stage}"))
            .spawn(move || {
                if let Err(payload) = panic::catch_unwind(AssertUnwindSafe(body)) {
                    shared.fail(PipelineError::WorkerPanicked {
                        stage,
                        message: panic_message(&*payload),
                    });
                }
            })
            .unwrap_or_else(|_| panic!("olympipe-rs failed to spawn {stage} worker"));
        lock(&self.handles).push(handle);
    }

    fn fail(self: &Arc<Self>, err: PipelineError) {
        self.distribute(&err);
        self.cancel_all();
    }

    /// Record `err` on this node and on linked graphs, without overwriting an
    /// error that was stored earlier.
    fn distribute(self: &Arc<Self>, err: &PipelineError) {
        let mut seen = HashSet::<*const Self>::new();
        let mut stack = vec![Arc::clone(self)];
        while let Some(node) = stack.pop() {
            if !seen.insert(Arc::as_ptr(&node)) {
                continue;
            }
            {
                let mut slot = lock(&node.error);
                if slot.is_none() {
                    *slot = Some(err.clone());
                }
            }
            stack.extend(live_peers(&node));
        }
    }

    fn cancel_all(self: &Arc<Self>) {
        let mut seen = HashSet::<*const Self>::new();
        let mut stack = vec![Arc::clone(self)];
        while let Some(node) = stack.pop() {
            if !seen.insert(Arc::as_ptr(&node)) {
                continue;
            }
            node.cancelled.store(true, Ordering::Relaxed);
            stack.extend(live_peers(&node));
        }
    }

    /// `parent` is the gathered pipeline, `child` is a source consumed by it.
    fn link(parent: &Arc<Self>, child: &Arc<Self>) {
        if Arc::ptr_eq(parent, child) {
            return;
        }
        // Strong on the parent only: the child stays alive until shutdown, and
        // the back-edge is weak so an abandoned gather does not leak.
        lock(&parent.retained).push(Arc::clone(child));
        lock(&parent.peers).push(Arc::downgrade(child));
        lock(&child.peers).push(Arc::downgrade(parent));
        // A source may already have failed before it was gathered.
        if let Some(err) = stored_error(child) {
            parent.distribute(&err);
        }
        if let Some(err) = stored_error(parent) {
            child.distribute(&err);
        }
        if parent.is_cancelled() || child.is_cancelled() {
            parent.cancel_all();
        }
    }

    fn join_handles(&self) {
        let handles = {
            let mut guard = lock(&self.handles);
            std::mem::take(&mut *guard)
        };
        for handle in handles {
            if let Err(payload) = handle.join() {
                let mut slot = lock(&self.error);
                if slot.is_none() {
                    *slot = Some(PipelineError::WorkerPanicked {
                        stage: "worker",
                        message: panic_message(&*payload),
                    });
                }
                self.cancelled.store(true, Ordering::Relaxed);
            }
        }
    }

    /// Join every worker exactly once, then return the first error.
    ///
    /// The handle mutex is not held while joining: the other branch of a split
    /// may be blocked in its own terminal, and a joined thread must be able to
    /// finish without taking that lock. The second terminal waits until the
    /// joiner has published the error, so it cannot observe a success that has
    /// not been joined yet.
    fn shutdown(self: &Arc<Self>) -> Option<PipelineError> {
        {
            let mut guard = lock(&self.finish);
            if guard.done {
                return guard.error.clone();
            }
            if guard.started {
                while !guard.done {
                    guard = self
                        .finish_cv
                        .wait(guard)
                        .unwrap_or_else(|err| err.into_inner());
                }
                return guard.error.clone();
            }
            guard.started = true;
        }

        // Publish even if joining panics, so the waiting terminal cannot block
        // forever on `finish_cv`.
        struct Publish<'a> {
            shared: &'a Shared,
            error: Option<PipelineError>,
        }
        impl Drop for Publish<'_> {
            fn drop(&mut self) {
                let mut guard = lock(&self.shared.finish);
                if !guard.done {
                    guard.error = self.error.take();
                    guard.done = true;
                    self.shared.finish_cv.notify_all();
                }
            }
        }

        let mut publish = Publish {
            shared: self,
            error: None,
        };
        publish.error = self.join_graph();
        let error = publish.error.clone();
        drop(publish);
        error
    }

    fn join_graph(self: &Arc<Self>) -> Option<PipelineError> {
        self.join_handles();
        let peers = live_peers(self);
        for peer in &peers {
            peer.join_handles();
        }
        let error = first_error(self);
        lock(&self.retained).clear();
        lock(&self.peers).clear();
        error
    }
}

fn live_peers(shared: &Shared) -> Vec<Arc<Shared>> {
    lock(&shared.peers)
        .iter()
        .filter_map(Weak::upgrade)
        .collect()
}

fn stored_error(shared: &Shared) -> Option<PipelineError> {
    lock(&shared.error).clone()
}

fn first_error(root: &Shared) -> Option<PipelineError> {
    if let Some(err) = stored_error(root) {
        return Some(err);
    }
    let mut seen = HashSet::<*const Shared>::from([root as *const Shared]);
    let mut stack = live_peers(root);
    while let Some(node) = stack.pop() {
        if !seen.insert(Arc::as_ptr(&node)) {
            continue;
        }
        if let Some(err) = stored_error(&node) {
            return Some(err);
        }
        stack.extend(live_peers(&node));
    }
    None
}

/// A parallel data pipeline backed by bounded crossbeam channels and worker threads.
///
/// Each stage method consumes the pipeline and returns a new one whose type
/// reflects the output of that stage. Worker threads share an internal state:
/// their [`JoinHandle`]s, the first [`PipelineError`] (if any), and a
/// cancellation flag. A panic in any worker — including the feeder that drains
/// the source iterator — or a fired [`Pipeline::timeout`] records that error,
/// cancels the graph, and makes every stage stop. [`Pipeline::try_collect`],
/// [`Pipeline::try_for_each`], and [`Pipeline::try_wait_for_completion`] return
/// the error; [`Pipeline::collect`], [`Pipeline::for_each`], and
/// [`Pipeline::wait_for_completion`] panic with it. The same split applies to
/// [`Pipeline::try_wait_for_results`], [`Pipeline::try_wait_for_all_completions`],
/// [`Pipeline::try_wait_and_reduce`], and [`try_collect_pair`]: those drain every
/// pipeline concurrently, because bounded [`Pipeline::split`] branches deadlock
/// if one side is read to completion before the other starts.
///
/// Per-item `Result` failures stay on [`Pipeline::task_or`] (the callback sees
/// the item) and on [`Pipeline::try_task`] / [`Pipeline::task_with_mode`]. Only
/// [`ErrorMode::FailFast`] cancels the pipeline; `try_task` then records
/// [`PipelineError::TaskFailed`]. The other modes also catch a panic raised for
/// that single item.
///
/// # Channels and back-pressure
///
/// Each inter-stage channel holds `buffer_size` messages (default 4). A
/// message is one item when `buffer_size` is 1 or 2, or when
/// [`Pipeline::with_max_packet`]`(1)` is set. Otherwise the feeder packs up to
/// 64 items that are already available into one message, and flushes a partial
/// message as soon as the next source item is not ready. Downstream stages do
/// the same: they forward a partial packet when the next input would block, so
/// trickle and startup latency stay one item deep.
///
/// At most `buffer_size` items are buffered for a one-item packet, and at most
/// `buffer_size * packet` items otherwise (`packet` defaults to 64). A slow
/// consumer still stalls the producer; the stall just happens on packet
/// boundaries.
///
/// The feeder thread is created in [`Pipeline::new`] but does not pull the
/// source until the first stage or terminal method (or until the pipeline is
/// dropped). [`Pipeline::with_max_packet`] called before that also caps the
/// feeder. After the feeder has started, it keeps the packet size it was given
/// and a later `with_max_packet` only affects stages added afterwards.
///
/// Worker threads are named `olympipe-<stage>` (`olympipe-task`,
/// `olympipe-feeder`, …). A single worker preserves input order. `task`
/// with more than one worker does not.
pub struct Pipeline<T: Send + 'static> {
    /// `Some` until the feeder is allowed to pull. Dropped first so a discarded
    /// pipeline unblocks the waiting feeder before the receiver disconnects.
    pending_release: Option<FeederRelease>,
    pub(crate) receiver: Receiver<Packet<T>>,
    shared: Arc<Shared>,
    pub(crate) buffer_size: usize,
    chunk_limit: usize,
}

fn downstream<T: Send + 'static>(
    receiver: Receiver<Packet<T>>,
    shared: Arc<Shared>,
    buffer_size: usize,
    chunk_limit: usize,
) -> Pipeline<T> {
    Pipeline {
        pending_release: None,
        receiver,
        shared,
        buffer_size,
        chunk_limit,
    }
}

// ─── constructor ──────────────────────────────────────────────────────────────

impl<T: Send + 'static> Pipeline<T> {
    /// Create a pipeline from any iterator.  A feeder thread is spawned
    /// immediately to push items into the first bounded channel.
    ///
    /// A panic while pulling the iterator fails the pipeline with stage
    /// `"feeder"`.
    pub fn new(iter: impl IntoIterator<Item = T> + Send + 'static) -> Self {
        Self::new_with_buffer(iter, DEFAULT_BUFFER)
    }

    /// Like [`Self::new`], but sets the feeder and subsequent stage channel capacity.
    ///
    /// `buffer_size` is the number of **messages** in each channel, not the
    /// number of items. For 1 or 2 that is the same thing (one item per
    /// message). Above that, a message holds up to 64 items that the producer
    /// already has, so a full channel retains at most `buffer_size * 64` items.
    /// [`Self::with_max_packet`]`(1)` opts back into one item per message.
    /// See the [channel notes](Pipeline#channels-and-back-pressure).
    ///
    /// The default packet size stays 64 whenever `buffer_size > 2`, including
    /// when a later [`Self::with_buffer`] picks a larger buffer. The feeder
    /// channel created here is not resized afterwards.
    pub fn new_with_buffer(
        iter: impl IntoIterator<Item = T> + Send + 'static,
        buffer_size: usize,
    ) -> Self {
        let buffer_size = buffer_size.max(1);
        let limit = chunk_limit(buffer_size);
        let (tx, rx) = bounded(buffer_size);
        let shared = Shared::new();
        let gate = FeederGate::new();
        let gate_feeder = Arc::clone(&gate);
        let shared_feeder = Arc::clone(&shared);
        Shared::spawn(&shared, "feeder", move || {
            let limit = gate_feeder.wait();
            feed(iter, tx, &shared_feeder, limit);
        });
        Pipeline {
            pending_release: Some(FeederRelease { gate, limit }),
            receiver: rx,
            shared,
            buffer_size,
            chunk_limit: limit,
        }
    }

    /// Override the inter-stage channel capacity for *subsequent* stages
    /// (default: 4). Prefer [`Self::new_with_buffer`] if the feeder channel
    /// must also use that capacity.
    ///
    /// The same packet rules as [`Self::new_with_buffer`] apply: capacity is a
    /// message count, and messages grow past one item only when `size > 2`.
    /// This resets the packet size of subsequent stages to that default, so
    /// call [`Self::with_max_packet`] after `with_buffer` when both are set.
    /// It does not change the feeder's channel capacity or packet size.
    pub fn with_buffer(mut self, size: usize) -> Self {
        self.buffer_size = size.max(1);
        self.chunk_limit = chunk_limit(self.buffer_size);
        self
    }

    /// Cap items per channel message for stages added after this call.
    ///
    /// `n` is clamped to `1..=64`. [`Self::with_max_packet`]`(1)` restores
    /// strict per-item messages: each channel holds at most `buffer_size`
    /// items, which is the behaviour from before packets carried 64 items.
    ///
    /// The feeder has not pulled the source yet on a pipeline fresh from
    /// [`Self::new`] or [`Self::new_with_buffer`], so this also sets the
    /// feeder's packet size.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let values = Pipeline::new(0..10)
    ///     .with_max_packet(1)
    ///     .task(|x: i32| x + 1, 1)
    ///     .collect();
    /// assert_eq!(values, (1..11).collect::<Vec<_>>());
    /// ```
    ///
    /// After the first stage or terminal method the feeder is already pulling
    /// and keeps the packet size it started with. A later `with_max_packet`
    /// only affects stages added from then on, the same way as
    /// [`Self::with_buffer`]. The feeder channel itself is fixed at
    /// construction and holds at most `buffer_size * packet` items.
    ///
    /// For large items (images, tensors), call this before the stages that
    /// move them, typically `.with_max_packet(1)` so a default channel keeps
    /// 4 items instead of 256.
    pub fn with_max_packet(mut self, n: usize) -> Self {
        let n = n.clamp(1, MAX_CHUNK);
        self.chunk_limit = n;
        if let Some(release) = self.pending_release.as_mut() {
            release.limit = n;
        }
        self
    }
}

// ─── internal helpers ─────────────────────────────────────────────────────────

impl<T: Send + 'static> Pipeline<T> {
    /// Let the feeder pull. No-op once a stage or terminal has already done so.
    fn activate(&mut self) {
        // Drop publishes `FeederRelease::limit` and wakes the feeder.
        self.pending_release.take();
    }

    /// Spawn `count` worker threads that each read from `self.receiver` and
    /// write to a fresh bounded channel.  `worker` must be `Clone` so it can
    /// be duplicated per thread.  Use `spawn_single` for stages that run
    /// exactly one thread and whose closure cannot be cloned.
    fn spawn_stage<U, W>(mut self, stage: &'static str, count: usize, worker: W) -> Pipeline<U>
    where
        U: Send + 'static,
        W: Fn(Receiver<Packet<T>>, Sender<Packet<U>>, Arc<Shared>) + Send + Clone + 'static,
    {
        self.activate();
        let (tx, rx) = bounded::<Packet<U>>(self.buffer_size);
        let shared = Arc::clone(&self.shared);
        for _ in 0..count {
            let worker = worker.clone();
            let in_rx = self.receiver.clone();
            let out_tx = tx.clone();
            let shared_worker = Arc::clone(&shared);
            Shared::spawn(&shared, stage, move || {
                worker(in_rx, out_tx, shared_worker);
            });
        }
        drop(tx);
        downstream(rx, shared, self.buffer_size, self.chunk_limit)
    }

    /// Like `spawn_stage` but for a single worker thread whose closure does
    /// not need to implement `Clone`.
    fn spawn_single<U, W>(mut self, stage: &'static str, worker: W) -> Pipeline<U>
    where
        U: Send + 'static,
        W: FnOnce(Receiver<Packet<T>>, Sender<Packet<U>>, Arc<Shared>) + Send + 'static,
    {
        self.activate();
        let (tx, rx) = bounded::<Packet<U>>(self.buffer_size);
        let shared = Arc::clone(&self.shared);
        let in_rx = self.receiver;
        let shared_worker = Arc::clone(&shared);
        let buffer_size = self.buffer_size;
        let chunk_limit = self.chunk_limit;
        Shared::spawn(&shared, stage, move || worker(in_rx, tx, shared_worker));
        downstream(rx, shared, buffer_size, chunk_limit)
    }

    /// Drain `self`, drop the receiver so upstream senders unblock, join every
    /// worker, and return the first recorded error.
    fn finish<R>(
        mut self,
        drain: impl FnOnce(&Receiver<Packet<T>>, &Shared) -> R,
    ) -> Result<R, PipelineError> {
        self.activate();
        let output = drain(&self.receiver, &self.shared);
        let shared = Arc::clone(&self.shared);
        drop(self);
        match shared.shutdown() {
            Some(err) => Err(err),
            None => Ok(output),
        }
    }
}

// ─── stage methods ────────────────────────────────────────────────────────────

impl<T: Send + 'static> Pipeline<T> {
    /// Map each item through `f`.  `count` worker threads run concurrently,
    /// sharing the input channel (MPMC) and writing to a shared output channel.
    ///
    /// A `count` of zero runs one worker. One worker preserves input order;
    /// several workers do not (each takes a whole packet).
    ///
    /// A panic in `f` fails the pipeline (stage `"task"`).
    pub fn task<U, F>(self, f: F, count: usize) -> Pipeline<U>
    where
        U: Send + 'static,
        F: Fn(T) -> U + Send + Clone + 'static,
    {
        let count = count.max(1);
        // One worker may drain every packet already queued. Several workers
        // take one packet each so a large packet is not stolen from the others.
        let drain = count == 1;
        let limit = self.chunk_limit;
        self.spawn_stage("task", count, move |rx, tx, shared| {
            map_out(rx, tx, &shared, limit, drain, |item, out| {
                if shared.is_cancelled() {
                    return false;
                }
                let mapped = f(item);
                !shared.is_cancelled() && out.push(mapped)
            });
        })
    }

    /// Map each item through `method` on a worker-local value built by `init`.
    ///
    /// `init` runs once on each worker thread, after the thread has started, so
    /// `S` does not have to be [`Send`]. `method` borrows that state mutably
    /// for every item the worker receives. `count == 0` runs one worker. One
    /// worker preserves input order; several workers do not (each takes a whole
    /// packet), same as [`Pipeline::task`].
    ///
    /// The state is dropped at the end of the worker. Use
    /// [`Pipeline::class_task_with_close`] when that drop is not enough.
    /// A panic in `init` or `method` fails the pipeline with stage `"class_task"`.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let values = Pipeline::new(1..=3)
    ///     .class_task(
    ///         || 0,
    ///         |state, x: i32| {
    ///             *state += x;
    ///             *state
    ///         },
    ///         1,
    ///     )
    ///     .collect();
    /// assert_eq!(values, vec![1, 3, 6]);
    /// ```
    pub fn class_task<S, U, I, M>(self, init: I, method: M, count: usize) -> Pipeline<U>
    where
        S: 'static,
        U: Send + 'static,
        I: Fn() -> S + Send + Clone + 'static,
        M: Fn(&mut S, T) -> U + Send + Clone + 'static,
    {
        self.class_task_with_close(init, method, |_| {}, count)
    }

    /// [`Pipeline::class_task`] plus a hook invoked with the state when the
    /// worker stops.
    ///
    /// `close` is [`Fn`], not [`FnOnce`], so it can be cloned onto each worker
    /// (the stage body passed to the thread pool is itself [`Fn`]). It is
    /// called once per worker, on that worker's thread, after the worker has
    /// stopped pulling, in every case where the worker returns normally:
    ///
    /// * the input was exhausted;
    /// * the output channel disconnected (the downstream stage stopped reading,
    ///   for example [`Pipeline::limit`], or the branch was dropped);
    /// * the pipeline was cancelled (a failure in this stage's `method` is not
    ///   this case — that panics — but a failure elsewhere in the graph is).
    ///
    /// `close` is not called if `init` or `method` panics: the panic unwinds
    /// out of the worker first, and [`Drop`] of `S` still runs during that
    /// unwind when `init` had already returned. A panic inside `close` fails
    /// the pipeline with stage `"class_task"` after `close` has started.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    /// use std::sync::atomic::{AtomicUsize, Ordering};
    /// use std::sync::Arc;
    ///
    /// let closed = Arc::new(AtomicUsize::new(0));
    /// let flag = Arc::clone(&closed);
    /// let values = Pipeline::new(vec![1, 2, 3])
    ///     .class_task_with_close(
    ///         || 0,
    ///         |state, x: i32| {
    ///             *state += x;
    ///             *state
    ///         },
    ///         move |state| {
    ///             flag.store(state as usize, Ordering::Relaxed);
    ///         },
    ///         1,
    ///     )
    ///     .collect();
    /// assert_eq!(values, vec![1, 3, 6]);
    /// assert_eq!(closed.load(Ordering::Relaxed), 6);
    /// ```
    pub fn class_task_with_close<S, U, I, M, C>(
        self,
        init: I,
        method: M,
        close: C,
        count: usize,
    ) -> Pipeline<U>
    where
        S: 'static,
        U: Send + 'static,
        I: Fn() -> S + Send + Clone + 'static,
        M: Fn(&mut S, T) -> U + Send + Clone + 'static,
        C: Fn(S) + Send + Clone + 'static,
    {
        let count = count.max(1);
        // One worker may drain every packet already queued. Several workers
        // take one packet each, matching `task`, so a large packet is not
        // stolen from the others and a single worker keeps input order.
        let drain = count == 1;
        let limit = self.chunk_limit;
        self.spawn_stage("class_task", count, move |rx, tx, shared| {
            // `S` is built here, so it never has to cross a thread boundary.
            let mut state = init();
            map_out(rx, tx, &shared, limit, drain, |item, out| {
                if shared.is_cancelled() {
                    return false;
                }
                let mapped = method(&mut state, item);
                !shared.is_cancelled() && out.push(mapped)
            });
            // Unwind from `init` or `method` skips this call. `Shared::spawn`
            // records that panic; a panic from `close` itself is recorded too.
            close(state);
        })
    }

    /// Map each item through `f`, which may fail.
    ///
    /// This is the item-aware callback form of per-item error handling: `Err`
    /// from `f` is passed to `on_error` together with the original item and
    /// does **not** fail the pipeline. `Some(v)` forwards `v`; `None` skips the
    /// item. [`Pipeline::try_task`] is the mode-based form ([`ErrorMode`]) and
    /// does not hand the item to a callback.
    ///
    /// A panic inside `f` or `on_error` is not recoverable: it fails the
    /// pipeline with stage `"task_or"` (see [`PipelineError`]).
    pub fn task_or<U, E, F, H>(self, f: F, on_error: H) -> Pipeline<U>
    where
        T: Clone,
        U: Send + 'static,
        E: 'static,
        F: Fn(T) -> Result<U, E> + Send + 'static,
        H: Fn(T, E) -> Option<U> + Send + 'static,
    {
        let limit = self.chunk_limit;
        self.spawn_single("task_or", move |rx, tx, shared| {
            map_out(rx, tx, &shared, limit, true, |item, out| {
                let cloned = item.clone();
                match f(item) {
                    Ok(value) => out.push(value),
                    Err(err) => match on_error(cloned, err) {
                        Some(value) => out.push(value),
                        None => true,
                    },
                }
            });
        })
    }

    /// Map each item through `f`, applying `mode` when `f` returns [`Err`].
    ///
    /// `count == 0` runs one worker. One worker preserves input order; several
    /// workers do not, same as [`Pipeline::task`].
    ///
    /// * [`ErrorMode::FailFast`] — the first `Err` cancels the pipeline with
    ///   [`PipelineError::TaskFailed`] `{ stage: "try_task", message }` where
    ///   `message` is `err.to_string()`. Panics are not caught, so they still
    ///   fail the stage as [`PipelineError::WorkerPanicked`] with no extra
    ///   `catch_unwind` on the success path.
    /// * [`ErrorMode::Skip`] — the item is dropped.
    /// * [`ErrorMode::SkipAndLog`] — the item is dropped and
    ///   `olympipe-rs: [try_task] skipped item: {err}` is written to stderr.
    /// * [`ErrorMode::Replace`] — a clone of the replacement is emitted instead.
    ///
    /// In every mode except `FailFast`, a panic inside `f` is a per-item
    /// failure too (the panic payload is the message, or `"(unknown panic)"`).
    /// That matches a policy which catches every failure of one item and lets
    /// the rest of the stream continue. `FailFast` does not catch it.
    ///
    /// [`Pipeline::task_or`] remains the callback form that also receives the
    /// original item.
    ///
    /// ```
    /// use olympipe_rs::{ErrorMode, Pipeline};
    ///
    /// let values = Pipeline::new(vec!["1", "x", "3"])
    ///     .try_task(|s: &str| s.parse::<i32>(), 1, ErrorMode::Skip)
    ///     .collect();
    /// assert_eq!(values, vec![1, 3]);
    /// ```
    pub fn try_task<U, E, F>(self, f: F, count: usize, mode: ErrorMode<U>) -> Pipeline<U>
    where
        U: Clone + Send + 'static,
        E: std::fmt::Display + 'static,
        F: Fn(T) -> Result<U, E> + Send + Clone + 'static,
    {
        let count = count.max(1);
        let drain = count == 1;
        let limit = self.chunk_limit;
        // Fail-fast must not pay for `catch_unwind`. A panic still reaches
        // `Shared::spawn`, which records stage "try_task".
        if matches!(mode, ErrorMode::FailFast) {
            return self.spawn_stage("try_task", count, move |rx, tx, shared| {
                map_out(rx, tx, &shared, limit, drain, |item, out| {
                    if shared.is_cancelled() {
                        return false;
                    }
                    match f(item) {
                        Ok(value) => !shared.is_cancelled() && out.push(value),
                        Err(err) => {
                            shared.fail(PipelineError::TaskFailed {
                                stage: "try_task",
                                message: err.to_string(),
                            });
                            false
                        }
                    }
                });
            });
        }
        self.spawn_stage("try_task", count, move |rx, tx, shared| {
            map_out(rx, tx, &shared, limit, drain, |item, out| {
                if shared.is_cancelled() {
                    return false;
                }
                match panic::catch_unwind(AssertUnwindSafe(|| f(item))) {
                    Ok(Ok(value)) => !shared.is_cancelled() && out.push(value),
                    Ok(Err(err)) => {
                        !shared.is_cancelled() && note_skipped(&mode, "try_task", &err, out)
                    }
                    Err(payload) => {
                        !shared.is_cancelled()
                            && note_skipped(&mode, "try_task", &panic_message(&payload), out)
                    }
                }
            });
        })
    }

    /// [`Pipeline::task`] with an [`ErrorMode`] applied to panics inside `f`.
    ///
    /// `f` itself cannot return `Err`. [`ErrorMode::FailFast`] is `task`: the
    /// panic is not caught and the stage name stays `"task"`. The other modes
    /// catch the panic per item (`Skip` drops it, `SkipAndLog` also writes
    /// `olympipe-rs: [task_with_mode] skipped item: {message}` to stderr,
    /// `Replace` emits a clone of the replacement). `count` follows `task`.
    ///
    /// ```
    /// use olympipe_rs::{ErrorMode, Pipeline};
    ///
    /// let values = Pipeline::new(vec![1, 0, 4])
    ///     .task_with_mode(
    ///         |x: i32| {
    ///             if x == 0 {
    ///                 panic!("zero");
    ///             }
    ///             10 / x
    ///         },
    ///         1,
    ///         ErrorMode::Replace(-1),
    ///     )
    ///     .collect();
    /// assert_eq!(values, vec![10, -1, 2]);
    /// ```
    pub fn task_with_mode<U, F>(self, f: F, count: usize, mode: ErrorMode<U>) -> Pipeline<U>
    where
        U: Clone + Send + 'static,
        F: Fn(T) -> U + Send + Clone + 'static,
    {
        if matches!(mode, ErrorMode::FailFast) {
            return self.task(f, count);
        }
        let count = count.max(1);
        let drain = count == 1;
        let limit = self.chunk_limit;
        self.spawn_stage("task_with_mode", count, move |rx, tx, shared| {
            map_out(rx, tx, &shared, limit, drain, |item, out| {
                if shared.is_cancelled() {
                    return false;
                }
                match panic::catch_unwind(AssertUnwindSafe(|| f(item))) {
                    Ok(value) => !shared.is_cancelled() && out.push(value),
                    Err(payload) => {
                        !shared.is_cancelled()
                            && note_skipped(&mode, "task_with_mode", &panic_message(&payload), out)
                    }
                }
            });
        })
    }

    /// Map each item through `f`, reusing a JSON file from `cache_dir` when the
    /// same `step` has already seen that input.
    ///
    /// Rust cannot hash a closure, so `step` is the cache identity. Changing it
    /// (or the JSON produced for an input) invalidates previous files. Put a
    /// version in the name when the computation changes, for example
    /// `"resize-v2"`.
    ///
    /// The key is the lowercase hex SHA-256 of the raw 32-byte SHA-256 of
    /// `step` (UTF-8) concatenated with the `serde_json` bytes of the input.
    /// The file is `{cache_dir}/{first 16 hex chars of sha256(step)}/{key}.json`
    /// ([`cache_file`](crate::cache_file)). A hit is read and deserialized and `f` is not called.
    /// A miss calls `f` and writes the JSON atomically: a unique temporary file
    /// in that directory, then `rename`, so concurrent workers never observe a
    /// partial file. Directories are created as needed. A missing, corrupt, or
    /// undeserializable file is a miss and is overwritten.
    ///
    /// Keys follow `serde_json`. A [`std::collections::HashMap`] does not have
    /// a stable key order, so equal maps can miss. Use
    /// [`std::collections::BTreeMap`] (or another stably encoded type) when the
    /// input has a map.
    ///
    /// `count == 0` runs one worker, same as [`Pipeline::task`]. One worker
    /// preserves input order; several workers do not. The output is `U` itself.
    /// Python olympipe wraps the value in a `CacheEnvelope` and needs
    /// `uncache`; this method does not.
    ///
    /// Serializing the input is part of the key. If that fails the pipeline
    /// stops with [`PipelineError::TaskFailed`] `{ stage: "cached_task", .. }`
    /// and `f` is not called. A panic in `f` fails the pipeline with stage
    /// `"cached_task"`. An error while writing the cache (I/O, or serializing
    /// `U`) does not drop the value: it is still sent downstream, and the
    /// worker writes `olympipe-rs: [cached_task] cache write failed: {err}` to
    /// stderr once.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    /// use std::sync::atomic::{AtomicUsize, Ordering};
    /// use std::sync::Arc;
    ///
    /// let dir = std::env::temp_dir().join(format!(
    ///     "olympipe-rs-doc-cache-{}",
    ///     std::process::id()
    /// ));
    /// let _ = std::fs::remove_dir_all(&dir);
    /// let calls = Arc::new(AtomicUsize::new(0));
    /// let counted = Arc::clone(&calls);
    /// let values = Pipeline::new(vec![1, 2, 1])
    ///     .cached_task("double-v1", &dir, move |x: i32| {
    ///         counted.fetch_add(1, Ordering::SeqCst);
    ///         x * 2
    ///     }, 1)
    ///     .collect();
    /// assert_eq!(values, vec![2, 4, 2]);
    /// assert_eq!(calls.load(Ordering::SeqCst), 2);
    /// let _ = std::fs::remove_dir_all(&dir);
    /// ```
    #[cfg(feature = "cache")]
    #[cfg_attr(docsrs, doc(cfg(feature = "cache")))]
    pub fn cached_task<U, F>(
        self,
        step: &str,
        cache_dir: impl Into<std::path::PathBuf>,
        f: F,
        count: usize,
    ) -> Pipeline<U>
    where
        T: serde::Serialize,
        U: serde::Serialize + serde::de::DeserializeOwned + Send + 'static,
        F: Fn(T) -> U + Send + Clone + 'static,
    {
        let count = count.max(1);
        let drain = count == 1;
        let limit = self.chunk_limit;
        let step = step.to_owned();
        let cache_dir = cache_dir.into();
        self.spawn_stage("cached_task", count, move |rx, tx, shared| {
            let mut logged = false;
            map_out(rx, tx, &shared, limit, drain, |item, out| {
                if shared.is_cancelled() {
                    return false;
                }
                match crate::cache::resolve(&step, &cache_dir, item, &f, &mut logged) {
                    Ok(value) => !shared.is_cancelled() && out.push(value),
                    Err(err) => {
                        shared.fail(err);
                        false
                    }
                }
            });
        })
    }

    /// Keep only items for which `f` returns `true`.
    ///
    /// A panic in `f` fails the pipeline (stage `"filter"`).
    pub fn filter<F>(self, f: F) -> Pipeline<T>
    where
        F: Fn(&T) -> bool + Send + 'static,
    {
        let limit = self.chunk_limit;
        self.spawn_single("filter", move |rx, tx, shared| {
            map_out(rx, tx, &shared, limit, true, |item, out| {
                if f(&item) { out.push(item) } else { true }
            });
        })
    }

    /// Group items into `Vec<T>` of at most `size` elements.
    /// The last (potentially incomplete) batch is always emitted.
    pub fn batch(self, size: usize) -> Pipeline<Vec<T>> {
        assert!(size >= 1, "batch size must be >= 1");
        let limit = self.chunk_limit;
        self.spawn_single("batch", move |rx, tx, shared| {
            let mut buf: Vec<T> = Vec::with_capacity(size);
            let mut out = BatchTx::new(tx, limit);
            loop {
                let step = pull(&rx, &shared, true, |item| {
                    buf.push(item);
                    if buf.len() >= size {
                        let full = std::mem::replace(&mut buf, Vec::with_capacity(size));
                        return out.push(full);
                    }
                    true
                });
                match step {
                    Step::Yield => {
                        if !out.flush() {
                            break;
                        }
                    }
                    Step::Closed => {
                        if !shared.is_cancelled() {
                            if !buf.is_empty() {
                                let _ = out.push(std::mem::take(&mut buf));
                            }
                            let _ = out.flush();
                        }
                        break;
                    }
                    Step::Halted | Step::Cancelled => break,
                }
            }
        })
    }

    /// Collect items arriving within `window` of each other into a `Vec<T>`.
    /// A new batch begins whenever the inter-item gap exceeds `window`, or when
    /// the upstream channel disconnects.
    pub fn temporal_batch(self, window: Duration) -> Pipeline<Vec<T>> {
        self.spawn_single("temporal_batch", move |rx, tx, shared| {
            loop {
                if shared.is_cancelled() {
                    return;
                }
                let first = match rx.recv() {
                    Ok(packet) => packet,
                    Err(_) => return,
                };
                if shared.is_cancelled() {
                    return;
                }
                let mut batch = Vec::new();
                if !extend_batch(&mut batch, first, &shared) {
                    return;
                }
                loop {
                    if shared.is_cancelled() {
                        return;
                    }
                    match rx.recv_timeout(window) {
                        Ok(packet) => {
                            if !extend_batch(&mut batch, packet, &shared) {
                                return;
                            }
                        }
                        Err(RecvTimeoutError::Timeout) => break,
                        Err(RecvTimeoutError::Disconnected) => {
                            if !shared.is_cancelled() {
                                let _ = tx.send(Packet::One(batch));
                            }
                            return;
                        }
                    }
                }
                if shared.is_cancelled() || tx.send(Packet::One(batch)).is_err() {
                    return;
                }
            }
        })
    }

    /// Apply `f` to each item and forward every element yielded by the
    /// returned iterator (flatMap).
    ///
    /// A panic in `f` fails the pipeline (stage `"explode"`).
    pub fn explode<U, I, F>(self, f: F) -> Pipeline<U>
    where
        U: Send + 'static,
        I: IntoIterator<Item = U>,
        F: Fn(T) -> I + Send + 'static,
    {
        let limit = self.chunk_limit;
        self.spawn_single("explode", move |rx, tx, shared| {
            map_out(rx, tx, &shared, limit, true, |item, out| {
                for produced in f(item) {
                    if shared.is_cancelled() || !out.push(produced) {
                        return false;
                    }
                }
                true
            });
        })
    }

    /// Route each item to one or both output pipelines according to `f`.
    ///
    /// Both output channels are **bounded** (`self.buffer_size`). A branch that
    /// is not consumed applies back-pressure to the router, so the two branches
    /// must be consumed **concurrently** — via [`Pipeline::gather`], or with one
    /// branch drained on another thread. Draining them one after the other can
    /// block the router once a buffer fills and stall the pipeline.
    ///
    /// Dropping a branch disconnects it. The router keeps feeding the other
    /// branch and does not block on the disconnected one. If both branches are
    /// gone, the router stops.
    ///
    /// A panic in `f` fails the pipeline (stage `"split"`).
    ///
    /// For more than two branches, use [`Pipeline::split_by`] or
    /// [`Pipeline::split_array`]. For sequential consumption, use
    /// [`Pipeline::split_unbounded`].
    ///
    /// # Example
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    /// use std::thread;
    ///
    /// let (evens, odds) = Pipeline::new(0..10i32).split(|x| {
    ///     if x % 2 == 0 {
    ///         (Some(x), None)
    ///     } else {
    ///         (None, Some(x))
    ///     }
    /// });
    ///
    /// thread::scope(|scope| {
    ///     let odds = scope.spawn(|| odds.collect());
    ///     let mut evens = evens.collect();
    ///     let mut odds = odds.join().unwrap();
    ///     evens.sort();
    ///     odds.sort();
    ///     assert_eq!(evens, vec![0, 2, 4, 6, 8]);
    ///     assert_eq!(odds, vec![1, 3, 5, 7, 9]);
    /// });
    /// ```
    pub fn split<A, B, F>(self, f: F) -> (Pipeline<A>, Pipeline<B>)
    where
        A: Send + 'static,
        B: Send + 'static,
        F: Fn(T) -> (Option<A>, Option<B>) + Send + 'static,
    {
        self.split_inner(false, f)
    }

    /// Like [`Pipeline::split`], but both output channels are unbounded.
    ///
    /// There is no back-pressure between the router and its branches: a branch
    /// that is not yet consumed can retain the whole input. That makes it safe
    /// to drain one branch completely before the other. Prefer [`Pipeline::split`]
    /// when the branches are consumed concurrently and memory should stay bounded.
    ///
    /// # Example
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let (evens, odds) = Pipeline::new(0..10i32).split_unbounded(|x| {
    ///     if x % 2 == 0 {
    ///         (Some(x), None)
    ///     } else {
    ///         (None, Some(x))
    ///     }
    /// });
    /// // Sequential consumption is safe because the router never blocks on send.
    /// let evens = evens.collect();
    /// let odds = odds.collect();
    /// assert_eq!(evens, vec![0, 2, 4, 6, 8]);
    /// assert_eq!(odds, vec![1, 3, 5, 7, 9]);
    /// ```
    pub fn split_unbounded<A, B, F>(self, f: F) -> (Pipeline<A>, Pipeline<B>)
    where
        A: Send + 'static,
        B: Send + 'static,
        F: Fn(T) -> (Option<A>, Option<B>) + Send + 'static,
    {
        self.split_inner(true, f)
    }

    fn split_inner<A, B, F>(mut self, unbounded_out: bool, f: F) -> (Pipeline<A>, Pipeline<B>)
    where
        A: Send + 'static,
        B: Send + 'static,
        F: Fn(T) -> (Option<A>, Option<B>) + Send + 'static,
    {
        self.activate();
        let buffer_size = self.buffer_size;
        let chunk_limit = self.chunk_limit;
        let shared = Arc::clone(&self.shared);
        let (tx_a, rx_a) = if unbounded_out {
            unbounded()
        } else {
            bounded(buffer_size)
        };
        let (tx_b, rx_b) = if unbounded_out {
            unbounded()
        } else {
            bounded(buffer_size)
        };
        let input = self.receiver;
        let shared_router = Arc::clone(&shared);
        Shared::spawn(&shared, "split", move || {
            route(input, tx_a, tx_b, chunk_limit, f, &shared_router);
        });
        (
            downstream(rx_a, Arc::clone(&shared), buffer_size, chunk_limit),
            downstream(rx_b, shared, buffer_size, chunk_limit),
        )
    }

    /// Route each item to branch `f(&item)` of `n`.
    ///
    /// `n` must be at least 1. Every branch is a bounded channel of
    /// `buffer_size` messages, packetised on its own with the pipeline's chunk
    /// limit, and all branches share one cancellation state. An index `>= n`
    /// panics the router; the pipeline fails with
    /// [`PipelineError::WorkerPanicked`] `{ stage: "split", .. }`.
    ///
    /// Dropping a branch disconnects it. The router keeps feeding the others
    /// and stops when every branch is gone. A branch nobody reads applies
    /// back-pressure, so the branches must be consumed concurrently
    /// ([`Pipeline::gather_all`], [`Pipeline::wait_for_results`], or one thread
    /// per branch). [`Pipeline::split_by_unbounded`] is the sequential opt-in.
    ///
    /// # Panics
    ///
    /// Panics if `n` is 0.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let mut values = Pipeline::gather_all(
    ///     Pipeline::new(0..6i32).split_by(2, |x| if x % 2 == 0 { 0 } else { 1 }),
    /// )
    /// .collect();
    /// values.sort();
    /// assert_eq!(values, vec![0, 1, 2, 3, 4, 5]);
    /// ```
    pub fn split_by<F>(self, n: usize, f: F) -> Vec<Pipeline<T>>
    where
        F: Fn(&T) -> usize + Send + 'static,
    {
        self.split_by_inner(n, false, f)
    }

    /// Like [`Pipeline::split_by`], but every branch channel is unbounded.
    ///
    /// A branch that is not yet consumed can retain the whole input, which
    /// makes it safe to drain the [`Vec`] from front to back. Prefer
    /// [`Pipeline::split_by`] when the branches are read concurrently.
    ///
    /// # Panics
    ///
    /// Panics if `n` is 0.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let mut branches = Pipeline::new(0..6i32).split_by_unbounded(2, |x| usize::from(x % 2 == 1));
    /// let odds = branches.pop().expect("odds");
    /// let evens = branches.pop().expect("evens");
    /// assert_eq!(evens.collect(), vec![0, 2, 4]);
    /// assert_eq!(odds.collect(), vec![1, 3, 5]);
    /// ```
    pub fn split_by_unbounded<F>(self, n: usize, f: F) -> Vec<Pipeline<T>>
    where
        F: Fn(&T) -> usize + Send + 'static,
    {
        self.split_by_inner(n, true, f)
    }

    fn split_by_inner<F>(mut self, n: usize, unbounded_out: bool, f: F) -> Vec<Pipeline<T>>
    where
        F: Fn(&T) -> usize + Send + 'static,
    {
        assert!(n >= 1, "split_by requires at least one branch");
        self.activate();
        let buffer_size = self.buffer_size;
        let chunk_limit = self.chunk_limit;
        let shared = Arc::clone(&self.shared);
        let cap = if unbounded_out {
            None
        } else {
            Some(buffer_size)
        };
        let ends = open_channels::<T>(n, cap);
        let txs = ends.txs;
        let rxs = ends.rxs;
        let input = self.receiver;
        let shared_router = Arc::clone(&shared);
        Shared::spawn(&shared, "split", move || {
            route_by(input, txs, chunk_limit, f, &shared_router);
        });
        rxs.into_iter()
            .map(|rx| downstream(rx, Arc::clone(&shared), buffer_size, chunk_limit))
            .collect()
    }

    /// Route each item through `f` into `N` branches.
    ///
    /// `f` returns one [`Option`] per branch. `Some` is sent on that branch,
    /// `None` skips it, so one item can land on several branches or on none.
    /// `N` must be at least 1. Channels are bounded (`buffer_size` messages),
    /// each branch packetises on its own, and every branch shares one
    /// cancellation state. Dropping a branch disconnects it; the router keeps
    /// feeding the rest and stops when all of them are gone.
    ///
    /// Consume the branches concurrently. [`Pipeline::split_array_unbounded`]
    /// allows draining them one after another.
    ///
    /// A panic in `f` fails the pipeline (stage `"split"`).
    ///
    /// # Panics
    ///
    /// Panics if `N` is 0.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let [evens, odds] = Pipeline::new(0..6i32).split_array(|x| {
    ///     if x % 2 == 0 {
    ///         [Some(x), None]
    ///     } else {
    ///         [None, Some(x)]
    ///     }
    /// });
    /// let mut values = evens.gather(vec![odds]).collect();
    /// values.sort();
    /// assert_eq!(values, (0..6).collect::<Vec<_>>());
    /// ```
    pub fn split_array<const N: usize, U, F>(self, f: F) -> [Pipeline<U>; N]
    where
        U: Send + 'static,
        F: Fn(T) -> [Option<U>; N] + Send + 'static,
    {
        self.split_array_inner(false, f)
    }

    /// Like [`Pipeline::split_array`], but every branch channel is unbounded.
    ///
    /// Sequential consumption is safe. The router can retain every item that a
    /// branch has not read yet.
    ///
    /// # Panics
    ///
    /// Panics if `N` is 0.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let [evens, odds] = Pipeline::new(0..6i32).split_array_unbounded(|x| {
    ///     if x % 2 == 0 {
    ///         [Some(x), None]
    ///     } else {
    ///         [None, Some(x)]
    ///     }
    /// });
    /// assert_eq!(evens.collect(), vec![0, 2, 4]);
    /// assert_eq!(odds.collect(), vec![1, 3, 5]);
    /// ```
    pub fn split_array_unbounded<const N: usize, U, F>(self, f: F) -> [Pipeline<U>; N]
    where
        U: Send + 'static,
        F: Fn(T) -> [Option<U>; N] + Send + 'static,
    {
        self.split_array_inner(true, f)
    }

    fn split_array_inner<const N: usize, U, F>(
        mut self,
        unbounded_out: bool,
        f: F,
    ) -> [Pipeline<U>; N]
    where
        U: Send + 'static,
        F: Fn(T) -> [Option<U>; N] + Send + 'static,
    {
        assert!(N >= 1, "split_array requires at least one branch");
        self.activate();
        let buffer_size = self.buffer_size;
        let chunk_limit = self.chunk_limit;
        let shared = Arc::clone(&self.shared);
        let cap = if unbounded_out {
            None
        } else {
            Some(buffer_size)
        };
        let ends = open_channels::<U>(N, cap);
        let txs = ends.txs;
        let rxs = ends.rxs;
        let input = self.receiver;
        let shared_router = Arc::clone(&shared);
        Shared::spawn(&shared, "split", move || {
            route_fanout(input, txs, chunk_limit, f, &shared_router);
        });
        let mut rxs = rxs.into_iter();
        std::array::from_fn(|_| {
            downstream(
                rxs.next().expect("one receiver per branch"),
                Arc::clone(&shared),
                buffer_size,
                chunk_limit,
            )
        })
    }

    /// Merge `self` and all pipelines in `others` into a single output stream.
    /// One forwarder thread is spawned per source; all handles are merged into
    /// the returned pipeline's handle registry.
    ///
    /// A failure in any source (including a source iterator panic) fails the
    /// gathered pipeline.
    ///
    /// [`Pipeline::gather_all`] is the same merge when every branch, including
    /// this one, is already in a [`Vec`] (the shape [`Pipeline::split_by`]
    /// returns).
    pub fn gather(mut self, others: Vec<Pipeline<T>>) -> Pipeline<T> {
        self.activate();
        let others: Vec<Pipeline<T>> = others
            .into_iter()
            .map(|mut other| {
                other.activate();
                other
            })
            .collect();
        let buffer_size = self.buffer_size;
        let chunk_limit = self.chunk_limit;
        let (tx, rx) = bounded::<Packet<T>>(buffer_size);
        let shared = Arc::clone(&self.shared);

        let tx0 = tx.clone();
        let self_rx = self.receiver;
        let shared_fwd = Arc::clone(&shared);
        Shared::spawn(&shared, "gather", move || forward(self_rx, tx0, shared_fwd));

        for other in others {
            Shared::link(&shared, &other.shared);
            let drained = {
                let mut guard = lock(&other.shared.handles);
                std::mem::take(&mut *guard)
            };
            lock(&shared.handles).extend(drained);

            let tx_n = tx.clone();
            let other_rx = other.receiver;
            let shared_fwd = Arc::clone(&shared);
            Shared::spawn(&shared, "gather", move || {
                forward(other_rx, tx_n, shared_fwd)
            });
        }
        drop(tx);

        downstream(rx, shared, buffer_size, chunk_limit)
    }

    /// Merge every pipeline in `pipes` into one stream.
    ///
    /// The output channel uses the first pipeline's buffer size and packet
    /// limit, because this calls [`Pipeline::gather`] with that pipeline as
    /// `self`. A failure in any source fails the gathered pipeline.
    ///
    /// # Panics
    ///
    /// Panics if `pipes` is empty.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let mut values = Pipeline::gather_all(vec![
    ///     Pipeline::new(vec![1, 2]),
    ///     Pipeline::new(vec![3, 4]),
    /// ])
    /// .collect();
    /// values.sort();
    /// assert_eq!(values, vec![1, 2, 3, 4]);
    /// ```
    pub fn gather_all(pipes: Vec<Pipeline<T>>) -> Pipeline<T> {
        assert!(
            !pipes.is_empty(),
            "gather_all requires at least one pipeline"
        );
        let mut pipes = pipes.into_iter();
        let first = pipes
            .next()
            .expect("gather_all requires at least one pipeline");
        first.gather(pipes.collect())
    }

    /// Fold all items into a single accumulated value and emit it as the sole
    /// output item.
    ///
    /// A panic in `f` fails the pipeline (stage `"reduce"`). The accumulator is
    /// not emitted when the pipeline is cancelled.
    pub fn reduce<U, F>(self, init: U, f: F) -> Pipeline<U>
    where
        U: Send + 'static,
        F: Fn(U, T) -> U + Send + 'static,
    {
        self.spawn_single("reduce", move |rx, tx, shared| {
            let mut acc = Some(init);
            loop {
                match pull(&rx, &shared, true, |item| {
                    let current = acc.take().expect("accumulator");
                    acc = Some(f(current, item));
                    true
                }) {
                    Step::Yield => {}
                    Step::Closed => {
                        if !shared.is_cancelled() {
                            if let Some(value) = acc.take() {
                                let _ = tx.send(Packet::One(value));
                            }
                        }
                        break;
                    }
                    Step::Halted | Step::Cancelled => break,
                }
            }
        })
    }

    /// Fold every input into `initial`, then stream what `emitter` produces
    /// from that state.
    ///
    /// A single worker runs the fold, so `reducer` sees items in input order.
    /// `reducer` is `(state, item) -> state`, the same order as
    /// [`Pipeline::reduce`] and [`Iterator::fold`]. Python olympipe passes
    /// `(item, state)`.
    ///
    /// `emitter` runs only after the input is exhausted, and only if this
    /// pipeline was not cancelled or failed. Each produced item is sent
    /// downstream with the same packetised channel as other stages, so a full
    /// buffer stalls this worker and a disconnected or cancelled downstream
    /// stops the rest of the iterator. Nothing is sent when the fold stops
    /// because the pipeline failed or was cancelled, including when the input
    /// ends after a failure.
    ///
    /// A panic in `reducer` or `emitter` fails the pipeline with stage
    /// `"checkpoint"`.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let sorted = Pipeline::new(vec![3, 1, 2]).checkpoint(
    ///     Vec::new(),
    ///     |mut state, item| {
    ///         state.push(item);
    ///         state
    ///     },
    ///     |mut state| {
    ///         state.sort();
    ///         state
    ///     },
    /// );
    /// assert_eq!(sorted.collect(), vec![1, 2, 3]);
    /// ```
    pub fn checkpoint<S, U, I, R, E>(self, initial: S, reducer: R, emitter: E) -> Pipeline<U>
    where
        S: Send + 'static,
        U: Send + 'static,
        R: Fn(S, T) -> S + Send + 'static,
        E: FnOnce(S) -> I + Send + 'static,
        I: IntoIterator<Item = U>,
    {
        let limit = self.chunk_limit;
        self.spawn_single("checkpoint", move |rx, tx, shared| {
            let mut state = Some(initial);
            loop {
                match pull(&rx, &shared, true, |item| {
                    let current = state.take().expect("checkpoint state");
                    state = Some(reducer(current, item));
                    true
                }) {
                    Step::Yield => {}
                    Step::Closed => {
                        if !shared.is_cancelled() {
                            if let Some(finished) = state.take() {
                                let mut out = BatchTx::new(tx, limit);
                                for produced in emitter(finished) {
                                    if shared.is_cancelled() || !out.push(produced) {
                                        return;
                                    }
                                }
                                if !shared.is_cancelled() {
                                    let _ = out.flush();
                                }
                            }
                        }
                        break;
                    }
                    Step::Halted | Step::Cancelled => break,
                }
            }
        })
    }

    /// Pass at most `n` items downstream, then stop.
    pub fn limit(self, n: usize) -> Pipeline<T> {
        let limit = self.chunk_limit;
        self.spawn_single("limit", move |rx, tx, shared| {
            let mut sent = 0usize;
            map_out(rx, tx, &shared, limit, true, |item, out| {
                if !out.push(item) {
                    return false;
                }
                sent += 1;
                sent < n
            });
        })
    }

    /// Fail the pipeline if no item arrives within `duration` between two
    /// consecutive items.
    ///
    /// The first item is awaited without a deadline. On expiry the stage
    /// records [`PipelineError::Timeout`], cancels the graph, and drops its
    /// output sender. [`Pipeline::try_collect`] (and the other `try_*`
    /// terminals) return that error; [`Pipeline::collect`] panics.
    pub fn timeout(self, duration: Duration) -> Pipeline<T> {
        let limit = self.chunk_limit;
        self.spawn_single("timeout", move |rx, tx, shared| {
            let mut out = BatchTx::new(tx, limit);
            let first = match rx.recv() {
                Ok(packet) => packet,
                Err(_) => return,
            };
            if !forward_packet(&mut out, first, &shared) {
                return;
            }
            loop {
                if shared.is_cancelled() {
                    return;
                }
                match rx.recv_timeout(duration) {
                    Ok(packet) => {
                        if !forward_packet(&mut out, packet, &shared) {
                            break;
                        }
                    }
                    Err(RecvTimeoutError::Timeout) => {
                        shared.fail(PipelineError::Timeout(duration));
                        break;
                    }
                    Err(RecvTimeoutError::Disconnected) => break,
                }
            }
        })
    }

    /// Print each item to stdout and forward it unchanged.
    pub fn debug(self) -> Pipeline<T>
    where
        T: std::fmt::Debug,
    {
        let limit = self.chunk_limit;
        self.spawn_single("debug", move |rx, tx, shared| {
            map_out(rx, tx, &shared, limit, true, |item, out| {
                println!("[olympipe-rs] {item:?}");
                out.push(item)
            });
        })
    }
}

/// Push every item of `packet` into `batch`. `false` if the pipeline is cancelled.
fn extend_batch<T>(batch: &mut Vec<T>, packet: Packet<T>, shared: &Shared) -> bool {
    feed_packet(packet, shared, &mut |item| {
        batch.push(item);
        true
    })
}

fn forward_packet<T>(out: &mut BatchTx<T>, packet: Packet<T>, shared: &Shared) -> bool {
    let ok = feed_packet(packet, shared, &mut |item| out.push(item));
    ok && !shared.is_cancelled() && out.flush()
}

/// Forward packets unchanged. Gather does not repack: the source stage already
/// coalesced, and one send per packet keeps per-source order.
fn forward<T>(rx: Receiver<Packet<T>>, tx: Sender<Packet<T>>, shared: Arc<Shared>) {
    loop {
        if shared.is_cancelled() {
            break;
        }
        match rx.recv() {
            Ok(packet) => {
                if shared.is_cancelled() || tx.send(packet).is_err() {
                    break;
                }
            }
            Err(_) => break,
        }
    }
}

fn route<T, A, B, F>(
    rx: Receiver<Packet<T>>,
    tx_a: Sender<Packet<A>>,
    tx_b: Sender<Packet<B>>,
    limit: usize,
    f: F,
    shared: &Shared,
) where
    F: Fn(T) -> (Option<A>, Option<B>),
{
    let mut out_a = BatchTx::new(tx_a, limit);
    let mut out_b = BatchTx::new(tx_b, limit);
    let mut a_open = true;
    let mut b_open = true;
    loop {
        if shared.is_cancelled() || !(a_open || b_open) {
            break;
        }
        let step = pull(&rx, shared, true, |item| {
            if shared.is_cancelled() {
                return false;
            }
            let (a, b) = f(item);
            if a_open {
                if let Some(value) = a {
                    if !out_a.push(value) {
                        a_open = false;
                    }
                }
            }
            if b_open {
                if let Some(value) = b {
                    if !out_b.push(value) {
                        b_open = false;
                    }
                }
            }
            a_open || b_open
        });
        if a_open && !out_a.flush() {
            a_open = false;
        }
        if b_open && !out_b.flush() {
            b_open = false;
        }
        if step != Step::Yield || !(a_open || b_open) {
            break;
        }
    }
}

/// N-way channels. `None` is unbounded; `Some(capacity)` is bounded. Kept off
/// `route` so the two-branch split path does not grow a `Vec` of senders.
struct ChannelEnds<T> {
    txs: Vec<Sender<Packet<T>>>,
    rxs: Vec<Receiver<Packet<T>>>,
}

fn open_channels<T>(n: usize, capacity: Option<usize>) -> ChannelEnds<T> {
    let mut txs = Vec::with_capacity(n);
    let mut rxs = Vec::with_capacity(n);
    for _ in 0..n {
        let (tx, rx) = match capacity {
            Some(capacity) => bounded(capacity),
            None => unbounded(),
        };
        txs.push(tx);
        rxs.push(rx);
    }
    ChannelEnds { txs, rxs }
}

fn flush_open<T>(outs: &mut [Option<BatchTx<T>>]) {
    for slot in outs.iter_mut() {
        let failed = match slot.as_mut() {
            Some(out) => !out.flush(),
            None => false,
        };
        if failed {
            *slot = None;
        }
    }
}

/// Index routing. An out-of-range index panics so [`Shared::spawn`] records
/// stage `"split"`, matching a panic inside the two-way split closure.
fn route_by<T, F>(
    rx: Receiver<Packet<T>>,
    txs: Vec<Sender<Packet<T>>>,
    limit: usize,
    f: F,
    shared: &Shared,
) where
    F: Fn(&T) -> usize,
{
    let n = txs.len();
    let mut outs: Vec<Option<BatchTx<T>>> = txs
        .into_iter()
        .map(|tx| Some(BatchTx::new(tx, limit)))
        .collect();
    loop {
        if shared.is_cancelled() || outs.iter().all(Option::is_none) {
            break;
        }
        let step = pull(&rx, shared, true, |item| {
            if shared.is_cancelled() {
                return false;
            }
            let index = f(&item);
            if index >= n {
                panic!("split index {index} is outside 0..{n}");
            }
            let disconnected = match outs[index].as_mut() {
                Some(out) => !out.push(item),
                None => false,
            };
            if disconnected {
                outs[index] = None;
            }
            outs.iter().any(Option::is_some)
        });
        flush_open(&mut outs);
        if step != Step::Yield || outs.iter().all(Option::is_none) {
            break;
        }
    }
}

/// One item may be copied onto several branches, or onto none. Separate from
/// `route` so the two-output hot path stays a pair of senders.
fn route_fanout<T, U, const N: usize, F>(
    rx: Receiver<Packet<T>>,
    txs: Vec<Sender<Packet<U>>>,
    limit: usize,
    f: F,
    shared: &Shared,
) where
    F: Fn(T) -> [Option<U>; N],
{
    let mut outs: Vec<Option<BatchTx<U>>> = txs
        .into_iter()
        .map(|tx| Some(BatchTx::new(tx, limit)))
        .collect();
    loop {
        if shared.is_cancelled() || outs.iter().all(Option::is_none) {
            break;
        }
        let step = pull(&rx, shared, true, |item| {
            if shared.is_cancelled() {
                return false;
            }
            let parts = f(item);
            for (index, part) in parts.into_iter().enumerate() {
                let Some(value) = part else {
                    continue;
                };
                let disconnected = match outs[index].as_mut() {
                    Some(out) => !out.push(value),
                    None => false,
                };
                if disconnected {
                    outs[index] = None;
                }
            }
            outs.iter().any(Option::is_some)
        });
        flush_open(&mut outs);
        if step != Step::Yield || outs.iter().all(Option::is_none) {
            break;
        }
    }
}

/// `Skip` / `SkipAndLog` / `Replace` for one item. `FailFast` never reaches here.
fn note_skipped<U: Clone>(
    mode: &ErrorMode<U>,
    stage: &str,
    err: &dyn std::fmt::Display,
    out: &mut BatchTx<U>,
) -> bool {
    match mode {
        ErrorMode::Skip => true,
        ErrorMode::SkipAndLog => {
            eprintln!("olympipe-rs: [{stage}] skipped item: {err}");
            true
        }
        ErrorMode::Replace(value) => out.push(value.clone()),
        ErrorMode::FailFast => unreachable!("FailFast does not recover per item"),
    }
}

// ─── terminal methods ─────────────────────────────────────────────────────────

impl<T: Send + 'static> Pipeline<T> {
    /// Collect all output items into a `Vec`, then join every worker thread.
    ///
    /// # Panics
    ///
    /// Panics if a worker panics or [`Self::timeout`] fires:
    /// `panic!("olympipe-rs pipeline failed: {err}")`.
    /// The message includes the original worker panic text.
    /// Use [`Self::try_collect`] to receive the [`PipelineError`] instead.
    /// Recoverable per-item errors belong in [`Self::task_or`] and do not panic.
    pub fn collect(self) -> Vec<T> {
        match self.try_collect() {
            Ok(items) => items,
            Err(err) => panic!("olympipe-rs pipeline failed: {err}"),
        }
    }

    /// Apply `f` to each output item, then join every worker thread.
    ///
    /// # Panics
    ///
    /// Panics if a worker panics or [`Self::timeout`] fires:
    /// `panic!("olympipe-rs pipeline failed: {err}")`.
    /// The message includes the original worker panic text.
    /// Use [`Self::try_for_each`] to receive the [`PipelineError`] instead.
    /// Recoverable per-item errors belong in [`Self::task_or`] and do not panic.
    pub fn for_each<F>(self, f: F)
    where
        F: FnMut(T),
    {
        if let Err(err) = self.try_for_each(f) {
            panic!("olympipe-rs pipeline failed: {err}");
        }
    }

    /// Drain and discard all output, then join every worker thread.
    ///
    /// # Panics
    ///
    /// Panics if a worker panics or [`Self::timeout`] fires:
    /// `panic!("olympipe-rs pipeline failed: {err}")`.
    /// The message includes the original worker panic text.
    /// Use [`Self::try_wait_for_completion`] to receive the [`PipelineError`]
    /// instead. Recoverable per-item errors belong in [`Self::task_or`] and do
    /// not panic.
    pub fn wait_for_completion(self) {
        if let Err(err) = self.try_wait_for_completion() {
            panic!("olympipe-rs pipeline failed: {err}");
        }
    }

    /// Collect all output items, join every worker, and return the first
    /// pipeline failure.
    ///
    /// `Ok` means every worker finished without a panic or a timeout. On `Err`,
    /// items already queued are discarded: the pipeline's output is not a
    /// partial success.
    pub fn try_collect(self) -> Result<Vec<T>, PipelineError> {
        self.finish(|rx, shared| {
            let mut items = Vec::new();
            while let Some(packet) = recv_user(rx, shared) {
                match packet {
                    Packet::One(item) => items.push(item),
                    Packet::Many(more) => items.extend(more),
                }
            }
            items
        })
    }

    /// Apply `f` to each output item, join every worker, and return the first
    /// pipeline failure.
    ///
    /// A panic in `f` is a panic of the calling thread, not a [`PipelineError`].
    pub fn try_for_each<F>(self, mut f: F) -> Result<(), PipelineError>
    where
        F: FnMut(T),
    {
        self.finish(|rx, shared| {
            while let Some(packet) = recv_user(rx, shared) {
                match packet {
                    Packet::One(item) => f(item),
                    Packet::Many(more) => {
                        for item in more {
                            f(item);
                        }
                    }
                }
            }
        })
    }

    /// Drain and discard all output, join every worker, and return the first
    /// pipeline failure.
    pub fn try_wait_for_completion(self) -> Result<(), PipelineError> {
        self.finish(|rx, shared| while recv_user(rx, shared).is_some() {})
    }

    /// Collect this pipeline and `others`, with this pipeline's items first.
    ///
    /// Every pipeline is drained at the same time. Bounded branches of one
    /// [`Pipeline::split`] / [`Pipeline::split_by`] graph deadlock if they are
    /// read one after another; concurrent drains keep the router moving.
    ///
    /// # Panics
    ///
    /// Panics if any pipeline fails, with `olympipe-rs pipeline failed: {err}`,
    /// the same message as [`Pipeline::collect`].
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let mut branches = Pipeline::new(0..6i32).split_by(2, |x| usize::from(x % 2 == 1));
    /// let odds = branches.pop().expect("odds");
    /// let evens = branches.pop().expect("evens");
    /// let mut groups = evens.wait_for_results(vec![odds]);
    /// for group in &mut groups {
    ///     group.sort();
    /// }
    /// assert_eq!(groups[0], vec![0, 2, 4]);
    /// assert_eq!(groups[1], vec![1, 3, 5]);
    /// ```
    pub fn wait_for_results(self, others: Vec<Pipeline<T>>) -> Vec<Vec<T>> {
        unwrap_pipeline(self.try_wait_for_results(others))
    }

    /// [`Pipeline::wait_for_results`] as a [`Result`].
    ///
    /// On failure the successful pipelines' items are discarded. When several
    /// pipelines fail, the error is the one from the earliest pipeline in the
    /// list (`self`, then `others` in order) — each terminal still runs to
    /// completion so a sibling blocked on a shared router can observe the
    /// cancellation and return.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let mut branches = Pipeline::new(vec![1, 2, 3]).split_by(1, |_| 0);
    /// let only = branches.pop().expect("branch");
    /// let groups = only.try_wait_for_results(vec![]).expect("results");
    /// assert_eq!(groups, vec![vec![1, 2, 3]]);
    /// ```
    pub fn try_wait_for_results(
        self,
        others: Vec<Pipeline<T>>,
    ) -> Result<Vec<Vec<T>>, PipelineError> {
        let mut pipes = Vec::with_capacity(1 + others.len());
        pipes.push(self);
        pipes.extend(others);
        // Reading one bounded branch to completion before the next fills the
        // router and deadlocks. Every terminal has to be in `recv` together.
        collect_concurrent(pipes, Pipeline::try_collect)
    }

    /// Drain and discard this pipeline and `others`, concurrently.
    ///
    /// # Panics
    ///
    /// Panics if any pipeline fails, with `olympipe-rs pipeline failed: {err}`.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let mut branches = Pipeline::new(0..4i32).split_by(2, |x| usize::from(x % 2 == 1));
    /// let odds = branches.pop().expect("odds");
    /// let evens = branches.pop().expect("evens");
    /// evens.wait_for_all_completions(vec![odds]);
    /// ```
    pub fn wait_for_all_completions(self, others: Vec<Pipeline<T>>) {
        unwrap_pipeline(self.try_wait_for_all_completions(others));
    }

    /// [`Pipeline::wait_for_all_completions`] as a [`Result`].
    ///
    /// Output is discarded. Failure handling matches
    /// [`Pipeline::try_wait_for_results`]: siblings are still joined, so a
    /// shared cancellation flag can unblock them.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// Pipeline::new(0..3i32)
    ///     .try_wait_for_all_completions(vec![])
    ///     .expect("completed");
    /// ```
    pub fn try_wait_for_all_completions(
        self,
        others: Vec<Pipeline<T>>,
    ) -> Result<(), PipelineError> {
        let mut pipes = Vec::with_capacity(1 + others.len());
        pipes.push(self);
        pipes.extend(others);
        collect_concurrent(pipes, Pipeline::try_wait_for_completion).map(|_| ())
    }

    /// Fold every item with `f` and return the accumulator.
    ///
    /// `f` takes `(accumulator, item)`, the same order as [`Pipeline::reduce`].
    /// An empty input returns `init`. One worker performs the fold, so input
    /// order is preserved.
    ///
    /// # Panics
    ///
    /// Panics if the pipeline fails, with `olympipe-rs pipeline failed: {err}`.
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let sum = Pipeline::new(1..=4).wait_and_reduce(0, |acc, x| acc + x);
    /// assert_eq!(sum, 10);
    /// ```
    pub fn wait_and_reduce<U, F>(self, init: U, f: F) -> U
    where
        U: Send + 'static,
        F: Fn(U, T) -> U + Send + 'static,
    {
        unwrap_pipeline(self.try_wait_and_reduce(init, f))
    }

    /// [`Pipeline::wait_and_reduce`] as a [`Result`].
    ///
    /// A panic in `f` is [`PipelineError::WorkerPanicked`] with stage
    /// `"reduce"`, because the fold is [`Pipeline::reduce`].
    ///
    /// ```
    /// use olympipe_rs::Pipeline;
    ///
    /// let sum = Pipeline::new(1..=3)
    ///     .try_wait_and_reduce(0, |acc, x| acc + x)
    ///     .expect("sum");
    /// assert_eq!(sum, 6);
    /// ```
    pub fn try_wait_and_reduce<U, F>(self, init: U, f: F) -> Result<U, PipelineError>
    where
        U: Send + 'static,
        F: Fn(U, T) -> U + Send + 'static,
    {
        let mut reduced = self.reduce(init, f).try_collect()?;
        Ok(reduced.pop().expect("reduce emits the accumulator"))
    }
}

/// Drain `a` and `b` at the same time and return both vectors.
///
/// The two pipelines may have different item types. They are read concurrently
/// so a bounded split (see [`Pipeline::split_array`]) cannot deadlock on a
/// full branch. If both fail, `a`'s error is returned. The other terminal is
/// still joined: a shared cancellation flag must be able to wake it.
///
/// ```
/// use olympipe_rs::{try_collect_pair, Pipeline};
///
/// let numbers = Pipeline::new(vec![1, 2]);
/// let labels = Pipeline::new(vec!["a", "b"]);
/// let (numbers, labels) = try_collect_pair(numbers, labels).expect("pair");
/// assert_eq!(numbers, vec![1, 2]);
/// assert_eq!(labels, vec!["a", "b"]);
/// ```
pub fn try_collect_pair<A, B>(
    a: Pipeline<A>,
    b: Pipeline<B>,
) -> Result<(Vec<A>, Vec<B>), PipelineError>
where
    A: Send + 'static,
    B: Send + 'static,
{
    thread::scope(|scope| {
        let right = scope.spawn(|| b.try_collect());
        let left = a.try_collect();
        let right = match right.join() {
            Ok(result) => result,
            Err(payload) => std::panic::resume_unwind(payload),
        };
        match (left, right) {
            (Ok(left), Ok(right)) => Ok((left, right)),
            (Err(err), _) => Err(err),
            (Ok(_), Err(err)) => Err(err),
        }
    })
}

fn unwrap_pipeline<T>(result: Result<T, PipelineError>) -> T {
    match result {
        Ok(value) => value,
        Err(err) => panic!("olympipe-rs pipeline failed: {err}"),
    }
}

/// Run `drive` on every pipeline at once and keep the first error in list order.
fn collect_concurrent<T, R, Drive>(
    pipes: Vec<Pipeline<T>>,
    drive: Drive,
) -> Result<Vec<R>, PipelineError>
where
    T: Send + 'static,
    R: Send,
    Drive: Fn(Pipeline<T>) -> Result<R, PipelineError> + Sync,
{
    thread::scope(|scope| {
        let handles: Vec<_> = pipes
            .into_iter()
            .map(|pipe| scope.spawn(|| drive(pipe)))
            .collect();
        let mut values = Vec::with_capacity(handles.len());
        let mut failure = None;
        for handle in handles {
            match handle.join() {
                Ok(Ok(value)) => {
                    if failure.is_none() {
                        values.push(value);
                    }
                }
                Ok(Err(err)) => {
                    if failure.is_none() {
                        failure = Some(err);
                    }
                }
                Err(payload) => std::panic::resume_unwind(payload),
            }
        }
        match failure {
            Some(err) => Err(err),
            None => Ok(values),
        }
    })
}

/// One packet for a terminal, or `None` on cancel / disconnect.
/// The cancellation load is relaxed; it is not taken per item inside a packet.
fn recv_user<T>(rx: &Receiver<Packet<T>>, shared: &Shared) -> Option<Packet<T>> {
    if shared.is_cancelled() {
        return None;
    }
    match rx.recv() {
        Ok(packet) if !shared.is_cancelled() => Some(packet),
        _ => None,
    }
}