Skip to main content

agentmux_srv/server/remote_ops/
upgrade_op.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `app.upgrade`: ask this machine to upgrade itself (spec ยง4.3). A remote request never starts anything
5//! by itself and is never "the user's own turn": it is filed as the same sensitive request a local agent's
6//! `App upgrade` files, so this machine's super agent decides and the person here sees the banner and can
7//! veto the countdown, whoever is or is not at the screen (`SPEC_SUPER_AGENT_2026_10_25.md`). The requester
8//! is `agent@machine` on the request, the banner and the audit, never anything that reads as the operator.
9//!
10//! `{ reason, version? }` files it and answers like the local tool (`requested` with the request's id, or
11//! why there is nothing to install). `{ status: <id> }` reads where a request this caller made stands.
12
13use serde_json::{json, Value};
14
15use super::{AppState, RemoteCaller, RemoteRefusal, RemoteResult};
16use crate::backend::upgrade_request::{self, Outcome};
17
18/// The request owner for a remote caller: not an agent here, so no local uid stands in for it.
19fn owner(caller: &RemoteCaller) -> String {
20    format!("{}{}", crate::backend::sensitive_requests::REMOTE_OWNER_PREFIX, caller.actor())
21}
22
23pub(super) async fn upgrade(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
24    let uid = owner(caller);
25    if let Some(id) = params.get("status").and_then(Value::as_str) {
26        let mut shown = crate::server::sensitive_handlers::status_for(&uid, id).map_err(RemoteRefusal::failed)?;
27        // How the restart stands is here too: its state, and when and why it was not confirmed. The requester's
28        // own machine reads this to tell it how the request ends (`upgrade_watch`).
29        if let (Some(d), Value::Object(map)) = (crate::backend::update::check::current_status().download, &mut shown) {
30            map.insert("restart".into(), super::app_ops::download_summary(&d));
31        }
32        return Ok(shown);
33    }
34    let reason = params.get("reason").and_then(Value::as_str).map(str::trim).filter(|r| !r.is_empty()).unwrap_or_else(|| caller.reason.trim());
35    if reason.is_empty() {
36        return Err(RemoteRefusal::failed("a reason is required"));
37    }
38    // Ask GitHub first: nothing checks on its own.
39    let status = crate::backend::update::check::check_now().await.unwrap_or_else(|_| crate::backend::update::check::current_status());
40    let version = match upgrade_request::installable(&status, params.get("version").and_then(Value::as_str)) {
41        Ok(v) => v,
42        Err(outcome) => return Ok(json!(outcome)),
43    };
44    tracing::info!(requester = %caller.actor(), version = %version, "upgrade: a remote agent asks as a sensitive request");
45    let facts = std::collections::BTreeMap::from([("version".to_string(), version.clone())]);
46    let filed = crate::server::sensitive_handlers::file_request(state, &uid, &caller.actor(), "upgrade", &facts, reason)
47        .await
48        .map_err(RemoteRefusal::failed)?;
49    Ok(json!(Outcome::Requested {
50        version,
51        id: filed["id"].as_str().unwrap_or_default().to_string(),
52        review: filed["review"].as_str().unwrap_or_default().to_string(),
53    }))
54}