Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions loopdy_plugin/managed_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@
_ID = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$")
_PROFILE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$")
_CRON_SESSION = re.compile(r"^cron_.+_\d{8}_\d{6}$")


def _is_scheduled_silence(response: Any) -> bool:
"""True when a scheduled run replied with Hermes's cron silence marker (e.g. ``[SILENT]``).

Uses the same predicate the cron scheduler uses to skip delivery, so a run that stays
silent on its delivery lane never reaches the phone as a push either.
"""
try:
from gateway.response_filters import is_autonomous_silence_response
except ImportError:
return isinstance(response, str) and response.strip().upper().startswith("[SILENT]")
return is_autonomous_silence_response(response)
_EVENT_TYPES = {
"session.completed", "session.failed", "scheduled.completed", "scheduled.failed",
"approval.required", "clarification.required", "subagent.completed", "subagent.failed",
Expand Down Expand Up @@ -740,8 +753,14 @@ def _observe(self, hook: str, *, profile: str, **payload: Any):
turn = payload.get("turn_id")
if hook == "post_llm_call" and isinstance(turn, str) and _ID.fullmatch(turn):
coordinate = (profile, session_id, turn)
scheduled = payload.get("platform") == "cron" or _CRON_SESSION.fullmatch(session_id) is not None
try:
response_text = self._rich_text(payload.get("assistant_response"))
# A silent scheduled run keeps no reply, so neither this alert nor the
# on_session_end fallback pushes the marker. Failures still alert.
if scheduled and _is_scheduled_silence(payload.get("assistant_response")):
response_text = ""
else:
response_text = self._rich_text(payload.get("assistant_response"))
except ManagedNotificationError:
response_text = ""
if response_text:
Expand All @@ -754,7 +773,6 @@ def _observe(self, hook: str, *, profile: str, **payload: Any):
# reply is saved. on_session_end comes only after post-turn work (external memory
# sync, reviews) that can take several seconds, so the reply alert goes out now.
if response_text and not child_hook:
scheduled = payload.get("platform") == "cron" or _CRON_SESSION.fullmatch(session_id) is not None
try:
self._queue_event(profile, session_id, turn,
"scheduled.completed" if scheduled else "session.completed",
Expand Down
24 changes: 24 additions & 0 deletions tests/test_managed_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,30 @@ def test_scheduled_run_alerts_without_a_session_subscription(self):
self.service.drain_pending()
self.assertIn("scheduled.completed",[e["eventType"] for e in self.events_sent()])

def test_silent_scheduled_run_sends_no_alert(self):
# Regression: a cron run answering "[SILENT]" skipped Telegram delivery but
# still reached the phone as a push reading "[SILENT]".
self.grant["eventTypes"]=["scheduled.completed","scheduled.failed","session.completed","session.failed"]
grant_id=str(uuid.uuid4()); self.grant["grantId"]=grant_id; self.grant_id=grant_id
self.service.enroll(grant_id,str(uuid.uuid4())); self.calls.clear()
cron="cron_143638f07ae3_20260927_185802"
for turn,reply in (("turn-s","[SILENT]"),("turn-t","[SILENT] No qualifying restock.")):
self.service.observe("post_llm_call",profile="default",session_id=cron,turn_id=turn,assistant_response=reply,platform="cron")
self.service.observe("on_session_end",profile="default",session_id=cron,turn_id=turn,completed=True,platform="cron")
self.service.drain_pending()
self.assertEqual(self.events_sent(),[])
self.service.observe("post_llm_call",profile="default",session_id=cron,turn_id="turn-r",assistant_response="Restock: ETB in stock at MSRP.",platform="cron")
self.service.observe("on_session_end",profile="default",session_id=cron,turn_id="turn-r",completed=True,platform="cron")
self.service.observe("on_session_end",profile="default",session_id=cron,turn_id="turn-f",failed=True,error="provider unreachable",platform="cron")
self.service.drain_pending()
self.assertEqual([e["eventType"] for e in self.events_sent()],["scheduled.completed","scheduled.failed"])

def test_silence_marker_in_chat_still_alerts(self):
self.service.observe("post_llm_call",profile="default",session_id="desktop-chat",turn_id="turn-a",assistant_response="[SILENT]")
self.service.observe("on_session_end",profile="default",session_id="desktop-chat",turn_id="turn-a",completed=True,platform="desktop")
self.service.drain_pending()
self.assertEqual([e["eventType"] for e in self.events_sent()],["session.completed"])

def test_subagent_completion_alerts_for_an_unopened_parent_session(self):
self.grant["eventTypes"]=["session.completed","session.failed","subagent.completed","subagent.failed"]
grant_id=str(uuid.uuid4()); self.grant["grantId"]=grant_id; self.grant_id=grant_id
Expand Down