diff --git a/agentscope-harness/src/test/java/io/agentscope/harness/agent/sandbox/SandboxManagerIsolationTest.java b/agentscope-harness/src/test/java/io/agentscope/harness/agent/sandbox/SandboxManagerIsolationTest.java index 9d117b8970..e7b86ecf2a 100644 --- a/agentscope-harness/src/test/java/io/agentscope/harness/agent/sandbox/SandboxManagerIsolationTest.java +++ b/agentscope-harness/src/test/java/io/agentscope/harness/agent/sandbox/SandboxManagerIsolationTest.java @@ -405,6 +405,9 @@ void constructor_blankAgentId_failsAtBuildTime() { IllegalArgumentException.class, () -> new SandboxManager(client, stateStore, " ")); assertTrue(ex.getMessage().contains("must not be null or blank")); + // The null path is a distinct branch of the same check. + assertThrows( + IllegalArgumentException.class, () -> new SandboxManager(client, stateStore, null)); } /** A store mock whose agent id matches the given value, as real wiring always does. */ diff --git a/docs/v2/en/docs/harness/sandbox.md b/docs/v2/en/docs/harness/sandbox.md index a58d6d315a..bc60ac4dc4 100644 --- a/docs/v2/en/docs/harness/sandbox.md +++ b/docs/v2/en/docs/harness/sandbox.md @@ -117,6 +117,10 @@ If you're using a local `AgentStateStore` (the default `JsonFileAgentStateStore` In `USER` / `AGENT` / `GLOBAL` modes across replicas, two replicas serving the same user and agent concurrently both write to the same slot — last writer wins. If that's not OK, you need a distributed lock. +The guard key mirrors the persisted slot's granularity: for `USER` scope it is `{agentId}/{userId}`. A subagent spawned inside a sandboxed call therefore takes its own lock instead of colliding with the one its parent holds for the whole call. `GLOBAL` scope cannot be combined with subagents at all — the build fails with an explicit error, because a child spawned inside the parent call would otherwise block on the lease the parent already holds. + +One upgrade consequence to plan for: during a rolling upgrade old nodes keep locking `user:` while new ones take `user:/` — the names never meet, so peers of different versions don't serialise and can write the same slot concurrently (last write wins). Old binaries cannot be taught the new key format, so the mixed-version window is only safe if the two versions never serve the same `{agentId}/{userId}` at once: stop routing affected traffic to old replicas and let their in-flight calls drain before new replicas take over those users. If mixed-version rolling upgrades are a supported mode for your deployment, treat this as a release gate. + **Recommended**: use `distributedStore(...)` — snapshot and execution guard are auto-injected: ```java diff --git a/docs/v2/en/docs/harness/subagent.md b/docs/v2/en/docs/harness/subagent.md index 96010d0a4c..24775b6398 100644 --- a/docs/v2/en/docs/harness/subagent.md +++ b/docs/v2/en/docs/harness/subagent.md @@ -178,6 +178,8 @@ No spec file needed; always available. Its role is "generic fallback" — it mir - **ISOLATED** (default): the subagent has its own workspace (if `workspace.path` is omitted, the framework auto-creates a subdirectory). Subagent runtime state is bucketed per "parent sessionId × user" — so spawning the same subagent across different conversations of the same user doesn't cross-contaminate. - **SHARED**: the subagent uses the parent's workspace directly. Good for cases where the subagent's output is read by the parent immediately (e.g. `general-purpose`). +**Sandbox mode**: when the parent runs in a sandbox, an ISOLATED declared subagent installs its own sandbox context and workspace projection — it does not inherit the parent's (caller-managed sharing through `externalSandbox` / `externalSandboxState` is still honored), so what you see is your own projection, not the parent's. `general-purpose` is the exception — it shares the parent's sandbox. With `USER` isolation the execution guard is keyed by `{agentId}/{userId}`, so parent and child no longer block each other. `GLOBAL` isolation cannot be combined with subagents — the build fails with an explicit error, since a child spawned inside a call would otherwise block on the lease the parent already holds. + ## Sync or background? The parent creates a subagent with `agent_spawn`; the key knob is `timeout_seconds`: diff --git a/docs/v2/zh/docs/harness/sandbox.md b/docs/v2/zh/docs/harness/sandbox.md index bf1de986f1..6fe831f8ae 100644 --- a/docs/v2/zh/docs/harness/sandbox.md +++ b/docs/v2/zh/docs/harness/sandbox.md @@ -117,6 +117,10 @@ HarnessAgent.builder() `USER` / `AGENT` / `GLOBAL` 模式在多副本下,两个副本同时处理同一「用户 + agent」的请求会都把状态写到同一个 slot,最后写入的为准。如果你不想这样,需要一把分布式锁。 +执行锁的 key 与持久化 slot 同粒度:`USER` 作用域下是 `{agentId}/{userId}`。所以沙箱中 spawn 的子 agent 会用自己的锁,不会和父 call 持有的锁冲突;`GLOBAL` 作用域完全不能与子 agent 组合——构建期会直接报错拒绝,否则 call 内 spawn 的子 agent 会阻塞在父已持有的租约上。 + +滚动升级的一个后果需要规划:升级期间旧节点仍锁 `user:`,新节点锁 `user:/`——两者永不相遇,不同版本的节点之间不互斥,可能并发写同一个 slot(后写覆盖)。旧版本二进制无法识别新的 key 格式,因此混跑窗口期唯一安全的做法是让两个版本不同时服务同一个 `{agentId}/{userId}`:**停止把受影响的流量路由到旧副本,待其在途调用排空后,再让新副本接管这些用户**。如果你的部署把混跑滚动升级视为受支持的模式,请将此列为发布门槛。 + **推荐方式**:使用 `distributedStore(...)`,快照和执行锁都会自动注入: ```java diff --git a/docs/v2/zh/docs/harness/subagent.md b/docs/v2/zh/docs/harness/subagent.md index 9bb3417194..6b6e101f33 100644 --- a/docs/v2/zh/docs/harness/subagent.md +++ b/docs/v2/zh/docs/harness/subagent.md @@ -171,6 +171,8 @@ HarnessAgent.builder() - **ISOLATED**(默认):子 agent 有自己独立的工作区(如果声明里 `workspace.path` 没写,框架会自动开一个子目录)。子 agent 的运行时状态按"父 sessionId × 用户"分桶——同一用户在不同对话里 spawn 同名子 agent 也互不污染。 - **SHARED**:子 agent 直接用主工作区。适合子 agent 的输出会被父立即读到的情况(例如 `general-purpose`)。 +**沙箱模式**:父 agent 使用沙箱时,ISOLATED 声明式子 agent 会安装**自己的**沙箱上下文与工作区投影——不再继承父的(调用方通过 `externalSandbox` / `externalSandboxState` 显式共享的方式仍然有效),因此子 agent 看到的是自己的投影而非父的。`general-purpose` 是例外——它共享父沙箱。`USER` 作用域下执行锁按 `{agentId}/{userId}` 加锁,父子不会互相阻塞;`GLOBAL` 作用域不能与子 agent 组合——构建期会直接报错拒绝,否则 call 内 spawn 的子 agent 会阻塞在父已持有的租约上。 + ## 同步还是后台? 主 agent 通过 `agent_spawn` 创建子 agent,关键是 `timeout_seconds`: