agentmux_srv_core/backend/model_gateway/
pending_route.rs1use super::launch::{self, Choice};
11use crate::backend::storage::content::AgentContent;
12use crate::backend::storage::store::Store;
13use serde::{Deserialize, Serialize};
14
15pub const CONTENT_TYPE: &str = "agent:model_route_pending";
17
18#[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
27pub 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
32pub 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#[derive(Debug, PartialEq, Eq)]
42pub enum Settled {
43 Nothing,
45 Waiting(String),
47 Taken(Choice),
49 Dropped,
51}
52
53pub 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}