Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/copilot-bot): make the bot's mentions actually ping, by name or id (#14501)

Bently01ebc6c32736
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py
index 021710b2dd..327a3cdd04 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py
@@ -80,6 +80,14 @@ ROLE_ID_PREFIX = "role:"
# message, each returning up to this many prefix matches.
MENTION_QUERY_CAP = 8
MENTION_QUERY_LIMIT = 20
+# Who owns a name when several things share it, best first. See
+# ``DiscordAdapter._mentionables_for``.
+_RANK_CONVERSATION = 0
+_RANK_MEMBER = 1
+_RANK_ROLE = 2
+_RANK_PREFIX = 3
+# A raw Discord mention token: user ``<@id>`` / ``<@!id>`` or role ``<@&id>``.
+_RAW_MENTION = re.compile(r"<@([!&]?)(\d{15,21})>")
# First word after an @ that is not inside an email, URL or existing token.
_MENTION_CANDIDATE = re.compile(r"(?<![\w@<])@([A-Za-z0-9_][\w'\-]{0,31})")
@@ -729,54 +737,94 @@ class DiscordAdapter(SocketAdapter):
text: str,
known: tuple[tuple[str, str], ...],
) -> tuple[tuple[str, str], ...]:
- """Everyone ``text`` may ping in ``channel``: ``known`` (author and
- inbound mentions) plus, for a server channel, any member whose display
- name or username matches an ``@Name`` in the text and any role by name.
-
- Members are found with a gateway member query per name, which needs no
- privileged intent and works on any server. ``@everyone`` and ``@here``
- are never listed, so they stay plain text and the ping object keeps
- them off regardless. A DM has no server, so only ``known`` applies.
+ """Everyone ``text`` may ping in ``channel``, as ``(name, id)`` pairs.
+
+ ``known`` (the author and anyone the inbound message mentioned) always
+ applies. A server channel adds, from the text itself: members named
+ with ``@Name``, members written as a raw ``<@id>``, and roles.
+
+ One name can point at several of these, and the shared resolver pings
+ nobody for a name that belongs to two different ids. So each name is
+ claimed only by its best source, and only a tie at that level is a
+ real clash. Best first:
+
+ 1. someone already in this conversation
+ 2. a server member whose display name or username is exactly the name
+ 3. a role of that name (no member lookup is run for a role's name)
+ 4. a member the typed ``@Name`` only prefixes (``@Bently`` for
+ ``Bently [SOMN]``)
+
+ So a "Bently" role never hides the Bently who just wrote to the bot,
+ and a member called "PlatformBot" never steals ``@Platform`` from the
+ Platform role. Members are found with a gateway query per name, which
+ needs no privileged intent. ``@everyone`` and ``@here`` are never
+ listed. A DM has no server, so only ``known`` applies.
"""
guild = getattr(channel, "guild", None)
if not isinstance(guild, discord.Guild):
return known
bot_id = self._client.user.id if self._client.user else None
- pairs: list[tuple[str, str]] = list(known)
- seen: set[tuple[str, str]] = set(pairs)
-
- def add(name: Optional[str], token_id: str) -> None:
- if name and (name, token_id) not in seen:
- seen.add((name, token_id))
- pairs.append((name, token_id))
-
+ candidates: list[tuple[int, str, str]] = [
+ (_RANK_CONVERSATION, name, token_id) for name, token_id in known
+ ]
role_names: set[str] = set()
for role in guild.roles:
if role.id == guild.id or role.is_default():
continue
- add(role.name, f"{ROLE_ID_PREFIX}{role.id}")
+ candidates.append((_RANK_ROLE, role.name, f"{ROLE_ID_PREFIX}{role.id}"))
role_names.add(role.name.casefold())
for query in _mention_queries(text):
+ # A name that is a role means the role, unless someone in this
+ # conversation holds it, and they are already in ``known``.
if query.casefold() in role_names:
continue
- try:
- members = await guild.query_members(
- query=query, limit=MENTION_QUERY_LIMIT, cache=False
- )
- except (
- asyncio.TimeoutError,
- ValueError,
- discord.ClientException,
- discord.HTTPException,
- ):
- logger.warning("Member lookup for @%s failed", query, exc_info=True)
- continue
- for member in members:
+ for member in await self._query_members(guild, query):
if member.id == bot_id or member.bot:
continue
- add(member.display_name, str(member.id))
- add(member.name, str(member.id))
- return tuple(pairs)
+ member_id = str(member.id)
+ for name in (member.display_name, member.name):
+ exact = name.casefold() == query.casefold()
+ candidates.append(
+ (_RANK_MEMBER if exact else _RANK_PREFIX, name, member_id)
+ )
+ candidates.append((_RANK_PREFIX, query, member_id))
+ listed_ids = {token_id for _, _, token_id in candidates}
+ for user_id in _raw_user_mention_ids(text):
+ if user_id in listed_ids or user_id == str(bot_id):
+ continue
+ member = await self._guild_member(guild, int(user_id))
+ if member is not None and not member.bot:
+ candidates.append((_RANK_MEMBER, member.display_name, user_id))
+ return _best_claims(candidates)
+
+ async def _query_members(
+ self, guild: discord.Guild, query: str
+ ) -> list[discord.Member]:
+ try:
+ return await guild.query_members(
+ query=query, limit=MENTION_QUERY_LIMIT, cache=False
+ )
+ except (
+ asyncio.TimeoutError,
+ ValueError,
+ discord.ClientException,
+ discord.HTTPException,
+ ):
+ logger.warning("Member lookup for @%s failed", query, exc_info=True)
+ return []
+
+ async def _guild_member(
+ self, guild: discord.Guild, user_id: int
+ ) -> Optional[discord.Member]:
+ """The server member behind a raw ``<@id>``, or None when that id is not
+ a member here. Cache first, then one REST lookup."""
+ member = guild.get_member(user_id)
+ if member is not None:
+ return member
+ try:
+ return await guild.fetch_member(user_id)
+ except (discord.NotFound, discord.Forbidden, discord.HTTPException):
+ return None
async def _thread_history(
self, message: discord.Message
@@ -969,6 +1017,14 @@ def _resolve_mentions(
token and turn the pinged IDs into Discord's ping-safety object.
"""
rendered, pinged = resolve_mentions(text, mentionable_users, _mention_token)
+ # The shared resolver reads user ids; a raw role token is Discord's own.
+ allowlisted = {token_id for _, token_id in mentionable_users}
+ for match in _RAW_MENTION.finditer(rendered):
+ if match.group(1) != "&":
+ continue
+ token_id = f"{ROLE_ID_PREFIX}{match.group(2)}"
+ if token_id in allowlisted and token_id not in pinged:
+ pinged.append(token_id)
user_ids: list[int] = []
role_ids: list[int] = []
for token_id in pinged:
@@ -994,6 +1050,35 @@ def _mention_token(_name: str, token_id: str) -> str:
return f"<@{token_id}>"
+def _best_claims(
+ candidates: list[tuple[int, str, str]],
+) -> tuple[tuple[str, str], ...]:
+ """Keep, for each name, only the ``(name, id)`` pairs from its best rank,
+ in first-seen order. A lower-ranked source never makes a name ambiguous."""
+ best: dict[str, int] = {}
+ for rank, name, _ in candidates:
+ key = name.casefold()
+ best[key] = min(rank, best.get(key, rank))
+ pairs: list[tuple[str, str]] = []
+ for rank, name, token_id in candidates:
+ pair = (name, token_id)
+ if name and rank == best[name.casefold()] and pair not in pairs:
+ pairs.append(pair)
+ return tuple(pairs)
+
+
+def _raw_user_mention_ids(text: str) -> list[str]:
+ """Distinct user ids written as raw ``<@id>`` / ``<@!id>`` tokens, capped."""
+ ids: list[str] = []
+ for match in _RAW_MENTION.finditer(text):
+ if match.group(1) == "&" or match.group(2) in ids:
+ continue
+ ids.append(match.group(2))
+ if len(ids) >= MENTION_QUERY_CAP:
+ break
+ return ids
+
+
def _mention_queries(text: str) -> list[str]:
"""The distinct first words following an ``@`` in ``text``, capped, used as
member-query prefixes. ``everyone`` and ``here`` are skipped outright."""
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py
index af99f2d0c1..957880fd61 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter_test.py
@@ -889,6 +889,160 @@ class TestCollectMentionableUsers:
assert not any(name == "@everyone" for name, _ in result)
assert not any(token == "1000" for _, token in result)
+ @pytest.mark.asyncio
+ async def test_typed_prefix_pings_a_member_with_a_decorated_name(self):
+ """The written "@Bently" must ping "Bently [SOMN]".
+
+ Discord matches `query` as a prefix, so the member is found — but
+ listing only their full display name and username meant the text the
+ author actually wrote matched neither, and the mention rendered as
+ plain text that pings nobody.
+ """
+ adapter, _ = _bare_adapter(bot_id=1000)
+ member = _mention(3000, "Bently [SOMN]")
+ member.name = "bentlybro"
+ member.bot = False
+ guild = _guild_with([], [])
+ guild.query_members = AsyncMock(return_value=[member])
+ channel = MagicMock(spec=discord.TextChannel)
+ channel.guild = guild
+
+ pairs = await adapter._mentionables_for(channel, "Paging @Bently", ())
+ rendered, allowed = _resolve_mentions("Paging @Bently", pairs)
+
+ assert rendered == "Paging <@3000>"
+ assert [obj.id for obj in allowed.users] == [3000]
+
+ @pytest.mark.asyncio
+ async def test_an_ambiguous_prefix_stays_plain_text(self):
+ # Two members share the typed prefix, so there is no way to know who
+ # was meant — pinging whichever sorts first would be worse than not
+ # pinging at all.
+ adapter, _ = _bare_adapter(bot_id=1000)
+ one = _mention(3000, "Bently [SOMN]")
+ one.name = "bentlybro"
+ one.bot = False
+ two = _mention(4000, "Bently Two")
+ two.name = "bently2"
+ two.bot = False
+ guild = _guild_with([], [])
+ guild.query_members = AsyncMock(return_value=[one, two])
+ channel = MagicMock(spec=discord.TextChannel)
+ channel.guild = guild
+
+ pairs = await adapter._mentionables_for(channel, "Paging @Bently", ())
+ rendered, allowed = _resolve_mentions("Paging @Bently", pairs)
+
+ assert rendered == "Paging @Bently"
+ assert allowed.users is False
+
+ @pytest.mark.asyncio
+ async def test_the_person_talking_to_the_bot_outranks_a_role_of_their_name(
+ self,
+ ):
+ """Live repro: the server has a "Bently" role, so "@Bently" belonged to
+ a user and a role, counted as a clash, and pinged nobody, though
+ Bently had just written to the bot."""
+ adapter, _ = _bare_adapter(bot_id=1000)
+ guild = _guild_with([], [_role(42, "Bently"), _role(43, "Platform")])
+ guild.query_members = AsyncMock(return_value=[])
+ channel = MagicMock(spec=discord.Thread)
+ channel.guild = guild
+ known = (("Bently", "353922987235213313"),)
+
+ text = "Hi @Bently! and @Platform"
+ pairs = await adapter._mentionables_for(channel, text, known)
+ rendered, allowed = _resolve_mentions(text, pairs)
+
+ assert rendered == "Hi <@353922987235213313>! and <@&43>"
+ assert [obj.id for obj in allowed.users] == [353922987235213313]
+ assert [obj.id for obj in allowed.roles] == [43]
+
+ @pytest.mark.asyncio
+ async def test_an_exact_member_name_outranks_a_prefix_match(self):
+ """ "@Sam" with members "Sam" and "Samantha" is Sam, not a clash."""
+ adapter, _ = _bare_adapter(bot_id=1000)
+ sam = _mention(3000, "Sam")
+ sam.name = "sam"
+ sam.bot = False
+ samantha = _mention(4000, "Samantha")
+ samantha.name = "samantha99"
+ samantha.bot = False
+ guild = _guild_with([], [])
+ guild.query_members = AsyncMock(return_value=[sam, samantha])
+ channel = MagicMock(spec=discord.TextChannel)
+ channel.guild = guild
+
+ pairs = await adapter._mentionables_for(channel, "ping @Sam", ())
+ rendered, allowed = _resolve_mentions("ping @Sam", pairs)
+
+ assert rendered == "ping <@3000>"
+ assert [obj.id for obj in allowed.users] == [3000]
+
+ @pytest.mark.asyncio
+ async def test_a_raw_id_the_model_wrote_pings_the_person_in_the_conversation(
+ self,
+ ):
+ """Live repro: the model wrote "Hi <@353922987235213313>!" from the id
+ in its prompt, and Discord rendered a mention that pinged nobody."""
+ adapter, _ = _bare_adapter(bot_id=1000)
+ guild = _guild_with([], [])
+ guild.query_members = AsyncMock(return_value=[])
+ channel = MagicMock(spec=discord.Thread)
+ channel.guild = guild
+ known = (("Bently", "353922987235213313"),)
+
+ text = "Hi <@353922987235213313>!"
+ pairs = await adapter._mentionables_for(channel, text, known)
+ rendered, allowed = _resolve_mentions(text, pairs)
+
+ assert rendered == text
+ assert [obj.id for obj in allowed.users] == [353922987235213313]
+
+ @pytest.mark.asyncio
+ async def test_a_raw_id_for_another_server_member_pings_them(self):
+ adapter, _ = _bare_adapter(bot_id=1000)
+ member = _mention(454545454545454545, "Nick")
+ member.bot = False
+ guild = _guild_with([], [])
+ guild.query_members = AsyncMock(return_value=[])
+ guild.get_member = MagicMock(return_value=member)
+ channel = MagicMock(spec=discord.TextChannel)
+ channel.guild = guild
+
+ text = "cc <@454545454545454545>"
+ pairs = await adapter._mentionables_for(channel, text, ())
+ _, allowed = _resolve_mentions(text, pairs)
+
+ assert [obj.id for obj in allowed.users] == [454545454545454545]
+
+ @pytest.mark.asyncio
+ async def test_a_raw_id_for_someone_outside_the_server_pings_nobody(self):
+ adapter, _ = _bare_adapter(bot_id=1000)
+ guild = _guild_with([], [])
+ guild.query_members = AsyncMock(return_value=[])
+ guild.get_member = MagicMock(return_value=None)
+ guild.fetch_member = AsyncMock(
+ side_effect=discord.NotFound(MagicMock(status=404), "Unknown Member")
+ )
+ channel = MagicMock(spec=discord.TextChannel)
+ channel.guild = guild
+
+ text = "Hi <@999999999999999999>"
+ pairs = await adapter._mentionables_for(channel, text, ())
+ rendered, allowed = _resolve_mentions(text, pairs)
+
+ assert pairs == ()
+ assert rendered == text
+ assert allowed.users is False
+
+ def test_a_raw_role_token_pings_only_an_allowlisted_role(self):
+ _, allowed = _resolve_mentions(
+ "Heads up <@&424242424242424242> and <@&434343434343434343>",
+ (("Platform", "role:424242424242424242"),),
+ )
+ assert [obj.id for obj in allowed.roles] == [424242424242424242]
+
@pytest.mark.asyncio
async def test_send_time_lookup_in_a_dm_keeps_only_known_users(self):
adapter, _ = _bare_adapter(bot_id=1000)
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter.py b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter.py
index a74e7c6b90..b91fbfb714 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter.py
@@ -444,7 +444,7 @@ class SlackAdapter(WebhookAdapter):
text=await self._strip_mentions(team, text),
bot_mentioned=bot_mentioned,
thread_history=thread_history,
- mentionable_users=await self._collect_mentionable_users(team, text),
+ mentionable_users=await self._collect_mentionable_users(team, text, user),
attachments=attachments,
skipped_attachments=skipped,
)
@@ -864,12 +864,14 @@ class SlackAdapter(WebhookAdapter):
return _USER_MENTION_RE.sub(_replace, text).strip()
async def _collect_mentionable_users(
- self, team_id: str, text: str
+ self, team_id: str, text: str, author_id: str = ""
) -> tuple[tuple[str, str], ...]:
+ """The author and everyone the inbound message mentioned. The author
+ is who the bot is answering, so "@name" back to them must ping."""
bot_id = await self._bot_user_id_for(team_id)
pairs: list[tuple[str, str]] = []
- for match in _USER_MENTION_RE.finditer(text):
- uid = match.group(1)
+ mentioned = [m.group(1) for m in _USER_MENTION_RE.finditer(text)]
+ for uid in ([author_id] if author_id else []) + mentioned:
if bot_id and uid == bot_id:
continue
pair = (await self._user_display_name(team_id, uid), uid)
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter_test.py
index d4c1ae9e75..bf6a576988 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/slack/adapter_test.py
@@ -105,6 +105,8 @@ class TestInboundRouting:
assert ctx.text == "hi there" # bot mention stripped
# The opaque channel_id carries the workspace so sends pick its token.
assert ctx.channel_id == "T1|C1|"
+ # The author is who the bot answers, so a reply naming them pings.
+ assert [uid for _, uid in ctx.mentionable_users] == ["U1"]
@pytest.mark.asyncio
async def test_thread_reply_is_not_bot_mentioned(self, adapter):
@@ -648,21 +650,22 @@ class TestOutbound:
@pytest.mark.asyncio
async def test_raw_control_sequences_are_escaped_but_allowlist_pings(self, adapter):
- # A model-output <!channel> or raw <@Uid> must be neutralized; only
- # the allowlisted @Bently comes back as a live mention token.
+ # A model-output <!channel>, or a raw <@Uid> for someone not on the
+ # allowlist, must be neutralized. The allowlisted person pings whether
+ # the model named them (@Bently) or wrote their id (<@U9>).
await adapter.send_message(
- "T1|C1|", "<!channel> <@U9> @Bently", (("Bently", "U9"),)
+ "T1|C1|", "<!channel> <@U8> <@U9> @Bently", (("Bently", "U9"),)
)
text = adapter._clients["T1"].chat_postMessage.await_args.kwargs["text"]
- assert text == "&lt;!channel&gt; &lt;@U9&gt; <@U9>"
+ assert text == "&lt;!channel&gt; &lt;@U8&gt; <@U9> <@U9>"
@pytest.mark.asyncio
async def test_allowlisted_name_with_escapable_chars_still_pings(self, adapter):
# Display names like "R&D" are raw; matching must happen before the
# text is escaped or the ping silently disappears.
- await adapter.send_message("T1|C1|", "hey @R&D, see <@U7>", (("R&D", "U7"),))
+ await adapter.send_message("T1|C1|", "hey @R&D, see <@U6>", (("R&D", "U7"),))
text = adapter._clients["T1"].chat_postMessage.await_args.kwargs["text"]
- assert text == "hey <@U7>, see &lt;@U7&gt;"
+ assert text == "hey <@U7>, see &lt;@U6&gt;"
@pytest.mark.asyncio
async def test_post_channel_message_chunks_before_escaping(self, adapter):
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter.py b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter.py
index f40ecf5a57..ce1b271891 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter.py
@@ -687,21 +687,26 @@ def _mentions_bot(activity: dict[str, Any]) -> bool:
def _mentionable_users(
activity: dict[str, Any],
) -> tuple[tuple[str, str], ...]:
- """Users the bot may ping back — those @mentioned in this message.
+ """Users the bot may ping back: the author, and those @mentioned in this
+ message.
- Teams offers no cheap roster read, so the allowlist is exactly who the
- author already addressed, which is the conservative reading of the shared
- mention-safety contract.
+ Teams offers no cheap roster read, so beyond the author the allowlist is
+ exactly who the author already addressed, which is the conservative
+ reading of the shared mention-safety contract.
"""
users: list[tuple[str, str]] = []
own = _bot_identities(activity)
+ sender = activity.get("from") or {}
+ if sender.get("id") and sender.get("name") and not _is_own_id(sender["id"], own):
+ users.append((sender["name"], sender["id"]))
for entity in activity.get("entities") or []:
if entity.get("type") != "mention":
continue
mentioned = entity.get("mentioned") or {}
user_id, name = mentioned.get("id"), mentioned.get("name")
if user_id and name and not _is_own_id(user_id, own):
- users.append((name, user_id))
+ if (name, user_id) not in users:
+ users.append((name, user_id))
return tuple(users)
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter_test.py
index d616984e16..f3991649c3 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/teams/adapter_test.py
@@ -342,7 +342,7 @@ async def test_a_users_mention_is_not_mistaken_for_the_bot(app_id):
ctx = await TeamsAdapter(MagicMock())._build_context(activity)
assert ctx is not None
assert ctx.bot_mentioned is False
- assert ctx.mentionable_users == (("Bob", "29:bob"),)
+ assert ctx.mentionable_users == (("Ada", "29:user"), ("Bob", "29:bob"))
@pytest.mark.asyncio
@@ -363,7 +363,16 @@ async def test_mentionable_users_exclude_the_bot(app_id):
)
ctx = await TeamsAdapter(MagicMock())._build_context(activity)
assert ctx is not None
- assert ctx.mentionable_users == (("Grace", "29:grace"),)
+ assert ctx.mentionable_users == (("Ada", "29:user"), ("Grace", "29:grace"))
+
+
+async def test_the_author_can_be_pinged_back_by_name_or_id(app_id):
+ """The bot is answering the author, so "@Ada" or "<@29:user>" back to
+ them must ping, even when the message mentioned nobody."""
+ ctx = await TeamsAdapter(MagicMock())._build_context(_activity())
+ assert ctx is not None
+ assert ctx.mentionable_users == (("Ada", "29:user"),)
# ── Threading ──────────────────────────────────────────────────────
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter.py b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter.py
index 0c38a8441e..5597dcd973 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter.py
@@ -49,6 +49,11 @@ from .text import to_html
logger = logging.getLogger(__name__)
+# A resolved mention, held behind private-use markers while the text is
+# HTML-escaped.
+_MENTION_OPEN = "\ue000"
+_MENTION_CLOSE = "\ue001"
+_MENTION_STASH_RE = re.compile("\ue000([^\ue000\ue001]+)\ue001")
_EXPIRED_NOTICE = "This question has expired — type your answer instead."
_NOT_YOUR_QUESTION = (
"This question was for someone else — they still need to answer it."
@@ -354,15 +359,25 @@ class TelegramAdapter(WebhookAdapter):
# -- Outbound --
def _render(self, text: str, mentionable_users: tuple[tuple[str, str], ...]) -> str:
- # Localize (which HTML-escapes) FIRST, then inject mention anchors —
- # the anchors are HTML and must survive escaping. The allowlist IS the
- # ping safety: non-allowlisted names stay plain text.
- rendered, _pinged = resolve_mentions(
- self.localize_markup(text),
+ # Resolve on the raw text, where names and "<@id>" look as the model
+ # wrote them, and hold each hit behind private-use markers that survive
+ # the HTML escaping (to_html strips NUL, so NUL can't be the marker).
+ # Then localize, then swap in the mention anchors, which are HTML and
+ # must not be escaped. The allowlist IS the ping safety: anything not
+ # on it stays plain, escaped text.
+ names = {uid: name for name, uid in mentionable_users}
+ resolved, _pinged = resolve_mentions(
+ text.replace(_MENTION_OPEN, "").replace(_MENTION_CLOSE, ""),
mentionable_users,
- lambda name, uid: f'<a href="tg://user?id={uid}">@{html.escape(name)}</a>',
+ lambda _name, uid: f"{_MENTION_OPEN}{uid}{_MENTION_CLOSE}",
+ )
+ return _MENTION_STASH_RE.sub(
+ lambda m: (
+ f'<a href="tg://user?id={m.group(1)}">'
+ f"@{html.escape(names.get(m.group(1), m.group(1)))}</a>"
+ ),
+ self.localize_markup(resolved),
)
- return rendered
async def send_message(
self,
@@ -671,10 +686,16 @@ def _context_from_callback_query(
def _collect_mentionable_users(message: dict[str, Any]) -> tuple[tuple[str, str], ...]:
- # text_mention entities carry full user objects (users without a public
- # @username); those are the only inbound mentions with a numeric id we
- # can ping safely on the way back out.
+ # The author, under their @username and first name, since either is what
+ # a reply to them would write. Then text_mention entities: they carry full
+ # user objects (users without a public @username), the only other inbound
+ # mentions with a numeric id we can ping safely on the way back out.
pairs: list[tuple[str, str]] = []
+ sender = message.get("from") or {}
+ if sender.get("id") and not sender.get("is_bot"):
+ for name in (sender.get("username"), sender.get("first_name")):
+ if name and (name, str(sender["id"])) not in pairs:
+ pairs.append((name, str(sender["id"])))
for entity in message.get("entities") or []:
user = entity.get("user")
if entity.get("type") == "text_mention" and user and not user.get("is_bot"):
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter_test.py
index ffa4b33451..b6cc52018f 100644
--- a/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/telegram/adapter_test.py
@@ -287,6 +287,34 @@ class TestOutbound:
assert kwargs["parse_mode"] == "HTML"
assert kwargs["text"] == "<b>bold</b> &amp; plain"
+ @pytest.mark.asyncio
+ async def test_text_that_looks_like_a_marker_is_left_alone(self):
+ """The stash markers are private-use characters, never text a model
+ writes, so ordinary words and hex like E000 pass through untouched."""
+ a = _adapter()
+ await a.send_message(
+ "-100555", "code E000 and E001 for @Bently", (("Bently", "7"),)
+ )
+ assert a._client.call.call_args.kwargs["text"] == (
+ 'code E000 and E001 for <a href="tg://user?id=7">@Bently</a>'
+ )
+
+ @pytest.mark.asyncio
+ async def test_mentions_by_name_and_id_ping_and_everything_else_is_escaped(
+ self,
+ ):
+ """The mention is resolved before HTML escaping, so "<@7>" the model
+ wrote is still recognisable, while an unknown "<@9>" and a
+ "<b>" it made up come out escaped."""
+ a = _adapter()
+ await a.send_message(
+ "-100555", "hi @Bently & <@7>, not <@9> <b>x</b>", (("Bently", "7"),)
+ )
+ anchor = '<a href="tg://user?id=7">@Bently</a>'
+ assert a._client.call.call_args.kwargs["text"] == (
+ f"hi {anchor} &amp; {anchor}, not &lt;@9&gt; &lt;b&gt;x&lt;/b&gt;"
+ )
+
@pytest.mark.asyncio
async def test_send_choice_buttons_sends_inline_keyboard(self):
a = _adapter()
@@ -384,6 +412,25 @@ def test_collect_mentionable_users_only_text_mentions_with_ids():
assert _collect_mentionable_users(message) == (("Sam", "5"),)
+def test_the_author_is_mentionable_by_username_and_first_name():
+ message = {
+ "from": {"id": 7, "first_name": "Bently", "username": "bentlybro"},
+ "entities": [
+ {"type": "text_mention", "user": {"id": 5, "first_name": "Sam"}},
+ ],
+ }
+ assert _collect_mentionable_users(message) == (
+ ("bentlybro", "7"),
+ ("Bently", "7"),
+ ("Sam", "5"),
+ )
+
+
+def test_a_bot_author_is_never_mentionable():
+ message = {"from": {"id": 9, "first_name": "Other", "is_bot": True}}
+ assert _collect_mentionable_users(message) == ()
+
+
async def _noop() -> None:
return None
diff --git a/autogpt_platform/backend/backend/copilot/bot/text.py b/autogpt_platform/backend/backend/copilot/bot/text.py
index 2f9432a7a2..46a4c964bf 100644
--- a/autogpt_platform/backend/backend/copilot/bot/text.py
+++ b/autogpt_platform/backend/backend/copilot/bot/text.py
@@ -1,7 +1,7 @@
"""Text formatting helpers — message batching and chunk splitting."""
import re
-from collections import Counter
+from collections import defaultdict
from typing import Callable, Iterator
from backend.data.sharing.workspace_refs import cut_lands_inside_artifact_link
@@ -90,51 +90,70 @@ def resolve_mentions(
mentionable_users: tuple[tuple[str, str], ...],
render_token: Callable[[str, str], str],
) -> tuple[str, list[str]]:
- """Substitute ``@DisplayName`` with a platform mention token, but only for
- users on ``mentionable_users``. Returns ``(rendered_text, pinged_user_ids)``.
-
- Security-sensitive shared policy: only allowlisted names are ever
- converted, so the bot never pings a user the LLM invented (``@everyone``,
- ``@here``, a hallucinated or elsewhere-learned name) — those stay plain
- text. Longest names first so ``@John Smith`` matches before ``@John``, and
- the match is word-bounded so ``@Name`` inside an email/URL is left alone.
- ``render_token(display_name, user_id)`` produces the platform's mention
- markup; the adapter turns ``pinged_user_ids`` into its own ping-safety
- object.
+ """Turn the mentions the model wrote into platform mention tokens, for
+ allowlisted people only. Returns ``(rendered_text, pinged_ids)``.
+
+ The model mentions someone in one of two ways, and both resolve here:
+
+ - by id, as ``<@ID>`` (or ``<@!ID>``). Every turn tells the model each
+ sender's user id, so this is the exact form and the one it reaches for.
+ - by name, as ``@Name``. Longest name first, so ``@John Smith`` wins over
+ ``@John``, and word-bounded, so an email or URL is left alone.
+
+ Security-sensitive shared policy: only ids and names on
+ ``mentionable_users`` ever become a ping. An id or name the model invented,
+ ``@everyone`` and ``@here`` stay exactly as written. A name that belongs to
+ two *different* ids is ambiguous and pings neither; the same person listed
+ under a name twice is not a clash. ``render_token(name, id)`` produces the
+ platform's markup, and the adapter turns ``pinged_ids`` into its own
+ ping-safety object.
"""
if not mentionable_users:
return text, []
- # Names that two different allowlisted users share case-insensitively are
- # ambiguous — we can't know which the author meant, so leave them plain
- # rather than ping whichever happens to sort first.
- name_counts = Counter(name.casefold() for name, _ in mentionable_users)
+ ids_by_name: dict[str, set[str]] = defaultdict(set)
+ for name, user_id in mentionable_users:
+ ids_by_name[name.casefold()].add(user_id)
users_by_name = {
name.casefold(): (name, user_id)
for name, user_id in mentionable_users
- if name_counts[name.casefold()] == 1
+ if len(ids_by_name[name.casefold()]) == 1
}
- if not users_by_name:
- return text, []
+ names_by_id: dict[str, str] = {}
+ for name, user_id in mentionable_users:
+ names_by_id.setdefault(user_id, name)
# ONE combined pattern + ONE sub() pass over the original text. re.sub never
- # re-scans replacement output, so a rendered token can't be matched again —
- # e.g. a display name equal to another user's ID inside an emitted <@U123>.
- # Longest alternative first so "@John Smith" wins over "@John"; the same
- # boundaries as before keep emails/URLs and "@John-Smith" prefixes unmatched.
- alternation = "|".join(
- re.escape(name)
- for name, _ in sorted(users_by_name.values(), key=lambda pair: -len(pair[0]))
- )
- pattern = re.compile(
- rf"(?<![\w@])@({alternation})(?![\w-])",
- re.IGNORECASE,
- )
+ # re-scans replacement output, so a rendered token can't be matched again,
+ # e.g. a display name equal to another user's id inside an emitted <@U123>.
+ alternatives: list[str] = [
+ r"<@!?(?P<id>"
+ + "|".join(
+ re.escape(user_id) for user_id in sorted(names_by_id, key=len, reverse=True)
+ )
+ + r")>"
+ ]
+ if users_by_name:
+ alternatives.append(
+ r"(?<![\w@])@(?P<name>"
+ + "|".join(
+ re.escape(name)
+ for name, _ in sorted(
+ users_by_name.values(), key=lambda pair: -len(pair[0])
+ )
+ )
+ + r")(?![\w-])"
+ )
+ pattern = re.compile("|".join(alternatives), re.IGNORECASE)
pinged: list[str] = []
def _render(match: re.Match[str]) -> str:
- display_name, user_id = users_by_name[match.group(1).casefold()]
+ if match.group("id") is not None:
+ user_id = _canonical_id(match.group("id"), names_by_id)
+ display_name = names_by_id[user_id]
+ else:
+ display_name, user_id = users_by_name[match.group("name").casefold()]
if user_id not in pinged:
pinged.append(user_id)
return render_token(display_name, user_id)
@@ -142,6 +161,17 @@ def resolve_mentions(
return pattern.sub(_render, text), pinged
+def _canonical_id(matched: str, names_by_id: dict[str, str]) -> str:
+ """The allowlisted id a case-insensitive match stands for. Ids are matched
+ ignoring case only because names share the pattern; the id itself is
+ returned exactly as it was allowlisted."""
+ if matched in names_by_id:
+ return matched
+ return next(
+ user_id for user_id in names_by_id if user_id.casefold() == matched.casefold()
+ )
+
+
def _guarded_cut(text: str, cut: int) -> int:
"""Keep a workspace artifact markdown link from being split across chunks.
diff --git a/autogpt_platform/backend/backend/copilot/bot/text_test.py b/autogpt_platform/backend/backend/copilot/bot/text_test.py
index 44b84f728a..9f7f57d886 100644
--- a/autogpt_platform/backend/backend/copilot/bot/text_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/text_test.py
@@ -92,6 +92,50 @@ class TestResolveMentions:
assert rendered == "hi @Bently"
assert pinged == []
+ def test_a_raw_id_the_model_wrote_pings_the_allowlisted_person(self):
+ """Live repro: every turn gives the model the sender's user id, so it
+ wrote "Hi <@353922987235213313>!" and that pinged nobody."""
+ rendered, pinged = resolve_mentions(
+ "Hi <@353922987235213313>!", (("Bently", "353922987235213313"),), _token
+ )
+ assert rendered == "Hi <@353922987235213313>!"
+ assert pinged == ["353922987235213313"]
+
+ def test_the_nickname_id_form_pings_too(self):
+ rendered, pinged = resolve_mentions("Hi <@!U1>", (("Bently", "U1"),), _token)
+ assert rendered == "Hi <@U1>"
+ assert pinged == ["U1"]
+
+ def test_an_id_that_is_not_allowlisted_stays_as_written(self):
+ rendered, pinged = resolve_mentions(
+ "Hi <@U999> and <@!U998>", (("Bently", "U1"),), _token
+ )
+ assert rendered == "Hi <@U999> and <@!U998>"
+ assert pinged == []
+
+ def test_name_and_id_for_the_same_person_ping_once(self):
+ rendered, pinged = resolve_mentions(
+ "@Bently and <@U1>", (("Bently", "U1"),), _token
+ )
+ assert rendered == "<@U1> and <@U1>"
+ assert pinged == ["U1"]
+
+ def test_one_person_listed_under_a_name_twice_is_not_a_clash(self):
+ """The same member reached twice (author and a member lookup, in two
+ casings) is one person, so the name still pings."""
+ rendered, pinged = resolve_mentions(
+ "hi @bently", (("Bently", "U1"), ("bently", "U1")), _token
+ )
+ assert rendered == "hi <@U1>"
+ assert pinged == ["U1"]
+
+ def test_an_ambiguous_name_does_not_block_that_persons_id(self):
+ rendered, pinged = resolve_mentions(
+ "@John or <@U2>", (("John", "U1"), ("John", "U2")), _token
+ )
+ assert rendered == "@John or <@U2>"
+ assert pinged == ["U2"]
+
class TestIterChunks:
def test_short_text_is_one_chunk(self):