model_metadata_cache.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """``context_length_cache.yaml`` writes and their cross-process serialization.
  2. Every agent (CLI, gateway sessions, cron jobs, subagents) persists probed context windows with a
  3. load -> mutate -> write cycle; unserialized, one writer rewrites a stale snapshot and drops the
  4. other's entry (or resurrects an invalidated, too-large length).
  5. """
  6. import contextlib
  7. import functools
  8. import logging
  9. import threading
  10. from utils import atomic_yaml_write
  11. logger = logging.getLogger(__name__)
  12. _CONTEXT_CACHE_LOCK_HOLDER = threading.local()
  13. _CONTEXT_CACHE_LOCK_TIMEOUT_SECONDS = 5.0
  14. def _write_context_cache(cache: dict[str, int], bedrock_confirmed: dict | None = None) -> None:
  15. """Atomic write (a truncating write killed mid-dump leaves a partial file that
  16. _load_context_cache() swallows as {}, wiping EVERY cached length). Raises on failure."""
  17. from agent.model_metadata import _get_context_cache_path # facade seam: tests patch the path there
  18. document = {"context_lengths": cache}
  19. if bedrock_confirmed:
  20. document["bedrock_confirmed_v1"] = bedrock_confirmed
  21. atomic_yaml_write(_get_context_cache_path(), document)
  22. def locked_context_cache(fn):
  23. """Run ``fn``'s load -> mutate -> write cycle under the cache file's cross-process lock.
  24. The cache is best-effort: when the lock cannot be taken in time the update is skipped, never raised.
  25. """
  26. @functools.wraps(fn)
  27. def wrapper(*args, **kwargs):
  28. from agent.model_metadata import _get_context_cache_path # facade seam: tests patch the path there
  29. from hermes_cli.auth import _file_lock
  30. path = _get_context_cache_path()
  31. lock_path = path.with_name(path.name + ".lock")
  32. with contextlib.ExitStack() as stack:
  33. try:
  34. stack.enter_context(_file_lock(
  35. lock_path, _CONTEXT_CACHE_LOCK_HOLDER, _CONTEXT_CACHE_LOCK_TIMEOUT_SECONDS,
  36. f"Timed out waiting for the context length cache lock ({lock_path})"))
  37. except (OSError, TimeoutError):
  38. logger.debug("Skipping context length cache update", exc_info=True)
  39. return None
  40. return fn(*args, **kwargs)
  41. return wrapper
  42. @locked_context_cache
  43. def drop_context_cache_rows(stale_keys: set[str], label: str) -> None:
  44. """Remove every key shape of one cached entry (and its Bedrock provenance) under the cache lock.
  45. Only the file update is locked: callers evict their in-process probe caches unconditionally."""
  46. from agent.model_metadata import _load_context_cache_document # facade seam: tests patch it there
  47. document = _load_context_cache_document()
  48. cache = document.get("context_lengths") or {}
  49. confirmed = document.get("bedrock_confirmed_v1") or {}
  50. confirmed = confirmed if isinstance(confirmed, dict) else {}
  51. if not any(k in cache or k in confirmed for k in stale_keys):
  52. return
  53. for k in stale_keys:
  54. cache.pop(k, None)
  55. confirmed.pop(k, None)
  56. try:
  57. _write_context_cache(cache, confirmed)
  58. except Exception:
  59. logger.debug("Failed to invalidate context length cache entry %s", label, exc_info=True)