#[derive(Debug, Clone)]
pub struct SchedulerBudget {
pub max_tokens: u32,
pub max_turns: u32,
pub max_total_tokens: u64,
pub max_wall_ms: Option<u64>,
}
impl Default for SchedulerBudget {
fn default() -> Self {
Self {
max_tokens: 128_000,
max_turns: 25,
max_total_tokens: 1_000_000,
max_wall_ms: None,
}
}
}
impl SchedulerBudget {
pub fn should_terminate(
&self,
turns: u32,
total_tokens: u64,
now_ms: Option<u64>,
started_at_ms: Option<u64>,
) -> Option<&'static str> {
if turns >= self.max_turns {
return Some("max_turns");
}
if total_tokens >= self.max_total_tokens {
return Some("token_budget");
}
if let (Some(limit), Some(now), Some(start)) = (self.max_wall_ms, now_ms, started_at_ms) {
if now.saturating_sub(start) >= limit {
return Some("wall_time");
}
}
None
}
}
pub type LoopPolicy = SchedulerBudget;