Skip to content

fix(whatsapp): match reply-to-bot when botIds carry device suffix#29049

Closed
luyao618 wants to merge 1 commit into
NousResearch:mainfrom
luyao618:fix/whatsapp-reply-to-bot-device-suffix
Closed

fix(whatsapp): match reply-to-bot when botIds carry device suffix#29049
luyao618 wants to merge 1 commit into
NousResearch:mainfrom
luyao618:fix/whatsapp-reply-to-bot-device-suffix

Conversation

@luyao618

Copy link
Copy Markdown
Contributor

Summary

Fixes #29023. WhatsApp reply-to-bot detection (_message_is_reply_to_bot) consistently returned False on multi-device accounts because of a normalized-ID shape mismatch:

  • Baileys delivers quotedParticipant without the device suffix: "5511999999999@s.whatsapp.net"
  • sock.user.{id,lid} populates botIds with the device suffix (:N → normalized to @N): "5511999999999@10@s.whatsapp.net"

The set-membership check in gateway/platforms/whatsapp.py therefore never matched, breaking the reply-to-bot admission shortcut in groups configured with require_mention: true. This is especially painful for voice notes, where WhatsApp doesn't support text @mentions at all — there's literally no other way to wake the bot.

Fix

In _bot_ids_from_message, when a normalized bot id has three @-separated parts (i.e. carries a device suffix), add a suffix-stripped alias alongside the original. Legacy two-part ids are unchanged, so single-device accounts keep their existing behavior.

parts = normalized.split("@")
if len(parts) > 2:
    bot_ids.add(f"{parts[0]}@{parts[-1]}")

This is exactly the patch the issue author proposed in the Fix section.

Tests

Added two regression tests in tests/gateway/test_whatsapp_group_gating.py:

  • test_reply_to_bot_matches_device_suffixed_bot_ids — multi-device botIds (:10 suffix) + non-suffixed quotedParticipant must match and admit the message under require_mention=true.
  • test_reply_to_bot_legacy_non_multidevice_unchanged — legacy two-part ids continue to match as before.
$ python -m pytest tests/gateway/test_whatsapp_group_gating.py -q
28 passed in 2.52s

Risk

Tiny, surgical, single-function change. The added alias is only emitted when the id has a third @-separated component, so it cannot affect legacy callers. No other code paths consume _bot_ids_from_message in a way that would break on the extra alias (the other callers use it for split('@', 1)[0] bare-id extraction and in-checks against mentionedIds).

Baileys delivers quotedParticipant without the multi-device suffix
(e.g. "5511999999999@s.whatsapp.net"), but sock.user.{id,lid} in
botIds carries one (e.g. "5511999999999@10@s.whatsapp.net"). The
strict set-membership check in _message_is_reply_to_bot therefore
always returned False on multi-device WhatsApp accounts, making the
reply-to-bot admission shortcut unusable for groups with
require_mention=true. This is especially bad for voice notes, which
cannot include text @mentions at all.

Expose a suffix-stripped alias alongside the original normalized id
so reply-to-bot detection succeeds for both multi-device and legacy
single-device accounts.

Fixes NousResearch#29023
@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery platform/whatsapp WhatsApp Business adapter P2 Medium — degraded but workaround exists labels May 20, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related: fixes #29023. See also PRs #11469 and #7323 (same WhatsApp device-suffix JID mismatch root cause, earlier approaches).

@luyao618

Copy link
Copy Markdown
Contributor Author

Closing — the failing test check is unrelated infrastructure drift (plugin registry snapshot expecting no xai, and an unrelated stdout-wrapper assertion in hermes_cli). Will reopen with a fresh branch once main stabilizes if the WhatsApp device-suffix bug is still reproducible.

@luyao618 luyao618 closed this May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/whatsapp WhatsApp Business adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: WhatsApp reply-to-bot detection fails — device suffix mismatch in botIds vs quotedParticipant

2 participants