Skip to main content

agentmux_srv/server/remote_ops/
moved_ops.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `agent.moved` (SPEC_MOVE_AN_AGENT_TO_ANOTHER_COMPUTER section 6.2 and 6.5): the computer an agent left tells the computers
5//! that hold its name that it went to another install, with a statement signed by its install key. It is believed only from
6//! the install the name's pin is bound to, so a paired computer that never hosted the agent cannot rewrite a lead or a message
7//! route by claiming the agent moved. Taking it moves no pin: the new key is pinned when it is shown with the destination's
8//! own certificate (`reactive::lan_handover`).
9
10use serde::Deserialize;
11use serde_json::{json, Value};
12
13use super::{AppState, RemoteCaller, RemoteRefusal, RemoteResult};
14use crate::backend::agent_move::handover::HandoverCertificate;
15
16#[derive(Deserialize)]
17struct MovedReq {
18    certificate: HandoverCertificate,
19}
20
21pub(super) async fn moved(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
22    let req: MovedReq = serde_json::from_value(params).map_err(|e| RemoteRefusal::new("bad_params", format!("agent.moved: {e}")))?;
23    if caller.instance_id.is_empty() || !caller.install_trusted {
24        return Err(RemoteRefusal::new("needs_certificate", "a move is told only by a trusted install that showed a certificate"));
25    }
26    let now = agentmux_common::time::now_secs();
27    let key = super::move_ops::source_key(state, caller)?;
28    req.certificate.verify(&key, now).map_err(|e| RemoteRefusal::new("bad_statement", e.to_string()))?;
29    crate::server::reactive::lan_handover::accept(state, &caller.instance_id, &req.certificate, now).await.map_err(|why| {
30        tracing::warn!(name = %req.certificate.name, from = %caller.instance_id, %why, "agent.moved refused");
31        RemoteRefusal::new("not_accepted", why)
32    })?;
33    Ok(json!({ "accepted": true, "name": req.certificate.name }))
34}