fix(serverless): clear React Query cache on workspace switch

Channel lists, message timelines, members, and agent lists are keyed in
React Query without the workspace id, so switching workspaces bled the
previous workspace's channels/data into the new one. resetWorkspaceState()
now calls queryClient.clear() alongside the existing module-singleton
resets, per the AGENTS.md workspace-switch contract.
This commit is contained in:
Michael Neale
2026-06-02 17:05:57 +10:00
parent 5afc0e0266
commit 10b8f06463
@@ -1,4 +1,5 @@
import { useEffect, useRef, useState } from "react";
import { type QueryClient, useQueryClient } from "@tanstack/react-query";
import { relayClient } from "@/shared/api/relayClient";
import {
@@ -20,12 +21,17 @@ import type { Workspace } from "./types";
* cache or singleton that holds workspace data, add its reset here.
* See AGENTS.md "Workspace Switching" for the full contract.
*/
function resetWorkspaceState(): void {
function resetWorkspaceState(queryClient: QueryClient): void {
relayClient.disconnect();
resetAgentObserverStore();
resetMediaCaches();
clearSearchHitEventCache();
clearAllDrafts();
// Clear ALL React Query caches. Channel lists, message timelines, members,
// agents, etc. are keyed without the workspace id, so without this the
// previous workspace's data (e.g. its channels) bleeds into the new one on
// switch. The key-based remount of <AppReady> refetches everything fresh.
queryClient.clear();
}
type WorkspaceInitResult =
@@ -56,6 +62,8 @@ export function useWorkspaceInit(
appliedKey: null,
});
const queryClient = useQueryClient();
// Track whether this is the initial mount or a workspace switch.
// On the initial mount we skip resetting singletons (they're fresh).
const hasInitializedRef = useRef(false);
@@ -111,7 +119,7 @@ export function useWorkspaceInit(
// On workspace switch (not initial mount), reset module singletons
// so the new tree starts with a clean slate.
if (hasInitializedRef.current) {
resetWorkspaceState();
resetWorkspaceState(queryClient);
}
hasInitializedRef.current = true;