From a450d6437d1a07c77a35ec264c1455ac546cf3b1 Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Mon, 20 Jul 2026 14:05:59 +0530 Subject: [PATCH] fix(automations): track newest incoming message on episode re-arm --- app/models/automation_rule_pending_execution.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/models/automation_rule_pending_execution.rb b/app/models/automation_rule_pending_execution.rb index ac50feba6..3f287b201 100644 --- a/app/models/automation_rule_pending_execution.rb +++ b/app/models/automation_rule_pending_execution.rb @@ -78,10 +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 !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. + elsif !row.terminal? + # Track the newest qualifying message. Reply-chase advances due_at with each agent reply; + # awaiting-agent keeps its first clock (its anchor is the stable waiting_since, so due_at is + # unchanged). Re-anchoring a row still processing (its worker died mid-run) back to pending + # also keeps a stale reclaim from firing the old clock instead of the latest one. row.update!(status: :pending, due_at: due_at, message_id: message.id) end end