Keep active-turn environments stable across settings updates (#33658)

## Why

With deferred execution enabled, rebuilding a step context from the latest
thread environment snapshot could apply a settings update to a turn that was
already in progress.

## What changed

- Preserve each turn's captured environment selections when creating later
  step contexts.
- Refresh only the readiness of environments that were already starting, while
  retaining their original selection order.
- Apply updated environment selections to the next turn.

## Testing

Add coverage for changing the selected workspace while a turn is paused,
verifying that the active turn keeps its original working directory and the
next turn uses the new one.

GitOrigin-RevId: c6f4388c843384ef045b7abdbc303dcfca11d230
This commit is contained in:
sayan-oai authored and copyberry committed 2026-07-16 18:23:43 +00:00
1 parent b7983c2a07
commit c4ce0493dc
22 files changed
+463 -182

No files matched your search

@@ -300,7 +300,13 @@ pub async fn handle(
}
fn single_local_environment_cwd(turn: &TurnContext) -> Result<AbsolutePathBuf, FunctionCallError> {
let [turn_environment] = turn.environments.turn_environments.as_slice() else {
let mut environments = turn.environments.turn_environments();
let Some(turn_environment) = environments.next() else {
return Err(FunctionCallError::RespondToModel(
"spawn_agents_on_csv requires exactly one local environment".to_string(),
));
};
if environments.next().is_some() {
return Err(FunctionCallError::RespondToModel(
"spawn_agents_on_csv requires exactly one local environment".to_string(),
));
@@ -124,9 +124,8 @@ async fn to_extension_call(invocation: &ToolInvocation) -> ExtensionToolCall {
reasoning_effort: invocation.turn.effective_reasoning_effort(),
})
.and_then(|metadata| to_ascii_json_string(&metadata).ok());
let mut environments =
Vec::with_capacity(invocation.step_context.environments.turn_environments.len());
for environment in &invocation.step_context.environments.turn_environments {
let mut environments = Vec::new();
for environment in invocation.step_context.environments.turn_environments() {
// TODO(anp): Migrate extension ToolEnvironment and granted-permission lookup to PathUri
// so extensions can receive foreign environment cwd values.
let Ok(native_cwd) = environment.cwd().to_abs_path() else {
@@ -337,8 +336,7 @@ mod tests {
let truncation_policy = turn.model_info.truncation_policy.into();
let expected_sandbox_cwds = turn
.environments
.turn_environments
.iter()
.turn_environments()
.map(|environment| Some(environment.cwd().clone()))
.collect::<Vec<_>>();
let history_item = ResponseItem::Message {
+1 -2
View File
@@ -162,8 +162,7 @@ fn resolve_tool_environment<'a>(
|| Ok(environments.primary()),
|environment_id| {
environments
.turn_environments
.iter()
.turn_environments()
.find(|environment| environment.environment_id == environment_id)
.map(Some)
.ok_or_else(|| {
@@ -244,6 +244,7 @@ impl ToolOutput for ViewImageOutput {
#[cfg(test)]
mod tests {
use super::*;
use crate::environment_selection::TurnEnvironmentState;
use crate::session::step_context::StepContext;
use crate::session::tests::make_session_and_context;
use crate::session::turn_context::TurnEnvironment;
@@ -262,17 +263,17 @@ mod tests {
fn replace_primary_environment_cwd(turn: &mut crate::TurnContext, cwd: AbsolutePathBuf) {
let current = turn
.environments
.turn_environments
.first()
.turn_environments()
.next()
.cloned()
.expect("default local turn environment");
turn.environments.turn_environments[0] = TurnEnvironment::new(
turn.environments.environments[0] = TurnEnvironmentState::Ready(TurnEnvironment::new(
current.environment_id,
current.environment,
PathUri::from_abs_path(&cwd),
Vec::new(),
current.shell,
);
));
}
#[test]
@@ -72,14 +72,12 @@ impl ToolExecutor<ToolInvocation> for WaitForEnvironmentHandler {
let environment_id = args.environment_id;
let already_ready = step_context
.environments
.turn_environments
.iter()
.turn_environments()
.any(|environment| environment.environment_id == environment_id);
if !already_ready {
let Some(environment) = step_context
.environments
.starting
.iter()
.starting()
.find(|environment| environment.selection.environment_id == environment_id)
.cloned()
else {