fix: [WIDGET-WORKS] Fix LID messages not syncing to Chatwoot during historical sync #18
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
Fixes LID (Linked Identity Device) messages not syncing to Chatwoot during historical sync (
syncFullHistoryfeature). During historical sync, Baileys provides stripped message keys without theremoteJidAltfield, preventing LID→phone resolution that works correctly in real-time message processing.This fix adds batch LID resolution using the
IsOnWhatsapptable before processing messages in themessages.sethandler, ensuring historical LID messages are properly imported to Chatwoot.Problem Statement
Impact
Root Cause
File:
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:1045During historical sync (
messages.setevent), Baileys provides stripped message keys containing only{id, fromMe, remoteJid}. Critical fields are undefined:remoteJidAlt- not available (required for LID resolution)senderPn- not availableaddressingMode- not availableWhy real-time works but historical doesn't:
remoteJidAltavailable?messages.upsert)messages.set)A prior fix attempt (commit
630f5c56, July 2025) tried usingsenderPn, but this field is not available in stripped keys during historical sync.Solution
Implementation
Part 1: Batch LID Resolution ✅ Implemented
Added LID→phone resolution in
messages.sethandler before message processing:@lidremoteJids from the message batchIsOnWhatsapptable with OR conditions onjidOptionsfieldMap<lidJid, phoneJid>for O(1) lookupremoteJidwith phone number where mapping existsPart 2: Self-Healing ✅ Already exists
Existing code at
chatwoot.service.ts:644-666automatically updates LID contacts with real phone numbers or merges contacts when real-time messages arrive.Feature Flag
CHATWOOT_LID_HISTORICAL_SYNC_ENABLEDtrue(enabled by default, opt-out with=false)Changes
Files Modified (3 files, 75 insertions)
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts(+68 lines)messages.setprocessing loop (line 1045)[WIDGET-WORKS]labeling with context commentssrc/config/env.config.ts(+6 lines)LID_HISTORICAL_SYNC_ENABLEDtoChatwoottype definitiontrue[WIDGET-WORKS]comments for upstream merge trackingCHANGELOG.md(+1 line)Testing Plan
Pre-Deployment Verification
Post-Deployment Testing
Phase 1: Single Test Instance
LOG_BAILEYS=verbosemessages.seteventPhase 2: Production Rollout
7. If test successful, deploy to remaining 7 instances
8. Monitor for any errors or unexpected behavior
9. Verify historical sync no longer misses LID messages
Rollback Plan
# Disable feature if issues arise CHATWOOT_LID_HISTORICAL_SYNC_ENABLED=falseUpstream Compatibility
Research Summary
[WIDGET-WORKS]labelingRelated Upstream Changes
Verification
Manual Backfill Results (Completed 2026-01-30)
This fix prevents the issue from recurring on future reconnects.
Additional Context
docs/troubleshooting/30-01-26-chatwoot-syncfullhistory-lid-missing.mdscripts/backfill-lid-to-chatwoot.ts(already executed)Reviewers: Please verify: