Skip to content

Commit d62cab9

Browse files
authored
fix: don't truncate at new lines (#6907)
1 parent d5dfba2 commit d62cab9

File tree

5 files changed

+8
-18
lines changed

5 files changed

+8
-18
lines changed

codex-rs/core/src/truncate.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,22 +282,11 @@ fn truncate_on_boundary(input: &str, max_len: usize) -> &str {
282282
}
283283

284284
fn pick_prefix_end(s: &str, left_budget: usize) -> usize {
285-
if let Some(head) = s.get(..left_budget)
286-
&& let Some(i) = head.rfind('\n')
287-
{
288-
return i + 1;
289-
}
290285
truncate_on_boundary(s, left_budget).len()
291286
}
292287

293288
fn pick_suffix_start(s: &str, right_budget: usize) -> usize {
294289
let start_tail = s.len().saturating_sub(right_budget);
295-
if let Some(tail) = s.get(start_tail..)
296-
&& let Some(i) = tail.find('\n')
297-
{
298-
return start_tail + i + 1;
299-
}
300-
301290
let mut idx = start_tail.min(s.len());
302291
while idx < s.len() && !s.is_char_boundary(idx) {
303292
idx += 1;
@@ -420,15 +409,15 @@ mod tests {
420409
fn truncate_middle_tokens_handles_utf8_content() {
421410
let s = "😀😀😀😀😀😀😀😀😀😀\nsecond line with text\n";
422411
let (out, tokens) = truncate_with_token_budget(s, TruncationPolicy::Tokens(8));
423-
assert_eq!(out, "😀😀😀😀…8 tokens truncated…");
412+
assert_eq!(out, "😀😀😀😀…8 tokens truncated… line with text\n");
424413
assert_eq!(tokens, Some(16));
425414
}
426415

427416
#[test]
428417
fn truncate_middle_bytes_handles_utf8_content() {
429418
let s = "😀😀😀😀😀😀😀😀😀😀\nsecond line with text\n";
430419
let out = truncate_text(s, TruncationPolicy::Bytes(20));
431-
assert_eq!(out, "😀😀…31 chars truncated…");
420+
assert_eq!(out, "😀😀…21 chars truncated…with text\n");
432421
}
433422

434423
#[test]

codex-rs/core/tests/suite/shell_serialization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ Output:
457457
4
458458
5
459459
6
460-
.*…45 tokens truncated….*
460+
.*…46 tokens truncated….*
461461
396
462462
397
463463
398

codex-rs/core/tests/suite/truncation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ async fn tool_call_output_configured_limit_chars_type() -> Result<()> {
167167
"expected truncated shell output to be plain text"
168168
);
169169

170-
assert_eq!(output.len(), 400094, "we should be almost 100k tokens");
170+
assert_eq!(output.len(), 400097, "we should be almost 100k tokens");
171171

172172
assert!(
173173
!output.contains("tokens truncated"),
@@ -245,7 +245,7 @@ async fn tool_call_output_exceeds_limit_truncated_chars_limit() -> Result<()> {
245245
);
246246

247247
assert_eq!(output.len(), 9976); // ~10k characters
248-
let truncated_pattern = r#"(?s)^Exit code: 0\nWall time: 0 seconds\nTotal output lines: 100000\n.*?…578898 chars truncated….*$"#;
248+
let truncated_pattern = r#"(?s)^Exit code: 0\nWall time: 0 seconds\nTotal output lines: 100000\nOutput:\n.*?…\d+ chars truncated….*$"#;
249249

250250
assert_regex_match(truncated_pattern, &output);
251251

codex-rs/core/tests/suite/unified_exec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,7 @@ PY
15861586
let large_output = outputs.get(call_id).expect("missing large output summary");
15871587

15881588
let output_text = large_output.output.replace("\r\n", "\n");
1589-
let truncated_pattern = r"(?s)^Total output lines: \d+\n\n(token token \n){5,}.*…\d+ tokens truncated…(token token \n){5,}$";
1589+
let truncated_pattern = r"(?s)^Total output lines: \d+\n\n(token token \n){5,}.*…\d+ tokens truncated….*(token token \n){5,}$";
15901590
assert_regex_match(truncated_pattern, &output_text);
15911591

15921592
let original_tokens = large_output

codex-rs/core/tests/suite/user_shell_cmd.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ async fn user_shell_command_output_is_truncated_in_history() -> anyhow::Result<(
257257

258258
let head = (1..=69).map(|i| format!("{i}\n")).collect::<String>();
259259
let tail = (352..=400).map(|i| format!("{i}\n")).collect::<String>();
260-
let truncated_body = format!("Total output lines: 400\n\n{head}…273 tokens truncated…{tail}");
260+
let truncated_body =
261+
format!("Total output lines: 400\n\n{head}70…273 tokens truncated…351\n{tail}");
261262
let escaped_command = escape(&command);
262263
let escaped_truncated_body = escape(&truncated_body);
263264
let expected_pattern = format!(

0 commit comments

Comments
 (0)