graphforge-cli 0.6.0-rc.4

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

use clap::Args;
use fs4::{FileExt, TryLockError};
use graphforge_api::telemetry::{
    ComponentHandoff, ComponentKind, ComponentRole, Failure, HandoffKind, JobFamily, JobSnapshot,
    JobStage, OtlpConfig, Outcome, Stage, TelemetryConfig, TelemetryMode, TelemetryRuntime,
    WaitReason,
};
use graphforge_api::{
    DiscoveryPortableV2Error, DiscoveryPortableV2Mismatch, DiscoveryPortableV2Request,
    DiscoveryResearchVersionError, DiscoveryResearchVersionRequest, PortableV2ErrorCode,
    verify_discovered_portable_v2, verify_discovered_research_version,
};
use graphforge_api::{
    GraphForge, OperationId, PortableV2ImportRequest, PortableV2Limits, PortableV2Mode,
};
use graphforge_discovery::{
    DiscoveryError, DiscoveryErrorCode, DiscoveryLimits, DiscoveryManifest, ObjectDescriptor,
    RefSet, RepositoryIdentity, ResearchLineage,
};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::fmt::Write as _;
use std::fs::{File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt as _;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
use url::Url;

#[cfg(test)]
use crate::hub_http::fetch;
use crate::hub_http::{
    HttpResponse, HttpTransport, MAX_METADATA_BYTES, RETRY_POLICY, RetryPolicy, Transport,
    endpoint, fetch_once, fetch_with_attempts, hash_reader, limit_error, network, parse_input,
    parse_input_at, read_bounded, storage, validate_url, validation,
};

mod module_fetch;
pub(crate) use module_fetch::{ModuleFetchArgs, run_module_fetch};

/// Largest single object a clone downloads: the cumulative object bound
/// `gf publish` admits ([`DiscoveryLimits::default`]), so any object a
/// publisher could produce fits. The discovery manifest keeps that cumulative
/// bound across all objects; clone downloads only one of them.
pub(crate) const MAX_OBJECT_BYTES: u64 = 1024 * 1024_u64.pow(4);

const CLONE_CONTRACT: &str = "graphforge-hub-clone/1";

/// Clone a verified portable project from GraphForge Hub.
#[derive(Args)]
pub(crate) struct CloneArgs {
    /// Canonical owner/repository name or an HTTPS Hub repository URL.
    pub repository: String,
    /// New project directory; defaults to the repository name.
    pub destination: Option<PathBuf>,
    /// Explicit local OTLP collector base URL. Clone telemetry is otherwise disabled.
    #[arg(long)]
    pub telemetry_endpoint: Option<String>,
    /// Branch ref naming a research head when the Hub advertises `lineage`.
    #[arg(long = "ref")]
    pub git_ref: Option<String>,
    /// Immutable research Version UUID to clone instead of the Project package.
    #[arg(long)]
    pub version_uuid: Option<String>,
}

#[derive(Clone, Default)]
enum CloneClock {
    #[default]
    Monotonic,
    #[cfg(test)]
    Manual(std::rc::Rc<std::cell::Cell<Instant>>),
}

impl CloneClock {
    fn now(&self) -> Instant {
        match self {
            Self::Monotonic => Instant::now(),
            #[cfg(test)]
            Self::Manual(clock) => clock.get(),
        }
    }

    fn elapsed_since(&self, started: Instant) -> Duration {
        match self {
            Self::Monotonic => started.elapsed(),
            #[cfg(test)]
            Self::Manual(clock) => clock.get().duration_since(started),
        }
    }

    fn wait(&self, duration: Duration) {
        match self {
            Self::Monotonic => std::thread::sleep(duration),
            #[cfg(test)]
            Self::Manual(clock) => clock.set(clock.get() + duration),
        }
    }

    #[cfg(test)]
    fn manual() -> Self {
        Self::Manual(std::rc::Rc::new(std::cell::Cell::new(Instant::now())))
    }
}

struct CloneProfile<'a> {
    runtime: &'a TelemetryRuntime,
    clock: CloneClock,
    started: Option<Instant>,
    cursor_ns: u64,
    stages: Vec<JobStage>,
    handoffs: Vec<ComponentHandoff>,
}

impl<'a> CloneProfile<'a> {
    fn new(runtime: &'a TelemetryRuntime) -> Self {
        Self {
            runtime,
            clock: CloneClock::default(),
            started: None,
            cursor_ns: 0,
            stages: Vec::new(),
            handoffs: Vec::new(),
        }
    }

    fn handoff(
        &mut self,
        from: ComponentKind,
        to: ComponentKind,
        kind: HandoffKind,
        bytes: Option<u64>,
    ) {
        self.handoffs.push(ComponentHandoff {
            start_offset_ns: self.cursor_ns,
            from,
            to,
            kind,
            duration_ns: 0,
            wait_duration_ns: 0,
            bytes,
            records: None,
        });
    }

    fn stage<T>(
        &mut self,
        stage: Stage,
        component: ComponentKind,
        role: ComponentRole,
        wait_reason: Option<WaitReason>,
        attempt: u32,
        operation: impl FnOnce() -> Result<(T, Option<u64>, Option<u64>), graphforge_api::GfError>,
    ) -> Result<T, graphforge_api::GfError> {
        let origin = *self.started.get_or_insert_with(|| self.clock.now());
        let operation_start_ns =
            u64::try_from(self.clock.elapsed_since(origin).as_nanos()).unwrap_or(u64::MAX);
        if !self.stages.is_empty() && operation_start_ns > self.cursor_ns {
            let duration_ns = operation_start_ns - self.cursor_ns;
            self.stages.push(JobStage {
                stage: Stage::Orchestration,
                component: ComponentKind::Cli,
                component_role: ComponentRole::Coordination,
                start_offset_ns: self.cursor_ns,
                duration_ns,
                wait_duration_ns: 0,
                wait_reason: None,
                attempt: 1,
                bytes: None,
                resumed_bytes: None,
                records: None,
                outcome: Outcome::Ok,
            });
            self.cursor_ns = operation_start_ns;
        }
        let started = self.clock.now();
        let result = operation();
        let duration_ns = u64::try_from(self.clock.elapsed_since(started).as_nanos())
            .unwrap_or(u64::MAX)
            .max(1);
        let (bytes, records) = result
            .as_ref()
            .map_or((None, None), |(_, bytes, records)| (*bytes, *records));
        self.stages.push(JobStage {
            stage,
            component,
            component_role: role,
            start_offset_ns: self.cursor_ns,
            duration_ns,
            wait_duration_ns: wait_reason.map_or(0, |_| duration_ns),
            wait_reason,
            attempt,
            bytes,
            resumed_bytes: None,
            records,
            outcome: if result.is_ok() {
                Outcome::Ok
            } else {
                Outcome::Failed
            },
        });
        self.cursor_ns = self.cursor_ns.saturating_add(duration_ns);
        result.map(|(value, _, _)| value)
    }

    fn finish(mut self, result: &Result<(), graphforge_api::GfError>) {
        let elapsed_ns = self.started.map_or(0, |started| {
            u64::try_from(self.clock.elapsed_since(started).as_nanos()).unwrap_or(u64::MAX)
        });
        if elapsed_ns > self.cursor_ns {
            let duration_ns = elapsed_ns - self.cursor_ns;
            self.stages.push(JobStage {
                stage: Stage::Orchestration,
                component: ComponentKind::Cli,
                component_role: ComponentRole::Coordination,
                start_offset_ns: self.cursor_ns,
                duration_ns,
                wait_duration_ns: 0,
                wait_reason: None,
                attempt: 1,
                bytes: None,
                resumed_bytes: None,
                records: None,
                outcome: if result.is_ok() {
                    Outcome::Ok
                } else {
                    Outcome::Failed
                },
            });
            self.cursor_ns = elapsed_ns;
        }
        let failure = result.as_ref().err().map(classify_failure);
        let _ = self.runtime.record_job(JobSnapshot {
            family: JobFamily::Clone,
            enqueued_ns: 0,
            started_ns: 0,
            finished_ns: self.cursor_ns,
            outcome: if result.is_ok() {
                Outcome::Ok
            } else {
                Outcome::Failed
            },
            failure,
            stages: self.stages,
            handoffs: self.handoffs,
        });
    }
}

