diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts index 398c82ae91..d1d4504df4 100644 --- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts +++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts @@ -885,6 +885,29 @@ describe("useBuilderChatPanel – retrySession", () => { expect(result.current.sessionId).toBe("sess-retry-seed"); expect(mockSendMessage).toHaveBeenCalledOnce(); }); + + it("clears stale messages when retrySession is called (setMessages reset)", async () => { + // Simulate stale messages from a previous session + mockChatMessages = [ + { + id: "stale-1", + role: "assistant", + parts: [{ type: "text", text: "Old message from failed session" }], + }, + ]; + + const { result } = renderHook(() => useBuilderChatPanel()); + + // Messages should be present before retry (from mock) + expect(result.current.messages).toHaveLength(1); + + act(() => { + result.current.retrySession(); + }); + + // setMessages([]) clears the internal useChat message list + expect(mockSetMessages).toHaveBeenCalledWith([]); + }); }); describe("useBuilderChatPanel – handleSend", () => { diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts index 38f2395676..8a6a89efd4 100644 --- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts +++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts @@ -346,12 +346,15 @@ export function useBuilderChatPanel({ // hasSentSeedMessageRef is reset so the seed message is re-sent to the // new session (it may have been set to true by a previous successful session // that was later invalidated without a flowID change). + // Messages are cleared so stale messages from the previous session are not + // shown alongside content from the new session. function retrySession() { if (flowID) graphSessionCache.delete(flowID); setSessionId(null); setSessionError(false); isCreatingSessionRef.current = false; hasSentSeedMessageRef.current = false; + setMessages([]); } function handleSend() {