The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): unify update-watcher/home-channel/completion-claim helpers in run_notifications (2098→1732 LOC)
Teknium76e474d80b50
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.pyindex a20a93a08c4..c527c532043 100644--- a/gateway/run_notifications.py+++ b/gateway/run_notifications.py@@ -1,4 +1,4 @@-"""Process/completion/update notifications, media delivery and async-delegation delivery methods for GatewayRunner.+"""Process/completion/update notifications, media delivery and async-delegation delivery for GatewayRunner.Split out of ``gateway/run.py``; bound onto ``GatewayRunner`` via the MRO.``gateway.run`` internals are imported lazily inside method bodies (import cycle),@@ -7,17 +7,18 @@ so ``patch("gateway.run.X")`` keeps intercepting them at call time.from __future__ import annotations-import logging-from typing import TYPE_CHECKINGimport asyncio+import dataclassesimport json+import loggingimport timefrom contextlib import suppress+from pathlib import Path+from typing import TYPE_CHECKING, Any, Dict, Optional, cast+from gateway.config import Platform, _BUILTIN_PLATFORM_VALUESfrom gateway.platforms.base import MessageEvent, MessageTypefrom gateway.session import SessionEntry, SessionSource-from pathlib import Path-from typing import Any, Dict, Optional, castif TYPE_CHECKING: # string annotations only; never imported at runtime (cycle)from gateway.run import GatewayRunner, TurnRunner # noqa: F401@@ -25,9 +26,64 @@ if TYPE_CHECKING: # string annotations only; never imported at runtime (cycle)# Log-record parity with the origin module.logger = logging.getLogger("gateway.run")+_VIDEO_EXTS = {'.mp4', '.mov', '.avi', '.mkv', '.webm', '.3gp'}+_IMAGE_EXTS = {'.jpg', '.jpeg', '.png', '.webp', '.gif'}++# Durable async-delegation claim transitions: kind -> (tools.async_delegation function, failure log).+_DURABLE_CLAIM_OPS = {+ "drop": ("drop_completion_delivery", "Could not drop durable completion claim"),+ "release": ("release_completion_delivery", "Could not release durable completion claim"),+}+class GatewayNotificationsMixin:- """Process/completion/update notifications, media delivery and async-delegation delivery methods for GatewayRunner."""+ """Process/completion/update notifications, media delivery and async-delegation delivery for GatewayRunner."""++ # Routing-complete coalescing keys: process completions (short-window fan-in) and async+ # delegations (originating session + parent session + route).+ _COMPLETION_BATCH_KEY_FIELDS = ("session_key", "platform", "chat_type", "chat_id", "thread_id", "user_id")+ _ASYNC_GROUP_KEY_FIELDS = ("session_key", "parent_session_id", *_COMPLETION_BATCH_KEY_FIELDS[1:])++ @dataclasses.dataclass+ class _UpdatePaths:+ """Marker files ``hermes update --gateway`` and its watcher exchange under HERMES_HOME."""++ pending: Path+ claimed: Path+ output: Path+ exit_code: Path+ prompt: Path+ response: Path++ def any_pending(self) -> bool:+ return self.pending.exists() or self.claimed.exists()++ def unlink_all(self) -> None:+ for p in (self.pending, self.claimed, self.output, self.exit_code, self.prompt, self.response):+ p.unlink(missing_ok=True)++ @dataclasses.dataclass+ class _UpdateTarget:+ """Resolved delivery target for update watcher messages."""++ adapter: Any+ chat_id: Any+ session_key: Optional[str]+ metadata: Any+ platform: Any++ def send_metadata(self):+ from gateway.run import _non_conversational_metadata+ return _non_conversational_metadata(self.metadata, platform=self.platform)++ @dataclasses.dataclass+ class _CompletionClaim:+ """Pre-flight outcome for one completion delivery."""++ delegation_id: str = ""+ claim_id: str = ""+ proceed: bool = True+ early_result: Optional[bool] = Noneasync def _deliver_platform_notice(self, source, content: str) -> None:"""Deliver a setup/operational notice using platform-specific privacy rules."""@@ -37,15 +93,9 @@ class GatewayNotificationsMixin:returnconfig = getattr(self, "config", None)- if (- config- and getattr(source, "platform", None) == Platform.SLACK- and _is_slack_ignored_channel(config, getattr(source, "chat_id", None))- ):- logger.info(- "Skipping Slack platform notice for configured ignored channel %s",- getattr(source, "chat_id", None),- )+ chat_id = getattr(source, "chat_id", None)+ if config and getattr(source, "platform", None) == Platform.SLACK and _is_slack_ignored_channel(config, chat_id):+ logger.info("Skipping Slack platform notice for configured ignored channel %s", chat_id)returnnotice_delivery = "public"@@ -55,27 +105,74 @@ class GatewayNotificationsMixin:metadata = self._thread_metadata_for_source(source)if notice_delivery == "private" and getattr(source, "user_id", None):try:- result = await adapter.send_private_notice(- source.chat_id,- source.user_id,- content,- metadata=metadata,- )+ result = await adapter.send_private_notice(source.chat_id, source.user_id, content, metadata=metadata)if getattr(result, "success", False):returnexcept Exception:logger.debug("[%s] send_private_notice failed, falling back to public",- getattr(source, "platform", "?"),- exc_info=True,+ getattr(source, "platform", "?"), exc_info=True,)await adapter.send(source.chat_id, content, metadata=metadata)+ async def _resolve_compression_lineage_target(+ self, session_db: Any, session_entry: SessionEntry, pinned_session_id: str,+ ) -> Optional[str]:+ """Return the live compression tip of ``pinned_session_id`` if the route owns that lineage, else None."""+ try:+ target_session_id = await session_db.get_compression_tip(pinned_session_id)+ except Exception:+ logger.debug("Async-delegation compression-tip lookup failed for %s", pinned_session_id, exc_info=True)+ target_session_id = None++ if not target_session_id or target_session_id == pinned_session_id:+ logger.warning(+ "Async-delegation completion pinned to compressed session %s "+ "without a continuation; dropping injection.",+ pinned_session_id,+ )+ return None++ try:+ tip_row = await session_db.get_session(target_session_id)+ except Exception:+ tip_row = None+ if tip_row is None or tip_row.get("ended_at"):+ logger.warning(+ "Async-delegation compression continuation %s is %s; dropping injection.",+ target_session_id, "unknown" if tip_row is None else "ended",+ )+ return None++ route_owns_lineage = session_entry.session_id in {pinned_session_id, target_session_id}+ if not route_owns_lineage:+ # A delegation may survive several compression rotations: accept an intermediate stale+ # route only when its own verified compression tip is the same live target.+ try:+ route_row = await session_db.get_session(session_entry.session_id)+ route_tip = (+ await session_db.get_compression_tip(session_entry.session_id)+ if route_row is not None+ and route_row.get("ended_at")+ and route_row.get("end_reason") == "compression"+ else None+ )+ except Exception:+ route_tip = None+ route_owns_lineage = route_tip == target_session_id++ if not route_owns_lineage:+ logger.warning(+ "Async-delegation completion for compression lineage %s -> %s "+ "does not own current route %s; dropping injection.",+ pinned_session_id, target_session_id, session_entry.session_id,+ )+ return None+ return target_session_id+async def _resolve_async_delegation_session(- self,- session_entry: SessionEntry,- pinned_session_id: str,+ self, session_entry: SessionEntry, pinned_session_id: str,) -> Optional[SessionEntry]:"""Resolve an async completion to its verified owning gateway session.@@ -96,11 +193,7 @@ class GatewayNotificationsMixin:try:pinned_row = await session_db.get_session(pinned_session_id)except Exception:- logger.debug(- "Async-delegation parent lookup failed for %s",- pinned_session_id,- exc_info=True,- )+ logger.debug("Async-delegation parent lookup failed for %s", pinned_session_id, exc_info=True)if pinned_row is None:logger.warning(@@ -119,8 +212,7 @@ class GatewayNotificationsMixin:"Async-delegation completion pinned to user-closed session %s ""(end_reason=%r); dropping injection instead of resurrecting it ""(#55578 fail-closed).",- pinned_session_id,- _end_reason,+ pinned_session_id, _end_reason,)return Noneif _end_reason != "compression":@@ -130,75 +222,15 @@ class GatewayNotificationsMixin:logger.info("Async-delegation completion pinned to %s-ended session %s; ""retargeting to the chat's current session %s.",- _end_reason or "idle",- pinned_session_id,- session_entry.session_id,+ _end_reason or "idle", pinned_session_id, session_entry.session_id,)return session_entryfollows_compression = True- try:- target_session_id = await session_db.get_compression_tip(- pinned_session_id- )- except Exception:- logger.debug(- "Async-delegation compression-tip lookup failed for %s",- pinned_session_id,- exc_info=True,- )- target_session_id = None-- if not target_session_id or target_session_id == pinned_session_id:- logger.warning(- "Async-delegation completion pinned to compressed session %s "- "without a continuation; dropping injection.",- pinned_session_id,- )- return None-- try:- tip_row = await session_db.get_session(target_session_id)- except Exception:- tip_row = None- if tip_row is None or tip_row.get("ended_at"):- logger.warning(- "Async-delegation compression continuation %s is %s; "- "dropping injection.",- target_session_id,- "unknown" if tip_row is None else "ended",- )- return None-- route_owns_lineage = session_entry.session_id in {- pinned_session_id,- target_session_id,- }- if not route_owns_lineage:- # A long-running delegation may survive multiple compression- # rotations. Accept an intermediate stale route only when its- # own verified compression tip is the same live target.- try:- route_row = await session_db.get_session(session_entry.session_id)- route_tip = (- await session_db.get_compression_tip(session_entry.session_id)- if route_row is not None- and route_row.get("ended_at")- and route_row.get("end_reason") == "compression"- else None- )- except Exception:- route_tip = None- route_owns_lineage = route_tip == target_session_id-- if not route_owns_lineage:- logger.warning(- "Async-delegation completion for compression lineage %s -> %s "- "does not own current route %s; dropping injection.",- pinned_session_id,- target_session_id,- session_entry.session_id,- )+ target_session_id = await self._resolve_compression_lineage_target(+ session_db, session_entry, pinned_session_id,+ )+ if target_session_id is None:return Noneif target_session_id == session_entry.session_id:@@ -207,30 +239,22 @@ class GatewayNotificationsMixin:prior_session_id = session_entry.session_idif follows_compression:switched = await self.async_session_store.advance_compression_session(- session_entry.session_key,- prior_session_id,- target_session_id,+ session_entry.session_key, prior_session_id, target_session_id,)else:- switched = await self.async_session_store.switch_session(- session_entry.session_key,- target_session_id,- )+ switched = await self.async_session_store.switch_session(session_entry.session_key, target_session_id)if switched is None:logger.warning("Async-delegation completion could not bind routing key %s to ""owning session %s; dropping injection.",- session_entry.session_key,- target_session_id,+ session_entry.session_key, target_session_id,)return Nonelogger.info("Pinned async-delegation completion to owning session %s ""(was %s) for routing key %s (#57498)",- target_session_id,- prior_session_id,- session_entry.session_key,+ target_session_id, prior_session_id, session_entry.session_key,)return switched@@ -249,48 +273,39 @@ class GatewayNotificationsMixin:streamed reply was either shown as text or is stale inspected content, and promoting itsent files the model never asked to deliver."""- from pathlib import Pathfrom urllib.parse import quote as _quotetry:- # Capture [[as_document]] before extract_media strips it, so the dispatch partition- # below can route image-extension files through send_document (preserving bytes) instead- # of send_multiple_images (Telegram sendPhoto recompresses to ~1280px).+ # Capture [[as_document]] before extract_media strips it: image-extension files then route+ # through send_document (preserving bytes) instead of send_multiple_images (Telegram+ # sendPhoto recompresses to ~1280px).force_document_attachments = "[[as_document]]" in responsefrom gateway.platforms.base import BasePlatformAdapter, should_send_media_as_audiomedia_files, cleaned = adapter.extract_media(response)media_files = BasePlatformAdapter.filter_media_delivery_paths(media_files)- # Do NOT deduplicate explicit MEDIA tags against prior turns here. This rescan is- # already EXPLICIT-ONLY (see docstring): a MEDIA: directive in the final streamed reply- # is the model deliberately attaching a file — including a user-requested resend. Stale- # auto-appended tags are deduped upstream (_collect_auto_append_media_tags). Strip image- # URLs for parity with the non-streaming chain, but do NOT run extract_local_files here.+ # Do NOT deduplicate explicit MEDIA tags against prior turns here: a MEDIA: directive in+ # the final streamed reply is the model deliberately attaching a file (including a+ # user-requested resend). Stale auto-appended tags are deduped upstream+ # (_collect_auto_append_media_tags). Strip image URLs for parity with the non-streaming+ # chain, but do NOT run extract_local_files here.adapter.extract_images(cleaned)_thread_meta = (dict(thread_metadata)if thread_metadata is not None- else self._thread_metadata_for_source(- event.source,- self._reply_anchor_for_event(event),- )+ else self._thread_metadata_for_source(event.source, self._reply_anchor_for_event(event)))-- _VIDEO_EXTS = {'.mp4', '.mov', '.avi', '.mkv', '.webm', '.3gp'}- _IMAGE_EXTS = {'.jpg', '.jpeg', '.png', '.webp', '.gif'}+ chat_id = event.source.chat_id# Partition out images so they can be sent as a single batch (e.g. Signal's multi-- # attachment RPC). When [[as_document]] was set, image-extension files skip the photo- # path and route to send_document below — preserving original bytes.+ # attachment RPC); with [[as_document]] set they fall through to send_document.image_paths: list = []non_image_media: list = []for media_path, is_voice in media_files:ext = Path(media_path).suffix.lower()- if (ext in _IMAGE_EXTS- and not is_voice- and not force_document_attachments):+ if ext in _IMAGE_EXTS and not is_voice and not force_document_attachments:image_paths.append(media_path)else:non_image_media.append((media_path, is_voice))@@ -298,11 +313,7 @@ class GatewayNotificationsMixin:if image_paths:try:images = [(f"file://{_quote(p)}", "") for p in image_paths]- await adapter.send_multiple_images(- chat_id=event.source.chat_id,- images=images,- metadata=_thread_meta,- )+ await adapter.send_multiple_images(chat_id=chat_id, images=images, metadata=_thread_meta)except Exception as e:logger.warning("[%s] Post-stream image batch delivery failed: %s", adapter.name, e)@@ -311,23 +322,12 @@ class GatewayNotificationsMixin:ext = Path(media_path).suffix.lower()if should_send_media_as_audio(event.source.platform, ext, is_voice=is_voice):await adapter.send_voice(- chat_id=event.source.chat_id,- audio_path=media_path,- metadata=_thread_meta,- is_voice=is_voice,+ chat_id=chat_id, audio_path=media_path, metadata=_thread_meta, is_voice=is_voice,)elif ext in _VIDEO_EXTS:- await adapter.send_video(- chat_id=event.source.chat_id,- video_path=media_path,- metadata=_thread_meta,- )+ await adapter.send_video(chat_id=chat_id, video_path=media_path, metadata=_thread_meta)else:- await adapter.send_document(- chat_id=event.source.chat_id,- file_path=media_path,- metadata=_thread_meta,- )+ await adapter.send_document(chat_id=chat_id, file_path=media_path, metadata=_thread_meta)except Exception as e:logger.warning("[%s] Post-stream media delivery failed: %s", adapter.name, e)@@ -350,10 +350,9 @@ class GatewayNotificationsMixin:if not text_already_delivered:text_content = _strip_response_attachments_for_direct_send(response, adapter)if text_content:- # Reconcile-by-edit first (live finding, 2026-08-16 canary): when the stream- # consumer delivered/sealed a message but its recorded payload didn't confirm the- # final (post-stream mutation), plain-sending here creates the duplicate — the- # sealed message already carries most of the answer.+ # Reconcile-by-edit first: when the stream consumer delivered/sealed a message whose+ # recorded payload didn't confirm the final (post-stream mutation), a plain send here+ # would duplicate it — the sealed message already carries most of the answer._reconciled = False_sc_msg_id = getattr(stream_consumer, "message_id", None)if (@@ -363,10 +362,7 @@ class GatewayNotificationsMixin:):try:_edit_res = await adapter.edit_message(- chat_id=source.chat_id,- message_id=_sc_msg_id,- content=text_content,- finalize=True,+ chat_id=source.chat_id, message_id=_sc_msg_id, content=text_content, finalize=True,)if getattr(_edit_res, "success", False):_reconciled = True@@ -375,16 +371,9 @@ class GatewayNotificationsMixin:_sc_msg_id,)except Exception as _qe:- logger.debug(- "Queued-lane reconcile edit failed (%s); falling back to send.",- _qe,- )+ logger.debug("Queued-lane reconcile edit failed (%s); falling back to send.", _qe)if not _reconciled:- await adapter.send(- source.chat_id,- text_content,- metadata=metadata,- )+ await adapter.send(source.chat_id, text_content, metadata=metadata)# Failed turns still deliver their (normalized failure) text above, but must not upload# attachments as if the turn succeeded — mirrors the ``not agent_result.get("failed")``@@ -392,17 +381,8 @@ class GatewayNotificationsMixin:if not deliver_media:return- synthetic_event = MessageEvent(- text="",- source=source,- message_id=event_message_id,- )- await self._deliver_media_from_response(- response,- synthetic_event,- adapter,- thread_metadata=metadata,- )+ synthetic_event = MessageEvent(text="", source=source, message_id=event_message_id)+ await self._deliver_media_from_response(response, synthetic_event, adapter, thread_metadata=metadata)def _schedule_update_notification_watch(self) -> None:"""Ensure a background task is watching for update completion."""@@ -411,12 +391,127 @@ class GatewayNotificationsMixin:returntry:- self._update_notification_task = asyncio.create_task(- self._watch_update_progress()- )+ self._update_notification_task = asyncio.create_task(self._watch_update_progress())except RuntimeError:logger.debug("Skipping update notification watcher: no running event loop")+ @classmethod+ def _update_paths(cls) -> "GatewayNotificationsMixin._UpdatePaths":+ from gateway.run import _hermes_home+ return cls._UpdatePaths(+ pending=_hermes_home / ".update_pending.json",+ claimed=_hermes_home / ".update_pending.claimed.json",+ output=_hermes_home / ".update_output.txt",+ exit_code=_hermes_home / ".update_exit_code",+ prompt=_hermes_home / ".update_prompt.json",+ response=_hermes_home / ".update_response",+ )++ def _resolve_update_target(self, paths: "_UpdatePaths") -> Optional["_UpdateTarget"]:+ """Resolve adapter/chat/session for update watcher messages from the pending marker."""+ for path in (paths.claimed, paths.pending):+ if not path.exists():+ continue+ try:+ pending = json.loads(path.read_text(encoding="utf-8"))+ platform_str = pending.get("platform")+ chat_id = pending.get("chat_id")+ session_key = pending.get("session_key")+ if not (platform_str and chat_id):+ return None+ platform = Platform(platform_str)+ adapter = self.adapters.get(platform)+ metadata = self._thread_metadata_for_target(+ platform, chat_id, pending.get("thread_id"),+ chat_type=pending.get("chat_type"),+ reply_to_message_id=pending.get("message_id"),+ adapter=adapter,+ )+ if not adapter:+ return None+ # Fallback session key if not stored (old pending files)+ return self._UpdateTarget(+ adapter, chat_id, session_key or f"{platform_str}:{chat_id}", metadata, platform,+ )+ except Exception:+ pass+ return None++ async def _watch_update_completion_only(self, paths: "_UpdatePaths", deadline: float, poll_interval: float) -> None:+ """Fallback when no adapter/chat can be resolved: wait for the exit code, then notify."""+ logger.warning("Update watcher: cannot resolve adapter/chat_id, falling back to completion-only")+ # Keep polling until _send_update_notification actually delivers (True) — it re-resolves the+ # adapter each call and returns False (markers kept) while the platform is still reconnecting.+ loop = asyncio.get_running_loop()+ while paths.any_pending() and loop.time() < deadline:+ if paths.exit_code.exists() and await self._send_update_notification():+ return+ await asyncio.sleep(poll_interval)+ if paths.any_pending() and not paths.exit_code.exists():+ paths.exit_code.write_text("124", encoding="utf-8")+ await self._send_update_notification()++ @staticmethod+ def _read_update_output_since(path: Path, offset: int) -> tuple[str, int]:+ """Read update output defensively; logs may contain invalid UTF-8."""+ try:+ data = path.read_bytes()+ except OSError:+ return "", offset+ if len(data) <= offset:+ return "", len(data)+ return data[offset:].decode("utf-8", errors="replace"), len(data)++ async def _send_update_output(self, target: "_UpdateTarget", text: str) -> None:+ """Send buffered update output as fenced chunks that fit message limits (Telegram: 4096)."""+ from tools.ansi_strip import strip_ansi+ clean = strip_ansi(text).strip()+ if not clean:+ return+ max_chunk = 3500+ for i in range(0, len(clean), max_chunk):+ try:+ await target.adapter.send(+ target.chat_id, f"```\n{clean[i:i + max_chunk]}\n```", metadata=target.send_metadata(),+ )+ except Exception as e:+ logger.debug("Update stream send failed: %s", e)++ async def _forward_update_prompt(self, target: "_UpdateTarget", prompt_text: str, default: str) -> None:+ """Forward an update prompt: platform-native buttons first (Discord, Telegram), else text."""+ sent_buttons = False+ adapter = target.adapter+ if getattr(type(adapter), "send_update_prompt", None) is not None:+ try:+ await adapter.send_update_prompt(+ chat_id=target.chat_id, prompt=prompt_text, default=default,+ session_key=target.session_key, metadata=target.send_metadata(),+ )+ sent_buttons = True+ except Exception as btn_err:+ logger.debug("Button-based update prompt failed: %s", btn_err)+ if not sent_buttons:+ default_hint = f" (default: {default})" if default else ""+ _p = getattr(adapter, "typed_command_prefix", "/")+ await adapter.send(+ target.chat_id,+ f"⚕ **Update needs your input:**\n\n"+ f"{prompt_text}{default_hint}\n\n"+ f"Reply `{_p}approve` (yes) or `{_p}deny` (no), "+ f"or type your answer directly.",+ metadata=target.send_metadata(),+ )+ # Keep the prompt marker on disk until the user answers so a watcher after a mid-prompt+ # gateway restart can recover by re-forwarding it.+ self._session_state(target.session_key).persistent.update_prompt_pending = True+ logger.info("Forwarded update prompt to %s: %s", target.session_key, prompt_text[:80])++ def _clear_update_markers(self, paths: "_UpdatePaths", session_key: Optional[str]) -> None:+ paths.unlink_all()+ state = self._peek_session_state(session_key)+ if state is not None:+ state.persistent.update_prompt_pending = False+async def _watch_update_progress(self,poll_interval: float = 2.0,@@ -428,345 +523,162 @@ class GatewayNotificationsMixin:Polls ``.update_output.txt`` for new content and sends chunks to the user periodically;detects ``.update_prompt.json`` (written when the update process needs input) and forwards it."""- from gateway.run import _hermes_home, _non_conversational_metadata- pending_path = _hermes_home / ".update_pending.json"- claimed_path = _hermes_home / ".update_pending.claimed.json"- output_path = _hermes_home / ".update_output.txt"- exit_code_path = _hermes_home / ".update_exit_code"- prompt_path = _hermes_home / ".update_prompt.json"-+ paths = self._update_paths()loop = asyncio.get_running_loop()deadline = loop.time() + timeout- # Resolve the adapter and chat_id for sending messages- adapter = None- chat_id = None- session_key = None- metadata = None- for path in (claimed_path, pending_path):- if path.exists():- try:- pending = json.loads(path.read_text(encoding="utf-8"))- platform_str = pending.get("platform")- chat_id = pending.get("chat_id")- chat_type = pending.get("chat_type")- session_key = pending.get("session_key")- thread_id = pending.get("thread_id")- message_id = pending.get("message_id")- if platform_str and chat_id:- platform = Platform(platform_str)- adapter = self.adapters.get(platform)- metadata = self._thread_metadata_for_target(- platform,- chat_id,- thread_id,- chat_type=chat_type,- reply_to_message_id=message_id,- adapter=adapter,- )- # Fallback session key if not stored (old pending files)- if not session_key:- session_key = f"{platform_str}:{chat_id}"- break- except Exception:- pass-- if not adapter or not chat_id:- logger.warning("Update watcher: cannot resolve adapter/chat_id, falling back to completion-only")- # Completion-only fallback: wait for the exit code, then keep polling until- # _send_update_notification actually delivers (True) — it re-resolves the adapter each- # call and returns False (markers kept) while the platform is still reconnecting.- while (pending_path.exists() or claimed_path.exists()) and loop.time() < deadline:- if exit_code_path.exists() and await self._send_update_notification():- return- await asyncio.sleep(poll_interval)- if (pending_path.exists() or claimed_path.exists()) and not exit_code_path.exists():- exit_code_path.write_text("124", encoding="utf-8")- await self._send_update_notification()+ target = self._resolve_update_target(paths)+ if target is None:+ await self._watch_update_completion_only(paths, deadline, poll_interval)return-- def _strip_ansi(text: str) -> str:- from tools.ansi_strip import strip_ansi- return strip_ansi(text)-- def _read_output_since(path: Path, offset: int) -> tuple[str, int]:- """Read update output defensively; logs may contain invalid UTF-8."""- try:- data = path.read_bytes()- except OSError:- return "", offset- if len(data) <= offset:- return "", len(data)- return data[offset:].decode("utf-8", errors="replace"), len(data)+ session_key = target.session_keybytes_sent = 0last_stream_time = loop.time()buffer = ""async def _flush_buffer() -> None:- """Send buffered output to the user."""nonlocal buffer, last_stream_time- if not buffer.strip():- buffer = ""- return- # Chunk to fit message limits (Telegram: 4096, others: generous)- clean = _strip_ansi(buffer).strip()- buffer = ""- last_stream_time = loop.time()- if not clean:- return- # Split into chunks if too long- max_chunk = 3500- chunks = [clean[i:i + max_chunk] for i in range(0, len(clean), max_chunk)]- for chunk in chunks:+ text, buffer = buffer, ""+ if text.strip():+ last_stream_time = loop.time()+ await self._send_update_output(target, text)++ def _read_new_output() -> None:+ nonlocal buffer, bytes_sent+ if paths.output.exists():try:- await adapter.send(- chat_id,- f"```\n{chunk}\n```",- metadata=_non_conversational_metadata(metadata, platform=platform),- )- except Exception as e:- logger.debug("Update stream send failed: %s", e)+ chunk, bytes_sent = self._read_update_output_since(paths.output, bytes_sent)+ buffer += chunk+ except OSError:+ passwhile loop.time() < deadline:- # Check for completion- if exit_code_path.exists():- # Read any remaining output- if output_path.exists():- try:- chunk, bytes_sent = _read_output_since(output_path, bytes_sent)- if chunk:- buffer += chunk- except OSError:- pass+ if paths.exit_code.exists():+ _read_new_output()await _flush_buffer()-- # Send final statustry:- exit_code_raw = exit_code_path.read_text(encoding="utf-8").strip() or "1"- exit_code = int(exit_code_raw)- if exit_code == 0:- await adapter.send(- chat_id,- "✅ Hermes update finished.",- metadata=_non_conversational_metadata(metadata, platform=platform),- )- else:- await adapter.send(- chat_id,- "❌ Hermes update failed (exit code {}).".format(exit_code),- metadata=_non_conversational_metadata(metadata, platform=platform),- )+ exit_code = int(paths.exit_code.read_text(encoding="utf-8").strip() or "1")+ await target.adapter.send(+ target.chat_id,+ "✅ Hermes update finished." if exit_code == 0+ else "❌ Hermes update failed (exit code {}).".format(exit_code),+ metadata=target.send_metadata(),+ )logger.info("Update finished (exit=%s), notified %s", exit_code, session_key)except Exception as e:logger.warning("Update final notification failed: %s", e)-- # Cleanup- for p in (pending_path, claimed_path, output_path,- exit_code_path, prompt_path):- p.unlink(missing_ok=True)- (_hermes_home / ".update_response").unlink(missing_ok=True)- _up_done = self._peek_session_state(session_key)- if _up_done is not None:- _up_done.persistent.update_prompt_pending = False+ self._clear_update_markers(paths, session_key)return- # Check for new output- if output_path.exists():- try:- chunk, bytes_sent = _read_output_since(output_path, bytes_sent)- if chunk:- buffer += chunk- except OSError:- pass-- # Flush buffer periodically+ _read_new_output()if buffer.strip() and (loop.time() - last_stream_time) >= stream_interval:await _flush_buffer()- # Check for prompts — only forward if we haven't already sent one that's still awaiting- # a response. Without this guard the watcher would re-read the same .update_prompt.json- # every poll cycle and spam the user with duplicate prompt messages.- _up_pending_state = (- self._peek_session_state(session_key) if session_key else None- )- if (prompt_path.exists() and session_key- and not (- _up_pending_state is not None- and _up_pending_state.persistent.update_prompt_pending- )):+ # Forward a prompt only when none is still awaiting a response; otherwise the watcher+ # would re-read the same .update_prompt.json every poll and spam duplicate prompts.+ _pending_state = self._peek_session_state(session_key) if session_key else None+ if (+ paths.prompt.exists() and session_key+ and not (_pending_state is not None and _pending_state.persistent.update_prompt_pending)+ ):try:- prompt_data = json.loads(prompt_path.read_text(encoding="utf-8"))+ prompt_data = json.loads(paths.prompt.read_text(encoding="utf-8"))prompt_text = prompt_data.get("prompt", "")- default = prompt_data.get("default", "")if prompt_text:- # Flush any buffered output first so the user sees- # context before the prompt- await _flush_buffer()- # Try platform-native buttons first (Discord, Telegram)- sent_buttons = False- if getattr(type(adapter), "send_update_prompt", None) is not None:- try:- await adapter.send_update_prompt(- chat_id=chat_id,- prompt=prompt_text,- default=default,- session_key=session_key,- metadata=_non_conversational_metadata(metadata, platform=platform),- )- sent_buttons = True- except Exception as btn_err:- logger.debug("Button-based update prompt failed: %s", btn_err)- if not sent_buttons:- default_hint = f" (default: {default})" if default else ""- _p = getattr(adapter, "typed_command_prefix", "/")- await adapter.send(- chat_id,- f"⚕ **Update needs your input:**\n\n"- f"{prompt_text}{default_hint}\n\n"- f"Reply `{_p}approve` (yes) or `{_p}deny` (no), "- f"or type your answer directly.",- metadata=_non_conversational_metadata(metadata, platform=platform),- )- # Keep the prompt marker on disk until the user answers so a watcher after a- # mid-prompt gateway restart can recover by re-forwarding it.- self._session_state(- session_key- ).persistent.update_prompt_pending = True- # .update_response to continue — it doesn't re-check- logger.info("Forwarded update prompt to %s: %s", session_key, prompt_text[:80])+ await _flush_buffer() # user sees context before the prompt+ await self._forward_update_prompt(target, prompt_text, prompt_data.get("default", ""))except (json.JSONDecodeError, OSError) as e:logger.debug("Failed to read update prompt: %s", e)await asyncio.sleep(poll_interval)- # Timeout- if not exit_code_path.exists():+ if not paths.exit_code.exists():logger.warning("Update watcher timed out after %.0fs", timeout)- exit_code_path.write_text("124", encoding="utf-8")+ paths.exit_code.write_text("124", encoding="utf-8")await _flush_buffer()with suppress(Exception):- await adapter.send(- chat_id,- "❌ Hermes update timed out after 30 minutes.",- metadata=_non_conversational_metadata(metadata, platform=platform),+ await target.adapter.send(+ target.chat_id, "❌ Hermes update timed out after 30 minutes.", metadata=target.send_metadata(),)- for p in (pending_path, claimed_path, output_path,- exit_code_path, prompt_path):- p.unlink(missing_ok=True)- (_hermes_home / ".update_response").unlink(missing_ok=True)- _up_timeout_state = self._peek_session_state(session_key)- if _up_timeout_state is not None:- _up_timeout_state.persistent.update_prompt_pending = False+ self._clear_update_markers(paths, session_key)async def _send_update_notification(self) -> bool:"""If an update finished, notify the user.False while the update is still running (caller may retry); True after a definitive send/skip."""- from gateway.run import _hermes_home, _non_conversational_metadata- pending_path = _hermes_home / ".update_pending.json"- claimed_path = _hermes_home / ".update_pending.claimed.json"- output_path = _hermes_home / ".update_output.txt"- exit_code_path = _hermes_home / ".update_exit_code"-- if not pending_path.exists() and not claimed_path.exists():+ from gateway.run import _non_conversational_metadata+ paths = self._update_paths()+ if not paths.any_pending():return Falsecleanup = True- active_pending_path = claimed_path+ active_pending_path = paths.claimed++ def _defer(reason: str, *args) -> bool:+ nonlocal cleanup, active_pending_path+ logger.info(reason, *args)+ cleanup = False+ active_pending_path = paths.pending+ paths.claimed.replace(paths.pending)+ return False+try:- if pending_path.exists():+ if paths.pending.exists():try:- pending_path.replace(claimed_path)+ paths.pending.replace(paths.claimed)except FileNotFoundError:- if not claimed_path.exists():+ if not paths.claimed.exists():return True- elif not claimed_path.exists():+ elif not paths.claimed.exists():return True- pending = json.loads(claimed_path.read_text(encoding="utf-8"))+ pending = json.loads(paths.claimed.read_text(encoding="utf-8"))platform_str = pending.get("platform")chat_id = pending.get("chat_id")- chat_type = pending.get("chat_type")- thread_id = pending.get("thread_id")- message_id = pending.get("message_id")-- if not exit_code_path.exists():- logger.info("Update notification deferred: update still running")- cleanup = False- active_pending_path = pending_path- claimed_path.replace(pending_path)- return False- exit_code_raw = exit_code_path.read_text(encoding="utf-8").strip() or "1"- exit_code = int(exit_code_raw)+ if not paths.exit_code.exists():+ return _defer("Update notification deferred: update still running")- # Read the captured update output- output = ""- if output_path.exists():- output = output_path.read_bytes().decode("utf-8", errors="replace")+ exit_code = int(paths.exit_code.read_text(encoding="utf-8").strip() or "1")+ output = paths.output.read_bytes().decode("utf-8", errors="replace") if paths.output.exists() else ""- # Resolve adapterplatform = Platform(platform_str)adapter = self.adapters.get(platform)if not adapter and chat_id:# The update finished, but the target platform has not reconnected yet (common right- # after the restart that `hermes update` triggers). Treating "adapter missing" as a- # definitive skip would delete the markers and silently lose the notification;- # preserve them so a later retry (watcher poll or next startup) can deliver it.- logger.info(- "Update notification deferred: %s adapter not connected yet",- platform_str,- )- cleanup = False- active_pending_path = pending_path- claimed_path.replace(pending_path)- return False+ # after the restart that `hermes update` triggers). A definitive skip would delete the+ # markers and silently lose the notification; preserve them for a later retry.+ return _defer("Update notification deferred: %s adapter not connected yet", platform_str)if adapter and chat_id:metadata = self._thread_metadata_for_target(- platform,- chat_id,- thread_id,- chat_type=chat_type,- reply_to_message_id=message_id,+ platform, chat_id, pending.get("thread_id"),+ chat_type=pending.get("chat_type"),+ reply_to_message_id=pending.get("message_id"),adapter=adapter,)- # Strip ANSI escape codes for clean displayfrom tools.ansi_strip import strip_ansioutput = strip_ansi(output).strip()if output:if len(output) > 3500:output = "…" + output[-3500:]- if exit_code == 0:- msg = f"✅ Hermes update finished.\n\n```\n{output}\n```"- else:- msg = f"❌ Hermes update failed.\n\n```\n{output}\n```"+ status = "✅ Hermes update finished." if exit_code == 0 else "❌ Hermes update failed."+ msg = f"{status}\n\n```\n{output}\n```"elif exit_code == 0:msg = "✅ Hermes update finished successfully."else:msg = "❌ Hermes update failed. Check the gateway logs or run `hermes update` manually for details."- await adapter.send(- chat_id,- msg,- metadata=_non_conversational_metadata(metadata, platform=platform),- )- logger.info(- "Sent post-update notification to %s:%s (exit=%s)",- platform_str,- chat_id,- exit_code,- )+ await adapter.send(chat_id, msg, metadata=_non_conversational_metadata(metadata, platform=platform))+ logger.info("Sent post-update notification to %s:%s (exit=%s)", platform_str, chat_id, exit_code)except Exception as e:logger.warning("Post-update notification failed: %s", e)finally:if cleanup:- active_pending_path.unlink(missing_ok=True)- claimed_path.unlink(missing_ok=True)- output_path.unlink(missing_ok=True)- exit_code_path.unlink(missing_ok=True)+ for p in (active_pending_path, paths.claimed, paths.output, paths.exit_code):+ p.unlink(missing_ok=True)return True@@ -781,9 +693,7 @@ class GatewayNotificationsMixin:data = json.loads(notify_path.read_text(encoding="utf-8"))platform_str = data.get("platform")chat_id = data.get("chat_id")- chat_type = data.get("chat_type")thread_id = data.get("thread_id")- message_id = data.get("message_id")if not platform_str or not chat_id:return None@@ -791,57 +701,42 @@ class GatewayNotificationsMixin:platform = Platform(platform_str)transport = resolve_delivery_transport(platform, self.config, self.adapters)if transport is None:- logger.debug(- "Restart notification skipped: no live transport for %s",- platform_str,- )+ logger.debug("Restart notification skipped: no live transport for %s", platform_str)return Noneplatform_cfg = self.config.platforms.get(platform)if platform_cfg is not None and not platform_cfg.gateway_restart_notification:logger.info(- "Restart notification suppressed: %s has gateway_restart_notification=false",- platform_str,+ "Restart notification suppressed: %s has gateway_restart_notification=false", platform_str,)return Nonemetadata = self._thread_metadata_for_target(- platform,- chat_id,- thread_id,- chat_type=chat_type,- reply_to_message_id=message_id,+ platform, chat_id, thread_id,+ chat_type=data.get("chat_type"),+ reply_to_message_id=data.get("message_id"),adapter=transport.adapter,)if data.get("delivered_via_upstream_relay") is True:metadata = dict(metadata or {})- if data.get("user_id"):- metadata["user_id"] = str(data["user_id"])- if data.get("scope_id"):- metadata["scope_id"] = str(data["scope_id"])+ for field in ("user_id", "scope_id"):+ if data.get(field):+ metadata[field] = str(data[field])result = await transport.send(- platform,- str(chat_id),+ platform, str(chat_id),"♻ Gateway restarted successfully. Your session continues.",metadata=_non_conversational_metadata(metadata, platform=platform),)# adapter.send() catches provider errors (e.g. "Chat not found") and returns- # SendResult(success=False) rather than raising, so inspect the result before claiming- # success — otherwise the log line hides real delivery failures.+ # SendResult(success=False) rather than raising, so inspect the result before claiming success.if result is not None and getattr(result, "success", True) is False:logger.warning("Restart notification to %s:%s was not delivered: %s",- platform_str,- chat_id,- getattr(result, "error", "send returned success=False"),+ platform_str, chat_id, getattr(result, "error", "send returned success=False"),)return None- logger.info(- "Sent restart notification to %s:%s",- platform_str,- chat_id,- )+ logger.info("Sent restart notification to %s:%s", platform_str, chat_id)return str(platform_str), str(chat_id), str(thread_id) if thread_id else Noneexcept Exception as e:logger.warning("Restart notification failed: %s", e)@@ -849,6 +744,44 @@ class GatewayNotificationsMixin:finally:notify_path.unlink(missing_ok=True)+ def _home_channel_transports(self):+ """Yield ``(platform, platform_cfg, home, transport)`` for every home channel with a live transport."""+ from gateway.run import resolve_delivery_transport+ for platform, platform_cfg in self.config.platforms.items():+ home = platform_cfg.home_channel+ if not home or not home.chat_id:+ continue+ transport = resolve_delivery_transport(platform, self.config, self.adapters)+ if transport is None:+ continue+ yield platform, platform_cfg, home, transport++ async def _send_home_channel_message(self, platform, home, transport, message: str, failure_fmt: str) -> bool:+ """Best-effort send to one home channel; True on success, failures logged with ``failure_fmt``."""+ from gateway.run import _non_conversational_metadata+ try:+ metadata = self._thread_metadata_for_target(platform, home.chat_id, home.thread_id, adapter=transport.adapter)+ if transport.is_relay:+ metadata = dict(metadata or {})+ if home.user_id:+ metadata["user_id"] = home.user_id+ if home.scope_id:+ metadata["scope_id"] = home.scope_id+ send_metadata = _non_conversational_metadata(metadata, platform=platform)+ if send_metadata is not None or transport.is_relay:+ result = await transport.send(platform, str(home.chat_id), message, metadata=send_metadata)+ else:+ result = await transport.adapter.send(str(home.chat_id), message)+ if result is not None and getattr(result, "success", True) is False:+ logger.warning(+ failure_fmt, platform.value, home.chat_id, getattr(result, "error", "send returned success=False"),+ )+ return False+ return True+ except Exception as exc:+ logger.warning(failure_fmt, platform.value, home.chat_id, exc)+ return False+async def _send_home_channel_startup_notifications(self,*,@@ -856,24 +789,14 @@ class GatewayNotificationsMixin:) -> set[tuple[str, str, Optional[str]]]:"""Notify configured home channels that the gateway is back online.- The notification is best-effort and sent once per connected platform- home channel. ``skip_targets`` lets startup avoid duplicate messages- when a more specific restart notification is queued for the same chat.+ Best-effort, once per connected platform home channel. ``skip_targets`` lets startup avoid+ duplicate messages when a more specific restart notification is queued for the same chat."""- from gateway.run import _non_conversational_metadata, resolve_delivery_transportdelivered: set[tuple[str, str, Optional[str]]] = set()skipped = skip_targets or set()message = "♻️ Gateway online — Hermes is back and ready."- for platform, platform_cfg in self.config.platforms.items():- home = platform_cfg.home_channel- if not home or not home.chat_id:- continue-- transport = resolve_delivery_transport(platform, self.config, self.adapters)- if transport is None:- continue-+ for platform, platform_cfg, home, transport in self._home_channel_transports():if not platform_cfg.gateway_restart_notification:logger.info("Home-channel startup notification suppressed: %s has gateway_restart_notification=false",@@ -885,51 +808,11 @@ class GatewayNotificationsMixin:if target in skipped or target in delivered:continue- try:- metadata = self._thread_metadata_for_target(- platform,- home.chat_id,- home.thread_id,- adapter=transport.adapter,- )- if transport.is_relay:- metadata = dict(metadata or {})- if home.user_id:- metadata["user_id"] = home.user_id- if home.scope_id:- metadata["scope_id"] = home.scope_id- send_metadata = _non_conversational_metadata(metadata, platform=platform)- if send_metadata is not None or transport.is_relay:- result = await transport.send(- platform,- str(home.chat_id),- message,- metadata=send_metadata,- )- else:- result = await transport.adapter.send(str(home.chat_id), message)- if result is not None and getattr(result, "success", True) is False:- logger.warning(- "Home-channel startup notification failed for %s:%s: %s",- platform.value,- home.chat_id,- getattr(result, "error", "send returned success=False"),- )- continue-+ if await self._send_home_channel_message(+ platform, home, transport, message, "Home-channel startup notification failed for %s:%s: %s",+ ):delivered.add(target)- logger.info(- "Sent home-channel startup notification to %s:%s",- platform.value,- home.chat_id,- )- except Exception as exc:- logger.warning(- "Home-channel startup notification failed for %s:%s: %s",- platform.value,- home.chat_id,- exc,- )+ logger.info("Sent home-channel startup notification to %s:%s", platform.value, home.chat_id)return delivered@@ -940,16 +823,13 @@ class GatewayNotificationsMixin:— /resume, /history, and session_search all silently break. Best-effort: failures arelogged, not raised."""- from gateway.run import _non_conversational_metadata, resolve_delivery_transporterror = getattr(self, "_session_db_init_error", None)if not error:returnfrom hermes_state import classify_persistence_error, format_session_db_unavailable- cause = classify_persistence_error(error)- hint = format_session_db_unavailable()- if cause == "corrupt":+ if classify_persistence_error(error) == "corrupt":message = ("⚠️ Session database corruption detected. Messages may not be ""persisted. Recovery options:\n"@@ -962,58 +842,16 @@ class GatewayNotificationsMixin:else:message = (f"⚠️ Session database unavailable — messages may not be persisted. "- f"{hint}\n"+ f"{format_session_db_unavailable()}\n"f"Run `hermes doctor` for diagnostics.")- logger.warning(- "Broadcasting state.db failure warning to home channels: %s", error- )+ logger.warning("Broadcasting state.db failure warning to home channels: %s", error)- for platform, platform_cfg in self.config.platforms.items():- home = platform_cfg.home_channel- if not home or not home.chat_id:- continue- transport = resolve_delivery_transport(platform, self.config, self.adapters)- if transport is None:- continue- try:- metadata = self._thread_metadata_for_target(- platform,- home.chat_id,- home.thread_id,- adapter=transport.adapter,- )- if transport.is_relay:- metadata = dict(metadata or {})- if home.user_id:- metadata["user_id"] = home.user_id- if home.scope_id:- metadata["scope_id"] = home.scope_id- send_metadata = _non_conversational_metadata(metadata, platform=platform)- if send_metadata is not None or transport.is_relay:- result = await transport.send(- platform,- str(home.chat_id),- message,- metadata=send_metadata,- )- else:- result = await transport.adapter.send(str(home.chat_id), message)- if result is not None and getattr(result, "success", True) is False:- logger.warning(- "state.db warning notification failed for %s:%s: %s",- platform.value,- home.chat_id,- getattr(result, "error", "send returned success=False"),- )- except Exception as exc:- logger.warning(- "state.db warning notification failed for %s:%s: %s",- platform.value,- home.chat_id,- exc,- )+ for platform, _platform_cfg, home, transport in self._home_channel_transports():+ await self._send_home_channel_message(+ platform, home, transport, message, "state.db warning notification failed for %s:%s: %s",+ )def _build_process_event_source(self, evt: dict):"""Resolve the canonical source for a synthetic background-process event.@@ -1021,12 +859,9 @@ class GatewayNotificationsMixin:Prefer the persisted session-store origin; the active foreground event causes cross-topic bleed."""from gateway.run import _parse_session_key- from gateway.session import SessionSourcesession_key = str(evt.get("session_key") or "").strip()- derived_platform = ""- derived_chat_type = ""- derived_chat_id = ""+ derived = {}if session_key:try:@@ -1035,32 +870,23 @@ class GatewayNotificationsMixin:if entry and getattr(entry, "origin", None):return entry.originexcept Exception as exc:- logger.debug(- "Synthetic process-event session-store lookup failed for %s: %s",- session_key,- exc,- )+ logger.debug("Synthetic process-event session-store lookup failed for %s: %s", session_key, exc)cached_source = self._get_cached_session_source(session_key)if cached_source is not None:return cached_source- _parsed = _parse_session_key(session_key)- if _parsed:- derived_platform = _parsed["platform"]- derived_chat_type = _parsed["chat_type"]- derived_chat_id = _parsed["chat_id"]+ derived = _parse_session_key(session_key) or {}- platform_name = str(evt.get("platform") or derived_platform or "").strip().lower()- chat_type = str(evt.get("chat_type") or derived_chat_type or "").strip().lower()- chat_id = str(evt.get("chat_id") or derived_chat_id or "").strip()+ platform_name = str(evt.get("platform") or derived.get("platform") or "").strip().lower()+ chat_type = str(evt.get("chat_type") or derived.get("chat_type") or "").strip().lower()+ chat_id = str(evt.get("chat_id") or derived.get("chat_id") or "").strip()if not platform_name or not chat_type or not chat_id:logger.warning("Synthetic event source unresolvable: ""session_key=%r platform=%r chat_type=%r chat_id=%r ""evt_type=%s",- session_key, platform_name, chat_type, chat_id,- evt.get("type", "?"),+ session_key, platform_name, chat_type, chat_id, evt.get("type", "?"),)return None@@ -1076,18 +902,18 @@ class GatewayNotificationsMixin:except Exception:raise ValueError(platform_name)except Exception:- logger.warning(- "Synthetic process event has invalid platform metadata: %r",- platform_name,- )+ logger.warning("Synthetic process event has invalid platform metadata: %r", platform_name)return None- scope_id = str(evt.get("scope_id") or "").strip() or None+ def _opt(field: str) -> Optional[str]:+ return str(evt.get(field) or "").strip() or None++ scope_id = _opt("scope_id")if scope_id is None and chat_type not in ("dm", "thread"):# Reconstructed (non-persisted) source for a scoped chat with no scope discriminator: a- # relay connector's fail-closed tenant guard may decline the reply unless user_id resolves it- # (resolveByUser). Don't fail — DMs/author-bound chats still route and native adapters need- # no scope_id — but warn so a post-restart egress decline isn't silent.+ # relay connector's fail-closed tenant guard may decline the reply unless user_id resolves+ # it. Don't fail — DMs/author-bound chats still route and native adapters need no+ # scope_id — but warn so a post-restart egress decline isn't silent.logger.warning("Synthetic event source for %s chat=%s (%s) reconstructed ""without scope_id; scoped relay egress may be declined by "@@ -1098,9 +924,9 @@ class GatewayNotificationsMixin:platform=platform,chat_id=chat_id,chat_type=chat_type,- thread_id=str(evt.get("thread_id") or "").strip() or None,- user_id=str(evt.get("user_id") or "").strip() or None,- user_name=str(evt.get("user_name") or "").strip() or None,+ thread_id=_opt("thread_id"),+ user_id=_opt("user_id"),+ user_name=_opt("user_name"),scope_id=scope_id,)@@ -1124,9 +950,41 @@ class GatewayNotificationsMixin:except Exception as exc:logger.error("Watch notification injection error: %s", exc)- async def _inject_watch_notification(- self, synth_text: str, evt: dict,- ) -> Optional[bool]:+ def _adapter_by_platform_value(self, platform_name: str):+ """Literal ``p.value == platform_name`` scan over connected adapters (native adapters only)."""+ for p, a in self.adapters.items():+ if p.value == platform_name:+ return a+ return None++ async def _self_post_api_server(self, adapter, synth_text: str, raw_sid: str, evt: dict) -> bool:+ """Deliver to a non-push (api_server) session by raw session id.++ Async-delegation completions are persisted as a durable delivery row — after the parent+ turn's event.complete the CLIENT owns the next turn on this stateless surface, so never+ self-post them as a new role=user prompt. Other watch events wake the session via self-post.+ """+ from gateway.wake import deliver_wake, persist_delegation_delivery+ if evt.get("type") == "async_delegation":+ try:+ logger.info(+ "Async delegation completion — persisting delivery row for api_server session %s (no wake turn)",+ raw_sid,+ )+ await persist_delegation_delivery(adapter, text=synth_text, session_id=raw_sid, evt=evt)+ return True+ except Exception as e:+ logger.warning("Async delegation delivery persist failed for session %s: %s", raw_sid, e)+ return False+ try:+ logger.info("Watch pattern notification — waking api_server session %s via self-post", raw_sid)+ await deliver_wake(adapter, text=synth_text, session_id=raw_sid)+ return True+ except Exception as e:+ logger.warning("Watch notification self-post wake failed for session %s: %s", raw_sid, e)+ return False++ async def _inject_watch_notification(self, synth_text: str, evt: dict) -> Optional[bool]:"""Inject a watch/completion notification as a synthetic message event.Routing comes from the queued event, never the active foreground message. Returns@@ -1134,6 +992,7 @@ class GatewayNotificationsMixin:gateway route. Not transactional: a crash after acceptance can replay (at-least-once)."""from gateway.run import _parse_session_key, resolve_delivery_transport+ from gateway.wake import adapter_supports_pushsource = await asyncio.to_thread(self._build_process_event_source, evt)if not source:# API-server sessions bind the RAW X-Hermes-Session-Id key (_bind_api_server_session), not a@@ -1145,53 +1004,10 @@ class GatewayNotificationsMixin:raw_sid = _skif raw_sid:adapter = self.adapters.get(Platform.API_SERVER)- from gateway.wake import (- adapter_supports_push,- deliver_wake,- persist_delegation_delivery,- )if adapter is not None and not adapter_supports_push(adapter):- if evt.get("type") == "async_delegation":- # after the parent turn's event.complete the CLIENT owns the next turn on- # this stateless surface. Persist the completion as a durable delivery row —- # never self-post it as a new role=user prompt.- try:- logger.info(- "Async delegation completion — persisting "- "delivery row for api_server session %s "- "(no wake turn)",- raw_sid,- )- await persist_delegation_delivery(- adapter, text=synth_text,- session_id=raw_sid, evt=evt,- )- return True- except Exception as e:- logger.warning(- "Async delegation delivery persist failed "- "for session %s: %s",- raw_sid, e,- )- return False- try:- logger.info(- "Watch pattern notification — waking api_server "- "session %s via self-post",- raw_sid,- )- await deliver_wake(adapter, text=synth_text, session_id=raw_sid)- return True- except Exception as e:- logger.warning(- "Watch notification self-post wake failed for "- "session %s: %s",- raw_sid, e,- )- return False+ return await self._self_post_api_server(adapter, synth_text, raw_sid, evt)logger.warning(- "Dropping watch notification for raw session %s: no "- "api_server adapter to self-post through",+ "Dropping watch notification for raw session %s: no api_server adapter to self-post through",raw_sid,)return None@@ -1203,8 +1019,8 @@ class GatewayNotificationsMixin:platform_name = source.platform.value if hasattr(source.platform, "value") else str(source.platform)# Alias-aware resolution (relay-plane): one adapter under Platform.RELAY fronts N logical# platforms, so a literal ``p.value == platform_name`` scan misses "slack" and drops the- # completion as "no gateway route". Use the shared transport resolver — native adapter wins;- # relay is eligible only when it advertises fronting the logical platform.+ # completion as "no gateway route". Native adapter wins; relay is eligible only when it+ # advertises fronting the logical platform.adapter = Nonetry:_platform_enum = Platform(platform_name)@@ -1212,9 +1028,7 @@ class GatewayNotificationsMixin:_platform_enum = Noneif _platform_enum is not None:try:- _transport = resolve_delivery_transport(- _platform_enum, self.config, self.adapters,- )+ _transport = resolve_delivery_transport(_platform_enum, self.config, self.adapters)except Exception:_transport = Noneif _transport is not None:@@ -1222,54 +1036,15 @@ class GatewayNotificationsMixin:if adapter is None:# Legacy literal scan — still correct for native adapters; keeps minimal runner stubs (tests)# and exotic platform strings working when the resolver can't run.- for p, a in self.adapters.items():- if p.value == platform_name:- adapter = a- break+ adapter = self._adapter_by_platform_value(platform_name)if not adapter:return None- from gateway.wake import adapter_supports_push as _wake_push_ok- if not _wake_push_ok(adapter):+ if not adapter_supports_push(adapter):# Non-push adapter (api_server) resolved WITH routing metadata: its chat_id is the raw session# id (_bind_api_server_session binds chat_id = session_id), so handle_message would run the# wake under a build_session_key() key that never matches the raw session — self-post.- from gateway.wake import deliver_wake, persist_delegation_deliveryraw_sid = str(evt.get("origin_session_id") or "").strip() or str(source.chat_id or "")- if evt.get("type") == "async_delegation":- # Same client-owns-the-turn rule as the raw-key branch above: persist the completion as a- # delivery row, never self-post it as a new role=user prompt.- try:- logger.info(- "Async delegation completion — persisting delivery "- "row for api_server session %s (no wake turn)",- raw_sid,- )- await persist_delegation_delivery(- adapter, text=synth_text, session_id=raw_sid, evt=evt,- )- return True- except Exception as e:- logger.warning(- "Async delegation delivery persist failed for "- "session %s: %s",- raw_sid, e,- )- return False- try:- logger.info(- "Watch pattern notification — waking api_server session "- "%s via self-post",- raw_sid,- )- await deliver_wake(adapter, text=synth_text, session_id=raw_sid)- return True- except Exception as e:- logger.warning(- "Watch notification self-post wake failed for session "- "%s: %s",- raw_sid, e,- )- return False+ return await self._self_post_api_server(adapter, synth_text, raw_sid, evt)try:metadata = {}parent_session_id = str(evt.get("parent_session_id") or "").strip()@@ -1285,9 +1060,7 @@ class GatewayNotificationsMixin:)logger.info("Watch pattern notification — injecting for %s chat=%s thread=%s",- platform_name,- source.chat_id,- source.thread_id,+ platform_name, source.chat_id, source.thread_id,)# Relay-plane egress priming: a synthetic turn injected right after a restart reaches a relay# adapter whose per-chat routing caches are cold (they warm only on inbound), so its replies@@ -1320,6 +1093,22 @@ class GatewayNotificationsMixin:return (evt_type, producer_id, started_at)return None+ def _mark_completions_delivered_locked(self, identities) -> None:+ """Move identities inflight -> delivered and trim retention. Caller holds ``_completion_delivery_lock``."""+ for identity in identities:+ self._completion_deliveries_inflight.discard(identity)+ self._completion_deliveries_delivered[identity] = None+ while len(self._completion_deliveries_delivered) > self._completion_delivery_retention:+ self._completion_deliveries_delivered.popitem(last=False)++ def _completion_identity_seen(self, identity) -> bool:+ """True when ``identity`` is inflight or already delivered in this gateway lifecycle."""+ with self._completion_delivery_lock:+ return (+ identity in self._completion_deliveries_inflight+ or identity in self._completion_deliveries_delivered+ )+async def _classify_completion_target(self, parent_session_id: str) -> str:"""Classify an async-completion delivery target before adapter acceptance.@@ -1337,10 +1126,7 @@ class GatewayNotificationsMixin:try:parent = await session_db.get_session(parent_session_id)except Exception:- logger.debug(- "Async-completion pre-flight parent lookup failed for %s",- parent_session_id, exc_info=True,- )+ logger.debug("Async-completion pre-flight parent lookup failed for %s", parent_session_id, exc_info=True)return "retry"if parent is None:return "terminal"@@ -1352,114 +1138,102 @@ class GatewayNotificationsMixin:# session_reset / new_session, user_exit, session_switch). Idle/timeout ends are normal on# scale-to-zero relays — the chat stays routable and the resolver retargets, so dropping loses# finished work. Boundary set shared with the resolver (_USER_BOUNDARY_END_REASONS): no drift.- if end_reason in _USER_BOUNDARY_END_REASONS:- return "terminal"- return "deliver"+ return "terminal" if end_reason in _USER_BOUNDARY_END_REASONS else "deliver"try:tip_session_id = await session_db.get_compression_tip(parent_session_id)if not tip_session_id or tip_session_id == parent_session_id:- # Rotation caught mid-flight: parent is compression-ended but- # its continuation isn't visible yet. Retry, don't drop.+ # Rotation caught mid-flight: parent is compression-ended but its continuation isn't+ # visible yet. Retry, don't drop.return "retry"tip = await session_db.get_session(tip_session_id)except Exception:- logger.debug(- "Async-completion pre-flight tip lookup failed for %s",- parent_session_id, exc_info=True,- )+ logger.debug("Async-completion pre-flight tip lookup failed for %s", parent_session_id, exc_info=True)return "retry"if tip is None or tip.get("ended_at"):return "retry"return "deliver"- async def _deliver_completion_notification(- self, synth_text: str, evt: dict,- ) -> Optional[bool]:- """Deliver once per live gateway, or return False for a retry.+ @staticmethod+ def _settle_durable_claim(kind: str, delegation_id: str, claim_id: str) -> None:+ """Best-effort ``drop``/``release`` of a durable completion claim."""+ fn_name, fail_msg = _DURABLE_CLAIM_OPS[kind]+ try:+ import tools.async_delegation as _ad- ``True``: adapter accepted; ``False``: injection failed, claim released for retry; ``None``:- another same-lifecycle caller owns/delivered it, or no route. No cross-process exactly-once.+ getattr(_ad, fn_name)(delegation_id, claim_id)+ except Exception:+ logger.debug(fail_msg, exc_info=True)++ async def _preflight_completion_delivery(self, evt: dict) -> "_CompletionClaim":+ """Claim the durable row (async delegations) and verify the target before adapter acceptance.++ Adapter acceptance is not proof of delivery: the inner resolver can still fail closed inside+ the pipeline after acceptance, falsely acking the durable row. Verifying first gives drops an+ honest durable disposition."""- identity = self._completion_delivery_identity(evt)- durable_claim_id = ""- durable_delegation_id = ""- if evt.get("type") == "async_delegation":- durable_delegation_id = str(evt.get("delegation_id") or "")- if durable_delegation_id:+ claim = self._CompletionClaim()+ evt_type = evt.get("type")+ if evt_type == "async_delegation":+ claim.delegation_id = str(evt.get("delegation_id") or "")+ if claim.delegation_id:try:from tools.async_delegation import claim_completion_delivery- durable_claim_id = f"gateway:{id(self)}:{__import__('uuid').uuid4().hex}"- if not claim_completion_delivery(- durable_delegation_id, durable_claim_id,- ):- return None+ claim.claim_id = f"gateway:{id(self)}:{__import__('uuid').uuid4().hex}"+ if not claim_completion_delivery(claim.delegation_id, claim.claim_id):+ claim.proceed = False+ return claimexcept Exception as exc:- logger.warning(- "Could not claim durable async completion %s: %s",- durable_delegation_id, exc,- )- return False- parent_session_id = str(evt.get("parent_session_id") or "").strip()- if parent_session_id:- # Adapter acceptance is not proof of delivery: the inner resolver can still fail closed- # inside the pipeline after acceptance, falsely acking the durable row as delivered.- # Verify the target before acceptance so drops get an honest durable disposition.- verdict = await self._classify_completion_target(parent_session_id)- if verdict == "terminal":- logger.warning(- "Async delegation %s targets permanently-gone session %s; "- "terminally dropping delivery (result remains in the "- "delegation records).",- durable_delegation_id or "<legacy>", parent_session_id,- )- if durable_claim_id:- try:- from tools.async_delegation import drop_completion_delivery-- drop_completion_delivery(- durable_delegation_id, durable_claim_id,- )- except Exception:- logger.debug(- "Could not drop durable completion claim",- exc_info=True,- )- return None- if verdict == "retry":- if durable_claim_id:- try:- from tools.async_delegation import release_completion_delivery+ logger.warning("Could not claim durable async completion %s: %s", claim.delegation_id, exc)+ claim.proceed, claim.early_result = False, False+ return claim+ elif evt_type != "completion":+ return claim++ # Background completions carry only session_key, so after /new the OLD session's notification+ # would land in the chat's NEW session. Stamped events get the same pre-flight as async+ # delegations; unstamped ones deliver.+ parent_session_id = str(evt.get("parent_session_id") or "").strip()+ if not parent_session_id:+ return claim+ verdict = await self._classify_completion_target(parent_session_id)+ if verdict == "terminal":+ if evt_type == "async_delegation":+ logger.warning(+ "Async delegation %s targets permanently-gone session %s; "+ "terminally dropping delivery (result remains in the "+ "delegation records).",+ claim.delegation_id or "<legacy>", parent_session_id,+ )+ if claim.claim_id:+ self._settle_durable_claim("drop", claim.delegation_id, claim.claim_id)+ else:+ logger.warning(+ "Background process %s completion targets "+ "permanently-gone session %s (user boundary such as "+ "/new); dropping notification (output remains "+ "available via process(action='log')).",+ evt.get("session_id") or "<unknown>", parent_session_id,+ )+ claim.proceed = False+ elif verdict == "retry":+ # Transient uncertainty (session DB down / compression rotation mid-flight): tell the+ # watcher to re-poll and retry rather than drop or misroute the result.+ if claim.claim_id:+ self._settle_durable_claim("release", claim.delegation_id, claim.claim_id)+ claim.proceed, claim.early_result = False, False+ return claim++ async def _deliver_completion_notification(self, synth_text: str, evt: dict) -> Optional[bool]:+ """Deliver once per live gateway, or return False for a retry.- release_completion_delivery(- durable_delegation_id, durable_claim_id,- )- except Exception:- logger.debug(- "Could not release durable completion claim",- exc_info=True,- )- return False- elif evt.get("type") == "completion":- # Background completions carry only session_key, so after /new the OLD session's- # notification would land in the chat's NEW session. Stamped events get the same- # pre-flight as async delegations (_classify_completion_target); unstamped ones deliver.- parent_session_id = str(evt.get("parent_session_id") or "").strip()- if parent_session_id:- verdict = await self._classify_completion_target(parent_session_id)- if verdict == "terminal":- logger.warning(- "Background process %s completion targets "- "permanently-gone session %s (user boundary such as "- "/new); dropping notification (output remains "- "available via process(action='log')).",- evt.get("session_id") or "<unknown>", parent_session_id,- )- return None- if verdict == "retry":- # Transient uncertainty (session DB down / compression rotation mid-flight): tell the- # watcher to re-poll and retry rather than drop or misroute the result.- return False+ ``True``: adapter accepted; ``False``: injection failed, claim released for retry; ``None``:+ another same-lifecycle caller owns/delivered it, or no route. No cross-process exactly-once.+ """+ identity = self._completion_delivery_identity(evt)+ claim = await self._preflight_completion_delivery(evt)+ if not claim.proceed:+ return claim.early_resultif identity is not None:with self._completion_delivery_lock:if (@@ -1478,54 +1252,28 @@ class GatewayNotificationsMixin:if identity is not None:with self._completion_delivery_lock:- self._completion_deliveries_inflight.discard(identity)- self._completion_deliveries_delivered[identity] = None- while (- len(self._completion_deliveries_delivered)- > self._completion_delivery_retention- ):- self._completion_deliveries_delivered.popitem(last=False)-- # When the durable async-delegation producer branch is present, its SQLite row is the- # authoritative replay state — ack it after adapter acceptance; no parallel ledger here.- if durable_claim_id:+ self._mark_completions_delivered_locked((identity,))++ # The durable async-delegation row is the authoritative replay state — ack it after+ # adapter acceptance; no parallel ledger here.+ if claim.claim_id:try:from tools.async_delegation import complete_completion_delivery- complete_completion_delivery(- durable_delegation_id, durable_claim_id,- )+ complete_completion_delivery(claim.delegation_id, claim.claim_id)except Exception as exc:- logger.warning(- "Could not acknowledge durable async completion %s: %s",- durable_delegation_id, exc,- )+ logger.warning("Could not acknowledge durable async completion %s: %s", claim.delegation_id, exc)return Truefinally:if identity is not None and not accepted:with self._completion_delivery_lock:self._completion_deliveries_inflight.discard(identity)- if durable_claim_id and not accepted:- try:- from tools.async_delegation import release_completion_delivery-- release_completion_delivery(- durable_delegation_id, durable_claim_id,- )- except Exception:- logger.debug("Could not release durable completion claim", exc_info=True)+ if claim.claim_id and not accepted:+ self._settle_durable_claim("release", claim.delegation_id, claim.claim_id)@staticmethod- def _completion_notification_batch_key(evt: dict) -> tuple[str, ...]:- """Return a routing-complete key for short-window process fan-in."""- return tuple(str(evt.get(field) or "") for field in (- "session_key",- "platform",- "chat_type",- "chat_id",- "thread_id",- "user_id",- ))+ def _event_route_key(evt: dict, fields: tuple[str, ...]) -> tuple[str, ...]:+ return tuple(str(evt.get(field) or "") for field in fields)@staticmethoddef _format_coalesced_process_completions(entries: list[tuple[str, dict, asyncio.Future]]) -> str:@@ -1544,14 +1292,10 @@ class GatewayNotificationsMixin:# Completion output normally passes the terminal redactor at the producer seam, but that is# configurable and this is user-facing, so keep the unconditional gateway floor. Redact# BEFORE slicing: truncating first can leave a credential fragment the patterns miss.- output = _redact_gateway_user_facing_secrets(- str(evt.get("output") or "")- ).strip()+ output = _redact_gateway_user_facing_secrets(str(evt.get("output") or "")).strip()if len(output) > 800:output = f"[… truncated …]\n{output[-800:]}"- lines.append(- f"\n- {session_id}: exit_code={exit_code}, reason={reason}"- )+ lines.append(f"\n- {session_id}: exit_code={exit_code}, reason={reason}")if output:lines.append(output)omitted = len(entries) - len(shown)@@ -1560,25 +1304,14 @@ class GatewayNotificationsMixin:f"\n- … and {omitted} more completion(s); inspect them with ""the process tool if they affect the conclusion.")- lines.append(- "If a result does not change the current conclusion, absorb it silently.]"- )+ lines.append("If a result does not change the current conclusion, absorb it silently.]")return "\n".join(lines)def _record_coalesced_completion_siblings(self, events: list[dict]) -> None:"""Extend a successful primary delivery claim to its batched siblings."""+ identities = [i for i in map(self._completion_delivery_identity, events) if i is not None]with self._completion_delivery_lock:- for evt in events:- identity = self._completion_delivery_identity(evt)- if identity is None:- continue- self._completion_deliveries_inflight.discard(identity)- self._completion_deliveries_delivered[identity] = None- while (- len(self._completion_deliveries_delivered)- > self._completion_delivery_retention- ):- self._completion_deliveries_delivered.popitem(last=False)+ self._mark_completions_delivered_locked(identities)async def _flush_process_completion_batch(self, key: tuple[str, ...]) -> None:"""Deliver one short-window completion batch and resolve its waiters."""@@ -1588,30 +1321,23 @@ class GatewayNotificationsMixin:try:await asyncio.sleep(self._completion_notification_batch_window)entries = self._completion_notification_batches.pop(key, [])- # Detach before adapter delivery. A completion that arrives while- # this batch is in flight must be able to schedule the next flush.+ # Detach before adapter delivery: a completion arriving while this batch is in flight+ # must be able to schedule the next flush.if self._completion_notification_batch_tasks.get(key) is current_task:self._completion_notification_batch_tasks.pop(key, None)if not entries:return- if len(entries) == 1:- synth_text = entries[0][0]- else:- synth_text = self._format_coalesced_process_completions(entries)+ synth_text = entries[0][0] if len(entries) == 1 else self._format_coalesced_process_completions(entries)# A duplicate primary can legitimately return None from the lifecycle dedupe seam; try the# next batch identity so a fresh sibling is never discarded with that duplicate.delivered = Nonefor _text, candidate_evt, _future in entries:- delivered = await self._deliver_completion_notification(- synth_text, candidate_evt,- )+ delivered = await self._deliver_completion_notification(synth_text, candidate_evt)if delivered is not None:breakif delivered is True and len(entries) > 1:- self._record_coalesced_completion_siblings(- [evt for _text, evt, _future in entries]- )+ self._record_coalesced_completion_siblings([evt for _text, evt, _future in entries])except asyncio.CancelledError:# Shutdown may cancel us mid fan-in or while adapter delivery is blocked: recover entries not# yet detached and resolve every waiter as retryable before adapters are torn down.@@ -1637,9 +1363,7 @@ class GatewayNotificationsMixin:self._completion_notification_batches_stopping = Truetasks = {task- for task in getattr(- self, "_completion_notification_batch_flush_tasks", set()- )+ for task in getattr(self, "_completion_notification_batch_flush_tasks", set())if not task.done()}for task in tasks:@@ -1657,46 +1381,35 @@ class GatewayNotificationsMixin:getattr(self, "_completion_notification_batch_tasks", {}).clear()getattr(self, "_completion_notification_batch_flush_tasks", set()).clear()- async def _enqueue_process_completion_notification(- self, synth_text: str, evt: dict,- ) -> Optional[bool]:+ async def _enqueue_process_completion_notification(self, synth_text: str, evt: dict) -> Optional[bool]:"""Fan in concurrent process completions that share one conversation."""# Some unit tests construct GatewayRunner with object.__new__. Keep the# batching seam lazy so those focused lifecycle tests remain valid.- if not hasattr(self, "_completion_notification_batches"):- self._completion_notification_batches = {}- if not hasattr(self, "_completion_notification_batch_tasks"):- self._completion_notification_batch_tasks = {}- if not hasattr(self, "_completion_notification_batch_flush_tasks"):- self._completion_notification_batch_flush_tasks = set()- if not hasattr(self, "_completion_notification_batch_window"):- self._completion_notification_batch_window = 0.1- if not hasattr(self, "_completion_notification_batches_stopping"):- self._completion_notification_batches_stopping = False+ for attr, default in (+ ("_completion_notification_batches", dict),+ ("_completion_notification_batch_tasks", dict),+ ("_completion_notification_batch_flush_tasks", set),+ ("_completion_notification_batch_window", lambda: 0.1),+ ("_completion_notification_batches_stopping", lambda: False),+ ("_background_tasks", set),+ ):+ if not hasattr(self, attr):+ setattr(self, attr, default())if self._completion_notification_batches_stopping:return False- key = self._completion_notification_batch_key(evt)+ key = self._event_route_key(evt, self._COMPLETION_BATCH_KEY_FIELDS)future = asyncio.get_running_loop().create_future()- self._completion_notification_batches.setdefault(key, []).append(- (synth_text, evt, future)- )+ self._completion_notification_batches.setdefault(key, []).append((synth_text, evt, future))if key not in self._completion_notification_batch_tasks:- task = asyncio.create_task(- self._flush_process_completion_batch(key)- )+ task = asyncio.create_task(self._flush_process_completion_batch(key))self._completion_notification_batch_tasks[key] = task- # Keep the flush alive under the gateway's normal lifecycle accounting; runners built via- # object.__new__ (focused tests) lazily receive the same ownership set.- if not hasattr(self, "_background_tasks"):- self._background_tasks = set()+ # Keep the flush alive under the gateway's normal lifecycle accounting.self._background_tasks.add(task)self._completion_notification_batch_flush_tasks.add(task)task.add_done_callback(self._background_tasks.discard)- task.add_done_callback(- self._completion_notification_batch_flush_tasks.discard- )+ task.add_done_callback(self._completion_notification_batch_flush_tasks.discard)return await futuredef _enrich_async_delegation_routing(self, evt: dict) -> None:@@ -1718,33 +1431,14 @@ class GatewayNotificationsMixin:evt["thread_id"] = parsed["thread_id"]@staticmethod- def _async_delegation_group_key(evt: dict) -> tuple[str, ...]:- """Return the async-completion coalescing key: originating session, parent session, route."""- return tuple(str(evt.get(field) or "") for field in (- "session_key",- "parent_session_id",- "platform",- "chat_type",- "chat_id",- "thread_id",- "user_id",- ))-- @staticmethod- def _format_coalesced_async_delegations(blocks: list[str]) -> str:- """Join per-delegation formatted blocks into one consolidated turn."""- header = (- f"[IMPORTANT: {len(blocks)} background subagent delegations "- "completed for this session. Treat these results as one "- "completion batch and send at most one consolidated user-facing "- "response. If a result does not change the current conclusion, "- "absorb it silently.]"- )- return "\n\n".join([header, *blocks])+ def _settle_sibling_claims(siblings: list[tuple[dict, str]], fn, fail_msg: str) -> None:+ for evt, claim_id in siblings:+ try:+ fn(evt, claim_id)+ except Exception:+ logger.debug(fail_msg, exc_info=True)- async def _deliver_async_delegation_group(- self, group: list[dict],- ) -> Optional[bool]:+ async def _deliver_async_delegation_group(self, group: list[dict]) -> Optional[bool]:"""Deliver a same-session batch of async completions as ONE turn.Single-event groups ride the per-event path. Multi-event groups deliver the primary via@@ -1762,13 +1456,8 @@ class GatewayNotificationsMixin:if not synth_text:continueidentity = self._completion_delivery_identity(evt)- if identity is not None:- with self._completion_delivery_lock:- if (- identity in self._completion_deliveries_inflight- or identity in self._completion_deliveries_delivered- ):- continue+ if identity is not None and self._completion_identity_seen(identity):+ continuedeliverable.append((evt, synth_text))if not deliverable:@@ -1777,11 +1466,7 @@ class GatewayNotificationsMixin:evt, synth_text = deliverable[0]return await self._deliver_completion_notification(synth_text, evt)- from tools.async_delegation import (- claim_event_delivery,- complete_event_delivery,- release_event_delivery,- )+ from tools.async_delegation import claim_event_delivery, complete_event_delivery, release_event_deliveryprimary_evt, primary_text = deliverable[0]blocks = [primary_text]@@ -1789,50 +1474,39 @@ class GatewayNotificationsMixin:for evt, synth_text in deliverable[1:]:claim_id = claim_event_delivery(evt, f"gateway-batch:{id(self)}")if claim_id is None:- # Another consumer owns this row's delivery; keep its result- # out of our consolidated text so it is never double-injected.+ # Another consumer owns this row's delivery; keep its result out of our+ # consolidated text so it is never double-injected.continuesiblings.append((evt, claim_id))blocks.append(synth_text)if not siblings:- return await self._deliver_completion_notification(- primary_text, primary_evt,- )+ return await self._deliver_completion_notification(primary_text, primary_evt)- consolidated = self._format_coalesced_async_delegations(blocks)+ header = (+ f"[IMPORTANT: {len(blocks)} background subagent delegations "+ "completed for this session. Treat these results as one "+ "completion batch and send at most one consolidated user-facing "+ "response. If a result does not change the current conclusion, "+ "absorb it silently.]"+ )+ consolidated = "\n\n".join([header, *blocks])delivered: Optional[bool] = Falsetry:- delivered = await self._deliver_completion_notification(- consolidated, primary_evt,- )+ delivered = await self._deliver_completion_notification(consolidated, primary_evt)finally:if delivered is True:- for evt, claim_id in siblings:- try:- complete_event_delivery(evt, claim_id)- except Exception:- logger.debug(- "Could not acknowledge coalesced durable completion",- exc_info=True,- )- self._record_coalesced_completion_siblings(- [evt for evt, _claim_id in siblings]+ self._settle_sibling_claims(+ siblings, complete_event_delivery, "Could not acknowledge coalesced durable completion",)+ self._record_coalesced_completion_siblings([evt for evt, _claim_id in siblings])else:- # Not delivered — release every sibling claim so a retry or another consumer can claim it,- # honestly leaving the durable rows pending.- for evt, claim_id in siblings:- try:- release_event_delivery(evt, claim_id)- except Exception:- logger.debug(- "Could not release coalesced durable claim",- exc_info=True,- )+ # Not delivered — release every sibling claim so a retry or another consumer can claim+ # it, honestly leaving the durable rows pending.+ self._settle_sibling_claims(siblings, release_event_delivery, "Could not release coalesced durable claim")if delivered is None:- # The primary was dropped/owned elsewhere but the siblings- # still need delivery — requeue just them for the next tick.+ # The primary was dropped/owned elsewhere but the siblings still need delivery —+ # requeue just them for the next tick.for evt, _claim_id in siblings:_pr.completion_queue.put(evt)return delivered@@ -1856,26 +1530,17 @@ class GatewayNotificationsMixin:evt = _pr.completion_queue.get_nowait()except Exception:break- if evt.get("type") == "async_delegation":- async_events.append(evt)- else:- requeue.append(evt)+ (async_events if evt.get("type") == "async_delegation" else requeue).append(evt)for evt in requeue:_pr.completion_queue.put(evt)# A same-tick drain often carries several completions for the SAME session (a fan-out# finishing together); delivering each individually floods it with N synthetic turns.# Group by full gateway route + parent session: one consolidated turn per group.groups: dict[tuple[str, ...], list[dict]] = {}- group_order: list[tuple[str, ...]] = []for evt in async_events:self._enrich_async_delegation_routing(evt)- key = self._async_delegation_group_key(evt)- if key not in groups:- groups[key] = []- group_order.append(key)- groups[key].append(evt)- for key in group_order:- group = groups[key]+ groups.setdefault(self._event_route_key(evt, self._ASYNC_GROUP_KEY_FIELDS), []).append(evt)+ for group in groups.values():try:delivered = await self._deliver_async_delegation_group(group)if delivered is False:@@ -1889,6 +1554,93 @@ class GatewayNotificationsMixin:logger.debug("Async delegation watcher error: %s", e)await asyncio.sleep(interval)+ @staticmethod+ def _redacted_output_tail(session, limit: int) -> str:+ """Last ``limit`` chars of process output through the secret redactors (unconditional floor)."""+ from gateway.run import _redact_gateway_user_facing_secrets+ new_output = session.output_buffer[-limit:] if session.output_buffer else ""+ if new_output:+ from agent.redact import redact_terminal_output+ new_output = redact_terminal_output(new_output, getattr(session, "command", "") or "")+ # redact_terminal_output() is unforced, so it returns raw text when security.redact_secrets+ # is off. This goes straight to the platform adapter, so it needs the same unconditional+ # floor as agent-notify.+ new_output = _redact_gateway_user_facing_secrets(new_output)+ return new_output++ async def _send_watcher_message(self, platform_name: str, chat_id, thread_id, message_text: str) -> None:+ from gateway.run import _non_conversational_metadata+ adapter = self._adapter_by_platform_value(platform_name)+ if adapter and chat_id:+ try:+ send_meta = {"thread_id": thread_id} if thread_id else None+ await adapter.send(+ chat_id, message_text, metadata=_non_conversational_metadata(send_meta, platform=platform_name),+ )+ except Exception as e:+ logger.error("Watcher delivery error: %s", e)++ @staticmethod+ def _build_process_completion_event(watcher: dict, session, session_id: str) -> dict:+ """Build the synthetic ``completion`` event for an agent-notify watcher."""+ from gateway.run import _redact_gateway_user_facing_secrets+ from agent.redact import redact_terminal_output+ from tools.ansi_strip import strip_ansi+ _command = getattr(session, "command", "") or ""+ _raw = strip_ansi(session.output_buffer) if session.output_buffer else ""+ _raw = redact_terminal_output(_raw, _command)+ # Truncate on line boundaries (never start mid-line): keep the last ~2000 chars snapped to+ # the preceding newline, prepending a marker when output was cut.+ _LIMIT = 2000+ if len(_raw) > _LIMIT:+ _tail = _raw[-_LIMIT:]+ _nl = _tail.find("\n")+ _tail = _tail[_nl + 1:] if _nl != -1 else _tail+ _out = f"[… output truncated — showing last {len(_tail)} chars]\n{_tail}"+ else:+ _out = _raw+ return {+ "type": "completion",+ "session_id": session_id,+ "session_key": watcher.get("session_key", ""),+ "platform": watcher.get("platform", ""),+ "chat_type": watcher.get("chat_type", ""),+ "chat_id": watcher.get("chat_id", ""),+ "thread_id": watcher.get("thread_id", ""),+ "user_id": watcher.get("user_id", ""),+ "user_name": watcher.get("user_name", ""),+ "message_id": str(watcher.get("message_id") or "").strip() or None,+ "started_at": getattr(session, "started_at", None),+ "command": _redact_gateway_user_facing_secrets(_command),+ "exit_code": session.exit_code,+ "completion_reason": getattr(session, "completion_reason", "exited"),+ "termination_source": getattr(session, "termination_source", ""),+ "output": _redact_gateway_user_facing_secrets(_out),+ # Spawning conversation's session-db id (stamped in terminal_tool); lets delivery+ # pre-flight drop this completion if the user closed that session (/new) first.+ "parent_session_id": (+ watcher.get("parent_session_id") or getattr(session, "parent_session_id", "") or ""+ ),+ }++ def _format_process_final_message(self, session_id: str, session, notify_mode: str) -> str:+ from gateway.run import _format_concise_process_notification, _redact_gateway_user_facing_secrets+ new_output = self._redacted_output_tail(session, 1000)+ if notify_mode != "concise":+ return (+ f"[Background process {session_id} finished with exit code {session.exit_code}~ "+ f"Here's the final output:\n{new_output}]"+ )+ _started = getattr(session, "started_at", None)+ _dur = max(0.0, time.time() - _started) if isinstance(_started, (int, float)) else None+ return _format_concise_process_notification(+ session_id,+ _redact_gateway_user_facing_secrets(getattr(session, "command", "") or ""),+ session.exit_code,+ new_output,+ duration_seconds=_dur,+ )+async def _run_process_watcher(self, watcher: dict) -> None:"""Periodically check a background process and push updates to the user.@@ -1897,22 +1649,13 @@ class GatewayNotificationsMixin:concise (default, one-line; failures append output tail) / all (running updates + finalraw output) / result (final raw only) / error (final raw only if exit != 0) / off."""- from gateway.run import (- _format_concise_process_notification,- _non_conversational_metadata,- _redact_gateway_user_facing_secrets,- )- from tools.process_registry import process_registry+ from tools.process_registry import format_process_notification, process_registrysession_id = watcher["session_id"]interval = watcher["check_interval"]- session_key = watcher.get("session_key", "")platform_name = watcher.get("platform", "")chat_id = watcher.get("chat_id", "")thread_id = watcher.get("thread_id", "")- user_id = watcher.get("user_id", "")- user_name = watcher.get("user_name", "")- message_id = str(watcher.get("message_id") or "").strip() or Noneagent_notify = watcher.get("notify_on_complete", False)notify_mode = self._load_background_notifications_mode()@@ -1920,8 +1663,7 @@ class GatewayNotificationsMixin:session_id, interval, notify_mode, agent_notify)if notify_mode == "off" and not agent_notify:- # Still wait for the process to exit so we can log it, but don't- # push any messages to the user.+ # Still wait for the process to exit so we can log it, but don't push any messages.while True:await asyncio.sleep(interval)session = process_registry.get(session_id)@@ -1946,153 +1688,45 @@ class GatewayNotificationsMixin:# Agent-triggered completion: inject a synthetic message unless the agent already consumed# the result via wait/log. poll() is read-only and deliberately does NOT mark consumed —# a status check must not suppress this delivery turn.- from tools.process_registry import format_process_notification, process_registry as _pr_check- if agent_notify and not _pr_check.is_completion_consumed(session_id):- from agent.redact import redact_terminal_output- from tools.ansi_strip import strip_ansi- _command = getattr(session, "command", "") or ""- _raw = strip_ansi(session.output_buffer) if session.output_buffer else ""- _raw = redact_terminal_output(_raw, _command)- _command = _redact_gateway_user_facing_secrets(_command)- # Truncate on line boundaries (never start mid-line): keep the last ~2000 chars- # snapped to the preceding newline, prepending a marker when output was cut.- _LIMIT = 2000- if len(_raw) > _LIMIT:- _tail = _raw[-_LIMIT:]- _nl = _tail.find("\n")- _tail = _tail[_nl + 1:] if _nl != -1 else _tail- _out = f"[… output truncated — showing last {len(_tail)} chars]\n{_tail}"- else:- _out = _raw- _out = _redact_gateway_user_facing_secrets(_out)- completion_evt = {- "type": "completion",- "session_id": session_id,- "session_key": session_key,- "platform": platform_name,- "chat_type": watcher.get("chat_type", ""),- "chat_id": chat_id,- "thread_id": thread_id,- "user_id": user_id,- "user_name": user_name,- "message_id": message_id,- "started_at": getattr(session, "started_at", None),- "command": _command,- "exit_code": session.exit_code,- "completion_reason": getattr(session, "completion_reason", "exited"),- "termination_source": getattr(session, "termination_source", ""),- "output": _out,- # Spawning conversation's session-db id (stamped in terminal_tool); lets delivery- # pre-flight drop this completion if the user closed that session (/new) first.- "parent_session_id": (- watcher.get("parent_session_id")- or getattr(session, "parent_session_id", "")- or ""- ),- }+ if agent_notify and not process_registry.is_completion_consumed(session_id):+ completion_evt = self._build_process_completion_event(watcher, session, session_id)synth_text = format_process_notification(completion_evt)if not synth_text:break- delivered = await self._enqueue_process_completion_notification(- synth_text, completion_evt,- )+ delivered = await self._enqueue_process_completion_notification(synth_text, completion_evt)if delivered is False:- # The process remains terminal; retry after failed- # adapter injection instead of suppressing the result.+ # The process remains terminal; retry after failed adapter injection instead+ # of suppressing the result.continuebreak# Normal text-only notification. Skip when the agent already consumed this completion via# wait/log (output returned inline) — the raw "finished" message would be a duplicate.# The agent_notify skip FALLS THROUGH here, hence this check. poll() is read-only.- if _pr_check.is_completion_consumed(session_id):+ if process_registry.is_completion_consumed(session_id):logger.debug("Process watcher: completion for %s already consumed ""via wait/log — skipping raw notification (#65379)",session_id,)break- # Decide whether to notify based on modeshould_notify = (notify_mode in {"concise", "all", "result"}or (notify_mode == "error" and session.exit_code not in {0, None}))if should_notify:- new_output = session.output_buffer[-1000:] if session.output_buffer else ""- if new_output:- from agent.redact import redact_terminal_output- new_output = redact_terminal_output(- new_output, getattr(session, "command", "") or ""- )- # redact_terminal_output() is unforced, so it returns raw text when- # security.redact_secrets is off. This goes straight to the platform- # adapter, so it needs the same unconditional floor as agent-notify.- new_output = _redact_gateway_user_facing_secrets(new_output)- if notify_mode == "concise":- _cmd_disp = _redact_gateway_user_facing_secrets(- getattr(session, "command", "") or ""- )- _started = getattr(session, "started_at", None)- _dur = None- if isinstance(_started, (int, float)):- _dur = max(0.0, time.time() - _started)- message_text = _format_concise_process_notification(- session_id,- _cmd_disp,- session.exit_code,- new_output,- duration_seconds=_dur,- )- else:- message_text = (- f"[Background process {session_id} finished with exit code {session.exit_code}~ "- f"Here's the final output:\n{new_output}]"- )- adapter = None- for p, a in self.adapters.items():- if p.value == platform_name:- adapter = a- break- if adapter and chat_id:- try:- send_meta = {"thread_id": thread_id} if thread_id else None- await adapter.send(- chat_id,- message_text,- metadata=_non_conversational_metadata(send_meta, platform=platform_name),- )- except Exception as e:- logger.error("Watcher delivery error: %s", e)+ message_text = self._format_process_final_message(session_id, session, notify_mode)+ await self._send_watcher_message(platform_name, chat_id, thread_id, message_text)breakelif has_new_output and notify_mode == "all" and not agent_notify:- # New output available -- deliver status update (only in "all" mode)- # Skip periodic updates for agent_notify watchers (they only care about completion)- new_output = session.output_buffer[-500:] if session.output_buffer else ""- if new_output:- from agent.redact import redact_terminal_output- new_output = redact_terminal_output(- new_output, getattr(session, "command", "") or ""- )- new_output = _redact_gateway_user_facing_secrets(new_output)+ # New output — deliver a status update (only in "all" mode; agent_notify watchers+ # only care about completion).+ new_output = self._redacted_output_tail(session, 500)message_text = (f"[Background process {session_id} is still running~ "f"New output:\n{new_output}]")- adapter = None- for p, a in self.adapters.items():- if p.value == platform_name:- adapter = a- break- if adapter and chat_id:- try:- send_meta = {"thread_id": thread_id} if thread_id else None- await adapter.send(- chat_id,- message_text,- metadata=_non_conversational_metadata(send_meta, platform=platform_name),- )- except Exception as e:- logger.error("Watcher delivery error: %s", e)+ await self._send_watcher_message(platform_name, chat_id, thread_id, message_text)logger.debug("Process watcher ended: %s", session_id)