AS AgentScopeAPI 手册 / API Reference

MongoSandboxExecutionGuard

public final class MongoSandboxExecutionGuard implements SandboxExecutionGuard, AutoCloseable
源码Source io/agentscope/extensions/mongodb/sandbox/MongoSandboxExecutionGuard.java:87–437

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

MongoDB-based SandboxExecutionGuard.

Uses a dedicated MongoDB collection as a distributed lock mechanism. Each lock is a document with a unique _id derived from the SandboxIsolationKey and a TTL index on expiresAt to auto-release stale locks.

Lock acquisition uses a two-step approach for correct mutual exclusion:

  • Attempt insertOne — atomic under the unique _id index; only one concurrent caller succeeds.

  • If the lock document already exists (duplicate key), attempt findOneAndUpdate (non-upsert) with a filter that matches only when expiresAt <= now — reclaiming an expired lock.

Acquisition polls until the lock is obtained or the acquisition timeout (lockTimeout) expires. The lock document itself lives for leaseTtl; while the returned SandboxLease is open, a background watchdog renews the lease every leaseTtl / 3 by pushing expiresAt forward, so an execution that outlives the initial TTL does not lose its own lock to reclamation. If renewal stops working (lock deleted or stolen), the watchdog only warns — like the Redis guard, TTL expiry remains a safety valve against permanent deadlock rather than a correctness guarantee.

Each acquisition generates a random UUID token that is written into the lock document. Renewal and release filter by this token, not by a process-level identifier, so that two guard instances in the same JVM (or two processes with the same PID) cannot accidentally release each other's locks. This mirrors the Redis guard's SET NX + Lua CAS token pattern.

The renewal executor uses a fixed-size thread pool (2 threads) so that a slow updateOne on one lease does not block renewal of other leases. The guard implements AutoCloseable and should be closed by io.agentscope.harness.agent.DistributedStore to shut down the executor when the store is closed.

嵌套类摘要Nested Class Summary

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

方法摘要Method Summary

方法Method说明Description
public static Builder builder(MongoClient mongoClient)
static
创建新的构建器。
Creates a new builder.
public SandboxLease tryEnter(SandboxIsolationKey key) throws InterruptedException
设置该参数。
Performs the try enter operation.
public void close()
关闭连接并释放相关资源。
Releases resources and connections.

方法详情Method Detail

builder

public static Builder builder(MongoClient mongoClient)
static

创建新的构建器。

Creates a new builder.

参数Parameters
名称Name类型Type说明Description
mongoClientMongoClientMongoDB 客户端。the MongoDB client
返回Returns Builder
一种新的构建器。a new builder
源码Source io/agentscope/extensions/mongodb/sandbox/MongoSandboxExecutionGuard.java:128–130

tryEnter

public SandboxLease tryEnter(SandboxIsolationKey key) throws InterruptedException

设置该参数。

Performs the try enter operation.

参数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/mongodb/sandbox/MongoSandboxExecutionGuard.java:132–216

close

public void close()

关闭连接并释放相关资源。

Releases resources and connections.

源码Source io/agentscope/extensions/mongodb/sandbox/MongoSandboxExecutionGuard.java:236–246

嵌套类详情Nested Class Detail

Builder

public static final class Builder

MongoSandboxExecutionGuard的构建器。位于 io.agentscope.extensions.mongodb.sandbox 包。核心成员包括 databaseName、collectionName、lockTimeout、leaseTtl、retryInterval、owner。

Builder for MongoSandboxExecutionGuard.

继承成员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.