hermes_state_prompt_refresh.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Operator-requested system prompt refresh epochs (``hermes sessions refresh-prompts``).
  2. A continuing session replays its stored system prompt every turn so the provider prefix cache
  3. stays warm. After an operator updates the profile's prompt sources (SOUL.md, skills, project
  4. context files) the verb records a refresh epoch here; a session whose stored prompt predates it
  5. rebuilds once on its next turn (``agent/prompt_refresh.py``). Epochs live in ``state_meta`` of
  6. the profile's own ``state.db``: durable across restarts, writable while the gateway runs, and
  7. never seen by another profile. No row means no request, and the restore path behaves as before.
  8. """
  9. from __future__ import annotations
  10. from typing import Optional
  11. PROFILE_KEY = "prompt_refresh.epoch"
  12. SESSION_KEY_PREFIX = "prompt_refresh.epoch:"
  13. class SessionPromptRefreshMixin:
  14. """Record and read refresh epochs. One counter orders profile-wide and per-session requests."""
  15. def request_prompt_refresh(self, session_id: Optional[str] = None) -> int:
  16. """Record a refresh request for every session (or just ``session_id``); returns the epoch."""
  17. key = PROFILE_KEY if session_id is None else SESSION_KEY_PREFIX + session_id
  18. def _do(conn) -> int:
  19. row = conn.execute(
  20. "SELECT MAX(CAST(value AS INTEGER)) FROM state_meta WHERE key = ? OR substr(key, 1, ?) = ?",
  21. (PROFILE_KEY, len(SESSION_KEY_PREFIX), SESSION_KEY_PREFIX),
  22. ).fetchone()
  23. epoch = int(row[0] or 0) + 1
  24. conn.execute(
  25. "INSERT INTO state_meta (key, value) VALUES (?, ?) "
  26. "ON CONFLICT(key) DO UPDATE SET value = excluded.value",
  27. (key, str(epoch)),
  28. )
  29. return epoch
  30. return self._execute_write(_do)
  31. def prompt_refresh_epoch(self, session_id: str) -> int:
  32. """The newest refresh epoch that applies to ``session_id`` (0 = never requested)."""
  33. row = self._read_one(
  34. "SELECT MAX(CAST(value AS INTEGER)) FROM state_meta WHERE key IN (?, ?)",
  35. (PROFILE_KEY, SESSION_KEY_PREFIX + session_id),
  36. )
  37. return int(row[0] or 0) if row else 0
  38. def count_sessions_with_stored_prompt(self) -> int:
  39. """Sessions whose next turn would replay a stored prompt (the ones a refresh rebuilds)."""
  40. row = self._read_one(
  41. "SELECT COUNT(*) FROM sessions WHERE system_prompt_hash IS NOT NULL OR system_prompt IS NOT NULL"
  42. )
  43. return int(row[0]) if row else 0