Class RedisSandboxExecutionGuard
java.lang.Object
io.agentscope.extensions.redis.sandbox.RedisSandboxExecutionGuard
- All Implemented Interfaces:
io.agentscope.harness.agent.sandbox.SandboxExecutionGuard
public final class RedisSandboxExecutionGuard
extends Object
implements io.agentscope.harness.agent.sandbox.SandboxExecutionGuard
Redis-backed
SandboxExecutionGuard that serialises concurrent access to a sandbox
isolation slot using a Redis SET NX PX lease.
Mechanism
tryEnter(io.agentscope.harness.agent.sandbox.SandboxIsolationKey)writes 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-agentagentscope: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(io.agentscope.harness.agent.sandbox.SandboxIsolationKey)blocks 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
Nested ClassesNested classes/interfaces inherited from interface io.agentscope.harness.agent.sandbox.SandboxExecutionGuard
io.agentscope.harness.agent.sandbox.SandboxExecutionGuard.NoopSandboxExecutionGuard -
Method Summary
Modifier and TypeMethodDescriptionbuilder(redis.clients.jedis.UnifiedJedis jedis) Creates a builder for this guard.io.agentscope.harness.agent.sandbox.SandboxLeasetryEnter(io.agentscope.harness.agent.sandbox.SandboxIsolationKey key) Acquires the Redis-backed lease forkey, spinning atretryIntervaluntil the slot is free.
-
Method Details
-
builder
Creates a builder for this guard.- Parameters:
jedis- initialized Jedis client; the same instance used forRedisSnapshotSpecis fine
-
tryEnter
public io.agentscope.harness.agent.sandbox.SandboxLease tryEnter(io.agentscope.harness.agent.sandbox.SandboxIsolationKey key) throws InterruptedException Acquires the Redis-backed lease forkey, spinning atretryIntervaluntil the slot is free.- Specified by:
tryEnterin interfaceio.agentscope.harness.agent.sandbox.SandboxExecutionGuard- Throws:
InterruptedException- if the calling thread is interrupted while waiting
-