The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(cron): synchronize watchdog E2E on abort events
ethernet52fbf9001d4c
diff --git a/tests/cron/test_cron_direct_api_call_watchdog.py b/tests/cron/test_cron_direct_api_call_watchdog.pyindex ab8a716c40b..a82b635af07 100644--- a/tests/cron/test_cron_direct_api_call_watchdog.py+++ b/tests/cron/test_cron_direct_api_call_watchdog.py@@ -32,6 +32,7 @@ sys.modules.setdefault("fal_client", types.SimpleNamespace())import run_agent+import agent.chat_completion_helpers as chat_completion_helpersfrom agent.chat_completion_helpers import direct_api_call@@ -51,7 +52,7 @@ def _make_agent(*, stale_timeout, platform="cron"):return agent-def _stalling_client(agent, *, aborted, release_after=5.0):+def _stalling_client(agent, *, aborted):"""A client whose request blocks until the watchdog aborts its sockets."""fake_client = MagicMock()release_after_abort = threading.Event()@@ -63,8 +64,7 @@ def _stalling_client(agent, *, aborted, release_after=5.0):def _stalled_request(**_kwargs):# The provider accepted the request and went silent. The socket# shutdown is what unblocks it, exactly as in production.- if not release_after_abort.wait(timeout=release_after):- raise AssertionError("watchdog never aborted the stalled request")+ release_after_abort.wait()raise ConnectionError("socket shut down")fake_client.chat.completions.create.side_effect = _stalled_request@@ -78,14 +78,11 @@ def test_stalled_inline_call_is_aborted_and_raises_retryable_timeout():aborted: list[str] = []_stalling_client(agent, aborted=aborted)- started = time.time()with pytest.raises(TimeoutError) as excinfo:direct_api_call(agent, {"model": "m", "messages": []})- elapsed = time.time() - startedassert aborted == ["stale_call_kill"]assert "no response" in str(excinfo.value)- assert elapsed < 4.0, "watchdog did not bound the call"def test_watchdog_abort_never_surfaces_as_interrupted_error():@@ -207,11 +204,12 @@ class _StallingWireClient:self.chat = SimpleNamespace(completions=SimpleNamespace(create=self._create))self.responses = SimpleNamespace()self.close_calls = 0+ self.request_started = threading.Event()self.sockets_shut_down = threading.Event()def _create(self, **_kwargs):- if not self.sockets_shut_down.wait(timeout=5.0):- raise AssertionError("watchdog never shut the stalled request down")+ self.request_started.set()+ self.sockets_shut_down.wait()raise ConnectionError("socket shut down")def close(self):@@ -248,6 +246,39 @@ def test_e2e_cron_turn_is_bounded_through_the_real_agent_routing(monkeypatch):wire = _StallingWireClient()agent = _build_cron_agent(monkeypatch)agent.client = wire+ timer_intervals = []++ class RequestStartedTimer:+ """Run the real watchdog callback after dispatch reaches the wire.++ CI process starvation can delay either side of a sub-second Timer by+ tens of seconds. The event fixes their causal order without replacing+ the routing, timeout resolver, watchdog callback, or abort lifecycle.+ """++ def __init__(self, interval, function):+ timer_intervals.append(interval)+ self._function = function+ self._cancelled = threading.Event()+ self.name = None+ self.daemon = False++ def _run(self):+ wire.request_started.wait()+ if not self._cancelled.is_set():+ self._function()++ def start(self):+ threading.Thread(+ target=self._run, name=self.name, daemon=self.daemon+ ).start()++ def cancel(self):+ self._cancelled.set()++ threading_proxy = SimpleNamespace(**vars(threading))+ threading_proxy.Timer = RequestStartedTimer+ monkeypatch.setattr(chat_completion_helpers, "threading", threading_proxy)monkeypatch.setattr("agent.process_bootstrap.OpenAI", lambda **_kwargs: wire)monkeypatch.setattr(run_agent.AIAgent,@@ -255,12 +286,12 @@ def test_e2e_cron_turn_is_bounded_through_the_real_agent_routing(monkeypatch):lambda self, client: (client.sockets_shut_down.set(), 1)[1],)- started = time.time()with pytest.raises(TimeoutError):agent._interruptible_api_call({"model": agent.model, "messages": []})- elapsed = time.time() - started- assert elapsed < 4.0, "cron turn was not bounded by the watchdog"+ assert timer_intervals == [pytest.approx(0.3)]+ assert wire.request_started.is_set()+ assert wire.sockets_shut_down.is_set()# The aborted pool is poisoned, so the killed client is really closed# instead of being cached for the retry.assert wire.close_calls == 1