Skip to content

fix(realtime): refresh tokens in background without dropping the WebSocket connection#200

Merged
noahgsolomon merged 3 commits intomainfrom
noah/fix-realtime-token-refresh-state-fix
Feb 25, 2026
Merged

fix(realtime): refresh tokens in background without dropping the WebSocket connection#200
noahgsolomon merged 3 commits intomainfrom
noah/fix-realtime-token-refresh-state-fix

Conversation

@noahgsolomon
Copy link
Contributor

dqwdqd.mp4

The active state had no handler for token expiry, so the scheduled expireToken event was silently dropped and tokens were never refreshed. Instead of tearing down and reconnecting, proactively fetch a fresh token at 90% of its lifetime and update the stored token in-place via a new authenticated transition in the active state. The WebSocket stays open and the fresh token is ready for any future reconnection. Includes retry logic if a background refresh fails.

…ocket connection

The active state had no handler for token expiry, so the scheduled
expireToken event was silently dropped and tokens were never refreshed.
Instead of tearing down and reconnecting, proactively fetch a fresh
token at 90% of its lifetime and update the stored token in-place via a
new authenticated transition in the active state. The WebSocket stays
open and the fresh token is ready for any future reconnection. Includes
retry logic if a background refresh fails.
@noahgsolomon noahgsolomon force-pushed the noah/fix-realtime-token-refresh-state-fix branch from d7e2baa to 0c3a73a Compare February 25, 2026 18:29
Add guards to skip token refresh operations if the
state machine is not in the active state. Prevents
stale callbacks from spawning parallel refresh loops
or sending events after disconnect.

Co-authored-by: Cursor <cursoragent@cursor.com>
Replace machine.current checks with a generation counter
because robot3 creates new machine objects on each
transition, making closure-captured references stale.
@noahgsolomon noahgsolomon merged commit b802926 into main Feb 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants