Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/aien-switchboard/tests/router.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / aien-switchboard / tests / router.rs` 411 lines ยท 14448 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use aien_switchboard::*; 17 18 const RULES: &str = r#" 19 [settings] 20 ceiling_percent = 90 21 default_backoff_secs = 600 22 23 [[accounts]] 24 id = "anthropic-1" 25 provider = "anthropic" 26 program = "claude" 27 settings_dir = "/tmp/sb/anthropic-1" 28 29 [[accounts]] 30 id = "google-1" 31 provider = "google" 32 program = "agy" 33 settings_dir = "/tmp/sb/google-1" 34 35 [[accounts]] 36 id = "openrouter-1" 37 provider = "openrouter" 38 program = "http" 39 key_ref = "openrouter-key" 40 41 [[accounts]] 42 id = "local" 43 provider = "local" 44 program = "aien" 45 46 [routes] 47 code = ["anthropic-1", "google-1", "openrouter-1", "local"] 48 review = ["google-1", "anthropic-1"] 49 default = ["openrouter-1"] 50 "#; 51 52 fn rules() -> RuleTable { 53 RuleTable::from_toml_str(RULES).unwrap() 54 } 55 fn id(s: &str) -> AccountId { 56 AccountId::new(s) 57 } 58 fn code() -> Job { 59 Job::new(JobKind::Code, JobSize::Small) 60 } 61 fn win(pct: f64, resets_at: Option) -> Vec { 62 vec![Window { 63 name: "five_hour".into(), 64 used_percent: pct, 65 resets_at, 66 }] 67 } 68 69 #[test] 70 fn rule_order_is_respected() { 71 let r = rules(); 72 let d = route(&code(), &r, &Gauges::new(), 1000); 73 assert_eq!(d.account, id("anthropic-1")); 74 assert_eq!( 75 d.alternates, 76 vec![id("google-1"), id("openrouter-1"), id("local")] 77 ); 78 let d = route( 79 &Job::new(JobKind::Review, JobSize::Small), 80 &r, 81 &Gauges::new(), 82 1000, 83 ); 84 assert_eq!(d.account, id("google-1")); 85 } 86 87 #[test] 88 fn unlisted_kind_uses_default_and_local_is_appended() { 89 let r = rules(); 90 assert_eq!( 91 r.route_for(JobKind::Research), 92 &[id("openrouter-1"), id("local")] 93 ); 94 assert_eq!( 95 r.route_for(JobKind::Review), 96 &[id("google-1"), id("anthropic-1"), id("local")] 97 ); 98 } 99 100 #[test] 101 fn cooled_down_account_is_skipped_with_a_plain_reason() { 102 let r = rules(); 103 let mut g = Gauges::new(); 104 g.on_limit_hit(&id("anthropic-1"), Some(5000), 1000, 600); 105 let d = route(&code(), &r, &g, 1000); 106 assert_eq!(d.account, id("google-1")); 107 assert!( 108 d.reason.contains("anthropic-1 is cooling down"), 109 "{}", 110 d.reason 111 ); 112 } 113 114 #[test] 115 fn account_over_the_ceiling_is_skipped_and_stale_window_is_not() { 116 let r = rules(); 117 let mut g = Gauges::new(); 118 g.set_windows(&id("anthropic-1"), win(95.0, Some(5000))); 119 let d = route(&code(), &r, &g, 1000); 120 assert_eq!(d.account, id("google-1")); 121 assert!(d.reason.contains("95% used"), "{}", d.reason); 122 // Once the window's reset time has passed, the account has room again. 123 let d = route(&code(), &r, &g, 5000); 124 assert_eq!(d.account, id("anthropic-1")); 125 // Exactly under the ceiling is fine. 126 g.set_windows(&id("anthropic-1"), win(89.9, None)); 127 assert_eq!(route(&code(), &r, &g, 1000).account, id("anthropic-1")); 128 } 129 130 #[test] 131 fn everything_exhausted_goes_to_local() { 132 let r = rules(); 133 let mut g = Gauges::new(); 134 for a in ["anthropic-1", "google-1", "openrouter-1"] { 135 g.on_limit_hit(&id(a), None, 1000, 600); 136 } 137 let d = route(&code(), &r, &g, 1000); 138 assert_eq!(d.account, id("local")); 139 assert!(d.alternates.is_empty()); 140 assert!( 141 d.reason.contains("stays on the local model"), 142 "{}", 143 d.reason 144 ); 145 } 146 147 #[test] 148 fn cooldown_expires() { 149 let r = rules(); 150 let mut g = Gauges::new(); 151 let until = g.on_limit_hit(&id("anthropic-1"), None, 1000, 600); 152 assert_eq!(until, 1600); 153 assert_eq!(route(&code(), &r, &g, 1599).account, id("google-1")); 154 assert_eq!(route(&code(), &r, &g, 1600).account, id("anthropic-1")); 155 } 156 157 #[test] 158 fn reset_time_in_the_past_falls_back_to_default_backoff_and_longer_cooldown_is_kept() { 159 let mut g = Gauges::new(); 160 assert_eq!(g.on_limit_hit(&id("a"), Some(500), 1000, 600), 1600); 161 assert_eq!(g.on_limit_hit(&id("a"), Some(9000), 1000, 600), 9000); 162 assert_eq!(g.on_limit_hit(&id("a"), None, 1000, 600), 9000); 163 } 164 165 #[test] 166 fn failover_on_limit_hit_then_cooldown_sticks() { 167 let r = rules(); 168 let mut g = Gauges::new(); 169 let mut run = FakeRunner::new(); 170 run.push( 171 &id("anthropic-1"), 172 RunResult::LimitHit { 173 resets_at: Some(4000), 174 }, 175 ); 176 let done = run_job(&code(), "hi", &r, &mut g, &mut run, 1000).unwrap(); 177 assert_eq!(done.account, id("google-1")); 178 assert_eq!(done.tried, vec![id("anthropic-1"), id("google-1")]); 179 assert!(done.output.contains("google-1")); 180 // The next job skips the cooled account without trying it. 181 let done = run_job(&code(), "again", &r, &mut g, &mut run, 1001).unwrap(); 182 assert_eq!(done.tried, vec![id("google-1")]); 183 } 184 185 #[test] 186 fn all_outside_accounts_hit_limits_then_local_answers() { 187 let r = rules(); 188 let mut g = Gauges::new(); 189 let mut run = FakeRunner::new(); 190 for a in ["anthropic-1", "google-1", "openrouter-1"] { 191 run.push(&id(a), RunResult::LimitHit { resets_at: None }); 192 } 193 let done = run_job(&code(), "x", &r, &mut g, &mut run, 1000).unwrap(); 194 assert_eq!(done.account, id("local")); 195 assert_eq!(done.tried.len(), 4); 196 } 197 198 #[test] 199 fn local_limit_hit_is_a_clear_error_not_a_loop() { 200 let r = rules(); 201 let mut g = Gauges::new(); 202 let mut run = FakeRunner::new(); 203 run.push(&id("local"), RunResult::LimitHit { resets_at: None }); 204 let only_local = RuleTable::from_toml_str( 205 "[[accounts]]\nid=\"local\"\nprovider=\"local\"\nprogram=\"aien\"\n[routes]\ndefault=[\"local\"]\n", 206 ) 207 .unwrap(); 208 let e = run_job(&code(), "x", &only_local, &mut g, &mut run, 1000).unwrap_err(); 209 assert!(e.to_string().contains("out of room"), "{e}"); 210 let _ = r; 211 } 212 213 #[test] 214 fn local_listed_early_is_moved_last() { 215 let r = RuleTable::from_toml_str( 216 "[[accounts]]\nid=\"local\"\nprovider=\"local\"\nprogram=\"aien\"\n\ 217 [[accounts]]\nid=\"g1\"\nprovider=\"google\"\nprogram=\"agy\"\nsettings_dir=\"/tmp/sb/g1\"\n\ 218 [routes]\ndefault=[\"local\",\"g1\"]\n", 219 ) 220 .unwrap(); 221 assert_eq!(r.route_for(JobKind::Code), &[id("g1"), id("local")]); 222 } 223 224 fn parse_err(text: &str) -> String { 225 RuleTable::from_toml_str(text).unwrap_err().to_string() 226 } 227 228 #[test] 229 fn parse_errors_are_clear() { 230 // Not TOML at all: parser reports line and column. 231 let e = parse_err("[routes\ndefault = ["); 232 assert!(e.starts_with("rule file is not valid:"), "{e}"); 233 assert!(e.contains("line 1"), "{e}"); 234 // Missing the required default route. 235 assert!(parse_err("[routes]\n").contains("default")); 236 // Misspelled key. 237 assert!(parse_err("[routes]\ndefalt = []\n").contains("defalt")); 238 // Unknown provider. 239 let e = parse_err( 240 "[[accounts]]\nid=\"a\"\nprovider=\"bing\"\nprogram=\"x\"\n[routes]\ndefault=[\"a\"]\n", 241 ); 242 assert!(e.contains("bing"), "{e}"); 243 } 244 245 #[test] 246 fn rule_mistakes_name_the_problem() { 247 let local = "[[accounts]]\nid=\"local\"\nprovider=\"local\"\nprogram=\"aien\"\n"; 248 let g1 = "[[accounts]]\nid=\"g1\"\nprovider=\"google\"\nprogram=\"agy\"\nsettings_dir=\"/tmp/sb/g1\"\n"; 249 assert!(parse_err(&format!("{g1}[routes]\ndefault=[\"g1\"]\n")) 250 .contains("no account with provider = \"local\"")); 251 assert!(parse_err(&format!("{local}[routes]\ndefault=[\"nope\"]\n")) 252 .contains("\"nope\", which is not in [[accounts]]")); 253 assert!(parse_err(&format!("{local}[routes]\ndefault=[]\n")).contains("empty list")); 254 assert!( 255 parse_err(&format!("{local}{local}[routes]\ndefault=[\"local\"]\n")) 256 .contains("listed twice") 257 ); 258 assert!( 259 parse_err(&format!("{local}{g1}[routes]\ndefault=[\"g1\",\"g1\"]\n")) 260 .contains("lists g1 twice") 261 ); 262 assert!(parse_err(&format!( 263 "[settings]\nceiling_percent=150\n{local}[routes]\ndefault=[\"local\"]\n" 264 )) 265 .contains("ceiling_percent")); 266 let both = "[[accounts]]\nid=\"x\"\nprovider=\"xai\"\nprogram=\"grok\"\nsettings_dir=\"/a\"\nkey_ref=\"k\"\n"; 267 assert!(parse_err(&format!("{local}{both}[routes]\ndefault=[\"x\"]\n")).contains("pick one")); 268 let secret = "[[accounts]]\nid=\"o\"\nprovider=\"openrouter\"\nprogram=\"http\"\nkey_ref=\"sk-or v1 abc def\"\n"; 269 assert!( 270 parse_err(&format!("{local}{secret}[routes]\ndefault=[\"o\"]\n")) 271 .contains("never the key itself") 272 ); 273 } 274 275 #[test] 276 fn missing_file_is_a_read_error() { 277 let e = 278 RuleTable::from_path(std::path::Path::new("/nonexistent/switchboard.toml")).unwrap_err(); 279 assert!(e.to_string().starts_with("cannot read rule file:"), "{e}"); 280 } 281 282 #[test] 283 fn routing_is_deterministic() { 284 let r = rules(); 285 let mut g = Gauges::new(); 286 g.set_windows(&id("anthropic-1"), win(91.0, None)); 287 let a = route(&code(), &r, &g, 1000); 288 let b = route(&code(), &r, &g, 1000); 289 assert_eq!(a, b); 290 } 291 292 const LOCAL: &str = "[[accounts]]\nid=\"local\"\nprovider=\"local\"\nprogram=\"aien\"\n"; 293 294 fn sub(id: &str, provider: &str, program: &str) -> String { 295 format!("[[accounts]]\nid=\"{id}\"\nprovider=\"{provider}\"\nprogram=\"{program}\"\nsettings_dir=\"/tmp/sb/{id}\"\n") 296 } 297 298 #[test] 299 fn route_with_two_subscriptions_of_one_provider_is_rejected() { 300 // Operator decision 2026-10-16 (aien-architecture#236, issue #592): no automatic 301 // routing between two subscription accounts of the same provider. 302 let accts = format!( 303 "{LOCAL}{}{}", 304 sub("a1", "anthropic", "claude"), 305 sub("a2", "anthropic", "claude") 306 ); 307 let e = RuleTable::from_toml_str(&format!( 308 "{accts}[routes]\ndefault=[\"local\"]\ncode=[\"a1\",\"a2\"]\n" 309 )) 310 .unwrap_err(); 311 let m = e.to_string(); 312 assert!(matches!(e, RulesError::Invalid(_)), "{m}"); 313 assert!( 314 m.contains("route \"code\"") && m.contains("a1") && m.contains("a2"), 315 "{m}" 316 ); 317 assert!(m.contains("anthropic"), "{m}"); 318 // The default route is checked too. 319 assert!( 320 RuleTable::from_toml_str(&format!("{accts}[routes]\ndefault=[\"a2\",\"a1\"]\n")).is_err() 321 ); 322 // Same accounts on separate routes are fine: manual launch may open any. 323 assert!(RuleTable::from_toml_str(&format!( 324 "{accts}[routes]\ndefault=[\"local\"]\ncode=[\"a1\"]\nreview=[\"a2\"]\n" 325 )) 326 .is_ok()); 327 } 328 329 #[test] 330 fn subscription_plus_api_key_or_other_provider_is_allowed() { 331 let key = "[[accounts]]\nid=\"a-key\"\nprovider=\"anthropic\"\nprogram=\"http\"\nkey_ref=\"anthropic-console\"\n"; 332 let accts = format!( 333 "{LOCAL}{}{key}{}", 334 sub("a1", "anthropic", "claude"), 335 sub("g1", "google", "agy") 336 ); 337 let r = RuleTable::from_toml_str(&format!( 338 "{accts}[routes]\ndefault=[\"a1\",\"a-key\",\"g1\"]\n" 339 )); 340 assert!(r.is_ok(), "{r:?}"); 341 } 342 343 #[test] 344 fn settings_folders_must_be_absolute_and_not_shared() { 345 let rel = "[[accounts]]\nid=\"a1\"\nprovider=\"anthropic\"\nprogram=\"claude\"\nsettings_dir=\"rel/a1\"\n"; 346 let e = RuleTable::from_toml_str(&format!("{LOCAL}{rel}[routes]\ndefault=[\"local\"]\n")) 347 .unwrap_err(); 348 assert!(e.to_string().contains("absolute"), "{e}"); 349 let shared = "[[accounts]]\nid=\"a2\"\nprovider=\"anthropic\"\nprogram=\"claude\"\nsettings_dir=\"/tmp/sb/a1\"\n"; 350 let e = RuleTable::from_toml_str(&format!( 351 "{LOCAL}{}{shared}[routes]\ndefault=[\"local\"]\n", 352 sub("a1", "anthropic", "claude") 353 )) 354 .unwrap_err(); 355 assert!(e.to_string().contains("same settings_dir"), "{e}"); 356 } 357 358 #[test] 359 fn route_error_cites_the_operator_decision_only() { 360 let accts = format!( 361 "{LOCAL}{}{}", 362 sub("a1", "anthropic", "claude"), 363 sub("a2", "anthropic", "claude") 364 ); 365 let m = RuleTable::from_toml_str(&format!("{accts}[routes]\ndefault=[\"a1\",\"a2\"]\n")) 366 .unwrap_err() 367 .to_string(); 368 assert!(m.contains("aien-architecture#236"), "{m}"); 369 assert!(!m.contains("amendment"), "{m}"); 370 } 371 372 #[test] 373 fn non_local_account_without_a_login_setting_is_refused() { 374 // A claude account with no settings_dir would use the default ~/.claude 375 // login, a subscription the route rule could not see. Review of #619. 376 let bare = "[[accounts]]\nid=\"a2\"\nprovider=\"anthropic\"\nprogram=\"claude\"\n"; 377 let accts = format!("{LOCAL}{}{bare}", sub("a1", "anthropic", "claude")); 378 let e = RuleTable::from_toml_str(&format!( 379 "{accts}[routes]\ndefault=[\"local\"]\ncode=[\"a1\",\"a2\"]\n" 380 )) 381 .unwrap_err(); 382 assert!(matches!(e, RulesError::Invalid(_)), "{e}"); 383 assert!(e.to_string().contains("\"a2\""), "{e}"); 384 // Not routed anywhere: still refused, for any non-local provider. 385 let g = "[[accounts]]\nid=\"g\"\nprovider=\"google\"\nprogram=\"agy\"\n"; 386 let e = RuleTable::from_toml_str(&format!("{LOCAL}{g}[routes]\ndefault=[\"local\"]\n")) 387 .unwrap_err(); 388 assert!(e.to_string().contains("settings_dir or key_ref"), "{e}"); 389 } 390 391 #[test] 392 fn program_must_be_a_bare_name_or_an_absolute_path() { 393 for bad in ["bin/claude", "./claude", "../x/claude"] { 394 let a = format!("[[accounts]]\nid=\"a1\"\nprovider=\"anthropic\"\nprogram=\"{bad}\"\nsettings_dir=\"/tmp/sb/a1\"\n"); 395 let e = RuleTable::from_toml_str(&format!("{LOCAL}{a}[routes]\ndefault=[\"local\"]\n")) 396 .unwrap_err(); 397 assert!(e.to_string().contains("absolute"), "{bad}: {e}"); 398 } 399 let ok = "[[accounts]]\nid=\"a1\"\nprovider=\"anthropic\"\nprogram=\"/opt/claude/bin/claude\"\nsettings_dir=\"/tmp/sb/a1\"\n"; 400 assert!( 401 RuleTable::from_toml_str(&format!("{LOCAL}{ok}[routes]\ndefault=[\"local\"]\n")).is_ok() 402 ); 403 } 404 405 #[test] 406 fn settings_dir_with_parent_steps_is_refused() { 407 let a = "[[accounts]]\nid=\"a2\"\nprovider=\"anthropic\"\nprogram=\"claude\"\nsettings_dir=\"/tmp/sb/x/../a1\"\n"; 408 let e = RuleTable::from_toml_str(&format!("{LOCAL}{a}[routes]\ndefault=[\"local\"]\n")) 409 .unwrap_err(); 410 assert!(e.to_string().contains(".."), "{e}"); 411 }