Skip to content

feat(sandbox): pause or suspend idle E2B sandboxes - #5339

Open
rzambre wants to merge 3 commits into
NVIDIA-NeMo:mainfrom
rzambre:rzambre/gym-agentenv-suspend-resume
Open

rzambre wants to merge 3 commits into
NVIDIA-NeMo:mainfrom
rzambre:rzambre/gym-agentenv-suspend-resume

Conversation

@rzambre

@rzambre rzambre commented Oct 29, 2026 •

Copy link
Copy Markdown

What does this PR do?

Rebased onto main after #3810 merged. The PR is now two commits on top of main. Both build on #3810's final
pause/resume behavior and on the create options from #3757, #5356 and #5607:

  • 5714c4ae5, pause;
  • c2dd0999c, suspend.

Why. An agent's sandbox sits idle while the agent waits on its model, but it holds its CPU and memory on the
backend the whole time. This PR adds create.idle_policy to the E2B provider, which parks idle sandboxes:

idle_policy What happens to a sandbox that no request has touched for create.idle_timeout_s (default 1.0)
never (default) Nothing. Today's behavior.
pause The provider pauses it through E2B's pause and resumes it through connect before the next request. It resumes on the node that paused it.
suspend The provider snapshots it to the backend's durable storage (create_snapshot) and kills it. The next request creates a sandbox from the snapshot, with the original create arguments, then deletes the snapshot.

Opt-in. The policy applies to every sandbox the provider creates. To turn it on, add a config overlay after
e2b.yaml in +config_paths:

sandbox:
  e2b:
    create:
      idle_policy: suspend   # or pause
      idle_timeout_s: 1.0

A command-line override such as ++sandbox.e2b.create.idle_policy=suspend works too.

Under suspend, the restored sandbox runs wherever the backend places a new sandbox:

  • behind a gateway that schedules creates across nodes, such as AgentENV's, the sandbox moves to a node that has room;
  • an idle sandbox then holds no node at all;
  • the provider keeps no node addresses and makes no placement decisions.

Behavior:

  • Requests are never cut off.
    • A per-sandbox lock and an in-flight count keep parking and requests apart, and the idle timer restarts after every
      request.
    • An explicit pause() waits for requests in flight.
  • Resume, consistent with feat(sandbox): implement E2B pause and resume #3810:
    • It reapplies the sandbox's lifetime, including a per-sandbox SandboxSpec.ttl_s.
    • It keeps the request timeout from create, unless connection.request_timeout_s is set.
  • 409, consistent with feat(sandbox): implement E2B pause and resume #3810: a 409 from pause never counts as paused.
  • A restore creates exactly as create() did.
    • It resends the original lifecycle (auto_resume), network rules and metadata, attribution included.
    • With custom_extension_params set, it goes through POST /sandboxes like create().
  • A failed resume or restore loses nothing. exec returns return_code 125 with error_type="sandbox", a
    suspended sandbox keeps its snapshot, and the next request tries again.
  • Under suspend, the Gym handle keeps its ID while the E2B sandbox behind it changes on every restore.
    • status() reports PAUSED while the sandbox is suspended.
    • serialize() restores the sandbox and returns the ID of the one now running.
  • endpoint() and serialize() keep the sandbox running from then on, because their traffic bypasses the
    provider.
  • Snapshot lifetime:
    • The snapshot is deleted only after the new sandbox exists.
    • close() and the provider's aclose() delete leftover snapshots.
    • If the kill after a snapshot fails, the old sandbox is killed after the next restore or at close.
    • A restore cancelled by its caller kills whatever it created.
  • Crashed process: as the existing pause docs say, a process that dies while its sandboxes are paused or suspended
    leaves them paused, or leaves their snapshots behind.

Relation to #3757: the idle policy is independent of create.auto_resume, which acts only when a sandbox reaches
its timeout.

Docs:

  • the E2B provider README;
  • the e2b.yaml comments;
  • an "Idle Sandboxes" subsection under "Pause and Resume" on the Fern E2B page.

