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.

refactor(answer): dedupe the answer projection one collection at a time (#4064)

Raghav Chamadiya1b969832e873
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.py
index b78e4b1d0..5a0b2238f 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
@@ -135,67 +135,76 @@ def _trim_key_symbols(
row["key_symbols"] = trimmed
-def _deduplicate(payload: dict[str, Any]) -> None:
- """Keep each source fragment once, then keep paths only where they add navigation."""
+_RATIONALE_TEXT_KEYS = ("rationale", "comment", "quote", "source", "text")
+
+
+def _lines_of(row: Any) -> tuple:
+ return tuple(row.get("lines") or ())
+
+
+def _drop_served_text(row: Any, text_keys: tuple[str, ...], slabs: list[str]) -> Any:
+ """A copy of *row* without its *text_keys* text when *slabs* already serve it."""
+ if not isinstance(row, dict):
+ return row
+ row = dict(row)
+ if _contained(_text(row, *text_keys), slabs):
+ for key in text_keys:
+ row.pop(key, None)
+ return row
+
+
+def _dedupe_bodies(payload: dict[str, Any]) -> list[Any]:
bodies = _unique(
list(payload.get("symbol_bodies") or []),
- lambda row: (_path(row), row.get("name"), tuple(row.get("lines") or ())),
+ lambda row: (_path(row), row.get("name"), _lines_of(row)),
)
payload["symbol_bodies"] = bodies
- body_text = [_text(row, "source") for row in bodies]
- body_spans = _body_spans(bodies)
+ return bodies
+
+def _dedupe_quotes(payload: dict[str, Any], body_text: list[str]) -> list[Any]:
quotes = _unique(
[
row
for row in (payload.get("quotes") or [])
if not _contained(_text(row, "quote"), body_text)
],
- lambda row: (_path(row), tuple(row.get("lines") or ()), _text(row, "quote")),
+ lambda row: (_path(row), _lines_of(row), _text(row, "quote")),
)
payload["quotes"] = quotes
- quote_text = [_text(row, "quote") for row in quotes]
+ return quotes
+
+def _dedupe_rationale(payload: dict[str, Any], served: list[str]) -> list[Any]:
rationale = _unique(
[
row
for row in (payload.get("code_rationale") or [])
- if not _contained(
- _text(row, "rationale", "comment", "quote", "source", "text"),
- [*body_text, *quote_text],
- )
+ if not _contained(_text(row, *_RATIONALE_TEXT_KEYS), served)
],
- lambda row: (
- _path(row),
- tuple(row.get("lines") or ()),
- _text(row, "rationale", "comment", "quote", "source", "text"),
- ),
+ lambda row: (_path(row), _lines_of(row), _text(row, *_RATIONALE_TEXT_KEYS)),
)
payload["code_rationale"] = rationale
- rationale_text = [
- _text(row, "rationale", "comment", "quote", "source", "text") for row in rationale
- ]
+ return rationale
- guesses: list[dict[str, Any]] = []
- for original in _unique(
+
+def _dedupe_guesses(payload: dict[str, Any], served: list[str]) -> list[Any]:
+ unique = _unique(
list(payload.get("best_guesses") or []),
- lambda row: (
- _path(row),
- _text(row, "excerpt"),
- _text(row, "why_relevant", "reason"),
- ),
- ):
- row = dict(original) if isinstance(original, dict) else original
- if isinstance(row, dict) and _contained(
- _text(row, "excerpt"), [*body_text, *quote_text, *rationale_text]
- ):
- row.pop("excerpt", None)
- guesses.append(row)
+ lambda row: (_path(row), _text(row, "excerpt"), _text(row, "why_relevant", "reason")),
+ )
+ guesses = [_drop_served_text(row, ("excerpt",), served) for row in unique]
payload["best_guesses"] = guesses
- guess_text = [_text(row, "excerpt") for row in guesses]
+ return guesses
- retrieval: list[dict[str, Any]] = []
- for original in _unique(
+
+def _dedupe_retrieval(
+ payload: dict[str, Any],
+ served: list[str],
+ body_spans: list[tuple[str, int, int]],
+ symbol_served: list[str],
+) -> list[Any]:
+ unique = _unique(
list(payload.get("retrieval") or []),
lambda row: (
_path(row),
@@ -203,28 +212,23 @@ def _deduplicate(payload: dict[str, Any]) -> None:
_text(row, "excerpt", "snippet"),
_text(row, "summary"),
),
- ):
- row = dict(original) if isinstance(original, dict) else original
- if isinstance(row, dict) and _contained(
- _text(row, "excerpt", "snippet"),
- [*body_text, *quote_text, *rationale_text, *guess_text],
- ):
- row.pop("excerpt", None)
- row.pop("snippet", None)
+ )
+ retrieval: list[Any] = []
+ for original in unique:
+ row = _drop_served_text(original, ("excerpt", "snippet"), served)
if isinstance(row, dict):
- _trim_key_symbols(row, body_spans, [*body_text, *quote_text])
+ _trim_key_symbols(row, body_spans, symbol_served)
retrieval.append(row)
payload["retrieval"] = retrieval
+ return retrieval
+
+def _dedupe_navigation(payload: dict[str, Any], evidence: tuple[list[Any], ...]) -> None:
+ """Keep fallback targets and candidates only for files no evidence row names."""
citations = _unique(list(payload.get("citations") or []), lambda row: row)
payload["citations"] = citations
occupied = {path for path in map(_nav_path, citations) if path}
- occupied.update(
- path
- for rows in (bodies, quotes, rationale, guesses, retrieval)
- for path in map(_nav_path, rows)
- if path
- )
+ occupied.update(path for rows in evidence for path in map(_nav_path, rows) if path)
fallbacks = [
row
@@ -234,12 +238,30 @@ def _deduplicate(payload: dict[str, Any]) -> None:
payload["fallback_targets"] = fallbacks
ranked_paths = {path for path in map(_nav_path, fallbacks) if path}
- candidates = [
+ payload["candidates"] = [
row
for row in _unique(list(payload.get("candidates") or []), lambda row: _path(row))
if _nav_path(row) not in occupied and _nav_path(row) not in ranked_paths
]
- payload["candidates"] = candidates
+
+
+def _deduplicate(payload: dict[str, Any]) -> None:
+ """Keep each source fragment once, then keep paths only where they add navigation."""
+ bodies = _dedupe_bodies(payload)
+ body_text = [_text(row, "source") for row in bodies]
+ quotes = _dedupe_quotes(payload, body_text)
+ quote_text = [_text(row, "quote") for row in quotes]
+ rationale = _dedupe_rationale(payload, [*body_text, *quote_text])
+ rationale_text = [_text(row, *_RATIONALE_TEXT_KEYS) for row in rationale]
+ guesses = _dedupe_guesses(payload, [*body_text, *quote_text, *rationale_text])
+ guess_text = [_text(row, "excerpt") for row in guesses]
+ retrieval = _dedupe_retrieval(
+ payload,
+ [*body_text, *quote_text, *rationale_text, *guess_text],
+ _body_spans(bodies),
+ [*body_text, *quote_text],
+ )
+ _dedupe_navigation(payload, (bodies, quotes, rationale, guesses, retrieval))
def _same_file(a: str | None, b: str | None) -> bool: