Skip to main content

agentmux_srv/server/remote_ops/
move_audit.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The audit rows of a move, on both computers (SPEC_MOVE_AN_AGENT_TO_ANOTHER_COMPUTER section 7(p)). One row per step the people
5//! on either side may want to read back: the source's `offered`, `accepted` or `refused`, `transferred` (a count) and `completed`;
6//! the destination's `received`, `refused`, `registered` and `aborted`. Every row is `agent.move.<step>`, names who asked, the agent,
7//! the other computer and sizes, and carries the move's id as its request id, so the rows of one move on both computers can be
8//! put side by side. Never content: no chunk, key or archive path goes in a row. The destination's consent is a sensitive request
9//! with its own rows (`sensitive.*`); the source's whole call keeps its `agent.move` row.
10
11use crate::server::state::AppState;
12
13/// The action name of a step of a move.
14pub(crate) fn action(step: &str) -> String {
15    format!("agent.move.{step}")
16}
17
18/// A row of the move `move_id`: `actor` did the step to `agent`, `note` says the rest (the other computer, sizes, counts).
19pub(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
23/// `12.3 MB` or `40 KB`: the size a row names.
24pub(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
33/// The first characters of a manifest hash: enough to tell two archives apart in a row.
34pub(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}