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 9473c05f0 fix(ai): discover queued runs after early attach closure 
(#7042)
9473c05f0 is described below

commit 9473c05f03337b4dea9f27c2d99e90e59682a11f
Author: aias00 <[email protected]>
AuthorDate: Fri Nov 27 12:05:16 2026 +0800

    fix(ai): discover queued runs after early attach closure (#7042)
---
 web/src/pages/ai/hooks/useActiveRunAttach.test.ts | 87 ++++++++++++++++++++++-
 web/src/pages/ai/hooks/useActiveRunAttach.ts      | 29 +++++++-
 2 files changed, 112 insertions(+), 4 deletions(-)

diff --git a/web/src/pages/ai/hooks/useActiveRunAttach.test.ts 
b/web/src/pages/ai/hooks/useActiveRunAttach.test.ts
index 458948389..8982da249 100644
--- a/web/src/pages/ai/hooks/useActiveRunAttach.test.ts
+++ b/web/src/pages/ai/hooks/useActiveRunAttach.test.ts
@@ -16,10 +16,14 @@
  */
 
 import { act, renderHook, waitFor } from '@testing-library/react';
-import { describe, expect, it, vi } from 'vitest';
+import { afterEach, describe, expect, it, vi } from 'vitest';
 import type { AiTimelineVO } from '../../../api/aiEvents';
 import { useActiveRunAttach } from './useActiveRunAttach';
 import { useConversationTimeline } from './useConversationTimeline';
+import { useAgentRun } from './useAgentRun';
+import { attachRunStream } from '../../../api/ai';
+
+vi.mock('../../../api/ai', () => ({ attachRunStream: vi.fn(), openRunStream: 
vi.fn() }));
 
 vi.mock('../../../api/aiConversations', () => ({
   getConversationTimeline: vi.fn(),
@@ -41,6 +45,85 @@ function page(activeRun: AiTimelineVO['activeRun']): 
AiTimelineVO {
 }
 
 describe('useActiveRunAttach', () => {
+  afterEach(() => vi.useRealTimers());
+
+  it('discoversRunningAfterTheQueuedAttachAlreadyClosedTest', async () => {
+    vi.useFakeTimers();
+    timelineMock
+      .mockReset()
+      .mockResolvedValueOnce(page({ id: 41, status: 'QUEUED' }))
+      .mockResolvedValueOnce(page({ id: 41, status: 'QUEUED' }))
+      .mockResolvedValue(page({ id: 41, status: 'RUNNING' }));
+    vi.mocked(attachRunStream)
+      .mockResolvedValueOnce(undefined)
+      .mockImplementation(() => new Promise<void>(() => {}));
+    const { result, unmount } = renderHook(() => {
+      const timeline = useConversationTimeline(7);
+      const run = useAgentRun(7, { refetchTimeline: timeline.refetch });
+      useActiveRunAttach(7, timeline, run);
+      return { timeline, run };
+    });
+    await act(async () => {});
+    expect(attachRunStream).toHaveBeenCalledTimes(1);
+    expect(result.current.run.isStreaming).toBe(false);
+    await act(async () => {
+      await vi.advanceTimersByTimeAsync(1000);
+    });
+    await act(async () => {
+      await vi.advanceTimersByTimeAsync(1000);
+    });
+    expect(result.current.timeline.activeRun?.status).toBe('RUNNING');
+    expect(attachRunStream).toHaveBeenCalledTimes(2);
+    expect(result.current.run.isStreaming).toBe(true);
+    const reads = timelineMock.mock.calls.length;
+    await act(async () => {
+      await vi.advanceTimersByTimeAsync(10_000);
+    });
+    expect(timelineMock).toHaveBeenCalledTimes(reads);
+    unmount();
+  });
+
+  it.each(['unmount', 'navigation', 'failure', 'budget'] as const)(
+    'stopsQueuedDiscoveryTest (%s)',
+    async (ending) => {
+      vi.useFakeTimers();
+      timelineMock.mockReset().mockResolvedValue(page({ id: 41, status: 
'QUEUED' }));
+      const attach = vi.fn().mockResolvedValue(undefined);
+      const view = renderHook(
+        ({ id }) => {
+          const timeline = useConversationTimeline(id);
+          useActiveRunAttach(id, timeline, { isStreaming: false, attach });
+        },
+        { initialProps: { id: 7 as number | null } },
+      );
+      await act(async () => {});
+      if (ending === 'unmount') view.unmount();
+      else if (ending === 'navigation') {
+        view.rerender({ id: null });
+        await act(async () => {});
+      } else if (ending === 'failure') {
+        timelineMock.mockRejectedValue(new Error('offline'));
+        await act(async () => {
+          await vi.advanceTimersByTimeAsync(1000);
+        });
+        expect(timelineMock).toHaveBeenCalledTimes(2);
+      } else {
+        for (let i = 0; i < 30; i++) {
+          await act(async () => {
+            await vi.advanceTimersByTimeAsync(1000);
+          });
+        }
+        expect(timelineMock).toHaveBeenCalledTimes(31);
+      }
+      const reads = timelineMock.mock.calls.length;
+      await act(async () => {
+        await vi.advanceTimersByTimeAsync(60_000);
+      });
+      expect(timelineMock).toHaveBeenCalledTimes(reads);
+      expect(attach).toHaveBeenCalledTimes(1);
+      view.unmount();
+    },
+  );
   it('attachesOnlyTheRunOfTheConversationOnScreenTest', async () => {
     let resolveSecond: (value: AiTimelineVO) => void = () => {};
     timelineMock.mockImplementation((id) =>
@@ -81,7 +164,7 @@ describe('useActiveRunAttach', () => {
       ({ status }: { status: 'QUEUED' | 'RUNNING' }) =>
         useActiveRunAttach(
           7,
-          { activeRun: { id: 42, status }, lastSeq: 0 },
+          { activeRun: { id: 42, status }, lastSeq: 0, refetch: vi.fn() },
           { isStreaming: false, attach },
         ),
       { initialProps: { status: 'QUEUED' as 'QUEUED' | 'RUNNING' } },
diff --git a/web/src/pages/ai/hooks/useActiveRunAttach.ts 
b/web/src/pages/ai/hooks/useActiveRunAttach.ts
index ab46c8db6..038b19994 100644
--- a/web/src/pages/ai/hooks/useActiveRunAttach.ts
+++ b/web/src/pages/ai/hooks/useActiveRunAttach.ts
@@ -34,7 +34,8 @@ import type { UseConversationTimelineResult } from 
'./useConversationTimeline';
  * to the same run id — which is the whole point of the exercise. It also lets 
a run that has moved
  * on from the state this visit attached to be attached once more: a reload 
during admission observes
  * QUEUED, whose stream closes immediately because no worker exists yet, and 
the run only becomes
- * observable when the timeline reports it RUNNING.
+ * observable when the timeline reports it RUNNING. While idle on QUEUED, up 
to thirty serial
+ * one-second refreshes discover that transition; a read failure stops 
discovery until a reload.
  *
  * The reset effect is declared before the attach effect on purpose: within 
one commit effects run
  * in declaration order, and arriving at a conversation must clear the 
previous visit's marker
@@ -42,7 +43,7 @@ import type { UseConversationTimelineResult } from 
'./useConversationTimeline';
  */
 export function useActiveRunAttach(
   conversationId: number | null,
-  timeline: Pick<UseConversationTimelineResult, 'activeRun' | 'lastSeq'>,
+  timeline: Pick<UseConversationTimelineResult, 'activeRun' | 'lastSeq' | 
'refetch'>,
   run: Pick<UseAgentRunResult, 'isStreaming' | 'attach'>,
 ): void {
   // What the last attach observed, not just which run: a reload during 
admission attaches to a
@@ -53,6 +54,30 @@ export function useActiveRunAttach(
   const requestedRunRef = useRef<{ id: number; status: string } | null>(null);
   const { activeRun, lastSeq } = timeline;
   const { isStreaming, attach } = run;
+  const queuedRunId = activeRun?.status === 'QUEUED' ? activeRun.id : null;
+  const { refetch } = timeline;
+
+  useEffect(() => {
+    if (conversationId === null || queuedRunId === null || isStreaming) return;
+    let cancelled = false;
+    let attempts = 0;
+    let timer: ReturnType<typeof setTimeout>;
+    const discover = async () => {
+      if (cancelled) return;
+      attempts += 1;
+      try {
+        await refetch();
+      } catch {
+        return; // The timeline already exposes the failure; do not turn it 
into a retry loop.
+      }
+      if (!cancelled && attempts < 30) timer = setTimeout(() => void 
discover(), 1000);
+    };
+    timer = setTimeout(() => void discover(), 1000);
+    return () => {
+      cancelled = true;
+      clearTimeout(timer);
+    };
+  }, [conversationId, isStreaming, queuedRunId, refetch]);
 
   useEffect(() => {
     requestedRunRef.current = null;

Reply via email to