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
Address review on organizations' connector apps
Petr Bauch16b810e36218
diff --git a/core/switch_core/bridges/agent/operations/agent_services.py b/core/switch_core/bridges/agent/operations/agent_services.pyindex 3bfeddd95..71065ca9a 100644--- a/core/switch_core/bridges/agent/operations/agent_services.py+++ b/core/switch_core/bridges/agent/operations/agent_services.py@@ -104,7 +104,9 @@ async def set_agent_service("agent": agent.name,"service": grant.service,"access": grant.access,- "summary": await broker.summary(agent.display_name or agent.name, grant),+ "summary": await broker.summary(+ session, agent.display_name or agent.name, grant+ ),"warning": warning,}diff --git a/core/switch_core/bridges/agent/operations/my_context.py b/core/switch_core/bridges/agent/operations/my_context.pyindex 4e9f750a6..8252a0b07 100644--- a/core/switch_core/bridges/agent/operations/my_context.py+++ b/core/switch_core/bridges/agent/operations/my_context.py@@ -18,6 +18,7 @@ from __future__ import annotationsfrom typing import Anyfrom sqlalchemy import func, select+from sqlalchemy.ext.asyncio import AsyncSessionfrom switch_core import messaging_platformsfrom switch_core.addressing import policy_kind@@ -177,6 +178,7 @@ async def get_my_context() -> dict[str, Any]:for agent in agents}bridges = await protocol.bridge_store.get_all(session)+ services = await _services(session, protocol, connections, agents, grants)admin = role in {"owner", "admin"} or owner.role == "admin"port = management_port()@@ -232,7 +234,7 @@ async def get_my_context() -> dict[str, Any]:],"how_to_add": _how_to_add(protocol),},- "services": await _services(protocol, connections, agents, grants),+ "services": services,"agents": [{"id": agent.id,@@ -289,6 +291,7 @@ def _how_to_add(protocol: Any) -> list[dict[str, Any]]:async def _services(+ session: AsyncSession,protocol: Any,connections: dict[str, str],agents: list[Agent],@@ -303,13 +306,13 @@ async def _services(definition = entry.definitionif not definition.enabled:continue- unavailable = await broker.unavailable(definition.slug)+ standing = await broker.standing(session, definition.slug)+ unavailable = standing.unavailableentries.append({"slug": definition.slug,"name": definition.name,- "available": unavailable is None- and await broker.connectable(definition.slug),+ "available": unavailable is None and standing.connectable,"unavailable_reason": Noneif unavailable is Noneelse unavailable.reason,diff --git a/core/switch_core/connections/app_credentials.py b/core/switch_core/connections/app_credentials.pyindex 87f05f612..c9f73c7aa 100644--- a/core/switch_core/connections/app_credentials.py+++ b/core/switch_core/connections/app_credentials.py@@ -33,7 +33,7 @@ changed on its next re-check.from __future__ import annotationsimport logging-from collections.abc import Mapping+from collections.abc import Callable, Mappingfrom dataclasses import dataclass, fieldfrom pathlib import Pathfrom typing import TYPE_CHECKING, Literal@@ -223,13 +223,23 @@ class _Configured:class AppLoadError(Exception):- """A saved app that cannot be used; the message is for the operator."""+ """A saved app that cannot be used; the message is for whoever saved it."""+++class UnreadableAppError(AppLoadError):+ """No current key opens the saved secrets; saving the app again fixes it."""+++class ServerCannotUseAppError(AppLoadError):+ """Nothing is wrong with the saved app, but this server cannot use any+ such app (GitHub's needs an https address), which only its operator can+ change."""def github_app_from_values(values: AppValues, public_url: str | None) -> GitHubApp:"""A GitHub App from saved values, called back at `public_url`."""if public_url is None or urlsplit(public_url).scheme != "https":- raise AppLoadError(GITHUB_NEEDS_HTTPS)+ raise ServerCannotUseAppError(GITHUB_NEEDS_HTTPS)try:connections = GitHubConnections(client_id=values.client_id,@@ -247,18 +257,39 @@ def github_app_from_values(values: AppValues, public_url: str | None) -> GitHubAreturn GitHubApp(connections, signer)-def app_from_values(- spec: AppSpec, values: AppValues, public_url: str | None-) -> tuple[GitHubApp | None, OAuthClientCredentials | None]:- """The app saved values describe, as its adapter takes it: a GitHub App- or an OAuth client, the other None. Raises `AppLoadError` when they- cannot be used."""+@dataclass(frozen=True)+class LoadedApp:+ """A saved app, opened: its values, and the GitHub App or OAuth client+ its adapter takes, the other None."""++ values: AppValues+ github_app: GitHubApp | None+ oauth_client: OAuthClientCredentials | None+++def load_saved_app(+ spec: AppSpec,+ *,+ client_id: str,+ settings: Mapping[str, str],+ secrets: Callable[[], dict[str, str]],+ public_url: str | None,+) -> LoadedApp:+ """A saved row's app, the server's or an organization's. `secrets`+ decrypts the row's. Raises `AppLoadError` when it cannot be used."""+ try:+ opened = secrets()+ except UndecryptableError:+ raise UnreadableAppError(UNREADABLE) from None+ values = AppValues(client_id, dict(settings), opened)if spec.kind == "github_app":- return github_app_from_values(values, public_url), None- if "client_secret" not in values.secrets:+ return LoadedApp(values, github_app_from_values(values, public_url), None)+ if "client_secret" not in opened:raise AppLoadError("The saved client has no client secret.")- return None, OAuthClientCredentials(- values.client_id, values.secrets["client_secret"], authentication=None+ return LoadedApp(+ values,+ None,+ OAuthClientCredentials(client_id, opened["client_secret"], authentication=None),)@@ -482,8 +513,14 @@ class ConnectorApps:if row is None:return self._none(spec, load_error=None, unreadable=False)try:- secrets = self._store.secrets(row)- except UndecryptableError:+ loaded = load_saved_app(+ spec,+ client_id=row.client_id,+ settings=row.settings,+ secrets=lambda: self._store.secrets(row),+ public_url=self.public_url,+ )+ except UnreadableAppError:logger.error("The app saved for %s cannot be decrypted with any current key; ""%s is not set up until it is saved again.",@@ -491,9 +528,6 @@ class ConnectorApps:spec.name,)return self._none(spec, load_error=UNREADABLE, unreadable=True)- values = AppValues(row.client_id, dict(row.settings), secrets)- try:- github_app, oauth_client = app_from_values(spec, values, self.public_url)except AppLoadError as error:logger.error("The app saved for %s cannot be used, so it is not set up: %s",@@ -504,9 +538,9 @@ class ConnectorApps:return EffectiveCredentials(spec=spec,source="saved",- values=values,- github_app=github_app,- oauth_client=oauth_client,+ values=loaded.values,+ github_app=loaded.github_app,+ oauth_client=loaded.oauth_client,configuration_variables=(),deprecated=False,saved_ignored=False,diff --git a/core/switch_core/connections/broker.py b/core/switch_core/connections/broker.pyindex 00c60447e..b04083834 100644--- a/core/switch_core/connections/broker.py+++ b/core/switch_core/connections/broker.py@@ -177,6 +177,53 @@ class _Choice:problem: str | None+def _client_replaced(+ connection: ServiceConnection, adapter: ServiceAdapter | None+) -> bool:+ """Whether `connection` signed in through a client other than the one+ `adapter` signs in through, so it must reconnect. Unknown either side+ is no."""+ current = None if adapter is None else adapter.current_client_id()+ return (+ connection.oauth_client_id is not None+ and current is not None+ and current != connection.oauth_client_id+ )+++@dataclass(frozen=True)+class ServiceStanding:+ """What a service is in one workspace, read once: why it cannot be+ granted (None if it can), whether a person can connect it, and the+ adapter it goes through."""++ unavailable: Unavailable | None+ connectable: bool+ adapter: ServiceAdapter | None++ @property+ def server_setup_needed(self) -> bool:+ """Whether it waits on the operator setting up its app here."""+ return (+ self.unavailable is not None and self.unavailable.code == "app_not_set_up"+ )++ def client_replaced(self, connection: ServiceConnection) -> bool:+ """Whether `connection` signed in through a client this workspace no+ longer uses for the service, so it must reconnect."""+ return _client_replaced(connection, self.adapter)+++@dataclass(frozen=True)+class SignIn:+ """A connection's sign-in, kept to revoke it once the row is gone: its+ secret, and the OAuth client it was made through, None for a key or a+ connection from before clients were recorded."""++ secret: ConnectionSecret = field(repr=False)+ oauth_client_id: str | None++@dataclass(frozen=True)class Unavailable:code: UnavailableCode@@ -437,7 +484,9 @@ class ServiceBroker:)return entry- async def sign_in(self, service: str) -> tuple[ConnectionDefinition, OAuthAdapter]:+ async def sign_in(+ self, session: AsyncSession, service: str+ ) -> tuple[ConnectionDefinition, OAuthAdapter]:"""The catalog entry and adapter a person signs in to `service`through, with Switch's generic OAuth flow (`connections/flows.py`)."""definition = self._entry(service).definition@@ -449,15 +498,15 @@ class ServiceBroker:f"{definition.name} is connected through its own sign-in.",retryable=False,)- adapter = await self._here(service)- if not isinstance(adapter, OAuthAdapter):+ choice = await self._choose(session, service)+ if not isinstance(choice.adapter, OAuthAdapter):raise ServiceError(503,INTERNAL,- await self.availability(service) or "Not available on this server.",+ self._reason(service, choice),retryable=False,)- return definition, adapter+ return definition, choice.adapterdef _require_switched_on(self, service: str) -> None:reason = self._disabled.get(service)@@ -490,36 +539,41 @@ class ServiceBroker:return set()return await self._workspace_services.disabled_services(session, self._catalog)- async def _choose(self, service: str) -> _Choice:+ async def _choose(self, session: AsyncSession, service: str) -> _Choice:"""The adapter `service` goes through in the bound workspace: its- organization's app where it saved one, else the server's."""+ organization's app where it saved one, else the server's. `session`+ is bound to the workspace."""if self._organization_apps is not None:app = await self._organization_apps.for_workspace(- require_tenant_id(), service+ session, require_tenant_id(), service)if app is not None:return _Choice(app.adapter, app.problem)return _Choice(self._adapters.get(service), None)- async def _here(self, service: str) -> ServiceAdapter | None:+ async def _here(self, session: AsyncSession, service: str) -> ServiceAdapter | None:"""`service`'s adapter in the bound workspace, or None."""- return (await self._choose(service)).adapter+ return (await self._choose(session, service)).adapter- async def _adapter(self, service: str) -> ServiceAdapter:+ async def _adapter(self, session: AsyncSession, service: str) -> ServiceAdapter:"""The adapter that issues for `service`, or the refusal saying why not."""- adapter = await self._refresher(service)+ choice = await self._choose(session, service)+ adapter = self._refusing(service, choice)if not adapter.can_issue:raise ServiceError(503,INTERNAL,- await self.availability(service) or "Not available on this server.",+ self._reason(service, choice),retryable=False,)return adapter- async def _refresher(self, service: str) -> ServiceAdapter:+ async def _refresher(self, session: AsyncSession, service: str) -> ServiceAdapter:"""The adapter that keeps `service`'s connections signed in."""- choice = await self._choose(service)+ return self._refusing(service, await self._choose(session, service))++ def _refusing(self, service: str, choice: _Choice) -> ServiceAdapter:+ """`choice`'s adapter, or the refusal saying why there is none."""if choice.adapter is None:raise ServiceError(503,@@ -727,7 +781,7 @@ class ServiceBroker:service: str,) -> ServiceToken:decision = await self.decide(session, agent_id, principal, service)- adapter = await self._adapter(service)+ adapter = await self._adapter(session, service)await session.commit()access = await self._access_token(decision.owner_id, service, adapter)request = IssueRequest(@@ -1034,7 +1088,7 @@ class ServiceBroker:"Connections.",retryable=False,)- if self._client_replaced(connection, adapter):+ if _client_replaced(connection, adapter):await self._store.mark_needs_reauthorization(session, owner_id, service, CLIENT_REPLACED)@@ -1119,6 +1173,38 @@ class ServiceBroker:await finish_shielded(mark())return self._revoked(service, error)+ def _signed_in(self, connection: ServiceConnection) -> SignIn:+ return SignIn(self._secret(connection), connection.oauth_client_id)++ async def _revoker(+ self, session: AsyncSession, service: str, oauth_client_id: str | None+ ) -> ServiceAdapter:+ """The adapter to revoke a sign-in made through `oauth_client_id`+ with: the one that applies here, or else the server's, whichever is+ that client's. A sign-in can outlive its app (an organization saved+ its own since, or replaced or removed it), and the vendor refuses a+ revocation from any other client. Raises `ServiceUnavailableError`,+ with what the person can do, when neither is."""+ entry = self._catalog.get(service)+ name = service if entry is None else entry.definition.name+ here = await self._here(session, service)+ server = self._adapters.get(service)+ if here is None and server is None:+ raise ServiceUnavailableError(+ f"{name} is not set up on this server, so Switch could not "+ f"revoke the sign-in. Revoke it at {name}."+ )+ for adapter in (here, server):+ if adapter is not None and (+ oauth_client_id is None+ or adapter.current_client_id() in (None, oauth_client_id)+ ):+ return adapter+ raise ServiceUnavailableError(+ f"Switch no longer uses the {name} app this sign-in was made through, "+ f"so it could not revoke it. Revoke it at {name}."+ )+def _secret(self, connection: ServiceConnection) -> ConnectionSecret:return ConnectionSecret(json.loads(self._keyring.decrypt(connection.encrypted_secret))@@ -1189,39 +1275,22 @@ class ServiceBroker:)return grants- async def connectable(self, service: str) -> bool:- """Whether a person can connect `service` here: it has an adapter, is- not switched off, and what it connects could be handed to an agent."""+ async def standing(self, session: AsyncSession, service: str) -> ServiceStanding:+ """What `service` is in the bound workspace, read once, so every+ answer about it comes from the same adapter. `session` is bound to+ the workspace."""+ choice = await self._choose(session, service)entry = self._catalog.get(service)- return (- entry is not None- and entry.definition.enabled- and _issued_to_agents(entry)- and service not in self._disabled- and await self._here(service) is not None- )-- async def server_setup_needed(self, service: str) -> bool:- """Whether `service` waits on the operator setting up its app here."""- unavailable = await self.unavailable(service)- return unavailable is not None and unavailable.code == "app_not_set_up"-- async def client_replaced(self, connection: ServiceConnection) -> bool:- """Whether `connection` signed in through a client its service no- longer uses here, so it must reconnect. Unknown either side is no."""- if connection.oauth_client_id is None:- return False- return self._client_replaced(connection, await self._here(connection.service))-- @staticmethod- def _client_replaced(- connection: ServiceConnection, adapter: ServiceAdapter | None- ) -> bool:- current = None if adapter is None else adapter.current_client_id()- return (- connection.oauth_client_id is not None- and current is not None- and current != connection.oauth_client_id+ return ServiceStanding(+ unavailable=self._unavailable(service, choice),+ connectable=(+ entry is not None+ and entry.definition.enabled+ and _issued_to_agents(entry)+ and service not in self._disabled+ and choice.adapter is not None+ ),+ adapter=choice.adapter,)def replace_adapter(self, service: str, adapter: ServiceAdapter | None) -> None:@@ -1239,15 +1308,24 @@ class ServiceBroker:else:self._adapters[service] = adapter- async def availability(self, service: str) -> str | None:+ async def availability(self, session: AsyncSession, service: str) -> str | None:"""Why `service` cannot be granted in the bound workspace, or None ifit can."""- unavailable = await self.unavailable(service)+ unavailable = (await self.standing(session, service)).unavailablereturn None if unavailable is None else unavailable.reason- async def unavailable(self, service: str) -> Unavailable | None:- """Why `service` cannot be granted in the bound workspace, as a code- and the sentence people read, or None if it can."""+ def _reason(self, service: str, choice: _Choice) -> str:+ """Why `service` cannot be used through `choice`, in a sentence."""+ unavailable = self._unavailable(service, choice)+ return (+ "Not available on this server."+ if unavailable is None+ else unavailable.reason+ )++ def _unavailable(self, service: str, choice: _Choice) -> Unavailable | None:+ """Why `service` cannot be granted through `choice`, as a code and+ the sentence people read, or None if it can."""entry = self._catalog.get(service)if entry is None or not entry.definition.enabled:return Unavailable("coming_soon", "Not available yet.")@@ -1264,7 +1342,6 @@ class ServiceBroker:and "loopback" not in oauth.redirect):return Unavailable("needs_public_url", needs_public_url(name))- choice = await self._choose(service)adapter = choice.adapterif adapter is None:kind = app_kind(entry.definition)@@ -1283,10 +1360,12 @@ class ServiceBroker:)return None- async def summary(self, agent_name: str, grant: ServiceGrant) -> str:+ async def summary(+ self, session: AsyncSession, agent_name: str, grant: ServiceGrant+ ) -> str:"""A grant's reach in a sentence, as the vendor's adapter words it."""access: AccessLevel = "write" if grant.access == "write" else "read"- adapter = await self._here(grant.service)+ adapter = await self._here(session, grant.service)if adapter is not None:return adapter.summary(agent_name, access, dict(grant.resources))verb = "read and write" if access == "write" else "read"@@ -1309,13 +1388,15 @@ class ServiceBroker:raise ServiceError(404, NOT_FOUND, f"No service named {service!r}.", retryable=False)- unavailable = await self.availability(service)- if unavailable is not None:- raise ServiceError(422, VALIDATION_ERROR, unavailable, retryable=False)+ standing = await self.standing(session, service)+ if standing.unavailable is not None:+ raise ServiceError(+ 422, VALIDATION_ERROR, standing.unavailable.reason, retryable=False+ )await self.require_workspace_enabled(session, service)entry = self._entry(service)name = entry.definition.name- adapter = await self._adapter(service)+ adapter = await self._adapter(session, service)connection = await self._store.get_connection(session, owner_id, service)if connection is None:raise ServiceError(@@ -1598,7 +1679,13 @@ class ServiceBroker:self._secret(previous).access_token == secret.access_token)await session.rollback()- note = None if kept else await self._revoke_unkept(service, secret)+ note = (+ None+ if kept+ else await self._revoke_unkept(+ session, service, secret, oauth_client_id+ )+ )raise ServiceError(refusal.status_code,refusal.code,@@ -1610,7 +1697,7 @@ class ServiceBroker:except ServiceConnectionBusy as error:raise ServiceError(503, INTERNAL, str(error), retryable=True) from Noneprevious = await self._store.get_connection(session, user_id, service)- replaced = None if previous is None else self._secret(previous)+ replaced = None if previous is None else self._signed_in(previous)issued_on_it = (ServiceTokenIssuance.owner_id == user_id,ServiceTokenIssuance.service == service,@@ -1639,15 +1726,13 @@ class ServiceBroker:await session.commit()warnings = []- if replaced is not None and replaced.access_token != secret.access_token:- adapter = await self._here(service)+ if replaced is not None and replaced.secret.access_token != secret.access_token:try:- if adapter is None:- raise ServiceUnavailableError(- f"{name} is not available on this server."- )+ adapter = await self._revoker(+ session, service, replaced.oauth_client_id+ )async with asyncio.timeout(VENDOR_CALL_SECONDS):- await adapter.revoke_connection(replaced)+ await adapter.revoke_connection(replaced.secret)except ConnectionNotRevocable as error:warnings.append(str(error))except Exception as error:@@ -1666,7 +1751,11 @@ class ServiceBroker:return " ".join(warnings) or Noneasync def _revoke_unkept(- self, service: str, secret: ConnectionSecret+ self,+ session: AsyncSession,+ service: str,+ secret: ConnectionSecret,+ oauth_client_id: str | None,) -> str | None:"""Revoke a new sign-in that was not kept; a sentence for the personwhen it could not be. A key is the person's own, and theirs to keep."""@@ -1678,12 +1767,8 @@ class ServiceBroker:f"Switch could not revoke the sign-in {name} just issued. Remove "f"Switch from your {name} account's connected apps.")- adapter = await self._here(service)try:- if adapter is None:- raise ServiceUnavailableError(- f"{name} is not available on this server."- )+ adapter = await self._revoker(session, service, oauth_client_id)async with asyncio.timeout(VENDOR_CALL_SECONDS):await adapter.revoke_connection(secret)except ConnectionNotRevocable as error:@@ -1718,12 +1803,12 @@ class ServiceBroker:raise ServiceError(403, FORBIDDEN, _never_expires(definition.name), retryable=False)- adapter = await self._here(service)+ adapter = self._adapters.get(service)if not isinstance(adapter, ApiKeyAdapter):raise ServiceError(503,INTERNAL,- await self.availability(service) or "Not available on this server.",+ self._reason(service, _Choice(adapter, None)),retryable=False,)try:@@ -1762,7 +1847,9 @@ class ServiceBroker:return Noneawait session.commit()return (- await self._access_token(user_id, service, await self._refresher(service))+ await self._access_token(+ user_id, service, await self._refresher(session, service)+ )).tokenasync def queue_agent_revocation(@@ -1857,7 +1944,7 @@ class ServiceBroker:async def remove_connections(self, session: AsyncSession, user_id: str- ) -> list[tuple[str, ConnectionSecret]]:+ ) -> list[tuple[str, SignIn]]:"""Delete every connection `user_id` holds here, in the caller'stransaction, and queue what was issued on them.@@ -1866,14 +1953,16 @@ class ServiceBroker:"""removed = []for connection in await self._store.list_connections(session, user_id):- secret = await self._remove_connection(session, user_id, connection.service)- if secret is not None:- removed.append((connection.service, secret))+ signed_in = await self._remove_connection(+ session, user_id, connection.service+ )+ if signed_in is not None:+ removed.append((connection.service, signed_in))return removedasync def _remove_connection(self, session: AsyncSession, user_id: str, service: str- ) -> ConnectionSecret | None:+ ) -> SignIn | None:try:await self._store.lock_connection(session, user_id, service)except ServiceConnectionBusy as error:@@ -1881,41 +1970,38 @@ class ServiceBroker:connection = await self._store.get_connection(session, user_id, service)if connection is None:return None- secret = self._secret(connection)+ signed_in = self._signed_in(connection)await self._store.queue_revocation(session,ServiceTokenIssuance.owner_id == user_id,ServiceTokenIssuance.service == service,)await self._store.delete_connection(session, user_id, service)- return secret+ return signed_inasync def finish_disconnect(self,session: AsyncSession,user_id: str,- removed: list[tuple[str, ConnectionSecret]],+ removed: list[tuple[str, SignIn]],) -> str | None:"""After the removal has committed: revoke each sign-in at its vendor,and the tokens issued on it. A warning names what could not be."""warnings = []- for service, secret in removed:+ for service, signed_in in removed:entry = self._catalog.get(service)name = service if entry is None else entry.definition.name- adapter = await self._here(service)- if adapter is None:- logger.warning(- "%s sign-in not revoked: the service is not set up on this server",- service,- )- warnings.append(- f"{name} is not set up on this server, so Switch could not "- f"revoke the sign-in. Revoke it at {name}."+ try:+ adapter = await self._revoker(+ session, service, signed_in.oauth_client_id)+ except ServiceUnavailableError as error:+ logger.warning("%s sign-in not revoked: %s", service, error)+ warnings.append(str(error))continuetry:async with asyncio.timeout(VENDOR_CALL_SECONDS):- await adapter.revoke_connection(secret)+ await adapter.revoke_connection(signed_in.secret)except ConnectionNotRevocable as error:warnings.append(str(error))except Exception as error:@@ -2047,12 +2133,18 @@ class ServiceBroker:session, conditions, now=now, until=until, limit=REVOCATION_BATCH)claimed = [(row.id, row.service, row.encrypted_token) for row in rows]+ # Once per service, before the revocations run side by side. An+ # issued token is revoked with itself, so any app's adapter will do.+ adapters = {+ service: await self._here(session, service)+ for service in {service for _, service, _ in claimed}+ }await session.commit()async def revoke(row_id: str, service: str, encrypted: str | None) -> str | None:- adapter = await self._here(service)+ adapter = adapters[service]try:if adapter is None or encrypted is None:raise ServiceUnavailableError(diff --git a/core/switch_core/connections/flows.py b/core/switch_core/connections/flows.pyindex 7e7deffa2..d98f36f2b 100644--- a/core/switch_core/connections/flows.py+++ b/core/switch_core/connections/flows.py@@ -86,6 +86,10 @@ class ServiceFlow:def service(self) -> str:return self.stored.service+ @property+ def tenant_id(self) -> str:+ return self.stored.tenant_id+@propertydef name(self) -> str:return str(self.stored.details["name"])diff --git a/core/switch_core/connections/organization_apps.py b/core/switch_core/connections/organization_apps.pyindex a2369eae4..9ac6f3a90 100644--- a/core/switch_core/connections/organization_apps.py+++ b/core/switch_core/connections/organization_apps.py@@ -1,21 +1,28 @@"""The connector apps organizations registered, as the broker uses them.A workspace whose organization saved an app for a service-(`organization_app_credentials`) is issued and refreshed through that app;-one whose organization saved none goes through the server's, as-`connections/app_credentials.py` resolves it. Only GitHub's App and the-static OAuth clients have one. All of this is behind the+(`organization_app_credentials`) signs in, is issued and is refreshed through+that app; one whose organization saved none goes through the server's, as+`connections/app_credentials.py` resolves it. All of this is behind the`organization_connector_apps` flag: with it off the broker holds no`OrganizationApps`, and no workspace looks past the server's apps.+Only the static OAuth clients are looked up here so far. GitHub's App is+not: its sign-in still goes through the App on the gateway's state, which is+the server's, so a connection made there would never match an+organization's App and would be sent back to reconnect forever.+An organization's adapter is built the first time one of its workspacesneeds it, and kept. The process that changes an organization's app forgetsthe adapter at once and announces the change (`app_signals.py`), and everyother process forgets it on hearing so. A signal can be missed, so a keptadapter is trusted for `TRUSTED_SECONDS` after its row was last read; thenext lookup after that reads the row again and rebuilds only if the row-changed. Which organization a workspace belongs to is read once and kept:-workspaces do not move between organizations.+changed. Which organization a workspace belongs to, or that it belongs to+none, is read once and kept: workspaces do not move between organizations.++The reads go through the caller's session, bound to the workspace, so a+lookup never holds a second connection while the caller holds one.An organization's app that cannot be used leaves the service unavailable inits workspaces, with the reason. It does not fall back to the server's app:@@ -32,22 +39,20 @@ from datetime import datetimefrom typing import TYPE_CHECKINGfrom sqlalchemy import select-from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker+from sqlalchemy.ext.asyncio import AsyncSessionfrom switch_core.connections.adapters import ServiceAdapterfrom switch_core.connections.app_credentials import (- UNREADABLE,AppLoadError,- AppValues,ConnectorApps,- app_from_values,+ ServerCannotUseAppError,+ cannot_work_here,+ load_saved_app,)from switch_core.db.models import OrganizationAppCredential, Tenant-from switch_core.db.session_scope import tenant_sessionfrom switch_core.db.stores.organization_app_credential_store import (OrganizationAppCredentialStore,)-from switch_core.keys import UndecryptableErrorfrom switch_core.signals import RECHECK_SECONDSif TYPE_CHECKING:@@ -62,7 +67,7 @@ TRUSTED_SECONDS = RECHECK_SECONDSdef unusable(name: str, error: str) -> str:"""What members are told about a service whose organization's app- cannot be used."""+ cannot be used, for a reason its organization admins can fix."""return (f"Your organization's {name} app can't be used: {error} An organization ""admin can fix it under Organization, Connectors."@@ -91,20 +96,17 @@ class _Kept:class OrganizationApps:def __init__(- self,- *,- apps: ConnectorApps,- store: OrganizationAppCredentialStore,- session_factory: async_sessionmaker[AsyncSession],+ self, *, apps: ConnectorApps, store: OrganizationAppCredentialStore) -> None:self._apps = appsself._store = store- self._session_factory = session_factory- self._organization_of: dict[str, str] = {}+ # Each workspace's organization; None for one in no organization.+ self._organization_of: dict[str, str | None] = {}self._kept: dict[tuple[str, str], _Kept] = {}- # Bumped by each `forget`, so a build that read the row before it- # does not keep what it read.+ # Bumped by each `forget`, and the epoch by each `forget_all`, so a+ # read begun before either does not keep what it read.self._generation: dict[tuple[str, str], int] = {}+ self._epoch = 0self._signals: ConnectorAppSignals | None = Nonedef use_signals(self, signals: ConnectorAppSignals) -> None:@@ -113,30 +115,33 @@ class OrganizationApps:self._signals = signalsasync def for_workspace(- self, tenant_id: str, service: str+ self, session: AsyncSession, tenant_id: str, service: str) -> OrganizationApp | None:"""The app `tenant_id`'s organization saved for `service`, or None- when it saved none and the server's applies."""- if service not in self._apps.specs:+ when it saved none and the server's applies. `session` is bound to+ `tenant_id`; it is read through only when nothing kept answers."""+ spec = self._apps.specs.get(service)+ if spec is None or spec.kind != "oauth_client":return None- organization_id = self._organization_of.get(tenant_id)- if organization_id is not None:+ if tenant_id in self._organization_of:+ organization_id = self._organization_of[tenant_id]+ # A workspace a process from before organizations created.+ if organization_id is None:+ return Nonekept = self._kept.get((organization_id, service))if kept is not None and time.monotonic() - kept.read_at < TRUSTED_SECONDS:return kept.app- async with tenant_session(self._session_factory, tenant_id) as session:+ else:+ organization_id = await session.scalar(+ select(Tenant.organization_id).where(Tenant.id == tenant_id)+ )+ self._organization_of[tenant_id] = organization_idif organization_id is None:- organization_id = await session.scalar(- select(Tenant.organization_id).where(Tenant.id == tenant_id)- )- # A workspace a process from before organizations created.- if organization_id is None:- return None- self._organization_of[tenant_id] = organization_id- key = (organization_id, service)- generation = self._generation.get(key, 0)- read_at = time.monotonic()- row = await self._store.get(session, organization_id, service)+ return None+ key = (organization_id, service)+ generation, epoch = self._generation.get(key, 0), self._epoch+ read_at = time.monotonic()+ row = await self._store.get(session, organization_id, service)kept = self._kept.get(key)if row is None:app = None@@ -148,7 +153,7 @@ class OrganizationApps:app = kept.appelse:app = self._build(row)- if self._generation.get(key, 0) == generation:+ if self._generation.get(key, 0) == generation and self._epoch == epoch:self._kept[key] = _Kept(app, read_at)return app@@ -167,22 +172,21 @@ class OrganizationApps:self._generation[key] = self._generation.get(key, 0) + 1def forget_all(self) -> None:- """Drop every kept adapter: after this process may have missed- announcements, as on reconnecting to the database."""- for organization_id, service in list(self._kept):- self.forget(organization_id, service)+ """Drop every kept adapter, and whatever a read under way would keep:+ after this process may have missed announcements, as on reconnecting+ to the database."""+ self._kept.clear()+ self._epoch += 1def _build(self, row: OrganizationAppCredential) -> OrganizationApp:spec = self._apps.specs[row.service]try:- try:- secrets = self._store.secrets(row)- except UndecryptableError:- raise AppLoadError(UNREADABLE) from None- github_app, oauth_client = app_from_values(+ loaded = load_saved_app(spec,- AppValues(row.client_id, dict(row.settings), secrets),- self._apps.public_url,+ client_id=row.client_id,+ settings=row.settings,+ secrets=lambda: self._store.secrets(row),+ public_url=self._apps.public_url,)except AppLoadError as error:logger.error(@@ -193,16 +197,23 @@ class OrganizationApps:spec.name,error,)+ problem = (+ cannot_work_here(spec.name)+ if isinstance(error, ServerCannotUseAppError)+ else unusable(spec.name, str(error))+ )return OrganizationApp(organization_id=row.organization_id,adapter=None,- problem=unusable(spec.name, str(error)),+ problem=problem,changed_at=row.updated_at,)return OrganizationApp(organization_id=row.organization_id,adapter=self._apps.build(- row.service, github_app=github_app, oauth_client=oauth_client+ row.service,+ github_app=loaded.github_app,+ oauth_client=loaded.oauth_client,),problem=None,changed_at=row.updated_at,diff --git a/core/switch_core/gateway/service_connections.py b/core/switch_core/gateway/service_connections.pyindex 2b5aa868b..a11cfbcb7 100644--- a/core/switch_core/gateway/service_connections.py+++ b/core/switch_core/gateway/service_connections.py@@ -22,6 +22,7 @@ from switch_core.connections.broker import (KEY_CONSENT,ServiceBroker,ServiceError,+ ServiceStanding,effective_tools,get_service_broker,)@@ -68,7 +69,9 @@ async def _owned_agent(session: AsyncSession, agent_id: str, user: User) -> Agenreturn agent-async def _grant_view(broker: ServiceBroker, agent: Agent, grant: ServiceGrant) -> dict:+async def _grant_view(+ session: AsyncSession, broker: ServiceBroker, agent: Agent, grant: ServiceGrant+) -> dict:entry = CATALOG.get(grant.service)access: AccessLevel = "write" if grant.access == "write" else "read"return {@@ -83,17 +86,19 @@ async def _grant_view(broker: ServiceBroker, agent: Agent, grant: ServiceGrant)else effective_tools(entry, access, grant.tool_mode, list(grant.tools))),"resources": dict(grant.resources),- "summary": await broker.summary(agent.display_name or agent.name, grant),+ "summary": await broker.summary(+ session, agent.display_name or agent.name, grant+ ),}-async def _status(broker: ServiceBroker, connection: ServiceConnection | None) -> str:+def _status(standing: ServiceStanding, connection: ServiceConnection | None) -> str:"""The connection's status, as a person acts on it: one made through anapp this server has since replaced needs reconnecting before anyone usesit and finds out."""if connection is None:return "not_connected"- if connection.status == "active" and await broker.client_replaced(connection):+ if connection.status == "active" and standing.client_replaced(connection):return "needs_reauthorization"return connection.status@@ -114,7 +119,8 @@ async def list_service_connections(for entry in CATALOG.values():definition = entry.definitionconnection = connections.get(definition.slug)- unavailable = await broker.unavailable(definition.slug)+ standing = await broker.standing(session, definition.slug)+ unavailable = standing.unavailableentries.append({"slug": definition.slug,@@ -123,7 +129,7 @@ async def list_service_connections("description": definition.description,"enabled": definition.enabled,"auth_type": definition.auth.type,- "connectable": await broker.connectable(definition.slug),+ "connectable": standing.connectable,"configured": unavailable is None,"unavailable_reason": Noneif unavailable is None@@ -133,9 +139,7 @@ async def list_service_connections("unavailable_code": None if unavailable is None else unavailable.code,# Whether it waits on the server's operator setting up its app# under Connectors, so a client can offer them that.- "server_setup_needed": await broker.server_setup_needed(- definition.slug- ),+ "server_setup_needed": standing.server_setup_needed,# Whether a workspace admin left it on for this workspace."workspace_enabled": definition.slug not in switched_off,# A grant hands the agent the owner's own token: it is on or@@ -157,7 +161,7 @@ async def list_service_connections(# What a person pastes to connect a service connected with a# key, and where they make one; null for any other."key": _key_form(definition),- "status": await _status(broker, connection),+ "status": _status(standing, connection),"consent": None if connection is None else connection.consent,"external_identity": (None if connection is None else connection.external_identity@@ -316,7 +320,9 @@ async def list_service_grants(agent = await _owned_agent(session, agent_id, user)grants = await STORE.list_grants(session, agent.id)return {- "grants": [await _grant_view(broker, agent, grant) for grant in grants],+ "grants": [+ await _grant_view(session, broker, agent, grant) for grant in grants+ ],"addressing_open": await _others_can_address(session, agent, user),}@@ -374,7 +380,10 @@ async def set_service_grant()except ServiceError as error:return service_refusal(error)- return {"grant": await _grant_view(broker, agent, grant), "warning": warning}+ return {+ "grant": await _grant_view(session, broker, agent, grant),+ "warning": warning,+ }@router.delete("/agents/{agent_id}/service-grants/{service}", response_model=None)diff --git a/core/switch_core/gateway/service_flows.py b/core/switch_core/gateway/service_flows.pyindex d61a62777..cbdf0a797 100644--- a/core/switch_core/gateway/service_flows.py+++ b/core/switch_core/gateway/service_flows.py@@ -17,7 +17,7 @@ from fastapi import APIRouter, Depends, HTTPException, Request, Responsefrom fastapi.responses import HTMLResponse, JSONResponse, RedirectResponsefrom pydantic import BaseModel, ConfigDict, Fieldfrom sqlalchemy import select, text-from sqlalchemy.ext.asyncio import AsyncSession+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerfrom switch_core.connections.adapters import (ServiceAdapterError,@@ -36,8 +36,9 @@ from switch_core.connections.flows import (ServiceFlows,)from switch_core.db.models import ServiceConnection, Tenant, User, require_tenant_id+from switch_core.db.session_scope import tenant_sessionfrom switch_core.gateway.auth import get_current_user-from switch_core.gateway.dependencies import get_session+from switch_core.gateway.dependencies import get_session, get_session_factoryfrom switch_core.gateway.service_connections import service_refusallogger = logging.getLogger(__name__)@@ -120,7 +121,7 @@ async def start("""Begin signing in: where to send the browser, and which way it returns."""try:await broker.require_workspace_enabled(session, service)- definition, adapter = await broker.sign_in(service)+ definition, adapter = await broker.sign_in(session, service)except ServiceError as error:return service_refusal(error)tenant = await session.get(Tenant, require_tenant_id())@@ -171,16 +172,26 @@ async def authorize(request: Request,broker: Annotated[ServiceBroker, Depends(get_service_broker)],flows: Annotated[ServiceFlows, Depends(get_service_flows)],+ session_factory: Annotated[+ async_sessionmaker[AsyncSession], Depends(get_session_factory)+ ],) -> Response:- """The browser leaves for the vendor through Core, which marks it."""- try:- definition, adapter = await broker.sign_in(service)- except ServiceError:- return _page("Connections", INTERRUPTED, 400)+ """The browser leaves for the vendor through Core, which marks it.++ Nobody is signed in here, so the workspace is the flow's: its app is+ the one the browser is sent to."""try:flow = await flows.flow(state)if flow.service != service or flow.mode != "core":raise FlowError(INTERRUPTED)+ except FlowError:+ return _page("Connections", INTERRUPTED, 400)+ async with tenant_session(session_factory, flow.tenant_id) as session:+ try:+ definition, adapter = await broker.sign_in(session, service)+ except ServiceError:+ return _page("Connections", INTERRUPTED, 400)+ try:url, nonce = await flows.vendor_url(flow, adapter)except (FlowError, ServiceAdapterError):return _page(definition.name, INTERRUPTED, 400)@@ -247,7 +258,7 @@ async def complete(Core exchanges it, reads the account and writes the connection."""flow = await _owned(flows, service, flow_id, user)try:- _, adapter = await broker.sign_in(service)+ _, adapter = await broker.sign_in(session, service)except ServiceError as error:return service_refusal(error)try:diff --git a/core/switch_core/gateway/workspace_access.py b/core/switch_core/gateway/workspace_access.pyindex ca5d484e2..66313b410 100644--- a/core/switch_core/gateway/workspace_access.py+++ b/core/switch_core/gateway/workspace_access.py@@ -16,8 +16,7 @@ from dataclasses import dataclass, fieldfrom sqlalchemy.ext.asyncio import AsyncSessionfrom switch_core.bridges.agent.protocol.agent_core import AgentCore-from switch_core.connections.adapters import ConnectionSecret-from switch_core.connections.broker import ServiceBroker+from switch_core.connections.broker import ServiceBroker, SignInfrom switch_core.connections.sign_in_flows import SignInFlowsfrom switch_core.db.stores.api_key_store import ApiKeyStore@@ -25,7 +24,7 @@ from switch_core.db.stores.api_key_store import ApiKeyStore@dataclass(frozen=True)class RevokedAccess:key_hashes: list[str]- removed_services: list[tuple[str, ConnectionSecret]] = field(repr=False)+ removed_services: list[tuple[str, SignIn]] = field(repr=False)async def revoke_workspace_access(diff --git a/core/switch_core/main.py b/core/switch_core/main.pyindex 01f0c6308..5f5364d6b 100644--- a/core/switch_core/main.py+++ b/core/switch_core/main.py@@ -1373,7 +1373,6 @@ async def run(config: SwitchConfig, process_id: str) -> None:OrganizationApps(apps=connector_apps,store=OrganizationAppCredentialStore(config.keyring),- session_factory=session_factory,)if config.organization_connector_apps_enabledelse Nonediff --git a/core/switch_core/template_runs.py b/core/switch_core/template_runs.pyindex dd22dd4dd..df6a9e159 100644--- a/core/switch_core/template_runs.py+++ b/core/switch_core/template_runs.py@@ -402,19 +402,21 @@ class TemplateAgents:))for connection in plan.connections:- unavailable = await self._broker.availability(connection.service)- if unavailable is not None:- problems.append(- Problem(- plan.slot,- "connection_unavailable",- f"{connection.service}: {unavailable}",- )- )- continueasync with tenant_session(self._protocol.session_factory, tenant_id) as session:+ unavailable = await self._broker.availability(+ session, connection.service+ )+ if unavailable is not None:+ problems.append(+ Problem(+ plan.slot,+ "connection_unavailable",+ f"{connection.service}: {unavailable}",+ )+ )+ continuetry:grants.append(await self._broker.check_grant_terms(diff --git a/core/tests/switch_core/connections/standing.py b/core/tests/switch_core/connections/standing.pynew file mode 100644index 000000000..dcd23538c--- /dev/null+++ b/core/tests/switch_core/connections/standing.py@@ -0,0 +1,29 @@+"""Asking the broker about a service the way a route does: in a session of+the bound workspace."""++from __future__ import annotations++from switch_core.connections.adapters.oauth import OAuthAdapter+from switch_core.connections.broker import ServiceBroker, ServiceStanding+from switch_core.connections.loader import ConnectionDefinition+++async def standing(broker: ServiceBroker, service: str) -> ServiceStanding:+ async with broker._session_factory() as session:+ return await broker.standing(session, service)+++async def connectable(broker: ServiceBroker, service: str) -> bool:+ return (await standing(broker, service)).connectable+++async def availability(broker: ServiceBroker, service: str) -> str | None:+ async with broker._session_factory() as session:+ return await broker.availability(session, service)+++async def sign_in(+ broker: ServiceBroker, service: str+) -> tuple[ConnectionDefinition, OAuthAdapter]:+ async with broker._session_factory() as session:+ return await broker.sign_in(session, service)diff --git a/core/tests/switch_core/connections/test_app_credentials.py b/core/tests/switch_core/connections/test_app_credentials.pyindex fd19db5d7..8aa1c12af 100644--- a/core/tests/switch_core/connections/test_app_credentials.py+++ b/core/tests/switch_core/connections/test_app_credentials.py@@ -34,6 +34,7 @@ from switch_core.db.stores.workspace_service_store import WorkspaceServiceStorefrom switch_core.keys import Keyringfrom tests.conftest import TEST_KEYRINGfrom tests.switch_core.connections.adapters.test_github import _app_files, _config+from tests.switch_core.connections.standing import connectable, sign_inPUBLIC_URL = "https://switch.example.test"GOOGLE_VARIABLE = "GOOGLE_WORKSPACE_CLIENT_CONFIG_PATH"@@ -332,7 +333,7 @@ class TestApply:apps = _apps(session_factory)await apps.load()broker = _broker(session_factory)- assert await broker.connectable("google-workspace") is False+ assert await connectable(broker, "google-workspace") is Falseawait _save(session_factory,@@ -342,15 +343,15 @@ class TestApply:)await apps.apply("google-workspace", broker=broker, state=State())- assert await broker.connectable("google-workspace") is True- _, adapter = await broker.sign_in("google-workspace")+ assert await connectable(broker, "google-workspace") is True+ _, adapter = await sign_in(broker, "google-workspace")assert isinstance(adapter, OAuthAdapter)async with session_factory() as session:await apps.store.remove(session, "google-workspace")await session.commit()await apps.apply("google-workspace", broker=broker, state=State())- assert await broker.connectable("google-workspace") is False+ assert await connectable(broker, "google-workspace") is Falseasync def test_the_saved_github_app_is_swapped_in_place(self, session_factorydiff --git a/core/tests/switch_core/connections/test_app_signals.py b/core/tests/switch_core/connections/test_app_signals.pyindex b95016cb1..2cc5a4d19 100644--- a/core/tests/switch_core/connections/test_app_signals.py+++ b/core/tests/switch_core/connections/test_app_signals.py@@ -25,6 +25,7 @@ from switch_core.connections.app_signals import ()from switch_core.connections.broker import ServiceBrokerfrom switch_core.signals import SignalBus+from tests.switch_core.connections.standing import connectablefrom tests.switch_core.connections.test_app_credentials import _apps, _broker, _saveDELIVERY_TIMEOUT = 10.0@@ -95,7 +96,7 @@ class TestAcrossProcesses:two_processes: tuple[_Process, _Process],) -> None:here, there = two_processes- assert await there.broker.connectable(GOOGLE) is False+ assert await connectable(there.broker, GOOGLE) is False# As the save route does: commit, apply here, announce.await _save(@@ -107,7 +108,7 @@ class TestAcrossProcesses:await here.apps.apply(GOOGLE, broker=here.broker, state=here.state)here.apps.announce(GOOGLE)- await _eventually(lambda: there.broker.connectable(GOOGLE))+ await _eventually(lambda: connectable(there.broker, GOOGLE))assert there.apps.effective(GOOGLE).values.client_id == "saved-client-id" # type: ignore[union-attr]async def test_a_client_removed_in_one_process_goes_from_the_other(@@ -124,7 +125,7 @@ class TestAcrossProcesses:)for process in (here, there):await process.apps.apply(GOOGLE, broker=process.broker, state=process.state)- assert await there.broker.connectable(GOOGLE) is True+ assert await connectable(there.broker, GOOGLE) is Trueasync with session_factory() as session:await here.apps.store.remove(session, GOOGLE)@@ -132,7 +133,7 @@ class TestAcrossProcesses:await here.apps.apply(GOOGLE, broker=here.broker, state=here.state)here.apps.announce(GOOGLE)- await _eventually(lambda: _not(there.broker.connectable(GOOGLE)))+ await _eventually(lambda: _not(connectable(there.broker, GOOGLE)))class TestRecheck:@@ -156,7 +157,7 @@ class TestRecheck:await process.bus.start()try:await process.signals.recheck()- await _eventually(lambda: process.broker.connectable(GOOGLE))+ await _eventually(lambda: connectable(process.broker, GOOGLE))finally:await process.bus.stop()assert await process.apps.changed_since_resolved() == []diff --git a/core/tests/switch_core/connections/test_broker.py b/core/tests/switch_core/connections/test_broker.pyindex fbe2ad85f..88360c0fc 100644--- a/core/tests/switch_core/connections/test_broker.py+++ b/core/tests/switch_core/connections/test_broker.py@@ -35,6 +35,7 @@ from switch_core.connections.broker import (Principal,ServiceBroker,ServiceError,+ SignIn,)from switch_core.connections.loader import CATALOG, CATALOG_ROOT, load_catalogfrom switch_core.connections.maintenance import maintain_once@@ -55,6 +56,7 @@ from switch_core.db.stores.workspace_service_store import WorkspaceServiceStorefrom switch_core.observability.metrics import MetricsRegistry, install, uninstallfrom tests.conftest import TEST_KEYRINGfrom tests.switch_core.connections.fake_vendor import FakeVendor+from tests.switch_core.connections.standing import availability, connectable, standingfrom tests.switch_core.connections.test_loader import (CLI_ENTRY,OAUTH_ENTRY,@@ -471,7 +473,7 @@ class TestChecks:False,)assert "not available on this server" in refused.message- assert await broker.connectable("github") is False+ assert await connectable(broker, "github") is Falseasync def test_an_owner_no_longer_in_the_workspace_is_forbidden(self, broker, session_factory@@ -756,13 +758,13 @@ class TestConnectOnly:)world = await _world(session_factory, expires_in=60)- reason = await broker.availability("github")+ reason = await availability(broker, "github")assert reason is not None and "not granted to agents" in reason- unavailable = await broker.unavailable("github")+ unavailable = (await standing(broker, "github")).unavailableassert unavailable is not None and unavailable.code == "not_fully_set_up"- assert await broker.server_setup_needed("github") is False- assert await broker.connectable("github") is True- assert await broker.connectable("asana") is False+ assert (await standing(broker, "github")).server_setup_needed is False+ assert await connectable(broker, "github") is True+ assert await connectable(broker, "asana") is Falserefused = await _refused(broker, session_factory, world.agent.id)assert (refused.status_code, refused.code, refused.retryable) == (503,@@ -842,10 +844,10 @@ class TestSwitchedOff:broker = _switched_off(session_factory, vendor, {"github": reason})world = await _world(session_factory)- assert await broker.availability("github") == reason- unavailable = await broker.unavailable("github")+ assert await availability(broker, "github") == reason+ unavailable = (await standing(broker, "github")).unavailableassert unavailable is not None and unavailable.code == "switched_off"- assert await broker.connectable("github") is False+ assert await connectable(broker, "github") is Falserefused = await _refused(broker, session_factory, world.agent.id)assert (refused.status_code, refused.code, refused.retryable) == (403,@@ -897,7 +899,7 @@ class TestSwitchedOff:self, session_factory, vendor) -> None:broker = _switched_off(session_factory, vendor, {"github": ""})- assert await broker.availability("github") == "Switched off on this server."+ assert await availability(broker, "github") == "Switched off on this server."async def test_a_service_outside_the_catalog_stops_the_server(self, session_factory, vendor@@ -1308,7 +1310,7 @@ class TestPassThrough:warning = await pt.broker.finish_disconnect(session,pt.world.owner.id,- [("unset", None)], # type: ignore[list-item]+ [("unset", SignIn(ConnectionSecret({}), None))],)assert warning is not Noneassert "unset is not set up on this server" in warning@@ -1771,4 +1773,6 @@ class TestReplacedClient:assert token.resources == RESOURCESconnection = await _connection(session_factory, world.owner.id)assert connection is not None- assert await broker.client_replaced(connection) is False+ assert (await standing(broker, connection.service)).client_replaced(+ connection+ ) is Falsediff --git a/core/tests/switch_core/connections/test_organization_apps.py b/core/tests/switch_core/connections/test_organization_apps.pyindex 6b2fd5494..8a16c8b15 100644--- a/core/tests/switch_core/connections/test_organization_apps.py+++ b/core/tests/switch_core/connections/test_organization_apps.py@@ -22,16 +22,22 @@ from sqlalchemy.pool import NullPoolfrom starlette.datastructures import Statefrom switch_core.connections import organization_apps as organization_apps_module-from switch_core.connections.app_credentials import SECRET_NAMES, ConnectorApps+from switch_core.connections.adapters import ServiceUnavailableError+from switch_core.connections.app_credentials import (+ SECRET_NAMES,+ ConnectorApps,+ cannot_work_here,+)from switch_core.connections.app_signals import (CONNECTOR_APP_CHANGED_SIGNAL,ConnectorAppSignals,)from switch_core.connections.broker import ServiceBroker, ServiceErrorfrom switch_core.connections.loader import CATALOG-from switch_core.connections.organization_apps import OrganizationApps+from switch_core.connections.organization_apps import OrganizationApp, OrganizationAppsfrom switch_core.connections.registry import ClientRegistration, build_adaptersfrom switch_core.db.models import Organization, OrganizationAppCredential, Tenant, User+from switch_core.db.session_scope import tenant_sessionfrom switch_core.db.stores.organization_app_credential_store import (OrganizationAppCredentialStore,)@@ -42,7 +48,6 @@ from switch_core.db.stores.service_connection_store import ServiceConnectionStorfrom switch_core.db.stores.workspace_service_store import WorkspaceServiceStorefrom switch_core.keys import Keyringfrom switch_core.signals import SignalBus-from switch_core.tenant_context import tenant_scopefrom tests.conftest import TEST_KEYRING, RLSHarnessfrom tests.switch_core.connections.adapters.test_github import _config@@ -104,11 +109,22 @@ async def _save(await session.commit()+class _Unread:+ """A session nothing may read through: what a lookup answered from+ memory, or for a service it does not cover, is handed."""++ def __getattr__(self, name: str) -> object:+ raise AssertionError(f"read through the session: {name}")++class _Server:"""One process: the server's apps, organizations' apps and the broker."""def __init__(- self, session_factory: async_sessionmaker[AsyncSession], tmp_path: Path+ self,+ session_factory: async_sessionmaker[AsyncSession],+ tmp_path: Path,+ public_url: str = PUBLIC_URL,) -> None:client_file = tmp_path / f"google-{uuid.uuid4().hex[:6]}.json"client_file.write_text(@@ -116,12 +132,12 @@ class _Server:{"client_id": SERVER_CLIENT, "client_secret": "example-secret-0000"}))- settings = _config(gateway_public_url=PUBLIC_URL)+ settings = _config(gateway_public_url=public_url)self.http = httpx.AsyncClient()self.registration = ClientRegistration(session_factory=session_factory,keyring=TEST_KEYRING,- public_url=PUBLIC_URL,+ public_url=public_url,server_name="switch.local",)self.apps = ConnectorApps(@@ -136,10 +152,15 @@ class _Server:self.organization_apps = OrganizationApps(apps=self.apps,store=OrganizationAppCredentialStore(TEST_KEYRING),- session_factory=session_factory,)self._session_factory = session_factory+ async def lookup(self, tenant_id: str, service: str) -> OrganizationApp | None:+ async with tenant_session(self._session_factory, tenant_id) as session:+ return await self.organization_apps.for_workspace(+ session, tenant_id, service+ )+async def start(self) -> ServiceBroker:await self.apps.load()return ServiceBroker(@@ -165,8 +186,8 @@ class _Server:async def _client_in(broker: ServiceBroker, tenant_id: str) -> str | None:"""The OAuth client `tenant_id` signs in to Google Workspace through."""- with tenant_scope(tenant_id):- _, adapter = await broker.sign_in(GOOGLE)+ async with tenant_session(broker._session_factory, tenant_id) as session:+ _, adapter = await broker.sign_in(session, GOOGLE)return adapter.current_client_id()@@ -202,29 +223,103 @@ class TestWhichApp:await _save(rls_harness.owner, organization_id, "lost-client", keyring=retired)broker = await _Server(rls_harness.restricted, tmp_path).start()- with tenant_scope(tenant_id):- unavailable = await broker.unavailable(GOOGLE)+ async with tenant_session(rls_harness.restricted, tenant_id) as session:+ standing = await broker.standing(session, GOOGLE)+ unavailable = standing.unavailableassert unavailable is not Noneassert unavailable.code == "organization_app_unusable"assert "Your organization's Google Workspace app" in unavailable.reason- assert await broker.connectable(GOOGLE) is False+ assert "An organization admin can fix it" in unavailable.reason+ assert standing.connectable is Falsewith pytest.raises(ServiceError) as refused:- await broker.sign_in(GOOGLE)+ await broker.sign_in(session, GOOGLE)assert refused.value.message == unavailable.reason- async def test_a_service_without_an_app_never_reads_the_organization(+ async def test_services_it_does_not_cover_never_read_the_organization(self, rls_harness: RLSHarness, tmp_path: Path) -> None:server = _Server(rls_harness.restricted, tmp_path)await server.start()- # No such workspace: a read would have nothing to bind.- assert (- await server.organization_apps.for_workspace("no-such", "atlassian") is None+ for service in ("atlassian", "github"):+ assert (+ await server.organization_apps.for_workspace(+ _Unread(), "no-such", service+ )+ is None+ )++ async def test_what_only_the_operator_can_fix_does_not_send_members_to_their_admin(+ self, rls_harness: RLSHarness, tmp_path: Path+ ) -> None:+ organization_id, _ = await _workspace(rls_harness.owner, in_organization=True)+ assert organization_id is not None+ async with rls_harness.owner() as session:+ user = User(name="admin", email=f"{_id('a')}@example.invalid", role="user")+ session.add(user)+ await session.flush()+ row = await OrganizationAppCredentialStore(TEST_KEYRING).save(+ session,+ organization_id=organization_id,+ service="github",+ client_id="example-client-id",+ settings={"slug": "example-app"},+ secrets={"client_secret": "s", "private_key": "k"},+ secret_names=SECRET_NAMES["github_app"],+ updated_by=user.id,+ )+ await session.commit()+ server = _Server(+ rls_harness.restricted, tmp_path, public_url="http://switch.example.test"+ )++ app = server.organization_apps._build(row)++ assert app.adapter is None+ assert app.problem == cannot_work_here("GitHub")+++class TestRevoking:+ async def test_a_sign_in_is_revoked_through_the_client_it_was_made_through(+ self, rls_harness: RLSHarness, tmp_path: Path+ ) -> None:+ organization_id, tenant_id = await _workspace(+ rls_harness.owner, in_organization=True)+ assert organization_id is not None+ await _save(rls_harness.owner, organization_id, "organization-client-id")+ broker = await _Server(rls_harness.restricted, tmp_path).start()++ async with tenant_session(rls_harness.restricted, tenant_id) as session:+ # Made through the server's app, before the organization saved its own.+ before = await broker._revoker(session, GOOGLE, SERVER_CLIENT)+ since = await broker._revoker(session, GOOGLE, "organization-client-id")+ with pytest.raises(ServiceUnavailableError) as gone:+ await broker._revoker(session, GOOGLE, "replaced-client-id")+ assert before.current_client_id() == SERVER_CLIENT+ assert since.current_client_id() == "organization-client-id"+ assert "no longer uses the Google Workspace app" in str(gone.value)class TestKept:+ async def test_what_a_workspace_belongs_to_is_read_once(+ self, rls_harness: RLSHarness, tmp_path: Path+ ) -> None:+ organization_id, in_one = await _workspace(+ rls_harness.owner, in_organization=True+ )+ _, in_none = await _workspace(rls_harness.owner, in_organization=False)+ assert organization_id is not None+ await _save(rls_harness.owner, organization_id, "organization-client-id")+ server = _Server(rls_harness.restricted, tmp_path)+ await server.start()+ first = await server.lookup(in_one, GOOGLE)+ assert await server.lookup(in_none, GOOGLE) is None++ apps = server.organization_apps+ assert await apps.for_workspace(_Unread(), in_one, GOOGLE) is first+ assert await apps.for_workspace(_Unread(), in_none, GOOGLE) is None+async def test_the_adapter_is_kept_until_the_app_changes(self, rls_harness: RLSHarness, tmp_path: Path) -> None:@@ -258,13 +353,13 @@ class TestKept:await server.start()monkeypatch.setattr(organization_apps_module, "TRUSTED_SECONDS", 0.0)- first = await server.organization_apps.for_workspace(tenant_id, GOOGLE)- unchanged = await server.organization_apps.for_workspace(tenant_id, GOOGLE)+ first = await server.lookup(tenant_id, GOOGLE)+ unchanged = await server.lookup(tenant_id, GOOGLE)assert first is not None and unchanged is first# Saved by a process whose announcement never arrived.await _save(rls_harness.owner, organization_id, "second-client")- changed = await server.organization_apps.for_workspace(tenant_id, GOOGLE)+ changed = await server.lookup(tenant_id, GOOGLE)assert changed is not None and changed.adapter is not Noneassert changed.adapter.current_client_id() == "second-client"@@ -295,8 +390,39 @@ class TestKept:return rowmonkeypatch.setattr(server.organization_apps._store, "get", read_then_change)- await server.organization_apps.for_workspace(tenant_id, GOOGLE)- await server.organization_apps.for_workspace(tenant_id, GOOGLE)+ await server.lookup(tenant_id, GOOGLE)+ await server.lookup(tenant_id, GOOGLE)+ assert reads == 2++ async def test_forgetting_everything_during_a_first_read_is_not_overwritten(+ self,+ rls_harness: RLSHarness,+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+ ) -> None:+ organization_id, tenant_id = await _workspace(+ rls_harness.owner, in_organization=True+ )+ assert organization_id is not None+ await _save(rls_harness.owner, organization_id, "first-client")+ server = _Server(rls_harness.restricted, tmp_path)+ await server.start()+ store = OrganizationAppCredentialStore(TEST_KEYRING)+ reads = 0++ async def read_then_reconnect(+ session: AsyncSession, organization: str, service: str+ ) -> OrganizationAppCredential | None:+ nonlocal reads+ reads += 1+ row = await store.get(session, organization, service)+ if reads == 1:+ server.organization_apps.forget_all()+ return row++ monkeypatch.setattr(server.organization_apps._store, "get", read_then_reconnect)+ await server.lookup(tenant_id, GOOGLE)+ await server.lookup(tenant_id, GOOGLE)assert reads == 2diff --git a/core/tests/switch_core/connections/test_registry.py b/core/tests/switch_core/connections/test_registry.pyindex c29b27e1b..fc62839cd 100644--- a/core/tests/switch_core/connections/test_registry.py+++ b/core/tests/switch_core/connections/test_registry.py@@ -31,6 +31,7 @@ from switch_core.db.stores.service_connection_store import ServiceConnectionStorfrom switch_core.db.stores.workspace_service_store import WorkspaceServiceStorefrom tests.conftest import TEST_KEYRINGfrom tests.switch_core.connections.adapters.test_github import _app_files, _config+from tests.switch_core.connections.standing import availability, connectable, standingfrom tests.switch_core.connections.test_loader import (CLI_ENTRY,OAUTH_ENTRY,@@ -219,13 +220,13 @@ async def test_an_app_not_set_up_here_says_who_sets_it_up("{} is not set up on this server. Whoever runs your Switch server can ""set it up in Switch Console, under Admin → Connector apps.")- assert await broker.availability("example") == reason.format("Example")- assert await broker.availability("github") == reason.format("GitHub")- assert await broker.connectable("example") is False+ assert await availability(broker, "example") == reason.format("Example")+ assert await availability(broker, "github") == reason.format("GitHub")+ assert await connectable(broker, "example") is Falsefor service in ("example", "github"):- unavailable = await broker.unavailable(service)+ unavailable = (await standing(broker, service)).unavailableassert unavailable is not None and unavailable.code == "app_not_set_up"- assert await broker.server_setup_needed(service) is True+ assert (await standing(broker, service)).server_setup_needed is Trueasync def test_a_service_signing_in_only_through_the_server_needs_its_public_url(@@ -233,16 +234,16 @@ async def test_a_service_signing_in_only_through_the_server_needs_its_public_url) -> None:core_only = STATIC.replace("redirect: [loopback, core]", "redirect: [core]")broker = _broker(_catalog(tmp_path, core_only), session_factory, {}, None)- example = await broker.unavailable("example")+ example = (await standing(broker, "example")).unavailableassert example is not Noneassert example.code == "needs_public_url"assert example.reason == ("Example can't be connected on this server: it needs the server's ""public address (GATEWAY_PUBLIC_URL).")- assert await broker.server_setup_needed("example") is False+ assert (await standing(broker, "example")).server_setup_needed is False# GitHub's App names its own callback, so it isn't held up the same way.- github = await broker.unavailable("github")+ github = (await standing(broker, "github")).unavailableassert github is not None and github.code == "app_not_set_up"with_url = _broker(_catalog(tmp_path / "again", core_only),@@ -250,7 +251,7 @@ async def test_a_service_signing_in_only_through_the_server_needs_its_public_url{},"https://switch.example.test",)- example = await with_url.unavailable("example")+ example = (await standing(with_url, "example")).unavailableassert example is not None and example.code == "app_not_set_up"@@ -259,20 +260,20 @@ async def test_github_cannot_work_on_a_server_without_an_https_address() -> None:catalog = _catalog(tmp_path, STATIC)broker = _broker(catalog, session_factory, {}, "http://127.0.0.1:8000")- github = await broker.unavailable("github")+ github = (await standing(broker, "github")).unavailableassert github is not Noneassert github.code == "needs_https"assert github.reason == ("GitHub can't be used on this server: it needs the server to have an ""https address.")- assert await broker.server_setup_needed("github") is False+ assert (await standing(broker, "github")).server_setup_needed is False# Google and the like take an http address back, so their app still can be.- example = await broker.unavailable("example")+ example = (await standing(broker, "example")).unavailableassert example is not None and example.code == "app_not_set_up"https = _broker(catalog, session_factory, {}, "https://switch.example.com")- github = await https.unavailable("github")+ github = (await standing(https, "github")).unavailableassert github is not None and github.code == "app_not_set_up"@@ -280,14 +281,14 @@ async def test_a_service_with_no_app_to_set_up_is_just_not_set_up(tmp_path: Path, session_factory) -> None:broker = _broker(_catalog(tmp_path, STATIC), session_factory, {}, None)- atlassian = await broker.unavailable("atlassian")+ atlassian = (await standing(broker, "atlassian")).unavailableassert atlassian is not Noneassert (atlassian.code, atlassian.reason) == ("not_set_up","Atlassian is not set up on this server.",)- assert await broker.server_setup_needed("atlassian") is False- asana = await broker.unavailable("asana")+ assert (await standing(broker, "atlassian")).server_setup_needed is False+ asana = (await standing(broker, "asana")).unavailableassert asana is not None and asana.code == "coming_soon"@@ -299,15 +300,15 @@ async def test_a_key_that_never_expires_is_neither_connected_nor_granted(catalog, github_app=None, oauth_clients={}, http=HTTP, registration=REG)broker = _broker(catalog, session_factory, adapters, None)- datadog = await broker.unavailable("datadog")+ datadog = (await standing(broker, "datadog")).unavailableassert datadog is not Noneassert (datadog.code, datadog.reason) == ("key_never_expires","Datadog keys do not expire and only Datadog can revoke them, so Switch ""does not hand them to agents.",)- assert await broker.connectable("datadog") is False- assert await broker.server_setup_needed("datadog") is False+ assert await connectable(broker, "datadog") is False+ assert (await standing(broker, "datadog")).server_setup_needed is Falseasync def test_a_replaced_adapter_is_used_at_once(@@ -326,12 +327,12 @@ async def test_a_replaced_adapter_is_used_at_once()["example"]broker.replace_adapter("example", adapter)- assert await broker.availability("example") is None- assert await broker.connectable("example") is True+ assert await availability(broker, "example") is None+ assert await connectable(broker, "example") is Truebroker.replace_adapter("example", None)- assert await broker.availability("example") is not None- assert await broker.connectable("example") is False+ assert await availability(broker, "example") is not None+ assert await connectable(broker, "example") is Falsewith pytest.raises(ValueError, match="No service named"):broker.replace_adapter("nowhere", None)diff --git a/core/tests/switch_core/gateway/test_service_flows.py b/core/tests/switch_core/gateway/test_service_flows.pyindex 178cfe351..f9ea00ed3 100644--- a/core/tests/switch_core/gateway/test_service_flows.py+++ b/core/tests/switch_core/gateway/test_service_flows.py@@ -73,6 +73,7 @@ def _world(public_url: str | None = None,disabled: dict[str, str] | None = None,workspace_gate: bool = False,+ organization_apps: object = None,) -> World:shutil.copytree(CATALOG_ROOT, root)# The vendor matches a loopback redirect's port, as Atlassian does.@@ -108,7 +109,7 @@ def _world(catalog["example"].definition, client, vendor.client())},- organization_apps=None,+ organization_apps=organization_apps, # type: ignore[arg-type]disabled=disabled or {},store=STORE,token_retention=timedelta(days=30),@@ -481,7 +482,40 @@ class TestLoopbackPorts:assert await _flow_ids(session_factory) == set()+class _AskedOrganizationApps:+ """Organizations' apps on, none saved: which workspace each lookup is for."""++ def __init__(self) -> None:+ self.tenants: list[str] = []++ async def for_workspace(+ self, session: AsyncSession, tenant_id: str, service: str+ ) -> None:+ self.tenants.append(tenant_id)++class TestCoreCallback:+ async def test_with_organizations_apps_the_browser_leaves_through_the_flows_workspace(+ self, session_factory, tmp_path+ ) -> None:+ asked = _AskedOrganizationApps()+ world = _world(+ session_factory,+ tmp_path / "catalog",+ redirect="[core]",+ public_url=PUBLIC,+ organization_apps=asked,+ )+ ada = await add_member(session_factory, "ada")+ async with world.harness.client() as client:+ started = await Console(client, ada).start()+ assert started.status_code == 200, started.text+ # The browser, signed in to nothing: the flow names the workspace.+ left = await client.get(started.json()["url"].removeprefix(PUBLIC))+ assert left.status_code == 307, left.text+ flow_tenant = (await _flow_row(session_factory, started.json()["id"])).tenant_id+ assert asked.tenants == [flow_tenant, flow_tenant]+async def test_returns_through_core_straight_to_console(self, session_factory, tmp_path) -> None: