Skip to content

Route process tools to selected environments#20647

Merged
starr-openai merged 17 commits into
mainfrom
pr20314-process-tools-on-pr20281
May 5, 2026
Merged

Route process tools to selected environments#20647
starr-openai merged 17 commits into
mainfrom
pr20314-process-tools-on-pr20281

Conversation

@starr-openai
Copy link
Copy Markdown
Contributor

@starr-openai starr-openai commented May 1, 2026

Why

When a turn exposes multiple selected environments, shell-style tools need a model-facing way to identify the intended target environment and handlers need to resolve that target before parsing cwd-relative permission fields or launching processes.

This PR scopes that rollout to process tools. Filesystem-oriented tools such as apply_patch, view_image, and list_dir are intentionally left for follow-up slices.

What Changed

  • Adds an include_environment_id option to shell-style tool schema builders.
  • Exposes optional environment_id on shell, shell_command, and exec_command only when ToolEnvironmentMode::Multiple is active.
  • Adds a shared handler helper that parses environment_id and workdir from JSON function-call arguments and returns the selected Environment plus effective absolute cwd.
  • Uses that helper in shell, shell_command, and exec_command handling so process execution uses the selected environment filesystem and cwd.
  • Changes ExecCommandRequest to carry a required resolved cwd, removing the process-manager fallback to the primary turn cwd for new exec commands.
  • Leaves write_stdin unchanged because it targets an existing process id, not a new environment.

Testing

  • Added unit coverage for process-tool schema exposure, selected environment resolution, primary fallback, no-environment handling, unknown environment ids, and resolving cwd-relative permission paths against the selected environment cwd.
  • Added a remote-suite e2e coverage case for exec_command routing across explicit zero environments, one local environment, and local+remote environments.
  • Ran just fmt and git diff --check.
@starr-openai starr-openai force-pushed the pr20314-process-tools-on-pr20281 branch 4 times, most recently from 765736f to f1b873f Compare May 1, 2026 18:20
@starr-openai starr-openai force-pushed the pr20314-envctx-on-pr20281 branch from db43360 to 3acd99c Compare May 1, 2026 18:23
@starr-openai starr-openai force-pushed the pr20314-process-tools-on-pr20281 branch 5 times, most recently from 7b87996 to 625359a Compare May 1, 2026 18:53
@starr-openai starr-openai force-pushed the pr20314-process-tools-on-pr20281 branch from cc4be64 to a477948 Compare May 1, 2026 19:09
@starr-openai starr-openai changed the base branch from pr20314-envctx-on-pr20281 to pr20314-env-selection-prep-on-20646 May 1, 2026 19:24
@starr-openai starr-openai force-pushed the pr20314-process-tools-on-pr20281 branch from a477948 to 79874e0 Compare May 1, 2026 19:24
@starr-openai starr-openai changed the title Gate process tools on selected environments Route process tools to selected environments May 1, 2026
@starr-openai starr-openai force-pushed the pr20314-env-selection-prep-on-20646 branch from 326f101 to 0888b48 Compare May 1, 2026 19:29
@starr-openai starr-openai force-pushed the pr20314-process-tools-on-pr20281 branch from 79874e0 to 508399c Compare May 1, 2026 19:31
@starr-openai starr-openai force-pushed the pr20314-env-selection-prep-on-20646 branch from 0888b48 to 281edbc Compare May 1, 2026 19:32
@starr-openai starr-openai force-pushed the pr20314-process-tools-on-pr20281 branch 2 times, most recently from ba48976 to 2d15bcb Compare May 1, 2026 19:43
@starr-openai starr-openai force-pushed the pr20314-env-selection-prep-on-20646 branch from 281edbc to d57209c Compare May 1, 2026 20:01
@starr-openai starr-openai force-pushed the pr20314-process-tools-on-pr20281 branch from 2d15bcb to 68ecaca Compare May 1, 2026 20:03
@starr-openai starr-openai force-pushed the pr20314-env-selection-prep-on-20646 branch from d57209c to b6984c4 Compare May 1, 2026 20:06
@starr-openai starr-openai force-pushed the pr20314-process-tools-on-pr20281 branch 2 times, most recently from 4749c07 to 642d669 Compare May 1, 2026 20:14
@starr-openai starr-openai force-pushed the pr20314-env-selection-prep-on-20646 branch from 6ae16cb to 5ee157f Compare May 1, 2026 21:25
@starr-openai starr-openai force-pushed the pr20314-process-tools-on-pr20281 branch from 642d669 to 3fa2aba Compare May 1, 2026 21:27
Allow the remote exec routing e2e to wait longer for the first remote unified exec turn, which can include container startup overhead on devboxes.

Co-authored-by: Codex <noreply@openai.com>
starr-openai added a commit that referenced this pull request May 4, 2026
Allow apply_patch freeform calls to include optional environment metadata immediately after the begin marker. The handler strips that metadata before invoking the existing patch parser, resolves the selected turn environment, and carries the selected environment filesystem into the apply_patch runtime.

Co-authored-by: Codex <noreply@openai.com>
Cover the no-environment e2e case by asserting exec_command is omitted from the model tool surface instead of sending an unavailable tool call. Keep the local/remote cases as actual routing executions.

Co-authored-by: Codex <noreply@openai.com>
starr-openai added a commit that referenced this pull request May 4, 2026
Allow apply_patch freeform calls to include optional environment metadata immediately after the begin marker. The handler strips that metadata before invoking the existing patch parser, resolves the selected turn environment, and carries the selected environment filesystem into the apply_patch runtime.

Co-authored-by: Codex <noreply@openai.com>
starr-openai added a commit that referenced this pull request May 4, 2026
Allow apply_patch freeform calls to include optional environment metadata immediately after the begin marker. The handler strips that metadata before invoking the existing patch parser, resolves the selected turn environment, and carries the selected environment filesystem into the apply_patch runtime.

Co-authored-by: Codex <noreply@openai.com>
Ensure the remote exec routing e2e drains the no-env turn before submitting the single-env routing case. Without this wait, the next turn can time out even though the routing logic is correct.

Co-authored-by: Codex <noreply@openai.com>
starr-openai added a commit that referenced this pull request May 4, 2026
Allow apply_patch freeform calls to include optional environment metadata immediately after the begin marker. The handler strips that metadata before invoking the existing patch parser, resolves the selected turn environment, and carries the selected environment filesystem into the apply_patch runtime.

Co-authored-by: Codex <noreply@openai.com>
Use a fresh TestCodex session for each no-env, single-env, and multi-env remote exec routing scenario so the e2e validates routing rather than cross-turn sequencing.

Co-authored-by: Codex <noreply@openai.com>
starr-openai added a commit that referenced this pull request May 4, 2026
Allow apply_patch freeform calls to include optional environment metadata immediately after the begin marker. The handler strips that metadata before invoking the existing patch parser, resolves the selected turn environment, and carries the selected environment filesystem into the apply_patch runtime.

Co-authored-by: Codex <noreply@openai.com>
Give each remote exec routing scenario its own mock responses server so broad response matchers cannot intercept requests from a later scenario.

Co-authored-by: Codex <noreply@openai.com>
starr-openai added a commit that referenced this pull request May 4, 2026
Allow apply_patch freeform calls to include optional environment metadata immediately after the begin marker. The handler strips that metadata before invoking the existing patch parser, resolves the selected turn environment, and carries the selected environment filesystem into the apply_patch runtime.

Co-authored-by: Codex <noreply@openai.com>
Use an explicit bash non-login command and short yield timeout in the remote routing e2e so the test varies only environment selection.

Co-authored-by: Codex <noreply@openai.com>
starr-openai added a commit that referenced this pull request May 4, 2026
Allow apply_patch freeform calls to include optional environment metadata immediately after the begin marker. The handler strips that metadata before invoking the existing patch parser, resolves the selected turn environment, and carries the selected environment filesystem into the apply_patch runtime.

Co-authored-by: Codex <noreply@openai.com>
Use the resolved exec cwd when applying granted shell permissions and building sandbox approval metadata so selected turn environments do not fall back to turn.cwd.

Also remove an extra remote-env test wait; submit_turn_with_environments already waits for TurnComplete.

Co-authored-by: Codex <noreply@openai.com>
starr-openai added a commit that referenced this pull request May 4, 2026
Allow apply_patch freeform calls to include optional environment metadata immediately after the begin marker. The handler strips that metadata before invoking the existing patch parser, resolves the selected turn environment, and carries the selected environment filesystem into the apply_patch runtime.

Co-authored-by: Codex <noreply@openai.com>
starr-openai added a commit that referenced this pull request May 4, 2026
Allow apply_patch freeform calls to include optional environment metadata immediately after the begin marker. The handler strips that metadata before invoking the existing patch parser, resolves the selected turn environment, and carries the selected environment filesystem into the apply_patch runtime.

Co-authored-by: Codex <noreply@openai.com>
Bazel clippy denies expect_used for the core integration test binary, so keep the helper assertion explicit instead of using expect.

Co-authored-by: Codex <noreply@openai.com>
starr-openai added a commit that referenced this pull request May 4, 2026
Allow apply_patch freeform calls to include optional environment metadata immediately after the begin marker. The handler strips that metadata before invoking the existing patch parser, resolves the selected turn environment, and carries the selected environment filesystem into the apply_patch runtime.

Co-authored-by: Codex <noreply@openai.com>
starr-openai and others added 2 commits May 4, 2026 16:26
The remote test container does not guarantee the host shell path exists. Request /bin/sh in the routing e2e so the same command can run locally and remotely.

Co-authored-by: Codex <noreply@openai.com>
Run the remote exec routing assertion from /tmp with a unique marker file so the test focuses on selected-environment routing instead of the remote-aware test cwd.

Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
@@ -17,6 +17,13 @@ pub struct ShellToolOptions {
}

pub fn create_exec_command_tool(options: CommandToolOptions) -> ToolSpec {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ideally we delete this one. nobody should be creating exec without knowing whether to add env or not.

environment_id: REMOTE_ENVIRONMENT_ID.to_string(),
cwd: remote_cwd.clone(),
};
let multi_env_output = exec_command_routing_output(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we inline, nobody else uses exec_command_routing_output

}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn exec_command_routes_to_selected_remote_environment() -> Result<()> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this go into exec tests?

use std::time::SystemTime;
use std::time::UNIX_EPOCH;
use tempfile::TempDir;
async fn unified_exec_test(server: &wiremock::MockServer) -> Result<TestCodex> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we inline?

Comment on lines 62 to +63
pub cwd: AbsolutePathBuf,
pub environment: Arc<Environment>,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supernit for the future: might be good to start using TurnEnvironment in places like this where we already store cwd+evn

Co-authored-by: Codex <noreply@openai.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

2 participants