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 the old
6/// agentmux-cef-X.Y.Z.exe pattern for backwards compat, then plain
7/// agentmux-cef.exe (dev mode).
8pub(crate) fn find_cef_binary(runtime_dir: &std::path::Path) -> std::path::PathBuf {
9    let ext = if cfg!(target_os = "windows") { ".exe" } else { "" };
10
11    let versioned = format!("agentmux-{}{}", env!("CARGO_PKG_VERSION"), ext);
12    let versioned_path = runtime_dir.join(&versioned);
13    if versioned_path.exists() {
14        return versioned_path;
15    }
16
17    if let Ok(entries) = std::fs::read_dir(runtime_dir) {
18        let prefix = "agentmux-";
19        let cef_prefix = "agentmux-cef";
20        for entry in entries.flatten() {
21            let name = entry.file_name();
22            let name = name.to_string_lossy();
23            if name.starts_with(prefix)
24                && !name.starts_with(cef_prefix)
25                && !name.starts_with("agentmux-srv")
26                // CRITICAL for the flat dev layout (macOS/Linux Phase 1):
27                // launcher + host + srv share one dir, so the launcher
28                // binary itself matches `agentmux-*`. Without this guard
29                // the launcher resolves ITSELF as the host and spawns a
30                // recursive launcher fork bomb. On Windows the launcher
31                // lives at the root (not in runtime/), so this is a no-op.
32                && !name.starts_with("agentmux-launcher")
33                && name.ends_with(ext)
34            {
35                return entry.path();
36            }
37        }
38    }
39
40    let versioned_old = format!("agentmux-cef-{}{}", env!("CARGO_PKG_VERSION"), ext);
41    let versioned_old_path = runtime_dir.join(&versioned_old);
42    if versioned_old_path.exists() {
43        return versioned_old_path;
44    }
45
46    runtime_dir.join(format!("agentmux-cef{}", ext))
47}