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
Read agents and rooms in bulk when switch-core starts up (#743)
Abel Dantas063ce7a32fba
diff --git a/core/switch_core/clients/agent_consumer.py b/core/switch_core/clients/agent_consumer.pyindex 7487f6737..0389af290 100644--- a/core/switch_core/clients/agent_consumer.py+++ b/core/switch_core/clients/agent_consumer.py@@ -343,6 +343,7 @@ class AgentConsumer(Consumer[AgentActor]):super().__init__(actor=actor)self._event_buffer = event_bufferself._agent_store = agent_store+ self._preloaded_agent: Agent | None = Noneself._room_store = room_storeself._bridge_store = bridge_storeself._document_store = document_store@@ -406,13 +407,23 @@ class AgentConsumer(Consumer[AgentActor]):fail the client at boot over a row that exists, which is why nothinghere falls back to whatever is ambient."""- async with tenant_session(self.session_factory, self.tenant_id) as session:- agent = await self._agent_store.get_by_client_id(session, self.client_id)- if agent is None:- raise RuntimeError(f"No agent found for client: {self.client_id}")- self.actor.set_agent(agent)+ agent = self._preloaded_agent+ self._preloaded_agent = None+ if agent is None:+ async with tenant_session(self.session_factory, self.tenant_id) as session:+ agent = await self._agent_store.get_by_client_id(+ session, self.client_id+ )+ if agent is None:+ raise RuntimeError(f"No agent found for client: {self.client_id}")+ self.actor.set_agent(agent)await super().start()+ def preload_agent(self, agent: Agent) -> None:+ """Hand `start` the agent row read in bulk at boot, so it needs no+ query of its own. Used once: a restart reads it fresh."""+ self._preloaded_agent = agent+# ── Event hooks ───────────────────────────────────────────────────────────async def on_self_join(self, room: RoomRef, event: InboundMembership) -> None:diff --git a/core/switch_core/clients/client_lifecycle_service.py b/core/switch_core/clients/client_lifecycle_service.pyindex d89535f07..2e7cb6685 100644--- a/core/switch_core/clients/client_lifecycle_service.py+++ b/core/switch_core/clients/client_lifecycle_service.py@@ -3,7 +3,7 @@ from __future__ import annotationsimport asyncioimport loggingimport uuid-from typing import Any+from typing import Any, Protocol, runtime_checkablefrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker@@ -11,8 +11,9 @@ from switch_core.clients.actor import Actor, AgentActor, ClientConfigfrom switch_core.clients.client_factory import ClientFactoryfrom switch_core.clients.consumer import Consumerfrom switch_core.config import SwitchConfig-from switch_core.db.models import Client, Tenant+from switch_core.db.models import Agent, Client, Tenantfrom switch_core.db.session_scope import tenant_session+from switch_core.db.stores.agent_store import AgentStorefrom switch_core.db.stores.client_store import ClientStorefrom switch_core.db.stores.tenant_store import TenantStorefrom switch_core.db.tenant_lookup import all_tenant_ids@@ -23,6 +24,13 @@ from switch_core.tenant_context import no_tenant, tenant_scopelogger = logging.getLogger(__name__)+@runtime_checkable+class _PreloadsAgent(Protocol):+ """A consumer that can start from an agent row read for it in bulk."""++ def preload_agent(self, agent: Agent) -> None: ...++class TenantIsolationNotInForce(Exception):"""A second tenant was asked for on a deployment that cannot keep tenants apart."""@@ -194,10 +202,41 @@ class ClientLifecycleService:)records = [r for r in records if r.type not in self.COLLAB_CLIENT_TYPES]+ try:+ agents = await self._agents_by_client_id(records)+ except Exception:+ # One bulk read must not cost every client. Without it each agent+ # client reads its own row as it starts, as it always did, with+ # its own retry.+ logger.exception("Bulk agent read failed; clients will each read their own")+ agents = {}logger.info("Starting %d clients", len(records))for record in records:self._register(record)+ # Before the task first runs: `_register` only schedules it, and+ # nothing here yields until the loop is done.+ consumer = self._consumers.get(record.id)+ agent = agents.get(record.id)+ if agent is not None and isinstance(consumer, _PreloadsAgent):+ consumer.preload_agent(agent)++ async def _agents_by_client_id(self, records: list[Client]) -> dict[str, Agent]:+ """The agent row behind each client, one query per tenant.++ Each agent client would otherwise read its own at start, and at boot+ they all start at once: a few hundred reads in the same second.+ """+ by_tenant: dict[str, list[str]] = {}+ for record in records:+ by_tenant.setdefault(record.tenant_id, []).append(record.id)+ agents: dict[str, Agent] = {}+ for tenant_id, client_ids in by_tenant.items():+ async with tenant_session(self._session_factory, tenant_id) as session:+ for agent in await AgentStore().get_by_client_ids(session, client_ids):+ if agent.client_id is not None:+ agents[agent.client_id] = agent+ return agentsasync def create_client(self,diff --git a/core/switch_core/db/stores/agent_store.py b/core/switch_core/db/stores/agent_store.pyindex b3eff01db..d9b647faa 100644--- a/core/switch_core/db/stores/agent_store.py+++ b/core/switch_core/db/stores/agent_store.py@@ -73,6 +73,17 @@ class AgentStore:)return result.scalar_one_or_none()+ async def get_by_client_ids(+ self, session: AsyncSession, client_ids: list[str]+ ) -> list[Agent]:+ """`get_by_client_id` for many clients in one query."""+ if not client_ids:+ return []+ result = await session.execute(+ select(Agent).where(Agent.client_id.in_(client_ids))+ )+ return list(result.scalars().all())+async def get_by_api_key_id(self, session: AsyncSession, api_key_id: str) -> Agent | None:diff --git a/core/switch_core/db/stores/message_store.py b/core/switch_core/db/stores/message_store.pyindex 90fa3c7a8..5048210ee 100644--- a/core/switch_core/db/stores/message_store.py+++ b/core/switch_core/db/stores/message_store.py@@ -144,6 +144,25 @@ class MessageStore:)return max(int(result.scalar_one()), 0)+ async def head_seqs(+ self, session: AsyncSession, room_ids: list[str]+ ) -> dict[str, int]:+ """`head_seq` for many rooms in one query, 0 for a room with nothing sent.++ For a client starting up in all its rooms at once: one round trip+ rather than one per room, which at boot is several hundred at the same+ moment.+ """+ if not room_ids:+ return {}+ result = await session.execute(+ select(Message.room_id, func.max(Message.seq))+ .where(Message.room_id.in_(room_ids))+ .group_by(Message.room_id)+ )+ 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 get_by_transport_event_id(self, session: AsyncSession, transport_event_id: str) -> Message | None:diff --git a/core/switch_core/db/stores/room_store.py b/core/switch_core/db/stores/room_store.pyindex 1c3930991..cb1e5db5d 100644--- a/core/switch_core/db/stores/room_store.py+++ b/core/switch_core/db/stores/room_store.py@@ -470,6 +470,20 @@ class RoomStore:)return list(result.scalars().all())+ async def member_room_ids(+ self, session: AsyncSession, client_id: str, room_ids: list[str]+ ) -> set[str]:+ """Which of `room_ids` the client is still a member of, in one query."""+ if not room_ids:+ return set()+ result = await session.execute(+ select(ClientRoom.room_id).where(+ ClientRoom.client_id == client_id,+ ClientRoom.room_id.in_(room_ids),+ )+ )+ return set(result.scalars().all())+async def get_client_ids(self, session: AsyncSession, room_id: str) -> list[str]:result = await session.execute(select(ClientRoom.client_id).where(ClientRoom.room_id == room_id)diff --git a/core/switch_core/transport/postgres.py b/core/switch_core/transport/postgres.pyindex 95b5c1a64..5c7111427 100644--- a/core/switch_core/transport/postgres.py+++ b/core/switch_core/transport/postgres.py@@ -277,8 +277,7 @@ class PostgresTransport:)delivery = asyncio.create_task(self._deliver_forever())try:- for transport_room_id in rooms:- await self._watch(transport_room_id)+ await self._watch_joined(rooms)await self._closed.wait()finally:delivery.cancel()@@ -452,15 +451,64 @@ class PostgresTransport:# Removed while the row and head were being read. `_unwatch` has# already undone the claim and there is nothing subscribed to undo.return- self._cursors[room_id] = seq- self._room_cache.attach(self.tenant_id, room_id, self)- self._listener.subscribe(room_id, self._on_room_advanced)- self._ephemeral.subscribe(transport_room_id, self._on_ephemeral)+ self._subscribe_room(room_id, transport_room_id, seq)if from_seq is not None:# The announcement for anything already written went out before# this subscription existed, so nothing will wake the loop for it.self._mark_pending(room_id)+ def _subscribe_room(self, room_id: str, transport_room_id: str, seq: int) -> None:+ """Deliver a claimed room from `seq` on."""+ self._cursors[room_id] = seq+ self._room_cache.attach(self.tenant_id, room_id, self)+ self._listener.subscribe(room_id, self._on_room_advanced)+ self._ephemeral.subscribe(transport_room_id, self._on_ephemeral)++ async def _watch_joined(self, transport_room_ids: list[str]) -> None:+ """`_watch` every room the client is in, from each room's head, reading+ membership and heads for all of them at once.++ At boot every client does this at the same moment, and one session+ per room per check was over a thousand sessions in a few seconds,+ enough to drain the pool. The guard is `_watch`'s, applied to the whole+ set: every room is claimed before anything is read, so a removal that+ lands on the read finds a claim to take back, and a room whose+ membership row is gone by the time it is read is released.++ A room `joined_rooms` did not resolve (it always does, but the cache is+ the only thing this relies on) goes through `_watch` on its own.+ """+ claimed: dict[str, str] = {}+ for transport_room_id in transport_room_ids:+ room_id = self._room_ids.get(transport_room_id)+ if room_id is None:+ await self._watch(transport_room_id)+ continue+ if room_id in self._watching:+ continue+ self._watching[room_id] = transport_room_id+ claimed[room_id] = transport_room_id+ if not claimed:+ return+ try:+ async with tenant_session(self._session_factory, self.tenant_id) as session:+ members = await self._room_store.member_room_ids(+ session, self.client_id, list(claimed)+ )+ heads = await self._message_store.head_seqs(session, list(claimed))+ except Exception:+ for room_id, transport_room_id in claimed.items():+ self._release_claim(room_id, transport_room_id)+ raise+ for room_id, transport_room_id in claimed.items():+ if room_id not in members:+ # Removed between the room list and here; see `_watch`.+ self._release_claim(room_id, transport_room_id)+ continue+ if self._watching.get(room_id) != transport_room_id:+ continue+ self._subscribe_room(room_id, transport_room_id, heads[room_id])+def _unwatch(self, transport_room_id: str) -> None:"""Stop delivering one room. Not watching it is success.@@ -1007,6 +1055,10 @@ class PostgresTransport:"""async with tenant_session(self._session_factory, self.tenant_id) as session:rooms = await self._room_store.get_for_client(session, self.client_id)+ # The rows carry both ids, so watching them needs no lookup per room.+ for room in rooms:+ if room.transport_room_id:+ self._room_ids[room.transport_room_id] = room.idreturn [room.transport_room_id for room in rooms if room.transport_room_id]async def set_display_name(self, display_name: str) -> None:diff --git a/core/tests/switch_core/clients/test_start_all_preloads_agents.py b/core/tests/switch_core/clients/test_start_all_preloads_agents.pynew file mode 100644index 000000000..350060fdb--- /dev/null+++ b/core/tests/switch_core/clients/test_start_all_preloads_agents.py@@ -0,0 +1,99 @@+"""`start_all` reads every agent row once per tenant and hands each consumer+its own, so no agent client queries for itself at boot."""++from __future__ import annotations++from typing import Any+from unittest.mock import MagicMock++import pytest+from sqlalchemy.exc import TimeoutError as PoolTimeoutError+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.clients.client_lifecycle_service import ClientLifecycleService+from switch_core.db.models import Agent+from switch_core.db.stores.client_store import ClientStore+from switch_core.db.stores.tenant_store import TenantStore+from tests.switch_core.test_room_wide_mention_wakes_no_agent import _make_agent+++class _Consumer:+ def __init__(self) -> None:+ self.display_name = "agent"+ self.transport_user_id = "@agent:test"+ self.preloaded: Agent | None = None++ def preload_agent(self, agent: Agent) -> None:+ self.preloaded = agent++ async def start(self) -> None:+ return None++ async def stop(self) -> None:+ return None+++class _Factory:+ def __init__(self) -> None:+ self.consumers: dict[str, _Consumer] = {}++ def create(self, record: Any) -> tuple[_Consumer, _Consumer]:+ consumer = _Consumer()+ self.consumers[record.id] = consumer+ return consumer, consumer+++async def test_each_agent_consumer_gets_its_own_row_before_it_starts(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ async with session_factory() as session:+ agents = [await _make_agent(session, f"pre-{n}") for n in range(3)]+ await session.commit()+ factory = _Factory()+ service = ClientLifecycleService(+ provisioning=MagicMock(),+ client_store=ClientStore(),+ tenant_store=TenantStore(),+ client_factory=factory, # type: ignore[arg-type]+ session_factory=session_factory,+ config=MagicMock(),+ tenants_isolated=True,+ )++ await service.start_all()++ for agent in agents:+ preloaded = factory.consumers[agent.client_id].preloaded+ assert preloaded is not None and preloaded.id == agent.id+++async def test_a_failed_bulk_read_still_starts_every_client(+ session_factory: async_sessionmaker[AsyncSession],+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The bulk read is an optimisation: when it fails, each client starts+ without a preload and reads its own row, instead of none starting."""+ async with session_factory() as session:+ agents = [await _make_agent(session, f"nopre-{n}") for n in range(2)]+ await session.commit()+ factory = _Factory()+ service = ClientLifecycleService(+ provisioning=MagicMock(),+ client_store=ClientStore(),+ tenant_store=TenantStore(),+ client_factory=factory, # type: ignore[arg-type]+ session_factory=session_factory,+ config=MagicMock(),+ tenants_isolated=True,+ )++ async def _fail(_records: Any) -> dict[str, Agent]:+ raise PoolTimeoutError("QueuePool limit reached")++ monkeypatch.setattr(service, "_agents_by_client_id", _fail)++ await service.start_all()++ for agent in agents:+ assert agent.client_id in factory.consumers+ assert factory.consumers[agent.client_id].preloaded is Nonediff --git a/core/tests/switch_core/transport/test_start_statements.py b/core/tests/switch_core/transport/test_start_statements.pynew file mode 100644index 000000000..26a4ed6c1--- /dev/null+++ b/core/tests/switch_core/transport/test_start_statements.py@@ -0,0 +1,130 @@+"""A client starting up reads its rooms in a fixed number of statements.++At boot every client starts in all its rooms at once. Reading each room's id,+membership and head one at a time was over a thousand short sessions in a few+seconds on pilot, enough to drain the pool. The count must not grow with the+number of rooms.+"""++from __future__ import annotations++import asyncio++from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.db.models import ClientRoom, Room+from tests.switch_core.statement_counts import StatementCounts+from tests.switch_core.transport.test_postgres_transport import (+ _make_client,+ _transport,+)+++async def _client_in_rooms(+ session_factory: async_sessionmaker[AsyncSession], rooms: int+) -> tuple[str, str, list[str]]:+ async with session_factory() as session:+ client_id, user_id = await _make_client(session, f"in-{rooms}")+ room_ids = []+ for n in range(rooms):+ room = Room(+ transport_room_id=f"!r{rooms}-{n}-{client_id[:8]}:test",+ name="r",+ description="",+ )+ session.add(room)+ await session.flush()+ session.add(ClientRoom(client_id=client_id, room_id=room.id))+ room_ids.append(room.id)+ await session.commit()+ return client_id, user_id, room_ids+++async def _start(+ session_factory: async_sessionmaker[AsyncSession],+ counts: StatementCounts,+ client_id: str,+ user_id: str,+ rooms: int,+) -> tuple[int, set[str]]:+ transport = _transport(session_factory, client_id=client_id, user_id=user_id)+ with counts.scope(f"start in {rooms}") as scope:+ task = asyncio.create_task(transport.receive_forever())+ for _ in range(300):+ if len(transport._cursors) == rooms:+ break+ await asyncio.sleep(0.01)+ watched = set(transport._cursors)+ await transport.close()+ task.cancel()+ return scope.count, watched+++async def test_starting_in_six_rooms_runs_as_many_statements_as_in_one(+ session_factory: async_sessionmaker[AsyncSession],+ statement_counts: StatementCounts,+) -> None:+ one = await _client_in_rooms(session_factory, 1)+ six = await _client_in_rooms(session_factory, 6)++ count_one, watched_one = await _start(+ session_factory, statement_counts, *one[:2], 1+ )+ count_six, watched_six = await _start(+ session_factory, statement_counts, *six[:2], 6+ )++ assert watched_one == set(one[2])+ assert watched_six == set(six[2])+ assert count_six == count_one+++async def test_a_room_left_before_its_membership_is_read_is_not_watched(+ session_factory: async_sessionmaker[AsyncSession],+ statement_counts: StatementCounts,+) -> None:+ """`_watch`'s guard, for the batch: the room list said member, the read+ after the claim says not, so the claim is released."""+ client_id, user_id, room_ids = await _client_in_rooms(session_factory, 3)+ gone = room_ids[0]+ transport = _transport(session_factory, client_id=client_id, user_id=user_id)+ real = transport._room_store.member_room_ids++ async def _left_one(session, client, ids): # type: ignore[no-untyped-def]+ return (await real(session, client, ids)) - {gone}++ transport._room_store.member_room_ids = _left_one # type: ignore[method-assign]+ task = asyncio.create_task(transport.receive_forever())+ for _ in range(300):+ if len(transport._cursors) == 2:+ break+ await asyncio.sleep(0.01)++ assert set(transport._cursors) == set(room_ids[1:])+ assert gone not in transport._watching+ await transport.close()+ task.cancel()+++async def test_a_failed_batch_read_releases_every_claim(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ """If the membership and head read fails, no room is left claimed with+ nothing subscribed, so a retry starts from a clean slate."""+ client_id, user_id, _ = await _client_in_rooms(session_factory, 3)+ transport = _transport(session_factory, client_id=client_id, user_id=user_id)++ async def _fail(*_args: object) -> set[str]:+ raise OSError("connection lost")++ transport._room_store.member_room_ids = _fail # type: ignore[method-assign]+ rooms = await transport.joined_rooms()+ try:+ await transport._watch_joined(rooms)+ except OSError:+ pass+ else:+ raise AssertionError("the read error should propagate to the caller")++ assert transport._watching == {}+ assert transport._cursors == {}