From b9e7030908c2de2dd17e6f1cb24cabdd52946fce Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 1 Jul 2026 12:16:37 +0530 Subject: [PATCH] fix: derive captain reopen cohort from handled conversations --- .../builders/captain/assistant_stats_builder.rb | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/enterprise/app/builders/captain/assistant_stats_builder.rb b/enterprise/app/builders/captain/assistant_stats_builder.rb index 266022ed9..99980e356 100644 --- a/enterprise/app/builders/captain/assistant_stats_builder.rb +++ b/enterprise/app/builders/captain/assistant_stats_builder.rb @@ -181,12 +181,13 @@ class Captain::AssistantStatsBuilder values.map { |value| quote(value) }.join(', ') end - # Of the conversations Captain auto-resolved (inbox-based), the share reopened afterwards. - # Covers both the evaluated (inference) and time-based (bot) resolve paths so the - # denominator matches auto_resolution_rate. + # Of the conversations Captain auto-resolved, the share reopened afterwards. The cohort is + # derived from the assistant's handled conversations (not current inbox membership) so a later + # inbox reassignment doesn't drop historical resolves, and covers both the evaluated (inference) + # and time-based (bot) resolve paths so the denominator matches auto_resolution_rate. def reopen_rate(range) - resolved_scope = account.reporting_events.where(name: RESOLVED_EVENT_NAMES, - inbox_id: assistant_inbox_ids, created_at: range) + resolved_scope = account.reporting_events.where(name: RESOLVED_EVENT_NAMES, created_at: range, + conversation_id: handled_scope(range).select(:conversation_id)) # event_start_time on a reopen is the preceding resolve's timestamp, so requiring it # within the window keeps only reopens that followed the in-window resolve, not an # earlier resolve/reopen cycle on the same conversation. @@ -198,10 +199,6 @@ class Captain::AssistantStatsBuilder rate(reopened, resolved_scope.distinct.count(:conversation_id)) end - def assistant_inbox_ids - @assistant_inbox_ids ||= assistant.inboxes.ids - end - # Approved/pending FAQ counts and the document total in a single round trip. def knowledge approved, pending, documents = Captain::AssistantResponse.by_assistant(assistant.id).reorder(nil).pick(