Skip to content

Commit b0466ac

Browse files
authored
perf(desktop): keep the member roster off the channel-switch path (#6456)
Switching channels triggered a full-roster fetch (kind:39002 plus a kind:0 profile batch with every member pubkey as an author) in the common case, and several render paths walked the full roster per render. None of this scales past a few hundred members; the product target is 10k+. - **Members query staleTime 30s → 5min.** Every membership change the client can observe already invalidates the key explicitly: live join/leave system messages for the active channel, member-added/removed notifications for the current identity, and all membership mutations — including previously-uncovered direct write paths (moderation kick, agent-deletion cleanup), which now invalidate through a shared helper. The 30s window bought correctness we already had and charged a roster fetch per switch. - **ChannelMembersBar no longer mounts the roster query for non-DM channels** — the count renders from the channel summary, and the private-channel huddle gate accepts `channel.isMember` (derived from the same kind:39002 event as the roster's self entry). - **Roster-derived lookups are cached on roster identity** (`rosterDerivations.ts`): role map, agent-member subset, member/bot pubkey sets. These were rebuilt O(members) on every live message / profile re-key. React Query's structural sharing keeps the roster identity stable, so each derivation computes once per distinct roster. - **Backend: the kind:0 profile join in `get_channel_members` is capped at the first 500 members** (roster order). Members past the cap keep `display_name: None` (UI falls back to pubkey labels and profile caches); `role=="bot"` agent flags are roster-derived and unaffected. Full roster pagination is the structural follow-up. - **Composer keystroke path**: `useCanAddChannelMembers` re-scanned channels + roster per keystroke; now memoized on data identities, sharing the cached pubkey set. ### Measured / estimated impact | metric | before | after | |---|---|---| | roster fetches while switching (live trace) | nearly every switch | ≤1 per channel per 5 min | | roster fetch cost on the wire (live, 51-member channel) | 273ms per fetch | amortized away | | kind:0 `authors` filter size at 10k members | ~670KB per request (~67 B/pubkey) | capped at 500 authors (~34KB) | | warm-switch longtask at 10k members (mock harness, 4× throttle) | 364ms | 318ms | | per-render roster walks (role map, agent sets) at 10k members | O(members) per live message | once per distinct roster | Deferred deliberately: protocol-level roster pagination and removing `memberPubkeys` from channel summaries (needs relay support). --------- Signed-off-by: Max Lampert <[email protected]>
1 parent 4752678 commit b0466ac

20 files changed

Lines changed: 406 additions & 64 deletions

‎desktop/src-tauri/src/commands/channels.rs‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ pub async fn get_channel_details(
121121
.ok_or_else(|| "channel not found".to_string())
122122
}
123123

124+
/// Cap for the kind:0 profile join in `get_channel_members`. Enriching a
125+
/// huge roster required an `authors` filter carrying every member pubkey — a
126+
/// query whose size and relay cost grow linearly with membership and which
127+
/// dominated channel-open latency on large channels. Members past the cap
128+
/// keep `display_name: None` (the UI falls back to pubkey-derived labels and
129+
/// resolves visible names through its profile caches); `role == "bot"` agent
130+
/// flags are roster-derived and unaffected by the cap.
131+
const MEMBER_PROFILE_JOIN_LIMIT: usize = 500;
132+
133+
/// The pubkeys eligible for the kind:0 profile join: roster order, capped.
134+
fn profile_join_pubkeys(members: &[crate::models::ChannelMemberInfo], limit: usize) -> Vec<String> {
135+
members
136+
.iter()
137+
.take(limit)
138+
.map(|member| member.pubkey.clone())
139+
.collect()
140+
}
141+
124142
#[tauri::command]
125143
pub async fn get_channel_members(
126144
channel_id: String,
@@ -142,8 +160,9 @@ pub async fn get_channel_members(
142160
.transpose()?
143161
.ok_or_else(|| "channel members not found".to_string())?;
144162

145-
// Batch-fetch kind:0 profiles to populate display names.
146-
let pubkeys: Vec<String> = response.members.iter().map(|m| m.pubkey.clone()).collect();
163+
// Batch-fetch kind:0 profiles to populate display names, capped so the
164+
// query cost is bounded on large rosters (see MEMBER_PROFILE_JOIN_LIMIT).
165+
let pubkeys = profile_join_pubkeys(&response.members, MEMBER_PROFILE_JOIN_LIMIT);
147166
if !pubkeys.is_empty() {
148167
let profile_events = query_relay(
149168
&state,

‎desktop/src-tauri/src/commands/channels_tests.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,26 @@ fn last_message_filters_stay_within_relay_channel_cap() {
490490
);
491491
assert_eq!(batches.concat(), filters);
492492
}
493+
494+
fn member(pubkey: &str) -> crate::models::ChannelMemberInfo {
495+
crate::models::ChannelMemberInfo {
496+
pubkey: pubkey.to_string(),
497+
role: "member".to_string(),
498+
is_agent: false,
499+
joined_at: None,
500+
display_name: None,
501+
}
502+
}
503+
504+
#[test]
505+
fn profile_join_pubkeys_caps_in_roster_order() {
506+
let members = vec![member(PK_A), member(PK_B), member(PK_C)];
507+
508+
assert_eq!(
509+
profile_join_pubkeys(&members, 2),
510+
vec![PK_A.to_string(), PK_B.to_string()]
511+
);
512+
assert_eq!(profile_join_pubkeys(&members, 3).len(), 3);
513+
assert_eq!(profile_join_pubkeys(&members, 10).len(), 3);
514+
assert!(profile_join_pubkeys(&[], 10).is_empty());
515+
}

‎desktop/src/features/agents/knownAgentPubkeys.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { normalizePubkey } from "@/shared/lib/pubkey";
2+
import { channelAgentMembers } from "@/shared/lib/rosterDerivations";
23

34
/**
45
* Pure merge behind `useKnownAgentPubkeys`: managed agents ∪ relay agents,
@@ -62,10 +63,11 @@ export function mergeChannelKnownAgentPubkeys(
6263
relayAgents: readonly { pubkey: string }[] | undefined,
6364
): ReadonlySet<string> {
6465
const pubkeys = new Set(mergeKnownAgentPubkeys(managedAgents, relayAgents));
65-
for (const member of channelMembers ?? []) {
66-
if (member.role === "bot" || member.isAgent) {
67-
pubkeys.add(normalizePubkey(member.pubkey));
68-
}
66+
// Identity-cached agent subset: avoids walking the full roster per call.
67+
for (const member of channelMembers
68+
? channelAgentMembers(channelMembers)
69+
: []) {
70+
pubkeys.add(normalizePubkey(member.pubkey));
6971
}
7072
return pubkeys;
7173
}

‎desktop/src/features/agents/ui/useManagedAgentActions.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from "react";
2+
import { useQueryClient } from "@tanstack/react-query";
23
import { toast } from "sonner";
34

45
import {
@@ -15,6 +16,7 @@ import {
1516
} from "@/features/agents/hooks";
1617
import { useGlobalAgentConfig } from "@/features/agents/useGlobalAgentConfig";
1718
import { useChannelsQuery } from "@/features/channels/hooks";
19+
import { invalidateChannelMembersRosters } from "@/features/channels/rosterFreshness";
1820
import { usePresenceQuery } from "@/features/presence/hooks";
1921
import type { AgentPersona, Channel, ManagedAgent } from "@/shared/api/types";
2022
import { removeChannelMember } from "@/shared/api/tauri";
@@ -34,6 +36,7 @@ import {
3436
} from "../lib/instanceInputForDefinition";
3537

3638
export function useManagedAgentActions() {
39+
const queryClient = useQueryClient();
3740
const { globalConfig } = useGlobalAgentConfig();
3841
const relayAgentsQuery = useRelayAgentsQuery();
3942
const managedAgentsQuery = useManagedAgentsQuery();
@@ -296,6 +299,9 @@ export function useManagedAgentActions() {
296299
await Promise.allSettled(
297300
channelIds.map((channelId) => removeChannelMember(channelId, pubkey)),
298301
);
302+
// Direct writes bypass the member mutations' invalidation; without this,
303+
// the deleted agent stays in cached rosters for the freshness window.
304+
await invalidateChannelMembersRosters(queryClient, channelIds);
299305
}
300306

301307
async function handleDelete(pubkey: string) {

‎desktop/src/features/channels/hooks.test.mjs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,22 @@ test("reconcileRefreshedCachedChannel_preservesRefreshedDmRecency", () => {
353353
ownerPubkey,
354354
]);
355355
});
356+
357+
test("invalidateChannelMembersRosters dedupes and targets member keys", async () => {
358+
const { invalidateChannelMembersRosters } = await import(
359+
"./rosterFreshness.ts"
360+
);
361+
const invalidated = [];
362+
const queryClient = {
363+
invalidateQueries: async ({ queryKey }) => {
364+
invalidated.push(queryKey);
365+
},
366+
};
367+
368+
await invalidateChannelMembersRosters(queryClient, ["ch-a", "ch-b", "ch-a"]);
369+
370+
assert.deepEqual(invalidated, [
371+
["channels", "ch-a", "members"],
372+
["channels", "ch-b", "members"],
373+
]);
374+
});

‎desktop/src/features/channels/hooks.ts‎

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ import { mergeConcurrentChannelRecency } from "@/features/channels/lib/channelRe
4444
import { useIdentityQuery } from "@/shared/api/hooks";
4545
import { useFocusedRefetchInterval } from "@/shared/lib/useDocumentVisible";
4646
import { useCommunities } from "@/features/communities/useCommunities";
47-
import { canAddChannelMembers } from "@/features/channels/lib/channelMemberAdmission";
4847
import {
4948
inspectChannelSnapshot,
5049
type ChannelSnapshot,
5150
writeChannelSnapshot,
5251
} from "@/features/channels/channelSnapshot";
52+
import {
53+
CHANNEL_MEMBERS_STALE_TIME_MS,
54+
channelMembersQueryKey,
55+
} from "@/features/channels/rosterFreshness";
5356

5457
export const channelsQueryKey = ["channels"] as const;
5558
/** Keeps focused polling at the established one-minute cadence. */
@@ -69,8 +72,6 @@ export const channelsFocusRefetchPolicy = {
6972
const channelsSnapshotPairKey = ["channels", "_snapshot-pair"] as const;
7073
const channelDetailQueryKey = (channelId: string) =>
7174
["channels", channelId, "detail"] as const;
72-
const channelMembersQueryKey = (channelId: string) =>
73-
["channels", channelId, "members"] as const;
7475
const channelTypeOrder = {
7576
stream: 0,
7677
forum: 1,
@@ -628,7 +629,7 @@ export function useChannelMembersQuery(
628629

629630
return getChannelMembers(channelId);
630631
},
631-
staleTime: 30_000,
632+
staleTime: CHANNEL_MEMBERS_STALE_TIME_MS,
632633
});
633634
}
634635

@@ -795,32 +796,6 @@ export function useDeleteChannelMutation(channelId: string | null) {
795796
});
796797
}
797798

798-
/**
799-
* Whether the signed-in identity may add *another* identity to this channel,
800-
* per {@link canAddChannelMembers}. Both queries are the ones the channel UI
801-
* already holds, so this shares their cache rather than fetching again.
802-
*/
803-
export function useCanAddChannelMembers(channelId: string | null) {
804-
const channelsQuery = useChannelsQuery();
805-
const membersQuery = useChannelMembersQuery(channelId);
806-
const identityQuery = useIdentityQuery();
807-
808-
const channel =
809-
channelsQuery.data?.find((candidate) => candidate.id === channelId) ?? null;
810-
const selfPubkey = identityQuery.data?.pubkey ?? null;
811-
const selfRole = selfPubkey
812-
? (membersQuery.data?.find(
813-
(member) => member.pubkey.toLowerCase() === selfPubkey.toLowerCase(),
814-
)?.role ?? null)
815-
: null;
816-
817-
return canAddChannelMembers({
818-
channelType: channel?.channelType,
819-
visibility: channel?.visibility,
820-
selfRole,
821-
});
822-
}
823-
824799
export function useAddChannelMembersMutation(channelId: string | null) {
825800
const queryClient = useQueryClient();
826801

‎desktop/src/features/channels/lib/huddleAvailability.test.mjs‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,9 @@ test("canStartHuddleInChannel blocks non-participant DMs", () => {
8787
});
8888

8989
test("canStartHuddleInChannel keeps private channels member-gated", () => {
90-
const privateChannel = channel({ visibility: "private" });
91-
9290
assert.equal(
9391
canStartHuddleInChannel({
94-
channel: privateChannel,
92+
channel: channel({ visibility: "private", isMember: false }),
9593
currentPubkey: SELF,
9694
selfMember: null,
9795
}),
@@ -100,14 +98,27 @@ test("canStartHuddleInChannel keeps private channels member-gated", () => {
10098

10199
assert.equal(
102100
canStartHuddleInChannel({
103-
channel: privateChannel,
101+
channel: channel({ visibility: "private", isMember: false }),
104102
currentPubkey: SELF,
105103
selfMember: member(),
106104
}),
107105
true,
108106
);
109107
});
110108

109+
test("canStartHuddleInChannel accepts channel-level membership without a roster", () => {
110+
// The roster is fetched lazily; `channel.isMember` derives from the same
111+
// kind:39002 event, so it must satisfy the private-channel gate on its own.
112+
assert.equal(
113+
canStartHuddleInChannel({
114+
channel: channel({ visibility: "private", isMember: true }),
115+
currentPubkey: SELF,
116+
selfMember: null,
117+
}),
118+
true,
119+
);
120+
});
121+
111122
test("canStartHuddleInChannel blocks archived channels and DMs", () => {
112123
assert.equal(
113124
canStartHuddleInChannel({

‎desktop/src/features/channels/lib/huddleAvailability.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,10 @@ export function canStartHuddleInChannel({
3131
);
3232
}
3333

34-
return channel.visibility === "open" || selfMember !== null;
34+
// `channel.isMember` and the roster's self entry derive from the same
35+
// kind:39002 event; either satisfies the private-channel gate, so callers
36+
// that no longer fetch the roster eagerly keep huddle access.
37+
return (
38+
channel.visibility === "open" || channel.isMember || selfMember !== null
39+
);
3540
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Member-roster freshness policy and the invalidation helper for write paths
3+
* that bypass the member mutations. Split from hooks.ts to keep that file
4+
* under the per-file line cap; behavior unchanged.
5+
*/
6+
7+
import type { useQueryClient } from "@tanstack/react-query";
8+
9+
/** Single source for the members cache key; hooks.ts imports it from here. */
10+
export const channelMembersQueryKey = (channelId: string) =>
11+
["channels", channelId, "members"] as const;
12+
13+
/**
14+
* Freshness window for the full member roster. Kept long because every
15+
* membership change the client can observe invalidates this key explicitly:
16+
* live join/leave/removed system messages for the active channel
17+
* (useChannelSubscription), member-added/removed notifications targeting the
18+
* current identity (useMembershipNotifications), and every membership
19+
* mutation (add/remove/join/leave, template apply). The residual staleness is
20+
* a third party joining a channel the viewer is not currently subscribed to,
21+
* which corrects within this window. The previous 30s window put a full
22+
* roster fetch (kind:39002 + a kind:0 batch over every member) on nearly
23+
* every channel switch.
24+
*/
25+
export const CHANNEL_MEMBERS_STALE_TIME_MS = 5 * 60_000;
26+
27+
/**
28+
* Invalidates cached rosters for channels whose membership was written
29+
* through direct `removeChannelMember` calls that bypass the member
30+
* mutations (moderation kick, agent-deletion cleanup). The roster's long
31+
* freshness window (CHANNEL_MEMBERS_STALE_TIME_MS) means any direct write
32+
* path that skips this leaves the removed identity visible until the window
33+
* lapses. Accepts a minimal client shape so node unit tests can stub it.
34+
*/
35+
export async function invalidateChannelMembersRosters(
36+
queryClient: Pick<ReturnType<typeof useQueryClient>, "invalidateQueries">,
37+
channelIds: Iterable<string>,
38+
) {
39+
const uniqueChannelIds = [...new Set(channelIds)];
40+
for (const channelId of uniqueChannelIds) {
41+
await queryClient.invalidateQueries({
42+
queryKey: channelMembersQueryKey(channelId),
43+
});
44+
}
45+
}

‎desktop/src/features/channels/ui/ChannelMembersBar.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ export function ChannelMembersBar({
6565
);
6666
const { startHuddle, isStarting: isStartingHuddle } = useHuddle();
6767
const queryClient = useQueryClient();
68-
const membersQuery = useChannelMembersQuery(channel.id);
68+
// The roster is only needed for DM huddle composition (agent detection and
69+
// participant naming). Streams/forums render the count from the channel
70+
// summary and gate huddle access on `channel.isMember`, so mounting this
71+
// bar must not put a full-roster fetch on the channel-switch path.
72+
const membersQuery = useChannelMembersQuery(
73+
channel.id,
74+
channel.channelType === "dm",
75+
);
6976
const providersQuery = useAvailableAcpRuntimes();
7077
const managedAgentsQuery = useManagedAgentsQuery();
7178
const relayAgentsQuery = useRelayAgentsQuery();

0 commit comments

Comments
 (0)