Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(sidebar): refresh the conversation list when a cloud start task is ready (OSS-14604) (#18741)

Hiep Le198727aed3d3
diff --git a/__tests__/hooks/query/use-task-polling.test.tsx b/__tests__/hooks/query/use-task-polling.test.tsx
index 49cc0f4ec4..bedef1b5b4 100644
--- a/__tests__/hooks/query/use-task-polling.test.tsx
+++ b/__tests__/hooks/query/use-task-polling.test.tsx
@@ -212,6 +212,33 @@ describe("useTaskPolling", () => {
});
});
+ it("marks the sidebar conversation list stale once the task is ready", async () => {
+ vi.mocked(AgentServerConversationService.getStartTask).mockResolvedValue(
+ readyTask,
+ );
+ const wrapper = createWrapper();
+ // Same key shapes `usePaginatedConversations` and `useStartTasks` read.
+ const conversationsKey = [
+ "user",
+ "conversations",
+ "paginated",
+ 20,
+ "default-local",
+ null,
+ ];
+ const startTasksKey = ["start-tasks", "search", 10];
+ queryClient.setQueryData(conversationsKey, { pages: [], pageParams: [] });
+ queryClient.setQueryData(startTasksKey, []);
+
+ renderHook(() => useTaskPollingController(), { wrapper });
+
+ await waitFor(() => expect(navigate).toHaveBeenCalledOnce());
+ expect(queryClient.getQueryState(conversationsKey)?.isInvalidated).toBe(
+ true,
+ );
+ expect(queryClient.getQueryState(startTasksKey)?.isInvalidated).toBe(true);
+ });
+
it("handles a ready task once when multiple components consume polling state", async () => {
vi.mocked(AgentServerConversationService.getStartTask).mockResolvedValue(
readyTask,
diff --git a/src/hooks/query/use-task-polling.ts b/src/hooks/query/use-task-polling.ts
index a021181d72..40c0dfaa41 100644
--- a/src/hooks/query/use-task-polling.ts
+++ b/src/hooks/query/use-task-polling.ts
@@ -1,5 +1,5 @@
import { useEffect, useLayoutEffect, useRef } from "react";
-import { useQuery } from "@tanstack/react-query";
+import { useQuery, useQueryClient } from "@tanstack/react-query";
import AgentServerConversationService from "#/api/conversation-service/agent-server-conversation-service.api";
import type { AppConversationStartTask } from "#/api/conversation-service/agent-server-conversation-service.types";
import {
@@ -115,6 +115,7 @@ export const useTaskPollingController = () => {
const { conversationId } = useOptionalConversationId();
const { navigate } = useNavigation();
const backendScopedPath = useBackendScopedPath();
+ const queryClient = useQueryClient();
const handledReadyTaskIdRef = useRef<string | null>(null);
// Reassign optimistic pending messages before paint on the real conversation
@@ -156,6 +157,13 @@ export const useTaskPollingController = () => {
trackCloudConversationReady(taskId, appConversationId);
storeTaskPlugins(task, appConversationId);
+ // The create mutation invalidated the conversation list before the
+ // conversation row existed (cloud returns the start task first). The row
+ // is persisted by the time the task is READY, so refetch now so the
+ // sidebar shows the new conversation without waiting for the 30s poll.
+ queryClient.invalidateQueries({ queryKey: ["user", "conversations"] });
+ queryClient.invalidateQueries({ queryKey: ["start-tasks"] });
+
void (async () => {
await flushPendingTaskAttachments(taskId, appConversationId);
@@ -174,7 +182,7 @@ export const useTaskPollingController = () => {
replace: true,
});
})();
- }, [backendScopedPath, task, taskId, navigate]);
+ }, [backendScopedPath, task, taskId, navigate, queryClient]);
return polling;
};