← Back to Pull requests

[18.0][FIX] mail_gateway: cache gateway map by state and type #1950

Closed
unknown closed this pull request without merging on Nov 03, 2026 17:19 UTC
View on GitHub
unknown unknown commented on Oct 30, 2026 14:00 UTC
Author

Problem

The webhook controller finds the gateway of each request with mail.gateway._get_gateway(key, state, gateway_type), which reads the map {webhook_key: gateway data} built by _get_gateway_map(state, gateway_type):

  • Verification requests (GET) look for the pending gateways.

  • Messages (POST) look for the integrated gateways.
  • _get_gateway_map() is cached with @tools.ormcache() without keys, so its arguments are not part of the cache key: all the calls share one cache entry, and the map computed first is returned for any state and gateway_type until the cache is cleared.

    Steps to reproduce

    1. Have an integrated WhatsApp gateway.
    2. Send a GET to its webhook URL (open it in a browser, or a verification with a wrong token). The lookup of the pending gateways caches an empty map.
    3. Receive a WhatsApp message. The lookup of the integrated gateways gets the cached empty map: "Gateway was not found for token ...", and the message is dropped. Meta gets a 200 answer, so it does not send it again.
    4. Every following message is dropped too, until the cache is cleared (restart, or a change of a gateway).

    The same happens with gateways of several types: with Telegram and WhatsApp, the first one that receives a webhook caches the map of its type and the others are not found.

    Change

    -    @tools.ormcache()
    +    @tools.ormcache("state", "gateway_type")
         def _get_gateway_map(self, state="integrated", gateway_type=False):
    

    state and gateway_type are now part of the cache key, so each combination has its own cached map (pending WhatsApp, integrated WhatsApp, integrated Telegram...) and every lookup gets the gateways it asked for.

    Nothing else changes: the maps are still cleared when a gateway is created or its webhook key, state, secret or user changes, and there are only a few combinations, so the cache stays as small and fast as before.

    Tests

    test_gateway_map_by_state (mail_gateway_whatsapp) looks up the pending gateways first and then the integrated one. It fails without the fix and passes with it.

    @BinhexTeam

    Diff: 263 lines (+197 / -66) across 3 files Reviewer: etobella Review duration: 5959m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.39) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 263 lines diff across 3 files
    0.39
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.334
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1950"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.3% (MEDIUM) · Higher = larger blast radius
    39.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.5% · Safe to merge
    15.5%
    Repository