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
fix(rooms): a channel rename never overwrites a name given meanwhile
raullopez-sandbox85b32465218d
diff --git a/core/switch_core/bridges/collaboration/collaboration_core.py b/core/switch_core/bridges/collaboration/collaboration_core.pyindex 8ab3f06ec..8093ef325 100644--- a/core/switch_core/bridges/collaboration/collaboration_core.py+++ b/core/switch_core/bridges/collaboration/collaboration_core.py@@ -55,7 +55,6 @@ from switch_core.db.stores.client_store import ClientStorefrom switch_core.db.stores.external_user_store import ExternalUserStorefrom switch_core.db.stores.room_store import (LINKED_CHANNEL_NAME_KEY,- NAME_SOURCE_KEY,RoomStore,)from switch_core.db.tenant_lookup import tenant_of_room@@ -880,15 +879,11 @@ class CollaborationCore:if room is None:returnmetadata = room.metadata_ or {}- source = metadata.get(NAME_SOURCE_KEY)- follows = source == "channel" or (- source is None- and old_name != new_name- and room.name == f"{bridge_name}: {old_name}"- )- if follows and room.name != name:- await self._room_service.take_platform_name(room.id, name, new_name)- elif metadata.get(LINKED_CHANNEL_NAME_KEY) != new_name:+ if room.name != name and await self._room_service.follow_channel_name(+ room.id, name, new_name, f"{bridge_name}: {old_name}"+ ):+ return+ if metadata.get(LINKED_CHANNEL_NAME_KEY) != new_name:await self._room_service.record_channel_name(room.id, new_name)# ── Inbound (platform → room) ───────────────────────────────────────────diff --git a/core/switch_core/bridges/collaboration/discord/adapter.py b/core/switch_core/bridges/collaboration/discord/adapter.pyindex bb7661337..37722b949 100644--- a/core/switch_core/bridges/collaboration/discord/adapter.py+++ b/core/switch_core/bridges/collaboration/discord/adapter.py@@ -628,6 +628,8 @@ class DiscordAdapter(PlatformAdapter):super().__init__()self._config = configself._guild_id = int(config.guild_id)+ # Channels whose name this bridge has reported since it started.+ self._channel_names_seen: set[int] = set()# The Gateway socket lives on the connection, not the adapter: the# socket is per bot token and the adapter is per guild. Intents are# built here and handed over, so the socket owner does not decide them.@@ -837,6 +839,12 @@ class DiscordAdapter(PlatformAdapter):made before the bridge followed renames."""if after.guild.id != self._guild_id:return+ # Only a rename, or the first word of a channel since this process+ # started, is worth the bridge's lookup; Discord sends an update for+ # every permission or topic change too.+ if before.name == after.name and after.id in self._channel_names_seen:+ return+ self._channel_names_seen.add(after.id)if self._on_channel_named is not None:await self._on_channel_named(str(after.id), before.name, after.name)diff --git a/core/switch_core/bridges/collaboration/discord/gateway.py b/core/switch_core/bridges/collaboration/discord/gateway.pyindex 9f7751091..b509480f8 100644--- a/core/switch_core/bridges/collaboration/discord/gateway.py+++ b/core/switch_core/bridges/collaboration/discord/gateway.py@@ -88,6 +88,8 @@ class DiscordGatewayClient:self._task: asyncio.Task[None] | None = None# The socket as owned work, once `own` has registered it.self._owned: OwnedKind | None = None+ # Channels whose update has been routed since this process started.+ self._channel_names_seen: set[int] = set()# command_guild_id=None → commands register globally, once for the# application across every guild (decision #7); guild-scoped registration# is the self-registered adapter's, which serves one guild.@@ -318,7 +320,11 @@ class DiscordGatewayClient:) -> None:"""Route a guild channel's change to the bridge its guild resolves to,fresh and with no tenant bound, as a message is (G1). A guild with no- install is dropped."""+ install is dropped. Only a rename, or a channel's first update since+ this process started, is resolved; the adapter makes the same cut."""+ if before.name == after.name and after.id in self._channel_names_seen:+ return+ self._channel_names_seen.add(after.id)with no_tenant():try:target = await self._install_service.resolve_by_workspace(diff --git a/core/switch_core/db/stores/room_store.py b/core/switch_core/db/stores/room_store.pyindex b51786a94..7ed4fa8dd 100644--- a/core/switch_core/db/stores/room_store.py+++ b/core/switch_core/db/stores/room_store.py@@ -772,7 +772,7 @@ class RoomStore:room = await session.get(Room, room_id)if room is None:raise ValueError(f"Room not found: {room_id}")- if name is not None:+ if name is not None and name != room.name:room.name = name# Named by someone: a platform's channel name no longer replaces it.room.metadata_ = {**(room.metadata_ or {}), NAME_SOURCE_KEY: "custom"}@@ -804,6 +804,31 @@ class RoomStore:}await session.flush()+ async def follow_channel_name(+ self,+ session: AsyncSession,+ room_id: str,+ name: str,+ channel_name: str,+ legacy_name: str,+ ) -> bool:+ """`set_platform_name`, decided on the row locked for it: only when the+ room is named after its channel, or, from before that was recorded,+ still has exactly `legacy_name`. False, and nothing written, when+ someone named the room meanwhile."""+ room = (+ await session.execute(+ select(Room).where(Room.id == room_id).with_for_update()+ )+ ).scalar_one_or_none()+ if room is None:+ return False+ source = (room.metadata_ or {}).get(NAME_SOURCE_KEY)+ if not (source == "channel" or (source is None and room.name == legacy_name)):+ return False+ await self.set_platform_name(session, room_id, name, channel_name)+ return True+async def record_channel_name(self, session: AsyncSession, room_id: str, channel_name: str) -> None:diff --git a/core/switch_core/room_service.py b/core/switch_core/room_service.pyindex 18cb3ef61..0fefe26a1 100644--- a/core/switch_core/room_service.py+++ b/core/switch_core/room_service.py@@ -1196,6 +1196,21 @@ class RoomService:await session.commit()self._room_changed(tenant_id, room_id)+ async def follow_channel_name(+ self, room_id: str, name: str, channel_name: str, legacy_name: str+ ) -> bool:+ """Rename a bridged room after its renamed channel when it still follows+ the channel (`RoomStore.follow_channel_name`); False when it does not."""+ tenant_id = await self._room_tenant(room_id)+ async with tenant_session(self._session_factory, tenant_id) as session:+ followed = await self._room_store.follow_channel_name(+ session, room_id, name, channel_name, legacy_name+ )+ await session.commit()+ if followed:+ self._room_changed(tenant_id, room_id)+ return followed+async def record_channel_name(self, room_id: str, channel_name: str) -> None:"""Keep the linked channel's current name on a room that does notfollow it, so whoever sees the room can be offered it."""diff --git a/core/tests/switch_core/bridges/collaboration/test_bridge_channel_rename.py b/core/tests/switch_core/bridges/collaboration/test_bridge_channel_rename.pyindex b6d297803..1ebb383e0 100644--- a/core/tests/switch_core/bridges/collaboration/test_bridge_channel_rename.py+++ b/core/tests/switch_core/bridges/collaboration/test_bridge_channel_rename.py@@ -36,14 +36,22 @@ class _RoomStore:class _RoomService:- def __init__(self) -> None:+ """Decides as `RoomStore.follow_channel_name` does, over the fake rooms."""++ def __init__(self, rooms: dict[str, SimpleNamespace]) -> None:+ self._rooms = {room.id: room for room in rooms.values()}self.renamed: list[tuple[str, str, str, str | None]] = []self.recorded: list[tuple[str, str]] = []- async def take_platform_name(- self, room_id: str, name: str, channel_name: str- ) -> None:+ async def follow_channel_name(+ self, room_id: str, name: str, channel_name: str, legacy_name: str+ ) -> bool:+ room = self._rooms[room_id]+ source = room.metadata_.get(NAME_SOURCE_KEY)+ if not (source == "channel" or (source is None and room.name == legacy_name)):+ return Falseself.renamed.append((room_id, name, channel_name, current_tenant_id()))+ return Trueasync def record_channel_name(self, room_id: str, channel_name: str) -> None:self.recorded.append((room_id, channel_name))@@ -62,7 +70,7 @@ def _bridge(rooms: dict[str, SimpleNamespace]) -> SimpleNamespace:return SimpleNamespace(_session_factory=lambda: session,_room_store=_RoomStore(rooms),- _room_service=_RoomService(),+ _room_service=_RoomService(rooms),_bridge_id="bridge-1",_bridge_tenant_id="tenant-1",_bridge_type="discord",@@ -125,8 +133,16 @@ async def test_naming_a_room_marks_it_custom_and_a_platform_name_channel(metadata_={"created_by_kind": "system"},),)+ # Saving the name it already has is not naming it.+ await store.update_fields(session, room.id, name="Discord: general")+ assert NAME_SOURCE_KEY not in room.metadata_await store.update_fields(session, room.id, name="Discord: our room")assert room.metadata_[NAME_SOURCE_KEY] == "custom"+ # Named by someone between the bridge's read and its write: kept.+ assert not await store.follow_channel_name(+ session, room.id, "Discord: general-2", "general-2", "Discord: our room"+ )+ assert room.name == "Discord: our room"await store.set_platform_name(session, room.id, "Discord: general-2", "general-2")@@ -141,7 +157,9 @@ async def test_the_adapter_reports_only_its_own_guilds_channels() -> None:async def named(channel_id: str, old: str, new: str) -> None:reported.append((channel_id, new))- adapter = SimpleNamespace(_guild_id=1, _on_channel_named=named)+ adapter = SimpleNamespace(+ _guild_id=1, _on_channel_named=named, _channel_names_seen=set()+ )def channel(guild_id: int, name: str) -> SimpleNamespace:return SimpleNamespace(id=42, name=name, guild=SimpleNamespace(id=guild_id))@@ -149,6 +167,9 @@ async def test_the_adapter_reports_only_its_own_guilds_channels() -> None:for before, after in ((channel(1, "old"), channel(1, "new")),(channel(2, "old"), channel(2, "new")),+ # A topic or permission change, once the channel has been seen: no lookup.+ (channel(1, "new"), channel(1, "new")),+ (channel(1, "new"), channel(1, "newer")),):await DiscordAdapter.dispatch_channel_update(adapter, before, after) # type: ignore[arg-type]- assert reported == [("42", "new")]+ assert reported == [("42", "new"), ("42", "newer")]