Skip to content

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

Address review: fail closed on empty redaction, don't let a notice swallow a message, defer agent-name lookup

Timo Bozsolik-Torresd35142d73df3
diff --git a/core/switch_core/bridges/agent/protocol/agent_core.py b/core/switch_core/bridges/agent/protocol/agent_core.py
index c05876bcf..f08438ea2 100644
--- a/core/switch_core/bridges/agent/protocol/agent_core.py
+++ b/core/switch_core/bridges/agent/protocol/agent_core.py
@@ -60,7 +60,7 @@ from switch_core.bridges.agent.registration_bootstrap import (
)
from switch_core.bridges.resource.service import ResourceService
from switch_core.budgets import BudgetGuard
-from switch_core.clients.actor import SystemActor
+from switch_core.clients.actor import AgentActor, SystemActor
from switch_core.clients.admin_messages import PLATFORM_MARKER as _PLATFORM_MARKER
from switch_core.clients.admin_messages import (
AdminMessageType,
@@ -1231,21 +1231,26 @@ class AgentCore:
room: RoomDescriptor,
content: str,
thread_root_id: str | None,
- agent_name: str,
+ client: AgentActor,
) -> str:
"""Check agent-authored content against Switch Trust before it is
sent. Returns the content to actually send — unchanged, redacted, or
carrying a short non-blocking annotation. Raises
- `GuardrailBlockedError` on a BLOCKED verdict, having already posted a
- notice in the room in place of the real content."""
+ `GuardrailBlockedError` on a BLOCKED verdict, or on a REDACTED verdict
+ Switch Trust gave us nothing to redact with — sending the original
+ content then would be indistinguishable from not checking at all —
+ having already posted a notice in the room in place of the real
+ content."""
check = await check_message(
self.trust_client,
role="assistant",
content=content,
room_id=room.id,
)
- if check.blocked:
- await self._post_trust_blocked_notice(room, thread_root_id, agent_name)
+ if check.blocked or (
+ check.outcome == "redacted" and check.redacted_content is None
+ ):
+ await self._post_trust_blocked_notice(room, thread_root_id, client)
raise GuardrailBlockedError(check)
if check.redacted_content is not None:
content = check.redacted_content
@@ -1255,7 +1260,7 @@ class AgentCore:
return content
async def _post_trust_blocked_notice(
- self, room: RoomDescriptor, thread_root_id: str | None, agent_name: str
+ self, room: RoomDescriptor, thread_root_id: str | None, client: AgentActor
) -> None:
"""Tell the room a response was blocked, in place of sending it."""
admin = next(
@@ -1271,8 +1276,8 @@ class AgentCore:
try:
await admin.send_admin(
room.transport_room_id,
- f"🚫 A response from @{agent_name} was blocked by Switch Trust "
- "and was not sent.",
+ f"🚫 A response from @{client.agent.name} was blocked by Switch "
+ "Trust and was not sent.",
message_type=AdminMessageType.TRUST_BLOCKED,
thread_root_id=thread_root_id,
)
@@ -1344,9 +1349,7 @@ class AgentCore:
thread_root_id = await self._resolve_thread_root(
client, room.transport_room_id, thread_id
)
- content = await self._enforce_trust(
- room, content, thread_root_id, client.agent.name
- )
+ content = await self._enforce_trust(room, content, thread_root_id, client)
event_id = await client.send_message(
room.transport_room_id,
content,
@@ -1815,7 +1818,7 @@ class AgentCore:
client = self.client_lifecycle.get_by_agent_id(agent_id)
if client is None:
raise ValueError("Agent client not running")
- detail = await self._enforce_trust(room, detail, None, client.agent.name)
+ detail = await self._enforce_trust(room, detail, None, client)
await client.send_message(
room.transport_room_id, f"*{detail}*", format="markdown", metered=True
)
@@ -2544,7 +2547,7 @@ class AgentCore:
client = self.client_lifecycle.get_by_agent_id(agent_id)
if client and client.transport:
- outcome = await self._enforce_trust(room, outcome, None, client.agent.name)
+ outcome = await self._enforce_trust(room, outcome, None, client)
await client.send_event(
room.transport_room_id,
"com.switch.task.finalise",
diff --git a/core/switch_core/bridges/collaboration/collaboration_core.py b/core/switch_core/bridges/collaboration/collaboration_core.py
index bf7c68028..c8ca41e89 100644
--- a/core/switch_core/bridges/collaboration/collaboration_core.py
+++ b/core/switch_core/bridges/collaboration/collaboration_core.py
@@ -960,31 +960,34 @@ class CollaborationCore:
content=content,
room_id=room_id,
)
- if check.blocked:
+ # A REDACTED verdict with nothing to redact with is not safe to
+ # treat as a pass: sending the original content then would be
+ # indistinguishable from not checking at all, so it is handled
+ # like a block.
+ if check.blocked or (
+ check.outcome == "redacted" and check.redacted_content is None
+ ):
logger.warning(
- "[BRIDGE-IN] message from %s blocked by Switch Trust "
- "(policy=%s) — not relayed into room %s",
+ "[BRIDGE-IN] message from %s not relayed — Switch Trust "
+ "outcome=%s (policy=%s) — room %s",
msg.sender_name,
+ check.outcome,
check.policy_id,
transport_room_id,
)
- await self._adapter.admin_message(
- msg.channel_id,
+ await self._post_trust_notice(
+ msg,
"🚫 Your message was blocked by Switch Trust and was not "
"delivered.",
- msg.root_id or msg.message_ref,
- message_type=AdminMessageType.TRUST_BLOCKED.value,
+ AdminMessageType.TRUST_BLOCKED,
)
return
if check.redacted_content is not None:
content = check.redacted_content
redaction_notice = trust_redaction_notice(check)
if redaction_notice is not None:
- await self._adapter.admin_message(
- msg.channel_id,
- redaction_notice,
- msg.root_id or msg.message_ref,
- message_type=AdminMessageType.TRUST_REDACTED.value,
+ await self._post_trust_notice(
+ msg, redaction_notice, AdminMessageType.TRUST_REDACTED
)
annotation = trust_annotation(check)
if annotation is not None:
@@ -1063,6 +1066,26 @@ class CollaborationCore:
external_post_id=msg.message_ref,
)
+ async def _post_trust_notice(
+ self, msg: InboundMessage, body: str, message_type: AdminMessageType
+ ) -> None:
+ """Tell the sender's platform a message was blocked or redacted.
+ Never lets a failed notice take the inbound message down with it —
+ mirrors `AgentCore._post_trust_blocked_notice`."""
+ try:
+ await self._adapter.admin_message(
+ msg.channel_id,
+ body,
+ msg.root_id or msg.message_ref,
+ message_type=message_type.value,
+ )
+ except Exception:
+ logger.warning(
+ "Could not post a Switch Trust notice to channel %s",
+ msg.channel_id,
+ exc_info=True,
+ )
+
async def _handle_inbound_command(self, cmd: InboundCommand) -> None:
room_ids = self._channel_to_room.get(cmd.channel_id)
if room_ids is None:
diff --git a/core/tests/integration/test_switch_trust_agent_e2e.py b/core/tests/integration/test_switch_trust_agent_e2e.py
index 0a2ba72e1..cd0b0d7d9 100644
--- a/core/tests/integration/test_switch_trust_agent_e2e.py
+++ b/core/tests/integration/test_switch_trust_agent_e2e.py
@@ -151,6 +151,43 @@ async def test_blocked_agent_message_is_not_sent_and_names_the_agent(
assert trust_client.room_ids == [room_id]
+async def test_redacted_outcome_with_nothing_to_redact_is_not_sent(
+ harness: Harness, session_env: SessionEnv
+) -> None:
+ """`redacted_content` can stay `None` even on a REDACTED verdict (no
+ finding carried usable detected text) — sending the original content then
+ would be indistinguishable from not checking at all."""
+ agent_id, room_id = await _setup_room(
+ harness, session_env, "e2e-trust-redacted-empty", with_admin=True
+ )
+ harness.protocol.trust_client = _FixedTrustClient(
+ TrustCheckResult(
+ outcome="redacted",
+ policy_id="policy-1",
+ policy_name="default",
+ findings=(
+ TrustFinding(
+ category="pii/email", detector_name="email", severity="high"
+ ),
+ ),
+ redacted_content=None,
+ )
+ )
+
+ with pytest.raises(GuardrailBlockedError):
+ await harness.protocol.send_message(
+ agent_id, room_id, "my email is [email protected]"
+ )
+
+ rows = await _timeline_messages(harness, session_env, room_id)
+ assert not any(
+ "my email is [email protected]" in row.content.get("body", "") for row in rows
+ )
+ admin_rows = [row for row in rows if "com.switch.admin" in row.content]
+ assert len(admin_rows) == 1
+ assert admin_rows[0].content["com.switch.admin"]["type"] == "trust_blocked"
+
+
async def test_allowed_agent_message_is_sent_unaffected(
harness: Harness, session_env: SessionEnv
) -> None:
diff --git a/core/tests/switch_core/bridges/collaboration/test_bridge_inbound_trust.py b/core/tests/switch_core/bridges/collaboration/test_bridge_inbound_trust.py
index 4c0a07309..07c18980f 100644
--- a/core/tests/switch_core/bridges/collaboration/test_bridge_inbound_trust.py
+++ b/core/tests/switch_core/bridges/collaboration/test_bridge_inbound_trust.py
@@ -43,8 +43,9 @@ class _FakeHumanActor:
class _FakeAdapter:
- def __init__(self) -> None:
+ def __init__(self, *, raise_on_admin_message: bool = False) -> None:
self.admin_messages: list[tuple[str, str, str | None, str]] = []
+ self._raise_on_admin_message = raise_on_admin_message
def translate_inbound(self, content: str) -> str:
return content
@@ -57,6 +58,8 @@ class _FakeAdapter:
*,
message_type: str,
) -> None:
+ if self._raise_on_admin_message:
+ raise RuntimeError("platform unavailable")
self.admin_messages.append((channel_id, body, root_or_ref, message_type))
@@ -99,6 +102,11 @@ def _bridge(
_channel_locks={},
_trust_client=trust_client,
)
+ # The real implementation, not a re-fake of it: exercises its try/except
+ # around a failing platform notify, same as production.
+ bridge._post_trust_notice = lambda msg, body, message_type: (
+ CollaborationCore._post_trust_notice(bridge, msg, body, message_type)
+ )
bridge.relayed = relayed
return bridge
@@ -171,6 +179,56 @@ async def test_redacted_message_reaches_the_room_redacted_and_sender_is_notified
assert message_type == AdminMessageType.TRUST_REDACTED.value
+async def test_redacted_outcome_with_nothing_to_redact_is_treated_as_blocked() -> None:
+ """`redacted_content` can stay `None` even on a REDACTED verdict (no
+ finding carried usable detected text) — sending the original content then
+ would be indistinguishable from not checking at all."""
+ human_actor = _FakeHumanActor()
+ adapter = _FakeAdapter()
+ result = TrustCheckResult(
+ outcome="redacted",
+ policy_id="policy-1",
+ policy_name="default",
+ findings=(
+ TrustFinding(category="pii/email", detector_name="email", severity="high"),
+ ),
+ redacted_content=None,
+ )
+ bridge = _bridge(human_actor, adapter, _FixedTrustClient(result))
+
+ await CollaborationCore._handle_inbound_message(
+ bridge, _message("my email is [email protected]")
+ )
+
+ assert human_actor.sent == []
+ assert len(adapter.admin_messages) == 1
+ _, body, _, message_type = adapter.admin_messages[0]
+ assert "blocked by Switch Trust" in body
+ assert message_type == AdminMessageType.TRUST_BLOCKED.value
+
+
+async def test_a_failed_redaction_notice_does_not_block_the_redacted_message() -> None:
+ human_actor = _FakeHumanActor()
+ adapter = _FakeAdapter(raise_on_admin_message=True)
+ result = TrustCheckResult(
+ outcome="redacted",
+ policy_id="policy-1",
+ policy_name="default",
+ findings=(
+ TrustFinding(category="pii/email", detector_name="email", severity="high"),
+ ),
+ redacted_content="my email is [redacted]",
+ )
+ bridge = _bridge(human_actor, adapter, _FixedTrustClient(result))
+
+ await CollaborationCore._handle_inbound_message(
+ bridge, _message("my email is [email protected]")
+ )
+
+ # The notice failed, but the redacted message still reached the room.
+ assert human_actor.sent == [(TRANSPORT_ROOM_ID, "my email is [redacted]")]
+
+
async def test_allowed_message_is_relayed_unaffected() -> None:
human_actor = _FakeHumanActor()
adapter = _FakeAdapter()