From a33ee3a1d0f4bfbb3ea9d1e202ec13d07dd7c2d8 Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Mon, 20 Jul 2026 13:01:03 +0530 Subject: [PATCH] fix(automations): re-anchor stuck processing reply-chase rows on a newer reply --- app/models/automation_rule_pending_execution.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/models/automation_rule_pending_execution.rb b/app/models/automation_rule_pending_execution.rb index e811b7a28..ac50feba6 100644 --- a/app/models/automation_rule_pending_execution.rb +++ b/app/models/automation_rule_pending_execution.rb @@ -78,9 +78,11 @@ class AutomationRulePendingExecution < ApplicationRecord # A message episode key can recur (no new incoming reply) while conditions swing back into # match, so a later qualifying message re-arms the condition-only skip instead of dropping. row.update!(status: :pending, skip_reason: nil, due_at: due_at, message_id: message.id) - elsif row.pending? && !message.incoming? - # Reply-chase tracks the latest agent reply; awaiting-agent keeps its first clock. - row.update!(due_at: due_at, message_id: message.id) + elsif !message.incoming? && !row.terminal? + # Reply-chase tracks the latest agent reply; awaiting-agent keeps its first clock. Re-anchor a + # row still processing (its worker died mid-run) back to pending too, so a stale reclaim can't + # fire the old clock instead of waiting the full delay from this latest reply. + row.update!(status: :pending, due_at: due_at, message_id: message.id) end end @@ -163,6 +165,10 @@ class AutomationRulePendingExecution < ApplicationRecord skipped? && skip_reason == CONDITIONS_CHANGED_SKIP end + def terminal? + executed? || skipped? + end + private def claimable?