Skip to content

feat(environment): require a sandbox provider for BaseEnvironmentServerWithTools and add start_episode_sandbox - #5917

Merged
ffrujeri merged 3 commits into
mainfrom
ffrujeri/self-contained-sandbox
Nov 16, 2026
Merged

ffrujeri merged 3 commits into
mainfrom
ffrujeri/self-contained-sandbox

Conversation

@ffrujeri

@ffrujeri ffrujeri commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #5350 (session routing). The stack: #5346 (episode tokens) → #5347 (the base class) → #5348 (shutdown sweep) → #5349 (MCP) → #5350 (session routing) → this PR. Review those first; this PR's diff against #5350 is the sandbox requirement. The BIRD-Interact stack (#5465 → #5409 → …) sits on this PR, and #5409 is the first environment that uses it.

What changed

An episode of a BaseEnvironmentServerWithTools has two planes:

Plane Where What it does
Control plane The Environment Server Receives a tool call, decides which operation to run and with which limits, records the call and answers
Data plane The episode's sandbox Holds the episode's world and runs every operation on it, the grader's included

The control plane never runs such an operation in its own process. This PR makes every environment say where its data plane runs, and gives it one way to start an episode's sandbox.

  • sandbox_provider is required on BaseEnvironmentServerWithToolsConfig: a provider block name, such as sandbox, or an inline mapping, such as {docker: {}}. A name that the config does not define fails at startup.
  • local logs a warning at startup. The local provider runs commands as host subprocesses, with no isolation. An environment that runs on the host chooses local explicitly and gets the warning; this includes an environment that never starts a sandbox.
  • start_episode_sandbox(request, cleanup, spec, setup, *, name="sandbox") starts a sandbox for the episode:
    • It requires spec.ttl_s, so the provider reclaims the sandbox if the server dies.
    • It merges the metadata as provider defaults, then spec.metadata, then nemo_gym_environment (the server's name) and rollout_id (the episode's capture key), each truncated to 63 characters.
    • It registers sandbox.stop in the episode's cleanup before it starts the sandbox. The episode's cleanup, or the shutdown sweep from feat(environment): finish every Environment Server's running cleanups at shutdown #5348, then also stops a sandbox whose start or first stop failed.
    • If setup fails, it stops the sandbox and raises. If that stop fails, it logs the failure and the episode's cleanup tries again.
  • The class docstring states the control plane and data plane rule.
  • Docs: the MCP guide's Environment Server example (from feat(environment): serve BaseEnvironmentServerWithTools episode tools over MCP #5349) sets sandbox_provider, which it now needs to validate.

A runtime check that refuses process creation and sqlite3 in the control plane is a follow-up (Linear GYMINT-733). It was left out to keep this version light, because its cost at scale is unknown.

Why

Linear GYMINT-734. Nothing in the base class told a subclass to put the work that tools do in a sandbox, and each environment started and stopped its sandboxes by hand. A sandbox gives each episode crash and security isolation for that work. The helper puts the rules from AGENTS.md's Sandbox section in one place: provider from config, metadata merge order, short labels, ttl_s, and a stop on every path.

Validation

Command Result
pytest tests/unit_tests/server_runtime/test_base_environment_server_with_tools.py tests/unit_tests/server_runtime/test_episode_tools_session_routing.py 68 passed
pytest -n 12 tests/unit_tests/server_runtime/ tests/unit_tests/agents/ tests/unit_tests/test_public_api.py tests/unit_tests/test_import_linter_coverage.py tests/unit_tests/test_moved_paths.py 2,229 passed
NEMO_GYM_MULTIWORKER_E2E=1 pytest tests/e2e/session_routing/test_episode_tools_routing_e2e.py 3 passed
pre-commit run on the changed files passed

New tests in test_base_environment_server_with_tools.py, with a fake AsyncSandbox:

  • A tools server without sandbox_provider fails validation.
  • local logs the startup warning; docker does not.
  • A provider name that the config does not define fails at startup.
  • An episode sandbox gets the provider's, the spec's and the episode's labels, runs its setup, and stops when the episode ends.
  • A spec without ttl_s is refused.
  • The episode's cleanup stops a sandbox whose start failed.
  • A failed setup stops the sandbox. When that stop also fails, the episode's cleanup stops it.
  • The shutdown sweep stops the sandbox of a running episode.

Each new behavior was broken on purpose, and its test failed:

Mutation Failing tests
No warning for local 1
No ttl_s check 1
Stop not registered in the cleanup 4
No stop when setup fails 1
No episode labels 1

Rollout evidence

