rch 2.1.12

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
//! 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,
}

/// 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) {
            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()
}

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 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"
        );
    }
}