maple_runtime/types/
attention.rs1use super::ids::CouplingId;
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct AttentionBudget {
14 pub total_capacity: u64,
16
17 pub allocated: HashMap<CouplingId, u64>,
19
20 pub safety_reserve: u64,
22
23 pub exhaustion_threshold: f64,
25}
26
27impl AttentionBudget {
28 pub fn new(total_capacity: u64) -> Self {
29 let safety_reserve = total_capacity / 10; Self {
31 total_capacity,
32 allocated: HashMap::new(),
33 safety_reserve,
34 exhaustion_threshold: 0.8,
35 }
36 }
37
38 pub fn used(&self) -> u64 {
40 self.allocated.values().sum()
41 }
42
43 pub fn available(&self) -> u64 {
45 self.total_capacity
46 .saturating_sub(self.used())
47 .saturating_sub(self.safety_reserve)
48 }
49
50 pub fn utilization(&self) -> f64 {
52 if self.total_capacity == 0 {
53 return 0.0;
54 }
55 self.used() as f64 / self.total_capacity as f64
56 }
57
58 pub fn is_exhaustion_imminent(&self) -> bool {
60 self.utilization() > self.exhaustion_threshold
61 }
62
63 pub fn can_allocate(&self, amount: u64) -> bool {
65 self.available() >= amount
66 }
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct AttentionBudgetSpec {
72 pub total_capacity: u64,
74
75 pub safety_reserve: Option<u64>,
77
78 pub exhaustion_threshold: Option<f64>,
80}
81
82impl Default for AttentionBudgetSpec {
83 fn default() -> Self {
84 Self {
85 total_capacity: 10000,
86 safety_reserve: None,
87 exhaustion_threshold: None,
88 }
89 }
90}
91
92#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
94pub enum ExhaustionBehavior {
95 Reject,
97
98 GracefulDegrade,
100
101 Queue,
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
107pub enum AttentionClass {
108 Critical,
110
111 High,
113
114 Normal,
116
117 Low,
119}
120
121impl AttentionClass {
122 pub fn cost_multiplier(&self) -> f64 {
124 match self {
125 AttentionClass::Critical => 2.0,
126 AttentionClass::High => 1.5,
127 AttentionClass::Normal => 1.0,
128 AttentionClass::Low => 0.5,
129 }
130 }
131}
132
133#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct AttentionConfig {
136 pub default_capacity: u64,
138
139 pub allow_unlimited: bool,
141
142 pub exhaustion_behavior: ExhaustionBehavior,
144
145 pub enable_rebalancing: bool,
147}
148
149impl Default for AttentionConfig {
150 fn default() -> Self {
151 Self {
152 default_capacity: 10000,
153 allow_unlimited: false,
154 exhaustion_behavior: ExhaustionBehavior::GracefulDegrade,
155 enable_rebalancing: true,
156 }
157 }
158}