diff --git a/webui/src/components/thread/ThreadComposer.tsx b/webui/src/components/thread/ThreadComposer.tsx index ea47cb651..8e01d8915 100644 --- a/webui/src/components/thread/ThreadComposer.tsx +++ b/webui/src/components/thread/ThreadComposer.tsx @@ -963,7 +963,7 @@ export function ThreadComposer({ const secondEnterPromptIdRef = useRef(null); const draggedQueuedPromptIdRef = useRef(null); const previousPendingQueueKeyRef = useRef(pendingQueueKey); - const wasStreamingRef = useRef(isStreaming); + const previousQueueRunRef = useRef({ key: pendingQueueKey, isStreaming }); const skipNextQueuedFlushRef = useRef(false); const skipQueuedPromptPersistRef = useRef(false); const voiceShortcutDownRef = useRef(false); @@ -1895,16 +1895,21 @@ export function ThreadComposer({ }, [onSend, queuedPrompts]); useEffect(() => { - const wasStreaming = wasStreamingRef.current; - wasStreamingRef.current = isStreaming; + const previous = previousQueueRunRef.current; + previousQueueRunRef.current = { key: pendingQueueKey, isStreaming }; if (!isStreaming) secondEnterPromptIdRef.current = null; - if (!wasStreaming || isStreaming || queuedPrompts.length === 0) return; + // Switching to an idle session is not completion of the previous session's run. + if (previous.key !== pendingQueueKey) { + skipNextQueuedFlushRef.current = false; + return; + } + if (!previous.isStreaming || isStreaming || queuedPrompts.length === 0) return; if (skipNextQueuedFlushRef.current) { skipNextQueuedFlushRef.current = false; return; } sendNextQueuedPrompt(); - }, [sendNextQueuedPrompt, isStreaming, queuedPrompts.length]); + }, [sendNextQueuedPrompt, isStreaming, pendingQueueKey, queuedPrompts.length]); const handleStop = useCallback(() => { secondEnterPromptIdRef.current = null; diff --git a/webui/src/tests/thread-composer.test.tsx b/webui/src/tests/thread-composer.test.tsx index 6ee3a48fe..3e6b287ab 100644 --- a/webui/src/tests/thread-composer.test.tsx +++ b/webui/src/tests/thread-composer.test.tsx @@ -3053,6 +3053,39 @@ describe("ThreadComposer", () => { }); }); + it("keeps queued guidance in its session when switching from running to idle", () => { + const sendA = vi.fn(); + const sendB = vi.fn(); + const view = render( + , + ); + fireEvent.change(screen.getByLabelText("Message input"), { + target: { value: "follow-up for A" }, + }); + fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" }); + expect(screen.getByText("follow-up for A")).toBeInTheDocument(); + expect(sendA).not.toHaveBeenCalled(); + + view.rerender( + , + ); + expect(sendB).not.toHaveBeenCalled(); + expect(screen.queryByText("follow-up for A")).not.toBeInTheDocument(); + + view.rerender( + , + ); + expect(screen.getByText("follow-up for A")).toBeInTheDocument(); + expect(sendA).not.toHaveBeenCalled(); + view.rerender( + , + ); + expect(sendA).toHaveBeenCalledTimes(1); + expect(sendA).toHaveBeenCalledWith("follow-up for A"); + expect(sendB).not.toHaveBeenCalled(); + expect(screen.queryByText("follow-up for A")).not.toBeInTheDocument(); + }); + it("persists queued guidance per chat across remounts", async () => { const onSend = vi.fn(); const { rerender, unmount } = render(