feat: add websocket auto-reconnect, resubscribe, and re-auth - #6
feat: add websocket auto-reconnect, resubscribe, and re-auth#6harley-poly wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit b760be9. Configure here.
| this.socket.addEventListener('error', (event: unknown) => { | ||
| this.handleError(event); | ||
| socket.addEventListener('error', (event: unknown) => { | ||
| this.emitter._emit('error', this.toError(event)); |
There was a problem hiding this comment.
Reconnect emits spurious error events per failed attempt
Medium Severity
Every call to openSocket registers a permanent error listener that emits to the user. During reconnect, each failed attempt fires this listener before the promise rejects, so users see a user-visible error event for every transient failure the reconnect loop is silently recovering from. For fatal auth failures (401/403/429), users get two error events—one from the permanent listener and another from the explicit _emit('error', …) in the reconnect catch block. The onClose handler is properly guarded by this.reconnecting, but the error listener has no such guard. The test for fatal auth expects exactly 1 onError call, which only passes because openSocket is mocked away entirely.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit b760be9. Configure here.
|
Closing at the author’s request as part of open-PR backlog cleanup. The branch is being retained if this work needs to be revisited. |


summary
makes the private and markets websockets resilient to dropped connections:
autoReconnect: trueby default;reconnectMaxAttemptsconfigurable).subscribe/unsubscribe; areconnectevent fires after a successful reconnect.wserror); transient/network failures keep retrying.note: per the server protocol,
order/position/tradestreams don't replay history on resubscribe (market-data and account-balance snapshots are re-sent automatically); useSUBSCRIPTION_TYPE_ORDER_SNAPSHOTto refetch open orders after a reconnect.test plan
pnpm lint(biome)pnpm typecheck/pnpm buildpnpm test(137 passed; newtests/websocket-reconnect.test.tscovers subscription tracking, resubscribe replay, transient-then-success reconnect, fatal-auth stop, and max-attempts)Note
Medium Risk
Changes real-time connection lifecycle and auth on reconnect; incorrect behavior could miss updates or retry invalid credentials, though fatal auth stops and tests mitigate common cases.
Overview
Adds automatic WebSocket recovery for private and markets streams: on unexpected drops,
BaseWebSocketreconnects with exponential backoff + jitter, re-signs the upgrade with fresh auth headers, replays trackedsubscribecalls, and emits a newreconnectevent. OptionsautoReconnect(default on) andreconnectMaxAttemptscontrol behavior; 401/403/429 upgrade failures stop retries and surfaceerror/close.Connection open/close/error handling moves into the base class (subclasses drop duplicate
handleError/handleClose).subscribe/unsubscribemaintain a subscription map for replay. README documents reconnect semantics and notes that order/position/trade history is not replayed on resubscribe (use order snapshot where needed).New
tests/websocket-reconnect.test.tscovers tracking, resubscribe, reconnect loop, guards, and edge cases.Reviewed by Cursor Bugbot for commit b760be9. Bugbot is set up for automated code reviews on this repo. Configure here.