mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
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:
@@ -2,23 +2,21 @@
|
||||
//! used for one response's hook contributions and for the rollout's posted bills.
|
||||
|
||||
/// Accumulate validated, nonnegative `f64` charges without dropping every small
|
||||
/// contribution after a large one. An unrepresentable total stays infinite.
|
||||
#[derive(Debug, Default)]
|
||||
/// contribution after a large one.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct BudgetUnitsSum {
|
||||
total: f64,
|
||||
remainder: f64,
|
||||
}
|
||||
|
||||
impl BudgetUnitsSum {
|
||||
pub fn add(&mut self, units: f64) {
|
||||
debug_assert!(units >= 0.0);
|
||||
if units == 0.0 || self.total.is_infinite() {
|
||||
return;
|
||||
/// Compute the next balance. Invalid inputs and arithmetic overflow leave this sum intact.
|
||||
pub fn checked_add(&self, units: f64) -> Option<Self> {
|
||||
if !units.is_finite() || units < 0.0 {
|
||||
return None;
|
||||
}
|
||||
if units.is_infinite() {
|
||||
self.total = f64::INFINITY;
|
||||
self.remainder = 0.0;
|
||||
return;
|
||||
if units == 0.0 {
|
||||
return Some(self.clone());
|
||||
}
|
||||
// Near f64::MAX the first addition can overflow before a negative
|
||||
// remainder brings it back into range. Halving both inputs avoids that.
|
||||
@@ -40,12 +38,9 @@ impl BudgetUnitsSum {
|
||||
// A nonnegative bill cannot lower posted spending. Keep any unused
|
||||
// correction for later receipts rather than reopening the budget.
|
||||
let next = (sum + remainder).max(previous);
|
||||
self.total = next / scale;
|
||||
self.remainder = if self.total.is_finite() {
|
||||
((sum - next) + remainder) / scale
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let total = next / scale;
|
||||
let remainder = ((sum - next) + remainder) / scale;
|
||||
(total.is_finite() && remainder.is_finite()).then_some(Self { total, remainder })
|
||||
}
|
||||
|
||||
pub fn total(&self) -> f64 {
|
||||
|
||||
Reference in New Issue
Block a user