Skip to content

Commit a4dfead

Browse files
baxennpub1rf6fvdj6ut0c4kcmjv4p5mmgh89nj58n69uu3fz3cvk3jn500hqs7emz79
andauthored
fix(desktop): prefer live agent mentions (block#2149)
Signed-off-by: npub1rf6fvdj6ut0c4kcmjv4p5mmgh89nj58n69uu3fz3cvk3jn500hqs7emz79 <1a7496365ae2df8adb1b932a1a6f68b9cb3950f3d179c8a451c32d194e8f7dc1@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1rf6fvdj6ut0c4kcmjv4p5mmgh89nj58n69uu3fz3cvk3jn500hqs7emz79 <1a7496365ae2df8adb1b932a1a6f68b9cb3950f3d179c8a451c32d194e8f7dc1@sprout-oss.stage.blox.sqprod.co>
1 parent 9fb1a90 commit a4dfead

9 files changed

Lines changed: 187 additions & 64 deletions

File tree

‎desktop/src/features/agents/lib/agentAutocompleteEligibility.test.mjs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
coalesceAgentAutocompleteCandidates,
66
getMentionableAgentPubkeys,
77
getSharedChannelIds,
8+
isAgentIdentityInManagedList,
89
relayAgentIsSharedWithUser,
910
shouldHideAgentFromMentions,
1011
} from "./agentAutocompleteEligibility.ts";
@@ -135,6 +136,32 @@ test("getMentionableAgentPubkeys: keeps managed agents and shared relay agents",
135136
assert.deepEqual(result, new Set([PUB_A, PUB_B, PUB_C]));
136137
});
137138

139+
test("isAgentIdentityInManagedList: keeps people and only current managed agent identities", () => {
140+
const managedAgentPubkeys = new Set([PUB_A]);
141+
142+
assert.equal(
143+
isAgentIdentityInManagedList(
144+
{ isAgent: false, pubkey: PUB_B },
145+
managedAgentPubkeys,
146+
),
147+
true,
148+
);
149+
assert.equal(
150+
isAgentIdentityInManagedList(
151+
{ isAgent: true, pubkey: PUB_A.toUpperCase() },
152+
managedAgentPubkeys,
153+
),
154+
true,
155+
);
156+
assert.equal(
157+
isAgentIdentityInManagedList(
158+
{ isAgent: true, pubkey: PUB_B },
159+
managedAgentPubkeys,
160+
),
161+
false,
162+
);
163+
});
164+
138165
test("shouldHideAgentFromMentions: never hides non-agents", () => {
139166
assert.equal(
140167
shouldHideAgentFromMentions({

‎desktop/src/features/agents/lib/agentAutocompleteEligibility.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ export function getMentionableAgentPubkeys({
5454
return pubkeys;
5555
}
5656

57+
export function isAgentIdentityInManagedList(
58+
candidate: { isAgent?: boolean; pubkey: string },
59+
managedAgentPubkeys: ReadonlySet<string>,
60+
) {
61+
return (
62+
candidate.isAgent !== true ||
63+
managedAgentPubkeys.has(normalizePubkey(candidate.pubkey))
64+
);
65+
}
66+
5767
export function shouldHideAgentFromMentions({
5868
isAgent,
5969
isMember,

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

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import { Bot, UserRoundPlus, X } from "lucide-react";
44
import {
55
useAddChannelMembersMutation,
66
useChannelMembersQuery,
7-
useChannelsQuery,
87
} from "@/features/channels/hooks";
98
import {
109
coalesceAgentAutocompleteCandidates,
11-
getMentionableAgentPubkeys,
12-
getSharedChannelIds,
10+
isAgentIdentityInManagedList,
1311
} from "@/features/agents/lib/agentAutocompleteEligibility";
1412
import { useIsArchivedPredicate } from "@/features/identity-archive/hooks";
1513
import { useClassifiedMembers } from "@/features/channels/lib/useClassifiedMembers";
@@ -146,7 +144,6 @@ export function MembersSidebar({
146144
const identityQuery = useIdentityQuery();
147145
const membersQuery = useChannelMembersQuery(channelId, open);
148146
const addMembersMutation = useAddChannelMembersMutation(channelId);
149-
const channelsQuery = useChannelsQuery({ enabled: open });
150147
const changeRoleMutation = useMutation({
151148
mutationFn: async ({ pubkey, role }: { pubkey: string; role: string }) => {
152149
if (!channelId) throw new Error("No channel selected.");
@@ -262,15 +259,7 @@ export function MembersSidebar({
262259
.map((member) => member.displayName?.trim().toLowerCase())
263260
.filter((label): label is string => Boolean(label)),
264261
);
265-
const sharedChannelIds = getSharedChannelIds(channelsQuery.data);
266-
const eligibleAgentPubkeys = getMentionableAgentPubkeys({
267-
currentPubkey,
268-
managedAgentPubkeys: (managedAgentsQuery.data ?? []).map(
269-
(agent) => agent.pubkey,
270-
),
271-
relayAgents: relayAgentsQuery.data,
272-
sharedChannelIds,
273-
});
262+
const managedAgentPubkeys = new Set(managedAgentsByPubkey.keys());
274263

275264
const addCandidate = (candidate: AddMemberSearchCandidate) => {
276265
const pubkey = normalizePubkey(candidate.pubkey);
@@ -281,7 +270,7 @@ export function MembersSidebar({
281270
)) ||
282271
memberPubkeys.has(pubkey) ||
283272
isArchivedDiscovery(pubkey) ||
284-
(candidate.isAgent && !eligibleAgentPubkeys.has(pubkey))
273+
!isAgentIdentityInManagedList(candidate, managedAgentPubkeys)
285274
) {
286275
return;
287276
}
@@ -320,10 +309,6 @@ export function MembersSidebar({
320309
}
321310

322311
for (const agent of relayAgentsQuery.data ?? []) {
323-
if (!eligibleAgentPubkeys.has(normalizePubkey(agent.pubkey))) {
324-
continue;
325-
}
326-
327312
addCandidate({
328313
pubkey: agent.pubkey,
329314
displayName: agent.name,
@@ -365,7 +350,6 @@ export function MembersSidebar({
365350
}, [
366351
canAddMembers,
367352
isArchivedDiscovery,
368-
channelsQuery.data,
369353
currentPubkey,
370354
managedAgentsQuery.data,
371355
memberPubkeys,
@@ -377,8 +361,7 @@ export function MembersSidebar({
377361
const isAddSearchLoading =
378362
userSearchQuery.isLoading ||
379363
managedAgentsQuery.isLoading ||
380-
relayAgentsQuery.isLoading ||
381-
channelsQuery.isLoading;
364+
relayAgentsQuery.isLoading;
382365
const handlePeopleSearchScroll = useUserSearchFetchMoreOnScroll(
383366
userSearchQuery,
384367
canAddMembers && normalizedDeferredSearchQuery.length > 0,

‎desktop/src/features/messages/lib/useMentions.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
coalesceAutocompleteCandidatesByKey,
1717
getMentionableAgentPubkeys,
1818
getSharedChannelIds,
19+
isAgentIdentityInManagedList,
1920
shouldHideAgentFromMentions,
2021
} from "@/features/agents/lib/agentAutocompleteEligibility";
2122
import {
@@ -245,6 +246,9 @@ export function useMentions(
245246
if (isArchivedDiscovery(pubkey)) {
246247
return;
247248
}
249+
if (!isAgentIdentityInManagedList(candidate, managedAgentPubkeys)) {
250+
return;
251+
}
248252
if (
249253
shouldHideAgentFromMentions({
250254
isAgent: candidate.isAgent === true,
@@ -256,7 +260,6 @@ export function useMentions(
256260
) {
257261
return;
258262
}
259-
260263
const current = candidatesByPubkey.get(pubkey);
261264
if (!current) {
262265
candidatesByPubkey.set(pubkey, { ...candidate, pubkey });
@@ -289,7 +292,6 @@ export function useMentions(
289292
isManagedAgent: current.isManagedAgent || candidate.isManagedAgent,
290293
});
291294
};
292-
293295
for (const member of members ?? []) {
294296
const pubkey = normalizePubkey(member.pubkey);
295297
const linkedPersonaId = activePersonaById.has(pubkey)
@@ -418,6 +420,7 @@ export function useMentions(
418420
managedAgentNamesByPubkey,
419421
managedAgentPersonaIds,
420422
managedAgentPersonaIdsByPubkey,
423+
managedAgentPubkeys,
421424
managedAgentsQuery.data,
422425
memberPubkeys,
423426
members,
@@ -540,10 +543,7 @@ export function useMentions(
540543
mentionQuery,
541544
activePersonaIds,
542545
)
543-
.slice(
544-
0,
545-
Math.max(MENTION_SUGGESTION_LIMIT, mentionCandidatesWithTeams.length),
546-
)
546+
.slice(0, MENTION_SUGGESTION_LIMIT)
547547
.map(({ candidate, label }) =>
548548
mapMentionCandidateToSuggestion({
549549
candidate,

‎desktop/tests/e2e/channels.spec.ts‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -812,10 +812,17 @@ test("routes an agent mention from an existing DM to the expanded conversation",
812812
);
813813
});
814814

815-
test("routes a relay-agent mention from an existing DM to the expanded conversation", async ({
815+
test("routes a managed relay-agent mention from an existing DM to the expanded conversation", async ({
816816
page,
817817
}) => {
818818
await installMockBridge(page, {
819+
managedAgents: [
820+
{
821+
pubkey: DM_RELAY_AGENT_PUBKEY,
822+
name: "quinn",
823+
status: "stopped",
824+
},
825+
],
819826
relayAgents: [
820827
{
821828
pubkey: DM_RELAY_AGENT_PUBKEY,
@@ -856,15 +863,13 @@ test("routes a relay-agent mention from an existing DM to the expanded conversat
856863
page.locator("[data-active='true'][data-channel-id]"),
857864
).toHaveAttribute("data-channel-id", sentChannelId ?? "");
858865
await expect(page.getByTestId("chat-title")).toContainText("alice");
859-
await expect(page.getByTestId("chat-title")).toContainText(
860-
DM_RELAY_AGENT_PUBKEY.slice(0, 8),
861-
);
866+
await expect(page.getByTestId("chat-title")).toContainText("quinn");
862867

863868
const sendCommands = (await readCommandPayloadLog(page)).slice(
864869
baselineCommands.length,
865870
);
866871
expect(sendCommands.map((entry) => entry.command)).toContain("open_dm");
867-
expect(sendCommands.map((entry) => entry.command)).not.toContain(
872+
expect(sendCommands.map((entry) => entry.command)).toContain(
868873
"start_managed_agent",
869874
);
870875
expect(sendCommands.map((entry) => entry.command)).not.toContain(
@@ -2554,7 +2559,18 @@ test("home inbox manage affordance opens management without leaving home", async
25542559
await expect(page).not.toHaveURL(/#\/channels\//);
25552560
});
25562561

2557-
test("members sidebar can invite and remove members", async ({ page }) => {
2562+
test("members sidebar can invite and remove managed agents", async ({
2563+
page,
2564+
}) => {
2565+
await installMockBridge(page, {
2566+
managedAgents: [
2567+
{
2568+
pubkey: TEST_IDENTITIES.charlie.pubkey,
2569+
name: "charlie",
2570+
status: "stopped",
2571+
},
2572+
],
2573+
});
25582574
await page.goto("/");
25592575
await openMembersSidebar(page, "general");
25602576
const initialMemberCount = await readMembersTriggerCount(page);
@@ -2847,9 +2863,18 @@ test("members sidebar collapses same-persona managed agents", async ({
28472863
await expect(page.getByText("Pinky", { exact: true })).toHaveCount(1);
28482864
});
28492865

2850-
test("private-channel members can add members and bots without admin", async ({
2866+
test("private-channel members can add people and managed agents without admin", async ({
28512867
page,
28522868
}) => {
2869+
await installMockBridge(page, {
2870+
managedAgents: [
2871+
{
2872+
pubkey: TEST_IDENTITIES.charlie.pubkey,
2873+
name: "charlie",
2874+
status: "stopped",
2875+
},
2876+
],
2877+
});
28532878
await page.goto("/");
28542879
// secret-projects is a private (non-DM) channel where the current user is a
28552880
// plain member, not owner/admin. They should still be able to add members

‎desktop/tests/e2e/identity-archive-hide.spec.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,38 +166,38 @@ test.describe("NIP-IA hide archived from discovery", () => {
166166
});
167167

168168
// Member-adder (ChannelMemberInviteCard) — the invite search excludes
169-
// existing channel members, so we test in #agents where Alice is NOT a
169+
// existing channel members, so we test in #agents where Bob is NOT a
170170
// member (only the mock identity + Charlie are): the ONLY thing that can
171-
// drop her from results is the archive filter. The control run (nothing
172-
// archived) proves she would otherwise appear — guarding a vacuous green.
171+
// drop Bob from results is the archive filter. The control run (nothing
172+
// archived) proves he would otherwise appear — guarding a vacuous green.
173173
test("member-adder: archived non-member is omitted from invite search", async ({
174174
page,
175175
}) => {
176-
await installMockBridge(page, { archivedIdentities: [ALICE_PUBKEY] });
176+
await installMockBridge(page, { archivedIdentities: [BOB_PUBKEY] });
177177
await page.goto("/");
178178
await page.getByTestId("channel-agents").click();
179179
await page.getByTestId("channel-members-trigger").click();
180180
await expect(page.getByTestId("members-sidebar")).toBeVisible();
181-
await page.getByTestId("channel-management-search-users").fill("alice");
181+
await page.getByTestId("channel-management-search-users").fill("bob");
182182
await expect(
183-
page.getByTestId(`channel-user-search-result-${ALICE_PUBKEY}`),
183+
page.getByTestId(`channel-user-search-result-${BOB_PUBKEY}`),
184184
).toHaveCount(0);
185185
});
186186

187187
test("member-adder: control — non-archived non-member IS in invite search", async ({
188188
page,
189189
}) => {
190-
// Same channel/query, nothing archived: Alice now appears. This is the
191-
// companion that makes the test above non-vacuous (proves she was dropped
190+
// Same channel/query, nothing archived: Bob now appears. This is the
191+
// companion that makes the test above non-vacuous (proves he was dropped
192192
// by the archive filter, not by member-exclusion or a broken search).
193193
await installMockBridge(page, { archivedIdentities: [] });
194194
await page.goto("/");
195195
await page.getByTestId("channel-agents").click();
196196
await page.getByTestId("channel-members-trigger").click();
197197
await expect(page.getByTestId("members-sidebar")).toBeVisible();
198-
await page.getByTestId("channel-management-search-users").fill("alice");
198+
await page.getByTestId("channel-management-search-users").fill("bob");
199199
await expect(
200-
page.getByTestId(`channel-user-search-result-${ALICE_PUBKEY}`),
200+
page.getByTestId(`channel-user-search-result-${BOB_PUBKEY}`),
201201
).toBeVisible();
202202
});
203203
});

0 commit comments

Comments
 (0)