The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a completion whose last refusal committed adds no second notice
kshitijk4poordb41bf1722da
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.pyindex 0a053911909..8248d48153a 100644--- a/gateway/run_notifications.py+++ b/gateway/run_notifications.py@@ -66,7 +66,7 @@ _IMAGE_EXTS = {'.jpg', '.jpeg', '.png', '.webp', '.gif'}_SELF_CLEARING_STORAGE_CAUSES = frozenset({"compression", "compression_closed", "turn_lease"})# 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+_COMPLETION_INJECTION_RETRY_MAX_DELAY = 60# Durable async-delegation claim transitions: kind -> (tools.async_delegation function, failure log)._DURABLE_CLAIM_OPS = {@@ -1746,11 +1746,23 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotificaif 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)+ max_delay=_COMPLETION_INJECTION_RETRY_MAX_DELAY, jitter_ratio=0.0)continue+ if (getattr(self, 'session_authority', None) is not None+ and self._completion_delivery_identity(completion_evt) is not None):+ from gateway.session_automation import completion_admission+ try:+ committed = completion_admission(self, completion_evt) is not None+ except Exception:+ # Failing open sends the notice; a dead watcher would send nothing.+ logger.debug("Process watcher: admission recheck failed for %s", session_id,+ exc_info=True)+ committed = False+ if committed:+ break # The last refusal was a lost ACK: the turn is committed.logger.warning(- "Process watcher: completion for %s was refused %d times; sending the plain "- "completion notice instead", session_id, completion_attempts,+ "Process watcher: completion for %s was refused %d times; falling back to the "+ "notify_mode=%s notice", session_id, completion_attempts, notify_mode,)# Text-only notification; skip when already consumed via wait/log (the agent_notify branch# FALLS THROUGH here, hence the re-check).diff --git a/tests/gateway/test_background_process_notifications.py b/tests/gateway/test_background_process_notifications.pyindex fda10733dc5..acba3f17090 100644--- a/tests/gateway/test_background_process_notifications.py+++ b/tests/gateway/test_background_process_notifications.py@@ -496,14 +496,17 @@ class TestConciseFormatter:@pytest.mark.asyncio-async def test_agent_notify_refused_completion_backs_off_then_sends_plain_notice(monkeypatch, tmp_path):[email protected]("committed", [False, True, "recheck_fails"])+async def test_agent_notify_refused_completion_backs_off_then_sends_plain_notice(monkeypatch, tmp_path, committed):"""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)."""+ dropped either: bounded backoff, then the plain completion notice (#138940). A last refusal+ whose admission actually committed (lost ACK) must not add a second notice."""import tools.process_registry as pr_module+ from gateway import session_automationfrom gateway import run_notifications as rnexited = SimpleNamespace(- output_buffer="done\n", exited=True, exit_code=0, command="echo done", started_at=None,+ output_buffer="done\n", exited=True, exit_code=0, command="echo done", started_at=1.0,)monkeypatch.setattr(pr_module, "process_registry", _FakeRegistry([exited] * 20, consumed=False))sleeps = []@@ -514,14 +517,23 @@ async def test_agent_notify_refused_completion_backs_off_then_sends_plain_noticerunner = _build_runner(monkeypatch, tmp_path, "concise")runner._enqueue_process_completion_notification = AsyncMock(return_value=False)+ runner.session_authority = object()+ def _admission(_runner, _evt):+ if committed == "recheck_fails":+ raise RuntimeError("store closed")+ return {"admission_id": "committed"} if committed else None+ monkeypatch.setattr(session_automation, "completion_admission", _admission)await runner._run_process_watcher({**_watcher_dict(), "check_interval": 5, "notify_on_complete": True})assert runner._enqueue_process_completion_notification.await_count == rn._MAX_COMPLETION_INJECTION_ATTEMPTSassert 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")+ if committed is True:+ adapter.send.assert_not_awaited()+ else:+ adapter.send.assert_awaited_once()+ assert adapter.send.await_args.args[1].startswith("✅ Background task finished")@pytest.mark.asyncio