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(ccr): only refuse a bare marker, not originals that start and end with markers (#4626)

Rahul selvaraj398d7cdcf4f1
diff --git a/headroom/cache/compression_store.py b/headroom/cache/compression_store.py
index 7eb1eccd0..82aa693d2 100644
--- a/headroom/cache/compression_store.py
+++ b/headroom/cache/compression_store.py
@@ -71,6 +71,8 @@ _SECRET_KEY_VALUE_RE = re.compile(
)
_AUTH_VALUE_RE = re.compile(r"(?i)\b(Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{12,}")
_API_KEY_VALUE_RE = re.compile(r"\bsk-[A-Za-z0-9_-]{12,}\b")
+# Exactly one CCR marker and nothing else (see ``store`` and #2694, #4266).
+_BARE_CCR_MARKER_RE = re.compile(r"<<ccr:[^>\n]+>>")
def _get_env_default_ttl_seconds() -> int:
@@ -383,7 +385,9 @@ class CompressionStore:
# loudly rather than silently converting "retrievable" into "gone".
# Narrow by design: only a *bare* marker is rejected. Legitimate
# originals may legally CONTAIN markers (nested offloads, a tool that
- # echoed one), and refusing those would drop recoverable data.
+ # echoed one), and refusing those would drop recoverable data. That
+ # includes an original that starts with one marker and ends with
+ # another (#4266), so the check is a full match of a single marker.
#
# The rejected value is never echoed into the log. It is provably a
# bare marker here, but `original` is the store's credential-bearing
@@ -391,7 +395,7 @@ class CompressionStore:
# OAuth token), and an error path is exactly where that sort of leak
# survives review. `hash_key` already identifies the entry.
stripped = original.strip()
- if stripped.startswith("<<ccr:") and stripped.endswith(">>") and "\n" not in stripped:
+ if _BARE_CCR_MARKER_RE.fullmatch(stripped):
logger.error(
"CCR store: refusing to persist a bare retrieval marker as "
"original_content (hash=%s tool=%s strategy=%s len=%d) — the "
diff --git a/tests/test_compression_store.py b/tests/test_compression_store.py
index 9f709d3b6..24a079e54 100644
--- a/tests/test_compression_store.py
+++ b/tests/test_compression_store.py
@@ -551,6 +551,36 @@ class TestCompressionStoreRetrieve:
entry = store.retrieve("nonexistent_hash_key")
assert entry is None
+ @pytest.mark.parametrize(
+ "original",
+ [
+ "<<ccr:abcd>> Keep this original instruction and the next reference <<ccr:ef01>>",
+ "<<ccr:abcd>><<ccr:ef01>>",
+ "<<ccr:abcd,base64,2.0KB>> then <<ccr:ef01 5_rows_offloaded>>",
+ ],
+ )
+ def test_original_with_separate_markers_is_stored(self, store: CompressionStore, original):
+ """An original that starts and ends with two different markers is content (#4266)."""
+ hash_key = store.store(original=original, compressed="x")
+
+ entry = store.retrieve(hash_key)
+
+ assert entry is not None
+ assert entry.original_content == original
+
+ @pytest.mark.parametrize(
+ "marker", ["<<ccr:abcd>>", " <<ccr:abcd,base64,2.0KB>>\n", "<<ccr:abcd 5_rows_offloaded>>"]
+ )
+ def test_bare_marker_is_refused_and_keeps_existing_entry(self, store: CompressionStore, marker):
+ """A bare marker is never stored, even over an existing entry (#2694)."""
+ hash_key = store.store(original="real bytes", compressed="x", explicit_hash="abcd")
+
+ assert store.store(original=marker, compressed="x", explicit_hash="abcd") == hash_key
+
+ entry = store.retrieve(hash_key)
+ assert entry is not None
+ assert entry.original_content == "real bytes"
+
def test_retrieve_expired_entry_returns_none(self, store_with_short_ttl: CompressionStore):
"""retrieve() returns None for expired entry."""
hash_key = store_with_short_ttl.store(original="[1]", compressed="[]")