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(clients): give a tenant created after startup a working admin client (CHOO-2623)
Wojciech Wojtyniakd6c91ee3157e
diff --git a/core/switch_core/clients/client_lifecycle_service.py b/core/switch_core/clients/client_lifecycle_service.pyindex 2c254ea91..eedefef4b 100644--- a/core/switch_core/clients/client_lifecycle_service.py+++ b/core/switch_core/clients/client_lifecycle_service.py@@ -10,7 +10,7 @@ from switch_core.clients.agent_client import AgentClientfrom switch_core.clients.client_base import ClientBase, ClientConfigfrom switch_core.clients.client_factory import ClientFactoryfrom switch_core.config import SwitchConfig-from switch_core.db.models import Client+from switch_core.db.models import Client, Tenantfrom switch_core.db.session_scope import unscoped_sessionfrom switch_core.db.stores.client_store import ClientStorefrom switch_core.db.stores.tenant_store import TenantStore@@ -86,6 +86,32 @@ class ClientLifecycleService:localpart=localpart,)+ async def create_tenant(self, name: str, slug: str) -> Tenant:+ """Create a tenant with everything it needs to function, in one call.++ Nothing in the running application ever created a `tenants` row+ before this: only the migration that seeds tenant zero does, so a+ second tenant onboarded so far meant inserting the row directly and+ restarting — the only thing that ever ran `ensure_system_client`'s+ enumeration. Between that insert and the restart the tenant existed+ with no admin client, so its rooms had no admin participant, with+ nothing to repair that short of the next boot.++ This is now the one seam a tenant comes into existence through, so+ that whatever eventually offers tenant creation (there is no such+ endpoint yet — Phase 2's scope) has a single place to call rather+ than a row to insert and a checklist to remember. It reuses+ `ensure_system_client` rather than duplicating its per-type,+ per-tenant provisioning logic: the new tenant is simply the one gap+ that enumeration has not filled yet.+ """+ tenant = Tenant(name=name, slug=slug)+ async with unscoped_session(self._session_factory) as session:+ await self._tenant_store.create(session, tenant)+ await session.commit()+ await self.ensure_system_client("admin")+ return tenant+async def start_all(self) -> None:# Every tenant's clients in one pass at boot, so this read is# unscoped by nature. Nothing is bound around `_start_task`: a clientdiff --git a/core/switch_core/db/stores/tenant_store.py b/core/switch_core/db/stores/tenant_store.pyindex 3f5f3b831..fdeeb6692 100644--- a/core/switch_core/db/stores/tenant_store.py+++ b/core/switch_core/db/stores/tenant_store.py@@ -7,6 +7,17 @@ from switch_core.db.models import Tenantclass TenantStore:+ async def create(self, session: AsyncSession, tenant: Tenant) -> Tenant:+ """Insert a tenant row.++ Called from `ClientLifecycleService.create_tenant`, the one path that+ creates a tenant in the running application and provisions what it+ needs in the same call, rather than a row appearing — today, only by+ direct SQL — with nothing to notice it until the next restart."""+ session.add(tenant)+ await session.flush()+ return tenant+async def get_all_ids(self, session: AsyncSession) -> list[str]:"""Every tenant in the deployment, oldest first.diff --git a/core/tests/switch_core/clients/test_ensure_system_client.py b/core/tests/switch_core/clients/test_ensure_system_client.pyindex 309e6bf13..cf9f79717 100644--- a/core/tests/switch_core/clients/test_ensure_system_client.py+++ b/core/tests/switch_core/clients/test_ensure_system_client.py@@ -120,3 +120,30 @@ async def test_nothing_stays_bound_afterwards(await _make_tenant(session_factory, f"tenant-{uuid.uuid4().hex[:8]}")await _service(session_factory).ensure_system_client("admin")assert current_tenant_id() is None++[email protected]_ambient_tenant+async def test_a_tenant_created_after_startup_gets_a_working_admin_client(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ """Bug 2, arranged the way it happened live: boot runs once, against+ whatever tenants exist then, and a second tenant onboards while the+ process keeps running — no second boot in between.++ Before `create_tenant`, the only way a tenant came into existence was a+ row inserted directly (`_make_tenant`, above, is exactly that), and+ nothing woke `ensure_system_client` up to notice it short of the next+ restart. Routing creation through the service instead means the tenant+ has a working admin client — a real row, addressed by the tenant id this+ call itself minted, not "some client exists somewhere" — by the time this+ returns.+ """+ service = _service(session_factory)+ await service.ensure_system_client("admin") # the boot-time call++ tenant = await service.create_tenant(name="Acme", slug="acme")++ clients = await _admin_clients(session_factory)+ assert sorted(c.tenant_id for c in clients) == sorted([TENANT_ZERO_ID, tenant.id])+ acme_client = next(c for c in clients if c.tenant_id == tenant.id)+ assert acme_client.matrix_user_id == "@switch-admin:test"