Skip to content

fix(harness): make MsgContext.canonicalKey injective for routing - #3535

Open
hermanrous wants to merge 2 commits into
agentscope-ai:mainfrom
hermanrous:fix/msgcontext-canonical-key-collision
Open

hermanrous wants to merge 2 commits into
agentscope-ai:mainfrom
hermanrous:fix/msgcontext-canonical-key-collision

Conversation

@hermanrous

@hermanrous hermanrous commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem

MsgContext#canonicalKey() builds the gateway routing key by joining the components with a bare | and never escaping that separator inside a component, so the key is not injective:

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

extra keys and values are joined the same way and can forge structural fragments too.

Impact

The canonical key is the gateway session key — it derives the session id, keys the per-session turn gate and the session -> key wake-up map. A collision routes two different conversations to the same gateway session, so one group/room can see another conversation's state.

Fix

Escape % (first) and | in each component before appending. A component without reserved characters keeps its old key, so existing session mappings are unaffected; HarnessGatewaySessionIdTest and SessionIdUtilsTest still pass.

Tests

New MsgContextCanonicalKeyTest fails on main (expected: not equal but was: <chatui|g:team|r:room-1>) and passes with the fix. The gateway test set (10 classes, 40 tests, including HarnessGatewaySessionIdTest) is green; spotless:check passes.

Compatibility

Components without reserved characters keep exactly the same key, so existing session mappings are unaffected. A component containing % or |, or an extra key containing =, moves to a new key (and a new gateway session id) after this change — that is inherent to making the key injective.

Fixes #3534

canonicalKey() joined the routing components with an unescaped separator, so
two different contexts could produce the same session key and share one gateway
session. Escaping % and | keeps keys without reserved characters unchanged.

Refs agentscope-ai#3534
Copilot AI balanced review requested due to automatic review settings October 13, 2026 16:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...o/agentscope/harness/agent/gateway/MsgContext.java 75.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@guslegend0510

Copy link
Copy Markdown
Contributor

Thanks — the %/| escaping looks right. I checked that the new test fails on main and passes here, and that keys without reserved characters are byte-identical to before.

Two small suggestions:

  1. extra keys can still collide through =: Map.of("a", "b=c") and Map.of("a=b", "c") both produce c|x:a=b=c. Escaping = in the extra key only (e.g. escape(e.getKey()).replace("=", "%3D")) closes this without changing keys whose values contain =.
  2. Since compatibility is the main risk here, could you pin it with a golden-string assertion, e.g. assertEquals("chatui|g:team|r:room-1|t:t1|ts:ts1|x:agentId=main", ctx.canonicalKey())? HarnessGatewaySessionIdTest only checks determinism and format, so it wouldn't catch a format change. A case for extra forging would be good too.

Minor: components containing % will get a new key (and a new gateway session id) after upgrade. That's unavoidable for injectivity, but maybe worth a line in the description.

Addresses review feedback on agentscope-ai#3535.

An extra key containing '=' could still forge a collision: {"a": "b=c"} and
{"a=b": "c"} both produced "c|x:a=b=c". Extra keys are now escaped for '='
as well, while values stay untouched.

The canonical key format is also pinned with a golden assertion because the
key derives persisted gateway session ids, plus a case covering every escape.
@hermanrous

Copy link
Copy Markdown
Contributor Author

Both points are implemented in e72a07b.

  1. I reproduced the = forging before touching anything: {"a": "b=c"} and {"a=b": "c"} both produced c|x:a=b=c. Extra keys now go through escapeKey() (% → %25, | → %7C, = → %3D), so those two produce c|x:a=b=c and c|x:a%3Db=c. Values are untouched, so {"a": "b=c"} keeps its previous key.
  2. Added the golden assertion assertEquals("chatui|g:team|r:room-1|t:t1|ts:ts1|x:agentId=main", …) and a second case pinning every escape: a%25b|g:g%7C1|r:r=2|t:t%253|ts:ts%7C4|x:k%3Dx=v%7Cy.

The description now notes the compatibility effect: a component containing % or |, or an extra key containing =, moves to a new key (and a new gateway session id) after upgrade; keys without reserved characters are unchanged.

Local checks: MsgContextCanonicalKeyTest 4/4; the gateway test set 43/43 (was 40); mvn -T1 -pl agentscope-harness -am spotless:check re-ran without its cache (462 files checked) and passed.

@juruoO-o

Copy link
Copy Markdown
Collaborator

[P1] The new encoding can reuse a different conversation's pre-upgrade session key.

The escaping fixes collisions within the new format, but old and new key spaces overlap. For example, with channel chatui:

  • Before upgrade, group team%7Croom produces chatui|g:team%7Croom.
  • After upgrade, group team|room also produces chatui|g:team%7Croom.

HarnessGateway.generateSessionId() deterministically hashes this key, so after an upgrade the second conversation can resolve to the first conversation's persisted session/history in the same user namespace. This is more than starting a fresh session for reserved characters; it can cross conversation boundaries.

Please isolate the new encoding from persisted legacy keys (or provide a migration that establishes ownership) and add a regression that creates the first conversation under the legacy encoding, upgrades, then routes the second conversation. Document the migration behavior alongside the unchanged ordinary-character keys.

Reviewed head: e72a07bb61af1f9f73d25531f897d6820c227581. Static source review; local Java/Maven tests were not run.

This branch has not been deployed

No deployments
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

5 participants