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): complete reference edit sets for Go, Java and Kotlin packages (#3642)

Swati Ahuja2cbfc33ee4e4
diff --git a/packages/server/src/repowise/server/mcp_server/_edit_sites.py b/packages/server/src/repowise/server/mcp_server/_edit_sites.py
index b1fdf973c..3700eed03 100644
--- a/packages/server/src/repowise/server/mcp_server/_edit_sites.py
+++ b/packages/server/src/repowise/server/mcp_server/_edit_sites.py
@@ -12,6 +12,7 @@ from __future__ import annotations
import asyncio
import json
+import os
import re
from collections import Counter
from itertools import islice
@@ -24,11 +25,12 @@ from sqlalchemy.ext.asyncio import AsyncSession
from repowise.core.fs_walk import PRUNED_DIRS, iter_glob
from repowise.core.ingestion.languages.registry import REGISTRY
from repowise.core.ingestion.symbol_identity import id_segment_name
-from repowise.core.persistence.models import GraphEdge, GraphNode
+from repowise.core.persistence.models import GraphEdge, GraphNode, WikiSymbol
from repowise.core.test_paths import is_test_related_path
from repowise.server.mcp_server._budget import OmissionCollector, cap_collection
from repowise.server.mcp_server._helpers import read_repo_file_text
from repowise.server.mcp_server._meta import _working_tree_dirty_paths, uncommitted_targets
+from repowise.server.mcp_server._verify import relocate_symbol
MAX_SITES = 200
MAX_TEXT_CHARS = 160
@@ -43,8 +45,16 @@ _MIN_CALL_CONFIDENCE = 0.7
_MEMBER_KINDS = frozenset({"method", "constructor", "property", "field"})
# Languages where using a symbol from another file takes an import naming that
# file or the symbol, so importers are every file that can use it. Elsewhere
-# (same-package or namespace visibility) sites are listed but never complete.
+# (same-package or namespace visibility) sites are listed but never complete,
+# except in the directory-package languages below.
_IMPORT_SCOPED_LANGUAGES = frozenset({"python", "typescript", "javascript"})
+# Languages whose same-package files are found and scanned too, so the set
+# can be complete: Go's package is its directory, a JVM package is every file
+# declaring it (any of these languages can share it).
+_DIRECTORY_PACKAGE_LANGUAGES = frozenset({"go", "java", "kotlin"})
+_JVM = ("java", "kotlin", "scala", "groovy")
+_PACKAGE_LINE = re.compile(r"^\s*package\s+([\w.]+)")
+_JVM_NAME = re.compile(r'^\s*@file\s*:\s*JvmName\s*\(\s*"(\w+)"')
_PYTHON_EXTENSIONS = REGISTRY.extensions_for(("python",))
# A statement that brings a name into a file, in the languages the graph
# resolves imports for. Continuation lines are found by bracket balance.
@@ -273,9 +283,16 @@ def _scan(
name: str,
definition: tuple[str, int, int],
call_lines: dict[str, set[int]],
-) -> tuple[list[dict[str, Any]], list[str], int, set[str], bool, dict[str, set[str]]]:
+ dirty: frozenset[str] = frozenset(),
+) -> tuple[
+ list[dict[str, Any]], list[str], int, set[str], bool, dict[str, set[str]], dict[str, str]
+]:
"""``(sites, unreadable, stale call lines, files renaming it, default-exported,
- local import aliases per Python file)``."""
+ local import aliases per Python file, package declared per file)``.
+
+ In a *dirty* file the graph's lines predate the edit, so a call is read
+ from the text (the name followed by ``(``) instead.
+ """
word = _word(name)
# Bound under any name by its importers, so no spelling can be scanned for.
default_export = re.compile(r"\bexport\s+default\b|\bas\s+default\b")
@@ -285,6 +302,7 @@ def _scan(
stale = 0
renamed: set[str] = set()
python_aliases: dict[str, set[str]] = {}
+ packages: dict[str, str] = {}
is_default = False
for rel in files:
lines = _read_lines(root, rel)
@@ -293,6 +311,8 @@ def _scan(
continue
imports = _import_lines(lines)
python = Path(rel).suffix in _PYTHON_EXTENSIONS
+ if not python and (declared := _declared_package(lines)):
+ packages[rel] = declared
aliases, local = _renames(lines, name, imports, python)
if aliases - local or _aliases_leave(rel, lines, local, imports):
renamed.add(rel)
@@ -304,15 +324,19 @@ def _scan(
if commonjs or any(default_export.search(t) and word.search(t) for t in lines):
is_default = True
spellings = [word] + [_word(a) for a in aliases]
- graph_calls = call_lines.get(rel, set())
+ live = rel in dirty
+ graph_calls = set() if live else call_lines.get(rel, set())
def_seen = rel != def_path
for n, text in enumerate(lines, 1):
in_graph = n in graph_calls
- if in_graph and not any(s.search(text) for s in spellings):
+ hits = [m for s in spellings for m in s.finditer(text)]
+ if in_graph and not hits:
stale += 1
continue
- if not in_graph and not any(s.search(text) for s in spellings):
+ if not hits:
continue
+ if live:
+ in_graph = any(_CALL_AFTER.match(text, m.end()) for m in hits)
if not def_seen and def_start <= n <= def_end:
kind, def_seen = "definition", True
elif n in imports:
@@ -322,7 +346,7 @@ def _scan(
else:
kind = "reference"
sites.append({"path": rel, "line": n, "kind": kind, "text": text.strip()[:MAX_TEXT_CHARS]})
- return sites, unreadable, stale, renamed, is_default, python_aliases
+ return sites, unreadable, stale, renamed, is_default, python_aliases, packages
def _aliases_leave(rel: str, lines: list[str], aliases: set[str], imports: set[int]) -> bool:
@@ -387,6 +411,78 @@ def _unseen_dirty_files(root: Path, local_path: str) -> list[str] | None:
return out
+def _declared_package(lines: list[str]) -> str | None:
+ declared = next(filter(None, map(_PACKAGE_LINE.match, lines)), None)
+ return declared.group(1) if declared else None
+
+
+def _package_scope(
+ root: Path, node: GraphNode, language: str
+) -> tuple[list[str], str | None, list[str]]:
+ """``(files, package, reasons)``: live files that may use *node* without an
+ import, its file's package, and why the edit set cannot be complete."""
+ if language in _IMPORT_SCOPED_LANGUAGES:
+ return [], None, []
+ if language not in _DIRECTORY_PACKAGE_LANGUAGES:
+ return [], None, [f"{language or 'unknown language'}: same-package use is not scanned"]
+ def_file = node.file_path
+ def_lines = _read_lines(root, def_file) or []
+ package = _declared_package(def_lines)
+ if not package:
+ return [], None, [f"{language}: no package declaration"]
+ parent = PurePosixPath(def_file).parent
+ if language == "go":
+ # The package is the directory: list it live, so a file the index
+ # skipped (generated, build-tagged, oversize) is scanned too.
+ exts = REGISTRY.extensions_for(("go",))
+ try:
+ names = sorted(e.name for e in os.scandir(root / parent) if e.is_file())
+ except OSError:
+ return [], package, ["go: package directory unreadable"]
+ return [(parent / n).as_posix() for n in names if PurePosixPath(n).suffix in exts], package, []
+ # A JVM package can span directories (src/main and src/test) or sit off its
+ # path, and another package can name the symbol fully qualified with no
+ # import, which the graph records no edge for: both are found by text.
+ # Ceilings: the needles are literal, so ``package a.b`` with odd spacing is
+ # missed, and gitignored sources (build/generated) are not searched, by design.
+ from repowise.server.mcp_server._line_hits import _files_holding
+
+ exts = REGISTRY.extensions_for(_JVM)
+ top = id_segment_name(node.node_id.split("::")[1]) if "::" in node.node_id else _bare_name(node)
+ needles = [f"package {package}", f"{package}.{top}"]
+ if PurePosixPath(def_file).suffix == ".kt":
+ # Java reaches a top-level Kotlin declaration through the file's facade
+ # class, ``UtilKt`` or its ``@file:JvmName``, never by the bare name.
+ facade = next(filter(None, map(_JVM_NAME.match, def_lines)), None)
+ stem = facade.group(1) if facade else f"{PurePosixPath(def_file).stem}Kt"
+ needles.append(f"{package}.{stem}")
+ files: set[str] = set()
+ reasons: list[str] = []
+ for needle in needles:
+ held = _files_holding(root, needle)
+ if held is None:
+ reasons.append(f"{language}: could not search for {needle}")
+ else:
+ files.update(f for f in held if PurePosixPath(f).suffix in exts)
+ return sorted(files), package, reasons
+
+
+def _live_definition(root: Path, node: GraphNode, name: str, language: str) -> tuple[int, int]:
+ """*node*'s bounds re-parsed from its edited file; ``(0, 0)`` when it is not found."""
+ text = read_repo_file_text(root, node.file_path)
+ row = WikiSymbol(
+ file_path=node.file_path,
+ symbol_id=node.node_id,
+ name=name,
+ language=language,
+ # Holds the parser's parent name; the id match is tried first anyway.
+ parent_name=node.parent_symbol_id,
+ start_line=node.start_line or 0,
+ end_line=node.end_line or 0,
+ )
+ return (relocate_symbol(row, text) if text is not None else None) or (0, 0)
+
+
async def reference_edit_set(
session: AsyncSession,
repo_id: str,
@@ -484,16 +580,33 @@ async def reference_edit_set(
if unseen is None:
reasons.append("working tree status unknown")
unseen = []
- unindexed = sorted(set(unseen) - files)
+ dirty = frozenset(unseen)
+ language = node.language or await session.scalar(
+ select(GraphNode.language).where(
+ GraphNode.repository_id == repo_id, GraphNode.node_id == def_file
+ )
+ )
+ language = (language or "").lower()
+ package_files, package, why = await asyncio.to_thread(_package_scope, root, node, language)
+ files.update(package_files)
+ reasons += why
+
+ unindexed = sorted(set(unseen) - files)
ordered = sorted(files)
if len(ordered) + len(unindexed) > MAX_FILES:
reasons.append(f"over {MAX_FILES} candidate files")
ordered = ordered[:MAX_FILES]
unindexed = unindexed[: max(0, MAX_FILES - len(ordered))]
- definition = (def_file, node.start_line or 0, node.end_line or node.start_line or 0)
- sites, unreadable, stale, renamed_in, is_default, python_aliases = await asyncio.to_thread(
- _scan, root, ordered + unindexed, name, definition, call_lines
+ if def_file in dirty:
+ # Indexed bounds predate the edit; re-parse as get_symbol does.
+ definition = (def_file, *await asyncio.to_thread(_live_definition, root, node, name, language))
+ else:
+ definition = (def_file, node.start_line or 0, node.end_line or node.start_line or 0)
+ sites, unreadable, stale, renamed_in, is_default, python_aliases, packages = (
+ await asyncio.to_thread(
+ _scan, root, ordered + unindexed, name, definition, call_lines, dirty
+ )
)
if python_aliases:
# Any module-level Python name is importable, so an alias another file
@@ -521,14 +634,16 @@ async def reference_edit_set(
reasons.append("low-confidence call edges")
if dynamic:
reasons.append("dynamic use of the symbol or its file")
- language = node.language or await session.scalar(
- select(GraphNode.language).where(
- GraphNode.repository_id == repo_id, GraphNode.node_id == def_file
- )
- )
- language = (language or "").lower()
- if language not in _IMPORT_SCOPED_LANGUAGES:
- reasons.append(f"{language or 'unknown language'}: same-package use is not scanned")
+ # A Go directory file declaring another package: the directory is not the
+ # package. Its external test package ``<name>_test`` imports it like any other.
+ scanned = set(ordered) - set(unreadable)
+ foreign = [
+ f
+ for f in package_files
+ if language == "go" and f in scanned and packages.get(f) not in (package, f"{package}_test")
+ ]
+ if foreign:
+ reasons.append(f"{language}: another package shares its directory: {', '.join(foreign[:3])}")
if renamed_in:
reasons.append(f"renamed on import or export: {', '.join(sorted(renamed_in)[:3])}")
if is_default:
diff --git a/tests/unit/server/mcp/test_context_references.py b/tests/unit/server/mcp/test_context_references.py
index 0ca76c1d3..57d96d289 100644
--- a/tests/unit/server/mcp/test_context_references.py
+++ b/tests/unit/server/mcp/test_context_references.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import json
+import subprocess
from datetime import UTC, datetime
import pytest
@@ -169,19 +170,20 @@ async def test_unindexed_edit_naming_the_symbol_is_listed_and_incomplete(
out = await reference_edit_set(session, repo_id, tmp_path, node)
- assert ("app/new.py", 1, "reference") in _sites(out)
+ # Its graph lines are unknown, so a call is read from the text.
+ assert ("app/new.py", 1, "call") in _sites(out)
assert out["complete"] is False
@pytest.mark.asyncio
-async def test_same_package_language_is_never_complete(session, repo_id, tmp_path):
- node = await _seed(session, repo_id, tmp_path, language="java")
+async def test_swift_module_visibility_is_never_complete(session, repo_id, tmp_path):
+ node = await _seed(session, repo_id, tmp_path, language="swift")
out = await reference_edit_set(session, repo_id, tmp_path, node)
assert (_USER, 9, "call") in _sites(out)
assert out["complete"] is False
- assert "java: same-package use is not scanned" in out["reasons"]
+ assert "swift: same-package use is not scanned" in out["reasons"]
@pytest.mark.asyncio
@@ -696,3 +698,280 @@ async def test_get_context_collapses_a_flood_into_recoverable_counts(setup_mcp,
def test_alias_member_access_on_an_export_line_is_incomplete(line: str) -> None:
lines = ['import { pick as choose } from "./d";', line]
assert _edit_sites._aliases_leave("u.ts", lines, {"choose"}, {1})
+
+
+async def _seed_package(
+ session, repo_id, tmp_path, files: dict[str, str], language: str, symbol, git: bool = True
+):
+ """Write *files* and index *symbol* ``(path, name, start, end)``; files are not indexed,
+ as only the live tree decides which share its package."""
+ if git:
+ # JVM package files are found by git grep.
+ subprocess.run(["git", "init", "-q", str(tmp_path)], check=True)
+ for rel, text in files.items():
+ (tmp_path / rel).parent.mkdir(parents=True, exist_ok=True)
+ (tmp_path / rel).write_text(text, encoding="utf-8")
+ path, name, start, end = symbol
+ node = GraphNode(
+ id="pkg_sym",
+ repository_id=repo_id,
+ node_id=f"{path}::{name}",
+ node_type="symbol",
+ name=name,
+ file_path=path,
+ kind="function" if language == "go" else "class",
+ language=language,
+ start_line=start,
+ end_line=end,
+ created_at=_NOW,
+ )
+ session.add(node)
+ await session.flush()
+ return node
+
+
+_MODEL = "src/main/java/org/sp/petclinic/model"
+_BASE_ENTITY = "package org.sp.petclinic.model;\n\npublic class BaseEntity {\n}\n"
+
+
+def _model(cls: str, parent: str, package: str = "org.sp.petclinic.model") -> str:
+ return f"package {package};\n\npublic class {cls} extends {parent} {{\n}}\n"
+
+
+async def test_java_same_package_subclasses_are_listed_and_complete(session, repo_id, tmp_path):
+ node = await _seed_package(
+ session,
+ repo_id,
+ tmp_path,
+ {
+ f"{_MODEL}/BaseEntity.java": _BASE_ENTITY,
+ f"{_MODEL}/NamedEntity.java": _model("NamedEntity", "BaseEntity"),
+ f"{_MODEL}/Person.java": _model("Person", "BaseEntity"),
+ # Same package under the test root: visible without an import.
+ "src/test/java/org/sp/petclinic/model/EntityTests.java": (
+ "package org.sp.petclinic.model;\n\nclass EntityTests {\n"
+ " BaseEntity e = new BaseEntity();\n}\n"
+ ),
+ # Another package naming it fully qualified, with no import.
+ "src/main/java/org/sp/petclinic/vet/Vet.java": (
+ "package org.sp.petclinic.vet;\n\n"
+ "class Vet extends org.sp.petclinic.model.BaseEntity {\n}\n"
+ ),
+ # A subpackage holding another BaseEntity is not a use.
+ f"{_MODEL}/sub/Other.java": _model("BaseEntity", "Object", "org.sp.petclinic.model.sub"),
+ },
+ "java",
+ (f"{_MODEL}/BaseEntity.java", "BaseEntity", 3, 4),
+ )
+
+ out = await reference_edit_set(session, repo_id, tmp_path, node)
+
+ sites = _sites(out)
+ assert (f"{_MODEL}/BaseEntity.java", 3, "definition") in sites
+ assert (f"{_MODEL}/NamedEntity.java", 3, "reference") in sites
+ assert (f"{_MODEL}/Person.java", 3, "reference") in sites
+ assert ("src/test/java/org/sp/petclinic/model/EntityTests.java", 4, "reference") in sites
+ assert ("src/main/java/org/sp/petclinic/vet/Vet.java", 3, "reference") in sites
+ assert out["complete"] is True, out.get("reasons")
+
+
+async def test_java_package_without_git_to_search_is_incomplete(session, repo_id, tmp_path):
+ node = await _seed_package(
+ session,
+ repo_id,
+ tmp_path,
+ {f"{_MODEL}/BaseEntity.java": _BASE_ENTITY},
+ "java",
+ (f"{_MODEL}/BaseEntity.java", "BaseEntity", 3, 4),
+ git=False,
+ )
+
+ out = await reference_edit_set(session, repo_id, tmp_path, node)
+
+ assert out["complete"] is False
+ assert "java: could not search for package org.sp.petclinic.model" in out["reasons"]
+
+
+_LEGACY = "package cobra\n\nfunc legacyArgs(args []string) error {\n\treturn nil\n}\n"
+
+
+async def test_go_package_is_its_live_directory(session, repo_id, tmp_path):
+ node = await _seed_package(
+ session,
+ repo_id,
+ tmp_path,
+ {
+ "args.go": _LEGACY,
+ "command.go": "package cobra\n\nfunc run(a []string) error {\n\treturn legacyArgs(a)\n}\n",
+ # A file the index skipped (generated, build-tagged) still shares the package.
+ "zz_generated.go": "package cobra\n\nvar f = legacyArgs\n",
+ # The external test package beside it imports it like any other.
+ "args_test.go": 'package cobra_test\n\nimport "testing"\n',
+ "doc/gen.go": "package doc\n\nfunc legacyArgs() {}\n",
+ },
+ "go",
+ ("args.go", "legacyArgs", 3, 5),
+ git=False,
+ )
+
+ out = await reference_edit_set(session, repo_id, tmp_path, node)
+
+ assert ("command.go", 4, "reference") in _sites(out)
+ assert ("zz_generated.go", 3, "reference") in _sites(out)
+ assert not any(p == "doc/gen.go" for p, _n, _k in _sites(out))
+ assert out["complete"] is True, out.get("reasons")
+
+
+async def test_go_directory_holding_another_package_is_incomplete(session, repo_id, tmp_path):
+ node = await _seed_package(
+ session,
+ repo_id,
+ tmp_path,
+ {"args.go": _LEGACY, "gen.go": "//go:build ignore\n\npackage main\n"},
+ "go",
+ ("args.go", "legacyArgs", 3, 5),
+ git=False,
+ )
+
+ out = await reference_edit_set(session, repo_id, tmp_path, node)
+
+ assert out["complete"] is False
+ assert "go: another package shares its directory: gen.go" in out["reasons"]
+
+
+async def test_kotlin_same_package_in_another_directory_is_listed(session, repo_id, tmp_path):
+ node = await _seed_package(
+ session,
+ repo_id,
+ tmp_path,
+ {
+ "src/com/ex/Util.kt": "package com.ex\n\nclass Helper {\n}\n",
+ "src/com/ex/Main.kt": "package com.ex\n\nval h = Helper()\n",
+ # Kotlin does not tie a package to its directory.
+ "app/misc/Extra.kt": "package com.ex\n\nval e = Helper()\n",
+ },
+ "kotlin",
+ ("src/com/ex/Util.kt", "Helper", 3, 4),
+ )
+
+ out = await reference_edit_set(session, repo_id, tmp_path, node)
+
+ assert ("src/com/ex/Main.kt", 3, "reference") in _sites(out)
+ assert ("app/misc/Extra.kt", 3, "reference") in _sites(out)
+ assert out["complete"] is True, out.get("reasons")
+
+
+_FIND = (
+ "package cobra\n\nfunc run(args []string) error {\n\treturn Find(args)\n}\n\n"
+ "func Find(args []string) error {\n\treturn nil\n}\n"
+)
+
+
+async def test_dirty_file_labels_a_shifted_call_as_a_call(session, repo_id, tmp_path, monkeypatch):
+ node = await _seed_package(
+ session, repo_id, tmp_path, {"find.go": _FIND}, "go", ("find.go", "Find", 7, 9), git=False
+ )
+ session.add(_edge(1, repo_id, "find.go::run", "find.go::Find", "calls", call_lines_json="[4]"))
+ await session.flush()
+ # Three lines inserted above both: the call lands on Find's indexed line.
+ edited = _FIND.replace("\n\nfunc run", "\n\n// a\n// b\n// c\nfunc run", 1)
+ (tmp_path / "find.go").write_text(edited, encoding="utf-8")
+ _dirty(monkeypatch, frozenset({"find.go"}))
+
+ out = await reference_edit_set(session, repo_id, tmp_path, node)
+
+ assert _sites(out) == [("find.go", 10, "definition"), ("find.go", 7, "call")]
+ assert not any(r.startswith("stale") for r in out.get("reasons", []))
+
+
+_OP = """package p;
+
+enum Op {
+ PLUS {
+ int apply(int a) { return a; }
+ };
+ abstract int apply(int a);
+}
+
+class Box {
+ void put(int x) {}
+ void put(String s) {}
+}
+"""
+
+
+ ("symbol_id", "parent", "line"),
+ [("p/Op.java::Box::put#1(String)", "Box", 13), ("p/Op.java::Op::PLUS::apply", "Op", 6)],
+)
+def test_live_definition_relocates_overloads_and_enum_constant_members(
+ tmp_path, symbol_id, parent, line
+):
+ (tmp_path / "p").mkdir()
+ # One line inserted at the top moves every symbol down by one.
+ (tmp_path / "p" / "Op.java").write_text("// edited\n" + _OP, encoding="utf-8")
+ node = GraphNode(
+ node_id=symbol_id, file_path="p/Op.java", start_line=1, end_line=1, parent_symbol_id=parent
+ )
+
+ name = _edit_sites._bare_name(node)
+
+ assert _edit_sites._live_definition(tmp_path, node, name, "java") == (line, line)
+
+
+ ("header", "facade"), [("", "UtilKt"), ('@file:JvmName("Tools")\n', "Tools")]
+)
+async def test_java_callers_of_a_kotlin_top_level_function_are_listed(
+ session, repo_id, tmp_path, header, facade
+):
+ node = await _seed_package(
+ session,
+ repo_id,
+ tmp_path,
+ {
+ "src/com/ex/Util.kt": f"{header}package com.ex\n\nfun foo() = 1\n",
+ "src/app/A.java": (
+ f"package app;\n\nimport com.ex.{facade};\n\n"
+ f"class A {{ int a = {facade}.foo(); }}\n"
+ ),
+ "src/app/B.java": (
+ f"package app;\n\nimport static com.ex.{facade}.foo;\n\nclass B {{ int b = foo(); }}\n"
+ ),
+ },
+ "kotlin",
+ ("src/com/ex/Util.kt", "foo", 3 + bool(header), 3 + bool(header)),
+ )
+
+ out = await reference_edit_set(session, repo_id, tmp_path, node)
+
+ sites = _sites(out)
+ assert ("src/app/A.java", 5, "reference") in sites
+ assert ("src/app/B.java", 3, "import") in sites
+ assert ("src/app/B.java", 5, "reference") in sites
+ assert out["complete"] is True, out.get("reasons")
+
+
+async def test_oversize_package_file_is_incomplete(session, repo_id, tmp_path):
+ node = await _seed_package(
+ session,
+ repo_id,
+ tmp_path,
+ {"args.go": _LEGACY, "big.go": "package cobra\n" + "// x\n" * 500_000},
+ "go",
+ ("args.go", "legacyArgs", 3, 5),
+ git=False,
+ )
+
+ out = await reference_edit_set(session, repo_id, tmp_path, node)
+
+ assert out["complete"] is False
+ assert "unreadable: big.go" in out["reasons"]