Skip to main content

agentmux_srv_core/backend/spawn_env/
run_account.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The account a pane's run launched on, noted on its block at spawn.
5
6use serde_json::json;
7
8use crate::backend::agent_state::RUN_ACCOUNT_META_KEY;
9use crate::backend::obj::{self, Block, MetaMapType};
10use crate::backend::storage::store::Store;
11
12/// Note on `block_id` the account its spawn resolves: its agent's link for
13/// its provider, as the identity injection resolves it. Best-effort; a pane
14/// without it falls back to the agent's current link.
15pub fn note_run_account(mstore: &Store, identity_store: &Store, block_id: &str) {
16    use crate::backend::providers::resolve_provider_alias;
17    let Ok(Some(block)) = mstore.get::<Block>(block_id) else {
18        return;
19    };
20    let agent = obj::meta_get_string(&block.meta, "agentId", "");
21    let Some(def) = mstore.agent_def_list().ok().and_then(|d| d.into_iter().find(|d| !agent.is_empty() && d.id == agent)) else {
22        return;
23    };
24    let provider = resolve_provider_alias(&def.provider).to_string();
25    let account = crate::identity::resolver::effective_identity_links(mstore, identity_store, &def)
26        .unwrap_or_default()
27        .into_iter()
28        // A Claude agent's account is its plan or, when it is moved onto one, its Anthropic API key.
29        .find(|l| resolve_provider_alias(&l.provider) == provider || (provider == "claude" && l.provider.eq_ignore_ascii_case("anthropic")))
30        .map(|l| json!(l.account_id))
31        .unwrap_or(serde_json::Value::Null);
32    if block.meta.get(RUN_ACCOUNT_META_KEY).unwrap_or(&serde_json::Value::Null) == &account {
33        return;
34    }
35    let mut meta = MetaMapType::new();
36    meta.insert(RUN_ACCOUNT_META_KEY.to_string(), account);
37    if let Err(e) = crate::backend::wcore::object_meta::update_object_meta(mstore, &format!("block:{block_id}"), &meta) {
38        tracing::warn!(block_id, error = %e, "agent.spawn: couldn't note the account a pane runs on");
39    }
40}