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
@@ -18,6 +18,7 @@ use codex_protocol::models::ContentItem;
use codex_protocol::models::ContentItemKind;
use codex_protocol::models::input_text;
use codex_protocol::protocol::NetworkAccess;
use codex_utils_output_truncation::ErrorContextBudget;
use codex_utils_path_uri::PathUri;
use serde::Deserialize;
use serde::Serialize;
@@ -594,26 +595,20 @@ fn environment_states(
network_access: None,
});
}
// Bound the additional model context even when many selected environments fail.
const MAX_ERROR_BYTES: usize = 256;
const MAX_TOTAL_ERROR_BYTES: usize = 512;
let mut remaining_error_bytes = MAX_TOTAL_ERROR_BYTES;
let mut error_budget = ErrorContextBudget::default();
for environment in &snapshot.environments {
if let TurnEnvironmentState::Failed {
selection, error, ..
} = environment
{
let detail = error
[..error.floor_char_boundary(remaining_error_bytes.min(MAX_ERROR_BYTES))]
.to_string();
remaining_error_bytes -= detail.len();
let detail = error_budget.take(error);
environments.insert(
selection.environment_id.clone(),
EnvironmentState {
cwd: selection.cwd.clone(),
status: EnvironmentStatus::Failed,
shell: None,
error: (!detail.is_empty()).then_some(detail),
error: (!detail.is_empty()).then(|| detail.to_owned()),
is_primary: false,
filesystem: None,
network_access: None,
@@ -467,9 +467,12 @@ fn failure_context_is_escaped_incremental_and_cleared_on_recovery() -> Result<()
fn failure_context_limits_total_detail_bytes_at_utf8_boundaries() {
use codex_protocol::protocol::EnvironmentConfigState;
use codex_protocol::protocol::TurnEnvironmentSelection;
let long_error = "界".repeat(300);
let snapshot = TurnEnvironmentSnapshot {
environments: (0..4)
.map(|index| TurnEnvironmentState::Failed {
environments: [long_error.as_str(), &long_error, &long_error, "abc", "d"]
.into_iter()
.enumerate()
.map(|(index, error)| TurnEnvironmentState::Failed {
required_skills: Vec::new(),
selection: TurnEnvironmentSelection {
selected_capability_roots: Default::default(),
@@ -478,7 +481,7 @@ fn failure_context_limits_total_detail_bytes_at_utf8_boundaries() {
workspace_roots: Vec::new(),
config: EnvironmentConfigState::FromThread,
},
error: "界".repeat(300),
error: error.to_owned(),
})
.collect(),
};
@@ -494,6 +497,7 @@ fn failure_context_limits_total_detail_bytes_at_utf8_boundaries() {
Some(truncated.as_str()),
Some(truncated.as_str()),
None,
Some("ab"),
None
]
);
@@ -3,6 +3,7 @@ use codex_tools::JsonToolOutput;
use codex_tools::ResponsesApiTool;
use codex_tools::ToolName;
use codex_tools::ToolSpec;
use codex_utils_output_truncation::ErrorContextBudget;
use serde::Deserialize;
use serde_json::json;
use std::collections::BTreeMap;
@@ -170,10 +171,8 @@ impl ToolExecutor<ToolInvocation> for WaitForEnvironmentHandler {
impl CoreToolRuntime for WaitForEnvironmentHandler {}
// Keep failure output small while preserving a UTF-8 boundary.
fn environment_failure(environment_id: &str, error: &str) -> FunctionCallError {
const MAX_ERROR_BYTES: usize = 256;
let reason = &error[..error.floor_char_boundary(MAX_ERROR_BYTES)];
let reason = ErrorContextBudget::default().take(error);
FunctionCallError::RespondToModel(format!(
"Environment `{environment_id}` failed to start: {reason}"
))