feat(gateway): add channelHealthRestartMode to preserve Anthropic prompt cache on stale-socket#1674
Open
BingqingLyu wants to merge 2 commits into
Open
feat(gateway): add channelHealthRestartMode to preserve Anthropic prompt cache on stale-socket#1674BingqingLyu wants to merge 2 commits into
BingqingLyu wants to merge 2 commits into
Conversation
…on stale-socket
Add a configurable restart mode for the channel health-monitor that allows
lightweight channel reconnects instead of full stop/start cycles.
When set to "reconnect", the health-monitor skips resetting the restart-attempt
counter during a stale-socket recovery, signalling to the channel runtime that
this is a soft reconnect. This preserves SDK-level session state (e.g. Discord
gateway RESUME) and avoids invalidating Anthropic's prompt-cache context, which
is tied to the model inference session and gets cold-started on every channel
teardown.
Changes:
- Add ChannelHealthRestartMode type ("stop-start" | "reconnect") to channel config types
- Add restartMode field to ChannelHealthMonitorConfig
- Add channelHealthRestartMode to GatewayConfig
- Add defaultRestartMode param to ChannelHealthMonitorDeps
- Thread defaultRestartMode through server.impl.ts and server-reload-handlers.ts
- Add getChannelHealthMonitorConfig() to ChannelManager for per-channel overrides
- Resolve effective restartMode in health-monitor runCheck() with fallback to stop-start on reconnect failure
Closes openclaw#57569
- Rename 'reconnect' mode to 'graceful' — more accurate: the only difference is whether resetRestartAttempts() is called, not whether the underlying SDK reconnects - Remove misleading promise of 'Discord RESUME / Anthropic cache' preservation from JSDoc — that depends on provider implementation - Remove non-functional fallback log in catch block - Remove optional chaining on required ChannelManager.getChannelHealthMonitorConfig - Move ChannelHealthMonitorPublicConfig import to file top in server-channels.ts, eliminating inline import() in type signature - Simplify runCheck restart logic: stopChannel always runs first, then branch only on whether to reset the counter
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
Adds a configurable
channelHealthRestartModeoption to the channel health-monitor that allows lightweight channel reconnects instead of full stop/start cycles when a stale socket is detected.Closes openclaw#57569
Problem
When a channel (Discord, Telegram, etc.) reports a stale-socket, the health-monitor performs a
stopChannel → startChannelcycle. This is correct for hard failures, but for transient stale-socket conditions it has a costly side effect: Anthropic's prompt cache gets cold-started, forcing a full context re-send (~120k tokens) on the next turn.Real-world impact: ~240× token usage spike after any stale-socket event, until the cache warms back up over 2-3 turns.
Solution
Add a
restartModethat controls how the health-monitor handles channel recovery:"stop-start"(default): existing behavior — full teardown + restart, resets attempt counter"reconnect": lightweight recovery — stop + start without resetting the restart counter, signalling to the channel runtime that this is a soft reconnect. Falls back to"stop-start"on error.Changes
src/config/types.channels.tsChannelHealthRestartModetype andrestartModetoChannelHealthMonitorConfigsrc/config/types.gateway.tschannelHealthRestartModetoGatewayConfigsrc/gateway/channel-health-monitor.tsdefaultRestartModeparam; resolve effective mode per-channel inrunCheck()src/gateway/server-channels.tsgetChannelHealthMonitorConfig()toChannelManagersrc/gateway/server.impl.tsdefaultRestartModefrom config into health-monitorsrc/gateway/server-reload-handlers.tsdefaultRestartModethrough config-reload pathBackwards Compatibility
Default is
"stop-start"— no behaviour change for existing installs. Opt-in to"reconnect"explicitly.