agentmux_srv/bootstrap/mod.rs
1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Server bootstrap sequence, extracted from `main()`.
5//!
6//! Each function here corresponds to one (or a small cluster of) the
7//! numbered phases that used to live inline in `async fn main()`:
8//! crash-monitor branch -> parent-process watchdog -> logging init ->
9//! PATH enrichment -> CLI/config parsing -> data-dir + migration setup ->
10//! DB/store opening -> event bus + subagent watcher + background task
11//! spawns -> TCP listener binds -> router/AppState build -> ESTART stderr
12//! emission -> stdin-watch thread -> SIGINT/SIGTERM handler.
13//!
14//! This module is purely a relocation of that logic into named functions;
15//! `main()` calls them in the same order with the same effect.
16
17use std::sync::Arc;
18
19use clap::Parser;
20use tokio::net::TcpListener;
21
22use crate::backend;
23use crate::backend::eventbus::EventBus;
24use crate::backend::reactive::{self, Poller, PollerConfig};
25use crate::backend::storage::filestore::FileStore;
26use crate::backend::storage::migrations::OBJECT_SCHEMA_VERSION;
27use crate::backend::storage::snapshot::maybe_snapshot_pre_migration;
28use crate::backend::storage::store::Store;
29use crate::backend::mps::Broker;
30use crate::backend::wconfig;
31use crate::backend::{base, docsite, sysinfo, wcore};
32use crate::config::{self, CliArgs};
33use crate::event_log;
34use crate::messaging;
35use crate::migrations;
36use crate::persist;
37use crate::persist_subscriber;
38use crate::registry;
39use crate::server::{self, AppState};
40use crate::state;
41
42/// `eprintln!` that can't panic. srv's stderr is a pipe the launcher reads, so
43/// once the launcher is gone a write fails with EPIPE (Rust ignores SIGPIPE),
44/// and `eprintln!` panics on a failed write. On the paths that must run when
45/// the launcher has died, the parent and stdin watchers, that panic killed
46/// the watcher thread before it could shut srv down, which left srv running
47/// with no launcher, holding `srv.lock`, so the app could not be opened again.
48macro_rules! eprintln_lossy {
49 ($($arg:tt)*) => {{
50 use std::io::Write as _;
51 let _ = writeln!(std::io::stderr(), $($arg)*);
52 }};
53}
54
55mod watchers;
56mod logging;
57mod stores;
58mod background;
59mod network;
60mod plumbing;
61mod shutdown;
62mod delivery;
63
64pub use watchers::*;
65pub use logging::*;
66pub use stores::*;
67pub use background::*;
68pub use network::*;
69pub use plumbing::*;
70pub use shutdown::*;
71pub use delivery::*;