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): retain fallback_targets not named in emitted answer rows (#2995)
Rishabh Mishrac219425e7ad8
diff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/projection.py b/packages/server/src/repowise/server/mcp_server/tool_answer/projection.pyindex 88fabfe76..e6e0b99ff 100644--- a/packages/server/src/repowise/server/mcp_server/tool_answer/projection.py+++ b/packages/server/src/repowise/server/mcp_server/tool_answer/projection.py@@ -392,7 +392,42 @@ def _slim_best_guesses(payload: dict[str, Any], facts: dict[str, Any]) -> bool:return True-def _default_shape(payload: dict[str, Any], question: str) -> None:+def _prune_fallback_targets(+ payload: dict[str, Any], raw_fallbacks: list[Any] | None+) -> None:+ """Retain fallback targets not already represented in emitted evidence rows."""+ fallbacks = (+ raw_fallbacks+ if raw_fallbacks is not None+ else payload.get("fallback_targets") or []+ )+ if not fallbacks:+ payload.pop("fallback_targets", None)+ return++ emitted = {+ path+ for key in (+ "citations",+ "symbol_bodies",+ "code_rationale",+ "quotes",+ "best_guesses",+ "retrieval",+ )+ for row in payload.get(key) or []+ if (path := _nav_path(row))+ }+ targets = [row for row in fallbacks if _nav_path(row) not in emitted]+ if targets:+ payload["fallback_targets"] = targets+ else:+ payload.pop("fallback_targets", None)+++def _default_shape(+ payload: dict[str, Any], question: str, *, raw_fallbacks: list[Any] | None = None+) -> None:confidence = _shape_confidence(payload)why = question.lstrip().lower().startswith("why")if confidence == "high":@@ -435,8 +470,8 @@ def _default_shape(payload: dict[str, Any], question: str) -> None:else:_keep(payload, "retrieval", 3)payload.pop("candidates", None)- if payload.get("best_guesses") or payload.get("retrieval") or payload.get("symbol_bodies"):- payload.pop("fallback_targets", None)+ _prune_fallback_targets(payload, raw_fallbacks)+if not str(payload.get("answer") or "").strip():payload["answer"] = str(payload.get("note") or "No grounded answer was found.")payload.setdefault(@@ -497,7 +532,7 @@ def _serve_ranked_list(payload: dict[str, Any]) -> None:{"path": _path(row), "lines": row.get("lines"), "comment": comment}]# Every cited path is a guess or a rationale row, so citations would repeat them.- for key in ("citations", "note", "next_action_hint"):+ for key in ("citations", "note", "next_action_hint", "fallback_targets"):payload.pop(key, None)if isinstance(payload.get("_meta"), dict):payload["_meta"].pop("hint", None)@@ -666,6 +701,7 @@ def _project() -> tuple[dict[str, Any], bool, bool]:"""The projection, whether it slimmed ``best_guesses``, and whether it is the ranked list."""payload = copy.deepcopy(raw)+ raw_fallbacks = _unique(list(payload.get("fallback_targets") or []), lambda row: row)totals = {key: len(payload.get(key) or []) if isinstance(payload.get(key), list) else 0for key in _COLLECTIONS@@ -675,7 +711,7 @@ def _project(facts = payload.pop("_candidate_file_facts", None)slimmed = Falseif not expanded:- _default_shape(payload, question)+ _default_shape(payload, question, raw_fallbacks=raw_fallbacks)if _shape_confidence(payload) == "low":slimmed = _slim_best_guesses(payload, facts if isinstance(facts, dict) else {})bodies_cut = not expanded and _budget_keyless_bodies(payload, repo_root)diff --git a/tests/unit/server/mcp/test_answer_projection.py b/tests/unit/server/mcp/test_answer_projection.pyindex af3159508..850f8088c 100644--- a/tests/unit/server/mcp/test_answer_projection.py+++ b/tests/unit/server/mcp/test_answer_projection.py@@ -733,3 +733,60 @@ def test_evidence_projection_only_gains_candidate_files():assert after.pop("candidate_files") == _POOL[:5]assert after == before+++def test_degraded_low_confidence_retains_trimmed_fallback_targets():+ paths = [f"pkg/f{i}.py" for i in range(1, 6)]+ raw = {+ "answer": "...",+ "citations": ["pkg/f1.py"],+ "confidence": "low",+ "retrieval_quality": "high",+ "degraded": "synthesis-failed",+ "fallback_targets": paths,+ "retrieval": [{"path": p, "excerpt": "x" + p} for p in paths],+ "code_rationale": [+ {"path": "pkg/f1.py", "lines": [4, 5], "comment": "TCC rationale text"}+ ],+ "note": "model used its entire budget",+ "_meta": {"degraded": "synthesis-failed"},+ }+ out = project_answer_payload(raw, question="where is LCOM4 computed")++ assert [r["path"] for r in out["retrieval"]] == ["pkg/f1.py", "pkg/f2.py", "pkg/f3.py"]+ assert out["fallback_targets"] == ["pkg/f4.py", "pkg/f5.py"]+ assert out["fallback_targets_total"] == 5+ assert out["fallback_targets_emitted"] == 2+ assert out["retrieval_total"] == 5+ assert out["retrieval_emitted"] == 3+++def test_high_confidence_drops_fallback_targets_with_overflow():+ paths = [f"pkg/f{i}.py" for i in range(1, 6)]+ raw = {+ "answer": "Computed in pkg/f1.py.",+ "citations": ["pkg/f1.py"],+ "confidence": "high",+ "retrieval_quality": "high",+ "fallback_targets": paths,+ "retrieval": [{"path": p, "excerpt": "x" + p} for p in paths],+ }+ out = project_answer_payload(raw, question="where is LCOM4 computed")+ assert "fallback_targets" not in out+ assert "retrieval" not in out+++def test_degraded_empty_retrieval_retains_full_fallback_targets():+ paths = [f"pkg/f{i}.py" for i in range(1, 6)]+ raw = {+ "answer": "",+ "citations": [],+ "confidence": "low",+ "degraded": "synthesis-failed",+ "fallback_targets": paths,+ "retrieval": [],+ "note": "no local evidence matched",+ }+ out = project_answer_payload(raw, question="where is LCOM4 computed")+ assert out["fallback_targets"] == paths+ assert "retrieval" not in out