Skip to main content

agentmux_srv/server/reactive/
flow_notes.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Tells the Atlas map what [`deliver`](super::deliver) did with a message: `backend::message_flow`. Ids, hosts and the
5//! outcome only; nothing here reads the message's text or files.
6
7use super::*;
8use crate::backend::message_flow::{self, MessageFlowOutcome, MessageFlowVia, Seen};
9
10/// How the message came to this srv: by the tier it carries.
11pub(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
20/// What an answer to a sender says became of the message.
21pub(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
31/// `req` was taken by its receiver here.
32pub(super) fn note_delivered(state: &AppState, req: &InjectionRequest) {
33    note(state, req, arrival_via(req), MessageFlowOutcome::Delivered, None);
34}
35
36/// `req` was given to the cloud relay, which knows it as `injection_id`; gives `answer` back to return.
37pub(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
42/// Reports what `answer` made of `req` where it arrived, and gives the answer back to return.
43pub(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
48/// The host of the LAN peer that runs `agent`, when the peer list has it.
49pub(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
59/// Reports `req` as handled here: `to_host` is where its receiver runs, when known (`None` is this computer for a
60/// local result). A message with no sender (a cron job, a bridge) has no line to draw and says nothing.
61pub(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
65/// [`note`] under `msg_id`, for a message the relay gave a new id (the receiving srv knows it by that one).
66pub(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}