fix: auto-refresh stale agent token on WebSocket failure - #67
Merged
Conversation
When an external process rotates the dashboard's agent token (via registerOrRotate or spawn), the cached token becomes invalid but the dashboard keeps returning it. This causes persistent WebSocket failures that survive page refresh. Server: add ?refresh=true param to GET /api/relay-config that clears both the in-memory token cache and the registration promise cache, forcing a fresh registerOrRotate call. Client: add TokenRefreshMonitor that detects when WebSocket stays in 'reconnecting' state for >10s and re-fetches config with refresh=true, causing RelayProvider to remount with the new valid token. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
at_live_tokens in memory and never validated them, so when another process (workflow restart, broker spawn, etc.) calledregisterOrRotatefor the same agent name, the cached token became permanently staleChanges
Server-side (3 files):
relaycast-provider-helpers.ts: ExportclearRegistrationCache()to flush the in-process registration promise cacheproxy-server.ts: AddclearCachedAgentToken()that clears bothinMemoryAgentTokenand the registration cacherelay-config.ts: Support?refresh=truequery param onGET /api/relay-configto force cache clear + re-registrationtypes.ts: AddclearCachedAgentTokentoRouteContextinterfaceClient-side (1 file):
RelayConfigProvider.tsx: AddTokenRefreshMonitorcomponent that watches WebSocket connection status. When it stays inreconnectingfor >10s, re-fetches/api/relay-config?refresh=trueto get a fresh token, causingRelayProviderto remount with the valid tokenRoot cause
The
@relaycast/reactRelayProvider uses anat_live_agent token for its WebSocket connection. Unlike workspace tokens (rk_live_), agent tokens get rotated whenregisterOrRotateis called for an existing agent. The dashboard server cached the token in two places (inMemoryAgentTokenandregistrationCacheMap) that were never cleared, so any external rotation left the dashboard permanently broken until the server process restarted.Test plan
rotateTokenendpoint externally, confirm WS recovers within ~15s/api/relay-config?refresh=truecalls)🤖 Generated with Claude Code