@@ -841,6 +841,150 @@ def _fail_generate(*a, **k):
841841 assert saved ["1" ] == "Payment Flow" , "genuine label was not reused"
842842
843843
844+ def _auth_billing_graph (out : Path ) -> None :
845+ """Two saved communities plus one unclustered node (#2298)."""
846+ graph = {
847+ "directed" : False ,
848+ "multigraph" : False ,
849+ "nodes" : [
850+ {"id" : "a1" , "label" : "auth_hub" , "community" : 0 },
851+ {"id" : "a2" , "label" : "auth_a" , "community" : 0 },
852+ {"id" : "a3" , "label" : "auth_b" , "community" : 0 },
853+ {"id" : "a4" , "label" : "auth_c" , "community" : 0 },
854+ {"id" : "b1" , "label" : "bill_hub" , "community" : 1 },
855+ {"id" : "b2" , "label" : "bill_a" , "community" : 1 },
856+ {"id" : "b3" , "label" : "bill_b" , "community" : 1 },
857+ {"id" : "b4" , "label" : "bill_c" , "community" : 1 },
858+ {"id" : "a_new" , "label" : "auth_new" },
859+ ],
860+ "links" : [
861+ {"source" : "a1" , "target" : "a2" , "relation" : "calls" },
862+ {"source" : "a1" , "target" : "a3" , "relation" : "calls" },
863+ {"source" : "a1" , "target" : "a4" , "relation" : "calls" },
864+ {"source" : "b1" , "target" : "b2" , "relation" : "calls" },
865+ {"source" : "b1" , "target" : "b3" , "relation" : "calls" },
866+ {"source" : "b1" , "target" : "b4" , "relation" : "calls" },
867+ ],
868+ }
869+ (out / "graph.json" ).write_text (json .dumps (graph ), encoding = "utf-8" )
870+
871+
872+ def _patch_cluster_only_reuse (monkeypatch ):
873+ # `graphify.cluster` is a lazy function on the package until the submodule
874+ # is imported. cluster-only does `from graphify.cluster import cluster`.
875+ import graphify .cluster # noqa: F401
876+ import graphify .__main__ as cli
877+
878+ monkeypatch .setattr (cli , "_check_skill_version" , lambda _ : None )
879+ monkeypatch .setattr ("graphify.export.to_html" , lambda * a , ** k : None )
880+
881+ def _fail_generate (* a , ** k ):
882+ raise AssertionError ("generate_community_labels must not be called on the reuse path" )
883+
884+ monkeypatch .setattr ("graphify.llm.generate_community_labels" , _fail_generate )
885+ return cli
886+
887+
888+ def test_cluster_only_keeps_label_on_majority_membership_overlap (tmp_path , monkeypatch ):
889+ """#2298: one added node must not replace a saved name when the shared
890+ members are a strict majority of both the saved set and the current set.
891+ A one-node fragment of a large community must not keep that name."""
892+ from graphify .cluster import community_member_sigs
893+
894+ cli = _patch_cluster_only_reuse (monkeypatch )
895+
896+ root = tmp_path / "add-one"
897+ out = root / "graphify-out"
898+ out .mkdir (parents = True )
899+ _auth_billing_graph (out )
900+ labels_path = out / ".graphify_labels.json"
901+ labels_path .write_text (
902+ json .dumps ({"0" : "Auth Engine" , "1" : "Billing Desk" }), encoding = "utf-8"
903+ )
904+ old_members = {
905+ 0 : ["a1" , "a2" , "a3" , "a4" ],
906+ 1 : ["b1" , "b2" , "b3" , "b4" ],
907+ }
908+ (out / ".graphify_labels.json.sig" ).write_text (
909+ json .dumps ({str (k ): v for k , v in community_member_sigs (old_members ).items ()}),
910+ encoding = "utf-8" ,
911+ )
912+
913+ def _cluster_with_new (G , * args , ** kwargs ):
914+ return {
915+ 0 : ["a1" , "a2" , "a3" , "a4" , "a_new" ],
916+ 1 : ["b1" , "b2" , "b3" , "b4" ],
917+ }
918+
919+ monkeypatch .setattr ("graphify.cluster.cluster" , _cluster_with_new )
920+ monkeypatch .setattr (sys , "argv" , ["graphify" , "cluster-only" , str (root ), "--no-viz" ])
921+ cli .main ()
922+ labels = json .loads (labels_path .read_text (encoding = "utf-8" ))
923+ assert labels ["0" ] == "Auth Engine"
924+ assert labels ["1" ] == "Billing Desk"
925+
926+ fragment = tmp_path / "fragment"
927+ fragment_out = fragment / "graphify-out"
928+ fragment_out .mkdir (parents = True )
929+ member_ids = [f"n{ i :03d} " for i in range (200 )]
930+ fragment_graph = {
931+ "directed" : False ,
932+ "multigraph" : False ,
933+ "nodes" : [
934+ {"id" : nid , "label" : nid , "community" : 0 }
935+ for nid in member_ids
936+ ],
937+ "links" : [],
938+ }
939+ (fragment_out / "graph.json" ).write_text (json .dumps (fragment_graph ), encoding = "utf-8" )
940+ (fragment_out / ".graphify_labels.json" ).write_text (
941+ json .dumps ({"0" : "Big Service" }), encoding = "utf-8"
942+ )
943+ (fragment_out / ".graphify_labels.json.sig" ).write_text (
944+ json .dumps ({str (k ): v for k , v in community_member_sigs ({0 : member_ids }).items ()}),
945+ encoding = "utf-8" ,
946+ )
947+
948+ def _cluster_singletons (G , * args , ** kwargs ):
949+ return {i : [f"n{ i :03d} " ] for i in range (200 )}
950+
951+ monkeypatch .setattr ("graphify.cluster.cluster" , _cluster_singletons )
952+ monkeypatch .setattr (sys , "argv" , ["graphify" , "cluster-only" , str (fragment ), "--no-viz" ])
953+ cli .main ()
954+ fragment_labels = json .loads (
955+ (fragment_out / ".graphify_labels.json" ).read_text (encoding = "utf-8" )
956+ )
957+ assert "Big Service" not in fragment_labels .values ()
958+
959+
960+ def test_cluster_only_keeps_unchanged_label_when_count_changes (tmp_path , monkeypatch ):
961+ """#2298: with no signature file, a new singleton community must not hub-rename
962+ communities whose membership did not shrink."""
963+ cli = _patch_cluster_only_reuse (monkeypatch )
964+
965+ out = tmp_path / "graphify-out"
966+ out .mkdir ()
967+ _auth_billing_graph (out )
968+ labels_path = out / ".graphify_labels.json"
969+ labels_path .write_text (
970+ json .dumps ({"0" : "Auth Engine" , "1" : "Billing Desk" }), encoding = "utf-8"
971+ )
972+
973+ def _cluster_plus_singleton (G , * args , ** kwargs ):
974+ return {
975+ 0 : ["a1" , "a2" , "a3" , "a4" ],
976+ 1 : ["b1" , "b2" , "b3" , "b4" ],
977+ 2 : ["a_new" ],
978+ }
979+
980+ monkeypatch .setattr ("graphify.cluster.cluster" , _cluster_plus_singleton )
981+ monkeypatch .setattr (sys , "argv" , ["graphify" , "cluster-only" , str (tmp_path ), "--no-viz" ])
982+ cli .main ()
983+ labels = json .loads (labels_path .read_text (encoding = "utf-8" ))
984+ assert labels ["0" ] == "Auth Engine"
985+ assert labels ["1" ] == "Billing Desk"
986+
987+
844988# ── #2534 case 2: prompt key must not collide with the placeholder sentinel ──
845989
846990
0 commit comments