Skip to content

fix(harness): make the gateway session key injective - #3592

Closed
King52HerTz wants to merge 1 commit into
agentscope-ai:mainfrom
King52HerTz:fix/msg-context-canonicalkey-injective
Closed

King52HerTz wants to merge 1 commit into
agentscope-ai:mainfrom
King52HerTz:fix/msg-context-canonicalkey-injective

Conversation

@King52HerTz

Copy link
Copy Markdown
Contributor

Problem

Closes #3534.

MsgContext.canonicalKey() joins the routing components with a bare | and does not escape that separator inside a value, so the key is not injective: one component can imitate another label.

new MsgContext("chatui", "team|r:room-1", null, null, null, Map.of()).canonicalKey();
new MsgContext("chatui", "team", "room-1", null, null, Map.of()).canonicalKey();
// both -> "chatui|g:team|r:room-1"

The key is load-bearing, not a display string: HarnessGateway derives the session id from it (SessionIdUtils.deterministicHash, a 12-char hex), keys the per-session turn gate with it, and keeps a session-id -> key reverse map for wake-up driven runs. A collision therefore routes two genuinely different conversations onto one gateway session - one group/room's participants end up in the other's conversation state and turn serialisation.

Two more cases in the same family that the report doesn't mention:

  • The extra map is joined the same way, so one entry's value can spell out a whole second entry: Map.of("a", "b|x:c=d") and Map.of("a", "b", "c", "d") produce the identical key.
  • The channel component is concatenated raw too, so a channel named chatui|g:team collides with channel chatui plus group team.

Change

Escape % and | inside every component - channel, group, room, threadId, threadTs, and both halves of each extra entry - so | occurs in the key only as a separator and % only as an escape introducer. % is escaped before |; the opposite order would re-escape the % it just wrote and make a literal %7C value collide with a literal | one.

Compatibility - the one thing to review

The escape is conditional: a component containing neither character is returned unchanged. Ordinary channel/group/room/thread ids contain neither, so existing keys, and therefore the session ids hashed from them, stay byte-identical and nothing needs migrating. The only conversations whose key changes are those whose ids literally contain % or | - exactly the ones that could previously be collided with another conversation, and they will start a fresh session rather than continuing in a shared one. There is a test pinning the unchanged shape of an ordinary key.

Deliberately not included

  • The existing normalisation stays as-is: a blank component is skipped and components are trim()ed, so group = " " is still equivalent to no group. That collapses to the same conversation by design; it is not a way to imitate a second component, so I left it alone rather than widening the change.
  • userId still does not participate in the key, as the class documents.
  • No parser is added for the key. It stays opaque to callers; the fix is only that it can no longer be ambiguous at construction.

Testing

6 new tests in MsgContextCanonicalKeyTest: the reported collision, the channel variant, the extra-value-forges-an-entry variant, the %7C vs | ordering case, an exact-key regression guard for ordinary ids, and stability of the derived session id.

mvn -B -T1 test -pl :agentscope-harness -am -Dtest=MsgContextCanonicalKeyTest   # 6 tests, BUILD SUCCESS
mvn -B -T1 test -pl :agentscope-harness -am   # BUILD SUCCESS: 8858 tests, 0 failures, 0 errors, 54 skipped (core + harness, all their suites)

Mutation matrix - each breaks the product code only:

What was broken Result
All 7 escape call sites removed (main's behaviour) the 4 forge/collision tests red; the exact-key regression test stayed green, as it must
Escape order flipped (` before%`)
Escape removed for extra keys/values only only the extra-entry test red

MsgContext.canonicalKey() joined its routing components with a bare '|' and
never escaped that separator inside a value, so a single component could
imitate another label: group "team|r:room-1" produced exactly the key of group
"team" plus room "room-1". The key is what the gateway derives the session id,
the per-key turn gate and the session-to-key wake-up map from, so a collision
routes two distinct conversations onto one gateway session.

The same hole exists on the extra map, where one entry's value can spell out a
whole second "|x:" entry, and on the channel component.

Escape '%' before '|' inside every component ('%' first, so the escape itself
stays unambiguous). Components containing neither character - the common case -
are returned unchanged, so existing session keys and their derived ids do not
move.
@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@King52HerTz

Copy link
Copy Markdown
Contributor Author

Closing this PR to avoid maintaining overlapping fixes for the same routing-key collision issue. The earlier PR #3535 addresses this problem; please continue the discussion there. Thanks.

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

Labels

P1 紧急不重要

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: MsgContext.canonicalKey() is not injective — distinct conversations can collapse to one gateway session key

2 participants