You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature]: Option to retain (or pause) sandboxes between calls instead of deleting them on every release #3390
SandboxManager.release always calls stop() then shutdown() for self-managed sandboxes; for the E2B backend, shutdown() deletes the remote sandbox. Every subsequent call() therefore creates a new sandbox and restores the whole workspace from the snapshot, plus re-applies the workspace projection.
Yet both layers already support reuse: E2bSandbox.ensureSandbox first tries connectSandbox(previousId), and AbstractBaseSandbox.start skips the snapshot restore when the workspace is still present. They just never get the chance, because the sandbox was deleted at the end of the previous call.
Measurements (Alibaba Cloud Agent Sandbox via E2B protocol, cn-hangzhou, AgentScope 2.0.3, workspace projection on, 300 KB of skill files)
Delete on release (current): 10–19 s per call.
Keep the sandbox alive (wrapping the client so that shutdown() is a no-op): first call 12 s, then about 1.05 s per call. Skill scripts still run, and files persist across calls. After the platform timeout expires, the next call recreates the sandbox from the snapshot as usual.
Platform API: connect to a live sandbox takes 0.6 s; pause (deep sleep) takes 15.8 s synchronously; resume takes 1.0 s.
Proposal
Add a release policy on SandboxFilesystemSpec (or on SandboxClientOptions):
DELETE (current default);
RETAIN: do not call shutdown(), and let the provider's timeout reclaim the sandbox;
optionally PAUSE: pause asynchronously after release, for providers that support it.
With RETAIN or PAUSE, the existing reconnect path in the E2B backend applies unchanged. An explicit API for deleting a user's retained sandbox would also help data-deletion flows.
Related
#3339 and #3340 (file transfer speed); #2303 (remote snapshot rebinding, also needed for retained sandboxes with a Redis snapshot).
All three technical premises were checked against main (91550713) and are confirmed:
Release always destroys the sandbox.SandboxManager.release() calls sandbox.stop() then sandbox.shutdown() unconditionally for every self-managed result (agentscope-harness/.../sandbox/SandboxManager.java:184-212). Only Priority-1 user-managed results (SandboxAcquireResult.userManaged(external), SandboxManager.java:77) short-circuit — so today the only supported way to keep a sandbox alive across calls is for the caller to own the instance.
For E2B, shutdown() means delete.E2bSandbox.shutdown() calls platform.killSandbox(id) whenever e2bState.isSandboxOwned() (.../e2b/E2bSandbox.java:112-123), and E2bSandboxClient sets sandboxOwned=true on every create (E2bSandboxClient.java:67). The same shape holds for the other backends: Daytona deleteSandbox, Kubernetes terminate() unless the claim is not owned, AgentRun mcp.close() + delete.
The reuse path already exists but is unreachable.E2bSandbox.ensureSandbox() tries connectSandbox(sandboxId) before creating (E2bSandbox.java:217-241), sandboxId is persisted in E2bSandboxState and reloaded through the Priority-3 client.resume(state) path per isolation key, and AbstractBaseSandbox.start() Branch A skips the snapshot restore entirely when workspaceRootReady + probeWorkspaceRootForPreservedResume() still finds the workspace (AbstractBaseSandbox.java:70-98). Nothing ever gives those branches a live sandbox to reconnect to.
Scope:agentscope-harness (SandboxManager, SandboxFilesystemSpec — release policy belongs next to isolationScope / snapshotSpec / executionGuard, SandboxManager.java:44-48 — and SandboxAcquireResult), plus per-backend shutdown() policies in agentscope-extensions-sandbox-{e2b,daytona,kubernetes,agentrun}.
Compatibility: additive if DELETE stays the default. RETAIN requires no new timeout plumbing — E2bSandboxClientOptions.sandboxTimeoutSeconds (default 300) already is the provider-side reclaim the proposal relies on.
Four things I'd want settled before a PR:
Snapshot retention is coupled to shutdown.cleanupSnapshots() runs only from shutdown() (E2bSandbox.java:122), and the comment there spells out the reason: E2B only unlocks snapshot templates afterkillSandbox, so deleting them while a restored sandbox runs returns 400. Under RETAIN the retention policy (snapshotRetention) silently stops applying and templates accumulate. RETAIN needs an explicit GC story — a reconcile pass on reconnect, or on an actual destroy.
E2bSandboxClient.delete(Sandbox) is an empty no-op (E2bSandboxClient.java:92-93). The "explicit API for deleting a retained sandbox" the reporter asks for already has a home — it is just unimplemented for E2B. That should land withRETAIN, not after: without it there is no way to reclaim a retained sandbox for data-deletion flows.
Concurrency contract changes. The execution guard lease is carried by the acquire result and closed by SandboxLifecycleMiddleware.releaseForCall() right after sandboxManager.release(...) (SandboxManager.java:36-37,94-97, SandboxLifecycleMiddleware.java:176-189), which is safe today only because the sandbox dies with the call. A retained sandbox becomes a cross-call shared resource — two calls in the same isolation scope may connectSandbox() to the same instance and interleave writes in /home/user. Either the guard has to extend over the retained lifetime or RETAIN should be documented as single-writer-per-scope.
PAUSE should not be a common enum default.E2bPlatformHttp exposes only create/connect/snapshot/kill (E2bPlatformHttp.java:62-167) — there is no pause/resume surface, and the reporter's own measurements are against an E2B-protocol-compatible provider (Alibaba Cloud Agent Sandbox), where pause is 15.8 s synchronous. I'd model PAUSE as a per-backend optional capability (an opt-in interface next to SandboxClient) rather than a value every backend must explain.
Security note: a longer-lived sandbox keeps workspace writes (including AGENTS.md / skills/ — see #3388) and any injected credentials alive between calls, so RETAIN should probably be paired with the isolation-scope docs and a hard TTL ceiling.
Recommendation: proceed with RETAIN scoped to E2B first — the reconnect and preserved-workspace branches are already implemented, so the delta is one policy field plus the retention-GC and delete() work above, and the reported 10-19 s → ~1 s per call is a material harness win. Keep PAUSE as a follow-up pending a maintainer decision on the backend capability interface. Feasible as a contained change, but it needs a design decision on points 1 and 3 — marking for maintainer input rather than auto-generating a PR.
Hi, I'd like to work on this issue. Could you assign it to me if it's available?
I'd propose starting with E2B RETAIN support, keeping DELETE as the default and leaving PAUSE for a follow-up. The initial scope would include:
Preserving the existing stop() / snapshot persistence flow while retaining the remote sandbox between calls.
Implementing explicit deletion through E2bSandboxClient.delete().
Defining native-snapshot cleanup and retry behavior, including snapshots still in use.
Adding lifecycle tests and English/Chinese documentation for reuse, expiry/recovery, deletion, and isolation requirements.
After checking the current code, I'd like to align on two design points before implementation:
Snapshot cleanup: cleanup currently runs on shutdown. Reconciliation on reconnect or explicit deletion could reuse the existing retry bookkeeping, but reconnect-only cleanup would not cover sessions that are never used again. What cleanup guarantee should the first version provide?
Concurrency: I propose requiring a single writer per isolation key, with the execution guard covering acquisition through state persistence, rather than holding a lease while the retained sandbox is idle. I'll coordinate with fix(sandbox): serialize same-key concurrent calls to prevent persisted state races #2846 to avoid duplicating its concurrency changes.
Remote-snapshot persistence/recovery also depends on the rebinding fix in #2423 (for #2303); I'd track that dependency rather than duplicate it here. The docs would distinguish retaining a live sandbox from recovering after expiry, which requires a configured, working snapshot store.
Activity
oss-maintainer commentedon Oct 3, 2026
Issue Evaluation
Category:
enhancement| Status: Evaluated | Feasibility: feasible (RETAIN) / needs-discussion (PAUSE)All three technical premises were checked against
main(91550713) and are confirmed:SandboxManager.release()callssandbox.stop()thensandbox.shutdown()unconditionally for every self-managed result (agentscope-harness/.../sandbox/SandboxManager.java:184-212). Only Priority-1 user-managed results (SandboxAcquireResult.userManaged(external),SandboxManager.java:77) short-circuit — so today the only supported way to keep a sandbox alive across calls is for the caller to own the instance.shutdown()means delete.E2bSandbox.shutdown()callsplatform.killSandbox(id)whenevere2bState.isSandboxOwned()(.../e2b/E2bSandbox.java:112-123), andE2bSandboxClientsetssandboxOwned=trueon every create (E2bSandboxClient.java:67). The same shape holds for the other backends: DaytonadeleteSandbox, Kubernetesterminate()unless the claim is not owned, AgentRunmcp.close()+ delete.E2bSandbox.ensureSandbox()triesconnectSandbox(sandboxId)before creating (E2bSandbox.java:217-241),sandboxIdis persisted inE2bSandboxStateand reloaded through the Priority-3client.resume(state)path per isolation key, andAbstractBaseSandbox.start()Branch A skips the snapshot restore entirely whenworkspaceRootReady+probeWorkspaceRootForPreservedResume()still finds the workspace (AbstractBaseSandbox.java:70-98). Nothing ever gives those branches a live sandbox to reconnect to.Scope:
agentscope-harness(SandboxManager,SandboxFilesystemSpec— release policy belongs next toisolationScope/snapshotSpec/executionGuard,SandboxManager.java:44-48— andSandboxAcquireResult), plus per-backendshutdown()policies inagentscope-extensions-sandbox-{e2b,daytona,kubernetes,agentrun}.Compatibility: additive if
DELETEstays the default.RETAINrequires no new timeout plumbing —E2bSandboxClientOptions.sandboxTimeoutSeconds(default 300) already is the provider-side reclaim the proposal relies on.Four things I'd want settled before a PR:
cleanupSnapshots()runs only fromshutdown()(E2bSandbox.java:122), and the comment there spells out the reason: E2B only unlocks snapshot templates afterkillSandbox, so deleting them while a restored sandbox runs returns 400. UnderRETAINthe retention policy (snapshotRetention) silently stops applying and templates accumulate.RETAINneeds an explicit GC story — a reconcile pass on reconnect, or on an actual destroy.E2bSandboxClient.delete(Sandbox)is an empty no-op (E2bSandboxClient.java:92-93). The "explicit API for deleting a retained sandbox" the reporter asks for already has a home — it is just unimplemented for E2B. That should land withRETAIN, not after: without it there is no way to reclaim a retained sandbox for data-deletion flows.leaseis carried by the acquire result and closed bySandboxLifecycleMiddleware.releaseForCall()right aftersandboxManager.release(...)(SandboxManager.java:36-37,94-97,SandboxLifecycleMiddleware.java:176-189), which is safe today only because the sandbox dies with the call. A retained sandbox becomes a cross-call shared resource — two calls in the same isolation scope mayconnectSandbox()to the same instance and interleave writes in/home/user. Either the guard has to extend over the retained lifetime orRETAINshould be documented as single-writer-per-scope.E2bPlatformHttpexposes only create/connect/snapshot/kill (E2bPlatformHttp.java:62-167) — there is no pause/resume surface, and the reporter's own measurements are against an E2B-protocol-compatible provider (Alibaba Cloud Agent Sandbox), where pause is 15.8 s synchronous. I'd model PAUSE as a per-backend optional capability (an opt-in interface next toSandboxClient) rather than a value every backend must explain.Security note: a longer-lived sandbox keeps workspace writes (including
AGENTS.md/skills/— see #3388) and any injected credentials alive between calls, soRETAINshould probably be paired with the isolation-scope docs and a hard TTL ceiling.Recommendation: proceed with
RETAINscoped to E2B first — the reconnect and preserved-workspace branches are already implemented, so the delta is one policy field plus the retention-GC anddelete()work above, and the reported 10-19 s → ~1 s per call is a material harness win. KeepPAUSEas a follow-up pending a maintainer decision on the backend capability interface. Feasible as a contained change, but it needs a design decision on points 1 and 3 — marking for maintainer input rather than auto-generating a PR.Automated evaluation by github-manager-bot
Shay-Deng commentedon Oct 3, 2026
Hi, I'd like to work on this issue. Could you assign it to me if it's available?
I'd propose starting with E2B RETAIN support, keeping
DELETEas the default and leavingPAUSEfor a follow-up. The initial scope would include:stop()/ snapshot persistence flow while retaining the remote sandbox between calls.E2bSandboxClient.delete().After checking the current code, I'd like to align on two design points before implementation:
Remote-snapshot persistence/recovery also depends on the rebinding fix in #2423 (for #2303); I'd track that dependency rather than duplicate it here. The docs would distinguish retaining a live sandbox from recovering after expiry, which requires a configured, working snapshot store.
Does this scope sound reasonable?
feat(harness): add sandbox release policy to retain sandboxes between…