Skip to content

[Bug] get_repository_stats overcounts functions when classes exist (CONTAINS* traversal duplicates) #759

@qwertist

Description

@qwertist

Description

codegraph_get_repository_stats reports an inflated function count for repositories that contain classes with methods. The count includes duplicates because class methods are reachable via two CONTAINS paths.

Steps to Reproduce

pip install codegraphcontext==0.3.1
cgc index /path/to/go/project  # Go project with 31 structs and their methods

Then via MCP:

# Per-repo stats reports inflated count:
codegraph_get_repository_stats(repo_path="/path/to/project")
# → functions: 325

# But actual unique functions in the graph:
codegraph_execute_cypher_query("MATCH (f:Function) WHERE f.path STARTS WITH '/path/to/project' RETURN count(f)")
# → 226

# The root cause — CONTAINS* traversal double-counts:
codegraph_execute_cypher_query("MATCH (r:Repository {path: '/path/to/project'})-[:CONTAINS*]->(f:Function) RETURN count(f) AS with_dupes, count(DISTINCT f) AS no_dupes")
# → with_dupes: 325, no_dupes: 226

Root Cause

Class methods are reachable via two paths in the graph:

  1. Repository → File → Function (direct)
  2. Repository → File → Class → Function (via class)

The stats query uses count(f) on CONTAINS* traversal, which counts each method twice. The difference (325 - 226 = 99) exactly matches the number of class methods:

MATCH (c:Class)-[:CONTAINS]->(f:Function) WHERE c.path STARTS WITH '/path/to/project'
RETURN count(f)
--99

Fix

Change count(f) to count(DISTINCT f) in the repository stats query.

Environment

  • codegraphcontext: 0.3.1
  • Database backend: KùzuDB (default)
  • Python: 3.12.3
  • OS: Linux (Ubuntu)
  • Test repo: Go project with 21 files, 31 structs, 226 unique functions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions