agentmux_srv/server/remote_ops/
move_audit.rs1use crate::server::state::AppState;
12
13pub(crate) fn action(step: &str) -> String {
15 format!("agent.move.{step}")
16}
17
18pub(crate) fn row(state: &AppState, step: &str, actor: &str, agent: &str, move_id: &str, error: Option<&str>, note: &str) {
20 state.reactive_handler.log_fleet_action_audit(Some(actor), agent, "", &action(step), error.is_none(), error, move_id, Some(note));
21}
22
23pub(crate) fn size_text(bytes: u64) -> String {
25 const MIB: u64 = 1024 * 1024;
26 if bytes >= MIB {
27 format!("{:.1} MB", bytes as f64 / MIB as f64)
28 } else {
29 format!("{} KB", bytes.div_ceil(1024).max(1))
30 }
31}
32
33pub(crate) fn short(hash: &str) -> String {
35 hash.chars().take(12).collect()
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[tokio::test]
43 async fn a_row_carries_the_moves_id_and_says_ok_or_why_not() {
44 let state = crate::server::tests::test_state();
45 let id = format!("move-audit-{}", uuid::Uuid::new_v4());
46 row(&state, "offered", "Lead@narko", "Worker", &id, None, "to charlie: 1.5 MB, manifest abc");
47 row(&state, "refused", "Lead@narko", "Worker", &id, Some("the person at charlie did not allow the move"), "to charlie");
48 let rows: Vec<_> = state.reactive_handler.get_audit_log(usize::MAX).into_iter().filter(|e| e.event_kind == "action" && e.request_id == id).collect();
49 assert_eq!(rows.len(), 2, "{rows:?}");
50 let offered = rows.iter().find(|e| e.outcome.as_deref() == Some("agent.move.offered")).expect("the offer is a row");
51 assert!(offered.success);
52 assert_eq!(offered.target_agent, "Worker");
53 let refused = rows.iter().find(|e| e.outcome.as_deref() == Some("agent.move.refused")).expect("the refusal is a row");
54 assert!(!refused.success);
55 }
56
57 #[test]
58 fn sizes_and_hashes_are_short() {
59 assert_eq!(size_text(1536 * 1024), "1.5 MB");
60 assert_eq!(size_text(10), "1 KB");
61 assert_eq!(short("0123456789abcdef0123"), "0123456789ab");
62 }
63}