Skip to content

fix(gateway): reset heartbeat ack state on reconnect#353

Merged
thewilloftheshadow merged 2 commits intobuape:mainfrom
BYWallace:codex/gateway-heartbeat-ack-reset
Feb 16, 2026
Merged

fix(gateway): reset heartbeat ack state on reconnect#353
thewilloftheshadow merged 2 commits intobuape:mainfrom
BYWallace:codex/gateway-heartbeat-ack-reset

Conversation

@BYWallace
Copy link
Copy Markdown
Contributor

@BYWallace BYWallace commented Feb 11, 2026

Summary

Uncovered this when running my personal OpenClaw assistant using Discord as a channel for a bot: #353
#353

Have opened a downstream fix to harden their logic but also discovered some improvements for Carbon.

  • reset lastHeartbeatAck when a new heartbeat cycle starts in startHeartbeat(...)
  • add a defensive lastHeartbeatAck reset in GatewayPlugin.disconnect()
  • add regression tests for stale ACK carryover and restart behavior

Why

Issue #350 identified a stale heartbeat ACK state that can carry across reconnects and cause immediate reconnect churn on the first heartbeat of a new connection.

This PR fixes that state carryover directly in gateway heartbeat lifecycle.

Files changed

  • packages/carbon/src/plugins/gateway/utils/heartbeat.ts
  • packages/carbon/src/plugins/gateway/GatewayPlugin.ts
  • packages/carbon/tests/plugins/gateway-heartbeat.test.ts

Tests

Ran:

  • pnpm test packages/carbon/tests/plugins/gateway-heartbeat.test.ts

Results:

  • 1 file passed
  • 3 tests passed

Closes #350

Summary by CodeRabbit

  • Bug Fixes

    • Reset gateway heartbeat acknowledgment state during disconnects and when restarting heartbeats to avoid stale ACKs and reduce false reconnects.
  • Tests

    • Added tests covering heartbeat start, disconnect behavior, and repeated restarts to ensure stable connection handling.
  • Chores

    • Added a changeset documenting the heartbeat ACK reset fix for a patch release.

@BYWallace BYWallace requested a review from a team as a code owner February 11, 2026 06:17
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Feb 11, 2026

🦋 Changeset detected

Latest commit: 03b7a8b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@buape/carbon Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the package: carbon The carbon package label Feb 11, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 11, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Resets the gateway heartbeat acknowledgement state by setting lastHeartbeatAck = true when starting heartbeats and during GatewayPlugin.disconnect. Adds tests and a changeset documenting the fix to avoid stale-ACK driven reconnection behavior.

Changes

Cohort / File(s) Summary
Heartbeat logic
packages/carbon/src/plugins/gateway/utils/heartbeat.ts, packages/carbon/src/plugins/gateway/GatewayPlugin.ts
Set manager.lastHeartbeatAck = true at the start of startHeartbeat and set lastHeartbeatAck = true in GatewayPlugin.disconnect to avoid treating prior stale state as an unacked heartbeat.
Tests
packages/carbon/tests/plugins/gateway-heartbeat.test.ts
New Vitest suite with fake timers and deterministic randomness covering startHeartbeat, disconnect, and repeated restarts to assert ACK-reset behavior and correct Heartbeat sends.
Release metadata
.changeset/bright-insects-smile.md, package.json
Add changeset noting the heartbeat ACK reset fix; package manifest line updated.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰 I nudge the pulses, soft and spry,

lastACK set true so echoes die,
No stale loops hopping through the night,
Reconnects behave, hearts beat right,
A tiny fix — a rabbit’s delight.

🚥 Pre-merge checks | ✅ 3 | ❌ 2
❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Linked Issues check ❓ Inconclusive The PR addresses issue #350's root cause by preventing stale heartbeat ACK state from triggering immediate reconnection logic, but does not fully implement the suggested multi-attempt stale session detection. Verify whether resetting lastHeartbeatAck alone resolves #350 or if additional stale session detection (clearing sessionId/resumeGatewayUrl after N failed attempts) is still required.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(gateway): reset heartbeat ack state on reconnect' accurately describes the main change: resetting lastHeartbeatAck state during heartbeat initialization and disconnect.
Out of Scope Changes check ✅ Passed All changes are directly related to resetting heartbeat ACK state on reconnection and disconnect, with corresponding regression tests; no out-of-scope modifications detected.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@thewilloftheshadow thewilloftheshadow added this pull request to the merge queue Feb 16, 2026
@thewilloftheshadow
Copy link
Copy Markdown
Member

Thanks for the PR!

@thewilloftheshadow thewilloftheshadow removed this pull request from the merge queue due to a manual request Feb 16, 2026
@thewilloftheshadow thewilloftheshadow merged commit 51ffa8f into buape:main Feb 16, 2026
5 of 6 checks passed
@BYWallace BYWallace deleted the codex/gateway-heartbeat-ack-reset branch February 17, 2026 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: carbon The carbon package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gateway: stale resume sessions loop forever when maxAttempts is high/infinite

2 participants