analysis
code_context_agent.tools.graph.analysis ¶
Graph analysis algorithms for code understanding.
This module provides the CodeAnalyzer class with methods for: - Centrality analysis (hotspots, foundations, entry points) - Clustering (community detection, pattern-based grouping) - Proximity/similarity analysis
CodeAnalyzer ¶
Analyzer for code graphs using NetworkX algorithms.
Provides methods for finding important code (centrality), detecting logical modules (clustering), and analyzing relationships between code elements.
Initialize the analyzer with a code graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph | CodeGraph | The CodeGraph to analyze | required |
Source code in src/code_context_agent/tools/graph/analysis.py
find_hotspots ¶
Find code hotspots using betweenness centrality.
Hotspots are code elements that lie on many shortest paths between other elements - they are often bottlenecks or central integration points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
top_k | int | Number of top hotspots to return | 10 |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | List of dictionaries with node info and betweenness score |
Source code in src/code_context_agent/tools/graph/analysis.py
find_foundations ¶
Find foundational code using PageRank.
Foundations are code elements that are heavily depended upon by other important code - the core infrastructure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
top_k | int | Number of top foundations to return | 10 |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | List of dictionaries with node info and PageRank score |
Source code in src/code_context_agent/tools/graph/analysis.py
find_trusted_foundations ¶
Find foundational code using TrustRank (noise-resistant PageRank).
TrustRank propagates trust from seed nodes, making it more resistant to noise than standard PageRank. If no seed nodes provided, uses entry points as seeds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed_nodes | list[str] | None | List of trusted node IDs (defaults to entry points) | None |
top_k | int | Number of top results to return | 10 |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | List of dictionaries with node info and trust score |
Source code in src/code_context_agent/tools/graph/analysis.py
find_entry_points ¶
Find likely entry points in the code.
Entry points are nodes with no incoming call edges but outgoing calls - they initiate execution flow.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | List of dictionaries with entry point node info |
Source code in src/code_context_agent/tools/graph/analysis.py
detect_modules ¶
Detect logical modules using Louvain community detection.
Uses the Louvain algorithm to find communities of densely connected code elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolution | float | Clustering resolution (< 1 = larger clusters, > 1 = smaller) | 1.0 |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | List of module dictionaries with members and metrics |
Source code in src/code_context_agent/tools/graph/analysis.py
find_clusters_by_pattern ¶
Find clusters of nodes matching a specific AST-grep rule.
Groups nodes by their rule_id metadata to find related business logic patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule_id | str | The rule identifier to filter by | required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | List of matching nodes grouped by file |
Source code in src/code_context_agent/tools/graph/analysis.py
find_clusters_by_category ¶
Find all nodes matching a business logic category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category | str | Category to filter by (e.g., "db", "auth", "http") | required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | List of matching nodes with their locations |
Source code in src/code_context_agent/tools/graph/analysis.py
find_triangles ¶
Find tightly-coupled code triads using triangle detection.
Triangles in the call/import graph indicate three pieces of code that all depend on each other — potential cohesion or coupling issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
top_k | int | Maximum number of triangles to return | 10 |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | List of triangle dictionaries with the three node IDs |
Source code in src/code_context_agent/tools/graph/analysis.py
get_similar_nodes ¶
Find nodes similar to a given node based on graph structure.
Uses personalized PageRank to find nodes closely related to the target node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id | str | The node to find similar nodes for | required |
top_k | int | Number of similar nodes to return | 5 |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | List of similar nodes with similarity scores |
Source code in src/code_context_agent/tools/graph/analysis.py
calculate_coupling ¶
Calculate coupling strength between two nodes.
Considers shared neighbors, direct edges, and path length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_a | str | First node ID | required |
node_b | str | Second node ID | required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary with coupling metrics |
Source code in src/code_context_agent/tools/graph/analysis.py
get_dependency_chain ¶
Get the dependency chain from/to a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id | str | Starting node | required |
direction | str | "outgoing" (what this depends on) or "incoming" (what depends on this) | 'outgoing' |
max_depth | int | Maximum depth to traverse | 5 |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary with nodes and edges in the chain |
Source code in src/code_context_agent/tools/graph/analysis.py
find_unused_symbols ¶
Find symbols with zero incoming cross-file references.
Identifies functions, classes, and methods that are defined but never referenced from other files — dead code candidates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_types | list[str] | None | Filter to specific types (default: function, class, method) | None |
exclude_patterns | list[str] | None | Regex patterns to exclude from results | None |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | List of unused symbol dicts with id, name, file_path, node_type |
Source code in src/code_context_agent/tools/graph/analysis.py
find_refactoring_candidates ¶
Identify refactoring opportunities by combining multiple signals.
Combines: - Clone pairs (SIMILAR_TO edges) -> "extract shared helper" - Code smell pattern matches (rule_id contains "code_smell") -> structural issues - Unused symbols -> "dead code removal"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
top_k | int | Maximum number of candidates to return | 10 |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | Ranked list of refactoring candidates with type, files, and rationale. |
Source code in src/code_context_agent/tools/graph/analysis.py
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | |