Skip to content

The agent that grows with you

fix(discord): retire the auto-thread rename mask once someone else renames the thread

John Paul Soliva019ac2a3e83f
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py
index eec0d0afe95..4017f54aa61 100644
--- a/plugins/platforms/discord/adapter.py
+++ b/plugins/platforms/discord/adapter.py
@@ -1114,9 +1114,9 @@ class DiscordAdapter(DiscordMediaMixin, BasePlatformAdapter):
self._voice_fx_cfg: Dict[str, Any] = self._load_voice_fx_config()
# Threads the bot participated in (no @mention needed there); persisted across restarts.
self._threads = ThreadParticipationTracker("discord")
- # thread id -> (name it replaced, name it set) for Hermes's own semantic renames; see
- # _format_thread_chat_name. In memory: after a restart the next turn re-renders once.
- self._semantic_thread_renames: Dict[str, Tuple[str, str]] = {}
+ # thread id -> (name it replaced, name it set, set name seen yet) for Hermes's own semantic
+ # renames; see _format_thread_chat_name. In memory: after a restart the next turn re-renders once.
+ self._semantic_thread_renames: Dict[str, Tuple[str, str, bool]] = {}
# Persistent typing loops per channel (DMs don't reliably show bot typing events).
self._typing_tasks: Dict[str, asyncio.Task] = {}
self._bot_task: Optional[asyncio.Task] = None
@@ -5459,7 +5459,7 @@ class DiscordAdapter(DiscordMediaMixin, BasePlatformAdapter):
if only_if_current_name is not None:
renames = self._semantic_thread_renames
renames.pop(str(thread_id_int), None)
- renames[str(thread_id_int)] = (current_name, cleaned)
+ renames[str(thread_id_int)] = (current_name, cleaned, False)
while len(renames) > _SEMANTIC_THREAD_RENAMES_MAX:
renames.pop(next(iter(renames)))
logger.info(
@@ -5808,9 +5808,19 @@ class DiscordAdapter(DiscordMediaMixin, BasePlatformAdapter):
# chat_name keys the pinned session-context prompt, and Hermes's own title rename lands
# between a new thread's first and second turns: keep the name the turns were pinned under.
# A rename by anyone else no longer matches what Hermes set, so it still re-renders.
- renamed = self._semantic_thread_renames.get(str(getattr(thread, "id", "")))
- if renamed and renamed[1] == thread_name:
- thread_name = renamed[0]
+ thread_key = str(getattr(thread, "id", ""))
+ renamed = self._semantic_thread_renames.get(thread_key)
+ if renamed:
+ replaced, set_name, set_seen = renamed
+ if thread_name == set_name:
+ if not set_seen:
+ self._semantic_thread_renames[thread_key] = (replaced, set_name, True)
+ thread_name = replaced
+ elif set_seen or thread_name != replaced:
+ # Someone else renamed it (the old name before Hermes's title is first seen is only
+ # cache lag). Equal text is not provenance: if they later restore Hermes's title,
+ # that is their choice of name and must show as itself.
+ self._semantic_thread_renames.pop(thread_key, None)
parent = getattr(thread, "parent", None)
guild = getattr(thread, "guild", None) or getattr(parent, "guild", None)
guild_name = getattr(guild, "name", None)
diff --git a/tests/gateway/test_discord_auto_thread_rename_pin.py b/tests/gateway/test_discord_auto_thread_rename_pin.py
index 7613c2c7bf2..f7d64a121b9 100644
--- a/tests/gateway/test_discord_auto_thread_rename_pin.py
+++ b/tests/gateway/test_discord_auto_thread_rename_pin.py
@@ -4,7 +4,8 @@
auto-thread once the LLM title arrives, normally before the user's second message, so turn 2 read
the new name and re-rendered the already-sent prompt: a prompt-cache miss on turn 2 of every
auto-threaded conversation on the default config. A rename by anyone else is a real metadata
-change and still re-renders.
+change and still re-renders, including one that later restores Hermes's title: equal text is not
+edit provenance.
"""
from datetime import datetime, timezone
@@ -45,7 +46,8 @@ def _message(channel, message_id):
@pytest.mark.asyncio
-async def test_hermes_title_rename_keeps_the_pin_and_a_human_rename_does_not(monkeypatch):
[email protected]("moderator_name", ["Renamed by a moderator", "what broke?"])
+async def test_hermes_title_rename_keeps_the_pin_and_a_human_rename_does_not(monkeypatch, moderator_name):
monkeypatch.setattr(discord_platform.discord, "Thread", _Thread, raising=False)
monkeypatch.setattr(discord_platform, "DISCORD_AVAILABLE", True)
monkeypatch.setenv("DISCORD_REQUIRE_MENTION", "false")
@@ -72,9 +74,14 @@ async def test_hermes_title_rename_keeps_the_pin_and_a_human_rename_does_not(mon
# The title lane's call, as gateway/run_topics.py makes it for a native auto-thread.
assert await adapter.rename_thread("800", "Database outage", only_if_current_name="what broke?")
assert thread.name == "Database outage"
+ # A message read through a cache that has not caught up with the edit still sees the old name.
+ thread.name = "what broke?"
+ assert await turn(thread, 104) == first
+ thread.name = "Database outage"
assert await turn(thread, 101) == first
- thread.name = "Renamed by a moderator"
- after_human_rename = await turn(thread, 102)
- assert after_human_rename != first
- assert "Renamed by a moderator" in after_human_rename
+ thread.name = moderator_name
+ assert f"Hermes Server / #ops / {moderator_name}" in await turn(thread, 102)
+ # The moderator then restores Hermes's title: it shows as itself, not as the opening name.
+ thread.name = "Database outage"
+ assert "Hermes Server / #ops / Database outage" in await turn(thread, 103)