Repository navigation
feat(server_runtime): a per-run token that servers can require on their orchestration routes - #6796
Closed
d-molinari wants to merge 3 commits into
Closed
d-molinari wants to merge 3 commits into
d-molinari wants to merge 3 commits into
Conversation
…eep the secret from sandboxes A restart replaces the run's secret, so the cookies and MCP tokens a server signed before it no longer decode. A resources server or agent that signs with it (signs_sessions_with_run_secret, or mcp_bearer_token) now takes part in partial-rollout checkpoints as restart_only whatever it declares, with one warning: a restored session could not be reached, so its episodes start over after a crash instead of verifying on an empty session. The mcp_bearer_token description, get_session_secret, checkpoint_mode_in_effect and participant_workers.py say so. The secret lives in the server's environment, which every process it starts inherits. The Apptainer and local providers now leave the run-secret variables out of the environment they pass to sandboxed code (nemo_gym.core.run_session_secrets, which both packages may import). The get_session_secret docstring and the MCP tutorial state the guarantee's limit: code that shares the server's user and the host's process table (the local provider's, or Apptainer's without --containall) can still read a multi-worker server's secret from its workers' environment. Addresses the review notes on #6161 about the run secret and checkpoint restore, and about its inheritance in Apptainer and local sandboxes. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Daniele Molinari <[email protected]>
…ialize reports Pi shows the model <name>_<tool>, as OpenCode does (gym_mcp.mjs); mcp__<name>__<tool> is what Gym records for Pi's calls, and what mini-SWE uses. The single_agent_turn README, the _check_mcp_server_names docstring and the mcp_server_name comment now say so, and the README names only the agents that refuse names that collide in their own naming (OpenCode and Pi). The MCP tutorial no longer says the deprecated sandboxed agents' tool_servers entries carry mcp_server_name: since 19ab60f they keep the server's reference name. test_bearer_token_and_advertised_name_present_an_ordinary_endpoint now checks that initialize introduces the endpoint under mcp_server_name, so reverting mcp_auto_exposure to the config name fails it. Addresses the review notes on #6161 about the naming docs and the serverInfo.name test. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Daniele Molinari <[email protected]>
…ir orchestration routes
Code in a sandbox can often reach a run's servers over the network, and a
caller's address does not tell it apart from the run's own servers: a
provider's port forwarder can make sandbox traffic arrive from loopback.
The launcher (ServerSupervisor) now makes one random token per run, unless
NEMO_GYM_RUN_TOKEN is already set, and every server it starts inherits it
through that variable. ServerClient sends it in X-NeMo-Gym-Run-Token with
every call to another server. A client that the launcher did not start
reads it from a file that only the launcher's user can read on its host
(in ~/.cache/nemo-gym/run-tokens/, or NEMO_GYM_RUN_TOKEN_DIR). The token
never enters the config, the local and Apptainer sandbox providers drop
the variable, and an HTTP error that Gym raises keeps no copy of it.
A server requires it with `require_run_token: true` in the global config,
or with requires_run_token on its class. It then answers its orchestration
routes (SimpleServer.run_token_routes) only for callers that send it, and
HTTP 401 otherwise:
- agents: /run, /aggregate_metrics, /v1/agent_sessions (/close,
/capabilities), /rollouts/{rollout_id}/stop;
- resources servers: /seed_session, /close_session, /verify,
/aggregate_metrics, /reverify_mode;
- environment servers: /run, /aggregate_metrics.
Routes that code in a sandbox calls with a credential of its own (tool
routes, /mcp, model inference, an agent's own routes) stay open. With
require_run_token, a server without a token fails at startup, and the
launcher refuses a reused venv whose Gym does not send the token.
The default is off: the token is made and sent, and nothing checks it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Daniele Molinari <[email protected]>
d-molinari
force-pushed
the
dmolinari/session-secret-followups
branch
from
November 30, 2026 21:22
0d1efce to
0d4b381
Compare
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This is the follow-up promised in #5010's review of
host_relay: a server cannot tell the run's own callers from code in a sandbox that reaches it over the network. Gym's servers now send each other a per-run token, and a server can require it. Stacked on #6746 (it uses #6746's sandbox environment filter).The token:
ServerSupervisor) makes one random token per run, unlessNEMO_GYM_RUN_TOKENis already set, and every server inherits it through that variable.ServerClientsends it inX-NeMo-Gym-Run-Token.gym evalin another shell, reads it from a user-only file named after the head server's address (~/.cache/nemo-gym/run-tokens/, orNEMO_GYM_RUN_TOKEN_DIR).The check:
require_run_token: true, orrequires_run_token = Trueon a server class, answers these routes only for callers that send the token, and 401 otherwise:/run,/aggregate_metrics,/v1/agent_sessions(with/closeand/capabilities),/rollouts/{rollout_id}/stop;/seed_session,/close_session,/verify,/aggregate_metrics,/reverify_mode;/run,/aggregate_metrics.A server adds routes with
run_token_routes()and may answer 404 instead (run_token_refusal()). The check wraps each route after routing, so prefixes don't matter, and refuses before the body is read.Left open, because sandboxed code calls these with credentials of its own:
/mcp;calls_model_from_sandbox;/ci_policy.Keeping the token from sandboxed code: the local and Apptainer providers drop the variable (feat(server_runtime): run-secret follow-ups, a per-run token for orchestration routes, and an MCP token age limit #6746's filter, extended), and
raise_for_statusleaves the header out of the request info an error keeps. The limit is feat(server_runtime): run-secret follow-ups, a per-run token for orchestration routes, and an MCP token age limit #6746's: those two providers share the server's user, so they do not isolate it.At startup, with
require_run_token: a server without a token fails at startup, and the launcher refuses a reused venv whose Gym does not send the token.Default: off. The token is made and sent, and nothing checks it. The configuration reference documents all of this.
Validation
On Linux (aarch64, Python 3.13), at this commit:
-m sandbox: 2,106 passed. Combined core coverage: 95.11%. Pre-commit on the 22 changed files: passed./verify) each fail their tests.single_agent_turn299,legacy_agent13;simple_agent192,browsecomp_agent162,collab_intel_agent66,pi_agent416,codex_agent635,opencode_agent1,165,stirrup_agent216,prime_agent147,claude_code_agent298;example_session_state_mgmt5,example_mcp_weather6.tests/e2e/session_routingwithNEMO_GYM_MULTIWORKER_E2E=1(the newtest_run_token_e2e.py, plus the routing and episode-tools files): 10/10 passed in each of 3 runs.Rollouts
N/A: nothing changes unless a run sets
require_run_token, and no config here does. With it set, the new e2e runs episodes through an agent, a 4-worker resources server and a fake model. They all score, and callers without the token get 401.Compatibility and benchmark impact
None by default. With
require_run_token: true, a caller of the protected routes other than Gym'sServerClient, such as a training framework's own HTTP client, sends the header. Servers from another run shareNEMO_GYM_RUN_TOKEN.Checklist
git commit -s) (so the DCO check passes).🤖 Generated with Claude Code