Skip to content

The agent that grows with you

refactor(agent/prompt): split build_system_prompt_parts into tier helpers; unify model-gate resolver

Teknium593c8e914604
diff --git a/agent/system_prompt.py b/agent/system_prompt.py
index 64c39c7c835..864362f9a1e 100644
--- a/agent/system_prompt.py
+++ b/agent/system_prompt.py
@@ -1,26 +1,12 @@
"""System-prompt assembly for :class:`AIAgent`.
-The agent's system prompt is built once per session and reused across all
-turns — only context compression triggers a rebuild. This keeps the
-upstream prefix cache warm. See ``hermes-agent-dev``'s
-``references/system-prompt-invariant.md`` for the invariants and
-``references/self-improvement-loop.md`` for how the background-review
-fork inherits the cached prompt verbatim.
-
-Three tiers are joined with ``\\n\\n``:
-
-* ``stable`` — identity (SOUL.md or DEFAULT_AGENT_IDENTITY), tool
- guidance, computer-use guidance, nous subscription block, tool-use
- enforcement guidance + per-model operational guidance,
- alibaba model-name workaround, environment hints, coding guidance,
- platform hints.
-* ``context`` — caller-supplied ``system_message`` plus context files
- (AGENTS.md / .cursorrules / etc.) discovered under ``TERMINAL_CWD``,
- plus the session's coding-workspace snapshot.
-* ``volatile`` — skills index, memory snapshot, USER.md profile, external
- memory provider block, timestamp/session/model/provider line.
-
-Pure helpers that read the agent's state. AIAgent keeps thin forwarders.
+Built once per session and reused across turns (only context compression
+triggers a rebuild) so the upstream prefix cache stays warm. Three tiers
+are joined with ``\\n\\n``: ``stable`` (identity, guidance, env hints, coding
+brief, platform hints), ``context`` (workspace snapshot, caller
+``system_message``, context files) and ``volatile`` (skills index, memory,
+USER.md, external memory provider, timestamp line). See the
+``hermes-agent-dev`` skill's ``references/system-prompt-invariant.md``.
"""
from __future__ import annotations
@@ -29,6 +15,7 @@ import json
import logging
import os
import re
+from pathlib import Path
from typing import Any, Dict, List, Optional
from agent.prompt_builder import (
@@ -40,7 +27,6 @@ from agent.prompt_builder import (
KANBAN_GUIDANCE,
MEMORY_GUIDANCE,
USER_PROFILE_GUIDANCE,
- OPENAI_MODEL_EXECUTION_GUIDANCE,
PARALLEL_TOOL_CALL_GUIDANCE,
PLATFORM_HINTS,
SESSION_SEARCH_GUIDANCE,
@@ -54,7 +40,6 @@ from agent.prompt_builder import (
)
from agent.runtime_cwd import resolve_context_cwd
from hermes_constants import get_default_hermes_root, get_hermes_home
-from pathlib import Path
from utils import is_truthy_value
logger = logging.getLogger(__name__)
@@ -63,42 +48,38 @@ _PLUGIN_SECTION_FRAME_RE = re.compile(
r"<!-- hermes-plugin-section-chars:(?P<chars>[0-9]{1,4}) -->\n\n",
re.MULTILINE,
)
+_TRUTHY_GATES = {"true", "always", "yes", "on"}
+_FALSY_GATES = {"false", "never", "no", "off"}
def _ra():
- """Lazy reference to the ``run_agent`` module.
-
- Helpers like ``load_soul_md``, ``build_environment_hints``,
- ``build_context_files_prompt``,
- ``build_skills_system_prompt`` and ``get_toolset_for_tool`` are
- imported into ``run_agent``'s namespace. Many tests
- ``patch("run_agent.load_soul_md", ...)``; if we imported them
- directly here those patches would not reach us. Looking them up
- through ``run_agent`` on every call preserves the patch contract.
- """
+ """Lazy ``run_agent`` handle: tests ``patch("run_agent.load_soul_md")`` etc.,
+ so the helpers must be resolved through that namespace on every call."""
import run_agent
return run_agent
-def _resolve_platform_hint(agent: Any, platform_key: str, default_hint: str) -> str:
- """Apply a per-platform prompt-hint override to the default hint.
-
- Reads ``agent._platform_hint_overrides`` (populated from
- ``config.yaml`` ``platform_hints`` by ``agent_init``) and resolves the
- effective hint for *platform_key*:
+def _model_gate(setting: Any, model: Optional[str], default_models) -> bool:
+ """Resolve a config gate: True/"true"-ish -> on, False/"false"-ish -> off,
+ list -> case-insensitive model-substring match, anything else ("auto") ->
+ match against *default_models*."""
+ model_lower = (model or "").lower()
+ if setting is True or (isinstance(setting, str) and setting.lower() in _TRUTHY_GATES):
+ return True
+ if setting is False or (isinstance(setting, str) and setting.lower() in _FALSY_GATES):
+ return False
+ if isinstance(setting, list):
+ return any(p.lower() in model_lower for p in setting if isinstance(p, str))
+ return any(p in model_lower for p in default_models)
- * ``replace`` — substitute the default hint entirely.
- * ``append`` — keep the default and append the extra text.
- * a bare string value — treated as ``append`` (convenience shorthand).
- Precedence: ``replace`` wins over ``append`` if both are present.
- Override text is added on top of (not instead of) the SOUL/context/
- memory tiers — it only affects the platform-hint segment, so other
- platforms are unaffected and general system instructions still apply.
+def _resolve_platform_hint(agent: Any, platform_key: str, default_hint: str) -> str:
+ """Apply the ``platform_hints.<platform>`` config override to *default_hint*.
- Defensive: any malformed entry falls back to the unmodified default so
- a bad config value can never break prompt assembly or leak across
- platforms.
+ ``replace`` substitutes the default, ``append`` adds text (a bare string is
+ shorthand for append); ``replace`` wins when both are present. Malformed
+ entries fall back to the unmodified default so bad config can never break
+ prompt assembly or leak across platforms.
"""
if not platform_key:
return default_hint
@@ -108,21 +89,13 @@ def _resolve_platform_hint(agent: Any, platform_key: str, default_hint: str) ->
spec = overrides.get(platform_key)
if spec is None:
return default_hint
-
- # Shorthand: a bare string is treated as append text.
if isinstance(spec, str):
extra = spec.strip()
return f"{default_hint}\n\n{extra}".strip() if extra else default_hint
-
if not isinstance(spec, dict):
return default_hint
-
replace_text = spec.get("replace")
- if isinstance(replace_text, str) and replace_text.strip():
- base = replace_text.strip()
- else:
- base = default_hint
-
+ base = replace_text.strip() if isinstance(replace_text, str) and replace_text.strip() else default_hint
append_text = spec.get("append")
if isinstance(append_text, str) and append_text.strip():
return f"{base}\n\n{append_text.strip()}".strip()
@@ -137,22 +110,10 @@ _TUI_EMBEDDED_PANE_CLARIFIER = (
def _tui_embedded_pane_clarifier(hint: str) -> str:
- """Append the desktop-embedded-terminal-pane clarifier to a tui hint.
-
- Triggered by ``HERMES_DESKTOP_TERMINAL=1`` (set by ``main.cjs`` only on the
- shell env of the desktop's embedded TUI PTY — never on the chat backend).
- This is a runtime-surface qualifier, not a config override, so it lives at
- the resolution site rather than inside ``_resolve_platform_hint`` (which
- is purely the config-platform_hints override applier). Byte-stable for the
- cache: called once per session build, deterministically from env state.
-
- Idempotent and empty-safe: re-applying on an already-augmented hint is a
- no-op, and an empty input returns empty (we never synthesize the
- clarifier without its tui framing).
- """
- if not hint:
- return hint
- if _TUI_EMBEDDED_PANE_CLARIFIER in hint:
+ """Append the desktop embedded-terminal clarifier to a tui hint when
+ ``HERMES_DESKTOP_TERMINAL`` is set (only on the desktop's TUI PTY, never the
+ chat backend). Idempotent; empty input stays empty."""
+ if not hint or _TUI_EMBEDDED_PANE_CLARIFIER in hint:
return hint
if not is_truthy_value(os.getenv("HERMES_DESKTOP_TERMINAL")):
return hint
@@ -166,9 +127,8 @@ def _plugin_session_info(agent: Any) -> Dict[str, str]:
except Exception:
cwd = ""
try:
- # Prefer the agent's own home (override-aware, session_db fallback) —
- # ambient get_active_profile_name() misreports on threads that lost
- # the HERMES_HOME ContextVar (#86313 class; plugin half per @helix4u).
+ # Prefer the agent's own home: ambient get_active_profile_name()
+ # misreports on threads that lost the HERMES_HOME ContextVar.
_home = _agent_home(agent)
if _home is not None:
profile_name = _profile_name_for_home(_home)
@@ -189,12 +149,11 @@ def _plugin_session_info(agent: Any) -> Dict[str, str]:
def _frozen_plugin_prompt_sections(agent: Any) -> tuple:
- """Render once on a new session; never re-evaluate a restored prompt.
+ """Render plugin sections once per session and freeze them on the agent.
- Compression rebuilds reuse the per-agent tuple. A fresh process restores
- ``_cached_system_prompt`` before reconstructing its static cache prefix;
- because plugin sections live after memory in the volatile tail, that
- reconstruction can safely omit them and must not call plugin code again.
+ A restored ``_cached_system_prompt`` is parsed instead of re-running plugin
+ code; a render that raises at a rebuild boundary keeps the previous bytes
+ (stashed by ``invalidate_system_prompt``) instead of silently vanishing.
"""
attr = "_plugin_system_prompt_sections_snapshot"
if hasattr(agent, attr):
@@ -209,9 +168,6 @@ def _frozen_plugin_prompt_sections(agent: Any) -> tuple:
rendered = tuple(render_system_prompt_sections(_plugin_session_info(agent)))
except Exception as exc:
- # Fail-open: a plugin whose render raises at a rebuild boundary
- # keeps its last good bytes (stashed by invalidate_system_prompt)
- # instead of silently vanishing from the prompt.
previous = getattr(agent, "_plugin_system_prompt_sections_previous", None)
if previous:
logger.warning(
@@ -227,7 +183,9 @@ def _frozen_plugin_prompt_sections(agent: Any) -> tuple:
def _restore_plugin_prompt_sections(prompt: str) -> tuple:
- """Recover frozen section bytes from the already-persisted full prompt."""
+ """Recover frozen section bytes from the persisted full prompt. Only the
+ exact canonical container emitted by core is accepted — user/project text
+ may resemble a frame."""
from hermes_cli.plugins import (
MAX_SYSTEM_PROMPT_SECTION_CHARS,
PLUGIN_SECTIONS_END,
@@ -252,8 +210,7 @@ def _restore_plugin_prompt_sections(prompt: str) -> tuple:
content_len = int(match.group("chars"))
if content_len > MAX_SYSTEM_PROMPT_SECTION_CHARS:
continue
- content_start = match.end()
- content = framed[content_start : content_start + content_len]
+ content = framed[match.end() : match.end() + content_len]
if len(content) != content_len:
continue
restored.append(
@@ -264,8 +221,6 @@ def _restore_plugin_prompt_sections(prompt: str) -> tuple:
plugin="persisted-prompt",
)
)
- # User/project text may resemble a frame. Accept only the exact canonical
- # container emitted by core, never a partial or malformed lookalike.
if format_system_prompt_sections(restored) != framed:
return ()
return tuple(restored)
@@ -279,39 +234,19 @@ def restore_plugin_prompt_sections(agent: Any, prompt: str) -> None:
def _plugin_section_blocks(sections: tuple, position: str) -> List[str]:
from hermes_cli.plugins import format_system_prompt_sections
- selected = [section for section in sections if section.position == position]
- block = format_system_prompt_sections(selected)
+ block = format_system_prompt_sections([s for s in sections if s.position == position])
return [block] if block else []
def _session_start_like(agent: Any, now: Any) -> Any:
"""Best-known conversation start time, or ``now`` as a fallback.
- ``Conversation started:`` must reference when the conversation actually
- began, not when the system prompt was last (re)built. The prompt is
- rebuilt on compression, fresh-agent gateway turns, and resume paths, and
- stamping build time made the date drift forward across midnight (a chat
- that started on Wednesday read as "Conversation started: Thursday" after
- a Thursday-morning resume), contradicting the fresh per-turn time hint.
- Prefer, in order:
-
- 0. the LINEAGE-ROOT session id's embedded timestamp — compaction can
- rotate the session id, and each rotated id embeds its OWN mint time,
- so after months of compactions rung 1 alone would quietly re-birth
- the conversation at its latest rotation. Walking to the lineage root
- (same walk as ``_conversation_root_id``) recovers the ORIGINAL
- birth stamp — a Bot Mode forever-chat keeps knowing when it was
- first born, across every compaction (maintainer-directed, #98426);
- 1. the timestamp embedded in ``session_id`` (``YYYYMMDD_HHMMSS_...``) —
- immutable for the life of the session, so the line is byte-stable
- across every rebuild boundary (preserving prefix-cache KV);
- 2. ``agent.session_start`` (session-creation stamp);
- 3. ``now`` (initial/legacy build without either).
-
- Session-id and ``session_start`` stamps are recorded in the box's local
- wall-clock; attach that zone first, then convert to the configured /
- rendered zone (``now``'s tzinfo) so the displayed date is consistent with
- the per-turn clock even when the box's TZ differs from the configured one.
+ ``Conversation started:`` must be byte-stable across rebuilds (compression,
+ resume, fresh gateway turns), so prefer immutable sources in order: the
+ lineage-root session id's embedded stamp (compaction rotates ids, each with
+ its own mint time), the current session id's stamp, ``agent.session_start``,
+ then ``now``. Stamps are box-local wall-clock: attach that zone first, then
+ convert to ``now``'s zone so the date matches the per-turn clock.
"""
from datetime import datetime
@@ -333,11 +268,6 @@ def _session_start_like(agent: Any, now: Any) -> Any:
pass
return dt
- # 0. Lineage root: compaction rotation mints NEW ids with NEW embedded
- # stamps. Walk to the root id (cached on the agent — the lineage only
- # grows at compaction, and this function runs at that exact boundary,
- # so one walk per rebuild is fresh enough) and prefer ITS embedded
- # timestamp: the conversation's true birth. Fail-open to rung 1.
session_id = getattr(agent, "session_id", None)
root_id = None
try:
@@ -351,41 +281,24 @@ def _session_start_like(agent: Any, now: Any) -> Any:
m = re.match(r"^(\d{8})_(\d{6})", candidate)
if m:
try:
- embedded = datetime.strptime(
- f"{m.group(1)}_{m.group(2)}", "%Y%m%d_%H%M%S"
- )
+ embedded = datetime.strptime(f"{m.group(1)}_{m.group(2)}", "%Y%m%d_%H%M%S")
return _to_display_tz(embedded)
except ValueError:
pass
- # 2. Session-creation stamp set by the runner.
session_start = getattr(agent, "session_start", None)
if hasattr(session_start, "astimezone"):
return _to_display_tz(session_start)
-
- # 3. Fallback: build time.
return now
def _agent_home(agent: Any) -> Optional[Path]:
- """The agent's OWN profile home.
-
- Resolution order:
-
- 1. A bound HERMES_HOME ContextVar override wins. Surfaces that multiplex
- several profiles over ONE shared session DB (the messaging gateway:
- ``gateway/run.py`` hands every agent the launch-home ``state.db`` and
- binds the profile home per turn via ``_profile_runtime_scope`` +
- ``copy_context``) would otherwise have the db-derived launch home
- STOMP the correctly-bound profile — inverting the leak this helper
- exists to fix (found by @kshitijk4poor's post-merge probe on #86313).
- 2. Fallback: the home containing the agent's ``_session_db.db_path``
- (``<home>/state.db``) — ground truth on threads that lost the
- ContextVar (ContextVars don't propagate into ``threading.Thread``),
- where the unbound build previously fell back to the launch home and
- leaked the default profile's skills/identity into a bot prompt.
-
- Returns None when neither resolves so callers fall back to ambient.
+ """The agent's OWN profile home, or None to use ambient resolution.
+
+ A bound HERMES_HOME ContextVar override wins (the gateway multiplexes
+ profiles over one shared session DB and binds the home per turn); else the
+ parent of ``_session_db.db_path`` — ground truth on threads that lost the
+ ContextVar, where ambient resolution would leak the launch profile.
"""
try:
from hermes_constants import get_hermes_home_override
@@ -396,8 +309,7 @@ def _agent_home(agent: Any) -> Optional[Path]:
except Exception:
pass
try:
- db = getattr(agent, "_session_db", None)
- db_path = getattr(db, "db_path", None)
+ db_path = getattr(getattr(agent, "_session_db", None), "db_path", None)
if db_path:
return Path(db_path).parent
except Exception:
@@ -412,14 +324,11 @@ def _agent_skills_dir(agent: Any) -> Optional[Path]:
def _profile_name_for_home(home: Path) -> str:
- """Derive the profile name for an explicit agent home.
-
- ``<root>/profiles/X`` -> ``"X"``; anything else -> ``"default"``.
+ """``<root>/profiles/X`` -> ``"X"``; anything else -> ``"default"``.
- Uses :func:`get_default_hermes_root` (NOT ``get_hermes_home()``): on a
- correctly bound profile session the ambient home IS the profile dir, so
- ``get_hermes_home()/profiles`` would never contain ``home`` and every
- profile would misreport as "default".
+ Uses ``get_default_hermes_root()`` (NOT ``get_hermes_home()``): on a bound
+ profile session the ambient home IS the profile dir, so every profile would
+ misreport as "default".
"""
try:
from hermes_constants import get_default_hermes_root
@@ -428,37 +337,261 @@ def _profile_name_for_home(home: Path) -> str:
rel = home.resolve().relative_to((root / "profiles").resolve())
return rel.parts[0] if rel.parts else "default"
except (ValueError, OSError):
- # Home IS the root (default profile) or unrelatable -> default.
return "default"
+def _tool_guidance_block(agent: Any) -> Optional[str]:
+ """Tool-aware behavioral guidance, injected only when the tools are loaded."""
+ tool_guidance = []
+ # With both memory stores disabled no store is built, so the full guidance
+ # would steer the model at a tool that always answers "Memory is not
+ # available"; with only USER.md enabled the narrower block is used.
+ if "memory" in agent.valid_tool_names:
+ if getattr(agent, "_memory_enabled", True):
+ tool_guidance.append(MEMORY_GUIDANCE)
+ elif getattr(agent, "_user_profile_enabled", True):
+ tool_guidance.append(USER_PROFILE_GUIDANCE)
+ if "session_search" in agent.valid_tool_names:
+ tool_guidance.append(SESSION_SEARCH_GUIDANCE)
+ if "skill_manage" in agent.valid_tool_names:
+ tool_guidance.append(SKILLS_GUIDANCE)
+ # Kanban lifecycle: resolved once at __init__ (_kanban_worker_guidance);
+ # the kanban_show fallback covers code paths that bypass agent_init.
+ _kanban_guidance = getattr(agent, "_kanban_worker_guidance", None)
+ if _kanban_guidance:
+ tool_guidance.append(_kanban_guidance)
+ elif _kanban_guidance is None and "kanban_show" in agent.valid_tool_names:
+ tool_guidance.append(KANBAN_GUIDANCE)
+ return " ".join(tool_guidance) if tool_guidance else None
+
+
+def _skills_prompt(agent: Any, _r: Any) -> str:
+ """Skills index (empty without skills tools). Focus mode demotes non-coding
+ categories to names-only — never hidden, every name stays visible."""
+ if not any(name in agent.valid_tool_names for name in ['skills_list', 'skill_view', 'skill_manage']):
+ return ""
+ avail_toolsets = {
+ toolset
+ for toolset in (_r.get_toolset_for_tool(tool_name) for tool_name in agent.valid_tool_names)
+ if toolset
+ }
+ _compact_cats = frozenset()
+ try:
+ from agent.coding_context import coding_compact_skill_categories
+
+ _compact_cats = coding_compact_skill_categories(platform=agent.platform, cwd=resolve_context_cwd())
+ except Exception:
+ _compact_cats = frozenset()
+ return _r.build_skills_system_prompt(
+ available_tools=agent.valid_tool_names,
+ available_toolsets=avail_toolsets,
+ compact_categories=_compact_cats or None,
+ skills_dir_override=_agent_skills_dir(agent),
+ )
+
+
+def _bot_mode_parts(agent: Any) -> List[str]:
+ """Bot Mode teammate protocol — only in a bot's canonical "Bot Chat" session.
+ Marks the prompt timeless (the volatile date line is dropped) since a birth
+ date pinned in a months-long session is misinformation."""
+ parts: List[str] = []
+ try:
+ from tools.bot_mode_probe import (
+ BOT_CHAT_TITLE,
+ epoch_line,
+ get_bot_mode_protocol_section,
+ )
+ _title = str(getattr(agent, "_session_title_hint", "") or "").strip()
+ if not _title:
+ _sdb = getattr(agent, "_session_db", None)
+ _sid = getattr(agent, "session_id", None)
+ _title = str((_sdb.get_session_title(_sid) if (_sdb and _sid) else None) or "").strip()
+ if _title == BOT_CHAT_TITLE:
+ _bot_section = get_bot_mode_protocol_section(_agent_home(agent))
+ if _bot_section:
+ parts.append(_bot_section)
+ # Capability epoch lets the restore path rebuild ONCE per
+ # user-initiated capability change in an eternal session.
+ parts.append(epoch_line(_agent_home(agent)))
+ agent._bot_chat_timeless_prompt = True
+ except Exception:
+ pass
+ return parts
+
+
+def _active_profile_line(agent: Any) -> str:
+ """Name the running profile so the agent doesn't conflate ``~/.hermes/skills``
+ (default) with ``~/.hermes/profiles/<active>/skills``. Resolved from the
+ agent's OWN home first (a build thread that lost the ContextVar would
+ otherwise print "default" for a bot profile)."""
+ _agent_home_path = _agent_home(agent)
+ active_profile = "default"
+ try:
+ if _agent_home_path is not None:
+ active_profile = _profile_name_for_home(_agent_home_path)
+ else:
+ from agent.file_safety import _resolve_active_profile_name
+ active_profile = _resolve_active_profile_name()
+ except Exception:
+ active_profile = "default"
+ # With an explicit agent home, the default profile's data lives at the
+ # ROOT (get_hermes_home() on a bound profile session is the PROFILE dir).
+ # Without one, keep the ambient (patchable) resolution byte-identical.
+ if _agent_home_path is not None:
+ _home_str = str(_agent_home_path)
+ _root_str = str(get_default_hermes_root())
+ else:
+ _home_str = _root_str = str(get_hermes_home())
+ if active_profile == "default":
+ return (
+ "Active Hermes profile: default. Other profiles (if any) live "
+ "under " + _root_str + "/profiles/<name>/. Each profile has its own "
+ "skills/, plugins/, cron/, and memories/ that affect a different "
+ "session than this one. Do not modify another profile's "
+ "skills/plugins/cron/memories unless the user explicitly directs "
+ "you to."
+ )
+ # A non-default name is only returned when the resolved home is ALREADY
+ # <root>/profiles/<name>, so the profile home is the session home itself.
+ profile_home = _home_str
+ default_root = get_default_hermes_root()
+ return (
+ f"Active Hermes profile: {active_profile}. This session reads "
+ f"and writes {profile_home}/. The default "
+ f"profile's data lives at {default_root}/skills/, {default_root}/plugins/, "
+ f"{default_root}/cron/, {default_root}/memories/ — those belong to a "
+ f"different session run from a different shell. Do NOT modify "
+ f"another profile's skills/plugins/cron/memories unless the user "
+ f"explicitly directs you to."
+ )
+
+
+def _platform_hint(agent: Any) -> str:
+ """Built-in/plugin platform hint + Telegram rich-messages opt-in + config
+ override + desktop TUI clarifier."""
+ platform_key = (agent.platform or "").lower().strip()
+ _default_hint = ""
+ if platform_key in PLATFORM_HINTS:
+ _default_hint = PLATFORM_HINTS[platform_key]
+ elif platform_key:
+ try:
+ from gateway.platform_registry import platform_registry
+ _entry = platform_registry.get(platform_key)
+ if _entry and _entry.platform_hint:
+ _default_hint = _entry.platform_hint
+ except Exception:
+ pass
+
+ # Same precedence the adapter uses: top-level platforms.telegram.extra
+ # overrides gateway.platforms.telegram.extra at the leaf.
+ if platform_key == "telegram" and _default_hint:
+ try:
+ from hermes_cli.config import load_config_readonly
+ _cfg = load_config_readonly()
+ _gw_tg_extra = (((_cfg.get("gateway") or {}).get("platforms") or {}).get("telegram") or {}).get("extra")
+ _top_tg_extra = ((_cfg.get("platforms") or {}).get("telegram") or {}).get("extra")
+ if not isinstance(_gw_tg_extra, dict):
+ _gw_tg_extra = {}
+ if not isinstance(_top_tg_extra, dict):
+ _top_tg_extra = {}
+ if {**_gw_tg_extra, **_top_tg_extra}.get("rich_messages"):
+ _default_hint = _default_hint.rstrip() + " " + TELEGRAM_RICH_MESSAGES_HINT
+ except Exception:
+ pass # Config read failure — fall back to base hint only
+
+ _effective_hint = _resolve_platform_hint(agent, platform_key, _default_hint)
+ if platform_key == "tui" and _effective_hint:
+ _effective_hint = _tui_embedded_pane_clarifier(_effective_hint)
+ return _effective_hint
+
+
+def _zone_bits(now: Any, tz: Any) -> List[str]:
+ """IANA key, abbreviation (if different) and UTC offset — all constant for
+ the day, so the byte-stable date line stays cacheable."""
+ bits = []
+ _iana = getattr(tz, "key", None)
+ if _iana:
+ bits.append(_iana)
+ _abbrev = now.strftime("%Z")
+ if _abbrev and _abbrev != _iana:
+ bits.append(_abbrev)
+ _offset = now.strftime("%z")
+ if _offset: # '-0400' -> 'UTC-04:00'
+ bits.append(f"UTC{_offset[:3]}:{_offset[3:]}")
+ return bits
+
+
+def _timestamp_line(agent: Any) -> str:
+ """Date-only (not minute-precision) so the prompt is byte-stable for the
+ day; zone + offset included so tools that need an explicit offset don't
+ have to guess EST vs EDT. Long-lived sessions get an "as of" line on
+ rebuild days (the cache prefix is already invalidated at that boundary)."""
+ from hermes_time import get_timezone as _hermes_tz, now as _hermes_now
+ now = _hermes_now()
+ _bits = _zone_bits(now, _hermes_tz())
+ _zone_suffix = f" ({', '.join(_bits)})" if _bits else ""
+ _start = _session_start_like(agent, now)
+ timestamp_line = f"Conversation started: {_start.strftime('%A, %B %d, %Y')}{_zone_suffix}"
+ if now.strftime("%Y%m%d") != _start.strftime("%Y%m%d"):
+ timestamp_line += (
+ f"\nToday's date (as of the last context rebuild): "
+ f"{now.strftime('%A, %B %d, %Y')} — trust this over the start "
+ f"date for what day it is now; query tools for exact time."
+ )
+ if getattr(agent, "_bot_chat_timeless_prompt", False):
+ timestamp_line = f"Timezone: {', '.join(_bits)}" if _bits else ""
+ if agent.pass_session_id and agent.session_id:
+ timestamp_line += f"\nSession ID: {agent.session_id}"
+ if agent.model:
+ timestamp_line += f"\nModel: {agent.model}"
+ if agent.provider:
+ timestamp_line += f"\nProvider: {agent.provider}"
+ if agent.platform:
+ timestamp_line += f"\nPlatform: {agent.platform}"
+ return timestamp_line
+
+
+def _memory_parts(agent: Any) -> List[str]:
+ """Built-in memory/USER.md blocks plus the external provider block (gated on
+ the same check ``inject_memory_provider_tools`` uses, so we never advertise
+ tools the toolset config gated off)."""
+ parts: List[str] = []
+ if agent._memory_store:
+ if agent._memory_enabled:
+ mem_block = agent._memory_store.format_for_system_prompt("memory")
+ if mem_block:
+ parts.append(mem_block)
+ if agent._user_profile_enabled:
+ user_block = agent._memory_store.format_for_system_prompt("user")
+ if user_block:
+ parts.append(user_block)
+ if agent._memory_manager:
+ try:
+ from agent.memory_manager import memory_provider_tools_exposed as _mem_exposed
+ except Exception:
+ _mem_exposed = None
+ if _mem_exposed is None or _mem_exposed(agent):
+ try:
+ _ext_mem_block = agent._memory_manager.build_system_prompt()
+ if _ext_mem_block:
+ parts.append(_ext_mem_block)
+ except Exception:
+ pass
+ return parts
+
+
def build_system_prompt_parts(agent: Any, system_message: Optional[str] = None) -> Dict[str, str]:
"""Assemble the system prompt as three ordered cache tiers.
- Returns a dict with three keys:
- * ``stable`` — the cross-session-stable prefix, through the coding
- operating brief when a workspace snapshot follows.
- * ``context`` — the workspace snapshot followed by the remaining
- session-stable guidance, context files, and caller-supplied
- system_message.
- * ``volatile`` — skills index, memory snapshot, user profile,
- external memory provider block, timestamp line.
-
- Joined into a single string by :func:`build_system_prompt` and
- cached on ``agent._cached_system_prompt`` for the lifetime of the
- AIAgent. Hermes never re-renders parts of this string mid-
- session — that's the only way to keep upstream prompt caches
- warm across turns.
+ ``stable`` runs through the coding operating brief when a workspace
+ snapshot follows; ``context`` holds the snapshot, the remaining
+ session-stable guidance, context files and the caller ``system_message``;
+ ``volatile`` holds skills index, memory, user profile, external memory
+ block and the timestamp line. Never re-rendered mid-session.
"""
- # Local import to avoid pulling model_tools at module load. Tests
- # patch ``run_agent.get_toolset_for_tool`` and similar helpers, so
- # we resolve through ``_ra()`` to honor those patches.
_r = _ra()
- # Resolve the model's context window once so context-file caps can scale
- # to it (dynamic cap — see prompt_builder._dynamic_context_file_max_chars).
- # None falls back to the historical flat default. This value is stable for
- # the life of the conversation, so it does not threaten prompt caching.
+ # Model context window scales the context-file caps; stable per conversation.
_ctx_len: Optional[int] = None
_cc = getattr(agent, "context_compressor", None)
if _cc is not None:
@@ -468,198 +601,58 @@ def build_system_prompt_parts(agent: Any, system_message: Optional[str] = None)
# ── Stable tier ────────────────────────────────────────────────
stable_parts: List[str] = []
-
- # Try SOUL.md as primary identity unless the caller explicitly skipped it.
- # Some execution modes (cron) still want HERMES_HOME persona while keeping
- # cwd project instructions disabled.
+ # SOUL.md is primary identity (cron keeps the persona while skipping cwd
+ # instructions). Scoped to the agent's OWN home — see _agent_home.
_soul_loaded = False
if agent.load_soul_identity or not agent.skip_context_files:
- # Scope the SOUL.md read to the agent's OWN home (see _agent_home) —
- # ambient resolution on a thread that lost the HERMES_HOME ContextVar
- # reads the launch profile's SOUL.md instead (#50233).
_soul_content = _r.load_soul_md(_ctx_len, home_override=_agent_home(agent))
if _soul_content:
stable_parts.append(_soul_content)
_soul_loaded = True
-
if not _soul_loaded:
- # Fallback to hardcoded identity
stable_parts.append(DEFAULT_AGENT_IDENTITY)
- # Pointer to the docs (and, when it exists, the hermes-agent skill) for
- # user questions about Hermes itself. The skill_view() pointer is a
- # dangling reference in two cases — no skill tools in the toolset
- # (Blank Slate) OR the hermes-agent skill not installed — so the
- # variant is chosen AFTER the skills index is built (see below) and
- # this slot holds its position. Toolset and skill set are fixed
- # per-session, so cache-safe either way.
+ # The skill_view() pointer dangles without skill tools OR without the
+ # hermes-agent skill installed, so the variant is chosen after the skills
+ # index is built; this slot holds its position.
_has_skill_view = "skill_view" in (agent.valid_tool_names or set())
_help_guidance_slot = len(stable_parts)
stable_parts.append(HERMES_AGENT_HELP_GUIDANCE_NO_SKILLS)
- # Universal task-completion / no-fabrication guidance. Applied to ALL
- # models regardless of tool_use_enforcement gating — the failure modes
- # this targets (stopping after a stub; fabricating output when a real
- # path is blocked) are not model-family specific. Gated only by
- # config.yaml ``agent.task_completion_guidance`` (default True) so
- # users who want a leaner prompt can turn it off.
+ # Universal (model-agnostic) guidance, each gated by its config.yaml key.
if getattr(agent, "_task_completion_guidance", True) and agent.valid_tool_names:
stable_parts.append(TASK_COMPLETION_GUIDANCE)
-
- # Universal parallel-tool-call guidance. Tells the model to batch
- # independent tool calls into one assistant turn rather than emitting one
- # call per turn — the runtime already runs independent calls concurrently
- # (read-only tools always; non-overlapping path-scoped file ops), so the
- # only thing missing was steering the model to produce the batch. Cuts
- # round-trips and the resent-context cost that compounds over a long
- # conversation. Gated by config.yaml ``agent.parallel_tool_call_guidance``
- # (default True) and only injected when tools are actually loaded.
if getattr(agent, "_parallel_tool_call_guidance", True) and agent.valid_tool_names:
stable_parts.append(PARALLEL_TOOL_CALL_GUIDANCE)
-
- # Tool-aware behavioral guidance: only inject when the tools are loaded
- tool_guidance = []
- # MEMORY_GUIDANCE instructs the model to save facts to the built-in
- # MEMORY.md/USER.md stores. With both disabled in config no store is built,
- # so the guidance would steer the model at a tool whose every call returns
- # "Memory is not available". Defaults to True for the rare code paths that
- # build an agent view without going through agent_init.
- # When only the user profile store is enabled, the narrower
- # USER_PROFILE_GUIDANCE is injected instead — the full block instructs the
- # model to write notes to a MEMORY.md store that does not exist.
- _mem_enabled = getattr(agent, "_memory_enabled", True)
- _profile_enabled = getattr(agent, "_user_profile_enabled", True)
- if "memory" in agent.valid_tool_names:
- if _mem_enabled:
- tool_guidance.append(MEMORY_GUIDANCE)
- elif _profile_enabled:
- tool_guidance.append(USER_PROFILE_GUIDANCE)
- if "session_search" in agent.valid_tool_names:
- tool_guidance.append(SESSION_SEARCH_GUIDANCE)
- if "skill_manage" in agent.valid_tool_names:
- tool_guidance.append(SKILLS_GUIDANCE)
- # Kanban worker/orchestrator lifecycle — only present when the
- # dispatcher spawned this process (kanban_show check_fn gates on
- # HERMES_KANBAN_TASK env var). Normal chat sessions never see
- # this block. Resolved once at __init__ (see _kanban_worker_guidance).
- _kanban_guidance = getattr(agent, "_kanban_worker_guidance", None)
- if _kanban_guidance:
- tool_guidance.append(_kanban_guidance)
- elif _kanban_guidance is None and "kanban_show" in agent.valid_tool_names:
- # Fallback for code paths that bypass agent_init (rare).
- tool_guidance.append(KANBAN_GUIDANCE)
- if tool_guidance:
- stable_parts.append(" ".join(tool_guidance))
-
- # Steering only lands inside tool results, so it's only reachable when the
- # agent has tools. Static text → byte-stable prompt (no cache hit).
+ _tool_block = _tool_guidance_block(agent)
+ if _tool_block:
+ stable_parts.append(_tool_block)
+ # Steering only lands inside tool results, so only reachable with tools.
if agent.valid_tool_names:
stable_parts.append(STEER_CHANNEL_NOTE)
- # Tool-use enforcement: tells the model to actually call tools instead
- # of describing intended actions. Controlled by config.yaml
- # agent.tool_use_enforcement:
- # "auto" (default) — matches TOOL_USE_ENFORCEMENT_MODELS
- # true — always inject (all models)
- # false — never inject
- # list — custom model-name substrings to match
+ # agent.tool_use_enforcement / agent.execution_guidance: "auto" (default)
+ # matches the hardcoded model lists; true/false force; a list gives custom
+ # model-name substrings. Execution guidance is an independent gate so
+ # DeepSeek/Kimi/Qwen-class models get it even with enforcement off.
if agent.valid_tool_names:
- _enforce = agent._tool_use_enforcement
- _inject = False
- if _enforce is True or (isinstance(_enforce, str) and _enforce.lower() in {"true", "always", "yes", "on"}):
- _inject = True
- elif _enforce is False or (isinstance(_enforce, str) and _enforce.lower() in {"false", "never", "no", "off"}):
- _inject = False
- elif isinstance(_enforce, list):
- model_lower = (agent.model or "").lower()
- _inject = any(p.lower() in model_lower for p in _enforce if isinstance(p, str))
- else:
- # "auto" or any unrecognised value — use hardcoded defaults
- model_lower = (agent.model or "").lower()
- _inject = any(p in model_lower for p in TOOL_USE_ENFORCEMENT_MODELS)
- if _inject:
+ if _model_gate(agent._tool_use_enforcement, agent.model, TOOL_USE_ENFORCEMENT_MODELS):
stable_parts.append(TOOL_USE_ENFORCEMENT_GUIDANCE)
_model_lower = (agent.model or "").lower()
- # Google model operational guidance (conciseness, absolute
- # paths, parallel tool calls, verify-before-edit, etc.)
if "gemini" in _model_lower or "gemma" in _model_lower:
stable_parts.append(GOOGLE_MODEL_OPERATIONAL_GUIDANCE)
-
- # Execution-discipline guidance (tool persistence, mandatory tool use
- # for arithmetic, external-write read-back, count reconciliation,
- # literal preservation, verification-gated completion). Historically
- # nested inside the tool-use-enforcement branch and fenced to
- # gpt/codex/grok; now an independent gate so DeepSeek/Kimi/Qwen-class
- # models receive it even when tool_use_enforcement is off. Controlled
- # by config.yaml agent.execution_guidance:
- # "auto" (default) — matches EXECUTION_GUIDANCE_MODELS
- # true — always inject (all models)
- # false — never inject
- # list — custom model-name substrings to match
- # Resolved once at session start keyed on the (fixed) model name, so
- # the system prompt stays byte-stable for the life of the conversation.
- if agent.valid_tool_names:
- _exec_guidance = getattr(agent, "_execution_guidance", "auto")
- _exec_inject = False
- if _exec_guidance is True or (isinstance(_exec_guidance, str) and _exec_guidance.lower() in {"true", "always", "yes", "on"}):
- _exec_inject = True
- elif _exec_guidance is False or (isinstance(_exec_guidance, str) and _exec_guidance.lower() in {"false", "never", "no", "off"}):
- _exec_inject = False
- elif isinstance(_exec_guidance, list):
- model_lower = (agent.model or "").lower()
- _exec_inject = any(p.lower() in model_lower for p in _exec_guidance if isinstance(p, str))
- else:
- # "auto" or any unrecognised value — use hardcoded defaults
- model_lower = (agent.model or "").lower()
- _exec_inject = any(p in model_lower for p in EXECUTION_GUIDANCE_MODELS)
- if _exec_inject:
+ if _model_gate(getattr(agent, "_execution_guidance", "auto"), agent.model, EXECUTION_GUIDANCE_MODELS):
from agent.prompt_builder import execution_guidance_text
stable_parts.append(execution_guidance_text(agent.valid_tool_names))
- has_skills_tools = any(name in agent.valid_tool_names for name in ['skills_list', 'skill_view', 'skill_manage'])
- if has_skills_tools:
- avail_toolsets = {
- toolset
- for toolset in (
- _r.get_toolset_for_tool(tool_name) for tool_name in agent.valid_tool_names
- )
- if toolset
- }
- # Focus mode (opt-in) demotes non-coding skill categories to
- # names-only in the index (never hidden — skill_view/skills_list
- # reach everything, and every name stays visible for recall). The
- # default coding posture leaves the index untouched.
- _compact_cats = frozenset()
- try:
- from agent.coding_context import coding_compact_skill_categories
-
- _compact_cats = coding_compact_skill_categories(
- platform=agent.platform, cwd=resolve_context_cwd()
- )
- except Exception:
- _compact_cats = frozenset()
- skills_prompt = _r.build_skills_system_prompt(
- available_tools=agent.valid_tool_names,
- available_toolsets=avail_toolsets,
- compact_categories=_compact_cats or None,
- skills_dir_override=_agent_skills_dir(agent),
- )
- else:
- skills_prompt = ""
-
- # Resolve the help-guidance variant now that the skills index exists:
- # the skill-pointer variant requires BOTH skill_view in the toolset AND
- # the hermes-agent skill actually present in the index (gating on the
- # rendered index line keeps this a pure string check — no second
- # filesystem scan, and it inherits the index cache's stability).
+ skills_prompt = _skills_prompt(agent, _r)
+ # Skill-pointer variant requires BOTH skill_view AND the hermes-agent skill
+ # in the rendered index (pure string check — inherits the index's stability).
if _has_skill_view and "- hermes-agent:" in skills_prompt:
stable_parts[_help_guidance_slot] = HERMES_AGENT_HELP_GUIDANCE
- # Alibaba Coding Plan API always returns "glm-4.7" as model name regardless
- # of the requested model. Inject explicit model identity into the system prompt
- # so the agent can correctly report which model it is (workaround for API bug).
- # Stable for the lifetime of an agent instance — model and provider are fixed
- # at construction time.
+ # Alibaba Coding Plan always reports "glm-4.7" as the model name; inject
+ # the real identity so the agent can answer correctly.
if agent.provider == "alibaba":
_model_short = agent.model.split("/")[-1] if "/" in agent.model else agent.model
stable_parts.append(
@@ -669,18 +662,13 @@ def build_system_prompt_parts(agent: Any, system_message: Optional[str] = None)
f"not on any model name returned by the API."
)
- # Environment hints (WSL, Termux, etc.) — tell the agent about the
- # execution environment so it can translate paths and adapt behavior.
- # Stable for the lifetime of the process.
_env_hints = _r.build_environment_hints()
if _env_hints:
stable_parts.append(_env_hints)
- # Coding posture (base Hermes, any interactive coding surface in a code
- # workspace — see agent/coding_context.py). Keep the operating brief in
- # the cross-session-stable prefix, while placing the live git/workspace
- # snapshot behind its own cache boundary. The post-snapshot blocks must
- # stay in their historical position after the workspace snapshot.
+ # Coding posture: operating brief stays in the stable prefix; the live
+ # git/workspace snapshot sits behind its own cache boundary, and the blocks
+ # below it must keep their historical post-snapshot position.
coding_workspace_parts: List[str] = []
coding_trailing_parts: List[str] = []
if agent.valid_tool_names:
@@ -695,25 +683,15 @@ def build_system_prompt_parts(agent: Any, system_message: Optional[str] = None)
)
stable_parts.extend(coding_prefix_parts)
except Exception:
- # Coding-context probing must never block prompt build.
- pass
-
- # Guidance assembled after the coding posture historically followed the
- # workspace snapshot. With no snapshot, the coding tail instead remains
- # directly after the coding prefix in the cacheable prefix.
+ pass # Coding-context probing must never block prompt build.
if coding_workspace_parts:
post_workspace_parts: List[str] = []
else:
stable_parts.extend(coding_trailing_parts)
post_workspace_parts = stable_parts
- # Local Python toolchain probe — names python/pip/uv/PEP-668 state when
- # something is non-default so the model can pick the right install
- # strategy without discovering by failure. Emits a single line; emits
- # NOTHING when the environment is clean (no token cost). Skipped
- # entirely for remote terminal backends (the host's Python state is
- # irrelevant when tools run inside docker/modal/ssh). Gated by
- # config.yaml ``agent.environment_probe`` (default True).
+ # Local Python toolchain probe: one line, nothing when the env is clean,
+ # skipped for remote terminal backends. config.yaml agent.environment_probe.
if getattr(agent, "_environment_probe", True):
try:
from tools.env_probe import get_environment_probe_line
@@ -721,184 +699,31 @@ def build_system_prompt_parts(agent: Any, system_message: Optional[str] = None)
if _probe_line:
post_workspace_parts.append(_probe_line)
except Exception:
- # Probe failure must never block prompt build.
- pass
-
- # Bot Mode teammate protocol — injected ONLY into a bot's canonical
- # "Bot Chat" session (the conversation teammate bots message into via
- # `hermes -p <bot> chat --in ~ -c "Bot Chat"` and the desktop pins), on
- # installs where Bot Mode manages profiles (ui_meta['hermes-bots']).
- # Regular sessions never carry it — the desktop's composer middleware
- # owns the @mention send path. Title is read once at first build and the
- # rendered prompt is cached + DB-restored, so this is cache-safe.
- # Gated by config.yaml ``agent.bot_mode_protocol`` (default True).
+ pass # Probe failure must never block prompt build.
if getattr(agent, "_bot_mode_protocol", True):
- try:
- from tools.bot_mode_probe import (
- BOT_CHAT_TITLE,
- epoch_line,
- get_bot_mode_protocol_section,
- )
- _title = str(getattr(agent, "_session_title_hint", "") or "").strip()
- if not _title:
- _sdb = getattr(agent, "_session_db", None)
- _sid = getattr(agent, "session_id", None)
- _title = str((_sdb.get_session_title(_sid) if (_sdb and _sid) else None) or "").strip()
- if _title == BOT_CHAT_TITLE:
- _bot_section = get_bot_mode_protocol_section(_agent_home(agent))
- if _bot_section:
- post_workspace_parts.append(_bot_section)
- # Eternal-session support: stamp the capability epoch so
- # the restore path can detect user-initiated capability
- # changes (skills/toolsets/MCP/SOUL/roster) and rebuild
- # ONCE per change instead of waiting for /new or
- # compression. Also marks this prompt as timeless — the
- # volatile timestamp line is omitted (see below), since a
- # birth date pinned in a session that lives for months is
- # misinformation.
- post_workspace_parts.append(epoch_line(_agent_home(agent)))
- agent._bot_chat_timeless_prompt = True
- except Exception:
- pass
-
- # Active-profile hint — names the Hermes profile the agent is running
- # under so it doesn't conflate ~/.hermes/skills/ (default profile) with
- # ~/.hermes/profiles/<active>/skills/ (this profile's). Deterministic
- # for the lifetime of the agent — profile name doesn't change
- # mid-session, so this doesn't break the prompt cache.
- # See file_safety._resolve_active_profile_name + classify_cross_profile_target
- # for the matching tool-side guard.
- #
- # Resolve from the agent's OWN home first (its session_db path), not the
- # ambient HERMES_HOME: on a build thread that lost the ContextVar this
- # line would otherwise print "default" for a bot profile — the same
- # thread-fallback bug that leaked default's skills index.
- _agent_home_path = _agent_home(agent)
- active_profile = "default"
- try:
- if _agent_home_path is not None:
- active_profile = _profile_name_for_home(_agent_home_path)
- else:
- from agent.file_safety import _resolve_active_profile_name
- active_profile = _resolve_active_profile_name()
- except Exception:
- active_profile = "default"
- # Home string for the message text: prefer the agent's own home so the
- # paths named match the profile just resolved. When we have an explicit
- # agent home, the root (where the default profile's data lives) comes
- # from get_default_hermes_root(): get_hermes_home() on a bound profile
- # session is the PROFILE dir, which would misname the default profile's
- # paths. Without an agent home, keep the ambient resolution byte-identical
- # to the legacy behavior (and patchable via this module's get_hermes_home).
- if _agent_home_path is not None:
- _home_str = str(_agent_home_path)
- _root_str = str(get_default_hermes_root())
- else:
- _home_str = _root_str = str(get_hermes_home())
- if active_profile == "default":
- post_workspace_parts.append(
- "Active Hermes profile: default. Other profiles (if any) live "
- "under " + _root_str + "/profiles/<name>/. Each profile has its own "
- "skills/, plugins/, cron/, and memories/ that affect a different "
- "session than this one. Do not modify another profile's "
- "skills/plugins/cron/memories unless the user explicitly directs "
- "you to."
- )
- else:
- # A non-default name is only ever returned when the resolved home is
- # ALREADY <root>/profiles/<name> — that is exactly how both
- # _profile_name_for_home() and _resolve_active_profile_name() derive
- # it. So the profile home is the session home itself; appending
- # /profiles/<name> again doubled it (#72894). The default profile's
- # data sits at the ROOT (get_default_hermes_root()), which in ambient
- # profile mode is NOT get_hermes_home().
- profile_home = _home_str
- default_root = get_default_hermes_root()
- post_workspace_parts.append(
- f"Active Hermes profile: {active_profile}. This session reads "
- f"and writes {profile_home}/. The default "
- f"profile's data lives at {default_root}/skills/, {default_root}/plugins/, "
- f"{default_root}/cron/, {default_root}/memories/ — those belong to a "
- f"different session run from a different shell. Do NOT modify "
- f"another profile's skills/plugins/cron/memories unless the user "
- f"explicitly directs you to."
- )
-
- platform_key = (agent.platform or "").lower().strip()
- # Resolve the built-in/plugin default hint for this platform, then apply
- # any per-platform override from config (platform_hints.<platform>).
- _default_hint = ""
- if platform_key in PLATFORM_HINTS:
- _default_hint = PLATFORM_HINTS[platform_key]
- elif platform_key:
- # Check plugin registry for platform-specific LLM guidance
- try:
- from gateway.platform_registry import platform_registry
- _entry = platform_registry.get(platform_key)
- if _entry and _entry.platform_hint:
- _default_hint = _entry.platform_hint
- except Exception:
- pass
-
- # For Telegram: append the rich-messages extension only when the user has
- # opted in to ``gateway.platforms.telegram.extra.rich_messages: true``
- # (the canonical location the adapter reads from). Merge with the
- # top-level ``platforms.telegram.extra`` so config-wizard writes and
- # dashboard-setup keys are also visible — same precedence the adapter
- # uses: top-level platform overrides gateway.platforms at the leaf.
- if platform_key == "telegram" and _default_hint:
- try:
- from hermes_cli.config import load_config_readonly
- _cfg = load_config_readonly()
- _gw_tg_extra = (((_cfg.get("gateway") or {}).get("platforms") or {}).get("telegram") or {}).get("extra")
- _top_tg_extra = ((_cfg.get("platforms") or {}).get("telegram") or {}).get("extra")
- if not isinstance(_gw_tg_extra, dict):
- _gw_tg_extra = {}
- if not isinstance(_top_tg_extra, dict):
- _top_tg_extra = {}
- _tg_extra = {**_gw_tg_extra, **_top_tg_extra}
- if _tg_extra.get("rich_messages"):
- _default_hint = _default_hint.rstrip() + " " + TELEGRAM_RICH_MESSAGES_HINT
- except Exception:
- pass # Config read failure — fall back to base hint only
-
- _effective_hint = _resolve_platform_hint(agent, platform_key, _default_hint)
- if platform_key == "tui" and _effective_hint:
- _effective_hint = _tui_embedded_pane_clarifier(_effective_hint)
+ post_workspace_parts.extend(_bot_mode_parts(agent))
+ post_workspace_parts.append(_active_profile_line(agent))
+ _effective_hint = _platform_hint(agent)
if _effective_hint:
post_workspace_parts.append(_effective_hint)
# ── Context tier (cwd-dependent, may change between sessions) ─
context_parts: List[str] = []
-
if coding_workspace_parts:
context_parts.extend(coding_workspace_parts)
context_parts.extend(coding_trailing_parts)
context_parts.extend(post_workspace_parts)
-
- # Note: ephemeral_system_prompt is NOT included here. It's injected at
- # API-call time only so it stays out of the cached/stored system prompt.
+ # ephemeral_system_prompt is injected at API-call time only, never cached.
if system_message is not None:
context_parts.append(system_message)
-
if not agent.skip_context_files:
- # Prefer the configured TERMINAL_CWD (gateway mode). When unset (local
- # CLI), None lets build_context_files_prompt fall back to the launch
- # dir — the user's real cwd there, but the install dir for the gateway
- # daemon, which is why the gateway sets TERMINAL_CWD.
- #
- # allow_install_tree_fallback: for cli/tui the launch dir IS the
- # user's shell cwd, so an in-tree fallback is a deliberate choice
- # (developing Hermes). Every other surface (desktop chat panel,
- # gateway daemons) self-spawns into the install tree, where the
- # fallback would inject this repo's contributor AGENTS.md (#64590).
+ # TERMINAL_CWD when set (gateway); None lets discovery fall back to the
+ # launch dir. The install-tree fallback is only legitimate for cli/tui
+ # where the launch dir IS the user's shell cwd; desktop-pinned launch
+ # dirs are treated as the fallback they really are so the guard can
+ # reject Hermes's bundled contributor AGENTS.md.
context_cwd = resolve_context_cwd()
if getattr(agent, "_context_cwd_is_launch_artifact", False):
- # Desktop session creation pins the backend launch directory so
- # tools have a deterministic cwd even when the user picked no
- # workspace. Preserve that tool routing, but let context discovery
- # see it as the fallback it really is. The install-tree guard can
- # then reject Hermes's bundled contributor AGENTS.md (#97448).
context_cwd = None
context_files_prompt = _r.build_context_files_prompt(
cwd=context_cwd, skip_soul=_soul_loaded,
@@ -909,122 +734,17 @@ def build_system_prompt_parts(agent: Any, system_message: Optional[str] = None)
context_parts.append(context_files_prompt)
# ── Volatile tier (most likely to differ on a rebuild; kept last so the stable prefix stays reusable) ──
+ # Skills are runtime-mutable, so the index leads the volatile band: on a
+ # longest-prefix backend an unchanged index still falls inside the reused
+ # prefix and a changed one only re-prefills from here on.
volatile_parts: List[str] = []
- # Skills are runtime-mutable: the agent adds and patches them across a
- # session (SKILLS_GUIDANCE tells it to patch a skill the moment it goes
- # stale). The built prompt is cached per session and only rebuilt on
- # compaction/restore (see build_system_prompt), so a skill change is not
- # byte-stable across rebuilds. With the index in the stable band, a rebuild
- # that picked up a skill change would bust the cached prefix from the index
- # down, taking the whole scaffold with it. Render it at the FRONT of the
- # volatile band instead, ahead of the turn-varying memory/timestamp tail:
- # on an implicit longest-prefix backend an unchanged index still falls
- # inside the reused prefix, and a changed one only re-prefills from here on.
- # (No effect for single-block cache_control backends, where the whole
- # system message is one cache unit regardless of internal order.)
if skills_prompt:
volatile_parts.append(skills_prompt)
-
- if agent._memory_store:
- if agent._memory_enabled:
- mem_block = agent._memory_store.format_for_system_prompt("memory")
- if mem_block:
- volatile_parts.append(mem_block)
- # USER.md is always included when enabled.
- if agent._user_profile_enabled:
- user_block = agent._memory_store.format_for_system_prompt("user")
- if user_block:
- volatile_parts.append(user_block)
-
- # External memory provider system prompt block (additive to built-in).
- # Gated on the same check ``inject_memory_provider_tools`` uses so we
- # never advertise provider tools that the agent's toolset configuration
- # has already gated off (#81014).
- if agent._memory_manager:
- try:
- from agent.memory_manager import memory_provider_tools_exposed as _mem_exposed
- except Exception:
- _mem_exposed = None
- if _mem_exposed is None or _mem_exposed(agent):
- try:
- _ext_mem_block = agent._memory_manager.build_system_prompt()
- if _ext_mem_block:
- volatile_parts.append(_ext_mem_block)
- except Exception:
- pass
-
- # Plugin sections are intentionally confined to one coarse anchor in the
- # volatile tail. This preserves deterministic ordering and lets a resumed
- # process reconstruct the stable cache prefix without re-running plugins.
- volatile_parts.extend(
- _plugin_section_blocks(_frozen_plugin_prompt_sections(agent), "after_memory")
- )
-
- from hermes_time import get_timezone as _hermes_tz, now as _hermes_now
- now = _hermes_now()
- # Date-only (not minute-precision) so the system prompt is byte-stable
- # for the full day. Minute-precision changes invalidate prefix-cache KV
- # on every rebuild path (compression boundary, fresh-agent gateway turns,
- # session resume without a stored prompt). The model can still query the
- # exact wall-clock time via tools when it actually needs it.
- # Credit: @iamfoz (PR #20451).
- #
- # Zone and UTC offset ARE included: tools that accept instants reject naive
- # datetimes and require an explicit offset, and with the bare date the model
- # has to infer EST vs EDT on its own (a coin-flip near a DST boundary, and a
- # wrong guess silently writes the record onto the wrong day). Both values
- # are constant for the whole day -- they shift only at a DST transition --
- # so the byte-stability the comment above depends on is preserved.
- # ``get_timezone()`` returns None when no timezone is configured, in which
- # case we fall back to the abbreviation of the server-local (still tz-aware)
- # time.
- _tz = _hermes_tz()
- _zone_bits = []
- _iana = getattr(_tz, "key", None)
- if _iana:
- _zone_bits.append(_iana)
- _abbrev = now.strftime("%Z")
- if _abbrev and _abbrev != _iana:
- _zone_bits.append(_abbrev)
- _offset = now.strftime("%z")
- if _offset: # '-0400' -> 'UTC-04:00'
- _zone_bits.append(f"UTC{_offset[:3]}:{_offset[3:]}")
- _zone_suffix = f" ({', '.join(_zone_bits)})" if _zone_bits else ""
- _start = _session_start_like(agent, now)
- timestamp_line = (
- f"Conversation started: {_start.strftime('%A, %B %d, %Y')}{_zone_suffix}"
- )
- # Second line (maintainer design, salvaging #96224's anchor): long-lived
- # sessions — Bot Mode forever-chats, messenger channels people never
- # close — span many days and many compactions. A lone birth date leads
- # the model to believe it is still living in that old day. The prompt is
- # rebuilt at every compaction boundary, so stamp the rebuild day too:
- # 'started' stays anchored and byte-stable, 'as of' refreshes exactly
- # when the cache prefix is already being invalidated (compaction), so
- # the added line costs no extra cache churn. Same-day sessions skip the
- # second line entirely — nothing to correct, and the single-line shape
- # stays byte-identical for the day (prefix-cache safe).
- if now.strftime("%Y%m%d") != _start.strftime("%Y%m%d"):
- timestamp_line += (
- f"\nToday's date (as of the last context rebuild): "
- f"{now.strftime('%A, %B %d, %Y')} — trust this over the start "
- f"date for what day it is now; query tools for exact time."
- )
- # Bot Chat sessions are effectively eternal — a birth date frozen in the
- # prompt becomes confidently-wrong misinformation within days. Timeless
- # prompts keep the identity lines but drop the date (the timezone still
- # rides workspace context; live time comes from the terminal tool).
- if getattr(agent, "_bot_chat_timeless_prompt", False):
- timestamp_line = f"Timezone: {', '.join(_zone_bits)}" if _zone_bits else ""
- if agent.pass_session_id and agent.session_id:
- timestamp_line += f"\nSession ID: {agent.session_id}"
- if agent.model:
- timestamp_line += f"\nModel: {agent.model}"
- if agent.provider:
- timestamp_line += f"\nProvider: {agent.provider}"
- if agent.platform:
- timestamp_line += f"\nPlatform: {agent.platform}"
- volatile_parts.append(timestamp_line)
+ volatile_parts.extend(_memory_parts(agent))
+ # Plugin sections are confined to one coarse anchor in the volatile tail so
+ # a resumed process can reconstruct the stable prefix without re-running plugins.
+ volatile_parts.extend(_plugin_section_blocks(_frozen_plugin_prompt_sections(agent), "after_memory"))
+ volatile_parts.append(_timestamp_line(agent))
return {
"stable": "\n\n".join(p.strip() for p in stable_parts if p and p.strip()),
@@ -1034,47 +754,22 @@ def build_system_prompt_parts(agent: Any, system_message: Optional[str] = None)
def build_system_prompt(agent: Any, system_message: Optional[str] = None) -> str:
- """Assemble the full system prompt from all layers.
-
- Called once per session (cached on ``agent._cached_system_prompt``) and
- only rebuilt after context compression events. This ensures the system
- prompt is stable across all turns in a session, maximizing prefix cache
- hits.
-
- Layers are ordered cache-friendly: stable identity/guidance first,
- then session-stable context files, then per-call volatile content
- (skills index, memory, USER profile, timestamp). For explicit
- cache_control backends the whole string is one cached block. For
- implicit longest-prefix backends the order is what matters: the
- content most likely to change is rendered last, so when the prompt is
- rebuilt (on compaction/restore) the unchanged stable scaffold ahead of
- the change stays in the reused prefix.
- """
+ """Assemble the full prompt; cached on ``agent._cached_system_prompt`` and
+ only rebuilt after compression. Tiers are ordered stable -> context ->
+ volatile so implicit longest-prefix caches keep the unchanged scaffold."""
parts = build_system_prompt_parts(agent, system_message=system_message)
joined = "\n\n".join(p for p in (parts["stable"], parts["context"], parts["volatile"]) if p)
agent._cached_system_prompt_static = parts["stable"]
-
- # Surface context-file truncation warnings through the normal agent status
- # channel so gateway/CLI users see them in chat instead of only in logs.
+ # Surface context-file truncation warnings in chat, not only in logs.
for warning in drain_truncation_warnings():
agent._emit_status(warning)
-
return joined
def invalidate_system_prompt(agent: Any) -> None:
- """Invalidate the cached system prompt, forcing a rebuild on the next turn.
-
- Called after context compression events. Also reloads memory from disk
- so the rebuilt prompt captures any writes from this session, and clears
- the frozen plugin-section snapshot so plugins re-render at the same
- boundary (maintainer-directed, #95681 arc): a plugin section is just
- another prompt block carrying state — freezing it while memory, skills,
- and guidance refresh would recreate the stale-block disease inside
- plugin-land. The previous bytes are stashed so a plugin whose render
- RAISES falls back to its last good section instead of vanishing
- (fail-open guard, not a freeze).
- """
+ """Force a rebuild on the next turn (after compression). Reloads memory
+ from disk and clears the frozen plugin snapshot so plugins re-render at the
+ same boundary; the previous bytes are stashed as the fail-open fallback."""
agent._cached_system_prompt = None
agent._cached_system_prompt_static = None
_snapshot_attr = "_plugin_system_prompt_sections_snapshot"
@@ -1093,25 +788,12 @@ def reconstruct_static_prefix(
) -> None:
"""Reconstruct ``_cached_system_prompt_static`` for a stored prompt.
- The static prefix is not persisted (only the full prompt is), so any
- path that adopts a stored/kept ``_cached_system_prompt`` — session
- restore, the compression keep-prompt path, or a failover to a cache-on
- provider mid-turn (#72626) — must rebuild the stable tier to regain the
- two-block ``[static, volatile]`` system layout.
-
- Safety: the rebuilt stable tier is used ONLY when the stored prompt
- literally starts with it (checked here AND re-checked by
- ``_apply_system_cache_markers``'s ``startswith`` gate). If any
- stable-tier input changed since the prompt was persisted (identity
- changed, SOUL.md edited), the prefix mismatches, the static stays
- None, and requests fall back to the legacy layout with the stored
- prompt bytes untouched — never a rewritten prompt.
-
- A failed reconstruction is memoized per stored prompt
- (``_static_rebuild_failed_for``): ``build_system_prompt_parts`` does
- real file I/O (SOUL.md, context files, memory), and callers on the
- retry-loop hot path must not re-run it every attempt when the inputs
- haven't changed. A legitimately changed stored prompt retries once.
+ Only the full prompt is persisted, so restore / keep-prompt compression /
+ mid-turn failover to a cache-on provider must rebuild the stable tier to
+ regain the ``[static, volatile]`` layout. The rebuilt tier is used ONLY
+ when the stored prompt literally starts with it; otherwise static stays
+ None and the stored bytes are sent untouched. A failed rebuild is memoized
+ per stored prompt so the retry-loop hot path doesn't redo the file I/O.
"""
if not getattr(agent, "_use_prompt_caching", False):
return
@@ -1130,37 +812,27 @@ def reconstruct_static_prefix(
agent._static_rebuild_failed_for = None
return
except Exception:
- logger.debug(
- "static system-prefix reconstruction failed on %s",
- log_label,
- exc_info=True,
- )
+ logger.debug("static system-prefix reconstruction failed on %s", log_label, exc_info=True)
agent._cached_system_prompt_static = None
agent._static_rebuild_failed_for = stored
def format_tools_for_system_message(agent: Any) -> str:
- """Format tool definitions for the system message in the trajectory format.
-
- Returns:
- str: JSON string representation of tool definitions
- """
+ """JSON tool definitions in the trajectory format."""
if not agent.tools:
return "[]"
-
- # Convert tool definitions to the format expected in trajectories
- formatted_tools = []
- for tool in agent.tools:
- func = tool["function"]
- formatted_tool = {
- "name": func["name"],
- "description": func.get("description", ""),
- "parameters": func.get("parameters", {}),
- "required": None # Match the format in the example
- }
- formatted_tools.append(formatted_tool)
-
- return json.dumps(formatted_tools, ensure_ascii=False)
+ return json.dumps(
+ [
+ {
+ "name": tool["function"]["name"],
+ "description": tool["function"].get("description", ""),
+ "parameters": tool["function"].get("parameters", {}),
+ "required": None, # Match the format in the example
+ }
+ for tool in agent.tools
+ ],
+ ensure_ascii=False,
+ )
__all__ = [