Skip to main content

agentmux_launcher/
binary_resolution.rs

1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4/// Find the CEF host binary in the runtime directory.
5/// Tries versioned name first (agentmux-X.Y.Z.exe), then any other
6/// agentmux-<digit>... name (an older versioned host), then the old
7/// agentmux-cef-X.Y.Z.exe pattern for backwards compat, then plain
8/// agentmux-cef.exe (dev mode).
9pub(crate) fn find_cef_binary(runtime_dir: &std::path::Path) -> std::path::PathBuf {
10    let ext = if cfg!(target_os = "windows") { ".exe" } else { "" };
11
12    let versioned = format!("agentmux-{}{}", env!("CARGO_PKG_VERSION"), ext);
13    let versioned_path = runtime_dir.join(&versioned);
14    if versioned_path.exists() {
15        return versioned_path;
16    }
17
18    if let Ok(entries) = std::fs::read_dir(runtime_dir) {
19        let prefix = "agentmux-";
20        let cef_prefix = "agentmux-cef";
21        for entry in entries.flatten() {
22            let name = entry.file_name();
23            let name = name.to_string_lossy();
24            if name.starts_with(prefix)
25                // Only a versioned name (agentmux-0.59.9) is a host. Anything
26                // else next to the host is a helper or a package script: the
27                // .deb and .rpm ship agentmux-install-update in this same
28                // directory, and with no digit test it was the one name left
29                // after the three exclusions below, so the launcher started
30                // it as the window and gave up after three exits (v0.60.3).
31                && name[prefix.len()..].starts_with(|c: char| c.is_ascii_digit())
32                && !name.starts_with(cef_prefix)
33                && !name.starts_with("agentmux-srv")
34                // CRITICAL for the flat dev layout (macOS/Linux Phase 1):
35                // launcher + host + srv share one dir, so the launcher
36                // binary itself matches `agentmux-*`. Without this guard
37                // the launcher resolves ITSELF as the host and spawns a
38                // recursive launcher fork bomb. On Windows the launcher
39                // lives at the root (not in runtime/), so this is a no-op.
40                && !name.starts_with("agentmux-launcher")
41                && name.ends_with(ext)
42            {
43                return entry.path();
44            }
45        }
46    }
47
48    let versioned_old = format!("agentmux-cef-{}{}", env!("CARGO_PKG_VERSION"), ext);
49    let versioned_old_path = runtime_dir.join(&versioned_old);
50    if versioned_old_path.exists() {
51        return versioned_old_path;
52    }
53
54    runtime_dir.join(format!("agentmux-cef{}", ext))
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    fn touch(dir: &std::path::Path, name: &str) {
62        std::fs::write(dir.join(name), b"").unwrap();
63    }
64
65    /// The host's name on this platform: Windows falls back to `agentmux-cef.exe`.
66    fn host_name() -> String {
67        format!("agentmux-cef{}", if cfg!(target_os = "windows") { ".exe" } else { "" })
68    }
69
70    /// The installed .deb/.rpm layout: host, launcher, srv and the upgrade
71    /// helper side by side. The helper must never be picked as the host.
72    #[test]
73    fn a_package_helper_next_to_the_host_is_not_the_host() {
74        let dir = tempfile::tempdir().unwrap();
75        for n in ["agentmux-cef", "agentmux-launcher", "agentmux-install-update", "agentmux-srv-0.60.3-linux.x64"] {
76            touch(dir.path(), n);
77        }
78        assert_eq!(find_cef_binary(dir.path()), dir.path().join(host_name()));
79    }
80
81    #[test]
82    fn any_agentmux_helper_is_ignored() {
83        let dir = tempfile::tempdir().unwrap();
84        for n in ["agentmux-cef", "agentmux-bashwrap", "agentmux-mcp", "agentmux-install-update"] {
85            touch(dir.path(), n);
86        }
87        assert_eq!(find_cef_binary(dir.path()), dir.path().join(host_name()));
88    }
89
90    #[test]
91    fn an_older_versioned_host_is_still_found() {
92        let dir = tempfile::tempdir().unwrap();
93        for n in ["agentmux-launcher", "agentmux-0.50.1", "agentmux-install-update"] {
94            touch(dir.path(), n);
95        }
96        let ext = if cfg!(target_os = "windows") { ".exe" } else { "" };
97        let want = format!("agentmux-0.50.1{ext}");
98        // On Windows the name needs the extension to match the scan.
99        if ext == ".exe" {
100            touch(dir.path(), &want);
101        }
102        assert_eq!(find_cef_binary(dir.path()), dir.path().join(want));
103    }
104
105    #[test]
106    fn the_exact_versioned_name_wins() {
107        let dir = tempfile::tempdir().unwrap();
108        let ext = if cfg!(target_os = "windows") { ".exe" } else { "" };
109        let exact = format!("agentmux-{}{ext}", env!("CARGO_PKG_VERSION"));
110        for n in [exact.as_str(), "agentmux-0.50.1", "agentmux-cef"] {
111            touch(dir.path(), n);
112        }
113        assert_eq!(find_cef_binary(dir.path()), dir.path().join(exact));
114    }
115}