refactor(agent/models): unify model_metadata TTL disk memos

_ttl_memo_get/_ttl_memo_put back both the local-probe and endpoint-metadata
disk caches (file layout, key order and log strings verified identical).
This commit is contained in:
Teknium
2026-09-02 13:33:56 -07:00
parent 684e8322ff
commit 2273f2bd20
+50 -56
View File
@@ -213,59 +213,73 @@ def _is_connect_timeout(exc: BaseException) -> bool:
_LOCAL_PROBE_DISK_TTL_SECONDS = 300.0
def _local_probe_disk_cache_path() -> Path:
def _cache_file(name: str) -> Path:
from hermes_constants import get_hermes_home
return get_hermes_home() / "cache" / "local_endpoint_probes.json"
return get_hermes_home() / "cache" / name
def _load_local_probe_disk_cache() -> Dict[str, Any]:
def _load_json_dict(path: Path) -> Dict[str, Any]:
"""JSON object at ``path``, or {} when missing/invalid."""
try:
with _local_probe_disk_cache_path().open("r", encoding="utf-8") as f:
with path.open("r", encoding="utf-8") as f:
data = json.load(f)
return data if isinstance(data, dict) else {}
except Exception:
return {}
def _local_probe_disk_get(kind: str, key: str) -> Optional[Any]:
"""Return a fresh cached value for ``kind:key``, else None."""
entry = _load_local_probe_disk_cache().get(f"{kind}:{key}")
def _ttl_memo_get(path: Path, key: str, ttl: float, *, ts_key: str, value_key: str) -> Optional[Any]:
"""Fresh (< ``ttl`` seconds) ``value_key`` of the ``key`` entry in a TTL memo file, else None."""
entry = _load_json_dict(path).get(key)
if not isinstance(entry, dict):
return None
try:
if (time.time() - float(entry["ts"])) >= _LOCAL_PROBE_DISK_TTL_SECONDS:
if (time.time() - float(entry[ts_key])) >= ttl:
return None
return entry["value"]
return entry[value_key]
except Exception:
return None
def _local_probe_disk_put(kind: str, key: str, value: Any) -> None:
"""Persist a successful probe result. Best-effort; prunes stale entries."""
def _ttl_memo_put(
path: Path, key: str, value: Any, ttl: float, *, ts_key: str, value_key: str, what: str, ts_first: bool = False,
) -> None:
"""Write ``key`` into a TTL memo file, dropping expired siblings. Best-effort."""
try:
now = time.time()
data = _load_local_probe_disk_cache()
data = {
k: v
for k, v in data.items()
if isinstance(v, dict)
and (now - float(v.get("ts", 0))) < _LOCAL_PROBE_DISK_TTL_SECONDS
k: v for k, v in _load_json_dict(path).items()
if isinstance(v, dict) and (now - float(v.get(ts_key, 0))) < ttl
}
data[f"{kind}:{key}"] = {"value": value, "ts": now}
atomic_json_write(
_local_probe_disk_cache_path(),
data,
indent=0,
separators=(",", ":"),
)
data[key] = {ts_key: now, value_key: value} if ts_first else {value_key: value, ts_key: now}
atomic_json_write(path, data, indent=0, separators=(",", ":"))
except Exception as e:
logger.debug("Failed to save local probe disk cache: %s", e)
logger.debug("Failed to save %s: %s", what, e)
def _local_probe_disk_cache_path() -> Path:
return _cache_file("local_endpoint_probes.json")
def _local_probe_disk_get(kind: str, key: str) -> Optional[Any]:
"""Return a fresh cached value for ``kind:key``, else None."""
return _ttl_memo_get(
_local_probe_disk_cache_path(), f"{kind}:{key}", _LOCAL_PROBE_DISK_TTL_SECONDS,
ts_key="ts", value_key="value",
)
def _local_probe_disk_put(kind: str, key: str, value: Any) -> None:
"""Persist a successful probe result. Best-effort; prunes stale entries."""
_ttl_memo_put(
_local_probe_disk_cache_path(), f"{kind}:{key}", value, _LOCAL_PROBE_DISK_TTL_SECONDS,
ts_key="ts", value_key="value", what="local probe disk cache",
)
def _get_model_metadata_cache_path() -> Path:
"""Return path to the OpenRouter model metadata disk cache."""
from hermes_constants import get_hermes_home
return get_hermes_home() / "cache" / "openrouter_model_metadata.json"
return _cache_file("openrouter_model_metadata.json")
def _model_metadata_disk_cache_age_seconds() -> Optional[float]:
@@ -314,8 +328,7 @@ def _save_model_metadata_disk_cache(data: Dict[str, Dict[str, Any]]) -> None:
def _get_endpoint_metadata_cache_path() -> Path:
"""On-disk memo of remote ``/models`` probes (see ``_endpoint_disk_cache_get``)."""
from hermes_constants import get_hermes_home
return get_hermes_home() / "cache" / "endpoint_model_metadata.json"
return _cache_file("endpoint_model_metadata.json")
def _endpoint_disk_cache_get(normalized: str) -> Optional[Dict[str, Dict[str, Any]]]:
@@ -326,38 +339,19 @@ def _endpoint_disk_cache_get(normalized: str) -> Optional[Dict[str, Dict[str, An
the live probe. Same TTL as the in-memory cache keeps the portal
authoritative. Local endpoints are never memoized (transient loaded context).
"""
try:
with _get_endpoint_metadata_cache_path().open("r", encoding="utf-8") as f:
data = json.load(f)
entry = data.get(normalized) if isinstance(data, dict) else None
if not isinstance(entry, dict):
return None
if (time.time() - float(entry.get("at", 0))) >= _ENDPOINT_MODEL_CACHE_TTL:
return None
models = entry.get("models")
return models if isinstance(models, dict) else None
except Exception:
return None
models = _ttl_memo_get(
_get_endpoint_metadata_cache_path(), normalized, _ENDPOINT_MODEL_CACHE_TTL,
ts_key="at", value_key="models",
)
return models if isinstance(models, dict) else None
def _endpoint_disk_cache_put(normalized: str, cache: Dict[str, Dict[str, Any]]) -> None:
"""Memoize a successful remote ``/models`` probe; expired siblings are dropped."""
try:
path = _get_endpoint_metadata_cache_path()
data: Dict[str, Any] = {}
if path.exists():
with path.open("r", encoding="utf-8") as f:
loaded = json.load(f)
if isinstance(loaded, dict):
now = time.time()
data = {
k: v for k, v in loaded.items()
if isinstance(v, dict) and (now - float(v.get("at", 0))) < _ENDPOINT_MODEL_CACHE_TTL
}
data[normalized] = {"at": time.time(), "models": cache}
atomic_json_write(path, data, indent=0, separators=(",", ":"))
except Exception as e:
logger.debug("Failed to save endpoint model metadata disk cache: %s", e)
_ttl_memo_put(
_get_endpoint_metadata_cache_path(), normalized, cache, _ENDPOINT_MODEL_CACHE_TTL,
ts_key="at", value_key="models", what="endpoint model metadata disk cache", ts_first=True,
)
# Descending probe tiers for unknown models; tier[0] is also the default fallback.