
    Ki                        d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZmZ 	 	 d	 	 	 	 	 ddZ	 	 d	 	 	 	 	 dd	Z	 d	 	 	 dd
Z	 	 d	 	 	 	 	 ddZ	 d	 	 	 ddZy)z.MCP tool wrappers for graph analysis features.    )annotations)Any   )find_bridge_nodesfind_hub_nodesfind_knowledge_gapsfind_surprising_connectionsgenerate_suggested_questions   )
_get_store_validate_repo_rootc                |    t        |       }t        t        |            }t        ||      }|t	        |      g ddS )u[  Find the most connected nodes in the codebase graph.

    Hub nodes have the highest total degree (in + out edges).
    These are architectural hotspots — changes to them have
    disproportionate blast radius.

    Args:
        repo_root: Repository root (auto-detected if empty).
        top_n: Number of top hubs to return (default 10).
    top_n)u1   get_impact_radius — check blast radius of a hubu/   query_graph callers_of — see what calls a hubu3   get_bridge_nodes — find architectural chokepoints)	hub_nodescountnext_tool_suggestions)r   r   strr   len)	repo_rootr   rootstorehubss        r/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/code_review_graph/tools/analysis_tools.pyget_hub_nodes_funcr      sA     y)Ds4y!E%u-DT"
     c                |    t        |       }t        t        |            }t        ||      }|t	        |      g ddS )aI  Find architectural chokepoints via betweenness centrality.

    Bridge nodes sit on the shortest paths between many node
    pairs. If they break, multiple code regions lose
    connectivity.

    Args:
        repo_root: Repository root (auto-detected if empty).
        top_n: Number of top bridges to return (default 10).
    r   )u+   get_hub_nodes — find most connected nodesu(   get_impact_radius — check blast radiusu.   detect_changes — see if bridges are affected)bridge_nodesr   r   )r   r   r   r   r   )r   r   r   r   bridgess        r   get_bridge_nodes_funcr    -   sA     y)Ds4y!EU3GW"
 r   c           	        t        |       }t        t        |            }t        |      }t	        d |j                         D              }||t        |d         t        |d         t        |d         t        |d         dg ddS )	a  Identify structural weaknesses in the codebase.

    Finds: isolated nodes (disconnected), thin communities
    (< 3 members), untested hotspots (high-degree, no tests),
    and single-file communities.

    Args:
        repo_root: Repository root (auto-detected if empty).
    c              3  2   K   | ]  }t        |        y w)N)r   ).0vs     r   	<genexpr>z*get_knowledge_gaps_func.<locals>.<genexpr>X   s     .1A.s   isolated_nodesthin_communitiesuntested_hotspotssingle_file_communities)r&   r'   r(   r)   )u*   refactor dead_code — find unused symbolsu(   get_hub_nodes — find high-impact nodesu*   get_suggested_questions — review prompts)gaps
total_gapssummaryr   )r   r   r   r   sumvaluesr   )r   r   r   r*   totals        r   get_knowledge_gaps_funcr0   I   s     y)Ds4y!Eu%D...E!$'7"89 #D);$< =!$T*=%>!?'*./(	
"
 r   c                |    t        |       }t        t        |            }t        ||      }|t	        |      g ddS )a7  Find unexpected architectural coupling in the codebase.

    Scores edges by surprise factors: cross-community,
    cross-language, peripheral-to-hub, cross-test-boundary.

    Args:
        repo_root: Repository root (auto-detected if empty).
        top_n: Number of top surprises to return (default 15).
    r   )u1   get_architecture_overview — community structureu-   query_graph callers_of — trace the couplingu%   get_bridge_nodes — find chokepoints)surprising_connectionsr   r   )r   r   r   r	   r   )r   r   r   r   	surprisess        r   get_surprising_connections_funcr4   l   sA     y)Ds4y!E+E?I"+Y"
 r   c           
     >   t        |       }t        t        |            }t        |      }g g g d}|D ]&  }|j	                  |d   g       j                  |       ( |t        |      |j                         D ci c]  \  }}|t        |       c}}g ddS c c}}w )a  Auto-generate review questions from graph analysis.

    Produces questions about: bridge nodes, untested hubs,
    surprising connections, thin communities, and untested
    hotspots.

    Args:
        repo_root: Repository root (auto-detected if empty).
    )highmediumlowpriority)u,   get_knowledge_gaps — structural weaknessesu%   detect_changes — risk-scored reviewu+   get_architecture_overview — community map)	questionsr   by_priorityr   )r   r   r   r
   getappendr   items)r   r   r   r:   r;   qkr$   s           r   get_suggested_questions_funcrA      s     y)Ds4y!E,U3IB7K 5*r*11!45 Y"-"3"3"5
!QAs1vI
"
 
s   :B
N) 
   )r   r   r   intreturndict[str, Any])rB   )r   r   rE   rF   )rB      )__doc__
__future__r   typingr   analysisr   r   r   r	   r
   _commonr   r   r   r    r0   r4   rA    r   r   <module>rN      s    4 "   5  :  :    H  8 r   