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
+2 -1
View File
@@ -1,4 +1,5 @@
//! Numerical accounting for nonnegative rollout-budget units.
//! Numerical accounting for nonnegative rollout-budget units. The same sum is
//! used for one response's hook contributions and for the rollout's posted bills.
/// Accumulate validated, nonnegative `f64` charges without dropping every small
/// contribution after a large one. An unrepresentable total stays infinite.
+5
View File
@@ -248,6 +248,9 @@ pub enum CodexErrorDetails {
GatewayLoginRequired(String),
#[error("Fatal error: {0}")]
Fatal(String),
/// The provider attempt ended. Retrying inference cannot settle its bill.
#[error("{0}")]
UsageAccountingFailed(String),
// -----------------------------------------------------------------
// Automatic conversions for common external error types
// -----------------------------------------------------------------
@@ -420,6 +423,7 @@ impl CodexErr {
UnsupportedOperation(message: String),
RefreshTokenFailed(error: RefreshTokenFailedError),
Fatal(message: String),
UsageAccountingFailed(message: String),
Io(error: io::Error),
Json(error: serde_json::Error),
#[cfg(target_os = "linux")]
@@ -484,6 +488,7 @@ impl CodexErr {
| CodexErrorDetails::Interrupted
| CodexErrorDetails::EnvVar(_)
| CodexErrorDetails::Fatal(_)
| CodexErrorDetails::UsageAccountingFailed(_)
| CodexErrorDetails::UsageNotIncluded
| CodexErrorDetails::QuotaExceeded
| CodexErrorDetails::InvalidImageRequest()