Repository navigation
Conversation
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]>
d986f45 to
c2dd099
Compare
|
PR rebased and description updated. |
hemildesai
left a comment
There was a problem hiding this comment.
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 underidle_policy: pause(L1018). - A failed snapshot delete in
close()is dropped, so it can't be retried by a secondclose()oraclose()(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?
|
/ok to test c2dd099 |
… 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
left a comment
There was a problem hiding this comment.
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.
|
/ok to test add96b7 |
hemildesai
left a comment
There was a problem hiding this comment.
CI is green on add96b7 and all four review points are fixed. Approving. Thanks for the careful work on the idle policies!
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_policyto the E2B provider, which parks idle sandboxes:idle_policycreate.idle_timeout_s(default 1.0)never(default)pausepauseand resumes it throughconnectbefore the next request. It resumes on the node that paused it.suspendcreate_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.yamlin+config_paths:A command-line override such as
++sandbox.e2b.create.idle_policy=suspendworks too.Under
suspend, the restored sandbox runs wherever the backend places a new sandbox:Behavior:
request.
pause()waits for requests in flight.SandboxSpec.ttl_s.connection.request_timeout_sis set.pausenever counts as paused.pause()raises feat(sandbox): implement E2B pause and resume #3810's "refused pause with 409" error.create()did.auto_resume),networkrules and metadata, attribution included.custom_extension_paramsset, it goes throughPOST /sandboxeslikecreate().execreturnsreturn_code125 witherror_type="sandbox", asuspended sandbox keeps its snapshot, and the next request tries again.
suspend, the Gym handle keeps its ID while the E2B sandbox behind it changes on every restore.status()reportsPAUSEDwhile the sandbox is suspended.serialize()restores the sandbox and returns the ID of the one now running.endpoint()andserialize()keep the sandbox running from then on, because their traffic bypasses theprovider.
close()and the provider'saclose()delete leftover snapshots.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 reachesits timeout.
Docs:
e2b.yamlcomments;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.python -m pytest tests/unit_tests/sandbox --no-cov -q -o addopts='': 1980 passed, 8skipped. That includes
test_e2b_provider.py: 197 tests, all passing.5714c4ae5, the E2B tests intests/unit_tests/sandboxpass (239 passed, 1 skipped).test_e2b_provider.py.provider.pyande2b.yaml.RAY_TMPDIR=/tmp python -m pytest tests/unit_tests --no-cov -q -o addopts='': 19,741 passed, 207skipped, 17 failed.
390c9541f) in the same venv. Most needmlflow,wandborcloudpickle, and nonetouch the sandbox.
mlflow,cloudpickle): four undertests/unit_tests/artifacts/andserver_runtime/test_review_boundary_interfaces.py.pre-commit run --all-files, including the import-linter layering check: passed.git diff --check: passed.Signed-off-bytrailer.cd fern && npm run check -- --warnings, because Node.js isn't available on the test machine. Thedocs 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:
9f76bef).services/at the same commit, with therandomstrategy.connection.api_urlandconnection.sandbox_url. It had nonode addresses.
Provider smoke on the rebased head
c2dd0999c(python:3.12-slim, 1 vCPU / 1 GiB), all passed:pause:suspend, 8 suspend/restore cycles:Explicit
pause()/resume()undersuspend:status()readPAUSED, thenRUNNING.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:
From the pre-rebase head (
d986f457d), same lab:No-capacity restore (scheduler capped, both nodes filled):
return_code125 witherror_type="sandbox"and kept the snapshot.Real-model rollouts:
mini_swe_agent_2withnvidia/nemotron-3-ultra, through Gym'sopenai_modelserver, onSWE-bench Verified
scikit-learn__scikit-learn-14141, at 1 vCPU / 4 GiB.idle_policyneverpausesuspendidle 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:
DELETE /templates/{id}names no sandbox, so AgentENV's gateway sends it through thescheduler, and it uses up a round-robin turn.
round_robinevery restore of a single sandbox landed on the samenode.
randomstrategy spreads restores as expected.JSON, so the error text Gym reports is a JSON parse error. The classification (125 /
"sandbox", snapshot kept) isstill correct.
Compatibility and benchmark impact
never, changes nothing.create.idle_policyandcreate.idle_timeout_s, ine2b.yaml.pauseorsuspend:suspendneeds a backend with E2B snapshots, and storage that every node the gateway schedules onto can read.Checklist
pre-commit run --all-files).git commit -s).🤖 Generated with Claude Code