Skip to content

Commit d63bd59

Browse files
committed
Bound persisted rollout tool output size
1 parent dca105c commit d63bd59

2 files changed

Lines changed: 134 additions & 4 deletions

File tree

‎codex-rs/rollout/src/recorder.rs‎

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ use crate::default_client::originator;
5252
use crate::state_db;
5353
use crate::state_db::StateDbHandle;
5454
use codex_git_utils::collect_git_info;
55+
use codex_protocol::models::FunctionCallOutputBody;
56+
use codex_protocol::models::FunctionCallOutputContentItem;
57+
use codex_protocol::models::FunctionCallOutputPayload;
58+
use codex_protocol::models::ResponseItem;
5559
use codex_protocol::protocol::EventMsg;
5660
use codex_protocol::protocol::GitInfo as ProtocolGitInfo;
5761
use codex_protocol::protocol::InitialHistory;
@@ -187,17 +191,20 @@ impl RolloutRecorderParams {
187191
}
188192

189193
const PERSISTED_EXEC_AGGREGATED_OUTPUT_MAX_BYTES: usize = 10_000;
194+
const PERSISTED_RESPONSE_OUTPUT_MAX_BYTES: usize = 64 * 1024;
190195

191196
fn sanitize_rollout_item_for_persistence(
192197
item: RolloutItem,
193198
mode: EventPersistenceMode,
194199
) -> RolloutItem {
195-
if mode != EventPersistenceMode::Extended {
196-
return item;
197-
}
198-
199200
match item {
201+
RolloutItem::ResponseItem(item) => {
202+
RolloutItem::ResponseItem(sanitize_response_item_for_persistence(item))
203+
}
200204
RolloutItem::EventMsg(EventMsg::ExecCommandEnd(mut event)) => {
205+
if mode != EventPersistenceMode::Extended {
206+
return RolloutItem::EventMsg(EventMsg::ExecCommandEnd(event));
207+
}
201208
// Persist only a bounded aggregated summary of command output.
202209
event.aggregated_output = truncate_middle_chars(
203210
&event.aggregated_output,
@@ -213,6 +220,52 @@ fn sanitize_rollout_item_for_persistence(
213220
}
214221
}
215222

223+
fn sanitize_response_item_for_persistence(item: ResponseItem) -> ResponseItem {
224+
match item {
225+
ResponseItem::FunctionCallOutput {
226+
call_id,
227+
mut output,
228+
} => {
229+
truncate_function_call_output_payload(&mut output);
230+
ResponseItem::FunctionCallOutput { call_id, output }
231+
}
232+
ResponseItem::CustomToolCallOutput {
233+
call_id,
234+
name,
235+
mut output,
236+
} => {
237+
truncate_function_call_output_payload(&mut output);
238+
ResponseItem::CustomToolCallOutput {
239+
call_id,
240+
name,
241+
output,
242+
}
243+
}
244+
other => other,
245+
}
246+
}
247+
248+
fn truncate_function_call_output_payload(output: &mut FunctionCallOutputPayload) {
249+
match &mut output.body {
250+
FunctionCallOutputBody::Text(text) => {
251+
truncate_text_for_persisted_response_output(text);
252+
}
253+
FunctionCallOutputBody::ContentItems(items) => {
254+
for item in items {
255+
if let FunctionCallOutputContentItem::InputText { text } = item {
256+
truncate_text_for_persisted_response_output(text);
257+
}
258+
}
259+
}
260+
}
261+
}
262+
263+
fn truncate_text_for_persisted_response_output(text: &mut String) {
264+
if text.len() > PERSISTED_RESPONSE_OUTPUT_MAX_BYTES {
265+
*text = truncate_middle_chars(text, PERSISTED_RESPONSE_OUTPUT_MAX_BYTES);
266+
}
267+
}
268+
216269
#[derive(Clone, Copy)]
217270
enum ThreadListArchiveFilter {
218271
Active,

‎codex-rs/rollout/src/recorder_tests.rs‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ use crate::config::RolloutConfig;
55
use chrono::TimeZone;
66
use codex_protocol::ThreadId;
77
use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig;
8+
use codex_protocol::models::FunctionCallOutputBody;
9+
use codex_protocol::models::FunctionCallOutputContentItem;
10+
use codex_protocol::models::FunctionCallOutputPayload;
811
use codex_protocol::models::ResponseItem;
912
use codex_protocol::protocol::AgentMessageEvent;
1013
use codex_protocol::protocol::AskForApproval;
@@ -69,6 +72,80 @@ fn write_session_file(root: &Path, ts: &str, uuid: Uuid) -> std::io::Result<Path
6972
Ok(path)
7073
}
7174

75+
#[test]
76+
fn sanitize_rollout_item_truncates_large_function_call_output() {
77+
let large_output = format!("{}{}{}", "a".repeat(80_000), "middle", "z".repeat(80_000));
78+
let item = RolloutItem::ResponseItem(ResponseItem::FunctionCallOutput {
79+
call_id: "call_1".to_string(),
80+
output: FunctionCallOutputPayload::from_text(large_output.clone()),
81+
});
82+
83+
let sanitized = sanitize_rollout_item_for_persistence(item, EventPersistenceMode::Limited);
84+
85+
let RolloutItem::ResponseItem(ResponseItem::FunctionCallOutput { output, .. }) = sanitized
86+
else {
87+
panic!("expected function_call_output");
88+
};
89+
let FunctionCallOutputBody::Text(text) = output.body else {
90+
panic!("expected text output");
91+
};
92+
assert!(
93+
text.len() < large_output.len(),
94+
"persisted function output should be truncated"
95+
);
96+
assert!(
97+
text.contains("truncated"),
98+
"truncated output should carry an explicit marker"
99+
);
100+
assert!(
101+
text.starts_with('a') && text.ends_with('z'),
102+
"truncated output should preserve prefix and suffix"
103+
);
104+
}
105+
106+
#[test]
107+
fn sanitize_rollout_item_truncates_structured_text_function_output() {
108+
let large_text = format!("{}{}", "prefix ".repeat(20_000), "suffix");
109+
let item = RolloutItem::ResponseItem(ResponseItem::CustomToolCallOutput {
110+
call_id: "call_2".to_string(),
111+
name: Some("custom".to_string()),
112+
output: FunctionCallOutputPayload::from_content_items(vec![
113+
FunctionCallOutputContentItem::InputText {
114+
text: large_text.clone(),
115+
},
116+
FunctionCallOutputContentItem::InputImage {
117+
image_url: "data:image/png;base64,abc".to_string(),
118+
detail: None,
119+
},
120+
]),
121+
});
122+
123+
let sanitized = sanitize_rollout_item_for_persistence(item, EventPersistenceMode::Limited);
124+
125+
let RolloutItem::ResponseItem(ResponseItem::CustomToolCallOutput { output, .. }) = sanitized
126+
else {
127+
panic!("expected custom_tool_call_output");
128+
};
129+
let FunctionCallOutputBody::ContentItems(items) = output.body else {
130+
panic!("expected structured output");
131+
};
132+
let FunctionCallOutputContentItem::InputText { text } = &items[0] else {
133+
panic!("expected leading text item");
134+
};
135+
assert!(
136+
text.len() < large_text.len(),
137+
"persisted structured text output should be truncated"
138+
);
139+
assert!(
140+
text.contains("truncated"),
141+
"truncated structured text should carry an explicit marker"
142+
);
143+
assert!(
144+
matches!(items[1], FunctionCallOutputContentItem::InputImage { .. }),
145+
"non-text content items should be preserved"
146+
);
147+
}
148+
72149
#[tokio::test]
73150
async fn state_db_init_backfills_before_returning() -> anyhow::Result<()> {
74151
let home = TempDir::new().expect("temp dir");

0 commit comments

Comments
 (0)