Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(core): split a chats signal's room ids as well as its user ids

raullopez-sandboxdb1d58c699f9
diff --git a/core/switch_core/chats/signals.py b/core/switch_core/chats/signals.py
index 59b28f1b7..4dd7293f9 100644
--- a/core/switch_core/chats/signals.py
+++ b/core/switch_core/chats/signals.py
@@ -11,8 +11,8 @@ its next periodic re-read.
So each such change, once committed, announces `chats.memberships_changed`
naming the tenant and the people whose chats changed. Every other process
wakes the streams it holds for them, and each re-reads what its person may
-see. A change concerning more people than one signal can name is split over
-as many signals as it takes.
+see. A change concerning more people or rooms than one signal can name is
+split over as many signals as it takes.
Signals sent while this process's signal connection was down were never
queued, so when it comes back every stream held here is woken once: a wake-up
@@ -24,7 +24,7 @@ from __future__ import annotations
import json
from typing import TYPE_CHECKING, Any
-from switch_core.signals import payload_chunks
+from switch_core.signals import MAX_PAYLOAD_BYTES, payload_chunks
if TYPE_CHECKING:
from switch_core.chats.service import ChatService
@@ -54,15 +54,17 @@ class ChatSignals:
"""The users' part in chats `room_ids` changed and the change has
committed: every other process wakes the streams it holds for them.
This one has already woken its own."""
- for chunk in payload_chunks(
- user_ids, _SIGNAL_ENVELOPE_BYTES + len(json.dumps(room_ids))
- ):
- self._bus.publish(
- MEMBERSHIPS_CHANGED_SIGNAL,
- tenant_id=tenant_id,
- user_ids=chunk,
- room_ids=room_ids,
- )
+ # Room ids take at most half a payload, leaving the rest for user ids.
+ for rooms in payload_chunks(room_ids, MAX_PAYLOAD_BYTES // 2) or [[]]:
+ for users in payload_chunks(
+ user_ids, _SIGNAL_ENVELOPE_BYTES + len(json.dumps(rooms))
+ ):
+ self._bus.publish(
+ MEMBERSHIPS_CHANGED_SIGNAL,
+ tenant_id=tenant_id,
+ user_ids=users,
+ room_ids=rooms,
+ )
def _on_changed(self, signal: dict[str, Any]) -> None:
# A process on the release before room ids names none: any room.
diff --git a/core/tests/switch_core/gateway/test_chats_routes.py b/core/tests/switch_core/gateway/test_chats_routes.py
index 28da005c3..e1385bb4b 100644
--- a/core/tests/switch_core/gateway/test_chats_routes.py
+++ b/core/tests/switch_core/gateway/test_chats_routes.py
@@ -1269,6 +1269,25 @@ async def test_a_change_concerning_more_people_than_one_signal_holds_reaches_the
assert sorted(woken) == sorted(user_ids)
+async def test_a_change_naming_more_rooms_than_one_signal_holds_reaches_them_all(
+ two_processes: tuple[_Process, _Process],
+) -> None:
+ """An agent in hundreds of rooms changes every one of them at once: the
+ room ids are split over signals too, and every room reaches the stream."""
+ here, there = two_processes
+ user_id = str(uuid.uuid4())
+ room_ids = [str(uuid.uuid4()) for _ in range(500)]
+ woken: list[str] = []
+ there.service.watch_memberships(
+ TENANT, user_id, lambda rooms: woken.extend(rooms or [])
+ )
+
+ here.signals.memberships_changed(TENANT, [user_id], room_ids)
+
+ await _eventually(lambda: len(woken) == len(room_ids))
+ assert sorted(woken) == sorted(room_ids)
+
+
# ── Agent owners ─────────────────────────────────────────────────────────────
TENANT = "00000000-0000-0000-0000-000000000000"