Skip to content

fix(sandbox): scope the execution guard key by agent for USER isolation (backport to 2.0.x) - #4520

Merged
juruoO-o merged 10 commits into
agentscope-ai:2.0.xfrom
Ernest-L:backport/sandbox-guard-key-2.0.x
Dec 2, 2026
Merged

juruoO-o merged 10 commits into
agentscope-ai:2.0.xfrom
Ernest-L:backport/sandbox-guard-key-2.0.x

Conversation

@Ernest-L

Copy link
Copy Markdown
Contributor

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, default ISOLATED) self-deadlocks on SandboxExecutionGuard because the guard key drops agentId while the persisted slot keeps it. The guard now keys on {agentId}/{userId} for USER scope; SandboxManager's constructor validates the wiring (blank/mismatched agentId fails at build time); forExecutionGuard throws 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: SandboxLifecycleMiddlewareProjectionNarrowingTest does 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 verify on this branch with the CI Windows flags (-Djacoco.skip=true -DskipDurableWindowsTests=true, javadoc/source skipped as in PR builds):

Compatibility

  • Additive public API only (SandboxIsolationKey#forExecutionGuard, SessionSandboxStateStore#getAgentId); SandboxManager constructor 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

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn clean verify — full reactor)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (sandbox.md / subagent.md, en + zh)
  • Code is ready for review

Ernest-L and others added 6 commits December 1, 2026 01:11
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

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@juruoO-o

juruoO-o commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

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 main: if 2.0.x is intentionally shipping first, please track the upstream merge so the next mainline release does not reintroduce the deadlock.

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.
Ernest-L added a commit to Ernest-L/agentscope-java that referenced this pull request Dec 1, 2026
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).
@Ernest-L

Ernest-L commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

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. sandbox.md (en + zh) now says this explicitly: stop routing affected traffic to old replicas, drain their in-flight calls, and only then let new replicas take over those users — and treat that as a release gate if mixed-version rolling upgrades are a supported mode. Applied on this PR (3c37c9e) and on #4441 (d16062e) so mainline and 2.0.x carry the same wording.

Upstream tracking — #4441 remains open on main and will be tracked until merged, so the next mainline release carries the fix; the two PRs cross-reference each other.

Ernest-L and others added 3 commits December 1, 2026 22:34
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 juruoO-o left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@juruoO-o
juruoO-o merged commit b602f0c into agentscope-ai:2.0.x Dec 2, 2026
7 checks passed
@Ernest-L

Ernest-L commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Opened the companion port of jujn's 3181063 to 2.0.x as #4581 (the commit had not reached 2.0.x when #4520 was cut). #4581 keeps this PR's rollout guidance and rewrites the stale GLOBAL sentences to the new build-time rejection, so the two lines stay in lockstep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants