How a restored episode now resumes through a typed Gym environment server

NVIDIA-NeMo/RL #5340. NeMo-RL links pin the PR head d421014e (removed code: the base 1a78def0); Gym links pin 9e2e5d0a, the version RL pins.

Before
Turn and prefix recovery refused every typed Gym environment server at setup. Recipes had to swap the server for the legacy_agent relay, or recover whole groups instead.
This PR
Lets typed servers through, gives each saved episode exactly one owner, and re-sends a restored episode under the task number it first ran with.
Status
Works for string_match: the author's 2-GPU run resumed 4 of 4 restored episodes (0 of 4 before the task-number fix). One open problem: servers with an episode cap can stall every snapshot, see the second page.

Three pages on this PR, one question each. This one: what changed and why resume now works.

What “typed” means here. Every Gym environment server subclasses one base class, BaseEnvironmentServer[Req, Resp]. A typed one fills in the request and response types and runs the episode steps itself: single_agent_turn, single_agent_turn_legacy (typed, despite its name), harbor, and others. The only untyped one is the legacy_agent relay (and misalignment_monitored, built on it). It forwards /run to an agent without reading it, and the agent runs the episode. There is no flag: the server type the config names decides it. Full list and picture: Gym server roles.

Background. Dotted words have a short explanation: hover or tap them. New to Gym's server kinds? Start with Gym server roles.

The PR removes two refusals: a setup check (_reject_typed_environment_servers, at the base) and a refusal of environment participants in discover (at the base). The checkpoint summary now counts an environment server's episodes as its own (_commit_summary). That alone was not enough: Gym checks that a resumed episode is the same task (begin), and a row with no task ID gets its ID from the number _ng_task_index (materialize_task). RL gave every dispatch a new number, so Gym refused every resume.

Who owns the episode, and why resume used to fail

① Who saves the episode at a checkpoint (row r7) legacy_agent relay (the only option before) typed server, e.g. single_agent_turn_legacy (now allowed) legacy_agent: forwards to the agent's /run not a participant; saves nothing agent: owns r7 (its own /run episode) episode_ids [r7] run_episode_ids [r7] agent.py:885 environment server: owns r7 kind "environment" episode_ids [r7] environment.py:61 agent: holds a session for r7, does not own it episode_ids [r7] run_episode_ids [] RL _commit_summary: one owner each ② Restore, then re-send r7's row to Gym first send: {"question": "…", "_ng_task_index": 7, "_ng_rollout_id": "r7"} Gym saves with the episode: task_id "7" → task_digest D7 (keys starting _ng_ are left out) without the PR's fix with the fix RL takes a new number for every dispatch "_ng_task_index": 12 → task_id "12" → D12 "_ng_rollout_id": "r7-a1" (restored attempt) a continued row keeps the number it ran with "_ng_task_index": 7 → task_id "7" → D7 _nemo_gym_task_indices Gym begin(): D12 ≠ D7 "continues a checkpoint recorded for a different task" → RL starts r7 over Gym begin(): D7 = D7 r7 resumes at its saved turn environment.py:108 works fails not a checkpoint participant Only the environment kind checks the task; agents don't.

The two paths meet at the checkpoint summary: after it, a saved episode has one owner and goes through the same restore. What still differs after that point: a typed server checks the task when an episode resumes (so the number must match), and a typed server can limit how many episodes run at once. That limit is the open problem on the second page. The fix only matters for rows with no task_id, problem_id or instance_id of their own, like string_match's.

The dispatch change, as pseudocode

Before (base)

# rollout manager, one prompt group, on the driver's event loop
rows = build_inputs(sample, task_index=take_task_index())  # one new number, every row
ledger.mark_group_dispatched(group)                         # number not saved
send(rows)

After (this PR)

# rollout manager, one prompt group, on the driver's event loop
idx, fresh = {}, None
for g in pending:
    a = group.siblings[g].current_attempt
    if a.environment_attempt > 0 and a.environment_task_index is not None:
        idx[g] = a.environment_task_index                 # continued: keep 7
    else:
        fresh = fresh or take_task_index(); idx[g] = fresh  # new or restarted: share 12
rows = build_inputs(sample, task_indices=idx)
ledger.mark_group_dispatched(group, environment_task_indices=idx)  # saved (snapshot schema 9)
send(rows)                                                  # a continued row whose number changed is refused

Real code: _nemo_gym_task_indices, its call in _generate_for_finalization_attempt, and the saved field environment_task_index. Old snapshots (schemas 2–8) still load, without the number.

So what. Recipes can keep their typed Gym servers with rollout_recovery.target_level: turn or prefix; no more swapping to legacy_agent. The review asks for one unit test that checks the dispatch path above really passes the kept number to both the rows and the ledger: removing either still leaves every CPU test green. Before using this on a server with max_concurrent_episodes set, read the second page.

🟡 review comment C2 — rollout_manager.py:3288 (missing wiring test)

🔴 review comment C1 — capped servers (reply on gym_checkpoint_adapter.py:190)

🟡 review comment C3 — older-schema comment (reply on rollout_recovery.py:2074)