Description
PR #3124 (issue #3114) gave authedFetch in apps/mobile/src/services/aiChat.ts a 401-triggered refresh-and-retry, but the XHR-based SSE send path (streamChat) still attaches the stored token directly with no refresh. A first chat message sent more than ~15 minutes after login — with no interim REST call to freshen the token — still 401s at stream open. The UI retry replays the same stale token, because HomeScreen.tsx skips createAiSession once a session id exists, so no refreshing REST call intervenes.
Both reviewers on #3124 flagged this; it was held out of that PR to keep the diff confined to authedFetch.
Proposed Fix
Apply the same refresh-once-on-401 lifecycle to the SSE open path: either preflight-refresh when the token is near/at expiry before opening the stream, or catch the 401 at stream open, call the shared refreshToken() (single-flighted, as in #3124), and reopen once.
Affected Files
apps/mobile/src/services/aiChat.ts (streamChat)
apps/mobile/src/screens/chat/HomeScreen.tsx (retry path context)
Reported By
Review findings on #3124; follow-up to #3114
Description
PR #3124 (issue #3114) gave
authedFetchinapps/mobile/src/services/aiChat.tsa 401-triggered refresh-and-retry, but the XHR-based SSE send path (streamChat) still attaches the stored token directly with no refresh. A first chat message sent more than ~15 minutes after login — with no interim REST call to freshen the token — still 401s at stream open. The UI retry replays the same stale token, becauseHomeScreen.tsxskipscreateAiSessiononce a session id exists, so no refreshing REST call intervenes.Both reviewers on #3124 flagged this; it was held out of that PR to keep the diff confined to
authedFetch.Proposed Fix
Apply the same refresh-once-on-401 lifecycle to the SSE open path: either preflight-refresh when the token is near/at expiry before opening the stream, or catch the 401 at stream open, call the shared
refreshToken()(single-flighted, as in #3124), and reopen once.Affected Files
apps/mobile/src/services/aiChat.ts(streamChat)apps/mobile/src/screens/chat/HomeScreen.tsx(retry path context)Reported By
Review findings on #3124; follow-up to #3114