agentmux_srv_core/backend/reactive/
sent_audit.rs1use super::types::{AuditLogEntry, InjectionRequest};
10use super::{audit_hash, now_unix_millis};
11
12pub const SENT: &str = "sent";
14
15fn 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
38pub 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}