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(mcp): report an empty completion instead of shipping a blank answer (#1126)
Raghav Chamadiya9eeaf137a192
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 6031ff4d5..b7fa1870e 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@@ -280,6 +280,32 @@ def _synthesis_failure_note(exc: BaseException, provider, timeout_s: float, time)+def _empty_completion_note(provider, response) -> str:+ """Note for a call that succeeded and returned no text.++ Measured against a local reasoning model on ollama: it spent all 1024+ tokens thinking and emitted an empty content block. The call did not fail,+ so nothing marked it degraded, and get_answer shipped a blank answer as a+ normal result. That is the same silent empty answer #1119 was reported as,+ reached from the other side.+ """+ who = (+ f"provider={getattr(provider, 'provider_name', None) or '?'}, "+ f"model={getattr(provider, 'model_name', None) or '?'}"+ )+ if getattr(response, "stop_reason", None) == "max_tokens":+ return (+ f"DEGRADED: the model used its entire {_SYNTHESIS_MAX_TOKENS}-token "+ f"budget without emitting an answer ({who}). Reasoning models spend "+ "that budget on hidden thinking; try a non-reasoning model for "+ "synthesis. Read the listed files to answer meanwhile."+ )+ return (+ f"DEGRADED: the model returned an empty completion ({who}). "+ "Read the listed files to answer."+ )++async def synthesize(provider, system_prompt: str, user_prompt: str) -> tuple[str, str | None]:"""Run one synthesis call. Returns ``(answer_text, failure_note)``.@@ -320,7 +346,8 @@ async def synthesize(provider, system_prompt: str, user_prompt: str) -> tuple[sttimed_out = Trueresponse, failure = None, excif failure is None:- return (getattr(response, "content", None) or "").strip(), None+ text = (getattr(response, "content", None) or "").strip()+ return (text, None) if text else ("", _empty_completion_note(provider, response))_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 f2573a66f..e957847a8 100644--- a/tests/unit/server/mcp/test_answer_synthesis_timeout.py+++ b/tests/unit/server/mcp/test_answer_synthesis_timeout.py@@ -323,14 +323,46 @@ async def test_a_non_timeout_failure_is_not_reported_as_a_budget_overrun():assert _TIMEOUT_ENV not in note-async def test_an_empty_completion_is_not_mistaken_for_a_failure():+async def test_an_empty_completion_is_reported_rather_than_shipped_blank():+ """A call can succeed and still produce nothing usable.++ Measured on ollama: a local reasoning model spent all 1024 tokens on hidden+ thinking and returned an empty content block. Nothing raised, so this used+ to reach the agent as an ordinary answer that happened to be blank.+ """+class _Empty(_SlowProvider):async def generate(self, **kwargs):- return SimpleNamespace(content=None)+ return SimpleNamespace(content=None, stop_reason=None)answer, note = await synthesize(_Empty(duration=0, budget=30.0), "sys", "user")assert answer == ""- assert note is None+ assert note is not None+ assert "empty completion" in note+ assert "slowprov" in note+++async def test_a_reasoning_model_that_burned_its_budget_is_named_as_such():+ """ "Empty" and "spent 1024 tokens thinking" need different remedies."""++ class _AllThinking(_SlowProvider):+ async def generate(self, **kwargs):+ return SimpleNamespace(content="", stop_reason="max_tokens")++ answer, note = await synthesize(_AllThinking(duration=0, budget=30.0), "sys", "user")+ assert answer == ""+ assert str(_SYNTHESIS_MAX_TOKENS) in note+ assert "reasoning" in note.lower()+++async def test_whitespace_only_completion_counts_as_empty():+ class _Blank(_SlowProvider):+ async def generate(self, **kwargs):+ return SimpleNamespace(content=" \n ", stop_reason="end_turn")++ answer, note = await synthesize(_Blank(duration=0, budget=30.0), "sys", "user")+ assert answer == ""+ assert note is not None# --- the degraded payload both failure modes share -------------------------