spate-s3 0.4.0

Object-storage (S3) source for the Spate pipeline framework: workers read objects and byte ranges as units from a work feed, filled by a lister run or by storage notifications, with reads pinned to ETags. Applications should depend on the `spate` facade crate with the `s3` feature.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
//! The async edge: the shared listing helper and one fetcher task per
//! unit part in progress, reading its member objects' bytes to the pipeline thread.
//!
//! Runs on the pipeline's I/O runtime. Every channel send `await`s, so a
//! fetcher never blocks a runtime worker and sink workers sharing the
//! runtime keep running while lanes are back-pressured.
//!
//! Each ETag-pinned object is read as a sequence of **bounded ranged GETs,
//! each drained fully into memory before any of its chunks is forwarded**.
//! The next window's GET runs while the current window is handed on. Each
//! body is drained as it arrives, so a back-pressured lane parks on buffered
//! bytes, never an idle S3 body that the client's request timeout (or an
//! intermediary) would reset mid-stream. See [`stream_object`].
//!
//! # Determinism
//!
//! A fetcher never lists: it reads its unit's member objects in descriptor
//! order, so every (ordinal, record index) offset is minted against the
//! immutable descriptor, identical under every reader of the unit.
//!
//! # Retries
//!
//! Transient GET failures are retried inside the fetcher with capped
//! exponential backoff, resuming from the last delivered byte with a
//! ranged GET **conditioned on the object's ETag**, so a splice of two
//! different object versions is impossible. Persistent failure (attempt
//! budget exhausted) and the object-level non-retryable classes (missing
//! key, failed precondition) **poison the unit**, which is failed back to the
//! feed for another delivery and dead-lettered at the feed's cap, while
//! credentials/configuration failures stay fatal for the pipeline. An
//! object whose store returns no ETag cannot be resumed mid-stream safely
//! and poisons the unit on a mid-object break instead.
//!
//! A failed precondition on a notification unit that names no version, or a
//! range the store cannot satisfy on one, is an overwrite
//! ([`is_overwrite`](crate::error::is_overwrite)). The fetcher
//! HEADs the key and reports its current content as
//! [`SplitFailure::Overwritten`], so the reader can re-enqueue the key.

use crate::error::classify;
use crate::lane_ctx::{LaneLabel, PoisonKind};
use crate::split::SplitRange;
use crate::trim::RangeTrim;
use crate::unit::{Origin, quoted};
use bytes::Bytes;
use futures_util::StreamExt as _;
use futures_util::future::{Either, select};
use object_store::path::Path;
use object_store::{GetOptions, GetRange, ObjectStore, ObjectStoreExt as _};
use spate_core::error::{ErrorClass, SourceError};
use std::future::Future;
use std::pin::{Pin, pin};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use tokio::sync::mpsc;

/// One listed object, in a lane's slice order.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ObjectEntry {
    /// Full object key (an `object_store` path string).
    pub(crate) key: String,
    /// Object size in bytes, from the listing.
    pub(crate) size: u64,
    /// ETag from the listing, if the store reports one.
    pub(crate) etag: Option<String>,
    /// Last-modified time (ms since epoch), used as the records' event time.
    pub(crate) last_modified_ms: i64,
}

/// Messages a fetcher streams to its lane, strictly in stream order.
#[derive(Debug)]
pub(crate) enum ChunkMsg {
    /// The next object is starting.
    ObjectStart {
        /// Ordinal within the lane's slice.
        ordinal: u32,
        /// Object key.
        key: String,
        /// The object's last-modified time (ms since epoch).
        last_modified_ms: i64,
    },
    /// The next bytes of the current object (at most `chunk_bytes` each).
    Chunk(Bytes),
    /// The current object's bytes are complete.
    ObjectEnd,
    /// The lane cannot continue reading its split.
    LaneFailed(SplitFailure),
}

/// How a unit read failed. The classification decides whether one object
/// condemns the unit (poison, failed back to the feed) or the whole
/// pipeline (fatal).
#[derive(Debug)]
pub(crate) enum SplitFailure {
    /// Object-level failure: a member was deleted after listing, was
    /// overwritten under its ETag pin, is corrupt, or exhausted its read
    /// budget. The unit is failed back to the feed; it never fails the
    /// pipeline directly.
    Poison(PoisonKind, String),
    /// Instance- or configuration-level failure (credentials, permissions,
    /// client misconfiguration): terminal for the pipeline.
    Fatal(SourceError),
    /// A notification unit's key was overwritten under its ETag pin. The
    /// records framed before the failure stand, and the reader re-enqueues
    /// the key at the content the HEAD found.
    Overwritten(Box<Overwrite>),
}

impl std::fmt::Display for SplitFailure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SplitFailure::Poison(_, reason) => write!(f, "split poisoned: {reason}"),
            SplitFailure::Fatal(e) => write!(f, "{e}"),
            SplitFailure::Overwritten(o) => write!(f, "\"{}\" was overwritten", o.key),
        }
    }
}

/// The content a key holds after an overwrite cut its read short, as a HEAD
/// reports it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Overwrite {
    /// The key read.
    pub(crate) key: String,
    /// The current ETag, quoted, or `None` when the store sent none.
    pub(crate) etag: Option<String>,
    /// The current size in bytes.
    pub(crate) size: u64,
    /// The current last-modified time (ms since epoch).
    pub(crate) last_modified_ms: i64,
}

/// Collect and sort the full listing under `prefix`.
#[cfg(test)]
pub(crate) async fn list_all(
    store: &Arc<dyn ObjectStore>,
    prefix: Option<&Path>,
) -> Result<Vec<ObjectEntry>, object_store::Error> {
    let mut entries = Vec::new();
    let mut stream = store.list(prefix);
    while let Some(meta) = stream.next().await {
        let meta = meta?;
        entries.push(ObjectEntry {
            key: meta.location.to_string(),
            size: meta.size,
            etag: meta.e_tag,
            last_modified_ms: meta.last_modified.timestamp_millis(),
        });
    }
    entries.sort_unstable_by(|a, b| a.key.cmp(&b.key));
    Ok(entries)
}

/// Everything one fetcher task needs.
pub(crate) struct FetcherParams {
    /// The split being read; names the work in failure reports and logs.
    pub(crate) split: LaneLabel,
    pub(crate) store: Arc<dyn ObjectStore>,
    /// The split's member objects, ordinal order.
    pub(crate) slice: Arc<Vec<ObjectEntry>>,
    /// First ordinal to fetch (the committed watermark's object).
    pub(crate) start_ordinal: u32,
    /// Committed ETag of the resume object, pinning its content.
    pub(crate) resume_etag: Option<String>,
    /// Upper bound on a single [`ChunkMsg::Chunk`].
    pub(crate) chunk_bytes: usize,
    /// Target size of a bounded ranged GET (`prefetch_bytes`); byte-range
    /// boundary windows can add `chunk_bytes`. Each GET is fully buffered
    /// before its chunks are handed on, and the next window's GET runs while
    /// they are.
    pub(crate) range_bytes: usize,
    pub(crate) tx: mpsc::Sender<ChunkMsg>,
    /// Backpressure pause (set by `Source::pause`): checked between sends.
    pub(crate) pause: Arc<AtomicBool>,
    /// First backoff step (doubles per attempt, capped; tests shrink it).
    pub(crate) retry_base: Duration,
    /// `spate_s3_source_get_retries_total`, when metrics are attached.
    pub(crate) retries: Option<spate_core::metrics::Counter>,
    /// The byte range of the slice's one member, for a ranged unit.
    pub(crate) range: Option<SplitRange>,
    /// `spate_s3_source_bytes_read_total`, counting the bytes a ranged read
    /// fetches and trims; the lane counts the bytes it forwards.
    pub(crate) bytes_read: Option<spate_core::metrics::Counter>,
    /// Where the split's unit came from: a notification may name a version
    /// to read, and a failed precondition on one is an overwrite.
    pub(crate) origin: Origin,
}

/// How many attempts one object GET gets before escalating. Failing fast (a
/// poisoned unit) beats a backfill wedged invisibly in an endless retry loop.
pub(crate) const MAX_ATTEMPTS: u32 = 8;
pub(crate) const BACKOFF_CAP: Duration = Duration::from_secs(5);
const PAUSE_POLL: Duration = Duration::from_millis(25);

/// Stream the split's member objects. Ends by dropping `tx` (the lane
/// observes a closed channel = end of input) or after a `LaneFailed`.
pub(crate) async fn run_fetcher(params: FetcherParams) {
    let FetcherParams {
        split,
        store,
        slice,
        start_ordinal,
        resume_etag,
        chunk_bytes,
        range_bytes,
        tx,
        pause,
        retry_base,
        retries,
        range,
        bytes_read,
        origin,
    } = params;
    let mut ahead = ReadAhead::default();

    for (ordinal, entry) in slice.iter().enumerate().skip(start_ordinal as usize) {
        // A unit holds at most `MAX_UNIT_MEMBERS` members, so ordinals always
        // fit the composite-offset layout.
        let ordinal = ordinal as u32;
        // Pin the GET to the exact content the offsets were (or will be)
        // minted against: the committed ETag for the resume object, the
        // listed ETag otherwise.
        let pinned_etag = if ordinal == start_ordinal && resume_etag.is_some() {
            resume_etag.clone()
        } else {
            entry.etag.clone()
        };
        let start = ChunkMsg::ObjectStart {
            ordinal,
            key: entry.key.clone(),
            last_modified_ms: entry.last_modified_ms,
        };
        if !ahead.alongside(gated_send(&pause, &tx, start)).await {
            return; // lane dropped: shutdown
        }
        // The next member's first window is read while this member is handed
        // on. A byte-range unit has one member.
        let next = match range {
            Some(_) => None,
            None => slice
                .iter()
                .skip(ordinal as usize + 1)
                .find(|later| later.size > 0)
                .and_then(|later| {
                    // A later member is never the resume object, so its own
                    // listed ETag pins it. One with no ETag takes the
                    // streaming path, whose body must not open early.
                    let etag = later.etag.clone()?;
                    let path = object_path(&split, later).ok()?;
                    Some(WindowRead {
                        split: Arc::clone(&split),
                        store: Arc::clone(&store),
                        path,
                        entry: later.clone(),
                        etag,
                        start: 0,
                        end: (range_bytes.max(1) as u64).min(later.size),
                        retry_base,
                        retries: retries.clone(),
                        origin: origin.clone(),
                    })
                }),
        };
        match stream_object(
            &split,
            &store,
            entry,
            pinned_etag.as_deref(),
            range,
            range_bytes,
            chunk_bytes,
            &tx,
            &pause,
            retry_base,
            retries.as_ref(),
            bytes_read.as_ref(),
            &origin,
            &mut ahead,
            next,
        )
        .await
        {
            Ok(true) => {}
            Ok(false) => return, // lane dropped: shutdown
            Err(e) => {
                let _ = tx.send(ChunkMsg::LaneFailed(e)).await;
                return;
            }
        }
        if ahead.alongside(tx.send(ChunkMsg::ObjectEnd)).await.is_err() {
            return;
        }
    }
    // Slice exhausted: dropping tx closes the channel, which is the lane's
    // end-of-slice signal.
}

/// Wait while paused. Fails when the lane is gone (channel closed), so a
/// paused fetcher still exits promptly on shutdown.
async fn pause_gate(pause: &AtomicBool, tx: &mpsc::Sender<ChunkMsg>) -> Result<(), ()> {
    while pause.load(Ordering::Relaxed) {
        if tx.is_closed() {
            return Err(());
        }
        // Tokio's own timer, so a frozen test clock cannot hold a paused
        // fetcher.
        tokio::time::sleep(PAUSE_POLL).await;
    }
    Ok(())
}

/// Wait out a pause, then send `msg`. `false` when the lane is gone.
async fn gated_send(pause: &AtomicBool, tx: &mpsc::Sender<ChunkMsg>, msg: ChunkMsg) -> bool {
    pause_gate(pause, tx).await.is_ok() && tx.send(msg).await.is_ok()
}

/// One window's bytes, or the failure that ended its read.
type Window = Result<Bytes, SplitFailure>;

/// One bounded ranged GET of an ETag-pinned object.
struct WindowRead {
    split: LaneLabel,
    store: Arc<dyn ObjectStore>,
    path: Path,
    entry: ObjectEntry,
    etag: String,
    start: u64,
    end: u64,
    retry_base: Duration,
    retries: Option<spate_core::metrics::Counter>,
    origin: Origin,
}

impl WindowRead {
    /// Read the window into memory, retrying a transient failure on a budget
    /// of its own.
    async fn fetch(self) -> Window {
        let mut attempt: u32 = 0;
        loop {
            let options = GetOptions {
                if_match: Some(self.etag.clone()),
                range: Some(GetRange::Bounded(self.start..self.end)),
                version: version_of(&self.origin),
                ..Default::default()
            };
            // The body is drained before the window is handed on, so the
            // connection is released and a back-pressured lane never leaves
            // an idle S3 body open.
            let fetched = match self.store.get_opts(&self.path, options).await {
                Ok(result) => result.bytes().await,
                Err(e) => Err(e),
            };
            match fetched {
                // A conforming store never returns an empty bounded range;
                // guard anyway so a misbehaving one poisons the object rather
                // than spinning.
                Ok(bytes) if bytes.is_empty() => {
                    return Err(SplitFailure::Poison(
                        PoisonKind::Undecodable,
                        format!(
                            "split {}: ranged read of \"{}\" at byte {} returned no bytes",
                            self.split, self.entry.key, self.start
                        ),
                    ));
                }
                Ok(bytes) => return Ok(bytes),
                Err(e) if crate::error::is_overwrite(&e, &self.origin) => {
                    return Err(head_after_overwrite(
                        &self.split,
                        &self.store,
                        &self.path,
                        &self.entry,
                        &self.etag,
                        self.start,
                        self.retry_base,
                        self.retries.as_ref(),
                    )
                    .await);
                }
                Err(e) => {
                    retry_or_fail(
                        &self.split,
                        &self.entry,
                        &e,
                        self.start,
                        &mut attempt,
                        self.retry_base,
                        self.retries.as_ref(),
                    )
                    .await?;
                }
            }
        }
    }
}

/// The one window a fetcher reads ahead: the next window of the object it is
/// handing on, or the first window of the unit's next member.
///
/// The read is a future of the fetcher's own task, polled only inside
/// [`alongside`](Self::alongside) and [`take`](Self::take). Dropping the
/// fetcher cancels it.
#[derive(Default)]
struct ReadAhead(Option<Ahead>);

enum Ahead {
    Reading(Pin<Box<dyn Future<Output = Window> + Send>>),
    Read(Window),
}

impl ReadAhead {
    /// Start reading `read`. At most one window is ahead at a time.
    fn start(&mut self, read: WindowRead) {
        debug_assert!(self.0.is_none(), "a second window read ahead");
        self.0 = Some(Ahead::Reading(Box::pin(read.fetch())));
    }

    /// Run `work` to completion while the window ahead keeps reading.
    async fn alongside<T>(&mut self, work: impl Future<Output = T>) -> T {
        let work = pin!(work);
        let Some(Ahead::Reading(reading)) = &mut self.0 else {
            return work.await;
        };
        match select(work, reading.as_mut()).await {
            Either::Left((out, _)) => out,
            Either::Right((window, work)) => {
                self.0 = Some(Ahead::Read(window));
                work.await
            }
        }
    }

    /// The window read ahead, awaited to the end of its read, or `None` when
    /// none was started.
    async fn take(&mut self) -> Option<Window> {
        Some(match self.0.take()? {
            Ahead::Reading(reading) => reading.await,
            Ahead::Read(window) => window,
        })
    }
}

/// Stream one object's bytes as bounded [`ChunkMsg::Chunk`]s. `Ok(true)` =
/// complete, `Ok(false)` = receiver dropped (shutdown), `Err` = terminal
/// failure.
///
/// When the object's content is pinned by an ETag (S3 always supplies one, as
/// does `object_store`'s `LocalFileSystem`) the read is a sequence of **bounded
/// ranged GETs, each drained fully into memory before any of its chunks is
/// forwarded** ([`stream_object_ranged`]), so a lane parked on backpressure
/// only holds buffered bytes, never an idle GET body that S3 (or an
/// intermediary) could reset or time out mid-stream. Without an ETag a resumed
/// ranged read could splice two object versions, so such stores keep a single
/// continuous stream ([`stream_object_streaming`]).
///
/// `ahead` holds this object's first window when it was read ahead, and `next`
/// is the next member's first window, read while this object's last window is
/// handed on.
#[expect(
    clippy::too_many_arguments,
    reason = "internal seam between run_fetcher and the read loops"
)]
async fn stream_object(
    split: &LaneLabel,
    store: &Arc<dyn ObjectStore>,
    entry: &ObjectEntry,
    pinned_etag: Option<&str>,
    range: Option<SplitRange>,
    range_bytes: usize,
    chunk_bytes: usize,
    tx: &mpsc::Sender<ChunkMsg>,
    pause: &AtomicBool,
    retry_base: Duration,
    retries: Option<&spate_core::metrics::Counter>,
    bytes_read: Option<&spate_core::metrics::Counter>,
    origin: &Origin,
    ahead: &mut ReadAhead,
    next: Option<WindowRead>,
) -> Result<bool, SplitFailure> {
    // A whole object with no bytes is read without a request, so its key
    // need not be one the store can address.
    if entry.size == 0 && range.is_none() {
        return Ok(true);
    }
    match pinned_etag {
        Some(etag) => {
            stream_object_ranged(
                split,
                store,
                entry,
                etag,
                range.map(|range| RangeTrim::new(range, entry.size)),
                range_bytes,
                chunk_bytes,
                tx,
                pause,
                retry_base,
                retries,
                bytes_read,
                origin,
                ahead,
                next,
            )
            .await
        }
        None if range.is_some() => Err(SplitFailure::Fatal(SourceError::Client {
            class: ErrorClass::Fatal,
            reason: format!(
                "split {split}: ranged read of \"{}\" has no ETag pin — internal wiring bug",
                entry.key
            ),
        })),
        None => {
            stream_object_streaming(
                split,
                store,
                entry,
                chunk_bytes,
                tx,
                pause,
                retry_base,
                retries,
                origin,
            )
            .await
        }
    }
}

/// Pinned read path: walk the object in `range_bytes` windows, each a
/// short-lived ranged GET fully buffered before its hand-off. While one window
/// is handed on, the next is read: the object's next window, or `next` after
/// its last. Queued chunks can retain the previous window's buffer, so a lane
/// holds three windows when each spans at least
/// [`LANE_HANDOFF_CHUNKS`](crate::lane_ctx::LANE_HANDOFF_CHUNKS) chunks, and
/// more when windows are shorter. The ETag pin keeps the multi-GET read
/// splice-safe: an overwrite between windows trips the `if_match` precondition.
///
/// A window read ahead that fails is reported only after the window before it
/// is handed on, so the lane sees the same messages as if it had been read
/// later.
///
/// With a `trim`, the windows follow [`RangeTrim::next_window`] and only the
/// owned bytes are forwarded. A window reaching the range's end may run
/// `chunk_bytes` past `range_bytes`.
#[expect(
    clippy::too_many_arguments,
    reason = "internal read loop, mirrors stream_object_streaming"
)]
async fn stream_object_ranged(
    split: &LaneLabel,
    store: &Arc<dyn ObjectStore>,
    entry: &ObjectEntry,
    etag: &str,
    mut trim: Option<RangeTrim>,
    range_bytes: usize,
    chunk_bytes: usize,
    tx: &mpsc::Sender<ChunkMsg>,
    pause: &AtomicBool,
    retry_base: Duration,
    retries: Option<&spate_core::metrics::Counter>,
    bytes_read: Option<&spate_core::metrics::Counter>,
    origin: &Origin,
    ahead: &mut ReadAhead,
    mut next: Option<WindowRead>,
) -> Result<bool, SplitFailure> {
    let path = object_path(split, entry)?;
    let window = range_bytes.max(1) as u64;
    // The end of the window starting at `at`, or `None` past the last one.
    // Saturating: `entry.size` is remote listing data and may be
    // adversarially close to u64::MAX.
    let window_end = |trim: &Option<RangeTrim>, at: u64| match trim {
        Some(trim) => trim.next_window(at, window, chunk_bytes as u64),
        None if at < entry.size => Some(at.saturating_add(window).min(entry.size)),
        None => None,
    };
    let read_at = |start: u64, end: u64| WindowRead {
        split: Arc::clone(split),
        store: Arc::clone(store),
        path: path.clone(),
        entry: entry.clone(),
        etag: etag.to_owned(),
        start,
        end,
        retry_base,
        retries: retries.cloned(),
        origin: origin.clone(),
    };
    // `delivered` is the GET position of the window in hand.
    let mut delivered: u64 = trim.as_ref().map_or(0, RangeTrim::first_byte);
    let mut buffered = match ahead.take().await {
        Some(window) => window?,
        None => match window_end(&trim, delivered) {
            Some(end) => read_at(delivered, end).fetch().await?,
            None => return Ok(true),
        },
    };

    loop {
        let read = buffered.len() as u64;
        let mut bytes = match &mut trim {
            Some(trim) => {
                let owned = trim.feed(delivered, buffered);
                if let Some(counter) = bytes_read {
                    counter.increment(read - owned.len() as u64);
                }
                owned
            }
            None => buffered,
        };
        // The trim is fed before the next window is planned: its end depends
        // on whether this window reached the owned range's end.
        let following = delivered + read;
        let own_next = window_end(&trim, following);
        // A paused lane opens no GET, including one a pause reached while
        // the fetcher waited on the window it now holds.
        if pause_gate(pause, tx).await.is_err() {
            return Ok(false);
        }
        match own_next {
            Some(end) => ahead.start(read_at(following, end)),
            None => {
                if let Some(read) = next.take() {
                    ahead.start(read);
                }
            }
        }
        while !bytes.is_empty() {
            let take = bytes.len().min(chunk_bytes);
            let chunk = bytes.split_to(take);
            if !ahead
                .alongside(gated_send(pause, tx, ChunkMsg::Chunk(chunk)))
                .await
            {
                return Ok(false);
            }
        }
        if own_next.is_none() {
            return Ok(true);
        }
        delivered = following;
        let Some(window) = ahead.take().await else {
            unreachable!("the object's next window was started above");
        };
        buffered = window?;
    }
}

/// Unpinned read path (store reports no ETag): a single continuous stream. A
/// resumed ranged GET across a failure could splice two object versions when
/// the content is not pinned, so a mid-object break is fatal rather than
/// resumed. This holds the body open across the hand-off, and is the
/// fallback for stores that cannot be safely range-resumed; S3 and
/// `LocalFileSystem` always report an ETag and take the ranged path.
#[expect(
    clippy::too_many_arguments,
    reason = "internal read loop, mirrors stream_object_ranged"
)]
async fn stream_object_streaming(
    split: &LaneLabel,
    store: &Arc<dyn ObjectStore>,
    entry: &ObjectEntry,
    chunk_bytes: usize,
    tx: &mpsc::Sender<ChunkMsg>,
    pause: &AtomicBool,
    retry_base: Duration,
    retries: Option<&spate_core::metrics::Counter>,
    origin: &Origin,
) -> Result<bool, SplitFailure> {
    let path = object_path(split, entry)?;
    let mut delivered: u64 = 0;
    let mut attempt: u32 = 0;

    'attempts: loop {
        // In this fallback `delivered` is always 0: a mid-object stream error
        // is fatal below (no ETag to pin a resumed read), so a retry never
        // resumes partway, and every GET re-reads the whole object un-ranged.
        let options = GetOptions {
            version: version_of(origin),
            ..Default::default()
        };
        let result = match store.get_opts(&path, options).await {
            Ok(r) => r,
            Err(e) => {
                retry_or_fail(
                    split,
                    entry,
                    &e,
                    delivered,
                    &mut attempt,
                    retry_base,
                    retries,
                )
                .await?;
                continue 'attempts;
            }
        };
        let mut stream = result.into_stream();
        loop {
            match stream.next().await {
                Some(Ok(mut bytes)) => {
                    delivered += bytes.len() as u64;
                    while !bytes.is_empty() {
                        let take = bytes.len().min(chunk_bytes);
                        let chunk = bytes.split_to(take);
                        if pause_gate(pause, tx).await.is_err() {
                            return Ok(false);
                        }
                        if tx.send(ChunkMsg::Chunk(chunk)).await.is_err() {
                            return Ok(false);
                        }
                    }
                    // Progress resets the attempt budget: an object larger
                    // than one failure-free window still completes.
                    attempt = 0;
                }
                Some(Err(e)) => {
                    if delivered > 0 {
                        // Scope decides, same as `retry_or_fail`: a
                        // credentials/permission/config failure holds for
                        // every object on every instance, so it is
                        // pipeline-fatal even mid-stream. Everything else
                        // stays object-level: without an ETag a resumed read
                        // could splice two object versions, so a mid-object
                        // break cannot be retried by this owner. Poison.
                        if crate::error::is_pipeline_fatal(&e) {
                            return Err(SplitFailure::Fatal(SourceError::Client {
                                class: ErrorClass::Fatal,
                                reason: format!(
                                    "split {split}: reading \"{}\" failed mid-object at byte \
                                     {delivered}: {}",
                                    entry.key,
                                    crate::error::bounded_reason(&e)
                                ),
                            }));
                        }
                        return Err(SplitFailure::Poison(
                            PoisonKind::Undecodable,
                            format!(
                                "split {split}: read of \"{}\" failed mid-object at byte \
                                 {delivered} and the store reports no ETag to pin a \
                                 resumed read to: {}",
                                entry.key,
                                crate::error::bounded_reason(&e)
                            ),
                        ));
                    }
                    retry_or_fail(
                        split,
                        entry,
                        &e,
                        delivered,
                        &mut attempt,
                        retry_base,
                        retries,
                    )
                    .await?;
                    continue 'attempts;
                }
                None => return Ok(true),
            }
        }
    }
}

/// The object version a part from `origin` reads, `None` for the key.
fn version_of(origin: &Origin) -> Option<String> {
    match origin {
        Origin::Notification { version, .. } => version.clone(),
        Origin::Lister => None,
    }
}

/// HEAD `path` after the store refused a read at byte `delivered` pinned to
/// `pinned` (see [`is_overwrite`](crate::error::is_overwrite)), and report
/// what the key holds now.
///
/// A HEAD answering the pinned ETag poisons the unit as `EtagDrift`, since
/// re-enqueueing at the same content would fail the same way. Any other
/// answer, an absent ETag included, is [`SplitFailure::Overwritten`]. A HEAD
/// that fails is classified as a read is: 404 poisons, 401 and 403 are
/// fatal, and a transient failure is retried on the read's budget. Its
/// reason says the HEAD failed.
#[expect(
    clippy::too_many_arguments,
    reason = "internal seam beside the read loops, which carry the same context"
)]
async fn head_after_overwrite(
    split: &LaneLabel,
    store: &Arc<dyn ObjectStore>,
    path: &Path,
    entry: &ObjectEntry,
    pinned: &str,
    delivered: u64,
    retry_base: Duration,
    retries: Option<&spate_core::metrics::Counter>,
) -> SplitFailure {
    let mut attempt = 0;
    loop {
        match store.head(path).await {
            Ok(meta) => {
                let etag = meta.e_tag.map(quoted);
                if etag.as_deref() == Some(pinned) {
                    return SplitFailure::Poison(
                        PoisonKind::EtagDrift,
                        format!(
                            "split {split}: the store refused reading \"{}\" at byte \
                             {delivered} under its ETag pin, and the object still reports the \
                             ETag {pinned}",
                            entry.key
                        ),
                    );
                }
                return SplitFailure::Overwritten(Box::new(Overwrite {
                    key: entry.key.clone(),
                    etag,
                    size: meta.size,
                    last_modified_ms: meta.last_modified.timestamp_millis(),
                }));
            }
            Err(e) => {
                if let Err(failure) = retry_or_fail(
                    split,
                    entry,
                    &e,
                    delivered,
                    &mut attempt,
                    retry_base,
                    retries,
                )
                .await
                {
                    let headed = |reason: String| {
                        format!("{reason} (the HEAD after the store refused the pinned read)")
                    };
                    return match failure {
                        SplitFailure::Poison(kind, reason) => {
                            SplitFailure::Poison(kind, headed(reason))
                        }
                        SplitFailure::Fatal(SourceError::Client { class, reason }) => {
                            SplitFailure::Fatal(SourceError::Client {
                                class,
                                reason: headed(reason),
                            })
                        }
                        other => other,
                    };
                }
            }
        }
    }
}

/// The store path of a listed object, under the exact key the listing gave.
///
/// `Path::from` would read the key as unencoded parts and percent-encode `%`,
/// `#`, `[`, non-ASCII and other reserved bytes, addressing a different
/// object. A key that is not [`addressable`](crate::listing::addressable)
/// cannot be read, or would be read under another key, and poisons the unit.
fn object_path(split: &LaneLabel, entry: &ObjectEntry) -> Result<Path, SplitFailure> {
    match Path::parse(&entry.key) {
        Ok(path) if path.as_ref() == entry.key => Ok(path),
        parsed => {
            let why = parsed.map_or_else(
                |e| e.to_string(),
                |path| format!("the object store would read \"{path}\" in its place"),
            );
            Err(SplitFailure::Poison(
                PoisonKind::NotFound,
                format!(
                    "split {split}: \"{}\" is not an object key: {why}",
                    entry.key
                ),
            ))
        }
    }
}

/// Back off and bump the attempt counter for a retryable error; escalate
/// non-retryable classes and exhausted budgets.
///
/// Escalation splits by scope: credentials/config failures are pipeline
/// [`Fatal`](SplitFailure::Fatal). Everything else is object-level
/// [`Poison`](SplitFailure::Poison), handled by handing the split back: a
/// deleted or overwritten object (`NotFound`, a failed `if_match`
/// precondition), or a read that keeps failing past the attempt budget.
async fn retry_or_fail(
    split: &LaneLabel,
    entry: &ObjectEntry,
    e: &object_store::Error,
    delivered: u64,
    attempt: &mut u32,
    retry_base: Duration,
    retries: Option<&spate_core::metrics::Counter>,
) -> Result<(), SplitFailure> {
    if classify(e) != ErrorClass::Retryable {
        let reason = format!(
            "split {split}: reading \"{}\" failed at byte {delivered}: {}",
            entry.key,
            crate::error::bounded_reason(e)
        );
        return Err(if crate::error::is_pipeline_fatal(e) {
            SplitFailure::Fatal(SourceError::Client {
                class: ErrorClass::Fatal,
                reason,
            })
        } else {
            SplitFailure::Poison(crate::error::poison_kind(e), reason)
        });
    }
    *attempt += 1;
    if *attempt >= MAX_ATTEMPTS {
        return Err(SplitFailure::Poison(
            PoisonKind::RetriesExhausted,
            format!(
                "split {split}: reading \"{}\" still failing at byte {delivered} after \
             {MAX_ATTEMPTS} attempts: {}",
                entry.key,
                crate::error::bounded_reason(e)
            ),
        ));
    }
    let backoff = retry_base
        .saturating_mul(1 << (*attempt - 1).min(16))
        .min(BACKOFF_CAP);
    tracing::warn!(
        split = %split,
        key = %entry.key,
        attempt = *attempt,
        delivered,
        error = %crate::error::bounded_reason(e),
        "transient object read failure; backing off and resuming with a ranged get"
    );
    // Tokio's own timer, so a frozen test clock cannot hold a retrying
    // read.
    tokio::time::sleep(backoff).await;
    // Counted once the backoff ends, so a fetcher aborted while it waits
    // counts no attempt it never made.
    if let Some(c) = retries {
        c.increment(1);
    }
    Ok(())
}

#[expect(
    clippy::disallowed_methods,
    reason = "a fetcher task ends when the test aborts it or the test's runtime drops"
)]
#[cfg(test)]
mod tests {
    use super::*;
    use object_store::memory::InMemory;
    use object_store::{GetResult, GetResultPayload, PutPayload};
    use std::fmt;
    use std::sync::atomic::AtomicU32;
    use std::sync::atomic::AtomicU64;
    use std::sync::mpsc as std_mpsc;

