agentmux_srv_core/backend/reactive/
cli_native_audit.rs1use super::handler::{Handler, ReactiveHandler};
10use super::types::{AuditLogEntry, CliNativeAudit};
11use super::{audit_hash, now_unix_millis};
12use crate::backend::cli_native::reader::Event;
13
14const PREVIEW_CHARS: usize = 200;
16
17fn preview(text: &str) -> String {
19 let first = text.lines().map(str::trim).find(|l| !l.is_empty()).unwrap_or_default();
20 match first.char_indices().nth(PREVIEW_CHARS) {
21 Some((cut, _)) => format!("{}…", &first[..cut]),
22 None => first.to_string(),
23 }
24}
25
26fn entry(ev: &Event) -> AuditLogEntry {
27 AuditLogEntry {
28 timestamp: now_unix_millis(),
29 source_agent: Some(ev.source.clone()),
30 target_agent: ev.target.clone(),
31 block_id: ev.block_id.clone(),
32 message_hash: audit_hash(&ev.text),
33 message_length: ev.text.len(),
34 success: true,
35 error_message: None,
36 request_id: ev.msg_id.clone(),
37 outcome: Some(ev.state.to_string()),
38 reason: ev.reason.clone(),
39 event_kind: "cli_native".to_string(),
40 evicted_block: None,
41 evicted_agent: None,
42 audit_source_uid: String::new(),
43 lan_claimed_uid: String::new(),
44 wan: None,
45 cli_native: Some(CliNativeAudit {
46 seen_by: ev.seen_by.to_string(),
47 identity: ev.identity.to_string(),
48 claimed_name: ev.claimed_name.clone(),
49 cli_to_name: ev.cli_to_name.clone(),
50 sender_pid: ev.sender_pid,
51 from_mode: ev.from_mode.clone(),
52 preview: preview(&ev.text),
53 }),
54 turn: None,
55 host: None,
56 channel: None,
57 version: None,
58 tier: None,
59 trust: None,
60 msg_id: None,
61 prompt: None,
62 answer: None,
63 level: None,
64 standing: None,
65 }
66}
67
68impl Handler {
69 pub(super) fn log_cli_native(&mut self, ev: &Event) {
70 super::audit_store::push(&mut self.audit_log, super::AUDIT_LOG_MAX, entry(ev));
71 }
72}
73
74impl ReactiveHandler {
75 pub fn audit_cli_native(&self, ev: &Event) {
77 self.inner.lock().log_cli_native(ev);
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 fn ev(text: &str) -> Event {
86 Event {
87 block_id: "b1".into(),
88 msg_id: "m1".into(),
89 state: "read",
90 seen_by: "receiver",
91 source: "alpha".into(),
92 identity: "agent",
93 target: "beta".into(),
94 claimed_name: Some("alpha-7".into()),
95 cli_to_name: None,
96 sender_pid: Some(101),
97 from_mode: Some("bypass".into()),
98 text: text.into(),
99 reason: None,
100 }
101 }
102
103 #[test]
104 fn the_line_holds_a_hash_a_length_and_a_preview_never_the_text() {
105 let e = entry(&ev("\n first line here\nsecond line with a secret"));
106 let json = serde_json::to_string(&e).unwrap();
107 assert!(!json.contains("second line"), "{json}");
108 assert_eq!(e.event_kind, "cli_native");
109 assert_eq!((e.request_id.as_str(), e.outcome.as_deref(), e.message_length), ("m1", Some("read"), 44));
110 let c = e.cli_native.unwrap();
111 assert_eq!((c.preview.as_str(), c.identity.as_str(), c.sender_pid), ("first line here", "agent", Some(101)));
112 }
113
114 #[test]
115 fn a_long_first_line_is_cut() {
116 let p = preview(&"é".repeat(500));
117 assert_eq!(p.chars().count(), PREVIEW_CHARS + 1);
118 assert!(p.ends_with('…'));
119 }
120
121 #[test]
122 fn the_handlers_ring_and_route_see_the_line() {
123 let handler = ReactiveHandler::new();
124 handler.audit_cli_native(&ev("hello"));
125 let log = handler.get_audit_log(1);
126 assert_eq!((log[0].event_kind.as_str(), log[0].source_agent.as_deref(), log[0].target_agent.as_str()), ("cli_native", Some("alpha"), "beta"));
127 }
128}