Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions gateway/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7921,17 +7921,20 @@ async def _handle_message_with_agent(self, event, source, _quick_key: str, run_g
# Build the context prompt to inject
context_prompt = build_session_context_prompt(context, redact_pii=_redact_pii)

# If the previous session expired and was auto-reset, prepend a notice
# so the agent knows this is a fresh conversation (not an intentional /reset).
# If the previous session expired and was auto-reset, capture a notice
# for the agent BUT do NOT modify context_prompt — one-time transient
# notices must not contaminate the agent cache signature (which is
# computed from combined_ephemeral = context_prompt + ...).
# The notice is injected into combined_ephemeral AFTER _sig is computed.
_reset_context_note = None
if getattr(session_entry, 'was_auto_reset', False):
reset_reason = getattr(session_entry, 'auto_reset_reason', None) or 'idle'
if reset_reason == "suspended":
context_note = "[System note: The user's previous session was stopped and suspended. This is a fresh conversation with no prior context.]"
_reset_context_note = "[System note: The user's previous session was stopped and suspended. This is a fresh conversation with no prior context.]"
elif reset_reason == "daily":
context_note = "[System note: The user's session was automatically reset by the daily schedule. This is a fresh conversation with no prior context.]"
_reset_context_note = "[System note: The user's session was automatically reset by the daily schedule. This is a fresh conversation with no prior context.]"
else:
context_note = "[System note: The user's previous session expired due to inactivity. This is a fresh conversation with no prior context.]"
context_prompt = context_note + "\n\n" + context_prompt
_reset_context_note = "[System note: The user's previous session expired due to inactivity. This is a fresh conversation with no prior context.]"

# Send a user-facing notification explaining the reset, unless:
# - notifications are disabled in config
Expand Down Expand Up @@ -8433,6 +8436,7 @@ async def _handle_message_with_agent(self, event, source, _quick_key: str, run_g
run_generation=run_generation,
event_message_id=self._reply_anchor_for_event(event),
channel_prompt=event.channel_prompt,
reset_context_note=_reset_context_note,
)

# Stop persistent typing indicator now that the agent is done
Expand Down Expand Up @@ -14918,6 +14922,7 @@ async def _interrupt_and_clear_session(

def _evict_cached_agent(self, session_key: str) -> None:
"""Remove a cached agent for a session (called on /new, /model, etc)."""
logger.debug("Evicting cached agent for session %s", session_key)
_lock = getattr(self, "_agent_cache_lock", None)
if _lock:
with _lock:
Expand Down Expand Up @@ -15406,6 +15411,7 @@ async def _run_agent(
_interrupt_depth: int = 0,
event_message_id: Optional[str] = None,
channel_prompt: Optional[str] = None,
reset_context_note: Optional[str] = None,
) -> Dict[str, Any]:
"""
Run the agent with the given message and context.
Expand Down Expand Up @@ -16276,10 +16282,23 @@ def _interim_assistant_cb(text: str, *, already_streamed: bool = False) -> None:
except KeyError:
pass
self._init_cached_agent_for_turn(agent, _interrupt_depth)
logger.debug("Reusing cached agent for session %s", session_key)
logger.info("CACHE HIT: reusing agent for session %s (sig=%s)", session_key, _sig[:8])

Comment thread
fayenix marked this conversation as resolved.
Resolved
if agent is None:
# Config changed or first message — create fresh agent
_cached_for_log = cached
if _cached_for_log:
logger.info("CACHE MISS: sig changed (old=%s new=%s)",
_cached_for_log[1][:8], _sig[:8])
else:
logger.info("CACHE MISS: no cached agent for session %s (sig=%s)", session_key, _sig[:8])
# Inject auto-reset notice into ephemeral system prompt AFTER
# _sig computation so one-time transient notices don't contaminate
# the agent cache signature and cause per-turn evictions.
Comment thread
fayenix marked this conversation as resolved.
Resolved
# Only needed for fresh agents (cache MISS); cached agents
# already have their frozen system prompt.
if reset_context_note:
combined_ephemeral = (reset_context_note + "\n\n" + (combined_ephemeral or "")).strip()
agent = AIAgent(
model=turn_route["model"],
**turn_route["runtime"],
Expand Down Expand Up @@ -17288,6 +17307,21 @@ async def _notify_long_running():
_run_failed = _result_for_fb.get("failed") if _result_for_fb else False
if _agent is not None and hasattr(_agent, 'model') and not _run_failed:
_cfg_model = _resolve_gateway_model()
# Normalize _cfg_model the same way AIAgent.__init__ does
# (run_agent.py:1089-1090), so vendor-prefixed config values
# (e.g. "deepseek/deepseek-v4-pro") match the stripped agent
# model ("deepseek-v4-pro"). Without this, every turn evicts
# the cached agent unconditionally.
try:
from hermes_cli.model_normalize import (
_AGGREGATOR_PROVIDERS,
normalize_model_for_provider,
)
_agent_provider = getattr(_agent, 'provider', '') or ''
if _agent_provider not in _AGGREGATOR_PROVIDERS:
_cfg_model = normalize_model_for_provider(_cfg_model, _agent_provider)
except Exception:
pass
if _agent.model != _cfg_model and not self._is_intentional_model_switch(session_key, _agent.model):
# Fallback activated on a successful run — evict cached
# agent so the next message retries the primary model.
Expand Down
2 changes: 0 additions & 2 deletions gateway/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,6 @@ def build_session_context_prompt(
id_lines.append(f" - Thread: `{src.thread_id}` (use as `channel_id` for fetch_messages etc.)")
else:
id_lines.append(f" - Channel: `{src.chat_id}`")
if src.message_id:
id_lines.append(f" - Triggering message: `{src.message_id}`")
lines.extend(id_lines)
else:
lines.append("")
Expand Down