Repository navigation
fix: resolve WhatsApp phone-JID/LID alias when looking up handover state - #5
Conversation
Handover rows are keyed by `(platform, chat_id)` and the WhatsApp bridge surfaces the same human under either a phone-JID (`60173380115@s.whatsapp.net`) or a LID (`122299244130458@lid`) depending on protocol negotiation. A row stored under one form was silently invisible to lookups using the other, so the bot bypassed an active handover whenever the bridge flipped variants — which is how Kong got two "the handover is active" replies from the bot mid-handover. The keying mismatch is fixed by walking `gateway.whatsapp_identity.expand_whatsapp_aliases` (the same helper Hermes core uses for session keys), enumerating every JID variant for the inbound chat_id, and probing all of them in `is_active` / `/takeback` / owner-extend paths. Subsequent writes pin to the row's stored chat_id so we don't fragment state across variants. The `trigger_handover` tool also reuses an existing alias row instead of creating a sibling under the inbound form.
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
WalkthroughThe changes introduce WhatsApp chat ID alias resolution to the handover system. Instead of a simple platform+chat_id lookup, the system now expands a chat ID into multiple alias forms (numeric, numeric@s.whatsapp.net, numeric@lid) and searches for an active handover across all forms. Upon finding an active handover, the system uses the persisted chat ID from that row for all subsequent operations, preventing state fragmentation. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant HandoverRule as Handover Rule
participant State
participant Storage as HandoverStore
participant TriggerHandler as Trigger Handler
Client->>HandoverRule: Inbound message (chat_id alias variant)
HandoverRule->>State: alias_chat_ids(platform, chat_id)
State-->>HandoverRule: [numeric, numeric@s.whatsapp.net, numeric@lid]
HandoverRule->>Storage: find_active(platform, candidates)
Storage->>Storage: Search for active row across aliases
Storage->>Storage: Deactivate expired matches lazily
Storage-->>HandoverRule: HandoverRow (with canonical chat_id)
HandoverRule->>HandoverRule: Set is_active, stored_chat_id
rect rgba(100, 150, 200, 0.5)
Note over HandoverRule,Storage: Deactivation & Extension Operations
HandoverRule->>Storage: Deactivate/extend using stored_chat_id
Storage-->>HandoverRule: Success
end
HandoverRule-->>Client: Response (using canonical chat_id)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Summary
(platform, chat_id)and the WhatsApp bridge can surface the same human under either a phone JID (60173380115@s.whatsapp.net) or a LID (122299244130458@lid). A row stored under one form was invisible to lookups using the other.is_activereturnedFalseand the bot dispatched.gateway.whatsapp_identity.expand_whatsapp_aliases(the same helper core uses for session keys), enumerate every JID variant, and probe all of them. Pin subsequent writes to the row's storedchat_idso state never fragments across variants. Thetrigger_handovertool also reuses an existing alias row instead of creating a sibling.Root cause
Live SQLite row at the time of incident:
Inbound
gateway.loglines for the same chat at 16:30/16:31:60173380115and122299244130458are the two forms of the same chat (verified viawhatsapp/session/lid-mapping-122299244130458_reverse.json→60173380115). Pre-fixis_activekeyed by the LID form missed the row. Nogateway-policyskip log was emitted for either inbound — confirms the rule returnedNoneand the bot ran.Test plan
python -m pytest tests/ -o 'addopts=' -q(48 passed, +4 new tests covering LID→phone lookup, /takeback across alias, owner-extend across alias,trigger_handoverreusing an alias row)profiles/basketball-jersey-demo/plugins/gateway_policy/and restarted gatewaySummary by CodeRabbit
Release Notes
Bug Fixes
Tests