agentmux_srv_core/identity/stable_account.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A signed-in account's id that is the same on every machine
5//! (`docs/specs/SPEC_AGENTS_OPERATE_OTHER_MACHINES_2026_10_30.md` §4.4).
6//!
7//! An account's local id is a UUID made when it was added, and its label
8//! (`email ·2`) is numbered per machine, so two machines that signed in to one
9//! provider account share neither. The stable id is derived from what the
10//! provider says the account is: for Claude, a hash of the OAuth account and
11//! organization ids its CLI recorded ([`identity_key_from_oauth_dir`]), so one
12//! email in a personal and a Team organization stays two accounts. Without
13//! those ids it falls back to a hash of the provider and email.
14//!
15//! The id is a name, not a secret. It travels to other machines and into logs,
16//! so the provider's own ids and the email are hashed out of it; but an email
17//! is guessable, so the fallback form can be confirmed against a known address
18//! by anyone who sees the id. Two caveats: a sign-in in the provider's ambient
19//! home, or one whose `.claude.json` is unreadable, has no provider ids and gets
20//! the email form, so one provider account can show both forms on different
21//! machines. A matcher across machines treats an email-form id as a weaker
22//! claim and compares the email too.
23
24use sha2::{Digest, Sha256};
25
26use crate::backend::storage::identities::{IdentityAccount, SecretRef};
27
28use super::account_email::identity_key_from_oauth_dir;
29
30/// `<provider>:<16 hex>` for `a`, or `None` when it has neither a provider
31/// identity nor an email (an API key, say). The provider is the canonical one,
32/// so a `claude-code` alias row and a `claude` row of one sign-in agree.
33pub fn stable_account_id(a: &IdentityAccount) -> Option<String> {
34 let provider = match crate::backend::providers::resolve_provider_alias(&a.provider) {
35 "" => a.provider.to_lowercase(),
36 c => c.to_string(),
37 };
38 if let SecretRef::OAuthConfigDir { dir } = &a.secret_ref {
39 if let Some(key) = identity_key_from_oauth_dir(&a.provider, dir) {
40 return Some(format!("{provider}:{key}"));
41 }
42 }
43 let email = a.context.get("email")?.as_str()?.trim().to_lowercase();
44 if email.is_empty() {
45 return None;
46 }
47 let digest = Sha256::digest(format!("{provider}\0email\0{email}").as_bytes());
48 Some(format!("{provider}:e{}", &hex::encode(digest)[..15]))
49}
50
51/// Whether `id` has the shape [`stable_account_id`] makes: a lower-case provider, a colon and a
52/// short token, so a peer cannot put free text where an account id belongs.
53pub fn is_stable_id(id: &str) -> bool {
54 let Some((provider, token)) = id.split_once(':') else { return false };
55 let word = |s: &str| !s.is_empty() && s.len() <= 32 && s.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-');
56 word(provider) && word(token)
57}
58
59/// For each pane block, the stable id of the account its run is on (`agent:run_account`, else the
60/// agent's link): the lookup the LAN agent list reports. The accounts are read once.
61pub fn stable_id_of_block<'a>(
62 mstore: &'a crate::backend::storage::store::Store,
63 id_store: &crate::backend::storage::store::Store,
64 identity_store: &'a crate::backend::storage::store::Store,
65) -> impl Fn(&str) -> Option<String> + 'a {
66 let stable: std::collections::HashMap<String, String> = id_store
67 .identity_list(None)
68 .unwrap_or_default()
69 .iter()
70 .filter_map(|a| Some((a.id.clone(), stable_account_id(a)?)))
71 .collect();
72 move |block_id: &str| {
73 let block = mstore.get::<crate::backend::obj::Block>(block_id).ok()??;
74 crate::backend::agent_state::run_accounts_of_block(identity_store, &block).into_iter().find_map(|a| stable.get(&a).cloned())
75 }
76}
77
78#[cfg(test)]
79#[path = "stable_account_tests.rs"]
80mod tests;