Skip to content

fix(a2a): resolve runner session IDs while preserving legacy metadata - #3746

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
guslegend0510:fix/a2a-runner-session-id
Oct 25, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
guslegend0510:fix/a2a-runner-session-id

Conversation

@guslegend0510

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT, based on main 07c5f3078.

Description

A standard A2A client that sends contextId without custom message.metadata["sessionId"] currently gives the server's AgentRunner an empty session ID. Resolve AgentRequestOptions.sessionId from 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

  • Added 20 parameterized cases across blocking and streaming requests using the real SDK 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.
  • JDK 17: mvn -B -T1C clean verify passed for all 99 modules. Test reports: 11,232 tests, 220 skipped, 0 failures/errors.
  • Documentation: npm test (13 passed), npm run validate, and npm run broken-links passed.
  • git diff --check passed.

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated
  • Code is ready for review

@mintlify

mintlify Bot commented Oct 23, 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 09744ea.

Approve preview

@jujn
jujn merged commit e6f015e into agentscope-ai:main Oct 25, 2026
6 checks passed
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.
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.

3 participants