prompt_refresh.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """Adopt an operator's prompt refresh request on a continuing session's next turn.
  2. A continuing session replays its stored system prompt verbatim so the provider prefix cache stays
  3. warm, which also means SOUL.md, skills and project-context edits never reach it before ``/new``.
  4. ``hermes sessions refresh-prompts`` records a refresh epoch in the profile's state.db
  5. (``hermes_state_prompt_refresh``). At the start of the session's next turn this module compares
  6. that epoch with the one stamped when the session's prompt was built (``model_config``) and, when
  7. the prompt is older, rebuilds it once through the normal builder: history, session id and the
  8. tools[] pin are kept, ``on_session_start`` does not fire, and the new bytes plus the epoch are
  9. persisted so every later turn restores them verbatim. One prefix-cache break per request.
  10. No request recorded means the stored prompt is reused exactly as before: the only extra work is
  11. one ``state_meta`` point read. Nothing watches files and nothing changes mid-turn.
  12. """
  13. from __future__ import annotations
  14. import logging
  15. from typing import Any
  16. logger = logging.getLogger(__name__)
  17. # model_config key: the refresh epoch the session's stored prompt already satisfies.
  18. SESSION_EPOCH_KEY = "prompt_refresh_epoch"
  19. def requested_epoch(agent: Any) -> int:
  20. """Newest refresh epoch recorded for the agent's session; 0 without a request (or a store that
  21. keeps none, such as a managed worker's facade)."""
  22. reader = getattr(getattr(agent, "_session_db", None), "prompt_refresh_epoch", None)
  23. session_id = getattr(agent, "session_id", None)
  24. if reader is None or not session_id:
  25. return 0
  26. try:
  27. epoch = reader(session_id)
  28. except Exception:
  29. logger.warning("Prompt refresh epoch read failed (session=%s); reusing the stored prompt.",
  30. session_id, exc_info=True)
  31. return 0
  32. return epoch if isinstance(epoch, int) else 0
  33. def _applied_epoch(session_row: Any) -> int:
  34. from hermes_state_sessions import _parse_model_config
  35. value = _parse_model_config((session_row or {}).get("model_config")).get(SESSION_EPOCH_KEY)
  36. return value if isinstance(value, int) else 0
  37. def stored_prompt_refresh_epoch(agent: Any, session_row: Any) -> int:
  38. """The epoch to rebuild the stored prompt at, or 0 when the stored prompt is current."""
  39. epoch = requested_epoch(agent)
  40. if epoch and _applied_epoch(session_row) < epoch:
  41. return epoch
  42. agent._prompt_refresh_epoch = epoch
  43. return 0
  44. def stamp_prompt_epoch(agent: Any, epoch: int) -> None:
  45. """Record that the agent's freshly built prompt satisfies ``epoch`` (read BEFORE the build, so a
  46. request that lands mid-build still fires on the next turn)."""
  47. agent._prompt_refresh_epoch = epoch
  48. if epoch:
  49. _persist_epoch(agent, epoch)
  50. def _persist_epoch(agent: Any, epoch: int) -> bool:
  51. db = getattr(agent, "_session_db", None)
  52. if not db:
  53. return True
  54. try:
  55. db.patch_session_model_config(agent.session_id, {SESSION_EPOCH_KEY: epoch})
  56. except Exception:
  57. logger.warning("Could not record the prompt refresh epoch (session=%s); the refresh re-fires "
  58. "next turn.", agent.session_id, exc_info=True)
  59. return False
  60. return True
  61. def rebuild_for_refresh(agent: Any, system_message: Any, conversation_history: Any, epoch: int) -> None:
  62. """Rebuild the session's prompt from current sources and persist it with ``epoch``."""
  63. from agent.prompt_builder import clear_skills_system_prompt_cache
  64. from agent.surface_switch import stage_surface_switch_note
  65. from hermes_cli.observability.shared_metrics_efficiency import record_cache_break
  66. logger.info("Operator requested a system prompt refresh (epoch %d) for session %s; rebuilding it "
  67. "from current SOUL.md, skills and context files (one-time prefix-cache break).",
  68. epoch, agent.session_id)
  69. # Same boundary compaction uses: memory reloads from disk, plugin sections re-render.
  70. agent._invalidate_system_prompt()
  71. agent._auto_load_skills_resolved = False # pinned skills (skills.auto_load) re-read too
  72. # As in the Bot Chat capability refresh: build the index from disk, not a cached render.
  73. clear_skills_system_prompt_cache(clear_snapshot=True)
  74. agent._cached_system_prompt = agent._build_system_prompt(system_message)
  75. record_cache_break(agent, "system_prompt_rebuild")
  76. stage_surface_switch_note(agent, agent._cached_system_prompt, conversation_history)
  77. db = getattr(agent, "_session_db", None)
  78. if db:
  79. try:
  80. db.update_system_prompt(agent.session_id, agent._cached_system_prompt)
  81. except Exception:
  82. logger.warning("Could not persist the refreshed system prompt (session=%s); the refresh "
  83. "re-fires next turn.", agent.session_id, exc_info=True)
  84. return
  85. if _persist_epoch(agent, epoch):
  86. agent._prompt_refresh_epoch = epoch
  87. def refresh_warm_prompt(agent: Any, system_message: Any, conversation_history: Any) -> None:
  88. """An agent kept warm across turns (gateway agent cache, TUI/Desktop) adopts a refresh request
  89. recorded after its prompt was set. Agents whose prompt did not come from the restore/build path
  90. (seeded maintenance agents, forks) carry no epoch and are left alone."""
  91. seen = getattr(agent, "_prompt_refresh_epoch", None)
  92. if agent._cached_system_prompt is None or not isinstance(seen, int) or not conversation_history:
  93. return
  94. epoch = requested_epoch(agent)
  95. if epoch > seen:
  96. rebuild_for_refresh(agent, system_message, conversation_history, epoch)