Skip to content

fix(discord): make gateway close codes and resume outcomes unambiguous (#15) - #21

Merged
EthanThatOneKid merged 1 commit into
mainfrom
fix/gateway-close-code-diagnostics
Sep 26, 2026
Merged

EthanThatOneKid merged 1 commit into
mainfrom
fix/gateway-close-code-diagnostics

Conversation

@EthanThatOneKid

Copy link
Copy Markdown
Contributor

Instruments the gateway so wazootech/data#15 can be diagnosed and its misleading log lines stop misdiagnosing it. No behavior change to admission or answering.

The two defects in the log vocabulary

  1. Our local closes were indistinguishable from Discord's. 4002 is Discord's decode error; the bridge also used 4002 for its own invalid-session close, so close 4002 in the log read as a Discord rejection while it was our own reaction to one. Local closes move to 4900-4902 (Discord's documented codes end at 4014) and every close event is now rendered by describeClose(), which labels the side that raised it: local close 4902 (local: invalid session) vs Discord close 4002 (decode error).
  2. gateway socket open (resume) was chosen by URL, not by decision. After the first READY the resume URL stays in use, so a socket that was about to identify fresh still logged (resume). It now reports the decision: gateway socket open (fresh identify) or gateway socket open (resume from seq <n>).

Evidence now recorded per cycle

  • resuming session <id> at seq <n> at the moment a RESUME is sent.
  • A rejected resume is named explicitly: if READY arrives while a resume is pending, the log says the resume was rejected and the session was dropped, rather than silently reporting a normal READY.
  • session invalidated by Discord (op 9, resumable=<bool>), annotated with , in reply to our resume when it answers one — this separates Discord invalidated the session from our resume payload was rejected, which is the open question in data-discord never completes a gateway resume, reconnecting every ~40 min #15.

Verification

npm run typecheck is clean and npm test is 30/30 (three new tests in lib/discord-gateway.test.ts cover the close-code vocabulary, the local/Discord distinction for 4002, and the transport cases).

The underlying question in #15 — why the transport drops every ~40 min — is still open, and this change is what #15 asked for first ("log the raw close code and reason distinctly from our local close codes"). Once it deploys, one reconnect cycle is enough to tell a rejected resume from an invalidated session. #15's third recommendation (replace the hand-rolled gateway with a maintained client, as FartLabs/goop does with discord.js) is a larger change and remains a decision, not something this PR assumes.

@wazoocomputer

wazoocomputer Bot commented Sep 26, 2026

Copy link
Copy Markdown

This pull request refactors the Discord gateway to make close codes and resume outcomes unambiguous in the logs, addressing the diagnostic challenges described in #15. It clarifies whether a close event originated from Discord or locally, and precisely reports the decision behind a socket opening (fresh identify or resume). This change is purely for instrumentation and does not alter admission or answering behavior.

File Path Change Type Summary
channels/discord/index.ts modified Update close code handling and resume logging
lib/discord-gateway.test.ts added Add tests for close code description and local/Discord distinction
lib/discord-gateway.ts added Define local close codes and description logic

Reviewers should begin by examining lib/discord-gateway.ts to understand the new close code vocabulary and lib/discord-gateway.test.ts for its coverage.

1 similar comment
@wazoocomputer

wazoocomputer Bot commented Sep 26, 2026

Copy link
Copy Markdown

This pull request refactors the Discord gateway to make close codes and resume outcomes unambiguous in the logs, addressing the diagnostic challenges described in #15. It clarifies whether a close event originated from Discord or locally, and precisely reports the decision behind a socket opening (fresh identify or resume). This change is purely for instrumentation and does not alter admission or answering behavior.

File Path Change Type Summary
channels/discord/index.ts modified Update close code handling and resume logging
lib/discord-gateway.test.ts added Add tests for close code description and local/Discord distinction
lib/discord-gateway.ts added Define local close codes and description logic

Reviewers should begin by examining lib/discord-gateway.ts to understand the new close code vocabulary and lib/discord-gateway.test.ts for its coverage.

@EthanThatOneKid
EthanThatOneKid merged commit a29a3f4 into main Sep 26, 2026
1 check passed
@EthanThatOneKid
EthanThatOneKid deleted the fix/gateway-close-code-diagnostics branch September 26, 2026 07:58
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.

1 participant