Skip to main content

agentmux_srv/server/app_api/
fleet_result.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What a broadcast reports per target (`fleet::fleet_broadcast_impl`): delivered, kept for an agent
5//! out of quota (`person_hold`), or failed. Each is recorded in the audit log as it happened.
6
7use crate::backend::rpc_types::{FleetActionFailure, FleetActionHeld, FleetActionResult};
8
9use super::fleet::{BroadcastOutcome, FLEET_BROADCAST_AUDIT_ACTION};
10use super::AppState;
11
12/// Sorts the outcomes of a broadcast into a [`FleetActionResult`]. `is_local` says whether a block is
13/// an agent of this instance, whose history gets what the person typed.
14pub(super) fn settle_broadcast(
15    state: &AppState,
16    outcomes: Vec<BroadcastOutcome>,
17    is_local: impl Fn(&str) -> bool,
18    msg_id: &str,
19    message: &str,
20) -> FleetActionResult {
21    let mut result = FleetActionResult::default();
22    for outcome in outcomes {
23        let target_agent = outcome.agent_id.as_deref().unwrap_or(&outcome.block_id);
24        let audit = |success: bool, error: Option<&str>, note: Option<&str>| {
25            state.reactive_handler.log_fleet_action_audit(None, target_agent, &outcome.block_id, FLEET_BROADCAST_AUDIT_ACTION, success, error, msg_id, note);
26        };
27        match &outcome.result {
28            Ok(()) => {
29                audit(true, None, None);
30                // What the user typed, in each recipient of this instance's history.
31                if is_local(&outcome.block_id) {
32                    super::input_history::record_broadcast(state, &outcome.block_id, message);
33                }
34                result.succeeded.push(outcome.block_id);
35            }
36            // Kept for an agent out of quota, with its check started: not a failure.
37            Err(error) if crate::server::person_hold::held_note(error).is_some() => {
38                audit(true, None, Some("held for an agent out of quota"));
39                let note = crate::server::person_hold::held_note(error).unwrap_or_default().to_string();
40                result.held.push(FleetActionHeld { id: outcome.block_id, note });
41            }
42            Err(error) => {
43                audit(false, Some(error), None);
44                result.failed.push(FleetActionFailure { id: outcome.block_id, error: error.clone() });
45            }
46        }
47    }
48    result
49}