zblob 0.3.0

Resumable verified-streaming blob & directory transfer over Zenoh (BLAKE3/bao, range resume, content-addressed dedup)
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
//! The blob client: fetches the manifest, then streams verified bao slices
//! into a caller-chosen destination, resuming interrupted transfers.
//!
//! Every reply is verified against the manifest's BLAKE3 `root` **before** it
//! touches disk (see [`crate::verify`]), so there is no end-of-transfer hash
//! pass and a tampered reply poisons nothing — it is simply dropped and its
//! chunk re-requested. Resume state is a chunk bitfield persisted next to the
//! `.part` file (see [`crate::resume`]); every retry re-derives its query from
//! the bitfield's holes, so "resume" and "retry" are the same code path.
//!
//! The caller chooses the destination path ([`BlobClient::download_to`]) — the
//! server's advisory filename is never joined to any path (the v1 traversal
//! vector, C2).
//!
//! # Transfers are call builders
//!
//! Each entry point returns a builder that runs when awaited, matching
//! `zenoh::Session::get`'s own idiom: [`Download`], [`StagedDownload`],
//! [`DownloadToWriter`] and [`Upload`]. What a transfer cannot do without is
//! positional; progress, cancellation, overwrite policy and multi-origin
//! striping are optional and set on the builder.
//!
//! ```no_run
//! # use zblob::{BlobClient, CancelToken, DownloadRequest, Overwrite};
//! # async fn f(client: BlobClient, req: DownloadRequest, cancel: CancelToken)
//! # -> zblob::Result<()> {
//! let stats = client
//!     .download_to(&req, "/tmp/out.bin".as_ref())
//!     .cancel(&cancel)
//!     .overwrite(Overwrite::Replace)
//!     .await?;
//! # let _ = stats; Ok(()) }
//! ```
//!
//! Cancellation is observed *while waiting on the network*, not between
//! replies — see [`crate::CancelToken`] for why that distinction is the whole
//! reason the token is not an `AtomicBool`.

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use bao_tree::io::sync::Size;
use tokio::io::{AsyncSeekExt, AsyncWriteExt, SeekFrom};
use zenoh::qos::Priority;
use zenoh::query::ConsolidationMode;

use crate::cancel::CancelToken;
use crate::chunk::{MIN_CHUNK_SIZE, TransferChunks};
use crate::error::{BlobError, Result};
use crate::hash::Hash;
use crate::id::BlobId;
use crate::keys::{
    MAX_RANGE_SPANS, availability_key, manifest_key, push_offer_key, push_slice_key, slice_selector,
};
use crate::manifest::{BlobSpec, Manifest, validate_id};
use crate::obs::{TransferStats, zdebug};
use crate::prefix::QueryPrefix;
use crate::progress::{Progress, ProgressSink};
use crate::resume::ResumeState;
use crate::server::{BlobSource, DynReadAt, ReadAtSize, SourceFingerprint};
use crate::verify;
use crate::wire::{Availability, ENC_AVAIL, ENC_MANIFEST, ENC_PUSH, ENC_SLICE, decode};

/// What to do when the destination path already exists at completion time.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum Overwrite {
    /// Fail with [`BlobError::DestinationExists`]. The default.
    ///
    /// Checked *before* the transfer, so an occupied destination costs
    /// nothing. It is checked again at the end — the destination can appear
    /// while a download is in flight — and in that case the finished `.part`
    /// is kept next to it, so the transfer is not thrown away either.
    #[default]
    Refuse,
    /// Atomically replace the existing file.
    Replace,
}

/// Retry/backoff policy for the download loop. An *attempt* is a slice query
/// that made no progress; queries that verify at least one new chunk reset
/// the budget.
#[derive(Debug, Clone)]
pub struct RetryPolicy {
    /// Consecutive no-progress attempts before giving up (default 5).
    pub max_attempts: u32,
    /// Backoff before retry attempt 1 (doubles each attempt; default 250 ms).
    pub base_backoff: Duration,
    /// Backoff ceiling (default 10 s).
    pub max_backoff: Duration,
}

impl Default for RetryPolicy {
    fn default() -> Self {
        RetryPolicy {
            max_attempts: 5,
            base_backoff: Duration::from_millis(250),
            max_backoff: Duration::from_secs(10),
        }
    }
}

impl RetryPolicy {
    fn backoff(&self, attempt: u32) -> Duration {
        let exp = self.base_backoff.saturating_mul(1u32 << attempt.min(16));
        exp.min(self.max_backoff)
    }
}

#[derive(Debug, Clone)]
struct ClientConfig {
    query_timeout: Duration,
    retry: RetryPolicy,
    max_chunks_per_query: u32,
    max_blob_size: u64,
    max_probe_replies: usize,
    overwrite: Overwrite,
    priority: Priority,
}

impl Default for ClientConfig {
    fn default() -> Self {
        ClientConfig {
            query_timeout: Duration::from_secs(30),
            retry: RetryPolicy::default(),
            max_chunks_per_query: 512,
            max_blob_size: 1 << 40, // 1 TiB — a remote peer must not size our disk.
            // How many responders a fan-out probe will collect. The number of
            // peers that answer is not ours to choose, so it is bounded.
            max_probe_replies: 256,
            overwrite: Overwrite::default(),
            // Bulk transfer yields. Replies inherit the *query's* QoS (a
            // server cannot set it), so the only place this can be decided is
            // here — and a multi-megabyte transfer at the default `Data`
            // priority shares a lane with telemetry and alerts, starving them
            // on a constrained link. See `BlobClientBuilder::priority`.
            priority: Priority::DataLow,
        }
    }
}

/// What to download: a blob id, optionally with a pinned BLAKE3 root.
///
/// **Pin the root whenever you know it.** The manifest travels over the same
/// channel as the data, so without a pin the first fetch is
/// trust-on-first-use: integrity holds *within* the transfer (a server cannot
/// mix content), but the server chooses *which* content. With
/// [`DownloadRequest::pinned`], a server offering different bytes is rejected
/// before anything is written.
#[derive(Debug, Clone)]
pub struct DownloadRequest {
    /// The blob id to fetch.
    pub id: String,
    /// If set, the transfer fails unless the manifest's root matches exactly.
    pub expected_root: Option<Hash>,
}

impl DownloadRequest {
    /// Fetch `id`, trusting the manifest's root (TOFU — see type docs).
    pub fn new(id: impl Into<String>) -> Self {
        DownloadRequest {
            id: id.into(),
            expected_root: None,
        }
    }

    /// Fetch `id` and require its content to match `root`.
    pub fn pinned(id: impl Into<String>, root: Hash) -> Self {
        DownloadRequest {
            id: id.into(),
            expected_root: Some(root),
        }
    }

    /// Fetch a **content-addressed** artifact: the id *is* the root, so the
    /// request is pinned by construction and trust-on-first-use is not
    /// expressible.
    ///
    /// This is the natural request shape for a Tier-2 snapshot re-keyed with
    /// [`TreeIndex::keyed_by_root`](crate::TreeIndex::keyed_by_root), and for
    /// any deployment that names blobs by their content hash.
    pub fn by_root(root: Hash) -> Self {
        DownloadRequest {
            id: root.to_string(),
            expected_root: Some(root),
        }
    }
}

/// Where a staged download landed, from [`BlobClient::download_staged`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Staged {
    /// The file, named by the blob's id inside the directory you gave.
    pub path: PathBuf,
    /// The server's advisory filename, reduced to a single safe component —
    /// `None` if it offered none, or offered something unusable.
    ///
    /// A *suggestion*: nothing has been named this, and this crate will never
    /// name anything this. Renaming to it is the caller's decision (and
    /// usually the user's).
    pub suggested: Option<String>,
    /// Transfer statistics, as [`BlobClient::download_to`] returns.
    pub stats: TransferStats,
}

/// What one origin said about a blob, from [`BlobClient::probe`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct BlobProbe {
    /// The prefix that answered — pass it to a [`BlobClient`] to fetch from
    /// this holder specifically.
    pub origin: QueryPrefix,
    /// The manifest this holder serves: identity, size and geometry.
    pub manifest: Manifest,
    /// Which chunks it holds, if it answered the `have` endpoint.
    pub availability: Option<Availability>,
}

/// Recover the origin prefix from a reply key of the form
/// `<origin>/<id>/<endpoint>`.
///
/// The *server* built this key from its own concrete prefix, whatever
/// wildcard the query went through — which is precisely why a probe can
/// attribute its answers at all.
fn origin_of(reply_key: &str, id: &str, endpoint: &str) -> Option<String> {
    let rest = reply_key.strip_suffix(endpoint)?.strip_suffix('/')?;
    let rest = rest.strip_suffix(id)?.strip_suffix('/')?;
    (!rest.is_empty()).then(|| rest.to_string())
}

/// Downloads blobs served by a [`crate::BlobServer`] under the same key prefix.
#[derive(Debug)]
pub struct BlobClient {
    session: zenoh::Session,
    prefix: QueryPrefix,
    cfg: ClientConfig,
    // Single-flight guard: destinations with a download in progress *through
    // this client*. Two concurrent downloads to one path would corrupt each
    // other's .part/sidecar pair.
    active: Arc<std::sync::Mutex<std::collections::HashSet<PathBuf>>>,
}

/// Builder for a [`BlobClient`] (see [`BlobClient::builder`]).
#[derive(Debug)]
pub struct BlobClientBuilder {
    session: zenoh::Session,
    prefix: QueryPrefix,
    cfg: ClientConfig,
}

impl BlobClientBuilder {
    /// Per-query timeout (default 30 s). Transfers larger than one query's
    /// chunk budget span multiple queries, so this bounds *stall* time, not
    /// total transfer time.
    #[must_use]
    pub fn query_timeout(mut self, t: Duration) -> Self {
        self.cfg.query_timeout = t;
        self
    }

    /// Retry/backoff policy (default: 5 attempts, 250 ms base, 10 s cap).
    #[must_use]
    pub fn retry(mut self, retry: RetryPolicy) -> Self {
        self.cfg.retry = retry;
        self
    }

    /// Max chunks requested per query (default 512; must not exceed the
    /// server's own cap or queries are rejected).
    #[must_use]
    pub fn max_chunks_per_query(mut self, n: u32) -> Self {
        self.cfg.max_chunks_per_query = n.max(1);
        self
    }

    /// Upper bound on `total_len` this client will accept from a manifest
    /// (default 1 TiB) — the allocation/preallocation defense.
    #[must_use]
    pub fn max_blob_size(mut self, bytes: u64) -> Self {
        self.cfg.max_blob_size = bytes;
        self
    }

    /// Overwrite policy for the destination (default [`Overwrite::Refuse`]).
    #[must_use]
    pub fn overwrite(mut self, ow: Overwrite) -> Self {
        self.cfg.overwrite = ow;
        self
    }

    /// Zenoh priority for every query this client issues
    /// (default [`Priority::DataLow`]).
    ///
    /// **This is the only place bulk QoS can be set.** Zenoh replies inherit
    /// the querier's QoS — server-side reply-QoS setters are no-ops — so a
    /// blob transfer competes with telemetry unless the *client* yields. The
    /// default deliberately sits below `Priority::Data` so a large transfer
    /// cannot starve an alert on a constrained link. Raise it only if you
    /// know the link is not shared.
    #[must_use]
    pub fn priority(mut self, priority: Priority) -> Self {
        self.cfg.priority = priority;
        self
    }

    /// Build the client.
    pub fn build(self) -> BlobClient {
        BlobClient {
            session: self.session,
            prefix: self.prefix,
            cfg: self.cfg,
            active: Arc::new(std::sync::Mutex::new(std::collections::HashSet::new())),
        }
    }
}

impl BlobClient {
    /// Start building a client for blobs under `key_prefix`.
    pub fn builder(session: &zenoh::Session, key_prefix: QueryPrefix) -> BlobClientBuilder {
        BlobClientBuilder {
            session: session.clone(),
            prefix: key_prefix,
            cfg: ClientConfig::default(),
        }
    }

    /// Build a client with default configuration (see [`BlobClient::builder`]).
    pub fn new(session: &zenoh::Session, key_prefix: QueryPrefix) -> Self {
        Self::builder(session, key_prefix).build()
    }

    /// Fetch and validate just the manifest for blob `id` — probe existence,
    /// size, and root before committing to a download.
    pub async fn fetch_manifest(&self, id: &str) -> Result<Manifest> {
        self.fetch_manifest_matching(id, None).await
    }

    /// Ask every origin that answers what it knows about `id`.
    ///
    /// This is the sequence RFC 07 §2.5 prescribes for a consumer that cannot
    /// name the origin holding a blob: ask for something *tiny* across
    /// origins, then fetch from one chosen origin's concrete key. Both replies
    /// here are small and bounded — a manifest, and a bitfield — so a
    /// wildcard-origin probe is legitimate where a wildcard-origin *fetch*
    /// would be one full copy per responder.
    ///
    /// Each result carries the `origin` that answered, as a prefix you can
    /// hand straight to another [`BlobClient`]. That attribution is the whole
    /// point of probing and was previously left for callers to reconstruct
    /// from raw reply keys.
    ///
    /// Availability is `None` for a holder that answered `manifest` but not
    /// `have`.
    pub async fn probe(&self, id: &str) -> Result<Vec<BlobProbe>> {
        validate_id(id)?;
        let mut found: Vec<BlobProbe> = Vec::new();

        let replies = self
            .session
            .get(manifest_key(self.prefix.as_str(), id))
            .consolidation(ConsolidationMode::None)
            .priority(self.cfg.priority)
            .timeout(self.cfg.query_timeout)
            .await
            .map_err(BlobError::zenoh)?;
        while let Ok(reply) = replies.recv_async().await {
            if found.len() >= self.cfg.max_probe_replies {
                break;
            }
            let Ok(sample) = reply.result() else { continue };
            if !ENC_MANIFEST.matches(sample.encoding()) {
                continue;
            }
            // The reply key names the origin: it is the prefix the *server*
            // built the key from, whatever wildcard we asked through.
            let Some(origin) = origin_of(sample.key_expr().as_str(), id, "manifest") else {
                continue;
            };
            let Ok(manifest) = decode::<Manifest>(&sample.payload().to_bytes()) else {
                continue;
            };
            if manifest.validate(self.cfg.max_blob_size).is_err() || manifest.id != id {
                continue;
            }
            let Ok(origin) = QueryPrefix::new(origin) else {
                continue;
            };
            if found.iter().any(|p| p.origin == origin) {
                continue; // one entry per origin, not per reply
            }
            found.push(BlobProbe {
                origin,
                manifest,
                availability: None,
            });
        }
        if found.is_empty() {
            return Ok(found);
        }

        // Second question, same shape: which chunks does each holder have?
        // A holder that does not answer keeps `None` rather than being
        // dropped — `have` is optional, `manifest` is what makes it a holder.
        let replies = self
            .session
            .get(availability_key(self.prefix.as_str(), id))
            .consolidation(ConsolidationMode::None)
            .priority(self.cfg.priority)
            .timeout(self.cfg.query_timeout)
            .await
            .map_err(BlobError::zenoh)?;
        while let Ok(reply) = replies.recv_async().await {
            let Ok(sample) = reply.result() else { continue };
            if !ENC_AVAIL.matches(sample.encoding()) {
                continue;
            }
            let Some(origin) = origin_of(sample.key_expr().as_str(), id, "have") else {
                continue;
            };
            let Ok(avail) = decode::<Availability>(&sample.payload().to_bytes()) else {
                continue;
            };
            let Some(entry) = found.iter_mut().find(|p| p.origin.as_str() == origin) else {
                continue; // answered `have` but not `manifest`; nothing to attach to
            };
            if avail
                .validate(entry.manifest.chunk_count().unwrap_or(u32::MAX))
                .is_ok()
            {
                entry.availability = Some(avail);
            }
        }
        Ok(found)
    }

    /// Like [`fetch_manifest`](Self::fetch_manifest), but with multi-responder
    /// resilience: a malformed, invalid, mismatched-id, or (when pinned)
    /// wrong-root reply is *skipped*, not fatal — one hostile or stale
    /// responder must not be able to deny a fetch that an honest replica
    /// still answers. The first rejection is kept for diagnostics if nobody
    /// acceptable replies.
    async fn fetch_manifest_matching(
        &self,
        id: &str,
        expected_root: Option<Hash>,
    ) -> Result<Manifest> {
        validate_id(id)?;
        let key = manifest_key(self.prefix.as_str(), id);
        let replies = self
            .session
            .get(&key)
            .consolidation(ConsolidationMode::None)
            .priority(self.cfg.priority)
            .timeout(self.cfg.query_timeout)
            .await
            .map_err(BlobError::zenoh)?;
        let mut rejected: Option<BlobError> = None;
        while let Ok(reply) = replies.recv_async().await {
            let Ok(sample) = reply.result() else { continue };
            if !ENC_MANIFEST.matches(sample.encoding()) {
                continue; // stale/foreign responder; keep listening.
            }
            let verdict = decode::<Manifest>(&sample.payload().to_bytes())
                .and_then(|m| m.validate(self.cfg.max_blob_size).map(|()| m))
                .and_then(|m| {
                    if m.id != id {
                        return Err(BlobError::MalformedMessage(format!(
                            "manifest id {:?} does not match requested {id:?}",
                            m.id
                        )));
                    }
                    if let Some(expected) = expected_root
                        && m.root != expected
                    {
                        return Err(BlobError::RootMismatch {
                            expected,
                            actual: m.root,
                        });
                    }
                    Ok(m)
                });
            match verdict {
                Ok(manifest) => return Ok(manifest),
                Err(e) => {
                    zdebug!(id, error = %e, "skipping unacceptable manifest reply");
                    rejected.get_or_insert(e);
                }
            }
        }
        Err(rejected.unwrap_or_else(|| BlobError::NotFound(id.to_string())))
    }

    /// Download into `dir`, staged under the blob's id.
    ///
    /// [`download_to`](Self::download_to) is deliberately caller-chooses-the
    /// -destination: the server's filename is advisory and this crate never
    /// joins it to a path, which is the structural fix for v1's traversal
    /// vector — applied at the API's shape rather than at the write site. That
    /// stays true here and is not negotiable.
    ///
    /// What it left every caller to reinvent is the *convention* around it,
    /// and both downstream GUIs reinvented the same one: stage under the id,
    /// keep the suggested name aside, offer it in a save-as dialog later.
    /// Staging under the **id** rather than the suggested name is the load
    /// bearing part — two concurrent downloads whose servers both claim
    /// `report.pcap` must not collide.
    ///
    /// Pairing the two in one call means the safe path is also the shortest
    /// one, which is the only way a security property reliably survives
    /// contact with application code.
    /// The Zenoh priority this client's queries (and therefore the replies
    /// they draw) carry.
    ///
    /// Replies inherit the querier's QoS, so the client is the only place
    /// this can be set — see [`BlobClientBuilder::priority`].
    #[must_use]
    pub fn priority(&self) -> Priority {
        self.cfg.priority
    }

