agentmux_srv/server/reactive/
registration_flags.rs1use super::{is_self_registration, AppState, MismatchAuditSummary, RemoteRegistrationEntry};
5use crate::backend::reactive::registry as agent_registry;
6
7fn remote_claims(state: &AppState, agent_id: &str) -> Vec<RemoteRegistrationEntry> {
15 crate::registry::resolve_shared_reactive_dir()
16 .map(|shared_dir| {
17 agent_registry::lookup_all_shared(&shared_dir, agent_id)
18 .into_iter()
19 .filter(|e| !is_self_registration(&e.local_url, &state.local_web_url))
20 .filter(|e| agent_registry::pid_alive(e.pid))
21 .map(|e| RemoteRegistrationEntry {
22 channel: e.channel,
23 pid: e.pid,
24 updated_at: e.updated_at,
25 })
26 .collect::<Vec<_>>()
27 })
28 .unwrap_or_default()
29}
30
31pub(super) fn recent_mismatch_for(
36 audit: &[crate::backend::reactive::AuditLogEntry],
37 agent_id: &str,
38 block_id: Option<&str>,
39) -> Option<MismatchAuditSummary> {
40 let target_lower = agent_id.to_lowercase();
41 audit
42 .iter()
43 .find(|e| {
44 e.outcome.as_deref() == Some("identity-mismatch")
45 && match block_id {
46 Some(b) => e.block_id == b,
47 None => e.target_agent.to_lowercase() == target_lower,
48 }
49 })
50 .map(|e| MismatchAuditSummary {
51 timestamp: e.timestamp,
52 block_id: e.block_id.clone(),
53 error_message: e.error_message.clone(),
54 })
55}
56
57#[derive(serde::Serialize, serde::Deserialize, ts_rs::TS)]
60#[ts(export, export_to = "../../../frontend/types/rpc/")]
61pub(super) struct RegistrationFlag {
62 agent_id: String,
63 block_id: String,
64 remote: Vec<RemoteRegistrationEntry>,
65 recent_mismatch: Option<MismatchAuditSummary>,
66}
67
68#[derive(serde::Serialize, serde::Deserialize, ts_rs::TS)]
71#[ts(export, export_to = "../../../frontend/types/rpc/")]
72pub(super) struct ReactiveRegistrationFlagsResult {
73 flags: Vec<RegistrationFlag>,
74}
75
76#[derive(serde::Serialize, serde::Deserialize, ts_rs::TS)]
77#[ts(export, export_to = "../../../frontend/types/rpc/")]
78pub(super) struct ReactiveRegistrationFlagsParams {}
79
80fn flags(state: &AppState) -> ReactiveRegistrationFlagsResult {
81 let audit = state.reactive_handler.get_audit_log(100);
82 let flags = state
83 .reactive_handler
84 .list_agents()
85 .into_iter()
86 .filter_map(|a| {
87 let remote = remote_claims(state, &a.agent_id);
88 let recent_mismatch = recent_mismatch_for(&audit, &a.agent_id, Some(&a.block_id));
89 (!remote.is_empty() || recent_mismatch.is_some()).then(|| RegistrationFlag {
90 agent_id: a.agent_id,
91 block_id: a.block_id,
92 remote,
93 recent_mismatch,
94 })
95 })
96 .collect();
97 ReactiveRegistrationFlagsResult { flags }
98}
99
100pub(super) fn register(
101 engine: &std::sync::Arc<crate::backend::rpc::engine::WshRpcEngine>,
102 state: AppState,
103) {
104 engine.register_typed(
105 "reactive.registration_flags",
106 move |_: ReactiveRegistrationFlagsParams, _ctx| {
107 let state = state.clone();
108 async move { Ok(flags(&state)) }
109 },
110 );
111}
112
113pub(super) fn problems_of(
116 state: &AppState,
117 local: &Option<crate::backend::reactive::AgentRegistration>,
118 agent_id: &str,
119) -> (Vec<RemoteRegistrationEntry>, Option<MismatchAuditSummary>) {
120 let audit = state.reactive_handler.get_audit_log(100);
121 let block_id = local.as_ref().map(|l| l.block_id.as_str());
122 (
123 remote_claims(state, agent_id),
124 recent_mismatch_for(&audit, agent_id, block_id),
125 )
126}