Skip to content

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

Stop idle controllers re-reading every agent, and read a controller's agents' rooms at once (CHOO-3667)

Abel Dantasfa003df1d5d7
diff --git a/core/switch_core/bridges/agent/protocol/controller_stream.py b/core/switch_core/bridges/agent/protocol/controller_stream.py
index 4562bc18c..db90e0e35 100644
--- a/core/switch_core/bridges/agent/protocol/controller_stream.py
+++ b/core/switch_core/bridges/agent/protocol/controller_stream.py
@@ -219,8 +219,8 @@ class _ControllerStream:
yield raw
delivered = False
- for attached in list(self._attached.values()):
- for raw in await self._deliver(attached):
+ for attached, head in await self._due():
+ for raw in await self._deliver(attached, head):
delivered = True
yield raw
if delivered:
@@ -393,7 +393,23 @@ class _ControllerStream:
)
return frames
- async def _deliver(self, attached: _Attached) -> list[ControllerFrame]:
+ async def _due(self) -> list[tuple[_Attached, int]]:
+ """The attached agents a delivery pass has work for, each with its
+ head: events past its cursor, or rooms without an unread-count baseline
+ yet. The heads are read in one query, so a pass over a controller's
+ agents, such as the one after each idle interval, costs one read when
+ nothing is new rather than a session per agent."""
+ if not self._attached:
+ return []
+ heads = await self._buffer.heads(self._tenant, set(self._attached))
+ return [
+ (attached, heads[attached.agent_id])
+ for attached in list(self._attached.values())
+ if heads[attached.agent_id] > attached.cursor
+ or self._presence.rooms(attached.agent_id) - attached.counted
+ ]
+
+ async def _deliver(self, attached: _Attached, bound: int) -> list[ControllerFrame]:
agent_id = attached.agent_id
for room_id in self._presence.rooms(agent_id) - attached.counted:
await self._buffer.ensure_counting(
@@ -405,12 +421,11 @@ class _ControllerStream:
# read. The frames are built after it, and returned rather than
# yielded, so the connection is back in the pool before any is sent.
async with self._buffer.one_session(self._tenant):
- # Take the number of the newest of the agent's own events before
- # reading, and read no further than it. A read that finds nothing moves
- # the cursor up to this number. An event added to `agent_events` while
- # the read runs is numbered above it, so the cursor does not skip that
- # event.
- bound = await self._buffer.head(self._tenant, agent_id)
+ # `bound` is the agent's head, read before this: the read goes no
+ # further than it, and a read that finds nothing moves the cursor
+ # up to it. An event added to `agent_events` since is numbered above
+ # it, so the cursor does not skip that event; its doorbell brings
+ # the next pass.
try:
pending = await self._buffer.read_from(
self._tenant,
diff --git a/core/switch_core/db/stores/room_store.py b/core/switch_core/db/stores/room_store.py
index e91ce9b15..f0f9c22de 100644
--- a/core/switch_core/db/stores/room_store.py
+++ b/core/switch_core/db/stores/room_store.py
@@ -462,6 +462,24 @@ class RoomStore:
result = await session.execute(stmt)
return list(result.scalars().all())
+ async def room_ids_by_agent(
+ self, session: AsyncSession, agent_ids: Collection[str]
+ ) -> dict[str, set[str]]:
+ """The ids of every room each agent is a member of, archived rooms
+ included, in one query: what `get_rooms_for_agent(include_archived=True)`
+ gives one agent at a time. Every agent asked about has a key."""
+ by_agent: dict[str, set[str]] = {agent_id: set() for agent_id in agent_ids}
+ if not by_agent:
+ return by_agent
+ result = await session.execute(
+ select(room_agents.c.agent_id, room_agents.c.room_id)
+ .join(Room, Room.id == room_agents.c.room_id)
+ .where(room_agents.c.agent_id.in_(by_agent))
+ )
+ for agent_id, room_id in result.all():
+ by_agent[agent_id].add(room_id)
+ return by_agent
+
async def get_agent_ids(self, session: AsyncSession, room_id: str) -> list[str]:
result = await session.execute(
select(room_agents.c.agent_id).where(room_agents.c.room_id == room_id)
diff --git a/core/switch_core/management/signals.py b/core/switch_core/management/signals.py
index 5f2a1200c..023054199 100644
--- a/core/switch_core/management/signals.py
+++ b/core/switch_core/management/signals.py
@@ -316,15 +316,12 @@ class ControllerSignals:
here, and the stream that loads them runs over there."""
if self._held_elsewhere(controller, leases) is None:
return {}
- return {
- agent_id: {
- room.id
- for room in await self._rooms.get_rooms_for_agent(
- session, agent_id, include_archived=True
- )
- }
- for agent_id in sorted(self._presence.agents_of(controller.id))
- }
+ # One query for all of them: every process re-reads this for every
+ # controller another process holds at each re-check, so a query per
+ # agent would grow with processes times agents.
+ return await self._rooms.room_ids_by_agent(
+ session, self._presence.agents_of(controller.id)
+ )
def _held_elsewhere(
self, controller: AgentController, leases: ProcessLeases
diff --git a/core/tests/switch_core/db/stores/test_room_archive.py b/core/tests/switch_core/db/stores/test_room_archive.py
index 69a360b0f..d3ea6e0e2 100644
--- a/core/tests/switch_core/db/stores/test_room_archive.py
+++ b/core/tests/switch_core/db/stores/test_room_archive.py
@@ -151,3 +151,27 @@ class TestArchivedFilteringInListing:
session, agent.id, include_archived=True
)
assert {r.id for r in with_archived} == {active.id, archived.id}
+
+ async def test_room_ids_by_agent_reads_many_agents_archived_included(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ store = RoomStore()
+ async with session_factory() as session:
+ first = await _make_agent(session, "agent-1")
+ second = await _make_agent(session, "agent-2")
+ roomless = await _make_agent(session, "agent-3")
+ active = await _make_room(store, session, "active")
+ archived = await _make_room(store, session, "archived")
+ await store.add_agents(session, active.id, [first.id, second.id])
+ await store.add_agents(session, archived.id, [first.id])
+ await store.set_archived(session, archived.id, True)
+ await session.commit()
+
+ assert await store.room_ids_by_agent(
+ session, [first.id, second.id, roomless.id]
+ ) == {
+ first.id: {active.id, archived.id},
+ second.id: {active.id},
+ roomless.id: set(),
+ }
+ assert await store.room_ids_by_agent(session, []) == {}
diff --git a/core/tests/switch_core/management/test_stream.py b/core/tests/switch_core/management/test_stream.py
index 2e4e474cb..af6c54f14 100644
--- a/core/tests/switch_core/management/test_stream.py
+++ b/core/tests/switch_core/management/test_stream.py
@@ -228,9 +228,9 @@ class TestOpening:
async def test_an_agents_pass_reads_in_one_session(
self, harness: Harness, monkeypatch: pytest.MonkeyPatch
) -> None:
- """The agent's head, its own events, what was dropped after its cursor
- and the unread counts cost one pool checkout and one transaction, not
- one of each per read."""
+ """The agent's own events, what was dropped after its cursor and the
+ unread counts cost one pool checkout and one transaction, not one of
+ each per read, after one read of the heads of the controller's agents."""
owner = await add_member(harness.session_factory, "ada")
async with harness.client() as client:
controller = await enroll_console(harness, client, owner)
@@ -249,7 +249,7 @@ class TestOpening:
assert name == "agent.event"
assert data["event"]["payload"]["body"] == "@you"
- assert counting.opened == 1
+ assert counting.opened == 2
async def test_a_cursor_from_before_a_restart_is_a_gap(
self, harness: Harness
@@ -776,3 +776,32 @@ async def test_a_waiting_stream_wakes_promptly_for_an_event(harness: Harness) ->
raw = await asyncio.wait_for(pending, timeout=2)
await stream.aclose()
assert raw["event"] == "agent.event"
+
+
+async def test_an_idle_pass_reads_its_agents_heads_together(
+ harness: Harness, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """With nothing new for any agent, the pass after each idle interval reads
+ the heads of all the controller's agents in one query, not each agent's
+ events in a session of its own."""
+ monkeypatch.setattr(controller_routes, "IDLE_INTERVAL_SECONDS", 0.05)
+ owner = await add_member(harness.session_factory, "ada")
+ async with harness.client() as client:
+ controller = await enroll_console(harness, client, owner)
+ for n in range(5):
+ agent_id = await place_agent(client, controller, name=f"agent-{n}")
+ await add_room(harness.session_factory, agent_id, name=f"room-{n}")
+ opened = await open_connection(client, controller)
+ stream = await open_stream(harness, controller, opened)
+ await take(stream, 6)
+ assert await anext(stream) is controller_routes.IDLE
+
+ buffer = harness.protocol.event_buffer
+ counting = CountingSessionFactory(buffer._session_factory)
+ monkeypatch.setattr(buffer, "_session_factory", counting)
+ assert await anext(stream) is controller_routes.IDLE
+ await stream.aclose()
+
+ # One read of the heads for the pass, one more when the wait checks
+ # again for anything that arrived as it began.
+ assert counting.opened == 2