    /// Download a blob to the file at `dest` (written via `<dest>.part` + a
    /// resume sidecar, then atomically renamed into place). Returns a
    /// [`Download`] — `.await` it to run the transfer.
    ///
    /// Call again with the same arguments to resume after `Incomplete`,
    /// `Cancelled`, or a crash. Set progress, cancellation, overwrite policy
    /// and striping on the returned builder.
    pub fn download_to<'a>(&'a self, req: &'a DownloadRequest, dest: &'a Path) -> Download<'a> {
        Download {
            client: self,
            req,
            dest,
            sink: None,
            cancel: None,
            overwrite: None,
            holders: None,
        }
    }

    /// Download into `dir`, staged under the blob's id, returning where it
    /// landed and the server's advisory filename. `.await` the returned
    /// [`StagedDownload`] to run it.
    ///
    /// [`download_to`](Self::download_to) is deliberately caller-chooses-the
    /// -destination: the server's filename is advisory and this crate never
    /// joins it to a path, which is the structural fix for v1's traversal
    /// vector — applied at the API's shape rather than at the write site. That
    /// stays true here and is not negotiable.
    ///
    /// What it left every caller to reinvent is the *convention* around it,
    /// and both downstream GUIs reinvented the same one: stage under the id,
    /// keep the suggested name aside, offer it in a save-as dialog later.
    /// Staging under the **id** rather than the suggested name is the load
    /// bearing part — two concurrent downloads whose servers both claim
    /// `report.pcap` must not collide.
    ///
    /// Pairing the two in one call means the safe path is also the shortest
    /// one, which is the only way a security property reliably survives
    /// contact with application code.
    pub fn download_staged<'a>(
        &'a self,
        req: &'a DownloadRequest,
        dir: &'a Path,
    ) -> StagedDownload<'a> {
        StagedDownload {
            inner: self.download_to(req, dir),
        }
    }

    /// Download into `writer`, **without resume**: no `.part`, no sidecar — an
    /// interrupted call must start over, and the writer must be
    /// sized/seekable for the whole blob. `.await` the returned
    /// [`DownloadToWriter`] to run it.
    pub fn download_to_writer<'a, W>(
        &'a self,
        req: &'a DownloadRequest,
        writer: &'a mut W,
    ) -> DownloadToWriter<'a, W>
    where
        W: tokio::io::AsyncWrite + tokio::io::AsyncSeek + Unpin + Send,
    {
        DownloadToWriter {
            client: self,
            req,
            writer,
            sink: None,
            cancel: None,
        }
    }

    /// Upload (push) the file at `path` to a server that has
    /// [`accept_push`](crate::BlobServerBuilder::accept_push) configured,
    /// under `spec`. `.await` the returned [`Upload`] to run it.
    ///
    /// The whole file is hashed locally first; every slice the server receives
    /// is verified against that root, and a completed upload is registered and
    /// served by the receiver. Interrupted uploads resume: the server's offer
    /// reply names exactly the chunks it is still missing. Resolves to the
    /// manifest (distribute `(id, root)` to downloaders).
    pub fn upload_file(&self, spec: BlobSpec, path: impl Into<PathBuf>) -> Upload<'_> {
        Upload {
            client: self,
            spec,
            src: UploadSrc::Path(path.into()),
            token: None,
            sink: None,
            cancel: None,
        }
    }

    /// Upload (push) an arbitrary [`BlobSource`] — an in-memory buffer, a
    /// generated artifact, anything positional — without staging it in a
    /// file first. `.await` the returned [`Upload`] to run it.
    ///
    /// The read-side counterpart of
    /// [`register_source`](crate::BlobServer::register_source): the same
    /// trait a server registers from is what a client can push from, so a
    /// caller holding bytes never pays them to disk just to send them.
    /// Everything [`upload_file`](Self::upload_file) documents holds here
    /// too, with two differences:
    ///
    /// - The source is opened twice (once to hash, once to send), so
    ///   [`BlobSource::open`] must be cheap and reopenable — which the
    ///   provided [`MemoryBlobSource`](crate::MemoryBlobSource) and
    ///   [`FileBlobSource`](crate::FileBlobSource) both are. If the source
    ///   reports a changed [`fingerprint`](BlobSource::fingerprint) between
    ///   those passes, the upload fails with one clear error instead of the
    ///   receiver silently rejecting every slice hashed from stale bytes.
    /// - No [`Progress::Completed`] event is emitted: that event names the
    ///   final *path* of an assembled artifact, and a source upload has
    ///   none. The returned manifest is the completion signal; the last
    ///   [`Progress::Chunk`] event carries the final counts.
    pub fn upload_source(&self, spec: BlobSpec, source: Arc<dyn BlobSource>) -> Upload<'_> {
        Upload {
            client: self,
            spec,
            src: UploadSrc::Source(source),
            token: None,
            sink: None,
            cancel: None,
        }
    }

    async fn run_download_staged(
        &self,
        req: &DownloadRequest,
        dir: &Path,
        sink: &dyn ProgressSink,
        cancel: &CancelToken,
        overwrite: Overwrite,
    ) -> Result<Staged> {
        validate_id(&req.id)?;
        tokio::fs::create_dir_all(dir).await?;
        // `validate_id` has already refused separators, `..` and wildcards, so
        // the id is a single safe component.
        let path = dir.join(&req.id);
        let stats = self
            .run_download_to(req, &path, sink, cancel, overwrite)
            .await?;
        // Re-read rather than plumbing it out of the transfer: the manifest is
        // cheap, and the alternative is threading a value through a function
        // whose whole job is bytes.
        let suggested = self
            .fetch_manifest_matching(&req.id, req.expected_root)
            .await
            .ok()
            .and_then(|m| m.suggested_filename());
        Ok(Staged {
            path,
            suggested,
            stats,
        })
    }

    /// Download a blob to the file at `dest` (written via `<dest>.part` + a
    /// resume sidecar, then atomically renamed into place). Progress events go
    /// to `sink`; a set `cancel` stops cooperatively with state persisted.
    /// Returns per-call [`TransferStats`].
    ///
    /// Call again with the same arguments to resume after `Incomplete`,
    /// `Cancelled`, or a crash.
    async fn run_download_to(
        &self,
        req: &DownloadRequest,
        dest: &Path,
        sink: &dyn ProgressSink,
        cancel: &CancelToken,
        overwrite: Overwrite,
    ) -> Result<TransferStats> {
        // Single-flight: a second concurrent download to the same destination
        // through this client is refused instead of silently corrupting state.
        let guard = ActiveGuard::acquire(&self.active, dest)?;
        let result = self
            .download_to_inner(req, dest, sink, cancel, overwrite)
            .await;
        drop(guard);
        match &result {
            Err(BlobError::Cancelled { .. }) | Ok(_) => {}
            Err(e) => sink.emit(Progress::Failed {
                error: e.to_string(),
            }),
        }
        result
    }

    /// Download `req`, **striping** its chunks across the given holders.
    ///
    /// The ordinary [`download_to`](Self::download_to) asks one key expression
    /// and takes whoever answers. With several replicas that is
    /// multi-*responder* tolerance rather than multi-*source* transfer: reply
    /// consolidation is off, so every matching holder sends every requested
    /// slice and the client discards the duplicates — *after* they have
    /// crossed the wire. Zenoh cannot cancel remote replies in flight, so N
    /// replicas cost N times the bandwidth.
    ///
    /// This addresses each range to exactly one holder's concrete prefix, so
    /// each range crosses the wire once. Holders that reported an
    /// [`Availability`] only receive ranges they claim to have; stragglers at
    /// the end are duplicated to a second holder, which is BitTorrent's
    /// endgame and the standard answer to one slow peer holding up a transfer.
    ///
    /// Get `holders` from [`probe`](Self::probe). With fewer than two this
    /// degrades to `download_to`, which is the right thing rather than an
    /// error.
    async fn run_download_striped(
        &self,
        req: &DownloadRequest,
        dest: &Path,
        holders: &[BlobProbe],
        sink: &dyn ProgressSink,
        cancel: &CancelToken,
        overwrite: Overwrite,
    ) -> Result<TransferStats> {
        if holders.len() < 2 {
            return self
                .run_download_to(req, dest, sink, cancel, overwrite)
                .await;
        }
        let guard = ActiveGuard::acquire(&self.active, dest)?;
        let result = self
            .download_striped_inner(req, dest, holders, sink, cancel, overwrite)
            .await;
        drop(guard);
        if let Err(e) = &result
            && !e.is_cancelled()
        {
            sink.emit(Progress::Failed {
                error: e.to_string(),
            });
        }
        result
    }

    async fn download_striped_inner(
        &self,
        req: &DownloadRequest,
        dest: &Path,
        holders: &[BlobProbe],
        sink: &dyn ProgressSink,
        cancel: &CancelToken,
        overwrite: Overwrite,
    ) -> Result<TransferStats> {
        let started_at = tokio::time::Instant::now();
        if overwrite == Overwrite::Refuse && tokio::fs::try_exists(dest).await? {
            return Err(BlobError::DestinationExists(dest.to_path_buf()));
        }
        // Every holder must agree on the content, or "striping" would be
        // splicing two different blobs together.
        let manifest = holders[0].manifest.clone();
        if let Some(expected) = req.expected_root
            && manifest.root != expected
        {
            return Err(BlobError::RootMismatch {
                expected,
                actual: manifest.root,
            });
        }
        if holders.iter().any(|h| h.manifest.root != manifest.root) {
            return Err(BlobError::MalformedMessage(
                "holders disagree about this blob's root; probe again and pick one set".into(),
            ));
        }
        let chunks = manifest.chunks()?;
        let count = chunks.count();

        if let Some(parent) = dest.parent()
            && !parent.as_os_str().is_empty()
        {
            tokio::fs::create_dir_all(parent).await?;
        }
        let part = part_path(dest);
        let existing_len = tokio::fs::metadata(&part).await.map(|m| m.len()).ok();
        let mut state = match ResumeState::load(&part).await {
            Some(s) if s.matches(&manifest, count) && existing_len == Some(manifest.total_len) => {
                sink.emit(Progress::Resumed {
                    received: s.received(),
                    total: count,
                });
                s
            }
            _ => {
                let file = tokio::fs::File::create(&part).await?;
                file.set_len(manifest.total_len).await?;
                let fresh = ResumeState::fresh(&manifest, count);
                fresh.save_atomic(&part).await?;
                sink.emit(Progress::Started {
                    total_len: manifest.total_len,
                    chunk_count: count,
                });
                fresh
            }
        };
        let mut file = tokio::fs::OpenOptions::new()
            .write(true)
            .open(&part)
            .await?;
        let mut stats = TransferStats {
            chunks_resumed: state.received(),
            ..Default::default()
        };
        self.fill_holes_striped(
            &manifest, &chunks, holders, &mut file, &mut state, &part, sink, cancel, &mut stats,
        )
        .await?;

        file.sync_data().await?;
        drop(file);
        if overwrite == Overwrite::Refuse && tokio::fs::try_exists(dest).await? {
            return Err(BlobError::DestinationExists(dest.to_path_buf()));
        }
        tokio::fs::rename(&part, dest).await?;
        ResumeState::remove(&part).await;
        sink.emit(Progress::Completed {
            path: dest.to_path_buf(),
        });
        stats.elapsed = started_at.elapsed();
        Ok(stats)
    }

    /// Ask every responder which chunks of `id` it holds. Returns one
    /// [`Availability`] per reply — with replicated servers this is how a
    /// caller sees the swarm (with reply consolidation disabled, ordinary
    /// downloads already accept whichever replica answers each chunk first).
    pub async fn fetch_availability(&self, id: &str) -> Result<Vec<Availability>> {
        validate_id(id)?;
        let key = availability_key(self.prefix.as_str(), id);
        let replies = self
            .session
            .get(&key)
            .consolidation(ConsolidationMode::None)
            .priority(self.cfg.priority)
            .timeout(self.cfg.query_timeout)
            .await
            .map_err(BlobError::zenoh)?;
        let mut out = Vec::new();
        while let Ok(reply) = replies.recv_async().await {
            // One responder per entry, and the responder count is not ours to
            // choose — cap it so a flood of repliers cannot grow this vector
            // without bound.
            if out.len() >= self.cfg.max_probe_replies {
                break;
            }
            let Ok(sample) = reply.result() else { continue };
            if !ENC_AVAIL.matches(sample.encoding()) {
                continue;
            }
            // A malformed reply from one responder must not hide the others.
            if let Ok(avail) = decode::<Availability>(&sample.payload().to_bytes())
                && avail
                    .validate(self.cfg.max_blob_size.div_ceil(MIN_CHUNK_SIZE as u64) as u32)
                    .is_ok()
            {
                out.push(avail);
            }
        }
        Ok(out)
    }

    /// Delete any partial download + sidecar for a previous
    /// [`download_to`](Self::download_to) call with destination `dest`.
    pub async fn delete_partial(&self, dest: &Path) {
        let part = part_path(dest);
        let _ = tokio::fs::remove_file(&part).await;
        ResumeState::remove(&part).await;
    }

    /// Upload (push) the file at `path` to a server that has
    /// [`accept_push`](crate::BlobServerBuilder::accept_push) configured,
    /// under `spec`. `token` is an opaque credential the server's
    /// [`PushPolicy`](crate::PushPolicy) sees with every request.
    ///
    /// The whole file is hashed locally first; every slice the server receives
    /// is verified against that root, and a completed upload is registered and
    /// served by the receiver. Interrupted uploads resume: the server's offer
    /// reply names exactly the chunks it is still missing. Returns the
    /// manifest (distribute `(id, root)` to downloaders).
    async fn run_upload(
        &self,
        spec: BlobSpec,
        src: UploadSrc,
        token: Option<Vec<u8>>,
        sink: &dyn ProgressSink,
        cancel: &CancelToken,
    ) -> Result<Manifest> {
        use crate::verify::{MemOutboard, chunk_range};
        // An upload has exactly one destination: the receiver spools state
        // keyed by the id, and its acknowledgements echo the query key
        // verbatim — which for a wildcard query is a key *expression*.
        if !self.prefix.is_concrete() {
            return Err(BlobError::Usage(format!(
                "cannot upload to the wildcard prefix {} — an upload has exactly one destination",
                self.prefix
            )));
        }

        // Hash the source once: outboard + manifest, exactly like server-side
        // registration. For a `BlobSource`, snapshot its fingerprint *before*
        // hashing: the slice pass reopens and re-reads the source later, and
        // a source that mutates in between would produce slices the receiver
        // rejects one by one, with nothing naming the actual culprit.
        let (outboard, total_len, fingerprint) = match &src {
            UploadSrc::Path(path) => {
                let hash_path = path.clone();
                let (ob, len) =
                    tokio::task::spawn_blocking(move || -> std::io::Result<(MemOutboard, u64)> {
                        let file = std::fs::File::open(&hash_path)?;
                        let total_len = file.metadata()?.len();
                        Ok((verify::compute_outboard(file)?, total_len))
                    })
                    .await??;
                (ob, len, None)
            }
            UploadSrc::Source(source) => {
                let source = source.clone();
                tokio::task::spawn_blocking(
                    move || -> std::io::Result<(MemOutboard, u64, Option<SourceFingerprint>)> {
                        let fingerprint = source.fingerprint();
                        let reader = source.open()?;
                        let total_len = reader
                            .size()?
                            .ok_or_else(|| std::io::Error::other("source has no known size"))?;
                        let ob = verify::compute_outboard_sized(
                            verify::ReadAtCursor::new(DynReadAt(&*reader)),
                            total_len,
                        )?;
                        Ok((ob, total_len, fingerprint))
                    },
                )
                .await??
            }
        };
        let outboard = Arc::new(outboard);
        let chunks = TransferChunks::new(spec.chunk_size, total_len)?;
        let manifest = Manifest {
            version: crate::wire::WIRE_VERSION,
            id: BlobId::new(spec.id.clone())?,
            filename: spec.filename,
            total_len,
            chunk_size: spec.chunk_size,
            root: crate::hash::Hash::from(outboard.root),
            created_ms: spec.created_ms,
            // The uploader has nothing to advertise; the *receiver* is the one
            // with limits, and it states them in its offer reply.
            ext: crate::wire::Ext::new(),
        };
        manifest.validate(u64::MAX)?;

        // Offer: the reply lists the chunk ranges the server still wants.
        let offer_key = push_offer_key(self.prefix.as_str(), &manifest.id);
        let mut builder = self
            .session
            .get(&offer_key)
            .consolidation(ConsolidationMode::None)
            .priority(self.cfg.priority)
            .timeout(self.cfg.query_timeout)
            .payload(crate::wire::encode(&manifest)?);
        if let Some(tok) = &token {
            builder = builder.attachment(tok.clone());
        }
        let replies = builder.await.map_err(BlobError::zenoh)?;
        let mut wanted: Option<Vec<(u32, u32)>> = None;
        // A refusal from *a* responder is not a refusal by *the* responder.
        // More than one server can serve one prefix — zensight's netring runs
        // a second `BlobServer` on its artifact prefix deliberately — so a
        // server with push disabled answering "not enabled here" must not
        // abort an upload another server is accepting. Same rule as every
        // `fetch_*` loop (see the crate docs, fact 3): skip, keep the first
        // rejection for diagnostics, fail only if nobody accepts.
        let mut refusal: Option<String> = None;
        while let Ok(reply) = replies.recv_async().await {
            match reply.result() {
                Ok(sample) if ENC_PUSH.matches(sample.encoding()) => {
                    match decode::<Vec<(u32, u32)>>(&sample.payload().to_bytes()) {
                        Ok(ranges) => {
                            wanted = Some(ranges);
                            break;
                        }
                        Err(e) => {
                            refusal.get_or_insert_with(|| format!("undecodable offer reply: {e}"));
                        }
                    }
                }
                Ok(_) => continue,
                Err(e) => {
                    refusal.get_or_insert_with(|| {
                        String::from_utf8_lossy(&e.payload().to_bytes()).into_owned()
                    });
                }
            }
        }
        let wanted = wanted.ok_or_else(|| {
            BlobError::PushDenied(
                refusal.unwrap_or_else(|| "no push endpoint answered the offer".into()),
            )
        })?;

        // The offer reply is attacker input like everything else off the wire:
        // enforce sorted, disjoint, in-bounds, non-empty spans *before* any
        // arithmetic — a hostile responder must not be able to drive an
        // underflow or an out-of-range slice encoding.
        let count = chunks.count();
        let mut prev_end = 0u32;
        for &(a, b) in &wanted {
            if a < prev_end || a >= b || b > count {
                return Err(BlobError::MalformedMessage(format!(
                    "push offer replied malformed wanted ranges ({a}, {b}) for {count} chunks"
                )));
            }
            prev_end = b;
        }
        let to_send: u32 = wanted.iter().map(|(a, b)| b - a).sum();
        if to_send < count {
            sink.emit(Progress::Resumed {
                received: count - to_send,
                total: count,
            });
        } else {
            sink.emit(Progress::Started {
                total_len,
                chunk_count: count,
            });
        }
        zdebug!(id = %manifest.id, total = count, to_send, "push offer accepted");

        // Stream the wanted slices, one acknowledged query each. One reader
        // (the source's second and last open) serves the whole loop, shuttled
        // in and out of each blocking encode.
        let mut sent = count - to_send;
        let mut bytes_sent: u64 = 0;
        let mut reader: Box<dyn ReadAtSize> = tokio::task::spawn_blocking({
            let src = src.clone();
            move || -> std::io::Result<Box<dyn ReadAtSize>> {
                match &src {
                    UploadSrc::Path(path) => Ok(Box::new(std::fs::File::open(path)?)),
                    UploadSrc::Source(source) => source.open(),
                }
            }
        })
        .await??;
        for (start, end) in wanted {
            for index in start..end {
                if cancel.is_cancelled() {
                    sink.emit(Progress::Cancelled {
                        received: sent,
                        total: count,
                    });
                    return Err(BlobError::Cancelled {
                        received: sent,
                        total: count,
                    });
                }
                let ob = outboard.clone();
                let byte_range = chunks.byte_range(index);
                let (r, slice) = tokio::task::spawn_blocking(
                    move || -> (Box<dyn ReadAtSize>, std::io::Result<Vec<u8>>) {
                        let slice = verify::encode_slice(
                            DynReadAt(&*reader),
                            &*ob,
                            chunk_range(byte_range),
                        );
                        (reader, slice)
                    },
                )
                .await?;
                reader = r;
                let slice = slice?;

                let mut b = self
                    .session
                    .get(push_slice_key(self.prefix.as_str(), &manifest.id, index))
                    .consolidation(ConsolidationMode::None)
                    .priority(self.cfg.priority)
                    .timeout(self.cfg.query_timeout)
                    .payload(slice);
                if let Some(tok) = &token {
                    b = b.attachment(tok.clone());
                }
                let replies = b.await.map_err(BlobError::zenoh)?;
                let mut acked = false;
                // As in the offer loop: one responder's error is not the
                // answer. The server that declined the offer holds no spool
                // state for this id and will reject every slice; the one that
                // accepted acknowledges them.
                let mut slice_refusal: Option<String> = None;
                loop {
                    // Waiting for an ack is the long pole of a push round; a
                    // cancel here must not have to outlast the query timeout.
                    let Some(recv) = cancel.until_cancelled(replies.recv_async()).await else {
                        sink.emit(Progress::Cancelled {
                            received: sent,
                            total: count,
                        });
                        return Err(BlobError::Cancelled {
                            received: sent,
                            total: count,
                        });
                    };
                    let Ok(reply) = recv else { break };
                    match reply.result() {
                        Ok(sample) if ENC_PUSH.matches(sample.encoding()) => {
                            match decode::<u32>(&sample.payload().to_bytes()) {
                                Ok(_remaining) => {
                                    acked = true;
                                    break;
                                }
                                Err(e) => {
                                    slice_refusal
                                        .get_or_insert_with(|| format!("undecodable ack: {e}"));
                                }
                            }
                        }
                        Ok(_) => continue,
                        Err(e) => {
                            slice_refusal.get_or_insert_with(|| {
                                String::from_utf8_lossy(&e.payload().to_bytes()).into_owned()
                            });
                        }
                    }
                }
                if !acked {
                    return match slice_refusal {
                        Some(reason) => Err(BlobError::PushDenied(reason)),
                        None => Err(BlobError::Incomplete {
                            received: sent,
                            total: count,
                        }),
                    };
                }
                sent += 1;
                bytes_sent += chunks.len_of(index) as u64;
                sink.emit(Progress::Chunk {
                    index,
                    received: sent,
                    total: count,
                    bytes_received: bytes_sent,
                });
            }
        }
        match &src {
            // `Completed` names the final path of an assembled artifact; a
            // source upload has none, so its completion signal is the
            // returned manifest (see `upload_source`).
            UploadSrc::Path(path) => sink.emit(Progress::Completed { path: path.clone() }),
            UploadSrc::Source(source) => {
                // The hash pass and the slice pass read the source at
                // different times. If it changed in between, some receivers
                // now hold slices of bytes that no longer exist under a root
                // that never described them — fail loudly on the side that
                // can fix it, exactly like the serve-time check in
                // `server.rs`.
                if let (Some(then), Some(now)) = (fingerprint, source.fingerprint())
                    && then != now
                {
                    return Err(BlobError::Protocol(format!(
                        "the source behind blob {:?} changed during the upload (was {} bytes, now {}); the receiver holds slices hashed from bytes that no longer exist — re-upload it",
                        manifest.id, then.len, now.len
                    )));
                }
            }
        }
        Ok(manifest)
    }

    /// Download a blob into any seekable async writer (a `tokio::fs::File`
    /// opened with custom options, an in-memory `Cursor`, …). Verified leaves
    /// are written at their blob offsets exactly like
    /// [`download_to`](Self::download_to), but **without resume**: no `.part`,
    /// no sidecar — an interrupted call must start over, and the writer must
    /// be sized/seekable for the whole blob. Emits `Started`/`Chunk` progress;
    /// the `Ok` return is completion.
    async fn run_download_to_writer<W>(
        &self,
        req: &DownloadRequest,
        writer: &mut W,
        sink: &dyn ProgressSink,
        cancel: &CancelToken,
    ) -> Result<TransferStats>
    where
        W: tokio::io::AsyncWrite + tokio::io::AsyncSeek + Unpin + Send,
    {
        let started_at = tokio::time::Instant::now();
        let (manifest, chunks) = self.start(req).await?;
        let count = chunks.count();
        zdebug!(id = %manifest.id, total_len = manifest.total_len, chunks = count, "writer download start");
        sink.emit(Progress::Started {
            total_len: manifest.total_len,
            chunk_count: count,
        });

        let mut state = ResumeState::fresh(&manifest, count);
        let mut stats = TransferStats::default();
        let mut target = WriterTarget(writer);
        let result = self
            .fill_holes(
                &manifest,
                &chunks,
                &mut target,
                &mut state,
                None,
                sink,
                cancel,
                &mut stats,
            )
            .await;
        match &result {
            Err(BlobError::Cancelled { .. }) | Ok(_) => {}
            Err(e) => sink.emit(Progress::Failed {
                error: e.to_string(),
            }),
        }
        result?;
        stats.elapsed = started_at.elapsed();
        Ok(stats)
    }

    async fn download_to_inner(
        &self,
        req: &DownloadRequest,
        dest: &Path,
        sink: &dyn ProgressSink,
        cancel: &CancelToken,
        overwrite: Overwrite,
    ) -> Result<TransferStats> {
        let started_at = tokio::time::Instant::now();

        // Refuse *before* transferring, not after.
        //
        // This used to be checked once the download had completed and been
        // fsynced, so a full transfer crossed the wire — and the `.part` was
        // preallocated to the remote-supplied total_len — for a request that
        // was then refused. It is re-checked after the transfer as well, since
        // the destination can appear while we are fetching; that late check is
        // the TOCTOU backstop, not the policy.
        if overwrite == Overwrite::Refuse && tokio::fs::try_exists(dest).await? {
            return Err(BlobError::DestinationExists(dest.to_path_buf()));
        }

        let (manifest, chunks) = self.start(req).await?;
        let count = chunks.count();
        zdebug!(id = %manifest.id, total_len = manifest.total_len, chunks = count, "download start");

        if let Some(parent) = dest.parent()
            && !parent.as_os_str().is_empty()
        {
            tokio::fs::create_dir_all(parent).await?;
        }
        let part = part_path(dest);

        // Resume probe: reuse a matching partial, else start fresh. A sidecar
        // that doesn't match (different id/root/geometry → a regenerated
        // source) is discarded rather than spliced.
        let existing_len = tokio::fs::metadata(&part).await.map(|m| m.len()).ok();
        let mut state = match ResumeState::load(&part).await {
            Some(s) if s.matches(&manifest, count) && existing_len == Some(manifest.total_len) => {
                sink.emit(Progress::Resumed {
                    received: s.received(),
                    total: count,
                });
                s
            }
            _ => {
                let file = tokio::fs::File::create(&part).await?;
                file.set_len(manifest.total_len).await?;
                let fresh = ResumeState::fresh(&manifest, count);
                fresh.save_atomic(&part).await?;
                sink.emit(Progress::Started {
                    total_len: manifest.total_len,
                    chunk_count: count,
                });
                fresh
            }
        };

        // Open the partial for writing without truncating (we may be resuming).
        let mut file = tokio::fs::OpenOptions::new()
            .write(true)
            .open(&part)
            .await?;

        let mut stats = TransferStats {
            chunks_resumed: state.received(),
            ..Default::default()
        };
        self.fill_holes(
            &manifest,
            &chunks,
            &mut file,
            &mut state,
            Some(&part),
            sink,
            cancel,
            &mut stats,
        )
        .await?;

        // Every byte on disk was verified against the root as it was written —
        // no second hash pass. Make it durable, then move it into place.
        file.sync_data().await?;
        drop(file);
        if overwrite == Overwrite::Refuse && tokio::fs::try_exists(dest).await? {
            return Err(BlobError::DestinationExists(dest.to_path_buf()));
        }
        tokio::fs::rename(&part, dest).await?;
        ResumeState::remove(&part).await;
        sink.emit(Progress::Completed {
            path: dest.to_path_buf(),
        });
        stats.elapsed = started_at.elapsed();
        zdebug!(id = %manifest.id, fetched = stats.chunks_fetched, rejected = stats.rejected, "download complete");
        Ok(stats)
    }

    /// Fetch + validate the manifest and enforce root pinning (pinning is
    /// applied per reply, so a wrong-root responder cannot mask a right one).
    async fn start(&self, req: &DownloadRequest) -> Result<(Manifest, TransferChunks)> {
        let manifest = self
            .fetch_manifest_matching(&req.id, req.expected_root)
            .await?;
        // An empty blob carries no slices, so nothing later proves the root;
        // check it here or a server could serve "verified" emptiness.
        if manifest.total_len == 0 && manifest.root != Hash::of(b"") {
            return Err(BlobError::RootMismatch {
                expected: Hash::of(b""),
                actual: manifest.root,
            });
        }
        let chunks = manifest.chunks()?;
        Ok((manifest, chunks))
    }

    /// The retry/resume loop: query the bitfield's holes until full or out of
    /// attempts. Progress ≡ the bitfield, so resume and retry are one path.
    /// `part` enables sidecar persistence (file downloads); `None` keeps the
    /// bitfield purely in memory (writer downloads).
    #[allow(clippy::too_many_arguments)]
    async fn fill_holes<T: SliceTarget>(
        &self,
        manifest: &Manifest,
        chunks: &TransferChunks,
        file: &mut T,
        state: &mut ResumeState,
        part: Option<&Path>,
        sink: &dyn ProgressSink,
        cancel: &CancelToken,
        stats: &mut TransferStats,
    ) -> Result<()> {
        let count = chunks.count();
        let root: blake3::Hash = manifest.root.into();
        let mut bytes_received: u64 = (0..count)
            .filter(|i| state.is_set(*i))
            .map(|i| chunks.len_of(i) as u64)
            .sum();
        let mut no_progress = 0u32;
        let mut marks_since_save = 0u32;
        let mut last_save = tokio::time::Instant::now();

        while !state.is_complete(count) {
            if cancel.is_cancelled() {
                return Self::persist_cancel(file, state, part, sink, count).await;
            }

            // Take as many holes as one query may carry — clamped to whatever
            // the *server* said it accepts. Without this a server that lowered
            // its cap rejected every query from a client with the old default,
            // and the client had no way to find out why.
            let mut holes = state.missing_ranges(count);
            holes.truncate(MAX_RANGE_SPANS);
            let mut budget = manifest
                .max_chunks_per_query()
                .map_or(self.cfg.max_chunks_per_query, |served| {
                    self.cfg.max_chunks_per_query.min(served)
                })
                .max(1);
            for r in holes.iter_mut() {
                let take = (r.end - r.start).min(budget);
                r.end = r.start + take;
                budget -= take;
            }
            holes.retain(|r| !r.is_empty());

            let selector = slice_selector(self.prefix.as_str(), &manifest.id, &holes);
            let before = state.received();
            // Counted here as well as on the striped and tier-2 paths: this
            // one was omitted, so `queries` — documented as "Zenoh queries
            // this call issued", and the number a caller uses to tell whether
            // range batching is working — reported 0 for every ordinary
            // single-origin download.
            stats.queries += 1;
            let replies = self
                .session
                .get(&selector)
                .consolidation(ConsolidationMode::None)
                .priority(self.cfg.priority)
                .timeout(self.cfg.query_timeout)
                .await
                .map_err(BlobError::zenoh)?;

            loop {
                // Wait *and* watch the token: a cancel must not be held up by
                // a reply that may never arrive (see `cancel.rs`). `None` here
                // is cancellation, which persists and returns; a closed channel
                // is an exhausted round, which falls through to the retry
                // logic below. Collapsing the two would turn a cancel into a
                // backoff sleep.
                let Some(recv) = cancel.until_cancelled(replies.recv_async()).await else {
                    drop(replies);
                    return Self::persist_cancel(file, state, part, sink, count).await;
                };
                let Ok(reply) = recv else { break };
                let Ok(sample) = reply.result() else { continue };
                if !ENC_SLICE.matches(sample.encoding()) {
                    continue;
                }
                let Some(index) = parse_slice_index(sample.key_expr().as_str()) else {
                    continue;
                };
                if index >= count || state.is_set(index) {
                    continue; // duplicate (consolidation None) or nonsense.
                }
                // Verify-decode this slice against the pinned root; a bad
                // slice is dropped alone and its chunk stays a hole.
                let payload = sample.payload().to_bytes();
                let byte_range = chunks.byte_range(index);
                let mut leaves: Vec<(u64, Vec<u8>)> = Vec::new();
                let decoded = verify::decode_slice(
                    &root,
                    manifest.total_len,
                    verify::chunk_range(byte_range),
                    &payload,
                    |off, data| {
                        leaves.push((off, data.to_vec()));
                        Ok(())
                    },
                );
                if decoded.is_err() {
                    stats.rejected += 1;
                    zdebug!(id = %manifest.id, index, "rejected unverifiable slice");
                    continue;
                }
                for (off, data) in leaves {
                    file.write_leaf(off, &data).await?;
                }
                if state.mark(index) {
                    bytes_received += chunks.len_of(index) as u64;
                    stats.chunks_fetched += 1;
                    stats.bytes_fetched += chunks.len_of(index) as u64;
                    marks_since_save += 1;
                    sink.emit(Progress::Chunk {
                        index,
                        received: state.received(),
                        total: count,
                        bytes_received,
                    });
                    // Batched persistence: data first (commit), then bits —
                    // bits must never claim data the OS hasn't received.
                    if let Some(part) = part
                        && (marks_since_save >= 64 || last_save.elapsed() > Duration::from_secs(2))
                    {
                        file.commit().await?;
                        state.save_atomic(part).await?;
                        marks_since_save = 0;
                        last_save = tokio::time::Instant::now();
                    }
                }
            }

            if state.is_complete(count) {
                break;
            }
            if state.received() == before {
                no_progress += 1;
                stats.retries += 1;
                zdebug!(id = %manifest.id, attempt = no_progress, "no progress; backing off");
                if no_progress >= self.cfg.retry.max_attempts {
                    file.commit().await?;
                    if let Some(part) = part {
                        state.save_atomic(part).await?;
                    }
                    return Err(BlobError::Incomplete {
                        received: state.received(),
                        total: count,
                    });
                }
                tokio::time::sleep(self.cfg.retry.backoff(no_progress - 1)).await;
            } else {
                no_progress = 0;
            }
        }

        file.commit().await?;
        if let Some(part) = part {
            state.save_atomic(part).await?;
        }
        Ok(())
    }

    /// One striping round: partition the outstanding holes across holders,
    /// query each holder for only its share, and write whatever verifies.
    #[allow(clippy::too_many_arguments)]
    async fn fill_holes_striped(
        &self,
        manifest: &Manifest,
        chunks: &TransferChunks,
        holders: &[BlobProbe],
        file: &mut tokio::fs::File,
        state: &mut ResumeState,
        part: &Path,
        sink: &dyn ProgressSink,
        cancel: &CancelToken,
        stats: &mut TransferStats,
    ) -> Result<()> {
        let count = chunks.count();
        let root: blake3::Hash = manifest.root.into();
        let total_len = manifest.total_len;
        let mut bytes_received: u64 = (0..count)
            .filter(|i| state.is_set(*i))
            .map(|i| chunks.len_of(i) as u64)
            .sum();
        let mut no_progress = 0u32;
        // Per-holder rejection tally: a peer that keeps sending unusable
        // replies is dropped from the rotation rather than re-raced forever.
        let mut rejects: Vec<u32> = vec![0; holders.len()];
        let budget = manifest
            .max_chunks_per_query()
            .map_or(self.cfg.max_chunks_per_query, |served| {
                self.cfg.max_chunks_per_query.min(served)
            })
            .max(1);

        while !state.is_complete(count) {
            if cancel.is_cancelled() {
                return Self::persist_cancel(file, state, Some(part), sink, count).await;
            }
            let before = state.received();

            // Which holders are still worth asking?
            let live: Vec<usize> = (0..holders.len())
                .filter(|i| rejects[*i] < self.cfg.retry.max_attempts.max(1) * 8)
                .collect();
            if live.is_empty() {
                return Err(BlobError::Incomplete {
                    received: state.received(),
                    total: count,
                });
            }

            // Deal the outstanding chunks out, round-robin, skipping a holder
            // that says it does not have one. The last few are duplicated to a
            // second holder — endgame mode: one slow peer must not hold up the
            // tail of an otherwise finished transfer.
            let missing: Vec<u32> = (0..count).filter(|i| !state.is_set(*i)).collect();
            let endgame = missing.len() <= live.len().max(2) * 2;
            let mut assignment: Vec<Vec<u32>> = vec![Vec::new(); holders.len()];
            let mut cursor = 0usize;
            for &idx in missing.iter().take(budget as usize * live.len()) {
                let mut dealt = 0;
                let copies = if endgame { 2.min(live.len()) } else { 1 };
                while dealt < copies {
                    let h = live[cursor % live.len()];
                    cursor += 1;
                    let claims = holders[h]
                        .availability
                        .as_ref()
                        .is_none_or(|a| a.is_set(idx));
                    if claims && assignment[h].len() < budget as usize {
                        assignment[h].push(idx);
                        dealt += 1;
                    }
                    // Give up on this chunk for this round if nobody claims it.
                    if cursor.is_multiple_of(live.len()) && dealt == 0 {
                        break;
                    }
                }
            }

            // Ask each holder for its share, concurrently; verified leaves come
            // back over a channel so the file stays under one writer.
            type Verified = (u32, Vec<(u64, Vec<u8>)>, usize, u32);
            let (tx, mut rx) = tokio::sync::mpsc::channel::<Verified>(live.len() * 2);
            let mut tasks = tokio::task::JoinSet::new();
            for h in live.iter().copied() {
                let indices = std::mem::take(&mut assignment[h]);
                if indices.is_empty() {
                    continue;
                }
                let ranges = coalesce(&indices);
                let selector = slice_selector(holders[h].origin.as_str(), &manifest.id, &ranges);
                let session = self.session.clone();
                let timeout = self.cfg.query_timeout;
                let priority = self.cfg.priority;
                let chunks = *chunks;
                let tx = tx.clone();
                tasks.spawn(async move {
                    let Ok(replies) = session
                        .get(&selector)
                        .consolidation(ConsolidationMode::None)
                        .priority(priority)
                        .timeout(timeout)
                        .await
                    else {
                        return;
                    };
                    while let Ok(reply) = replies.recv_async().await {
                        let Ok(sample) = reply.result() else { continue };
                        if !ENC_SLICE.matches(sample.encoding()) {
                            continue;
                        }
                        let Some(index) = parse_slice_index(sample.key_expr().as_str()) else {
                            continue;
                        };
                        let mut leaves: Vec<(u64, Vec<u8>)> = Vec::new();
                        let ok = verify::decode_slice(
                            &root,
                            total_len,
                            verify::chunk_range(chunks.byte_range(index)),
                            &sample.payload().to_bytes(),
                            |off, data| {
                                leaves.push((off, data.to_vec()));
                                Ok(())
                            },
                        )
                        .is_ok();
                        let msg = if ok {
                            (index, leaves, h, 0)
                        } else {
                            (index, Vec::new(), h, 1)
                        };
                        if tx.send(msg).await.is_err() {
                            return;
                        }
                    }
                });
            }
            drop(tx);

            loop {
                // As in the single-origin loop: cancellation must be observed
                // while waiting on the holders, not only between rounds.
                // Dropping `tasks` here aborts the outstanding queries.
                let Some(next) = cancel.until_cancelled(rx.recv()).await else {
                    return Self::persist_cancel(file, state, Some(part), sink, count).await;
                };
                let Some((index, leaves, holder, rejected)) = next else {
                    break;
                };
                if rejected > 0 {
                    rejects[holder] += rejected;
                    stats.rejected += rejected;
                    continue;
                }
                if index >= count || state.is_set(index) {
                    continue; // an endgame duplicate that lost the race
                }
                for (off, data) in leaves {
                    file.write_leaf(off, &data).await?;
                }
                if state.mark(index) {
                    bytes_received += chunks.len_of(index) as u64;
                    stats.chunks_fetched += 1;
                    stats.bytes_fetched += chunks.len_of(index) as u64;
                    sink.emit(Progress::Chunk {
                        index,
                        received: state.received(),
                        total: count,
                        bytes_received,
                    });
                }
            }
            tasks.shutdown().await;
            stats.queries += live.len() as u64;
            file.commit().await?;
            state.save_atomic(part).await?;

            if state.is_complete(count) {
                break;
            }
            if state.received() == before {
                no_progress += 1;
                stats.retries += 1;
                if no_progress >= self.cfg.retry.max_attempts {
                    return Err(BlobError::Incomplete {
                        received: state.received(),
                        total: count,
                    });
                }
                tokio::time::sleep(self.cfg.retry.backoff(no_progress - 1)).await;
            } else {
                no_progress = 0;
            }
        }
        file.commit().await?;
        state.save_atomic(part).await?;
        Ok(())
    }

    async fn persist_cancel<T: SliceTarget>(
        file: &mut T,
        state: &ResumeState,
        part: Option<&Path>,
        sink: &dyn ProgressSink,
        count: u32,
    ) -> Result<()> {
        file.commit().await?;
        if let Some(part) = part {
            state.save_atomic(part).await?;
        }
        sink.emit(Progress::Cancelled {
            received: state.received(),
            total: count,
        });
        Err(BlobError::Cancelled {
            received: state.received(),
            total: count,
        })
    }
}

/// A configured download, awaited to run it.
///
/// Returned by [`BlobClient::download_to`]. The two arguments a transfer
/// cannot do without — what to fetch and where to put it — are positional;
/// progress, cancellation, overwrite policy and multi-origin striping are
/// optional and set here.
///
/// The old shape took all five positionally, which meant `&()` and
/// `&CancelToken::new()` at nearly every call site: two arguments that
/// existed to say "no thanks", and could be swapped with the ones that
/// mattered without the compiler noticing.
///
/// ```no_run
/// # use zblob::{BlobClient, DownloadRequest, CancelToken, Overwrite};
/// # async fn f(client: BlobClient, req: DownloadRequest, cancel: CancelToken) -> zblob::Result<()> {
/// let stats = client.download_to(&req, "/tmp/out.bin".as_ref()).await?;
///
/// let stats = client
///     .download_to(&req, "/tmp/out.bin".as_ref())
///     .cancel(&cancel)
///     .overwrite(Overwrite::Replace)
///     .await?;
/// # let _ = stats; Ok(()) }
/// ```
#[must_use = "a download does nothing until it is awaited"]
pub struct Download<'a> {
    client: &'a BlobClient,
    req: &'a DownloadRequest,
    dest: &'a Path,
    sink: Option<&'a dyn ProgressSink>,
    cancel: Option<&'a CancelToken>,
    overwrite: Option<Overwrite>,
    holders: Option<&'a [BlobProbe]>,
}

