Skip to content

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

fix(addressing): auto-address only a direct room's sole agent

raullopez-sandbox4c64d4f45a6a
diff --git a/core/switch_core/delivery/addressing.py b/core/switch_core/delivery/addressing.py
index a846f2203..4214e0501 100644
--- a/core/switch_core/delivery/addressing.py
+++ b/core/switch_core/delivery/addressing.py
@@ -4,7 +4,7 @@ Two separate questions, deliberately kept apart:
- **Addressed** is about intent. Did this message ask this agent for something
— by name, by the alias it goes under in this room, by a role it holds, or
- by being the only other party in a direct chat?
+ by being the only agent in a direct chat (`auto_addressed_room`)?
- **Permitted** is about authority. An agent may carry a scoped addressing
policy saying who is allowed to ask it for things at all.
@@ -56,6 +56,29 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
+
+def named_agents(
+ names: Mapping[str, str], aliases: Mapping[str, str], body: str
+) -> list[str]:
+ """The ids of the agents `body` names by `@name` or room `@alias`, sorted.
+
+ For display only: it reads no pill, role or room type, so it says who a
+ message visibly names rather than who it addresses.
+ """
+ text = strip_emphasis(body)
+ named = {
+ agent_id
+ for agent_id, name in names.items()
+ if mention_regex(name).search(text) is not None
+ }
+ named.update(
+ agent_id
+ for agent_id, alias in aliases.items()
+ if agent_id in names and mention_regex(alias).search(text) is not None
+ )
+ return sorted(named)
+
+
# Posted (once, guarded by an auto-reply flag) when a sender tags an agent but
# the agent's scoped addressing policy does not permit that sender to address
# it there. The message is demoted to unaddressed room chatter; this reply is
@@ -151,7 +174,8 @@ class AddressingResolver:
message: IncomingMessage,
) -> bool | None:
"""The addressing answer that needs no database read, or None when the
- room's aliases and role leases have to be consulted.
+ room's aliases, role leases or (in a direct room) 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
@@ -159,12 +183,12 @@ class AddressingResolver:
"""
if ADMIN_MARKER in message.content:
return False
- if channel_type == "direct":
- return True
if self.mentions_name(
agent=agent, agent_user_id=agent_user_id, message=message
):
return True
+ if channel_type == "direct":
+ return None
if "@" not in message.body:
return False
return None
@@ -184,8 +208,8 @@ class AddressingResolver:
**A system message never addresses anyone**, whatever the room type.
Switch's own notices — a command's answer, "Added X to this room", the
guidance shown when someone tags the app itself — are output, not a
- request for a reply. Two ways they were read as one: in a direct room
- every message addresses the agent, so running `/list-agents` in a 1:1
+ request for a reply. Two ways they were read as one: in a one-agent
+ direct room every message addresses the agent, so running `/list-agents` in a 1:1
chat had the agent start a session to respond to its own roster; and in
any room, a notice that lists the agents present writes each `@name`,
which tagged every one of them. The marker exists to say "generated by
@@ -199,6 +223,10 @@ class AddressingResolver:
)
if decided is not None:
return decided
+ if await self.auto_addressed_room(
+ session, room_id=room_id, channel_type=channel_type
+ ):
+ return True
if await self.mentions_alias(
session, agent=agent, room_id=room_id, message=message
):
@@ -207,6 +235,20 @@ class AddressingResolver:
session, agent=agent, room_id=room_id, message=message
)
+ async def auto_addressed_room(
+ self, session: AsyncSession, *, room_id: str, channel_type: str | None
+ ) -> bool:
+ """Whether every message in this room is for its agent without naming it.
+
+ Only a direct room with exactly one agent. A direct room that gained a
+ second agent reads as a group: each message has to name the agents it
+ is for, or it is a note for nobody. Public and private rooms never
+ auto-address, whatever their agent count.
+ """
+ if channel_type != "direct":
+ return False
+ return len(await self._room_store.get_agent_ids(session, room_id)) == 1
+
def mentions_name(
self, *, agent: Agent, agent_user_id: str, message: IncomingMessage
) -> bool:
diff --git a/core/tests/switch_core/delivery/test_system_message_never_addresses.py b/core/tests/switch_core/delivery/test_system_message_never_addresses.py
index ee57b941e..3ab391bbe 100644
--- a/core/tests/switch_core/delivery/test_system_message_never_addresses.py
+++ b/core/tests/switch_core/delivery/test_system_message_never_addresses.py
@@ -46,7 +46,8 @@ def _meta(channel_type: str) -> RoomMeta:
def _client(name: str = "flintai-sdk.ts") -> SimpleNamespace:
- """A resolver with the two store-backed checks stubbed out.
+ """A resolver with the store-backed checks stubbed out, in rooms holding
+ one agent.
Only the name mention is left live: alias and role routing have their own
tests, and what these pin is that the marker beats all three.
@@ -65,8 +66,14 @@ def _client(name: str = "flintai-sdk.ts") -> SimpleNamespace:
resolver.addressed_without_lookup = lambda **kw: (
AddressingResolver.addressed_without_lookup(resolver, **kw)
)
+ async def _one_agent_direct_room(
+ _session: object, *, room_id: str, channel_type: str | None
+ ) -> bool:
+ return channel_type == "direct"
+
resolver.mentions_alias = _no
resolver.mentions_role = _no
+ resolver.auto_addressed_room = _one_agent_direct_room
return resolver