Skip to main content

agentmux_srv_core/
srv_info.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What srv tells a UI about the machine it runs on: sent as the `srvinfo`
5//! event on every WebSocket connect (`server/websocket.rs`). A UI shows these
6//! names and builds paths from the home directory, and the paths have to be
7//! srv's, which is only the same machine as the UI's when the UI is the
8//! desktop app.
9
10use std::sync::OnceLock;
11
12/// The account-wide AgentMux root (`~/.agentmux`, or its override) as it was
13/// when srv started. Read before startup sets `AGENTMUX_DATA_HOME` to the data
14/// directory (`bootstrap/stores.rs`), after which the same resolver answers
15/// with the data directory instead.
16static HOME_DIR: OnceLock<Option<std::path::PathBuf>> = OnceLock::new();
17
18/// Record the AgentMux root. Call first thing in `main`, before anything
19/// changes the environment.
20pub fn capture_home_dir() {
21    let _ = HOME_DIR.set(
22        // Kept as a PathBuf: a root that isn't valid UTF-8 must still name
23        // the same directory. Only the `srvinfo` payload stringifies it.
24        agentmux_common::data_paths::agentmux_root().ok(),
25    );
26}
27
28/// The account-wide AgentMux root captured at startup (`capture_home_dir`).
29pub fn agentmux_home() -> Option<std::path::PathBuf> {
30    HOME_DIR.get().cloned().flatten()
31}
32
33/// The account-wide AgentMux root, for code that means `~/.agentmux` itself
34/// (`archives/`, `channels/`, `shared/`). Inside srv, after startup,
35/// `agentmux_common::data_paths::agentmux_root()` answers with this version's
36/// data directory instead (see [`HOME_DIR`]); this answers with the root
37/// captured before that. Only when no capture ran (tests, tools that don't
38/// run srv's `main`) does it ask the resolver; a capture that ran and found
39/// no root stays `None`, since asking now would get the data directory.
40pub fn agentmux_root() -> Option<std::path::PathBuf> {
41    match HOME_DIR.get() {
42        Some(captured) => captured.clone(),
43        None => agentmux_common::data_paths::agentmux_root().ok(),
44    }
45}
46
47/// The `srvinfo` event: srv's version, and the machine it runs on.
48#[derive(Debug, Clone, serde::Serialize, ts_rs::TS)]
49#[ts(export, export_to = "../../../frontend/types/rpc/")]
50#[serde(rename_all = "camelCase")]
51pub struct SrvInfo {
52    pub version: String,
53    /// srv's OS, as `process.platform` names it (`darwin`, `win32`, `linux`).
54    pub platform: String,
55    pub user_name: String,
56    pub host_name: String,
57    /// The account-wide AgentMux root on srv's machine (`~/.agentmux`, or its override).
58    pub home_dir: Option<String>,
59}
60
61/// The `data` of the `srvinfo` event.
62pub fn srv_info(version: &str, host_name: &str) -> SrvInfo {
63    SrvInfo {
64        version: version.to_string(),
65        platform: agentmux_common::platform_name::platform_name().to_string(),
66        user_name: whoami::username(),
67        host_name: host_name.to_string(),
68        home_dir: agentmux_home().map(|p| p.to_string_lossy().into_owned()),
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn srv_info_names_the_machine_and_the_version() {
78        let info = serde_json::to_value(srv_info("1.2.3", "box")).unwrap();
79        assert_eq!(info["version"], "1.2.3");
80        assert_eq!(info["hostName"], "box");
81        assert_eq!(
82            info["platform"],
83            agentmux_common::platform_name::platform_name()
84        );
85        assert_eq!(info["userName"], whoami::username());
86        assert!(info.get("homeDir").is_some());
87    }
88}