    fn entry(key: &str, size: u64, etag: Option<&str>) -> ObjectEntry {
        ObjectEntry {
            key: key.into(),
            size,
            etag: etag.map(str::to_owned),
            last_modified_ms: 0,
        }
    }

    // ---------------------------------------------------- fetcher tests --

    /// The shape of a `GetOptions::range`, recorded so tests can assert which
    /// read path ran: the pinned path issues `Bounded` windows, the streaming
    /// fallback a single un-ranged (`Full`) or `Offset` resume GET.
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum RangeKind {
        Full,
        Offset(u64),
        Bounded(u64, u64),
    }

    fn range_kind(range: &Option<GetRange>) -> RangeKind {
        match range {
            None => RangeKind::Full,
            Some(GetRange::Offset(o)) => RangeKind::Offset(*o),
            Some(GetRange::Bounded(r)) => RangeKind::Bounded(r.start, r.end),
            Some(GetRange::Suffix(_)) => unreachable!("the fetcher never issues a suffix range"),
        }
    }

    /// Wraps a store, failing the first `fail_gets` `get_opts` calls with a
    /// retryable error, failing the first `fail_past_gets` bounded GETs that
    /// end past byte `fail_past` the same way, and cutting the first
    /// `cut_streams` result streams after `cut_after` bytes (with a retryable
    /// error, or `PermissionDenied` when `cut_fatal` is set). `error_text`,
    /// when set, replaces the injected errors' text. Records every requested
    /// range in `ranges` for read-path assertions.
    #[derive(Debug)]
    struct FlakyStore {
        inner: InMemory,
        fail_gets: AtomicU32,
        fail_past: u64,
        fail_past_gets: AtomicU32,
        cut_streams: AtomicU32,
        cut_after: usize,
        cut_fatal: bool,
        error_text: Option<String>,
        gets: AtomicU32,
        ranges: std::sync::Mutex<Vec<RangeKind>>,
        /// Payload streams drained to completion (a cut or abandoned body
        /// never counts). This is the observable that separates "window
        /// fully buffered before hand-off" from "connection held open".
        bodies_drained: Arc<AtomicU32>,
        /// Each GET's key and `If-Match`, in request order.
        requests: std::sync::Mutex<Vec<(String, Option<String>)>>,
        /// The GET, counted from 1, that waits for a permit on `release`
        /// before it reads; 0 holds none.
        hold_on: u32,
        release: Arc<tokio::sync::Semaphore>,
        /// The GET, counted from 1, answered with an empty body; 0 answers
        /// none.
        empty_on: u32,
        /// Held GETs started, and held GET futures dropped, finished or not.
        held: AtomicU32,
        held_dropped: Arc<AtomicU32>,
        /// Window buffers alive now and the most alive at once, counted when
        /// `count_live` is set.
        count_live: bool,
        live: Arc<AtomicU32>,
        max_live: Arc<AtomicU32>,
    }

    /// Counts its own drop.
    struct DropCount(Arc<AtomicU32>);

    impl Drop for DropCount {
        fn drop(&mut self) {
            self.0.fetch_add(1, Ordering::Relaxed);
        }
    }

    /// A window's bytes, counted alive until the last chunk sharing them is
    /// dropped.
    struct LiveWindow {
        bytes: Bytes,
        live: Arc<AtomicU32>,
    }

    impl AsRef<[u8]> for LiveWindow {
        fn as_ref(&self) -> &[u8] {
            &self.bytes
        }
    }

    impl Drop for LiveWindow {
        fn drop(&mut self) {
            self.live.fetch_sub(1, Ordering::Relaxed);
        }
    }

    impl FlakyStore {
        fn new(inner: InMemory) -> FlakyStore {
            FlakyStore {
                inner,
                fail_gets: AtomicU32::new(0),
                fail_past: u64::MAX,
                fail_past_gets: AtomicU32::new(0),
                cut_streams: AtomicU32::new(0),
                cut_after: 0,
                cut_fatal: false,
                error_text: None,
                gets: AtomicU32::new(0),
                ranges: std::sync::Mutex::new(Vec::new()),
                bodies_drained: Arc::new(AtomicU32::new(0)),
                requests: std::sync::Mutex::new(Vec::new()),
                hold_on: 0,
                empty_on: 0,
                release: Arc::new(tokio::sync::Semaphore::new(0)),
                held: AtomicU32::new(0),
                held_dropped: Arc::new(AtomicU32::new(0)),
                count_live: false,
                live: Arc::new(AtomicU32::new(0)),
                max_live: Arc::new(AtomicU32::new(0)),
            }
        }

        /// Each GET's key and `If-Match`, in request order.
        fn requests(&self) -> Vec<(String, Option<String>)> {
            self.requests.lock().unwrap().clone()
        }

        fn bodies_drained(&self) -> u32 {
            self.bodies_drained.load(Ordering::Relaxed)
        }

        /// Chain an end-of-stream marker onto the payload: it fires only
        /// when the body is polled all the way to its natural end.
        fn track_drain(&self, result: GetResult) -> GetResult {
            let GetResult {
                payload,
                meta,
                range,
                attributes,
                extensions,
            } = result;
            let stream = match payload {
                GetResultPayload::Stream(s) => s,
                #[allow(unreachable_patterns)]
                _ => unreachable!("InMemory yields streams"),
            };
            let stream = if self.count_live {
                let live = Arc::clone(&self.live);
                let max_live = Arc::clone(&self.max_live);
                stream
                    .map(move |item| {
                        item.map(|bytes| {
                            let now = live.fetch_add(1, Ordering::Relaxed) + 1;
                            max_live.fetch_max(now, Ordering::Relaxed);
                            Bytes::from_owner(LiveWindow {
                                bytes,
                                live: Arc::clone(&live),
                            })
                        })
                    })
                    .boxed()
            } else {
                stream
            };
            let drained = Arc::clone(&self.bodies_drained);
            let marker = futures_util::stream::poll_fn(move |_| {
                drained.fetch_add(1, Ordering::Relaxed);
                std::task::Poll::Ready(None::<object_store::Result<Bytes>>)
            });
            GetResult {
                payload: GetResultPayload::Stream(Box::pin(stream.chain(marker))),
                meta,
                range,
                attributes,
                extensions,
            }
        }

        fn recorded_ranges(&self) -> Vec<RangeKind> {
            self.ranges.lock().unwrap().clone()
        }

        fn generic(what: &str) -> object_store::Error {
            object_store::Error::Generic {
                store: "flaky",
                source: what.to_owned().into(),
            }
        }

        fn cut_error(fatal: bool, text: Option<&str>) -> object_store::Error {
            if fatal {
                object_store::Error::PermissionDenied {
                    path: "k".into(),
                    source: text.unwrap_or("injected permission loss").to_owned().into(),
                }
            } else {
                Self::generic(text.unwrap_or("injected stream cut"))
            }
        }
    }

