Skip to content

feat(checkpoint): export Blackjack game state for partial-rollout checkpoints - #3896

Merged
cmunley1 merged 2 commits into
mainfrom
ananthsub/partial-ckpt-blackjack
Oct 20, 2026
Merged

cmunley1 merged 2 commits into
mainfrom
ananthsub/partial-ckpt-blackjack

Conversation

@ananthsub

@ananthsub ananthsub commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

Blackjack is the first Gymnasium environment to opt in to checkpointing.
Its resources server now exports a live game, so an agent that can continue an episode can resume the game after a crash instead of dealing again.
gymnasium_agent, the agent Blackjack runs with today, declares no checkpoint session support, so it takes part as restart-only and a game a checkpoint catches still starts over from its input.
The export becomes useful end to end once that agent can continue an episode.

The rest of a Blackjack game depends only on the player's hand, the dealer's hand, and the session's random number generator. Blackjack sets checkpoint_mode = "exported" and overrides the two Gymnasium hooks:

  • export_env_state writes both hands and the generator state (random.Random.getstate()) as JSON.
  • restore_env_state checks that the hands are lists of card ranks, rebuilds the generator with setstate, and raises ValueError for any malformed state. Nothing is installed if any session is invalid.

The Gymnasium base handles everything else: tracking sessions from /reset to the terminal /step, exporting every live session at commit, and closing a retired session.

  • A step for a game that is gone. Retire now stops and frees a session instead of fencing it, so once the controller forgets the rollout, a late /step can reach the server; until then the retire's mark refuses it with stale_attempt. Blackjack answers a step for a session with no game with 404, instead of crashing on an empty hand. The same crash exists on main.

How it works

Where this PR sits in the overall flow

The highlighted part is what this PR adds.

flowchart LR
  C["Controller<br/>NeMo RL, or rollout collection"]
  CO["Coordination<br/>prepare, commit, restore, resume,<br/>retire, forget"]
  K["Control plane on every server<br/>phases, lease, storage,<br/>retire: refuse, stop, free;<br/>forget"]
  subgraph G["One participant per Gym server"]
    E["Environment server<br/>episode steps"]
    M["Policy model<br/>held responses, generation cuts"]
    A["Agent<br/>sessions parked at boundaries"]
    R["Resources server<br/>session state"]
  end
  W["Inference worker<br/>stages cut prefixes"]
  D[("Checkpoint directory<br/>records, then manifest")]
  L[("Capture ledger<br/>retire and delete from #3938, #3939")]
  C --> CO --> K
  K --> E & M & A & R
  M --> W
  M --> L
  G --> D
  classDef this fill:#fde68a,stroke:#b45309,stroke-width:2px,color:#1f2937
  class R this
Loading

One checkpoint, a crash, and the restore, end to end:

sequenceDiagram
  participant C as Controller
  participant G as Gym participants
  participant D as Checkpoint directory
  C->>G: prepare, in order environment, model, agent, resources
  Note over G: admission closes, in-flight work parks at a boundary,<br/>undelivered model responses are held
  G-->>C: prepared, or blockers at the deadline
  C->>G: commit with the episodes the controller continues
  G->>D: each participant writes its records, then its manifest
  Note over G: restored state the commit's scope leaves out is deleted
  C->>C: publish the checkpoint with the controller's own state
  C->>G: resume, in order resources, agent, model, environment
  Note over C,G: crash - every Gym process dies
  C->>G: restore the checkpoint in fresh processes, all or nothing
  D-->>G: records installed under attempt N+1, attempt N's capture ledger retired
  C->>G: resume
  C->>G: /run as attempt N+1 continues each episode from its boundary
  Note over C,G: retiring an episode, only while no checkpoint is open
  C->>G: retire - environment, then agent, then model and resources
  Note over G: each server marks the attempt, stops its work, waits, frees its state, then replies
  C->>G: forget, once the rollout is done for good - same order
  Note over G: the mark refusing the retired attempt goes away
Loading

This PR

The game state that decides the rest of a hand, saved at a checkpoint and restored into a fresh server.

sequenceDiagram
  participant A as Agent
  participant B as Blackjack
  A->>B: /reset deals the hands
  A->>B: /step hit
  Note over B: commit - export_env_state saves the player's hand,<br/>the dealer's hand, and the random number generator state
  Note over A,B: crash, then restore into a fresh server
  Note over B: restore_env_state checks the hands and rebuilds the generator
  A->>B: /step stand
  B-->>A: the same outcome as an uncheckpointed game, including the dealer's draws
