diff --git a/ui/src/components/AgentSetupProgress.test.tsx b/ui/src/components/AgentSetupProgress.test.tsx index 02ca7e10ab5..44e1ad8c925 100644 --- a/ui/src/components/AgentSetupProgress.test.tsx +++ b/ui/src/components/AgentSetupProgress.test.tsx @@ -169,6 +169,44 @@ describe("agent setup progress", () => { const flush = () => act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); }); const token = () => container.querySelector('[data-testid="token"]')?.textContent; + it("keeps a finished checklist on screen when the app refreshes every query", async () => { + const done: AgentSetupProgress = { ...WAITING, status: "done", summary: "Role ARN: arn:aws:iam::123456789012:role/paperclip-conn-1", + items: [{ key: "create-role", title: "Create the role", status: "done", detail: null }] }; + const create = vi.fn().mockResolvedValue(HANDOFF); + let status: "waiting" | "done" = "waiting"; + const client: AgentSetupProgressClient = { create, get: vi.fn(async () => status === "done" ? done : WAITING) }; + await act(async () => { root.render(); }); + await flush(); + status = "done"; + await act(async () => { await queryClient.invalidateQueries({ queryKey: queryKeys.agentSetupProgress(ID) }); }); + await vi.waitFor(() => expect(container.textContent).toContain("Your agent set up AWS")); + // The live connection reconnects or the tab returns: the app refreshes every query on screen. + await act(async () => { await queryClient.invalidateQueries({ type: "active" }); }); + await flush(); + expect(create).toHaveBeenCalledOnce(); + expect(token()).toBe("pcsp_token"); + expect(container.textContent).toContain("Your agent set up AWS"); + expect(container.textContent).toContain("Role ARN: arn:aws:iam::123456789012:role/paperclip-conn-1"); + }); + + it("still creates a fresh checklist when Start again's first create fails and its retry succeeds", async () => { + const failedAttempt: AgentSetupProgress = { ...WAITING, status: "failed", summary: "Stopped: no permission", + items: [{ key: "deploy", title: "Create the role", status: "failed", detail: "iam:CreateRole denied" }] }; + const second: AgentSetupProgressHandoff = { ...HANDOFF, token: "pcsp_after_retry", progress: { ...WAITING, id: "88888888-8888-4888-8888-888888888888" } }; + const create = vi.fn().mockResolvedValueOnce(HANDOFF).mockRejectedValueOnce(new Error("offline")).mockResolvedValueOnce(second); + const client: AgentSetupProgressClient = { create, get: vi.fn(async (id: string) => id === ID ? failedAttempt : second.progress) }; + // The app's queries retry a failed request; this one retries once, at once. + const retrying = new QueryClient({ defaultOptions: { queries: { retry: 1, retryDelay: 0 } } }); + await act(async () => { root.render(); }); + await flush(); + await act(async () => { await retrying.invalidateQueries({ queryKey: queryKeys.agentSetupProgress(ID) }); }); + await vi.waitFor(() => expect(container.textContent).toContain("Your agent couldn’t finish setting up AWS")); + const startAgain = [...container.querySelectorAll("button")].find((b) => b.textContent === "Start again")!; + await act(async () => { startAgain.click(); }); + await vi.waitFor(() => expect(token()).toBe("pcsp_after_retry")); + expect(create).toHaveBeenCalledTimes(3); + }); + it("offers a fresh checklist after a failed attempt, and opening the step again later starts fresh", async () => { const failedAttempt: AgentSetupProgress = { ...WAITING, status: "failed", summary: "Stopped: no permission", items: [{ key: "deploy", title: "Create the role", status: "failed", detail: "iam:CreateRole denied" }] }; diff --git a/ui/src/components/AgentSetupProgress.tsx b/ui/src/components/AgentSetupProgress.tsx index d077052552d..00dda6d30d9 100644 --- a/ui/src/components/AgentSetupProgress.tsx +++ b/ui/src/components/AgentSetupProgress.tsx @@ -73,12 +73,21 @@ export function useAgentSetupProgress({ const accountFailed = !accountOverride && account.failed; const handoffKey = ["agent-setup-progress-handoff", accountId, companyId ?? null, purpose, subjectId ?? null] as const; const storageKey = agentSetupHandoffStorageKey(accountId, companyId, purpose, subjectId); + // Set by renew and kept until a new checklist is created, so a retry of a failed create still + // creates rather than returning the checklist being replaced. + const replacing = useRef(false); const handoffQuery = useQuery({ queryKey: handoffKey, queryFn: async () => { + // Any other refetch (the app refreshes every query on screen when its live connection + // reconnects or the tab returns) keeps the checklist on screen. A finished one is no longer + // in storage, so falling through here would replace it with a blank checklist. + const current = queryClient.getQueryData<{ handoff: AgentSetupProgressHandoff; fromEarlierVisit: boolean }>(handoffKey); + if (current && !replacing.current) return current; // A handoff found in this tab's storage came from an earlier visit; a created one is new. const stored = storedAgentSetupHandoff(storageKey); const handoff = stored ?? await client.create(companyId!, { purpose, ...(subjectId ? { subjectId } : {}) }); + replacing.current = false; storeAgentSetupHandoff(storageKey, handoff); return { handoff, fromEarlierVisit: Boolean(stored) }; }, @@ -115,6 +124,7 @@ export function useAgentSetupProgress({ return; } storeAgentSetupHandoff(storageKey, null); + replacing.current = true; void handoffQuery.refetch(); }; diff --git a/ui/src/lib/live-reconcile.test.ts b/ui/src/lib/live-reconcile.test.ts index 03ad50a9916..a2b7743f6f2 100644 --- a/ui/src/lib/live-reconcile.test.ts +++ b/ui/src/lib/live-reconcile.test.ts @@ -17,6 +17,7 @@ describe("live reconcile scope", () => { queryKeys.workspacePreferences.mine("user-1"), queryKeys.sidebarPreferences.companyOrder("user-1"), queryKeys.sidebarPreferences.projectOrder("company-1", "user-1"), + ["agent-setup-progress-handoff", "user-1", "company-1", "aws_connection", "conn-1"], ]) { expect(isLiveReconciledQuery({ queryKey }), JSON.stringify(queryKey)).toBe(false); } @@ -31,6 +32,8 @@ describe("live reconcile scope", () => { queryKeys.projects.all("company-1"), ["plugins", "company-1"], ["instance-admin"], + // The checklist's progress still reconciles; only its prompt token and URLs do not. + queryKeys.agentSetupProgress("progress-1"), ]) { expect(isLiveReconciledQuery({ queryKey }), JSON.stringify(queryKey)).toBe(true); } diff --git a/ui/src/lib/live-reconcile.ts b/ui/src/lib/live-reconcile.ts index c00da7bba36..14302cc480f 100644 --- a/ui/src/lib/live-reconcile.ts +++ b/ui/src/lib/live-reconcile.ts @@ -21,6 +21,9 @@ export const LIVE_EVENT_FREE_QUERY_KEYS: readonly QueryKey[] = [ queryKeys.plugins.uiContributions, ["workspace-preferences"], ["sidebar-preferences"], + // A setup checklist's prompt token and URLs: created once per setup, never changed by an event + // (the checklist's progress has its own key and does reconcile). + ["agent-setup-progress-handoff"], ]; /** How long the reads above stay fresh. A window-focus refetch runs at most this often. */