Skip to content

fix(#2475): store userId/sessionId directly on CallExecution instead of parsing slotKey - #2476

Closed
waterWang wants to merge 1 commit into
agentscope-ai:mainfrom
waterWang:fix-2475-slotkey-slash-parsing
Closed

waterWang wants to merge 1 commit into
agentscope-ai:mainfrom
waterWang:fix-2475-slotkey-slash-parsing

Conversation

@waterWang

Copy link
Copy Markdown

Fixes #2475

Problem

When RuntimeContext.sessionId contains a / character (e.g., DingTalk openConversationId values like cidLcMOzfRs62haqnq1NO8SxeYdmc/lO8bWyidOisoKAzM=), ReActAgent persists the AgentState under a corrupted (userId, sessionId) pair because SlotRef.parse() splits the concatenated slot key at the last /.

Root Cause

SlotRef.parse() uses lastIndexOf('/') to split the concatenated key, but this is ambiguous when sessionId itself contains /:

slotKey = "u1/cidLcMOzfRs62haqnq1NO8SxeYdmc/lO8bWyidOisoKAzM="
// parsed as: userId="u1/cidLcMOzfRs62haqnq1NO8SxeYdmc", sessionId="lO8bWyidOisoKAzM="
// CORRECT: userId="u1", sessionId="cidLcMOzfRs62haqnq1NO8SxeYdmc/lO8bWyidOisoKAzM="

Fix

  1. Store userId and sessionId directly on CallExecution — the original values are now carried through the per-call scope instead of being reconstructed from the concatenated slot key.
  2. Update saveStateToSession() to use scope.userId / scope.sessionId directly.
  3. Remove SlotRef.parse() — no longer needed.

Impact

  • Conversation history replay via getAgentState(userId, sessionId) now works correctly for any session whose id contains /.
  • State-store rows no longer accumulate under corrupted user_id values.
  • Per-user queries like listSessionIds(userId) return correct results.

Testing

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

@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@oss-maintainer oss-maintainer 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.

Review — Approved ✅

Clean, minimal fix for the SlotRef.parse() ambiguity when sessionId contains /.

What changed

  • userId and sessionId are now stored directly on CallExecution as separate fields
  • saveStateToSession() uses scope.userId / scope.sessionId directly instead of round-tripping through the concatenated slot key
  • SlotRef.parse() is removed — no longer needed
  • Constructor updated to accept the original identity values

Assessment

  • Correctness: Eliminates the root cause entirely. No more ambiguity from lastIndexOf('/') splitting.
  • Scope: 12 additions / 13 deletions in a single file — well-contained.
  • Backward compatibility: The slotKey field is preserved on CallExecution (still used as map key), so no downstream breakage.
  • Suggestion: Add a regression test with a sessionId containing / (e.g. "cidLcMOzfRs62haqnq1NO8SxeYdmc/lO8bWyidOisoKAzM=") to verify state is stored and retrievable under the correct (userId, sessionId) pair.

@oss-maintainer

This comment was marked as abuse.

@oss-maintainer

This comment was marked as abuse.

@gxgeek-n

Copy link
Copy Markdown
Contributor

@oss-maintainer noted on #2475 that "a regression test covering sessionId with / would prevent this from recurring" — I wrote one on top of this PR and verified it in both directions.

Commit: gxgeek-n@c664ed09 (branch test/2475-slotkey-sessionid-slash, based on this PR's head 0bdb6bb9)

AgentStateSlotKeySessionIdSlashTest — end-to-end through a real ReActAgent call against InMemoryAgentStateStore, 6 cases:

case asserts
sessionIdWithSlash_statePersistedUnderOriginalSlot state lands under the original (userId, sessionId)
sessionIdWithSlash_noStateUnderCorruptedSlot nothing leaks into the pair the old SlotRef.parse() derived
sessionIdWithSlash_stateReplayedAcrossCalls state survives a second call in the same session
anonymousUserSessionIdWithSlash_... __anon__ prefix path (also corrupted before)
sessionIdWithMultipleSlashes_... a/b/c/d
sessionIdWithoutSlash_unchangedBehavior plain sessionId still works — guards against over-correction

Verified both directions:

The only case green on both is the plain-sessionId one, which the bug never touched — so the suite genuinely detects the regression rather than passing vacuously.

The fixture uses a real DingTalk-style openConversationId (cidLcMOzfRs62haqnq1NO8SxeYdmc/lO8bWyidOisoKAzM=) and hard-codes the expected corrupted pair, so slot-derivation regressions fail loudly instead of silently.

Feel free to cherry-pick c664ed09 straight into this PR — that keeps fix + test together in one changeset, which seems cleaner than a separate PR that would depend on this one. Happy to open a standalone PR instead if you'd rather keep them split; also glad to adjust naming or placement (currently agentscope-core/src/test/java/io/agentscope/core/state/).

spotless:apply已跑过,格式合规。

@oss-maintainer

This comment was marked as abuse.

@zouyx zouyx closed this Aug 7, 2026
juruoO-o added a commit that referenced this pull request Nov 11, 2026
## AgentScope-Java Version

2.0.4-SNAPSHOT (main @ 9c8b10d)

## Description

Fixes #2475.

**Problem.** With an `AgentStateStore` configured, `saveStateToSession`
recovered `(userId, sessionId)` by splitting the slot key `(userId or
"__anon__") + "/" + sessionId` at its last `/`, while loads used the
caller's real pair. For a sessionId containing `/` (e.g. a Base64
DingTalk `openConversationId`), saves and loads therefore addressed
different pairs:
- A trailing `/` split into a blank sessionId, so the save failed
(`sessionId must not be blank`).
- On stores that key user and session separately (InMemory, JsonFile,
JDBC/MySQL/PostgreSQL, Mongo), each call started from an empty history,
`getAgentState(uid, sid)` returned a fresh state, and state was written
under made-up user ids such as `u1/cid...`, which also polluted per-user
queries like `listSessionIds`. On versioning stores, every save from the
second turn on also hit a spurious CAS conflict (the default OVERWRITE
logs a warning; `FAIL` throws).
- Redis/OSS/COS join user and session with `/`, so both pairs mapped to
the same storage key and the history loss was masked there.

**Fix.** `CallExecution` now carries the resolved `(userId, sessionId)`
as final fields and derives `slotKey` from them. `saveStateToSession`
passes that pair to the store. Every per-call save goes through this one
method: normal completion, both structured-output paths, the failure
path, user interrupt, and `streamEvents`. `SlotRef` and the unused 3-arg
constructor are removed. The slot-key format is unchanged; it stays an
opaque cache/serialization key.

Identities without `/` still land on the same rows: saves now pass the
caller's raw userId, so a blank or literal `"__anon__"` userId is no
longer rewritten to `null`, and every in-repo store maps null, blank and
`"__anon__"` to the same bucket.

**Tests** (`ReActAgentPerSessionStateTest`):
- Parameterized over `("u1", base64 id)`, `("u1", "tenant/chat/42")`,
`("u1", "conv/")`, `(null, base64 id)`: state is readable under the
caller's pair, and `agent_state` is written under exactly that pair.
- Two turns on one session: no CAS conflict, and a rebuilt agent replays
both turns.

Without the fix, the new cases fail (4 failures, 1 error).

**How to test:** `mvn -pl agentscope-core verify`

**Out of scope (happy to follow up separately):**
- The slot key is still not injective: `("u1/a", "b")` and `("u1",
"a/b")` share a key. This needs a userId containing `/`.
- Rows written under corrupted pairs before this fix stay orphaned on
the stores that key user and session separately (plus the JPA store in
agentscope-service). This may be worth a release note.

Credit: same approach as #2476 by @waterWang; the regression-test idea
comes from @gxgeek-n.

## Checklist

- [x] Code has been formatted with `mvn spotless:apply`
- [x] All tests are passing: agentscope-core `verify` (2491 tests) and
agentscope-harness (1057 tests) locally; full repo left to CI
- [x] Javadoc comments are complete and follow project conventions
- [ ] Related documentation has been updated (N/A, no public API change)
- [x] Code is ready for review

---------

Co-authored-by: ”zhuzhiyu.zzy“ <[email protected]>
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.

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

4 participants