A sophisticated AI-powered code review system using Claude with specialized sub-agents for comprehensive analysis.
- 9 Specialized Sub-Agents analyzing different aspects:
- CorrectnessAgent - Logical errors and bugs
- SecurityAgent - Security vulnerabilities
- PerformanceAgent - Performance bottlenecks
- StyleAgent - Code readability and style
- RobustnessAgent - Error handling and resilience
- StructureAgent - Architecture and design patterns
- TestingAgent - Test quality and completeness
- CoverageAgent - Test coverage analysis
- DocumentationAgent - Documentation quality
- Automatically fixes failing tests through iterative analysis
- Intelligent stopping conditions to prevent infinite loops
- Service pattern architecture for testability
- Automatically refactors Python classes by extracting functions into service classes
- Identifies primary business logic function in each class
- Validation hooks ensure:
- Service classes are created properly
- No environment variable access in services
- External clients use interfaces, not concrete implementations
- Intelligent retry mechanism learns from past failures
- Iterative refactoring until all functions are properly extracted
The project uses dependency injection to make the code testable:
IClaudeService (interface)
├── ClaudeServiceImpl (production)
└── FakeClaudeService (testing)
This allows:
- Easy unit testing without API calls
- Mocking Claude responses
- Faster test execution
- Predictable test behavior
# Create venv and install all dependencies
make setup-venv
# Activate the virtual environment
source venv/bin/activate
# Run tests
make test# Install production dependencies
make install
# Install all dependencies (including test dependencies)
make install-dev
# Install and run tests
make all# Create virtual environment (recommended)
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
pip install -r requirements-test.txtThere are several ways to run these scripts from any location on your system:
# Code review from anywhere
/path/to/code-review-agent/venv/bin/python /path/to/code-review-agent/src/code_review_agent.py --file myfile.py
# Test fixer from anywhere
/path/to/code-review-agent/venv/bin/python /path/to/code-review-agent/src/test_fixer.pyAdd the project directory to your PATH in ~/.bashrc or ~/.zshrc:
# Add these lines to ~/.bashrc or ~/.zshrc
export CODE_REVIEW_AGENT="/path/to/code-review-agent"
export PATH="$PATH:$CODE_REVIEW_AGENT"
export PYTHONPATH="$PYTHONPATH:$CODE_REVIEW_AGENT"
# Create wrapper scripts in the project directory
# Then you can run: code-review --file myfile.pyAdd to ~/.bashrc or ~/.zshrc:
# Add these aliases
alias code-review='cd /path/to/code-review-agent && venv/bin/python src/code_review_agent.py'
alias test-fixer='cd /path/to/code-review-agent && venv/bin/python src/test_fixer.py'
# Usage:
# code-review --file ~/projects/myapp/src/main.py
# test-fixer ~/projects/myapp/tests/Create executable wrapper scripts that can be called from anywhere:
# Create ~/bin directory if it doesn't exist
mkdir -p ~/bin
# Create ~/bin/code-review
cat > ~/bin/code-review << 'EOF'
#!/bin/bash
SCRIPT_DIR="/path/to/code-review-agent"
cd "$SCRIPT_DIR" && venv/bin/python src/code_review_agent.py "$@"
EOF
# Create ~/bin/test-fixer
cat > ~/bin/test-fixer << 'EOF'
#!/bin/bash
SCRIPT_DIR="/path/to/code-review-agent"
cd "$SCRIPT_DIR" && venv/bin/python src/test_fixer.py "$@"
EOF
# Make scripts executable
chmod +x ~/bin/code-review
chmod +x ~/bin/test-fixer
# Add ~/bin to PATH if not already (in ~/.bashrc or ~/.zshrc)
export PATH="$HOME/bin:$PATH"
# Now you can run from anywhere:
# code-review --file ~/projects/myapp/main.py
# test-fixer ~/projects/myapp/tests/# Run make targets from anywhere
make -C /path/to/code-review-agent test
make -C /path/to/code-review-agent run-review-file FILE=/absolute/path/to/file.py
make -C /path/to/code-review-agent run-fixer-path PATH=/absolute/path/to/tests/
# Create a shell alias for convenience
alias code-review-make='make -C /path/to/code-review-agent'
# Then use: code-review-make test# Show all available commands
make help
# Install dependencies and run tests
make all
# Run tests
make test
# Run code review on a file
make run-review-file FILE=test_fixer.py
# Run test fixer
make run-fixer# Using Make (recommended)
make run-review # Interactive mode
make run-review-file FILE=path/to/file.py # Review specific file
# Using Python directly from project root
python src/code_review_agent.py --model sonnet
python src/code_review_agent.py --file path/to/file.py
python src/code_review_agent.py --file test_fixer.py --stats true
# Or with venv
venv/bin/python src/code_review_agent.py --model sonnet# Using Make (recommended)
make run-fixer # Fix tests in current directory
make run-fixer-path PATH=test_main.py # Fix specific test file
# Using Python directly from project root
python src/test_fixer.py # Fix tests in current directory
python src/test_fixer.py test_main.py # Fix specific test file
python src/test_fixer.py --max-iterations 10 # Custom safety limit
python src/test_fixer.py --model opus # Use different model
# Or with venv
venv/bin/python src/test_fixer.py# Refactor a Python class file
python src/refactoring_agent.py path/to/file.py
# With options
python src/refactoring_agent.py file.py --model opus # Use Claude Opus
python src/refactoring_agent.py file.py --max-iterations 30 # Limit iterations
python src/refactoring_agent.py file.py --verbose # Verbose output
python src/refactoring_agent.py file.py --dry-run # Preview changes
# Example: Refactor the sample file
python src/refactoring_agent.py examples/sample_class_to_refactor.py
# Or with venv
venv/bin/python src/refactoring_agent.py file.py- Analysis: The agent analyzes your class to understand its structure
- Primary Function: Identifies the main business logic function (not constructor)
- Extraction: Iteratively extracts other functions into service classes
- Validation: Each extraction is validated with hooks:
- Service class creation check
- No environment variables in services
- External clients use interfaces
- Retry Logic: Failed extractions are retried with knowledge of past failures
- Completion: Continues until all functions are properly refactored
The test fixer uses an intelligent, agent-driven approach instead of arbitrary limits.
After each fix attempt, Claude analyzes:
- Progress tracking: Are failures decreasing?
- Pattern detection: Are we repeating the same failed approaches?
- Strategy assessment: Do we have alternative strategies to try?
- Solvability analysis: Is this problem fixable with available information?
Based on this analysis, Claude decides whether to continue or stop.
CONTINUE: I'll try a different approach - using mock objects instead of
real dependencies, which should resolve the initialization errors.
or
STOP: We've attempted 3 different mocking strategies and the same
import error persists. This appears to be a dependency issue that
requires manual investigation of the project setup.
While the agent makes primary decisions, these safety conditions override:
- Success: All tests pass ✅
- Safety limit: 20 iterations (rarely hit, agent stops before this)
- Breaking changes: Test count drops significantly
- User interruption: Ctrl+C
✅ Intelligent decisions: Agent recognizes when it's stuck vs. when it has new ideas ✅ Learns from history: Previous attempts inform future strategies ✅ Avoids repetition: Won't try the same failed approach twice ✅ Natural stopping: Stops when truly stuck, not at arbitrary limits ✅ Better success rate: Can try more approaches when making progress
# Run all tests
make test
# Run with verbose output
make test-verbose
# Run with coverage report
make test-coverage
# Run specific test file
make test-specific FILE=test_main.py
# Run only code review agent tests
make test-main
# Run only test fixer tests
make test-fixer
# Show all available targets
make help# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test file
pytest test_main.py
# Run with coverage
pytest --cov=. --cov-report=htmlRun tests in an isolated Docker container:
# Build and run tests in one command
make docker-test
# Or build and run separately
make docker-build-test
docker run --rm code-review-agent-test
# Clean up Docker image
make docker-cleanThis ensures tests run in a clean, reproducible environment with all dependencies properly installed.
code-review-agent/
├── src/ # Source code
│ ├── __init__.py
│ ├── code_review_agent.py # Code review orchestrator
│ ├── test_fixer.py # Automatic test fixer
│ ├── refactoring_agent.py # Automatic code refactoring
│ ├── claude_service.py # Service interface & implementations
│ └── cli_tools.py # Rich console utilities
├── tests/ # Test files
│ ├── __init__.py
│ ├── test_main.py # Unit tests for code_review_agent
│ ├── test_test_fixer.py # Unit tests for test_fixer
│ └── test_refactoring_agent.py # Unit tests for refactoring_agent
├── examples/ # Example files
│ └── sample_class_to_refactor.py # Sample for refactoring demo
├── requirements.txt # Production dependencies
├── requirements-test.txt # Test dependencies
├── projects.db # Project tracking database
├── Makefile # Build automation
├── Dockerfile.test # Docker image for running tests
├── .dockerignore # Docker ignore patterns
├── .gitignore # Git ignore patterns
└── README.md # This file
The Makefile provides convenient shortcuts for common tasks:
make test- Run all testsmake test-verbose- Run tests with detailed outputmake test-coverage- Generate coverage reportmake test-main- Test code_review_agent onlymake test-fixer- Test test_fixer onlymake test-specific FILE=tests/<file>- Test specific file
make install- Install production dependenciesmake install-dev- Install all dependencies (including dev)make all- Clean, install, and test
make run-review- Start code review agent (interactive)make run-review-file FILE=<file>- Review specific filemake run-fixer- Run test fixer on current directorymake run-fixer-path PATH=<path>- Fix specific test path
make clean- Remove generated files (cache, coverage, etc.)make db-reset- Reset projects databasemake format- Format code with black (if installed)make lint- Lint code with flake8 (if installed)make check- Run lint + tests
make docker-build-test- Build Docker test imagemake docker-test- Build and run tests in Dockermake docker-clean- Remove Docker test image
make help- Show all available targets
import asyncio
from src.claude_service import FakeClaudeService
from src.code_review_agent import main
async def test_code_review():
# Create fake service with mock responses
mock_responses = [
{"type": "text", "content": "Analysis complete"}
]
fake_service = FakeClaudeService(mock_responses=mock_responses)
# Test main() with dependency injection
await main(claude_service=fake_service)
# Verify queries sent
queries = fake_service.get_queries()
assert len(queries) == 1
assert "review" in queries[0]
asyncio.run(test_code_review())- Main functions accept optional service parameters
- Enables testing without external dependencies
- Follows SOLID principles
IClaudeServicedefines clean contract- Multiple implementations for different use cases
- Easy to extend with new implementations
- Each agent has one specific focus area
- Service layer handles only Claude communication
- Clear separation of concerns
- Testability: Full unit test coverage without API calls
- Flexibility: Easy to swap implementations
- Maintainability: Clear interfaces and separation
- Reliability: Tests run fast and deterministically
- Extensibility: Simple to add new agent types
When adding new features:
- Define interfaces for external dependencies
- Create both real and fake implementations
- Write unit tests using fake implementations
- Document stopping conditions for loops
- Follow existing patterns for consistency
MIT License - feel free to use and modify as needed.