The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(gateway): drive the watch-drain backoff clock without moving the loop's
Kurt VanDusen849bf6097178
diff --git a/gateway/run_notifications_ownership.py b/gateway/run_notifications_ownership.pyindex 9a78b487d6b..d03f57b68bc 100644--- a/gateway/run_notifications_ownership.py+++ b/gateway/run_notifications_ownership.py@@ -144,7 +144,8 @@ class GatewayNotificationOwnershipMixin:The session authority admits a notice only into its pinned session or that session'scompression continuation, so after /new it refuses the notice on every retry and the drainwould spin on it (#141562). This is the completion pre-flight's verdict, applied after the- refusal: a /resume reopens the pinned session, which delivers again.+ refusal. A dropped notice is gone for good; a /resume reopens the pinned session, so the+ process's later notices are admitted again."""pinned = str(evt.get("parent_session_id") or "").strip()if not pinned:diff --git a/tests/gateway/test_background_process_notifications.py b/tests/gateway/test_background_process_notifications.pyindex edf57f12ccc..d03460aa836 100644--- a/tests/gateway/test_background_process_notifications.py+++ b/tests/gateway/test_background_process_notifications.py@@ -17,6 +17,8 @@ from unittest.mock import AsyncMockimport pytest+from tests.module_clock import install_module_clock+from gateway.config import GatewayConfig, Platformfrom gateway.run import GatewayRunner, _parse_session_keyfrom gateway.run_notifications import INTERNAL_NOTIFICATION_FOOTER@@ -342,19 +344,18 @@ async def test_post_turn_watch_drain_refused_event_backs_off_then_drops(monkeypacompletion_queue = queue.Queue()completion_queue.put(_watch_event())- clock = {"now": 1000.0}- monkeypatch.setattr(run_notifications.time, "monotonic", lambda: clock["now"])+ clock = install_module_clock(monkeypatch, run_notifications)attempt_times: list[float] = []async def refused(self, synth_text, evt, **kwargs):- attempt_times.append(clock["now"])+ attempt_times.append(clock.now)return Falsemonkeypatch.setattr(run_notifications.GatewayNotificationsMixin, "_inject_watch_notification", refused)for _ in range(500):await runner._drain_watch_notifications(completion_queue)- clock["now"] += 2.0+ clock.now += 2.0if completion_queue.empty():break@@ -382,8 +383,7 @@ async def test_post_turn_watch_drain_transient_refusal_recovers_without_drop(moncompletion_queue = queue.Queue()completion_queue.put(_watch_event())- clock = {"now": 1000.0}- monkeypatch.setattr(run_notifications.time, "monotonic", lambda: clock["now"])+ clock = install_module_clock(monkeypatch, run_notifications)flaky = {"refusals_left": 2}async def flaky_inject(self, synth_text, evt, **kwargs):@@ -396,7 +396,7 @@ async def test_post_turn_watch_drain_transient_refusal_recovers_without_drop(monfor _ in range(500):await runner._drain_watch_notifications(completion_queue)- clock["now"] += 2.0+ clock.now += 2.0if completion_queue.empty():breakdiff --git a/tests/gateway/test_completion_delivery.py b/tests/gateway/test_completion_delivery.pyindex 514dbfcd298..e10d8468a20 100644--- a/tests/gateway/test_completion_delivery.py+++ b/tests/gateway/test_completion_delivery.py@@ -7,10 +7,8 @@ state (when available) is acknowledged through its authoritative SQLite API."""import asyncio-import itertoolsimport jsonimport queue-import timefrom collections import OrderedDictfrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock, patch@@ -21,6 +19,7 @@ from gateway import run_notificationsfrom gateway.config import Platformfrom gateway.run import GatewayRunnerfrom tools.process_registry import ProcessRegistry, ProcessSession+from tests.module_clock import install_module_clockclass AdmittingHandler(AsyncMock):@@ -990,8 +989,7 @@ def test_watch_drain_retries_transport_failure(monkeypatch, isolated_registry):isolated_registry.completion_queue.put(evt)_stop_after_sleeps(monkeypatch, runner, count=3)# The refused event backs off (#140899); each read is a minute later, so the retry is due.- real, minutes = time.monotonic, itertools.count()- monkeypatch.setattr(run_notifications.time, "monotonic", lambda: real() + 60 * next(minutes))+ install_module_clock(monkeypatch, run_notifications, step=60)asyncio.run(runner._async_delegation_watcher(interval=0))assert adapter.handle_message.await_count == 2assert isolated_registry.completion_queue.empty()diff --git a/tests/gateway/test_watch_notification_reset_pin.py b/tests/gateway/test_watch_notification_reset_pin.pyindex 8f43fc7790d..e02be41db8a 100644--- a/tests/gateway/test_watch_notification_reset_pin.py+++ b/tests/gateway/test_watch_notification_reset_pin.py@@ -3,10 +3,10 @@ forever: the session authority refuses it ``admission_conflict`` on every drainReal GatewayRunner, DiscordAdapter, SessionAuthority and drain over SQLite; no agent turn runs."""import loggingimport queue-import timeimport pytest+from tests.module_clock import install_module_clockfrom tests.waiting import async_wait_until@@ -135,10 +135,7 @@ async def test_unproven_refusal_requeues_with_backoff_until_the_cap(monkeypatch,try:pending = queue.Queue()pending.put(_notice(entry, await _foreign_pin(runner)))- # Only ever forward: the event loop keeps its own timers on the same clock.- skew = {'s': 0.0}- real = time.monotonic- monkeypatch.setattr(run_notifications.time, 'monotonic', lambda: real() + skew['s'])+ clock = install_module_clock(monkeypatch, run_notifications)with caplog.at_level(logging.WARNING, logger='gateway.run'):await runner._drain_watch_notifications(pending)@@ -146,7 +143,7 @@ async def test_unproven_refusal_requeues_with_backoff_until_the_cap(monkeypatch,for _ in range(200):if pending.empty():break- skew['s'] += 60+ clock.now += 60await runner._drain_watch_notifications(pending)messages = [r.getMessage() for r in caplog.records]diff --git a/tests/module_clock.py b/tests/module_clock.pynew file mode 100644index 00000000000..25afbff5df3--- /dev/null+++ b/tests/module_clock.py@@ -0,0 +1,26 @@+"""One module's view of ``time`` with a test-driven ``monotonic``.++Patching ``time.monotonic`` itself also moves the running event loop's clock (its timers read the+same function); this swaps only the module's ``time`` binding, so the loop keeps real time."""+import time+++class ModuleClock:+ """``time`` for one module: ``monotonic()`` returns ``now``, advanced by ``step`` per read."""++ def __init__(self, now: float, step: float):+ self.now, self.step = now, step++ def monotonic(self) -> float:+ value = self.now+ self.now += self.step+ return value++ def __getattr__(self, name):+ return getattr(time, name)+++def install_module_clock(monkeypatch, module, *, now: float = 1000.0, step: float = 0.0) -> ModuleClock:+ clock = ModuleClock(now, step)+ monkeypatch.setattr(module, "time", clock)+ return clock