diff --git a/gateway/run.py b/gateway/run.py index 83342c39591b1..9add1f7c7dd27 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -6723,10 +6723,25 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew _profile = get_active_profile_name() or "default" except Exception: _profile = None + # Honor per-platform extra overrides (mirrors SessionStore._generate_session_key + # and the adapters' text-batching keys) so the fallback path never diverges + # from the primary path on group/thread session isolation. + _group_sessions_per_user = getattr(config, "group_sessions_per_user", True) + _thread_sessions_per_user = getattr(config, "thread_sessions_per_user", False) + if config is not None and getattr(source, "platform", None) is not None: + _platform_cfg = getattr(config, "platforms", {}).get(source.platform) + if _platform_cfg and isinstance(getattr(_platform_cfg, "extra", None), dict): + _extra = _platform_cfg.extra + _group_sessions_per_user = _extra.get( + "group_sessions_per_user", _group_sessions_per_user + ) + _thread_sessions_per_user = _extra.get( + "thread_sessions_per_user", _thread_sessions_per_user + ) return build_session_key( source, - group_sessions_per_user=getattr(config, "group_sessions_per_user", True), - thread_sessions_per_user=getattr(config, "thread_sessions_per_user", False), + group_sessions_per_user=_group_sessions_per_user, + thread_sessions_per_user=_thread_sessions_per_user, profile=_profile, ) @@ -15999,6 +16014,18 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew ) or "" _group_sessions_per_user = getattr(self.config, "group_sessions_per_user", True) _thread_sessions_per_user = getattr(self.config, "thread_sessions_per_user", False) + # Resolve per-platform extra overrides so shared-session attribution + # (user_name prefixing) matches the session key that build_session_key + # produced for this source (see _session_key_for_source). + _platform_cfg = getattr(self.config, "platforms", {}).get(source.platform) + if _platform_cfg and isinstance(getattr(_platform_cfg, "extra", None), dict): + _extra = _platform_cfg.extra + _group_sessions_per_user = _extra.get( + "group_sessions_per_user", _group_sessions_per_user + ) + _thread_sessions_per_user = _extra.get( + "thread_sessions_per_user", _thread_sessions_per_user + ) # Prefer the already resolved session key from the caller so this write # key matches the consume key at the run_conversation site. Fall back # to deriving it here for tests and legacy standalone callers. diff --git a/gateway/session.py b/gateway/session.py index fa019133c0584..16dea6d4338ef 100644 --- a/gateway/session.py +++ b/gateway/session.py @@ -1732,13 +1732,38 @@ class SessionStore: def _generate_session_key(self, source: SessionSource) -> str: """Generate a session key from a source.""" + group_sessions_per_user, thread_sessions_per_user = self._resolve_session_isolation( + source + ) return build_session_key( source, - group_sessions_per_user=getattr(self.config, "group_sessions_per_user", True), - thread_sessions_per_user=getattr(self.config, "thread_sessions_per_user", False), + group_sessions_per_user=group_sessions_per_user, + thread_sessions_per_user=thread_sessions_per_user, profile=self._resolve_profile_for_key(source), ) + def _resolve_session_isolation( + self, source: SessionSource + ) -> tuple[bool, bool]: + """Resolve group/thread session isolation for a source. + + Per-platform ``extra.group_sessions_per_user`` / + ``extra.thread_sessions_per_user`` overrides win when present, + falling back to the global gateway config. This mirrors the + resolution the adapters use for text-batching keys so the main + dispatch path and the adapter's batching key never diverge. + """ + default_group = getattr(self.config, "group_sessions_per_user", True) + default_thread = getattr(self.config, "thread_sessions_per_user", False) + platform_cfg = getattr(self.config, "platforms", {}).get(source.platform) + if platform_cfg and isinstance(getattr(platform_cfg, "extra", None), dict): + extra = platform_cfg.extra + return ( + extra.get("group_sessions_per_user", default_group), + extra.get("thread_sessions_per_user", default_thread), + ) + return default_group, default_thread + def _legacy_slack_session_key(self, source: SessionSource) -> Optional[str]: """Return the pre-workspace Slack key for an explicitly scoped source. diff --git a/tests/gateway/test_session.py b/tests/gateway/test_session.py index 9b68bbc352b18..09b7d0ae023c0 100644 --- a/tests/gateway/test_session.py +++ b/tests/gateway/test_session.py @@ -687,6 +687,73 @@ class TestWhatsAppSessionKeyConsistency: assert second_entry.session_key == "agent:main:discord:group:guild-123" assert first_entry.session_id == second_entry.session_id + def test_store_honors_per_platform_group_sessions_override(self, store): + """Per-platform extra.group_sessions_per_user must win over the global + config in the main session-key path (mirrors the adapters' text-batching + keys). WhatsApp groups with the override set to False share one session + even though the global default is True.""" + from gateway.config import PlatformConfig + + store.config.group_sessions_per_user = True # global default: isolated + store.config.platforms[Platform.WHATSAPP] = PlatformConfig( + enabled=True, + extra={"group_sessions_per_user": False}, + ) + + alice = SessionSource( + platform=Platform.WHATSAPP, + chat_id="120363000000000000@g.us", + chat_type="group", + user_id="alice@lid", + user_name="Alice", + ) + bob = SessionSource( + platform=Platform.WHATSAPP, + chat_id="120363000000000000@g.us", + chat_type="group", + user_id="bob@lid", + user_name="Bob", + ) + + alice_entry = store.get_or_create_session(alice) + bob_entry = store.get_or_create_session(bob) + + # Shared group session — no per-user suffix, both resolve to the same key. + expected = "agent:main:whatsapp:group:120363000000000000@g.us" + assert alice_entry.session_key == expected + assert bob_entry.session_key == expected + assert alice_entry.session_id == bob_entry.session_id + + def test_store_keeps_global_isolation_when_no_platform_override(self, store): + """Without a per-platform override, the global default (isolated + per-user group sessions) must be preserved.""" + from gateway.config import PlatformConfig + + store.config.group_sessions_per_user = True + store.config.platforms[Platform.WHATSAPP] = PlatformConfig(enabled=True) + + alice = SessionSource( + platform=Platform.WHATSAPP, + chat_id="120363000000000000@g.us", + chat_type="group", + user_id="alice@lid", + user_name="Alice", + ) + bob = SessionSource( + platform=Platform.WHATSAPP, + chat_id="120363000000000000@g.us", + chat_type="group", + user_id="bob@lid", + user_name="Bob", + ) + + alice_entry = store.get_or_create_session(alice) + bob_entry = store.get_or_create_session(bob) + + # Isolated per-user sessions (global default) — distinct keys. + assert alice_entry.session_key != bob_entry.session_key + assert alice_entry.session_id != bob_entry.session_id + def test_telegram_dm_includes_chat_id(self): """Non-WhatsApp DMs should also include chat_id to separate users.""" source = SessionSource(