headroom/tests/test_proxy_stats_recent_requests.py
Vinay Gupta bd8de9f382
fix(proxy): keep recent stats request rows (#1922)
## Description

`/stats.recent_requests` was built from the request-log tail, but then
filtered out any row whose compact token fields were incomplete. That
made the dashboard/API summary diverge from the raw request counters:
`requests.by_model` could show many recent requests for a model while
`recent_requests` only showed the few rows with complete
compression-token accounting.

This fixes the stats payload so the compact `recent_requests` table
mirrors the latest request-log rows without masking unknown token
accounting as measured zero. Missing/non-finite compact numeric fields
now remain `null`, and each row exposes `token_accounting_status` plus
`has_exact_tokens` so API clients and the dashboard can distinguish
complete, partial, and missing token accounting.

Closes #1914

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring

## Changes Made

- Removed the token-completeness filter from
`_build_recent_request_payload()`.
- Preserved unknown compact recent-request numeric fields as `null`.
- Added `token_accounting_status` and `has_exact_tokens` to compact
recent request rows.
- Updated the dashboard recent-requests table to render unknown
token/latency fields as `unknown`.
- Hardened `build_session_summary()` against token-incomplete
request-log rows and surfaced an `unknown_token_accounting` bucket.
- Added TypeScript SDK fields for the compact recent-request stats
contract.
- Added a regression test covering missing, partial, and complete
token-accounting rows.

## Testing

- [x] Focused stats regression passes
- [x] Adjacent summary/MCP tests pass
- [x] TypeScript SDK typecheck/tests pass
- [x] Ruff check passes
- [x] Ruff format check passes
- [x] Diff whitespace check passes
- [ ] Full suite not run

### Test Output

```text
$ uv run --no-project ... pytest tests/test_proxy_stats_recent_requests.py -q
4 passed, 1 warning

$ uv run --no-project ... pytest tests/test_proxy_dashboard_stats_cache.py::test_session_summary_uses_generic_cli_filtering_keys tests/test_proxy_dashboard_stats_cache.py::test_session_summary_surfaces_codex_ws_counters tests/test_ccr_mcp_server.py -q
19 passed, 1 skipped

$ npm run typecheck
tsc --noEmit

$ npm test
296 passed, 33 skipped

$ uv run --no-project --with ruff ruff check headroom/proxy/server.py headroom/proxy/cost.py headroom/ccr/mcp_server.py tests/test_proxy_stats_recent_requests.py
All checks passed!

$ uv run --no-project --with ruff ruff format --check headroom/proxy/server.py headroom/proxy/cost.py headroom/ccr/mcp_server.py tests/test_proxy_stats_recent_requests.py
4 files already formatted

$ git diff --check
clean
```

## Verification

- Principal engineer review: no blockers after the final
finite-number/accounting-status alignment.
- Senior developer review: no blockers; implementation and focused
coverage look solid for #1914.
- Architect/design review: original API/UI contract blocker resolved;
unknowns remain visible as `null`/`unknown` instead of measured zero.

## Notes

The normal editable `uv run pytest ...` path is blocked locally by the
known native build issue in `esaxx-rs` (`fatal error: 'cstdint' file not
found`) while building the Rust extension on this machine. I validated
the Python-only stats path with a temporary `headroom._core` import stub
and `HEADROOM_REQUIRE_RUST_CORE=false`; no repository files were changed
for that stub.

## Review Readiness

- [x] I have performed a self-review
- [ ] This PR is ready for human review
2026-07-13 10:09:51 -04:00

245 lines
8 KiB
Python

from __future__ import annotations
import pytest
from fastapi.testclient import TestClient
from headroom.proxy import server
from headroom.proxy.models import ProxyConfig
from headroom.proxy.server import create_app
class FakeRequestLogger:
def __init__(self) -> None:
self._logs: list[dict[str, object]] = []
@property
def logs(self) -> list[dict[str, object]]:
return self._logs
@logs.setter
def logs(self, value: list[dict[str, object]]) -> None:
self._logs = value
def get_recent(self, limit: int) -> list[dict[str, object]]:
return self._logs[-limit:]
class FakeLogEntry(dict[str, object]):
def __getattr__(self, name: str) -> object:
return self.get(name)
def test_stats_refreshes_recent_requests_when_cached() -> None:
app = create_app(
ProxyConfig(
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
cost_tracking_enabled=False,
log_requests=False,
ccr_inject_tool=False,
ccr_handle_responses=False,
ccr_context_tracking=False,
http2=False,
)
)
logger = FakeRequestLogger()
app.state.proxy.logger = logger
first_log = FakeLogEntry(
{
"timestamp": "2026-06-11T10:00:00Z",
"provider": "openai",
"model": "gpt-4.1",
"input_tokens_original": 100,
"input_tokens_optimized": 60,
"tokens_saved": 40,
"savings_percent": 40.0,
}
)
second_log = FakeLogEntry(
{
"timestamp": "2026-06-11T10:01:00Z",
"provider": "anthropic",
"model": "claude-sonnet",
"input_tokens_original": 200,
"input_tokens_optimized": 120,
"tokens_saved": 80,
"savings_percent": 40.0,
}
)
# Loopback client/Host: recent_requests is served only to loopback callers.
with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as client:
logger.logs = [first_log]
first_response = client.get("/stats?cached=1")
assert first_response.status_code == 200
assert first_response.json()["recent_requests"][-1]["model"] == "gpt-4.1"
logger.logs = [first_log, second_log]
second_response = client.get("/stats?cached=1")
assert second_response.status_code == 200
second_payload = second_response.json()
assert second_payload["recent_requests"][-1]["model"] == "claude-sonnet"
assert second_payload["request_logs"][-1]["model"] == "claude-sonnet"
def test_stats_recent_requests_includes_token_incomplete_requests() -> None:
app = create_app(
ProxyConfig(
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
cost_tracking_enabled=False,
log_requests=False,
ccr_inject_tool=False,
ccr_handle_responses=False,
ccr_context_tracking=False,
http2=False,
)
)
logger = FakeRequestLogger()
app.state.proxy.logger = logger
logger.logs = [
FakeLogEntry(
{
"request_id": "req-haiku-1",
"timestamp": "2026-07-09T10:00:00Z",
"provider": "anthropic",
"model": "claude-haiku",
"transforms_applied": [],
}
),
FakeLogEntry(
{
"request_id": "req-haiku-2",
"timestamp": "2026-07-09T10:01:00Z",
"provider": "anthropic",
"model": "claude-haiku",
"input_tokens_original": None,
"input_tokens_optimized": None,
"output_tokens": None,
"tokens_saved": 0,
"savings_percent": 0.0,
"transforms_applied": [],
}
),
FakeLogEntry(
{
"request_id": "req-sonnet-1",
"timestamp": "2026-07-09T10:02:00Z",
"provider": "anthropic",
"model": "claude-sonnet",
"input_tokens_original": 200,
"input_tokens_optimized": 120,
"output_tokens": 40,
"tokens_saved": 80,
"savings_percent": 40.0,
"transforms_applied": ["smart_crusher"],
}
),
]
with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as client:
response = client.get("/stats")
assert response.status_code == 200
payload = response.json()
assert [req["model"] for req in payload["recent_requests"]] == [
"claude-haiku",
"claude-haiku",
"claude-sonnet",
]
assert payload["recent_requests"][0]["input_tokens_optimized"] is None
assert payload["recent_requests"][0]["token_accounting_status"] == "missing"
assert payload["recent_requests"][0]["has_exact_tokens"] is False
assert payload["recent_requests"][1]["output_tokens"] is None
assert payload["recent_requests"][1]["token_accounting_status"] == "partial"
assert payload["recent_requests"][1]["tokens_saved"] == 0
assert payload["recent_requests"][2]["token_accounting_status"] == "complete"
assert payload["recent_requests"][2]["has_exact_tokens"] is True
assert payload["summary"]["uncompressed_requests"]["unknown_token_accounting"] == 2
assert payload["request_logs"][-1]["model"] == "claude-sonnet"
def test_agent_usage_totals_use_proxy_only_savings(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("HEADROOM_REQUIRE_RUST_CORE", "false")
monkeypatch.setattr(
server,
"_get_context_tool_stats",
lambda: {
"tool": "rtk",
"label": "RTK",
"tokens_saved": 500,
"session": {},
"lifetime": {},
},
)
app = create_app(
ProxyConfig(
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
cost_tracking_enabled=False,
log_requests=False,
ccr_inject_tool=False,
ccr_handle_responses=False,
ccr_context_tracking=False,
http2=False,
)
)
logger = FakeRequestLogger()
app.state.proxy.logger = logger
logger.logs = [
FakeLogEntry(
{
"timestamp": "2026-06-11T10:00:00Z",
"provider": "openai",
"model": "gpt-5.2-codex",
"tags": {"client": "codex"},
"input_tokens_original": 1000,
"input_tokens_optimized": 900,
"output_tokens": 50,
"tokens_saved": 100,
"savings_percent": 10.0,
}
)
]
with TestClient(app) as client:
proxy = client.app.state.proxy
proxy.metrics.tokens_input_total = 900
proxy.metrics.tokens_saved_total = 100
proxy.metrics.tokens_output_total = 50
response = client.get("/stats")
assert response.status_code == 200
payload = response.json()
assert payload["tokens"]["saved"] == 600
assert payload["agent_usage"]["totals"]["before_tokens"] == 1000
assert payload["agent_usage"]["totals"]["tokens_saved"] == 100
assert payload["agent_usage"]["totals"]["savings_percent"] == 10.0
assert payload["agent_usage"]["agents"][0]["share_of_saved_percent"] == 100.0
def test_stats_preserves_default_smart_crusher_compaction_state() -> None:
config = ProxyConfig(
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
cost_tracking_enabled=False,
)
# Loopback client/Host: the `config` block is served only to loopback callers.
client = TestClient(
create_app(config), base_url="http://127.0.0.1", client=("127.0.0.1", 12345)
)
response = client.get("/stats")
assert response.status_code == 200
assert response.json()["config"]["smart_crusher_with_compaction"] is None