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
[CHOO-3906] Organizations above workspaces: schema, isolation and creation
Petr Bauch3dbf870505fb
diff --git a/core/switch_core/accounts/deletion.py b/core/switch_core/accounts/deletion.pyindex bababeae8..816eb06e0 100644--- a/core/switch_core/accounts/deletion.py+++ b/core/switch_core/accounts/deletion.py@@ -122,6 +122,7 @@ USER_REFERENCES: dict[tuple[str, str], Handling] = {("workspace_service_settings", "updated_by"): "handed over",("service_app_credentials", "updated_by"): "handed over",("provider_connections", "user_id"): "database cascades",+ ("organization_members", "user_id"): "database cascades",("template_installs", "owner_id"): "database cascades",("external_user_claims", "user_id"): "database cascades",("chat_operations", "user_id"): "database cascades",diff --git a/core/switch_core/clients/client_lifecycle_service.py b/core/switch_core/clients/client_lifecycle_service.pyindex ae8ff56c9..f28c7377f 100644--- a/core/switch_core/clients/client_lifecycle_service.py+++ b/core/switch_core/clients/client_lifecycle_service.py@@ -228,7 +228,7 @@ class ClientLifecycleService:localpart=localpart,)- async def create_tenant(self, name: str, slug: str) -> Tenant:+ async def create_tenant(self, name: str, slug: str, organization_id: str) -> Tenant:"""Create a tenant with everything it needs to function, in one call.Nothing in the running application ever created a `tenants` row@@ -256,6 +256,10 @@ class ClientLifecycleService:the row is written by a session scoped to exactly the tenant beingcreated, and to nothing else. The id is generated here rather than bythe database for that reason.++ The workspace belongs to `organization_id`, which must exist: creating+ the organization, or choosing one the caller administers, is the+ route's business, as authorising is."""if not self._tenants_isolated:raise TenantIsolationNotInForce(@@ -265,7 +269,9 @@ class ClientLifecycleService:"workspace. Connect it as the restricted runtime role before ""creating another.")- tenant = Tenant(id=str(uuid.uuid4()), name=name, slug=slug)+ tenant = Tenant(+ id=str(uuid.uuid4()), name=name, slug=slug, organization_id=organization_id+ )async with tenant_session(self._session_factory, tenant.id) as session:await self._tenant_store.create(session, tenant)await session.commit()diff --git a/core/switch_core/db/models.py b/core/switch_core/db/models.pyindex 70dff1e2c..fccda9c1f 100644--- a/core/switch_core/db/models.py+++ b/core/switch_core/db/models.py@@ -52,7 +52,7 @@ from switch_core.db.session_activity_notify_ddl import (CREATE_SESSION_ACTIVITY_NOTIFY_FUNCTION,)from switch_core.db.tenant_lookup import attach_tenant_lookups-from switch_core.tenant_context import current_tenant_id+from switch_core.tenant_context import current_organization_id, current_tenant_id#: The highest boot number `agent_event_boot` hands out. An agent's own event#: numbers start at the boot number shifted left 32 bits, so they stay below@@ -80,6 +80,65 @@ def _uuid() -> str:# ── Tenants ─────────────────────────────────────────────────────────────────+class Organization(Base):+ """The customer: it owns workspaces, its own admins and, later, its billing.++ Every workspace (`Tenant`) belongs to one organization. Organization-scoped+ tables compare against it (`db/rls_ddl.py`), on `id` here as `tenants`+ does. `workos_organization_id` is the customer's root organization at+ WorkOS where the hosted service links one, and `customer_id` the+ customer's id as WorkOS and billing know it; both are null on a server+ that keeps organizations in its own database. See+ `docs/design/organizations-v1.md`.+ """++ __tablename__ = "organizations"++ id: Mapped[str] = mapped_column(Text, primary_key=True, default=_uuid)+ slug: Mapped[str] = mapped_column(Text, unique=True, nullable=False)+ name: Mapped[str] = mapped_column(Text, nullable=False)+ workos_organization_id: Mapped[str | None] = mapped_column(+ Text, unique=True, nullable=True+ )+ customer_id: Mapped[str | None] = mapped_column(Text, unique=True, nullable=True)+ created_at: Mapped[datetime] = mapped_column(+ DateTime(timezone=True), server_default=func.now(), nullable=False+ )+++class OrganizationMember(Base):+ """A user's membership in an organization, and their role in it.++ `org_admin` administers the organization and every workspace in it;+ `member` is what joining any of its workspaces makes someone. Workspace+ roles stay on `TenantMember`. A checked string, as `tenant_members.role`+ is.+ """++ __tablename__ = "organization_members"+ __table_args__ = (+ CheckConstraint(+ "role IN ('org_admin', 'member')", name="ck_organization_members_role"+ ),+ )++ organization_id: Mapped[str] = mapped_column(+ Text,+ ForeignKey("organizations.id", name="fk_organization_members_organization"),+ primary_key=True,+ )+ # Cascades: someone who has left every workspace of an organization is+ # still its member, and no workspace-bound session reaches that row when+ # their account is deleted.+ user_id: Mapped[str] = mapped_column(+ Text, ForeignKey("users.id", ondelete="CASCADE"), primary_key=True+ )+ role: Mapped[str] = mapped_column(Text, nullable=False)+ created_at: Mapped[datetime] = mapped_column(+ DateTime(timezone=True), server_default=func.now(), nullable=False+ )++class Tenant(Base):"""The top-level customer boundary every scoped table hangs off.@@ -103,6 +162,13 @@ class Tenant(Base):services_on_by_default: Mapped[bool] = mapped_column(Boolean, server_default="false", nullable=False)+ # Nullable until every process writes it: a release from before+ # organizations creates workspaces without one.+ organization_id: Mapped[str | None] = mapped_column(+ Text,+ ForeignKey("organizations.id", name="fk_tenants_organization"),+ nullable=True,+ )class TenantMember(Base):@@ -207,6 +273,51 @@ class TenantScoped:)+class OrganizationNotBoundError(RuntimeError):+ """An organization-scoped row was constructed with no organization bound.++ The organization's counterpart of `TenantNotBoundError`. A write reached+ from a workspace-bound request names its organization explicitly+ (`Tenant.organization_id`) or binds it with `organization_scope`.+ """+++def require_organization_id() -> str:+ """The organization bound to this unit of work, raising when none is.++ The Python-side default on every organization-scoped column, named after+ the SQL function in `db/rls_ddl.py`. Unlike that function it does not+ derive the organization from a bound workspace: that needs a read, and a+ column default is no place for one.+ """+ organization_id = current_organization_id()+ if organization_id is None:+ raise OrganizationNotBoundError(+ "no organization is bound to this session; pass organization_id "+ "explicitly, or bind one with organization_scope before writing"+ )+ return organization_id+++class OrganizationScoped:+ """Mixin carrying the organization column of every per-organization table.++ The foreign key is named for the reason `TenantScoped`'s is.+ """++ @declared_attr+ def organization_id(cls) -> Mapped[str]: # noqa: N805 - SQLAlchemy convention+ return mapped_column(+ Text,+ ForeignKey(+ "organizations.id",+ name=f"fk_{cls.__tablename__}_organization", # type: ignore[attr-defined]+ ),+ nullable=False,+ default=require_organization_id,+ )++# ── Users ──────────────────────────────────────────────────────────────────────diff --git a/core/switch_core/db/rls_ddl.py b/core/switch_core/db/rls_ddl.pyindex 858b49689..bce8cabf0 100644--- a/core/switch_core/db/rls_ddl.py+++ b/core/switch_core/db/rls_ddl.py@@ -91,6 +91,22 @@ merely happens to have a column called `tenant_id` without the constraint isnot tied to a tenant, and a policy comparing it would reject every row thetable holds.+**Organizations are the second boundary, and they hang off the first.** An+organization owns workspaces, and a few tables belong to the organization+rather than to any one workspace: `organizations` itself (compared on `id`,+as `tenants` is) and every table whose `organization_id` foreign key targets+`organizations.id`. Their policy has the same shape over+`require_organization_id()`, which answers from the bound workspace when one+is bound — the organization `tenants.organization_id` names for it — and from+`app.organization_id` only when no workspace is. So a workspace-bound session+sees its own organization's rows and no other's with nothing new to bind, and+binding an organization that is not the workspace's own is refused rather+than quietly widened. The function reads `tenants` under that table's own+policy, so it is not an exemption: it can only ever see the bound workspace's+row. A table is organization-scoped only when it is not tenant-scoped:+`tenants.organization_id` points at `organizations` too, and `tenants` stays+on its own boundary.+**Where the SQL lives.** Same house pattern as `notify_ddl.py`: this module'sDDL is attached to `Base.metadata` (`db/models.py`) so `create_all` builds itand every test exercises the same policies the server would run in@@ -106,6 +122,8 @@ from sqlalchemy import DDL, MetaData, Table, eventREQUIRE_TENANT_FUNCTION_NAME = "require_tenant_id"POLICY_NAME = "tenant_isolation"+REQUIRE_ORGANIZATION_FUNCTION_NAME = "require_organization_id"+ORGANIZATION_POLICY_NAME = "organization_isolation"# The whole exemption from tenancy, written out. Everything else in# `Base.metadata` is scoped, so this list is the only place a table can be@@ -221,18 +239,49 @@ DROP_REQUIRE_TENANT_FUNCTION = (f"DROP FUNCTION IF EXISTS {REQUIRE_TENANT_FUNCTION_NAME}()")+CREATE_REQUIRE_ORGANIZATION_FUNCTION = f"""+CREATE OR REPLACE FUNCTION {REQUIRE_ORGANIZATION_FUNCTION_NAME}() RETURNS text+LANGUAGE plpgsql STABLE AS $$+DECLARE+ bound_tenant text := current_setting('app.tenant_id', true);+ bound_organization text := current_setting('app.organization_id', true);+ tenant_organization text;+BEGIN+ IF bound_tenant IS NOT NULL AND btrim(bound_tenant) <> '' THEN+ SELECT organization_id INTO tenant_organization+ FROM tenants WHERE id = bound_tenant;+ IF tenant_organization IS NULL THEN+ RAISE EXCEPTION 'the workspace bound to this session belongs to no organization'+ USING ERRCODE = '{_TENANT_NOT_SET_SQLSTATE}';+ END IF;+ IF bound_organization IS NOT NULL AND btrim(bound_organization) <> ''+ AND bound_organization <> tenant_organization THEN+ RAISE EXCEPTION 'app.organization_id is not the bound workspace''s organization'+ USING ERRCODE = '{_TENANT_NOT_SET_SQLSTATE}';+ END IF;+ RETURN tenant_organization;+ END IF;+ IF bound_organization IS NULL OR btrim(bound_organization) = '' THEN+ RAISE EXCEPTION 'neither app.organization_id nor app.tenant_id is set on this session'+ USING ERRCODE = '{_TENANT_NOT_SET_SQLSTATE}';+ END IF;+ RETURN bound_organization;+END;+$$+"""++DROP_REQUIRE_ORGANIZATION_FUNCTION = (+ f"DROP FUNCTION IF EXISTS {REQUIRE_ORGANIZATION_FUNCTION_NAME}()"+)+def enable_rls_ddl(table_name: str) -> str:return f'ALTER TABLE "{table_name}" ENABLE ROW LEVEL SECURITY'def create_policy_ddl(table_name: str, tenant_column: str) -> str:- predicate = f'"{tenant_column}" = (SELECT {REQUIRE_TENANT_FUNCTION_NAME}())'- return (- f'CREATE POLICY {POLICY_NAME} ON "{table_name}"\n'- f" FOR ALL\n"- f" USING ({predicate})\n"- f" WITH CHECK ({predicate})"+ return _policy_ddl(+ POLICY_NAME, table_name, tenant_column, REQUIRE_TENANT_FUNCTION_NAME)@@ -240,6 +289,29 @@ def drop_policy_ddl(table_name: str) -> str:return f'DROP POLICY IF EXISTS {POLICY_NAME} ON "{table_name}"'+def create_organization_policy_ddl(table_name: str, organization_column: str) -> str:+ return _policy_ddl(+ ORGANIZATION_POLICY_NAME,+ table_name,+ organization_column,+ REQUIRE_ORGANIZATION_FUNCTION_NAME,+ )+++def drop_organization_policy_ddl(table_name: str) -> str:+ return f'DROP POLICY IF EXISTS {ORGANIZATION_POLICY_NAME} ON "{table_name}"'+++def _policy_ddl(policy: str, table_name: str, column: str, function: str) -> str:+ predicate = f'"{column}" = (SELECT {function}())'+ return (+ f'CREATE POLICY {policy} ON "{table_name}"\n'+ f" FOR ALL\n"+ f" USING ({predicate})\n"+ f" WITH CHECK ({predicate})"+ )++def _tenant_column(table: Table) -> str | None:"""The column this table's policy would compare, or None if it has none.@@ -260,6 +332,39 @@ def _tenant_column(table: Table) -> str | None:return None+def _organization_column(table: Table) -> str | None:+ """The column this table's organization policy would compare, or None.++ `organizations` is compared on `id`, as `tenants` is. Every other table+ qualifies by carrying an `organization_id` column whose foreign key+ targets `organizations.id` — and by not being tenant-scoped, which+ `tenants` is, though its `organization_id` points there too.+ """+ if _tenant_column(table) is not None:+ return None+ if table.name == "organizations":+ return "id"+ organization_id_col = table.columns.get("organization_id")+ if organization_id_col is None:+ return None+ if any(+ fk.column.table.name == "organizations" and fk.column.name == "id"+ for fk in organization_id_col.foreign_keys+ ):+ return "organization_id"+ return None+++def organization_scoped_tables(metadata: MetaData) -> dict[str, str]:+ """Table name -> the column its organization policy compares."""+ return {+ table.name: column+ for table in metadata.tables.values()+ if table.name not in GLOBAL_TABLES+ and (column := _organization_column(table)) is not None+ }++def unscoped_tables(metadata: MetaData) -> list[str]:"""Every table that breaks the rule: not global, and carrying no tenant.@@ -271,12 +376,17 @@ def unscoped_tables(metadata: MetaData) -> list[str]:return sorted(table.namefor table in metadata.tables.values()- if table.name not in GLOBAL_TABLES and _tenant_column(table) is None+ if table.name not in GLOBAL_TABLES+ and _tenant_column(table) is None+ and _organization_column(table) is None)def scoped_tables(metadata: MetaData) -> dict[str, str]:- """Table name -> the column its policy compares: everything not global.+ """Table name -> the column its tenant policy compares.++ Everything not global and not organization-scoped+ (`organization_scoped_tables`).Raises `UnscopedTableError` naming any table that is neither in`GLOBAL_TABLES` nor actually carrying a tenant. The failure is the point:@@ -290,15 +400,16 @@ def scoped_tables(metadata: MetaData) -> dict[str, str]:if table.name in GLOBAL_TABLES:continuecolumn = _tenant_column(table)- if column is None:- violations.append(table.name)- else:+ if column is not None:tables[table.name] = column+ elif _organization_column(table) is None:+ violations.append(table.name)if violations:raise UnscopedTableError(f"table(s) carry no tenant and are not global: {sorted(violations)}. "- "Every table in the model metadata is tenant-scoped unless it is "- "named in db/rls_ddl.py's GLOBAL_TABLES. Inherit TenantScoped (or, "+ "Every table in the model metadata is tenant-scoped, or "+ "organization-scoped (OrganizationScoped), unless it is named in "+ "db/rls_ddl.py's GLOBAL_TABLES. Inherit TenantScoped (or, ""for a plain Table(), add the equivalent tenant_id column with a ""foreign key to tenants.id), or — if the table genuinely holds no ""customer data — add it to GLOBAL_TABLES, which is a decision "@@ -340,6 +451,37 @@ def attach_row_level_security(metadata: MetaData) -> None:"before_drop",DDL(drop_policy_ddl(table_name)).execute_if(dialect="postgresql"),)+ event.listen(+ metadata,+ "before_create",+ DDL(CREATE_REQUIRE_ORGANIZATION_FUNCTION).execute_if(dialect="postgresql"),+ )+ for table_name, column in organization_scoped_tables(metadata).items():+ table = metadata.tables[table_name]+ event.listen(+ table,+ "after_create",+ DDL(enable_rls_ddl(table_name)).execute_if(dialect="postgresql"),+ )+ event.listen(+ table,+ "after_create",+ DDL(create_organization_policy_ddl(table_name, column)).execute_if(+ dialect="postgresql"+ ),+ )+ event.listen(+ table,+ "before_drop",+ DDL(drop_organization_policy_ddl(table_name)).execute_if(+ dialect="postgresql"+ ),+ )+ event.listen(+ metadata,+ "after_drop",+ DDL(DROP_REQUIRE_ORGANIZATION_FUNCTION).execute_if(dialect="postgresql"),+ )event.listen(metadata,"after_drop",diff --git a/core/switch_core/db/session_scope.py b/core/switch_core/db/session_scope.pyindex f0ff7bca2..501e9b2a4 100644--- a/core/switch_core/db/session_scope.py+++ b/core/switch_core/db/session_scope.py@@ -62,7 +62,7 @@ from dataclasses import dataclassfrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker-from switch_core.tenant_context import tenant_scope+from switch_core.tenant_context import no_tenant, organization_scope, tenant_scope@dataclass(frozen=True)@@ -118,6 +118,21 @@ async def tenant_session(yield session+@asynccontextmanager+async def organization_session(+ session_factory: async_sessionmaker[AsyncSession], organization_id: str+) -> AsyncIterator[AsyncSession]:+ """Open a session bound to `organization_id`, and to no workspace.++ For work on an organization itself: creating it, its members, its+ settings. Any workspace bound around the block is unbound inside it, so+ the session sees exactly this organization's rows and no workspace's.+ """+ with no_tenant(), organization_scope(organization_id):+ async with session_factory() as session:+ yield session++def holds_shared_session(session_factory: async_sessionmaker[AsyncSession], tenant_id: str) -> bool:diff --git a/core/switch_core/db/stores/organization_store.py b/core/switch_core/db/stores/organization_store.pynew file mode 100644index 000000000..daf8bb0a3--- /dev/null+++ b/core/switch_core/db/stores/organization_store.py@@ -0,0 +1,66 @@+from __future__ import annotations++from sqlalchemy import delete+from sqlalchemy.exc import IntegrityError+from sqlalchemy.ext.asyncio import AsyncSession++from switch_core.db.models import Organization, OrganizationMember++# Postgres's default name for `organizations.slug`'s unique constraint.+_SLUG_CONSTRAINT = "organizations_slug_key"+++class OrganizationSlugTaken(Exception):+ """The slug belongs to another organization. Nothing was written."""++ def __init__(self, slug: str) -> None:+ super().__init__(f"Organization slug {slug!r} is taken")+ self.slug = slug+++def _violated_constraint(err: IntegrityError) -> str | None:+ return getattr(getattr(err.orig, "__cause__", None), "constraint_name", None)+++class OrganizationStore:+ """Organizations and their members.++ Every method runs on a session whose organization is bound, explicitly+ (`db/session_scope.organization_session`) or through a bound workspace:+ the policy on both tables (`db/rls_ddl.py`) narrows every read to it and+ refuses every write outside it.+ """++ async def create(+ self, session: AsyncSession, organization: Organization+ ) -> Organization:+ """Insert `organization`, on a session bound to its own id.++ A taken slug raises `OrganizationSlugTaken`; every other integrity+ error propagates as it is.+ """+ session.add(organization)+ try:+ await session.flush()+ except IntegrityError as err:+ if _violated_constraint(err) == _SLUG_CONSTRAINT:+ raise OrganizationSlugTaken(organization.slug) from err+ raise+ return organization++ async def delete_empty(self, session: AsyncSession, organization_id: str) -> None:+ """Delete an organization that no workspace or member was ever added to.++ Undoes a `create` whose workspace could not be created after it. A+ foreign key refuses it once anything references the organization.+ """+ await session.execute(+ delete(Organization).where(Organization.id == organization_id)+ )++ async def role(+ self, session: AsyncSession, organization_id: str, user_id: str+ ) -> str | None:+ """`user_id`'s role in `organization_id`, or None if not a member."""+ member = await session.get(OrganizationMember, (organization_id, user_id))+ return member.role if member is not None else Nonediff --git a/core/switch_core/db/stores/user_store.py b/core/switch_core/db/stores/user_store.pyindex 0c52befe0..ebe5e3a3c 100644--- a/core/switch_core/db/stores/user_store.py+++ b/core/switch_core/db/stores/user_store.py@@ -11,6 +11,8 @@ from sqlalchemy.ext.asyncio import AsyncSessionfrom switch_core.authz import administers_tenant, owns_tenantfrom switch_core.db.models import (OidcIdentity,+ OrganizationMember,+ Tenant,TenantMember,User,require_tenant_id,@@ -120,14 +122,21 @@ class UserStore:)if existing.first() is not None:return False+ tenant_id = require_tenant_id()session.add(TenantMember(- tenant_id=require_tenant_id(),+ tenant_id=tenant_id,user_id=user.id,role="owner" if user.role == "admin" else "member",))await session.flush()+ await self._join_organization(+ session,+ tenant_id=tenant_id,+ user_id=user.id,+ role="org_admin" if user.role == "admin" else "member",+ )return Trueasync def get(self, session: AsyncSession, user_id: str) -> User | None:@@ -407,7 +416,13 @@ class UserStore:return list(result.scalars().all())async def add_membership(- self, session: AsyncSession, *, tenant_id: str, user_id: str, role: str+ self,+ session: AsyncSession,+ *,+ tenant_id: str,+ user_id: str,+ role: str,+ organization_role: str,) -> TenantMember:"""Insert a `role` membership for `user_id` in `tenant_id`.@@ -420,12 +435,58 @@ class UserStore:checks before calling. Kept as the one place `TenantMember` isconstructed (`tests/switch_core/test_seed_admin_membership.py` pinsthat), so a route never writes one directly.++ Joining a workspace makes `user_id` a member of its organization too,+ as `organization_role` when they are not one already+ (`_join_organization`)."""membership = TenantMember(tenant_id=tenant_id, user_id=user_id, role=role)session.add(membership)await session.flush()+ await self._join_organization(+ session, tenant_id=tenant_id, user_id=user_id, role=organization_role+ )return membership+ async def _join_organization(+ self, session: AsyncSession, *, tenant_id: str, user_id: str, role: str+ ) -> None:+ """Make `user_id` a `role` member of `tenant_id`'s organization, if not one.++ An existing organization membership is left as it is: joining another+ workspace never changes someone's role in the organization. Runs on+ the workspace-bound session that wrote the workspace membership, whose+ organization `require_organization_id()` derives from the workspace,+ so the row passes the organization policy with nothing else bound.++ A workspace with no organization was created by a release from before+ organizations; it gets one from the revision that makes the column+ `NOT NULL`. Until then its members join no organization, and each one+ that would have is logged.+ """+ tenant = await session.get(Tenant, tenant_id)+ if tenant is None:+ raise RuntimeError(f"Workspace {tenant_id} is not visible to this session")+ if tenant.organization_id is None:+ logger.warning(+ "Workspace %s belongs to no organization; user %s joined it "+ "without joining an organization",+ tenant_id,+ user_id,+ )+ return+ existing = await session.get(+ OrganizationMember, (tenant.organization_id, user_id)+ )+ if existing is not None:+ return+ session.add(+ OrganizationMember(+ organization_id=tenant.organization_id, user_id=user_id, role=role+ )+ )+ await session.flush()+async def tenant_role(self, session: AsyncSession, tenant_id: str, user_id: str) -> str | None:diff --git a/core/switch_core/db/tenant_session.py b/core/switch_core/db/tenant_session.pyindex cb41dad8e..683d7c756 100644--- a/core/switch_core/db/tenant_session.py+++ b/core/switch_core/db/tenant_session.py@@ -70,13 +70,16 @@ from sqlalchemy import event, textfrom sqlalchemy.engine import Connectionfrom sqlalchemy.orm import ORMExecuteState, Session, SessionTransaction-from switch_core.tenant_context import current_tenant_id+from switch_core.tenant_context import current_organization_id, current_tenant_id_registered = False# Where the hook records what it actually stamped on the current transaction,# so the drift check below can compare it against what is bound now._STAMPED = "switch_stamped_tenant_id"+# The same for the organization, which is stamped beside the tenant only when+# one is bound explicitly (`tenant_context.organization_scope`).+_STAMPED_ORGANIZATION = "switch_stamped_organization_id"# Set on the session whenever a statement goes out through the ORM, and read# by `TenantCheckedSession.get` to tell "this answer came from the database"@@ -149,16 +152,22 @@ def _set_tenant_on_begin(# to add and no business overwriting it.returntenant_id = current_tenant_id()+ organization_id = current_organization_id()# Recorded even when it is None, because "this transaction was stamped# with nothing" is exactly what the drift check needs to be able to tell# apart from "no transaction has begun yet".session.info[_STAMPED] = tenant_id- if tenant_id is None:- return- connection.execute(- text("SELECT set_config('app.tenant_id', :tenant_id, true)"),- {"tenant_id": tenant_id},- )+ session.info[_STAMPED_ORGANIZATION] = organization_id+ if tenant_id is not None:+ connection.execute(+ text("SELECT set_config('app.tenant_id', :tenant_id, true)"),+ {"tenant_id": tenant_id},+ )+ if organization_id is not None:+ connection.execute(+ text("SELECT set_config('app.organization_id', :organization_id, true)"),+ {"organization_id": organization_id},+ )def _forget_the_stamp(session: Session, transaction: SessionTransaction) -> None:@@ -173,6 +182,7 @@ def _forget_the_stamp(session: Session, transaction: SessionTransaction) -> None"""if transaction.parent is None:session.info.pop(_STAMPED, None)+ session.info.pop(_STAMPED_ORGANIZATION, None)def _refuse_a_binding_that_came_too_late(state: ORMExecuteState) -> None:@@ -203,6 +213,15 @@ def _refuse_a_flush_that_drifted(def _refuse_drift(session: Session) -> None:if _STAMPED not in session.info:return+ stamped_organization = session.info.get(_STAMPED_ORGANIZATION)+ bound_organization = current_organization_id()+ if bound_organization != stamped_organization:+ raise TenantBindingDriftError(+ f"this session's transaction was opened with organization "+ f"{stamped_organization!r} and the organization bound now is "+ f"{bound_organization!r}. Open the session inside the "+ "organization_scope, or commit this transaction before rebinding."+ )stamped = session.info[_STAMPED]bound = current_tenant_id()if bound == stamped:@@ -254,6 +273,44 @@ def _tenant_of(instance: object) -> str | None:return None if tenant_id is None else str(tenant_id)+def _organization_of(instance: object) -> str | None:+ """The organization `instance` belongs to, if its table is organization-scoped.++ The same cases `db/rls_ddl._organization_column` derives the policy from.+ `tenants` carries an `organization_id` too but is tenant-scoped, and is+ left to `_tenant_of`.+ """+ table = getattr(type(instance), "__tablename__", None)+ if table == "organizations":+ identifier = getattr(instance, "id", None)+ return None if identifier is None else str(identifier)+ if table == "tenants":+ return None+ organization_id = getattr(instance, "organization_id", None)+ return None if organization_id is None else str(organization_id)+++def _refuse_a_row_from_another_organization(instance: object) -> None:+ """Raise when `instance` belongs to an organization other than the bound one.++ Only an explicitly bound organization can be compared here. A session+ bound to a workspace alone has its organization derived in the database,+ which an identity-map hit never reaches, and the honest answer for that+ case is the same as for nothing bound: no comparison rather than a guess.+ """+ organization_id = _organization_of(instance)+ if organization_id is None:+ return+ bound = current_organization_id()+ if bound is None or bound == organization_id:+ return+ raise CrossTenantIdentityMapError(+ f"Session.get answered from the identity map with a row of "+ f"organization {organization_id!r} while organization {bound!r} is "+ "bound. Open a fresh session inside the new binding."+ )++def _refuse_a_row_from_another_tenant(instance: object) -> None:"""Raise when `instance` belongs to a tenant other than the bound one.@@ -331,6 +388,7 @@ class TenantCheckedSession(Session):instance = super().get(*args, **kwargs)if instance is not None and not self.info[_ISSUED_A_STATEMENT]:_refuse_a_row_from_another_tenant(instance)+ _refuse_a_row_from_another_organization(instance)return instancedef get_one(self, *args: Any, **kwargs: Any) -> Any:@@ -339,6 +397,7 @@ class TenantCheckedSession(Session):instance = super().get_one(*args, **kwargs)if not self.info[_ISSUED_A_STATEMENT]:_refuse_a_row_from_another_tenant(instance)+ _refuse_a_row_from_another_organization(instance)return instancediff --git a/core/switch_core/gateway/app.py b/core/switch_core/gateway/app.pyindex eaf360fb1..fcc7b3ee9 100644--- a/core/switch_core/gateway/app.py+++ b/core/switch_core/gateway/app.py@@ -29,6 +29,7 @@ from switch_core.db.stores.collaboration_bridge_store import CollaborationBridgefrom switch_core.db.stores.external_user_store import ExternalUserStorefrom switch_core.db.stores.invitation_store import InvitationStorefrom switch_core.db.stores.join_domain_store import JoinDomainStore+from switch_core.db.stores.organization_store import OrganizationStorefrom switch_core.db.stores.room_group_store import RoomGroupStorefrom switch_core.db.stores.room_store import RoomStorefrom switch_core.db.stores.template_store import TemplateStore@@ -100,6 +101,7 @@ def create_gateway_app(api_key_store: ApiKeyStore,invitation_store: InvitationStore,join_domain_store: JoinDomainStore,+ organization_store: OrganizationStore,template_store: TemplateStore,usage_store: UsageStore,budget_store: BudgetStore,@@ -127,6 +129,7 @@ def create_gateway_app(api_key_store=api_key_store,invitation_store=invitation_store,join_domain_store=join_domain_store,+ organization_store=organization_store,template_store=template_store,usage_store=usage_store,budget_store=budget_store,diff --git a/core/switch_core/gateway/dependencies.py b/core/switch_core/gateway/dependencies.pyindex 0502d1024..bee924f83 100644--- a/core/switch_core/gateway/dependencies.py+++ b/core/switch_core/gateway/dependencies.py@@ -24,6 +24,7 @@ from switch_core.db.stores.external_user_store import ExternalUserStorefrom switch_core.db.stores.invitation_store import InvitationStorefrom switch_core.db.stores.join_domain_store import JoinDomainStorefrom switch_core.db.stores.messaging_install_store import MessagingInstallStore+from switch_core.db.stores.organization_store import OrganizationStorefrom switch_core.db.stores.room_group_store import RoomGroupStorefrom switch_core.db.stores.room_store import RoomStorefrom switch_core.db.stores.template_store import TemplateStore@@ -54,6 +55,7 @@ def init_dependencies(api_key_store: ApiKeyStore,invitation_store: InvitationStore,join_domain_store: JoinDomainStore,+ organization_store: OrganizationStore,template_store: TemplateStore,usage_store: UsageStore,budget_store: BudgetStore,@@ -78,6 +80,7 @@ def init_dependencies(_state["api_key_store"] = api_key_store_state["invitation_store"] = invitation_store_state["join_domain_store"] = join_domain_store+ _state["organization_store"] = organization_store_state["template_store"] = template_store_state["usage_store"] = usage_store_state["budget_store"] = budget_store@@ -205,6 +208,10 @@ def get_join_domain_store() -> JoinDomainStore:return _state["join_domain_store"] # type: ignore[no-any-return]+def get_organization_store() -> OrganizationStore:+ return _state["organization_store"] # type: ignore[no-any-return]++def get_usage_store() -> UsageStore:return _state["usage_store"] # type: ignore[no-any-return]diff --git a/core/switch_core/gateway/tenants.py b/core/switch_core/gateway/tenants.pyindex 1b13f1513..233eae038 100644--- a/core/switch_core/gateway/tenants.py+++ b/core/switch_core/gateway/tenants.py@@ -4,6 +4,7 @@ import hashlibimport loggingimport reimport secrets+import uuidfrom datetime import UTC, datetime, timedeltafrom typing import Annotated, Literal@@ -28,13 +29,14 @@ from switch_core.connections.broker import (from switch_core.db.audit import AuditAction, list_audit_events, record_audit_eventfrom switch_core.db.models import (Invitation,+ Organization,Tenant,TenantJoinDomain,TenantMember,User,require_tenant_id,)-from switch_core.db.session_scope import tenant_session+from switch_core.db.session_scope import organization_session, tenant_sessionfrom switch_core.db.stores.agent_store import AgentStorefrom switch_core.db.stores.api_key_store import ApiKeyStorefrom switch_core.db.stores.budget_store import (@@ -51,6 +53,10 @@ from switch_core.db.stores.join_domain_store import (JoinDomainNotFound,JoinDomainStore,)+from switch_core.db.stores.organization_store import (+ OrganizationSlugTaken,+ OrganizationStore,+)from switch_core.db.stores.tenant_store import TenantSlugTakenfrom switch_core.db.stores.usage_store import UsageStorefrom switch_core.db.stores.user_store import UserStore@@ -81,6 +87,7 @@ from switch_core.gateway.dependencies import (get_invitation_store,get_invite_mailer,get_join_domain_store,+ get_organization_store,get_protocol,get_session,get_session_factory,@@ -353,6 +360,7 @@ async def _lock_workspace_allowance(async def _provision_workspace(session_factory: async_sessionmaker[AsyncSession],user_store: UserStore,+ organization_store: OrganizationStore,client_lifecycle: ClientLifecycleService,caller: AuthenticatedCaller,name: str,@@ -361,11 +369,26 @@ async def _provision_workspace(tenant: Tenant | None = Nonefor attempt in range(_SLUG_ATTEMPTS):slug = base_slug if attempt == 0 else f"{base_slug}-{secrets.token_hex(2)}"+ organization = Organization(id=str(uuid.uuid4()), name=name, slug=slug)+ try:+ async with organization_session(+ session_factory, organization.id+ ) as session:+ await organization_store.create(session, organization)+ await session.commit()+ except OrganizationSlugTaken:+ continuetry:- tenant = await client_lifecycle.create_tenant(name, slug)+ tenant = await client_lifecycle.create_tenant(name, slug, organization.id)except TenantSlugTaken:+ await _discard_organization(+ session_factory, organization_store, organization.id+ )continueexcept TenantIsolationNotInForce as exc:+ await _discard_organization(+ session_factory, organization_store, organization.id+ )raise HTTPException(status_code=409, detail=str(exc)) from excbreakif tenant is None:@@ -382,7 +405,11 @@ async def _provision_workspace(try:async with tenant_session(session_factory, tenant.id) as session:await user_store.add_membership(- session, tenant_id=tenant.id, user_id=caller.id, role="owner"+ session,+ tenant_id=tenant.id,+ user_id=caller.id,+ role="owner",+ organization_role="org_admin",)await record_audit_event(session,@@ -407,6 +434,17 @@ async def _provision_workspace(return tenant+async def _discard_organization(+ session_factory: async_sessionmaker[AsyncSession],+ organization_store: OrganizationStore,+ organization_id: str,+) -> None:+ """Delete the organization made for a workspace that could not be created."""+ async with organization_session(session_factory, organization_id) as session:+ await organization_store.delete_empty(session, organization_id)+ await session.commit()++@router.post("/tenants", status_code=201)async def create_tenant(req: TenantCreateRequest,@@ -417,12 +455,16 @@ async def create_tenant(async_sessionmaker[AsyncSession], Depends(get_session_factory)],user_store: Annotated[UserStore, Depends(get_user_store)],+ organization_store: Annotated[OrganizationStore, Depends(get_organization_store)],client_lifecycle: Annotated[ClientLifecycleService, Depends(get_client_lifecycle)],config: Annotated[SwitchConfig, Depends(get_config)],) -> TenantMembershipResponse:"""Create a workspace. The caller becomes its `owner`, and their sessionswitches into it.+ The workspace comes with an organization of its own, of the same name,+ whose `org_admin` the caller becomes.+Whether they may is `workspace_creation_refusal`'s decision: sign-up modeand a per-person cap on workspaces created, with operators exempt. It isauthenticated with `get_authenticated_caller` because the caller who@@ -462,12 +504,22 @@ async def create_tenant(# Not held across provisioning, which opens a session per tenant.await session.commit()tenant = await _provision_workspace(- session_factory, user_store, client_lifecycle, caller, req.name+ session_factory,+ user_store,+ organization_store,+ client_lifecycle,+ caller,+ req.name,)else:user = await _lock_workspace_allowance(session, caller, config)tenant = await _provision_workspace(- session_factory, user_store, client_lifecycle, caller, req.name+ session_factory,+ user_store,+ organization_store,+ client_lifecycle,+ caller,+ req.name,)user.workspaces_created += 1await session.commit()@@ -965,7 +1017,11 @@ async def _join_through() from excrole = invitation.roleawait user_store.add_membership(- session, tenant_id=tenant_id, user_id=caller.id, role=role+ session,+ tenant_id=tenant_id,+ user_id=caller.id,+ role=role,+ organization_role="member",)await record_audit_event(session,@@ -1285,7 +1341,11 @@ async def join_tenant_by_domain(if role is None:role = "member"await user_store.add_membership(- session, tenant_id=tenant_id, user_id=caller.id, role=role+ session,+ tenant_id=tenant_id,+ user_id=caller.id,+ role=role,+ organization_role="member",)await record_audit_event(session,diff --git a/core/switch_core/main.py b/core/switch_core/main.pyindex a1025feea..038d868ad 100644--- a/core/switch_core/main.py+++ b/core/switch_core/main.py@@ -152,6 +152,7 @@ from switch_core.db.stores.media_store import MediaStorefrom switch_core.db.stores.message_store import MessageStorefrom switch_core.db.stores.messaging_event_store import MessagingEventReceiptStorefrom switch_core.db.stores.messaging_install_store import MessagingInstallStore+from switch_core.db.stores.organization_store import OrganizationStorefrom switch_core.db.stores.ownership_lease_store import OwnershipLeaseStorefrom switch_core.db.stores.package_store import PackageStorefrom switch_core.db.stores.reference_store import ReferenceStore@@ -729,6 +730,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:api_key_store = ApiKeyStore()invitation_store = InvitationStore()join_domain_store = JoinDomainStore()+ organization_store = OrganizationStore()tenant_store = TenantStore()reference_store = ReferenceStore()reference_type_store = ReferenceTypeStore()@@ -1216,6 +1218,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:api_key_store=api_key_store,invitation_store=invitation_store,join_domain_store=join_domain_store,+ organization_store=organization_store,template_store=template_store,usage_store=usage_store,budget_store=budget_store,diff --git a/core/switch_core/migrations/versions/238c68b7306e_organizations.py b/core/switch_core/migrations/versions/238c68b7306e_organizations.pynew file mode 100644index 000000000..f059229bf--- /dev/null+++ b/core/switch_core/migrations/versions/238c68b7306e_organizations.py@@ -0,0 +1,168 @@+"""organizations: a customer owning workspaces, and its members++Two organization-scoped tables, `organizations` and `organization_members`,+the `require_organization_id()` function their policies compare against, and+a nullable `tenants.organization_id`. Every existing workspace gets an+organization of its own, named and slugged as the workspace is; its owners+and admins become the organization's `org_admin`s and everyone else its+`member`s.++The row-level-security DDL is a verbatim copy of `switch_core/db/rls_ddl.py`+as it stood when this migration was written, copied rather than imported for+the reason `265ed188ad6f` gives.++Expand only: a process from before this revision never reads the new tables+or column. A workspace it creates meanwhile has no organization, which is+why the column is nullable; the revision that makes it `NOT NULL` gives those+workspaces one first.++Revision ID: 238c68b7306e+Revises: e5a9c3d71f08+Create Date: 2026-10-28 00:00:00.000000++"""++from collections.abc import Sequence++import sqlalchemy as sa+from alembic import op++revision: str = "238c68b7306e"+down_revision: str | Sequence[str] | None = "e5a9c3d71f08"+branch_labels: str | Sequence[str] | None = None+depends_on: str | Sequence[str] | None = None+expand_only: bool = True++REQUIRE_ORGANIZATION_FUNCTION_NAME = "require_organization_id"+ORGANIZATION_POLICY_NAME = "organization_isolation"+_SQLSTATE = "42501"++CREATE_REQUIRE_ORGANIZATION_FUNCTION = f"""+CREATE OR REPLACE FUNCTION {REQUIRE_ORGANIZATION_FUNCTION_NAME}() RETURNS text+LANGUAGE plpgsql STABLE AS $$+DECLARE+ bound_tenant text := current_setting('app.tenant_id', true);+ bound_organization text := current_setting('app.organization_id', true);+ tenant_organization text;+BEGIN+ IF bound_tenant IS NOT NULL AND btrim(bound_tenant) <> '' THEN+ SELECT organization_id INTO tenant_organization+ FROM tenants WHERE id = bound_tenant;+ IF tenant_organization IS NULL THEN+ RAISE EXCEPTION 'the workspace bound to this session belongs to no organization'+ USING ERRCODE = '{_SQLSTATE}';+ END IF;+ IF bound_organization IS NOT NULL AND btrim(bound_organization) <> ''+ AND bound_organization <> tenant_organization THEN+ RAISE EXCEPTION 'app.organization_id is not the bound workspace''s organization'+ USING ERRCODE = '{_SQLSTATE}';+ END IF;+ RETURN tenant_organization;+ END IF;+ IF bound_organization IS NULL OR btrim(bound_organization) = '' THEN+ RAISE EXCEPTION 'neither app.organization_id nor app.tenant_id is set on this session'+ USING ERRCODE = '{_SQLSTATE}';+ END IF;+ RETURN bound_organization;+END;+$$+"""+++def _create_policy(table: str, column: str) -> str:+ predicate = f'"{column}" = (SELECT {REQUIRE_ORGANIZATION_FUNCTION_NAME}())'+ return (+ f'CREATE POLICY {ORGANIZATION_POLICY_NAME} ON "{table}"\n'+ f" FOR ALL\n"+ f" USING ({predicate})\n"+ f" WITH CHECK ({predicate})"+ )+++def upgrade() -> None:+ op.execute(CREATE_REQUIRE_ORGANIZATION_FUNCTION)++ op.create_table(+ "organizations",+ sa.Column("id", sa.Text(), nullable=False),+ sa.Column("slug", sa.Text(), nullable=False),+ sa.Column("name", sa.Text(), nullable=False),+ sa.Column("workos_organization_id", sa.Text(), nullable=True),+ sa.Column("customer_id", sa.Text(), nullable=True),+ sa.Column(+ "created_at",+ sa.DateTime(timezone=True),+ server_default=sa.text("now()"),+ nullable=False,+ ),+ sa.PrimaryKeyConstraint("id"),+ sa.UniqueConstraint("slug"),+ sa.UniqueConstraint("workos_organization_id"),+ sa.UniqueConstraint("customer_id"),+ )+ op.execute('ALTER TABLE "organizations" ENABLE ROW LEVEL SECURITY')+ op.execute(_create_policy("organizations", "id"))++ op.create_table(+ "organization_members",+ sa.Column("organization_id", sa.Text(), nullable=False),+ sa.Column("user_id", sa.Text(), nullable=False),+ sa.Column("role", sa.Text(), nullable=False),+ sa.Column(+ "created_at",+ sa.DateTime(timezone=True),+ server_default=sa.text("now()"),+ nullable=False,+ ),+ sa.PrimaryKeyConstraint("organization_id", "user_id"),+ sa.ForeignKeyConstraint(+ ["organization_id"],+ ["organizations.id"],+ name="fk_organization_members_organization",+ ),+ sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),+ sa.CheckConstraint(+ "role IN ('org_admin', 'member')", name="ck_organization_members_role"+ ),+ )+ op.execute('ALTER TABLE "organization_members" ENABLE ROW LEVEL SECURITY')+ op.execute(_create_policy("organization_members", "organization_id"))++ op.add_column("tenants", sa.Column("organization_id", sa.Text(), nullable=True))+ op.create_foreign_key(+ "fk_tenants_organization",+ "tenants",+ "organizations",+ ["organization_id"],+ ["id"],+ )++ # One organization per workspace. Workspace slugs are unique, so the slug+ # carries each organization back to the workspace it was made for.+ op.execute(+ "INSERT INTO organizations (id, slug, name, created_at) "+ "SELECT gen_random_uuid()::text, slug, name, created_at FROM tenants"+ )+ op.execute(+ "UPDATE tenants SET organization_id = organizations.id "+ "FROM organizations WHERE organizations.slug = tenants.slug"+ )+ op.execute(+ "INSERT INTO organization_members (organization_id, user_id, role, created_at) "+ "SELECT tenants.organization_id, tenant_members.user_id, "+ "CASE WHEN tenant_members.role IN ('owner', 'admin') "+ "THEN 'org_admin' ELSE 'member' END, tenant_members.created_at "+ "FROM tenant_members JOIN tenants ON tenants.id = tenant_members.tenant_id"+ )+++def downgrade() -> None:+ op.drop_constraint("fk_tenants_organization", "tenants", type_="foreignkey")+ op.drop_column("tenants", "organization_id")+ op.execute(+ f'DROP POLICY IF EXISTS {ORGANIZATION_POLICY_NAME} ON "organization_members"'+ )+ op.drop_table("organization_members")+ op.execute(f'DROP POLICY IF EXISTS {ORGANIZATION_POLICY_NAME} ON "organizations"')+ op.drop_table("organizations")+ op.execute(f"DROP FUNCTION IF EXISTS {REQUIRE_ORGANIZATION_FUNCTION_NAME}()")diff --git a/core/switch_core/tenant_context.py b/core/switch_core/tenant_context.pyindex 5f0415dbe..c44df5ac7 100644--- a/core/switch_core/tenant_context.py+++ b/core/switch_core/tenant_context.py@@ -36,6 +36,15 @@ _current_tenant_id: ContextVar[str | None] = ContextVar("switch_tenant_id", default=None)+# The organization bound to the current context, for work that acts on an+# organization with no workspace in hand (creating its first workspace, its+# settings). A workspace-bound session needs none: the database derives the+# organization from the bound workspace (`require_organization_id()` in+# `db/rls_ddl.py`), so binding both is allowed only when they agree.+_current_organization_id: ContextVar[str | None] = ContextVar(+ "switch_organization_id", default=None+)+def current_tenant_id() -> str | None:"""The tenant bound to the current context, or `None` if none is."""@@ -61,8 +70,17 @@ def clear_tenant_id() -> Token[str | None]:return _current_tenant_id.set(None)+def current_organization_id() -> str | None:+ """The organization bound to the current context, or `None` if none is.++ `None` while only a workspace is bound: the workspace's organization is+ the database's to derive, not something this context carries.+ """+ return _current_organization_id.get()++@contextmanager-def _held(token: Token[str | None]) -> Iterator[None]:+def _held(var: ContextVar[str | None], token: Token[str | None]) -> Iterator[None]:"""Hold `token` for the block and restore what it replaced on the way out.Not restored when the block is unwound by `GeneratorExit`. That is a frame@@ -82,13 +100,25 @@ def _held(token: Token[str | None]) -> Iterator[None]:raisefinally:if not finalising:- unbind_tenant_id(token)+ var.reset(token)@contextmanagerdef tenant_scope(tenant_id: str) -> Iterator[None]:"""Bind `tenant_id` for the duration of the block."""- with _held(bind_tenant_id(tenant_id)):+ with _held(_current_tenant_id, bind_tenant_id(tenant_id)):+ yield+++@contextmanager+def organization_scope(organization_id: str) -> Iterator[None]:+ """Bind `organization_id` for the duration of the block.++ For work on an organization itself. Inside a `tenant_scope` it must name+ the bound workspace's own organization, or the first statement touching an+ organization's table is refused.+ """+ with _held(_current_organization_id, _current_organization_id.set(organization_id)):yield@@ -109,5 +139,8 @@ def no_tenant() -> Iterator[None]:has to bind the tenant of the row it is acting on — and one that forgetsreads nothing rather than reading the wrong tenant."""- with _held(clear_tenant_id()):+ with (+ _held(_current_tenant_id, clear_tenant_id()),+ _held(_current_organization_id, _current_organization_id.set(None)),+ ):yielddiff --git a/core/tests/conftest.py b/core/tests/conftest.pyindex 179db8e3e..16834ba01 100644--- a/core/tests/conftest.py+++ b/core/tests/conftest.py@@ -31,7 +31,7 @@ from switch_core.bridges.collaboration.platforms import PLATFORMSfrom switch_core.db import encrypted_jsonfrom switch_core.db.base import Basefrom switch_core.db.engine import create_session_factory-from switch_core.db.models import TENANT_ZERO_ID, Tenant+from switch_core.db.models import TENANT_ZERO_ID, Organization, Tenantfrom switch_core.db.runtime_role import grant_runtime_rolefrom switch_core.keys import Keyringfrom switch_core.messages.notify import MessageListener@@ -68,13 +68,23 @@ async def _seed_tenant_zero(conn: AsyncConnection) -> None:"""Insert the one tenant every scoped row's `tenant_id` default points at.Every scoped table's `tenant_id` FK requires a matching `tenants` row, so- this must run right after the schema exists.+ this must run right after the schema exists. It belongs to an+ organization of its own, as the migration gives every existing workspace+ one; the organization reuses the tenant's id."""await conn.execute(- insert(Tenant.__table__).values(+ insert(Organization.__table__).values(id=TENANT_ZERO_ID, slug="default", name="Default"))+ await conn.execute(+ insert(Tenant.__table__).values(+ id=TENANT_ZERO_ID,+ slug="default",+ name="Default",+ organization_id=TENANT_ZERO_ID,+ )+ )# Every table, emptied in one statement. `CASCADE` is what lets a singlediff --git a/core/tests/switch_core/clients/test_ensure_system_client.py b/core/tests/switch_core/clients/test_ensure_system_client.pyindex e39d5fad6..4363ee332 100644--- a/core/tests/switch_core/clients/test_ensure_system_client.py+++ b/core/tests/switch_core/clients/test_ensure_system_client.py@@ -149,7 +149,9 @@ async def test_a_tenant_created_after_startup_gets_a_working_admin_client(service = _service(rls_harness.restricted)await service.ensure_system_client("admin") # the boot-time call- tenant = await service.create_tenant(name="Acme", slug="acme")+ tenant = await service.create_tenant(+ name="Acme", slug="acme", organization_id=TENANT_ZERO_ID+ )clients = await _admin_clients(rls_harness.owner)assert sorted(c.tenant_id for c in clients) == sorted([TENANT_ZERO_ID, tenant.id])diff --git a/core/tests/switch_core/db/test_frozen_ddl_matches_create_all.py b/core/tests/switch_core/db/test_frozen_ddl_matches_create_all.pyindex 7eaf81256..f7aa327d3 100644--- a/core/tests/switch_core/db/test_frozen_ddl_matches_create_all.py+++ b/core/tests/switch_core/db/test_frozen_ddl_matches_create_all.py@@ -52,7 +52,12 @@ from sqlalchemy.ext.asyncio import create_async_engineimport switch_core.db.models # noqa: F401 — registers every table, and the RLS/notify DDL, on Base.metadatafrom switch_core.db.base import Basefrom switch_core.db.notify_ddl import NOTIFY_FUNCTION_NAME, NOTIFY_TRIGGER_NAME-from switch_core.db.rls_ddl import POLICY_NAME, REQUIRE_TENANT_FUNCTION_NAME+from switch_core.db.rls_ddl import (+ ORGANIZATION_POLICY_NAME,+ POLICY_NAME,+ REQUIRE_ORGANIZATION_FUNCTION_NAME,+ REQUIRE_TENANT_FUNCTION_NAME,+)from switch_core.db.session_activity_notify_ddl import (SESSION_ACTIVITY_NOTIFY_FUNCTION,)@@ -92,13 +97,15 @@ def _create_all(connection: Connection) -> None:Base.metadata.create_all(connection)-def _policies(connection: Connection) -> dict[str, tuple[str | None, str | None]]:+def _policies(+ connection: Connection, name: str = POLICY_NAME+) -> dict[str, tuple[str | None, str | None]]:rows = connection.execute(text("SELECT tablename, qual, with_check FROM pg_policies ""WHERE schemaname = 'public' AND policyname = :name"),- {"name": POLICY_NAME},+ {"name": name},)return {row.tablename: (row.qual, row.with_check) for row in rows}@@ -231,8 +238,10 @@ _SESSION_ACTIVITY_TRIGGERS = (@dataclass(frozen=True)class _DDLSnapshot:policies: dict[str, tuple[str | None, str | None]]+ organization_policies: dict[str, tuple[str | None, str | None]]rls_enabled: dict[str, bool]require_tenant_id_body: str+ require_organization_id_body: strtenant_lookups: dict[str, tuple[Any, ...]]security_definer_functions: set[str]notify_function_body: str@@ -244,8 +253,12 @@ class _DDLSnapshot:def _snapshot(connection: Connection) -> _DDLSnapshot:return _DDLSnapshot(policies=_policies(connection),+ organization_policies=_policies(connection, ORGANIZATION_POLICY_NAME),rls_enabled=_rls_enabled(connection),require_tenant_id_body=_function_body(connection, REQUIRE_TENANT_FUNCTION_NAME),+ require_organization_id_body=_function_body(+ connection, REQUIRE_ORGANIZATION_FUNCTION_NAME+ ),tenant_lookups=_tenant_lookups(connection),security_definer_functions=_security_definer_functions(connection),notify_function_body=_function_body(connection, NOTIFY_FUNCTION_NAME),@@ -327,6 +340,20 @@ async def test_migration_ddl_matches_the_live_copy(ddl_parity_urls: Any) -> None"no longer match db/rls_ddl.py's live copy:\n"+ _diff(migrated.policies, live.policies))+ assert live.organization_policies, (+ "create_all did not attach any organization_isolation policies"+ )+ assert migrated.organization_policies == live.organization_policies, (+ "the migration's frozen organization_isolation policies no longer "+ "match db/rls_ddl.py's live copy:\n"+ + _diff(migrated.organization_policies, live.organization_policies)+ )+ assert _without_comments(+ migrated.require_organization_id_body+ ) == _without_comments(live.require_organization_id_body), (+ "the migration's frozen require_organization_id() body no longer "+ "matches db/rls_ddl.py's live copy"+ )assert migrated.rls_enabled == live.rls_enabled, ("row-level security is enabled on a different set of tables in the ""migrated database than db/rls_ddl.py's live copy would enable it on:\n"diff --git a/core/tests/switch_core/db/test_organization_isolation.py b/core/tests/switch_core/db/test_organization_isolation.pynew file mode 100644index 000000000..b29324c5b--- /dev/null+++ b/core/tests/switch_core/db/test_organization_isolation.py@@ -0,0 +1,229 @@+"""Row-level security on the organization boundary, through the restricted role.++`organizations` and `organization_members` are compared against+`require_organization_id()` (`db/rls_ddl.py`), which answers from the bound+workspace when there is one and from an explicitly bound organization when+there is not. Every assertion here goes through `rls_harness.restricted`,+the role the policies apply to; `owner` only arranges fixtures.+"""++from __future__ import annotations++import uuid++import pytest+from sqlalchemy import select+from sqlalchemy.exc import DBAPIError+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.db.models import Organization, OrganizationMember, Tenant, User+from switch_core.db.session_scope import organization_session, tenant_session+from switch_core.db.stores.organization_store import OrganizationStore+from switch_core.db.stores.user_store import UserStore+from switch_core.db.tenant_session import TenantBindingDriftError+from switch_core.tenant_context import organization_scope+from tests.conftest import RLSHarness+++def _id(prefix: str) -> str:+ return f"{prefix}-{uuid.uuid4().hex[:8]}"+++async def _make_organization(+ owner: async_sessionmaker[AsyncSession], *, members: dict[str, str]+) -> tuple[str, str]:+ """An organization with one workspace and `members` (user id -> role)."""+ organization_id, tenant_id = _id("org"), _id("tenant")+ async with owner() as session:+ session.add(Organization(id=organization_id, slug=organization_id, name="O"))+ await session.flush()+ session.add(+ Tenant(+ id=tenant_id,+ slug=tenant_id,+ name="W",+ organization_id=organization_id,+ )+ )+ for user_id, role in members.items():+ if await session.get(User, user_id) is None:+ session.add(+ User(+ id=user_id,+ name=user_id,+ email=f"{user_id}@example.com",+ role="user",+ )+ )+ await session.flush()+ session.add(+ OrganizationMember(+ organization_id=organization_id, user_id=user_id, role=role+ )+ )+ await session.commit()+ return organization_id, tenant_id+++async def _visible_members(session: AsyncSession) -> set[tuple[str, str]]:+ rows = await session.execute(+ select(OrganizationMember.organization_id, OrganizationMember.user_id)+ )+ return {(row.organization_id, row.user_id) for row in rows}+++class TestReads:+ async def test_a_workspace_session_sees_its_own_organization_only(+ self, rls_harness: RLSHarness+ ) -> None:+ org_a, tenant_a = await _make_organization(+ rls_harness.owner, members={"ada": "org_admin"}+ )+ org_b, _ = await _make_organization(+ rls_harness.owner, members={"bob": "org_admin"}+ )++ async with tenant_session(rls_harness.restricted, tenant_a) as session:+ organizations = set((await session.scalars(select(Organization.id))).all())+ members = await _visible_members(session)++ assert organizations == {org_a}+ assert members == {(org_a, "ada")}+ assert org_b not in organizations++ async def test_an_organization_session_sees_that_organization_only(+ self, rls_harness: RLSHarness+ ) -> None:+ org_a, _ = await _make_organization(+ rls_harness.owner, members={"ada": "org_admin"}+ )+ await _make_organization(rls_harness.owner, members={"bob": "member"})++ async with organization_session(rls_harness.restricted, org_a) as session:+ members = await _visible_members(session)++ assert members == {(org_a, "ada")}++ async def test_nothing_bound_is_refused(self, rls_harness: RLSHarness) -> None:+ await _make_organization(rls_harness.owner, members={"ada": "org_admin"})++ async with rls_harness.restricted() as session:+ with pytest.raises(DBAPIError, match="neither app.organization_id"):+ await _visible_members(session)++ async def test_another_organization_inside_a_workspace_is_refused(+ self, rls_harness: RLSHarness+ ) -> None:+ _, tenant_a = await _make_organization(+ rls_harness.owner, members={"ada": "org_admin"}+ )+ org_b, _ = await _make_organization(+ rls_harness.owner, members={"bob": "org_admin"}+ )++ with organization_scope(org_b):+ async with tenant_session(rls_harness.restricted, tenant_a) as session:+ with pytest.raises(DBAPIError, match="not the bound workspace"):+ await _visible_members(session)++ async def test_a_workspace_without_an_organization_is_refused(+ self, rls_harness: RLSHarness+ ) -> None:+ tenant_id = _id("tenant")+ async with rls_harness.owner() as session:+ session.add(Tenant(id=tenant_id, slug=tenant_id, name="Legacy"))+ await session.commit()++ async with tenant_session(rls_harness.restricted, tenant_id) as session:+ with pytest.raises(DBAPIError, match="belongs to no organization"):+ await _visible_members(session)+++class TestWrites:+ async def test_a_member_cannot_be_written_into_another_organization(+ self, rls_harness: RLSHarness+ ) -> None:+ org_a, _ = await _make_organization(+ rls_harness.owner, members={"ada": "org_admin"}+ )+ org_b, _ = await _make_organization(+ rls_harness.owner, members={"bob": "org_admin"}+ )++ async with organization_session(rls_harness.restricted, org_a) as session:+ session.add(+ OrganizationMember(organization_id=org_b, user_id="ada", role="member")+ )+ with pytest.raises(DBAPIError, match="row-level security"):+ await session.flush()++ async def test_an_organization_is_created_under_its_own_binding(+ self, rls_harness: RLSHarness+ ) -> None:+ organization = Organization(id=_id("org"), slug=_id("slug"), name="New")++ async with organization_session(+ rls_harness.restricted, organization.id+ ) as session:+ await OrganizationStore().create(session, organization)+ await session.commit()++ async with organization_session(+ rls_harness.restricted, organization.id+ ) as session:+ found = await session.scalar(select(Organization.name))+ assert found == "New"++ async def test_binding_an_organization_inside_an_open_transaction_is_refused(+ self, rls_harness: RLSHarness+ ) -> None:+ org_a, _ = await _make_organization(+ rls_harness.owner, members={"ada": "org_admin"}+ )++ async with organization_session(rls_harness.restricted, org_a) as session:+ await _visible_members(session)+ with organization_scope(_id("org")):+ with pytest.raises(TenantBindingDriftError):+ await _visible_members(session)+++class TestJoiningAWorkspaceJoinsItsOrganization:+ async def test_a_new_member_joins_as_the_role_given_and_keeps_an_existing_one(+ self, rls_harness: RLSHarness+ ) -> None:+ org_a, tenant_a = await _make_organization(+ rls_harness.owner, members={"ada": "org_admin"}+ )+ async with rls_harness.owner() as session:+ session.add(User(id="cy", name="cy", email="[email protected]", role="user"))+ await session.commit()++ store = UserStore()+ async with tenant_session(rls_harness.restricted, tenant_a) as session:+ await store.add_membership(+ session,+ tenant_id=tenant_a,+ user_id="cy",+ role="admin",+ organization_role="member",+ )+ await store.add_membership(+ session,+ tenant_id=tenant_a,+ user_id="ada",+ role="member",+ organization_role="member",+ )+ await session.commit()++ async with organization_session(rls_harness.restricted, org_a) as session:+ roles = dict(+ (+ await session.execute(+ select(OrganizationMember.user_id, OrganizationMember.role)+ )+ ).all()+ )++ assert roles == {"ada": "org_admin", "cy": "member"}diff --git a/core/tests/switch_core/db/test_row_level_security.py b/core/tests/switch_core/db/test_row_level_security.pyindex d124156e8..7a76022b3 100644--- a/core/tests/switch_core/db/test_row_level_security.py+++ b/core/tests/switch_core/db/test_row_level_security.py@@ -46,6 +46,7 @@ from switch_core.db.rls_ddl import (GLOBAL_TABLES,POLICY_NAME,REQUIRE_TENANT_FUNCTION_NAME,+ organization_scoped_tables,scoped_tables,unscoped_tables,)@@ -372,7 +373,15 @@ class TestCatalogueCoverage:mode a derived list has: silently being short."""assert unscoped_tables(Base.metadata) == []scoped = scoped_tables(Base.metadata)- assert set(scoped) == set(Base.metadata.tables) - set(GLOBAL_TABLES)+ organization_scoped = organization_scoped_tables(Base.metadata)+ assert set(scoped).isdisjoint(organization_scoped)+ assert set(scoped) | set(organization_scoped) == set(+ Base.metadata.tables+ ) - set(GLOBAL_TABLES)+ assert organization_scoped == {+ "organizations": "id",+ "organization_members": "organization_id",+ }assert len(scoped) >= 30, scopedassert scoped["tenants"] == "id"assert scoped["messages"] == "tenant_id"diff --git a/core/tests/switch_core/db/test_tenant_schema_catalogue.py b/core/tests/switch_core/db/test_tenant_schema_catalogue.pyindex a1660190d..ce6639b04 100644--- a/core/tests/switch_core/db/test_tenant_schema_catalogue.py+++ b/core/tests/switch_core/db/test_tenant_schema_catalogue.py@@ -31,7 +31,12 @@ from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession, async_sessionm# junction tables) on `Base.metadata`.import switch_core.db.models # noqa: F401from switch_core.db.base import Base-from switch_core.db.rls_ddl import GLOBAL_TABLES, scoped_tables, unscoped_tables+from switch_core.db.rls_ddl import (+ GLOBAL_TABLES,+ organization_scoped_tables,+ scoped_tables,+ unscoped_tables,+)def _scoped_tables() -> set[str]:@@ -137,7 +142,11 @@ class TestEveryTableIsScopedUnlessItIsNamedGlobal:async def test_scoped_is_everything_else(self) -> None:scoped = _scoped_tables()- assert scoped == set(Base.metadata.tables) - set(GLOBAL_TABLES)+ organization_scoped = set(organization_scoped_tables(Base.metadata))+ assert scoped.isdisjoint(organization_scoped)+ assert scoped | organization_scoped == set(Base.metadata.tables) - set(+ GLOBAL_TABLES+ )# Not vacuous, and the shape is what the design doc describes.assert len(scoped) >= 30, scopedassert {"messages", "agents", "tenants", "tenant_members"} <= scopeddiff --git a/core/tests/switch_core/gateway/conftest.py b/core/tests/switch_core/gateway/conftest.pyindex dd09857cc..2d2b204fb 100644--- a/core/tests/switch_core/gateway/conftest.py+++ b/core/tests/switch_core/gateway/conftest.py@@ -250,6 +250,7 @@ async def _user(session: AsyncSession, name: str, role: str | None) -> User:tenant_id="00000000-0000-0000-0000-000000000000",user_id=user.id,role=role,+ organization_role="org_admin" if role in ("owner", "admin") else "member",)return userdiff --git a/core/tests/switch_core/gateway/test_app_route_mounting.py b/core/tests/switch_core/gateway/test_app_route_mounting.pyindex f9c7a3ed3..d0c3b276e 100644--- a/core/tests/switch_core/gateway/test_app_route_mounting.py+++ b/core/tests/switch_core/gateway/test_app_route_mounting.py@@ -34,6 +34,7 @@ def _app(**overrides: object) -> object:api_key_store=MagicMock(),invitation_store=MagicMock(),join_domain_store=MagicMock(),+ organization_store=MagicMock(),template_store=MagicMock(),usage_store=MagicMock(),budget_store=MagicMock(),diff --git a/core/tests/switch_core/gateway/test_tenant_api_routes.py b/core/tests/switch_core/gateway/test_tenant_api_routes.pyindex b346955b5..03994ffb6 100644--- a/core/tests/switch_core/gateway/test_tenant_api_routes.py+++ b/core/tests/switch_core/gateway/test_tenant_api_routes.py@@ -56,6 +56,7 @@ from switch_core.db.stores.api_key_store import ApiKeyStorefrom switch_core.db.stores.budget_store import BudgetStorefrom switch_core.db.stores.invitation_store import InvitationStorefrom switch_core.db.stores.join_domain_store import JoinDomainStore+from switch_core.db.stores.organization_store import OrganizationStorefrom switch_core.db.stores.service_connection_store import ServiceConnectionStorefrom switch_core.db.stores.tenant_store import TenantStorefrom switch_core.db.stores.usage_store import UsageStore@@ -82,8 +83,10 @@ class _FakeClientLifecycle:def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:self._session_factory = session_factory- async def create_tenant(self, name: str, slug: str) -> Tenant:- tenant = Tenant(id=str(uuid.uuid4()), name=name, slug=slug)+ async def create_tenant(self, name: str, slug: str, organization_id: str) -> Tenant:+ tenant = Tenant(+ id=str(uuid.uuid4()), name=name, slug=slug, organization_id=organization_id+ )async with tenant_session(self._session_factory, tenant.id) as session:await TenantStore().create(session, tenant)await session.commit()@@ -98,9 +101,9 @@ class _ProvisioningFailsLifecycle(_FakeClientLifecycle):super().__init__(session_factory)self.calls = 0- async def create_tenant(self, name: str, slug: str) -> Tenant:+ async def create_tenant(self, name: str, slug: str, organization_id: str) -> Tenant:self.calls += 1- await super().create_tenant(name, slug)+ await super().create_tenant(name, slug, organization_id)raise IntegrityError("INSERT INTO clients ...", None, Exception("duplicate"))@@ -149,6 +152,9 @@ def _app(app.dependency_overrides[gw_deps.get_api_key_store] = lambda: ApiKeyStore()app.dependency_overrides[gw_deps.get_invitation_store] = lambda: InvitationStore()app.dependency_overrides[gw_deps.get_join_domain_store] = lambda: JoinDomainStore()+ app.dependency_overrides[gw_deps.get_organization_store] = lambda: (+ OrganizationStore()+ )app.dependency_overrides[gw_deps.get_usage_store] = lambda: UsageStore()app.dependency_overrides[gw_deps.get_budget_store] = lambda: BudgetStore()app.dependency_overrides[gw_deps.get_protocol] = lambda: _fake_protocol()diff --git a/core/tests/switch_core/test_migration_organizations.py b/core/tests/switch_core/test_migration_organizations.pynew file mode 100644index 000000000..e0a83bc2c--- /dev/null+++ b/core/tests/switch_core/test_migration_organizations.py@@ -0,0 +1,100 @@+"""`238c68b7306e` gives every existing workspace an organization of its own.++Seeds two workspaces, one with an owner, an admin and a member and one with+nobody, stops the chain one revision short, runs it, and reads what it made:+one organization per workspace, named as it is, its owners and admins as+`org_admin` and everyone else as `member`.+"""++from __future__ import annotations++from sqlalchemy import text++from tests.switch_core.test_migration_operator_owns_tenant_zero import (+ _drop_fixture,+ _fixture_engine,+ _upgrade_to,+)++_PRE_MIGRATION_REVISION = "e5a9c3d71f08"+_MIGRATION_UNDER_TEST = "238c68b7306e"+_TENANT_ZERO_ID = "00000000-0000-0000-0000-000000000000"+++async def test_every_workspace_gets_an_organization_and_its_admins_run_it(+ postgres_url: str,+) -> None:+ engine = await _fixture_engine(postgres_url)+ try:+ async with engine.begin() as connection:+ await connection.run_sync(_upgrade_to(_PRE_MIGRATION_REVISION))++ async with engine.begin() as connection:+ for user in ("ada", "bob", "cy"):+ await connection.execute(+ text(+ "INSERT INTO users (id, name, email, role, password_hash, metadata) "+ f"VALUES ('{user}', '{user}', '{user}@example.com', 'user', "+ "'bcrypt-hash', NULL)"+ )+ )+ for user, role in (("ada", "owner"), ("bob", "admin"), ("cy", "member")):+ await connection.execute(+ text(+ "INSERT INTO tenant_members (tenant_id, user_id, role) "+ f"VALUES ('{_TENANT_ZERO_ID}', '{user}', '{role}')"+ )+ )+ await connection.execute(+ text(+ "INSERT INTO tenants (id, slug, name) "+ "VALUES ('empty', 'empty', 'Empty')"+ )+ )++ async with engine.begin() as connection:+ await connection.run_sync(_upgrade_to(_MIGRATION_UNDER_TEST))++ async with engine.connect() as connection:+ workspaces = {+ row.id: (row.slug, row.name)+ for row in await connection.execute(+ text(+ "SELECT tenants.id, organizations.slug, organizations.name "+ "FROM tenants JOIN organizations "+ "ON organizations.id = tenants.organization_id"+ )+ )+ }+ organizations = (+ await connection.execute(text("SELECT count(*) FROM organizations"))+ ).scalar_one()+ members = {+ row.user_id: row.role+ for row in await connection.execute(+ text(+ "SELECT organization_members.user_id, organization_members.role "+ "FROM organization_members JOIN tenants "+ "ON tenants.organization_id = organization_members.organization_id "+ f"WHERE tenants.id = '{_TENANT_ZERO_ID}'"+ )+ )+ }+ empty_members = (+ await connection.execute(+ text(+ "SELECT count(*) FROM organization_members JOIN tenants "+ "ON tenants.organization_id = organization_members.organization_id "+ "WHERE tenants.id = 'empty'"+ )+ )+ ).scalar_one()+ finally:+ await engine.dispose()+ await _drop_fixture(postgres_url)++ assert organizations == 2+ assert workspaces[_TENANT_ZERO_ID][0] == "default"+ assert workspaces["empty"] == ("empty", "Empty")+ assert members == {"ada": "org_admin", "bob": "org_admin", "cy": "member"}+ assert empty_members == 0diff --git a/scripts/e2e_surface.py b/scripts/e2e_surface.pyindex a7c217cfe..1f75eb592 100644--- a/scripts/e2e_surface.py+++ b/scripts/e2e_surface.py@@ -97,6 +97,7 @@ _COLLABORATORS = ("api_key_store","invitation_store","join_domain_store",+ "organization_store","template_store","usage_store","budget_store",