Skip to content

The agent that grows with you

fix(discord): a thread rename event retires Hermes's auto-thread title mask

John Paul Solivab85e24df3b8d
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py
index 4017f54aa61..e913ebf1059 100644
--- a/plugins/platforms/discord/adapter.py
+++ b/plugins/platforms/discord/adapter.py
@@ -1713,6 +1713,13 @@ class DiscordAdapter(DiscordMediaMixin, BasePlatformAdapter):
async def _on_platform_thread_update(self, before, after) -> None:
"""Normalize ``on_thread_update`` renames into ``thread_renamed``; non-rename updates are dropped."""
+ # The event is the only provenance signal: any name other than the one Hermes set came from
+ # someone else, even when its text matches the name Hermes replaced (cache lag looks the same).
+ thread_key = str(getattr(after, "id", ""))
+ renamed = self._semantic_thread_renames.get(thread_key)
+ if renamed and getattr(after, "name", None) != renamed[1]:
+ self._semantic_thread_renames.pop(thread_key, None)
+
def _build():
old_name = getattr(before, "name", None)
new_name = getattr(after, "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 f7d64a121b9..55b8b2e974e 100644
--- a/tests/gateway/test_discord_auto_thread_rename_pin.py
+++ b/tests/gateway/test_discord_auto_thread_rename_pin.py
@@ -85,3 +85,28 @@ async def test_hermes_title_rename_keeps_the_pin_and_a_human_rename_does_not(mon
# 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)
+
+
+async def test_a_restore_before_any_turn_retires_the_mask_through_the_update_event(monkeypatch):
+ # Restoring the opening name before Hermes's title was ever read looks like cache lag to the
+ # formatter; only the THREAD_UPDATE says someone else named it, so that event retires the record.
+ monkeypatch.setattr(discord_platform.discord, "Thread", _Thread, raising=False)
+ monkeypatch.setattr(discord_platform, "DISCORD_AVAILABLE", True)
+ monkeypatch.setenv("DISCORD_REQUIRE_MENTION", "false")
+ parent = _Text(700)
+ thread = _Thread(800, parent, name="what broke?")
+ adapter = DiscordAdapter(PlatformConfig(enabled=True, token="fake"))
+ adapter._client = SimpleNamespace(user=SimpleNamespace(id=999), get_channel=lambda _id: thread)
+
+ async def rename(name):
+ before = SimpleNamespace(id=thread.id, name=thread.name)
+ thread.name = name
+ await adapter._on_platform_thread_update(before, thread)
+
+ assert await adapter.rename_thread("800", "Database outage", only_if_current_name="what broke?")
+ await adapter._on_platform_thread_update(SimpleNamespace(id=800, name="what broke?"), thread)
+ await rename("what broke?")
+ assert adapter._format_thread_chat_name(thread).endswith("what broke?")
+ await rename("Database outage")
+ assert adapter._format_thread_chat_name(thread) == "Hermes Server / #ops / Database outage"
diff --git a/tests/gateway/test_discord_platform_events.py b/tests/gateway/test_discord_platform_events.py
index b57e7370ee0..997dd6baf10 100644
--- a/tests/gateway/test_discord_platform_events.py
+++ b/tests/gateway/test_discord_platform_events.py
@@ -83,6 +83,7 @@ def _adapter() -> DiscordAdapter:
a.platform = Platform.DISCORD
a.config = SimpleNamespace(extra={})
a.gateway_runner = None
+ a._semantic_thread_renames = {}
return a