Include nested Apps tool results in Code Mode sampling requests (#62423)

## Why

Result body capture covered direct Apps calls, leaving eligible calls made inside Code Mode without their result bodies in follow-up sampling metadata.

## What changed

Capture eligible nested MCP results and bind them to the originating `exec` or `wait` output for the current turn. Send `tool_result` and `tool_result_call_id` together when both fit the request budget. Keep the bodies out of conversation history and reclaim capture capacity when the output binding is lost.

Require analytics to be enabled and the provider to allow request metadata before starting turn-level capture. Check the call's credentials at dispatch and again when recording the result.

## Testing

Add coverage for yielded and finished outputs, late results, retries with replacement output items, capture budget recovery, and the request size boundary for sending the body and call ID together.

GitOrigin-RevId: 0983261ac39021099d14efa5c9a9acac47ac9269
This commit is contained in:
ssetty-oai
2026-11-19 03:58:29 +00:00
committed by copyberry
parent 770c38d0b1
commit b5d9f7116e
10 changed files with 612 additions and 85 deletions
+15 -1
View File
@@ -26,6 +26,10 @@ use std::task::Poll;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
#[cfg(test)]
#[path = "common_tests.rs"]
mod tests;
pub const WS_REQUEST_HEADER_TRACEPARENT_CLIENT_METADATA_KEY: &str = "ws_request_header_traceparent";
pub const WS_REQUEST_HEADER_TRACESTATE_CLIENT_METADATA_KEY: &str = "ws_request_header_tracestate";
@@ -81,12 +85,22 @@ pub(crate) fn with_tool_results(
return None;
}
const FIELD: &str = r#","tool_result":"#;
let Some(next_remaining) = remaining.checked_sub(FIELD.len() + body.compact_bytes()) else {
const CALL_ID_FIELD: &str = r#","tool_result_call_id":"#;
let call_id_field_bytes = body
.nested_call_id_json()
.map_or(0, |_| CALL_ID_FIELD.len());
let Some(next_remaining) =
remaining.checked_sub(FIELD.len() + body.compact_bytes() + call_id_field_bytes)
else {
continue;
};
enriched.push_str(&json[cursor..close]);
enriched.push_str(FIELD);
enriched.push_str(body.as_json());
if let Some(id) = body.nested_call_id_json() {
enriched.push_str(CALL_ID_FIELD);
enriched.push_str(id);
}
cursor = close;
remaining = next_remaining;
}
+40
View File
@@ -0,0 +1,40 @@
//! Checks that private nested call IDs and their results share the wire budget.
#![cfg(test)]
use super::MAX_RESPONSE_MESSAGE_BYTES;
use super::with_tool_results;
use codex_protocol::models::ToolResultBody;
use pretty_assertions::assert_eq;
use serde_json::Value;
use serde_json::json;
use std::sync::Arc;
#[test]
fn nested_call_id_and_result_fit_the_wire_budget_together() {
let call_id = "private-\"id\\\nλ";
let result = json!({"content": [{"type": "text", "text": "private result"}]});
let mcp_result = serde_json::from_value(result.clone()).unwrap();
let mut body = ToolResultBody::from_mcp_result(&mcp_result).unwrap();
body.set_nested_call_id(call_id);
assert!(!format!("{body:?}").contains("private"));
let bodies = [(0, 0, Arc::new(body))];
let mut request = json!({"input": [{"internal_chat_message_metadata_passthrough": {
"executed_tool_calls": [{"name": "search", "arguments": {}}]
}}]})
.to_string();
let enriched = with_tool_results(&request, &bodies).unwrap();
let wire: Value = serde_json::from_str(&enriched).unwrap();
assert_eq!(
wire["input"][0]["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0],
json!({"name": "search", "arguments": {}, "tool_result": result, "tool_result_call_id": call_id})
);
request.push_str(&" ".repeat(MAX_RESPONSE_MESSAGE_BYTES - enriched.len()));
assert_eq!(
with_tool_results(&request, &bodies).unwrap().len(),
MAX_RESPONSE_MESSAGE_BYTES
);
request.push(' ');
assert!(with_tool_results(&request, &bodies).is_none());
}