AS AgentScopeAPI 手册 / API Reference

SandboxLifecycleMiddleware

public class SandboxLifecycleMiddleware implements HarnessRuntimeMiddleware
源码Source io/agentscope/harness/agent/middleware/SandboxLifecycleMiddleware.java:56–180

沙箱生命周期中间件,在智能体执行流程中提供横切扩展。位于 io.agentscope.harness.agent.middleware 包,实现 HarnessRuntimeMiddleware。核心成员包括 setBeforeStartCallback、acquireForCall、releaseForCall、SandboxLifecycleMiddleware。

Middleware that manages the sandbox session lifecycle around each agent call.

Pre-next.apply

  • Read SandboxContext from the current RuntimeContext

  • Acquire a session via SandboxManager

  • Start the session (4-branch workspace init)

  • Bind the live session on the per-call RuntimeContext for the SandboxBackedFilesystem proxy to resolve

doFinally

  • Release the session via SandboxManager (stop + optional shutdown)

  • Persist sandbox session state via SandboxManager and io.agentscope.harness.agent.sandbox.SessionSandboxStateStore

  • 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

构造器Constructor说明Description
public SandboxLifecycleMiddleware(SandboxManager sandboxManager, SandboxBackedFilesystem filesystemProxy)
初始化 `SandboxLifecycleMiddleware` 实例。
Creates a new instance.

方法摘要Method Summary

方法Method说明Description
public void setBeforeStartCallback(Consumer<RuntimeContext> callback)
设置之前启动回调。
Registers a callback that runs after the sandbox session is acquired but before `io.agentscope.harness.agent.sandbox.Sandbox#start()` applies workspace projection.
public void acquireForCall(RuntimeContext ctx)
设置该参数。
Acquires the sandbox for the current call.
public void releaseForCall(RuntimeContext ctx)
设置该参数。
Releases the sandbox after the current call.

构造器详情Constructor Detail

SandboxLifecycleMiddleware

public SandboxLifecycleMiddleware(SandboxManager sandboxManager, SandboxBackedFilesystem filesystemProxy)

初始化 SandboxLifecycleMiddleware 实例。

Creates a new instance.

参数Parameters
名称Name类型Type说明Description
sandboxManagerSandboxManager沙箱管理器(类型 `SandboxManager`)。The sandbox manager.
filesystemProxySandboxBackedFilesystem文件系统代理(类型 `SandboxBackedFilesystem`)。The filesystem proxy.
源码Source io/agentscope/harness/agent/middleware/SandboxLifecycleMiddleware.java:64–68

方法详情Method Detail

setBeforeStartCallback

public void setBeforeStartCallback(Consumer<RuntimeContext> callback)

设置之前启动回调。

Registers a callback that runs after the sandbox session is acquired but before io.agentscope.harness.agent.sandbox.Sandbox#start() applies workspace projection.

参数Parameters
名称Name类型Type说明Description
callbackConsumer<RuntimeContext>回调函数(类型 `Consumer`)。receives the per-call `RuntimeContext`; may be `null` to clear
源码Source io/agentscope/harness/agent/middleware/SandboxLifecycleMiddleware.java:78–80

acquireForCall

public void acquireForCall(RuntimeContext ctx)

设置该参数。

Acquires the sandbox for the current call.

参数Parameters
名称Name类型Type说明Description
ctxRuntimeContext运行时上下文(类型 `RuntimeContext`)。the per-call RuntimeContext (must not be null)
源码Source io/agentscope/harness/agent/middleware/SandboxLifecycleMiddleware.java:89–143

releaseForCall

public void releaseForCall(RuntimeContext ctx)

设置该参数。

Releases the sandbox after the current call.

参数Parameters
名称Name类型Type说明Description
ctxRuntimeContext运行时上下文(类型 `RuntimeContext`)。the per-call RuntimeContext (captured at acquire time)
源码Source io/agentscope/harness/agent/middleware/SandboxLifecycleMiddleware.java:151–179

继承成员Inherited Members

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.