Repository navigation
fix(chat): Forward A2A contextId on every turn, adopt once - #1481
Conversation
9dbd2da to
aef30c8
Compare
| _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]>
aef30c8 to
a337295
Compare
|
/run-e2e |
E2E HyperShift Tests FailedCommit: To re-run tests after fixing issues, comment |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
|
/run-e2e |
E2E HyperShift Tests FailedCommit: To re-run tests after fixing issues, comment |
|
/run-e2e Previous run failed in |
E2E HyperShift Tests FailedCommit: To re-run tests after fixing issues, comment |
|
/run-e2e Prior run failed in |
|
E2E on hypershift failed 3 times not related to this PR:
Going to run e2e again |
E2E HyperShift Tests FailedCommit: To re-run tests after fixing issues, comment |
…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]>
Summary
The backend was synthesizing its own
session_idand never forwarding it to the agent asparams.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:Forward the client's session_id as
params.contextIdon 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.Adopt the agent-assigned
contextIdfrom the response only on the first turn (when the caller had nosession_idyet). Some A2A SDKs mint a freshcontextIdon every response even when the client supplied one — adopting that every turn was churning the UI'ssession_idstate and causing telemetry to split across a new bucket per message.Adds a
caller_supplied_session_idflag to_stream_from_responseso the adoption logic can see whether the caller is already on a known conversation, and one INFO-level log line tracing the outboundcontextIdand any first-turn adoption.Test plan
abctlshows one session bucket with event count climbing by ~23 per message (instead of one new bucket per message).[a2a-debug] adopting agent contextId (first turn): …log line appears only on the first message of each conversation, never on follow-ups.backend/tests/doesn't cover the A2A wire-format construction.Assisted-By: Claude (Anthropic AI) [email protected]