Deduplicate large tool output strings in rollout traces (#62407)

## Why

Repeated tool output, such as the same image returned in several results,
can consume the trace bundle budget. When output bytes are unavailable,
the surrounding result can still carry terminal metadata and conversation
links.

## What changed

- Store JSON string values of at least 64 KiB, measured after encoding,
  separately from their tool result or runtime event. Identical encoded
  values share one record per bundle, while each occurrence retains its
  position and complete captures reconstruct the original JSON bytes.
- Keep separate compression shards open for content and surrounding
  records. Budget for the serialized event and fall back to a single
  capped or omitted payload when the split layout or its references
  exceed the applicable limit.
- Track missing values separately from literal `null` so reduction can
  preserve surviving terminal metadata and keep tool output IDs attached
  to their original items.

## Testing

Add regression tests for shared image and text payloads, byte-for-byte
reconstruction, recording completion under tight bundle limits, and
reduction with missing output.

GitOrigin-RevId: a003197ea998639b6c2ae27f408990c0186406bd
This commit is contained in:
cassirer-openai
2026-11-19 02:41:02 +00:00
committed by copyberry
parent 9274511443
commit 508a429666
18 changed files with 1072 additions and 259 deletions
+3 -28
View File
@@ -103,21 +103,6 @@ struct DispatchedToolTraceRequest<'a> {
payload: &'a JsonValue,
}
/// Raw response payload for dispatch-level tool trace events.
#[derive(Serialize)]
#[serde(rename_all = "snake_case", tag = "type")]
enum DispatchedToolTraceResponse<'a> {
DirectResponse {
response_item: &'a ResponseInputItem,
},
CodeModeResponse {
value: &'a JsonValue,
},
Error {
error: String,
},
}
impl ToolDispatchTraceContext {
/// Builds a context that accepts trace calls and records nothing.
pub(crate) fn disabled() -> Self {
@@ -153,15 +138,7 @@ impl ToolDispatchTraceContext {
let Some(context) = self.active.take() else {
return;
};
let response = match &result {
ToolDispatchResult::DirectResponse { response_item } => {
DispatchedToolTraceResponse::DirectResponse { response_item }
}
ToolDispatchResult::CodeModeResponse { value } => {
DispatchedToolTraceResponse::CodeModeResponse { value }
}
};
append_tool_call_ended(&context, status, &response);
append_tool_call_ended(&context, status, &result);
}
/// Records a dispatch failure before the tool produced a normal result payload.
@@ -172,9 +149,7 @@ impl ToolDispatchTraceContext {
append_tool_call_ended(
&context,
ExecutionStatus::Failed,
&DispatchedToolTraceResponse::Error {
error: error.to_string(),
},
&json!({"type": "error", "error": error.to_string()}),
);
}
}
@@ -344,7 +319,7 @@ fn truncate_preview(value: &str) -> String {
fn append_tool_call_ended(
context: &EnabledToolDispatchTraceContext,
status: ExecutionStatus,
response: &DispatchedToolTraceResponse<'_>,
response: &impl Serialize,
) {
let tool_call_id = context.tool_call_id.clone();
let _ = context.writer.event_with_payload(