Skip to main content

agentmux_srv/server/remote_ops/
identity_ops.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `identity.accounts` and `identity.switch` (spec §4.3, §4.4): the signed-in accounts here with
5//! their stable id and quota, and moving an agent to one. Both run the code a local `Identity`
6//! call runs (`server::account_episodes`); a switch is audited as the remote agent,
7//! `slug@machine`, never as the operator, and never on the strength of the signed `agent_uid`.
8
9use serde::Deserialize;
10use serde_json::{json, Value};
11
12use super::{AppState, RemoteCaller, RemoteRefusal, RemoteResult};
13use crate::server::account_episodes::{self as episodes, AccountSwitchWhen, AgentAccountSwitchReq};
14
15/// `identity.switch`'s params.
16#[derive(Debug, Deserialize)]
17struct SwitchParams {
18    /// The agent on this machine (slug or definition id), or `all limited`. A remote caller names
19    /// it: there is no "yourself" across machines.
20    #[serde(alias = "agent")]
21    agent_id: String,
22    /// A stable id, a local id or label, an email with exactly one account with quota, or `any with quota`.
23    account: String,
24    #[serde(default)]
25    when: AccountSwitchWhen,
26}
27
28fn failed(error: String) -> RemoteRefusal {
29    RemoteRefusal::failed(error)
30}
31
32/// Every signed-in account here, as `Identity` `accounts` with scope `machine` lists them.
33pub(super) async fn accounts(state: &AppState, _caller: &RemoteCaller, _params: Value) -> RemoteResult {
34    let result = episodes::identity_accounts(state.clone()).await.map_err(failed)?;
35    serde_json::to_value(result).map_err(|e| failed(e.to_string()))
36}
37
38/// Move an agent here to another account. A state change: the transport has already required a
39/// certificate from a trusted install for it.
40pub(super) async fn switch(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
41    let p: SwitchParams = serde_json::from_value(params).map_err(|e| RemoteRefusal::new("bad_params", format!("identity.switch: {e}")))?;
42    let agent = p.agent_id.trim();
43    if agent.is_empty() || p.account.trim().is_empty() {
44        return Err(RemoteRefusal::new("bad_params", "identity.switch: say which agent and which account"));
45    }
46    let actor = caller.actor();
47    if episodes::is_all_limited(agent) {
48        let req = AgentAccountSwitchReq { agent_id: None, account: p.account, when: p.when };
49        let all = episodes::switch_all_limited(state.clone(), actor.clone(), req).await.map_err(failed)?;
50        tracing::info!(actor = %actor, agents = all.agents.len(), "remote identity.switch: moved every limited agent");
51        return serde_json::to_value(all).map_err(|e| failed(e.to_string()));
52    }
53    let req = AgentAccountSwitchReq { agent_id: Some(agent.to_string()), account: p.account, when: p.when };
54    let done = episodes::switch_account(state.clone(), actor.clone(), req).await.map_err(|e| failed(e.to_string()))?;
55    tracing::info!(actor = %actor, agent = %agent, status = ?done.status, "remote identity.switch: switched an agent's account");
56    Ok(json!(done))
57}
58
59#[cfg(test)]
60#[path = "identity_ops_tests.rs"]
61mod tests;