The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): stop anchoring synthetic process notifications to the stale trigger message (#123097)
brooklyn!34343e79ab60
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.pyindex 90b46b3b369..ba01812b7a4 100644--- a/gateway/run_notifications.py+++ b/gateway/run_notifications.py@@ -1331,9 +1331,23 @@ class GatewayNotificationsMixin:parent_session_id = str(evt.get("parent_session_id") or "").strip()if parent_session_id:metadata["gateway_session_id"] = parent_session_id+ # The queued event's ``message_id`` is the message that STARTED the process, and the+ # persisted origin carries the same stale id. A synthetic completion is not a reply to+ # it: by delivery time the user has often continued elsewhere, and an event anchored+ # there makes the finished job's reply quote that old message on every reply-anchoring+ # platform (#52694: a background completion visibly replying to a stale Discord DM+ # message). Routing is unaffected — topic lanes carry thread_id and the anchor-less+ # synthetic-send branches are covered (#87051); the original id rides metadata for+ # debugging only.+ trigger_message_id = str(evt.get("message_id") or "").strip() or None+ if trigger_message_id:+ metadata["original_trigger_message_id"] = trigger_message_id+ if getattr(source, "message_id", None):+ from gateway.session_identity import replace_source+ source = replace_source(source, message_id=None)synth_event = MessageEvent(text=synth_text, message_type=MessageType.TEXT, source=source, internal=True,- message_id=str(evt.get("message_id") or "").strip() or None, metadata=metadata,+ metadata=metadata,)logger.info("Watch pattern notification — injecting for %s chat=%s thread=%s",diff --git a/tests/gateway/test_background_process_notifications.py b/tests/gateway/test_background_process_notifications.pyindex cf32f16c8a0..9dc7d3e355b 100644--- a/tests/gateway/test_background_process_notifications.py+++ b/tests/gateway/test_background_process_notifications.py@@ -323,7 +323,17 @@ async def test_post_turn_watch_drain_all_injects_from_queued_event_origin(monkey@pytest.mark.asyncio-async def test_inject_watch_notification_carries_message_id_reply_anchor(monkeypatch, tmp_path):+async def test_inject_watch_notification_drops_stale_trigger_reply_anchor(monkeypatch, tmp_path):+ """Regression for #52694: the synthetic watch event must not reuse the triggering+ message id as its reply anchor.++ The id names the message that STARTED the process; by the time the process exits the+ user has often moved on, and a reply quoting that id answers a stale message (a finished+ background job visibly replying to an old Discord DM message from another topic).+ Routing stays on the persisted origin — Telegram DM-topic lanes route anchor-less+ synthetic sends through the topic's message_thread_id (#87051) — and the original id+ survives only as event metadata for debugging.+ """from gateway.session import SessionSourcerunner = _build_runner(monkeypatch, tmp_path, "all")@@ -336,6 +346,7 @@ async def test_inject_watch_notification_carries_message_id_reply_anchor(monkeypthread_id="24296",user_id="1",user_name="Fabio",+ message_id="777", # persisted origin: an equally stale triggering id))@@ -349,8 +360,17 @@ async def test_inject_watch_notification_carries_message_id_reply_anchor(monkeypadapter.handle_message.assert_awaited_once()synth_event = adapter.handle_message.await_args.args[0]- assert synth_event.message_id == "777"+ assert synth_event.internal is True+ # No stale anchor on the event OR the restored origin it routes through.+ assert synth_event.message_id is None+ assert synth_event.source.message_id is None+ # Routing provenance is untouched: the notification still lands in the origin topic.assert synth_event.source.thread_id == "24296"+ # The derived final-reply anchor is empty on every platform branch.+ from gateway.platforms.base import _reply_anchor_for_event+ 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"@pytest.mark.asyncio