Add bounded tool-result metadata support to executed tool calls (#44336)

## What changed

- Add host-recorded `tool_result_metadata` snapshots with size limits, redacted debug output, and protection against deserialization from untrusted input. MCP capture remains disabled.
- Preserve accepted metadata across retries and delayed Code Mode results. Shed raw metadata before source evidence, calls, or completion markers when the request budget is exceeded.
- Send raw metadata only to allowed OpenAI and ChatGPT HTTPS endpoints, filtering by the resolved destination for HTTP and WebSocket requests.
- Encode source parse failures as an array entry with type `parse_failed` and an empty ID.

## Testing

Add coverage for metadata bounds and redaction, retained-call updates, budget shedding, disabled MCP capture, and HTTP/WebSocket destination filtering.

GitOrigin-RevId: b1a396323ad0623167cdd9e20ff9453f0d01bbf7
This commit is contained in:
ningyi-oai authored and copyberry committed 2026-09-09 23:46:17 +00:00
1 parent b5544d5732
commit 0df6366a87
18 files changed
+1198 -120

No files matched your search

+43
View File
@@ -184,6 +184,9 @@ impl McpHandler {
self.tool_info.tool.name.as_ref(),
)
.await;
// Use the executed call's binding; a later catalog refresh must not change eligibility.
// Only the new metadata is internal; tool execution and call accounting are not.
let result_metadata_capture_allowed = false;
let mcp_tool = prepared_mcp_call.as_ref().map(|call| {
McpToolContext::from_prepared_call(
call,
@@ -241,6 +244,7 @@ impl McpHandler {
Ok(boxed_tool_output(McpToolOutput {
result: result.result,
tool_input: result.tool_input,
result_metadata_capture_allowed,
wall_time: started.elapsed(),
original_image_detail_supported: can_request_original_image_detail(
&step_context.settings.model_info,
@@ -566,6 +570,7 @@ mod tests {
use crate::tools::registry::PostToolUsePayload;
use crate::tools::registry::PreToolUsePayload;
use crate::turn_diff_tracker::TurnDiffTracker;
use codex_features::Feature;
use pretty_assertions::assert_eq;
use serde_json::json;
use std::time::Duration;
@@ -692,11 +697,21 @@ mod tests {
"file_id": "file_123"
}
}),
result_metadata_capture_allowed: false,
wall_time: Duration::from_millis(42),
original_image_detail_supported: true,
truncation_policy: codex_utils_output_truncation::TruncationPolicy::Bytes(1024),
};
let (session, turn) = make_session_and_context().await;
let mut session = session;
let mut turn = turn;
Arc::make_mut(&mut turn.config)
.features
.enable(Feature::ExecutedToolCallMetadata)
.expect("test feature must be configurable");
let recorder =
crate::tools::executed_tool_calls::ExecutedToolCalls::new(&turn.config.features);
session.services.executed_tool_calls = recorder.clone();
let turn = Arc::new(turn);
let handler = McpHandler::new(tool_info("filesystem", "filesystem", "read_file"))
.expect("MCP tool spec should build");
@@ -730,6 +745,34 @@ mod tests {
}),
})
);
// Reuse the direct invocation to catch recording placed after the Code Mode-only return.
let mut output = output;
output.result.meta = Some(json!({ "provider/custom": { "items": [1, null] } }));
output.result_metadata_capture_allowed = true;
recorder.record_tool_call(
&crate::tools::router::ToolCall {
tool_name: invocation.tool_name.clone(),
call_id: invocation.call_id.clone(),
payload: invocation.payload.clone(),
encrypted_function_args: None,
},
&invocation.source,
&invocation.step_context,
);
handler.on_tool_result_accepted(&invocation, &output);
let mut items = [codex_protocol::models::ResponseItem::from(
output.to_response_item(&invocation.call_id, &invocation.payload),
)];
recorder.attach_to_prompt(&mut items, &mut Default::default());
assert_eq!(
serde_json::to_value(items[0].executed_tool_call_metadata()).unwrap()["executed_tool_calls"],
json!([{
"name": codex_tools::code_mode_name_for_tool_name(&invocation.tool_name),
"arguments": { "path": "/tmp/notes.txt" },
"tool_result_metadata": { "provider/custom": { "items": [1, null] } },
}]),
);
}
#[test]