No separate issue: this builds on the pause/resume capability from #3810.

Validation

All of the following ran against the final head, c2dd0999c, with the locked E2B 2.46.0.

  • Sandbox tests: python -m pytest tests/unit_tests/sandbox --no-cov -q -o addopts='': 1980 passed, 8
    skipped
    . That includes test_e2b_provider.py: 197 tests, all passing.
  • Pause commit alone: at 5714c4ae5, the E2B tests in tests/unit_tests/sandbox pass (239 passed, 1 skipped).
  • Regression check: the two commits add 45 tests to test_e2b_provider.py.
    • All 45 fail against main's provider.py and e2b.yaml.
    • Every one of main's tests in the file still passes.
  • Core suite: RAY_TMPDIR=/tmp python -m pytest tests/unit_tests --no-cov -q -o addopts='': 19,741 passed, 207
    skipped, 17 failed
    .
    • The same 17 fail on main (390c9541f) in the same venv. Most need mlflow, wandb or cloudpickle, and none
      touch the sandbox.
    • It leaves out five files that need packages outside the installed extras (mlflow, cloudpickle): four under
      tests/unit_tests/artifacts/ and server_runtime/test_review_boundary_interfaces.py.
  • pre-commit run --all-files, including the import-linter layering check: passed.
  • git diff --check: passed.
  • DCO: both commits have a Signed-off-by trailer.
  • Not run: cd fern && npm run check -- --warnings, because Node.js isn't available on the test machine. The
    docs change adds one subsection and two config lines to an existing page, with no new pages, links or nav entries.

CI classification: full, because this changes the shared sandbox provider.

Rollouts

Setup:

  • Two upstream AgentENV nodes (kvcache-ai/AgentENV at 9f76bef).
  • AgentENV's own gateway and scheduler, built from services/ at the same commit, with the random strategy.
  • A shared S3-compatible snapshot store (MinIO).
  • Gym's provider pointed only at the gateway, through connection.api_url and connection.sandbox_url. It had no
    node addresses.

Provider smoke on the rebased head c2dd0999c (python:3.12-slim, 1 vCPU / 1 GiB), all passed:

  • pause:

    • Paused for 45 s, past the gateway's 30 s binding TTL, then resumed on the same node.
    • A file and a background process survived.
  • suspend, 8 suspend/restore cycles:

    • The scheduler placed the restores on both nodes, with six moves between them.
    • A file and a background process survived every move.
    • Each cycle left the old sandbox gone and one snapshot in the catalog.
  • Explicit pause()/resume() under suspend: status() read PAUSED, then RUNNING.

  • Closing a suspended sandbox deleted its snapshot from the shared catalog.

  • Afterwards: 0 sandboxes and 0 snapshots on either node.

  • Timings, from the provider's DEBUG lines:

    Operation Count Median Max
    Suspend (snapshot + kill) 9 0.20 s 0.21 s
    Restore 8 0.23 s 0.30 s
    Pause 1 0.07 s
    Resume 1 0.11 s

From the pre-rebase head (d986f457d), same lab:

  • No-capacity restore (scheduler capped, both nodes filled):

    • It returned return_code 125 with error_type="sandbox" and kept the snapshot.
    • Once a node freed, the next request restored the sandbox onto that node.
  • Real-model rollouts: mini_swe_agent_2 with nvidia/nemotron-3-ultra, through Gym's openai_model server, on
    SWE-bench Verified scikit-learn__scikit-learn-14141, at 1 vCPU / 4 GiB.

    idle_policy Rollouts Reward Sandbox errors What the nodes saw Left afterwards
    never 1 1.0 0 1 create, running throughout 0 sandboxes, 0 snapshots
    pause 1 1.0 0 1 create, alternating running and paused 0 sandboxes, 0 snapshots
    suspend 2 1.0 (2/2) 0 33 suspends and 33 restores, across both nodes (snapshots taken 14/19, sandboxes created 15/20) 0 sandboxes, 0 snapshots
    • The verifier scored every rollout, and no trajectory contains a resume or restore error.
    • This shows correctness, not a reward comparison: one task with one or two rollouts per policy.
    • The rebase changed how a restore builds its create arguments (lifecycle, network, extension params) and how an
      idle pause treats a 409. The rebased provider smoke above covers the suspend/restore path.

