Skip to content

[Bug]:AgentState is persisted under a corrupted (userId, sessionId) slot when sessionId contains '/' #2475

Closed

Description

@xseruer

Bug Description

When RuntimeContext.sessionId contains a / character, ReActAgent persists the AgentState under a corrupted (userId, sessionId) pair at the end of each call. The state is written to the AgentStateStore with the wrong keys, so any subsequent read with the original (userId, sessionId) — e.g. getAgentState(userId, sessionId) for conversation-history replay — always returns an empty/fresh state.

This is not an exotic input: DingTalk openConversationId values (commonly used as sessionIds in ChatOps scenarios) are standard Base64 and legitimately contain /, e.g. cidLcMOzfRs62haqnq1NO8SxeYdmc/lO8bWyidOisoKAzM=.

Affected Version

  • agentscope-java 2.0.0 (still present on current main)

Root Cause

ReActAgent builds an internal slot key by concatenating the identity with /:

// ReActAgent#slotKey
private static String slotKey(String userId, String sessionId) {
    return (userId == null || userId.isBlank() ? "__anon__" : userId) + "/" + sessionId;
}

At end-of-call persistence (saveStateToSession), the pair is re-derived by splitting the concatenated key at the last /:

// ReActAgent$SlotRef
static SlotRef parse(String slotKey) {
    int slash = slotKey.lastIndexOf('/');   // <-- breaks when sessionId contains '/'
    String u = slotKey.substring(0, slash);
    String s = slotKey.substring(slash + 1);
    return new SlotRef("__anon__".equals(u) ? null : u, s);
}

For userId = "u1", sessionId = "cidLcMOzfRs62haqnq1NO8SxeYdmc/lO8bWyidOisoKAzM=":

  • slot key: u1/cidLcMOzfRs62haqnq1NO8SxeYdmc/lO8bWyidOisoKAzM=
  • parsed back as: userId = "u1/cidLcMOzfRs62haqnq1NO8SxeYdmc", sessionId = "lO8bWyidOisoKAzM="

stateStore.save(...) is then called with these corrupted keys. Note that saveAgentState(userId, sessionId) (the public admin API) passes the identity through directly and is NOT affected — so the same session can end up with two diverging rows in the store (one correct, one corrupted), depending on which code path performed the last write.

Reproduction

InMemoryAgentStateStore store = new InMemoryAgentStateStore();
ReActAgent agent = ReActAgent.builder()
        .name("asst").sysPrompt("hi").model(anyModel).stateStore(store).build();

String sessionId = "cidLcMOzfRs62haqnq1NO8SxeYdmc/lO8bWyidOisoKAzM=";
RuntimeContext ctx = RuntimeContext.builder().userId("u1").sessionId(sessionId).build();
agent.call(List.of(userMsg("hello")), ctx).block();

// EXPECTED: state stored under ("u1", sessionId)
store.get("u1", sessionId, "agent_state", AgentState.class);          // -> empty  (BUG)

// ACTUAL: state stored under a '/'-split pair
store.listSessionIds("u1/cidLcMOzfRs62haqnq1NO8SxeYdmc");             // -> ["lO8bWyidOisoKAzM="]

Impact

  • Conversation history replay via getAgentState(userId, sessionId) silently returns empty for any session whose id contains / — multi-turn context is effectively lost across calls that rely on the persisted state (each call start reloads from the store using the correct keys, so it never sees what the previous call saved).
  • State-store rows accumulate under corrupted user_id values (e.g. u1/cidLcMOzfRs62haqnq1NO8SxeYdmc), which also breaks per-user queries such as listSessionIds(userId).

Suggested Fix

Do not round-trip the identity through the concatenated slot key. Carry the original (userId, sessionId) on the per-call scope (CallExecution, resolved once in activateSlotForContext) and use them directly in saveStateToSession; remove SlotRef.parse. The concatenated slotKey can remain as an opaque cache/serialization key, but it must never be split back.

A regression test suggestion: run a call with a sessionId containing / and assert the state is retrievable from the store under the untouched pair, and that no sibling slot appears under a polluted userId.

Activity

  1. added
    bug
    on Jul 29, 2026
  2. added a commit that references this issue on Jul 29, 2026
  3. oss-maintainer commented on Jul 29, 2026

    @oss-maintainer
  4. added
    area/build
    area/core/agent
    area/core/memory
    on Aug 11, 2026
  5. moved this from Backlog to In progress in AgentScope Javaon Aug 11, 2026
  6. quhengshuo commented on Sep 28, 2026

    @quhengshuo
    Contributor

    Hi, I'd like to pick this up. PR #2476 (same approach) was closed after a Spotless failure and never merged, and the bug is still on main (9c8b10d).
    Plan: carry the resolved (userId, sessionId) on CallExecution and use it in saveStateToSession, remove SlotRef.parse, and keep the slot key as an opaque cache key. Regression tests go into ReActAgentPerSessionStateTest (slash / multi-slash / trailing-slash / anonymous sessionIds, plus multi-turn replay). It already works locally; I can open the PR right away.

  7. quhengshuo commented on Sep 28, 2026

    @quhengshuo
    Contributor

    Opened #3335. The new regression tests in ReActAgentPerSessionStateTest fail on current main (4 failures, 1 error) and pass with the fix.

  8. moved this from In progress to Done in AgentScope Javaon Oct 23, 2026
  9. added a commit that references this issue on Nov 11, 2026
    01eaa24
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/buildarea/core/agentarea/core/memorybug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions