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: log dropped trust notices, don't notify before relay confirms

Timo Bozsolik-Torresb31fbb7b6065
diff --git a/core/switch_core/bridges/agent/protocol/agent_core.py b/core/switch_core/bridges/agent/protocol/agent_core.py
index 0e7b32a5b..4127d7536 100644
--- a/core/switch_core/bridges/agent/protocol/agent_core.py
+++ b/core/switch_core/bridges/agent/protocol/agent_core.py
@@ -1294,6 +1294,12 @@ class AgentCore:
None,
)
if admin is None:
+ logger.warning(
+ "No admin client running for %s; dropping a Switch Trust %s "
+ "notice that would have been posted there",
+ room.id,
+ outcome,
+ )
return
message_type = (
AdminMessageType.TRUST_ALERTED
@@ -1853,9 +1859,17 @@ class AgentCore:
event_id = await client.send_message(
room.transport_room_id, f"*{detail}*", format="markdown", metered=True
)
- if pending_alert is not None and event_id is not None:
- annotation, outcome = pending_alert
- await self._post_trust_alert_notice(room, event_id, annotation, outcome)
+ if pending_alert is not None:
+ if event_id is None:
+ logger.warning(
+ "Status update to %s failed to send; dropping its pending "
+ "Switch Trust %s notice along with it",
+ room.id,
+ pending_alert[1],
+ )
+ else:
+ annotation, outcome = pending_alert
+ await self._post_trust_alert_notice(room, event_id, annotation, outcome)
async def set_runtime_state(
self,
@@ -2597,11 +2611,19 @@ class AgentCore:
event_id = await client.send_message(
room.transport_room_id, outcome, format="markdown", metered=True
)
- if pending_alert is not None and event_id is not None:
- annotation, trust_outcome = pending_alert
- await self._post_trust_alert_notice(
- room, event_id, annotation, trust_outcome
- )
+ if pending_alert is not None:
+ if event_id is None:
+ logger.warning(
+ "Finalise message for task %s failed to send; dropping "
+ "its pending Switch Trust %s notice along with it",
+ task_id,
+ pending_alert[1],
+ )
+ else:
+ annotation, trust_outcome = pending_alert
+ await self._post_trust_alert_notice(
+ room, event_id, annotation, trust_outcome
+ )
async def cancel_task(self, agent_id: str, task_id: str, reason: str) -> None:
"""Cancel a task (requester only)."""
diff --git a/core/switch_core/bridges/collaboration/collaboration_core.py b/core/switch_core/bridges/collaboration/collaboration_core.py
index 46d94918e..9de0f0b65 100644
--- a/core/switch_core/bridges/collaboration/collaboration_core.py
+++ b/core/switch_core/bridges/collaboration/collaboration_core.py
@@ -984,24 +984,9 @@ class CollaborationCore:
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._post_trust_notice(
- msg, redaction_notice, AdminMessageType.TRUST_REDACTED
- )
annotation = trust_annotation(check)
if annotation is not None:
content = f"{content}\n\n{annotation}"
- # The room itself never relays a human-authored event back out
- # to a platform (see handle_outbound_message's human-sender
- # skip), so without this the sender would never see the
- # annotation their own client just appended to their message.
- notice_type = (
- AdminMessageType.TRUST_ALERTED
- if check.outcome == "alerted"
- else AdminMessageType.TRUST_ERRORED
- )
- await self._post_trust_notice(msg, annotation, notice_type)
event_id = await human_actor.send_message(
transport_room_id,
content,
@@ -1017,6 +1002,27 @@ class CollaborationCore:
transport_room_id,
)
return
+ # Tell the sender what happened only now that the message is
+ # actually confirmed to have reached the room — posting this
+ # beforehand risks telling them their message was flagged/
+ # redacted when it never got there at all.
+ if check.redacted_content is not None:
+ redaction_notice = trust_redaction_notice(check)
+ if redaction_notice is not None:
+ await self._post_trust_notice(
+ msg, redaction_notice, AdminMessageType.TRUST_REDACTED
+ )
+ if annotation is not None:
+ # The room itself never relays a human-authored event back out
+ # to a platform (see handle_outbound_message's human-sender
+ # skip), so without this the sender would never see the
+ # annotation their own client just appended to their message.
+ notice_type = (
+ AdminMessageType.TRUST_ALERTED
+ if check.outcome == "alerted"
+ else AdminMessageType.TRUST_ERRORED
+ )
+ await self._post_trust_notice(msg, annotation, notice_type)
# Record the correlation so a later reply (either direction) threads.
await self._record_message_map(
external_channel_id=msg.channel_id,
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 f944b10c3..5953d8b6d 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
@@ -33,11 +33,16 @@ class _FakeHumanActor:
"""Already-joined human actor — these tests exercise the trust gate, not
the join race `test_bridge_unknown_member_first_message.py` covers."""
- def __init__(self) -> None:
+ def __init__(self, *, fail_send: bool = False) -> None:
self.transport_user_id = "@ext_alice:switch.local"
self.sent: list[tuple[str, str]] = []
+ self._fail_send = fail_send
- async def send_message(self, room_id: str, content: str, **_kw: object) -> str:
+ async def send_message(
+ self, room_id: str, content: str, **_kw: object
+ ) -> str | None:
+ if self._fail_send:
+ return None
self.sent.append((room_id, content))
return "$event-1"
@@ -275,6 +280,32 @@ async def test_alerted_message_reaches_the_room_and_sender_is_notified() -> None
assert message_type == AdminMessageType.TRUST_ALERTED.value
+async def test_a_failed_relay_does_not_notify_the_sender_of_an_alert_never_sent() -> (
+ None
+):
+ """If the message never actually reached the room, telling the sender it
+ was flagged/redacted would be misleading — the notice must wait until the
+ send is confirmed."""
+ human_actor = _FakeHumanActor(fail_send=True)
+ adapter = _FakeAdapter()
+ result = TrustCheckResult(
+ outcome="alerted",
+ policy_id="policy-1",
+ policy_name="default",
+ findings=(
+ TrustFinding(category="pii/email", detector_name="email", severity="low"),
+ ),
+ )
+ bridge = _bridge(human_actor, adapter, _FixedTrustClient(result))
+
+ await CollaborationCore._handle_inbound_message(
+ bridge, _message("my email is [email protected]")
+ )
+
+ assert human_actor.sent == []
+ assert adapter.admin_messages == []
+
+
async def test_trust_check_failure_fails_open_and_annotates_instead_of_blocking() -> (
None
):