From 4c5815ebcaf4ddb1b198ebfc0068d04da0d930d8 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sun, 11 Oct 2026 11:41:50 -0700 Subject: [PATCH] refactor(gateway): lift watch-notification metadata into a helper (merge #43 CC) Both sides of merge #43 added metadata branches to _inject_watch_notification (the branch: automation identities + local completion card; main: proven_session_id + completion_ids), which put it at CC 22 over the 20 cap. The metadata block moves verbatim into the pure module-level _watch_notification_metadata(evt, source). No behaviour change; 432 notification/completion/watch gateway tests pass. --- gateway/run_notifications.py | 55 ++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py index c7f7ac7604..2e8848fb0f 100644 --- a/gateway/run_notifications.py +++ b/gateway/run_notifications.py @@ -106,6 +106,36 @@ def _raw_process_event_session_id(evt: dict) -> str: return str(evt.get("origin_session_id") or session_key or "").strip() +def _watch_notification_metadata(evt: dict, source) -> dict[str, Any]: + """Synthetic-event metadata for a watch/completion notification: routing pins, proof ids, + automation identities, and the local async-result card.""" + metadata: dict[str, Any] = {"notification_origin": "process_registry_synthetic"} + session_key = str(evt.get("session_key") or "").strip() + from agent.notification_presentation import diagnostic_process_event + if diagnostic_process_event(evt): + metadata["notification_category"] = "diagnostic" + if session_key.startswith("agent:"): + metadata["gateway_session_key"] = session_key + parent_session_id = str(evt.get("parent_session_id") or "").strip() + if parent_session_id: + metadata["gateway_session_id"] = parent_session_id + if evt.get('_automation_identities'): + metadata['automation_identities'] = evt['_automation_identities'] + if source.platform == Platform.LOCAL and evt.get("type", "completion") == "completion": + # A local completion persists as the async-result card the in-process TUI wrote; + # messaging rows stay internal_notification, as on main. + from tools.process_registry_notifications import ( + PROCESS_COMPLETE_DISPLAY_KIND, process_completion_display_text) + metadata["display_kind"] = PROCESS_COMPLETE_DISPLAY_KIND + metadata["display_text"] = evt.get("_display_text") or process_completion_display_text([evt]) + proven_session_id = str(evt.get("proven_session_id") or "").strip() + if proven_session_id: + metadata["gateway_proven_session_id"] = proven_session_id + if completion_ids := evt.get("completion_ids"): + metadata["gateway_completion_ids"] = list(completion_ids) + return metadata + + class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotificationOwnershipMixin): """Process/completion/update notifications, media delivery and async-delegation delivery for GatewayRunner.""" @@ -827,31 +857,8 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotifica 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: dict[str, Any] = {"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): - metadata["notification_category"] = "diagnostic" - if session_key.startswith("agent:"): - metadata["gateway_session_key"] = session_key - parent_session_id = str(evt.get("parent_session_id") or "").strip() - if parent_session_id: - metadata["gateway_session_id"] = parent_session_id - if evt.get('_automation_identities'): - metadata['automation_identities'] = evt['_automation_identities'] - if source.platform == Platform.LOCAL and evt.get("type", "completion") == "completion": - # A local completion persists as the async-result card the in-process TUI wrote; - # messaging rows stay internal_notification, as on main. - from tools.process_registry_notifications import ( - PROCESS_COMPLETE_DISPLAY_KIND, process_completion_display_text) - metadata["display_kind"] = PROCESS_COMPLETE_DISPLAY_KIND - metadata["display_text"] = evt.get("_display_text") or process_completion_display_text([evt]) - proven_session_id = str(evt.get("proven_session_id") or "").strip() - if proven_session_id: - metadata["gateway_proven_session_id"] = proven_session_id - if completion_ids := evt.get("completion_ids"): - metadata["gateway_completion_ids"] = list(completion_ids) + metadata = _watch_notification_metadata(evt, source) # 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