[Bug]: RemoteSandboxSnapshot loses RemoteSnapshotClient after JSON deserialization, only DockerSandboxClient handles the re-binding
Describe the bug
When using a RemoteSnapshotSpec (or a subclass such as MinioSnapshotSpec), the RemoteSandboxSnapshot is serialized into SandboxState and persisted via the state store. On the next agent call, the state is deserialized from JSON via Jackson's @JsonCreator, which only reconstructs the id field but leaves the RemoteSnapshotClient field null. This causes RemoteSandboxSnapshot.isRestorable() to throw "RemoteSnapshotClient is not bound to snapshot id: ...", preventing the sandbox from starting.
Root cause
The SandboxClient interface provides a two-parameter deserializeState(String json, SandboxSnapshotSpec snapshotSpec) default method that simply delegates to the single-parameter version, ignoring the snapshotSpec:
default SandboxState deserializeState(String json, SandboxSnapshotSpec snapshotSpec) {
return deserializeState(json);
}
Only DockerSandboxClient overrides this method to re-bind the snapshot client via rebindRemoteSnapshot(). All other implementations that use RemoteSnapshotSpec are affected.
Affected clients
AgentRunSandboxClientDaytonaSandboxClientE2bSandboxClientKubernetesSandboxClientInMemorySandboxClient(theoretically, but in-memory state is not persisted across restarts)
To Reproduce
- Configure a sandbox with a
RemoteSnapshotSpec(e.g.MinioSnapshotSpec) - Configure a state store (e.g.
PgSqlAgentStateStoreorJsonFileAgentStateStore) - Send an agent message that triggers sandbox usage (first call succeeds)
- Send a second agent message with the same session scope (second call fails)
Expected behavior
All SandboxClient implementations that use RemoteSnapshotSpec should re-bind the RemoteSnapshotClient from the spec when deserializing state, matching the approach already in DockerSandboxClient.rebindRemoteSnapshot().
Fix reference
DockerSandboxClient already has the correct implementation:
private static void rebindRemoteSnapshot(SandboxState state, SandboxSnapshotSpec snapshotSpec) {
if (!(snapshotSpec instanceof RemoteSnapshotSpec remoteSnapshotSpec)) {
return;
}
SandboxSnapshot snapshot = state.getSnapshot();
if (!(snapshot instanceof RemoteSandboxSnapshot)) {
return;
}
state.setSnapshot(
new RemoteSandboxSnapshot(remoteSnapshotSpec.getClient(), snapshot.getId()));
}
The same pattern should be applied to the affected SandboxClient implementations, or the default method in SandboxClient should be updated to perform this re-binding generically.
Environment
- AgentScope-Java Version: 2.0.1-SNAPSHOT (also present in 2.0.0)
- Java Version: 21
- OS: any
Fix approaches:
Approach A: Fix each affected SandboxClient individually
Override deserializeState(String, SandboxSnapshotSpec) in each client, mirroring DockerSandboxClient:
AgentRunSandboxClient
@Override
public SandboxState deserializeState(String json, SandboxSnapshotSpec snapshotSpec) {
SandboxState state = deserializeState(json);
if (snapshotSpec instanceof RemoteSnapshotSpec rs) {
SandboxSnapshot snapshot = state.getSnapshot();
if (snapshot instanceof RemoteSandboxSnapshot && snapshot.getId() != null) {
state.setSnapshot(snapshotSpec.build(snapshot.getId()));
}
}
return state;
}
DaytonaSandboxClient, E2bSandboxClient, KubernetesSandboxClient
Apply the same rebindRemoteSnapshot() pattern from DockerSandboxClient.
Approach B: Fix in the SandboxClient interface default method (recommended)
Move rebindRemoteSnapshot() into the interface default so all implementations benefit automatically:
default SandboxState deserializeState(String json, SandboxSnapshotSpec snapshotSpec) {
SandboxState state = deserializeState(json);
rebindRemoteSnapshot(state, snapshotSpec);
return state;
}
static void rebindRemoteSnapshot(SandboxState state, SandboxSnapshotSpec snapshotSpec) {
if (!(snapshotSpec instanceof RemoteSnapshotSpec rs)) {
return;
}
SandboxSnapshot snapshot = state.getSnapshot();
if (!(snapshot instanceof RemoteSandboxSnapshot) || snapshot.getId() == null) {
return;
}
state.setSnapshot(new RemoteSandboxSnapshot(rs.getClient(), snapshot.getId()));
}
This way all existing and future SandboxClient implementations get the fix automatically, and individual overrides are only needed for custom behavior.
This is a valid bug. When RemoteSandboxSnapshot is serialized to JSON and deserialized back, the RemoteSnapshotClient field is lost (null) because Jackson only reconstructs the id field. The SandboxClient.deserializeState() default method ignores the snapshotSpec parameter, so the client is never re-bound.
Impact: Any agent using remote snapshots (Docker with remote daemon, MinIO-backed snapshots, etc.) cannot restore from persisted state — isRestorable() throws immediately.
Suggested fix: Override deserializeState() in RemoteSandboxClient (or the relevant client implementation) to:
- Deserialize the JSON to reconstruct the snapshot id
- Re-bind the RemoteSnapshotClient from the snapshotSpec
- Return a fully-functional RemoteSandboxSnapshot with the client attached
Alternatively, the default method in SandboxClient could be updated to pass the snapshotSpec through to the snapshot constructor.
Hi @jujn, could you take a look when you get a chance?
This is still reproducible on current main (c416c378) with an E2B sandbox using a MinIO-backed RemoteSnapshotSpec in the harness:
- First agent call: sandbox starts fine.
- Second call (same user scope): fails with
RemoteSnapshotClient is not bound to snapshot id: ...atRemoteSandboxSnapshot.isRestorable()→AbstractBaseSandbox.start().
We also found the same missing re-binding silently breaks snapshot persistence in the other direction: at stop(), RemoteSandboxSnapshot.persist() throws the same "not bound" error, so the workspace archive is never uploaded. This may share a root cause with the backup-on-destroy path reported in #2538.
Root cause is unchanged from the report: SandboxClient.deserializeState(json, snapshotSpec)'s default method drops the spec, and only DockerSandboxClient re-binds the client.
It looks like #2423 already implements this fix (Approach B) — approved with green CI and no merge conflicts. Could it be merged when you get a chance?
Additional evidence on current main: E2B is still affected.
E2bSandboxClientdoes not overridedeserializeState(String, SandboxSnapshotSpec), so the default method drops the spec.- A persisted
E2bSandboxStateholding aRemoteSandboxSnapshotdeserializes with only itsid;RemoteSnapshotClientisnull, andisRestorable()/restore()/persist()then throwRemoteSnapshotClient is not bound to snapshot id: .... KubernetesSandboxClientalready has a local rebind override (deserializeState(json, snapshotSpec)), andDockerSandboxClienthasrebindRemoteSnapshot().E2bSandboxClienthas neither.- All E2B serde tests call the single-argument
deserializeState(json), so CI cannot catch this.
Repro: E2B + RemoteSnapshotSpec (or a subclass) + a state store. The first call succeeds; the second call for the same scope fails while restoring the workspace.
PR #2423 (rebind in the interface default) looks like the right general fix and would cover E2B. I am preparing an E2B-specific regression test and, if #2423 is not merged soon, a focused E2B override. Please let me know if you prefer to wait for #2423 instead of a separate E2B PR.
Status update against current main:
E2bSandboxClientandAgentRunSandboxClientnow overridedeserializeState(String, SandboxSnapshotSpec)and rebind the client fromRemoteSnapshotSpec— commit c8875287, which closed #4080.DockerSandboxClientandKubernetesSandboxClientalready had the override.DaytonaSandboxClientstill implements only the single-argumentdeserializeState(String), so it inherits the default method that drops the spec. A persistedRemoteSandboxSnapshotunder Daytona still deserializes withclient == nulland fails atisRestorable()/persist().
The E2B and AgentRun paths are fixed; this issue is not fully closed for all RemoteSnapshotSpec users.
Two options:
- Merge #2423 (
fix/2303-remote-snapshot-rebind). It moves the rebind into theSandboxClientdefault method and covers every implementation, Daytona included. - Add a focused
DaytonaSandboxClientoverride mirroringE2bSandboxClient.
@chcodex which do you prefer?