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
Check the last-admin rule before leaving a workspace changes anything
Petr Bauchc6313e93c6bb
diff --git a/core/switch_core/accounts/deletion.py b/core/switch_core/accounts/deletion.pyindex d894a0ff4..e58f3d4a8 100644--- a/core/switch_core/accounts/deletion.py+++ b/core/switch_core/accounts/deletion.py@@ -531,6 +531,12 @@ class AccountDeletion:user_id = user.idtenant_id = workspace.idasync with tenant_session(self.session_factory, tenant_id) as session:+ await self._still_not_the_last_admin(+ session,+ user_id,+ workspace,+ "Nothing of theirs there has changed",+ )agent_ids = [agent.id for agent in await self.agents.get_by_owner(session, user_id)]@@ -567,7 +573,13 @@ class AccountDeletion:member = await self._rename_messages(user_id, tenant_id)async with tenant_session(self.session_factory, tenant_id) as session:- await self._still_not_the_last_admin(session, user_id, workspace)+ await self._still_not_the_last_admin(+ session,+ user_id,+ workspace,+ "Their agents there are already deleted, their chats archived "+ "and their messages renamed",+ )if member is not None:await self._retire_member(session, member, user.name)successor = await self._successor(session, user_id)@@ -744,11 +756,13 @@ class AccountDeletion:)async def _still_not_the_last_admin(- self, session: AsyncSession, user_id: str, workspace: Workspace+ self, session: AsyncSession, user_id: str, workspace: Workspace, done: str) -> None:- """Check `refusal`'s last-admin rule again, in the transaction that- removes the membership and with the admin rows locked: another admin- may have been demoted or removed since `refusal` read them.+ """Check `refusal`'s last-admin rule again, with the admin rows locked:+ another admin may have been demoted or removed since `refusal` read+ them. It runs before anything in the workspace changes, and again in+ the transaction that removes the membership; `done` says, for the+ error, what has changed by then.Their own membership row decides, not `workspace.role`: that is therole they act with, which is `admin` for the organization's@@ -763,8 +777,8 @@ class AccountDeletion:raise RuntimeError(f"Stopped deleting user {user_id} part-way: they became the only "f"admin of workspace {workspace.id}, which has other members, after "- "the deletion was checked. Their agents there are already deleted; "- "make another member an admin and delete the account again."+ f"the deletion was checked. {done}; make another member an admin "+ "and delete the account again.")async def _successor(self, session: AsyncSession, user_id: str) -> str | None:diff --git a/core/tests/switch_core/accounts/test_account_deletion.py b/core/tests/switch_core/accounts/test_account_deletion.pyindex 23e2a6fc1..ea1ae70ef 100644--- a/core/tests/switch_core/accounts/test_account_deletion.py+++ b/core/tests/switch_core/accounts/test_account_deletion.py@@ -22,6 +22,7 @@ from switch_core.accounts import deletion as deletion_modulefrom switch_core.accounts.deletion import (USER_REFERENCES,AccountDeletion,+ AccountDeletionIncomplete,Workspace,)from switch_core.bridges.resource.service import ResourceService@@ -282,6 +283,63 @@ async def test_the_only_admin_of_a_shared_workspace_is_refused(assert reason is not None and "only admin of Acme" in reason+async def test_becoming_the_only_admin_after_the_check_stops_before_anything_changes(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ """The other admin was demoted after `refusal` passed: nothing of the+ person's in the workspace is deleted, archived or renamed."""+ deletion, protocol, _ = _deletion(session_factory)+ async with session_factory() as session:+ leaving = await _user(session, "leaving")+ other = await _user(session, "other")+ await _member(session, TENANT_ZERO_ID, leaving, "admin")+ await _member(session, TENANT_ZERO_ID, other, "member")+ agent = await _agent(session, TENANT_ZERO_ID, leaving, "helper")+ theirs = Client(+ transport_user_id=f"@switch-member-{leaving.id}:test",+ display_name="leaving",+ type="member",+ user_id=leaving.id,+ )+ room = Room(+ transport_room_id=f"!{uuid.uuid4().hex}:test", name="Chat", description=""+ )+ session.add_all([theirs, room])+ await session.flush()+ await MessageStore().create(+ session,+ Message(+ room_id=room.id,+ transport_event_id="$mine",+ sender_id=theirs.transport_user_id,+ sender_client_id=theirs.id,+ sender_name="leaving",+ event_type="m.room.message",+ content={"body": "hi", "sender_name": "leaving"},+ ),+ [],+ )+ await session.commit()+ leaving_id, agent_id = leaving.id, agent.id++ async with session_factory() as session:+ user = await session.get(User, leaving_id)+ assert user is not None+ with pytest.raises(AccountDeletionIncomplete) as raised:+ await deletion.delete(+ session,+ user,+ [Workspace(TENANT_ZERO_ID, "Acme", "admin")],+ actor_id=leaving_id,+ )++ assert "Nothing of theirs there has changed" in str(raised.value.__cause__)+ async with session_factory() as session:+ message = (await session.execute(select(Message))).scalar_one()+ assert message.sender_name == "leaving"+ assert await session.get(Agent, agent_id) is not None++async def test_an_org_admin_with_a_member_row_takes_no_admin_away(session_factory: async_sessionmaker[AsyncSession],) -> None: