Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat(readiness): M2 readiness enforcement scaffold (default OFF) (#9828)
Kian Jones4213f0c0da21
diff --git a/fern/openapi.json b/fern/openapi.jsonindex 9fbcf50dc..7e5c36067 100644--- a/fern/openapi.json+++ b/fern/openapi.json@@ -14752,7 +14752,7 @@"get": {"tags": ["health"],"summary": "Check Health",- "description": "Async health check endpoint.",+ "description": "Liveness endpoint; returns 200 when process is responsive.","operationId": "check_health","responses": {"200": {@@ -14768,6 +14768,26 @@}}},+ "/v1/ready/": {+ "get": {+ "tags": ["health"],+ "summary": "Check Readiness",+ "description": "Readiness endpoint gated by internal readiness state when enforcement is enabled.",+ "operationId": "check_readiness",+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/Health"+ }+ }+ }+ }+ }+ }+ },"/v1/providers/": {"get": {"tags": ["providers"],diff --git a/letta/server/rest_api/middleware/check_password.py b/letta/server/rest_api/middleware/check_password.pyindex f1217e94e..86f4c871d 100644--- a/letta/server/rest_api/middleware/check_password.py+++ b/letta/server/rest_api/middleware/check_password.py@@ -8,8 +8,16 @@ class CheckPasswordMiddleware(BaseHTTPMiddleware):self.password = passwordasync def dispatch(self, request, call_next):- # Exclude health check endpoint from password protection- if request.url.path in {"/v1/health", "/v1/health/", "/latest/health/"}:+ # Exclude health/readiness probe endpoints from password protection+ if request.url.path in {+ "/v1/health",+ "/v1/health/",+ "/latest/health/",+ "/v1/ready",+ "/v1/ready/",+ "/latest/ready",+ "/latest/ready/",+ }:return await call_next(request)if (diff --git a/letta/server/rest_api/routers/v1/health.py b/letta/server/rest_api/routers/v1/health.pyindex 6eaed0e58..e25ccec81 100644--- a/letta/server/rest_api/routers/v1/health.py+++ b/letta/server/rest_api/routers/v1/health.py@@ -1,6 +1,7 @@from typing import TYPE_CHECKINGfrom fastapi import APIRouter+from fastapi.responses import JSONResponsefrom letta import __version__from letta.schemas.health import Health@@ -8,13 +9,37 @@ from letta.schemas.health import Healthif TYPE_CHECKING:pass-router = APIRouter(prefix="/health", tags=["health"])+router = APIRouter(tags=["health"])+# States that should cause readiness to return 503 when enforcement is enabled.+_UNREADY_STATES = {"degraded", "degraded_dependency", "manual_disable", "warming"}[email protected]("/", response_model=Health, operation_id="check_health")+[email protected]("/health/", response_model=Health, operation_id="check_health")async def check_health():- """Async health check endpoint."""- return Health(- version=__version__,- status="ok",- )+ """Liveness endpoint; returns 200 when process is responsive."""+ return Health(version=__version__, status="ok")++[email protected]("/ready/", response_model=Health, operation_id="check_readiness")+async def check_readiness():+ """Readiness endpoint gated by internal readiness state when enforcement is enabled."""+ from letta.settings import readiness_settings++ if not readiness_settings.enforcement_enabled:+ return Health(version=__version__, status="ok")++ from letta.monitoring.readiness_state import get_readiness_state++ state = get_readiness_state()++ # During drain we optionally return 503 so k8s stops new routing before termination.+ if state == "draining":+ if readiness_settings.drain_returns_503:+ return JSONResponse(status_code=503, content={"version": __version__, "status": "draining"})+ return Health(version=__version__, status="ok")++ if state in _UNREADY_STATES:+ return JSONResponse(status_code=503, content={"version": __version__, "status": state})++ return Health(version=__version__, status="ok")diff --git a/letta/settings.py b/letta/settings.pyindex 55f40d19e..8b2e127e3 100644--- a/letta/settings.py+++ b/letta/settings.py@@ -594,6 +594,34 @@ class TelemetrySettings(BaseSettings):return "socket" in self.provider_trace_backends+class ReadinessSettings(BaseSettings):+ """Readiness enforcement configuration. All gates default to OFF (non-breaking)."""++ model_config = SettingsConfigDict(env_prefix="letta_readiness_", extra="ignore")++ # Master kill switch — enables health probe integration with readiness_state module.+ # When False (default), /v1/health/ always returns 200 regardless of internal state.+ enforcement_enabled: bool = Field(default=False, description="Gate /v1/health/ on internal readiness state.")++ # When True, /v1/health/ returns 503 during draining state (graceful shutdown signal to k8s).+ drain_returns_503: bool = Field(default=True, description="Return HTTP 503 when readiness state is 'draining'.")++ # When True, event loop lag above threshold triggers degraded_dependency state.+ event_loop_lag_gating_enabled: bool = Field(default=False, description="Gate readiness on event loop lag threshold.")+ event_loop_lag_threshold_ms: float = Field(+ default=5000.0, ge=100.0, description="Event loop lag threshold (ms) for degraded transition."+ )++ # Stabilization window prevents flapping: state must be consistently bad for this many seconds before degrading.+ degraded_stabilization_seconds: float = Field(+ default=30.0, ge=0.0, description="Seconds of sustained overload before transitioning to degraded_dependency."+ )+ # Recovery: state must be healthy for this long before returning to ready.+ recovery_stabilization_seconds: float = Field(+ default=15.0, ge=0.0, description="Seconds of sustained health before recovering from degraded_dependency."+ )++# singletonsettings = Settings(_env_parse_none_str="None")test_settings = TestSettings()@@ -602,3 +630,4 @@ tool_settings = ToolSettings()summarizer_settings = SummarizerSettings()log_settings = LogSettings()telemetry_settings = TelemetrySettings()+readiness_settings = ReadinessSettings()