AS AgentScopeAPI 手册 / API Reference

RedisSandboxExecutionGuard

public final class RedisSandboxExecutionGuard implements SandboxExecutionGuard
源码Source io/agentscope/extensions/redis/sandbox/RedisSandboxExecutionGuard.java:95–262

Redis 沙箱执行守卫,在多次调用之间保存和恢复会话与记忆数据。位于 io.agentscope.extensions.redis.sandbox 包,实现 SandboxExecutionGuard。核心成员包括 builder、tryEnter。

Redis-backed SandboxExecutionGuard that serialises concurrent access to a sandbox isolation slot using a Redis SET NX PX lease.

Mechanism

  • #tryEnter writes a unique token to a Redis key with SET NX PX <ttlMs>. If the key is already held by another caller, it retries at retryInterval intervals until the slot becomes free or the thread is interrupted.

  • The returned SandboxLease#close() executes a Lua CAS script that deletes the key only when the stored value equals the caller's token, preventing another holder's lock from being released by mistake.

Key format

`<keyPrefix><scope_lower>:<value>`

Examples (with default prefix agentscope:sandbox:lock:):

  • agentscope:sandbox:lock:agent:code-agent

  • agentscope:sandbox:lock:global:__global__

Usage

`UnifiedJedis jedis = new JedisPooled("localhost", 6379);

SandboxExecutionGuard guard = RedisSandboxExecutionGuard.builder(jedis)
.leaseTtl(Duration.ofMinutes(30)) // must exceed worst-case call duration
.retryInterval(Duration.ofMillis(500))
.build();

HarnessAgent.builder()
.filesystem(new DockerFilesystemSpec()
.isolationScope(IsolationScope.AGENT)
.executionGuard(guard))
...
.build();
`

TTL guidance

Set leaseTtl to comfortably exceed the worst-case agent call duration (including retries and LLM latency). If a call outlasts the TTL, Redis will evict the key and a concurrent caller may enter — this is a safety valve to prevent permanent deadlock, not an indication that two callers will safely share state. Monitor call durations and size the TTL accordingly.

Extending this guard

This class serves as the canonical reference implementation for SandboxExecutionGuard. Alternative stores (ZooKeeper, etcd, database advisory locks, …) should follow the same contract:

  • #tryEnter blocks until the slot is acquired or the thread is interrupted.

  • The returned SandboxLease releases the slot idempotently on SandboxLease#close().

  • Release failures must be logged and swallowed — never thrown — so the harness post-call path always completes.

嵌套类摘要Nested Class Summary

类Class类型Kind说明Description
Builder类class
`RedisSandboxExecutionGuard`的构建器。
Builder for `RedisSandboxExecutionGuard`.

方法摘要Method Summary

方法Method说明Description
public static Builder builder(UnifiedJedis jedis)
static
创建构建器用于此守卫。
Creates a builder for this guard.
public SandboxLease tryEnter(SandboxIsolationKey key) throws InterruptedException
设置该参数。
Acquires the Redis-backed lease for `key`, spinning at `retryInterval` until the slot is free.

方法详情Method Detail

builder

public static Builder builder(UnifiedJedis jedis)
static

创建构建器用于此守卫。

Creates a builder for this guard.

参数Parameters
名称Name类型Type说明Description
jedisUnifiedJedisjedis(类型 `UnifiedJedis`)。initialized Jedis client; the same instance used for `io.agentscope.extensions.redis.snapshot.RedisSnapshotSpec` is fine
返回Returns Builder
返回当前实例,以支持链式调用。Returns the current instance for chaining.
源码Source io/agentscope/extensions/redis/sandbox/RedisSandboxExecutionGuard.java:128–130

tryEnter

public SandboxLease tryEnter(SandboxIsolationKey key) throws InterruptedException

设置该参数。

Acquires the Redis-backed lease for key, spinning at retryInterval until the slot is free.

参数Parameters
名称Name类型Type说明Description
keySandboxIsolationKey键(类型 `SandboxIsolationKey`)。The key.
返回Returns SandboxLease
返回SandboxLease。Returns sandbox lease.
抛出Throws
  • InterruptedException 可能抛出 `InterruptedException`。Thrown when the operation cannot be completed.
源码Source io/agentscope/extensions/redis/sandbox/RedisSandboxExecutionGuard.java:138–159

嵌套类详情Nested Class Detail

Builder

public static final class Builder

RedisSandboxExecutionGuard的构建器。位于 io.agentscope.extensions.redis.sandbox 包。核心成员包括 keyPrefix、leaseTtl、retryInterval、build。

Builder for RedisSandboxExecutionGuard.

继承成员Inherited Members

继承自Methods inherited from SandboxExecutionGuard
基于官方固定版本源码静态生成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.