Skip to content

fix(ui): Retry streaming chat on 401 with force-refreshed token - #1430

Merged
huang195 merged 2 commits into
rossoctl:mainfrom
huang195:fix/streaming-chat-401-retry
May 3, 2026
Merged

huang195 merged 2 commits into
rossoctl:mainfrom
huang195:fix/streaming-chat-401-retry

Conversation

@huang195

@huang195 huang195 commented May 2, 2026

Copy link
Copy Markdown
Member

Summary

  • Adds 401 retry with force-refresh to the streaming chat path in AgentChat
  • Exposes forceRefreshToken from AuthContext for component-level use

Root cause

When a user creates a new agent, client-registration adds the agent's audience scope to Keycloak as a default scope. But the user's existing token was issued before the scope existed, so it lacks the aud claim. The non-streaming chat path already handles this via apiFetch's 401 retry logic (issue #1009), but the streaming path used raw fetch() without any retry — resulting in a persistent 401 that required a full re-login to resolve.

Changes

  • AuthContext.tsx: Added forceRefreshToken to the exported AuthContextType interface and context value
  • AgentChat.tsx: On 401 from the streaming endpoint, force-refreshes the token and retries the request once

Test plan

  • TypeScript compiles cleanly (tsc --noEmit)
  • No new lint errors introduced (pre-existing only)
  • Manual: create agent → immediately chat → should auto-recover without re-login

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

When a new agent is created, its audience scope is registered in Keycloak
after the user's current token was issued. The streaming chat path used
raw fetch() without 401 retry logic, so the stale token caused a
persistent 401 until the user manually re-logged.

Now the streaming path retries once with a force-refreshed token on 401,
matching the existing behavior in apiFetch (issue rossoctl#1009). Also exposes
forceRefreshToken from AuthContext so components can use it directly.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
- Release the connection slot before retrying on 401
- Apply same 401 retry pattern to handleHitlResponse

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>

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

Correct fix for a real usability bug — the streaming and HITL paths now match apiFetch's existing 401 retry pattern.

Areas reviewed: Frontend (TypeScript/React)
Commits: 2, all DCO signed-off
CI: All passing (E2E HyperShift pending manual trigger — expected)

token = await forceRefreshToken();
if (token) {
headers['Authorization'] = `Bearer ${token}`;
response = await fetch(streamUrl, {

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.

suggestion: The retry reuses the same AbortController. If the user clicks stop between the 401 and the retry completing, response.body?.cancel() could throw on an already-aborted stream. Consider wrapping the cancel in a try/catch or checking controller.signal.aborted before retrying:

if (response.status === 401 && !controller.signal.aborted) {
  try { await response.body?.cancel(); } catch { /* aborted */ }
  ...
}

if (token) {
headers['Authorization'] = `Bearer ${token}`;
response = await fetch(hitlUrl, { method: 'POST', headers, body: hitlBody });
}

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 HITL retry fires but the response is never checked. Pre-existing (original code fire-and-forgot too), but now that you're inspecting response.status for 401, it'd be consistent to also check the retry response:

if (!response.ok) {
  console.error(`[AgentChat] HITL ${action} failed after retry: ${response.status}`);
}

@huang195
huang195 merged commit 6891d45 into rossoctl:main May 3, 2026
22 checks passed
@huang195
huang195 deleted the fix/streaming-chat-401-retry branch May 3, 2026 11:52
@xjacka xjacka mentioned this pull request May 4, 2026
huang195 added a commit that referenced this pull request May 4, 2026
The streaming chat endpoint wrapped agent 401 responses inside SSE error
events (HTTP 200), preventing the frontend's existing retry logic (PR
#1430) from triggering token refresh. This caused users to see "Agent
error: 401" and need to manually re-login after deploying a new agent.

Root cause: when a new agent is deployed, client-registration adds its
audience scope to Keycloak. Tokens issued before that point lack the
agent's SPIFFE ID in the aud claim. The frontend's forceRefreshToken()
correctly obtains a new token with the updated audience, but it only
fires on HTTP 401 — not on SSE error payloads.

Fix: open the agent connection before committing to StreamingResponse.
If the agent returns 401, raise HTTPException(401) so the frontend sees
a real HTTP 401 and retries with a refreshed token transparently.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
aslom pushed a commit to aslom/rossoctl that referenced this pull request May 11, 2026
The streaming chat endpoint wrapped agent 401 responses inside SSE error
events (HTTP 200), preventing the frontend's existing retry logic (PR
rossoctl#1430) from triggering token refresh. This caused users to see "Agent
error: 401" and need to manually re-login after deploying a new agent.

Root cause: when a new agent is deployed, client-registration adds its
audience scope to Keycloak. Tokens issued before that point lack the
agent's SPIFFE ID in the aud claim. The frontend's forceRefreshToken()
correctly obtains a new token with the updated audience, but it only
fires on HTTP 401 — not on SSE error payloads.

Fix: open the agent connection before committing to StreamingResponse.
If the agent returns 401, raise HTTPException(401) so the frontend sees
a real HTTP 401 and retries with a refreshed token transparently.

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

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants