Skip to main content

agentmux_srv/server/remote_ops/
mod.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Remote operations: an agent does on another proven machine what it can do on its own
5//! (`docs/specs/SPEC_AGENTS_OPERATE_OTHER_MACHINES_2026_10_30.md`).
6//!
7//! The transport (this module's `receive` and `send`) owns the signature, the audience,
8//! the clock window, the nonce, the machine's proof, the off switch and both audit rows.
9//! An operation is a handler that gets a [`RemoteCaller`] and its `params` and never sees
10//! the envelope: it runs the same code a local call runs, with [`RemoteCaller::actor`] as
11//! the actor, and anything that is sensitive locally files the same sensitive request.
12//!
13//! To add an operation: write `async fn` in a file of its own, and add one arm to [`lookup`].
14
15pub(crate) mod agent_ops;
16pub(crate) mod app_ops;
17pub(crate) mod audit_ops;
18mod disk_ops;
19pub(crate) mod fleet_sync;
20mod google_ops;
21pub(crate) mod identity_ops;
22mod live_facts_ops;
23pub(crate) mod memory;
24pub(crate) mod move_audit;
25pub(crate) mod move_consent;
26mod move_capacity;
27mod move_harness;
28mod move_key_request;
29mod move_ops;
30mod moved_ops;
31pub(crate) mod op;
32mod disk_cleanup_op;
33pub(crate) mod log_ops;
34pub(crate) mod receive;
35mod reclaim_op;
36mod upgrade_op;
37mod upgrade_watch;
38pub(crate) mod send;
39pub(crate) mod signin_ops;
40pub(crate) mod sync_loop;
41mod sync_ops;
42pub(crate) mod tasks;
43mod zoom_ops;
44
45use futures_util::future::BoxFuture;
46use serde_json::Value;
47
48use super::AppState;
49
50/// How the requesting machine was proven.
51#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52pub(crate) enum Verification {
53    /// A LAN peer whose agent key this machine pinned, signing a request meant for it.
54    Lan,
55    /// An approved install of the same account, over the relay.
56    #[allow(dead_code)] // the relay kind follows the LAN transport
57    Wan,
58}
59
60impl Verification {
61    pub(crate) fn as_str(self) -> &'static str {
62        match self {
63            Verification::Lan => "lan-verified",
64            Verification::Wan => "wan-verified",
65        }
66    }
67}
68
69/// Who asks, as the receiving machine proved it.
70///
71/// The identity is the **slug**: the key this machine pinned for that name signed the request.
72/// `agent_uid` and `reason` are signed too, but nothing ties the uid to the key, so a handler
73/// uses them to describe the caller and never to authorize it. `instance_id` is the install
74/// whose certificate vouches for the agent's key; it is empty when no valid certificate is held.
75/// `install_trusted` says this machine also trusts that install (pinned here, or live in the
76/// account's directory, and not revoked): it is looked into only for an operation that changes
77/// state, so it is `false` for a read, and always `true` inside a write's handler.
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub(crate) struct RemoteCaller {
80    pub agent_uid: String,
81    pub slug: String,
82    /// The requesting machine's name.
83    pub machine: String,
84    /// The requesting install's id, from the agent's certificate; empty when none was shown.
85    pub instance_id: String,
86    pub install_trusted: bool,
87    pub verification: Verification,
88    /// The requester's own words, to show as theirs. Never acted on, never given to an agent.
89    pub reason: String,
90}
91
92impl RemoteCaller {
93    /// `slug@machine`: the actor for audit rows, cards and history. Never the operator.
94    pub(crate) fn actor(&self) -> String {
95        format!("{}@{}", self.slug, self.machine)
96    }
97}
98
99/// An operation that did not run, and why. `code` is snake_case and goes to the requester as
100/// `refused_reason`; `message` is for the requester's agent to read. A plain failure uses `failed`.
101#[derive(Debug, Clone, PartialEq, Eq)]
102pub(crate) struct RemoteRefusal {
103    pub code: &'static str,
104    pub message: String,
105}
106
107impl RemoteRefusal {
108    pub(crate) fn new(code: &'static str, message: impl Into<String>) -> Self {
109        Self { code, message: message.into() }
110    }
111
112    pub(crate) fn failed(message: impl Into<String>) -> Self {
113        Self::new("failed", message)
114    }
115}
116
117/// `Ok` is the operation's `result` (status `done`).
118pub(crate) type RemoteResult = Result<Value, RemoteRefusal>;
119
120pub(crate) type OpFn = for<'a> fn(&'a AppState, &'a RemoteCaller, Value) -> BoxFuture<'a, RemoteResult>;
121
122/// One operation: its handler, and whether it changes anything on this machine.
123#[derive(Clone, Copy)]
124pub(crate) struct Op {
125    pub run: OpFn,
126    /// A state-changing operation needs a caller backed by a certificate from the install that
127    /// hosts the agent, an install this machine trusts (`needs_certificate`, `install_not_trusted`
128    /// otherwise). A read can go on the first-seen pin alone:
129    /// that pin comes from unauthenticated discovery, which is enough to read a version and too
130    /// thin to stop an agent or move an account.
131    pub changes_state: bool,
132    /// A read of personal data (an account's email) takes the same proof as a write: a certificate
133    /// from an install this machine has approved. A first-seen pin proves only that the peer stayed
134    /// the same, not that anyone approved it. Always `true` for a write.
135    pub needs_approved_install: bool,
136    /// The most bytes of params this operation takes. The default is the transport's
137    /// `MAX_PARAMS_BYTES`; one that carries text raises it with [`Op::with_max_params`]. The sender
138    /// and the receiver both read it from here, the receiver before it verifies anything, so the
139    /// unsigned op name only bounds the work.
140    pub max_params: usize,
141}
142
143impl Op {
144    pub(crate) const fn read(run: OpFn) -> Self {
145        Self { run, changes_state: false, needs_approved_install: false, max_params: crate::server::remote_ops::op::MAX_PARAMS_BYTES }
146    }
147
148    /// A read that returns personal data: refused (`needs_certificate`, `install_not_trusted`) to
149    /// a peer that is only first-seen, as a write is.
150    pub(crate) const fn read_personal(run: OpFn) -> Self {
151        Self { run, changes_state: false, needs_approved_install: true, max_params: crate::server::remote_ops::op::MAX_PARAMS_BYTES }
152    }
153
154    pub(crate) const fn write(run: OpFn) -> Self {
155        Self { run, changes_state: true, needs_approved_install: true, max_params: crate::server::remote_ops::op::MAX_PARAMS_BYTES }
156    }
157
158    pub(crate) const fn with_max_params(mut self, max_params: usize) -> Self {
159        self.max_params = max_params;
160        self
161    }
162}
163
164/// The params limit of `op`: its own, or the default for one this build does not know.
165pub(crate) fn max_params_of(op: &str) -> usize {
166    lookup(op).map_or(crate::server::remote_ops::op::MAX_PARAMS_BYTES, |o| o.max_params)
167}
168
169/// The operation `op`, if this build has one. One arm per operation: say whether it reads or writes.
170pub(crate) fn lookup(op: &str) -> Option<Op> {
171    Some(match op {
172        "app.version" => Op::read(|state, caller, params| Box::pin(app_ops::version(state, caller, params))),
173        "app.activity" => Op::read(|state, caller, params| Box::pin(app_ops::activity(state, caller, params))),
174        "app.health" => Op::read(|state, caller, params| Box::pin(app_ops::health(state, caller, params))),
175        // Filing a task changes the board, so it needs a certified caller from an approved install.
176        // So does reading it: a task's text and result are the workspace's own, and a first-seen pin is thin.
177        "tasks.enqueue" => Op::write(|state, caller, params| Box::pin(tasks::enqueue(state, caller, params))),
178        "tasks.list" => Op::read_personal(|state, caller, params| Box::pin(tasks::list(state, caller, params))),
179        // The signed-in accounts here with quota, and moving an agent to one. The listing has account
180        // emails and a switch changes state: both need a certificate from an approved install.
181        "identity.accounts" => Op::read_personal(|state, caller, params| Box::pin(identity_ops::accounts(state, caller, params))),
182        "identity.switch" => Op::write(|state, caller, params| Box::pin(identity_ops::switch(state, caller, params))),
183        "identity.signin.start" => Op::write(|state, caller, params| Box::pin(signin_ops::start(state, caller, params))),
184        "identity.signin.notice" => Op::write(|state, caller, params| Box::pin(signin_ops::notice(state, caller, params))),
185        "identity.signin.finish" => Op::write(|state, caller, params| Box::pin(signin_ops::finish(state, caller, params))),
186        // The Google accounts connected here and who holds what (emails: personal data), and turning a
187        // service on or off for an agent here. `connect` has no remote form: the sign-in is the person's.
188        "google.status" => Op::read_personal(|state, caller, params| Box::pin(google_ops::status(state, caller, params))),
189        "google.grant" => Op::write(|state, caller, params| Box::pin(google_ops::grant(state, caller, params))),
190        // Which agents run on what, and in what state, with no message text, prompt or account email: a read for an
191        // approved install, like the account listing.
192        "agents.status" => Op::read_personal(|state, caller, params| Box::pin(live_facts_ops::status(state, caller, params))),
193        "agents.start" => Op::write(|state, caller, params| Box::pin(agent_ops::start(state, caller, params))),
194        "agents.open" => Op::write(|state, caller, params| Box::pin(agent_ops::open(state, caller, params))),
195        "agents.stop" => Op::write(|state, caller, params| Box::pin(agent_ops::stop(state, caller, params))),
196        "agents.set_seat" => Op::write(|state, caller, params| Box::pin(agent_ops::set_seat(state, caller, params))),
197        "agents.rename" => Op::write(|state, caller, params| Box::pin(agent_ops::rename(state, caller, params))),
198        "agents.update" => Op::write(|state, caller, params| Box::pin(agent_ops::update(state, caller, params))),
199        // A write too: an environment holds the operator's own lines, and a read can go on a first-seen pin.
200        "agents.env_get" => Op::write(|state, caller, params| Box::pin(agent_ops::env_get(state, caller, params))),
201        "agents.env_set" => Op::write(|state, caller, params| Box::pin(agent_ops::env_set(state, caller, params))),
202        // A Global Memory entry is up to 1 MiB of text (the cap `globalmemory.write` has), so this one
203        // takes 1.2 MB of params; every other operation keeps the default.
204        "memory.global.put" => Op::write(|state, caller, params| Box::pin(memory::put(state, caller, params))).with_max_params(memory::MAX_PUT_PARAMS),
205        "memory.global.remove" => Op::write(|state, caller, params| Box::pin(memory::remove(state, caller, params))),
206        // The listing is the ids and content hashes of the person's private instructions: a peer
207        // known only by its first-seen pin does not see it.
208        "memory.global.list" => Op::read_personal(|state, caller, params| Box::pin(memory::list(state, caller, params))),
209        // A collection's membership and records, between a person's own machines: write ops (a certificate from an
210        // approved install), answered only by the install that holds the computer's sync lock and only to a member of the
211        // collection named. A push and a membership offer carry a batch, so they take more params than the default.
212        "agent.move.preflight" => Op::write(|state, caller, params| Box::pin(move_ops::preflight(state, caller, params))),
213        "agent.move.offer" => Op::write(|state, caller, params| Box::pin(move_ops::offer(state, caller, params))),
214        "agent.move.chunk" => Op::write(|state, caller, params| Box::pin(move_ops::chunk(state, caller, params))).with_max_params(move_ops::MAX_CHUNK_PARAMS),
215        "agent.moved" => Op::write(|state, caller, params| Box::pin(moved_ops::moved(state, caller, params))),
216        "agent.move.commit" => Op::write(|state, caller, params| Box::pin(move_ops::commit(state, caller, params))),
217        "agent.move.status" => Op::write(|state, caller, params| Box::pin(move_ops::status(state, caller, params))),
218        "agent.move.abort" => Op::write(|state, caller, params| Box::pin(move_ops::abort(state, caller, params))),
219        "sync.hello" => Op::write(|state, caller, params| Box::pin(sync_ops::hello(state, caller, params))),
220        "sync.pull" => Op::write(|state, caller, params| Box::pin(sync_ops::pull(state, caller, params))),
221        "sync.push" => Op::write(|state, caller, params| Box::pin(sync_ops::push(state, caller, params))).with_max_params(crate::backend::user_sync::ops::MAX_WIRE_BYTES),
222        "sync.members" => Op::write(|state, caller, params| Box::pin(sync_ops::members(state, caller, params))).with_max_params(crate::backend::user_sync::ops::MAX_WIRE_BYTES),
223        "sync.join" => Op::write(|state, caller, params| Box::pin(sync_ops::join(state, caller, params))),
224        "sync.keys" => Op::write(|state, caller, params| Box::pin(sync_ops::keys(state, caller, params))),
225        #[cfg(test)]
226        "test.big" => Op::read(|_, _, params| Box::pin(async move { Ok(serde_json::json!({ "bytes": params.to_string().len() })) })).with_max_params(1_200_000),
227        // Sizes of build folders and older versions, with a time budget: a read that deletes nothing.
228        // The report names every agent's clone folder, so it takes a certificate from an approved install like the logs do.
229        "app.disk.report" => Op::read_personal(|state, caller, params| Box::pin(disk_ops::disk_report(state, caller, params))),
230        "app.logs.tail" => Op::read_personal(|state, caller, params| Box::pin(log_ops::logs_tail(state, caller, params))),
231        // The Audit's rows (agents and what they did, no message text): answered only when the owner shares it
232        // (`audit:sharewithpaired`), and only to an approved install, like the logs.
233        "audit.query" => Op::read_personal(|state, caller, params| Box::pin(audit_ops::query(state, caller, params))),
234        "app.history_reclaim" => Op::write(|state, caller, params| Box::pin(reclaim_op::history_reclaim(state, caller, params))),
235        // Files the same sensitive request a local agent's App upgrade files: this machine's super agent and the person's veto.
236        "app.upgrade" => Op::write(|state, caller, params| Box::pin(upgrade_op::upgrade(state, caller, params))),
237        // Files the `disk_cleanup` sensitive request here: its super agent decides and the person can veto the countdown.
238        "app.disk.cleanup" => Op::write(|state, caller, params| Box::pin(disk_cleanup_op::disk_cleanup(state, caller, params))),
239        // Zoom on the window the person last used. A `get` and a `check` return which panes are open and text from the
240        // screen (labels), so they take a certificate from an approved install like the logs do; a change needs it too.
241        "zoom.get" => Op::read_personal(|state, caller, params| Box::pin(zoom_ops::get(state, caller, params))),
242        "zoom.check" => Op::read_personal(|state, caller, params| Box::pin(zoom_ops::check(state, caller, params))),
243        "zoom.set" => Op::write(|state, caller, params| Box::pin(zoom_ops::set(state, caller, params))),
244        "zoom.fit" => Op::write(|state, caller, params| Box::pin(zoom_ops::fit(state, caller, params))),
245        "zoom.reset" => Op::write(|state, caller, params| Box::pin(zoom_ops::reset(state, caller, params))),
246        "zoom.undo" => Op::write(|state, caller, params| Box::pin(zoom_ops::undo(state, caller, params))),
247        #[cfg(test)]
248        "test.change" => Op::write(|_, caller, _| Box::pin(async move { Ok(serde_json::json!({ "changed_by": caller.actor() })) })),
249        _ => return None,
250    })
251}
252
253/// Whether this machine takes remote operations: on unless `network:remote_ops` is `false`.
254pub(crate) fn enabled(state: &AppState) -> bool {
255    state.config_watcher.get_settings().network_remote_ops != Some(false)
256}
257
258#[cfg(test)]
259mod tests;