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(compression): report source-line span in CCR compression marker (#2597)

TenderDeve18e1c3c9badc
diff --git a/headroom/transforms/kompress_compressor.py b/headroom/transforms/kompress_compressor.py
index 0ee2ff2e6..0f5f0c91d 100644
--- a/headroom/transforms/kompress_compressor.py
+++ b/headroom/transforms/kompress_compressor.py
@@ -1452,8 +1452,14 @@ class KompressCompressor(Transform):
cache_key = self._store_in_ccr(ccr_source, compressed, ccr_source_tokens)
if cache_key:
result.cache_key = cache_key
+ # Report the source line span so a reader can tell content was
+ # compressed away rather than absent — "items" counts words, which
+ # does not map to lines and reads as evidence of absence (#2586).
+ source_lines = ccr_source.count("\n") + 1
+ line_word = "line" if source_lines == 1 else "lines"
result.compressed += (
- f"\n[{n_words} items compressed to {compressed_count}."
+ f"\n[{n_words} items compressed to {compressed_count}"
+ f" (from {source_lines} source {line_word})."
f" Retrieve more: hash={cache_key}]"
)
@@ -1809,8 +1815,14 @@ class KompressCompressor(Transform):
cache_key = self._store_in_ccr(ccr_source, compressed, ccr_source_tokens)
if cache_key:
result.cache_key = cache_key
+ # Report the source line span so a reader can tell content was
+ # compressed away rather than absent — "items" counts words, which
+ # does not map to lines and reads as evidence of absence (#2586).
+ source_lines = ccr_source.count("\n") + 1
+ line_word = "line" if source_lines == 1 else "lines"
result.compressed += (
- f"\n[{n_words} items compressed to {compressed_count}."
+ f"\n[{n_words} items compressed to {compressed_count}"
+ f" (from {source_lines} source {line_word})."
f" Retrieve more: hash={cache_key}]"
)
diff --git a/headroom/transforms/kompress_remote.py b/headroom/transforms/kompress_remote.py
index 92f0810ee..d3a5db1af 100644
--- a/headroom/transforms/kompress_remote.py
+++ b/headroom/transforms/kompress_remote.py
@@ -129,9 +129,14 @@ class RemoteKompressCompressor:
cache_key = store_kompress_in_ccr(content, compressed, result.original_tokens)
if cache_key:
result.cache_key = cache_key
+ # Report the source line span so a reader can tell content was
+ # compressed away rather than absent (#2586).
+ source_lines = content.count("\n") + 1
+ line_word = "line" if source_lines == 1 else "lines"
result.compressed += (
f"\n[{result.original_tokens} items compressed to "
- f"{result.compressed_tokens}. Retrieve more: hash={cache_key}]"
+ f"{result.compressed_tokens} (from {source_lines} source {line_word})."
+ f" Retrieve more: hash={cache_key}]"
)
return result
diff --git a/tests/test_ccr_marker_policy.py b/tests/test_ccr_marker_policy.py
index a9a0e033c..09d89d592 100644
--- a/tests/test_ccr_marker_policy.py
+++ b/tests/test_ccr_marker_policy.py
@@ -27,6 +27,13 @@ def test_has_new_ccr_markers_filters_replayed_forwarded_markers() -> None:
)
+def test_source_line_span_marker_is_still_detected() -> None:
+ # The compressor annotates the count with a source-line span (#2586); the
+ # retrieval hash must still be extracted from the enriched marker.
+ marker = "[122 items compressed to 27 (from 5 source lines). Retrieve more: hash=c00eb437e5e5c00eb437e5e5]"
+ assert _hashes(marker) == ["c00eb437e5e5c00eb437e5e5"]
+
+
def test_has_new_ccr_markers_detects_hash_not_seen_in_previous_forward() -> None:
old = "[100 items compressed to 10. Retrieve more: hash=abc123def456abc123def456]"
new = "[50 items compressed to 5. Retrieve more: hash=deadbeefdeadbeefdeadbeef]"