huskies: merge 646_bug_watchdog_from_bug_624_is_not_actually_enforcing_max_turns_max_budget_usd_in_production

This commit is contained in:
dave
2026-04-26 13:07:02 +00:00
parent 8673e563a9
commit 148c88bd40
5 changed files with 212 additions and 12 deletions
+22 -1
View File
@@ -3,7 +3,7 @@ pub mod gates;
pub mod lifecycle;
pub mod local_prompt;
pub mod merge;
mod pool;
pub(crate) mod pool;
pub(crate) mod pty;
pub mod runtime;
pub mod token_usage;
@@ -161,6 +161,27 @@ pub struct TokenUsage {
}
impl TokenUsage {
/// Estimate USD cost from token counts using approximate per-model pricing.
///
/// Used by the watchdog to compute in-flight budget from per-message usage
/// data in the agent log (since `total_cost_usd` is only available in the
/// `result` event at session end). Uses conservative (high) pricing when
/// the model is unknown so budget limits are hit sooner rather than later.
pub fn estimate_cost_usd(&self, model: Option<&str>) -> f64 {
// Per-million-token pricing (input, output, cache_read, cache_create).
let (inp, out, cr, cc) = match model {
Some(m) if m.contains("haiku") => (0.80, 4.0, 0.08, 1.00),
Some(m) if m.contains("sonnet") => (3.0, 15.0, 0.30, 3.75),
// Opus or unknown → most expensive = conservative.
_ => (15.0, 75.0, 1.50, 18.75),
};
(self.input_tokens as f64 * inp
+ self.output_tokens as f64 * out
+ self.cache_read_input_tokens as f64 * cr
+ self.cache_creation_input_tokens as f64 * cc)
/ 1_000_000.0
}
/// Parse token usage from a Claude Code `result` JSON event.
pub fn from_result_event(json: &serde_json::Value) -> Option<Self> {
let usage = json.get("usage")?;