🙌 OpenHands: AI-Driven Development
fix: stop duplicating the first user message on cloud backends (#924)
Hiep Le390440601019
diff --git a/__tests__/contexts/conversation-websocket-context.test.tsx b/__tests__/contexts/conversation-websocket-context.test.tsxindex 8cdacb4d4b..f41996af41 100644--- a/__tests__/contexts/conversation-websocket-context.test.tsx+++ b/__tests__/contexts/conversation-websocket-context.test.tsx@@ -89,7 +89,10 @@ describe("ConversationWebSocketProvider — conversation-scoped event store", ()// Act: switch to conversation B.rerender(<QueryClientProvider client={queryClient}>- <ConversationWebSocketProvider conversationId="conv-b" conversationUrl={null}>+ <ConversationWebSocketProvider+ conversationId="conv-b"+ conversationUrl={null}+ ><div /></ConversationWebSocketProvider></QueryClientProvider>,@@ -120,4 +123,35 @@ describe("ConversationWebSocketProvider — conversation-scoped event store", ()// appending a second copy — exactly two events, no double-insertion.expect(eventIds()).toHaveLength(2);});++ it("consumes the optimistic pending bubble when the echoed user message arrives via REST preload", async () => {+ // Arrange: a cloud start-task conversation left a "Sending…" bubble whose+ // content matches the first message the server has already persisted. With+ // the WebSocket stubbed, the only path that delivers the echo is the REST+ // history preload — the path that previously left this bubble orphaned.+ useOptimisticUserMessageStore.setState({+ pendingMessages: [+ {+ id: "pending-1",+ conversationId: "conv-a",+ text: "User message",+ content: "User message",+ status: "sending",+ imageUrls: [],+ fileUrls: [],+ timestamp: new Date().toISOString(),+ },+ ],+ });++ // Act: open the conversation; preload returns the echoed user message.+ renderProvider("conv-a");++ // Assert: the preloaded echo cleared the bubble, so it isn't shown twice.+ await waitFor(() =>+ expect(useOptimisticUserMessageStore.getState().pendingMessages).toEqual(+ [],+ ),+ );+ });});diff --git a/src/contexts/conversation-websocket-context.tsx b/src/contexts/conversation-websocket-context.tsxindex 4d38cf9809..3c77f719c8 100644--- a/src/contexts/conversation-websocket-context.tsx+++ b/src/contexts/conversation-websocket-context.tsx@@ -248,7 +248,28 @@ export function ConversationWebSocketProvider({return;}addEvents(preloadedHistory.events);- }, [preloadedHistory, addEvents]);++ // The first user message of a cloud start-task conversation is persisted+ // server-side and reaches us via this REST preload, not over the WebSocket+ // (which subscribes with resend_mode='since' after the latest preloaded+ // timestamp). Consume any matching optimistic "Sending…" bubble here too —+ // mirroring the WS handler — so it doesn't linger as a duplicate of the echo.+ if (conversationId) {+ for (const event of preloadedHistory.events) {+ if (isUserMessageEvent(event)) {+ consumeMatchingPendingMessage(+ conversationId,+ extractMessageEventText(event),+ );+ }+ }+ }+ }, [+ preloadedHistory,+ addEvents,+ conversationId,+ consumeMatchingPendingMessage,+ ]);/*** Timestamp of the latest event we already have from REST. Used as