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
//! Preflight checks for worker deployments.

use crate::fleet::ssh::SshExecutor;
use crate::ui::context::OutputContext;
use anyhow::Result;
use futures::stream::{self, StreamExt};
use rch_common::{FleetConfig, WorkerConfig};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use tracing::{debug, info, warn};

/// Maximum concurrent SSH queries to workers (prevents overwhelming network/SSH agent).
const MAX_CONCURRENT_QUERIES: usize = 10;

/// Default timeout for individual worker queries in seconds.
const DEFAULT_QUERY_TIMEOUT_SECS: u64 = 30;

/// Retry an async operation with configurable retry count and delay.
///
/// Uses exponential backoff between retries. Returns the last error if all attempts fail.
pub async fn with_retry<T, E, F, Fut>(
    config: &FleetConfig,
    operation: F,
) -> std::result::Result<T, E>
where
    F: Fn() -> Fut,
    Fut: std::future::Future<Output = std::result::Result<T, E>>,
    E: std::fmt::Display,
{
    let mut last_error = None;

    for attempt in 0..=config.retry_count {
        match operation().await {
            Ok(result) => return Ok(result),
            Err(e) => {
                if attempt < config.retry_count {
                    debug!(
                        attempt = %attempt,
                        max_retries = %config.retry_count,
                        error = %e,
                        delay_ms = %config.retry_delay_ms,
                        "Retrying after transient error"
                    );
                    tokio::time::sleep(config.retry_delay()).await;
                }
                last_error = Some(e);
            }
        }
    }

    Err(last_error.unwrap())
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Severity {
    Info,
    Warning,
    Error,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PreflightResult {
    pub ssh_ok: bool,
    pub disk_space_mb: u64,
    pub disk_ok: bool,
    pub rsync_ok: bool,
    pub zstd_ok: bool,
    pub rustup_ok: bool,
    pub current_version: Option<String>,
    pub issues: Vec<PreflightIssue>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PreflightIssue {
    pub severity: Severity,
    pub check: String,
    pub message: String,
    pub remediation: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkerStatus {
    pub worker_id: String,
    pub reachable: bool,
    pub healthy: bool,
    pub version: Option<String>,
    pub issues: Vec<String>,
}

pub async fn run_preflight(
    worker: &WorkerConfig,
    _ctx: &OutputContext,
    config: &FleetConfig,
) -> Result<PreflightResult> {
    let mut issues = Vec::new();

    // Log config values being used
    debug!(
        ssh_connect_timeout = config.ssh_connect_timeout_secs,
        ssh_command_timeout = config.ssh_command_timeout_secs,
        min_disk_space_mb = config.min_disk_space_mb,
        "Using fleet configuration for preflight"
    );

    // =========================================================================
    // SSH Connectivity Check
    // =========================================================================
    debug!(
        worker = %worker.id,
        host = %worker.host,
        "Checking SSH connectivity"
    );

    let ssh_executor = SshExecutor::new(worker).connect_timeout(config.ssh_connect_timeout());

    let ssh_result = ssh_executor.check_connectivity().await;

    let ssh_ok = match &ssh_result {
        Ok(connected) => {
            if *connected {
                info!(
                    worker = %worker.id,
                    ssh_ok = true,
                    "SSH connectivity check passed"
                );
                true
            } else {
                warn!(
                    worker = %worker.id,
                    host = %worker.host,
                    "SSH connectivity check returned false"
                );
                issues.push(PreflightIssue {
                    severity: Severity::Error,
                    check: "ssh".to_string(),
                    message: format!("SSH connection to {} failed", worker.host),
                    remediation: Some(format!(
                        "Check: 1) Host is reachable: ping {} 2) SSH service running 3) Key auth: ssh -i {} {}@{}",
                        worker.host, worker.identity_file, worker.user, worker.host
                    )),
                });
                false
            }
        }
        Err(e) => {
            warn!(
                worker = %worker.id,
                host = %worker.host,
                error = %e,
                "SSH connectivity check failed"
            );
            issues.push(PreflightIssue {
                severity: Severity::Error,
                check: "ssh".to_string(),
                message: format!("SSH connection to {} failed: {}", worker.host, e),
                remediation: Some(format!(
                    "Check: 1) Host is reachable: ping {} 2) SSH service running 3) Key auth: ssh -i {} {}@{}",
                    worker.host, worker.identity_file, worker.user, worker.host
                )),
            });
            false
        }
    };

    info!(
        worker = %worker.id,
        ssh_ok = %ssh_ok,
        "SSH connectivity check complete"
    );

    // =========================================================================
    // Disk Space Check
    // =========================================================================
    let (disk_space_mb, disk_ok) = if ssh_ok {
        debug!(worker = %worker.id, "Checking disk space");

        // Portable df command that works on Linux and macOS
        // -P: POSIX format (consistent columns)
        // -m: 1MB blocks
        // awk extracts available space (4th column)
        let disk_cmd = "df -Pm /tmp 2>/dev/null | tail -1 | awk '{print $4}'";

        match ssh_executor.run_command(disk_cmd).await {
            Ok(output) => {
                if output.success() {
                    match output.stdout.trim().parse::<u64>() {
                        Ok(mb) => {
                            let ok = mb >= config.min_disk_space_mb;
                            if !ok {
                                warn!(
                                    worker = %worker.id,
                                    disk_mb = %mb,
                                    threshold_mb = %config.min_disk_space_mb,
                                    "Low disk space warning"
                                );
                                issues.push(PreflightIssue {
                                    severity: Severity::Warning,
                                    check: "disk_space".to_string(),
                                    message: format!(
                                        "Low disk space: {}MB available, {}MB required",
                                        mb, config.min_disk_space_mb
                                    ),
                                    remediation: Some(format!(
                                        "Free up disk space on {}: rm -rf /tmp/rch-* or expand volume",
                                        worker.host
                                    )),
                                });
                            }
                            info!(
                                worker = %worker.id,
                                disk_mb = %mb,
                                threshold_mb = %config.min_disk_space_mb,
                                ok = %ok,
                                "Disk space check complete"
                            );
                            (mb, ok)
                        }
                        Err(e) => {
                            warn!(
                                worker = %worker.id,
                                error = %e,
                                raw = %output.stdout.trim(),
                                "Failed to parse disk space output"
                            );
                            issues.push(PreflightIssue {
                                severity: Severity::Warning,
                                check: "disk_space".to_string(),
                                message: format!("Could not parse disk space: {}", e),
                                remediation: None,
                            });
                            (0, false)
                        }
                    }
                } else {
                    warn!(
                        worker = %worker.id,
                        exit_code = %output.exit_code,
                        stderr = %output.stderr.trim(),
                        "Disk space command failed"
                    );
                    issues.push(PreflightIssue {
                        severity: Severity::Warning,
                        check: "disk_space".to_string(),
                        message: format!(
                            "Disk space check failed with exit code {}",
                            output.exit_code
                        ),
                        remediation: None,
                    });
                    (0, false)
                }
            }
            Err(e) => {
                warn!(
                    worker = %worker.id,
                    error = %e,
                    "Disk space check failed"
                );
                issues.push(PreflightIssue {
                    severity: Severity::Warning,
                    check: "disk_space".to_string(),
                    message: format!("Disk space check failed: {}", e),
                    remediation: None,
                });
                (0, false)
            }
        }
    } else {
        // SSH failed, can't check disk
        debug!(worker = %worker.id, "Skipping disk space check - SSH not available");
        (0, false)
    };

    // =========================================================================
    // rsync/zstd Availability Check
    // =========================================================================
    let (rsync_ok, zstd_ok) = if ssh_ok {
        debug!(worker = %worker.id, "Checking rsync availability");

        // Check rsync using POSIX-compliant `command -v`
        let rsync_result = ssh_executor.run_command("command -v rsync").await;
        let rsync_ok = rsync_result.as_ref().map(|o| o.success()).unwrap_or(false);

        if !rsync_ok {
            warn!(worker = %worker.id, tool = "rsync", "Required tool not found");
            issues.push(PreflightIssue {
                severity: Severity::Error,
                check: "rsync".to_string(),
                message: "rsync is not installed or not in PATH".to_string(),
                remediation: Some(format!(
                    "Install rsync on {}: sudo apt install rsync (Debian/Ubuntu) or sudo yum install rsync (RHEL/CentOS)",
                    worker.host
                )),
            });
        } else {
            debug!(worker = %worker.id, "rsync found");
            // Optionally collect version for diagnostics
            if let Ok(output) = ssh_executor
                .run_command("rsync --version 2>/dev/null | head -1")
                .await
                && output.success()
                && !output.stdout.trim().is_empty()
            {
                debug!(
                    worker = %worker.id,
                    version = %output.stdout.trim(),
                    "rsync version"
                );
            }
        }

        debug!(worker = %worker.id, "Checking zstd availability");

        // Check zstd using POSIX-compliant `command -v`
        let zstd_result = ssh_executor.run_command("command -v zstd").await;
        let zstd_ok = zstd_result.as_ref().map(|o| o.success()).unwrap_or(false);

        if !zstd_ok {
            warn!(worker = %worker.id, tool = "zstd", "Required tool not found");
            issues.push(PreflightIssue {
                severity: Severity::Error,
                check: "zstd".to_string(),
                message: "zstd is not installed or not in PATH".to_string(),
                remediation: Some(format!(
                    "Install zstd on {}: sudo apt install zstd (Debian/Ubuntu) or sudo yum install zstd (RHEL/CentOS)",
                    worker.host
                )),
            });
        } else {
            debug!(worker = %worker.id, "zstd found");
            // Optionally collect version for diagnostics
            if let Ok(output) = ssh_executor.run_command("zstd --version 2>/dev/null").await
                && output.success()
                && !output.stdout.trim().is_empty()
            {
                debug!(
                    worker = %worker.id,
                    version = %output.stdout.trim(),
                    "zstd version"
                );
            }
        }

        info!(
            worker = %worker.id,
            rsync = %rsync_ok,
            zstd = %zstd_ok,
            "Tool availability check complete"
        );

        (rsync_ok, zstd_ok)
    } else {
        // SSH failed, can't check tools
        debug!(
            worker = %worker.id,
            "Skipping rsync/zstd check - SSH not available"
        );
        (false, false)
    };

    // =========================================================================
    // rustup Check and Version Detection
    // =========================================================================
    let (rustup_ok, current_version) = if ssh_ok {
        debug!(worker = %worker.id, "Checking rustup availability");

        // Check rustup using POSIX-compliant `command -v`
        let rustup_result = ssh_executor.run_command("command -v rustup").await;
        let rustup_ok = rustup_result.as_ref().map(|o| o.success()).unwrap_or(false);

        if !rustup_ok {
            warn!(worker = %worker.id, tool = "rustup", "Required tool not found");
            issues.push(PreflightIssue {
                severity: Severity::Error,
                check: "rustup".to_string(),
                message: "rustup is not installed or not in PATH".to_string(),
                remediation: Some(format!(
                    "Install rustup on {}: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh",
                    worker.host
                )),
            });
        } else {
            debug!(worker = %worker.id, "rustup found");
            // Check rustc version for diagnostics
            if let Ok(output) = ssh_executor
                .run_command("rustc --version 2>/dev/null")
                .await
                && output.success()
                && !output.stdout.trim().is_empty()
            {
                debug!(
                    worker = %worker.id,
                    version = %output.stdout.trim(),
                    "rustc version"
                );
            }
        }

        // Check rch-wkr version if rustup is available (worker can compile)
        debug!(worker = %worker.id, "Checking rch-wkr version");
        let version = if let Ok(output) = ssh_executor
            .run_command(
                "~/.local/bin/rch-wkr --version 2>/dev/null || rch-wkr --version 2>/dev/null",
            )
            .await
        {
            if output.success() && !output.stdout.trim().is_empty() {
                let version_str = output.stdout.trim();
                // Extract version number (e.g., "rch-wkr 1.0.0" -> "1.0.0")
                let version = version_str.split_whitespace().last().map(|s| s.to_string());
                info!(
                    worker = %worker.id,
                    version = ?version,
                    "rch-wkr version detected"
                );
                version
            } else {
                debug!(worker = %worker.id, "rch-wkr not installed (will be deployed)");
                None
            }
        } else {
            debug!(worker = %worker.id, "rch-wkr not installed (will be deployed)");
            None
        };

        info!(
            worker = %worker.id,
            rustup = %rustup_ok,
            rch_wkr_version = ?version,
            "rustup and rch-wkr check complete"
        );

        (rustup_ok, version)
    } else {
        // SSH failed, can't check rustup
        debug!(
            worker = %worker.id,
            "Skipping rustup/rch-wkr check - SSH not available"
        );
        (false, None)
    };

    Ok(PreflightResult {
        ssh_ok,
        disk_space_mb,
        disk_ok,
        rsync_ok,
        zstd_ok,
        rustup_ok,
        current_version,
        issues,
    })
}

/// Query a single worker's status with timeout handling.
///
/// This function is designed to be called concurrently for multiple workers.
/// It handles individual worker timeouts gracefully, returning a WorkerStatus
/// with reachable=false if the query times out.
async fn query_single_worker(
    worker: &WorkerConfig,
    config: &FleetConfig,
    timeout: Duration,
) -> WorkerStatus {
    debug!(
        worker = %worker.id,
        host = %worker.host,
        timeout_ms = %timeout.as_millis(),
        "Querying worker status"
    );

    // Wrap the entire query in a timeout
    match tokio::time::timeout(timeout, query_worker_inner(worker, config)).await {
        Ok(status) => {
            debug!(
                worker = %worker.id,
                reachable = %status.reachable,
                healthy = %status.healthy,
                version = ?status.version,
                issues = ?status.issues,
                "Worker query completed"
            );
            status
        }
        Err(_) => {
            warn!(
                worker = %worker.id,
                host = %worker.host,
                timeout_ms = %timeout.as_millis(),
                "Worker query timed out"
            );
            WorkerStatus {
                worker_id: worker.id.0.clone(),
                reachable: false,
                healthy: false,
                version: None,
                issues: vec![format!("Query timed out after {}s", timeout.as_secs())],
            }
        }
    }
}

/// Inner worker query logic (without timeout wrapper).
async fn query_worker_inner(worker: &WorkerConfig, config: &FleetConfig) -> WorkerStatus {
    let ssh_executor = SshExecutor::new(worker).connect_timeout(config.ssh_connect_timeout());

    // Check SSH connectivity
    let reachable = ssh_executor.check_connectivity().await.unwrap_or(false);

    let mut issues = Vec::new();
    let mut version = None;
    let mut healthy = reachable;

    if reachable {
        // Check rch-wkr version
        if let Ok(output) = ssh_executor
            .run_command(
                "~/.local/bin/rch-wkr --version 2>/dev/null || rch-wkr --version 2>/dev/null",
            )
            .await
            && output.success()
            && !output.stdout.trim().is_empty()
        {
            // split_whitespace() already handles leading/trailing whitespace
            version = output
                .stdout
                .split_whitespace()
                .last()
                .map(|s| s.to_string());
        }

        // Check disk space
        let disk_cmd = "df -Pm /tmp 2>/dev/null | tail -1 | awk '{print $4}'";
        if let Ok(output) = ssh_executor.run_command(disk_cmd).await
            && output.success()
            && let Ok(mb) = output.stdout.trim().parse::<u64>()
            && mb < config.min_disk_space_mb
        {
            issues.push(format!("Low disk space: {}MB", mb));
            healthy = false;
        }
    } else {
        issues.push("Unreachable".to_string());
    }

    WorkerStatus {
        worker_id: worker.id.0.clone(),
        reachable,
        healthy,
        version,
        issues,
    }
}

/// Query fleet status for all workers with parallel execution.
///
/// Queries all workers concurrently with bounded parallelism to prevent
/// overwhelming the network or SSH agent. Each worker query has an individual
/// timeout to ensure fast overall completion even if some workers are slow.
///
/// # Arguments
///
/// * `workers` - Slice of worker configurations to query
/// * `ctx` - Output context for progress display
/// * `config` - Fleet configuration with timeouts and thresholds
///
/// # Returns
///
/// A vector of `WorkerStatus` for each worker, in the same order as input.
/// Workers that timeout or fail are marked as unreachable with appropriate issues.
pub async fn get_fleet_status(
    workers: &[&WorkerConfig],
    ctx: &OutputContext,
    config: &FleetConfig,
) -> Result<Vec<WorkerStatus>> {
    use indicatif::{ProgressBar, ProgressStyle};
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    // Handle empty workers case
    if workers.is_empty() {
        debug!("No workers to query");
        return Ok(vec![]);
    }

    // Determine concurrency limit (use config or default)
    let max_concurrent = config.max_concurrent_workers.min(MAX_CONCURRENT_QUERIES);
    let query_timeout = Duration::from_secs(
        config
            .ssh_command_timeout_secs
            .min(DEFAULT_QUERY_TIMEOUT_SECS),
    );

    // Log config values being used
    info!(
        workers = %workers.len(),
        max_concurrent = %max_concurrent,
        ssh_timeout = %config.ssh_connect_timeout_secs,
        query_timeout_secs = %query_timeout.as_secs(),
        "Starting parallel fleet status check"
    );

    // Human-readable progress output (non-JSON/non-quiet mode)
    if !ctx.is_json() && !ctx.is_quiet() {
        eprintln!("Querying {} workers...", workers.len());
    }

    // Create progress bar for multi-worker operations (non-JSON/non-quiet mode only)
    let progress_bar = if !ctx.is_json() && !ctx.is_quiet() && workers.len() > 1 {
        let pb = ProgressBar::new(workers.len() as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{spinner:.green} [{bar:40}] {pos}/{len} workers ({eta})")
                .unwrap_or_else(|_| ProgressStyle::default_bar())
                .progress_chars("=>-"),
        );
        Some(Arc::new(pb))
    } else {
        None
    };

    // Capture verbose flag for worker-level output
    let show_per_worker = ctx.is_verbose() && !ctx.is_json() && !ctx.is_quiet();

    let start = std::time::Instant::now();
    let completed = Arc::new(AtomicUsize::new(0));

    // Clone config for use in async closures
    let config = config.clone();

    // Query all workers in parallel with bounded concurrency
    // Note: We collect into Vec<(usize, WorkerStatus)> to preserve order
    let results: Vec<(usize, WorkerStatus)> = stream::iter(workers.iter().enumerate())
        .map(|(idx, worker)| {
            let config = config.clone();
            let pb = progress_bar.clone();
            let completed = completed.clone();

            async move {
                let status = query_single_worker(worker, &config, query_timeout).await;

                // Update progress
                let done = completed.fetch_add(1, Ordering::Relaxed) + 1;
                if let Some(ref pb) = pb {
                    pb.set_position(done as u64);
                }

                (idx, status)
            }
        })
        .buffer_unordered(max_concurrent)
        .collect()
        .await;

    // Sort by original index to preserve order
    let results: Vec<WorkerStatus> = {
        let mut sorted = results;
        sorted.sort_by_key(|(idx, _)| *idx);
        sorted.into_iter().map(|(_, status)| status).collect()
    };

    // Finish progress bar
    if let Some(pb) = progress_bar {
        pb.finish_with_message("Done");
    }

    // Log summary
    let duration = start.elapsed();
    let reachable_count = results.iter().filter(|r| r.reachable).count();
    let healthy_count = results.iter().filter(|r| r.healthy).count();
    let unreachable_count = results.len() - reachable_count;

    info!(
        total = %results.len(),
        reachable = %reachable_count,
        healthy = %healthy_count,
        unreachable = %unreachable_count,
        duration_ms = %duration.as_millis(),
        "Fleet status query complete"
    );

    // Verbose per-worker status output
    if show_per_worker {
        eprintln!("\nPer-worker status:");
        for r in &results {
            let icon = if r.healthy {
                "✓"
            } else if r.reachable {
                "âš "
            } else {
                "✗"
            };
            let status_text = if r.healthy {
                "healthy"
            } else if r.reachable {
                "issues found"
            } else {
                "unreachable"
            };
            eprintln!(
                "  {} {} - {}{}",
                icon,
                r.worker_id,
                status_text,
                r.version
                    .as_ref()
                    .map(|v| format!(" (v{})", v))
                    .unwrap_or_default()
            );
            if !r.issues.is_empty() {
                for issue in &r.issues {
                    eprintln!("      → {}", issue);
                }
            }
        }
    }

    // Human-readable summary (non-JSON mode)
    if !ctx.is_json() && !ctx.is_quiet() && unreachable_count > 0 {
        eprintln!("Warning: {} worker(s) unreachable", unreachable_count);
    }

    Ok(results)
}

/// Minimum disk space required in MB.
#[allow(dead_code)] // Reserved for bd-3029.2 preflight disk check
pub const MIN_DISK_SPACE_MB: u64 = 500;

/// SSH connectivity check timeout in seconds (fast fail for preflight).
#[allow(dead_code)] // Available as fallback default
pub const SSH_CONNECT_TIMEOUT_SECS: u64 = 5;

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

    // ========================
    // Severity tests
    // ========================

    #[test]
    fn severity_info_serializes() {
        let sev = Severity::Info;
        let json = serde_json::to_string(&sev).unwrap();
        assert_eq!(json, "\"Info\"");
    }

    #[test]
    fn severity_warning_serializes() {
        let sev = Severity::Warning;
        let json = serde_json::to_string(&sev).unwrap();
        assert_eq!(json, "\"Warning\"");
    }

    #[test]
    fn severity_error_serializes() {
        let sev = Severity::Error;
        let json = serde_json::to_string(&sev).unwrap();
        assert_eq!(json, "\"Error\"");
    }

    #[test]
    fn severity_ordering() {
        assert!(Severity::Info < Severity::Warning);
        assert!(Severity::Warning < Severity::Error);
        assert!(Severity::Info < Severity::Error);
    }

    #[test]
    fn severity_equality() {
        assert_eq!(Severity::Info, Severity::Info);
        assert_eq!(Severity::Warning, Severity::Warning);
        assert_eq!(Severity::Error, Severity::Error);
        assert_ne!(Severity::Info, Severity::Warning);
    }

    #[test]
    fn severity_deserializes() {
        let json = "\"Warning\"";
        let sev: Severity = serde_json::from_str(json).unwrap();
        assert_eq!(sev, Severity::Warning);
    }

    // ========================
    // PreflightResult tests
    // ========================

    #[test]
    fn preflight_result_default() {
        let result = PreflightResult::default();
        assert!(!result.ssh_ok);
        assert_eq!(result.disk_space_mb, 0);
        assert!(!result.disk_ok);
        assert!(!result.rsync_ok);
        assert!(!result.zstd_ok);
        assert!(!result.rustup_ok);
        assert!(result.current_version.is_none());
        assert!(result.issues.is_empty());
    }

    #[test]
    fn preflight_result_serializes() {
        let result = PreflightResult {
            ssh_ok: true,
            disk_space_mb: 10000,
            disk_ok: true,
            rsync_ok: true,
            zstd_ok: true,
            rustup_ok: false,
            current_version: Some("1.0.0".to_string()),
            issues: vec![PreflightIssue {
                severity: Severity::Warning,
                check: "rustup".to_string(),
                message: "rustup not found".to_string(),
                remediation: Some("Install rustup".to_string()),
            }],
        };
        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("\"ssh_ok\":true"));
        assert!(json.contains("10000"));
        assert!(json.contains("1.0.0"));
        assert!(json.contains("rustup not found"));
    }

    #[test]
    fn preflight_result_deserializes_roundtrip() {
        let result = PreflightResult {
            ssh_ok: true,
            disk_space_mb: 5000,
            disk_ok: true,
            rsync_ok: true,
            zstd_ok: true,
            rustup_ok: true,
            current_version: Some("2.0.0".to_string()),
            issues: vec![],
        };
        let json = serde_json::to_string(&result).unwrap();
        let restored: PreflightResult = serde_json::from_str(&json).unwrap();
        assert!(restored.ssh_ok);
        assert_eq!(restored.disk_space_mb, 5000);
        assert!(restored.disk_ok);
        assert_eq!(restored.current_version, Some("2.0.0".to_string()));
    }

    // ========================
    // PreflightIssue tests
    // ========================

    #[test]
    fn preflight_issue_serializes() {
        let issue = PreflightIssue {
            severity: Severity::Error,
            check: "disk_space".to_string(),
            message: "Insufficient disk space".to_string(),
            remediation: Some("Free up at least 500MB".to_string()),
        };
        let json = serde_json::to_string(&issue).unwrap();
        assert!(json.contains("Error"));
        assert!(json.contains("disk_space"));
        assert!(json.contains("Insufficient disk space"));
        assert!(json.contains("Free up at least 500MB"));
    }

    #[test]
    fn preflight_issue_without_remediation_serializes() {
        let issue = PreflightIssue {
            severity: Severity::Info,
            check: "version".to_string(),
            message: "Current version is up to date".to_string(),
            remediation: None,
        };
        let json = serde_json::to_string(&issue).unwrap();
        assert!(json.contains("Info"));
        assert!(json.contains("version"));
        assert!(json.contains("null") || json.contains("remediation\":null"));
    }

    #[test]
    fn preflight_issue_deserializes_roundtrip() {
        let issue = PreflightIssue {
            severity: Severity::Warning,
            check: "ssh".to_string(),
            message: "SSH key not found".to_string(),
            remediation: Some("Generate SSH key".to_string()),
        };
        let json = serde_json::to_string(&issue).unwrap();
        let restored: PreflightIssue = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.severity, Severity::Warning);
        assert_eq!(restored.check, "ssh");
        assert_eq!(restored.message, "SSH key not found");
        assert_eq!(restored.remediation, Some("Generate SSH key".to_string()));
    }

    // ========================
    // WorkerStatus tests
    // ========================

    #[test]
    fn worker_status_serializes() {
        let status = WorkerStatus {
            worker_id: "worker-1".to_string(),
            reachable: true,
            healthy: true,
            version: Some("1.0.0".to_string()),
            issues: vec!["minor warning".to_string()],
        };
        let json = serde_json::to_string(&status).unwrap();
        assert!(json.contains("worker-1"));
        assert!(json.contains("\"reachable\":true"));
        assert!(json.contains("\"healthy\":true"));
        assert!(json.contains("1.0.0"));
        assert!(json.contains("minor warning"));
    }

    #[test]
    fn worker_status_without_version_serializes() {
        let status = WorkerStatus {
            worker_id: "worker-2".to_string(),
            reachable: false,
            healthy: false,
            version: None,
            issues: vec!["Connection refused".to_string()],
        };
        let json = serde_json::to_string(&status).unwrap();
        assert!(json.contains("worker-2"));
        assert!(json.contains("\"reachable\":false"));
        assert!(json.contains("\"healthy\":false"));
        assert!(json.contains("Connection refused"));
    }

    #[test]
    fn worker_status_deserializes_roundtrip() {
        let status = WorkerStatus {
            worker_id: "test-worker".to_string(),
            reachable: true,
            healthy: false,
            version: Some("3.0.0".to_string()),
            issues: vec!["issue1".to_string(), "issue2".to_string()],
        };
        let json = serde_json::to_string(&status).unwrap();
        let restored: WorkerStatus = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.worker_id, "test-worker");
        assert!(restored.reachable);
        assert!(!restored.healthy);
        assert_eq!(restored.version, Some("3.0.0".to_string()));
        assert_eq!(restored.issues.len(), 2);
    }

    // ========================
    // MIN_DISK_SPACE_MB constant test
    // ========================

    #[test]
    fn min_disk_space_constant() {
        assert_eq!(MIN_DISK_SPACE_MB, 500);
    }

    // ========================
    // Parallel execution constants
    // ========================

    #[test]
    fn max_concurrent_queries_constant() {
        assert_eq!(MAX_CONCURRENT_QUERIES, 10);
    }

    #[test]
    fn default_query_timeout_constant() {
        assert_eq!(DEFAULT_QUERY_TIMEOUT_SECS, 30);
    }

    // ========================
    // get_fleet_status tests
    // ========================

    #[tokio::test]
    async fn test_fleet_status_empty_workers_returns_empty() {
        use crate::ui::test_utils::OutputCapture;
        use rch_common::FleetConfig;

        let capture = OutputCapture::json();
        let ctx = capture.context();
        let config = FleetConfig::default();
        let workers: Vec<&rch_common::WorkerConfig> = vec![];

        let result = get_fleet_status(&workers, &ctx, &config).await;
        assert!(result.is_ok());
        assert!(result.unwrap().is_empty());
    }

    // ========================
    // WorkerStatus creation tests
    // ========================

    #[test]
    fn worker_status_unreachable() {
        let status = WorkerStatus {
            worker_id: "test".to_string(),
            reachable: false,
            healthy: false,
            version: None,
            issues: vec!["Unreachable".to_string()],
        };
        assert!(!status.reachable);
        assert!(!status.healthy);
        assert!(status.version.is_none());
        assert!(!status.issues.is_empty());
    }

    #[test]
    fn worker_status_healthy() {
        let status = WorkerStatus {
            worker_id: "healthy-worker".to_string(),
            reachable: true,
            healthy: true,
            version: Some("1.0.0".to_string()),
            issues: vec![],
        };
        assert!(status.reachable);
        assert!(status.healthy);
        assert_eq!(status.version, Some("1.0.0".to_string()));
        assert!(status.issues.is_empty());
    }

    #[test]
    fn worker_status_reachable_but_unhealthy() {
        let status = WorkerStatus {
            worker_id: "degraded-worker".to_string(),
            reachable: true,
            healthy: false,
            version: Some("0.9.0".to_string()),
            issues: vec!["Low disk space: 100MB".to_string()],
        };
        assert!(status.reachable);
        assert!(!status.healthy);
        assert!(status.version.is_some());
        assert!(!status.issues.is_empty());
    }

    #[test]
    fn worker_status_timeout_issue() {
        let timeout_secs = 30;
        let status = WorkerStatus {
            worker_id: "slow-worker".to_string(),
            reachable: false,
            healthy: false,
            version: None,
            issues: vec![format!("Query timed out after {}s", timeout_secs)],
        };
        assert!(!status.reachable);
        assert!(!status.healthy);
        assert!(status.issues[0].contains("timed out"));
    }
}