diff --git a/run_agent.py b/run_agent.py index 6c35dbbcf2594..e92f335c87cfb 100644 --- a/run_agent.py +++ b/run_agent.py @@ -189,6 +189,36 @@ from agent.trajectory import ( convert_scratchpad_to_think, has_incomplete_scratchpad, save_trajectory as _save_trajectory_to_file, ) + +# Trivial user-input pattern — used by run_conversation() to gate +# memory-provider prefetch on short, semantically-empty queries. +# Covers greetings, acknowledgements, and trailing-punctuation variants +# (e.g. "hi!", "hey.", "thanks :)"). +_RE_TRIVIAL_USER_QUERY = re.compile( + r'^(yes|no|ok|okay|sure|thanks|thank you|y|n|yep|nope|yeah|nah|' + r'hi|hey|hello|yo|sup|' + r'continue|go ahead|do it|proceed|got it|cool|nice|great|done|next|lgtm|k)' + r'[\s!?.:;,"' + "'" + r'~\u2018\u2019\u201c\u201d\u2014\u2013\u2026()\[\]{}<>*&^%$#@!+=`\u00a0]*$', + re.IGNORECASE, +) + + +def _is_trivial_user_query(query: str) -> bool: + """Return True if the query is a greeting or too trivial to warrant prefetch. + + Strips leading/trailing whitespace first, then checks against + _RE_TRIVIAL_USER_QUERY which permits common trailing punctuation + (exclamation, question mark, emoji, etc.) so variants like "hi!", + "hey.", and "thanks :)" register as trivial. + """ + if not query: + return True + stripped = query.strip() + if not stripped: + return True + if stripped.startswith("/"): + return True + return bool(_RE_TRIVIAL_USER_QUERY.match(stripped)) from utils import atomic_json_write, base_url_host_matches, base_url_hostname, env_var_enabled, normalize_proxy_url from hermes_cli.config import cfg_get @@ -12121,23 +12151,11 @@ class AIAgent: # # Skip prefetch on trivial prompts (greetings, acknowledgements) to # prevent memory-context injection on turns that carry no semantic signal. - _TRIVIAL_QUERY_RE = re.compile( - r'^(yes|no|ok|okay|sure|thanks|thank you|y|n|yep|nope|yeah|nah|' - r'hi|hey|hello|yo|sup|' - r'continue|go ahead|do it|proceed|got it|cool|nice|great|done|next|lgtm|k)$', - re.IGNORECASE, - ) - _ext_prefetch_cache = "" if self._memory_manager: try: _query = original_user_message if isinstance(original_user_message, str) else "" - _is_trivial = ( - not _query - or _query.strip().startswith("/") - or re.match(_TRIVIAL_QUERY_RE, _query.strip()) - ) - if not _is_trivial: + if not _is_trivial_user_query(_query): _ext_prefetch_cache = self._memory_manager.prefetch_all(_query) or "" except Exception: pass diff --git a/tests/honcho_plugin/test_session.py b/tests/honcho_plugin/test_session.py index 64fcfc7ebfdbd..53e26b9e4d888 100644 --- a/tests/honcho_plugin/test_session.py +++ b/tests/honcho_plugin/test_session.py @@ -1134,6 +1134,11 @@ class TestTrivialPromptHeuristic: for t in ("ok", "OK", " ok ", "y", "yes", "sure", "thanks", "lgtm", "/help", "", " "): assert HonchoMemoryProvider._is_trivial_prompt(t), f"expected trivial: {t!r}" + def test_classifier_catches_greetings(self): + """Greeting words must register as trivial so context injection is skipped.""" + for t in ("hi", "HI", "hey", "hello", "yo", "sup", " hi ", "hey!", "hello."): + assert HonchoMemoryProvider._is_trivial_prompt(t), f"expected trivial: {t!r}" + def test_classifier_lets_substantive_prompts_through(self): for t in ("hello world", "what's my name", "explain this", "ok so what's next"): assert not HonchoMemoryProvider._is_trivial_prompt(t), f"expected non-trivial: {t!r}"