Share truncation logic for environment startup errors (#63077)

## What changed

Use `ErrorContextBudget` to truncate model-visible startup error details in
environment world state and `wait_for_environment` responses. Keep the
existing 256-byte limit per detail and 512-byte total for world state error
details, with truncation at UTF-8 character boundaries.

## Testing

Extend the world state test to cover a multibyte error that cannot fit in
the remaining budget, followed by an ASCII error that uses the last two bytes.

GitOrigin-RevId: a6296fc854a383186dacaec5576e9f02e179d34c
This commit is contained in:
Adam Perry @ OpenAI
2026-11-22 02:54:25 +00:00
committed by copyberry
parent 6fd58416e3
commit 357fe240c9
4 changed files with 42 additions and 16 deletions
+29 -1
View File
@@ -1,4 +1,4 @@
//! Shared byte/token truncation for tool and exec output.
//! Shared byte/token truncation for model-visible error details, tool output, and exec output.
use std::borrow::Cow;
@@ -13,10 +13,38 @@ pub use codex_utils_string::approx_bytes_for_tokens;
pub use codex_utils_string::approx_token_count;
pub use codex_utils_string::approx_tokens_from_byte_count;
use codex_utils_string::plan_token_truncation;
use codex_utils_string::take_bytes_at_char_boundary;
use codex_utils_string::truncate_middle_chars;
pub use codex_protocol::protocol::TruncationPolicy;
/// Bounds error details in a single context fragment or tool result.
/// Limits apply to the raw UTF-8 text before escaping or adding markup.
pub struct ErrorContextBudget {
remaining_bytes: usize,
}
impl ErrorContextBudget {
const MAX_DETAIL_BYTES: usize = 256;
const MAX_TOTAL_BYTES: usize = 512;
/// Charge only the returned prefix so later errors can use any leftover bytes.
pub fn take<'a>(&mut self, error: &'a str) -> &'a str {
let prefix =
take_bytes_at_char_boundary(error, self.remaining_bytes.min(Self::MAX_DETAIL_BYTES));
self.remaining_bytes -= prefix.len();
prefix
}
}
impl Default for ErrorContextBudget {
fn default() -> Self {
Self {
remaining_bytes: Self::MAX_TOTAL_BYTES,
}
}
}
/// Adds the existing 20% allowance for serialization and headers.
/// Saved history budgets already include this allowance.
pub fn with_serialization_allowance(policy: TruncationPolicy) -> TruncationPolicy {