Skip to main content

agentmux_srv_core/backend/audit_query/
other_channels.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The other channels on this computer, asked over loopback for the rows they keep in their own file
5//! (SPEC_AUDIT_LIVE_LOG_OF_EVERY_AGENT_ACTION_2026_11_03.md ยง3.6 point 5). A channel that gave the shared folder up
6//! writes its rows to its own file, which this srv cannot read; the channel is asked for them with its `local_url` and
7//! key from the host-global registry, exactly as the Swarm asks it for its agents (`swarm_remote::channel_statuses`).
8//! A channel that fails, times out, answers badly or predates the route contributes nothing.
9
10use std::time::Duration;
11
12use crate::backend::rpc_types::{AuditChannelRows, AuditQueryReq, AuditQueryResult};
13use crate::backend::swarm_remote::ChannelStatusTarget;
14
15/// The route a channel answers on: its own file's rows, as an `audit.query` answer.
16pub const OWN_ROWS_PATH: &str = "/agentmux/audit/own";
17
18/// Loopback, so a channel that takes longer is hung, and the Audit shouldn't wait on it.
19const TIMEOUT: Duration = Duration::from_millis(2500);
20/// A page of 200 rows is well under this; a larger answer is not an answer.
21const MAX_RESPONSE_BYTES: usize = 4 * 1024 * 1024;
22
23/// What to send another channel: the filter of this request, for its first page. Its paging, scope and totals are this
24/// srv's own.
25pub fn request_for_others(req: &AuditQueryReq) -> AuditQueryReq {
26    AuditQueryReq { cursor: None, totals: None, scope: Some("channel".to_string()), ..req.clone() }
27}
28
29/// Asks each target concurrently. One entry per channel that answered with rows, in the order of `targets`.
30pub async fn ask(http: &reqwest::Client, targets: &[ChannelStatusTarget], req: &AuditQueryReq) -> Vec<AuditChannelRows> {
31    let req = request_for_others(req);
32    let answers = futures_util::future::join_all(targets.iter().map(|t| {
33        let req = &req;
34        async move {
35            let resp = http
36                .post(format!("{}{OWN_ROWS_PATH}", t.local_url))
37                .header(agentmux_common::AUTH_KEY_HEADER, &t.auth_key)
38                .json(req)
39                .timeout(TIMEOUT)
40                .send()
41                .await
42                .ok()
43                .filter(|r| r.status().is_success())?;
44            let body = crate::backend::lan::discovery::LanDiscovery::read_body_capped(resp, MAX_RESPONSE_BYTES).await.ok()?;
45            let result: AuditQueryResult = serde_json::from_slice(&body).ok()?;
46            Some(rows_of(&t.channel, result))
47        }
48    }))
49    .await;
50    answers.into_iter().flatten().filter(|c| !c.entries.is_empty()).collect()
51}
52
53/// A channel's answer as the rows of its section. Each row says which channel it is from and gets an id of its own;
54/// a pane link is dropped, since the pane is in the other channel's window.
55pub fn rows_of(channel: &str, result: AuditQueryResult) -> AuditChannelRows {
56    let mut entries = result.entries;
57    for e in &mut entries {
58        e.id = format!("{channel}:{}", e.id);
59        e.links.pane = None;
60        for end in [e.from.as_mut(), e.to.as_mut()].into_iter().flatten() {
61            end.links.pane = None;
62        }
63        let provenance = e.provenance.get_or_insert_with(Default::default);
64        if provenance.channel.as_deref().is_none_or(str::is_empty) {
65            provenance.channel = Some(channel.to_string());
66        }
67    }
68    AuditChannelRows { channel: channel.to_string(), entries, more: result.next_cursor.is_some() }
69}
70
71#[cfg(test)]
72#[path = "other_channels_tests.rs"]
73mod tests;