RedisSandboxExecutionGuard
public final class RedisSandboxExecutionGuard implements SandboxExecutionGuard
io/agentscope/extensions/redis/sandbox/RedisSandboxExecutionGuard.java:95–262Redis 沙箱执行守卫,在多次调用之间保存和恢复会话与记忆数据。位于 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
-
#tryEnterwrites a unique token to a Redis key withSET NX PX <ttlMs>. If the key is already held by another caller, it retries atretryIntervalintervals 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:
-
#tryEnterblocks until the slot is acquired or the thread is interrupted. -
The returned
SandboxLeasereleases the slot idempotently onSandboxLease#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)
创建构建器用于此守卫。
Creates a builder for this guard.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
jedis | UnifiedJedis | jedis(类型 `UnifiedJedis`)。initialized Jedis client; the same instance used for `io.agentscope.extensions.redis.snapshot.RedisSnapshotSpec` is fine |
Builderio/agentscope/extensions/redis/sandbox/RedisSandboxExecutionGuard.java:128–130tryEnter
public SandboxLease tryEnter(SandboxIsolationKey key) throws InterruptedException
设置该参数。
Acquires the Redis-backed lease for key, spinning at retryInterval until the slot is free.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | SandboxIsolationKey | 键(类型 `SandboxIsolationKey`)。The key. |
SandboxLeaseInterruptedException可能抛出 `InterruptedException`。Thrown when the operation cannot be completed.
io/agentscope/extensions/redis/sandbox/RedisSandboxExecutionGuard.java:138–159嵌套类详情Nested Class Detail
Builder
public static final class BuilderRedisSandboxExecutionGuard的构建器。位于 io.agentscope.extensions.redis.sandbox 包。核心成员包括 keyPrefix、leaseTtl、retryInterval、build。
Builder for RedisSandboxExecutionGuard.