This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 345399211 fix(ai): exclude active conversations before retention limit 
(#7227)
345399211 is described below

commit 34539921165049e4221f3f3bd28c8fd7c20ddc70
Author: aias00 <[email protected]>
AuthorDate: Fri Nov 27 12:04:37 2026 +0800

    fix(ai): exclude active conversations before retention limit (#7227)
---
 .../ai/conversation/AiConversationRepository.java  |  5 +++-
 .../ops/ai/conversation/AiConversationService.java |  7 ++---
 .../MybatisPlusAiConversationRepository.java       |  7 +++++
 .../AiConversationPersistenceIntegrationTest.java  | 34 ++++++++++++++++++++++
 4 files changed, 47 insertions(+), 6 deletions(-)

diff --git 
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationRepository.java
 
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationRepository.java
index 961971e8b..aa5170364 100644
--- 
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationRepository.java
+++ 
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationRepository.java
@@ -55,7 +55,10 @@ public interface AiConversationRepository {
 
     int deleteById(Long id);
 
-    /** Ids created strictly before the cutoff, oldest first, capped at limit. 
*/
+    /**
+     * Ids created strictly before the cutoff with no active run, oldest 
first, capped at limit.
+     * Busy conversations are excluded before the limit so they cannot block 
later idle candidates.
+     */
     List<Long> findIdsCreatedBefore(LocalDateTime cutoff, int limit);
 
     int deleteByIds(List<Long> ids);
diff --git 
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationService.java
 
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationService.java
index 4452db8a8..bd8a5d16d 100644
--- 
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationService.java
+++ 
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationService.java
@@ -489,11 +489,8 @@ public class AiConversationService implements 
ApplicationRunner {
             }
             List<Long> purgable = skipConversationsWithARunInFlight(ids);
             if (purgable.isEmpty()) {
-                // The whole page is mid-run, so nothing was deleted and the 
next query returns this
-                // same page: no cursor here, and the loop stays bounded by 
maxBatches. This is only
-                // reachable while a run is genuinely live — 
reapOrphanedRuns() has already turned
-                // every ownerless non-terminal run into FAILED/ORPHANED 
earlier in the same pass — so
-                // what is left finishes on its own and a later pass purges it.
+                // A run may have started after the candidate query excluded 
busy conversations.
+                // Recheck before deleting; the next query skips these 
now-busy rows in SQL.
                 continue;
             }
             int deleted = deleteCascade(purgable);
diff --git 
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/MybatisPlusAiConversationRepository.java
 
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/MybatisPlusAiConversationRepository.java
index c95d7acd8..e64eb7fb1 100644
--- 
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/MybatisPlusAiConversationRepository.java
+++ 
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/MybatisPlusAiConversationRepository.java
@@ -22,6 +22,7 @@ import 
com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import lombok.RequiredArgsConstructor;
 import org.apache.rocketmq.studio.common.util.LikePatterns;
 import org.apache.rocketmq.studio.common.domain.PageResult;
+import org.apache.rocketmq.studio.ops.ai.conversation.event.RunStatus;
 import org.apache.rocketmq.studio.persistence.entity.RmqAiConversation;
 import org.apache.rocketmq.studio.persistence.mapper.RmqAiConversationMapper;
 import org.springframework.stereotype.Repository;
@@ -109,6 +110,12 @@ public class MybatisPlusAiConversationRepository 
implements AiConversationReposi
         return conversationMapper.selectObjs(new 
QueryWrapper<RmqAiConversation>()
                         .select("id")
                         .lt("gmt_create", cutoff)
+                        // Exclude busy conversations before LIMIT, or a full 
busy page starves
+                        // every idle expired conversation behind it on every 
cleanup iteration.
+                        .notExists("SELECT 1 FROM rmq_ai_run active_run "
+                                + "WHERE active_run.conversation_id = 
rmq_ai_conversation.id "
+                                + "AND active_run.status IN ({0}, {1})",
+                                RunStatus.QUEUED.name(), 
RunStatus.RUNNING.name())
                         .orderByAsc("gmt_create", "id")
                         .last("LIMIT " + limit))
                 .stream()
diff --git 
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationPersistenceIntegrationTest.java
 
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationPersistenceIntegrationTest.java
index c761526c0..fd8bc7b92 100644
--- 
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationPersistenceIntegrationTest.java
+++ 
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationPersistenceIntegrationTest.java
@@ -33,6 +33,8 @@ import 
org.apache.rocketmq.studio.persistence.mapper.RmqAiRunMapper;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 import org.mockito.InOrder;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
@@ -399,6 +401,38 @@ class AiConversationPersistenceIntegrationTest {
         
assertThat(runRepository.findActiveConversationIds(List.of())).isEmpty();
     }
 
+    @ParameterizedTest
+    @EnumSource(value = RunStatus.class, names = {"QUEUED", "RUNNING"})
+    void 
retentionShouldReachIdleConversationsBehindAFullBusyPageTest(RunStatus status) {
+        Seeded busy = seed();
+        Seeded idle = seed();
+        RmqAiRun active = new RmqAiRun();
+        active.setId(busy.secondRunId);
+        active.setStatus(status.name());
+        // Fresh activity must not be mistaken for an orphan by the preceding 
sweep.
+        active.setGmtModified(LocalDateTime.now(java.time.ZoneOffset.UTC));
+        runRepository.update(active);
+        int originalBatchSize = properties.getCleanupBatchSize();
+        int originalMaxBatches = properties.getCleanupMaxBatches();
+        try {
+            properties.setRetentionDays(1);
+            properties.setCleanupBatchSize(1);
+            properties.setCleanupMaxBatches(2);
+
+            conversationService.purgeExpired();
+
+            
assertThat(conversationMapper.selectById(busy.conversationId)).isNotNull();
+            assertThat(countRuns(busy.conversationId)).isEqualTo(2);
+            
assertThat(countEvents(busy.conversationId)).isEqualTo(EVENT_COUNT);
+            
assertThat(conversationMapper.selectById(idle.conversationId)).isNull();
+            assertThat(countRuns(idle.conversationId)).isZero();
+            assertThat(countEvents(idle.conversationId)).isZero();
+        } finally {
+            properties.setCleanupBatchSize(originalBatchSize);
+            properties.setCleanupMaxBatches(originalMaxBatches);
+        }
+    }
+
     // --- harness 
------------------------------------------------------------------
 
     private void verifyNoCascade() {

Reply via email to