Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Report the workspace the operator entered as the session's

Petr Bauch3cfaa8f394c8
diff --git a/core/switch_core/gateway/auth.py b/core/switch_core/gateway/auth.py
index cbf4a3379..45edceec2 100644
--- a/core/switch_core/gateway/auth.py
+++ b/core/switch_core/gateway/auth.py
@@ -524,6 +524,35 @@ async def _operator_may_enter(
return await session.get(Tenant, tenant_id) is not None
+async def operator_entered_workspace(
+ session_factory: async_sessionmaker[AsyncSession],
+ user: User,
+ tenant_claim: str | None,
+ tenants: list[TenantMembershipResponse],
+) -> TenantMembershipResponse | None:
+ """The workspace the platform operator's claim names, when it is none of
+ their own and exists: one they entered on the operator flag alone, which
+ `_resolve_tenant_id` binds. None for anyone else."""
+ if (
+ user.role != "admin"
+ or tenant_claim is None
+ or any(t.id == tenant_claim for t in tenants)
+ ):
+ return None
+ async with tenant_session(session_factory, tenant_claim) as session:
+ tenant = await session.get(Tenant, tenant_claim)
+ if tenant is None:
+ return None
+ return TenantMembershipResponse(
+ id=tenant.id,
+ slug=tenant.slug,
+ name=tenant.name,
+ role="admin",
+ organization_role=None,
+ organization_id=tenant.organization_id,
+ )
+
+
async def record_operator_entry(
session_factory: async_sessionmaker[AsyncSession],
user_store: UserStore,
@@ -726,19 +755,24 @@ def describe_session_state(
user: User,
tenant_claim: str | None,
tenants: list[TenantMembershipResponse],
+ operator_entered: TenantMembershipResponse | None,
tenants_isolated: bool,
) -> SessionStateResponse:
"""What `_resolve_tenant_id` would make of this session, as data.
Follows its order exactly — a claim is honoured only if it names one of
- `tenants`, and a bad claim is *not* rescued by a sole membership — so a
- client told "ready" is never refused by the next request, and one told
- otherwise knows which of `/tenants/{id}/switch`, `POST /organizations` or
- `/invitations/accept` gets it unstuck.
+ `tenants`, or is the workspace the platform operator entered
+ (`operator_entered`, which `_resolve_tenant_id` binds on
+ `_operator_may_enter`), and a bad claim is *not* rescued by a sole
+ membership — so a client told "ready" is never refused by the next
+ request, and one told otherwise knows which of `/tenants/{id}/switch`,
+ `POST /organizations` or `/invitations/accept` gets it unstuck.
"""
tenant: TenantMembershipResponse | None = None
if tenant_claim is not None:
tenant = next((t for t in tenants if t.id == tenant_claim), None)
+ if tenant is None and operator_entered is not None:
+ tenant = operator_entered if operator_entered.id == tenant_claim else None
elif len(tenants) == 1:
tenant = tenants[0]
diff --git a/core/switch_core/gateway/auth_routes.py b/core/switch_core/gateway/auth_routes.py
index 7251786d4..9f7409d81 100644
--- a/core/switch_core/gateway/auth_routes.py
+++ b/core/switch_core/gateway/auth_routes.py
@@ -42,6 +42,7 @@ from switch_core.gateway.auth import (
hash_password_off_loop,
initial_tenant_claim,
list_tenant_memberships,
+ operator_entered_workspace,
require_admin,
set_session_cookie,
verify_password_off_loop,
@@ -366,8 +367,16 @@ async def session_state(
# One connection at a time: release this one before the lookup opens its own.
await session.commit()
tenants = await list_tenant_memberships(session_factory, user_store, user.id)
+ entered = await operator_entered_workspace(
+ session_factory, user, auth.tenant_claim, tenants
+ )
return describe_session_state(
- config, user, auth.tenant_claim, tenants, client_lifecycle.tenants_isolated
+ config,
+ user,
+ auth.tenant_claim,
+ tenants,
+ entered,
+ client_lifecycle.tenants_isolated,
)
diff --git a/core/tests/switch_core/gateway/test_tenant_resolution.py b/core/tests/switch_core/gateway/test_tenant_resolution.py
index 23a3a1248..cb07244ed 100644
--- a/core/tests/switch_core/gateway/test_tenant_resolution.py
+++ b/core/tests/switch_core/gateway/test_tenant_resolution.py
@@ -37,6 +37,7 @@ paper over here.
from __future__ import annotations
import asyncio
+import uuid
from collections.abc import AsyncIterator
from types import SimpleNamespace
from typing import Annotated
@@ -468,6 +469,56 @@ class TestSessionStateEndpoint:
assert body["state"] == "needs_selection"
assert body["tenant"] is None
+ async def test_the_workspace_the_operator_entered_is_ready(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ """Tenant resolution lets the platform operator into any workspace that
+ exists, so the one they entered is where the session is, not a reason
+ to pick again."""
+ operator_id = await _make_user(
+ session_factory, name="operator", tenant_id=TENANT_ZERO_ID
+ )
+ entered = f"tenant-{uuid.uuid4().hex[:8]}"
+ async with session_factory() as session:
+ operator = await session.get(User, operator_id)
+ assert operator is not None
+ operator.role = "admin"
+ session.add(Tenant(id=entered, slug=entered, name="Elsewhere"))
+ await session.commit()
+ token = create_jwt(
+ operator_id, "[email protected]", "admin", _KEYRING, entered
+ )
+
+ body = (
+ await self._get(session_factory, token, signup_mode="default_tenant")
+ ).json()
+
+ assert body["state"] == "ready"
+ assert body["tenant"]["id"] == entered
+ assert body["tenant"]["name"] == "Elsewhere"
+ assert {t["id"] for t in body["tenants"]} == {TENANT_ZERO_ID}
+
+ async def test_a_claim_naming_no_workspace_is_not_rescued_for_the_operator(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ operator_id = await _make_user(
+ session_factory, name="operator-stale", tenant_id=TENANT_ZERO_ID
+ )
+ async with session_factory() as session:
+ operator = await session.get(User, operator_id)
+ assert operator is not None
+ operator.role = "admin"
+ await session.commit()
+ token = create_jwt(
+ operator_id, "[email protected]", "admin", _KEYRING, "gone"
+ )
+
+ body = (
+ await self._get(session_factory, token, signup_mode="default_tenant")
+ ).json()
+
+ assert body["state"] == "needs_selection"
+
async def test_no_membership_needs_a_workspace(
self, session_factory: async_sessionmaker[AsyncSession]
) -> None: