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(why): dedupe a card's commits by full sha, not a seven-character prefix (#3726)

Swati Ahujaace215e8142e
diff --git a/packages/server/src/repowise/server/mcp_server/tool_why/search.py b/packages/server/src/repowise/server/mcp_server/tool_why/search.py
index 7152a02e8..23bd9a632 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_why/search.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_why/search.py
@@ -5,6 +5,7 @@ from __future__ import annotations
import asyncio
import contextlib
import json
+import re
from collections.abc import Callable
from typing import Any
@@ -161,13 +162,13 @@ def _focus_archaeology(arch: dict[str, Any], query: str, recall: str) -> None:
none. A card serves its strongest commits instead, ties kept, when they
carry at least two of the question's terms and clear the margin.
"""
- # One entry per commit: the indexed lane and the live log repeat commits,
- # abbreviating the sha to different lengths (seven is git's own).
+ # One entry per commit: the indexed lane and the live log repeat commits.
+ key = _commit_keys(arch)
words: dict[str, frozenset[str]] = {}
for lane in _COMMIT_LANES:
for r in arch.get(lane) or []:
if isinstance(r, dict):
- words.setdefault(_short_sha(r), _message_words(r))
+ words.setdefault(key[id(r)], _message_words(r))
idf = term_idf(question_terms(query, prose=True), list(words.values()))
score = {sha: relevance(w, idf) for sha, w in words.items()}
eligible = {
@@ -186,7 +187,7 @@ def _focus_archaeology(arch: dict[str, Any], query: str, recall: str) -> None:
continue
kept, dropped = [], 0
for r in rows:
- sha = _short_sha(r) if isinstance(r, dict) else None
+ sha = key.get(id(r))
if sha in serve and sha not in served:
served.add(sha)
kept.append(r)
@@ -201,8 +202,43 @@ def _focus_archaeology(arch: dict[str, Any], query: str, recall: str) -> None:
arch[f"{lane}_omitted"] = recovery_note(dropped, recall)
-def _short_sha(row: dict) -> str:
- return str(row.get("sha") or "")[:7]
+def _full_sha(row: dict) -> str | None:
+ """The row's full commit sha: its ``commit`` field or its commit evidence ref."""
+ refs = row.get("evidence_refs") or []
+ candidates = [row.get("commit"), row.get("sha")]
+ candidates += [r.get("commit") for r in refs if isinstance(r, dict) and r.get("kind") == "commit"]
+ for c in candidates:
+ if isinstance(c, str) and _FULL_SHA_RE.fullmatch(c):
+ return c.lower()
+ return None
+
+
+_FULL_SHA_RE = re.compile(r"[0-9a-fA-F]{40}|[0-9a-fA-F]{64}")
+
+
+def _commit_keys(arch: dict[str, Any]) -> dict[int, str]:
+ """``{id(row): commit key}`` for every commit row of *arch*.
+
+ The indexed lanes abbreviate a sha to eight characters and the live log to
+ twelve, and either may carry the full sha beside it. A full sha is the key;
+ an abbreviated one resolves to the card's single full sha it prefixes, else
+ stays its own key. A row with no sha is its own commit.
+ """
+ rows = [r for lane in _COMMIT_LANES for r in arch.get(lane) or [] if isinstance(r, dict)]
+ full = {id(r): _full_sha(r) for r in rows}
+ known = {f for f in full.values() if f}
+ keys: dict[int, str] = {}
+ for r in rows:
+ if full[id(r)]:
+ keys[id(r)] = full[id(r)]
+ continue
+ short = str(r.get("sha") or "").lower()
+ if not short:
+ keys[id(r)] = f"row:{id(r)}"
+ continue
+ matches = [f for f in known if f.startswith(short)]
+ keys[id(r)] = matches[0] if len(matches) == 1 else short
+ return keys
def _message_words(row: dict) -> frozenset[str]:
diff --git a/tests/unit/server/mcp/test_why_relevance_floor.py b/tests/unit/server/mcp/test_why_relevance_floor.py
index 26bab8806..7916b0789 100644
--- a/tests/unit/server/mcp/test_why_relevance_floor.py
+++ b/tests/unit/server/mcp/test_why_relevance_floor.py
@@ -385,9 +385,10 @@ def test_a_commit_in_two_lanes_is_served_once():
from repowise.server.mcp_server.tool_why.search import _focus_archaeology
message = "Allow running persistent run hooks of all parents"
+ full = "4cafa37a1b2c" + "0" * 28
arch = {
"file_commits": [_commit("4cafa37a", message)],
- "git_log": [_commit("4cafa37a1b2c", message)],
+ "git_log": [{**_commit("4cafa37a1b2c", message), "commit": full}],
}
_focus_archaeology(arch, "why do persistent hooks of all parents run", "recall")
@@ -396,6 +397,44 @@ def test_a_commit_in_two_lanes_is_served_once():
assert "git_log" not in arch and "git_log_omitted" not in arch
+def test_commits_sharing_a_short_prefix_stay_distinct():
+ """Two commits whose shas share seven characters are two commits."""
+ from repowise.server.mcp_server.tool_why.search import _commit_keys
+
+ a, b = "abcdef1" + "1" * 33, "abcdef1" + "2" * 33
+ rows = [{**_commit(a[:12], "one"), "commit": a}, {**_commit(b[:12], "two"), "commit": b}]
+ keys = _commit_keys({"git_log": rows})
+
+ assert len(set(keys.values())) == 2
+
+
+def test_an_abbreviated_sha_resolves_to_the_full_one_it_prefixes():
+ from repowise.server.mcp_server.tool_why.search import _commit_keys
+
+ full = "abcdef12" + "3" * 32
+ indexed = _commit("abcdef12", "one")
+ live = {**_commit(full[:12], "one"), "commit": full}
+ keys = _commit_keys({"file_commits": [indexed], "git_log": [live]})
+
+ assert keys[id(indexed)] == keys[id(live)] == full
+
+
+def test_rows_without_a_sha_are_never_merged():
+ from repowise.server.mcp_server.tool_why.search import _focus_archaeology
+
+ arch = {
+ "git_log": [
+ {"message": "Allow running persistent run hooks of all parents"},
+ {"message": "Allow running persistent run hooks of all parents"},
+ {"message": "docs: update README"},
+ ]
+ }
+ _focus_archaeology(arch, "why do persistent hooks of all parents run", "recall")
+
+ # Two tied commits, not one row standing for both.
+ assert len(arch["git_log"]) == 2
+
+
def test_a_near_tie_between_commits_serves_none():
"""Two commits each carrying two question words at almost the same weight:
the question does not single either out."""