The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
test(cost): cover cost tracking edge cases
Steven Enamakel0baaf12f92fd
diff --git a/crates/openhuman-core/src/agent/cost_tests.rs b/crates/openhuman-core/src/agent/cost_tests.rsindex 5d5695d21d..37bb064299 100644--- a/crates/openhuman-core/src/agent/cost_tests.rs+++ b/crates/openhuman-core/src/agent/cost_tests.rs@@ -1,94 +1,86 @@use super::*;-fn usage(input: u64, output: u64, cached: u64, charged: f64) -> BilledUsage {- BilledUsage::from_counts(input, output)- .with_cached_input_tokens(cached)- .with_charged_usd(charged)+fn counts(input: u64, output: u64, cached: u64) -> BilledUsage {+ BilledUsage::from_counts(input, output).with_cached_input_tokens(cached)}-const FLASH: &str = crate::config::MODEL_MANAGED_DEFAULT;+/// The thread that exposed the guess: glm-5.3-flash is not catalogued, and a+/// $3 / $0.30 / $15 per-MTok default priced its 72-call turn at $3.02 when the+/// provider billed about $0.22.+const UNCATALOGUED: &str = "openrouter/z-ai/glm-5.3-flash";+const CATALOGUED: &str = "claude-sonnet-4-6";#[test]-fn lookup_pricing_prices_the_managed_default() {- let p = lookup_pricing(FLASH);- assert_eq!(p.model, FLASH);- assert_eq!(p.input_per_mtok_usd, 0.0886);- assert_eq!(p.output_per_mtok_usd, 0.1772);+fn an_uncatalogued_model_has_no_price_and_no_estimate() {+ assert!(lookup_pricing(UNCATALOGUED).is_none());+ let usage = counts(312_065 + 5_398_592, 30_790, 5_398_592);+ assert_eq!(estimate_call_cost_usd(UNCATALOGUED, &usage), None);+ assert_eq!(call_cost(UNCATALOGUED, &usage), CallCost::Unknown);}#[test]-fn lookup_pricing_maps_role_aliases_and_retired_tiers_onto_the_default() {- // Old cost records and `hint:*` routes ran on the managed default; they- // must price from its row, not via the $3/$15 fallback, which would inflate- // cost and could trip budget gates.- for model in [- "hint:chat",- "hint:burst",- "burst-v1",- "vision-v1",- "reasoning-v1",- ] {- let p = lookup_pricing(model);- assert_eq!(p.model, FLASH, "{model} must price as the managed default");- }+fn a_catalogued_model_is_estimated_at_its_list_rates() {+ let pricing = lookup_pricing(CATALOGUED).expect("catalogued");+ assert_eq!(pricing.model, "claude-sonnet-4-6");+ // 1M uncached input at $3 + 1M cached at $0.30 + 1M output at $15.+ let usage = counts(2_000_000, 1_000_000, 1_000_000);+ let estimate = estimate_call_cost_usd(CATALOGUED, &usage).expect("priced");+ assert!((estimate - 18.30).abs() < 1e-9, "estimate {estimate}");+ assert_eq!(call_cost(CATALOGUED, &usage), CallCost::Estimated(estimate));}#[test]-fn lookup_pricing_falls_back_for_unknown_model() {- let p = lookup_pricing("totally-unknown-model");- assert_eq!(p.model, "<fallback>");+fn a_reported_charge_wins_even_when_it_is_zero() {+ let charged = counts(1_000, 100, 0).with_charged_usd(0.0123);+ assert_eq!(call_cost(UNCATALOGUED, &charged), CallCost::Charged(0.0123));+ // A free route reports $0: that is a known cost, not a missing one.+ let free = counts(1_000, 100, 0).with_charged_usd(0.0);+ assert_eq!(call_cost(UNCATALOGUED, &free), CallCost::Charged(0.0));+ // `with_reported_charge(None)` leaves the cost unknown.+ let none = counts(1_000, 100, 0).with_reported_charge(None);+ assert_eq!(call_cost(UNCATALOGUED, &none), CallCost::Unknown);}#[test]-fn lookup_pricing_handles_concrete_vendor_names() {- // A catalog id prices from the vendor catalog; a dotted, non-catalog- // vendor name no longer guesses a tier and takes the conservative fallback.- assert_eq!(lookup_pricing("claude-opus-4.7").model, "<fallback>");+fn an_estimate_carried_on_the_usage_is_not_taken_for_a_charge() {+ let estimated = counts(1_000_000, 0, 0).with_estimated_usd(9.99);assert_eq!(- lookup_pricing("claude-sonnet-4-6").output_per_mtok_usd,- 15.0+ call_cost(CATALOGUED, &estimated),+ CallCost::Estimated(3.0),+ "a carried estimate is re-priced from the catalog, not trusted as billed");}#[test]-fn estimate_call_cost_subtracts_cached_input() {- // 1M standard input + 1M cached input + 1M output on the managed default.- let u = usage(2_000_000, 1_000_000, 1_000_000, 0.0);- let est = estimate_call_cost_usd(FLASH, &u);- // 1M*0.0886 + 1M*0.0886 + 1M*0.1772 = 0.3544- assert!((est - 0.3544).abs() < 1e-6, "got {est}");-}+fn a_turn_with_any_unknown_call_has_no_total() {+ let mut turn = TurnCost::new();+ turn.add_call(UNCATALOGUED, &counts(10, 1, 0).with_charged_usd(0.5));+ assert_eq!(turn.total_usd(), Some(0.5));+ assert_eq!(turn.cost.source, CostSource::Charged);-#[test]-fn call_cost_prefers_charged_when_present() {- let u = usage(100_000, 200_000, 0, 0.42);- assert_eq!(call_cost_usd(FLASH, &u), 0.42);-}+ turn.add_call(CATALOGUED, &counts(1_000_000, 0, 0));+ assert_eq!(turn.cost.source, CostSource::Estimated);+ assert!((turn.total_usd().unwrap() - 3.5).abs() < 1e-9);-#[test]-fn call_cost_falls_back_to_estimate_when_charged_zero() {- let u = usage(1_000_000, 0, 0, 0.0);- // 1M input * 0.0886 = 0.0886- assert!((call_cost_usd(FLASH, &u) - 0.0886).abs() < 1e-6);+ turn.add_call(UNCATALOGUED, &counts(10, 1, 0));+ assert_eq!(turn.cost.source, CostSource::Unknown);+ assert_eq!(turn.total_usd(), None, "an unknown call voids the total");+ assert_eq!(turn.call_count, 3);+ assert_eq!(turn.input_tokens, 1_000_020);}#[test]-fn turn_cost_accumulates_charged_and_estimated_separately() {- let mut tc = TurnCost::new();- tc.add_call(FLASH, &usage(0, 0, 0, 0.10));- tc.add_call(FLASH, &usage(1_000_000, 0, 0, 0.0)); // est: 0.0886- assert_eq!(tc.call_count, 2);- assert!((tc.charged_usd - 0.10).abs() < 1e-6);- assert!((tc.estimated_usd - 0.0886).abs() < 1e-6);- assert!((tc.total_usd() - 0.1886).abs() < 1e-6);-}+fn merged_tallies_keep_the_least_certain_source() {+ let mut parent = CostTally::default();+ parent.add(CallCost::Charged(1.0));+ let mut child = CostTally::default();+ child.add(CallCost::Estimated(0.25));+ parent.merge(child);+ assert_eq!(parent.source, CostSource::Estimated);+ assert_eq!(parent.usd(), Some(1.25));-#[test]-fn turn_cost_aggregates_token_counts() {- let mut tc = TurnCost::new();- tc.add_call(FLASH, &usage(100, 50, 20, 0.0));- tc.add_call(FLASH, &usage(200, 75, 0, 0.0));- assert_eq!(tc.input_tokens, 300);- assert_eq!(tc.output_tokens, 125);- assert_eq!(tc.cached_input_tokens, 20);+ let mut unknown = CostTally::default();+ unknown.add(CallCost::Unknown);+ parent.merge(unknown);+ assert_eq!(parent.usd(), None);}