Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(answer): give reasoning models room to write the synthesized answer (#3094)
Raghav Chamadiya99adcd4b5601
diff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/config.py b/packages/server/src/repowise/server/mcp_server/tool_answer/config.pyindex 5f6929ce6..10e340015 100644--- a/packages/server/src/repowise/server/mcp_server/tool_answer/config.py+++ b/packages/server/src/repowise/server/mcp_server/tool_answer/config.py@@ -313,14 +313,15 @@ _MAX_RICH_SIG_LINES = 4# Synthesis sampling. Answers target 150-400 words, so for a non-reasoning model# the token cap is headroom. A reasoning model spends the budget on hidden-# thinking first and can come back empty with a ``length`` finish_reason, so the-# env override lets such a model be given room. Low temperature: the answer-# must track the excerpts, not embellish.+# thinking first and can come back empty with a ``length`` finish_reason, so it+# gets a larger default, and the env override sets both. Low temperature: the+# answer must track the excerpts, not embellish._SYNTHESIS_MAX_TOKENS_ENV = "REPOWISE_SYNTHESIS_MAX_TOKENS"_SYNTHESIS_MAX_TOKENS_DEFAULT = 1024+_SYNTHESIS_REASONING_MAX_TOKENS_DEFAULT = 4096-def _synthesis_max_tokens() -> int:+def _synthesis_max_tokens(default: int = _SYNTHESIS_MAX_TOKENS_DEFAULT) -> int:"""The synthesis token budget, from env or the hosted-model default.An unparseable or non-positive value warns and keeps the default instead@@ -328,18 +329,19 @@ def _synthesis_max_tokens() -> int:"""raw = os.environ.get(_SYNTHESIS_MAX_TOKENS_ENV, "").strip()if not raw:- return _SYNTHESIS_MAX_TOKENS_DEFAULT+ return defaulttry:value = int(raw)except ValueError:value = 0if value <= 0:_log.warning("Ignoring unusable %s=%r", _SYNTHESIS_MAX_TOKENS_ENV, raw)- return _SYNTHESIS_MAX_TOKENS_DEFAULT+ return defaultreturn value_SYNTHESIS_MAX_TOKENS = _synthesis_max_tokens()+_SYNTHESIS_REASONING_MAX_TOKENS = _synthesis_max_tokens(_SYNTHESIS_REASONING_MAX_TOKENS_DEFAULT)_SYNTHESIS_TEMPERATURE = 0.2_SYSTEM_PROMPT = (diff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/synthesis.py b/packages/server/src/repowise/server/mcp_server/tool_answer/synthesis.pyindex b5a8c9505..182f25465 100644--- a/packages/server/src/repowise/server/mcp_server/tool_answer/synthesis.py+++ b/packages/server/src/repowise/server/mcp_server/tool_answer/synthesis.py@@ -21,6 +21,7 @@ from repowise.core.repo_config import load_repo_configfrom repowise.server.mcp_server.tool_answer.config import (_SYNTHESIS_MAX_TOKENS,_SYNTHESIS_MAX_TOKENS_ENV,+ _SYNTHESIS_REASONING_MAX_TOKENS,_SYNTHESIS_TEMPERATURE,)@@ -78,6 +79,27 @@ def _resolve_reasoning_for_answer(repo_path: Path | None) -> ReasoningMode:return resolve_reasoning(config=config)+def _synthesis_reasoning_and_budget(+ provider, reasoning: ReasoningMode+) -> tuple[ReasoningMode, int]:+ """The reasoning mode and token cap one synthesis call is sent with.++ An OpenAI reasoning model left on ``auto`` thinks at its default effort and+ can spend the whole cap before the first answer token, so synthesis asks+ for ``low`` and gets reasoning headroom. An explicit mode is kept as given.+ Other providers keep their defaults; their ``auto`` semantics differ.+ """+ try:+ modes = provider.supported_reasoning_modes()+ except Exception:+ # A provider that cannot report its modes must not fail the answer.+ modes = ()+ if getattr(provider, "provider_name", None) != "openai" or "low" not in modes:+ return reasoning, _SYNTHESIS_MAX_TOKENS+ effective: ReasoningMode = "low" if reasoning == "auto" else reasoning+ return effective, _SYNTHESIS_REASONING_MAX_TOKENS++def _load_repo_provider_config(repo_path: Path | None,) -> tuple[str | None, str | None, dict[str, str]]:@@ -315,7 +337,7 @@ def _synthesis_failure_note(exc: BaseException, provider, timeout_s: float, time)-def _empty_completion_note(provider, response) -> str:+def _empty_completion_note(provider, response, max_tokens: int = _SYNTHESIS_MAX_TOKENS) -> str:"""Note for a call that succeeded and returned no text.Measured against a local reasoning model on ollama: it spent all 1024@@ -330,7 +352,7 @@ def _empty_completion_note(provider, response) -> str:)if getattr(response, "stop_reason", None) == "max_tokens":return (- f"DEGRADED: the model used its entire {_SYNTHESIS_MAX_TOKENS}-token "+ f"DEGRADED: the model used its entire {max_tokens}-token "f"budget without emitting an answer ({who}). Reasoning models spend ""that budget on hidden thinking before any answer token; raise it "f"with {_SYNTHESIS_MAX_TOKENS_ENV}=<tokens>, or use a non-reasoning "@@ -442,6 +464,7 @@ async def synthesize(persisted."""timeout_s = _synthesis_timeout(provider)+ reasoning, max_tokens = _synthesis_reasoning_and_budget(provider, reasoning)async def _generate() -> tuple[object | None, BaseException | None]:"""Swallow the provider's own errors so only our deadline escapes.@@ -456,7 +479,7 @@ async def synthesize(await provider.generate(system_prompt=system_prompt,user_prompt=user_prompt,- max_tokens=_SYNTHESIS_MAX_TOKENS,+ max_tokens=max_tokens,temperature=_SYNTHESIS_TEMPERATURE,reasoning=reasoning,),@@ -479,7 +502,9 @@ async def synthesize(# go unpriced.await _record_synthesis_cost(provider, response, session_factory, repo_id)text = (getattr(response, "content", None) or "").strip()- return (text, None) if text else ("", _empty_completion_note(provider, response))+ if text:+ return text, None+ return "", _empty_completion_note(provider, response, max_tokens)_log.warning("get_answer LLM call failed (provider=%s, model=%s, budget=%.1fs, timed_out=%s): %s",diff --git a/tests/unit/server/mcp/test_answer_synthesis_timeout.py b/tests/unit/server/mcp/test_answer_synthesis_timeout.pyindex e835256d9..85726ced3 100644--- a/tests/unit/server/mcp/test_answer_synthesis_timeout.py+++ b/tests/unit/server/mcp/test_answer_synthesis_timeout.py@@ -21,6 +21,8 @@ from repowise.server.mcp_server.tool_answer.config import (_SYNTHESIS_MAX_TOKENS,_SYNTHESIS_MAX_TOKENS_DEFAULT,_SYNTHESIS_MAX_TOKENS_ENV,+ _SYNTHESIS_REASONING_MAX_TOKENS,+ _SYNTHESIS_REASONING_MAX_TOKENS_DEFAULT,_SYNTHESIS_TEMPERATURE,_synthesis_max_tokens,)@@ -29,6 +31,7 @@ from repowise.server.mcp_server.tool_answer.synthesis import (_MAX_TIMEOUT_S,_TIMEOUT_ENV,_synthesis_failure_note,+ _synthesis_reasoning_and_budget,_synthesis_timeout,synthesize,)@@ -512,3 +515,118 @@ async def test_degraded_answer_does_not_promise_hits_it_does_not_have():assert answerassert "matched nothing" in answerassert "ranked hit" not in answer+++# --- reasoning effort and budget per model ---------------------------------+#+# gpt-5.6-luna on ``auto`` thinks at its default effort and returned empty+# answers inside the 1024 cap. Synthesis asks it for ``low`` with headroom.+++def _openai(model: str):+ return get_provider("openai", api_key="sk-test", model=model, with_rate_limiter=False)+++def test_auto_on_an_openai_reasoning_model_becomes_low_with_headroom():+ mode, max_tokens = _synthesis_reasoning_and_budget(_openai("gpt-5.6-luna"), "auto")+ assert mode == "low"+ assert max_tokens == _SYNTHESIS_REASONING_MAX_TOKENS == _SYNTHESIS_REASONING_MAX_TOKENS_DEFAULT++[email protected]("explicit", ["none", "medium", "high"])+def test_an_explicit_reasoning_mode_is_kept(explicit):+ mode, max_tokens = _synthesis_reasoning_and_budget(_openai("gpt-5.6-luna"), explicit)+ assert mode == explicit+ assert max_tokens == _SYNTHESIS_REASONING_MAX_TOKENS+++def test_a_non_reasoning_model_keeps_auto_and_the_cap():+ assert _synthesis_reasoning_and_budget(_openai("gpt-4o"), "auto") == (+ "auto",+ _SYNTHESIS_MAX_TOKENS,+ )+++def test_other_providers_keep_their_defaults():+ anthropic = get_provider(+ "anthropic", api_key="sk-test", model="claude-haiku-4-5", with_rate_limiter=False+ )+ assert _synthesis_reasoning_and_budget(anthropic, "auto") == ("auto", _SYNTHESIS_MAX_TOKENS)+ assert _synthesis_reasoning_and_budget(_SlowProvider(0, 30.0), "auto") == (+ "auto",+ _SYNTHESIS_MAX_TOKENS,+ )+++def test_max_tokens_helper_applies_the_env_override_to_the_reasoning_default(monkeypatch):+ monkeypatch.setenv(_SYNTHESIS_MAX_TOKENS_ENV, "2000")+ assert _synthesis_max_tokens(_SYNTHESIS_REASONING_MAX_TOKENS_DEFAULT) == 2000+ monkeypatch.setenv(_SYNTHESIS_MAX_TOKENS_ENV, "junk")+ assert (+ _synthesis_max_tokens(_SYNTHESIS_REASONING_MAX_TOKENS_DEFAULT)+ == _SYNTHESIS_REASONING_MAX_TOKENS_DEFAULT+ )+++async def test_the_reasoning_settings_reach_the_call():+ class _Reasoner(_SlowProvider):+ provider_name = "openai"++ def supported_reasoning_modes(self):+ return ("auto", "none", "low", "medium", "high", "xhigh")++ class _BurnedBudget(_Reasoner):+ async def generate(self, **kwargs):+ self.calls.append(kwargs)+ return SimpleNamespace(content="", stop_reason="max_tokens")++ provider = _Reasoner(duration=0, budget=30.0)+ await synthesize(provider, "sys", "user")+ assert provider.calls[0]["reasoning"] == "low"+ assert provider.calls[0]["max_tokens"] == _SYNTHESIS_REASONING_MAX_TOKENS++ _, note = await synthesize(_BurnedBudget(duration=0, budget=30.0), "sys", "user")+ assert f"{_SYNTHESIS_REASONING_MAX_TOKENS}-token" in note+++async def test_synthesize_uses_the_resolved_reasoning_budget(monkeypatch):+ """The env override lands in this module constant at import, so patching it+ stands in for REPOWISE_SYNTHESIS_MAX_TOKENS on a reasoning model."""+ monkeypatch.setattr(synthesis_module, "_SYNTHESIS_REASONING_MAX_TOKENS", 2000)++ class _Reasoner(_SlowProvider):+ provider_name = "openai"++ def supported_reasoning_modes(self):+ return ("auto", "low")++ provider = _Reasoner(duration=0, budget=30.0)+ await synthesize(provider, "sys", "user")+ assert provider.calls[0]["max_tokens"] == 2000+++async def test_the_real_openai_provider_sends_low_effort_and_the_budget(monkeypatch):+ """auto -> low must reach the Chat Completions request as reasoning_effort."""+ provider = _openai("gpt-5.6-luna")+ sent: dict = {}++ async def _create(**kwargs):+ sent.update(kwargs)+ message = SimpleNamespace(content="answer", tool_calls=None)+ return SimpleNamespace(+ choices=[SimpleNamespace(message=message, finish_reason="stop")],+ usage=SimpleNamespace(+ prompt_tokens=10,+ completion_tokens=5,+ total_tokens=15,+ prompt_tokens_details=None,+ ),+ model="gpt-5.6-luna",+ )++ monkeypatch.setattr(provider._client.chat.completions, "create", _create)+ answer, note = await synthesize(provider, "sys", "user")++ assert note is None and answer == "answer"+ assert sent["reasoning_effort"] == "low"+ assert sent["max_completion_tokens"] == _SYNTHESIS_REASONING_MAX_TOKENS