Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

perf(community): exact-invalidate the servers() list so a mention can't cascade-refetch the server subtree

Shelly#38633d30e4447352
diff --git a/src/web/src/hooks/community/use-community-ws.ts b/src/web/src/hooks/community/use-community-ws.ts
index 1f2e17e38..f755d4276 100644
--- a/src/web/src/hooks/community/use-community-ws.ts
+++ b/src/web/src/hooks/community/use-community-ws.ts
@@ -697,7 +697,11 @@ export function useCommunityWs(options?: UseCommunityWsOptions) {
: prev,
)
} else {
- void queryClient.invalidateQueries({ queryKey: communityKeys.servers() })
+ // Refresh the rail LIST only (drop the deleted server). `exact`
+ // so this doesn't cascade-refetch every other server's nested
+ // detail subtree; the deleted server's own subtree is cleared by
+ // the removeQueries below.
+ void queryClient.invalidateQueries({ queryKey: communityKeys.servers(), exact: true })
queryClient.removeQueries({ queryKey: communityKeys.server(event.serverId) })
// #10: if the deleted server is the one the viewer is looking at,
// the store pointers now dangle — reset them so the UI drops back
@@ -813,7 +817,10 @@ export function useCommunityWs(options?: UseCommunityWsOptions) {
// so the layout's eject effect can detect the drop and route
// the user away from the now-forbidden URL.
if (event.userId === viewerUserIdRef.current) {
- void queryClient.invalidateQueries({ queryKey: communityKeys.servers() })
+ // Rail LIST only (the layout's eject effect reads it to route the
+ // kicked viewer away). `exact` so a kick doesn't cascade-refetch
+ // every server's nested detail subtree.
+ void queryClient.invalidateQueries({ queryKey: communityKeys.servers(), exact: true })
}
} else {
queryClient.setQueryData<InfiniteData<MembersEnvelope> | undefined>(
@@ -921,9 +928,14 @@ export function useCommunityWs(options?: UseCommunityWsOptions) {
case "community:mention.create": {
void queryClient.invalidateQueries({ queryKey: communityKeys.inbox() })
// The server rail badge counts unread mentions per server; refresh
- // it on every new mention. No debounce — mention.create is rare
- // and the servers list is small.
- void queryClient.invalidateQueries({ queryKey: communityKeys.servers() })
+ // it on every new mention. `exact: true` is essential: the mention
+ // count lives in the `servers()` LIST query, but members/presence/
+ // invites/audit-log/server(id) are all nested UNDER `servers()` in
+ // the key hierarchy — a non-exact invalidate prefix-matches and
+ // force-refetches that whole subtree (and invalidate overrides the
+ // staleTime: Infinity those carry). With an active bot in the server,
+ // every mention.create would otherwise storm-refetch all of them.
+ void queryClient.invalidateQueries({ queryKey: communityKeys.servers(), exact: true })
cbs.onMention?.(event)
return
}
diff --git a/src/web/src/hooks/community/use-eager-channel-read.test.ts b/src/web/src/hooks/community/use-eager-channel-read.test.ts
index 9c9ceeb95..57a74105a 100644
--- a/src/web/src/hooks/community/use-eager-channel-read.test.ts
+++ b/src/web/src/hooks/community/use-eager-channel-read.test.ts
@@ -153,7 +153,7 @@ describe("useEagerChannelRead — rail-badge fan-out gate", () => {
expect(keys).not.toContain(JSON.stringify(communityKeys.servers()))
})
- it("refreshes ONLY server(serverId) — not the servers() prefix — when this server has mentions", async () => {
+ it("refreshes the servers() list with exact:true (no cascade) when this server has mentions", async () => {
const useHook = await loadHook()
serversCache = { servers: [server("srv_1", 3)] }
useHook({
@@ -164,9 +164,17 @@ describe("useEagerChannelRead — rail-badge fan-out gate", () => {
})
flushEffects()
await settle()
- const keys = invalidatedKeys()
- expect(keys).toContain(JSON.stringify(communityKeys.server("srv_1")))
- expect(keys).not.toContain(JSON.stringify(communityKeys.servers()))
+ // The badge count lives in the servers() LIST; refresh that, and ONLY that,
+ // with exact:true. A non-exact servers() invalidate prefix-matches the
+ // nested server(id)/members/presence/… subtree and force-refetches all of
+ // it (the per-switch storm). We must NOT touch server(serverId) (it doesn't
+ // even hold the count).
+ const serversCall = invalidateQueries.mock.calls.find(
+ (c) => JSON.stringify(c[0].queryKey) === JSON.stringify(communityKeys.servers()),
+ )
+ expect(serversCall).toBeTruthy()
+ expect(serversCall![0].exact).toBe(true)
+ expect(invalidatedKeys()).not.toContain(JSON.stringify(communityKeys.server("srv_1")))
})
it("does not PUT until the read-state snapshot is ready", async () => {
diff --git a/src/web/src/hooks/community/use-eager-channel-read.ts b/src/web/src/hooks/community/use-eager-channel-read.ts
index e0110dc44..3c7dd5c9f 100644
--- a/src/web/src/hooks/community/use-eager-channel-read.ts
+++ b/src/web/src/hooks/community/use-eager-channel-read.ts
@@ -67,10 +67,15 @@ export function useEagerChannelRead({
// The rail mention badge (`server.mentions`) only needs refreshing when
// this server actually carries mentions the PUT may have cleared. Read
// it from the `servers()` cache and gate on it: the common case (no
- // mentions anywhere in the server) skips the invalidate entirely, so a
- // plain channel switch no longer cascade-refetches every server-level
- // query. When there ARE mentions, refresh only `server(serverId)` — the
- // one query that feeds the badge — not the whole `servers()` prefix.
+ // mentions anywhere in the server) skips the invalidate entirely.
+ //
+ // The badge count lives in the `servers()` LIST query — NOT in
+ // `server(serverId)` (that's the detail: categories/channels, no
+ // count). So refresh `servers()` and, critically, with `exact: true`:
+ // members/presence/invites/audit-log/server(id) are all nested under
+ // `servers()` in the key hierarchy, and a non-exact invalidate
+ // prefix-matches and force-refetches that whole subtree (overriding
+ // their staleTime: Infinity). `exact` refreshes only the badge list.
if (!serverId) return
const servers = queryClient.getQueryData<ServersResponse>(
communityKeys.servers(),
@@ -80,7 +85,8 @@ export function useEagerChannelRead({
)
if (hasMentions) {
void queryClient.invalidateQueries({
- queryKey: communityKeys.server(serverId),
+ queryKey: communityKeys.servers(),
+ exact: true,
})
}
})