The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
Port from can1357/oh-my-pi#7306: reject answer-shaped auto-title output
Tekniumd5167831b854
diff --git a/agent/title_generator.py b/agent/title_generator.pyindex cb8b662fa44..5323f8bd047 100644--- a/agent/title_generator.py+++ b/agent/title_generator.py@@ -63,6 +63,14 @@ MAX_TITLE_INPUT_CHARS = 1000# Codex CLI independently landed on the same ~50-char slice.MAX_DERIVED_TITLE_CHARS = 48+# Upper bound on accepted title word count. Titling is a 3-7 word task; a+# small tiny-model sometimes ignores the task and answers the user's message+# instead — that answer must never become the session title (see the+# answer-shaped output guard in generate_title; port of+# can1357/oh-my-pi#7306). 12 leaves headroom for legitimate wordy titles+# while excluding full-sentence answers.+_MAX_TITLE_WORDS = 12+_TITLE_PROMPT_TEMPLATE = ("You name chat sessions. Given the user's opening message, write a title ""that lets them find this conversation again in a list.\n\n"@@ -404,7 +412,22 @@ def generate_title(extra_body={"response_format": _TITLE_RESPONSE_FORMAT},)content = response.choices[0].message.content or ""- return _clean_title(_extract_title_text(content))+ title = _clean_title(_extract_title_text(content))+ # Answer-shaped output guard: titling is a 3-7 word task, so a title+ # with many words is a model that ignored the task and answered+ # the user's message instead ("I don't have context on X — that's+ # not something I recognize..."). Truncating would store half an+ # assistant blob as the session title, which is still an assistant+ # blob — reject instead so the caller retries on the next exchange+ # (maybe_auto_title fires for the first two exchanges).+ # Port of can1357/oh-my-pi#7306.+ if title is not None and len(title.split()) > _MAX_TITLE_WORDS:+ logger.debug(+ "Rejecting answer-shaped title output (%d words > %d)",+ len(title.split()), _MAX_TITLE_WORDS,+ )+ return None+ return titleexcept Exception as e:# Log at WARNING so this shows up in agent.log without debug mode.# Full detail at debug level for operators who need the stack.diff --git a/tests/agent/test_title_generator.py b/tests/agent/test_title_generator.pyindex 56190980b7b..e4ae76b8aed 100644--- a/tests/agent/test_title_generator.py+++ b/tests/agent/test_title_generator.py@@ -90,6 +90,43 @@ class TestGenerateTitle:assert len(title) == 80assert title.endswith("...")+ def test_rejects_answer_shaped_output(self):+ """A model that ignores the titling task and answers the user's+ message returns a full sentence; without a word bound the whole+ reply (truncated mid-sentence) became the session title.+ Regression for the can1357/oh-my-pi#7306 bug class."""+ answer = (+ "I don't have context on a \"registration system\" - that's not "+ "something I recognize from this conversation, and I don't see "+ "any prior discussion or code about it here"+ )+ mock_response = MagicMock()+ mock_response.choices = [MagicMock()]+ mock_response.choices[0].message.content = answer++ with patch("agent.title_generator.call_llm", return_value=mock_response):+ assert generate_title("how does the registration system work?", "...") is None++ def test_rejects_many_short_words(self):+ """13 short words stays under the 80-char cap but is not a title."""+ mock_response = MagicMock()+ mock_response.choices = [MagicMock()]+ mock_response.choices[0].message.content = (+ "one two three four five six seven eight nine ten eleven twelve thirteen"+ )++ with patch("agent.title_generator.call_llm", return_value=mock_response):+ assert generate_title("question", "answer") is None++ def test_accepts_normal_title(self):+ """A normal 3-7 word title is unaffected by the answer-shape guard."""+ mock_response = MagicMock()+ mock_response.choices = [MagicMock()]+ mock_response.choices[0].message.content = "Investigate the title resolver bug"++ with patch("agent.title_generator.call_llm", return_value=mock_response):+ assert generate_title("question", "answer") == "Investigate the title resolver bug"+def test_invokes_failure_callback_on_exception(self):