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
aien-sovereign-core-mv / crates / aien-switchboard / src / router.rs
| 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 | //! The routing decision: pure, deterministic, no clock reads, no I/O. |
| 17 | |
| 18 | use crate::account::AccountId; |
| 19 | use crate::gauge::Gauges; |
| 20 | use crate::job::Job; |
| 21 | use crate::rules::RuleTable; |
| 22 | |
| 23 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 24 | pub struct Decision { |
| 25 | pub account: AccountId, |
| 26 | /// Plain-words explanation, safe to show the operator. |
| 27 | pub reason: String, |
| 28 | /// Later accounts in the same route that also have room, in order. The |
| 29 | /// local account is always the last entry unless it was chosen. |
| 30 | pub alternates: Vec<AccountId>, |
| 31 | } |
| 32 | |
| 33 | /// Why an account has no room right now, or `None` if it has room. |
| 34 | fn no_room(id: &AccountId, rules: &RuleTable, gauges: &Gauges, now: u64) -> Option<String> { |
| 35 | let g = gauges.get(id)?; |
| 36 | if let Some(t) = g.cooled_until.filter(|t| *t > now) { |
| 37 | return Some(format!( |
| 38 | "{id} is cooling down after hitting its limit (back in {} s)", |
| 39 | t - now |
| 40 | )); |
| 41 | } |
| 42 | g.full_window(rules.ceiling_percent(), now).map(|w| { |
| 43 | format!( |
| 44 | "{id} is {:.0}% used in its {} window (ceiling {:.0}%)", |
| 45 | w.used_percent, |
| 46 | w.name, |
| 47 | rules.ceiling_percent() |
| 48 | ) |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | /// Pick the first account in the job's route that has room. The local |
| 53 | /// account is the last entry of every route and is never skipped, so a |
| 54 | /// decision always exists and AIEN works offline. |
| 55 | pub fn route(job: &Job, rules: &RuleTable, gauges: &Gauges, now: u64) -> Decision { |
| 56 | let order = rules.route_for(job.kind); |
| 57 | let mut skipped: Vec<String> = Vec::new(); |
| 58 | let mut chosen: Option<(usize, bool)> = None; // (index, had room) |
| 59 | |
| 60 | for (i, id) in order.iter().enumerate() { |
| 61 | if id == rules.local() { |
| 62 | chosen = Some((i, false)); |
| 63 | break; |
| 64 | } |
| 65 | match no_room(id, rules, gauges, now) { |
| 66 | Some(why) => skipped.push(why), |
| 67 | None => { |
| 68 | chosen = Some((i, true)); |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | // The route always ends with local, so `chosen` is always set. |
| 74 | let (idx, _) = chosen.unwrap_or((order.len() - 1, false)); |
| 75 | let account = order[idx].clone(); |
| 76 | |
| 77 | let mut reason = String::new(); |
| 78 | if account == *rules.local() { |
| 79 | if skipped.is_empty() { |
| 80 | reason.push_str(&format!( |
| 81 | "{account} is the first choice for {} jobs.", |
| 82 | job.kind |
| 83 | )); |
| 84 | } else { |
| 85 | reason.push_str(&format!( |
| 86 | "No outside account has room for this {} job, so it stays on the local model {account}. ", |
| 87 | job.kind |
| 88 | )); |
| 89 | reason.push_str(&format!("Skipped: {}.", skipped.join("; "))); |
| 90 | } |
| 91 | } else { |
| 92 | reason.push_str(&format!( |
| 93 | "{account} is the first account in the {} route with room.", |
| 94 | job.kind |
| 95 | )); |
| 96 | if !skipped.is_empty() { |
| 97 | reason.push_str(&format!(" Skipped: {}.", skipped.join("; "))); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | let mut alternates = Vec::new(); |
| 102 | for id in &order[idx + 1..] { |
| 103 | if id == rules.local() || no_room(id, rules, gauges, now).is_none() { |
| 104 | alternates.push(id.clone()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | Decision { |
| 109 | account, |
| 110 | reason, |
| 111 | alternates, |
| 112 | } |
| 113 | } |