Class SandboxLifecycleMiddleware
java.lang.Object
io.agentscope.harness.agent.middleware.SandboxLifecycleMiddleware
- All Implemented Interfaces:
io.agentscope.core.middleware.MiddlewareBase,HarnessRuntimeMiddleware
Middleware that manages the sandbox session lifecycle around each agent call.
Pre-
Pre-next.apply
- Read
SandboxContextfrom the currentRuntimeContext - Acquire a session via
SandboxManager - Start the session (4-branch workspace init)
- Bind the live session on the per-call
RuntimeContextfor theSandboxBackedFilesystemproxy to resolve
doFinally
- Release the session via
SandboxManager(stop + optional shutdown) - Persist sandbox session state via
SandboxManagerandSessionSandboxStateStore - 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 Summary
ConstructorsConstructorDescriptionSandboxLifecycleMiddleware(SandboxManager sandboxManager, SandboxBackedFilesystem filesystemProxy) -
Method Summary
Modifier and TypeMethodDescriptionvoidacquireForCall(io.agentscope.core.agent.RuntimeContext ctx) Acquires the sandbox for the current call.voidreleaseForCall(io.agentscope.core.agent.RuntimeContext ctx) Releases the sandbox after the current call.voidsetBeforeStartCallback(Consumer<io.agentscope.core.agent.RuntimeContext> callback) Registers a callback that runs after the sandbox session is acquired but beforeSandbox.start()applies workspace projection.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.agentscope.core.middleware.MiddlewareBase
onActing, onAgent, onModelCall, onReasoning, onSystemPrompt, order
-
Constructor Details
-
SandboxLifecycleMiddleware
public SandboxLifecycleMiddleware(SandboxManager sandboxManager, SandboxBackedFilesystem filesystemProxy)
-
-
Method Details
-
setBeforeStartCallback
Registers a callback that runs after the sandbox session is acquired but beforeSandbox.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-callRuntimeContext; may benullto clear
-
acquireForCall
public void acquireForCall(io.agentscope.core.agent.RuntimeContext ctx) Acquires the sandbox for the current call. Called fromReActAgent.beforeAgentExecution()to ensure the sandbox is available for both thecall()andstreamEvents()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 fromReActAgent.afterAgentExecution()to ensure cleanup for both paths.- Parameters:
ctx- the per-call RuntimeContext (captured at acquire time)
-