Skip to main content

agentmux_srv_core/backend/model_gateway/
pending_route.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The route a moved agent is waiting to take (docs/specs/SPEC_MOVE_AN_AGENT_TO_ANOTHER_COMPUTER_2026_11_24.md 7.3, "route and key").
5//! A move never copies a key's value, and a route whose key is not in this computer's Vault is not set: the agent arrives on its
6//! harness's own sign-in and says so (`needs a key`) in My Agents. The route it came with is kept here, as a content blob beside the
7//! chosen one ([`super::launch::CONTENT_TYPE`]), and is taken, once, when the key lands: then it is the agent's chosen route and
8//! this is cleared. A route the person chooses meanwhile wins: the pending one is dropped, never put over it.
9
10use super::launch::{self, Choice};
11use crate::backend::storage::content::AgentContent;
12use crate::backend::storage::store::Store;
13use serde::{Deserialize, Serialize};
14
15/// The definition content type that holds the [`Pending`] route.
16pub const CONTENT_TYPE: &str = "agent:model_route_pending";
17
18/// The route and model a moved agent had, and the Vault key (its label) that must be here before it is taken.
19#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
20pub struct Pending {
21    pub harness: String,
22    pub route: String,
23    pub model: String,
24    pub key_label: String,
25}
26
27/// The route agent `def_id` is waiting to take, if any.
28pub fn pending_of(mstore: &Store, def_id: &str) -> Option<Pending> {
29    serde_json::from_str(&mstore.agent_content_get(def_id, CONTENT_TYPE).ok()??.content).ok()
30}
31
32/// Keeps the route `def_id` is waiting to take.
33pub fn set_pending(mstore: &Store, def_id: &str, pending: &Pending) -> Result<(), String> {
34    let content = serde_json::to_string(pending).map_err(|e| e.to_string())?;
35    mstore
36        .agent_content_set(&AgentContent { agent_id: def_id.to_string(), content_type: CONTENT_TYPE.to_string(), content, updated_at: agentmux_common::time::now_ms() })
37        .map_err(|e| e.to_string())
38}
39
40/// What settling a waiting agent came to.
41#[derive(Debug, PartialEq, Eq)]
42pub enum Settled {
43    /// It waits for nothing.
44    Nothing,
45    /// The key is still not here (its label).
46    Waiting(String),
47    /// The key is here: the route is now the agent's chosen one.
48    Taken(Choice),
49    /// The agent has a route of its own now, or the pending one is not in the catalog any longer: it is dropped.
50    Dropped,
51}
52
53/// Takes the pending route of `def_id` when its key is here (`key_present`, by label), unless the agent has chosen a route since.
54pub fn settle(mstore: &Store, def_id: &str, key_present: impl Fn(&str) -> bool) -> Settled {
55    let Some(pending) = pending_of(mstore, def_id) else { return Settled::Nothing };
56    let clear = || {
57        let _ = mstore.agent_content_delete(def_id, CONTENT_TYPE);
58    };
59    if launch::choice_of(mstore, def_id).is_some() {
60        clear();
61        return Settled::Dropped;
62    }
63    if !key_present(&pending.key_label) {
64        return Settled::Waiting(pending.key_label);
65    }
66    match launch::set_choice(mstore, def_id, &pending.harness, &pending.route, &pending.model) {
67        Ok(choice) => {
68            clear();
69            Settled::Taken(choice)
70        }
71        Err(_) => {
72            clear();
73            Settled::Dropped
74        }
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    fn store_with(id: &str) -> Store {
83        let store = Store::open_in_memory().unwrap();
84        let mut def = crate::backend::storage::agents::test_agent_def(id, "Mover", "claude", "host", 1, "");
85        store.agent_def_insert(&mut def).unwrap();
86        store
87    }
88
89    fn waiting() -> Pending {
90        Pending { harness: "claude".into(), route: "openrouter".into(), model: "deepseek/deepseek-v4-pro-0813".into(), key_label: "OpenRouter API key".into() }
91    }
92
93    #[test]
94    fn an_agent_that_waits_for_nothing_settles_to_nothing() {
95        let store = store_with("def-none");
96        assert_eq!(settle(&store, "def-none", |_| true), Settled::Nothing);
97    }
98
99    #[test]
100    fn the_route_waits_for_its_key_and_is_taken_once_when_it_lands() {
101        let store = store_with("def-wait");
102        set_pending(&store, "def-wait", &waiting()).unwrap();
103        assert_eq!(settle(&store, "def-wait", |_| false), Settled::Waiting("OpenRouter API key".into()));
104        assert!(launch::choice_of(&store, "def-wait").is_none(), "no route is set while the key is missing");
105        let Settled::Taken(choice) = settle(&store, "def-wait", |label| label == "OpenRouter API key") else { panic!("the key landed") };
106        assert_eq!(choice.route, "openrouter");
107        assert_eq!(launch::choice_of(&store, "def-wait"), Some(choice));
108        assert!(pending_of(&store, "def-wait").is_none(), "taken once");
109        assert_eq!(settle(&store, "def-wait", |_| true), Settled::Nothing);
110    }
111
112    #[test]
113    fn a_route_chosen_meanwhile_wins_and_the_pending_one_is_dropped() {
114        let store = store_with("def-own");
115        set_pending(&store, "def-own", &waiting()).unwrap();
116        launch::set_choice(&store, "def-own", "claude", "openrouter", "deepseek/deepseek-v4-pro-0813").unwrap();
117        let own = launch::choice_of(&store, "def-own");
118        assert_eq!(settle(&store, "def-own", |_| true), Settled::Dropped);
119        assert_eq!(launch::choice_of(&store, "def-own"), own);
120        assert!(pending_of(&store, "def-own").is_none());
121    }
122
123    #[test]
124    fn a_route_the_catalog_no_longer_has_is_dropped_not_kept_forever() {
125        let store = store_with("def-gone");
126        set_pending(&store, "def-gone", &Pending { route: "no-such-route".into(), ..waiting() }).unwrap();
127        assert_eq!(settle(&store, "def-gone", |_| true), Settled::Dropped);
128        assert!(pending_of(&store, "def-gone").is_none());
129    }
130}