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.

test: stop three sources of cross-test pollution on main (#3774)

Tejas Chopra21d166b77a96
diff --git a/tests/conftest.py b/tests/conftest.py
index e9db73f7e..df58299dd 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -2,7 +2,9 @@
# CRITICAL: Must be set before ANY imports that could trigger sentence_transformers
# The Rust tokenizers use parallelism that deadlocks with pytest-asyncio
+import logging
import os
+import sys
os.environ["TOKENIZERS_PARALLELISM"] = "false"
@@ -441,3 +443,84 @@ def sample_request_metrics():
turns_dropped=0,
messages_hash="def456",
)
+
+
[email protected](autouse=True)
+def _isolate_kompress_model_cache():
+ """Keep one test's loaded Kompress model out of every later test's ``/readyz``.
+
+ ``KompressCompressor.preload()`` stores the loaded handle in a MODULE-LEVEL
+ dict keyed by model id (``kompress_compressor._kompress_cache``). Tests that
+ exercise preload patch the *loader* to return a fake model, but the fake is
+ still written to that real dict under the real ``HF_MODEL_ID`` -- and
+ nothing ever removed it.
+
+ The proxy health endpoint then found it. ``_reconcile_kompress_health``
+ asks each pipeline's ContentRouter for an already-instantiated compressor
+ and calls ``is_ready()`` / ``ready_backend()``, both of which are reads of
+ that same module dict, so a *brand-new* proxy in a later test reported
+ ``kompress: {ready: true, backend: "onnx", status: "healthy"}`` when nothing
+ had been loaded. Four ``tests/test_proxy_health.py`` cases failed in a full
+ run and passed in isolation, which is the signature of exactly this.
+
+ Snapshot/restore rather than an unconditional clear: a test that installs
+ its own cache (several ``monkeypatch.setattr(kc, "_kompress_cache", ...)``
+ do) must keep it for its own duration.
+
+ Costs nothing for the ~13k tests that never touch ML: the module is only
+ consulted through ``sys.modules``, so this never imports it.
+ """
+ module_name = "headroom.transforms.kompress_compressor"
+ before = sys.modules.get(module_name)
+ # None when the module has not been imported yet, so anything found at
+ # teardown was put there by this test.
+ snapshot = dict(getattr(before, "_kompress_cache", {})) if before is not None else None
+
+ yield
+
+ after = sys.modules.get(module_name)
+ if after is None:
+ return
+ cache = getattr(after, "_kompress_cache", None)
+ if cache is None:
+ return
+ cache.clear()
+ if snapshot:
+ cache.update(snapshot)
+
+
[email protected](autouse=True)
+def _detach_leaked_proxy_log_handler():
+ """Stop one test's proxy file logger from following the whole session around.
+
+ ``_setup_file_logging`` attaches a named handler to the ``headroom`` logger
+ and nothing detaches it, so any test that builds a proxy app leaves it in
+ place for every later test. That broke
+ ``test_runtime_log_refuses_a_symlinked_path``, which asserts no such handler
+ is attached after a refused setup: the handler it found was a LEFTOVER from
+ ``tests/gateway/test_compress_turn_seam.py``, not one the refused call
+ created, so the test reported a security regression that had not happened.
+
+ The leaked handler also pointed at ``~/.headroom/logs/proxy-8787.log`` --
+ the developer's real home directory, not a tmp_path -- so the leak was
+ writing outside the test sandbox as well. That part is worth fixing at the
+ source; this only stops it leaking forward.
+
+ Detach rather than close-and-keep: a handler attached during a test belongs
+ to that test's app, and its file may live in a ``tmp_path`` that is about to
+ disappear underneath it.
+ """
+ yield
+
+ helpers = sys.modules.get("headroom.proxy.helpers")
+ handler_name = getattr(helpers, "_PROXY_LOG_HANDLER_NAME", None)
+ if handler_name is None:
+ return
+ logger = logging.getLogger("headroom")
+ for handler in list(logger.handlers):
+ if getattr(handler, "name", None) == handler_name:
+ logger.removeHandler(handler)
+ try:
+ handler.close()
+ except Exception: # pragma: no cover - a closed/rotated file is fine
+ pass
diff --git a/tests/test_transforms/test_dense_line_elider.py b/tests/test_transforms/test_dense_line_elider.py
index dda8a7eb7..c00f58ff3 100644
--- a/tests/test_transforms/test_dense_line_elider.py
+++ b/tests/test_transforms/test_dense_line_elider.py
@@ -63,11 +63,32 @@ def test_router_applies_elision_when_no_compressor_wins():
assert "content elided" not in off.compress(html, context="tool_result").compressed
+# These exercise the DENSE-LINE ELIDER, so they must pin the router to it.
+# Without `enable_html_extractor=False` the strategy chosen depends on whether
+# the optional `html` extra is installed: with `trafilatura` present the router
+# picks HTMLExtractor instead, which strips <script>/<style> and returns only
+# the visible text -- so the assertions below were silently testing a different
+# compressor. CI does not install that extra (it runs `--extra proxy`), so both
+# tests passed there and failed in any full-extras checkout, including at the
+# commit that introduced them.
+#
+# NOTE the reason they fail under HTMLExtractor is a real defect, not just a
+# routing surprise: on `<html><head><script>{3.2kB}</script></head>
+# <body>hi there</body></html>` it emits `hi there` -- 3232 chars to 8, logged
+# as 99.8% savings -- with NO `Retrieve original: hash=` marker, so the script
+# is unrecoverable through CCR. With an empty <body> its output is blank, the
+# router rejects it ("must never blank a non-empty block") and falls back to
+# elision, which is why only the prose cases fail. Tracked separately; pinning
+# the strategy here keeps that bug from hiding inside an elider test.
def test_elided_block_carries_a_retrievable_marker():
from headroom.cache.compression_store import get_compression_store
html = "<html><head><script>" + MINIFIED_JS + "</script></head><body>hi there</body></html>"
- router = ContentRouter(ContentRouterConfig(enable_kompress=False, min_section_tokens=10))
+ router = ContentRouter(
+ ContentRouterConfig(
+ enable_kompress=False, enable_html_extractor=False, min_section_tokens=10
+ )
+ )
out = router.compress(html, context="tool_result").compressed
assert "Retrieve original: hash=" in out
key = out.rsplit("hash=", 1)[1].rstrip("]\n")
@@ -96,7 +117,13 @@ def test_messages_path_keeps_elided_tool_result(tokenizer):
{"role": "assistant", "content": "ok"},
{"role": "user", "content": "and now?"},
]
- router = ContentRouter(ContentRouterConfig(enable_kompress=False, min_section_tokens=10))
+ # Same strategy pin as the test above: the elider, not whichever compressor
+ # the installed extras happen to make available.
+ router = ContentRouter(
+ ContentRouterConfig(
+ enable_kompress=False, enable_html_extractor=False, min_section_tokens=10
+ )
+ )
result = router.apply(messages, tokenizer)
block = result.messages[2]["content"][0]["content"]
text = block if isinstance(block, str) else block[0]["text"]