Loading

Where this sits in the stack

This PR uses the resources server checkpoint participant from #3886, which has merged, and needs nothing else from the open part of the partial-rollout checkpointing stack. It is one of five PRs that port the environments the old stack checkpointed onto the new hooks. Workplace Assistant and the Gymnasium fix target main, and Blackjack builds on the Gymnasium fix. Indirect prompt injection and proof refinement are still based on the end-to-end suite (#3889):

Relationship to the old stack

Ported from #2946 onto the v2 checkpoint hooks. The old version serialized the same three fields through the Gymnasium base's serialize_session_state and deserialize_session_state, keyed by rollout attempt. Here the state is keyed by the cookie session ID, and restore also validates the hands and turns a bad generator state into a clear error.

Issue

No separate issue. This is the Blackjack part of the partial-rollout checkpointing port.

Validation

Run on this branch, on the current main:

  • RAY_TMPDIR=/tmp python -m pytest -q resources_servers/blackjack/tests: passed. The new tests drive a checkpoint-enabled server through its control routes:
    • With a fixed seed, reset and hit, commit, restore into a fresh server instance, then stand. The restored game's final step equals an uncheckpointed game with the same seed and actions, including the dealer's cards drawn after the restore. Breaking the generator restore makes this test fail.
    • Invalid state written at commit fails the restore, and nothing is installed.
    • Malformed states (missing generator, bad generator state, a hand that is not a list) are rejected.
    • A retired session is closed, and a later step is refused rather than served from a recreated game.
    • A session the server already dropped is left out of the export.
  • RAY_TMPDIR=/tmp python -m pytest -q tests/unit_tests/test_checkpoint_*.py resources_servers/gymnasium/tests responses_api_agents/gymnasium_agent/tests: passed.
  • ruff check, ruff format, and pre-commit run --files on the changed files.

Rollout evidence

Setup

The model was Qwen/Qwen3-4B-Instruct-2507, served by vLLM 0.24.0 with the hermes tool-call parser on one NVIDIA RTX 6000 Ada Generation (48 GB).
Gym's vllm_model server ran with uses_reasoning_parser=false, because the served model is an instruct model without a reasoning parser.
Every rollout sampled at temperature 1.0.
Each tree ran in its own worktree, with server venvs created by gym env start.

Blackjack with checkpointing off

Blackjack ran through gymnasium_agent with resources_servers/blackjack/configs/blackjack.yaml and its example data, on main at 717c04a3b3, on #3895, and with this change on #3895:

gym env start --config resources_servers/blackjack/configs/blackjack.yaml --model-type vllm_model \
    --model Qwen/Qwen3-4B-Instruct-2507 --model-url http://127.0.0.1:8100/v1 --model-api-key dummy \
    ++policy_model.responses_api_models.vllm_model.uses_reasoning_parser=false
gym eval run --no-serve --config resources_servers/blackjack/configs/blackjack.yaml --model-type vllm_model \
    --model Qwen/Qwen3-4B-Instruct-2507 --model-url http://127.0.0.1:8100/v1 --model-api-key dummy \
    ++policy_model.responses_api_models.vllm_model.uses_reasoning_parser=false \
    --agent blackjack_gymnasium_agent --input resources_servers/blackjack/data/example.jsonl \
    --output rollouts.jsonl --num-repeats 20 --concurrency 16 --temperature 1.0
Run Rollouts Mean reward Outcomes
main 100 -0.12 40 wins, 8 draws, 30 losses, 22 busts
#3895 100 -0.20 37 wins, 6 draws, 31 losses, 26 busts
this change 100 -0.29 31 wins, 9 draws, 43 losses, 17 busts

Blackjack deals from an unseeded random number generator, so each run plays different hands and only the distributions can be compared.
A chi-square test on the four outcomes gives p = 0.25 against main and p = 0.18 against #3895, so the runs are consistent with one another.
A game takes one to four model turns in every run.
Every rollout ended with terminated set and no failure, and the server logs show no tracebacks or error responses.

Blackjack with checkpointing on

gymnasium_agent declares no checkpoint session support, so it takes part in a checkpoint as a restart-only agent.
A Blackjack game that a checkpoint catches mid-play is therefore a restart: after a crash it starts over from its input, and the game state this change exports is not used end to end yet.
The run below checks that behavior with real server processes.

