mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +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:
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user