Skip to content

refactor(discord): let carbon own gateway reconnects - #59019

Merged
obviyus merged 2 commits into
mainfrom
refactor/discord-carbon-reconnect
Apr 1, 2026
Merged

obviyus merged 2 commits into
mainfrom
refactor/discord-carbon-reconnect

Conversation

@obviyus

@obviyus obviyus commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator

Fixes #58764
Fixes #58577

Carbon already owns the Discord gateway reconnect/resume state machine, so this removes the OpenClaw-side reconnect controller and hands reconnect behavior back to Carbon.

OpenClaw now only supervises startup readiness and lifecycle shutdown. It no longer reads gateway debug events as control flow, no longer suppresses reconnect exhaustion, and no longer carries the stale-socket/force-stop reconnect patch layer.

@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord agents Agent runtime and tooling size: XL maintainer Maintainer-authored PR labels Apr 1, 2026
@obviyus
obviyus force-pushed the refactor/discord-carbon-reconnect branch from efab5d1 to ab6da92 Compare April 1, 2026 13:05
@greptile-apps

greptile-apps Bot commented Apr 1, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR removes OpenClaw's bespoke gateway reconnect controller (provider.lifecycle.reconnect.ts, ~500 lines) and delegates the full reconnect/resume state machine back to Carbon. OpenClaw now only handles startup readiness polling and lifecycle shutdown, dramatically simplifying the code.

Key changes:

  • Deleted provider.lifecycle.reconnect.ts: removes the stall watchdog, HELLO-timeout heuristic, socket drain/terminate logic, and the force-stop channel that wired reconnect errors back into waitForDiscordGatewayStop.
  • New waitForGatewayReady in provider.lifecycle.ts: replaces the old ensureStartupReady with a simpler two-attempt ready-poll; on timeout it calls gateway.disconnect() + gateway.connect(false) and waits again, throwing on a second timeout.
  • reconnect-exhausted no longer suppressed: the old code suppressed this event during shutdown (because OpenClaw was deliberately setting maxAttempts=0). Now it surfaces as a lifecycle error, letting the monitor supervisor decide whether to restart.
  • Gateway debug-event markers updated to match new Carbon string format (\"Gateway websocket closed\" instead of \"WebSocket connection closed\").
  • context-pruning.test.ts: two test calls gain the new required dropThinkingBlocks: false field — an unrelated fixture fix bundled in this PR.
  • Minor: readyState?: number added to DiscordGatewaySocket type (currently unused).

Confidence Score: 5/5

Safe to merge — no critical or blocking issues found; remaining findings are minor style and test-coverage suggestions.

All three findings are P2: one is a very-narrow-window race (effectively cosmetic given single-threaded JS), one is an unused type field, and one is a missing assertion in a test. The architectural simplification is well-motivated, the behavioral changes are intentional and clearly documented, and the updated test suite covers the key new paths.

No files require special attention, but reviewers may want to confirm that the Carbon library version in use handles disconnect() followed immediately by connect(false) without racing a socket-close-triggered auto-resume.

Comments Outside Diff (1)

  1. extensions/discord/src/monitor/gateway-handle.ts, line 14 (link)

    P2 Unused readyState field on DiscordGatewaySocket

    readyState?: number is added to the type, but is not read anywhere in the updated (or remaining) code in this extension. The provider.lifecycle.reconnect.ts file that used MutableDiscordGateway.ws was deleted in this PR, and no remaining code references this field.

    If this is forward-looking, a short comment would clarify intent; otherwise it can be deferred until there's a concrete use-site.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: extensions/discord/src/monitor/gateway-handle.ts
    Line: 14
    
    Comment:
    **Unused `readyState` field on `DiscordGatewaySocket`**
    
    `readyState?: number` is added to the type, but is not read anywhere in the updated (or remaining) code in this extension. The `provider.lifecycle.reconnect.ts` file that used `MutableDiscordGateway.ws` was deleted in this PR, and no remaining code references this field.
    
    If this is forward-looking, a short comment would clarify intent; otherwise it can be deferred until there's a concrete use-site.
    
    How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: extensions/discord/src/monitor/provider.lifecycle.ts
Line: 79-80

Comment:
**No abort guard before gateway restart**

`disconnect()` and `connect(false)` are called without first checking whether the abort signal has fired. If an abort is received in the brief microtask window between `waitUntilReady()` returning `"timeout"` and these two calls executing, the gateway will be restarted unnecessarily. The second `waitUntilReady()` will return `"stopped"` immediately, so there is no lasting harm — but the spurious `connect(false)` could leave a dangling reconnect attempt visible at a higher level.

The old `reconnectGatewayFresh` path guarded this with `if (shouldStop()) return` before calling `disconnect`. Consider adding a similar early-exit here:

```typescript
if (params.abortSignal?.aborted) {
  return;
}
params.gateway.disconnect();
params.gateway.connect(false);
```

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: extensions/discord/src/monitor/gateway-handle.ts
Line: 14

Comment:
**Unused `readyState` field on `DiscordGatewaySocket`**

`readyState?: number` is added to the type, but is not read anywhere in the updated (or remaining) code in this extension. The `provider.lifecycle.reconnect.ts` file that used `MutableDiscordGateway.ws` was deleted in this PR, and no remaining code references this field.

If this is forward-looking, a short comment would clarify intent; otherwise it can be deferred until there's a concrete use-site.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: extensions/discord/src/monitor/provider.lifecycle.test.ts
Line: 271-274

Comment:
**Missing `connect(false)` argument assertion in failure path**

The "fails when startup still is not ready after a restart" test asserts `gateway.connect` was called once, but drops the `expect(gateway.connect).toHaveBeenCalledWith(false)` assertion that was present in the old version. The sibling "restarts the gateway once … then recovers" test does assert the `false` argument, but the failure-path test would benefit from the same check to confirm the restart always uses a fresh identify (`false`) rather than a resume.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "refactor(discord): let carbon own gatewa..." | Re-trigger Greptile

@obviyus
obviyus force-pushed the refactor/discord-carbon-reconnect branch 2 times, most recently from 7716b0c to 51afe63 Compare April 1, 2026 13:29
@obviyus obviyus self-assigned this Apr 1, 2026
@obviyus
obviyus force-pushed the refactor/discord-carbon-reconnect branch from 51afe63 to 1061d01 Compare April 1, 2026 13:41
@openclaw-barnacle openclaw-barnacle Bot removed the agents Agent runtime and tooling label Apr 1, 2026
@obviyus
obviyus merged commit 095e7b8 into main Apr 1, 2026
9 checks passed
@obviyus
obviyus deleted the refactor/discord-carbon-reconnect branch April 1, 2026 13:42
@obviyus

obviyus commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator Author

Landed on main.

yetval added a commit to yetval/openclaw that referenced this pull request Apr 3, 2026
Resolves conflicts from the carbon-native reconnect refactor (openclaw#59019):
- Remove provider.lifecycle.reconnect.ts (deleted in refactor)
- Accept rewritten provider.lifecycle.test.ts (watchdog tests no longer apply)
jjjojoj pushed a commit to jjjojoj/openclaw-jjjojoj that referenced this pull request Apr 6, 2026
* refactor(discord): let carbon own gateway reconnects

* fix: finalize Discord gateway reconnect landing (openclaw#59019)
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
* refactor(discord): let carbon own gateway reconnects

* fix: finalize Discord gateway reconnect landing (openclaw#59019)
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
* refactor(discord): let carbon own gateway reconnects

* fix: finalize Discord gateway reconnect landing (openclaw#59019)
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
* refactor(discord): let carbon own gateway reconnects

* fix: finalize Discord gateway reconnect landing (openclaw#59019)
Nachx639 pushed a commit to Nachx639/clawdbot that referenced this pull request Jun 17, 2026
* refactor(discord): let carbon own gateway reconnects

* fix: finalize Discord gateway reconnect landing (openclaw#59019)
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
* refactor(discord): let carbon own gateway reconnects

* fix: finalize Discord gateway reconnect landing (openclaw#59019)
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request Sep 29, 2026
* refactor(discord): let carbon own gateway reconnects

* fix: finalize Discord gateway reconnect landing (openclaw#59019)
lhfer pushed a commit to lhfer/openclaw that referenced this pull request Oct 11, 2026
* refactor(discord): let carbon own gateway reconnects

* fix: finalize Discord gateway reconnect landing (openclaw#59019)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: discord Channel integration: discord maintainer Maintainer-authored PR size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gateway crashes on Discord WebSocket close code 1005 (maxReconnectAttempts=0) Discord gateway never reaches ready state after stale-socket recovery

1 participant