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(tests): arrange each tenant's rows on its own session (CHOO-2623)
Wojciech Wojtyniak0434761c0f49
diff --git a/core/tests/switch_core/db/stores/test_agent_store.py b/core/tests/switch_core/db/stores/test_agent_store.pyindex eaac37b96..555e65538 100644--- a/core/tests/switch_core/db/stores/test_agent_store.py+++ b/core/tests/switch_core/db/stores/test_agent_store.py@@ -6,8 +6,8 @@ from sqlalchemy import selectfrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerfrom switch_core.db.models import Agent, ApiKey, Client, Room, Task, Tenant, User+from switch_core.db.session_scope import tenant_sessionfrom switch_core.db.stores.agent_store import AgentStore-from switch_core.tenant_context import tenant_scopeasync def _make_agent(session: AsyncSession, name: str, *, unique: str = "") -> Agent:@@ -49,18 +49,6 @@ async def _make_agent(session: AsyncSession, name: str, *, unique: str = "") ->return agent-async def _make_agent_for_tenant(- session: AsyncSession, tenant_id: str, name: str-) -> Agent:- """An agent named `name`, filed under `tenant_id` rather than the ambient- tenant. The underlying user/api-key/client identifiers stay globally- unique (suffixed with a fresh uuid) regardless of tenant — only `name`- is meant to collide with another tenant's agent here."""- unique = uuid.uuid4().hex[:8]- with tenant_scope(tenant_id):- return await _make_agent(session, name, unique=unique)--async def _make_room(session: AsyncSession, name: str) -> Room:room = Room(matrix_room_id=f"!{name}:test", name=name, description=f"{name} desc")session.add(room)@@ -141,6 +129,14 @@ class TestGetByNameMultiTenant:matched both rows and raised `MultipleResultsFound` out of every callerthat resolves an agent by name (mention routing, registration, thegateway).++ Each tenant gets its own session, opened inside its own binding with+ `tenant_session`, for both the arrangement and the read back. A session's+ transaction is stamped with whatever tenant was bound when it began —+ `set_config` rides `after_begin`, issued once — so entering+ `tenant_scope(other_tenant)` around statements on the session already open+ for tenant zero would rebind the contextvar without moving what Postgres+ was told, and the two would disagree from that statement on."""async def test_get_by_name_returns_the_bound_tenants_agent(@@ -152,20 +148,24 @@ class TestGetByNameMultiTenant:async with session_factory() as session:session.add(Tenant(id=other_tenant, slug=other_tenant, name=other_tenant))await session.flush()- own = await _make_agent(session, "shared-name")- other = await _make_agent_for_tenant(session, other_tenant, "shared-name")+ own_id = (await _make_agent(session, "shared-name")).idawait session.commit()+ async with tenant_session(session_factory, other_tenant) as other_session:+ other_id = (+ await _make_agent(other_session, "shared-name", unique="other")+ ).id+ await other_session.commit()+async with session_factory() as verify:result = await store.get_by_name(verify, "shared-name")- assert result is not None- assert result.id == own.id+ assert result is not None+ assert result.id == own_id- async with session_factory() as verify:- with tenant_scope(other_tenant):- result = await store.get_by_name(verify, "shared-name")- assert result is not None- assert result.id == other.id+ async with tenant_session(session_factory, other_tenant) as verify:+ result = await store.get_by_name(verify, "shared-name")+ assert result is not None+ assert result.id == other_idasync def test_get_by_name_insensitive_returns_the_bound_tenants_agent(self, session_factory: async_sessionmaker[AsyncSession]@@ -176,17 +176,21 @@ class TestGetByNameMultiTenant:async with session_factory() as session:session.add(Tenant(id=other_tenant, slug=other_tenant, name=other_tenant))await session.flush()- own = await _make_agent(session, "SharedName")- other = await _make_agent_for_tenant(session, other_tenant, "sharedname")+ own_id = (await _make_agent(session, "SharedName")).idawait session.commit()+ async with tenant_session(session_factory, other_tenant) as other_session:+ other_id = (+ await _make_agent(other_session, "sharedname", unique="other")+ ).id+ await other_session.commit()+async with session_factory() as verify:result = await store.get_by_name_insensitive(verify, "sharedname")- assert result is not None- assert result.id == own.id+ assert result is not None+ assert result.id == own_id- async with session_factory() as verify:- with tenant_scope(other_tenant):- result = await store.get_by_name_insensitive(verify, "sharedname")- assert result is not None- assert result.id == other.id+ async with tenant_session(session_factory, other_tenant) as verify:+ result = await store.get_by_name_insensitive(verify, "sharedname")+ assert result is not None+ assert result.id == other_iddiff --git a/core/tests/switch_core/db/stores/test_client_store.py b/core/tests/switch_core/db/stores/test_client_store.pyindex 264d00e3a..1e97875ad 100644--- a/core/tests/switch_core/db/stores/test_client_store.py+++ b/core/tests/switch_core/db/stores/test_client_store.py@@ -5,8 +5,8 @@ import uuidfrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerfrom switch_core.db.models import Client, Tenant+from switch_core.db.session_scope import tenant_sessionfrom switch_core.db.stores.client_store import ClientStore-from switch_core.tenant_context import tenant_scopeasync def _make_client(session: AsyncSession, matrix_user_id: str) -> Client:@@ -20,14 +20,6 @@ async def _make_client(session: AsyncSession, matrix_user_id: str) -> Client:return client-async def _make_client_for_tenant(- session: AsyncSession, tenant_id: str, matrix_user_id: str-) -> Client:- """A client filed under `tenant_id` rather than the ambient tenant."""- with tenant_scope(tenant_id):- return await _make_client(session, matrix_user_id)--class TestGetByMatrixUserIdMultiTenant:"""`Client.matrix_user_id` is unique per tenant(`uq_clients_tenant_matrix_user_id`), not globally — two tenants may each@@ -35,6 +27,14 @@ class TestGetByMatrixUserIdMultiTenant:`get_by_matrix_user_id` matched both rows and raised`MultipleResultsFound` out of message routing and provisioning, whichresolve the sending client from an inbound event this way.++ Each tenant gets its own session, opened inside its own binding with+ `tenant_session`, for both the arrangement and the read back. A session's+ transaction is stamped with whatever tenant was bound when it began —+ `set_config` rides `after_begin`, issued once — so entering+ `tenant_scope(other_tenant)` around statements on the session already open+ for tenant zero would rebind the contextvar without moving what Postgres+ was told, and the two would disagree from that statement on."""async def test_returns_the_bound_tenants_client(@@ -47,21 +47,19 @@ class TestGetByMatrixUserIdMultiTenant:async with session_factory() as session:session.add(Tenant(id=other_tenant, slug=other_tenant, name=other_tenant))await session.flush()- own = await _make_client(session, shared_matrix_user_id)- other = await _make_client_for_tenant(- session, other_tenant, shared_matrix_user_id- )+ own_id = (await _make_client(session, shared_matrix_user_id)).idawait session.commit()+ async with tenant_session(session_factory, other_tenant) as other_session:+ other_id = (await _make_client(other_session, shared_matrix_user_id)).id+ await other_session.commit()+async with session_factory() as verify:result = await store.get_by_matrix_user_id(verify, shared_matrix_user_id)- assert result is not None- assert result.id == own.id+ assert result is not None+ assert result.id == own_id- async with session_factory() as verify:- with tenant_scope(other_tenant):- result = await store.get_by_matrix_user_id(- verify, shared_matrix_user_id- )- assert result is not None- assert result.id == other.id+ async with tenant_session(session_factory, other_tenant) as verify:+ result = await store.get_by_matrix_user_id(verify, shared_matrix_user_id)+ assert result is not None+ assert result.id == other_iddiff --git a/core/tests/switch_core/db/stores/test_room_store.py b/core/tests/switch_core/db/stores/test_room_store.pyindex 73c583c77..7e1942c09 100644--- a/core/tests/switch_core/db/stores/test_room_store.py+++ b/core/tests/switch_core/db/stores/test_room_store.py@@ -5,8 +5,8 @@ import uuidfrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerfrom switch_core.db.models import Room, Tenant+from switch_core.db.session_scope import tenant_sessionfrom switch_core.db.stores.room_store import RoomStore-from switch_core.tenant_context import tenant_scopeasync def _make_room(session: AsyncSession, matrix_room_id: str) -> Room:@@ -20,14 +20,6 @@ async def _make_room(session: AsyncSession, matrix_room_id: str) -> Room:return room-async def _make_room_for_tenant(- session: AsyncSession, tenant_id: str, matrix_room_id: str-) -> Room:- """A room filed under `tenant_id` rather than the ambient tenant."""- with tenant_scope(tenant_id):- return await _make_room(session, matrix_room_id)--class TestGetByMatrixRoomIdMultiTenant:"""`Room.matrix_room_id` is unique per tenant(`uq_rooms_tenant_matrix_room_id`), not globally — two tenants may each@@ -35,6 +27,14 @@ class TestGetByMatrixRoomIdMultiTenant:an unfiltered `get_by_matrix_room_id` matched both rows and raised`MultipleResultsFound` out of every inbound-event path that resolves theroom this way (provisioning, the Postgres transport, admin commands).++ Each tenant gets its own session, opened inside its own binding with+ `tenant_session`, for both the arrangement and the read back. A session's+ transaction is stamped with whatever tenant was bound when it began —+ `set_config` rides `after_begin`, issued once — so entering+ `tenant_scope(other_tenant)` around statements on the session already open+ for tenant zero would rebind the contextvar without moving what Postgres+ was told, and the two would disagree from that statement on."""async def test_returns_the_bound_tenants_room(@@ -47,21 +47,19 @@ class TestGetByMatrixRoomIdMultiTenant:async with session_factory() as session:session.add(Tenant(id=other_tenant, slug=other_tenant, name=other_tenant))await session.flush()- own = await _make_room(session, shared_matrix_room_id)- other = await _make_room_for_tenant(- session, other_tenant, shared_matrix_room_id- )+ own_id = (await _make_room(session, shared_matrix_room_id)).idawait session.commit()+ async with tenant_session(session_factory, other_tenant) as other_session:+ other_id = (await _make_room(other_session, shared_matrix_room_id)).id+ await other_session.commit()+async with session_factory() as verify:result = await store.get_by_matrix_room_id(verify, shared_matrix_room_id)- assert result is not None- assert result.id == own.id+ assert result is not None+ assert result.id == own_id- async with session_factory() as verify:- with tenant_scope(other_tenant):- result = await store.get_by_matrix_room_id(- verify, shared_matrix_room_id- )- assert result is not None- assert result.id == other.id+ async with tenant_session(session_factory, other_tenant) as verify:+ result = await store.get_by_matrix_room_id(verify, shared_matrix_room_id)+ assert result is not None+ assert result.id == other_id