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 / dispatch.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 | //! Route, run, and fail over on a limit hit. |
| 17 | |
| 18 | use crate::account::AccountId; |
| 19 | use crate::gauge::Gauges; |
| 20 | use crate::job::Job; |
| 21 | use crate::router::route; |
| 22 | use crate::rules::RuleTable; |
| 23 | use crate::runner::{RunResult, Runner}; |
| 24 | use std::fmt; |
| 25 | |
| 26 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 27 | pub struct Completed { |
| 28 | pub account: AccountId, |
| 29 | pub output: String, |
| 30 | /// Every account tried, in order, ending with `account`. |
| 31 | pub tried: Vec<AccountId>, |
| 32 | } |
| 33 | |
| 34 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 35 | pub enum DispatchError { |
| 36 | /// Even the local account reported a limit hit. |
| 37 | Exhausted { tried: Vec<AccountId> }, |
| 38 | } |
| 39 | |
| 40 | impl fmt::Display for DispatchError { |
| 41 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 42 | match self { |
| 43 | DispatchError::Exhausted { tried } => { |
| 44 | let t: Vec<&str> = tried.iter().map(|i| i.as_str()).collect(); |
| 45 | write!(f, "every account is out of room (tried: {})", t.join(", ")) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl std::error::Error for DispatchError {} |
| 52 | |
| 53 | /// Run `job` on the best account; on a limit hit, cool that account down and |
| 54 | /// route again. Each account is tried at most once, so this always ends. |
| 55 | pub fn run_job( |
| 56 | job: &Job, |
| 57 | prompt: &str, |
| 58 | rules: &RuleTable, |
| 59 | gauges: &mut Gauges, |
| 60 | runner: &mut dyn Runner, |
| 61 | now: u64, |
| 62 | ) -> Result<Completed, DispatchError> { |
| 63 | let mut tried: Vec<AccountId> = Vec::new(); |
| 64 | loop { |
| 65 | let d = route(job, rules, gauges, now); |
| 66 | if tried.contains(&d.account) { |
| 67 | return Err(DispatchError::Exhausted { tried }); |
| 68 | } |
| 69 | tried.push(d.account.clone()); |
| 70 | // `route` only returns ids from the rule table. |
| 71 | let account = match rules.account(&d.account) { |
| 72 | Some(a) => a, |
| 73 | None => return Err(DispatchError::Exhausted { tried }), |
| 74 | }; |
| 75 | match runner.run(account, job, prompt) { |
| 76 | RunResult::Output(output) => { |
| 77 | return Ok(Completed { |
| 78 | account: d.account, |
| 79 | output, |
| 80 | tried, |
| 81 | }) |
| 82 | } |
| 83 | RunResult::LimitHit { resets_at } => { |
| 84 | gauges.on_limit_hit(&d.account, resets_at, now, rules.default_backoff_secs()); |
| 85 | if d.account == *rules.local() { |
| 86 | return Err(DispatchError::Exhausted { tried }); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |