Gate new rollout work on shared budget exhaustion (#61582)

## Why

A budget can be exhausted while another thread waits to start work. New requests, tools, and automatic turns must observe that stop without discarding an already-consumed response whose cost exhausted the budget.

## What changed

- Share a work gate across the rollout tree while preserving native and host accounting ownership.
- Recheck the budget before HTTP and WebSocket requests, hook execution, tool dispatch, and compaction. Prevent automatic wakeups and late input from restarting stopped work, leaving pending input queued.
- Record response costs without rejecting the consumed response solely because its bill exhausts the budget, and stop new work when enforced accounting overflows.
- Expose a thread-registration receipt that remains committed after unload.

## Testing

Extend rollout-budget coverage to verify that accepted response text remains in history after exhaustion and that waiting tools observe the shared stop while completed tools retain their results.

GitOrigin-RevId: e583d657c6f29e374d374cdb014bf23fd6c28614
This commit is contained in:
tongzhou wang
2026-11-15 00:03:23 +00:00
committed by copyberry
parent 2e61e8ec4e
commit ce82e38b16
26 changed files with 333 additions and 108 deletions
+6
View File
@@ -39,6 +39,12 @@ const _: Option<&dyn AgentControl> = None;
/// remote ownership, retries and recovery are separate backend work.
/// Boxed Send futures allow callers to use `Arc<dyn AgentControl>`.
pub trait AgentControl: Send + Sync {
/// Expose the account only when this controller actually owns native billing.
/// External controllers keep their own ledger and need not provide a numeric read.
fn native_rollout_budget(&self) -> Option<std::sync::Arc<crate::RolloutBudget>> {
None
}
fn identity(&self) -> SessionId;
/// Resolve an ID or a name relative to the caller's captured source, without loading.
+4
View File
@@ -33,6 +33,10 @@ use futures::future::BoxFuture;
use std::collections::HashSet;
impl AgentControl for LocalAgentControl {
fn native_rollout_budget(&self) -> Option<std::sync::Arc<crate::RolloutBudget>> {
Some(std::sync::Arc::clone(&self.runtime.rollout_budget))
}
fn identity(&self) -> SessionId {
self.session_id()
}
+2 -9
View File
@@ -4,7 +4,6 @@
use super::LocalAgentControl;
use crate::rollout_budget::RolloutBudgetReminder;
use codex_protocol::ThreadId;
use codex_protocol::error::CodexErr;
use codex_protocol::error::Result as CodexResult;
use codex_protocol::protocol::TokenUsage;
@@ -14,14 +13,8 @@ impl LocalAgentControl {
usage: Option<&TokenUsage>,
hook_charge: codex_hooks::HookCharge,
) -> CodexResult<()> {
if self
.runtime
.rollout_budget
.record_usage(usage, hook_charge)?
{
return Err(CodexErr::SessionBudgetExceeded);
}
Ok(())
// The caller keeps a consumed response even when its bill stops new work.
self.runtime.rollout_budget.record_usage(usage, hook_charge)
}
pub(crate) fn pending_budget_reminder(