Skip to content

Commit f4e332a

Browse files
committed
fix: tighten Buzz mention disambiguation
1 parent 6d7e371 commit f4e332a

2 files changed

Lines changed: 62 additions & 11 deletions

File tree

‎extensions/buzz/src/mentions.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
const BOT_PUBLIC_KEY = "a".repeat(64);
1010
const ALICE_PUBLIC_KEY = "b".repeat(64);
1111
const SECOND_ALICE_PUBLIC_KEY = "c".repeat(64);
12+
const BOB_PUBLIC_KEY = "d".repeat(64);
1213

1314
function members(...values: BuzzMentionMember[]): BuzzMentionMember[] {
1415
return [{ publicKey: BOT_PUBLIC_KEY, displayName: "OpenClaw" }, ...values];
@@ -82,6 +83,51 @@ describe("Buzz outbound mentions", () => {
8283
).toEqual([ALICE_PUBLIC_KEY]);
8384
});
8485

86+
it("requires explicit identities to resolve the named member they accompany", () => {
87+
const explicitBob = nip19.npubEncode(BOB_PUBLIC_KEY);
88+
const roomMembers = members(
89+
{ publicKey: ALICE_PUBLIC_KEY, displayName: "Alice" },
90+
{ publicKey: SECOND_ALICE_PUBLIC_KEY, displayName: "Alice" },
91+
{ publicKey: BOB_PUBLIC_KEY, displayName: "Bob" },
92+
);
93+
94+
expect(() =>
95+
resolveBuzzMessageMentions({
96+
text: `Hello @Missing (nostr:${explicitBob})`,
97+
members: roomMembers,
98+
senderPublicKey: BOT_PUBLIC_KEY,
99+
}),
100+
).toThrow('Buzz mention "@missing" does not match a current room member');
101+
102+
expect(() =>
103+
resolveBuzzMessageMentions({
104+
text: `Hello @Alice (nostr:${explicitBob})`,
105+
members: roomMembers,
106+
senderPublicKey: BOT_PUBLIC_KEY,
107+
}),
108+
).toThrow('Buzz mention "@alice" is ambiguous');
109+
});
110+
111+
it("bounds ambiguous-member guidance", () => {
112+
const duplicateMembers = Array.from({ length: 8 }, (_, index) => ({
113+
publicKey: (index + 1).toString(16).repeat(64),
114+
displayName: "Alice",
115+
}));
116+
117+
expect(() =>
118+
resolveBuzzMessageMentions({
119+
text: "Hello @Alice",
120+
members: members(...duplicateMembers),
121+
senderPublicKey: BOT_PUBLIC_KEY,
122+
}),
123+
).toThrow(
124+
`candidates: ${duplicateMembers
125+
.slice(0, 5)
126+
.map((member) => nip19.npubEncode(member.publicKey))
127+
.join(", ")}, and 3 more.`,
128+
);
129+
});
130+
85131
it("rejects explicit identities outside the room and excludes the bot identity", () => {
86132
const outsider = "d".repeat(64);
87133
expect(() =>

‎extensions/buzz/src/mentions.ts‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { nip19 } from "nostr-tools";
22

33
export const BUZZ_MENTION_MAX_COUNT = 50;
4+
const BUZZ_AMBIGUITY_CANDIDATE_LIMIT = 5;
45

56
export type BuzzMentionMember = {
67
publicKey: string;
@@ -243,26 +244,30 @@ export function resolveBuzzMessageMentions(params: {
243244
.map((member) => member.displayName)
244245
.filter((name): name is string => Boolean(name)),
245246
);
246-
const hasExplicitMentions = explicitPublicKeys.length > 0;
247-
if (hasAtMentionCandidate(stripped) && names.length === 0 && !hasExplicitMentions) {
247+
if (hasAtMentionCandidate(stripped) && names.length === 0) {
248248
throw new Error(
249249
"Buzz mention does not match a current room member; use nostr:npub... for an explicit identity",
250250
);
251251
}
252252
for (const name of names) {
253253
const matches = namesToPublicKeys.get(name) ?? [];
254-
if (matches.length !== 1) {
255-
if (hasExplicitMentions) {
254+
if (matches.length === 0) {
255+
throw new Error(
256+
`Buzz mention "@${name}" does not match a current room member; use nostr:npub... for an explicit identity`,
257+
);
258+
}
259+
if (matches.length > 1) {
260+
if (matches.some((publicKey) => explicitPublicKeys.includes(publicKey))) {
256261
continue;
257262
}
258-
if (matches.length === 0) {
259-
throw new Error(
260-
`Buzz mention "@${name}" does not match a current room member; use nostr:npub... for an explicit identity`,
261-
);
262-
}
263-
const candidates = matches.map((publicKey) => nip19.npubEncode(publicKey)).join(", ");
263+
const visibleCandidates = matches
264+
.slice(0, BUZZ_AMBIGUITY_CANDIDATE_LIMIT)
265+
.map((publicKey) => nip19.npubEncode(publicKey))
266+
.join(", ");
267+
const hiddenCandidateCount = matches.length - BUZZ_AMBIGUITY_CANDIDATE_LIMIT;
268+
const candidateSuffix = hiddenCandidateCount > 0 ? `, and ${hiddenCandidateCount} more` : "";
264269
throw new Error(
265-
`Buzz mention "@${name}" is ambiguous; candidates: ${candidates}. Use nostr:npub... for an explicit identity`,
270+
`Buzz mention "@${name}" is ambiguous; candidates: ${visibleCandidates}${candidateSuffix}. Use nostr:npub... for an explicit identity`,
266271
);
267272
}
268273
const publicKey = matches[0];

0 commit comments

Comments
 (0)