-
-
Notifications
You must be signed in to change notification settings - Fork 11k
fix(analyze): god_nodes honours exclude_hubs_percentile (#3205) #3239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abhay-codes07
wants to merge
1
commit into
Graphify-Labs:v8
Choose a base branch
from
abhay-codes07:fix/god-nodes-exclude-hubs
base: v8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| """god_nodes() honours exclude_hubs_percentile (#3205). | ||
|
|
||
| The percentile only ever reached cluster()'s community-resolution step; the | ||
| god-node ranking had no exclusion parameter at all, so running it at every | ||
| percentile value returned identical output and utility hubs stayed at the | ||
| top regardless of the setting. The same threshold computation cluster() | ||
| uses now applies to the ranking, and the CLI/MCP surfaces expose it. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import inspect | ||
| import json | ||
|
|
||
| import networkx as nx | ||
| import pytest | ||
|
|
||
| from graphify.analyze import god_nodes | ||
|
|
||
|
|
||
| def _graph(): | ||
| """One mega-hub (degree 40), two mid symbols, a tail of leaves. | ||
|
|
||
| The hub label must not be in _BUILTIN_NOISE_LABELS - the ranking already | ||
| filters those - so the test isolates the percentile mechanism.""" | ||
| G = nx.Graph() | ||
| G.add_node("hub", label="Registry", file_type="code", source_file="u.py", source_location="L1") | ||
| for i in range(40): | ||
| G.add_node(f"leaf{i}", label=f"leaf{i}", file_type="code", | ||
| source_file=f"l{i}.py", source_location="L1") | ||
| G.add_edge("hub", f"leaf{i}", relation="calls") | ||
| for name, deg in (("core", 6), ("svc", 4)): | ||
| G.add_node(name, label=name, file_type="code", source_file=f"{name}.py", | ||
| source_location="L1") | ||
| for i in range(deg): | ||
| G.add_edge(name, f"leaf{i}", relation="calls") | ||
| return G | ||
|
|
||
|
|
||
| def test_without_the_parameter_the_hub_still_ranks_first(): | ||
| assert god_nodes(_graph(), top_n=3)[0]["label"] == "Registry" | ||
|
|
||
|
|
||
| def test_the_percentile_suppresses_the_hub(): | ||
| gods = god_nodes(_graph(), top_n=10, exclude_hubs_percentile=90) | ||
| labels = [g["label"] for g in gods] | ||
| assert "Registry" not in labels, labels | ||
| assert gods, "suppressing the hub must not empty the ranking" | ||
| assert max(g["degree"] for g in gods) < 40 | ||
|
|
||
|
|
||
| def test_the_threshold_matches_clusters_computation(): | ||
| """The ranking must exclude exactly what cluster()'s formula excludes: | ||
| degrees sorted ascending, idx = max(0, int(n * pct / 100) - 1), | ||
| everything with degree > degrees[idx] is a hub.""" | ||
| G = _graph() | ||
| pct = 90 | ||
| degrees = sorted(d for _, d in G.degree()) | ||
| idx = max(0, int(len(degrees) * pct / 100) - 1) | ||
| threshold = degrees[idx] | ||
| expected_hubs = {n for n, d in G.degree() if d > threshold} | ||
| assert "hub" in expected_hubs | ||
| gods = {g["id"] for g in god_nodes(G, top_n=100, exclude_hubs_percentile=pct)} | ||
| assert gods.isdisjoint(expected_hubs) | ||
| assert gods, "non-hub symbols must survive" | ||
|
|
||
|
|
||
| def test_percentile_100_excludes_nothing(): | ||
| gods = god_nodes(_graph(), top_n=3, exclude_hubs_percentile=100) | ||
| assert gods[0]["label"] == "Registry" | ||
|
|
||
|
|
||
| def test_the_analyzer_signature_stays_backward_compatible(): | ||
| sig = inspect.signature(god_nodes) | ||
| assert sig.parameters["exclude_hubs_percentile"].default is None | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # CLI surface | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_the_cli_command_takes_the_flag(tmp_path, monkeypatch, capsys): | ||
| import graphify.__main__ as mainmod | ||
| from graphify.export import to_json | ||
| G = _graph() | ||
| gp = tmp_path / "graph.json" | ||
| to_json(G, {0: list(G.nodes)}, str(gp)) | ||
| monkeypatch.setattr(mainmod, "_check_skill_version", lambda *_a, **_k: None) | ||
|
|
||
| def run(*extra): | ||
| monkeypatch.setattr(mainmod.sys, "argv", | ||
| ["graphify", "god-nodes", "--graph", str(gp), "--json", *extra]) | ||
| try: | ||
| mainmod.main() | ||
| except SystemExit as exc: | ||
| assert exc.code in (None, 0) | ||
| return json.loads(capsys.readouterr().out) | ||
|
|
||
| assert run()[0]["label"] == "Registry" | ||
| filtered = run("--exclude-hubs", "90") | ||
| assert filtered and all(g["label"] != "Registry" for g in filtered) | ||
| filtered2 = run("--exclude-hubs=90") | ||
| assert filtered2 and all(g["label"] != "Registry" for g in filtered2) | ||
|
|
||
|
|
||
| def test_a_bad_flag_value_is_a_usage_error(tmp_path, monkeypatch, capsys): | ||
| import graphify.__main__ as mainmod | ||
| monkeypatch.setattr(mainmod, "_check_skill_version", lambda *_a, **_k: None) | ||
| monkeypatch.setattr(mainmod.sys, "argv", | ||
| ["graphify", "god-nodes", "--exclude-hubs", "lots"]) | ||
| with pytest.raises(SystemExit) as info: | ||
| mainmod.main() | ||
| assert info.value.code == 1 | ||
| assert "--exclude-hubs" in capsys.readouterr().err |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
god_nodes()32 callers depend on it (afferent coupling).
Grounded coupling-delta finding (deterministic), not an LLM guess.