Repository navigation
gateway: suppress status/lifecycle messages in group chats #25748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,12 @@ def _normalize_server_url(raw: str) -> str: | |
| return value.rstrip("/") | ||
|
|
||
|
|
||
| def _truthy(value: Any, default: bool = False) -> bool: | ||
| if value is None: | ||
| return default | ||
| if isinstance(value, bool): | ||
| return value | ||
| return str(value).strip().lower() in {"1", "true", "yes", "y", "on"} | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -124,6 +130,13 @@ def __init__(self, config: PlatformConfig): | |
| if not str(self.webhook_path).startswith("/"): | ||
| self.webhook_path = f"/{self.webhook_path}" | ||
| self.send_read_receipts = bool(extra.get("send_read_receipts", True)) | ||
| # Whether kind="status" messages are delivered into BlueBubbles / iMessage | ||
| # group chats. Default False because every iMessage send is a permanent, | ||
| # un-editable bubble visible to every member. DMs still receive status. | ||
| self.show_status_in_groups = _truthy( | ||
| os.getenv("BLUEBUBBLES_SHOW_STATUS_IN_GROUPS"), | ||
| bool(extra.get("show_status_in_groups", False)), | ||
| ) | ||
| self.client: Optional[httpx.AsyncClient] = None | ||
| self._runner = None | ||
| self._private_api_enabled: Optional[bool] = None | ||
|
|
@@ -669,10 +682,33 @@ async def get_chat_info(self, chat_id: str) -> Dict[str, Any]: | |
| info["name"] = display_name | ||
| if participants: | ||
| info["participants"] = participants | ||
| if len(participants) > 1: | ||
| info["type"] = "group" | ||
| except Exception: | ||
| pass | ||
| return info | ||
|
|
||
| async def _should_suppress_status( | ||
| self, | ||
| chat_id: str, | ||
| *, | ||
| kind: str = "status", | ||
| metadata: Optional[Dict[str, Any]] = None, | ||
| ) -> bool: | ||
| """Drop kind="status" messages in BlueBubbles/iMessage groups by default.""" | ||
| del kind, metadata | ||
| if self.show_status_in_groups: | ||
| return False | ||
| try: | ||
| info = await self.get_chat_info(str(chat_id or "")) | ||
| except Exception as exc: | ||
| logger.warning( | ||
| "[bluebubbles] get_chat_info failed during status policy check; dropping kind=status: %s", | ||
| exc, | ||
| ) | ||
| return True | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This converts a chat-info failure into suppression, contrary to the base policy's stated fail-open contract. Return |
||
| return (info or {}).get("type") == "group" | ||
|
|
||
| def format_message(self, content: str) -> str: | ||
| return strip_markdown(content) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2537,6 +2537,7 @@ async def _handle_active_session_busy_message(self, event: MessageEvent, session | |
| else (None if event.source.platform == Platform.TELEGRAM and event.source.thread_id else event.message_id) | ||
| ), | ||
| metadata=thread_meta, | ||
| kind="status", | ||
| ) | ||
| return True | ||
|
|
||
|
|
@@ -2686,6 +2687,7 @@ async def _handle_active_session_busy_message(self, event: MessageEvent, session | |
| else (None if event.source.platform == Platform.TELEGRAM and event.source.thread_id else event.message_id) | ||
| ), | ||
| metadata=thread_meta, | ||
| kind="status", | ||
| ) | ||
| except Exception as e: | ||
| logger.debug("Failed to send busy-ack: %s", e) | ||
|
|
@@ -2807,7 +2809,12 @@ async def _notify_active_sessions_of_shutdown(self) -> None: | |
| # correct forum topic / thread. | ||
| metadata = {"thread_id": thread_id} if thread_id else None | ||
|
|
||
| result = await adapter.send(chat_id, msg, metadata=metadata) | ||
| result = await adapter._send_with_retry( | ||
| chat_id=chat_id, | ||
| content=msg, | ||
| metadata=metadata, | ||
| kind="status", | ||
| ) | ||
| if result is not None and getattr(result, "success", True) is False: | ||
| logger.debug( | ||
| "Failed to send shutdown notification to %s:%s: %s", | ||
|
|
@@ -2853,9 +2860,18 @@ async def _notify_active_sessions_of_shutdown(self) -> None: | |
| try: | ||
| metadata = {"thread_id": home.thread_id} if home.thread_id else None | ||
| if metadata: | ||
| result = await adapter.send(str(home.chat_id), msg, metadata=metadata) | ||
| result = await adapter._send_with_retry( | ||
| chat_id=str(home.chat_id), | ||
| content=msg, | ||
| metadata=metadata, | ||
| kind="status", | ||
| ) | ||
| else: | ||
| result = await adapter.send(str(home.chat_id), msg) | ||
| result = await adapter._send_with_retry( | ||
| chat_id=str(home.chat_id), | ||
| content=msg, | ||
| kind="status", | ||
| ) | ||
| if result is not None and getattr(result, "success", True) is False: | ||
| logger.debug( | ||
| "Failed to send shutdown notification to home channel %s:%s: %s", | ||
|
|
@@ -14818,10 +14834,11 @@ def _status_callback_sync(event_type: str, message: str) -> None: | |
| return | ||
| try: | ||
| _fut = asyncio.run_coroutine_threadsafe( | ||
| _status_adapter.send( | ||
| _status_chat_id, | ||
| message, | ||
| _status_adapter._send_with_retry( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Current main's status callback first calls |
||
| chat_id=_status_chat_id, | ||
| content=message, | ||
| metadata=_status_thread_metadata, | ||
| kind="status", | ||
| ), | ||
| _loop_for_step, | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any, Dict, Optional | ||
|
|
||
| import pytest | ||
|
|
||
| from gateway.config import PlatformConfig | ||
| from gateway.platforms.bluebubbles import BlueBubblesAdapter | ||
| from gateway.platforms.base import SendResult | ||
|
|
||
|
|
||
| class _RecordingBlueBubblesAdapter(BlueBubblesAdapter): | ||
| def __init__(self, *, chat_type: str = "dm", show_status_in_groups: bool = False): | ||
| super().__init__( | ||
| PlatformConfig( | ||
| enabled=True, | ||
| extra={"show_status_in_groups": show_status_in_groups}, | ||
| ) | ||
| ) | ||
| self._chat_type = chat_type | ||
| self.sent: list[dict[str, Any]] = [] | ||
|
|
||
| async def send( | ||
| self, | ||
| chat_id: str, | ||
| content: str, | ||
| reply_to: Optional[str] = None, | ||
| metadata: Optional[Dict[str, Any]] = None, | ||
| ) -> SendResult: | ||
| self.sent.append( | ||
| { | ||
| "chat_id": chat_id, | ||
| "content": content, | ||
| "reply_to": reply_to, | ||
| "metadata": metadata, | ||
| } | ||
| ) | ||
| return SendResult(success=True, message_id="bb-1") | ||
|
|
||
| async def get_chat_info(self, chat_id: str) -> Dict[str, Any]: | ||
| return {"name": chat_id, "type": self._chat_type, "chat_id": chat_id} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_bluebubbles_suppresses_kind_status_in_groups_by_default(): | ||
| adapter = _RecordingBlueBubblesAdapter(chat_type="group") | ||
|
|
||
| result = await adapter._send_with_retry( | ||
| chat_id="chat;+;guid", | ||
| content="⚡ Interrupting current task", | ||
| kind="status", | ||
| ) | ||
|
|
||
| assert result.success is True | ||
| assert result.suppressed is True | ||
| assert adapter.sent == [] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_bluebubbles_delivers_normal_replies_in_groups(): | ||
| adapter = _RecordingBlueBubblesAdapter(chat_type="group") | ||
|
|
||
| result = await adapter._send_with_retry( | ||
| chat_id="chat;+;guid", | ||
| content="normal answer", | ||
| ) | ||
|
|
||
| assert result.success is True | ||
| assert result.suppressed is False | ||
| assert len(adapter.sent) == 1 | ||
| assert adapter.sent[0]["content"] == "normal answer" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_bluebubbles_delivers_status_in_dms(): | ||
| adapter = _RecordingBlueBubblesAdapter(chat_type="dm") | ||
|
|
||
| result = await adapter._send_with_retry( | ||
| chat_id="person@example.com", | ||
| content="⚡ Interrupting current task", | ||
| kind="status", | ||
| ) | ||
|
|
||
| assert result.success is True | ||
| assert result.suppressed is False | ||
| assert len(adapter.sent) == 1 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_bluebubbles_status_group_suppression_can_be_disabled(): | ||
| adapter = _RecordingBlueBubblesAdapter( | ||
| chat_type="group", | ||
| show_status_in_groups=True, | ||
| ) | ||
|
|
||
| result = await adapter._send_with_retry( | ||
| chat_id="chat;+;guid", | ||
| content="⚡ Interrupting current task", | ||
| kind="status", | ||
| ) | ||
|
|
||
| assert result.success is True | ||
| assert result.suppressed is False | ||
| assert len(adapter.sent) == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep this behavior setting in the configured
PlatformConfig.extrapath only rather than adding a new environment-variable override;AGENTS.md:102-107reserves behavioral settings forconfig.yaml.