Skip to content

[Bug]: ReActAgent slot key collides when userId contains '/' #3337

Closed

Description

@quhengshuo

Describe the bug

ReActAgent.slotKey(userId, sessionId) joins the two with /, so ("u1/a", "b") and ("u1", "a/b") both map to u1/a/b. The two identities then share the cached state, CAS versions and permission engine, the same-session call gate, and interrupt routing: getAgentState("u1", "a/b") can return the other identity's cached state, interrupt("u1", "a/b") can stop the other identity's call, and without an AgentStateStore both share one conversation.

It needs a userId containing / plus a matching second identity, so it is much rarer than #2475. Found while reviewing #3335.

To Reproduce

ReActAgent agent = ReActAgent.builder().name("asst").sysPrompt("hi").model(model).build();
AgentState a = agent.getAgentState("u1/a", "b");
AgentState b = agent.getAgentState("u1", "a/b");
// a == b: two different (userId, sessionId) pairs get the same cached AgentState

Expected behavior

Different (userId, sessionId) pairs never share a slot.

Environment

  • AgentScope-Java Version: main @ 3a6baf6
  • Java Version: 17
  • OS: any

Additional context

Possible fix: key the maps by a (userId, sessionId) record instead of a joined string, or escape the user part. Tests that read slotVersions reflectively with string keys (e.g. "u1/sessA") would need updating.

Activity

  1. oss-maintainer commented on Sep 28, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed (verified against main @ 3a6baf6c)

    The collision is real and the reported consequences hold up. ReActAgent.slotKey builds every per-slot map key as a plain string join:

    // agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java:397-400
    private static String slotKey(String userId, String sessionId) {
        Objects.requireNonNull(sessionId, "sessionId must not be null");
        return (userId == null || userId.isBlank() ? "__anon__" : userId) + "/" + sessionId;
    }

    slotKey("u1/a", "b") == slotKey("u1", "a/b") == "u1/a/b", and the mapping is not injective. Nothing validates the id characters: RuntimeContext.Builder accepts arbitrary userId/sessionId, and JdbcAgentStateStore (lines 569-574) explicitly documents slash-containing slot ids as legitimate input — so this shape is reachable by design, not only by misuse.

    Confirmed consequences (each structure is keyed by the same ambiguous string):

    1. Shared cached state — stateCache (ReActAgent.java:286) via getAgentState(String, String) → computeIfAbsent (:4522-4538); activateSlotForContext also does stateCache.put(slot, loaded) (:681), so two identities overwrite each other's loaded state and out-of-call accessors return whichever loaded last.
    2. Shared CAS versions + permission engine + call gate — slotVersions (:293, read as the CAS expected in saveAgentState :4770-4775) and permissionEngineCache (:302, populated :700-704 / :4715-4716) both collide, so ASK rules granted to one identity apply to the other; callSerializationKey (:727-737) routes both onto one FIFO gate in AgentBase.callGates (AgentBase.java:108, 374-397).
    3. Cross-identity interrupt — interrupt(userId, sessionId, msg) (:963-964) → interruptRunning(slotKey(...)) → runningCalls.get(key) (AgentBase.java:212-220, registration at :329), so interrupt("u1", "a/b") can cancel the in-flight call of ("u1/a", "b").
    4. No-store mode shares one conversation — activateSlotForContext (:683-696) returns a single AgentState (one history) per colliding key.

    Additional consequence not in the report: cross-identity persistence corruption. saveStateToSession (:475-496) re-derives the identity from the joined key through SlotRef.parse (:402-410, splits on lastIndexOf('/')) and persistAgentStateCas (:535-573) writes with those parsed values, while loads use the true identity (:671-678). A call for ("u1", "a/b") therefore persists into the store slot of ("u1/a", "b"). This is silent cross-tenant data corruption on every call-path save (:1241, :1348, :1414, :4439) — the same failure mode as #2475, reachable via the userId side instead of the sessionId side.

    Severity: high — cross-tenant conversation sharing, permission-rule leakage, cross-identity interrupt, and wrong-slot persistence. Not critical because it needs a /-containing userId (or a user literally named __anon__) with both colliding identities served by the same agent instance.

    Coordination note: PR #3335 (open, ReActAgent.java + AgentStateStore.java + ReActAgentPerSessionStateTest.java, "persist agent state under the caller's identity", fixes #2475) already rewrites the persist path where consequence 5 lives; the earlier attempt PR #2476 was closed unmerged. Please don't land an independent fix for the persistence half here — it should build on #3335.

    Suggested direction

    • Key the per-slot structures structurally (e.g. a record SlotId(String userId, String sessionId)) instead of a joined string; ThreadSessionManager.SessionKey (agui starter, :307 / :359) already shows this pattern, as does InMemoryAgentStateStore (two-level map, :192-198) and JsonFileAgentStateStore (escaped path segments, :287-288 / :398-405). If a string key must stay, make it unambiguous (length-prefix the user segment, or escape / and the __anon__ sentinel in userId), and drop the SlotRef.parse round-trip by carrying userId/sessionId explicitly instead of re-deriving them from the key (CallExecution has no identity fields today; that is the approach PR fix(#2475): store userId/sessionId directly on CallExecution instead of parsing slotKey #2476 proposed and fix(core): persist agent state under the caller's identity #3335 is taking).
    • Tests that read slotVersions reflectively with string keys need updating: ReActAgentPerSessionStateTest.java:97-112 and the literal "u1/sessA" keys at :662 / :667.

    Open question for maintainers (blocking any PR here): the same joined-key pattern is repeated in the store layer — RedisAgentStateStore.java:450, JedisAgentStateStore.java:332, RedissonAgentStateStore.java:320 (user + "/" + session) and JdbcAgentStateStore.java:562, PostgresAgentStateStore.java:710, MysqlAgentStateStore.java:902 (':' separator), CosAgentStateStore.java:256, OssAgentStateStore.java:259 (object paths). Changing those key formats breaks already-persisted data. Should the fix be scoped to agentscope-core in-memory slots only (no migration), or extended to the store layer with a documented migration/compat path? We are holding off on generating a fix PR until that scope decision and #3335's outcome are known.


    Automated evaluation by github-manager-bot

  2. added
    area/core/agent
    area/core/memory
    bug
    on Sep 28, 2026
  3. aiyili commented on Oct 21, 2026

    @aiyili
    Contributor

    Hi, I'd like to work on this. Plan: stop keying by the joined string — key the agent-state cache, CAS versions, call gate and interrupt routing by a (userId, sessionId) record (or an escape that cannot collide, e.g. length-prefixed), and keep the persisted slot key format backward-compatible for existing stores. Tests asserting ("u1/a","b") != ("u1","a/b") across all the affected surfaces. PR coming shortly.

  4. juruoO-o commented on Nov 12, 2026

    @juruoO-o
    Collaborator

    Closing as fixed by #3335 (merged): agent state is persisted under the caller's identity, handling / in userId/sessionId keys. The sibling report #2475 (slot key with / in sessionId) was closed by the same PR. Reopen if collisions persist.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/core/agentarea/core/memorybug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions