Skip to content

feat(opencode): resume OpenCode in place on sandboxes whose snapshots keep processes - #5413

Merged
zyzhou5 merged 9 commits into
mainfrom
pranav/envckpt-10-opencode-in-place
Nov 24, 2026
Merged

zyzhou5 merged 9 commits into
mainfrom
pranav/envckpt-10-opencode-in-place

Conversation

@pthombre

@pthombre pthombre commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

On OpenSandbox, a crash restore relaunches OpenCode from its saved conversation (#5268). On AgentENV, whose snapshots keep the sandbox's processes (#5410), the restored sandbox is still running OpenCode mid-task. #5411 and #5412 added the pieces to hold it through a checkpoint and take it over after a restore. This PR wires them into the OpenCode agent, so a restored OpenCode session continues in place: its next model call reaches the new job's model server, and nothing is relaunched.

At launch (only when snapshot_keeps_processes)

Other providers launch exactly as before.

After a crash (the re-bind)

  1. check_restored checks that the restored launch's supervisor is alive with the same identity, still holding the pause, with its relay up.
  2. If it can be taken over, the agent re-points the relay at the new model URL with a higher generation, continues the pause, and takes the launch over with SandboxSession.adopt. The relay re-sends the cut call, and OpenCode sees only a slow model call. A launch that had already ended is collected the same way.
  3. Otherwise, or if the take-over fails at any step, the launch is abandoned (killed, nothing continued) and OpenCode is relaunched from the saved conversation, as on OpenSandbox. A check that could not run is made once more first.

Each outcome is logged so a run can count it. relay_gone (the checkpoint was taken before the launch's relay started) is expected and logged at INFO; a launch resumed in place and every other reason a launch cannot resume (relay_unavailable included, since every image runs the relay) are logged at WARNING.

The wall clock

OpenCode orders a conversation by message IDs it makes from the clock, so a clock that jumps while OpenCode runs can put a message out of order. Gym does not set the sandbox's clock; that is the provider's to keep. On AgentENV, snapshots.keep_guest_clock (#5410, on in the AgentENV config) makes a restored sandbox continue from the clock its snapshot was taken at.

Setting

sandbox_model_relay_port (a fixed default): the sandbox port of the model relay on providers whose snapshots keep processes.

Also adds a SWE-bench Verified benchmark config for OpenCode sessions on AgentENV (benchmarks/swebench/verified/opencode_agentenv.yaml, on top of main's opencode.yaml), and docs for both restore paths.

Found in the review of this PR and fixed here (last commit)

Every path that does not take a restored launch over now ends it or checks it again; before, some left it running beside a relaunch.

  • A take-over that fails at any step ends the restored launch before the relaunch. A failure before the launch was continued (resolving the model URL, pointing the relay) skipped the abandon, because the session had launched nothing itself. The abandon is now skipped only when a positive receipt was read.
  • A launch taken over that ended by itself and then failed raises, as after execute, instead of being run again.
  • A launch taken over whose receipt says stopped (a stop, or an earlier abandon) is relaunched, not reported as finished.
  • A checkpoint between the re-bind and the take-over exports the pause the restored launch still holds, so the next restore checks that launch again instead of relaunching beside it.
  • A restored-launch check that raises or is cancelled undoes the re-bind, so the retried seed decides again.
  • check_failed and a failed abandon are each tried once more (the abandon not after a termination failure). An abandon that still fails is logged and the relaunch goes on; see the follow-ups.
  • Closing a session whose restored launch was never taken over ends that launch.
  • A session with a launch and no held pause stops what the restore may have brought back of it before relaunching.
  • resumed_conversation is saved and restored with the session, so a resumed launch that is taken over is judged as a resumed launch.

How it works

flowchart TD
  CK["checkpoint: hold pause N,<br/>relay holds model calls,<br/>AgentENV memory snapshot"] --> CR["crash, new job: fork from snapshot,<br/>environment server re-seeds, agent re-binds"]
  CR --> KP{"bookmark keeps processes?"}
  KP -->|"no"| RL["relaunch from saved conversation<br/>(as on OpenSandbox)"]
  KP -->|"yes"| CHK{"check_restored"}
  CHK -->|"RestoredLaunch"| EP["re-point relay: new model URL,<br/>generation plus 1"]
  EP --> AD["continue pause N, adopt launch"]
  AD --> OK["OpenCode continues in place,<br/>cut call re-sent byte for byte"]
  AD -->|"take-over fails"| AB
  CHK -->|"NotResumable"| AB["abandon launch (KILL, no CONT)"]
  AB --> RL
Loading

Where this sits in the stack

This stack checkpoints environment-server episodes whose agent harness runs in a sandbox the resources server lends. #5264, #5265 and #5266 have merged, and the rest of the stack is rebased onto main after them. #5265 to #5331 add checkpoints for OpenCode and the SWE servers on OpenSandbox. #5410 to #5413 add AgentENV (E2B API, VM memory snapshots), where a restored sandbox still runs the harness's processes, so OpenCode continues in place instead of being relaunched. Each PR's diff shows only that PR's change.

  1. feat(checkpoint): protocol for harnesses that borrow a sandbox #5264: feat(checkpoint): protocol for harnesses that borrow a sandbox (merged)
  2. feat(sandbox): snapshot, fork and delete for OpenSandbox, with a checkpointer #5265: feat(sandbox): snapshot, fork and delete for OpenSandbox, with a checkpointer (merged)
  3. feat(swebench): checkpoint the task sandboxes of episode sessions #5266: feat(swebench): checkpoint the task sandboxes of episode sessions (merged)
  4. feat(swebench): typed sessions on the owner base with capture-once verify #5267: feat(swebench): typed sessions on the owner base with capture-once verify
  5. feat(opencode): pause at checkpoints and resume after a crash #5268: feat(opencode): pause at checkpoints and resume after a crash
  6. test(checkpoint): end-to-end harness scenarios, SWE-bench recipe, docs #5269: test(checkpoint): end-to-end harness scenarios, SWE-bench recipe, docs
  7. feat(swe): checkpoint the episode sessions of the repository-task servers #5331: feat(swe): repository-task servers on the sandbox owner base
  8. feat(sandbox): AgentENV snapshots, lease and kill-at-timeout for the E2B provider #5410: feat(sandbox): AgentENV snapshots, lease and kill-at-timeout for the E2B provider
  9. feat(checkpoint): hold-pause a harness when the sandbox snapshot keeps its processes #5411: feat(checkpoint): hold-pause a harness when the sandbox snapshot keeps its processes
  10. feat(agent-utils): resume a supervised harness in place after a restore that kept its processes #5412: feat(agent-utils): resume a supervised harness in place after a restore that kept its processes
  11. feat(opencode): resume OpenCode in place on sandboxes whose snapshots keep processes #5413: feat(opencode): resume OpenCode in place on sandboxes whose snapshots keep processes (this PR)

Issue

No tracking issue exists. This finishes AgentENV in-place resume for OpenCode, the harness used in SWE training.

Validation

Head 3d0e643db9, on #5412 6ff53fb3ac. The stack was rebased onto main after #5266 merged. Tests ran as Slurm jobs on one node each, in the rl-gym CI container with the repository's dev dependencies; nothing ran on a login node. In that container main itself has 179 failures and 13 errors in the core unit suite (job 9188100), so results are compared with main's list by test id. Two test files that hang there, on main too, are left out: tests/unit_tests/test_slurm_control.py and tests/unit_tests/benchmarks/test_agentic_vbench_staging.py.

  • This head is the stack's tip (jobs 9188099, 9188102): import-linter clean; ruff check and ruff format --check clean; all ten server suites pass; the core suite differs from main's list only by tests that fail from run to run on either side.
  • Process-level checkpoint e2e on the stack tip (jobs 9188102, 9188735, NEMO_GYM_CHECKPOINT_E2E=1): harness scenarios 11 passed, sandbox 6, sequences 4, fake backend 1, test_checkpoint_e2e.py 45 passed and 12 skipped.
  • Tests added in the review: a take-over that fails while the relay is pointed and while the launch is adopted; export of a pending take-over; the re-bind undone on a cancel and on an error; a stopped receipt after a take-over; an abandon that fails twice; a launch that ended by itself and failed; close with a pending take-over; the check_failed retry; stopping a launch with no held pause; and, end to end, a launch taken over once, checkpointed, and taken over again (one launch, pause 2, relay generation 3, the held call re-sent byte for byte to the third model server).
  • How the relay was run: on Node through a fake OpenCode binary, not on OpenCode's real binary (BUN_BE_BUN=1). A relay_unavailable warning in a live run would be the first sign that it does not start there.
  • CI: the core job has no Node or Bun, so tests that need the relay are skipped there.
  • Review: three reviewers and a second pass that challenged their findings.
  • Not run: pre-commit run --all-files; any live sandbox, real-model or training run on this head.
  • Earlier evidence, not repeated on this head (previous versions of the stack, which set the clock back inside the sandbox): live tests against AgentENV of snapshot, fork and in-place resume with real OpenCode, and a 16-node run on the five-route SWE blend with a restore from its checkpoint in which every restored launch resumed in place.
  • Training run, on the stack two rebases ago (e62ed9ca9a), not repeated on this head: 16-node full-SWE run on AgentENV, Slurm job 9048275 (2 h, five routes, 8 Gym replicas), compared with reference job 8760594 on an earlier stack. 3 training steps (2981 s, 2567 s, 670 s; reference 2948 s, 2606 s, 681 s); 8 rollout checkpoints saved, 0 failed (reference 7 / 0); checkpoint save median 57 s, Gym commit median 50 s (reference 65 s and 48 s); 351 full 16-rollout batches, median 730 s (reference 347, 720 s); no snapshot, fork, anti-cheat, base-commit or patch-extract failures. The AgentENV gateway returned about four times as many 502s as in the reference; on the servers of this stack each was a retried first attempt and none exhausted its retries. Not exercised: the run had no crash, so no restore and no resume in place ran; scale_swe is not on the checkpoint layer.

Follow-ups, not in this PR:

  • An abandon that fails twice is logged and OpenCode is relaunched; two harnesses may then share a repository. Whether that should fail the activation instead is an open decision.
  • A dedicated exception type for a failed take-over.
  • A WARNING at launch when the relay did not start (today the restore logs it).

Compatibility

  • OpenSandbox and other providers whose snapshots do not keep processes: launch, checkpoint and restore are unchanged.
  • AgentENV: OpenCode's model calls go through the in-sandbox relay. Gym does not change the sandbox's clock or the image's time daemons; a restored sandbox's clock is what the provider gives it (snapshots.keep_guest_clock, feat(sandbox): AgentENV snapshots, lease and kill-at-timeout for the E2B provider #5410).
  • Images without Python resume in place like any other: the relay runs on OpenCode's binary.
  • Bookmarks gain optional keeps_processes and relay_gen. Older bookmarks restore through the relaunch path; a relay_python field from an earlier version of this PR is ignored.
  • Saved session state gains resumed_conversation and, for a restored launch not yet taken over, its held pause; older state restores through the relaunch path.
  • nemo_gym.agents exports check_restored, RestoredLaunch, NotResumable, abandon_launch and stop_and_confirm_cleanup.
  • Log lines a run can count: OpenCode resumed in place: (WARNING), cannot resume in place: ... reason ... (INFO for relay_gone, which also covers a relay that died or was restarting; WARNING otherwise), abandoned a restored launch, could not abandon.

🤖 Generated with Claude Code

@pthombre pthombre added feature New capabilities, enhancements, or enablement work area:agent Agent harnesses and Responses API agent behavior complexity:high Cross-component or shared-contract change with a large review surface labels Oct 31, 2026
@copy-pr-bot

copy-pr-bot Bot commented Oct 31, 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.

@pthombre
pthombre added this pull request to stack #5270 October 31, 2026 18:08
@pthombre
pthombre force-pushed the pranav/envckpt-10-opencode-in-place branch from 7725780 to a3b18a2 Compare October 31, 2026 19:53
@pthombre
pthombre force-pushed the pranav/envckpt-10-opencode-in-place branch from a3b18a2 to 744b51c Compare November 2, 2026 06:16
@pthombre
pthombre force-pushed the pranav/envckpt-10-opencode-in-place branch from 744b51c to 4bcb151 Compare November 3, 2026 03:22
@pthombre
pthombre force-pushed the pranav/envckpt-10-opencode-in-place branch from 4bcb151 to 15099f2 Compare November 3, 2026 06:40
@pthombre
pthombre force-pushed the pranav/envckpt-10-opencode-in-place branch from 15099f2 to abf7b47 Compare November 5, 2026 20:07
@pthombre

pthombre commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Being rebased onto current main, after #5264 and #5265 (already rebased). Until this branch is pushed, its diff still shows their old commits. Not ready for review yet.

@pthombre
pthombre force-pushed the pranav/envckpt-10-opencode-in-place branch from abf7b47 to 2b4046b Compare November 8, 2026 04:51
@pthombre
pthombre force-pushed the pranav/envckpt-10-opencode-in-place branch from 2b4046b to 27962ac Compare November 9, 2026 05:37
Base automatically changed from pranav/envckpt-9-resume-in-place to main November 23, 2026 22:52
pthombre and others added 5 commits November 23, 2026 14:52
… keep processes

On a provider whose snapshots keep the sandbox's processes (`snapshot_keeps_processes`, AgentENV), a checkpointed
OpenCode session now runs its supervisor in hold mode and calls the model through the model relay in the sandbox
(`sandbox_model_relay_port`, default 18431); without Python 3.6+ in the image the runner falls back to the direct
model URL. Other providers launch exactly as before.

After a crash, the re-bind checks the restored launch (`check_restored`): when its supervisor, held pause and relay
survived, or the launch had already ended, the activation points the relay at the replacement attempt's model URL
(endpoint generation + 1) before it continues the pause, and takes the launch over with `SandboxSession.adopt`, so the
call the checkpoint cut is re-sent byte for byte and nothing is relaunched. Otherwise the launch is abandoned (killed,
nothing continued) and relaunched from the saved conversation as before; a failed take-over falls back the same way.
The bookmark carries `keeps_processes` and `relay_gen`. WARNING lines `OpenCode resumed in place:`, `OpenCode cannot
resume in place:` and `OpenCode abandoned a restored launch:` count the outcomes.

Adds benchmarks/swebench/verified/opencode_sessions_agentenv.yaml and documents the path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
…n, and log them at INFO

Some task images have no Python 3.6 or later. The model relay cannot run there, so every launch runs without it and a
restore cannot continue it in place: it is relaunched from its saved conversation (7% of resumed launches in one
16-node job). That was only visible as a WARNING at restore time.

- At seed, a process-keeping session checks once for the relay's Python, as the runner looks for it, and records it
  with the session's checkpoint (`relay_python`). Without one it logs a single WARNING naming the session, the task
  and the sandbox: its launches cannot resume in place after a restore.
- On restore, `relay_unavailable` for such a session reports "no Python in the image".
- `relay_unavailable` and `relay_gone` are expected outcomes and are logged at INFO, with the abandonment that follows.
  Every other reason (`adopt_failed`, `supervisor_gone`, `identity_mismatch`, `pause_mismatch`) stays a WARNING.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
…y image can resume in place

The relay was model_relay.py on the task image's Python, so a launch on an image
without Python 3.6+ ran without it and a restore relaunched it instead of
resuming it in place. OpenCode's binary is Bun, which runs model_relay.mjs with
BUN_BE_BUN=1; the runner sets that for the relay's process only, since OpenCode
itself would start as Bun with it. Verified in AgentENV sandboxes on images with
and without Python.

The seed-time Python probe and its warning go, and a launch without a relay is
now logged at WARNING: it no longer happens by design. The in-place resume tests'
stand-in OpenCode runs the relay the same way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
…oes not take it over

Review fixes for resume in place.

- A take-over that fails before the launch is continued (resolving the model URL, pointing the relay) abandons the
  restored launch before the relaunch: the session had launched nothing itself, so `cleanup_confirmed` was true and
  the abandon was skipped while that launch still ran. The abandon is now skipped only when a positive receipt was
  read.
- A launch taken over that ended by itself and then failed raises, as after `execute`, instead of being run again.
- A launch taken over whose receipt says it was stopped (a stop or an earlier abandon) is relaunched, not reported
  as finished.
- A checkpoint between the re-bind and the take-over exports the pause the restored launch still holds, so the next
  restore checks that launch again instead of relaunching beside it.
- A restored-launch check that raises or is cancelled undoes the re-bind, so the retried seed decides again.
- A check that could not run (`check_failed`) is made once more before a launch that may be resumable is ended.
- An abandon that fails is tried once more, except after a termination failure.
- Closing a session whose restored launch was never taken over ends that launch.
- A session with a launch and no held pause stops what the restore may have brought back of it before relaunching
  (`stop_and_confirm_cleanup`, now exported from `nemo_gym.agents`).
- `resumed_conversation` is saved and restored, so a resumed launch taken over is graded as a resumed launch.
- README: `relay_gone` fallbacks are logged at INFO. The base sessions recipe names AgentENV. `check_restored` is in
  its alphabetical place in the public API.

Tests: the model-calling fake wraps `started()` instead of patching `run()`; longer control deadlines; the relay is
checked to be up before the checkpoint; one clean-up helper that also ends the crashed attempt. New unit tests cover each
fix above.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
…pabilities

The E2B provider now declares it as SandboxCapabilities.snapshot_keeps in place of the untyped
snapshot_keeps_processes attribute, and names its lease create.renew_ttl_s.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
@zyzhou5
zyzhou5 force-pushed the pranav/envckpt-10-opencode-in-place branch from 03f98a2 to dae470f Compare November 23, 2026 22:52
@zyzhou5

zyzhou5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

/ok to test dae470f

@nemo-automation-bot
nemo-automation-bot Bot requested review from a team November 23, 2026 23:00
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

…e check, log a resume once it happened

- A restored launch that had ended before the re-bind checked it calls no model: its take-over now only collects its
  receipt and what it left, without pointing the relay. A relay upload that failed used to abandon it and run the
  task again.
- A restored launch whose held pause expired before the take-over (`pause_expired`) was ended by the supervisor at the
  pause's limit, not by OpenCode: it is relaunched, as one a stop ended, instead of being reported as finished.
- A restored-launch check that still cannot run when asked again fails the re-bind, so the retried seed checks again,
  instead of ending a launch that may be resumable. A check that answers keeps ending the launches it rules out.
- "OpenCode resumed in place" is logged once the take-over has continued the launch. Before it, the take-over logs
  "taking over a restored launch" (or "collecting a restored launch that had ended") at INFO, so a failed take-over
  no longer logs a resume as well.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zeyu Zhou <[email protected]>
@zyzhou5

zyzhou5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

/ok to test ac24b39

… run

From the review of resume in place:

- An abandon of a restored launch that still fails after its retry, or ends in a termination failure, now raises.
  The re-bind, activation or close that asked fails instead of relaunching OpenCode beside a launch that may still
  run, and its retry ends the launch again.
- A running restored launch whose seed granted episode MCP tools is not taken over: its process keeps the previous
  attempt's MCP URLs and credentials. It is ended and relaunched, which renders the replacement attempt's grants
  (`mcp_tool_grants`, logged at INFO as an expected fallback). One that had ended is still collected.
- A take-over cancelled before it started the launch (a close or retire while the relay is re-pointed) leaves the
  restored launch pending, so the close ends it.
- A close forgets the restored launch only once it is ended, so a close cancelled or failed while ending it ends it
  on its retry, before the sandbox is released.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zeyu Zhou <[email protected]>
@zyzhou5

zyzhou5 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

/ok to test 83a3c3d

A close relaunches nothing, and the sandbox's owner stops the sandbox at the end of the episode, so a close whose
abandon of the restored launch still fails after its retry now logs the failure and releases the session, as before
83a3c3d. Failing it held the session for retries that could not succeed. A re-bind or activation still fails
rather than relaunch beside the launch, and a close cancelled while it ends the launch still ends it on its retry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zeyu Zhou <[email protected]>
@zyzhou5

zyzhou5 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

/ok to test ce15983

… tools on both attempts

- A restored launch that recorded no held pause is stopped before the relaunch. A stop that cannot be confirmed now
  raises, as an abandon does, so the re-bind fails and its retry stops it again instead of relaunching beside it.
- The restore decision gets the replacement attempt's seed. A running launch is not taken over when the launch's own
  attempt or the replacement attempt has MCP tools, configured or granted (`effective_tool_accesses`): the process
  keeps the MCP servers it was launched with, and only a relaunch renders the replacement attempt's. This covers a
  configured server the replacement overrides and grants only the replacement adds. A launch that had ended is still
  collected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zeyu Zhou <[email protected]>
@zyzhou5

zyzhou5 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

/ok to test e025edd

@zyzhou5
zyzhou5 added this pull request to the merge queue Nov 24, 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/45483710387

Merged via the queue into main with commit 6f6f4c7 Nov 24, 2026
79 of 82 checks passed
@zyzhou5
zyzhou5 deleted the pranav/envckpt-10-opencode-in-place branch November 24, 2026 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent Agent harnesses and Responses API agent behavior complexity:high Cross-component or shared-contract change with a large review surface feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer risk:high Autobot PR risk: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants