mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(cache): preserve operator-written provider fallbacks during the legacy purge
The upgrade purge in write_learned() dropped every bare-provider key, but
resolve_learned_ttl() intentionally supports hand-written provider
fallbacks (as a plain number or a {"ttl_seconds": N} dict), so an
operator-maintained entry was silently destroyed on every run. Identify
legacy estimator aggregates by their generated metadata shape
(max_hit_idle et al.) instead, and keep everything else. Regression
tests cover both directions.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Vendored
+13
-7
@@ -143,12 +143,14 @@ def write_learned(table: dict[str, dict[str, Any]], path: str) -> dict[str, Any]
|
||||
"""Merge ``table`` into the learned file at ``path`` (atomic tmp+rename).
|
||||
|
||||
Merging (not replacing) keeps keys learned from an earlier, since-rotated
|
||||
observation window instead of silently un-learning them — but only
|
||||
``provider/model`` keys. Bare-provider aggregates written by earlier
|
||||
versions of this tool are exactly the unsound cross-model intervals the
|
||||
module docstring rules out; left in place they would keep feeding
|
||||
``resolve_learned_ttl``'s provider fallback forever, so they are dropped
|
||||
on every write.
|
||||
observation window instead of silently un-learning them. Bare-provider
|
||||
aggregates written by earlier versions of this tool are exactly the unsound
|
||||
cross-model intervals the module docstring rules out; left in place they
|
||||
would keep feeding ``resolve_learned_ttl``'s provider fallback forever, so
|
||||
they are dropped on every write. They are identified by the estimator's own
|
||||
metadata shape (``max_hit_idle``) — a bare-provider key WITHOUT that shape
|
||||
is an operator-maintained fallback (``resolve_learned_ttl`` supports both a
|
||||
plain number and a ``{"ttl_seconds": N}`` dict there) and is preserved.
|
||||
"""
|
||||
merged: dict[str, Any] = {}
|
||||
try:
|
||||
@@ -159,7 +161,11 @@ def write_learned(table: dict[str, dict[str, Any]], path: str) -> dict[str, Any]
|
||||
except (OSError, json.JSONDecodeError):
|
||||
pass
|
||||
merged.update(table)
|
||||
merged = {k: v for k, v in merged.items() if "/" in k}
|
||||
merged = {
|
||||
k: v
|
||||
for k, v in merged.items()
|
||||
if "/" in k or not (isinstance(v, dict) and "max_hit_idle" in v)
|
||||
}
|
||||
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
|
||||
tmp = f"{path}.tmp.{os.getpid()}"
|
||||
with open(tmp, "w", encoding="utf-8") as f:
|
||||
|
||||
@@ -41,6 +41,18 @@ def _corpus(hit_idles: list[float], expiry_idles: list[float]) -> list[dict]:
|
||||
]
|
||||
|
||||
|
||||
def _legacy_aggregate(ttl: int) -> dict:
|
||||
# The exact record shape the earlier per-provider estimator wrote under a
|
||||
# bare provider key; `max_hit_idle` is what marks it as estimator output.
|
||||
return {
|
||||
"ttl_seconds": ttl,
|
||||
"max_hit_idle": ttl // 2,
|
||||
"hits": 5,
|
||||
"ttl_expiry_misses": 4,
|
||||
"updated_at": 1000.0,
|
||||
}
|
||||
|
||||
|
||||
class TestEstimateTtls:
|
||||
def test_upper_bound_of_interval_is_emitted(self):
|
||||
# Alive at up to 480s, first observed death beyond that at 600s
|
||||
@@ -132,10 +144,12 @@ class TestWriteLearned:
|
||||
# Files written by the earlier per-provider version of this tool carry
|
||||
# exactly the unsound cross-model aggregates the estimator no longer
|
||||
# emits; a merge that preserves them would keep feeding the consumer's
|
||||
# provider fallback forever.
|
||||
# provider fallback forever. They are recognized by the estimator's own
|
||||
# metadata shape — a bare-provider key withOUT it is an operator-written
|
||||
# fallback and must survive the purge.
|
||||
out = tmp_path / "learned.json"
|
||||
out.write_text(
|
||||
json.dumps({"openai": {"ttl_seconds": 1200}, "kimi/k2": {"ttl_seconds": 900}})
|
||||
json.dumps({"openai": _legacy_aggregate(1200), "kimi/k2": {"ttl_seconds": 900}})
|
||||
)
|
||||
write_learned({"openai/gpt-5.5": {"ttl_seconds": 600}}, str(out))
|
||||
data = json.loads(out.read_text())
|
||||
@@ -143,6 +157,21 @@ class TestWriteLearned:
|
||||
assert data["kimi/k2"]["ttl_seconds"] == 900
|
||||
assert data["openai/gpt-5.5"]["ttl_seconds"] == 600
|
||||
|
||||
def test_manual_bare_provider_fallbacks_survive_the_purge(self, tmp_path):
|
||||
# Both hand-written forms resolve_learned_ttl accepts for a bare
|
||||
# provider key: a {"ttl_seconds": N} dict and a plain number.
|
||||
out = tmp_path / "learned.json"
|
||||
out.write_text(
|
||||
json.dumps(
|
||||
{"openai": {"ttl_seconds": 3600}, "kimi": 900, "codex": _legacy_aggregate(1200)}
|
||||
)
|
||||
)
|
||||
write_learned({"openai/gpt-5.5": {"ttl_seconds": 600}}, str(out))
|
||||
data = json.loads(out.read_text())
|
||||
assert data["openai"] == {"ttl_seconds": 3600}
|
||||
assert data["kimi"] == 900
|
||||
assert "codex" not in data
|
||||
|
||||
def test_corrupt_existing_file_is_replaced_not_fatal(self, tmp_path):
|
||||
out = tmp_path / "learned.json"
|
||||
out.write_text("{not json")
|
||||
@@ -183,9 +212,12 @@ class TestMain:
|
||||
def test_upgrade_purges_legacy_provider_aggregate(self, paths):
|
||||
# Upgrade regression: a learned file from the earlier per-provider
|
||||
# version must not keep serving its aggregate to unseen models after
|
||||
# a run of the model-scoped estimator.
|
||||
# a run of the model-scoped estimator — while an operator-written
|
||||
# provider fallback in the same file keeps resolving.
|
||||
obs, out = paths
|
||||
out.write_text(json.dumps({"openai": {"ttl_seconds": 1200}}))
|
||||
out.write_text(
|
||||
json.dumps({"openai": _legacy_aggregate(1200), "kimi": {"ttl_seconds": 3600}})
|
||||
)
|
||||
obs.write_text("\n".join(json.dumps(r) for r in _corpus([120, 300, 480], [600, 900, 1200])))
|
||||
|
||||
assert main([]) == 0
|
||||
@@ -193,11 +225,12 @@ class TestMain:
|
||||
assert "openai" not in data
|
||||
assert data["openai/gpt-5.5"]["ttl_seconds"] == 600
|
||||
assert resolve_learned_ttl("openai", "unseen-model") is None
|
||||
assert resolve_learned_ttl("kimi", "unseen-model") == 3600
|
||||
|
||||
def test_upgrade_purge_runs_even_with_no_estimable_data(self, paths, capsys):
|
||||
# The purge must not depend on today's window producing an estimate.
|
||||
obs, out = paths
|
||||
out.write_text(json.dumps({"openai": {"ttl_seconds": 1200}}))
|
||||
out.write_text(json.dumps({"openai": _legacy_aggregate(1200)}))
|
||||
obs.write_text(json.dumps(_row(idle=120, hit=True)) + "\n")
|
||||
|
||||
assert main([]) == 0
|
||||
|
||||
Reference in New Issue
Block a user