Skip to content

fix(chat): Forward A2A contextId on every turn, adopt once - #1481

Merged
huang195 merged 1 commit into
rossoctl:mainfrom
huang195:fix/a2a-contextid-forwarding
May 6, 2026
Merged

huang195 merged 1 commit into
rossoctl:mainfrom
huang195:fix/a2a-contextid-forwarding

Conversation

@huang195

@huang195 huang195 commented May 6, 2026

Copy link
Copy Markdown
Member

Summary

The backend was synthesizing its own session_id and never forwarding it to the agent as params.contextId. Every HTTP call from the UI created a fresh A2A context server-side, which also caused the authbridge session telemetry viewer (abctl) to split a single conversation across many session buckets.

Two behaviors changed in /kagenti/backend/app/routers/chat.py:

  1. Forward the client's session_id as params.contextId on every A2A send. Applies to both /send (non-streaming) and /stream (SSE). Multi-turn conversations now stay in one A2A context chain as the spec requires.

  2. Adopt the agent-assigned contextId from the response only on the first turn (when the caller had no session_id yet). Some A2A SDKs mint a fresh contextId on every response even when the client supplied one — adopting that every turn was churning the UI's session_id state and causing telemetry to split across a new bucket per message.

Adds a caller_supplied_session_id flag to _stream_from_response so the adoption logic can see whether the caller is already on a known conversation, and one INFO-level log line tracing the outbound contextId and any first-turn adoption.

Test plan

  • Manual: send 2-3 messages in one conversation via the UI, confirm abctl shows one session bucket with event count climbing by ~23 per message (instead of one new bucket per message).
  • Manual: start a fresh second conversation, confirm it creates exactly one new bucket and stays there across all its turns.
  • Confirmed the [a2a-debug] adopting agent contextId (first turn): … log line appears only on the first message of each conversation, never on follow-ups.
  • Automated test coverage could follow — the current test suite in backend/tests/ doesn't cover the A2A wire-format construction.

Assisted-By: Claude (Anthropic AI) [email protected]

@huang195
huang195 force-pushed the fix/a2a-contextid-forwarding branch from 9dbd2da to aef30c8 Compare May 6, 2026 02:55
Comment on lines +553 to +559
_stream_from_response(
client,
response,
session_id,
user.username,
caller_supplied_session_id=bool(request.session_id),
),
The backend was synthesizing its own session_id and never forwarding it
to the agent as params.contextId. Each HTTP call from the UI created a
fresh A2A context server-side, which in turn caused authbridge's session
telemetry to split one conversation into many buckets.

Two behaviors changed:

1. When the UI supplies session_id on a message, forward it as
   params.contextId in the outbound A2A JSON-RPC. Multi-turn conversations
   now stay in one A2A context chain as the spec requires.

2. Adopt the agent-assigned contextId from the response ONLY on the very
   first turn (when the caller had no session_id yet). Some A2A SDKs mint
   a fresh contextId on every response even when the client supplied one;
   adopting that every turn was churning the UI's session_id state and
   causing telemetry to split across a new bucket per message.

Applies to both /send (non-streaming) and /stream (SSE) handlers.
Adds a caller_supplied_session_id flag to _stream_from_response so the
adoption logic can see whether the caller is on a known conversation.
Adds one INFO log line that traces the outbound contextId and any
first-turn adoption — useful for diagnosing session-attribution issues.

Signed-off-by: Hai Huang <[email protected]>
@huang195
huang195 force-pushed the fix/a2a-contextid-forwarding branch from aef30c8 to a337295 Compare May 6, 2026 02:58
@huang195

huang195 commented May 6, 2026

Copy link
Copy Markdown
Member Author

/run-e2e

@github-actions github-actions Bot added the safe-to-test Maintainer reviewed - safe to run E2E tests label May 6, 2026
@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

E2E HyperShift Tests Started

Triggered by: @huang195
Commit: a33729528f7e75facb025420e8e1fcb15e5ac523
Workflow Run: View logs

Tests are running on OpenShift 4.20.11. This typically takes 30-45 minutes.


To re-run tests after new commits, comment /run-e2e again.

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

