Skip to main content

agentmux_common/
build_kind.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What kind of build this binary is: a published release, a local package
5//! build, or a source-tree dev build.
6//!
7//! It is a fact of the binary, never of the data channel it runs on. An
8//! in-app upgrade keeps the data channel of the build it replaced
9//! (`runtime_mode::recorded_channel`), so a published release that a local
10//! build upgraded to runs on a `local-…` channel for good. Anything that
11//! says or decides "this is a local build" from the channel name is wrong
12//! there. Ask this instead, and show the channel only as where the data is.
13
14use std::path::Path;
15
16/// The kind of build a binary is.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum BuildKind {
19    /// Built by the release pipeline: what the website, the stores and the
20    /// in-app upgrade hand out.
21    Release,
22    /// Built on someone's machine by the packaging scripts (`task package`).
23    Local,
24    /// Run from a source tree (`task dev`, `cargo run`).
25    Dev,
26}
27
28impl BuildKind {
29    /// The token sent to peers and shown in listings: `release`, `local` or `dev`.
30    pub fn label(self) -> &'static str {
31        match self {
32            BuildKind::Release => "release",
33            BuildKind::Local => "local",
34            BuildKind::Dev => "dev",
35        }
36    }
37
38    /// The kind a peer's token names; `None` for anything else, including the
39    /// empty string of a peer from before the kind was advertised.
40    pub fn from_label(label: &str) -> Option<Self> {
41        match label {
42            "release" => Some(BuildKind::Release),
43            "local" => Some(BuildKind::Local),
44            "dev" => Some(BuildKind::Dev),
45            _ => None,
46        }
47    }
48}
49
50/// The kind of a build from what its binary carries:
51/// - `dev`: it runs from a source tree's output folder;
52/// - `portable_label`: the label in a portable folder's
53///   `agentmux-portable.marker` (`runtime_mode::portable_build_label`). A
54///   release portable's label is its plain version (`0.60.22`); a local one's
55///   carries the commit and a build stamp (`0.60.22+g2a5424fe2.…`);
56/// - `made_for_channel`: the channel the packaging script compiled in as the
57///   build's default. The release pipeline compiles in `stable`; a local
58///   package build gets a `local-…` channel of its own. This is a constant of
59///   the binary: an upgrade's recorded channel does not change it.
60pub fn build_kind_of(dev: bool, portable_label: Option<&str>, made_for_channel: &str) -> BuildKind {
61    if dev {
62        BuildKind::Dev
63    } else if portable_label.is_some_and(|l| !l.trim().is_empty() && !is_plain_version(l.trim())) || made_for_channel != "stable" {
64        BuildKind::Local
65    } else {
66        BuildKind::Release
67    }
68}
69
70/// `x.y.z`, three numbers and nothing else: what a release calls itself.
71fn is_plain_version(label: &str) -> bool {
72    let mut parts = label.split('.');
73    let three = (0..3).all(|_| parts.next().is_some_and(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit())));
74    three && parts.next().is_none()
75}
76
77/// The kind of the build whose binary sits in `exe_dir`.
78pub fn build_kind_in(exe_dir: &Path) -> BuildKind {
79    let dev = crate::runtime_mode::exe_dir_is_dev(exe_dir);
80    let label = crate::runtime_mode::portable_build_label(exe_dir);
81    build_kind_of(dev, label.as_deref(), crate::data_paths::build_channel_default())
82}
83
84/// The kind of the running binary. Read once: the binary does not change
85/// under a running process.
86pub fn build_kind() -> BuildKind {
87    static KIND: std::sync::OnceLock<BuildKind> = std::sync::OnceLock::new();
88    *KIND.get_or_init(|| {
89        let dir = std::env::current_exe().ok().and_then(|p| p.parent().map(Path::to_path_buf));
90        match dir {
91            Some(dir) => build_kind_in(&dir),
92            // No path to judge by: only the compiled-in channel is left.
93            None => build_kind_of(false, None, crate::data_paths::build_channel_default()),
94        }
95    })
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101
102    #[test]
103    fn a_release_is_a_release_whatever_channel_it_runs_on() {
104        // The function takes no running channel at all: a release that kept a
105        // local build's data channel through an upgrade is still a release.
106        assert_eq!(build_kind_of(false, None, "stable"), BuildKind::Release);
107    }
108
109    #[test]
110    fn a_local_package_build_is_local_by_its_marker_or_its_compiled_channel() {
111        assert_eq!(build_kind_of(false, Some("0.60.22+g2a5424fe2.20261110T070000.1"), "local-main-b28b7a-0000000a"), BuildKind::Local);
112        // An AppImage or a package has no marker file; the compiled-in channel still says so.
113        assert_eq!(build_kind_of(false, None, "local-main-b28b7a-0000000a"), BuildKind::Local);
114        // A stamped label says local even when `stable` was compiled in.
115        assert_eq!(build_kind_of(false, Some("0.60.22+gabc.1"), "stable"), BuildKind::Local);
116        assert_eq!(build_kind_of(false, Some("  "), "stable"), BuildKind::Release);
117    }
118
119    #[test]
120    fn a_release_portable_carries_a_marker_with_its_plain_version() {
121        assert_eq!(build_kind_of(false, Some("0.60.22"), "stable"), BuildKind::Release);
122        for not_plain in ["0.60", "0.60.22.1", "0.60.x", "v0.60.22", "0.60.22-rc1"] {
123            assert!(!is_plain_version(not_plain), "{not_plain}");
124        }
125    }
126
127    #[test]
128    fn a_source_tree_build_is_dev_first() {
129        assert_eq!(build_kind_of(true, None, "stable"), BuildKind::Dev);
130        assert_eq!(build_kind_of(true, Some("x"), "local-x-000000-00000000"), BuildKind::Dev);
131    }
132
133    #[test]
134    fn labels_round_trip_and_unknown_tokens_are_refused() {
135        for kind in [BuildKind::Release, BuildKind::Local, BuildKind::Dev] {
136            assert_eq!(BuildKind::from_label(kind.label()), Some(kind));
137        }
138        for other in ["", "stable", "Release", "local-main-b28b7a-0000000a"] {
139            assert_eq!(BuildKind::from_label(other), None, "{other:?}");
140        }
141    }
142}