plan_snapshot_sync.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """Keep the cached plan snapshot in step with what the gateway says on each reply.
  2. The pickers read a cached account snapshot (``hermes_cli.nous_account``) and a cached Nous catalog
  3. (``hermes_cli.models_pricing``), so a plan change made in the browser (upgrade, downgrade, cancel) or a
  4. limit filling / resetting would only show after the next session opened. Every Nous reply already
  5. carries the usage-window headers, so this compares them with the snapshot and, only when they
  6. disagree, refreshes both caches once in the background. No extra call on an ordinary turn, and
  7. nothing at all for a reply that is not from Nous.
  8. """
  9. from __future__ import annotations
  10. import logging
  11. import threading
  12. import time
  13. from typing import Any, Optional
  14. logger = logging.getLogger(__name__)
  15. _lock = threading.Lock()
  16. # Both keyed by Hermes home: one process can serve several profiles, each with its own account.
  17. _inflight: set[str] = set()
  18. # What the headers said the last time a refresh ran, and when. The portal can trail the gateway by a few
  19. # minutes (usage is ingested in batches), so the same mismatch is refreshed once per retry interval, not on
  20. # every reply meanwhile, and not never again: a refresh that read the portal before it caught up retries.
  21. _last_attempt: dict[str, tuple[tuple[bool, frozenset[str]], float]] = {}
  22. _RETRY_AFTER_S = 120.0
  23. def _profile_key() -> str:
  24. from hermes_constants import hermes_home_key
  25. return hermes_home_key()
  26. def _snapshot_full_ids(snapshot: Any) -> Optional[frozenset[str]]:
  27. """Ids of the full allowances in the cached snapshot; None when it has no window plan."""
  28. window = getattr(snapshot, "usage_window", None)
  29. from agent.credits_tracker import _window_seed_fields
  30. if _window_seed_fields(window) is None:
  31. return None
  32. return frozenset(g.id for g in (getattr(window, "gauges", None) or ()) if g.full)
  33. def plan_snapshot_is_stale(state: Any, snapshot: Any) -> bool:
  34. """True when a reply's credits state contradicts the cached snapshot in a way a picker shows:
  35. window headers but a non-window snapshot (upgrade), the reverse (downgrade or cancel), or a
  36. different set of full allowances (a limit filled or reset). No snapshot yet: nothing to fix (the
  37. session-start seed is fetching it)."""
  38. if state is None or snapshot is None or getattr(snapshot, "source", None) == "error":
  39. return False
  40. on_window = bool(getattr(state, "is_usage_window", False))
  41. full_ids = _snapshot_full_ids(snapshot)
  42. if on_window != (full_ids is not None):
  43. return True
  44. if not on_window:
  45. return False
  46. return frozenset(g.id for g in getattr(state, "gauges", ()) if g.full) != full_ids
  47. def _refresh(plan_changed: bool, key: str) -> None:
  48. try:
  49. from hermes_cli.nous_account import get_nous_portal_account_info
  50. if getattr(get_nous_portal_account_info(force_fresh=True), "source", None) == "error":
  51. _last_attempt.pop(key, None) # a failed fetch (reported, not raised) is no attempt either
  52. if plan_changed:
  53. # The per-model levels (Large / Free tags) come with the catalog; refetch so they move with the plan.
  54. from hermes_cli.models_pricing import get_pricing_for_provider
  55. get_pricing_for_provider("nous", force_refresh=True)
  56. except Exception:
  57. logger.debug("plan sync ▸ refresh failed (fail-open)", exc_info=True)
  58. _last_attempt.pop(key, None) # a transient portal error must not block the next re-sync
  59. finally:
  60. with _lock:
  61. _inflight.discard(key)
  62. def sync_plan_snapshot(state: Any) -> bool:
  63. """Start one background refresh when *state* (a fresh ``CreditsState`` from reply headers) says the
  64. cached plan is out of date. Returns True iff a refresh started. Never raises, never blocks."""
  65. key = ""
  66. try:
  67. key = _profile_key()
  68. if not getattr(state, "is_usage_window", False):
  69. # Non-window reply: only a cached window snapshot (a downgrade / cancel) can be stale. Peek the
  70. # raw cache first so every other account pays no auth-state read per reply.
  71. from hermes_cli import nous_account
  72. cached = nous_account._account_info_cache
  73. if cached is None or _snapshot_full_ids(cached[2]) is None:
  74. _last_attempt.pop(key, None)
  75. return False
  76. from hermes_cli.nous_account import cached_nous_portal_account_info
  77. snapshot = cached_nous_portal_account_info(any_age=True)
  78. if not plan_snapshot_is_stale(state, snapshot):
  79. _last_attempt.pop(key, None) # in step again: the next mismatch gets its own refresh
  80. return False
  81. on_window = bool(getattr(state, "is_usage_window", False))
  82. seen = (on_window, frozenset(g.id for g in getattr(state, "gauges", ()) if g.full))
  83. plan_changed = on_window != (_snapshot_full_ids(snapshot) is not None)
  84. now = time.monotonic()
  85. with _lock:
  86. last = _last_attempt.get(key)
  87. if key in _inflight or (last is not None and last[0] == seen and now - last[1] < _RETRY_AFTER_S):
  88. return False
  89. _inflight.add(key)
  90. _last_attempt[key] = (seen, now)
  91. # Under this profile's contextvars (its Hermes home and auth store), as the credits seed thread does.
  92. from agent.memory_provider import spawn_context_thread
  93. spawn_context_thread(_refresh, args=(plan_changed, key), name="nous-plan-sync").start()
  94. return True
  95. except Exception:
  96. logger.debug("plan sync ▸ check failed (fail-open)", exc_info=True)
  97. with _lock:
  98. _inflight.discard(key)
  99. return False