Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(desktop): keep the workspace singleton across a same-workspace tab swap (MUL-6303) (#7092)

Bohan Jiangf66b0ba6313b
diff --git a/apps/desktop/src/renderer/src/components/workspace-route-layout.test.tsx b/apps/desktop/src/renderer/src/components/workspace-route-layout.test.tsx
index fb03537e33..d828c9ff87 100644
--- a/apps/desktop/src/renderer/src/components/workspace-route-layout.test.tsx
+++ b/apps/desktop/src/renderer/src/components/workspace-route-layout.test.tsx
@@ -1,3 +1,4 @@
+import { useEffect } from "react";
import { describe, expect, it, vi, beforeEach } from "vitest";
import { act, render, waitFor } from "@testing-library/react";
import { MemoryRouter, Route, Routes } from "react-router-dom";
@@ -16,6 +17,7 @@ const state = vi.hoisted(() => ({
modalAriaLabel: "source-backfill-modal-marker",
currentSlug: null as string | null,
pendingDeletes: new Set<string>(),
+ childQuerySlugs: [] as (string | null)[],
}));
vi.mock("@multica/core/auth", () => {
@@ -30,8 +32,14 @@ vi.mock("@multica/core/auth", () => {
// A real-enough stand-in for the platform singleton: the layout both writes
// and reads it (the clear paths check `getCurrentSlug()` before wiping), so a
// bare vi.fn() would make every guard trivially true.
+// The slug-equality early return mirrors the real singleton
+// (packages/core/platform/workspace-storage.ts). It is load-bearing for the
+// tab-swap case below: the incoming layout of a same-workspace swap writes the
+// slug that is already there, so its write is a no-op and cannot be what stops
+// the outgoing cleanup from clearing it.
vi.mock("@multica/core/platform", () => ({
setCurrentWorkspace: vi.fn((slug: string | null) => {
+ if (state.currentSlug === slug) return;
state.currentSlug = slug;
}),
getCurrentSlug: () => state.currentSlug,
@@ -149,6 +157,7 @@ beforeEach(() => {
state.modalRenders = 0;
state.currentSlug = null;
state.pendingDeletes = new Set<string>();
+ state.childQuerySlugs = [];
});
describe("WorkspaceRouteLayout", () => {
@@ -244,3 +253,88 @@ describe("WorkspaceRouteLayout workspace singleton lifecycle", () => {
expect(state.currentSlug).toBe("other");
});
});
+
+/**
+ * MUL-6303 / #7086. Desktop mounts exactly one tab at a time and keys the host
+ * on the active tab id (tab-content.tsx), so opening or switching a tab
+ * unmounts the whole router subtree and builds a new one beside it. React
+ * renders the incoming tree first and only then runs the outgoing tree's
+ * effect cleanups — so the release path above has to survive a successor that
+ * has already taken over.
+ *
+ * The guard it shipped with (slug equality) could not see that successor when
+ * both tabs belonged to the SAME workspace: the "+" button opens
+ * /<slug>/issues in the active workspace, so the incoming layout carried the
+ * same slug, its write deduped to a no-op, and the outgoing cleanup cleared
+ * the singleton out from under it. Every request the new tab fired then went
+ * out without X-Workspace-Slug and came back 400, and the shell dropped its
+ * sidebar — until the app was relaunched.
+ */
+describe("WorkspaceRouteLayout across a tab swap", () => {
+ // Records the singleton as a child sees it. Real workspace-scoped queries
+ // fire from effects exactly like this one, which is why the ordering matters:
+ // an outgoing cleanup that clears the singleton still lands BEFORE the
+ // incoming tab's first request goes out.
+ function ChildQuery() {
+ useEffect(() => {
+ state.childQuerySlugs.push(state.currentSlug);
+ }, []);
+ return <div data-testid="outlet" />;
+ }
+
+ // Mirrors ActiveTabHost: one tab mounted at a time, keyed by tab id.
+ function TabHost({ slug }: { slug: string }) {
+ const qc = new QueryClient({
+ defaultOptions: { queries: { retry: false, gcTime: 0 } },
+ });
+ qc.setQueryData(["workspace-by-slug"], state.workspace);
+ qc.setQueryData(["workspace-list"], state.wsList);
+ return (
+ <QueryClientProvider client={qc}>
+ <MemoryRouter initialEntries={[`/${slug}/issues`]}>
+ <Routes>
+ <Route path=":workspaceSlug/*" element={<WorkspaceRouteLayout />}>
+ <Route path="*" element={<ChildQuery />} />
+ </Route>
+ </Routes>
+ </MemoryRouter>
+ </QueryClientProvider>
+ );
+ }
+
+ it("keeps the workspace when the incoming tab is in the SAME workspace", () => {
+ const { rerender } = render(<TabHost key="tab-1" slug="acme" />);
+ expect(state.currentSlug).toBe("acme");
+
+ rerender(<TabHost key="tab-2" slug="acme" />);
+
+ expect(state.currentSlug).toBe("acme");
+ // Both tabs' first requests carried a workspace. The second entry is the
+ // one that used to be null, which is what produced the 400s.
+ expect(state.childQuerySlugs).toEqual(["acme", "acme"]);
+ });
+
+ it("adopts the incoming workspace when the tab is in a DIFFERENT workspace", () => {
+ state.wsList = [
+ { id: "ws-1", slug: "acme" },
+ { id: "ws-2", slug: "globex" },
+ ];
+ const { rerender } = render(<TabHost key="tab-1" slug="acme" />);
+ expect(state.currentSlug).toBe("acme");
+
+ state.workspace = { id: "ws-2", slug: "globex" };
+ rerender(<TabHost key="tab-2" slug="globex" />);
+
+ expect(state.currentSlug).toBe("globex");
+ expect(state.childQuerySlugs).toEqual(["acme", "globex"]);
+ });
+
+ it("still releases the workspace when the last tab goes away", () => {
+ const { unmount } = render(<TabHost key="tab-1" slug="acme" />);
+ expect(state.currentSlug).toBe("acme");
+
+ unmount();
+
+ expect(state.currentSlug).toBeNull();
+ });
+});
diff --git a/apps/desktop/src/renderer/src/components/workspace-route-layout.tsx b/apps/desktop/src/renderer/src/components/workspace-route-layout.tsx
index 2fb8b44dcc..088cf36444 100644
--- a/apps/desktop/src/renderer/src/components/workspace-route-layout.tsx
+++ b/apps/desktop/src/renderer/src/components/workspace-route-layout.tsx
@@ -1,4 +1,4 @@
-import { useEffect } from "react";
+import { useEffect, useState } from "react";
import { Outlet, useParams } from "react-router-dom";
import { useQuery } from "@tanstack/react-query";
import { WorkspaceSlugProvider } from "@multica/core/paths";
@@ -16,6 +16,19 @@ import { SourceBackfillModal } from "@multica/views/onboarding";
import { useTabStore } from "@/stores/tab-store";
import { useWindowOverlayStore } from "@/stores/window-overlay-store";
+/**
+ * Which mounted layout instance currently owns the platform workspace
+ * singleton. Claimed during render (next to the setCurrentWorkspace write it
+ * guards) and read by the unmount cleanup, which must not release a singleton
+ * a successor has already taken over (MUL-6303 / #7086).
+ *
+ * Module scope rather than context: the two instances that have to agree here
+ * never share a React tree. Desktop mounts exactly one tab at a time and keys
+ * the host on the active tab id (tab-content.tsx), so switching or opening a
+ * tab tears the whole router subtree down and builds a new one beside it.
+ */
+let singletonOwner: symbol | null = null;
+
/**
* Desktop equivalent of apps/web/app/[workspaceSlug]/layout.tsx.
*
@@ -35,6 +48,11 @@ import { useWindowOverlayStore } from "@/stores/window-overlay-store";
*/
export function WorkspaceRouteLayout() {
const { workspaceSlug } = useParams<{ workspaceSlug: string }>();
+ // Identity for this mounted instance. A Symbol so two layouts can never
+ // compare equal — including the incoming and outgoing layout of a
+ // same-workspace tab swap, which carry the same slug and workspace id and
+ // are therefore indistinguishable by value.
+ const [instanceId] = useState(() => Symbol("workspace-route-layout"));
const user = useAuthStore((s) => s.user);
const isAuthLoading = useAuthStore((s) => s.isLoading);
// While a WindowOverlay is open (onboarding, accept-invite, new-workspace),
@@ -81,7 +99,13 @@ export function WorkspaceRouteLayout() {
// while the deleted workspace is STILL in the list cache (the invalidation
// refetch is a network round-trip). Without the guard we write the dead slug
// straight back over the cleanup.
+ //
+ // Claiming ownership here — in the same render pass, before any cleanup can
+ // run — is what makes the unmount release below safe. React renders the
+ // incoming tree before it runs the outgoing tree's effect cleanups, so by
+ // the time the old layout tries to release, the new one already owns.
if (workspace && workspaceSlug && !isWorkspaceDeletePending(workspace.id)) {
+ singletonOwner = instanceId;
setCurrentWorkspace(workspaceSlug, workspace.id);
}
@@ -121,12 +145,29 @@ export function WorkspaceRouteLayout() {
setCurrentWorkspace(null, null);
}, [listReady, workspace, workspaceSlug]);
+ // The ownership check is what keeps a same-workspace tab swap from wiping
+ // the workspace context (MUL-6303 / #7086). The slug check alone cannot see
+ // that swap: the "+" button opens /<slug>/issues in the ACTIVE workspace
+ // (tab-bar.tsx), so the incoming layout carries the SAME slug, its render
+ // write dedupes to a no-op inside setCurrentWorkspace, and the outgoing
+ // cleanup then found getCurrentSlug() still equal to its own slug and
+ // cleared it. Every workspace-scoped request the new tab fired went out
+ // without X-Workspace-Slug and came back 400, and the shell — which gates
+ // its chrome on the same singleton — dropped the sidebar with it. Nothing
+ // re-ran the render write afterwards, so it stayed broken until relaunch.
+ //
+ // Both checks still earn their place: ownership covers a successor that
+ // shares this slug, and the slug check covers this same instance being
+ // re-run for a new slug (the effect's own dependency change), where the
+ // singleton has already moved on and must not be clobbered.
useEffect(() => {
return () => {
+ if (singletonOwner !== instanceId) return;
if (getCurrentSlug() !== workspaceSlug) return;
+ singletonOwner = null;
setCurrentWorkspace(null, null);
};
- }, [workspaceSlug]);
+ }, [workspaceSlug, instanceId]);
if (isAuthLoading) return null;
if (!user) return null;