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(chats): two processes creating one person's identity share it
raullopez-sandbox75428e631fa8
diff --git a/core/switch_core/chats/service.py b/core/switch_core/chats/service.pyindex 1b7946875..f8a467c4f 100644--- a/core/switch_core/chats/service.py+++ b/core/switch_core/chats/service.py@@ -308,15 +308,31 @@ class ChatService:client = await self.member_client(session, user.id)if client is not None:return client- client = Client(- transport_user_id=f"@switch-member-{user.id}:{self._id_server_name}",- display_name=user.name,- type=MEMBER_CLIENT_TYPE,- user_id=user.id,+ # Another process may be creating the same one: the first+ # insert wins, and this one reads what it wrote.+ created = await session.scalar(+ pg_insert(Client)+ .values(+ id=str(uuid.uuid4()),+ tenant_id=tenant_id,+ transport_user_id=f"@switch-member-{user.id}:{self._id_server_name}",+ display_name=user.name,+ type=MEMBER_CLIENT_TYPE,+ user_id=user.id,+ )+ .on_conflict_do_nothing()+ .returning(Client.id))- session.add(client)await session.commit()- logger.info("Created member client %s for user %s", client.id, user.id)+ if created is not None:+ logger.info(+ "Created member client %s for user %s", created, user.id+ )+ client = await self.member_client(session, user.id)+ if client is None:+ raise RuntimeError(+ f"No member client for user {user.id} after creating one"+ )return clientasync def has_tenant_role(diff --git a/core/tests/switch_core/gateway/test_chats_groups.py b/core/tests/switch_core/gateway/test_chats_groups.pyindex de5331cee..6ddeddaeb 100644--- a/core/tests/switch_core/gateway/test_chats_groups.py+++ b/core/tests/switch_core/gateway/test_chats_groups.py@@ -1368,3 +1368,30 @@ async def test_only_a_manager_renames_a_group_and_everyone_sees_it("Launch crew",)await events.stream.aclose() # type: ignore[attr-defined]+++async def test_two_processes_creating_one_persons_identity_share_it(+ chats: _Harness, monkeypatch: pytest.MonkeyPatch+) -> None:+ other = _second_process(chats)+ both_looked = asyncio.Barrier(2)+ for service in (chats.service, other):+ look = service.member_client++ async def look_then_wait(+ session: AsyncSession, user_id: str, look: Any = look+ ) -> Client | None:+ found = await look(session, user_id)+ # Both find none before either creates it.+ if found is None:+ await both_looked.wait()+ return found++ monkeypatch.setattr(service, "member_client", look_then_wait)++ tenant = "00000000-0000-0000-0000-000000000000"+ first, second = await asyncio.gather(+ chats.service.member_actor(tenant, chats.bob),+ other.member_actor(tenant, chats.bob),+ )+ assert first.id == second.id