E2E HyperShift Tests Failed

Commit: a33729528f7e75facb025420e8e1fcb15e5ac523
Result: Tests failed - please check the logs
Workflow Run: View logs


To re-run tests after fixing issues, comment /run-e2e again.

@esnible esnible left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Single-file Python change that correctly fixes A2A contextId forwarding and first-turn adoption for both /send (non-streaming) and /stream (SSE) paths. The core logic is sound: forward the client's session_id as params.contextId on outbound A2A calls, and only adopt the agent-assigned contextId on the very first turn. The ChatResponse already returns session_id, so the adopted ID propagates back to the UI correctly.

All CI checks pass. The E2E HyperShift failure is an infrastructure teardown timeout unrelated to this change (Ansible reported failed=0 in the final play recap — the cluster was successfully destroyed).

Assisted-By: Claude Code

PS: I would love to see a diagram, Mermaid or ASCII art, showing the flow of authorization and session headers and cookies from the browser to the ui-backend to authbridge to the workload, then to authbridge again and then to a tool and back. https://github.com/kagenti/kagenti/blob/main/docs/tech-details.md gives the networking layer but as a new developer on the team it was difficult to understand what changes this PR introduced.

I was also unable to understand if these changes would have an effect if the end user was using the A2A Inspector instead of Kagenti to talk to the agents.

# turns must reuse the caller's session_id — some agent SDKs
# mint a fresh contextId every response, and adopting that
# would churn the UI state and split events across buckets.
if not request.session_id:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: The /send adoption guard is if agent_ctx: while the /stream path (added below in _stream_from_response) uses if agent_ctx and agent_ctx != session_id:. The extra check is harmless here since on a first turn session_id is a freshly-minted uuid4().hex (near-zero chance of colliding with the agent's contextId), but the two blocks read as if they should match. Consider adding and agent_ctx != session_id here for consistency.

# turns must reuse the caller's session_id — some agent SDKs
# mint a fresh contextId every response, and adopting that
# would churn the UI state and split events across buckets.
if not request.session_id:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: The PR description says "Adds one INFO log line tracing the outbound contextId and any first-turn adoption" and the test plan references a [a2a-debug] adopting agent contextId (first turn) log line. The diff contains no added log lines — it only removes an existing logger.debug(...). Worth updating the PR description to match the actual change.

@huang195

huang195 commented May 6, 2026

Copy link
Copy Markdown
Member Author

/run-e2e

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

E2E HyperShift Tests Started

Triggered by: @huang195
Commit: a33729528f7e75facb025420e8e1fcb15e5ac523
Workflow Run: View logs

Tests are running on OpenShift 4.20.11. This typically takes 30-45 minutes.


To re-run tests after new commits, comment /run-e2e again.

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

E2E HyperShift Tests Failed

Commit: a33729528f7e75facb025420e8e1fcb15e5ac523
Result: Tests failed - please check the logs
Workflow Run: View logs


To re-run tests after fixing issues, comment /run-e2e again.

@huang195

huang195 commented May 6, 2026

Copy link
Copy Markdown
Member Author

/run-e2e

Previous run failed in agent-oauth-secret-1 OpenShift build with Could not resolve host: github.com — transient DNS issue in the HyperShift CI env, unrelated to this PR. Retrying.

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

E2E HyperShift Tests Started

Triggered by: @huang195
Commit: a33729528f7e75facb025420e8e1fcb15e5ac523
Workflow Run: View logs

Tests are running on OpenShift 4.20.11. This typically takes 30-45 minutes.


To re-run tests after new commits, comment /run-e2e again.

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

E2E HyperShift Tests Failed

Commit: a33729528f7e75facb025420e8e1fcb15e5ac523
Result: Tests failed - please check the logs
Workflow Run: View logs


To re-run tests after fixing issues, comment /run-e2e again.

@huang195

huang195 commented May 6, 2026

Copy link
Copy Markdown
Member Author

/run-e2e

Prior run failed in Deploy Kagenti with registry.redhat.io returning 503/504 (cert-manager, servicemesh-operator, keycloak all stuck in ImagePullBackOff). Registry outage, unrelated to this PR. Retrying.

@huang195

huang195 commented May 6, 2026

Copy link
Copy Markdown
Member Author

E2E on hypershift failed 3 times not related to this PR:

  • 1st (25413706469) │ Backend E2E tests │ HTTP 401 from agent — pre-existing Keycloak/token fixture issue
  • 2nd (25434841506) │ OpenShift build of agent-oauth-secret │ Could not resolve host: github.com — DNS flake
  • 3rd (25437081400) │ Deploy Kagenti │ registry.redhat.io returning 503/504 — registry outage

Going to run e2e again

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

E2E HyperShift Tests Started

Triggered by: @huang195
Commit: a33729528f7e75facb025420e8e1fcb15e5ac523
Workflow Run: View logs

Tests are running on OpenShift 4.20.11. This typically takes 30-45 minutes.


To re-run tests after new commits, comment /run-e2e again.

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

E2E HyperShift Tests Failed

Commit: a33729528f7e75facb025420e8e1fcb15e5ac523
Result: Tests failed - please check the logs
Workflow Run: View logs


To re-run tests after fixing issues, comment /run-e2e again.

@huang195
huang195 merged commit 6ba371c into rossoctl:main May 6, 2026
22 checks passed
@huang195
huang195 deleted the fix/a2a-contextid-forwarding branch May 6, 2026 15:18
huang195 added a commit that referenced this pull request May 7, 2026
…ealm

Root cause of the HyperShift E2E 401s on PR #1481 (and other PRs that
happened to race the operator's ClientRegistrationReconciler): the test
token had no `aud` claim because the Keycloak realm scope that carries
the audience mapper (`agent-team1-weather-service-aud`) was not yet
present when `87-setup-test-credentials.sh` queried for it. The fixture
logged "No agent audience scopes found (agents may not be deployed yet)"
and moved on, attaching nothing to `kagenti-e2e-tests`. The subsequent
`client_credentials` token therefore contained no `aud`, and AuthBridge's
`jwt-validation` plugin rejected every inbound request with "audience is
required (prevents confused deputy attacks)" → 401.

The scope is created asynchronously by kagenti-operator's
ClientRegistrationReconciler (default path) or the legacy
client-registration sidecar (opt-in). Neither is synchronous with the
weather-service Deployment admission, so the fixture and the scope
creator race.

This change gates the fixture on the observable output of that
reconciler — scope presence in realm `default-default-client-scopes` —
rather than orchestrating startup timing.

Changes:
- 87-setup-test-credentials.sh: when agent deployments exist in team1,
  poll the realm for at least one agent-*-aud scope before attaching.
  Default timeout 300s; SCOPE_WAIT_TIMEOUT env var overrides. Admin
  token is refreshed each iteration (master-realm admin tokens live
  ~60s). Happy path pays ~0s (first iteration finds scopes).
- tests/conftest.py: extract token acquisition into `_acquire_agent_token`
  and expose a new `keycloak_agent_token_refresh` fixture that returns a
  zero-arg callable minting a fresh token. Existing `keycloak_agent_token`
  keeps its session-scope cache behavior; the refresh fixture is there so
  tests can pick up a freshly-scoped token if a future race puts scope
  attachment after the session fixture cached its tokenless-of-aud value.
  Not wired into tests in this PR; left as follow-up pending observation.

Verification plan:
- Green Kind E2E with artificial operator stall: add sleep 60 in
  74-deploy-weather-agent.sh, verify wait loop kicks in and proceeds
  once scope appears.
- HyperShift re-run via /run-e2e on a PR to confirm: expected log
  sequence is "Waiting up to 300s for agent-*-aud scopes..." followed
  by "Agent audience scope(s) detected after Ns" within ~6 min, then
  "Added scope 'agent-team1-weather-service-aud' to kagenti-e2e-tests".
- Regression check on a healthy cluster: expect ~0s detection time, no
  measurable slowdown on the happy path.

Refs: issue seen on #1480, #1481.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

safe-to-test Maintainer reviewed - safe to run E2E tests

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants