Skip to content

feat(openenv): serve the typed resources sessions API - #5483

Merged
yaoyu-33 merged 36 commits into
mainfrom
yuya/gymint-176-openenv-sessions
Nov 13, 2026
Merged

yaoyu-33 merged 36 commits into
mainfrom
yuya/gymint-176-openenv-sessions

Conversation

@yaoyu-33

@yaoyu-33 yaoyu-33 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Keep OpenEnv on the three state hooks and migrate verification to the shared public context. Seed/close/retirement and typed ownership now come from #5709, with the MCP annotation fix inherited from #5708.

The shared lifecycle in #5709 and MCP support in #5708 are merged into main. The child diff contains only this environment and its related documentation/configuration; shared implementation is reviewed once in the dependency. No separate issue is needed for this focused migration of an existing resources server.

  • Publish the environment handle before reset so shared failed-initialization cleanup can release it. An initialization flag prevents tools from serving partially reset state.
  • Keep reset/step behavior and terminal scoring. Failed cleanup retains the same environment and identity for retry; verification snapshots preserve measured outcomes and close prevents reconstruction.

Retains the accumulated reward (retain(reward)), including 0.0.

After verification completes, pending cleanup no longer permits new tool calls. The environment handle stays owned until close/retirement succeeds.

Validation on e075d206943cbb233e7f92d951167330f3d927a4, Python 3.13.14, with this checkout first on PYTHONPATH and NEMO_GYM_EXTRA_ROOTS pointing to it:

Real-model smoke on preceding migration revision 45ee9d2c6c810360f55f8b44bd25c5867bb95c6b (shared core 4e92d4891) used nvidia/nemotron-3.5-lightning-30b-a3b, Python 3.13.14 and OpenEnv Echo/Maze environments. Echo completed two model calls and scored 0.0; Maze executed eight steps, reached the harness step cap without a final answer, and scored -1.7. Both produced real tool outputs, unmasked verification responses and successful lifecycle cleanup, with environment/identity maps empty. Scores are retained as observed, not treated as task-success claims. Loaded source hashes matched that revision; app.py (verified-session tool rejection) and the shared lifecycle changed afterwards and are covered by unit tests only.; these two tasks do not establish full benchmark parity.

These targeted checks do not replace the repository CI matrix or approval of the current head.

Compatibility

A tool call without a live session is now 404 session_lost (main: 200 {"error": ...}); /verify without a live session is now a masked sample (main: reward 0.0); a tool call after /verify is 404; a cookie seed over a typed ID is 409. The shipped configs use legacy_agent, i.e. the cookie seed; the typed path is unit-tested only, and the smoke used the cookie seed.

Main refresh validation

Merged main at acb67734c into this branch, retaining main's MCP annotation resolution and seed-validation regressions. The diff against main contains only this environment and its related documentation/configuration. No new environment behavior was added by this refresh.

On workstation ws (Python 3.13.14), this environment's complete test directory passed: 60 passed. The nine environment suites passed 904 tests in total. Shared lifecycle, MCP seed/exposure, and process-supervisor tests passed 213 tests. Tests ran in a combined validation checkout with the merged core and the exact environment sources from these nine heads, with the checkout first on PYTHONPATH and NEMO_GYM_EXTRA_ROOTS. Prior real-model smoke evidence above remains historical; no fresh model rollout was run for this merge-only update. New merge commits are DCO signed off.

pre-commit run --all-files passed on the combined checkout, including Ruff, formatting, guardwords, config flags, environment table generation, and import layering. The generated README row matches the row already included in #5485. git diff --check passed for each refreshed branch.

An Environment Server such as single_agent_turn seeds a resources server
with a typed ResourcesSeedSessionRequest and releases it with
/close_session. The OpenEnv adapter took only the legacy seed body,
answered with a response the Environment Server rejects, and had no
close, so an episode that never reached /verify leaked its environment
instance.

The server now:
- accepts the typed seed next to the legacy body, keys the environment
  instance by resources_session_id and binds the cookie to it; a repeated
  seed for the same episode keeps the instance, another episode gets 409;
- closes the instance by resources_session_id, with or without the
  cookie, unless /verify already did; closing an unknown or closed
  session succeeds, a late seed is fenced with 409, and an env.close()
  failure raises so the close can be retried;
- answers a tool call (/step or a per-tool route) for a session it no
  longer holds with ResourcesSessionLostError (404), and a verify for it
  with a masked lost-session sample instead of a reward of 0;
- closes an environment instance whose reset() fails during a seed.

/verify still closes the instance, since a legacy /run never closes its
session. The README describes the session lifecycle and no longer claims
a verifier_metadata.expected_output check that the code does not do.

Refs GYMINT-176

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@yaoyu-33 yaoyu-33 added feature New capabilities, enhancements, or enablement work area:environment Individual environments, benchmarks, verifiers, and environment-specific resources servers labels Nov 3, 2026
@copy-pr-bot

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

@yaoyu-33

yaoyu-33 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 9202a48

@yaoyu-33
yaoyu-33 marked this pull request as ready for review November 3, 2026 18:33
@yaoyu-33 yaoyu-33 added the needs-review PR is ready for code review and waiting on a reviewer label Nov 3, 2026
yaoyu-33 and others added 3 commits November 3, 2026 11:05
…nge a retried verify

A typed session keeps its environment until /close_session, so a step still in flight when the Environment Server verified could add to the accumulated reward and make a retried verify report a different score. Verify now marks the episode done; later steps get the existing "Episode has ended" error.

Also points the conformance import at nemo_gym.server_runtime.testing, fixes the close docstring, and makes two tests check what their comments claim.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@yaoyu-33

yaoyu-33 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 2cdcb92

weijiac0619
weijiac0619 previously approved these changes Nov 4, 2026
@weijiac0619

Copy link
Copy Markdown
Contributor

/ok to test 5cebd02

yaoyu-33 and others added 2 commits November 5, 2026 23:17
…; correct the close wording

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@yaoyu-33

yaoyu-33 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test e075d20

The docs now describe only the typed Environment Server path. The cookie
seed still works unchanged; the PR description records that compatibility.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@yaoyu-33

yaoyu-33 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test a61b923

@kajalj22
kajalj22 removed request for a team November 6, 2026 22:51
Base automatically changed from yuya/shared-resources-lifecycle to main November 8, 2026 06:36
@yaoyu-33

yaoyu-33 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 1b63883

@yaoyu-33
yaoyu-33 added this pull request to the merge queue Nov 13, 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/43493425011

Merged via the queue into main with commit 0d6d0ba Nov 13, 2026
101 of 111 checks passed
@yaoyu-33
yaoyu-33 deleted the yuya/gymint-176-openenv-sessions branch November 13, 2026 19:47
tayunvidia pushed a commit to tayunvidia/Gym that referenced this pull request Nov 13, 2026
…Mo#5485)

* feat(newton_bench): serve the typed resources sessions API

An Environment Server such as single_agent_turn seeds a resources server
with a typed ResourcesSeedSessionRequest and releases it with
/close_session. NewtonBench's seed took only its legacy body, so a typed
seed failed validation with 422, and there was no close.

The server now:
- accepts the typed seed next to the legacy body, reads module_name,
  difficulty, system, noise_level and law_version from task_data, keys
  the session by resources_session_id and binds the cookie to it;
  task_data that fails validation or names an unknown module gets 422;
- keeps the session, including its Python worker, when a seed is
  repeated for the same episode, and refuses another episode with 409;
- closes by resources_session_id, with or without the cookie, through
  the existing _close_session (worker process and metadata); closing a
  session that verify, /end_session or the idle sweep already released
  succeeds, and ResourcesSessionFence refuses a late seed with 409;
- answers a tool call for a session it no longer holds with
  ResourcesSessionLostError (404) instead of a 400, and a verify for it
  with a masked sample instead of a 400; a session the idle sweep reaped
  after session_ttl reports session_expired rather than session_lost.

/verify, /end_session and the idle sweep still release sessions as
before, so the legacy /run path is unchanged. A new
newton_bench_single_agent_turn.yaml runs the rows through
single_agent_turn_legacy with Simple Agent's direct HTTP grant, and the
README describes it and the session lifecycle.

Refs GYMINT-178

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* fix(newton_bench): preserve typed state until session close

Signed-off-by: yaoyu-33 <[email protected]>

* fix(newton_bench): remember a swept session as expired for as long as session_ttl

The idle sweep's expiry records lasted only session_close_retry_window_seconds (300 s), while the sweep runs every 600 s with a 1800 s TTL. A client back from a stall more than 5 minutes after the sweep, the case the record exists for, got session_lost instead of the session_expired the README promises. The records now last the longer of session_ttl and the close retry window.

The README also says that a retried typed verify scores the law again. Adds a test that moves the clock past the close retry window (fails on the previous code) and moves the conformance import to nemo_gym.server_runtime.testing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* fix(newton_bench): release verified and retired session state

Signed-off-by: yaoyu-33 <[email protected]>

* test(newton_bench): name verify retry test after metadata cleanup

Signed-off-by: yaoyu-33 <[email protected]>

* fix(newton_bench): accept a /run row that carries its own task_id

The /run seed guard refused any body sharing a key with ResourcesSeedSessionRequest, so a dataset row with a scalar task_id got 422 on every seed. Treat a body as a malformed typed seed only when it names resources_session_id or carries typed episode_id and task_id objects, the rule NVIDIA-NeMo#5480 settled on.

Also import check_resources_session_contract from nemo_gym.server_runtime, which main's public-API test requires; name the session agent in the README's single-agent-turn command (--agent takes an agent, not the Environment Server); and state verify's no-await invariant next to its retry bookkeeping.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* fix(newton_bench): keep a reused /run cookie's session ID

A /run seed that reused a verified cookie minted a new session ID so the previous verify's retry record stayed valid. Like NVIDIA-NeMo#5480 and NVIDIA-NeMo#5483, a cookie seed now keeps its ID and replaces the previous run's state. It also drops that run's verify retry record, so the new run's tool calls and verify are not answered from the finished run.

Add ClosedResourcesSessionRecords.forget() for that, with a unit test; the same change is in each of the four session PRs (newton_bench, workplace_assistant, tavily_search, indirect_prompt_injection), so whichever merges first carries it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* refactor(newton_bench): adopt shared resources lifecycle hooks

Signed-off-by: yaoyu-33 <[email protected]>

* fix(newton_bench): avoid inheriting server sockets in Python workers

Signed-off-by: yaoyu-33 <[email protected]>

* fix(server_runtime): close gaps in the shared resources session lifecycle

- The repo contract test counts a server that implements the state hooks as
  closing through the inherited close_resources_session. newton_bench,
  workplace_assistant, tavily_search and indirect_prompt_injection override
  seed_session but close through the hooks, and failed it.
- Export reject_partial_typed_seed from nemo_gym.server_runtime, so servers
  that override seed_session reuse the shared guard instead of a copy.
- The MCP seed wrapper still recognises a request param annotated as
  Annotated[Request, ...]; keeping Annotated metadata had made it inject a
  second Request and fail with KeyError.
- Name the 409 for a typed seed over a cookie session "cookie-seeded", not
  "legacy", and document that a served seed_session override declares
  request before body.
- Shared tests now cover the MCP-wrapped seed route, close after verify
  (checked against the verified episode), and a cookie seed of a verified
  typed session.
- The add-benchmark skill describes the three state hooks as the default
  for a stateful server.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* fix(server_runtime): retire a hook session without fencing it

The inherited retire_session_state recorded a typed session as closed. The
checkpoint participant also calls that hook to undo a restore
(delete_restored) and to drop an unclaimed session, and both promise to
refuse nothing later; with the fence, a restore that was rolled back and
installed again in the same process left its session fenced, so its verify
was masked as lost and its continuation's seed got 409. The participant
already refuses a retired attempt's requests until it forgets the rollout,
so retirement now releases the state and drops the identity without
recording a close. A close still fences.

Also write down that an exported server using the state hooks carries each
session's identity through its checkpoint itself.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* fix(newton_bench): start workers from a forkserver and keep end_session from dropping the session

Spawned Python workers each re-imported the server: about 2 s and 230 MB per
worker on a laptop, every session's first call paid it inside the
max_execution_time + 5 second response wait, and a spawned worker's
interpreter teardown made close() block the event loop about 250-390 ms.
Workers now fork from a forkserver that preloads the server (__main__ when it
runs as app.py) and the sandbox module once. Workers still hold none of the
server's sockets; a later worker starts and answers in a few milliseconds
with 17 MB RSS, and close() takes about 1 ms. Only the first start in a
process waits for the forkserver's import, so execute_python now starts the
worker in a thread, one start per session at a time, and stops a worker
whose session verify or the idle sweep released while it started.

A model can call /end_session as a tool. It released the task metadata, so
the Environment Server's verify was masked session_lost and the model's own
action dropped its sample from the score. /end_session now stops only the
Python worker; the metadata stays until verify, close, checkpoint retirement
or the idle sweep. The route is also no longer offered as an MCP tool.

Also:
- Use the shared reject_partial_typed_seed guard instead of a private copy.
- A verify after close of a session the idle sweep reaped reports
  session_expired, as the README says, instead of session_lost.
- README: worker start cost and the /end_session behavior.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* fix(newton_bench): mask a judge outage instead of scoring it as not equivalent

NewtonBench's symbolic judge catches every API error and, after three failed
attempts, returns False: the same value as a "not equivalent" verdict. A
missing judge key or a sustained 429 therefore set R_symbolic to -1, so an
exact law with RMSLE 0 scored 0.4 instead of 1.0.

- Refuse to start when NewtonBench cannot resolve an API key for its judge
  model, using NewtonBench's own key lookup (environment and its .env file).
- Record each judge API attempt during verify. When the last attempt raised,
  the judge gave no verdict, so verify raises a JudgeError and judge_failsafe
  masks the sample (mask_sample, failure_kind judge_failed).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* test(newton_bench): give the judge a placeholder key and keep it off the network

The server now refuses to start without a judge key, so tests that build the
app with setup_webserver need one. The placeholder comes with a requests.post
that fails at once, so a test that reaches the judge never calls a real API.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* fix(mcp): preserve annotated seed validators and request injection

Signed-off-by: yaoyu-33 <[email protected]>

* feat(sessions): centralize resources lifecycle and verification ownership

Signed-off-by: yaoyu-33 <[email protected]>

* fix(sessions): preserve cleanup ownership and shared verification semantics

Signed-off-by: yaoyu-33 <[email protected]>

* fix(resources): preserve grades and retry cleanup ownership

Signed-off-by: yaoyu-33 <[email protected]>

* fix(newton_bench): reject tools after verification cleanup fails

Signed-off-by: yaoyu-33 <[email protected]>

* test(mcp): keep a validator annotated directly on the seed body

The wrapper strips Annotated only to recognize an injected Request. A
validator written directly on the body parameter, the form an author is
most likely to write, was covered only through a union arm, so a wrapper
that stripped every parameter still passed.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* fix(resources): keep cancellation and reseed cleanup consistent; document the hook contract

- A cancellation that arrives while a failed initialization is being cleaned up now propagates instead of
  being swallowed by the cleanup guard.
- A cookie reseed that released the preceding run moves its finished receipt into the retry window, so a
  failed replacement no longer leaves a pending-cleanup record with nothing to release.
- Document that a disconnect leaves a verification release pending, that the release hook runs under the
  session lock, that state is per worker with session routing keeping a session on its owner, and the
  override rules for servers that keep their own seed and close.
- Rename the example server's private episode table so it no longer shares a name with the base's.
- Add regressions for the receipt deadline guard, worker ownership wiring, cookie seed races and fences,
  typed seed takeover, restore refusals and a cancelled close waiting on verification.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* fix(newton_bench): serialize exec on a worker pipe and keep a lost session a 404

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* docs(newton_bench): drop the /run seed explanation

The docs now describe only the typed Environment Server path. The cookie
seed still works unchanged; the PR description records that compatibility.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>

* refactor(newton_bench): make the session config the default

Signed-off-by: yaoyu-33 <[email protected]>

* fix(ci): scope release rehearsal changelogs to PR changes

Signed-off-by: yaoyu-33 <[email protected]>

---------

Signed-off-by: yaoyu-33 <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>

This branch was previously deployed

1 inactive deployment
public — 1b638836 Deployed Nov 9, 2026 by copy-pr-bot[bot] via release / finalize / notify #11199
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:environment Individual environments, benchmarks, verifiers, and environment-specific resources servers 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.

2 participants