feat(migrate-v2): resolve WhatsApp LIDs from store/auth, alias DMs

v1 stored every WhatsApp DM as `<phone>@s.whatsapp.net`. v2's WA
adapter sometimes resolves the chat to `<lid>@lid` instead — when
WhatsApp delivers via the LID protocol and Baileys hasn't yet learned
a LID→phone mapping for that contact (cold cache after migration).
The router then can't find the phone-keyed messaging_group and
silently drops the message at router.ts:184.

Baileys persists every LID↔phone pair it has ever learned to disk as
`store/auth/lid-mapping-<phone>.json` (forward) and
`lid-mapping-<lid>_reverse.json` (reverse). v1 will already have these
populated for every contact it has talked to. New step 2d-whatsapp-lids
parses the reverse files and writes paired LID-keyed `messaging_groups`
+ `messaging_group_agents` rows so both `<phone>@s.whatsapp.net` and
`<lid>@lid` route to the same agent_group with the same engage rules.

No Baileys boot, no WhatsApp connectivity required — pure filesystem
read of files we've already copied via 2b-channel-auth. Step is
no-op-on-skip if either store/auth or whatsapp DM rows are missing.

Anything that slips through (a contact whose LID v1 never learned)
falls back to the runtime approval flow once the WA adapter sets
isMention=true on DMs — each unknown LID DM auto-creates an
approval-required messaging_group and the owner gets a one-tap
register prompt.

Verified end-to-end on a 12-group v1 install: 3 DM rows aliased,
inbound DM routed via the LID-keyed row.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Gavriel Cohen
2026-05-02 15:39:36 +03:00
parent 2a915e8af0
commit dca02f5453
2 changed files with 207 additions and 0 deletions
+15
View File
@@ -392,6 +392,21 @@ else
record_step "$STEP_NAME" "failed"
fi
done
# 2d. WhatsApp LID resolution. After whatsapp is installed (so Baileys
# is on disk) and auth files have been copied (so we can connect with
# the migrated identity), boot Baileys briefly to learn LID↔phone
# mappings during initial sync, then write paired LID-keyed
# messaging_groups. Best-effort: any failure degrades to runtime
# approval flow, which the WA adapter's isMention=true on DMs handles.
for ch in "${SELECTED_CHANNELS[@]}"; do
if [ "$ch" = "whatsapp" ]; then
run_step "2d-whatsapp-lids" \
"Resolve WhatsApp LIDs for migrated DMs" \
"setup/migrate-v2/whatsapp-resolve-lids.ts"
break
fi
done
fi
echo