Skip to main content

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 store_attach;
59mod store_boot;
60mod shared_root;
61// Moved down to `registry/` and `migrations/` so the core does not reach up into `bootstrap`; the old paths resolve.
62pub(crate) use crate::migrations::carry_forward;
63pub(crate) use crate::registry::shared_guard;
64mod background;
65mod messaging_bridges;
66mod network;
67mod plumbing;
68mod shutdown;
69mod delivery;
70mod state_services;
71
72pub use watchers::*;
73pub use logging::*;
74pub use stores::*;
75pub use background::*;
76pub use network::*;
77pub use plumbing::*;
78pub use shutdown::*;
79pub use delivery::*;
80pub use state_services::*;