mothership 0.0.100

Process supervisor with HTTP exposure - wrap, monitor, and expose your fleet
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
//! Mothership - Process supervisor with HTTP exposure
//!
//! Usage:
//!   mothership                    # Load ship-manifest.toml from CWD
//!   mothership -c /path/to.toml   # Load specific manifest
//!   mothership validate           # Validate manifest
//!   mothership --tui              # Run with TUI dashboard

use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::{Notify, watch};
use tracing::{debug, info};
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};

use mothership::Lifecycle;
use mothership::charter::{Manifest, Vessel};
#[cfg(feature = "tokio-postgres")]
use mothership::flagship::{Coordinator, FlagshipRole, PostgresElection};
use mothership::fleet::{Fleet, run_prelaunch, set_suppress_stdout, verify_uplinks};
use mothership::http::{HttpExposure, MetricsRegistry, MetricsServer, set_global_metrics_registry};
#[cfg(feature = "tui")]
use mothership::tui::TuiApp;

/// Tag filter for selecting ships
#[derive(Debug, Clone, Default)]
pub struct TagFilter {
    /// Only run ships with these tags (OR logic)
    pub only: Vec<String>,
    /// Exclude ships with these tags
    pub except: Vec<String>,
}

impl TagFilter {
    /// Check if a ship's tags pass the filter
    pub fn matches(&self, tags: &[String]) -> bool {
        // If --except specified, reject ships with any of those tags
        if !self.except.is_empty() {
            for tag in tags {
                if self.except.contains(tag) {
                    return false;
                }
            }
        }

        // If --only specified, require at least one matching tag
        if !self.only.is_empty() {
            for tag in tags {
                if self.only.contains(tag) {
                    return true;
                }
            }
            return false;
        }

        true
    }

    pub fn is_empty(&self) -> bool {
        self.only.is_empty() && self.except.is_empty()
    }
}

/// Merge two vecs, flattening nested values from ArgAction::Append
fn merge_vecs(a: Vec<String>, b: Vec<String>) -> Vec<String> {
    let mut result = a;
    result.extend(b);
    result
}

/// Mothership - Process supervisor with HTTP exposure
#[derive(Parser)]
#[command(name = "mothership")]
#[command(about = "Wrap, monitor, and expose your fleet", long_about = None)]
#[command(disable_version_flag = true)]
struct Cli {
    /// Print version
    #[arg(short = 'v', long = "version")]
    version: bool,

    #[command(subcommand)]
    command: Option<Commands>,

    /// Path to ship-manifest.toml
    #[arg(short, long, value_name = "FILE", default_value = "ship-manifest.toml")]
    config: PathBuf,

    /// Enable TUI dashboard (disables JSON logging to stdout)
    #[arg(long)]
    tui: bool,

    /// Only run ships with these tags (comma-separated or multiple flags)
    #[arg(long, value_delimiter = ',', action = clap::ArgAction::Append)]
    only: Vec<String>,

    /// Exclude ships with these tags (comma-separated or multiple flags)
    #[arg(long, value_delimiter = ',', action = clap::ArgAction::Append)]
    except: Vec<String>,
}

#[derive(clap::Subcommand)]
enum Commands {
    /// Run the fleet (default)
    Run {
        /// Path to ship-manifest.toml
        #[arg(short, long, value_name = "FILE")]
        config: Option<PathBuf>,

        /// Enable TUI dashboard
        #[arg(long)]
        tui: bool,

        /// Only run ships with these tags
        #[arg(long, value_delimiter = ',', action = clap::ArgAction::Append)]
        only: Vec<String>,

        /// Exclude ships with these tags
        #[arg(long, value_delimiter = ',', action = clap::ArgAction::Append)]
        except: Vec<String>,
    },
    /// Request launch clearance (validate manifest)
    #[clap(alias = "validate")]
    Clearance {
        /// Path to ship-manifest.toml
        #[arg(short, long, value_name = "FILE")]
        config: Option<PathBuf>,
        /// Show detailed output
        #[arg(short, long)]
        verbose: bool,
    },
    /// Pre-flight check (validate manifest + verify uplinks)
    Preflight {
        /// Path to ship-manifest.toml
        #[arg(short, long, value_name = "FILE")]
        config: Option<PathBuf>,
        /// Show detailed output
        #[arg(short, long)]
        verbose: bool,
    },
    /// Show navigation chart (routing configuration)
    Chart {
        /// Path to ship-manifest.toml
        #[arg(short, long, value_name = "FILE")]
        config: Option<PathBuf>,
    },
    /// Initialize a new ship-manifest.toml
    Init {
        /// Force overwrite if file exists
        #[arg(short, long)]
        force: bool,
    },
}

fn runs_process_supervision(command: Option<&Commands>) -> bool {
    matches!(command, None | Some(Commands::Run { .. }))
}

#[cfg(target_os = "linux")]
fn maybe_run_pid1_shim(should_supervise: bool) -> Result<()> {
    use nix::errno::Errno;
    use nix::sys::signal::{self, SigSet, SigmaskHow, Signal};
    use nix::sys::wait::{WaitPidFlag, WaitStatus, waitpid};
    use nix::unistd::Pid;
    use std::os::unix::process::CommandExt;

    const SHIM_CHILD_ENV: &str = "MS_PID1_SHIM_CHILD";

    if !should_supervise {
        return Ok(());
    }

    if std::process::id() != 1 || std::env::var_os(SHIM_CHILD_ENV).is_some() {
        return Ok(());
    }

    let exe = std::env::current_exe()?;
    let mut cmd = std::process::Command::new(exe);
    cmd.args(std::env::args_os().skip(1));
    cmd.env(SHIM_CHILD_ENV, "1");

    // Spawn child as its own process group so signals can be forwarded to the whole subtree.
    unsafe {
        cmd.pre_exec(|| {
            if libc::setpgid(0, 0) != 0 {
                return Err(std::io::Error::last_os_error());
            }
            Ok(())
        });
    }

    let child_pid = {
        let child = cmd.spawn()?;
        Pid::from_raw(child.id() as i32)
    };

    let mut sigset = SigSet::empty();
    for sig in [
        Signal::SIGCHLD,
        Signal::SIGTERM,
        Signal::SIGINT,
        Signal::SIGHUP,
        Signal::SIGQUIT,
    ] {
        sigset.add(sig);
    }
    signal::pthread_sigmask(SigmaskHow::SIG_BLOCK, Some(&sigset), None)?;

    loop {
        loop {
            match waitpid(Pid::from_raw(-1), Some(WaitPidFlag::WNOHANG)) {
                Ok(WaitStatus::StillAlive) => break,
                Ok(WaitStatus::Exited(pid, code)) if pid == child_pid => std::process::exit(code),
                Ok(WaitStatus::Signaled(pid, sig, _)) if pid == child_pid => {
                    std::process::exit(128 + sig as i32);
                }
                Ok(_) => {}
                Err(Errno::ECHILD) => std::process::exit(0),
                Err(e) => return Err(e.into()),
            }
        }

        match sigset.wait()? {
            Signal::SIGCHLD => {}
            sig => {
                let _ = signal::killpg(child_pid, sig);
                let _ = signal::kill(child_pid, sig);
            }
        }
    }
}

#[cfg(not(target_os = "linux"))]
fn maybe_run_pid1_shim(_should_supervise: bool) -> Result<()> {
    Ok(())
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    maybe_run_pid1_shim(runs_process_supervision(cli.command.as_ref()))?;

    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()?;
    runtime.block_on(async_main(cli))
}

async fn async_main(cli: Cli) -> Result<()> {
    // Handle version flag
    if cli.version {
        println!("mothership {}", env!("CARGO_PKG_VERSION"));
        return Ok(());
    }

    // Determine if TUI is enabled
    #[cfg(feature = "tui")]
    let tui_enabled = match &cli.command {
        Some(Commands::Run { tui, .. }) => *tui || cli.tui,
        None => cli.tui,
        _ => false,
    };

    #[cfg(not(feature = "tui"))]
    let tui_enabled = {
        let requested = match &cli.command {
            Some(Commands::Run { tui, .. }) => *tui || cli.tui,
            None => cli.tui,
            _ => false,
        };
        if requested {
            eprintln!("Warning: TUI requested but not compiled in. Build with --features tui");
        }
        false
    };

    // Initialize logging (skip JSON to stdout if TUI is enabled)
    if !tui_enabled {
        tracing_subscriber::registry()
            .with(fmt::layer().json())
            .with(EnvFilter::from_default_env().add_directive("mothership=info".parse()?))
            .init();
    }

    match cli.command {
        Some(Commands::Init { force }) => {
            init_manifest(force)?;
        }
        Some(Commands::Clearance { config, verbose }) => {
            let path = config.unwrap_or(cli.config);
            validate_manifest(&path, verbose)?;
        }
        Some(Commands::Preflight { config, verbose }) => {
            let path = config.unwrap_or(cli.config);
            preflight_check(&path, verbose).await?;
        }
        Some(Commands::Chart { config }) => {
            let path = config.unwrap_or(cli.config);
            show_chart(&path)?;
        }
        Some(Commands::Run {
            config,
            tui,
            only,
            except,
        }) => {
            let path = config.unwrap_or(cli.config);
            let filter = TagFilter {
                only: merge_vecs(cli.only, only),
                except: merge_vecs(cli.except, except),
            };
            run_fleet(&path, tui || cli.tui, filter).await?;
        }
        None => {
            let filter = TagFilter {
                only: cli.only,
                except: cli.except,
            };
            run_fleet(&cli.config, cli.tui, filter).await?;
        }
    }

    Ok(())
}

fn init_manifest(force: bool) -> Result<()> {
    let path = PathBuf::from("ship-manifest.toml");

    if path.exists() && !force {
        anyhow::bail!("ship-manifest.toml already exists. Use --force to overwrite.");
    }

    let template = r#"# Ship Manifest - Fleet Configuration
# https://github.com/seuros/mothership

# =============================================================================
# BASE TEMPLATES (optional - reduce duplication)
# =============================================================================
# Define defaults inherited by all ships/modules

# [base.ship]
# env = { RAILS_ENV = "production" }
# critical = true
# tags = ["ruby"]
# comment = "Default ship settings"

# [base.module]
# phase = "request"
# tags = ["security"]

# =============================================================================
# MOTHERSHIP (global configuration)
# =============================================================================

[mothership]
metrics_port = 9090  # Prometheus metrics at http://127.0.0.1:9090/metrics

# Named binds (external listeners)
[mothership.bind]
http = "0.0.0.0:80"
# https = "0.0.0.0:443"
# ws = "0.0.0.0:8080"

# Environment variables inherited by all ships
# [mothership.env]
# RAILS_ENV = "production"

# =============================================================================
# FLEET (ship definitions)
# =============================================================================
# Ships can be filtered by tags at runtime:
#   mothership --only web          # Only ships tagged "web"
#   mothership --only web,api      # Ships tagged "web" OR "api"
#   mothership --except workers    # All except "workers"

# Web ships (HTTP-serving applications)
[[fleet.web]]
name = "app"
command = "ruby"
args = ["app.rb"]
bind = "tcp://127.0.0.1:3000"
healthcheck = "/health"
tags = ["web", "ruby"]
routes = [
  { bind = "http", pattern = "/.*" },
]

# Background workers
# [[fleet.workers]]
# name = "sidekiq"
# command = "bundle"
# args = ["exec", "sidekiq"]
# critical = false  # Non-critical: crash won't kill fleet
# tags = ["workers", "ruby"]

# One-shot jobs (run once and exit)
# [[fleet.jobs]]
# name = "migrate"
# command = "bundle"
# args = ["exec", "rails", "db:migrate"]
# oneshot = true
# tags = ["jobs", "ruby"]

# WebSocket server
# [[fleet.web]]
# name = "cable"
# command = "anycable-go"
# bind = "tcp://127.0.0.1:8081"
# depends_on = ["app"]
# tags = ["web", "websocket"]
# routes = [
#   { bind = "ws", pattern = "/cable" },
# ]

# =============================================================================
# WASM MODULES (optional plugins)
# =============================================================================

# [[modules]]
# name = "auth"
# wasm = "modules/auth.wasm"
# routes = ["/admin/.*"]
# phase = "request"
# tags = ["security"]
"#;

    std::fs::write(&path, template)?;
    println!("Created ship-manifest.toml");
    println!();
    println!("Next steps:");
    println!("  1. Edit ship-manifest.toml with your app configuration");
    println!("  2. Run: mothership clearance");
    println!("  3. Run: mothership --tui");

    Ok(())
}

fn validate_manifest(path: &PathBuf, verbose: bool) -> Result<()> {
    info!(path = %path.display(), "Validating manifest");

    println!("[CLEARANCE] {}", path.display());

    let manifest = match Manifest::load(path) {
        Ok(m) => {
            println!("manifest: ✓");
            m
        }
        Err(e) => {
            println!("manifest: ✗");
            if verbose {
                println!("  error: {}", e);
            }
            println!();
            println!("[RESULT] fail");
            return Err(e);
        }
    };

    if verbose {
        // Binds
        if !manifest.mothership.bind.is_empty() {
            println!();
            println!("[BINDS]");
            for (name, bind) in &manifest.mothership.bind {
                println!("- {}: {:?}", name, bind);
            }
        }

        // Fleet
        if !manifest.fleet.is_empty() {
            println!();
            println!("[FLEET] {} ships", manifest.ship_count());
            for (group_name, ships) in &manifest.fleet {
                for ship in ships {
                    let flags = format!(
                        "{}{}",
                        if ship.critical { "" } else { " [non-critical]" },
                        if ship.oneshot { " [oneshot]" } else { "" }
                    );
                    println!("- {}/{}: {}{}", group_name, ship.name, ship.command, flags);
                }
            }
        }

        // Bays
        if !manifest.bays.is_empty() {
            println!();
            println!("[BAYS] {} bays", manifest.bay_count());
            for (bay_type, bays) in &manifest.bays {
                for bay in bays {
                    println!("- {}/{}: {}", bay_type, bay.name, bay.command);
                }
            }
        }

        // Modules
        if !manifest.modules.is_empty() {
            println!();
            println!("[MODULES] {}", manifest.modules.len());
            for m in &manifest.modules {
                println!("- {}: {} [{:?}]", m.name, m.wasm, m.phase);
            }
        }

        // Uplinks
        if !manifest.mothership.uplinks.is_empty() {
            println!();
            println!("[UPLINKS] {} configured", manifest.mothership.uplinks.len());
            for uplink in &manifest.mothership.uplinks {
                println!("- {}: {}", uplink.name, uplink.url);
            }
        }

        // Prelaunch
        if !manifest.mothership.prelaunch.is_empty() {
            println!();
            println!("[PRELAUNCH] {} jobs", manifest.mothership.prelaunch.len());
            for job in &manifest.mothership.prelaunch {
                let deps = if job.depends_on.is_empty() {
                    String::new()
                } else {
                    format!(" (after: {})", job.depends_on.join(", "))
                };
                println!(
                    "- {}: {} {}{}",
                    job.name,
                    job.command,
                    job.args.join(" "),
                    deps
                );
            }
        }
    } else {
        // Compact summary
        println!("ships: {}", manifest.ship_count());
        println!("bays: {}", manifest.bay_count());
        println!("uplinks: {}", manifest.mothership.uplinks.len());
        println!("prelaunch: {}", manifest.mothership.prelaunch.len());
    }

    println!();
    println!("[RESULT] pass");

    Ok(())
}

async fn preflight_check(path: &PathBuf, verbose: bool) -> Result<()> {
    println!("[PREFLIGHT] {}", path.display());

    // Step 1: Validate manifest
    let manifest = match Manifest::load(path) {
        Ok(m) => {
            println!("manifest: ✓");
            m
        }
        Err(e) => {
            println!("manifest: ✗");
            if verbose {
                println!("  error: {}", e);
            }
            println!();
            println!("[RESULT] fail");
            return Err(e);
        }
    };

    // Step 2: Check uplinks
    if manifest.mothership.uplinks.is_empty() {
        println!("uplinks: ✓ (none configured)");
    } else {
        let total = manifest.mothership.uplinks.len();
        let mut passed = 0;
        let mut failed_uplinks: Vec<(&str, String)> = Vec::new();

        for uplink in &manifest.mothership.uplinks {
            match verify_uplinks(std::slice::from_ref(uplink)).await {
                Ok(()) => {
                    passed += 1;
                    if verbose {
                        println!("- {}: ✓ ({})", uplink.name, uplink.url);
                    }
                }
                Err(e) => {
                    let error_msg = e.to_string();
                    failed_uplinks.push((&uplink.name, error_msg.clone()));
                    if verbose {
                        println!("- {}: ✗ ({})", uplink.name, uplink.url);
                        println!("  error: {}", error_msg);
                    }
                }
            }
        }

        if failed_uplinks.is_empty() {
            println!("uplinks: ✓ ({}/{})", passed, total);
        } else {
            println!("uplinks: ✗ ({}/{})", passed, total);
            if !verbose {
                // Show failed uplinks even in non-verbose mode
                for (name, error) in &failed_uplinks {
                    println!("  {}: {}", name, error);
                }
            }
            println!();
            println!("[RESULT] fail");
            anyhow::bail!("{} uplink(s) unreachable", failed_uplinks.len());
        }
    }

    println!();
    println!("[RESULT] pass");

    Ok(())
}

fn show_chart(path: &PathBuf) -> Result<()> {
    use mothership::charter::Bind;

    let manifest = Manifest::load(path)?;

    println!("Navigation Chart");
    println!("================");
    println!();

    // Docking ports (binds)
    if manifest.mothership.bind.is_empty() {
        println!("Docking Ports: (none configured)");
    } else {
        println!("Docking Ports:");
        for (name, bind) in &manifest.mothership.bind {
            let addr = match bind {
                Bind::Tcp { host, port, .. } => format!("{}:{}", host, port),
                Bind::Unix { path } => format!("unix://{}", path),
            };
            let proto_marker = if bind.has_proxy_protocol() {
                " [PROXY]"
            } else {
                ""
            };
            println!("  {} → {}{}", name, addr, proto_marker);
        }
    }
    println!();

    // Cargo bays (static files)
    if !manifest.mothership.static_dirs.is_empty() {
        println!("Cargo Bays (static files):");
        for static_cfg in &manifest.mothership.static_dirs {
            println!("  {} → {}", static_cfg.prefix, static_cfg.path);
            if let Some(ref bind) = static_cfg.bind {
                println!("    bind: {}", bind);
            }
        }
        println!();
    }

    // Compression
    println!(
        "Compression: {}",
        if manifest.mothership.compression {
            "enabled"
        } else {
            "disabled"
        }
    );
    println!();

    // Flight vectors (routes per bind, in declaration order)
    println!("Flight Vectors:");

    // Collect routes in declaration order, grouped by bind
    #[allow(clippy::type_complexity)]
    let mut vectors_by_port: indexmap::IndexMap<String, Vec<(&Vessel, &str, Option<&str>)>> =
        indexmap::IndexMap::new();

    for vessel in &manifest.vessels {
        for route in vessel.routes() {
            vectors_by_port
                .entry(route.bind.clone())
                .or_default()
                .push((vessel, &route.pattern, route.strip_prefix.as_deref()));
        }
    }

    if vectors_by_port.is_empty() {
        println!("  (no vectors configured)");
    } else {
        for (port_name, vectors) in &vectors_by_port {
            let port_addr = manifest.mothership.bind.get(port_name).map(|b| match b {
                Bind::Tcp { host, port, .. } => format!("{}:{}", host, port),
                Bind::Unix { path } => format!("unix://{}", path),
            });
            println!();
            println!(
                "  [{}] ({})",
                port_name,
                port_addr.as_deref().unwrap_or("unknown")
            );
            for (vessel, pattern, strip_prefix) in vectors {
                let (destination, suffix) = match vessel {
                    Vessel::Ship { config, .. } => {
                        if let Some(bind) = &config.bind {
                            let addr = match bind {
                                Bind::Tcp { host, port, .. } => format!("{}:{}", host, port),
                                Bind::Unix { path } => format!("unix://{}", path),
                            };
                            (addr, "".to_string())
                        } else {
                            ("(no dock)".to_string(), "".to_string())
                        }
                    }
                    Vessel::Bay { bay_type, .. } => {
                        ("(docked)".to_string(), format!(" âš“ {}", bay_type))
                    }
                };

                let strip = strip_prefix
                    .map(|p| format!(" (strip: {})", p))
                    .unwrap_or_default();
                println!(
                    "    {} → {} [{}]{}{}",
                    pattern,
                    destination,
                    vessel.name(),
                    suffix,
                    strip
                );
            }
        }
    }
    println!();

    // Docking Bays
    if !manifest.bays.is_empty() {
        println!("Docking Bays:");
        for (bay_type, bays) in &manifest.bays {
            for bay in bays {
                let critical = if bay.critical { " âš " } else { "" };
                println!("  {} [{}]{}", bay.name, bay_type, critical);
                println!("    command: {} {}", bay.command, bay.args.join(" "));
            }
        }
        println!();
    }

    // WASM modules
    if !manifest.modules.is_empty() {
        println!("Onboard Modules:");
        for m in &manifest.modules {
            println!("  {} [{:?}]", m.name, m.phase);
            for route in &m.routes {
                println!("    {}", route);
            }
        }
    }

    Ok(())
}

/// Run flagship coordination for multi-server deployments
///
/// Returns `true` if this instance should run prelaunch jobs (Flagship or disabled),
/// `false` if this instance should skip prelaunch (Escort).
///
/// For Flagship: signals ready/abort after prelaunch completes (caller must signal).
/// For Escort: waits for Flagship signal before returning.
async fn run_flagship_coordination(
    config: &mothership::charter::FlagshipConfig,
    #[allow(unused_variables)] manifest: &Manifest,
    tui_enabled: bool,
) -> Result<bool> {
    match config.election.as_str() {
        "static" => {
            // Static election: evaluate env var or command
            let is_flagship = config
                .static_flagship
                .evaluate()
                .map_err(|e| anyhow::anyhow!("failed to evaluate static_flagship: {}", e))?;

            if !tui_enabled {
                if is_flagship {
                    info!(
                        role = "flagship",
                        "Static election: this instance is the flagship"
                    );
                } else {
                    info!(
                        role = "escort",
                        "Static election: this instance is an escort (skipping prelaunch)"
                    );
                }
            }

            // In static mode, escorts skip prelaunch (external coordination assumed)
            Ok(is_flagship)
        }
        #[cfg(feature = "tokio-postgres")]
        "postgres" => {
            use std::time::Duration;

            let app_name = manifest
                .fleet
                .values()
                .flatten()
                .next()
                .map(|v| v.name.clone())
                .unwrap_or_else(|| "mothership".to_string());

            let prelaunch_timeout = Duration::from_secs(config.prelaunch_timeout);

            let url = config
                .expanded_election_url()
                .ok_or_else(|| anyhow::anyhow!("election_url required for postgres election"))?
                .map_err(|e| anyhow::anyhow!("failed to expand election_url: {}", e))?;

            let election = PostgresElection::new(url);
            let mut coordinator = Coordinator::new(election, app_name.clone(), prelaunch_timeout);

            let role = coordinator
                .elect()
                .await
                .map_err(|e| anyhow::anyhow!("flagship election failed: {}", e))?;

            match role {
                FlagshipRole::Flagship => {
                    if !tui_enabled {
                        info!(role = "flagship", "Elected as flagship, will run prelaunch");
                    }
                    // Signal running before prelaunch
                    coordinator.signal_running().await.ok();

                    // Run prelaunch
                    if !manifest.mothership.prelaunch.is_empty() {
                        if !tui_enabled {
                            info!(
                                count = manifest.mothership.prelaunch.len(),
                                "Running prelaunch jobs (flagship)"
                            );
                        }
                        match run_prelaunch(
                            &manifest.mothership.prelaunch,
                            &manifest.mothership.env,
                        )
                        .await
                        {
                            Ok(()) => {
                                coordinator.signal_ready().await.map_err(|e| {
                                    anyhow::anyhow!("failed to signal ready: {}", e)
                                })?;
                                if !tui_enabled {
                                    info!("Prelaunch complete, signaled ready to escorts");
                                }
                            }
                            Err(e) => {
                                coordinator.signal_abort().await.ok();
                                return Err(anyhow::anyhow!(
                                    "prelaunch failed, signaled abort: {}",
                                    e
                                ));
                            }
                        }
                    } else {
                        // No prelaunch jobs, signal ready immediately
                        coordinator
                            .signal_ready()
                            .await
                            .map_err(|e| anyhow::anyhow!("failed to signal ready: {}", e))?;
                    }

                    // Prelaunch already ran, skip the normal prelaunch path
                    Ok(false)
                }
                FlagshipRole::Escort => {
                    if !tui_enabled {
                        info!(
                            role = "escort",
                            timeout_secs = prelaunch_timeout.as_secs(),
                            "Waiting for flagship signal"
                        );
                    }
                    coordinator
                        .wait_for_ready()
                        .await
                        .map_err(|e| anyhow::anyhow!("escort wait failed: {}", e))?;
                    if !tui_enabled {
                        info!("Received ready signal from flagship");
                    }
                    // Escort skips prelaunch
                    Ok(false)
                }
                FlagshipRole::Disabled => {
                    // Shouldn't happen when flagship is enabled
                    Ok(true)
                }
            }
        }
        #[cfg(not(feature = "tokio-postgres"))]
        "postgres" => {
            anyhow::bail!("postgres election requires the 'tokio-postgres' feature");
        }
        other => {
            anyhow::bail!("unknown election backend: {}", other);
        }
    }
}

async fn run_fleet(path: &PathBuf, tui_enabled: bool, filter: TagFilter) -> Result<()> {
    // Initialize lifecycle state machine
    let mut lifecycle = Lifecycle::new();

    // Suppress stdout logging if TUI is enabled
    if tui_enabled {
        set_suppress_stdout(true);
    } else {
        info!(path = %path.display(), "Loading manifest");
    }

    let mut manifest = Manifest::load(path)?;

    // Transition: Initializing → Preflight (manifest loaded)
    lifecycle.manifest_loaded();

    // Apply tag filter to ships
    if !filter.is_empty() {
        let before_count = manifest.ship_count();
        manifest.filter_ships(&|tags| filter.matches(tags));
        manifest.validate_vessel_dependencies()?;
        let after_count = manifest.ship_count();
        if !tui_enabled {
            info!(
                before = before_count,
                after = after_count,
                only = ?filter.only,
                except = ?filter.except,
                "Applied tag filter"
            );
        }
    }

    // Verify uplinks (external dependencies) before launching fleet
    // ALL instances verify their own uplinks (connectivity check)
    if !manifest.mothership.uplinks.is_empty() {
        if !tui_enabled {
            info!(
                count = manifest.mothership.uplinks.len(),
                "Verifying uplinks"
            );
        }
        if let Err(e) = verify_uplinks(&manifest.mothership.uplinks).await {
            lifecycle.preflight_failed();
            return Err(e.into());
        }
        if !tui_enabled {
            info!("All uplinks verified");
        }
        // Transition: Preflight → Electing
        lifecycle.preflight_complete();
    } else {
        // No uplinks to verify, skip preflight
        lifecycle.preflight_skipped();
    }

    // Flagship coordination: only ONE instance runs prelaunch
    let flagship_config = &manifest.mothership.flagship;
    let should_run_prelaunch = if flagship_config.enabled {
        match run_flagship_coordination(flagship_config, &manifest, tui_enabled).await {
            Ok(result) => {
                lifecycle.election_complete();
                result
            }
            Err(e) => {
                lifecycle.election_failed();
                return Err(e);
            }
        }
    } else {
        // Flagship disabled - run prelaunch on this instance (single-server mode)
        lifecycle.election_skipped();
        true
    };

    // Run prelaunch jobs before any ship/bay starts
    if should_run_prelaunch && !manifest.mothership.prelaunch.is_empty() {
        if !tui_enabled {
            info!(
                count = manifest.mothership.prelaunch.len(),
                "Running prelaunch jobs"
            );
        }
        if let Err(e) =
            run_prelaunch(&manifest.mothership.prelaunch, &manifest.mothership.env).await
        {
            lifecycle.prelaunch_failed();
            return Err(e.into());
        }
        if !tui_enabled {
            info!("All prelaunch jobs completed");
        }
        // Transition: Prelaunch → Docking
        lifecycle.prelaunch_complete();
    } else {
        // No prelaunch jobs or skipped (escort)
        lifecycle.prelaunch_skipped();
    }

    // Transition: Docking → Launching (no bays in current implementation)
    lifecycle.docking_skipped();

    let fleet = Arc::new(Fleet::from_manifest(&manifest));

    // Set up shutdown channels
    let shutdown = Arc::new(Notify::new());
    let (http_shutdown_tx, http_shutdown_rx) = watch::channel(false);
    let (metrics_shutdown_tx, metrics_shutdown_rx) = watch::channel(false);
    #[cfg(feature = "tui")]
    let (tui_shutdown_tx, tui_shutdown_rx) = watch::channel(false);
    #[cfg(not(feature = "tui"))]
    let (tui_shutdown_tx, _tui_shutdown_rx) = watch::channel(false);

    // Create metrics registry (shared with HTTP proxy for request counting)
    let metrics_registry = Arc::new(MetricsRegistry::new());
    set_global_metrics_registry(metrics_registry.clone());

    // Signal handler (SIGINT and SIGTERM)
    let shutdown_signal = shutdown.clone();
    let http_shutdown = http_shutdown_tx.clone();
    let metrics_shutdown = metrics_shutdown_tx.clone();
    let tui_shutdown = tui_shutdown_tx.clone();
    tokio::spawn(async move {
        #[cfg(unix)]
        {
            use tokio::signal::unix::{SignalKind, signal};
            let mut sigterm = signal(SignalKind::terminate()).expect("SIGTERM handler");
            let mut sigint = signal(SignalKind::interrupt()).expect("SIGINT handler");

            tokio::select! {
                _ = sigterm.recv() => {
                    if !tui_enabled {
                        info!("Received SIGTERM, initiating shutdown");
                    }
                }
                _ = sigint.recv() => {
                    if !tui_enabled {
                        info!("Received SIGINT, initiating shutdown");
                    }
                }
            }
        }
        #[cfg(not(unix))]
        {
            tokio::signal::ctrl_c().await.ok();
            if !tui_enabled {
                info!("Received SIGINT, initiating shutdown");
            }
        }
        shutdown_signal.notify_one();
        let _ = http_shutdown.send(true);
        let _ = metrics_shutdown.send(true);
        let _ = tui_shutdown.send(true);
    });

    // Launch fleet (processes)
    if let Err(e) = fleet.launch().await {
        lifecycle.launch_failed();
        fleet.shutdown().await;
        return Err(e);
    }

    // Transition: Launching → Running
    lifecycle.launch_complete();

    // Start HTTP exposure layer if configured
    let http_handle = if let Some(http_exposure) = HttpExposure::from_manifest(&manifest) {
        // Register bay connectors after fleet has launched (bays are now docked)
        for bay in fleet.all_bays() {
            if let Some(connector) = fleet.get_bay_connector(&bay.name).await {
                http_exposure
                    .register_bay_connector(bay.name.clone(), connector)
                    .await;
                debug!(bay = %bay.name, "Registered bay connector with HTTP layer");
            }
        }

        Some(tokio::spawn(async move {
            if let Err(e) = http_exposure.run(http_shutdown_rx).await {
                tracing::error!(error = %e, "HTTP exposure layer failed");
            }
        }))
    } else {
        if !tui_enabled {
            info!("No mothership.bind configured, HTTP exposure disabled");
        }
        None
    };

    // Start metrics server if configured
    let metrics_handle = if let Some(port) = manifest.mothership.metrics_port {
        let metrics_fleet = fleet.clone();
        let metrics_reg = metrics_registry.clone();
        Some(tokio::spawn(async move {
            let server = MetricsServer::new(port, metrics_fleet, metrics_reg);
            server.run(metrics_shutdown_rx).await;
        }))
    } else {
        None
    };

    // Monitor fleet in background
    let fleet_monitor = fleet.clone();
    let shutdown_notify = shutdown.clone();
    let monitor_handle = tokio::spawn(async move {
        fleet_monitor.monitor(shutdown_notify).await;
    });

    // Start TUI if enabled
    #[cfg(feature = "tui")]
    let tui_handle = if tui_enabled {
        let tui_fleet = fleet.clone();
        Some(tokio::spawn(async move {
            match TuiApp::new(tui_fleet, tui_shutdown_rx).await {
                Ok(mut app) => {
                    if let Err(e) = app.run().await {
                        eprintln!("TUI error: {}", e);
                    }
                }
                Err(e) => {
                    eprintln!("Failed to start TUI: {}", e);
                }
            }
        }))
    } else {
        None
    };

    #[cfg(not(feature = "tui"))]
    let tui_handle: Option<tokio::task::JoinHandle<()>> = None;

    // Wait for shutdown signal (or TUI exit)
    #[cfg(feature = "tui")]
    if let Some(mut tui) = tui_handle {
        // TUI mode - wait for TUI exit or shutdown signal
        tokio::select! {
            _ = &mut tui => {
                shutdown.notify_one();
            }
            _ = shutdown.notified() => {
                let _ = tui_shutdown_tx.send(true);
                let _ = tui.await;
            }
        }
    } else {
        // Headless mode - wait for signal
        shutdown.notified().await;
    }

    #[cfg(not(feature = "tui"))]
    {
        let _ = tui_handle; // suppress unused warning
        shutdown.notified().await;
    }

    // Transition: Running → Draining
    lifecycle.drain_started();

    // Stop monitoring
    monitor_handle.abort();

    // Stop HTTP server
    if let Some(handle) = http_handle {
        let _ = http_shutdown_tx.send(true);
        let _ = handle.await;
    }

    // Stop metrics server
    if let Some(handle) = metrics_handle {
        let _ = metrics_shutdown_tx.send(true);
        let _ = handle.await;
    }

    // Graceful shutdown of processes
    fleet.shutdown().await;

    // Transition: Draining → Landed
    lifecycle.drain_complete();

    if !tui_enabled {
        info!(status = %lifecycle.status(), "Mothership landed");
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::{fs, path::Path, time::Duration};

    use super::{Commands, runs_process_supervision};
    use tempfile::tempdir;
    use tokio::time::{Instant, sleep};

    async fn wait_for_pid_file(path: &Path) -> i32 {
        let deadline = Instant::now() + Duration::from_secs(2);
        loop {
            if let Ok(contents) = fs::read_to_string(path)
                && let Ok(pid) = contents.trim().parse::<i32>()
                && pid > 0
            {
                return pid;
            }
            if Instant::now() >= deadline {
                panic!("timed out waiting for pid file: {}", path.display());
            }
            sleep(Duration::from_millis(20)).await;
        }
    }

    fn process_exists(pid: i32) -> bool {
        let result = unsafe { libc::kill(pid, 0) };
        if result == 0 {
            return true;
        }
        std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
    }

    async fn wait_for_exit(pid: i32) {
        let deadline = Instant::now() + Duration::from_secs(2);
        loop {
            if !process_exists(pid) {
                return;
            }
            if Instant::now() >= deadline {
                panic!("process {} still running after shutdown", pid);
            }
            sleep(Duration::from_millis(20)).await;
        }
    }

    #[test]
    fn default_command_runs_supervision() {
        assert!(runs_process_supervision(None));
    }

    #[test]
    fn run_subcommand_runs_supervision() {
        let cmd = Commands::Run {
            config: None,
            tui: false,
            only: vec![],
            except: vec![],
        };
        assert!(runs_process_supervision(Some(&cmd)));
    }

    #[test]
    fn utility_commands_skip_supervision() {
        let cmd = Commands::Chart { config: None };
        assert!(!runs_process_supervision(Some(&cmd)));
    }

    #[tokio::test]
    async fn run_fleet_cleans_up_started_ships_when_launch_fails() {
        let temp = tempdir().expect("temp dir");
        let pid_path = temp.path().join("ship-child.pid");
        let manifest_path = temp.path().join("ship-manifest.toml");

        let manifest = format!(
            r#"
[[fleet.web]]
name = "app"
command = "sh"
args = ["-c", "echo $$ > \"{pid_path}\"; sleep 1000"]
critical = false

[[fleet.web]]
name = "broken"
command = "/definitely/missing-command"
critical = false
"#,
            pid_path = pid_path.display()
        );
        fs::write(&manifest_path, manifest).expect("write manifest");

        let result = super::run_fleet(&manifest_path, false, super::TagFilter::default()).await;
        assert!(result.is_err(), "launch should fail");

        let child_pid = wait_for_pid_file(&pid_path).await;
        wait_for_exit(child_pid).await;
    }

    #[tokio::test]
    async fn run_fleet_fails_when_tag_filter_removes_dependency() {
        let temp = tempdir().expect("temp dir");
        let manifest_path = temp.path().join("ship-manifest.toml");

        let manifest = r#"
[[fleet.jobs]]
name = "migrate"
command = "sh"
args = ["-c", "exit 0"]
tags = ["jobs"]

[[fleet.web]]
name = "app"
command = "sh"
args = ["-c", "sleep 1"]
depends_on = ["migrate"]
tags = ["web"]
"#;
        fs::write(&manifest_path, manifest).expect("write manifest");

        let result = super::run_fleet(
            &manifest_path,
            false,
            super::TagFilter {
                only: vec!["web".to_string()],
                except: vec![],
            },
        )
        .await;

        let error = result.expect_err("filtered dependency should fail");
        assert!(
            error.to_string().contains("depends on unknown vessel 'migrate'"),
            "unexpected error: {error}"
        );
    }
}