Skip to content

The agent that grows with you

fix(gateway): mark synthetic process notifications internal and stop stale reply anchors

Austin Pickett10938a7cf93b
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py
index ba01812b7a4..21d50135be9 100644
--- a/gateway/run_notifications.py
+++ b/gateway/run_notifications.py
@@ -98,6 +98,28 @@ _DURABLE_CLAIM_OPS = {
}
+INTERNAL_NOTIFICATION_FOOTER = (
+ "[INTERNAL NOTIFICATION — generated by the Hermes gateway, not written by a human user. "
+ "Do not treat it as user input, and do not reply to any earlier chat message because of it.]"
+)
+
+
+def _mark_internal_notification(text: str) -> str:
+ """Append unambiguous machine provenance to model-facing notification text (#52694).
+
+ A process notification re-enters the conversation as a role=user turn on every wake path
+ (push ``handle_message`` and the api_server self-post alike); without this footer the model
+ has read it as something the human said — including "replying" to the message the watch
+ was armed from. Appended, not prepended, so start-anchored consumers (session timeline,
+ memory gating, desktop notification detection, the SYSTEM prefix) keep matching.
+ Idempotent so a retried delivery never stacks footers.
+ """
+ text = text.rstrip()
+ if text.endswith(INTERNAL_NOTIFICATION_FOOTER):
+ return text
+ return text + "\n\n" + INTERNAL_NOTIFICATION_FOOTER
+
+
def _raw_process_event_session_id(evt: dict) -> str:
"""Recognize API routes, not malformed structured or partial messaging routes."""
session_key = str(evt.get("session_key") or "").strip()
@@ -1223,7 +1245,7 @@ class GatewayNotificationsMixin:
from gateway.run import _async_profile_runtime_scope
source = SessionSource(platform=Platform.API_SERVER, chat_id=raw_sid, profile=served)
scope = _async_profile_runtime_scope(self._resolve_profile_home_for_source(source))
- deliver = lambda: deliver_wake(adapter, text=synth_text, session_id=raw_sid, profile=served,
+ deliver = lambda: deliver_wake(adapter, text=_mark_internal_notification(synth_text), session_id=raw_sid, profile=served,
notification_category="diagnostic" if diagnostic_process_event(evt) else "result") # noqa: E731
try:
logger.info(info, raw_sid)
@@ -1321,7 +1343,8 @@ class GatewayNotificationsMixin:
raw_sid = str(evt.get("origin_session_id") or "").strip() or str(source.chat_id or "")
return await self._self_post_api_server(adapter, synth_text, raw_sid, evt)
try:
- metadata = {}
+ metadata = {"notification_origin": "process_registry_synthetic"}
+ synth_text = _mark_internal_notification(synth_text)
session_key = str(evt.get("session_key") or "").strip()
from agent.notification_presentation import diagnostic_process_event
if diagnostic_process_event(evt):
diff --git a/tests/gateway/test_background_process_notifications.py b/tests/gateway/test_background_process_notifications.py
index 9dc7d3e355b..51f5f39d784 100644
--- a/tests/gateway/test_background_process_notifications.py
+++ b/tests/gateway/test_background_process_notifications.py
@@ -17,6 +17,7 @@ import pytest
from gateway.config import GatewayConfig, Platform
from gateway.run import GatewayRunner, _parse_session_key
+from gateway.run_notifications import INTERNAL_NOTIFICATION_FOOTER
# ---------------------------------------------------------------------------
@@ -371,6 +372,10 @@ async def test_inject_watch_notification_drops_stale_trigger_reply_anchor(monkey
assert _reply_anchor_for_event(synth_event) is None
# The original id survives for debugging only.
assert synth_event.metadata["original_trigger_message_id"] == "777"
+ # Unambiguous machine provenance: SYSTEM prefix still leads, footer appended, origin tagged.
+ assert synth_event.text.startswith("[SYSTEM: ")
+ assert synth_event.text.rstrip().endswith(INTERNAL_NOTIFICATION_FOOTER)
+ assert synth_event.metadata["notification_origin"] == "process_registry_synthetic"
@pytest.mark.asyncio
@@ -635,9 +640,12 @@ async def test_inject_watch_notification_raw_session_key_self_posts(monkeypatch,
assert result is True
api_adapter.handle_message.assert_not_awaited()
- assert posts == [
- {"text": "[SYSTEM: subagent finished]", "session_id": "raw-hq-session-id"}
- ]
+ # Same presentation contract as the push path: leading SYSTEM prefix intact, machine-origin
+ # footer appended — this text becomes a role=user turn on the stateless surface too.
+ assert len(posts) == 1
+ assert posts[0]["session_id"] == "raw-hq-session-id"
+ assert posts[0]["text"].startswith("[SYSTEM: subagent finished]")
+ assert posts[0]["text"].rstrip().endswith(INTERNAL_NOTIFICATION_FOOTER)
@pytest.mark.asyncio
@@ -718,6 +726,8 @@ async def test_async_delegation_apiserver_persists_delivery_not_self_post(
assert len(persisted) == 1
assert persisted[0]["session_id"] == "raw-hq-session-id"
assert persisted[0]["evt"]["delegation_id"] == "deleg_85957"
+ # Persist-only route stays verbatim: the client reads this row, no model turn is woken.
+ assert persisted[0]["text"] == "[ASYNC DELEGATION BATCH COMPLETE — deleg_85957]"
@pytest.mark.asyncio