use delegation::usage::UsageLedger;
use durable::ConversationId;
use harness::chat::CellCall;
use harness::chat::gate::{GateRun, RequestGate, ToolBlock};
use std::collections::HashMap;
use std::sync::{Arc, Mutex, PoisonError};
use crate::session_stats::SessionStats;
pub const FINAL_ANSWER_NOTE: &str =
"Tools are off for your last reply: answer now with what you have, and say what remains unknown or unfinished.";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FinalAnswer {
Pending,
Final,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct LaneRun {
pub lane: String,
pub run: String,
}
impl LaneRun {
pub fn of(run: &GateRun) -> LaneRun {
LaneRun {
lane: format!("conversation:{}", run.conversation_id.0),
run: run.run.map_or_else(String::new, |run| run.to_string()),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RequestDecision {
Allow,
Final(String),
Block(String),
}
pub type RootOf = Arc<dyn Fn(ConversationId) -> Option<String> + Send + Sync>;
pub struct BudgetGate {
usage: UsageLedger,
stats: Arc<SessionStats>,
root_of: RootOf,
finals: Arc<Mutex<HashMap<LaneRun, FinalAnswer>>>,
}
pub type Finals = Arc<Mutex<HashMap<LaneRun, FinalAnswer>>>;
pub fn frame_finals() -> Finals {
static FINALS: std::sync::LazyLock<Finals> = std::sync::LazyLock::new(Finals::default);
FINALS.clone()
}
impl BudgetGate {
pub fn new(usage: UsageLedger, stats: Arc<SessionStats>, root_of: RootOf) -> Arc<BudgetGate> {
BudgetGate::with_finals(usage, stats, root_of, Finals::default())
}
pub fn with_finals(
usage: UsageLedger,
stats: Arc<SessionStats>,
root_of: RootOf,
finals: Finals,
) -> Arc<BudgetGate> {
Arc::new(BudgetGate {
usage,
stats,
root_of,
finals,
})
}
fn exhaustion(&self, root: Option<&str>) -> Option<delegation::usage::Exhaustion> {
self.usage.budget_exhaustion(root).ok().flatten()
}
fn state(&self, run: &LaneRun) -> Option<FinalAnswer> {
self.finals
.lock()
.unwrap_or_else(PoisonError::into_inner)
.get(run)
.copied()
}
fn set(&self, run: &LaneRun, state: FinalAnswer) {
let mut finals = self.finals.lock().unwrap_or_else(PoisonError::into_inner);
finals.retain(|key, _| key.lane != run.lane || key == run);
finals.insert(run.clone(), state);
}
pub fn tool(&self, run: &LaneRun, root: Option<&str>) -> Option<ToolBlock> {
let exhausted = self.exhaustion(root)?;
self.stats.usage_limit_block();
if exhausted.kind != "cost" && self.state(run) != Some(FinalAnswer::Final) {
self.set(run, FinalAnswer::Pending);
return Some(ToolBlock {
reason: format!("{} {FINAL_ANSWER_NOTE}", exhausted.reason),
terminate: false,
});
}
Some(ToolBlock {
reason: exhausted.reason,
terminate: true,
})
}
pub fn request(&self, run: &LaneRun, root: Option<&str>) -> RequestDecision {
let Some(exhausted) = self.exhaustion(root) else {
return RequestDecision::Allow;
};
if exhausted.kind != "cost" && self.state(run) != Some(FinalAnswer::Final) {
self.set(run, FinalAnswer::Final);
return RequestDecision::Final(format!("[Ultron] {} {FINAL_ANSWER_NOTE}", exhausted.reason));
}
self.stats.usage_limit_block();
RequestDecision::Block(exhausted.reason)
}
pub fn before_tool(&self, run: &GateRun, _call: &CellCall) -> Option<ToolBlock> {
let root = (self.root_of)(run.conversation_id);
self.tool(&LaneRun::of(run), root.as_deref())
}
pub fn before_request(&self, run: &GateRun) -> RequestGate {
let root = (self.root_of)(run.conversation_id);
match self.request(&LaneRun::of(run), root.as_deref()) {
RequestDecision::Allow => RequestGate::Allow,
RequestDecision::Final(note) => RequestGate::Final { note },
RequestDecision::Block(reason) => RequestGate::Block { reason },
}
}
}