agentmux_srv/server/reactive/
flow_notes.rs1use super::*;
8use crate::backend::message_flow::{self, MessageFlowOutcome, MessageFlowVia, Seen};
9
10pub(super) fn arrival_via(req: &InjectionRequest) -> MessageFlowVia {
12 match req.delivery_tier.as_deref() {
13 Some("channel") => MessageFlowVia::Channel,
14 Some("lan") => MessageFlowVia::Lan,
15 Some("wan") => MessageFlowVia::Wan,
16 _ => MessageFlowVia::Host,
17 }
18}
19
20pub(super) fn answer_outcome(answer: &serde_json::Value) -> MessageFlowOutcome {
22 if answer.get("held").and_then(|h| h.as_bool()) == Some(true) {
23 MessageFlowOutcome::Held
24 } else if answer.get("success").and_then(|s| s.as_bool()) == Some(true) {
25 MessageFlowOutcome::Delivered
26 } else {
27 MessageFlowOutcome::Refused
28 }
29}
30
31pub(super) fn note_delivered(state: &AppState, req: &InjectionRequest) {
33 note(state, req, arrival_via(req), MessageFlowOutcome::Delivered, None);
34}
35
36pub(super) fn queued(state: &AppState, req: &InjectionRequest, injection_id: &str, answer: serde_json::Value) -> serde_json::Value {
38 note_as(state, req, injection_id, MessageFlowVia::Wan, MessageFlowOutcome::Queued, None);
39 answer
40}
41
42pub(super) fn noted(state: &AppState, req: &InjectionRequest, answer: serde_json::Value) -> serde_json::Value {
44 note(state, req, arrival_via(req), answer_outcome(&answer), None);
45 answer
46}
47
48pub(super) fn lan_host_of(state: &AppState, agent: &str) -> Option<String> {
50 state
51 .lan_discovery
52 .get_instances()
53 .into_iter()
54 .find(|peer| peer.agents.iter().any(|a| a.eq_ignore_ascii_case(agent)))
55 .map(|peer| peer.hostname)
56 .filter(|host| !host.is_empty())
57}
58
59pub(super) fn note(state: &AppState, req: &InjectionRequest, via: MessageFlowVia, outcome: MessageFlowOutcome, to_host: Option<&str>) {
62 note_as(state, req, req.request_id.as_deref().unwrap_or(""), via, outcome, to_host);
63}
64
65pub(super) fn note_as(state: &AppState, req: &InjectionRequest, msg_id: &str, via: MessageFlowVia, outcome: MessageFlowOutcome, to_host: Option<&str>) {
67 let Some(from) = req.source_agent.as_deref() else { return };
68 let here = crate::backend::reactive::registry::local_host_label();
69 let from_host = match arrival_via(req) {
70 MessageFlowVia::Host | MessageFlowVia::Channel => Some(here.clone()),
71 MessageFlowVia::Lan => lan_host_of(state, from),
72 MessageFlowVia::Wan => None,
73 };
74 message_flow::note(&Seen {
75 msg_id,
76 from,
77 from_host: from_host.as_deref(),
78 to: &req.target_agent,
79 to_host: Some(to_host.unwrap_or(&here)),
80 via,
81 outcome,
82 });
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88 use serde_json::json;
89
90 fn req(tier: Option<&str>) -> InjectionRequest {
91 InjectionRequest { delivery_tier: tier.map(str::to_string), ..Default::default() }
92 }
93
94 #[test]
95 fn a_message_arrives_by_the_tier_it_carries_and_a_local_caller_has_none() {
96 let via = |t| arrival_via(&req(t));
97 assert_eq!(via(None), MessageFlowVia::Host);
98 assert_eq!(via(Some("host")), MessageFlowVia::Host);
99 assert_eq!(via(Some("channel")), MessageFlowVia::Channel);
100 assert_eq!(via(Some("lan")), MessageFlowVia::Lan);
101 assert_eq!(via(Some("wan")), MessageFlowVia::Wan);
102 }
103
104 #[test]
105 fn an_answer_is_held_before_it_is_delivered_and_anything_else_is_refused() {
106 assert_eq!(answer_outcome(&json!({ "success": true, "request_id": "m" })), MessageFlowOutcome::Delivered);
107 assert_eq!(answer_outcome(&json!({ "success": true, "held": true })), MessageFlowOutcome::Held);
108 assert_eq!(answer_outcome(&json!({ "success": false, "held": true })), MessageFlowOutcome::Held);
109 assert_eq!(answer_outcome(&json!({ "success": false, "error": "no" })), MessageFlowOutcome::Refused);
110 assert_eq!(answer_outcome(&json!({})), MessageFlowOutcome::Refused);
111 }
112}