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
+16 -1
View File
@@ -207,6 +207,16 @@ pub enum ThreadRegistration {
Replaced,
}
/// A manager-started runtime's receipt stays committed across unload. A runtime
/// started without a manager has no receipt in its thread extension store.
pub struct ThreadRegistrationReceipt(Arc<AtomicBool>);
impl ThreadRegistrationReceipt {
pub fn was_registered(&self) -> bool {
self.0.load(Ordering::Acquire)
}
}
// TODO(ccunningham): Add an explicit non-interrupting live-turn snapshot once
// core can represent sampling boundaries directly instead of relying on
// whichever items happened to be persisted mid-turn.
@@ -2903,6 +2913,8 @@ impl ThreadManagerState {
&session_source,
) => manager,
};
let registered = Arc::new(AtomicBool::new(false));
thread_extension_init.insert(ThreadRegistrationReceipt(Arc::clone(&registered)));
let spawn_result = Session::spawn(SessionSpawnArgs {
attached_thread,
startup,
@@ -2997,7 +3009,7 @@ impl ThreadManagerState {
}
}
let finalize_result = self
.finalize_thread_spawn(session, io, tracked_session_source)
.finalize_thread_spawn(session, io, tracked_session_source, &registered)
.await;
let (new_thread, tier_acceptance) = match finalize_result {
Ok(registered) => registered,
@@ -3040,6 +3052,7 @@ impl ThreadManagerState {
session: Arc<Session>,
io: SessionIo,
session_source: SessionSource,
registered: &AtomicBool,
) -> CodexResult<(NewThread, Option<ServiceTierAcceptance>)> {
let thread_id = session.thread_id();
let event = io.next_event().await?;
@@ -3080,6 +3093,8 @@ impl ThreadManagerState {
session_configured.rollout_path.clone(),
session_source,
));
// All registration checks passed; publish the receipt with this insertion.
registered.store(true, Ordering::Release);
e.insert(thread.clone());
let tier_acceptance = pending_service_tier.and_then(|(mut state, update)| {
state.service_tier_acceptance =