Reject overflowing rollout budget charges before posting (#62559)

## Why

An overflowing hook total could send a partial charge to the host, and
an overflow in native accounting could leave the posted balance infinite.

## What changed

Reject overflowing hook totals before reporting usage. Check both the
combined provider and hook charge and the cumulative rollout balance
before posting, preserving the previous balance on overflow. Native
budgets with limit enforcement enabled still stop new work on overflow.

## Testing

Update the hook overflow integration case to verify that the response's
tool is blocked and the host receives no usage report.

GitOrigin-RevId: 2ac80a6aa1d857924a8807df7bb70c6b7c107cd2
This commit is contained in:
tongzhou wang
2026-11-19 17:46:53 +00:00
committed by copyberry
parent d9f6fcff94
commit 65d69f25b4
13 changed files with 88 additions and 104 deletions
+17 -19
View File
@@ -164,12 +164,17 @@ pub(crate) fn preview(
.collect()
}
/// One response's successful hook prices. On overflow, units is a finite
/// subtotal of real contributions, not the price of the entire response.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct HookCharge {
pub units: f64,
pub overflowed: bool,
/// Sum of successful hook prices. An overflowing sum is rejected before posting.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum HookCharge {
Units(f64),
Overflow,
}
impl Default for HookCharge {
fn default() -> Self {
Self::Units(0.0)
}
}
/// A failed observer does not erase prices reported by successful siblings.
@@ -245,30 +250,23 @@ pub(crate) async fn run(
)
.await;
let mut outcome = ModelRequestHookOutcome::default();
let mut reported_units = BudgetUnitsSum::default();
let mut largest_price = 0.0_f64;
let mut reported_units = Some(BudgetUnitsSum::default());
let mut events = Vec::with_capacity(results.len());
for result in results {
if billable && let Some(price) = result.data {
match price {
Ok(units) => {
largest_price = largest_price.max(units);
reported_units.add(units);
reported_units = reported_units.and_then(|sum| sum.checked_add(units));
}
Err(error) => outcome.invalid_price = Some(error),
}
}
events.push(result.completed);
}
outcome.charge.units = reported_units.total();
if !outcome.charge.units.is_finite() {
// An individual price is a real lower bound, even if earlier f64 additions rounded up.
outcome.charge = HookCharge {
units: largest_price,
overflowed: true,
};
outcome.invalid_price = Some("total hook cost exceeds f64 range".into());
}
outcome.charge = match reported_units {
Some(sum) => HookCharge::Units(sum.total()),
None => HookCharge::Overflow,
};
(events, outcome)
}