Skip to content

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

Keep the Switch Trust settings until they change (CHOO-3667)

Abel Dantas864057261992
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8b9791612..1256ed299 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -224,6 +224,11 @@ version of their own to them without also giving them a release of their own.
group whose subgroup would move up next to a namesake is refused too, and
the dashboard now shows why. Names can no longer contain `/` or be blank.
Existing duplicates are left as they are and can be reached by id.
+- **Checking a message against Switch Trust no longer reads its settings each
+ time.** Each process keeps the settings it read until they change: saving or
+ clearing them in the gateway tells every process, so a change still takes
+ effect on the next message. Settings are read again after 30 seconds at most,
+ for a change whose signal was lost.
#### Fixed
- **On Slack, the πŸ‘€ and the activity card no longer arrive after the agent's
diff --git a/core/switch_core/gateway/app.py b/core/switch_core/gateway/app.py
index eaf360fb1..fbf34efe5 100644
--- a/core/switch_core/gateway/app.py
+++ b/core/switch_core/gateway/app.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+from collections.abc import Callable
from pathlib import Path
from fastapi import FastAPI
@@ -111,6 +112,7 @@ def create_gateway_app(
chat_service: ChatService,
connector_apps: ConnectorApps,
config: SwitchConfig,
+ trust_settings_changed: Callable[[], None] | None = None,
) -> FastAPI:
init_dependencies(
agent_store=agent_store,
@@ -136,6 +138,7 @@ def create_gateway_app(
invite_mailer=invite_mailer,
user_changes=user_changes,
config=config,
+ trust_settings_changed=trust_settings_changed,
)
app = FastAPI(title="Switch Gateway API")
diff --git a/core/switch_core/gateway/dependencies.py b/core/switch_core/gateway/dependencies.py
index 0502d1024..46c0e3d11 100644
--- a/core/switch_core/gateway/dependencies.py
+++ b/core/switch_core/gateway/dependencies.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from collections.abc import AsyncIterator
+from collections.abc import AsyncIterator, Callable
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession
@@ -63,6 +63,7 @@ def init_dependencies(
invite_mailer: InviteMailer | None,
user_changes: LocalUserChanges,
config: SwitchConfig,
+ trust_settings_changed: Callable[[], None] | None = None,
) -> None:
_state["agent_store"] = agent_store
_state["room_store"] = room_store
@@ -87,6 +88,7 @@ def init_dependencies(
_state["invite_mailer"] = invite_mailer
_state["user_changes"] = user_changes
_state["config"] = config
+ _state["trust_settings_changed"] = trust_settings_changed
async def get_session() -> AsyncIterator[AsyncSession]:
@@ -225,6 +227,15 @@ def get_config() -> SwitchConfig:
return _state["config"] # type: ignore[no-any-return]
+def trust_settings_changed() -> None:
+ """Tell every process the Switch Trust settings changed, once the change
+ has committed (`trust/signals.py`). Nothing is wired to hear it in a route
+ test."""
+ changed = _state.get("trust_settings_changed")
+ if changed is not None:
+ changed()
+
+
def get_protocol() -> AgentCore:
return _state["protocol"] # type: ignore[no-any-return]
diff --git a/core/switch_core/gateway/trust_settings.py b/core/switch_core/gateway/trust_settings.py
index 9ab01ab7c..68f5f2ca0 100644
--- a/core/switch_core/gateway/trust_settings.py
+++ b/core/switch_core/gateway/trust_settings.py
@@ -17,7 +17,11 @@ from switch_core.config import SwitchConfig
from switch_core.db.models import TrustSettings, User
from switch_core.db.stores.trust_settings_store import TrustSettingsStore
from switch_core.gateway.auth import require_admin
-from switch_core.gateway.dependencies import get_config, get_session
+from switch_core.gateway.dependencies import (
+ get_config,
+ get_session,
+ trust_settings_changed,
+)
from switch_core.gateway.schemas import (
TrustSettingsResponse,
TrustSettingsUpdateRequest,
@@ -97,6 +101,7 @@ async def update_trust_settings(
api_key_encrypted=api_key_encrypted,
)
await session.commit()
+ trust_settings_changed()
return _to_response(settings, config)
@@ -108,4 +113,5 @@ async def delete_trust_settings(
) -> TrustSettingsResponse:
await TrustSettingsStore().clear(session)
await session.commit()
+ trust_settings_changed()
return _to_response(None, config)
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index a1025feea..ff9c2073f 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -219,6 +219,7 @@ from switch_core.transport.invites import InviteBus
from switch_core.transport.membership_signals import MembershipSignals
from switch_core.transport.room_cache import RoomCacheLimits, RoomDeliveryCache
from switch_core.trust.setup import build_trust_client
+from switch_core.trust.signals import TrustSettingsSignals
from switch_core.user_changes import LocalUserChanges, SignalUserChanges
from switch_core.version import switch_core_version
@@ -785,6 +786,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:
# ── Switch Trust guardrails ──────────────────────────────────────────────
trust_client, trust_http = build_trust_client(session_factory, config)
+ trust_signals = TrustSettingsSignals(client=trust_client, bus=signal_bus)
# ── Resource service ─────────────────────────────────────────────────────
resource_service = ResourceService(
@@ -1231,6 +1233,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:
chat_service=chat_service,
connector_apps=connector_apps,
config=config,
+ trust_settings_changed=trust_signals.changed,
)
register_platforms(collab_lifecycle)
diff --git a/core/switch_core/trust/client.py b/core/switch_core/trust/client.py
index 1a5e8fc7a..7cc192252 100644
--- a/core/switch_core/trust/client.py
+++ b/core/switch_core/trust/client.py
@@ -11,6 +11,7 @@ call site has to ask whether the feature is on β€” the same shape as
from __future__ import annotations
import logging
+import time
import uuid
from dataclasses import dataclass
from typing import Any, Literal, Protocol
@@ -124,14 +125,27 @@ class NullTrustClient:
return TrustCheckResult(outcome="ok", policy_id=None, policy_name=None)
+# How long settings read for a check are kept at most. A change made through
+# the gateway reaches every process at once (`trust/signals.py`); this is for
+# one whose signal was lost.
+SETTINGS_MAX_AGE_SECONDS = 30.0
+
+
class DynamicTrustClient:
- """Reads the one server-global ``trust_settings`` row on every call.
+ """Checks with the one server-global ``trust_settings`` row, read once
+ and kept until it changes.
Unlike `HttpTrustClient`, built once at boot from fixed arguments, this
- resolves its settings fresh each check β€” so a change made through the
- gateway's settings endpoint (`gateway/trust_settings.py`) takes effect on
- the next message, with no restart. Behaves exactly like `NullTrustClient`
- when no row exists yet, or one is missing `policy_id`/`api_key_encrypted`.
+ follows the settings as they change: a change made through the gateway's
+ settings endpoint (`gateway/trust_settings.py`) makes every process read
+ the row again before its next check (`forget`, `trust/signals.py`), so it
+ takes effect on the next message, with no restart. Settings older than
+ `max_age_seconds` are read again too. Behaves exactly like
+ `NullTrustClient` when no row exists yet, or one is missing
+ `policy_id`/`api_key_encrypted`.
+
+ Every message an agent sends is checked, and reading the row for each one
+ was a transaction per message for a row that hardly ever changes.
"""
def __init__(
@@ -142,21 +156,46 @@ class DynamicTrustClient:
keyring: Keyring,
client: httpx.AsyncClient,
timeout_seconds: float,
+ max_age_seconds: float = SETTINGS_MAX_AGE_SECONDS,
) -> None:
self._session_factory = session_factory
self._store = store
self._keyring = keyring
self._client = client
self._timeout_seconds = timeout_seconds
+ self._max_age_seconds = max_age_seconds
+ # The client the settings last read make, None for no check, and when
+ # they were read. Nothing until the first check.
+ self._checker: HttpTrustClient | None = None
+ self._read_at: float | None = None
+ # Advanced by `forget`, so settings read before it are not kept.
+ self._generation = 0
+
+ def forget(self) -> None:
+ """Read the settings again before the next check: they changed."""
+ self._generation += 1
+ self._read_at = None
+ self._checker = None
async def check(
self, *, role: Role, content: str, room_id: str
) -> TrustCheckResult:
+ checker = await self._current()
+ if checker is None:
+ return TrustCheckResult(outcome="ok", policy_id=None, policy_name=None)
+ return await checker.check(role=role, content=content, room_id=room_id)
+
+ async def _current(self) -> HttpTrustClient | None:
+ read_at = self._read_at
+ if read_at is not None and time.monotonic() - read_at < self._max_age_seconds:
+ return self._checker
+ generation = self._generation
# The DB read and the decrypt below are not network calls to Switch
# Trust, so neither raises `GuardrailsCheckError` on its own β€” but a
# stuck pool or an `UndecryptableError` from a key rotated out too
# soon is exactly the kind of failure `check_message` exists to fail
# open on, so anything either raises is folded into the same error.
+ # Nothing is kept then, so the next check reads again.
try:
async with self._session_factory() as session:
settings = await self._store.get(session)
@@ -165,20 +204,23 @@ class DynamicTrustClient:
or not settings.policy_id
or not settings.api_key_encrypted
):
- return TrustCheckResult(outcome="ok", policy_id=None, policy_name=None)
- api_key = self._keyring.decrypt(settings.api_key_encrypted)
+ checker = None
+ else:
+ checker = HttpTrustClient(
+ base_url=settings.endpoint,
+ api_key=self._keyring.decrypt(settings.api_key_encrypted),
+ policy_id=settings.policy_id,
+ timeout_seconds=self._timeout_seconds,
+ client=self._client,
+ )
except Exception as error:
raise GuardrailsCheckError(
f"Could not read Switch Trust settings: {error}"
) from error
- http_client = HttpTrustClient(
- base_url=settings.endpoint,
- api_key=api_key,
- policy_id=settings.policy_id,
- timeout_seconds=self._timeout_seconds,
- client=self._client,
- )
- return await http_client.check(role=role, content=content, room_id=room_id)
+ if generation == self._generation:
+ self._checker = checker
+ self._read_at = time.monotonic()
+ return checker
class HttpTrustClient:
diff --git a/core/switch_core/trust/setup.py b/core/switch_core/trust/setup.py
index 6fcbb9323..6a7e204d7 100644
--- a/core/switch_core/trust/setup.py
+++ b/core/switch_core/trust/setup.py
@@ -13,13 +13,13 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from switch_core.config import SwitchConfig
from switch_core.db.stores.trust_settings_store import TrustSettingsStore
-from switch_core.trust.client import DynamicTrustClient, TrustClient
+from switch_core.trust.client import DynamicTrustClient
def build_trust_client(
session_factory: async_sessionmaker[AsyncSession],
config: SwitchConfig,
-) -> tuple[TrustClient, httpx.AsyncClient]:
+) -> tuple[DynamicTrustClient, httpx.AsyncClient]:
"""The trust client, and its HTTP client so `main` can close it."""
http_client = httpx.AsyncClient()
client = DynamicTrustClient(
diff --git a/core/switch_core/trust/signals.py b/core/switch_core/trust/signals.py
new file mode 100644
index 000000000..53a5a97b8
--- /dev/null
+++ b/core/switch_core/trust/signals.py
@@ -0,0 +1,37 @@
+"""Telling every process the Switch Trust settings changed.
+
+The settings are one server-global row that every check reads, and each
+process keeps what it read (`DynamicTrustClient`). The gateway route that
+changes them calls `changed` once the change has committed: this process
+forgets its settings at once, and every other process when the signal reaches
+it. A process whose listening connection was down forgets them once it is back,
+and a signal lost some other way costs at most `SETTINGS_MAX_AGE_SECONDS`.
+"""
+
+from __future__ import annotations
+
+from typing import Any
+
+from switch_core.signals import SignalBus
+from switch_core.trust.client import DynamicTrustClient
+
+TRUST_SETTINGS_CHANGED_SIGNAL = "trust.settings_changed"
+
+
+class TrustSettingsSignals:
+ def __init__(self, *, client: DynamicTrustClient, bus: SignalBus) -> None:
+ self._client = client
+ self._bus = bus
+ bus.handle(TRUST_SETTINGS_CHANGED_SIGNAL, self._on_changed)
+ bus.on_resync(self._forget)
+
+ def changed(self) -> None:
+ """The settings changed and the change has committed."""
+ self._client.forget()
+ self._bus.publish(TRUST_SETTINGS_CHANGED_SIGNAL)
+
+ def _on_changed(self, _signal: dict[str, Any]) -> None:
+ self._client.forget()
+
+ async def _forget(self) -> None:
+ self._client.forget()
diff --git a/core/tests/switch_core/gateway/test_trust_settings.py b/core/tests/switch_core/gateway/test_trust_settings.py
index 03ef9ee6d..014410b83 100644
--- a/core/tests/switch_core/gateway/test_trust_settings.py
+++ b/core/tests/switch_core/gateway/test_trust_settings.py
@@ -161,6 +161,29 @@ async def test_delete_turns_it_off(
assert get_resp.json()["enabled"] is False
+async def test_saving_and_clearing_tell_every_process(
+ session_factory: async_sessionmaker[AsyncSession],
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Every process keeps the settings it read until told they changed
+ (`trust/signals.py`), and a refused change tells nobody."""
+ told: list[str] = []
+ monkeypatch.setitem(
+ gw_deps._state, "trust_settings_changed", lambda: told.append("changed")
+ )
+ mallory = await _add_user(session_factory, name="mallory", role="user")
+ async with _client(session_factory, mallory) as client:
+ await client.put("/trust-settings", json=_CONFIGURED_BODY)
+ assert told == []
+
+ admin = await _add_user(session_factory, name="op", role="admin")
+ async with _client(session_factory, admin) as client:
+ await client.put("/trust-settings", json=_CONFIGURED_BODY)
+ await client.delete("/trust-settings")
+
+ assert told == ["changed", "changed"]
+
+
def test_a_blank_policy_id_is_treated_as_unset() -> None:
req = TrustSettingsUpdateRequest(endpoint="https://trust.example", policy_id=" ")
assert req.policy_id is None
diff --git a/core/tests/switch_core/trust/test_client.py b/core/tests/switch_core/trust/test_client.py
index 98c489321..4def22113 100644
--- a/core/tests/switch_core/trust/test_client.py
+++ b/core/tests/switch_core/trust/test_client.py
@@ -5,6 +5,7 @@ sites that use them.
import json
import uuid
+from collections.abc import Callable
from dataclasses import dataclass
import httpx
@@ -33,10 +34,20 @@ class _FakeRow:
class _FakeStore:
def __init__(self, row: _FakeRow | None) -> None:
- self._row = row
+ self.row = row
+ self.reads = 0
+ # Run as a read happens, for a change that lands while it is made.
+ self.during_read: Callable[[], None] | None = None
+ self.fail_next = False
async def get(self, session: object) -> _FakeRow | None:
- return self._row
+ self.reads += 1
+ if self.during_read is not None:
+ self.during_read()
+ if self.fail_next:
+ self.fail_next = False
+ raise RuntimeError("the pool timed out")
+ return self.row
class _FakeSession:
@@ -62,13 +73,16 @@ class _UndecryptableKeyring:
raise UndecryptableError("no key in the keyring opens this value")
-def _dynamic_client(row: _FakeRow | None, handler, keyring=None) -> DynamicTrustClient:
+def _dynamic_client(
+ row: _FakeRow | None, handler, keyring=None, store=None, **kwargs
+) -> DynamicTrustClient:
return DynamicTrustClient(
session_factory=_FakeSessionFactory(), # type: ignore[arg-type]
- store=_FakeStore(row), # type: ignore[arg-type]
+ store=store or _FakeStore(row), # type: ignore[arg-type]
keyring=keyring or _FakeKeyring(), # type: ignore[arg-type]
client=httpx.AsyncClient(transport=httpx.MockTransport(handler)),
timeout_seconds=1.0,
+ **kwargs,
)
@@ -148,6 +162,86 @@ def _client(handler) -> HttpTrustClient:
)
+def _ok(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(200, json={"outcome": "GUARDRAIL_RESULT_OUTCOME_OK"})
+
+
+_ROW = _FakeRow(
+ endpoint="https://trust.example", policy_id="pol_123", api_key_encrypted="enc:k"
+)
+
+
+async def test_dynamic_client_reads_the_settings_once_for_many_checks() -> None:
+ """Every message an agent sends is checked; the row hardly ever changes."""
+ store = _FakeStore(_ROW)
+ client = _dynamic_client(None, _ok, store=store)
+
+ for _ in range(3):
+ await client.check(role="user", content="hi", room_id="room-1")
+
+ assert store.reads == 1
+
+
+async def test_settings_forgotten_are_read_before_the_next_check() -> None:
+ """A change made through the gateway takes effect on the next message."""
+ policies: list[str | None] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ policies.append(request.headers.get("x-guardrails-policy-id"))
+ return _ok(request)
+
+ store = _FakeStore(None)
+ client = _dynamic_client(None, handler, store=store)
+ await client.check(role="user", content="hi", room_id="room-1")
+ store.row = _ROW
+
+ client.forget()
+ await client.check(role="user", content="hi", room_id="room-1")
+
+ assert (policies, store.reads) == (["pol_123"], 2)
+
+
+async def test_settings_read_while_they_changed_are_not_kept() -> None:
+ """The read may have seen the row from before the change."""
+ store = _FakeStore(_ROW)
+ client = _dynamic_client(None, _ok, store=store)
+ store.during_read = client.forget
+ await client.check(role="user", content="hi", room_id="room-1")
+ store.during_read = None
+
+ await client.check(role="user", content="hi", room_id="room-1")
+
+ assert store.reads == 2
+
+
+async def test_settings_older_than_the_most_kept_are_read_again() -> None:
+ """For a change whose signal was lost."""
+ store = _FakeStore(_ROW)
+ client = _dynamic_client(None, _ok, store=store, max_age_seconds=0.0)
+
+ await client.check(role="user", content="hi", room_id="room-1")
+ await client.check(role="user", content="hi", room_id="room-1")
+
+ assert store.reads == 2
+
+
+async def test_a_failed_read_is_read_again_by_the_next_check() -> None:
+ store = _FakeStore(_ROW)
+ client = _dynamic_client(None, _ok, store=store)
+ store.fail_next = True
+ with pytest.raises(GuardrailsCheckError):
+ await client.check(role="user", content="hi", room_id="room-1")
+
+ result = await client.check(role="user", content="hi", room_id="room-1")
+
+ assert (result.outcome, store.reads) == ("ok", 2)
+
+
@pytest.mark.asyncio
async def test_posts_the_one_message_with_the_expected_headers():
seen: dict[str, object] = {}
diff --git a/core/tests/switch_core/trust/test_signals.py b/core/tests/switch_core/trust/test_signals.py
new file mode 100644
index 000000000..7213010d6
--- /dev/null
+++ b/core/tests/switch_core/trust/test_signals.py
@@ -0,0 +1,69 @@
+"""Every process forgets the Switch Trust settings it read once they change,
+however it hears of it: from its own gateway route, from another process's
+signal, or by reconnecting after it may have missed one."""
+
+from __future__ import annotations
+
+from collections.abc import Awaitable, Callable
+from typing import Any
+
+from switch_core.trust.signals import (
+ TRUST_SETTINGS_CHANGED_SIGNAL,
+ TrustSettingsSignals,
+)
+
+
+class _Client:
+ def __init__(self) -> None:
+ self.forgotten = 0
+
+ def forget(self) -> None:
+ self.forgotten += 1
+
+
+class _Bus:
+ def __init__(self) -> None:
+ self.handlers: dict[str, Callable[[dict[str, Any]], None]] = {}
+ self.resyncs: list[Callable[[], Awaitable[None]]] = []
+ self.published: list[str] = []
+
+ def handle(self, kind: str, handler: Callable[[dict[str, Any]], None]) -> None:
+ self.handlers[kind] = handler
+
+ def on_resync(self, resync: Callable[[], Awaitable[None]]) -> None:
+ self.resyncs.append(resync)
+
+ def publish(self, kind: str, **fields: Any) -> None:
+ self.published.append(kind)
+
+
+def _signals() -> tuple[TrustSettingsSignals, _Client, _Bus]:
+ client, bus = _Client(), _Bus()
+ signals = TrustSettingsSignals(client=client, bus=bus) # type: ignore[arg-type]
+ return signals, client, bus
+
+
+def test_a_change_here_is_forgotten_here_and_sent_to_the_others() -> None:
+ signals, client, bus = _signals()
+
+ signals.changed()
+
+ assert client.forgotten == 1
+ assert bus.published == [TRUST_SETTINGS_CHANGED_SIGNAL]
+
+
+def test_a_change_elsewhere_is_forgotten_here() -> None:
+ _, client, bus = _signals()
+
+ bus.handlers[TRUST_SETTINGS_CHANGED_SIGNAL]({"kind": TRUST_SETTINGS_CHANGED_SIGNAL})
+
+ assert client.forgotten == 1
+
+
+async def test_reconnecting_forgets_what_a_lost_signal_would_have() -> None:
+ _, client, bus = _signals()
+
+ for resync in bus.resyncs:
+ await resync()
+
+ assert client.forgotten == 1