Class SandboxLifecycleMiddleware

java.lang.Object
io.agentscope.harness.agent.middleware.SandboxLifecycleMiddleware
All Implemented Interfaces:
io.agentscope.core.middleware.MiddlewareBase, HarnessRuntimeMiddleware

public class SandboxLifecycleMiddleware extends Object implements HarnessRuntimeMiddleware
Middleware that manages the sandbox session lifecycle around each agent call.

Pre-next.apply

  1. Read SandboxContext from the current RuntimeContext
  2. Acquire a session via SandboxManager
  3. Start the session (4-branch workspace init)
  4. Bind the live session on the per-call RuntimeContext for the SandboxBackedFilesystem proxy to resolve

doFinally

  1. Release the session via SandboxManager (stop + optional shutdown)
  2. Persist sandbox session state via SandboxManager and SessionSandboxStateStore
  3. Clear this call's session binding from the RuntimeContext

Post-call failures (persist, release) are logged but do not propagate — this ensures the agent call result is always returned to the caller even if sandbox cleanup fails.

The sandbox is bound per call on the invocation's RuntimeContext rather than on a shared agent-level slot: distinct (userId, sessionId) sessions run in parallel on the same agent bean, so a shared slot would let concurrent calls corrupt each other's binding (issue #2490).

  • Constructor Details

  • Method Details

    • setBeforeStartCallback

      public void setBeforeStartCallback(Consumer<io.agentscope.core.agent.RuntimeContext> callback)
      Registers a callback that runs after the sandbox session is acquired but before Sandbox.start() applies workspace projection. This allows callers to materialise resources on the host workspace (e.g. .skills-cache/) so that projection picks them up in the same call.
      Parameters:
      callback - receives the per-call RuntimeContext; may be null to clear
    • acquireForCall

      public void acquireForCall(io.agentscope.core.agent.RuntimeContext ctx)
      Acquires the sandbox for the current call. Called from ReActAgent.beforeAgentExecution() to ensure the sandbox is available for both the call() and streamEvents() paths.
      Parameters:
      ctx - the per-call RuntimeContext (must not be null)
    • releaseForCall

      public void releaseForCall(io.agentscope.core.agent.RuntimeContext ctx)
      Releases the sandbox after the current call. Called from ReActAgent.afterAgentExecution() to ensure cleanup for both paths.
      Parameters:
      ctx - the per-call RuntimeContext (captured at acquire time)