Centralize turn environment selection state (#38461)

## What changed

- Store `TurnEnvironmentSelection` directly on each resolved `TurnEnvironment`
  instead of copying its environment ID, working directory, and workspace roots
  into separate fields.
- Update environment resolution, tool execution, approvals, and related tests to
  access selection state through the retained value.

GitOrigin-RevId: ab723d21e112b8af5bcaf1f772a5e376553312bc
This commit is contained in:
sayan-oai authored and copyberry committed 2026-08-14 00:22:54 +00:00
1 parent 18bbb585e7
commit 535795f7d1
31 files changed
+159 -146

No files matched your search

@@ -282,7 +282,7 @@ async fn effective_patch_permissions(
crate::tools::handlers::EffectiveAdditionalPermissions,
codex_protocol::permissions::FileSystemSandboxPolicy,
)> {
let environment_id = environment.environment_id.as_str();
let environment_id = environment.selection.environment_id.as_str();
let file_paths = file_paths_for_action(action);
let native_cwd = cwd.to_abs_path()?;
let granted_permissions = merge_permission_profiles(
@@ -565,7 +565,7 @@ async fn execute_verified_patch(
let emitter = ToolEmitter::apply_patch_for_environment(
changes.clone(),
apply.auto_approved,
turn_environment.environment_id.clone(),
turn_environment.selection.environment_id.clone(),
);
let event_ctx = ToolEventCtx::new(
tool_ctx.session.as_ref(),
@@ -150,7 +150,7 @@ async fn to_extension_call(invocation: &ToolInvocation) -> ExtensionToolCall {
};
let additional_permissions = apply_granted_turn_permissions(
invocation.session.as_ref(),
&environment.environment_id,
&environment.selection.environment_id,
native_cwd.as_path(),
SandboxPermissions::UseDefault,
/*additional_permissions*/ None,
@@ -161,7 +161,7 @@ async fn to_extension_call(invocation: &ToolInvocation) -> ExtensionToolCall {
.turn
.file_system_sandbox_context(additional_permissions, environment);
environments.push(ToolEnvironment {
environment_id: environment.environment_id.clone(),
environment_id: environment.selection.environment_id.clone(),
cwd: native_cwd,
file_system: environment.environment.get_filesystem(),
file_system_sandbox_context,
+1 -1
View File
@@ -175,7 +175,7 @@ fn resolve_tool_environment<'a>(
|environment_id| {
environments
.turn_environments()
.find(|environment| environment.environment_id == environment_id)
.find(|environment| environment.selection.environment_id == environment_id)
.map(Some)
.ok_or_else(|| {
FunctionCallError::RespondToModel(format!(
+1 -1
View File
@@ -95,7 +95,7 @@ async fn run_exec_like(args: RunExecLikeArgs) -> Result<FunctionToolOutput, Func
let requested_additional_permissions = additional_permissions.clone();
let effective_additional_permissions = apply_granted_turn_permissions(
session.as_ref(),
&turn_environment.environment_id,
&turn_environment.selection.environment_id,
exec_params.cwd.as_path(),
exec_params.sandbox_permissions,
additional_permissions,
@@ -125,7 +125,7 @@ impl ShellCommandHandler {
capture_policy: ExecCapturePolicy::ShellTool,
env,
network: turn_context.network.clone(),
network_environment_id: Some(turn_environment.environment_id.clone()),
network_environment_id: Some(turn_environment.selection.environment_id.clone()),
sandbox_permissions,
windows_sandbox_level: turn_context.windows_sandbox_level,
windows_sandbox_private_desktop: turn_context
@@ -25,6 +25,7 @@ use crate::tools::handlers::ShellCommandHandler;
use crate::tools::hook_names::HookToolName;
use crate::tools::registry::CoreToolRuntime;
use crate::turn_diff_tracker::TurnDiffTracker;
use codex_protocol::protocol::TurnEnvironmentSelection;
use codex_shell_command::is_safe_command::is_known_safe_command;
use codex_shell_command::powershell::try_find_powershell_executable_blocking;
use codex_shell_command::powershell::try_find_pwsh_executable_blocking;
@@ -103,7 +104,11 @@ async fn shell_command_handler_to_exec_params_uses_selected_environment() {
let expected_cwd = selected_cwd.join("subdir");
let active_permission_profile = ActivePermissionProfile::new("selected-profile");
let selected_environment = TurnEnvironment::new(
"selected-environment".to_string(),
TurnEnvironmentSelection {
environment_id: "selected-environment".to_string(),
cwd: PathUri::from_abs_path(&selected_cwd),
workspace_roots: Vec::new(),
},
Arc::clone(
&turn_context
.environments
@@ -111,8 +116,6 @@ async fn shell_command_handler_to_exec_params_uses_selected_environment() {
.expect("primary environment")
.environment,
),
PathUri::from_abs_path(&selected_cwd),
Vec::new(),
Some(selected_shell),
TurnEnvironmentConfig {
allow_login_shell: true,
@@ -197,7 +197,7 @@ impl ExecCommandHandler {
&hook_command,
&cwd,
native_cwd.as_ref(),
&turn_environment.environment_id,
&turn_environment.selection.environment_id,
)
.await;
let shell_mode =
@@ -217,13 +217,13 @@ impl ExecCommandHandler {
let Some(remote_shell) = turn_environment.shell.as_ref() else {
return Err(FunctionCallError::RespondToModel(format!(
"environment `{}` does not report a shell",
turn_environment.environment_id
turn_environment.selection.environment_id
)));
};
if detect_shell_type(Path::new(&requested_shell)) != Some(remote_shell.shell_type) {
return Err(FunctionCallError::RespondToModel(format!(
"environment `{}` only supports `{}`",
turn_environment.environment_id,
turn_environment.selection.environment_id,
remote_shell.name()
)));
}
@@ -258,7 +258,7 @@ impl ExecCommandHandler {
let permission_cwd = native_cwd.as_ref().unwrap_or(&turn.config.cwd);
let effective_additional_permissions = apply_granted_turn_permissions(
context.session.as_ref(),
&turn_environment.environment_id,
&turn_environment.selection.environment_id,
permission_cwd.as_path(),
sandbox_permissions,
additional_permissions,
@@ -285,11 +285,12 @@ mod tests {
.next()
.cloned()
.expect("default local turn environment");
let mut selection = current.selection;
selection.cwd = PathUri::from_abs_path(&cwd);
selection.workspace_roots.clear();
turn.environments.environments[0] = TurnEnvironmentState::Ready(TurnEnvironment::new(
current.environment_id,
selection,
current.environment,
PathUri::from_abs_path(&cwd),
Vec::new(),
current.shell,
current.config,
));
@@ -123,7 +123,7 @@ impl ToolExecutor<ToolInvocation> for WaitForEnvironmentHandler {
let already_ready = step_context
.environments
.turn_environments()
.any(|environment| environment.environment_id == environment_id);
.any(|environment| environment.selection.environment_id == environment_id);
if !already_ready {
let Some(environment) = step_context
.environments