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
Per-workspace decision settings, restart-safe decisions and fallbacks
agent-runner35360ee2a7852d9
diff --git a/console/packages/plugins/src/switch-skill/SKILL.md b/console/packages/plugins/src/switch-skill/SKILL.mdindex 5bb60ee96..37f118ae4 100644--- a/console/packages/plugins/src/switch-skill/SKILL.md+++ b/console/packages/plugins/src/switch-skill/SKILL.md@@ -146,19 +146,33 @@ does, and there is no polling tool to call.`!follow-ups off` turns the two follow-ups off and `!follow-ups on` turnsthem back on. Each command on its own says what is set now.- In a room where people turned on automatic routing (`!auto-route on`, on a- server with a decision service), a message that tags nobody and that no- follow-up or assigned agent caught goes to Switch's decision service, which- picks the one agent best placed to answer it, or nobody. When it picks you,- the person's own message reaches you, and its line reads- `[Switch] <sender> posted in room <room> without tagging anyone, and Switch- picked you to answer (…)` rather than `addressed you`. Switch can pick- wrong: if the message is not really yours to answer (people talking to each- other, or something another agent covers), stay silent and post nothing.- In such a room, replies in a thread go to the decision service too, rather- than to the agent that spoke there last; the person's next channel message- after you answer still reaches you as a follow-up. `!auto-route off` turns- it off, and `!auto-route` on its own says which it is.+ In a room where people turned on automatic routing (`!auto-route on`), a+ message that tags nobody and that no follow-up or assigned agent caught goes+ to a decision service, which picks the one agent best placed to answer it,+ or nobody. When it picks you, the person's own message reaches you, and its+ line reads `[Switch] <sender> posted in room <room> without tagging anyone,+ and Switch picked you to answer (…)` rather than `addressed you`. Switch can+ pick wrong: if the message is not really yours to answer (people talking to+ each other, or something another agent covers), stay silent and post+ nothing. In such a room, replies in a thread go to the decision service too,+ rather than to the agent that spoke there last, and answering a routed+ message does not make the person's next untagged message a follow-up to you:+ it is decided again. `!auto-route off` turns it off, and `!auto-route` on its+ own says which it is and whether it can act.++ Which decision service a room uses is its workspace's choice, made by a+ workspace admin in Switch Console → Admin → Decision routing: the server's+ own default, the workspace's self-hosted endpoint, Claude Haiku, or none.+ With none (or no server default), `!auto-route on` says auto-route needs a+ decision service and changes nothing, and the room works as it does without+ it. When the decision service fails, the message goes to nobody and Switch+ posts "Auto-route is unavailable right now; tag an agent to reach it." in+ the room, at most once an hour. A room can name a triage agent for those+ messages with `!auto-route fallback @agent` (`!auto-route fallback off`+ clears it; it can't be the room's assigned agent). If you are that agent,+ such a message reaches you marked as a fallback: auto-route was unavailable,+ so it came to you as the room's triage agent. Answer it if it is yours, or+ say briefly which agent to tag.- **`room_join`** — `[Switch] <name> joined room <room>`, but only in roomswhere an operator opted you in (per-room, per-agent, off by default — set viathe `join_event_listeners` argument on `create_room` / `update_room`, or thediff --git a/console/packages/plugins/src/switch-skill/switch-skill.test.ts b/console/packages/plugins/src/switch-skill/switch-skill.test.tsindex 8f767917e..024ea4da9 100644--- a/console/packages/plugins/src/switch-skill/switch-skill.test.ts+++ b/console/packages/plugins/src/switch-skill/switch-skill.test.ts@@ -19,4 +19,13 @@ describe('switch skill', () => {expect(text).toContain('`!auto-route off`');expect(text).toMatch(/not really yours to answer.*stay silent/);});++ it("describes each workspace's decision service, the room notice and the triage agent", () => {+ const text = SWITCH_SKILL_CONTEXT.replace(/\s+/g, ' ');+ expect(text).toContain('Switch Console → Admin → Decision routing');+ expect(text).toContain('Auto-route is unavailable right now; tag an agent to reach it.');+ expect(text).toContain('`!auto-route fallback @agent`');+ expect(text).toContain('`!auto-route fallback off`');+ expect(text).toContain('came to you as the room\'s triage agent');+ });});diff --git a/console/tooling-e2e/surface/server-surface.json b/console/tooling-e2e/surface/server-surface.jsonindex 85cb5d22e..bb54da3f4 100644--- a/console/tooling-e2e/surface/server-surface.json+++ b/console/tooling-e2e/surface/server-surface.json@@ -130,6 +130,11 @@"service_connections"]},+ {+ "route": "DELETE /gateway/decision-settings",+ "auth": "session",+ "flags": []+ },{"route": "DELETE /gateway/documents/{document_id}","auth": "session",@@ -524,6 +529,11 @@"service_connections"]},+ {+ "route": "GET /gateway/decision-settings",+ "auth": "session",+ "flags": []+ },{"route": "GET /gateway/docs","auth": "none",@@ -1223,6 +1233,11 @@"service_connections"]},+ {+ "route": "POST /gateway/decision-settings/test",+ "auth": "session",+ "flags": []+ },{"route": "POST /gateway/documents","auth": "session",@@ -1577,6 +1592,11 @@"service_connections"]},+ {+ "route": "PUT /gateway/decision-settings",+ "auth": "session",+ "flags": []+ },{"route": "PUT /gateway/management/agents/{agent_id}","auth": "session",@@ -1612,6 +1632,11 @@"auth": "session","flags": []},+ {+ "route": "PUT /gateway/rooms/{room_id}/auto-route",+ "auth": "session",+ "flags": []+ },{"route": "PUT /gateway/rooms/{room_id}/group","auth": "session",diff --git a/core/switch_core/bridges/agent/commands.py b/core/switch_core/bridges/agent/commands.pyindex e2abd10e1..df01bfc8c 100644--- a/core/switch_core/bridges/agent/commands.py+++ b/core/switch_core/bridges/agent/commands.py@@ -48,11 +48,19 @@ from switch_core.db.stores.external_user_store import (ExternalUserStore,NoSuchLink,)+from switch_core.db.stores.room_store import auto_route_fallback_infrom switch_core.db.stores.session_activity_store import (RETRYABLE_TURN_STATUSES,AgentSessionActivityStore,)from switch_core.db.stores.user_store import UserStore+from switch_core.decision.room import (+ AutoRouteRefused,+ auto_route_state,+ room_agent,+ set_auto_route_fallback,+ set_auto_route_on,+)from switch_core.delivery.addressing import AddressingResolverfrom switch_core.events import CommandEventfrom switch_core.platform_access import administers_on_record@@ -900,11 +908,6 @@ _AUTO_ROUTE_FLAG_OFF = ("`!auto-route` has no effect here. Agents only get messages that mention ""them, follow-ups, or every message when they are the room's assigned agent.")-_AUTO_ROUTE_NO_ENDPOINT = (- "This Switch server has no decision service configured, so `!auto-route` "- "has no effect here and nothing was changed. Whoever runs the server sets "- "it up with `DECISION_ENDPOINT_URL`."-)_AUTO_ROUTE_ON = ("Automatic routing is **on** in this room. When a message tags nobody and ""no follow-up or assigned agent picks it up, Switch asks a decision service "@@ -917,6 +920,11 @@ _AUTO_ROUTE_OFF = ("reaches an agent only as a follow-up or through an assigned agent. Turn it ""on with `!auto-route on`.")+_AUTO_ROUTE_USAGE = (+ "Usage: `!auto-route on`, `!auto-route off`, `!auto-route` to see which it "+ "is, or `!auto-route fallback @agent` / `!auto-route fallback off` to name "+ "or clear the room's triage agent."+)async def _cmd_auto_route(@@ -926,37 +934,126 @@ async def _cmd_auto_route(_is_direct: bool,) -> None:host = cast("CommandConsumer", client)- if not host._decision_routing_enabled:+ availability = host._decision_availability+ if not availability.enabled:await _reply(client, room, event, _AUTO_ROUTE_FLAG_OFF)return- if not host._decision_endpoint_configured:- await _reply(client, room, event, _AUTO_ROUTE_NO_ENDPOINT)- returnmeta = await client._resolve_room_meta(room.room_id)if meta is None:await _reply(client, room, event, "Room not found.")return- choice = event.args.strip().lower()- if choice not in ("", "on", "off"):- await _reply(- client,- room,- event,- "Usage: `!auto-route on`, `!auto-route off`, or `!auto-route` to see "- "which it is.",- )+ words = event.args.split()+ choice = words[0].lower() if words else ""+ if choice == "fallback":+ await _auto_route_fallback(client, room, event, meta.room_id, words[1:])+ return+ if choice not in ("", "on", "off") or len(words) > 1:+ await _reply(client, room, event, _AUTO_ROUTE_USAGE)returnasync with client.session_factory() as session:+ row = await session.get(Room, meta.room_id)+ if row is None:+ await _reply(client, room, event, "Room not found.")+ returnif choice:- await client._room_store.set_auto_route_on(- session, meta.room_id, choice == "on"- )+ try:+ await set_auto_route_on(+ session, availability, client._room_store, row, choice == "on"+ )+ except AutoRouteRefused as refused:+ await _reply(client, room, event, f"{refused} Nothing was changed.")+ returnawait session.commit()- on = await client._room_store.auto_route_on(session, meta.room_id)+ await session.refresh(row)+ state = await auto_route_state(session, availability, row)+ fallback = (+ await client._agent_store.get(session, state.fallback_agent)+ if state.fallback_agent is not None+ else None+ )- await _reply(client, room, event, _AUTO_ROUTE_ON if on else _AUTO_ROUTE_OFF)+ body = _AUTO_ROUTE_ON if state.on else _AUTO_ROUTE_OFF+ if state.on and fallback is not None:+ body += (+ f" While the decision service is failing, those messages go to "+ f"**{_label(fallback)}**, the room's triage agent."+ )+ if state.on and not state.available:+ body += f" It can't act right now: {state.reason}"+ await _reply(client, room, event, body)+++async def _auto_route_fallback(+ client: AgentRecipient,+ room: RoomRef,+ event: CommandEvent,+ room_id: str,+ args: list[str],+) -> None:+ """`!auto-route fallback [@agent|off]`: show, name or clear the room's+ triage agent, which gets what auto-route could not decide because the+ decision service failed."""+ async with client.session_factory() as session:+ row = await session.get(Room, room_id)+ if row is None:+ await _reply(client, room, event, "Room not found.")+ return+ if not args:+ current = auto_route_fallback_in(row.metadata_)+ agent = (+ await client._agent_store.get(session, current)+ if current is not None+ else None+ )+ await _reply(+ client,+ room,+ event,+ f"While the decision service is failing, messages auto-route "+ f"could not decide go to **{_label(agent)}**. Clear it with "+ "`!auto-route fallback off`."+ if current is not None+ else "This room has no triage agent: while the decision service "+ "is failing, messages auto-route could not decide go to nobody. "+ "Name one with `!auto-route fallback @agent`.",+ )+ return+ if len(args) > 1:+ await _reply(client, room, event, _AUTO_ROUTE_USAGE)+ return+ handle = args[0]+ target: Agent | None = None+ if handle.lower() != "off":+ target = await room_agent(session, room_id, handle)+ if target is None:+ await _reply(+ client,+ room,+ event,+ f"No agent named `{handle.removeprefix('@')}` in this room. "+ "Run `!list-agents`.",+ )+ return+ try:+ await set_auto_route_fallback(session, client._room_store, row, target)+ except AutoRouteRefused as refused:+ await _reply(client, room, event, f"{refused} Nothing was changed.")+ return+ await session.commit()++ await _reply(+ client,+ room,+ event,+ f"**{_label(target)}** is now this room's triage agent: while the "+ "decision service is failing, messages auto-route could not decide go "+ "to it, marked as a fallback."+ if target is not None+ else "This room no longer has a triage agent: while the decision "+ "service is failing, messages auto-route could not decide go to nobody.",+ )async def _cmd_assign_agent(@@ -2346,10 +2443,13 @@ COMMANDS: list[Command] = [Command("auto-route","Let Switch pick the agent to answer messages that tag nobody, using a "- "decision service. Usage: `!auto-route on`, `!auto-route off`, or "- "`!auto-route`.",+ "decision service. Usage: `!auto-route on`, `!auto-route off`, "+ "`!auto-route`, or `!auto-route fallback @agent|off` for the agent that "+ "gets them while the decision service is failing.",_cmd_auto_route,- args_spec=(CommandArg("state", "on or off", required=False),),+ args_spec=(+ CommandArg("state", "on, off, or fallback @agent|off", required=False),+ ),admin_owned=True,),Command(diff --git a/core/switch_core/bridges/agent/protocol/message_events.py b/core/switch_core/bridges/agent/protocol/message_events.pyindex 33bc1c839..e4542a84c 100644--- a/core/switch_core/bridges/agent/protocol/message_events.py+++ b/core/switch_core/bridges/agent/protocol/message_events.py@@ -11,7 +11,7 @@ the same event whichever path produced it.from __future__ import annotations-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, Literalfrom switch_core.bridges.agent.protocol.types import (AgentEvent,@@ -52,7 +52,7 @@ def message_event(opening_message: str | None = None,sender_role: str | None = None,switch_notice: str | None = None,- routed: bool = False,+ routed_reason: Literal["decided", "fallback"] | None = None,) -> AgentEvent:"""Build the `message` event the agent receives, with any attached files.@@ -88,7 +88,8 @@ def message_event(opening_message=opening_message,sender_role=sender_role,switch_notice=switch_notice,- routed=routed,+ routed=routed_reason is not None,+ routed_reason=routed_reason,),)diff --git a/core/switch_core/bridges/agent/protocol/types.py b/core/switch_core/bridges/agent/protocol/types.pyindex 519c7e820..e0be6d0d1 100644--- a/core/switch_core/bridges/agent/protocol/types.py+++ b/core/switch_core/bridges/agent/protocol/types.py@@ -276,10 +276,14 @@ class MessagePayload(BaseModel):# its template has a new revision it was not brought fully up to; told# with every message until the agent reads about it. None for nothing.switch_notice: str | None = None- # True when the message tagged nobody and the decision service picked- # this agent to answer it (`delivery/decision.py`). Such a message is- # always addressed, so every host wakes for it.+ # True when the message tagged nobody and Switch routed it to this agent+ # (`delivery/decision.py`). Such a message is always addressed, so every+ # host wakes for it. `routed_reason` says why: "decided", the decision+ # service picked this agent; "fallback", the decision service was+ # unavailable and this agent is the room's triage agent. None when not+ # routed.routed: bool = False+ routed_reason: Literal["decided", "fallback"] | None = Noneclass CommandPayload(BaseModel):diff --git a/core/switch_core/clients/admin_messages.py b/core/switch_core/clients/admin_messages.pyindex fcb06bc49..f9c46328e 100644--- a/core/switch_core/clients/admin_messages.py+++ b/core/switch_core/clients/admin_messages.py@@ -118,6 +118,7 @@ class AdminMessageType(StrEnum):TRUST_ALERTED = "trust_alerted"TRUST_ERRORED = "trust_errored"AGENT_LIFECYCLE = "agent_lifecycle"+ AUTO_ROUTE_UNAVAILABLE = "auto_route_unavailable"def admin_extra_content(message_type: AdminMessageType | None) -> dict[str, object]:diff --git a/core/switch_core/clients/command_consumer.py b/core/switch_core/clients/command_consumer.pyindex 704e0cec9..757df42c9 100644--- a/core/switch_core/clients/command_consumer.py+++ b/core/switch_core/clients/command_consumer.py@@ -27,6 +27,7 @@ from switch_core.events import COMMAND_EVENT_TYPE, CommandEventfrom switch_core.transport import RoomRefif TYPE_CHECKING:+ from switch_core.decision.settings import DecisionAvailabilityfrom switch_core.room_service import RoomServicelogger = logging.getLogger(__name__)@@ -70,8 +71,7 @@ class CommandConsumer(Consumer[SystemActor]):deletion_requests: AgentDeletionRequests,frontend_base_url: str | None,follow_the_conversation_enabled: bool,- decision_routing_enabled: bool,- decision_endpoint_configured: bool,+ decision_availability: DecisionAvailability,) -> None:super().__init__(actor=actor)self._agent_store = agent_store@@ -93,10 +93,9 @@ class CommandConsumer(Consumer[SystemActor]):# Whether `!follow-ups` and `!assign-agent` mean anything here: with# the feature flag off they say so rather than claim a change.self._follow_the_conversation_enabled = follow_the_conversation_enabled- # Whether `!auto-route` means anything here: the feature flag, and a- # decision service to ask.- self._decision_routing_enabled = decision_routing_enabled- self._decision_endpoint_configured = decision_endpoint_configured+ # Whether `!auto-route` means anything in a room: the feature flag,+ # and a decision backend for the room's workspace.+ self._decision_availability = decision_availabilityself._frontend_base_url = (frontend_base_url.rstrip("/") if frontend_base_url else None)diff --git a/core/switch_core/clients/room_meta.py b/core/switch_core/clients/room_meta.pyindex dc74b70da..0862b80a1 100644--- a/core/switch_core/clients/room_meta.py+++ b/core/switch_core/clients/room_meta.py@@ -23,6 +23,9 @@ class RoomMeta(BaseModel):# Whether a message no rule delivers is sent to the decision service# (`!auto-route`), read with the room from its metadata.auto_route_on: bool = False+ # The agent that gets what auto-route could not decide because the+ # decision service failed (`!auto-route fallback`), if any.+ auto_route_fallback_agent_id: str | None = None# `{agent_id, body}`: what that agent says as it joins, in place of its# usual greeting (a Switch Console chat's opening message).agent_greeting: dict[str, str] | None = Nonediff --git a/core/switch_core/config.py b/core/switch_core/config.pyindex bbcba6f56..e74f68459 100644--- a/core/switch_core/config.py+++ b/core/switch_core/config.py@@ -248,12 +248,21 @@ class SwitchConfig(BaseSettings):# Decision routing (feature flag `decision_routing`): the decision service# Switch asks who should answer a message no rule delivered to anyone- # (`POST {DECISION_ENDPOINT_URL}`). With no endpoint, `!auto-route` says so- # and no message is ever sent anywhere. The key, when set, is sent as a- # bearer token.+ # (`POST {DECISION_ENDPOINT_URL}`). These are the server default, used by+ # every workspace that has not chosen a backend of its own in Switch+ # Console (`decision_settings`); with no endpoint and no choice, auto-route+ # is unavailable in that workspace and no message is sent anywhere. The+ # key, when set, is sent as a bearer token. The timeout applies to every+ # workspace's backend.decision_endpoint_url: str | None = Nonedecision_api_key: str | None = Nonedecision_timeout_seconds: float = 5.0+ # Whether a workspace may choose Haiku on Amazon Bedrock, which runs in the+ # server's decision service on the server's own AWS credentials.+ decision_bedrock_enabled: bool = False+ # A message still waiting for a decision after this long (a restart, a+ # long outage) is dropped and logged rather than answered late.+ decision_pending_max_age_seconds: int = Field(default=120, ge=1, le=3600)@field_validator("decision_endpoint_url", "decision_api_key", mode="before")@classmethoddiff --git a/core/switch_core/db/key_rotation.py b/core/switch_core/db/key_rotation.pyindex 9583ea6f1..8c1e1d95b 100644--- a/core/switch_core/db/key_rotation.py+++ b/core/switch_core/db/key_rotation.py@@ -34,6 +34,7 @@ from switch_core.db.encrypted_json import reencrypt_stale_valuesfrom switch_core.db.models import (ApiKey,CollaborationBridge,+ DecisionSettings,MachineWorkspace,MessagingInstall,OrganizationAppCredential,@@ -62,6 +63,7 @@ _ENCRYPTED_JSON_COLUMNS: tuple[tuple[type[Any], str], ...] = ()_ENCRYPTED_TEXT_COLUMNS: tuple[tuple[type[Any], str], ...] = ((ApiKey, "encrypted_key"),+ (DecisionSettings, "api_key_encrypted"),(MessagingInstall, "encrypted_bot_token"),(ProviderConnection, "encrypted_credential"),(MachineWorkspace, "enrollment_code_encrypted"),diff --git a/core/switch_core/db/models.py b/core/switch_core/db/models.pyindex e850b2aea..5dcb5e2c9 100644--- a/core/switch_core/db/models.py+++ b/core/switch_core/db/models.py@@ -10,6 +10,7 @@ from sqlalchemy import (CheckConstraint,Column,DateTime,+ Float,ForeignKey,ForeignKeyConstraint,Index,@@ -2744,6 +2745,50 @@ class TrustSettings(Base):)+# The backends a workspace may choose for decision routing+# (`decision/settings.py`): none (auto-route off in the workspace), its own+# endpoint, or Haiku run by the server's decision service.+DECISION_BACKENDS = ("none", "endpoint", "haiku_anthropic", "haiku_bedrock")+++class DecisionSettings(TenantScoped, Base):+ """A workspace's choice of decision backend for auto-route+ (`decision/settings.py`): at most one row per workspace.++ No row means the workspace uses the server default, the decision service+ in the server's configuration (`DECISION_ENDPOINT_URL`). A row with+ backend `none` turns auto-route off in the workspace even when the server+ has a default. `api_key_encrypted` is under `config.keyring`: the bearer+ token for an `endpoint`, the Anthropic key for `haiku_anthropic`, never+ set for `haiku_bedrock`. `threshold` is sent to the service as the+ confidence it needs to name an agent; None leaves it to the service.+ """++ __tablename__ = "decision_settings"+ __table_args__ = (+ PrimaryKeyConstraint("tenant_id", name="pk_decision_settings"),+ CheckConstraint(+ "backend IN ('none', 'endpoint', 'haiku_anthropic', 'haiku_bedrock')",+ name="ck_decision_settings_backend",+ ),+ CheckConstraint(+ "threshold IS NULL OR (threshold >= 0 AND threshold <= 1)",+ name="ck_decision_settings_threshold",+ ),+ )++ backend: Mapped[str] = mapped_column(Text, nullable=False)+ endpoint: Mapped[str | None] = mapped_column(Text, nullable=True)+ api_key_encrypted: Mapped[str | None] = mapped_column(Text, nullable=True)+ threshold: Mapped[float | None] = mapped_column(Float, nullable=True)+ updated_at: Mapped[datetime] = mapped_column(+ DateTime(timezone=True),+ server_default=func.now(),+ onupdate=func.now(),+ nullable=False,+ )++# ── Messages ─────────────────────────────────────────────────────────────────@@ -3129,6 +3174,48 @@ class PendingAttachmentGroup(TenantScoped, Base):deadline: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)+class PendingDecision(TenantScoped, Base):+ """A person's message waiting for the decision service to say who answers+ it (`delivery/decision.py`).++ Written by the router in the transaction that writes the message, when no+ rule delivered it to anyone in a room with auto-route on, and deleted in+ the transaction that stores the chosen agent's own event, or when the+ decision is settled some other way. Every switch-core process runs the+ worker, which claims a room's oldest row with `FOR UPDATE SKIP LOCKED` and+ a lease (`claimed_until`) rather than holding a transaction open across+ the call to the service. A row whose lease lapsed, because the process+ deciding it stopped, is claimed again. A row older than+ `DECISION_PENDING_MAX_AGE_SECONDS` is dropped rather than answered late.+ """++ __tablename__ = "pending_decisions"+ __table_args__ = (+ PrimaryKeyConstraint(+ "tenant_id", "room_id", "room_seq", name="pk_pending_decisions"+ ),+ # The worker's question: which of a tenant's rows are oldest.+ Index("ix_pending_decisions_created_at", "tenant_id", "created_at"),+ ForeignKeyConstraint(+ ["tenant_id", "room_id"],+ ["rooms.tenant_id", "rooms.id"],+ name="fk_pending_decisions_room",+ ondelete="CASCADE",+ ),+ )++ room_id: Mapped[str] = mapped_column(Text, nullable=False)+ # The message's place in its room, and its key (`transport_event_id`).+ room_seq: Mapped[int] = mapped_column(BigInteger, nullable=False)+ message_key: Mapped[str] = mapped_column(Text, nullable=False)+ created_at: Mapped[datetime] = mapped_column(+ DateTime(timezone=True), server_default=func.now(), nullable=False+ )+ claimed_until: Mapped[datetime | None] = mapped_column(+ DateTime(timezone=True), nullable=True+ )++class AgentRoomRead(TenantScoped, Base):"""The baseline for an agent's unread count in one room, and who owns it.diff --git a/core/switch_core/db/stores/decision_settings_store.py b/core/switch_core/db/stores/decision_settings_store.pynew file mode 100644index 000000000..b51eaa491--- /dev/null+++ b/core/switch_core/db/stores/decision_settings_store.py@@ -0,0 +1,77 @@+"""A workspace's choice of decision backend for auto-route (`decision_settings`).++No row means the workspace uses the server default. Every query names the+workspace it is for, so a caller resolving another room's workspace (the+decision worker) and a request bound to its own read the same way.+"""++from __future__ import annotations++from sqlalchemy import delete, func, select+from sqlalchemy.dialects.postgresql import insert+from sqlalchemy.ext.asyncio import AsyncSession++from switch_core.db.models import DecisionSettings+++class DecisionSettingsStore:+ async def get(+ self, session: AsyncSession, tenant_id: str+ ) -> DecisionSettings | None:+ result = await session.execute(+ select(DecisionSettings)+ .where(DecisionSettings.tenant_id == tenant_id)+ .execution_options(populate_existing=True)+ )+ return result.scalar_one_or_none()++ async def get_for_update(+ self, session: AsyncSession, tenant_id: str+ ) -> DecisionSettings | None:+ """The row, locked until the caller's transaction ends: a save reads+ the stored key to keep it, and two saves at once must not each keep a+ key the other replaced."""+ result = await session.execute(+ select(DecisionSettings)+ .where(DecisionSettings.tenant_id == tenant_id)+ .with_for_update()+ .execution_options(populate_existing=True)+ )+ return result.scalar_one_or_none()++ async def save(+ self,+ session: AsyncSession,+ tenant_id: str,+ *,+ backend: str,+ endpoint: str | None,+ api_key_encrypted: str | None,+ threshold: float | None,+ ) -> DecisionSettings:+ """Write the workspace's row exactly as given, replacing any there."""+ values = {+ "backend": backend,+ "endpoint": endpoint,+ "api_key_encrypted": api_key_encrypted,+ "threshold": threshold,+ }+ await session.execute(+ insert(DecisionSettings)+ .values(tenant_id=tenant_id, **values)+ .on_conflict_do_update(+ index_elements=[DecisionSettings.tenant_id],+ set_={**values, "updated_at": func.now()},+ )+ )+ await session.flush()+ saved = await self.get(session, tenant_id)+ assert saved is not None+ return saved++ async def clear(self, session: AsyncSession, tenant_id: str) -> None:+ """Back to the server default."""+ await session.execute(+ delete(DecisionSettings).where(DecisionSettings.tenant_id == tenant_id)+ )+ await session.flush()diff --git a/core/switch_core/db/stores/pending_decision_store.py b/core/switch_core/db/stores/pending_decision_store.pynew file mode 100644index 000000000..d23dc0233--- /dev/null+++ b/core/switch_core/db/stores/pending_decision_store.py@@ -0,0 +1,133 @@+"""Messages waiting for the decision service (`pending_decisions`).++The router adds a row in the transaction that writes the message; the+decision worker (`MessageRouter.decide_pending`) claims, expires and finishes+them. Every query runs in the caller's tenant-bound session.+"""++from __future__ import annotations++from dataclasses import dataclass++from sqlalchemy import delete, exists, func, select, update+from sqlalchemy.ext.asyncio import AsyncSession+from sqlalchemy.orm import aliased++from switch_core.db.models import PendingDecision+++@dataclass(frozen=True)+class Pending:+ """One claimed or expired row."""++ room_id: str+ room_seq: int+ message_key: str+++class PendingDecisionStore:+ async def add(+ self,+ session: AsyncSession,+ *,+ tenant_id: str,+ room_id: str,+ room_seq: int,+ message_key: str,+ ) -> None:+ session.add(+ PendingDecision(+ tenant_id=tenant_id,+ room_id=room_id,+ room_seq=room_seq,+ message_key=message_key,+ )+ )+ await session.flush()++ async def expire(+ self, session: AsyncSession, *, tenant_id: str, max_age_seconds: float+ ) -> list[Pending]:+ """Delete and return the rows older than `max_age_seconds`, on the+ database's clock, the one that stamped them."""+ rows = await session.execute(+ delete(PendingDecision)+ .where(+ PendingDecision.tenant_id == tenant_id,+ PendingDecision.created_at+ < func.now() - func.make_interval(0, 0, 0, 0, 0, 0, max_age_seconds),+ )+ .returning(+ PendingDecision.room_id,+ PendingDecision.room_seq,+ PendingDecision.message_key,+ )+ )+ return [Pending(*row) for row in rows.all()]++ async def claim(+ self, session: AsyncSession, *, tenant_id: str, lease_seconds: float, limit: int+ ) -> list[Pending]:+ """Lease the oldest row of up to `limit` rooms whose oldest row is+ not leased already, and return them.++ One row per room at a time keeps a room's decisions in the order of+ its messages. `SKIP LOCKED` lets every process claim at once without+ any two taking the same row; the lease outlives this transaction, so+ nothing is held open across the call to the service, and a row whose+ process stopped mid-decision is claimed again once it lapses.+ """+ earlier = aliased(PendingDecision)+ due = (+ select(PendingDecision.room_id, PendingDecision.room_seq)+ .where(+ PendingDecision.tenant_id == tenant_id,+ (PendingDecision.claimed_until.is_(None))+ | (PendingDecision.claimed_until < func.now()),+ ~exists().where(+ earlier.tenant_id == PendingDecision.tenant_id,+ earlier.room_id == PendingDecision.room_id,+ earlier.room_seq < PendingDecision.room_seq,+ ),+ )+ .order_by(PendingDecision.created_at)+ .limit(limit)+ .with_for_update(skip_locked=True, of=PendingDecision)+ .subquery()+ )+ rows = await session.execute(+ update(PendingDecision)+ .where(+ PendingDecision.tenant_id == tenant_id,+ PendingDecision.room_id == due.c.room_id,+ PendingDecision.room_seq == due.c.room_seq,+ )+ .values(+ claimed_until=func.now()+ + func.make_interval(0, 0, 0, 0, 0, 0, lease_seconds)+ )+ .returning(+ PendingDecision.room_id,+ PendingDecision.room_seq,+ PendingDecision.message_key,+ )+ .execution_options(synchronize_session=False)+ )+ return [Pending(*row) for row in rows.all()]++ async def finish(+ self, session: AsyncSession, *, tenant_id: str, pending: Pending+ ) -> bool:+ """Delete the row, and say whether it was still there: False when+ another process finished it after this one's lease lapsed, so this+ one must not deliver it again."""+ deleted = await session.scalar(+ delete(PendingDecision)+ .where(+ PendingDecision.tenant_id == tenant_id,+ PendingDecision.room_id == pending.room_id,+ PendingDecision.room_seq == pending.room_seq,+ )+ .returning(PendingDecision.room_seq)+ )+ return deleted is not Nonediff --git a/core/switch_core/db/stores/room_store.py b/core/switch_core/db/stores/room_store.pyindex 4bc972177..fc44f7838 100644--- a/core/switch_core/db/stores/room_store.py+++ b/core/switch_core/db/stores/room_store.py@@ -5,6 +5,9 @@ from typing import NamedTuplefrom sqlalchemy import (ColumnElement,+ DateTime,+ Text,+ cast,delete,exists,literal,@@ -51,6 +54,12 @@ MAIN_AGENT_KEY = "main_agent_id"# `!auto-route on`: messages no rule delivers are sent to the decision# service. Absent by default, so automatic routing is off.AUTO_ROUTE_ON_KEY = "auto_route_on"+# `!auto-route fallback @name`: the agent id that gets the messages auto-route+# could not decide because the decision service failed. Absent by default.+AUTO_ROUTE_FALLBACK_KEY = "auto_route_fallback_agent"+# When Switch last told the room auto-route is unavailable, so it says so at+# most once an hour however many processes and restarts there are.+DECISION_NOTICE_AT_KEY = "decision_notice_at"def follow_ups_enabled_in(metadata: dict | None) -> bool:@@ -69,6 +78,12 @@ def auto_route_on_in(metadata: dict | None) -> bool:return (metadata or {}).get(AUTO_ROUTE_ON_KEY) is True+def auto_route_fallback_in(metadata: dict | None) -> str | None:+ """The agent id a room's metadata names as its triage agent, if any."""+ value = (metadata or {}).get(AUTO_ROUTE_FALLBACK_KEY)+ return value if isinstance(value, str) else None++@dataclass(frozen=True)class Membership:"""A client's membership of one room, as `client_rooms` records it."""@@ -135,6 +150,58 @@ class RoomStore:session, room_id, AUTO_ROUTE_ON_KEY, True if on else None)+ async def auto_route_fallback(+ self, session: AsyncSession, room_id: str+ ) -> str | None:+ """The agent id of the room's triage agent for auto-route, if any."""+ room = await session.get(Room, room_id)+ if room is None:+ return None+ return auto_route_fallback_in(room.metadata_)++ async def set_auto_route_fallback(+ self, session: AsyncSession, room_id: str, agent_id: str | None+ ) -> None:+ """Name the room's triage agent, or none with None. Raises ValueError+ for a room that does not exist."""+ await self._set_metadata_key(+ session, room_id, AUTO_ROUTE_FALLBACK_KEY, agent_id+ )++ async def claim_decision_notice(+ self, session: AsyncSession, room_id: str, *, every_seconds: int+ ) -> bool:+ """Record that the room is being told auto-route is unavailable, and+ say whether to tell it: False when it was told less than+ `every_seconds` ago. One statement on the database's clock, so two+ processes failing at once tell the room once."""+ stamped = await session.scalar(+ update(Room)+ .where(+ Room.id == room_id,+ or_(+ Room.metadata_[DECISION_NOTICE_AT_KEY].astext.is_(None),+ Room.metadata_[DECISION_NOTICE_AT_KEY].astext.cast(+ DateTime(timezone=True)+ )+ < sa_func.now()+ - sa_func.make_interval(0, 0, 0, 0, 0, 0, every_seconds),+ ),+ )+ .values(+ metadata_=sa_func.coalesce(+ Room.metadata_, sa_func.jsonb_build_object()+ ).op("||")(+ sa_func.jsonb_build_object(+ DECISION_NOTICE_AT_KEY, cast(sa_func.now(), Text)+ )+ )+ )+ .returning(Room.id)+ .execution_options(synchronize_session=False)+ )+ return stamped is not None+async def _set_metadata_key(self, session: AsyncSession, room_id: str, key: str, value: object) -> None:diff --git a/core/switch_core/decision/client.py b/core/switch_core/decision/client.pyindex ce4bd40c2..6498c1f73 100644--- a/core/switch_core/decision/client.py+++ b/core/switch_core/decision/client.py@@ -1,10 +1,11 @@"""The decision service: asked which agent, if any, should answer a person'smessage that no rule delivered to anyone.-Switch posts the room's context to `DECISION_ENDPOINT_URL` and the service-names one of the candidate agents, or nobody. Which model decides, and how it-is asked, is the service's business: Switch depends only on the contract-below, so the model behind it can change without a Switch release.+Switch posts the room's context to the backend the room's workspace resolves+to (`decision/settings.py`) and the service names one of the candidate+agents, or nobody. Which model decides, and how it is asked, is the service's+business: Switch depends only on the contract below, so the model behind it+can change without a Switch release.Request::@@ -13,28 +14,66 @@ Request::agents: [{name, description, role}]thread: [{sender, sender_kind, text, addressed}] oldest firstrecent: [{sender, sender_kind, text, addressed}] oldest first+ confidence_threshold only when the workspace set oneResponse::- {agent, model_choice, confidence, threshold, reason?, model, latency_ms}+ {agent, model_choice, confidence, threshold, reason?, model, backend?, latency_ms}++Where it goes (a URL with no path of its own gets the service's `/decide`+route, so either the service's base URL or its `/decide` URL works):++- the server default: `DECISION_ENDPOINT_URL`, with `DECISION_API_KEY` as the+ bearer token;+- a workspace's own endpoint, with the workspace's key as the bearer token;+- Haiku: the server's decision service, told the backend per request in+ `X-Decision-Backend` (`anthropic` with the workspace's key in+ `X-Decision-Anthropic-Key`, or `bedrock`).A decision never holds up the room or breaks it, so every way of not getting-an answer means nobody answers: a timeout, a connection error, a response-other than 200, a body that is not the response above. Each is logged as a-warning naming it. A response naming an agent that was not a candidate is-the service breaking its contract, and is logged as an error.+an answer means nobody answers (or the room's triage agent, see+`delivery/decision.py`): a timeout, a connection error, a response other than+200, a body that is not the response above, or one naming an agent that was+not a candidate. Each is logged, and reported without identifiers+(`decision_failed`)."""from __future__ import annotationsimport logging-from typing import Literal+import time+from collections.abc import Mapping+from dataclasses import dataclass+from typing import TYPE_CHECKING, Literal+from urllib.parse import urlsplit, urlunsplitimport httpxfrom pydantic import BaseModel, ConfigDict, ValidationError+from switch_core.db.session_scope import tenant_session+from switch_core.decision.settings import (+ Backend,+ DecisionAvailability,+ ResolvedSettings,+ ServerDefault,+)+from switch_core.keys import UndecryptableError+from switch_core.outbound import OutboundPolicy, OutboundURLRefused+from switch_core.telemetry import emit_safely++if TYPE_CHECKING:+ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++ from switch_core.keys import Keyring+ from switch_core.telemetry import TelemetryService+logger = logging.getLogger(__name__)+# How long a workspace's resolved settings are kept before they are read+# again: short, so a save in another process applies within seconds without a+# restart. A save in this process applies at once (`forget`).+SETTINGS_CACHE_SECONDS = 5.0+class DecisionRoom(BaseModel):name: str@@ -84,89 +123,543 @@ class DecisionAnswer(BaseModel):threshold: floatreason: str | None = Nonemodel: str+ backend: str | None = Nonelatency_ms: float+FailureReason = Literal[+ "timeout",+ "unreachable",+ "error_status",+ "bad_answer",+ "key_unreadable",+ "endpoint_refused",+]+# A backend as telemetry and the log name it: the server default is told apart+# from a workspace's own endpoint.+BackendLabel = Literal["server_default", "endpoint", "haiku_anthropic", "haiku_bedrock"]++class DecisionServiceError(Exception):"""The service gave no usable answer: it could not be reached, did notanswer in time, answered with an error, or answered something else thana decision."""--class DecisionClient:- """Posts to the decision service's endpoint. One per process, over one- shared HTTP client."""+ def __init__(+ self,+ reason: FailureReason,+ message: str,+ *,+ status: int | None = None,+ body: str | None = None,+ ) -> None:+ super().__init__(message)+ self.reason: FailureReason = reason+ self.status = status+ self.body = body+++@dataclass(frozen=True)+class DecisionTarget:+ """Where one decision is posted and how."""++ url: str+ headers: Mapping[str, str]+ backend: BackendLabel+ threshold: float | None+ # A URL a workspace admin chose, reached only within the server's+ # outbound policy (`switch_core/outbound.py`). The server's own decision+ # service, which the operator set and Haiku runs in, is not.+ guarded: bool+++def decide_url(url: str) -> str:+ """Where a decision is posted: `url` itself when it names a path (the+ `/decide` route, or wherever a proxy puts it), else the service's+ `/decide` route under it, so the service's base URL works as well."""+ parts = urlsplit(url)+ if parts.path.strip("/"):+ return url+ return urlunsplit(parts._replace(path="/decide"))+++def decision_target(+ *,+ server: ServerDefault,+ source: Literal["workspace", "server_default"],+ backend: Backend,+ endpoint: str | None,+ api_key: str | None,+ threshold: float | None,+) -> DecisionTarget:+ """The request for a backend. `api_key` is the workspace's key, in the+ clear; the server default's is the server's own. Raises ValueError for a+ backend with nowhere to go (none, or Haiku without a server default)."""+ headers = {"Content-Type": "application/json"}+ if source == "server_default":+ if server.endpoint_url is None:+ raise ValueError("The server has no decision service configured.")+ if server.api_key is not None:+ headers["Authorization"] = f"Bearer {server.api_key}"+ return DecisionTarget(+ decide_url(server.endpoint_url),+ headers,+ "server_default",+ threshold,+ guarded=False,+ )+ if backend == "endpoint":+ if endpoint is None:+ raise ValueError("A self-hosted endpoint needs a URL.")+ if api_key is not None:+ headers["Authorization"] = f"Bearer {api_key}"+ return DecisionTarget(+ decide_url(endpoint), headers, "endpoint", threshold, guarded=True+ )+ if backend in ("haiku_anthropic", "haiku_bedrock"):+ if server.endpoint_url is None:+ raise ValueError(+ "Haiku runs in the server's decision service, and this server has none."+ )+ if server.api_key is not None:+ headers["Authorization"] = f"Bearer {server.api_key}"+ if backend == "haiku_anthropic":+ if api_key is None:+ raise ValueError("Haiku on the Anthropic API needs an Anthropic key.")+ headers["X-Decision-Backend"] = "anthropic"+ headers["X-Decision-Anthropic-Key"] = api_key+ else:+ headers["X-Decision-Backend"] = "bedrock"+ return DecisionTarget(+ decide_url(server.endpoint_url), headers, backend, threshold, guarded=False+ )+ raise ValueError("No decision service is chosen.")+++async def post_decision(+ client: httpx.AsyncClient,+ target: DecisionTarget,+ request: DecisionRequest,+ *,+ timeout_seconds: float,+ policy: OutboundPolicy,+) -> DecisionAnswer:+ """Post `request` to `target` and return the answer, or raise+ `DecisionServiceError` saying why there is none. Headers are never+ logged: they can carry a key.++ For a guarded target, `client` must be a guarded one+ (`guarded_async_client`), which checks the resolved addresses again and+ connects only to one it checked; the check here first makes a refusal a+ failure of its own, `endpoint_refused`."""+ url = target.url+ body = request.model_dump(mode="json")+ if target.threshold is not None:+ body["confidence_threshold"] = target.threshold+ try:+ if target.guarded:+ await policy.check_url(url)+ response = await client.post(+ url, json=body, headers=dict(target.headers), timeout=timeout_seconds+ )+ except OutboundURLRefused as error:+ raise DecisionServiceError(+ "endpoint_refused",+ f"POST {url} refused by the server's outbound policy: {error}",+ ) from error+ except httpx.TimeoutException as error:+ raise DecisionServiceError(+ "timeout",+ f"POST {url} did not answer within {timeout_seconds}s "+ f"({type(error).__name__})",+ ) from error+ except httpx.HTTPError as error:+ raise DecisionServiceError(+ "unreachable",+ f"POST {url} could not be reached: {type(error).__name__}: {error}",+ ) from error+ if response.status_code != 200:+ raise DecisionServiceError(+ "error_status",+ f"POST {url} answered {response.status_code}: {response.text[:200]}",+ status=response.status_code,+ body=response.text[:2000],+ )+ try:+ return DecisionAnswer.model_validate_json(response.content)+ except ValidationError as error:+ raise DecisionServiceError(+ "bad_answer",+ f"POST {url} answered something other than a decision: "+ f"{error.error_count()} problem(s), first: "+ f"{error.errors()[0]['loc']} {error.errors()[0]['msg']}",+ ) from error+++@dataclass(frozen=True)+class Decided:+ """The service answered: `answer.agent` is the candidate it picked, or+ None for nobody."""++ answer: DecisionAnswer+ backend: BackendLabel+ latency_ms: int+++@dataclass(frozen=True)+class Failed:+ """The backend gave no usable answer."""++ reason: FailureReason+ backend: BackendLabel+ message: str+++@dataclass(frozen=True)+class Unavailable:+ """The workspace has no backend now (it chose none since, or the server+ lost its default): nobody is asked, and it is not a failure."""++ reason: str | None+++DecisionOutcome = Decided | Failed | Unavailable+++def confidence_bucket(confidence: float | None) -> str:+ if confidence is None:+ return "none"+ if confidence < 0.5:+ return "lt_0.5"+ for upper, bucket in (+ (0.6, "0.5_0.6"),+ (0.7, "0.6_0.7"),+ (0.8, "0.7_0.8"),+ (0.9, "0.8_0.9"),+ ):+ if confidence < upper:+ return bucket+ return "0.9_1.0"+++@dataclass(frozen=True)+class TestOutcome:+ """What the admin's Test button shows: the sample's pick, or why there+ was none. `error_code` is one of `unreachable`, `timeout`,+ `unauthorized`, `key_refused`, `bad_answer`, `backend_unavailable`,+ `not_configured` and `refused` (an endpoint the server's outbound policy+ does not allow)."""++ ok: bool+ agent: str | None+ model_choice: str | None+ confidence: float | None+ latency_ms: int | None+ backend: str | None+ error_code: str | None+ error_message: str | None+++# The one decision the Test button asks about: three agents and a question+# only one of them covers. Nothing from any room.+SAMPLE_REQUEST = DecisionRequest(+ room=DecisionRoom(name="sample", instructions=None),+ message=DecisionMessage(+ sender="sam",+ text="The deploy to staging failed with a timeout. Can someone look at it?",+ thread_id=None,+ ),+ agents=[+ DecisionAgent(+ name="deploy-bot",+ description="Runs and troubleshoots deploys and the CI pipeline.",+ role=None,+ ),+ DecisionAgent(+ name="billing-bot",+ description="Answers questions about invoices, plans and payments.",+ role=None,+ ),+ DecisionAgent(+ name="docs-bot",+ description="Finds and explains the product documentation.",+ role=None,+ ),+ ],+ thread=[],+ recent=[],+)+++class DynamicDecisionClient:+ """Asks each room's workspace's decision backend, resolved per decision+ (`decision/settings.py`) through a short per-workspace cache, so a saved+ setting applies without a restart. One per process, over one shared HTTP+ client."""def __init__(self,*,- endpoint_url: str,- api_key: str | None,- timeout_seconds: float,+ session_factory: async_sessionmaker[AsyncSession],+ availability: DecisionAvailability,+ keyring: Keyring,client: httpx.AsyncClient,+ guarded_client: httpx.AsyncClient,+ outbound_policy: OutboundPolicy,+ timeout_seconds: float,+ telemetry: TelemetryService | None,+ cache_seconds: float,) -> None:- self.endpoint_url = endpoint_url- self._headers = {"Content-Type": "application/json"}- if api_key is not None:- self._headers["Authorization"] = f"Bearer {api_key}"- self._timeout_seconds = timeout_seconds+ """`client` reaches the server's own decision service; a workspace's+ endpoint is reached only through `guarded_client`, a+ `guarded_async_client` over `outbound_policy`."""+ self._session_factory = session_factory+ self.availability = availability+ self._keyring = keyringself._client = client+ self._guarded_client = guarded_client+ self.outbound_policy = outbound_policy+ self.timeout_seconds = timeout_seconds+ self._telemetry = telemetry+ self._cache_seconds = cache_seconds+ self._cache: dict[str, tuple[float, ResolvedSettings]] = {}++ @property+ def server(self) -> ServerDefault:+ return self.availability.server++ async def aclose(self) -> None:+ await self._client.aclose()+ await self._guarded_client.aclose()++ async def _post(+ self, target: DecisionTarget, request: DecisionRequest+ ) -> DecisionAnswer:+ return await post_decision(+ self._guarded_client if target.guarded else self._client,+ target,+ request,+ timeout_seconds=self.timeout_seconds,+ policy=self.outbound_policy,+ )++ async def settings(self, session: AsyncSession, tenant_id: str) -> ResolvedSettings:+ """The workspace's resolved settings, read through `session` when the+ cache has none fresh."""+ cached = self._cache.get(tenant_id)+ now = time.monotonic()+ if cached is not None and cached[0] > now:+ return cached[1]+ resolved = await self.availability.settings(session, tenant_id)+ self._cache[tenant_id] = (now + self._cache_seconds, resolved)+ return resolved++ async def available(self, session: AsyncSession, tenant_id: str) -> bool:+ """Whether the workspace's rooms route by decision: the server's flag,+ and a backend for the workspace."""+ if not self.availability.enabled:+ return False+ return (await self.settings(session, tenant_id)).has_backend++ def forget(self, tenant_id: str) -> None:+ """Drop the workspace's cached settings, after a save here."""+ self._cache.pop(tenant_id, None)async def decide(- self, request: DecisionRequest, *, room_id: str- ) -> DecisionAnswer | None:- """The service's decision about `request`, or None when there is no- usable one, which has been logged."""+ self, request: DecisionRequest, *, tenant_id: str, room_id: str+ ) -> DecisionOutcome:+ """Ask the workspace's backend about `request`. A failure is logged+ here; so is an answer naming an agent that was not a candidate, which+ counts as a bad answer."""+ async with tenant_session(self._session_factory, tenant_id) as session:+ settings = await self.settings(session, tenant_id)+ if not self.availability.enabled or not settings.has_backend:+ return Unavailable(reason=settings.reason)+ label: BackendLabel = (+ "server_default"+ if settings.source == "server_default"+ else settings.backend # type: ignore[assignment]+ )try:- answer = await self._ask(request)- except DecisionServiceError as error:- logger.warning(- "Decision service gave no answer for a message in room %s, so "- "no agent receives it: %s",+ api_key = (+ self._keyring.decrypt(settings.api_key_encrypted)+ if settings.api_key_encrypted is not None+ else None+ )+ except UndecryptableError:+ return self._failed(room_id,- error,+ label,+ "key_unreadable",+ "the workspace's decision key is encrypted under a secret this "+ "server no longer holds; a workspace admin has to save it again",)- return None+ target = decision_target(+ server=self.server,+ source=settings.source,+ backend=settings.backend,+ endpoint=settings.endpoint,+ api_key=api_key,+ threshold=settings.threshold,+ )+ started = time.monotonic()+ try:+ answer = await self._post(target, request)+ except DecisionServiceError as error:+ return self._failed(room_id, label, error.reason, str(error))+ latency_ms = round((time.monotonic() - started) * 1000)candidates = [agent.name for agent in request.agents]if answer.agent is not None and answer.agent not in candidates:logger.error(- "Decision service at %s named %r for a message in room %s, which "+ "Decision service (%s) named %r for a message in room %s, which ""is not one of the candidates %s; no agent receives it",- self.endpoint_url,+ label,answer.agent,room_id,candidates,)- return None- return answer+ emit_safely(+ self._telemetry,+ "decision_failed",+ {"backend": label, "reason": "bad_answer"},+ )+ return Failed(+ reason="bad_answer",+ backend=label,+ message=f"named {answer.agent!r}, not a candidate",+ )+ emit_safely(+ self._telemetry,+ "decision_made",+ {+ "backend": label,+ "outcome": "agent" if answer.agent is not None else "none",+ "confidence": confidence_bucket(answer.confidence),+ "latency_ms": latency_ms,+ },+ )+ return Decided(answer=answer, backend=label, latency_ms=latency_ms)++ def _failed(+ self, room_id: str, label: BackendLabel, reason: FailureReason, message: str+ ) -> Failed:+ logger.warning(+ "Decision service (%s) gave no answer for a message in room %s: %s",+ label,+ room_id,+ message,+ )+ emit_safely(+ self._telemetry, "decision_failed", {"backend": label, "reason": reason}+ )+ return Failed(reason=reason, backend=label, message=message)++ async def test(+ self,+ *,+ backend: Backend | Literal["default"],+ endpoint: str | None,+ api_key: str | None,+ threshold: float | None,+ ) -> TestOutcome:+ """Ask `backend` about the sample decision, with unsaved values, and+ say what it picked or why it could not. `api_key` is in the clear.+ `default` is the server default, asked with the server's own key;+ `api_key` is ignored for it."""++ def error(code: str, message: str, latency: int | None = None) -> TestOutcome:+ return TestOutcome(+ ok=False,+ agent=None,+ model_choice=None,+ confidence=None,+ latency_ms=latency,+ backend=backend,+ error_code=code,+ error_message=message,+ )- async def _ask(self, request: DecisionRequest) -> DecisionAnswer:- url = self.endpoint_url- try:- response = await self._client.post(- url,- json=request.model_dump(mode="json"),- headers=self._headers,- timeout=self._timeout_seconds,+ if backend == "haiku_bedrock" and not self.server.haiku_bedrock:+ return error(+ "backend_unavailable",+ "Haiku on Bedrock isn't offered on this server: it needs the "+ "server's decision service and DECISION_BEDROCK_ENABLED.",)- except httpx.TimeoutException as error:- raise DecisionServiceError(- f"POST {url} did not answer within {self._timeout_seconds}s "- f"({type(error).__name__})"- ) from error- except httpx.HTTPError as error:- raise DecisionServiceError(- f"POST {url} could not be reached: {type(error).__name__}: {error}"- ) from error- if response.status_code != 200:- raise DecisionServiceError(- f"POST {url} answered {response.status_code}: {response.text[:200]}"+ try:+ target = (+ decision_target(+ server=self.server,+ source="server_default",+ backend="endpoint",+ endpoint=None,+ api_key=None,+ threshold=threshold,+ )+ if backend == "default"+ else decision_target(+ server=self.server,+ source="workspace",+ backend=backend,+ endpoint=endpoint,+ api_key=api_key,+ threshold=threshold,+ ))+ except ValueError as refused:+ return error("not_configured", str(refused))+ started = time.monotonic()try:- return DecisionAnswer.model_validate_json(response.content)- except ValidationError as error:- raise DecisionServiceError(- f"POST {url} answered something other than a decision: "- f"{error.error_count()} problem(s), first: "- f"{error.errors()[0]['loc']} {error.errors()[0]['msg']}"- ) from error+ answer = await self._post(target, SAMPLE_REQUEST)+ except DecisionServiceError as failure:+ latency = round((time.monotonic() - started) * 1000)+ code = _test_error_code(backend, failure)+ return error(code, _test_error_message(code, failure), latency)+ return TestOutcome(+ ok=True,+ agent=answer.agent,+ model_choice=answer.model_choice,+ confidence=answer.confidence,+ latency_ms=round((time.monotonic() - started) * 1000),+ backend=backend,+ error_code=None,+ error_message=None,+ )+++def _test_error_code(+ backend: Backend | Literal["default"], failure: DecisionServiceError+) -> str:+ if failure.reason == "endpoint_refused":+ return "refused"+ if failure.reason in ("timeout", "unreachable", "bad_answer"):+ return failure.reason+ status = failure.status+ body = (failure.body or "").lower()+ if status in (401, 403):+ return "unauthorized"+ if status == 400 and "backend_unavailable" in body:+ return "backend_unavailable"+ if (+ backend == "haiku_anthropic"+ and status == 502+ and any(+ word in body for word in ("401", "auth", "api key", "api_key", "x-api-key")+ )+ ):+ return "key_refused"+ return "bad_answer"+++def _test_error_message(code: str, failure: DecisionServiceError) -> str:+ return {+ "timeout": f"The decision service did not answer in time. {failure}",+ "unreachable": f"The decision service could not be reached. {failure}",+ "unauthorized": f"The decision service refused the key. {failure}",+ "refused": (+ "Switch won't connect to that address: a workspace's endpoint must "+ "be a public address, or a private host the server's operator lists "+ f"in OUTBOUND_ALLOWED_PRIVATE_HOSTS. {failure}"+ ),+ "key_refused": f"The Anthropic API refused the key. {failure}",+ "backend_unavailable": f"The decision service can't run that backend. {failure}",+ }.get(code, f"The decision service did not answer with a decision. {failure}")diff --git a/core/switch_core/decision/room.py b/core/switch_core/decision/room.pynew file mode 100644index 000000000..627b4217a--- /dev/null+++ b/core/switch_core/decision/room.py@@ -0,0 +1,121 @@+"""A room's auto-route switch and triage agent, one implementation for the+room detail, the room toggle (`PUT /rooms/{id}/auto-route`) and+`!auto-route`.++The switch is room metadata `auto_route_on`, the triage agent+`auto_route_fallback_agent` (an agent id). Whether auto-route can act in the+room is the workspace's (`decision/settings.py`).+"""++from __future__ import annotations++from dataclasses import dataclass+from typing import TYPE_CHECKING++from sqlalchemy import func, select++from switch_core.db.models import Agent, Room, room_agents+from switch_core.db.stores.room_store import (+ RoomStore,+ auto_route_fallback_in,+ auto_route_on_in,+ main_agent_in,+)++if TYPE_CHECKING:+ from sqlalchemy.ext.asyncio import AsyncSession++ from switch_core.decision.settings import DecisionAvailability+++@dataclass(frozen=True)+class AutoRouteState:+ """`on` is the room's switch; `available` whether auto-route can act in+ the room, and `reason` why not; `fallback_agent` the triage agent's id,+ and `fallback_agent_name` its name, for display."""++ on: bool+ available: bool+ reason: str | None+ fallback_agent: str | None+ fallback_agent_name: str | None+++class AutoRouteRefused(ValueError):+ """A change to the room's auto-route that cannot be made; the message+ says why, for the person who asked."""+++async def auto_route_state(+ session: AsyncSession, availability: DecisionAvailability, room: Room+) -> AutoRouteState:+ checked = await availability.check(session, room.tenant_id)+ fallback_id = auto_route_fallback_in(room.metadata_)+ fallback = await session.get(Agent, fallback_id) if fallback_id else None+ return AutoRouteState(+ on=auto_route_on_in(room.metadata_),+ available=checked.available,+ reason=checked.reason,+ fallback_agent=fallback_id,+ fallback_agent_name=fallback.name if fallback is not None else None,+ )+++async def set_auto_route_on(+ session: AsyncSession,+ availability: DecisionAvailability,+ room_store: RoomStore,+ room: Room,+ on: bool,+) -> None:+ """Turn the room's auto-route on or off. Turning it on is refused where+ it cannot act, so nobody is told it is working when it is not; turning it+ off always works."""+ if on:+ checked = await availability.check(session, room.tenant_id)+ if not checked.available:+ raise AutoRouteRefused(checked.reason or "Auto-route is unavailable here.")+ await room_store.set_auto_route_on(session, room.id, on)+++async def room_agent(session: AsyncSession, room_id: str, handle: str) -> Agent | None:+ """The agent in the room going by `handle`, ignoring case: its id, its+ name or its alias here, with or without a leading `@`."""+ handle = handle.removeprefix("@").lower()+ result = await session.execute(+ select(Agent)+ .join(room_agents, room_agents.c.agent_id == Agent.id)+ .where(+ room_agents.c.room_id == room_id,+ (func.lower(Agent.id) == handle)+ | (func.lower(Agent.name) == handle)+ | (func.lower(room_agents.c.alias) == handle),+ )+ .limit(1)+ )+ return result.scalar_one_or_none()+++async def set_auto_route_fallback(+ session: AsyncSession, room_store: RoomStore, room: Room, agent: Agent | None+) -> None:+ """Name the room's triage agent, or none. It must be in the room, and it+ cannot be the room's assigned agent, which already gets every untagged+ message before auto-route is asked."""+ if agent is not None:+ member = await session.scalar(+ select(room_agents.c.agent_id).where(+ room_agents.c.room_id == room.id, room_agents.c.agent_id == agent.id+ )+ )+ if member is None:+ raise AutoRouteRefused(f"{agent.name} is not in this room.")+ if main_agent_in(room.metadata_) == agent.id:+ raise AutoRouteRefused(+ f"{agent.name} is this room's assigned agent, so it already gets "+ "every message that tags nobody; a triage agent has to be "+ "another agent."+ )+ await room_store.set_auto_route_fallback(+ session, room.id, agent.id if agent is not None else None+ )diff --git a/core/switch_core/decision/settings.py b/core/switch_core/decision/settings.pynew file mode 100644index 000000000..021d4f572--- /dev/null+++ b/core/switch_core/decision/settings.py@@ -0,0 +1,187 @@+"""Which decision backend a workspace's rooms use, and whether auto-route is+available there.++A workspace chooses in Switch Console (`gateway/decision_settings.py`, stored+in `decision_settings`), or leaves it to the server default: the decision+service in the server's configuration (`DECISION_ENDPOINT_URL`, its key and+timeout). The choices:++- `none`: auto-route is off in the workspace, even with a server default.+- `endpoint`: the workspace's own decision service, with an optional bearer+ key.+- `haiku_anthropic`: Claude Haiku through the Anthropic API, with the+ workspace's key. It runs in the server's decision service, which is told+ the backend and key per request, so it needs a server default.+- `haiku_bedrock`: Claude Haiku on Amazon Bedrock, on the server's own AWS+ credentials, also in the server's decision service: offered only when the+ operator sets `DECISION_BEDROCK_ENABLED`.++Auto-route is available in a workspace when the `decision_routing` flag is+on and the workspace resolves to a backend other than none. The room detail,+the room toggle and `!auto-route` all ask `DecisionAvailability`.+"""++from __future__ import annotations++from dataclasses import dataclass+from typing import TYPE_CHECKING, Literal++from switch_core.db.stores.decision_settings_store import DecisionSettingsStore++if TYPE_CHECKING:+ from sqlalchemy.ext.asyncio import AsyncSession++ from switch_core.config import SwitchConfig+ from switch_core.db.models import DecisionSettings++Backend = Literal["none", "endpoint", "haiku_anthropic", "haiku_bedrock"]+Source = Literal["workspace", "server_default"]++FLAG_OFF_REASON = (+ "Auto-route isn't switched on for this Switch server. Whoever runs the "+ "server turns it on with the decision_routing feature flag."+)+NO_BACKEND_REASON = (+ "Auto-route needs a decision service. A workspace admin can set one up in "+ "Switch Console → Admin → Decision routing."+)+WORKSPACE_OFF_REASON = (+ "This workspace's admins turned auto-route off by choosing no decision "+ "service. A workspace admin can choose one in Switch Console → Admin → "+ "Decision routing."+)+++@dataclass(frozen=True)+class ServerDefault:+ """The decision service in the server's configuration: every workspace+ that has chosen nothing uses it, and Haiku runs in it."""++ endpoint_url: str | None+ api_key: str | None+ bedrock_enabled: bool++ @classmethod+ def from_config(cls, config: SwitchConfig) -> ServerDefault:+ return cls(+ endpoint_url=config.decision_endpoint_url,+ api_key=config.decision_api_key,+ bedrock_enabled=config.decision_bedrock_enabled,+ )++ @property+ def configured(self) -> bool:+ return self.endpoint_url is not None++ @property+ def haiku_anthropic(self) -> bool:+ """Whether a workspace may choose Haiku on the Anthropic API."""+ return self.configured++ @property+ def haiku_bedrock(self) -> bool:+ """Whether a workspace may choose Haiku on Bedrock."""+ return self.configured and self.bedrock_enabled+++@dataclass(frozen=True)+class ResolvedSettings:+ """The backend a workspace's decisions go to, and where they come from.++ `api_key_encrypted` is the workspace's stored key, still encrypted; the+ server default's key is the server's own (`ServerDefault.api_key`).+ `backend` is `none` when nothing applies, whether chosen or for want of a+ server default; `reason` then says which.+ """++ source: Source+ backend: Backend+ endpoint: str | None+ api_key_encrypted: str | None+ threshold: float | None+ reason: str | None++ @property+ def has_backend(self) -> bool:+ return self.backend != "none"+++def resolve(row: DecisionSettings | None, server: ServerDefault) -> ResolvedSettings:+ """What applies to a workspace whose row is `row` (None: no choice)."""+ if row is None:+ if not server.configured:+ return ResolvedSettings(+ source="server_default",+ backend="none",+ endpoint=None,+ api_key_encrypted=None,+ threshold=None,+ reason=NO_BACKEND_REASON,+ )+ return ResolvedSettings(+ source="server_default",+ backend="endpoint",+ endpoint=server.endpoint_url,+ api_key_encrypted=None,+ threshold=None,+ reason=None,+ )+ backend: Backend = row.backend # type: ignore[assignment]+ reason: str | None = None+ if backend == "none":+ reason = WORKSPACE_OFF_REASON+ elif backend in ("haiku_anthropic", "haiku_bedrock") and not server.configured:+ # Saved while the server had a decision service, which has since+ # gone: Haiku has nowhere to run.+ backend, reason = "none", NO_BACKEND_REASON+ elif backend == "haiku_bedrock" and not server.bedrock_enabled:+ backend, reason = "none", NO_BACKEND_REASON+ return ResolvedSettings(+ source="workspace",+ backend=backend,+ endpoint=row.endpoint if backend == "endpoint" else None,+ api_key_encrypted=row.api_key_encrypted,+ threshold=row.threshold,+ reason=reason,+ )+++@dataclass(frozen=True)+class Availability:+ """Whether auto-route can act in a workspace's rooms, and if not, why and+ who can fix it."""++ available: bool+ reason: str | None+++def availability(enabled: bool, settings: ResolvedSettings) -> Availability:+ if not enabled:+ return Availability(available=False, reason=FLAG_OFF_REASON)+ if not settings.has_backend:+ return Availability(available=False, reason=settings.reason)+ return Availability(available=True, reason=None)+++@dataclass(frozen=True)+class DecisionAvailability:+ """The one availability check, for the room detail, the room toggle and+ `!auto-route`: the server's flag and default, and the workspace's row."""++ enabled: bool+ server: ServerDefault+ store: DecisionSettingsStore++ @classmethod+ def from_config(cls, config: SwitchConfig) -> DecisionAvailability:+ return cls(+ enabled=config.decision_routing_enabled,+ server=ServerDefault.from_config(config),+ store=DecisionSettingsStore(),+ )++ async def settings(self, session: AsyncSession, tenant_id: str) -> ResolvedSettings:+ return resolve(await self.store.get(session, tenant_id), self.server)++ async def check(self, session: AsyncSession, tenant_id: str) -> Availability:+ return availability(self.enabled, await self.settings(session, tenant_id))diff --git a/core/switch_core/decision/setup.py b/core/switch_core/decision/setup.pyindex 1e749fb01..43871716f 100644--- a/core/switch_core/decision/setup.py+++ b/core/switch_core/decision/setup.py@@ -3,40 +3,52 @@from __future__ import annotationsimport logging+from typing import TYPE_CHECKINGimport httpx-from switch_core.config import SwitchConfig-from switch_core.decision.client import DecisionClient+from switch_core.decision.client import SETTINGS_CACHE_SECONDS, DynamicDecisionClient+from switch_core.decision.settings import DecisionAvailabilityfrom switch_core.feature_flags import DECISION_ROUTING+from switch_core.outbound import guarded_async_client++if TYPE_CHECKING:+ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++ from switch_core.config import SwitchConfig+ from switch_core.telemetry import TelemetryServicelogger = logging.getLogger(__name__)def build_decision_client(config: SwitchConfig,-) -> tuple[DecisionClient, httpx.AsyncClient] | None:- """The decision client, and its HTTP client so `main` can close it; None- when this server does not route by decision, because the flag is off or- no endpoint is configured. The second is said in the log, since a room's- `!auto-route` is then refused."""- if not config.decision_routing_enabled:- return None- if config.decision_endpoint_url is None:+ session_factory: async_sessionmaker[AsyncSession],+ telemetry: TelemetryService | None,+) -> DynamicDecisionClient:+ """The decision client; `main` closes it (`aclose`). A workspace's own+ endpoint is reached through the server's outbound policy+ (`OUTBOUND_ALLOWED_PRIVATE_HOSTS`), the server's decision service directly.++ Built whether or not the `decision_routing` flag is on: workspace admins+ can set up and test a backend before the flag goes on. Only with it on is+ the client handed to the router."""+ if config.decision_routing_enabled and config.decision_endpoint_url is None:logger.warning(- "The %r feature flag is on but DECISION_ENDPOINT_URL is not set: no "- "message is routed by decision, and `!auto-route` says the server "- "has no decision service.",+ "The %r feature flag is on but DECISION_ENDPOINT_URL is not set: "+ "there is no server default, so auto-route is unavailable in every "+ "workspace whose admins have not chosen a decision service in "+ "Switch Console (Admin → Decision routing), and Haiku is not offered.",DECISION_ROUTING,)- return None- http_client = httpx.AsyncClient()- return (- DecisionClient(- endpoint_url=config.decision_endpoint_url,- api_key=config.decision_api_key,- timeout_seconds=config.decision_timeout_seconds,- client=http_client,- ),- http_client,+ return DynamicDecisionClient(+ session_factory=session_factory,+ availability=DecisionAvailability.from_config(config),+ keyring=config.keyring,+ client=httpx.AsyncClient(),+ guarded_client=guarded_async_client(config.outbound_policy),+ outbound_policy=config.outbound_policy,+ timeout_seconds=config.decision_timeout_seconds,+ telemetry=telemetry,+ cache_seconds=SETTINGS_CACHE_SECONDS,)diff --git a/core/switch_core/delivery/conversation.py b/core/switch_core/delivery/conversation.pyindex 1eb193c14..4206c34d1 100644--- a/core/switch_core/delivery/conversation.py+++ b/core/switch_core/delivery/conversation.py@@ -307,6 +307,12 @@ class RoomConversation:self._channel_rows: list[Message] | None = Noneself._agent_names: set[str] | None = None+ @property+ def auto_route_on(self) -> bool:+ """Whether the room routes by decision: the decision service is asked+ about a message these rules deliver to nobody."""+ return self._auto_route_on+async def decide(self, session: AsyncSession, *, agent: Agent, agent_user_id: str) -> ConversationDecision:diff --git a/core/switch_core/delivery/decision.py b/core/switch_core/delivery/decision.pyindex 8fba859e2..2a0e1b04e 100644--- a/core/switch_core/delivery/decision.py+++ b/core/switch_core/delivery/decision.py@@ -2,17 +2,21 @@rule delivered to anyone.In a room with automatic routing on (`!auto-route on`, on a server with the-`decision_routing` feature and a `DECISION_ENDPOINT_URL`), a person's text-message that names nobody and that no follow-up or assigned agent took is-marked undecided as it is written (`MessageRouter._follow_the_conversation`).-It passed the same filters as a follow-up (`may_follow`): a person sent it, it-has no `@` in it, it is not Switch's own or another app's, it is not in a-direct room, and it is not thanks or praise (`is_small_talk`).--Once the message has committed, the router asks the decision service about-it, in the room's after-commit queue (`_AfterCommit`), so a slow or failed-decision never holds up the write, and decisions in a room are made in the-order of its messages:+`decision_routing` feature, in a workspace with a decision backend:+`decision/settings.py`), a person's text message that names nobody and that+no follow-up or assigned agent took is marked undecided as it is written+(`MessageRouter._follow_the_conversation`). It passed the same filters as a+follow-up (`may_follow`): a person sent it, it has no `@` in it, it is not+Switch's own or another app's, it is not in a direct room, and it is not+thanks or praise (`is_small_talk`). In a workspace with no backend nothing is+marked, and rooms behave as they do without auto-route.++Marking it writes a `pending_decisions` row in the transaction that writes+the message, so a decision survives a restart. Once the message has+committed, the process that wrote it is woken to decide it, and every process+sweeps for waiting rows (`MessageRouter.decide_pending`), so a slow or failed+decision never holds up the write, and a room's messages are decided in+order:1. The candidates are the room's agents other than the sender whoseaddressing policy and budget let the sender through, as for a mention,@@ -21,18 +25,22 @@ order of its messages:session closed before the call.3. When it names a candidate, the agent is checked once more, after thecall: still in the room, its policy and budget still letting the sender- through. Then the person's own message is stored as the agent's own event,- addressed and marked `routed`, under the message's key and place in the- room. It doesn't count as the person addressing the agent, so the person's- next untagged message is decided again rather than followed up.--Nobody receives the message when the service names nobody, when it cannot be-asked (a timeout, a connection error, an error response or a malformed one:-logged as a warning), or when it names an agent that was not a candidate-(logged as an error). Every decision is logged at INFO.--The queue is in memory, so a decision still waiting when the process stops-is not made.+ through. Then, in one transaction, the pending row is deleted and the+ person's own message is stored as the agent's own event, addressed and+ marked routed (`routed_reason: "decided"`), under the message's key and+ place in the room, so it is never delivered twice. It doesn't count as the+ person addressing the agent, so the person's next untagged message is+ decided again rather than followed up.++Nobody receives the message when the service names nobody. When the service+fails (a timeout, a connection error, an error response, a malformed one, or+one naming an agent that was not a candidate), the room is told auto-route is+unavailable (`DECISION_NOTICE`), at most once an hour, and the message goes+to the room's triage agent if it has one (`!auto-route fallback @agent`),+through the same checks, marked `routed_reason: "fallback"`; otherwise to+nobody. A message still waiting after `DECISION_PENDING_MAX_AGE_SECONDS` is+dropped and logged rather than answered late. Every decision is logged at+INFO."""from __future__ import annotations@@ -66,6 +74,12 @@ if TYPE_CHECKING:from switch_core.delivery.recipient import AgentRecipient, RoomMemberfrom switch_core.transport.types import InboundMessage+# What the room is told when its decision backend fails, at most once every+# `DECISION_NOTICE_EVERY_SECONDS` (room metadata `decision_notice_at`), and+# never for a decision of nobody.+DECISION_NOTICE = "Auto-route is unavailable right now; tag an agent to reach it."+DECISION_NOTICE_EVERY_SECONDS = 3600+# The most of a thread's latest messages, and of the room's latest top-level# messages, sent as context: enough to see who is talking to whom, and short# enough to keep the decision quick.diff --git a/core/switch_core/delivery/recipient.py b/core/switch_core/delivery/recipient.pyindex 69ee52231..09219f079 100644--- a/core/switch_core/delivery/recipient.py+++ b/core/switch_core/delivery/recipient.py@@ -634,7 +634,13 @@ class AgentRecipient:)notice = await self._switch_notice(session) if addressed else Noneown_event = _message_own_event(- meta, event, thread_id, self.agent.id, sender_role, notice, routed=False+ meta,+ event,+ thread_id,+ self.agent.id,+ sender_role,+ notice,+ routed_reason=None,)async def after(row: InboundEvent) -> None:@@ -690,7 +696,7 @@ class AgentRecipient:self.agent.id,sender_role,notice,- routed=False,+ routed_reason=None,),after=after,)@@ -739,12 +745,16 @@ class AgentRecipient:return (await self._gate_addressed(session, agent, event, self._meta)).addressedasync def decide_routed(- self, session: AsyncSession, event: InboundMessage+ self,+ session: AsyncSession,+ event: InboundMessage,+ reason: Literal["decided", "fallback"],) -> Delivery:- """The agent's own event for a message the decision service picked it- to answer (`delivery/decision.py`), decided once the message has- committed: when its addressing policy and budget still let the sender- through.+ """The agent's own event for a message routed to it once the message+ has committed (`delivery/decision.py`): the decision service picked it+ (`decided`), or the service failed and it is the room's triage agent+ (`fallback`). Only when its addressing policy and budget let the+ sender through.As quiet as a follow-up, for the same reason: the sender asked no onein particular. A refusal is logged rather than posted, and an agent@@ -755,12 +765,13 @@ class AgentRecipient:gate = await self._gate_addressed(session, agent, event, meta)if not gate.addressed:logger.info(- "Decision routing: room=%s message=%s agent=%s outcome="- "dropped_not_permitted reason=the sender may no longer instruct "- "this agent, or it is over budget",+ "Decision routing: room=%s message=%s agent=%s routed_reason=%s "+ "outcome=dropped_not_permitted reason=the sender may not "+ "instruct this agent, or it is over budget",meta.room_id,event.event_id,agent.name,+ reason,)return NOTHING@@ -778,7 +789,7 @@ class AgentRecipient:self.agent.id,sender_role,notice,- routed=True,+ routed_reason=reason,),after=after,)@@ -1952,11 +1963,11 @@ def _message_own_event(sender_role: str | None,switch_notice: str | None,*,- routed: bool,+ routed_reason: Literal["decided", "fallback"] | None,) -> Callable[[InboundEvent], OwnEvent]:"""How an agent's own event for a message is built once the message is- written. `routed` marks a message the decision service picked the agent- to answer."""+ written. `routed_reason` marks a message Switch routed to the agent after+ it tagged nobody (`delivery/decision.py`)."""sender_name = event.sender_nameif not sender_name:sender_name = event.sender@@ -1979,7 +1990,7 @@ def _message_own_event(opening_message=opening_message_for(meta, agent_id),sender_role=sender_role,switch_notice=switch_notice,- routed=routed,+ routed_reason=routed_reason,)# Serialising runs once per agent per message, so only when it will# be seen.diff --git a/core/switch_core/delivery/router.py b/core/switch_core/delivery/router.pyindex ef3193fd6..afbfd46ea 100644--- a/core/switch_core/delivery/router.py+++ b/core/switch_core/delivery/router.py@@ -97,12 +97,15 @@ from switch_core.db.models import (from switch_core.db.notify_ddl import MESSAGE_CHANNEL, message_announcementfrom switch_core.db.raw import RawStatementfrom switch_core.db.session_scope import tenant_session+from switch_core.db.stores.pending_decision_store import Pending, PendingDecisionStorefrom switch_core.db.stores.room_store import (+ auto_route_fallback_in,auto_route_on_in,follow_ups_enabled_in,main_agent_in,)from switch_core.db.stores.template_install_store import TemplateInstallStore+from switch_core.decision.client import Decided, Unavailablefrom switch_core.delivery.addressing import AddressingResolver, SenderPrincipalfrom switch_core.delivery.conversation import (ConversationMessage,@@ -111,7 +114,11 @@ from switch_core.delivery.conversation import (is_small_talk,may_follow,)-from switch_core.delivery.decision import decision_request+from switch_core.delivery.decision import (+ DECISION_NOTICE,+ DECISION_NOTICE_EVERY_SECONDS,+ decision_request,+)from switch_core.delivery.recipient import (NOTHING,AgentRecipient,@@ -148,7 +155,7 @@ if TYPE_CHECKING:from switch_core.db.stores.budget_store import BudgetStanding, BudgetStorefrom switch_core.db.stores.client_store import ClientStorefrom switch_core.db.stores.message_store import MessageStore- from switch_core.decision.client import DecisionClient+ from switch_core.decision.client import DynamicDecisionClientfrom switch_core.transport.port import MessageTransportlogger = logging.getLogger(__name__)@@ -165,6 +172,14 @@ ATTACHMENT_GROUP_TIMEOUT_SECONDS = 5.0# what decides a post whose writer has stopped since.GROUP_SWEEP_SECONDS = 5.0+# How often every process looks for messages waiting for a decision, for one+# whose process stopped before deciding it. The process that wrote a message+# starts deciding it as soon as the message commits.+DECISION_SWEEP_SECONDS = 5.0++# The most rooms one pass decides at once, one message each.+_DECISION_BATCH = 20+# The one custom event an agent can be part of. The rest (reports,# observations) are the platform's own bookkeeping and are nobody's own event._COMMAND_EVENT = "com.switch.command"@@ -320,6 +335,7 @@ class RoomReads:follow_ups_enabled=follow_ups_enabled_in(room.metadata_),main_agent_id=main_agent_in(room.metadata_),auto_route_on=auto_route_on_in(room.metadata_),+ auto_route_fallback_agent_id=auto_route_fallback_in(room.metadata_),agent_greeting=(room.metadata_ or {}).get("agent_greeting"),console_chat="chat_operation_id" in (room.metadata_ or {}),),@@ -541,9 +557,9 @@ class _Plan:arrival: AgentRecipient | None = Nonegroup: _GroupStep | None = None# A message no rule delivered, which the decision service is asked about- # once it has committed (`delivery/decision.py`): the room's conversation- # as the rules read it for the message.- undecided: RoomConversation | None = None+ # once it has committed (`delivery/decision.py`): recorded in+ # `pending_decisions` with the message.+ undecided: bool = Falseclass Routed:@@ -644,18 +660,8 @@ class Routed:warning = router.mention_warning(reads, row)if warning is not None:work.append(("A mention heads-up from Switch", warning))- if plan.undecided is not None:- assert isinstance(row, InboundMessage)- tenant_id, room_id = reads.tenant_id, reads.room_id- conversation = plan.undecided- work.append(- (- f"Asking the decision service about message {row.event_id}",- lambda: router.route_undecided(- tenant_id, room_id, conversation, row- ),- )- )+ if plan.undecided:+ router.wake_decisions(reads.tenant_id)if work:router.after_commit(reads.tenant_id, reads.room_id, work)@@ -759,8 +765,7 @@ class _Unwritten:class _AfterCommit:- """The replies and reports routed rows owe, and the questions they leave- for the decision service (`delivery/decision.py`), run once each row's+ """The replies and reports routed rows owe, run once each row'stransaction has committed.Off the writer's path, as the consumers it replaced ran them: whoever@@ -834,15 +839,26 @@ class MessageRouter:client_store: ClientStore,budget_store: BudgetStore,conversation: ConversationRules,- decisions: DecisionClient | None,+ decisions: DynamicDecisionClient | None,+ decision_max_age_seconds: float,) -> None:- """`decisions` is the decision service, None when this server does- not route by decision: the `decision_routing` flag is off or no- endpoint is configured."""+ """`decisions` is the decision client, None when this server does+ not route by decision (the `decision_routing` flag is off). A message+ still waiting for a decision after `decision_max_age_seconds` is+ dropped."""self.services = servicesself._messages = message_storeself._conversation = conversationself._decisions = decisions+ self._decision_max_age_seconds = decision_max_age_seconds+ self._pending = PendingDecisionStore()+ # How the room is told auto-route is unavailable: as Switch's admin+ # client, which the client factory builds after this router.+ self._notices: Callable[[str, str, str], Awaitable[None]] | None = None+ # The pass deciding each tenant's waiting messages in this process,+ # and the tenants woken again while theirs ran.+ self._decision_runs: dict[str, asyncio.Task[None]] = {}+ self._decision_again: set[str] = set()self._client_store = client_storeself._budget_store = budget_storeself._transports: Callable[[Actor[ClientConfig]], MessageTransport] | None = (@@ -858,6 +874,11 @@ class MessageRouter:self._timers: set[asyncio.TimerHandle] = set()self._flushes: set[asyncio.Task[None]] = set()+ def use_notices(self, post: Callable[[str, str, str], Awaitable[None]]) -> None:+ """How a room is told auto-route is unavailable: `post(tenant_id,+ transport_room_id, body)` posts an admin notice there."""+ self._notices = post+def use_transports(self, transport_for: Callable[[Actor[ClientConfig]], MessageTransport]) -> None:@@ -977,6 +998,14 @@ class MessageRouter:deadline_in: float | None = Nonestored: list[tuple[str, int]] = []if reads is not None:+ if plan.undecided:+ await self._pending.add(+ session,+ tenant_id=tenant_id,+ room_id=room_id,+ room_seq=message.seq,+ message_key=message.transport_event_id,+ )if plan.group is not None:deadline_in = await self._step_group(session, reads, plan, row)stored = await self._store_own_events(session, reads, plan.deliveries, row)@@ -1211,7 +1240,8 @@ class MessageRouter:automatic routing on (`_asks_for_a_decision`)."""# Not stamped until written (`_Unwritten`); the database stamps it# with its own clock a moment from now.- conversation = self._conversation_of(+ conversation = await self._conversation_of(+ session,reads,recipients,ConversationMessage(@@ -1235,14 +1265,17 @@ class MessageRouter:delivery = await recipient.decide_follow_up(session, event, decision)if delivery.own_event is not None:deliveries.append((recipient, delivery))- if not deliveries and await self._asks_for_a_decision(- session, reads.meta, conversation+ if (+ not deliveries+ and conversation.auto_route_on+ and await self._asks_for_a_decision(session, conversation)):- return _Plan(undecided=conversation)+ return _Plan(undecided=True)return _Plan(deliveries=deliveries)- def _conversation_of(+ async def _conversation_of(self,+ session: AsyncSession,reads: RoomReads,recipients: list[AgentRecipient],message: ConversationMessage,@@ -1262,128 +1295,398 @@ class MessageRouter:channel_type=meta.channel_type,follow_ups_enabled=meta.follow_ups_enabled,main_agent_id=meta.main_agent_id,- auto_route_on=self._auto_routes(meta),+ auto_route_on=await self._auto_routes(session, reads),message=message,)- def _auto_routes(self, meta: RoomMeta) -> bool:+ async def _auto_routes(self, session: AsyncSession, reads: RoomReads) -> bool:"""Whether messages in the room go to the decision service: the room- turned it on, and this server can ask one."""- return meta.auto_route_on and self._decisions is not None+ turned it on, the server routes by decision, and the room's workspace+ has a decision backend (`decision/settings.py`). A workspace with none+ keeps today's behaviour, follow-ups in threads included."""+ return (+ reads.meta.auto_route_on+ and self._decisions is not None+ and await self._decisions.available(session, reads.tenant_id)+ )+ @staticmethodasync def _asks_for_a_decision(- self, session: AsyncSession, meta: RoomMeta, conversation: RoomConversation+ session: AsyncSession, conversation: RoomConversation) -> bool:"""Whether a message that has passed `may_follow` and that no rule- delivered is one to ask the decision service about: in a room routing- by decision, a person's message with text that is not small talk."""+ delivered, in a room routing by decision, is one to ask the decision+ service about: a person's message with text that is not small talk."""body = conversation.message.bodyreturn (- self._auto_routes(meta)- and bool(body.strip())+ bool(body.strip())and not is_small_talk(body)and await conversation.from_a_person(session))- async def route_undecided(- self,- tenant_id: str,- room_id: str,- conversation: RoomConversation,- row: InboundMessage,- ) -> None:- """Ask the decision service who answers `row`, a message no rule- delivered, and deliver it to the agent it names- (`delivery/decision.py`). Run once the row has committed, in the- room's after-commit queue."""+ # ── Deciding what no rule delivered (`delivery/decision.py`) ──────────────++ def wake_decisions(self, tenant_id: str) -> None:+ """Decide the tenant's waiting messages now, in the background: a+ message marked undecided has just committed. A pass already running+ for the tenant runs once more instead."""+ if self._decisions is None:+ return+ if tenant_id in self._decision_runs:+ self._decision_again.add(tenant_id)+ return+ self._decision_runs[tenant_id] = asyncio.get_running_loop().create_task(+ self._decide_pending_logged(tenant_id), context=contextvars.Context()+ )++ async def _decide_pending_logged(self, tenant_id: str) -> None:+ try:+ while True:+ self._decision_again.discard(tenant_id)+ try:+ await self.decide_pending(tenant_id)+ except Exception:+ metrics().increment(ROUTING_FAILURES, {"stage": "decision"})+ logger.error(+ "Could not decide the waiting messages of tenant %s; the "+ "next sweep tries again",+ tenant_id,+ exc_info=True,+ )+ if tenant_id not in self._decision_again:+ return+ finally:+ self._decision_runs.pop(tenant_id, None)++ async def decide_pending(self, tenant_id: str) -> int:+ """Decide the tenant's messages waiting for the decision service, and+ return how many were settled.++ Every process runs this, woken when a message it wrote commits and on+ a sweep (`DECISION_SWEEP_SECONDS`), so a message whose process stopped+ is decided by another, or by the same after a restart. Rows older than+ `DECISION_PENDING_MAX_AGE_SECONDS` are dropped first. Each pass claims+ the oldest waiting message of up to `_DECISION_BATCH` rooms and decides+ them side by side, a room's messages in order. A message that fails+ to be decided for a reason other than the service keeps its claim+ until the lease lapses, so it does not hold up the rest.+ """+ decisions = self._decisions+ if decisions is None:+ return 0+ factory = self.services.session_factory+ async with tenant_session(factory, tenant_id) as session:+ expired = await self._pending.expire(+ session,+ tenant_id=tenant_id,+ max_age_seconds=self._decision_max_age_seconds,+ )+ await session.commit()+ for pending in expired:+ logger.warning(+ "Decision routing: room=%s message=%s outcome=expired reason=it "+ "waited more than %ss for a decision (the server was down or "+ "busy), so no agent receives it",+ pending.room_id,+ pending.message_key,+ self._decision_max_age_seconds,+ )+ settled = 0+ lease = decisions.timeout_seconds * 2 + 30+ while True:+ async with tenant_session(factory, tenant_id) as session:+ claimed = await self._pending.claim(+ session,+ tenant_id=tenant_id,+ lease_seconds=lease,+ limit=_DECISION_BATCH,+ )+ await session.commit()+ if not claimed:+ return settled+ outcomes = await asyncio.gather(+ *(self._decide_claimed(tenant_id, pending) for pending in claimed),+ return_exceptions=True,+ )+ for pending, outcome in zip(claimed, outcomes, strict=True):+ if isinstance(outcome, BaseException):+ metrics().increment(ROUTING_FAILURES, {"stage": "decision"})+ logger.error(+ "Could not decide message %s in room %s; it is tried "+ "again once its claim lapses, until it expires",+ pending.message_key,+ pending.room_id,+ exc_info=outcome,+ )+ else:+ settled += 1++ async def _decide_claimed(self, tenant_id: str, pending: Pending) -> None:+ """Ask the decision service about one claimed message and settle it:+ deliver it to the agent the service names, to the room's triage agent+ when the service fails, or to nobody."""decisions = self._decisionsassert decisions is not None- async with tenant_session(self.services.session_factory, tenant_id) as session:- now = await self._reads_now(session, tenant_id, room_id)- room = await session.get(Room, room_id)- if now is None or room is None:+ room_id = pending.room_id+ with log_context(room_id=room_id):+ async with tenant_session(+ self.services.session_factory, tenant_id+ ) as session:+ reads = await self._reads_now(session, tenant_id, room_id)+ message = await session.scalar(+ select(Message).where(+ Message.room_id == room_id, Message.seq == pending.room_seq+ )+ )+ room = await session.get(Room, room_id)+ row = (+ to_inbound(message, [], transport_room_id=reads.transport_room_id)+ if reads is not None and message is not None+ else None+ )+ if reads is None or room is None or not isinstance(row, InboundMessage):+ await self._settle(+ tenant_id, pending, "the room or message is gone"+ )+ return+ if not reads.meta.auto_route_on:+ await self._settle(tenant_id, pending, "auto-route was turned off")+ return+ candidates = [+ recipient+ for recipient in self._others(reads, row)+ if await recipient.admits(session, row)+ ]+ if not candidates:+ await self._settle(+ tenant_id,+ pending,+ "no agent here may answer the sender, so the decision "+ "service is not asked",+ )+ return+ assert message is not None+ conversation = self._conversation.for_message(+ addressing=reads.addressing,+ room_id=room_id,+ channel_type=reads.meta.channel_type,+ follow_ups_enabled=reads.meta.follow_ups_enabled,+ main_agent_id=reads.meta.main_agent_id,+ auto_route_on=True,+ message=ConversationMessage(+ event_id=row.event_id,+ sender=row.sender,+ body=row.body or "",+ content=row.content,+ thread_root_id=row.thread_root_id,+ # A `DateTime` column, typed `str` on the model.+ sent_at=cast("datetime", message.sent_at),+ ),+ )+ request = await decision_request(+ session,+ room=room,+ members=reads.members,+ roles=await reads.role_names(session),+ conversation=conversation,+ message=row,+ candidates=candidates,+ )+ fallback_id = reads.meta.auto_route_fallback_agent_id+ transport_room_id = reads.transport_room_id+ outcome = await decisions.decide(+ request, tenant_id=tenant_id, room_id=room_id+ )+ if isinstance(outcome, Unavailable):+ await self._settle(+ tenant_id,+ pending,+ "the workspace has no decision service now, so it is not asked",+ )return- candidates = [- recipient- for recipient in self._others(now, row)- if await recipient.admits(session, row)- ]- if not candidates:+ if isinstance(outcome, Decided):+ answer = outcome.answerlogger.info(- "Decision routing: room=%s message=%s no agent here may "- "answer the sender; the decision service is not asked",+ "Decision routing: room=%s message=%s candidates=%d choice=%s "+ "model_choice=%s confidence=%s threshold=%s latency_ms=%s "+ "model=%s backend=%s",room_id,row.event_id,+ len(candidates),+ answer.agent or "nobody",+ answer.model_choice,+ answer.confidence,+ answer.threshold,+ outcome.latency_ms,+ answer.model,+ outcome.backend,+ )+ chosen_name = answer.agent+ await self._deliver_decided(+ tenant_id,+ pending,+ row,+ pick=(+ None+ if chosen_name is None+ else lambda recipient: recipient.agent.name == chosen_name+ ),+ reason="decided",+ transport_room_id=transport_room_id,+ notice=False,)return- request = await decision_request(- session,- room=room,- members=now.members,- roles=await now.role_names(session),- conversation=conversation,- message=row,- candidates=candidates,+ if fallback_id is None:+ logger.info(+ "Decision routing: room=%s message=%s outcome=nobody "+ "reason=the decision service failed (%s) and the room has "+ "no triage agent",+ room_id,+ row.event_id,+ outcome.reason,+ )+ await self._deliver_decided(+ tenant_id,+ pending,+ row,+ pick=(+ None+ if fallback_id is None+ else lambda recipient: recipient.agent.id == fallback_id+ ),+ reason="fallback",+ transport_room_id=transport_room_id,+ notice=True,)- answer = await decisions.decide(request, room_id=room_id)- if answer is None:- return++ async def _settle(self, tenant_id: str, pending: Pending, why: str) -> None:+ """Settle a waiting message with no delivery, saying why."""+ async with tenant_session(self.services.session_factory, tenant_id) as session:+ await self._pending.finish(session, tenant_id=tenant_id, pending=pending)+ await session.commit()logger.info(- "Decision routing: room=%s message=%s candidates=%d choice=%s "- "model_choice=%s confidence=%s threshold=%s latency_ms=%s model=%s",- room_id,- row.event_id,- len(candidates),- answer.agent or "nobody",- answer.model_choice,- answer.confidence,- answer.threshold,- answer.latency_ms,- answer.model,+ "Decision routing: room=%s message=%s outcome=nobody reason=%s",+ pending.room_id,+ pending.message_key,+ why,)- if answer.agent is None:- return++ async def _deliver_decided(+ self,+ tenant_id: str,+ pending: Pending,+ row: InboundMessage,+ *,+ pick: Callable[[AgentRecipient], bool] | None,+ reason: Literal["decided", "fallback"],+ transport_room_id: str,+ notice: bool,+ ) -> None:+ """Settle a decided message: in one transaction, delete its pending+ row and store the picked agent's own event, marked `routed_reason`,+ once the agent is checked again (still in the room, its policy and+ budget letting the sender through). With `notice`, the backend failed,+ and the room is told at most once an hour (`DECISION_NOTICE_AT_KEY`).++ A row another process settled already, after this one's claim lapsed,+ is left alone: its delivery is that process's.+ """+ room_id = pending.room_id+ chosen: AgentRecipient | None = None+ delivery: Delivery | None = None+ stored = False+ tell_room = Falseasync with tenant_session(self.services.session_factory, tenant_id) as session:- now = await self._reads_now(session, tenant_id, room_id)- chosen = next(- (- recipient- for recipient in (self._others(now, row) if now else [])- if recipient.agent.name == answer.agent- ),- None,- )- if chosen is None:+ if not await self._pending.finish(+ session, tenant_id=tenant_id, pending=pending+ ):+ await session.rollback()logger.info(- "Decision routing: room=%s message=%s agent=%s outcome="- "dropped_not_member reason=the agent left the room before "- "the message could be delivered",+ "Decision routing: room=%s message=%s was settled by another "+ "process meanwhile; not delivered again",room_id,row.event_id,- answer.agent,)return- delivery = await chosen.decide_routed(session, row)- if delivery.own_event is None:+ if pick is not None:+ reads = await self._reads_now(session, tenant_id, room_id)+ chosen = next(+ (+ recipient+ for recipient in (self._others(reads, row) if reads else [])+ if pick(recipient)+ ),+ None,+ )+ if chosen is None:+ logger.info(+ "Decision routing: room=%s message=%s routed_reason=%s "+ "outcome=dropped_not_member reason=the agent is not in "+ "the room",+ room_id,+ row.event_id,+ reason,+ )+ else:+ delivery = await chosen.decide_routed(session, row, reason)+ if delivery.own_event is not None:+ own = delivery.own_event(row)+ stored = (+ await self.services.event_buffer.store(+ session,+ tenant_id=tenant_id,+ agent_id=chosen.agent.id,+ client_id=chosen.client_id,+ room_id=room_id,+ event=own.event,+ room_seq=own.room_seq,+ message_key=own.message_key,+ )+ is not None+ )+ if notice:+ tell_room = await self.services.room_store.claim_decision_notice(+ session, room_id, every_seconds=DECISION_NOTICE_EVERY_SECONDS+ )+ await session.commit()+ if stored:+ assert chosen is not None and delivery is not None+ self.services.event_buffer.ring(chosen.agent.id)+ logger.info(+ "Decision routing: room=%s message=%s agent=%s routed_reason=%s "+ "outcome=delivered",+ room_id,+ row.event_id,+ chosen.agent.name,+ reason,+ )+ if delivery.after is not None:+ await chosen.actor.connect()+ await delivery.after(row)+ if tell_room:+ await self._tell_room(tenant_id, room_id, transport_room_id)++ async def _tell_room(+ self, tenant_id: str, room_id: str, transport_room_id: str+ ) -> None:+ """Post the "auto-route is unavailable" notice. Best effort: the+ message is settled either way, and a notice that cannot be posted is+ logged."""+ if self._notices is None:+ logger.warning(+ "Decision routing: room=%s could not be told auto-route is "+ "unavailable: no admin client posts notices in this process",+ room_id,+ )return- own = delivery.own_event(row)- await self.services.event_buffer.enqueue(- tenant_id,- chosen.agent.id,- room_id,- own.event,- room_seq=own.room_seq,- message_key=own.message_key,- )- logger.info(- "Decision routing: room=%s message=%s agent=%s outcome=delivered",- room_id,- row.event_id,- chosen.agent.name,- )- if delivery.after is not None:- await chosen.actor.connect()- await delivery.after(row)+ try:+ await self._notices(tenant_id, transport_room_id, DECISION_NOTICE)+ except Exception:+ logger.warning(+ "Decision routing: room=%s could not be told auto-route is unavailable",+ room_id,+ exc_info=True,+ )async def _reads_now(self, session: AsyncSession, tenant_id: str, room_id: str@@ -1507,8 +1810,11 @@ class MessageRouter:conversation = (Noneif undecided or any(named.values())- else self._conversation_of(- reads, recipients, _post_as_message(files, lead=lead, live=live)+ else await self._conversation_of(+ session,+ reads,+ recipients,+ _post_as_message(files, lead=lead, live=live),))deliveries = []@@ -2048,13 +2354,17 @@ class MessageRouter:return checkasync def idle(self) -> None:- """Wait for the replies already owed, and posts already being decided,- to be done."""+ """Wait for the replies already owed, and posts and messages already+ being decided, to be done."""while True:await self._after.idle()- if not self._flushes:+ if not self._flushes and not self._decision_runs:return- await asyncio.gather(*list(self._flushes), return_exceptions=True)+ await asyncio.gather(+ *list(self._flushes),+ *list(self._decision_runs.values()),+ return_exceptions=True,+ )def close(self) -> None:"""Stop the timers set for posts' deadlines. The sweep in everydiff --git a/core/switch_core/feature_flags.py b/core/switch_core/feature_flags.pyindex 566dda166..09b93d52c 100644--- a/core/switch_core/feature_flags.py+++ b/core/switch_core/feature_flags.py@@ -52,7 +52,8 @@ FOLLOW_THE_CONVERSATION = "follow_the_conversation"# Decision routing: in a room that turned it on (`!auto-route on`), a person's# message that no rule delivered to any agent is sent, once it is saved, to-# the decision service (`DECISION_ENDPOINT_URL`), which picks the one agent+# the decision service its workspace uses (`decision/settings.py`: the+# workspace's choice, else `DECISION_ENDPOINT_URL`), which picks the one agent# that should answer it, or nobody (`delivery/decision.py`). It builds on the# follow-the-conversation filters, so the server refuses it without that flag.DECISION_ROUTING = "decision_routing"@@ -94,6 +95,27 @@ BUDDY_SLACK_HANDOFF = "buddy_slack_handoff"# server's apps, as before organizations.ORGANIZATION_CONNECTOR_APPS = "organization_connector_apps"+# Flags that need another flag on: the server refuses a configuration with+# the key on and any of its values off, and tools that turn flags off one at a+# time (scripts/e2e_surface.py) turn the dependants off with it.+FLAG_REQUIRES: dict[str, frozenset[str]] = {+ DECISION_ROUTING: frozenset({FOLLOW_THE_CONVERSATION}),+}+++def without_flag(flags: frozenset[str], flag: str) -> frozenset[str]:+ """`flags` with `flag` off, and every flag that needs it off too."""+ off = {flag}+ changed = True+ while changed:+ changed = False+ for dependant, needs in FLAG_REQUIRES.items():+ if dependant not in off and needs & off:+ off.add(dependant)+ changed = True+ return flags - off++# Every flag the server recognises.KNOWN_FEATURE_FLAGS: frozenset[str] = frozenset({diff --git a/core/switch_core/gateway/app.py b/core/switch_core/gateway/app.pyindex 7cb9c0f0c..676038749 100644--- a/core/switch_core/gateway/app.py+++ b/core/switch_core/gateway/app.py@@ -46,6 +46,7 @@ from switch_core.gateway.chats import router as chats_routerfrom switch_core.gateway.collaborations import router as collaborations_routerfrom switch_core.gateway.connection_catalog import router as connection_catalog_routerfrom switch_core.gateway.connector_apps import router as connector_apps_router+from switch_core.gateway.decision_settings import router as decision_settings_routerfrom switch_core.gateway.dependencies import init_dependenciesfrom switch_core.gateway.documents import router as documents_routerfrom switch_core.gateway.ecosystem import router as ecosystem_router@@ -251,6 +252,7 @@ def create_gateway_app(app.include_router(template_runs_router, tags=["templates"])app.include_router(ecosystem_router, prefix="/ecosystem", tags=["ecosystem"])app.include_router(trust_settings_router, tags=["trust-settings"])+ app.include_router(decision_settings_router, tags=["decision-settings"])app.include_router(feature_flags_router, tags=["feature-flags"])app.include_router(messaging_installs_router,diff --git a/core/switch_core/gateway/decision_settings.py b/core/switch_core/gateway/decision_settings.pynew file mode 100644index 000000000..217cb7bfa--- /dev/null+++ b/core/switch_core/gateway/decision_settings.py@@ -0,0 +1,271 @@+"""A workspace's decision backend for auto-route — that workspace's admins only.++Tenant-scoped, unlike Switch Trust's settings (`gateway/trust_settings.py`):+every route is gated by `require_tenant_admin` and reads or writes the row of+the workspace the request is bound to, never another's. No row means the+server default (`decision/settings.py`). The key is never returned; a key the+server can no longer decrypt still loads, as set but unreadable, so an admin+can save a replacement.+"""++from __future__ import annotations++import logging+from typing import Annotated, cast++from fastapi import APIRouter, Depends, HTTPException, Request, Response+from sqlalchemy.ext.asyncio import AsyncSession++from switch_core.config import SwitchConfig+from switch_core.db.models import DecisionSettings, User, require_tenant_id+from switch_core.db.stores.decision_settings_store import DecisionSettingsStore+from switch_core.decision.client import DynamicDecisionClient+from switch_core.decision.settings import (+ DecisionAvailability,+ availability,+ resolve,+)+from switch_core.gateway.auth import require_tenant_admin+from switch_core.gateway.dependencies import get_config, get_session+from switch_core.gateway.schemas import (+ DecisionEffective,+ DecisionOptions,+ DecisionServerDefault,+ DecisionSettingsResponse,+ DecisionSettingsTestRequest,+ DecisionSettingsUpdateRequest,+ DecisionTestError,+ DecisionTestResponse,+ DecisionWorkspaceSettings,+ TestErrorCode,+)+from switch_core.keys import Keyring, UndecryptableError+from switch_core.outbound import OutboundPolicy, OutboundURLRefused++router = APIRouter()++logger = logging.getLogger(__name__)++_STORE = DecisionSettingsStore()+++def get_decision_client(request: Request) -> DynamicDecisionClient:+ """The process's decision client (`decision/setup.py`)."""+ client: DynamicDecisionClient = request.app.state.decision_client+ return client+++def _response(+ row: DecisionSettings | None, checks: DecisionAvailability, keyring: Keyring+) -> DecisionSettingsResponse:+ server = checks.server+ resolved = resolve(row, server)+ applies = availability(checks.enabled, resolved)+ workspace: DecisionWorkspaceSettings | None = None+ if row is not None:+ last4: str | None = None+ unreadable = False+ if row.api_key_encrypted is not None:+ try:+ last4 = keyring.decrypt(row.api_key_encrypted)[-4:]+ except UndecryptableError:+ unreadable = True+ logger.warning(+ "Workspace %s's decision key is not decryptable under any "+ "current key; reporting it as set but unreadable.",+ row.tenant_id,+ )+ workspace = DecisionWorkspaceSettings(+ backend=row.backend, # type: ignore[arg-type]+ endpoint=row.endpoint,+ threshold=row.threshold,+ has_api_key=row.api_key_encrypted is not None,+ api_key_last4=last4,+ api_key_unreadable=unreadable,+ updated_at=row.updated_at.isoformat(),+ )+ return DecisionSettingsResponse(+ source=resolved.source,+ effective=DecisionEffective(+ backend=resolved.backend,+ endpoint=resolved.endpoint,+ threshold=resolved.threshold,+ available=applies.available,+ reason=applies.reason,+ ),+ workspace=workspace,+ server_default=DecisionServerDefault(+ configured=server.configured,+ endpoint=server.endpoint_url,+ has_api_key=server.api_key is not None,+ ),+ options=DecisionOptions(+ haiku_anthropic=server.haiku_anthropic,+ haiku_bedrock=server.haiku_bedrock,+ ),+ )+++def _refuse_unoffered(+ req: DecisionSettingsUpdateRequest, checks: DecisionAvailability+) -> None:+ server = checks.server+ if req.backend == "haiku_anthropic" and not server.haiku_anthropic:+ raise HTTPException(+ status_code=422,+ detail="Haiku runs in the server's decision service, and this server "+ "has none configured.",+ )+ if req.backend == "haiku_bedrock" and not server.haiku_bedrock:+ raise HTTPException(+ status_code=422,+ detail="Haiku on Bedrock isn't offered on this server: it needs the "+ "server's decision service and DECISION_BEDROCK_ENABLED.",+ )+++async def _refuse_unreachable(endpoint: str, policy: OutboundPolicy) -> None:+ """Refuse an endpoint the server would never connect to on the+ workspace's behalf: link-local and metadata addresses always, and private+ ones unless the operator lists them."""+ try:+ await policy.check_url(endpoint)+ except OutboundURLRefused as refused:+ raise HTTPException(+ status_code=422,+ detail=f"{refused} A workspace's decision endpoint must be a public "+ "address, or a private host the server's operator lists in "+ "OUTBOUND_ALLOWED_PRIVATE_HOSTS.",+ ) from refused+++def _kept_key(+ req: DecisionSettingsUpdateRequest | DecisionSettingsTestRequest,+ existing: DecisionSettings | None,+) -> str | None:+ """The stored key a request leaves in place: only when it names the same+ backend, so an endpoint's token is never sent to Anthropic as a key."""+ if req.clear_api_key or existing is None or existing.backend != req.backend:+ return None+ return existing.api_key_encrypted++[email protected]("/decision-settings")+async def get_decision_settings(+ session: Annotated[AsyncSession, Depends(get_session)],+ config: Annotated[SwitchConfig, Depends(get_config)],+ client: Annotated[DynamicDecisionClient, Depends(get_decision_client)],+ _admin: Annotated[User, Depends(require_tenant_admin)],+) -> DecisionSettingsResponse:+ row = await _STORE.get(session, require_tenant_id())+ return _response(row, client.availability, config.keyring)++[email protected]("/decision-settings")+async def update_decision_settings(+ req: DecisionSettingsUpdateRequest,+ session: Annotated[AsyncSession, Depends(get_session)],+ config: Annotated[SwitchConfig, Depends(get_config)],+ client: Annotated[DynamicDecisionClient, Depends(get_decision_client)],+ _admin: Annotated[User, Depends(require_tenant_admin)],+) -> DecisionSettingsResponse:+ _refuse_unoffered(req, client.availability)+ if req.backend == "endpoint":+ assert req.endpoint is not None+ await _refuse_unreachable(req.endpoint, client.outbound_policy)+ tenant_id = require_tenant_id()+ existing = await _STORE.get_for_update(session, tenant_id)+ api_key_encrypted = (+ config.keyring.encrypt(req.api_key)+ if req.api_key is not None+ else _kept_key(req, existing)+ )+ if req.backend in ("none", "haiku_bedrock"):+ api_key_encrypted = None+ if req.backend == "haiku_anthropic" and api_key_encrypted is None:+ raise HTTPException(+ status_code=422, detail="Haiku on the Anthropic API needs an Anthropic key."+ )+ saved = await _STORE.save(+ session,+ tenant_id,+ backend=req.backend,+ endpoint=req.endpoint if req.backend == "endpoint" else None,+ api_key_encrypted=api_key_encrypted,+ threshold=req.threshold,+ )+ await session.commit()+ client.forget(tenant_id)+ return _response(saved, client.availability, config.keyring)++[email protected]("/decision-settings", status_code=204)+async def delete_decision_settings(+ session: Annotated[AsyncSession, Depends(get_session)],+ client: Annotated[DynamicDecisionClient, Depends(get_decision_client)],+ _admin: Annotated[User, Depends(require_tenant_admin)],+) -> Response:+ tenant_id = require_tenant_id()+ await _STORE.clear(session, tenant_id)+ await session.commit()+ client.forget(tenant_id)+ return Response(status_code=204)++[email protected]("/decision-settings/test")+async def test_decision_settings(+ req: DecisionSettingsTestRequest,+ session: Annotated[AsyncSession, Depends(get_session)],+ config: Annotated[SwitchConfig, Depends(get_config)],+ client: Annotated[DynamicDecisionClient, Depends(get_decision_client)],+ _admin: Annotated[User, Depends(require_tenant_admin)],+) -> DecisionTestResponse:+ """Run the sample decision against the values entered, saved or not; an+ omitted key means the stored one. `backend: "default"` tests the server+ default, with the server's own endpoint and key and never the+ workspace's. Nothing is saved, and no room's data is sent."""+ existing = await _STORE.get(session, require_tenant_id())+ # Not held across the call to the decision service.+ await session.commit()+ api_key = req.api_key+ if api_key is None and req.backend != "default":+ stored = _kept_key(req, existing)+ if stored is not None:+ try:+ api_key = config.keyring.decrypt(stored)+ except UndecryptableError:+ return DecisionTestResponse(+ ok=False,+ agent=None,+ model_choice=None,+ confidence=None,+ latency_ms=None,+ backend=req.backend,+ error=DecisionTestError(+ code="not_configured",+ message="The stored key can't be decrypted by this server; "+ "enter it again.",+ ),+ )+ outcome = await client.test(+ backend=req.backend,+ endpoint=req.endpoint if req.backend == "endpoint" else None,+ api_key=api_key if req.backend in ("endpoint", "haiku_anthropic") else None,+ threshold=req.threshold,+ )+ return DecisionTestResponse(+ ok=outcome.ok,+ agent=outcome.agent,+ model_choice=outcome.model_choice,+ confidence=outcome.confidence,+ latency_ms=outcome.latency_ms,+ backend=outcome.backend,+ error=(+ DecisionTestError(+ code=cast("TestErrorCode", outcome.error_code),+ message=outcome.error_message or "",+ )+ if outcome.error_code is not None+ else None+ ),+ )diff --git a/core/switch_core/gateway/rooms.py b/core/switch_core/gateway/rooms.pyindex 9e05e0a77..186fd5917 100644--- a/core/switch_core/gateway/rooms.py+++ b/core/switch_core/gateway/rooms.py@@ -17,6 +17,7 @@ from switch_core.authz import (can,can_manage,can_unlink_from_room,+ require_manage,)from switch_core.bridges.agent.operations.agent_management import management_portfrom switch_core.bridges.agent.protocol.agent_core import AgentCore@@ -34,6 +35,15 @@ from switch_core.db.stores.room_group_store import RoomGroupStorefrom switch_core.db.stores.room_store import LINKED_CHANNEL_NAME_KEY, RoomStorefrom switch_core.db.stores.template_store import TemplateStorefrom switch_core.db.stores.user_store import UserStore+from switch_core.decision.room import (+ AutoRouteRefused,+ AutoRouteState,+ auto_route_state,+ room_agent,+ set_auto_route_fallback,+ set_auto_route_on,+)+from switch_core.decision.settings import DecisionAvailabilityfrom switch_core.feature_flags import AGENT_MANAGEMENT, SWITCH_BUDDYfrom switch_core.gateway.auth import (authorize_room,@@ -64,6 +74,8 @@ from switch_core.gateway.schemas import (BulkDeleteResponse,RoomAgentsRequest,RoomAgentUpdateRequest,+ RoomAutoRoute,+ RoomAutoRouteRequest,RoomCreateRequest,RoomDeleteResponse,RoomDetail,@@ -182,6 +194,7 @@ async def _build_room_detail(external_user_store: ExternalUserStore,protocol: AgentCore,principal: Principal,+ decisions: DecisionAvailability,) -> RoomDetail:agent_ids = await room_store.get_agent_ids(session, room.id)client_ids = await room_store.get_client_ids(session, room.id)@@ -217,6 +230,7 @@ async def _build_room_detail(join_event_listeners = await room_store.get_join_event_listeners(session, room.id)owner = await session.get(User, room.owner_id) if room.owner_id else Nonetakes_part = await takes_part_in_room(session, room_store, room.id, principal.id)+ auto_route = await auto_route_state(session, decisions, room)# Not held across the deeplink, which can ask the platform on a cache miss.await session.commit()external_channel_url = await _external_channel_url(room)@@ -266,6 +280,7 @@ async def _build_room_detail(can_unlink=can_unlink_from_room(principal, takes_part=takes_part),can_transfer=_can_transfer(principal, room),can_change_members=_can_change_members(principal, room),+ auto_route=_auto_route_detail(auto_route),)@@ -282,6 +297,24 @@ def _can_transfer(principal: Principal, room: Room) -> bool:return can_manage(principal, room.owner_id) and kind_refusal(room) is None+def _auto_route_detail(state: AutoRouteState) -> RoomAutoRoute:+ return RoomAutoRoute(+ on=state.on,+ available=state.available,+ reason=state.reason,+ fallback_agent=state.fallback_agent,+ fallback_agent_name=state.fallback_agent_name,+ )+++def get_decision_availability(+ config: Annotated[SwitchConfig, Depends(get_config)],+) -> DecisionAvailability:+ """Whether auto-route can act in a room: the server's flag and default,+ and the room's workspace's choice (`decision/settings.py`)."""+ return DecisionAvailability.from_config(config)++async def _list_room_role_details(session: AsyncSession, room_id: str, protocol: AgentCore) -> list[RoomRoleDetail]:@@ -443,6 +476,7 @@ async def create_room(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -492,6 +526,7 @@ async def create_room(external_user_store,protocol,Principal(user.id, is_admin),+ decisions,)@@ -931,6 +966,7 @@ async def get_room(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -944,6 +980,7 @@ async def get_room(external_user_store,protocol,principal,+ decisions,)@@ -957,6 +994,7 @@ async def patch_room(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -1001,6 +1039,7 @@ async def patch_room(external_user_store,protocol,principal,+ decisions,)@@ -1100,6 +1139,7 @@ async def put_room_group(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -1132,6 +1172,7 @@ async def put_room_group(external_user_store,protocol,principal,+ decisions,)@@ -1145,6 +1186,7 @@ async def put_protection(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -1166,6 +1208,7 @@ async def put_protection(external_user_store,protocol,principal,+ decisions,)@@ -1179,6 +1222,7 @@ async def put_observe(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -1200,9 +1244,52 @@ async def put_observe(external_user_store,protocol,principal,+ decisions,)[email protected]("/{room_id}/auto-route")+async def put_auto_route(+ room_id: str,+ req: RoomAutoRouteRequest,+ session: Annotated[AsyncSession, Depends(get_session)],+ room_store: Annotated[RoomStore, Depends(get_room_store)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],+ user: Annotated[User, Depends(get_current_user)],+ is_admin: Annotated[bool, Depends(get_tenant_is_admin)],+) -> RoomAutoRoute:+ """Turn the room's auto-route on or off, and name or clear its triage+ agent: the same settings as `!auto-route` and `!auto-route fallback`.+ For the room's owner and the workspace's admins. Turning it on where it+ cannot act (`available` false) is refused with 409 and the reason."""+ principal = Principal(user.id, is_admin)+ room = await authorize_room(session, room_store, room_id, principal, "read")+ try:+ require_manage(principal, room.owner_id)+ except PermissionError as e:+ raise HTTPException(status_code=403, detail=str(e)) from e+ try:+ if "fallback_agent" in req.model_fields_set:+ agent = None+ if req.fallback_agent is not None:+ agent = await room_agent(session, room.id, req.fallback_agent)+ if agent is None:+ raise HTTPException(+ status_code=422,+ detail=f"No agent {req.fallback_agent!r} in this room.",+ )+ await set_auto_route_fallback(session, room_store, room, agent)+ if req.on is not None:+ await set_auto_route_on(session, decisions, room_store, room, req.on)+ except AutoRouteRefused as refused:+ raise HTTPException(+ status_code=409 if req.on else 422, detail=str(refused)+ ) from refused+ await session.commit()+ await session.refresh(room)+ return _auto_route_detail(await auto_route_state(session, decisions, room))++@router.post("/{room_id}/agents")async def post_room_agents(room_id: str,@@ -1213,6 +1300,7 @@ async def post_room_agents(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -1241,6 +1329,7 @@ async def post_room_agents(external_user_store,protocol,principal,+ decisions,)@@ -1255,6 +1344,7 @@ async def patch_room_agent(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -1277,6 +1367,7 @@ async def patch_room_agent(external_user_store,protocol,principal,+ decisions,)@@ -1290,6 +1381,7 @@ async def delete_room_agent(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -1313,6 +1405,7 @@ async def delete_room_agent(external_user_store,protocol,principal,+ decisions,)@@ -1326,6 +1419,7 @@ async def post_room_users(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -1349,6 +1443,7 @@ async def post_room_users(external_user_store,protocol,principal,+ decisions,)@@ -1361,6 +1456,7 @@ async def _set_archived(bridge_store: CollaborationBridgeStore,external_user_store: ExternalUserStore,protocol: AgentCore,+ decisions: DecisionAvailability,user: User,is_admin: bool,) -> RoomDetail:@@ -1381,6 +1477,7 @@ async def _set_archived(external_user_store,protocol,principal,+ decisions,)@@ -1393,6 +1490,7 @@ async def sync_room_name(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -1422,6 +1520,7 @@ async def sync_room_name(external_user_store,protocol,principal,+ decisions,)@@ -1434,6 +1533,7 @@ async def archive_room(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -1448,6 +1548,7 @@ async def archive_room(bridge_store,external_user_store,protocol,+ decisions,user,is_admin,)@@ -1462,6 +1563,7 @@ async def unarchive_room(bridge_store: Annotated[CollaborationBridgeStore, Depends(get_bridge_store)],external_user_store: Annotated[ExternalUserStore, Depends(get_external_user_store)],protocol: Annotated[AgentCore, Depends(get_protocol)],+ decisions: Annotated[DecisionAvailability, Depends(get_decision_availability)],user: Annotated[User, Depends(get_current_user)],is_admin: Annotated[bool, Depends(get_tenant_is_admin)],) -> RoomDetail:@@ -1475,6 +1577,7 @@ async def unarchive_room(bridge_store,external_user_store,protocol,+ decisions,user,is_admin,)diff --git a/core/switch_core/gateway/schemas.py b/core/switch_core/gateway/schemas.pyindex 2b622df40..5babe32ce 100644--- a/core/switch_core/gateway/schemas.py+++ b/core/switch_core/gateway/schemas.py@@ -109,6 +109,29 @@ class RoomSummary(BaseModel):can_transfer: bool+class RoomAutoRoute(BaseModel):+ """A room's auto-route (`decision/room.py`): `on` is the room's switch,+ the same one `!auto-route` sets; `available` whether it can act in the+ room (the server's flag and the workspace's decision backend), with+ `reason` saying why not and who can fix it; `fallback_agent` the id of+ the room's triage agent, if any, and `fallback_agent_name` its name."""++ on: bool+ available: bool+ reason: str | None+ fallback_agent: str | None+ fallback_agent_name: str | None+++class RoomAutoRouteRequest(BaseModel):+ """`PUT /rooms/{id}/auto-route`. Either field may be left out to leave it+ as it is. `fallback_agent` is the id of an agent in the room (its name+ or alias here is accepted too); null clears it."""++ on: bool | None = None+ fallback_agent: str | None = None++class RoomDetail(RoomSummary):transport_room_id: str# Deprecated alias of `transport_room_id`, sent for the compatibility@@ -134,6 +157,7 @@ class RoomDetail(RoomSummary):# when it has not seen one. A room not named after its channel can take# it with `POST /rooms/{id}/sync-name`.linked_channel_name: str | None = None+ auto_route: RoomAutoRouteclass RoomCreateRequest(BaseModel):@@ -2068,6 +2092,125 @@ class BudgetResponse(BaseModel):# ── Switch Trust ──────────────────────────────────────────────────────────────+DecisionBackendName = Literal["none", "endpoint", "haiku_anthropic", "haiku_bedrock"]+++class DecisionEffective(BaseModel):+ """What a workspace's rooms use now: its own choice, or the server+ default, or nothing. `available` folds in the server's flag."""++ backend: DecisionBackendName+ endpoint: str | None+ threshold: float | None+ available: bool+ reason: str | None+++class DecisionWorkspaceSettings(BaseModel):+ """The workspace's saved choice. The key is never returned: only whether+ one is set, its last four characters, and whether this server can still+ decrypt it."""++ backend: DecisionBackendName+ endpoint: str | None+ threshold: float | None+ has_api_key: bool+ api_key_last4: str | None+ api_key_unreadable: bool+ updated_at: str+++class DecisionServerDefault(BaseModel):+ configured: bool+ endpoint: str | None+ has_api_key: bool+++class DecisionOptions(BaseModel):+ """Which Haiku backends the server offers: both need the server's+ decision service, and Bedrock also `DECISION_BEDROCK_ENABLED`."""++ haiku_anthropic: bool+ haiku_bedrock: bool+++class DecisionSettingsResponse(BaseModel):+ source: Literal["workspace", "server_default"]+ effective: DecisionEffective+ workspace: DecisionWorkspaceSettings | None+ server_default: DecisionServerDefault+ options: DecisionOptions+++class DecisionSettingsUpdateRequest(BaseModel):+ """`api_key` omitted keeps the stored key when the backend stays the+ same; `clear_api_key` drops it. A key is the bearer token for an+ `endpoint`, and the Anthropic key for `haiku_anthropic`."""++ backend: DecisionBackendName+ endpoint: str | None = None+ api_key: str | None = None+ clear_api_key: bool = False+ threshold: float | None = Field(default=None, ge=0, le=1)++ @model_validator(mode="after")+ def _validate(self) -> DecisionSettingsUpdateRequest:+ if self.api_key is not None and not self.api_key.strip():+ raise ValueError(+ "api_key must not be blank; omit it to keep the stored key, or "+ "set clear_api_key"+ )+ if self.backend == "endpoint":+ value = self.endpoint+ if value is None or not value.strip():+ raise ValueError("a self-hosted endpoint needs its URL")+ if value != value.strip():+ raise ValueError("endpoint has leading or trailing whitespace")+ parts = urlsplit(value)+ if parts.scheme not in ("http", "https"):+ raise ValueError("endpoint must be an http(s) URL")+ if not parts.netloc:+ raise ValueError("endpoint must include a host")+ return self+++class DecisionSettingsTestRequest(DecisionSettingsUpdateRequest):+ """The PUT body, or `backend: "default"` to test the server default with+ the server's own endpoint and key, never the workspace's."""++ backend: DecisionBackendName | Literal["default"] # type: ignore[assignment]+++TestErrorCode = Literal[+ "unreachable",+ "timeout",+ "unauthorized",+ "key_refused",+ "bad_answer",+ "backend_unavailable",+ "not_configured",+ "refused",+]+++class DecisionTestError(BaseModel):+ code: TestErrorCode+ message: str+++class DecisionTestResponse(BaseModel):+ """The sample decision's pick (`agent`, None for nobody), confidence and+ latency, or why there was none."""++ ok: bool+ agent: str | None+ model_choice: str | None+ confidence: float | None+ latency_ms: int | None+ backend: str | None+ error: DecisionTestError | None++class TrustSettingsResponse(BaseModel):"""Switch Trust's one server-global settings row. The API key itself isnever returned — `has_api_key`/`api_key_last4` only confirm one is set,diff --git a/core/switch_core/main.py b/core/switch_core/main.pyindex f026333a0..09a9bd0e3 100644--- a/core/switch_core/main.py+++ b/core/switch_core/main.py@@ -94,6 +94,7 @@ from switch_core.bridges.resource.service import ResourceServicefrom switch_core.chats.service import ChatServicefrom switch_core.chats.signals import ChatSignalsfrom switch_core.clients.actor import Actor, AgentActor, HumanActor, SystemActor+from switch_core.clients.admin_messages import AdminMessageTypefrom switch_core.clients.agent_admin import AgentAdministration, AgentDeletionRequestsfrom switch_core.clients.client_factory import ClientFactoryfrom switch_core.clients.client_lifecycle_service import ClientLifecycleService@@ -180,7 +181,11 @@ from switch_core.db.tenant_lookup import all_tenant_idsfrom switch_core.decision.setup import build_decision_clientfrom switch_core.delivery.conversation import ConversationRules, ConversationSettingsfrom switch_core.delivery.recipient import RecipientServices-from switch_core.delivery.router import GROUP_SWEEP_SECONDS, MessageRouter+from switch_core.delivery.router import (+ DECISION_SWEEP_SECONDS,+ GROUP_SWEEP_SECONDS,+ MessageRouter,+)from switch_core.feature_flags import AGENT_MANAGEMENT, SWITCH_BUDDYfrom switch_core.gateway.app import create_gateway_appfrom switch_core.gateway.auth import hash_password@@ -353,6 +358,28 @@ async def _attachment_group_sweep_loop()+async def _decision_sweep_loop(+ session_factory: async_sessionmaker[AsyncSession], router: MessageRouter+) -> None:+ """Decide the messages waiting for the decision service in every tenant:+ the process that wrote one starts at once, and this is what decides one+ whose process stopped first, or that a restart left behind. Every process+ runs it; each message is claimed by one."""+ with no_tenant():+ while True:+ await asyncio.sleep(DECISION_SWEEP_SECONDS)+ try:+ tenants = await all_tenant_ids(session_factory)+ except Exception:+ logger.exception("Decision sweep failed")+ continue+ for tenant_id in tenants:+ try:+ await router.decide_pending(tenant_id)+ except Exception:+ logger.exception("Decision sweep failed for tenant %s", tenant_id)++async def _recheck_agent_rooms(session_factory: async_sessionmaker[AsyncSession], router: MessageRouter) -> None:@@ -877,7 +904,9 @@ async def run(config: SwitchConfig, process_id: str) -> None:trust_signals = TrustSettingsSignals(client=trust_client, bus=signal_bus)# ── Decision routing (off unless the decision_routing flag is on) ────────- decision = build_decision_client(config)+ # Built either way, so workspace admins can set up and test a backend; only+ # the router's copy is withheld with the flag off.+ decision_client = build_decision_client(config, session_factory, telemetry)# ── Resource service ─────────────────────────────────────────────────────resource_service = ResourceService(@@ -965,7 +994,8 @@ async def run(config: SwitchConfig, process_id: str) -> None:agent_store=agent_store,room_role_store=room_role_store,),- decisions=decision[0] if decision is not None else None,+ decisions=decision_client if config.decision_routing_enabled else None,+ decision_max_age_seconds=config.decision_pending_max_age_seconds,)MembershipSignals(invites=invites, bus=signal_bus, router=router)@@ -1045,6 +1075,28 @@ async def run(config: SwitchConfig, process_id: str) -> None:))+ async def post_decision_notice(+ tenant_id: str, transport_room_id: str, body: str+ ) -> None:+ admin = next(+ (+ client+ for client in client_lifecycle.get_by_type("admin", tenant_id)+ if isinstance(client, SystemActor)+ ),+ None,+ )+ if admin is None:+ raise RuntimeError(f"No admin client is running for tenant {tenant_id}")+ await admin.send_admin(+ transport_room_id,+ body,+ message_type=AdminMessageType.AUTO_ROUTE_UNAVAILABLE,+ thread_root_id=None,+ )++ router.use_notices(post_decision_notice)+# ── Collaboration bridge lifecycle ───────────────────────────────────────collab_lifecycle = CollaborationBridgeLifecycleService(bridge_store=bridge_store,@@ -1114,8 +1166,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:connections=connections,frontend_base_url=config.dashboard_base_url,follow_the_conversation_enabled=config.follow_the_conversation_enabled,- decision_routing_enabled=config.decision_routing_enabled,- decision_endpoint_configured=config.decision_endpoint_url is not None,+ decision_availability=decision_client.availability,)# ── Agent management (off unless the agent_management flag is on) ─────────@@ -1482,6 +1533,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:config.workos_client_id,config.workos_redirect_url,)+ gateway_app.state.decision_client = decision_client# A connector app the operator saves or removes, put to work in every process.ConnectorAppSignals(apps=connector_apps,@@ -1544,6 +1596,11 @@ async def run(config: SwitchConfig, process_id: str) -> None:attachment_group_task = asyncio.create_task(_attachment_group_sweep_loop(session_factory, router))+ decision_task = (+ asyncio.create_task(_decision_sweep_loop(session_factory, router))+ if config.decision_routing_enabled+ else None+ )connection_sweep_task = asyncio.create_task(_connection_sweep_loop(protocol))@@ -1601,6 +1658,8 @@ async def run(config: SwitchConfig, process_id: str) -> None:sweep_task.cancel()retention_task.cancel()attachment_group_task.cancel()+ if decision_task is not None:+ decision_task.cancel()router.close()connection_sweep_task.cancel()service_token_task.cancel()@@ -1637,8 +1696,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:await protocol.sessions.aclose()await _drain_telemetry(telemetry, message_telemetry, telemetry_http)await trust_http.aclose()- if decision is not None:- await decision[1].aclose()+ await decision_client.aclose()await observability.aclose()# So a probe during teardown gets a 503 rather than the last# cached answer, which may still say ready.diff --git a/core/switch_core/migrations/versions/689d7d8ff913_decision_settings_and_pending_decisions.py b/core/switch_core/migrations/versions/689d7d8ff913_decision_settings_and_pending_decisions.pynew file mode 100644index 000000000..d64994a1f--- /dev/null+++ b/core/switch_core/migrations/versions/689d7d8ff913_decision_settings_and_pending_decisions.py@@ -0,0 +1,121 @@+"""decision settings per workspace, and decisions that survive a restart++Two tenant-scoped tables for decision routing (`delivery/decision.py`):++- `decision_settings`: a workspace's choice of decision backend, at most one+ row per workspace. No row means the server default from the environment, so+ an upgraded server behaves as before with the table empty.+- `pending_decisions`: a person's message waiting for the decision service,+ written with the message and deleted once it is decided, so a restart does+ not lose it.++The row-level-security DDL is a verbatim copy of `switch_core/db/rls_ddl.py`+as it stood when this migration was written, copied rather than imported for+the reason `265ed188ad6f` gives.++Expand only: a process from before this revision never reads either table,+and decides untagged messages in memory, as it always did.++Revision ID: 689d7d8ff913+Revises: b7e1d4c9a2f6+Create Date: 2026-10-27 00:00:00.000000++"""++from collections.abc import Sequence++import sqlalchemy as sa+from alembic import op++revision: str = "689d7d8ff913"+down_revision: str | Sequence[str] | None = "b7e1d4c9a2f6"+branch_labels: str | Sequence[str] | None = None+depends_on: str | Sequence[str] | None = None+expand_only: bool = True++REQUIRE_TENANT_FUNCTION_NAME = "require_tenant_id"+POLICY_NAME = "tenant_isolation"+SETTINGS = "decision_settings"+PENDING = "pending_decisions"++_PREDICATE = f'"tenant_id" = (SELECT {REQUIRE_TENANT_FUNCTION_NAME}())'+++def _create_policy(table: str) -> str:+ return (+ f'CREATE POLICY {POLICY_NAME} ON "{table}"\n'+ f" FOR ALL\n"+ f" USING ({_PREDICATE})\n"+ f" WITH CHECK ({_PREDICATE})"+ )+++def upgrade() -> None:+ op.create_table(+ SETTINGS,+ sa.Column("tenant_id", sa.Text(), nullable=False),+ sa.Column("backend", sa.Text(), nullable=False),+ sa.Column("endpoint", sa.Text(), nullable=True),+ sa.Column("api_key_encrypted", sa.Text(), nullable=True),+ sa.Column("threshold", sa.Float(), nullable=True),+ sa.Column(+ "updated_at",+ sa.DateTime(timezone=True),+ server_default=sa.text("now()"),+ nullable=False,+ ),+ sa.PrimaryKeyConstraint("tenant_id", name="pk_decision_settings"),+ sa.ForeignKeyConstraint(+ ["tenant_id"], ["tenants.id"], name="fk_decision_settings_tenant"+ ),+ sa.CheckConstraint(+ "backend IN ('none', 'endpoint', 'haiku_anthropic', 'haiku_bedrock')",+ name="ck_decision_settings_backend",+ ),+ sa.CheckConstraint(+ "threshold IS NULL OR (threshold >= 0 AND threshold <= 1)",+ name="ck_decision_settings_threshold",+ ),+ )+ op.execute(f'ALTER TABLE "{SETTINGS}" ENABLE ROW LEVEL SECURITY')+ op.execute(_create_policy(SETTINGS))++ op.create_table(+ PENDING,+ sa.Column("tenant_id", sa.Text(), nullable=False),+ sa.Column("room_id", sa.Text(), nullable=False),+ sa.Column("room_seq", sa.BigInteger(), nullable=False),+ sa.Column("message_key", sa.Text(), nullable=False),+ sa.Column(+ "created_at",+ sa.DateTime(timezone=True),+ server_default=sa.text("now()"),+ nullable=False,+ ),+ sa.Column("claimed_until", sa.DateTime(timezone=True), nullable=True),+ sa.PrimaryKeyConstraint(+ "tenant_id", "room_id", "room_seq", name="pk_pending_decisions"+ ),+ sa.ForeignKeyConstraint(+ ["tenant_id"], ["tenants.id"], name="fk_pending_decisions_tenant"+ ),+ sa.ForeignKeyConstraint(+ ["tenant_id", "room_id"],+ ["rooms.tenant_id", "rooms.id"],+ name="fk_pending_decisions_room",+ ondelete="CASCADE",+ ),+ )+ op.create_index(+ "ix_pending_decisions_created_at", PENDING, ["tenant_id", "created_at"]+ )+ op.execute(f'ALTER TABLE "{PENDING}" ENABLE ROW LEVEL SECURITY')+ op.execute(_create_policy(PENDING))+++def downgrade() -> None:+ op.execute(f'DROP POLICY IF EXISTS {POLICY_NAME} ON "{PENDING}"')+ op.drop_index("ix_pending_decisions_created_at", table_name=PENDING)+ op.drop_table(PENDING)+ op.execute(f'DROP POLICY IF EXISTS {POLICY_NAME} ON "{SETTINGS}"')+ op.drop_table(SETTINGS)diff --git a/core/switch_core/telemetry/catalogue.py b/core/switch_core/telemetry/catalogue.pyindex da574fae0..644b724e1 100644--- a/core/switch_core/telemetry/catalogue.py+++ b/core/switch_core/telemetry/catalogue.py@@ -195,6 +195,32 @@ BRIDGE_FAILURE_REASON = one_of(TRISTATE = one_of("true", "false", "unknown")+# Which decision backend a workspace's auto-route used: the server's own+# decision service as its default, the workspace's own endpoint, or Haiku.+DECISION_BACKEND = one_of(+ "server_default", "endpoint", "haiku_anthropic", "haiku_bedrock"+)++# The confidence the decision service gave its choice, in tenths above one+# half, where a threshold sits; `none` when it gave none.+DECISION_CONFIDENCE = one_of(+ "none", "lt_0.5", "0.5_0.6", "0.6_0.7", "0.7_0.8", "0.8_0.9", "0.9_1.0"+)++# Why a decision service gave no usable answer: no answer in time, no+# connection, an answer other than 200, an answer that was not a decision (or+# named an agent that was not a candidate), a workspace key the server can no+# longer decrypt, or a workspace endpoint the server's outbound policy refuses.+DECISION_FAILURE_REASON = one_of(+ "timeout",+ "unreachable",+ "error_status",+ "bad_answer",+ "key_unreadable",+ "endpoint_refused",+)++# ── The catalogue ────────────────────────────────────────────────────────────_SNAPSHOT_COUNTS = (@@ -553,6 +579,23 @@ CATALOGUE: Mapping[str, Mapping[str, PropertyType]] = {"has_attachment": BOOLEAN,"in_thread": BOOLEAN,},+ # ── Decision routing ─────────────────────────────────────────────────────+ # One per message auto-route asked a decision service about. Which agent,+ # room or message is never said: only whether an agent was picked, how+ # sure the service was, how long it took and which kind of backend+ # answered. `latency_ms` is measured by Switch, around the whole request.+ "decision_made": {+ "backend": DECISION_BACKEND,+ "outcome": one_of("agent", "none"),+ "confidence": DECISION_CONFIDENCE,+ "latency_ms": NUMBER,+ },+ # A decision service that gave no usable answer, so the message went to+ # nobody, or to the room's triage agent.+ "decision_failed": {+ "backend": DECISION_BACKEND,+ "reason": DECISION_FAILURE_REASON,+ },# ── Resources, keys and groups ───────────────────────────────────────────# Creating is intent, attaching is use, and the gap between them is the# signal — hence both.diff --git a/core/tests/benchmarks/server.py b/core/tests/benchmarks/server.pyindex 36bc1e767..01af46b09 100644--- a/core/tests/benchmarks/server.py+++ b/core/tests/benchmarks/server.py@@ -543,6 +543,7 @@ async def _serve(room_role_store=session_env.room_role_store,),decisions=None,+ decision_max_age_seconds=120,)provisioning: Provisioning = PostgresProvisioning(diff --git a/core/tests/integration/conftest.py b/core/tests/integration/conftest.pyindex 316445804..25e76717a 100644--- a/core/tests/integration/conftest.py+++ b/core/tests/integration/conftest.py@@ -655,6 +655,7 @@ async def harness(room_role_store=session_env.room_role_store,),decisions=None,+ decision_max_age_seconds=120,)provisioning: Provisioning = PostgresProvisioning(diff --git a/core/tests/integration/test_switch_trust_agent_e2e.py b/core/tests/integration/test_switch_trust_agent_e2e.pyindex 51aaba998..d1f802df4 100644--- a/core/tests/integration/test_switch_trust_agent_e2e.py+++ b/core/tests/integration/test_switch_trust_agent_e2e.py@@ -16,6 +16,8 @@ from switch_core.bridges.agent.protocol.agent_connections import AgentConnectionfrom switch_core.clients.actor import SystemActorfrom switch_core.clients.command_consumer import CommandConsumerfrom switch_core.db.models import Message+from switch_core.db.stores.decision_settings_store import DecisionSettingsStore+from switch_core.decision.settings import DecisionAvailability, ServerDefaultfrom switch_core.room_service import RoomCreateConfigfrom switch_core.trust.client import (GuardrailBlockedError,@@ -90,8 +92,13 @@ async def _wire_admin_client(harness: Harness, session_env: SessionEnv) -> None:connections=AgentConnectionRegistry(),frontend_base_url=session_env.config.dashboard_base_url,follow_the_conversation_enabled=False,- decision_routing_enabled=False,- decision_endpoint_configured=False,+ decision_availability=DecisionAvailability(+ enabled=False,+ server=ServerDefault(+ endpoint_url=None, api_key=None, bedrock_enabled=False+ ),+ store=DecisionSettingsStore(),+ ),)await harness.client_lifecycle.ensure_system_client("admin")diff --git a/core/tests/routing.py b/core/tests/routing.pyindex 196421086..3f4a2ec7b 100644--- a/core/tests/routing.py+++ b/core/tests/routing.py@@ -55,7 +55,7 @@ from switch_core.db.stores.reference_store import ReferenceStorefrom switch_core.db.stores.room_role_store import RoomRoleStorefrom switch_core.db.stores.room_store import RoomStorefrom switch_core.db.stores.usage_store import UsageStore-from switch_core.decision.client import DecisionClient+from switch_core.decision.client import DynamicDecisionClientfrom switch_core.delivery.conversation import ConversationRules, ConversationSettingsfrom switch_core.delivery.recipient import RecipientServicesfrom switch_core.delivery.router import MessageRouter@@ -202,7 +202,7 @@ def router_on(telemetry: RecordedTelemetry | None = None,conversation: ConversationSettings | None = None,announcer: Announcer | None = None,- decisions: DecisionClient | None = None,+ decisions: DynamicDecisionClient | None = None,) -> MessageRouter:"""A router on the test database with real stores. A test that has noagent in its rooms can write through it with nothing else set up.@@ -246,6 +246,7 @@ def router_on(room_role_store=RoomRoleStore(),),decisions=decisions,+ decision_max_age_seconds=120,)@@ -256,7 +257,7 @@ def process_on(connections: AgentConnectionRegistry | None = None,conversation: ConversationSettings | None = None,announcer: Announcer | None = None,- decisions: DecisionClient | None = None,+ decisions: DynamicDecisionClient | None = None,) -> Process:"""A process's routing on the test database: real stores, its ownregistry and buffer, telemetry that records. A test of presence acrossdiff --git a/core/tests/switch_core/bridges/agent/test_commands_auto_route.py b/core/tests/switch_core/bridges/agent/test_commands_auto_route.pyindex de3d5daec..c6d6bb460 100644--- a/core/tests/switch_core/bridges/agent/test_commands_auto_route.py+++ b/core/tests/switch_core/bridges/agent/test_commands_auto_route.py@@ -11,8 +11,23 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerfrom switch_core.bridges.agent.commands import COMMANDS_BY_NAME, _cmd_auto_routefrom switch_core.bridges.collaboration.own_app_setup import slack_manifest-from switch_core.db.models import Room-from switch_core.db.stores.room_store import AUTO_ROUTE_ON_KEY, RoomStore+from switch_core.db.models import (+ TENANT_ZERO_ID,+ Agent,+ ApiKey,+ Client,+ Room,+ User,+ room_agents,+)+from switch_core.db.stores.agent_store import AgentStore+from switch_core.db.stores.decision_settings_store import DecisionSettingsStore+from switch_core.db.stores.room_store import (+ AUTO_ROUTE_FALLBACK_KEY,+ AUTO_ROUTE_ON_KEY,+ RoomStore,+)+from switch_core.decision.settings import DecisionAvailability, ServerDefaultfrom switch_core.transport import RoomRef@@ -49,8 +64,16 @@ def _host(_resolve_room_meta=_resolve_room_meta,reply_command=_reply_command,_room_store=RoomStore(),- _decision_routing_enabled=flag,- _decision_endpoint_configured=endpoint,+ _agent_store=AgentStore(),+ _decision_availability=DecisionAvailability(+ enabled=flag,+ server=ServerDefault(+ endpoint_url="http://decision.test/decide" if endpoint else None,+ api_key=None,+ bedrock_enabled=False,+ ),+ store=DecisionSettingsStore(),+ ),)return host, posted@@ -139,15 +162,174 @@ class TestAutoRouteCommand:)assert not await _on(session_factory, room_id)- @pytest.mark.parametrize("args", ["", "on"])- async def test_without_a_decision_service_it_says_so_and_changes_nothing(- self, session_factory: async_sessionmaker[AsyncSession], args: str+ async def test_on_without_a_decision_service_says_who_can_fix_it(+ self, session_factory: async_sessionmaker[AsyncSession]) -> None:room_id = await _room(session_factory)host, posted = _host(session_factory, room_id, endpoint=False)- await _run(host, args)+ await _run(host, "on")++ assert posted[-1].startswith("Auto-route needs a decision service.")+ assert "Switch Console → Admin → Decision routing" in posted[-1]+ assert "Nothing was changed" in posted[-1]+ assert not await _on(session_factory, room_id)++ async def test_on_where_the_workspace_chose_none_is_refused(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ room_id = await _room(session_factory)+ async with session_factory() as session:+ await DecisionSettingsStore().save(+ session,+ TENANT_ZERO_ID,+ backend="none",+ endpoint=None,+ api_key_encrypted=None,+ threshold=None,+ )+ await session.commit()+ host, posted = _host(session_factory, room_id)++ await _run(host, "on")++ assert "turned auto-route off" in posted[-1]+ assert not await _on(session_factory, room_id)++ async def test_on_and_unavailable_since_says_it_cannot_act(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ room_id = await _room(session_factory)+ host, _ = _host(session_factory, room_id)+ await _run(host, "on")+ unavailable, posted = _host(session_factory, room_id, endpoint=False)- assert "has no decision service configured" in posted[-1]- assert "nothing was changed" in posted[-1]+ await _run(unavailable, "")+ assert "Automatic routing is **on**" in posted[-1]+ assert (+ "It can't act right now: Auto-route needs a decision service" in posted[-1]+ )++ await _run(unavailable, "off")+ assert "Automatic routing is **off**" in posted[-1]assert not await _on(session_factory, room_id)+++async def _agents(+ session_factory: async_sessionmaker[AsyncSession], room_id: str+) -> dict[str, Agent]:+ """Two agents in the room, `triage` and `helper`."""+ agents: dict[str, Agent] = {}+ async with session_factory() as session:+ owner = User(name="owner", email="[email protected]", role="user")+ session.add(owner)+ await session.flush()+ for name in ("triage", "helper"):+ client = Client(+ transport_user_id=f"@{name}-{uuid.uuid4().hex[:6]}:test",+ display_name=name,+ type="agent",+ )+ key = ApiKey(+ user_id=owner.id,+ key_hash=f"hash-{uuid.uuid4().hex}",+ encrypted_key="enc",+ label=name,+ type="agent",+ )+ session.add_all([client, key])+ await session.flush()+ agent = Agent(+ name=name,+ description="d",+ agent_type="session_addressable",+ connector_type="claude_code",+ integration_profile={"connection_model": "session_addressable"},+ client_id=client.id,+ api_key_id=key.id,+ owner_id=owner.id,+ )+ session.add(agent)+ await session.flush()+ await session.execute(+ room_agents.insert().values(room_id=room_id, agent_id=agent.id)+ )+ agents[name] = agent+ await session.commit()+ return agents+++async def _fallback(+ session_factory: async_sessionmaker[AsyncSession], room_id: str+) -> str | None:+ async with session_factory() as session:+ return await RoomStore().auto_route_fallback(session, room_id)+++class TestTheTriageAgent:+ async def test_naming_showing_and_clearing_it(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ room_id = await _room(session_factory)+ agents = await _agents(session_factory, room_id)+ host, posted = _host(session_factory, room_id)++ await _run(host, "fallback")+ assert "This room has no triage agent" in posted[-1]++ await _run(host, "fallback @Triage")+ assert "is now this room's triage agent" in posted[-1]+ assert await _fallback(session_factory, room_id) == agents["triage"].id+ async with session_factory() as session:+ room = await RoomStore().get(session, room_id)+ assert room is not None+ assert room.metadata_[AUTO_ROUTE_FALLBACK_KEY] == agents["triage"].id++ await _run(host, "fallback")+ assert "go to **triage**" in posted[-1]+ await _run(host, "on")+ assert "the room's triage agent" in posted[-1]++ await _run(host, "fallback off")+ assert "no longer has a triage agent" in posted[-1]+ assert await _fallback(session_factory, room_id) is None++ async def test_an_agent_not_in_the_room_is_refused(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ room_id = await _room(session_factory)+ await _agents(session_factory, room_id)+ host, posted = _host(session_factory, room_id)++ await _run(host, "fallback @nobody")++ assert "No agent named `nobody` in this room" in posted[-1]+ assert await _fallback(session_factory, room_id) is None++ async def test_the_assigned_agent_is_refused(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ room_id = await _room(session_factory)+ agents = await _agents(session_factory, room_id)+ async with session_factory() as session:+ await RoomStore().set_main_agent(session, room_id, agents["helper"].id)+ await session.commit()+ host, posted = _host(session_factory, room_id)++ await _run(host, "fallback @helper")++ assert "assigned agent" in posted[-1]+ assert "Nothing was changed" in posted[-1]+ assert await _fallback(session_factory, room_id) is None++ @pytest.mark.parametrize("args", ["on off", "fallback @a @b", "maybe"])+ async def test_anything_else_gets_the_usage(+ self, session_factory: async_sessionmaker[AsyncSession], args: str+ ) -> None:+ room_id = await _room(session_factory)+ host, posted = _host(session_factory, room_id)++ await _run(host, args)++ assert posted[-1].startswith("Usage: `!auto-route on`")+ assert "`!auto-route fallback @agent`" in posted[-1]diff --git a/core/tests/switch_core/clients/test_command_consumer_ownership.py b/core/tests/switch_core/clients/test_command_consumer_ownership.pyindex 8ef9dc23a..1c7e54076 100644--- a/core/tests/switch_core/clients/test_command_consumer_ownership.py+++ b/core/tests/switch_core/clients/test_command_consumer_ownership.py@@ -34,6 +34,7 @@ from switch_core.db.models import TENANT_ZERO_ID, Client, Message, Roomfrom switch_core.db.stores.agent_session_store import AgentSessionStorefrom switch_core.db.stores.agent_store import AgentStorefrom switch_core.db.stores.client_store import ClientStore+from switch_core.db.stores.decision_settings_store import DecisionSettingsStorefrom switch_core.db.stores.delivery_cursor_store import DeliveryCursorStorefrom switch_core.db.stores.document_store import DocumentStorefrom switch_core.db.stores.external_user_store import ExternalUserStore@@ -47,6 +48,7 @@ from switch_core.db.stores.switch_core_process_store import SwitchCoreProcessStofrom switch_core.db.stores.tenant_store import TenantStorefrom switch_core.db.stores.usage_store import UsageStorefrom switch_core.db.stores.user_store import UserStore+from switch_core.decision.settings import DecisionAvailability, ServerDefaultfrom switch_core.messages.notify import MessageListenerfrom switch_core.ownership import Ownershipfrom switch_core.process_lease import ProcessLease@@ -136,8 +138,13 @@ async def _process(connections=AgentConnectionRegistry(),frontend_base_url=None,follow_the_conversation_enabled=False,- decision_routing_enabled=False,- decision_endpoint_configured=False,+ decision_availability=DecisionAvailability(+ enabled=False,+ server=ServerDefault(+ endpoint_url=None, api_key=None, bedrock_enabled=False+ ),+ store=DecisionSettingsStore(),+ ),)lease = process_lease_on(session_factory)ownership = Ownership(diff --git a/core/tests/switch_core/db/stores/test_decision_settings_store.py b/core/tests/switch_core/db/stores/test_decision_settings_store.pynew file mode 100644index 000000000..39abc8961--- /dev/null+++ b/core/tests/switch_core/db/stores/test_decision_settings_store.py@@ -0,0 +1,147 @@+"""A workspace's decision backend row, on real Postgres: one per workspace,+replaced in place, cleared back to the server default, and invisible to+every other workspace under row-level security."""++from __future__ import annotations++import uuid++import pytest+from sqlalchemy import select+from sqlalchemy.exc import IntegrityError+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.db.models import TENANT_ZERO_ID, DecisionSettings, Tenant+from switch_core.db.session_scope import tenant_session+from switch_core.db.stores.decision_settings_store import DecisionSettingsStore+from tests.conftest import RLSHarness++STORE = DecisionSettingsStore()+TENANT_B = "decision-store-tenant-b"+++async def _tenant(+ session_factory: async_sessionmaker[AsyncSession], tenant_id: str+) -> None:+ async with session_factory() as session:+ session.add(Tenant(id=tenant_id, slug=tenant_id, name=tenant_id))+ await session.commit()+++async def test_two_workspaces_rows_stay_separate(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ await _tenant(session_factory, TENANT_B)+ async with session_factory() as session:+ await STORE.save(+ session,+ TENANT_ZERO_ID,+ backend="endpoint",+ endpoint="http://a.test/decide",+ api_key_encrypted="enc-a",+ threshold=0.6,+ )+ await STORE.save(+ session,+ TENANT_B,+ backend="none",+ endpoint=None,+ api_key_encrypted=None,+ threshold=None,+ )+ await session.commit()++ async with session_factory() as session:+ a = await STORE.get(session, TENANT_ZERO_ID)+ b = await STORE.get(session, TENANT_B)+ assert a is not None and b is not None+ assert (a.backend, a.endpoint, a.api_key_encrypted, a.threshold) == (+ "endpoint",+ "http://a.test/decide",+ "enc-a",+ 0.6,+ )+ assert (b.backend, b.endpoint, b.api_key_encrypted) == ("none", None, None)++ await STORE.clear(session, TENANT_B)+ await session.commit()+ assert await STORE.get(session, TENANT_B) is None+ assert await STORE.get(session, TENANT_ZERO_ID) is not None+++async def test_a_save_replaces_the_row_in_place(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ async with session_factory() as session:+ first = await STORE.save(+ session,+ TENANT_ZERO_ID,+ backend="haiku_anthropic",+ endpoint=None,+ api_key_encrypted="enc-1",+ threshold=None,+ )+ stamped = first.updated_at+ await session.commit()+ second = await STORE.save(+ session,+ TENANT_ZERO_ID,+ backend="endpoint",+ endpoint="http://b.test/decide",+ api_key_encrypted=None,+ threshold=0.9,+ )+ await session.commit()+ rows = (await session.scalars(select(DecisionSettings))).all()++ assert len(rows) == 1+ assert second.backend == "endpoint" and second.api_key_encrypted is None+ assert second.updated_at >= stamped+++ ("backend", "threshold"), [("somewhere", None), ("none", 1.5), ("none", -0.1)]+)+async def test_the_database_refuses_what_the_routes_refuse(+ session_factory: async_sessionmaker[AsyncSession],+ backend: str,+ threshold: float | None,+) -> None:+ async with session_factory() as session:+ with pytest.raises(IntegrityError):+ await STORE.save(+ session,+ TENANT_ZERO_ID,+ backend=backend,+ endpoint=None,+ api_key_encrypted=None,+ threshold=threshold,+ )+++async def test_a_workspace_cannot_read_anothers_row_under_row_level_security(+ rls_harness: RLSHarness,+) -> None:+ tenant_a = f"decision-rls-{uuid.uuid4().hex[:8]}"+ await _tenant(rls_harness.owner, tenant_a)+ await _tenant(rls_harness.owner, TENANT_B)+ async with tenant_session(rls_harness.restricted, tenant_a) as session:+ await STORE.save(+ session,+ tenant_a,+ backend="haiku_anthropic",+ endpoint=None,+ api_key_encrypted="enc-a",+ threshold=None,+ )+ await session.commit()++ async with tenant_session(rls_harness.restricted, TENANT_B) as session:+ assert await STORE.get(session, tenant_a) is None+ assert (await session.scalars(select(DecisionSettings))).all() == []+ await STORE.clear(session, tenant_a)+ await session.commit()++ async with tenant_session(rls_harness.restricted, tenant_a) as session:+ row = await STORE.get(session, tenant_a)+ assert row is not None and row.api_key_encrypted == "enc-a"diff --git a/core/tests/switch_core/db/stores/test_pending_decision_store.py b/core/tests/switch_core/db/stores/test_pending_decision_store.pynew file mode 100644index 000000000..f9584bc98--- /dev/null+++ b/core/tests/switch_core/db/stores/test_pending_decision_store.py@@ -0,0 +1,172 @@+"""Messages waiting for the decision service, on real Postgres: claimed one+per room in order, leased rather than held, expired by age, finished once,+and kept apart by workspace."""++from __future__ import annotations++import uuid+from datetime import timedelta++from sqlalchemy import func, select, update+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.db.models import TENANT_ZERO_ID, PendingDecision, Room, Tenant+from switch_core.db.session_scope import tenant_session+from switch_core.db.stores.pending_decision_store import Pending, PendingDecisionStore++STORE = PendingDecisionStore()+TENANT_B = "pending-store-tenant-b"+++async def _room(+ session_factory: async_sessionmaker[AsyncSession], tenant_id: str = TENANT_ZERO_ID+) -> str:+ async with tenant_session(session_factory, tenant_id) as session:+ room = Room(+ transport_room_id=f"!r-{uuid.uuid4().hex[:8]}:test",+ name="room",+ description="d",+ )+ session.add(room)+ await session.commit()+ return room.id+++async def _add(+ session_factory: async_sessionmaker[AsyncSession],+ room_id: str,+ seq: int,+ tenant_id: str = TENANT_ZERO_ID,+) -> None:+ async with tenant_session(session_factory, tenant_id) as session:+ await STORE.add(+ session,+ tenant_id=tenant_id,+ room_id=room_id,+ room_seq=seq,+ message_key=f"$m{seq}",+ )+ await session.commit()+++async def _claim(+ session_factory: async_sessionmaker[AsyncSession],+ tenant_id: str = TENANT_ZERO_ID,+ *,+ limit: int = 10,+) -> list[Pending]:+ async with tenant_session(session_factory, tenant_id) as session:+ claimed = await STORE.claim(+ session, tenant_id=tenant_id, lease_seconds=60, limit=limit+ )+ await session.commit()+ return claimed+++async def test_one_message_per_room_oldest_first(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ ops, sales = await _room(session_factory), await _room(session_factory)+ await _add(session_factory, ops, 5)+ await _add(session_factory, ops, 7)+ await _add(session_factory, sales, 3)++ first = await _claim(session_factory)+ again = await _claim(session_factory)++ assert sorted((p.room_id, p.room_seq) for p in first) == sorted(+ [(ops, 5), (sales, 3)]+ )+ # The room's next message waits until its first is settled.+ assert again == []+ async with session_factory() as session:+ assert await STORE.finish(+ session, tenant_id=TENANT_ZERO_ID, pending=Pending(ops, 5, "$m5")+ )+ await session.commit()+ assert await _claim(session_factory) == [Pending(ops, 7, "$m7")]+++async def test_a_lapsed_lease_is_claimed_again(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ room = await _room(session_factory)+ await _add(session_factory, room, 1)+ assert len(await _claim(session_factory)) == 1+ async with session_factory() as session:+ await session.execute(+ update(PendingDecision).values(+ claimed_until=func.now() - timedelta(seconds=1)+ )+ )+ await session.commit()++ assert await _claim(session_factory) == [Pending(room, 1, "$m1")]+++async def test_finish_says_whether_the_row_was_still_there(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ room = await _room(session_factory)+ await _add(session_factory, room, 1)+ pending = Pending(room, 1, "$m1")++ async with session_factory() as session:+ assert await STORE.finish(session, tenant_id=TENANT_ZERO_ID, pending=pending)+ assert not await STORE.finish(+ session, tenant_id=TENANT_ZERO_ID, pending=pending+ )+ await session.commit()+++async def test_old_rows_expire_and_young_ones_stay(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ room = await _room(session_factory)+ await _add(session_factory, room, 1)+ await _add(session_factory, room, 2)+ async with session_factory() as session:+ await session.execute(+ update(PendingDecision)+ .where(PendingDecision.room_seq == 1)+ .values(created_at=func.now() - timedelta(minutes=3))+ )+ await session.commit()++ async with session_factory() as session:+ expired = await STORE.expire(+ session, tenant_id=TENANT_ZERO_ID, max_age_seconds=120+ )+ await session.commit()+ left = (await session.scalars(select(PendingDecision.room_seq))).all()++ assert expired == [Pending(room, 1, "$m1")]+ assert left == [2]+++async def test_each_workspace_claims_only_its_own(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ async with session_factory() as session:+ session.add(Tenant(id=TENANT_B, slug=TENANT_B, name=TENANT_B))+ await session.commit()+ mine = await _room(session_factory)+ theirs = await _room(session_factory, TENANT_B)+ await _add(session_factory, mine, 1)+ await _add(session_factory, theirs, 1, TENANT_B)++ assert [p.room_id for p in await _claim(session_factory)] == [mine]+ assert [p.room_id for p in await _claim(session_factory, TENANT_B)] == [theirs]+++async def test_a_deleted_room_takes_its_waiting_messages_with_it(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ room = await _room(session_factory)+ await _add(session_factory, room, 1)++ async with session_factory() as session:+ found = await session.get(Room, room)+ await session.delete(found)+ await session.commit()+ assert (await session.scalars(select(PendingDecision))).all() == []diff --git a/core/tests/switch_core/db/stores/test_room_auto_route.py b/core/tests/switch_core/db/stores/test_room_auto_route.pyindex 7d62b1936..c49cd2f1b 100644--- a/core/tests/switch_core/db/stores/test_room_auto_route.py+++ b/core/tests/switch_core/db/stores/test_room_auto_route.py@@ -9,8 +9,11 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerfrom switch_core.db.models import Roomfrom switch_core.db.stores.room_store import (+ AUTO_ROUTE_FALLBACK_KEY,AUTO_ROUTE_ON_KEY,+ DECISION_NOTICE_AT_KEY,RoomStore,+ auto_route_fallback_in,auto_route_on_in,)@@ -66,3 +69,60 @@ async def test_a_missing_room_is_refused(assert not await RoomStore().auto_route_on(session, str(uuid.uuid4()))with pytest.raises(ValueError, match="does not exist"):await RoomStore().set_auto_route_on(session, str(uuid.uuid4()), True)+++async def test_the_triage_agent_is_kept_in_the_metadata(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ room_id = await _room(session_factory)+ store = RoomStore()+ async with session_factory() as session:+ assert await store.auto_route_fallback(session, room_id) is None+ await store.set_auto_route_fallback(session, room_id, "agent-1")+ await session.commit()+ assert await store.auto_route_fallback(session, room_id) == "agent-1"+ assert auto_route_fallback_in({AUTO_ROUTE_FALLBACK_KEY: 7}) is None+ await store.set_auto_route_fallback(session, room_id, None)+ await session.commit()+ room = await session.get(Room, room_id)+ assert room is not None+ assert room.metadata_ == {"created_by_kind": "user"}+++async def test_the_room_is_told_at_most_once_an_hour(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ room_id = await _room(session_factory)+ store = RoomStore()+ async with session_factory() as session:+ assert await store.claim_decision_notice(session, room_id, every_seconds=3600)+ await session.commit()+ assert not await store.claim_decision_notice(+ session, room_id, every_seconds=3600+ )+ await session.commit()+ # A shorter limit, as if the hour were up.+ assert await store.claim_decision_notice(session, room_id, every_seconds=0)+ await session.commit()+ room = await session.get(Room, room_id)+ await session.refresh(room)+ assert room is not None+ assert room.metadata_["created_by_kind"] == "user"+ assert DECISION_NOTICE_AT_KEY in room.metadata_+++async def test_a_room_with_no_metadata_can_be_told(+ session_factory: async_sessionmaker[AsyncSession],+) -> None:+ async with session_factory() as session:+ room = Room(+ transport_room_id=f"!r-{uuid.uuid4().hex[:8]}:test",+ name="bare",+ description="d",+ metadata_=None,+ )+ session.add(room)+ await session.commit()+ assert await RoomStore().claim_decision_notice(+ session, room.id, every_seconds=3600+ )diff --git a/core/tests/switch_core/decision/test_client.py b/core/tests/switch_core/decision/test_client.pyindex 01ed65b37..5e57b04a9 100644--- a/core/tests/switch_core/decision/test_client.py+++ b/core/tests/switch_core/decision/test_client.py@@ -1,6 +1,6 @@-"""The decision client against a stub decision service on a real socket: a-good answer, and every way of not getting one, each of which means nobody-answers."""+"""The decision client against stub decision services on real sockets: a+good answer, every way of not getting one, where each workspace's decisions+go, the Test button's classified errors, and what is reported to telemetry."""from __future__ import annotations@@ -14,16 +14,29 @@ from typing import Anyimport httpximport pytest+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker+from switch_core.db.models import TENANT_ZERO_ID, Tenant+from switch_core.db.stores.decision_settings_store import DecisionSettingsStorefrom switch_core.decision.client import (ContextMessage,+ Decided,DecisionAgent,DecisionAnswer,- DecisionClient,DecisionMessage,DecisionRequest,DecisionRoom,+ DynamicDecisionClient,+ Failed,+ Unavailable,+ confidence_bucket,+ decide_url,)+from switch_core.decision.settings import DecisionAvailability, ServerDefault+from switch_core.keys import Keyring+from switch_core.outbound import OutboundPolicy, guarded_async_client+from switch_core.telemetry.catalogue import validate+from tests.conftest import TEST_KEYRING_GOOD = {"agent": "deploy",@@ -60,7 +73,13 @@ class _Stub:body = await reader.readexactly(int(headers.get("content-length", "0")))self.received.append((headers, json.loads(body)))await asyncio.sleep(self.delay)- reason = {200: "OK", 500: "Internal Server Error", 502: "Bad Gateway"}+ reason = {+ 200: "OK",+ 400: "Bad Request",+ 401: "Unauthorized",+ 500: "Internal Server Error",+ 502: "Bad Gateway",+ }writer.write(f"HTTP/1.1 {self.status} {reason.get(self.status, 'Status')}\r\n"f"Content-Type: application/json\r\n"@@ -83,20 +102,98 @@ async def stub() -> AsyncIterator[_Stub]:server.close()+async def other() -> AsyncIterator[_Stub]:+ """A second decision service: a workspace's own endpoint."""+ service = _Stub()+ server = await asyncio.start_server(service.handle, "127.0.0.1", 0)+ port = server.sockets[0].getsockname()[1]+ service.url = f"http://127.0.0.1:{port}/decide"+ async with server:+ yield service+ server.close()++@pytest.fixtureasync def http() -> AsyncIterator[httpx.AsyncClient]:async with httpx.AsyncClient() as client:yield client+class _Telemetry:+ """Records what is reported, after checking it against the catalogue, as+ the real service does."""++ def __init__(self) -> None:+ self.events: list[tuple[str, dict[str, Any]]] = []++ def emit(self, event: str, **properties: Any) -> None:+ validate(event, properties)+ self.events.append((event, properties))+++TENANT_B = "decision-client-tenant-b"++def _client(- http: httpx.AsyncClient, url: str, *, api_key: str | None = None-) -> DecisionClient:- return DecisionClient(- endpoint_url=url, api_key=api_key, timeout_seconds=0.5, client=http+ session_factory: async_sessionmaker[AsyncSession],+ http: httpx.AsyncClient,+ url: str | None,+ *,+ api_key: str | None = None,+ bedrock: bool = False,+ enabled: bool = True,+ telemetry: _Telemetry | None = None,+ cache_seconds: float = 0,+ keyring: Keyring = TEST_KEYRING,+ allowed: str = "127.0.0.1",+) -> DynamicDecisionClient:+ return DynamicDecisionClient(+ session_factory=session_factory,+ availability=DecisionAvailability(+ enabled=enabled,+ server=ServerDefault(+ endpoint_url=url, api_key=api_key, bedrock_enabled=bedrock+ ),+ store=DecisionSettingsStore(),+ ),+ keyring=keyring,+ client=http,+ guarded_client=guarded_async_client(OutboundPolicy.parse(allowed)),+ outbound_policy=OutboundPolicy.parse(allowed),+ timeout_seconds=0.5,+ telemetry=telemetry, # type: ignore[arg-type]+ cache_seconds=cache_seconds,)+async def _save(+ session_factory: async_sessionmaker[AsyncSession],+ tenant_id: str,+ *,+ backend: str,+ endpoint: str | None = None,+ api_key: str | None = None,+ threshold: float | None = None,+) -> None:+ async with session_factory() as session:+ await DecisionSettingsStore().save(+ session,+ tenant_id,+ backend=backend,+ endpoint=endpoint,+ api_key_encrypted=TEST_KEYRING.encrypt(api_key) if api_key else None,+ threshold=threshold,+ )+ await session.commit()+++async def _tenant_b(session_factory: async_sessionmaker[AsyncSession]) -> None:+ async with session_factory() as session:+ session.add(Tenant(id=TENANT_B, slug=TENANT_B, name=TENANT_B))+ await session.commit()++def _request() -> DecisionRequest:return DecisionRequest(room=DecisionRoom(name="ops", instructions="Keep it short."),@@ -131,132 +228,600 @@ def _closed_port() -> int:return port-async def test_a_good_answer(stub: _Stub, http: httpx.AsyncClient) -> None:- answer = await _client(http, stub.url, api_key="placeholder").decide(- _request(), room_id="room-1"- )+async def _decide(client: DynamicDecisionClient, tenant_id: str = TENANT_ZERO_ID):+ return await client.decide(_request(), tenant_id=tenant_id, room_id="room-1")+++class TestTheServerDefault:+ async def test_a_good_answer(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ outcome = await _decide(+ _client(session_factory, http, stub.url, api_key="placeholder")+ )- assert answer == DecisionAnswer(- agent="deploy",- model_choice="deploy",- confidence=0.91,- threshold=0.6,- reason="the question is about a deploy",- model="clef-flash",- latency_ms=312.0,+ assert isinstance(outcome, Decided)+ assert outcome.backend == "server_default"+ assert outcome.answer == DecisionAnswer(+ agent="deploy",+ model_choice="deploy",+ confidence=0.91,+ threshold=0.6,+ reason="the question is about a deploy",+ model="clef-flash",+ latency_ms=312.0,+ )+ [(headers, body)] = stub.received+ assert headers["authorization"] == "Bearer placeholder"+ assert "x-decision-backend" not in headers+ assert body == _request().model_dump(mode="json")+ assert "confidence_threshold" not in body++ async def test_no_key_sends_no_authorization(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ await _decide(_client(session_factory, http, stub.url))++ [(headers, _)] = stub.received+ assert "authorization" not in headers++ async def test_nobody_is_an_answer(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ stub.body = json.dumps(+ {**_GOOD, "agent": None, "model_choice": "billing", "confidence": 0.45}+ ).encode()++ outcome = await _decide(_client(session_factory, http, stub.url))++ assert isinstance(outcome, Decided)+ assert outcome.answer.agent is None+ assert outcome.answer.model_choice == "billing"++ async def test_no_server_default_and_no_choice_is_unavailable(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ http: httpx.AsyncClient,+ ) -> None:+ outcome = await _decide(_client(session_factory, http, None))++ assert isinstance(outcome, Unavailable)+ assert outcome.reason is not None+ assert "Decision routing" in outcome.reason++ async def test_the_flag_off_is_unavailable(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ client = _client(session_factory, http, stub.url, enabled=False)++ assert isinstance(await _decide(client), Unavailable)+ async with session_factory() as session:+ assert not await client.available(session, TENANT_ZERO_ID)+ assert stub.received == []+++class TestFailures:+ async def test_a_timeout(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ caplog: pytest.LogCaptureFixture,+ ) -> None:+ stub.delay = 2.0++ with caplog.at_level(logging.WARNING):+ outcome = await _decide(_client(session_factory, http, stub.url))++ assert isinstance(outcome, Failed) and outcome.reason == "timeout"+ assert "did not answer within 0.5s" in caplog.text+ assert "room-1" in caplog.text++ async def test_a_service_that_is_not_running(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ http: httpx.AsyncClient,+ caplog: pytest.LogCaptureFixture,+ ) -> None:+ url = f"http://127.0.0.1:{_closed_port()}/decide"++ with caplog.at_level(logging.WARNING):+ outcome = await _decide(_client(session_factory, http, url))++ assert isinstance(outcome, Failed) and outcome.reason == "unreachable"+ assert "could not be reached" in caplog.text+ assert url in caplog.text++ @pytest.mark.parametrize("status", [500, 502, 401, 201])+ async def test_any_status_but_200(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ caplog: pytest.LogCaptureFixture,+ status: int,+ ) -> None:+ stub.status = status+ stub.body = b'{"detail": "model server unreachable"}'++ with caplog.at_level(logging.WARNING):+ outcome = await _decide(_client(session_factory, http, stub.url))++ assert isinstance(outcome, Failed) and outcome.reason == "error_status"+ assert f"answered {status}" in caplog.text+ assert "model server unreachable" in caplog.text++ @pytest.mark.parametrize(+ "body",+ [+ b"not json",+ b"[]",+ json.dumps({k: v for k, v in _GOOD.items() if k != "agent"}).encode(),+ json.dumps({**_GOOD, "agent": 7}).encode(),+ json.dumps({**_GOOD, "confidence": "high"}).encode(),+ json.dumps({**_GOOD, "threshold": None}).encode(),+ ],)- [(headers, body)] = stub.received- assert headers["authorization"] == "Bearer placeholder"- assert body == _request().model_dump(mode="json")- assert body["recent"][0] == {- "sender": "alice",- "sender_kind": "person",- "text": "@deploy is the pipeline green?",- "addressed": ["deploy"],- }---async def test_no_key_sends_no_authorization(- stub: _Stub, http: httpx.AsyncClient-) -> None:- await _client(http, stub.url).decide(_request(), room_id="room-1")+ async def test_a_malformed_body(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ caplog: pytest.LogCaptureFixture,+ body: bytes,+ ) -> None:+ stub.body = body- [(headers, _)] = stub.received- assert "authorization" not in headers+ with caplog.at_level(logging.WARNING):+ outcome = await _decide(_client(session_factory, http, stub.url))+ assert isinstance(outcome, Failed) and outcome.reason == "bad_answer"+ assert "answered something other than a decision" in caplog.text-async def test_nobody_is_an_answer(stub: _Stub, http: httpx.AsyncClient) -> None:- stub.body = json.dumps(- {**_GOOD, "agent": None, "model_choice": "billing", "confidence": 0.45}- ).encode()+ async def test_a_name_that_was_not_a_candidate(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ caplog: pytest.LogCaptureFixture,+ ) -> None:+ stub.body = json.dumps({**_GOOD, "agent": "docs"}).encode()++ with caplog.at_level(logging.WARNING):+ outcome = await _decide(_client(session_factory, http, stub.url))++ assert isinstance(outcome, Failed) and outcome.reason == "bad_answer"+ [record] = [+ r for r in caplog.records if "not one of the candidates" in r.message+ ]+ assert record.levelno == logging.ERROR+ assert "'docs'" in record.message++ async def test_a_key_the_server_cannot_decrypt(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ other: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ await _save(+ session_factory,+ TENANT_ZERO_ID,+ backend="endpoint",+ endpoint=other.url,+ api_key="lost-key",+ )+ rotated = Keyring.parse("other:" + "z" * 40, legacy_secret=None)- answer = await _client(http, stub.url).decide(_request(), room_id="room-1")+ outcome = await _decide(+ _client(session_factory, http, stub.url, keyring=rotated)+ )- assert answer is not None- assert answer.agent is None- assert answer.model_choice == "billing"- assert answer.confidence == 0.45+ assert isinstance(outcome, Failed) and outcome.reason == "key_unreadable"+ assert other.received == [] and stub.received == []-async def test_a_timeout_means_nobody(- stub: _Stub, http: httpx.AsyncClient, caplog: pytest.LogCaptureFixture-) -> None:- stub.delay = 2.0+class TestEachWorkspacesBackend:+ async def test_a_saved_endpoint_applies_to_its_workspace_only(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ other: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ await _tenant_b(session_factory)+ client = _client(session_factory, http, stub.url, api_key="server-key")+ await _decide(client, TENANT_ZERO_ID)+ await _decide(client, TENANT_B)+ assert len(stub.received) == 2++ await _save(+ session_factory,+ TENANT_ZERO_ID,+ backend="endpoint",+ endpoint=other.url,+ api_key="workspace-key",+ threshold=0.75,+ )+ outcome = await _decide(client, TENANT_ZERO_ID)+ await _decide(client, TENANT_B)++ assert isinstance(outcome, Decided) and outcome.backend == "endpoint"+ [(headers, body)] = other.received+ assert headers["authorization"] == "Bearer workspace-key"+ assert body["confidence_threshold"] == 0.75+ assert len(stub.received) == 3+ assert stub.received[-1][0]["authorization"] == "Bearer server-key"++ async def test_none_overrides_the_server_default(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ await _tenant_b(session_factory)+ await _save(session_factory, TENANT_ZERO_ID, backend="none")+ client = _client(session_factory, http, stub.url)++ outcome = await _decide(client, TENANT_ZERO_ID)+ async with session_factory() as session:+ assert not await client.available(session, TENANT_ZERO_ID)+ assert await client.available(session, TENANT_B)++ assert isinstance(outcome, Unavailable)+ assert outcome.reason is not None and "turned auto-route off" in outcome.reason+ assert stub.received == []++ async def test_haiku_goes_to_the_servers_service_with_the_backend_header(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ await _save(+ session_factory,+ TENANT_ZERO_ID,+ backend="haiku_anthropic",+ api_key="fake-key-AwcA",+ )+ client = _client(session_factory, http, stub.url, api_key="server-key")- with caplog.at_level(logging.WARNING):- answer = await _client(http, stub.url).decide(_request(), room_id="room-1")+ outcome = await _decide(client)- assert answer is None- assert "did not answer within 0.5s" in caplog.text- assert "room-1" in caplog.text+ assert isinstance(outcome, Decided) and outcome.backend == "haiku_anthropic"+ [(headers, _)] = stub.received+ assert headers["x-decision-backend"] == "anthropic"+ assert headers["x-decision-anthropic-key"] == "fake-key-AwcA"+ assert headers["authorization"] == "Bearer server-key"+ async def test_bedrock_is_asked_for_only_where_the_server_allows_it(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ await _save(session_factory, TENANT_ZERO_ID, backend="haiku_bedrock")++ refused = await _decide(_client(session_factory, http, stub.url))+ allowed = await _decide(_client(session_factory, http, stub.url, bedrock=True))++ assert isinstance(refused, Unavailable)+ assert isinstance(allowed, Decided) and allowed.backend == "haiku_bedrock"+ [(headers, _)] = stub.received+ assert headers["x-decision-backend"] == "bedrock"+ assert "x-decision-anthropic-key" not in headers++ async def test_a_cached_setting_is_dropped_on_forget(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ client = _client(session_factory, http, stub.url, cache_seconds=60)+ await _decide(client)+ await _save(session_factory, TENANT_ZERO_ID, backend="none")-async def test_a_service_that_is_not_running_means_nobody(- http: httpx.AsyncClient, caplog: pytest.LogCaptureFixture-) -> None:- url = f"http://127.0.0.1:{_closed_port()}/decide"+ assert isinstance(await _decide(client), Decided)+ client.forget(TENANT_ZERO_ID)+ assert isinstance(await _decide(client), Unavailable)- with caplog.at_level(logging.WARNING):- answer = await _client(http, url).decide(_request(), room_id="room-1")- assert answer is None- assert "could not be reached" in caplog.text- assert url in caplog.text+class TestTelemetry:+ async def test_a_decision_reports_counts_and_timings_only(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ telemetry = _Telemetry()+ client = _client(session_factory, http, stub.url, telemetry=telemetry)++ await _decide(client)+ stub.body = json.dumps({**_GOOD, "agent": None, "confidence": None}).encode()+ await _decide(client)++ [(made, first), (_, second)] = telemetry.events+ assert made == "decision_made"+ assert set(first) == {"backend", "outcome", "confidence", "latency_ms"}+ assert first["backend"] == "server_default"+ assert first["outcome"] == "agent"+ assert first["confidence"] == "0.9_1.0"+ assert isinstance(first["latency_ms"], int) and first["latency_ms"] >= 0+ assert second["outcome"] == "none" and second["confidence"] == "none"+ flat = json.dumps(telemetry.events)+ for identifier in ("room-1", "deploy", "alice", "ops", "pipeline"):+ assert identifier not in flat++ async def test_a_failure_reports_its_reason(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ http: httpx.AsyncClient,+ ) -> None:+ telemetry = _Telemetry()+ url = f"http://127.0.0.1:{_closed_port()}/decide"++ await _decide(_client(session_factory, http, url, telemetry=telemetry))++ assert telemetry.events == [+ ("decision_failed", {"backend": "server_default", "reason": "unreachable"})+ ]++ @pytest.mark.parametrize(+ ("confidence", "bucket"),+ [+ (None, "none"),+ (0.2, "lt_0.5"),+ (0.5, "0.5_0.6"),+ (0.65, "0.6_0.7"),+ (0.79, "0.7_0.8"),+ (0.8, "0.8_0.9"),+ (0.95, "0.9_1.0"),+ (1.0, "0.9_1.0"),+ ],+ )+ def test_confidence_buckets(self, confidence: float | None, bucket: str) -> None:+ assert confidence_bucket(confidence) == bucket[email protected]("status", [500, 502, 401, 201])-async def test_any_status_but_200_means_nobody(- stub: _Stub,- http: httpx.AsyncClient,- caplog: pytest.LogCaptureFixture,- status: int,-) -> None:- stub.status = status- stub.body = b'{"detail": "model server unreachable"}'+class TestTheTestButton:+ async def test_a_working_endpoint(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ other: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ other.body = json.dumps({**_GOOD, "agent": "deploy-bot"}).encode()- with caplog.at_level(logging.WARNING):- answer = await _client(http, stub.url).decide(_request(), room_id="room-1")+ outcome = await _client(session_factory, http, None).test(+ backend="endpoint", endpoint=other.url, api_key="k", threshold=0.5+ )- assert answer is None- assert f"answered {status}" in caplog.text- assert "model server unreachable" in caplog.text+ assert outcome.ok and outcome.agent == "deploy-bot"+ assert outcome.confidence == 0.91+ assert outcome.latency_ms is not None+ [(headers, body)] = other.received+ assert headers["authorization"] == "Bearer k"+ assert body["confidence_threshold"] == 0.5+ assert {a["name"] for a in body["agents"]} == {+ "deploy-bot",+ "billing-bot",+ "docs-bot",+ }++ async def test_an_endpoint_that_does_not_answer(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ http: httpx.AsyncClient,+ ) -> None:+ outcome = await _client(session_factory, http, None).test(+ backend="endpoint",+ endpoint=f"http://127.0.0.1:{_closed_port()}/decide",+ api_key=None,+ threshold=None,+ )++ assert not outcome.ok and outcome.error_code == "unreachable"++ async def test_a_slow_endpoint(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ other: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ other.delay = 2.0++ outcome = await _client(session_factory, http, None).test(+ backend="endpoint", endpoint=other.url, api_key=None, threshold=None+ )++ assert outcome.error_code == "timeout"++ @pytest.mark.parametrize(+ ("backend", "status", "body", "code"),+ [+ ("endpoint", 401, b'{"detail": "bad token"}', "unauthorized"),+ (+ "haiku_anthropic",+ 502,+ b'{"detail": "anthropic: 401 invalid x-api-key"}',+ "key_refused",+ ),+ (+ "haiku_anthropic",+ 400,+ b'{"detail": "no", "code": "backend_unavailable"}',+ "backend_unavailable",+ ),+ ("endpoint", 500, b'{"detail": "boom"}', "bad_answer"),+ ("endpoint", 200, b"[]", "bad_answer"),+ ],+ )+ async def test_classified_errors(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ backend: str,+ status: int,+ body: bytes,+ code: str,+ ) -> None:+ stub.status, stub.body = status, body++ outcome = await _client(session_factory, http, stub.url).test(+ backend=backend, # type: ignore[arg-type]+ endpoint=stub.url if backend == "endpoint" else None,+ api_key="fake-key",+ threshold=None,+ )++ assert not outcome.ok and outcome.error_code == code+ assert outcome.error_message++ async def test_haiku_without_a_server_service_or_key(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ no_service = await _client(session_factory, http, None).test(+ backend="haiku_anthropic", endpoint=None, api_key="k", threshold=None+ )+ no_key = await _client(session_factory, http, stub.url).test(+ backend="haiku_anthropic", endpoint=None, api_key=None, threshold=None+ )+ bedrock = await _client(session_factory, http, stub.url).test(+ backend="haiku_bedrock", endpoint=None, api_key=None, threshold=None+ )++ assert no_service.error_code == "not_configured"+ assert no_key.error_code == "not_configured"+ assert bedrock.error_code == "backend_unavailable"+ assert stub.received == []++ async def test_the_server_default_with_the_servers_own_key(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ outcome = await _client(+ session_factory, http, stub.url, api_key="server-key"+ ).test(+ backend="default",+ endpoint=None,+ api_key="workspace-key",+ threshold=None,+ )+ missing = await _client(session_factory, http, None).test(+ backend="default", endpoint=None, api_key=None, threshold=None+ )++ assert outcome.ok and outcome.agent == "deploy"+ [(headers, _)] = stub.received+ assert headers["authorization"] == "Bearer server-key"+ assert "x-decision-backend" not in headers+ assert missing.error_code == "not_configured"@pytest.mark.parametrize(- "body",+ ("url", "posted"),[- b"not json",- b"[]",- json.dumps({k: v for k, v in _GOOD.items() if k != "agent"}).encode(),- json.dumps({**_GOOD, "agent": 7}).encode(),- json.dumps({**_GOOD, "confidence": "high"}).encode(),- json.dumps({**_GOOD, "threshold": None}).encode(),+ ("http://decision.test", "http://decision.test/decide"),+ ("http://decision.test:8230/", "http://decision.test:8230/decide"),+ ("http://decision.test/decide", "http://decision.test/decide"),+ ("https://proxy.test/team/decide", "https://proxy.test/team/decide"),],)-async def test_a_malformed_body_means_nobody(- stub: _Stub,- http: httpx.AsyncClient,- caplog: pytest.LogCaptureFixture,- body: bytes,-) -> None:- stub.body = body+def test_a_base_url_gets_the_decide_route(url: str, posted: str) -> None:+ assert decide_url(url) == posted- with caplog.at_level(logging.WARNING):- answer = await _client(http, stub.url).decide(_request(), room_id="room-1")- assert answer is None- assert "answered something other than a decision" in caplog.text+class TestTheOutboundPolicy:+ async def test_a_workspace_endpoint_it_refuses_is_a_failure_and_not_sent(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ other: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ await _save(+ session_factory, TENANT_ZERO_ID, backend="endpoint", endpoint=other.url+ )+ telemetry = _Telemetry()+ outcome = await _decide(+ _client(session_factory, http, stub.url, allowed="", telemetry=telemetry)+ )-async def test_a_name_that_was_not_a_candidate_means_nobody(- stub: _Stub, http: httpx.AsyncClient, caplog: pytest.LogCaptureFixture-) -> None:- stub.body = json.dumps({**_GOOD, "agent": "docs"}).encode()+ assert isinstance(outcome, Failed) and outcome.reason == "endpoint_refused"+ assert other.received == [] and stub.received == []+ assert telemetry.events == [+ ("decision_failed", {"backend": "endpoint", "reason": "endpoint_refused"})+ ]++ async def test_a_listed_private_endpoint_is_reached(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ other: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ await _save(+ session_factory, TENANT_ZERO_ID, backend="endpoint", endpoint=other.url+ )++ outcome = await _decide(+ _client(session_factory, http, stub.url, allowed="127.0.0.0/8")+ )- with caplog.at_level(logging.WARNING):- answer = await _client(http, stub.url).decide(_request(), room_id="room-1")+ assert isinstance(outcome, Decided) and outcome.backend == "endpoint"+ assert len(other.received) == 1++ async def test_the_server_default_and_haiku_are_not_checked(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ stub: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ client = _client(session_factory, http, stub.url, allowed="")++ default = await _decide(client)+ await _save(+ session_factory,+ TENANT_ZERO_ID,+ backend="haiku_anthropic",+ api_key="fake-key",+ )+ haiku = await _decide(client)++ assert isinstance(default, Decided) and default.backend == "server_default"+ assert isinstance(haiku, Decided) and haiku.backend == "haiku_anthropic"+ assert len(stub.received) == 2++ async def test_the_test_button_says_refused(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ other: _Stub,+ http: httpx.AsyncClient,+ ) -> None:+ outcome = await _client(session_factory, http, None, allowed="").test(+ backend="endpoint", endpoint=other.url, api_key=None, threshold=None+ )- assert answer is None- [record] = [r for r in caplog.records if "not one of the candidates" in r.message]- assert record.levelno == logging.ERROR- assert "'docs'" in record.message+ assert outcome.error_code == "refused"+ assert outcome.error_message is not None+ assert "OUTBOUND_ALLOWED_PRIVATE_HOSTS" in outcome.error_message+ assert other.received == []diff --git a/core/tests/switch_core/delivery/test_conversation_journeys.py b/core/tests/switch_core/delivery/test_conversation_journeys.pyindex 6294a09d3..9b7556bab 100644--- a/core/tests/switch_core/delivery/test_conversation_journeys.py+++ b/core/tests/switch_core/delivery/test_conversation_journeys.py@@ -27,7 +27,12 @@ from switch_core.bridges.agent.commands import (_cmd_unassign_agent,)from switch_core.bridges.agent.protocol.types import AgentEvent, MessagePayload-from switch_core.clients.admin_messages import ADMIN_MARKER, AUTO_REPLY_FLAG+from switch_core.clients.admin_messages import (+ ADMIN_MARKER,+ AUTO_REPLY_FLAG,+ AdminMessageType,+ admin_extra_content,+)from switch_core.clients.room_meta import RoomMetafrom switch_core.db.models import (TENANT_ZERO_ID,@@ -41,10 +46,12 @@ from switch_core.db.models import (from switch_core.db.session_scope import tenant_sessionfrom switch_core.db.stores.agent_store import AgentStorefrom switch_core.db.stores.client_store import ClientStore+from switch_core.db.stores.decision_settings_store import DecisionSettingsStorefrom switch_core.db.stores.external_user_store import ExternalUserStorefrom switch_core.db.stores.room_role_store import RoomRoleStorefrom switch_core.db.stores.room_store import RoomStore-from switch_core.decision.client import DecisionClient+from switch_core.decision.client import DynamicDecisionClient+from switch_core.decision.settings import DecisionAvailability, ServerDefaultfrom switch_core.delivery import router as router_modulefrom switch_core.delivery.conversation import ConversationSettingsfrom switch_core.delivery.recipient import AgentRecipient@@ -66,7 +73,7 @@ class Journey:self,world: World,settings: ConversationSettings,- decisions: DecisionClient | None = None,+ decisions: DynamicDecisionClient | None = None,) -> None:self.world = worldself.settings = settings@@ -76,6 +83,8 @@ class Journey:self.clients: dict[str, Client] = {}self.at: dict[str, float] = {}self.notices: list[str] = []+ # What Switch told the room on its own: "auto-route is unavailable".+ self.room_notices: list[str] = []async def start(self) -> Journey:world = self.world@@ -96,8 +105,18 @@ class Journey:self.clients[name] = clientsession.add(ClientRoom(client_id=client.id, room_id=room.id))await session.commit()+ self.process.router.use_notices(self._room_notice)return self+ async def _room_notice(self, _tenant_id: str, _room: str, body: str) -> None:+ self.room_notices.append(body)+ await self.process.say(+ self.room,+ body,+ sender=self.clients["admin"],+ extra_content=admin_extra_content(AdminMessageType.AUTO_ROUTE_UNAVAILABLE),+ )+async def _set_the_clock(self, at: float) -> None:async with self.world.session_factory() as session:for event_id, then in self.at.items():@@ -240,8 +259,17 @@ class Journey:client_store=ClientStore(),_connections=SimpleNamespace(live_connection_ids=lambda: set()),_follow_the_conversation_enabled=True,- _decision_routing_enabled=self.decisions is not None,- _decision_endpoint_configured=self.decisions is not None,+ _decision_availability=(+ self.decisions.availability+ if self.decisions is not None+ else DecisionAvailability(+ enabled=False,+ server=ServerDefault(+ endpoint_url=None, api_key=None, bedrock_enabled=False+ ),+ store=DecisionSettingsStore(),+ )+ ),)async def command(self, who: str, command: str, args: str, *, at: float) -> str:diff --git a/core/tests/switch_core/delivery/test_decision_routing.py b/core/tests/switch_core/delivery/test_decision_routing.pyindex 3109abce8..b959af8d6 100644--- a/core/tests/switch_core/delivery/test_decision_routing.py+++ b/core/tests/switch_core/delivery/test_decision_routing.py@@ -11,32 +11,43 @@ is read back from `agent_events`.from __future__ import annotations+import asyncioimport jsonimport loggingfrom collections.abc import Awaitable, Callable-from datetime import UTC, datetime-from typing import Any+from datetime import UTC, datetime, timedelta+from typing import Any, Literalimport httpximport pytest-from sqlalchemy import delete, select, update+from sqlalchemy import Text, cast, delete, func, select, update+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerfrom switch_core.addressing import owner_only_policyfrom switch_core.bridges.agent.protocol.event_buffer import is_notifiablefrom switch_core.bridges.agent.protocol.message_events import message_eventfrom switch_core.bridges.agent.protocol.types import AgentEvent, MessagePayloadfrom switch_core.db.models import (+ TENANT_ZERO_ID,Agent,AgentEventRow,ClientRoom,+ Message,+ PendingDecision,RoleLease,Room,RoomRole,)-from switch_core.db.stores.room_store import RoomStore-from switch_core.decision.client import DecisionClient-from switch_core.delivery.decision import THREAD_CONTEXT+from switch_core.db.stores.decision_settings_store import DecisionSettingsStore+from switch_core.db.stores.pending_decision_store import PendingDecisionStore+from switch_core.db.stores.room_store import DECISION_NOTICE_AT_KEY, RoomStore+from switch_core.decision.client import DynamicDecisionClient+from switch_core.decision.settings import DecisionAvailability, ServerDefault+from switch_core.delivery.decision import DECISION_NOTICE, THREAD_CONTEXT+from switch_core.outbound import OutboundPolicyfrom switch_core.transport import InboundMessage+from tests.conftest import TEST_KEYRING+from tests.routing import process_onfrom tests.switch_core.delivery.test_conversation_journeys import ON, Journeyfrom tests.switch_core.delivery.world import World@@ -51,9 +62,11 @@ class DecisionService:self.status = 200self.error: Exception | None = Noneself.during: Callable[[], Awaitable[None]] | None = None+ self.headers: list[dict[str, str]] = []async def handle(self, request: httpx.Request) -> httpx.Response:self.requests.append(json.loads(request.content))+ self.headers.append(dict(request.headers))if self.during is not None:await self.during()if self.error is not None:@@ -71,12 +84,31 @@ class DecisionService:},)- def client(self) -> DecisionClient:- return DecisionClient(- endpoint_url="http://decision.test/decide",- api_key=None,- timeout_seconds=1.0,+ def client(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> DynamicDecisionClient:+ """A decision client whose server default is this stub, reading each+ workspace's settings afresh on every decision."""+ return DynamicDecisionClient(+ session_factory=session_factory,+ availability=DecisionAvailability(+ enabled=True,+ server=ServerDefault(+ endpoint_url="http://decision.test/decide",+ api_key=None,+ bedrock_enabled=False,+ ),+ store=DecisionSettingsStore(),+ ),+ keyring=TEST_KEYRING,client=httpx.AsyncClient(transport=httpx.MockTransport(self.handle)),+ guarded_client=httpx.AsyncClient(+ transport=httpx.MockTransport(self.handle)+ ),+ outbound_policy=OutboundPolicy.parse("10.9.0.0/16"),+ timeout_seconds=1.0,+ telemetry=None,+ cache_seconds=0,)@@ -87,7 +119,7 @@ def service() -> DecisionService:@pytest.fixtureasync def room(world: World, service: DecisionService) -> Journey:- journey = await Journey(world, ON, service.client()).start()+ journey = await Journey(world, ON, service.client(world.session_factory)).start()reply = await journey.command("alice", "auto-route", "on", at=0)assert "Automatic routing is **on**" in replyreturn journey@@ -413,7 +445,9 @@ class TestWhatTheAgentIsGiven:assert event.payload.body == "can someone check why the deploy failed?"assert own.room_seq > 0assert "candidates=2 choice=helper" in caplog.text- assert "confidence=0.9 threshold=0.6 latency_ms=5" in caplog.text+ assert "confidence=0.9 threshold=0.6 latency_ms=" in caplog.text+ assert "backend=server_default" in caplog.text+ assert event.payload.routed_reason == "decided"async def test_a_mention_is_not_marked_routed(self, world: World, room: Journey@@ -466,7 +500,7 @@ class TestWhatTheAgentIsGiven:if r.levelno == logging.WARNING and "Decision service" in r.message]assert len(warnings) == 2- assert "no agent receives it" in warnings[0].message+ assert "gave no answer" in warnings[0].messageasync def test_a_name_that_was_not_a_candidate_delivers_nothing(self,@@ -567,7 +601,9 @@ class TestWhatTheAgentIsGiven:class TestTheRoutedPayload:- def _event(self, *, routed: bool) -> AgentEvent:+ def _event(+ self, *, routed_reason: Literal["decided", "fallback"] | None+ ) -> AgentEvent:return message_event(room_id="room",bridge_id=None,@@ -583,22 +619,24 @@ class TestTheRoutedPayload:sender_name="alice",body="hi",thread_id=None,- routed=routed,+ routed_reason=routed_reason,)def test_a_routed_message_is_addressed_and_notifiable(self) -> None:- event = self._event(routed=True)+ event = self._event(routed_reason="decided")assert isinstance(event.payload, MessagePayload)assert event.payload.routed+ assert event.payload.routed_reason == "decided"assert event.payload.addressedassert is_notifiable(event)assert AgentEvent.model_validate(event.model_dump(mode="json")) == eventdef test_not_routed_unless_said(self) -> None:- payload = self._event(routed=False).payload+ payload = self._event(routed_reason=None).payloadassert isinstance(payload, MessagePayload)assert not payload.routed+ assert payload.routed_reason is Noneassert (MessagePayload.model_validate({@@ -612,3 +650,371 @@ class TestTheRoutedPayload:).routedis False)+++async def _pending(world: World) -> list[PendingDecision]:+ async with world.session_factory() as session:+ return list((await session.scalars(select(PendingDecision))).all())+++def _stop_waking(journey: Journey) -> None:+ """The process that writes messages stops before it decides them, as a+ process does when it is restarted with decisions waiting."""+ journey.process.router.wake_decisions = lambda _tenant_id: None # type: ignore[method-assign]+++class TestDecisionsSurviveARestart:+ async def test_the_waiting_message_is_recorded_with_the_message(+ self, world: World, room: Journey, service: DecisionService+ ) -> None:+ _stop_waking(room)++ told = await room.say("bob", "can someone check the deploy?", at=10)++ [pending] = await _pending(world)+ assert pending.message_key == told+ assert pending.room_id == world.room_id+ async with world.session_factory() as session:+ message = await session.scalar(+ select(Message).where(Message.transport_event_id == told)+ )+ assert message is not None and pending.room_seq == message.seq+ assert service.requests == []++ async def test_after_a_restart_it_is_decided_and_delivered_once(+ self, world: World, room: Journey, service: DecisionService+ ) -> None:+ _stop_waking(room)+ service.pick = "helper"+ told = await room.say("bob", "can someone check the deploy?", at=10)++ restarted = process_on(+ world.session_factory,+ conversation=ON,+ decisions=service.client(world.session_factory),+ )+ assert await restarted.router.decide_pending(TENANT_ZERO_ID) == 1+ assert await restarted.router.decide_pending(TENANT_ZERO_ID) == 0++ assert await room.reached(told) == {"helper"}+ assert len(service.requests) == 1+ assert await _pending(world) == []+ async with world.session_factory() as session:+ events = (+ await session.scalars(+ select(AgentEventRow).where(AgentEventRow.message_key == told)+ )+ ).all()+ assert len(events) == 1++ async def test_a_message_waiting_too_long_is_dropped_and_logged(+ self,+ world: World,+ room: Journey,+ service: DecisionService,+ caplog: pytest.LogCaptureFixture,+ ) -> None:+ _stop_waking(room)+ service.pick = "helper"+ told = await room.say("bob", "can someone check the deploy?", at=10)+ async with world.session_factory() as session:+ await session.execute(+ update(PendingDecision).values(+ created_at=func.now() - timedelta(minutes=10)+ )+ )+ await session.commit()++ with caplog.at_level(logging.WARNING):+ assert await room.process.router.decide_pending(TENANT_ZERO_ID) == 0++ assert await room.reached(told) == set()+ assert service.requests == []+ assert await _pending(world) == []+ assert "outcome=expired" in caplog.text+ assert told in caplog.text++ async def test_two_processes_never_deliver_it_twice(+ self, world: World, room: Journey, service: DecisionService+ ) -> None:+ _stop_waking(room)+ service.pick = "helper"+ other = process_on(+ world.session_factory,+ conversation=ON,+ decisions=service.client(world.session_factory),+ )+ told = await room.say("bob", "can someone check the deploy?", at=10)++ first, second = await asyncio.gather(+ room.process.router.decide_pending(TENANT_ZERO_ID),+ other.router.decide_pending(TENANT_ZERO_ID),+ )++ assert sorted([first, second]) == [0, 1]+ assert len(service.requests) == 1+ assert await room.reached(told) == {"helper"}++ async def test_a_claim_whose_process_stopped_is_taken_over(+ self, world: World, room: Journey, service: DecisionService+ ) -> None:+ _stop_waking(room)+ service.pick = "helper"+ told = await room.say("bob", "can someone check the deploy?", at=10)+ async with world.session_factory() as session:+ claimed = await PendingDecisionStore().claim(+ session, tenant_id=TENANT_ZERO_ID, lease_seconds=60, limit=10+ )+ await session.commit()+ assert [p.message_key for p in claimed] == [told]++ # Leased: nobody else takes it.+ assert await room.process.router.decide_pending(TENANT_ZERO_ID) == 0+ async with world.session_factory() as session:+ await session.execute(+ update(PendingDecision).values(+ claimed_until=func.now() - timedelta(seconds=1)+ )+ )+ await session.commit()++ assert await room.process.router.decide_pending(TENANT_ZERO_ID) == 1+ assert await room.reached(told) == {"helper"}++ async def test_one_settled_elsewhere_is_not_delivered_again(+ self, world: World, room: Journey, service: DecisionService+ ) -> None:+ service.pick = "helper"++ async def settled_elsewhere() -> None:+ async with world.session_factory() as session:+ await session.execute(delete(PendingDecision))+ await session.commit()++ service.during = settled_elsewhere++ told = await room.say("bob", "can someone check the deploy?", at=10)++ assert await room.reached(told) == set()++ async def test_a_rooms_messages_are_decided_in_order(+ self, world: World, room: Journey, service: DecisionService+ ) -> None:+ _stop_waking(room)+ first = await room.say("bob", "can someone check the deploy?", at=10)+ second = await room.say("alice", "and the release notes?", at=20)++ await room.process.router.decide_pending(TENANT_ZERO_ID)++ assert [r["message"]["text"] for r in service.requests] == [+ "can someone check the deploy?",+ "and the release notes?",+ ]+ assert first != second+++class TestTheRoomIsTold:+ async def test_the_first_failure_tells_the_room(+ self, room: Journey, service: DecisionService+ ) -> None:+ service.status = 502++ told = await room.say("bob", "can someone check the deploy?", at=10)++ assert room.room_notices == [DECISION_NOTICE]+ assert await room.reached(told) == set()++ async def test_a_second_failure_within_the_hour_does_not(+ self, room: Journey, service: DecisionService+ ) -> None:+ service.error = httpx.ConnectError("connection refused")++ await room.say("bob", "can someone check the deploy?", at=10)+ await room.say("alice", "is anyone looking at the gateway?", at=600)++ assert room.room_notices == [DECISION_NOTICE]+ assert len(service.requests) == 2++ async def test_a_failure_an_hour_later_tells_it_again(+ self, world: World, room: Journey, service: DecisionService+ ) -> None:+ service.status = 500+ await room.say("bob", "can someone check the deploy?", at=10)+ async with world.session_factory() as session:+ await session.execute(+ update(Room)+ .where(Room.id == world.room_id)+ .values(+ metadata_=Room.metadata_.op("||")(+ func.jsonb_build_object(+ DECISION_NOTICE_AT_KEY,+ cast(func.now() - timedelta(minutes=61), Text),+ )+ )+ )+ )+ await session.commit()++ await room.say("alice", "is anyone looking at the gateway?", at=20)++ assert room.room_notices == [DECISION_NOTICE, DECISION_NOTICE]++ async def test_a_decision_of_nobody_does_not(+ self, room: Journey, service: DecisionService+ ) -> None:+ service.pick = None++ await room.say("bob", "what's the weather in London tomorrow?", at=10)++ assert room.room_notices == []+ assert len(service.requests) == 1+++class TestTheTriageAgent:+ async def test_it_gets_the_message_while_the_service_fails(+ self, world: World, room: Journey, service: DecisionService+ ) -> None:+ reply = await room.command("alice", "auto-route", "fallback @reviewer", at=5)+ assert "is now this room's triage agent" in reply+ service.status = 502++ told = await room.say("bob", "can someone check the deploy?", at=10)++ assert await room.reached(told) == {"reviewer"}+ event = AgentEvent.model_validate(+ (await _own_event(world, "reviewer", told)).payload+ )+ assert isinstance(event.payload, MessagePayload)+ assert event.payload.routed+ assert event.payload.routed_reason == "fallback"+ assert event.payload.addressed+ assert room.room_notices == [DECISION_NOTICE]++ async def test_not_for_a_decision_of_nobody(+ self, room: Journey, service: DecisionService+ ) -> None:+ await room.command("alice", "auto-route", "fallback @reviewer", at=5)+ service.pick = None++ told = await room.say("bob", "thanks, that sorted it for today then", at=10)++ assert await room.reached(told) == set()+ assert len(service.requests) == 1++ async def test_not_when_its_policy_refuses_the_sender(+ self,+ world: World,+ room: Journey,+ service: DecisionService,+ caplog: pytest.LogCaptureFixture,+ ) -> None:+ await room.command("alice", "auto-route", "fallback @reviewer", at=5)+ await _owner_only(world, "reviewer")+ service.error = httpx.ReadTimeout("timed out")++ with caplog.at_level(logging.INFO):+ told = await room.say("bob", "can someone check the deploy?", at=10)++ assert await room.reached(told) == set()+ assert "routed_reason=fallback outcome=dropped_not_permitted" in caplog.text++ async def test_it_cannot_be_the_assigned_agent(self, room: Journey) -> None:+ await room.command("alice", "assign-agent", "@reviewer", at=5)++ reply = await room.command("alice", "auto-route", "fallback @reviewer", at=6)++ assert "assigned agent" in reply+ assert "Nothing was changed" in reply++ async def test_off_clears_it(self, room: Journey, service: DecisionService) -> None:+ await room.command("alice", "auto-route", "fallback @reviewer", at=5)+ reply = await room.command("alice", "auto-route", "fallback off", at=6)+ assert "no longer has a triage agent" in reply+ service.status = 502++ told = await room.say("bob", "can someone check the deploy?", at=10)++ assert await room.reached(told) == set()+++class TestAWorkspaceWithNoBackend:+ async def test_none_keeps_todays_behaviour_and_asks_nobody(+ self, world: World, room: Journey, service: DecisionService+ ) -> None:+ async with world.session_factory() as session:+ await DecisionSettingsStore().save(+ session,+ TENANT_ZERO_ID,+ backend="none",+ endpoint=None,+ api_key_encrypted=None,+ threshold=None,+ )+ await session.commit()+ answer = await room.post("helper", "It starts the stack", at=6)++ told = await room.say("bob", "can someone check why the deploy failed?", at=10)+ in_thread = await room.say("alice", "why?", at=20, thread=answer)+ reply = await room.command("alice", "auto-route", "", at=30)++ assert service.requests == []+ assert await _pending(world) == []+ assert await room.reached(told) == set()+ assert await room.reached(in_thread) == {"helper"}+ assert room.room_notices == []+ assert "turned auto-route off" in reply++ async def test_a_workspace_endpoint_is_asked_instead_of_the_default(+ self, world: World, room: Journey, service: DecisionService+ ) -> None:+ own = DecisionService()+ own.pick = "reviewer"+ room.decisions._guarded_client = httpx.AsyncClient( # type: ignore[union-attr]+ transport=httpx.MockTransport(own.handle)+ )+ room.process.router._decisions = room.decisions+ async with world.session_factory() as session:+ await DecisionSettingsStore().save(+ session,+ TENANT_ZERO_ID,+ backend="endpoint",+ endpoint="http://10.9.0.5/decide",+ api_key_encrypted=TEST_KEYRING.encrypt("own-token"),+ threshold=0.7,+ )+ await session.commit()++ told = await room.say("bob", "can someone check the deploy?", at=10)++ assert service.requests == []+ [request] = own.requests+ assert request["confidence_threshold"] == 0.7+ assert own.headers[-1]["authorization"] == "Bearer own-token"+ assert await room.reached(told) == {"reviewer"}++ async def test_a_workspace_endpoint_the_policy_refuses_is_a_failure(+ self,+ world: World,+ room: Journey,+ service: DecisionService,+ caplog: pytest.LogCaptureFixture,+ ) -> None:+ service.pick = "helper"+ async with world.session_factory() as session:+ await DecisionSettingsStore().save(+ session,+ TENANT_ZERO_ID,+ backend="endpoint",+ endpoint="http://10.0.0.5:8230",+ api_key_encrypted=None,+ threshold=None,+ )+ await session.commit()++ with caplog.at_level(logging.WARNING):+ told = await room.say("bob", "can someone check the deploy?", at=10)++ assert service.requests == []+ assert await room.reached(told) == set()+ assert room.room_notices == [DECISION_NOTICE]+ assert "refused by the server's outbound policy" in caplog.textdiff --git a/core/tests/switch_core/fixtures/agent_controllers/stream_frames.json b/core/tests/switch_core/fixtures/agent_controllers/stream_frames.jsonindex 7bf392b0f..2962b5a25 100644--- a/core/tests/switch_core/fixtures/agent_controllers/stream_frames.json+++ b/core/tests/switch_core/fixtures/agent_controllers/stream_frames.json@@ -60,7 +60,8 @@"opening_message": null,"sender_role": null,"switch_notice": null,- "routed": false+ "routed": false,+ "routed_reason": null},"sequence": 1,"missed": {diff --git a/core/tests/switch_core/gateway/test_chats_groups.py b/core/tests/switch_core/gateway/test_chats_groups.pyindex 8aee6c204..fe5919a10 100644--- a/core/tests/switch_core/gateway/test_chats_groups.py+++ b/core/tests/switch_core/gateway/test_chats_groups.py@@ -1416,6 +1416,7 @@ async def test_only_a_manager_renames_a_group_and_everyone_sees_it(None, # type: ignore[arg-type]None, # type: ignore[arg-type]None, # type: ignore[arg-type]+ None, # type: ignore[arg-type]user,False,)diff --git a/core/tests/switch_core/gateway/test_decision_settings.py b/core/tests/switch_core/gateway/test_decision_settings.pynew file mode 100644index 000000000..bd4321d42--- /dev/null+++ b/core/tests/switch_core/gateway/test_decision_settings.py@@ -0,0 +1,600 @@+"""A workspace's decision backend, through its routes on real Postgres.++Signed in with real session cookies bound to a workspace: only that+workspace's admins get in, each sees and changes only its own row, the key+never comes back, and the Test button asks a stub decision service.+"""++from __future__ import annotations++import json+from collections.abc import AsyncIterator+from types import SimpleNamespace+from typing import Any++import httpx+import pytest+from fastapi import FastAPI+from sqlalchemy import select+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.db.models import (+ TENANT_ZERO_ID,+ DecisionSettings,+ Tenant,+ TenantMember,+ User,+)+from switch_core.db.stores.decision_settings_store import DecisionSettingsStore+from switch_core.db.stores.user_store import UserStore+from switch_core.decision.client import DynamicDecisionClient+from switch_core.decision.settings import DecisionAvailability, ServerDefault+from switch_core.gateway import dependencies as gw_deps+from switch_core.gateway.auth import create_jwt+from switch_core.gateway.decision_settings import get_decision_client+from switch_core.gateway.decision_settings import router as decision_router+from switch_core.keys import Keyring+from switch_core.outbound import OutboundPolicy++KEYRING = Keyring.parse("test:" + "d" * 40, legacy_secret=None)+TENANT_B = "decision-settings-tenant-b"+SERVER_URL = "http://decision.test/decide"+++class _Service:+ """The decision service behind every URL, recording what it is sent."""++ def __init__(self) -> None:+ self.calls: list[tuple[str, dict[str, str], dict[str, Any]]] = []+ self.status = 200++ def handle(self, request: httpx.Request) -> httpx.Response:+ self.calls.append(+ (str(request.url), dict(request.headers), json.loads(request.content))+ )+ return httpx.Response(+ self.status,+ json={+ "agent": "deploy-bot",+ "model_choice": "deploy-bot",+ "confidence": 0.88,+ "threshold": 0.6,+ "model": "stub",+ "latency_ms": 4,+ },+ )+++def _app(+ session_factory: async_sessionmaker[AsyncSession],+ service: _Service,+ *,+ server_url: str | None = SERVER_URL,+ bedrock: bool = False,+ allowed: str = "10.9.0.0/16",+) -> FastAPI:+ async def _session() -> AsyncIterator[AsyncSession]:+ async with session_factory() as session:+ yield session++ client = DynamicDecisionClient(+ session_factory=session_factory,+ availability=DecisionAvailability(+ enabled=True,+ server=ServerDefault(+ endpoint_url=server_url,+ api_key="server-key" if server_url else None,+ bedrock_enabled=bedrock,+ ),+ store=DecisionSettingsStore(),+ ),+ keyring=KEYRING,+ client=httpx.AsyncClient(transport=httpx.MockTransport(service.handle)),+ # The workspace's endpoints are checked against the policy before the+ # request; the request itself is the stub's.+ guarded_client=httpx.AsyncClient(transport=httpx.MockTransport(service.handle)),+ outbound_policy=OutboundPolicy.parse(allowed),+ timeout_seconds=1.0,+ telemetry=None,+ cache_seconds=60,+ )+ app = FastAPI()+ app.include_router(decision_router)+ app.dependency_overrides[gw_deps.get_session] = _session+ app.dependency_overrides[gw_deps.get_session_factory] = lambda: session_factory+ app.dependency_overrides[gw_deps.get_user_store] = lambda: UserStore()+ app.dependency_overrides[gw_deps.get_config] = lambda: SimpleNamespace(+ keyring=KEYRING, gateway_tenant_choice_enabled=False+ )+ app.dependency_overrides[get_decision_client] = lambda: client+ app.state.client = client+ return app+++async def _person(+ session_factory: async_sessionmaker[AsyncSession],+ name: str,+ *,+ tenant_id: str = TENANT_ZERO_ID,+ role: str,+) -> User:+ async with session_factory() as session:+ if tenant_id != TENANT_ZERO_ID and await session.get(Tenant, tenant_id) is None:+ session.add(Tenant(id=tenant_id, slug=tenant_id, name=tenant_id))+ await session.flush()+ user = User(name=name, email=f"{name}@example.invalid", role="user")+ session.add(user)+ await session.flush()+ session.add(TenantMember(tenant_id=tenant_id, user_id=user.id, role=role))+ await session.commit()+ return user+++def _as(app: FastAPI, user: User, tenant_id: str = TENANT_ZERO_ID) -> httpx.AsyncClient:+ return httpx.AsyncClient(+ transport=httpx.ASGITransport(app=app),+ base_url="http://test",+ cookies={+ "switch_auth": create_jwt(+ user.id, user.email, user.role, KEYRING, tenant_id+ )+ },+ )+++async def _row(+ session_factory: async_sessionmaker[AsyncSession], tenant_id: str+) -> DecisionSettings | None:+ async with session_factory() as session:+ return await session.scalar(+ select(DecisionSettings).where(DecisionSettings.tenant_id == tenant_id)+ )+++def service() -> _Service:+ return _Service()+++async def test_a_member_who_is_not_an_admin_is_refused_every_route(+ session_factory: async_sessionmaker[AsyncSession], service: _Service+) -> None:+ member = await _person(session_factory, "mallory", role="member")+ async with _as(_app(session_factory, service), member) as client:+ responses = [+ await client.get("/decision-settings"),+ await client.put("/decision-settings", json={"backend": "none"}),+ await client.delete("/decision-settings"),+ await client.post("/decision-settings/test", json={"backend": "default"}),+ ]++ assert [r.status_code for r in responses] == [403, 403, 403, 403]+ assert await _row(session_factory, TENANT_ZERO_ID) is None+ assert service.calls == []+++async def test_with_no_row_the_server_default_applies(+ session_factory: async_sessionmaker[AsyncSession], service: _Service+) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ async with _as(_app(session_factory, service), admin) as client:+ body = (await client.get("/decision-settings")).json()++ assert body == {+ "source": "server_default",+ "effective": {+ "backend": "endpoint",+ "endpoint": SERVER_URL,+ "threshold": None,+ "available": True,+ "reason": None,+ },+ "workspace": None,+ "server_default": {+ "configured": True,+ "endpoint": SERVER_URL,+ "has_api_key": True,+ },+ "options": {"haiku_anthropic": True, "haiku_bedrock": False},+ }+++async def test_no_server_default_offers_no_haiku_and_says_why(+ session_factory: async_sessionmaker[AsyncSession], service: _Service+) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ async with _as(_app(session_factory, service, server_url=None), admin) as client:+ body = (await client.get("/decision-settings")).json()+ haiku = await client.put(+ "/decision-settings",+ json={"backend": "haiku_anthropic", "api_key": "fake-key"},+ )++ assert body["effective"]["available"] is False+ assert "Decision routing" in body["effective"]["reason"]+ assert body["server_default"]["configured"] is False+ assert body["options"] == {"haiku_anthropic": False, "haiku_bedrock": False}+ assert haiku.status_code == 422+++async def test_a_haiku_key_round_trips_as_its_last_four(+ session_factory: async_sessionmaker[AsyncSession], service: _Service+) -> None:+ admin = await _person(session_factory, "ada", role="owner")+ async with _as(_app(session_factory, service), admin) as client:+ saved = await client.put(+ "/decision-settings",+ json={+ "backend": "haiku_anthropic",+ "api_key": "fake-key-AwcA",+ "threshold": 0.7,+ },+ )+ again = await client.get("/decision-settings")++ assert saved.status_code == 200, saved.text+ body = saved.json()+ assert body["source"] == "workspace"+ assert body["effective"]["backend"] == "haiku_anthropic"+ workspace = body["workspace"]+ assert workspace["has_api_key"] is True+ assert workspace["api_key_last4"] == "AwcA"+ assert workspace["api_key_unreadable"] is False+ assert workspace["threshold"] == 0.7+ assert "fake-key" not in saved.text+ assert again.json() == body+ row = await _row(session_factory, TENANT_ZERO_ID)+ assert row is not None and row.api_key_encrypted is not None+ assert "AwcA" not in row.api_key_encrypted+++async def test_haiku_needs_a_key_new_or_kept_for_the_same_backend(+ session_factory: async_sessionmaker[AsyncSession], service: _Service+) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ async with _as(_app(session_factory, service), admin) as client:+ missing = await client.put(+ "/decision-settings", json={"backend": "haiku_anthropic"}+ )+ await client.put(+ "/decision-settings",+ json={+ "backend": "endpoint",+ "endpoint": "http://10.9.0.5/decide",+ "api_key": "endpoint-token",+ },+ )+ not_carried = await client.put(+ "/decision-settings", json={"backend": "haiku_anthropic"}+ )+ await client.put(+ "/decision-settings",+ json={"backend": "haiku_anthropic", "api_key": "fake-key-1234"},+ )+ kept = await client.put(+ "/decision-settings",+ json={"backend": "haiku_anthropic", "threshold": 0.5},+ )+ cleared = await client.put(+ "/decision-settings",+ json={"backend": "haiku_anthropic", "clear_api_key": True},+ )++ assert missing.status_code == 422+ assert not_carried.status_code == 422+ assert kept.status_code == 200+ assert kept.json()["workspace"]["api_key_last4"] == "1234"+ assert cleared.status_code == 422+++ "body",+ [+ {"backend": "none", "threshold": 1.5},+ {"backend": "none", "threshold": -0.1},+ {"backend": "endpoint", "endpoint": "ftp://10.9.0.5/decide"},+ {"backend": "endpoint"},+ {"backend": "endpoint", "endpoint": "http://10.9.0.5", "api_key": " "},+ {"backend": "default"},+ {"backend": "somewhere"},+ ],+)+async def test_bad_values_are_refused_and_the_setting_stays(+ session_factory: async_sessionmaker[AsyncSession],+ service: _Service,+ body: dict[str, Any],+) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ async with _as(_app(session_factory, service), admin) as client:+ await client.put("/decision-settings", json={"backend": "none"})+ refused = await client.put("/decision-settings", json=body)++ assert refused.status_code == 422+ row = await _row(session_factory, TENANT_ZERO_ID)+ assert row is not None and row.backend == "none" and row.threshold is None+++async def test_bedrock_is_refused_unless_the_server_allows_it(+ session_factory: async_sessionmaker[AsyncSession], service: _Service+) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ async with _as(_app(session_factory, service), admin) as client:+ refused = await client.put(+ "/decision-settings", json={"backend": "haiku_bedrock"}+ )+ async with _as(_app(session_factory, service, bedrock=True), admin) as client:+ allowed = await client.put(+ "/decision-settings",+ json={"backend": "haiku_bedrock", "api_key": "not-stored"},+ )++ assert refused.status_code == 422+ assert "DECISION_BEDROCK_ENABLED" in refused.json()["detail"]+ assert allowed.status_code == 200+ assert allowed.json()["options"]["haiku_bedrock"] is True+ assert allowed.json()["workspace"]["has_api_key"] is False+++async def test_a_key_the_server_cannot_decrypt_still_loads(+ session_factory: async_sessionmaker[AsyncSession], service: _Service+) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ lost = Keyring.parse("gone:" + "g" * 40, legacy_secret=None)+ async with session_factory() as session:+ await DecisionSettingsStore().save(+ session,+ TENANT_ZERO_ID,+ backend="endpoint",+ endpoint="http://10.9.0.5/decide",+ api_key_encrypted=lost.encrypt("lost-token"),+ threshold=None,+ )+ await session.commit()++ async with _as(_app(session_factory, service), admin) as client:+ loaded = await client.get("/decision-settings")+ replaced = await client.put(+ "/decision-settings",+ json={+ "backend": "endpoint",+ "endpoint": "http://10.9.0.5/decide",+ "api_key": "new-token",+ },+ )++ assert loaded.status_code == 200+ workspace = loaded.json()["workspace"]+ assert workspace["has_api_key"] is True+ assert workspace["api_key_unreadable"] is True+ assert workspace["api_key_last4"] is None+ assert replaced.json()["workspace"]["api_key_last4"] == "oken"+++async def test_delete_returns_the_workspace_to_the_server_default(+ session_factory: async_sessionmaker[AsyncSession], service: _Service+) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ app = _app(session_factory, service)+ async with _as(app, admin) as client:+ await client.put("/decision-settings", json={"backend": "none"})+ async with session_factory() as session:+ assert not await app.state.client.available(session, TENANT_ZERO_ID)+ deleted = await client.delete("/decision-settings")+ body = (await client.get("/decision-settings")).json()++ assert deleted.status_code == 204+ assert body["source"] == "server_default" and body["workspace"] is None+ assert await _row(session_factory, TENANT_ZERO_ID) is None+ # The save dropped the process's cached setting, so it applies at once.+ async with session_factory() as session:+ assert await app.state.client.available(session, TENANT_ZERO_ID)+++async def test_another_workspaces_admin_neither_reads_nor_changes_it(+ session_factory: async_sessionmaker[AsyncSession], service: _Service+) -> None:+ admin_a = await _person(session_factory, "ada", role="admin")+ admin_b = await _person(session_factory, "bea", tenant_id=TENANT_B, role="admin")+ app = _app(session_factory, service)+ async with _as(app, admin_a) as client:+ await client.put(+ "/decision-settings",+ json={"backend": "haiku_anthropic", "api_key": "fake-key-AAAA"},+ )++ async with _as(app, admin_b, TENANT_B) as client:+ own = (await client.get("/decision-settings")).json()+ await client.put("/decision-settings", json={"backend": "none"})+ await client.delete("/decision-settings")+ async with _as(app, admin_b, TENANT_ZERO_ID) as client:+ forged = [+ await client.get("/decision-settings"),+ await client.put("/decision-settings", json={"backend": "none"}),+ await client.delete("/decision-settings"),+ ]++ assert own["workspace"] is None and own["source"] == "server_default"+ assert "AAAA" not in json.dumps(own)+ assert [r.status_code for r in forged] == [403, 403, 403]+ assert all("AAAA" not in r.text for r in forged)+ row = await _row(session_factory, TENANT_ZERO_ID)+ assert row is not None and row.backend == "haiku_anthropic"+ assert await _row(session_factory, TENANT_B) is None+++class TestTheTestButton:+ async def test_unsaved_values_are_tried_and_nothing_is_saved(+ self, session_factory: async_sessionmaker[AsyncSession], service: _Service+ ) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ async with _as(_app(session_factory, service), admin) as client:+ tried = await client.post(+ "/decision-settings/test",+ json={+ "backend": "endpoint",+ "endpoint": "http://10.9.0.5/decide",+ "api_key": "own-token",+ },+ )++ assert tried.status_code == 200+ body = tried.json()+ assert body["ok"] is True+ assert body["agent"] == "deploy-bot"+ assert body["confidence"] == 0.88+ assert isinstance(body["latency_ms"], int)+ assert body["error"] is None+ [(url, headers, _)] = service.calls+ assert url == "http://10.9.0.5/decide"+ assert headers["authorization"] == "Bearer own-token"+ assert await _row(session_factory, TENANT_ZERO_ID) is None++ async def test_an_omitted_key_is_the_stored_one(+ self, session_factory: async_sessionmaker[AsyncSession], service: _Service+ ) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ async with _as(_app(session_factory, service), admin) as client:+ await client.put(+ "/decision-settings",+ json={"backend": "haiku_anthropic", "api_key": "fake-key-SAVD"},+ )+ await client.post(+ "/decision-settings/test", json={"backend": "haiku_anthropic"}+ )++ [(url, headers, _)] = service.calls+ assert url == SERVER_URL+ assert headers["x-decision-backend"] == "anthropic"+ assert headers["x-decision-anthropic-key"] == "fake-key-SAVD"++ async def test_default_uses_the_servers_key_never_the_workspaces(+ self, session_factory: async_sessionmaker[AsyncSession], service: _Service+ ) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ async with _as(_app(session_factory, service), admin) as client:+ await client.put(+ "/decision-settings",+ json={+ "backend": "endpoint",+ "endpoint": "http://10.9.0.5/decide",+ "api_key": "workspace-token",+ },+ )+ tried = await client.post(+ "/decision-settings/test",+ json={"backend": "default", "api_key": "workspace-token"},+ )++ assert tried.json()["ok"] is True+ [(url, headers, _)] = service.calls+ assert url == SERVER_URL+ assert headers["authorization"] == "Bearer server-key"+ assert "workspace-token" not in json.dumps(headers)++ async def test_a_refused_key_is_classified(+ self, session_factory: async_sessionmaker[AsyncSession], service: _Service+ ) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ service.status = 401+ async with _as(_app(session_factory, service), admin) as client:+ tried = await client.post(+ "/decision-settings/test",+ json={"backend": "endpoint", "endpoint": "http://10.9.0.5/decide"},+ )++ body = tried.json()+ assert body["ok"] is False+ assert body["error"]["code"] == "unauthorized"+ assert body["error"]["message"]++ async def test_nothing_to_test_is_not_configured(+ self, session_factory: async_sessionmaker[AsyncSession], service: _Service+ ) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ async with _as(+ _app(session_factory, service, server_url=None), admin+ ) as client:+ default = await client.post(+ "/decision-settings/test", json={"backend": "default"}+ )+ none = await client.post(+ "/decision-settings/test", json={"backend": "none"}+ )++ assert default.json()["error"]["code"] == "not_configured"+ assert none.json()["error"]["code"] == "not_configured"+ assert service.calls == []+++class TestTheOutboundPolicy:+ @pytest.mark.parametrize("allowed", ["", "169.254.0.0/16"])+ async def test_a_metadata_address_is_always_refused(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ service: _Service,+ allowed: str,+ ) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ async with _as(+ _app(session_factory, service, allowed=allowed), admin+ ) as client:+ refused = await client.put(+ "/decision-settings",+ json={"backend": "endpoint", "endpoint": "http://169.254.169.254/"},+ )++ assert refused.status_code == 422+ assert "OUTBOUND_ALLOWED_PRIVATE_HOSTS" in refused.json()["detail"]+ assert await _row(session_factory, TENANT_ZERO_ID) is None++ async def test_a_private_address_needs_the_operators_listing(+ self, session_factory: async_sessionmaker[AsyncSession], service: _Service+ ) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ body = {"backend": "endpoint", "endpoint": "http://10.0.0.5:8230"}+ async with _as(_app(session_factory, service, allowed=""), admin) as client:+ refused = await client.put("/decision-settings", json=body)+ async with _as(+ _app(session_factory, service, allowed="10.0.0.5"), admin+ ) as client:+ allowed = await client.put("/decision-settings", json=body)++ assert refused.status_code == 422+ assert "OUTBOUND_ALLOWED_PRIVATE_HOSTS" in refused.json()["detail"]+ assert allowed.status_code == 200+ assert allowed.json()["workspace"]["endpoint"] == "http://10.0.0.5:8230"++ async def test_testing_a_refused_address_is_refused_and_not_sent(+ self, session_factory: async_sessionmaker[AsyncSession], service: _Service+ ) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ async with _as(_app(session_factory, service, allowed=""), admin) as client:+ tried = await client.post(+ "/decision-settings/test",+ json={"backend": "endpoint", "endpoint": "http://10.0.0.5:8230"},+ )++ body = tried.json()+ assert body["ok"] is False+ assert body["error"]["code"] == "refused"+ assert "OUTBOUND_ALLOWED_PRIVATE_HOSTS" in body["error"]["message"]+ assert service.calls == []++ async def test_the_server_default_at_a_private_address_is_not_checked(+ self, session_factory: async_sessionmaker[AsyncSession], service: _Service+ ) -> None:+ admin = await _person(session_factory, "ada", role="admin")+ app = _app(+ session_factory,+ service,+ server_url="http://10.1.2.3:8230",+ allowed="",+ )+ async with _as(app, admin) as client:+ tried = await client.post(+ "/decision-settings/test", json={"backend": "default"}+ )++ assert tried.json()["ok"] is True+ [(url, _, _)] = service.calls+ assert url == "http://10.1.2.3:8230/decide"diff --git a/core/tests/switch_core/gateway/test_held_connections.py b/core/tests/switch_core/gateway/test_held_connections.pyindex 0975451b0..7bd91e1db 100644--- a/core/tests/switch_core/gateway/test_held_connections.py+++ b/core/tests/switch_core/gateway/test_held_connections.py@@ -258,6 +258,10 @@ def _config() -> SimpleNamespace:gateway_signup_open=True,gateway_signup_max_per_hour=100,gateway_admin_email="[email protected]",+ decision_routing_enabled=False,+ decision_endpoint_url=None,+ decision_api_key=None,+ decision_bedrock_enabled=False,)diff --git a/core/tests/switch_core/gateway/test_room_access_permission_authz.py b/core/tests/switch_core/gateway/test_room_access_permission_authz.pyindex 745d3331c..7ed744c8f 100644--- a/core/tests/switch_core/gateway/test_room_access_permission_authz.py+++ b/core/tests/switch_core/gateway/test_room_access_permission_authz.py@@ -87,6 +87,7 @@ async def _patch(None, # type: ignore[arg-type] # bridge_store — unused before the gateNone, # type: ignore[arg-type] # external_user_storeNone, # type: ignore[arg-type] # protocol+ None, # type: ignore[arg-type] # decisionsuser,await _is_admin(session, user),)diff --git a/core/tests/switch_core/gateway/test_room_auto_route.py b/core/tests/switch_core/gateway/test_room_auto_route.pynew file mode 100644index 000000000..a4545ac04--- /dev/null+++ b/core/tests/switch_core/gateway/test_room_auto_route.py@@ -0,0 +1,362 @@+"""A room's auto-route in its detail and its toggle (`PUT /rooms/{id}/auto-route`),+through the real routes on Postgres.++The detail says whether auto-route is on, whether it can act (the server's+flag, and the workspace's decision backend) and why not, and the room's+triage agent; the toggle writes the same settings `!auto-route` does.+"""++from __future__ import annotations++import uuid+from collections.abc import AsyncIterator+from types import SimpleNamespace+from typing import Any++import httpx+import pytest+from fastapi import FastAPI+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.db.models import (+ TENANT_ZERO_ID,+ Agent,+ ApiKey,+ Client,+ Room,+ TenantMember,+ User,+ room_agents,+)+from switch_core.db.stores.agent_store import AgentStore+from switch_core.db.stores.collaboration_bridge_store import CollaborationBridgeStore+from switch_core.db.stores.decision_settings_store import DecisionSettingsStore+from switch_core.db.stores.external_user_store import ExternalUserStore+from switch_core.db.stores.room_role_store import RoomRoleStore+from switch_core.db.stores.room_store import (+ AUTO_ROUTE_FALLBACK_KEY,+ AUTO_ROUTE_ON_KEY,+ RoomStore,+)+from switch_core.db.stores.user_store import UserStore+from switch_core.decision.settings import (+ FLAG_OFF_REASON,+ NO_BACKEND_REASON,+ DecisionAvailability,+ ServerDefault,+)+from switch_core.gateway import dependencies as gw_deps+from switch_core.gateway.auth import create_jwt+from switch_core.gateway.rooms import get_decision_availability+from switch_core.gateway.rooms import router as rooms_router+from switch_core.keys import Keyring++KEYRING = Keyring.parse("test:" + "r" * 40, legacy_secret=None)+++class _Protocol:+ def __init__(self) -> None:+ self.room_role_store = RoomRoleStore()+ self.agent_store = AgentStore()+ self.connections = SimpleNamespace(live_connection_ids=lambda: set())++ async def get_agent_statuses_by_ids_in_session(+ self, session: AsyncSession, room_id: str, agent_ids: list[str]+ ) -> dict[str, Any]:+ return {}+++def _app(+ session_factory: async_sessionmaker[AsyncSession],+ *,+ enabled: bool = True,+ server_url: str | None = "http://decision.test/decide",+) -> FastAPI:+ async def _session() -> AsyncIterator[AsyncSession]:+ async with session_factory() as session:+ yield session++ app = FastAPI()+ app.include_router(rooms_router, prefix="/rooms")+ protocol = _Protocol()+ app.dependency_overrides.update(+ {+ gw_deps.get_session: _session,+ gw_deps.get_session_factory: lambda: session_factory,+ gw_deps.get_user_store: UserStore,+ gw_deps.get_room_store: RoomStore,+ gw_deps.get_bridge_store: CollaborationBridgeStore,+ gw_deps.get_external_user_store: ExternalUserStore,+ gw_deps.get_protocol: lambda: protocol,+ gw_deps.get_config: lambda: SimpleNamespace(+ keyring=KEYRING, gateway_tenant_choice_enabled=False+ ),+ get_decision_availability: lambda: DecisionAvailability(+ enabled=enabled,+ server=ServerDefault(+ endpoint_url=server_url, api_key=None, bedrock_enabled=False+ ),+ store=DecisionSettingsStore(),+ ),+ }+ )+ return app+++async def _user(+ session_factory: async_sessionmaker[AsyncSession], name: str, role: str+) -> User:+ async with session_factory() as session:+ user = User(name=name, email=f"{name}@example.invalid", role="user")+ session.add(user)+ await session.flush()+ session.add(TenantMember(tenant_id=TENANT_ZERO_ID, user_id=user.id, role=role))+ await session.commit()+ return user+++async def _agent(session: AsyncSession, owner: User, name: str) -> Agent:+ client = Client(+ transport_user_id=f"@{name}-{uuid.uuid4().hex[:6]}:test",+ display_name=name,+ type="agent",+ )+ session.add(client)+ key = ApiKey(+ user_id=owner.id,+ key_hash=f"hash-{uuid.uuid4().hex}",+ encrypted_key="enc",+ label=name,+ type="agent",+ )+ session.add(key)+ await session.flush()+ agent = Agent(+ name=name,+ description="d",+ agent_type="session_addressable",+ connector_type="claude_code",+ integration_profile={"connection_model": "session_addressable"},+ client_id=client.id,+ api_key_id=key.id,+ owner_id=owner.id,+ )+ session.add(agent)+ await session.flush()+ return agent+++async def _room(+ session_factory: async_sessionmaker[AsyncSession], owner: User+) -> tuple[Room, Agent, Agent]:+ async with session_factory() as session:+ room = Room(+ transport_room_id=f"!r-{uuid.uuid4().hex[:8]}:test",+ name="ops",+ description="d",+ owner_id=owner.id,+ read_visibility="public",+ write_visibility="public",+ )+ session.add(room)+ await session.flush()+ triage = await _agent(session, owner, "triage")+ other = await _agent(session, owner, "other")+ for agent in (triage, other):+ await session.execute(+ room_agents.insert().values(room_id=room.id, agent_id=agent.id)+ )+ await session.commit()+ return room, triage, other+++def _as(app: FastAPI, user: User) -> httpx.AsyncClient:+ return httpx.AsyncClient(+ transport=httpx.ASGITransport(app=app),+ base_url="http://test",+ cookies={+ "switch_auth": create_jwt(+ user.id, user.email, user.role, KEYRING, TENANT_ZERO_ID+ )+ },+ )+++async def _metadata(+ session_factory: async_sessionmaker[AsyncSession], room_id: str+) -> dict[str, Any]:+ async with session_factory() as session:+ room = await session.get(Room, room_id)+ assert room is not None+ return dict(room.metadata_ or {})+++class TestTheRoomDetail:+ async def test_available_with_a_server_default(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ owner = await _user(session_factory, "owner", "member")+ room, _, _ = await _room(session_factory, owner)+ async with _as(_app(session_factory), owner) as client:+ detail = (await client.get(f"/rooms/{room.id}")).json()++ assert detail["auto_route"] == {+ "on": False,+ "available": True,+ "reason": None,+ "fallback_agent": None,+ "fallback_agent_name": None,+ }++ async def test_unavailable_with_the_flag_off(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ owner = await _user(session_factory, "owner", "member")+ room, _, _ = await _room(session_factory, owner)+ async with _as(_app(session_factory, enabled=False), owner) as client:+ detail = (await client.get(f"/rooms/{room.id}")).json()++ assert detail["auto_route"]["available"] is False+ assert detail["auto_route"]["reason"] == FLAG_OFF_REASON++ async def test_unavailable_with_no_backend(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ owner = await _user(session_factory, "owner", "member")+ room, _, _ = await _room(session_factory, owner)+ async with _as(_app(session_factory, server_url=None), owner) as client:+ detail = (await client.get(f"/rooms/{room.id}")).json()++ assert detail["auto_route"]["available"] is False+ assert detail["auto_route"]["reason"] == NO_BACKEND_REASON+ assert "Switch Console → Admin → Decision routing" in NO_BACKEND_REASON++ async def test_unavailable_when_the_workspace_chose_none(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ owner = await _user(session_factory, "owner", "member")+ room, _, _ = await _room(session_factory, owner)+ async with session_factory() as session:+ await DecisionSettingsStore().save(+ session,+ TENANT_ZERO_ID,+ backend="none",+ endpoint=None,+ api_key_encrypted=None,+ threshold=None,+ )+ await session.commit()+ async with _as(_app(session_factory), owner) as client:+ detail = (await client.get(f"/rooms/{room.id}")).json()++ assert detail["auto_route"]["available"] is False+ assert "turned auto-route off" in detail["auto_route"]["reason"]+++class TestTheToggle:+ async def test_the_owner_turns_it_on_and_names_a_triage_agent(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ owner = await _user(session_factory, "owner", "member")+ room, triage, _ = await _room(session_factory, owner)+ async with _as(_app(session_factory), owner) as client:+ put = await client.put(+ f"/rooms/{room.id}/auto-route",+ json={"on": True, "fallback_agent": triage.id},+ )+ detail = (await client.get(f"/rooms/{room.id}")).json()++ assert put.status_code == 200, put.text+ expected = {+ "on": True,+ "available": True,+ "reason": None,+ "fallback_agent": triage.id,+ "fallback_agent_name": "triage",+ }+ assert put.json() == expected+ assert detail["auto_route"] == expected+ metadata = await _metadata(session_factory, room.id)+ assert metadata[AUTO_ROUTE_ON_KEY] is True+ assert metadata[AUTO_ROUTE_FALLBACK_KEY] == triage.id++ async def test_leaving_a_field_out_keeps_it_and_null_clears_the_agent(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ owner = await _user(session_factory, "owner", "member")+ room, triage, _ = await _room(session_factory, owner)+ async with _as(_app(session_factory), owner) as client:+ await client.put(+ f"/rooms/{room.id}/auto-route",+ json={"on": True, "fallback_agent": triage.id},+ )+ off = await client.put(f"/rooms/{room.id}/auto-route", json={"on": False})+ cleared = await client.put(+ f"/rooms/{room.id}/auto-route", json={"fallback_agent": None}+ )++ assert off.json()["on"] is False+ assert off.json()["fallback_agent"] == triage.id+ assert cleared.json()["fallback_agent"] is None+ assert cleared.json()["on"] is False++ async def test_on_is_refused_where_it_cannot_act(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ owner = await _user(session_factory, "owner", "member")+ room, _, _ = await _room(session_factory, owner)+ async with _as(_app(session_factory, server_url=None), owner) as client:+ refused = await client.put(+ f"/rooms/{room.id}/auto-route", json={"on": True}+ )++ assert refused.status_code == 409+ assert refused.json()["detail"] == NO_BACKEND_REASON+ assert AUTO_ROUTE_ON_KEY not in await _metadata(session_factory, room.id)++ async def test_the_assigned_agent_or_a_stranger_cannot_be_triage(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ owner = await _user(session_factory, "owner", "member")+ room, triage, _ = await _room(session_factory, owner)+ async with session_factory() as session:+ await RoomStore().set_main_agent(session, room.id, triage.id)+ await session.commit()+ async with _as(_app(session_factory), owner) as client:+ assigned = await client.put(+ f"/rooms/{room.id}/auto-route", json={"fallback_agent": triage.id}+ )+ stranger = await client.put(+ f"/rooms/{room.id}/auto-route", json={"fallback_agent": "nobody-here"}+ )++ assert assigned.status_code == 422+ assert "assigned agent" in assigned.json()["detail"]+ assert stranger.status_code == 422++ @pytest.mark.parametrize("role", ["member"])+ async def test_someone_who_does_not_own_the_room_is_refused(+ self, session_factory: async_sessionmaker[AsyncSession], role: str+ ) -> None:+ owner = await _user(session_factory, "owner", "member")+ other = await _user(session_factory, "other", role)+ room, _, _ = await _room(session_factory, owner)+ async with _as(_app(session_factory), other) as client:+ refused = await client.put(+ f"/rooms/{room.id}/auto-route", json={"on": True}+ )++ assert refused.status_code == 403+ assert AUTO_ROUTE_ON_KEY not in await _metadata(session_factory, room.id)++ async def test_a_workspace_admin_may(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ owner = await _user(session_factory, "owner", "member")+ admin = await _user(session_factory, "admin", "admin")+ room, _, _ = await _room(session_factory, owner)+ async with _as(_app(session_factory), admin) as client:+ put = await client.put(f"/rooms/{room.id}/auto-route", json={"on": True})++ assert put.status_code == 200+ assert put.json()["on"] is Truediff --git a/core/tests/switch_core/gateway/test_room_create_platform_error.py b/core/tests/switch_core/gateway/test_room_create_platform_error.pyindex 4a1374506..f541179cd 100644--- a/core/tests/switch_core/gateway/test_room_create_platform_error.py+++ b/core/tests/switch_core/gateway/test_room_create_platform_error.py@@ -60,6 +60,7 @@ async def _call(exc: Exception) -> HTTPException:bridge_store=None, # type: ignore[arg-type]external_user_store=None, # type: ignore[arg-type]protocol=None, # type: ignore[arg-type]+ decisions=None, # type: ignore[arg-type]user=type("U", (), {"id": "u1"})(), # type: ignore[arg-type]is_admin=False,)@@ -98,6 +99,7 @@ async def test_an_ordinary_bug_is_not_dressed_up_as_a_platform_refusal() -> Nonebridge_store=None, # type: ignore[arg-type]external_user_store=None, # type: ignore[arg-type]protocol=None, # type: ignore[arg-type]+ decisions=None, # type: ignore[arg-type]user=type("U", (), {"id": "u1"})(), # type: ignore[arg-type]is_admin=False,)diff --git a/core/tests/switch_core/gateway/test_room_participant_read.py b/core/tests/switch_core/gateway/test_room_participant_read.pyindex 0d3a14cce..6d0d221bb 100644--- a/core/tests/switch_core/gateway/test_room_participant_read.py+++ b/core/tests/switch_core/gateway/test_room_participant_read.py@@ -65,6 +65,7 @@ async def _read(session: AsyncSession, room_id: str, user: User) -> object:None,None,None,+ None,user,False, # type: ignore[arg-type])@@ -80,6 +81,7 @@ async def _write(session: AsyncSession, room_id: str, user: User) -> None:None, # type: ignore[arg-type]None, # type: ignore[arg-type]None, # type: ignore[arg-type]+ None, # type: ignore[arg-type]user,False,)diff --git a/core/tests/switch_core/gateway/test_room_sync_name.py b/core/tests/switch_core/gateway/test_room_sync_name.pyindex 84062b866..439529653 100644--- a/core/tests/switch_core/gateway/test_room_sync_name.py+++ b/core/tests/switch_core/gateway/test_room_sync_name.py@@ -27,6 +27,7 @@ async def _sync(chats: _Harness, room_id: str, user: User) -> str:CollaborationBridgeStore(),None, # type: ignore[arg-type]None, # type: ignore[arg-type]+ None, # type: ignore[arg-type]user,False,)diff --git a/core/tests/switch_core/test_config_decision_routing.py b/core/tests/switch_core/test_config_decision_routing.pyindex 6824e85ce..220040b53 100644--- a/core/tests/switch_core/test_config_decision_routing.py+++ b/core/tests/switch_core/test_config_decision_routing.py@@ -1,8 +1,11 @@-"""Decision routing's settings: the `decision_routing` flag, the decision-service's endpoint, key and timeout."""+"""Decision routing's settings: the `decision_routing` flag, the server+default decision service's endpoint, key and timeout, whether workspaces may+choose Bedrock, and how long a decision may wait."""from __future__ import annotations+from typing import Any+import pytestfrom pydantic import ValidationError@@ -25,6 +28,9 @@ _BASE_KWARGS = dict(_BOTH = f"{FOLLOW_THE_CONVERSATION},{DECISION_ROUTING}"+# The client reads no settings until it decides, which these tests never do.+_NO_DB: Any = None+@pytest.fixture(autouse=True)def _no_ambient_settings(monkeypatch: pytest.MonkeyPatch) -> None:@@ -33,6 +39,8 @@ def _no_ambient_settings(monkeypatch: pytest.MonkeyPatch) -> None:"DECISION_ENDPOINT_URL","DECISION_API_KEY","DECISION_TIMEOUT_SECONDS",+ "DECISION_BEDROCK_ENABLED",+ "DECISION_PENDING_MAX_AGE_SECONDS",):monkeypatch.delenv(name, raising=False)@@ -48,7 +56,11 @@ def test_off_by_default_with_nothing_configured() -> None:assert config.decision_endpoint_url is Noneassert config.decision_api_key is Noneassert config.decision_timeout_seconds == 5.0- assert build_decision_client(config) is None+ assert config.decision_bedrock_enabled is False+ assert config.decision_pending_max_age_seconds == 120+ client = build_decision_client(config, _NO_DB, None)+ assert client.availability.enabled is False+ assert client.server.configured is Falsedef test_the_flag_needs_follow_the_conversation() -> None:@@ -67,9 +79,31 @@ def test_on_with_follow_the_conversation() -> None:assert config.decision_routing_enabledassert config.decision_endpoint_url == "http://127.0.0.1:8100/decide"assert config.decision_api_key == "placeholder"- built = build_decision_client(config)- assert built is not None- assert built[0].endpoint_url == "http://127.0.0.1:8100/decide"+ client = build_decision_client(config, _NO_DB, None)+ assert client.availability.enabled+ assert client.server.endpoint_url == "http://127.0.0.1:8100/decide"+ assert client.server.api_key == "placeholder"+ assert client.timeout_seconds == 2.5+ assert client.server.haiku_anthropic and not client.server.haiku_bedrock+++def test_bedrock_and_the_wait_are_settings() -> None:+ config = _config(+ feature_flags_enabled=_BOTH,+ decision_endpoint_url="http://127.0.0.1:8100/decide",+ decision_bedrock_enabled=True,+ decision_pending_max_age_seconds=30,+ )++ client = build_decision_client(config, _NO_DB, None)+ assert client.server.haiku_bedrock+ assert config.decision_pending_max_age_seconds == 30++[email protected]("seconds", [0, 3601])+def test_the_wait_is_bounded(seconds: int) -> None:+ with pytest.raises(ValidationError, match="decision_pending_max_age_seconds"):+ _config(decision_pending_max_age_seconds=seconds)@pytest.mark.parametrize("timeout", [0, -1])@@ -93,12 +127,14 @@ def test_blank_settings_are_unset(monkeypatch: pytest.MonkeyPatch) -> None:assert config.decision_api_key is None-def test_the_flag_without_an_endpoint_routes_nothing_and_says_so(+def test_the_flag_without_an_endpoint_says_workspaces_must_choose(caplog: pytest.LogCaptureFixture,) -> None:config = _config(feature_flags_enabled=_BOTH)with caplog.at_level("WARNING"):- assert build_decision_client(config) is None+ client = build_decision_client(config, _NO_DB, None)+ assert client.server.configured is Falseassert "DECISION_ENDPOINT_URL is not set" in caplog.text+ assert "Admin → Decision routing" in caplog.textdiff --git a/core/tests/switch_core/test_migration_decision_routing.py b/core/tests/switch_core/test_migration_decision_routing.pynew file mode 100644index 000000000..476514192--- /dev/null+++ b/core/tests/switch_core/test_migration_decision_routing.py@@ -0,0 +1,126 @@+"""`689d7d8ff913` adds `decision_settings` and `pending_decisions`, each+tenant-scoped under row-level security, and its downgrade drops them."""++from __future__ import annotations++from pathlib import Path+from typing import Any++import pytest+from alembic.config import Config+from alembic.runtime.environment import EnvironmentContext+from alembic.script import ScriptDirectory+from sqlalchemy import Connection, text+from sqlalchemy.ext.asyncio import AsyncConnection, create_async_engine++_CORE = Path(__file__).resolve().parents[2]+_DB = "migration_decision_routing"++_PRE_MIGRATION_REVISION = "f68b679599ab"+_MIGRATION_UNDER_TEST = "689d7d8ff913"+_TABLES = ("decision_settings", "pending_decisions")+++def _script_directory(config: Config) -> ScriptDirectory:+ config.set_main_option("script_location", str(_CORE / "switch_core" / "migrations"))+ return ScriptDirectory.from_config(config)+++def _migrate(revision: str, *, downgrade: bool) -> Any:+ def migrate(connection: Connection) -> None:+ config = Config(str(_CORE / "alembic.ini"))+ script = _script_directory(config)++ def revisions(current_revision: str, context: Any) -> Any:+ if downgrade:+ return script._downgrade_revs(revision, current_revision)+ return script._upgrade_revs(revision, current_revision)++ with EnvironmentContext(config, script, fn=revisions) as environment:+ environment.configure(connection=connection)+ with environment.begin_transaction():+ environment.run_migrations()++ return migrate+++async def database_url(postgres_url: str) -> Any:+ admin = create_async_engine(postgres_url, isolation_level="AUTOCOMMIT")+ async with admin.connect() as connection:+ await connection.execute(text(f'DROP DATABASE IF EXISTS "{_DB}"'))+ await connection.execute(text(f'CREATE DATABASE "{_DB}"'))+ await admin.dispose()++ base, _, _ = postgres_url.rpartition("/")+ try:+ yield f"{base}/{_DB}"+ finally:+ admin = create_async_engine(postgres_url, isolation_level="AUTOCOMMIT")+ async with admin.connect() as connection:+ await connection.execute(text(f'DROP DATABASE IF EXISTS "{_DB}"'))+ await admin.dispose()+++async def _tables(connection: AsyncConnection) -> dict[str, bool]:+ """Each table of the two that exists, and whether row-level security is on."""+ rows = await connection.execute(+ text(+ "SELECT relname, relrowsecurity FROM pg_class "+ "WHERE relname = ANY(:names) AND relkind = 'r'"+ ),+ {"names": list(_TABLES)},+ )+ return {name: secured for name, secured in rows}+++async def _policies(connection: AsyncConnection) -> set[str]:+ rows = await connection.execute(+ text(+ "SELECT tablename FROM pg_policies "+ "WHERE policyname = 'tenant_isolation' AND tablename = ANY(:names)"+ ),+ {"names": list(_TABLES)},+ )+ return {row[0] for row in rows}+++async def test_upgrades_and_downgrades(database_url: str) -> None:+ engine = create_async_engine(database_url)+ try:+ async with engine.begin() as connection:+ await connection.run_sync(+ _migrate(_PRE_MIGRATION_REVISION, downgrade=False)+ )+ assert await _tables(connection) == {}++ async with engine.begin() as connection:+ await connection.run_sync(_migrate(_MIGRATION_UNDER_TEST, downgrade=False))+ assert await _tables(connection) == dict.fromkeys(_TABLES, True)+ assert await _policies(connection) == set(_TABLES)++ async with engine.begin() as connection:+ await connection.execute(+ text("INSERT INTO tenants (id, slug, name) VALUES ('t', 't', 't')")+ )+ await connection.execute(+ text(+ "INSERT INTO decision_settings (tenant_id, backend, threshold) "+ "VALUES ('t', 'none', 0.5)"+ )+ )+ with pytest.raises(Exception, match="ck_decision_settings_backend"):+ async with connection.begin_nested():+ await connection.execute(+ text(+ "INSERT INTO decision_settings (tenant_id, backend) "+ "VALUES ('t', 'elsewhere')"+ )+ )++ async with engine.begin() as connection:+ await connection.run_sync(_migrate(_PRE_MIGRATION_REVISION, downgrade=True))+ assert await _tables(connection) == {}+ assert await _policies(connection) == set()+ finally:+ await engine.dispose()diff --git a/docs/TELEMETRY.md b/docs/TELEMETRY.mdindex 8cbd9cc83..9e32f7dd9 100644--- a/docs/TELEMETRY.md+++ b/docs/TELEMETRY.md@@ -281,6 +281,17 @@ out-of-step setting, with the restart offered. "No" is written downexplicitly rather than left out, because a stack started under a "yes" wouldotherwise keep the setting it started with.+What that server reports is its own catalogue (`docs/old/telemetry-events.md`),+held to the same rule: counts, durations and closed values, never a room,+agent, person or message. Auto-route is an example: each decision the server+asks a decision service for reports only which kind of backend answered+(`server_default`, `endpoint`, `haiku_anthropic` or `haiku_bedrock`), whether+it picked an agent or nobody, its confidence in tenths and its latency+(`decision_made`), or why it failed (`decision_failed`: `timeout`,+`unreachable`, `error_status`, `bad_answer`, `key_unreadable`,+`endpoint_refused`). Which agent was+picked, in which room, for which message, is never sent.+Three limits are stated in the app rather than left to be discovered.- **The server reads the setting when it starts.** Changing the answer while itdiff --git a/docs/old/bridges/SLACK_DISTRIBUTED_APP.md b/docs/old/bridges/SLACK_DISTRIBUTED_APP.mdindex ce3f567ab..eab02c81f 100644--- a/docs/old/bridges/SLACK_DISTRIBUTED_APP.md+++ b/docs/old/bridges/SLACK_DISTRIBUTED_APP.md@@ -132,7 +132,7 @@ the same job once installed — the differences are all in `settings` and{ "command": "/agents-greet", "url": "https://HOST/messaging/slack/commands", "description": "Have agents in the room introduce themselves", "should_escape": false },{ "command": "/room-url", "url": "https://HOST/messaging/slack/commands", "description": "Show the frontend URL for this room", "should_escape": false },{ "command": "/follow-ups", "url": "https://HOST/messaging/slack/commands", "description": "Turn follow-ups without an @ on or off for this room", "usage_hint": "on | off", "should_escape": false },- { "command": "/auto-route", "url": "https://HOST/messaging/slack/commands", "description": "Let Switch pick the agent to answer messages that tag nobody", "usage_hint": "on | off", "should_escape": false },+ { "command": "/auto-route", "url": "https://HOST/messaging/slack/commands", "description": "Let Switch pick the agent to answer messages that tag nobody", "usage_hint": "on | off | fallback @agent", "should_escape": true },{ "command": "/assign-agent", "url": "https://HOST/messaging/slack/commands", "description": "Send every message in this room to one agent", "usage_hint": "@agent-name", "should_escape": true },{ "command": "/unassign-agent", "url": "https://HOST/messaging/slack/commands", "description": "Stop sending every message to the assigned agent", "should_escape": false }]diff --git a/docs/old/bridges/SLACK_SETUP.md b/docs/old/bridges/SLACK_SETUP.mdindex 8dba97612..9add1dff7 100644--- a/docs/old/bridges/SLACK_SETUP.md+++ b/docs/old/bridges/SLACK_SETUP.md@@ -81,7 +81,7 @@ You need one Slack app for the whole bridge. There are two ways to create it —{ "command": "/agents-greet", "description": "Have agents in the room introduce themselves", "should_escape": false },{ "command": "/room-url", "description": "Show the frontend URL for this room", "should_escape": false },{ "command": "/follow-ups", "description": "Turn follow-ups without an @ on or off for this room", "usage_hint": "on | off", "should_escape": false },- { "command": "/auto-route", "description": "Let Switch pick the agent to answer messages that tag nobody", "usage_hint": "on | off", "should_escape": false },+ { "command": "/auto-route", "description": "Let Switch pick the agent to answer messages that tag nobody", "usage_hint": "on | off | fallback @agent", "should_escape": true },{ "command": "/assign-agent", "description": "Send every message in this room to one agent", "usage_hint": "@agent-name", "should_escape": true },{ "command": "/unassign-agent", "description": "Stop sending every message to the assigned agent", "should_escape": false }]diff --git a/docs/old/decision-routing.md b/docs/old/decision-routing.mdnew file mode 100644index 000000000..b2d198256--- /dev/null+++ b/docs/old/decision-routing.md@@ -0,0 +1,122 @@+# Decision routing (auto-route): setting it up and what rooms do++Auto-route lets Switch pick the agent that answers a message that tags nobody.+In a room with `!auto-route on`, a person's untagged message that no follow-up+or assigned agent took is sent, with the room's recent context, to a decision+service, which names one of the room's agents or nobody. The service itself+(`decision/`, image `switch-decision`) is documented in `decision/README.md`;+this page covers how a server and its workspaces choose one, and what a room+does when there is none or it fails.++## Turning it on for a server++Auto-route needs two feature flags, `follow_the_conversation` and+`decision_routing`, in `FEATURE_FLAGS_ENABLED`. With the flag off nothing is+ever sent to a decision service, `!auto-route` says the feature is off, and+the room toggle shows it unavailable.++## The server default++The server's own decision service is the default for every workspace that+has not chosen one:++| Setting | Meaning |+| --- | --- |+| `DECISION_ENDPOINT_URL` | The decision service. Its base URL (`http://host:8230`) or its `/decide` URL both work. The bundled Helm chart (`decision.enabled`) and the compose `decision` profile set it. |+| `DECISION_API_KEY` | Sent as the bearer token, when the service requires one. |+| `DECISION_TIMEOUT_SECONDS` | How long one decision may take (default 5), for every workspace's backend. |+| `DECISION_BEDROCK_ENABLED` | Whether a workspace may choose Haiku on Amazon Bedrock (default off). Bedrock runs in the server's decision service on the server's own AWS credentials, which is why the operator has to allow it. |+| `DECISION_PENDING_MAX_AGE_SECONDS` | How long a message may wait for a decision, across a restart or an outage, before it is dropped rather than answered late (default 120). |++With no `DECISION_ENDPOINT_URL`, there is no server default: auto-route is+unavailable in every workspace until its admins choose a backend, and Haiku is+not offered (it runs in the server's decision service).++## Each workspace's choice++A workspace's admins (its owners and admins, and the server operator) choose+in Switch Console → Admin → Decision routing. Switch stores one row per+workspace (`decision_settings`); no row means the server default.++- **Server default**: whatever the server is configured with. "Use server+ default" deletes the workspace's row.+- **None**: auto-route is off in the workspace's rooms, even with a server+ default. Rooms behave as they do without it.+- **Self-hosted endpoint**: the workspace's own decision service, a URL and an+ optional key sent as its bearer token. That workspace's room context goes+ there, by its admin's choice; no other workspace is affected.+- **Haiku (Anthropic API)**: the workspace's Anthropic key, sent per request+ to the server's decision service, which calls Anthropic with it.+- **Haiku on Bedrock**: offered only where the operator set+ `DECISION_BEDROCK_ENABLED`; no key is stored.+- **Confidence threshold**: between 0 and 1, sent to the service as the+ confidence it needs to name an agent. Empty leaves it to the service.++A saved key is encrypted under the server's keys (`SECRET_KEYS`, re-encrypted+on rotation like every other stored secret) and only its last four characters+are ever shown. A key the server can no longer decrypt still loads, shown as+set but unreadable, so an admin can save a replacement; until then that+workspace's decisions fail (see below).++A save applies to the workspace's next decision with no restart: the process+that saved it at once, the others within a few seconds.++**Test** runs one fixed sample decision (three made-up agents and a deploy+question, nothing from any room) against the values entered, saved or not, and+shows the pick, the confidence and the time it took, or why it failed: not+reachable, timed out, key refused, an answer that is not a decision, a backend+the service cannot run, or nothing to test.++The same settings are served by the gateway API, for that workspace's admins+only: `GET`, `PUT` and `DELETE /gateway/decision-settings`, and+`POST /gateway/decision-settings/test`.++## In a room++`!auto-route on` and the auto-route toggle on the room page (Console and the+gateway, `PUT /gateway/rooms/{id}/auto-route`, for the room's owner and+workspace admins) set the same switch. Turning it on is refused, with the+reason and who can fix it, where it cannot act: the flag is off, or the+workspace resolves to no backend. A room's detail reports+`auto_route: {on, available, reason, fallback_agent, fallback_agent_name}`.++### When there is no backend++Nothing changes from a room without auto-route: tags, aliases, roles, direct+rooms, follow-ups (in threads too) and the assigned agent all work, and no+message content is sent anywhere.++### When the backend fails++A timeout, a refused connection, an answer other than 200, or an answer that+is not a decision (including one naming an agent that was not a candidate) is+a failure. A decision of nobody is not.++- The message goes to nobody, and the server log names the failure.+- Switch posts "Auto-route is unavailable right now; tag an agent to reach+ it." in the room, at most once an hour per room. The time is kept in the+ room (`decision_notice_at`), so restarts and several processes don't repeat+ it.+- **Triage agent**: a room can name one of its agents with+ `!auto-route fallback @agent` (or from the toggle). While the backend fails,+ the message goes to that agent instead, through the same addressing-policy+ and budget checks as any routed message, marked `routed_reason: "fallback"`+ so it knows why it got it. It cannot be the room's assigned agent, which+ already gets every untagged message before auto-route is asked.+ `!auto-route fallback off` clears it.++### Across a restart++A message waiting for a decision is recorded with the message+(`pending_decisions`) and decided by whichever process gets to it first: the+one that wrote it at once, any other on its sweep every few seconds, or the+same one after a restart. Each is delivered at most once. One still waiting+after `DECISION_PENDING_MAX_AGE_SECONDS` is dropped and logged.++## What is reported++Every decision is logged with its room, candidate count, choice, confidence,+threshold, latency and backend, never the message. Product telemetry, when it+is on, counts decisions and failures without any room, agent or message+identifier: `decision_made` and `decision_failed` in+`docs/old/telemetry-events.md`.diff --git a/docs/old/telemetry-events.md b/docs/old/telemetry-events.mdindex 9581a1f8b..ea209ebf7 100644--- a/docs/old/telemetry-events.md+++ b/docs/old/telemetry-events.md@@ -850,6 +850,31 @@ separates an agent answering people from agents talking among themselves.| `has_attachment` | boolean || `in_thread` | boolean |+### Decision routing — one per decision++One per message auto-route asked a decision service about+(`decision/client.py`). Neither says which room, agent or message: only+whether an agent was picked, how sure the service was, how long it took, and+which kind of backend answered, so the share of decisions that chose nobody,+failures by reason and latency percentiles can be read per backend.++**`decision_made`** — the service answered, with an agent or with nobody.++| Property | Type |+|---|---|+| `backend` | `server_default` \| `endpoint` \| `haiku_anthropic` \| `haiku_bedrock` — the server's own decision service as a workspace's default, a workspace's own endpoint, or Haiku |+| `outcome` | `agent` \| `none` |+| `confidence` | `none` \| `lt_0.5` \| `0.5_0.6` \| `0.6_0.7` \| `0.7_0.8` \| `0.8_0.9` \| `0.9_1.0` — the service's confidence in its choice, in tenths above one half; `none` when it gave none |+| `latency_ms` | number — measured by Switch around the whole request |++**`decision_failed`** — the service gave no usable answer, so the message went+to nobody or to the room's triage agent.++| Property | Type |+|---|---|+| `backend` | as above |+| `reason` | `timeout` \| `unreachable` \| `error_status` \| `bad_answer` (not a decision, or an agent that was not a candidate) \| `key_unreadable` (a workspace key the server can no longer decrypt) \| `endpoint_refused` (a workspace endpoint the server's outbound policy does not allow: link-local, or private and not in `OUTBOUND_ALLOWED_PRIVATE_HOSTS`) |+### Resources, keys, groups and the restWhat each one is for, and how to make it fire, is under@@ -1134,6 +1159,14 @@ Two things to set up first, or a third of the list cannot fire at all:Tag two agents and there are two. Tag an agent whose policy refuses you andthere are none.+**Decision routing** — `decision/client.py`++- `decision_made` — with `decision_routing` on and a decision service set up,+ `!auto-route on` in a room and post an untagged question. "thanks!" is not+ asked about, so it emits nothing.+- `decision_failed` — the same with the decision service stopped+ (`unreachable`), or slower than `DECISION_TIMEOUT_SECONDS` (`timeout`).+**Connectors** — `bridges/collaboration/lifecycle_service.py`- `bridge_connected` — a bridge reaching its platform. Also fires withdiff --git a/scripts/e2e_surface.py b/scripts/e2e_surface.pyindex 82a0aa305..e332fc247 100644--- a/scripts/e2e_surface.py+++ b/scripts/e2e_surface.py@@ -47,7 +47,7 @@ from starlette.routing import Routefrom switch_core.bridges.agent.operations import declared_operationsfrom switch_core.config import SwitchConfig-from switch_core.feature_flags import KNOWN_FEATURE_FLAGS+from switch_core.feature_flags import KNOWN_FEATURE_FLAGS, without_flagfrom switch_core.gateway.app import create_gateway_appfrom switch_core.gateway.auth import (get_authenticated_caller,@@ -216,7 +216,7 @@ def gateway_routes() -> list[dict[str, Any]]:routes = _routes_by_key(gateway_app(_config(every)))gated_by: dict[str, list[str]] = {key: [] for key in routes}for flag in sorted(every):- without = _routes_by_key(gateway_app(_config(every - {flag})))+ without = _routes_by_key(gateway_app(_config(without_flag(every, flag))))unexpected = sorted(set(without) - set(routes))if unexpected:raise ValueError(