Skip to main content

agentmux_srv_core/backend/slug_migration/
locations.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The plan's list of every place an agent's old slug is stored (K1–K13 of
5//! the spec's §3), read through each step's own before-image, so the plan
6//! shows exactly what a run would touch and nothing it wouldn't.
7
8use serde_json::Value;
9
10use super::home::{Home, Install};
11use super::journal::Step;
12use super::plan::Def;
13use super::steps::{before, Agent, Ctx};
14use crate::backend::rpc_types::ForkSlugLocation;
15
16fn loc(key: &str, place: impl Into<String>, value: impl Into<String>) -> ForkSlugLocation {
17    ForkSlugLocation { key: key.to_string(), place: place.into(), value: value.into() }
18}
19
20fn text(v: &Value) -> String {
21    v.as_str().unwrap_or_default().to_string()
22}
23
24pub(super) fn locations(home: &Home, installs: &[Install], def: &Def, new_slug: &str) -> Result<Vec<ForkSlugLocation>, String> {
25    let ctx = Ctx { home, installs, run_dir: Default::default(), backups: &[], retired_at: 0 };
26    let a = Agent { uid: def.uid.clone(), name: def.name.clone(), old: def.slug.clone(), new: new_slug.to_string() };
27    let mut out = vec![loc(
28        "K2",
29        def.path.display().to_string(),
30        if def.former.is_empty() { def.slug.clone() } else { format!("{} (former: {})", def.slug, def.former.join(", ")) },
31    )];
32    for row in before(&ctx, &a, Step::S2)?.as_array().into_iter().flatten() {
33        out.push(loc("K1", text(&row["db"]), row["slug"].as_str().unwrap_or("(no slug)")));
34    }
35    for db in before(&ctx, &a, Step::S3)?.as_array().into_iter().flatten() {
36        for r in db["rows"].as_array().into_iter().flatten() {
37            let key = if text(&r[0]) == "db_agent_wan_keys" { "K5" } else if text(&r[0]) == "db_agent_lan_keys" { "K4" } else { "K3" };
38            out.push(loc(key, text(&db["db"]), format!("{} row under {}", text(&r[0]), text(&r[1]))));
39        }
40    }
41    for db in before(&ctx, &a, Step::S4)?.as_array().into_iter().flatten() {
42        for k in db["keys"].as_array().into_iter().flatten() {
43            out.push(loc("K5", text(&db["db"]), format!("WAN key under {}", text(k))));
44        }
45        let published = db["publications"].as_array().map_or(0, Vec::len);
46        if published > 0 {
47            out.push(loc("K5", text(&db["db"]), format!("published to {published} account directory(ies)")));
48        }
49    }
50    for c in before(&ctx, &a, Step::S5)?.as_array().into_iter().flatten() {
51        out.push(loc("K7", text(c), "MuxBus credential, kept for the grace subscription"));
52    }
53    let name_only = before(&ctx, &a, Step::S6)?;
54    for u in name_only["updates"].as_array().into_iter().flatten() {
55        out.push(loc("K13", text(&u["db"]), format!("{} {} names {}", text(&u["table"]), text(&u["id"]), text(&u["set"][0][1]))));
56    }
57    for g in name_only["grants"].as_array().into_iter().flatten() {
58        out.push(loc("K13", text(&g["db"]), format!("trust grant {} -> {} ({})", text(&g["agent_id"]), text(&g["peer"]), text(&g["tier"]))));
59    }
60    let identity = before(&ctx, &a, Step::S7)?;
61    for r in identity["rows"].as_array().into_iter().flatten() {
62        let what = if text(&r["table"]) == "db_client" { "layout snapshot" } else { "pane" };
63        let keys: Vec<String> = r["changes"].as_array().into_iter().flatten().map(|c| text(&c["key"])).collect();
64        out.push(loc("K12", text(&r["db"]), format!("{what} {}: {}", text(&r["oid"]), keys.join(", "))));
65    }
66    for f in identity["files"].as_array().into_iter().flatten() {
67        out.push(loc("K12", text(f), format!("signs as {}", a.old)));
68    }
69    let files = before(&ctx, &a, Step::S8)?;
70    for m in files["moves"].as_array().into_iter().flatten() {
71        let key = if text(&m["from"]).ends_with(".cwd") { "K11" } else { "K10" };
72        let note = if m["to_existed"].as_bool() == Some(true) { " (the new one exists too: both kept)" } else { "" };
73        out.push(loc(key, text(&m["from"]), format!("moves to {}{note}", text(&m["to"]))));
74    }
75    for d in files["deletes"].as_array().into_iter().flatten() {
76        out.push(loc("K9", text(d), "stale registration, deleted"));
77    }
78    Ok(out)
79}