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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(kompress): degrade when a native dep is installed but unloadable (#3133)
gglucass8f3d677354f5
diff --git a/headroom/transforms/kompress_compressor.py b/headroom/transforms/kompress_compressor.pyindex 64cffc12f..9bea434f5 100644--- a/headroom/transforms/kompress_compressor.py+++ b/headroom/transforms/kompress_compressor.py@@ -23,6 +23,7 @@ import reimport threadingimport timefrom dataclasses import dataclass+from functools import lru_cachefrom typing import Any, Literalfrom ..config import TransformResult@@ -494,6 +495,7 @@ def _kompress_content_signature(content: str) -> Any:)+@lru_cache(maxsize=1)def _is_onnx_available() -> bool:"""Check if ONNX Runtime is available (lightweight, no torch needed)."""try:@@ -501,10 +503,13 @@ def _is_onnx_available() -> bool:import transformers # noqa: F401return True- except ImportError:+ except (ImportError, OSError):+ # OSError: the package is installed but its native library will not+ # load. See _is_pytorch_available for the full case.return False+@lru_cache(maxsize=1)def _is_pytorch_available() -> bool:"""Check if full PyTorch stack is available (requires [ml] extra)."""try:@@ -513,7 +518,14 @@ def _is_pytorch_available() -> bool:import transformers # noqa: F401return True- except ImportError:+ except (ImportError, OSError):+ # An installed-but-unloadable native dependency raises OSError, not+ # ImportError: on Windows without the MSVC redistributable, importing+ # torch fails with WinError 126 loading c10.dll. Catching only+ # ImportError let that escape a probe whose contract is to return a+ # bool, and callers guard the call with `except ImportError` too, so it+ # propagated into the request path instead of degrading to "no+ # kompress".return Falsediff --git a/tests/test_transforms/test_kompress_compressor.py b/tests/test_transforms/test_kompress_compressor.pyindex 669923dfb..948e9c529 100644--- a/tests/test_transforms/test_kompress_compressor.py+++ b/tests/test_transforms/test_kompress_compressor.py@@ -39,11 +39,59 @@ class TestLazyImports:_is_pytorch_available,)+ # The probe memoises, so a real answer from an earlier call would+ # otherwise mask the patch above.+ _is_pytorch_available.cache_clear()# Without both torch and onnxruntime, should return Falseassert _is_pytorch_available() is False+ _is_pytorch_available.cache_clear()# Note: is_kompress_available() may still return True if onnxruntime# was already imported before patching. Test the individual checkers.+ def test_unloadable_native_dep_degrades_instead_of_raising(self) -> None:+ """An installed-but-unloadable torch raises OSError, not ImportError.++ On Windows without the MSVC redistributable, `import torch` fails with+ WinError 126 loading c10.dll. The probe caught only ImportError, so the+ OSError escaped a function whose contract is to return a bool -- and the+ request-path gate in content_router guards with `except ImportError`+ too, so it propagated into request handling instead of degrading to+ "kompress unavailable".+ """+ import builtins++ from headroom.transforms import kompress_compressor as kmod++ kmod._is_onnx_available.cache_clear()+ kmod._is_pytorch_available.cache_clear()++ real_import = builtins.__import__+ attempts = {"torch": 0}++ def fake_import(name: str, *args: object, **kwargs: object) -> object:+ if name in ("onnxruntime", "optimum"):+ raise ImportError(f"No module named {name!r}")+ if name == "torch":+ attempts["torch"] += 1+ raise OSError(+ "[WinError 126] The specified module could not be found. "+ 'Error loading "torch\\lib\\c10.dll" or one of its dependencies.'+ )+ return real_import(name, *args, **kwargs)++ with patch.object(builtins, "__import__", fake_import):+ # Must degrade, not raise.+ assert kmod._is_pytorch_available() is False+ assert kmod.is_kompress_available() is False+ # Cached: a failing import is not retried per request. Python drops+ # failed modules from sys.modules, so nothing memoises it for us.+ for _ in range(5):+ assert kmod.is_kompress_available() is False+ assert attempts["torch"] == 1++ kmod._is_onnx_available.cache_clear()+ kmod._is_pytorch_available.cache_clear()+def test_dataclasses_importable_without_torch(self) -> None:"""KompressConfig, KompressResult, KompressCompressor are importable without torch."""from headroom.transforms.kompress_compressor import (