Skip to content

[Bug]: Kubernetes sandbox workspace never survives across calls — per-call pod termination makes resume fall back to fresh create and discard the persisted snapshot id #2771

Closed

Description

@Buktal

Describe the bug

With the stock KubernetesSandboxClient (Mode 2 filesystem(SandboxFilesystemSpec), a SandboxSnapshotSpec and a state store configured), workspace contents are silently lost at the end of every call, and each call orphans the previous snapshot archive.

Trigger chain:

  1. KubernetesSandboxClient.create() sets claimOwned = true. HarnessAgent auto-registers SandboxLifecycleMiddleware, which releases the sandbox after every call: SandboxManager.release() (self-managed path) → stop() (uploads the workspace archive under snapshot.getId()) → shutdown(). KubernetesSandbox.shutdown() sees claimOwned == true and calls terminate(), deleting the claim and the pod — every call.
  2. On the next call, SandboxManager.acquire() Priority 3 loads the persisted state and calls KubernetesSandboxClient.resume() → getSandbox(claimName, namespace), which throws because the claim was terminated in step 1. acquire() catches this, logs a warn, and falls through to Priority 4 fresh create.
  3. Priority 4 creates a sandbox with a new sessionId, so snapshotSpec.build(newSessionId) produces a new snapshot id. The previous archive is never referenced again (orphaned), and start() runs the fresh-init branch (workspace rebuilt from spec only) — all files written in previous calls are gone. The only trace is the Priority 3 fallback warn log; the agent call itself succeeds.

This contradicts the documented 4-branch workspace-start logic in AbstractBaseSandbox.start(), whose Branch B/C exist precisely to restore the workspace from the snapshot once the container is gone. On Docker this works: DockerSandboxClient.resume() does not require a live container, so the next call reaches Branch B and restores the workspace. On Kubernetes, the per-call claim termination in step 1 makes those restore branches unreachable, and the Priority 4 fallback drops the persisted snapshot id.

To Reproduce

  1. Build a HarnessAgent with filesystem(new KubernetesFilesystemSpec(...).snapshotSpec(new RemoteSnapshotSpec(client)) ...) and a distributed state store.
  2. First call: write a file into the sandbox workspace. Observe the archive uploaded to remote storage and the claim/pod terminated at release.
  3. Second call with the same scope: observe in the logs Failed to load persisted state for scope ..., falling through to fresh create, a new pod with a new sessionId, an empty workspace, and the previous archive orphaned in remote storage.

Expected behavior

The workspace persists across calls via the snapshot mechanism, matching the Docker behavior. Either:

  • resume() should not hard-fail when the claim is gone, letting start() degrade to the Branch B restore path, or
  • the fresh-create fallback should seed the new sandbox's snapshot with the persisted snapshot id so the workspace is restored from the previous archive.

Error messages

[sandbox] Failed to load persisted state for scope ..., falling through to fresh create: ...

Then a silent workspace reset — no further error.

Environment (please complete the following information:)

  • AgentScope-Java Version: 2.0.1 (also verified on current main bf7b7dae)
  • Java Version: 21
  • OS: any
  • Module: agentscope-extensions-sandbox-kubernetes

Additional context

Workaround in use locally: subclass KubernetesSandboxClient and flip claimOwned to false right after create(), so the per-call shutdown() only closes the connection and the pod survives across calls; eviction deletes the pod explicitly. This confirms the trigger chain above but is fragile — it depends on overriding lifecycle ownership semantics that the harness assumes it controls.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions