agentmux_srv_core/backend/
swarm_cloud.rs1use super::agent_state::AgentState;
12use super::lan::discovery::LanInstance;
13use super::runtime_adapter::RuntimeKind;
14use super::swarm_remote::{fnv1a32, RemoteAgent, RemoteChannel, RemoteHost, SwarmOtherInstancesResult};
15use crate::muxbus::cloud_installs::{CloudInstall, CloudView};
16use crate::muxbus::cloud_installs::NewerInstall;
17use crate::muxbus::install_presence::InstallPresence;
18
19pub const STALE_AFTER_MS: u64 = 3 * 60_000;
21pub const HIDE_AFTER_MS: u64 = 10 * 60_000;
23
24pub fn add_cloud(snapshot: &mut SwarmOtherInstancesResult, view: Option<&CloudView>, own_wan_id: Option<&str>, lan: &[LanInstance], now_ms: u64) {
27 let Some(view) = view else {
28 snapshot.cloud = Some("off");
29 return;
30 };
31 snapshot.cloud = Some(view.state.label());
32 let mut here: Vec<String> = vec![snapshot.channel.to_lowercase()];
34 for host in snapshot.hosts.iter().filter(|h| h.tier == "host") {
35 here.extend(host.channels.iter().map(|c| c.channel.to_lowercase()));
36 }
37 let hosts = cloud_hosts(&view.installs, own_wan_id, &snapshot.hostname, &here, lan, now_ms);
38 snapshot.hosts.extend(hosts);
39 let newer = newer_hosts(&view.newer, own_wan_id, &snapshot.hostname, &here, lan, now_ms);
40 snapshot.hosts.extend(newer);
41}
42
43pub fn newer_hosts(newer: &[NewerInstall], own_wan_id: Option<&str>, hostname: &str, channels_here: &[String], lan: &[LanInstance], now_ms: u64) -> Vec<RemoteHost> {
47 let same = |a: &str, b: &str| a.trim().eq_ignore_ascii_case(b.trim());
48 let mut hosts: Vec<RemoteHost> = newer
49 .iter()
50 .filter(|n| own_wan_id != Some(n.instance_id.as_str()) && now_ms.saturating_sub(n.seen_at_ms) <= HIDE_AFTER_MS)
51 .filter(|n| !(same(&n.hostname, hostname) && channels_here.iter().any(|ch| same(ch, &n.channel))))
52 .filter(|n| !lan.iter().any(|p| same(&p.hostname, &n.hostname) && same(&p.channel, &n.channel)))
53 .map(|n| newer_host_of(n, now_ms))
54 .collect();
55 hosts.sort_by(|a, b| a.display_name.to_lowercase().cmp(&b.display_name.to_lowercase()));
56 hosts
57}
58
59fn newer_host_of(n: &NewerInstall, now_ms: u64) -> RemoteHost {
60 let needs = if n.app_version.is_empty() { "needs a newer AgentMux".to_string() } else { format!("needs AgentMux {} or newer", n.app_version) };
61 RemoteHost {
62 host_id: format!("cloud:{}#{:08x}", n.hostname.to_lowercase(), fnv1a32(&n.instance_id)),
63 display_name: format!("{} ({needs})", n.hostname),
64 tier: "cloud",
65 channels: vec![RemoteChannel {
66 channel: n.channel.clone(),
67 seen_at_ms: n.seen_at_ms,
68 stale: now_ms.saturating_sub(n.seen_at_ms) > STALE_AFTER_MS,
69 os: n.os.clone(),
70 agents: Vec::new(),
71 route: None,
72 paired_computer: None,
73 }],
74 }
75}
76
77pub fn cloud_hosts(
80 installs: &[CloudInstall],
81 own_wan_id: Option<&str>,
82 hostname: &str,
83 channels_here: &[String],
84 lan: &[LanInstance],
85 now_ms: u64,
86) -> Vec<RemoteHost> {
87 let same = |a: &str, b: &str| a.trim().eq_ignore_ascii_case(b.trim());
88 let mut hosts: Vec<RemoteHost> = installs
89 .iter()
90 .filter(|c| shown(c, own_wan_id, now_ms))
91 .filter(|c| !(same(&c.record.hostname, hostname) && channels_here.iter().any(|ch| same(ch, &c.record.channel))))
92 .filter(|c| !lan.iter().any(|p| on_the_lan(p, &c.record) || replaced_by(p, c, now_ms)))
93 .map(|c| host_of(c, now_ms))
94 .collect();
95 hosts.sort_by(|a, b| (a.display_name.to_lowercase(), &a.channels[0].channel).cmp(&(b.display_name.to_lowercase(), &b.channels[0].channel)));
96 hosts
97}
98
99fn on_the_lan(peer: &LanInstance, record: &InstallPresence) -> bool {
106 let same = |a: &str, b: &str| a.trim().eq_ignore_ascii_case(b.trim());
107 !peer.channel.is_empty()
108 && same(&peer.hostname, &record.hostname)
109 && same(&peer.channel, &record.channel)
110 && (peer.install_id.is_empty() || peer.install_id == record.instance_id)
111}
112
113fn replaced_by(peer: &LanInstance, install: &CloudInstall, now_ms: u64) -> bool {
119 let same = |a: &str, b: &str| a.trim().eq_ignore_ascii_case(b.trim());
120 let record = &install.record;
121 !peer.channel.is_empty()
122 && !peer.install_id.is_empty()
123 && peer.install_id != record.instance_id
124 && same(&peer.hostname, &record.hostname)
125 && same(&peer.channel, &record.channel)
126 && now_ms.saturating_sub(install.seen_at_ms) > STALE_AFTER_MS
127}
128
129fn shown(install: &CloudInstall, own_wan_id: Option<&str>, now_ms: u64) -> bool {
131 own_wan_id != Some(install.record.instance_id.as_str()) && now_ms.saturating_sub(install.seen_at_ms) <= HIDE_AFTER_MS
132}
133
134pub fn own_wan_id() -> Option<String> {
136 let wan = super::storage::wan_identity::global()?;
137 wan.instance_ensure(&super::reactive::registry::local_host_label()).ok().map(|instance| instance.instance_id)
138}
139
140pub fn agent_state<'a>(view: &'a CloudView, name: &str, own_wan_id: Option<&str>, now_ms: u64) -> Option<&'a str> {
144 let seen = view.installs.iter().filter(|c| shown(c, own_wan_id, now_ms));
145 let with_agent = seen.filter_map(|c| c.record.agents.iter().find(|a| a.name.eq_ignore_ascii_case(name)).map(|a| (c.seen_at_ms, a)));
146 with_agent.max_by_key(|(seen_at_ms, _)| *seen_at_ms).and_then(|(_, a)| a.state.as_deref())
147}
148
149fn host_of(install: &CloudInstall, now_ms: u64) -> RemoteHost {
150 let record = &install.record;
151 let agents = record
152 .agents
153 .iter()
154 .map(|a| RemoteAgent {
155 name: a.name.clone(),
156 block_id: String::new(),
157 status: a.state.as_deref().and_then(|s| serde_json::from_value::<AgentState>(serde_json::Value::String(s.to_string())).ok()),
158 line: a.line.as_deref().and_then(super::agent_line::clean_line),
159 provider: None,
160 seat: None,
161 account: None,
162 account_label: None,
163 runtime: RuntimeKind::parse(&a.kind),
164 })
165 .collect();
166 RemoteHost {
167 host_id: format!("cloud:{}#{:08x}", record.hostname.to_lowercase(), fnv1a32(&record.instance_id)),
169 display_name: record.hostname.clone(),
170 tier: "cloud",
171 channels: vec![RemoteChannel {
172 channel: record.channel.clone(),
173 seen_at_ms: install.seen_at_ms,
174 stale: now_ms.saturating_sub(install.seen_at_ms) > STALE_AFTER_MS,
175 os: record.os.clone(),
176 agents,
177 route: None,
178 paired_computer: None,
179 }],
180 }
181}
182
183#[cfg(test)]
184mod tests;