Two AgentENV behaviors seen in the lab, neither caused by this PR:

  • Round-robin skew: DELETE /templates/{id} names no sandbox, so AgentENV's gateway sends it through the
    scheduler, and it uses up a round-robin turn.
    • Each suspend cycle deletes a snapshot, so under round_robin every restore of a single sandbox landed on the same
      node.
    • The random strategy spreads restores as expected.
  • Plain-text 503: with no capacity, AgentENV's gateway returns 503 with a plain-text body. The E2B SDK expects
    JSON, so the error text Gym reports is a JSON parse error. The classification (125 / "sandbox", snapshot kept) is
    still correct.

Compatibility and benchmark impact

  • The default, never, changes nothing.
    • New config keys: create.idle_policy and create.idle_timeout_s, in e2b.yaml.
    • No benchmark configs or datasets change.
  • With pause or suspend:
    • Every idle period adds one park, plus a resume or restore that the agent sees as latency on its next command.
    • That was sub-second in the lab above, but it depends on the backend.
  • suspend needs a backend with E2B snapshots, and storage that every node the gateway schedules onto can read.
  • Reward impact is unmeasured beyond the small sample above.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally.
  • Documentation added or updated.
  • Pre-commit checks pass locally (pre-commit run --all-files).
  • New source files include the required Apache-2.0 SPDX header. (N/A: no new source files.)
  • All commits have DCO sign-off (git commit -s).

🤖 Generated with Claude Code

@copy-pr-bot

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

@nemo-automation-bot nemo-automation-bot Bot added the community-request Issue reported or requested by someone from the community label Oct 29, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added area:sandbox Sandbox providers, isolation, and sandbox runtime contracts feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer waiting-on-maintainers Waiting on maintainers to respond labels Oct 30, 2026
@github-actions github-actions Bot added the sla:triage-overdue Review assignment is over the one-business-day SLA label Nov 2, 2026
rzambre and others added 2 commits November 5, 2026 20:30
An agent's sandbox sits idle while the agent waits on its model, and
keeps holding CPU and memory on the backend. Add create.idle_policy to
the E2B provider. With "pause", the provider pauses a sandbox that no
request has touched for create.idle_timeout_s seconds and resumes it
before the next request, through E2B's pause and connect. The default,
"never", keeps the current behavior.

- A request is never cut off: a per-sandbox lock and in-flight count
  keep pauses and requests apart, and an explicit pause() waits for
  requests in flight.
- A resume reapplies the sandbox's own lifetime, including a
  per-sandbox SandboxSpec.ttl_s, and keeps the request timeout from
  create, as resume() does.
- As in pause(), a 409 never counts as paused: an idle pause that gets
  one leaves the sandbox running, and an explicit pause() raises.
- A failed resume makes exec return code 125 with error_type "sandbox",
  and the next request retries.
- endpoint() and serialize() keep the sandbox running from then on,
  because their traffic bypasses the provider.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Rohit Zambre <[email protected]>
A paused E2B sandbox can only resume on the node that paused it, so an
idle sandbox still ties up that node. Add idle_policy "suspend": the
provider snapshots an idle sandbox to the backend's durable storage
with create_snapshot and kills it. The next request creates a sandbox
from the snapshot, with the arguments of the original create, and then
deletes the snapshot.

The restored sandbox runs wherever the backend places a new sandbox.
The provider keeps no node addresses: behind a gateway that schedules
creates across nodes, such as AgentENV's, a sandbox's state moves to
any node that can read the snapshot.

- A restore resends the original lifecycle, network rules and metadata,
  attribution included, and goes through POST /sandboxes like create()
  when create.custom_extension_params is set.
