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(organizations): service-switch review fixes for #1210
Petr Bauch7141d49c7ace
diff --git a/core/switch_core/connections/broker.py b/core/switch_core/connections/broker.pyindex 8b8487f74..c726e6158 100644--- a/core/switch_core/connections/broker.py+++ b/core/switch_core/connections/broker.py@@ -172,11 +172,13 @@ UnavailableCode = Literal[]-def organization_switched_off(name: str) -> str:- """Why a service is off where the workspace's organization locked it so."""+def organization_locked(name: str, enabled: bool) -> str:+ """Why a service's switch is out of a workspace's hands: its organization+ locked it on or off. What agents and the dashboard both say."""return (- f"Your organization has switched {name} off for its workspaces. An "- "organization admin can change it under Organization, Services."+ f"Your organization has {name} locked {'on' if enabled else 'off'} for its "+ "workspaces. An organization admin can change it under Organization, "+ "Services.")@@ -529,6 +531,11 @@ class ServiceBroker:)return definition, choice.adapter+ def server_switched_off(self, service: str) -> str | None:+ """Why the server switched `service` off for every workspace+ (`DISABLED_SERVICES`), or None where it did not."""+ return self._disabled.get(service)+def _require_switched_on(self, service: str) -> None:reason = self._disabled.get(service)if reason is not None:@@ -554,7 +561,7 @@ class ServiceBroker:raise ServiceError(403,FORBIDDEN,- organization_switched_off(name)+ organization_locked(name, enabled=False)if switch.lockedelse f"{name} is not enabled in this workspace. Ask a workspace admin; ""they switch it on under Connectors in Switch Console.",diff --git a/core/switch_core/db/audit.py b/core/switch_core/db/audit.pyindex fccbd1746..03bd46ad5 100644--- a/core/switch_core/db/audit.py+++ b/core/switch_core/db/audit.py@@ -71,6 +71,7 @@ class AuditAction(StrEnum):AGENT_DELETED = "agent.deleted"WORKSPACE_SERVICE_ENABLED = "workspace_service.enabled"WORKSPACE_SERVICE_DISABLED = "workspace_service.disabled"+ WORKSPACE_SERVICE_CLEARED = "workspace_service.cleared"class OrganizationAuditAction(StrEnum):diff --git a/core/switch_core/db/stores/workspace_service_store.py b/core/switch_core/db/stores/workspace_service_store.pyindex 0c879e24d..e9ad829b0 100644--- a/core/switch_core/db/stores/workspace_service_store.py+++ b/core/switch_core/db/stores/workspace_service_store.py@@ -17,9 +17,10 @@ from __future__ import annotationsfrom collections.abc import Iterablefrom dataclasses import dataclass-from sqlalchemy import func, select+from sqlalchemy import Text, and_, column, delete, func, literal, select, valuesfrom sqlalchemy.dialects.postgresql import insertfrom sqlalchemy.ext.asyncio import AsyncSession+from sqlalchemy.orm import aliasedfrom switch_core.db.models import (OrganizationServiceSetting,@@ -38,6 +39,38 @@ class ServiceSwitch:locked: bool+@dataclass(frozen=True)+class OrganizationSetting:+ """What an organization set for a service: on or off, locked or not."""++ enabled: bool+ locked: bool+++def resolve(+ default: bool, own: bool | None, organization: OrganizationSetting | None+) -> ServiceSwitch:+ """The first of: the organization's setting, if locked; the workspace's+ own; the organization's, unlocked; the workspace's default."""+ if organization is not None and organization.locked:+ return ServiceSwitch(organization.enabled, locked=True)+ if own is not None:+ return ServiceSwitch(own, locked=False)+ if organization is not None:+ return ServiceSwitch(organization.enabled, locked=False)+ return ServiceSwitch(default, locked=False)+++@dataclass(frozen=True)+class _Sides:+ """For one service: the workspace's default, its admin's own switch (None+ if never touched), and its organization's setting (None if it has none)."""++ default: bool+ own: bool | None+ organization: OrganizationSetting | None++class WorkspaceServiceStore:async def on_by_default(self, session: AsyncSession) -> bool:"""Whether a service no admin has switched is on in the workspace."""@@ -49,28 +82,89 @@ class WorkspaceServiceStore:raise LookupError(f"No workspace {tenant_id!r}.")return on+ async def _sides(+ self, session: AsyncSession, services: list[str], *, organizations: bool+ ) -> dict[str, _Sides]:+ """Every side of each of `services`, in one query: it is on the path+ of every token an agent is issued."""+ tenant_id = require_tenant_id()+ if not services:+ return {}+ wanted = values(column("service", Text), name="wanted").data(+ [(service,) for service in services]+ )+ own = aliased(WorkspaceServiceSetting)+ above = aliased(OrganizationServiceSetting)+ query = (+ select(+ wanted.c.service,+ Tenant.services_on_by_default,+ own.enabled,+ above.enabled,+ above.locked,+ )+ .select_from(wanted)+ .join(Tenant, Tenant.id == tenant_id)+ .outerjoin(+ own, and_(own.tenant_id == Tenant.id, own.service == wanted.c.service)+ )+ .outerjoin(+ above,+ and_(+ literal(organizations),+ above.organization_id == Tenant.organization_id,+ above.service == wanted.c.service,+ ),+ )+ )+ sides = {+ service: _Sides(+ default=default,+ own=own_enabled,+ organization=(+ None+ if above_enabled is None+ else OrganizationSetting(above_enabled, above_locked)+ ),+ )+ for service, default, own_enabled, above_enabled, above_locked in (+ await session.execute(query)+ ).tuples()+ }+ if len(sides) != len(set(services)):+ raise LookupError(f"No workspace {tenant_id!r}.")+ return sides+async def switches(self, session: AsyncSession, services: Iterable[str], *, organizations: bool) -> dict[str, ServiceSwitch]:"""Each of `services`, on or off in the workspace, and whether itsorganization locked it."""- default = await self.on_by_default(session)- own = await self.list_settings(session)- organization = (- await self.organization_settings(session) if organizations else {}- )- switches: dict[str, ServiceSwitch] = {}- for service in services:- set_above = organization.get(service)- if set_above is not None and set_above.locked:- switches[service] = ServiceSwitch(set_above.enabled, locked=True)- elif service in own:- switches[service] = ServiceSwitch(own[service].enabled, locked=False)- elif set_above is not None:- switches[service] = ServiceSwitch(set_above.enabled, locked=False)- else:- switches[service] = ServiceSwitch(default, locked=False)- return switches+ return {+ service: resolve(side.default, side.own, side.organization)+ for service, side in (+ await self._sides(session, list(services), organizations=organizations)+ ).items()+ }++ async def switches_under(+ self,+ session: AsyncSession,+ services: Iterable[str],+ organization: dict[str, OrganizationSetting],+ ) -> dict[str, tuple[ServiceSwitch, bool]]:+ """Each of `services` in the workspace were its organization's+ settings `organization`, as one the organization already holds or is+ about to make; and whether the workspace's admin set it themselves."""+ return {+ service: (+ resolve(side.default, side.own, organization.get(service)),+ side.own is not None,+ )+ for service, side in (+ await self._sides(session, list(services), organizations=False)+ ).items()+ }async def disabled_services(self, session: AsyncSession, services: Iterable[str], *, organizations: bool@@ -142,3 +236,14 @@ class WorkspaceServiceStore:},))++ async def clear(self, session: AsyncSession, service: str) -> bool:+ """Drop the workspace admin's own switch, so the organization's+ setting or the workspace's default applies; whether there was one."""+ result = await session.execute(+ delete(WorkspaceServiceSetting).where(+ WorkspaceServiceSetting.tenant_id == require_tenant_id(),+ WorkspaceServiceSetting.service == service,+ )+ )+ return bool(result.rowcount) # type: ignore[attr-defined]diff --git a/core/switch_core/gateway/auth.py b/core/switch_core/gateway/auth.pyindex 8afd30c29..6f4c6162d 100644--- a/core/switch_core/gateway/auth.py+++ b/core/switch_core/gateway/auth.py@@ -403,6 +403,20 @@ async def workspaces_in_organization()+async def organization_workspaces(+ session_factory: async_sessionmaker[AsyncSession],+ admin_id: str,+ organization_id: str,+) -> list[Tenant]:+ """Every workspace of `organization_id`, as its `org_admin` `admin_id`+ reaches them, each read on its own short session."""+ return await _in_organization(+ session_factory,+ await tenants_bindable_by_user(session_factory, admin_id),+ organization_id,+ )++async def organization_workspace_ids(session_factory: async_sessionmaker[AsyncSession],admin_id: str,diff --git a/core/switch_core/gateway/organization_services.py b/core/switch_core/gateway/organization_services.pyindex 927f30641..f971b2c0c 100644--- a/core/switch_core/gateway/organization_services.py+++ b/core/switch_core/gateway/organization_services.py@@ -4,15 +4,23 @@ workspace in it, and lock them there.Whether a service is on in a workspace is the first of(`db/stores/workspace_service_store.py`): the organization's setting, iflocked; the workspace admin's own; the organization's, unlocked; the-workspace's default. So an unlocked setting is a default each workspace may-override, and a locked one is out of every workspace admin's reach: off and-locked is how an organization keeps a service out of all its workspaces.-Clearing the setting gives up the organization's say.+workspace's default. So an unlocked setting reaches only the workspaces+whose admin never set the service themselves, and a locked one is out of+every workspace admin's reach: off and locked is how an organization keeps a+service out of all its workspaces. Clearing the setting gives up the+organization's say.A change that leaves the service off in a workspace withdraws what it was-issued there, as a workspace admin's switch does: tokens are queued for-revocation, revoked before the answer where the vendor allows, and the-warning says what could not be. Grants are kept.+issued there, as a workspace admin's switch does, and must not leave a token+live behind a lock. A workspace's tokens can only be queued from a session+bound to it, so one transaction cannot cover the setting and every+workspace. Instead, every workspace the change would switch off has its+tokens queued first, each in its own transaction, and if any cannot be, the+setting is not saved. Then the setting commits. Then each of those+workspaces is swept again, for a token issued in between, and the vendor+revocations run; a workspace that fails there is logged and named in the+warning, and the rest carry on. A queued token the vendor has not revoked+yet is retried by the periodic tick. Grants are kept.`org_admin` only, acting on the bound workspace's organization. Mounted onlywith both `service_connections` and `organization_connector_apps` on, the@@ -21,12 +29,13 @@ flag the rest of the organization's say in services ships under.from __future__ import annotations+import loggingfrom datetime import datetimefrom typing import Annotated, Anyfrom fastapi import APIRouter, Depends, HTTPExceptionfrom pydantic import BaseModel, ConfigDict-from sqlalchemy import func, select+from sqlalchemy import selectfrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerfrom switch_core.connections.broker import ServiceBroker, get_service_broker@@ -39,14 +48,18 @@ from switch_core.db.models import (OrganizationServiceSetting,ServiceConnection,ServiceTokenIssuance,+ Tenant,User,)from switch_core.db.session_scope import tenant_sessionfrom switch_core.db.stores.organization_service_store import OrganizationServiceStorefrom switch_core.db.stores.organization_store import OrganizationStorefrom switch_core.db.stores.user_store import UserStore-from switch_core.db.stores.workspace_service_store import WorkspaceServiceStore-from switch_core.gateway.auth import get_current_user, organization_workspace_ids+from switch_core.db.stores.workspace_service_store import (+ OrganizationSetting,+ WorkspaceServiceStore,+)+from switch_core.gateway.auth import get_current_user, organization_workspacesfrom switch_core.gateway.dependencies import (get_organization_store,get_session,@@ -55,6 +68,8 @@ from switch_core.gateway.dependencies import ()from switch_core.gateway.organizations import require_organization_admin+logger = logging.getLogger(__name__)+router = APIRouter(prefix="/organizations/current/services")SETTINGS = OrganizationServiceStore()WORKSPACES = WorkspaceServiceStore()@@ -86,73 +101,197 @@ async def _admin_of(return access.organization_id+def _setting_of(row: OrganizationServiceSetting) -> OrganizationSetting:+ return OrganizationSetting(enabled=row.enabled, locked=row.locked)+++def _setting_body(setting: OrganizationSetting | None) -> dict[str, bool] | None:+ return (+ None+ if setting is None+ else {"enabled": setting.enabled, "locked": setting.locked}+ )++async def _reach(- session_factory: async_sessionmaker[AsyncSession], workspace_ids: list[str]-) -> tuple[dict[str, int], dict[str, int]]:- """In how many of the workspaces each service is on, and how many people- there have it connected: what an admin weighs before switching it. One- short session per workspace, after the request's has let its go."""+ session_factory: async_sessionmaker[AsyncSession],+ workspaces: list[Tenant],+ settings: dict[str, OrganizationSetting],+) -> tuple[dict[str, int], dict[str, int], dict[str, int]]:+ """For each service: in how many of the workspaces it is on, in how many+ their admin set it themselves, and how many people, each once, have it+ connected across them. A workspace's rows are readable only bound to it,+ so each is read on its own short session, in two queries, after the+ request's has let its connection go."""+ services = _services()on: dict[str, int] = {}- connected: dict[str, int] = {}- for tenant_id in workspace_ids:- async with tenant_session(session_factory, tenant_id) as session:- switches = await WORKSPACES.switches(- session, _services(), organizations=True- )- counts = await session.execute(- select(- ServiceConnection.service,- func.count(func.distinct(ServiceConnection.user_id)),- )+ own: dict[str, int] = {}+ people: dict[str, set[str]] = {}+ for workspace in workspaces:+ async with tenant_session(session_factory, workspace.id) as session:+ switches = await WORKSPACES.switches_under(session, services, settings)+ connected = await session.execute(+ select(ServiceConnection.service, ServiceConnection.user_id).where(- ServiceConnection.tenant_id == tenant_id,+ ServiceConnection.tenant_id == workspace.id,ServiceConnection.status == "active",)- .group_by(ServiceConnection.service)+ .distinct())- for service, count in counts.tuples():- connected[service] = connected.get(service, 0) + count- for service, switch in switches.items():+ for service, user_id in connected.tuples():+ people.setdefault(service, set()).add(user_id)+ for service, (switch, set_here) in switches.items():if switch.enabled:on[service] = on.get(service, 0) + 1- return on, connected+ if set_here:+ own[service] = own.get(service, 0) + 1+ return on, own, {service: len(users) for service, users in people.items()}+++async def _queue_where_off(+ session_factory: async_sessionmaker[AsyncSession],+ broker: ServiceBroker,+ workspaces: list[Tenant],+ service: str,+ setting: OrganizationSetting | None,+) -> list[Tenant]:+ """Before the setting is saved: queue what each workspace `setting` would+ switch `service` off in was issued for it, each in its own transaction.+ The workspaces it would switch off. Raises if any cannot be queued, so+ the caller saves nothing: no lock is put in force with tokens behind it+ left live."""+ issued_for_it = (ServiceTokenIssuance.service == service,)+ off: list[Tenant] = []+ for workspace in workspaces:+ try:+ async with tenant_session(session_factory, workspace.id) as session:+ (switch, _) = (+ await WORKSPACES.switches_under(+ session,+ [service],+ {} if setting is None else {service: setting},+ )+ )[service]+ if switch.enabled:+ continue+ await broker.queue_revocation(session, *issued_for_it)+ await session.commit()+ except Exception:+ logger.exception(+ "Could not queue %s's tokens in workspace %s before an "+ "organization switched it off there; the change is not saved",+ service,+ workspace.id,+ )+ raise HTTPException(+ status_code=503,+ detail=f"Switch couldn't withdraw {_definition_name(service)} from "+ f"{workspace.name}, so nothing was changed. Try again.",+ ) from None+ off.append(workspace)+ return off-async def _withdraw_where_off(+async def _withdraw(session_factory: async_sessionmaker[AsyncSession],broker: ServiceBroker,- workspace_ids: list[str],+ workspaces: list[Tenant],service: str,) -> str | None:- """After a change has committed: in each workspace where `service` is now- off, withdraw what it was issued there, as a workspace admin's switch- does. The warnings, once each, of what could not be taken back yet."""+ """After the setting has committed: in each workspace it switched+ `service` off in, queue again what was issued in between, then revoke.+ A workspace that fails is logged and named; the rest carry on. The+ warnings, once each, of what could not be taken back yet."""issued_for_it = (ServiceTokenIssuance.service == service,)warnings: list[str] = []- for tenant_id in workspace_ids:- async with tenant_session(session_factory, tenant_id) as session:- if await WORKSPACES.is_enabled(session, service, organizations=True):- continue- await broker.queue_revocation(session, *issued_for_it)- await session.commit()- warning = await broker.revoke_withdrawn(session, issued_for_it)+ failed: list[str] = []+ for workspace in workspaces:+ try:+ async with tenant_session(session_factory, workspace.id) as session:+ await broker.queue_revocation(session, *issued_for_it)+ await session.commit()+ warning = await broker.revoke_withdrawn(session, issued_for_it)+ except Exception:+ logger.exception(+ "Could not finish withdrawing %s in workspace %s after its "+ "organization switched it off there",+ service,+ workspace.id,+ )+ failed.append(workspace.name)+ continueif warning is not None and warning not in warnings:warnings.append(warning)+ if failed:+ warnings.append(+ f"Switch couldn't finish withdrawing {_definition_name(service)} in "+ f"{', '.join(failed)}. What it had queued there is revoked by its "+ "periodic retry; tokens issued since may stay valid until they expire."+ )return " ".join(warnings) or None-def _setting_body(setting: OrganizationServiceSetting | None) -> dict[str, bool] | None:- return (- None- if setting is None- else {"enabled": setting.enabled, "locked": setting.locked}+async def _apply(+ session: AsyncSession,+ session_factory: async_sessionmaker[AsyncSession],+ broker: ServiceBroker,+ user: User,+ organization_id: str,+ service: str,+ previous: OrganizationSetting | None,+ setting: OrganizationSetting | None,+) -> str | None:+ """Put `setting` (None: no say) in force for `service`, withdrawing it+ wherever that switches it off, as the module describes."""+ # Only switching off, or giving up a setting of on, can leave a workspace+ # without the service.+ can_switch_off = (setting is not None and not setting.enabled) or (+ setting is None and previous is not None and previous.enabled+ )+ # The request's connection goes back before the scans open one per workspace.+ await session.commit()+ workspaces = (+ await organization_workspaces(session_factory, user.id, organization_id)+ if can_switch_off+ else [])+ off = await _queue_where_off(session_factory, broker, workspaces, service, setting)+ if setting is None:+ await SETTINGS.clear(session, organization_id, service)+ else:+ await SETTINGS.set(+ session,+ organization_id,+ service,+ enabled=setting.enabled,+ locked=setting.locked,+ updated_by=user.id,+ )+ details: dict[str, Any] = {"service": service, "previous": _setting_body(previous)}+ if setting is not None:+ details.update(enabled=setting.enabled, locked=setting.locked)+ await record_organization_audit_event(+ session,+ organization_id=organization_id,+ actor_user_id=user.id,+ action=(+ OrganizationAuditAction.SERVICE_CLEARED+ if setting is None+ else OrganizationAuditAction.SERVICE_SET+ ),+ target_type="service",+ target_id=service,+ details=details,+ )+ await session.commit()+ return await _withdraw(session_factory, broker, off, service)@router.get("")async def list_organization_services(session: Annotated[AsyncSession, Depends(get_session)],session_factory: Annotated[Any, Depends(get_session_factory)],+ broker: Annotated[ServiceBroker, Depends(get_service_broker)],user_store: Annotated[UserStore, Depends(get_user_store)],organization_store: Annotated[OrganizationStore, Depends(get_organization_store)],user: Annotated[User, Depends(get_current_user)],@@ -177,10 +316,14 @@ async def list_organization_services(}# The request's connection goes back before the scan opens one per workspace.await session.commit()- workspace_ids = await organization_workspace_ids(+ workspaces = await organization_workspaces(session_factory, user.id, organization_id)- on, connected = await _reach(session_factory, workspace_ids)+ on, own, connected = await _reach(+ session_factory,+ workspaces,+ {service: _setting_of(row) for service, row in settings.items()},+ )services = []for entry in CATALOG.values():definition = entry.definition@@ -188,22 +331,30 @@ async def list_organization_services(continuesetting = settings.get(definition.slug)updated_at: datetime | None = None if setting is None else setting.updated_at+ server_off = broker.server_switched_off(definition.slug)services.append({"slug": definition.slug,"name": definition.name,- "setting": _setting_body(setting),+ "setting": None+ if setting is None+ else _setting_body(_setting_of(setting)),"updated_by_name": (Noneif setting is None or setting.updated_by is Noneelse authors.get(setting.updated_by)),"updated_at": None if updated_at is None else updated_at.isoformat(),- "workspaces_on": on.get(definition.slug, 0),+ # Off on the whole server, whatever any workspace says.+ "server_switched_off": server_off,+ "workspaces_on": 0 if server_off else on.get(definition.slug, 0),+ # Workspaces whose admin set it themselves: an unlocked+ # setting does not reach them.+ "workspaces_set_own": own.get(definition.slug, 0),"connected_members": connected.get(definition.slug, 0),})- return {"workspaces": len(workspace_ids), "services": services}+ return {"workspaces": len(workspaces), "services": services}class SettingBody(BaseModel):@@ -227,40 +378,22 @@ async def set_organization_service(not. Where that leaves it off, what was issued for it is withdrawn."""organization_id = await _admin_of(session, user_store, organization_store, user)_definition_name(service)- previous = (await SETTINGS.list_settings(session, organization_id)).get(service)- details: dict[str, Any] = {- "service": service,- "enabled": body.enabled,- "locked": body.locked,- "previous": _setting_body(previous),- }- await SETTINGS.set(+ row = (await SETTINGS.list_settings(session, organization_id)).get(service)+ previous = None if row is None else _setting_of(row)+ setting = OrganizationSetting(enabled=body.enabled, locked=body.locked)+ if previous == setting:+ return {"slug": service, "setting": _setting_body(setting), "warning": None}+ warning = await _apply(session,+ session_factory,+ broker,+ user,organization_id,service,- enabled=body.enabled,- locked=body.locked,- updated_by=user.id,- )- await record_organization_audit_event(- session,- organization_id=organization_id,- actor_user_id=user.id,- action=OrganizationAuditAction.SERVICE_SET,- target_type="service",- target_id=service,- details=details,+ previous,+ setting,)- await session.commit()- workspace_ids = await organization_workspace_ids(- session_factory, user.id, organization_id- )- warning = await _withdraw_where_off(session_factory, broker, workspace_ids, service)- return {- "slug": service,- "setting": {"enabled": body.enabled, "locked": body.locked},- "warning": warning,- }+ return {"slug": service, "setting": _setting_body(setting), "warning": warning}@router.delete("/{service}")@@ -278,26 +411,20 @@ async def clear_organization_service(for it is withdrawn."""organization_id = await _admin_of(session, user_store, organization_store, user)name = _definition_name(service)- previous = (await SETTINGS.list_settings(session, organization_id)).get(service)- if previous is None:+ row = (await SETTINGS.list_settings(session, organization_id)).get(service)+ if row is None:raise HTTPException(status_code=404,detail=f"Your organization has no setting for {name}.",)- details = {"service": service, "previous": _setting_body(previous)}- await SETTINGS.clear(session, organization_id, service)- await record_organization_audit_event(+ warning = await _apply(session,- organization_id=organization_id,- actor_user_id=user.id,- action=OrganizationAuditAction.SERVICE_CLEARED,- target_type="service",- target_id=service,- details=details,- )- await session.commit()- workspace_ids = await organization_workspace_ids(- session_factory, user.id, organization_id+ session_factory,+ broker,+ user,+ organization_id,+ service,+ _setting_of(row),+ None,)- warning = await _withdraw_where_off(session_factory, broker, workspace_ids, service)return {"slug": service, "setting": None, "warning": warning}diff --git a/core/switch_core/gateway/workspace_services.py b/core/switch_core/gateway/workspace_services.pyindex 3e725261e..73034ec90 100644--- a/core/switch_core/gateway/workspace_services.py+++ b/core/switch_core/gateway/workspace_services.py@@ -26,14 +26,20 @@ from pydantic import BaseModel, ConfigDictfrom sqlalchemy import func, selectfrom sqlalchemy.ext.asyncio import AsyncSession-from switch_core.connections.broker import ServiceBroker, get_service_broker+from switch_core.connections.broker import (+ ServiceBroker,+ get_service_broker,+ organization_locked,+)from switch_core.connections.loader import CATALOGfrom switch_core.db.audit import AuditAction, record_audit_eventfrom switch_core.db.models import (+ OrganizationServiceSetting,ServiceConnection,ServiceGrant,ServiceTokenIssuance,User,+ WorkspaceServiceSetting,require_tenant_id,)from switch_core.db.stores.workspace_service_store import WorkspaceServiceStore@@ -63,18 +69,20 @@ async def list_workspace_services("""Every catalog service that can be connected, whether the workspace hasit on, and who it reaches: what an admin weighs before switching it off."""settings = await STORE.list_settings(session)+ above = await STORE.organization_settings(session) if organizations else {}switches = await STORE.switches(session,[entry.definition.slug for entry in CATALOG.values()],organizations=organizations,)+ authors = {setting.updated_by for setting in settings.values()} | {+ setting.updated_by+ for setting in above.values()+ if setting.updated_by is not None+ }names = {user.id: user.name- for user in await session.scalars(- select(User).where(- User.id.in_({setting.updated_by for setting in settings.values()})- )- )+ for user in await session.scalars(select(User).where(User.id.in_(authors)))}connected = await _counts(session, ServiceConnection.user_id, ServiceConnection)granted = await _counts(session, ServiceGrant.agent_id, ServiceGrant)@@ -83,7 +91,12 @@ async def list_workspace_services(definition = entry.definitionif not definition.enabled:continue- setting = settings.get(definition.slug)+ switch = switches[definition.slug]+ # A locked switch is the organization's doing, and named so; otherwise+ # whoever last touched this workspace's own.+ setting: WorkspaceServiceSetting | OrganizationServiceSetting | None = (+ above[definition.slug] if switch.locked else settings.get(definition.slug)+ )updated_at: datetime | None = None if setting is None else setting.updated_atservices.append({@@ -94,8 +107,13 @@ async def list_workspace_services(# does nothing until an organization admin unlocks it."organization_locked": switches[definition.slug].locked,"updated_by_name": (- None if setting is None else names.get(setting.updated_by)+ None+ if setting is None or setting.updated_by is None+ else names.get(setting.updated_by)),+ # Whether this workspace's admin set it themselves, which an+ # organization's unlocked setting does not override.+ "workspace_set": definition.slug in settings,"updated_at": None if updated_at is None else updated_at.isoformat(),"connected_members": connected.get(definition.slug, 0),"granted_agents": granted.get(definition.slug, 0),@@ -132,12 +150,9 @@ async def set_workspace_service(service]if switch.locked:- state = "on" if switch.enabled else "off"raise HTTPException(status_code=409,- detail=f"Your organization has {entry.definition.name} locked {state} "- "for its workspaces. An organization admin can change it under "- "Organization, Services.",+ detail=organization_locked(entry.definition.name, switch.enabled),)await STORE.set_enabled(session, service, body.enabled, user.id)issued_for_it = (ServiceTokenIssuance.service == service,)@@ -161,3 +176,46 @@ async def set_workspace_service(None if body.enabled else await broker.revoke_withdrawn(session, issued_for_it))return {"slug": service, "enabled": body.enabled, "warning": warning}++[email protected]("/{service}")+async def clear_workspace_service(+ service: str,+ user: Annotated[User, Depends(require_tenant_admin)],+ session: Annotated[AsyncSession, Depends(get_session)],+ broker: Annotated[ServiceBroker, Depends(get_service_broker)],+ organizations: Annotated[bool, Depends(organization_apps_on)],+) -> dict[str, Any]:+ """Drop the workspace's own switch for `service`, so its organization's+ setting applies, or else the workspace's default. Where that leaves it+ off, what was issued for it is withdrawn, as switching it off does."""+ entry = CATALOG.get(service)+ if entry is None or not entry.definition.enabled:+ raise HTTPException(status_code=404, detail=f"No service named {service!r}.")+ if not await STORE.clear(session, service):+ raise HTTPException(+ status_code=404,+ detail=f"This workspace has no switch of its own for {entry.definition.name}.",+ )+ switch = (await STORE.switches(session, [service], organizations=organizations))[+ service+ ]+ issued_for_it = (ServiceTokenIssuance.service == service,)+ if not switch.enabled:+ await broker.queue_revocation(session, *issued_for_it)+ await record_audit_event(+ session,+ tenant_id=require_tenant_id(),+ actor_user_id=user.id,+ action=AuditAction.WORKSPACE_SERVICE_CLEARED,+ target_type="service",+ target_id=service,+ details={"service": service, "enabled": switch.enabled},+ )+ await session.commit()+ warning = (+ None+ if switch.enabled+ else await broker.revoke_withdrawn(session, issued_for_it)+ )+ return {"slug": service, "enabled": switch.enabled, "warning": warning}diff --git a/core/tests/switch_core/gateway/test_organization_services.py b/core/tests/switch_core/gateway/test_organization_services.pyindex d26dcfbe4..17911a8b4 100644--- a/core/tests/switch_core/gateway/test_organization_services.py+++ b/core/tests/switch_core/gateway/test_organization_services.py@@ -20,7 +20,7 @@ from switch_core.connections.app_credentials import ConnectorAppsfrom switch_core.connections.broker import (ServiceBroker,ServiceError,- organization_switched_off,+ organization_locked,)from switch_core.connections.organization_apps import OrganizationAppsfrom switch_core.db.audit import OrganizationAuditAction@@ -30,6 +30,7 @@ from switch_core.db.models import (OrganizationMember,Tenant,User,+ require_tenant_id,)from switch_core.db.session_scope import tenant_sessionfrom switch_core.db.stores.organization_app_credential_store import (@@ -43,6 +44,7 @@ from switch_core.gateway.organization_services import (from tests.conftest import TEST_KEYRINGfrom tests.switch_core.connections.fake_vendor import FakeVendorfrom tests.switch_core.connections.service_harness import (+ STORE,agent_with_key,build_service_harness,)@@ -240,7 +242,7 @@ class TestLocked:async with tenant_session(session_factory, TENANT_ZERO_ID) as session:with pytest.raises(ServiceError) as refused:await broker.require_workspace_enabled(session, "github")- assert refused.value.message == organization_switched_off("GitHub")+ assert refused.value.message == organization_locked("GitHub", enabled=False)class TestUnlocked:@@ -313,3 +315,178 @@ class TestClearing:"service": "github","previous": {"enabled": True, "locked": True},}+++async def _connected_in(session_factory, user: User, tenant_id: str) -> None:+ async with tenant_session(session_factory, tenant_id) as session:+ await STORE.save_connection(+ session,+ user_id=user.id,+ service="github",+ consent="write",+ granted_scopes=[],+ account_id=f"account-{tenant_id}",+ external_identity=user.name,+ encrypted_secret=TEST_KEYRING.encrypt("{}"),+ oauth_client_id=None,+ )+ await session.commit()+++class TestWithdrawingSafely:+ async def test_nothing_is_saved_when_a_workspace_cannot_be_prepared(+ self, harness: Harness, session_factory, monkeypatch+ ) -> None:+ admin = await _org_admin(session_factory)+ broker: ServiceBroker = harness.gateway_app.state.service_broker+ queue = broker.queue_revocation++ async def failing_in_the_sibling(session, *conditions):+ if require_tenant_id() == SIBLING:+ raise RuntimeError("the database went away")+ await queue(session, *conditions)++ monkeypatch.setattr(broker, "queue_revocation", failing_in_the_sibling)+ answer = await _call(+ harness,+ admin,+ "PUT",+ f"{ORGANIZATION}/github",+ {"enabled": False, "locked": True},+ )+ organization = _by_slug(await _call(harness, admin, "GET", ORGANIZATION))++ assert answer.status_code == 503+ assert "nothing was changed" in answer.json()["detail"]+ assert organization["github"]["setting"] is None+ assert await _events(session_factory) == []++ async def test_a_workspace_failing_afterwards_is_named_and_the_rest_carry_on(+ self, harness: Harness, vendor: FakeVendor, session_factory, monkeypatch+ ) -> None:+ admin = await _org_admin(session_factory)+ ada = await add_member(session_factory, "ada")+ agent_id, _ = await agent_with_key(session_factory, ada, "builder")+ github = await _granted(session_factory, ada, agent_id, "github")+ async with session_factory() as session:+ session.add(_issuance(github, ada.id, "github", "ghs_here"))+ await session.commit()+ broker: ServiceBroker = harness.gateway_app.state.service_broker+ revoke = broker.revoke_withdrawn++ async def failing_in_the_sibling(session, conditions):+ if require_tenant_id() == SIBLING:+ raise RuntimeError("the vendor went away")+ return await revoke(session, conditions)++ monkeypatch.setattr(broker, "revoke_withdrawn", failing_in_the_sibling)+ answer = await _call(+ harness,+ admin,+ "PUT",+ f"{ORGANIZATION}/github",+ {"enabled": False, "locked": True},+ )++ assert answer.status_code == 200, answer.text+ assert "Sibling" in answer.json()["warning"]+ assert vendor.revoked == ["ghs_here"]+ assert answer.json()["setting"] == {"enabled": False, "locked": True}++ async def test_the_same_setting_twice_is_recorded_once(+ self, harness: Harness, session_factory+ ) -> None:+ admin = await _org_admin(session_factory)+ for _ in range(2):+ answer = await _call(+ harness,+ admin,+ "PUT",+ f"{ORGANIZATION}/github",+ {"enabled": True, "locked": True},+ )+ assert answer.status_code == 200, answer.text+ assert len(await _events(session_factory)) == 1+++class TestWhatThePagesSay:+ async def test_a_person_connected_in_two_workspaces_is_one_person(+ self, harness: Harness, session_factory+ ) -> None:+ admin = await _org_admin(session_factory)+ ada = await add_member(session_factory, "ada")+ await _connected_in(session_factory, ada, TENANT_ZERO_ID)+ await _connected_in(session_factory, ada, SIBLING)+ organization = _by_slug(await _call(harness, admin, "GET", ORGANIZATION))+ assert organization["github"]["connected_members"] == 1++ async def test_a_service_off_on_the_server_is_on_nowhere(+ self, harness: Harness, session_factory+ ) -> None:+ admin = await _org_admin(session_factory)+ await _call(+ harness,+ admin,+ "PUT",+ f"{ORGANIZATION}/github",+ {"enabled": True, "locked": True},+ )+ broker: ServiceBroker = harness.gateway_app.state.service_broker+ broker._disabled["github"] = "GitHub is switched off on this server."+ github = _by_slug(await _call(harness, admin, "GET", ORGANIZATION))["github"]+ assert github["workspaces_on"] == 0+ assert github["server_switched_off"] == "GitHub is switched off on this server."++ async def test_a_locked_switch_is_credited_to_the_organization(+ self, harness: Harness, session_factory+ ) -> None:+ admin = await _org_admin(session_factory)+ workspace_admin = await _person(+ session_factory, "workspace-admin", member_role="admin"+ )+ await _call(+ harness, workspace_admin, "PUT", f"{WORKSPACE}/github", {"enabled": True}+ )+ await _call(+ harness,+ admin,+ "PUT",+ f"{ORGANIZATION}/github",+ {"enabled": False, "locked": True},+ )+ github = _by_slug(await _call(harness, workspace_admin, "GET", WORKSPACE))[+ "github"+ ]+ assert github["enabled"] is False+ assert github["updated_by_name"] == "org-admin"++ async def test_an_unlocked_setting_reaches_a_workspace_once_it_drops_its_own(+ self, harness: Harness, session_factory+ ) -> None:+ admin = await _org_admin(session_factory)+ workspace_admin = await _person(+ session_factory, "workspace-admin", member_role="admin"+ )+ await _call(+ harness, workspace_admin, "PUT", f"{WORKSPACE}/github", {"enabled": False}+ )+ await _call(+ harness,+ admin,+ "PUT",+ f"{ORGANIZATION}/github",+ {"enabled": True, "locked": False},+ )+ before = _by_slug(await _call(harness, workspace_admin, "GET", WORKSPACE))+ organization = _by_slug(await _call(harness, admin, "GET", ORGANIZATION))+ dropped = await _call(harness, workspace_admin, "DELETE", f"{WORKSPACE}/github")+ after = _by_slug(await _call(harness, workspace_admin, "GET", WORKSPACE))++ assert before["github"]["enabled"] is False+ assert before["github"]["workspace_set"] is True+ assert organization["github"]["workspaces_set_own"] == 1+ assert organization["github"]["workspaces_on"] == 1+ assert dropped.status_code == 200, dropped.text+ assert dropped.json()["enabled"] is True+ assert after["github"]["enabled"] is True+ assert after["github"]["workspace_set"] is Falsediff --git a/core/tests/switch_core/gateway/test_workspace_services.py b/core/tests/switch_core/gateway/test_workspace_services.pyindex cf069851c..7535152aa 100644--- a/core/tests/switch_core/gateway/test_workspace_services.py+++ b/core/tests/switch_core/gateway/test_workspace_services.py@@ -253,6 +253,7 @@ class TestResults:"enabled": on_by_default,"organization_locked": False,"updated_by_name": None,+ "workspace_set": False,"updated_at": None,"connected_members": 0,"granted_agents": 0,diff --git a/gateway/src/data/api.ts b/gateway/src/data/api.tsindex ea31e86d3..05852c502 100644--- a/gateway/src/data/api.ts+++ b/gateway/src/data/api.ts@@ -1294,9 +1294,13 @@ export interface OrganizationService {setting: OrganizationServiceSetting | null;updated_by_name: string | null;updated_at: string | null;+ /** Why the server switched it off everywhere, whatever any workspace says; null if it didn't. */+ server_switched_off: string | null;/** In how many of the organization's workspaces it is on now. */workspaces_on: number;- /** People across the organization's workspaces with it connected. */+ /** Workspaces whose admin set it themselves: an unlocked setting doesn't reach them. */+ workspaces_set_own: number;+ /** People across the organization's workspaces with it connected, each once. */connected_members: number;}diff --git a/gateway/src/pages/organization/OrganizationPage.test.tsx b/gateway/src/pages/organization/OrganizationPage.test.tsxindex 6f9d8cfb2..5395b7162 100644--- a/gateway/src/pages/organization/OrganizationPage.test.tsx+++ b/gateway/src/pages/organization/OrganizationPage.test.tsx@@ -171,7 +171,9 @@ describe("OrganizationPage", () => {setting: null,updated_by_name: null,updated_at: null,+ server_switched_off: null,workspaces_on: 2,+ workspaces_set_own: 1,connected_members: 3,};const fetchMock = answer({@@ -186,7 +188,7 @@ describe("OrganizationPage", () => {render(<OrganizationPage />);const card = await screen.findByTestId("organization-service-github");- expect(within(card).getByText("On in 2 of 3 workspaces · 3 people connected")).toBeTruthy();+ expect(within(card).getByText("On in 2 of 3 workspaces · 1 sets its own · 3 people connected")).toBeTruthy();expect((within(card).getByLabelText("Lock GitHub") as HTMLInputElement).disabled).toBe(true);fireEvent.mouseDown(within(card).getByRole("combobox"));fireEvent.click(await screen.findByRole("option", { name: "Off" }));diff --git a/gateway/src/pages/organization/OrganizationServicesSection.tsx b/gateway/src/pages/organization/OrganizationServicesSection.tsxindex 89ac21794..d183b4349 100644--- a/gateway/src/pages/organization/OrganizationServicesSection.tsx+++ b/gateway/src/pages/organization/OrganizationServicesSection.tsx@@ -32,9 +32,17 @@ function choiceOf(service: OrganizationService): Choice {return service.setting.enabled ? "on" : "off";}-/** "On in 2 of 3 workspaces · 4 people connected · set by Ada, 1 Oct 2026". */+/** "On in 2 of 3 workspaces · 1 sets its own · 4 people connected · set by Ada, 1 Oct 2026". */export function reachText(service: OrganizationService, workspaces: number): string {- const parts = [`On in ${service.workspaces_on} of ${workspaces} workspace${workspaces === 1 ? "" : "s"}`];+ if (service.server_switched_off !== null) return service.server_switched_off;+ const parts = [+ `On in ${service.workspaces_on} of ${workspaces} workspace${workspaces === 1 ? "" : "s"}`,+ ];+ // An unlocked setting doesn't reach a workspace that set its own.+ if (service.workspaces_set_own > 0 && !service.setting?.locked)+ parts.push(+ `${service.workspaces_set_own} ${service.workspaces_set_own === 1 ? "sets its" : "set their"} own`,+ );if (service.connected_members > 0)parts.push(`${service.connected_members} ${service.connected_members === 1 ? "person" : "people"} connected`,@@ -90,9 +98,11 @@ export default function OrganizationServicesSection() {<Stack spacing={1.5}><Typography variant="h6">Services</Typography><Typography variant="body2" sx={{ color: "text.secondary" }}>- Switch a service on or off for every workspace in the organization. Workspace admins can- change it in their own workspace unless you lock it. Switching a service off keeps- agents' grants, and gives them back when it is switched on again.+ Switch a service on or off for the workspaces in the organization. Unlocked, your+ setting applies only where a workspace admin hasn't set the service themselves, and+ they can still change it. Locked, it applies to every workspace and they can't.+ Switching a service off keeps agents' grants, and gives them back when it is+ switched on again.</Typography>{error && <Alert severity="error">{error}</Alert>}{actionError && (