π OpenHands: AI-Driven Development
fix(conversation): fast-poll active conversation until title is set (#1508) (#1520)
Vasco Schiavo39bb90b8ef41
diff --git a/__tests__/hooks/query/use-active-conversation.test.ts b/__tests__/hooks/query/use-active-conversation.test.tsindex 915a48d8e2..d82a8c491d 100644--- a/__tests__/hooks/query/use-active-conversation.test.ts+++ b/__tests__/hooks/query/use-active-conversation.test.ts@@ -10,11 +10,16 @@* The fix adds sandbox_status === "PAUSED" as a second fast-poll trigger so* the hook picks up the PAUSED β RUNNING transition within ~3 s regardless of* whether conversation_url is present.+ *+ * It also fast-polls while the title is still unset and the agent is actively+ * executing, so the header title (which lands asynchronously) refreshes within+ * ~3 s instead of on the slow 30 s tick.*/import { describe, it, expect, vi, beforeEach } from "vitest";import { renderHook } from "@testing-library/react";import { useActiveConversation } from "#/hooks/query/use-active-conversation";import type { AppConversation } from "#/api/conversation-service/agent-server-conversation-service.types";+import { ExecutionStatus } from "#/types/agent-server/core/base/common";// ββ Mocks βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ@@ -26,8 +31,7 @@ const { mockUseUserConversation, mockSetCurrentConversation } = vi.hoisted();vi.mock("#/hooks/query/use-user-conversation", () => ({- useUserConversation: (...args: unknown[]) =>- mockUseUserConversation(...args),+ useUserConversation: (...args: unknown[]) => mockUseUserConversation(...args),}));vi.mock("#/hooks/use-conversation-id", () => ({@@ -69,9 +73,9 @@ function renderAndCaptureIntervalFn(): IntervalFn {return captured;}-function makeQuery(- data: Partial<AppConversation> | null | undefined,-): { state: { data: AppConversation | null | undefined } } {+function makeQuery(data: Partial<AppConversation> | null | undefined): {+ state: { data: AppConversation | null | undefined };+} {if (!data) return { state: { data: data as null | undefined } };return {state: {@@ -89,7 +93,8 @@ function makeQuery(created_at: "2024-01-01T00:00:00Z",updated_at: "2024-01-01T00:00:00Z",execution_status: null,- conversation_url: "https://sandbox.example.com/api/conversations/conv-1",+ conversation_url:+ "https://sandbox.example.com/api/conversations/conv-1",session_api_key: null,sandbox_id: null,sub_conversation_ids: [],@@ -112,7 +117,8 @@ describe("useActiveConversation β refetchInterval callback", () => {const result = intervalFn(makeQuery({sandbox_status: "PAUSED",- conversation_url: "https://sandbox.example.com/api/conversations/conv-1",+ conversation_url:+ "https://sandbox.example.com/api/conversations/conv-1",}),);@@ -143,7 +149,8 @@ describe("useActiveConversation β refetchInterval callback", () => {const result = intervalFn(makeQuery({sandbox_status: "RUNNING",- conversation_url: "https://sandbox.example.com/api/conversations/conv-1",+ conversation_url:+ "https://sandbox.example.com/api/conversations/conv-1",}),);@@ -157,4 +164,86 @@ describe("useActiveConversation β refetchInterval callback", () => {expect(intervalFn(makeQuery(null))).toBe(30000);expect(intervalFn(makeQuery(undefined))).toBe(30000);});++ // ββ fast-poll until the title is set βββββββββββββββββββββββββββββββββββββββ++ it("returns 3000 when title is unset and the agent is actively executing", () => {+ const intervalFn = renderAndCaptureIntervalFn();++ const result = intervalFn(+ makeQuery({+ title: null,+ execution_status: ExecutionStatus.RUNNING,+ conversation_url:+ "https://sandbox.example.com/api/conversations/conv-1",+ sandbox_status: "RUNNING",+ }),+ );++ expect(result).toBe(3000);+ });++ it("returns 3000 when title is an empty string and the agent is actively executing", () => {+ const intervalFn = renderAndCaptureIntervalFn();++ const result = intervalFn(+ makeQuery({+ title: "",+ execution_status: ExecutionStatus.IDLE,+ conversation_url:+ "https://sandbox.example.com/api/conversations/conv-1",+ sandbox_status: "RUNNING",+ }),+ );++ expect(result).toBe(3000);+ });++ it("returns 30000 when title is set, even while the agent is executing", () => {+ const intervalFn = renderAndCaptureIntervalFn();++ const result = intervalFn(+ makeQuery({+ title: "Generated title",+ execution_status: ExecutionStatus.RUNNING,+ conversation_url:+ "https://sandbox.example.com/api/conversations/conv-1",+ sandbox_status: "RUNNING",+ }),+ );++ expect(result).toBe(30000);+ });++ it("returns 30000 when title is unset but execution has errored (no title is coming)", () => {+ const intervalFn = renderAndCaptureIntervalFn();++ const result = intervalFn(+ makeQuery({+ title: null,+ execution_status: ExecutionStatus.ERROR,+ conversation_url:+ "https://sandbox.example.com/api/conversations/conv-1",+ sandbox_status: "RUNNING",+ }),+ );++ expect(result).toBe(30000);+ });++ it("returns 30000 when title is unset and execution_status is null (idle/terminal, not active)", () => {+ const intervalFn = renderAndCaptureIntervalFn();++ const result = intervalFn(+ makeQuery({+ title: null,+ execution_status: null,+ conversation_url:+ "https://sandbox.example.com/api/conversations/conv-1",+ sandbox_status: "RUNNING",+ }),+ );++ expect(result).toBe(30000);+ });});diff --git a/src/hooks/query/use-active-conversation.ts b/src/hooks/query/use-active-conversation.tsindex 79fbc90b90..b6e396c4c5 100644--- a/src/hooks/query/use-active-conversation.ts+++ b/src/hooks/query/use-active-conversation.ts@@ -2,6 +2,7 @@ import { useEffect } from "react";import { useOptionalConversationId } from "#/hooks/use-conversation-id";import { useUserConversation } from "./use-user-conversation";import ConversationService from "#/api/conversation-service/conversation-service.api";+import { isExecutionActive } from "#/utils/status";export const useActiveConversation = () => {// Optional: the chat input renders on the home page too (no conversation@@ -15,15 +16,17 @@ export const useActiveConversation = () => {const userConversation = useUserConversation(actualConversationId,- // Poll at 3 s while the sandbox URL is absent OR while the sandbox is- // PAUSED. A paused sandbox still carries the old conversation_url (it isn't- // cleared), so checking only for a missing URL would leave us on the slow- // 30 s interval while the sandbox is waking up after a resume call.+ // Fast-poll (3 s) while: the sandbox URL is absent; the sandbox is PAUSED+ // (it keeps the stale conversation_url, so a missing-URL check alone misses+ // the wake-up); or the agent is executing but has no title yet (the title+ // lands asynchronously after conversation_url is already set).(query) => {const data = query.state.data;if (data &&- (!data.conversation_url || data.sandbox_status === "PAUSED")+ (!data.conversation_url ||+ data.sandbox_status === "PAUSED" ||+ (!data.title && isExecutionActive(data.execution_status)))) {return 3000;}