Skip to content

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

6.0 KB
Raw Blame
"""The current request's tenant, held in a context variable.

Shaped like :mod:`switch_core.logging_context`: a module-level `ContextVar`,
a bind that returns a token, and an unbind that resets it — the house pattern
for anything that needs to travel with a request/task without being threaded
through every function signature along the way.

This module only holds the value and the plumbing to bind/read/unbind it. It
does not decide *how* a tenant is resolved (that is `gateway/auth.py` for the
JWT cookie and `bridges/agent/auth.py` for the bearer token) and it does not
decide what happens when nothing is bound — callers do, deliberately:

- `db/tenant_session.py`'s `after_begin` hook treats "nothing bound" as
  "do nothing" — a system session (auth resolution, startup seeding, Alembic)
  has no tenant and must not be given one it invented.
- `db/models.py`'s `require_tenant_id` treats "nothing bound" as an error and
  raises, because a scoped row has to land in *some* tenant and guessing
  which is how a write ends up in a real customer's data. It answered tenant
  zero until row-level security landed; that made the guess unsafe rather
  than merely approximate, since `with check` cannot tell a guess apart from
  a write tenant zero actually intended.

Neither of those answers lives here. A `current_tenant_id()` that quietly
substituted a default would make an unbound context indistinguishable from a
bound one, which is exactly the ambiguity the fail-closed design (see
`docs/old/multi-tenancy-phase1-db.md`) depends on not existing.
"""

from __future__ import annotations

from collections.abc import Iterator
from contextlib import contextmanager
from contextvars import ContextVar, Token

_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."""
    return _current_tenant_id.get()


def bind_tenant_id(tenant_id: str) -> Token[str | None]:
    """Bind `tenant_id` for the current context, returning a token to unbind with."""
    return _current_tenant_id.set(tenant_id)


def unbind_tenant_id(token: Token[str | None]) -> None:
    _current_tenant_id.reset(token)


def clear_tenant_id() -> Token[str | None]:
    """Unbind whatever tenant is bound, returning a token to restore it with.

    Distinct from never having bound one only in that it can be undone. What
    the reader sees is the same: `current_tenant_id()` is `None` and the
    `after_begin` hook issues no `set_config`.
    """
    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(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
    being *finalised* rather than resumed — a coroutine dropped while
    suspended and later closed by the garbage collector — and the collector
    runs it in whatever context it happens to be in, not the one the token
    belongs to. `Token.reset` refuses to cross contexts, correctly: restoring
    there would write this scope's tenant into an unrelated one. There is also
    nothing left to restore, since the context the token belongs to is
    unreachable, which is why the frame is being finalised at all.
    """
    finalising = False
    try:
        yield
    except GeneratorExit:
        finalising = True
        raise
    finally:
        if not finalising:
            var.reset(token)


@contextmanager
def tenant_scope(tenant_id: str) -> Iterator[None]:
    """Bind `tenant_id` for the duration of the block."""
    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


@contextmanager
def no_tenant() -> Iterator[None]:
    """Unbind for the duration of the block, whatever was bound going in.

    Two uses, and they are the same idea from both ends:

    - a lookup that must span tenants (which tenant is this room in? which
      rooms is this client a member of?) must not inherit a caller's tenant,
      or it silently narrows to it;
    - a long-lived background task must not inherit the context of whatever
      created it. An `asyncio.Task` snapshots the contextvars of its creator,
      so a bridge restarted from an HTTP request would otherwise run its whole
      life under the requesting user's tenant. Entering this at the top of the
      task body makes the task ambient-free, so every unit of work inside it
      has to bind the tenant of the row it is acting on — and one that forgets
      reads nothing rather than reading the wrong tenant.
    """
    with (
        _held(_current_tenant_id, clear_tenant_id()),
        _held(_current_organization_id, _current_organization_id.set(None)),
    ):
        yield