fix(heartbeat): normalize system event session keys to lowercase#178
Open
BingqingLyu wants to merge 1 commit into
Open
fix(heartbeat): normalize system event session keys to lowercase#178BingqingLyu wants to merge 1 commit into
BingqingLyu wants to merge 1 commit into
Conversation
System events use an in-memory map keyed by session key. The requireSessionKey() helper only trimmed the key without case normalization, but session keys are canonicalized to lowercase by parseAgentSessionKey() throughout the rest of the codebase. When a plugin (e.g. Hermes) enqueues a system event with a mixed-case session key like `agent:main:slack:channel:C0AKA9RBSAU`, the event is stored under the mixed-case key. Later, when the heartbeat runner peeks or drains events using the canonical lowercase key, the lookup misses the event entirely. The heartbeat still fires but sees no pending events, so the model responds with HEARTBEAT_OK and the turn is pruned — making it appear as if no heartbeat ran at all. Main/thread sessions are unaffected because their keys (`agent:main:main`, `agent:main:main:thread:*`) are already lowercase. Channel sessions with platform-specific IDs (Slack C0XX, Discord guild IDs, etc.) are the ones that hit this mismatch. Fix: lowercase the key in requireSessionKey() so all enqueue/peek/drain operations use the same canonical form, matching the rest of the session key infrastructure. Closes openclaw#34338
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
requestHeartbeatNow({ sessionKey })silently produces no visible output for channel sessions (agent:main:slack:channel:*) while working correctly for main/thread sessions.requestHeartbeatNowto wake channel sessions cannot reliably deliver system events — the events sit in the queue indefinitely and the heartbeat turn is pruned as if nothing happened.requireSessionKey()insystem-events.tsnow lowercases the key, matching the canonical form used byparseAgentSessionKey()throughout the codebase. Two new tests verify cross-case enqueue/peek and deduplication.Change Type
Scope
Linked Issue/PR
Closes openclaw#34338
User-visible / Behavior Changes
System events enqueued with mixed-case session keys (e.g., Slack channel IDs like
C0AKA9RBSAU) are now correctly found by the heartbeat runner's peek/drain calls. Previously, the heartbeat runner normalized session keys to lowercase viaparseAgentSessionKey()but the system events queue stored them verbatim — causing a key mismatch for any session key containing uppercase characters.Before: Plugin enqueues event → heartbeat fires → event not found (case mismatch) → model responds HEARTBEAT_OK → turn pruned → appears as "no heartbeat turn ran"
After: Plugin enqueues event → key normalized to lowercase → heartbeat finds event → model acts on it → visible output
Security Impact
Repro + Verification
Environment: Any setup with a plugin calling
requestHeartbeatNowfor a channel session.Steps to reproduce (before fix):
enqueueSystemEvent("test", { sessionKey: "agent:main:slack:channel:C0XX" })peekSystemEvents("agent:main:slack:channel:c0xx")[]— event is invisibleExpected: Event should be found regardless of key casing.
Actual (after fix):
peekSystemEvents("agent:main:slack:channel:c0xx")returns["test"]✅Evidence
New tests added to
src/infra/system-events.test.ts:normalizes session key case so channel sessions match— enqueues with uppercase Slack ID, peeks with lowercase, verifies matchdeduplicates across case variants of the same session key— enqueues from two case variants, verifies both land in the same queueAll 34 tests pass across system-events, heartbeat-runner scheduler, and heartbeat-wake.
Human Verification
requestHeartbeatNow()→ heartbeat-wake → heartbeat-runner →runHeartbeatOnce()→resolveHeartbeatSession()→peekSystemEventEntries()→getReplyFromConfig()→drainSystemEventEntries()parseAgentSessionKey()(used byresolveHeartbeatSession) lowercases all keys, whilerequireSessionKey()(used by system events) did notCompatibility / Migration
Fully backward compatible. The only behavioral change is that system event keys are now case-insensitive. Any code already passing lowercase keys sees no change. Code passing mixed-case keys now works correctly instead of silently failing.
Failure Recovery
One-line revert in
requireSessionKey()(changereturn trimmed.toLowerCase()back toreturn trimmed). No data migration — system events are ephemeral (in-memory only).Risks and Mitigations
.toLowerCase()on every system event call🤖 Generated with Claude Code · AI-assisted: fully tested with existing + new unit tests, code traced end-to-end through the heartbeat pipeline.