Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(proxy): promote Kompress health after runtime load (#2402)

Rod Boev54526bc8586c
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.py
index 346125d8c..d7e698bfc 100644
--- a/headroom/proxy/server.py
+++ b/headroom/proxy/server.py
@@ -2578,7 +2578,47 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
**details,
}
+ def _kompress_health_routers() -> list[ContentRouter]:
+ routers: list[ContentRouter] = []
+ for pipeline in (proxy.anthropic_pipeline, proxy.openai_pipeline):
+ for transform in getattr(pipeline, "transforms", ()):
+ if (
+ isinstance(transform, ContentRouter)
+ and transform.config.enable_kompress
+ and all(transform is not item for item in routers)
+ ):
+ routers.append(transform)
+ return routers
+
+ def _reconcile_kompress_health() -> bool:
+ routers = _kompress_health_routers()
+ if not routers:
+ return False
+
+ compressors: list[Any] = []
+ for router in routers:
+ for name in ("_kompress", "_kompress_remote"):
+ compressor = getattr(router, name, None)
+ if compressor is not None and all(compressor is not item for item in compressors):
+ compressors.append(compressor)
+
+ for compressor in compressors:
+ try:
+ if not compressor.is_ready():
+ continue
+ backend = compressor.ready_backend()
+ if not backend:
+ continue
+ except Exception:
+ continue
+ if proxy.warmup.kompress.status == "loaded":
+ return True
+ proxy.warmup.kompress.mark_loaded(handle=compressor, backend=backend)
+ return True
+ return True
+
def _health_checks() -> dict[str, dict[str, Any]]:
+ kompress_enabled = _reconcile_kompress_health()
memory_status = (
proxy.memory_handler.health_status()
if proxy.memory_handler
@@ -2625,7 +2665,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
error=_upstream_check_cache["error"],
),
"kompress": _component_health(
- enabled=not config.disable_kompress,
+ enabled=kompress_enabled,
ready=proxy.warmup.kompress.status == "loaded",
backend=proxy.warmup.kompress.info.get("backend", None),
),
diff --git a/headroom/transforms/kompress_compressor.py b/headroom/transforms/kompress_compressor.py
index 596ec1b44..08f5d4cba 100644
--- a/headroom/transforms/kompress_compressor.py
+++ b/headroom/transforms/kompress_compressor.py
@@ -1177,6 +1177,11 @@ class KompressCompressor(Transform):
"""
return self.config.model_id in _kompress_cache
+ def ready_backend(self) -> str | None:
+ """Return the cached backend without triggering a load."""
+ entry = _kompress_cache.get(self.config.model_id)
+ return entry[2] if entry is not None else None
+
def ensure_background_load(self) -> None:
"""Kick off a one-shot, non-blocking background download of the model.
diff --git a/headroom/transforms/kompress_remote.py b/headroom/transforms/kompress_remote.py
index 8615e613a..92f0810ee 100644
--- a/headroom/transforms/kompress_remote.py
+++ b/headroom/transforms/kompress_remote.py
@@ -61,6 +61,9 @@ class RemoteKompressCompressor:
def is_ready(self) -> bool:
return True
+ def ready_backend(self) -> str | None:
+ return "remote"
+
def preload(self, *, allow_download: bool = True) -> str:
return "remote"
diff --git a/tests/test_proxy_health.py b/tests/test_proxy_health.py
index f771fdf5e..af459535e 100644
--- a/tests/test_proxy_health.py
+++ b/tests/test_proxy_health.py
@@ -1,9 +1,48 @@
+import pytest
from fastapi.testclient import TestClient
from headroom.proxy.models import ProxyConfig
from headroom.proxy.server import create_app
+class _ReadyCompressor:
+ def __init__(self, backend="onnx", ready=True, error=None):
+ self.backend = backend
+ self.ready = ready
+ self.error = error
+ self.calls = []
+
+ def is_ready(self):
+ self.calls.append("is_ready")
+ if self.error:
+ raise self.error
+ return self.ready
+
+ def ready_backend(self):
+ self.calls.append("ready_backend")
+ return self.backend
+
+
+def _health_app(monkeypatch, compressor=None, *, disabled=False, **config_kwargs):
+ monkeypatch.setenv("HEADROOM_SKIP_UPSTREAM_CHECK", "1")
+ app = create_app(
+ ProxyConfig(
+ optimize=False,
+ cache_enabled=False,
+ rate_limit_enabled=False,
+ disable_kompress=disabled,
+ **config_kwargs,
+ )
+ )
+ app.state.ready = True
+ proxy = app.state.proxy
+ proxy.http_client = object()
+ router = proxy.anthropic_pipeline.transforms[-1]
+ if compressor is not None:
+ router._kompress = compressor
+ return app, proxy
+
+
def test_readyz_excludes_kompress_from_aggregate_readiness(monkeypatch):
monkeypatch.setenv("HEADROOM_SKIP_UPSTREAM_CHECK", "1")
@@ -32,3 +71,183 @@ def test_readyz_excludes_kompress_from_aggregate_readiness(monkeypatch):
"status": "unhealthy",
"backend": None,
}
+
+
+def test_readyz_promotes_deferred_kompress_after_runtime_load(monkeypatch):
+ compressor = _ReadyCompressor()
+ app, proxy = _health_app(monkeypatch, compressor)
+ proxy.warmup.kompress.info["source_status"] = "deferred"
+
+ payload = TestClient(app).get("/readyz").json()
+
+ assert payload["checks"]["kompress"] == {
+ "enabled": True,
+ "ready": True,
+ "status": "healthy",
+ "backend": "onnx",
+ }
+
+
+def test_readyz_promotes_remote_kompress_backend(monkeypatch):
+ compressor = _ReadyCompressor(backend="remote")
+ app, proxy = _health_app(monkeypatch)
+ router = proxy.anthropic_pipeline.transforms[-1]
+ router._kompress = None
+ router._kompress_remote = compressor
+
+ payload = TestClient(app).get("/readyz").json()
+
+ assert payload["checks"]["kompress"]["backend"] == "remote"
+ assert payload["checks"]["kompress"]["ready"] is True
+
+
+def test_readyz_keeps_pending_kompress_unloaded(monkeypatch):
+ compressor = _ReadyCompressor(backend="onnx", ready=False)
+ app, proxy = _health_app(monkeypatch, compressor)
+ router = proxy.anthropic_pipeline.transforms[-1]
+ router._kompress = compressor
+
+ payload = TestClient(app).get("/readyz").json()
+
+ assert payload["checks"]["kompress"] == {
+ "enabled": True,
+ "ready": False,
+ "status": "unhealthy",
+ "backend": None,
+ }
+ assert compressor.calls == ["is_ready"]
+
+
+def test_readyz_never_starts_kompress_loading(monkeypatch):
+ compressor = _ReadyCompressor()
+ app, proxy = _health_app(monkeypatch, compressor)
+ router = proxy.anthropic_pipeline.transforms[-1]
+ router._kompress = compressor
+
+ TestClient(app).get("/readyz")
+
+ assert compressor.calls == ["is_ready", "ready_backend"]
+
+
+def test_readyz_kompress_inspection_failure_fails_open(monkeypatch):
+ compressor = _ReadyCompressor(error=RuntimeError("inspection failed"))
+ app, proxy = _health_app(monkeypatch, compressor)
+ proxy.warmup.kompress.mark_loaded(handle=object(), backend="onnx")
+
+ payload = TestClient(app).get("/readyz").json()
+
+ assert payload["checks"]["kompress"]["ready"] is True
+ assert payload["checks"]["kompress"]["backend"] == "onnx"
+
+
+def test_readyz_disabled_kompress_skips_inspection(monkeypatch):
+ compressor = _ReadyCompressor()
+ app, proxy = _health_app(monkeypatch, compressor, disabled=True)
+ router = proxy.anthropic_pipeline.transforms[-1]
+ router._kompress = compressor
+
+ payload = TestClient(app).get("/readyz").json()
+
+ assert payload["checks"]["kompress"] == {
+ "enabled": False,
+ "ready": True,
+ "status": "disabled",
+ "backend": None,
+ }
+ assert compressor.calls == []
+
+
+def test_readyz_per_provider_kompress_override_reenables_health(monkeypatch):
+ compressor = _ReadyCompressor()
+ app, proxy = _health_app(
+ monkeypatch,
+ disabled=True,
+ disable_kompress_anthropic=False,
+ )
+ router = proxy.anthropic_pipeline.transforms[-1]
+ router._kompress = compressor
+
+ payload = TestClient(app).get("/readyz").json()
+
+ assert payload["checks"]["kompress"] == {
+ "enabled": True,
+ "ready": True,
+ "status": "healthy",
+ "backend": "onnx",
+ }
+ assert compressor.calls == ["is_ready", "ready_backend"]
+
+
+def test_readyz_never_calls_lazy_kompress_getters(monkeypatch):
+ app, proxy = _health_app(monkeypatch)
+ router = proxy.anthropic_pipeline.transforms[-1]
+
+ def _boom():
+ raise AssertionError("health should not instantiate kompress")
+
+ router._get_kompress = _boom
+ router._get_remote_kompress = _boom
+
+ payload = TestClient(app).get("/readyz").json()
+
+ assert payload["checks"]["kompress"] == {
+ "enabled": True,
+ "ready": False,
+ "status": "unhealthy",
+ "backend": None,
+ }
+
+
+ ("slot_status", "compressor", "disabled", "expected"),
+ [
+ (
+ "null",
+ None,
+ False,
+ {"enabled": True, "ready": False, "status": "unhealthy", "backend": None},
+ ),
+ (
+ "null",
+ _ReadyCompressor(),
+ False,
+ {"enabled": True, "ready": True, "status": "healthy", "backend": "onnx"},
+ ),
+ (
+ "null",
+ _ReadyCompressor(backend="remote"),
+ False,
+ {"enabled": True, "ready": True, "status": "healthy", "backend": "remote"},
+ ),
+ (
+ "error",
+ _ReadyCompressor(),
+ False,
+ {"enabled": True, "ready": True, "status": "healthy", "backend": "onnx"},
+ ),
+ (
+ "loaded",
+ _ReadyCompressor(),
+ False,
+ {"enabled": True, "ready": True, "status": "healthy", "backend": "existing"},
+ ),
+ (
+ "null",
+ _ReadyCompressor(),
+ True,
+ {"enabled": False, "ready": True, "status": "disabled", "backend": None},
+ ),
+ ],
+)
+def test_readyz_kompress_state_matrix(monkeypatch, slot_status, compressor, disabled, expected):
+ app, proxy = _health_app(monkeypatch, compressor, disabled=disabled)
+ if slot_status == "error":
+ proxy.warmup.kompress.mark_error("not cached")
+ elif slot_status == "loaded":
+ proxy.warmup.kompress.mark_loaded(handle=object(), backend=expected["backend"])
+ if compressor is not None and expected["backend"] == "existing":
+ compressor.backend = "new"
+
+ payload = TestClient(app).get("/readyz").json()["checks"]["kompress"]
+
+ assert payload == expected