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.
This commit is contained in:
KoalaTrapLord committed 2026-04-03 12:29:18 -04:00
1 parent e55ac9aebf
commit a69b273f5f
1 file changed
+8 -4
+8 -4
View File
@@ -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: