Skip to content

fix(e2b): restore NATIVE_SNAPSHOT sessions with a single sandbox - #3562

Merged
jujn merged 3 commits into
agentscope-ai:mainfrom
chcodex:issue-3555-eager-aardvark
Oct 15, 2026
Merged

jujn merged 3 commits into
agentscope-ai:mainfrom
chcodex:issue-3555-eager-aardvark

Conversation

@chcodex

@chcodex chcodex commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

What's wrong

A session holding a restorable native snapshot created the sandbox twice: E2bSandbox.start() called ensureSandbox() first, which created one from the configured template, and only then did the base start logic hydrate the snapshot and create a second sandbox from the snapshot id, killing the first. A restore cost four platform calls and two cold starts instead of one. If that second create failed, the first sandbox was never deleted and leaked.

Fixes #3555

## What's wrong

A session holding a restorable native snapshot created the sandbox twice:
`E2bSandbox.start()` called `ensureSandbox()` first, which created one from the
configured template, and only then did the base start logic hydrate the snapshot
and create a second sandbox from the snapshot id, killing the first. A restore
cost four platform calls and two cold starts instead of one. If that second
create failed, the first sandbox was never deleted and leaked.

## What changes

Decide the source before creating, instead of creating and rebuilding:

- `peekNativeSnapshotId()` reads the native snapshot id out of the persisted
  archive — a bounded 4096-byte read, only in `NATIVE_SNAPSHOT` mode, returning
  null on any failure so it degrades exactly like an unrestorable snapshot.
- `createSandboxForRestore()` creates straight from that snapshot when one is
  present and degrades to the original template when the snapshot cannot be
  created from, so a bad snapshot no longer fails `start()` and nothing is left
  behind to delete.
- `createdFromSnapshotId` records the snapshot the live sandbox was built from,
  so the hydrate that follows in the same start no longer rebuilds it; it is
  cleared when the sandbox was connected or built from the template.
- `shutdown()` clears the sandbox id after a successful kill, so the next resume
  does not spend a guaranteed-404 connect on a dead id.

## Verification

- `mvn -T1 -pl agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b -am test`
  → 109 tests in the module, 1245 in harness, spotless clean.
- `E2bSandboxRecoveryTest` pins the single create, the hydrate short-circuit and
  its negative path, the template fallbacks, the snapshot-create failure without
  a leak, the reconnect-failure path, and the shutdown id handling.
- `E2bNativeSnapshotRestoreIntegrationTest` (opt-in, `E2B_API_KEY`) exercises the
  live service: the snapshot id is accepted as the create template, the
  workspace comes back, and the replaced sandbox is really deleted.

Fixes agentscope-ai#3555
@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

@chcodex

chcodex commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

@oss-maintainer please review this PR.

@jujn
jujn merged commit e7bfda1 into agentscope-ai:main Oct 15, 2026
6 checks passed
jujn pushed a commit that referenced this pull request Oct 19, 2026
## Summary
- Treat only confirmed-missing native snapshots (HTTP 404 or
`isRestorable() == false`) as cold starts, with an explicit warning.
- Fail the current call on snapshot-store/read/restorability errors,
corrupt references, and non-404 snapshot creation failures, without
mutating session state — the next call retries the same snapshot instead
of silently starting empty.
- Hydrate the persisted TAR archive whenever a sandbox is recreated from
the template: a template-created sandbox can never hold the previous
workspace, so the workspace-ready flags are now cleared on every
template path. Previously a resume whose persisted state still marked
the workspace ready skipped the archive and overwrote it with an empty
workspace on the next stop.
- Carry platform HTTP status codes in typed exceptions and base retries
on those codes (408/429/502/503); 404 is never retried.
- Keep the successful restore path to a single sandbox creation and
reuse the peeked archive (`restoreCount() == 1`).
- Document the failure behavior in English and Chinese.

Related: follow-up to #3562; related closed issue #3555. (Not using
`Fixes #3555` because it is closed.)
chcodex added a commit to chcodex/agentscope-java that referenced this pull request Oct 20, 2026
…ntscope-ai#3562)

A session holding a restorable native snapshot created the sandbox
twice: `E2bSandbox.start()` called `ensureSandbox()` first, which
created one from the configured template, and only then did the base
start logic hydrate the snapshot and create a second sandbox from the
snapshot id, killing the first. A restore cost four platform calls and
two cold starts instead of one. If that second create failed, the first
sandbox was never deleted and leaked.

Fixes agentscope-ai#3555

---------

Co-authored-by: jujn <[email protected]>
(cherry picked from commit e7bfda1)
chcodex added a commit to chcodex/agentscope-java that referenced this pull request Oct 20, 2026
…#3609)

- Treat only confirmed-missing native snapshots (HTTP 404 or
`isRestorable() == false`) as cold starts, with an explicit warning.
- Fail the current call on snapshot-store/read/restorability errors,
corrupt references, and non-404 snapshot creation failures, without
mutating session state — the next call retries the same snapshot instead
of silently starting empty.
- Hydrate the persisted TAR archive whenever a sandbox is recreated from
the template: a template-created sandbox can never hold the previous
workspace, so the workspace-ready flags are now cleared on every
template path. Previously a resume whose persisted state still marked
the workspace ready skipped the archive and overwrote it with an empty
workspace on the next stop.
- Carry platform HTTP status codes in typed exceptions and base retries
on those codes (408/429/502/503); 404 is never retried.
- Keep the successful restore path to a single sandbox creation and
reuse the peeked archive (`restoreCount() == 1`).
- Document the failure behavior in English and Chinese.

Related: follow-up to agentscope-ai#3562; related closed issue agentscope-ai#3555. (Not using
`Fixes agentscope-ai#3555` because it is closed.)

(cherry picked from commit 9ea0540)
chickenlj pushed a commit that referenced this pull request Nov 13, 2026
Reuse the selected native snapshot restore source so recovery does not create a redundant sandbox; retain legacy TAR and platform contracts.

(cherry picked from commit e7bfda1)
Constraint: No AgentScope Service or SDK migration
Confidence: high
Scope-risk: narrow
Not-tested: Live provider integration and Java 17 runtime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: E2B NATIVE_SNAPSHOT restore creates two sandboxes and discards one

2 participants