Skip to main content

agentmux_srv/server/remote_ops/
disk_cleanup_op.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `app.disk.cleanup`: ask this machine to clear its agents' idle build folders (spec ยง4.3). Like
5//! `app.upgrade` it starts nothing by itself: it files the `disk_cleanup` sensitive request, so this
6//! machine's super agent decides and the person here sees the banner and can veto the countdown. The
7//! requester is `agent@machine`, never anything that reads as the operator.
8//!
9//! What runs when the countdown ends is the pass a low disk runs (`build_cache_service::clean_when_asked`):
10//! orphan incremental folders, and the `target` of a clone nobody built in for days, never one with a
11//! build running. The older version folders are not touched; they stay a person's call (`app.disk.report`
12//! lists them).
13//!
14//! `{ reason }` files it and answers with the request's id. `{ status: <id> }` reads where a request this
15//! caller made stands.
16
17use serde_json::{json, Value};
18
19use super::{AppState, RemoteCaller, RemoteRefusal, RemoteResult};
20
21/// The request owner for a remote caller: not an agent here, so no local uid stands in for it.
22fn owner(caller: &RemoteCaller) -> String {
23    format!("{}{}", crate::backend::sensitive_requests::REMOTE_OWNER_PREFIX, caller.actor())
24}
25
26pub(super) async fn disk_cleanup(state: &AppState, caller: &RemoteCaller, params: Value) -> RemoteResult {
27    let uid = owner(caller);
28    if let Some(id) = params.get("status").and_then(Value::as_str) {
29        return crate::server::sensitive_handlers::status_for(&uid, id).map_err(RemoteRefusal::failed);
30    }
31    let reason = params.get("reason").and_then(Value::as_str).map(str::trim).filter(|r| !r.is_empty()).unwrap_or_else(|| caller.reason.trim());
32    if reason.is_empty() {
33        return Err(RemoteRefusal::failed("a reason is required"));
34    }
35    // Say so now rather than after a countdown that could only end in a refusal.
36    if !crate::backend::build_cache_service::this_srv_may_clean() {
37        return Err(RemoteRefusal::new(
38            "not_available",
39            "this AgentMux is not the one that cleans build folders on its computer (a development, headless or test instance, or cleaning is turned off for it)",
40        ));
41    }
42    tracing::info!(requester = %caller.actor(), "disk cleanup: a remote agent asks as a sensitive request");
43    let filed = crate::server::sensitive_handlers::file_request(state, &uid, &caller.actor(), "disk_cleanup", &std::collections::BTreeMap::new(), reason)
44        .await
45        .map_err(RemoteRefusal::failed)?;
46    Ok(json!({
47        "status": "requested",
48        "id": filed["id"].as_str().unwrap_or_default(),
49        "review": filed["review"].as_str().unwrap_or_default(),
50    }))
51}