mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-27 14:17:10 -04:00
## Description `headroom_stats` currently formats only the rolling session view from `/stats`, so users see session numbers with no explicit scope label and no lifetime totals even though the proxy already exposes lifetime savings data. This PR keeps the current session summary, labels it as rolling-session output, and appends lifetime totals from `persistent_savings.lifetime`. It stays formatting-only on an existing payload surface. Closes #1166 ## 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 - label the existing `headroom_stats` session block as rolling-session output - append lifetime totals from the existing stats payload - add focused formatter regressions and fallback coverage - update `CHANGELOG.md` ## Testing - [ ] Unit tests pass (`uv run pytest tests/test_ccr_mcp_server.py -v`) - [ ] Linting passes (`uv run ruff check .`) - [ ] Type checking passes (`uv run mypy headroom`) or explain N/A truthfully - [x] New tests added for new functionality when applicable - [ ] Manual testing performed ### Test Output ```text Focused local commands passed: - uv run pytest tests/test_ccr_mcp_server.py -x -v 9 passed, 1 skipped - uv run ruff check headroom/ccr/mcp_server.py tests/test_ccr_mcp_server.py All checks passed - uv run ruff format headroom/ccr/mcp_server.py tests/test_ccr_mcp_server.py --check 2 files already formatted Base proof on origin/main with the updated regression file: - pytest -k "window_scoped" failed because the output still says "Headroom Session Summary" - pytest -k "includes_lifetime_totals_from_persistent_savings" failed because the formatted text still has no "Lifetime Savings:" section Not run locally: - uv run mypy headroom - Template-level broader commands `uv run pytest tests/test_ccr_mcp_server.py -v` and `uv run ruff check .` ``` ## Real Behavior Proof - Environment: focused `HeadroomMCPServer._handle_stats()` test payloads with and without `persistent_savings.lifetime` - Exact command / steps: run `uv run pytest tests/test_ccr_mcp_server.py -x -v`, specifically the new `_handle_stats()` regressions that feed summary-only, summary-plus-lifetime, missing-lifetime, and zero-lifetime payloads through the MCP stats formatter - Observed result: output contains `Headroom Window-Scoped Session Summary`, appends `Lifetime Savings:` when lifetime data is present, and omits that section cleanly when lifetime data is absent - Not tested: broader MCP output redesign beyond this formatter ## 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 - [ ] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] 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 - [x] I have updated the CHANGELOG.md if applicable ## Additional Notes - Scoped to the MCP text surface only; dashboard and broader savings-window work stay out of scope. - Attribution: the issue body identified the exact mismatch between current `headroom_stats` output and the already-live lifetime stats payload.
203 lines
7.5 KiB
Python
203 lines
7.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", "")
|
|
|
|
|
|
def test_handle_stats_session_output_is_window_scoped() -> None:
|
|
"""window-scoped stats output should be explicitly labeled after this change."""
|
|
|
|
async def fetch_stats() -> dict[str, object]:
|
|
return {
|
|
"summary": {
|
|
"mode": "token",
|
|
"api_requests": 3,
|
|
"compression": {},
|
|
}
|
|
}
|
|
|
|
server = mcp_server.HeadroomMCPServer(check_proxy=True)
|
|
server._fetch_full_proxy_stats = fetch_stats
|
|
response = asyncio.run(server._handle_stats())
|
|
text = response[0].kwargs["text"]
|
|
|
|
assert "Headroom Window-Scoped Session Summary" in text
|
|
assert "Headroom Session Summary" not in text
|
|
|
|
|
|
def test_handle_stats_includes_lifetime_totals_from_persistent_savings() -> None:
|
|
"""Lifetime savings are appended from /stats persistent_savings.lifetime."""
|
|
|
|
async def fetch_stats() -> dict[str, object]:
|
|
return {
|
|
"summary": {
|
|
"mode": "token",
|
|
"api_requests": 3,
|
|
"compression": {},
|
|
},
|
|
"persistent_savings": {
|
|
"lifetime": {"tokens_saved": 12345, "compression_savings_usd": 7.25}
|
|
},
|
|
}
|
|
|
|
server = mcp_server.HeadroomMCPServer(check_proxy=True)
|
|
server._fetch_full_proxy_stats = fetch_stats
|
|
response = asyncio.run(server._handle_stats())
|
|
text = response[0].kwargs["text"]
|
|
|
|
assert "Lifetime Savings:" in text
|
|
assert "Tokens saved: 12,345" in text
|
|
assert "Compression savings: $7.25" in text
|
|
|
|
|
|
def test_handle_stats_falls_back_gracefully_without_persistent_lifetime() -> None:
|
|
"""Missing lifetime data should still return a valid session summary."""
|
|
|
|
async def fetch_stats() -> dict[str, object]:
|
|
return {
|
|
"summary": {
|
|
"mode": "token",
|
|
"api_requests": 3,
|
|
"compression": {},
|
|
},
|
|
"persistent_savings": {"lifetime": None},
|
|
}
|
|
|
|
server = mcp_server.HeadroomMCPServer(check_proxy=True)
|
|
server._fetch_full_proxy_stats = fetch_stats
|
|
response = asyncio.run(server._handle_stats())
|
|
text = response[0].kwargs["text"]
|
|
|
|
assert "Headroom Window-Scoped Session Summary" in text
|
|
assert "Lifetime Savings:" not in text
|
|
|
|
|
|
def test_handle_stats_shows_zero_lifetime_totals_when_present() -> None:
|
|
"""A present lifetime payload should still render explicit zero totals."""
|
|
|
|
async def fetch_stats() -> dict[str, object]:
|
|
return {
|
|
"summary": {
|
|
"mode": "token",
|
|
"api_requests": 3,
|
|
"compression": {},
|
|
},
|
|
"persistent_savings": {"lifetime": {"tokens_saved": 0, "compression_savings_usd": 0.0}},
|
|
}
|
|
|
|
server = mcp_server.HeadroomMCPServer(check_proxy=True)
|
|
server._fetch_full_proxy_stats = fetch_stats
|
|
response = asyncio.run(server._handle_stats())
|
|
text = response[0].kwargs["text"]
|
|
|
|
assert "Lifetime Savings:" in text
|
|
assert "Tokens saved: 0" in text
|
|
assert "Compression savings: $0.00" in text
|