Skip to content

fix(a2a): propagate userId/sessionId end-to-end across the wire - #3938

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
xzxiaoshan:feat/a2a-user-id
Nov 2, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
xzxiaoshan:feat/a2a-user-id

Conversation

@xzxiaoshan

@xzxiaoshan xzxiaoshan commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

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

// 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.

Compatibility

  • Purely additive: convertFromMsg(List) overload, 2-arg executor constructor, AgentRunner interface untouched; getSessionId from fix(a2a): resolve runner session IDs while preserving legacy metadata #3746 kept byte-identical (its tests unchanged and passing).
  • Old client → new server: identical behavior (legacy userId fallback → "").
  • New client → old server / non-AgentScope peer: an unknown metadata key is ignored per protocol; contextId is a native field. Anonymous calls send no envelope key.
  • One observable change: a session-id-less A2aAgent call now carries contextId = <agent name> (the client's default session name) instead of letting the server generate a per-request UUID. Slot semantics are equivalent (ReActAgent's default session also falls back to the agent name); deployments grouping server tasks by contextId will see the constant instead.

Docs

  • docs/v2/en/integration/protocol/a2a.md and docs/v2/zh/integration/protocol/a2a.md: identity propagation on the client, identity/session resolution precedence, resolver usage, and the delegation trust model on the server.

The Session resolved from A2aAgent#call(msgs, RuntimeContext) was
local-only: identity was dropped at the serialization boundary and the
server's flat metadata lookup could never see it (agentscope-ai#1603).

- client: convertFromMsg carries the user id on the envelope-level
  metadata key `_agentscope_user_id` (written after the per-message
  wrapping, safe from <msgId, metadata> key collisions) and the session
  id as the native A2A contextId
- server: getUserId reads the envelope key, with the flat legacy
  `userId` key kept as a deprecated fallback; getSessionId unchanged
- runner: BaseReActAgentRunner forwards (userId, sessionId) into the
  agent's RuntimeContext via streamEvents(msgs, ctx)
- adjudication: new A2aContextResolver lets a deployment override the
  envelope-asserted user id with a transport-verified one
  (resolver > envelope > none; conflicts WARN; a throwing resolver
  fails closed); wired via AgentScopeA2aServer.Builder#contextResolver
- honoring an envelope-asserted identity without a resolver logs a
  one-time warning; identity values in logs are bounded and stripped

Fixes agentscope-ai#1603
@mintlify

mintlify Bot commented Nov 2, 2026

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 50deb8f.

Approve preview

@jujn
jujn merged commit fcc36d6 into agentscope-ai:main Nov 2, 2026
7 checks passed
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]:A2A在传递userId、sessionId时发送逻辑和取值逻辑不一致,导致userId、sessionId在server端丢失。

2 participants