Skip to content

feat(example-session-state-mgmt): serve Environment Server sessions - #4058

Open
ananthsub wants to merge 3 commits into
mainfrom
ananthsub/session-state-mgmt-sessions
Open

ananthsub wants to merge 3 commits into
mainfrom
ananthsub/session-state-mgmt-sessions

Conversation

@ananthsub

@ananthsub ananthsub commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Lets the example session-state Resources Server serve Environment Server sessions, and runs it through one. It seeded only an Agent's legacy body, so a typed seed from an Environment Server returned 422.

  • A typed seed reads initial_count from task_data and keys the counter by resources_session_id, binding the session cookie to it so the agent's tool calls and verification find the same counter. Seeding the same session again keeps the existing counter.
  • close_resources_session releases the counter. A seed or tool call that arrives after close gets 409, so a closed session cannot be recreated.
  • configs/example_session_state_mgmt_single_agent_turn.yaml fronts Simple Agent with single_agent_turn_legacy and grants it direct HTTP access to the counter tools. The existing configs, including the nooa one, still run through /run.
  • The server's tests run the shared session contract check, nemo_gym.testing.session_conformance.check_resources_session_contract.
  • The Stateful Environment tutorial told servers to overwrite state on a repeated seed. A repeated seed for the same session is a retry of the same episode, so it must keep the session; the tutorial now says so and shows the example's typed seed and close.

This example makes a strong end-to-end check of session plumbing: its counter starts at the task's initial_count, so a reward of 1.0 needs the seed, the agent's tool calls, and verification to reach the same Resources session.

Issue

No issue is linked; this is part of the Environment Server work and builds on #3784, now on main.

Validation

  • pytest resources_servers/example_session_state_mgmt/tests -q: 3 passed. They cover the shared contract check and a lifecycle test that seeds through the typed route, counts from the seeded value with the seed's cookie, closes, and checks that a late tool call and a late seed both get 409.
  • pytest tests/unit_tests/test_session_contract_repo.py tests/unit_tests/test_task_data.py -q: 365 passed.
  • gym env resolve on the new config resolves to single_agent_turn_legacy fronting the session agent.
  • cd fern && npm run check: 0 errors, 1 existing warning.
  • pre-commit run on the changed files.

Rollouts

gpt-5.6-sol through the new config on this branch, on the example dataset after gym dataset collate: all five tasks scored 1.0, and every count included the seeded start (for example 3 seeded + 1 + 2 = 6). Earlier, with no tool grant, the same tasks scored 0.0 because tool calls landed in fresh sessions; Simple Agent now fails such episodes instead.

Compatibility

No change to the legacy /run path, its request or result shapes, or existing configs.

@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team October 5, 2026 18:35
@yaoyu-33 yaoyu-33 added area:environment Individual environments, benchmarks, verifiers, and environment-specific resources servers complexity:low Localized change in one scope with a small, straightforward review surface feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer labels Oct 5, 2026
@ananthsub
ananthsub force-pushed the ananthsub/session-state-mgmt-sessions branch from 399e05a to 04a97ce Compare October 5, 2026 21:35
The example seeded only an Agent's legacy body, so a typed Environment Server
seed returned 422. It now accepts a typed seed, reading initial_count from
task_data and keying the counter by resources_session_id, and closes through
close_resources_session. A closed session cannot be recreated by a late seed or
tool call. The legacy /run path is unchanged.

Signed-off-by: Ananth Subramaniam <[email protected]>
…nvironment Server

Adds a config that fronts Simple Agent with single_agent_turn_legacy and grants
it direct HTTP access to the counter tools, and checks the server with the
shared session contract check. Both needed Simple Agent sessions and
nemo_gym.testing from #3784.

Signed-off-by: Ananth Subramaniam <[email protected]>
…e Environment Server sessions

The tutorial told servers to overwrite state on a repeated seed. A repeated
seed for the same session is a retry of the same episode, so it must keep the
session. The tutorial now also shows the example's typed seed and close.

Signed-off-by: Ananth Subramaniam <[email protected]>
@ananthsub
ananthsub force-pushed the ananthsub/session-state-mgmt-sessions branch from 04a97ce to d1ae09b Compare October 5, 2026 22:42
@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team October 5, 2026 22:43
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

@zyzhou5 zyzhou5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. The only thing is that I found the naming of single_agent_turn to be a bit confusing especially when we aim to distinguish between legacy path and the new path.

@yaoyu-33 yaoyu-33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head d1ae09b39c8108d385322b93173f33955c49a886 against main at c6cad9e52.

Already on main

  • This example already serves typed sessions, but only through the NOOA subclass. NOOAStatefulCounterResourcesServer in nooa_app.py (#3846) has a typed seed with an identity check, a close that records the closed ID in ClosedResourcesSessionRecords, and a 409 for tool calls on a session that isn't open. It is wired by example_session_state_mgmt_nooa.yaml (single_agent_turn + nooa_agent).
  • The base app.py is still legacy-only. It now declares checkpoint_mode = "exported" with export, restore and retire hooks.
  • The contract helpers have grown since this PR opened (#4679, #4692): ResourcesSessionFence, ResourcesSeedRejectedError (422), ResourcesSessionConflictError (409), ResourcesSessionLostError (404) and lost_session_verification. check_resources_session_contract now also checks conflicting seeds and closes, and optionally the masked verify after close.

What this PR still adds

  • A typed seed and close on the base server, so any Environment Server and agent can use it without the NOOA subclass.
  • example_session_state_mgmt_single_agent_turn.yaml, which runs the example as single_agent_turn_legacy + simple_agent over direct HTTP. Main has no config like it.
  • The tutorial fix. stateful-environment.mdx on main still recommends overwriting the counter on a repeated seed, which contradicts the idempotent-seed rule.
  • Conformance and lifecycle tests for the base server.

Contract gaps. I ran this PR's app.py, with its imports moved to main's paths, through main's check_resources_session_contract:

  1. No conflict check. A seed with the same resources_session_id and another episode returns 200, because of setdefault. The close doesn't compare episode_id either. The PR's own test_follows_the_session_contract (keeps_state=True) fails on main at that step. Record (episode_id, task_id) per session and raise ResourcesSessionConflictError, as example_single_tool_call/app.py does.
  2. Unbounded closed set. Closed IDs live in a set that is never pruned. ResourcesSessionFence(session_close_retry_window_seconds) bounds it and also serializes seed and close.
  3. Malformed task_data returns 500. StatefulCounterSeedSessionRequest.model_validate(body.task_data) raises a bare ValidationError, so the Environment Server reruns the whole episode. Wrap it in ResourcesSeedRejectedError (422).
  4. Lost sessions.
    • A tool call after close gets 409 rather than ResourcesSessionLostError (404).
    • A tool call for a never-seeded session still creates a counter via setdefault(..., 0).
    • /verify after close returns reward 0.0 unmasked; it should return lost_session_verification(...).
    • Passing verify_body= to the conformance check covers the verify case.
  5. Cookie bound before the closed check. The typed seed sets request.session[SESSION_ID_KEY] before raising for a closed ID, so a refused reply can carry that session's cookie. Bind it after the checks pass.
  6. Two implementations after rebase. nooa_app.py overrides seed, close and the tools with its own records. Consider moving one fence-based typed seed and close into app.py and letting nooa_app.py inherit it, rather than keeping both.

Needs a rebase.

  • app.py and tests/test_app.py conflict with main.
  • The imports use modules that no longer exist: nemo_gym.base_resources_server, nemo_gym.server_utils and nemo_gym.episode_types are now nemo_gym.server_runtime, nemo_gym.client and nemo_gym.core.
  • ray_enabled = False was dropped on main (#4470), and the rebase should keep main's checkpoint hooks.
  • The tutorial merges cleanly, but its snippet should show the fence and the conflict checks once they're in.

This branch was previously deployed

1 inactive deployment
public — d1ae09b3 Deployed Oct 5, 2026 by copy-pr-bot[bot] via release / finalize / notify #4357
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 complexity:low Localized change in one scope with a small, straightforward review surface 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.

3 participants