The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): count cron and API-server work in the scale-to-zero idle predicate
Ben Barclay743dc935f580
diff --git a/gateway/run.py b/gateway/run.pyindex 8340ae80eb6..55a212235ce 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -8672,8 +8672,17 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewdef _scale_to_zero_is_idle(self) -> bool:from gateway.scale_to_zero import is_idle+ # _active_work_count(), NOT _running_agent_count(): cron jobs run+ # through a standalone AIAgent on the scheduler's own thread pool and+ # API-server runs live on the adapter — both entirely outside+ # _running_agents (the same blind spot as the #60432 shutdown drain).+ # Counting agents alone let the idle predicate read True DURING a+ # running cron job; a suspend then freezes the job mid-flight.+ # Observed live on staging 2026-08-20: is_idle held True throughout a+ # cron run at 10:45:04-22 — only watcher-tick timing (next tick 9s+ # after the job finished) avoided a mid-job freeze.return is_idle(- running_agent_count=self._running_agent_count(),+ running_agent_count=self._active_work_count(),seconds_since_last_inbound=time.time() - self._last_inbound_at,idle_timeout_seconds=self._scale_to_zero_idle_timeout_seconds(),has_live_background_work=self._scale_to_zero_has_live_background_work(),diff --git a/tests/gateway/test_scale_to_zero_watcher.py b/tests/gateway/test_scale_to_zero_watcher.pyindex f4fc4fdb23b..35094df99b5 100644--- a/tests/gateway/test_scale_to_zero_watcher.py+++ b/tests/gateway/test_scale_to_zero_watcher.py@@ -415,3 +415,48 @@ async def test_heartbeat_poll_task_does_not_block_idle():finally:r._heartbeat_poll_task.cancel()await asyncio.gather(r._heartbeat_poll_task, return_exceptions=True)+++# ── in-flight cron / API-server work must block suspend (the 10:45 near-miss) ──+#+# Cron jobs run on the scheduler's thread pool and API-server runs live on the+# adapter — both outside _running_agents (the #60432 blind spot). The idle+# predicate must consume _active_work_count() (agents + cron + api runs), or a+# suspend can freeze a cron job mid-run: observed on staging 2026-08-20, where+# is_idle held True throughout a live cron run and only tick timing saved it.+++def _work_count_runner(monkeypatch, *, agents=0, cron_ids=(), api_runs=0):+ from types import SimpleNamespace++ r = GatewayRunner.__new__(GatewayRunner)+ r._running = True+ r._running_agents = {f"a{i}": object() for i in range(agents)}+ r._background_tasks = set()+ r._last_inbound_at = 0.0 # inbound-quiet for hours+ monkeypatch.setattr(+ r, "_scale_to_zero_idle_timeout_seconds", lambda: 300.0, raising=False+ )+ monkeypatch.setattr(+ "cron.scheduler.get_running_job_ids", lambda: set(cron_ids)+ )+ api_adapter = SimpleNamespace(active_agent_work_count=lambda: api_runs)+ from gateway.platforms.base import Platform++ r.adapters = {Platform.API_SERVER: api_adapter}+ return r+++def test_running_cron_job_blocks_idle(monkeypatch):+ r = _work_count_runner(monkeypatch, cron_ids={"job1"})+ assert r._scale_to_zero_is_idle() is False+++def test_active_api_run_blocks_idle(monkeypatch):+ r = _work_count_runner(monkeypatch, api_runs=1)+ assert r._scale_to_zero_is_idle() is False+++def test_idle_true_when_all_work_sources_quiet(monkeypatch):+ r = _work_count_runner(monkeypatch)+ assert r._scale_to_zero_is_idle() is True