Skip to content

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

perf(chats): a change to one chat re-summarises only that chat

raullopez-sandbox5cd2c43bb780
diff --git a/core/switch_core/chats/service.py b/core/switch_core/chats/service.py
index 0a9b64cf9..06e39a359 100644
--- a/core/switch_core/chats/service.py
+++ b/core/switch_core/chats/service.py
@@ -109,6 +109,9 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
+# Told the rooms whose part in a person's chats changed, or None for any.
+MembershipWatcher = Callable[[list[str] | None], None]
+
# Fixed so the ids derived from it are stable across processes and releases.
_CHATS_NAMESPACE = uuid.UUID("6f1d3c2e-8a4b-4f5e-9c7d-2b1a0e9f8d7c")
@@ -193,7 +196,7 @@ class ChatService:
self._locks: weakref.WeakValueDictionary[tuple[str, ...], asyncio.Lock] = (
weakref.WeakValueDictionary()
)
- self._membership_watchers: dict[tuple[str, str], set[Callable[[], None]]] = {}
+ self._membership_watchers: dict[tuple[str, str], set[MembershipWatcher]] = {}
# Where a change to people's chats made here is announced to the
# other processes, once wired (`use_signals`).
self._signals: ChatSignals | None = None
@@ -215,14 +218,15 @@ class ChatService:
self._signals = signals
def watch_memberships(
- self, tenant_id: str, user_id: str, callback: Callable[[], None]
+ self, tenant_id: str, user_id: str, callback: MembershipWatcher
) -> None:
"""Call `callback` whenever the user's chats change: at once when this
- process changed them, and once told when another process did."""
+ process changed them, and once told when another process did. It is
+ given the rooms that changed, or None when any may have."""
self._membership_watchers.setdefault((tenant_id, user_id), set()).add(callback)
def unwatch_memberships(
- self, tenant_id: str, user_id: str, callback: Callable[[], None]
+ self, tenant_id: str, user_id: str, callback: MembershipWatcher
) -> None:
watchers = self._membership_watchers.get((tenant_id, user_id))
if watchers is None:
@@ -231,27 +235,35 @@ class ChatService:
if not watchers:
del self._membership_watchers[(tenant_id, user_id)]
- def wake_membership_watchers(self, tenant_id: str, user_ids: list[str]) -> None:
- """Call every callback watching these users' chats in this process."""
+ def wake_membership_watchers(
+ self, tenant_id: str, user_ids: list[str], room_ids: list[str] | None
+ ) -> None:
+ """Call every callback watching these users' chats in this process,
+ with the rooms that changed (None: any)."""
for user_id in user_ids:
for callback in list(
self._membership_watchers.get((tenant_id, user_id), ())
):
- callback()
+ callback(room_ids)
def wake_all_membership_watchers(self) -> None:
- """Call every callback watching anyone's chats in this process, once."""
+ """Call every callback watching anyone's chats in this process, once,
+ with any room in question."""
for watchers in list(self._membership_watchers.values()):
for callback in list(watchers):
- callback()
+ callback(None)
- def _memberships_changed(self, tenant_id: str, user_ids: list[str]) -> None:
- """The users' chats changed through this process, and the change has
+ def _memberships_changed(
+ self, tenant_id: str, user_ids: list[str], room_ids: list[str]
+ ) -> None:
+ """The users' part in chats `room_ids` changed through this process
+ (joined, left, or what their summaries show), and the change has
committed: the streams here are woken at once, and the other
- processes are told to wake theirs."""
- self.wake_membership_watchers(tenant_id, user_ids)
+ processes are told to wake theirs. A stream re-reads which chats its
+ person has on every wake, and re-summarises only these."""
+ self.wake_membership_watchers(tenant_id, user_ids, room_ids)
if self._signals is not None:
- self._signals.memberships_changed(tenant_id, user_ids)
+ self._signals.memberships_changed(tenant_id, user_ids, room_ids)
def _lock(self, *key: str) -> asyncio.Lock:
lock = self._locks.get(key)
@@ -480,7 +492,9 @@ class ChatService:
len(to_join),
len(to_drop),
)
- self._memberships_changed(tenant_id, [user_id])
+ self._memberships_changed(
+ tenant_id, [user_id], [room.id for room in [*to_join, *to_drop]]
+ )
async def sync_room(self, tenant_id: str, room_id: str) -> None:
"""`sync_owned_rooms` for everyone a change to this room's agents may
@@ -530,7 +544,9 @@ class ChatService:
):
await self.sync_owned_rooms(tenant_id, user_id)
self._memberships_changed(
- tenant_id, sorted(user_id for user_id in members if user_id is not None)
+ tenant_id,
+ sorted(user_id for user_id in members if user_id is not None),
+ [room_id],
)
async def agent_changed(self, tenant_id: str, agent_id: str) -> None:
@@ -553,7 +569,16 @@ class ChatService:
)
).scalars()
user_ids = sorted(user_id for user_id in members if user_id is not None)
- self._memberships_changed(tenant_id, user_ids)
+ room_ids = list(
+ (
+ await session.execute(
+ select(room_agents.c.room_id).where(
+ room_agents.c.agent_id == agent_id
+ )
+ )
+ ).scalars()
+ )
+ self._memberships_changed(tenant_id, user_ids, room_ids)
async def _make_lasting(self, tenant_id: str, user_id: str, room_id: str) -> None:
async with tenant_session(self.session_factory, tenant_id) as session:
@@ -849,7 +874,7 @@ class ChatService:
await self._make_lasting(tenant_id, target_user_id, room.id)
async with tenant_session(self.session_factory, tenant_id) as session:
member_ids = [member.id for member, _ in await self.members(session, room)]
- self._memberships_changed(tenant_id, member_ids)
+ self._memberships_changed(tenant_id, member_ids, [room.id])
return room
async def remove_member(
@@ -890,7 +915,7 @@ class ChatService:
await self._make_lasting(tenant_id, target_user_id, room.id)
async with tenant_session(self.session_factory, tenant_id) as session:
member_ids = [member.id for member, _ in await self.members(session, room)]
- self._memberships_changed(tenant_id, [target_user_id, *member_ids])
+ self._memberships_changed(tenant_id, [target_user_id, *member_ids], [room.id])
async def add_agent(
self, tenant_id: str, actor: User, room_id: str, agent_id: str
@@ -957,7 +982,7 @@ class ChatService:
await self._require_manager(session, tenant_id, user, room)
member_ids = [member.id for member, _ in await self.members(session, room)]
await self._room_service.set_room_archived(room.id, True)
- self._memberships_changed(tenant_id, member_ids)
+ self._memberships_changed(tenant_id, member_ids, [room.id])
async def _start_reading(
self, session: AsyncSession, room_id: str, user_ids: list[str]
@@ -1012,7 +1037,7 @@ class ChatService:
)
await self._read_through(session, room.id, [user.id], seq)
await session.commit()
- self._memberships_changed(tenant_id, [user.id])
+ self._memberships_changed(tenant_id, [user.id], [room.id])
async def set_hidden(
self, tenant_id: str, user: User, room_id: str, hidden: bool
@@ -1313,7 +1338,7 @@ class ChatService:
.values(state="done", room_id=room.id, result={"roomId": room.id})
)
await session.commit()
- self._memberships_changed(tenant_id, [user.id, *member_user_ids])
+ self._memberships_changed(tenant_id, [user.id, *member_user_ids], [room.id])
if len(agents) == 1:
self._start_session(agents[0].id, room.id)
return room
diff --git a/core/switch_core/chats/signals.py b/core/switch_core/chats/signals.py
index 2f5aa9ee4..59b28f1b7 100644
--- a/core/switch_core/chats/signals.py
+++ b/core/switch_core/chats/signals.py
@@ -21,6 +21,7 @@ costs that stream one re-read.
from __future__ import annotations
+import json
from typing import TYPE_CHECKING, Any
from switch_core.signals import payload_chunks
@@ -47,17 +48,27 @@ class ChatSignals:
# (`gateway/chats.py`), which is all a missed signal can cost it.
service.use_signals(self)
- def memberships_changed(self, tenant_id: str, user_ids: list[str]) -> None:
- """The users' chats 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):
+ def memberships_changed(
+ self, tenant_id: str, user_ids: list[str], room_ids: list[str]
+ ) -> None:
+ """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
+ MEMBERSHIPS_CHANGED_SIGNAL,
+ tenant_id=tenant_id,
+ user_ids=chunk,
+ room_ids=room_ids,
)
def _on_changed(self, signal: dict[str, Any]) -> None:
- self._service.wake_membership_watchers(signal["tenant_id"], signal["user_ids"])
+ # A process on the release before room ids names none: any room.
+ self._service.wake_membership_watchers(
+ signal["tenant_id"], signal["user_ids"], signal.get("room_ids")
+ )
async def _wake_all(self) -> None:
self._service.wake_all_membership_watchers()
diff --git a/core/switch_core/gateway/chats.py b/core/switch_core/gateway/chats.py
index 313611f9a..57142da6f 100644
--- a/core/switch_core/gateway/chats.py
+++ b/core/switch_core/gateway/chats.py
@@ -620,6 +620,7 @@ class _ChatStream:
self.announced: set[str] = set()
self.sent: dict[str, dict[str, Any]] = {}
self.resummarize = False
+ self.resummarize_rooms: set[str] = set()
self.dirty: set[str] = set()
self.wake = asyncio.Event()
@@ -627,8 +628,11 @@ class _ChatStream:
self.dirty.add(room_id)
self.wake.set()
- def on_memberships(self) -> None:
- self.resummarize = True
+ def on_memberships(self, room_ids: list[str] | None) -> None:
+ if room_ids is None:
+ self.resummarize = True
+ else:
+ self.resummarize_rooms.update(room_ids)
self.wake.set()
def close(self) -> None:
@@ -653,6 +657,7 @@ class _ChatStream:
frames: list[bytes] = []
service = self.service
resummarize, self.resummarize = self.resummarize, False
+ resummarize_rooms, self.resummarize_rooms = self.resummarize_rooms, set()
if read is None:
await service.sync_owned_rooms(self.tenant_id, self.user.id)
async with tenant_session(service.session_factory, self.tenant_id) as session:
@@ -707,7 +712,11 @@ class _ChatStream:
)
service.listener.subscribe(room_id, self.on_room)
for room_id, room in listed.items():
- if room_id not in self.announced or resummarize:
+ if (
+ room_id not in self.announced
+ or resummarize
+ or 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:
diff --git a/core/tests/switch_core/gateway/test_chats_groups.py b/core/tests/switch_core/gateway/test_chats_groups.py
index cae64f6b5..b2bd8735e 100644
--- a/core/tests/switch_core/gateway/test_chats_groups.py
+++ b/core/tests/switch_core/gateway/test_chats_groups.py
@@ -19,6 +19,7 @@ from switch_core.bridges.agent.registration_bootstrap import (
)
from switch_core.chats.service import ChatService
from switch_core.db.models import Agent, Client, Message, Room, User, room_agents
+from switch_core.gateway import chats as chats_module
from tests.switch_core.gateway.conftest import (
_chat_service,
_FakeActivityListener,
@@ -1013,3 +1014,43 @@ async def test_a_reconnect_cursor_still_decides_where_a_chat_resumes(
name, data = await events.next()
assert (name, data["body"]) == ("message", "new")
await events.stream.aclose() # type: ignore[attr-defined]
+
+
+async def test_a_change_to_one_chat_re_summarises_only_that_chat(
+ chats: _Harness, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ agent = await chats.agent("helper", owner=chats.alice)
+ room_ids = []
+ for index in range(20):
+ created = await chats.client.post(
+ "/chats",
+ json={
+ "agentIds": [],
+ "memberUserIds": [chats.bob.id, chats.admin.id],
+ "requestId": f"c{index}",
+ },
+ headers=chats.as_user(chats.alice),
+ )
+ room_ids.append(created.json()["chat"]["roomId"])
+ streams = [
+ _stream(chats, user, {}, recheck=60.0) for user in (chats.bob, chats.admin)
+ ]
+ for events in streams:
+ assert await events.until("ready") == {}
+
+ summaries: list[str] = []
+ summarise = chats_module.chat_summary
+
+ async def counted(session: AsyncSession, room: Room, **kwargs: Any) -> Any:
+ summaries.append(room.id)
+ return await summarise(session, room, **kwargs)
+
+ monkeypatch.setattr(chats_module, "chat_summary", counted)
+ added = await _add_agent(chats, chats.alice.id, room_ids[0], agent)
+ assert added.status_code == 200
+ for events in streams:
+ name, summary = await events.next()
+ assert (name, summary["roomId"]) == ("chat", room_ids[0])
+ await events.stream.aclose() # type: ignore[attr-defined]
+ # The add answers with one summary; each of the two streams sends one.
+ assert summaries == [room_ids[0]] * 3
diff --git a/core/tests/switch_core/gateway/test_chats_routes.py b/core/tests/switch_core/gateway/test_chats_routes.py
index e8ad909d1..d4225a40a 100644
--- a/core/tests/switch_core/gateway/test_chats_routes.py
+++ b/core/tests/switch_core/gateway/test_chats_routes.py
@@ -1161,10 +1161,10 @@ async def test_a_change_concerning_more_people_than_one_signal_holds_reaches_the
woken: list[str] = []
for user_id in user_ids:
there.service.watch_memberships(
- TENANT, user_id, lambda user_id=user_id: woken.append(user_id)
+ TENANT, user_id, lambda _rooms, user_id=user_id: woken.append(user_id)
)
- here.signals.memberships_changed(TENANT, user_ids)
+ here.signals.memberships_changed(TENANT, user_ids, [])
await _eventually(lambda: len(woken) == len(user_ids))
assert sorted(woken) == sorted(user_ids)