mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
## Description
`CCRToolInjector.scan_for_markers` decides whether the
`headroom_retrieve` tool is offered to the model, by extracting
redeemable hashes from CCR markers in the conversation. Its bracket
patterns anchor the hash on a trailing `]` and expect the phrase
"compressed to M":
```python
re.compile(r"\[(\d+) \w+ compressed to (\d+)\. Retrieve more: hash=([a-f0-9]{24})\]"),
re.compile(r"\[.*?compressed.*?hash=([a-f0-9]{24})\]", re.IGNORECASE),
```
`CodeCompressor` (wired into `ContentRouter`, so this is a live pipeline
path) emits a differently-shaped marker:
```
# [128 tokens compressed. 3 function bodies elided. Retrieve more: hash=<24hex>. Expires in 30m.]
```
Two things make every bracket pattern miss it:
1. it says "N tokens compressed." rather than "compressed to M", and
2. the hash is followed by ". Expires in Nm." before the closing `]`, so
the `hash=(...)\]` anchor never matches.
`CodeCompressor` stores the original under exactly that hash
(`_store_in_ccr` -> `store.store(...)` returns `SHA-256[:24]`), so the
content is retrievable — but because `scan_for_markers` never detects
the marker, the retrieve tool is not offered for code-compressed content
and the model is left a `Retrieve more: hash=...` instruction it cannot
redeem. This is the same silent-data-loss failure mode the "Retrieve
original: hash=" pattern was added to fix (#1006).
The fix adds a pattern that matches the load-bearing `Retrieve more:
hash=<12-24 hex>` phrase directly, regardless of the surrounding wording
or a trailing "Expires" clause — mirroring how
`transforms/content_router.py` (`_ALREADY_COMPRESSED_MARKERS`),
`parser.py`, and `evals/session_probes.py` already detect this phrase by
substring. `_scan_text` dedups across patterns, and `verify_ownership`
still gates every detected hash on `store.exists(...)`, so this cannot
adopt a non-Headroom marker.
Reproduction:
```python
from headroom.ccr.tool_injection import CCRToolInjector
h = "abc123def456abc123def456" # 24 hex, as CodeCompressor emits
marker = f"# [128 tokens compressed. Retrieve more: hash={h}. Expires in 30m.]"
CCRToolInjector().scan_for_markers([{"role": "tool", "content": marker}])
# before: [] after: ['abc123def456abc123def456']
```
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `headroom/ccr/tool_injection.py`: added `re.compile(r"Retrieve more:
hash=([a-f0-9]{12,24})")` to `_marker_patterns`, paralleling the
existing `Retrieve original: hash=` pattern, with a comment explaining
the CodeCompressor / Expires-suffix case.
- `tests/test_ccr_tool_injection.py`: added
`test_scan_detects_code_compressor_marker`.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
### Test Output
```text
tests/test_ccr_tool_injection.py -> 48 passed
uvx ruff@0.16.2 check headroom/ccr/tool_injection.py tests/test_ccr_tool_injection.py -> All checks passed!
uvx mypy@1.20.2 headroom/ccr/tool_injection.py -> Success: no issues found
```
## Real Behavior Proof
- Environment: Windows 11, Python 3.12.11, project venv, pytest 9.1.1,
ruff 0.16.2 and mypy 1.20.2 via uvx.
- Exact command / steps: ran `scan_for_markers` against a
CodeCompressor-shaped marker directly (returned `[]` before the fix);
removed the new pattern and ran the new test (`python -m pytest
tests/test_ccr_tool_injection.py -k code_compressor`) -> failed with
`assert 'abc123def456abc123def456' in []`; restored the pattern and ran
the file -> 48 passed. Also confirmed a kompress-style marker (`...
Retrieve more: hash=<h>]`) and the `<<ccr:...>>` / "Retrieve original"
markers still detect.
- Observed result: before the fix a CodeCompressor marker yields no
detected hashes (retrieve tool not offered); after the fix its hash is
detected and, since the store holds it, `headroom_retrieve` can redeem
it.
- Not tested: an end-to-end proxy turn through ContentRouter ->
CodeCompressor -> next-turn injection; the detection seam
(`scan_for_markers`) that gates injection is exercised directly.
## Runtime Rollout Safety
- Rollout-managed feature(s): none. This is CCR marker detection, not a
rollout-channel-gated runtime feature.
- Minimum rollout channel: N/A.
- Stable/default behavior changed: `scan_for_markers` now also detects
the `Retrieve more: hash=` phrase directly, so code-compressed markers
become redeemable. Existing bracket / `<<ccr:>>` / `Retrieve original:`
detection is unchanged (additive pattern, deduped). `verify_ownership`
still filters to hashes the store actually holds.
- Kill switch / disable path: N/A.
- Unsafe override required: no.
- Qualification impact: the retrieve tool is now offered for
code-compressed content that previously produced an unredeemable marker.
- Rollback path: revert this PR; the new pattern is removed.
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation (N/A:
internal marker detection)
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`
## Additional Notes
Relationship to the open feature PR #1881 (terse `[ccr:<id>]` markers):
I checked its diff to be sure this is not a duplicate. #1881 changes the
bracket patterns' hash character-class to `CCR_HEX_ID_INNER` but keeps
the `hash=(...)\]` anchor, and it leaves CodeCompressor emitting the
`hash=<id>. Expires in Nm.]` form — so the marker in this report is
still undetected after #1881, and #1881 adds no direct `Retrieve more:
hash=` pattern. This fix is a small, targeted correctness change to the
current code; if #1881 lands first I am happy to rebase the one-line
pattern onto its refactored `_marker_patterns` list.
Co-authored-by: Tejas Chopra <chopratejas@gmail.com>
824 lines
30 KiB
Python
824 lines
30 KiB
Python
"""Tests for CCR tool injection and MCP integration."""
|
|
|
|
import json
|
|
|
|
from headroom.ccr import (
|
|
CCR_TOOL_NAME,
|
|
CCRToolInjector,
|
|
create_ccr_tool_definition,
|
|
create_system_instructions,
|
|
parse_tool_call,
|
|
)
|
|
|
|
|
|
class _AlwaysOwnStore:
|
|
"""Stub compression store for verify_ownership() (issue #2836) in tests
|
|
that only exercise injection plumbing with hand-typed marker hashes,
|
|
not real CompressionStore-backed storage.
|
|
"""
|
|
|
|
def exists(self, hash_key: str, clean_expired: bool = False) -> bool:
|
|
return True
|
|
|
|
|
|
class TestCCRToolDefinition:
|
|
"""Test tool definition creation for different providers."""
|
|
|
|
def test_anthropic_format(self):
|
|
"""Anthropic tool definition has correct format."""
|
|
tool = create_ccr_tool_definition("anthropic")
|
|
|
|
assert tool["name"] == CCR_TOOL_NAME
|
|
assert "description" in tool
|
|
assert "input_schema" in tool
|
|
assert tool["input_schema"]["type"] == "object"
|
|
assert "hash" in tool["input_schema"]["properties"]
|
|
# Retrieval is by hash only — no query/search parameter.
|
|
assert "query" not in tool["input_schema"]["properties"]
|
|
assert tool["input_schema"]["required"] == ["hash"]
|
|
|
|
def test_openai_format(self):
|
|
"""OpenAI tool definition has correct format."""
|
|
tool = create_ccr_tool_definition("openai")
|
|
|
|
assert tool["type"] == "function"
|
|
assert tool["function"]["name"] == CCR_TOOL_NAME
|
|
assert "description" in tool["function"]
|
|
assert "parameters" in tool["function"]
|
|
assert tool["function"]["parameters"]["required"] == ["hash"]
|
|
|
|
def test_google_format(self):
|
|
"""Google tool definition has correct format."""
|
|
tool = create_ccr_tool_definition("google")
|
|
|
|
assert tool["name"] == CCR_TOOL_NAME
|
|
assert "parameters" in tool
|
|
assert tool["parameters"]["required"] == ["hash"]
|
|
|
|
|
|
class TestCCRToolInjector:
|
|
"""Test CCRToolInjector functionality."""
|
|
|
|
def test_scan_for_markers_finds_hash(self):
|
|
"""Scanner detects compression markers in messages."""
|
|
messages = [
|
|
{"role": "user", "content": "Find errors"},
|
|
{
|
|
"role": "tool",
|
|
"content": '[{"id": 1}]\n[100 items compressed to 10. Retrieve more: hash=abc123def456abc123def456]',
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector()
|
|
hashes = injector.scan_for_markers(messages)
|
|
|
|
assert len(hashes) == 1
|
|
assert "abc123def456abc123def456" in hashes
|
|
assert injector.has_compressed_content
|
|
|
|
def test_scan_detects_read_lifecycle_stale_marker(self):
|
|
"""A read_lifecycle STALE marker carries a retrievable CCR hash via the
|
|
'Retrieve original: hash=' phrase but never says 'compressed', so the
|
|
other patterns miss it. The injector must still detect it, or the
|
|
retrieve tool is not offered and the marker is unredeemable (#1006)."""
|
|
ccr_hash = "a1b2c3d4e5f6a1b2c3d4e5f6" # 24 hex chars (SHA-256[:24])
|
|
messages = [
|
|
{
|
|
"role": "tool",
|
|
"content": (
|
|
"[Read content stale: app.py was modified after this read — "
|
|
f"re-read the file for current content. Retrieve original: hash={ccr_hash}]"
|
|
),
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector()
|
|
hashes = injector.scan_for_markers(messages)
|
|
|
|
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 = [
|
|
{
|
|
"role": "tool",
|
|
"content": "[50 items compressed to 5. Retrieve more: hash=aaa111111111aaa111111111]",
|
|
},
|
|
{
|
|
"role": "tool",
|
|
"content": "[200 items compressed to 20. Retrieve more: hash=bbb222222222bbb222222222]",
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector()
|
|
hashes = injector.scan_for_markers(messages)
|
|
|
|
assert len(hashes) == 2
|
|
assert "aaa111111111aaa111111111" in hashes
|
|
assert "bbb222222222bbb222222222" in hashes
|
|
|
|
def test_scan_no_duplicates(self):
|
|
"""Scanner deduplicates repeated hashes."""
|
|
messages = [
|
|
{
|
|
"role": "tool",
|
|
"content": "[100 items compressed to 10. Retrieve more: hash=aabbcc123456aabbcc123456]",
|
|
},
|
|
{
|
|
"role": "assistant",
|
|
"content": "I see [100 items compressed to 10. Retrieve more: hash=aabbcc123456aabbcc123456]",
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector()
|
|
hashes = injector.scan_for_markers(messages)
|
|
|
|
assert len(hashes) == 1
|
|
|
|
def test_scan_anthropic_content_blocks(self):
|
|
"""Scanner handles Anthropic's content block format."""
|
|
messages = [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "Find errors"},
|
|
],
|
|
},
|
|
{
|
|
"role": "assistant",
|
|
"content": [
|
|
{
|
|
"type": "tool_result",
|
|
"content": "[100 items compressed to 10. Retrieve more: hash=b10cf0a2b3c4b10cf0a2b3c4]",
|
|
},
|
|
],
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector()
|
|
hashes = injector.scan_for_markers(messages)
|
|
|
|
assert "b10cf0a2b3c4b10cf0a2b3c4" in hashes
|
|
|
|
def test_inject_tool_when_compression_detected(self):
|
|
"""Tool is injected when compression markers are found."""
|
|
messages = [
|
|
{
|
|
"role": "tool",
|
|
"content": "[100 items compressed to 10. Retrieve more: hash=abc123def456abc123def456]",
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector(provider="anthropic")
|
|
injector.scan_for_markers(messages)
|
|
tools, was_injected = injector.inject_tool_definition(None)
|
|
|
|
assert was_injected
|
|
assert len(tools) == 1
|
|
assert tools[0]["name"] == CCR_TOOL_NAME
|
|
|
|
def test_inject_tool_adds_to_existing(self):
|
|
"""CCR tool is added to existing tools list."""
|
|
messages = [
|
|
{
|
|
"role": "tool",
|
|
"content": "[100 items compressed to 10. Retrieve more: hash=e1e2e3f4f5f6e1e2e3f4f5f6]",
|
|
},
|
|
]
|
|
existing_tools = [{"name": "other_tool", "input_schema": {}}]
|
|
|
|
injector = CCRToolInjector(provider="anthropic")
|
|
injector.scan_for_markers(messages)
|
|
tools, was_injected = injector.inject_tool_definition(existing_tools)
|
|
|
|
assert was_injected
|
|
assert len(tools) == 2
|
|
assert tools[0]["name"] == "other_tool"
|
|
assert tools[1]["name"] == CCR_TOOL_NAME
|
|
|
|
def test_skip_injection_if_tool_present_anthropic(self):
|
|
"""Injection skipped if tool already present (Anthropic format)."""
|
|
messages = [
|
|
{
|
|
"role": "tool",
|
|
"content": "[100 items compressed to 10. Retrieve more: hash=aac123456789aac123456789]",
|
|
},
|
|
]
|
|
# Tool already present (e.g., from MCP)
|
|
existing_tools = [{"name": CCR_TOOL_NAME, "input_schema": {}}]
|
|
|
|
injector = CCRToolInjector(provider="anthropic")
|
|
injector.scan_for_markers(messages)
|
|
tools, was_injected = injector.inject_tool_definition(existing_tools)
|
|
|
|
assert not was_injected
|
|
assert len(tools) == 1 # Not duplicated
|
|
|
|
def test_skip_injection_if_tool_present_openai(self):
|
|
"""Injection skipped if tool already present (OpenAI format)."""
|
|
messages = [
|
|
{
|
|
"role": "tool",
|
|
"content": "[100 items compressed to 10. Retrieve more: hash=bbc456789012bbc456789012]",
|
|
},
|
|
]
|
|
# OpenAI format tool already present
|
|
existing_tools = [
|
|
{"type": "function", "function": {"name": CCR_TOOL_NAME, "parameters": {}}}
|
|
]
|
|
|
|
injector = CCRToolInjector(provider="openai")
|
|
injector.scan_for_markers(messages)
|
|
tools, was_injected = injector.inject_tool_definition(existing_tools)
|
|
|
|
assert not was_injected
|
|
assert len(tools) == 1
|
|
|
|
def test_no_injection_without_compression(self):
|
|
"""No injection when no compression markers found."""
|
|
messages = [
|
|
{"role": "user", "content": "Hello"},
|
|
{"role": "tool", "content": '{"result": "ok"}'},
|
|
]
|
|
|
|
injector = CCRToolInjector()
|
|
injector.scan_for_markers(messages)
|
|
tools, was_injected = injector.inject_tool_definition(None)
|
|
|
|
assert not was_injected
|
|
assert tools == []
|
|
|
|
def test_inject_system_instructions(self):
|
|
"""System instructions are injected when compression detected."""
|
|
messages = [
|
|
{"role": "system", "content": "You are helpful."},
|
|
{
|
|
"role": "tool",
|
|
"content": "[100 items compressed to 10. Retrieve more: hash=abc123def456abc123def456]",
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector(inject_system_instructions=True)
|
|
injector.scan_for_markers(messages)
|
|
updated = injector.inject_into_system_message(messages)
|
|
|
|
assert "Compressed Context Available" in updated[0]["content"]
|
|
assert "abc123def456abc123def456" in updated[0]["content"]
|
|
|
|
def test_process_request_full_flow(self):
|
|
"""process_request handles complete injection flow."""
|
|
messages = [
|
|
{"role": "system", "content": "Assistant"},
|
|
{"role": "user", "content": "Search for errors"},
|
|
{
|
|
"role": "tool",
|
|
"content": "[500 items compressed to 25. Retrieve more: hash=f011f10abcdef011f10abcde]",
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector(
|
|
provider="anthropic",
|
|
inject_tool=True,
|
|
inject_system_instructions=True,
|
|
# verify_ownership() (issue #2836) requires the store to
|
|
# recognize the hash; this test only exercises injection
|
|
# plumbing, not real storage, so stub ownership as always-true.
|
|
compression_store=_AlwaysOwnStore(),
|
|
)
|
|
updated_messages, updated_tools, was_injected = injector.process_request(messages, None)
|
|
|
|
assert was_injected
|
|
assert updated_tools is not None
|
|
assert len(updated_tools) == 1
|
|
assert updated_tools[0]["name"] == CCR_TOOL_NAME
|
|
assert "Compressed Context Available" in updated_messages[0]["content"]
|
|
|
|
|
|
class TestParseToolCall:
|
|
"""Test parsing of tool calls from LLM responses."""
|
|
|
|
def test_parse_anthropic_format(self):
|
|
"""Parse Anthropic tool call format."""
|
|
tool_call = {
|
|
"id": "toolu_123",
|
|
"name": CCR_TOOL_NAME,
|
|
"input": {"hash": "abc123def456abc123def456"},
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "anthropic")
|
|
|
|
assert hash_key == "abc123def456abc123def456"
|
|
|
|
def test_parse_openai_format(self):
|
|
"""Parse OpenAI tool call format."""
|
|
tool_call = {
|
|
"id": "call_123",
|
|
"function": {
|
|
"name": CCR_TOOL_NAME,
|
|
"arguments": json.dumps({"hash": "def456abc123def456abc123"}),
|
|
},
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "openai")
|
|
|
|
assert hash_key == "def456abc123def456abc123"
|
|
|
|
def test_parse_null_function_returns_none_without_crashing(self):
|
|
"""A tool call with an explicit {"function": null} / {"functionCall": null}
|
|
must return None, not raise AttributeError."""
|
|
assert parse_tool_call({"id": "c1", "function": None}, "openai") is None
|
|
assert parse_tool_call({"functionCall": None}, "google") is None
|
|
|
|
def test_parse_normalises_uppercase_hash_to_lowercase(self):
|
|
"""An uppercase hash echoed by the model must be lowercased so it
|
|
matches the store (which keys entries by a lowercase hash)."""
|
|
tool_call = {
|
|
"id": "toolu_123",
|
|
"name": CCR_TOOL_NAME,
|
|
"input": {"hash": "ABC123DEF456ABC123DEF456"},
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "anthropic")
|
|
|
|
assert hash_key == "abc123def456abc123def456"
|
|
|
|
def test_parse_non_ccr_tool(self):
|
|
"""Returns None for non-CCR tool calls."""
|
|
tool_call = {
|
|
"name": "other_tool",
|
|
"input": {"param": "value"},
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "anthropic")
|
|
|
|
assert hash_key is None
|
|
|
|
def test_parse_malformed_openai_args(self):
|
|
"""Handles malformed JSON in OpenAI arguments."""
|
|
tool_call = {
|
|
"id": "call_123",
|
|
"function": {
|
|
"name": CCR_TOOL_NAME,
|
|
"arguments": "not valid json",
|
|
},
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "openai")
|
|
|
|
assert hash_key is None
|
|
|
|
def test_parse_openai_non_object_arguments_returns_none(self):
|
|
"""OpenAI arguments that decode to a non-object (array/string/number)
|
|
must return None, not crash on `.get`."""
|
|
for args in ("[]", '"abc"', "123"):
|
|
tool_call = {"function": {"name": CCR_TOOL_NAME, "arguments": args}}
|
|
assert parse_tool_call(tool_call, "openai") is None
|
|
|
|
def test_parse_openai_null_arguments_returns_none(self):
|
|
"""A null `arguments` value (json.loads(None) -> TypeError) is handled."""
|
|
tool_call = {"function": {"name": CCR_TOOL_NAME, "arguments": None}}
|
|
assert parse_tool_call(tool_call, "openai") is None
|
|
|
|
def test_parse_anthropic_non_dict_input_returns_none(self):
|
|
"""A non-dict Anthropic `input` must return None, not crash."""
|
|
tool_call = {"name": CCR_TOOL_NAME, "input": ["not", "a", "dict"]}
|
|
assert parse_tool_call(tool_call, "anthropic") is None
|
|
|
|
|
|
class TestHashSecurityValidation:
|
|
"""Test hash validation security measures.
|
|
|
|
CCR hashes are 12 hex chars (SmartCrusher) or 24 hex chars (legacy
|
|
bracket markers / compression_store). Any other length or non-hex input
|
|
is rejected to prevent hash spoofing with malformed hashes.
|
|
"""
|
|
|
|
def test_rejects_short_hash(self):
|
|
"""Rejects hash that's too short (potential spoofing attack)."""
|
|
tool_call = {
|
|
"name": CCR_TOOL_NAME,
|
|
"input": {"hash": "abc123"}, # Only 6 chars
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "anthropic")
|
|
assert hash_key is None # Rejected
|
|
|
|
def test_rejects_long_hash(self):
|
|
"""Rejects hash that's too long."""
|
|
tool_call = {
|
|
"name": CCR_TOOL_NAME,
|
|
"input": {"hash": "abc123def456abc123def456abc123"}, # 30 chars
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "anthropic")
|
|
assert hash_key is None # Rejected
|
|
|
|
def test_rejects_non_hex_characters(self):
|
|
"""Rejects hash with non-hex characters."""
|
|
tool_call = {
|
|
"name": CCR_TOOL_NAME,
|
|
"input": {"hash": "abc123xyz456abc123xyz456"}, # Contains xyz
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "anthropic")
|
|
assert hash_key is None # Rejected
|
|
|
|
def test_accepts_valid_24_char_hash(self):
|
|
"""Accepts properly formatted 24-char hex hash."""
|
|
tool_call = {
|
|
"name": CCR_TOOL_NAME,
|
|
"input": {"hash": "abc123def456abc123def456"},
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "anthropic")
|
|
assert hash_key == "abc123def456abc123def456"
|
|
|
|
def test_accepts_uppercase_hex(self):
|
|
"""Accepts uppercase hex characters (normalized to lowercase internally)."""
|
|
tool_call = {
|
|
"name": CCR_TOOL_NAME,
|
|
"input": {"hash": "ABC123DEF456ABC123DEF456"},
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "anthropic")
|
|
# Note: validation accepts uppercase since we use .lower() for hex check
|
|
assert hash_key == "abc123def456abc123def456"
|
|
|
|
|
|
class TestSmartCrusherCcrMarkers:
|
|
"""Regression tests for issue #1095.
|
|
|
|
SmartCrusher emits 12-hex-char hashes inside ``<<ccr:HASH ...>>`` markers
|
|
(the row-drop summary and the opaque-blob form). The injector must detect
|
|
those markers and ``parse_tool_call`` must accept the 12-char hashes —
|
|
previously both only recognized the 24-char legacy bracket markers.
|
|
"""
|
|
|
|
def test_scan_detects_row_drop_marker(self):
|
|
"""Detects ``<<ccr:HASH N_rows_offloaded>>`` (12-char hash)."""
|
|
messages = [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{
|
|
"type": "tool_result",
|
|
"tool_use_id": "x",
|
|
"content": '{"kept": 12, "ccr": "<<ccr:e21a26620105 988_rows_offloaded>>"}',
|
|
}
|
|
],
|
|
}
|
|
]
|
|
|
|
injector = CCRToolInjector(provider="anthropic")
|
|
hashes = injector.scan_for_markers(messages)
|
|
|
|
assert hashes == ["e21a26620105"]
|
|
assert injector.has_compressed_content
|
|
|
|
def test_scan_detects_opaque_blob_marker(self):
|
|
"""Detects the ``<<ccr:HASH,KIND,SIZE>>`` opaque-blob form."""
|
|
messages = [
|
|
{"role": "tool", "content": "<<ccr:deadbeefdead,string,2.3KB>>"},
|
|
]
|
|
|
|
hashes = CCRToolInjector().scan_for_markers(messages)
|
|
|
|
assert hashes == ["deadbeefdead"]
|
|
|
|
def test_legacy_bracket_marker_still_detected(self):
|
|
"""The 24-char legacy bracket marker keeps working alongside the new one."""
|
|
messages = [
|
|
{
|
|
"role": "tool",
|
|
"content": "[100 items compressed to 10. Retrieve more: hash=abc123def456abc123def456]",
|
|
},
|
|
]
|
|
|
|
hashes = CCRToolInjector().scan_for_markers(messages)
|
|
|
|
assert hashes == ["abc123def456abc123def456"]
|
|
|
|
def test_parse_tool_call_accepts_12_char_hash(self):
|
|
"""``parse_tool_call`` accepts a 12-char SmartCrusher hash."""
|
|
tool_call = {
|
|
"name": CCR_TOOL_NAME,
|
|
"input": {"hash": "e21a26620105"},
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "anthropic")
|
|
|
|
assert hash_key == "e21a26620105"
|
|
|
|
def test_parse_tool_call_still_accepts_24_char_hash(self):
|
|
"""24-char legacy hashes remain valid (regression guard)."""
|
|
tool_call = {
|
|
"name": CCR_TOOL_NAME,
|
|
"input": {"hash": "abc123def456abc123def456"},
|
|
}
|
|
|
|
hash_key = parse_tool_call(tool_call, "anthropic")
|
|
|
|
assert hash_key == "abc123def456abc123def456"
|
|
|
|
|
|
class TestSystemInstructions:
|
|
"""Test system instruction generation."""
|
|
|
|
def test_create_instructions_single_hash(self):
|
|
"""Instructions include single hash."""
|
|
instructions = create_system_instructions(["hash123"])
|
|
|
|
assert "hash123" in instructions
|
|
assert CCR_TOOL_NAME in instructions
|
|
assert "Compressed Context Available" in instructions
|
|
|
|
def test_create_instructions_multiple_hashes(self):
|
|
"""Instructions include multiple hashes."""
|
|
hashes = ["hash1", "hash2", "hash3"]
|
|
instructions = create_system_instructions(hashes)
|
|
|
|
for h in hashes:
|
|
assert h in instructions
|
|
|
|
def test_create_instructions_truncates_many_hashes(self):
|
|
"""Instructions truncate when many hashes present."""
|
|
hashes = [f"hash{i}" for i in range(10)]
|
|
instructions = create_system_instructions(hashes)
|
|
|
|
# First 5 should be present, rest truncated
|
|
assert "hash0" in instructions
|
|
assert "hash4" in instructions
|
|
assert "..." in instructions
|
|
|
|
|
|
class TestAlternativeMarkerFormats:
|
|
"""Test CCR marker detection for different compressor formats.
|
|
|
|
Different compressors use slightly different marker formats:
|
|
- SmartCrusher: [N items compressed to M. Retrieve more: hash=xxx]
|
|
- TextCompressor: [N lines compressed to M. Retrieve more: hash=xxx]
|
|
- LogCompressor: [N lines compressed to M. Retrieve more: hash=xxx]
|
|
- SearchCompressor: [N matches compressed to M. Retrieve more: hash=xxx]
|
|
- Kompress: [N items compressed to M. Retrieve more: hash=xxx]
|
|
|
|
The CCRToolInjector should detect all these formats.
|
|
"""
|
|
|
|
def test_textcompressor_format(self):
|
|
"""Detects TextCompressor marker format (lines)."""
|
|
messages = [
|
|
{
|
|
"role": "assistant",
|
|
"content": "Build output:\n[500 lines compressed to 50. Retrieve more: hash=aabbccddeeff001122334455]",
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector()
|
|
hashes = injector.scan_for_markers(messages)
|
|
|
|
assert len(hashes) == 1
|
|
assert "aabbccddeeff001122334455" in hashes
|
|
|
|
def test_searchcompressor_format(self):
|
|
"""Detects SearchCompressor marker format (matches)."""
|
|
messages = [
|
|
{
|
|
"role": "assistant",
|
|
"content": "Search results:\n[100 matches compressed to 10. Retrieve more: hash=112233445566778899001122]",
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector()
|
|
hashes = injector.scan_for_markers(messages)
|
|
|
|
assert len(hashes) == 1
|
|
assert "112233445566778899001122" in hashes
|
|
|
|
def test_mixed_compressor_formats(self):
|
|
"""Detects multiple marker formats in same conversation."""
|
|
messages = [
|
|
{
|
|
"role": "assistant",
|
|
"content": "Search results:\n[50 matches compressed to 5. Retrieve more: hash=aaaa11111111aaaa11111111]",
|
|
},
|
|
{
|
|
"role": "assistant",
|
|
"content": "Build logs:\n[200 lines compressed to 20. Retrieve more: hash=bbbb22222222bbbb22222222]",
|
|
},
|
|
{
|
|
"role": "assistant",
|
|
"content": "Database:\n[1000 items compressed to 100. Retrieve more: hash=cccc33333333cccc33333333]",
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector()
|
|
hashes = injector.scan_for_markers(messages)
|
|
|
|
assert len(hashes) == 3
|
|
assert "aaaa11111111aaaa11111111" in hashes
|
|
assert "bbbb22222222bbbb22222222" in hashes
|
|
assert "cccc33333333cccc33333333" in hashes
|
|
|
|
def test_generic_compressed_marker(self):
|
|
"""Detects generic compression markers via fallback pattern."""
|
|
messages = [
|
|
{
|
|
"role": "assistant",
|
|
"content": "Data:\n[Content compressed for efficiency. hash=fedcba9876543210fedcba98]",
|
|
},
|
|
]
|
|
|
|
injector = CCRToolInjector()
|
|
hashes = injector.scan_for_markers(messages)
|
|
|
|
assert len(hashes) == 1
|
|
assert "fedcba9876543210fedcba98" in hashes
|
|
|
|
|
|
class TestVerifyOwnership:
|
|
"""Regression tests for issue #2836.
|
|
|
|
Shape-only marker scanning (``scan_for_markers``) matches markers from
|
|
ANY context tool that happens to use the same bracket format, not just
|
|
Headroom's own. ``verify_ownership`` closes that gap by checking each
|
|
detected hash against the actual compression store before it can drive
|
|
retrieve-tool injection.
|
|
"""
|
|
|
|
def test_foreign_marker_is_dropped(self):
|
|
"""The exact repro from issue #2836: a marker Headroom never
|
|
created must not be adopted, even though its shape matches.
|
|
"""
|
|
from headroom.cache.compression_store import reset_compression_store
|
|
|
|
reset_compression_store()
|
|
try:
|
|
foreign = (
|
|
"[374 items compressed to 267 (from 65 source lines). "
|
|
"Retrieve more: hash=ddc3d69afad7bc53fbee11e2]"
|
|
)
|
|
injector = CCRToolInjector(provider="anthropic")
|
|
injector.scan_for_markers([{"role": "user", "content": foreign}])
|
|
|
|
# Shape-only scan still finds it — that's the bug surface.
|
|
assert injector.detected_hashes == ["ddc3d69afad7bc53fbee11e2"]
|
|
|
|
injector.verify_ownership()
|
|
|
|
assert injector.detected_hashes == []
|
|
assert injector.has_compressed_content is False
|
|
finally:
|
|
reset_compression_store()
|
|
|
|
def test_real_hash_survives_verification(self):
|
|
"""A hash Headroom actually stored must still be recognized."""
|
|
from headroom.cache.compression_store import (
|
|
get_compression_store,
|
|
reset_compression_store,
|
|
)
|
|
|
|
reset_compression_store()
|
|
try:
|
|
store = get_compression_store()
|
|
real_hash = store.store(
|
|
original="original content",
|
|
compressed="compressed content",
|
|
explicit_hash="abc123def456abc123def456",
|
|
)
|
|
|
|
marker = f"[100 items compressed to 10. Retrieve more: hash={real_hash}]"
|
|
injector = CCRToolInjector(provider="anthropic")
|
|
injector.scan_for_markers([{"role": "user", "content": marker}])
|
|
injector.verify_ownership()
|
|
|
|
assert injector.detected_hashes == [real_hash]
|
|
assert injector.has_compressed_content is True
|
|
finally:
|
|
reset_compression_store()
|
|
|
|
def test_mixed_own_and_foreign_hashes_keeps_only_own(self):
|
|
"""One own hash and one foreign hash in the same scan — only the
|
|
own hash survives verification.
|
|
"""
|
|
from headroom.cache.compression_store import (
|
|
get_compression_store,
|
|
reset_compression_store,
|
|
)
|
|
|
|
reset_compression_store()
|
|
try:
|
|
store = get_compression_store()
|
|
store.store(
|
|
original="mine",
|
|
compressed="mine-compressed",
|
|
explicit_hash="111111111111111111111111",
|
|
)
|
|
messages = [
|
|
{
|
|
"role": "user",
|
|
"content": (
|
|
"[10 items compressed to 5. Retrieve more: hash=111111111111111111111111]"
|
|
"\n[20 items compressed to 8. Retrieve more: hash=222222222222222222222222]"
|
|
),
|
|
}
|
|
]
|
|
injector = CCRToolInjector(provider="anthropic")
|
|
injector.scan_for_markers(messages)
|
|
assert set(injector.detected_hashes) == {
|
|
"111111111111111111111111",
|
|
"222222222222222222222222",
|
|
}
|
|
|
|
injector.verify_ownership()
|
|
|
|
assert injector.detected_hashes == ["111111111111111111111111"]
|
|
finally:
|
|
reset_compression_store()
|
|
|
|
def test_explicit_store_takes_precedence_over_global(self):
|
|
"""A store passed to verify_ownership() overrides the default
|
|
(global/request-scoped) resolution — matches the constructor's
|
|
compression_store field too.
|
|
"""
|
|
|
|
class _NeverOwnStore:
|
|
def exists(self, hash_key, clean_expired=False): # noqa: ANN001
|
|
return False
|
|
|
|
injector = CCRToolInjector(provider="anthropic", compression_store=_NeverOwnStore())
|
|
injector.scan_for_markers(
|
|
[
|
|
{
|
|
"role": "user",
|
|
"content": "[1 items compressed to 1. Retrieve more: hash=abcabcabcabcabcabcabcabc]",
|
|
}
|
|
]
|
|
)
|
|
injector.verify_ownership()
|
|
|
|
assert injector.detected_hashes == []
|
|
|
|
def test_store_lookup_exception_is_treated_as_not_owned(self):
|
|
"""A store lookup failure must not crash CCR verification — it
|
|
should drop the marker (the safe direction), not raise.
|
|
"""
|
|
|
|
class _BrokenStore:
|
|
def exists(self, hash_key, clean_expired=False): # noqa: ANN001
|
|
raise RuntimeError("store backend unavailable")
|
|
|
|
injector = CCRToolInjector(provider="anthropic", compression_store=_BrokenStore())
|
|
injector.scan_for_markers(
|
|
[
|
|
{
|
|
"role": "user",
|
|
"content": "[1 items compressed to 1. Retrieve more: hash=abcabcabcabcabcabcabcabc]",
|
|
}
|
|
]
|
|
)
|
|
injector.verify_ownership() # must not raise
|
|
|
|
assert injector.detected_hashes == []
|
|
|
|
def test_verify_ownership_is_noop_on_empty_hashes(self):
|
|
"""No detected hashes -> verify_ownership must not touch the store
|
|
at all (nothing to verify).
|
|
"""
|
|
|
|
class _ExplodingStore:
|
|
def exists(self, hash_key, clean_expired=False): # noqa: ANN001
|
|
raise AssertionError("should not be called with no detected hashes")
|
|
|
|
injector = CCRToolInjector(provider="anthropic", compression_store=_ExplodingStore())
|
|
injector.scan_for_markers([{"role": "user", "content": "no markers here"}])
|
|
result = injector.verify_ownership()
|
|
|
|
assert result == []
|