Surface environment startup failure reasons to the model (#44277)

## Why

Failed environments were omitted from environment context, and `wait_for_environment` returned a generic failure or treated an already-failed environment as unselected. The model needs the failure reason to explain problems such as an empty repository.

## What changed

- Preserve the selected environment and error when startup or configuration fails.
- Include failed status and XML-escaped error details in environment context, and return the reason from `wait_for_environment` for failures detected before or during a wait.
- Limit error details to 256 bytes per environment and 512 bytes total in context, preserving UTF-8 boundaries. Limit wait failure details to 256 bytes.

## Testing

Add coverage for failures before a turn and while waiting, verifying that the model receives the reason and the turn completes. Add unit tests for XML escaping, incremental context updates, clearing errors on recovery, and UTF-8-safe truncation.

GitOrigin-RevId: 093ab9b447c7b3243fbb53b665180db95479f2a7
This commit is contained in:
Rasmus Rygaard
2026-09-09 18:50:32 +00:00
committed by copyberry
parent f5c5d9b2b0
commit 9caddc5cf5
7 changed files with 287 additions and 16 deletions
@@ -7,6 +7,7 @@ use serde::Deserialize;
use serde_json::json;
use std::collections::BTreeMap;
use crate::environment_selection::TurnEnvironmentState;
use crate::function_tool::FunctionCallError;
use crate::tools::context::ToolInvocation;
use crate::tools::context::ToolPayload;
@@ -128,6 +129,22 @@ impl ToolExecutor<ToolInvocation> for WaitForEnvironmentHandler {
.turn_environments()
.any(|environment| environment.selection.environment_id == environment_id);
if !already_ready {
if let Some(error) =
step_context
.environments
.environments
.iter()
.find_map(|state| match state {
TurnEnvironmentState::Failed { selection, error }
if selection.environment_id == environment_id =>
{
Some(error)
}
_ => None,
})
{
return Err(environment_failure(&environment_id, error));
}
let Some(environment) = step_context
.environments
.starting()
@@ -139,11 +156,10 @@ impl ToolExecutor<ToolInvocation> for WaitForEnvironmentHandler {
)));
};
environment.wait_until_ready().await.map_err(|_| {
FunctionCallError::RespondToModel(format!(
"Environment `{environment_id}` failed to start and is unavailable. Continue without it."
))
})?;
environment
.wait_until_ready()
.await
.map_err(|error| environment_failure(&environment_id, &error.to_string()))?;
}
Ok(boxed_tool_output(JsonToolOutput::new(json!({
@@ -155,3 +171,12 @@ 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)];
FunctionCallError::RespondToModel(format!(
"Environment `{environment_id}` failed to start: {reason}"
))
}