Skip to main content

agentmux_srv/server/account_episodes/
key_account.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! An Anthropic API key as an account a Claude agent can be moved onto
5//! (`docs/specs/SPEC_AGENT_PANE_API_KEY_ACCOUNT_SWITCH_2026_11_16.md`).
6//!
7//! A key account is stored under provider `anthropic`, not `claude`, and its link stays under `anthropic`: the spawn
8//! then carries it through the gateway's key route (`identity::resolver::inject_steps::inject_key_route`), so the agent
9//! never holds the key. An agent has exactly one payer: moving it onto a key removes its plan link, and moving it onto a
10//! plan removes the key link. Nothing here is reached by the automatic moves: a failover picks only accounts of the
11//! agent's own provider, and `machine::scan` leaves a pane on a key out of the episodes.
12
13use crate::backend::model_gateway::{catalog::KEY_ROUTE_ID, launch, token};
14use crate::backend::obj;
15use crate::backend::storage::identities::IdentityAccount;
16use crate::server::AppState;
17
18use super::canonical;
19
20/// An Anthropic API key: provider `anthropic`, kind `api_key`.
21pub fn is_key(a: &IdentityAccount) -> bool {
22    a.kind == "api_key" && a.provider.eq_ignore_ascii_case(KEY_ROUTE_ID)
23}
24
25/// Whether `a` can be the account of an agent whose (canonical) provider is `provider`: an account of that provider, or,
26/// for a Claude agent, an Anthropic API key.
27pub(super) fn serves(provider: &str, a: &IdentityAccount) -> bool {
28    canonical(&a.provider) == provider || (provider == "claude" && is_key(a))
29}
30
31/// Whether a link stored under `link_provider` is one of the account links of an agent whose provider is `provider`.
32pub(super) fn is_link_of(provider: &str, link_provider: &str) -> bool {
33    canonical(link_provider) == provider || (provider == "claude" && link_provider.eq_ignore_ascii_case(KEY_ROUTE_ID))
34}
35
36/// The provider a link to `to` is stored under for an agent of `provider`: a key's own, else the agent's.
37pub(super) fn link_provider<'a>(to: &IdentityAccount, provider: &'a str) -> &'a str {
38    if is_key(to) {
39        KEY_ROUTE_ID
40    } else {
41        provider
42    }
43}
44
45/// Why `def_id` cannot be moved onto a key account now, or `Ok`. An agent on a model route is billed to the route. A
46/// container agent can take a key: its container reaches the gateway, never the key (`container::key_route`), and a
47/// container with no way to the gateway says so at the turn, with what to do instead.
48pub(super) fn can_take_a_key(state: &AppState, def_id: &str) -> Result<(), String> {
49    if launch::choice_of(&state.mstore, def_id).is_some() {
50        return Err("ACCOUNT_CANT_RUN: this agent runs on a model route, which is billed to the route's key; move it back to its own sign-in first".to_string());
51    }
52    Ok(())
53}
54
55/// The agent just left its API key: its gateway token stops working now, whatever a running process still holds.
56pub(super) fn left_the_key(def_id: &str) {
57    token::revoke(def_id, KEY_ROUTE_ID);
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    fn account(provider: &str, kind: &str) -> IdentityAccount {
65        IdentityAccount {
66            id: "a".into(),
67            name: "n".into(),
68            provider: provider.into(),
69            kind: kind.into(),
70            display_name: String::new(),
71            secret_ref: crate::backend::storage::identities::SecretRef::Env { env_var: "X".into() },
72            context: serde_json::json!({}),
73            status: "valid".into(),
74            created_at: 0,
75            updated_at: 0,
76        }
77    }
78
79    #[test]
80    fn only_an_anthropic_api_key_is_a_key_account() {
81        assert!(is_key(&account("anthropic", "api_key")));
82        assert!(!is_key(&account("anthropic", "oauth")));
83        assert!(!is_key(&account("openai", "api_key")));
84        assert!(!is_key(&account("claude", "oauth")));
85    }
86
87    #[test]
88    fn a_claude_agent_is_served_by_plans_and_keys_and_no_other_agent_by_a_key() {
89        assert!(serves("claude", &account("claude", "oauth")));
90        assert!(serves("claude", &account("claude-code", "oauth")));
91        assert!(serves("claude", &account("anthropic", "api_key")));
92        assert!(!serves("codex", &account("anthropic", "api_key")), "another harness is not offered an Anthropic key");
93        assert!(!serves("claude", &account("openai", "api_key")));
94        assert!(is_link_of("claude", "anthropic") && is_link_of("claude", "claude-code") && !is_link_of("codex", "anthropic"));
95    }
96
97    #[test]
98    fn a_key_is_linked_under_its_own_provider_and_a_plan_under_the_agents() {
99        assert_eq!(link_provider(&account("anthropic", "api_key"), "claude"), "anthropic");
100        assert_eq!(link_provider(&account("claude", "oauth"), "claude"), "claude");
101    }
102}