impl std::fmt::Debug for Download<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Download")
            .field("req", &self.req)
            .field("dest", &self.dest)
            .field("overwrite", &self.overwrite)
            .field("holders", &self.holders.map(<[BlobProbe]>::len))
            .finish_non_exhaustive()
    }
}

impl<'a> Download<'a> {
    /// Send progress events to `sink` (default: discard them).
    pub fn progress(mut self, sink: &'a dyn ProgressSink) -> Self {
        self.sink = Some(sink);
        self
    }

    /// Stop when `cancel` is cancelled, persisting resume state
    /// (default: never).
    pub fn cancel(mut self, cancel: &'a CancelToken) -> Self {
        self.cancel = Some(cancel);
        self
    }

    /// Override the client's overwrite policy for this transfer.
    ///
    /// It was previously settable only per *client*, which is the wrong
    /// granularity: whether replacing an existing file is acceptable is a
    /// property of the transfer, not of the connection.
    pub fn overwrite(mut self, policy: Overwrite) -> Self {
        self.overwrite = Some(policy);
        self
    }

    /// Fetch from several holders at once, dealing chunks between them.
    ///
    /// Get `holders` from [`BlobClient::probe`]. With fewer than two this
    /// degrades to an ordinary download, which is the right thing rather than
    /// an error.
    pub fn striped(mut self, holders: &'a [BlobProbe]) -> Self {
        self.holders = Some(holders);
        self
    }
}

/// The no-op progress sink used when a call sets none.
pub(crate) const NO_PROGRESS: &(dyn ProgressSink + 'static) = &();

impl<'a> std::future::IntoFuture for Download<'a> {
    type Output = Result<TransferStats>;
    type IntoFuture =
        std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + Send + 'a>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            let sink = self.sink.unwrap_or(NO_PROGRESS);
            let fresh;
            let cancel = match self.cancel {
                Some(c) => c,
                None => {
                    fresh = CancelToken::new();
                    &fresh
                }
            };
            let overwrite = self.overwrite.unwrap_or(self.client.cfg.overwrite);
            match self.holders {
                Some(holders) => {
                    self.client
                        .run_download_striped(self.req, self.dest, holders, sink, cancel, overwrite)
                        .await
                }
                None => {
                    self.client
                        .run_download_to(self.req, self.dest, sink, cancel, overwrite)
                        .await
                }
            }
        })
    }
}

/// A configured staged download, awaited to run it. See
/// [`BlobClient::download_staged`].
#[must_use = "a download does nothing until it is awaited"]
#[derive(Debug)]
pub struct StagedDownload<'a> {
    inner: Download<'a>,
}

impl<'a> StagedDownload<'a> {
    /// Send progress events to `sink` (default: discard them).
    pub fn progress(mut self, sink: &'a dyn ProgressSink) -> Self {
        self.inner = self.inner.progress(sink);
        self
    }

    /// Stop when `cancel` is cancelled, persisting resume state.
    pub fn cancel(mut self, cancel: &'a CancelToken) -> Self {
        self.inner = self.inner.cancel(cancel);
        self
    }

    /// Override the client's overwrite policy for this transfer.
    pub fn overwrite(mut self, policy: Overwrite) -> Self {
        self.inner = self.inner.overwrite(policy);
        self
    }
}

impl<'a> std::future::IntoFuture for StagedDownload<'a> {
    type Output = Result<Staged>;
    type IntoFuture =
        std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + Send + 'a>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            let d = self.inner;
            let sink = d.sink.unwrap_or(NO_PROGRESS);
            let fresh;
            let cancel = match d.cancel {
                Some(c) => c,
                None => {
                    fresh = CancelToken::new();
                    &fresh
                }
            };
            let overwrite = d.overwrite.unwrap_or(d.client.cfg.overwrite);
            d.client
                .run_download_staged(d.req, d.dest, sink, cancel, overwrite)
                .await
        })
    }
}

/// A configured download into a writer, awaited to run it. See
/// [`BlobClient::download_to_writer`].
#[must_use = "a download does nothing until it is awaited"]
pub struct DownloadToWriter<'a, W> {
    client: &'a BlobClient,
    req: &'a DownloadRequest,
    writer: &'a mut W,
    sink: Option<&'a dyn ProgressSink>,
    cancel: Option<&'a CancelToken>,
}

impl<W> std::fmt::Debug for DownloadToWriter<'_, W> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DownloadToWriter")
            .field("req", &self.req)
            .finish_non_exhaustive()
    }
}

impl<'a, W> DownloadToWriter<'a, W> {
    /// Send progress events to `sink` (default: discard them).
    pub fn progress(mut self, sink: &'a dyn ProgressSink) -> Self {
        self.sink = Some(sink);
        self
    }

    /// Stop when `cancel` is cancelled. There is no resume state to persist
    /// here — an interrupted writer download starts over.
    pub fn cancel(mut self, cancel: &'a CancelToken) -> Self {
        self.cancel = Some(cancel);
        self
    }
}

impl<'a, W> std::future::IntoFuture for DownloadToWriter<'a, W>
where
    W: tokio::io::AsyncWrite + tokio::io::AsyncSeek + Unpin + Send,
{
    type Output = Result<TransferStats>;
    type IntoFuture =
        std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + Send + 'a>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            let sink = self.sink.unwrap_or(NO_PROGRESS);
            let fresh;
            let cancel = match self.cancel {
                Some(c) => c,
                None => {
                    fresh = CancelToken::new();
                    &fresh
                }
            };
            self.client
                .run_download_to_writer(self.req, self.writer, sink, cancel)
                .await
        })
    }
}

/// What an upload reads from: the two entry points' single meeting point.
#[derive(Clone)]
enum UploadSrc {
    /// [`BlobClient::upload_file`] — opened as a plain `std::fs::File`.
    Path(PathBuf),
    /// [`BlobClient::upload_source`] — opened through the trait.
    Source(Arc<dyn BlobSource>),
}

impl std::fmt::Debug for UploadSrc {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UploadSrc::Path(path) => f.debug_tuple("Path").field(path).finish(),
            UploadSrc::Source(_) => f.write_str("Source"),
        }
    }
}

/// A configured upload, awaited to run it. See [`BlobClient::upload_file`]
/// and [`BlobClient::upload_source`].
#[must_use = "an upload does nothing until it is awaited"]
pub struct Upload<'a> {
    client: &'a BlobClient,
    spec: BlobSpec,
    src: UploadSrc,
    token: Option<Vec<u8>>,
    sink: Option<&'a dyn ProgressSink>,
    cancel: Option<&'a CancelToken>,
}

impl std::fmt::Debug for Upload<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Upload")
            .field("spec", &self.spec)
            .field("src", &self.src)
            .field("token", &self.token.as_ref().map(Vec::len))
            .finish_non_exhaustive()
    }
}

impl<'a> Upload<'a> {
    /// Attach an opaque credential the server's
    /// [`PushPolicy`](crate::PushPolicy) sees with every request.
    pub fn token(mut self, token: impl Into<Vec<u8>>) -> Self {
        self.token = Some(token.into());
        self
    }

    /// Send progress events to `sink` (default: discard them).
    pub fn progress(mut self, sink: &'a dyn ProgressSink) -> Self {
        self.sink = Some(sink);
        self
    }

    /// Stop when `cancel` is cancelled. The server keeps its spool, so a
    /// later upload of the same id resumes from what it already holds.
    pub fn cancel(mut self, cancel: &'a CancelToken) -> Self {
        self.cancel = Some(cancel);
        self
    }
}

impl<'a> std::future::IntoFuture for Upload<'a> {
    type Output = Result<Manifest>;
    type IntoFuture =
        std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + Send + 'a>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            let sink = self.sink.unwrap_or(NO_PROGRESS);
            let fresh;
            let cancel = match self.cancel {
                Some(c) => c,
                None => {
                    fresh = CancelToken::new();
                    &fresh
                }
            };
            self.client
                .run_upload(self.spec, self.src, self.token, sink, cancel)
                .await
        })
    }
}

/// Where verified leaves land: a positional write plus a durability point.
trait SliceTarget: Send {
    /// Write verified bytes at their blob offset.
    async fn write_leaf(&mut self, offset: u64, data: &[u8]) -> Result<()>;
    /// Make everything written so far durable/visible (fsync for files,
    /// flush for writers).
    async fn commit(&mut self) -> Result<()>;
}

impl SliceTarget for tokio::fs::File {
    async fn write_leaf(&mut self, offset: u64, data: &[u8]) -> Result<()> {
        self.seek(SeekFrom::Start(offset)).await?;
        self.write_all(data).await?;
        Ok(())
    }
    async fn commit(&mut self) -> Result<()> {
        self.sync_data().await?;
        Ok(())
    }
}

/// Adapter for [`BlobClient::download_to_writer`].
struct WriterTarget<'a, W>(&'a mut W);

impl<W> SliceTarget for WriterTarget<'_, W>
where
    W: tokio::io::AsyncWrite + tokio::io::AsyncSeek + Unpin + Send,
{
    async fn write_leaf(&mut self, offset: u64, data: &[u8]) -> Result<()> {
        self.0.seek(SeekFrom::Start(offset)).await?;
        self.0.write_all(data).await?;
        Ok(())
    }
    async fn commit(&mut self) -> Result<()> {
        self.0.flush().await?;
        Ok(())
    }
}

/// Turn a sorted list of chunk indices into the coalesced half-open spans the
/// `ranges` selector grammar wants.
fn coalesce(indices: &[u32]) -> Vec<std::ops::Range<u32>> {
    let mut out: Vec<std::ops::Range<u32>> = Vec::new();
    for &i in indices {
        match out.last_mut() {
            Some(last) if last.end == i => last.end = i + 1,
            _ => out.push(i..i + 1),
        }
    }
    out.truncate(MAX_RANGE_SPANS);
    out
}

/// RAII entry in a client's active-destination set.
struct ActiveGuard {
    active: Arc<std::sync::Mutex<std::collections::HashSet<PathBuf>>>,
    dest: PathBuf,
}

impl ActiveGuard {
    fn acquire(
        active: &Arc<std::sync::Mutex<std::collections::HashSet<PathBuf>>>,
        dest: &Path,
    ) -> Result<Self> {
        let mut set = active.lock().unwrap_or_else(|e| e.into_inner());
        if !set.insert(dest.to_path_buf()) {
            return Err(BlobError::Usage(format!(
                "a download to {dest:?} is already in progress on this client"
            )));
        }
        Ok(ActiveGuard {
            active: active.clone(),
            dest: dest.to_path_buf(),
        })
    }
}

impl Drop for ActiveGuard {
    fn drop(&mut self) {
        self.active
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .remove(&self.dest);
    }
}

/// The partial-file path for a destination: `<dest>.part`.
fn part_path(dest: &Path) -> PathBuf {
    let mut p = dest.as_os_str().to_os_string();
    p.push(".part");
    PathBuf::from(p)
}

/// Parse the slice index from a `…/slice/<index>` key, if present.
fn parse_slice_index(key: &str) -> Option<u32> {
    let (head, idx) = key.rsplit_once('/')?;
    if head.ends_with("/slice") {
        idx.parse().ok()
    } else {
        None
    }
}

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

    #[test]
    fn slice_index_parsing() {
        assert_eq!(parse_slice_index("p/A/slice/7"), Some(7));
        assert_eq!(parse_slice_index("p/A/manifest"), None);
        assert_eq!(parse_slice_index("p/A/slice/x"), None);
        assert_eq!(parse_slice_index("p/A/chunk/7"), None); // v1 keys fail closed
    }

    #[test]
    fn part_path_appends_extension() {
        assert_eq!(
            part_path(Path::new("/tmp/out/file.bin")),
            Path::new("/tmp/out/file.bin.part")
        );
    }

    #[test]
    fn backoff_doubles_and_caps() {
        let r = RetryPolicy::default();
        assert_eq!(r.backoff(0), Duration::from_millis(250));
        assert_eq!(r.backoff(1), Duration::from_millis(500));
        assert_eq!(r.backoff(10), Duration::from_secs(10)); // capped
    }

    /// `coalesce` decides how many spans a striped query carries, and the
    /// range grammar caps that at `MAX_RANGE_SPANS` — so merging one range too
    /// few turns a legal request into a refused one. It is pure, load-bearing,
    /// and was untested.
    #[test]
    fn coalesce_merges_exactly_the_adjacent_runs() {
        assert_eq!(coalesce(&[]), Vec::<std::ops::Range<u32>>::new());
        assert_eq!(coalesce(&[5]), vec![5..6]);
        assert_eq!(coalesce(&[0, 1, 2]), vec![0..3]);
        assert_eq!(coalesce(&[0, 2, 4]), vec![0..1, 2..3, 4..5]);
        assert_eq!(coalesce(&[0, 1, 3, 4, 5, 9]), vec![0..2, 3..6, 9..10]);
        // Only *consecutive* adjacency merges: the input is the client's own
        // ascending hole list, and a repeat would otherwise silently extend a
        // range past what was asked for.
        assert_eq!(coalesce(&[1, 1]), vec![1..2, 1..2]);
        assert_eq!(coalesce(&[3, 1]), vec![3..4, 1..2]);

        // The covered set is exactly the input, for any ascending run.
        let indices: Vec<u32> = (0..50).filter(|i| i % 7 != 0).collect();
        let covered: Vec<u32> = coalesce(&indices).into_iter().flatten().collect();
        assert_eq!(covered, indices);
    }

    /// `origin_of` is how a wildcard probe attributes each answer to the
    /// holder that sent it — get it wrong and a client fetches from a prefix
    /// nobody serves.
    #[test]
    fn origin_of_strips_exactly_the_id_and_endpoint() {
        assert_eq!(
            origin_of("v1/host-a/@blob/art/blob-1/manifest", "blob-1", "manifest").as_deref(),
            Some("v1/host-a/@blob/art")
        );
        assert_eq!(
            origin_of("p/blob-1/have", "blob-1", "have").as_deref(),
            Some("p")
        );
        // A different id, endpoint, or a key with nothing left over is not
        // this blob's origin.
        assert_eq!(origin_of("p/other/manifest", "blob-1", "manifest"), None);
        assert_eq!(origin_of("p/blob-1/have", "blob-1", "manifest"), None);
        assert_eq!(origin_of("blob-1/manifest", "blob-1", "manifest"), None);
        assert_eq!(origin_of("", "blob-1", "manifest"), None);
        // An id that is a suffix of a longer segment must not match: the
        // separator is required, or `xblob-1` would be read as `blob-1`.
        assert_eq!(origin_of("p/xblob-1/manifest", "blob-1", "manifest"), None);
    }
}