Skip to main content

agentmux_srv/server/agent_handlers/identity/
links.rs

1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The agent ↔ identity junction RPCs (`linkagentidentity`, `unlinkagentidentity`), moved out of
5//! `identity::register` unchanged as named functions.
6
7use std::sync::Arc;
8
9use crate::backend::eventbus::EventBus;
10use crate::backend::mps::Broker;
11use crate::backend::rpc_types::{CommandLinkAgentIdentityData, CommandUnlinkAgentIdentityData, UnlinkAgentIdentityResult};
12use crate::backend::storage::store::Store;
13
14/// The stores and buses a link change touches: the global identity store holds the links, the pane store the
15/// panes whose failure a rebind clears.
16pub(super) struct LinkDeps {
17    pub identity_store: Arc<Store>,
18    pub broker: Arc<Broker>,
19    pub panes: Arc<Store>,
20    pub event_bus: Arc<EventBus>,
21}
22
23pub(super) fn link_agent_identity(deps: &LinkDeps, cmd: &CommandLinkAgentIdentityData) -> Result<(), String> {
24    let LinkDeps { identity_store, broker, panes, event_bus } = deps;
25    // Read before the link changes: an old pane counts against the agent's current links
26    // (SPEC_UNRESPONSIVE_AGENTS_2026_10_23.md §6.3).
27    let failed = crate::server::account_episodes::failed_panes(panes, identity_store, &cmd.agent_id, &cmd.provider);
28    identity_store
29        .agent_identity_link(&cmd.agent_id, &cmd.account_id, &cmd.provider)
30        .map_err(|e| format!("linkagentidentity: {e}"))?;
31    crate::server::account_episodes::after_rebind(panes, event_bus, failed, &cmd.account_id);
32    broker.publish_event_for::<crate::backend::mps_events::AgentIdentitiesChanged>(&cmd.agent_id, &(), vec![], 0);
33    // See app_api/identity.rs's identical call: watch_agent's one-shot config-dir resolution can race ahead of
34    // this exact binding.
35    if let Some(watcher) = crate::backend::subagent_watcher::global() {
36        watcher.recheck_all_watched_agents();
37    }
38    Ok(())
39}
40
41pub(super) fn unlink_agent_identity(deps: &LinkDeps, cmd: &CommandUnlinkAgentIdentityData) -> Result<UnlinkAgentIdentityResult, String> {
42    let LinkDeps { identity_store, broker, panes, event_bus } = deps;
43    let failed = crate::server::account_episodes::failed_panes(panes, identity_store, &cmd.agent_id, &cmd.provider);
44    let removed = identity_store
45        .agent_identity_unlink(&cmd.agent_id, &cmd.provider)
46        .map_err(|e| format!("unlinkagentidentity: {e}"))?;
47    // info!, not debug!: the production filter is "agentmuxsrv=info,info". "identity.unlink:" is `muxlog auth`
48    // vocabulary.
49    tracing::info!(
50        agent_id = %cmd.agent_id,
51        provider = %cmd.provider,
52        unlinked = removed,
53        "identity.unlink: agent-identity link removed (unlinkagentidentity)"
54    );
55    if removed {
56        // A silent unlink is the first half of a rebind (alias rows), the link that follows clears; a real one
57        // leaves the agent without an account, so its old limit goes.
58        if !cmd.silent {
59            crate::server::account_episodes::after_rebind(panes, event_bus, failed, "");
60        }
61        broker.publish_event_for::<crate::backend::mps_events::AgentIdentitiesChanged>(&cmd.agent_id, &(), vec![], 0);
62        // Spec §3 names unlink alongside delete: a live process for this agent still holds the unlinked
63        // account's tokens until restarted, so the same disclosure chip. Skipped for `silent` unlinks (alias
64        // migration, not a real unbind: see CommandUnlinkAgentIdentityData).
65        if !cmd.silent {
66            let revoked = crate::backend::mps_events::CredentialsRevokedEvent {
67                credentials_revoked: true,
68                provider: cmd.provider.clone(),
69                account_id: None,
70            };
71            broker.publish_event_for::<crate::backend::mps_events::AgentCredentialsRevoked>(&cmd.agent_id, &revoked, vec![], 0);
72        }
73    }
74    Ok(UnlinkAgentIdentityResult { unlinked: removed })
75}