From 3c0d55f87a3b2facac4aeb1684b447f3cd726446 Mon Sep 17 00:00:00 2001 From: Aakash Bakhle <48802744+aakashb95@users.noreply.github.com> Date: Mon, 23 Mar 2026 17:09:58 +0530 Subject: [PATCH] fix: handoff only if conversation pending (#13882) # Pull Request Template ## Description - Skip handoff when the conversation is no longer pending. - Fetch conversation status with Conversation.uncached to avoid stale query cache when checking pending state. ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. Difficult to reproduce locally, but seen a couple of conversations where bot hands off an extra time or Captain re-generates usually due to a retry on FaradayTimeoutError but at perform time, the conversation status is stale so it appears as if Captain responded in an open conversation. ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] Any dependent changes have been merged and published in downstream modules --- .../app/jobs/captain/conversation/response_builder_job.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/enterprise/app/jobs/captain/conversation/response_builder_job.rb b/enterprise/app/jobs/captain/conversation/response_builder_job.rb index acbe431a6..268c6a3ee 100644 --- a/enterprise/app/jobs/captain/conversation/response_builder_job.rb +++ b/enterprise/app/jobs/captain/conversation/response_builder_job.rb @@ -138,7 +138,7 @@ class Captain::Conversation::ResponseBuilderJob < ApplicationJob def handle_error(error) log_error(error) - process_action('handoff') + process_action('handoff') if conversation_pending? true end @@ -151,7 +151,7 @@ class Captain::Conversation::ResponseBuilderJob < ApplicationJob end def conversation_pending? - status = Conversation.where(id: @conversation.id).pick(:status) + status = Conversation.uncached { Conversation.where(id: @conversation.id).pick(:status) } status == 'pending' || status == Conversation.statuses[:pending] end end