Avoid temporary JSON buffers when sizing executed tool call metadata (#62889)

## Why

The metadata budget check only needs the serialized byte count.
Using `serde_json::to_vec` allocated a buffer for JSON that was
immediately discarded.

## What changed

Use `serialized_json_bytes` to count the JSON bytes for the executed tool
call list and its accompanying metadata fields. Keep the existing wire
size accounting and serialization error fallback.

GitOrigin-RevId: 1767507eeb52677439e7fce1cbaa24dbec913024
This commit is contained in:
Sebastian Ingino
2026-11-21 03:27:56 +00:00
committed by copyberry
parent 8a1a476659
commit 63a15005de
@@ -1,5 +1,6 @@
use std::collections::HashSet;
use codex_utils_string::serialized_json_bytes;
use codex_utils_string::to_json_string_bounded;
use schemars::JsonSchema;
use serde::Deserialize;
@@ -32,8 +33,7 @@ fn executed_tool_call_metadata_field_bytes(
tool_calls_complete: metadata.tool_calls_complete,
..Default::default()
};
let mut bytes =
serde_json::to_vec(&fields).map_or(usize::MAX, |fields| fields.len().saturating_sub(2));
let mut bytes = serialized_json_bytes(&fields).map_or(usize::MAX, |len| len.saturating_sub(2));
if metadata.executed_tool_calls.is_some() {
bytes = bytes
.saturating_add(usize::from(bytes > 0))
@@ -61,9 +61,7 @@ pub fn executed_tool_call_metadata_bytes(item: &ResponseItem) -> usize {
.executed_tool_calls
.as_ref()
.map_or(0, |calls| {
serde_json::to_vec(calls)
.map(|calls| calls.len())
.unwrap_or(usize::MAX)
serialized_json_bytes(calls).unwrap_or(usize::MAX)
})
.saturating_add(executed_tool_call_metadata_field_bytes(metadata))
}