headroom/tests/test_ccr_mcp_server.py
Tejas Chopra c2fc4d3753
fix(ccr): make headroom_retrieve a hash-only full-content lookup (#1532)
The optional `query` parameter on headroom_retrieve routed retrieval
through CompressionStore.search(), which BM25-scored the items inside a
single cached blob and dropped everything below a 0.3 relevance floor.
On small per-blob corpora with conversational queries this returned an
empty result the large majority of the time, so the LLM saw "nothing
found" for content that was actually present — pushing users to turn
compression off entirely.

Retrieval is fundamentally a hash lookup (this already matches the Rust
proxy's CCR store, which is put/get only — "no BM25 search"). Remove the
query/search path end to end and always return the full original
content:

Core (Python proxy):
- tool schemas (anthropic/openai/google) drop the `query` property
- parse_tool_call returns the hash (str | None) instead of (hash, query)
- response handler, proxy POST/GET/tool-call handlers, the MCP retrieve
tool, and the streaming feedback recorders retrieve by hash only
- proactive context-tracker expansion always restores full content
- delete CompressionStore.search() and its BM25 machinery (the bm25
module stays — it is still used by relevance/)
- CCRToolCall.query, CCRToolResult.was_search, and
ExpansionRecommendation.expand_full/search_query are removed

Plugins (advertised a now-defunct query param to the LLM):
- hermes (Python), openclaw + opencode (TypeScript) retrieve tools drop
`query` from their schemas, signatures, request URLs, and tests

Benchmarks/docs:
- ccr_regression + adversarial benchmarks switch from store.search() to
full hash retrieval (search input-injection tests repurposed to the
hash, the only remaining input surface)
- wiki/ARCHITECTURE.md, wiki/ccr.md, docs/content/docs/ccr.mdx,
config.py and store docstrings updated to describe hash-only retrieval

Tests updated to assert full-content retrieval and guard the removed
surface; the full CCR/proxy/store/TOIN suite passes. ruff + mypy clean.

## Description

<!-- Briefly explain the change and why it is needed. -->

Closes #

## Type of Change

- [ ] 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

- 

## Testing

<!-- Check what you actually ran, then paste the real command output
below. -->

- [ ] Unit tests pass (`pytest`)
- [ ] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [ ] New tests added for new functionality
- [ ] Manual testing performed

### Test Output

```text
# Paste relevant command output or artifact links here
```

## Real Behavior Proof

- Environment:
- Exact command / steps:
- Observed result:
- Not tested:

## Review Readiness

- [ ] I have performed a self-review
- [ ] This PR is ready for human review

## Checklist

- [ ] My code follows the project's style guidelines
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] I have updated the CHANGELOG.md if applicable

## Screenshots (if applicable)

Add screenshots to help explain your changes.

## Additional Notes

<!-- Mention any N/A checklist items, tradeoffs, follow-ups, or
maintainer context. -->
2026-06-28 10:32:43 -07:00

112 lines
4.5 KiB
Python

from __future__ import annotations
import asyncio
import json
import pytest
from headroom.cache.compression_store import (
get_compression_store,
reset_compression_store,
)
from tests._mcp_stub import import_module_with_mcp_stub
mcp_server = import_module_with_mcp_stub("headroom.ccr.mcp_server")
def test_shared_stats_work_without_fcntl(monkeypatch, tmp_path) -> None:
monkeypatch.setattr(mcp_server, "_HAS_FCNTL", False)
monkeypatch.setattr(mcp_server, "fcntl", None)
monkeypatch.setattr(mcp_server, "SHARED_STATS_DIR", tmp_path)
monkeypatch.setattr(mcp_server, "SHARED_STATS_FILE", tmp_path / "session_stats.jsonl")
monkeypatch.setattr(mcp_server.os, "getpid", lambda: 4242)
monkeypatch.setattr(mcp_server.time, "time", lambda: 1001.0)
event = {"type": "compress", "timestamp": 1000.0}
mcp_server._append_shared_event(event)
raw_lines = mcp_server.SHARED_STATS_FILE.read_text(encoding="utf-8").splitlines()
assert len(raw_lines) == 1
assert json.loads(raw_lines[0]) == {"type": "compress", "timestamp": 1000.0, "pid": 4242}
events = mcp_server._read_shared_events(window_seconds=60)
assert events == [{"type": "compress", "timestamp": 1000.0, "pid": 4242}]
# --- Shared compression store wiring ---------------------------------------
# MCP's _get_local_store() must return the get_compression_store() singleton —
# the same instance the proxy and response_handler use — so content compressed
# on either side is retrievable in-process. These pin that wiring so a private
# store can't creep back.
@pytest.fixture
def fresh_store():
reset_compression_store()
yield
reset_compression_store()
def test_mcp_uses_shared_singleton_store(fresh_store) -> None:
"""MCP's store is the global singleton, not a private instance."""
server = mcp_server.HeadroomMCPServer(check_proxy=False)
assert server._get_local_store() is get_compression_store()
def test_mcp_retrieves_proxy_stored_content(fresh_store) -> None:
"""Content stored via the singleton (as the proxy does) is retrievable
through MCP's local-store path. The HTTP fallback is disabled so this
passes only via the shared store."""
original = '{"some": "original proxy-compressed content"}'
hash_key = get_compression_store().store(original, '{"compressed": true}')
server = mcp_server.HeadroomMCPServer(check_proxy=False)
result = asyncio.run(server._retrieve_content(hash_key))
assert result.get("source") == "local"
assert result["original_content"] == original
def test_compress_savings_percent_tracks_token_counts(fresh_store) -> None:
"""``savings_percent`` must be the *removed* percentage derived from the
token counts — never the retained percentage. Regression for the inversion
where ``(1 - compression_ratio)`` reported a no-op (0% saved) as 100%."""
pytest.importorskip("mcp", reason="MCP SDK required")
server = mcp_server.HeadroomMCPServer(check_proxy=False)
# Repetitive JSON array — the shape the engine actually compresses.
content = json.dumps([{"id": i, "status": "ok", "kind": "run"} for i in range(40)])
result = server._compress_content(content)
orig = result["original_tokens"]
comp = result["compressed_tokens"]
expected = round((1 - comp / orig) * 100, 1) if orig > 0 else 0
# Reported savings agrees with the token fields (and with tokens_saved).
assert result["savings_percent"] == expected
assert 0.0 <= result["savings_percent"] <= 100.0
if result["tokens_saved"] == 0:
assert result["savings_percent"] == 0.0 # not inverted to 100
else:
assert result["savings_percent"] > 0.0
def test_mcp_retrieve_returns_full_content(fresh_store) -> None:
"""Retrieval is by hash: a stored, unexpired entry always returns its full
original content (never empty, never a spurious "not found")."""
original = "the the the the the the the the the the\n" * 5
hash_key = get_compression_store().store(original, "<<small>>")
server = mcp_server.HeadroomMCPServer(check_proxy=False)
result = asyncio.run(server._retrieve_content(hash_key))
assert "error" not in result
assert result.get("source") == "local"
assert result["original_content"] == original
def test_mcp_retrieve_missing_hash_still_errors(fresh_store) -> None:
"""A genuinely missing hash must still report "Content not found"."""
server = mcp_server.HeadroomMCPServer(check_proxy=False)
result = asyncio.run(server._retrieve_content("nonexistent_hash"))
assert "Content not found" in result.get("error", "")