#4181: fix(whatsapp): resolve Brazilian mobile JID variants (8/9 digit)
channel: whatsapp-web
Cluster:
WhatsApp Enhancements and Fixes
## Summary
Brazilian mobile numbers transitioned from 8 to 9 digits (adding a leading `9`) around 2012-2016. WhatsApp accounts created before the migration may still be registered internally with the **old 8-digit format**, causing silent message delivery failures.
## Changes
### `brazil-jid.ts` (new file)
Resolver module that:
- Detects Brazilian mobile numbers (`55` + 2-digit area code + 8/9 digit local)
- Generates both 8-digit and 9-digit variants
- Uses `sock.onWhatsApp()` to find which format is actually registered
- Caches verified JIDs for 7 days to avoid repeated lookups
### `send-api.ts`
- Optionally accepts `onWhatsApp` in the sock params
- Calls the resolver before sending messages, polls, reactions, and presence updates
- Falls back gracefully if `onWhatsApp` is not available
### `monitor.ts`
- Passes `onWhatsApp` to `createWebSendApi` so the resolver can query WhatsApp
## Example
| Input Number | Variants Checked | Resolution |
|-------------|-----------------|------------|
| +5511**9**99998888 | `5511999998888`, `551199998888` | ✅ Queries find correct one |
## Testing
- Tested locally with Brazilian numbers in both formats
- Confirmed message delivery to legacy 8-digit account
- Cache stored at `~/.config/clawdbot/brazil-jid-cache.json`
## Logs
```
[brazil-jid] Checking variants for 5511999998888: [ '5511999998888', '551199998888' ]
[brazil-jid] Found: 5511999998888 → 551199998888@s.whatsapp.net
```
Fixes #4168
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adds a Brazilian WhatsApp JID resolver to handle legacy 8-digit vs modern 9-digit mobile numbers by generating both variants, probing `sock.onWhatsApp()`, and caching the resolved JID to disk. It wires the resolver into the outbound web send API so messages/polls/reactions/presence updates resolve the correct JID before sending, and passes `onWhatsApp` through from the monitor socket wrapper.
Overall the approach fits the existing `createWebSendApi` abstraction by keeping the resolution local to outbound sends and falling back when `onWhatsApp` is unavailable. The main concerns are around correctness of the `onWhatsApp()` response handling and operational considerations of a per-user JSON cache written directly to a fixed config path.
<h3>Confidence Score: 3/5</h3>
- This PR is likely safe to merge, but has a couple correctness/operational edge cases worth addressing.
- The change is localized to outbound WhatsApp send paths and is guarded when `onWhatsApp` isn’t available. However, the resolver’s assumption about `onWhatsApp()` result ordering can cause missed resolutions, and the new on-disk JSON cache introduces cross-platform path/atomic-write concerns that could lead to surprising behavior or cache corruption in multi-process use.
- src/web/inbound/brazil-jid.ts
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#19590: fix(whatsapp): normalize Brazilian mobile numbers in JID conversion
by qualiobra · 2026-02-18
83.7%
#2772: fix: search WhatsApp account subdirs for LID mapping files
by impozzible · 2026-01-27
77.7%
#20190: WhatsApp: normalize BR 9th-digit outbound targets
by saurabhchopade · 2026-02-18
77.4%
#23059: fix(whatsapp): resolve LID JIDs for reply-to-bot implicit mention d...
by azisseno · 2026-02-21
77.3%
#16608: feat(whatsapp): resolve outbound @mentions to clickable WhatsApp me...
by lucasmpramos · 2026-02-14
77.0%
#15786: fix: strip device suffix from selfJid in WhatsApp mention matching
by kenken64 · 2026-02-13
76.4%
#10196: fix(whatsapp): sanitize raw mention IDs in outbound messages
by koala73 · 2026-02-06
75.3%
#16655: fix(whatsapp): resolve reply-to sender E.164 for LID JIDs (have bot...
by mascarenhas · 2026-02-15
74.9%
#8052: fix(whatsapp): strip leading whitespace from outbound messages
by FelixFoster · 2026-02-03
74.3%
#5665: fix: match group JIDs in groupAllowFrom allowlist
by koala73 · 2026-01-31
74.1%