Skip to content

[codex] retain resolved environments across turns#27955

Merged
pakrym-oai merged 14 commits into
mainfrom
pakrym/full-ci-session-turn-environments
Jun 15, 2026
Merged

[codex] retain resolved environments across turns#27955
pakrym-oai merged 14 commits into
mainfrom
pakrym/full-ci-session-turn-environments

Conversation

@pakrym-oai

@pakrym-oai pakrym-oai commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Why

Selected execution environments are thread-scoped resources, but startup and turn construction repeatedly resolved their IDs and working directories. That discarded existing environment handles and shell metadata even when a selection had not changed.

Session configuration updates also need to affect future turns without changing the resolved environment set already captured by a running turn.

What changed

  • Create a ThreadEnvironments service inside Codex from the spawned EnvironmentManager and raw environment selections, then store it on SessionServices.
  • Split service construction from update_selections, allowing session configuration updates to mutate the resolved set in place.
  • Retain an existing TurnEnvironment when its environment ID and working directory match; resolve only added or changed selections and remove selections that are no longer present.
  • Normalize duplicate IDs by keeping the first selection and skip individual selections that fail to resolve instead of rejecting the entire update.
  • Give each TurnContext a cloned TurnEnvironmentSnapshot, so later session configuration updates affect future turns without rewriting an active turn.
  • Reuse the service-owned environment manager and resolved snapshot for startup work, MCP initialization, and child-thread spawning instead of flowing resolved environments through spawn arguments.

Test plan

  • cargo check -p codex-core --tests
  • just test -p codex-core environment_selection
  • just test -p codex-core turn_environments
  • just test -p codex-core session_update_settings_does_not_rewrite_sticky_environment_cwds
  • just test -p codex-core default_turn_does_not_overlay_legacy_fallback_cwd_onto_stored_thread_environments

@pakrym-oai
pakrym-oai marked this pull request as ready for review June 13, 2026 06:41
@pakrym-oai
pakrym-oai requested a review from a team as a code owner June 13, 2026 06:41
pakrym-oai added a commit that referenced this pull request Jun 13, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 53814c2004

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread codex-rs/core/src/session/turn_context.rs
Comment thread codex-rs/core/src/codex_delegate.rs Outdated
Comment thread codex-rs/core/src/session/turn_context.rs Outdated
Comment thread codex-rs/core/src/environment_selection.rs
pakrym-oai added a commit that referenced this pull request Jun 13, 2026
pakrym-oai added a commit that referenced this pull request Jun 13, 2026
@pakrym-oai
pakrym-oai force-pushed the pakrym/full-ci-session-turn-environments branch from f00d3ed to b9f12dc Compare June 15, 2026 20:17
@pakrym-oai pakrym-oai changed the title [codex] retain resolved turn environments in session state [codex] retain resolved environments across turns Jun 15, 2026
@pakrym-oai

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e19fb2d6c0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@@ -108,7 +111,7 @@ pub(crate) async fn run_codex_thread_interactive(
inherited_exec_policy: Some(Arc::clone(&parent_session.services.exec_policy)),
parent_rollout_thread_trace: codex_rollout_trace::ThreadTraceContext::disabled(),
parent_trace: None,
environment_selections: parent_ctx.environments.clone(),
environment_selections: parent_ctx.environments.to_selections(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Badge Preserve parent environment handles for child spawn

Converting the parent's resolved environment snapshot back to ID/cwd selections makes the child re-resolve in Codex::spawn. If that remote ID was upserted after the turn began, the child can load AGENTS/skills and run in a different environment than the active parent context. guidance

Useful? React with 👍 / 👎.

@pakrym-oai
pakrym-oai force-pushed the pakrym/full-ci-session-turn-environments branch from e19fb2d to f4c74a4 Compare June 15, 2026 21:54
warn!("failed to resolve turn environments: {err}");
ResolvedTurnEnvironments::default()
});
let turn_environments = self.services.turn_environments.snapshot().await;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we have an integraiton test confirming the same env is used across two UserInputs?

or is that too heavy for this stage

Comment on lines +676 to +680
if let Some(environments) = &updates.environments {
self.services
.turn_environments
.update_selections(&environments.environments)
.await;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[codex] this makes a window where SessionConfiguration is stored before this await, so another turn build during env resolution could combine new settings with prev environment (run against wrong cwd/host)

@pakrym-oai
pakrym-oai merged commit 9728992 into main Jun 15, 2026
61 of 74 checks passed
@pakrym-oai
pakrym-oai deleted the pakrym/full-ci-session-turn-environments branch June 15, 2026 23:15
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants