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

[Bug]: KubernetesFilesystemSpec + JdbcSnapshotSpec: RemoteSandboxSnapshot deserialization fails on second call due to @JsonTypeInfo "type" field conflict

Open Buktal opened this issue 5 months ago • 0 comments

Describe the bug

Using KubernetesFilesystemSpec together with JdbcSnapshotSpec causes a SandboxConfigurationException on every request after the first one. The first request succeeds and persists a KubernetesSandboxState (containing a RemoteSandboxSnapshot) to the AgentStateStore. On the next request, SandboxLifecycleMiddleware calls SandboxManager.acquire, which calls KubernetesSandboxClient.deserializeState on the stored JSON, and Jackson throws UnrecognizedPropertyException: Unrecognized field "type" against RemoteSandboxSnapshot.

Root cause summary:

  1. RemoteSandboxSnapshot.getType() conflicts with @JsonTypeInfo(As.PROPERTY, property = "type") on the SandboxSnapshot interface.

    SandboxSnapshot is annotated with:

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
    @JsonSubTypes({
        @JsonSubTypes.Type(value = RemoteSandboxSnapshot.class, name = "remote"),
        ...
    })
    

    This causes Jackson to emit a "type": "remote" discriminator field when serializing RemoteSandboxSnapshot. During deserialization, Jackson resolves the subtype correctly, but then the concrete RemoteSandboxSnapshot class encounters "type" again as an ordinary field. Because RemoteSandboxSnapshot has a getType() method (returning "remote") but no corresponding setter or constructor parameter, and is not annotated with @JsonIgnoreProperties(ignoreUnknown = true), Jackson fails with UnrecognizedPropertyException.

    The source-code comment on RemoteSandboxSnapshot already acknowledges the class is "not directly serializable to JSON because RemoteSnapshotClient cannot be serialized", yet no guard is in place to prevent it from being serialized as part of KubernetesSandboxState.

    Affected locations:

    • agentscope-harness/.../snapshot/RemoteSandboxSnapshot.java — missing @JsonIgnoreProperties(ignoreUnknown = true) and @JsonIgnore on getType()
    • agentscope-extensions-sandbox-kubernetes/.../KubernetesSandboxClient.java — deserializeState wraps the Jackson exception but the root cause originates in the snapshot layer
  2. No build-time guard for the incompatible KubernetesFilesystemSpec + JdbcSnapshotSpec combination.

    JdbcSnapshotSpec creates a RemoteSandboxSnapshot at runtime (holding a live RemoteSnapshotClient). When this snapshot is embedded in KubernetesSandboxState and serialized to the AgentStateStore, the client field cannot be meaningfully restored on the next call. The combination should either be disallowed at HarnessAgent.build() time with a clear error, or RemoteSandboxSnapshot must be made round-trip safe.

To Reproduce

  1. Configure a HarnessAgent with KubernetesFilesystemSpec and JdbcSnapshotSpec:
    HarnessAgent agent = HarnessAgent.builder()
        .stateStore(redisAgentStateStore)
        .filesystem(new KubernetesFilesystemSpec()
            .namespace("sandbox-ns")
            .image("ubuntu:24.04")
            .isolationScope(IsolationScope.USER)
            .snapshotSpec(new JdbcSnapshotSpec(dataSource, "agent_sandbox_snapshots")))
        .build();
    
  2. Call agent.streamEvents(msgs, ctx) for a session — this succeeds and persists KubernetesSandboxState (with RemoteSandboxSnapshot) to the store.
  3. Call agent.streamEvents(msgs, ctx) again for the same session.
  4. Observe the exception on step 3.

Expected behavior

The sandbox state is restored successfully on subsequent calls, and the agent continues executing in the same Kubernetes sandbox.

Error messages

io.agentscope.harness.agent.sandbox.SandboxException$SandboxConfigurationException: Failed to deserialize Kubernetes sandbox state
    at io.agentscope.extensions.sandbox.kubernetes.KubernetesSandboxClient.deserializeState(KubernetesSandboxClient.java:129)
    at io.agentscope.harness.agent.sandbox.SandboxManager.acquire(SandboxManager.java:105)
    at io.agentscope.harness.agent.middleware.SandboxLifecycleMiddleware.acquireForCall(SandboxLifecycleMiddleware.java:82)
    at io.agentscope.harness.agent.HarnessAgent.lambda$wrappedStreamEvents$22(HarnessAgent.java:779)
    at reactor.core.publisher.FluxUsing.subscribe(FluxUsing.java:75)
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "type"
    (class io.agentscope.harness.agent.sandbox.snapshot.RemoteSandboxSnapshot),
    not marked as ignorable (2 known properties: "client", "id")
    (through reference chain: io.agentscope.extensions.sandbox.kubernetes.KubernetesSandboxState["snapshot"]
    ->io.agentscope.harness.agent.sandbox.snapshot.RemoteSandboxSnapshot["type"])

Proposed fix

Option A — Make RemoteSandboxSnapshot round-trip safe (preferred):

Add @JsonIgnoreProperties(ignoreUnknown = true) and annotate getType() with @JsonIgnore to prevent the @JsonTypeInfo discriminator from colliding with the bean properties. Also add a @JsonCreator that reconstructs the object from only id (the client must be re-injected from the spec on resume):

// RemoteSandboxSnapshot.java
@JsonIgnoreProperties(ignoreUnknown = true)
public class RemoteSandboxSnapshot implements SandboxSnapshot {

    @JsonIgnore
    private final RemoteSnapshotClient client;
    private final String id;

    @JsonCreator
    public RemoteSandboxSnapshot(@JsonProperty("id") String id) {
        this.client = null; // client re-injected by JdbcSnapshotSpec on resume
        this.id = id;
    }

    @JsonIgnore
    @Override
    public String getType() { return "remote"; }
    ...
}

Option B — Guard at build time:

In HarnessAgent.Builder.build(), detect the incompatible combination and throw a descriptive IllegalArgumentException:

if (filesystem instanceof KubernetesFilesystemSpec ks
        && ks.getSnapshotSpec() instanceof JdbcSnapshotSpec) {
    throw new IllegalArgumentException(
        "JdbcSnapshotSpec is not compatible with KubernetesFilesystemSpec. " +
        "Use OssSnapshotSpec or NoopSnapshotSpec instead.");
}

Workaround (until fixed)

Replace JdbcSnapshotSpec with NoopSnapshotSpec (no snapshot persistence; sandbox cold-starts if the pod is lost) or OssSnapshotSpec (cross-replica resumable):

.snapshotSpec(new NoopSnapshotSpec())
// or
.snapshotSpec(new OssSnapshotSpec(ossClient, "my-bucket", "agentscope/snapshots/"))

Environment

  • AgentScope-Java Version: 2.0.0-RC2
  • Java Version: 21
  • OS: Linux (Kubernetes cluster)

Buktal avatar Jun 11 '26 09:06 Buktal