Repository navigation
feat(gateway): resilient connection lifecycle, transport observability, and normalized event types - #12
Merged
Conversation
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
Reworks the WebSocket gateway into a pure-protocol, single-use
GatewayConnectionwith an explicit reconnect state machine, addstransport-level observability, and opens the protocol types to the
platform's additive field evolution. Terminal failures are now reported
by rejecting
start()with stable error codes instead of hanging orretrying silently.
Connection & recovery
session and RESUME with the last seq, so events missed during the
outage are replayed by the server instead of being lost to a fresh
IDENTIFY. Reconnect budget only resets after READY/RESUMED, so
"connect-then-kicked" loops still exhaust a finite budget.
means the link is dead — the client closes and reconnects, with a
watchdog to terminate the socket when even the close handshake stalls
in a network black hole.
start()once with aGatewayError(
GATEWAY_RETRY_EXHAUSTEDfor finite budgets,GATEWAY_FATAL_CLOSEfor 4914/4915) instead of looping forever. The unlimited default is
unchanged; set
reconnect.maxAttemptswhen an outer supervisor ownsrestarts.
heartbeat_intervalvaluesno longer clamp
setIntervalto 1ms (heartbeat storm) — they failfast and reconnect; numeric strings are coerced for forward
compatibility. The jittered first heartbeat is tracked and released
in
cleanup(), so a finished run never holds the event loop.Observability
bot.connectionState/bot.ping/bot.lastHeartbeatAt— healthchecks without tracking
ready/resumedyourselfdisconnectedevent ({ code, reason, willReconnect }) firedafter the reconnect decision is made, so
willReconnectalwaysreflects reality
connectionevent streaming state transitions(
idle → connecting → connected/resuming → disconnected)handler can never stall the protocol loop
Type layer
InboundMessage.author— normalized, closed sender model; flatsenderId/senderName/senderIsBotare deprecated but keptOpenFieldsopen index signatures +msg.rawpassthrough: newplatform fields are readable immediately without SDK updates
GatewayErrormatches on a stablecodestring (notinstanceof),so it works across module copies
Fixes
replacement refresh loop's controller
Previously a fatal close (4914/4915) or an exhausted retry budget left
start()pending forever — no rejection, no cleanup, no signal to thecaller. It now rejects once with a
GatewayErrorafter releasingresources. Callers that don't
await bot.start()should add a.catch()to avoid an unhandledRejection crash.Testing
217 tests green, including integration tests against a real WebSocket
server (
test/helpers/fake-gateway.ts): resume-on-reconnect, close-codedecision table, black-holed handshake timeout, heartbeat-storm
regression, budget exhaustion and restart. Coverage on changed modules:
reconnect.ts91%,token.ts92%,gateway-connection.ts~74%,errors.ts100%.Docs
docs link, feedback assistant and developer channel QR codes (bundled
into the npm package)