Repository navigation
feat: add environment server lifecycle - #3484
Conversation
|
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. |
## 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]>
0b8dd59 to
d336cda
Compare
d336cda to
712041e
Compare
897ee5f to
f18bb9c
Compare
4567ff6 to
d3f4ed3
Compare
d3f4ed3 to
6bc2d08
Compare
|
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? |
The merge-base changed after approval.
6bc2d08 to
518b304
Compare
|
Addressed the four lifecycle findings from the review summary:
|
Signed-off-by: Ananth Subramaniam <[email protected]>
What does this PR do?
Adds
BaseEnvironmentServer, a narrowCleanupContext, concreterun_request(), abstractrun(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
TimeoutErrorand 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 resultThe 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)Rollouts
See the final PR for end-to-end rollout evidence.
Compatibility and benchmark impact
The external route remains
POST /run.