Skip to main content

agentmux_srv/server/remote_ops/
live_facts_ops.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `agents.status` (live facts spec ยง6.2): this machine's agents with the facts a peer asked for, the answer the
5//! discovery route gives a local agent (`live_facts::handler::answer`), as a signed read. It carries state, model and
6//! counts only: no message text, prompt, transcript, credential or account email, and no pane or folder of this
7//! machine.
8
9use std::collections::HashMap;
10
11use serde_json::{json, Value};
12
13use super::{AppState, RemoteCaller, RemoteRefusal, RemoteResult};
14use crate::server::live_facts::handler::{answer, is_status_view};
15
16/// The most agents one answer carries when the asker names no page: a long fleet is read in pages.
17const DEFAULT_LIMIT: u64 = 200;
18
19fn bad(message: impl Into<String>) -> RemoteRefusal {
20    RemoteRefusal::new("bad_params", format!("agents.status: {}", message.into()))
21}
22
23/// The query the discovery answer takes, from the op's params: text or numbers by name. The asker's `host` is dropped:
24/// the machine that answers is this one.
25fn query_of(params: &Value) -> Result<HashMap<String, String>, String> {
26    let mut out = HashMap::new();
27    let map = match params {
28        Value::Null => return Ok(out),
29        Value::Object(map) => map,
30        _ => return Err("the params are an object of the query's names".to_string()),
31    };
32    for (key, value) in map.iter().filter(|(k, _)| k.as_str() != "host") {
33        let text = match value {
34            Value::String(s) => s.clone(),
35            Value::Number(n) => n.to_string(),
36            Value::Null => continue,
37            _ => return Err(format!("{key} is text or a number")),
38        };
39        out.insert(key.clone(), text);
40    }
41    Ok(out)
42}
43
44/// What a peer does not learn of a row: where its pane or its folder is on this machine.
45fn without_local_places(row: &mut Value) {
46    let Some(fields) = row.as_object_mut() else { return };
47    fields.remove("working_directory");
48    fields.remove("block_id");
49    if let Some(runtime) = fields.get_mut("runtime").and_then(Value::as_object_mut) {
50        runtime.remove("block_id");
51        runtime.remove("waiting_in");
52    }
53}
54
55/// `agents.status`: `{host, version, as_of_ms, view, answer}`; the answer is the compact status for `view: status`,
56/// else `{agents, page}`.
57pub(super) async fn status(state: &AppState, _caller: &RemoteCaller, params: Value) -> RemoteResult {
58    let mut query = query_of(&params).map_err(bad)?;
59    query.entry("limit".to_string()).or_insert_with(|| DEFAULT_LIMIT.to_string());
60    let compact = is_status_view(&query).map_err(bad)?;
61    let body = answer(state, &query).await.map_err(bad)?;
62    let answer = if compact {
63        body
64    } else {
65        let mut agents = body.pointer("/host/agents").cloned().unwrap_or_else(|| json!([]));
66        agents.as_array_mut().into_iter().flatten().for_each(without_local_places);
67        json!({ "agents": agents, "page": body.pointer("/host/page").cloned().unwrap_or(Value::Null) })
68    };
69    Ok(json!({
70        "host": state.hostname,
71        "version": state.version,
72        "as_of_ms": agentmux_common::time::now_ms_u64(),
73        "view": if compact { "status" } else { "list" },
74        "answer": answer,
75    }))
76}
77
78#[cfg(test)]
79#[path = "live_facts_ops_tests.rs"]
80mod tests;