Skip to content

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

Share room lookups between a process's clients (CHOO-3667)

Abel Dantas01fe7f31f92f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8b9791612..78c3f22e6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -224,6 +224,11 @@ version of their own to them without also giving them a release of their own.
group whose subgroup would move up next to a namesake is refused too, and
the dashboard now shows why. Names can no longer contain `/` or be blank.
Existing duplicates are left as they are and can be reached by id.
+- **Writing to a room for the first time through a process no longer reads the
+ room.** A process's clients now share the room ids they look up, where each
+ kept its own, and the first one a process misses reads every open room of
+ the workspace at once, so with many agents behind a load balancer most sends
+ no longer start with a read of the room.
#### Fixed
- **On Slack, the πŸ‘€ and the activity card no longer arrive after the agent's
diff --git a/core/switch_core/clients/client_factory.py b/core/switch_core/clients/client_factory.py
index 924e7d0e8..5acb6c7af 100644
--- a/core/switch_core/clients/client_factory.py
+++ b/core/switch_core/clients/client_factory.py
@@ -19,7 +19,7 @@ from switch_core.transport import MessageTransport
from switch_core.transport.ephemeral import EphemeralBus
from switch_core.transport.invites import InviteBus
from switch_core.transport.observer import ParticipantMessageObserver
-from switch_core.transport.postgres import PostgresTransport
+from switch_core.transport.postgres import PostgresTransport, RoomIds
from switch_core.transport.room_cache import RoomDeliveryCache
if TYPE_CHECKING:
@@ -62,6 +62,8 @@ class ClientFactory:
# Every transport writes through it, and it posts agents' replies
# through transports built here, so it is handed how to build one.
self._router = router
+ # Shared by every transport this factory builds (`RoomIds`).
+ self._room_ids = RoomIds()
router.use_transports(self.transport_for)
self._registry: dict[
str,
@@ -157,4 +159,5 @@ class ClientFactory:
room_cache=self._room_cache,
message_observer=self._message_observer,
router=self._router,
+ room_ids=self._room_ids,
)
diff --git a/core/switch_core/db/stores/room_store.py b/core/switch_core/db/stores/room_store.py
index e91ce9b15..10c6f6ee6 100644
--- a/core/switch_core/db/stores/room_store.py
+++ b/core/switch_core/db/stores/room_store.py
@@ -173,6 +173,18 @@ class RoomStore:
)
return result.scalar_one_or_none()
+ async def transport_ids_of_open_rooms(
+ self, session: AsyncSession, tenant_id: str
+ ) -> list[tuple[str, str]]:
+ """The transport room id and the room id of every room of the tenant
+ that is not archived, in one read (`transport.postgres.RoomIds`)."""
+ result = await session.execute(
+ select(Room.transport_room_id, Room.id).where(
+ Room.tenant_id == tenant_id, Room.archived_at.is_(None)
+ )
+ )
+ return [(transport_room_id, room_id) for transport_room_id, room_id in result]
+
async def run_rooms(self, session: AsyncSession, root_id: str) -> list[Room]:
"""Every room of the run rooted at ``root_id``, the root first, then in
the order they were created. Archived rooms are included: a run is a
diff --git a/core/switch_core/transport/postgres.py b/core/switch_core/transport/postgres.py
index 60ee6936f..7af36523e 100644
--- a/core/switch_core/transport/postgres.py
+++ b/core/switch_core/transport/postgres.py
@@ -43,6 +43,7 @@ from __future__ import annotations
import asyncio
import logging
import uuid
+from collections.abc import Awaitable, Callable
from datetime import UTC, datetime
from typing import TYPE_CHECKING, Any, cast
@@ -174,6 +175,77 @@ MEMBERSHIP_EVENT_TYPE = "m.room.member"
STOP_GRACE_SECONDS = 5.0
+class RoomIds:
+ """The room id behind each transport room id a process's transports
+ have resolved, by tenant, shared between them.
+
+ Every client looks up a room before its first write there, and with
+ each client keeping its own answers, a deployment of thousands of agents
+ behind a load balancer read a room for nearly every message: an agent
+ writes to a room rarely, and seldom twice through the same process. A
+ room's transport id never changes, so this only grows and never goes
+ stale. Keyed by tenant as well, since `rooms.matrix_room_id` is unique per
+ tenant, not globally.
+
+ The first time a transport of a tenant misses, every open room of the
+ tenant is read at once (`TenantRoomIds.read_all_once`), so a process does
+ not read thousands of rooms one at a time as its clients first write to
+ them. A room created after that is read on its own, once.
+ """
+
+ def __init__(self) -> None:
+ self._ids: dict[tuple[str, str], str] = {}
+ # The tenants whose open rooms were read in full, or are being read,
+ # set once the read has ended.
+ self._read: dict[str, asyncio.Event] = {}
+
+ def of_tenant(self, tenant_id: str) -> TenantRoomIds:
+ return TenantRoomIds(self._ids, self._read, tenant_id)
+
+
+class TenantRoomIds:
+ """`RoomIds` as the transports of one tenant read and write it."""
+
+ __slots__ = ("_ids", "_read", "_tenant_id")
+
+ def __init__(
+ self,
+ ids: dict[tuple[str, str], str],
+ read: dict[str, asyncio.Event],
+ tenant_id: str,
+ ) -> None:
+ self._ids = ids
+ self._read = read
+ self._tenant_id = tenant_id
+
+ def get(self, transport_room_id: str) -> str | None:
+ return self._ids.get((self._tenant_id, transport_room_id))
+
+ def __setitem__(self, transport_room_id: str, room_id: str) -> None:
+ self._ids[(self._tenant_id, transport_room_id)] = room_id
+
+ async def read_all_once(
+ self, read: Callable[[], Awaitable[list[tuple[str, str]]]]
+ ) -> None:
+ """Read every open room of the tenant with `read`, the first time
+ this is asked in the process. A caller asking while that read is under
+ way waits for it, holding nothing; one asking after it returns at
+ once. A read that fails is tried again by the next caller."""
+ done = self._read.get(self._tenant_id)
+ if done is not None:
+ await done.wait()
+ return
+ done = self._read[self._tenant_id] = asyncio.Event()
+ try:
+ for transport_room_id, room_id in await read():
+ self._ids[(self._tenant_id, transport_room_id)] = room_id
+ except BaseException:
+ del self._read[self._tenant_id]
+ raise
+ finally:
+ done.set()
+
+
class PostgresTransport:
"""Carries a room's events in the `messages` table.
@@ -200,6 +272,7 @@ class PostgresTransport:
room_cache: RoomDeliveryCache,
message_observer: ParticipantMessageObserver,
router: MessageRouter,
+ room_ids: RoomIds | None = None,
) -> None:
self.user_id = user_id
self.client_id = client_id
@@ -287,8 +360,11 @@ class PostgresTransport:
self._wake = asyncio.Event()
self._delivering = False
# A room's transport id never changes, so this only grows and never
- # goes stale.
- self._room_ids: dict[str, str] = {}
+ # goes stale. Shared with every other transport of the tenant in the
+ # process, so a room one client found costs the next client no read.
+ self._room_ids = (room_ids if room_ids is not None else RoomIds()).of_tenant(
+ tenant_id
+ )
# ── Session ───────────────────────────────────────────────────────────────
@@ -1415,12 +1491,22 @@ class PostgresTransport:
"""
tenant_id = self.tenant_id
cached = self._room_ids.get(transport_room_id)
+ if cached is not None:
+ return cached, tenant_id
+ await self._room_ids.read_all_once(self._open_rooms)
+ cached = self._room_ids.get(transport_room_id)
if cached is not None:
return cached, tenant_id
async with tenant_session(self._session_factory, tenant_id) as session:
room_id = await self._resolve_room(session, transport_room_id)
return room_id, tenant_id
+ async def _open_rooms(self) -> list[tuple[str, str]]:
+ async with tenant_session(self._session_factory, self.tenant_id) as session:
+ return await self._room_store.transport_ids_of_open_rooms(
+ session, self.tenant_id
+ )
+
def _now_ms() -> int:
return int(datetime.now(UTC).timestamp() * 1000)
diff --git a/core/tests/switch_core/delivery/test_router_sessions.py b/core/tests/switch_core/delivery/test_router_sessions.py
index a277fed2d..634844db0 100644
--- a/core/tests/switch_core/delivery/test_router_sessions.py
+++ b/core/tests/switch_core/delivery/test_router_sessions.py
@@ -207,3 +207,62 @@ class TestWritingAMessage:
assert (scope.taken, scope.peak) == (1, 1)
assert len(await own_events(session_factory, agents["a3"][0].id)) == 1
+
+
+class TestFindingTheRoom:
+ async def test_a_room_one_client_found_costs_the_next_no_read(
+ self,
+ session_factory: async_sessionmaker[AsyncSession],
+ pool_checkouts: PoolCheckouts,
+ ) -> None:
+ """Every client looks a room up before its first write there. The
+ answers are shared by the process's clients, so a room another client
+ already wrote to costs the write alone (`RoomIds`)."""
+ process, room, _, _ = await _seed(session_factory, names=["member"])
+ rita = await make_person(session_factory, "rita")
+ await add_member(session_factory, room, rita)
+
+ with pool_checkouts.scope("say") as scope:
+ await process.say(room, "first time here", sender=rita)
+
+ assert (scope.taken, scope.peak) == (1, 1)
+
+ async def test_the_first_room_found_finds_every_open_room_of_the_tenant(
+ self,
+ session_factory: async_sessionmaker[AsyncSession],
+ pool_checkouts: PoolCheckouts,
+ ) -> None:
+ """The first miss in a process reads every open room of the tenant,
+ so a room nobody in the process has written to yet costs no read of
+ its own either; a room archived by then is left to be read on its own."""
+ process = process_on(session_factory)
+ louisa = await make_person(session_factory, "louisa")
+ rooms = [await make_room(session_factory, f"room-{n}") for n in range(3)]
+ for room in rooms:
+ await add_member(session_factory, room, louisa)
+ await process.say(rooms[0], "hello", sender=louisa)
+ rita = await make_person(session_factory, "rita")
+ await add_member(session_factory, rooms[2], rita)
+
+ with pool_checkouts.scope("say") as scope:
+ await process.say(rooms[2], "first time anyone writes here", sender=rita)
+
+ assert (scope.taken, scope.peak) == (1, 1)
+
+ async def test_a_room_created_after_the_first_read_is_read_on_its_own(
+ self,
+ session_factory: async_sessionmaker[AsyncSession],
+ pool_checkouts: PoolCheckouts,
+ ) -> None:
+ process = process_on(session_factory)
+ louisa = await make_person(session_factory, "louisa")
+ first = await make_room(session_factory, "first")
+ await add_member(session_factory, first, louisa)
+ await process.say(first, "hello", sender=louisa)
+ later = await make_room(session_factory, "later")
+ await add_member(session_factory, later, louisa)
+
+ with pool_checkouts.scope("say") as scope:
+ await process.say(later, "a room made since", sender=louisa)
+
+ assert (scope.taken, scope.peak) == (2, 1)
diff --git a/core/tests/switch_core/transport/test_room_ids.py b/core/tests/switch_core/transport/test_room_ids.py
new file mode 100644
index 000000000..3ad0c46ad
--- /dev/null
+++ b/core/tests/switch_core/transport/test_room_ids.py
@@ -0,0 +1,61 @@
+"""The room ids a process's transports share: every open room of a tenant
+read at once, by whichever transport misses first, and only once."""
+
+from __future__ import annotations
+
+import asyncio
+
+import pytest
+
+from switch_core.transport.postgres import RoomIds
+
+
+async def test_transports_missing_together_read_the_rooms_once() -> None:
+ ids = RoomIds()
+ reads = 0
+ release = asyncio.Event()
+
+ async def read() -> list[tuple[str, str]]:
+ nonlocal reads
+ reads += 1
+ await release.wait()
+ return [("!a:test", "room-a")]
+
+ first = asyncio.ensure_future(ids.of_tenant("t1").read_all_once(read))
+ second = asyncio.ensure_future(ids.of_tenant("t1").read_all_once(read))
+ await asyncio.sleep(0)
+ release.set()
+ await asyncio.gather(first, second)
+ await ids.of_tenant("t1").read_all_once(read)
+
+ assert reads == 1
+ assert ids.of_tenant("t1").get("!a:test") == "room-a"
+
+
+async def test_each_tenant_has_its_own_rooms() -> None:
+ ids = RoomIds()
+
+ async def read() -> list[tuple[str, str]]:
+ return [("!a:test", "room-a")]
+
+ await ids.of_tenant("t1").read_all_once(read)
+
+ assert ids.of_tenant("t2").get("!a:test") is None
+
+
+async def test_a_read_that_failed_is_tried_again() -> None:
+ ids = RoomIds()
+ failed = False
+
+ async def read() -> list[tuple[str, str]]:
+ nonlocal failed
+ if not failed:
+ failed = True
+ raise ConnectionError("the pool timed out")
+ return [("!a:test", "room-a")]
+
+ with pytest.raises(ConnectionError):
+ await ids.of_tenant("t1").read_all_once(read)
+ await ids.of_tenant("t1").read_all_once(read)
+
+ assert ids.of_tenant("t1").get("!a:test") == "room-a"