pub struct BudgetTracker { /* private fields */ }Expand description
Per-job cost.budget counters (ARCP v1.1 §9.6).
Tracks remaining authority for each declared currency. Reporting cost
is the agent’s responsibility; the runtime decrements counters on each
cost.* metric emitted via ToolContext::charge or
BudgetTracker::charge. Once a counter falls below zero further
charges return ARCPError::BudgetExhausted.
Implementations§
Source§impl BudgetTracker
impl BudgetTracker
Sourcepub fn from_budget(budget: &CostBudget) -> Self
pub fn from_budget(budget: &CostBudget) -> Self
Construct a tracker pre-seeded from a CostBudget lease
capability.
Sourcepub fn is_disabled(&self) -> bool
pub fn is_disabled(&self) -> bool
True when no currencies are tracked (i.e. budgeting is disabled).
Sourcepub fn remaining(&self, currency: &str) -> Option<f64>
pub fn remaining(&self, currency: &str) -> Option<f64>
Remaining budget for currency, if tracked. None means the
currency was not in the declared lease (i.e. unbudgeted, treated
as unbounded for that currency).
Sourcepub fn snapshot_remaining(&self) -> HashMap<String, f64>
pub fn snapshot_remaining(&self) -> HashMap<String, f64>
Snapshot of remaining-per-currency for all tracked currencies.
Sourcepub fn charge(&self, currency: &str, amount: f64) -> Result<f64, ARCPError>
pub fn charge(&self, currency: &str, amount: f64) -> Result<f64, ARCPError>
Charge amount to currency. Returns Ok(remaining) after
the decrement, or ARCPError::BudgetExhausted when the
charge would push the counter below zero.
Negative amounts are rejected per §9.6.
Currencies not present in the declared lease are silently
ignored (returns Ok(f64::INFINITY)), matching the §9.6 rule
that “unreported / unbudgeted costs are not enforced”.
§Examples
use arcp_core::messages::{CostBudget, CostBudgetAmount};
use arcp_runtime::runtime::context::BudgetTracker;
let tracker = BudgetTracker::from_budget(&CostBudget {
amounts: vec![CostBudgetAmount { currency: "USD".into(), amount: 1.00 }],
});
assert!(tracker.charge("USD", 0.30).is_ok());
assert!(tracker.charge("USD", 5.00).is_err()); // overspend rejected
assert!((tracker.remaining("USD").unwrap() - 0.70).abs() < 1e-9);§Errors
ARCPError::InvalidArgumentfor a negative or non-finite amount.ARCPError::BudgetExhaustedwhen the charge would overspend the remaining budget. The charge is rejected and the counter is left unchanged so the agent sees the canonical signal on the first operation that would have overspent.
Trait Implementations§
Source§impl Clone for BudgetTracker
impl Clone for BudgetTracker
Source§fn clone(&self) -> BudgetTracker
fn clone(&self) -> BudgetTracker
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more