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(rooms): renaming an agent tells everyone who sees its rooms

raullopez-sandboxb296d6a3d932
diff --git a/core/switch_core/chats/service.py b/core/switch_core/chats/service.py
index fff1da5f5..918681317 100644
--- a/core/switch_core/chats/service.py
+++ b/core/switch_core/chats/service.py
@@ -200,6 +200,9 @@ class ChatService:
weakref.WeakValueDictionary()
)
self._membership_watchers: dict[tuple[str, str], set[MembershipWatcher]] = {}
+ self._agent_room_listeners: tuple[
+ Callable[[str, str, list[str]], None], ...
+ ] = ()
# Where a change to people's chats made here is announced to the
# other processes, once wired (`use_signals`).
self._signals: ChatSignals | None = None
@@ -560,9 +563,34 @@ class ChatService:
[room_id],
)
+ def on_agent_room_changed(
+ self, listener: Callable[[str, str, list[str]], None]
+ ) -> None:
+ """Call `listener(tenant_id, room_id, [])` for each room an agent is in
+ when its name, display name or icon changes (`room_changes`), so
+ whoever shows the room reads it again. It must not block."""
+ self._agent_room_listeners = (*self._agent_room_listeners, listener)
+
async def agent_changed(self, tenant_id: str, agent_id: str) -> None:
- """Wake the streams of everyone in a chat with this agent, so they
- re-send its summaries. Does nothing with `agent_chats` off."""
+ """Tell everyone who sees a room with this agent that it changed, and
+ with `agent_chats` on, wake the streams of everyone in a chat with it
+ so they re-send its summaries."""
+ async with tenant_session(self.session_factory, tenant_id) as session:
+ room_ids = list(
+ (
+ await session.execute(
+ select(room_agents.c.room_id).where(
+ room_agents.c.agent_id == agent_id
+ )
+ )
+ ).scalars()
+ )
+ for room_id in room_ids:
+ for listener in self._agent_room_listeners:
+ try:
+ listener(tenant_id, room_id, [])
+ except Exception:
+ logger.exception("Agent room listener failed for room %s", room_id)
if not self.agent_chats_enabled:
return
async with tenant_session(self.session_factory, tenant_id) as session:
@@ -580,15 +608,6 @@ class ChatService:
)
).scalars()
user_ids = sorted(user_id for user_id in members if user_id is not None)
- room_ids = list(
- (
- await session.execute(
- select(room_agents.c.room_id).where(
- room_agents.c.agent_id == agent_id
- )
- )
- ).scalars()
- )
self._memberships_changed(tenant_id, user_ids, room_ids)
async def _make_lasting(self, tenant_id: str, user_id: str, room_id: str) -> None:
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index 93b6825e6..c446abfe8 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -1173,6 +1173,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:
room_service.on_room_agents_changed(chat_service.sync_room)
protocol.set_room_agents_listener(room_service.room_agents_changed)
protocol.set_agent_profile_listener(chat_service.agent_changed)
+ chat_service.on_agent_room_changed(room_changes.room_changed)
# ── Connector app credentials ─────────────────────────────────────────────
# Each connector's app, from configuration or a row the operator saved,
diff --git a/core/tests/switch_core/gateway/test_chats_groups.py b/core/tests/switch_core/gateway/test_chats_groups.py
index 98aa883d9..962110ad4 100644
--- a/core/tests/switch_core/gateway/test_chats_groups.py
+++ b/core/tests/switch_core/gateway/test_chats_groups.py
@@ -1286,3 +1286,31 @@ async def test_people_are_told_when_a_room_of_theirs_changes(chats: _Harness) ->
)
assert archived.status_code == 204
assert await told() == {chats.alice.id}
+
+
+async def test_renaming_an_agent_tells_everyone_who_sees_its_rooms(
+ chats: _Harness,
+) -> None:
+ published = _Published()
+ changes = RoomChanges(session_factory=chats.factory, publisher=published)
+ chats.service.on_agent_room_changed(changes.room_changed)
+ agent = await chats.agent("renamed", owner=chats.admin)
+ room_id = await chats.new_chat(chats.alice, agent, "c1")
+ assert (await chats.invite(chats.alice, room_id, chats.bob)).status_code == 200
+ events = _stream(chats, chats.bob, {}, recheck=60.0)
+ assert await events.until("ready") == {}
+
+ async with chats.factory() as session:
+ row = await session.get(Agent, agent.id)
+ assert row is not None
+ row.display_name = "QA Blair"
+ await session.commit()
+ await chats.service.agent_changed("00000000-0000-0000-0000-000000000000", agent.id)
+ await asyncio.gather(*changes._tasks)
+ assert {(user, kind, id) for user, kind, id in published.notices} == {
+ (user, ROOM, room_id) for user in (chats.alice.id, chats.bob.id, chats.admin.id)
+ }
+ name, summary = await events.next()
+ assert (name, summary["roomId"]) == ("chat", room_id)
+ assert summary["agents"][0]["displayName"] == "QA Blair"
+ await events.stream.aclose() # type: ignore[attr-defined]