Skip to content

feat(gateway): add channelHealthRestartMode to preserve Anthropic prompt cache on stale-socket#1674

Open
BingqingLyu wants to merge 2 commits into
mainfrom
fork-pr-57572-feat-channel-health-reconnect-mode
Open

feat(gateway): add channelHealthRestartMode to preserve Anthropic prompt cache on stale-socket#1674
BingqingLyu wants to merge 2 commits into
mainfrom
fork-pr-57572-feat-channel-health-reconnect-mode

Conversation

@BingqingLyu

@BingqingLyu BingqingLyu commented Apr 27, 2026

Copy link
Copy Markdown
Owner

Summary

Adds a configurable channelHealthRestartMode option 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 → startChannel cycle. 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 restartMode that controls how the health-monitor handles channel recovery:

// Global default — applies to all channels
{
  "gateway": {
    "channelHealthRestartMode": "reconnect"  // "stop-start" | "reconnect" (default: "stop-start")
  }
}

// Per-channel override
{
  "channels": {
    "discord": { "healthMonitor": { "restartMode": "reconnect" } },
    "telegram": { "healthMonitor": { "restartMode": "reconnect" } }
  }
}
  • "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

File Change
src/config/types.channels.ts Add ChannelHealthRestartMode type and restartMode to ChannelHealthMonitorConfig
src/config/types.gateway.ts Add channelHealthRestartMode to GatewayConfig
src/gateway/channel-health-monitor.ts Add defaultRestartMode param; resolve effective mode per-channel in runCheck()
src/gateway/server-channels.ts Add getChannelHealthMonitorConfig() to ChannelManager
src/gateway/server.impl.ts Thread defaultRestartMode from config into health-monitor
src/gateway/server-reload-handlers.ts Thread defaultRestartMode through config-reload path

Backwards Compatibility

Default is "stop-start" — no behaviour change for existing installs. Opt-in to "reconnect" explicitly.

…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
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.

[Feature Request] health-monitor: channel-level reconnect should preserve Anthropic prompt cache

2 participants