From de8a9ffdac50ffb11a02dc8e446b10dd1fea3d3b Mon Sep 17 00:00:00 2001 From: magicray1217 Date: Fri, 17 Apr 2026 09:19:11 +0800 Subject: [PATCH] fix(agent): restrict auto-title to first exchange only maybe_auto_title() was intended to fire only on the first user-assistant exchange, but the boundary condition (user_msg_count > 2) allowed it to also fire on the second exchange. This could generate a title based on the second message instead of the opener. Tighten the guard to user_msg_count > 1 so only the very first exchange triggers title generation. Add regression test for the 2-user-messages case. Fixes NousResearch/hermes-agent#11201 --- agent/title_generator.py | 6 +++--- tests/agent/test_title_generator.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/agent/title_generator.py b/agent/title_generator.py index d6ed9200a26d..841968942908 100644 --- a/agent/title_generator.py +++ b/agent/title_generator.py @@ -110,10 +110,10 @@ def maybe_auto_title( # Count user messages in history to detect first exchange. # conversation_history includes the exchange that just happened, - # so for a first exchange we expect exactly 1 user message - # (or 2 counting system). Be generous: generate on first 2 exchanges. + # so for a first exchange we expect exactly 1 user message. + # Only generate a title on the very first exchange (user_msg_count == 1). user_msg_count = sum(1 for m in (conversation_history or []) if m.get("role") == "user") - if user_msg_count > 2: + if user_msg_count > 1: return thread = threading.Thread( diff --git a/tests/agent/test_title_generator.py b/tests/agent/test_title_generator.py index 98fb8fb21310..c3a60f9a4544 100644 --- a/tests/agent/test_title_generator.py +++ b/tests/agent/test_title_generator.py @@ -136,6 +136,22 @@ def test_skips_if_not_first_exchange(self): time.sleep(0.1) mock_auto.assert_not_called() + def test_skips_on_second_exchange(self): + """Should not fire for conversations with exactly 2 user messages (second exchange).""" + db = MagicMock() + history = [ + {"role": "user", "content": "first"}, + {"role": "assistant", "content": "response 1"}, + {"role": "user", "content": "second"}, + {"role": "assistant", "content": "response 2"}, + ] + + with patch("agent.title_generator.auto_title_session") as mock_auto: + maybe_auto_title(db, "sess-1", "second", "response 2", history) + import time + time.sleep(0.1) + mock_auto.assert_not_called() + def test_fires_on_first_exchange(self): """Should fire a background thread for the first exchange.""" db = MagicMock()