rch 2.1.30

Remote Compilation Helper - PreToolUse hook CLI
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
//! Dependency-closure sync planning and remote dependency preflight for the hook.
//!
//! This submodule owns the planning + verification layer that sits *between*
//! command classification and the remote-build pipeline (`transfer_orchestration`),
//! extracted from `hook.rs` per bead `remote_compilation_helper-zcecy.14`:
//!
//! - **Sync-closure planning** — given the dependency sync roots resolved by the
//!   runtime planner, [`build_sync_closure_plan`] canonicalizes each root against
//!   the worker path-topology policy, filters out-of-topology roots, dedupes
//!   aliases, and orders the plan deterministically; [`build_sync_closure_manifest`]
//!   renders the plan as a serializable [`SyncClosureManifest`]. [`merge_sync_result`]
//!   folds the per-root [`SyncResult`]s that `transfer_orchestration` accumulates.
//! - **Sync-topology predicates** — `effective_sync_topology_roots` /
//!   [`is_within_sync_topology`] / `map_sync_root_to_remote_root` encode the
//!   `/data/projects` + `/dp` canonical-root conventions.
//! - **Dependency preflight** — once the closure is synced,
//!   [`verify_remote_dependency_manifests`] probes each remote root over SSH (via
//!   the sibling `ssh` submodule's stdin-capable executor) to confirm every
//!   required `Cargo.toml` plus source entrypoint exists remotely, producing a
//!   [`DependencyPreflightReport`]; a failed verification surfaces as a
//!   [`DependencyPreflightFailure`] that forces a local fallback. The cargo
//!   manifest/workspace parsers (`cargo_package_source_entrypoints`,
//!   `cargo_workspace_member_source_entrypoints`, …) compute the required-path
//!   set from each package's `Cargo.toml`.
//!
//! It reaches its support layer from the parent via `use super::*` (the
//! path-topology types, `WorkerConfig` / `HookReporter`, and the `rch_common`
//! types/consts) and the offload SSH primitives from the sibling `ssh` submodule.
//! Symbols consumed by the parent (`build_dependency_runtime_fail_open_report` and
//! the error downcasts in `run_hook` / `run_exec`) and by the sibling
//! `transfer_orchestration` are `pub(super)`; the manifest/probe parsers and the
//! status/evidence helpers stay private. The cluster's unit tests remain in
//! `hook::tests` and reach the test-only items through an explicit
//! `use super::dependency_closure::{…}` (matching the sibling-import convention),
//! while the preflight types/consts that `hook` itself re-exports arrive via the
//! test module's `use super::*`.

use super::ssh::{run_offload_ssh_command_with_stdin, should_skip_remote_preflight};
use super::*;

pub(super) fn merge_sync_result(base: &SyncResult, extra: &SyncResult) -> SyncResult {
    SyncResult {
        bytes_transferred: base
            .bytes_transferred
            .saturating_add(extra.bytes_transferred),
        files_transferred: base
            .files_transferred
            .saturating_add(extra.files_transferred),
        duration_ms: base.duration_ms.saturating_add(extra.duration_ms),
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct SyncClosurePlanEntry {
    pub(super) local_root: PathBuf,
    pub(super) remote_root: String,
    pub(super) project_id: String,
    pub(super) root_hash: String,
    pub(super) is_primary: bool,
    pub(super) mode: SyncClosureMode,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum SyncClosureMode {
    Full,
    WorkspaceMetadata,
}

/// Stable source and artifact locations for a freshly materialized owned closure.
/// The caller must hold the source-pair lease, require an absent source root,
/// verify the selected bytes, and retain the complete tree before releasing it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct OwnedSyncClosureLayout {
    pub(super) source_root: String,
    pub(super) pooled_target_name: String,
    pub(super) plan: Vec<SyncClosurePlanEntry>,
}

#[derive(Debug, thiserror::Error)]
pub(super) enum OwnedSyncClosureLayoutError {
    #[error("invalid owned closure staging base: {0}")]
    InvalidBase(String),
    #[error("owned closure requires a valid pooled Cargo target name")]
    InvalidPoolName,
    #[error("invalid owned closure plan: {0}")]
    InvalidPlan(&'static str),
    #[error("invalid owned closure root {root}: {reason}")]
    InvalidRoot { root: PathBuf, reason: String },
    #[error("owned closure roots have overlapping transfer selections: {first} and {second}")]
    OverlappingRoots { first: PathBuf, second: PathBuf },
}

/// Outcome of syncing a single closure root during multi-root transfer.
///
/// Used to collect per-root results and enable partial failure diagnostics
/// instead of aborting the entire sync on the first dependency root failure.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum SyncRootOutcome {
    /// Root synced successfully.
    Synced,
    /// Dependency root sync was skipped (transfer estimation indicated skip).
    Skipped { reason: String },
    /// Dependency root sync failed (non-fatal for non-primary roots).
    Failed { error: String },
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub(super) struct SyncClosureManifest {
    pub(super) schema_version: &'static str,
    pub(super) generated_at_unix_ms: i64,
    pub(super) project_root: String,
    pub(super) entries: Vec<SyncClosureManifestEntry>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub(super) struct SyncClosureManifestEntry {
    pub(super) order: usize,
    pub(super) local_root: String,
    pub(super) remote_root: String,
    pub(super) project_id: String,
    pub(super) root_hash: String,
    pub(super) is_primary: bool,
    mode: SyncClosureMode,
}

pub(super) const DEPENDENCY_PREFLIGHT_SCHEMA_VERSION: &str = "rch.dependency_preflight.v1";
const DEPENDENCY_PREFLIGHT_CODE_PRESENT: &str = "RCH-I410";
pub(super) const DEPENDENCY_PREFLIGHT_CODE_MISSING: &str = "RCH-E410";
pub(super) const DEPENDENCY_PREFLIGHT_CODE_STALE: &str = "RCH-E411";
pub(super) const DEPENDENCY_PREFLIGHT_CODE_UNKNOWN: &str = "RCH-E412";
pub(super) const DEPENDENCY_PREFLIGHT_CODE_POLICY: &str = "RCH-E413";
pub(super) const DEPENDENCY_PREFLIGHT_CODE_TIMEOUT: &str = "RCH-E414";
pub(super) const DEPENDENCY_PREFLIGHT_CODE_MATERIALIZATION: &str = "RCH-E415";
pub(super) const DEPENDENCY_PREFLIGHT_REMEDIATION_MISSING: &str = "Ensure every dependency root in the closure is synced and Cargo.toml plus required source entrypoints exist remotely.";
pub(super) const DEPENDENCY_PREFLIGHT_REMEDIATION_STALE: &str = "One or more dependency roots were not refreshed; rerun after successful sync of skipped roots.";
pub(super) const DEPENDENCY_PREFLIGHT_REMEDIATION_UNKNOWN: &str =
    "Dependency verification could not determine remote state; inspect sync/SSH logs and retry.";
pub(super) const DEPENDENCY_PREFLIGHT_REMEDIATION_POLICY: &str = "Path dependency topology policy failed; move dependencies under the canonical project root (path_topology.canonical_root, default /data/projects, alias /dp) and retry.";
pub(super) const DEPENDENCY_PREFLIGHT_REMEDIATION_TIMEOUT: &str = "Dependency planner timed out; rerun after system load decreases or investigate cargo metadata latency.";
pub(super) const DEPENDENCY_PREFLIGHT_REMEDIATION_MATERIALIZATION: &str = "Cargo path materialization is incomplete; repair the reported repository manifest/path dependency before retrying remote Cargo execution.";
const DEPENDENCY_PREFLIGHT_PATHS_PER_TIMEOUT_WINDOW: usize = 128;
const WORKSPACE_METADATA_SYNC_PATTERNS: &[&str] = &[
    "Cargo.toml",
    "Cargo.lock",
    "rust-toolchain",
    "rust-toolchain.toml",
    ".cargo/",
    ".cargo/**",
];

#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub(super) enum DependencyPreflightStatus {
    Present,
    Missing,
    Stale,
    PolicyViolation,
    Timeout,
    MaterializationUnavailable,
    Unknown,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub(super) struct DependencyPreflightEvidence {
    pub(super) root: String,
    pub(super) manifest: String,
    pub(super) required_path: String,
    pub(super) required_kind: &'static str,
    pub(super) status: DependencyPreflightStatus,
    pub(super) reason_code: &'static str,
    pub(super) detail: String,
    pub(super) is_primary: bool,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub(super) struct DependencyPreflightReport {
    pub(super) schema_version: &'static str,
    pub(super) worker: String,
    pub(super) verified: bool,
    pub(super) reason_code: Option<&'static str>,
    pub(super) remediation: Option<&'static str>,
    pub(super) evidence: Vec<DependencyPreflightEvidence>,
}

#[derive(Debug, thiserror::Error)]
#[error("dependency preflight verification failed [{reason_code}]")]
pub(super) struct DependencyPreflightFailure {
    pub(super) reason_code: &'static str,
    pub(super) remediation: String,
    report: DependencyPreflightReport,
}

impl DependencyPreflightFailure {
    pub(super) fn from_report(report: DependencyPreflightReport) -> Self {
        let reason_code = report
            .reason_code
            .unwrap_or(DEPENDENCY_PREFLIGHT_CODE_UNKNOWN);
        let remediation = report
            .remediation
            .unwrap_or(DEPENDENCY_PREFLIGHT_REMEDIATION_UNKNOWN);
        Self {
            reason_code,
            remediation: dependency_preflight_remediation(reason_code, remediation, &report.worker),
            report,
        }
    }

    pub(super) fn report_json(&self) -> String {
        serde_json::to_string(&self.report).unwrap_or_else(|err| {
            format!(
                "{{\"schema_version\":\"{}\",\"verified\":false,\"reason_code\":\"{}\",\"serialization_error\":\"{}\"}}",
                DEPENDENCY_PREFLIGHT_SCHEMA_VERSION, self.reason_code, err
            )
        })
    }

    pub(super) fn evidence_summary(&self) -> String {
        dependency_preflight_evidence_summary(&self.report.evidence)
    }
}

fn dependency_preflight_remediation(
    reason_code: &str,
    remediation: &str,
    worker_id: &str,
) -> String {
    if matches!(
        reason_code,
        DEPENDENCY_PREFLIGHT_CODE_MISSING | DEPENDENCY_PREFLIGHT_CODE_STALE
    ) {
        let worker = shell_escape::escape(worker_id.into());
        return format!(
            "{remediation} To explicitly refresh this worker's RCH-managed source cache, run `rch sync --worker {worker} --force`, then rerun the Cargo command."
        );
    }

    remediation.to_string()
}

fn dependency_preflight_status_label(status: DependencyPreflightStatus) -> &'static str {
    match status {
        DependencyPreflightStatus::Present => "present",
        DependencyPreflightStatus::Missing => "missing",
        DependencyPreflightStatus::Stale => "stale",
        DependencyPreflightStatus::PolicyViolation => "policy_violation",
        DependencyPreflightStatus::Timeout => "timeout",
        DependencyPreflightStatus::MaterializationUnavailable => "materialization_unavailable",
        DependencyPreflightStatus::Unknown => "unknown",
    }
}

fn dependency_preflight_evidence_summary(evidence: &[DependencyPreflightEvidence]) -> String {
    let selected = evidence
        .iter()
        .find(|item| item.status != DependencyPreflightStatus::Present)
        .or_else(|| evidence.first());

    match selected {
        Some(item) => format!(
            "{} {} {}: {}",
            dependency_preflight_status_label(item.status),
            item.required_kind,
            item.required_path,
            item.detail
        ),
        None => "no dependency preflight evidence rows".to_string(),
    }
}

pub(super) fn parse_dependency_preflight_probe_output(
    stdout: &str,
) -> (
    std::collections::BTreeSet<String>,
    std::collections::BTreeSet<String>,
) {
    let mut present = std::collections::BTreeSet::new();
    let mut missing = std::collections::BTreeSet::new();

    for line in stdout.lines() {
        if let Some(path) = line.strip_prefix("RCH_DEP_PRESENT:") {
            present.insert(path.trim().to_string());
        } else if let Some(path) = line.strip_prefix("RCH_DEP_MISSING:") {
            missing.insert(path.trim().to_string());
        }
    }

    (present, missing)
}

fn dependency_preflight_failure_reason(
    evidence: &[DependencyPreflightEvidence],
) -> Option<(&'static str, &'static str)> {
    // For Cargo dependency-closure builds, every synchronized root must reflect
    // the current local state. Proceeding when any non-primary dependency root
    // is stale or missing can silently compile against an older sibling checkout
    // on the worker, which is worse than falling back to a local build.
    if evidence
        .iter()
        .any(|item| item.status == DependencyPreflightStatus::Missing)
    {
        return Some((
            DEPENDENCY_PREFLIGHT_CODE_MISSING,
            DEPENDENCY_PREFLIGHT_REMEDIATION_MISSING,
        ));
    }
    if evidence
        .iter()
        .any(|item| item.status == DependencyPreflightStatus::Stale)
    {
        return Some((
            DEPENDENCY_PREFLIGHT_CODE_STALE,
            DEPENDENCY_PREFLIGHT_REMEDIATION_STALE,
        ));
    }
    if evidence
        .iter()
        .any(|item| item.status == DependencyPreflightStatus::MaterializationUnavailable)
    {
        return Some((
            DEPENDENCY_PREFLIGHT_CODE_MATERIALIZATION,
            DEPENDENCY_PREFLIGHT_REMEDIATION_MATERIALIZATION,
        ));
    }
    if evidence
        .iter()
        .any(|item| item.status == DependencyPreflightStatus::Unknown)
    {
        return Some((
            DEPENDENCY_PREFLIGHT_CODE_UNKNOWN,
            DEPENDENCY_PREFLIGHT_REMEDIATION_UNKNOWN,
        ));
    }
    None
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct DependencyPreflightCheck {
    pub(super) root: String,
    pub(super) manifest: String,
    pub(super) required_path: String,
    pub(super) required_kind: &'static str,
    pub(super) is_primary: bool,
}

fn clean_relative_cargo_path(raw: &str) -> Option<PathBuf> {
    let path = Path::new(raw);
    if path.is_absolute() {
        return None;
    }
    if path.components().any(|component| {
        matches!(
            component,
            Component::ParentDir | Component::RootDir | Component::Prefix(_)
        )
    }) {
        return None;
    }
    Some(path.to_path_buf())
}

fn table_path_value(table: &toml::Table, key: &str) -> Option<PathBuf> {
    table
        .get(key)
        .and_then(toml::Value::as_str)
        .and_then(clean_relative_cargo_path)
}

fn target_array_paths(table: &toml::Table, key: &str) -> Vec<PathBuf> {
    table
        .get(key)
        .and_then(toml::Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(toml::Value::as_table)
        .filter_map(|target| table_path_value(target, "path"))
        .collect()
}

fn table_string_array(table: &toml::Table, key: &str) -> Vec<String> {
    table
        .get(key)
        .and_then(toml::Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(toml::Value::as_str)
        .map(str::to_string)
        .collect()
}

fn package_auto_discovery_enabled(table: &toml::Table, key: &str) -> bool {
    table
        .get("package")
        .and_then(toml::Value::as_table)
        .and_then(|package| package.get(key))
        .and_then(toml::Value::as_bool)
        .unwrap_or(true)
}

fn insert_existing_relative_entrypoint(
    paths: &mut std::collections::BTreeSet<PathBuf>,
    package_root: &Path,
    relative_path: impl Into<PathBuf>,
) {
    let relative_path = relative_path.into();
    if package_root.join(&relative_path).is_file() {
        paths.insert(relative_path);
    }
}

fn insert_auto_discovered_target_entrypoints(
    paths: &mut std::collections::BTreeSet<PathBuf>,
    package_root: &Path,
    relative_dir: &str,
) {
    let Ok(entries) = std::fs::read_dir(package_root.join(relative_dir)) else {
        return;
    };

    for entry in entries.flatten() {
        let Ok(file_type) = entry.file_type() else {
            continue;
        };
        let file_name = entry.file_name();
        if file_type.is_file() {
            if entry.path().extension().is_some_and(|ext| ext == "rs") {
                paths.insert(PathBuf::from(relative_dir).join(file_name));
            }
        } else if file_type.is_dir() {
            insert_existing_relative_entrypoint(
                paths,
                package_root,
                PathBuf::from(relative_dir).join(file_name).join("main.rs"),
            );
        }
    }
}

pub(super) fn cargo_package_source_entrypoints(package_root: &Path) -> Vec<PathBuf> {
    let manifest_path = package_root.join("Cargo.toml");
    let Ok(contents) = std::fs::read_to_string(&manifest_path) else {
        return Vec::new();
    };
    let Ok(table) = toml::from_str::<toml::Table>(&contents) else {
        return Vec::new();
    };
    if !table.contains_key("package") {
        return Vec::new();
    }

    let mut paths = std::collections::BTreeSet::<PathBuf>::new();

    if let Some(lib_path) = table
        .get("lib")
        .and_then(toml::Value::as_table)
        .and_then(|lib| table_path_value(lib, "path"))
    {
        paths.insert(lib_path);
    } else if package_auto_discovery_enabled(&table, "autolib") || table.contains_key("lib") {
        insert_existing_relative_entrypoint(&mut paths, package_root, "src/lib.rs");
    }

    if package_auto_discovery_enabled(&table, "autobins") {
        insert_existing_relative_entrypoint(&mut paths, package_root, "src/main.rs");
        insert_auto_discovered_target_entrypoints(&mut paths, package_root, "src/bin");
    }
    if package_auto_discovery_enabled(&table, "autoexamples") {
        insert_auto_discovered_target_entrypoints(&mut paths, package_root, "examples");
    }
    if package_auto_discovery_enabled(&table, "autotests") {
        insert_auto_discovered_target_entrypoints(&mut paths, package_root, "tests");
    }
    if package_auto_discovery_enabled(&table, "autobenches") {
        insert_auto_discovered_target_entrypoints(&mut paths, package_root, "benches");
    }

    for key in ["bin", "example", "test", "bench"] {
        paths.extend(target_array_paths(&table, key));
    }

    paths.into_iter().collect()
}

fn cargo_manifest_table(package_root: &Path) -> Option<toml::Table> {
    let manifest_path = package_root.join("Cargo.toml");
    let contents = std::fs::read_to_string(&manifest_path).ok()?;
    toml::from_str::<toml::Table>(&contents).ok()
}

fn has_glob_meta(pattern: &str) -> bool {
    pattern
        .bytes()
        .any(|byte| matches!(byte, b'*' | b'?' | b'['))
}

fn path_slash_string(path: &Path) -> Option<String> {
    Some(
        path.to_str()?
            .trim_start_matches("./")
            .replace(std::path::MAIN_SEPARATOR, "/"),
    )
}

fn workspace_exclude_matches(relative_member: &Path, exclude_patterns: &[String]) -> bool {
    let Some(relative_member) = path_slash_string(relative_member) else {
        return false;
    };

    exclude_patterns.iter().any(|pattern| {
        let Some(pattern_path) = clean_relative_cargo_path(pattern) else {
            return false;
        };
        let Some(pattern_slash) = path_slash_string(&pattern_path) else {
            return false;
        };
        if has_glob_meta(&pattern_slash) {
            glob::Pattern::new(&pattern_slash)
                .map(|compiled| compiled.matches(&relative_member))
                .unwrap_or(false)
        } else {
            relative_member == pattern_slash
                || relative_member
                    .strip_prefix(&pattern_slash)
                    .is_some_and(|suffix| suffix.starts_with('/'))
        }
    })
}

fn workspace_member_manifest_paths(workspace_root: &Path) -> Vec<PathBuf> {
    let Some(table) = cargo_manifest_table(workspace_root) else {
        return Vec::new();
    };
    let Some(workspace) = table.get("workspace").and_then(toml::Value::as_table) else {
        return Vec::new();
    };

    let members = table_string_array(workspace, "members");
    let exclude_patterns = table_string_array(workspace, "exclude");
    let mut manifests = std::collections::BTreeSet::<PathBuf>::new();

    for member in members {
        let Some(member_path) = clean_relative_cargo_path(&member) else {
            continue;
        };
        if has_glob_meta(&member) {
            let glob_path = workspace_root.join(&member_path).join("Cargo.toml");
            let Some(glob_pattern) = glob_path.to_str() else {
                continue;
            };
            let Ok(entries) = glob::glob(glob_pattern) else {
                continue;
            };
            for entry in entries.flatten() {
                if !entry.is_file() {
                    continue;
                }
                let Ok(relative_manifest) = entry.strip_prefix(workspace_root) else {
                    continue;
                };
                let Some(relative_member) = relative_manifest.parent() else {
                    continue;
                };
                if !workspace_exclude_matches(relative_member, &exclude_patterns) {
                    manifests.insert(relative_manifest.to_path_buf());
                }
            }
        } else {
            let relative_manifest = member_path.join("Cargo.toml");
            if workspace_root.join(&relative_manifest).is_file()
                && !workspace_exclude_matches(&member_path, &exclude_patterns)
            {
                manifests.insert(relative_manifest);
            }
        }
    }

    manifests.into_iter().collect()
}

pub(super) fn cargo_workspace_member_source_entrypoints(workspace_root: &Path) -> Vec<PathBuf> {
    let mut paths = std::collections::BTreeSet::<PathBuf>::new();
    for manifest_path in workspace_member_manifest_paths(workspace_root) {
        paths.insert(manifest_path.clone());
        if let Some(member_root) = manifest_path.parent() {
            paths.extend(
                cargo_package_source_entrypoints(&workspace_root.join(member_root))
                    .into_iter()
                    .map(|entrypoint| member_root.join(entrypoint)),
            );
        }
    }

    paths.into_iter().collect()
}

pub(super) fn dependency_preflight_checks_for_entry(
    entry: &SyncClosurePlanEntry,
) -> Vec<DependencyPreflightCheck> {
    let remote_root = PathBuf::from(&entry.remote_root);
    let manifest = remote_root.join("Cargo.toml").to_string_lossy().to_string();
    let mut checks = Vec::new();
    checks.push(DependencyPreflightCheck {
        root: entry.remote_root.clone(),
        manifest: manifest.clone(),
        required_path: manifest.clone(),
        required_kind: "manifest",
        is_primary: entry.is_primary,
    });

    if entry.mode == SyncClosureMode::Full {
        checks.extend(
            cargo_package_source_entrypoints(&entry.local_root)
                .into_iter()
                .map(|relative_path| DependencyPreflightCheck {
                    root: entry.remote_root.clone(),
                    manifest: manifest.clone(),
                    required_path: remote_root
                        .join(relative_path)
                        .to_string_lossy()
                        .to_string(),
                    required_kind: "source_entrypoint",
                    is_primary: entry.is_primary,
                }),
        );
        checks.extend(
            cargo_workspace_member_source_entrypoints(&entry.local_root)
                .into_iter()
                .map(|relative_path| DependencyPreflightCheck {
                    root: entry.remote_root.clone(),
                    manifest: manifest.clone(),
                    required_path: remote_root
                        .join(relative_path)
                        .to_string_lossy()
                        .to_string(),
                    required_kind: "source_entrypoint",
                    is_primary: entry.is_primary,
                }),
        );
    }

    checks.sort_by(|left, right| {
        (&left.required_path, left.required_kind).cmp(&(&right.required_path, right.required_kind))
    });
    checks.dedup_by(|left, right| {
        left.required_path == right.required_path && left.required_kind == right.required_kind
    });
    checks
}

pub(super) fn synced_dependency_preflight_checks(
    root_outcomes: &[(SyncClosurePlanEntry, SyncRootOutcome)],
) -> Vec<DependencyPreflightCheck> {
    root_outcomes
        .iter()
        .filter(|(_, outcome)| matches!(outcome, SyncRootOutcome::Synced))
        .flat_map(|(entry, _)| dependency_preflight_checks_for_entry(entry))
        .collect()
}

pub(super) fn build_dependency_preflight_report(
    worker: &WorkerConfig,
    root_outcomes: &[(SyncClosurePlanEntry, SyncRootOutcome)],
    present_paths: &std::collections::BTreeSet<String>,
    missing_paths: &std::collections::BTreeSet<String>,
    probe_failure: Option<&str>,
) -> DependencyPreflightReport {
    let mut evidence = Vec::new();
    let mut unknown_probe_samples = std::collections::BTreeSet::<(String, &'static str)>::new();

    for (entry, outcome) in root_outcomes {
        for check in dependency_preflight_checks_for_entry(entry) {
            let (status, reason_code, detail) = match outcome {
                SyncRootOutcome::Synced => {
                    if missing_paths.contains(&check.required_path) {
                        let detail = match check.required_kind {
                            "manifest" => "required Cargo.toml is missing on remote worker",
                            "source_entrypoint" => {
                                "required package source entrypoint is missing on remote worker"
                            }
                            _ => "required dependency path is missing on remote worker",
                        };
                        (
                            DependencyPreflightStatus::Missing,
                            DEPENDENCY_PREFLIGHT_CODE_MISSING,
                            format!("{detail}: {}", check.required_path),
                        )
                    } else if present_paths.contains(&check.required_path) {
                        (
                            DependencyPreflightStatus::Present,
                            DEPENDENCY_PREFLIGHT_CODE_PRESENT,
                            format!(
                                "required {} present after current sync: {}",
                                check.required_kind, check.required_path
                            ),
                        )
                    } else {
                        let detail = probe_failure
                            .map(|failure| {
                                format!(
                                    "dependency probe unavailable for {} under {}; sample required_path={}; additional unreported paths for this root/kind share this failure; {}",
                                    check.required_kind,
                                    check.root,
                                    check.required_path,
                                    failure
                                )
                            })
                            .unwrap_or_else(|| {
                                format!(
                                    "probe output omitted status for synced required path: {}",
                                    check.required_path
                                )
                            });
                        (
                            DependencyPreflightStatus::Unknown,
                            DEPENDENCY_PREFLIGHT_CODE_UNKNOWN,
                            detail,
                        )
                    }
                }
                SyncRootOutcome::Skipped { reason } => (
                    DependencyPreflightStatus::Stale,
                    DEPENDENCY_PREFLIGHT_CODE_STALE,
                    format!(
                        "dependency root skipped before verification for {}: {}",
                        check.required_path, reason
                    ),
                ),
                SyncRootOutcome::Failed { error } => (
                    DependencyPreflightStatus::Unknown,
                    DEPENDENCY_PREFLIGHT_CODE_UNKNOWN,
                    format!(
                        "dependency root sync failed before verification for {}: {}",
                        check.required_path, error
                    ),
                ),
            };

            if probe_failure.is_some()
                && status == DependencyPreflightStatus::Unknown
                && matches!(outcome, SyncRootOutcome::Synced)
                && !unknown_probe_samples.insert((check.root.clone(), check.required_kind))
            {
                continue;
            }

            evidence.push(DependencyPreflightEvidence {
                root: check.root,
                manifest: check.manifest,
                required_path: check.required_path,
                required_kind: check.required_kind,
                status,
                reason_code,
                detail,
                is_primary: check.is_primary,
            });
        }
    }

    if evidence.is_empty() {
        for (entry, outcome) in root_outcomes {
            let manifest = PathBuf::from(&entry.remote_root)
                .join("Cargo.toml")
                .to_string_lossy()
                .to_string();
            let (status, reason_code, detail) = match outcome {
                SyncRootOutcome::Synced => (
                    DependencyPreflightStatus::Unknown,
                    DEPENDENCY_PREFLIGHT_CODE_UNKNOWN,
                    "dependency preflight had no required paths to verify".to_string(),
                ),
                SyncRootOutcome::Skipped { reason } => (
                    DependencyPreflightStatus::Stale,
                    DEPENDENCY_PREFLIGHT_CODE_STALE,
                    format!("dependency root skipped before verification: {}", reason),
                ),
                SyncRootOutcome::Failed { error } => (
                    DependencyPreflightStatus::Unknown,
                    DEPENDENCY_PREFLIGHT_CODE_UNKNOWN,
                    format!("dependency root sync failed before verification: {}", error),
                ),
            };
            evidence.push(DependencyPreflightEvidence {
                root: entry.remote_root.clone(),
                manifest: manifest.clone(),
                required_path: manifest,
                required_kind: "manifest",
                status,
                reason_code,
                detail,
                is_primary: entry.is_primary,
            });
        }
    }

    let (verified, reason_code, remediation) = match dependency_preflight_failure_reason(&evidence)
    {
        Some((reason_code, remediation)) => (false, Some(reason_code), Some(remediation)),
        None => (true, None, None),
    };

    DependencyPreflightReport {
        schema_version: DEPENDENCY_PREFLIGHT_SCHEMA_VERSION,
        worker: worker.id.as_str().to_string(),
        verified,
        reason_code,
        remediation,
        evidence,
    }
}

pub(super) fn canonicalize_sync_root_for_plan(root: &Path, policy: &PathTopologyPolicy) -> PathBuf {
    normalize_dependency_root_for_runtime(root, policy)
        .or_else(|| std::fs::canonicalize(root).ok())
        .unwrap_or_else(|| root.to_path_buf())
}

fn manifest_declares_workspace(manifest_path: &Path) -> bool {
    let Ok(contents) = std::fs::read_to_string(manifest_path) else {
        return false;
    };
    let Ok(table) = toml::from_str::<toml::Table>(&contents) else {
        return false;
    };
    table.contains_key("workspace")
}

fn enclosing_workspace_root_for_sync_root(
    root: &Path,
    policy: &PathTopologyPolicy,
) -> Option<PathBuf> {
    for candidate in root.ancestors() {
        if !is_within_sync_topology(candidate, policy) {
            break;
        }
        let manifest_path = candidate.join("Cargo.toml");
        if manifest_path.is_file() && manifest_declares_workspace(&manifest_path) {
            return (candidate != root).then(|| candidate.to_path_buf());
        }
    }

    None
}

pub(super) fn workspace_metadata_sync_patterns() -> Vec<String> {
    WORKSPACE_METADATA_SYNC_PATTERNS
        .iter()
        .map(|pattern| (*pattern).to_string())
        .collect()
}

fn effective_sync_topology_roots(policy: &PathTopologyPolicy) -> Vec<PathBuf> {
    let mut roots = Vec::new();
    roots.push(policy.canonical_root().to_path_buf());
    roots.push(policy.alias_root().to_path_buf());

    if !policy.canonical_root().exists()
        && let Ok(alias_target) = std::fs::canonicalize(policy.alias_root())
    {
        roots.push(alias_target);
    }

    roots.sort();
    roots.dedup();
    roots
}

/// Returns `true` if `path` is within the allowed topology roots.
pub(super) fn is_within_sync_topology(path: &Path, policy: &PathTopologyPolicy) -> bool {
    effective_sync_topology_roots(policy)
        .into_iter()
        .any(|root| path.starts_with(&root))
}

fn map_sync_root_to_remote_root(path: &Path, policy: &PathTopologyPolicy) -> String {
    // The remote canonical root comes from the same policy the rest of the
    // hook uses, never the compile-time `DEFAULT_CANONICAL_PROJECT_ROOT`,
    // because hosts that don't ship `/data/projects` (which is most of them)
    // configure their own canonical via `~/.config/rch/config.toml` and
    // would otherwise see their `[path_topology]` setting silently ignored
    // for the sync-target translation step. See rch#15.
    let remote_root = policy.canonical_root();

    for root in effective_sync_topology_roots(policy) {
        if let Ok(relative) = path.strip_prefix(&root) {
            // Joining an empty suffix appends a separator. A project at the
            // topology root must retain its exact canonical authority spelling.
            if relative.as_os_str().is_empty() {
                return remote_root.to_string_lossy().to_string();
            }
            return remote_root.join(relative).to_string_lossy().to_string();
        }
    }

    path.to_string_lossy().to_string()
}

pub(super) fn build_sync_closure_plan(
    sync_roots: &[PathBuf],
    normalized_project_root: &Path,
    project_hash: &str,
    topology_policy: &PathTopologyPolicy,
) -> Vec<SyncClosurePlanEntry> {
    let mut ordered_entries = std::collections::BTreeSet::<(PathBuf, SyncClosureMode)>::new();
    // The entry/project root is a `Full` sync root that rsyncs its whole tree,
    // so any dependency root physically inside it is already covered. Compute it
    // up front so intra-primary members can be collapsed instead of re-synced.
    let primary_root = canonicalize_sync_root_for_plan(normalized_project_root, topology_policy);
    for root in sync_roots {
        let canonicalized = canonicalize_sync_root_for_plan(root, topology_policy);
        if !is_within_sync_topology(&canonicalized, topology_policy) {
            warn!(
                "Dependency root {} (canonicalized: {}) is outside allowed topology ({} / {}); skipping from sync closure",
                root.display(),
                canonicalized.display(),
                topology_policy.canonical_root().display(),
                topology_policy.alias_root().display(),
            );
            continue;
        }
        // Intra-workspace collapse (rch#33, restoring #8's behavior scoped so the
        // #29d0d63 foreign-workspace metadata path is untouched): a member root
        // physically inside the primary `Full` root is transferred as part of the
        // primary tree, so its own `Full` entry — and the enclosing-workspace
        // metadata entry, which is the primary itself — are pure redundancy that
        // costs one SSH+rsync round-trip per member. Foreign-workspace path deps
        // (outside the primary root) keep today's member-`Full` + workspace-
        // metadata handling below.
        if canonicalized != primary_root && canonicalized.starts_with(&primary_root) {
            continue;
        }
        ordered_entries.insert((canonicalized.clone(), SyncClosureMode::Full));
        if let Some(workspace_root) =
            enclosing_workspace_root_for_sync_root(&canonicalized, topology_policy)
        {
            ordered_entries.insert((workspace_root, SyncClosureMode::WorkspaceMetadata));
        }
    }

    ordered_entries.insert((primary_root.clone(), SyncClosureMode::Full));
    let full_roots: Vec<PathBuf> = ordered_entries
        .iter()
        .filter(|(_, mode)| *mode == SyncClosureMode::Full)
        .map(|(root, _)| root.clone())
        .collect();
    for root in full_roots {
        ordered_entries.remove(&(root, SyncClosureMode::WorkspaceMetadata));
    }

    ordered_entries
        .into_iter()
        .map(|(root, mode)| {
            let is_primary = root == primary_root && mode == SyncClosureMode::Full;
            let root_hash = if is_primary {
                project_hash.to_string()
            } else {
                compute_project_hash_with_dependency_roots_and_policy(&root, &[], topology_policy)
            };
            SyncClosurePlanEntry {
                remote_root: map_sync_root_to_remote_root(&root, topology_policy),
                project_id: project_id_from_path(&root),
                root_hash,
                is_primary,
                mode,
                local_root: root,
            }
        })
        .collect()
}

/// Project an exact closure beneath one owned non-Git source container without
/// flattening sibling dependencies or their enclosing workspace metadata.
///
/// Both paths are stable across source edits. The existing pool name already
/// binds the project, worker, toolchain, target, profile, and features; the new
/// domain additionally binds the worker base and complete transfer layout. This
/// migrates artifacts that embedded a different source path without turning
/// every invocation into a cold build. Content identity belongs to the receipt
/// and freshness ledger, never to this path key.
pub(super) fn build_owned_sync_closure_layout(
    plan: &[SyncClosurePlanEntry],
    topology_policy: &PathTopologyPolicy,
    remote_base: &str,
    legacy_pool_name: &str,
) -> Result<OwnedSyncClosureLayout, OwnedSyncClosureLayoutError> {
    use OwnedSyncClosureLayoutError as Error;

    let base = rch_common::types::validate_remote_base(remote_base).map_err(Error::InvalidBase)?;
    if base.chars().any(char::is_control)
        || base.contains('\\')
        || base.split('/').any(|part| part == "." || part == "..")
    {
        return Err(Error::InvalidBase("invalid path component".into()));
    }
    let base: PathBuf = Path::new(&base).components().collect();
    let base = base
        .to_str()
        .ok_or_else(|| Error::InvalidBase("path is not UTF-8".into()))?;
    let (pool_prefix, _) = legacy_pool_name
        .rsplit_once("-pool-")
        .filter(|(prefix, key)| {
            prefix.starts_with(".rch-target-")
                && prefix.len() > ".rch-target-".len()
                && prefix[".rch-target-".len()..]
                    .bytes()
                    .all(|byte| byte.is_ascii_alphanumeric() || b"-_".contains(&byte))
                && key.len() == 32
                && key.bytes().all(|byte| byte.is_ascii_hexdigit())
        })
        .ok_or(Error::InvalidPoolName)?;
    if plan.iter().filter(|entry| entry.is_primary).count() != 1
        || plan
            .iter()
            .any(|entry| entry.is_primary && entry.mode != SyncClosureMode::Full)
    {
        return Err(Error::InvalidPlan(
            "exactly one full primary root is required",
        ));
    }

    let mut topology_root = None;
    let mut entries = Vec::with_capacity(plan.len());
    for entry in plan {
        let normalized = normalize_project_path_with_policy(&entry.local_root, topology_policy)
            .map_err(|error| Error::InvalidRoot {
                root: entry.local_root.clone(),
                reason: error.to_string(),
            })?;
        if topology_root
            .as_ref()
            .is_some_and(|root| root != normalized.canonical_root())
        {
            return Err(Error::InvalidPlan(
                "canonical topology changed during planning",
            ));
        }
        topology_root = Some(normalized.canonical_root().to_path_buf());
        let relative = normalized
            .canonical_path()
            .strip_prefix(normalized.canonical_root())
            .map_err(|_| Error::InvalidPlan("root escaped its normalized topology"))?;
        let literal = relative.to_str().ok_or_else(|| Error::InvalidRoot {
            root: entry.local_root.clone(),
            reason: "path is not UTF-8".into(),
        })?;
        if !normalized.canonical_path().is_dir()
            || literal.chars().any(char::is_control)
            || literal.contains('\\')
            || relative
                .components()
                .any(|part| !matches!(part, std::path::Component::Normal(_)))
        {
            return Err(Error::InvalidRoot {
                root: entry.local_root.clone(),
                reason: "root must be a directory with literal path components".into(),
            });
        }
        let mut entry = entry.clone();
        entry.local_root = normalized.canonical_path().to_path_buf();
        entries.push((relative.to_path_buf(), entry));
    }
    entries.sort_by(|(left, _), (right, _)| left.cmp(right));
    for (index, (ancestor, entry)) in entries.iter().enumerate() {
        for (descendant, other) in &entries[index + 1..] {
            let Ok(relative) = descendant.strip_prefix(ancestor) else {
                continue;
            };
            // Metadata ancestors intentionally coexist with package roots. Its
            // selected .cargo subtree and direct metadata names must not overlap
            // another root, and a full ancestor already owns every descendant.
            let metadata_overlap = relative.components().next().is_some_and(|part| {
                matches!(
                    part.as_os_str().to_str(),
                    Some(
                        ".cargo"
                            | "Cargo.toml"
                            | "Cargo.lock"
                            | "rust-toolchain"
                            | "rust-toolchain.toml"
                    )
                )
            });
            if relative.as_os_str().is_empty()
                || entry.mode == SyncClosureMode::Full
                || metadata_overlap
            {
                return Err(Error::OverlappingRoots {
                    first: entry.local_root.clone(),
                    second: other.local_root.clone(),
                });
            }
        }
    }

    fn field(hasher: &mut blake3::Hasher, value: &[u8]) {
        hasher.update(&(value.len() as u64).to_le_bytes());
        hasher.update(value);
    }
    let mut hasher = blake3::Hasher::new();
    hasher.update(b"rch-source-content-pair-layout-v1\0");
    field(&mut hasher, base.as_bytes());
    field(&mut hasher, legacy_pool_name.as_bytes());
    for (relative, entry) in &entries {
        field(
            &mut hasher,
            relative.to_str().expect("validated UTF-8 path").as_bytes(),
        );
        field(
            &mut hasher,
            match entry.mode {
                SyncClosureMode::Full => b"full",
                SyncClosureMode::WorkspaceMetadata => b"workspace_metadata",
            },
        );
        field(&mut hasher, &[u8::from(entry.is_primary)]);
    }
    let key = hasher.finalize().to_hex()[..32].to_string();
    let source_root = format!("{base}/source-content-pair-{key}");
    let projected_root = Path::new(&source_root).join("projects");
    let plan = entries
        .into_iter()
        .map(|(relative, mut entry)| {
            let destination = if relative.as_os_str().is_empty() {
                projected_root.clone()
            } else {
                projected_root.join(relative)
            };
            entry.remote_root = destination.to_string_lossy().into_owned();
            entry
        })
        .collect();
    Ok(OwnedSyncClosureLayout {
        source_root,
        pooled_target_name: format!("{pool_prefix}-pool-{key}"),
        plan,
    })
}

pub(super) fn build_sync_closure_manifest(
    plan: &[SyncClosurePlanEntry],
    normalized_project_root: &Path,
) -> SyncClosureManifest {
    let generated_at_unix_ms = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_millis() as i64)
        .unwrap_or_default();
    let entries = plan
        .iter()
        .enumerate()
        .map(|(idx, entry)| SyncClosureManifestEntry {
            order: idx + 1,
            local_root: entry.local_root.to_string_lossy().to_string(),
            remote_root: entry.remote_root.clone(),
            project_id: entry.project_id.clone(),
            root_hash: entry.root_hash.clone(),
            is_primary: entry.is_primary,
            mode: entry.mode,
        })
        .collect();
    SyncClosureManifest {
        schema_version: "rch.sync_closure_manifest.v2",
        generated_at_unix_ms,
        project_root: normalized_project_root.to_string_lossy().to_string(),
        entries,
    }
}

pub(super) async fn verify_remote_dependency_manifests(
    worker: &WorkerConfig,
    root_outcomes: &[(SyncClosurePlanEntry, SyncRootOutcome)],
    reporter: &HookReporter,
    source_identity: Option<&str>,
) -> anyhow::Result<()> {
    if should_skip_remote_preflight(worker) {
        reporter.verbose("[RCH] remote dependency preflight skipped in mock mode");
        return Ok(());
    }
    if root_outcomes.is_empty() {
        return Ok(());
    }

    let synced_checks = synced_dependency_preflight_checks(root_outcomes);

    let mut present_paths = std::collections::BTreeSet::new();
    let mut missing_paths = std::collections::BTreeSet::new();
    let mut probe_failure: Option<String> = None;

    if let Some(verify_cmd) = build_remote_dependency_preflight_command(&synced_checks) {
        let remote_command = source_identity
            .map(|identity| super::ssh::wrap_remote_source_activity("sh -s", identity))
            .transpose()?
            .unwrap_or_else(|| "sh -s".to_owned());
        // Stdin transport has no argv-size limit. Probe the entire closure in
        // one SSH session instead of paying a handshake and login-shell startup
        // for every 128 paths. Retain the former batches' total timeout budget.
        match run_offload_ssh_command_with_stdin(
            worker,
            &remote_command,
            verify_cmd.as_bytes(),
            dependency_preflight_timeout(synced_checks.len()),
        )
        .await
        {
            Ok(output) => {
                let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
                let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
                let (present, missing) = parse_dependency_preflight_probe_output(&stdout);
                // Do not publish positive evidence from a failed SSH session.
                // Even complete stdout can arrive before a transport failure;
                // otherwise the report can say verified despite probe_failure.
                let expected_missing_exit = output.status.code() == Some(43) && !missing.is_empty();
                if output.status.success() || expected_missing_exit {
                    present_paths.extend(present);
                    missing_paths.extend(missing);
                } else {
                    probe_failure = Some(format!(
                        "probe exited with status {:?}; stdout='{}'; stderr='{}'",
                        output.status.code(),
                        stdout,
                        stderr
                    ));
                }
            }
            Err(err) => {
                probe_failure = Some(err.to_string());
            }
        }
    }

    let report = build_dependency_preflight_report(
        worker,
        root_outcomes,
        &present_paths,
        &missing_paths,
        probe_failure.as_deref(),
    );
    let report_json = serde_json::to_string(&report).unwrap_or_else(|err| {
        format!(
            "{{\"schema_version\":\"{}\",\"verified\":false,\"reason_code\":\"{}\",\"serialization_error\":\"{}\"}}",
            DEPENDENCY_PREFLIGHT_SCHEMA_VERSION, DEPENDENCY_PREFLIGHT_CODE_UNKNOWN, err
        )
    });
    reporter.verbose(&format!(
        "[RCH] dependency preflight report: {}",
        report_json
    ));
    if report.verified {
        reporter.verbose(&format!(
            "[RCH] remote dependency preflight verified {} roots on {}",
            report.evidence.len(),
            worker.id
        ));
        return Ok(());
    }

    let failure = DependencyPreflightFailure::from_report(report);
    let evidence_summary = failure.evidence_summary();
    warn!(
        "Remote dependency preflight blocked remote execution on {} [{}] remediation='{}' evidence='{}'",
        worker.id, failure.reason_code, failure.remediation, evidence_summary
    );
    reporter.verbose(&format!(
        "[RCH] dependency preflight remediation [{}]: {}",
        failure.reason_code, failure.remediation
    ));
    Err(failure.into())
}

pub(super) fn build_remote_dependency_preflight_command(
    checks: &[DependencyPreflightCheck],
) -> Option<String> {
    if checks.is_empty() {
        return None;
    }

    let commands = checks
        .iter()
        .map(|check| {
            let escaped = shell_escape::escape(check.required_path.clone().into());
            escaped.to_string()
        })
        .collect::<Vec<_>>()
        .join(" ");

    Some(format!(
        "missing=0; for required in {commands}; do if [ -f \"$required\" ]; then printf 'RCH_DEP_PRESENT:%s\\n' \"$required\"; else printf 'RCH_DEP_MISSING:%s\\n' \"$required\"; missing=1; fi; done; if [ \"$missing\" -ne 0 ]; then exit 43; fi; echo RCH_REMOTE_DEPENDENCIES_OK"
    ))
}

pub(super) fn dependency_preflight_timeout(check_count: usize) -> Duration {
    let windows = check_count
        .div_ceil(DEPENDENCY_PREFLIGHT_PATHS_PER_TIMEOUT_WINDOW)
        .max(1);
    Duration::from_secs(20).saturating_mul(u32::try_from(windows).unwrap_or(u32::MAX))
}

#[cfg(all(test, unix))]
mod owned_layout_tests {
    use super::*;
    use rch_common::cargo_path_deps::{
        SelectedCargoEntry, validate_selected_cargo_path, validate_selected_cargo_tree_at,
    };
    use rch_common::test_guard;
    use std::collections::BTreeMap;
    use std::fs;

    const POOL: &str = ".rch-target-worker-pool-0123456789abcdef0123456789abcdef";

    fn fixture() -> (
        tempfile::TempDir,
        PathTopologyPolicy,
        Vec<SyncClosurePlanEntry>,
    ) {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("projects");
        fs::create_dir_all(root.join("app/src")).unwrap();
        fs::create_dir_all(root.join("foreign/crates/member/src")).unwrap();
        let root = fs::canonicalize(root).unwrap();
        let policy = PathTopologyPolicy::new(root.clone(), root.clone());
        fs::write(
            root.join("foreign/Cargo.toml"),
            "[workspace]\nmembers = [\"crates/member\"]\n[workspace.package]\nedition = \"2021\"\n",
        )
        .unwrap();
        let plan = build_sync_closure_plan(
            &[root.join("app"), root.join("foreign/crates/member")],
            &root.join("app"),
            "source-stamp-a",
            &policy,
        );
        (temp, policy, plan)
    }

    #[test]
    fn owned_layout_preserves_siblings_workspace_metadata_and_manifest_provenance() {
        let _guard = test_guard!();
        let (temp, policy, plan) = fixture();
        let root = policy.canonical_root();
        let files = [
            (
                "app/Cargo.toml",
                "[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2021\"\n[dependencies]\nmember = { path = \"../foreign/crates/member\" }\n",
            ),
            (
                "app/src/lib.rs",
                "pub fn value() -> u32 { member::value() }\n",
            ),
            (
                "foreign/Cargo.toml",
                "[workspace]\nmembers = [\"crates/member\"]\n[workspace.package]\nedition = \"2021\"\n",
            ),
            (
                "foreign/crates/member/Cargo.toml",
                "[package]\nname = \"member\"\nversion = \"0.1.0\"\nedition.workspace = true\n",
            ),
            (
                "foreign/crates/member/src/lib.rs",
                "pub fn value() -> u32 { 7 }\n",
            ),
        ];
        for (relative, contents) in files {
            fs::write(root.join(relative), contents).unwrap();
        }
        fs::create_dir_all(root.join("app/.git")).unwrap();
        fs::write(root.join("app/.git/HEAD"), "canonical-checkout-sentinel\n").unwrap();
        let base = temp.path().join("worker/staging");
        let layout =
            build_owned_sync_closure_layout(&plan, &policy, base.to_str().unwrap(), POOL).unwrap();
        assert_eq!(layout.plan.len(), 3);
        assert!(layout.plan.iter().any(|entry| {
            entry.mode == SyncClosureMode::WorkspaceMetadata
                && entry.remote_root == format!("{}/projects/foreign", layout.source_root)
        }));
        let mut selected = BTreeMap::new();
        for (relative, contents) in files {
            let local = root.join(relative);
            let entry = layout
                .plan
                .iter()
                .filter(|entry| local.starts_with(&entry.local_root))
                .max_by_key(|entry| entry.local_root.components().count())
                .unwrap();
            let destination =
                Path::new(&entry.remote_root).join(local.strip_prefix(&entry.local_root).unwrap());
            fs::create_dir_all(destination.parent().unwrap()).unwrap();
            fs::copy(&local, &destination).unwrap();
            assert_eq!(fs::read_to_string(&destination).unwrap(), contents);
            selected.insert(
                destination
                    .strip_prefix(&layout.source_root)
                    .unwrap()
                    .to_path_buf(),
                SelectedCargoEntry::File(
                    relative
                        .ends_with("Cargo.toml")
                        .then(|| contents.to_owned()),
                ),
            );
        }
        let selected_manifest = Path::new("projects/app/Cargo.toml");
        validate_selected_cargo_tree_at(&selected, selected_manifest).unwrap();
        assert_eq!(
            validate_selected_cargo_path(
                &selected,
                Path::new("projects/app/../foreign/crates/member/Cargo.toml"),
            )
            .unwrap(),
            Path::new("projects/foreign/crates/member/Cargo.toml")
        );
        let manifest = build_sync_closure_manifest(&layout.plan, &root.join("app"));
        for entry in &manifest.entries {
            let original = plan
                .iter()
                .find(|original| original.local_root == Path::new(&entry.local_root))
                .unwrap();
            assert_eq!(entry.root_hash, original.root_hash);
            assert_eq!(entry.project_id, original.project_id);
            assert_eq!(entry.mode, original.mode);
            assert_eq!(entry.is_primary, original.is_primary);
            assert!(entry.remote_root.starts_with(&layout.source_root));
        }
        assert_eq!(
            fs::read_to_string(root.join("app/.git/HEAD")).unwrap(),
            "canonical-checkout-sentinel\n"
        );
        assert!(
            !Path::new(&layout.source_root)
                .join("projects/app/.git")
                .exists()
        );
        selected.insert(
            selected_manifest.to_path_buf(),
            SelectedCargoEntry::File(Some(format!(
                "[package]\nname = \"app\"\nversion = \"0.1.0\"\n[dependencies]\nmember = {{ path = {:?} }}\n",
                root.join("foreign/crates/member").to_str().unwrap()
            ))),
        );
        assert!(validate_selected_cargo_tree_at(&selected, selected_manifest).is_err());
    }

    #[test]
    fn owned_layout_reuses_paths_across_source_edits_and_plan_order() {
        let _guard = test_guard!();
        let (_temp, policy, plan) = fixture();
        let first = build_owned_sync_closure_layout(&plan, &policy, "/tmp/rch", POOL).unwrap();
        let mut changed = plan.clone();
        changed.reverse();
        for entry in &mut changed {
            entry.root_hash = "source-stamp-b".into();
        }
        fs::write(
            policy.canonical_root().join("app/src/lib.rs"),
            "pub fn changed() {}\n",
        )
        .unwrap();
        let next = build_owned_sync_closure_layout(&changed, &policy, "/tmp//rch/", POOL).unwrap();
        assert_eq!(first.source_root, next.source_root);
        assert_eq!(first.pooled_target_name, next.pooled_target_name);
        assert_ne!(first.pooled_target_name, POOL);
        for (before, after) in first.plan.iter().zip(&next.plan) {
            assert_eq!(before.local_root, after.local_root);
            assert_eq!(before.remote_root, after.remote_root);
            assert_eq!(after.root_hash, "source-stamp-b");
        }
    }

    #[test]
    fn owned_layout_key_binds_target_dimensions_worker_base_and_source_projection() {
        let _guard = test_guard!();
        let (_temp, policy, plan) = fixture();
        let first = build_owned_sync_closure_layout(&plan, &policy, "/tmp/rch", POOL).unwrap();
        let other_pool = ".rch-target-worker-pool-fedcba9876543210fedcba9876543210";
        let changed_pool =
            build_owned_sync_closure_layout(&plan, &policy, "/tmp/rch", other_pool).unwrap();
        let changed_base =
            build_owned_sync_closure_layout(&plan, &policy, "/data/tmp/rch", POOL).unwrap();
        let mut selected = plan.clone();
        selected.retain(|entry| entry.is_primary);
        let changed_projection =
            build_owned_sync_closure_layout(&selected, &policy, "/tmp/rch", POOL).unwrap();
        for changed in [changed_pool, changed_base, changed_projection] {
            assert_ne!(first.source_root, changed.source_root);
            assert_ne!(first.pooled_target_name, changed.pooled_target_name);
        }
    }

    #[test]
    fn owned_layout_normalizes_aliases_and_a_symlinked_canonical_root() {
        let _guard = test_guard!();
        let (temp, policy, plan) = fixture();
        let configured = temp.path().join("configured-projects");
        let alias = temp.path().join("alias-projects");
        std::os::unix::fs::symlink(policy.canonical_root(), &configured).unwrap();
        std::os::unix::fs::symlink(policy.canonical_root(), &alias).unwrap();
        let aliased_policy = PathTopologyPolicy::new(configured, alias.clone());
        let canonical =
            build_owned_sync_closure_layout(&plan, &aliased_policy, "/tmp/rch", POOL).unwrap();
        let mut aliased = plan;
        for entry in &mut aliased {
            entry.local_root = alias.join(
                entry
                    .local_root
                    .strip_prefix(policy.canonical_root())
                    .unwrap(),
            );
        }
        let aliases =
            build_owned_sync_closure_layout(&aliased, &aliased_policy, "/tmp/rch", POOL).unwrap();
        assert_eq!(canonical, aliases);
    }

    #[test]
    fn owned_layout_rejects_ambiguous_authority_and_invalid_roots() {
        let _guard = test_guard!();
        let (temp, policy, plan) = fixture();
        for base in [
            "relative",
            "/tmp",
            "/tmp/./rch",
            "/tmp/rch/../other",
            "/tmp/rch\nother",
            "/tmp/rch\\other",
        ] {
            assert!(
                matches!(
                    build_owned_sync_closure_layout(&plan, &policy, base, POOL),
                    Err(OwnedSyncClosureLayoutError::InvalidBase(_))
                ),
                "{base:?}"
            );
        }
        for pool in [
            ".rch-target-worker-job-1",
            "../pool",
            ".rch-target-worker-pool-short",
            ".rch-target-unsafe/name-pool-0123456789abcdef0123456789abcdef",
        ] {
            assert!(
                matches!(
                    build_owned_sync_closure_layout(&plan, &policy, "/tmp/rch", pool),
                    Err(OwnedSyncClosureLayoutError::InvalidPoolName)
                ),
                "{pool:?}"
            );
        }
        assert!(matches!(
            build_owned_sync_closure_layout(&[], &policy, "/tmp/rch", POOL),
            Err(OwnedSyncClosureLayoutError::InvalidPlan(_))
        ));
        let mut invalid = plan.clone();
        invalid
            .iter_mut()
            .find(|entry| entry.is_primary)
            .unwrap()
            .local_root = temp.path().to_path_buf();
        assert!(matches!(
            build_owned_sync_closure_layout(&invalid, &policy, "/tmp/rch", POOL),
            Err(OwnedSyncClosureLayoutError::InvalidRoot { .. })
        ));
        let mut duplicate = plan.clone();
        let mut extra = duplicate[0].clone();
        extra.is_primary = false;
        duplicate.push(extra);
        assert!(matches!(
            build_owned_sync_closure_layout(&duplicate, &policy, "/tmp/rch", POOL),
            Err(OwnedSyncClosureLayoutError::OverlappingRoots { .. })
        ));
        let mut overlapping = plan.clone();
        overlapping
            .iter_mut()
            .find(|entry| entry.mode == SyncClosureMode::WorkspaceMetadata)
            .unwrap()
            .mode = SyncClosureMode::Full;
        assert!(matches!(
            build_owned_sync_closure_layout(&overlapping, &policy, "/tmp/rch", POOL),
            Err(OwnedSyncClosureLayoutError::OverlappingRoots { .. })
        ));
    }

    #[test]
    fn owned_layout_handles_a_primary_at_the_topology_root() {
        let _guard = test_guard!();
        let (_temp, policy, _) = fixture();
        let plan = build_sync_closure_plan(&[], policy.canonical_root(), "topology-root", &policy);
        let layout = build_owned_sync_closure_layout(&plan, &policy, "/tmp/rch", POOL).unwrap();
        assert_eq!(layout.plan.len(), 1);
        assert_eq!(
            layout.plan[0].remote_root,
            format!("{}/projects", layout.source_root)
        );
    }
}

#[cfg(all(test, unix))]
mod stdin_tests {
    use super::*;
    use rch_common::test_guard;

    #[tokio::test]
    async fn dependency_preflight_streams_large_batches() {
        use std::os::unix::fs::PermissionsExt as _;

        const DIR_ENV: &str = "RCH_DEPENDENCY_STDIN_TEST_DIR";
        const ROOT_COUNT: usize = 128;
        let _guard = test_guard!();
        if let Some(dir) = std::env::var_os(DIR_ENV) {
            let dir = PathBuf::from(dir);
            let project = dir.join("project with ' quote");
            let worker = WorkerConfig {
                id: WorkerId::new("dependency-stdin-test"),
                host: "dependency-stdin.invalid".into(),
                user: "ubuntu".into(),
                ..WorkerConfig::default()
            };
            assert!(!should_skip_remote_preflight(&worker));
            let outcomes: Vec<_> = (0..ROOT_COUNT)
                .map(|index| {
                    let remote_root = if index == ROOT_COUNT - 1 {
                        project.clone()
                    } else {
                        let mut root = dir.join(format!("absent-{index}"));
                        for _ in 0..20 {
                            root.push("r".repeat(96));
                        }
                        root
                    };
                    (
                        SyncClosurePlanEntry {
                            local_root: project.clone(),
                            remote_root: remote_root.to_string_lossy().into_owned(),
                            project_id: format!("fixture-{index}"),
                            root_hash: "fixture".into(),
                            is_primary: index == ROOT_COUNT - 1,
                            mode: SyncClosureMode::Full,
                        },
                        SyncRootOutcome::Synced,
                    )
                })
                .collect();
            let checks = synced_dependency_preflight_checks(&outcomes);
            assert_eq!(checks.len(), ROOT_COUNT * 2);
            let command = build_remote_dependency_preflight_command(&checks).unwrap();
            assert!(command.len() > 256 * 1024);

            let reporter = HookReporter::new(OutputVisibility::Summary);
            let error = verify_remote_dependency_manifests(&worker, &outcomes, &reporter, None)
                .await
                .expect_err("absent fixture paths must remain missing, not pass preflight");
            let report = error
                .downcast::<DependencyPreflightFailure>()
                .expect("typed dependency failure")
                .report;
            assert!(!report.verified);
            assert_eq!(report.evidence.len(), ROOT_COUNT * 2);
            assert_eq!(
                report
                    .evidence
                    .iter()
                    .filter(|item| item.status == DependencyPreflightStatus::Missing)
                    .count(),
                (ROOT_COUNT - 1) * 2,
                "E2BIG must not turn missing files into unknown transport failures"
            );
            assert_eq!(
                report
                    .evidence
                    .iter()
                    .filter(|item| item.status == DependencyPreflightStatus::Present)
                    .count(),
                2,
                "the final root proves the entire stdin script was processed"
            );
            assert_eq!(
                std::fs::read_to_string(dir.join("ssh-calls")).unwrap(),
                "probe\n",
                "oversized multi-root checks must use exactly one SSH connection"
            );
            verify_remote_dependency_manifests(
                &worker,
                &outcomes[ROOT_COUNT - 1..],
                &reporter,
                None,
            )
            .await
            .expect("healthy paths must still pass preflight");
            // SSH can forward every present line and still exit unsuccessfully.
            // None of those lines may certify a failed batch as verified.
            for code in [1, 42, 43, 255] {
                std::fs::write(dir.join("exit-after-output"), code.to_string()).unwrap();
                let failure = verify_remote_dependency_manifests(
                    &worker,
                    &outcomes[ROOT_COUNT - 1..],
                    &reporter,
                    None,
                )
                .await
                .expect_err("failed transport must not verify complete positive output")
                .downcast::<DependencyPreflightFailure>()
                .expect("typed transport verification failure");
                assert_eq!(failure.reason_code, DEPENDENCY_PREFLIGHT_CODE_UNKNOWN);
                assert!(!failure.report.verified);
                assert_eq!(failure.report.evidence.len(), 2);
                assert!(failure.report.evidence.iter().all(|item| {
                    item.status == DependencyPreflightStatus::Unknown
                        && item.detail.contains(&format!("status Some({code})"))
                }));
            }
            std::fs::write(dir.join("finished"), "ok").unwrap();
            return;
        }

        let dir = tempfile::tempdir().unwrap();
        let project = dir.path().join("project with ' quote");
        std::fs::create_dir_all(project.join("src")).unwrap();
        std::fs::write(
            project.join("Cargo.toml"),
            "[package]\nname = \"preflight-fixture\"\nversion = \"0.1.0\"\n",
        )
        .unwrap();
        std::fs::write(project.join("src/lib.rs"), "pub fn fixture() {}\n").unwrap();
        let bin = dir.path().join("bin");
        std::fs::create_dir(&bin).unwrap();
        let ssh = bin.join("ssh");
        std::fs::write(
            &ssh,
            r#"#!/bin/sh
set -eu
LC_ALL=C
export LC_ALL
printf 'probe\n' >> "$RCH_DEPENDENCY_STDIN_TEST_DIR/ssh-calls"
remote=''
for arg; do
    [ "${#arg}" -lt 131072 ] || exit 90
    remote=$arg
done
[ "$remote" = "sh -lc 'sh -s'" ] || exit 91
if [ -f "$RCH_DEPENDENCY_STDIN_TEST_DIR/exit-after-output" ]; then
    /bin/sh -c "$remote"
    exit "$(cat "$RCH_DEPENDENCY_STDIN_TEST_DIR/exit-after-output")"
fi
exec /bin/sh -c "$remote"
"#,
        )
        .unwrap();
        std::fs::set_permissions(&ssh, std::fs::Permissions::from_mode(0o755)).unwrap();
        let old_path = std::env::var_os("PATH").unwrap_or_default();
        let path =
            std::env::join_paths(std::iter::once(bin).chain(std::env::split_paths(&old_path)))
                .unwrap();
        let name = concat!(
            module_path!(),
            "::dependency_preflight_streams_large_batches"
        );
        let name = name.split_once("::").unwrap().1;
        let output = timeout(
            Duration::from_secs(60),
            Command::new(std::env::current_exe().unwrap())
                .args(["--exact", name, "--nocapture"])
                .env(DIR_ENV, dir.path())
                .env("PATH", path)
                .env("RCH_MOCK_SSH", "0")
                .kill_on_drop(true)
                .output(),
        )
        .await
        .expect("dependency stdin regression timed out")
        .expect("start isolated dependency stdin regression");
        assert!(
            output.status.success(),
            "stdout={} stderr={}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
        assert!(
            dir.path().join("finished").is_file(),
            "child test did not run"
        );
    }
}