Skip to content

The agent that grows with you

fix(gateway): a refused process completion backs off for minutes, then sends the plain notice

kshitijk4poor963af5d62e94
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py
index b219f0325db..0a053911909 100644
--- a/gateway/run_notifications.py
+++ b/gateway/run_notifications.py
@@ -17,6 +17,7 @@ from pathlib import Path
from typing import Any, Dict, Optional, cast
from agent.i18n import t
+from agent.retry_utils import jittered_backoff
from gateway.config import Platform, _BUILTIN_PLATFORM_VALUES
from gateway.platforms.base import BasePlatformAdapter, _mark_notify_metadata
from gateway.platforms.event import MessageEvent, MessageType
@@ -63,7 +64,8 @@ _IMAGE_EXTS = {'.jpg', '.jpeg', '.png', '.webp', '.gif'}
# Storage causes that clear on their own (one session's lease/compression, not the store): the
# home-channel notice appends the operator restart tail for every OTHER cause.
_SELF_CLEARING_STORAGE_CAUSES = frozenset({"compression", "compression_closed", "turn_lease"})
-_MAX_COMPLETION_INJECTION_ATTEMPTS = 3
+# Process-watcher retries of a refused completion: ~5 min at the default 5 s check interval.
+_MAX_COMPLETION_INJECTION_ATTEMPTS = 8
_MAX_COMPLETION_INJECTION_RETRY_SECONDS = 60
# Durable async-delegation claim transitions: kind -> (tools.async_delegation function, failure log).
@@ -1727,30 +1729,29 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotifica
# turn itself installs the guard).
turn_busy = await self._launching_turn_active(platform_name, watcher)
delivered = await self._enqueue_process_completion_notification(synth_text, completion_evt)
- if delivered is False:
- completion_attempts += 1
- if completion_attempts >= _MAX_COMPLETION_INJECTION_ATTEMPTS:
- logger.warning(
- "Process watcher: completion for %s was refused after %d injection attempts; "
- "stopping retries",
- session_id, completion_attempts,
- )
- break
- # The process remains terminal. Retry a refused admission with bounded
- # exponential backoff so a disconnected adapter cannot spin the gateway loop.
- retry_interval = min(
- max(retry_interval, 1) * 2, _MAX_COMPLETION_INJECTION_RETRY_SECONDS)
+ if delivered is not False:
+ # The agent normally reports the result itself, so the chat gets no separate
+ # receipt. While the launching turn is still running the injection only queues a
+ # follow-up, and the chat would stay mute for as long as that turn lasts
+ # (#112033): send the concise receipt now.
+ if turn_busy and (notify_mode in {"concise", "all", "result"} or (
+ notify_mode == "error" and session.exit_code not in {0, None}
+ )):
+ message_text = self._format_process_final_message(session_id, session, "concise")
+ await self._send_watcher_message(platform_name, chat_id, thread_id, message_text, watcher)
+ break
+ # A refusal may clear (adapter reconnecting, store draining), so retry with backoff
+ # for a few minutes, then fall through to the plain notice instead of dropping it.
+ completion_attempts += 1
+ if completion_attempts < _MAX_COMPLETION_INJECTION_ATTEMPTS:
+ retry_interval = jittered_backoff(
+ completion_attempts, base_delay=max(interval, 1) * 2,
+ max_delay=_MAX_COMPLETION_INJECTION_RETRY_SECONDS, jitter_ratio=0.0)
continue
- # The agent normally reports the result itself, so the chat gets no separate receipt.
- # While the launching turn is still running the injection only queues a follow-up, and
- # the chat would stay mute for as long as that turn lasts (#112033): send the concise
- # receipt now.
- if turn_busy and (notify_mode in {"concise", "all", "result"} or (
- notify_mode == "error" and session.exit_code not in {0, None}
- )):
- message_text = self._format_process_final_message(session_id, session, "concise")
- await self._send_watcher_message(platform_name, chat_id, thread_id, message_text, watcher)
- break
+ logger.warning(
+ "Process watcher: completion for %s was refused %d times; sending the plain "
+ "completion notice instead", session_id, completion_attempts,
+ )
# Text-only notification; skip when already consumed via wait/log (the agent_notify branch
# FALLS THROUGH here, hence the re-check).
if process_registry.is_completion_consumed(session_id):
diff --git a/tests/gateway/test_background_process_notifications.py b/tests/gateway/test_background_process_notifications.py
index 4d26de5f4d6..fda10733dc5 100644
--- a/tests/gateway/test_background_process_notifications.py
+++ b/tests/gateway/test_background_process_notifications.py
@@ -496,25 +496,32 @@ class TestConciseFormatter:
@pytest.mark.asyncio
-async def test_agent_notify_stops_after_bounded_failed_completion_admissions(monkeypatch, tmp_path):
- """A refused completion injection must not poll an exited process forever (#138940)."""
+async def test_agent_notify_refused_completion_backs_off_then_sends_plain_notice(monkeypatch, tmp_path):
+ """A refused completion injection must not poll an exited process forever, and must not be
+ dropped either: bounded backoff, then the plain completion notice (#138940)."""
import tools.process_registry as pr_module
+ from gateway import run_notifications as rn
exited = SimpleNamespace(
output_buffer="done\n", exited=True, exit_code=0, command="echo done", started_at=None,
)
- monkeypatch.setattr(pr_module, "process_registry", _FakeRegistry([exited] * 4, consumed=False))
+ monkeypatch.setattr(pr_module, "process_registry", _FakeRegistry([exited] * 20, consumed=False))
+ sleeps = []
- async def _instant_sleep(*_a, **_kw):
- pass
- monkeypatch.setattr(asyncio, "sleep", _instant_sleep)
+ async def _record_sleep(delay, *_a, **_kw):
+ sleeps.append(delay)
+ monkeypatch.setattr(asyncio, "sleep", _record_sleep)
runner = _build_runner(monkeypatch, tmp_path, "concise")
runner._enqueue_process_completion_notification = AsyncMock(return_value=False)
- await runner._run_process_watcher({**_watcher_dict(), "notify_on_complete": True})
+ await runner._run_process_watcher({**_watcher_dict(), "check_interval": 5, "notify_on_complete": True})
- assert runner._enqueue_process_completion_notification.await_count == 3
+ assert runner._enqueue_process_completion_notification.await_count == rn._MAX_COMPLETION_INJECTION_ATTEMPTS
+ assert sleeps == [5, 10, 20, 40, 60, 60, 60, 60]
+ adapter = runner.adapters[Platform.TELEGRAM]
+ adapter.send.assert_awaited_once()
+ assert adapter.send.await_args.args[1].startswith("✅ Background task finished")
@pytest.mark.asyncio