Support fractional rollout budgets and configurable context reminders (#61579)

## What changed

- Add a positive, finite `features.rollout_budget.limit_budget` allowance while retaining `limit_tokens` compatibility. Reject configurations that specify both.
- Preserve fractional balances in reminders and compare thresholds without rounding the balance down.
- Accumulate charges with compensated summation and allow spending exactly the allowance, including floating-point rounding tolerance for cases such as `0.1 + 0.2` against `0.3`.
- Add `reminder_at_context_start`, defaulting to `true`. Set it to `false` to suppress reminders at thread start and after compaction; combine it with an empty threshold list to disable budget reminders entirely.

## Testing

Extend configuration and rollout-budget tests to cover fractional allowances, legacy integer persistence, invalid limits, threshold-only reminders, and enforcement at decimal equality and after overspending.

GitOrigin-RevId: b8e7a2faa8d39e562bcae196364de419698bc67d
This commit is contained in:
tongzhou wang
2026-11-14 23:53:23 +00:00
committed by copyberry
parent f226c9ccfb
commit c8e4cbcd94
17 changed files with 279 additions and 93 deletions
+53
View File
@@ -0,0 +1,53 @@
//! Numerical accounting for nonnegative rollout-budget units.
/// Accumulate validated, nonnegative `f64` charges without dropping every small
/// contribution after a large one. An unrepresentable total stays infinite.
#[derive(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;
}
if units.is_infinite() {
self.total = f64::INFINITY;
self.remainder = 0.0;
return;
}
// Near f64::MAX the first addition can overflow before a negative
// remainder brings it back into range. Halving both inputs avoids that.
let scale = if (self.total + units).is_finite() {
1.0
} else {
0.5
};
let previous = self.total * scale;
let units = units * scale;
let sum = previous + units;
// Recover the small part lost to rounding before adding the prior remainder.
let lost = if previous >= units {
(previous - sum) + units
} else {
(units - sum) + previous
};
let remainder = self.remainder * scale + lost;
// 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
};
}
pub fn total(&self) -> f64 {
self.total
}
}
+2
View File
@@ -1,6 +1,7 @@
pub mod account;
mod agent_path;
pub mod auth;
mod budget_units;
mod context_window_id;
mod item_id_generator;
mod request_country;
@@ -13,6 +14,7 @@ mod session_id;
mod thread_id;
mod tool_name;
pub use agent_path::AgentPath;
pub use budget_units::BudgetUnitsSum;
pub use context_window_id::ContextWindowId;
pub use item_id_generator::ItemIdGenerator;
#[cfg(test)]