You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Building on a Python analysis-engine corpus (297 code files, 54 docs, 59 images → 8333 nodes, 19086 edges), Leiden community detection returned 274 communities. The top-10 communities (~4400 nodes total) all reported cohesion between 0.010 and 0.020 — i.e., most communities have edge density barely above random.
Why this matters
The skill's GRAPH_REPORT.md surfaces communities + god nodes as the primary navigation layer.
When cohesion is ~0.01 across the top communities, the resulting labels are too broad to be useful ("Core Services & Viewer" holds 705 heterogeneous nodes).
Downstream: community-labelled wiki articles become dumping grounds, not focused references.
Root cause (hypothesis)
Code graphs are leaf-heavy: docstring summaries, helper functions, and per-file __init__ nodes all attach to a small number of hubs with degree=1. Leiden's modularity score collapses because these leaves don't contribute to within-community density.
Proposed fix
Prune degree-1 leaves before clustering, attach them back post-cluster to their single neighbour's community.
Tune resolution parameter — default Leiden resolution appears to be 1.0; for code graphs of 5K+ nodes, bumping to 1.5–2.0 yields fewer, denser communities in practice.
Post-cluster merge step: merge any community smaller than N nodes (e.g., 10) into its most-connected neighbour.
@safishamsi I'd like to take this one — here's the plan, following @harnill's points 1 and 3:
1. Leaf-pruning (point 1). Exclude degree-1 leaves whose sole neighbour is a hub before partitioning — right alongside the existing hub-exclusion in cluster() — and reattach each to its neighbour's community afterwards (a leaf has exactly one neighbour, so it's unambiguous). I gate on the neighbour being a hub (degree >= 3) so short chains / small peripheral structures aren't disturbed on small graphs.
2. Small-community merge (point 3). After the existing split passes, merge sub-N communities (default N = 10) into the most-connected substantial neighbour (itself >= N), deterministic tie-break; disconnected fragments left as-is.
Resolution (point 2): the --resolution flag already exists (default 1.0), so I'll leave the default alone rather than risk a global regression — happy to add an opt-in adaptive bump for >5k-node graphs behind the flag as a separate change if you want it.
One honest finding from prototyping on a synthetic leaf-heavy graph (560 nodes, 501 leaves): leaf-pruning is the real cohesion win (mean community cohesion 0.04 → 0.28), while the merge is a count-vs-cohesion trade-off — it cuts the tiny-fragment tail (the "274 communities" half) but slightly dilutes the largest communities it folds fragments into. So I've kept _MIN_MERGE_SIZE a module constant and the two stages independent, so you can go cohesion-maximal (pruning only) or fewer-communities (with merge) as you prefer.
Both stages preserve the two invariants the tests rely on: all nodes covered, and deterministic community IDs. I have it implemented with tests passing on both the Leiden and Louvain-fallback paths, and I'll include before/after numbers on both in the PR.
Does the approach look right to you? And @harnill — a sanitized graph.json from your repro would be great to validate against real numbers before I finalize.
Activity
arcticloud commentedon Jul 9, 2026
@safishamsi I'd like to take this one — here's the plan, following @harnill's points 1 and 3:
1. Leaf-pruning (point 1). Exclude degree-1 leaves whose sole neighbour is a hub before partitioning — right alongside the existing hub-exclusion in
cluster()— and reattach each to its neighbour's community afterwards (a leaf has exactly one neighbour, so it's unambiguous). I gate on the neighbour being a hub (degree >= 3) so short chains / small peripheral structures aren't disturbed on small graphs.2. Small-community merge (point 3). After the existing split passes, merge sub-
Ncommunities (defaultN = 10) into the most-connected substantial neighbour (itself>= N), deterministic tie-break; disconnected fragments left as-is.Resolution (point 2): the
--resolutionflag already exists (default 1.0), so I'll leave the default alone rather than risk a global regression — happy to add an opt-in adaptive bump for >5k-node graphs behind the flag as a separate change if you want it.One honest finding from prototyping on a synthetic leaf-heavy graph (560 nodes, 501 leaves): leaf-pruning is the real cohesion win (mean community cohesion 0.04 → 0.28), while the merge is a count-vs-cohesion trade-off — it cuts the tiny-fragment tail (the "274 communities" half) but slightly dilutes the largest communities it folds fragments into. So I've kept
_MIN_MERGE_SIZEa module constant and the two stages independent, so you can go cohesion-maximal (pruning only) or fewer-communities (with merge) as you prefer.Both stages preserve the two invariants the tests rely on: all nodes covered, and deterministic community IDs. I have it implemented with tests passing on both the Leiden and Louvain-fallback paths, and I'll include before/after numbers on both in the PR.
Does the approach look right to you? And @harnill — a sanitized
graph.jsonfrom your repro would be great to validate against real numbers before I finalize.