chatwoot/app/builders
Shivam Mishra 379e28df1f
fix: prevent bot metrics double-counting when handoff and resolution coexist [CW-6210] (#14032)
The bot metrics dashboard can show `handoff_rate + resolution_rate >
100%`. A single conversation can accumulate both
`conversation_bot_handoff` and `conversation_bot_resolved` events, and
the rate queries count them independently against a shared denominator.

## How it happens

```
Customer messages bot inbox
        │
        ▼
   ┌──────────┐
   │ pending  │ (bot handling)
   └────┬─────┘
        │ bot can't help
        ▼
   ┌──────────┐
   │   open   │ (handed off → conversation_bot_handoff event created)
   └────┬─────┘
        │ agent clicks "Resolve" WITHOUT sending a message
        ▼
   ┌──────────┐
   │ resolved │ conversation_resolved fires
   └──────────┘
        │
        ▼
   create_bot_resolved_event guard checks:
      inbox.active_bot?
      no outgoing messages with sender_type: 'User'  ← agent never messaged!
        │
        ▼
   conversation_bot_resolved event ALSO created ← BUG
        │
        ▼
   Same conversation counted in BOTH rates → sum exceeds 100%
```

## Why fix at the read path, not the write path

An earlier attempt added guards in the listener to make the two events
mutually exclusive per conversation — deleting `bot_resolved` when a
handoff fires, suppressing resolutions when a handoff exists. This was
rejected because conversations can be reopened across multiple cycles
(bot resolves on day 1, customer returns on day 5, bot hands off).
Deleting the day-1 resolution corrupts historical reports, and the async
event dispatcher makes listener-level guards vulnerable to race
conditions.

## What this PR does

Within a reporting window, if a conversation has both events, **handoff
wins** — the conversation is excluded from the resolution count. This is
applied via SQL subquery across all three read paths:

```
                    ┌─────────────────────────┐
                    │   Reporting Events DB    │
                    │                          │
                    │  conv_bot_handoff: [A,B] │
                    │  conv_bot_resolved: [A,C]│
                    └────────┬────────────────┘
                             │
              ┌──────────────┼──────────────┐
              ▼              ▼              ▼
       BotMetricsBuilder  ReportHelper  CountReportBuilder
       (rate cards)       (bot_summary)  (timeseries charts)
              │              │              │
              ▼              ▼              ▼
       resolutions:        resolutions:   resolutions:
       [A,C] minus [A,B]  same logic     same logic
       = [C] only          = [C] only     = [C] only

       Result: Conversation A → handoff only
               Conversation B → handoff only
               Conversation C → resolution only
```

For wide date ranges spanning multiple lifecycles, a conversation
bot-resolved in one cycle and handed off in a later cycle will only show
as a handoff. This is an acceptable tradeoff — the alternative (>100%
rates) is clearly worse, and narrow ranges handle this correctly since
the events fall into different windows. No reporting events are
modified, so historical data stays intact.

## Diagnostic tool

`rake bot_metrics:diagnose` — read-only task that prompts for account ID
and date range, shows a before/after rate comparison without modifying
data.

---------

Co-authored-by: aakashb95 <aakashbakhle@gmail.com>
Co-authored-by: Aakash Bakhle <48802744+aakashb95@users.noreply.github.com>
2026-05-13 18:43:23 +05:30
..
campaigns fix: correct typo in CampaignConversationBuilder (#11336) 2025-04-21 11:57:38 +05:30
csat_surveys feat: Add CSAT response APIs (#2503) 2021-06-29 20:59:41 +05:30
email fix: sanitize parentheses from email From header to prevent SMTP 553 errors (#14075) 2026-04-21 11:30:20 +05:30
messages feat(voice): add WhatsApp inbound call webhook pipeline [3] (#14315) 2026-05-12 11:23:57 +05:30
v2 fix: prevent bot metrics double-counting when handoff and resolution coexist [CW-6210] (#14032) 2026-05-13 18:43:23 +05:30
account_builder.rb feat: onboarding account details with enriched data [UPM-17][UPM-18] (#13979) 2026-04-28 10:35:51 +05:30
agent_builder.rb fix: Add validation to the name attribute in user (#10805) 2026-04-27 15:47:11 +05:30
contact_inbox_builder.rb feat: outbound voice call essentials (#12782) 2025-11-24 17:47:00 -08:00
contact_inbox_with_contact_builder.rb feat: Instagram Inbox using Instagram Business Login (#11054) 2025-04-08 10:47:41 +05:30
conversation_builder.rb feat: Ability to lock to single conversation (#5881) 2022-11-25 13:01:04 +03:00
notification_builder.rb fix(notifications): Respect conversation access when notifying agents (#14412) 2026-05-12 10:57:29 +04:00
notification_subscription_builder.rb chore: Use "create!" and "save!" bang methods when not checking the result (#5358) 2022-09-13 17:40:06 +05:30
year_in_review_builder.rb feat(ce): Add Year in review feature (#13078) 2025-12-15 17:24:45 -08:00