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(chats): list chats and recheck a stream in a fixed number of reads
raullopez-sandboxb21b3b80e09e
diff --git a/core/switch_core/chats/service.py b/core/switch_core/chats/service.pyindex f8a467c4f..c436d8965 100644--- a/core/switch_core/chats/service.py+++ b/core/switch_core/chats/service.py@@ -392,9 +392,20 @@ class ChatService:) -> bool:if room.owner_id is not None and room.owner_id == user.id:return True+ return await self.administers(session, tenant_id, user)++ async def administers(+ self, session: AsyncSession, tenant_id: str, user: User+ ) -> bool:+ """Whether the user manages every room in the tenant (`can_manage`)."""role = await self._user_store.tenant_role(session, tenant_id, user.id)return administers_tenant(is_operator=user.role == "admin", tenant_role=role)+ async def head_seqs(+ self, session: AsyncSession, room_ids: list[str]+ ) -> dict[str, int]:+ return await self._message_store.head_seqs(session, room_ids)+async def _require_manager(self, session: AsyncSession, tenant_id: str, user: User, room: Room) -> None:@@ -690,23 +701,37 @@ class ChatService:async def stream_rooms(self, session: AsyncSession, user_id: str) -> list[Room]:"""The caller's member rooms that are chats: unarchived, and eitherholding an agent or started in Switch Console (which may have none)."""+ return [+ room+ for room, listed in await self.readable_and_listed(session, user_id)+ if listed+ ]++ async def readable_and_listed(+ self, session: AsyncSession, user_id: str+ ) -> list[tuple[Room, bool]]:+ """`readable_rooms`, each with whether it is one of `stream_rooms`, in+ one query."""client = await self.member_client(session, user_id)if client is None:return []+ listed = or_(+ exists().where(room_agents.c.room_id == Room.id),+ Room.metadata_.has_key("chat_operation_id"),+ )result = await session.execute(- select(Room)+ select(Room, listed).join(ClientRoom, ClientRoom.room_id == Room.id)- .where(- ClientRoom.client_id == client.id,- Room.archived_at.is_(None),- or_(- exists().where(room_agents.c.room_id == Room.id),- Room.metadata_.has_key("chat_operation_id"),- ),- )+ .where(ClientRoom.client_id == client.id, Room.archived_at.is_(None)).order_by(Room.created_at))- return list(result.scalars().all())+ return [(room, bool(is_listed)) for room, is_listed in result.all()]++ async def moved_rooms(+ self, session: AsyncSession, cursors: dict[str, int]+ ) -> set[str]:+ """Of the rooms in `cursors`, those with a row past their cursor."""+ return await self._message_store.rooms_past(session, cursors)async def room_activity(self, session: AsyncSession, room_id: strdiff --git a/core/switch_core/chats/views.py b/core/switch_core/chats/views.pyindex 3b5b0d361..03a5fc65d 100644--- a/core/switch_core/chats/views.py+++ b/core/switch_core/chats/views.py@@ -6,13 +6,13 @@ describe a room in exactly the same terms.from __future__ import annotations-from collections.abc import Sequence+from collections.abc import Mapping, Sequencefrom datetime import datetimefrom typing import Literalfrom pydantic import BaseModel, ConfigDictfrom pydantic.alias_generators import to_camel-from sqlalchemy import func, select+from sqlalchemy import and_, func, selectfrom sqlalchemy.ext.asyncio import AsyncSessionfrom switch_core.chats import MEMBER_CLIENT_TYPE@@ -170,109 +170,161 @@ async def _bridge(session: AsyncSession, room: Room) -> CollaborationBridge | Noasync def chat_summary(session: AsyncSession, room: Room, *, viewer_id: str, can_manage: bool) -> ChatSummary:- rows = (- await session.execute(- select(Agent, room_agents.c.alias, User.name)- .join(room_agents, room_agents.c.agent_id == Agent.id)- .outerjoin(User, User.id == Agent.owner_id)- .where(room_agents.c.room_id == room.id)- .order_by(Agent.name)- )- ).all()- agents = [agent for agent, _alias, _owner in rows]- providers = {- definition.agent_id: definition.definition.get("provider")- for definition in (- await session.execute(- select(AgentDefinition).where(- AgentDefinition.agent_id.in_([agent.id for agent in agents])+ [summary] = await chat_summaries(+ session, [room], viewer_id=viewer_id, can_manage={room.id: can_manage}+ )+ return summary+++async def chat_summaries(+ session: AsyncSession,+ rooms: Sequence[Room],+ *,+ viewer_id: str,+ can_manage: Mapping[str, bool],+) -> list[ChatSummary]:+ """The rooms' summaries for the viewer, in the order given, read with one+ query per kind of fact (agents, providers, bridges, people, unread counts,+ last messages) whatever the number of rooms."""+ if not rooms:+ return []+ room_ids = [room.id for room in rooms]+ agent_rows: dict[str, list[tuple[Agent, str | None, str | None]]] = {}+ for room_id, agent, alias, owner_name in await session.execute(+ select(room_agents.c.room_id, Agent, room_agents.c.alias, User.name)+ .join(room_agents, room_agents.c.agent_id == Agent.id)+ .outerjoin(User, User.id == Agent.owner_id)+ .where(room_agents.c.room_id.in_(room_ids))+ .order_by(Agent.name)+ ):+ agent_rows.setdefault(room_id, []).append((agent, alias, owner_name))+ agent_ids = {agent.id for rows in agent_rows.values() for agent, _, _ in rows}+ providers = (+ {+ definition.agent_id: definition.definition.get("provider")+ for definition in (+ await session.execute(+ select(AgentDefinition).where(+ AgentDefinition.agent_id.in_(agent_ids)+ ))- )- ).scalars()- }- bridge = await _bridge(session, room)- member_user_ids = sorted(- user_id- for user_id in (- await session.execute(- select(Client.user_id)- .join(ClientRoom, ClientRoom.client_id == Client.id)- .where(ClientRoom.room_id == room.id, Client.type == MEMBER_CLIENT_TYPE)- )- ).scalars()- if user_id is not None+ ).scalars()+ }+ if agent_ids+ else {}+ )+ bridge_ids = {room.bridge_id for room in rooms if room.bridge_id is not None}+ bridges = (+ {+ bridge.id: bridge+ for bridge in (+ await session.execute(+ select(CollaborationBridge).where(+ CollaborationBridge.id.in_(bridge_ids)+ )+ )+ ).scalars()+ }+ if bridge_ids+ else {})- unread_count = await session.scalar(- select(func.count())+ members: dict[str, list[str]] = {}+ for room_id, user_id in await session.execute(+ select(ClientRoom.room_id, Client.user_id)+ .join(Client, Client.id == ClientRoom.client_id)+ .where(ClientRoom.room_id.in_(room_ids), Client.type == MEMBER_CLIENT_TYPE)+ ):+ if user_id is not None:+ members.setdefault(room_id, []).append(user_id)+ viewer_client = (+ select(Client.id)+ .where(Client.user_id == viewer_id, Client.type == MEMBER_CLIENT_TYPE)+ .scalar_subquery()+ )+ unread: dict[str, int] = {}+ for room_id, count in await session.execute(+ select(Message.room_id, func.count()).select_from(Message)- .where(- Message.room_id == room.id,- Message.event_type.in_(SHOWN_EVENT_TYPES),- Message.seq- > func.coalesce(- select(ChatRead.read_through_seq)- .where(ChatRead.user_id == viewer_id, ChatRead.room_id == room.id)- .scalar_subquery(),- 0,- ),- Message.sender_client_id.is_distinct_from(- select(Client.id)- .where(Client.user_id == viewer_id, Client.type == MEMBER_CLIENT_TYPE)- .scalar_subquery()+ .outerjoin(+ ChatRead,+ and_(+ ChatRead.room_id == Message.room_id,+ ChatRead.user_id == viewer_id,),)- )- channel_name: str | None = None- if bridge is not None and room.external_channel_id is not None:- prefix = f"{bridge.display_name}: "- channel_name = (- room.name[len(prefix) :] if room.name.startswith(prefix) else room.name+ .where(+ Message.room_id.in_(room_ids),+ Message.event_type.in_(SHOWN_EVENT_TYPES),+ Message.seq > func.coalesce(ChatRead.read_through_seq, 0),+ Message.sender_client_id.is_distinct_from(viewer_client),)- last = (- await session.execute(- select(Message)- .where(- Message.room_id == room.id, Message.event_type.in_(SHOWN_EVENT_TYPES)+ .group_by(Message.room_id)+ ):+ unread[room_id] = count+ last_messages = {+ message.room_id: message+ for message in (+ await session.execute(+ select(Message)+ .where(+ Message.room_id.in_(room_ids),+ Message.event_type.in_(SHOWN_EVENT_TYPES),+ )+ .distinct(Message.room_id)+ .order_by(Message.room_id, Message.seq.desc()))- .order_by(Message.seq.desc())- .limit(1)- )- ).scalar_one_or_none()- return ChatSummary(- room_id=room.id,- name=room.name,- channel_type=room.channel_type or "channel_public",- bridge_type=bridge.type if bridge is not None else None,- channel_name=channel_name,- agents=[- ChatAgent(- id=agent.id,- name=agent.name,- display_name=agent.display_name,- icon_url=agent.icon_url,- provider=_provider(agent, providers.get(agent.id)),- alias=alias,- owner_name=owner_name,- owned_by_viewer=agent.owner_id == viewer_id,+ ).scalars()+ }+ summaries: list[ChatSummary] = []+ for room in rooms:+ rows = agent_rows.get(room.id, [])+ bridge = bridges.get(room.bridge_id) if room.bridge_id else None+ channel_name: str | None = None+ if bridge is not None and room.external_channel_id is not None:+ prefix = f"{bridge.display_name}: "+ channel_name = (+ room.name[len(prefix) :] if room.name.startswith(prefix) else room.name)- for agent, alias, owner_name in rows- ],- can_manage=can_manage,- owns_agent=any(agent.owner_id == viewer_id for agent in agents),- member_count=len(member_user_ids),- member_user_ids=member_user_ids,- unread_count=unread_count or 0,- last_message=(- ChatLastMessage(- seq=last.seq,- sent_at=_iso(last.sent_at),- preview=(last.body or _command_text(last))[:PREVIEW_CHARS],- sender_name=last.sender_name or "",+ member_user_ids = sorted(members.get(room.id, []))+ last = last_messages.get(room.id)+ summaries.append(+ ChatSummary(+ room_id=room.id,+ name=room.name,+ channel_type=room.channel_type or "channel_public",+ bridge_type=bridge.type if bridge is not None else None,+ channel_name=channel_name,+ agents=[+ ChatAgent(+ id=agent.id,+ name=agent.name,+ display_name=agent.display_name,+ icon_url=agent.icon_url,+ provider=_provider(agent, providers.get(agent.id)),+ alias=alias,+ owner_name=owner_name,+ owned_by_viewer=agent.owner_id == viewer_id,+ )+ for agent, alias, owner_name in rows+ ],+ can_manage=can_manage[room.id],+ owns_agent=any(agent.owner_id == viewer_id for agent, _, _ in rows),+ member_count=len(member_user_ids),+ member_user_ids=member_user_ids,+ unread_count=unread.get(room.id, 0),+ last_message=(+ ChatLastMessage(+ seq=last.seq,+ sent_at=_iso(last.sent_at),+ preview=(last.body or _command_text(last))[:PREVIEW_CHARS],+ sender_name=last.sender_name or "",+ )+ if last is not None+ else None+ ),)- if last is not None- else None- ),- )+ )+ return summariesdef _command_text(message: Message) -> str:diff --git a/core/switch_core/db/stores/message_store.py b/core/switch_core/db/stores/message_store.pyindex 26ce03cce..31313d92f 100644--- a/core/switch_core/db/stores/message_store.py+++ b/core/switch_core/db/stores/message_store.py@@ -1,7 +1,7 @@from collections.abc import Collectionfrom datetime import datetime-from sqlalchemy import func, or_, select, text+from sqlalchemy import BigInteger, Text, and_, column, func, or_, select, text, valuesfrom sqlalchemy.ext.asyncio import AsyncSessionfrom switch_core.db.models import Message, MessageAttachment@@ -190,6 +190,29 @@ class MessageStore:heads = {room_id: max(int(seq or 0), 0) for room_id, seq in result.all()}return {room_id: heads.get(room_id, 0) for room_id in room_ids}+ async def rooms_past(+ self, session: AsyncSession, cursors: dict[str, int]+ ) -> set[str]:+ """Of the rooms in `cursors`, the ones with a row after their cursor,+ in one query whose result is only those rooms."""+ if not cursors:+ return set()+ position = values(+ column("room_id", Text), column("seq", BigInteger), name="position"+ ).data(list(cursors.items()))+ result = await session.execute(+ select(Message.room_id)+ .join(+ position,+ and_(+ Message.room_id == position.c.room_id,+ Message.seq > position.c.seq,+ ),+ )+ .distinct()+ )+ return set(result.scalars().all())+async def get_by_transport_event_id(self, session: AsyncSession, transport_event_id: str) -> Message | None:diff --git a/core/switch_core/gateway/chats.py b/core/switch_core/gateway/chats.pyindex e10f7d4f5..f0a4495cc 100644--- a/core/switch_core/gateway/chats.py+++ b/core/switch_core/gateway/chats.py@@ -48,7 +48,7 @@ from switch_core.chats.views import (ChatMessage,ChatSummary,chat_messages,- chat_summary,+ chat_summaries,)from switch_core.db.models import (AgentSessionActivityItem,@@ -247,11 +247,30 @@ CurrentUser = Annotated[User, Depends(get_current_user)]async def _summary(service: ChatService, session: AsyncSession, tenant_id: str, user: User, room: Room) -> ChatSummary:- return await chat_summary(+ [summary] = await _summaries(service, session, tenant_id, user, [room])+ return summary+++async def _summaries(+ service: ChatService,+ session: AsyncSession,+ tenant_id: str,+ user: User,+ rooms: list[Room],+) -> list[ChatSummary]:+ """The rooms' summaries, read in a fixed number of queries."""+ if not rooms:+ return []+ administers = await service.administers(session, tenant_id, user)+ return await chat_summaries(session,- room,+ rooms,viewer_id=user.id,- can_manage=await service.can_manage(session, tenant_id, user, room),+ can_manage={+ room.id: administers+ or (room.owner_id is not None and room.owner_id == user.id)+ for room in rooms+ },)@@ -275,10 +294,7 @@ async def list_chats(user: CurrentUser, service: Service) -> ChatList:return ChatList(chats=[])rooms = await service.list_chats(session, user.id)return ChatList(- chats=[- await _summary(service, session, tenant_id, user, room)- for room in rooms- ]+ chats=await _summaries(service, session, tenant_id, user, rooms))@@ -722,14 +738,9 @@ class _ChatStream:_removed(room_id, "access") for room_id in self.after if initial)return frames, False- readable = {- room.id: room- for room in await service.readable_rooms(session, self.user.id)- }- listed = {- room.id: room- for room in await service.stream_rooms(session, self.user.id)- }+ rooms = await service.readable_and_listed(session, self.user.id)+ readable = {room.id: room for room, _ in rooms}+ listed = {room.id: room for room, is_listed in rooms if is_listed}for room_id in [r for r in self.cursors if r not in readable]:frames.append(self._untrack(room_id, "access"))if initial:@@ -744,46 +755,71 @@ class _ChatStream:frames.append(_removed(room_id, "unlisted"))self.announced.discard(room_id)self.sent.pop(room_id, None)- for room_id, room in readable.items():- if room_id in self.cursors:- continue+ new = [room_id for room_id in readable if room_id not in self.cursors]+ heads = (+ await service.head_seqs(+ session, [r for r in new if r not in self.after]+ )+ if initial+ else {}+ )+ announce: list[Room] = []+ for room_id in new:+ room = readable[room_id]if initial and room_id in self.after:self.cursors[room_id] = self.after[room_id]if room_id in listed:self.announced.add(room_id)else:if room_id in listed:- frames.extend(await self._announce(session, room))- self.announced.add(room_id)+ announce.append(room)# A room joined since the stream opened is followed from# where the caller joined it, so whatever was said before# the stream noticed it is still sent.self.cursors[room_id] = (- await service.head_seq(session, room_id)+ heads[room_id]if initialelse await service.read_position(session, self.user.id, room_id))service.listener.subscribe(room_id, self.on_room)for room_id, room in listed.items():- if (+ if room not in announce and (room_id not in self.announcedor resummarizeor room_id in resummarize_rooms):- frames.extend(await self._announce(session, room))- self.announced.add(room_id)- for room_id in list(self.cursors) if read is None else read:- if room_id in readable:+ announce.append(room)+ frames.extend(await self._announce(session, announce))+ self.announced.update(room.id for room in announce)+ to_read = [+ room_id+ for room_id in (list(self.cursors) if read is None else read)+ if room_id in readable+ ]+ # One look at which rooms moved past their cursors, then reads only+ # those: a recheck of quiet rooms costs one query, and fetches none.+ moved = await service.moved_rooms(+ session, {room_id: self.cursors[room_id] for room_id in to_read}+ )+ for room_id in to_read:+ if room_id in moved:frames.extend(await self._read(session, readable[room_id]))return frames, True- async def _announce(self, session: AsyncSession, room: Room) -> list[bytes]:- summary = await _summary(self.service, session, self.tenant_id, self.user, room)- dump = summary.model_dump(by_alias=True)- if self.sent.get(room.id) == dump:- return []- self.sent[room.id] = dump- return [_frame("chat", dump)]+ async def _announce(self, session: AsyncSession, rooms: list[Room]) -> list[bytes]:+ """`chat` frames for the rooms whose summary differs from the last sent."""+ frames: list[bytes] = []+ for room, summary in zip(+ rooms,+ await _summaries(self.service, session, self.tenant_id, self.user, rooms),+ strict=True,+ ):+ dump = summary.model_dump(by_alias=True)+ if self.sent.get(room.id) == dump:+ continue+ self.sent[room.id] = dump+ frames.append(_frame("chat", dump))+ return framesasync def _read(self, session: AsyncSession, room: Room) -> list[bytes]:frames: list[bytes] = []diff --git a/core/tests/switch_core/gateway/test_chats_groups.py b/core/tests/switch_core/gateway/test_chats_groups.pyindex 6ddeddaeb..29ed41363 100644--- a/core/tests/switch_core/gateway/test_chats_groups.py+++ b/core/tests/switch_core/gateway/test_chats_groups.py@@ -1045,13 +1045,13 @@ async def test_a_change_to_one_chat_re_summarises_only_that_chat(assert await events.until("ready") == {}summaries: list[str] = []- summarise = chats_module.chat_summary+ summarise = chats_module.chat_summaries- async def counted(session: AsyncSession, room: Room, **kwargs: Any) -> Any:- summaries.append(room.id)- return await summarise(session, room, **kwargs)+ async def counted(session: AsyncSession, rooms: list[Room], **kwargs: Any) -> Any:+ summaries.extend(room.id for room in rooms)+ return await summarise(session, rooms, **kwargs)- monkeypatch.setattr(chats_module, "chat_summary", counted)+ monkeypatch.setattr(chats_module, "chat_summaries", counted)added = await _add_agent(chats, chats.alice.id, room_ids[0], agent)assert added.status_code == 200for events in streams:diff --git a/core/tests/switch_core/gateway/test_chats_query_counts.py b/core/tests/switch_core/gateway/test_chats_query_counts.pynew file mode 100644index 000000000..39d0432a7--- /dev/null+++ b/core/tests/switch_core/gateway/test_chats_query_counts.py@@ -0,0 +1,74 @@+"""Listing chats and a stream's periodic recheck cost a fixed number of+statements whatever the number of rooms, and fetch rows only for rooms, not+for their history; a recheck still finds a message whose notification was+missed. Measured at the engine (`statement_counts.py`)."""++from __future__ import annotations++from switch_core.gateway.chats import _ChatStream+from tests.switch_core.gateway.conftest import _Harness+from tests.switch_core.statement_counts import StatementCounts++TENANT = "00000000-0000-0000-0000-000000000000"+++async def _chat(chats: _Harness, request_id: str) -> str:+ created = await chats.client.post(+ "/chats",+ json={"agentIds": [], "memberUserIds": [chats.bob.id], "requestId": request_id},+ headers=chats.as_user(chats.alice),+ )+ assert created.status_code == 200, created.text+ room_id = str(created.json()["chat"]["roomId"])+ for index in range(3):+ sent = await chats.send(chats.bob, room_id, f"{request_id}-{index}", "hello")+ assert sent.status_code == 200+ return room_id+++async def test_listing_and_rechecking_do_not_grow_with_rooms(+ chats: _Harness, statement_counts: StatementCounts+) -> None:+ measured: dict[int, tuple[int, int, int, int]] = {}+ for count in range(1, 21):+ await _chat(chats, f"c{count}")+ if count not in (1, 20):+ continue+ with statement_counts.scope(f"list-{count}") as listing:+ response = await chats.client.get(+ "/chats", headers=chats.as_user(chats.alice)+ )+ assert response.status_code == 200+ stream = _ChatStream(chats.service, TENANT, chats.alice, {})+ await stream.refresh(None, initial=True)+ with statement_counts.scope(f"idle-{count}") as idle:+ frames, alive = await stream.refresh(None, initial=False)+ stream.close()+ assert alive and frames == []+ measured[count] = (listing.count, listing.rows, idle.count, idle.rows)++ one, twenty = measured[1], measured[20]+ assert twenty[0] == one[0], measured+ assert twenty[2] == one[2], measured+ # Rows: a handful per room (the room, its two people, its last message,+ # its unread count), never its history of four messages.+ assert twenty[1] <= one[1] + 6 * 19, measured+ assert twenty[3] <= one[3] + 19, measured+++async def test_a_recheck_still_finds_a_message_whose_notice_was_missed(+ chats: _Harness,+) -> None:+ quiet = await _chat(chats, "quiet")+ busy = await _chat(chats, "busy")+ stream = _ChatStream(chats.service, TENANT, chats.alice, {})+ await stream.refresh(None, initial=True)+ # Sent with no notification rung, as when one is lost.+ sent = await chats.send(chats.bob, busy, "late", "are you there?")+ assert sent.status_code == 200++ frames, _ = await stream.refresh(None, initial=False)+ stream.close()+ bodies = [frame.decode() for frame in frames]+ assert len(bodies) == 1 and "are you there?" in bodies[0]+ assert quiet not in bodies[0]diff --git a/core/tests/switch_core/statement_counts.py b/core/tests/switch_core/statement_counts.pyindex 4abc2755c..b9d3cabbe 100644--- a/core/tests/switch_core/statement_counts.py+++ b/core/tests/switch_core/statement_counts.py@@ -32,6 +32,8 @@ class StatementScope:label: strstatements: list[str] = field(default_factory=list)+ # Rows each statement returned, as the driver fetched them.+ rows: int = 0@propertydef count(self) -> int:@@ -48,9 +50,27 @@ class StatementCounts:self._engine = engine.sync_engineself.scopes: list[StatementScope] = []event.listen(self._engine, "before_cursor_execute", self._on_execute)+ event.listen(self._engine, "after_cursor_execute", self._on_executed)def close(self) -> None:event.remove(self._engine, "before_cursor_execute", self._on_execute)+ event.remove(self._engine, "after_cursor_execute", self._on_executed)++ def _on_executed(+ self,+ _conn: Any,+ cursor: Any,+ _statement: str,+ _parameters: Any,+ _context: Any,+ _executemany: bool,+ ) -> None:+ scope = _current.get()+ if scope is None:+ return+ # The asyncpg adapter holds a statement's result rows in `_rows`.+ fetched = getattr(cursor, "_rows", None)+ scope.rows += len(fetched) if fetched is not None else max(cursor.rowcount, 0)def _on_execute(self,