Skip to main content

agentmux_srv_core/backend/reactive/
sent_audit.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The sender's half of a message's trail: the `sent` line written where a message is handed to this srv. The receiving
5//! srv writes the line it writes today under the same message id (`msg_id`), and `audit.query` joins the two into one
6//! row. A message another srv handed over (a peer, another channel, the cloud) has no `sent` line here: its sender wrote
7//! it.
8
9use super::types::{AuditLogEntry, InjectionRequest};
10use super::{audit_hash, now_unix_millis};
11
12/// `event_kind` of the line, and its `outcome`.
13pub const SENT: &str = "sent";
14
15/// The line for `req`, a message handed over by a caller on this computer. `None` unless it has an id and came
16/// straight from a local caller (the `host` tier, not forwarded): the id is what the receiving end's line is joined by.
17fn line(req: &InjectionRequest) -> Option<AuditLogEntry> {
18    let id = req.request_id.clone().filter(|id| !id.is_empty())?;
19    if req.delivery_tier.as_deref().unwrap_or("host") != "host" || req.forward_hops != 0 {
20        return None;
21    }
22    Some(AuditLogEntry {
23        timestamp: now_unix_millis(),
24        source_agent: req.source_agent.clone(),
25        target_agent: req.target_agent.clone(),
26        block_id: String::new(),
27        message_hash: audit_hash(&req.message),
28        message_length: req.message.len(),
29        request_id: id.clone(),
30        outcome: Some(SENT.to_string()),
31        event_kind: SENT.to_string(),
32        tier: Some("host".to_string()),
33        msg_id: Some(id),
34        ..Default::default()
35    })
36}
37
38/// Writes the `sent` line of `req` to the audit file, when it has one, after giving a request with no id the one it will
39/// be delivered under. Called from `admit` once the message is accepted: after the replay check (which treats a signed
40/// message with no id as one it cannot count, so it must see the request as it came) and the refusals of files and of an
41/// empty reply, and before a hold, so a held message is joined to its later delivery. Not kept in the in-memory ring: that serves the deliveries, and a message would take two places in it.
42pub fn record(req: &mut InjectionRequest) {
43    req.request_id = Some(req.request_id.take().filter(|id| !id.is_empty()).unwrap_or_else(|| uuid::Uuid::new_v4().to_string()));
44    if let Some(entry) = line(req) {
45        super::audit_store::record(&entry);
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    fn request(tier: Option<&str>, hops: u8, id: Option<&str>) -> InjectionRequest {
54        InjectionRequest {
55            target_agent: "opaz".into(),
56            message: "hello".into(),
57            source_agent: Some("loap".into()),
58            delivery_tier: tier.map(str::to_string),
59            forward_hops: hops,
60            request_id: id.map(str::to_string),
61            ..Default::default()
62        }
63    }
64
65    #[test]
66    fn a_message_a_local_caller_handed_over_has_a_sent_line_under_its_id() {
67        let e = line(&request(Some("host"), 0, Some("m-1"))).unwrap();
68        assert_eq!((e.event_kind.as_str(), e.outcome.as_deref(), e.msg_id.as_deref(), e.request_id.as_str()), ("sent", Some("sent"), Some("m-1"), "m-1"));
69        assert_eq!((e.source_agent.as_deref(), e.target_agent.as_str(), e.tier.as_deref(), e.message_length), (Some("loap"), "opaz", Some("host"), 5));
70        assert!(line(&request(None, 0, Some("m-1"))).is_some(), "no tier means a local caller");
71    }
72
73    #[test]
74    fn a_message_another_srv_handed_over_or_with_no_id_has_none() {
75        for (tier, hops, id) in [(Some("channel"), 0, Some("m")), (Some("lan"), 0, Some("m")), (Some("wan"), 0, Some("m")), (Some("host"), 1, Some("m")), (Some("host"), 0, None), (Some("host"), 0, Some(""))] {
76            assert!(line(&request(tier, hops, id)).is_none(), "{tier:?} {hops} {id:?}");
77        }
78    }
79}