Repository navigation
fix(ui): Retry streaming chat on 401 with force-refreshed token - #1430
Conversation
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
left a comment
There was a problem hiding this comment.
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, { |
There was a problem hiding this comment.
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 }); | ||
| } |
There was a problem hiding this comment.
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}`);
}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]>
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]>
Summary
forceRefreshTokenfrom AuthContext for component-level useRoot 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
audclaim. The non-streaming chat path already handles this viaapiFetch's 401 retry logic (issue #1009), but the streaming path used rawfetch()without any retry — resulting in a persistent 401 that required a full re-login to resolve.Changes
forceRefreshTokento the exportedAuthContextTypeinterface and context valueTest plan
tsc --noEmit)Assisted-By: Claude (Anthropic AI) [email protected]