The tree was the checkpoint stack (ananthsub/partial-ckpt-e2e) with #3895 and this change cherry-picked on top; both applied without conflicts.
A driver built on the process-level checkpoint suite (tests/e2e/checkpoint/checkpoint_deployment.py) started the policy model, the Blackjack resources server, gymnasium_agent and the flat-row wrapper, each as its own process from its own venv.
It sent 120 games to the wrapper's /run and drove the checkpoint through nemo_gym._checkpoint.coordination, as a training controller would.

  1. When prepare started, 54 games had finished and 66 were in flight.
    The Blackjack server held a dealt game for each of the 66.
  2. Prepare returned in 4 ms without waiting for the games.
    gymnasium_agent reported all 66 as restarts.
    The policy model held their 66 model calls without reporting a blocker, and the Blackjack server reported no request in flight.
  3. The commit scope was empty.
    The Blackjack server's commit wrote no record, although it held 66 live sessions, and the agent and policy model wrote none either.
  4. Every Gym server process group was killed with SIGKILL, fresh processes were started from the same configs, and restore and resume ran with the empty scope.
  5. The controller retired the 66 restarted attempts.
    Every participant then reported them as retired, and the Blackjack server held no sessions.
    A /run of a retired attempt sent straight to the agent was refused with HTTP 409 stale_attempt.
  6. The 66 games ran again from their input as attempt 1.
    All returned HTTP 200 and ended with terminated set: 25 wins, 7 draws, 26 losses and 8 busts, for a mean reward of -0.14.
    The 54 games that finished before the checkpoint had a mean of -0.26, in the range of the uninterrupted runs above.

At the end, every participant was idle and the Blackjack server held no sessions.
While the checkpoint was open, the 66 restarted games did not advance: the policy model held their undelivered model responses until resume, as it does for every call, so none of them finished in the three seconds before the crash.
The agent log shows five truncated model responses at the moment of the kill, which come from killing the policy model while the agent still waited on it.

What is not covered

  • A restored Blackjack game is never continued end to end, because gymnasium_agent restarts every game a checkpoint catches.
    The restored-state equivalence is covered by this change's unit test instead: a restored game deals the same cards and ends with the same outcome as an uncheckpointed game with the same seed.
  • Blackjack's export and restore hooks did not run in the process-level run, because a restart's session is never exported.
    Unit tests cover restore of malformed and invalid state and the release of a retired session.

Compatibility

  • With checkpointing off, nothing changes.
  • With checkpointing on, the Blackjack resources server exports live games.
    Through gymnasium_agent, which is restart-only, a game a checkpoint catches is still a restart and starts over after a crash.
  • Game behavior, data, and rewards are unchanged.

@ananthsub ananthsub added feature New capabilities, enhancements, or enablement work area:environment Individual environments, benchmarks, verifiers, and environment-specific resources servers labels Oct 1, 2026
@copy-pr-bot

copy-pr-bot Bot commented Oct 1, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from cf53406 to b25ea94 Compare October 1, 2026 21:53
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch from 6f93202 to 8b4bb07 Compare October 1, 2026 21:53
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from b25ea94 to c1d3223 Compare October 1, 2026 22:05
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch from 8b4bb07 to 930c857 Compare October 1, 2026 22:05
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from c1d3223 to 8cee79e Compare October 2, 2026 13:22
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch 2 times, most recently from 492a18b to 9a99133 Compare October 2, 2026 19:43
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from 8cee79e to dc25a06 Compare October 2, 2026 19:43
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch from 9a99133 to eb5f7f4 Compare October 2, 2026 21:17
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from 01cfa55 to c3fbf5f Compare October 2, 2026 23:32
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch from eb5f7f4 to f0c480a Compare October 2, 2026 23:32
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from c3fbf5f to 6bd5512 Compare October 2, 2026 23:43
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch from f0c480a to 72115fd Compare October 2, 2026 23:43
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from 6bd5512 to c9f11b1 Compare October 6, 2026 19:28
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch from 72115fd to 7a09ddc Compare October 6, 2026 19:28
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch 2 times, most recently from 69a7f74 to da0629a Compare October 13, 2026 17:55
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch 2 times, most recently from 9a9592c to b5bdf33 Compare October 13, 2026 20:27
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch 2 times, most recently from 620084f to b813fdd Compare October 13, 2026 22:56
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from b5bdf33 to b4bfe35 Compare October 13, 2026 22:56
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch from b813fdd to 936a132 Compare October 14, 2026 16:01
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from b4bfe35 to 4d51770 Compare October 14, 2026 16:01
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch from 936a132 to e8f442b Compare October 14, 2026 17:33
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from 2dd08b3 to 6bccbb0 Compare October 14, 2026 20:36
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch from e8f442b to 185993d Compare October 14, 2026 20:36
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from 6bccbb0 to 4b8212a Compare October 15, 2026 13:21
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch from 185993d to 40d8ba5 Compare October 15, 2026 13:21
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch from 4b8212a to 8a0c514 Compare October 15, 2026 15:41
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch 2 times, most recently from 19d9fd8 to ad43688 Compare October 15, 2026 17:37
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-gymnasium branch 2 times, most recently from 15bfbc3 to cb18c76 Compare October 15, 2026 18:16
@ananthsub
ananthsub force-pushed the ananthsub/partial-ckpt-blackjack branch from ad43688 to 31ee9be Compare October 15, 2026 18:16
…r setup

GymnasiumServer built its own FastAPI app and copied part of the shared
resources server setup, so it never served /reverify_mode and, with
checkpointing on, never installed the checkpoint participant.

Split SimpleResourcesServer.setup_webserver into create_resources_app
(middleware, checkpoint participant, /aggregate_metrics, /reverify_mode)
and register_protocol_routes (/seed_session, /close_session, /verify).
GymnasiumServer now overrides only the protocol routes with /reset and
/step.

GymnasiumServer declares /reset in checkpoint_session_starts, so a
reset passes the same checkpoint admission boundary as /seed_session. A
terminal /step reports the session's end through
checkpoint_session_ended, as does OpenAir's optional /close. Gymnasium
servers stay restart_only unless a
subclass declares checkpoint_mode = "exported"; the base then exports and
restores session_state per session through the overridable
export_env_state and restore_env_state hooks.

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

Blackjack declares checkpoint_mode = "exported". Each session's two hands
and its random number generator state decide the rest of the game, so
export_env_state writes them as JSON and restore_env_state validates them
and rebuilds the generator. A restored game deals the same cards and ends
with the same outcome as an uncheckpointed game with the same seed.

Ported from #2946 onto the v2 checkpoint hooks.

Signed-off-by: Ananth Subramaniam <[email protected]>
zyzhou5
zyzhou5 previously approved these changes Oct 19, 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/39395422621

@ananthsub

Copy link
Copy Markdown
Contributor Author

Rollout evidence rerun on vLLM 0.29.0, the version Gym's vllm extra pins. The evidence in the description ran on vLLM 0.24.0.

The tree was main at dc709bcfc3, which contains this change, #3895 and #3894. The model was Qwen/Qwen3-4B-Instruct-2507, served with the hermes tool-call parser on one NVIDIA RTX 6000 Ada Generation, with uses_reasoning_parser=false and temperature 1.0.

Checkpointing off. 100 Blackjack games gave a mean reward of -0.07: 45 wins, 3 draws, 28 losses and 24 busts. Blackjack deals from an unseeded random number generator, so only the outcome distributions can be compared. A chi-square test gives p = 0.44 and p = 0.57 against the earlier main and #3895 runs. Against the earlier run of this change, which had the lowest mean of the three (-0.29), it gives p = 0.02. Every game ended with terminated set and no failure.

Checkpoint, crash and restore. The same driver as in the description, built on tests/e2e/checkpoint/checkpoint_deployment.py, sent 120 games and cut a checkpoint with 48 finished and 72 mid-play. The results match the earlier run:

  • Prepare returned in 4 ms. gymnasium_agent reported all 72 games as restarts, because it declares no checkpoint session support. The Blackjack server held 72 live sessions and reported no request in flight.
  • The commit scope was empty, and no participant wrote a record.
  • After every Gym process group was killed with SIGKILL and fresh processes were restored and resumed, the controller retired the 72 attempts. Every participant then reported them as retired, and the Blackjack server held no sessions. A /run of a retired attempt sent straight to the agent was refused with HTTP 409 stale_attempt.
  • The 72 games ran again from their input as attempt 1. All returned HTTP 200 with terminated set: 25 wins, 6 draws, 22 losses and 19 busts.

At the end, every participant was idle. The only tracebacks are in the agent log at the moment of the kill, where the agent was still waiting on the killed policy model.

This branch was previously deployed

1 inactive deployment
public — 8e54f1cf Deployed Oct 16, 2026 by copy-pr-bot[bot] via release / finalize / notify #5593
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 sla:review-overdue Review response is over the one-business-day SLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants