From d4c235c8d688b1bd107308082911659dc53bbec8 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Mon, 27 Oct 2025 14:11:15 +0530 Subject: [PATCH] feat: better handoff handling --- .../captain/conversation/response_builder_job.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/enterprise/app/jobs/captain/conversation/response_builder_job.rb b/enterprise/app/jobs/captain/conversation/response_builder_job.rb index 0ef537132..6e9d5f28e 100644 --- a/enterprise/app/jobs/captain/conversation/response_builder_job.rb +++ b/enterprise/app/jobs/captain/conversation/response_builder_job.rb @@ -13,15 +13,18 @@ class Captain::Conversation::ResponseBuilderJob < ApplicationJob ActiveRecord::Base.transaction do generate_and_process_response end - - # Process handoff OUTSIDE the transaction so after_commit callbacks work properly - # This ensures Rails' dirty tracking (previous_changes, saved_change_to_*?) works correctly - process_handoff if @handoff_requested rescue StandardError => e raise e if e.is_a?(ActiveStorage::FileNotFoundError) || e.is_a?(Faraday::BadRequestError) handle_error(e) ensure + # Process handoff OUTSIDE the transaction so after_commit callbacks work properly + # This is because within a transaction, each save operation overrides previous_changes + # There's a separate gem to handle this: https://github.com/Shopify/ar_transaction_changes/ + # But for us, moving the handoff outside the transaction works well enough + # This ensures Rails' dirty tracking (previous_changes, saved_change_to_*?) works correctly + process_handoff if @handoff_requested + Current.executed_by = nil Current.handoff_requested = nil end @@ -43,7 +46,6 @@ class Captain::Conversation::ResponseBuilderJob < ApplicationJob if handoff_requested? || Current.handoff_requested @handoff_requested = true - create_handoff_message return end @@ -84,6 +86,7 @@ class Captain::Conversation::ResponseBuilderJob < ApplicationJob def process_handoff I18n.with_locale(@assistant.account.locale) do + create_handoff_message @conversation.bot_handoff! end end @@ -120,7 +123,6 @@ class Captain::Conversation::ResponseBuilderJob < ApplicationJob def handle_error(error) log_error(error) @handoff_requested = true - create_handoff_message true end