Skip to content

The agent that grows with you

fix(gateway): bound failed completion notification retries

KoNit-K19c96fb8c3a3
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py
index 6a16787b69e..b219f0325db 100644
--- a/gateway/run_notifications.py
+++ b/gateway/run_notifications.py
@@ -63,6 +63,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
+_MAX_COMPLETION_INJECTION_RETRY_SECONDS = 60
# Durable async-delegation claim transitions: kind -> (tools.async_delegation function, failure log).
_DURABLE_CLAIM_OPS = {
@@ -1688,8 +1690,10 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotifica
session_id, interval, notify_mode, agent_notify)
silent = notify_mode == "off" and not agent_notify
last_output_len = 0
+ completion_attempts = 0
+ retry_interval = interval
while True:
- await asyncio.sleep(interval)
+ await asyncio.sleep(retry_interval)
session = process_registry.get(session_id)
if session is None:
break
@@ -1724,8 +1728,18 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotifica
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:
- # The process remains terminal; retry after failed adapter injection instead
- # of suppressing the result.
+ 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)
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
diff --git a/tests/gateway/test_background_process_notifications.py b/tests/gateway/test_background_process_notifications.py
index 51f5f39d784..4d26de5f4d6 100644
--- a/tests/gateway/test_background_process_notifications.py
+++ b/tests/gateway/test_background_process_notifications.py
@@ -495,6 +495,28 @@ class TestConciseFormatter:
assert len(text) < 200
+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)."""
+ import tools.process_registry as pr_module
+
+ 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))
+
+ async def _instant_sleep(*_a, **_kw):
+ pass
+ monkeypatch.setattr(asyncio, "sleep", _instant_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})
+
+ assert runner._enqueue_process_completion_notification.await_count == 3
+
+
@pytest.mark.asyncio
async def test_concise_mode_no_interim_output_updates(monkeypatch, tmp_path):
"""concise never pushes 'is still running~ New output' interim updates."""