Skip to content

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

perf(core): answer a direct room's agent count from the room's own read

raullopez-sandbox9c3f3161a7d7
diff --git a/core/switch_core/clients/room_meta.py b/core/switch_core/clients/room_meta.py
index 105762c76..ff0d2756e 100644
--- a/core/switch_core/clients/room_meta.py
+++ b/core/switch_core/clients/room_meta.py
@@ -12,6 +12,10 @@ class RoomMeta(BaseModel):
# rooms and when no bridge resolves.
agent_greetings_enabled: bool = True
channel_type: str | None = None
+ # How many agents the room holds, read with the room so a direct room's
+ # "is this for its one agent?" needs no read per message. None where the
+ # meta was built without it; addressing then reads the count itself.
+ agent_count: int | None = None
# The room's follow-the-conversation settings (`!follow-ups`,
# `!assign-agent`), read with the room from its metadata.
follow_ups_enabled: bool = True
diff --git a/core/switch_core/delivery/addressing.py b/core/switch_core/delivery/addressing.py
index 1eaf2946c..87303ce24 100644
--- a/core/switch_core/delivery/addressing.py
+++ b/core/switch_core/delivery/addressing.py
@@ -194,11 +194,12 @@ class AddressingResolver:
agent: Agent,
agent_user_id: str,
channel_type: str | None,
+ agent_count: int | None,
message: IncomingMessage,
) -> bool | None:
"""The addressing answer that needs no database read, or None when the
- room's aliases, role leases or (in a direct room) agent count have to
- be consulted.
+ room's aliases, role leases or (in a direct room whose `agent_count`
+ the caller does not know) agent count have to be consulted.
Callers use it to decide whether to take a pool slot at all: most room
chatter carries no `@` and is answered here, and a room fans every
@@ -211,7 +212,10 @@ class AddressingResolver:
):
return True
if channel_type == "direct":
- return None
+ if agent_count is None:
+ return None
+ if agent_count == 1:
+ return True
if "@" not in message.addressable_body:
return False
return None
@@ -242,6 +246,7 @@ class AddressingResolver:
agent=agent,
agent_user_id=agent_user_id,
channel_type=channel_type,
+ agent_count=None,
message=message,
)
if decided is not None:
diff --git a/core/switch_core/delivery/recipient.py b/core/switch_core/delivery/recipient.py
index 57c2a2ca1..bc33e718a 100644
--- a/core/switch_core/delivery/recipient.py
+++ b/core/switch_core/delivery/recipient.py
@@ -1553,6 +1553,7 @@ class AgentRecipient:
agent=self.agent,
agent_user_id=self.transport_user_id,
channel_type=meta.channel_type,
+ agent_count=meta.agent_count,
message=self._as_incoming(event),
)
diff --git a/core/switch_core/delivery/router.py b/core/switch_core/delivery/router.py
index 031fb991c..44c8d416d 100644
--- a/core/switch_core/delivery/router.py
+++ b/core/switch_core/delivery/router.py
@@ -220,7 +220,13 @@ class RoomReads:
"""
found = (
await session.execute(
- select(Room, CollaborationBridge.agent_greetings_enabled)
+ select(
+ Room,
+ CollaborationBridge.agent_greetings_enabled,
+ select(func.count())
+ .where(room_agents.c.room_id == Room.id)
+ .scalar_subquery(),
+ )
.outerjoin(
CollaborationBridge,
and_(
@@ -233,7 +239,7 @@ class RoomReads:
).first()
if found is None:
return None
- room, greetings = found
+ room, greetings, agent_count = found
rows = (
await session.execute(
select(
@@ -291,6 +297,7 @@ class RoomReads:
bridge_id=room.bridge_id,
agent_greetings_enabled=True if greetings is None else greetings,
channel_type=room.channel_type,
+ agent_count=agent_count,
follow_ups_enabled=follow_ups_enabled_in(room.metadata_),
main_agent_id=main_agent_in(room.metadata_),
agent_greeting=(room.metadata_ or {}).get("agent_greeting"),
@@ -437,6 +444,16 @@ class _RoomAddressing(AddressingResolver):
return await super().role_of(session, room_id, agent_id)
return (await self._reads.role_names(session)).get(agent_id)
+ async def auto_addressed_room(
+ self, session: AsyncSession, *, room_id: str, channel_type: str | None
+ ) -> bool:
+ count = self._reads.meta.agent_count
+ if room_id != self._reads.room_id or count is None:
+ return await super().auto_addressed_room(
+ session, room_id=room_id, channel_type=channel_type
+ )
+ return channel_type == "direct" and count == 1
+
async def group_of(self, session: AsyncSession, room_id: str) -> str | None:
if room_id != self._reads.room_id:
return await super().group_of(session, room_id)
diff --git a/core/tests/switch_core/delivery/test_router.py b/core/tests/switch_core/delivery/test_router.py
index 057ae385c..e58de81ba 100644
--- a/core/tests/switch_core/delivery/test_router.py
+++ b/core/tests/switch_core/delivery/test_router.py
@@ -608,6 +608,46 @@ class TestCost:
assert small.get("SELECT") == large.get("SELECT"), (small, large)
assert small == large
+ @pytest.mark.parametrize("agents", [1, 2])
+ async def test_a_direct_room_reads_no_agent_count_per_message(
+ self,
+ agents: int,
+ session_factory: SessionFactory,
+ statement_counts: StatementCounts,
+ ) -> None:
+ """Whether a direct room holds one agent rides on the room's own read,
+ so every message there costs no query for the room's agents: one
+ agent hears an unnamed message, two do not."""
+ process = process_on(session_factory)
+ room = await make_room(session_factory, channel_type="direct")
+ alice = await make_person(session_factory, "alice")
+ await add_member(session_factory, room, alice)
+ ids = []
+ for n in range(agents):
+ agent, client = await make_agent(
+ session_factory, f"d{n}-{uuid.uuid4().hex[:4]}"
+ )
+ await add_member(session_factory, room, client, agent=agent)
+ _go_live(process, agent.id)
+ ids.append(agent.id)
+ await process.say(room, "hello", sender=alice)
+ await process.idle()
+ before = [len(await own_events(session_factory, i)) for i in ids]
+
+ with statement_counts.scope("direct") as scope:
+ await process.say(room, "just chatting", sender=alice)
+ await process.idle()
+
+ agent_reads = [
+ s
+ for s in (" ".join(st.split()) for st in scope.statements)
+ if s.startswith("SELECT room_agents.agent_id FROM room_agents")
+ ]
+ assert agent_reads == []
+ after = [len(await own_events(session_factory, i)) for i in ids]
+ heard = 1 if agents == 1 else 0
+ assert after == [b + heard for b in before]
+
async def test_writes_grow_with_the_agents_a_message_is_for(
self, session_factory: SessionFactory, statement_counts: StatementCounts
) -> None: