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: remove a bridge or agent and its clients in one transaction (CHOO-1492) (#525)

Christian McDermott6fb84205ca43
diff --git a/core/switch_core/bridges/agent/protocol/service.py b/core/switch_core/bridges/agent/protocol/service.py
index c3c60c99a..71a9fd7ac 100644
--- a/core/switch_core/bridges/agent/protocol/service.py
+++ b/core/switch_core/bridges/agent/protocol/service.py
@@ -953,13 +953,14 @@ class ProtocolService:
await self._remove_bridge_identities(tenant_id, resolved_name)
+ # One transaction for the agent and its client, stopped above; see
+ # `ClientLifecycleService.delete_record`.
async with self.session_factory() as session:
await self.agent_store.delete(session, resolved_id)
+ await self.client_lifecycle.delete_record(session, client_id)
await session.commit()
self.api_key_cache.invalidate_agent(resolved_id)
- await self.client_lifecycle.remove(client_id)
-
emit_safely(self.telemetry, "agent_deleted", removed)
async def _room_count_for(self, agent_id: str) -> int:
diff --git a/core/switch_core/bridges/collaboration/lifecycle_service.py b/core/switch_core/bridges/collaboration/lifecycle_service.py
index bb68e0d12..9c4a50936 100644
--- a/core/switch_core/bridges/collaboration/lifecycle_service.py
+++ b/core/switch_core/bridges/collaboration/lifecycle_service.py
@@ -888,9 +888,10 @@ class CollaborationBridgeLifecycleService:
so an operator who disconnects an app and reconnects one named the same
collided with the row left by the last one.
- Order matters: the clients are children of nothing but the bridge and
- external-user rows point at them, so those go first or the foreign keys
- refuse.
+ One transaction, all or nothing. The clients are children of nothing
+ but the bridge, and external-user rows point at them, so within it the
+ dependents go before the clients or the foreign keys refuse. The
+ clients are stopped before it opens; see `delete_record` for why.
"""
# The durable record, not `self._connected`: that is empty until a
# connect succeeds *in this process*, so a connector that worked for
@@ -901,6 +902,15 @@ class CollaborationBridgeLifecycleService:
await self.stop(bridge_id)
async with self._session_factory() as session:
bridge = await self._bridge_store.get(session, bridge_id)
+ puppets = await self._external_user_store.get_by_bridge(session, bridge_id)
+
+ removed = [u.client_id for u in puppets if u.client_id]
+ if bridge is not None:
+ removed.append(bridge.client_id)
+ for client_id in removed:
+ await self._client_lifecycle.stop(client_id)
+
+ async with self._session_factory() as session:
dependent_rooms = await self._room_store.get_by_bridge(session, bridge_id)
room_count = len(dependent_rooms)
if dependent_rooms:
@@ -913,18 +923,12 @@ class CollaborationBridgeLifecycleService:
)
for room in dependent_rooms:
await self._room_store.clear_bridge(session, room.id)
- puppets = await self._external_user_store.get_by_bridge(session, bridge_id)
- puppet_client_ids = [u.client_id for u in puppets if u.client_id]
await self._external_user_store.delete_by_bridge(session, bridge_id)
await self._bridge_store.delete(session, bridge_id)
+ for client_id in removed:
+ await self._client_lifecycle.delete_record(session, client_id)
await session.commit()
- removed = list(puppet_client_ids)
- if bridge is not None:
- removed.append(bridge.client_id)
- for client_id in removed:
- await self._client_lifecycle.remove(client_id)
-
# The Matrix accounts themselves outlive this: the homeserver offers no
# deprovisioning call Switch can make. Said out loud because it is the
# reason a reconnection cannot reuse the old name — see
diff --git a/core/switch_core/clients/client_lifecycle_service.py b/core/switch_core/clients/client_lifecycle_service.py
index 1a3148296..d1d841066 100644
--- a/core/switch_core/clients/client_lifecycle_service.py
+++ b/core/switch_core/clients/client_lifecycle_service.py
@@ -258,12 +258,22 @@ class ClientLifecycleService:
self._client_tenants.clear()
self._tasks.clear()
- async def remove(self, client_id: str) -> None:
- await self.stop(client_id)
- async with self._session_factory() as session:
- await self._client_store.delete(session, client_id)
- await session.commit()
- logger.info("Removed client %s", client_id)
+ async def delete_record(self, session: AsyncSession, client_id: str) -> None:
+ """Delete a client's row in the caller's transaction, committing nothing.
+
+ The counterpart to `stop`, and separate from it on purpose. A client
+ row is only ever deleted alongside whatever owned the client — a
+ bridge and the puppets it minted, an agent — and those rows have to go
+ in one transaction or not at all: the owner's delete commits first
+ otherwise, and a failure after it leaves clients nothing points at and
+ nothing will retry.
+
+ Stopping the running client is the half that cannot join a
+ transaction, so callers do that first. A rollback then leaves a
+ stopped client whose row survives, which the next start repairs — the
+ opposite order leaves an orphan row that nothing repairs.
+ """
+ await self._client_store.delete(session, client_id)
def get(self, client_id: str) -> ClientBase[ClientConfig] | None:
return self._clients.get(client_id)
diff --git a/core/switch_core/db/stores/agent_store.py b/core/switch_core/db/stores/agent_store.py
index 29d0db7a0..3719d9e9a 100644
--- a/core/switch_core/db/stores/agent_store.py
+++ b/core/switch_core/db/stores/agent_store.py
@@ -3,7 +3,6 @@ from sqlalchemy.ext.asyncio import AsyncSession
from switch_core.db.models import (
Agent,
- ClientRoom,
Model,
Tool,
require_tenant_id,
@@ -128,6 +127,12 @@ class AgentStore:
await session.flush()
async def delete(self, session: AsyncSession, agent_id: str) -> None:
+ """Delete an agent and the rows that belong to it alone.
+
+ Not the agent's client, and not that client's room memberships:
+ `ClientStore.delete` owns both, and `delete_agent` calls it in this
+ same transaction.
+ """
agent = await session.get(Agent, agent_id)
if not agent:
return
@@ -136,9 +141,6 @@ class AgentStore:
await session.execute(
delete(room_agents).where(room_agents.c.agent_id == agent_id)
)
- await session.execute(
- delete(ClientRoom).where(ClientRoom.client_id == agent.client_id)
- )
await session.delete(agent)
await session.flush()
diff --git a/core/switch_core/db/stores/client_store.py b/core/switch_core/db/stores/client_store.py
index fe8e74b74..2105e388e 100644
--- a/core/switch_core/db/stores/client_store.py
+++ b/core/switch_core/db/stores/client_store.py
@@ -1,7 +1,7 @@
-from sqlalchemy import select
+from sqlalchemy import delete, select
from sqlalchemy.ext.asyncio import AsyncSession
-from switch_core.db.models import Client, require_tenant_id
+from switch_core.db.models import Client, ClientRoom, require_tenant_id
class ClientStore:
@@ -45,7 +45,27 @@ class ClientStore:
return client
async def delete(self, session: AsyncSession, client_id: str) -> None:
+ """Delete a client and the room memberships that point at it.
+
+ `client_rooms` carries a composite foreign key on
+ `(tenant_id, client_id)` with no `ON DELETE` rule, so a client that has
+ ever joined a room cannot be deleted while its membership rows stand —
+ Postgres refuses the delete. Memberships are cleared here rather than
+ by each caller: the rows describe where the client was, and once it is
+ gone they describe nothing.
+
+ Memberships only. `agents`, `collaboration_bridges` and
+ `external_users` also reference a client with no `ON DELETE` rule, and
+ those rows are owned by something with its own lifecycle, so deleting
+ them is the caller's to sequence — the client goes last, after whatever
+ claims it. A caller that gets that order wrong gets the same foreign
+ key violation this clears for `client_rooms`.
+ """
client = await session.get(Client, client_id)
- if client:
- await session.delete(client)
- await session.flush()
+ if client is None:
+ return
+ await session.execute(
+ delete(ClientRoom).where(ClientRoom.client_id == client_id)
+ )
+ await session.delete(client)
+ await session.flush()
diff --git a/core/tests/switch_core/bridges/agent/protocol/test_bridge_identity_tenant_scope.py b/core/tests/switch_core/bridges/agent/protocol/test_bridge_identity_tenant_scope.py
index f1bb06cc6..88ca945cf 100644
--- a/core/tests/switch_core/bridges/agent/protocol/test_bridge_identity_tenant_scope.py
+++ b/core/tests/switch_core/bridges/agent/protocol/test_bridge_identity_tenant_scope.py
@@ -146,7 +146,7 @@ class _StoppableClientLifecycle:
async def stop(self, client_id: str) -> None:
self.stopped.append(client_id)
- async def remove(self, client_id: str) -> None:
+ async def delete_record(self, session: AsyncSession, client_id: str) -> None:
self.removed.append(client_id)
diff --git a/core/tests/switch_core/bridges/agent/protocol/test_delete_agent.py b/core/tests/switch_core/bridges/agent/protocol/test_delete_agent.py
new file mode 100644
index 000000000..04d2b823e
--- /dev/null
+++ b/core/tests/switch_core/bridges/agent/protocol/test_delete_agent.py
@@ -0,0 +1,82 @@
+"""`delete_agent` takes the agent's client with it, room memberships and all.
+
+An agent that has been in a room holds two memberships: a `room_agents` row,
+which `AgentStore.delete` clears, and a `client_rooms` row for its client,
+which `ClientStore.delete` clears — reached through
+`ClientLifecycleService.delete_record` in the same transaction as the agent's
+delete. Nothing else clears the second, so this pins the whole chain against a
+real database.
+"""
+
+from __future__ import annotations
+
+import uuid
+
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+
+from switch_core.bridges.agent.protocol.event_buffer import EventBuffer
+from switch_core.db.models import Room
+from switch_core.db.stores.client_store import ClientStore
+from switch_core.db.stores.room_store import RoomStore
+from tests.switch_core.bridges.agent.protocol.registration_harness import (
+ FakeClientLifecycle,
+ make_owner,
+ make_service,
+ register,
+)
+
+
+class _DeletingClientLifecycle(FakeClientLifecycle):
+ """Registration's fake, plus the `stop` and `delete_record` calls
+ `delete_agent` makes. `delete_record` deletes through the real store in the
+ caller's session and commits nothing, as the real service does, so what
+ the test sees is what `delete_agent`'s own transaction left."""
+
+ def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
+ super().__init__(session_factory)
+ self.stopped: list[str] = []
+
+ async def stop(self, client_id: str) -> None:
+ self.stopped.append(client_id)
+
+ async def delete_record(self, session: AsyncSession, client_id: str) -> None:
+ await ClientStore().delete(session, client_id)
+
+
+class TestDeleteAgentInARoom:
+ async def test_the_agent_its_client_and_both_memberships_go(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ svc = make_service(session_factory)
+ lifecycle = _DeletingClientLifecycle(session_factory)
+ svc.client_lifecycle = lifecycle # type: ignore[assignment]
+ svc.event_buffer = EventBuffer()
+
+ owner = await make_owner(session_factory)
+ agent_id = await register(svc, "in-a-room", owner)
+ async with session_factory() as session:
+ agent = await svc.agent_store.get(session, agent_id)
+ assert agent is not None
+ client_id = agent.client_id
+ room = Room(
+ matrix_room_id=f"!{uuid.uuid4().hex[:8]}:test",
+ name="a room",
+ description="somewhere the agent has been",
+ )
+ session.add(room)
+ await session.flush()
+ room_id = room.id
+ await RoomStore().add_agents(session, room_id, [agent_id])
+ await RoomStore().add_client(session, client_id, room_id)
+ await session.commit()
+
+ await svc.delete_agent(agent_id=agent_id)
+
+ assert lifecycle.stopped == [client_id]
+ async with session_factory() as session:
+ assert await svc.agent_store.get(session, agent_id) is None
+ assert await ClientStore().get(session, client_id) is None
+ assert await RoomStore().get_agent_ids(session, room_id) == []
+ assert await RoomStore().get_client_ids(session, room_id) == []
+ # Only the memberships go; the room outlives its member.
+ assert await RoomStore().get(session, room_id) is not None
diff --git a/core/tests/switch_core/bridges/agent/test_api_key_cache.py b/core/tests/switch_core/bridges/agent/test_api_key_cache.py
index ec815ef9e..b2bece810 100644
--- a/core/tests/switch_core/bridges/agent/test_api_key_cache.py
+++ b/core/tests/switch_core/bridges/agent/test_api_key_cache.py
@@ -75,7 +75,7 @@ class _FakeClientLifecycle:
async def stop(self, client_id: str) -> None:
return None
- async def remove(self, client_id: str) -> None:
+ async def delete_record(self, session: AsyncSession, client_id: str) -> None:
return None
diff --git a/core/tests/switch_core/bridges/collaboration/test_lifecycle_remove.py b/core/tests/switch_core/bridges/collaboration/test_lifecycle_remove.py
index f245c31b4..d7c3dd166 100644
--- a/core/tests/switch_core/bridges/collaboration/test_lifecycle_remove.py
+++ b/core/tests/switch_core/bridges/collaboration/test_lifecycle_remove.py
@@ -25,23 +25,49 @@ from switch_core.db.stores.room_store import RoomStore
class _ClientLifecycle:
- """Deletes the row, as the real service does, and remembers what it was
- asked for. The real one also stops a live client; none of these are
- running, and a MagicMock here would let a missing deletion pass."""
+ """Deletes the row in the caller's transaction, as the real service does,
+ and remembers what it was asked for. The real one also stops a live
+ client; none of these are running, and a MagicMock here would let a
+ missing deletion pass.
+
+ `delete_record` deliberately does not commit: the client rows have to join
+ the bridge teardown's transaction, and a fake that committed on its own
+ would pass a removal that commits them separately.
+ """
- def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
- self._session_factory = session_factory
+ def __init__(self) -> None:
self.removed: list[str] = []
+ self.stopped: list[str] = []
+
+ async def stop(self, client_id: str) -> None:
+ self.stopped.append(client_id)
- async def remove(self, client_id: str) -> None:
+ async def delete_record(self, session: AsyncSession, client_id: str) -> None:
self.removed.append(client_id)
- async with self._session_factory() as session:
- await ClientStore().delete(session, client_id)
- await session.commit()
+ await ClientStore().delete(session, client_id)
+
+
+class _FailingClientLifecycle(_ClientLifecycle):
+ """Deletes clients until the nth, which raises.
+
+ Stands in for anything that can go wrong partway through the teardown — a
+ foreign key, a dropped connection — so the transaction boundary can be
+ asserted rather than assumed.
+ """
+
+ def __init__(self, *, fail_on_nth: int) -> None:
+ super().__init__()
+ self._fail_on_nth = fail_on_nth
+
+ async def delete_record(self, session: AsyncSession, client_id: str) -> None:
+ if len(self.removed) + 1 == self._fail_on_nth:
+ raise RuntimeError(f"deleting client {client_id} failed")
+ await super().delete_record(session, client_id)
def _service(
session_factory: async_sessionmaker[AsyncSession],
+ client_lifecycle: _ClientLifecycle,
) -> CollaborationBridgeLifecycleService:
"""Build the service with real stores; mock the deps remove() never touches."""
return CollaborationBridgeLifecycleService(
@@ -52,7 +78,7 @@ def _service(
room_store=RoomStore(),
agent_store=MagicMock(),
client_store=MagicMock(),
- client_lifecycle=_ClientLifecycle(session_factory),
+ client_lifecycle=client_lifecycle,
room_service=MagicMock(),
matrix_admin=MagicMock(),
session_factory=session_factory,
@@ -125,7 +151,7 @@ async def test_remove_detaches_dependent_rooms(
while rooms point at it raised a raw FK error. remove() now detaches the
rooms (non-destructive) before deleting the bridge.
"""
- service = _service(session_factory)
+ service = _service(session_factory, _ClientLifecycle())
async with session_factory() as session:
bridge_id, _ = await _make_bridge(session)
room_id = await _make_bridged_room(session, bridge_id=bridge_id)
@@ -152,7 +178,7 @@ async def test_remove_detaches_dependent_rooms(
async def test_remove_without_dependent_rooms_still_deletes(
session_factory: async_sessionmaker[AsyncSession],
) -> None:
- service = _service(session_factory)
+ service = _service(session_factory, _ClientLifecycle())
async with session_factory() as session:
bridge_id, _ = await _make_bridge(session)
await session.commit()
@@ -174,7 +200,7 @@ async def test_disconnecting_takes_every_identity_switch_made_for_it(
app and reconnecting one named the same hit the leftover row —
`duplicate key value violates unique constraint "clients_matrix_user_id_key"`.
"""
- service = _service(session_factory)
+ service = _service(session_factory, _ClientLifecycle())
async with session_factory() as session:
bridge_id, bridge_client_id = await _make_bridge(session)
_external_user_id, puppet_client_id = await _make_external_user(
@@ -189,12 +215,88 @@ async def test_disconnecting_takes_every_identity_switch_made_for_it(
assert await ClientStore().get(session, puppet_client_id) is None
+async def test_identities_that_were_in_rooms_go_too(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ """The case that actually happens: identities with room memberships.
+
+ Both of these clients have been in a room — the bridge because it carries
+ the channel, the puppet because the person it stands for spoke there — and
+ `client_rooms` references `clients` with no `ON DELETE` rule, so the
+ memberships have to go before the client rows can.
+ """
+ service = _service(session_factory, _ClientLifecycle())
+ async with session_factory() as session:
+ bridge_id, bridge_client_id = await _make_bridge(session)
+ room_id = await _make_bridged_room(session, bridge_id=bridge_id)
+ _external_user_id, puppet_client_id = await _make_external_user(
+ session, bridge_id=bridge_id
+ )
+ await RoomStore().add_client(session, bridge_client_id, room_id)
+ await RoomStore().add_client(session, puppet_client_id, room_id)
+ await session.commit()
+
+ await service.remove(bridge_id)
+
+ async with session_factory() as session:
+ assert await ClientStore().get(session, bridge_client_id) is None
+ assert await ClientStore().get(session, puppet_client_id) is None
+ # The room outlives the connection as an internal-only room, with
+ # nobody left claiming to be a member on the platform's behalf.
+ assert await RoomStore().get(session, room_id) is not None
+ assert await RoomStore().get_client_ids(session, room_id) == []
+
+
+async def test_a_failed_client_delete_leaves_the_bridge_intact(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ """The teardown is one transaction, so a failure part-way undoes all of it.
+
+ If the bridge committed separately from its clients, a raise part-way
+ would leave clients that nothing points at and nothing retries. Rolled
+ back instead, the operator can try again with everything still in place.
+ """
+ lifecycle = _FailingClientLifecycle(fail_on_nth=2)
+ service = _service(session_factory, lifecycle)
+ async with session_factory() as session:
+ bridge_id, bridge_client_id = await _make_bridge(session)
+ room_id = await _make_bridged_room(session, bridge_id=bridge_id)
+ _first_id, first_puppet = await _make_external_user(
+ session, bridge_id=bridge_id
+ )
+ _second_id, second_puppet = await _make_external_user(
+ session, bridge_id=bridge_id
+ )
+ await RoomStore().add_client(session, bridge_client_id, room_id)
+ await RoomStore().add_client(session, first_puppet, room_id)
+ await session.commit()
+
+ with pytest.raises(RuntimeError):
+ await service.remove(bridge_id)
+
+ async with session_factory() as session:
+ # Nothing committed: the bridge, its rooms, its external users and
+ # every one of its clients are as they were.
+ assert await CollaborationBridgeStore().get(session, bridge_id) is not None
+ room = await RoomStore().get(session, room_id)
+ assert room is not None
+ assert room.bridge_id == bridge_id
+ assert await ExternalUserStore().get_by_bridge(session, bridge_id) != []
+ for client_id in (bridge_client_id, first_puppet, second_puppet):
+ assert await ClientStore().get(session, client_id) is not None
+ assert sorted(await RoomStore().get_client_ids(session, room_id)) == sorted(
+ [bridge_client_id, first_puppet]
+ )
+
+
@pytest.mark.asyncio
async def test_an_app_with_nobody_on_it_still_loses_its_own_client(
session_factory: async_sessionmaker[AsyncSession],
) -> None:
# Louis's case exactly: a Telegram connection nobody had messaged yet.
- service = _service(session_factory)
+ service = _service(session_factory, _ClientLifecycle())
async with session_factory() as session:
bridge_id, bridge_client_id = await _make_bridge(session)
await session.commit()
@@ -245,7 +347,7 @@ async def test_a_starting_bridge_is_recorded_in_the_rooms_it_carries(
no rows is in none of them and relays nothing outward — while still
receiving, because inbound posts into a room by id.
"""
- service = _service(session_factory)
+ service = _service(session_factory, _ClientLifecycle())
async with session_factory() as session:
bridge_id, client_id = await _make_bridge(session)
room_id = await _make_bridged_room(session, bridge_id=bridge_id)
diff --git a/core/tests/switch_core/db/stores/test_client_store.py b/core/tests/switch_core/db/stores/test_client_store.py
index 1e97875ad..77a7de14a 100644
--- a/core/tests/switch_core/db/stores/test_client_store.py
+++ b/core/tests/switch_core/db/stores/test_client_store.py
@@ -2,11 +2,14 @@ from __future__ import annotations
import uuid
+import pytest
+from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
-from switch_core.db.models import Client, Tenant
+from switch_core.db.models import Client, Room, Tenant
from switch_core.db.session_scope import tenant_session
from switch_core.db.stores.client_store import ClientStore
+from switch_core.db.stores.room_store import RoomStore
async def _make_client(session: AsyncSession, matrix_user_id: str) -> Client:
@@ -20,6 +23,152 @@ async def _make_client(session: AsyncSession, matrix_user_id: str) -> Client:
return client
+async def _make_room(session: AsyncSession) -> str:
+ room = Room(
+ matrix_room_id=f"!{uuid.uuid4().hex[:8]}:test",
+ name="a room",
+ description="somewhere a client can be a member of",
+ )
+ session.add(room)
+ await session.flush()
+ return room.id
+
+
+class TestDeleteClearsMemberships:
+ """A client that has joined a room must still be deletable.
+
+ `client_rooms` references `clients` with no `ON DELETE` rule, so the
+ membership rows would otherwise hold the client row hostage — and every
+ client worth deleting has them: a bridge puppet has been in every room the
+ person it stands for spoke in.
+ """
+
+ async def test_a_client_that_is_in_a_room_can_be_deleted(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ store = ClientStore()
+ async with session_factory() as session:
+ client_id = (
+ await _make_client(session, f"@member-{uuid.uuid4().hex[:8]}:test")
+ ).id
+ room_id = await _make_room(session)
+ await RoomStore().add_client(session, client_id, room_id)
+ await session.commit()
+
+ async with session_factory() as session:
+ await store.delete(session, client_id)
+ await session.commit()
+
+ async with session_factory() as session:
+ assert await store.get(session, client_id) is None
+ assert await RoomStore().get_client_ids(session, room_id) == []
+ # Only the membership goes. Deleting a client is not a reason to
+ # lose the room — a cascade on the wrong side would take it.
+ assert await RoomStore().get(session, room_id) is not None
+
+ async def test_other_clients_keep_their_memberships(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ store = ClientStore()
+ async with session_factory() as session:
+ going = (
+ await _make_client(session, f"@going-{uuid.uuid4().hex[:8]}:test")
+ ).id
+ staying = (
+ await _make_client(session, f"@staying-{uuid.uuid4().hex[:8]}:test")
+ ).id
+ room_id = await _make_room(session)
+ await RoomStore().add_client(session, going, room_id)
+ await RoomStore().add_client(session, staying, room_id)
+ await session.commit()
+
+ async with session_factory() as session:
+ await store.delete(session, going)
+ await session.commit()
+
+ async with session_factory() as session:
+ assert await RoomStore().get_client_ids(session, room_id) == [staying]
+
+
+class TestDeleteDoesNotReachAcrossTenants:
+ """What keeps the membership clear from being a cross-tenant delete.
+
+ `delete(ClientRoom).where(ClientRoom.client_id == ...)` carries no tenant
+ predicate. Under the runtime role the `FOR ALL` isolation policy narrows
+ it, but these tests connect as the container's superuser, which owns the
+ tables and so is bound by no policy — the same gap `db/tenant_lookup.py`
+ warns about for fan-out reads, where an unfiltered statement acts on every
+ tenant's rows at once.
+
+ It is safe here for a structural reason rather than a policy one, and that
+ reason is what these pin: `clients.id` is a standalone primary key, so one
+ client id belongs to exactly one tenant and a `client_rooms` row carrying
+ it under a second tenant cannot be written in the first place. Both halves
+ are load-bearing — drop the global uniqueness and the predicate needs a
+ tenant of its own.
+ """
+
+ async def test_another_tenants_memberships_are_untouched(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ store = ClientStore()
+ other_tenant = f"tenant-{uuid.uuid4().hex[:8]}"
+
+ async with session_factory() as session:
+ going = (
+ await _make_client(session, f"@going-{uuid.uuid4().hex[:8]}:test")
+ ).id
+ here = await _make_room(session)
+ await RoomStore().add_client(session, going, here)
+ session.add(Tenant(id=other_tenant, slug=other_tenant, name=other_tenant))
+ await session.commit()
+
+ async with tenant_session(session_factory, other_tenant) as other:
+ theirs = (
+ await _make_client(other, f"@theirs-{uuid.uuid4().hex[:8]}:test")
+ ).id
+ elsewhere = await _make_room(other)
+ await RoomStore().add_client(other, theirs, elsewhere)
+ await other.commit()
+
+ async with session_factory() as session:
+ await store.delete(session, going)
+ await session.commit()
+
+ async with tenant_session(session_factory, other_tenant) as verify:
+ assert await store.get(verify, theirs) is not None
+ assert await RoomStore().get_client_ids(verify, elsewhere) == [theirs]
+
+ async def test_a_client_id_belongs_to_exactly_one_tenant(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ """The invariant the unfiltered delete predicate rests on.
+
+ If a client id could be reused under a second tenant, the delete above
+ would take that tenant's memberships with it on any connection no
+ policy narrows.
+ """
+ other_tenant = f"tenant-{uuid.uuid4().hex[:8]}"
+ async with session_factory() as session:
+ client_id = (
+ await _make_client(session, f"@sole-{uuid.uuid4().hex[:8]}:test")
+ ).id
+ session.add(Tenant(id=other_tenant, slug=other_tenant, name=other_tenant))
+ await session.commit()
+
+ with pytest.raises(IntegrityError):
+ async with tenant_session(session_factory, other_tenant) as other:
+ other.add(
+ Client(
+ id=client_id,
+ matrix_user_id=f"@copy-{uuid.uuid4().hex[:8]}:test",
+ display_name="a second tenant reusing the id",
+ type="agent",
+ )
+ )
+ await other.commit()
+
+
class TestGetByMatrixUserIdMultiTenant:
"""`Client.matrix_user_id` is unique per tenant
(`uq_clients_tenant_matrix_user_id`), not globally — two tenants may each