Skip to content

core: use current step environments for tools - #29547

Merged
sayan-oai merged 3 commits into
mainfrom
codex/tools-use-step-context-environments
Jun 23, 2026
Merged

core: use current step environments for tools#29547
sayan-oai merged 3 commits into
mainfrom
codex/tools-use-step-context-environments

Conversation

@sayan-oai

@sayan-oai sayan-oai commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Why

With deferred executors, an environment can become ready between two sampling requests in the same turn. The model-visible environment update, advertised tools, and eventual tool execution must all describe the same request-time view.

Otherwise, a request built while only environment B is ready can advertise a tool without an environment_id; if higher-priority environment A becomes ready before execution, that call could silently run in A instead.

This PR is stacked on #29527.

Design

run_turn captures one Arc<StepContext> at each sampling-request boundary. That step owns the request's TurnContext and environment snapshot.

  • World-state environment updates and tool planning borrow that same step.
  • ToolCallRuntime retains the Arc while asynchronous tool calls execute.
  • ToolInvocation carries the step to handlers; its temporary turn compatibility field is derived from the same object.
  • ToolRouter does not retain StepContext; it only uses it while constructing the request's tool set.
  • With DeferredExecutor disabled, step capture keeps using the environments frozen at turn start.

Simply: every sampling request gets one consistent picture of its environments, from what the model sees through where its tool calls run.

What changed

  • Build environment-dependent tool specs from the current request's StepContext.
  • Use that same step for unified exec, legacy shell, apply_patch, view_image, and request_permissions execution.
  • Hide environment-backed tools, including request_permissions, while no environment is attached.
  • Resolve legacy shell paths and metadata from the selected step environment instead of the stale turn-start environment.
  • Capture explicit steps at non-turn-loop boundaries such as compaction, prompt debug, and startup prewarm.
  • Reconcile prompt-debug history from the same step used to build its tools.

Follow-up

  • Bind yielded code-mode cells to the tool runtime that created them, so nested calls made after yielding continue to use the originating request's StepContext.

Test plan

  • just test -p codex-core deferred_executor_updates_context_and_tools_after_startup
  • just test -p codex-core environment_count_controls_environment_backed_tools
  • just test -p codex-core build_prompt_input_includes_context_and_user_message

@sayan-oai
sayan-oai requested a review from a team as a code owner June 23, 2026 03:42

@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: 26c11ecd14

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/core/src/tools/handlers/shell/shell_command.rs
@sayan-oai

Copy link
Copy Markdown
Contributor Author

@codex review this

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 3be0e765e3

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/core/src/tools/router.rs Outdated
Comment thread codex-rs/core/src/session/tests/guardian_tests.rs Outdated
Comment thread codex-rs/core/src/session/mod.rs
Comment thread codex-rs/core/src/session/step_context.rs Outdated
Comment thread codex-rs/core/src/tools/handlers/shell/shell_command.rs
Comment thread codex-rs/core/tests/suite/remote_env.rs
Comment thread codex-rs/core/tests/suite/remote_env.rs
Comment thread codex-rs/core/src/session/turn_context.rs
@sayan-oai
sayan-oai force-pushed the codex/tools-use-step-context-environments branch from 3be0e76 to 3cd0872 Compare June 23, 2026 09:45
@sayan-oai

Copy link
Copy Markdown
Contributor Author

@codex review this

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 3cd0872bf6

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/core/src/session/turn.rs
Base automatically changed from codex/compaction-current-step-world-state to main June 23, 2026 17:33
@sayan-oai
sayan-oai force-pushed the codex/tools-use-step-context-environments branch from 3cd0872 to f6f0154 Compare June 23, 2026 19:15
@sayan-oai
sayan-oai enabled auto-merge (squash) June 23, 2026 19:17
@sayan-oai
sayan-oai force-pushed the codex/tools-use-step-context-environments branch from f6f0154 to 3be800b Compare June 23, 2026 20:08
@sayan-oai
sayan-oai merged commit 4cc6a4b into main Jun 23, 2026
31 checks passed
@sayan-oai
sayan-oai deleted the codex/tools-use-step-context-environments branch June 23, 2026 20:21
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 23, 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