The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): the fire startup budget runs from handler entry, so a slow token verify cannot outlast the forwarder
kshitijk4poor500ad4decc98
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.pyindex 6b6efc08576..615824ec612 100644--- a/gateway/platforms/api_server.py+++ b/gateway/platforms/api_server.py@@ -3984,6 +3984,9 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):"""POST /api/cron/fire — Chronos fire webhook (NAS -> agent), authenticated by aNAS-minted JWT via the pluggable verifier, NOT API_SERVER_KEY. 202 + background run soa long turn never trips NAS's timeout; the store CAS claim guards double-fire on retry."""+ # The startup wait's budget runs from here: a slow JWKS fetch must not push it past the+ # dashboard forwarder's timeout (see api_server_fire_startup).+ received_at = asyncio.get_running_loop().time()from hermes_cli.config import cfg_get, load_configfrom plugins.cron_providers.chronos.verify import get_fire_verifierauth = request.headers.get("Authorization", "")@@ -4037,7 +4040,7 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):from gateway.run import _gateway_runner_refrunner = _gateway_runner_ref()from gateway.platforms.api_server_fire_startup import refuse_until_started- refusal = await refuse_until_started(runner, job_id)+ refusal = await refuse_until_started(runner, job_id, received_at=received_at)if refusal is not None:return refusal# Live adapters (parity with the built-in ticker): E2EE / relay-fronted platformsdiff --git a/gateway/platforms/api_server_fire_startup.py b/gateway/platforms/api_server_fire_startup.pyindex 788da192254..7403a10d954 100644--- a/gateway/platforms/api_server_fire_startup.py+++ b/gateway/platforms/api_server_fire_startup.py@@ -14,20 +14,23 @@ from typing import Any, Optionalfrom aiohttp import web-# How long a fire that lands during startup waits before it is refused as retryable. Kept under the-# dashboard forwarder's 10s timeout: past it NAS sees a 503 while this handler still runs the job, and-# the retry would run it twice.+# How long a fire may spend in the handler (token verify included) before a still-starting gateway is+# refused as retryable. Kept under the dashboard forwarder's 10s timeout: past it NAS sees a 503 while+# this handler still runs the job, and the retry would run it twice.FIRE_STARTUP_WAIT_SECONDS = 7.0_POLL_SECONDS = 0.1-async def refuse_until_started(runner: Any, job_id: str) -> Optional["web.Response"]:+async def refuse_until_started(runner: Any, job_id: str, *, received_at: float) -> Optional["web.Response"]:"""None once ``runner`` has finished starting (``_running``: every platform connect attempted and itsadapters published), or when there is no runner to ask (a self-hosted api_server, a test double).Otherwise the retryable 503, worded like the dashboard's own "gateway unreachable" so NAS classifies- it the same, with the dashboard's 60s Retry-After (``_CRON_FIRE_RETRY_AFTER_SECONDS``)."""+ it the same, with the dashboard's 60s Retry-After (``_CRON_FIRE_RETRY_AFTER_SECONDS``).++ ``received_at`` is the loop time the handler was entered: time already spent verifying the token+ counts against the budget."""loop = asyncio.get_running_loop()- deadline = loop.time() + FIRE_STARTUP_WAIT_SECONDS+ deadline = received_at + FIRE_STARTUP_WAIT_SECONDSwhile runner is not None and not getattr(runner, "_running", True):if loop.time() >= deadline:return web.json_response(diff --git a/tests/gateway/test_cron_fire_startup_gate.py b/tests/gateway/test_cron_fire_startup_gate.pyindex abeb62ca3e6..48d63df74fa 100644--- a/tests/gateway/test_cron_fire_startup_gate.py+++ b/tests/gateway/test_cron_fire_startup_gate.py@@ -113,3 +113,28 @@ async def test_started_gateway_is_accepted_immediately(adapter, provider):resp = await _post_fire(adapter, runner)assert resp.status == 202+++async def test_slow_token_verify_spends_the_startup_budget(adapter, provider, monkeypatch):+ """The budget runs from handler entry: a slow JWKS fetch plus a full wait would outlast the dashboard+ forwarder's timeout, NAS would retry while this handler still ran the job, and it would run twice."""+ monkeypatch.setattr(api_server_fire_startup, "FIRE_STARTUP_WAIT_SECONDS", 1.5)++ async def _slow_verifier(**_kw):+ await asyncio.sleep(2.0)+ return {"purpose": "cron_fire"}++ monkeypatch.setattr("plugins.cron_providers.chronos.verify.get_fire_verifier", lambda: _slow_verifier)+ runner = SimpleNamespace(_draining=False, _external_drain_active=False, _running=False, adapters={})++ async def _finish_startup(): # inside a wait measured from the gate, outside one measured from entry+ await asyncio.sleep(2.5)+ runner._running = True++ finisher = asyncio.ensure_future(_finish_startup())+ resp = await _post_fire(adapter, runner)+ await finisher++ assert resp.status == 503+ assert provider.claimed == [] and provider.fired == []