agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: RemoteSandboxSnapshot loses RemoteSnapshotClient after JSON deserialization, only DockerSandboxClient handles the re-binding

Open Peng-io opened this issue 4 months ago • 5 comments

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

  • AgentRunSandboxClient
  • DaytonaSandboxClient
  • E2bSandboxClient
  • KubernetesSandboxClient
  • InMemorySandboxClient (theoretically, but in-memory state is not persisted across restarts)

To Reproduce

  1. Configure a sandbox with a RemoteSnapshotSpec (e.g. MinioSnapshotSpec)
  2. Configure a state store (e.g. PgSqlAgentStateStore or JsonFileAgentStateStore)
  3. Send an agent message that triggers sandbox usage (first call succeeds)
  4. 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

Peng-io avatar Jul 19 '26 11:07 Peng-io

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.

Peng-io avatar Jul 19 '26 11:07 Peng-io

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:

  1. Deserialize the JSON to reconstruct the snapshot id
  2. Re-bind the RemoteSnapshotClient from the snapshotSpec
  3. 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.

oss-maintainer avatar Jul 28 '26 09:07 oss-maintainer

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:

  1. First agent call: sandbox starts fine.
  2. Second call (same user scope): fails with RemoteSnapshotClient is not bound to snapshot id: ... at RemoteSandboxSnapshot.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?

Peng-io avatar Oct 16 '26 03:10 Peng-io

Additional evidence on current main: E2B is still affected.

  • E2bSandboxClient does not override deserializeState(String, SandboxSnapshotSpec), so the default method drops the spec.
  • A persisted E2bSandboxState holding a RemoteSandboxSnapshot deserializes with only its id; RemoteSnapshotClient is null, and isRestorable() / restore() / persist() then throw RemoteSnapshotClient is not bound to snapshot id: ....
  • KubernetesSandboxClient already has a local rebind override (deserializeState(json, snapshotSpec)), and DockerSandboxClient has rebindRemoteSnapshot(). E2bSandboxClient has 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.

chcodex avatar Oct 20 '26 13:10 chcodex

Status update against current main:

  • E2bSandboxClient and AgentRunSandboxClient now override deserializeState(String, SandboxSnapshotSpec) and rebind the client from RemoteSnapshotSpec — commit c8875287, which closed #4080.
  • DockerSandboxClient and KubernetesSandboxClient already had the override.
  • DaytonaSandboxClient still implements only the single-argument deserializeState(String), so it inherits the default method that drops the spec. A persisted RemoteSandboxSnapshot under Daytona still deserializes with client == null and fails at isRestorable() / persist().

The E2B and AgentRun paths are fixed; this issue is not fully closed for all RemoteSnapshotSpec users.

Two options:

  1. Merge #2423 (fix/2303-remote-snapshot-rebind). It moves the rebind into the SandboxClient default method and covers every implementation, Daytona included.
  2. Add a focused DaytonaSandboxClient override mirroring E2bSandboxClient.

@chcodex which do you prefer?

Peng-io avatar Nov 11 '26 15:11 Peng-io