Files
hermes-agent/hermes_cli/context_switch_guard.py
yoniebans f0f9f9ebf6 refactor(hermes_cli): the CLI through the persistence interface
Each legacy store call and direct store-file open in the CLI and cli.py moves to the interface operation that carries it. The lost-and-found and schema-history modules go: session recovery uses the backend's store admin for them, and their tests move with it, as does the CLI's sqlite3 resolution row in the resolution allowlist. The gateway label reader reads nothing when its interface handle cannot open. The legacy orphan-sweep test goes for its backend copy, and the legacy tests that imported the removed CLI modules follow the names to their new homes.
2026-11-17 17:51:20 +01:00

165 lines
6.4 KiB
Python

"""Warn when an in-session model switch will trigger preflight compression on the next turn."""
from __future__ import annotations
from typing import Any, Callable, List, Optional
from agent.model_metadata import MINIMUM_CONTEXT_LENGTH
from hermes_cli.model_switch import ModelSwitchResult, resolve_display_context_length
def _append_warning(result: ModelSwitchResult, text: str) -> None:
if result.warning_message:
result.warning_message = f"{result.warning_message} | {text}"
else:
result.warning_message = text
def _threshold_tokens(compressor: Any, model: str, context_length: int, provider: str = "") -> int:
"""The trigger the compressor WILL use after the switch (cap, model_thresholds and small-window
floor included), so the warning quotes the real number; duck-typed engines keep the plain ratio."""
preview = getattr(compressor, "preview_threshold_tokens", None)
if callable(preview):
return int(preview(model, context_length, provider))
return max(int(context_length * float(getattr(compressor, "threshold_percent", 0.5))), MINIMUM_CONTEXT_LENGTH)
def _estimate_tokens(agent: Any, messages: Optional[list[dict]]) -> Optional[int]:
cc = getattr(agent, "context_compressor", None)
if cc is None:
return None
if messages is not None:
protect = (
int(getattr(cc, "protect_first_n", 3)) + int(getattr(cc, "protect_last_n", 20)) + 1)
if len(messages) <= protect:
return None
try:
from agent.model_metadata import estimate_request_tokens_rough
system_prompt = getattr(agent, "_cached_system_prompt", None) or ""
tools = getattr(agent, "tools", None)
return int(
estimate_request_tokens_rough(
messages, system_prompt=system_prompt, tools=tools or None))
except Exception:
pass
# session_prompt_tokens is a lifetime sum, not occupancy (#126343): never a fallback here.
last = int(getattr(cc, "last_prompt_tokens", 0) or 0)
return last if last > 0 else None
def merge_preflight_compression_warning(
result: ModelSwitchResult,
*,
agent: Any = None,
messages: Optional[list[dict]] = None,
custom_providers: list | None = None,
config_context_length: int | None = None,
configured_model: str | None = None,
configured_provider: str | None = None,
configured_base_url: str | None = None) -> None:
"""If the next user message will likely preflight-compress, append a warning."""
if not result.success or agent is None:
return
if not getattr(agent, "compression_enabled", True):
return
cc = getattr(agent, "context_compressor", None)
if cc is None:
return
# Fall back to the agent's custom providers: without them the shrink warning used the
# hardcoded catalog (e.g. "qwen" → 131072) even when the provider declared 1M.
if custom_providers is None:
custom_providers = getattr(agent, "_custom_providers", None)
def _or_agent(value, attr):
return value if value is not None else getattr(agent, attr, None)
old_ctx = int(getattr(cc, "context_length", 0) or 0)
new_ctx = resolve_display_context_length(
result.new_model,
result.target_provider,
base_url=result.base_url or getattr(agent, "base_url", "") or "",
api_key=result.api_key or getattr(agent, "api_key", "") or "",
model_info=result.model_info,
custom_providers=custom_providers,
config_context_length=config_context_length,
configured_model=_or_agent(configured_model, "model"),
configured_provider=_or_agent(configured_provider, "provider"),
configured_base_url=_or_agent(configured_base_url, "base_url"))
if not new_ctx:
return
estimate = _estimate_tokens(agent, messages)
if estimate is None:
return
new_threshold = _threshold_tokens(cc, result.new_model, new_ctx, result.target_provider)
if estimate < new_threshold:
return
if int(getattr(cc, "_ineffective_compression_count", 0) or 0) >= 2:
return
parts: list[str] = []
if old_ctx and new_ctx < old_ctx:
parts.append(f"Context window shrinks ({old_ctx:,} → {new_ctx:,}). ")
parts.append(
f"Session is ~{estimate:,} tokens; "
f"{result.new_model} allows {new_ctx:,} "
f"(auto-compress at ~{new_threshold:,}). "
f"Your next message will run preflight compression before the model replies.")
_append_warning(result, "".join(parts))
def enrich_model_switch_warnings_for_gateway(
result: ModelSwitchResult,
runner: Any,
*,
session_key: str,
source: Any,
custom_providers: list | None = None,
load_gateway_config: Callable[[], dict] | None = None) -> None:
"""Gateway helper: cached agent + the session's stored transcript."""
lock = getattr(runner, "_agent_cache_lock", None)
cache = getattr(runner, "_agent_cache", None)
agent = None
if lock is not None and cache is not None:
with lock:
entry = cache.get(session_key)
if entry and entry[0] is not None:
agent = entry[0]
if agent is None:
return
configured: dict = dict.fromkeys(
("config_context_length", "configured_model", "configured_provider", "configured_base_url"))
if load_gateway_config is not None:
try:
cfg = load_gateway_config()
model_cfg = cfg.get("model", {}) if isinstance(cfg, dict) else {}
if isinstance(model_cfg, dict) and model_cfg.get("context_length") is not None:
configured.update(
config_context_length=int(model_cfg["context_length"]),
configured_model=model_cfg.get("default") or model_cfg.get("model"),
configured_provider=model_cfg.get("provider"),
configured_base_url=model_cfg.get("base_url"))
except Exception:
pass
messages = None
db = getattr(runner, "_session_db", None)
store = getattr(runner, "session_store", None)
if db is not None and store is not None:
try:
entry = store.get_or_create_session(source)
messages = [m.as_openai_message() for m in db.messages.read_conversation(entry.session_id)]
except Exception:
pass
merge_preflight_compression_warning(
result, agent=agent, messages=messages, custom_providers=custom_providers, **configured)