The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): re-ask the scale-to-zero gate before every dormant sequence
teknium12bd0fa4138ca
diff --git a/gateway/run.py b/gateway/run.pyindex 2ed27576d8e..27401a7a7e6 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -3667,6 +3667,7 @@ class GatewayRunner(# the clock; and a one-shot latch so the "platform owns the suspend" notice logs once.self._scale_to_zero_cooldown_until: float = 0.0self._scale_to_zero_no_suspend_logged: bool = False+ self._scale_to_zero_direct_platform_logged: bool = Falsedef _open_session_db_for_active_scope(self, raise_on_error: bool = False) -> Any:"""AsyncSessionDB for the active profile scope, resolved per access (not in ``__init__``) sincediff --git a/gateway/run_shutdown.py b/gateway/run_shutdown.pyindex 3b7e53a50ad..19c14965494 100644--- a/gateway/run_shutdown.py+++ b/gateway/run_shutdown.py@@ -335,6 +335,9 @@ class GatewayShutdownMixin:``self.config`` belongs to the launch profile, while ``_profile_adapters`` holdslive adapters for multiplexed secondary profiles. A direct secondary connectionmust block suspension just like a direct primary connection.++ config.platforms is pre-seeded with disabled placeholders, and the api_server is+ force-enabled on every hosted container (counting it silently disarmed the feature)."""non_messaging = {Platform.LOCAL, Platform.API_SERVER, Platform.WEBHOOK}active = []@@ -451,6 +454,7 @@ class GatewayShutdownMixin:sees only INBOUND connections and would freeze mid-job. Without a flaps socket NAS brokersthe stop through the stamped GATEWAY_RELAY_SLEEP_URL; with no lever at all the watcherabstains."""+ from gateway.scale_to_zero import messaging_is_relay_only_or_absentawait asyncio.sleep(min(interval, 30.0)) # let startup settlewhile self._running:try:@@ -459,6 +463,22 @@ class GatewayShutdownMixin:returnif time.time() < self._scale_to_zero_cooldown_until or not self._scale_to_zero_is_idle():continue+ # The arm gate ran once at boot. A direct adapter that came up since (profile+ # reconcile hot-adding a secondary, a re-enabled platform) owns a socket no wake+ # URL can revive, so the same gate is re-asked before every dormant sequence.+ active = self._scale_to_zero_active_messaging_platforms()+ if not messaging_is_relay_only_or_absent(active):+ if not self._scale_to_zero_direct_platform_logged:+ self._scale_to_zero_direct_platform_logged = True+ logger.info(+ "scale-to-zero: idle, but directly connected messaging platform(s) %s "+ "hold a live socket that a suspended instance cannot wake from — staying "+ "awake. Route them through the relay connector or disable them to allow "+ "suspend.",+ ", ".join(str(getattr(p, "value", p)) for p in active),+ )+ continue+ self._scale_to_zero_direct_platform_logged = Falsego_dormant = getattr(self._relay_adapter_for_dormancy(), "go_dormant", None)if not callable(go_dormant):continuediff --git a/tests/gateway/test_scale_to_zero.py b/tests/gateway/test_scale_to_zero.pyindex 05d620fbc7c..d94ba3250cd 100644--- a/tests/gateway/test_scale_to_zero.py+++ b/tests/gateway/test_scale_to_zero.py@@ -81,6 +81,70 @@ def test_scale_to_zero_gate_accounts_for_secondary_profile_direct_adapter(monkeyassert runner._scale_to_zero_should_arm() is False+def test_watcher_reasks_gate_before_dormant_sequence(monkeypatch):+ # Arming is a boot-time snapshot. A direct adapter hot-added afterwards (profile reconcile)+ # must stop the dormant sequence; with relay only, the same tick still reaches go_dormant.+ import asyncio+ import time++ from gateway.config import GatewayConfig, Platform, PlatformConfig+ from gateway.run_shutdown import GatewayShutdownMixin++ monkeypatch.setenv("GATEWAY_RELAY_SLEEP_URL", "https://sleep.example.test/instance")++ class Relay:+ def __init__(self):+ self.calls = []++ def hold_redial(self):+ return True++ def release_redial(self):+ return True++ async def go_dormant(self):+ self.calls.append("go_dormant")+ return False # refuse the ack: the tick must abandon before any suspend call++ class Runner(GatewayShutdownMixin):+ def __init__(self):+ self.config = GatewayConfig(platforms={Platform.RELAY: PlatformConfig(enabled=True)})+ self.adapters = {Platform.RELAY: Relay()}+ self._profile_adapters = {}+ self._running = True+ self._running_agents = {}+ self._background_tasks = set()+ self._last_inbound_at = time.time() - 3600+ self._scale_to_zero_cooldown_until = 0.0+ self._scale_to_zero_no_suspend_logged = False+ self._scale_to_zero_direct_platform_logged = False++ def _running_agent_count(self):+ return 0++ def _update_runtime_status(self, state):+ pass++ async def one_tick(runner):+ task = asyncio.ensure_future(runner._scale_to_zero_watcher(interval=0.0))+ await asyncio.sleep(0.05)+ runner._running = False+ task.cancel()+ try:+ await task+ except asyncio.CancelledError:+ pass++ hot_added = Runner()+ hot_added._profile_adapters = {"imessage": {Platform("photon"): object()}}+ asyncio.run(one_tick(hot_added))+ assert hot_added.adapters[Platform.RELAY].calls == []++ relay_only = Runner()+ asyncio.run(one_tick(relay_only))+ assert relay_only.adapters[Platform.RELAY].calls == ["go_dormant"]++# ── should_arm (D1/D11/§3.4(1)) ──────────────────────────────────────────────