Account for synchronous MCP post-hook costs in rollout budgets (#61580)

## What changed

Allow successful synchronous `PostModelRequest` MCP hooks to return `extra_codex_rollout_budget_units` in `structuredContent`. Add these charges to rollout spending while preserving provider usage separately, including completed responses without token usage and incomplete responses with reported usage.

Validate prices as finite and nonnegative. Preserve valid sibling charges when another hook reports an invalid price, and surface accounting failures without retrying inference. Carry hook charges and overflow information through host accounting and enforce local budget limits.

## Testing

Extend coverage for hook-driven budget exhaustion, failed hooks, overflow, interrupted responses, and foreground and background compaction accounting.

GitOrigin-RevId: dc54ba53478d82848bcafb55ecef88215019d09f
This commit is contained in:
tongzhou wang
2026-11-14 23:53:53 +00:00
committed by copyberry
parent c8e4cbcd94
commit c8c3f70893
42 changed files with 595 additions and 156 deletions
+20 -8
View File
@@ -13,6 +13,7 @@ use crate::session::session::Session;
use crate::session::step_context::StepContext;
use codex_hooks::ModelRequestHookEvent;
use codex_hooks::ModelRequestHookInput;
use codex_hooks::ModelRequestHookOutcome;
use codex_hooks::SamplingKind;
use codex_protocol::error::Result;
use codex_protocol::protocol::HookCompletedEvent;
@@ -118,12 +119,20 @@ struct PostHooks {
}
impl PostHooks {
async fn run(mut self, session: &Arc<Session>, event: ModelRequestHookEvent) {
async fn run(
mut self,
session: &Arc<Session>,
event: ModelRequestHookEvent,
) -> ModelRequestHookOutcome {
self.input.event = event;
run_model_request_hooks(session, &self.step, &self.hooks, self.input).await;
run_model_request_hooks(session, &self.step, &self.hooks, self.input).await
}
async fn run_sync(mut self, session: Arc<Session>, event: ModelRequestHookEvent) {
async fn run_sync(
mut self,
session: Arc<Session>,
event: ModelRequestHookEvent,
) -> ModelRequestHookOutcome {
let (mut done, wait) = oneshot::channel();
self.input.event = event;
// Dropping the waiter cancels execution while the task finishes publishing
@@ -137,7 +146,7 @@ impl PostHooks {
let started = Instant::now();
emit_hook_started_events(&session, turn, runs.clone()).await;
let mut finished: Vec<HookCompletedEvent> = Vec::new();
let events = tokio::select! {
let (events, outcome) = tokio::select! {
biased;
() = done.closed() => {
let completed_at = chrono::Utc::now().timestamp();
@@ -157,14 +166,17 @@ impl PostHooks {
run,
});
}
finished
(finished, ModelRequestHookOutcome::default())
}
events = self.hooks.run_model_request(self.input, |event| finished.push(event.clone())) => events,
};
emit_hook_completed_events(&session, turn, events).await;
let _ = done.send(());
let _ = done.send(outcome);
});
let _ = wait.await;
wait.await.unwrap_or_else(|_| ModelRequestHookOutcome {
invalid_price: Some("synchronous hook task exited without reporting its charge".into()),
..Default::default()
})
}
fn schedule(self, session: Arc<Session>, event: ModelRequestHookEvent) {
@@ -198,7 +210,7 @@ impl SamplingAttempt {
pub(crate) fn start_post_hooks(
&mut self,
event: ModelRequestHookEvent,
) -> Option<impl Future<Output = ()> + Send + 'static> {
) -> Option<impl Future<Output = ModelRequestHookOutcome> + Send + 'static> {
let post = self.post.take()?;
let session = Arc::clone(&self.session);
if post.mode == HookExecutionMode::Sync {