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): detect retrieval markers with an "Expires in Nm." suffix after the hash (#3138)

Abhay Singh12a26b8f0ced
diff --git a/headroom/ccr/tool_injection.py b/headroom/ccr/tool_injection.py
index 86f6cac3f..0ec9cf21e 100644
--- a/headroom/ccr/tool_injection.py
+++ b/headroom/ccr/tool_injection.py
@@ -224,6 +224,15 @@ class CCRToolInjector:
# redeem (silent data loss, #1006). Match the load-bearing
# "Retrieve original: hash=" phrase directly.
re.compile(r"Retrieve original: hash=([a-f0-9]{12,24})"),
+ # CodeCompressor (and any marker that appends "Expires in Nm.]" or
+ # uses "N tokens compressed." rather than "compressed to M"):
+ # `[128 tokens compressed. ... Retrieve more: hash=xxx. Expires in
+ # 30m.]`. The bracket patterns above anchor the hash on a trailing
+ # `]`, so the hash=...`. Expires` suffix (and the missing "to M")
+ # makes them all miss it -- the same silent-data-loss failure as
+ # #1006. Match the load-bearing "Retrieve more: hash=" phrase
+ # directly, matching how parser.py / session_probes detect it.
+ re.compile(r"Retrieve more: hash=([a-f0-9]{12,24})"),
]
)
diff --git a/tests/test_ccr_tool_injection.py b/tests/test_ccr_tool_injection.py
index e5e5b5a76..3dac71069 100644
--- a/tests/test_ccr_tool_injection.py
+++ b/tests/test_ccr_tool_injection.py
@@ -98,6 +98,32 @@ class TestCCRToolInjector:
assert ccr_hash in hashes
assert injector.has_compressed_content
+ def test_scan_detects_code_compressor_marker(self):
+ """CodeCompressor emits `[N tokens compressed. ... Retrieve more:
+ hash=xxx. Expires in Nm.]` — it says 'tokens compressed' (not
+ 'compressed to M') and puts '. Expires in Nm.' between the hash and the
+ closing ']'. The bracket patterns anchor the hash on a trailing ']', so
+ they all miss it and the retrieve tool is never offered for
+ code-compressed content — the same silent-data-loss failure as #1006.
+ The 'Retrieve more: hash=' phrase must be detected directly."""
+ ccr_hash = "abc123def456abc123def456" # 24 hex chars (SHA-256[:24])
+ messages = [
+ {
+ "role": "tool",
+ "content": (
+ "def process(items):\n ...\n"
+ "# [128 tokens compressed. 3 function bodies elided. "
+ f"Retrieve more: hash={ccr_hash}. Expires in 30m.]"
+ ),
+ },
+ ]
+
+ injector = CCRToolInjector()
+ hashes = injector.scan_for_markers(messages)
+
+ assert ccr_hash in hashes
+ assert injector.has_compressed_content
+
def test_scan_for_markers_multiple_hashes(self):
"""Scanner finds multiple distinct hashes."""
messages = [