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/src/gauge.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 / src / gauge.rs` 95 lines ยท 3182 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 //! Usage gauges: how much room an account has, as far as we know. 17 18 use crate::account::AccountId; 19 use std::collections::BTreeMap; 20 21 /// One usage window, for example a five-hour or seven-day window. 22 #[derive(Debug, Clone, PartialEq)] 23 pub struct Window { 24 pub name: String, 25 /// 0.0 to 100.0. 26 pub used_percent: f64, 27 /// Unix seconds when the window refills, when known. 28 pub resets_at: Option, 29 } 30 31 #[derive(Debug, Clone, PartialEq, Default)] 32 pub struct Gauge { 33 pub windows: Vec, 34 /// The account is treated as full until this Unix second. 35 pub cooled_until: Option, 36 } 37 38 impl Gauge { 39 pub fn is_cooled(&self, now: u64) -> bool { 40 matches!(self.cooled_until, Some(t) if t > now) 41 } 42 43 /// The first window at or over `ceiling_percent` that has not refilled 44 /// yet, if any. A window whose reset time has passed is stale and counts 45 /// as empty. 46 pub fn full_window(&self, ceiling_percent: f64, now: u64) -> Option<&Window> { 47 self.windows.iter().find(|w| { 48 let refilled = matches!(w.resets_at, Some(t) if t <= now); 49 !refilled && w.used_percent >= ceiling_percent 50 }) 51 } 52 } 53 54 /// All known gauges. An account with no entry has an empty gauge, which means 55 /// "no information, assume room" (programs that report nothing rely on the 56 /// limit-hit signal instead). 57 #[derive(Debug, Clone, PartialEq, Default)] 58 pub struct Gauges { 59 map: BTreeMap, 60 } 61 62 impl Gauges { 63 pub fn new() -> Self { 64 Gauges::default() 65 } 66 67 pub fn get(&self, id: &AccountId) -> Option<&Gauge> { 68 self.map.get(id) 69 } 70 71 pub fn set_windows(&mut self, id: &AccountId, windows: Vec) { 72 self.map.entry(id.clone()).or_default().windows = windows; 73 } 74 75 /// Record that `id` hit its limit. The cooldown lasts until `resets_at` 76 /// when that is in the future, otherwise `default_backoff_secs` from 77 /// `now`. An existing longer cooldown is kept. Returns the new 78 /// cooled-until second. 79 pub fn on_limit_hit( 80 &mut self, 81 id: &AccountId, 82 resets_at: Option, 83 now: u64, 84 default_backoff_secs: u64, 85 ) -> u64 { 86 let until = match resets_at { 87 Some(t) if t > now => t, 88 _ => now.saturating_add(default_backoff_secs), 89 }; 90 let g = self.map.entry(id.clone()).or_default(); 91 let until = g.cooled_until.map_or(until, |old| old.max(until)); 92 g.cooled_until = Some(until); 93 until 94 } 95 }