Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(answer): keep one best_guesses row per file (#3673)

Yunus Ozen04639289e235
diff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/payload.py b/packages/server/src/repowise/server/mcp_server/tool_answer/payload.py
index 1ebbe7c72..94448b797 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_answer/payload.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_answer/payload.py
@@ -134,19 +134,34 @@ def _build_best_guesses(hits: list[dict]) -> list[dict]:
Lets the agent pick ONE file to verify on an ambiguous retrieval. ``file``
goes through ``hit_file_path`` and unresolvable hits are skipped, since a
spotlight or module page's ``target_path`` is not an openable file.
+
+ One row per file: a symbol page resolves to the same file as its file
+ page, so a later hit for a file already listed is skipped and the slot
+ goes to the next distinct file. This is the same per-file dedupe as
+ ``serialize_candidate_file_facts``, with this list's own cap. The loop
+ deliberately walks past the first ``_GATED_RETURN_HITS`` hits, so a
+ skipped repeat is replaced rather than leaving the list short.
"""
- return [
- {
- "file": hit_file_path(h),
- "why_relevant": _candidate_justification(h),
- "score": round(h.get("score", 0.0), 3),
- # Absent rather than null: most rows carry no penalty.
- **({"domain_penalty": h["_domain_penalty"]} if h.get("_domain_penalty") else {}),
- **({"excerpt": h["excerpt"]} if h.get("excerpt") else {}),
- }
- for h in hits[:_GATED_RETURN_HITS]
- if hit_file_path(h)
- ]
+ guesses: list[dict] = []
+ seen: set[str] = set()
+ for h in hits:
+ path = hit_file_path(h)
+ if not path or path in seen:
+ continue
+ seen.add(path)
+ guesses.append(
+ {
+ "file": path,
+ "why_relevant": _candidate_justification(h),
+ "score": round(h.get("score", 0.0), 3),
+ # Absent rather than null: most rows carry no penalty.
+ **({"domain_penalty": h["_domain_penalty"]} if h.get("_domain_penalty") else {}),
+ **({"excerpt": h["excerpt"]} if h.get("excerpt") else {}),
+ }
+ )
+ if len(guesses) >= _GATED_RETURN_HITS:
+ break
+ return guesses
def _with_candidates(payload: dict, resolved_pool: list[dict]) -> dict:
diff --git a/tests/unit/server/mcp/test_payload_cuts.py b/tests/unit/server/mcp/test_payload_cuts.py
index 314b866dc..cdeeff04e 100644
--- a/tests/unit/server/mcp/test_payload_cuts.py
+++ b/tests/unit/server/mcp/test_payload_cuts.py
@@ -144,6 +144,38 @@ def test_null_domain_penalty_is_absent_not_null():
assert penalised[0]["domain_penalty"] == "ui question; cross-domain"
+def test_best_guesses_keep_one_row_per_file():
+ # A symbol page resolves to its file, so command.go would take two of the
+ # three slots and completions.go would never be offered (#3594).
+ hits = [
+ {"page_type": "file_page", "target_path": "command.go", "score": 5.774},
+ {"page_type": "file_page", "target_path": "args.go", "score": 4.042},
+ {"page_type": "symbol_spotlight", "target_path": "command.go::Find", "score": 3.0},
+ {"page_type": "file_page", "target_path": "completions.go", "score": 2.9},
+ ]
+ guesses = _build_best_guesses(hits)
+ assert [g["file"] for g in guesses] == ["command.go", "args.go", "completions.go"]
+ assert guesses[0]["score"] == 5.774
+
+
+def test_best_guesses_unchanged_for_distinct_files():
+ hits = [
+ {"page_type": "file_page", "target_path": "a.py", "score": 3.0},
+ {"page_type": "file_page", "target_path": "b.py", "score": 2.0},
+ {"page_type": "file_page", "target_path": "c.py", "score": 1.0},
+ {"page_type": "file_page", "target_path": "d.py", "score": 0.5},
+ ]
+ assert [g["file"] for g in _build_best_guesses(hits)] == ["a.py", "b.py", "c.py"]
+
+
+def test_best_guesses_still_skip_hits_without_a_file():
+ hits = [
+ {"page_type": "module_page", "target_path": "pkg", "score": 3.0},
+ {"page_type": "file_page", "target_path": "a.py", "score": 2.0},
+ ]
+ assert [g["file"] for g in _build_best_guesses(hits)] == ["a.py"]
+
+
# ---------------------------------------------------------------------------
# get_answer.candidates — 20 rows measured at up to 39.9% of the payload
# ---------------------------------------------------------------------------