    impl fmt::Display for FlakyStore {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "FlakyStore")
        }
    }

    #[async_trait::async_trait]
    impl ObjectStore for FlakyStore {
        async fn put_opts(
            &self,
            location: &Path,
            payload: PutPayload,
            opts: object_store::PutOptions,
        ) -> object_store::Result<object_store::PutResult> {
            self.inner.put_opts(location, payload, opts).await
        }

        async fn put_multipart_opts(
            &self,
            location: &Path,
            opts: object_store::PutMultipartOptions,
        ) -> object_store::Result<Box<dyn object_store::MultipartUpload>> {
            self.inner.put_multipart_opts(location, opts).await
        }

        async fn get_opts(
            &self,
            location: &Path,
            options: GetOptions,
        ) -> object_store::Result<GetResult> {
            let nth = self.gets.fetch_add(1, Ordering::Relaxed) + 1;
            self.ranges.lock().unwrap().push(range_kind(&options.range));
            self.requests
                .lock()
                .unwrap()
                .push((location.to_string(), options.if_match.clone()));
            if nth == self.hold_on {
                self.held.fetch_add(1, Ordering::Relaxed);
                let _dropped = DropCount(Arc::clone(&self.held_dropped));
                self.release.acquire().await.unwrap().forget();
            }
            if self
                .fail_gets
                .try_update(Ordering::Relaxed, Ordering::Relaxed, |n| n.checked_sub(1))
                .is_ok()
            {
                return Err(Self::generic(
                    self.error_text.as_deref().unwrap_or("injected get failure"),
                ));
            }
            if let Some(GetRange::Bounded(r)) = &options.range
                && r.end > self.fail_past
                && self
                    .fail_past_gets
                    .try_update(Ordering::Relaxed, Ordering::Relaxed, |n| n.checked_sub(1))
                    .is_ok()
            {
                return Err(Self::generic(
                    self.error_text
                        .as_deref()
                        .unwrap_or("injected get failure past the range end"),
                ));
            }
            let result = self.inner.get_opts(location, options).await?;
            if nth == self.empty_on {
                return Ok(GetResult {
                    payload: GetResultPayload::Stream(Box::pin(futures_util::stream::empty())),
                    ..result
                });
            }
            if self
                .cut_streams
                .try_update(Ordering::Relaxed, Ordering::Relaxed, |n| n.checked_sub(1))
                .is_ok()
            {
                let cut_after = self.cut_after;
                let cut_fatal = self.cut_fatal;
                let error_text = self.error_text.clone();
                let GetResult {
                    payload,
                    meta,
                    range,
                    attributes,
                    extensions,
                } = result;
                let inner_stream = match payload {
                    GetResultPayload::Stream(s) => s,
                    #[allow(unreachable_patterns)]
                    _ => unreachable!("InMemory yields streams"),
                };
                let mut sent = 0usize;
                let cut = inner_stream.flat_map(move |item| {
                    let out: Vec<object_store::Result<Bytes>> = match item {
                        Ok(bytes) => {
                            if sent >= cut_after {
                                vec![Err(Self::cut_error(cut_fatal, error_text.as_deref()))]
                            } else {
                                let take = bytes.len().min(cut_after - sent);
                                sent += take;
                                let mut out = vec![Ok(bytes.slice(0..take))];
                                if sent >= cut_after {
                                    out.push(Err(Self::cut_error(
                                        cut_fatal,
                                        error_text.as_deref(),
                                    )));
                                }
                                out
                            }
                        }
                        Err(e) => vec![Err(e)],
                    };
                    futures_util::stream::iter(out)
                });
                return Ok(GetResult {
                    payload: GetResultPayload::Stream(Box::pin(cut)),
                    meta,
                    range,
                    attributes,
                    extensions,
                });
            }
            Ok(self.track_drain(result))
        }

        fn delete_stream(
            &self,
            locations: futures_util::stream::BoxStream<'static, object_store::Result<Path>>,
        ) -> futures_util::stream::BoxStream<'static, object_store::Result<Path>> {
            self.inner.delete_stream(locations)
        }

        fn list(
            &self,
            prefix: Option<&Path>,
        ) -> futures_util::stream::BoxStream<'static, object_store::Result<object_store::ObjectMeta>>
        {
            self.inner.list(prefix)
        }

        async fn list_with_delimiter(
            &self,
            prefix: Option<&Path>,
        ) -> object_store::Result<object_store::ListResult> {
            self.inner.list_with_delimiter(prefix).await
        }

        async fn copy_opts(
            &self,
            from: &Path,
            to: &Path,
            options: object_store::CopyOptions,
        ) -> object_store::Result<()> {
            self.inner.copy_opts(from, to, options).await
        }
    }

    async fn seeded(objects: &[(&str, &[u8])]) -> InMemory {
        let store = InMemory::new();
        for (key, body) in objects {
            store
                .put(&Path::from(*key), PutPayload::from(body.to_vec()))
                .await
                .unwrap();
        }
        store
    }

    async fn listed(store: &Arc<dyn ObjectStore>) -> Vec<ObjectEntry> {
        list_all(store, None).await.unwrap()
    }

    /// Drive a fetcher over `slice` and collect its message stream.
    async fn collect_fetch(
        store: Arc<dyn ObjectStore>,
        slice: Vec<ObjectEntry>,
        start_ordinal: u32,
        chunk_bytes: usize,
        range_bytes: usize,
    ) -> Vec<ChunkMsg> {
        let (tx, mut rx) = mpsc::channel(64);
        let params = FetcherParams {
            split: "s3-test".into(),
            store,
            slice: Arc::new(slice),
            start_ordinal,
            resume_etag: None,
            chunk_bytes,
            range_bytes,
            tx,
            pause: Arc::new(AtomicBool::new(false)),
            retry_base: Duration::from_millis(1),
            retries: None,
            range: None,
            bytes_read: None,
            origin: Origin::Lister,
        };
        let task = tokio::spawn(run_fetcher(params));
        let mut msgs = Vec::new();
        while let Some(m) = rx.recv().await {
            msgs.push(m);
        }
        task.await.unwrap();
        msgs
    }

    fn assembled(msgs: &[ChunkMsg]) -> Vec<(String, Vec<u8>)> {
        let mut out: Vec<(String, Vec<u8>)> = Vec::new();
        for m in msgs {
            match m {
                ChunkMsg::ObjectStart { key, .. } => out.push((key.clone(), Vec::new())),
                ChunkMsg::Chunk(b) => out.last_mut().unwrap().1.extend_from_slice(b),
                ChunkMsg::ObjectEnd => {}
                ChunkMsg::LaneFailed(e) => panic!("lane failed: {e}"),
            }
        }
        out
    }

    /// Listed keys a read must address verbatim. `Path::from` percent-encodes
    /// every one except `dir/a b.ndjson`.
    const ENCODED_KEYS: [&str; 4] = [
        "dir/a b.ndjson",
        "dir/a%20b.ndjson",
        "dir/x#y[1].ndjson",
        "dir/\u{e9}.ndjson",
    ];

    async fn seeded_verbatim(keys: &[&str]) -> Arc<dyn ObjectStore> {
        let store = InMemory::new();
        for key in keys {
            store
                .put(
                    &Path::parse(*key).unwrap(),
                    PutPayload::from(key.as_bytes().to_vec()),
                )
                .await
                .unwrap();
        }
        Arc::new(store)
    }

    /// Each key with its body (the key's bytes), sorted as the listing is.
    fn verbatim_bodies() -> Vec<(String, Vec<u8>)> {
        let mut want: Vec<_> = ENCODED_KEYS
            .iter()
            .map(|k| ((*k).to_owned(), k.as_bytes().to_vec()))
            .collect();
        want.sort();
        want
    }

    /// Pins that the fetcher starts the next member's GET while its lane has
    /// not read the previous member.
    #[tokio::test(start_paused = true)]
    async fn the_next_members_get_starts_before_the_lane_reads() {
        let store = Arc::new(FlakyStore::new(
            seeded(&[("a.ndjson", b"a\n"), ("b.ndjson", b"b\n")]).await,
        ));
        let slice = listed(&(Arc::clone(&store) as Arc<dyn ObjectStore>)).await;
        assert!(slice.iter().all(|e| e.etag.is_some()), "the ranged path");
        let (tx, _rx) = mpsc::channel(crate::lane_ctx::LANE_HANDOFF_CHUNKS);
        let params = FetcherParams {
            split: "s3-test".into(),
            store: Arc::clone(&store) as Arc<dyn ObjectStore>,
            slice: Arc::new(slice),
            start_ordinal: 0,
            resume_etag: None,
            chunk_bytes: 64,
            range_bytes: 64,
            tx,
            pause: Arc::new(AtomicBool::new(false)),
            retry_base: Duration::from_millis(1),
            retries: None,
            range: None,
            bytes_read: None,
            origin: Origin::Lister,
        };
        let task = tokio::spawn(run_fetcher(params));
        park().await;
        assert_eq!(
            store.gets.load(Ordering::Relaxed),
            2,
            "the second member's GET started with nothing read"
        );
        task.abort();
    }

    /// A ranged read fetches each listed object under the key the listing
    /// gave, whatever characters it holds. Regression for #1081.
    #[tokio::test]
    async fn ranged_read_fetches_the_listed_key_verbatim() {
        let store = seeded_verbatim(&ENCODED_KEYS).await;
        let slice = listed(&store).await;
        assert!(slice.iter().all(|e| e.etag.is_some()));
        let got = assembled(&collect_fetch(store, slice, 0, 4, 64).await);
        assert_eq!(got, verbatim_bodies());
    }

    /// The no-ETag streaming read fetches each listed object under the key
    /// the listing gave. Regression for #1081.
    #[tokio::test]
    async fn streaming_read_fetches_the_listed_key_verbatim() {
        let store = seeded_verbatim(&ENCODED_KEYS).await;
        let mut slice = listed(&store).await;
        for entry in &mut slice {
            entry.etag = None;
        }
        let got = assembled(&collect_fetch(store, slice, 0, 4, 64).await);
        assert_eq!(got, verbatim_bodies());
    }

    /// A key that is not a valid store path poisons the unit as `NotFound`
    /// on both read paths, before any GET, and does not fail the pipeline.
    #[tokio::test]
    async fn an_unparseable_key_poisons_the_split() {
        for etag in [Some("\"e\""), None] {
            let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
            let slice = vec![entry("dir//x.ndjson", 1, etag)];
            let msgs = collect_fetch(store, slice, 0, 4, 64).await;
            let poison = msgs.iter().find_map(|m| match m {
                ChunkMsg::LaneFailed(SplitFailure::Poison(kind, reason)) => {
                    Some((*kind, reason.clone()))
                }
                _ => None,
            });
            let (kind, reason) = poison.expect("an unparseable key is split poison");
            assert_eq!(kind, PoisonKind::NotFound);
            assert!(
                reason.contains("\"dir//x.ndjson\" is not an object key"),
                "{reason}"
            );
        }
    }

    /// A key that parses to a different key poisons the unit rather than
    /// reading the object the parsed key names. Regression for #1116.
    #[tokio::test]
    async fn a_key_the_store_would_rewrite_poisons_the_split() {
        let store: Arc<dyn ObjectStore> = Arc::new(seeded(&[("p/a", b"other\n")]).await);
        let msgs = collect_fetch(store, vec![entry("/p/a", 6, None)], 0, 4, 64).await;
        assert!(
            !msgs.iter().any(|m| matches!(m, ChunkMsg::Chunk(_))),
            "no bytes of p/a are read for /p/a"
        );
        let reason = msgs.iter().find_map(|m| match m {
            ChunkMsg::LaneFailed(SplitFailure::Poison(PoisonKind::NotFound, reason)) => {
                Some(reason.clone())
            }
            _ => None,
        });
        let reason = reason.expect("a rewritten key is not_found split poison");
        assert!(reason.contains("\"/p/a\" is not an object key"), "{reason}");
        assert!(reason.contains("\"p/a\" in its place"), "{reason}");
    }

    #[tokio::test]
    async fn streams_objects_in_order_with_bounded_chunks() {
        let store: Arc<dyn ObjectStore> =
            Arc::new(seeded(&[("p/a", b"aaaaaaaaaa"), ("p/b", b"bb")]).await);
        let slice = listed(&store).await;
        let msgs = collect_fetch(store, slice, 0, 4, 64).await;
        for m in &msgs {
            if let ChunkMsg::Chunk(b) = m {
                assert!(b.len() <= 4, "chunk over the bound: {}", b.len());
            }
        }
        assert_eq!(
            assembled(&msgs),
            vec![
                ("p/a".to_string(), b"aaaaaaaaaa".to_vec()),
                ("p/b".to_string(), b"bb".to_vec())
            ]
        );
    }

    #[tokio::test]
    async fn start_ordinal_skips_committed_objects() {
        let flaky = Arc::new(FlakyStore::new(
            seeded(&[("p/a", b"first"), ("p/b", b"second")]).await,
        ));
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let slice = listed(&store).await;
        let msgs = collect_fetch(store, slice, 1, 64, 64).await;
        assert_eq!(
            assembled(&msgs),
            vec![("p/b".to_string(), b"second".to_vec())]
        );
        let keys: Vec<String> = flaky.requests().into_iter().map(|(k, _)| k).collect();
        assert_eq!(keys, ["p/b"], "a skipped member is never read");
    }

    #[tokio::test]
    async fn transient_get_failures_are_retried() {
        let flaky = FlakyStore::new(seeded(&[("p/a", b"payload")]).await);
        flaky.fail_gets.store(2, Ordering::Relaxed);
        let store: Arc<dyn ObjectStore> = Arc::new(flaky);
        let slice = listed(&store).await;
        let msgs = collect_fetch(store, slice, 0, 64, 64).await;
        assert_eq!(
            assembled(&msgs),
            vec![("p/a".to_string(), b"payload".to_vec())]
        );
    }

    #[tokio::test]
    async fn mid_stream_cut_resumes_with_a_ranged_get_without_gap_or_dup() {
        let body = b"0123456789abcdefghij";
        let flaky = FlakyStore {
            cut_after: 7,
            ..FlakyStore::new(seeded(&[("p/a", body)]).await)
        };
        flaky.cut_streams.store(1, Ordering::Relaxed);
        let store: Arc<dyn ObjectStore> = Arc::new(flaky);
        let slice = listed(&store).await;
        // range_bytes 8 → the 20-byte object spans three windows; the cut lands
        // inside the first, which is re-read whole (nothing was forwarded yet).
        let msgs = collect_fetch(store, slice, 0, 4, 8).await;
        assert_eq!(
            assembled(&msgs),
            vec![("p/a".to_string(), body.to_vec())],
            "a window that fails mid-read is retried cleanly, without gap or dup"
        );
    }

    #[tokio::test]
    async fn missing_object_poisons_the_split() {
        let store: Arc<dyn ObjectStore> = Arc::new(seeded(&[("p/a", b"x")]).await);
        // A slice naming a key that does not exist (deleted after planning);
        // an ETag from the stale listing routes it through the ranged path.
        let slice = vec![entry("p/ghost", 1, Some("\"e\""))];
        let (tx, mut rx) = mpsc::channel(8);
        let params = FetcherParams {
            split: "s3-test".into(),
            store,
            slice: Arc::new(slice),
            start_ordinal: 0,
            resume_etag: None,
            chunk_bytes: 64,
            range_bytes: 64,
            tx,
            pause: Arc::new(AtomicBool::new(false)),
            retry_base: Duration::from_millis(1),
            retries: None,
            range: None,
            bytes_read: None,
            origin: Origin::Lister,
        };
        tokio::spawn(run_fetcher(params));
        let mut saw_poison = false;
        while let Some(m) = rx.recv().await {
            if let ChunkMsg::LaneFailed(failure) = m {
                let SplitFailure::Poison(kind, reason) = failure else {
                    panic!("a missing object is split poison, not pipeline-fatal: {failure}");
                };
                assert_eq!(kind, PoisonKind::NotFound);
                assert!(reason.contains("p/ghost"), "{reason}");
                saw_poison = true;
            }
        }
        assert!(saw_poison);
    }

    #[tokio::test]
    async fn stale_etag_pin_poisons_the_split() {
        let store: Arc<dyn ObjectStore> = Arc::new(seeded(&[("p/a", b"new content")]).await);
        let mut slice = listed(&store).await;
        slice[0].etag = Some("\"stale\"".into());
        let (tx, mut rx) = mpsc::channel(8);
        let params = FetcherParams {
            split: "s3-test".into(),
            store,
            slice: Arc::new(slice),
            start_ordinal: 0,
            resume_etag: None,
            chunk_bytes: 64,
            range_bytes: 64,
            tx,
            pause: Arc::new(AtomicBool::new(false)),
            retry_base: Duration::from_millis(1),
            retries: None,
            range: None,
            bytes_read: None,
            origin: Origin::Lister,
        };
        tokio::spawn(run_fetcher(params));
        let mut saw_poison = false;
        while let Some(m) = rx.recv().await {
            if let ChunkMsg::LaneFailed(failure) = m {
                assert!(
                    matches!(failure, SplitFailure::Poison(PoisonKind::EtagDrift, _)),
                    "an overwrite under the pin is split poison: {failure}"
                );
                saw_poison = true;
            }
        }
        assert!(saw_poison, "a failed precondition must poison the split");
    }

    #[tokio::test(start_paused = true)]
    async fn pause_halts_fetching_and_resume_continues() {
        let flaky = Arc::new(FlakyStore::new(seeded(&[("p/a", b"abcdefgh")]).await));
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let slice = listed(&store).await;
        let (tx, mut rx) = mpsc::channel(1); // tiny: sends interleave with pauses
        let pause = Arc::new(AtomicBool::new(true));
        let params = FetcherParams {
            split: "s3-test".into(),
            store,
            slice: Arc::new(slice),
            start_ordinal: 0,
            resume_etag: None,
            chunk_bytes: 2,
            range_bytes: 64,
            tx,
            pause: Arc::clone(&pause),
            retry_base: Duration::from_millis(1),
            retries: None,
            range: None,
            bytes_read: None,
            origin: Origin::Lister,
        };
        tokio::spawn(run_fetcher(params));
        // Paused before the first send: nothing arrives. On paused time the
        // sleep returns only once the fetcher is parked.
        tokio::time::sleep(Duration::from_millis(80)).await;
        assert!(
            rx.try_recv().is_err(),
            "paused fetcher must not deliver messages"
        );
        assert_eq!(flaky.gets.load(Ordering::Relaxed), 0, "nor read ahead");
        pause.store(false, Ordering::Relaxed);
        let mut msgs = Vec::new();
        while let Some(m) = rx.recv().await {
            msgs.push(m);
        }
        assert_eq!(
            assembled(&msgs),
            vec![("p/a".to_string(), b"abcdefgh".to_vec())]
        );
    }

    #[tokio::test]
    async fn dropping_the_lane_ends_the_fetcher() {
        let store: Arc<dyn ObjectStore> = Arc::new(seeded(&[("p/a", &[b'x'; 4096][..])]).await);
        let slice = listed(&store).await;
        let (tx, rx) = mpsc::channel(1);
        let params = FetcherParams {
            split: "s3-test".into(),
            store,
            slice: Arc::new(slice),
            start_ordinal: 0,
            resume_etag: None,
            chunk_bytes: 8,
            range_bytes: 4096,
            tx,
            pause: Arc::new(AtomicBool::new(false)),
            retry_base: Duration::from_millis(1),
            retries: None,
            range: None,
            bytes_read: None,
            origin: Origin::Lister,
        };
        let task = tokio::spawn(run_fetcher(params));
        drop(rx);
        tokio::time::timeout(Duration::from_secs(5), task)
            .await
            .expect("fetcher must end promptly when the lane is dropped")
            .unwrap();
    }

    #[tokio::test]
    async fn pinned_reads_walk_the_object_in_bounded_contiguous_windows() {
        // 20-byte object, 8-byte windows → 0..8, 8..16, 16..20.
        let flaky = Arc::new(FlakyStore::new(
            seeded(&[("p/a", b"0123456789abcdefghij")]).await,
        ));
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let slice = listed(&store).await;
        assert!(
            slice[0].etag.is_some(),
            "InMemory reports an ETag → pinned path"
        );
        let msgs = collect_fetch(Arc::clone(&store), slice, 0, 4, 8).await;
        assert_eq!(
            assembled(&msgs),
            vec![("p/a".to_string(), b"0123456789abcdefghij".to_vec())]
        );
        assert_eq!(
            flaky.recorded_ranges(),
            vec![
                RangeKind::Bounded(0, 8),
                RangeKind::Bounded(8, 16),
                RangeKind::Bounded(16, 20),
            ],
            "windows must be bounded, contiguous, and cover the object exactly"
        );
    }

    const DEPTH: usize = crate::lane_ctx::LANE_HANDOFF_CHUNKS;

    /// Fetcher parameters over a whole-object unit, unpaused, with no
    /// metrics.
    fn fetcher(
        store: Arc<dyn ObjectStore>,
        slice: Vec<ObjectEntry>,
        chunk_bytes: usize,
        range_bytes: usize,
        tx: mpsc::Sender<ChunkMsg>,
    ) -> FetcherParams {
        FetcherParams {
            split: "s3-test".into(),
            store,
            slice: Arc::new(slice),
            start_ordinal: 0,
            resume_etag: None,
            chunk_bytes,
            range_bytes,
            tx,
            pause: Arc::new(AtomicBool::new(false)),
            retry_base: Duration::from_millis(1),
            retries: None,
            range: None,
            bytes_read: None,
            origin: Origin::Lister,
        }
    }

    /// Returns once every task is idle: on paused time the sleep fires only
    /// when nothing else can run.
    async fn park() {
        tokio::time::sleep(Duration::from_millis(80)).await;
    }

    async fn drain(rx: &mut mpsc::Receiver<ChunkMsg>) -> Vec<ChunkMsg> {
        let mut msgs = Vec::new();
        while let Some(m) = rx.recv().await {
            msgs.push(m);
        }
        msgs
    }

    fn debugged(msgs: &[ChunkMsg]) -> Vec<String> {
        msgs.iter().map(|m| format!("{m:?}")).collect()
    }

    /// Pins that a lane blocked on its hand-off holds the window it is
    /// handing on and the next one, each drained in full, and starts no
    /// third. Regression for #877.
    #[tokio::test(start_paused = true)]
    async fn a_blocked_lane_holds_two_windows_and_fetches_no_further() {
        let flaky = Arc::new(FlakyStore::new(seeded(&[("p/a", &[b'x'; 48][..])]).await));
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let slice = listed(&store).await;
        let (tx, _rx) = mpsc::channel(DEPTH);
        let task = tokio::spawn(run_fetcher(fetcher(store, slice, 2, 16, tx)));
        park().await;
        assert_eq!(
            flaky.recorded_ranges(),
            vec![RangeKind::Bounded(0, 16), RangeKind::Bounded(16, 32)],
            "the second window is read while the first is handed on"
        );
        // The drain marker fires only when a body is read to its end, so a
        // read-ahead that held its body open fails here.
        assert_eq!(flaky.bodies_drained(), 2);
        task.abort();
    }

    /// Pins that the next member's first window is read, under that
    /// member's own ETag, while the lane has not taken the last window of
    /// the member before it, and that the lane receives the same messages as
    /// a lane that never blocks. Regression for #877.
    #[tokio::test(start_paused = true)]
    async fn the_next_members_get_starts_while_the_last_window_is_queued() {
        let flaky = Arc::new(FlakyStore::new(
            seeded(&[("p/a", &[b'a'; 16][..]), ("p/b", &[b'b'; 12][..])]).await,
        ));
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let slice = listed(&store).await;
        assert_ne!(slice[0].etag, slice[1].etag);
        let (tx, mut rx) = mpsc::channel(DEPTH);
        let task = tokio::spawn(run_fetcher(fetcher(
            Arc::clone(&store),
            slice.clone(),
            2,
            16,
            tx,
        )));
        park().await;
        assert_eq!(
            flaky.requests(),
            vec![
                ("p/a".to_string(), slice[0].etag.clone()),
                ("p/b".to_string(), slice[1].etag.clone()),
            ]
        );
        let msgs = drain(&mut rx).await;
        task.await.unwrap();
        let unblocked = collect_fetch(store, slice, 0, 2, 16).await;
        assert_eq!(debugged(&msgs), debugged(&unblocked));
    }

    /// Pins that the read-ahead skips an empty member, which is read without
    /// a request, and reads the next member that has bytes. Its
    /// `ObjectStart`/`ObjectEnd` pair still reaches the lane in place.
    #[tokio::test(start_paused = true)]
    async fn a_zero_byte_member_is_skipped_by_the_read_ahead() {
        let flaky = Arc::new(FlakyStore::new(
            seeded(&[
                ("p/a", &[b'a'; 16][..]),
                ("p/b", b""),
                ("p/c", &[b'c'; 12][..]),
            ])
            .await,
        ));
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let slice = listed(&store).await;
        let (tx, mut rx) = mpsc::channel(DEPTH);
        let task = tokio::spawn(run_fetcher(fetcher(
            Arc::clone(&store),
            slice.clone(),
            2,
            16,
            tx,
        )));
        park().await;
        let keys: Vec<String> = flaky.requests().into_iter().map(|(k, _)| k).collect();
        assert_eq!(keys, ["p/a", "p/c"]);
        let msgs = drain(&mut rx).await;
        task.await.unwrap();
        let unblocked = collect_fetch(store, slice, 0, 2, 16).await;
        assert_eq!(debugged(&msgs), debugged(&unblocked));
        assert_eq!(
            assembled(&msgs),
            vec![
                ("p/a".to_string(), vec![b'a'; 16]),
                ("p/b".to_string(), Vec::new()),
                ("p/c".to_string(), vec![b'c'; 12]),
            ]
        );
    }

    /// Pins that a fetcher paused mid-object finishes the one window it is
    /// reading ahead while it waits, and starts no other.
    #[tokio::test(start_paused = true)]
    async fn a_paused_lane_finishes_its_read_ahead_and_fetches_no_further() {
        let mut flaky = FlakyStore::new(seeded(&[("p/a", &[b'x'; 48][..])]).await);
        flaky.hold_on = 2;
        let flaky = Arc::new(flaky);
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let slice = listed(&store).await;
        let (tx, mut rx) = mpsc::channel(DEPTH);
        let params = fetcher(store, slice, 2, 16, tx);
        let pause = Arc::clone(&params.pause);
        let task = tokio::spawn(run_fetcher(params));
        park().await;
        assert!(matches!(
            rx.recv().await,
            Some(ChunkMsg::ObjectStart { .. })
        ));
        assert!(matches!(rx.recv().await, Some(ChunkMsg::Chunk(_))));
        pause.store(true, Ordering::Relaxed);
        park().await;
        assert_eq!(flaky.bodies_drained(), 1, "the read-ahead is still held");
        flaky.release.add_permits(1);
        park().await;
        assert_eq!(
            flaky.recorded_ranges(),
            vec![RangeKind::Bounded(0, 16), RangeKind::Bounded(16, 32)]
        );
        assert_eq!(
            flaky.bodies_drained(),
            2,
            "the read-ahead finished while the fetcher waited out the pause"
        );
        task.abort();
    }

    /// Pins that a byte-range read plans its next window only after the
    /// trim has seen the window in hand, so a read ahead never runs past the
    /// owned bytes.
    #[tokio::test(start_paused = true)]
    async fn a_blocked_ranged_read_stops_at_the_owned_bytes() {
        let flaky = Arc::new(FlakyStore::new(
            seeded(&[("p/a", b"aa\nbbbb\ncc\r\n \ndddddd\nee\nfff")]).await,
        ));
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let slice = listed(&store).await;
        // ObjectStart fills the one slot, so the fetcher blocks on the first
        // chunk of the first window.
        let (tx, mut rx) = mpsc::channel(1);
        let mut params = fetcher(store, slice, 2, 4, tx);
        params.range = Some(SplitRange::new(8, 21, b'\n'));
        let task = tokio::spawn(run_fetcher(params));
        park().await;
        assert_eq!(
            flaky.recorded_ranges(),
            vec![RangeKind::Bounded(7, 11), RangeKind::Bounded(11, 15)]
        );
        let msgs = drain(&mut rx).await;
        task.await.unwrap();
        assert_eq!(assembled(&msgs)[0].1, b"cc\r\n \ndddddd\n");
        assert_eq!(
            flaky.recorded_ranges(),
            vec![
                RangeKind::Bounded(7, 11),
                RangeKind::Bounded(11, 15),
                RangeKind::Bounded(15, 19),
                RangeKind::Bounded(19, 23),
            ]
        );
    }

    /// Pins the read-ahead's memory bound for windows of at least
    /// `LANE_HANDOFF_CHUNKS` chunks: the window queued chunks retain, the
    /// window being handed on and the window read ahead, and no fourth.
    #[tokio::test(start_paused = true)]
    async fn a_lane_holds_at_most_three_windows() {
        let mut flaky = FlakyStore::new(seeded(&[("p/a", &[b'x'; 80][..])]).await);
        flaky.count_live = true;
        let flaky = Arc::new(flaky);
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let slice = listed(&store).await;
        let (tx, mut rx) = mpsc::channel(DEPTH);
        let task = tokio::spawn(run_fetcher(fetcher(store, slice, 2, 16, tx)));
        loop {
            park().await;
            // Dropped at once, as a lane drops each chunk once it is framed.
            if rx.recv().await.is_none() {
                break;
            }
        }
        task.await.unwrap();
        assert_eq!(flaky.recorded_ranges().len(), 5);
        assert_eq!(flaky.live.load(Ordering::Relaxed), 0);
        assert_eq!(flaky.max_live.load(Ordering::Relaxed), 3);
    }

    /// Pins that a failed read ahead within an object reaches the lane only
    /// after every chunk of the window before it.
    #[tokio::test(start_paused = true)]
    async fn a_failed_read_ahead_reaches_the_lane_after_the_earlier_window() {
        for (origin, overwritten) in [(notified(None), true), (Origin::Lister, false)] {
            let (inner, entry) = thirty_bytes().await;
            let store = Arc::new(
                HeadStore::new(inner, 2).head_answers(vec![Ok(meta(Some("\"moved\""), 30))]),
            );
            let (tx, mut rx) = mpsc::channel(DEPTH);
            let mut params = fetcher(store, vec![entry], 2, 10, tx);
            params.origin = origin;
            let task = tokio::spawn(run_fetcher(params));
            park().await;
            let mut msgs = drain(&mut rx).await;
            task.await.unwrap();
            let failure = match msgs.pop() {
                Some(ChunkMsg::LaneFailed(failure)) => failure,
                other => panic!("the read did not fail: {other:?}"),
            };
            match failure {
                SplitFailure::Overwritten(_) => assert!(overwritten),
                SplitFailure::Poison(PoisonKind::EtagDrift, _) => assert!(!overwritten),
                other => panic!("unexpected failure: {other}"),
            }
            assert!(matches!(msgs[0], ChunkMsg::ObjectStart { .. }));
            assert_eq!(msgs.len(), 6, "ObjectStart and five chunks");
            assert_eq!(assembled(&msgs)[0].1, b"0123456789");
        }
    }

    /// Pins that a next member whose read ahead fails is reported after its
    /// own `ObjectStart`, where a read started at that point would report it.
    #[tokio::test(start_paused = true)]
    async fn a_failed_member_read_ahead_follows_that_members_start() {
        let store: Arc<dyn ObjectStore> = Arc::new(seeded(&[("p/a", &[b'a'; 16][..])]).await);
        let mut slice = listed(&store).await;
        slice.push(entry("p/gone", 12, Some("\"gone\"")));
        let (tx, mut rx) = mpsc::channel(DEPTH);
        let task = tokio::spawn(run_fetcher(fetcher(store, slice, 2, 16, tx)));
        park().await;
        let msgs = drain(&mut rx).await;
        task.await.unwrap();
        let tail: Vec<String> = debugged(&msgs[msgs.len() - 3..])
            .into_iter()
            .map(|m| m.split([' ', '(']).next().unwrap().to_owned())
            .collect();
        assert_eq!(tail, ["ObjectEnd", "ObjectStart", "LaneFailed"]);
        assert!(matches!(
            msgs.last(),
            Some(ChunkMsg::LaneFailed(SplitFailure::Poison(
                PoisonKind::NotFound,
                _
            )))
        ));
    }

    /// Pins that each window of an object retries on a budget of its own:
    /// failures of one window never count against the next.
    #[tokio::test(start_paused = true)]
    async fn each_window_gets_its_own_retry_budget() {
        // Window 2 (8..16) fails one attempt short of the budget, then one
        // attempt past it.
        for (window_two_failures, completes) in [(MAX_ATTEMPTS - 1, true), (MAX_ATTEMPTS, false)] {
            let mut flaky = FlakyStore::new(seeded(&[("p/a", b"0123456789abcdef")]).await);
            flaky.fail_gets = AtomicU32::new(MAX_ATTEMPTS - 1);
            flaky.fail_past = 8;
            flaky.fail_past_gets = AtomicU32::new(window_two_failures);
            let flaky = Arc::new(flaky);
            let store: Arc<dyn ObjectStore> = flaky.clone();
            let slice = listed(&store).await;
            let (tx, mut rx) = mpsc::channel(DEPTH);
            let task = tokio::spawn(run_fetcher(fetcher(store, slice, 2, 8, tx)));
            let msgs = drain(&mut rx).await;
            task.await.unwrap();
            if completes {
                assert_eq!(
                    assembled(&msgs),
                    vec![("p/a".to_string(), b"0123456789abcdef".to_vec())]
                );
                assert_eq!(flaky.gets.load(Ordering::Relaxed), 2 * MAX_ATTEMPTS);
            } else {
                assert!(matches!(
                    msgs.last(),
                    Some(ChunkMsg::LaneFailed(SplitFailure::Poison(
                        PoisonKind::RetriesExhausted,
                        _
                    )))
                ));
                assert_eq!(assembled(&msgs[..msgs.len() - 1])[0].1, b"01234567");
                assert_eq!(flaky.gets.load(Ordering::Relaxed), 2 * MAX_ATTEMPTS);
            }
        }
    }

    /// Pins that a lane dropped while a window is read ahead ends the
    /// fetcher and cancels that read.
    #[tokio::test(start_paused = true)]
    async fn dropping_the_lane_mid_read_ahead_ends_the_fetcher() {
        for paused in [false, true] {
            let mut flaky = FlakyStore::new(seeded(&[("p/a", &[b'x'; 48][..])]).await);
            flaky.hold_on = 2;
            let flaky = Arc::new(flaky);
            let store: Arc<dyn ObjectStore> = flaky.clone();
            let slice = listed(&store).await;
            let (tx, mut rx) = mpsc::channel(DEPTH);
            let params = fetcher(store, slice, 2, 16, tx);
            params.pause.store(paused, Ordering::Relaxed);
            let pause = Arc::clone(&params.pause);
            let task = tokio::spawn(run_fetcher(params));
            park().await;
            if paused {
                assert_eq!(
                    flaky.held.load(Ordering::Relaxed),
                    0,
                    "paused before any GET"
                );
                pause.store(false, Ordering::Relaxed);
                park().await;
                // One slot frees, so the fetcher's next send waits out the
                // pause instead of the full hand-off.
                pause.store(true, Ordering::Relaxed);
                assert!(rx.recv().await.is_some());
                park().await;
            }
            assert_eq!(flaky.held.load(Ordering::Relaxed), 1);
            drop(rx);
            tokio::time::timeout(Duration::from_secs(5), task)
                .await
                .expect("the fetcher ends when its lane is dropped")
                .unwrap();
            assert_eq!(flaky.held_dropped.load(Ordering::Relaxed), 1);
        }
    }

    /// Pins that a bounded read answered with no bytes poisons the unit,
    /// where reading the window again would succeed.
    #[tokio::test(start_paused = true)]
    async fn an_empty_ranged_read_poisons_the_unit() {
        let mut flaky = FlakyStore::new(seeded(&[("p/a", b"0123456789")]).await);
        flaky.empty_on = 1;
        let store: Arc<dyn ObjectStore> = Arc::new(flaky);
        let slice = listed(&store).await;
        let msgs = collect_fetch(store, slice, 0, 4, 8).await;
        assert!(
            matches!(
                msgs.last(),
                Some(ChunkMsg::LaneFailed(SplitFailure::Poison(PoisonKind::Undecodable, reason)))
                    if reason.contains("returned no bytes")
            ),
            "{msgs:?}"
        );
    }

    /// Pins that a pause reached while the fetcher waits on its read ahead,
    /// with nothing left to send, stops it from opening the window after.
    #[tokio::test(start_paused = true)]
    async fn a_pause_during_the_read_ahead_wait_opens_no_further_get() {
        let mut flaky = FlakyStore::new(seeded(&[("p/a", &[b'x'; 48][..])]).await);
        flaky.hold_on = 2;
        let flaky = Arc::new(flaky);
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let slice = listed(&store).await;
        let (tx, _rx) = mpsc::channel(DEPTH);
        // One chunk per window fits the hand-off, so the fetcher waits on the
        // held second GET with nothing left to send.
        let params = fetcher(store, slice, 16, 16, tx);
        let pause = Arc::clone(&params.pause);
        let task = tokio::spawn(run_fetcher(params));
        park().await;
        assert_eq!(flaky.held.load(Ordering::Relaxed), 1);
        pause.store(true, Ordering::Relaxed);
        flaky.release.add_permits(1);
        park().await;
        assert_eq!(
            flaky.recorded_ranges(),
            vec![RangeKind::Bounded(0, 16), RangeKind::Bounded(16, 32)],
            "a paused lane opened a GET"
        );
        task.abort();
    }

    /// Pins that the next member's read ahead keeps draining while the
    /// fetcher waits to hand on the member boundary: the `ObjectEnd` of the
    /// member before it, or its own `ObjectStart`.
    #[tokio::test(start_paused = true)]
    async fn a_read_ahead_drains_while_the_member_boundary_waits() {
        // Member a is an `ObjectStart` and eight chunks, so a hand-off of
        // nine waits on its `ObjectEnd` and one of ten on b's `ObjectStart`.
        for depth in [9, 10] {
            let mut flaky = FlakyStore::new(
                seeded(&[("p/a", &[b'a'; 16][..]), ("p/b", &[b'b'; 12][..])]).await,
            );
            flaky.hold_on = 2;
            let flaky = Arc::new(flaky);
            let store: Arc<dyn ObjectStore> = flaky.clone();
            let slice = listed(&store).await;
            let (tx, mut rx) = mpsc::channel(depth);
            let task = tokio::spawn(run_fetcher(fetcher(store, slice, 2, 16, tx)));
            park().await;
            assert_eq!(flaky.held.load(Ordering::Relaxed), 1, "depth {depth}");
            flaky.release.add_permits(1);
            park().await;
            assert_eq!(flaky.bodies_drained(), 2, "depth {depth}");
            let msgs = drain(&mut rx).await;
            task.await.unwrap();
            assert_eq!(assembled(&msgs)[1].1, vec![b'b'; 12]);
        }
    }

    /// Pins that a member the store reports no ETag for is never read
    /// ahead: its un-ranged body must not open before its turn.
    #[tokio::test(start_paused = true)]
    async fn a_member_without_an_etag_is_not_read_ahead() {
        let flaky = Arc::new(FlakyStore::new(
            seeded(&[("p/a", &[b'a'; 16][..]), ("p/b", &[b'b'; 12][..])]).await,
        ));
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let mut slice = listed(&store).await;
        slice[1].etag = None;
        let (tx, mut rx) = mpsc::channel(DEPTH);
        let task = tokio::spawn(run_fetcher(fetcher(store, slice, 2, 16, tx)));
        park().await;
        assert_eq!(flaky.recorded_ranges(), vec![RangeKind::Bounded(0, 16)]);
        let msgs = drain(&mut rx).await;
        task.await.unwrap();
        assert_eq!(
            flaky.recorded_ranges(),
            vec![RangeKind::Bounded(0, 16), RangeKind::Full]
        );
        assert_eq!(assembled(&msgs)[1].1, vec![b'b'; 12]);
    }

    /// The messages a lane receives for whole members with these bodies,
    /// read in `range_bytes` windows and `chunk_bytes` chunks.
    fn expected_messages(
        slice: &[ObjectEntry],
        bodies: &[Vec<u8>],
        chunk_bytes: usize,
        range_bytes: usize,
    ) -> Vec<String> {
        let mut msgs = Vec::new();
        for (ordinal, (entry, body)) in slice.iter().zip(bodies).enumerate() {
            msgs.push(ChunkMsg::ObjectStart {
                ordinal: ordinal as u32,
                key: entry.key.clone(),
                last_modified_ms: entry.last_modified_ms,
            });
            for window in body.chunks(range_bytes) {
                for chunk in window.chunks(chunk_bytes) {
                    msgs.push(ChunkMsg::Chunk(Bytes::copy_from_slice(chunk)));
                }
            }
            msgs.push(ChunkMsg::ObjectEnd);
        }
        debugged(&msgs)
    }

    proptest::proptest! {
        #![proptest_config(proptest::prelude::ProptestConfig::with_cases(64))]

        /// Pins that the messages a lane receives and the GETs a unit makes
        /// depend on neither the hand-off depth nor how fast the lane reads.
        #[test]
        fn a_unit_streams_its_members_in_order_under_any_windows(
            sizes in proptest::collection::vec(0usize..48, 1..7),
            range_bytes in 1usize..20,
            chunk_bytes in 1usize..8,
            depth in 1usize..6,
            park_every in 1usize..4,
        ) {
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_time()
                .start_paused(true)
                .build()
                .unwrap();
            let bodies: Vec<Vec<u8>> = sizes
                .iter()
                .enumerate()
                .map(|(i, n)| (0..*n).map(|b| b'a' + ((b + i) % 26) as u8).collect())
                .collect();
            let (msgs, slice, ranges) = rt.block_on(async {
                let keys: Vec<String> = (0..bodies.len()).map(|i| format!("p/{i}")).collect();
                let objects: Vec<(&str, &[u8])> = keys
                    .iter()
                    .zip(&bodies)
                    .map(|(k, b)| (k.as_str(), b.as_slice()))
                    .collect();
                let flaky = Arc::new(FlakyStore::new(seeded(&objects).await));
                let store: Arc<dyn ObjectStore> = flaky.clone();
                let slice = listed(&store).await;
                let (tx, mut rx) = mpsc::channel(depth);
                let task = tokio::spawn(run_fetcher(fetcher(
                    store,
                    slice.clone(),
                    chunk_bytes,
                    range_bytes,
                    tx,
                )));
                let mut msgs = Vec::new();
                while let Some(m) = rx.recv().await {
                    msgs.push(m);
                    if msgs.len() % park_every == 0 {
                        park().await;
                    }
                }
                task.await.unwrap();
                (msgs, slice, flaky.recorded_ranges())
            });
            proptest::prop_assert_eq!(
                debugged(&msgs),
                expected_messages(&slice, &bodies, chunk_bytes, range_bytes)
            );
            let want: Vec<RangeKind> = bodies
                .iter()
                .flat_map(|body| {
                    let size = body.len() as u64;
                    (0..size)
                        .step_by(range_bytes)
                        .map(move |w| RangeKind::Bounded(w, (w + range_bytes as u64).min(size)))
                })
                .collect();
            proptest::prop_assert_eq!(ranges, want);
        }
    }

    /// Run a fetcher over one no-ETag object whose stream is cut after
    /// `cut_after` bytes, returning the terminal `LaneFailed` failure.
    async fn no_etag_mid_stream_failure(cut_fatal: bool) -> SplitFailure {
        no_etag_mid_stream_failure_with(cut_fatal, None).await
    }

    /// [`no_etag_mid_stream_failure`] with the cut's error text replaced.
    async fn no_etag_mid_stream_failure_with(
        cut_fatal: bool,
        error_text: Option<String>,
    ) -> SplitFailure {
        let flaky = FlakyStore {
            cut_after: 4,
            cut_fatal,
            error_text,
            ..FlakyStore::new(seeded(&[("p/a", b"0123456789")]).await)
        };
        flaky.cut_streams.store(1, Ordering::Relaxed);
        let store: Arc<dyn ObjectStore> = Arc::new(flaky);
        let mut slice = listed(&store).await;
        slice[0].etag = None; // no pin → streaming fallback, no mid-object resume
        let msgs = collect_fetch(store, slice, 0, 64, 64).await;
        match msgs.into_iter().last() {
            Some(ChunkMsg::LaneFailed(failure)) => failure,
            other => panic!("a mid-stream break without an ETag must fail the lane: {other:?}"),
        }
    }

    #[tokio::test]
    async fn mid_stream_permission_loss_without_etag_is_pipeline_fatal() {
        // Credentials/permission failures hold for every object on every
        // instance: no peer fares better, so poisoning (hand-back + retry
        // elsewhere) would serialize the same failure across the fleet.
        let failure = no_etag_mid_stream_failure(true).await;
        let SplitFailure::Fatal(e) = failure else {
            panic!("PermissionDenied mid-stream must be pipeline-fatal, got: {failure}");
        };
        assert!(e.to_string().contains("p/a"), "{e}");
    }

    #[tokio::test]
    async fn mid_stream_generic_failure_without_etag_poisons_the_split() {
        // A transport break after bytes were delivered cannot be resumed
        // without an ETag pin (a re-read could splice two versions):
        // object-level poison, never pipeline-fatal.
        let failure = no_etag_mid_stream_failure(false).await;
        let SplitFailure::Poison(kind, reason) = failure else {
            panic!("a generic mid-stream break is split poison, got: {failure}");
        };
        assert_eq!(kind, PoisonKind::Undecodable);
        assert!(reason.contains("no ETag"), "{reason}");
    }

    #[tokio::test]
    async fn an_object_without_an_etag_uses_the_streaming_fallback() {
        let flaky = Arc::new(FlakyStore::new(seeded(&[("p/a", b"hello world")]).await));
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let mut slice = listed(&store).await;
        slice[0].etag = None; // store reports no ETag → one continuous stream
        let msgs = collect_fetch(Arc::clone(&store), slice, 0, 4, 4).await;
        assert_eq!(
            assembled(&msgs),
            vec![("p/a".to_string(), b"hello world".to_vec())]
        );
        assert_eq!(
            flaky.recorded_ranges(),
            vec![RangeKind::Full],
            "the fallback issues a single un-ranged GET, never a bounded window"
        );
    }

    /// A fetcher parked in the pause wait gives up when its lane drops the
    /// receiver, without ever being unpaused.
    #[tokio::test(start_paused = true)]
    async fn a_paused_fetcher_exits_when_its_lane_drops_the_receiver() {
        let store: Arc<dyn ObjectStore> = Arc::new(seeded(&[("p/a", b"aaaa")]).await);
        let slice = listed(&store).await;
        let (tx, rx) = mpsc::channel(8);
        let params = FetcherParams {
            split: "s3-test".into(),
            store,
            slice: Arc::new(slice),
            start_ordinal: 0,
            resume_etag: None,
            chunk_bytes: 64,
            range_bytes: 64,
            tx,
            pause: Arc::new(AtomicBool::new(true)),
            retry_base: Duration::from_millis(1),
            retries: None,
            range: None,
            bytes_read: None,
            origin: Origin::Lister,
        };
        let task = tokio::spawn(run_fetcher(params));
        // On paused time the sleep returns only once the fetcher is parked.
        tokio::time::sleep(Duration::from_millis(80)).await;
        drop(rx);
        tokio::time::timeout(Duration::from_secs(5), task)
            .await
            .expect("a paused fetcher exits once the receiver is gone")
            .unwrap();
    }

    /// A read the store refuses with a TLS alert fails the lane as
    /// pipeline-fatal, and the error names the alert.
    #[tokio::test]
    async fn a_tls_rejection_on_a_read_is_fatal_and_named() {
        let addr =
            spate_test::tls_alert_server(b"", u8::from(rustls::AlertDescription::HandshakeFailure));
        let store: Arc<dyn ObjectStore> = Arc::new(crate::test_servers::tls_store_at(
            &format!("https://{addr}"),
            &crate::test_servers::TestCa::new("any"),
        ));
        let slice = vec![ObjectEntry {
            key: "p/a".to_owned(),
            size: 10,
            etag: Some("\"e\"".to_owned()),
            last_modified_ms: 0,
        }];
        let msgs = collect_fetch(store, slice, 0, 64, 64).await;
        let Some(ChunkMsg::LaneFailed(SplitFailure::Fatal(e))) = msgs.into_iter().last() else {
            panic!("expected a pipeline-fatal lane failure");
        };
        assert!(e.to_string().contains("HandshakeFailure"), "{e}");
    }

    /// A store error body of 64 KiB, as an `AccessDenied` or `NoSuchKey` XML
    /// document quotes it.
    fn large_body(code: &str) -> String {
        format!(
            "<Error><Code>{code}</Code><Message>{}</Message></Error>",
            "x".repeat(64 * 1024)
        )
    }

    /// The store's text in `reason` is cut: under 2 KiB, marked `…`.
    fn assert_cut(reason: &str) {
        assert!(reason.len() < 2048, "{} bytes", reason.len());
        assert!(reason.contains('…'), "{reason}");
    }

    /// One entry under `key` with an ETag, read from a local server answering
    /// every request with `status` and `body`; returns the lane's failure.
    async fn read_answered(status: &'static str, body: String, key: &str) -> SplitFailure {
        let url = crate::test_servers::sequence_server(vec![(status, body)]);
        let store: Arc<dyn ObjectStore> = Arc::new(crate::test_servers::store_at(&url));
        let slice = vec![entry(key, 10, Some("\"e\""))];
        match collect_fetch(store, slice, 0, 64, 64)
            .await
            .into_iter()
            .last()
        {
            Some(ChunkMsg::LaneFailed(failure)) => failure,
            other => panic!("a refused read must fail the lane: {other:?}"),
        }
    }

    /// A read refused 403 with a 64 KiB body is pipeline-fatal, and its
    /// reason carries the status and the store's text cut to the bound.
    /// Regression for #1305.
    #[tokio::test]
    async fn a_refused_read_with_a_large_body_is_fatal_and_cut() {
        let failure = read_answered("403 Forbidden", large_body("AccessDenied"), "p/a").await;
        let SplitFailure::Fatal(e) = failure else {
            panic!("a 403 read must be fatal, got: {failure}");
        };
        let reason = e.to_string();
        assert!(reason.contains("p/a"), "{reason}");
        assert!(reason.contains("403 Forbidden"), "{reason}");
        assert!(reason.contains("AccessDenied"), "{reason}");
        assert!(reason.ends_with('…'), "{reason}");
        assert_cut(&reason);
    }

    /// A read answered 404 with a 64 KiB body poisons the unit as
    /// `NotFound`, with the store's text cut to the bound.
    #[tokio::test]
    async fn a_missing_object_with_a_large_body_poisons_and_cut() {
        let failure = read_answered("404 Not Found", large_body("NoSuchKey"), "p/a").await;
        let SplitFailure::Poison(PoisonKind::NotFound, reason) = failure else {
            panic!("a 404 read must poison as not_found, got: {failure}");
        };
        assert!(reason.contains("p/a"), "{reason}");
        assert!(reason.contains("NoSuchKey"), "{reason}");
        assert!(reason.ends_with('…'), "{reason}");
        assert_cut(&reason);
    }

    /// A multibyte error body is cut on a character: 512 characters of the
    /// store's body and the `…`.
    #[tokio::test]
    async fn a_multibyte_error_body_is_cut_on_a_character() {
        let body = format!(
            "<Error><Message>{}</Message></Error>",
            "é".repeat(64 * 1024)
        );
        let failure = read_answered("403 Forbidden", body, "p/a").await;
        let SplitFailure::Fatal(e) = failure else {
            panic!("a 403 read must be fatal, got: {failure}");
        };
        let reason = e.to_string();
        let text = reason
            .split_once("403 Forbidden: ")
            .map(|(_, text)| text)
            .expect("the store's body follows the status");
        assert_eq!(text.chars().count(), 512 + 1, "{text}");
        assert!(text.ends_with("é…"), "{text}");
    }

    /// A refused read of a key longer than the bound names the key, the kind
    /// of failure and the status, with the store's body cut to the bound.
    #[tokio::test]
    async fn a_long_key_refusal_keeps_the_status() {
        let key = format!("p/{}", "k".repeat(1000));
        let failure = read_answered("403 Forbidden", large_body("AccessDenied"), &key).await;
        let SplitFailure::Fatal(e) = failure else {
            panic!("a 403 read must be fatal, got: {failure}");
        };
        let reason = e.to_string();
        assert!(reason.contains(&format!("\"{key}\"")), "{reason}");
        assert!(
            reason.contains("lacked the necessary privileges"),
            "{reason}"
        );
        assert!(reason.contains("403 Forbidden: <Error>"), "{reason}");
        assert!(reason.ends_with('…'), "{reason}");
        assert!(
            reason.len() < 2048 + 2 * key.len(),
            "{} bytes",
            reason.len()
        );
    }

    /// Pins that a TLS rejection on a read of a long key is still named.
    #[tokio::test]
    async fn a_tls_rejection_on_a_long_key_is_still_named() {
        let addr =
            spate_test::tls_alert_server(b"", u8::from(rustls::AlertDescription::HandshakeFailure));
        let store: Arc<dyn ObjectStore> = Arc::new(crate::test_servers::tls_store_at(
            &format!("https://{addr}"),
            &crate::test_servers::TestCa::new("any"),
        ));
        let slice = vec![entry(&format!("p/{}", "k".repeat(1000)), 10, Some("\"e\""))];
        let msgs = collect_fetch(store, slice, 0, 64, 64).await;
        let Some(ChunkMsg::LaneFailed(SplitFailure::Fatal(e))) = msgs.into_iter().last() else {
            panic!("expected a pipeline-fatal lane failure");
        };
        assert!(e.to_string().contains("HandshakeFailure"), "{e}");
    }

    /// A read failing past its attempt budget poisons the unit with the
    /// last error's text cut to the bound.
    #[tokio::test(start_paused = true)]
    async fn a_read_failing_past_the_budget_with_a_large_error_is_cut() {
        let flaky = FlakyStore {
            error_text: Some("x".repeat(64 * 1024)),
            ..FlakyStore::new(seeded(&[("p/a", b"0123456789")]).await)
        };
        flaky.fail_gets.store(MAX_ATTEMPTS, Ordering::Relaxed);
        let store: Arc<dyn ObjectStore> = Arc::new(flaky);
        let slice = listed(&store).await;
        let msgs = collect_fetch(store, slice, 0, 64, 64).await;
        let Some(ChunkMsg::LaneFailed(SplitFailure::Poison(PoisonKind::RetriesExhausted, reason))) =
            msgs.into_iter().last()
        else {
            panic!("a read failing past its budget must poison as retries_exhausted");
        };
        assert!(reason.contains("attempts"), "{reason}");
        assert!(reason.contains("xxxx"), "{reason}");
        assert!(reason.ends_with('…'), "{reason}");
        assert_cut(&reason);
    }

    /// A read of a key that encodes past the bound, failing 503 past its
    /// attempt budget, names the status and the store's code.
    #[tokio::test(start_paused = true)]
    async fn a_read_of_a_long_key_past_the_budget_keeps_the_status() {
        let text = format!(
            "Error performing GET http://127.0.0.1/b/p/{} in 1ms - Server returned non-2xx \
             status code: 503 Service Unavailable: <Error><Code>SlowDown</Code><Message>{}",
            "%E6%95%B0".repeat(60),
            "x".repeat(64 * 1024)
        );
        let flaky = FlakyStore {
            error_text: Some(text),
            ..FlakyStore::new(seeded(&[("p/a", b"0123456789")]).await)
        };
        flaky.fail_gets.store(MAX_ATTEMPTS, Ordering::Relaxed);
        let store: Arc<dyn ObjectStore> = Arc::new(flaky);
        let slice = listed(&store).await;
        let msgs = collect_fetch(store, slice, 0, 64, 64).await;
        let Some(ChunkMsg::LaneFailed(SplitFailure::Poison(PoisonKind::RetriesExhausted, reason))) =
            msgs.into_iter().last()
        else {
            panic!("a read failing past its budget must poison as retries_exhausted");
        };
        assert!(reason.contains("503 Service Unavailable"), "{reason}");
        assert!(reason.contains("SlowDown"), "{reason}");
        assert!(reason.ends_with('…'), "{reason}");
        assert_cut(&reason);
    }

    /// A mid-object failure without an ETag, fatal or poison, carries the
    /// store's text cut to the bound.
    #[tokio::test]
    async fn a_mid_object_failure_with_a_large_error_is_cut() {
        let text = Some("x".repeat(64 * 1024));
        let SplitFailure::Fatal(e) = no_etag_mid_stream_failure_with(true, text.clone()).await
        else {
            panic!("PermissionDenied mid-stream must be pipeline-fatal");
        };
        let reason = e.to_string();
        assert!(
            reason.contains("p/a") && reason.contains("mid-object"),
            "{reason}"
        );
        assert!(reason.ends_with('…'), "{reason}");
        assert_cut(&reason);
        let SplitFailure::Poison(PoisonKind::Undecodable, reason) =
            no_etag_mid_stream_failure_with(false, text).await
        else {
            panic!("a generic mid-stream break is split poison");
        };
        assert!(
            reason.contains("p/a") && reason.contains("mid-object"),
            "{reason}"
        );
        assert!(reason.ends_with('…'), "{reason}");
        assert_cut(&reason);
    }

    /// Each transient read failure's log line carries the store's text cut
    /// to the bound.
    #[test]
    fn a_retried_read_logs_its_store_error_cut() {
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .start_paused(true)
            .build()
            .unwrap();
        let lines = spate_test::capture_logs(tracing::Level::WARN, || {
            rt.block_on(async {
                let flaky = FlakyStore {
                    error_text: Some("x".repeat(64 * 1024)),
                    ..FlakyStore::new(seeded(&[("p/a", b"0123456789")]).await)
                };
                flaky.fail_gets.store(2, Ordering::Relaxed);
                let store: Arc<dyn ObjectStore> = Arc::new(flaky);
                let slice = listed(&store).await;
                let msgs = collect_fetch(store, slice, 0, 64, 64).await;
                assert_eq!(
                    assembled(&msgs),
                    vec![("p/a".to_string(), b"0123456789".to_vec())]
                );
            });
        });
        let retried: Vec<_> = lines
            .iter()
            .filter(|l| l.contains("transient object read failure"))
            .collect();
        assert_eq!(retried.len(), 2, "{lines:?}");
        for line in retried {
            assert!(line.contains("xxxx…"), "{line}");
            assert!(line.len() < 2048, "{} bytes", line.len());
        }
    }

    // ---------------------------------------------- ranged reads, end to end --

    /// Window sizes and injected failures for one [`read_through_lane`] run.
    #[derive(Clone, Copy, Debug)]
    struct ReadPlan {
        range_bytes: usize,
        chunk_bytes: usize,
        /// Retryable failures of the first GETs, which read the range's head.
        head_failures: u32,
        /// Retryable failures of the first GETs reaching past the range's end.
        tail_failures: u32,
        /// Cut the first GET's body after this many bytes.
        cut_after: Option<usize>,
    }

    impl ReadPlan {
        fn windows(range_bytes: usize, chunk_bytes: usize) -> ReadPlan {
            ReadPlan {
                range_bytes,
                chunk_bytes,
                head_failures: 0,
                tail_failures: 0,
                cut_after: None,
            }
        }
    }

    fn runtime() -> tokio::runtime::Runtime {
        tokio::runtime::Builder::new_multi_thread()
            .worker_threads(1)
            .enable_all()
            .build()
            .unwrap()
    }

    /// Read `range` of `object` through a fetcher over a [`FlakyStore`] and a
    /// lane resuming `resume` records in, returning each emitted record's
    /// offset and bytes. Panics if the split poisons.
    fn read_through_lane(
        rt: &tokio::runtime::Runtime,
        object: &[u8],
        range: Option<SplitRange>,
        resume: u64,
        plan: ReadPlan,
    ) -> Vec<(i64, Vec<u8>)> {
        use crate::lane::S3Lane;
        use crate::lane_ctx::SplitTracker;
        use crate::offset::Position;
        use spate_core::clock::tokio::SystemClock;
        use spate_core::source::{PayloadBatch as _, SourceLane as _};

        let flaky = FlakyStore {
            cut_after: plan.cut_after.unwrap_or(0),
            fail_past: range.map_or(u64::MAX, |r| r.end),
            ..FlakyStore::new(rt.block_on(seeded(&[("p/big.ndjson", object)])))
        };
        flaky.fail_gets.store(plan.head_failures, Ordering::Relaxed);
        flaky
            .fail_past_gets
            .store(plan.tail_failures, Ordering::Relaxed);
        flaky
            .cut_streams
            .store(u32::from(plan.cut_after.is_some()), Ordering::Relaxed);
        let store: Arc<dyn ObjectStore> = Arc::new(flaky);
        let slice = rt.block_on(listed(&store));
        let (tx, rx) = mpsc::channel(4);
        let fetcher = spate_core::task::spawn_on(
            "s3-fetcher",
            run_fetcher(FetcherParams {
                split: "s3-test".into(),
                store,
                slice: Arc::new(slice),
                start_ordinal: 0,
                resume_etag: None,
                chunk_bytes: plan.chunk_bytes,
                range_bytes: plan.range_bytes,
                tx,
                pause: Arc::new(AtomicBool::new(false)),
                retry_base: Duration::from_millis(1),
                retries: None,
                range,
                bytes_read: None,
                origin: Origin::Lister,
            }),
            rt.handle(),
        );
        let tracker = Arc::new(SplitTracker::new());
        let (poison_tx, poison_rx) = std_mpsc::channel();
        let mut lane = S3Lane::new(
            spate_core::source::LaneId(0),
            spate_core::record::PartitionId(0),
            rx,
            fetcher,
            rt.handle().clone(),
            Arc::new(SystemClock),
            spate_core::checkpoint::Checkpointer::new().handle(),
            crate::config::Compression::None,
            Arc::new(|| Box::new(crate::testutil::TestLineFramer::new(1 << 20))),
            Some(Position {
                ordinal: 0,
                record: resume,
            }),
            Arc::clone(&tracker),
            poison_tx,
            spate_core::feed::wake_channel().0,
            None,
        );
        let deadline = std::time::Instant::now() + Duration::from_secs(30);
        let mut out = Vec::new();
        loop {
            match lane
                .poll(1024, Duration::from_millis(20))
                .expect("the lane polls")
            {
                Some(mut batch) => {
                    while let Some(p) = batch.next_payload() {
                        out.push((p.offset, p.bytes.to_vec()));
                    }
                }
                None if tracker.terminal().is_some() => return out,
                None => {
                    if let Ok(report) = poison_rx.try_recv() {
                        panic!("the split poisoned: {}", report.reason);
                    }
                    assert!(
                        std::time::Instant::now() < deadline,
                        "the lane never reached end of input"
                    );
                }
            }
        }
    }

    fn bytes_of(records: Vec<(i64, Vec<u8>)>) -> Vec<Vec<u8>> {
        records.into_iter().map(|(_, bytes)| bytes).collect()
    }

    /// The records of `object` framed whole, in one chunk.
    fn framed_whole(object: &[u8]) -> Vec<Vec<u8>> {
        let mut framer = crate::framer::ObjectFramer::new(Arc::new(|| {
            Box::new(crate::testutil::TestLineFramer::new(1 << 20))
        }));
        framer.begin_object(crate::framer::Codec::Plain).unwrap();
        framer.push_chunk(Bytes::copy_from_slice(object)).unwrap();
        framer.finish_object().unwrap();
        std::iter::from_fn(|| framer.pop_record()).collect()
    }

    /// Object bytes of short records, `\r\n` endings, whitespace-only lines,
    /// lines longer than a whole range, and an optional unterminated final
    /// line.
    fn arb_lines_object() -> impl proptest::strategy::Strategy<Value = Vec<u8>> {
        use proptest::prelude::*;
        (
            proptest::collection::vec(
                prop_oneof![
                    4 => "[a-z]{1,6}".prop_map(|s| format!("{s}\n")),
                    1 => "[a-z]{1,6}".prop_map(|s| format!("{s}\r\n")),
                    1 => "[ \t]{0,3}".prop_map(|s| format!("{s}\n")),
                    1 => "[a-z]{30,60}".prop_map(|s| format!("{s}\n")),
                ],
                1..30,
            ),
            proptest::option::of("[a-z]{1,6}"),
        )
            .prop_map(|(lines, last)| {
                let mut object = lines.concat();
                object.push_str(last.as_deref().unwrap_or_default());
                object.into_bytes()
            })
    }

    proptest::proptest! {
        #![proptest_config(proptest::prelude::ProptestConfig::with_cases(48))]

        /// An object tiled into ranges, each read through the fetcher and a
        /// lane with failures injected in the head and tail windows, yields
        /// the whole object's records exactly once and in order.
        #[test]
        fn ranges_tiling_an_object_emit_each_record_once(
            object in arb_lines_object(),
            pieces in 1u64..10,
            range_bytes in 1usize..32,
            chunk_bytes in 1usize..16,
            head_failures in 0u32..2,
            tail_failures in 0u32..3,
            cut_after in proptest::option::of(0usize..24),
        ) {
            let rt = runtime();
            let size = object.len() as u64;
            let n = pieces.min(size);
            let bound = |i: u64| (u128::from(i) * u128::from(size) / u128::from(n)) as u64;
            let plan = ReadPlan { range_bytes, chunk_bytes, head_failures, tail_failures, cut_after };
            let mut joined = Vec::new();
            for i in 0..n {
                let range = SplitRange::new(bound(i), bound(i + 1), b'\n');
                joined.extend(bytes_of(read_through_lane(&rt, &object, Some(range), 0, plan)));
            }
            proptest::prop_assert_eq!(joined, framed_whole(&object));
        }
    }

    /// A range read under one window layout, then resumed at every record
    /// count under others, emits exactly the records past the resume point at
    /// the same offsets. The ranges put a delimiter at `start - 1`, `end - 1`
    /// and `end`, include an empty range, one ending at the object's end and
    /// one whose head spans several windows, and the layouts put a window edge
    /// at `end - 1` and at the object's end.
    #[test]
    fn a_resumed_range_emits_the_same_tail_under_any_windows() {
        let rt = runtime();
        // Delimiters at 2, 7, 11, 13, 20 and 23; 27 bytes.
        let object = b"aa\nbbbb\ncc\r\n \ndddddd\nee\nfff";
        let ranges = [
            (0, 3),
            (3, 8),
            (3, 7),
            (5, 14),
            (8, 21),
            (15, 20),
            (14, 27),
            (16, 27),
        ];
        let reference = ReadPlan::windows(4, 2);
        let others = [
            ReadPlan {
                head_failures: 1,
                tail_failures: 1,
                ..ReadPlan::windows(5, 3)
            },
            // From byte 7, windows end at 20, `end - 1` of (8, 21).
            ReadPlan::windows(13, 1),
            ReadPlan::windows(64, 64),
        ];
        for (start, end) in ranges {
            let range = Some(SplitRange::new(start, end, b'\n'));
            let full = read_through_lane(&rt, object, range, 0, reference);
            for plan in others {
                for k in 0..=full.len() {
                    let resumed = read_through_lane(&rt, object, range, k as u64, plan);
                    assert_eq!(
                        resumed,
                        full[k..],
                        "range [{start}, {end}) resumed at {k} under {plan:?}"
                    );
                }
            }
        }
        // Spot-check the reference reads against the ownership rule.
        let read = |start, end| {
            bytes_of(read_through_lane(
                &rt,
                object,
                Some(SplitRange::new(start, end, b'\n')),
                0,
                reference,
            ))
        };
        assert_eq!(read(3, 7), vec![b"bbbb".to_vec()]);
        assert_eq!(read(5, 14), vec![b"cc".to_vec()]);
        assert_eq!(read(8, 21), vec![b"cc".to_vec(), b"dddddd".to_vec()]);
        assert!(read(15, 20).is_empty());
        assert_eq!(
            read(14, 27),
            vec![b"dddddd".to_vec(), b"ee".to_vec(), b"fff".to_vec()]
        );
        assert_eq!(read(16, 27), vec![b"ee".to_vec(), b"fff".to_vec()]);
    }

    /// A ranged read counts the bytes it fetches and trims into
    /// `bytes_read`, so with the forwarded bytes the lane counts, the total is
    /// every byte read from the store.
    #[tokio::test]
    async fn a_ranged_read_counts_the_bytes_it_trims() {
        let flaky = Arc::new(FlakyStore::new(
            seeded(&[("p/a", b"aa\nbbbb\ncc\r\n \ndddddd\nee\nfff")]).await,
        ));
        let store: Arc<dyn ObjectStore> = flaky.clone();
        let slice = listed(&store).await;
        let trimmed = Arc::new(AtomicU64::new(0));
        let (tx, mut rx) = mpsc::channel(64);
        let task = tokio::spawn(run_fetcher(FetcherParams {
            split: "s3-test".into(),
            store,
            slice: Arc::new(slice),
            start_ordinal: 0,
            resume_etag: None,
            chunk_bytes: 2,
            range_bytes: 4,
            tx,
            pause: Arc::new(AtomicBool::new(false)),
            retry_base: Duration::from_millis(1),
            retries: None,
            range: Some(SplitRange::new(8, 21, b'\n')),
            bytes_read: Some(spate_core::metrics::Counter::from_arc(Arc::clone(&trimmed))),
            origin: Origin::Lister,
        }));
        let mut msgs = Vec::new();
        while let Some(m) = rx.recv().await {
            msgs.push(m);
        }
        task.await.unwrap();
        let forwarded = assembled(&msgs);
        assert_eq!(forwarded[0].1, b"cc\r\n \ndddddd\n");
        let fetched: u64 = flaky
            .recorded_ranges()
            .iter()
            .map(|r| match r {
                RangeKind::Bounded(start, end) => end - start,
                other => panic!("a ranged read issues bounded windows, got {other:?}"),
            })
            .sum();
        assert_eq!(fetched, 16, "windows 7..11, 11..15, 15..19 and 19..23");
        assert_eq!(
            trimmed.load(Ordering::Relaxed) + forwarded[0].1.len() as u64,
            fetched
        );
    }

    // ------------------------------------------- notification reads --

    /// One request a [`HeadStore`] saw: `(if_match, version, head)`.
    type Seen = (Option<String>, Option<String>, bool);

    /// An in-memory store that records each request's `If-Match`, version
    /// and HEAD flag, fails one GET with a failed precondition, and answers
    /// HEADs from a script.
    #[derive(Debug)]
    struct HeadStore {
        inner: InMemory,
        /// The GET, counted from 1 and HEADs not counted, that answers 412,
        /// or 416 when `range_refused` is set.
        precondition_on: u32,
        range_refused: bool,
        gets: AtomicU32,
        /// Answers for the HEADs, in order; the inner store answers once
        /// the script is spent.
        heads: std::sync::Mutex<std::collections::VecDeque<object_store::Result<ObjectMeta>>>,
        seen: std::sync::Mutex<Vec<Seen>>,
    }

    use object_store::ObjectMeta;

    impl HeadStore {
        fn new(inner: InMemory, precondition_on: u32) -> HeadStore {
            HeadStore {
                inner,
                precondition_on,
                range_refused: false,
                gets: AtomicU32::new(0),
                heads: std::sync::Mutex::new(std::collections::VecDeque::new()),
                seen: std::sync::Mutex::new(Vec::new()),
            }
        }

        fn refusing_the_range(mut self) -> HeadStore {
            self.range_refused = true;
            self
        }

        fn head_answers(self, answers: Vec<object_store::Result<ObjectMeta>>) -> HeadStore {
            *self.heads.lock().unwrap() = answers.into();
            self
        }

        fn seen(&self) -> Vec<Seen> {
            self.seen.lock().unwrap().clone()
        }

        fn heads(&self) -> usize {
            self.seen().iter().filter(|(_, _, head)| *head).count()
        }
    }

    impl fmt::Display for HeadStore {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "HeadStore")
        }
    }

    #[async_trait::async_trait]
    impl ObjectStore for HeadStore {
        async fn put_opts(
            &self,
            location: &Path,
            payload: PutPayload,
            opts: object_store::PutOptions,
        ) -> object_store::Result<object_store::PutResult> {
            self.inner.put_opts(location, payload, opts).await
        }

        async fn put_multipart_opts(
            &self,
            location: &Path,
            opts: object_store::PutMultipartOptions,
        ) -> object_store::Result<Box<dyn object_store::MultipartUpload>> {
            self.inner.put_multipart_opts(location, opts).await
        }

        async fn get_opts(
            &self,
            location: &Path,
            options: GetOptions,
        ) -> object_store::Result<GetResult> {
            self.seen.lock().unwrap().push((
                options.if_match.clone(),
                options.version.clone(),
                options.head,
            ));
            if options.head {
                let scripted = self.heads.lock().unwrap().pop_front();
                if let Some(answer) = scripted {
                    let meta = answer?;
                    return Ok(GetResult {
                        payload: GetResultPayload::Stream(Box::pin(futures_util::stream::empty())),
                        range: 0..meta.size,
                        meta,
                        attributes: Default::default(),
                        extensions: Default::default(),
                    });
                }
                return self.inner.get_opts(location, options).await;
            }
            if self.gets.fetch_add(1, Ordering::Relaxed) + 1 == self.precondition_on {
                if self.range_refused {
                    return Err(object_store::Error::Generic {
                        store: "head",
                        source: "Server returned non-2xx status code: 416 Range Not Satisfiable: "
                            .into(),
                    });
                }
                return Err(object_store::Error::Precondition {
                    path: location.to_string(),
                    source: "overwritten".into(),
                });
            }
            self.inner.get_opts(location, options).await
        }

        fn delete_stream(
            &self,
            locations: futures_util::stream::BoxStream<'static, object_store::Result<Path>>,
        ) -> futures_util::stream::BoxStream<'static, object_store::Result<Path>> {
            self.inner.delete_stream(locations)
        }

        fn list(
            &self,
            prefix: Option<&Path>,
        ) -> futures_util::stream::BoxStream<'static, object_store::Result<ObjectMeta>> {
            self.inner.list(prefix)
        }

        async fn list_with_delimiter(
            &self,
            prefix: Option<&Path>,
        ) -> object_store::Result<object_store::ListResult> {
            self.inner.list_with_delimiter(prefix).await
        }

        async fn copy_opts(
            &self,
            from: &Path,
            to: &Path,
            options: object_store::CopyOptions,
        ) -> object_store::Result<()> {
            self.inner.copy_opts(from, to, options).await
        }
    }

    fn notified(version: Option<&str>) -> Origin {
        Origin::Notification {
            version: version.map(str::to_owned),
            re_enqueues: 0,
        }
    }

    fn meta(etag: Option<&str>, size: u64) -> ObjectMeta {
        ObjectMeta {
            location: Path::from("k"),
            last_modified: chrono::DateTime::from_timestamp_millis(1_760_000_000_000).unwrap(),
            size,
            e_tag: etag.map(str::to_owned),
            version: None,
        }
    }

    /// A 30-byte object `k` and its listed entry, the ETag the store reports.
    async fn thirty_bytes() -> (InMemory, ObjectEntry) {
        let store = seeded(&[("k", b"0123456789abcdefghijABCDEFGHIJ")]).await;
        let listed = list_all(&(Arc::new(store.clone()) as Arc<dyn ObjectStore>), None)
            .await
            .unwrap();
        (store, listed[0].clone())
    }

    /// Drive a fetcher over the one `entry` of a unit from `origin`, in
    /// 10-byte windows, and collect its message stream.
    async fn fetch_from(
        store: Arc<HeadStore>,
        entry: ObjectEntry,
        origin: Origin,
        retries: Option<spate_core::metrics::Counter>,
    ) -> Vec<ChunkMsg> {
        let (tx, mut rx) = mpsc::channel(64);
        let task = tokio::spawn(run_fetcher(FetcherParams {
            split: "s3-test".into(),
            store,
            slice: Arc::new(vec![entry]),
            start_ordinal: 0,
            resume_etag: None,
            chunk_bytes: 64,
            range_bytes: 10,
            tx,
            pause: Arc::new(AtomicBool::new(false)),
            retry_base: Duration::from_millis(1),
            retries,
            range: None,
            bytes_read: None,
            origin,
        }));
        let mut msgs = Vec::new();
        while let Some(m) = rx.recv().await {
            msgs.push(m);
        }
        task.await.unwrap();
        msgs
    }

    fn last_failure(msgs: Vec<ChunkMsg>) -> SplitFailure {
        match msgs.into_iter().last() {
            Some(ChunkMsg::LaneFailed(failure)) => failure,
            other => panic!("the read did not fail: {other:?}"),
        }
    }

    /// Pins that a notification unit naming a version sends it on every
    /// window of a pinned read and on an unpinned read.
    #[tokio::test]
    async fn a_notification_unit_with_a_version_id_reads_that_version() {
        let (inner, entry) = thirty_bytes().await;
        let store = Arc::new(HeadStore::new(inner.clone(), 0));
        let msgs = fetch_from(
            Arc::clone(&store),
            entry.clone(),
            notified(Some("v1")),
            None,
        )
        .await;
        assert_eq!(assembled(&msgs)[0].1.len(), 30);
        let seen = store.seen();
        assert_eq!(seen.len(), 3, "{seen:?}");
        for (if_match, version, head) in seen {
            assert_eq!(if_match, entry.etag);
            assert_eq!(version.as_deref(), Some("v1"));
            assert!(!head);
        }

        let store = Arc::new(HeadStore::new(inner, 0));
        let unpinned = ObjectEntry {
            etag: None,
            ..entry
        };
        fetch_from(Arc::clone(&store), unpinned, notified(Some("v1")), None).await;
        assert_eq!(store.seen(), [(None, Some("v1".to_owned()), false)]);
    }

    /// Pins that a notification whose `versionId` is absent, null, `""` or
    /// `"null"` reads the key, as a lister's unit does.
    #[tokio::test]
    async fn a_null_or_absent_version_id_reads_by_key() {
        let (inner, entry) = thirty_bytes().await;
        let scope = crate::unit::Scope {
            bucket: Some("bucket".to_owned()),
            prefix: Path::from(""),
        };
        let packing = crate::split::Packing {
            target_bytes: 1 << 20,
            compression: crate::config::Compression::None,
            delimiter: Some(b'\n'),
        };
        let mut origins = vec![Origin::Lister];
        for version in [
            None,
            Some(serde_json::Value::Null),
            Some("".into()),
            Some("null".into()),
        ] {
            let mut object = serde_json::json!({ "key": "k", "size": 30 });
            if let Some(version) = version {
                object["versionId"] = version;
            }
            let body = serde_json::json!({ "Records": [{
                "eventName": "ObjectCreated:Put",
                "s3": { "bucket": { "name": "bucket" }, "object": object },
            }]});
            let crate::unit::Decoded::Read(cursors) =
                crate::unit::decode(&serde_json::to_vec(&body).unwrap(), &scope, packing)
            else {
                panic!("the notification is not read");
            };
            origins.extend(cursors.into_iter().flatten().map(|part| part.origin));
        }
        assert_eq!(origins.len(), 5);
        for origin in origins {
            let store = Arc::new(HeadStore::new(inner.clone(), 0));
            fetch_from(Arc::clone(&store), entry.clone(), origin.clone(), None).await;
            let seen = store.seen();
            assert!(!seen.is_empty());
            assert!(
                seen.iter().all(|(_, version, _)| version.is_none()),
                "{origin:?}: {seen:?}"
            );
        }
    }

    /// Pins that a failed precondition mid-read of a notification unit with
    /// no version HEADs the key once and ends the lane's stream with the
    /// overwrite at the content the HEAD reports, after the windows already
    /// read.
    #[tokio::test]
    async fn a_412_on_a_notification_read_heads_the_key_and_reports_the_overwrite() {
        let (inner, entry) = thirty_bytes().await;
        let store =
            Arc::new(HeadStore::new(inner, 2).head_answers(vec![Ok(meta(Some("new"), 99))]));
        let msgs = fetch_from(Arc::clone(&store), entry, notified(None), None).await;
        let chunks: Vec<&[u8]> = msgs
            .iter()
            .filter_map(|m| match m {
                ChunkMsg::Chunk(b) => Some(b.as_ref()),
                _ => None,
            })
            .collect();
        assert_eq!(chunks, [b"0123456789".as_slice()]);
        assert_eq!(store.heads(), 1);
        let SplitFailure::Overwritten(overwrite) = last_failure(msgs) else {
            panic!("not an overwrite");
        };
        assert_eq!(
            *overwrite,
            Overwrite {
                key: "k".to_owned(),
                etag: Some("\"new\"".to_owned()),
                size: 99,
                last_modified_ms: 1_760_000_000_000,
            }
        );
    }

    /// Pins that a failed precondition on a lister's unit or on a read that
    /// names a version poisons the unit as `EtagDrift` without a HEAD.
    #[tokio::test]
    async fn a_412_on_a_lister_or_versioned_read_poisons_without_a_head() {
        for origin in [Origin::Lister, notified(Some("v1"))] {
            let (inner, entry) = thirty_bytes().await;
            let store = Arc::new(HeadStore::new(inner, 1));
            let msgs = fetch_from(Arc::clone(&store), entry, origin.clone(), None).await;
            assert_eq!(store.heads(), 0, "{origin:?}");
            assert!(
                matches!(
                    last_failure(msgs),
                    SplitFailure::Poison(PoisonKind::EtagDrift, _)
                ),
                "{origin:?}"
            );
        }
    }

    /// Pins that a HEAD reporting the ETag the read was pinned to poisons
    /// the split as `EtagDrift`, since a unit at that ETag fails the same
    /// way.
    #[tokio::test]
    async fn a_head_reporting_the_pinned_etag_poisons_the_read() {
        let (inner, entry) = thirty_bytes().await;
        let pinned = entry.etag.clone().unwrap();
        let store = Arc::new(
            HeadStore::new(inner, 1)
                .head_answers(vec![Ok(meta(Some(pinned.trim_matches('"')), 30))]),
        );
        let entry = ObjectEntry {
            etag: Some(crate::unit::quoted(pinned.clone())),
            ..entry
        };
        let msgs = fetch_from(Arc::clone(&store), entry, notified(None), None).await;
        assert_eq!(store.heads(), 1);
        assert!(matches!(
            last_failure(msgs),
            SplitFailure::Poison(PoisonKind::EtagDrift, _)
        ));
    }

    /// Pins the text each split failure displays: a poison and an overwrite
    /// name the object, and a fatal failure shows its error.
    #[test]
    fn a_split_failure_displays_its_cause() {
        let poison = SplitFailure::Poison(PoisonKind::NotFound, "\"k\" is gone".into());
        assert_eq!(poison.to_string(), "split poisoned: \"k\" is gone");
        let overwrite = SplitFailure::Overwritten(Box::new(Overwrite {
            key: "data/k".into(),
            etag: None,
            size: 1,
            last_modified_ms: 0,
        }));
        assert_eq!(overwrite.to_string(), "\"data/k\" was overwritten");
        let fatal = SplitFailure::Fatal(SourceError::Client {
            class: ErrorClass::Fatal,
            reason: "denied".into(),
        });
        assert!(fatal.to_string().contains("denied"), "{fatal}");
    }

    /// Pins that a HEAD answering no ETag reports an overwrite that reads
    /// unpinned.
    #[tokio::test]
    async fn a_head_reporting_no_etag_reports_an_unpinned_overwrite() {
        let (inner, entry) = thirty_bytes().await;
        let store = Arc::new(HeadStore::new(inner, 1).head_answers(vec![Ok(meta(None, 7))]));
        let msgs = fetch_from(store, entry, notified(None), None).await;
        let SplitFailure::Overwritten(overwrite) = last_failure(msgs) else {
            panic!("not an overwrite");
        };
        assert_eq!((overwrite.etag, overwrite.size), (None, 7));
    }

    /// Pins that a HEAD after a 412 is classified as a read is: 404 poisons
    /// the split as `NotFound`, and 403 fails the pipeline, each reason
    /// naming the HEAD and the byte the read stopped at.
    #[tokio::test]
    async fn a_head_after_a_412_classifies_404_and_403_as_a_read_does() {
        let (inner, entry) = thirty_bytes().await;
        let store = Arc::new(HeadStore::new(inner.clone(), 2).head_answers(vec![Err(
            object_store::Error::NotFound {
                path: "k".into(),
                source: "gone".into(),
            },
        )]));
        let msgs = fetch_from(store, entry.clone(), notified(None), None).await;
        let SplitFailure::Poison(PoisonKind::NotFound, reason) = last_failure(msgs) else {
            panic!("a 404 on the HEAD must poison as not_found");
        };
        assert!(reason.contains("at byte 10"), "{reason}");
        assert!(reason.contains("the HEAD after"), "{reason}");
        let store = Arc::new(HeadStore::new(inner, 2).head_answers(vec![Err(
            object_store::Error::PermissionDenied {
                path: "k".into(),
                source: "denied".into(),
            },
        )]));
        let msgs = fetch_from(store, entry, notified(None), None).await;
        let SplitFailure::Fatal(e) = last_failure(msgs) else {
            panic!("a 403 on the HEAD must be fatal");
        };
        assert!(e.to_string().contains("the HEAD after"), "{e}");
    }

    /// A HEAD after a 412 that fails with a 64 KiB error, 404 or 403, keeps
    /// the HEAD's suffix behind the store's text cut to the bound.
    #[tokio::test]
    async fn a_failed_head_with_a_large_error_keeps_its_suffix() {
        let big = || "x".repeat(64 * 1024);
        let suffix = "(the HEAD after the store refused the pinned read)";
        let (inner, entry) = thirty_bytes().await;
        let store = Arc::new(HeadStore::new(inner.clone(), 2).head_answers(vec![Err(
            object_store::Error::NotFound {
                path: "k".into(),
                source: big().into(),
            },
        )]));
        let msgs = fetch_from(store, entry.clone(), notified(None), None).await;
        let SplitFailure::Poison(PoisonKind::NotFound, reason) = last_failure(msgs) else {
            panic!("a 404 on the HEAD must poison as not_found");
        };
        assert!(reason.ends_with(suffix), "{reason}");
        assert_cut(&reason);
        let store = Arc::new(HeadStore::new(inner, 2).head_answers(vec![Err(
            object_store::Error::PermissionDenied {
                path: "k".into(),
                source: big().into(),
            },
        )]));
        let msgs = fetch_from(store, entry, notified(None), None).await;
        let SplitFailure::Fatal(e) = last_failure(msgs) else {
            panic!("a 403 on the HEAD must be fatal");
        };
        let reason = e.to_string();
        assert!(reason.ends_with(suffix), "{reason}");
        assert_cut(&reason);
    }

    /// Pins that a ranged GET a notification unit's read is refused with 416,
    /// as RustFS answers past the end of a smaller new object, HEADs the key
    /// and reports the overwrite, without retrying the GET.
    #[tokio::test]
    async fn a_416_on_a_notification_read_heads_the_key_and_reports_the_overwrite() {
        let (inner, entry) = thirty_bytes().await;
        let store = Arc::new(
            HeadStore::new(inner, 2)
                .refusing_the_range()
                .head_answers(vec![Ok(meta(Some("new"), 5))]),
        );
        let msgs = fetch_from(Arc::clone(&store), entry, notified(None), None).await;
        let SplitFailure::Overwritten(overwrite) = last_failure(msgs) else {
            panic!("a 416 on a notification read must report an overwrite");
        };
        assert_eq!(overwrite.size, 5);
        assert_eq!(store.heads(), 1);
        assert_eq!(store.seen().len(), 3, "two GETs and the HEAD, no retry");
    }

    /// Pins that a transient HEAD failure after a 412 is retried and counted
    /// in `get_retries`, and that a HEAD failing past the attempt budget
    /// poisons the unit as `RetriesExhausted`.
    #[tokio::test(start_paused = true)]
    async fn a_retryable_head_after_a_412_is_retried() {
        let transient = || object_store::Error::Generic {
            store: "head",
            source: "503".into(),
        };
        let (inner, entry) = thirty_bytes().await;
        let retried = Arc::new(AtomicU64::new(0));
        let store = Arc::new(
            HeadStore::new(inner.clone(), 1)
                .head_answers(vec![Err(transient()), Ok(meta(Some("new"), 30))]),
        );
        let msgs = fetch_from(
            Arc::clone(&store),
            entry.clone(),
            notified(None),
            Some(spate_core::metrics::Counter::from_arc(Arc::clone(&retried))),
        )
        .await;
        assert!(matches!(last_failure(msgs), SplitFailure::Overwritten(_)));
        assert_eq!(store.heads(), 2);
        assert_eq!(retried.load(Ordering::Relaxed), 1);

        let failing = (0..MAX_ATTEMPTS).map(|_| Err(transient())).collect();
        let store = Arc::new(HeadStore::new(inner, 1).head_answers(failing));
        let msgs = fetch_from(Arc::clone(&store), entry, notified(None), None).await;
        assert!(matches!(
            last_failure(msgs),
            SplitFailure::Poison(PoisonKind::RetriesExhausted, _)
        ));
        assert_eq!(store.heads(), MAX_ATTEMPTS as usize);
    }
}