mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Support optional Apps tool results in Responses requests (#62388)
## What changed Allow callers to attach eligible Apps tool results as `tool_result` on selected executed tool calls in Responses HTTP and WebSocket requests. Keep text content and optional object-valued `structuredContent`, with a 1 MiB per-result limit and a 15 MiB budget for the enriched message. Attach results after request observers receive the original JSON. Keep result IDs local and suppress WebSocket payload tracing for enriched frames. Preserve the enriched HTTP body on retries to the original endpoint and retry with the original JSON on HTTP 307 and 308 redirects. Codex's default request construction supplies no result bodies. ## Testing Add coverage for result eligibility and size limits, HTTP retry and redirect behavior, and TLS fallback without forwarding the enriched body to a redirect destination. GitOrigin-RevId: c3f2f86f31349e40fcb699d48637c3cbbbe38837
This commit is contained in:
@@ -3,6 +3,7 @@ use codex_protocol::ResponseUsageMetadata;
|
||||
use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig;
|
||||
use codex_protocol::config_types::Verbosity as VerbosityConfig;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::models::ToolResultBody;
|
||||
use codex_protocol::openai_models::ReasoningEffort as ReasoningEffortConfig;
|
||||
use codex_protocol::protocol::ModelVerification;
|
||||
use codex_protocol::protocol::RateLimitSnapshot;
|
||||
@@ -28,6 +29,74 @@ use tokio::sync::oneshot;
|
||||
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";
|
||||
|
||||
/// Maximum outgoing Responses message size for attaching optional tool observations.
|
||||
pub const MAX_RESPONSE_MESSAGE_BYTES: usize = 15 * 1024 * 1024;
|
||||
|
||||
/// Adds authorized connector results to the selected rows without re-encoding the ordinary request.
|
||||
/// Borrowed raw JSON leaves routing field order, tool schemas, and unrelated inputs intact.
|
||||
pub(crate) fn with_tool_results(
|
||||
json: &str,
|
||||
results: &[(usize, usize, Arc<ToolResultBody>)],
|
||||
) -> Option<String> {
|
||||
#[derive(Deserialize)]
|
||||
struct Input<'a> {
|
||||
#[serde(borrow)]
|
||||
input: Vec<Item<'a>>,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
struct Item<'a> {
|
||||
#[serde(borrow)]
|
||||
internal_chat_message_metadata_passthrough: Option<Metadata<'a>>,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
struct Metadata<'a> {
|
||||
#[serde(borrow)]
|
||||
executed_tool_calls: Option<Vec<&'a RawValue>>,
|
||||
}
|
||||
|
||||
let mut remaining = MAX_RESPONSE_MESSAGE_BYTES.checked_sub(json.len())?;
|
||||
if results.is_empty() || remaining == 0 {
|
||||
return None;
|
||||
}
|
||||
let input: Input<'_> = serde_json::from_str(json).ok()?;
|
||||
let mut enriched = String::new();
|
||||
let mut cursor = 0;
|
||||
for (item, call, body) in results {
|
||||
let row = input
|
||||
.input
|
||||
.get(*item)?
|
||||
.internal_chat_message_metadata_passthrough
|
||||
.as_ref()?
|
||||
.executed_tool_calls
|
||||
.as_ref()?
|
||||
.get(*call)?
|
||||
.get();
|
||||
// Native ExecutedToolCall rows always contain name and arguments.
|
||||
let row_fields = row.strip_prefix('{')?.strip_suffix('}')?;
|
||||
if row_fields.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let close = row.as_ptr() as usize - json.as_ptr() as usize + row.len() - 1;
|
||||
if close <= cursor {
|
||||
return None;
|
||||
}
|
||||
const FIELD: &str = r#","tool_result":"#;
|
||||
let Some(next_remaining) = remaining.checked_sub(FIELD.len() + body.compact_bytes()) else {
|
||||
continue;
|
||||
};
|
||||
enriched.push_str(&json[cursor..close]);
|
||||
enriched.push_str(FIELD);
|
||||
enriched.push_str(body.as_json());
|
||||
cursor = close;
|
||||
remaining = next_remaining;
|
||||
}
|
||||
if enriched.is_empty() {
|
||||
return None;
|
||||
}
|
||||
enriched.push_str(&json[cursor..]);
|
||||
Some(enriched)
|
||||
}
|
||||
|
||||
/// Explicit per-request access selection using the Responses API wire values.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
|
||||
pub struct AccessPrograms {
|
||||
@@ -359,6 +428,7 @@ impl<'a> From<&'a ResponsesApiRequest> for ResponseCreateWsRequest<'a> {
|
||||
model: &request.model,
|
||||
previous_response_id: None,
|
||||
input: &request.input,
|
||||
tool_results: Vec::new(),
|
||||
tools: request.tools.as_ref().map(ResponsesApiTools::as_raw_value),
|
||||
tool_choice: &request.tool_choice,
|
||||
parallel_tool_calls: request.parallel_tool_calls,
|
||||
@@ -390,6 +460,9 @@ pub struct ResponseCreateWsRequest<'a> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub previous_response_id: Option<String>,
|
||||
pub input: &'a [ResponseItem],
|
||||
/// Observations for this wire frame only. Canonical request serialization omits them.
|
||||
#[serde(skip)]
|
||||
pub tool_results: Vec<(usize, usize, Arc<ToolResultBody>)>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tools: Option<&'a RawValue>,
|
||||
pub tool_choice: &'a str,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::auth::SharedAuthProvider;
|
||||
use crate::common::ResponseStream;
|
||||
use crate::common::ResponsesApiRequest;
|
||||
use crate::common::with_tool_results;
|
||||
use crate::endpoint::session::EndpointSession;
|
||||
use crate::error::ApiError;
|
||||
use crate::provider::Provider;
|
||||
@@ -14,10 +15,13 @@ use codex_client::EncodedJsonBody;
|
||||
use codex_client::HttpTransport;
|
||||
use codex_client::RequestCompression;
|
||||
use codex_client::RequestTelemetry;
|
||||
use codex_client::TransportError;
|
||||
use codex_protocol::models::ToolResultBody;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use http::HeaderMap;
|
||||
use http::HeaderValue;
|
||||
use http::Method;
|
||||
use http::StatusCode;
|
||||
use serde_json::Value;
|
||||
use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
@@ -35,6 +39,7 @@ pub struct ResponsesOptions {
|
||||
pub session_source: Option<SessionSource>,
|
||||
pub extra_headers: HeaderMap,
|
||||
pub compression: Compression,
|
||||
pub tool_results: Vec<(usize, usize, Arc<ToolResultBody>)>,
|
||||
pub turn_state: Option<Arc<OnceLock<String>>>,
|
||||
}
|
||||
|
||||
@@ -66,7 +71,7 @@ impl<T: HttpTransport> ResponsesClient<T> {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Gives the observer a shared copy of the exact JSON before transport dispatch.
|
||||
/// Observes the encoded request before private connector results are attached.
|
||||
#[instrument(
|
||||
name = "responses.stream_request",
|
||||
level = "info",
|
||||
@@ -89,6 +94,7 @@ impl<T: HttpTransport> ResponsesClient<T> {
|
||||
session_source,
|
||||
extra_headers,
|
||||
compression,
|
||||
tool_results,
|
||||
turn_state,
|
||||
} = options;
|
||||
let body = EncodedJsonBody::encode(&request)
|
||||
@@ -104,6 +110,30 @@ impl<T: HttpTransport> ResponsesClient<T> {
|
||||
insert_header(&mut headers, "x-openai-subagent", &subagent);
|
||||
}
|
||||
|
||||
if !tool_results.is_empty()
|
||||
&& let Ok(json) = std::str::from_utf8(body.as_bytes())
|
||||
&& let Some(json) = with_tool_results(json, &tool_results)
|
||||
{
|
||||
let stream = self
|
||||
.stream_encoded(
|
||||
body.with_private_json(json),
|
||||
headers.clone(),
|
||||
compression,
|
||||
turn_state.clone(),
|
||||
)
|
||||
.await;
|
||||
// A redirect that preserves the request body must start over without private results.
|
||||
if !matches!(
|
||||
stream,
|
||||
Err(ApiError::Transport(TransportError::Http {
|
||||
status: StatusCode::TEMPORARY_REDIRECT | StatusCode::PERMANENT_REDIRECT,
|
||||
headers: Some(ref headers),
|
||||
..
|
||||
})) if headers.get(http::header::LOCATION).is_some_and(|location| !location.is_empty())
|
||||
) {
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
self.stream_encoded(body, headers, compression, turn_state)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::common::ResponseStream;
|
||||
use crate::common::ResponsesWsRequest;
|
||||
use crate::common::SafetyBufferingTreatment;
|
||||
use crate::common::WS_REQUEST_HEADER_TRACEPARENT_CLIENT_METADATA_KEY;
|
||||
use crate::common::with_tool_results;
|
||||
use crate::endpoint::responses_websocket_probe::ResponsesWebsocketClose;
|
||||
use crate::endpoint::responses_websocket_probe::ResponsesWebsocketProbe;
|
||||
use crate::endpoint::responses_websocket_probe::ResponsesWebsocketProbeError;
|
||||
@@ -56,6 +57,7 @@ use tracing::debug;
|
||||
use tracing::error;
|
||||
use tracing::info;
|
||||
use tracing::instrument;
|
||||
use tracing::instrument::WithSubscriber;
|
||||
use tungstenite::extensions::ExtensionsConfig;
|
||||
use tungstenite::extensions::compression::deflate::DeflateConfig;
|
||||
use tungstenite::protocol::WebSocketConfig;
|
||||
@@ -69,9 +71,14 @@ struct WsStream {
|
||||
pump_task: tokio::task::JoinHandle<()>,
|
||||
}
|
||||
|
||||
struct WsRequestText {
|
||||
text: Utf8Bytes,
|
||||
private: bool,
|
||||
}
|
||||
|
||||
enum WsCommand {
|
||||
Send {
|
||||
message: Message,
|
||||
request: WsRequestText,
|
||||
tx_result: oneshot::Sender<Result<(), WsError>>,
|
||||
},
|
||||
}
|
||||
@@ -121,8 +128,14 @@ impl WsStream {
|
||||
break;
|
||||
};
|
||||
match command {
|
||||
WsCommand::Send { message, tx_result } => {
|
||||
let result = send_message(&mut inner, message).await;
|
||||
WsCommand::Send { request, tx_result } => {
|
||||
let send = send_message(&mut inner, Message::Text(request.text));
|
||||
// Tungstenite traces frame payloads from inside this future.
|
||||
let result = if request.private {
|
||||
send.with_subscriber(tracing::Dispatch::none()).await
|
||||
} else {
|
||||
send.await
|
||||
};
|
||||
let should_break = result.is_err();
|
||||
let _ = tx_result.send(result);
|
||||
if should_break {
|
||||
@@ -190,8 +203,8 @@ impl WsStream {
|
||||
Err(WsError::ConnectionClosed)
|
||||
}
|
||||
|
||||
async fn send(&mut self, message: Message) -> Result<(), WsError> {
|
||||
self.request(|tx_result| WsCommand::Send { message, tx_result })
|
||||
async fn send(&mut self, request: WsRequestText) -> Result<(), WsError> {
|
||||
self.request(|tx_result| WsCommand::Send { request, tx_result })
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -294,7 +307,7 @@ impl ResponsesWebsocketConnection {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Gives the observer a shared copy of the exact JSON before transport dispatch.
|
||||
/// Gives the observer the serialized request before private tool results are attached.
|
||||
#[instrument(
|
||||
name = "responses_websocket.stream_request",
|
||||
level = "info",
|
||||
@@ -343,6 +356,11 @@ impl ResponsesWebsocketConnection {
|
||||
};
|
||||
let request_text = Utf8Bytes::from(serialize_websocket_request(&request)?);
|
||||
on_encoded(request_text.as_ref());
|
||||
let enriched = with_tool_results(request_text.as_str(), &ws_request.tool_results);
|
||||
let request_text = WsRequestText {
|
||||
private: enriched.is_some(),
|
||||
text: enriched.map(Utf8Bytes::from).unwrap_or(request_text),
|
||||
};
|
||||
|
||||
let current_span = Span::current();
|
||||
tokio::spawn(
|
||||
@@ -841,7 +859,7 @@ fn is_websocket_receive_failure(
|
||||
async fn run_websocket_response_stream(
|
||||
ws_stream: &mut WsStream,
|
||||
tx_event: mpsc::Sender<std::result::Result<ResponseEvent, ApiError>>,
|
||||
request_text: Utf8Bytes,
|
||||
request_text: WsRequestText,
|
||||
idle_timeout: Duration,
|
||||
telemetry: Option<Arc<dyn WebsocketTelemetry>>,
|
||||
turn_state: Option<&OnceLock<String>>,
|
||||
@@ -883,11 +901,14 @@ async fn run_websocket_response_stream(
|
||||
interruption = Some(request);
|
||||
send_websocket_request(
|
||||
ws_stream,
|
||||
serde_json::json!({
|
||||
"type": "response.interrupt",
|
||||
"response_id": response_id,
|
||||
"mode": "discard_partial_items",
|
||||
}).to_string().into(),
|
||||
WsRequestText {
|
||||
text: serde_json::json!({
|
||||
"type": "response.interrupt",
|
||||
"response_id": response_id,
|
||||
"mode": "discard_partial_items",
|
||||
}).to_string().into(),
|
||||
private: false,
|
||||
},
|
||||
idle_timeout,
|
||||
/*telemetry*/ None,
|
||||
timing_log_context.connection_reused,
|
||||
@@ -1118,19 +1139,19 @@ fn safety_buffering_for_event(
|
||||
|
||||
async fn send_websocket_request(
|
||||
ws_stream: &mut WsStream,
|
||||
request_text: Utf8Bytes,
|
||||
request_text: WsRequestText,
|
||||
idle_timeout: Duration,
|
||||
telemetry: Option<&Arc<dyn WebsocketTelemetry>>,
|
||||
connection_reused: bool,
|
||||
) -> Result<(), ApiError> {
|
||||
if ws_stream
|
||||
.max_message_bytes
|
||||
.is_some_and(|limit| request_text.len() > limit)
|
||||
.is_some_and(|limit| request_text.text.len() > limit)
|
||||
{
|
||||
return Err(ApiError::WebsocketMessageTooLarge);
|
||||
}
|
||||
let request_start = Instant::now();
|
||||
let result = tokio::time::timeout(idle_timeout, ws_stream.send(Message::Text(request_text)))
|
||||
let result = tokio::time::timeout(idle_timeout, ws_stream.send(request_text))
|
||||
.await
|
||||
.map_err(|_| ApiError::Stream("idle timeout sending websocket request".into()))
|
||||
.and_then(|result| result.map_err(map_ws_stream_error));
|
||||
|
||||
@@ -35,6 +35,7 @@ pub use crate::auth::AuthRefreshFuture;
|
||||
pub use crate::auth::SharedAuthProvider;
|
||||
pub use crate::auth::auth_header_telemetry;
|
||||
pub use crate::common::AccessPrograms;
|
||||
pub use crate::common::MAX_RESPONSE_MESSAGE_BYTES;
|
||||
pub use crate::common::MemorySummarizeInput;
|
||||
pub use crate::common::MemorySummarizeOutput;
|
||||
pub use crate::common::OpenAiVerbosity;
|
||||
|
||||
@@ -19,16 +19,26 @@ use codex_client::RequestBody;
|
||||
use codex_client::Response;
|
||||
use codex_client::StreamResponse;
|
||||
use codex_client::TransportError;
|
||||
use codex_http_client::HttpClientBuilder;
|
||||
use codex_protocol::ResponseItemId;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ExecutedToolCall;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::models::ToolResultBody;
|
||||
use codex_protocol::models::ToolResultMetadata;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
use http::HeaderMap;
|
||||
use http::HeaderValue;
|
||||
use http::StatusCode;
|
||||
use http::header::AUTHORIZATION;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::Value;
|
||||
use serde_json::value::RawValue;
|
||||
use wiremock::Mock;
|
||||
use wiremock::MockServer;
|
||||
use wiremock::ResponseTemplate;
|
||||
use wiremock::matchers::method;
|
||||
|
||||
fn assert_path_ends_with(requests: &[Request], suffix: &str) {
|
||||
assert_eq!(requests.len(), 1);
|
||||
@@ -485,17 +495,11 @@ async fn streaming_client_adds_auth_headers() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn streaming_client_retries_on_transport_error() -> Result<()> {
|
||||
let transport = FlakyTransport::new();
|
||||
|
||||
let mut provider = provider("openai");
|
||||
provider.retry.max_attempts = 2;
|
||||
|
||||
let request = ResponsesApiRequest {
|
||||
fn sampling_request(input: Vec<ResponseItem>) -> ResponsesApiRequest {
|
||||
ResponsesApiRequest {
|
||||
request_country: Default::default(),
|
||||
model: "gpt-test".into(),
|
||||
input: Vec::new(),
|
||||
input,
|
||||
tools: Some(empty_tools().into()),
|
||||
tool_choice: "auto".into(),
|
||||
parallel_tool_calls: false,
|
||||
@@ -510,7 +514,92 @@ async fn streaming_client_retries_on_transport_error() -> Result<()> {
|
||||
text: None,
|
||||
client_metadata: None,
|
||||
access_programs: None,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn responses_retry_keeps_tool_results_and_redirect_omits_them() -> Result<()> {
|
||||
let mut item: ResponseItem = serde_json::from_value(serde_json::json!({
|
||||
"type": "function_call_output", "call_id": "read", "output": "one match",
|
||||
}))?;
|
||||
let mut call = ExecutedToolCall::new("search".into(), serde_json::json!({"query": "test"}));
|
||||
call.set_tool_result_metadata(ToolResultMetadata::new(&serde_json::json!({"matches": 1})));
|
||||
item.append_executed_tool_calls(vec![call]);
|
||||
let request = sampling_request(vec![item]);
|
||||
let private = serde_json::json!({"content": [{"type": "text", "text": "private match"}]});
|
||||
let result = serde_json::from_value(private.clone())?;
|
||||
let body = Arc::new(ToolResultBody::from_mcp_result(&result).expect("eligible Apps result"));
|
||||
let canonical = serde_json::to_value(&request)?;
|
||||
let mut enriched = canonical.clone();
|
||||
enriched["input"][0]["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0]
|
||||
["tool_result"] = private;
|
||||
|
||||
for status in [307, 308] {
|
||||
let origin = MockServer::start().await;
|
||||
let destination = MockServer::start().await;
|
||||
Mock::given(method("POST"))
|
||||
.respond_with(ResponseTemplate::new(503))
|
||||
.with_priority(/*p*/ 1)
|
||||
.up_to_n_times(1)
|
||||
.mount(&origin)
|
||||
.await;
|
||||
Mock::given(method("POST"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(status).insert_header("location", destination.uri()),
|
||||
)
|
||||
.mount(&origin)
|
||||
.await;
|
||||
Mock::given(method("POST"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_raw("", "text/event-stream"))
|
||||
.mount(&destination)
|
||||
.await;
|
||||
let mut provider = provider("openai");
|
||||
provider.base_url = origin.uri();
|
||||
provider.retry.retry_5xx = true;
|
||||
let transport = codex_client::ReqwestTransport::from_http_client(
|
||||
HttpClientBuilder::new().build_direct()?,
|
||||
);
|
||||
let client = ResponsesClient::new(
|
||||
transport,
|
||||
provider,
|
||||
Arc::new(StaticAuth::new("test-secret", "account")),
|
||||
);
|
||||
let mut observed = Bytes::new();
|
||||
let _stream = client
|
||||
.stream_request_with_observer(
|
||||
request.clone(),
|
||||
ResponsesOptions {
|
||||
tool_results: vec![(0, 0, Arc::clone(&body))],
|
||||
..Default::default()
|
||||
},
|
||||
|json| observed = json.clone(),
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(serde_json::from_slice::<Value>(&observed)?, canonical);
|
||||
|
||||
let sent = origin.received_requests().await.unwrap();
|
||||
let forwarded = destination.received_requests().await.unwrap();
|
||||
assert_eq!((sent.len(), forwarded.len()), (3, 1));
|
||||
assert_eq!(sent[0].body, sent[1].body);
|
||||
for (actual, expected, authenticated) in [
|
||||
(&sent[0], &enriched, true),
|
||||
(&sent[1], &enriched, true),
|
||||
(&sent[2], &canonical, true),
|
||||
(&forwarded[0], &canonical, false),
|
||||
] {
|
||||
assert_eq!(&actual.body_json::<Value>()?, expected);
|
||||
assert_eq!(actual.headers.contains_key(AUTHORIZATION), authenticated);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn streaming_client_retries_on_transport_error() -> Result<()> {
|
||||
let transport = FlakyTransport::new();
|
||||
let mut provider = provider("openai");
|
||||
provider.retry.max_attempts = 2;
|
||||
let request = sampling_request(Vec::new());
|
||||
let client = ResponsesClient::new(transport.clone(), provider, Arc::new(NoAuth));
|
||||
|
||||
let _stream = client
|
||||
@@ -650,6 +739,7 @@ async fn azure_store_sends_ids_and_headers() -> Result<()> {
|
||||
session_source: Some(SessionSource::SubAgent(SubAgentSource::Review)),
|
||||
extra_headers,
|
||||
compression: Compression::None,
|
||||
tool_results: Vec::new(),
|
||||
turn_state: None,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -1693,6 +1693,7 @@ impl<'a> ResponsesHttpExecutor<'a> {
|
||||
headers
|
||||
},
|
||||
compression,
|
||||
tool_results: Vec::new(),
|
||||
turn_state: Some(Arc::clone(turn_state)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,12 @@
|
||||
//! Only the wire copy changes; model-visible input and WebSocket continuation history stay intact.
|
||||
|
||||
use crate::tools::metadata_metrics;
|
||||
use codex_api::MAX_RESPONSE_MESSAGE_BYTES;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::models::bound_executed_tool_calls_for_message;
|
||||
use codex_utils_string::serialized_json_bytes;
|
||||
use serde::Serialize;
|
||||
|
||||
const MAX_RESPONSE_MESSAGE_BYTES: usize = 15 * 1024 * 1024;
|
||||
|
||||
pub(super) fn bounded_input<T: Serialize>(
|
||||
message: &T,
|
||||
input: &[ResponseItem],
|
||||
|
||||
@@ -4,6 +4,7 @@ use http::HeaderValue;
|
||||
use http::Method;
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use std::fmt;
|
||||
use std::time::Duration;
|
||||
|
||||
/// A JSON request body serialized once into reference-counted bytes.
|
||||
@@ -11,11 +12,27 @@ use std::time::Duration;
|
||||
/// Clones share the encoded allocation. Internally, the body can also hold the
|
||||
/// final compressed wire bytes while retaining the original JSON only when
|
||||
/// request-body trace logging is enabled.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct EncodedJsonBody {
|
||||
bytes: Bytes,
|
||||
trace_bytes: Option<Bytes>,
|
||||
prepared: bool,
|
||||
private: bool,
|
||||
}
|
||||
|
||||
impl fmt::Debug for EncodedJsonBody {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut body = f.debug_struct("EncodedJsonBody");
|
||||
if self.private {
|
||||
return body
|
||||
.field("byte_len", &self.bytes.len())
|
||||
.finish_non_exhaustive();
|
||||
}
|
||||
body.field("bytes", &self.bytes)
|
||||
.field("trace_bytes", &self.trace_bytes)
|
||||
.field("prepared", &self.prepared)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl EncodedJsonBody {
|
||||
@@ -25,6 +42,7 @@ impl EncodedJsonBody {
|
||||
bytes: Bytes::from(bytes),
|
||||
trace_bytes: None,
|
||||
prepared: false,
|
||||
private: false,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -38,6 +56,16 @@ impl EncodedJsonBody {
|
||||
&self.bytes
|
||||
}
|
||||
|
||||
/// Send richer JSON only to the configured origin and use the original body in traces.
|
||||
pub fn with_private_json(&self, json: String) -> Self {
|
||||
Self {
|
||||
bytes: Bytes::from(json),
|
||||
trace_bytes: Some(self.bytes.clone()),
|
||||
prepared: false,
|
||||
private: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn trace_bytes(&self) -> &[u8] {
|
||||
self.trace_bytes.as_ref().unwrap_or(&self.bytes)
|
||||
}
|
||||
@@ -78,6 +106,12 @@ impl PreparedRequestBody {
|
||||
}
|
||||
}
|
||||
|
||||
// Wire bytes can contain private connector results. Deliberately omit Debug.
|
||||
pub(crate) struct WireBody {
|
||||
pub(crate) headers: HeaderMap,
|
||||
pub(crate) body: Option<Bytes>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Request {
|
||||
pub method: Method,
|
||||
@@ -129,7 +163,14 @@ impl Request {
|
||||
self.body,
|
||||
Some(RequestBody::Json(_) | RequestBody::EncodedJson(_))
|
||||
);
|
||||
let trace_bytes = if self.compression != RequestCompression::None
|
||||
let private_trace = match &self.body {
|
||||
Some(RequestBody::EncodedJson(body)) if body.private => body.trace_bytes.clone(),
|
||||
_ => None,
|
||||
};
|
||||
let private = private_trace.is_some();
|
||||
let trace_bytes = if private {
|
||||
private_trace
|
||||
} else if self.compression != RequestCompression::None
|
||||
&& tracing::enabled!(target: "codex_http_client::transport", tracing::Level::TRACE)
|
||||
{
|
||||
match self.body.as_ref() {
|
||||
@@ -142,13 +183,14 @@ impl Request {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let prepared = self.prepare_body_for_send()?;
|
||||
let prepared = self.prepare_wire_body()?;
|
||||
self.headers = prepared.headers;
|
||||
self.body = match (is_json, prepared.body) {
|
||||
(true, Some(bytes)) => Some(RequestBody::EncodedJson(EncodedJsonBody {
|
||||
bytes,
|
||||
trace_bytes,
|
||||
prepared: true,
|
||||
private,
|
||||
})),
|
||||
(false, Some(body)) => Some(RequestBody::Raw(body)),
|
||||
(_, None) => None,
|
||||
@@ -161,15 +203,31 @@ impl Request {
|
||||
///
|
||||
/// Auth schemes such as AWS SigV4 need to sign the final body bytes, including
|
||||
/// compression and content headers. Calling this method does not mutate the
|
||||
/// request.
|
||||
/// request. Use [`Self::into_prepared`] for private JSON to retain its redacted
|
||||
/// debug representation.
|
||||
pub fn prepare_body_for_send(&self) -> Result<PreparedRequestBody, String> {
|
||||
if self.has_private_body() {
|
||||
return Err("private JSON requires Request::into_prepared".to_string());
|
||||
}
|
||||
let wire = self.prepare_wire_body()?;
|
||||
Ok(PreparedRequestBody {
|
||||
headers: wire.headers,
|
||||
body: wire.body,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn has_private_body(&self) -> bool {
|
||||
matches!(&self.body, Some(RequestBody::EncodedJson(body)) if body.private)
|
||||
}
|
||||
|
||||
pub(crate) fn prepare_wire_body(&self) -> Result<WireBody, String> {
|
||||
let headers = self.headers.clone();
|
||||
match self.body.as_ref() {
|
||||
Some(RequestBody::Raw(raw_body)) => {
|
||||
if self.compression != RequestCompression::None {
|
||||
return Err("request compression cannot be used with raw bodies".to_string());
|
||||
}
|
||||
Ok(PreparedRequestBody {
|
||||
Ok(WireBody {
|
||||
headers,
|
||||
body: Some(raw_body.clone()),
|
||||
})
|
||||
@@ -179,7 +237,7 @@ impl Request {
|
||||
self.prepare_encoded_json(headers, &body)
|
||||
}
|
||||
Some(RequestBody::EncodedJson(body)) => self.prepare_encoded_json(headers, body),
|
||||
None => Ok(PreparedRequestBody {
|
||||
None => Ok(WireBody {
|
||||
headers,
|
||||
body: None,
|
||||
}),
|
||||
@@ -190,9 +248,9 @@ impl Request {
|
||||
&self,
|
||||
mut headers: HeaderMap,
|
||||
body: &EncodedJsonBody,
|
||||
) -> Result<PreparedRequestBody, String> {
|
||||
) -> Result<WireBody, String> {
|
||||
if body.prepared {
|
||||
return Ok(PreparedRequestBody {
|
||||
return Ok(WireBody {
|
||||
headers,
|
||||
body: Some(body.bytes.clone()),
|
||||
});
|
||||
@@ -240,7 +298,7 @@ impl Request {
|
||||
);
|
||||
}
|
||||
|
||||
Ok(PreparedRequestBody {
|
||||
Ok(WireBody {
|
||||
headers,
|
||||
body: Some(bytes),
|
||||
})
|
||||
@@ -328,6 +386,92 @@ mod tests {
|
||||
Some(&HeaderValue::from_static("application/json"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordinary_request_bodies_keep_debug_fields() {
|
||||
let canonical = EncodedJsonBody::encode(&json!({"model": "test"})).expect("valid JSON");
|
||||
let json = &canonical.bytes;
|
||||
assert_eq!(
|
||||
format!("{canonical:?}"),
|
||||
format!("EncodedJsonBody {{ bytes: {json:?}, trace_bytes: None, prepared: false }}")
|
||||
);
|
||||
|
||||
for compression in [RequestCompression::None, RequestCompression::Zstd] {
|
||||
let mut request = Request::new(Method::POST, "https://example.com".to_string())
|
||||
.with_compression(compression);
|
||||
request
|
||||
.headers
|
||||
.insert("x-test", HeaderValue::from_static("header-marker"));
|
||||
request.body = Some(RequestBody::EncodedJson(canonical.clone()));
|
||||
let subscriber = tracing_subscriber::registry();
|
||||
let prepared =
|
||||
tracing::subscriber::with_default(subscriber, || request.into_prepared())
|
||||
.expect("body should prepare");
|
||||
let wire = prepared.prepare_body_for_send().expect("prepared body");
|
||||
let Some(RequestBody::EncodedJson(encoded)) = prepared.body.as_ref() else {
|
||||
panic!("expected encoded JSON");
|
||||
};
|
||||
let PreparedRequestBody { headers, body } = &wire;
|
||||
let bytes = body.as_ref().expect("wire bytes");
|
||||
let trace_bytes = (compression == RequestCompression::Zstd).then_some(json);
|
||||
assert_eq!(
|
||||
format!("{encoded:?}"),
|
||||
format!(
|
||||
"EncodedJsonBody {{ bytes: {bytes:?}, trace_bytes: {trace_bytes:?}, prepared: true }}"
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
format!("{wire:?}"),
|
||||
format!("PreparedRequestBody {{ headers: {headers:?}, body: Some({bytes:?}) }}")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn private_json_keeps_canonical_trace_body() {
|
||||
let mut call = json!({
|
||||
"arguments": "argument-marker",
|
||||
"tool_result_metadata": "metadata-marker"
|
||||
});
|
||||
let canonical = EncodedJsonBody::encode(&call).expect("valid JSON");
|
||||
call["tool_result"] = json!("private-result-marker");
|
||||
let wire = call.to_string();
|
||||
let enriched = canonical.with_private_json(wire.clone());
|
||||
for compression in [RequestCompression::None, RequestCompression::Zstd] {
|
||||
let mut request = Request::new(Method::POST, "https://example.com".to_string())
|
||||
.with_compression(compression);
|
||||
request.body = Some(RequestBody::EncodedJson(enriched.clone()));
|
||||
let prepared = request
|
||||
.clone()
|
||||
.into_prepared()
|
||||
.expect("body should prepare");
|
||||
for request in [&request, &prepared] {
|
||||
assert_eq!(
|
||||
request.prepare_body_for_send().unwrap_err(),
|
||||
"private JSON requires Request::into_prepared"
|
||||
);
|
||||
}
|
||||
let Some(RequestBody::EncodedJson(body)) = prepared.body.as_ref() else {
|
||||
panic!("expected encoded JSON");
|
||||
};
|
||||
for encoded in [&enriched, body] {
|
||||
let len = encoded.as_bytes().len();
|
||||
assert_eq!(
|
||||
format!("{encoded:?}"),
|
||||
format!("EncodedJsonBody {{ byte_len: {len}, .. }}")
|
||||
);
|
||||
}
|
||||
assert_eq!(body.trace_bytes(), canonical.as_bytes());
|
||||
let wire_body = prepared.prepare_wire_body().expect("wire body");
|
||||
let bytes = wire_body.body.as_deref().expect("wire bytes");
|
||||
assert_eq!(bytes, body.as_bytes());
|
||||
let bytes = match compression {
|
||||
RequestCompression::None => bytes.to_vec(),
|
||||
RequestCompression::Zstd => zstd::stream::decode_all(bytes).expect("valid zstd"),
|
||||
};
|
||||
assert_eq!(bytes, wire.as_bytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
||||
@@ -20,6 +20,13 @@ use crate::client::TransportClient;
|
||||
pub(crate) struct RequestDraft {
|
||||
pub(crate) request: reqwest::Request,
|
||||
pub(crate) headers: Vec<HeaderUpdate>,
|
||||
pub(crate) private_body: bool,
|
||||
}
|
||||
|
||||
/// Streaming preserves the exact length but prevents reqwest from replaying the body on redirects.
|
||||
pub(crate) fn prevent_body_redirects(request: &mut reqwest::Request) {
|
||||
let body = request.body_mut();
|
||||
*body = body.take().map(reqwest::Body::wrap);
|
||||
}
|
||||
|
||||
pub(crate) enum HeaderUpdate {
|
||||
@@ -30,7 +37,11 @@ pub(crate) enum HeaderUpdate {
|
||||
impl From<reqwest::Request> for RequestDraft {
|
||||
fn from(mut request: reqwest::Request) -> Self {
|
||||
let headers = vec![HeaderUpdate::Replace(std::mem::take(request.headers_mut()))];
|
||||
Self { request, headers }
|
||||
Self {
|
||||
request,
|
||||
headers,
|
||||
private_body: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ use crate::RouteFailureClass;
|
||||
use crate::client::TransportClient;
|
||||
use crate::client::apply_default_headers;
|
||||
use crate::request_draft::RequestDraft;
|
||||
use crate::request_draft::prevent_body_redirects;
|
||||
use crate::route_aware_redirect::MAX_REDIRECTS;
|
||||
use crate::route_aware_redirect::insert_referer;
|
||||
use crate::route_aware_redirect::is_redirect;
|
||||
@@ -104,7 +105,12 @@ impl RouteAwareClientPool {
|
||||
.await?;
|
||||
client.request_logging = self.client_builder.request_logging;
|
||||
if let Some(headers) = draft_headers.take() {
|
||||
request = RequestDraft { request, headers }.build(&client)?;
|
||||
request = RequestDraft {
|
||||
request,
|
||||
headers,
|
||||
private_body: draft.private_body,
|
||||
}
|
||||
.build(&client)?;
|
||||
request_url = request.url().to_string();
|
||||
apply_default_headers(request.headers_mut(), &self.default_headers);
|
||||
}
|
||||
@@ -122,6 +128,11 @@ impl RouteAwareClientPool {
|
||||
let version = request.version();
|
||||
let timeout = request.timeout().copied();
|
||||
let replay = request.try_clone();
|
||||
let private_body = draft.private_body && redirects == 0;
|
||||
if private_body {
|
||||
// Connection recovery uses the original URL and clients with redirects disabled.
|
||||
prevent_body_redirects(&mut request);
|
||||
}
|
||||
let result = if follows_redirects_manually {
|
||||
client.execute_without_request_logging(request).await
|
||||
} else {
|
||||
@@ -174,7 +185,7 @@ impl RouteAwareClientPool {
|
||||
headers,
|
||||
version,
|
||||
timeout,
|
||||
replay,
|
||||
replay.filter(|_| !private_body),
|
||||
next_url,
|
||||
)
|
||||
})
|
||||
|
||||
@@ -23,6 +23,8 @@ use super::OutboundProxyRoute;
|
||||
use super::RouteAwareClientPool;
|
||||
use super::RouteAwareRequestError;
|
||||
use super::SelectedTlsBackend;
|
||||
use crate::HttpClientBuilder;
|
||||
use crate::HttpClientTlsConfig;
|
||||
use crate::client::RequestLogging;
|
||||
use crate::client::TransportClient;
|
||||
use crate::tls_backend_fallback::should_retry_with_rustls;
|
||||
@@ -40,6 +42,7 @@ type SuccessfulTlsFallbackServer = (
|
||||
String,
|
||||
TransportClient,
|
||||
mpsc::Receiver<io::Result<Vec<String>>>,
|
||||
String,
|
||||
);
|
||||
|
||||
#[tokio::test]
|
||||
@@ -214,11 +217,25 @@ async fn does_not_retry_a_cached_rustls_tls_protocol_failure() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn successful_rustls_fallback_replays_the_request_and_reuses_the_destination() {
|
||||
let (url, trusted_rustls_client, observed_requests) =
|
||||
assert_tls_fallback_replay(/*private_body*/ false).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn private_body_recovers_with_rustls_without_following_the_redirect() {
|
||||
assert_tls_fallback_replay(/*private_body*/ true).await;
|
||||
}
|
||||
|
||||
async fn assert_tls_fallback_replay(private_body: bool) {
|
||||
let (url, trusted_rustls_client, observed_requests, certificate_pem) =
|
||||
spawn_successful_tls_fallback_server().expect("TLS fallback server should start");
|
||||
let pool = RouteAwareClientPool::new(
|
||||
let mut client_builder = HttpClientBuilder::new();
|
||||
client_builder.tls = HttpClientTlsConfig::default()
|
||||
.with_root_certificate_pem(certificate_pem.as_bytes())
|
||||
.expect("fixture root should parse");
|
||||
let pool = RouteAwareClientPool::with_builder(
|
||||
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
|
||||
ClientRouteClass::Other,
|
||||
client_builder,
|
||||
)
|
||||
.with_tls_backend_fallback();
|
||||
use_native_tls_for_direct_route(&pool);
|
||||
@@ -236,8 +253,15 @@ async fn successful_rustls_fallback_replays_the_request_and_reuses_the_destinati
|
||||
);
|
||||
assert!(!rustls_clients.requires_rustls(&destination, &OutboundProxyRoute::Direct));
|
||||
|
||||
let mut initialize_request = reqwest::Request::new(Method::POST, destination.clone());
|
||||
*initialize_request.body_mut() = Some(Bytes::from_static(b"mcp-initialize").into());
|
||||
let (body, status) = if private_body {
|
||||
("private-initialize", http::StatusCode::TEMPORARY_REDIRECT)
|
||||
} else {
|
||||
("mcp-initialize", http::StatusCode::OK)
|
||||
};
|
||||
let mut initialize_request =
|
||||
crate::request_draft::RequestDraft::new(Method::POST, &url).expect("valid request URL");
|
||||
*initialize_request.request.body_mut() = Some(body.into());
|
||||
initialize_request.private_body = private_body;
|
||||
let initialize_response = pool
|
||||
.send_with_resolver(initialize_request, |_| async {
|
||||
Ok(OutboundProxyRoute::Direct)
|
||||
@@ -245,6 +269,7 @@ async fn successful_rustls_fallback_replays_the_request_and_reuses_the_destinati
|
||||
.await
|
||||
.expect("native TLS protocol failure should recover with rustls");
|
||||
|
||||
assert_eq!(initialize_response.status(), status);
|
||||
assert_eq!(
|
||||
initialize_response
|
||||
.text()
|
||||
@@ -273,7 +298,7 @@ async fn successful_rustls_fallback_replays_the_request_and_reuses_the_destinati
|
||||
.recv_timeout(Duration::from_secs(5))
|
||||
.expect("TLS fallback server should finish")
|
||||
.expect("TLS fallback server should capture both requests"),
|
||||
vec!["mcp-initialize".to_string(), "mcp-tool-call".to_string()]
|
||||
vec![body.to_string(), "mcp-tool-call".to_string()]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -372,6 +397,7 @@ fn spawn_successful_tls_fallback_server() -> io::Result<SuccessfulTlsFallbackSer
|
||||
codex_utils_rustls_provider::ensure_rustls_crypto_provider();
|
||||
let CertifiedKey { cert, signing_key } =
|
||||
generate_simple_self_signed(vec!["127.0.0.1".to_string()]).map_err(io::Error::other)?;
|
||||
let certificate_pem = cert.pem();
|
||||
let certificate = cert.der().clone();
|
||||
let private_key = PrivateKeyDer::from(signing_key);
|
||||
let tls_config = Arc::new(
|
||||
@@ -483,9 +509,12 @@ fn spawn_successful_tls_fallback_server() -> io::Result<SuccessfulTlsFallbackSer
|
||||
let body = std::str::from_utf8(&request[header_end..header_end + content_length])
|
||||
.map_err(io::Error::other)?;
|
||||
observed_requests.push(body.to_string());
|
||||
tls.write_all(
|
||||
b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok",
|
||||
)?;
|
||||
if body == "private-initialize" {
|
||||
tls.write_all(b"HTTP/1.1 307 Temporary Redirect\r\nLocation: /redirected\r\n")?;
|
||||
} else {
|
||||
tls.write_all(b"HTTP/1.1 200 OK\r\n")?;
|
||||
}
|
||||
tls.write_all(b"Content-Length: 2\r\nConnection: close\r\n\r\nok")?;
|
||||
tls.flush()?;
|
||||
}
|
||||
Ok(observed_requests)
|
||||
@@ -497,6 +526,7 @@ fn spawn_successful_tls_fallback_server() -> io::Result<SuccessfulTlsFallbackSer
|
||||
format!("https://{address}/mcp"),
|
||||
trusted_rustls_client,
|
||||
requests_rx,
|
||||
certificate_pem,
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ use crate::request::Request;
|
||||
use crate::request::RequestBody;
|
||||
use crate::request::Response;
|
||||
use crate::request_draft::RequestDraft;
|
||||
use crate::request_draft::prevent_body_redirects;
|
||||
use crate::retry_after::RetryAfter;
|
||||
use bytes::Bytes;
|
||||
use bytes::BytesMut;
|
||||
@@ -74,7 +75,8 @@ impl ReqwestTransport {
|
||||
}
|
||||
|
||||
async fn send(&self, req: Request) -> Result<HttpResponse, TransportError> {
|
||||
let prepared = req.prepare_body_for_send().map_err(TransportError::Build)?;
|
||||
let private = req.has_private_body();
|
||||
let prepared = req.prepare_wire_body().map_err(TransportError::Build)?;
|
||||
|
||||
let Request {
|
||||
method,
|
||||
@@ -91,6 +93,7 @@ impl ReqwestTransport {
|
||||
RequestDraft::new(method, url).map_err(|error| Self::map_error(error.into()))?;
|
||||
request.extend_headers(prepared.headers);
|
||||
*request.request.timeout_mut() = timeout;
|
||||
request.private_body = private;
|
||||
*request.request.body_mut() = prepared.body.map(Into::into);
|
||||
|
||||
match &self.client {
|
||||
@@ -100,9 +103,12 @@ impl ReqwestTransport {
|
||||
.await
|
||||
.map_err(Self::map_route_aware_error),
|
||||
HttpClientBackend::Direct(client) => {
|
||||
let request = request
|
||||
let mut request = request
|
||||
.build(client)
|
||||
.map_err(|error| Self::map_error(error.into()))?;
|
||||
if private {
|
||||
prevent_body_redirects(&mut request);
|
||||
}
|
||||
client
|
||||
.execute(request)
|
||||
.await
|
||||
|
||||
@@ -48,6 +48,7 @@ pub use executed_tool_calls::ExecutedToolCall;
|
||||
pub use executed_tool_calls::ExecutedToolCallArguments;
|
||||
pub use executed_tool_calls::ExecutedToolCallTruncation;
|
||||
pub use executed_tool_calls::MAX_TOOL_RESULT_SOURCE_FIELD_BYTES;
|
||||
pub use executed_tool_calls::ToolResultBody;
|
||||
pub use executed_tool_calls::ToolResultMetadata;
|
||||
pub use executed_tool_calls::ToolResultSource;
|
||||
pub use executed_tool_calls::ToolResultSources;
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use codex_utils_string::to_json_string_bounded;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use serde::Serializer;
|
||||
use serde_json::Value;
|
||||
use ts_rs::TS;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::InternalChatMessageMetadataPassthrough;
|
||||
use super::ResponseItem;
|
||||
use crate::mcp::CallToolResult;
|
||||
|
||||
/// Maximum distinct result sources retained for one tool invocation.
|
||||
const MAX_TOOL_RESULT_SOURCES: usize = 32;
|
||||
/// Maximum UTF-8 bytes for each source's `type` and `id` separately, not the source list.
|
||||
pub const MAX_TOOL_RESULT_SOURCE_FIELD_BYTES: usize = 128;
|
||||
const MAX_TOOL_RESULT_BYTES: usize = 1024 * 1024;
|
||||
const MAX_TOOL_RESULT_DEPTH: usize = 64;
|
||||
const RESOURCE_ACCESS_METADATA_KEY: &str = "openai/resource_access";
|
||||
const EXECUTED_TOOL_CALL_METADATA_FIELD_BYTES: usize = b"\"executed_tool_calls\":".len();
|
||||
const INTERNAL_CHAT_MESSAGE_METADATA_PASSTHROUGH_FIELD_BYTES: usize =
|
||||
@@ -223,7 +230,7 @@ fn shed_generic_result_metadata(
|
||||
if metadata.retain_resource_access() {
|
||||
continue;
|
||||
}
|
||||
**metadata = ToolResultMetadata::default();
|
||||
metadata.clear_value();
|
||||
total_metadata_bytes = total_metadata_bytes
|
||||
.saturating_sub(bytes.saturating_add(b",\"tool_result_metadata\":".len()));
|
||||
*bytes = 0;
|
||||
@@ -264,7 +271,7 @@ fn shed_remaining_result_metadata(
|
||||
if metadata.is_none() {
|
||||
continue;
|
||||
}
|
||||
*metadata = ToolResultMetadata::default();
|
||||
metadata.clear_value();
|
||||
// The required name and arguments always precede this optional serialized field.
|
||||
total_metadata_bytes = total_metadata_bytes
|
||||
.saturating_sub(bytes.saturating_add(b",\"tool_result_metadata\":".len()));
|
||||
@@ -428,9 +435,27 @@ pub struct ExecutedToolCall {
|
||||
}
|
||||
|
||||
/// MCP result metadata, optionally reduced to resource evidence, or a size-omission marker.
|
||||
#[derive(Clone, Default, Serialize, PartialEq, Eq)]
|
||||
#[serde(transparent)]
|
||||
pub struct ToolResultMetadata(Option<serde_json::Value>);
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct ToolResultMetadata(ResultObservation);
|
||||
|
||||
// Keep ordinary metadata inline. Only a result carrying both a body ID and metadata needs a box.
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
enum ResultObservation {
|
||||
Plain(Option<Value>),
|
||||
Bound { id: Uuid, value: Option<Box<Value>> },
|
||||
}
|
||||
|
||||
impl Default for ToolResultMetadata {
|
||||
fn default() -> Self {
|
||||
Self(ResultObservation::Plain(None))
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for ToolResultMetadata {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
self.value().serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for ToolResultMetadata {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
@@ -441,12 +466,61 @@ impl std::fmt::Debug for ToolResultMetadata {
|
||||
impl ToolResultMetadata {
|
||||
/// Captures the complete result; retention and outgoing-request budgets apply later.
|
||||
pub fn new(metadata: &serde_json::Value) -> Self {
|
||||
Self(Some(metadata.clone()))
|
||||
Self(ResultObservation::Plain(Some(metadata.clone())))
|
||||
}
|
||||
|
||||
pub fn with_result_id(mut self, id: Option<Uuid>) -> Self {
|
||||
self.set_result_id(id);
|
||||
self
|
||||
}
|
||||
|
||||
fn set_result_id(&mut self, id: Option<Uuid>) {
|
||||
match (&mut self.0, id) {
|
||||
(ResultObservation::Plain(value), Some(id)) => {
|
||||
self.0 = ResultObservation::Bound {
|
||||
id,
|
||||
value: value.take().map(Box::new),
|
||||
};
|
||||
}
|
||||
(ResultObservation::Bound { id: current, .. }, Some(id)) => *current = id,
|
||||
(ResultObservation::Bound { value, .. }, None) => {
|
||||
self.0 = ResultObservation::Plain(value.take().map(|value| *value));
|
||||
}
|
||||
(ResultObservation::Plain(_), None) => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn result_id(&self) -> Option<Uuid> {
|
||||
match &self.0 {
|
||||
ResultObservation::Plain(_) => None,
|
||||
ResultObservation::Bound { id, .. } => Some(*id),
|
||||
}
|
||||
}
|
||||
|
||||
fn value(&self) -> Option<&Value> {
|
||||
match &self.0 {
|
||||
ResultObservation::Plain(value) => value.as_ref(),
|
||||
ResultObservation::Bound { value, .. } => value.as_deref(),
|
||||
}
|
||||
}
|
||||
|
||||
/// A body-only observation can be recorded even when the provider supplied no `_meta`.
|
||||
pub fn has_observation(&self) -> bool {
|
||||
!matches!(self.0, ResultObservation::Plain(None))
|
||||
}
|
||||
|
||||
// Shedding the provider's serialized _meta leaves the host's per-call binding intact.
|
||||
fn clear_value(&mut self) {
|
||||
*self = Self::default().with_result_id(self.result_id());
|
||||
}
|
||||
|
||||
/// Leaves generic metadata unchanged when it has no resource-access field.
|
||||
fn retain_resource_access(&mut self) -> bool {
|
||||
let Some(serde_json::Value::Object(metadata)) = self.0.as_mut() else {
|
||||
let value = match &mut self.0 {
|
||||
ResultObservation::Plain(value) => value.as_mut(),
|
||||
ResultObservation::Bound { value, .. } => value.as_deref_mut(),
|
||||
};
|
||||
let Some(Value::Object(metadata)) = value else {
|
||||
return false;
|
||||
};
|
||||
if !metadata.contains_key(RESOURCE_ACCESS_METADATA_KEY) {
|
||||
@@ -458,13 +532,12 @@ impl ToolResultMetadata {
|
||||
|
||||
fn omitted_due_to_size_limit(overage_bytes: usize) -> Self {
|
||||
// MCP `_meta` is an object; this string is a harness omission status, not provider data.
|
||||
Self(Some(serde_json::Value::String(format!(
|
||||
"omitted_due_to_size_limit (overage_bytes={overage_bytes})"
|
||||
))))
|
||||
let marker = format!("omitted_due_to_size_limit (overage_bytes={overage_bytes})");
|
||||
Self(ResultObservation::Plain(Some(marker.into())))
|
||||
}
|
||||
|
||||
fn is_omitted_due_to_size_limit(&self) -> bool {
|
||||
let Some(serde_json::Value::String(value)) = self.0.as_ref() else {
|
||||
let Some(Value::String(value)) = self.value() else {
|
||||
return false;
|
||||
};
|
||||
value == "omitted_due_to_size_limit"
|
||||
@@ -481,7 +554,7 @@ impl ToolResultMetadata {
|
||||
let omitted = Self::omitted_due_to_size_limit(overage_bytes);
|
||||
let omitted_bytes = serde_json::to_vec(&omitted).map_or(usize::MAX, |value| value.len());
|
||||
if omitted_bytes < original_bytes {
|
||||
*self = omitted;
|
||||
*self = omitted.with_result_id(self.result_id());
|
||||
omitted_bytes
|
||||
} else {
|
||||
original_bytes
|
||||
@@ -490,12 +563,84 @@ impl ToolResultMetadata {
|
||||
|
||||
/// Whether there is no captured result metadata.
|
||||
fn is_none(&self) -> bool {
|
||||
self.0.is_none()
|
||||
self.value().is_none()
|
||||
}
|
||||
|
||||
/// Whether the captured snapshot contains metadata or an omission marker.
|
||||
pub fn is_some(&self) -> bool {
|
||||
self.0.is_some()
|
||||
self.value().is_some()
|
||||
}
|
||||
}
|
||||
|
||||
/// Projected successful Apps result, kept separately from the ordinary tool output and history.
|
||||
#[derive(derive_more::Debug)]
|
||||
#[debug("ToolResultBody([redacted])")]
|
||||
pub struct ToolResultBody(Box<str>);
|
||||
|
||||
impl ToolResultBody {
|
||||
/// Projects only plaintext and structured content that fits the receiver's limits.
|
||||
pub fn from_mcp_result(result: &CallToolResult) -> Option<Self> {
|
||||
#[derive(Serialize)]
|
||||
#[serde(tag = "type", rename = "text")]
|
||||
struct Text<'a> {
|
||||
text: &'a str,
|
||||
}
|
||||
#[derive(Serialize)]
|
||||
struct Body<'a> {
|
||||
content: Vec<Text<'a>>,
|
||||
#[serde(rename = "structuredContent", skip_serializing_if = "Option::is_none")]
|
||||
structured: Option<&'a serde_json::Value>,
|
||||
}
|
||||
|
||||
// Even empty text blocks cost this many bytes. Bound the borrowed views before collecting.
|
||||
if !result.success()
|
||||
|| result.content.len() > MAX_TOOL_RESULT_BYTES / br#"{"type":"text","text":""}"#.len()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let mut content = Vec::with_capacity(result.content.len());
|
||||
for block in &result.content {
|
||||
if block["type"] != "text" || block["_meta"]["codex/encryptedContent"] == true {
|
||||
return None;
|
||||
}
|
||||
let text = block["text"]
|
||||
.as_str()
|
||||
.filter(|text| text.len() <= MAX_TOOL_RESULT_BYTES)?;
|
||||
content.push(Text { text });
|
||||
}
|
||||
let structured = result.structured_content.as_ref();
|
||||
if structured
|
||||
.is_some_and(|v| !v.is_object() || !within_tool_result_depth(v, MAX_TOOL_RESULT_DEPTH))
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let body = Body {
|
||||
content,
|
||||
structured,
|
||||
};
|
||||
let json = to_json_string_bounded(&body, MAX_TOOL_RESULT_BYTES).ok()?;
|
||||
Some(Self(json.into_boxed_str()))
|
||||
}
|
||||
|
||||
pub fn compact_bytes(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
pub fn as_json(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
// The receiver counts the body at depth zero and structuredContent at depth one.
|
||||
fn within_tool_result_depth(value: &Value, remaining: usize) -> bool {
|
||||
let Some(remaining) = remaining.checked_sub(1) else {
|
||||
return false;
|
||||
};
|
||||
let child_fits = |child| within_tool_result_depth(child, remaining);
|
||||
match value {
|
||||
Value::Array(items) => items.iter().all(child_fits),
|
||||
Value::Object(fields) => fields.values().all(child_fits),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -571,6 +716,14 @@ impl ExecutedToolCall {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tool_result_id(&self) -> Option<Uuid> {
|
||||
self.tool_result_metadata.result_id()
|
||||
}
|
||||
|
||||
pub fn clear_tool_result_id(&mut self) {
|
||||
self.tool_result_metadata.set_result_id(/*id*/ None);
|
||||
}
|
||||
|
||||
/// Replaces oversized arguments with internally generated truncation metadata.
|
||||
pub fn truncated(name: String, original_bytes: usize, max_bytes: usize) -> Self {
|
||||
let mut call = Self::new(name, serde_json::Value::Null);
|
||||
@@ -583,9 +736,9 @@ impl ExecutedToolCall {
|
||||
&self.arguments
|
||||
}
|
||||
|
||||
/// Whether this call has result metadata or an omission marker.
|
||||
/// Whether this call already has a host-recorded result observation.
|
||||
pub fn has_tool_result_metadata(&self) -> bool {
|
||||
self.tool_result_metadata.is_some()
|
||||
self.tool_result_metadata.has_observation()
|
||||
}
|
||||
|
||||
/// Replaces this invocation's capture outcome, including clearing omitted evidence.
|
||||
@@ -708,7 +861,7 @@ impl ResponseItem {
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.enumerate()
|
||||
.filter(|(_, call)| call.tool_result_metadata.is_some())
|
||||
.filter(|(_, call)| call.tool_result_metadata.has_observation())
|
||||
.map(|(index, call)| {
|
||||
(
|
||||
index,
|
||||
@@ -733,14 +886,27 @@ impl ResponseItem {
|
||||
})
|
||||
}
|
||||
|
||||
/// Omits raw tool results without changing existing call, source or completion metadata.
|
||||
/// Removes local body bindings while retaining recorded arguments and provider metadata.
|
||||
pub fn clear_tool_result_ids(&mut self) {
|
||||
if let Some(calls) = self
|
||||
.internal_chat_message_metadata_passthrough_mut()
|
||||
.and_then(Option::as_mut)
|
||||
.and_then(|metadata| metadata.executed_tool_calls.as_mut())
|
||||
{
|
||||
for call in calls {
|
||||
call.clear_tool_result_id();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Clears provider metadata while retaining local body bindings and the call inventory.
|
||||
pub fn clear_tool_result_metadata(&mut self) {
|
||||
if let Some(metadata) = self
|
||||
.internal_chat_message_metadata_passthrough_mut()
|
||||
.and_then(Option::as_mut)
|
||||
{
|
||||
for call in metadata.executed_tool_calls.iter_mut().flatten() {
|
||||
call.tool_result_metadata = ToolResultMetadata::default();
|
||||
call.tool_result_metadata.clear_value();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -783,7 +949,7 @@ impl ResponseItem {
|
||||
{
|
||||
for call in metadata.executed_tool_calls.iter_mut().flatten() {
|
||||
if !call.tool_result_metadata.retain_resource_access() {
|
||||
call.tool_result_metadata = ToolResultMetadata::default();
|
||||
call.tool_result_metadata.clear_value();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use anyhow::Result;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
|
||||
use super::super::FunctionCallOutputPayload;
|
||||
use super::super::ResponseInputItem;
|
||||
@@ -36,6 +37,15 @@ fn result_metadata_comparison_tracks_raw_values_and_call_bindings() {
|
||||
)]);
|
||||
assert!(empty.has_same_tool_result_metadata(&without_metadata));
|
||||
|
||||
let mut body_only = without_metadata.clone();
|
||||
let binding = ToolResultMetadata::default().with_result_id(Some(Uuid::new_v4()));
|
||||
first_executed_tool_call(&mut body_only)
|
||||
.unwrap()
|
||||
.set_tool_result_metadata(binding);
|
||||
assert!(!without_metadata.has_same_tool_result_metadata(&body_only));
|
||||
body_only.clear_tool_result_ids();
|
||||
assert_eq!(body_only, without_metadata);
|
||||
|
||||
let mut with_metadata = without_metadata.clone();
|
||||
first_executed_tool_call(&mut with_metadata)
|
||||
.unwrap()
|
||||
@@ -309,11 +319,13 @@ fn tool_result_metadata_is_host_only_and_redacted_without_a_per_result_limit() -
|
||||
serde_json::json!({}),
|
||||
serde_json::json!(null),
|
||||
] {
|
||||
let capture = ToolResultMetadata::new(&metadata);
|
||||
let id = Uuid::new_v4();
|
||||
let capture = ToolResultMetadata::new(&metadata).with_result_id(Some(id));
|
||||
assert_eq!(format!("{capture:?}"), "ToolResultMetadata([redacted])");
|
||||
assert!(capture.is_some());
|
||||
call.set_tool_result_metadata(capture);
|
||||
let wire = serde_json::to_value(&call)?;
|
||||
assert_eq!(call.clone().tool_result_id(), Some(id));
|
||||
let mut wire = serde_json::to_value(&call)?;
|
||||
assert_eq!(
|
||||
wire,
|
||||
serde_json::json!({
|
||||
@@ -323,6 +335,8 @@ fn tool_result_metadata_is_host_only_and_redacted_without_a_per_result_limit() -
|
||||
})
|
||||
);
|
||||
assert!(!format!("{call:?}").contains("not-for-debug"));
|
||||
wire["tool_result_id"] = serde_json::json!(id);
|
||||
wire["tool_result"] = serde_json::json!({"content": []});
|
||||
assert_eq!(
|
||||
serde_json::from_value::<ExecutedToolCall>(wire)?,
|
||||
without_metadata
|
||||
@@ -331,8 +345,61 @@ fn tool_result_metadata_is_host_only_and_redacted_without_a_per_result_limit() -
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn projected_tool_result_preserves_only_eligible_fields_within_receiver_limits() -> Result<()> {
|
||||
let mut nested = serde_json::Value::Null;
|
||||
for _ in 2..MAX_TOOL_RESULT_DEPTH {
|
||||
nested = serde_json::Value::Array(vec![nested]);
|
||||
}
|
||||
let expected = json!({
|
||||
"content": [{"type": "text", "text": "private é\n"}, {"type": "text", "text": ""}],
|
||||
"structuredContent": {"nested": nested, "padding": ""},
|
||||
});
|
||||
let mut result: CallToolResult = serde_json::from_value(expected.clone())?;
|
||||
result.meta = Some(json!({"provider": 1}));
|
||||
result.content[0]["annotations"] = json!({});
|
||||
let body = ToolResultBody::from_mcp_result(&result).unwrap();
|
||||
assert_eq!(serde_json::from_str::<Value>(body.as_json())?, expected);
|
||||
assert!(!format!("{body:?}").contains("private"));
|
||||
|
||||
result.structured_content.as_mut().unwrap()["padding"] =
|
||||
json!("x".repeat(MAX_TOOL_RESULT_BYTES - body.compact_bytes()));
|
||||
let body = ToolResultBody::from_mcp_result(&result).unwrap();
|
||||
assert_eq!(body.as_json().len(), MAX_TOOL_RESULT_BYTES);
|
||||
result.content[1]["text"] = json!("!");
|
||||
assert!(ToolResultBody::from_mcp_result(&result).is_none());
|
||||
|
||||
result.content.clear();
|
||||
assert!(ToolResultBody::from_mcp_result(&result).is_some());
|
||||
result.structured_content = Some(json!({"extra": expected["structuredContent"]}));
|
||||
assert!(ToolResultBody::from_mcp_result(&result).is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn projected_tool_result_rejects_ineligible_results() -> Result<()> {
|
||||
let text = json!({"type": "text", "text": "first"});
|
||||
let encrypted =
|
||||
json!({"type": "text", "text": "cipher", "_meta": {"codex/encryptedContent": true}});
|
||||
for value in [
|
||||
json!({"content": [text], "isError": true}),
|
||||
json!({"content": [text], "structuredContent": []}),
|
||||
json!({"content": [text, {"type": "image", "mimeType": "image/png", "data": "AQID"}]}),
|
||||
json!({"content": [text, encrypted]}),
|
||||
json!({"content": [text, {"type": "text"}]}),
|
||||
] {
|
||||
let result = serde_json::from_value(value)?;
|
||||
assert!(
|
||||
ToolResultBody::from_mcp_result(&result).is_none(),
|
||||
"accepted {result:?}"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wire_budget_helpers_preserve_resource_evidence_markers_and_small_values() {
|
||||
let result_id = Some(Uuid::new_v4());
|
||||
let resource_only = serde_json::json!({
|
||||
"openai/resource_access": { "resource_coverage": "complete", "resources": [] },
|
||||
});
|
||||
@@ -343,7 +410,8 @@ fn wire_budget_helpers_preserve_resource_evidence_markers_and_small_values() {
|
||||
ToolResultMetadata::new(&serde_json::json!({
|
||||
"openai/resource_access": resource_only["openai/resource_access"],
|
||||
"payload": "x".repeat(512),
|
||||
})),
|
||||
}))
|
||||
.with_result_id(result_id),
|
||||
ToolResultMetadata::new(&serde_json::json!({
|
||||
"omitted_due_to_size_limit": { "overage_bytes": 1 },
|
||||
"payload": "x".repeat(512),
|
||||
@@ -372,7 +440,9 @@ fn wire_budget_helpers_preserve_resource_evidence_markers_and_small_values() {
|
||||
.executed_tool_calls
|
||||
.as_mut()
|
||||
.unwrap();
|
||||
calls[0].set_tool_result_metadata(ToolResultMetadata::new(&resource_only));
|
||||
calls[0].set_tool_result_metadata(
|
||||
ToolResultMetadata::new(&resource_only).with_result_id(result_id),
|
||||
);
|
||||
calls[1].set_tool_result_metadata(ToolResultMetadata::omitted_due_to_size_limit(
|
||||
/*overage_bytes*/ 77,
|
||||
));
|
||||
@@ -381,17 +451,18 @@ fn wire_budget_helpers_preserve_resource_evidence_markers_and_small_values() {
|
||||
item.retain_tool_resource_access_or_omit_metadata(/*overage_bytes*/ 1_234);
|
||||
assert_eq!(item, expected);
|
||||
|
||||
let omitted = ToolResultMetadata::omitted_due_to_size_limit(/*overage_bytes*/ 1);
|
||||
first_executed_tool_call(&mut expected)
|
||||
.unwrap()
|
||||
.set_tool_result_metadata(ToolResultMetadata::omitted_due_to_size_limit(
|
||||
/*overage_bytes*/ 1,
|
||||
));
|
||||
.set_tool_result_metadata(omitted.with_result_id(result_id));
|
||||
item.omit_tool_result_metadata(/*overage_bytes*/ 1);
|
||||
assert_eq!(item, expected);
|
||||
item.omit_tool_result_metadata(/*overage_bytes*/ 99_999);
|
||||
assert_eq!(item, expected);
|
||||
item.clear_tool_result_metadata();
|
||||
assert!(!item.has_tool_result_metadata());
|
||||
let retained = first_executed_tool_call(&mut item).unwrap();
|
||||
assert_eq!(retained.tool_result_id(), result_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user