Skip to main content

agentmux_srv/server/remote_ops/
agent_ops.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `agents.start`, `agents.open`, `agents.stop`, `agents.set_seat`, `agents.rename`, `agents.update` (model and effort), `agents.env_get` and `agents.env_set`: an
5//! agent controls another machine's agents as it controls its own (spec ยง4.3, P3). Each runs the code of
6//! the local `Agents` action with the remote agent (`slug@machine`) as the actor, so every rule a local
7//! caller meets is met here too: a stop of another agent waits for the person's keep-running window, a
8//! start counts against the sender's start limit, an environment refuses the names an agent may not set.
9//! The agent a call names is one on this machine; an id or slug, as a local call takes it.
10
11use axum::http::StatusCode;
12use serde_json::{json, Value};
13
14use super::{AppState, RemoteCaller, RemoteRefusal, RemoteResult};
15use crate::server::app_api::{agent_definition, agent_env, agent_lifecycle, agent_open_remote, agent_rename, agent_seats};
16
17/// A refusal of the local code, as the requester reads it.
18fn refused((status, message): (StatusCode, String)) -> RemoteRefusal {
19    let code = match status.as_u16() {
20        400 => "invalid_arg",
21        401 | 403 => "forbidden",
22        404 => "not_found",
23        409 => "conflict",
24        429 => "rate_limited",
25        _ => "failed",
26    };
27    RemoteRefusal::new(code, message)
28}
29
30/// The `agent` a call names: an id or slug of an agent on this machine.
31fn agent_of(params: &Value) -> Result<&str, RemoteRefusal> {
32    params
33        .get("agent")
34        .and_then(Value::as_str)
35        .map(str::trim)
36        .filter(|a| !a.is_empty())
37        .ok_or_else(|| RemoteRefusal::new("invalid_arg", "agent is required: the id or slug of an agent on this machine"))
38}
39
40pub(super) async fn start(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
41    let agent = agent_of(&params)?;
42    agent_lifecycle::run_remote(state, &caller.actor(), "start", agent).await.map_err(refused)
43}
44
45/// Opens the agent in the caller's own tab, without focus (`agent_open_remote`).
46pub(super) async fn open(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
47    let agent = agent_of(&params)?;
48    agent_open_remote::run_remote(state, &caller.actor(), agent).await.map_err(refused)
49}
50
51pub(super) async fn stop(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
52    let agent = agent_of(&params)?;
53    agent_lifecycle::run_remote(state, &caller.actor(), "stop", agent).await.map_err(refused)
54}
55
56pub(super) async fn set_seat(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
57    let req: agent_seats::SetSeatsRequest =
58        serde_json::from_value(params).map_err(|e| RemoteRefusal::new("invalid_arg", format!("seats: {e}")))?;
59    agent_seats::run_remote(state, &caller.actor(), req).await.map_err(refused)
60}
61
62/// The agent `agent` (an id or slug on this machine) gets the displayed name `name`, as a local `Agents rename` does.
63pub(super) async fn rename(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
64    let req: agent_rename::RenameRequest =
65        serde_json::from_value(params).map_err(|e| RemoteRefusal::new("invalid_arg", format!("rename: {e}")))?;
66    agent_rename::run_remote(state, &caller.actor(), req).await.map_err(refused)
67}
68
69/// The agent `agent` (an id or slug on this machine) has its definition, model or effort changed, as a local `Agents update` does.
70pub(super) async fn update(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
71    // From another machine only what the pane's own picker sets: the rest of an update (a spend cap, leaving a model
72    // route, how the agent is listed) is for this machine's agents and its person.
73    if let Some(other) = params.as_object().and_then(|m| m.keys().find(|k| !matches!(k.as_str(), "agent" | "model" | "effort"))) {
74        return Err(RemoteRefusal::new("invalid_arg", format!("update over host takes agent, model and effort; {other} is changed on that machine")));
75    }
76    let req: agent_definition::UpdateRequest =
77        serde_json::from_value(params).map_err(|e| RemoteRefusal::new("invalid_arg", format!("update: {e}")))?;
78    agent_definition::run_remote(state, &caller.actor(), req).await.map_err(refused)
79}
80
81pub(super) async fn env_get(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
82    env(state, caller, "get", params).await
83}
84
85pub(super) async fn env_set(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
86    env(state, caller, "set", params).await
87}
88
89async fn env(state: &AppState, caller: &RemoteCaller, action: &str, params: Value) -> RemoteResult {
90    let mut body = params;
91    let Some(map) = body.as_object_mut() else {
92        return Err(RemoteRefusal::new("invalid_arg", "params must be an object"));
93    };
94    map.insert("action".to_string(), json!(action));
95    let req: agent_env::AgentEnvRequest =
96        serde_json::from_value(body).map_err(|e| RemoteRefusal::new("invalid_arg", format!("env: {e}")))?;
97    let (state, who) = (state.clone(), caller.actor());
98    tokio::task::spawn_blocking(move || agent_env::run_remote(&state, &who, req))
99        .await
100        .map_err(|e| RemoteRefusal::failed(format!("env: {e}")))?
101        .map_err(refused)
102}
103
104#[cfg(test)]
105#[path = "agent_ops_tests.rs"]
106mod tests;