Skip to main content

agentmux_srv_core/backend/lan/
peer_words.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A LAN peer in words: how it is installed and whether it runs agents. A host can run two
5//! installs (the Microsoft Store copy and a setup.exe one), and a peer that is an install with no
6//! agents answers every lookup of an agent with "not found"; saying which peer it was shows the
7//! real cause.
8
9use super::discovery::LanInstance;
10
11/// A peer's `os`, `install`, kind of build and WAN instance id from its mDNS TXT record: self-reported, so only a
12/// plain token is kept, of the build only a kind `BuildKind` knows, and of the id only one that is shaped like one.
13pub(crate) fn advertised(info: &mdns_sd::ServiceInfo) -> (String, String, String, String) {
14    let token = |key: &str| info.get_property_val_str(key).and_then(crate::backend::host_os::sanitize_os).unwrap_or_default();
15    (token("os"), token("install"), build_token(info.get_property_val_str("build")), install_id_token(info.get_property_val_str("install_id")))
16}
17
18/// The WAN instance id a peer advertises (`install_id`: 26 lowercase base32 characters, a hash of the install's
19/// public key), kept only when it is shaped like one; empty for a peer that predates advertising it. An
20/// identifier and nothing more: the peer chose it, and nothing here proves the peer holds the key behind it.
21pub(crate) fn install_id_token(said: Option<&str>) -> String {
22    said.map(str::trim)
23        .filter(|s| s.len() == 26 && s.bytes().all(|b| b.is_ascii_lowercase() || (b'2'..=b'7').contains(&b)))
24        .map(str::to_string)
25        .unwrap_or_default()
26}
27
28/// The kind of build a peer says it is, kept only when it is one of the tokens `BuildKind` knows
29/// (`release`, `local`, `dev`): self-reported, and shown to people and agents.
30pub(crate) fn build_token(said: Option<&str>) -> String {
31    said.map(str::trim)
32        .and_then(agentmux_common::build_kind::BuildKind::from_label)
33        .map(|k| k.label().to_string())
34        .unwrap_or_default()
35}
36
37/// How a build's kind is said in a message; empty when the peer didn't say.
38pub(crate) fn build_words(build: &str) -> &'static str {
39    match build {
40        "release" => "release",
41        "local" => "local build",
42        "dev" => "dev build",
43        _ => "",
44    }
45}
46
47/// A re-fired resolution often carries an empty TXT record: apply `new` only when it says something,
48/// so a stale re-fire never erases an install already known.
49pub(crate) fn keep_known(known: &mut String, new: String) {
50    if !new.is_empty() {
51        *known = new;
52    }
53}
54
55/// How an install is said in a message ("Microsoft Store"); empty when the peer didn't say or it is unknown.
56pub fn install_words(install: &str) -> &'static str {
57    match install {
58        "windows-store" => "Microsoft Store",
59        "windows-setup" => "installed",
60        "windows-portable" | "linux-portable" => "portable",
61        "macos-app" => "app",
62        "linux-appimage" => "AppImage",
63        "linux-package" => "package",
64        _ => "",
65    }
66}
67
68/// A peer in words, so the real cause of a missing agent shows: "Area54/stable (release, Microsoft Store, no agents)".
69/// `host/channel` is the peer's address (the channel is where its data lives); what kind of build it is
70/// comes from the build itself, so a release that kept a local build's channel still reads "release".
71/// "No agents" is said only of a peer that was asked and runs nobody.
72pub fn describe_peer(p: &LanInstance) -> String {
73    let host = p.hostname.trim();
74    let name = if p.channel.is_empty() { host.to_string() } else { format!("{host}/{}", p.channel) };
75    let mut notes: Vec<&str> = Vec::new();
76    let kind = build_words(&p.build);
77    if !kind.is_empty() {
78        notes.push(kind);
79    }
80    let how = install_words(&p.install);
81    if !how.is_empty() {
82        notes.push(how);
83    }
84    if p.last_polled_ok > 0 && p.agents.is_empty() {
85        notes.push("no agents");
86    }
87    if notes.is_empty() {
88        name
89    } else {
90        format!("{name} ({})", notes.join(", "))
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::install_id_token;
97
98    #[test]
99    fn an_install_id_is_kept_only_when_it_is_shaped_like_one() {
100        let id = "mw3am46w5weex4a4fqrc3avnua";
101        assert_eq!(install_id_token(Some(id)), id);
102        assert_eq!(install_id_token(Some(&format!(" {id} "))), id, "trimmed");
103        for bad in ["", "short", "MW3AM46W5WEEX4A4FQRC3AVNUA", "mw3am46w5weex4a4fqrc3avnu1", "mw3am46w5weex4a4fqrc3avnuaa", "mw3am46w5weex4a4fqrc3avné"] {
104            assert_eq!(install_id_token(Some(bad)), "", "{bad:?}");
105        }
106        assert_eq!(install_id_token(None), "");
107    }
108}