Skip to content

feat: add environment server lifecycle - #3484

Merged
ananthsub merged 1 commit into
mainfrom
ananthsub/episode-orchestration-03-processor-lifecycle
Sep 22, 2026
Merged

ananthsub merged 1 commit into
mainfrom
ananthsub/episode-orchestration-03-processor-lifecycle

Conversation

@ananthsub

@ananthsub ananthsub commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds BaseEnvironmentServer, a narrow CleanupContext, concrete run_request(), abstract run(request, cleanup), admission, an opt-in episode deadline, typed failure conversion, aggregation, and cancellation-resistant cleanup.

Cleanup is bounded and LIFO. Explicit cleanup and final unwinding share a per-entry lock, and AnyIO cancellation is shielded without busy-spinning. Internal TimeoutError and unexpected protocol exceptions become distinct typed failures.

Single-agent interaction flow

This diagram shows the built-in single-agent use case. Other Environment Server protocols may coordinate different participants and use different request, result, and lifecycle flows.

sequenceDiagram
    participant C as Rollout collector
    participant E as Environment Server
    participant R as Task / Resources Server
    participant A as Agent Server
    participant M as Model Server
    participant S as Task sandbox

    C->>E: POST /run
    E->>R: Seed task session
    R-->>E: Tool access and optional SandboxAccess
    E->>A: Seed agent session
    A->>S: Connect as borrower when supplied
    E->>A: Invoke /v1/responses
    A->>M: Model and capture requests
    A-->>E: Agent response
    E->>A: Close agent session
    E->>R: Verify response
    E->>R: Close task session
    E-->>C: Episode result
Loading

The Environment Server coordinates ordering and the final result. Agent and Task/Resources Servers retain ownership of the state and resources they create.

This is part 3 of 14 in the Environment Server stack. Previous: #3483. Next: #3543.

Validation

  • uv run pytest tests/unit_tests/test_environment_server.py -q (11 passed)
  • scoped Ruff and pre-commit checks

Rollouts

See the final PR for end-to-end rollout evidence.

Compatibility and benchmark impact

The external route remains POST /run.

@copy-pr-bot

copy-pr-bot Bot commented Sep 17, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@ananthsub
ananthsub added this pull request to stack #3493 September 17, 2026 16:31
@ananthsub ananthsub changed the title ananthsub/episode orchestration 03 processor lifecycle feat: add episode processor lifecycle Sep 17, 2026
jeffwillette pushed a commit to jeffwillette/Gym that referenced this pull request Sep 17, 2026
## Background / motivation

- ARC and sandbox callers may run the published image as a non-root
identity.
- The image installed uv and managed Python below `/root`, and runtime
paths were root-owned.
- Follow the non-root runtime contract established by [Automodel
NVIDIA-NeMo#3484](NVIDIA-NeMo/Automodel#3484) and
[Megatron-LM NVIDIA-NeMo#6421](NVIDIA/Megatron-LM#6421).
- Preserve root as the published image default.

## What changed

- Install pinned `uv` and `uvx` release archives in `/usr/local/bin`
after architecture-specific SHA-256 verification, with managed Python
under `/opt/uv/python`.
- Add configurable `RUNTIME_UID` and `RUNTIME_GID` build arguments,
defaulting to `65532:65532`.
- Conditionally create the runtime group/user and `/home/nemo-runtime`.
- Keep the preserved Git checkout root-owned during package
installation, then transfer the completed source tree and mutable
runtime paths to the runtime identity.
- Separate the root-owned build cache from writable runtime uv/XDG
caches.
- Validate the non-root contract during the image build, then restore
`USER root`.
- Exclude local `.venv` content from Docker build contexts.
- Allowlist the pinned `uv` archive checksums for the change-set secrets
scan.

## Details

- Runtime callers opt in with `--user 65532:65532` and explicitly set
`HOME=/home/nemo-runtime` and `XDG_CACHE_HOME=/opt/nemo-gym/cache/xdg`.
- The symlinked project environment continues to read packages from the
root-owned build cache at `/opt/uv/cache`.
- Runtime uv state uses `/opt/nemo-gym/cache/uv`; Gym cache, results,
source, and prefetched environments are owned by UID/GID 65532.
- The build-stage smoke test verifies:
  - UID 65532 execution.
  - `uv`, `uvx`, and the project interpreter.
  - Home, XDG, uv, source, cache, results, and site-packages writes.
  - Removal of a root-created site-packages probe.
  - `gym --help`.
  - Python 3.13 virtualenv creation and execution with `uv`.
- The final Dockerfile instruction restores `USER root`.
- `detect-secrets` flagged both `uv_sha256` literals as `Hex High
Entropy String`. Each is preceded by a column-0 `# pragma: allowlist
nextline secret` line; the inline same-line form is unusable here
because BuildKit joins `RUN` continuation lines into a single shell
command, so a trailing `#` would comment out the remainder of the
instruction.

## Tested

- `docker buildx build --check --build-context nemo-gym=. --target
release -f docker/Dockerfile .`
- Check completed with no warnings at source
`52e5eac7d1f917b43622cbe6a2f04a0a2c091b13`.
- `docker buildx build --builder multiarch --progress=plain
--build-context nemo-gym=. -f docker/Dockerfile --tag
nemo-gym-rootless:pr2560 --load .`
  - Release image build and embedded UID 65532 smoke test passed.
  - Loaded image: `nemo-gym-rootless:pr2560`.
- `docker image inspect --format {{json .Config.User}}
nemo-gym-rootless:pr2560`
  - Observed configured user: `root`.
- `docker run --rm --entrypoint id nemo-gym-rootless:pr2560 -u`
  - Observed UID: `0`.
- `docker run --rm --user 65532:65532 --env HOME=/home/nemo-runtime
--env XDG_CACHE_HOME=/opt/nemo-gym/cache/xdg nemo-gym-rootless:pr2560
--help`
  - Exited successfully.
- Runtime UID/write checks against the loaded image:
  - Observed `runtime-user=65532`.
- `uv run --locked --extra dev pytest tests/unit_tests/ -q`
  - 2,461 passed, 41 skipped, 15 subtests passed.
- `uv run pre-commit run --all-files`
  - All hooks passed.
- CI-Events OCI-builder workload `oci-90e81a1077c4365e79891ccf`:
- Built exact source `52e5eac7d1f917b43622cbe6a2f04a0a2c091b13` for
`amd64`.
- Used the declared
`nvcr.io/nvidia/cuda-dl-base:26.03-cuda13.2-devel-ubuntu24.04` base
through the documented flattened-filesystem path; no alternate base
image was selected.
  - Gitleaks, DevSkim, Bandit, and LLM review passed.
- OCI build, embedded non-root smoke test, registry push, archive, and
terminal status passed.
- Used the contract-permitted native fallback classified as
`gvisor-dpkg-hardlink`.
- Published immutable image:
`766267172432.dkr.ecr.us-east-1.amazonaws.com/nemo-autobot/agent-sandbox@sha256:6cb5462321dda99f1eaadb273efe3a30b62806d91ec059b0f5f26b39ca189177`.
- ECR verified tag `gym-pr-2560-rootless-52e5eac7-amd64` at the same
digest; pushed `2026-08-15T01:55:48Z`.
- `detect-secrets-hook --baseline
.github/workflows/config/.secrets.baseline` over the CI change-set (`git
diff --name-only --diff-filter=d --merge-base origin/main`)
- Exit code 0 at source `c4d47d7c3df4b547ea4c7b0f422cf5b714208cfb`;
previously exit 123 on `docker/Dockerfile:83` and `:87`.
- BuildKit continuation probe (`case` block with the same column-0
comment lines)
- Resolved command retained both assignments; observed `PROBE
arch=x86_64 sha=04f8b82f…be829`.
- `docker buildx build --check --build-context nemo-gym=. --target
release -f docker/Dockerfile .`
  - Check completed with no warnings after the change.

---------

Signed-off-by: oliver könig <[email protected]>
@ananthsub
ananthsub force-pushed the ananthsub/episode-orchestration-03-processor-lifecycle branch 2 times, most recently from 0b8dd59 to d336cda Compare September 17, 2026 20:28
@ananthsub
ananthsub force-pushed the ananthsub/episode-orchestration-03-processor-lifecycle branch from d336cda to 712041e Compare September 18, 2026 00:55
@ananthsub ananthsub changed the title feat: add episode processor lifecycle feat: add environment server lifecycle Sep 18, 2026
@ananthsub
ananthsub force-pushed the ananthsub/episode-orchestration-03-processor-lifecycle branch 2 times, most recently from 897ee5f to f18bb9c Compare September 18, 2026 01:32
@ananthsub
ananthsub marked this pull request as ready for review September 18, 2026 19:45
@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team September 18, 2026 19:46
@ananthsub
ananthsub force-pushed the ananthsub/episode-orchestration-03-processor-lifecycle branch from 4567ff6 to d3f4ed3 Compare September 18, 2026 22:42
@yaoyu-33 yaoyu-33 added area:env-infra Shared environment framework, lifecycle, registry, scaffolding, and validation complexity:high Cross-component or shared-contract change with a large review surface feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer labels Sep 18, 2026
@ananthsub
ananthsub force-pushed the ananthsub/episode-orchestration-03-processor-lifecycle branch from d3f4ed3 to 6bc2d08 Compare September 19, 2026 01:02
ffrujeri
ffrujeri previously approved these changes Sep 19, 2026
Comment thread nemo_gym/base_environment_server.py Resolved
@ffrujeri
ffrujeri self-requested a review September 19, 2026 18:29
Comment thread nemo_gym/base_environment_server.py Resolved
@cmunley1

Copy link
Copy Markdown
Contributor

claude review comments for consideration

nemo_gym/base_environment_server.py:66

CleanupHandle.close() checks entry.active then awaits the callback before setting it False — no lock. Two concurrent close() calls (or close() racing aclose()'s unwind) can both pass the guard and double-invoke a non-idempotent callback. Can we add a lock per entry, or document that concurrent close() calls on the same handle are unsupported?

nemo_gym/base_environment_server.py:196

On CancelledError we build a typed EpisodeFailure response here, then unconditionally raise cancelled right after — that response is never returned. Should this return response instead, or is the construction dead code we should delete?

nemo_gym/base_environment_server.py:186

except TimeoutError can't distinguish the episode's own admission timeout from a TimeoutError raised inside run() (they're aliased in 3.11+), so both get reported as "Episode timed out." opensandbox/provider.py already guards this with hard_cap.expired() — can we reuse that pattern?

nemo_gym/base_environment_server.py:157

Exceptions other than TimeoutError/HandledEpisodeError/CancelledError escape run_request uncaught and surface as an untyped 500, despite /run declaring response_model=self.response_model. Per the repo's env-design guidance ("must handle failures with meaningful error responses, not crash"), should there be a catch-all that still returns a typed EpisodeFailure?

@ananthsub
ananthsub dismissed ffrujeri’s stale review September 21, 2026 16:09

The merge-base changed after approval.

@ananthsub
ananthsub force-pushed the ananthsub/episode-orchestration-03-processor-lifecycle branch from 6bc2d08 to 518b304 Compare September 21, 2026 17:19
@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team September 21, 2026 17:20
@ananthsub

ananthsub commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Addressed the four lifecycle findings from the review summary:

  • cleanup entries now serialize explicit close and final unwind with a per-entry lock;
  • cancellation no longer constructs an unused response and is re-raised after shielded cleanup;
  • the episode deadline is checked explicitly, so an internal TimeoutError is not mislabeled as an episode timeout;
  • unexpected protocol exceptions are logged and returned as terminal typed EpisodeFailure responses instead of untyped 500s.

Comment thread nemo_gym/base_environment_server.py Resolved
Signed-off-by: Ananth Subramaniam <[email protected]>

This branch was previously deployed

1 inactive deployment
public — c3a8dd32 Deployed Sep 22, 2026 by copy-pr-bot[bot] via release / finalize / notify #3464
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 complexity:high Cross-component or shared-contract change with a large review surface feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer sla:review-overdue Review response is over the one-business-day SLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants