From bfbf31d5768d5440a2e934c6dcdf42b59c7e874c Mon Sep 17 00:00:00 2001 From: Tim Smykov <71883740+timsmykov@users.noreply.github.com> Date: Sun, 13 Sep 2026 14:51:19 -0700 Subject: [PATCH] fix(gateway): polish background process notifications MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The raw-output watcher modes (all/result/error) and the interim running update sent the bracketed debug wrapper with the internal process id (`[Background process proc_… finished with exit code N~ Here's the final output: …]`) to Telegram/Discord/Slack chats. Reuse the concise one-line status header for every mode and append the bounded, ANSI-stripped output tail in a code block; the running update gets the same shape. Salvaged from #54266 (rebased onto the post-#102117 run_notifications sibling; the concise mode had landed in between, so the header is shared rather than reimplemented). Also covers #13122 (ANSI stripping). --- gateway/run_notifications.py | 32 +++++++++++++--------- website/docs/user-guide/messaging/index.md | 6 ++-- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py index 1b66585b8b..440db0d8eb 100644 --- a/gateway/run_notifications.py +++ b/gateway/run_notifications.py @@ -1644,7 +1644,8 @@ class GatewayNotificationsMixin: 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 "" + from tools.ansi_strip import strip_ansi + new_output = strip_ansi(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 "") @@ -1703,19 +1704,26 @@ class GatewayNotificationsMixin: } def _format_process_final_message(self, session_id: str, session, notify_mode: str) -> str: + """Human-facing completion message. Every mode shares the one-line status header; the + raw-output modes (all/result/error) append the bounded output tail under it instead of the + old bracketed ``[Background process proc_… finished~ …]`` debug wrapper (#54266).""" 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, - ) + command = _redact_gateway_user_facing_secrets(getattr(session, "command", "") or "") + if notify_mode == "concise": + return _format_concise_process_notification(session_id, command, session.exit_code, new_output, + duration_seconds=_dur) + header = _format_concise_process_notification(session_id, command, session.exit_code, "", duration_seconds=_dur) + return f"{header}\n\nFinal output:\n```\n{new_output.strip()}\n```" if new_output.strip() else header + + def _format_process_running_message(self, session) -> str: + from gateway.run import _redact_gateway_user_facing_secrets, _shorten_command_for_display + new_output = self._redacted_output_tail(session, 500) + short_cmd = _shorten_command_for_display(_redact_gateway_user_facing_secrets(getattr(session, "command", "") or "")) + header = "⏳ Background task still running" + (f" — `{short_cmd}`" if short_cmd else "") + return f"{header}\n\nRecent output:\n```\n{new_output.strip()}\n```" if new_output.strip() else header async def _run_process_watcher(self, watcher: dict) -> None: """Poll a background process and push updates until it exits. Mode @@ -1782,9 +1790,7 @@ class GatewayNotificationsMixin: elif has_new_output and notify_mode == "all" and not agent_notify: # 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) await self._send_watcher_message( - platform_name, chat_id, thread_id, - f"[Background process {session_id} is still running~ New output:\n{new_output}]", watcher, + platform_name, chat_id, thread_id, self._format_process_running_message(session), watcher, ) logger.debug("Process watcher ended%s: %s", " (silent)" if silent else "", session_id) diff --git a/website/docs/user-guide/messaging/index.md b/website/docs/user-guide/messaging/index.md index 96446f46e0..ea9a505e80 100644 --- a/website/docs/user-guide/messaging/index.md +++ b/website/docs/user-guide/messaging/index.md @@ -519,9 +519,9 @@ display: | Mode | What you receive | |------|-----------------| | `concise` | One-line status message on completion; failures append a short output tail (default) | -| `all` | Running-output updates **and** the final raw-output message | -| `result` | Only the final raw-output completion message (regardless of exit code) | -| `error` | Only the final raw-output message when the exit code is non-zero | +| `all` | Running-output updates **and** the final status message with the output tail | +| `result` | Only the final status message with the output tail (regardless of exit code) | +| `error` | Only the final status message with the output tail when the exit code is non-zero | | `off` | No process watcher messages at all | You can also set this via environment variable: