Repository navigation
fix(sandbox): scope the execution guard key by agent for USER isolation (backport to 2.0.x) - #4520
Conversation
A declared subagent spawned inside a sandboxed call acquires its own sandbox,
but SandboxIsolationKey#resolve drops agentId for USER scope: the guard key is
just userId while the persisted slot is sandbox/user/{agentId}/{userId}. The
parent holds the guard for the whole call window, so the nested subagent waits
on the same key forever.
Derive a guard-only key that mirrors the slot granularity:
- USER scope now locks on {agentId}/{userId};
- SESSION/AGENT already discriminate parent from child;
- GLOBAL shares one slot by definition and is documented as unsupported with
subagents.
The persisted slot key is unchanged, so existing sandbox state and snapshots
are untouched. Lock names change for USER scope (ephemeral leases; rolling
upgrade sees a one-lease-TTL window).
Tests: SandboxIsolationKeyTest (agent dimension, stability, non-USER scopes)
and SandboxManagerIsolationTest (nested parent/child keys differ while the
state-store key stays identical).
…uard lockstep - forExecutionGuard throws IllegalArgumentException for USER scope with a null/blank agentId instead of returning the deadlock-prone slot key - SessionSandboxStateStoreTest pins the slot id to end with the guard key's value, catching drift between the two compositions - docs: the mixed-version window is the whole rollout, not one lease TTL; GLOBAL subagent behaviour described accurately (child blocks on the parent's lease)
- SandboxManager constructor rejects a null/blank agentId and asserts it matches the state store's agent id, so guard keys and persisted slots cannot silently drift apart; SessionSandboxStateStore exposes getAgentId() for the check - drift test extended to SESSION and AGENT scopes (AGENT has the same two-source coupling between store id and resolve() id); the nested parent/child test now wires each manager to its own agent-scoped store, mirroring HarnessAgent.build - forExecutionGuard keeps its USER-scope blank check as a guard for direct callers; javadoc notes the constructor already covers SandboxManager users Full harness suite 2284/0 plus extension guard suites (PostgreSQL 20, MongoDB 24, JDBC 6).
…-USER null path Per review follow-up (all three optional items): - the guard-key/slot consistency check no longer tolerates a null store id: getAgentId() returns a final field the store's constructor requires non-null, so the comparison holds unconditionally; test mocks are stubbed accordingly (SandboxManagerIsolationTest setUp + middleware lifecycle tests) - sandbox.md (en + zh) states the rolling-upgrade consequence of the USER lock-name change: peers of different versions do not serialise and can write the same slot concurrently until the rollout completes - SandboxIsolationKeyTest pins that forExecutionGuard(null/blank) on non-USER scopes returns this and never throws — the path a refactor hoisting the check above the scope switch would break Full harness suite 2284/0.
The e2b and kubernetes sandbox modules construct SandboxManager with unstubbed SessionSandboxStateStore mocks, whose getAgentId() returns null — the constructor's consistency check now rejects that, breaking both CI platforms. Stub each mock with the id its manager is built with, matching real wiring. Verified with a full-reactor 'mvn clean verify' (114 modules, 13531 tests, 0 failures) using the CI Windows flags; durable-sandbox stays covered by the ubuntu job as before.
…mocks Two construction sites exist only on 2.0.x (not present in the cherry-picked commits' context): AgentRunSandboxWorkspaceRootTest and SessionTreeMirrorIsolationTest build SandboxManager with unstubbed SessionSandboxStateStore mocks. Stub getAgentId() to match the manager's id, mirroring real wiring. Full-reactor 'mvn clean verify' (114 modules, 11871 tests, 0 failures) with the CI Windows flags on top of the backport series.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
The fix looks right. One release concern: “complete the rollout” does not prevent old and new replicas from writing the same USER slot during the mixed-version window, because they no longer share a lock. Could the upgrade docs/release notes explicitly say to stop routing affected traffic to old replicas and drain their in-flight work before allowing new replicas to take over (or otherwise guarantee that the two versions cannot write the same slot)? If mixed-version rolling upgrades are supported, please treat that as a release gate. Also, #4441 is still open on |
Per review: 'complete the rollout' does not prevent old and new replicas from writing the same USER slot concurrently — they no longer share a lock, and old binaries cannot be taught the new key format. The only safe mixed-version mode is exclusion by routing: stop routing affected traffic to old replicas, drain their in-flight calls, and only then let new replicas take over those users. State this explicitly; treat it as a release gate when mixed-version rolling upgrades are a supported mode.
Per review on agentscope-ai#4520: 'complete the rollout' does not prevent old and new replicas from writing the same USER slot concurrently — they no longer share a lock, and old binaries cannot be taught the new key format. State the exclusion-by-routing requirement explicitly (stop routing affected traffic to old replicas, drain in-flight calls, then let new replicas take over) and treat it as a release gate when mixed-version rolling upgrades are a supported mode. Kept in lockstep with the 2.0.x backport (agentscope-ai#4520).
|
Both points taken. Upgrade guidance — agreed that "complete the rollout" understates the risk: the lock names never meet, so nothing serialises the two versions and a concurrent same-slot write is possible throughout the window. Old binaries can't be taught the new key, so the only safe mixed-version mode is exclusion by routing. Upstream tracking — #4441 remains open on |
Codecov flagged SandboxManager line 89 as partially covered: the blank agentId path was tested but the null path (a distinct short-circuit branch of the same check) was not. Add it.
juruoO-o
left a comment
There was a problem hiding this comment.
The 2.0.x English and Chinese upgrade guidance explicitly requires removing affected traffic from old replicas and draining their in-flight calls before transferring ownership. The guard key matches the USER slot and the regression tests cover nested agents and manager/store ID agreement. Ubuntu and Windows CI passed. One mainline follow-up: #4441 has now merged, and its merged sandbox docs at 09126ec do not contain this rollout instruction. Please carry the guidance into the mainline docs too.
AgentScope-Java Version
2.0.x(2.0.6-SNAPSHOT at bab86b1). Backport of #4441 for the 2.0.x line — ships with 2.0.6.Description
Backport of #4441 (fix(sandbox): scope the execution guard key by agent for USER isolation). A sandboxed agent that spawns a declared subagent (
subagents/*.md, defaultISOLATED) self-deadlocks onSandboxExecutionGuardbecause the guard key dropsagentIdwhile the persisted slot keeps it. The guard now keys on{agentId}/{userId}forUSERscope;SandboxManager's constructor validates the wiring (blank/mismatched agentId fails at build time);forExecutionGuardthrows for USER scope with a null/blank agentId instead of returning the deadlock-prone slot key.Cherry-picked from #4441 (commits ee50e85 → 8e06e76). One conflict resolved:
SandboxLifecycleMiddlewareProjectionNarrowingTestdoes not exist on 2.0.x — its hunk (a test-mock stub) was dropped with the file. Two 2.0.x-specific construction sites were additionally stubbed, since the constructor check is repo-wide:AgentRunSandboxWorkspaceRootTest(agentrun module)SessionTreeMirrorIsolationTest(2.0.x-only session-mirror test)Tests
Full-reactor
mvn clean verifyon this branch with the CI Windows flags (-Djacoco.skip=true -DskipDurableWindowsTests=true, javadoc/source skipped as in PR builds):durable-sandboxremains covered by the ubuntu job, as before ([harness] 沙箱模式下会话镜像按 agent 级兜底字段钉沙箱,同一 agent 多用户并发时会话日志可能写入他人沙箱 #4273-era layout)Compatibility
SandboxIsolationKey#forExecutionGuard,SessionSandboxStateStore#getAgentId);SandboxManagerconstructor now rejects blank/mismatched agentIds (framework wiring always passes a consistent non-blank id).USER-scope lock names change (...:user:<uid>→...:user:<agent>/<uid>); during a rolling upgrade old and new nodes do not serialise and can write the same slot concurrently — complete the rollout promptly. Ephemeral leases; no data migration.Checklist
mvn spotless:applymvn clean verify— full reactor)sandbox.md/subagent.md, en + zh)