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(mcp): keep pathless symbol rows out of get_answer's homonym union (#2391)

JANG BYUNGKUNb610ecfacc92
diff --git a/packages/server/src/repowise/server/mcp_server/tool_answer/symbols.py b/packages/server/src/repowise/server/mcp_server/tool_answer/symbols.py
index f91994db5..7469b8251 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_answer/symbols.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_answer/symbols.py
@@ -428,6 +428,11 @@ async def _anchor_symbol_hits(
)
by_name: dict[str, list] = {}
for row in res.scalars().all():
+ # A pathless row names nothing the reply can point at. Dropping it here
+ # keeps it out of every route below: the anchor, and both union branches
+ # that would otherwise read the live file at an empty path.
+ if not row.file_path:
+ continue
by_name.setdefault(row.name, []).append(row)
# Verify bounds against the live file before any body is sliced from a
diff --git a/tests/unit/server/mcp/test_answer_calibration.py b/tests/unit/server/mcp/test_answer_calibration.py
index 004c2ecbf..eea98dba3 100644
--- a/tests/unit/server/mcp/test_answer_calibration.py
+++ b/tests/unit/server/mcp/test_answer_calibration.py
@@ -742,6 +742,79 @@ async def test_anchor_qualified_miss_records_not_found():
assert all(not h.get("_symbol_anchored") for h in out)
+def _spy_repo_reads(monkeypatch) -> list[str]:
+ from repowise.server.mcp_server.tool_answer import symbols
+
+ reads: list[str] = []
+ real = symbols._read_repo_text
+
+ def _spy(repo_root, file_path):
+ reads.append(file_path)
+ return real(repo_root, file_path)
+
+ monkeypatch.setattr(symbols, "_read_repo_text", _spy)
+ return reads
+
+
+async def test_anchor_bare_homonym_union_drops_a_pathless_row(monkeypatch, tmp_path):
+ """A pathless def must not reach the bare-homonym union: it would be read
+ at an empty path and named in the reply as a file to open (#2346)."""
+ from repowise.server.mcp_server.tool_answer.symbols import _anchor_symbol_hits
+
+ reads = _spy_repo_reads(monkeypatch)
+ rows = [
+ _Sym("extract_all", "a/x.py", parent_name="A", qualified_name="A.extract_all"),
+ _Sym("extract_all", "b/y.py", parent_name="B", qualified_name="B.extract_all"),
+ _Sym("extract_all", "", parent_name="C", qualified_name="C.extract_all"),
+ ]
+ hits = [{"target_path": "c/z.py", "score": 2.0}]
+ _, homonyms = await _anchor_symbol_hits(
+ _FakeSession(rows), "r", {"extract_all"}, hits, repo_root=tmp_path
+ )
+ assert {d["file_path"] for d in homonyms["union"]["extract_all"]} == {"a/x.py", "b/y.py"}
+ assert "" not in reads
+
+
+async def test_anchor_narrowed_union_drops_a_pathless_row(monkeypatch, tmp_path):
+ """Same guard on the narrowed branch: the qualifier matches several defs,
+ one of them pathless."""
+ from repowise.server.mcp_server.tool_answer.symbols import _anchor_symbol_hits
+
+ reads = _spy_repo_reads(monkeypatch)
+ rows = [
+ _Sym("extract_all", "a/x.py", parent_name="Alpha", qualified_name="Alpha.extract_all"),
+ _Sym("extract_all", "a/w.py", parent_name="Alpha", qualified_name="Alpha.extract_all"),
+ _Sym("extract_all", "", parent_name="Alpha", qualified_name="Alpha.extract_all"),
+ _Sym("extract_all", "b/y.py", parent_name="Beta", qualified_name="Beta.extract_all"),
+ ]
+ hits = [{"target_path": "c/z.py", "score": 2.0}]
+ _, homonyms = await _anchor_symbol_hits(
+ _FakeSession(rows), "r", {"extract_all", "Alpha"}, hits, repo_root=tmp_path
+ )
+ assert {d["file_path"] for d in homonyms["union"]["extract_all"]} == {"a/x.py", "a/w.py"}
+ assert "" not in reads
+
+
+async def test_anchor_homonym_with_one_pathless_twin_anchors_the_real_def():
+ """Once the pathless row is dropped, the one def left is not a homonym: it
+ anchors like any single def instead of forming a union of one real body
+ and one empty path."""
+ from repowise.server.mcp_server.tool_answer.symbols import _anchor_symbol_hits
+
+ rows = [
+ _Sym("extract_all", "a/x.py", parent_name="A", qualified_name="A.extract_all"),
+ _Sym("extract_all", "", parent_name="B", qualified_name="B.extract_all"),
+ ]
+ hits = [{"target_path": "c/z.py", "score": 2.0}]
+ out, homonyms = await _anchor_symbol_hits(_FakeSession(rows), "r", {"extract_all"}, hits)
+ assert not homonyms["union"]
+ assert out[0]["target_path"] == "a/x.py"
+ assert out[0]["_symbol_anchored"] is True
+
+
@pytest.mark.asyncio
async def test_anchor_disambiguates_homonym_by_named_parent():
from repowise.server.mcp_server.tool_answer.symbols import _anchor_symbol_hits