Skip to content

fix(whatsapp): forward quotedMessageId/context to agent prompt - #28878

Closed
Shizoqua wants to merge 1 commit into
NousResearch:mainfrom
Shizoqua:fix/whatsapp-forward-quoted-message-context
Closed

fix(whatsapp): forward quotedMessageId/context to agent prompt#28878
Shizoqua wants to merge 1 commit into
NousResearch:mainfrom
Shizoqua:fix/whatsapp-forward-quoted-message-context

Conversation

@Shizoqua

Copy link
Copy Markdown
Contributor

The WhatsApp bridge (PR #25489) already extracts quotedMessageId, quotedParticipant, quotedRemoteJid, and hasQuotedMessage from Baileys' contextInfo, but the adapter in gateway/platforms/whatsapp.py only used quotedParticipant for admission control. The remaining fields were dropped, so reply-based corrections in WhatsApp lost all context — the agent only saw the new reply text with no reference to the quoted message.

This forwards the quoted-message context to the agent using the same shape as the Matrix adapter (see #27946 fix), keeping cross-platform behavior consistent. Non-reply messages are unchanged.

Fixes #28823

The WhatsApp bridge (PR NousResearch#25489) already extracts quotedMessageId,
quotedParticipant, quotedRemoteJid, and hasQuotedMessage from Baileys'
contextInfo, but the adapter in gateway/platforms/whatsapp.py only used
quotedParticipant for admission control. The remaining fields were dropped,
so reply-based corrections in WhatsApp lost all context — the agent only
saw the new reply text with no reference to the quoted message.

This forwards the quoted-message context to the agent using the same shape
as the Matrix adapter (see NousResearch#27946 fix), keeping cross-platform behavior
consistent. Non-reply messages are unchanged.

Fixes NousResearch#28823
@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 19, 2026

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on. The underlying bug is real on current main, but this patch only carries the quoted message id; the gateway prompt path still needs quoted text before the agent sees useful reply context.

Problems

  • gateway/platforms/whatsapp.py:1166-1174 on current main drops quoted metadata when constructing MessageEvent, so the premise is valid.
  • gateway/run.py:8048-8056 only injects reply context into the prompt when both event.reply_to_text and event.reply_to_message_id are set.
  • This PR's new test at tests/gateway/test_whatsapp_formatting.py:350-351 verifies reply_to_message_id == "ABC123" while reply_to_text is None, so the existing prompt injection guard would still skip the [Replying to: ...] prefix.

Suggested changes

  • Have scripts/whatsapp-bridge/bridge.js extract a quotedText value from Baileys contextInfo.quotedMessage and include it in the bridge payload.
  • Map that field to MessageEvent.reply_to_text in gateway/platforms/whatsapp.py.
  • Add a regression that runs through GatewayRunner._prepare_inbound_message_text and proves WhatsApp replies produce the visible [Replying to: "..."] prompt prefix.

Automated hermes-sweeper review.

message_id=data.get("messageId"),
media_urls=cached_urls,
media_types=media_types,
reply_to_message_id=reply_to_message_id,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting only reply_to_message_id does not make the quoted context visible to the agent: gateway/run.py injects the reply prefix only when reply_to_text is also present. This needs the bridge to send quoted text and the adapter to populate MessageEvent.reply_to_text as well.


assert event is not None
assert event.reply_to_message_id == "ABC123"
assert event.reply_to_text is None # bridge doesn't emit quoted text yet

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion locks in the incomplete behavior. A regression for the reported bug should prove the quoted body reaches the prompt, e.g. by feeding the event through _prepare_inbound_message_text and checking for the [Replying to: "..."] prefix.

@teknium1

Copy link
Copy Markdown
Contributor

Implemented on current main. This is an automated hermes-sweeper review.

  • 11627fdcb92a0ad57c9b637538f8fbea39142272 added the complete WhatsApp reply-context path, shipped in v2026.7.7.
  • scripts/whatsapp-bridge/bridge_helpers.js:313-317 extracts both quotedMessageId and quotedText from Baileys context.
  • plugins/platforms/whatsapp/adapter.py:1443-1455 and :1505-1517 forward both values through MessageEvent.
  • gateway/run.py:10592-10606 renders the structured reply context into the agent prompt.
  • tests/gateway/test_whatsapp_native_delivery.py:95-121 covers the adapter contract, including the quoted text required by the prompt renderer.

This also resolves the missing-quoted-text concern raised in the prior review and the adapter gap documented in #28823.

@teknium1 teknium1 closed this Jul 13, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jul 13, 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 sweeper:implemented-on-main Sweeper: behavior already present on current main type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: WhatsApp: quotedMessageId/context is not forwarded to agent — reply context is lost

4 participants