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

  1. tryEnter(io.agentscope.harness.agent.sandbox.SandboxIsolationKey) 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.
  2. 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(io.agentscope.harness.agent.sandbox.SandboxIsolationKey) 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

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 

    Nested classes/interfaces inherited from interface io.agentscope.harness.agent.sandbox.SandboxExecutionGuard

    io.agentscope.harness.agent.sandbox.SandboxExecutionGuard.NoopSandboxExecutionGuard
  • Method Summary

    Modifier and Type
    Method
    Description
    builder(redis.clients.jedis.UnifiedJedis jedis)
    Creates a builder for this guard.
    io.agentscope.harness.agent.sandbox.SandboxLease
    tryEnter(io.agentscope.harness.agent.sandbox.SandboxIsolationKey key)
    Acquires the Redis-backed lease for key, spinning at retryInterval until the slot is free.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • builder

      public static RedisSandboxExecutionGuard.Builder builder(redis.clients.jedis.UnifiedJedis jedis)
      Creates a builder for this guard.
      Parameters:
      jedis - initialized Jedis client; the same instance used for RedisSnapshotSpec is fine
    • tryEnter

      public io.agentscope.harness.agent.sandbox.SandboxLease tryEnter(io.agentscope.harness.agent.sandbox.SandboxIsolationKey key) throws InterruptedException
      Acquires the Redis-backed lease for key, spinning at retryInterval until the slot is free.
      Specified by:
      tryEnter in interface io.agentscope.harness.agent.sandbox.SandboxExecutionGuard
      Throws:
      InterruptedException - if the calling thread is interrupted while waiting