Skip to main content

agentmux_srv/server/reactive/
former_slug.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Former slugs on this instance's message routes
5//! (`docs/specs/SPEC_FORK_SLUG_MIGRATION_2026_10_22.md` Phase 1): a slug an
6//! agent was renamed from still reaches it, and never speaks for it. So does a
7//! display name it was renamed from, by its route (`Store::former_name_owner`
8//! answers for both kinds).
9//!
10//! The registries other instances write are keyed by the name an agent runs
11//! under now, so a miss on a former slug is looked up again under its current
12//! one. The request itself still carries the name it was sent to, because its
13//! signatures cover it; the instance that runs the agent resolves the former
14//! slug the same way (`Handler`'s former slug resolver). Every lookup here
15//! runs only after the name missed as it was sent.
16
17use std::path::Path;
18
19use super::{agent_registry, AppState, InjectionRequest};
20use crate::backend::reactive::registry::AgentEntry;
21use crate::backend::storage::agents::FormerSlugOwner;
22
23/// What a transcript request for `agent` answers when no pane holds it: an agent that exists but has no open pane
24/// (stopped, parked, between restarts) is told apart from a name nobody has. The prefix stays "agent not found",
25/// which the forwarding code keys on.
26pub(crate) fn transcript_not_found_error(known: bool, agent: &str) -> String {
27    if known {
28        format!("agent not found: '{agent}' is a known agent with no open pane on this machine; Agents watch reads its stored transcript")
29    } else {
30        "agent not found".to_string()
31    }
32}
33
34/// The agent `name` is a former slug or display name of, unless a live
35/// registration here holds `name` itself (a live name always wins).
36pub(super) fn owner(state: &AppState, name: &str) -> Option<FormerSlugOwner> {
37    if state.reactive_handler.has_live_name(name) {
38        return None;
39    }
40    state.mstore.former_name_owner(name).ok().flatten()
41}
42
43/// The slug a message to `name` is looked for under when nothing is
44/// registered as `name`: the agent's whose current display name routes to it
45/// ("Naki-2" for "Naki #2"; registries file an agent under the slug it runs
46/// as), else the agent's it is a former name of. A live name here is its
47/// holder's. The request still goes out addressed to `name`, so its
48/// signatures cover what the sender signed (#1459).
49pub(super) fn delivery_alias(state: &AppState, name: &str) -> Option<String> {
50    if state.reactive_handler.has_live_name(name) {
51        return None;
52    }
53    match state.mstore.display_route_owner(name) {
54        Ok(Some(owner)) => Some(owner.slug),
55        _ => state.mstore.former_name_owner(name).ok().flatten().map(|o| o.slug),
56    }
57}
58
59/// The same-channel registry entry for `target`, else for the slug `alias`
60/// it was renamed to, with the name it was found under.
61pub(super) fn registry_entry(data_dir: &Path, target: &str, alias: Option<&str>) -> Option<(String, AgentEntry)> {
62    std::iter::once(target)
63        .chain(alias)
64        .find_map(|name| agent_registry::lookup(data_dir, name).map(|entry| (name.to_string(), entry)))
65}
66
67/// The cross-channel registry's entries for `target`, else for `alias`, with
68/// the name they were found under.
69pub(super) fn shared_entries(shared_dir: &Path, target: &str, alias: Option<&str>) -> (String, Vec<AgentEntry>) {
70    let found = agent_registry::lookup_all_shared(shared_dir, target);
71    match alias {
72        Some(alias) if found.is_empty() => (alias.to_string(), agent_registry::lookup_all_shared(shared_dir, alias)),
73        _ => (target.to_string(), found),
74    }
75}
76
77/// Whether a failed forward to an entry found under `found_as` may evict it:
78/// only an entry found under the name as sent. One found under the current
79/// slug failed for a name its instance may not know; that says nothing about
80/// whether the agent is still there.
81pub(super) fn may_evict(found_as: &str, req: &InjectionRequest) -> bool {
82    found_as == req.target_agent
83}
84
85/// A former slug never speaks (Phase 1, "identity is not aliased"): the
86/// handler's rule (`refuse_former_slug_sender`), which every delivery also
87/// passes through, applied here while verifying so a forwarded request
88/// carries the verdict too. `true` when it refused the claim.
89pub(super) fn refuse_former_sender(state: &AppState, req: &mut InjectionRequest) -> bool {
90    crate::backend::reactive::handler::refuse_former_slug_sender(
91        req,
92        |name| state.reactive_handler.has_live_name(name),
93        |name| state.mstore.former_name_owner(name).ok().flatten().map(|o| o.uid),
94    )
95}
96
97/// The host key a request signed as `name` is checked with, outside jekt
98/// delivery (UI automation's `verified_block_id`): none for a former slug,
99/// which never signs, whatever key is still filed under it.
100pub(crate) fn signing_key(state: &AppState, name: &str) -> Result<Vec<u8>, String> {
101    let former = crate::backend::reactive::handler::former_slug_claim(
102        name,
103        |n| state.reactive_handler.has_live_name(n),
104        |n| state.mstore.former_name_owner(n).ok().flatten().map(|o| o.uid),
105    );
106    if former.is_some() {
107        tracing::warn!(agent_id = %name, "signed as a name its agent was renamed from, which can't sign: refused");
108        return Err(format!("agent_id={name:?} is a name its agent was renamed from, which can't sign"));
109    }
110    match state.mstore.agent_jekt_key_load(name) {
111        Ok(Some(k)) => Ok(k),
112        Ok(None) => Err(format!("no signing key on file for agent_id={name:?} — respawn this agent to get one")),
113        Err(e) => Err(format!("load signing key: {e}")),
114    }
115}
116
117/// `GET /agentmux/reactive/agent?id=` for a former slug: the agent's current
118/// name, and only the public key filed under that name — never one left
119/// under the former slug — when the agent alone owns it. A peer re-pins
120/// under the current name; the former one gets no key to verify against.
121pub(super) fn renamed_lookup(state: &AppState, id: &str, value: &mut serde_json::Value) {
122    let Some(owner) = owner(state, id) else {
123        return;
124    };
125    let Some(obj) = value.as_object_mut() else {
126        return;
127    };
128    obj.remove("lan_public_key");
129    obj.insert("current_name".into(), serde_json::json!(owner.slug));
130    if crate::server::actor::uid_owns_name(&state.mstore, &owner.uid, &owner.slug) {
131        if let Ok(Some(pubkey)) = state.mstore.agent_lan_public_key_load(&owner.slug) {
132            obj.insert("lan_public_key".into(), serde_json::json!(pubkey));
133        }
134    }
135}
136
137#[cfg(test)]
138#[path = "../tests/reactive/former_slug_tests.rs"]
139mod tests;