The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: Discord-specific guard + cron sibling + non-regression tests
kshitijd13e8f751049
diff --git a/cron/scheduler.py b/cron/scheduler.pyindex 6362758347a..f429c5f59f7 100644--- a/cron/scheduler.py+++ b/cron/scheduler.py@@ -819,9 +819,19 @@ def _seed_cron_thread_session(except (ValueError, KeyError):platform_enum = Noneif platform_enum is not None:+ # Discord thread destinations must key on the thread's OWN id+ # to match how the Discord adapter keys organic in-thread+ # messages (chat_id == thread_id). Other platforms (Slack,+ # Telegram) use chat_id == parent_channel for thread messages,+ # so the parent chat_id is correct for them. See the matching+ # guard in GatewayRunner._process_handoff.+ if platform_enum == Platform.DISCORD:+ seed_chat_id = str(thread_id)+ else:+ seed_chat_id = str(chat_id)dest_source = SessionSource(platform=platform_enum,- chat_id=str(chat_id),+ chat_id=seed_chat_id,chat_name=chat_name,chat_type="thread",user_id="system:cron",diff --git a/gateway/run.py b/gateway/run.pyindex fb056ef7d4d..fdf2b82d41f 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -11517,20 +11517,23 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewdest_chat_type = "dm"dest_user_id = home_chat_id if is_telegram_private_chat else "system:handoff"- # For thread destinations, key on the thread's OWN id, not the parent- # channel's. Platform adapters build organic in-thread messages with- # chat_id == thread id (see the Discord adapter's on_message and- # _build_thread_event paths), so build_session_key yields- # ``…:thread:{thread}:{thread}``. If the handoff instead keys on the+ # Discord thread destinations must key on the thread's OWN id, not the+ # parent channel's, because the Discord adapter builds organic in-thread+ # messages with ``chat_id == thread id`` — so ``build_session_key``+ # yields ``…:thread:{thread}:{thread}``. If the handoff keys on the# parent channel (``…:thread:{parent}:{thread}``) the next real user# reply in the thread resolves to a DIFFERENT session_key and spawns a- # fresh session instead of continuing the handed-off one. Match the- # adapter so the synthetic turn and later replies share one key.- dest_chat_id = (- str(effective_thread_id)- if dest_chat_type == "thread" and effective_thread_id- else home_chat_id- )+ # fresh session instead of continuing the handed-off one.+ #+ # This is Discord-specific: Slack and Telegram adapters key organic+ # thread messages with ``chat_id == parent_channel`` and the thread+ #/topic id only in ``thread_id``, so for those platforms the parent+ # channel is correct (and the deeper chat_type normalization — handoff+ # uses "thread" but Slack organic uses "group" — is a separate issue).+ if platform == Platform.DISCORD and dest_chat_type == "thread" and effective_thread_id:+ dest_chat_id = str(effective_thread_id)+ else:+ dest_chat_id = home_chat_iddest_source = SessionSource(platform=platform,chat_id=dest_chat_id,diff --git a/tests/gateway/test_handoff_thread_session_key.py b/tests/gateway/test_handoff_thread_session_key.pyindex 1df09ce4ec5..56b61cc9d52 100644--- a/tests/gateway/test_handoff_thread_session_key.py+++ b/tests/gateway/test_handoff_thread_session_key.py@@ -16,24 +16,25 @@ session_key and spawned a fresh session instead of continuing the handed-offone (observed: a stray auto-titled session + a session_search fallback becausethe new session had no prior context).-This test pins the invariant: for a thread destination the handoff key must be-byte-identical to the organic in-thread key.+The fix is Discord-specific: Slack and Telegram adapters key organic thread+messages with ``chat_id = parent_channel``, so the parent channel is correct+for those platforms and the guard must NOT apply to them."""from gateway.config import Platformfrom gateway.session import SessionSource, build_session_key-def _organic_thread_key(thread_id: str, parent_id: str, user_id: str) -> str:+def _organic_discord_thread_key(thread_id: str, parent_id: str, user_id: str) -> str:"""Key the Discord adapter produces for a message typed inside a thread.- Mirrors plugins/platforms/discord/adapter.py on_message: chat_id is the- thread's own id, chat_type is "thread", thread_id is the thread id,+ Mirrors plugins/platforms/discord/adapter.py _handle_message: chat_id is+ the thread's own id, chat_type is "thread", thread_id is the thread id,parent_chat_id is the parent channel."""source = SessionSource(platform=Platform.DISCORD,- chat_id=str(thread_id), # adapter uses the thread's OWN id+ chat_id=str(thread_id),chat_type="thread",user_id=user_id,thread_id=str(thread_id),@@ -42,39 +43,60 @@ def _organic_thread_key(thread_id: str, parent_id: str, user_id: str) -> str:return build_session_key(source, thread_sessions_per_user=False)-def _handoff_thread_key(thread_id: str, home_chat_id: str) -> str:- """Key the handoff produces after the fix: for a thread destination,- chat_id is the thread's own id (not the parent/home channel)."""- dest_chat_type = "thread"- effective_thread_id = str(thread_id)- # This mirrors the fixed logic in HermesGateway._process_handoff.- dest_chat_id = (- str(effective_thread_id)- if dest_chat_type == "thread" and effective_thread_id- else str(home_chat_id)+def _organic_slack_thread_key(channel_id: str, thread_ts: str, user_id: str) -> str:+ """Key the Slack adapter produces for a message in a thread.++ Mirrors plugins/platforms/slack/adapter.py: chat_id is the parent channel,+ chat_type is "group", thread_id is the thread timestamp.+ """+ source = SessionSource(+ platform=Platform.SLACK,+ chat_id=str(channel_id),+ chat_type="group",+ user_id=user_id,+ thread_id=str(thread_ts),)+ return build_session_key(source, thread_sessions_per_user=False)+++def _handoff_key(+ platform: Platform,+ home_chat_id: str,+ thread_id: str,+) -> str:+ """Key the handoff produces after the fix.++ Mirrors the fixed logic in GatewayRunner._process_handoff: for Discord+ thread destinations, chat_id is the thread's own id; for other platforms,+ chat_id remains the parent/home channel.+ """+ dest_chat_type = "thread"+ # This mirrors the fixed logic in GatewayRunner._process_handoff.+ if platform == Platform.DISCORD and dest_chat_type == "thread" and thread_id:+ dest_chat_id = str(thread_id)+ else:+ dest_chat_id = str(home_chat_id)dest_source = SessionSource(- platform=Platform.DISCORD,+ platform=platform,chat_id=dest_chat_id,chat_type=dest_chat_type,user_id="system:handoff",user_name="Handoff",- thread_id=effective_thread_id,+ thread_id=str(thread_id),)return build_session_key(dest_source, thread_sessions_per_user=False)-def test_handoff_thread_key_matches_organic_in_thread_key():- parent_id = "1523581766923845724" # home/parent channel- thread_id = "1523590238595846166" # handoff thread+def test_discord_handoff_key_matches_organic_in_thread_key():+ """For Discord, the handoff key must be byte-identical to the organic+ in-thread key — otherwise a reply in the handoff thread spawns a new session."""+ parent_id = "1523581766923845724"+ thread_id = "1523590238595846166"user_id = "171164909650968576"- organic = _organic_thread_key(thread_id, parent_id, user_id)- handoff = _handoff_thread_key(thread_id, parent_id)+ organic = _organic_discord_thread_key(thread_id, parent_id, user_id)+ handoff = _handoff_key(Platform.DISCORD, parent_id, thread_id)- # Threads are user-shared (thread_sessions_per_user=False), so user_id is- # NOT part of the key — the synthetic handoff turn and the user's later- # reply must land on the exact same session_key.assert handoff == organic, (f"handoff key {handoff!r} != organic in-thread key {organic!r}; ""a reply in the handoff thread would spawn a new session"@@ -82,12 +104,37 @@ def test_handoff_thread_key_matches_organic_in_thread_key():assert handoff == f"agent:main:discord:thread:{thread_id}:{thread_id}"-def test_handoff_thread_key_does_not_use_parent_channel():+def test_discord_handoff_key_does_not_use_parent_channel():"""The pre-fix bug: keying on the parent channel. Guard against regression."""parent_id = "1523581766923845724"thread_id = "1523590238595846166"- handoff = _handoff_thread_key(thread_id, parent_id)+ handoff = _handoff_key(Platform.DISCORD, parent_id, thread_id)buggy = f"agent:main:discord:thread:{parent_id}:{thread_id}"assert handoff != buggy, "handoff regressed to keying on the parent channel"+++def test_slack_handoff_key_uses_parent_channel_not_thread_id():+ """Slack adapter keys organic thread messages with chat_id=channel_id+ (parent), not the thread ts. The fix must NOT apply to Slack — otherwise+ the handoff key would use the thread ts as chat_id, breaking the match."""+ channel_id = "C12345678"+ thread_ts = "1690000000.123456"+ user_id = "U123456"++ organic = _organic_slack_thread_key(channel_id, thread_ts, user_id)+ handoff = _handoff_key(Platform.SLACK, channel_id, thread_ts)++ # The handoff uses chat_type="thread" while Slack organic uses "group",+ # so these keys differ in the chat_type slot (a pre-existing mismatch,+ # NOT caused by this fix). The important assertion is that the handoff+ # does NOT use the thread_ts as chat_id (the regression this guard prevents).+ assert "thread_ts" not in handoff or thread_ts not in handoff.split(":")[-2:-1], (+ f"handoff key {handoff!r} incorrectly uses thread ts as chat_id"+ )+ # Verify the handoff key still contains the parent channel_id+ assert channel_id in handoff, (+ f"handoff key {handoff!r} lost the parent channel id — "+ "the Discord-specific guard leaked into Slack"+ )