mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +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:
@@ -28,6 +28,7 @@ regex = { workspace = true }
|
||||
schemars = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
serde_path_to_error = { workspace = true }
|
||||
sha2 = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
tokio = { workspace = true, features = ["fs", "io-util", "process", "sync", "time"] }
|
||||
|
||||
@@ -268,6 +268,7 @@ pub(crate) async fn run_command(
|
||||
Err(error) => (None, String::new(), String::new(), Some(error.to_string())),
|
||||
};
|
||||
return HandlerRunResult {
|
||||
structured_content: None,
|
||||
context_metadata: codex_protocol::models::ContentItemMetadata::command_hook(),
|
||||
started_at,
|
||||
completed_at: chrono::Utc::now().timestamp(),
|
||||
@@ -432,6 +433,7 @@ fn finish_command_run(
|
||||
) -> HandlerRunResult {
|
||||
Span::current().record("hook.command_outcome", completion.outcome);
|
||||
HandlerRunResult {
|
||||
structured_content: None,
|
||||
context_metadata: codex_protocol::models::ContentItemMetadata::command_hook(),
|
||||
started_at,
|
||||
completed_at: chrono::Utc::now().timestamp(),
|
||||
|
||||
@@ -92,11 +92,18 @@ pub(crate) async fn run_mcp_tool(
|
||||
}
|
||||
.await;
|
||||
|
||||
let (exit_code, stdout, error, source_tool_namespace) = match result {
|
||||
Ok(output) => (Some(0), output.text, None, output.source_tool_namespace),
|
||||
Err(error) => (None, String::new(), Some(error.to_string()), None),
|
||||
let (exit_code, stdout, error, source_tool_namespace, structured_content) = match result {
|
||||
Ok(output) => (
|
||||
Some(0),
|
||||
output.text,
|
||||
None,
|
||||
output.source_tool_namespace,
|
||||
output.structured_content,
|
||||
),
|
||||
Err(error) => (None, String::new(), Some(error.to_string()), None, None),
|
||||
};
|
||||
HandlerRunResult {
|
||||
structured_content,
|
||||
context_metadata: ContentItemMetadata::tool(source_tool_namespace.map(Into::into)),
|
||||
started_at,
|
||||
completed_at: chrono::Utc::now().timestamp(),
|
||||
|
||||
@@ -31,6 +31,7 @@ impl HookMcpExecutor for RecordingExecutor {
|
||||
async move {
|
||||
self.calls.lock().expect("lock calls").push(call);
|
||||
Ok(HookMcpOutput {
|
||||
structured_content: None,
|
||||
text: self.output.clone(),
|
||||
source_tool_namespace: Some("security".into()),
|
||||
})
|
||||
|
||||
@@ -161,6 +161,7 @@ pub(crate) enum ConfiguredHandlerKind {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct HandlerRunResult {
|
||||
pub structured_content: Option<serde_json::Value>,
|
||||
pub context_metadata: codex_protocol::models::ContentItemMetadata,
|
||||
pub started_at: i64,
|
||||
pub completed_at: i64,
|
||||
|
||||
@@ -2305,6 +2305,7 @@ impl HookMcpExecutor for StaticMcpExecutor {
|
||||
self.calls.lock().expect("lock MCP calls").push(call);
|
||||
Ok(HookMcpOutput {
|
||||
text: output,
|
||||
structured_content: None,
|
||||
source_tool_namespace: None,
|
||||
})
|
||||
}
|
||||
@@ -2800,6 +2801,7 @@ async fn executor_stop_hooks_do_not_delay_stop_completion() {
|
||||
self.release.notified().await;
|
||||
Ok(HookMcpOutput {
|
||||
text: String::new(),
|
||||
structured_content: None,
|
||||
source_tool_namespace: None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -540,6 +540,7 @@ mod tests {
|
||||
|
||||
fn run_result(exit_code: Option<i32>, stdout: &str, stderr: &str) -> HandlerRunResult {
|
||||
HandlerRunResult {
|
||||
structured_content: None,
|
||||
context_metadata: codex_protocol::models::ContentItemMetadata::harness(),
|
||||
started_at: 1_700_000_000,
|
||||
completed_at: 1_700_000_001,
|
||||
|
||||
@@ -118,6 +118,7 @@ fn handler() -> ConfiguredHandler {
|
||||
|
||||
fn run_result(exit_code: Option<i32>, stdout: &str, stderr: &str) -> HandlerRunResult {
|
||||
HandlerRunResult {
|
||||
structured_content: None,
|
||||
context_metadata: codex_protocol::models::ContentItemMetadata::harness(),
|
||||
started_at: 1,
|
||||
completed_at: 2,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
//! Observation-only lifecycle hooks. Output is logged, never interpreted as control flow.
|
||||
//! Sampling hooks observe attempts. Successful synchronous MCP post-hooks can
|
||||
//! add a price to a completed response or to provider-reported incomplete usage.
|
||||
use codex_protocol::BudgetUnitsSum;
|
||||
use codex_protocol::protocol::HookCompletedEvent;
|
||||
use codex_protocol::protocol::HookEventName;
|
||||
use codex_protocol::protocol::HookOutputEntry;
|
||||
@@ -8,6 +10,7 @@ use codex_protocol::protocol::HookRunSummary;
|
||||
use codex_protocol::protocol::TokenUsage;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
use super::common;
|
||||
@@ -157,19 +160,55 @@ pub(crate) fn preview(
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// One response's successful hook prices. On overflow, units is a finite
|
||||
/// subtotal of real contributions, not the price of the entire response.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct HookCharge {
|
||||
pub units: f64,
|
||||
pub overflowed: bool,
|
||||
}
|
||||
|
||||
/// A failed observer does not erase prices reported by successful siblings.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ModelRequestHookOutcome {
|
||||
pub charge: HookCharge,
|
||||
pub invalid_price: Option<String>,
|
||||
}
|
||||
|
||||
/// Structured MCP output from a synchronous post-hook. Omitted units add zero
|
||||
/// to the provider's charge.
|
||||
#[derive(Deserialize)]
|
||||
struct PostHookOutput {
|
||||
#[serde(default)]
|
||||
extra_codex_rollout_budget_units: f64,
|
||||
}
|
||||
|
||||
type ReportedPrice = Result<f64, String>;
|
||||
|
||||
pub(crate) async fn run(
|
||||
engine: &ClaudeHooksEngine,
|
||||
request: ModelRequestHookInput,
|
||||
on_completed: impl FnMut(&HookCompletedEvent) + Send,
|
||||
) -> Vec<HookCompletedEvent> {
|
||||
) -> (Vec<HookCompletedEvent>, ModelRequestHookOutcome) {
|
||||
let matched = dispatcher::select_handlers(
|
||||
&engine.handlers,
|
||||
request.event.hook_event(),
|
||||
/*matcher_input*/ None,
|
||||
);
|
||||
if matched.is_empty() {
|
||||
return Vec::new();
|
||||
return Default::default();
|
||||
}
|
||||
let billable = matches!(
|
||||
request.event,
|
||||
ModelRequestHookEvent::PostModelRequest {
|
||||
status: ModelRequestStatus::Completed,
|
||||
..
|
||||
} | ModelRequestHookEvent::PostModelRequest {
|
||||
status: ModelRequestStatus::Failed,
|
||||
token_usage: Some(_),
|
||||
..
|
||||
}
|
||||
);
|
||||
let input = match serde_json::to_value(&request) {
|
||||
Ok(input) => input,
|
||||
Err(error) => {
|
||||
@@ -181,10 +220,12 @@ pub(crate) async fn run(
|
||||
for event in &mut events {
|
||||
identify_run(&mut event.run, request.lifecycle_id);
|
||||
}
|
||||
return events;
|
||||
return (events, Default::default());
|
||||
}
|
||||
};
|
||||
dispatcher::execute_handlers_with_progress(
|
||||
// The dispatcher returns synchronous results in configuration order. Prices
|
||||
// become visible together, only after the entire group finishes.
|
||||
let results = dispatcher::execute_handlers_with_progress(
|
||||
engine,
|
||||
matched,
|
||||
&input,
|
||||
@@ -198,17 +239,40 @@ pub(crate) async fn run(
|
||||
},
|
||||
on_completed,
|
||||
)
|
||||
.await
|
||||
.into_iter()
|
||||
.map(|result| result.completed)
|
||||
.collect()
|
||||
.await;
|
||||
let mut outcome = ModelRequestHookOutcome::default();
|
||||
let mut reported_units = BudgetUnitsSum::default();
|
||||
let mut largest_price = 0.0_f64;
|
||||
let mut events = Vec::with_capacity(results.len());
|
||||
for result in results {
|
||||
if billable && let Some(price) = result.data {
|
||||
match price {
|
||||
Ok(units) => {
|
||||
largest_price = largest_price.max(units);
|
||||
reported_units.add(units);
|
||||
}
|
||||
Err(error) => outcome.invalid_price = Some(error),
|
||||
}
|
||||
}
|
||||
events.push(result.completed);
|
||||
}
|
||||
outcome.charge.units = reported_units.total();
|
||||
if !outcome.charge.units.is_finite() {
|
||||
// An individual price is a real lower bound, even if earlier f64 additions rounded up.
|
||||
outcome.charge = HookCharge {
|
||||
units: largest_price,
|
||||
overflowed: true,
|
||||
};
|
||||
outcome.invalid_price = Some("total hook cost exceeds f64 range".into());
|
||||
}
|
||||
(events, outcome)
|
||||
}
|
||||
|
||||
fn parse_completed(
|
||||
handler: &ConfiguredHandler,
|
||||
result: HandlerRunResult,
|
||||
turn_id: Option<String>,
|
||||
) -> dispatcher::ParsedHandler<()> {
|
||||
) -> dispatcher::ParsedHandler<Option<ReportedPrice>> {
|
||||
let status = if result.error.is_none() && result.exit_code == Some(0) {
|
||||
HookRunStatus::Completed
|
||||
} else {
|
||||
@@ -231,12 +295,39 @@ fn parse_completed(
|
||||
text: error.clone(),
|
||||
});
|
||||
}
|
||||
let price = if status == HookRunStatus::Completed {
|
||||
result
|
||||
.structured_content
|
||||
.as_ref()
|
||||
.filter(|body| !body.is_null())
|
||||
.map(|body| {
|
||||
if !body.is_object() {
|
||||
return Err("post-hook structuredContent must be an object or null".into());
|
||||
}
|
||||
let output: PostHookOutput =
|
||||
serde_path_to_error::deserialize(body).map_err(|error| {
|
||||
// Serde's underlying error can contain the raw hook value.
|
||||
format!("invalid post-hook structuredContent.{}", error.path())
|
||||
})?;
|
||||
let units = output.extra_codex_rollout_budget_units;
|
||||
if units.is_finite() && units >= 0.0 {
|
||||
Ok(units)
|
||||
} else {
|
||||
Err(
|
||||
"hook extra_codex_rollout_budget_units must be finite and nonnegative"
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
dispatcher::ParsedHandler {
|
||||
completed: HookCompletedEvent {
|
||||
turn_id,
|
||||
run: dispatcher::completed_summary(handler, &result, status, entries),
|
||||
},
|
||||
data: (),
|
||||
data: price,
|
||||
completion_order: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -630,6 +630,7 @@ mod tests {
|
||||
|
||||
fn run_result(exit_code: Option<i32>, stdout: &str, stderr: &str) -> HandlerRunResult {
|
||||
HandlerRunResult {
|
||||
structured_content: None,
|
||||
context_metadata: codex_protocol::models::ContentItemMetadata::harness(),
|
||||
started_at: 1_700_000_000,
|
||||
completed_at: 1_700_000_001,
|
||||
|
||||
@@ -814,6 +814,7 @@ mod tests {
|
||||
|
||||
fn run_result(exit_code: Option<i32>, stdout: &str, stderr: &str) -> HandlerRunResult {
|
||||
HandlerRunResult {
|
||||
structured_content: None,
|
||||
context_metadata: codex_protocol::models::ContentItemMetadata::harness(),
|
||||
started_at: 1,
|
||||
completed_at: 2,
|
||||
|
||||
@@ -43,6 +43,7 @@ fn session_end_ignores_successful_output() {
|
||||
let completed = parse_completed(
|
||||
&handler(/*matcher*/ None),
|
||||
HandlerRunResult {
|
||||
structured_content: None,
|
||||
context_metadata: codex_protocol::models::ContentItemMetadata::harness(),
|
||||
started_at: 1,
|
||||
completed_at: 2,
|
||||
|
||||
@@ -564,6 +564,7 @@ mod tests {
|
||||
|
||||
fn run_result(exit_code: Option<i32>, stdout: &str, stderr: &str) -> HandlerRunResult {
|
||||
HandlerRunResult {
|
||||
structured_content: None,
|
||||
context_metadata: codex_protocol::models::ContentItemMetadata::harness(),
|
||||
started_at: 1,
|
||||
completed_at: 2,
|
||||
|
||||
@@ -702,6 +702,7 @@ mod tests {
|
||||
|
||||
fn run_result(exit_code: Option<i32>, stdout: &str, stderr: &str) -> HandlerRunResult {
|
||||
HandlerRunResult {
|
||||
structured_content: None,
|
||||
context_metadata: codex_protocol::models::ContentItemMetadata::harness(),
|
||||
started_at: 1,
|
||||
completed_at: 2,
|
||||
|
||||
@@ -483,6 +483,7 @@ mod tests {
|
||||
|
||||
fn run_result(exit_code: Option<i32>, stdout: &str, stderr: &str) -> HandlerRunResult {
|
||||
HandlerRunResult {
|
||||
structured_content: None,
|
||||
context_metadata: codex_protocol::models::ContentItemMetadata::harness(),
|
||||
started_at: 1,
|
||||
completed_at: 2,
|
||||
|
||||
@@ -134,7 +134,9 @@ pub fn hook_key(
|
||||
)
|
||||
}
|
||||
|
||||
pub use events::model_request::HookCharge;
|
||||
pub use events::model_request::ModelRequestHookEvent;
|
||||
pub use events::model_request::ModelRequestHookInput;
|
||||
pub use events::model_request::ModelRequestHookOutcome;
|
||||
pub use events::model_request::ModelRequestStatus;
|
||||
pub use events::model_request::SamplingKind;
|
||||
|
||||
@@ -17,9 +17,10 @@ pub struct HookMcpCall {
|
||||
pub timeout: Duration,
|
||||
}
|
||||
|
||||
/// Text returned by an MCP hook and the callable namespace that supplied it.
|
||||
/// The MCP hook's text, structured result, and originating callable namespace.
|
||||
pub struct HookMcpOutput {
|
||||
pub text: String,
|
||||
pub structured_content: Option<serde_json::Value>,
|
||||
pub source_tool_namespace: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
@@ -382,7 +382,10 @@ impl Hooks {
|
||||
&self,
|
||||
request: crate::ModelRequestHookInput,
|
||||
on_completed: impl FnMut(&HookCompletedEvent) + Send,
|
||||
) -> Vec<HookCompletedEvent> {
|
||||
) -> (
|
||||
Vec<codex_protocol::protocol::HookCompletedEvent>,
|
||||
crate::ModelRequestHookOutcome,
|
||||
) {
|
||||
crate::events::model_request::run(&self.engine, request, on_completed).await
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user