Available Refactoring Operations in code-review-graph: Rename Preview and Dead Code Detection
The code-review-graph repository exposes graph-driven refactoring operations—including rename preview and dead code detection—through its Model-Context-Protocol (MCP) server, enabling AI-assisted analysis of symbol dependencies and unreachable functions with minimal token usage.
The tirth8205/code-review-graph toolchain constructs a local-first knowledge graph stored in SQLite to power sophisticated refactoring analysis. By leveraging call relations and AST metadata captured in code_review_graph/refactor.py, the system performs precise impact calculations and orphan detection without re-parsing unchanged source files.
Graph-Driven Refactoring Architecture
The refactoring engine resides in code_review_graph/refactor.py and operates on the GraphStore SQLite database located at .code-review-graph/graph.db. Unlike conventional refactoring tools that rely on static file scanning, this implementation traverses the knowledge graph's edges—representing calls, imports, and inheritance—to calculate precise blast radii and symbol reachability.
The architecture leverages three core components:
code_review_graph/refactor.py– Contains the rename preview engine, dead-code detection algorithms, and suggestion logic.code_review_graph/graph.py– Provides theGraphStoreclass with methods for querying nodes, edges, and performing Full-Text Search (FTS5) indexing.code_review_graph/main.py– Registers refactoring capabilities as MCP tools, enabling AI agents to invoke graph analysis without loading full source files into context.
Rename Preview Operations
Rename preview leverages impact radius calculation to determine every location affected by a symbol rename. When targeting a function or class, the engine performs a breadth-first search (BFS) from the target node, traversing both forward edges (calls made by the function) and reverse edges (callers of the function) up to a configurable depth.
The analysis identifies:
- Direct callers – Functions invoking the target symbol.
- Transitive dependencies – Upstream and downstream code affected by signature changes.
- Import statements – Module-level imports that reference the symbol.
- Test coverage – Test functions that exercise the target code.
This graph traversal approach minimizes token usage by returning only affected node paths rather than entire file contents. The system queries the SQLite-backed graph to retrieve precise line numbers and file references, optimizing AI-assisted refactoring workflows.
Dead Code Detection
Dead code detection analyzes the call graph to identify orphan nodes—functions, methods, and classes with zero incoming edges from reachable entry points. The engine in code_review_graph/refactor.py excludes test files and framework entry points from the orphan calculation, then flags symbols that cannot be reached via any execution path starting from main(), CLI handlers, or API endpoints.
The detection algorithm:
- Builds a set of entry point nodes identified by decorators,
if __name__ == "__main__"blocks, or framework patterns. - Performs reverse BFS from all entry points to mark reachable nodes.
- Identifies function and class nodes with no incoming "calls" edges from the reachable set.
- Generates confidence scores based on export statements (e.g.,
__all__in Python) and dynamic import patterns.
This process runs against the indexed SQLite graph store, completing in seconds even for large codebases due to efficient edge lookups.
Integration with MCP Tools
The refactoring capabilities are exposed as Model-Context-Protocol (MCP) tools through the server defined in code_review_graph/main.py. The @mcp.tool() decorators register graph-driven refactoring functions that AI assistants can invoke with targeted parameters.
When processing a rename preview request, the server returns a compact JSON response:
{
"symbol": "auth.py::authenticate_user",
"affected_nodes": [
{"file": "api.py", "line": 34, "type": "call"},
{"file": "test_auth.py", "line": 56, "type": "reference"}
],
"token_estimate": 450
}
Dead code detection similarly returns structured data identifying orphan nodes:
{
"dead_symbols": [
{
"file": "utils.py",
"line": 45,
"name": "legacy_hash_function",
"incoming_calls": 0,
"confidence": 0.95
}
]
}
These responses enable AI agents to suggest precise refactoring actions while consuming approximately 90% fewer tokens than traditional full-file context approaches.
Incremental Update Support
Refactoring analysis benefits from the incremental parsing system in code_review_graph/incremental.py. When files change, the get_changed_files() function detects modifications via Git diff and triggers partial graph rebuilds that update only affected nodes and their immediate dependencies.
This ensures that rename preview and dead code detection queries always operate on current call graphs without requiring full codebase re-parses. The incremental approach maintains sub-second response times for refactoring queries even as repositories scale to thousands of files.
Summary
code_review_graph/refactor.pyimplements graph-driven rename preview and dead code detection using SQLite-backed call graphs.- Rename preview performs BFS traversal from target symbols to calculate precise blast radii, identifying all affected callers and dependencies.
- Dead code detection identifies orphan nodes with zero incoming call edges from reachable entry points, filtering out tests and framework boilerplate.
- MCP integration exposes these tools through
code_review_graph/main.py, enabling token-efficient AI-assisted refactoring workflows. - Incremental updates via
code_review_graph/incremental.pyensure analysis results stay current without re-parsing unchanged files.
Frequently Asked Questions
How does rename preview calculate the blast radius of a refactoring operation?
The rename preview engine in code_review_graph/refactor.py performs a bidirectional breadth-first search (BFS) from the target symbol node, traversing call edges and import relationships up to a configurable maximum depth. This graph traversal identifies all functions, classes, and modules that reference the symbol, providing a complete impact analysis without scanning unrelated source files.
What types of dead code can the detection engine identify?
The dead code detection system identifies functions, methods, and classes that have no incoming call edges from reachable entry points such as main() blocks, CLI handlers, or API endpoints. It filters out test files and considers export statements like __all__ to reduce false positives, focusing on production code that is never executed in any valid program path.
Are refactoring operations available via CLI or only through the MCP server?
While the repository provides CLI commands like code-review-graph build and code-review-graph query-graph for graph management, the specific refactoring operations—rename preview and dead code detection—are primarily exposed as MCP tools through code_review_graph/main.py. This design prioritizes integration with AI coding assistants, though the underlying functions in refactor.py can be invoked programmatically via Python.
How does the system ensure dead code detection accuracy during incremental updates?
The code_review_graph/incremental.py module detects changed files using Git diff and triggers partial graph rebuilds that update only modified nodes and their immediate dependencies. This ensures that dead code detection queries in refactor.py operate on current call graphs without requiring full codebase re-parses, maintaining both accuracy and performance as the repository evolves.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →