Python
fix(server): session titles for models that cannot turn reasoning off (OPE-201)
Devika Verma185ad5142d08
diff --git a/coworker/server/manager.py b/coworker/server/manager.pyindex 9aa5c4b3..04b8927b 100644--- a/coworker/server/manager.py+++ b/coworker/server/manager.py@@ -8094,6 +8094,9 @@ class SessionManager:# instruction ("4-5 word title") yields English titles for Chinese/Japanese/…# sessions. Same-language also relaxes the word count to a length that is natural# for the opener's script (CJK titles are a few characters, not 4-5 "words").+ # A title asked again without an effort setting (OPE-201): room for the reasoning a+ # model that cannot turn it off spends before the few words of the title.+ _AUTOTITLE_REASONING_MAX_TOKENS = 1024_AUTOTITLE_PROMPT = ("You title chat sessions. Given the user's opening message(s) — and, when ""present, the assistant's first reply for context — reply with ONLY a short "@@ -8192,31 +8195,49 @@ class SessionManager:fallback stays; the small-talk sentinel leaves auto_title unset so the turn-2retry can run."""try:- turn = await asyncio.to_thread(- engine.provider.complete,- model=engine.model,- messages=[- {"role": "system", "content": self._AUTOTITLE_PROMPT},- {- "role": "user",- "content": "\n\n".join(openers)- + (- f"\n\n[the assistant's first reply]\n{assistant}"- if assistant- else ""- ),- },- ],- temperature=0.2,- # Reasoning-routed models spend hidden tokens BEFORE emitting text; a- # tight cap plus default effort yields an empty completion and a silent- # no-op. Effort "none" means "no thinking": every provider's effort- # mapper handles it (OpenAI-vocabulary endpoints get it on the wire,- # Anthropic omits the field; see effort.NO_EFFORT), and 64 leaves- # headroom either way.- max_tokens=64,- reasoning_effort="none",- )+ messages = [+ {"role": "system", "content": self._AUTOTITLE_PROMPT},+ {+ "role": "user",+ "content": "\n\n".join(openers)+ + (+ f"\n\n[the assistant's first reply]\n{assistant}"+ if assistant+ else ""+ ),+ },+ ]+ try:+ turn = await asyncio.to_thread(+ engine.provider.complete,+ model=engine.model,+ messages=messages,+ temperature=0.2,+ # Reasoning-routed models spend hidden tokens BEFORE emitting text; a+ # tight cap plus default effort yields an empty completion and a silent+ # no-op. Effort "none" means "no thinking": every provider's effort+ # mapper handles it (OpenAI-vocabulary endpoints get it on the wire,+ # Anthropic omits the field; see effort.NO_EFFORT), and 64 leaves+ # headroom either way.+ max_tokens=64,+ reasoning_effort="none",+ )+ except Exception as exc:+ # OPE-201: some endpoints cannot turn reasoning off and refuse "none"+ # (OpenRouter GLM 5.3: 400 "Reasoning is mandatory for this endpoint and+ # cannot be disabled"). Ask once more with the model's own effort, and+ # room for the reasoning it spends before the few words of the title.+ text = str(exc).lower()+ if "reasoning" not in text and "effort" not in text:+ raise+ logger.info("autotitle for %s: retrying without reasoning effort (%s)", session_id, exc)+ turn = await asyncio.to_thread(+ engine.provider.complete,+ model=engine.model,+ messages=messages,+ temperature=0.2,+ max_tokens=self._AUTOTITLE_REASONING_MAX_TOKENS,+ )raw = (getattr(turn, "text", None) or "").strip()# Sanitize: surrounding quotes off, whitespace collapsed, capped at 60.title = " ".join(raw.strip("\"'“”‘’`").split())diff --git a/tests/test_autotitle.py b/tests/test_autotitle.pyindex f722c2b7..541fa449 100644--- a/tests/test_autotitle.py+++ b/tests/test_autotitle.py@@ -208,3 +208,39 @@ async def test_non_english_small_talk_still_hits_the_sentinel(tmp_path):await _turn(mgr, "lang3", "帮我整理季度汇报")assert len(provider.title_calls) == 2assert mgr.session_store.load("lang3").title == "季度汇报整理"+++class _SettingsRecorder(TitleAwareProvider):+ def __init__(self, chat_turns, titles):+ super().__init__(chat_turns, titles)+ self.title_settings: list[dict] = []++ def complete(self, *, model, messages, tools=None, **settings):+ if messages and "title chat sessions" in str(messages[0].get("content", "")):+ self.title_settings.append(dict(settings))+ return super().complete(model=model, messages=messages, tools=tools, **settings)+++async def test_a_model_that_cannot_turn_reasoning_off_still_gets_a_title(tmp_path):+ # OPE-201: an endpoint whose reasoning is mandatory refuses effort "none" (OpenRouter+ # GLM 5.3 answers 400). The title is asked once more without the effort, with room for+ # the reasoning the model spends first.+ refused = RuntimeError("Error code: 400 - {'error': {'message': 'Reasoning is mandatory for "+ "this endpoint and cannot be disabled.', 'code': 400}}")+ provider = _SettingsRecorder([_text("sure")], [refused, "Release Notes Draft"])+ mgr = SessionManager(workspace=tmp_path, provider=provider)+ await _turn(mgr, "s20", "draft the release notes")++ first, second = provider.title_settings+ assert first["reasoning_effort"] == "none" and first["max_tokens"] == 64+ assert "reasoning_effort" not in second+ assert second["max_tokens"] == SessionManager._AUTOTITLE_REASONING_MAX_TOKENS+ assert mgr.session_store.title_state("s20")["auto_title"] == "Release Notes Draft"+++async def test_other_title_failures_are_not_retried(tmp_path):+ provider = _SettingsRecorder([_text("ok")], [RuntimeError("model down"), "Never Used"])+ mgr = SessionManager(workspace=tmp_path, provider=provider)+ await _turn(mgr, "s21", "summarize this doc")+ assert len(provider.title_settings) == 1+ assert mgr.session_store.title_state("s21")["auto_title"] is None