- The Gym handle keeps its ID while the E2B sandbox behind it changes.
  status() reports PAUSED while suspended, and serialize() restores the
  sandbox and returns the ID of the one now running.
- The snapshot is deleted only after the new sandbox exists. A failed
  restore keeps it, exec returns code 125 with error_type "sandbox",
  and the next request retries.
- If the kill after a snapshot fails, the old sandbox is killed after
  the next restore or at close. A restore cancelled by its caller kills
  whatever it created.
- close() and the provider's aclose() delete leftover snapshots.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Rohit Zambre <[email protected]>
@rzambre
rzambre force-pushed the rzambre/gym-agentenv-suspend-resume branch from d986f45 to c2dd099 Compare November 5, 2026 19:17
@rzambre

rzambre commented Nov 10, 2026

Copy link
Copy Markdown
Author

PR rebased and description updated.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels Nov 10, 2026

@hemildesai hemildesai 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.

SHIP WITH CARE

Thanks — this is careful work. The lock / in-flight count / epoch design holds up: I traced concurrent requests, idle parks already past their sleep, cancelled restores (the reaper kills what they created), and close() racing an in-flight park, and found no way for a park to cut off a running command. The real-model rollout evidence is appreciated.

Four gaps, all in cleanup and explicit pause(), each reproduced with a scratch test against the PR's own RemoteFakeSandbox:

  • Explicit pause() raises after an idle pause under idle_policy: pause (L1018).
  • A failed snapshot delete in close() is dropped, so it can't be retried by a second close() or aclose() (L840).
  • A failed snapshot delete after a restore is no longer tracked anywhere (L1144).
  • aclose() cleans up suspended snapshots but leaves idle-paused sandboxes paused, which removes the TTL backstop (L900).

Locally tests/unit_tests/sandbox/test_e2b_provider.py passes (188 passed, 9 skipped). Unit/functional workflows haven't run on c2dd099 yet — could someone post /ok to test?

One thing I couldn't confirm: does AgentENV accept a snapshot ID as templateID on the custom_extension_params restore path?

@github-actions github-actions Bot removed the sla:triage-overdue Review assignment is over the one-business-day SLA label Nov 13, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels Nov 13, 2026
@hemildesai

Copy link
Copy Markdown
Contributor

/ok to test c2dd099

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the waiting-on-maintainers Waiting on maintainers to respond label Nov 17, 2026
… provider close

Address the review of the E2B idle policies:

- An explicit pause() of a sandbox the idle timer already paused no
  longer sends a second pause, whose 409 made it raise. A park whose
  outcome is unknown is still sent again.
- close() no longer drops a snapshot it could not delete: it raises,
  keeps the sandbox's state, and the next close() or aclose() deletes
  the snapshot.
- A snapshot whose delete fails after a restore stays listed and is
  retried after the next restore, at close() and at aclose().
- aclose() kills the sandboxes the idle policy left paused, since a
  paused sandbox never reaches its TTL. Running sandboxes are still
  left to their TTL.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Rohit Zambre <[email protected]>

@hemildesai hemildesai 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.

Thanks Rohit. add96b7 fixes all four points, and I've resolved those threads. tests/unit_tests/sandbox/test_e2b_provider.py passes locally on this head (197 passed, 9 skipped). CI still needs to run on the new head; I'll approve once it's green.

@hemildesai

Copy link
Copy Markdown
Contributor

/ok to test add96b7

@hemildesai hemildesai 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.

CI is green on add96b7 and all four review points are fixed. Approving. Thanks for the careful work on the idle policies!

This branch was previously deployed

1 inactive deployment
public — add96b76 Deployed Nov 17, 2026 by copy-pr-bot[bot] via release / finalize / notify #13116
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:sandbox Sandbox providers, isolation, and sandbox runtime contracts community-request Issue reported or requested by someone from the community feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer waiting-on-maintainers Waiting on maintainers to respond

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants