[Bug]: KubernetesFilesystemSpec + JdbcSnapshotSpec: RemoteSandboxSnapshot deserialization fails on second call due to @JsonTypeInfo "type" field conflict
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:
-
RemoteSandboxSnapshot.getType()conflicts with@JsonTypeInfo(As.PROPERTY, property = "type")on theSandboxSnapshotinterface.SandboxSnapshotis 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 serializingRemoteSandboxSnapshot. During deserialization, Jackson resolves the subtype correctly, but then the concreteRemoteSandboxSnapshotclass encounters"type"again as an ordinary field. BecauseRemoteSandboxSnapshothas agetType()method (returning"remote") but no corresponding setter or constructor parameter, and is not annotated with@JsonIgnoreProperties(ignoreUnknown = true), Jackson fails withUnrecognizedPropertyException.The source-code comment on
RemoteSandboxSnapshotalready acknowledges the class is "not directly serializable to JSON becauseRemoteSnapshotClientcannot be serialized", yet no guard is in place to prevent it from being serialized as part ofKubernetesSandboxState.Affected locations:
agentscope-harness/.../snapshot/RemoteSandboxSnapshot.java— missing@JsonIgnoreProperties(ignoreUnknown = true)and@JsonIgnoreongetType()agentscope-extensions-sandbox-kubernetes/.../KubernetesSandboxClient.java—deserializeStatewraps the Jackson exception but the root cause originates in the snapshot layer
-
No build-time guard for the incompatible
KubernetesFilesystemSpec+JdbcSnapshotSpeccombination.JdbcSnapshotSpeccreates aRemoteSandboxSnapshotat runtime (holding a liveRemoteSnapshotClient). When this snapshot is embedded inKubernetesSandboxStateand serialized to theAgentStateStore, theclientfield cannot be meaningfully restored on the next call. The combination should either be disallowed atHarnessAgent.build()time with a clear error, orRemoteSandboxSnapshotmust be made round-trip safe.
To Reproduce
- Configure a
HarnessAgentwithKubernetesFilesystemSpecandJdbcSnapshotSpec: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(); - Call
agent.streamEvents(msgs, ctx)for a session — this succeeds and persistsKubernetesSandboxState(withRemoteSandboxSnapshot) to the store. - Call
agent.streamEvents(msgs, ctx)again for the same session. - 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)