Skip to content

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
dmolinari/session-secret-followupsfrom
dmolinari/gym-run-token
Closed

d-molinari wants to merge 3 commits into
dmolinari/session-secret-followupsfrom
dmolinari/gym-run-token

Conversation

@d-molinari

Copy link
Copy Markdown
Contributor

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:

    • The launcher (ServerSupervisor) makes one random token per run, unless NEMO_GYM_RUN_TOKEN is already set, and every server inherits it through that variable.
    • ServerClient sends it in X-NeMo-Gym-Run-Token.
    • A client that the launcher did not start, such as gym eval in another shell, reads it from a user-only file named after the head server's address (~/.cache/nemo-gym/run-tokens/, or NEMO_GYM_RUN_TOKEN_DIR).
    • The token never enters the config.
  • The check: require_run_token: true, or requires_run_token = True on a server class, answers these routes only for callers that send the token, and 401 otherwise:

    • agents: /run, /aggregate_metrics, /v1/agent_sessions (with /close and /capabilities), /rollouts/{rollout_id}/stop;
    • resources servers: /seed_session, /close_session, /verify, /aggregate_metrics, /reverify_mode;
    • environment servers: /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:

    • tool routes, with cookies: browsecomp and stirrup hand direct-HTTP grants into sandboxes;
    • /mcp;
    • Environment Server tools, with the episode token;
    • model inference: about 30 agents set calls_model_from_sandbox;
    • agents' own routes, such as browsecomp's token count and collab_intel's /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_status leaves 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:

  • Core shards 0-3 with coverage: 6,749 / 5,650 / 6,323 / 6,072 passed. -m sandbox: 2,106 passed. Combined core coverage: 95.11%. Pre-commit on the 22 changed files: passed.
  • The new tests fail on feat(server_runtime): run-secret follow-ups, a per-run token for orchestration routes, and an MCP token age limit #6746's code. 15 mutants of the guarded lines (header, file, filter, guard, class flag, startup refusal, wiring, cleanup, probe, error redaction, /verify) each fail their tests.
  • Server suites, all passed:
    • environment: single_agent_turn 299, legacy_agent 13;
    • agents: simple_agent 192, browsecomp_agent 162, collab_intel_agent 66, pi_agent 416, codex_agent 635, opencode_agent 1,165, stirrup_agent 216, prime_agent 147, claude_code_agent 298;
    • resources: example_session_state_mgmt 5, example_mcp_weather 6.
  • tests/e2e/session_routing with NEMO_GYM_MULTIWORKER_E2E=1 (the new test_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's ServerClient, such as a training framework's own HTTP client, sends the header. Servers from another run share NEMO_GYM_RUN_TOKEN.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Documentation added or updated, or N/A with justification.
  • Pre-commit checks pass locally (run on the changed files).
  • New source files include the required Apache-2.0 SPDX header.
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

🤖 Generated with Claude Code

d-molinari and others added 3 commits November 24, 2026 09:53
…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]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 25, 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.

@d-molinari

Copy link
Copy Markdown
Contributor Author

Folded into #6746, which now carries this commit unchanged on top of #6746's two, with #6782's after it, rebased onto main. Closing.

@d-molinari d-molinari closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant