Skip to main content

agentmux_srv/server/remote_ops/
move_key_request.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The Vault request for a key a moved agent's route needs and this computer does not have (SPEC_MOVE_AN_AGENT_TO_ANOTHER_COMPUTER
5//! section 7.3, "route and key"). A move never copies a key's value; the agent arrives on its harness's own sign-in and the person is
6//! asked, in the Vault, to add the key. The request names the agent (by its uid, the id the agent's own requests carry, which is how
7//! its My Agents row and the Vault pane find it) and the key's label and nothing else, and it is the same
8//! request an agent's own `Vault request` would file: the person fills the entry in or dismisses it, and nothing waits on the answer.
9
10use crate::backend::vault::requests::{requests, Asked, VaultRequest};
11
12/// The kind of entry a route's key is.
13const KEY_KIND: &str = "api-key";
14
15/// The request, built from the agent (its uid) and the key's label.
16pub(super) fn request_for(uid: &str, name: &str, key_label: &str) -> VaultRequest {
17    VaultRequest {
18        id: uuid::Uuid::new_v4().to_string(),
19        agent: uid.to_string(),
20        kind: KEY_KIND.to_string(),
21        label: key_label.to_string(),
22        why: format!("{name} moved here from another computer and its model route needs this key. Add it, then choose its route."),
23        requested_ms: agentmux_common::time::now_ms_u64(),
24        // No pane: the agent has not started here, so the person answers it in the Vault.
25        block_id: String::new(),
26        vault: String::new(),
27    }
28}
29
30/// Files the request and leaves it for the person; the agent does not wait on it. A new request is published, so a Vault pane or an
31/// agent row that is already open lists it at once (they learn of requests only from the Vault event).
32pub(super) fn ask(broker: &crate::backend::mps::Broker, uid: &str, name: &str, key_label: &str) {
33    if let Asked::Waiting { new: true, .. } = requests().ask(request_for(uid, name, key_label), false) {
34        crate::server::vault_handlers::publish_change(broker);
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn the_request_names_the_agent_by_its_uid_and_the_key_and_is_of_a_kind_the_vault_knows() {
44        let r = request_for("uid-alpha-1", "Alpha", "OpenRouter API key");
45        assert_eq!((r.agent.as_str(), r.label.as_str()), ("uid-alpha-1", "OpenRouter API key"), "the uid, as an agent's own request carries it, not its slug or name");
46        assert!(crate::backend::vault::kinds::kind(&r.kind).is_some(), "{} is not a Vault kind", r.kind);
47        assert!(r.why.contains("Alpha moved here"), "{}", r.why);
48        assert!(r.block_id.is_empty(), "the agent has no pane here yet");
49    }
50
51    #[tokio::test]
52    async fn asking_leaves_one_request_open_for_the_person() {
53        let state = crate::server::tests::test_state();
54        let label = format!("Move key test {}", uuid::Uuid::new_v4());
55        ask(&state.broker, "beta", "Beta", &label);
56        let open = requests().list().into_iter().filter(|r| r.label == label).count();
57        assert_eq!(open, 1);
58    }
59}