Repository navigation
fix(a2a): resolve runner session IDs while preserving legacy metadata - #3746
Merged
Merged
Conversation
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
jujn
approved these changes
Oct 25, 2026
1 task done
jujn
pushed a commit
that referenced
this pull request
Nov 2, 2026
Fixes #1603 ## Description `A2aAgent#call(msgs, RuntimeContext)` already resolves a first-class `Session(userId, sessionId)`, but that identity was **local-only**: it was dropped at the serialization boundary, and the server read user/session ids from flat message metadata keys the client never writes flat (`convertFromMsg` wraps per-message metadata as `<msgId, metadata>`). As a result `AgentRequestOptions.userId` was always empty and the built-in runner never forwarded identity into the agent — #1603. This closes the three gaps: | # | Side | Change | |---|------|--------| | 1 | client | `MessageConvertUtil.convertFromMsg(msgs, userId, sessionId)` (old overload delegates): user id → envelope-level metadata key `_agentscope_user_id`; session id → the native A2A `contextId`. `A2aAgent#exchange` passes the call's session identity. | | 2 | server | `getUserId` reads the envelope key, keeping the flat legacy `userId` key as a deprecated fallback. `getSessionId` is unchanged (#3746 behavior). | | 3 | runner | `BaseReActAgentRunner` builds the per-call `RuntimeContext` from the options and calls `agent.streamEvents(msgs, ctx)`, so per-user state slots, session logs and tools see the caller's `(userId, sessionId)`. | Plus a deployment-side adjudication hook: - **`A2aContextResolver`** (new functional interface, wired via `AgentScopeA2aServer.Builder#contextResolver`): a transport-verified user id (mTLS cert, OAuth token from `RequestContext#getCallContext()`) **overrides** the envelope-asserted one. Priority: `resolver > envelope assertion > none`; disagreement logs a WARN (log values bounded and control-char stripped); a resolver that **throws fails closed** (request rejected, never downgrades to the unverified assertion). A resolver returning `null`/blank deliberately keeps the envelope trust. - Honoring an envelope-asserted identity while no resolver is configured logs a **one-time warning**: the assertion is a delegation claim any caller can make, and it keys per-user agent state, session logs and tool-visible identity. - The envelope key is written **after** the per-message `<msgId, metadata>` wrapping, so a message whose id collides with the reserved key cannot spoof or suppress the identity (regression-tested). ### Usage ```java // client — identity now travels with every call RuntimeContext ctx = RuntimeContext.builder().userId("alice").sessionId("chat-1").build(); remote.call(List.of(msg), ctx).block(); // server — let a verified transport identity win over the client's claim AgentScopeA2aServer.builder() .agentBuilder(agentBuilder) .contextResolver(context -> { String verified = verifiedUserIdOf(context.getCallContext()); return verified == null ? null : RuntimeContext.builder().userId(verified).build(); }) .build(); ``` > Note for #1603 reporters: identity set inside **per-message** `Msg.metadata` was never read by the server and still is not — message metadata is message-scoped. Use the first-class `RuntimeContext` shown above; the docs (EN/ZH) describe the precedence.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AgentScope-Java Version
2.1.0-SNAPSHOT, based on main
07c5f3078.Description
A standard A2A client that sends
contextIdwithout custommessage.metadata["sessionId"]currently gives the server'sAgentRunneran empty session ID. ResolveAgentRequestOptions.sessionIdfrom the SDK-resolved context ID when no non-blank legacy session ID is provided.Explicit legacy metadata keeps precedence, including when the SDK auto-generates a context ID. This preserves existing session keys. Null, empty and whitespace-only legacy values fall back to the protocol context; existing non-string metadata values retain their string conversion. The protocol message's context ID is not rewritten.
This changes request-option mapping only; it does not add automatic session persistence or runtime-context forwarding in the built-in runner. English/Chinese documentation and the field Javadoc describe the precedence.
Refs #1603 and #1827.
Validation
RequestContext.Builder, including generated IDs, conflicting IDs, null/blank metadata, legacy numeric values, and unchanged task/user IDs. Before the fix, 12 cases failed; all 20 pass with it.AgentScopeAgentExecutorTest: 41 tests passed.mvn -B -T1C clean verifypassed for all 99 modules. Test reports: 11,232 tests, 220 skipped, 0 failures/errors.npm test(13 passed),npm run validate, andnpm run broken-linkspassed.git diff --checkpassed.Checklist
mvn spotless:applymvn test)