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 CI regressions for cache benchmark work

JerrettDavisd00c6739e198
diff --git a/benchmarks/claude_session_mode_benchmark.py b/benchmarks/claude_session_mode_benchmark.py
index 135d57dcd..ccaa64a65 100644
--- a/benchmarks/claude_session_mode_benchmark.py
+++ b/benchmarks/claude_session_mode_benchmark.py
@@ -11,7 +11,7 @@ import logging
import os
from collections import Counter
from dataclasses import asdict, dataclass, field
-from datetime import UTC, datetime, timedelta
+from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Any
@@ -236,10 +236,10 @@ def decode_project_key(project_key: str) -> str:
def _parse_timestamp(value: str | None) -> datetime:
if not value:
- return datetime.min.replace(tzinfo=UTC)
+ return datetime.min.replace(tzinfo=timezone.utc)
if value.endswith("Z"):
value = value[:-1] + "+00:00"
- return datetime.fromisoformat(value).astimezone(UTC)
+ return datetime.fromisoformat(value).astimezone(timezone.utc)
def _canonical_block_key(block: Any) -> str:
diff --git a/tests/test_provider_model_fallback.py b/tests/test_provider_model_fallback.py
index 986c35c93..e29a4bcbe 100644
--- a/tests/test_provider_model_fallback.py
+++ b/tests/test_provider_model_fallback.py
@@ -151,16 +151,14 @@ class TestAnthropicConfigLoading:
"""Test loading config from file path in env var."""
config = {"context_limits": {"file-model": 400000}}
- with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
- json.dump(config, f)
- f.flush()
+ with tempfile.TemporaryDirectory() as tmpdir:
+ config_path = Path(tmpdir) / "model_limits.json"
+ config_path.write_text(json.dumps(config))
- with patch.dict(os.environ, {"HEADROOM_MODEL_LIMITS": f.name}):
+ with patch.dict(os.environ, {"HEADROOM_MODEL_LIMITS": str(config_path)}):
loaded = anthropic_load_config()
assert loaded["context_limits"]["file-model"] == 400000
- os.unlink(f.name)
-
def test_load_from_config_file(self):
"""Test loading from ~/.headroom/models.json."""
config = {
@@ -291,16 +289,14 @@ class TestOpenAIConfigLoading:
"""Test loading pricing from config."""
config = {"openai": {"pricing": {"test-model": [5.0, 15.0]}}}
- with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
- json.dump(config, f)
- f.flush()
+ with tempfile.TemporaryDirectory() as tmpdir:
+ config_path = Path(tmpdir) / "model_limits.json"
+ config_path.write_text(json.dumps(config))
- with patch.dict(os.environ, {"HEADROOM_MODEL_LIMITS": f.name}):
+ with patch.dict(os.environ, {"HEADROOM_MODEL_LIMITS": str(config_path)}):
loaded = openai_load_config()
assert loaded["pricing"]["test-model"] == [5.0, 15.0]
- os.unlink(f.name)
-
class TestCrossProviderConsistency:
"""Tests for consistency across providers."""
@@ -328,10 +324,8 @@ class TestCrossProviderConsistency:
anthropic.get_context_limit("claude-future-model-xyz")
openai.get_context_limit("gpt-future-model-xyz")
- def test_both_providers_warn_for_unknown_models(self, caplog):
+ def test_both_providers_warn_for_unknown_models(self):
"""Test that both providers warn for unknown models."""
- import logging
-
# Clear warning caches
from headroom.providers import anthropic as anthropic_module
from headroom.providers import openai as openai_module
@@ -339,12 +333,17 @@ class TestCrossProviderConsistency:
anthropic_module._UNKNOWN_MODEL_WARNINGS.clear()
openai_module._UNKNOWN_MODEL_WARNINGS.clear()
- with caplog.at_level(logging.WARNING):
+ with (
+ patch.object(anthropic_module.logger, "warning") as anthropic_warning,
+ patch.object(openai_module.logger, "warning") as openai_warning,
+ ):
anthropic = AnthropicProvider()
anthropic.get_context_limit("claude-test-unknown-model")
openai = OpenAIProvider()
openai.get_context_limit("gpt-test-unknown-model")
- assert "claude-test-unknown-model" in caplog.text
- assert "gpt-test-unknown-model" in caplog.text
+ anthropic_warning.assert_called_once()
+ openai_warning.assert_called_once()
+ assert "claude-test-unknown-model" in anthropic_warning.call_args.args[0]
+ assert "gpt-test-unknown-model" in openai_warning.call_args.args[0]
diff --git a/tests/test_proxy_openai_cache_stability.py b/tests/test_proxy_openai_cache_stability.py
index 41e4d1bf1..a2ca8deed 100644
--- a/tests/test_proxy_openai_cache_stability.py
+++ b/tests/test_proxy_openai_cache_stability.py
@@ -49,8 +49,8 @@ def test_openai_cache_mode_freezes_previous_turns() -> None:
proxy.config.mode = "cache"
fake_tracker = _FakePrefixTracker(frozen_count=0)
- proxy.session_tracker_store.compute_session_id = (
- lambda request, model, messages: "stable-session"
+ proxy.session_tracker_store.compute_session_id = lambda request, model, messages: (
+ "stable-session"
)
proxy.session_tracker_store.get_or_create = lambda session_id, provider: fake_tracker
@@ -110,8 +110,8 @@ def test_openai_cache_mode_restores_mutated_frozen_prefix() -> None:
proxy.config.mode = "cache"
fake_tracker = _FakePrefixTracker(frozen_count=0)
- proxy.session_tracker_store.compute_session_id = (
- lambda request, model, messages: "stable-session"
+ proxy.session_tracker_store.compute_session_id = lambda request, model, messages: (
+ "stable-session"
)
proxy.session_tracker_store.get_or_create = lambda session_id, provider: fake_tracker