fn classify_failure(error: &graphforge_api::GfError) -> Failure {
    let detail = match error {
        graphforge_api::GfError::Validation(detail) | graphforge_api::GfError::Storage(detail) => {
            detail.as_str()
        }
        _ => return Failure::Internal,
    };
    let code = detail.split_once(':').map_or(detail, |(code, _)| code);
    match code {
        "hub.network" | "hub.unsafe_location" => Failure::Network,
        "hub.limit_exceeded" | "hub.package.limit_exceeded" => Failure::ResourceLimit,
        "hub.invalid_identity"
        | "hub.malformed_response"
        | "hub.unsupported_future"
        | "hub.missing_ref"
        | "hub.missing_object"
        | "hub.duplicate"
        | "hub.destination_conflict"
        | "hub.concurrent_clone"
        | "hub.interrupted"
        | "hub.package.cancelled"
        | "hub.package.unsupported_future"
        | "hub.package.incompatible"
        | "hub.integrity"
        | "hub.integrity_failure"
        | "hub.package.repository_mismatch"
        | "hub.package.immutable_version_mismatch"
        | "hub.package.package_digest_mismatch"
        | "hub.package.research_version_mismatch"
        | "hub.module.identity_mismatch"
        | "hub.module.content_digest_mismatch"
        | "hub.package.invalid_participant"
        | "hub.package.invalid_structure"
        | "hub.package.invalid_path"
        | "hub.package.duplicate_entry"
        | "hub.package.digest_mismatch"
        | "hub.package.concurrent_mutation" => Failure::InvalidInput,
        "hub.package.io" => Failure::Storage,
        _ if matches!(error, graphforge_api::GfError::Storage(_)) => Failure::Storage,
        _ => Failure::Internal,
    }
}

#[derive(Serialize, Deserialize)]
struct CloneResult {
    contract: String,
    repository: String,
    destination: String,
    immutable_version: String,
    package_digest: String,
    generation_uuid: String,
    resumed_bytes: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    research_version_uuid: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    research_version_kind: Option<String>,
}

struct ResearchCloneContext {
    lineage_bytes: Vec<u8>,
    version_uuid: String,
    version_kind: String,
    identity_digest: String,
    package_digest: String,
}

fn select_bundle(
    manifest: &DiscoveryManifest,
) -> Result<&ObjectDescriptor, graphforge_api::GfError> {
    manifest
        .package_object()
        .map_err(|error| protocol_error(&error))
}

fn fetch_inventory_object(
    transport: &dyn Transport,
    object: &ObjectDescriptor,
    max_bytes: usize,
    retry: &RetryPolicy,
) -> Result<Vec<u8>, graphforge_api::GfError> {
    let location = object
        .locations
        .first()
        .ok_or_else(|| validation("hub.missing_object", "inventory object has no location"))?;
    let url = Url::parse(location)
        .map_err(|_| validation("hub.unsafe_location", "inventory location is invalid"))?;
    validate_url(&url)?;
    let mut attempts = 0;
    let response = fetch_with_attempts(
        transport,
        &url,
        None,
        None,
        max_bytes as u64,
        &mut attempts,
        retry,
    )?;
    let bytes = read_bounded(response, max_bytes)?;
    let actual = hash_reader(&mut std::io::Cursor::new(&bytes))?;
    if actual != object.digest.0 {
        return Err(validation(
            "hub.integrity_failure",
            "inventory object digest mismatch",
        ));
    }
    Ok(bytes)
}

fn resolve_research_version_uuid(
    lineage: &ResearchLineage,
    git_ref: Option<&str>,
    version_uuid: Option<&str>,
) -> Result<String, graphforge_api::GfError> {
    if let Some(uuid) = version_uuid {
        if lineage.version(uuid).is_none() {
            return Err(validation(
                "hub.missing_object",
                "research Version is absent from lineage",
            ));
        }
        return Ok(uuid.to_owned());
    }
    let git_ref = git_ref.ok_or_else(|| {
        validation(
            "hub.missing_ref",
            "branch ref or version UUID is required for research clone",
        )
    })?;
    let branch = lineage
        .branches
        .iter()
        .find(|branch| branch.ref_name == git_ref)
        .ok_or_else(|| {
            validation(
                "hub.missing_ref",
                "branch ref is absent from research lineage",
            )
        })?;
    Ok(branch.head_version_uuid.clone())
}

fn prepare_research_clone(
    transport: &dyn Transport,
    manifest: &DiscoveryManifest,
    refs: &RefSet,
    limits: DiscoveryLimits,
    git_ref: Option<&str>,
    version_uuid: Option<&str>,
    retry: &RetryPolicy,
) -> Result<(ObjectDescriptor, ResearchCloneContext), graphforge_api::GfError> {
    let lineage_object = manifest
        .lineage_object()
        .map_err(|error| protocol_error(&error))?;
    let lineage_bytes =
        fetch_inventory_object(transport, lineage_object, limits.max_lineage_bytes, retry)?;
    let lineage = ResearchLineage::from_json(&lineage_bytes, limits)
        .map_err(|error| protocol_error(&error))?;
    manifest
        .bind_lineage(refs, &lineage)
        .map_err(|error| protocol_error(&error))?;
    let selected_uuid = resolve_research_version_uuid(&lineage, git_ref, version_uuid)?;
    let (version, object) = manifest
        .research_version_object(&lineage, &selected_uuid)
        .map_err(|error| protocol_error(&error))?;
    Ok((
        object.clone(),
        ResearchCloneContext {
            lineage_bytes,
            version_uuid: selected_uuid,
            version_kind: version.kind.clone(),
            identity_digest: version.identity_digest.0.clone(),
            package_digest: version
                .package
                .as_ref()
                .map(|package| package.package_digest.0.clone())
                .ok_or_else(|| {
                    validation(
                        "hub.missing_object",
                        "research Version package is not advertised",
                    )
                })?,
        },
    ))
}

fn staging_path(destination: &Path) -> Result<PathBuf, graphforge_api::GfError> {
    let parent = destination.parent().unwrap_or_else(|| Path::new("."));
    let name = destination
        .file_name()
        .and_then(|v| v.to_str())
        .ok_or_else(|| {
            validation(
                "hub.destination_conflict",
                "destination must have a UTF-8 name",
            )
        })?;
    Ok(parent.join(format!(".{name}.graphforge-clone")))
}

/// Owner-private staging beside the destination, held by an exclusive lock:
/// the download, its resume checkpoint, the import target `project/` with
/// the import's own residue, and the install record.
#[derive(Debug)]
pub(crate) struct CloneStaging {
    pub(crate) root: PathBuf,
    pub(crate) partial: PathBuf,
    project: PathBuf,
    operation: PathBuf,
    installed: PathBuf,
    lock: PathBuf,
    _lock: File,
}

impl CloneStaging {
    fn new(root: PathBuf, lock: File) -> Self {
        Self {
            partial: root.join("package.part"),
            project: root.join("project"),
            operation: root.join("operation"),
            installed: root.join("installed.json"),
            lock: root.join("clone.lock"),
            root,
            _lock: lock,
        }
    }

    fn lock_name(&self) -> std::ffi::OsString {
        file_name(&self.lock)
    }
}

#[cfg(unix)]
pub(crate) fn acquire_staging(destination: &Path) -> Result<CloneStaging, graphforge_api::GfError> {
    use std::os::unix::fs::{DirBuilderExt as _, PermissionsExt as _};
    let root = staging_path(destination)?;
    match std::fs::symlink_metadata(&root) {
        Ok(m) if m.file_type().is_symlink() || !m.is_dir() => {
            return Err(validation(
                "hub.destination_conflict",
                "staging path is unsafe",
            ));
        }
        Ok(_) => std::fs::set_permissions(&root, std::fs::Permissions::from_mode(0o700))
            .map_err(storage)?,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            let mut b = std::fs::DirBuilder::new();
            b.mode(0o700);
            b.create(&root).map_err(storage)?;
        }
        Err(e) => return Err(storage(e)),
    }
    let lock_path = root.join("clone.lock");
    if std::fs::symlink_metadata(&lock_path).is_ok_and(|m| m.file_type().is_symlink()) {
        return Err(validation(
            "hub.destination_conflict",
            "staging lock is unsafe",
        ));
    }
    let lock = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .mode(0o600)
        .custom_flags(libc::O_NOFOLLOW)
        .open(lock_path)
        .map_err(storage)?;
    match FileExt::try_lock(&lock) {
        Ok(()) => {}
        Err(TryLockError::WouldBlock) => {
            return Err(validation(
                "hub.concurrent_clone",
                "clone already in progress",
            ));
        }
        Err(TryLockError::Error(e)) => return Err(storage(e)),
    }
    Ok(CloneStaging::new(root, lock))
}

#[cfg(not(unix))]
pub(crate) fn acquire_staging(destination: &Path) -> Result<CloneStaging, graphforge_api::GfError> {
    let root = staging_path(destination)?;
    match std::fs::symlink_metadata(&root) {
        Ok(m) if !m.is_dir() => {
            return Err(validation(
                "hub.destination_conflict",
                "staging path is unsafe",
            ));
        }
        Ok(_) => {}
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            std::fs::create_dir(&root).map_err(storage)?
        }
        Err(e) => return Err(storage(e)),
    }
    let lock = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(root.join("clone.lock"))
        .map_err(storage)?;
    match FileExt::try_lock(&lock) {
        Ok(()) => {}
        Err(TryLockError::WouldBlock) => {
            return Err(validation(
                "hub.concurrent_clone",
                "clone already in progress",
            ));
        }
        Err(TryLockError::Error(e)) => return Err(storage(e)),
    }
    Ok(CloneStaging::new(root, lock))
}

#[derive(Serialize, Deserialize)]
struct ResumeState {
    digest: String,
    length: u64,
    location: String,
    etag: String,
}

fn save_resume(
    checkpoint: &Path,
    object: &ObjectDescriptor,
    location: &str,
    response: &HttpResponse,
) -> Result<(), graphforge_api::GfError> {
    let etag = response
        .etag
        .as_deref()
        .filter(|v| v.starts_with('"') && !v.starts_with("W/"))
        .ok_or_else(|| validation("hub.integrity", "object response requires a strong ETag"))?;
    let state = ResumeState {
        digest: object.digest.0.clone(),
        length: object.length,
        location: location.to_owned(),
        etag: etag.to_owned(),
    };
    reject_unsafe_state_path(checkpoint)?;
    let temporary = checkpoint.with_extension("resume.json.tmp");
    reject_unsafe_state_path(&temporary)?;
    let bytes = serde_json::to_vec(&state).map_err(storage)?;
    let mut file = open_private_checkpoint(&temporary)?;
    file.write_all(&bytes).map_err(storage)?;
    file.sync_all().map_err(storage)?;
    drop(file);
    std::fs::rename(&temporary, checkpoint).map_err(storage)?;
    if let Some(parent) = checkpoint.parent() {
        let _ = File::open(parent).and_then(|directory| directory.sync_all());
    }
    Ok(())
}

fn reject_unsafe_state_path(path: &Path) -> Result<(), graphforge_api::GfError> {
    match std::fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_file() => {
            Err(validation(
                "hub.destination_conflict",
                "resume checkpoint path is unsafe",
            ))
        }
        Ok(_) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(storage(error)),
    }
}

#[cfg(unix)]
fn open_private_checkpoint(path: &Path) -> Result<File, graphforge_api::GfError> {
    OpenOptions::new()
        .write(true)
        .create(true)
        .truncate(true)
        .mode(0o600)
        .custom_flags(libc::O_NOFOLLOW)
        .open(path)
        .map_err(storage)
}

#[cfg(not(unix))]
fn open_private_checkpoint(path: &Path) -> Result<File, graphforge_api::GfError> {
    reject_unsafe_state_path(path)?;
    OpenOptions::new()
        .write(true)
        .create(true)
        .truncate(true)
        .open(path)
        .map_err(storage)
}

fn read_resume(checkpoint: &Path) -> Result<Option<ResumeState>, graphforge_api::GfError> {
    match std::fs::symlink_metadata(checkpoint) {
        Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_file() => {
            Err(validation(
                "hub.destination_conflict",
                "resume checkpoint path is unsafe",
            ))
        }
        Ok(_) => {
            let file = open_read_nofollow(checkpoint).map_err(storage)?;
            let mut bytes = Vec::new();
            file.take(64 * 1024)
                .read_to_end(&mut bytes)
                .map_err(storage)?;
            Ok(serde_json::from_slice(&bytes).ok())
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(storage(error)),
    }
}

#[cfg(test)]
fn download(
    transport: &dyn Transport,
    object: &ObjectDescriptor,
    partial: &Path,
) -> Result<DownloadReport, graphforge_api::GfError> {
    download_with_progress(
        transport,
        object,
        partial,
        &mut DownloadReport::default(),
        &mut DownloadControl::quiet(&TEST_RETRY_POLICY),
    )
}

/// Retry policy for in-process tests: production attempt count, no waiting.
#[cfg(test)]
const TEST_RETRY_POLICY: RetryPolicy = RetryPolicy {
    attempts: RETRY_POLICY.attempts,
    initial_backoff: Duration::ZERO,
    max_backoff: Duration::ZERO,
};

/// Caller-owned retry bound, cancellation, and byte progress for a download.
pub(crate) struct DownloadControl<'a> {
    pub(crate) retry: &'a RetryPolicy,
    pub(crate) cancelled: &'a AtomicBool,
    /// Called with the durable byte count and the object length.
    pub(crate) progress: Option<&'a mut dyn FnMut(u64, u64)>,
}

static NEVER_CANCELLED: AtomicBool = AtomicBool::new(false);

impl<'a> DownloadControl<'a> {
    /// No cancellation and no progress reporting.
    pub(crate) fn quiet(retry: &'a RetryPolicy) -> Self {
        Self {
            retry,
            cancelled: &NEVER_CANCELLED,
            progress: None,
        }
    }

    fn report(&mut self, bytes: u64, length: u64) {
        if let Some(progress) = self.progress.as_mut() {
            progress(bytes, length);
        }
    }
}

fn cancelled_error() -> graphforge_api::GfError {
    validation("hub.interrupted", "clone was cancelled; rerun to resume")
}

/// Download `object` into `partial`, resuming a previous invocation's bytes
/// and retrying transient failures in-process.
///
/// Each request after the first asks for the remaining bytes with `Range` and
/// the strong ETag the bytes on disk came from as `If-Range`. Only an exact
/// matching `206` appends; a `200` is the whole object again and replaces the
/// partial file, read to the object length. The complete file must match the
/// declared length and digest.
#[allow(clippy::too_many_lines)]
fn download_with_progress(
    transport: &dyn Transport,
    object: &ObjectDescriptor,
    partial: &Path,
    report: &mut DownloadReport,
    control: &mut DownloadControl<'_>,
) -> Result<DownloadReport, graphforge_api::GfError> {
    let checkpoint = partial.with_extension("resume.json");
    if object.length > MAX_OBJECT_BYTES {
        return Err(limit_error("object exceeds the clone byte bound"));
    }
    let mut offset = match std::fs::symlink_metadata(partial) {
        Ok(metadata) if metadata.is_file() && !metadata.file_type().is_symlink() => metadata.len(),
        Ok(_) => {
            return Err(validation(
                "hub.destination_conflict",
                "resume path is not a regular file",
            ));
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => 0,
        Err(error) => return Err(storage(error)),
    };
    if offset > object.length {
        open_partial_nofollow(partial, false).map_err(storage)?;
        offset = 0;
    }
    let location = object
        .locations
        .first()
        .ok_or_else(|| validation("hub.missing_object", "portable bundle has no location"))?;
    let url = Url::parse(location)
        .map_err(|_| validation("hub.unsafe_location", "invalid object URL"))?;
    let saved = read_resume(&checkpoint)?;
    let mut validator = saved
        .filter(|s| {
            s.digest == object.digest.0 && s.length == object.length && s.location == *location
        })
        .map(|s| s.etag);
    if offset > 0 && validator.is_none() {
        offset = 0;
    }
    report.resumed_bytes = offset;
    let mut first_request = true;
    let mut failures = 0_u32;
    let mut high_water = offset;
    let mut buffer = vec![0_u8; 64 * 1024].into_boxed_slice();
    control.report(offset, object.length);
    while offset < object.length {
        if control.cancelled.load(Ordering::Relaxed) {
            return Err(cancelled_error());
        }
        let range = (offset > 0).then_some(offset);
        // The server may ignore `Range` and send the whole object, so the
        // transport bound is always the object length; the expected size is
        // applied once the status is known.
        let failure = match fetch_once(
            transport,
            &url,
            range,
            range.and(validator.as_deref()),
            object.length,
            &mut report.attempts,
        ) {
            Err(failure) if failure.transient => failure.error,
            // The partial bytes no longer match the object the server holds
            // under this location: discard them and restart from zero rather
            // than repeat the same unsatisfiable range on every rerun.
            Err(failure) if range.is_some() && failure.status == Some(416) => {
                discard_partial(partial, &checkpoint)?;
                offset = 0;
                validator = None;
                if first_request {
                    report.resumed_bytes = 0;
                }
                failure.error
            }
            Err(failure) => return Err(failure.error),
            Ok(mut response) => {
                let append = if range.is_some() && response.status == 206 {
                    let expected_range =
                        format!("bytes {offset}-{}/{}", object.length - 1, object.length);
                    if response.content_range.as_deref() != Some(expected_range.as_str())
                        || response
                            .etag
                            .as_deref()
                            .is_some_and(|etag| Some(etag) != validator.as_deref())
                    {
                        let _ = std::fs::remove_file(partial);
                        let _ = std::fs::remove_file(&checkpoint);
                        return Err(validation(
                            "hub.integrity",
                            "range response does not match the requested object",
                        ));
                    }
                    true
                } else if response.status == 206 {
                    return Err(validation(
                        "hub.integrity",
                        "unrequested range response for the object",
                    ));
                } else {
                    false
                };
                if !append {
                    save_resume(&checkpoint, object, location, &response)?;
                    validator.clone_from(&response.etag);
                    offset = 0;
                    if first_request {
                        report.resumed_bytes = 0;
                    }
                }
                first_request = false;
                let mut file = open_partial_nofollow(partial, append).map_err(storage)?;
                let mut interrupted = None;
                loop {
                    if control.cancelled.load(Ordering::Relaxed) {
                        file.sync_all().map_err(storage)?;
                        return Err(cancelled_error());
                    }
                    let Ok(read) = response.body.read(&mut buffer) else {
                        interrupted = Some(network("object read failed"));
                        break;
                    };
                    if read == 0 {
                        break;
                    }
                    let next = offset
                        .checked_add(read as u64)
                        .filter(|next| *next <= object.length)
                        .ok_or_else(|| limit_error("object exceeds declared size"))?;
                    file.write_all(&buffer[..read]).map_err(storage)?;
                    offset = next;
                    report.transferred_bytes = report.transferred_bytes.saturating_add(read as u64);
                    control.report(offset, object.length);
                }
                file.sync_all().map_err(storage)?;
                match interrupted {
                    Some(error) => error,
                    None if offset < object.length => {
                        validation("hub.interrupted", "download is incomplete; rerun to resume")
                    }
                    None => break,
                }
            }
        };
        if offset > high_water {
            high_water = offset;
            failures = 0;
        }
        failures += 1;
        if failures >= control.retry.attempts {
            return Err(if offset > 0 {
                validation("hub.interrupted", "download is incomplete; rerun to resume")
            } else {
                failure
            });
        }
        std::thread::sleep(control.retry.backoff(failures));
    }
    let mut file = open_read_nofollow(partial).map_err(storage)?;
    let length = file.metadata().map_err(storage)?.len();
    if length != object.length {
        return Err(validation(
            "hub.interrupted",
            "download is incomplete; rerun to resume",
        ));
    }
    file.seek(SeekFrom::Start(0)).map_err(storage)?;
    let actual = hash_reader(&mut file)?;
    if actual != object.digest.0 {
        let _ = std::fs::remove_file(partial);
        let _ = std::fs::remove_file(&checkpoint);
        return Err(validation("hub.integrity", "download digest mismatch"));
    }
    Ok(*report)
}

fn discard_partial(partial: &Path, checkpoint: &Path) -> Result<(), graphforge_api::GfError> {
    for path in [partial, checkpoint] {
        match std::fs::remove_file(path) {
            Ok(()) => {}
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => return Err(storage(error)),
        }
    }
    Ok(())
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
struct DownloadReport {
    resumed_bytes: u64,
    transferred_bytes: u64,
    attempts: u32,
}

#[cfg(unix)]
fn open_partial_nofollow(path: &Path, append: bool) -> std::io::Result<File> {
    use std::os::unix::fs::OpenOptionsExt as _;

    OpenOptions::new()
        .create(true)
        .write(true)
        .append(append)
        .truncate(!append)
        .custom_flags(libc::O_NOFOLLOW)
        .open(path)
}

#[cfg(unix)]
fn open_read_nofollow(path: &Path) -> std::io::Result<File> {
    use std::os::unix::fs::OpenOptionsExt as _;

    OpenOptions::new()
        .read(true)
        .custom_flags(libc::O_NOFOLLOW)
        .open(path)
}

#[cfg(not(unix))]
fn open_read_nofollow(path: &Path) -> std::io::Result<File> {
    let file = File::open(path)?;
    if !file.metadata()?.is_file() {
        return Err(std::io::Error::other("resume path is not a regular file"));
    }
    Ok(file)
}

fn ensure_destination_absent(destination: &Path) -> Result<(), graphforge_api::GfError> {
    match std::fs::symlink_metadata(destination) {
        Ok(_) => Err(validation(
            "hub.destination_conflict",
            "destination already exists",
        )),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(storage(error)),
    }
}

#[cfg(not(unix))]
fn open_partial_nofollow(path: &Path, append: bool) -> std::io::Result<File> {
    let file = OpenOptions::new()
        .create(true)
        .write(true)
        .append(append)
        .truncate(!append)
        .open(path)?;
    if !file.metadata()?.is_file() {
        return Err(std::io::Error::other("resume path is not a regular file"));
    }
    Ok(file)
}

pub(crate) fn run_clone(
    args: CloneArgs,
    json: bool,
    output: &mut dyn Write,
) -> Result<(), graphforge_api::GfError> {
    let runtime = clone_telemetry_runtime(args.telemetry_endpoint.as_deref());
    let attached = PROCESS_ATTACHED.load(Ordering::Relaxed);
    if attached {
        install_interrupt_handler();
    }
    let mut stderr = std::io::stderr();
    let mut env = CloneEnv {
        retry: RETRY_POLICY,
        cancelled: if attached {
            &INTERRUPTED
        } else {
            &NEVER_CANCELLED
        },
        available_space,
        progress: CloneProgress::new(attached.then_some(&mut stderr as &mut dyn Write)),
    };
    let result = run_clone_profiled_with_delays(
        &HttpTransport::new(),
        args,
        json,
        output,
        &runtime,
        &CloneDelays::default(),
        &mut env,
    );
    let _ = runtime.shutdown();
    result
}

/// Set by the native `gf` process only: an embedding host keeps its own
/// signal handling and standard error.
static PROCESS_ATTACHED: AtomicBool = AtomicBool::new(false);
/// Set by the first Ctrl-C during a clone.
static INTERRUPTED: AtomicBool = AtomicBool::new(false);

/// Let `gf clone` handle Ctrl-C and print byte progress on standard error.
/// Only the native `gf` process calls this.
pub(crate) fn attach_to_process() {
    PROCESS_ATTACHED.store(true, Ordering::Relaxed);
}

/// The first Ctrl-C asks the running download, verification, or import to
/// stop at its next cancellation check, which leaves a state a rerun resumes.
/// A second Ctrl-C exits at once; that is also resumable.
fn install_interrupt_handler() {
    static INSTALL: std::sync::Once = std::sync::Once::new();
    INSTALL.call_once(|| {
        // A host that already owns SIGINT keeps it; the clone then simply
        // cannot be cancelled cooperatively.
        let _ = ctrlc::set_handler(|| {
            if INTERRUPTED.swap(true, Ordering::SeqCst) {
                std::process::exit(130);
            }
            let _ = writeln!(
                std::io::stderr(),
                "gf clone: cancelling; a rerun resumes. Press Ctrl-C again to stop now."
            );
        });
    });
}

fn available_space(path: &Path) -> std::io::Result<u64> {
    fs4::available_space(path)
}

/// Process-level effects of one clone: retry bound, cancellation, the free
/// space probe, and progress output. Tests inject each of them.
struct CloneEnv<'a> {
    retry: RetryPolicy,
    cancelled: &'a AtomicBool,
    available_space: fn(&Path) -> std::io::Result<u64>,
    progress: CloneProgress<'a>,
}

#[cfg(test)]
impl CloneEnv<'_> {
    fn quiet<'a>() -> CloneEnv<'a> {
        CloneEnv {
            retry: TEST_RETRY_POLICY,
            cancelled: &NEVER_CANCELLED,
            available_space,
            progress: CloneProgress::new(None),
        }
    }
}

/// At most one byte-progress line per interval, plus the final one.
const PROGRESS_INTERVAL: Duration = Duration::from_secs(1);

/// Human progress lines; writing them is best effort and never fails a clone.
struct CloneProgress<'a> {
    sink: Option<&'a mut dyn Write>,
    last: Option<Instant>,
}

impl<'a> CloneProgress<'a> {
    fn new(sink: Option<&'a mut dyn Write>) -> Self {
        Self { sink, last: None }
    }

    fn phase(&mut self, message: &str) {
        if let Some(sink) = self.sink.as_mut() {
            let _ = writeln!(sink, "gf clone: {message}");
        }
    }

    fn bytes(&mut self, done: u64, total: u64) {
        let Some(sink) = self.sink.as_mut() else {
            return;
        };
        let now = Instant::now();
        if done < total
            && self
                .last
                .is_some_and(|last| now.duration_since(last) < PROGRESS_INTERVAL)
        {
            return;
        }
        self.last = Some(now);
        let percent = if total == 0 {
            100
        } else {
            u128::from(done) * 100 / u128::from(total)
        };
        let _ = writeln!(
            sink,
            "gf clone: downloaded {} of {} ({percent}%)",
            human_bytes(done),
            human_bytes(total)
        );
    }
}

fn human_bytes(bytes: u64) -> String {
    const UNITS: [&str; 5] = ["B", "KiB", "MiB", "GiB", "TiB"];
    let mut unit = 0;
    let mut scale = 1_u64;
    while unit + 1 < UNITS.len() && bytes / scale >= 1024 {
        scale *= 1024;
        unit += 1;
    }
    if unit == 0 {
        return format!("{bytes} B");
    }
    // One decimal place, truncated, in exact integer arithmetic.
    let tenths = u128::from(bytes) * 10 / u128::from(scale);
    format!("{}.{} {}", tenths / 10, tenths % 10, UNITS[unit])
}

/// Free space a clone still needs beside the destination: the rest of the
/// download plus whatever part of the import's transient peak is not already
/// staged. The import was measured at 2.20-2.22x the package
/// (`observed_transient_peak_allocated_bytes`); 2.25x keeps a margin.
///
/// Bytes an earlier run staged for the import (its target, its private
/// stage) count toward that peak: the import either reuses them (a resumed or
/// replayed import) or removes them before writing anew (another operation,
/// a retry from scratch), so they are space the import already holds.
///
/// An import that already committed only replays: it materializes the package
/// once more (at most its length) and rewrites nothing in the project.
fn required_space(length: u64, downloaded: u64, staged_import: u64, committed: bool) -> u64 {
    let import_peak = if committed {
        u128::from(length)
    } else {
        (u128::from(length) * 9).div_ceil(4)
    };
    let import = import_peak.saturating_sub(u128::from(staged_import));
    u64::try_from(u128::from(length.saturating_sub(downloaded)) + import).unwrap_or(u64::MAX)
}

fn check_free_space(
    staging: &CloneStaging,
    length: u64,
    committed: bool,
    available: fn(&Path) -> std::io::Result<u64>,
) -> Result<(), graphforge_api::GfError> {
    let downloaded = std::fs::symlink_metadata(&staging.partial)
        .ok()
        .filter(std::fs::Metadata::is_file)
        .map_or(0, |metadata| metadata.len().min(length));
    let required = required_space(length, downloaded, staged_import_bytes(staging), committed);
    let available = available(&staging.root).map_err(storage)?;
    if available < required {
        return Err(graphforge_api::GfError::Storage(format!(
            "hub.insufficient_space: the destination filesystem has {} available; \
             this clone needs about {}",
            human_bytes(available),
            human_bytes(required)
        )));
    }
    Ok(())
}

/// Bytes in regular files under the staging directory other than the download,
/// its checkpoint, and the small control files; each hard-linked file once.
fn staged_import_bytes(staging: &CloneStaging) -> u64 {
    fn visit(directory: &Path, seen: &mut std::collections::BTreeSet<(u64, u64)>, total: &mut u64) {
        let Ok(entries) = std::fs::read_dir(directory) else {
            return;
        };
        for entry in entries.flatten() {
            let Ok(metadata) = std::fs::symlink_metadata(entry.path()) else {
                continue;
            };
            if metadata.is_dir() {
                visit(&entry.path(), seen, total);
            } else if metadata.is_file() {
                #[cfg(unix)]
                {
                    use std::os::unix::fs::MetadataExt as _;
                    if !seen.insert((metadata.dev(), metadata.ino())) {
                        continue;
                    }
                }
                *total = total.saturating_add(metadata.len());
            }
        }
    }
    let skip = [
        staging.partial.clone(),
        staging.partial.with_extension("resume.json"),
    ];
    let mut seen = std::collections::BTreeSet::new();
    let mut total = 0_u64;
    let Ok(entries) = std::fs::read_dir(&staging.root) else {
        return 0;
    };
    for entry in entries.flatten() {
        if skip.contains(&entry.path()) {
            continue;
        }
        let Ok(metadata) = std::fs::symlink_metadata(entry.path()) else {
            continue;
        };
        if metadata.is_dir() {
            visit(&entry.path(), &mut seen, &mut total);
        } else if metadata.is_file() {
            total = total.saturating_add(metadata.len());
        }
    }
    total
}

/// The import operation of one clone: a Project-package clone keeps its
/// historical derivation; a research clone binds the selected Version too, so
/// different Versions of one snapshot get distinct import operations and
/// therefore distinct generation identities.
fn clone_operation(
    identity: &RepositoryIdentity,
    immutable_version: &str,
    research: Option<&ResearchCloneContext>,
) -> OperationId {
    OperationId(match research {
        None => graphforge_api::hub_clone_operation(&canonical_name(identity), immutable_version),
        Some(context) => graphforge_api::hub_research_clone_operation(
            &canonical_name(identity),
            immutable_version,
            &context.version_uuid,
            &context.identity_digest,
        ),
    })
}

/// Whether an earlier run already committed `operation` in the staged import
/// target, so the import only replays.
fn import_committed(staging: &CloneStaging, operation: &OperationId) -> bool {
    if std::fs::symlink_metadata(&staging.project).is_err() {
        return false;
    }
    let expected = graphforge_api::portable_v2_import_generation(&operation.0);
    staging
        .project
        .to_str()
        .and_then(|path| GraphForge::new(Some(path)).ok())
        .and_then(|graph| graph.committed_generation_identity().ok())
        .is_some_and(|identity| identity.generation_uuid == expected)
}

/// Remove research validation scratch an interrupted run left in the staging
/// directory; it never outlives the run that created it.
fn remove_stale_scratch(staging: &CloneStaging) -> Result<(), graphforge_api::GfError> {
    for entry in std::fs::read_dir(&staging.root).map_err(storage)? {
        let entry = entry.map_err(storage)?;
        if entry
            .file_name()
            .to_string_lossy()
            .starts_with(RESEARCH_SCRATCH_PREFIX)
            && entry.file_type().map_err(storage)?.is_dir()
        {
            std::fs::remove_dir_all(entry.path()).map_err(storage)?;
        }
    }
    Ok(())
}

/// Prefix of the private directories research validation creates beside the
/// package it validates.
const RESEARCH_SCRATCH_PREFIX: &str = ".graphforge-research-validation-";

fn clone_telemetry_runtime(endpoint: Option<&str>) -> TelemetryRuntime {
    let Some(endpoint) = endpoint else {
        return TelemetryRuntime::default();
    };
    TelemetryRuntime::new(TelemetryConfig {
        mode: TelemetryMode::OtlpHttpJson,
        otlp: Some(OtlpConfig {
            endpoint: endpoint.to_owned(),
            headers: BTreeMap::default(),
        }),
        ..TelemetryConfig::default()
    })
    .unwrap_or_default()
}

#[cfg(test)]
pub(crate) fn run_clone_with(
    transport: &dyn Transport,
    args: CloneArgs,
    json: bool,
    output: &mut dyn Write,
) -> Result<(), graphforge_api::GfError> {
    run_clone_profiled(transport, args, json, output, &TelemetryRuntime::default())
}

#[cfg(test)]
fn run_clone_profiled(
    transport: &dyn Transport,
    args: CloneArgs,
    json: bool,
    output: &mut dyn Write,
    runtime: &TelemetryRuntime,
) -> Result<(), graphforge_api::GfError> {
    run_clone_profiled_with_delays(
        transport,
        args,
        json,
        output,
        runtime,
        &CloneDelays::default(),
        &mut CloneEnv::quiet(),
    )
}

#[derive(Default)]
struct CloneDelays {
    clock: CloneClock,
    verification: Duration,
    import: Duration,
    reopen: Duration,
    /// Runs immediately before the import, after verification.
    #[cfg(test)]
    before_import: Option<Box<dyn Fn()>>,
}

fn run_clone_profiled_with_delays(
    transport: &dyn Transport,
    args: CloneArgs,
    json: bool,
    output: &mut dyn Write,
    runtime: &TelemetryRuntime,
    delays: &CloneDelays,
    env: &mut CloneEnv<'_>,
) -> Result<(), graphforge_api::GfError> {
    let mut profile = CloneProfile::new(runtime);
    #[cfg(test)]
    {
        profile.clock = delays.clock.clone();
    }
    let result = run_clone_job(transport, args, &mut profile, delays, env)
        .and_then(|result| write_clone_result(&result, json, output));
    profile.finish(&result);
    result
}

/// Test-only process exit at a named clone phase, for kill-and-rerun tests.
#[cfg(test)]
fn clone_failpoint(name: &str) {
    if std::env::var("GRAPHFORGE_CLONE_FAILPOINT").as_deref() == Ok(name) {
        std::process::exit(CLONE_FAILPOINT_EXIT);
    }
}

#[cfg(test)]
const CLONE_FAILPOINT_EXIT: i32 = 86;

#[cfg(not(test))]
const fn clone_failpoint(_name: &str) {}

/// What one completed clone left in its staging directory before the
/// destination was installed, so a rerun after a crash can finish it.
#[derive(Serialize, Deserialize)]
struct InstallRecord {
    generation_uuid: String,
    result: CloneResult,
}

/// Resolve the staging of a destination that already exists.
///
/// A clone whose destination was installed but whose staging was not yet
/// removed is finished: the rerun reports the recorded result and removes the
/// staging. Any other staging for an existing destination can never be
/// installed there, because installation never replaces a path, so it is
/// removed and the caller reports `hub.destination_conflict`.
fn finish_installed_clone(
    destination: &Path,
) -> Result<Option<CloneResult>, graphforge_api::GfError> {
    let root = staging_path(destination)?;
    match std::fs::symlink_metadata(&root) {
        Ok(metadata) if metadata.is_dir() && !metadata.file_type().is_symlink() => {}
        _ => return Ok(None),
    }
    let staging = acquire_staging(destination)?;
    let record = if std::fs::symlink_metadata(&staging.project).is_ok() {
        None
    } else {
        read_install_record(&staging.installed)?
    };
    let finished = record.filter(|record| {
        destination
            .to_str()
            .and_then(|path| GraphForge::new(Some(path)).ok())
            .and_then(|graph| graph.committed_generation_identity().ok())
            .is_some_and(|identity| identity.generation_uuid.to_string() == record.generation_uuid)
    });
    release_staging(staging);
    Ok(finished.map(|record| record.result))
}

fn read_install_record(path: &Path) -> Result<Option<InstallRecord>, graphforge_api::GfError> {
    match std::fs::symlink_metadata(path) {
        Ok(metadata) if metadata.is_file() && !metadata.file_type().is_symlink() => {
            let mut bytes = Vec::new();
            open_read_nofollow(path)
                .map_err(storage)?
                .take(64 * 1024)
                .read_to_end(&mut bytes)
                .map_err(storage)?;
            Ok(serde_json::from_slice(&bytes).ok())
        }
        Ok(_) => Err(validation(
            "hub.destination_conflict",
            "install record path is unsafe",
        )),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(storage(error)),
    }
}

/// Write `bytes` to `path` in the staging directory and make it durable.
fn write_staging_file(path: &Path, bytes: &[u8]) -> Result<(), graphforge_api::GfError> {
    reject_unsafe_state_path(path)?;
    let temporary = path.with_extension("tmp");
    reject_unsafe_state_path(&temporary)?;
    let mut file = open_private_checkpoint(&temporary)?;
    file.write_all(bytes).map_err(storage)?;
    file.sync_all().map_err(storage)?;
    drop(file);
    std::fs::rename(&temporary, path).map_err(storage)?;
    if let Some(parent) = path.parent() {
        let _ = File::open(parent).and_then(|directory| directory.sync_all());
    }
    Ok(())
}

/// Bind the staged import target to `operation`. A target staged for another
/// operation (the Hub moved to a newer version since the previous run) is
/// removed with its import residue, so the import starts from an empty
/// target; the same operation resumes or replays in place.
fn bind_import_target(
    staging: &CloneStaging,
    operation: &OperationId,
) -> Result<(), graphforge_api::GfError> {
    let expected = operation.0.hyphenated().to_string();
    let recorded = match std::fs::symlink_metadata(&staging.operation) {
        Ok(metadata) if metadata.is_file() && !metadata.file_type().is_symlink() => {
            let mut bytes = Vec::new();
            open_read_nofollow(&staging.operation)
                .map_err(storage)?
                .take(128)
                .read_to_end(&mut bytes)
                .map_err(storage)?;
            Some(bytes)
        }
        Ok(_) => {
            return Err(validation(
                "hub.destination_conflict",
                "staged operation path is unsafe",
            ));
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
        Err(error) => return Err(storage(error)),
    };
    if recorded.as_deref() == Some(expected.as_bytes()) {
        return Ok(());
    }
    clear_import_target(staging)?;
    write_staging_file(&staging.operation, expected.as_bytes())
}

/// Remove the staged import target with the import's residue and the
/// operation binding; the download and its checkpoint stay.
fn clear_import_target(staging: &CloneStaging) -> Result<(), graphforge_api::GfError> {
    let keep = [
        staging.lock_name(),
        file_name(&staging.partial),
        file_name(&staging.partial.with_extension("resume.json")),
    ];
    for entry in std::fs::read_dir(&staging.root).map_err(storage)? {
        let entry = entry.map_err(storage)?;
        if keep.iter().any(|name| *name == entry.file_name()) {
            continue;
        }
        let path = entry.path();
        if entry.file_type().map_err(storage)?.is_dir() {
            std::fs::remove_dir_all(&path).map_err(storage)?;
        } else {
            std::fs::remove_file(&path).map_err(storage)?;
        }
    }
    Ok(())
}

fn file_name(path: &Path) -> std::ffi::OsString {
    path.file_name().map(ToOwned::to_owned).unwrap_or_default()
}

/// Atomically install the imported project at `destination`; an existing
/// destination is never replaced.
fn install_destination(
    staging: &CloneStaging,
    destination: &Path,
) -> Result<(), graphforge_api::GfError> {
    match graphforge_filesystem::rename_no_replace(&staging.project, destination) {
        Ok(()) => {}
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
            return Err(validation(
                "hub.destination_conflict",
                "destination already exists",
            ));
        }
        Err(error) => return Err(storage(error)),
    }
    if let Some(parent) = destination
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
    {
        let _ = File::open(parent).and_then(|directory| directory.sync_all());
    }
    Ok(())
}

fn import_error(error: &graphforge_api::PortableV2Error) -> graphforge_api::GfError {
    let code = portable_code(error.code);
    let mut message = format!("{code}: portable project import failed: {error}");
    if let Some(cause) = &error.cause {
        // Sanitized: host paths are reduced to their last two components.
        let _ = write!(message, " (cause: {cause})");
    }
    match error.code {
        PortableV2ErrorCode::Io | PortableV2ErrorCode::Cancelled => {
            graphforge_api::GfError::Storage(message)
        }
        _ => graphforge_api::GfError::Validation(message),
    }
}

fn portable_code(code: PortableV2ErrorCode) -> &'static str {
    match code {
        PortableV2ErrorCode::Cancelled => "hub.package.cancelled",
        PortableV2ErrorCode::LimitExceeded => "hub.package.limit_exceeded",
        PortableV2ErrorCode::Io => "hub.package.io",
        PortableV2ErrorCode::InvalidStructure => "hub.package.invalid_structure",
        PortableV2ErrorCode::InvalidPath => "hub.package.invalid_path",
        PortableV2ErrorCode::DuplicateEntry => "hub.package.duplicate_entry",
        PortableV2ErrorCode::UnsupportedFuture => "hub.package.unsupported_future",
        PortableV2ErrorCode::Incompatible => "hub.package.incompatible",
        PortableV2ErrorCode::DigestMismatch => "hub.package.digest_mismatch",
        PortableV2ErrorCode::ConcurrentMutation => "hub.package.concurrent_mutation",
    }
}

/// Remove a staging directory that holds nothing a rerun can reuse.
fn release_staging_if_unused(staging: CloneStaging) {
    let reusable = std::fs::read_dir(&staging.root).is_ok_and(|entries| {
        entries
            .flatten()
            .any(|entry| entry.file_name() != staging.lock_name())
    });
    if reusable {
        drop(staging);
    } else {
        release_staging(staging);
    }
}

/// Remove everything staged, on success and on failure alike.
///
/// The contents (partial file, checkpoint, staged output, and the lock file
/// itself) are removed while the lock is still held, so a concurrent run can
/// never acquire the directory and then have its files deleted. Only then is
/// the lock released and the now-empty directory removed with `remove_dir`,
/// which fails harmlessly if a concurrent run has meanwhile re-created its
/// lock; that run's staging is never touched.
pub(crate) fn release_staging(staging: CloneStaging) {
    clear_staging(&staging);
    let root = staging.root.clone();
    drop(staging);
    remove_staging_dir(&root);
}

/// `remove_dir`, not `remove_dir_all`: it refuses a directory that a concurrent
/// run has re-populated, and that refusal is expected and benign.
fn remove_staging_dir(root: &Path) {
    let _ = std::fs::remove_dir(root);
}

/// Best-effort removal of every entry in the staging directory. Cleanup can
/// neither turn a published success into a failure nor mask the real error.
fn clear_staging(staging: &CloneStaging) {
    let Ok(entries) = std::fs::read_dir(&staging.root) else {
        return;
    };
    let entries: Vec<_> = entries
        .flatten()
        .map(|entry| {
            let directory = entry.file_type().is_ok_and(|kind| kind.is_dir());
            (entry.path(), directory)
        })
        .collect();
    for (path, directory) in removal_order(staging, entries) {
        let _ = if directory {
            std::fs::remove_dir_all(&path)
        } else {
            std::fs::remove_file(&path)
        };
    }
}

/// Crash-safe removal order: the install record goes after everything it
/// describes, and the lock file last. A kill part-way through leaves either
/// the record (the rerun finishes the clone) or nothing a rerun needs.
fn removal_order(
    staging: &CloneStaging,
    mut entries: Vec<(PathBuf, bool)>,
) -> Vec<(PathBuf, bool)> {
    entries.sort_by_key(|(path, _)| (*path == staging.lock, *path == staging.installed));
    entries
}

#[allow(clippy::too_many_lines)]
fn run_clone_job(
    transport: &dyn Transport,
    mut args: CloneArgs,
    profile: &mut CloneProfile<'_>,
    delays: &CloneDelays,
    env: &mut CloneEnv<'_>,
) -> Result<CloneResult, graphforge_api::GfError> {
    if args.git_ref.is_some() && args.version_uuid.is_some() {
        return Err(validation(
            "hub.invalid_identity",
            "specify only one of --ref and --version-uuid",
        ));
    }
    let requested_destination = args.destination.take();
    let (identity, base, destination, finished) = profile.stage(
        Stage::IdentityValidation,
        ComponentKind::Cli,
        ComponentRole::Facade,
        None,
        1,
        || {
            let (identity, base) = parse_input(&args.repository)?;
            let destination =
                requested_destination.unwrap_or_else(|| PathBuf::from(&identity.repository));
            if std::fs::symlink_metadata(&destination).is_ok() {
                let finished = finish_installed_clone(&destination)?.ok_or_else(|| {
                    validation("hub.destination_conflict", "destination already exists")
                })?;
                return Ok(((identity, base, destination, Some(finished)), None, None));
            }
            ensure_destination_absent(&destination)?;
            // Refuse an inadmissible destination filesystem with its real
            // code before any network request or download.
            graphforge_api::filesystem_durability_preflight(&destination)?;
            Ok(((identity, base, destination, None), None, None))
        },
    )?;
    if let Some(finished) = finished {
        return Ok(finished);
    }
    profile.handoff(
        ComponentKind::Cli,
        ComponentKind::NetworkTransport,
        HandoffKind::Call,
        None,
    );
    let mut refs_attempts = 0;
    let refs_result = profile.stage(
        Stage::RefsDiscovery,
        ComponentKind::NetworkTransport,
        ComponentRole::Transfer,
        Some(WaitReason::Network),
        1,
        || {
            let bytes = read_bounded(
                fetch_with_attempts(
                    transport,
                    &endpoint(&base, "refs"),
                    None,
                    None,
                    MAX_METADATA_BYTES as u64,
                    &mut refs_attempts,
                    &env.retry,
                )?,
                MAX_METADATA_BYTES,
            )?;
            let length = bytes.len() as u64;
            Ok((bytes, Some(length), None))
        },
    );
    if let Some(stage) = profile.stages.last_mut() {
        stage.attempt = refs_attempts.max(1);
    }
    let refs_bytes = refs_result?;
    let mut manifest_attempts = 0;
    let manifest_result = profile.stage(
        Stage::ManifestDiscovery,
        ComponentKind::NetworkTransport,
        ComponentRole::Transfer,
        Some(WaitReason::Network),
        1,
        || {
            let bytes = read_bounded(
                fetch_with_attempts(
                    transport,
                    &endpoint(&base, "manifest"),
                    None,
                    None,
                    MAX_METADATA_BYTES as u64,
                    &mut manifest_attempts,
                    &env.retry,
                )?,
                MAX_METADATA_BYTES,
            )?;
            let length = bytes.len() as u64;
            Ok((bytes, Some(length), None))
        },
    );
    if let Some(stage) = profile.stages.last_mut() {
        stage.attempt = manifest_attempts.max(1);
    }
    let manifest_bytes = manifest_result?;
    let limits = DiscoveryLimits {
        max_response_bytes: MAX_METADATA_BYTES,
        ..DiscoveryLimits::default()
    };
    profile.handoff(
        ComponentKind::NetworkTransport,
        ComponentKind::Discovery,
        HandoffKind::Return,
        Some((refs_bytes.len() + manifest_bytes.len()) as u64),
    );
    let (manifest, staging) = profile.stage(
        Stage::ManifestDiscovery,
        ComponentKind::Discovery,
        ComponentRole::Verification,
        None,
        1,
        || {
            let refs =
                RefSet::from_json(&refs_bytes, limits).map_err(|error| protocol_error(&error))?;
            let manifest = DiscoveryManifest::from_json(&manifest_bytes, limits)
                .map_err(|error| protocol_error(&error))?;
            if refs.repository != identity || manifest.repository != identity {
                return Err(validation("hub.integrity", "discovery repository mismatch"));
            }
            refs.validate_manifest(&manifest)
                .map_err(|error| protocol_error(&error))?;
            let staging = acquire_staging(&destination)?;
            Ok(((manifest, staging), None, None))
        },
    )?;
    let discovered = Discovered {
        identity: &identity,
        destination: &destination,
        refs_bytes: &refs_bytes,
        manifest_bytes: &manifest_bytes,
        manifest: &manifest,
        limits,
    };
    let result = clone_into_staging(
        transport,
        &args,
        &discovered,
        &staging,
        profile,
        delays,
        env,
    );
    match result {
        Ok(result) => {
            // The destination is already atomically installed; cleanup cannot
            // turn success into a reported failure.
            profile.stage(
                Stage::Cleanup,
                ComponentKind::Storage,
                ComponentRole::Persistence,
                None,
                1,
                || {
                    release_staging(staging);
                    Ok(((), None, None))
                },
            )?;
            Ok(result)
        }
        Err(error) => {
            // A destination that appeared during the clone can never receive
            // this staging: installation never replaces a path.
            if std::fs::symlink_metadata(&destination).is_ok() {
                release_staging(staging);
            } else {
                release_staging_if_unused(staging);
            }
            Err(error)
        }
    }
}

/// Validated discovery state shared by the staged clone phases.
struct Discovered<'a> {
    identity: &'a RepositoryIdentity,
    destination: &'a Path,
    refs_bytes: &'a [u8],
    manifest_bytes: &'a [u8],
    manifest: &'a DiscoveryManifest,
    limits: DiscoveryLimits,
}

#[allow(clippy::too_many_lines)]
fn clone_into_staging(
    transport: &dyn Transport,
    args: &CloneArgs,
    discovered: &Discovered<'_>,
    staging: &CloneStaging,
    profile: &mut CloneProfile<'_>,
    delays: &CloneDelays,
    env: &mut CloneEnv<'_>,
) -> Result<CloneResult, graphforge_api::GfError> {
    let Discovered {
        identity,
        destination,
        refs_bytes,
        manifest_bytes,
        manifest,
        limits,
    } = *discovered;
    let refs = RefSet::from_json(refs_bytes, limits).map_err(|error| protocol_error(&error))?;
    let research_requested = args.git_ref.is_some() || args.version_uuid.is_some();
    let (object, research_context) = if research_requested {
        if manifest.lineage.is_none() {
            return Err(validation(
                "hub.missing_object",
                "research clone requires an advertised lineage document",
            ));
        }
        profile.handoff(
            ComponentKind::Discovery,
            ComponentKind::NetworkTransport,
            HandoffKind::Call,
            None,
        );
        let (object, context) = prepare_research_clone(
            transport,
            manifest,
            &refs,
            limits,
            args.git_ref.as_deref(),
            args.version_uuid.as_deref(),
            &env.retry,
        )?;
        profile.handoff(
            ComponentKind::NetworkTransport,
            ComponentKind::Discovery,
            HandoffKind::Return,
            Some(context.lineage_bytes.len() as u64),
        );
        (object, Some(context))
    } else {
        (select_bundle(manifest)?.clone(), None)
    };
    if object.length > MAX_OBJECT_BYTES {
        return Err(limit_error("object exceeds the clone byte bound"));
    }
    remove_stale_scratch(staging)?;
    // Verification later confirms the manifest's immutable version, so the
    // operation planned here is the one the import runs.
    let planned = clone_operation(
        identity,
        &manifest.immutable_version.0,
        research_context.as_ref(),
    );
    check_free_space(
        staging,
        object.length,
        import_committed(staging, &planned),
        env.available_space,
    )?;
    let partial = staging.partial.clone();
    profile.handoff(
        ComponentKind::Discovery,
        ComponentKind::NetworkTransport,
        HandoffKind::Call,
        None,
    );
    let mut download_progress = DownloadReport::default();
    let download_result = profile.stage(
        Stage::Download,
        ComponentKind::NetworkTransport,
        ComponentRole::Transfer,
        Some(WaitReason::Network),
        1,
        || {
            let CloneEnv {
                retry,
                cancelled,
                progress,
                ..
            } = env;
            let mut report_bytes = |done, total| progress.bytes(done, total);
            let report = download_with_progress(
                transport,
                &object,
                &partial,
                &mut download_progress,
                &mut DownloadControl {
                    retry,
                    cancelled,
                    progress: Some(&mut report_bytes),
                },
            )?;
            Ok((report, Some(report.transferred_bytes), None))
        },
    );
    if let Some(stage) = profile.stages.last_mut() {
        stage.attempt = download_progress.attempts.max(1);
        stage.bytes = Some(download_progress.transferred_bytes);
        stage.resumed_bytes = Some(download_progress.resumed_bytes);
    }
    let download = download_result?;
    clone_failpoint("clone.after_download");
    let portable_limits = PortableV2Limits::default();
    profile.handoff(
        ComponentKind::NetworkTransport,
        ComponentKind::PortableVerify,
        HandoffKind::Transfer,
        Some(download.resumed_bytes + download.transferred_bytes),
    );
    env.progress.phase("verifying the package");
    let cancelled = env.cancelled;
    let verified = profile.stage(
        Stage::PortableVerification,
        ComponentKind::PortableVerify,
        ComponentRole::Verification,
        None,
        1,
        || {
            delays.clock.wait(delays.verification);
            let outcome = if let Some(context) = &research_context {
                verify_discovered_research_version(&DiscoveryResearchVersionRequest {
                    manifest_json: manifest_bytes,
                    refs_json: refs_bytes,
                    lineage_json: &context.lineage_bytes,
                    expected_repository: identity,
                    version_uuid: &context.version_uuid,
                    package: &partial,
                    discovery_limits: limits,
                    portable_limits,
                    mode: PortableV2Mode::Full,
                    cancelled: Some(cancelled),
                    scratch: Some(&staging.root),
                })
                .map_err(research_version_error)?;
                (
                    manifest.immutable_version.0.clone(),
                    Some(context.version_uuid.clone()),
                    Some(context.version_kind.clone()),
                )
            } else {
                let accepted = verify_discovered_portable_v2(&DiscoveryPortableV2Request {
                    manifest_json: manifest_bytes,
                    refs_json: refs_bytes,
                    expected_repository: identity,
                    package: &partial,
                    discovery_limits: limits,
                    portable_limits,
                    mode: PortableV2Mode::Full,
                    cancelled: Some(cancelled),
                })
                .map_err(portable_error)?;
                (accepted.immutable_version, None, None)
            };
            Ok((outcome, Some(object.length), None))
        },
    )?;
    let (immutable_version, research_version_uuid, research_version_kind) = verified;
    let operation_id = clone_operation(identity, &immutable_version, research_context.as_ref());
    profile.handoff(
        ComponentKind::PortableVerify,
        ComponentKind::Api,
        HandoffKind::Call,
        Some(object.length),
    );
    profile.handoff(
        ComponentKind::Api,
        ComponentKind::PortableImport,
        HandoffKind::Call,
        Some(object.length),
    );
    env.progress.phase("importing the project");
    let imported = profile.stage(
        Stage::AtomicImport,
        ComponentKind::PortableImport,
        ComponentRole::Persistence,
        None,
        1,
        || {
            delays.clock.wait(delays.import);
            #[cfg(test)]
            if let Some(hook) = &delays.before_import {
                hook();
            }
            // The import targets a private directory inside the staging
            // directory; only a complete, reopened project is installed at
            // the destination, by one atomic rename.
            bind_import_target(staging, &operation_id)?;
            GraphForge::import_portable_v2(
                &staging.project,
                &PortableV2ImportRequest {
                    input: partial.clone(),
                    operation_id,
                    limits: portable_limits,
                },
                Some(cancelled),
            )
            .map_err(|error| {
                // An import that did not commit leaves nothing worth
                // keeping; the rerun starts from an empty target. A committed
                // one stays, and the rerun replays it.
                if error.committed_import.is_none()
                    && let Err(cleanup) = clear_import_target(staging)
                {
                    return graphforge_api::GfError::Storage(format!(
                        "{}: portable project import failed ({error}), and removing \
                         its staged target failed: {cleanup}",
                        portable_code(error.code)
                    ));
                }
                import_error(&error)
            })
            .map(|imported| (imported, Some(object.length), None))
        },
    )?;
    if let Some(context) = &research_context
        && imported.package_digest != context.package_digest
    {
        return Err(validation(
            "hub.package.package_digest_mismatch",
            "imported research package differs from the verified Version package",
        ));
    }
    profile.handoff(
        ComponentKind::PortableImport,
        ComponentKind::Storage,
        HandoffKind::Write,
        Some(object.length),
    );
    profile.handoff(
        ComponentKind::Storage,
        ComponentKind::Publication,
        HandoffKind::Write,
        Some(object.length),
    );
    profile.handoff(
        ComponentKind::Publication,
        ComponentKind::Api,
        HandoffKind::Return,
        None,
    );
    profile.handoff(
        ComponentKind::Api,
        ComponentKind::Recovery,
        HandoffKind::Return,
        None,
    );
    // `import_portable_v2` already reopened the imported project through the
    // public facade and verified the reopened generation UUID matches the
    // published receipt (see `import_portable_v2_with_allocation`), including
    // its own UTF-8 path check — success there already proves normal runtime
    // readability. The stage itself is kept (with its test-only injected
    // delay) so clone job telemetry keeps reporting a distinct
    // recovery/verification phase.
    profile.stage(
        Stage::Reopen,
        ComponentKind::Recovery,
        ComponentRole::Verification,
        None,
        1,
        || {
            delays.clock.wait(delays.reopen);
            Ok(((), None, None))
        },
    )?;
    profile.handoff(
        ComponentKind::Recovery,
        ComponentKind::Storage,
        HandoffKind::Call,
        None,
    );
    let result = CloneResult {
        contract: CLONE_CONTRACT.to_owned(),
        repository: canonical_name(identity),
        destination: destination.display().to_string(),
        immutable_version,
        package_digest: imported.package_digest,
        generation_uuid: imported.generation_uuid.to_string(),
        resumed_bytes: download.resumed_bytes,
        research_version_uuid,
        research_version_kind,
    };
    let record = InstallRecord {
        generation_uuid: result.generation_uuid.clone(),
        result,
    };
    write_staging_file(
        &staging.installed,
        &serde_json::to_vec(&record).map_err(storage)?,
    )?;
    clone_failpoint("clone.before_install");
    install_destination(staging, destination)?;
    clone_failpoint("clone.after_install");
    Ok(record.result)
}

fn write_clone_result(
    result: &CloneResult,
    json: bool,
    output: &mut dyn Write,
) -> Result<(), graphforge_api::GfError> {
    if json {
        serde_json::to_writer(&mut *output, &result)
            .map_err(|e| graphforge_api::GfError::Execution(e.to_string()))?;
        writeln!(output).map_err(storage)?;
    } else {
        writeln!(
            output,
            "Cloned {} to {}",
            result.repository, result.destination
        )
        .map_err(storage)?;
    }
    Ok(())
}

fn canonical_name(identity: &RepositoryIdentity) -> String {
    identity.canonical_name()
}
fn protocol_error(error: &DiscoveryError) -> graphforge_api::GfError {
    let code = match error.code {
        DiscoveryErrorCode::InvalidIdentity => "hub.invalid_identity",
        DiscoveryErrorCode::MalformedResponse => "hub.malformed_response",
        DiscoveryErrorCode::UnsupportedFuture => "hub.unsupported_future",
        DiscoveryErrorCode::MissingRef => "hub.missing_ref",
        DiscoveryErrorCode::MissingObject => "hub.missing_object",
        DiscoveryErrorCode::IntegrityFailure => "hub.integrity_failure",
        DiscoveryErrorCode::UnsafeLocation => "hub.unsafe_location",
        DiscoveryErrorCode::LimitExceeded => "hub.limit_exceeded",
        DiscoveryErrorCode::Duplicate => "hub.duplicate",
    };
    validation(code, error.detail())
}
fn research_version_error(error: DiscoveryResearchVersionError) -> graphforge_api::GfError {
    match error {
        DiscoveryResearchVersionError::Discovery(error) => protocol_error(&error),
        DiscoveryResearchVersionError::ReferenceMismatch(mismatch) => validation(
            match mismatch {
                DiscoveryPortableV2Mismatch::Repository => "hub.package.repository_mismatch",
                DiscoveryPortableV2Mismatch::ImmutableVersion => {
                    "hub.package.immutable_version_mismatch"
                }
                DiscoveryPortableV2Mismatch::PackageDigest => "hub.package.package_digest_mismatch",
                DiscoveryPortableV2Mismatch::ModuleIdentity => "hub.module.identity_mismatch",
                DiscoveryPortableV2Mismatch::ModuleContentDigest => {
                    "hub.module.content_digest_mismatch"
                }
                DiscoveryPortableV2Mismatch::ResearchVersionIdentity => {
                    "hub.package.research_version_mismatch"
                }
            },
            "research discovery reference mismatch",
        ),
        DiscoveryResearchVersionError::Portable(error) => validation(
            portable_code(error.code),
            "research portable verification failed",
        ),
    }
}

fn portable_error(error: DiscoveryPortableV2Error) -> graphforge_api::GfError {
    match error {
        DiscoveryPortableV2Error::Discovery(error) => protocol_error(&error),
        DiscoveryPortableV2Error::ReferenceMismatch(mismatch) => validation(
            match mismatch {
                DiscoveryPortableV2Mismatch::Repository => "hub.package.repository_mismatch",
                DiscoveryPortableV2Mismatch::ImmutableVersion => {
                    "hub.package.immutable_version_mismatch"
                }
                DiscoveryPortableV2Mismatch::PackageDigest => "hub.package.package_digest_mismatch",
                DiscoveryPortableV2Mismatch::ModuleIdentity => "hub.module.identity_mismatch",
                DiscoveryPortableV2Mismatch::ModuleContentDigest => {
                    "hub.module.content_digest_mismatch"
                }
                DiscoveryPortableV2Mismatch::ResearchVersionIdentity => {
                    "hub.package.research_version_mismatch"
                }
            },
            "portable discovery reference mismatch",
        ),
        DiscoveryPortableV2Error::Participant { .. } => validation(
            "hub.package.invalid_participant",
            "portable project participant is invalid",
        ),
        DiscoveryPortableV2Error::Portable(error) => validation(
            portable_code(error.code),
            "portable project verification failed",
        ),
    }
}
#[cfg(test)]
mod research_clone_tests;
#[cfg(test)]
mod transfer_tests;

#[cfg(test)]
mod tests;