Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: invalidate sandbox and VS Code URL caches on conversation resume (#13988)

Xingyao Wang595bb4749d3d
diff --git a/frontend/__tests__/hooks/mutation/use-unified-resume-conversation-sandbox.test.tsx b/frontend/__tests__/hooks/mutation/use-unified-resume-conversation-sandbox.test.tsx
new file mode 100644
index 0000000000..d11e1cac2f
--- /dev/null
+++ b/frontend/__tests__/hooks/mutation/use-unified-resume-conversation-sandbox.test.tsx
@@ -0,0 +1,99 @@
+import { renderHook, waitFor } from "@testing-library/react";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { describe, expect, it, vi, beforeEach } from "vitest";
+import React from "react";
+import { SandboxService } from "#/api/sandbox-service/sandbox-service.api";
+import V1ConversationService from "#/api/conversation-service/v1-conversation-service.api";
+import { useUnifiedResumeConversationSandbox } from "#/hooks/mutation/use-unified-start-conversation";
+
+// Mock the error message store
+vi.mock("#/stores/error-message-store", () => ({
+ useErrorMessageStore: (selector: (state: { removeErrorMessage: () => void }) => unknown) =>
+ selector({ removeErrorMessage: vi.fn() }),
+}));
+
+describe("useUnifiedResumeConversationSandbox", () => {
+ let queryClient: QueryClient;
+
+ beforeEach(() => {
+ queryClient = new QueryClient({
+ defaultOptions: {
+ queries: { retry: false },
+ mutations: { retry: false },
+ },
+ });
+ vi.restoreAllMocks();
+ });
+
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
+ <QueryClientProvider client={queryClient}>
+ {children}
+ </QueryClientProvider>
+ );
+
+ it("invalidates sandbox and vscode_url queries on settled", async () => {
+ // Mock the API calls in the mutation chain
+ vi.spyOn(V1ConversationService, "batchGetAppConversations").mockResolvedValue([
+ {
+ id: "test-conv-id",
+ created_by_user_id: null,
+ sandbox_id: "test-sandbox-id",
+ conversation_url: "http://localhost:3000",
+ session_api_key: "test-key",
+ selected_repository: null,
+ selected_branch: null,
+ git_provider: null,
+ title: "Test",
+ public: false,
+ sandbox_status: "RUNNING",
+ execution_status: null,
+ trigger: null,
+ pr_number: [],
+ llm_model: null,
+ metrics: null,
+ sub_conversation_ids: [],
+ created_at: new Date().toISOString(),
+ updated_at: new Date().toISOString(),
+ },
+ ]);
+ vi.spyOn(SandboxService, "resumeSandbox").mockResolvedValue({ success: true });
+
+ // Pre-populate query cache with stale sandbox data
+ queryClient.setQueryData(["sandboxes", "batch", ["test-sandbox-id"]], [
+ {
+ sandbox_id: "test-sandbox-id",
+ exposed_urls: [{ name: "VSCODE", url: "https://old-runtime.example.com" }],
+ },
+ ]);
+ queryClient.setQueryData(["unified", "vscode_url", "test-conv-id"], {
+ url: "https://old-runtime.example.com",
+ error: null,
+ });
+
+ // Spy on invalidateQueries
+ const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");
+
+ const { result } = renderHook(() => useUnifiedResumeConversationSandbox(), { wrapper });
+
+ // Trigger the mutation
+ result.current.mutate({ conversationId: "test-conv-id" });
+
+ await waitFor(() => {
+ expect(result.current.isSuccess || result.current.isError).toBe(true);
+ });
+
+ // Verify sandbox queries were invalidated
+ const invalidateCalls = invalidateSpy.mock.calls.map((call) => call[0]);
+ const sandboxInvalidation = invalidateCalls.find(
+ (call) => call?.queryKey?.[0] === "sandboxes",
+ );
+ const vscodeInvalidation = invalidateCalls.find(
+ (call) =>
+ call?.queryKey?.[0] === "unified" &&
+ call?.queryKey?.[1] === "vscode_url",
+ );
+
+ expect(sandboxInvalidation).toBeDefined();
+ expect(vscodeInvalidation).toBeDefined();
+ });
+});
diff --git a/frontend/__tests__/hooks/mutation/use-v1-resume-conversation.test.tsx b/frontend/__tests__/hooks/mutation/use-v1-resume-conversation.test.tsx
new file mode 100644
index 0000000000..97ec162b54
--- /dev/null
+++ b/frontend/__tests__/hooks/mutation/use-v1-resume-conversation.test.tsx
@@ -0,0 +1,94 @@
+import { renderHook, waitFor } from "@testing-library/react";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { describe, expect, it, vi, beforeEach } from "vitest";
+import React from "react";
+import V1ConversationService from "#/api/conversation-service/v1-conversation-service.api";
+import { useV1ResumeConversation } from "#/hooks/mutation/use-v1-resume-conversation";
+
+describe("useV1ResumeConversation", () => {
+ let queryClient: QueryClient;
+
+ beforeEach(() => {
+ queryClient = new QueryClient({
+ defaultOptions: {
+ queries: { retry: false },
+ mutations: { retry: false },
+ },
+ });
+ vi.restoreAllMocks();
+ });
+
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
+ <QueryClientProvider client={queryClient}>
+ {children}
+ </QueryClientProvider>
+ );
+
+ it("invalidates sandbox and vscode_url queries on settled", async () => {
+ // Mock the API calls in the mutation chain
+ vi.spyOn(V1ConversationService, "batchGetAppConversations").mockResolvedValue([
+ {
+ id: "test-conv-id",
+ created_by_user_id: null,
+ sandbox_id: "test-sandbox-id",
+ conversation_url: "http://localhost:3000",
+ session_api_key: "test-key",
+ selected_repository: null,
+ selected_branch: null,
+ git_provider: null,
+ title: "Test",
+ public: false,
+ sandbox_status: "RUNNING",
+ execution_status: null,
+ trigger: null,
+ pr_number: [],
+ llm_model: null,
+ metrics: null,
+ sub_conversation_ids: [],
+ created_at: new Date().toISOString(),
+ updated_at: new Date().toISOString(),
+ },
+ ]);
+ vi.spyOn(V1ConversationService, "resumeConversation").mockResolvedValue({
+ success: true,
+ });
+
+ // Pre-populate query cache with stale sandbox data
+ queryClient.setQueryData(["sandboxes", "batch", ["test-sandbox-id"]], [
+ {
+ sandbox_id: "test-sandbox-id",
+ exposed_urls: [{ name: "VSCODE", url: "https://old-runtime.example.com" }],
+ },
+ ]);
+ queryClient.setQueryData(["unified", "vscode_url", "test-conv-id"], {
+ url: "https://old-runtime.example.com",
+ error: null,
+ });
+
+ // Spy on invalidateQueries
+ const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");
+
+ const { result } = renderHook(() => useV1ResumeConversation(), { wrapper });
+
+ // Trigger the mutation
+ result.current.mutate({ conversationId: "test-conv-id" });
+
+ await waitFor(() => {
+ expect(result.current.isSuccess || result.current.isError).toBe(true);
+ });
+
+ // Verify sandbox queries were invalidated
+ const invalidateCalls = invalidateSpy.mock.calls.map((call) => call[0]);
+ const sandboxInvalidation = invalidateCalls.find(
+ (call) => call?.queryKey?.[0] === "sandboxes",
+ );
+ const vscodeInvalidation = invalidateCalls.find(
+ (call) =>
+ call?.queryKey?.[0] === "unified" &&
+ call?.queryKey?.[1] === "vscode_url",
+ );
+
+ expect(sandboxInvalidation).toBeDefined();
+ expect(vscodeInvalidation).toBeDefined();
+ });
+});
diff --git a/frontend/src/hooks/mutation/conversation-mutation-utils.ts b/frontend/src/hooks/mutation/conversation-mutation-utils.ts
index 47d19271b3..9173266322 100644
--- a/frontend/src/hooks/mutation/conversation-mutation-utils.ts
+++ b/frontend/src/hooks/mutation/conversation-mutation-utils.ts
@@ -149,4 +149,8 @@ export const invalidateConversationQueries = (
queryClient.invalidateQueries({
queryKey: ["v1-batch-get-app-conversations"],
});
+ // Invalidate sandbox and VS Code URL caches to pick up new runtime URLs after resume
+ // Uses partial key matching to invalidate all sandbox-related queries (batch, individual, etc.)
+ queryClient.invalidateQueries({ queryKey: ["sandboxes"] });
+ queryClient.invalidateQueries({ queryKey: ["unified", "vscode_url"] });
};
diff --git a/frontend/src/hooks/mutation/use-v1-resume-conversation.ts b/frontend/src/hooks/mutation/use-v1-resume-conversation.ts
index 3c73f15982..b606b463c8 100644
--- a/frontend/src/hooks/mutation/use-v1-resume-conversation.ts
+++ b/frontend/src/hooks/mutation/use-v1-resume-conversation.ts
@@ -35,6 +35,10 @@ export const useV1ResumeConversation = () => {
queryClient.invalidateQueries({
queryKey: ["v1-batch-get-app-conversations"],
});
+ // Invalidate sandbox data so new runtime URLs are picked up after resume
+ queryClient.invalidateQueries({ queryKey: ["sandboxes"] });
+ // Invalidate VS Code URL cache so it refetches with the new sandbox data
+ queryClient.invalidateQueries({ queryKey: ["unified", "vscode_url"] });
},
});
};