Skip to content

Re-request undecryptable messages from the phone instead of dropping them - #156

Open
6justgotme wants to merge 1 commit into
evolution-foundation:mainfrom
6justgotme:fix/rerequest-from-phone
Open

Re-request undecryptable messages from the phone instead of dropping them#156
6justgotme wants to merge 1 commit into
evolution-foundation:mainfrom
6justgotme:fix/rerequest-from-phone

Conversation

@6justgotme

@6justgotme 6justgotme commented Aug 3, 2026

Copy link
Copy Markdown

Problem

When a message can't be decrypted, whatsmeow emits events.UndecryptableMessage. In pkg/whatsmeow/service/whatsmeow.go the handler for that event only forwards a webhook when it's a view_once message (and reconnects for IDs starting with 66/67). Every other undecryptable message is logged and dropped — no webhook, and nothing is done to recover it.

Where this bites in practice is the first message from a brand-new contact. A new contact has no Signal session yet, so the first inbound message can fail to decrypt on the linked device; when it does, it's silently lost. The rest of the conversation decrypts fine once the session is established, so from the outside it looks like "the first message never arrives." Click-to-WhatsApp ad leads make this very visible, since every lead is a first-contact by definition.

Cause

Evolution Go runs as a companion/linked device, so it has no local history to fall back on. whatsmeow already handles this case: if the sender doesn't resend within RequestFromPhoneDelay (~5s), it can re-request the message from the account's own phone (the primary device, which already decrypted it). That path is gated behind Client.AutomaticMessageRerequestFromPhone, and the service never enables it — so the fallback never runs and the message stays lost.

Fix

Enable whatsmeow's AutomaticMessageRerequestFromPhone in StartClient, gated behind a new REREQUEST_FROM_PHONE env var so it's opt-in per deployment (default off):

client.AutomaticMessageRerequestFromPhone = w.config.RerequestFromPhone

With REREQUEST_FROM_PHONE=true, an undecryptable message gets re-requested from the phone and comes back through as a normal events.Message, so the webhook fires as expected.

Notes

  • Off by default — no behavior change unless an operator sets REREQUEST_FROM_PHONE=true.
  • Recovered messages arrive with up to ~5s of extra latency (the delay window before falling back to the phone). Fine trade-off versus losing them.
  • No change for messages that decrypt normally.
  • Requires the account's phone to be reachable to answer the re-request, which is the normal case.

@sourcery-ai

sourcery-ai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR enables automatic re-requesting of undecryptable messages from the primary phone so that previously dropped first-contact messages are recovered and delivered as normal events.

Sequence diagram for automatic rerequest of undecryptable messages

sequenceDiagram
  actor Sender
  participant WhatsAppServer
  participant PrimaryPhone
  participant LinkedDevice
  participant WebhookEndpoint

  Sender->>WhatsAppServer: send_message
  WhatsAppServer->>PrimaryPhone: deliver_message
  PrimaryPhone->>WhatsAppServer: sync_decrypted_message
  WhatsAppServer->>LinkedDevice: deliver_message
  LinkedDevice->>LinkedDevice: events.UndecryptableMessage

  alt [AutomaticMessageRerequestFromPhone enabled]
    LinkedDevice->>PrimaryPhone: AutomaticMessageRerequestFromPhone
    PrimaryPhone->>WhatsAppServer: resend_message
    WhatsAppServer->>LinkedDevice: events.Message
    LinkedDevice->>WebhookEndpoint: webhook_POST
  else [AutomaticMessageRerequestFromPhone disabled]
    LinkedDevice->>LinkedDevice: log_and_drop_message
  end
Loading

File-Level Changes

Change Details Files
Enable automatic re-request of undecryptable messages from the primary device instead of silently dropping them.
  • Set the WhatsApp client option AutomaticMessageRerequestFromPhone to true during client initialization.
  • Document in comments that undecryptable messages (including first CTWA lead messages) are now recovered via re-request from the phone after RequestFromPhoneDelay instead of being discarded without a webhook.
pkg/whatsmeow/service/whatsmeow.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider making AutomaticMessageRerequestFromPhone configurable via ClientData or service options instead of hardcoding it to true, so operators can explicitly opt in or out of the rerequest behavior per deployment.
  • The new inline comment is in Portuguese; for consistency across the codebase, consider rewriting it in the predominant project language and briefly summarizing the behavior change rather than its full rationale.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider making `AutomaticMessageRerequestFromPhone` configurable via `ClientData` or service options instead of hardcoding it to true, so operators can explicitly opt in or out of the rerequest behavior per deployment.
- The new inline comment is in Portuguese; for consistency across the codebase, consider rewriting it in the predominant project language and briefly summarizing the behavior change rather than its full rationale.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

…Phone

Companion/multi-device deployments drop the first message from new contacts when it fails to decrypt: whatsmeow emits UndecryptableMessage and the event handler skips the webhook (only view_once / ID 66-67 are handled), with no retry, so the message is lost.

Enable whatsmeow's AutomaticMessageRerequestFromPhone so undecryptable messages are re-requested from the phone and re-dispatched as normal Message events. Gated behind a new REREQUEST_FROM_PHONE env var (opt-in, default off) so operators choose per deployment.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@6justgotme
6justgotme force-pushed the fix/rerequest-from-phone branch from ea392fc to 3f84b3c Compare August 4, 2026 01:58
@6justgotme

Copy link
Copy Markdown
Author

Thanks for the review — addressed both points and pushed as 3f84b3c:

  • Configurable instead of hardcoded: it's now behind a REREQUEST_FROM_PHONE env var, read in config.Load and applied in StartClient (client.AutomaticMessageRerequestFromPhone = w.config.RerequestFromPhone). Default off, so behavior is unchanged unless an operator opts in.
  • Comment language: rewrote the inline comment in English and trimmed it to what the line does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant