Skip to main content

agentmux_launcher/
startup_events.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Startup event bus: typed events emitted by each launcher startup stage and
5//! consumed by the native splash screen renderer.
6//!
7//! Transport: `std::sync::mpsc::channel` (unbounded).  `Sender::send` never
8//! blocks and is safe to call from async tokio code.  The splash thread drains
9//! the receiver with `try_recv` every animation frame (~16 ms).
10//!
11//! On non-Windows platforms (or when the splash is disabled), the receiver is
12//! dropped immediately and subsequent sends silently return `Err` — callers
13//! ignore the result of every send so nothing blows up.
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum StartupStatus {
17    Ok,
18    Warn,
19    Error,
20}
21
22#[derive(Debug)]
23pub enum StartupEvent {
24    /// A top-level startup stage has begun.
25    StageBegin {
26        stage: &'static str,
27        label: &'static str,
28    },
29    /// A top-level startup stage has completed.
30    StageEnd {
31        stage: &'static str,
32        duration_ms: u64,
33        status: StartupStatus,
34        detail: Option<String>,
35    },
36    /// A sub-item within a stage has begun (e.g. an individual migration).
37    SubBegin {
38        stage: &'static str,
39        id: String,
40        label: String,
41    },
42    /// A sub-item within a stage has completed.
43    SubEnd {
44        stage: &'static str,
45        id: String,
46        duration_ms: u64,
47        status: StartupStatus,
48        /// Short human-readable annotation shown after the time (e.g. "✓").
49        detail: Option<String>,
50    },
51}
52
53/// Clone-able sender half — one per startup stage, cloned as needed.
54///
55/// Every clone shares one sender, so [`StartupEventSink::reconnect`] moves
56/// them all to a new receiver at once: the restart splash takes over the
57/// events the launcher IPC server (which holds a clone) forwards from the
58/// restarted host.
59#[derive(Clone, Debug)]
60pub struct StartupEventSink {
61    tx: std::sync::Arc<std::sync::Mutex<std::sync::mpsc::Sender<StartupEvent>>>,
62}
63
64impl StartupEventSink {
65    /// Create a linked `(sink, receiver)` pair.  Pass the receiver to
66    /// `splash::spawn_splash`; keep the sink in the launcher startup path.
67    pub fn new() -> (Self, std::sync::mpsc::Receiver<StartupEvent>) {
68        let (tx, rx) = std::sync::mpsc::channel();
69        (Self { tx: std::sync::Arc::new(std::sync::Mutex::new(tx)) }, rx)
70    }
71
72    /// Point this sink and every clone of it at a new receiver, for a splash
73    /// opened after the first one closed. The old receiver gets nothing more.
74    pub fn reconnect(&self) -> std::sync::mpsc::Receiver<StartupEvent> {
75        let (tx, rx) = std::sync::mpsc::channel();
76        if let Ok(mut current) = self.tx.lock() {
77            *current = tx;
78        }
79        rx
80    }
81
82    fn send(&self, event: StartupEvent) {
83        if let Ok(tx) = self.tx.lock() {
84            let _ = tx.send(event);
85        }
86    }
87
88    pub fn stage_begin(&self, stage: &'static str, label: &'static str) {
89        self.send(StartupEvent::StageBegin { stage, label });
90    }
91
92    pub fn stage_end(
93        &self,
94        stage: &'static str,
95        duration_ms: u64,
96        status: StartupStatus,
97        detail: Option<String>,
98    ) {
99        self.send(StartupEvent::StageEnd {
100            stage,
101            duration_ms,
102            status,
103            detail,
104        });
105    }
106
107    pub fn sub_begin(
108        &self,
109        stage: &'static str,
110        id: impl Into<String>,
111        label: impl Into<String>,
112    ) {
113        self.send(StartupEvent::SubBegin {
114            stage,
115            id: id.into(),
116            label: label.into(),
117        });
118    }
119
120    pub fn sub_end(
121        &self,
122        stage: &'static str,
123        id: impl Into<String>,
124        duration_ms: u64,
125        status: StartupStatus,
126        detail: Option<String>,
127    ) {
128        self.send(StartupEvent::SubEnd {
129            stage,
130            id: id.into(),
131            duration_ms,
132            status,
133            detail,
134        });
135    }
136}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141
142    #[test]
143    fn reconnect_moves_every_clone_to_the_new_receiver() {
144        let (sink, first) = StartupEventSink::new();
145        let ipc_clone = sink.clone();
146        ipc_clone.stage_begin("host", "Host startup");
147        assert!(matches!(first.try_recv(), Ok(StartupEvent::StageBegin { stage: "host", .. })));
148
149        let second = sink.reconnect();
150        ipc_clone.stage_begin("cef_init", "CEF initialize");
151        assert!(matches!(second.try_recv(), Ok(StartupEvent::StageBegin { stage: "cef_init", .. })));
152        assert!(first.try_recv().is_err(), "the old receiver gets nothing after a reconnect");
153    }
154}