Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
perf(core): coalesce a room's change notices over one second
raullopez-sandbox2be820f0b5ff
diff --git a/core/switch_core/main.py b/core/switch_core/main.pyindex bd946b43b..e5c07b093 100644--- a/core/switch_core/main.py+++ b/core/switch_core/main.py@@ -199,7 +199,7 @@ from switch_core.process_identity import new_process_idfrom switch_core.process_lease import ProcessLeasefrom switch_core.provisioning import Provisioningfrom switch_core.provisioning.postgres import PostgresProvisioning-from switch_core.room_changes import RoomChanges+from switch_core.room_changes import COALESCE_SECONDS, RoomChangesfrom switch_core.room_service import RoomServicefrom switch_core.session_activity.listener import AgentSessionActivityListenerfrom switch_core.session_activity.maintenance import (@@ -1018,7 +1018,9 @@ async def run(config: SwitchConfig, process_id: str) -> None:user_changes = LocalUserChanges()user_change_publisher = SignalUserChanges(local=user_changes, bus=signal_bus)room_changes = RoomChanges(- session_factory=session_factory, publisher=user_change_publisher+ session_factory=session_factory,+ publisher=user_change_publisher,+ window_seconds=COALESCE_SECONDS,)room_service.on_room_changed(room_changes.room_changed)postgres_provisioning.on_membership_changed(room_changes.room_changed)diff --git a/core/switch_core/room_changes.py b/core/switch_core/room_changes.pyindex 086b7d892..39d6fa99d 100644--- a/core/switch_core/room_changes.py+++ b/core/switch_core/room_changes.py@@ -9,7 +9,11 @@ client that left is named so the person who lost the room is told too.The notice is a "read again", so it is sent from a task of its own after thechange has committed, and a lost one costs only a delay: Console still reads-its lists every few minutes.+its lists every few minutes. Changes to one room within `COALESCE_SECONDS` of+the first are sent as one notice per person, so a bridge adding a channel's N+members makes one read and N notices, not N reads and N² notices. The read+starts after the window closes, so it sees every change made in it; a change+after that opens a window of its own."""from __future__ import annotations@@ -40,6 +44,8 @@ if TYPE_CHECKING:logger = logging.getLogger(__name__)+COALESCE_SECONDS = 1.0+class RoomChanges:def __init__(@@ -47,15 +53,24 @@ class RoomChanges:*,session_factory: async_sessionmaker[AsyncSession],publisher: UserChangePublisher,+ window_seconds: float,) -> None:self._session_factory = session_factoryself._publisher = publisher+ self._window_seconds = window_secondsself._tasks: set[asyncio.Task[None]] = set()+ # Client ids gathered for each room whose window is open.+ self._pending: dict[tuple[str, str], set[str]] = {}def room_changed(self, tenant_id: str, room_id: str, client_ids: list[str]) -> None:"""Tell everyone the room concerns, and the people behind `client_ids`(who may just have left it), that it changed. Returns at once."""- task = asyncio.create_task(self._publish(tenant_id, room_id, client_ids))+ key = (tenant_id, room_id)+ if key in self._pending:+ self._pending[key].update(client_ids)+ return+ self._pending[key] = set(client_ids)+ task = asyncio.create_task(self._publish(tenant_id, room_id))self._tasks.add(task)task.add_done_callback(self._finished)@@ -68,9 +83,11 @@ class RoomChanges:exc_info=task.exception(),)- async def _publish(- self, tenant_id: str, room_id: str, client_ids: list[str]- ) -> None:+ async def _publish(self, tenant_id: str, room_id: str) -> None:+ try:+ await asyncio.sleep(self._window_seconds)+ finally:+ client_ids = sorted(self._pending.pop((tenant_id, room_id)))async with tenant_session(self._session_factory, tenant_id) as session:user_ids = await people_concerned(session, room_id, client_ids)for user_id in user_ids:diff --git a/core/tests/switch_core/gateway/test_chats_groups.py b/core/tests/switch_core/gateway/test_chats_groups.pyindex 29ed41363..d660abbce 100644--- a/core/tests/switch_core/gateway/test_chats_groups.py+++ b/core/tests/switch_core/gateway/test_chats_groups.py@@ -5,6 +5,7 @@ a thread — against real Postgres, through the shared harness."""from __future__ import annotationsimport asyncio+import uuidfrom collections.abc import Callablefrom typing import Any@@ -14,6 +15,7 @@ from fastapi import HTTPExceptionfrom sqlalchemy import func, selectfrom sqlalchemy.ext.asyncio import AsyncSession+from switch_core import room_changes as room_changes_modulefrom switch_core.bridges.agent.protocol.agent_connections import AgentConnectionRegistryfrom switch_core.bridges.agent.registration_bootstrap import (BOOTSTRAP_OWNER_MARKER_META_KEY,@@ -24,7 +26,7 @@ from switch_core.db.stores.room_store import RoomStorefrom switch_core.gateway import chats as chats_modulefrom switch_core.gateway import rooms as rooms_modulefrom switch_core.gateway.schemas import RoomUpdateRequest-from switch_core.room_changes import RoomChanges+from switch_core.room_changes import RoomChanges, people_concernedfrom switch_core.user_changes import ROOMfrom tests.switch_core.gateway.conftest import (_chat_service,@@ -33,6 +35,7 @@ from tests.switch_core.gateway.conftest import (_Harness,)from tests.switch_core.gateway.test_chats_routes import (+ TENANT,_code,_events_of,_left_chat,@@ -1252,7 +1255,9 @@ class _Published:async def test_people_are_told_when_a_room_of_theirs_changes(chats: _Harness) -> None:published = _Published()- changes = RoomChanges(session_factory=chats.factory, publisher=published)+ changes = RoomChanges(+ session_factory=chats.factory, publisher=published, window_seconds=0.0+ )chats.room_service.on_room_changed(changes.room_changed)chats.service._provisioning.on_membership_changed(changes.room_changed) # type: ignore[attr-defined]@@ -1292,11 +1297,48 @@ async def test_people_are_told_when_a_room_of_theirs_changes(chats: _Harness) ->assert await told() == {chats.alice.id}+async def test_changes_to_a_room_within_a_window_make_one_read_and_one_notice_each(+ chats: _Harness, monkeypatch: pytest.MonkeyPatch+) -> None:+ published = _Published()+ changes = RoomChanges(+ session_factory=chats.factory, publisher=published, window_seconds=0.2+ )+ mine = await chats.agent("mine", owner=chats.alice)+ room_id = await chats.new_chat(chats.alice, mine, "c1")+ reads: list[list[str]] = []++ async def counted(+ session: AsyncSession, room: str, client_ids: list[str]+ ) -> list[str]:+ reads.append(client_ids)+ return await people_concerned(session, room, client_ids)++ monkeypatch.setattr(room_changes_module, "people_concerned", counted)++ for _ in range(5):+ changes.room_changed(TENANT, room_id, [])+ # The last change in the window names a client that just left.+ left = str(uuid.uuid4())+ changes.room_changed(TENANT, room_id, [left])+ await asyncio.gather(*changes._tasks)+ assert reads == [[left]]+ assert published.notices == [(chats.alice.id, ROOM, room_id)]++ # A change after the window closed opens a window of its own.+ published.notices.clear()+ changes.room_changed(TENANT, room_id, [])+ await asyncio.gather(*changes._tasks)+ assert len(reads) == 2 and published.notices == [(chats.alice.id, ROOM, room_id)]++async def test_renaming_an_agent_tells_everyone_who_sees_its_rooms(chats: _Harness,) -> None:published = _Published()- changes = RoomChanges(session_factory=chats.factory, publisher=published)+ changes = RoomChanges(+ session_factory=chats.factory, publisher=published, window_seconds=0.0+ )chats.service.on_agent_room_changed(changes.room_changed)agent = await chats.agent("renamed", owner=chats.admin)room_id = await chats.new_chat(chats.alice, agent, "c1")