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.

refactor(transforms): deprecate unused compress_with_stats, extract_batch and align_for_cache (#4098)

Tejas Choprae4a1ec19be78
diff --git a/headroom/transforms/cache_aligner.py b/headroom/transforms/cache_aligner.py
index be481ebfe..a772b2bbf 100644
--- a/headroom/transforms/cache_aligner.py
+++ b/headroom/transforms/cache_aligner.py
@@ -30,6 +30,7 @@ import binascii
import json
import logging
import uuid as _uuid
+import warnings
from dataclasses import dataclass
from datetime import datetime
from typing import Any
@@ -395,9 +396,21 @@ def align_for_cache(
) -> tuple[list[dict[str, Any]], str]:
"""Convenience wrapper that runs detection and returns the unchanged messages.
- Kept as a stable public API; the second tuple element is the stable
- prefix hash for callers that want to track cache prefix drift.
+ The second tuple element is the stable prefix hash for callers that want
+ to track cache prefix drift.
+
+ .. deprecated::
+ Unused inside Headroom; will be removed in a future release. Use
+ ``CacheAligner(config).apply(messages, tokenizer)`` and read the
+ ``stable_prefix_hash:`` marker from ``markers_inserted``.
"""
+ warnings.warn(
+ "align_for_cache is deprecated and will be removed in a future release; use "
+ "CacheAligner(config).apply(messages, tokenizer) and read the stable_prefix_hash: "
+ "marker from markers_inserted.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
cfg = config or CacheAlignerConfig()
aligner = CacheAligner(cfg)
tokenizer = Tokenizer(EstimatingTokenCounter()) # type: ignore[arg-type]
diff --git a/headroom/transforms/diff_compressor.py b/headroom/transforms/diff_compressor.py
index 4d1e2984e..15b906fc4 100644
--- a/headroom/transforms/diff_compressor.py
+++ b/headroom/transforms/diff_compressor.py
@@ -21,6 +21,7 @@ fallback. Build it locally with `scripts/build_rust_extension.sh`
from __future__ import annotations
import logging
+import warnings
from dataclasses import dataclass
from typing import Any
@@ -159,7 +160,19 @@ class DiffCompressor:
etc.) alongside the result. Stats is the raw PyO3 wrapper — no
Python equivalent to mirror to. Typed as `Any` because the PyO3
class has no Python type stub.
+
+ .. deprecated::
+ Unused inside Headroom; will be removed in a future release. Use
+ :meth:`compress`. Unlike ``compress()``, this method does not write
+ the CCR entry to the compression store, so a retrieval marker in
+ its output may not resolve.
"""
+ warnings.warn(
+ "DiffCompressor.compress_with_stats is deprecated and will be removed in a future "
+ "release; use DiffCompressor.compress instead.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
r, stats = self._rust.compress_with_stats(content, context)
result = DiffCompressionResult(
compressed=r.compressed,
diff --git a/headroom/transforms/html_extractor.py b/headroom/transforms/html_extractor.py
index 2439a0e8a..4f89d1707 100644
--- a/headroom/transforms/html_extractor.py
+++ b/headroom/transforms/html_extractor.py
@@ -17,6 +17,7 @@ Uses trafilatura for robust extraction - it handles:
from __future__ import annotations
import logging
+import warnings
from dataclasses import dataclass, field
from typing import Any
@@ -187,12 +188,22 @@ class HTMLExtractor:
) -> list[HTMLExtractionResult]:
"""Extract content from multiple HTML pages.
+ .. deprecated::
+ Unused inside Headroom; will be removed in a future release.
+ Call :meth:`extract` for each page instead.
+
Args:
html_contents: List of (html, url) tuples.
Returns:
List of HTMLExtractionResult in same order as input.
"""
+ warnings.warn(
+ "HTMLExtractor.extract_batch is deprecated and will be removed in a future release; "
+ "call HTMLExtractor.extract for each page instead.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
return [self.extract(html, url) for html, url in html_contents]
diff --git a/tests/test_cache_aligner_detector_only.py b/tests/test_cache_aligner_detector_only.py
index 5d8a25c6c..73fbb3455 100644
--- a/tests/test_cache_aligner_detector_only.py
+++ b/tests/test_cache_aligner_detector_only.py
@@ -335,3 +335,17 @@ def test_no_regex_imported() -> None:
assert "re" not in mod.__dict__, (
"headroom.transforms.cache_aligner must not depend on the regex module"
)
+
+
+def test_align_for_cache_is_deprecated_and_still_works() -> None:
+ from headroom.transforms.cache_aligner import align_for_cache
+
+ messages = [
+ {"role": "system", "content": "You are a helpful assistant."},
+ {"role": "user", "content": "hi"},
+ ]
+ with pytest.warns(DeprecationWarning, match="align_for_cache is deprecated") as record:
+ out, stable_hash = align_for_cache(messages)
+ assert record[0].filename == __file__
+ assert out == messages
+ assert isinstance(stable_hash, str)
diff --git a/tests/test_transforms/test_diff_compressor_rust_parity.py b/tests/test_transforms/test_diff_compressor_rust_parity.py
index 3feac188c..a38e0d5ab 100644
--- a/tests/test_transforms/test_diff_compressor_rust_parity.py
+++ b/tests/test_transforms/test_diff_compressor_rust_parity.py
@@ -94,7 +94,8 @@ def test_compress_with_stats_returns_python_dataclass_and_pyo3_stats():
diff = (
"diff --git a/a.py b/a.py\n--- a/a.py\n+++ b/a.py\n@@ -1 +1 @@\n-old\n+new\n"
) + "# pad\n" * 60
- result, stats = DiffCompressor(DiffCompressorConfig()).compress_with_stats(diff)
+ with pytest.warns(DeprecationWarning, match="compress_with_stats is deprecated"):
+ result, stats = DiffCompressor(DiffCompressorConfig()).compress_with_stats(diff)
assert isinstance(result, DiffCompressionResult)
assert stats.input_lines >= 60
diff --git a/tests/test_transforms/test_html_extractor.py b/tests/test_transforms/test_html_extractor.py
index b5416fa5c..6869534e6 100644
--- a/tests/test_transforms/test_html_extractor.py
+++ b/tests/test_transforms/test_html_extractor.py
@@ -538,7 +538,8 @@ class TestBatchExtraction:
),
]
- results = extractor.extract_batch(pages)
+ with pytest.warns(DeprecationWarning, match="extract_batch is deprecated"):
+ results = extractor.extract_batch(pages)
assert len(results) == 3
assert all(isinstance(r, HTMLExtractionResult) for r in results)