mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
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:
@@ -3,11 +3,13 @@ use super::turn_context::TurnContext;
|
||||
use crate::agent::host::AgentRuntimeServices;
|
||||
use crate::agent::host::AgentUsageReport;
|
||||
use crate::agent::host::UsageAccountingDecision;
|
||||
use crate::client_common::SamplingUsage;
|
||||
use crate::context::ContextualUserFragment;
|
||||
use crate::rollout_budget::RolloutBudget;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::CodexErrorDetails;
|
||||
use codex_protocol::error::Result as CodexResult;
|
||||
use codex_protocol::protocol::TokenUsage;
|
||||
|
||||
pub(super) async fn maybe_record_reminder(
|
||||
sess: &Session,
|
||||
@@ -55,28 +57,45 @@ impl Session {
|
||||
pub(crate) async fn record_rollout_budget_usage(
|
||||
&self,
|
||||
turn_id: &str,
|
||||
bill: &crate::client_common::SamplingUsage,
|
||||
usage: &SamplingUsage,
|
||||
) -> CodexResult<()> {
|
||||
if bill.charge.posted {
|
||||
if !usage.charge.posted {
|
||||
self.record_numeric_usage(turn_id, usage.provider.as_ref(), usage.charge.hooks.charge)
|
||||
.await?;
|
||||
}
|
||||
if let Some(message) = &usage.charge.hooks.invalid_price {
|
||||
// A bad price does not erase successful siblings' contributions.
|
||||
return Err(CodexErr::UsageAccountingFailed(format!(
|
||||
"Invalid post-hook budget units: {message}"
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn record_numeric_usage(
|
||||
&self,
|
||||
turn_id: &str,
|
||||
usage: Option<&TokenUsage>,
|
||||
hook_charge: codex_hooks::HookCharge,
|
||||
) -> CodexResult<()> {
|
||||
if usage.is_none() && hook_charge == codex_hooks::HookCharge::default() {
|
||||
return Ok(());
|
||||
}
|
||||
let Some(usage) = bill.provider.as_ref() else {
|
||||
return Ok(());
|
||||
};
|
||||
let stop_new_work = match &self.agent_runtime {
|
||||
AgentRuntimeServices::Controller => match self
|
||||
.services
|
||||
.agent_control
|
||||
.record_usage(usage.clone())
|
||||
.record_usage(usage.cloned(), hook_charge)
|
||||
.await
|
||||
{
|
||||
Ok(()) => false,
|
||||
// A legacy controller can report a posted bill and a stop through this error.
|
||||
Err(error)
|
||||
if matches!(error.details(), CodexErrorDetails::SessionBudgetExceeded) =>
|
||||
{
|
||||
true
|
||||
}
|
||||
Err(error) => return Err(error),
|
||||
Err(error) => return Err(CodexErr::UsageAccountingFailed(error.to_string())),
|
||||
},
|
||||
AgentRuntimeServices::Host(services) => {
|
||||
let Some(accounting) = &services.usage_accounting else {
|
||||
@@ -86,11 +105,15 @@ impl Session {
|
||||
.report_usage(AgentUsageReport {
|
||||
thread_id: self.thread_id(),
|
||||
turn_id: turn_id.to_owned(),
|
||||
usage: usage.clone(),
|
||||
usage: usage.cloned(),
|
||||
additional_budget_units: hook_charge.units,
|
||||
additional_budget_units_overflowed: hook_charge.overflowed,
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
CodexErr::Fatal(format!("Host usage accounting failed: {error}"))
|
||||
CodexErr::UsageAccountingFailed(format!(
|
||||
"Host usage accounting failed: {error}"
|
||||
))
|
||||
})?;
|
||||
decision == UsageAccountingDecision::Exhausted
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user