Skip to main content

agentmux_srv/server/
old_versions_handlers.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `GET /agentmux/old-versions`: the plan of the old-version cleanup (`backend/old_versions.rs`),
5//! read only. `POST /agentmux/old-versions` with `{"only": [<folder>, ...]}` removes those of the
6//! plan's folders that are still removable now, and nothing else. Authed like the other agent
7//! routes (`AGENTMUX_LOCAL_URL` and `X-AuthKey`).
8
9use super::*;
10use crate::backend::old_versions as ov;
11
12#[derive(Debug, Default, serde::Deserialize)]
13pub(super) struct RemoveReq {
14    #[serde(default)]
15    only: Vec<String>,
16}
17
18async fn respond(state: AppState, dry_run: bool, only: Option<Vec<String>>) -> Json<serde_json::Value> {
19    let Some(layout) = ov::layout_from_env() else {
20        return Json(json!({ "error": "this AgentMux has no per-version folders (a dev build or a test home)" }));
21    };
22    let policy = crate::backend::old_versions_service::policy(&state.config_watcher, crate::server::external_host::host().is_some());
23    let shown = layout.clone();
24    let run = tokio::task::spawn_blocking(move || ov::run(&layout, &policy, dry_run, only.as_deref(), std::time::SystemTime::now())).await;
25    match run {
26        Ok(run) => Json(json!({
27            "dry_run": dry_run,
28            "running": shown.running,
29            "channel": shown.channel,
30            "versions_root": shown.versions_root,
31            "scan_ok": run.scan_ok,
32            "reclaimable_bytes": run.plan.reclaimable_bytes(),
33            "plan": run.plan,
34            "report": run.report,
35        })),
36        Err(e) => Json(json!({ "error": format!("old versions: {e}") })),
37    }
38}
39
40pub(super) async fn handle_get(State(state): State<AppState>) -> Json<serde_json::Value> {
41    respond(state, true, None).await
42}
43
44pub(super) async fn handle_post(State(state): State<AppState>, Json(req): Json<RemoveReq>) -> Json<serde_json::Value> {
45    if req.only.is_empty() {
46        return Json(json!({ "error": "name the folders to remove in \"only\" (GET shows the plan first)" }));
47    }
48    respond(state, false, Some(req.only)).await
49}