N/A for this PR: it adds a config field and a helper to a base class that no environment on main uses yet. BIRD-Interact (#5409) moves its sandboxes onto start_episode_sandbox, and its rollouts run in that stack.

Compatibility

  • BaseEnvironmentServerWithTools configs must now set sandbox_provider. No server on main subclasses the class yet. Gym's own test servers, the session-routing tests and the e2e server set {local: {}}.
  • BaseEnvironmentServer and other server types do not change.

@copy-pr-bot

copy-pr-bot Bot commented Nov 10, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ffrujeri ffrujeri added area:env-infra Shared environment framework, lifecycle, registry, scaffolding, and validation feature New capabilities, enhancements, or enablement work labels Nov 10, 2026
@ffrujeri
ffrujeri added this pull request to stack #5467 November 10, 2026 05:28
@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test f18ed16

@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test 95fe617

@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test fd5d6cc

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

@ffrujeri
ffrujeri force-pushed the ffrujeri/self-contained-sandbox branch from fd5d6cc to e660930 Compare November 10, 2026 18:03
@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test e660930

@ffrujeri
ffrujeri force-pushed the ffrujeri/self-contained-sandbox branch from bafbbde to a87c8ac Compare November 13, 2026 18:11
@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test a87c8ac

@github-actions github-actions Bot added the sla:review-overdue Review response is over the one-business-day SLA label Nov 13, 2026
@ffrujeri
ffrujeri force-pushed the ffrujeri/self-contained-sandbox branch from a87c8ac to 26e7f04 Compare November 16, 2026 17:08
@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test 26e7f04

@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team November 16, 2026 17:10
@ffrujeri
ffrujeri requested a review from adil-a November 16, 2026 17:10
@github-actions github-actions Bot removed the sla:review-overdue Review response is over the one-business-day SLA label Nov 16, 2026
@ffrujeri
ffrujeri force-pushed the ffrujeri/self-contained-sandbox branch from 26e7f04 to e584c93 Compare November 16, 2026 17:57
@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test e584c93

@ffrujeri
ffrujeri enabled stacked auto-merge November 16, 2026 19:14
@ffrujeri
ffrujeri added this pull request to the merge queue Nov 16, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 16, 2026
Base automatically changed from ffrujeri/self-contained-session-routing to main November 16, 2026 20:08
ffrujeri and others added 3 commits November 16, 2026 12:08
…erWithTools and add start_episode_sandbox

An episode of a BaseEnvironmentServerWithTools has a control plane, the
server, which decides what a tool call runs, and a data plane, the
episode's sandbox, which runs every operation on the episode's world.
The control plane never runs such an operation in its own process.

- BaseEnvironmentServerWithToolsConfig.sandbox_provider is required, so
  every environment says where its data plane runs. A provider that the
  config does not define fails at startup. The local provider runs
  commands as host subprocesses, so the server logs a warning at startup.
- start_episode_sandbox(request, cleanup, spec, setup) starts a sandbox
  for the episode. It requires ttl_s, labels the sandbox with the
  server's name and the episode's rollout over the provider's and the
  spec's metadata, and registers the stop in the episode's cleanup
  before it starts the sandbox, so the cleanup or the shutdown sweep
  also stops a sandbox whose start or first stop failed. A failed setup
  stops the sandbox.
- The class docstring states the control plane and data plane rule.

A runtime check that refuses process creation and sqlite3 in the
control plane is left for a follow-up (GYMINT-733).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Felipe Vieira Frujeri <[email protected]>
…t Server example

BaseEnvironmentServerWithToolsConfig now requires sandbox_provider, so
the example would fail validation without it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Felipe Vieira Frujeri <[email protected]>
…up handle

start_episode_sandbox stopped such a sandbox with a direct call, which
the cleanup timeout did not bound and the episode's cleanup repeated. It
now closes the registered handle: the stop gets the cleanup timeout and
the entry's lock, a stop that succeeded is not run again, and one that
failed or timed out stays for the episode's cleanup to retry.

The docstring now names the providers that ignore ttl_s (Docker with a
custom entrypoint, Apptainer, Enroot and local).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Felipe Vieira Frujeri <[email protected]>
@ffrujeri
ffrujeri force-pushed the ffrujeri/self-contained-sandbox branch from e584c93 to 55cb90a Compare November 16, 2026 20:08
@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test 55cb90a

@ffrujeri
ffrujeri enabled stacked auto-merge November 16, 2026 22:14
@ffrujeri
ffrujeri added this pull request to the merge queue Nov 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/43969961580

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:env-infra Shared environment framework, lifecycle, registry, scaffolding, and validation feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants