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
+2 -5
View File
@@ -130,11 +130,8 @@ pub trait AgentControl: Send + Sync {
/// Account for one inference response, including compaction. Each call records usage;
/// callers report it once. `SessionBudgetExceeded` means the usage was recorded and
/// the shared budget is now exhausted.
fn record_usage(
&self,
usage: Option<TokenUsage>,
hook_charge: codex_hooks::HookCharge,
) -> BoxFuture<'_, Result<()>>;
fn record_usage(&self, usage: Option<TokenUsage>, hook_units: f64)
-> BoxFuture<'_, Result<()>>;
/// Report the terminal result to the parent and completion activity to the task
/// initiator. Local delivery remains best effort and uses the reporting runtime's
+2 -2
View File
@@ -290,9 +290,9 @@ impl AgentControl for LocalAgentControl {
fn record_usage(
&self,
usage: Option<TokenUsage>,
hook_charge: codex_hooks::HookCharge,
hook_units: f64,
) -> BoxFuture<'_, Result<()>> {
Box::pin(async move { self.record_rollout_budget_usage(usage.as_ref(), hook_charge) })
Box::pin(async move { self.record_rollout_budget_usage(usage.as_ref(), hook_units) })
}
fn turn_finished<'a>(
+2 -2
View File
@@ -11,10 +11,10 @@ impl LocalAgentControl {
pub(crate) fn record_rollout_budget_usage(
&self,
usage: Option<&TokenUsage>,
hook_charge: codex_hooks::HookCharge,
hook_units: f64,
) -> CodexResult<()> {
// The caller keeps a consumed response even when its bill stops new work.
self.runtime.rollout_budget.record_usage(usage, hook_charge)
self.runtime.rollout_budget.record_usage(usage, hook_units)
}
pub(crate) fn pending_budget_reminder(
-2
View File
@@ -15,8 +15,6 @@ pub struct AgentUsageReport {
pub usage: Option<TokenUsage>,
/// Synchronous post-hook contributions, independent of the provider's usage.
pub additional_budget_units: f64,
/// If f64 summation overflowed, this is the largest individual hook price, not the total.
pub additional_budget_units_overflowed: bool,
}
/// Each decision means the host recorded the reported spending.