Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(runtimes): verify new computers before showing connection success (#9688)
Frezca6173f8ebc34
diff --git a/packages/views/runtimes/components/connect-remote-dialog.test.tsx b/packages/views/runtimes/components/connect-remote-dialog.test.tsxindex be9bc91f4e..d2b1f4f189 100644--- a/packages/views/runtimes/components/connect-remote-dialog.test.tsx+++ b/packages/views/runtimes/components/connect-remote-dialog.test.tsx@@ -1,3 +1,4 @@+import type { ReactNode } from "react";import { QueryClient, QueryClientProvider } from "@tanstack/react-query";import { act, render, screen, waitFor } from "@testing-library/react";import userEvent from "@testing-library/user-event";@@ -5,12 +6,40 @@ import { beforeEach, describe, expect, it, vi } from "vitest";import { I18nProvider } from "@multica/core/i18n/react";import { configStore } from "@multica/core/config";import type { DaemonConfigStatus, DaemonSetupTarget } from "@multica/core/runtimes";+import { runtimeKeys } from "@multica/core/runtimes/queries";+import type { AgentRuntime } from "@multica/core/types";import enCommon from "../../locales/en/common.json";import enRuntimes from "../../locales/en/runtimes.json";import { ConnectRemoteDialog } from "./connect-remote-dialog";const TEST_RESOURCES = { en: { common: enCommon, runtimes: enRuntimes } };+const WS_ID = "11111111-1111-4111-8111-111111111111";+const USER_ID = "user-test";+const workspaceState = vi.hoisted(() => ({+ id: "11111111-1111-4111-8111-111111111111",+ slug: "workspace-test",+}));+const authState = vi.hoisted(() => ({ user: { id: "user-test" } as { id: string } | null }));+const runtimeApi = vi.hoisted(() => ({ listRuntimes: vi.fn<() => Promise<AgentRuntime[]>>() }));+vi.mock("@multica/core/api", () => ({ api: runtimeApi }));+vi.mock("@multica/core/auth", () => ({+ useAuthStore: Object.assign(+ (select: (state: typeof authState) => unknown) => select(authState),+ { getState: () => authState },+ ),+}));++// Keep connection assertions independent of exit animation timing.+vi.mock("motion/react", async (importOriginal) => ({+ ...await importOriginal<typeof import("motion/react")>(),+ AnimatePresence: ({ children }: { children: ReactNode }) => children,+ motion: {+ div: ({ children, className }: { children: ReactNode; className?: string }) => <div className={className}>{children}</div>,+ },+}));++// Mocked at the module boundary rather than through navigator.clipboard: jsdom// exposes no clipboard, and user-event installs a getter-only stub of its own// that would swallow the assertion. Assert what the button hands to copyText,@@ -22,21 +51,22 @@ const clipboard = vi.hoisted(() => ({vi.mock("@multica/ui/lib/clipboard", () => ({ copyText: clipboard.copyText }));vi.mock("@multica/core/hooks", () => ({- useWorkspaceId: () => "ws-test",+ useWorkspaceId: () => workspaceState.id,}));vi.mock("@multica/core/paths", () => ({paths: {workspace: () => ({agents: () => "/agents",- runtimeDetail: () => "/runtimes/rt-test",+ runtimeDetail: (id: string) => `/runtimes/${id}`,}),},- useWorkspaceSlug: () => "workspace-test",+ useWorkspaceSlug: () => workspaceState.slug,}));const wsEventState = vi.hoisted(() => ({handler: null as ((payload: unknown) => void) | null,+ reconnect: null as (() => void) | null,}));const WINDOWS_CMD =@@ -46,12 +76,44 @@ vi.mock("@multica/core/realtime", () => ({useWSEvent: (_event: string, handler: (payload: unknown) => void) => {wsEventState.handler = handler;},+ useWSReconnect: (handler: () => void) => { wsEventState.reconnect = handler; },}));+const navigationState = vi.hoisted(() => ({ push: vi.fn() }));vi.mock("../../navigation", () => ({- useNavigation: () => ({ push: vi.fn() }),+ useNavigation: () => ({ push: navigationState.push }),}));+function makeRuntime(overrides: Partial<AgentRuntime> = {}): AgentRuntime {+ return {+ id: "rt-new",+ workspace_id: WS_ID,+ daemon_id: "daemon-new",+ name: "Claude (new-machine)",+ runtime_mode: "local",+ provider: "claude",+ launch_header: "",+ status: "online",+ device_info: "new-machine",+ metadata: {},+ owner_id: USER_ID,+ visibility: "private",+ last_seen_at: null,+ created_at: "2026-10-29T00:00:00Z",+ updated_at: "2026-10-29T00:00:00Z",+ ...overrides,+ };+}++async function refreshRuntimes(qc: QueryClient, payload: unknown) {+ const calls = runtimeApi.listRuntimes.mock.calls.length;+ await act(async () => {+ await wsEventState.handler?.(payload);+ });+ await waitFor(() => expect(runtimeApi.listRuntimes.mock.calls.length).toBeGreaterThan(calls));+ await waitFor(() => expect(qc.isFetching()).toBe(0));+}+function resetConfigStore() {configStore.setState({cdnDomain: "",@@ -69,22 +131,36 @@ function renderDialog(config?: {daemonServerUrl?: string;daemonAppUrl?: string;daemonSetupTarget?: DaemonSetupTarget;-}, status: DaemonConfigStatus = "ready") {+}, status: DaemonConfigStatus = "ready", qc = new QueryClient({ defaultOptions: { queries: { retry: false } } })) {resetConfigStore();configStore.getState().setDaemonConfig(config ?? { daemonSetupTarget: "cloud" });configStore.getState().setDaemonConfigStatus(status);- const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });- return render(+ const dialog = () => (<QueryClientProvider client={qc}><I18nProvider locale="en" resources={TEST_RESOURCES}><ConnectRemoteDialog onClose={vi.fn()} /></I18nProvider>- </QueryClientProvider>,+ </QueryClientProvider>);+ const view = render(dialog());+ return { ...view, qc, rerenderDialog: () => view.rerender(dialog()) };+}++async function renderListeningDialog(runtimes: AgentRuntime[] = []) {+ runtimeApi.listRuntimes.mockResolvedValue(runtimes);+ const view = renderDialog();+ await waitFor(() => expect(view.qc.getQueryData(runtimeKeys.listMine(WS_ID))).toEqual(runtimes));+ return view;}describe("ConnectRemoteDialog", () => {beforeEach(() => {+ workspaceState.id = WS_ID;+ workspaceState.slug = "workspace-test";+ authState.user = { id: USER_ID };+ runtimeApi.listRuntimes.mockReset().mockResolvedValue([]);+ navigationState.push.mockReset();+ wsEventState.reconnect = null;wsEventState.handler = null;clipboard.copyText.mockReset().mockResolvedValue(true);});@@ -174,13 +250,14 @@ describe("ConnectRemoteDialog", () => {});});- it("transitions from setup instructions to the connected state", async () => {- const { baseElement } = renderDialog();+ it("confirms a newly registered own machine and opens its verified runtime", async () => {+ const user = userEvent.setup();+ const { baseElement, qc } = await renderListeningDialog();expect(baseElement).toHaveTextContent("multica setup");- act(() => {- wsEventState.handler?.({ runtime_id: "rt-test" });- });+ const runtime = makeRuntime();+ runtimeApi.listRuntimes.mockResolvedValue([runtime]);+ await refreshRuntimes(qc, { runtimes: [runtime] });await waitFor(() => {expect(screen.getByText("Machine connected")).toBeInTheDocument();@@ -189,5 +266,230 @@ describe("ConnectRemoteDialog", () => {).toBeInTheDocument();});expect(baseElement).not.toHaveTextContent("multica setup");+ expect(runtimeApi.listRuntimes).toHaveBeenCalledWith({ workspace_id: WS_ID, owner: "me" }, "workspace-test");+ await user.click(screen.getByRole("button", { name: "View runtime" }));+ expect(navigationState.push).toHaveBeenCalledWith("/runtimes/rt-new");+ });++ it.each([+ { action: "update" },+ { action: "deregister" },+ { action: "stale_sweep" },+ { action: "heartbeat_recovery" },+ { runtime_profile_id: "existing-profile" },+ { runtime_id: "unverified-runtime" },+ { runtimes: [makeRuntime()] },+ null,+ ])("keeps instructions for an event without a new verified machine: %j", async (payload) => {+ const { qc } = await renderListeningDialog();+ await refreshRuntimes(qc, payload);+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ expect(screen.getByText("multica setup")).toBeInTheDocument();+ });++ it("does not report success from an offline notification alone", async () => {+ renderDialog();+ await act(async () => { wsEventState.handler?.({ action: "stale_sweep" }); });+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ });++ it("does not treat an existing machine's recovery or new provider as a new connection", async () => {+ const runtime = makeRuntime({ status: "offline" });+ const { qc } = await renderListeningDialog([runtime]);+ runtimeApi.listRuntimes.mockResolvedValue([+ { ...runtime, status: "online" },+ makeRuntime({ id: "rt-another-provider", provider: "codex" }),+ ]);+ await refreshRuntimes(qc, { runtimes: [runtime] });+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ });++ it.each([+ { owner_id: "another-member" },+ { owner_id: null },+ { runtime_mode: "cloud" as const },+ { status: "offline" as const },+ { daemon_id: null },+ { workspace_id: "another-workspace" },+ { metadata: { pending_custom_runtime: true } },+ { metadata: { runtime_profile_registration_error: true } },+ ])("does not complete for an ineligible runtime: %j", async (overrides) => {+ const { qc } = await renderListeningDialog();+ const runtime = makeRuntime(overrides);+ runtimeApi.listRuntimes.mockResolvedValue([runtime]);+ await refreshRuntimes(qc, { runtimes: [runtime] });+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ });++ it("refreshes after reconnect to detect a registration whose event was missed", async () => {+ const { qc } = await renderListeningDialog();+ runtimeApi.listRuntimes.mockResolvedValue([makeRuntime()]);+ await act(async () => {+ await wsEventState.reconnect?.();+ });+ await waitFor(() => expect(qc.isFetching()).toBe(0));+ expect(await screen.findByText("Machine connected")).toBeInTheDocument();+ });++ it.each(["registration", "reconnect"] as const)(+ "replays %s notifications received during the uncached initial fetch",+ async (event) => {+ let finishInitialFetch!: (runtimes: AgentRuntime[]) => void;+ let finishRefresh!: (runtimes: AgentRuntime[]) => void;+ runtimeApi.listRuntimes+ .mockImplementationOnce(() => new Promise((resolve) => { finishInitialFetch = resolve; }))+ .mockImplementationOnce(() => new Promise((resolve) => { finishRefresh = resolve; }));+ const { qc } = renderDialog();+ await waitFor(() => expect(runtimeApi.listRuntimes).toHaveBeenCalledTimes(1));+ expect(qc.getQueryData(runtimeKeys.listMine(WS_ID))).toBeUndefined();++ await act(async () => {+ for (let i = 0; i < 3; i++) {+ if (event === "registration") wsEventState.handler?.({ action: "update" });+ else wsEventState.reconnect?.();+ }+ });+ expect(runtimeApi.listRuntimes).toHaveBeenCalledTimes(1);++ // The in-flight request captured the list before the machine registered.+ await act(async () => { finishInitialFetch([]); });+ await waitFor(() => expect(runtimeApi.listRuntimes).toHaveBeenCalledTimes(2));+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();++ await act(async () => { finishRefresh([makeRuntime()]); });+ expect(await screen.findByText("Machine connected")).toBeInTheDocument();+ expect(runtimeApi.listRuntimes).toHaveBeenCalledTimes(2);+ await userEvent.setup().click(screen.getByRole("button", { name: "View runtime" }));+ expect(navigationState.push).toHaveBeenCalledWith("/runtimes/rt-new");+ },+ );++ it("replays a queued refresh after an initial failure and establishes a fresh baseline", async () => {+ let failInitialFetch!: (error: Error) => void;+ const existing = makeRuntime({ id: "rt-existing", daemon_id: "daemon-existing" });+ runtimeApi.listRuntimes+ .mockImplementationOnce(() => new Promise((_resolve, reject) => { failInitialFetch = reject; }))+ .mockResolvedValue([existing]);+ const { qc } = renderDialog();+ await waitFor(() => expect(runtimeApi.listRuntimes).toHaveBeenCalledTimes(1));+ await act(async () => { wsEventState.handler?.({ action: "update" }); });+ await act(async () => { failInitialFetch(new Error("initial query failed")); });++ await waitFor(() => expect(runtimeApi.listRuntimes).toHaveBeenCalledTimes(2));+ await waitFor(() => expect(qc.isFetching()).toBe(0));+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ runtimeApi.listRuntimes.mockResolvedValue([existing, makeRuntime()]);+ await refreshRuntimes(qc, { action: "update" });+ expect(await screen.findByText("Machine connected")).toBeInTheDocument();+ expect(runtimeApi.listRuntimes).toHaveBeenCalledTimes(3);+ });++ it("waits for a successful initial fetch instead of trusting stale cached runtimes", async () => {+ const qc = new QueryClient({ defaultOptions: { queries: { retry: false, staleTime: Infinity } } });+ qc.setQueryData(runtimeKeys.listMine(WS_ID), []);+ let finishFetch!: (runtimes: AgentRuntime[]) => void;+ runtimeApi.listRuntimes.mockReturnValue(new Promise((resolve) => { finishFetch = resolve; }));+ renderDialog(undefined, "ready", qc);+ await act(async () => { finishFetch([makeRuntime()]); });+ await waitFor(() => expect(qc.isFetching()).toBe(0));+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ runtimeApi.listRuntimes.mockResolvedValue([makeRuntime()]);+ await refreshRuntimes(qc, { action: "update" });+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ });++ it("does not report success when the runtime refresh fails and recovers on a later refresh", async () => {+ const { qc } = await renderListeningDialog();+ runtimeApi.listRuntimes.mockRejectedValue(new Error("runtime query failed"));+ await refreshRuntimes(qc, { runtime_id: "unverified-runtime" });+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ runtimeApi.listRuntimes.mockResolvedValue([makeRuntime()]);+ await refreshRuntimes(qc, { action: "update" });+ expect(await screen.findByText("Machine connected")).toBeInTheDocument();+ });++ it("establishes its baseline only after the initial query recovers", async () => {+ runtimeApi.listRuntimes.mockRejectedValue(new Error("initial query failed"));+ const { qc } = renderDialog();+ await waitFor(() => expect(qc.getQueryState(runtimeKeys.listMine(WS_ID))?.status).toBe("error"));+ runtimeApi.listRuntimes.mockResolvedValue([makeRuntime()]);+ await refreshRuntimes(qc, { action: "update" });+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ await refreshRuntimes(qc, { action: "heartbeat_recovery" });+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ });++ it("completes when a new offline machine is subsequently confirmed online", async () => {+ const { qc } = await renderListeningDialog();+ runtimeApi.listRuntimes.mockResolvedValue([makeRuntime({ status: "offline" })]);+ await refreshRuntimes(qc, { action: "update" });+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ runtimeApi.listRuntimes.mockResolvedValue([makeRuntime()]);+ await refreshRuntimes(qc, { action: "heartbeat_recovery" });+ expect(await screen.findByText("Machine connected")).toBeInTheDocument();+ });++ it("keeps an empty workspace waiting through repeated background updates", async () => {+ const { qc } = await renderListeningDialog();+ for (const payload of [+ { action: "update" },+ { action: "update" },+ { action: "heartbeat_recovery" },+ { runtime_profile_id: "existing-profile" },+ { runtime_profile_id: "existing-profile" },+ { runtimes: [makeRuntime({ owner_id: "another-member" })] },+ ]) {+ await refreshRuntimes(qc, payload);+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ }+ });++ it("takes a fresh baseline when an open dialog moves to another workspace", async () => {+ const view = await renderListeningDialog();+ const nextWorkspace = "22222222-2222-4222-8222-222222222222";+ const existing = makeRuntime({ workspace_id: nextWorkspace });+ runtimeApi.listRuntimes.mockResolvedValue([existing]);+ workspaceState.id = nextWorkspace;+ workspaceState.slug = "another-workspace";+ view.rerenderDialog();+ await waitFor(() => expect(view.qc.getQueryData(runtimeKeys.listMine(nextWorkspace))).toEqual([existing]));+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ expect(runtimeApi.listRuntimes).toHaveBeenLastCalledWith({ workspace_id: nextWorkspace, owner: "me" }, "another-workspace");++ await refreshRuntimes(view.qc, { action: "update" });+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ });++ it("clears a previous workspace's success when switching to an unregistered workspace", async () => {+ const view = await renderListeningDialog();+ runtimeApi.listRuntimes.mockResolvedValue([makeRuntime()]);+ await refreshRuntimes(view.qc, { runtimes: [makeRuntime()] });+ expect(await screen.findByText("Machine connected")).toBeInTheDocument();++ const nextWorkspace = "22222222-2222-4222-8222-222222222222";+ runtimeApi.listRuntimes.mockResolvedValue([]);+ workspaceState.id = nextWorkspace;+ workspaceState.slug = "empty-workspace";+ view.rerenderDialog();+ await waitFor(() => expect(view.qc.getQueryData(runtimeKeys.listMine(nextWorkspace))).toEqual([]));+ await refreshRuntimes(view.qc, { runtime_profile_id: "existing-profile" });+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();+ expect(screen.getByText("multica setup")).toBeInTheDocument();+ });+++ it("clears connection success and establishes a new baseline when the account changes", async () => {+ const view = await renderListeningDialog();+ runtimeApi.listRuntimes.mockResolvedValue([makeRuntime()]);+ await refreshRuntimes(view.qc, { action: "update" });+ expect(await screen.findByText("Machine connected")).toBeInTheDocument();++ authState.user = { id: "another-user" };+ const existing = makeRuntime({ id: "rt-other-account", daemon_id: "daemon-other-account", owner_id: "another-user" });+ runtimeApi.listRuntimes.mockResolvedValue([existing]);+ view.rerenderDialog();+ await waitFor(() => expect(view.qc.getQueryData(runtimeKeys.listMine(WS_ID))).toEqual([existing]));+ await refreshRuntimes(view.qc, { runtime_profile_id: "existing-profile" });+ expect(screen.queryByText("Machine connected")).not.toBeInTheDocument();});});diff --git a/packages/views/runtimes/components/connect-remote-dialog.tsx b/packages/views/runtimes/components/connect-remote-dialog.tsxindex 120d6ae993..90948abd7f 100644--- a/packages/views/runtimes/components/connect-remote-dialog.tsx+++ b/packages/views/runtimes/components/connect-remote-dialog.tsx@@ -9,11 +9,12 @@ import {} from "react";import { AnimatePresence, motion, useReducedMotion } from "motion/react";import { Check, ChevronRight, Copy, Terminal } from "@multica/ui/icons";-import { useQueryClient } from "@tanstack/react-query";+import { useQuery, useQueryClient } from "@tanstack/react-query";+import { useAuthStore } from "@multica/core/auth";import { useWorkspaceId } from "@multica/core/hooks";-import { runtimeKeys } from "@multica/core/runtimes/queries";+import { runtimeKeys, runtimeListOptions } from "@multica/core/runtimes/queries";import { useDaemonSetupCommands } from "@multica/core/runtimes";-import { useWSEvent } from "@multica/core/realtime";+import { useWSEvent, useWSReconnect } from "@multica/core/realtime";import { paths, useWorkspaceSlug } from "@multica/core/paths";import {Dialog,@@ -40,30 +41,89 @@ import { useT } from "../../i18n";type Step = "instructions" | "success";export function ConnectRemoteDialog({ onClose }: { onClose: () => void }) {- const [step, setStep] = useState<Step>("instructions");const wsId = useWorkspaceId();+ const currentUserId = useAuthStore((state) => state.user?.id);+ return (+ <WorkspaceConnectDialog+ key={`${wsId}:${currentUserId ?? ""}`}+ wsId={wsId}+ currentUserId={currentUserId}+ onClose={onClose}+ />+ );+}++function WorkspaceConnectDialog({ wsId, currentUserId, onClose }: {+ wsId: string;+ currentUserId: string | undefined;+ onClose: () => void;+}) {+ const [step, setStep] = useState<Step>("instructions");const slug = useWorkspaceSlug();const qc = useQueryClient();const navigation = useNavigation();const shouldReduceMotion = useReducedMotion() ?? false;const newRuntimeIdRef = useRef<string | null>(null);+ const initialDaemonIds = useRef<Set<string> | null>(null);+ const pendingInitialRefresh = useRef(false);+ const { data: runtimes, isSuccess, isFetchedAfterMount, isFetching } = useQuery({+ ...runtimeListOptions(wsId, "me", slug ?? undefined),+ enabled: !!currentUserId && step === "instructions",+ refetchOnMount: "always",+ });- // `multica setup` is one blocking command that handles config + login- // + daemon start; the dialog passively listens for the resulting- // `daemon:register` WS event and auto-advances to success.- const handleDaemonRegister = useCallback(- (payload: unknown) => {- if (step !== "instructions") return;- qc.invalidateQueries({ queryKey: runtimeKeys.all(wsId) });- const p = payload as Record<string, unknown> | null;- if (p?.runtime_id && typeof p.runtime_id === "string") {- newRuntimeIdRef.current = p.runtime_id;- }- setStep("success");- },- [step, qc, wsId],- );+ // Registration, configuration updates and offline sweeps share this event.+ // Only the refreshed runtime list can confirm a newly connected computer.+ const handleDaemonRegister = useCallback(() => {+ if (step !== "instructions") return;+ // Invalidating an uncached in-flight query only joins its stale snapshot.+ if (!initialDaemonIds.current && qc.isFetching({ queryKey: runtimeKeys.listMine(wsId), exact: true })) {+ pendingInitialRefresh.current = true;+ return;+ }+ return qc.invalidateQueries({ queryKey: runtimeKeys.all(wsId) });+ }, [step, qc, wsId]);useWSEvent("daemon:register", handleDaemonRegister);+ useWSReconnect(handleDaemonRegister);++ useEffect(() => {+ if (+ step !== "instructions" ||+ !currentUserId ||+ !isSuccess ||+ !isFetchedAfterMount ||+ isFetching ||+ !runtimes+ ) return;+ // A fresh initial response avoids treating an old, uncached machine as new.+ const knownDaemonIds = initialDaemonIds.current;+ if (!knownDaemonIds) {+ initialDaemonIds.current = new Set(+ runtimes.flatMap((runtime) => runtime.daemon_id ? [runtime.daemon_id] : []),+ );+ return;+ }+ const runtime = runtimes.find((runtime) =>+ runtime.workspace_id === wsId &&+ runtime.owner_id === currentUserId &&+ runtime.runtime_mode === "local" &&+ runtime.status === "online" &&+ runtime.daemon_id &&+ !knownDaemonIds.has(runtime.daemon_id) &&+ runtime.metadata.pending_custom_runtime !== true &&+ runtime.metadata.runtime_profile_registration_error !== true,+ );+ if (!runtime) return;+ newRuntimeIdRef.current = runtime.id;+ setStep("success");+ }, [step, currentUserId, isSuccess, isFetchedAfterMount, isFetching, runtimes, wsId]);++ useEffect(() => {+ if (step !== "instructions" || isFetching || !pendingInitialRefresh.current) return;+ // Establish the initial baseline above before fetching queued notifications.+ pendingInitialRefresh.current = false;+ void qc.invalidateQueries({ queryKey: runtimeKeys.all(wsId) });+ }, [step, isFetching, qc, wsId]);const handleGoToAgents = () => {onClose();