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(cache): validate malformed learned TTL values (#4364)

xiaoheiai4719-collab929a40a80e33
diff --git a/headroom/cache/ttl_observations.py b/headroom/cache/ttl_observations.py
index ba3502069..2f9f997f4 100644
--- a/headroom/cache/ttl_observations.py
+++ b/headroom/cache/ttl_observations.py
@@ -101,6 +101,8 @@ def resolve_learned_ttl(provider: str, model: str) -> int | None:
``"provider/model"`` first, then ``"provider"``. Cached by mtime so a hot-path
call is a dict lookup. Best-effort: returns None on any error, so the caller
falls back to its own default (config TTL for CC, static default elsewhere).
+ Invalid table shapes are ignored; invalid model values fall through to the
+ provider fallback. Only positive, finite TTLs (not booleans) are accepted.
"""
global _learned_cache, _learned_mtime
path = _learned_path()
@@ -108,17 +110,24 @@ def resolve_learned_ttl(provider: str, model: str) -> int | None:
mt = os.path.getmtime(path)
if _learned_cache is None or mt != _learned_mtime:
with open(path, encoding="utf-8") as f:
- _learned_cache = json.load(f)
+ loaded = json.load(f)
+ _learned_cache = loaded if isinstance(loaded, dict) else {}
_learned_mtime = mt
except Exception:
return None
table = _learned_cache or {}
for key in (f"{provider}/{model}", provider):
v = table.get(key)
- if isinstance(v, dict) and isinstance(v.get("ttl_seconds"), int | float):
- return int(v["ttl_seconds"])
- if isinstance(v, int | float):
- return int(v)
+ if isinstance(v, dict):
+ v = v.get("ttl_seconds")
+ if isinstance(v, bool) or not isinstance(v, int | float):
+ continue
+ try:
+ ttl = int(v)
+ except (ValueError, OverflowError):
+ continue # JSON may decode NaN/Infinity; never fail a live request.
+ if ttl > 0:
+ return ttl
return None
diff --git a/tests/test_cache/test_ttl_observations.py b/tests/test_cache/test_ttl_observations.py
new file mode 100644
index 000000000..f85fe9c2d
--- /dev/null
+++ b/tests/test_cache/test_ttl_observations.py
@@ -0,0 +1,67 @@
+"""Learned TTL files must remain a best-effort input to live requests."""
+
+import json
+import os
+
+import pytest
+
+from headroom.cache import ttl_observations
+from headroom.cache.prefix_tracker import PrefixCacheTracker
+from headroom.transforms.cold_prefix import is_cold_prefix
+
+
+def learned_file(tmp_path, monkeypatch):
+ path = tmp_path / "learned.json"
+ monkeypatch.setenv("HEADROOM_CACHE_TTL_LEARNED_PATH", str(path))
+ monkeypatch.setattr(ttl_observations, "_learned_cache", None)
+ monkeypatch.setattr(ttl_observations, "_learned_mtime", -1.0)
+ return path
+
+
[email protected]("table", [[300], "not a table", 300, True])
+def test_non_object_table_is_ignored_even_after_caching(learned_file, table):
+ learned_file.write_text(json.dumps(table))
+ for _ in range(2):
+ assert ttl_observations.resolve_learned_ttl("openai", "fixture") is None
+
+
[email protected]("value", [float("nan"), float("inf"), -float("inf"), True, -30, 0])
[email protected]("nested", [False, True])
+def test_invalid_model_ttl_uses_valid_provider_fallback(learned_file, value, nested):
+ learned_file.write_text(
+ json.dumps(
+ {
+ "openai/fixture": {"ttl_seconds": value} if nested else value,
+ "openai": {"ttl_seconds": 900},
+ }
+ )
+ )
+ assert ttl_observations.resolve_learned_ttl("openai", "fixture") == 900
+
+
[email protected]("value", [600, 600.75])
[email protected]("nested", [False, True])
+def test_valid_numeric_ttls_retain_integer_conversion(learned_file, value, nested):
+ learned_file.write_text(json.dumps({"openai": {"ttl_seconds": value} if nested else value}))
+ assert ttl_observations.resolve_learned_ttl("openai", "fixture") == 600
+
+
[email protected]("value", [True, -30])
+def test_invalid_provider_ttl_does_not_make_a_warm_prefix_cold(learned_file, value):
+ learned_file.write_text(json.dumps({"openai": value}))
+ tracker = PrefixCacheTracker("openai")
+ tracker._idle_seconds_at_fetch = 120.0
+ ttl = ttl_observations.resolve_learned_ttl("openai", "fixture")
+ assert ttl is None
+ assert not is_cold_prefix(tracker, ttl_seconds=ttl)
+
+
+def test_corrected_file_is_reloaded_and_model_still_takes_precedence(learned_file):
+ learned_file.write_text('["wrong shape"]')
+ assert ttl_observations.resolve_learned_ttl("openai", "fixture") is None
+ old_mtime = learned_file.stat().st_mtime
+ learned_file.write_text(json.dumps({"openai/fixture": {"ttl_seconds": 600}, "openai": 900}))
+ os.utime(learned_file, (old_mtime + 2, old_mtime + 2))
+ assert ttl_observations.resolve_learned_ttl("openai", "fixture") == 600
+ assert ttl_observations.resolve_learned_ttl("openai", "other") == 900