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(graph): keep self edges for recursive calls (#4816)

Swati Ahuja6db053f549e0
diff --git a/packages/core/src/repowise/core/ingestion/call_overload_selection.py b/packages/core/src/repowise/core/ingestion/call_overload_selection.py
index d5d00afec..c8779aeae 100644
--- a/packages/core/src/repowise/core/ingestion/call_overload_selection.py
+++ b/packages/core/src/repowise/core/ingestion/call_overload_selection.py
@@ -404,6 +404,15 @@ class OverloadSelectionMixin:
for member, (fewest, most) in self._overload_members[defined]
)
+ def _build_variants(self, resolved: ResolvedCall) -> tuple[str, ...]:
+ """The callees an edge fans out to: every build variant of its target.
+
+ A variant calling its own name is recursion inside one build, where no
+ sibling compiles, so it keeps its self edge and reaches no sibling.
+ """
+ variants = self._variant_siblings.get(resolved.callee_id, (resolved.callee_id,))
+ return (resolved.caller_id,) if resolved.caller_id in variants else variants
+
def _narrow_overload(self, resolved: ResolvedCall, call: CallSite) -> ResolvedCall | None:
"""Move an edge on an overload set onto the member its arguments call.
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.py
index 73a207a5c..e9a9c67d2 100644
--- a/packages/core/src/repowise/core/ingestion/call_resolver.py
+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py
@@ -872,12 +872,10 @@ class CallResolver(
if call.spawned:
resolved = replace(resolved, spawned=True)
- for variant in self._variant_siblings.get(
- resolved.callee_id, (resolved.callee_id,)
- ):
- if variant != resolved.caller_id:
- results.append(replace(resolved, callee_id=variant))
-
+ results.extend(
+ replace(resolved, callee_id=variant)
+ for variant in self._build_variants(resolved)
+ )
return results
diff --git a/tests/unit/ingestion/test_recursive_self_edges.py b/tests/unit/ingestion/test_recursive_self_edges.py
new file mode 100644
index 000000000..8906d754d
--- /dev/null
+++ b/tests/unit/ingestion/test_recursive_self_edges.py
@@ -0,0 +1,65 @@
+"""A qualified recursive call keeps its self edge in every language.
+
+The cfg build-variant fan-out once dropped any edge whose callee was its own
+caller, which removed ordinary recursion too (``ClassName.f()`` inside ``f``).
+"""
+
+from __future__ import annotations
+
+from datetime import datetime
+from pathlib import Path
+
+import pytest
+
+from repowise.core.ingestion.call_resolver import CallResolver
+from repowise.core.ingestion.models import FileInfo
+from repowise.core.ingestion.parser import ASTParser
+
+_PARSER = ASTParser()
+
+CASES = [
+ (
+ "python",
+ "m.py",
+ "class C:\n @staticmethod\n def walk(n):\n return C.walk(n - 1) if n else 0\n",
+ "m.py::C::walk",
+ ),
+ (
+ "java",
+ "A.java",
+ "class A {\n static int f(int n) { return n == 0 ? 1 : A.f(n - 1); }\n}\n",
+ "A.java::A::f",
+ ),
+ (
+ "rust",
+ "lib.rs",
+ "pub struct T;\nimpl T {\n pub fn e(n: u32) -> u32 { T::e(n) }\n}\n",
+ "lib.rs::T::e",
+ ),
+]
+
+
+ ("language", "rel", "text", "symbol"), CASES, ids=[case[0] for case in CASES]
+)
+def test_a_recursive_call_is_a_self_edge(
+ tmp_path: Path, language: str, rel: str, text: str, symbol: str
+) -> None:
+ path = tmp_path / rel
+ path.write_text(text, encoding="utf-8")
+ info = FileInfo(
+ path=rel,
+ abs_path=str(path),
+ language=language,
+ size_bytes=len(text),
+ git_hash="",
+ last_modified=datetime.now(),
+ is_test=False,
+ is_config=False,
+ is_api_contract=False,
+ is_entry_point=False,
+ )
+ parsed = _PARSER.parse_file(info, text.encode("utf-8"))
+ resolver = CallResolver({rel: parsed}, {rel: set()}, repo_path=str(tmp_path))
+ edges = {(rc.caller_id, rc.callee_id) for rc in resolver.resolve_file(rel, parsed.calls)}
+ assert edges == {(symbol, symbol)}
diff --git a/tests/unit/ingestion/test_rust_cfg_variants.py b/tests/unit/ingestion/test_rust_cfg_variants.py
index a24422350..ab40b53df 100644
--- a/tests/unit/ingestion/test_rust_cfg_variants.py
+++ b/tests/unit/ingestion/test_rust_cfg_variants.py
@@ -128,13 +128,18 @@ class TestControls:
assert "lib.rs::k#cfg(unix)" in ids
assert "lib.rs::k#cfg(not(unix))" in ids
- def test_a_variant_calling_itself_draws_no_edge(self, tmp_path: Path) -> None:
+ def test_a_variant_calling_itself_recurses_without_reaching_its_sibling(
+ self, tmp_path: Path
+ ) -> None:
text = (
- "#[cfg(unix)]\nfn r() -> u32 { r() }\n"
- "#[cfg(not(unix))]\nfn r() -> u32 { 2 }\n"
+ "pub struct T;\nimpl T {\n"
+ " #[cfg(unix)]\n pub fn r(n: u32) -> u32 { T::r(n) }\n"
+ " #[cfg(not(unix))]\n pub fn r(n: u32) -> u32 { n }\n"
+ "}\n"
)
edges = _edges(tmp_path, {"lib.rs": text})
- assert not any(caller == callee for caller, callee in edges)
+ assert edges == {("lib.rs::T::r#cfg(unix)", "lib.rs::T::r#cfg(unix)")}
+
def test_rust_cfg_normalization_preserves_string_whitespace() -> None:
spaced = _normalize_cfg_whitespace('cfg(feature = "foo bar")')