Skip to main content

agentmux_srv_core/backend/audit_query/
peers.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The paired computers on the LAN, asked for their Audit rows (SPEC_AUDIT_LIVE_LOG_OF_EVERY_AGENT_ACTION_2026_11_03.md
5//! ยง3.6 point 5). A computer's answer is a page of its own `audit.query` on the `machine` scope; here it becomes the rows
6//! of one section: each row says which computer it is from, has an id of its own, and keeps no link that only works on
7//! the computer that wrote it (a pane, an agent's history, a Kanban task). The rows are shown and forgotten: nothing
8//! here stores them, and the cloud never sees them.
9
10use crate::backend::rpc_types::{AuditEntry, AuditLinks, AuditPeerRows, AuditQueryReq, AuditQueryResult};
11
12/// The remote operation the Audit asks a paired computer with.
13pub const QUERY_OP: &str = "audit.query";
14
15/// The setting that lets paired computers read this computer's Audit. Off unless set.
16pub const SETTING_AUDIT_SHARE: &str = "audit:sharewithpaired";
17
18/// The most rows one paired computer is asked for, and answers with.
19pub const PEER_LIMIT: u32 = 100;
20
21/// What a paired computer is asked, and what it answers: the filter of `req`, for the first page of every channel on
22/// that computer. Its paging, scope and totals are the asker's own.
23pub fn request_for_peer(req: &AuditQueryReq) -> AuditQueryReq {
24    AuditQueryReq { cursor: None, totals: None, scope: Some("machine".to_string()), limit: Some(req.limit.unwrap_or(50).min(PEER_LIMIT)), ..req.clone() }
25}
26
27/// Take from `entries` what belongs to the computer that wrote them. The links: a pane, an agent's history and a task
28/// of its Kanban would open nothing, or the wrong thing, anywhere else. The text of a dialog and the answer given to it
29/// (`prompt`, `answer` of a sensitive-operation row): the Audit shares what agents did, never what was typed.
30pub fn without_local_links(entries: &mut [AuditEntry]) {
31    for e in entries {
32        e.prompt = None;
33        e.answer = None;
34        e.links = AuditLinks::default();
35        for end in [e.from.as_mut(), e.to.as_mut()].into_iter().flatten() {
36            end.links = AuditLinks::default();
37        }
38    }
39}
40
41/// `host`'s answer as the rows of its section: its own channel's rows and its other channels' in one list, newest first.
42pub fn rows_of(host: &str, result: AuditQueryResult) -> AuditPeerRows {
43    let more = result.next_cursor.is_some() || result.other_channels.iter().flatten().any(|c| c.more);
44    let mut entries = result.entries;
45    for channel in result.other_channels.unwrap_or_default() {
46        entries.extend(channel.entries);
47    }
48    entries.sort_by(|a, b| (b.ts_ms, &b.id).cmp(&(a.ts_ms, &a.id)));
49    without_local_links(&mut entries);
50    for e in &mut entries {
51        e.id = format!("{host}/{}", e.id);
52        let provenance = e.provenance.get_or_insert_with(Default::default);
53        // The computer is the one asked, named as `Agents list` names it, so the rows of one computer are one section.
54        provenance.host = Some(host.to_string());
55        if provenance.channel.as_deref().is_none_or(str::is_empty) {
56            provenance.channel = result.channel.clone();
57        }
58    }
59    AuditPeerRows { host: host.to_string(), entries, more, note: None }
60}
61
62/// A computer that gave no rows, and why.
63pub fn no_rows(host: &str, note: &str) -> AuditPeerRows {
64    AuditPeerRows { host: host.to_string(), entries: Vec::new(), more: false, note: Some(note.to_string()) }
65}
66
67/// What a person is told when a paired computer's answer is a refusal or a failure: the reason, in words.
68pub fn note_for(refused_reason: Option<&str>, error: Option<&str>) -> String {
69    match refused_reason {
70        Some("not_shared") => "its owner has not turned on sharing its Audit with paired computers".to_string(),
71        Some("needs_certificate") | Some("install_not_trusted") => "this computer is not yet approved there".to_string(),
72        Some("remote_ops_off") => "it does not take requests from other computers".to_string(),
73        Some("peer_cannot_do_remote_ops") | Some("unknown_op") => "it runs a version that cannot share its Audit".to_string(),
74        Some(other) => format!("refused: {other}"),
75        None => error.map_or_else(|| "it did not answer".to_string(), |e| format!("failed: {e}")),
76    }
77}
78
79#[cfg(test)]
80#[path = "peers_tests.rs"]
81mod tests;