Skip to main content

agentmux_srv/bootstrap/
shared_root.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A srv given a data folder of its own must not write into the real shared folder
5//! by accident.
6//!
7//! The shared folder (`shared/`: agent definitions, accounts, the shared store) follows
8//! the AgentMux home, not the data folder. A srv started with only a data folder
9//! outside the home (`AGENTMUX_DATA_DIR` or `--wavedata`) therefore kept its own
10//! databases in that folder and still wrote the agents it created into the real
11//! `~/.agentmux/shared`. Where shared data goes has to be said: by a home override
12//! (an isolated home) or by `AGENTMUX_SHARED_DIR` (the launcher always exports it).
13
14use std::path::Path;
15
16/// Why srv must not start, or `None`. `home` is the AgentMux home as it was when srv started;
17/// `home_override` and `shared_dir_set` say whether `AGENTMUX_HOME_OVERRIDE` and
18/// `AGENTMUX_SHARED_DIR` were given.
19pub(super) fn refusal(
20    data_home: &Path,
21    home: Option<&Path>,
22    home_override: bool,
23    shared_dir_set: bool,
24) -> Option<String> {
25    if data_home.as_os_str().is_empty() || home_override || shared_dir_set {
26        return None;
27    }
28    let home = home?;
29    if data_home.starts_with(home) {
30        return None;
31    }
32    Some(format!(
33        "the data folder {} is outside the AgentMux home {}, so the shared folder ({}) would be the real one. \
34         Start it on an isolated home (AGENTMUX_HOME_OVERRIDE=<folder>), \
35         or set AGENTMUX_SHARED_DIR to the shared folder to use on purpose.",
36        data_home.display(),
37        home.display(),
38        home.join("shared").display(),
39    ))
40}
41
42/// Before any shared file is opened: [`refuse_unlisted_shared_root`], then claim the shared folder
43/// for this srv's version (`shared_guard`).
44pub(super) fn guard_shared_folder(data_home: &Path, version: &str) {
45    refuse_unlisted_shared_root(data_home);
46    super::shared_guard::claim_at_boot(version);
47}
48
49/// [`refusal`] for this process; exits with the reason on stderr when it applies.
50pub(super) fn refuse_unlisted_shared_root(data_home: &Path) {
51    let set = |name| std::env::var_os(name).is_some_and(|v| !v.is_empty());
52    let home = crate::srv_info::agentmux_home();
53    if let Some(why) = refusal(
54        data_home,
55        home.as_deref(),
56        set("AGENTMUX_HOME_OVERRIDE"),
57        set("AGENTMUX_SHARED_DIR"),
58    ) {
59        tracing::error!("startup: {why}");
60        eprintln!("agentmux-srv: {why}");
61        std::process::exit(1);
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use std::path::PathBuf;
69
70    fn home() -> PathBuf {
71        PathBuf::from("/home/me/.agentmux")
72    }
73
74    #[test]
75    fn a_data_folder_outside_the_home_with_nothing_said_about_shared_refuses() {
76        let why = refusal(Path::new("/tmp/iso"), Some(&home()), false, false).expect("refuses");
77        assert!(
78            why.contains("/tmp/iso")
79                && why.contains("AGENTMUX_HOME_OVERRIDE")
80                && why.contains("AGENTMUX_SHARED_DIR"),
81            "{why}"
82        );
83    }
84
85    #[test]
86    fn a_home_override_or_an_explicit_shared_dir_says_where_shared_data_goes() {
87        assert_eq!(
88            refusal(Path::new("/tmp/iso"), Some(&home()), true, false),
89            None
90        );
91        assert_eq!(
92            refusal(Path::new("/tmp/iso"), Some(&home()), false, true),
93            None
94        );
95    }
96
97    #[test]
98    fn a_data_folder_inside_the_home_or_none_at_all_is_fine() {
99        assert_eq!(
100            refusal(
101                &home().join("channels/stable/versions/1/data"),
102                Some(&home()),
103                false,
104                false
105            ),
106            None
107        );
108        assert_eq!(refusal(Path::new(""), Some(&home()), false, false), None);
109        assert_eq!(refusal(Path::new("/tmp/iso"), None, false, false), None);
110    }
111
112    #[test]
113    fn a_folder_that_only_shares_the_homes_name_prefix_is_outside_it() {
114        assert!(refusal(
115            Path::new("/home/me/.agentmux-iso/data"),
116            Some(&home()),
117            false,
118            false
119        )
120        .is_some());
121    }
122}