mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +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:
@@ -124,7 +124,11 @@ 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: TokenUsage) -> BoxFuture<'_, Result<()>>;
|
||||
fn record_usage(
|
||||
&self,
|
||||
usage: Option<TokenUsage>,
|
||||
hook_charge: codex_hooks::HookCharge,
|
||||
) -> 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
|
||||
|
||||
@@ -282,8 +282,12 @@ impl AgentControl for LocalAgentControl {
|
||||
self.execution_guard(version, source)
|
||||
}
|
||||
|
||||
fn record_usage(&self, usage: TokenUsage) -> BoxFuture<'_, Result<()>> {
|
||||
Box::pin(async move { self.record_rollout_budget_usage(&usage) })
|
||||
fn record_usage(
|
||||
&self,
|
||||
usage: Option<TokenUsage>,
|
||||
hook_charge: codex_hooks::HookCharge,
|
||||
) -> BoxFuture<'_, Result<()>> {
|
||||
Box::pin(async move { self.record_rollout_budget_usage(usage.as_ref(), hook_charge) })
|
||||
}
|
||||
|
||||
fn turn_finished<'a>(
|
||||
|
||||
@@ -9,8 +9,16 @@ use codex_protocol::error::Result as CodexResult;
|
||||
use codex_protocol::protocol::TokenUsage;
|
||||
|
||||
impl LocalAgentControl {
|
||||
pub(crate) fn record_rollout_budget_usage(&self, usage: &TokenUsage) -> CodexResult<()> {
|
||||
if self.runtime.rollout_budget.record_usage(usage)? {
|
||||
pub(crate) fn record_rollout_budget_usage(
|
||||
&self,
|
||||
usage: Option<&TokenUsage>,
|
||||
hook_charge: codex_hooks::HookCharge,
|
||||
) -> CodexResult<()> {
|
||||
if self
|
||||
.runtime
|
||||
.rollout_budget
|
||||
.record_usage(usage, hook_charge)?
|
||||
{
|
||||
return Err(CodexErr::SessionBudgetExceeded);
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -5,14 +5,18 @@ use codex_protocol::error::Result;
|
||||
use codex_protocol::protocol::TokenUsage;
|
||||
use futures::future::BoxFuture;
|
||||
|
||||
/// Provider-reported spending for one response, including compaction.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
/// Spending reported for one model response, including compaction.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct AgentUsageReport {
|
||||
pub thread_id: ThreadId,
|
||||
pub turn_id: String,
|
||||
/// Per-response usage, not cumulative totals. Remote adapters must explicitly
|
||||
/// carry `codex_rollout_budget_units`, which `TokenUsage` skips when serialized.
|
||||
pub usage: TokenUsage,
|
||||
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,
|
||||
}
|
||||
|
||||
/// Both decisions mean the host recorded the reported spending.
|
||||
|
||||
Reference in New Issue
Block a user