Repository navigation
feat(swebench): checkpoint the task sandboxes of episode sessions - #5266
Conversation
bf99bf3 to
e4a4266
Compare
e4a4266 to
8d8e770
Compare
8d8e770 to
b8962b1
Compare
| task = asyncio.ensure_future(self._release_expired(session_id)) | ||
| task.add_done_callback(lambda done: done.cancelled() or done.exception()) | ||
|
|
||
| async def _release_expired(self, session_id: str) -> None: |
There was a problem hiding this comment.
An identical retry refreshes seeded_at while it still holds the fence, but this task was already scheduled from the check before that refresh and does not look again. It also does not take session.lock. Can it stop the sandbox the retry just returned, and fence the id so the next seed is 409? It also looks able to stop a sandbox while an export is snapshotting it.
There was a problem hiding this comment.
This no longer applies at the current head. The file this comment is on (sandbox_owner.py) and its background reaper are gone: the PR is now a subclass of main's SandboxSessionResourcesServer.
- Stopping a sandbox a retry just returned. Expiry is the inherited sweep,
_close_abandoned_task_sessions. It takes the session lock and checks the record and the deadline again under it before it stops anything. The deadline is set once at seed time withsetdefault, so a retry does not move it. - Stopping a sandbox an export is snapshotting. Every stop of a task sandbox goes through
_stop_released_task_sandbox, which takes the same per-session export lock the commit holds while it snapshots. Two tests cover both orders: a commit that waits for a stop in progress, and a close that waits for a commit in progress.
b8962b1 to
5fc8ea3
Compare
5fc8ea3 to
f419b17
Compare
Remove the before_agent_close restore policy: its seed-reply header and
reader, the participant attribute, the class setting and its startup check,
and the changes in single_agent_turn, Simple Agent and Tool Simulation
Agent. A server whose grader needs what a snapshot does not keep runs
restart_only. The mark_restart early return added for the policy goes too.
Saved format: a session record has a version and its sandboxes by name
("task" for the task sandbox); the checkpointer key is
"<session ID>/<name>".
Commit: validate captures and task state before any snapshot, release each
session's export lock once its own snapshot is done, and leave out a
session past its deadline that no request has swept yet.
Restore: put the task state back before building the seed reply. After a
failed install, hand the forks to the checkpointer's new discard(), so a
retry is not refused when a fork could not be stopped.
A task sandbox started without an episode seed, such as for the task row
an agent's /run sends, is no longer registered for checkpoints. The server
reports the session through the new ResourcesParticipant.restart_session:
its seed reply carries the restart header, prepare lists its episode as a
restart, and a commit leaves it out.
A seed with another model than the declared task_model is refused.
Conformance kit: checkpoint before verify captures the patch, check the
restored and forgotten task state, and with verify_body compare the
server's own /verify after a restore with a run without the crash.
Also: _stop_released_task_sandbox is now _stop_unclaimed_task_sandbox,
checkpoint_settings is no longer exported, new lines use "X | None", and
private helpers follow the public API.
Signed-off-by: Pranav Thombre <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
/ok to test c277640 |
Conflict in nemo_gym/server_runtime/__init__.py: both sides added names to the public API list; both are kept. Signed-off-by: Pranav Thombre <[email protected]> Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
/ok to test 850c812 |
macandro96
left a comment
There was a problem hiding this comment.
One remaining P2 checkpoint-recovery correctness issue, reproduced on the current head.
| and not record.expired | ||
| # Past its deadline the provider may have ended the sandbox, and the sweep that marks the session expired | ||
| # runs only when a request arrives: without this, one such session would fail the commit for all. | ||
| and self._open_task_sessions.get(session_id, float("inf")) > monotonic() |
There was a problem hiding this comment.
[P2] Keep captured resources sessions exportable past the task sandbox deadline
This deadline check also drops a session whose replayable verification has already captured the patch and stopped the task sandbox, but is still grading. The inherited expiry sweep skips busy sessions, so the session has not actually expired. Nevertheless, export omits its saved capture, and the participant treats the omitted session as released. A checkpoint taken during that grading can therefore lose the state needed to replay verification after a crash.
I reproduced this at 850c812: hold verification after capture and sandbox teardown, move the original task deadline into the past, and export while verification remains busy. The sweep preserves the session, but export returns {}. A capture-only exemption makes the same regression pass through restore and verification.
Please apply the deadline cutoff only to sessions that still require a live task sandbox snapshot, retaining captured sessions with no task sandbox unless the session is explicitly marked expired. Add a checkpoint/restore regression for grading that continues past the original task deadline.
There was a problem hiding this comment.
Fixed in 5788aaa. The deadline now leaves out only a session that still has a task sandbox registered. A session whose verify stopped the sandbox is exported with its capture unless it is marked expired.
Test: test_a_session_still_being_graded_past_its_deadline_is_checkpointed_with_its_capture. A verify is held after its capture and the stop of the sandbox, the deadline is moved into the past, and a checkpoint is taken through the control routes. The session is not expired, the restored server returns the saved capture without capturing again, and nothing is snapshotted or forked. The test fails with the deadline check as it was at 850c812.
|
Re-review at 850c812: c277640 fixes 11 of my 12 threads. The Two points outside the diff:
|
A commit left out every session past its deadline. That also dropped a session whose verify had captured the patch and stopped the task sandbox but was still grading: the expiry sweep skips it while it is busy, so it had not expired, and a checkpoint taken then lost the capture a replayed verify needs. The deadline now excludes only a session that still has a task sandbox to snapshot. Signed-off-by: Pranav Thombre <[email protected]> Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
/ok to test 5788aaa |
…a stopped key without a wait Second review round of the sandbox session checkpoints. - ResourcesParticipant.restart_session() marks the request being handled, not a session ID. The middleware applies the mark when the seed succeeds, to the ID later requests carry, so a seed that fails reports nothing and a later seed on the same cookie leaves an earlier report in place. seed_failed and session_restarts are removed. - SandboxSessionCheckpointer.stop drops a key whose sandbox already stopped without waiting for a turn, so a close cancelled there no longer leaves the session in the checkpointer. - An export checks the snapshot index for unreleased checkpoints once per checkpoint, not once per call: a commit exports each session on its own. - Core tests for discard, and wording: "agent-run seed", and no "free" for a key that can be restored again. Signed-off-by: Pranav Thombre <[email protected]> Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
/ok to test 61ada45 |
…ommit past its deadline The deadline check asked only the registry whether a task sandbox was left to snapshot. A verify that claims the sandbox takes it out of the registry while it still runs and the checkpointer still tracks it, so a commit snapshotted it past its deadline, where a failed snapshot fails the commit for every session. The check now asks the checkpointer too. Signed-off-by: Pranav Thombre <[email protected]> Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
/ok to test 270e04a |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/44631979111 |
Resolve against main: - Drop this branch's earlier copy of the shielded stop (stop_sandbox() in nemo_gym.sandbox.utils and its test): #6410 merged a different version that puts the shield and limit in AsyncSandbox.stop(timeout_s=...). The sandbox package, LentSandboxes, the swebench session base and deepswe_external1 take main's code; OwnedSandboxes stops through AsyncSandbox.stop. - #5266 exports the checkpointer under its old name: its server, conformance helper and test move to OwnedSandboxes in owned_sandboxes, and the public API serves SandboxCheckpointConfig, SandboxCheckpointError and checkpoint_state_dir from there. No release shipped the old name. - The glossary keeps main's sandbox lease and capabilities rows beside the owner and lend/borrow rows. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: yaoyu-33 <[email protected]>
What changed and why
A resources server whose episode seed starts a task sandbox that the agent borrows (SWE-bench and the rest of the SWE family) already has its sessions on
main:SandboxSessionResourcesServerseeds the session, lends the sandbox, captures the patch once at verify, closes, fences late requests and expires sessions nobody closed. What it cannot do is take part in checkpoints. Nothing saves the task sandbox, so after a crash the episode starts over.CheckpointedSandboxSessionResourcesServeradds the checkpoint half as a subclass, on #5265'sSandboxSessionCheckpointer, and leaves the session half as it is onmain. An earlier version of this PR had a session base of its own;mainhas gained the session base since, so that is gone.new_task_sandbox(session_id, spec). A subclass starts each task sandbox through it: the sandbox goes on the provider every session shares and is registered for the session and its checkpoints. A commit never snapshots a sandbox a stop is releasing, and it fails for a task sandbox that was registered any other way./runsends, is not registered for checkpoints. While the server exports sessions, that seed's reply carriesx-ng-checkpoint-restart, prepare reports the episode as a restart, and a commit leaves the session out. Close, expiry and shutdown still stop its sandbox.task_model(the model of its episode seed'stask_data) and keeps what a restore needs through_export_task_sandbox_stateand_restore_task_sandbox_state. A seed that passes another model thantask_modelis refused. A restore puts the task state back before it builds the session's seed reply. What the server passes tocapture_task_sandbox_patch, and its task state, must be JSON values.version: 1andsandboxes, the checkpoint state of each of the session's sandboxes by name; the task sandbox is"task". The checkpointer tracks each sandbox as<session ID>/<name>. A restore refuses a version it does not know, and a sandbox name it does not restore.exportedonly when the provider can snapshot; otherwiserestart_only, with nostate_dirneeded. A server whose grader needs something a snapshot does not keep, such as processes the agent started, declarescheckpoint_mode = "restart_only".task_modelfails at startup when the server exports sessions.Changes outside
resources_servers/swebench:ResourcesParticipant.restart_session(): anexportedserver calls it from the handler of the request that starts a session it cannot export. If the request succeeds, the seed reply and prepare say the session's episode is a restart, and a commit leaves it out. It uses the existingx-ng-checkpoint-restartheader.SandboxSessionCheckpointer.discard(keys): gives up the sandboxes ofkeysand stops them. Every key can be restored again at once, whether or not the stops succeed.SandboxSessionCheckpointer.stop(key)drops a key whose sandbox already stopped without waiting for a turn, and an export checks the snapshot index for unreleased checkpoints once per checkpoint.nemo_gym.server_runtimeexportsSandboxSessionCheckpointer,SandboxCheckpointConfig,SandboxCheckpointErrorandcheckpoint_state_dir.Conformance kit.
check_sandbox_session_checkpoints, inresources_servers/swebench/testing/checkpoint_conformance.pynext to the class, checks any server on the subclass against fake providers: commit, crash, restore into a fork, a restored session's seed and saved capture, a restore whose fork fails, release of dropped snapshots, close of a running sandbox, shutdown, andrestart_onlywithout snapshots. It also takes a checkpoint before verify captured the patch, which is the usual case: the restore must install the task state the commit exported, a retire must stop the fork and forget that state, and, given the/verifybody for the seed (verify_body), the server's own/verifyafter the restore must reply as it does without the crash. It is not exported fromnemo_gym.server_runtime.End-to-end tests. A fake sandbox backend with snapshots and a small notes server on the subclass that Simple Agent can drive, through both environment-server variants, with and without a provider that can snapshot.
This rebuilds the checkpoint half of #4576 and the end-to-end infrastructure of #4221 (earlier stack) on
main's episode sessions.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, resume, restore,<br/>retire, forget, release"] E["Environment server<br/>bookmarks, re-seed after a restore"] A["Harness agent, e.g. OpenCode<br/>run until parked, re-bind, resume"] S["Process supervisor in the task sandbox<br/>pause, continue, stop"] R["Resources server, e.g. SWE-bench<br/>owns and lends the task sandbox"] OB["Session base with checkpoints<br/>export, restore, release"] K["Sandbox checkpointer<br/>snapshot index, release, gc"] P["Sandbox provider, e.g. OpenSandbox<br/>snapshot, fork, delete, list"] M["Policy model<br/>held calls, capture ledger"] D[("Checkpoint directory<br/>and state_dir")] C --> CO --> E & A & R & M E -- "seed, re-seed" --> R E -- "seed, re-seed, activation" --> A R -. "sandbox_access" .-> A A --> S R --- OB --> K --> P A -- "model calls" --> M CO --> D classDef this fill:khaki,stroke:darkgoldenrod,stroke-width:2px,color:black class OB thisThis PR
One session, from seed to close, with a checkpoint during the agent's work, a crash, and a restore.
Where this sits in the stack
This is one PR in a stack for checkpointing environment-server episodes whose agent harness runs in a sandbox the resources server lends (first pair: OpenCode and SWE-bench on OpenSandbox; #5410 to #5413 add AgentENV, where OpenCode resumes in place after a restore). #5264 and #5265 have merged, and the rest of the stack is rebased onto
mainafter them. Each PR is based on the one before it, so each diff shows only that PR's change. The stack runs parallel to thelegacy_agentpath of #4220 to #4936, which it does not change or depend on.Issue
No tracking issue exists. This implements the environment-server plan for checkpointing sandboxed harnesses, on top of the merged partial-rollout checkpoint stack (#3882 to #3909, #4997).
Validation
Run on c277640, on a Slurm node. The head (850c812) adds a merge of
maininto the branch; it conflicted in one place, the public API list ofnemo_gym.server_runtime, where both sides added names and both are kept. These suites were not rerun on the merged head, which CI tests.pytest resources_servers/swebench/tests/test_sandbox_session_checkpoints.py resources_servers/swebench/tests/test_sandbox_sessions.py: 100 passed.pytest tests/unit_tests/server_runtime tests/unit_tests/test_public_api_facades.py tests/unit_tests/test_moved_paths.py environment_servers/single_agent_turn/tests: 3393 passed, 3 failed, 2 collection errors. The failures and errors are from the test environment: itsmcppackage is older than the onemainpins (test_mcp_auto_exposure.py,test_review_boundary_interfaces.py) andcloudpickleis not installed (test_base_environment_server_with_tools.py). None is in a file this PR changes.pytest responses_api_agents/simple_agent/tests responses_api_agents/tool_simulation_agent/tests: 189 passed.NEMO_GYM_CHECKPOINT_E2E=1 pytest tests/e2e/checkpoint/test_checkpoint_sandbox_e2e.py: 6 passed. These start real Gym processes against a fake snapshot backend: commit then resume, crash then continue in a fork, release, and restart on a provider without snapshots, through both environment-server variants. They are skipped without the flag, and no CI workflow sets it, so they were run by hand.tests/unit_tests, the OpenCode agent, the SWE-bench and SWE-rebench servers, both single-agent-turn environment servers): 25000 passed, 180 failed, 13 errors. The previous head (932596c) in the same environment: 24990 passed, 179 failed, 13 errors. The failing tests are the same ones and come from the environment (mostly no/usr/bin/python3in the container, missingmlflow,PILandcloudpickle, the oldermcp, and a tree without a git checkout), excepttest_docker_pty.py::test_the_wait_for_a_missing_record_is_bounded_with_an_integer_only_sleep, a timing test that failed in this run only and is in no file this PR changes.swe_next,swemer_v1,swemer_v2,scale_swe,genrm_compare,example_session_state_mgmt,workplace_assistant,gymnasium, Simple Agent and Tool Simulation Agent: passed.lint-imports: 2 contracts kept.ruff check .andruff format --check .(ruff 0.9.9, the pre-commit version): clean.pre-commititself was not run._release_task_sandbox, the check after a commit takes its locks, the deadline check, its exemption for a session without a task sandbox, its hold on a sandbox a verify claimed, the order of a restore's install, the per-session lock release,discard, the restart report of a session without an episode seed, the task model check, and the two participant checks, the report of a session by its request (a seed that fails, and a later seed on the same cookie),stopof a key whose sandbox already stopped (in the core suite and through a close), the index check once per checkpoint, anddiscardin the core suite.What the tests of the subclass cover: the conformance kit (attempts 0 and 7, with
verify_body), the kit refusing three servers that lose or keep task state, a restored session that continues with its task state, a session restored after its capture, the saved format and its version, a commit and a stop, close, capture or release in either order, per-session lock release, a snapshot that fails once or always, expiry and a deadline no request swept, a session still being graded past its deadline, retire, a session seeded without an episode seed, a restore that fails after forking and its retry, a restore cancelled by a task cancel and through a cancel scope, invalid restore input, the startup check, and thestate_dirrequirement with and without snapshots.Compatibility
main's session base gains one hook,_stop_unclaimed_task_sandbox, which its_stop_task_sandboxcalls after waiting for a claimed sandbox to be released; its behaviour and the existing resources servers are unchanged.restart_sessionuses the existingx-ng-checkpoint-restartheader.checkpoint.state_dirwhen checkpointing is on and its provider can snapshot.verify_body, and therestore_policytext in test(checkpoint): end-to-end harness scenarios, SWE-bench recipe, docs #5269's tutorial and therestore_policychange in feat(checkpoint): hold-pause a harness when the sandbox snapshot keeps its processes #5411 are dropped.🤖 Generated with Claude Code