Skip to content

Use selected turn environments for runtime context#20281

Merged
starr-openai merged 18 commits intomainfrom
turncontext-env-stage1-combined
May 1, 2026
Merged

Use selected turn environments for runtime context#20281
starr-openai merged 18 commits intomainfrom
turncontext-env-stage1-combined

Conversation

@starr-openai
Copy link
Copy Markdown
Contributor

Summary

  • make selected turn environments the source of truth for session runtime cwd and MCP runtime environment selection
  • keep local/no-selection fallback behavior intact
  • add coverage for duplicate selected environments, cwd resolution, and MCP runtime environment selection

Validation

  • git diff --check
  • rustfmt was run on touched Rust files during the implementation workflow

CI should provide the full Bazel/test signal.

@starr-openai starr-openai requested a review from a team as a code owner April 29, 2026 23:27
@starr-openai starr-openai force-pushed the turncontext-env-stage1-combined branch from 21fea44 to bcbd73f Compare April 30, 2026 17:17
Comment thread codex-rs/core/src/session/turn_context.rs
Comment thread codex-rs/core/src/session/mod.rs Outdated
Comment thread codex-rs/core/src/environment_selection.rs
Comment thread codex-rs/core/src/thread_manager.rs Outdated
Comment thread codex-rs/core/src/thread_manager.rs Outdated
Comment thread codex-rs/core/src/session/turn_context.rs Outdated
Comment thread codex-rs/core/src/session/tests.rs Outdated
Comment thread codex-rs/core/src/session/session.rs
Comment thread codex-rs/core/src/session/mcp.rs Outdated
Comment thread codex-rs/core/src/session/mcp.rs Outdated
Comment thread codex-rs/core/src/session/mcp.rs Outdated
Comment thread codex-rs/codex-mcp/src/runtime.rs Outdated
@starr-openai starr-openai force-pushed the turncontext-env-stage1-combined branch from f8326f6 to cc129d8 Compare April 30, 2026 22:35
starr-openai added a commit that referenced this pull request Apr 30, 2026
Co-authored-by: Codex <noreply@openai.com>
starr-openai added a commit that referenced this pull request May 1, 2026
Co-authored-by: Codex <noreply@openai.com>
@starr-openai
Copy link
Copy Markdown
Contributor Author

Follow-up rationale for the latest cwd/environment cleanup:

  • SessionConfiguration.cwd and SessionConfiguration.environments[*].cwd now remain separate pieces of state.
  • A turn/session-level cwd update no longer rewrites the stored sticky environment cwd.
  • Environment cwd changes should come from explicit environment selections (TurnEnvironmentSelection.cwd / app-server TurnEnvironmentParams.cwd), while config.cwd remains the fallback when no primary environment is selected.
  • MCP startup/refresh now keeps the prior default-environment-or-local fallback inline, using the primary turn/stored environment only when one is explicitly selected.

This keeps the PR pointed in the direction of relying on explicit env cwd rather than deriving or mutating env cwd from unrelated turn-level cwd updates.

&& let Some(primary_environment) = next_configuration.environments.first_mut()
{
primary_environment.cwd = absolute_cwd;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

re previous comments about cwd handling:

im removing places where we took turn level cwd and overrode primary env cwd. instead, we'll rely on the explicit TurnEnvironmentParams.cwd for this, and only use turn level cwd as a fallback if none was provided.

});
if let Some(environment_selections) = environment_selections.as_ref() {
self.thread_manager
.validate_environment_selections(environment_selections)
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, move this method from thread_manager into message processor?

) -> CodexResult<()> {
validate_environment_selections(self.state.environment_manager.as_ref(), environments)
resolve_environment_selections(self.state.environment_manager.as_ref(), environments)
.map(|_| ())
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: what does this map do?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

we're just calling resolve_environment_selections for validation here but dropping the result

@@ -672,8 +656,6 @@ impl Session {
turn_environments: Vec<TurnEnvironment>,
) -> Arc<TurnContext> {
let primary_turn_environment = turn_environments.first();
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: not for this round, but we are still having multiple places deciding what primary is

let mcp_servers = with_codex_apps_mcp(mcp_servers, auth.as_ref(), &mcp_config);
let auth_statuses =
compute_auth_statuses(mcp_servers.iter(), store_mode, auth.as_ref()).await;
let mcp_runtime_environment = match turn_context.primary_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.

is this a copy of logic in session.rs?

@starr-openai starr-openai force-pushed the turncontext-env-stage1-combined branch 2 times, most recently from 2190528 to 1d64860 Compare May 1, 2026 03:01
@starr-openai starr-openai force-pushed the turncontext-env-stage1-combined branch from 1d64860 to 4deeb8d Compare May 1, 2026 03:12
starr-openai and others added 18 commits May 1, 2026 10:28
Route session runtime cwd and MCP startup state through selected turn environments so multi-environment turns avoid a parallel legacy Environment field.

Keep fallback behavior for local/no-selection sessions and add focused coverage for duplicate selections, cwd resolution, and MCP runtime environment selection.

Validation: git diff --check

Co-authored-by: Codex <noreply@openai.com>
Resolve relative cwd updates against turn-local environment selections, and keep the TurnContext helper surface minimal.

Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Keep environment cwd ownership on explicit environment selections, tighten MCP runtime environment fallback, and clarify resolved environment projection naming.

Co-authored-by: Codex <noreply@openai.com>
Route the remaining validation convenience API through resolve_environment_selections and keep the resolver tests attached to the resolver itself.

Co-authored-by: Codex <noreply@openai.com>
Keep the explicit turn-environment cwd assertion focused on the selected environment cwd, and remove fallback wording from the RMCP stored-environment test identifiers.

Co-authored-by: Codex <noreply@openai.com>
Add the test-only imports and current McpServerConfig fields required by the rebased branch, and derive Debug for ResolvedTurnEnvironments so resolver error tests compile under Bazel.

Co-authored-by: Codex <noreply@openai.com>
Use the shared test PathBufExt import for RMCP integration tests and run environment selection tests that touch the local executor under Tokio.

Co-authored-by: Codex <noreply@openai.com>
Use turn-local environments for pending MCP refreshes, keep disabled-environment sessions startable when no MCP servers are enabled, and reject ambiguous cwd plus explicit environment updates.

Co-authored-by: Codex <noreply@openai.com>
Add the required argument comment for the default-environment fallback flag.

Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Keep selected environment cwd scoped to resolved turn environments, remove fallback helpers added during review, and preserve existing MCP default/local fallback behavior inline.

Co-authored-by: Codex <noreply@openai.com>
Preserve environment cwd as an explicit environment-selection value, and keep session cwd updates scoped to session configuration only.

Co-authored-by: Codex <noreply@openai.com>
Remove the RMCP integration test added during the environment-selection review pass so this PR does not carry that broader test change.

Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Apply turn-level cwd overrides to the primary selected environment only for the active turn so runtime cwd, environment context diffs, and app-server turn-start behavior stay aligned without rewriting stored sticky environment selections.

Co-authored-by: Codex <noreply@openai.com>
@starr-openai starr-openai force-pushed the turncontext-env-stage1-combined branch from 4deeb8d to 5a0b31b Compare May 1, 2026 17:28
@starr-openai starr-openai merged commit be71b6f into main May 1, 2026
25 checks passed
@starr-openai starr-openai deleted the turncontext-env-stage1-combined branch May 1, 2026 18:00
@github-actions github-actions Bot locked and limited conversation to collaborators May 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants