From a69b273f5fc4619f16e352a25084aa9f49b85bba Mon Sep 17 00:00:00 2001 From: KoalaTrapLord <265133236+KoalaTrapLord@users.noreply.github.com> Date: Fri, 3 Apr 2026 12:29:18 -0400 Subject: [PATCH] fix: always register policy routes at startup regardless of model load state Previously /api/policy/health returned 404 when the model wasn't loaded because register_policy_routes() was gated behind _policy_svc.load(). The health endpoint itself already handles the unloaded state gracefully (returns {ready: false, status: degraded}), so the router must be mounted unconditionally so ai-bridge.js can poll it during startup. --- lab_api.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lab_api.py b/lab_api.py index 88e9e60..b75927d 100644 --- a/lab_api.py +++ b/lab_api.py @@ -92,16 +92,20 @@ async def _lifespan(application: FastAPI): log.warning("RAG staleness monitor failed: %s", exc) asyncio.ensure_future(_rag_staleness_monitor()) - # Policy routes: register if ML policy model is available + # Policy routes: always register at startup so /api/policy/health is reachable + # even before the model has loaded. The health endpoint returns {ready: false, + # status: degraded} when the model is absent — that is the correct behaviour. + # Gating include_router() behind load() caused 404s that broke ai-bridge.js. try: from ml.serving.policy_server import PolicyInferenceService from routes.policy import register_policy_routes _policy_svc = PolicyInferenceService() + register_policy_routes(application, _policy_svc) # mount router unconditionally + log.info("Policy routes registered (/api/policy/*)") if _policy_svc.load(): - register_policy_routes(application, _policy_svc) - log.info("Policy routes registered (/api/policy/*)") + log.info("Policy model loaded successfully") else: - log.warning("Policy model not loaded — /api/policy/* routes inactive") + log.warning("Policy model not loaded — /api/policy/health will report degraded") except ImportError: log.info("Policy service not available (ml.serving not installed)") except Exception as e: