iachat/spec/enterprise/models
Shivam Mishra 566de02385
feat: allow agent bot and captain responses to reset waiting since (#13181)
When AgentBot responds to customer messages, the `waiting_since`
timestamp is not reset, causing inflated reply time metrics when a human
agent eventually responds. This results in inaccurate reporting that
incorrectly includes periods when customers were satisfied with bot
responses.

### Timeline from Production Data

```
Dec 12, 16:20:14 - Customer sends message (ID: 368451924)
                   ↓ waiting_since = Dec 12, 16:20:14

Dec 12, 16:20:17 - AgentBot replies (ID: 368451960)
                   ↓ waiting_since STILL = Dec 12, 16:20:14 
                   ↓ (Bot response doesn't clear it)

14-day gap        - Customer satisfied, no messages
                   ↓ waiting_since STILL = Dec 12, 16:20:14 

Dec 26, 22:25:45 - Customer sends new message (ID: 383522275)
                   ↓ waiting_since STILL = Dec 12, 16:20:14 
                   ↓ (New message doesn't reset it)

Dec 26-27         - More AgentBot interactions
                   ↓ waiting_since STILL = Dec 12, 16:20:14 

Dec 27, 07:36:53 - Human agent finally replies (ID: 383799517)
                   ↓ Reply time calculated: 1,268,404 seconds
                   ↓ = 14.7 DAYS 
```
## Root Cause

The core issues is in `app/models/message.rb`, where **AgentBot messages
does not clear `waiting_since`** - The `human_response?` method only
returns true for `User` senders, so bot replies never trigger the
clearing logic. This means once `waiting_since` is set, it stays set
even when customers send new messages after receiving bot responses.

The solution is to simply reset `waiting_since` **after a bot has
responded**. This ensures reply time metrics reflect actual human agent
response times, not bot-handled periods.

### What triggers the rest

This is an intentional "gotcha", that only `AgentBot` and
`Captain::Assistant` messages trigger the waiting time reset. Automation
and campaign messages maintain current behavior (no reset). This is
because interactive bot assistants provide conversational help that
might satisfy customers. Automation and campaigns are one-way
communications and shouldn't affect waiting time calculations.

## Related Work

Extends PR #11787 which fixed `waiting_since` clearing on conversation
resolution. This PR addresses the bot interaction scenario which was not
covered by that fix.

Scripts to clean data:
https://gist.github.com/scmmishra/bd133208e219d0ab52fbfdf03036c48a
2026-01-07 13:57:43 +05:30
..
captain feat(enterprise): add voice conference API (#13064) 2025-12-15 15:11:59 -08:00
channel feat(voice): Improved voice call creation flow [EE] (#12268) 2025-08-22 13:38:23 +02:00
concerns feat(ee): Captain custom http tools (#12584) 2025-10-06 07:53:15 -07:00
enterprise/concerns chore: Automate SSL with Cloudflare (#12021) 2025-07-24 13:09:06 +04:00
account_saml_settings_spec.rb feat: update users on SAML setup and destroy [CW-2958][CW-5612] (#12346) 2025-09-15 21:20:22 +05:30
account_spec.rb chore: Update account deletion email copy (#12317) 2025-08-31 16:01:41 +02:00
account_user_spec.rb feat: Add APIs to manage custom roles in Chatwoot (#9995) 2024-08-23 17:18:28 +05:30
agent_capacity_policy_spec.rb feat: Add agent capacity controllers (#12200) 2025-08-26 19:12:58 -07:00
applied_sla_spec.rb feat: Conversation API to return applied_sla and sla_events (#9174) 2024-04-01 23:30:07 +05:30
assignment_policy_spec.rb feat: Add assignment policies controllers with jbuilder views (#12199) 2025-08-18 19:15:21 -07:00
automation_rule_spec.rb feat: sla-2 add automation backend support for SLA (#8775) 2024-02-01 15:42:12 +05:30
company_spec.rb feat: Add company model and API with tests (#12548) 2025-10-08 07:53:43 -07:00
contact_company_association_spec.rb feat: Add company auto-association for contacts (CW-5726 Part 2) (#12711) 2025-11-03 20:36:13 +05:30
conversation_spec.rb fix: Do not enforce max_limits if inbox_auto_assignment is disabled (#11849) 2025-07-01 15:10:39 -07:00
copilot_message_spec.rb fix: Update specs, add background response job implementation for copilot threads (#11600) 2025-05-27 14:10:27 -06:00
copilot_thread_spec.rb feat: Add support for more tool, standardize copilot chat service (#11560) 2025-05-23 01:07:07 -07:00
custom_role_spec.rb feat: Add APIs to manage custom roles in Chatwoot (#9995) 2024-08-23 17:18:28 +05:30
inbox_capacity_limit_spec.rb feat: Add agent capacity controllers (#12200) 2025-08-26 19:12:58 -07:00
inbox_member_spec.rb feat: auditlog for team and inbox member updates (#7516) 2023-08-15 19:55:19 -07:00
inbox_spec.rb feat: MFA (#12290) 2025-09-18 20:19:24 +05:30
macro_spec.rb feat: add audit trail for macros (#7352) 2023-06-20 13:04:59 +05:30
message_spec.rb feat: allow agent bot and captain responses to reset waiting since (#13181) 2026-01-07 13:57:43 +05:30
sla_event_spec.rb feat: Conversation API to return applied_sla and sla_events (#9174) 2024-04-01 23:30:07 +05:30
sla_policy_spec.rb feat: sla 1 - refactor sla_policies model and add applied_sla model (#8602) 2024-01-23 23:48:02 +05:30
team_member_spec.rb feat: auditlog for team and inbox member updates (#7516) 2023-08-15 19:55:19 -07:00
team_spec.rb feat: Add audit trail events for team (#7285) 2023-06-09 17:30:36 -07:00
user_spec.rb chore: Move Enterprise pricing validation to on create (#8856) 2024-02-05 19:18:29 +05:30
webhook_spec.rb feat: Audit log APIs (#6434) 2023-03-01 20:02:58 +05:30