Skip to content

Self-improving AI Employees for everyone

feat(broadcaster): typed CloudEvents siblings for plugin-status updates (Wave 11.I, X4)

trohitg08688bf3a137
diff --git a/server/services/status_broadcaster.py b/server/services/status_broadcaster.py
index 48c1f661..cbfe9128 100644
--- a/server/services/status_broadcaster.py
+++ b/server/services/status_broadcaster.py
@@ -355,6 +355,40 @@ class StatusBroadcaster:
# Android Status Updates
# =========================================================================
+ async def _emit_connection_typed(
+ self,
+ plugin: str,
+ connected: bool,
+ subject: Optional[str],
+ data: Dict[str, Any],
+ ) -> None:
+ """Emit a CloudEvents-typed sibling broadcast alongside the
+ legacy ``{plugin}_status`` raw frame (Wave 11.I, X4).
+
+ Wire key ``plugin_connection_status`` mirrors the
+ ``credential_catalogue_updated`` pattern: single wire channel
+ for typed-envelope listeners; the envelope's ``type`` field
+ carries the specific action (``{plugin}.connection.opened`` /
+ ``.closed``) and ``subject`` carries the device/account id.
+
+ Frontend listeners that grew up on the raw ``{plugin}_status``
+ frame keep working unchanged; future listeners can subscribe
+ to ``plugin_connection_status`` for the typed channel and
+ retire the raw frame in a coordinated FE migration (Phase 4 Y3).
+ """
+ from services.events import WorkflowEvent
+
+ event = WorkflowEvent.connection_status(
+ plugin=plugin,
+ connected=connected,
+ subject=subject,
+ data=data,
+ )
+ await self.broadcast({
+ "type": "plugin_connection_status",
+ "data": event.model_dump(mode="json"),
+ })
+
async def update_android_status(
self,
connected: bool,
@@ -366,7 +400,12 @@ class StatusBroadcaster:
qr_data: Optional[str] = None,
session_token: Optional[str] = None
):
- """Update Android relay connection status and broadcast."""
+ """Update Android relay connection status and broadcast.
+
+ Emits both the legacy ``android_status`` raw frame (FE
+ back-compat) and a CloudEvents-typed sibling via
+ :meth:`_emit_connection_typed` (Wave 11.I, X4).
+ """
self._status["android"] = {
"connected": connected,
"paired": paired,
@@ -382,6 +421,12 @@ class StatusBroadcaster:
"type": "android_status",
"data": self._status["android"]
})
+ await self._emit_connection_typed(
+ plugin="android",
+ connected=connected,
+ subject=device_id,
+ data=self._status["android"],
+ )
# =========================================================================
# Status updates -- per-service refresh bodies live in their plugin
@@ -401,7 +446,11 @@ class StatusBroadcaster:
device_id: Optional[str] = None,
qr: Optional[str] = None
):
- """Update WhatsApp connection status and broadcast."""
+ """Update WhatsApp connection status and broadcast.
+
+ Emits both the legacy ``whatsapp_status`` raw frame and a
+ CloudEvents-typed sibling (Wave 11.I, X4).
+ """
import time
self._status["whatsapp"] = {
"connected": connected,
@@ -417,6 +466,12 @@ class StatusBroadcaster:
"type": "whatsapp_status",
"data": self._status["whatsapp"]
})
+ await self._emit_connection_typed(
+ plugin="whatsapp",
+ connected=connected,
+ subject=device_id,
+ data=self._status["whatsapp"],
+ )
# =========================================================================
# Telegram Status Updates
@@ -430,7 +485,11 @@ class StatusBroadcaster:
bot_name: Optional[str] = None,
owner_chat_id: Optional[int] = None
):
- """Update Telegram bot connection status and broadcast."""
+ """Update Telegram bot connection status and broadcast.
+
+ Emits both the legacy ``telegram_status`` raw frame and a
+ CloudEvents-typed sibling (Wave 11.I, X4).
+ """
import time
self._status["telegram"] = {
"connected": connected,
@@ -445,6 +504,12 @@ class StatusBroadcaster:
"type": "telegram_status",
"data": self._status["telegram"]
})
+ await self._emit_connection_typed(
+ plugin="telegram",
+ connected=connected,
+ subject=bot_username,
+ data=self._status["telegram"],
+ )
# =========================================================================
# Node Status Updates
diff --git a/server/tests/test_status_broadcasts.py b/server/tests/test_status_broadcasts.py
index a3270045..837030f2 100644
--- a/server/tests/test_status_broadcasts.py
+++ b/server/tests/test_status_broadcasts.py
@@ -323,6 +323,74 @@ class TestStatusBroadcastsUseTypedBuilder:
)
+# ============================================================================
+# 3b) Plugin status broadcasts ALSO emit typed CloudEvents siblings
+# ============================================================================
+
+
+class TestStatusBroadcastsAlsoEmitTypedEnvelope:
+ """Wave 11.I, X4: every legacy ``update_<plugin>_status`` method
+ listed in :data:`_LEGACY_RAW_DICT_BROADCASTS` MUST also call
+ :meth:`StatusBroadcaster._emit_connection_typed` (or otherwise
+ construct ``WorkflowEvent.connection_status(...)``) so a typed
+ sibling broadcast goes out alongside the legacy raw frame.
+
+ Phase 4 Y3 retires the raw frame after frontend migrates to read
+ the typed channel. Until then, both frames coexist: existing FE
+ consumers stay compatible; typed listeners can opt in.
+ """
+
+ @pytest.fixture
+ def broadcaster_methods(self) -> dict[str, str]:
+ from services import status_broadcaster as sb
+
+ cls_src = inspect.getsource(sb.StatusBroadcaster)
+ return _split_methods(cls_src)
+
+ @pytest.mark.parametrize("method_name", sorted(_LEGACY_RAW_DICT_BROADCASTS))
+ def test_legacy_status_method_emits_typed_sibling(
+ self, broadcaster_methods, method_name: str,
+ ):
+ body = broadcaster_methods.get(method_name)
+ assert body is not None, (
+ f"StatusBroadcaster.{method_name} missing -- update "
+ f"_LEGACY_RAW_DICT_BROADCASTS or restore the method."
+ )
+ assert "_emit_connection_typed" in body or "WorkflowEvent.connection_status" in body, (
+ f"StatusBroadcaster.{method_name} emits only the legacy raw "
+ f"{{type: 'X_status', data: {{...}}}} frame. It must ALSO emit a "
+ f"CloudEvents-typed sibling via "
+ f"``await self._emit_connection_typed(plugin=..., connected=..., "
+ f"subject=..., data=...)`` so future typed-channel listeners "
+ f"can subscribe. Wave 11.I, X4 contract."
+ )
+
+ def test_emit_connection_typed_helper_exists(self):
+ """The shared helper must exist on the broadcaster -- locks
+ the canonical extension point so per-method migrations don't
+ re-invent the typed-envelope construction."""
+ from services.status_broadcaster import StatusBroadcaster
+
+ assert hasattr(StatusBroadcaster, "_emit_connection_typed"), (
+ "StatusBroadcaster._emit_connection_typed is the canonical "
+ "helper for typed plugin-connection broadcasts. It must "
+ "exist; per-method update_<plugin>_status implementations "
+ "call it after the legacy raw broadcast."
+ )
+
+ def test_emit_connection_typed_uses_workflow_event_factory(self):
+ """The helper itself must construct via the typed factory, not
+ a hand-rolled WorkflowEvent literal."""
+ from services.status_broadcaster import StatusBroadcaster
+
+ src = inspect.getsource(StatusBroadcaster._emit_connection_typed)
+ assert "WorkflowEvent.connection_status" in src, (
+ "_emit_connection_typed must call WorkflowEvent.connection_status(...) "
+ "(the typed factory) rather than hand-rolling the envelope. "
+ "Wave 11.I CloudEvents contract."
+ )
+
+
# ============================================================================
# 4) send_custom_event callsite ratchet
# ============================================================================