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
Batch the room and agent list queries so they don't grow with the data (CHOO-3323) (#709)
Abel Dantas1219141e8aa8
diff --git a/core/switch_core/bridges/agent/protocol/agent_detail.py b/core/switch_core/bridges/agent/protocol/agent_detail.pyindex 6e10b784b..3f6e327f3 100644--- a/core/switch_core/bridges/agent/protocol/agent_detail.py+++ b/core/switch_core/bridges/agent/protocol/agent_detail.py@@ -25,7 +25,7 @@ from switch_core.addressing import (from switch_core.agent_display_name import normalise_display_namefrom switch_core.agent_icon import normalise_icon_urlfrom switch_core.bridges.agent.protocol.agent_connections import AgentConnectionRegistry-from switch_core.bridges.agent.protocol.statuses import compute_agent_statuses+from switch_core.bridges.agent.protocol.statuses import agent_statuses_by_roomfrom switch_core.db.models import Agent, AgentSessionfrom switch_core.db.stores.agent_session_store import AgentSessionStorefrom switch_core.db.stores.agent_store import AgentStore@@ -54,8 +54,39 @@ async def build_agent_summary(agent: Agent,owner_name: str | None,) -> AgentSummary:- tools = await agent_store.get_tools(session, agent.id)- models = await agent_store.get_models(session, agent.id)+ """One agent's summary. For a list, `build_agent_summaries`."""+ tools = await agent_store.tool_counts(session, [agent.id])+ models = await agent_store.model_counts(session, [agent.id])+ return agent_summary(+ agent, owner_name, tools.get(agent.id, 0), models.get(agent.id, 0)+ )+++async def build_agent_summaries(+ session: AsyncSession,+ agent_store: AgentStore,+ agents: list[Agent],+ owner_names: dict[str, str],+) -> list[AgentSummary]:+ """Summaries for `agents`, in their order, with every agent's tools and+ models counted in one query each. `owner_names` is keyed by owner id."""+ agent_ids = [agent.id for agent in agents]+ tools = await agent_store.tool_counts(session, agent_ids)+ models = await agent_store.model_counts(session, agent_ids)+ return [+ agent_summary(+ agent,+ owner_names.get(agent.owner_id) if agent.owner_id else None,+ tools.get(agent.id, 0),+ models.get(agent.id, 0),+ )+ for agent in agents+ ]+++def agent_summary(+ agent: Agent, owner_name: str | None, tool_count: int, model_count: int+) -> AgentSummary:# `metadata_` is typed as dict | None but the JSONB column can hold any# JSON value, and some pre-existing rows stored a list. Treat anything# that isn't a dict as "no known-agent metadata."@@ -73,8 +104,8 @@ async def build_agent_summary(display_name=agent.display_name,connector_type=agent.connector_type,connection_model=profile.get("connection_model"),- tool_count=len(tools),- model_count=len(models),+ tool_count=tool_count,+ model_count=model_count,owner_id=agent.owner_id,owner_name=owner_name,oauth_client_id=agent.oauth_client_id,@@ -110,16 +141,12 @@ async def list_agent_summaries(`known_agent_type` are exact matches. Results are sorted by agent name."""agents = await agent_store.get_all(session)-- owner_ids = {a.owner_id for a in agents if a.owner_id}- owner_names: dict[str, str] = {}- for oid in owner_ids:- owner = await user_store.get(session, oid)- if owner:- owner_names[oid] = owner.name+ owner_names = await user_store.names_by_id(+ session, {a.owner_id for a in agents if a.owner_id}+ )needle = name_contains.lower() if name_contains else None- summaries: list[AgentSummary] = []+ selected: list[Agent] = []for agent in agents:if needle is not None and needle not in agent.name.lower():continue@@ -130,10 +157,9 @@ async def list_agent_summaries(agent_known_type = md.get("known_agent_type")if known_agent_type is not None and agent_known_type != known_agent_type:continue- summaries.append(- await build_agent_summary(session, agent_store, agent, agent_owner_name)- )+ selected.append(agent)+ summaries = await build_agent_summaries(session, agent_store, selected, owner_names)summaries.sort(key=lambda s: s.name)return summaries@@ -165,61 +191,67 @@ async def assemble_agent_detail(room_role_store: RoomRoleStore,connections: AgentConnectionRegistry,) -> AgentDetail:- owner_names: dict[str, str] = {}-- async def owner_name(owner_id: str | None) -> str | None:- if not owner_id:- return None- if owner_id not in owner_names:- owner = await user_store.get(session, owner_id)- if owner:- owner_names[owner_id] = owner.name- return owner_names.get(owner_id)-- summary = await build_agent_summary(- session, agent_store, agent, await owner_name(agent.owner_id)- )tools = await agent_store.get_tools(session, agent.id)models = await agent_store.get_models(session, agent.id)+ child_agents = await agent_store.get_children(session, [agent.id])+ owner_names = await user_store.names_by_id(+ session, {a.owner_id for a in [agent, *child_agents] if a.owner_id}+ )+ summary = agent_summary(+ agent,+ owner_names.get(agent.owner_id) if agent.owner_id else None,+ len(tools),+ len(models),+ )rooms = await room_store.get_rooms_for_agent(session, agent.id, include_archived=True)room_name_by_id = {room.id: room.name for room in rooms}- memberships: list[AgentRoomMembership] = []+ room_ids = [room.id for room in rooms]live_connection_ids = connections.live_connection_ids()- for room in rooms:- statuses = await compute_agent_statuses(- session, [agent], room.id, agent_session_store, connections- )- room_role = await room_role_store.agent_room_role(- session, room.id, agent.id, live_connection_ids- )- memberships.append(- AgentRoomMembership(- room_id=room.id,- room_name=room.name,- archived=room.archived_at is not None,- status=statuses[agent.id].value,- room_role=room_role,- )+ statuses = await agent_statuses_by_room(+ session, [agent], room_ids, agent_session_store, connections+ )+ room_roles = await room_role_store.agent_room_roles(+ session, agent.id, room_ids, live_connection_ids+ )+ memberships = [+ AgentRoomMembership(+ room_id=room.id,+ room_name=room.name,+ archived=room.archived_at is not None,+ status=statuses[room.id][agent.id].value,+ room_role=room_roles.get(room.id),)+ for room in rooms+ ]++ session_rows = await agent_session_store.get_sessions_for_agent(session, agent.id)+ agent_connections = connections.for_agent(agent.id)+ # Sessions and connections can name rooms the agent is no longer a member+ # of; those names are read together, once.+ other_room_ids = {+ room_id+ for room_id in [+ *(row.room_id for row in session_rows),+ *(room for conn in agent_connections for room in conn.rooms),+ ]+ if room_id is not None and room_id not in room_name_by_id+ }+ for other in (await room_store.get_many(session, other_room_ids)).values():+ room_name_by_id[other.id] = other.namenow = datetime.now(UTC)sessions: list[AgentSessionDetail] = []- session_rows = await agent_session_store.get_sessions_for_agent(session, agent.id)for row in session_rows:- room_name: str | None = None- if row.room_id is not None:- room_name = room_name_by_id.get(row.room_id)- if room_name is None:- linked_room = await room_store.get(session, row.room_id)- room_name = linked_room.name if linked_room else Nonesessions.append(AgentSessionDetail(room_id=row.room_id,- room_name=room_name,+ room_name=room_name_by_id.get(row.room_id)+ if row.room_id is not None+ else None,lifecycle=row.lifecycle,state=session_state(row, now),last_seen_at=str(row.last_seen_at),@@ -233,7 +265,7 @@ async def assemble_agent_detail(# than either being wrong alone. Rooms already covered by a row are skipped# so a client running both (a migration in progress) is not listed twice.listed_rooms = {row.room_id for row in session_rows}- for conn in connections.for_agent(agent.id):+ for conn in agent_connections:# An `all`-scope connection claiming nothing is one room-agnostic# session, not none.conn_rooms: list[str | None] = list(sorted(conn.rooms)) or [None]@@ -241,16 +273,12 @@ async def assemble_agent_detail(if conn_room in listed_rooms:continuelisted_rooms.add(conn_room)- room_name = None- if conn_room is not None:- room_name = room_name_by_id.get(conn_room)- if room_name is None:- linked_room = await room_store.get(session, conn_room)- room_name = linked_room.name if linked_room else Nonesessions.append(AgentSessionDetail(room_id=conn_room,- room_name=room_name,+ room_name=room_name_by_id.get(conn_room)+ if conn_room is not None+ else None,lifecycle="connection",state="live",last_seen_at=str(now),@@ -274,13 +302,9 @@ async def assemble_agent_detail())- child_agents = await agent_store.get_children(session, [agent.id])- children = [- await build_agent_summary(- session, agent_store, child, await owner_name(child.owner_id)- )- for child in child_agents- ]+ children = await build_agent_summaries(+ session, agent_store, child_agents, owner_names+ )return AgentDetail(**summary.model_dump(),diff --git a/core/switch_core/bridges/agent/protocol/statuses.py b/core/switch_core/bridges/agent/protocol/statuses.pyindex 98cc3634d..e3099023a 100644--- a/core/switch_core/bridges/agent/protocol/statuses.py+++ b/core/switch_core/bridges/agent/protocol/statuses.py@@ -56,9 +56,31 @@ async def compute_agent_statuses((`controller_statuses`), and none of the three sources above is askedabout it."""+ return (+ await agent_statuses_by_room(+ session, agents, [room_id], agent_session_store, connections+ )+ )[room_id]+++async def agent_statuses_by_room(+ session: AsyncSession,+ agents: list[Agent],+ room_ids: list[str],+ agent_session_store: AgentSessionStore,+ connections: AgentConnectionRegistry,+) -> dict[str, dict[str, AgentStatus]]:+ """`compute_agent_statuses` for each of `room_ids`, keyed by room id.++ The heartbeat rows are read once for all the rooms: one query for the+ room-agnostic rows and one for the room-scoped ones, however many rooms+ and agents there are. Everything else `compute_agent_statuses` describes+ is in memory and asked room by room, so each room's answer is exactly the+ one `compute_agent_statuses` gives for it alone.+ """controllers = connections.controllersbacked = [agent for agent in agents if controllers.is_bound(agent.id)]- statuses = controller_statuses(backed, connections)+ backed_statuses = controller_statuses(backed, connections)agents = [agent for agent in agents if not controllers.is_bound(agent.id)]always_on_ids: list[str] = []@@ -77,89 +99,98 @@ async def compute_agent_statuses(elif connection_model == "auto_session":auto_session_ids.append(agent.id)- live_always_on = await agent_session_store.get_live_agent_ids(- session, always_on_ids, None- )- # auto_session agents are LIVE when something covers this room, and DORMANT- # when only the room-agnostic "watching" signal is present.- live_auto_room = await agent_session_store.get_live_agent_ids(- session, auto_session_ids, room_id+ # Each agent has one connection model, so one read per kind of row covers+ # every model: room-agnostic rows say always_on is up and auto_session is+ # watching; room-scoped rows say auto_session and session_addressable are+ # in that room.+ room_agnostic = await agent_session_store.get_live_agent_ids(+ session, always_on_ids + auto_session_ids, None)- watching_auto = await agent_session_store.get_live_agent_ids(- session, auto_session_ids, None+ in_room = await agent_session_store.live_agent_ids_by_room(+ session, auto_session_ids + addressable_ids, room_ids)- live_addressable = await agent_session_store.get_live_agent_ids(- session, addressable_ids, room_id- )-- # always_on has no separate notion of a session: any live connection is the- # agent being up, and its scope is room-agnostic.- live_always_on |= connections.live_agents(always_on_ids)- # For the session-shaped models, LIVE means a session is *present* in the- # room, which is two different facts about two kinds of client. A session- # Switch holds a record of says so by the room it is bound to and the lease- # its host is still renewing; a client Switch has no record of leaves only- # the room slot its connection claimed.- #- # Neither is coverage. An `all`-scope watcher covers rooms it has not- # yielded, which is the delivery rule, not presence: treating it as LIVE- # would report an agent as present in a room where nothing but a watcher is- # listening, suppressing both the "no session" reply and the auto_session- # promise to start one.- live_auto_room |= agents_present_in(auto_session_ids, room_id, connections)- live_addressable |= agents_present_in(addressable_ids, room_id, connections)- # …whereas DORMANT is a promise that a session is coming, so what it asks- # of a connection is willingness, not mere connectivity. The client says so- # when it opens the stream, and one connection of an agent says nothing- # about another: a session worker is connected and will never spawn, and a- # controller with auto-start off is connected and has declined to. Reading- # either as watching promises "Starting a session…" over a room nothing- # will ever join. The heartbeat rows keep their own arm: a client still on- # /watch/heartbeat declares nothing, and that loop meant willingness.- watching_auto |= connections.agents_that_can_spawn_for(auto_session_ids, room_id)- # An agent whose client is connected but will start nothing is not- # disconnected. It is reachable and has no session here, which is what- # NO_SESSION says and what the room needs to hear: DISCONNECTED over a live- # controller with automatic starts off tells a user the agent is away, and- # the reply they get if they address it anyway contradicts that.- connected_auto = connections.live_agents(auto_session_ids)- # A spawn-capable connection covering the room will start a session on- # demand, whatever the agent was configured as. DORMANT rather than- # NO_SESSION is the honest report: nothing is attending yet, but something- # is watching and will be.- spawn_ready = {- aid- for aid in addressable_ids- if aid not in live_addressable and connections.can_spawn_for(aid, room_id)- }- for agent in agents:- model = model_by_id[agent.id]- if model == "always_on":- statuses[agent.id] = (- AgentStatus.LIVE- if agent.id in live_always_on- else AgentStatus.DISCONNECTED- )- elif model == "session_addressable":- if agent.id in live_addressable:- statuses[agent.id] = AgentStatus.LIVE- elif agent.id in spawn_ready:- statuses[agent.id] = AgentStatus.DORMANT- else:- statuses[agent.id] = AgentStatus.NO_SESSION- elif model == "auto_session":- if agent.id in live_auto_room:- statuses[agent.id] = AgentStatus.LIVE- elif agent.id in watching_auto:- statuses[agent.id] = AgentStatus.DORMANT- elif agent.id in connected_auto:- statuses[agent.id] = AgentStatus.NO_SESSION+ by_room: dict[str, dict[str, AgentStatus]] = {}+ for room_id in room_ids:+ statuses = dict(backed_statuses)+ live_here = in_room.get(room_id, set())+ live_always_on = room_agnostic & set(always_on_ids)+ # auto_session agents are LIVE when something covers this room, and+ # DORMANT when only the room-agnostic "watching" signal is present.+ live_auto_room = live_here & set(auto_session_ids)+ watching_auto = room_agnostic & set(auto_session_ids)+ live_addressable = live_here & set(addressable_ids)+ # always_on has no separate notion of a session: any live connection is the+ # agent being up, and its scope is room-agnostic.+ live_always_on |= connections.live_agents(always_on_ids)+ # For the session-shaped models, LIVE means a session is *present* in the+ # room, which is two different facts about two kinds of client. A session+ # Switch holds a record of says so by the room it is bound to and the lease+ # its host is still renewing; a client Switch has no record of leaves only+ # the room slot its connection claimed.+ #+ # Neither is coverage. An `all`-scope watcher covers rooms it has not+ # yielded, which is the delivery rule, not presence: treating it as LIVE+ # would report an agent as present in a room where nothing but a watcher is+ # listening, suppressing both the "no session" reply and the auto_session+ # promise to start one.+ live_auto_room |= agents_present_in(auto_session_ids, room_id, connections)+ live_addressable |= agents_present_in(addressable_ids, room_id, connections)+ # …whereas DORMANT is a promise that a session is coming, so what it asks+ # of a connection is willingness, not mere connectivity. The client says so+ # when it opens the stream, and one connection of an agent says nothing+ # about another: a session worker is connected and will never spawn, and a+ # controller with auto-start off is connected and has declined to. Reading+ # either as watching promises "Starting a session…" over a room nothing+ # will ever join. The heartbeat rows keep their own arm: a client still on+ # /watch/heartbeat declares nothing, and that loop meant willingness.+ watching_auto |= connections.agents_that_can_spawn_for(+ auto_session_ids, room_id+ )+ # An agent whose client is connected but will start nothing is not+ # disconnected. It is reachable and has no session here, which is what+ # NO_SESSION says and what the room needs to hear: DISCONNECTED over a live+ # controller with automatic starts off tells a user the agent is away, and+ # the reply they get if they address it anyway contradicts that.+ connected_auto = connections.live_agents(auto_session_ids)+ # A spawn-capable connection covering the room will start a session on+ # demand, whatever the agent was configured as. DORMANT rather than+ # NO_SESSION is the honest report: nothing is attending yet, but something+ # is watching and will be.+ spawn_ready = {+ aid+ for aid in addressable_ids+ if aid not in live_addressable and connections.can_spawn_for(aid, room_id)+ }++ for agent in agents:+ model = model_by_id[agent.id]+ if model == "always_on":+ statuses[agent.id] = (+ AgentStatus.LIVE+ if agent.id in live_always_on+ else AgentStatus.DISCONNECTED+ )+ elif model == "session_addressable":+ if agent.id in live_addressable:+ statuses[agent.id] = AgentStatus.LIVE+ elif agent.id in spawn_ready:+ statuses[agent.id] = AgentStatus.DORMANT+ else:+ statuses[agent.id] = AgentStatus.NO_SESSION+ elif model == "auto_session":+ if agent.id in live_auto_room:+ statuses[agent.id] = AgentStatus.LIVE+ elif agent.id in watching_auto:+ statuses[agent.id] = AgentStatus.DORMANT+ elif agent.id in connected_auto:+ statuses[agent.id] = AgentStatus.NO_SESSION+ else:+ statuses[agent.id] = AgentStatus.DISCONNECTEDelse:- statuses[agent.id] = AgentStatus.DISCONNECTED- else:- statuses[agent.id] = AgentStatus.AWAITING_MANUAL_POLL- return statuses+ statuses[agent.id] = AgentStatus.AWAITING_MANUAL_POLL+ by_room[room_id] = statuses+ return by_roomdef controller_statuses(diff --git a/core/switch_core/db/stores/agent_session_store.py b/core/switch_core/db/stores/agent_session_store.pyindex 439823889..ace35dca8 100644--- a/core/switch_core/db/stores/agent_session_store.py+++ b/core/switch_core/db/stores/agent_session_store.py@@ -1,3 +1,4 @@+from collections.abc import Collectionfrom datetime import UTC, datetime, timedeltafrom sqlalchemy import func, literal_column, select, update@@ -237,3 +238,30 @@ class AgentSessionStore:.where(AgentSession.last_seen_at > cutoff))return set(result.scalars().all())++ async def live_agent_ids_by_room(+ self,+ session: AsyncSession,+ agent_ids: Collection[str],+ room_ids: Collection[str],+ ) -> dict[str, set[str]]:+ """`get_live_agent_ids` for many concrete rooms in one query.++ Keyed by room id; a room where none of `agent_ids` is live has no key.+ Room-scoped rows only, under `SESSION_TTL`, exactly as+ `get_live_agent_ids` reads one room.+ """+ if not agent_ids or not room_ids:+ return {}+ cutoff = datetime.now(UTC) - self.SESSION_TTL+ result = await session.execute(+ select(AgentSession.room_id, AgentSession.agent_id)+ .where(AgentSession.agent_id.in_(agent_ids))+ .where(AgentSession.room_id.in_(room_ids))+ .where(AgentSession.lifecycle == "heartbeat")+ .where(AgentSession.last_seen_at > cutoff)+ )+ by_room: dict[str, set[str]] = {}+ for room_id, agent_id in result.all():+ by_room.setdefault(room_id, set()).add(agent_id)+ return by_roomdiff --git a/core/switch_core/db/stores/agent_store.py b/core/switch_core/db/stores/agent_store.pyindex 6daf9d98e..b3eff01db 100644--- a/core/switch_core/db/stores/agent_store.py+++ b/core/switch_core/db/stores/agent_store.py@@ -1,3 +1,5 @@+from collections.abc import Collection+from sqlalchemy import delete, func, or_, select, text, updatefrom sqlalchemy.ext.asyncio import AsyncSession@@ -183,6 +185,20 @@ class AgentStore:result = await session.execute(select(Tool).where(Tool.agent_id == agent_id))return list(result.scalars().all())+ async def tool_counts(+ self, session: AsyncSession, agent_ids: Collection[str]+ ) -> dict[str, int]:+ """How many tools each agent has, in one query. An agent with none+ has no key."""+ if not agent_ids:+ return {}+ result = await session.execute(+ select(Tool.agent_id, func.count())+ .where(Tool.agent_id.in_(agent_ids))+ .group_by(Tool.agent_id)+ )+ return {agent_id: count for agent_id, count in result.all()}+async def get_tool(self, session: AsyncSession, tool_id: str) -> Tool | None:return await session.get(Tool, tool_id)@@ -202,6 +218,20 @@ class AgentStore:result = await session.execute(select(Model).where(Model.agent_id == agent_id))return list(result.scalars().all())+ async def model_counts(+ self, session: AsyncSession, agent_ids: Collection[str]+ ) -> dict[str, int]:+ """How many models each agent has, in one query. An agent with none+ has no key."""+ if not agent_ids:+ return {}+ result = await session.execute(+ select(Model.agent_id, func.count())+ .where(Model.agent_id.in_(agent_ids))+ .group_by(Model.agent_id)+ )+ return {agent_id: count for agent_id, count in result.all()}+async def get_model(self, session: AsyncSession, model_id: str) -> Model | None:return await session.get(Model, model_id)diff --git a/core/switch_core/db/stores/collaboration_bridge_store.py b/core/switch_core/db/stores/collaboration_bridge_store.pyindex 134bdc67d..90dd8593c 100644--- a/core/switch_core/db/stores/collaboration_bridge_store.py+++ b/core/switch_core/db/stores/collaboration_bridge_store.py@@ -1,3 +1,5 @@+from collections.abc import Collection+from sqlalchemy import selectfrom sqlalchemy.ext.asyncio import AsyncSession@@ -17,6 +19,17 @@ class CollaborationBridgeStore:) -> CollaborationBridge | None:return await session.get(CollaborationBridge, bridge_id)+ async def get_many(+ self, session: AsyncSession, bridge_ids: Collection[str]+ ) -> list[CollaborationBridge]:+ """The bridges among `bridge_ids` that exist, in one query."""+ if not bridge_ids:+ return []+ result = await session.execute(+ select(CollaborationBridge).where(CollaborationBridge.id.in_(bridge_ids))+ )+ return list(result.scalars().all())+async def get_all(self, session: AsyncSession) -> list[CollaborationBridge]:result = await session.execute(select(CollaborationBridge))return list(result.scalars().all())diff --git a/core/switch_core/db/stores/external_user_store.py b/core/switch_core/db/stores/external_user_store.pyindex 92046cc04..8c5767ed4 100644--- a/core/switch_core/db/stores/external_user_store.py+++ b/core/switch_core/db/stores/external_user_store.py@@ -1,3 +1,4 @@+from collections.abc import Collectionfrom typing import Any, castfrom sqlalchemy import CursorResult, delete, select@@ -48,6 +49,17 @@ class ExternalUserStore:)return list(result.scalars().all())+ async def get_by_bridges(+ self, session: AsyncSession, bridge_ids: Collection[str]+ ) -> list[ExternalUser]:+ """`get_by_bridge` for several bridges in one query."""+ if not bridge_ids:+ return []+ result = await session.execute(+ select(ExternalUser).where(ExternalUser.bridge_id.in_(bridge_ids))+ )+ return list(result.scalars().all())+async def get_by_bridge_and_names(self, session: AsyncSession, bridge_id: str, usernames: list[str]) -> list[ExternalUser]:diff --git a/core/switch_core/db/stores/room_role_store.py b/core/switch_core/db/stores/room_role_store.pyindex bc55b004a..c7e13e5b7 100644--- a/core/switch_core/db/stores/room_role_store.py+++ b/core/switch_core/db/stores/room_role_store.py@@ -278,6 +278,27 @@ class RoomRoleStore:)return result.scalar_one_or_none()+ async def agent_room_roles(+ self,+ session: AsyncSession,+ agent_id: str,+ room_ids: Collection[str],+ live_connection_ids: Collection[str],+ ) -> dict[str, str]:+ """`agent_room_role` for many rooms in one query: the role name the+ agent live-holds in each, keyed by room id. A room where it holds+ none has no key."""+ if not room_ids:+ return {}+ result = await session.execute(+ select(RoleLease.room_id, RoomRole.name)+ .join(RoleLease, RoleLease.role_id == RoomRole.id)+ .where(RoleLease.room_id.in_(room_ids))+ .where(RoleLease.agent_id == agent_id)+ .where(self._live(live_connection_ids))+ )+ return {room_id: name for room_id, name in result.all()}+async def acquire_lease(self,session: AsyncSession,diff --git a/core/switch_core/db/stores/room_store.py b/core/switch_core/db/stores/room_store.pyindex af0096ea6..1c3930991 100644--- a/core/switch_core/db/stores/room_store.py+++ b/core/switch_core/db/stores/room_store.py@@ -1,3 +1,4 @@+from collections.abc import Collectionfrom datetime import UTC, datetimefrom sqlalchemy import delete, insert, or_, select, update@@ -333,6 +334,36 @@ class RoomStore:)return list(result.scalars().all())+ async def agent_ids_by_room(+ self, session: AsyncSession, room_ids: Collection[str]+ ) -> dict[str, list[str]]:+ """`get_agent_ids` for many rooms in one query, keyed by room id.++ A room with no agents has no key. For a list of rooms, which would+ otherwise read each room's members on a connection it holds for every+ round trip.+ """+ if not room_ids:+ return {}+ result = await session.execute(+ select(room_agents.c.room_id, room_agents.c.agent_id).where(+ room_agents.c.room_id.in_(room_ids)+ )+ )+ by_room: dict[str, list[str]] = {}+ for room_id, agent_id in result.all():+ by_room.setdefault(room_id, []).append(agent_id)+ return by_room++ async def get_many(+ self, session: AsyncSession, room_ids: Collection[str]+ ) -> dict[str, Room]:+ """The rooms among `room_ids` that exist, keyed by id, in one query."""+ if not room_ids:+ return {}+ result = await session.execute(select(Room).where(Room.id.in_(room_ids)))+ return {room.id: room for room in result.scalars().all()}+async def get_alias(self, session: AsyncSession, room_id: str, agent_id: str) -> str | None:@@ -445,6 +476,23 @@ class RoomStore:)return list(result.scalars().all())+ async def client_ids_by_room(+ self, session: AsyncSession, room_ids: Collection[str]+ ) -> dict[str, list[str]]:+ """`get_client_ids` for many rooms in one query, keyed by room id. A+ room with no client members has no key."""+ if not room_ids:+ return {}+ result = await session.execute(+ select(ClientRoom.room_id, ClientRoom.client_id).where(+ ClientRoom.room_id.in_(room_ids)+ )+ )+ by_room: dict[str, list[str]] = {}+ for room_id, client_id in result.all():+ by_room.setdefault(room_id, []).append(client_id)+ return by_room+async def get_member_agent_clients(self, session: AsyncSession, room_id: str) -> dict[str, str]:diff --git a/core/switch_core/db/stores/user_store.py b/core/switch_core/db/stores/user_store.pyindex 8cbfed74a..cb799157a 100644--- a/core/switch_core/db/stores/user_store.py+++ b/core/switch_core/db/stores/user_store.py@@ -1,6 +1,7 @@from __future__ import annotationsimport logging+from collections.abc import Collectionfrom datetime import timedeltafrom sqlalchemy import case, func, select@@ -118,6 +119,23 @@ class UserStore:async def get(self, session: AsyncSession, user_id: str) -> User | None:return await session.get(User, user_id)+ async def names_by_id(+ self, session: AsyncSession, user_ids: Collection[str]+ ) -> dict[str, str]:+ """Each existing user's name, keyed by id, in one query.++ For the lists that show an owner beside every row: one read for all+ the owners rather than one per owner. `users` carries no tenant, so+ what scopes this is the ids, which the caller took from rows its own+ tenant can see.+ """+ if not user_ids:+ return {}+ result = await session.execute(+ select(User.id, User.name).where(User.id.in_(user_ids))+ )+ return {user_id: name for user_id, name in result.all()}+async def exists(self, session: AsyncSession, user_id: str) -> bool:"""Whether this id names a real account, without loading the row.diff --git a/core/switch_core/gateway/rooms.py b/core/switch_core/gateway/rooms.pyindex 773cbf240..0016058e7 100644--- a/core/switch_core/gateway/rooms.py+++ b/core/switch_core/gateway/rooms.py@@ -290,23 +290,11 @@ async def list_rooms(group_names = {g.id: g.name for g in await room_group_store.get_all(session)}bridge_ids = {r.bridge_id for r in rooms if r.bridge_id}- bridge_names: dict[str, str] = {}- bridge_types: dict[str, str] = {}- ext_client_to_name: dict[str, str] = {}- for bid in bridge_ids:- bridge = await bridge_store.get(session, bid)- if bridge:- bridge_names[bid] = bridge.display_name- bridge_types[bid] = bridge.type- ext_users = await external_user_store.get_by_bridge(session, bid)- for eu in ext_users:- ext_client_to_name[eu.client_id] = eu.external_username-- owner_names: dict[str, str] = {}- for oid in {r.owner_id for r in rooms if r.owner_id}:- owner = await user_store.get(session, oid)- if owner:- owner_names[oid] = owner.name+ bridges = {b.id: b for b in await bridge_store.get_many(session, bridge_ids)}+ ext_client_to_name = {+ eu.client_id: eu.external_username+ for eu in await external_user_store.get_by_bridges(session, bridge_ids)+ }if search:term = search.lower()@@ -314,16 +302,24 @@ async def list_rooms(r for r in rooms if term in r.name.lower() or term in r.description.lower()]+ owner_names = await user_store.names_by_id(+ session, {r.owner_id for r in rooms if r.owner_id}+ )+ room_ids = [room.id for room in rooms]+ agent_ids_by_room = await room_store.agent_ids_by_room(session, room_ids)+ client_ids_by_room = await room_store.client_ids_by_room(session, room_ids)+ # Not held across the deeplinks, which can ask the platform on a cache miss.+ await session.commit()+summaries = []for room in rooms:- agent_ids = await room_store.get_agent_ids(session, room.id)- client_ids = await room_store.get_client_ids(session, room.id)connected_names = sorted(- ext_client_to_name[cid] for cid in client_ids if cid in ext_client_to_name- )- bridge_display_name = (- bridge_names.get(room.bridge_id) if room.bridge_id else None+ ext_client_to_name[cid]+ for cid in client_ids_by_room.get(room.id, [])+ if cid in ext_client_to_name)+ bridge = bridges.get(room.bridge_id) if room.bridge_id else None+ bridge_display_name = bridge.display_name if bridge else Nonename = room.nameif bridge_display_name and name.startswith(f"{bridge_display_name}: "):name = name[len(bridge_display_name) + 2 :]@@ -334,14 +330,12 @@ async def list_rooms(description=room.description,channel_type=room.channel_type,admin_mode=room.admin_mode,- agent_count=len(agent_ids),+ agent_count=len(agent_ids_by_room.get(room.id, [])),connected_user_count=len(connected_names),connected_user_names=connected_names,bridge_id=room.bridge_id,bridge_display_name=bridge_display_name,- bridge_type=(- bridge_types.get(room.bridge_id) if room.bridge_id else None- ),+ bridge_type=bridge.type if bridge else None,external_channel_url=await _external_channel_url(room),group_id=room.group_id,group_name=group_names.get(room.group_id) if room.group_id else None,diff --git a/core/tests/conftest.py b/core/tests/conftest.pyindex 70b5692d4..54a67d624 100644--- a/core/tests/conftest.py+++ b/core/tests/conftest.py@@ -30,6 +30,7 @@ from switch_core.db.runtime_role import grant_runtime_rolefrom switch_core.keys import Keyringfrom switch_core.tenant_context import tenant_scopefrom tests.switch_core.pool_checkouts import PoolCheckouts+from tests.switch_core.statement_counts import StatementCounts# Production configures this in `main.run()`; every test that touches a# connection config needs the same, and none needs a particular key.@@ -192,6 +193,20 @@ def pool_checkouts(tracker.close()+def statement_counts(+ session_factory: async_sessionmaker[AsyncSession],+) -> Iterator[StatementCounts]:+ """SQL statements run against `session_factory`'s engine, counted per+ request. Wrap an app under test with `statement_counts.wrap(app)`; see+ `statement_counts.py`."""+ counts = StatementCounts(session_factory.kw["bind"])+ try:+ yield counts+ finally:+ counts.close()++@dataclass(frozen=True)class RLSHarness:"""Two ways into the same database, for row-level-security tests thatdiff --git a/core/tests/switch_core/bridges/agent/protocol/test_controller_presence.py b/core/tests/switch_core/bridges/agent/protocol/test_controller_presence.pyindex e78956640..58e91387b 100644--- a/core/tests/switch_core/bridges/agent/protocol/test_controller_presence.py+++ b/core/tests/switch_core/bridges/agent/protocol/test_controller_presence.py@@ -73,6 +73,12 @@ class _NoRows:self.asked.append(list(agent_ids))return set()+ async def live_agent_ids_by_room(+ self, _session: Any, agent_ids: list[str], _room_ids: list[str]+ ) -> dict[str, set[str]]:+ self.asked.append(list(agent_ids))+ return {}+def _bind(registry: AgentConnectionRegistry,diff --git a/core/tests/switch_core/bridges/agent/protocol/test_runtime_state_sweep.py b/core/tests/switch_core/bridges/agent/protocol/test_runtime_state_sweep.pyindex fb89251a1..844f60f9d 100644--- a/core/tests/switch_core/bridges/agent/protocol/test_runtime_state_sweep.py+++ b/core/tests/switch_core/bridges/agent/protocol/test_runtime_state_sweep.py@@ -57,6 +57,11 @@ class _SessionStore:) -> set[str]:return set()+ async def live_agent_ids_by_room(+ self, _session: Any, _agent_ids: list[str], _room_ids: list[str]+ ) -> dict[str, set[str]]:+ return {}+def _service(session_factory: async_sessionmaker[AsyncSession],diff --git a/core/tests/switch_core/bridges/agent/protocol/test_statuses.py b/core/tests/switch_core/bridges/agent/protocol/test_statuses.pyindex e495022d7..df26690b6 100644--- a/core/tests/switch_core/bridges/agent/protocol/test_statuses.py+++ b/core/tests/switch_core/bridges/agent/protocol/test_statuses.py@@ -43,6 +43,15 @@ class _FakeSessionStore:self.calls.append((list(agent_ids), room_id))return {aid for aid in agent_ids if aid in self._live}+ async def live_agent_ids_by_room(+ self, _session: Any, agent_ids: list[str], room_ids: list[str]+ ) -> dict[str, set[str]]:+ by_room: dict[str, set[str]] = {}+ for room_id in room_ids:+ self.calls.append((list(agent_ids), room_id))+ by_room[room_id] = {aid for aid in agent_ids if aid in self._live}+ return by_room+def _registry(*,@@ -272,6 +281,11 @@ class TestPresenceIsAUnion:) -> set[str]:return set(agent_ids) if room_id is None else set()+ async def live_agent_ids_by_room(+ self, _session: Any, agent_ids: list[str], room_ids: list[str]+ ) -> dict[str, set[str]]:+ return {}+agents = [_agent("auto", "auto_session")]statuses = await compute_agent_statuses(diff --git a/core/tests/switch_core/gateway/test_query_counts.py b/core/tests/switch_core/gateway/test_query_counts.pynew file mode 100644index 000000000..f62bf0d84--- /dev/null+++ b/core/tests/switch_core/gateway/test_query_counts.py@@ -0,0 +1,428 @@+"""The list and detail routes Console reads run a fixed number of statements.++`GET /rooms`, `GET /agents` and `GET /agents/{id}` used to read one row at a+time per room, per agent, per owner and per bridge, all on one connection. The+time a request held it grew with the data, and under load every await between+those reads kept it held, which is how a pilot with a few hundred agents ran+its pool dry. Each route here is driven over a small dataset and again over a+larger one, through the real routes and real Postgres, and has to run exactly+as many statements both times.++The statements are counted at the engine (`tests/switch_core/statement_counts.py`)+and connections at the pool (`tests/switch_core/pool_checkouts.py`), so the+second contract holds too: the room list builds its channel deeplinks, which can+ask the messaging platform, holding no connection.+"""++from __future__ import annotations++import secrets+from collections.abc import AsyncIterator, Callable+from dataclasses import dataclass, field+from datetime import UTC, datetime+from types import SimpleNamespace+from typing import Any++import httpx+import pytest+from fastapi import FastAPI+from sqlalchemy import insert+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.bridges.agent.protocol.agent_connections import AgentConnectionRegistry+from switch_core.db.models import (+ TENANT_ZERO_ID,+ Agent,+ AgentSession,+ ApiKey,+ Client,+ ClientRoom,+ CollaborationBridge,+ ExternalUser,+ Model,+ Room,+ RoomRole,+ TenantMember,+ Tool,+ User,+ room_agents,+)+from switch_core.db.stores.agent_session_store import AgentSessionStore+from switch_core.db.stores.agent_store import AgentStore+from switch_core.db.stores.collaboration_bridge_store import CollaborationBridgeStore+from switch_core.db.stores.external_user_store import ExternalUserStore+from switch_core.db.stores.room_group_store import RoomGroupStore+from switch_core.db.stores.room_role_store import RoomRoleStore+from switch_core.db.stores.room_store import RoomStore+from switch_core.db.stores.user_store import UserStore+from switch_core.gateway import dependencies as gw_deps+from switch_core.gateway import rooms as rooms_module+from switch_core.gateway.agents import router as agents_router+from switch_core.gateway.auth import create_jwt+from switch_core.gateway.rooms import router as rooms_router+from switch_core.keys import Keyring+from tests.switch_core.pool_checkouts import PoolCheckouts+from tests.switch_core.statement_counts import StatementCounts++_KEYRING = Keyring.parse("test:" + secrets.token_hex(32), legacy_secret=None)+++@dataclass+class _Adapter:+ """A messaging platform's adapter, recording the connections held when asked."""++ tracker: PoolCheckouts+ held: list[int] = field(default_factory=list)++ async def channel_deeplink(self, external_channel_id: str) -> str | None:+ self.held.append(self.tracker.held_now())+ return f"platform://channel/{external_channel_id}"+++@dataclass+class _CollabLifecycle:+ adapter: _Adapter++ def get_adapter(self, bridge_id: str) -> _Adapter:+ return self.adapter+++def _protocol() -> SimpleNamespace:+ """The parts of `AgentCore` the agent detail reads: real stores and a real,+ empty connection registry, so presence is answered from the database."""+ return SimpleNamespace(+ agent_store=AgentStore(),+ agent_session_store=AgentSessionStore(),+ room_role_store=RoomRoleStore(),+ connections=AgentConnectionRegistry(),+ )+++def _app(+ session_factory: async_sessionmaker[AsyncSession],+ counts: StatementCounts,+ tracker: PoolCheckouts,+) -> Any:+ async def _session() -> AsyncIterator[AsyncSession]:+ async with session_factory() as session:+ yield session++ app = FastAPI()+ app.include_router(rooms_router, prefix="/rooms")+ app.include_router(agents_router, prefix="/agents")+ protocol = _protocol()+ overrides: dict[Callable[..., Any], Callable[..., Any]] = {+ gw_deps.get_session: _session,+ gw_deps.get_session_factory: lambda: session_factory,+ gw_deps.get_user_store: UserStore,+ gw_deps.get_room_store: RoomStore,+ gw_deps.get_room_group_store: RoomGroupStore,+ gw_deps.get_bridge_store: CollaborationBridgeStore,+ gw_deps.get_external_user_store: ExternalUserStore,+ gw_deps.get_agent_store: AgentStore,+ gw_deps.get_protocol: lambda: protocol,+ gw_deps.get_config: lambda: SimpleNamespace(+ keyring=_KEYRING, gateway_tenant_choice_enabled=False+ ),+ }+ app.dependency_overrides.update(overrides)+ return tracker.wrap(counts.wrap(app))+++def _client(app: Any, user: User) -> httpx.AsyncClient:+ token = create_jwt(user.id, user.email, user.role, _KEYRING, TENANT_ZERO_ID)+ return httpx.AsyncClient(+ transport=httpx.ASGITransport(app=app),+ base_url="http://test",+ cookies={"switch_auth": token},+ )+++class _Seed:+ """Rows for the routes to read, added in batches so one test can measure+ the same route over a small dataset and then a larger one."""++ def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:+ self._session_factory = session_factory+ self._n = 0+ self.bridges: list[CollaborationBridge] = []+ self.subject: Agent | None = None++ def _next(self, prefix: str) -> str:+ self._n += 1+ return f"{prefix}-{self._n}"++ async def admin(self) -> User:+ async with self._session_factory() as session:+ user = User(name="owner", email="[email protected]", role="user")+ session.add(user)+ await session.flush()+ session.add(+ TenantMember(tenant_id=TENANT_ZERO_ID, user_id=user.id, role="owner")+ )+ await session.commit()+ return user++ async def _user(self, session: AsyncSession) -> User:+ name = self._next("user")+ user = User(name=name, email=f"{name}@example.com", role="user")+ session.add(user)+ await session.flush()+ return user++ async def _client(self, session: AsyncSession, kind: str) -> Client:+ name = self._next(kind)+ client = Client(transport_user_id=f"@{name}:test", display_name=name, type=kind)+ session.add(client)+ await session.flush()+ return client++ async def _agent(+ self, session: AsyncSession, *, parent: Agent | None = None+ ) -> Agent:+ owner = await self._user(session)+ client = await self._client(session, "agent")+ name = self._next("agent")+ key = ApiKey(+ user_id=owner.id,+ key_hash=f"hash-{name}",+ encrypted_key="enc",+ label=name,+ type="agent",+ )+ session.add(key)+ await session.flush()+ agent = Agent(+ name=name,+ description="d",+ agent_type="session_addressable",+ connector_type="claude_code",+ integration_profile={"connection_model": "session_addressable"},+ client_id=client.id,+ api_key_id=key.id,+ owner_id=owner.id,+ parent_agent_id=parent.id if parent is not None else None,+ )+ session.add(agent)+ await session.flush()+ session.add_all(+ [+ Tool(name="search", description="s", agent_id=agent.id),+ Model(name="model", description="m", agent_id=agent.id),+ ]+ )+ await session.flush()+ return agent++ async def _bridges(self, session: AsyncSession) -> None:+ if self.bridges:+ return+ for platform in ("slack", "mattermost"):+ client = await self._client(session, "bridge")+ bridge = CollaborationBridge(+ type=platform,+ display_name=platform.title(),+ connection_config={},+ client_id=client.id,+ status="running",+ )+ session.add(bridge)+ await session.flush()+ self.bridges.append(bridge)++ async def rooms(self, count: int) -> None:+ """`count` rooms, each with its own owner and two member agents, every+ other one bridged with a connected platform user."""+ async with self._session_factory() as session:+ await self._bridges(session)+ for i in range(count):+ owner = await self._user(session)+ bridge = self.bridges[i % 2] if i % 3 else None+ name = self._next("room")+ room = Room(+ transport_room_id=f"!{name}:test",+ name=f"{bridge.display_name}: {name}" if bridge else name,+ description="d",+ owner_id=owner.id,+ bridge_id=bridge.id if bridge else None,+ external_channel_id=f"C-{name}" if bridge else None,+ )+ session.add(room)+ await session.flush()+ for _ in range(2):+ agent = await self._agent(session)+ await session.execute(+ insert(room_agents).values(room_id=room.id, agent_id=agent.id)+ )+ if bridge is not None:+ person = await self._client(session, "external")+ session.add(+ ExternalUser(+ bridge_id=bridge.id,+ external_user_id=f"U-{person.id}",+ external_username=person.display_name,+ client_id=person.id,+ )+ )+ session.add(ClientRoom(client_id=person.id, room_id=room.id))+ await session.flush()+ await session.commit()++ async def agents(self, count: int) -> None:+ """`count` agents, each with its own owner, a tool and a model."""+ async with self._session_factory() as session:+ for _ in range(count):+ await self._agent(session)+ await session.commit()++ async def subject_agent(self, rooms: int) -> Agent:+ """One agent in `rooms` more rooms, each with a live session row and a+ role, and with one more subagent per room."""+ async with self._session_factory() as session:+ if self.subject is None:+ self.subject = await self._agent(session)+ subject = self.subject+ for _ in range(rooms):+ name = self._next("room")+ room = Room(+ transport_room_id=f"!{name}:test", name=name, description="d"+ )+ session.add(room)+ await session.flush()+ await session.execute(+ insert(room_agents).values(room_id=room.id, agent_id=subject.id)+ )+ session.add_all(+ [+ AgentSession(+ agent_id=subject.id,+ room_id=room.id,+ lifecycle="heartbeat",+ last_seen_at=datetime.now(UTC),+ ),+ RoomRole(room_id=room.id, name="lead", instructions="i"),+ ]+ )+ await self._agent(session, parent=subject)+ await session.commit()+ return subject+++def adapter(pool_checkouts: PoolCheckouts, monkeypatch: pytest.MonkeyPatch) -> _Adapter:+ adapter = _Adapter(pool_checkouts)+ monkeypatch.setattr(+ rooms_module, "get_collab_lifecycle", lambda: _CollabLifecycle(adapter)+ )+ return adapter+++async def _count(+ client: httpx.AsyncClient, counts: StatementCounts, path: str+) -> tuple[int, Any]:+ response = await client.get(path)+ assert response.status_code == 200, response.text+ return counts.last(f"GET {path}").count, response.json()+++class TestRoomList:+ async def test_the_same_statements_for_one_room_and_many(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ statement_counts: StatementCounts,+ pool_checkouts: PoolCheckouts,+ adapter: _Adapter,+ ) -> None:+ seed = _Seed(session_factory)+ user = await seed.admin()+ app = _app(session_factory, statement_counts, pool_checkouts)++ async with _client(app, user) as client:+ await seed.rooms(3)+ few, few_rooms = await _count(client, statement_counts, "/rooms")+ await seed.rooms(9)+ many, many_rooms = await _count(client, statement_counts, "/rooms")++ assert (len(few_rooms), len(many_rooms)) == (3, 12)+ assert few == many, statement_counts.last("GET /rooms").statements+ bridged = [r for r in many_rooms if r["bridge_id"]]+ assert bridged and all(r["external_channel_url"] for r in bridged)+ assert all(r["agent_count"] == 2 for r in many_rooms)+ assert all(r["owner_name"] for r in many_rooms)+ assert all(r["connected_user_count"] == 1 for r in bridged)+ assert all(not r["name"].startswith("Slack: ") for r in bridged)++ async def test_the_platform_is_asked_for_deeplinks_holding_no_connection(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ statement_counts: StatementCounts,+ pool_checkouts: PoolCheckouts,+ adapter: _Adapter,+ ) -> None:+ seed = _Seed(session_factory)+ user = await seed.admin()+ await seed.rooms(4)+ app = _app(session_factory, statement_counts, pool_checkouts)++ async with _client(app, user) as client:+ assert (await client.get("/rooms")).status_code == 200++ assert adapter.held and set(adapter.held) == {0}+++class TestAgentList:+ async def test_the_same_statements_for_one_agent_and_many(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ statement_counts: StatementCounts,+ pool_checkouts: PoolCheckouts,+ ) -> None:+ seed = _Seed(session_factory)+ user = await seed.admin()+ app = _app(session_factory, statement_counts, pool_checkouts)++ async with _client(app, user) as client:+ await seed.agents(1)+ few, few_agents = await _count(client, statement_counts, "/agents")+ await seed.agents(9)+ many, many_agents = await _count(client, statement_counts, "/agents")++ assert (len(few_agents), len(many_agents)) == (1, 10)+ assert few == many, statement_counts.last("GET /agents").statements+ assert [a["name"] for a in many_agents] == sorted(+ a["name"] for a in many_agents+ )+ assert all(+ (a["tool_count"], a["model_count"]) == (1, 1) and a["owner_name"]+ for a in many_agents+ )+++class TestAgentDetail:+ async def test_the_same_statements_for_one_room_and_many(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ statement_counts: StatementCounts,+ pool_checkouts: PoolCheckouts,+ ) -> None:+ seed = _Seed(session_factory)+ user = await seed.admin()+ app = _app(session_factory, statement_counts, pool_checkouts)++ async with _client(app, user) as client:+ subject = await seed.subject_agent(1)+ path = f"/agents/{subject.id}"+ few, few_detail = await _count(client, statement_counts, path)+ await seed.subject_agent(6)+ many, many_detail = await _count(client, statement_counts, path)++ assert (len(few_detail["rooms"]), len(many_detail["rooms"])) == (1, 7)+ assert few == many, statement_counts.last(f"GET {path}").statements+ assert {m["status"] for m in many_detail["rooms"]} == {"live"}+ assert len(many_detail["sessions"]) == 7+ assert all(s["room_name"] for s in many_detail["sessions"])+ assert len(many_detail["children"]) == 7+ assert all(+ (c["tool_count"], c["model_count"]) == (1, 1) and c["owner_name"]+ for c in many_detail["children"]+ )diff --git a/core/tests/switch_core/management/test_query_counts.py b/core/tests/switch_core/management/test_query_counts.pynew file mode 100644index 000000000..f78370310--- /dev/null+++ b/core/tests/switch_core/management/test_query_counts.py@@ -0,0 +1,72 @@+"""The owner's managed agent and machine lists run a fixed number of statements.++Console reads both whenever its managed agents page is open, so neither may+read a row per agent or per machine: each is driven over one and over several,+and has to run exactly as many statements both times. See+`tests/switch_core/gateway/test_query_counts.py` for the gateway's own lists.+"""++from __future__ import annotations++import httpx+import pytest+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.db.models import User+from tests.switch_core.management.harness import (+ Harness,+ add_member,+ build_harness,+ cookies_for,+ create_managed_agent,+ enroll_console,+ provider,+ report_status,+)+from tests.switch_core.statement_counts import StatementCounts+++def harness(session_factory: async_sessionmaker[AsyncSession]) -> Harness:+ return build_harness(session_factory)+++async def _place(+ harness: Harness, client: httpx.AsyncClient, owner: User, count: int, start: int+) -> None:+ """`count` more machines, each running one more managed agent."""+ for i in range(start, start + count):+ controller = await enroll_console(harness, client, owner, name=f"machine-{i}")+ await report_status(client, controller, 1, providers=[provider("claude")])+ created = await create_managed_agent(+ client, owner, name=f"agent-{i}", controller_id=controller.controller_id+ )+ assert created.status_code == 201, created.text+++async def _count(+ client: httpx.AsyncClient, counts: StatementCounts, owner: User, path: str+) -> tuple[int, list[dict[str, object]]]:+ response = await client.get(path, cookies=cookies_for(owner))+ assert response.status_code == 200, response.text+ return counts.last(f"GET {path}").count, response.json()+++ "path", ["/gateway/management/agents", "/gateway/management/controllers"]+)+async def test_the_same_statements_for_one_and_many(+ harness: Harness, statement_counts: StatementCounts, path: str+) -> None:+ owner = await add_member(harness.session_factory, "ada")+ async with httpx.AsyncClient(+ transport=httpx.ASGITransport(app=statement_counts.wrap(harness.app)),+ base_url="http://test",+ ) as client:+ await _place(harness, client, owner, 1, 0)+ few, few_rows = await _count(client, statement_counts, owner, path)+ await _place(harness, client, owner, 4, 1)+ many, many_rows = await _count(client, statement_counts, owner, path)++ assert (len(few_rows), len(many_rows)) == (1, 5)+ assert few == many, statement_counts.last(f"GET {path}").statementsdiff --git a/core/tests/switch_core/statement_counts.py b/core/tests/switch_core/statement_counts.pynew file mode 100644index 000000000..4abc2755c--- /dev/null+++ b/core/tests/switch_core/statement_counts.py@@ -0,0 +1,95 @@+"""Count the SQL statements a request runs, at the engine itself.++A route that reads one row per room, per agent or per owner holds its+connection for a round trip per row, so the time it holds the connection grows+with the data and, under load, with every wait between those round trips. The+guard against that is a count that does not grow: the same route over one row+and over many runs the same number of statements.++Counted at the engine's `before_cursor_execute` event, which every statement+reaches whichever store or helper issued it. A request is a `StatementScope`,+entered with `StatementCounts.scope()` or, for an app under test,+`StatementCounts.wrap(app)`; the scope lives in a context variable, which+SQLAlchemy carries into the greenlet a statement runs in, the same way+`pool_checkouts.py` attributes connections.+"""++from __future__ import annotations++from collections.abc import Iterator+from contextlib import contextmanager+from contextvars import ContextVar+from dataclasses import dataclass, field+from typing import Any++from sqlalchemy import event+from sqlalchemy.ext.asyncio import AsyncEngine+++@dataclass+class StatementScope:+ """One request's statements, in the order they ran."""++ label: str+ statements: list[str] = field(default_factory=list)++ @property+ def count(self) -> int:+ return len(self.statements)+++_current: ContextVar[StatementScope | None] = ContextVar(+ "statement_count_scope", default=None+)+++class StatementCounts:+ def __init__(self, engine: AsyncEngine) -> None:+ self._engine = engine.sync_engine+ self.scopes: list[StatementScope] = []+ event.listen(self._engine, "before_cursor_execute", self._on_execute)++ def close(self) -> None:+ event.remove(self._engine, "before_cursor_execute", self._on_execute)++ def _on_execute(+ self,+ _conn: Any,+ _cursor: Any,+ statement: str,+ _parameters: Any,+ _context: Any,+ _executemany: bool,+ ) -> None:+ scope = _current.get()+ if scope is not None:+ scope.statements.append(statement)++ @contextmanager+ def scope(self, label: str) -> Iterator[StatementScope]:+ scope = StatementScope(label)+ self.scopes.append(scope)+ token = _current.set(scope)+ try:+ yield scope+ finally:+ _current.reset(token)++ def wrap(self, app: Any) -> Any:+ """`app`, with every HTTP request in a scope of its own."""++ async def asgi(scope: dict[str, Any], receive: Any, send: Any) -> None:+ if scope["type"] != "http":+ await app(scope, receive, send)+ return+ with self.scope(f"{scope['method']} {scope['path']}"):+ await app(scope, receive, send)++ return asgi++ def last(self, label: str) -> StatementScope:+ """The most recent request counted under `label`."""+ for scope in reversed(self.scopes):+ if scope.label == label:+ return scope+ raise AssertionError(f"no request {label!r} was counted")