A continuing session replays its stored system prompt verbatim on every turn so the provider prefix cache stays warm. The gateway restores that prompt from the session row on each fresh agent, and warm cached agents keep it in memory, so SOUL.md edits, new or edited skills and project-context changes never reach a long-running conversation until /new, which also drops the history. Operators who push profile updates and restart the gateway had no way to make existing conversations pick them up. `hermes sessions refresh-prompts [--session ID] [--json]` records a refresh epoch in the profile's state.db (state_meta, one counter orders profile-wide and per-session requests). At the start of a continuing session's next turn, _restore_or_build_system_prompt compares that epoch with the one stamped in the session's model_config when its prompt was built. When the stored prompt is older it follows the Bot Chat capability-refresh precedent in the same function: tools[] pin restored first, skills prompt cache and snapshot cleared, prompt rebuilt through _build_system_prompt (memory reloaded and plugin sections re-rendered, as at a compaction boundary), cache break recorded, on_session_start not fired, new bytes and epoch persisted so the following turns restore them verbatim. One cache break per request. Warm agents (gateway agent cache, TUI/Desktop) run the same check from build_turn_context against the epoch they saw when their prompt was set, so the verb works with the gateway running. A session first built after a request is stamped with it and does not rebuild again. With no request recorded, the stored prompt is reused byte-identically. The only added work per continuing turn is one state_meta point read. No file scanning, no config key, no watcher, no mid-turn mutation. The tools[] pin is kept on purpose: the refresh is about instructions, and tool changes already reach long sessions at the compaction boundary. The cache break is recorded as the existing system_prompt_rebuild cause rather than a new one, which would change the shared-metrics contract and schemas. Related: #141154 (per-turn source hashing, alternative design), #74622.
119 lines
5.6 KiB
Python
119 lines
5.6 KiB
Python
"""Adopt an operator's prompt refresh request on a continuing session's next turn.
|
|
|
|
A continuing session replays its stored system prompt verbatim so the provider prefix cache stays
|
|
warm, which also means SOUL.md, skills and project-context edits never reach it before ``/new``.
|
|
``hermes sessions refresh-prompts`` records a refresh epoch in the profile's state.db
|
|
(``hermes_state_prompt_refresh``). At the start of the session's next turn this module compares
|
|
that epoch with the one stamped when the session's prompt was built (``model_config``) and, when
|
|
the prompt is older, rebuilds it once through the normal builder: history, session id and the
|
|
tools[] pin are kept, ``on_session_start`` does not fire, and the new bytes plus the epoch are
|
|
persisted so every later turn restores them verbatim. One prefix-cache break per request.
|
|
|
|
No request recorded means the stored prompt is reused exactly as before: the only extra work is
|
|
one ``state_meta`` point read. Nothing watches files and nothing changes mid-turn.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# model_config key: the refresh epoch the session's stored prompt already satisfies.
|
|
SESSION_EPOCH_KEY = "prompt_refresh_epoch"
|
|
|
|
|
|
def requested_epoch(agent: Any) -> int:
|
|
"""Newest refresh epoch recorded for the agent's session; 0 without a request (or a store that
|
|
keeps none, such as a managed worker's facade)."""
|
|
reader = getattr(getattr(agent, "_session_db", None), "prompt_refresh_epoch", None)
|
|
session_id = getattr(agent, "session_id", None)
|
|
if reader is None or not session_id:
|
|
return 0
|
|
try:
|
|
epoch = reader(session_id)
|
|
except Exception:
|
|
logger.warning("Prompt refresh epoch read failed (session=%s); reusing the stored prompt.",
|
|
session_id, exc_info=True)
|
|
return 0
|
|
return epoch if isinstance(epoch, int) else 0
|
|
|
|
|
|
def _applied_epoch(session_row: Any) -> int:
|
|
from hermes_state_sessions import _parse_model_config
|
|
|
|
value = _parse_model_config((session_row or {}).get("model_config")).get(SESSION_EPOCH_KEY)
|
|
return value if isinstance(value, int) else 0
|
|
|
|
|
|
def stored_prompt_refresh_epoch(agent: Any, session_row: Any) -> int:
|
|
"""The epoch to rebuild the stored prompt at, or 0 when the stored prompt is current."""
|
|
epoch = requested_epoch(agent)
|
|
if epoch and _applied_epoch(session_row) < epoch:
|
|
return epoch
|
|
agent._prompt_refresh_epoch = epoch
|
|
return 0
|
|
|
|
|
|
def stamp_prompt_epoch(agent: Any, epoch: int) -> None:
|
|
"""Record that the agent's freshly built prompt satisfies ``epoch`` (read BEFORE the build, so a
|
|
request that lands mid-build still fires on the next turn)."""
|
|
agent._prompt_refresh_epoch = epoch
|
|
if epoch:
|
|
_persist_epoch(agent, epoch)
|
|
|
|
|
|
def _persist_epoch(agent: Any, epoch: int) -> bool:
|
|
db = getattr(agent, "_session_db", None)
|
|
if not db:
|
|
return True
|
|
try:
|
|
db.patch_session_model_config(agent.session_id, {SESSION_EPOCH_KEY: epoch})
|
|
except Exception:
|
|
logger.warning("Could not record the prompt refresh epoch (session=%s); the refresh re-fires "
|
|
"next turn.", agent.session_id, exc_info=True)
|
|
return False
|
|
return True
|
|
|
|
|
|
def rebuild_for_refresh(agent: Any, system_message: Any, conversation_history: Any, epoch: int) -> None:
|
|
"""Rebuild the session's prompt from current sources and persist it with ``epoch``."""
|
|
from agent.prompt_builder import clear_skills_system_prompt_cache
|
|
from agent.surface_switch import stage_surface_switch_note
|
|
from hermes_cli.observability.shared_metrics_efficiency import record_cache_break
|
|
|
|
logger.info("Operator requested a system prompt refresh (epoch %d) for session %s; rebuilding it "
|
|
"from current SOUL.md, skills and context files (one-time prefix-cache break).",
|
|
epoch, agent.session_id)
|
|
# Same boundary compaction uses: memory reloads from disk, plugin sections re-render.
|
|
agent._invalidate_system_prompt()
|
|
agent._auto_load_skills_resolved = False # pinned skills (skills.auto_load) re-read too
|
|
# As in the Bot Chat capability refresh: build the index from disk, not a cached render.
|
|
clear_skills_system_prompt_cache(clear_snapshot=True)
|
|
agent._cached_system_prompt = agent._build_system_prompt(system_message)
|
|
record_cache_break(agent, "system_prompt_rebuild")
|
|
stage_surface_switch_note(agent, agent._cached_system_prompt, conversation_history)
|
|
db = getattr(agent, "_session_db", None)
|
|
if db:
|
|
try:
|
|
db.update_system_prompt(agent.session_id, agent._cached_system_prompt)
|
|
except Exception:
|
|
logger.warning("Could not persist the refreshed system prompt (session=%s); the refresh "
|
|
"re-fires next turn.", agent.session_id, exc_info=True)
|
|
return
|
|
if _persist_epoch(agent, epoch):
|
|
agent._prompt_refresh_epoch = epoch
|
|
|
|
|
|
def refresh_warm_prompt(agent: Any, system_message: Any, conversation_history: Any) -> None:
|
|
"""An agent kept warm across turns (gateway agent cache, TUI/Desktop) adopts a refresh request
|
|
recorded after its prompt was set. Agents whose prompt did not come from the restore/build path
|
|
(seeded maintenance agents, forks) carry no epoch and are left alone."""
|
|
seen = getattr(agent, "_prompt_refresh_epoch", None)
|
|
if agent._cached_system_prompt is None or not isinstance(seen, int) or not conversation_history:
|
|
return
|
|
epoch = requested_epoch(agent)
|
|
if epoch > seen:
|
|
rebuild_for_refresh(agent, system_message, conversation_history, epoch)
|